Jump to content
  • Advertisement
Sign in to follow this  
jkristia

how to write to constant buffer?

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

Hi,

I'm completely new to DirectX, and just start looking into it using SharpDX, and I have a question regarding the constant buffer.

In most of the material I have been reading I see it is recommended to create multiple smaller constant buffers, e.g. in the example from '3D game programming with DirectX' it show to create a constant buffer for e.g.

 

cbuffer cbPerObject,

cbuffer cbPerFrame

cbuffer cbRarely

 

But what I have not been able to find is how to access those individual buffers.

I understand that the entire buffer must be written each time it Is updated (which is why smaller is better), and I understand that the buffer is updated through

context.UpdateSubresource(data, buffer)

 

But I have not been able to find how to make the link between the buffer defined in the shader file and the buffer used in the C# code.

 

Is there anyone who can explain how this is done?

 

And a side question, I tried to use effect instead and access the variables through GetVariableByName() and then do an effect.Apply() which works, and seems straight forward. What is the benefit of one over the other?, does effect.Apply update only the 'cbuffer' in where the modified variables are?. (I know I shouldn't worry about this right now, but I'm just curious)

 

Thanks

 

 

 

 

Share this post


Link to post
Share on other sites
Advertisement
The association is made when you set the constant buffer: Device.ImmediateContext.VertexShader.SetConstantBuffer

The first parameter of that function is the register slot. If you don't specify the slot in the hlsl, it'll be automatically assigned one, which you can find from looking at the compiled shader code. See here for info on the register keyword: http://msdn.microsoft.com/en-us/library/windows/desktop/dd607359(v=vs.85).aspx Edited by Telanor

Share this post


Link to post
Share on other sites

hmm, thank you for your response, but unfortunately it is still not clear to me how to use it since

 

if I have defined 3 cbuffers, are they automatically assigned slot 0,1,2, so I will call

 

SetConstantBuffer(0, ...)

SetConstantBuffer(1, ...)

SetConstantBuffer(2, ...)

 

to update all three?, if so, are they assigned in the order they are declared?

When accessing the variable in the vertex shader, do I still only access it by name even if the buffer is in slot1?

 

Thank you for your help.

Share this post


Link to post
Share on other sites

if I have defined 3 cbuffers, are they automatically assigned slot 0,1,2, so I will call
 
SetConstantBuffer(0, ...)
SetConstantBuffer(1, ...)
SetConstantBuffer(2, ...)
 
to update all three?, if so, are they assigned in the order they are declared?
When accessing the variable in the vertex shader, do I still only access it by name even if the buffer is in slot1?


I believe that's correct. I don't know if there's any documentation on how it assigns them but it most likely just starts at 0 and assigns in order, skipping over any already-used registers. Like I said, if you don't want to rely on what the compiler assigns, you can just assign them manually using the register keyword. And yes, you access them by name in hlsl, it doesn't matter what buffer they're assigned to or what slot the buffer is in. Also keep in mind that there's also a SetConstantBuffers function that allows you to set multiple in 1 call.

Share this post


Link to post
Share on other sites

Yeah they don't document the assignment order, and I'm pretty sure that's intentional. So it's generally not safe to rely on it. If you want to know ahead of time which slot a resource is assigned to, you should explicitly bind it to a slot like Telanor suggests. If you don't want to do this, you can also use the reflection API's to query the slot of a particular resource with GetResourceBindingDesc.

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!