Advertisement
szkristof32

OpenGL+shaderc UBO example

May 18th, 2025 (edited)
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.78 KB | Source Code | 0 0
  1. #define GLFW_INCLUDE_NONE
  2. #include <GLFW/glfw3.h>
  3. #include <glad/glad.h>
  4. #include <shaderc/shaderc.hpp>
  5.  
  6. #include <string>
  7.  
  8. std::string s_VertexShader = R"(
  9. #version 450 core
  10.  
  11. layout (location = 0) in vec3 in_position;
  12.  
  13. void main()
  14. {
  15.     gl_Position = vec4(in_position, 1.0f);
  16. }
  17. )";
  18. std::string s_FragmentShader = R"(
  19. #version 450 core
  20.  
  21. layout (location = 0) out vec4 out_colour;
  22.  
  23. layout (binding = 0, std140) uniform Colours
  24. {
  25.     vec3 Colour;
  26. };
  27.  
  28. void main()
  29. {
  30.     out_colour = vec4(Colour, 1.0);
  31. }
  32. )";
  33.  
  34. static std::vector<uint32_t> CompileShader(const std::string& source, shaderc_shader_kind stage);
  35.  
  36. int main()
  37. {
  38.     glfwInit();
  39.     glfwDefaultWindowHints();
  40.     GLFWwindow* window = glfwCreateWindow(1280, 720, "OpenGL shaderc ubo example", nullptr, nullptr);
  41.     glfwShowWindow(window);
  42.     glfwMakeContextCurrent(window);
  43.     gladLoadGLLoader((GLADloadproc)glfwGetProcAddress);
  44.  
  45.     float vertices[] = {
  46.         -0.5f, 0.5f, 0.0f,
  47.         -0.5f, -0.5f, 0.0f,
  48.         0.5f, 0.5f, 0.0f,
  49.         0.5f, -0.5f, 0.0f
  50.     };
  51.     uint32_t indices[] = {
  52.         0, 1, 2,
  53.         2, 1, 3
  54.     };
  55.     uint32_t vao, vbo, ibo, ubo;
  56.     glCreateVertexArrays(1, &vao);
  57.     glBindVertexArray(vao);
  58.  
  59.     glCreateBuffers(1, &vbo);
  60.     glBindBuffer(GL_ARRAY_BUFFER, vbo);
  61.     glNamedBufferData(vbo, sizeof(vertices), vertices, GL_STATIC_DRAW);
  62.     glEnableVertexAttribArray(0);
  63.     glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
  64.  
  65.     glCreateBuffers(1, &ibo);
  66.     glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
  67.     glNamedBufferData(ibo, sizeof(indices), indices, GL_STATIC_DRAW);
  68.  
  69.     glCreateBuffers(1, &ubo);
  70.     glNamedBufferData(ubo, 3 * sizeof(float), nullptr, GL_DYNAMIC_DRAW);
  71.  
  72.     uint32_t program, vertexId, fragmentId;
  73.     program = glCreateProgram();
  74.  
  75.     auto vertexBinary = CompileShader(s_VertexShader, shaderc_vertex_shader);
  76.     vertexId = glCreateShader(GL_VERTEX_SHADER);
  77.     glShaderBinary(1, &vertexId, GL_SHADER_BINARY_FORMAT_SPIR_V, vertexBinary.data(), vertexBinary.size() * sizeof(uint32_t));
  78.     glSpecializeShader(vertexId, "main", 0, nullptr, nullptr);
  79.     glAttachShader(program, vertexId);
  80.  
  81.     auto fragmentBinary = CompileShader(s_FragmentShader, shaderc_fragment_shader);
  82.     fragmentId = glCreateShader(GL_FRAGMENT_SHADER);
  83.     glShaderBinary(1, &fragmentId, GL_SHADER_BINARY_FORMAT_SPIR_V, fragmentBinary.data(), fragmentBinary.size() * sizeof(uint32_t));
  84.     glSpecializeShader(fragmentId, "main", 0, nullptr, nullptr);
  85.     glAttachShader(program, fragmentId);
  86.  
  87.     glLinkProgram(program);
  88.     glValidateProgram(program);
  89.  
  90.     glUseProgram(program);
  91.  
  92.     float colour[3] = { 0.2f, 1.0f, 0.3f };
  93.     glNamedBufferSubData(ubo, 0, 3 * sizeof(float), colour);
  94.     glBindBuffer(GL_UNIFORM_BUFFER, ubo);
  95.     glBindBufferBase(GL_UNIFORM_BUFFER, 0, ubo);
  96.  
  97.     while (!glfwWindowShouldClose(window))
  98.     {
  99.         glfwPollEvents();
  100.  
  101.         glClearColor(1.0f, 0.0f, 1.0f, 1.0f);
  102.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  103.        
  104.         glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr);
  105.  
  106.         glfwSwapBuffers(window);
  107.     }
  108.  
  109.     glUseProgram(0);
  110.  
  111.     glDetachShader(program, vertexId);
  112.     glDetachShader(program, fragmentId);
  113.     glDeleteShader(vertexId);
  114.     glDeleteShader(fragmentId);
  115.     glDeleteProgram(program);
  116.  
  117.     glDeleteBuffers(1, &ibo);
  118.     glDeleteBuffers(1, &vbo);
  119.     glDeleteVertexArrays(1, &vao);
  120.  
  121.     glfwDestroyWindow(window);
  122.     glfwTerminate();
  123. }
  124.  
  125. static std::vector<uint32_t> CompileShader(const std::string& source, shaderc_shader_kind stage)
  126. {
  127.     shaderc::Compiler compiler;
  128.     shaderc::CompileOptions compileOptions;
  129.     compileOptions.SetTargetEnvironment(shaderc_target_env_opengl, shaderc_env_version_opengl_4_5);
  130.     compileOptions.SetGenerateDebugInfo();
  131.     compileOptions.SetOptimizationLevel(shaderc_optimization_level_performance);
  132.  
  133.     shaderc::CompilationResult<uint32_t> result = compiler.CompileGlslToSpv(source, stage, "shader", compileOptions);
  134.     return std::vector<uint32_t>(result.begin(), result.end());
  135. }
  136.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement