compile shader

本文介绍了如何通过查询 GL_SHADER_COMPILER 来确定图形接口是否支持在线着色器编译,并提供了减轻编译资源消耗的方法。此外,还展示了如何使用 glReleaseShaderCompiler 函数释放编译器资源。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

First of all, you can determine whether an implementation supports online
shader compilation by checking for the value of GL_SHADER_COMPILER
using glGetBooleanv. If this value is GL_TRUE, then the implementation
supports online shader source compilation. If this value is GL_FALSE, then
only binary shaders are supported (more on that next). Given that an
online compiler is supported, this means you can specify your shaders using
glShaderSource as we have done so far in our examples. There is one thing
you can do to try to mitigate the resource impact of shader compilation.
Once you are finished compiling any shaders for your application, you can
call glReleaseShaderCompiler. This function provides a hint to the
implementation that you are done with the shader compiler and it can free
its resources. Note that this function is only a hint, if you decide to compile
more shaders using glCompileShader, the implementation will need to
reallocate its resources for the compiler.


Example 4-4 Querying for Whether a Shader Compiler is Available

GLboolean shaderCompiler;
GLint numBinaryFormats;
GLint *formats;
// Determine if a shader compiler available
glGetBooleanv(GL_SHADER_COMPILER, &shaderCompiler);
// Determine binary formats available
glGetIntegerv(GL_NUM_SHADER_BINARY_FORMATS, &numBinaryFormats);
formats = malloc(sizeof(GLint) * numBinaryFormats);
glGetIntegerv(GL_SHADER_BINARY_FORMATS, formats);

// "formats" now holds the list of supported binary formats


void glShaderBinary(GLint n, const GLuint* shaders,
GLenum binaryFormat,
const void* binary, GLint length)
n the number of shader objects in the shaders array

shaders an array of shader object handles. Depending on the specific binary format requirements,this will be either a vertex shader object, a fragment shader object, or both.This is defined by the vendor’s shader binary format extension

binaryFormat the vendor-specific binary format token 

binary pointer to the binary data generated by the offline compiler

length the number of bytes in the binary data



