Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define GLFW_INCLUDE_NONE
- #include <GLFW/glfw3.h>
- #include <glad/glad.h>
- #include <shaderc/shaderc.hpp>
- #include <string>
- std::string s_VertexShader = R"(
- #version 450 core
- layout (location = 0) in vec3 in_position;
- void main()
- {
- gl_Position = vec4(in_position, 1.0f);
- }
- )";
- std::string s_FragmentShader = R"(
- #version 450 core
- layout (location = 0) out vec4 out_colour;
- layout (binding = 0, std140) uniform Colours
- {
- vec3 Colour;
- };
- void main()
- {
- out_colour = vec4(Colour, 1.0);
- }
- )";
- static std::vector<uint32_t> CompileShader(const std::string& source, shaderc_shader_kind stage);
- int main()
- {
- glfwInit();
- glfwDefaultWindowHints();
- GLFWwindow* window = glfwCreateWindow(1280, 720, "OpenGL shaderc ubo example", nullptr, nullptr);
- glfwShowWindow(window);
- glfwMakeContextCurrent(window);
- gladLoadGLLoader((GLADloadproc)glfwGetProcAddress);
- float vertices[] = {
- -0.5f, 0.5f, 0.0f,
- -0.5f, -0.5f, 0.0f,
- 0.5f, 0.5f, 0.0f,
- 0.5f, -0.5f, 0.0f
- };
- uint32_t indices[] = {
- 0, 1, 2,
- 2, 1, 3
- };
- uint32_t vao, vbo, ibo, ubo;
- glCreateVertexArrays(1, &vao);
- glBindVertexArray(vao);
- glCreateBuffers(1, &vbo);
- glBindBuffer(GL_ARRAY_BUFFER, vbo);
- glNamedBufferData(vbo, sizeof(vertices), vertices, GL_STATIC_DRAW);
- glEnableVertexAttribArray(0);
- glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0);
- glCreateBuffers(1, &ibo);
- glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
- glNamedBufferData(ibo, sizeof(indices), indices, GL_STATIC_DRAW);
- glCreateBuffers(1, &ubo);
- glNamedBufferData(ubo, 3 * sizeof(float), nullptr, GL_DYNAMIC_DRAW);
- uint32_t program, vertexId, fragmentId;
- program = glCreateProgram();
- auto vertexBinary = CompileShader(s_VertexShader, shaderc_vertex_shader);
- vertexId = glCreateShader(GL_VERTEX_SHADER);
- glShaderBinary(1, &vertexId, GL_SHADER_BINARY_FORMAT_SPIR_V, vertexBinary.data(), vertexBinary.size() * sizeof(uint32_t));
- glSpecializeShader(vertexId, "main", 0, nullptr, nullptr);
- glAttachShader(program, vertexId);
- auto fragmentBinary = CompileShader(s_FragmentShader, shaderc_fragment_shader);
- fragmentId = glCreateShader(GL_FRAGMENT_SHADER);
- glShaderBinary(1, &fragmentId, GL_SHADER_BINARY_FORMAT_SPIR_V, fragmentBinary.data(), fragmentBinary.size() * sizeof(uint32_t));
- glSpecializeShader(fragmentId, "main", 0, nullptr, nullptr);
- glAttachShader(program, fragmentId);
- glLinkProgram(program);
- glValidateProgram(program);
- glUseProgram(program);
- float colour[3] = { 0.2f, 1.0f, 0.3f };
- glNamedBufferSubData(ubo, 0, 3 * sizeof(float), colour);
- glBindBuffer(GL_UNIFORM_BUFFER, ubo);
- glBindBufferBase(GL_UNIFORM_BUFFER, 0, ubo);
- while (!glfwWindowShouldClose(window))
- {
- glfwPollEvents();
- glClearColor(1.0f, 0.0f, 1.0f, 1.0f);
- glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
- glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, nullptr);
- glfwSwapBuffers(window);
- }
- glUseProgram(0);
- glDetachShader(program, vertexId);
- glDetachShader(program, fragmentId);
- glDeleteShader(vertexId);
- glDeleteShader(fragmentId);
- glDeleteProgram(program);
- glDeleteBuffers(1, &ibo);
- glDeleteBuffers(1, &vbo);
- glDeleteVertexArrays(1, &vao);
- glfwDestroyWindow(window);
- glfwTerminate();
- }
- static std::vector<uint32_t> CompileShader(const std::string& source, shaderc_shader_kind stage)
- {
- shaderc::Compiler compiler;
- shaderc::CompileOptions compileOptions;
- compileOptions.SetTargetEnvironment(shaderc_target_env_opengl, shaderc_env_version_opengl_4_5);
- compileOptions.SetGenerateDebugInfo();
- compileOptions.SetOptimizationLevel(shaderc_optimization_level_performance);
- shaderc::CompilationResult<uint32_t> result = compiler.CompileGlslToSpv(source, stage, "shader", compileOptions);
- return std::vector<uint32_t>(result.begin(), result.end());
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement