Advertisement
Guest User

Untitled

a guest
May 20th, 2016
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.96 KB | None | 0 0
  1. #ifndef GLUT2_IMAGE_H
  2. #define GLUT2_IMAGE_H
  3.  
  4. #include <GL/glew.h>
  5. #include <GL/gl.h>
  6. #include <helper.h>
  7.  
  8.  
  9. GLuint glob_program;
  10. GLuint glob_texture;
  11. bool once = false;
  12.  
  13. void draw() {
  14.     if (once) {
  15.         return;
  16.     }
  17.     once = true;
  18.     gl(glBindImageTexture(0, glob_texture, 0, GL_FALSE, 0, GL_WRITE_ONLY,
  19.         GL_RGBA16UI));
  20.  
  21.     std::cout << "glob_program is progam "
  22.         << (glIsProgram(glob_program) == GL_TRUE)
  23.         << std::endl;
  24.     GLint uniformTexPosition = gl(glGetUniformLocation(glob_program, "u_image"));
  25.     std::cout << "u_image position " << uniformTexPosition << std::endl;
  26.     gl(glUseProgram(glob_program));
  27.     gl(glUniform1i(uniformTexPosition, 0));
  28.     gl(glDispatchCompute(32, 32, 1));
  29. }
  30.  
  31. void initTextures() {
  32.     gl(glGenTextures(1, &glob_texture));
  33.     gl(glBindTexture(GL_TEXTURE_2D, glob_texture));
  34.     gl(glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA16UI, 32, 32));
  35. }
  36.  
  37. void initShader() {
  38.     const GLchar *compShaderT =
  39.         "#version 440 core\n"
  40.         "layout (local_size_x = 32, local_size_y = 32) in;\n"
  41.         "layout (location = 1, rgba16ui) uniform uimage2D u_image;\n"
  42.         "uniform atomic_uint u_counter;\n"
  43.         "void main() {\n"
  44.             "uint value = atomicCounterIncrement(u_counter);\n"
  45.             "ivec2 size = imageSize(u_image);\n"
  46.             "if (gl_GlobalInvocationID.x < size.x && gl_GlobalInvocationID.y < size.y)"
  47.             "imageStore(u_image, ivec2(gl_GlobalInvocationID.xy), "
  48.                 "uvec4(vec3(value), 0xffff));"
  49.         "}\n";
  50.  
  51.     GLuint computeShader = gl(glCreateShader(GL_COMPUTE_SHADER));
  52.     gl(glShaderSource(computeShader, 1, &compShaderT, nullptr));
  53.     gl(glCompileShader(computeShader));
  54.  
  55.     glShaderLog(computeShader);
  56.     glob_program = gl(glCreateProgram());
  57.     gl(glAttachShader(glob_program, computeShader));
  58.     gl(glLinkProgram(glob_program));
  59.  
  60.     gl(glDeleteShader(computeShader));
  61.  
  62.     glProgramLog(glob_program);
  63. }
  64.  
  65. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement