• Advertisement
Sign in to follow this  

context->UpdateSubresource(constant buffer) - how does it work?

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

I'm new to Direct3D 11, transitioning from DX9.

 

Question: How do context->UpdateSubresource and shaders work together? I.e., how does the context know where in the shader to stuff the subresource?

 

Background:

 

I have several shaders with contant buffers using this #include:

//--------------------------------------------------------------------------------------
// Constant Buffer Variables
//--------------------------------------------------------------------------------------
cbuffer cbNeverChanges : register(b0)
{
    float4 lightDir;
};

cbuffer cbChangeOnResize : register(b1)
{
    matrix Projection;
};

cbuffer cbChangesEveryFrame : register(b2)
{
    matrix View;
    matrix World;
    float4 vMeshColor;
};

I created 3 buffers, all from the same D3D11_BUFFER_DESC, changing only the ByteWidth, one to match in size each of the buffers in the shader. I update the corresponding buffer on resize, and per frame, as necessary. For example, when the window is resized, I update the context (buffer resize), followed by:

 

    // Re-initialize the projection matrix
    g_Projection = XMMatrixPerspectiveFovLH(XM_PIDIV4, width / (FLOAT)height, 0.01f, 100.0f);

    CBChangeOnResize cbChangesOnResize;
    cbChangesOnResize.mProjection = XMMatrixTranspose(g_Projection);
    g_pImmediateContext->UpdateSubresource(g_pCBChangeOnResize, 0, NULL, &cbChangesOnResize, 0, 0);


Everything works. So I'm curious - when a shader is set, how do the appropriate buffers in the GPU get set from those context subresources?

 

Other than the size of the structure (e.g., CBChangeOnResize above), there appears to be no information for the context to map a particular subresource to a particular buffer in the GPU. How does it work?

 

 

Share this post


Link to post
Share on other sites
Advertisement

UpdateSubresource is for updating the contents of a buffer, not for binding it. What you're looking for is ID3D11DeviceContext::PSSetConstantBuffers and the VS/GS/HS/DS equivalents. Are you using something that hides that part (Effects11)?

Edited by Mona2000

Share this post


Link to post
Share on other sites

What you're looking for is ID3D11DeviceContext::PSSetConstantBuffers

Ah! Indeed, that explains it well. Thanks!

 


Are you using something that hides that part (Effects11)?

Hah, no, just didn't think to look at the rendering routine I wrote. blink.png It's there, bigger than life. Hey, "you can't fix stupid."

        g_pImmediateContext->VSSetConstantBuffers(0, 1, &g_pCBNeverChanges);
        g_pImmediateContext->VSSetConstantBuffers(1, 1, &g_pCBChangeOnResize);
        g_pImmediateContext->VSSetConstantBuffers(2, 1, &g_pCBChangesEveryFrame);
Edited by Buckeye

Share this post


Link to post
Share on other sites

g_pImmediateContext->VSSetConstantBuffers(0, 1, &g_pCBNeverChanges);
        g_pImmediateContext->VSSetConstantBuffers(1, 1, &g_pCBChangeOnResize);
        g_pImmediateContext->VSSetConstantBuffers(2, 1, &g_pCBChangesEveryFrame);


As an unrelated note, the whole purpose of the first two parameters to these functions are to make it unnecessary to eat the overhead of calling them three times like this (assuming this is real code and not a simplified sample).

ID3D11Buffer* buffers[] = { g_pCBNeverChanges, g_pCBChangeOnResize, g_pCBChangesEveryFrame };
g_pImmediateContext->VSSetConstantBuffers(0, 3, buffers);

Share this post


Link to post
Share on other sites

Thanks, SeanMiddleditch! Changed my code and that works fine. I "stole" the original (separate calls) from sample code. Those calls are made for each set of shaders in the render loop, so it'll save a few cycles.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement