Wednesday, July 13, 2016

opengl - Should the modelview and projection matrices be calculated in the shader or on the CPU?


At minimum I would have a camera with rotation and world position; projections parameters such as angle of view and perspective vs. orthographic; and meshes with scale, angle, and world position.


When rendering a mesh I am wondering if I should calculate the final transformation matrix on the CPU and pass it to my shader, or if I should calculate the transformation matrix inside my shader.



Answer



There is no reason to calculate transformation matrix in shader, it would be just waste of resources, because you would need to calculate it for every vertex and that's not good idea.


If you calculate matrix in cpu, it would cost you few instructions and 16 * float size data transfer to GPU.


If you calculate matrix in gpu, it would cost you few instructions * vertices count and 9 * float size transfer to GPU - position, rotation, scale.



No comments:

Post a Comment

Simple past, Present perfect Past perfect

Can you tell me which form of the following sentences is the correct one please? Imagine two friends discussing the gym... I was in a good s...