In the tutorials and books I've been reading so far, I've learned how to create a constant buffer and put in a world matrix as part of that buffer. The world matrix is just an identity matrix, assuming that all models in the scene are already positioned correctly in the scene. I have a simple rainbow cube and for the next step in my learning, I want to create several instances of the cube at different positions, scales and orientations. Creating the matrix for each instance is easy enough, but I'm having trouble trying to work out how to create a vertex shader that takes, as one of its inputs, the matrix representing an instance's world transform. I was under the impression that I could use any semantic name in Direct3D11, hence my use of "INSTANCE" in the code below. If I can't do that, I'm not sure what I actually need to do.
here's what my shader wrapper in C# looks like:
using SharpDX.D3DCompiler;
using SharpDX.Direct3D11;
using SharpDX.DXGI;
using Device = SharpDX.Direct3D11.Device;
namespace SharpDx4.Direct3D.Shaders
{
public class DefaultVertexShader : ICompiledVertexShader
{
public DefaultVertexShader(Device device)
{
var shaderSource = Shaders.DefaultShaders;
ShaderSignature signature;
using (var bytecode = ShaderBytecode.Compile(shaderSource, "VShader", "vs_5_0"))
{
signature = ShaderSignature.GetInputSignature(bytecode);
Shader = new VertexShader(device, bytecode);
}
var elements = new[]
{
new InputElement("POSITION", 0, Format.R32G32B32_Float, 0, 0, InputClassification.PerVertexData, 0),
new InputElement("INSTANCE", 0, Format.R32G32B32A32_Float, 0, 0, InputClassification.PerInstanceData, 1),
new InputElement("COLOR", 0, Format.R32G32B32A32_Float, 12, 0)
};
InputLayout = new InputLayout(device, signature, elements);
}
public VertexShader Shader { get; private set; }
public InputLayout InputLayout { get; private set; }
public void Dispose()
{
Shader.Dispose();
}
}
}
Here's the class I'm using to hold a model's buffer data:
using System;
using System.Linq;
using SharpDX;
using SharpDX.Direct3D11;
using SharpDx4.Direct3D.Shaders;
using SharpDx4.Game.Geometry;
using Buffer = SharpDX.Direct3D11.Buffer;
namespace SharpDx4.Direct3D
{
public class ModelDeviceData : IDisposable
{
public Model Model { get; set; }
public VertexBufferBinding VerticesBufferBinding { get; set; }
public VertexBufferBinding InstancesBufferBinding { get; set; }
public Buffer IndicesBuffer { get; set; }
public ICompiledVertexShader VertexShader { get; set; }
public ICompiledPixelShader PixelShader { get; set; }
public void Dispose()
{
VerticesBufferBinding.Buffer.Dispose();
InstancesBufferBinding.Buffer.Dispose();
IndicesBuffer.Dispose();
}
public ModelDeviceData(Model model, Device device)
{
Model = model;
var vertices = Helpers.CreateBuffer(device, BindFlags.VertexBuffer, model.Vertices);
var instances = Helpers.CreateBuffer(device, BindFlags.VertexBuffer, Model.Instances.Select(m => m.WorldMatrix).ToArray());
VerticesBufferBinding = new VertexBufferBinding(vertices, Utilities.SizeOf<ColoredVertex>(), 0);
InstancesBufferBinding = new VertexBufferBinding(instances, Utilities.SizeOf<Matrix>(), 0);
IndicesBuffer = Helpers.CreateBuffer(device, BindFlags.IndexBuffer, model.Triangles);
}
}
}
And here's the shader code I've been mucking with.
cbuffer ConstantBuffer : register(b0)
{
matrix World;
matrix View;
matrix Projection;
}
struct VIn
{
float4 position: POSITION;
matrix instance: INSTANCE;
float4 color: COLOR;
};
struct VOut
{
float4 position : SV_POSITION;
float4 color : COLOR;
};
VOut VShader(VIn input)
{
VOut output;
output.position = mul(input.position, input.instance);
output.position = mul(output.position, View);
output.position = mul(output.position, Projection);
output.color = input.color;
return output;
}
When I try to run this, the line which instantiates an instance of InputLayout throws the following exception:
HRESULT: [0x80070057], Module: [General], ApiCode: [E_INVALIDARG/Invalid Arguments], Message: The parameter is incorrect.
As a beginner, obviously I'm doing something terribly wrong. Can anyone help me work out what that is? For the record I'm aware that there is such a thing as hardware instancing, but that's, like, chapter 15 in my book and I'm only up to chapter 5 so far. Yes I'm impatient, but please humour me; I'd like to get some results doing it this way first.