Passing bone matrices to shader

Started by
3 comments, last by JohnnyCode 11 years, 3 months ago
I have couple of different models with varying bones count.
To get away from shaders switch could i just use one for all without consequence?

I tried this and it worked:

MAX_BONES = 35;
...
vector<D3DXMATRIX> vMat; // vMat size varies from 20 - 30 depending on a model
...
effect->SetValue(hMatrices, &vMat[0], MAX_BONES * sizeof(D3DXMATRIX));


...
float4x4 mats[MAX_BONES];
...
Advertisement

Yes, that is a common approach.
However, your SetValue call should use vMat.size()*sizeof(D3DXMATRIX), otherwise you're reading past the end of the vector.

Then i always need a vector with size of MAX_BONES ?

Thats why i asked this question, is there a harm if it reads past the end of a vector, even if that data isn't used? Or is it undefined behaviour?

I got shader error if i pass it like this:

effect->SetValue(hMatrices, &vMat[0], vMat.size() * sizeof(D3DXMATRIX));
vMat size is 33, but in shader i have:

float4x4 mats[35];

[quote name='belfegor' timestamp='1357390934' post='5017725']is there a harm if it reads past the end of a vector, even if that data isn't used? Or is it undefined behaviour?[/quote]Yes it's harmful; it could crash your program. If you're lucky, nothing bad will happen (besides the extra bones containing garbage values), but it could crash depending on how the OS has decided to allocate your memory...

I got shader error if i pass it like this:[/quote]Ah, that must be an issue with the D3DX Effect framework -- I usually just use D3D shaders directly, and they allow you to send less data than the shader requires (with the unsent elements of the array containing garbage values).

In that case, just use vMat.resize(MAX_BONES) to avoid the error and also avoid the undefined behaviour wink.png

the bone indicies on verticies should always point to a defined matrix in the uniform array of matricies. So define the array correctly, feeding it matricies that conform to indicies. About that error, though it is unlikely, you may have a limit of 32 in the uniform array, in case you are using realy old pixel shader model.

This topic is closed to new replies.

Advertisement