开发者

Passing parameters to the constant buffer in SlimDX Direct3D 11

开发者 https://www.devze.com 2023-02-11 07:52 出处:网络
this is related to Setting up the constant buffer using SlimDX I have a shader, which is very simple and looks like this:

this is related to Setting up the constant buffer using SlimDX

I have a shader, which is very simple and looks like this:

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

All it does is apply the color passed in via the constant buffer to every pixel drawn, very simple.

In my code I define my constant buffer as follows:

[StructLayout(LayoutKind.Explicit)]
        struct ConstantBuffer
        {
            [FieldOffset(0)]
            public Color4 Color;
        }

And set it here, in my render loop.

ConstantBuffer cb = new ConstantBuffer();
cb.Color = new Color4(Color.White);
using (DataStream data = new DataStream(Marshal.SizeOf(typeof(ConstantBuffer)), true, true))           
{
    da开发者_JAVA百科ta.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 is my device.ImmediateContext and device is my Direct3D device. However when it runs, it draws my pixels as black, despite having passed through the color white, so clearly the value isn't being set.

Anybody have any ideas?


You have to create the D3D.Buffer object once, update it as often as you want (you specify the initial data as constructor argument, so if you're going to always have a white object, there's no need to call UpdateSubresource at all), and bind it to the pipeline (context.PixelShader.SetConstantBuffer(buffer, 0)) before issuing a draw call.

0

精彩评论

暂无评论...
验证码 换一张
取 消