在 Android 5 中使用计算着色器(Open GL ES 3.1)

Using compute shader in Android 5 (Open GL ES 3.1)

我正在学习 OpenGL ES,尤其是 OpenGL ES 3.1 中的计算着色器,特别是 Android 5.0.1。

我定义了 3 个着色器(计算、顶点和片段)并附加到两个不同的程序,一个用于计算着色器,一个用于顶点和片段着色器。

当我只使用顶点和片段着色器时没有问题,但现在我添加了计算着色器时出现以下错误:

02-11 20:02:10.375  13243-13264/com.example.daan.daggl2 I/VERSION﹕ OpenGL ES 3.1 NVIDIA 349.00
02-11 20:02:10.472  13243-13264/com.example.daan.daggl2 A/libc﹕ Fatal signal 11 (SIGSEGV), code 1, fault addr 0x0 in tid 13264 (GLThread 9847)

我想弄清楚我遗漏了什么,但我是这个主题的新手,我不确定从哪里开始寻找问题。

对象的构造函数是:

public Triangle() {
        buffers = new int[1];
        GLES31.glGenBuffers(1, buffers, 0);
        gVBO = buffers[0];

        // set up the vertex and fragment shaders
        int vertexShader = MyGLRenderer.loadShader(
            GLES31.GL_VERTEX_SHADER, vertexShaderCode);
        int fragmentShader = MyGLRenderer.loadShader(
            GLES31.GL_FRAGMENT_SHADER, fragmentShaderCode);

        program = GLES31.glCreateProgram();    
        GLES31.glAttachShader(program, vertexShader);
        GLES31.glAttachShader(program, fragmentShader);
        GLES31.glLinkProgram(program);
        checkGLError("LinkProgram/program");

        // set up the compute shader
        int computeShader = MyGLRenderer.loadShader(
            GLES31.GL_COMPUTE_SHADER, computeShaderCode);
        computeProgram = GLES31.glCreateProgram();
        GLES31.glAttachShader(computeProgram, computeShader);
        GLES31.glLinkProgram(computeProgram);
        checkGLError("LinkProgram/computeProgram");
    }

绘制函数:

public void draw(float[] mvpMatrix, float[] color) {

        GLES31.glUseProgram(computeProgram);
        checkGLError("UseProgram/computeProgram");
        int radiusId = GLES31.glGetUniformLocation(
            computeProgram, "radius");
        indexBufferBinding = 0;
        GLES31.glUniform1f(radiusId, (float) radius);
        GLES31.glBindBufferBase(
            GLES31.GL_SHADER_STORAGE_BUFFER, indexBufferBinding, gVBO);
        checkGLError("glBindBuffer/gVBO");
        GLES31.glDispatchCompute(2, 2, 1);
        GLES31.glBindBufferBase(
            GLES31.GL_SHADER_STORAGE_BUFFER, indexBufferBinding, 0);
        // See note 1 below
        //GLES31.glMemoryBarrier(
        //    GLES31.GL_VERTEX_ATTRIB_ARRAY_BARRIER_BIT);
        GLES31.glMemoryBarrier(
            GLES31.GL_SHADER_STORAGE_BARRIER_BIT);
        checkGLError("glMemoryBarrier/1");
        GLES31.glBindBuffer(GLES31.GL_ARRAY_BUFFER, gVBO);
        checkGLError("glBindBuffer/gVBO");

        GLES31.glUseProgram(program);

        int posId = GLES31.glGetAttribLocation(
            program, "a_v4Position");
        int fillId = GLES31.glGetAttribLocation(
            program, "a_v4FillColor");
        int mvpMatrixId = GLES31.glGetUniformLocation(
            program, "mvp_matrix");

        GLES31.glEnableVertexAttribArray(posId);
        GLES31.glEnableVertexAttribArray(fillId);
        GLES31.glUniformMatrix4fv(mvpMatrixId, 1, false, mvpMatrix, 0);
        // See note 2 below
        GLES31.glDrawArrays(GLES31.GL_POINTS, 0, 3);
    }

供参考,着色器位于 post 的末尾。

备注:

  1. 我在 Android 文档中找不到 GL_VERTEX_ATTRIB_ARRAY_BARRIER_BIT。它不应该在那里吗? documentation mentions it.
  2. 绘图调用使应用程序崩溃。直到 glDrawArrays() 之前一切似乎都很好。

如有任何关于寻找内容的想法,我们将不胜感激。

顶点着色器:

private final String vertexShaderCode =
            "uniform mat4 mvp_matrix;" +
            "attribute vec4 a_v4Position;" +
            "attribute vec4 a_v4FillColor;" +
            "varying vec4 v_v4FillColor;" +
            "void main(void) {" +
            " v_v4FillColor = a_v4FillColor;" +
            " gl_Position = mvp_matrix * a_v4Position;" +
            "}";

片段着色器:

    private final String fragmentShaderCode =
            "precision mediump float;" +
            "varying vec4 v_v4FillColor;" +
            "void main(void) {" +
            " gl_FragColor = v_v4FillColor;" +
            "}";

计算着色器:

    private final String computeShaderCode =
            "#version 310 es" +
            "\n" +
            "uniform float radius;" +
            "struct Vector3f { float x; float y; float z; float w; };" +
            "struct AttribData { Vector3f v; Vector3f c; };" +
            "layout(std140, binding = 0) buffer destBuffer { AttribData data[]; } outBuffer;" +
            "layout (local_size_x = 8, local_size_y = 8, local_size_z = 1) in;" +
            "void main() {" +
            " ivec2 storePos = ivec2(gl_GlobalInvocationID.xy);" +
            " uint gWidth = gl_WorkGroupSize.x * gl_NumWorkGroups.x;" +
            " uint gHeigth = gl_WorkGroupSize.y * gl_NumWorkGroups.y;" +
            " uint gSize = uint(gWidth) * uint(gHeigth);" +
            " uint offset = uint(storePos.y)*gWidth + uint(storePos.x);" +
            " float alpha = 2.0 * 3.1159265359 * (float(offset) / float(gSize));" +
            " outBuffer.data[offset].v.x = float(sin(alpha)) * float(radius);" +
            " outBuffer.data[offset].v.y = float(cos(alpha)) * float(radius);" +
            " outBuffer.data[offset].v.z = 0.0;" +
            " outBuffer.data[offset].v.w = 1.0;" +
            " outBuffer.data[offset].c.x = float(storePos.x) / float(gWidth);" +
            " outBuffer.data[offset].c.y = 0.0;" +
            " outBuffer.data[offset].c.z = 1.0;" +
            " outBuffer.data[offset].c.w = 1.0;" +
            "}";

是的,Android Java 绑定中缺少 GL_VERTEX_ATTRIB_ARRAY_BARRIER_BIT。 Android 中的 Java OpenGL 绑定是不完整的传统。 3.0 绑定中仍然缺少一些东西。

在这种情况下,由于它只是一个枚举值,您可以通过定义值本身轻松解决它。 C/C++ 定义为:

#define GL_VERTEX_ATTRIB_ARRAY_BARRIER_BIT 0x00000001

因此在 Java 中,您可以在代码中添加这样的定义:

static final int GL_VERTEX_ATTRIB_ARRAY_BARRIER_BIT = 0x00000001;

至于崩溃,我不太确定。我注意到发布的代码中没有 glVertexAttribPointer() 调用。如果它们确实不在您的代码中,那肯定是个问题。