Advertisement
Guest User

Untitled

a guest
May 21st, 2016
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.17 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. #include <fstream>
  9. #include <iomanip>
  10.  
  11.  
  12. GLuint glob_program;
  13. GLuint glob_texture;
  14. bool once = false;
  15.  
  16. void draw() {
  17.     if (once) {
  18.         return;
  19.     }
  20.     once = true;
  21.     gl(glBindImageTexture(0, glob_texture, 0, GL_FALSE, 0, GL_WRITE_ONLY,
  22.         GL_RGBA16UI));
  23.  
  24.     std::cout << "glob_program is progam "
  25.         << (glIsProgram(glob_program) == GL_TRUE)
  26.         << std::endl;
  27.     GLint uniformTexPosition = gl(glGetUniformLocation(glob_program, "u_image"));
  28.     std::cout << "u_image position " << uniformTexPosition << std::endl;
  29.     gl(glUseProgram(glob_program));
  30.     gl(glUniform1i(uniformTexPosition, 0));
  31.     gl(glDispatchCompute(32, 32, 1));
  32.  
  33.     // bind and get texture
  34.     gl(glBindTexture(GL_TEXTURE_2D, glob_texture));
  35.  
  36.     GLint texFormat;
  37.     GLint texWidth;
  38.     GLint texHeight;
  39.     gl(glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_INTERNAL_FORMAT,
  40.         &texFormat));
  41.     gl(glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &texWidth));
  42.     gl(glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT,
  43.         &texHeight));
  44.  
  45.     size_t size;
  46.     switch (texFormat) {
  47.         case GL_RGBA16UI:
  48.             size = 8;
  49.             std::cout << "GL_RGBA16UI" << std::endl;
  50.             break;
  51.         default:
  52.             size = 16;
  53.             std::cout << "GL ENUM 0x"
  54.                 << std::hex << texFormat
  55.                 << std::scientific << std::endl;
  56.             break;
  57.     }
  58.  
  59.     size = texWidth * texHeight * size;
  60.     std::cout << "Texture size " << size << "B" << std::endl;
  61.     GLbyte *data = new GLbyte[size];
  62.  
  63.     gl(glGetTexImage(GL_TEXTURE_2D, 0, texFormat, GL_UNSIGNED_BYTE, data));
  64.  
  65.     std::ofstream stream("out.tex", std::ios::trunc | std::ios::binary);
  66.     stream.write(reinterpret_cast<char *>(data), size);
  67.     stream.close();
  68.     delete[] data;
  69. }
  70.  
  71. void initTextures() {
  72.     gl(glGenTextures(1, &glob_texture));
  73.     gl(glBindTexture(GL_TEXTURE_2D, glob_texture));
  74.     gl(glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGBA16UI, 32, 32));
  75. }
  76.  
  77. void initShader() {
  78.     const GLchar *compShaderT =
  79.         "#version 440 core\n"
  80.         "layout (local_size_x = 32, local_size_y = 32) in;\n"
  81.         "layout (location = 1, rgba16ui) uniform uimage2D u_image;\n"
  82.         "uniform atomic_uint u_counter;\n"
  83.         "void main() {\n"
  84.             "uint value = atomicCounterIncrement(u_counter);\n"
  85.             "ivec2 size = imageSize(u_image);\n"
  86.             "if (gl_GlobalInvocationID.x < size.x && gl_GlobalInvocationID.y < size.y)"
  87.             "imageStore(u_image, ivec2(gl_GlobalInvocationID.xy), "
  88.                 "uvec4(vec3(value), 0xffff));"
  89.         "}\n";
  90.  
  91.     GLuint computeShader = gl(glCreateShader(GL_COMPUTE_SHADER));
  92.     gl(glShaderSource(computeShader, 1, &compShaderT, nullptr));
  93.     gl(glCompileShader(computeShader));
  94.  
  95.     glShaderLog(computeShader);
  96.     glob_program = gl(glCreateProgram());
  97.     gl(glAttachShader(glob_program, computeShader));
  98.     gl(glLinkProgram(glob_program));
  99.  
  100.     gl(glDeleteShader(computeShader));
  101.  
  102.     glProgramLog(glob_program);
  103. }
  104.  
  105. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement