Using RWTexture2D<float4> in vertex fragment shaders

I couldn’t get the commandBuffer.DrawMesh to behave right, it was flickery and artifacty.

But one solution, in which you can write to RWTextures from a vert frag shader in a simple material on a game object, no command buffers, is like this:

m_paintAccumulationRT = new RenderTexture(rtWH_x, rtWH_y, 24, RenderTextureFormat.ARGB32);// Must be ARGB32 but will get automagically converted to float or float4 or int or half, from your shader code declaration.
m_paintAccumulationRT.name = _MainTexInternal;
m_paintAccumulationRT.enableRandomWrite = true;
m_paintAccumulationRT.Create();

m_PaintAccumulator_mat.SetTexture(_MainTexInternal, m_paintAccumulationRT);
Graphics.ClearRandomWriteTargets();
Graphics.SetRandomWriteTarget(2, m_paintAccumulationRT);//with `, true);` it doesn't take RTs
// this is the Setup function, no need to do this every frame

The SetRandomWriteTarget function does not take RenderTargetIdentifiers, and the 3rd bool parameter hacks mentioned here How to write to an unordered access compute buffer from a pixel shader? are no longer valid (at least not for RenderTextures).
The number 2 must match the uav number you defined in the shader uniform RWTexture2D<float4> _MainTexInternal : register(u2);

That’s it. You should now be able to read and write to RWTextures from your shader. Both read and write syntax looks like this: _MainTexInternal[int2(int((mainUV.x)*2048),int((mainUV.y)*2048))].

3 Likes