Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two constant buffers with same elements

Suppose i have a following HLSL vertex shader fragment with constant buffers:

cbuffer matrixBuffer 
{
    matrix worldMatrix;
    matrix viewMatrix;
    matrix projectionMatrix;
};

cbuffer matrixBuffer2
{
    matrix worldMatrix2;
    matrix viewMatrix2;
    matrix projectionMatrix2;
};

Variables from constant buffers are then used in acctual vs function, so i need to set them.

And in C++ i declared following struct:

struct MatrixType
{
    D3DMATRIX world;
    D3DXMATRIX view;
    D3DXMATRIX projection;
};

In my app on initialization i create constant buffer pointer ID3D11Buffer*.

Later on per frame method i update constant buffer, that is i map buffer, update subresource, unmap buffer, and set buffer in vertex shader.

Everything is fine when i have just one constant buffer, but here is my question.

How does directx diffrentiate between buffers ? For example i want to set worldMatrix2 how to achieve this ?

I read msdn reference but i got no answer.

Is it allowed to have two or more constant buffers with same size and elements ? Are they stored in continuous piece of memory, so when i set buffers they are set in order they are declared in HLSL ?

like image 731
user1387886 Avatar asked Dec 21 '22 03:12

user1387886


1 Answers

You can specify which constant register each constant buffer is bound to in your shader program like this:

cbuffer matrixBuffer : register(b0)
{
    matrix worldMatrix;
    matrix viewMatrix;
    matrix projectionMatrix;
};

cbuffer matrixBuffer2 : register(b1)
{
    matrix worldMatrix2;
    matrix viewMatrix2;
    matrix projectionMatrix2;
};

When you bind your constant buffers to your vertex shader in your c++ code you specify which slot to bind to as follows:

// Set the buffers.
g_pd3dContext->VSSetConstantBuffers( 0, 1, &g_pConstantBuffer1 ); // Bind to slot 0
g_pd3dContext->VSSetConstantBuffers( 1, 1, &g_pConstantBuffer2 ); // Bind to slot 1

From MSDN:

void VSSetConstantBuffers(
    [in]  UINT StartSlot,   <-- this is the slot parameter
    [in]  UINT NumBuffers,
    [in]  ID3D11Buffer *const *ppConstantBuffers
);

In your example, you have a choice (because both buffers have the same structure). You could maintain two separate constant buffers and update each separately or you could just reuse the same constant buffer and update its data more than once per frame. In this case you would update using map subresource etc. execute a shader, then update the buffer again using map subresource, then execute the shader again.

like image 90
gareththegeek Avatar answered Dec 24 '22 00:12

gareththegeek