4
\$\begingroup\$

I am a beginner in OpenGL. I am learning about textures in OpenGL. What I don't understand is how to determine how many texture units are in the GPU. I heard someone said that you can see how many texture units by writing the following code.

int total_units;
glGetIntegerv(GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS, &total_units);
std::cout << total_units << '\n';  // the result is 192

Is there 192 texture units in my GPU? In documentation, it says:

GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS params returns one value, the maximum supported texture image units that can be used to access texture maps from the vertex shader and the fragment processor combined. If both the vertex shader and the fragment processing stage access the same texture image unit, then that counts as using two texture image units against this limit. The value must be at least 48. See glActiveTexture.

So I wanted to know how many texture units can be used to access texture maps from the vertex and fragment shaders. So I wrote and run the following code.

int vertex_units, fragment_units;
glGetIntegerv(GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS, &vertex_units);
std::cout << vertex_units << "\n";   // the result is 32
glGetInteferv(GL_MAX_TEXTURE_IMAGE_UNITS, &fragment_units);
std::cout << fragment_units << "\n"; // the result is also 32

So 32+32=64. But why does GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS shows me the result of 192? I think I am missing something. What do I need to calculate to get 192?

And also, in OpenGL, why are there only GL_TEXTURE0 to GL_TEXTURE31 macros? I think these macros are for each shaders. Am I right?

\$\endgroup\$
0

1 Answer 1

4
\$\begingroup\$

What do I need to calculate to get 192?

You need to recognize the fact that there are more than 2 shader stages. OpenGL 4.5 has six programmable stages: vertex, tessellation control, tessellation evaluation, geometry, fragment, and compute. Each of them can access some number of textures, and the "combined" value specifies how many can be accessed by all stages in total.

why are there only GL_TEXTURE0 to GL_TEXTURE31 macros?

The proper way to set the active texture unit is with the value GL_TEXTURE0 + i, where i is the texture unit index. All of the macros 1 to 31 are monotonically increasing in value, so GL_TEXTURE0 + 5 is the same value as GL_TEXTURE5.

So to set the active texture unit, use glActiveTexture(GL_TEXTURE0 + i);.

Basically, OpenGL only defines 32 macros because they never needed to define more than 1. The other 31 are just for the sake of convenience.

\$\endgroup\$
10
  • \$\begingroup\$ OpenGL 4.5 uses texture names and binds them to units. Units are referenced in the shaders. No active texture is needed. And macros can be defined at any time, they are not limited to utility headers. \$\endgroup\$
    – user144188
    May 8, 2021 at 13:43
  • \$\begingroup\$ @a_donda: All versions of OpenGL use "texture names" (ie: the things you get from glGen/CreateTextures. And all versions of OpenGL allow you to bind them to texture units (which happens whenever you call glBindTexture). \$\endgroup\$ May 8, 2021 at 13:45
  • \$\begingroup\$ The 4.5 apis are glCreateTextures() and glBindTextureUnit(), and they avoid much of the confusion that comes up with older style, like the one how come the different unit numbers to be. The docu mentions only vertex- and fragment stage. \$\endgroup\$
    – user144188
    May 8, 2021 at 13:51
  • \$\begingroup\$ @a_donda: "The docu mentions nowhere the stages other than vertex- and fragment." I don't know what "docu" you are referring to, but the OpenGL specification has recognized more than 2 shader stages since GL 3.2. And we've had 6 stages since 4.3. Can you link to the "docu" that says there are only vertex and fragment shader stages? \$\endgroup\$ May 8, 2021 at 13:53
  • \$\begingroup\$ @a_donda: "The 4.5 apis are glCreateTextures() and glBindTextureUnit(), and they avoid much of the confusion that comes up with older style" Sure, but that's not what the OP asked about, so while it is better form, it's rather orthogonal to the point of the question. \$\endgroup\$ May 8, 2021 at 13:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .