2011-02-16 96 views
1

这是关系到Setting up the constant buffer using SlimDX在SlimDX传递参数给定缓冲区的Direct3D 11

我有个着色器,这是非常简单的,看起来像这样:

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

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

有人有什么想法吗?

回答

1

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

+0

完美,非常感谢! – 2011-02-16 15:40:14