Sign in to follow this  
Tim Coolman

Understanding Buffer, Texture, and Sampler "registers"

Recommended Posts

BenS1    352
Good questions Tim, and ones I would like to know the answers to too.

Have you tried defining 2 constant buffers with the same register on the HLSL side?
My guess would be that you can, and then so long as you only set one of them from the C++ side and then only use one of them on your shader side you should be fine.

For example, imagine your game is a car racing game. In your code you might draw the roads first and then the cars in a separate Draw call.

So, when you draw the roads you could have a RoadParams specific constant buffer assigned to B0 and then set the appropriate Pixel Shader etc.

Once complete you could then assign a completely different CarParams constant buffer to B0, and set a different Pixel Shader (One that uses these params and draws car pixels instead of road pixels).

I'd imagine that should work fine, but I've not tried it.

Thanks
Ben

Share this post


Link to post
Share on other sites
Tim Coolman    171
[quote name='BenS1' timestamp='1352298391' post='4998415']
Have you tried defining 2 constant buffers with the same register on the HLSL side?
[/quote]

I have not tried this. I thought I read on another forum post that for registers to be reused they have to be "compatible" in some way. But I understand what you mean. If you have two different constant buffers defined with the same register slot in HLSL, maybe it is okay to use one or the other from a given shader as long as that slot has been provided with appropriate data from C++ first.

Anyone else know the answer? I won't know for sure until I have time to set up an experiment.

Thanks,
Tim

Share this post


Link to post
Share on other sites
MikeBMcL    173
You can bind up to 14 constant buffers per pipeline stage (and up to 128 texture buffers per pipeline stage). See the remarks here: [url="http://msdn.microsoft.com/en-us/library/bb509581(VS.85).aspx"]http://msdn.microsoft.com/en-us/library/bb509581(VS.85).aspx[/url] .

Share this post


Link to post
Share on other sites
Tim Coolman    171
[quote name='MikeBMcL' timestamp='1352339241' post='4998687']
You can bind up to 14 constant buffers per pipeline stage (and up to 128 texture buffers per pipeline stage).
[/quote]

I understand that. The thing I'm still unsure about is whether you can use the same register number for multiple [i]cbuffer[/i] structures in the same HLSL file, as long as you don't use more than one of those in a given pipeline stage.

For example, can you do this...
[source lang="cpp"]cbuffer BufferForVertexShader : register(b0)
{
float variable1;
float variable2;
uint variable3;
};

cbuffer BufferForPixelShader : register(b0)
{
float4x4 variable1;
float4 variable2;
int variable3;
int variable4;
};[/source]
... as long as you only access one cbuffer from either pipeline stage and have the proper buffer bound to slot 0 for each shader? Or does this cause compilation problems within the same HLSL file and require you to separate them out into different files?

Thanks.

Share this post


Link to post
Share on other sites
MikeBMcL    173
You can, yes, though it's probably better to just use an include file for common structs and define the shader-specific buffers in each shader's HLSL file. Or are you planning to put all of the shaders into one file?

(Note: By "you can" I mean that I tested compiling a vertex and pixel shader with two cbuffers both assigned to register(b0) where only one of those cbuffers is used and that I received no compile errors using the built-in shader compilation with VS 2012 and the Windows 8 SDK; you might have different results if you compile using an older version of fxc even though it should in theory work fine. It's not super hard to test so if you are using an older fxc, just try it and see what happens.).

Share this post


Link to post
Share on other sites
Tim Coolman    171
Thanks to both MikeBMcL and MJP for your input. This really helps clarify things for me. Always helps to understand a little bit better how things work, even the things that happen "behind the scenes".

Share this post


Link to post
Share on other sites

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