• Create Account

## Matrix palettes in vertex shader

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

11 replies to this topic

### #1Aardvajk  Members

12289
Like
1Likes
Like

Posted 07 May 2013 - 01:42 PM

I'm writing a 3D modeller at the moment so am trying to find a way to support as many joints as possible in a model since I can't just restrict to a domain-specific sensible number like you could in a game.

This is DirectX 9.0 by the way, should probably stress that.

The only way I've ever known how to get this data into a shader is an array of matrices as a global in the shader that is written to prior to the render. However, seems (obviously) there is a fairly strict limit on how much you can allocate for global data in a shader and I also don't know if this will vary from hardware to hardware so hard to know what I can rely upon here. On my laptop I can't get more than about 40 matrices in an array before it bombs out.

Just wondering what other approaches people are aware of to get this matrix data into the shader. I know I can reduce things by just inputting a quaternion and a translation vector, but that is still going to take half as many (registers?) as a matrix so will still have a hard limit.

I'm toying with the idea of using a floating point texture to store a list of pairs of quaternion and translation vectors, using 8 texels per joint, but just wanted to see if there were more obvious solutions I'm missing? I'd rather avoid software processing of the model for animation since I can't predict the model size/complexity so need it as performant as possible.

### #2eppo  Members

4601
Like
2Likes
Like

Posted 08 May 2013 - 03:22 AM

As far as I'm aware, on D3D9 hardware, vs_3_0 texture fetches are your only option.

3.0 vertex texture look-ups aren't the fastest, so you'll have to try and see if performance becomes an issue.

### #3Aardvajk  Members

12289
Like
0Likes
Like

Posted 08 May 2013 - 03:40 AM

As far as I'm aware, on D3D9 hardware, vs_3_0 texture fetches are your only option.

3.0 vertex texture look-ups aren't the fastest, so you'll have to try and see if performance becomes an issue.

Thanks eppo, nice to have some confirmation that I'm on the right track. Appreciate it won't be as fast but hopefully a good compromise. Can maybe combine the existing system with a texture fetch if the branching doesn't make the performance even worse.

### #4mhagain  Members

12440
Like
2Likes
Like

Posted 08 May 2013 - 02:29 PM

Another option may be to live with the VS constants limit and split the model into multiple draw calls instead.  Of course, then you get extra draw call overhead so there's a balancing act involved, but on DX10+ class hardware draw calls are cheaper, even if using D3D9 so you may come out on the right side of it.

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.

### #5Aardvajk  Members

12289
Like
0Likes
Like

Posted 09 May 2013 - 07:17 AM

Another option may be to live with the VS constants limit and split the model into multiple draw calls instead.  Of course, then you get extra draw call overhead so there's a balancing act involved, but on DX10+ class hardware draw calls are cheaper, even if using D3D9 so you may come out on the right side of it.

Yes, I'm leaning towards this now. If I sort the model based on joint number, I should then be able to divide it up into a set of renders that keep the number of matrices in the array down to a reasonable number. Seems better than some exotic system using floating point textures and lookups.

This will actually be fine - the overhead I want to avoid per-frame is reconstructing the vertex buffers. Splitting a model into a few draw calls should be negligable.

Thanks.

3482
Like
1Likes
Like

Posted 10 May 2013 - 04:38 PM

On my laptop I can't get more than about 40 matrices in an array before it bombs out.

DX9 hardware supports 256 vertex shader constants.

Assuming a 4x3 matrix for each bone transform, that should get you at best 256/3 = 85 matrices with one constant left unused.

That's not very realistic though. At a minimum you want 4 constants for the World-View-Projection transform, and probably a few more than that for other stuff.

Based on that, 80 matrices is a reasonable goal, and 70 should fit easily.

### #7Aardvajk  Members

12289
Like
0Likes
Like

Posted 11 May 2013 - 12:32 PM

On my laptop I can't get more than about 40 matrices in an array before it bombs out.

DX9 hardware supports 256 vertex shader constants.

Assuming a 4x3 matrix for each bone transform, that should get you at best 256/3 = 85 matrices with one constant left unused.

That's not very realistic though. At a minimum you want 4 constants for the World-View-Projection transform, and probably a few more than that for other stuff.

Based on that, 80 matrices is a reasonable goal, and 70 should fit easily.

Thanks for the info.

### #8MJP  Moderators

18236
Like
1Likes
Like

Posted 11 May 2013 - 09:18 PM

As far as I'm aware, on D3D9 hardware, vs_3_0 texture fetches are your only option.

3.0 vertex texture look-ups aren't the fastest, so you'll have to try and see if performance becomes an issue.

