Jump to content

  • Log In with Google      Sign In   
  • Create Account


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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 Buckeye   Crossbones+   -  Reputation: 4436

Like
0Likes
Like

Posted 04 May 2014 - 09:53 AM

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.


Sponsor:

#2 Mona2000   Members   -  Reputation: 590

Like
2Likes
Like

Posted 04 May 2014 - 09:56 AM

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, 04 May 2014 - 09:57 AM.


#3 Buckeye   Crossbones+   -  Reputation: 4436

Like
0Likes
Like

Posted 04 May 2014 - 10:00 AM


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, 04 May 2014 - 10:06 AM.

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


#4 SeanMiddleditch   Members   -  Reputation: 5118

Like
3Likes
Like

Posted 04 May 2014 - 11:07 AM

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


#5 Buckeye   Crossbones+   -  Reputation: 4436

Like
0Likes
Like

Posted 04 May 2014 - 12:52 PM

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.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS