简体   繁体   中英

Updating uniform buffer objects costs performance?

I replaced the uniforms like mvp matrix in shader with UBO to avoid huge volume of glUniform...() calls. But in order to update the UBO in each frame, I have to call glBufferData() which also costs performance. Now I'm a bit confused of whether to get back to use the uniforms or to use uniform blocks in the shader.

specifying a bunch of glUniform calls will be usually slower than a single call to glBufferData , except in the case when you only modify only a few uniform values each frame.

You should thus decide which strategy to adopt based on that. A couple notes:

  • Remember that glBufferData may not be the best way to update a buffer, because it requires the driver to copy the data from the pointer you provide before returning from the call. I would try using glMapBufferRange and write directly the changes into it. Remember that in order to avoid synchronization you need to tell the driver you are not going to read from the pointer it gives to you and that you'll be writing the whole range mapped using the flags GL_MAP_WRITE_BIT and GL_MAP_INVALIDATE_RANGE_BIT
  • Performance of opengl calls is implementation dependent: even if the driver is authorized to perform some optimization such as allocating a new buffer instead of waiting for the old one to be available since you didn't request to be able to use the old content in the mapped range, it could be synchronizing nevertheless, so the only reliable answer you can get is to try both on target hardware and do benchmarks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM