Edvin
Edvin

Reputation: 2011

Optimal matrix structure and CPU/GPU communication in modern OpenGL with glsl

I want to know your thoughts regarding where matrix multiplication should be processed, where to store the results and general communication between the CPU and GPU.

  1. Since the MVP matrix is the same for each object, calculating it on the CPU and then send it to the shaders seems like the way to go, yes?

  2. If I have multiple shaders and some only need to model matrix, some need only the MVP matrix and some need both. How should I proceed?

  3. I currently have a shader object for each vertex/fragment shader pair that stores the location of each uniform in the shaders. This seems like a good practise, yes?

Upvotes: 0

Views: 302

Answers (1)

Jennifer Wilcox
Jennifer Wilcox

Reputation: 343

  1. This may depend on how many vertices you process in a single draw call. If you only process a small handful of vertices then you're probably better off sending the M, V and P seprately, as GPUs tend to be much faster at floating point operations (most cases wouldn't take this approach though; you'd want to profile with your specific models to be sure). On the other hand, if you're rendering a lot of vertices in each call you'd may be better off doing the calculation on the CPU once (even though it will be a bit slower) because this means you won't keep recomputing the same value over and over again in the shader.
  2. If you're sending the whole MVP as one matrix then in the case where you need both MVP and M (I think lighting was a case where I ran into this) then just send both, as you don't really have many other options. And if you only need M then only send M.
  3. I don't understand what you mean here (and I don't have enough rep to ask in a comment). If you mean that you externally store the uniform IDs instead of querying them each render loop then yes that is pretty standard procedure.

More generally when grappling with performance questions like these your best bet is to use a profiler (something graphics specific like GPUPerfStudio would be good for this sort of problem) and find out which method is faster in your specific case, as many performance tweaks have varying degrees of usefulness depending on the more specific scenarios.

Upvotes: 1

Related Questions