问题
Are glGetUniformLocation
and glGetAttribLocation
time consuming?
Which way is better?
- Call
glGetAttribLocation
orglGetUniformLocation
every time I need it ? - Store locations in varables and use them when needed ?
回答1:
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
回答2:
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.
回答3:
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.
来源:https://stackoverflow.com/questions/11802885/opengl-should-i-store-attribute-uniform-locations