Use of undeclared identifier 'gl_InstanceID

2019-05-31 11:22发布

问题:

Hi everyone, i have been trying Instanced drawing in OpenGLES2.0, in IOS platform. My rendering code

 glEnableVertexAttribArray(...);
 glVertexAttribPointer(...)
 glDrawElementsInstancedEXT(GL_TRIANGLES,IndicesCount, GL_UNSIGNED_SHORT, 0, 5);

And my Vertex Shader

attribute vec4 VertPosition;
uniform mat4 mvpMatrix[600];

void main()
{
    gl_Position = (mvpMatrix[gl_InstanceID]) * VertPosition;
}

I'm getting ERROR: Use of undeclared identifier 'gl_InstanceID'

my glsl version is 1.0, if version is the issue then how can i upgrade ? Any other way to use "gl_InstanceID" in GLSL ?

回答1:

gl_InstanceID is only available starting from GLSL ES 3.0 as stated here.

So this is, as you already suspected, a version issue. As far as I know, the only available GLSL ES version in OpenGL ES 2.0 is GLSL ES 1.0, and if you want to use a higher GLSL ES version you have to upgrade to OpenGL ES 3.0. (more details here)

Edit: I was thinking about what you want to achieve with the usage of gl_InstanceID. This variable does only make sense when using one of the instanced draw commands (glDrawArraysInstanced etc.), which are also not available in ES 2.0.

Apparently, there is a possibility to use instanced rendering in OpenGL ES 2.0 by using the GL_EXT_draw_instanced extension. This extension provides one with two additional draw commands for instanced drawing (glDrawElementsInstancedEXT and glDrawArraysInstancedEXT). When using the extension, one has to enable it in the shader

#extension GL_EXT_draw_instanced : enable

and use gl_InstanceIDEXT instead of gl_InstanceID.