Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Access Structured Buffer like a texture


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
6 replies to this topic

#1 Migi0027   Crossbones+   -  Reputation: 2111

Like
0Likes
Like

Posted 17 March 2014 - 08:56 AM

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, 17 March 2014 - 08:57 AM.

Hi! Cuboid Zone
The Rule: Be polite, be professional, but have a plan to steal all their shaders!

Sponsor:

#2 kauna   Crossbones+   -  Reputation: 2832

Like
0Likes
Like

Posted 17 March 2014 - 01:40 PM

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!



#3 Migi0027   Crossbones+   -  Reputation: 2111

Like
0Likes
Like

Posted 17 March 2014 - 03:03 PM

Cheers to you too!

 

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


Hi! Cuboid Zone
The Rule: Be polite, be professional, but have a plan to steal all their shaders!

#4 imoogiBG   Members   -  Reputation: 1245

Like
0Likes
Like

Posted 17 March 2014 - 04:08 PM

bOutput[i] = gb;

 

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


Edited by imoogiBG, 17 March 2014 - 04:08 PM.


#5 Migi0027   Crossbones+   -  Reputation: 2111

Like
0Likes
Like

Posted 18 March 2014 - 12:15 AM

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, 18 March 2014 - 12:42 AM.

Hi! Cuboid Zone
The Rule: Be polite, be professional, but have a plan to steal all their shaders!

#6 kauna   Crossbones+   -  Reputation: 2832

Like
0Likes
Like

Posted 18 March 2014 - 05:05 AM

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

 

Cheers!



#7 Migi0027   Crossbones+   -  Reputation: 2111

Like
0Likes
Like

Posted 18 March 2014 - 09:59 AM

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/


Edited by Migi0027, 18 March 2014 - 09:59 AM.

Hi! Cuboid Zone
The Rule: Be polite, be professional, but have a plan to steal all their shaders!




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