Calculations on the CPU or the GPU?

Started by
4 comments, last by JimmyDeemo 13 years, 1 month ago
So i have been putting together a little game as a (re)learning experience. This time round i have been using OpenGL3 and shaders and there are a few things i'm confused about regarding calculations performed in regular code and those in the shader.

So with that in mind, can anyone give me a hand understanding the following questions.

1. Right now my simple shader looks like this:


#version 330

uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
uniform mat4 modelMatrix;

layout (location = 0) in vec4 in_Position;
layout (location = 1) in vec4 in_Color;

smooth out vec4 pass_Color;

void main(void)
{
gl_Position = projectionMatrix * viewMatrix * modelMatrix * in_Position;
pass_Color = in_Color;
}



So i set my projection, view and model matrix in my main code and simply multiply them in the shader, is that right? Should i be passing more 'raw' data to the GPU and calculating the matrices there?

2. Right now in order to translate and rotate my models (just a cube right now), I use the model matrix. I start with an identity matrix and then apply each transform to it before passing the matrix to the shader (see above). Is that the right idea or should i be using the model matrix for something else AND applying my transformations via another matrix.

Any pointers on this would be much appreciated, thanks in advance.
Advertisement
Sounds like your doing everyhting ok. The vertex shader is run for each vertex, imagine how many times you'd need to recreate your matrixes if you built them in the vertex shader (or worse, in the pixel shader). I could see the merit in working out the projection-virew-model matrix before hand. Your using the model matrix correctly.

I'd stick to what your currently doing.

Interested in Fractals? Check out my App, Fractal Scout, free on the Google Play store.


I'd stick to what your currently doing.



Phew that's good to know. I see what you mean about the matrices, it does seems like it would be expensive to do it like that.

However, bearing in mind what you said would it be more efficient to calculate the model matrix in the vertex shader? This is constantly changing, unlike (in my case) the projection and view matrices.

I suppose this is really an optimisation question isn't it as both methods have the same output; optimisation it probably overkill in my situation as I'm producing a small demo and nothing that complicated.

Always calculate 'model constants', like your model matrix, on the CPU. Like Nanoha said, re-calculating your matrixes per vertex is a waste, even if you're processing only one vertex. The reason for this is that the GPU has many cores, but the speed of each core is lower than the speed of the CPU core.
You can also pre-multiply your ProjectionMatrix et viewMatrix on CPU as they are independant of the model you're drawing. Thus you can save some bandwith and GPU computations.
Some of my previous work on my personal webpage
Ahh i see. Ok thanks a lot for the comments guys. Much appreciated.

This topic is closed to new replies.

Advertisement