how to write to constant buffer?

Started by
5 comments, last by jkristia 10 years, 7 months ago

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

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

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.

someone pointed me to this link

http://msdn.microsoft.com/en-us/library/windows/desktop/bb509581(v=vs.85).aspx

I think this is the info I am looking for. I will test it out later.

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.

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.

I think I got it, I will try it out later.

Thank you both for your help

This topic is closed to new replies.

Advertisement