aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlonkaars <loek@pipeframe.xyz>2024-05-16 18:55:58 +0200
committerlonkaars <loek@pipeframe.xyz>2024-05-16 18:55:58 +0200
commit107684b00d65eac84e4bf2dfd34073d309230d50 (patch)
tree131215f9b95d1a8a24fa92c99982ee691a7b4724
parent766a817a23403ea2aeb325bea9f340f54a3e7987 (diff)
move shader compilation from runtime to compile-time
-rw-r--r--.gitignore1
-rwxr-xr-xblob25
-rwxr-xr-xblobheader15
-rw-r--r--makefile14
-rw-r--r--shader.c8
5 files changed, 38 insertions, 25 deletions
diff --git a/.gitignore b/.gitignore
index 47cfc5d..91e6b1a 100644
--- a/.gitignore
+++ b/.gitignore
@@ -2,3 +2,4 @@
main
*_frag.*
*_vert.*
+*.spv
diff --git a/blob b/blob
new file mode 100755
index 0000000..6baae9c
--- /dev/null
+++ b/blob
@@ -0,0 +1,25 @@
+#!/bin/sh
+input="$1"
+base="$2"
+symbol_base="$(echo "$base" | tr '[:punct:]' '_')"
+
+# assembly (used to compile data with custom symbol name)
+cat << EOF > "$base.s"
+.section .rodata
+.global ${symbol_base}_head
+${symbol_base}_head:
+.incbin "$input"
+EOF
+
+# C header
+cat << EOF > "$base.h"
+#pragma once
+// NOTE: THIS FILE IS GENERATED, DO NOT EDIT
+#include <stddef.h>
+
+extern const char ${symbol_base}_head;
+const char* ${symbol_base} = &${symbol_base}_head;
+const size_t ${symbol_base}_size = $(wc -c < "$input");
+
+EOF
+
diff --git a/blobheader b/blobheader
deleted file mode 100755
index c188205..0000000
--- a/blobheader
+++ /dev/null
@@ -1,15 +0,0 @@
-#!/bin/sh
-source="$1"
-symbol_base="$(echo "$source" | tr '[:punct:]' '_')"
-cat << EOF
-#pragma once
-// NOTE: THIS FILE IS GENERATED, DO NOT EDIT
-#include <stddef.h>
-
-extern char _binary_${symbol_base}_start;
-extern char _binary_${symbol_base}_end;
-
-#define ${symbol_base} ((const char* const)(&_binary_${symbol_base}_start))
-#define ${symbol_base}_size ((size_t)(&_binary_${symbol_base}_end - &_binary_${symbol_base}_start))
-
-EOF
diff --git a/makefile b/makefile
index e024267..34c19bf 100644
--- a/makefile
+++ b/makefile
@@ -5,6 +5,7 @@ LDFLAGS += -lGLEW
CFLAGS += -g
.PHONY: FORCE
+.SECONDARY: # do not remove intermediate files
all: main FORCE
@@ -18,12 +19,13 @@ main: hello_vert.o
main.o: hello_vert.h
main.o: hello_frag.h
-%_frag.o %_frag.h &: %.frag
- ld -r -b binary -o $*_frag.o $<
- ./blobheader $< > $*_frag.h
-%_vert.o %_vert.h &: %.vert
- ld -r -b binary -o $*_vert.o $<
- ./blobheader $< > $*_vert.h
+%.s %.h &: %.spv
+ ./blob $< $*
+
+%_frag.spv: %.frag
+ glslc -o $@ $<
+%_vert.spv: %.vert
+ glslc -o $@ $<
clean: FORCE
git clean -fxdi
diff --git a/shader.c b/shader.c
index df17d5d..321439a 100644
--- a/shader.c
+++ b/shader.c
@@ -8,7 +8,7 @@ void check_shader(GLuint shader) {
glGetShaderiv(shader, GL_COMPILE_STATUS, &success);
if (success) return;
glGetShaderInfoLog(shader, LOG_MAX, NULL, debug);
- die("error: shader compilation failed:\n%s\n", debug);
+ die("error: shader compilation failed!\n%s", debug);
}
void check_program(GLuint program) {
@@ -17,13 +17,13 @@ void check_program(GLuint program) {
glGetProgramiv(program, GL_LINK_STATUS, &success);
if (success) return;
glGetProgramInfoLog(program, LOG_MAX, NULL, debug);
- die("error: shader linking failed:\n%s\n", 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);
- glShaderSource(shader, 1, &src, (const int*) &src_size);
- glCompileShader(shader);
+ glShaderBinary(1, &shader, GL_SHADER_BINARY_FORMAT_SPIR_V_ARB, src, src_size);
+ glSpecializeShaderARB(shader, "main", 0, 0, 0);
check_shader(shader);
return shader;
}