【问题标题】:Passing parameters to the constant buffer in SlimDX Direct3D 11在 SlimDX Direct3D 11 中将参数传递给常量缓冲区
【发布时间】:2011-02-16 13:53:09
【问题描述】:

这与Setting up the constant buffer using 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;
}

它所做的只是将通过常量缓冲区传入的颜色应用于绘制的每个像素,非常简单。

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

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

context 是我的 device.ImmediateContext 并且 device 是我的 Direct3D 设备。然而,当它运行时,它会将我的像素绘制为黑色,尽管它已经通过了白色,所以显然没有设置该值。

有人有什么想法吗?

【问题讨论】:

    标签: c# buffer direct3d shader slimdx


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-15
      相关资源
      最近更新 更多