Advertisement
Guest User

Untitled

a guest
Mar 8th, 2025
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.66 KB | None | 0 0
  1. #define GLEW_STATIC
  2. #include <GL/glew.h>
  3. #include <GLFW/glfw3.h>
  4. #include <SOIL/SOIL.h>
  5. #include <iostream>
  6. #include <vector>
  7. #include <malloc.h>
  8. #include <chrono>
  9.  
  10. unsigned char* _in;
  11. unsigned char* _out;
  12. GLuint texture_in;
  13. GLuint texture_out;
  14.  
  15. void loadTexture(int& width, int& height) {
  16.   width = 1920 << 1;
  17.   height = 1080 << 1;
  18.  
  19.   _in = (unsigned char*)malloc(width * height * 3);
  20.   _out = (unsigned char*)malloc(width * height * 3);
  21. //  for (int y = 0; y < height; ++y) {
  22. //    for (int x = 0; x < width; ++x) {
  23. //      ((int*)image)[y * width + x] = (((x ^ y) & 0xff) << 24 | (x & 0xff << 16) | (y & 0xff << 8) | ((x ^ y) & 0xff)) ^ 0x7f7f7f7f;
  24. //    }
  25. //  }
  26.  
  27.   uint8_t* p = _in;
  28.   for (int y = 0; y < height; y++) {
  29.     for (int x = 0; x < width; x++) {
  30.       if (x < 16) {
  31.         *p++ = 0x80;
  32.         *p++ = 0xff;
  33.         *p++ = 0xff;
  34.       } else if (x >= 64 && x < 192 && y >= 64 && y < 192) {
  35.         if (x < 80 || y < 80) {
  36.           *p++ = 0xb5;
  37.           *p++ = 0xb5;
  38.           *p++ = 0xb5;
  39.         } else if ((x ^ y) & 16) {
  40.           *p++ = 0;
  41.           *p++ = 0xff;
  42.           *p++ = 0;
  43.         } else {
  44.           *p++ = 0xff;
  45.           *p++ = 0;
  46.           *p++ = 0xff;
  47.         }
  48.       } else {
  49.         int m = 1;
  50.         if (y >= 128) m <<= 1;
  51.         if (x >= 128) m <<= 2;
  52.         if ((x ^ y) & m) {
  53.           *p++ = 0xff;
  54.           *p++ = 0xff;
  55.           *p++ = 0xff;
  56.         } else {
  57.           *p++ = 0x00;
  58.           *p++ = 0x00;
  59.           *p++ = 0x00;
  60.         }
  61.       }
  62.     }
  63.   }
  64.  
  65.   glGenTextures(1, &texture_in);
  66.   glGenTextures(1, &texture_out);
  67.  
  68.   glBindTexture(GL_TEXTURE_2D, texture_in);
  69.   glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, _in);
  70.   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  71.   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  72.  
  73.   glBindTexture(GL_TEXTURE_2D, texture_out);
  74.   glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, nullptr);
  75.   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  76.   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  77.  
  78. //  free(image);
  79. //  SOIL_free_image_data(image);
  80.   glBindTexture(GL_TEXTURE_2D, 0);
  81. }
  82.  
  83.  
  84.  
  85.  
  86. GLuint createComputeShader(const char* shaderSource) {
  87.   GLuint shader = glCreateShader(GL_COMPUTE_SHADER);
  88.   glShaderSource(shader, 1, &shaderSource, nullptr);
  89.   glCompileShader(shader);
  90.  
  91.   GLint success;
  92.   glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
  93.   if (!success) {
  94.     char infoLog[512];
  95.     glGetShaderInfoLog(shader, 512, nullptr, infoLog);
  96.     std::cerr << "ERROR::SHADER::COMPUTE::COMPILATION_FAILED\n" << infoLog << std::endl;
  97.   }
  98.  
  99.   GLuint program = glCreateProgram();
  100.   glAttachShader(program, shader);
  101.   glLinkProgram(program);
  102.  
  103.   glGetProgramiv(program, GL_LINK_STATUS, &success);
  104.   if (!success) {
  105.     char infoLog[512];
  106.     glGetProgramInfoLog(program, 512, nullptr, infoLog);
  107.     std::cerr << "ERROR::PROGRAM::LINKING_FAILED\n" << infoLog << std::endl;
  108.   }
  109.  
  110.   glDeleteShader(shader);
  111.   return program;
  112. }
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124. #include <iostream>
  125. #include <fstream>
  126. #include <sstream>
  127.  
  128. void readTextureToCPU(GLuint texture, int width, int height, bool save = false) {
  129.   glBindTexture(GL_TEXTURE_2D, texture);
  130.  
  131. //  std::vector<unsigned char> data(width * height * 3);
  132.   glGetTexImage(GL_TEXTURE_2D, 0, GL_RGB, GL_UNSIGNED_BYTE, _out);
  133.  
  134.   if (save) {
  135.     std::ofstream out("out.pnm", std::ios::binary);
  136.     out << "P6 " << width << " " << height << " 255\n";
  137.     out.write((char*)_out, width * height * 3);
  138.   }
  139.  
  140.   // Do something with the data
  141.  // std::cout << "First pixel: ("
  142.  //   << static_cast<int>(data[0]) << ", "
  143.  //   << static_cast<int>(data[1]) << ", "
  144.  //   << static_cast<int>(data[2]) << ", "
  145.  //   << static_cast<int>(data[3]) << ")" << std::endl;
  146.  
  147.   glBindTexture(GL_TEXTURE_2D, 0);
  148. }
  149.  
  150.  
  151.  
  152.  
  153.  
  154.  
  155. int main() {
  156.     // Initialize GLFW and create a window
  157.   if (!glfwInit()) {
  158.     std::cerr << "Failed to initialize GLFW" << std::endl;
  159.     return -1;
  160.   }
  161.  
  162.   glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
  163.   GLFWwindow* window = glfwCreateWindow(800, 600, "OpenGL Compute Shader Example", nullptr, nullptr);
  164.   if (!window) {
  165.     std::cerr << "Failed to create GLFW window" << std::endl;
  166.     glfwTerminate();
  167.     return -1;
  168.   }
  169.  
  170.   glfwMakeContextCurrent(window);
  171.  
  172.   // Initialize GLEW
  173.   if (glewInit() != GLEW_OK) {
  174.     std::cerr << "Failed to initialize GLEW" << std::endl;
  175.     return -1;
  176.   }
  177.  
  178.   // Load the texture
  179.   int width, height;
  180.   loadTexture(width, height);
  181.   if (!texture_in || !texture_out) {
  182.     throw("Texture problem");
  183.     return -1;
  184.   }
  185.  
  186.   // Create and use the compute shader
  187.   std::ifstream in("test.glsl");
  188.   if (!in.is_open()) throw("File did not open");
  189.   std::stringstream ss;
  190.   ss << in.rdbuf();
  191.   GLuint computeProgram = createComputeShader(ss.str().c_str());
  192.  
  193. //  glBindTexture(GL_TEXTURE_2D, texture_in);
  194.   glUseProgram(computeProgram);
  195.  
  196.  
  197.   glBindImageTexture(0, texture_in, 0, GL_FALSE, 0, GL_READ_ONLY, GL_RGBA8);
  198.   glBindImageTexture(1, texture_out, 0, GL_FALSE, 0, GL_WRITE_ONLY, GL_RGBA8);
  199.  
  200.   // Dispatch the compute shader
  201.   glDispatchCompute((GLuint)ceil(width / 32.0), (GLuint)ceil(height / 32.0), 1);
  202.  
  203. // Wait for the compute shader to finish
  204.   glMemoryBarrier(GL_SHADER_IMAGE_ACCESS_BARRIER_BIT);
  205.  
  206.     // Read the scrambled texture back to the CPU
  207.  
  208.   readTextureToCPU(texture_out, width, height, true);
  209.  
  210.   glUseProgram(0);
  211.  
  212.  
  213.   // Clean up
  214.   glDeleteProgram(computeProgram);
  215.   glDeleteTextures(1, &texture_in);
  216.   glDeleteTextures(1, &texture_out);
  217.   glfwDestroyWindow(window);
  218.   glfwTerminate();
  219.  
  220.   return 0;
  221. }
  222.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement