From 1b1314ed856f8380726fd07cde5dd73125101ef2 Mon Sep 17 00:00:00 2001 From: lonkaars Date: Sat, 18 May 2024 11:17:07 +0200 Subject: more problem research/isolation --- fill.vert | 10 ------ main.c | 101 ++++++++++++++++++++++++++++++++++++++++++----------------- makefile | 4 +-- spirv.frag | 8 +++++ spirv.vert | 7 +++++ visuals.frag | 9 ------ 6 files changed, 90 insertions(+), 49 deletions(-) delete mode 100644 fill.vert create mode 100644 spirv.frag create mode 100644 spirv.vert delete mode 100644 visuals.frag 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); -} - diff --git a/main.c b/main.c index 231f8c7..72c84ce 100644 --- a/main.c +++ b/main.c @@ -3,58 +3,103 @@ #include #include -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; } diff --git a/makefile b/makefile index 9d46d62..1ffe4ef 100644 --- a/makefile +++ b/makefile @@ -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/spirv.frag b/spirv.frag new file mode 100644 index 0000000..f417dc1 --- /dev/null +++ b/spirv.frag @@ -0,0 +1,8 @@ +#version 330 core +uniform float test; + +void main() { + vec2 uv = gl_FragCoord.xy / ivec2(800, 600); + gl_FragColor = vec4(uv.xy, test, 1.0); +} + 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); +} + diff --git a/visuals.frag b/visuals.frag deleted file mode 100644 index 8e83f6e..0000000 --- a/visuals.frag +++ /dev/null @@ -1,9 +0,0 @@ -#version 330 core - -uniform float test; - -void main() { - vec2 uv = gl_FragCoord.xy / ivec2(800, 600); - gl_FragColor = vec4(uv.xy, test, 1.0); -} - -- cgit v1.2.3