Jump to content
  • Advertisement
Sign in to follow this  
sunsflower

OpenGL how to properly managing textures?

This topic is 1026 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

In opengl there are texture units with different texture bindings and array textures. this makes me a bit confused what feature I should use if I have lots of textures. it seems that I can only use 4 texture units on my machine. and (1) is there a limit to the number of elements in an array texture object? can I just use many small textures and put them in array textures instead of a large texture atlas? ; or (2) should I bind different textures when using them? ; or (3) have many "uniform sampler2D(array) m_textureN" in my shader and binding to them?

Share this post


Link to post
Share on other sites
Advertisement
about (2): which means call "glBindTexture(GL_TEXTURE_2D, texture2d1ID)" in render loop to set different textures..
about (3): have many "uniform sampler2D m_textureN" :
//fragment shader
layout(location = 2) uniform sampler2DArray texarray;
layout(location = 3) uniform sampler2D tex2d1;
layout(location = 4) uniform sampler2D tex2d2;
is setting texture unit the only way to assign those different textures[ike: layout(binding = 2) uniform sampler2D tex2d1]? or I can use location to specify?

Share this post


Link to post
Share on other sites

There may be a limit to how many textures you can use an ONCE but I suspect you wouldn't reach that limit through standard use (there is probably a total limit too but I would imagine that is very high as well).

 

The simplest thing for you to do is to just bind a new texture each time you need a new texture.

glBindTexture(GL_TEXTURE_2D, rock);
DrawRock();
glBindTexture(GL_TEXTURE_2D, grass);
DrawGrass();
etc.

I doubt you will hit a limit to how many textures you can use that way and it is the simplest approach.

Next up is if you need to use more than one texture at a time, maybe you are doing a landscapr and need to blend between rock and grass in a shader.

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, rock);
glActiveTexture(GL_TEXTURE1);
glBindTexture(GL_TEXTURE_2D, grass);
DrawRockyGrass();
I did a quick check with intelisense and the highest GL_TEXTURE? that showed up for me was 29! That's pretty high but it may be limited by other factors. Still I would think you can use more than enough textures at once for whatever your needs.
 
Finally:
layout(location = 2) uniform sampler2DArray texarray;
layout(location = 3) uniform sampler2D tex2d1;
layout(location = 4) uniform sampler2D tex2d2;
 
You don't NEED to do this, you can treat them as normal uniforms and set their locations from code
uniform sampler2DArray texarray;
uniform sampler2D tex2d1;
uniform sampler2D tex2d2;
location = glGetAttribLocation(shader, tex2d1);
glProgramUniform1i(shader, location, 3);
glActiveTexture(GL_TEXTURE3);
glBindTexture(GL_TEXTURE_2D, grass);
RenderRockyGrass();

Just think about how many textures you will need to use at the same time, it probably won't be very high unless you are doing various maps - normal maps, diffuse maps, specular maps etc.

Edited by Nanoha

Share this post


Link to post
Share on other sites

According to opengl wiki

 

 

 

Array texture sizes are normally limited to the usual GL_MAX_TEXTURE_SIZE limitation. However, the dimension that represents the number of array levels is limited by GL_MAX_ARRAY_TEXTURE_LAYERS, which is generally much smaller. OpenGL 4.5 requires that the limit be at least 2048, while OpenGL 3.3 requires it be at least 256.

 

So if youre not using the very latest gl version, you are only guaranteed 256 textures in the array.

NOTE: looking online, most discrete gpu seem to support 2048 (nvidia), 8192 (amd), but intel integrated gpus are closer to the actual minimum.

You can also use multiple array textures for some extra capacity.

Share this post


Link to post
Share on other sites
thanks a lot! I think now I've decided how to manage textures in a game engine...

and, another problem is that if I use "glBindTexture(GL_TEXTURE_2D, texture)" every frame or even many times a frame, will there be a overhead that will damage the performance?

my idea about sprite animation is:

use one " uniform sampler2D texture" and one "uniform sampler2DArray texarr" in fragment shader. also a "uniform int curframe" to indicate which frame is on now. In the render function, I traverse every sprite and update the "texture" (with a normap 2d texture loaded before) and the "texarr" (with a normal 2d array texture loaded before) with the info stored in that sprite. It might work... but is it ok? Also, using this method requires dozens of drawing calls (even hundreds of...because for every sprite I have to update a world transform uniform to specify its location).

sorry for the badly organized post

Share this post


Link to post
Share on other sites

it seems that I can only use 4 texture units on my machine.

 

Just to clarify, you can have hundreds of textures loaded/created on the videocard, but you can only use 4 textures at the same time for a single drawcall; for example, if you have four texture units, that means you can only use 4 textures on a single triangle. To additionally confuse things, a "Texture" can actually be an array of multiple textures, and only take one Texture Unit spot.

 

When drawing a frame, you might do something like this:

//==============================================//Just once when the level resources (or general game resources) is loaded.

TextureID grass = MyTextureRetriever.LoadFromFile("../my/textures/grass.png");
TextureID dirt     = MyTextureRetriever.LoadFromMemory(dirtTextureData);
TextureID mask = MyTextureRetriever.LoadFromTheInternet("ftp://127.0.0.1/textures/mask.png");

//==============================================
//On every frame:

//Bind the textures you are going to use.
Bind [grass] to GL_TEXTURE0 //Texture Unit 0
Bind [dirt] to GL_TEXTURE1 //Texture Unit 1
Bind [mask] to GL_TEXTURE2 //Texture Unit 2

//Draw the terrain using those textures.
DrawTerrain();

//Bind other textures.
Bind [crateTexture] to GL_TEXTURE0
Unbind GL_TEXTURE1 and GL_TEXTURE2 //Since we're not using them here

//Draw the objects using those textures.
DrawCrate();

You can keep hundreds of textures loaded, but you can only have several active at once, so during a single frame of your game, you might switch your active textures hundreds of times (though generally you try to reduce the amount of switching, by drawing all the objects that share the same texture before switching to the next texture and drawing the objects using that one).

 

You want to use more than one texture if you are doing texture splatting, or bump mapping, or any of hundreds of other techniques. Nowadays games use textures for storing all kinds of data.

 

IIRC, the minimum number of texture units required to be supported is eight, so I'm not sure where your four is coming from?

Share this post


Link to post
Share on other sites
thanks! I am now much more clear about this! And the "four" is a wrong result due to my passing a wrong parameter to glGetIntegerv.. it's actually 32 units..

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!