title: OpenGL fragment shader doesn't write to SSBO
body: <p>I have a small OpenGL engine written and I want to use SSBOs to allow shaders to write debugging logs that I can write out. My log class looks like this:</p>
  
  <pre><code>#define SHADER_LOG_LINE_LENGTH 128
  #define SHADER_LOG_MAX_LINES 512
  #define SHADER_LOG_DATA_OFFSET sizeof(int32_t) * 3
  #define SHADER_LOG_DATA_SIZE SHADER_LOG_LINE_LENGTH * SHADER_LOG_MAX_LINES * sizeof(int32_t)
  #define SHADER_LOG_TOTAL_SIZE SHADER_LOG_DATA_OFFSET + SHADER_LOG_DATA_SIZE
  
  class ShaderLog
    {
      protected:
        GLuint ssbo;
        GLuint binding_point;
  
        int32_t number_of_lines;
        int32_t max_lines;
        int32_t line_length;
        int32_t data[SHADER_LOG_DATA_SIZE];
  
      public:
        ShaderLog()
          {
            glGenBuffers(1,&amp;(this-&gt;ssbo));
  
            this-&gt;number_of_lines = 0;
            this-&gt;max_lines = SHADER_LOG_MAX_LINES;
            this-&gt;line_length = SHADER_LOG_LINE_LENGTH;
            this-&gt;binding_point = 0;
  
            glBindBuffer(GL_SHADER_STORAGE_BUFFER,this-&gt;ssbo);
            glBufferData(GL_SHADER_STORAGE_BUFFER,SHADER_LOG_TOTAL_SIZE,&amp;(this-&gt;number_of_lines),GL_DYNAMIC_COPY);
            glBindBufferBase(GL_SHADER_STORAGE_BUFFER,this-&gt;binding_point,this-&gt;ssbo);
            glBindBuffer(GL_SHADER_STORAGE_BUFFER,0);
          };
  
        void connect_to_shader(Shader *shader, string shader_variable_name)
          {
            GLuint block_index = 0;
            block_index = glGetProgramResourceIndex(shader-&gt;get_shader_program_number(),GL_SHADER_STORAGE_BLOCK,shader_variable_name.c_str());
  
            if (block_index == GL_INVALID_INDEX)
              ErrorWriter::write_error("Shader log could not be connected to the shader.");
  
            glBindBufferBase(GL_SHADER_STORAGE_BUFFER,block_index,this-&gt;binding_point);
            glShaderStorageBlockBinding(shader-&gt;get_shader_program_number(),block_index,this-&gt;binding_point);
          }
  
        virtual void load_from_gpu()
          {
            glBindBuffer(GL_SHADER_STORAGE_BUFFER,this-&gt;ssbo);
            GLvoid* mapped_ssbo = glMapBuffer(GL_SHADER_STORAGE_BUFFER,GL_READ_ONLY);
  
            if (mapped_ssbo == NULL)
              ErrorWriter::write_error("Could not map shader log into client's memory space for reading.");
            else
              memcpy(&amp;(this-&gt;number_of_lines),mapped_ssbo,SHADER_LOG_DATA_SIZE);
  
            glUnmapBuffer(GL_SHADER_STORAGE_BUFFER);
          }
  
        int get_number_of_lines()
          {
            return this-&gt;number_of_lines;
          }
  
       ...
    };
  </code></pre>
  
  <p>In my fragment shader I do:</p>
  
  <pre><code>#version 430
  
  layout (std430, binding=0) buffer shader_log_data
    {
      int number_of_lines;
      int max_lines;
      int line_length;
      int data[];
    } shader_log;
  
  ...
  
  void main()
    {
      shader_log.number_of_lines = 20;  // just to test
      shader_log.data[0] = 10000;
  
      ...
    }
  </code></pre>
  
  <p>And the main program looks like this:</p>
  
  <pre><code>void render()
    {        
      ... // rendering
  
      shader_log-&gt;load_from_gpu();
      cout &lt;&lt; "lines: " &lt;&lt; shader_log-&gt;get_number_of_lines() &lt;&lt; endl;
  
      glutSwapBuffers();
    }
  
  ...
  
  int main(int argc, char** argv)
    { 
      ...
      shader_log = new ShaderLog();
      ...
      shader_log-&gt;connect_to_shader(shader,"shader_log_data");
      shader_log-&gt;update_gpu();
      ...
      // rendering loop
      ...
    }
  </code></pre>
  
  <p>Now the number of lines written out remains 0, even though it should be set to 20 by the shader. I tried loading the data from GPU after glBufferData(...) and they are there, the problem seems to be in the connection between the buffer and the shader. I also tried reading the data in the shader and outputting them to screen and they're always 0, which supports my hypothesis. Basically I seem to be able to write/read to/from the SSBO from the CPU but not from the shader. Could anyone help me with finding the issue?</p>