common code for shaders

This commit is contained in:
2025-09-15 23:39:18 +02:00
parent 858d504528
commit e91c113471
16 changed files with 139 additions and 91 deletions
+2 -2
View File
@@ -1,6 +1,6 @@
AUTOMAKE_OPTIONS = foreign subdir-objects -Wall
bin_PROGRAMS = forge
forge_SOURCES = src/main.c src/args.c src/forge.c src/file.c src/window.c src/shaders.c src/timer.c $(top_srcdir)/include/glad/gl.h
forge_SOURCES = src/main.c src/args.c src/forge.c src/file.c src/window.c src/shaders.c src/timer.c src/strings.c $(top_srcdir)/include/glad/gl.h
forge_CFLAGS = -Ofast -march=native -flto -funroll-loops -fprefetch-loop-arrays -fno-exceptions -fopenmp -I$(top_srcdir)/include -DGLFW_INCLUDE_NONE
forge_LDADD = -lm -lGL -lglfw
include_HEADERS = src/main.h src/args.h src/config.h src/types.h src/forge.h src/file.h src/constants.h src/window.h src/shaders.h src/logs.h src/timer.h $(top_srcdir)/include/glad/gl.h $(top_srcdir)/include/linmath.h
include_HEADERS = src/main.h src/args.h src/config.h src/types.h src/forge.h src/file.h src/constants.h src/window.h src/shaders.h src/logs.h src/timer.h src/strings.h $(top_srcdir)/include/glad/gl.h $(top_srcdir)/include/linmath.h
+8 -3
View File
@@ -1,6 +1,6 @@
TARGET ?= forge
INSTALL_DIR ?= $(HOME)/.local/bin
TEST_ARGS ?=
TEST_ARGS ?= --hot-reload --frag=./shaders
SHELL := /bin/bash
.PHONY: build
@@ -18,7 +18,7 @@ build:
-o build/$(TARGET)
run: build
./build/forge --hot-reload --frag=./shaders
./build/$(TARGET) $(TEST_ARGS)
.PHONY: install
install: build
@@ -26,7 +26,12 @@ install: build
.PHONY: valgrind
valgrind: build
valgrind --leak-check=full -s ./build/$(TARGET) $(TEST_ARGS)
valgrind \
--leak-check=full \
--track-fds=all \
--show-realloc-size-zero=no \
--undef-value-errors=no \
./build/$(TARGET) $(TEST_ARGS)
.PHONY: release
release: clean
+1 -1
View File
@@ -102,7 +102,7 @@ make -f Makefile.dev release-arch
- [ ] Multi-stage shaders
- [x] Test 2 stages with render to texture
- [x] 2 in 2 fx 1 mix 1 fx layout
- [ ] Include fragments with special directive
- [x] Include common code
- [ ] 16 input + 16 fx definition and selection (with param)
- [x] Feedback texture
- [ ] Free opengl memory
+19
View File
@@ -0,0 +1,19 @@
#version 460
// COMMON DEFINITIONS AND FUNCTIONS
uniform float iTime;
uniform vec2 iResolution;
uniform sampler2D frame0;
uniform sampler2D frame1;
uniform sampler2D frame2;
uniform sampler2D frame3;
uniform sampler2D frame4;
uniform sampler2D frame5;
uniform sampler2D frame6;
uniform sampler2D frame7;
in vec2 vUV;
// TODO things
+2 -10
View File
@@ -1,20 +1,12 @@
#version 460
uniform float iTime;
uniform vec2 iResolution;
uniform sampler2D frame0;
uniform sampler2D frame1;
in vec2 vUV;
layout(location = 3) out vec3 fragColor;
// SRC A
// ---------
// IN: 0 / 1
// OUT: 3
layout(location = 3) out vec3 fragColor;
void main() {
vec2 uv0 = vUV.st;
float ratio = iResolution.x / iResolution.y;
+2 -9
View File
@@ -1,19 +1,12 @@
#version 460
uniform float iTime;
uniform vec2 iResolution;
uniform sampler2D frame0;
uniform sampler2D frame2;
in vec2 vUV;
layout(location = 4) out vec3 fragColor;
// SRC B
// ---------
// IN: 0 / 2
// OUT: 4
layout(location = 4) out vec3 fragColor;
void main() {
fragColor = vec3(vUV, 0.0) * step(0.3, vUV.x) * step(-0.4, -vUV.x);
}
+2 -9
View File
@@ -1,19 +1,12 @@
#version 460
uniform float iTime;
uniform vec2 iResolution;
uniform sampler2D frame3;
uniform sampler2D frame5;
in vec2 vUV;
layout(location = 5) out vec3 fragColor;
// FX A
// ---------
// IN: 3 / 5
// OUT: 5
layout(location = 5) out vec3 fragColor;
void main() {
fragColor = texture(frame3, vUV).xyz;
}
+2 -9
View File
@@ -1,19 +1,12 @@
#version 460
uniform float iTime;
uniform vec2 iResolution;
uniform sampler2D frame4;
uniform sampler2D frame6;
in vec2 vUV;
layout(location = 6) out vec3 fragColor;
// FX B
// ---------
// IN: 4 / 6
// OUT: 6
layout(location = 6) out vec3 fragColor;
void main() {
fragColor = texture(frame4, vUV).xyz;
}
+2 -9
View File
@@ -1,19 +1,12 @@
#version 460
uniform float iTime;
uniform vec2 iResolution;
uniform sampler2D frame5;
uniform sampler2D frame6;
in vec2 vUV;
layout(location = 7) out vec3 fragColor;
// A+B
// ---------
// IN: 5 / 6
// OUT: 7
layout(location = 7) out vec3 fragColor;
void main() {
fragColor = texture(frame5, vUV).xyz + texture(frame6, vUV).xyz;
}
+2 -9
View File
@@ -1,19 +1,12 @@
#version 460
uniform float iTime;
uniform vec2 iResolution;
uniform sampler2D frame7;
uniform sampler2D frame0;
in vec2 vUV;
layout(location = 0) out vec3 fragColor;
// MFX
// ---------
// IN: 7 / 0
// OUT: 0
layout(location = 0) out vec3 fragColor;
void main() {
fragColor = texture(frame7, vUV).xyz;
}
+11 -1
View File
@@ -5,6 +5,7 @@
#include <sys/types.h>
#include "logs.h"
#include "strings.h"
#include "types.h"
time_t get_file_time(File file) {
@@ -64,4 +65,13 @@ File read_file(char *path) {
return file;
}
void free_file(File *file) { free(file->content); }
void prepend_file(File *src, File extra) {
char *old_src_content = src->content;
src->content = concat(extra.content, src->content);
free(old_src_content);
}
void free_file(File *file) {
free(file->content);
free(file->path);
}
+2
View File
@@ -11,4 +11,6 @@ void update_file(File *file);
void free_file(File *file);
void prepend_file(File *src, File extra);
#endif
+62 -22
View File
@@ -36,21 +36,33 @@ void compute_fps(Window *window, Timer *timer) {
}
}
void loop(Window *window, ShaderProgram program, bool hot_reload,
File *fragment_shaders, Timer *timer) {
Context context;
void hot_reload(ShaderProgram program, File *common_shader_code,
File *fragment_shaders) {
int i;
bool force_update = false;
compute_fps(window, timer);
if (should_update_file(*common_shader_code)) {
update_file(common_shader_code);
force_update = true;
}
if (hot_reload) {
// TODO extract to function
for (i = 0; i < FRAG_COUNT; i++) {
if (should_update_file(fragment_shaders[i])) {
if (force_update || should_update_file(fragment_shaders[i])) {
update_file(&fragment_shaders[i]);
prepend_file(&fragment_shaders[i], *common_shader_code);
update_program(program, fragment_shaders, i);
}
}
}
void loop(Window *window, ShaderProgram program, bool hr,
File *common_shader_code, File *fragment_shaders, Timer *timer) {
Context context;
compute_fps(window, timer);
if (hr) {
hot_reload(program, common_shader_code, fragment_shaders);
}
context = get_window_context(window);
@@ -60,23 +72,53 @@ void loop(Window *window, ShaderProgram program, bool hot_reload,
refresh_window(window);
}
File read_fragment_shader_file(char *frag_path, int i) {
File fragment_shader;
char *file_path = malloc(sizeof(char) * 1024);
sprintf(file_path, "%s/frag%d.glsl", frag_path, i);
fragment_shader = read_file(file_path);
if (fragment_shader.error) {
exit(EXIT_FAILURE);
}
return fragment_shader;
}
void init_files(char *frag_path, File *common_shader_code,
File *fragment_shaders) {
int i;
for (i = 0; i < FRAG_COUNT + 1; i++) {
if (i == 0) {
(*common_shader_code) = read_fragment_shader_file(frag_path, i);
} else {
fragment_shaders[i - 1] = read_fragment_shader_file(frag_path, i);
prepend_file(&fragment_shaders[i - 1], *common_shader_code);
}
}
}
void free_files(File *common_shader_code, File *fragment_shaders) {
int i;
for (i = 0; i < FRAG_COUNT; i++) {
free_file(&fragment_shaders[i]);
}
free_file(common_shader_code);
}
void forge_run(Parameters params) {
File fragment_shaders[FRAG_COUNT];
File common_shader_code;
ShaderProgram program;
Window *window;
Timer timer;
Context context;
int i;
char file_path[FRAG_COUNT][1024];
// TODO extract to function
for (i = 0; i < FRAG_COUNT; i++) {
sprintf(file_path[i], "%s/frag%d.glsl", params.frag_path, i + 1);
fragment_shaders[i] = read_file(file_path[i]);
if (fragment_shaders[i].error) {
exit(EXIT_FAILURE);
}
}
init_files(params.frag_path, &common_shader_code, fragment_shaders);
window = init_window(PACKAGE " " VERSION, params.screen, error_callback,
key_callback);
@@ -93,13 +135,11 @@ void forge_run(Parameters params) {
timer = create_timer(60);
while (!window_should_close(window)) {
loop(window, program, params.hot_reload, fragment_shaders, &timer);
loop(window, program, params.hot_reload, &common_shader_code,
fragment_shaders, &timer);
}
close_window(window, true);
// TODO extract to function
for (i = 0; i < FRAG_COUNT; i++) {
free_file(&fragment_shaders[i]);
}
free_files(&common_shader_code, fragment_shaders);
}
-2
View File
@@ -257,5 +257,3 @@ void apply_program(ShaderProgram program, Context context) {
glDrawArrays(GL_TRIANGLES, 0, 6);
}
}
// TODO clean buffers from opengl memories
+11
View File
@@ -0,0 +1,11 @@
#include <stdlib.h>
#include <string.h>
char *concat(const char *s1, const char *s2) {
char *result =
malloc(strlen(s1) + strlen(s2) + 1); // +1 for the null-terminator
// in real code you would check for errors in malloc here
strcpy(result, s1);
strcat(result, s2);
return result;
}
+6
View File
@@ -0,0 +1,6 @@
#ifndef STRINGS_H
#define STRINGS_H
char *concat(const char *s1, const char *s2);
#endif