<p>I'm using hardware tessellation to render a terrain mesh with variable LOD.</p>

<p>I have this snippet on my tessellation control shader:</p>

<pre><code>int edge1 = gl_InvocationID;
int edge2 = (gl_InvocationID + 1) % 3;
tcs_out[gl_InvocationID].color = vec4(computeLODColor(gl_TessLevelOuter[edge1], gl_TessLevelOuter[edge2]), 1.0);
</code></pre>

<p>This should compute a color for the control point based on the tessellation level of the two edges that intersect on the point (using quad patches).</p>

<p>It's all working fine except for this: <code>gl_TessLevelOuter[edge1]</code> and <code>gl_TessLevelOuter[edge1]</code>.</p>

<p>This indexing always return 0 (I've tried using it directly as a color). To test the problem, I tried this: <code>gl_TessLevelOuter[gl_InvocationID]</code>. It also has the same behavior.</p>

<p>If I do something like this: </p>

<pre><code>if(gl_InvocationID == 0) {
    tcs_out[gl_InvocationID].color = vec4(computeLODColor(gl_TessLevelOuter[0], gl_TessLevelOuter[1]), 1.0);
}
else if(gl_InvocationID == 1) {
    tcs_out[gl_InvocationID].color = vec4(computeLODColor(gl_TessLevelOuter[1], gl_TessLevelOuter[2]), 1.0);
}
else if(gl_InvocationID == 2) {
    tcs_out[gl_InvocationID].color = vec4(computeLODColor(gl_TessLevelOuter[2], gl_TessLevelOuter[3]), 1.0);
}
else if(gl_InvocationID == 3) {
    tcs_out[gl_InvocationID].color = vec4(computeLODColor(gl_TessLevelOuter[3], gl_TessLevelOuter[0]), 1.0);
}
</code></pre>

<p>then it all works as expected.</p>

<p>I've checked the OpenGL specification and couldn't find anything about undefined behavior when indexing <code>gl_TessLevelOuter</code> by a non-constant value.</p>

<p>So the question is, am I misunderstanding something, doing something against the specification or is it a GL implementation bug?</p>

<p>I'm using an AMD Radeon HD 6700 with updated drivers, and there are no warnings compiling the shaders. (I've never seen a single shader compiler warning  though, so not sure if that's another problem)</p>

CC BY-SA 3.0
2015-11-17T10:21:21.460Z
dynamic indexing can cause issues in some drivers
ratchet freak
CC BY-SA 3.0
2015-11-23T15:18:30.047Z
Well, I guess that's the case, can't find a better explanation.
MadEqua
<opengl><shader><glsl>
Indexing gl_TessLevelOuter[] with gl_InvocationID on a tessellation control shader
MadEqua
# Indexing gl_TessLevelOuter[] with gl_InvocationID on a tessellation control shader

(Asked by: username_0 on 2015-11-16)

I'm using hardware tessellation to render a terrain mesh with variable LOD.

I have this snippet on my tessellation control shader:

```
int edge1 = gl_InvocationID;
int edge2 = (gl_InvocationID + 1) % 3;
tcs_out[gl_InvocationID].color = vec4(computeLODColor(gl_TessLevelOuter[edge1], gl_TessLevelOuter[edge2]), 1.0);

```

This should compute a color for the control point based on the tessellation level of the two edges that intersect on the point (using quad patches).

It's all working fine except for this: `gl_TessLevelOuter[edge1]` and `gl_TessLevelOuter[edge1]`.

This indexing always return 0 (I've tried using it directly as a color). To test the problem, I tried this: `gl_TessLevelOuter[gl_InvocationID]`. It also has the same behavior.

If I do something like this:

```
if(gl_InvocationID == 0) {
    tcs_out[gl_InvocationID].color = vec4(computeLODColor(gl_TessLevelOuter[0], gl_TessLevelOuter[1]), 1.0);
}
else if(gl_InvocationID == 1) {
    tcs_out[gl_InvocationID].color = vec4(computeLODColor(gl_TessLevelOuter[1], gl_TessLevelOuter[2]), 1.0);
}
else if(gl_InvocationID == 2) {
    tcs_out[gl_InvocationID].color = vec4(computeLODColor(gl_TessLevelOuter[2], gl_TessLevelOuter[3]), 1.0);
}
else if(gl_InvocationID == 3) {
    tcs_out[gl_InvocationID].color = vec4(computeLODColor(gl_TessLevelOuter[3], gl_TessLevelOuter[0]), 1.0);
}

```

then it all works as expected.

I've checked the OpenGL specification and couldn't find anything about undefined behavior when indexing `gl_TessLevelOuter` by a non-constant value.

So the question is, am I misunderstanding something, doing something against the specification or is it a GL implementation bug?

I'm using an AMD Radeon HD 6700 with updated drivers, and there are no warnings compiling the shaders. (I've never seen a single shader compiler warning though, so not sure if that's another problem)

**Tags:** opengl, shader, glsl

> **Comments:**
> **username_1**: dynamic indexing can cause issues in some drivers  
> **username_0**: Well, I guess that's the case, can't find a better explanation.  

---

## 0 answers

