Forum

CS how to share data inbetween threads?

hi all,

I know this is a shader pipeline thing with bound resources and one thread not knowing what the other one does.

However, I tried writing my current data to another RWTexture2D with the same dimensions as my shader and reading from it later on. The compiler did not throw an error but it did not work.

uint2 this = tid.xy;
uint2 next = tid.xy+uint2(1,0);
tex2[tid.xy] = col;
col.r = tex2[this].r + tex2[next].r;

I thought I could access textures like arrays with xy? Is there another way to do this?

You need to use Load4 for textures like that
The code above valid only for buffers…
Also Load is bit expensive on my taste, I’ve would recommend using samplelevel and generate texcoords…

RWTexture2D texOut : BACKBUFFER;
RWTexture2D tex;
-code-
tex[tid.xy] = col;
col = float4(0,0,0,0);
col = tex.Load(tid.xy);
//col = tex[tid.xy]; //this works, so the data got written
texOut[tid.xy] = col;

no error, but no output either. To read and write to the texture is has to bebound from a UAV. Do I have to declare that somewhere explicitly? I also found an article claiming that in compute load from UAV is just allowed for DXGI_FORMAT_R32_FLOAT. I tried it with an RWTexture2d < float > but no output either.

I also tried your second suggestion, but the RWTexture2D has no SampleLevel or Sample function, it works just for Texture2D and I can’t write to that resource type.

To write from CS to texture you need specific setup, normally you’d write from texture to buffer, e.g.:
Use Load4 or SampleLevel to read from texture
Use [] to read write from buffer

For writing from compute to texture you need:

  1. Render TempTarget
  2. Reading and writing:
Texture2D InputTexture;
RWTexture2D<float4> RWOutputTexture : BACKBUFFER;

[numthreads(1,1,1)]
void CS_Simple(uint3 tid : SV_DispatchThreadID)
{
    RWOutputTexture[tid.xy] = InputTexture.Load(int3(tid.x, tid.y,0));
}

technique11 Simple
{
	pass P0
	{
		SetComputeShader( CompileShader( cs_5_0, CS_Simple() ) );
	}
}

P.s. Seems Load4 is bit lower level function to access bytes directly, Load is the one you need.

CS_ToTexture.zip (2.8 KB)

1 Like