Page 1 of 1

How about using quaternion instead of matrix?

Posted: Wed Jun 23, 2010 8:16 am
by LangFox
http://code.google.com/p/kri/wiki/Quaternions
Quaternion can be used instead of matrix, like vector transform or skinned animation. It's small and fast.
Hope Ogre could integrate quaternion into shader pipeline. :idea:

Re: How about using quaternion instead of matrix?

Posted: Wed Jun 23, 2010 8:54 am
by Fulmens
http://www.ogre3d.org/docs/api/html/cla ... rnion.html

As for quaternion in shaders...it's more the shading language area (Cg/HLSL/GLSL).
(Does shader runtime already have quaternion manipulation functions, or are they limited to matrix ?)

Re: How about using quaternion instead of matrix?

Posted: Wed Jun 23, 2010 8:55 am
by xadhoom
Do you mean like the Ogre::Quaternion??

xad

Edit: Ah, Fulmens beat me :wink:

Re: How about using quaternion instead of matrix?

Posted: Wed Jun 23, 2010 9:54 am
by LangFox
No, I hope Ogre could update some shader parameters with quaternion, not matrix.
For example, use world_quat to give vertex shader a quaternion for world transform, and use bone_quat to give quaternions for hardware skinning.
Then Ogre will send much less data from CPU to GPU, when doing skinned animation or instanced drawing.

Re: How about using quaternion instead of matrix?

Posted: Wed Jun 23, 2010 5:02 pm
by xadhoom
I fear this would have a huge impact to the current Scenegraph system of Ogre and I´m not sure if the limitation of having uniform scales only would be feasable. Providing both approaches might be a solution by using compilation defines or proper subclassing but this would probably introduce a significant maintenance overhead.

xad

Re: How about using quaternion instead of matrix?

Posted: Wed Jun 23, 2010 5:17 pm
by xavier
LangFox wrote:No, I hope Ogre could update some shader parameters with quaternion, not matrix.
For example, use world_quat to give vertex shader a quaternion for world transform, and use bone_quat to give quaternions for hardware skinning.
Then Ogre will send much less data from CPU to GPU, when doing skinned animation or instanced drawing.
This is an ill-informed request. The problem is that there are no instructions on GPU hardware for dealing with quaternions (as you can tell from all of the code they have to supply for using quaternions in a shader), whereas all you need for dealing with matrix/vector operations is present in hardware.

Re: How about using quaternion instead of matrix?

Posted: Thu Jun 24, 2010 10:17 am
by _tommo_
xavier wrote: This is an ill-informed request. The problem is that there are no instructions on GPU hardware for dealing with quaternions (as you can tell from all of the code they have to supply for using quaternions in a shader), whereas all you need for dealing with matrix/vector operations is present in hardware.
Uhm, this is true, but on more recent GPUs the matrix and vector code is anyway trimmed down to parallel float operations... so having to do only four of them instead of 16 could be a real advantage?
Surely it wouldn't be big anyway, as 99% of the shaders is not float limited but bandwidth limited.

Re: How about using quaternion instead of matrix?

Posted: Thu Jun 24, 2010 4:50 pm
by xavier
_tommo_ wrote:
xavier wrote: This is an ill-informed request. The problem is that there are no instructions on GPU hardware for dealing with quaternions (as you can tell from all of the code they have to supply for using quaternions in a shader), whereas all you need for dealing with matrix/vector operations is present in hardware.
Uhm, this is true, but on more recent GPUs the matrix and vector code is anyway trimmed down to parallel float operations... so having to do only four of them instead of 16 could be a real advantage?
Surely it wouldn't be big anyway, as 99% of the shaders is not float limited but bandwidth limited.
Actually, in terms of the assembler, most matrix/vector multiplies are done as 3 or 4 dot products into a swizzle -- no need for all of the extra math I see in the code link. So a float4x4/float4 is already only 4 operations. On top of that, the fixed-function bits of the hardware are designed to use matrices, so you have to provide matrices anyway.

Re: How about using quaternion instead of matrix?

Posted: Thu Jun 24, 2010 6:55 pm
by _tommo_
xavier wrote:Actually, in terms of the assembler, most matrix/vector multiplies are done as 3 or 4 dot products into a swizzle -- no need for all of the extra math I see in the code link. So a float4x4/float4 is already only 4 operations. On top of that, the fixed-function bits of the hardware are designed to use matrices, so you have to provide matrices anyway.
Ok those are good points, i forgot that shaders are 4-wide...
I will save the quaternions for my GPGPU render :D

Re: How about using quaternion instead of matrix?

Posted: Sat Jun 26, 2010 1:42 pm
by debdatta_basu
On current gen hardware, there is no reason to use quaternions for ordinary transforms.... However, in certain cases, Quaternion based transforms provide better results..

Look up dual quaternion skinning.... I agree this would be a nice option to have, albeit if only for skinning.

Re: How about using quaternion instead of matrix?

Posted: Wed Jul 07, 2010 2:48 am
by cyrfer
xavier wrote: On top of that, the fixed-function bits of the hardware are designed to use matrices, so you have to provide matrices anyway.
Does this apply if you are using shaders? I don't see why matrix is required if you have your own vertex and pixel shaders. This seems like a reasonable request at least to prepare for when fixed function is not supported.

Re: How about using quaternion instead of matrix?

Posted: Wed Jul 07, 2010 1:23 pm
by Praetor

Code: Select all

vec = mul(myMat, vec);
The mul shader instruction is designed to multiply with matrices. It has a low-level implementation. You'd have to create custom implementations to handle quaternions which likely would not be as fast. Still, quaternions can be interesting. There's some skinning research using quaternions that shows promise. But, without it being supported from hardware it will probably lose out on speed.

Re: How about using quaternion instead of matrix?

Posted: Wed Jul 07, 2010 6:44 pm
by cyrfer
Praetor wrote:But, without it being supported from hardware it will probably lose out on speed.
I don't think the only point is speed. Quaternions can make it easier to do some operations (not talking about matrix multiplication), and may be faster for those operations (if that matters). Also, a quaternion can be delivered as a single uniform (float4) while a matrix is at least 3 uniforms. I provide per-instance rotations via quaternion maps when using hardware instancing, so it makes sense to delivery a parent transformation in a quaternion, avoiding the need to convert every local transformation into a matrix before calculating the combined orientation.