浏览代码

[DepthPyramid] Refactored depth pyramid

/feature-ScreenSpaceProjection
Frédéric Vauchelles 7 年前
当前提交
e4b8c57a
共有 4 个文件被更改,包括 155 次插入72 次删除
  1. 79
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/HDRenderPipeline.cs
  2. 9
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/RenderPipelineResources/DepthDownsample.compute
  3. 128
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/RenderPipelineResources/DepthPyramid.cs
  4. 11
      ScriptableRenderPipeline/HDRenderPipeline/HDRP/RenderPipelineResources/DepthPyramid.cs.meta

79
ScriptableRenderPipeline/HDRenderPipeline/HDRP/HDRenderPipeline.cs


RendererConfiguration m_currentRendererConfigurationBakedLighting = HDUtils.k_RendererConfigurationBakedLighting;
Material m_CopyStencilForNoLighting;
GPUCopy m_GPUCopy;
DepthPyramid m_DepthPyramid;
ComputeShader m_DepthPyramidCS { get { return m_Asset.renderPipelineResources.depthPyramidCS; } }
int m_DepthPyramidKernel;
ComputeShader m_applyDistortionCS { get { return m_Asset.renderPipelineResources.applyDistortionCS; } }
int m_applyDistortionKernel;

readonly RenderTargetIdentifier m_GaussianPyramidColorBufferRT;
readonly RenderTargetIdentifier m_DepthPyramidBufferRT;
RenderTextureDescriptor m_GaussianPyramidColorBufferDesc;
RenderTextureDescriptor m_DepthPyramidBufferDesc;
readonly RenderTargetIdentifier m_DeferredShadowBufferRT;

m_Asset = asset;
m_GPUCopy = new GPUCopy(asset.renderPipelineResources.copyChannelCS);
m_DepthPyramid = new DepthPyramid(asset.renderPipelineResources.depthPyramidCS, m_GPUCopy, HDShaderIDs._DepthPyramidMips);
EncodeBC6H.DefaultInstance = EncodeBC6H.DefaultInstance ?? new EncodeBC6H(asset.renderPipelineResources.encodeBC6HCS);
m_ReflectionProbeCullResults = new ReflectionProbeCullResults(asset.reflectionSystemParameters);

autoGenerateMips = false
};
m_DepthPyramidKernel = m_DepthPyramidCS.FindKernel("KMain");
m_DepthPyramidBufferDesc = new RenderTextureDescriptor(2, 2, RenderTextureFormat.RFloat, 0)
{
useMipMap = true,
autoGenerateMips = false
};
m_DeferredShadowBuffer = HDShaderIDs._DeferredShadowTexture;
m_DeferredShadowBufferRT = new RenderTargetIdentifier(m_DeferredShadowBuffer);

