C# 将参数传递到SlimDX Direct3D 11中的常量缓冲区

C# 将参数传递到SlimDX Direct3D 11中的常量缓冲区,c#,buffer,direct3d,shader,slimdx,C#,Buffer,Direct3d,Shader,Slimdx,这与 我有一个着色器,非常简单,如下所示: cbuffer ConstantBuffer : register(b0) { float4 color; } float4 VShader(float4 position : POSITION) : SV_POSITION { return position; } float4 PShader(float4 position : SV_POSITION) : SV_Target { return color; } 它所做

这与

我有一个着色器,非常简单,如下所示:

cbuffer ConstantBuffer : register(b0)
{
    float4 color;
}

float4 VShader(float4 position : POSITION) : SV_POSITION
{
    return position;
}

float4 PShader(float4 position : SV_POSITION) : SV_Target
{
    return color;
}
它所做的只是将通过恒定缓冲区传入的颜色应用于绘制的每个像素,非常简单

在我的代码中,我将常量缓冲区定义如下:

[StructLayout(LayoutKind.Explicit)]
        struct ConstantBuffer
        {
            [FieldOffset(0)]
            public Color4 Color;
        }
并在这里设置它,在我的渲染循环中

ConstantBuffer cb = new ConstantBuffer();
cb.Color = new Color4(Color.White);
using (DataStream data = new DataStream(Marshal.SizeOf(typeof(ConstantBuffer)), true, true))           
{
    data.Write(cb);
    data.Position = 0;
    D3D.Buffer buffer = new D3D.Buffer(device,data, new BufferDescription
    {
        Usage = ResourceUsage.Default,
        SizeInBytes = Marshal.SizeOf(typeof(ConstantBuffer)),
        BindFlags = BindFlags.ConstantBuffer
    });
    context.UpdateSubresource(new DataBox(0, 0, data), buffer, 0);
}
上下文是我的设备。ImmediateContext和设备是我的Direct3D设备。然而,当它运行时,它将我的像素绘制为黑色,尽管已经通过了白色,所以很明显,该值没有被设置


有人有什么想法吗?

您必须创建一次D3D.Buffer对象,并根据需要随时更新它(您将初始数据指定为构造函数参数,因此如果您总是使用白色对象,则根本不需要调用UpdateSubresource),并将其绑定到管道(
context.PixelShader.SetConstantBuffer(Buffer,0)
)在发出绘制调用之前。

您必须创建一次D3D.Buffer对象,并根据需要经常更新它(您将初始数据指定为构造函数参数,因此如果您将始终拥有白色对象,则根本不需要调用UpdateSubresource),并将其绑定到管道(
context.PixelShader.SetConstantBuffer)(缓冲区,0)
),然后再发出draw调用