0

My vertex shader has both a constant buffer and a variable buffer. When I go to Map() and set their data, however, I have no idea how to tell the two buffers apart.

My code is as follows, and you can see that I'm using "Subresource 1" which is NOT what I want - I really want buffer 1, or slot 1, or something to say "the second of the two buffers":

virtual HRESULT SetVSShaderConstants() override
{
    HRESULT hr = S_OK;
    auto pd3dImmediateContext = DXUTGetD3D11DeviceContext();
    D3D11_MAPPED_SUBRESOURCE MappedResource;
    V(pd3dImmediateContext->Map(_pVSCBNeverChanges, 0, D3D11_MAP_WRITE_DISCARD, 0, &MappedResource));
    auto pCB = reinterpret_cast<VSCBBilliardBallNeverChanges *>(MappedResource.pData);
    pCB->_cameraPosition = _vertexShaderConstants._cameraPosition;
    pd3dImmediateContext->Unmap(_pVSCBNeverChanges, 0);
    return hr;
}

virtual HRESULT SetVSShaderVariables() override
{
    HRESULT hr = S_OK;
    auto pd3dImmediateContext = DXUTGetD3D11DeviceContext();
    D3D11_MAPPED_SUBRESOURCE MappedResource;
    V(pd3dImmediateContext->Map(_pVSCBChangesEveryFrame, 1, D3D11_MAP_WRITE_DISCARD, 0, &MappedResource));
    auto pCB = reinterpret_cast<VSCBBilliardBallChangesEveryFrame *>(MappedResource.pData);
    pCB->_viewMatrix                  = _vertexShaderVariables._viewMatrix;
    pCB->_worldInverseTransposeMatrix = _vertexShaderVariables._worldInverseTransposeMatrix;
    pCB->_worldMatrix                 = _vertexShaderVariables._worldMatrix;
    pCB->_worldViewProjectionMatrix   = _vertexShaderVariables._worldViewProjectionMatrix;
    pd3dImmediateContext->Unmap(_pVSCBChangesEveryFrame, 1);
    return hr;
}

And here are the two buffers in the vertex shader itself:

cbuffer VSCBBilliardBallNeverChanges : register (b0)
{
    float4      _cameraPosition;
}

cbuffer VSCBBilliardBallChangesEveryFrame : register (b1)
{
    float4x4    _worldMatrix;
    float4x4    _worldInverseTransposeMatrix;
    float4x4    _worldViewProjectionMatrix;
    float4x4    _viewMatrix;
};
Dave
  • 1,521
  • 17
  • 31

2 Answers2

1

You map ID3D11Buffer's, where those buffers eventually get bound to the pipeline has no bearing on how you populate them with data. It's the pointer that you pass to 'Map' that controls what Buffer gets mapped/updated, and it appears that you're already doing that by passing _pVSCBNeverChanges for the static buffer and _pVSCBChangesEveryFrame for the other.

You just need '0' for the subresource index on both Map operations.

Adam Miles
  • 3,504
  • 17
  • 15
  • I don't follow - I know on 'my' end which pointer represents what structure, but the Map() function doesn't know or have a way to indicate which constant buffer within the shader to actually update. – Dave Jul 25 '15 at 02:56
0

I believe that when you Map the memory, it does not know or matter. What does matter is when you call VSSetConstantBuffers - there's a slot index in that call:

    pd3dImmediateContext->VSSetConstantBuffers(0, 1, &_pVSCBNeverChanges);
    pd3dImmediateContext->VSSetConstantBuffers(1, 1, &_pVSCBChangesEveryFrame);
Dave
  • 1,521
  • 17
  • 31