diff options
author | lonkaars <loek@pipeframe.xyz> | 2024-05-18 11:17:07 +0200 |
---|---|---|
committer | lonkaars <loek@pipeframe.xyz> | 2024-05-18 11:17:07 +0200 |
commit | 1b1314ed856f8380726fd07cde5dd73125101ef2 (patch) | |
tree | f527f0b88abd6243710a0bb38343c9bbbadb445f | |
parent | 1f24a2196641f7d832300fd45c7f5e89559ecc34 (diff) |
more problem research/isolation
-rw-r--r-- | fill.vert | 10 | ||||
-rw-r--r-- | main.c | 101 | ||||
-rw-r--r-- | makefile | 4 | ||||
-rw-r--r-- | spirv.frag (renamed from visuals.frag) | 1 | ||||
-rw-r--r-- | spirv.vert | 7 |
5 files changed, 82 insertions, 41 deletions
diff --git a/fill.vert b/fill.vert deleted file mode 100644 index 3093ce0..0000000 --- a/fill.vert +++ /dev/null @@ -1,10 +0,0 @@ -#version 330 core -layout (location = 0) in vec3 vert; - -void main() { - // Setting w to 0 has the effect of an infinitely large zoom, which makes the - // single triangle fill the viewport completely. This is okay since I only - // care about the fragment shader. - gl_Position = vec4(vert.xyz, 0.001); -} - @@ -3,58 +3,103 @@ #include <GL/glew.h> #include <GLFW/glfw3.h> -const uint32_t fill_vert[] = -#include "fill_vert.h" +// spir-v shaders: +const uint32_t vert_spirv[] = +#include "spirv_vert.h" ; -const uint32_t visuals_frag[] = -#include "visuals_frag.h" +const uint32_t frag_spirv[] = +#include "spirv_frag.h" ; -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(); +// glsl source: +const char* vert_src = "\ +#version 330 core \n\ +layout (location = 0) in vec3 vert; \n\ + \n\ +void main() { \n\ + gl_Position = vec4(vert.xyz, 0.001); \n\ +} \n\ +\0"; +const char* frag_src = "\ +#version 330 core \n\ +uniform float test; \n\ + \n\ +void main() { \n\ + vec2 uv = gl_FragCoord.xy / ivec2(800, 600); \n\ + gl_FragColor = vec4(uv.xy, test, 1.0); \n\ +} \n\ +\0"; - // initialize shaders - GLuint vert = glCreateShader(GL_VERTEX_SHADER); - glShaderBinary(1, &vert, GL_SHADER_BINARY_FORMAT_SPIR_V, fill_vert, sizeof(fill_vert)); - glSpecializeShader(vert, "main", 0, NULL, NULL); - - GLuint frag = glCreateShader(GL_FRAGMENT_SHADER); - glShaderBinary(1, &frag, GL_SHADER_BINARY_FORMAT_SPIR_V, visuals_frag, sizeof(visuals_frag)); - glSpecializeShader(frag, "main", 0, NULL, NULL); +GLuint load_shader_src(GLenum type, const char* src) { + GLuint shader = glCreateShader(type); + glShaderSource(shader, 1, &frag_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); // <- used the program before getting uniform name + glUseProgram(shader); // <- use the program before getting uniform name glDeleteShader(vert); glDeleteShader(frag); + return shader; +} - // 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"); +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("[%u] = \"%.*s\"\n", i, length, name); + printf("\t[%u] = \"%.*s\"\n", i, length, name); } // directly get uniform location GLint uniform_location = glGetUniformLocation(shader, "test"); - printf("`test` uniform location = %d\n", uniform_location); + 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; } @@ -7,8 +7,8 @@ GLFLAGS += -fauto-map-locations main: main.c -main.c: fill_vert.h -main.c: visuals_frag.h +main.c: spirv_vert.h +main.c: spirv_frag.h %_frag.h: %.frag glslc $(GLFLAGS) -mfmt=c -o $@ $< diff --git a/visuals.frag b/spirv.frag index 8e83f6e..f417dc1 100644 --- a/visuals.frag +++ b/spirv.frag @@ -1,5 +1,4 @@ #version 330 core - uniform float test; void main() { diff --git a/spirv.vert b/spirv.vert new file mode 100644 index 0000000..f9cf360 --- /dev/null +++ b/spirv.vert @@ -0,0 +1,7 @@ +#version 330 core +layout (location = 0) in vec3 vert; + +void main() { + gl_Position = vec4(vert.xyz, 0.001); +} + |