Jump to content
  • Advertisement
Sign in to follow this  
jdub

Question about organization of constant buffers within shaders

This topic is 2148 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

If I have an HLSL file that contains multiple shaders such as the example below:

SamplerState SampleType;
Texture2D Texture;

cbuffer worldBuffer
{
	matrix world;
};

cbuffer projectionBuffer
{
	matrix projection;
};

cbuffer viewBuffer
{
	matrix view;
};

struct VS_INPUT
{
	float4 Position : POSITION0;
	float2 TexCoord : TEXCOORD;
};

struct PS_INPUT
{
	float4 Position : SV_POSITION;
	float2 TexCoord : TEXCOORD;
};

PS_INPUT VShader(VS_INPUT input)
{ 
	PS_INPUT output = (PS_INPUT)0;

	output.TexCoord = input.TexCoord;
	output.Position = mul(world, input.Position);
	output.Position = mul(projection, output.Position);

	return output;
}

float4 PShader(PS_INPUT input) : SV_TARGET
{
	return Texture.Sample(SampleType, input.TexCoord);
}

Does each shader have it's own copy of each of the constant buffers or do they share them?  In other words, do I have to call SetConstantBuffers() twice to set their value so that it is the same in both shaders?

Share this post


Link to post
Share on other sites
Advertisement

I will let another one answer this one for you as I'm not sure about this, but, I do think that the cb will stay in place until overwrited by another sb.

 

But one recommendation: Have a Object buffer (world, and other object things), and a Frame buffer (Projection, View, etc...).

 

-MIGI0027

Share this post


Link to post
Share on other sites

Shaders don't "have" constant buffers. Constant buffers are a resource, just like textures or vertex buffers. The way it works is that the shader expects a certain class of resource to be bound, and the app binds a resource containing the necessary data. In the case of a constant buffer the shader expects a buffer with the given layout and size, and then the app binds a buffer with data that matches the layout. If you bind a constant buffer to a shader stage, that buffer stays bound for any subsequent draw calls even if you switch the shader. So you don't need to re-bind a constant buffer if you're okay with the same data being used in the next draw call.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!