5
$\begingroup$

I have two methods of calculating tangent and cotengent (needed for normalMap lighting calculation).

  • The one is doing it from CPP code (with assimp library for example)
  • The second is doing it directly in shader (in Vertex shader/in fragment shader)

What is the best optimized method? (optimized for speed, with vsync activated)

I use Opengl 3.3+

$\endgroup$
  • 1
    $\begingroup$ Optimized for what? Memory use, speed etc. ? How often does your data update and so on. $\endgroup$ – joojaa Sep 12 '16 at 7:32
  • $\begingroup$ Optimized for speed $\endgroup$ – Irrmich Sep 12 '16 at 7:36
6
$\begingroup$

Doing it in the CPU side during initialization is what I'd go for, this is assuming you are initialising that data once and passing it to the GPU.

On the GPU side, doing the calculations per fragment would be too costly and should be out of the question, unless it's really needed for a special effect then you shouldn't add unneeded ALU work to the fragment shader, this will only cause your application to slow down. On the vertex shader is more acceptable but again if you have highly tessellated geometry, it'll be expensive.

Whenever there is data that you can compute just once, then go for that instead of doing it every frame. Even if you were planning on doing it just once on the GPU and using transform feedback or an FBO to store the data, I think this approach would be more costly. As always though, profile your application, careful tradeoffs are specific to architectures.

$\endgroup$
  • 2
    $\begingroup$ Just to add some more details about how the right answer can be situational, if your application is CPU bound, that means your GPU is doing less work and is waiting on your CPU between frames. In this case, it would be less expensive to do it on the GPU since the CPU is already working the entire time, but the GPU isn't. If you are GPU bound, the reverse is true. Also, passing tangent and bitangent increases memory usage. If you are memory bound, calculating in the shader can be a good move. $\endgroup$ – Alan Wolfe Sep 13 '16 at 14:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.