if (!m_FrameSettings.enableRoughRefraction)
return;
using (new ProfilingSample(cmd, "Pyramid Depth", CustomSamplerId.PyramidDepth.GetSampler()))
{
var depthPyramidDesc = m_DepthPyramidBufferDesc;
var minSize = Mathf.Min(depthPyramidDesc.width, depthPyramidDesc.height);
var lodCount = Mathf.FloorToInt(Mathf.Log(minSize, 2f));
if (lodCount > HDShaderIDs._DepthPyramidMips.Length)
{
Debug.LogWarningFormat("Cannot compute all mipmaps of the depth pyramid, max texture size supported: {0}", (2 << HDShaderIDs._DepthPyramidMips.Length).ToString());
lodCount = HDShaderIDs._DepthPyramidMips.Length;
}
cmd.SetGlobalVector(HDShaderIDs._DepthPyramidMipSize, new Vector4(depthPyramidDesc.width, depthPyramidDesc.height, lodCount, 0));
cmd.ReleaseTemporaryRT(HDShaderIDs._DepthPyramidMips[0]);
depthPyramidDesc.sRGB = false;
depthPyramidDesc.enableRandomWrite = true;
depthPyramidDesc.useMipMap = false;
cmd.GetTemporaryRT(HDShaderIDs._DepthPyramidMips[0], depthPyramidDesc, FilterMode.Bilinear);
m_GPUCopy.SampleCopyChannel_xyzw2x(cmd, GetDepthTexture(), HDShaderIDs._DepthPyramidMips[0], new Vector2(depthPyramidDesc.width, depthPyramidDesc.height));
cmd.CopyTexture(HDShaderIDs._DepthPyramidMips[0], 0, 0, m_DepthPyramidBufferRT, 0, 0);
m_DepthPyramid.RenderPyramidDepth(hdCamera, cmd, renderContext, GetDepthTexture(), m_DepthPyramidBufferRT);
var depthSize = new Vector4(m_DepthPyramid.renderTextureDescriptor.width, m_DepthPyramid.renderTextureDescriptor.height, m_DepthPyramid.usedMipMapCount, 0);
cmd.SetGlobalVector(HDShaderIDs._DepthPyramidMipSize, depthSize);
PushFullScreenDebugDepthMip(cmd, m_DepthPyramidBufferRT, m_DepthPyramid.usedMipMapCount, m_DepthPyramid.renderTextureDescriptor, hdCamera, debugMode);
for (var i = 0; i < lodCount; i++)
{
//var sourceTarget = i == 0
// ? m_DepthPyramidBufferRT
// : HDShaderIDs._DepthPyramidMips[i];
var srcMipWidth = depthPyramidDesc.width;
var srcMipHeight = depthPyramidDesc.height;
depthPyramidDesc.width = srcMipWidth >> 1;
depthPyramidDesc.height = srcMipHeight >> 1;
cmd.ReleaseTemporaryRT(HDShaderIDs._DepthPyramidMips[i + 1]);
cmd.GetTemporaryRT(HDShaderIDs._DepthPyramidMips[i + 1], depthPyramidDesc, FilterMode.Bilinear);
cmd.SetComputeTextureParam(m_DepthPyramidCS, m_DepthPyramidKernel, "_Source", HDShaderIDs._DepthPyramidMips[i]);
cmd.SetComputeTextureParam(m_DepthPyramidCS, m_DepthPyramidKernel, "_Result", HDShaderIDs._DepthPyramidMips[i + 1]);
cmd.SetComputeVectorParam(m_DepthPyramidCS, "_SrcSize", new Vector4(srcMipWidth, srcMipHeight, 1f / srcMipWidth, 1f / srcMipHeight));
cmd.DispatchCompute(
m_DepthPyramidCS,
m_DepthPyramidKernel,
Mathf.CeilToInt(depthPyramidDesc.width / 8f),
Mathf.CeilToInt(depthPyramidDesc.height / 8f),
1);
cmd.CopyTexture(HDShaderIDs._DepthPyramidMips[i + 1], 0, 0, m_DepthPyramidBufferRT, 0, i + 1);
}
PushFullScreenDebugDepthMip(cmd, m_DepthPyramidBufferRT, lodCount, m_DepthPyramidBufferDesc, hdCamera, debugMode);
cmd.SetGlobalTexture(HDShaderIDs._PyramidDepthTexture, m_DepthPyramidBuffer);
for (int i = 0; i < lodCount + 1; i++)
cmd.ReleaseTemporaryRT(HDShaderIDs._DepthPyramidMips[i]);
}
cmd.SetGlobalTexture(HDShaderIDs._PyramidDepthTexture, m_DepthPyramidBuffer);
}
void RenderPostProcess(HDCamera hdcamera, CommandBuffer cmd, PostProcessLayer layer)

