在着色器

时间:2015-07-24 23:23:23

标签: shader direct3d hlsl

我的顶点着色器有一个常量缓冲区和一个可变缓冲区。然而,当我去Map()并设置他们的数据时,我不知道如何区分这两个缓冲区。

我的代码如下,您可以看到我正在使用" Subresource 1"这不是我想要的 - 我真的想要缓冲区1或插槽1,或者说某些东西要说"两个缓冲区中的第二个":

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

以下是顶点着色器本身的两个缓冲区:

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

cbuffer VSCBBilliardBallChangesEveryFrame : register (b1)
{
    float4x4    _worldMatrix;
    float4x4    _worldInverseTransposeMatrix;
    float4x4    _worldViewProjectionMatrix;
    float4x4    _viewMatrix;
};

2 个答案:

答案 0 :(得分:1)

您映射ID3D11Buffer,其中这些缓冲区最终绑定到管道与您如何使用数据填充它们无关。它是传递给'Map'的指针,它控制着什么Buffer被映射/更新,看来你已经通过传递静态缓冲区的_pVSCBNeverChanges和另一个传递_pVSCBChangesEveryFrame来做到这一点。

两个Map操作的子资源索引只需要'0'。

答案 1 :(得分:0)

我相信当你映射内存时,它不知道或不重要。调用VSSetConstantBuffers时有什么关系 - 在该调用中有一个插槽索引:

    pd3dImmediateContext->VSSetConstantBuffers(0, 1, &_pVSCBNeverChanges);
    pd3dImmediateContext->VSSetConstantBuffers(1, 1, &_pVSCBChangesEveryFrame);