Thomas
6 年前
当前提交
7ffc71c9
共有 12 个文件被更改,包括 252 次插入 和 189 次删除
-
6com.unity.render-pipelines.core/CoreRP/Textures/DepthBits.cs
-
4com.unity.render-pipelines.high-definition/HDRP/Camera/HDCameraFrameHistoryType.cs
-
82com.unity.render-pipelines.high-definition/HDRP/Material/GGXConvolution/RuntimeFilterIBL.cs
-
2com.unity.render-pipelines.high-definition/HDRP/RenderPipeline/HDCustomSamplerId.cs
-
93com.unity.render-pipelines.high-definition/HDRP/RenderPipeline/HDRenderPipeline.cs
-
4com.unity.render-pipelines.high-definition/HDRP/RenderPipeline/HDStringConstants.cs
-
4com.unity.render-pipelines.high-definition/HDRP/RenderPipelineResources/ApplyDistorsion.compute
-
6com.unity.render-pipelines.high-definition/HDRP/RenderPipelineResources/BufferPyramidProcessor.cs
-
41com.unity.render-pipelines.high-definition/HDRP/RenderPipelineResources/ColorPyramid.compute
-
62com.unity.render-pipelines.high-definition/HDRP/RenderPipelineResources/DepthPyramid.compute
-
126com.unity.render-pipelines.high-definition/HDRP/RenderPipelineResources/MipGenerator.cs
-
11com.unity.render-pipelines.high-definition/HDRP/RenderPipelineResources/MipGenerator.cs.meta
|
|||
using UnityEngine.Serialization; |
|||
|
|||
DepthPyramid, |
|||
ColorPyramid, |
|||
VolumetricLighting, |
|||
Count |
|||
} |
|
|||
#include "CoreRP/ShaderLibrary/Common.hlsl" |
|||
|
|||
// ------------------------------------------------ |
|||
// Algorithm |
|||
// ------------------------------------------------ |
|||
// Downsample a depth texture by taking min value of sampled pixels |
|||
|
|||
// ------------------------------------------------ |
|||
// Variants |
|||
// ------------------------------------------------ |
|||
|
|||
#pragma kernel KDepthDownSample8 KERNEL_SIZE=8 KERNEL_NAME=KDepthDownSample8 |
|||
#pragma kernel KDepthDownSample1 KERNEL_SIZE=1 KERNEL_NAME=KDepthDownSample1 |
|||
|
|||
// ------------------------------------------------ |
|||
// Texture buffers |
|||
// ------------------------------------------------ |
|||
#pragma kernel KDepthDownsample8DualUav KERNEL_SIZE=8 KERNEL_NAME=KDepthDownsample8DualUav |
|||
Texture2D<float2> _Source; |
|||
RW_TEXTURE2D(float2, _Result); |
|||
|
|||
SamplerState sampler_PointClamp; //TODO: could we use min-sampler instead of using ALU? |
|||
RW_TEXTURE2D(float, _Destination); |
|||
RW_TEXTURE2D(float, _Source); |
|||
// ------------------------------------------------ |
|||
// Constant buffers |
|||
// ------------------------------------------------ |
|||
float4 _SrcSize; |
|||
int2 _RectOffset; // Offset in source texture |
|||
float4 _Size; // x: src width, y: src height, zw: unused |
|||
// ------------------------------------------------ |
|||
// Kernel |
|||
// ------------------------------------------------ |
|||
|
|||
# define MAX_DEPTH(l, r) min(l, r) |
|||
# define MAX_DEPTH(l, r) max(l, r) |
|||
// Downsample a depth texture by taking the min value of sampled pixels |
|||
// The size of the dispatch is (DstMipSize / KernelSize). |
|||
void KERNEL_NAME(uint2 groupId : SV_GroupID, uint2 groupThreadId : SV_GroupThreadID, uint2 dispatchThreadId : SV_DispatchThreadID) |
|||
void KERNEL_NAME(uint2 dispatchThreadId : SV_DispatchThreadID) |
|||
uint2 srcPixelUL = _RectOffset + (dispatchThreadId << 1); |
|||
// Offset by 0.5 so sampling get the proper pixels |
|||
float2 offset = float2(srcPixelUL) + 0.5; |
|||
uint2 srcPixelUL = dispatchThreadId << 1; |
|||
#if defined(PLATFORM_SUPPORT_GATHER) |
|||
float4 depths = GATHER_RED_TEXTURE2D(_Source, sampler_PointClamp, offset * _SrcSize.zw).wzxy; |
|||
#else |
|||
float p00 = SAMPLE_TEXTURE2D_LOD(_Source, sampler_PointClamp, (offset) * _SrcSize.zw, 0.0).x; |
|||
float p10 = SAMPLE_TEXTURE2D_LOD(_Source, sampler_PointClamp, (offset + float2(1.0, 0.0)) * _SrcSize.zw, 0.0).x; |
|||
float p01 = SAMPLE_TEXTURE2D_LOD(_Source, sampler_PointClamp, (offset + float2(0.0, 1.0)) * _SrcSize.zw, 0.0).x; |
|||
float p11 = SAMPLE_TEXTURE2D_LOD(_Source, sampler_PointClamp, (offset + float2(1.0, 1.0)) * _SrcSize.zw, 0.0).x; |
|||
// '_Source' and '_Destination' are two different MIP levels of the same texture. |
|||
// TODO: Use Gather here instead of 4 loads |
|||
uint2 size = uint2(_Size.xy) - 1u; |
|||
float p00 = _Source[min(srcPixelUL + uint2(0u, 0u), size)]; |
|||
float p10 = _Source[min(srcPixelUL + uint2(1u, 0u), size)]; |
|||
float p01 = _Source[min(srcPixelUL + uint2(0u, 1u), size)]; |
|||
float p11 = _Source[min(srcPixelUL + uint2(1u, 1u), size)]; |
|||
#endif |
|||
float maxDepth = MAX_DEPTH(MAX_DEPTH(depths.x, depths.y), MAX_DEPTH(depths.z, depths.w)); |
|||
uint2 dstPixel = (_RectOffset >> 1) + dispatchThreadId; |
|||
_Result[dstPixel] = float2(minDepth, maxDepth); |
|||
_Destination[dispatchThreadId] = minDepth; |
|||
#undef MAX_DEPTH |
|
|||
using UnityEngine.Assertions; |
|||
using UnityEngine.Rendering; |
|||
|
|||
namespace UnityEngine.Experimental.Rendering.HDPipeline |
|||
{ |
|||
using RTHandle = RTHandleSystem.RTHandle; |
|||
|
|||
public class MipGenerator |
|||
{ |
|||
RTHandle m_TempFullscreenTarget; |
|||
|
|||
ComputeShader m_DepthPyramidCS; |
|||
ComputeShader m_ColorPyramidCS; |
|||
|
|||
int m_DepthDownsampleKernel; |
|||
int m_ColorDownsampleKernel; |
|||
int m_ColorGaussianKernel; |
|||
|
|||
public MipGenerator(HDRenderPipelineAsset asset) |
|||
{ |
|||
m_DepthPyramidCS = asset.renderPipelineResources.depthPyramidCS; |
|||
m_ColorPyramidCS = asset.renderPipelineResources.colorPyramidCS; |
|||
|
|||
m_DepthDownsampleKernel = m_DepthPyramidCS.FindKernel("KDepthDownsample8DualUav"); |
|||
m_ColorDownsampleKernel = m_ColorPyramidCS.FindKernel("KColorDownsample"); |
|||
m_ColorGaussianKernel = m_ColorPyramidCS.FindKernel("KColorGaussian"); |
|||
} |
|||
|
|||
public void Release() |
|||
{ |
|||
RTHandles.Release(m_TempFullscreenTarget); |
|||
m_TempFullscreenTarget = null; |
|||
} |
|||
|
|||
// Generates an in-place depth pyramid
|
|||
// Returns the number of generated mips
|
|||
// TODO: Mip-mapping depth is problematic for precision at lower mips, generate a packed atlas instead
|
|||
public int RenderMinDepthPyramid(CommandBuffer cmd, Vector2Int size, RTHandle texture) |
|||
{ |
|||
var cs = m_DepthPyramidCS; |
|||
int kernel = m_DepthDownsampleKernel; |
|||
int srcMipLevel = 0; |
|||
int srcMipWidth = size.x; |
|||
int srcMipHeight = size.y; |
|||
|
|||
// TODO: Do it 1x MIP at a time for now. In the future, do 4x MIPs per pass, or even use a single pass.
|
|||
// Note: Gather() doesn't take a LOD parameter and we cannot bind an SRV of a MIP level,
|
|||
// and we don't support Min samplers either. So we are forced to perform 4x loads.
|
|||
while (srcMipWidth >= 2 || srcMipHeight >= 2) |
|||
{ |
|||
int dstMipWidth = Mathf.Max(1, srcMipWidth >> 1); |
|||
int dstMipHeight = Mathf.Max(1, srcMipHeight >> 1); |
|||
|
|||
cmd.SetComputeVectorParam(cs, HDShaderIDs._Size, new Vector4(srcMipWidth, srcMipHeight, 0f, 0f)); |
|||
cmd.SetComputeTextureParam(cs, kernel, HDShaderIDs._Source, texture, srcMipLevel); |
|||
cmd.SetComputeTextureParam(cs, kernel, HDShaderIDs._Destination, texture, srcMipLevel + 1); |
|||
cmd.DispatchCompute(cs, kernel, (dstMipWidth + 7) / 8, (dstMipHeight + 7) / 8, 1); |
|||
|
|||
srcMipLevel++; |
|||
srcMipWidth = srcMipWidth >> 1; |
|||
srcMipHeight = srcMipHeight >> 1; |
|||
} |
|||
|
|||
return srcMipLevel - 1; |
|||
} |
|||
|
|||
// Generates an in-place gaussian pyramid
|
|||
// Returns the number of mips
|
|||
public int RenderColorGaussianPyramid(CommandBuffer cmd, Vector2Int size, RTHandle texture) |
|||
{ |
|||
return RenderColorGaussianPyramid(cmd, size, texture.rt); |
|||
} |
|||
|
|||
// Need this RenderTexture variant because some of the code in HDRP still hasn't switched to
|
|||
// the RTHandle system :(
|
|||
public int RenderColorGaussianPyramid(CommandBuffer cmd, Vector2Int size, RenderTexture texture) |
|||
{ |
|||
// Only create the temporary target on-demand in case the game doesn't actually need it
|
|||
if (m_TempFullscreenTarget == null) |
|||
{ |
|||
m_TempFullscreenTarget = RTHandles.Alloc( |
|||
Vector2.one * 0.5f, |
|||
filterMode: FilterMode.Bilinear, |
|||
colorFormat: RenderTextureFormat.ARGBHalf, |
|||
sRGB: false, |
|||
enableRandomWrite: true, |
|||
useMipMap: false, |
|||
enableMSAA: false, |
|||
name: "Temp Gaussian Pyramid Target" |
|||
); |
|||
} |
|||
|
|||
var cs = m_ColorPyramidCS; |
|||
int downsampleKernel = m_ColorDownsampleKernel; |
|||
int gaussianKernel = m_ColorGaussianKernel; |
|||
int srcMipLevel = 0; |
|||
int srcMipWidth = size.x; |
|||
int srcMipHeight = size.y; |
|||
|
|||
// Note: smaller mips are excluded as we don't need them and the gaussian compute works
|
|||
// on 8x8 blocks
|
|||
// TODO: Could be further optimized by merging the smaller mips to reduce the amount of dispatches
|
|||
while (srcMipWidth >= 2 || srcMipHeight >= 2) |
|||
{ |
|||
int dstMipWidth = Mathf.Max(1, srcMipWidth >> 1); |
|||
int dstMipHeight = Mathf.Max(1, srcMipHeight >> 1); |
|||
|
|||
cmd.SetComputeVectorParam(cs, HDShaderIDs._Size, new Vector4(srcMipWidth, srcMipHeight, 0f, 0f)); |
|||
cmd.SetComputeTextureParam(cs, downsampleKernel, HDShaderIDs._Source, texture, srcMipLevel); |
|||
cmd.SetComputeTextureParam(cs, downsampleKernel, HDShaderIDs._Destination, m_TempFullscreenTarget); |
|||
cmd.DispatchCompute(cs, downsampleKernel, (dstMipWidth + 7) / 8, (dstMipHeight + 7) / 8, 1); |
|||
|
|||
cmd.SetComputeVectorParam(cs, HDShaderIDs._Size, new Vector4(dstMipWidth, dstMipHeight, 0f, 0f)); |
|||
cmd.SetComputeTextureParam(cs, gaussianKernel, HDShaderIDs._Source, m_TempFullscreenTarget); |
|||
cmd.SetComputeTextureParam(cs, gaussianKernel, HDShaderIDs._Destination, texture, srcMipLevel + 1); |
|||
cmd.DispatchCompute(cs, gaussianKernel, (dstMipWidth + 7) / 8, (dstMipHeight + 7) / 8, 1); |
|||
|
|||
srcMipLevel++; |
|||
srcMipWidth = srcMipWidth >> 1; |
|||
srcMipHeight = srcMipHeight >> 1; |
|||
} |
|||
|
|||
return srcMipLevel - 1; |
|||
} |
|||
} |
|||
} |
|
|||
fileFormatVersion: 2 |
|||
guid: 9fb5527a68d5789439b1caea92b87bdf |
|||
MonoImporter: |
|||
externalObjects: {} |
|||
serializedVersion: 2 |
|||
defaultReferences: [] |
|||
executionOrder: 0 |
|||
icon: {instanceID: 0} |
|||
userData: |
|||
assetBundleName: |
|||
assetBundleVariant: |
撰写
预览
正在加载...
取消
保存
Reference in new issue