Edit…
Ok maybe i missed something pumkin did.
I made a new test for comparision. I tried this on dx this time around. I made a regular quad and a instanced one. Other then the way the instanced version is passed in, They are nearly identical, the matrices are proper before hitting the shader ect.
The animated image shows two tests one after the other of the same matrix being passed but in different ways.
http://i936.photobucket.com/albums/ad207/xlightwavex/miscellaneous/Gl_instanced_matrixbug02_zpsfonl0b1v.gif
As per the gif the matrices are proper before they get into the shader. As you can see the same matrix when passed non instanced as a global works ok. The matrix when passed thru the instance isn’t. I mean its sort of working its translating now, but somethings still not right and the weird clipping.
Here is the new test project if anyone wants to take a look.
You need to comment out which Test will run within the Program.cs.
the two shaders are basically identical now other then how the matrix is passed when instanced, as shown below.
// FxInstancedObjMatrices.fx
// April 21 2018
#if OPENGL
#define SV_POSITION POSITION
#define VS_SHADERMODEL vs_3_0
#define PS_SHADERMODEL ps_3_0
#else
#define VS_SHADERMODEL vs_4_0
#define PS_SHADERMODEL ps_4_0
#endif
//_________________________________________________________________
matrix WorldViewProjection;
Texture2D TextureA;
sampler2D TexSamplerA = sampler_state
{
Texture = <TextureA>;
};
//_______________________________________________________________
// Game_RegularComparison
// techniques QuadDraw Not instanced
//_______________________________________________________________
struct VsInputQuad
{
float4 Position : POSITION0;
float2 TexCoord : TEXCOORD0;
};
struct VsOutputQuad
{
float4 Position : SV_Position;
float2 TexCoord : TEXCOORD0;
float4 Color : COLOR0;
};
VsOutputQuad VsQuadDraw(VsInputQuad input)
{
VsOutputQuad output;
output.Position = mul(input.Position, WorldViewProjection); // basically matrix identity
output.Color = float4(1.0f,0.0f,0.0f,1.0f);
output.TexCoord = input.TexCoord;
return output;
}
float4 PsQuadDraw(VsOutputQuad input) : COLOR0
{
float4 col = tex2D(TexSamplerA, input.TexCoord) * input.Color;
clip(col.a - .05f); // clip very low alpha pixels
return col;
}
technique QuadDraw
{
pass
{
VertexShader = compile VS_SHADERMODEL VsQuadDraw();
PixelShader = compile PS_SHADERMODEL PsQuadDraw();
}
}
//__________________________________________________________
// Game_HwiMatrixPassing version
// Instanced the instance passes the matrix.
//__________________________________________________________
struct VSVertexInputInstanced
{
float3 Position : POSITION0;//SV_POSITION;
float2 TexCoord : TEXCOORD0;
};
struct VSInstanceInputInstanced
{
float4x4 Transform : NORMAL1;
};
struct VSOutputInstanced
{
float4 Position : SV_POSITION;
float2 TexCoord : TEXCOORD0;
float4 Color : COLOR0;
};
VSOutputInstanced MainVsInstanced(in VSVertexInputInstanced vertexInput, VSInstanceInputInstanced instanceInput)
{
VSOutputInstanced output;
output.Position = mul(float4(vertexInput.Position, 1.0f), instanceInput.Transform);
output.TexCoord = vertexInput.TexCoord;
output.Color = float4(1.0f, 0.0f, 0.0f, .99f);
return output;
}
float4 MainPsInstanced(VSOutputInstanced input) : COLOR0
{
float4 col = tex2D(TexSamplerA, input.TexCoord) * input.Color;
clip(col.a - .05f); // clip very low alpha pixels
return col;
}
technique ParticleDrawingInstanced
{
pass
{
VertexShader = compile VS_SHADERMODEL MainVsInstanced();
PixelShader = compile PS_SHADERMODEL MainPsInstanced();
}
};
The vertex structure looks like this now.
public struct InstanceDataOrientationB : IVertexType
{
public Matrix Transform;
////public float instanceTimeOrId;
public static readonly VertexDeclaration VertexDeclaration;
static InstanceDataOrientationB()
{
var elements = new VertexElement[]
{
new VertexElement(sizeof(float) *0, VertexElementFormat.Vector4, VertexElementUsage.Normal, 1),
new VertexElement(sizeof(float) *4, VertexElementFormat.Vector4, VertexElementUsage.Normal, 2),
new VertexElement(sizeof(float) *8, VertexElementFormat.Vector4, VertexElementUsage.Normal, 3),
new VertexElement(sizeof(float) *12, VertexElementFormat.Vector4, VertexElementUsage.Normal, 4),
//new VertexElement(sizeof(float) *12, VertexElementFormat.Single, VertexElementUsage.BlendWeight, 0)
//new VertexElement( offset in bytes, VertexElementFormat.Single, VertexElementUsage. option, shader element usage id number )
};
VertexDeclaration = new VertexDeclaration(elements);
}
VertexDeclaration IVertexType.VertexDeclaration
{
get { return VertexDeclaration; }
}
}
// the vertexDataType
//[StructLayout(LayoutKind.Sequential)]
public struct VertexPositionUvB : IVertexType
{
public Vector3 position;
public Vector2 texCoordinate;
public static readonly VertexDeclaration VertexDeclaration;
static VertexPositionUvB()
{
var elements = new VertexElement[]
{
new VertexElement(0, VertexElementFormat.Vector3, VertexElementUsage.Position, 0),
new VertexElement(sizeof(float) * 3, VertexElementFormat.Vector2, VertexElementUsage.TextureCoordinate, 0),
};
VertexDeclaration = new VertexDeclaration(elements);
}
VertexDeclaration IVertexType.VertexDeclaration
{
get { return VertexDeclaration; }
}
}
Edit this is soo weird i changed it again to use texture coordinates no difference. Is it possible that the 4th column is not being feed into the shader fully ? E.G. the w components. Im getting the weirdest cliping artifacts im having trouble even understanding what im seeing but it looks like it is working except for some sort of messed up W values.