Is RenderTexture simultaneous writing and sampling possible?


I’ve been struggling with this one for a while:
I have a camera rendering a scene into a render texture. This rendering uses Unity’s shader replacement mechanism to render objects with a custom shader. This pixel shader first samples this render texture (the very same texture the camera is rendering to) and then modulate the sampled pixel color (paint application). This results in kind of an incremental modification of the render texture.
Everything works as expected with OpenGL, but D3D(11) seems a little bit more picky: looks like the texture sampled is… just another arbitrary texture.

Hope this makes sense, kinda difficult to explain.

I of course can provide source code if relevant.

Short answer: not really!

Longer answer: The behavior is probably undefined. You almost certainly won’t be simultaneously reading/writing, even if it appears to work. However depending on the underlying implementation you may be getting away with reading from last-frame’s result while writing to this frame’s one.

To do this correctly, look into how post effect shaders work in unity. It is entirely possible to define extra buffers and explicitly manage the saving of last frame’s result to be sampled from this frame. Here’s a great tutorial on the subject: