Im working with model instancing in XNA 4.0 and I sending my model instance transformation in a parallel stream. Im following this tutorial. However when I want a matrix as input to my shader I get what looks like a damaged matrix, because I get strange projection results.
Does anyone know the source to the problem and why I can’t pass the matrix when others suggest so?
Problem:
struct VertexShaderInput
{
float4 Position : POSITION0;
float3 Normal : NORMAL0;
float3 UV : TEXCOORD0;
float3 Color : COLOR0;
float3 Tangent : TANGENT0;
float3 Binormal : BINORMAL0;
float4x4 World : TEXCOORD3; //Problem
};
Changing the vertex shader function to the following does not help either:
VertexShaderOutput VertexShaderFunction(VertexShaderInput input, float4x4 World : TEXCOORD3)
{
}
This works if I build the matrix with the vectors alone, I dont know why. Im I losing data?
struct VertexShaderInput
{
float4 Position : POSITION0;
float3 Normal : NORMAL0;
float3 UV : TEXCOORD0;
float3 Color : COLOR0;
float3 Tangent : TANGENT0;
float3 Binormal : BINORMAL0;
float4 World1 : TEXCOORD3;
float4 World2 : TEXCOORD4;
float4 World3 : TEXCOORD5;
float4 World4 : TEXCOORD6;
};
Vertex format:
internal struct InstanceDataVertex
{
public Matrix World;
public InstanceDataVertex(Matrix World)
{
this.World = World;
}
public readonly static VertexDeclaration VertexDeclaration = new VertexDeclaration
(
new VertexElement(0, VertexElementFormat.Vector4, VertexElementUsage.TextureCoordinate, 3),
new VertexElement(sizeof(float) * 4, VertexElementFormat.Vector4, VertexElementUsage.TextureCoordinate, 4),
new VertexElement(sizeof(float) * 8, VertexElementFormat.Vector4, VertexElementUsage.TextureCoordinate, 5),
new VertexElement(sizeof(float) * 12, VertexElementFormat.Vector4, VertexElementUsage.TextureCoordinate, 6)
);
}
The input registers on the GPU are limited in size. The size of
TEXCOORDnisfloat4(as listed here). There are nofloat4x4inputs.Splitting your matrix across several input registers and then reconstructing it should work fine. It’s just a matter of making sure the right values in your C#
Matrixend up in the right places in your HLSLfloat4x4. I suspect the mapping is trivial, but I’m not sure.