Pre calculating bi-tangents

Started by
3 comments, last by MJP 12 years, 9 months ago
Currently, in my projects, I pass the position, normal, tangent and texture UV of each vertex to the shaders, and then calculate the bi tangent in the pixel shader at runtime.
Is it faster to pre-calculate the bi-tangent of each vertex when loading the mesh instead of doing it in pixel shader every frame?
Or should I try both methods and see which is faster? Because the way I'm currently doing it, I dont have to pass so much information to the GPU which might be faster right?
Advertisement
It depends: Is the mesh dynamic, so that it is passed once per frame, or else is it static and only loaded once at all? Is the vertex layout cache friendly, e.g. does a single vertex fit into a multiple of 32 bytes? Is the computation a "simple" cross-product or is there some unpacking required before? Is the vertex cache used nicely, so that the GPU doesn't need to wait too much on the VRAM? ...

I think it is best to try both ways. Even then you have checked on a single configuration only, but perhaps the qualitative result holds for other configurations. too. Just my 2 Cents.
First you don't need to calculate it in the fragment shader. Calculate it in the vertex shader instead.
As for your question, that depends. If your program uses a lot of memory bandwidth then calculating on the gpu will be faster.
And if it is gpu bound then offloading it from the gpu will help, but really you need to profile your application to be sure.

It depends: Is the mesh dynamic, so that it is passed once per frame, or else is it static and only loaded once at all?

The mesh is static.


Is the vertex layout cache friendly, e.g. does a single vertex fit into a multiple of 32 bytes?

A single vertex uses 44 bytes. So If I added the bi-tangent it would use 56 bytes.


Is the computation a "simple" cross-product or is there some unpacking required before?

Just a simple cross-product.


Is the vertex cache used nicely, so that the GPU doesn't need to wait too much on the VRAM?

What does that mean?

Is this related to the memory bus? The vertex should fit in the memory bus width?
You could never say for sure without profiling your application with actual assets on target hardware. So if you really want to know for sure, you should try that.

In general, GPU's tend to have much more in the way of shader ALU as opposed to bandwidth. So I would guess that calculating the bitangent in the vertex shader would win out in most cases. However I would also guess that the difference would be pretty minimal.

This topic is closed to new replies.

Advertisement