本文介绍了使用SlimDX设置常量缓冲区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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

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

MSDN上的C ++代码是:

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

有人知道如何将其翻译为SlimDX吗?或者,如果有人知道任何SlimDX教程或资源也将是有用的.

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

谢谢.

推荐答案

类似于此的方法应该起作用:

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

这篇关于使用SlimDX设置常量缓冲区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 11:03