【问题标题】:Setting up the constant buffer using SlimDX使用 SlimDX 设置常量缓冲区
【发布时间】:2011-02-10 20:23:22
【问题描述】:

我一直在学习 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 教程或资源。

谢谢。

【问题讨论】:

    标签: c# c++ direct3d shader 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);
    

    【讨论】:

    • 这是有道理的,但我收到以下错误:SlimDX.D3DCompiler.ConstantBuffer 没有采用零参数的构造函数(构造函数是 ConstantBuffer(intPtr Pointer) 以及 cb.World、cb.View 和cb.Projection 不存在,Visual Studio 将这些标记为错误(“SlimDX.D3DCompiler.ConstantBuffer 不包含 World 的定义”)。我使用的是 SlimDX 的最新稳定版本(一周前下载)
    • 这个sn-p中的ConstantBuffer是指你自己的用户类型,包含你要设置的常量。你可以随便叫它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-05
    相关资源
    最近更新 更多