fork download
  1. Shader:
  2. #version 430
  3. layout(local_size_x=128) in;
  4.  
  5. layout(r32f, location = 0) uniform imageBuffer data;
  6.  
  7. shared float local[256];
  8. void main() {
  9. int N = imageSize(data);
  10. int index = int(gl_GlobalInvocationID);
  11. int localindex = int(gl_LocalInvocationIndex);
  12.  
  13. local[localindex] = localindex;
  14. local[localindex+128] = localindex+128;
  15.  
  16. groupMemoryBarrier();
  17. imageStore(data, index, vec4(local[localindex]));
  18. imageStore(data, index+128, vec4(local[localindex+128]));
  19. }
  20.  
  21. code:
  22.  
  23. std::vector<float> data(256);
  24. std::fill(data.begin(), data.end(), -1.0f);
  25.  
  26. GLuint buffer;
  27.  
  28. glGenBuffers(1, &buffer);
  29. glBindBuffer(GL_TEXTURE_BUFFER, buffer);
  30. glBufferData(GL_TEXTURE_BUFFER, sizeof(float)*data.size(), 0, GL_STATIC_DRAW);
  31.  
  32. GLuint buffer_texture;
  33.  
  34. glGenTextures(1, &buffer_texture);
  35. glBindTexture(GL_TEXTURE_BUFFER, buffer_texture);
  36. glTexBuffer(GL_TEXTURE_BUFFER, GL_R32F, buffer);
  37.  
  38. glBindImageTexture(0, buffer_texture, 0, GL_FALSE, 0, GL_READ_WRITE, GL_R32F);
  39.  
  40. glUseProgram(test_program);
  41. glUniform1i(0, 0);
  42.  
  43. glDispatchCompute(1, 1, 1);
  44.  
  45. glGetBufferSubData(GL_TEXTURE_BUFFER, 0, sizeof(float)*data.size(), &data[0]);
  46.  
  47. for(size_t i = 0;i<data.size();i+=1)
  48. {
  49. if(i%16==0) std::cout << std::endl;
  50. std::cout << std::setw(4) << data[i] << ' ';
  51. }
  52. std::cout << std::endl;
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty