Hi everyone. I've just started transitioning from XNA to SlimDX (and DirectX11) and boy am I confused! I have a deferred shader and a good way to instance primitives (cubes cones, spheres, etc) in XNA and I'm trying currently trying to learn enough of SlimDX and Directx11 to recreate those.
My brain already hurts. I started off by trying to expand the 3rd SlimDX tutorial: simple triangle. I modified the tutorial effect file to this:
cbuffer ColorBuffer : register(cb0)
{
float4 color;
}
cbuffer WorldBuffer : register(cb1)
{
float4x4 world;
}
struct VS_INPUT
{
float4 Position : POSITION;
};
struct VS_OUTPUT
{
float4 Position : SV_POSITION;
float4 Color : COLOR0;
};
VS_OUTPUT VShader(VS_INPUT input)
{
VS_OUTPUT output = (VS_OUTPUT)0;
output.Position = mul(input.Position, world);
output.Color = color;
return output;
}
float4 PShader(VS_OUTPUT input) : SV_Target
{
return input.Color;
}
It's simple enough.
I then attempt to set the shader constants in the SlimDX MessagePump:
MessagePump.Run(form, () =>
{
context.ClearRenderTargetView(renderTarget, new Color4(0.5f, 0.5f, 1.0f));
System.DateTime now = System.DateTime.Now;
System.TimeSpan span = System.TimeSpan.FromTicks(now.Ticks - start.Ticks);
world = Matrix.RotationZ((float)span.TotalSeconds);
worldStream.Position = 0;
worldStream.Write(world);
worldStream.Position = 0;
worldBuffer = new Buffer(
device,
worldStream,
64,
ResourceUsage.Dynamic,
BindFlags.ConstantBuffer,
CpuAccessFlags.Write,
ResourceOptionFlags.None,
4);
color = new Color4(1, 1, 1, 1);
colorStream.Position = 0;
colorStream.Write(color);
colorStream.Position = 0;
context.VertexShader.SetConstantBuffer(worldBuffer, 0);
colorBuffer = new Buffer(
device,
colorStream,
16,
ResourceUsage.Dynamic,
BindFlags.ConstantBuffer,
CpuAccessFlags.Write,
ResourceOptionFlags.None,
4);
context.VertexShader.SetConstantBuffer(colorBuffer, 1);
// draw the triangle
context.Draw(3, 0);
swapChain.Present(0, PresentFlags.None);
});
The world matrix is set correctly, but the output behaves as if the color constant was never set. And to add to my puzzlement, I don't understand why context.VertexShader.SetConstantBuffer(worldBuffer, 0) sets the second ConstantBuffer in the shader file.
What am I doing wrong?