Sign in to follow this  

Getting invalid instance matrices in vertex shader

Recommended Posts

Hello all


Im using instancing to draw the opaque parts of of heavily repeated objects. I am running into some problems reading the instance data however.


My input structure for the vertex shader is like that:

struct VertexInput
	float3 position : POSITION0;
	float4 boneWeights : BLENDWEIGHT0;
	int4 bones : BLENDINDEX0;
	float3 normal : NORMAL0;
	float2 texCoord : TEXCOORD0;
	float2 texCoord2 : TEXCOORD1;

	float4 mat0 : TEXCOORD2;
	float4 mat1 : TEXCOORD3;
	float4 mat2 : TEXCOORD4;
	float4 mat3 : TEXCOORD5;

In order to get the position (before view and projection) i do the following:

VertexOutput main(VertexInput input) {
	float4x4 matInstance = float4x4(input.mat0, input.mat1, input.mat2, input.mat3);

	// bone & animation stuff

	position = mul(position, matInstance);
	// ...

the animation stuff and the per vertex input data is correct, I modified the last line to be: position = + eyePosition + float3(100, 0, 0); and the elements appear correctly in front of my camera.


I have checked with the graphics debugger, in my opinion the input data looks correct (im not showing the per vertex stuff, since thats working):

Instance buffer (i checked, its bound):




Input Layout:



Im using the DrawIndexedInstanced function.


The result is completely wrong however: 



Where should i begin to look at? What could be the reason of this strange behavior?


Thanks in advance,


Share this post

Link to post
Share on other sites

Hello again


Because i didnt know what else to try i just did a transpose on the instance matrix and what do you know, it works. In retrospect it makes sense, float4x4 is column-major, the float4x4 contstructor takes row-major. Thus i had to pass the matrix entries untransposed compared to view/proj and the rest.




Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this