I’ve been trying to blur a depth buffer based on the Universal RP DoF renderpass.
The depth texture has been successfully blurred but now I’m trying to blit result back to the depth buffer.
The problem is that cmd.blit binds the destination depth buffer as a depth/stencil buffer rather than as the color RT. Is this intended behavior ?
I tried working around the problem by rendering a fullscreen quad by hand but I’m again having trouble binding the depth buffer as a color RT.
void Render(CommandBuffer cmd, ref RenderingData renderingData)
{
ref var cameraData = ref renderingData.cameraData;
int wh = m_Descriptor.width;// / 2;
int hh = m_Descriptor.height;// / 2;
// Assumes a radius of 1 is 1 at 1080p
// Past a certain radius our gaussian kernel will look very bad so we'll clamp it for
// very high resolutions (4K+).
float maxRadius = BlurRadius * (wh / 1080f);
maxRadius = Mathf.Min(maxRadius, 5f);
m_Materials.m_ExpandDepthMaterial.SetFloat(ShaderConstants._MaxRadius, maxRadius);
// Temporary textures
cmd.GetTemporaryRT(ShaderConstants._PingTexture, GetStereoCompatibleDescriptor(wh, hh, m_DefaultDepthFormat), FilterMode.Bilinear);
cmd.GetTemporaryRT(ShaderConstants._PongTexture, GetStereoCompatibleDescriptor(wh, hh, m_DefaultDepthFormat), FilterMode.Bilinear);
// Blur
cmd.SetGlobalTexture(ShaderConstants._SourceTexture, source.id);
cmd.Blit(source.id, ShaderConstants._PingTexture, m_Materials.m_ExpandDepthMaterial, 0);
cmd.Blit(ShaderConstants._PingTexture, ShaderConstants._PongTexture, m_Materials.m_ExpandDepthMaterial, 1);
// Levels
cmd.Blit(ShaderConstants._PongTexture, ShaderConstants._PingTexture, m_Materials.m_ExpandDepthMaterial, 2);
// source.id gets bound to the Depth/Stencil slot rather than as RT0
//cmd.Blit(ShaderConstants._PongTexture, source.id, m_Materials.m_ExpandDepthMaterial, 2);
// Error: Graphics.CopyTexture called with null source texture
// cmd.ConvertTexture(ShaderConstants._PingTexture, source.id);
// Both _MainTex and RT0 remain unbound
cmd.SetViewProjectionMatrices(Matrix4x4.identity, Matrix4x4.identity);
cmd.SetViewport(cameraData.camera.pixelRect);
cmd.SetGlobalTexture(ShaderConstants._SourceTexture, ShaderConstants._PingTexture);
// Force pongtexture as the DS to avoid source.id being used.
cmd.SetRenderTarget(source.id, (RenderTargetIdentifier)ShaderConstants._PongTexture);
cmd.DrawMesh(RenderingUtils.fullscreenMesh, Matrix4x4.identity, m_Materials.m_ExpandDepthMaterial, 0, 3);
// Cleanup
cmd.ReleaseTemporaryRT(ShaderConstants._PingTexture);
cmd.ReleaseTemporaryRT(ShaderConstants._PongTexture);
}