使用SlimDX设置常量缓冲区

时间:2011-02-10 20:23:22

标签: c# c++ direct3d shader slimdx

我一直在关注Microsoft Direct3D11教程,但使用的是C#和SlimDX。我正在尝试设置常量缓冲区但不确定如何创建或设置它。

我只是尝试使用常量缓冲区设置三个矩阵(世界,视图和投影),但我在每个阶段都在苦苦挣扎,创建,数据输入并将其传递给着色器。

MSDN上的HLSL(我基本上已复制)是:

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

MSDN上的C ++代码是:

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 个答案:

答案 0 :(得分: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);