无法在Android中编译opengl片段着色器-错误:0:7:“ gl_GlobalInvocationID”:未声明的标识符

时间:2019-03-31 05:47:47

标签: java android opengl-es glsl shader

无法在android中编译以下opengl片段着色器代码。// Calculated field IF [city] = 'Delhi' AND [District] = 'D1' AND [District] = 'D2' THEN "Category 1" ELSE IF [city] = 'Moscow' AND [District] = 'M1' AND [District] = 'M3' THEN "Category 2" END 显示opengl版本为3.2,并且该设备似乎也支持该版本。

错误:-

GLES31.glGetString(GL_VERSION)

着色器代码:-

Error compiling shader: ERROR: 0:7: 'gl_GlobalInvocationID' : undeclared identifier 
    ERROR: 0:7: 'xy' :  field selection requires structure, vector, or matrix on left hand side 
    ERROR: 2 compilation errors.  No code generated.
03-31 10:39:17.822 23849-23887/com.research.gltest E/AndroidRuntime: FATAL EXCEPTION: GLThread 1686
    Process: com.research.gltest, PID: 23849
    java.lang.RuntimeException: Error creating shader.
        at com.research.gltest.ShaderHelper.compileShader(ShaderHelper.java:45)
        at com.research.gltest.GLLayer.onDrawFrame(GLLayer.java:325)

该错误发生在以下行:-

    #version 310 es
    precision mediump float;
    layout(binding = 0) uniform sampler2D u_Texture1;
    layout(std430) buffer;
    layout(binding = 1) buffer Output { float elements[]; } output_data;
    void main() {
    ivec2 gid = ivec2(gl_GlobalInvocationID.xy);
    if (gid.x >= 257 || gid.y >= 257) return;
    vec3 pixel = texelFetch(u_Texture1, gid, 0).xyz;
    int linear_index = 3 * (gid.y * 224 + gid.x);
    output_data.elements[linear_index + 0] = pixel.x; output_data.elements[linear_index + 1] = pixel.y;
    output_data.elements[linear_index + 2] = pixel.z;
    }

JavaCodehaderHelper.java):-

final int fragmentShaderHandle = ShaderHelper.compileShader(
            GLES31.GL_FRAGMENT_SHADER, fragmentShader)

NB:使用android中的glsl插件,代码存储在原始文件夹中。无论如何,我可以使用应用程序@(https://github.com/yulu/GLtext)编译并运行其他示例glsl shader代码(<3.0)

编辑:我将旧的着色器代码更改为opengl v3;但是现在它引发新的错误错误的编译程序:错误:计算着色器已与其他着色器链接

1 个答案:

答案 0 :(得分:2)

gl_GlobalInvocationIDCompute Shader Special Variables,只能在计算着色器中使用:

来自OpenGL ES Shading Language 3.20 Specification

  

内置变量gl_GlobalInvocationID是包含当前工作项的全局索引的计算着色器输入变量。该值从当前DispatchCompute调用启动的所有工作组中的所有其他调用中唯一标识该调用。计算公式为:

gl_GlobalInvocationID =
     gl_WorkGroupID * gl_WorkGroupSize + gl_LocalInvocationID;

在顶点着色器中,有内置变量gl_VertexIDgl_InstanceID,可以将其传递到片段着色器阶段。参见Vertex Shader Special Variables