<p>I've just come across this issue myself, <code>glUniform1i()</code> returning <code>GL_INVALID_VALUE </code>when it's not documented..
The error was actually the <code>GL_TEXTURE0 + TILE_TEXTURE_UNIT_INDEX</code> which I was using as well.
Calling <code>glUniform1i(tilesUniformIndex, TILE_TEXTURE_UNIT_INDEX)</code> is indeed the fix, as proposed by <a href="https://computergraphics.stackexchange.com/users/2654/nicol-bolas">Nicol Bolas</a>.</p>
<p>From my POV this seems to be an error in the documentation.</p>
<p>Not enough rep to comment!</p>

CC BY-SA 4.0
2024-04-22T17:30:27.800Z
2024-04-22T17:31:55.280Z
2024-04-22T17:31:55.280Z
Kerch
<p>I have a sampler variable in a shader:</p>

<pre><code> uniform sampler2DArray tiles;
</code></pre>

<p>Which I am trying to bind to a texture unit in GL:</p>

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

<p><code>TILE_TEXTURE_UNIT_INDEX</code> is a constant defined to be zero.</p>

<p>The location is being correctly introspected, output is:</p>

<pre><code> tiles: 7
</code></pre>

<p>which is the correct binding location I expect in context of the shader source.</p>

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

<p>According to <a href="https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glUniform.xhtml" rel="nofollow noreferrer">the documentation</a>, this can only happen if the <code>count</code> parameter is less than zero.  But <code>glUniform1i</code> /doesn't have a count parameter/!</p>

<p>I've tried:</p>

<pre><code>glUniform1iv(tilesUniformIndex, 1, new int[]{GL_TEXTURE0 + TILE_TEXTURE_UNIT_INDEX});
</code></pre>

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

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

CC BY-SA 4.0
2019-10-17T02:07:26.873Z
`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.
Nicol Bolas
CC BY-SA 4.0
2019-10-18T02:17:11.190Z
I take it the shader compiled successfully at both stages ? (compile & link)
PaulHK
CC BY-SA 4.0
2019-10-18T03:11:19.757Z
Yup.  (the first glok() not throwing an exception indicates this, but I also checked GL_LINK_STATUS  with glProgram...)
BadZen
CC BY-SA 4.0
2019-10-31T09:40:37.027Z
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.
PaulHK
CC BY-SA 4.0
2019-11-25T16:37:57.190Z
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)
BadZen
<opengl><texture>
Why is glUniform1i() returning GL_INVALID_VALUE?
BadZen
# Why is glUniform1i() returning GL_INVALID_VALUE?

(Asked by: username_0 on 2019-10-16)

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](https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glUniform.xhtml), 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?

**Tags:** opengl, texture

> **Comments:**
> **username_1**: `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.  
> **username_2**: I take it the shader compiled successfully at both stages ? (compile & link)  
> **username_0**: Yup.  (the first glok() not throwing an exception indicates this, but I also checked GL_LINK_STATUS  with glProgram...)  
> **username_2**: 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 username_1 username_1's point about the GL_TEXTURE0 issue.  
> **username_0**: 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)  

---

## 1 answers

### Answer by username_3 on 2024-04-22. Score: 1

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 [username_1 username_1](https://computergraphics.stackexchange.com/users/2654/username_1).

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

Not enough rep to comment!


---

