Sign in to follow this  

Access Structured Buffer like a texture

This topic is 1400 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Simple question,

 

if I wanted to access a structured buffer ( 1D Array, count of screen_width*screen_height ) as a 2d buffer, what I'd do is the following:

int index = y * screen_width + x;

 
So in this case, it's a compute shader filling out the buffer, and the shader is given the local thread id ( id.xy ranges from 0 to the screen dimensions ): [ PS. Dispatch is called by Dispatch(screen_width/32, screen_height/32, 1)

[numthreads(32, 32, 1)]
void main( uint3 DTid : SV_DispatchThreadID )
{
	...
	int i = DTid.y * Screen.x + DTid.x;
	bOutput[i] = gb;
}

 
Now I hope I filled the buffer correctly, now the next thing is accessing the buffer in a pixel shader, using the texture coordinates ( screen space ), like so: [ PS. The texture coordinates .xy ranges from 0 to 1, it's a full screen quad ]

int index = (input.Tex.y*Screen.y) * Screen.x + (input.Tex.x*Screen.x);

 
So in this case wouldn't I just need to multiply the texture coordinates by the screen dimensions, and do the same as before?:

float4 PShader(VS_Output input) : SV_TARGET
{
        ....
	int x = input.Tex.x*Screen.x;
	int y = input.Tex.y*Screen.y;
	int i = y * Screen.x + x;
	return bGBuffer[i].positionWS;

 
So I know the reading and writing is working, got that tested, but the indexing is failing, and I'm currently receiving a fully black screen. ( I.e. nothing ).

 

Thanks for your time!

 

Anyone know what I've done wrong this time?

-MIGU0027

Edited by Migi0027

Share this post


Link to post
Share on other sites

You could use the position with SV_POSITION from your VS_Output structure to get the exact pixel coordinates without needing to multiply by the screen resolution.

 

Such as:

 

int x = input.Pos.x;

int y = input.Pos.y;

int i = y * Screen.x + x;

 

assuming that the Pos is the position in the normalized device coordinates and is defined with SV_POSITION semantic in the VS_Output structure.

 

Cheers!

Share this post


Link to post
Share on other sites
bOutput[i] = gb;

 

Shouldn't it be something like bOutput[i].color = gb; or i'm missing something?

Edited by imoogiBG

Share this post


Link to post
Share on other sites

bOutput is a structured buffer, this is how it's treated in the compute shader:
 

struct GB
{
	// World Space Positions
	float4 positionWS;

	// World Space normals
	float4 normalWS;

	// Roughness
	float roughness;

	// Special Post Processing Flag
	float post_flag;
};

// Output Buffer
RWStructuredBuffer<GB> bOutput : register(u0);

[numthreads(32, 32, 1)]
void main( uint3 DTid : SV_DispatchThreadID )
{
	// New GB Instance
	GB gb = (GB)0;
	gb.positionWS = float4(tPosXYZ[DTid.xy].xyz,1);
	gb.positionWS = float4(1, 1, 1, 1); // Testing
	gb.normalWS = float4(1,0,0,1);
	gb.roughness = 1;
	gb.post_flag = 1;
	
	int i = DTid.y * Screen.x + DTid.x;
	bOutput[i] = gb;
}

Sorry, should have posted this.

Edited by Migi0027

Share this post


Link to post
Share on other sites

Maybe you should show a bit more code. Have you bound your structured buffer correctly? Is it created correctly?

 

Cheers!

Share this post


Link to post
Share on other sites

This topic is 1400 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this