It's not that vertex texture lookups are slow if you use vs_3_0, it's that the earliest SM3.0 hardware that supported vertex texture fetch was really slow at it (Nvidia 6000 and 7000 series). Any DX10 or higher GPU will have no problem with sampling textures in a vertex shader.

### #9Steve_Segreto  Members

2008
Like
0Likes
Like

Posted 12 May 2013 - 03:52 AM

As said before you should consider having your modeler split the model into different "skin partitions" where the vertices of each skin partition use at most say 60 bone transforms, also I think you should consider encoding the matrix palette transforms as a quat rotation + translation to take up half as many registers.

### #10mhagain  Members

12440
Like
0Likes
Like

Posted 12 May 2013 - 07:13 AM

also I think you should consider encoding the matrix palette transforms as a quat rotation + translation to take up half as many registers.

That's a tradeoff as you're going to need to convert it to a matrix for each bone in your vertex shader, so you could end up with less storage but at a cost of a high additional per-vertex computational overhead (if hardware supported quaternion transforms it would be different, of course).

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.

### #11kalle_h  Members

2402
Like
1Likes
Like

Posted 12 May 2013 - 05:53 PM

also I think you should consider encoding the matrix palette transforms as a quat rotation + translation to take up half as many registers.

That's a tradeoff as you're going to need to convert it to a matrix for each bone in your vertex shader, so you could end up with less storage but at a cost of a high additional per-vertex computational overhead (if hardware supported quaternion transforms it would be different, of course).

vec4	orientation	= u_quaternionArray[index];

float invS  = 2.0 / dot(orientation, orientation);
vec3 s = orientation.xyz * invS;
vec3 w = orientation.w   * s;
vec3 x = orientation.x   * s;
vec3 y = orientation.yyz * s.yzz;

vec4	posAndScale	= u_positionAndScaleArray[index];//xyz = translation, w = scale

mat4	objectToWorld = mat4(
posAndScale.w * vec4(1.0 - (y.x + y.z), x.y - w.z, x.z + w.y, 0.0),
posAndScale.w * vec4(x.y + w.z, 1.0 - (x.x + y.z), y.y - w.x, 0.0),
posAndScale.w * vec4(x.z - w.y, y.y + w.x, 1.0 - (x.x + y.x), 0.0),
vec4(posAndScale.xyz, 1.0));


Additional cost per vertex is not that big after all. Didn't even notice performance drop with iphone4s.  It's quite gpu friendly ALU code after all.

### #12Steve_Segreto  Members

2008
Like
0Likes
Like

Posted 12 May 2013 - 11:35 PM

also I think you should consider encoding the matrix palette transforms as a quat rotation + translation to take up half as many registers.

That's a tradeoff as you're going to need to convert it to a matrix for each bone in your vertex shader, so you could end up with less storage but at a cost of a high additional per-vertex computational overhead (if hardware supported quaternion transforms it would be different, of course).

vec4	orientation	= u_quaternionArray[index];

float invS  = 2.0 / dot(orientation, orientation);
vec3 s = orientation.xyz * invS;
vec3 w = orientation.w   * s;
vec3 x = orientation.x   * s;
vec3 y = orientation.yyz * s.yzz;

vec4	posAndScale	= u_positionAndScaleArray[index];//xyz = translation, w = scale

mat4	objectToWorld = mat4(
posAndScale.w * vec4(1.0 - (y.x + y.z), x.y - w.z, x.z + w.y, 0.0),
posAndScale.w * vec4(x.y + w.z, 1.0 - (x.x + y.z), y.y - w.x, 0.0),
posAndScale.w * vec4(x.z - w.y, y.y + w.x, 1.0 - (x.x + y.x), 0.0),
vec4(posAndScale.xyz, 1.0));


Additional cost per vertex is not that big after all. Didn't even notice performance drop with iphone4s.  It's quite gpu friendly ALU code after all.

Yes I'm aware its a tradeoff. Pretty cool that kalle_h profiled it on iphone4s and was happy with the tradeoff. Honestly re-reading the OP, I shouldn't even have suggested this since they are writing a 3D modeler, not a skinned character animation/rendering engine. So really all that one would expect from a modeler program is to break the character up into skin partitions based on constraints such as matrix palette array size and texture, as well as limiting per-vertex bone weights/indices to 4 or less, as well as possibly some sort of texture map description per vertex (normal, bump, refraction, etc). How an engine consumes that data is of little interest, provided the engine does not need to do costly processing on it (such as breaking it into skin partitions to meet Vertex Shader register limitations).

Edited by Steve_Segreto, 12 May 2013 - 11:40 PM.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.