diff options
Diffstat (limited to 'main.c')
-rw-r--r-- | main.c | 101 |
1 files changed, 73 insertions, 28 deletions
@@ -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; } |