#include<glad/glad.h> #include<GLFW/glfw3.h> #include<glm/glm.hpp> #include<glm/matrix.hpp> #include<glm/gtc/type_ptr.hpp> #define STB_IMAGE_IMPLEMENTATION #include<stb/stb_image.h> #include<iostream> //定义窗口大小属性 #define W_width 800 #define W_height 600 //顶点着色器 const char* vertexShaderSource = "#version 330 core \n" "layout ( location 0 ) in vec3 aPos; \n" "layout ( location 1 ) in vec2 aTex; \n" "uniform mat4 model; \n" "uniform mat4 view; \n" "uniform mat4 projection; \n" "out vec2 texCoord; \n" "void main() \n" "{ \n" " gl_Position = projectionview*model*vec4(aPos, 1.0f);\n" " texCoord = aTexCoord; \n" "} \n" ; //片段着色器 const char* fragmentShaderSource = "#version 330 core \n" "in vec2 texCoord; \n" "uniform sampler2D texture1; \n" "out vec4 fragColor; \n" "void main() \n" "{ \n" " fragColor = texture(texture1, texCoord); \n" "} \n" ; //回调函数 void framebuffer_size_callback(GLFWwindow* window, int width, int height) { glViewport(0,0, width, height); } int main() { //glfw初始化 glfwInit(); glfwWindowHint(GLFW_VERSION_MAJOR, 3); glfwWindowHint(GLFW_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); //创建glfw窗口 GLFWwindow* window = glfwCreateWindow(W_width, W_height, "3D", NULL, NULL); if(!window) { std::cout<<"failed to create window!" << std::endl; return -1; } //glfw窗口上下文绑定当前线程上下文 glfwMakeContextCurrent(window); //设置帧缓冲大小回调函数 glfwSetFramebufferSizeCallback(window, framebuffer_size_callback); //初始化glad if(!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) { std::cout<< "failed to loader glad !"<< std::endl; return -1; } glEnable(GL_DEPTH_TEST); //编译顶点着色器 GLuint vertexShader = glCreateShader(GL_VERTEX_SHADER); glShaderSource(vertexShader, 1, &vertexShaderSource, NULL); glCompileShader(vertexShader); int success; char infoLog[512]; glGetShaderiv(vertexShader, GL_COMPILE_STATUS, &success); if(!success) { glGetShaderInfoLog(vertexShader, 512, NULL, infoLog); std::cout<< "failed to compile shader: "<<infoLog<<std::endl; return -1; } //编译片段着色器 GLuint fragmentShader = glCreateShader(GL_FRAGMENT_SHADER); glShaderSource(fragmentShader, 1, &fragmentShaderSource, NULL); glCompileShader(fragmentShader); glGetShaderiv(fragmentShader, GL_COMPILE_STATUS, &success); if(!success) { glGetShaderInfoLog(fragmentShader, 512, NULL, infoLog); std::cout<< "failed to compile shader: "<<infoLog<<std::endl; return -1; } //链接项目 GLuint shaderProgram = glCreateProgram(); glAttachShader(shaderProgram, vertexShader); glAttachShader(shaderProgram, fragmentShader); glLinkProgram(shaderProgram); glGetProgramiv(shaderProgram, GL_LINK_STATUS, &success); if(!success) { glGetProgramInfoLog(shaderProgram, 512, NULL, infoLog); std::cout<< "failed to link shader: "<<infoLog<<std::endl; return -1; } glDeleteShader(vertexShader); glDeleteShader(fragmentShader); //创建纹理 GLuint texture1; glGenTextures(1, &texture1); glBindTexture(GL_TEXTURE_2D, texture1); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); int width, height, nrchannel; stbi_set_flip_vertically_on_load(true); unsigned char* data = stbi_load("muxiang.png", &width, &height, &nrchannel, 0); if (data) { glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data); glGenerateMipmap(GL_TEXTURE_2D); } else { std::cout<< "failed to load photo"<<std::endl; } stbi_image_free(data); //创建顶点属性数组 float vertices[] = { -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, -0.5f, 0.5f, 0.5f, 0.0f, 1.0f, -0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 0.5f, -0.5f, 0.5f, 1.0f, 0.0f, 0.5f, -0.5f, -0.5f, 1.0f, 1.0f, -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 0.5f, -0.5f, -0.5f, 1.0f, 1.0f, -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, -0.5f, 0.5f, -0.5f, 0.0f, 0.0f, 0.5f, 0.5f, -0.5f, 1.0f, 0.0f, -0.5f, 0.5f, -0.5f, 0.0f, 0.0f, 0.5f, 0.5f, -0.5f, 1.0f, 0.0f, 0.5f, 0.5f, 0.5f, 1.0f, 1.0f, -0.5f, 0.5f, 0.5f, 0.0f, 1.0f, -0.5f, 0.5f, 0.5f, 0.0f, 1.0f, -0.5f, 0.5f, -0.5f, 0.0f, 0.0f, -0.5f, -0.5f, 0.5f, 1.0f, 1.0f, -0.5f, -0.5f, -0.5f, 0.0f, 1.0f, 0.5f, 0.5f, 0.5f, 0.0f, 1.0f, 0.5f, 0.5f, -0.5f, 1.0f, 1.0f, 0.5f, -0.5f, 0.5f, 0.0f, 0.0f, 0.5f, -0.5f, -0.5f, 1.0f, 0.0f }; GLuint indices[] = { 0, 1, 2, 2, 3, 1, 4, 5, 6, 6, 7, 4, 8, 9, 10, 10, 11, 8, 12, 13, 14, 14, 15, 12, 16, 17, 18, 18, 19, 16, 20, 21, 22, 22, 23, 20 }; //创立VAO, VBO, EBO GLuint VAO, VBO, EBO; glGenVertexArrays(1, &VAO); glGenBuffers(1, &VBO); glGenBuffers(1, &EBO); glBindVertexArray(VAO); glBindBuffer(GL_ARRAY_BUFFER, VBO); glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, EBO); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), indices, GL_STATIC_DRAW); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 5*sizeof(float), (void*)0); glEnableVertexAttribArray(0); glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 5*sizeof(float), (void*)(3*sizeof(float))); glEnableVertexAttribArray(1); //绑定纹理 glUseProgram(shaderProgram); GLuint texLoaction = glGetUniformLocation(shaderProgram, "texture1"); glUniform1i(texLoaction, 0); while(!glfwWindowShouldClose(window)) { glClearColor(0.0f, 1.0f, 1.0f, 1.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glActiveTexture(GL_TEXTURE0); glBindTexture(GL_TEXTURE0, texture1); glm::mat4 model = glm::mat4(1.0f); glm::mat4 view = glm::mat4(1.0f); glm::mat4 projection = glm::mat4(1.0f); view = glm::translate(view, glm::vec3(0.0f, 0.0f, -3.0f)); projection = glm::perspective(glm::radians(45.0f), (float)W_width/(float)W_height, 0.1f, 100.0f); glUseProgram(shaderProgram); GLuint viewLocation = glGetUniformLocation(shaderProgram, "view"); GLuint projLocation = glGetUniformLocation(shaderProgram, "projection"); glUniformMatrix4fv(viewLocation, 1, GL_FALSE, glm::value_ptr(view)); glUniformMatrix4fv(projLocation, 1, GL_FALSE, glm::value_ptr(projection)); model = glm::rotate(model, (float)glfwGetTime()*glm::radians(45.0f), glm::vec3(0.5f, 1.0f, 0.0f)); GLuint modelLocation = glGetUniformLocation(shaderProgram, "model"); glUniformMatrix4fv(modelLocation, 1, GL_FALSE, glm::value_ptr(model)); glBindVertexArray(VAO); glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_INT, 0); glfwSwapBuffers(window); glfwPollEvents(); } glDeleteVertexArrays(1, &VAO); glDeleteBuffers(1, &VBO); glDeleteBuffers(1, &EBO); glfwTerminate(); return 0; }
最新发布
06-07
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值