开发者

Avoiding glBindTexture() calls?

开发者 https://www.devze.com 2023-01-15 23:21 出处:网络
My game renders lots of cubes which randomly have 1 of 12 textures. I already Z order the geometry so therefore I cant just render all the cubes with texture1 then 2 then 3 etc... b开发者_如何学Goecau

My game renders lots of cubes which randomly have 1 of 12 textures. I already Z order the geometry so therefore I cant just render all the cubes with texture1 then 2 then 3 etc... b开发者_如何学Goecause that would defeat z ordering. I already keep track of the previous texture and in they are == then I do not call glbindtexture, but its still way too many calls to this. What else can I do?

Thanks


Ultimate and fastest way would be to have an array of textures (normal ones or cubemaps). Then dynamically fetch the texture slice according to an id stored in each cube instance data/ or cube face data (if you want a different texture on a per cube face basis) using GLSL built-in gl_InstanceID or gl_PrimitiveID.

With this implementation you would bind your texture array just once.

This would of course required used of gpu_shader4 and texture_array extensions:

  • http://developer.download.nvidia.com/opengl/specs/GL_EXT_gpu_shader4.txt
  • http://developer.download.nvidia.com/opengl/specs/GL_EXT_texture_array.txt

I have used this mechanism (using D3D10, but principle applies too) and it worked very well.

I had to map on sprites (3D points of a constant screen size of 9x9 or 15x15 pixels IIRC) differents textures indicating each a different meaning for the user.

Edit:

If you don't feel comfy with all shader stuff, I would simply sort cubes by textures, and don't Z order the geometry. Then measure performances gains.

Also I would try to add a pre-Z pass where you render all your cubes in Z buffer only, then render normal scene, and see if it speed up things (if fragments bound, it could help).


You can pack your textures into one texture and offset the texture coordinates accordingly

glMatrixMode(GL_TEXTURE) will also allow you to perform transformations on the texture space (to avoid changing all the texture coords)


Also from NVIDIA: Bindless Graphics

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号