Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenGL - Should I store Attribute/Uniform locations?

Are glGetUniformLocation and glGetAttribLocation time consuming?

Which way is better?

  1. Call glGetAttribLocation or glGetUniformLocation every time I need it ?
  2. Store locations in varables and use them when needed ?
like image 590
EOG Avatar asked Aug 03 '12 20:08

EOG


3 Answers

Which way is better?

Think about it. No matter how fast glGetUniformLocation and glGetAttribLocation are, they cannot be faster than just fetching a variable. So if you are concerned about performance, then use the method that is always faster.

like image 68
Nicol Bolas Avatar answered Dec 10 '22 12:12

Nicol Bolas


According to my tests, fetching such locations took about 100~200 times the amount of time that it takes for a single glDrawElements call. This is only an estimate based on System.nanoTime(), but I think it's save to say that it's worth storing them in variables on initialization.

like image 26
Namgyu Ho Avatar answered Dec 10 '22 13:12

Namgyu Ho


Whether on Android or iPhone, for an opengl surface you will have methods like: onSurfaceCreated and onSurfaceChanged, get into the habit of fetching uniforms and attributes here in these 2 methods.

The only way you can make rendering faster (which will soon become your priority when your code crosses 1000 lines of code) is by only having gluseprogram, glbindbuffer, texture binds and other binds inside onDrawFrame method, always cache variables inside onSurfaceCreated and onSurfaceChanged

like image 37
Patt Mehta Avatar answered Dec 10 '22 13:12

Patt Mehta