简体   繁体   中英

GLSL Texture Mapping, Max number of textures

I am working on a C++ GLSL renderer. I figured out how to map multiple textures in GLSL. using " glActiveTexture(GL_TEXTURE0) " I can tell my shader which texture to load. The only problem is the constants GL_TEXTURE0 , GL_TEXTURE1 , ... , only go up to GL_TEXTURE31 . Does this mean you can only load 32 textures for you scene? Surely not.

Example:

unsigned int textureIDs[2];

glGenTextures(2, textureIDs);

glActiveTexture(GL_TEXTURE0);

glBindTexture(GL_TEXTURE_2D, textureIDs[0]);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image1.x, image1.y, 0, GL_RGB, GL_UNSIGNED_BYTE, image1.data);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);


glActiveTexture(GL_TEXTURE1);

glBindTexture(GL_TEXTURE_2D, textureIDs[1]);

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, image2.x, image2.y, 0, GL_RGB, GL_UNSIGNED_BYTE, image2.data);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

... Later on in my draw function ...

int handle_loc = glGetUniformLocation( ProgramHandle, "Tex1" );

glUniform1i( handle_loc, val );

... If the variable "val" is set to 0, the texture data from image1 is mapped to my shader, if it is set to 1, the texture data from image2 is mapped to my shader. ...

What am I doing wrong, how can I load more than 32 textures?

The constant only goes up to GL_TEXTURE31 because that is all that is required by GL3. There is a requirement of 16 texture image units per shader stage, and GL3 only has 2 stages (vertex and fragment).

Later on GL (3.2) promoted Geometry Shaders to core, so the minimum number of texture image units became 48 (thus GL_TEXTURE47 would be the highest an implementation would have to define).

GL4 adds Tessellation Evaluation / Control stages, which bumps the minimum up to 80 (16 x 5 stages) . At this point, however, most people stop using the constants and just use GL_TEXTURE0 + N to describe the N'th texture image unit.


As far as the number of textures you can have loaded, that is completely independent of the number of texture image units your implementation supports. Texture image units are binding locations, they are related to the maximum number of textures you can apply in a single invocation of a shader (in other words per-pass).

You can always resort to multiple passes if you require more than 16 textures in a shader stage, but I think you are confusing having textures "loaded" with having them bound to a texture image unit.

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