common code for shaders
This commit is contained in:
+2
-2
@@ -1,6 +1,6 @@
|
|||||||
AUTOMAKE_OPTIONS = foreign subdir-objects -Wall
|
AUTOMAKE_OPTIONS = foreign subdir-objects -Wall
|
||||||
bin_PROGRAMS = forge
|
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_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
|
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
@@ -1,6 +1,6 @@
|
|||||||
TARGET ?= forge
|
TARGET ?= forge
|
||||||
INSTALL_DIR ?= $(HOME)/.local/bin
|
INSTALL_DIR ?= $(HOME)/.local/bin
|
||||||
TEST_ARGS ?=
|
TEST_ARGS ?= --hot-reload --frag=./shaders
|
||||||
SHELL := /bin/bash
|
SHELL := /bin/bash
|
||||||
|
|
||||||
.PHONY: build
|
.PHONY: build
|
||||||
@@ -18,7 +18,7 @@ build:
|
|||||||
-o build/$(TARGET)
|
-o build/$(TARGET)
|
||||||
|
|
||||||
run: build
|
run: build
|
||||||
./build/forge --hot-reload --frag=./shaders
|
./build/$(TARGET) $(TEST_ARGS)
|
||||||
|
|
||||||
.PHONY: install
|
.PHONY: install
|
||||||
install: build
|
install: build
|
||||||
@@ -26,7 +26,12 @@ install: build
|
|||||||
|
|
||||||
.PHONY: valgrind
|
.PHONY: valgrind
|
||||||
valgrind: build
|
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
|
.PHONY: release
|
||||||
release: clean
|
release: clean
|
||||||
|
|||||||
@@ -102,7 +102,7 @@ make -f Makefile.dev release-arch
|
|||||||
- [ ] Multi-stage shaders
|
- [ ] Multi-stage shaders
|
||||||
- [x] Test 2 stages with render to texture
|
- [x] Test 2 stages with render to texture
|
||||||
- [x] 2 in 2 fx 1 mix 1 fx layout
|
- [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)
|
- [ ] 16 input + 16 fx definition and selection (with param)
|
||||||
- [x] Feedback texture
|
- [x] Feedback texture
|
||||||
- [ ] Free opengl memory
|
- [ ] Free opengl memory
|
||||||
|
|||||||
@@ -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
@@ -1,20 +1,12 @@
|
|||||||
#version 460
|
#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
|
// SRC A
|
||||||
// ---------
|
// ---------
|
||||||
// IN: 0 / 1
|
// IN: 0 / 1
|
||||||
// OUT: 3
|
// OUT: 3
|
||||||
|
|
||||||
|
layout(location = 3) out vec3 fragColor;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
vec2 uv0 = vUV.st;
|
vec2 uv0 = vUV.st;
|
||||||
float ratio = iResolution.x / iResolution.y;
|
float ratio = iResolution.x / iResolution.y;
|
||||||
|
|||||||
+2
-9
@@ -1,19 +1,12 @@
|
|||||||
#version 460
|
#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
|
// SRC B
|
||||||
// ---------
|
// ---------
|
||||||
// IN: 0 / 2
|
// IN: 0 / 2
|
||||||
// OUT: 4
|
// OUT: 4
|
||||||
|
|
||||||
|
layout(location = 4) out vec3 fragColor;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
fragColor = vec3(vUV, 0.0) * step(0.3, vUV.x) * step(-0.4, -vUV.x);
|
fragColor = vec3(vUV, 0.0) * step(0.3, vUV.x) * step(-0.4, -vUV.x);
|
||||||
}
|
}
|
||||||
+2
-9
@@ -1,19 +1,12 @@
|
|||||||
#version 460
|
#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
|
// FX A
|
||||||
// ---------
|
// ---------
|
||||||
// IN: 3 / 5
|
// IN: 3 / 5
|
||||||
// OUT: 5
|
// OUT: 5
|
||||||
|
|
||||||
|
layout(location = 5) out vec3 fragColor;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
fragColor = texture(frame3, vUV).xyz;
|
fragColor = texture(frame3, vUV).xyz;
|
||||||
}
|
}
|
||||||
+2
-9
@@ -1,19 +1,12 @@
|
|||||||
#version 460
|
#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
|
// FX B
|
||||||
// ---------
|
// ---------
|
||||||
// IN: 4 / 6
|
// IN: 4 / 6
|
||||||
// OUT: 6
|
// OUT: 6
|
||||||
|
|
||||||
|
layout(location = 6) out vec3 fragColor;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
fragColor = texture(frame4, vUV).xyz;
|
fragColor = texture(frame4, vUV).xyz;
|
||||||
}
|
}
|
||||||
+2
-9
@@ -1,19 +1,12 @@
|
|||||||
#version 460
|
#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
|
// A+B
|
||||||
// ---------
|
// ---------
|
||||||
// IN: 5 / 6
|
// IN: 5 / 6
|
||||||
// OUT: 7
|
// OUT: 7
|
||||||
|
|
||||||
|
layout(location = 7) out vec3 fragColor;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
fragColor = texture(frame5, vUV).xyz + texture(frame6, vUV).xyz;
|
fragColor = texture(frame5, vUV).xyz + texture(frame6, vUV).xyz;
|
||||||
}
|
}
|
||||||
+2
-9
@@ -1,19 +1,12 @@
|
|||||||
#version 460
|
#version 460
|
||||||
|
|
||||||
uniform float iTime;
|
|
||||||
uniform vec2 iResolution;
|
|
||||||
|
|
||||||
uniform sampler2D frame7;
|
|
||||||
uniform sampler2D frame0;
|
|
||||||
|
|
||||||
in vec2 vUV;
|
|
||||||
layout(location = 0) out vec3 fragColor;
|
|
||||||
|
|
||||||
// MFX
|
// MFX
|
||||||
// ---------
|
// ---------
|
||||||
// IN: 7 / 0
|
// IN: 7 / 0
|
||||||
// OUT: 0
|
// OUT: 0
|
||||||
|
|
||||||
|
layout(location = 0) out vec3 fragColor;
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
fragColor = texture(frame7, vUV).xyz;
|
fragColor = texture(frame7, vUV).xyz;
|
||||||
}
|
}
|
||||||
+11
-1
@@ -5,6 +5,7 @@
|
|||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
|
||||||
#include "logs.h"
|
#include "logs.h"
|
||||||
|
#include "strings.h"
|
||||||
#include "types.h"
|
#include "types.h"
|
||||||
|
|
||||||
time_t get_file_time(File file) {
|
time_t get_file_time(File file) {
|
||||||
@@ -64,4 +65,13 @@ File read_file(char *path) {
|
|||||||
return file;
|
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);
|
||||||
|
}
|
||||||
@@ -11,4 +11,6 @@ void update_file(File *file);
|
|||||||
|
|
||||||
void free_file(File *file);
|
void free_file(File *file);
|
||||||
|
|
||||||
|
void prepend_file(File *src, File extra);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
+66
-26
@@ -36,21 +36,33 @@ void compute_fps(Window *window, Timer *timer) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop(Window *window, ShaderProgram program, bool hot_reload,
|
void hot_reload(ShaderProgram program, File *common_shader_code,
|
||||||
File *fragment_shaders, Timer *timer) {
|
File *fragment_shaders) {
|
||||||
Context context;
|
|
||||||
int i;
|
int i;
|
||||||
|
bool force_update = false;
|
||||||
|
|
||||||
|
if (should_update_file(*common_shader_code)) {
|
||||||
|
update_file(common_shader_code);
|
||||||
|
force_update = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (i = 0; i < FRAG_COUNT; 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);
|
compute_fps(window, timer);
|
||||||
|
|
||||||
if (hot_reload) {
|
if (hr) {
|
||||||
// TODO extract to function
|
hot_reload(program, common_shader_code, fragment_shaders);
|
||||||
for (i = 0; i < FRAG_COUNT; i++) {
|
|
||||||
if (should_update_file(fragment_shaders[i])) {
|
|
||||||
update_file(&fragment_shaders[i]);
|
|
||||||
update_program(program, fragment_shaders, i);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
context = get_window_context(window);
|
context = get_window_context(window);
|
||||||
@@ -60,23 +72,53 @@ void loop(Window *window, ShaderProgram program, bool hot_reload,
|
|||||||
refresh_window(window);
|
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) {
|
void forge_run(Parameters params) {
|
||||||
File fragment_shaders[FRAG_COUNT];
|
File fragment_shaders[FRAG_COUNT];
|
||||||
|
File common_shader_code;
|
||||||
ShaderProgram program;
|
ShaderProgram program;
|
||||||
Window *window;
|
Window *window;
|
||||||
Timer timer;
|
Timer timer;
|
||||||
Context context;
|
Context context;
|
||||||
int i;
|
|
||||||
char file_path[FRAG_COUNT][1024];
|
|
||||||
|
|
||||||
// TODO extract to function
|
init_files(params.frag_path, &common_shader_code, fragment_shaders);
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
window = init_window(PACKAGE " " VERSION, params.screen, error_callback,
|
window = init_window(PACKAGE " " VERSION, params.screen, error_callback,
|
||||||
key_callback);
|
key_callback);
|
||||||
@@ -93,13 +135,11 @@ void forge_run(Parameters params) {
|
|||||||
timer = create_timer(60);
|
timer = create_timer(60);
|
||||||
|
|
||||||
while (!window_should_close(window)) {
|
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);
|
close_window(window, true);
|
||||||
|
|
||||||
// TODO extract to function
|
free_files(&common_shader_code, fragment_shaders);
|
||||||
for (i = 0; i < FRAG_COUNT; i++) {
|
|
||||||
free_file(&fragment_shaders[i]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
+1
-3
@@ -256,6 +256,4 @@ void apply_program(ShaderProgram program, Context context) {
|
|||||||
// draw output
|
// draw output
|
||||||
glDrawArrays(GL_TRIANGLES, 0, 6);
|
glDrawArrays(GL_TRIANGLES, 0, 6);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO clean buffers from opengl memories
|
|
||||||
@@ -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;
|
||||||
|
}
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
#ifndef STRINGS_H
|
||||||
|
#define STRINGS_H
|
||||||
|
|
||||||
|
char *concat(const char *s1, const char *s2);
|
||||||
|
|
||||||
|
#endif
|
||||||
Reference in New Issue
Block a user