From 1f24a2196641f7d832300fd45c7f5e89559ecc34 Mon Sep 17 00:00:00 2001 From: lonkaars Date: Sat, 18 May 2024 10:58:08 +0200 Subject: more problem isolation --- blob | 24 ------------------------ die.c | 16 ---------------- die.h | 5 ----- main.c | 38 ++++++++++++-------------------------- makefile | 33 +++++++-------------------------- shader.c | 52 ---------------------------------------------------- shader.h | 13 ------------- uniform.c | 40 ---------------------------------------- uniform.h | 10 ---------- 9 files changed, 19 insertions(+), 212 deletions(-) delete mode 100755 blob delete mode 100644 die.c delete mode 100644 die.h delete mode 100644 shader.c delete mode 100644 shader.h delete mode 100644 uniform.c delete mode 100644 uniform.h diff --git a/blob b/blob deleted file mode 100755 index 601337d..0000000 --- a/blob +++ /dev/null @@ -1,24 +0,0 @@ -#!/bin/sh -input="$1" -base="$2" - -# assembly (used to compile data with custom symbol name) -cat << EOF > "$base.s" -.section .rodata -.global ${base}_head -${base}_head: -.incbin "$input" -EOF - -# C header -cat << EOF > "$base.h" -#pragma once -// NOTE: THIS FILE IS GENERATED, DO NOT EDIT -#include - -extern const char ${base}_head; -static const char* ${base} = &${base}_head; -static const size_t ${base}_size = $(wc -c < "$input"); - -EOF - diff --git a/die.c b/die.c deleted file mode 100644 index 0635b31..0000000 --- a/die.c +++ /dev/null @@ -1,16 +0,0 @@ -#include -#include -#include - -#include "die.h" - -void die(const char* fmt, ...) { - va_list args; - va_start(args, fmt); - vfprintf(stderr, fmt, args); - fflush(stderr); - va_end(args); - - exit(EXIT_FAILURE); -} - diff --git a/die.h b/die.h deleted file mode 100644 index a3f391d..0000000 --- a/die.h +++ /dev/null @@ -1,5 +0,0 @@ -#pragma once - -/** @brief print error and exit */ -void die(const char*, ...); - diff --git a/main.c b/main.c index 4b8aa32..231f8c7 100644 --- a/main.c +++ b/main.c @@ -3,44 +3,30 @@ #include #include +const uint32_t fill_vert[] = #include "fill_vert.h" +; +const uint32_t visuals_frag[] = #include "visuals_frag.h" +; int main(int argc, char** argv) { // initialize window glfwInit(); - glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); - glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); - glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE); GLFWwindow* window = glfwCreateWindow(800, 600, "test", NULL, NULL); glfwMakeContextCurrent(window); glewInit(); - // initialize triangle - const float vertices[] = { - 0, 1, 0, - 1, -1, 0, - -1, -1, 0, - }; - GLuint VBO, VAO; - glGenBuffers(1, &VBO); - glBindBuffer(GL_ARRAY_BUFFER, VBO); - glGenVertexArrays(1, &VAO); - glBindVertexArray(VAO); - glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); - glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(float), (void*)0); - glEnableVertexAttribArray(0); - glBindBuffer(GL_ARRAY_BUFFER, 0); - glBindVertexArray(VAO); - // initialize shaders GLuint vert = glCreateShader(GL_VERTEX_SHADER); - glShaderBinary(1, &vert, GL_SHADER_BINARY_FORMAT_SPIR_V_ARB, fill_vert, fill_vert_size); - glSpecializeShaderARB(vert, "main", 0, NULL, NULL); + 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_ARB, visuals_frag, visuals_frag_size); - glSpecializeShaderARB(frag, "main", 0, NULL, NULL); + glShaderBinary(1, &frag, GL_SHADER_BINARY_FORMAT_SPIR_V, visuals_frag, sizeof(visuals_frag)); + glSpecializeShader(frag, "main", 0, NULL, NULL); + GLuint shader = glCreateProgram(); glAttachShader(shader, vert); glAttachShader(shader, frag); @@ -58,10 +44,10 @@ int main(int argc, char** argv) { // list all ACTIVE uniforms in shader: GLint count; - GLchar name[80]; - GLsizei length; glGetProgramiv(shader, GL_ACTIVE_UNIFORMS, &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); } diff --git a/makefile b/makefile index ef01ae8..9d46d62 100644 --- a/makefile +++ b/makefile @@ -2,35 +2,16 @@ LDFLAGS += -lglfw LDFLAGS += -lOpenGL LDFLAGS += -lGLEW -CFLAGS += -g - GLFLAGS += --target-env=opengl GLFLAGS += -fauto-map-locations -.PHONY: FORCE -.SECONDARY: # do not remove intermediate files - -all: main FORCE - -main: main.o -main: die.o -main: shader.o -main: visuals_frag.o -main: fill_vert.o -main: uniform.o - -# fix compile order -main.o: fill_vert.h -main.o: visuals_frag.h - -%.s %.h &: %.spv - ./blob $< $* +main: main.c -%_frag.spv: %.frag - glslc $(GLFLAGS) -o $@ $< -%_vert.spv: %.vert - glslc $(GLFLAGS) -o $@ $< +main.c: fill_vert.h +main.c: visuals_frag.h -clean: FORCE - git clean -fxdi +%_frag.h: %.frag + glslc $(GLFLAGS) -mfmt=c -o $@ $< +%_vert.h: %.vert + glslc $(GLFLAGS) -mfmt=c -o $@ $< diff --git a/shader.c b/shader.c deleted file mode 100644 index 6831b28..0000000 --- a/shader.c +++ /dev/null @@ -1,52 +0,0 @@ -#include "shader.h" - -#include "die.h" - -void check_shader(GLuint shader) { - int success; - char debug[LOG_MAX]; - glGetShaderiv(shader, GL_COMPILE_STATUS, &success); - if (success) return; - glGetShaderInfoLog(shader, LOG_MAX, NULL, debug); - die("error: shader compilation failed!\n%s", debug); -} - -void check_program(GLuint program) { - int success; - char debug[LOG_MAX]; - glGetProgramiv(program, GL_LINK_STATUS, &success); - if (success) return; - glGetProgramInfoLog(program, LOG_MAX, NULL, debug); - die("error: shader linking failed!\n%s", debug); -} - -GLuint load_shader(GLenum type, const char* const src, size_t src_size) { - GLuint shader = glCreateShader(type); - glShaderBinary(1, &shader, GL_SHADER_BINARY_FORMAT_SPIR_V_ARB, src, src_size); - glSpecializeShaderARB(shader, "main", 0, 0, 0); - check_shader(shader); - return shader; -} - -GLuint frag_shader(const char* const src, size_t src_size) { - return load_shader(GL_FRAGMENT_SHADER, src, src_size); -} -GLuint vert_shader(const char* const src, size_t src_size) { - return load_shader(GL_VERTEX_SHADER, src, src_size); -} - -GLuint link_shaders(GLuint vert, GLuint frag) { - GLuint shader = glCreateProgram(); - glAttachShader(shader, vert); - glAttachShader(shader, frag); - glLinkProgram(shader); - - check_program(shader); - - glUseProgram(shader); - - glDeleteShader(vert); - glDeleteShader(frag); - return shader; -} - diff --git a/shader.h b/shader.h deleted file mode 100644 index 6a4739e..0000000 --- a/shader.h +++ /dev/null @@ -1,13 +0,0 @@ -#pragma once - -#include -#include -#include -#include - -#define LOG_MAX 512 - -GLuint frag_shader(const char* const src, size_t src_size); -GLuint vert_shader(const char* const src, size_t src_size); -GLuint link_shaders(GLuint vert, GLuint frag); - diff --git a/uniform.c b/uniform.c deleted file mode 100644 index 6c66b7a..0000000 --- a/uniform.c +++ /dev/null @@ -1,40 +0,0 @@ -#include -#include -#include - -#include "uniform.h" - -int time_start = 0; -GLFWwindow* window = NULL; -GLint u_time, u_window; - -void init_uniform(GLuint shader, GLFWwindow* _window) { - window = _window; - - u_time = glGetUniformLocation(shader, "time"); - u_window = glGetUniformLocation(shader, "window"); - - time_start = time(NULL); -} - -/** @brief update `uniform float time` */ -static void update_u_time() { - struct timeval t; - gettimeofday(&t, NULL); - t.tv_sec -= time_start; - float time = t.tv_sec + (t.tv_usec / 1e6); - glUniform1f(u_time, time); -} - -/** @brief update `uniform ivec2 window` */ -static void update_u_window() { - int width, height; - glfwGetWindowSize(window, &width, &height); - glUniform2i(u_window, width, height); -} - -void update_uniform() { - update_u_time(); - update_u_window(); -} - diff --git a/uniform.h b/uniform.h deleted file mode 100644 index d7c58b3..0000000 --- a/uniform.h +++ /dev/null @@ -1,10 +0,0 @@ -#pragma once - -#include -#include -#include -#include - -void init_uniform(GLuint shader, GLFWwindow* window); -void update_uniform(); - -- cgit v1.2.3