cmd.ReleaseTemporaryRT(m_GaussianPyramidColorBuffer);
cmd.GetTemporaryRT(m_GaussianPyramidColorBuffer, m_GaussianPyramidColorBufferDesc, FilterMode.Trilinear);
m_DepthPyramidBufferDesc = BuildPyramidDescriptor(hdCamera, PyramidType.Depth, m_FrameSettings.enableStereo);
m_DepthPyramid.Initialize(hdCamera, m_FrameSettings.enableStereo);
cmd.GetTemporaryRT(m_DepthPyramidBuffer, m_DepthPyramidBufferDesc, FilterMode.Trilinear);
cmd.GetTemporaryRT(m_DepthPyramidBuffer, m_DepthPyramid.renderTextureDescriptor, FilterMode.Trilinear);
// End
if (!m_FrameSettings.enableForwardRenderingOnly)

9
ScriptableRenderPipeline/HDRenderPipeline/HDRP/RenderPipelineResources/DepthDownsample.compute


#include "CoreRP/ShaderLibrary/Common.hlsl"
#pragma kernel KMain_8 KERNEL_SIZE=8 KERNEL_NAME=KMain_8
#pragma kernel KMain_1 KERNEL_SIZE=1 KERNEL_NAME=KMain_1
Texture2D<float> _Source;
RWTexture2D<float> _Result;

