[URP Compute] Fix Ping-Pong Buffer Blurring Artifacts
Under Audit
Unity 2021.2.x - Unity 6.x.x
Published Wed, Mar 25
Implementing a ping-pong buffer with ComputeShader in a ScriptableRendererFeature can lead to unexpected image translation and blurring when using Texture2D.SampleLevel.
A ping-pong buffer implementation within your ScriptableRendererFeature using a ComputeShader experiences undesired image translation and blurring. When the ComputeShader attempts to use its own output as an input for subsequent dispatches via Texture2D.**SampleLevel**, the rendered image gradually shifts towards the screen's right corner and becomes increasingly blurred. This issue does not occur when directly copying pixel data using writer[id.xy] = reader[id.xy], indicating a problem related to texture sampling or UV coordinate generation within the ComputeShader. The RenderTextureDescriptor is configured with msaaSamples = 1, depthBufferBits = 0, enableRandomWrite = true, FilterMode.Bilinear, and TextureWrapMode.Repeat. The ComputeShader uses _LinearRepeat SamplerState and Texture2D.**SampleLevel** for sampling. The observed behavior suggests potential issues with mipmapping or how SampleLevel interprets UVs.
- Verify
RenderTextureDescriptor.autoGenerateMipsis set tofalseandmipmapCountto1for render textures not intended to use mipmaps, or manage mipmap generation explicitly. - Ensure that the
FilterModeof theRenderTextureand theSamplerState(_LinearRepeatin this case) match and are suitable for the intended sampling behavior, especiallyFilterMode.Pointif no interpolation is desired. - Adjust UV calculations in the
ComputeShaderby adding a half-pixel offset (0.5f / width,0.5f / height) touvifSampleLevelis causing off-by-one pixel issues withSV_DispatchThreadID.
Editor's Note:
Related Posts Haven't quite found a solution to your problem? We think these posts might help you.
Content inspired by a Unity discussion post.