• 13
• 18
• 19
• 27
• 10

# [DX10] Constant Buffer

This topic is 3718 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, just a little questions about Constant Buffer: I have a SM4.0 effect file with a lot of global constants: float3 foo; float4x4 m; and so on. I could try to group these variables to cbuffer structs. My questions: 1) Will this increase my performance? 2) Is it enough just to group them in cbuffer "structs" in the effect file or do I need additional steps in the application code? For example: Do I need to use CreateBuffer with a BindFlag on D3D10_BIND_CONSTANT_BUFFER? Thanks

##### Share on other sites
1. They're meant to improve performance by reducing the amount of state that needs to be sent every time you update your constants. The idea is to group them by update frequency, so that constants which seldom change aren't always getting sent down with constants that change every frame.

2. As far as I know, it should be enough to make the change in the effect file. Creating the buffers in code is for when you aren't using the effect framework. However I could be wrong.

##### Share on other sites
Hm ok thanks.

But its still kinda strange to me. Does that mean I have to sort them by frequency because if I update one variable in cBuffer Foo then ALL variables are updated?

##### Share on other sites
Yes.

DX10 uses constant buffers to manage shader variables. Even thou you don't declare one in the effect file, the effect interface will create them for you internally. If you look at the shader interfaces, there is only way to set constant buffers, not single registers.

Constant buffers has to be sent to the device as a whole, you can't modify parts of it. If you have a CB with 4096 floats and you modify the first one, the whole 4096 will get copied again. That is why it's important to group based on frequency of change. Per-frame, per-object, per-polygon, etc.

You can get better details from Gamefest presentations regarding DX10 found on MSDN's directx dev center.
http://msdn2.microsoft.com/en-us/xna/aa937787.aspx

To your second question, I do create a constant buffer then pass it to the effect. Then I update the CB before rendering.

Correct me if I'm wrong.

// at initecb = FXPool->AsEffect()->GetConstantBufferByName( "cbPerFrame" );ConstantBuffer = _dxx_cb_dyn( sizeof(D3DXMATRIX), NULL ).buffer();ecb->SetConstantBuffer( ConstantBuffer );//before renderingLPVOID pCBufferMapped = NULL;ConstantBuffer->Map( D3D10_MAP_WRITE_DISCARD, 0, &pCBufferMapped );CopyMemory( pCBufferMapped, &g_World, sizeof(D3DXMATRIX) );ConstantBuffer->Unmap();

Correct me if I'm wrong.