6

I have two sampler arrays in my fragment shader:

uniform sampler2D shadowMaps[12];
uniform samplerCubeShadow shadowMapsCube[12];

This works fine on my pc with opengl 4.2, however on my laptop (opengl 3.1) I'm getting the error 'array size too big'.

If I set it to 8, it works fine. Arrays of other types can be far larger however, and I can add more sampler arrays with a max size of 8 without a problem. So, how is this limit determined?

After lowering the array size to 8 the compilation works, but the linking fails silently (The log is empty and glGetError() returns 0).

If I declare each sampler individually (uniform sampler2D shadowMap1;uniform sampler2D shadowMap2; etc.), neither of these errors occur.

Silverlan
  • 2,783
  • 3
  • 31
  • 66
  • 1
    How are you then accessing to those samplers? Bare in mind that depending on your OpenGL version, accessing to samplers array using loops is not permited. See: http://stackoverflow.com/a/12031821/988027 – Dan Feb 03 '14 at 10:23
  • I was in fact using a loop to access them. Hm... the article mentioned array textures, but there doesn't seem to be a texture array type for samplerCubeShadow. Does that mean I'll have to declare them individually and access them inside the loop through if-conditions, or is there another way? – Silverlan Feb 03 '14 at 10:58
  • 2
    You probably want to have a look at this: http://www.opengl.org/wiki/GLSL_Sampler#Non-uniform_flow_control – Dan Feb 03 '14 at 11:14
  • 2
    It means you have to use an _integral constant expression_ for the index. No matter what else you do, it's what ends up being in the index that matters for pre-4.0. Running a loop is fine, as long as you're not using the loop counter as index. – Damon Feb 03 '14 at 11:21
  • Thanks, I've worked around the problem now. It's still failing to link if I have more than a total of 16 samplers in my shader (Again, only on my laptop with opengl 3.1). Is there a way to retrieve the max amount of samplers you can have? – Silverlan Feb 03 '14 at 14:10

1 Answers1

8

You have to take two things into account.

First, bear in mind that depending on your OpenGL version, accessing to samplers array using variables inside loops is not permited. See: https://stackoverflow.com/a/12031821/988027

Secondly, quoting from the OpenGL wiki page, there are a maximum amount of texture units you can use at the same time:

OpenGL contexts have a maximum number of texture image units, queriable from the constant GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS​.

Probably, this answer will help you. Particularly, have a look at the shader resource limitations.

JWWalker
  • 22,385
  • 6
  • 55
  • 76
Dan
  • 1,466
  • 1
  • 13
  • 27
  • 4
    `GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS​` is not the best limit to query in this case. There is also a per-stage limitation: (e.g. `GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS`. It is guaranteed to be at least 16 for all stages in desktop GL. But this limit is the one that actually restricts the number of samplers that can be used in a single shader invocation, the other limit is the total number of texture image units (48+ in GL3, 80+ in GL4). – Andon M. Coleman Feb 03 '14 at 16:24
  • Just to clarify, a "texture image unit" is different from an "image unit" (ARB_image_load_store)? – jozxyqk Mar 10 '14 at 08:41
  • OpenGL wiki page's link is dead – Winter Mar 24 '20 at 00:43