设置使用SlimDX

设置使用SlimDX

问题描述:

我一直在关注微软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教程或资源,这也将是有益的。

谢谢。

类似的东西来这应该工作:

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