2011-02-10 34 views
1

我一直在關注微軟Direct3D11教程,但使用C#和SlimDX不斷緩衝。我試圖設置常量緩衝區,但我不知道如何創建或設置。設置使用SlimDX

我只是試圖設置使用恆定緩衝器三個矩陣(世界視點投影),但我的每一個階段,創建,數據輸入掙扎並將它傳遞到着色器。

在MSDN(我已經基本上覆制)的HLSL是:

cbuffer ConstantBuffer : register(b0) 
{ 
    matrix World; 
    matrix View; 
    matrix Projection; 
} 

的C++代碼在MSDN是:

ID3D11Buffer* g_pConstantBuffer = NULL; 
XMMATRIX g_World; 
XMMATRIX g_View; 
XMMATRIX g_Projection; 

//set up the constant buffer 
D3D11_BUFFER_DESC bd; 
ZeroMemory(&bd, sizeof(bd)); 
bd.Usage = D3D11_USAGE_DEFAULT; 
bd.ByteWidth = sizeof(ConstantBuffer); 
bd.BindFlags = D3D11_BIND_CONSTANT_BUFFER; 
bd.CPUAccessFlags = 0; 
if(FAILED(g_pd3dDevice->CreateBuffer(&bd, NULL, &g_pConstantBuffer))) 
    return hr; 


// 
// Update variables 
// 
ConstantBuffer cb; 
cb.mWorld = XMMatrixTranspose(g_World); 
cb.mView = XMMatrixTranspose(g_View); 
cb.mProjection = XMMatrixTranspose(g_Projection); 
g_pImmediateContext->UpdateSubresource(g_pConstantBuffer, 0, NULL, &cb, 0, 0); 

有誰知道如何將此轉化爲SlimDX?或者,如果有人知道任何SlimDX教程或資源,這也將是有益的。

謝謝。

回答

1

類似的東西來這應該工作:

var buffer = new Buffer(device, new BufferDescription { 
    Usage = ResourceUsage.Default, 
    SizeInBytes = sizeof(ConstantBuffer), 
    BindFlags = BindFlags.ConstantBuffer 
}); 

var cb = new ConstantBuffer(); 
cb.World = Matrix.Transpose(world); 
cb.View = Matrix.Transpose(view); 
cb.Projection = Matrix.Transpose(projection); 

var data = new DataStream(sizeof(ConstantBuffer), true, true); 
data.Write(cb); 
data.Position = 0; 

context.UpdateSubresource(new DataBox(0, 0, data), buffer, 0); 
+0

這是有道理的,但我得到了以下錯誤:SlimDX.D3DCompiler.ConstantBuffer沒有構造函數參數爲零(構造函數是ConstantBuffer(IntPtr的指針)和也cb.World,cb.View和cb.Projection不存在,Visual Studio將這些標記爲錯誤(「SlimDX.D3DCompiler.ConstantBuffer不包含世界定義」)。我正在使用SlimDX的最新穩定版本(下載一個星期前) – 2011-02-10 21:16:56