float4 _SrcSize;
CBUFFER_END
#pragma kernel KMain
[numthreads(8, 8, 1)]
void KMain(uint2 groupId : SV_GroupID, uint2 groupThreadId : SV_GroupThreadID, uint2 dispatchThreadId : SV_DispatchThreadID)
[numthreads(KERNEL_SIZE, KERNEL_SIZE, 1)]
void KERNEL_NAME(uint2 groupId : SV_GroupID, uint2 groupThreadId : SV_GroupThreadID, uint2 dispatchThreadId : SV_DispatchThreadID)
{
// Upper-left pixel coordinate of quad that this thread will read
int2 threadUL = dispatchThreadId;

128
ScriptableRenderPipeline/HDRenderPipeline/HDRP/RenderPipelineResources/DepthPyramid.cs


using UnityEngine.Rendering;
namespace UnityEngine.Experimental.Rendering.HDPipeline
{
class DepthPyramid
{
const int k_DepthBlockSize = 4;
ComputeShader m_DepthPyramidCS;
GPUCopy m_GPUCopy;
RenderTextureDescriptor m_RenderTextureDescriptor;
int[] m_DepthPyramidMips = new int[0];
int m_DepthPyramidKernel_8;
int m_DepthPyramidKernel_1;
public RenderTextureDescriptor renderTextureDescriptor { get { return m_RenderTextureDescriptor; } }
public int usedMipMapCount { get { return Mathf.Min(bufferMipMapCount, m_DepthPyramidMips.Length); } }
public int bufferMipMapCount
{
get
{
var minSize = Mathf.Min(renderTextureDescriptor.width, renderTextureDescriptor.height);
return Mathf.FloorToInt(Mathf.Log(minSize, 2f));
}
}
public DepthPyramid(ComputeShader depthPyramidCS, GPUCopy gpuCopy, int[] mipIds)
{
m_DepthPyramidCS = depthPyramidCS;
m_GPUCopy = gpuCopy;
m_DepthPyramidKernel_8 = m_DepthPyramidCS.FindKernel("KMain_8");
m_DepthPyramidKernel_1 = m_DepthPyramidCS.FindKernel("KMain_1");
m_DepthPyramidMips = mipIds;
}
public void RenderPyramidDepth
(HDCamera hdCamera,
CommandBuffer cmd,
ScriptableRenderContext renderContext,
RenderTargetIdentifier depthTexture,
RenderTargetIdentifier targetTexture)
{
using (new ProfilingSample(cmd, "Pyramid Depth", CustomSamplerId.PyramidDepth.GetSampler()))
{
var depthPyramidDesc = m_RenderTextureDescriptor;
var lodCount = bufferMipMapCount;
if (lodCount > m_DepthPyramidMips.Length)
{
Debug.LogWarningFormat("Cannot compute all mipmaps of the depth pyramid, max texture size supported: {0}", (2 << m_DepthPyramidMips.Length).ToString());
lodCount = m_DepthPyramidMips.Length;
}
cmd.ReleaseTemporaryRT(m_DepthPyramidMips[0]);
depthPyramidDesc.sRGB = false;
depthPyramidDesc.enableRandomWrite = true;
depthPyramidDesc.useMipMap = false;
cmd.GetTemporaryRT(m_DepthPyramidMips[0], depthPyramidDesc, FilterMode.Bilinear);
m_GPUCopy.SampleCopyChannel_xyzw2x(cmd, depthTexture, m_DepthPyramidMips[0], new Vector2(depthPyramidDesc.width, depthPyramidDesc.height));
cmd.CopyTexture(m_DepthPyramidMips[0], 0, 0, targetTexture, 0, 0);
for (var i = 0; i < lodCount; i++)
{
var srcMipWidth = depthPyramidDesc.width;
var srcMipHeight = depthPyramidDesc.height;
depthPyramidDesc.width = srcMipWidth >> 1;
depthPyramidDesc.height = srcMipHeight >> 1;
var kernel = m_DepthPyramidKernel_8;
var kernelBlockSize = 8f;
if (depthPyramidDesc.width < 4 * k_DepthBlockSize
|| depthPyramidDesc.height < 4 * k_DepthBlockSize)
{
kernel = m_DepthPyramidKernel_1;
kernelBlockSize = 1;
}
cmd.ReleaseTemporaryRT(m_DepthPyramidMips[i + 1]);
cmd.GetTemporaryRT(m_DepthPyramidMips[i + 1], depthPyramidDesc, FilterMode.Bilinear);
cmd.SetComputeTextureParam(m_DepthPyramidCS, kernel, "_Source", m_DepthPyramidMips[i]);
cmd.SetComputeTextureParam(m_DepthPyramidCS, kernel, "_Result", m_DepthPyramidMips[i + 1]);
cmd.SetComputeVectorParam(m_DepthPyramidCS, "_SrcSize", new Vector4(srcMipWidth, srcMipHeight, 1f / srcMipWidth, 1f / srcMipHeight));
cmd.DispatchCompute(
m_DepthPyramidCS,
kernel,
Mathf.CeilToInt(depthPyramidDesc.width / kernelBlockSize),
Mathf.CeilToInt(depthPyramidDesc.height / kernelBlockSize),
1);
cmd.CopyTexture(m_DepthPyramidMips[i + 1], 0, 0, targetTexture, 0, i + 1);
}
for (int i = 0; i < lodCount + 1; i++)
cmd.ReleaseTemporaryRT(m_DepthPyramidMips[i]);
}
}
public void Initialize(HDCamera hdCamera, bool enableStereo)
{
var desc = hdCamera.renderTextureDesc;
desc.colorFormat = RenderTextureFormat.RFloat;
desc.depthBufferBits = 0;
desc.useMipMap = true;
desc.autoGenerateMips = false;
desc.msaaSamples = 1; // These are approximation textures, they don't need MSAA
// for stereo double-wide, each half of the texture will represent a single eye's pyramid
//var widthModifier = 1;
//if (stereoEnabled && (desc.dimension != TextureDimension.Tex2DArray))
// widthModifier = 2; // double-wide
//desc.width = pyramidSize * widthModifier;
desc.width = (int)hdCamera.screenSize.x;
desc.height = (int)hdCamera.screenSize.y;
m_RenderTextureDescriptor = desc;
}
}
}

11
ScriptableRenderPipeline/HDRenderPipeline/HDRP/RenderPipelineResources/DepthPyramid.cs.meta


fileFormatVersion: 2
guid: ecd07105781bbbf41b8918c61197d1e0
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存