Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


R8G8B8A8 texture format in Compute Shader


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 maxest   Members   -  Reputation: 425

Like
0Likes
Like

Posted 01 July 2011 - 01:00 PM

Hey,

I wanna use a 32-bit R8G8B8A8 texture format. I want to write data to it in compute shader and then read that data in pixel shader, but I can't get it working at all. First of all I cannot use format like DXGI_FORMAT_R8G8B8A8_UNORM because it's not possible to specify appropriate RWTexture2D in compute shader. So I decided to use DXGI_FORMAT_R8G8B8A8_UINT format, pack by 8-bit components to that in compute, and read in pixel shader. No success.

Packing code:
RWTexture2D<uint> texture: register(u0);

[numthreads(16, 16, 1)]
void main(uint3 dtID: SV_DispatchThreadID)
{
	uint r = 255;
	uint g = 255 << 8;
	uint b = 255 << 16;
	uint a = 255 << 24;
	texture[int2(dtID.x, dtID.y)] = r | g | b | a;
}

Unpacking code:
...

PS_OUTPUT main(PS_INPUT input)
{
	PS_OUTPUT output;

	uint value = texture.Load(int3(0, 0, 0));
	uint r = (value & 0x000000FF);
	uint g = (value & 0x0000FF00) >> 8;
	uint b = (value & 0x00FF0000) >> 16;
	uint a = (value & 0xFF000000) >> 24;

	output.color = float4(r, g, b, a) / 255.0f;

	return output;
}

Is there anything wrong I'm doing? Isn't there a simple way for four 8-bit components treatment in compute shaders?

Sponsor:

#2 BearishSun   Members   -  Reputation: 473

Like
0Likes
Like

Posted 01 July 2011 - 01:28 PM

What kind of output do you get when you run those shaders?

#3 n3Xus   Members   -  Reputation: 774

Like
1Likes
Like

Posted 01 July 2011 - 01:33 PM

Try changing the format from DXGI_FORMAT_R8G8B8A8_UINT to DXGI_FORMAT_R32_UINT, since you declare your texture as a uint in the shader.

#4 MJP   Moderators   -  Reputation: 14016

Like
1Likes
Like

Posted 01 July 2011 - 03:44 PM

If you just want to write to a R8G8B8A8_UNORM through a UAV, you can do that and it works fine. Just declare a RWTexture2D<float4>, and write float4's to the texture. However you can't read the values through a UAV, only through a SRV. If you need to read and write in the same shader, the workaround is to use R32_UINT, and then use the functions in D3DX_DXGIFormatConvert.inl to manually pack and unpack the values.

#5 maxest   Members   -  Reputation: 425

Like
0Likes
Like

Posted 02 July 2011 - 04:25 AM

Guys... THANK YOU! It was driving me crazy and all your ideas worked!
Obviously, for packing I needed R32_UINT rather than R8G8B8A8_UINT - thanks n3Xus! And to answer question of Bearish Sun, I got red color. What is obvious as I was writing to this one channel only.
And finally thank MJP for that float4. Actually I could have guessed that since UNORM is... well... normalized format. I'm aware of that compute shader write/read restriction, just couldn't get that writing in CS and reading in PS right ;).
Again, thank you guys!




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS