matrix inverse

What is the better choice to compute a matrix inverse in a shader -on gpu- or in the program
-on cpu-.

Depends on what you’re doing.
There are some inbuilt matrices readily available, no need to build those.
If the matrix doesn’t change per vertex or per fragment (ouch) there is no need to build it inside a shader.
If it does, build it where appropriate.
A general matrix inversion will be costly. Some special cases are just a transposition.

If your talking about general purpose GPU matrix inversion algorithms, that’s a different topic.

That depends…

If you change your matrix very often it may be better to calculate in a shader, otherwise it’s better to calculate on the CPU.

For example, if you have a matrix that is only changed once per model, it is better to calculate the inverse once on the CPU that to calculate it for every vertex on the GPU. But if you have a matrix that changes for every vertex, it may be better to calculate it in the vertex shader.

As a rule of thumb, prefer the method that is as far as possible on top of this list:

  • precalculate once at loading time
  • calculate once per batch on the CPU
  • calculate in the vertex shader
  • calculate in the fragment shader

Of course “as far as possible” means without sacrificing accuracy :wink: .

You may trade bandwith for computation power by calculating something in the vertex shader so you need less per vertex attributes. So if your matrix is per vertex but static, you could try to move your calculation to the shader and see if it improves something…

EDIT:
Doh… 2 minutes late :smiley:

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.