1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
#include <stdio.h>
#include <stdlib.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
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_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;
}
|