Writing AND Reading From a RWTexture2D Doesn't Work?

Hello,

I am trying to use a compute shader with RWTexture2D in order to modify an existing texture. It appears any time I read to and write from a RWTexture2D, it fails on NVidia hardware (but works fine on AMD). The RenderTextures are created with enableRandomWrite and it appears my setup is correct as I can either read OR write to the RWTexture2D, I just cannot do both at the same time (which is really what I need to do). Here is a super simple example of a shader that fails on NVidia:

#pragma kernel MainExample

RWTexture2D<float4>    _rwTexture;

[numthreads(42, 24, 1)]
void MainExample(uint3 id : SV_DispatchThreadID, uint groupIndex : SV_GroupIndex)
{
    // Brighten up the texture, it should now be pure white...
    _rwTexture[id.xy] += float4(0.5, 0.5, 0.5, 0.0);
}

Again, this works fine on AMD. The DX11 docs say that any DX11-level card (SM5) should be able to do this (read and write from the same texture) and the cards I’m testing are DX11/SM5 (NVidia GTX 975M and 1050 Ti). Any clues to what is going wrong?

I’m going to respond to my own post as someone on Reddit has figured out what’s going on. On certain hardware, you cannot use float4 in compute shaders. You can only use textures of a 32-bit scalar type (int, uint, float). The solution is to pack the ARGB into an R Int texture (32-bit integer texture). Here is the relevant post with some helper code to do this.

1 Like