Setting up the constant buffer using SlimDX

点点圈 提交于 2019-12-02 09:49:42

问题


I've been following the Microsoft Direct3D11 tutorials but using C# and SlimDX. I'm trying to set the constant buffer but am not sure how to either create or set it.

I'm simply trying to set three matrices (world, view and projection) using a constant buffer but I'm struggling at every stage, creation, data input and passing it to the shader.

The HLSL on MSDN (which I've essentially copied) is:

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

The C++ code on MSDN is:

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

Does anybody know how to translate this to SlimDX? Or if anybody knows any SlimDX tutorials or resources that would also be useful.

Thanks.


回答1:


Something similar to this should work:

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


来源:https://stackoverflow.com/questions/4962225/setting-up-the-constant-buffer-using-slimdx

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!