1
$\begingroup$

I have a sampler variable in a shader:

 uniform sampler2DArray tiles;

Which I am trying to bind to a texture unit in GL:

    int tilesUniformIndex = glGetUniformLocation(shaderProgramId,"tiles");
    System.out.println("tiles: "+tilesUniformIndex);
    glUseProgram(shaderProgramId);
    glok();  // <- This calls glGetError() and throws exception if not-ok
    glUniform1i(tilesUniformIndex, GL_TEXTURE0 + TILE_TEXTURE_UNIT_INDEX);
    glok();

TILE_TEXTURE_UNIT_INDEX is a constant defined to be zero.

The location is being correctly introspected, output is:

 tiles: 7

which is the correct binding location I expect in context of the shader source.

The /second/ glok() is throwing an exception, with 0x501 GL_INVALID_VALUE.

According to the documentation, this can only happen if the count parameter is less than zero. But glUniform1i /doesn't have a count parameter/!

I've tried:

glUniform1iv(tilesUniformIndex, 1, new int[]{GL_TEXTURE0 + TILE_TEXTURE_UNIT_INDEX});

as well, and the implementation of that does what it looks like (passes a memory pointer to the array data as output value).

So, what gives? Anyone know how this function can possibly behave like this? This is a simple, correct use case, right?

$\endgroup$
5
  • 2
    $\begingroup$ GL_TEXTURE0 + TILE_TEXTURE_UNIT_INDEX You don't set sampler uniforms to OpenGL enumerators. You set them to the actual texture unit index. This isn't your actual problem, but it's non-functional never-the-less. $\endgroup$ Commented Oct 17, 2019 at 2:07
  • $\begingroup$ I take it the shader compiled successfully at both stages ? (compile & link) $\endgroup$
    – PaulHK
    Commented Oct 18, 2019 at 2:17
  • $\begingroup$ Yup. (the first glok() not throwing an exception indicates this, but I also checked GL_LINK_STATUS with glProgram...) $\endgroup$
    – BadZen
    Commented Oct 18, 2019 at 3:11
  • $\begingroup$ I notice you're using a sampler2DArray, which isn't part of the GLSL spec (google tells me its an nvidia extension). But your glUniform1i example looks like it assuming the variable is a "sampler2D" - is this intentional ? This is in addition to Nicol Bolas's point about the GL_TEXTURE0 issue. $\endgroup$
    – PaulHK
    Commented Oct 31, 2019 at 9:40
  • 1
    $\begingroup$ sampler2Darray has been core GL since 3.0 / GLSL330. It was an extension before that but it certainly, definitely, absolutely supported (wokring now, core profile, not nvidia hardware, no extensions, etc) $\endgroup$
    – BadZen
    Commented Nov 25, 2019 at 16:37

1 Answer 1

1
$\begingroup$

I've just come across this issue myself, glUniform1i() returning GL_INVALID_VALUE when it's not documented.. The error was actually the GL_TEXTURE0 + TILE_TEXTURE_UNIT_INDEX which I was using as well. Calling glUniform1i(tilesUniformIndex, TILE_TEXTURE_UNIT_INDEX) is indeed the fix, as proposed by Nicol Bolas.

From my POV this seems to be an error in the documentation.

Not enough rep to comment!

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.