#include #include #include #include const uint32_t vert_spirv[] = #include "vert_spirv.h" ; const uint32_t frag_spirv[] = #include "frag_spirv.h" ; const char vert_src[] = { #include "vert_src.h" , 0x00 }; const char frag_src[] = { #include "frag_src.h" , 0x00 }; GLuint load_shader_src(GLenum type, const char* src) { GLuint shader = glCreateShader(type); glShaderSource(shader, 1, &src, NULL); glCompileShader(shader); return shader; } GLuint load_shader_spirv(GLenum type, const char* src, size_t size) { GLuint shader = glCreateShader(type); glShaderBinary(1, &shader, GL_SHADER_BINARY_FORMAT_SPIR_V, src, size); glSpecializeShader(shader, "main", 0, NULL, NULL); return shader; } GLuint link_shaders(GLuint vert, GLuint frag) { GLuint shader = glCreateProgram(); glAttachShader(shader, vert); glAttachShader(shader, frag); glLinkProgram(shader); glUseProgram(shader); // <- use the program before getting uniform name glDeleteShader(vert); glDeleteShader(frag); return shader; } void test(const char* label, GLuint shader) { printf("%s:\n", label); // list all ACTIVE uniforms in shader GLint count; glGetProgramiv(shader, GL_ACTIVE_UNIFORMS, &count); printf("\tactive uniform count: %d\n", count); for (unsigned i = 0; i < count; i++) { GLchar name[80]; GLsizei length; glGetActiveUniformName(shader, i, 80, &length, name); printf("\t(location = %u) = \"%.*s\"\n", i, length, name); } // try directly geting uniform location GLint uniform_location = glGetUniformLocation(shader, "test"); printf("\t`test` uniform location = %d\n", uniform_location); } int main(int argc, char** argv) { // initialize window glfwInit(); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 6); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE); GLFWwindow* window = glfwCreateWindow(800, 600, "test", NULL, NULL); glfwMakeContextCurrent(window); glewInit(); // print driver info printf("vendor: %s\n", glGetString(GL_VENDOR)); printf("renderer: %s\n", glGetString(GL_RENDERER)); printf("version: %s\n", glGetString(GL_VERSION)); printf("glsl version: %s\n", glGetString(GL_SHADING_LANGUAGE_VERSION)); printf("------------------------------\n"); // test w/ glsl source GLuint src_vert = load_shader_src(GL_VERTEX_SHADER, vert_src); GLuint src_frag = load_shader_src(GL_FRAGMENT_SHADER, vert_src); GLuint src_shader = link_shaders(src_vert, src_frag); test("GLSL", src_shader); // test w/ spir-v GLuint spirv_vert = load_shader_spirv(GL_VERTEX_SHADER, (char*) vert_spirv, sizeof(vert_spirv)); GLuint spirv_frag = load_shader_spirv(GL_FRAGMENT_SHADER, (char*) frag_spirv, sizeof(frag_spirv)); GLuint spirv_shader = link_shaders(spirv_vert, spirv_frag); test("SPIR-V", spirv_shader); return 0; }