Just switched from DX9 to DX11 and am attempting to instance a triangle. Created a vertex buffer and a buffer for the instance positions. Not sure what it is I'm doing incorrectly.
Input Layout:
var bytecode = ShaderBytecode.CompileFromFile("Instance.fx", "fx_5_0", ShaderFlags.None, EffectFlags.None, null, includeFX);
var effect = new Effect(device, bytecode);
var technique = effect.GetTechniqueByIndex(0);
var pass = technique.GetPassByIndex(0);
layout = new InputLayout(device, pass.Description.Signature, new[] {
new InputElement("POSITION", 0, Format.R32G32B32A32_Float, 0, 0, InputClassification.PerVertexData, 0),
new InputElement("COLOR", 0, Format.R32G32B32A32_Float, 16, 0, InputClassification.PerVertexData, 0),
new InputElement("TEXCOORD", 0, Format.R32G32B32A32_Float, 0, 1, InputClassification.PerInstanceData, 1)
});
Vertex Buffer:
var stream = new DataStream(3 * 8 * 4, true, true);
stream.Write(new Vector4( 0.2f, 0.0f, 0.0f, 1.0f));
stream.Write(new Vector4( 0.0f, 1.0f, 0.0f, 1.0f));
stream.Write(new Vector4(-0.2f, 0.0f, 0.0f, 1.0f));
stream.Write(new Vector4( 0.0f, 1.0f, 0.0f, 1.0f));
stream.Write(new Vector4( 0.0f, 2.0f, 0.0f, 1.0f));
stream.Write(new Vector4( 0.0f, 1.0f, 0.0f, 1.0f));
stream.Position = 0;
vertices = new SlimDX.Direct3D11.Buffer(device, stream, new BufferDescription()
{
BindFlags = BindFlags.VertexBuffer,
CpuAccessFlags = CpuAccessFlags.None,
OptionFlags = ResourceOptionFlags.None,
SizeInBytes = 3 * 8 * 4,
Usage = ResourceUsage.Default
});
stream.Dispose();
Instanced Positions Buffer:
SlimDX.Direct3D11.Buffer ipos;
var stream2 = new DataStream(4 * 4 * 4, true, true);
stream2.Write(new Vector4(0, 0, 0, 1));
stream2.Write(new Vector4(2, 2, 50, 1));
stream2.Write(new Vector4(3, 5, 20, 1));
stream2.Write(new Vector4(4, 0, 3, 1));
stream2.Position = 0;
ipos = new SlimDX.Direct3D11.Buffer(device, stream2, new BufferDescription()
{
BindFlags = BindFlags.VertexBuffer,
CpuAccessFlags = CpuAccessFlags.None,
OptionFlags = ResourceOptionFlags.None,
SizeInBytes = 4 * 4 * 4,
Usage = ResourceUsage.Default
});
stream2.Dispose();
Draw Code:
effect.GetVariableByName("World").AsMatrix().SetMatrix(World);
effect.GetVariableByName("View").AsMatrix().SetMatrix(View);
effect.GetVariableByName("Projection").AsMatrix().SetMatrix(Projection);
device.ImmediateContext.InputAssembler.InputLayout = layout;
device.ImmediateContext.InputAssembler.PrimitiveTopology = PrimitiveTopology.TriangleList;
device.ImmediateContext.InputAssembler.SetVertexBuffers(0, new VertexBufferBinding(vertices, 8 * 4, 0));
device.ImmediateContext.InputAssembler.SetVertexBuffers(1, new VertexBufferBinding(ipos, 4 * 4, 0));
pass.Apply(device.ImmediateContext);
device.ImmediateContext.DrawInstanced(3, 4, 0, 0);
Shaders:
float4x4 World;
float4x4 View;
float4x4 Projection;
struct VS_INPUT
{
float4 position : POSITION;
float4 xColor : COLOR0;
//float2 UV: TEXCOORD0;
float4 iposition : TEXCOORD0;
};
struct PS_INPUT
{
float4 Position : POSITION;
float4 xColor : COLOR0;
};
PS_INPUT VS_Shader( VS_INPUT input )
{
PS_INPUT output;
output.Position = (input.position) + input.iposition;
float4 worldPosition = mul(output.Position, World);
float4 viewPosition = mul(worldPosition, View);
output.Position = mul(viewPosition, Projection);
output.xColor = input.xColor;
output.xColor.a = 1;
return output;
}
float4 PS_Shader( PS_INPUT input ) : SV_Target
{
return input.xColor;
}
technique11 Render
{
pass P0
{
SetVertexShader( CompileShader( vs_5_0, VS_Shader() ) );
SetPixelShader( CompileShader( ps_5_0, PS_Shader() ) );
}
}
When the code is run, nothing is drawn. Any help is appreciated.