OpenGL - Should I store Attribute/Uniform locations?

烈酒焚心 提交于 2020-01-03 09:01:15

问题


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 ?

回答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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!