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

时间:2015-02-12 02:38:22

标签: android compute-shader opengl-es-3.1

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

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

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

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);
    }

作为参考,着色器位于帖子的末尾。

注意:

  1. 我无法在Android文档中找到GL_VERTEX_ATTRIB_ARRAY_BARRIER_BIT。不应该在那里吗? documentation mentions it
  2. 。{{3}}。
  3. 绘制调用会使应用程序崩溃。直到glDrawArrays()之前一切都很好。
  4. 对于寻找什么的任何想法都表示赞赏。

    顶点着色器:

    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;" +
                "}";
    

    Fragment Shader:

        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;" +
                "}";
    

1 个答案:

答案 0 :(得分:2)

是的,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()次调用。如果它们确实不在您的代码中,那肯定会成为一个问题。