title: Compute Shader write to texture
body: <p>I have implemented CPU code that copies a projected texture to a larger texture on a 3d object, 'decal baking' if you will, but now I need to implement it on the GPU. To do this I hope to use compute shader as its quite difficult to add an FBO in my current setup. </p>
  
  <p><a href="https://imgur.com/yxnPGZF" rel="nofollow noreferrer">Example image from my current implementation</a></p>
  
  <p>This question is more about how to use Compute shaders but for anyone interested, the idea is based on an answer I got from user jozxyqk, seen here: <a href="https://stackoverflow.com/a/27124029/2579996">https://stackoverflow.com/a/27124029/2579996</a></p>
  
  <p>The texture that is written-to is in my code called <code>_texture</code>, whilst the one projected is <code>_textureProj</code></p>
  
  <p><strong>Simple compute shader</strong></p>
  
  <pre><code>const char *csSrc[] = {
      "#version 440\n",
      "layout (binding = 0, rgba32f) uniform image2D destTex;\
       layout (local_size_x = 16, local_size_y = 16) in;\
       void main() {\
             ivec2 storePos = ivec2(gl_GlobalInvocationID.xy);\
             imageStore(destTex, storePos, vec4(0.0,0.0,1.0,1.0));\
      }"
  };
  </code></pre>
  
  <p>As you see I currently only want to have the texture updated to some arbitrary (blue) color.</p>
  
  <p><strong>Update function</strong></p>
  
  <pre><code>void updateTex(){ 
      glUseProgram(_computeShader);
      const GLint location = glGetUniformLocation(_computeShader, "destTex"); 
      if (location == -1){
          printf("Could not locate uniform location for texture in CS");
      }
      // bind texture
      glUniform1i(location, 0); 
      glBindImageTexture(0, *_texture, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_RGBA32F);
      // ^second param returns the GLint id - that im sure of. 
  
      glDispatchCompute(_texture-&gt;width() / 16, _texture-&gt;height() / 16, 1); 
      glMemoryBarrier(GL_SHADER_STORAGE_BARRIER_BIT);
  
      glUseProgram(0);
      printOpenGLError(); // reports no errors.
  }
  </code></pre>
  
  <p><strong>Problem</strong>
  If i call the <code>updateTex()</code> outside of my main program object i see <em>zero</em> effect, whereas if I call it within its scope like so:</p>
  
  <pre><code> glUseProgram(_id); // vert, frag shader pipe
    updateTex();
    // Pass uniforms to shader 
    // bind _textureProj &amp; _texture (latter is the one im trying to update) 
   glUseProgram(0);
  </code></pre>
  
  <p>Then upon rendering I see this:</p>
  
  <p><a href="https://imgur.com/iSrQGcq" rel="nofollow noreferrer"><img src="https://i.imgur.com/iSrQGcq.jpg"/></a></p>
  
  <p><strong>QUESTION:</strong>
  I realise that setting the update method within the main program object scope is not the proper way of doing it, however its the only way to get any visual results. It seems to me that what happens is that it pretty much eliminates the fragmentshader and draws to screenspace...</p>
  
  <p>What can I do to get this working properly? (my main focus is to be able to write <em>anything</em> to the texture &amp; update)</p>
  
  <p>Please let me know if more code needs posting. </p>