Search code examples
c++opengl-escompute-shader

Why doesn't the compute shader do the calculation?


I'm experimenting with compute shaders. What I want to do is sending the data of arr1 to compute shader variable shader_arr1[], make all of its elements 1 and read that result back to the CPU side in arr2[] variable. However, running the following program I get the same initial value of arr1[]{1,2,3,4,5,6,7} in arr2[], nothing was changed. Could you tell me what part I'm doing wrong?

        GLuint program = glCreateProgram();
        GLuint computeShader = glCreateShader(GL_COMPUTE_SHADER);

        const GLchar* const shaderSrc = {
            "#version 310 es\n"
            "\n"
            "layout (local_size_x = 1) in;\n"
            "layout(std430, binding = 0) writeonly buffer SSBO {\n"
            "   int shader_arr1[];\n"
            "};\n"
            "void main(void)\n"
            "{\n"
            "   shader_arr1[gl_GlobalInvocationID.x] = 1;\n"
            "}\n"
        };

        glShaderSource(computeShader, 1, &shaderSrc, NULL);
        glCompileShader(computeShader);
        int result;
        glGetShaderiv(computeShader, GL_COMPILE_STATUS, &result);

        if(result == GL_FALSE){
            int length;
            glGetShaderiv(computeShader, GL_INFO_LOG_LENGTH, &length);
            char* message = static_cast<char*>(malloc(length));
            glGetShaderInfoLog(computeShader, length, &length, message);
            __android_log_print(ANDROID_LOG_INFO, "MyLog", "Shader Compile Error:  %s", message);
            free(message);
        }


        GL_CALL(glAttachShader(program, computeShader));
        GL_CALL(glLinkProgram(program));
        GL_CALL(glValidateProgram(program));
        GL_CALL(glUseProgram(program));

        GLuint buff1Id, buff2Id;
        glGenBuffers(1, &buff1Id);
        glGenBuffers(1, &buff2Id);
        GLint arr1[7] = {1, 2, 3, 4, 5, 6, 7};
        GLint arr2[7];
        glBindBuffer(GL_SHADER_STORAGE_BUFFER, buff1Id);
        //glBindBuffer(GL_SHADER_STORAGE_BUFFER, buff2Id);
        glBindBufferBase(GL_SHADER_STORAGE_BUFFER,0, buff1Id);
        glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(arr1), arr1, GL_DYNAMIC_DRAW);
        //glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(arr2), arr2, GL_DYNAMIC_DRAW);

        GL_CALL(glDispatchCompute(1, 1, 1));
        GL_CALL(glMemoryBarrier(GL_BUFFER_UPDATE_BARRIER_BIT));

//        GL_CALL(glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, buff2Id));
//        GL_CALL(glBindBuffer(GL_SHADER_STORAGE_BUFFER, buff2Id));

        GLint* data = static_cast<GLint*>(glMapBufferRange(GL_SHADER_STORAGE_BUFFER, 0, 7,GL_MAP_READ_BIT));
        for(int i = 0; i < 7; ++i) {
            arr2[i] = data[i];
            __android_log_print(ANDROID_LOG_INFO, "MyLog", "Num %d", arr2[i]);
        }
        glUnmapBuffer(GL_SHADER_STORAGE_BUFFER);

Solution

  • I fixed the code. This is the working version of the program

            GLuint program = glCreateProgram();
            GLuint computeShader = glCreateShader(GL_COMPUTE_SHADER);
    
            const GLchar* const shaderSrc = {
                "#version 310 es\n"
                "\n"
                "layout (local_size_x = 1) in;\n"
                "layout(std430, binding = 0) writeonly buffer SSBO1 {\n"
                "   int shader_arr1[];\n"
                "};\n"
                "void main(void)\n"
                "{\n"
                "   shader_arr1[gl_GlobalInvocationID.x] = 1;\n"
                "}\n"
            };
    
            glShaderSource(computeShader, 1, &shaderSrc, NULL);
            glCompileShader(computeShader);
            int result;
            glGetShaderiv(computeShader, GL_COMPILE_STATUS, &result);
    
            if(result == GL_FALSE){
                int length;
                glGetShaderiv(computeShader, GL_INFO_LOG_LENGTH, &length);
                char* message = static_cast<char*>(malloc(length));
                glGetShaderInfoLog(computeShader, length, &length, message);
                __android_log_print(ANDROID_LOG_INFO, "MyLog", "Shader Compile Error:  %s", message);
                free(message);
            }
    
    
            GL_CALL(glAttachShader(program, computeShader));
            GL_CALL(glLinkProgram(program));
            GL_CALL(glValidateProgram(program));
            GL_CALL(glUseProgram(program));
    
            GLuint buff1Id, buff2Id;
            GL_CALL(glGenBuffers(1, &buff1Id));
            GL_CALL(glGenBuffers(1, &buff2Id));
            GLint arr1[7] = {1, 2, 3, 4, 5, 6, 7};
            GLint arr2[7];
            GL_CALL(glBindBuffer(GL_SHADER_STORAGE_BUFFER, buff1Id));
            GL_CALL(glBindBufferBase(GL_SHADER_STORAGE_BUFFER,0, buff1Id));
            GL_CALL(glBufferData(GL_SHADER_STORAGE_BUFFER, sizeof(arr1), arr1, GL_DYNAMIC_DRAW));
    
            GL_CALL(glDispatchCompute(7, 1, 1));
            GL_CALL(glMemoryBarrier(GL_BUFFER_UPDATE_BARRIER_BIT));
    
            GL_CALL(glBindBuffer(GL_SHADER_STORAGE_BUFFER, 0));
            GL_CALL(glBindBuffer(GL_SHADER_STORAGE_BUFFER, buff1Id));
            GL_CALL(glBindBufferBase(GL_SHADER_STORAGE_BUFFER, 0, buff1Id));
    
            GLint* data = static_cast<GLint*>(glMapBufferRange(GL_SHADER_STORAGE_BUFFER, 0, sizeof(GLint) * 7,GL_MAP_READ_BIT));
            for(int i = 0; i < 7; ++i) {
                arr2[i] = data[i];
                __android_log_print(ANDROID_LOG_INFO, "MyLog", "Num %d", arr2[i]);
            }
            glUnmapBuffer(GL_SHADER_STORAGE_BUFFER