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

Started by
3 comments, last by Buckeye 9 years, 11 months ago

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?

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

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)?


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);

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.


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);

Sean Middleditch – Game Systems Engineer – Join my team!

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.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

This topic is closed to new replies.

Advertisement