2013-06-27 17 views
3

假設我有一個以下HLSL頂點着色器片段具有恆定緩衝器:兩個具有相同的元件常數緩衝器

cbuffer matrixBuffer 
{ 
    matrix worldMatrix; 
    matrix viewMatrix; 
    matrix projectionMatrix; 
}; 

cbuffer matrixBuffer2 
{ 
    matrix worldMatrix2; 
    matrix viewMatrix2; 
    matrix projectionMatrix2; 
}; 

從恆定緩衝區的變量然後在acctual VS功能使用,所以我需要設置它們。

而在C++我聲明如下結構:

struct MatrixType 
{ 
    D3DMATRIX world; 
    D3DXMATRIX view; 
    D3DXMATRIX projection; 
}; 

在我的初始化程序,我創建常量緩衝區指針ID3D11Buffer *。

後來在每幀的方法我更新常量緩衝區,這是我的地圖緩衝區,更新子資源,unmap緩衝區,並在頂點着色器中設置緩衝區。

當我只有一個常量緩衝區時,一切都很好,但這裏是我的問題。

directx如何diffrentiate緩衝區之間?例如,我想設置worldMatrix2如何實現這一目標?

我讀msdn的參考,但我沒有得到答案。

它是否允許有兩個或多個具有相同大小和元素的常量緩衝區?它們是否存儲在連續的內存中,所以當我設置緩衝區時,它們是按順序設置的,以便它們在HLSL中聲明?

回答

10

可以指定常量寄存器每個常量緩衝器被綁定到你的着色器程序是這樣的:

cbuffer matrixBuffer : register(b0) 
{ 
    matrix worldMatrix; 
    matrix viewMatrix; 
    matrix projectionMatrix; 
}; 

cbuffer matrixBuffer2 : register(b1) 
{ 
    matrix worldMatrix2; 
    matrix viewMatrix2; 
    matrix projectionMatrix2; 
}; 

當您綁定的固定緩衝區的頂點着色器在你的C++代碼,你指定哪個插槽綁定到如下:

// Set the buffers. 
g_pd3dContext->VSSetConstantBuffers(0, 1, &g_pConstantBuffer1); // Bind to slot 0 
g_pd3dContext->VSSetConstantBuffers(1, 1, &g_pConstantBuffer2); // Bind to slot 1 

MSDN

void VSSetConstantBuffers(
    [in] UINT StartSlot, <-- this is the slot parameter 
    [in] UINT NumBuffers, 
    [in] ID3D11Buffer *const *ppConstantBuffers 
); 

在你的例子中,你有一個選擇(因爲兩個緩衝區都有相同的結構)。您可以維護兩個單獨的常量緩衝區並分別更新每個緩衝區,或者您可以重複使用相同的常量緩衝區並每幀更新一次以上的數據。在這種情況下,您將使用map子資源更新等執行着色器,然後使用map子資源再次更新緩衝區,然後再次執行着色器。

相關問題