Access Structured Buffer like a texture

Started by
5 comments, last by Migi0027 10 years, 1 month ago

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

FastCall22: "I want to make the distinction that my laptop is a whore-box that connects to different network"

Blog about... stuff (GDNet, WordPress): www.gamedev.net/blog/1882-the-cuboid-zone/, cuboidzone.wordpress.com/

Advertisement

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!

Cheers to you too!

Thanks for the tip! Though the problem is still there biggrin.png .

FastCall22: "I want to make the distinction that my laptop is a whore-box that connects to different network"

Blog about... stuff (GDNet, WordPress): www.gamedev.net/blog/1882-the-cuboid-zone/, cuboidzone.wordpress.com/

bOutput[i] = gb;

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

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.

FastCall22: "I want to make the distinction that my laptop is a whore-box that connects to different network"

Blog about... stuff (GDNet, WordPress): www.gamedev.net/blog/1882-the-cuboid-zone/, cuboidzone.wordpress.com/

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

Cheers!

I had a topic on exactly that a day ago, should have linked it: http://www.gamedev.net/topic/654496-structured-buffer-not-being-written-to/

FastCall22: "I want to make the distinction that my laptop is a whore-box that connects to different network"

Blog about... stuff (GDNet, WordPress): www.gamedev.net/blog/1882-the-cuboid-zone/, cuboidzone.wordpress.com/

This topic is closed to new replies.

Advertisement