UnityRef is currently in early development. Some features may be incomplete and/or not functioning.

UNITYREF

Your Pit Stop For Solving ANYTHING in Unity

graphics

[URP Compute] Fix Ping-Pong Buffer Blurring Artifacts

Under Audit

urp

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.

Issue

 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.

Experimental Fixes
  • Verify RenderTextureDescriptor.autoGenerateMips is set to false and mipmapCount to 1 for render textures not intended to use mipmaps, or manage mipmap generation explicitly.
  • Ensure that the FilterMode of the RenderTexture and the SamplerState (_LinearRepeat in this case) match and are suitable for the intended sampling behavior, especially FilterMode.Point if no interpolation is desired.
  • Adjust UV calculations in the ComputeShader by adding a half-pixel offset (0.5f / width, 0.5f / height) to uv if SampleLevel is causing off-by-one pixel issues with SV_DispatchThreadID.

Editor's Note:

The above fixes have not been verified by our audit team yet. They are provided exclusively for your own technical research. We recommend creating a backup of your project before proceeding with any attempts. Utilize at your own discretion!

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.