9 Commits

Author SHA1 Message Date
klemek a7bc58e94e refactor: store init params in main program
C-lang CI / build-no-video (push) Failing after 32s
C-lang CI / lint (push) Failing after 36s
C-lang CI / build-release (push) Has been skipped
2026-05-11 08:42:36 +02:00
klemek dfefe879c9 feat: video reconnect cli arg 2026-05-11 08:36:32 +02:00
klemek 7d99c617ef refactor: check every gl error 2026-05-11 08:30:21 +02:00
klemek cdc7df3e56 refactor: review format and imports 2026-05-11 07:56:26 +02:00
klemek 8f0da378b0 fix: forks should exit 2026-05-11 07:40:34 +02:00
klemek 7d03c9719e feat: video reconnect (wip egl error 3003) 2026-05-11 07:40:20 +02:00
klemek dd20515e2b style: format glsl files and fix inc_src 2026-05-11 07:34:47 +02:00
klemek 53c5639f92 docs: small linux dev/video help 2026-05-11 07:28:38 +02:00
klemek d85c5b47b6 tools: add more formatting config 2026-05-11 07:28:08 +02:00
59 changed files with 642 additions and 347 deletions
+4
View File
@@ -0,0 +1,4 @@
# yaml-language-server: $schema=https://json.schemastore.org/clang-format-21.x.json
---
BasedOnStyle: LLVM
IndentWidth: 2
+19
View File
@@ -0,0 +1,19 @@
# EditorConfig is awesome: https://EditorConfig.org
# top-most EditorConfig file
root = true
[*]
indent_style = space
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
[{*.c,*.h}]
indent_size = 2
[{Makefile.*,Makefile}]
indent_style = tab
indent_size = 2
+4 -1
View File
@@ -25,6 +25,10 @@ build:
-o build/$(TARGET) \ -o build/$(TARGET) \
-g -Og -g -Og
.PHONY: format
format:
clang-format -i src/*
.PHONY: run .PHONY: run
run: build run: build
./build/$(TARGET) $(TEST_ARGS) --monitor-only --internal-size=480 --video-size=240 --hot-reload ./build/$(TARGET) $(TEST_ARGS) --monitor-only --internal-size=480 --video-size=240 --hot-reload
@@ -101,4 +105,3 @@ release-arch: clean
mkdir -p build mkdir -p build
cp PKGBUILD build cp PKGBUILD build
cd build && makepkg cd build && makepkg
+10 -2
View File
@@ -175,14 +175,16 @@ These are configurable in the [`forge_project.cfg`](#forge_projectcfg).
### CLI arguments ### CLI arguments
```txt ```txt
usage: forge [-h] [-v] [-p=PROJECT_PATH] [-c=CFG_FILE] [-hr] [-s=SCREEN] [-m=SCREEN] [-mo] [-w] [-t=TEMPO] [-d] [-ar / -nar] [-arc=CYCLES] [-vi=FILE] [-vs=SIZE] [-is=SIZE] [-ls / -nls] [-ss / -nss] [-tm] [-tf] forge steel-dev
usage: forge [-h] [-v] [-p=PROJECT_PATH] [-c=CFG_FILE] [-hr] [-s=SCREEN] [-m=SCREEN] [-mo] [-w] [-t=TEMPO] [-d] [-ar / -nar] [-arc=CYCLES] [-vi=FILE] [-vs=SIZE] [-vr / -nvr] [-is=SIZE] [-ls / -nls] [-ss / -nss] [-tm] [-tf]
Fusion Of Real-time Generative Effects. Fusion Of Real-time Generative Effects.
options: options:
-h, --help show this help message and exit -h, --help show this help message and exit
-v, --version print version -v, --version print version
-p, --project forge project directory (default: /usr/share/forge/default) -p, --project forge project directory (default: ./default)
-c, --config config file name (default: forge_project.cfg) -c, --config config file name (default: forge_project.cfg)
-hr, --hot-reload hot reload of shaders scripts -hr, --hot-reload hot reload of shaders scripts
-s, --screen output screen number (default: primary) -s, --screen output screen number (default: primary)
@@ -196,6 +198,8 @@ options:
-arc, --auto-random-cycle auto random cycle length (default: 4) -arc, --auto-random-cycle auto random cycle length (default: 4)
-vi, --video-in path to video capture device (multiple allowed) -vi, --video-in path to video capture device (multiple allowed)
-vs, --video-size video capture desired height (default: internal texture height) -vs, --video-size video capture desired height (default: internal texture height)
-vr, --video-reconnect auto-reconnect video (default)
-nvr, --no-video-reconnect do not auto-reconnect video
-is, --internal-size internal texture height (default: 720) -is, --internal-size internal texture height (default: 720)
-ls, --load-state load saved state (default) -ls, --load-state load saved state (default)
-nls, --no-load-state do not load saved state -nls, --no-load-state do not load saved state
@@ -490,3 +494,7 @@ The code is available in the default project [default/inc_yuyv.glsl](./default/i
The sentences are defined in [default/inc_sentences.glsl](./default/inc_sentences.glsl). The sentences are defined in [default/inc_sentences.glsl](./default/inc_sentences.glsl).
The script [scripts/sentences.py](./scripts/sentences.py) can help you update them. The script [scripts/sentences.py](./scripts/sentences.py) can help you update them.
### My video device keeps changing ID each time it's unplugged
You should [bind your device to a fixed interface](https://linuxvox.com/blog/linux-bind-camera-to-fixed-devvideo/).
+2 -2
View File
@@ -76,7 +76,7 @@ void main() {
f += rect(uv3, vec2(-35, 28.5), vec2(2.1, 0.7)); f += rect(uv3, vec2(-35, 28.5), vec2(2.1, 0.7));
t += write_5(uv3, vec2(-37, 28), texts[0]); t += write_5(uv3, vec2(-37, 28), texts[0]);
if (iInputResolution1.x > 0) { if (iInputFormat1 == YUYV_FOURCC) {
c += s(uv2, 0, 2) * texture(iTex5, uv2); c += s(uv2, 0, 2) * texture(iTex5, uv2);
f += rect(uv3, vec2(-35, 26.75), vec2(2.8, 0.7)); f += rect(uv3, vec2(-35, 26.75), vec2(2.8, 0.7));
t += write_int(uv3, vec2(-37.6, 26.1), iInputFPS1, 2); t += write_int(uv3, vec2(-37.6, 26.1), iInputFPS1, 2);
@@ -89,7 +89,7 @@ void main() {
f += rect(uv3, vec2(-35, -11.5), vec2(2.1, 0.7)); f += rect(uv3, vec2(-35, -11.5), vec2(2.1, 0.7));
t += write_5(uv3, vec2(-37, -12), texts[1]); t += write_5(uv3, vec2(-37, -12), texts[1]);
if (iInputResolution2.x > 0) { if (iInputFormat2 == YUYV_FOURCC) {
c += s(uv2, 0, 0) * texture(iTex6, uv2); c += s(uv2, 0, 0) * texture(iTex6, uv2);
f += rect(uv3, vec2(-35, -13.25), vec2(2.8, 0.7)); f += rect(uv3, vec2(-35, -13.25), vec2(2.8, 0.7));
t += write_int(uv3, vec2(-37.6, -13.9), iInputFPS2, 2); t += write_int(uv3, vec2(-37.6, -13.9), iInputFPS2, 2);
+4 -4
View File
@@ -8,8 +8,8 @@
uniform int iDemo; uniform int iDemo;
uniform sampler2D iTex0; uniform sampler2D iTex0;
uniform sampler2D iTex3; uniform sampler2D iTex5;
uniform sampler2D iTex4; uniform sampler2D iTex6;
subroutine vec4 src_stage_sub(vec2 vUV, int seed, vec3 b1, vec2 f1, vec3 b2, vec2 f2, vec3 b3, vec2 f3); subroutine vec4 src_stage_sub(vec2 vUV, int seed, vec3 b1, vec2 f1, vec3 b2, vec2 f2, vec3 b3, vec2 f3);
@@ -270,7 +270,7 @@ subroutine(src_stage_sub) vec4 src_6(vec2 vUV, int seed, vec3 b1, vec2 f1, vec3
return src_2(vUV, seed, b1, f1, b2, f2, b3, f3); return src_2(vUV, seed, b1, f1, b2, f2, b3, f3);
} }
return src_thru(vUV, iTex3, seed, b1, f1, b2, f2, b3, f3); return src_thru(vUV, iTex5, seed, b1, f1, b2, f2, b3, f3);
} }
#include inc_cp437.glsl #include inc_cp437.glsl
@@ -434,7 +434,7 @@ subroutine(src_stage_sub) vec4 src_11(vec2 vUV, int seed, vec3 b1, vec2 f1, vec3
return src_3(vUV, seed, b1, f1, b2, f2, b3, f3); return src_3(vUV, seed, b1, f1, b2, f2, b3, f3);
} }
return src_thru(vUV, iTex4, seed, b1, f1, b2, f2, b3, f3); return src_thru(vUV, iTex6, seed, b1, f1, b2, f2, b3, f3);
} }
// SRC 12 : Scales // SRC 12 : Scales
+16 -1
View File
@@ -1,4 +1,3 @@
#include <bsd/string.h>
#include <limits.h> #include <limits.h>
#include <log.h> #include <log.h>
#include <stdbool.h> #include <stdbool.h>
@@ -33,6 +32,7 @@ static void print_help(int status_code) {
#ifdef VIDEO_IN #ifdef VIDEO_IN
"[-vi=FILE] " "[-vi=FILE] "
"[-vs=SIZE] " "[-vs=SIZE] "
"[-vr / -nvr] "
#endif /* VIDEO_IN */ #endif /* VIDEO_IN */
"[-is=SIZE] " "[-is=SIZE] "
"[-ls / -nls] " "[-ls / -nls] "
@@ -64,6 +64,8 @@ static void print_help(int status_code) {
"allowed)\n" "allowed)\n"
" -vs, --video-size video capture desired height (default: " " -vs, --video-size video capture desired height (default: "
"internal texture height)\n" "internal texture height)\n"
" -vr, --video-reconnect auto-reconnect video (default)\n"
" -nvr, --no-video-reconnect do not auto-reconnect video\n"
#endif /* VIDEO_IN */ #endif /* VIDEO_IN */
" -is, --internal-size internal texture height (default: 720)\n" " -is, --internal-size internal texture height (default: 720)\n"
" -ls, --load-state load saved state (default)\n" " -ls, --load-state load saved state (default)\n"
@@ -131,6 +133,7 @@ void args_parse(Parameters *params, int argc, char **argv) {
params->auto_random_cycle = 4; params->auto_random_cycle = 4;
params->video_in.length = 0; params->video_in.length = 0;
params->video_size = 0; params->video_size = 0;
params->video_reconnect = true;
params->internal_size = 720; params->internal_size = 720;
params->load_state = true; params->load_state = true;
params->save_state = true; params->save_state = true;
@@ -196,6 +199,18 @@ void args_parse(Parameters *params, int argc, char **argv) {
} }
#else #else
invalid_arg(arg); invalid_arg(arg);
#endif /* VIDEO_IN */
} else if (is_arg(arg, "-vr") || is_arg(arg, "--video-reconnect")) {
#ifdef VIDEO_IN
params->video_reconnect = true;
#else
invalid_arg(arg);
#endif /* VIDEO_IN */
} else if (is_arg(arg, "-nvr") || is_arg(arg, "--no-video-reconnect")) {
#ifdef VIDEO_IN
params->video_reconnect = false;
#else
invalid_arg(arg);
#endif /* VIDEO_IN */ #endif /* VIDEO_IN */
} else if (is_arg(arg, "-is") || is_arg(arg, "--internal-size")) { } else if (is_arg(arg, "-is") || is_arg(arg, "--internal-size")) {
params->internal_size = parse_uint(arg, value); params->internal_size = parse_uint(arg, value);
-1
View File
@@ -1,4 +1,3 @@
#include <bsd/string.h>
#include <hashmap.h> #include <hashmap.h>
#include <log.h> #include <log.h>
#include <stdlib.h> #include <stdlib.h>
-2
View File
@@ -1,4 +1,3 @@
#include <bsd/string.h>
#include <log.h> #include <log.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdio.h> #include <stdio.h>
@@ -10,7 +9,6 @@
#include "types.h" #include "types.h"
#include "file.h" #include "file.h"
#include "string.h"
static time_t get_file_time(const File *file) { static time_t get_file_time(const File *file) {
struct stat attr; struct stat attr;
+100 -40
View File
@@ -4,13 +4,12 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/wait.h> #include <unistd.h>
#include "types.h" #include "types.h"
#include "config.h" #include "config.h"
#include "config_file.h" #include "config_file.h"
#include "file.h"
#include "forge.h" #include "forge.h"
#include "midi.h" #include "midi.h"
#include "project.h" #include "project.h"
@@ -22,24 +21,24 @@
#include "video.h" #include "video.h"
#include "window.h" #include "window.h"
static Parameters init_params;
static SharedContext *context; static SharedContext *context;
static ShaderProgram program; static ShaderProgram program;
static Window *window_output; static Window *window_output;
static Window *window_monitor; static Window *window_monitor;
static VideoCaptureArray inputs;
static Timer timer; static Timer timer;
static MidiDevice midi; static MidiDevice midi;
static bool trace_midi; static bool trace_midi;
static Project project; static Project project;
static void compute_fps(bool trace_fps) { static void compute_fps() {
double fps; double fps;
char title[STR_LEN]; char title[STR_LEN];
if (timer_inc(&timer)) { if (timer_inc(&timer)) {
fps = timer_reset(&timer); fps = timer_reset(&timer);
if (trace_fps) { if (init_params.trace_fps) {
log_trace("(main) %.2ffps", fps); log_trace("(main) %.2ffps", fps);
} }
@@ -57,18 +56,16 @@ static void compute_fps(bool trace_fps) {
} }
} }
static void init_context(const Parameters *params) { static void init_context() {
context = shared_init_context("/" PACKAGE "_context"); context = shared_init_context("/" PACKAGE "_context");
context->stop = false; context->stop = false;
state_init(context, &project.state_config, params->demo, params->auto_random, state_init(context, &project.state_config, init_params.demo,
params->auto_random_cycle, params->base_tempo, params->load_state); init_params.auto_random, init_params.auto_random_cycle,
init_params.base_tempo, init_params.load_state);
memset(context->input_resolutions, 0, sizeof(context->input_resolutions)); memset(context->input_resolutions, 0, sizeof(context->input_resolutions));
memset(context->input_formats, 0, sizeof(context->input_formats));
memset(context->input_fps, 0, sizeof(context->input_fps));
memset(context->input_swap, 0, sizeof(context->input_swap));
} }
static void free_context() { shared_close_context(context); } static void free_context() { shared_close_context(context); }
@@ -78,37 +75,83 @@ static void reload_shader(unsigned int i) {
} }
#ifdef VIDEO_IN #ifdef VIDEO_IN
static void init_inputs(const StringArray *video_in, unsigned int video_size) { static void init_inputs() {
inputs.length = video_in->length; context->inputs.length = init_params.video_in.length;
for (unsigned int i = 0; i < video_in->length; i++) { for (unsigned int i = 0; i < init_params.video_in.length; i++) {
video_init(&inputs.values[i], video_in->values[i], video_size); video_init(&context->inputs.values[i], init_params.video_in.values[i],
init_params.video_size);
if (!inputs.values[i].error) { if (!context->inputs.values[i].error) {
context->input_resolutions[i][0] = inputs.values[i].width; context->input_resolutions[i][0] = context->inputs.values[i].width;
context->input_resolutions[i][1] = inputs.values[i].height; context->input_resolutions[i][1] = context->inputs.values[i].height;
context->input_formats[i] = inputs.values[i].pixelformat; context->inputs.values[i].needs_reload = false;
context->inputs.values[i].disconnected = false;
} }
} }
} }
static bool start_video_captures(unsigned int video_count, bool trace_fps) { static bool reconnect_video_captures() {
for (unsigned int i = 0; i < video_count; i++) { for (unsigned int i = 0; i < init_params.video_in.length; i++) {
if (!inputs.values[i].error && if (context->inputs.values[i].disconnected) {
!video_background_read(&inputs.values[i], context, i, trace_fps)) { video_init(&context->inputs.values[i], init_params.video_in.values[i],
init_params.video_size);
if (!context->inputs.values[i].error) {
context->input_resolutions[i][0] = context->inputs.values[i].width;
context->input_resolutions[i][1] = context->inputs.values[i].height;
if (!video_background_read(&context->inputs.values[i], context, i,
init_params.trace_fps)) {
return false; return false;
} }
context->inputs.values[i].needs_reload = true;
context->inputs.values[i].disconnected = false;
}
}
} }
return true; return true;
} }
static void free_video_captures(unsigned int video_count) { static bool start_video_captures() {
for (unsigned int i = 0; i < video_count; i++) { pid_t pid;
shaders_free_input(&program, &inputs.values[i]); for (unsigned int i = 0; i < context->inputs.length; i++) {
if (!context->inputs.values[i].error &&
!video_background_read(&context->inputs.values[i], context, i,
init_params.trace_fps)) {
return false;
}
}
if (!init_params.video_reconnect) {
return true;
}
pid = fork();
if (pid < 0) {
log_error("Could not create subprocess");
return false;
}
if (pid == 0) {
return true;
}
log_info("background reconnect acquisition started (pid: %d)", pid);
while (!context->stop) {
sleep(1);
if (!reconnect_video_captures()) {
return false;
}
}
exit(EXIT_SUCCESS);
}
video_free(&inputs.values[i]); static void free_video_captures() {
for (unsigned int i = 0; i < context->inputs.length; i++) {
shaders_free_input(&program, i);
video_free(&context->inputs.values[i]);
} }
} }
#endif /* VIDEO_IN */ #endif /* VIDEO_IN */
static void error_callback(int error, const char *description) { static void error_callback(int error, const char *description) {
@@ -140,18 +183,20 @@ static void midi_callback(unsigned char code, unsigned char value) {
} }
static bool init(const Parameters *params) { static bool init(const Parameters *params) {
init_params = *params;
project_init(&project, params->project_path, params->config_file); project_init(&project, params->project_path, params->config_file);
if (project.error) { if (project.error) {
return false; return false;
} }
init_context(params); init_context();
#ifdef VIDEO_IN #ifdef VIDEO_IN
init_inputs(&params->video_in, params->video_size); init_inputs();
if (!start_video_captures(params->video_in.length, params->trace_fps)) { if (!start_video_captures()) {
return false; return false;
} }
#endif /* VIDEO_IN */ #endif /* VIDEO_IN */
@@ -200,7 +245,7 @@ static bool init(const Parameters *params) {
} }
#ifdef VIDEO_IN #ifdef VIDEO_IN
shaders_link_inputs(&program, &project, &inputs); shaders_link_inputs(&program, &project, &context->inputs);
#endif /* VIDEO_IN */ #endif /* VIDEO_IN */
if (program.error) { if (program.error) {
@@ -221,12 +266,23 @@ static bool should_close() {
(window_monitor != NULL && window_should_close(window_monitor)); (window_monitor != NULL && window_should_close(window_monitor));
} }
static void loop(bool hr, bool trace_fps) { static bool loop() {
if (hr) { if (init_params.hot_reload) {
project_reload(&project, reload_shader); project_reload(&project, reload_shader);
} }
compute_fps(trace_fps); #ifdef VIDEO_IN
if (init_params.video_reconnect) {
for (unsigned int i = 0; i < context->inputs.length; i++) {
if (context->inputs.values[i].needs_reload) {
shaders_relink_input(&program, &project, &context->inputs, i);
context->inputs.values[i].needs_reload = false;
}
}
}
#endif /* VIDEO_IN */
compute_fps();
context->time = window_get_time(); context->time = window_get_time();
context->tempo_total = (float)tempo_total(&context->tempo); context->tempo_total = (float)tempo_total(&context->tempo);
@@ -248,12 +304,14 @@ static void loop(bool hr, bool trace_fps) {
} }
window_events(); window_events();
return true;
} }
static void shutdown(const Parameters *params) { static void shutdown() {
context->stop = true; context->stop = true;
if (params->save_state) { if (init_params.save_state) {
state_save(context, &project.state_config); state_save(context, &project.state_config);
} }
@@ -268,11 +326,11 @@ static void shutdown(const Parameters *params) {
if (window_monitor != NULL) { if (window_monitor != NULL) {
window_use(window_monitor, context); window_use(window_monitor, context);
shaders_free_window(&program, params->output); shaders_free_window(&program, init_params.output);
} }
#ifdef VIDEO_IN #ifdef VIDEO_IN
free_video_captures(params->video_in.length); free_video_captures();
#endif /* VIDEO_IN */ #endif /* VIDEO_IN */
free_context(); free_context();
@@ -288,8 +346,10 @@ void forge_run(const Parameters *params) {
} }
while (!should_close()) { while (!should_close()) {
loop(params->hot_reload, params->trace_fps); if (!loop()) {
return;
}
} }
shutdown(params); shutdown();
} }
+1 -1
View File
@@ -17,7 +17,7 @@ int main(int argc, char **argv) {
puts(PACKAGE " " VERSION); puts(PACKAGE " " VERSION);
set_seed((unsigned long)time(NULL)); rand_set_seed((unsigned long)time(NULL));
forge_run(&params); forge_run(&params);
+3 -4
View File
@@ -1,11 +1,10 @@
#include <GLFW/glfw3.h>
#include <alsa/asoundlib.h> #include <alsa/asoundlib.h>
#include <bsd/string.h> #include <log.h>
#include <stdlib.h>
#include "types.h" #include "types.h"
#include "config.h" #include "config.h"
#include "log.h"
void midi_open(MidiDevice *device, const char *name) { void midi_open(MidiDevice *device, const char *name) {
strlcpy(device->name, name, STR_LEN); strlcpy(device->name, name, STR_LEN);
@@ -61,5 +60,5 @@ bool midi_background_listen(const MidiDevice *device,
log_info("(%s) background acquisition stopped by main thread (pid: %d)", log_info("(%s) background acquisition stopped by main thread (pid: %d)",
device->name, pid); device->name, pid);
return false; exit(EXIT_SUCCESS);
} }
+1 -2
View File
@@ -1,4 +1,4 @@
#include <bsd/string.h> #include <log.h>
#include <string.h> #include <string.h>
#include "types.h" #include "types.h"
@@ -6,7 +6,6 @@
#include "config.h" #include "config.h"
#include "config_file.h" #include "config_file.h"
#include "file.h" #include "file.h"
#include "log.h"
#include "project.h" #include "project.h"
#include "state.h" #include "state.h"
#include "string.h" #include "string.h"
+1 -1
View File
@@ -13,7 +13,7 @@ static unsigned long rand(void) {
return (unsigned long)(x >> (22 + count)); return (unsigned long)(x >> (22 + count));
} }
void set_seed(unsigned long long seed) { void rand_set_seed(unsigned long long seed) {
mcg_state = 2 * seed + 1; mcg_state = 2 * seed + 1;
(void)rand(); (void)rand();
} }
+1 -1
View File
@@ -1,7 +1,7 @@
#ifndef RAND_H #ifndef RAND_H
#define RAND_H #define RAND_H
void set_seed(unsigned long long seed); void rand_set_seed(unsigned long long seed);
unsigned int rand_uint(unsigned int max); unsigned int rand_uint(unsigned int max);
#endif #endif
+281 -99
View File
@@ -26,20 +26,49 @@
static const GLuint unused_uniform = (GLuint)-1; static const GLuint unused_uniform = (GLuint)-1;
bool check_glerror(ShaderProgram *program) { bool check_glerror_ro(const char *context) {
unsigned int code; unsigned int code;
code = glGetError(); code = glGetError();
if (code > 0) { if (code > 0) {
log_warn("GL Error: %04x", code); log_warn("GL Error: %04x (%s)", code, context);
return true;
}
return false;
}
bool check_glerror(ShaderProgram *program, const char *context) {
if (check_glerror_ro(context)) {
program->error = true; program->error = true;
return true;
}
return false;
} }
return code > 0; bool check_eglerror_ro(const char *context) {
unsigned int code;
code = eglGetError();
if (code > 0 && code != EGL_SUCCESS) {
log_warn("EGL Error: %04x (%s)", code, context);
return true;
} }
static void init_gl(ShaderProgram *program) { return false;
}
bool check_eglerror(ShaderProgram *program, const char *context) {
if (check_eglerror_ro(context)) {
program->error = true;
return true;
}
return false;
}
static bool init_gl(ShaderProgram *program) {
gladLoadGL(glfwGetProcAddress); gladLoadGL(glfwGetProcAddress);
#ifdef VIDEO_IN #ifdef VIDEO_IN
@@ -47,48 +76,88 @@ static void init_gl(ShaderProgram *program) {
if (program->egl_display == EGL_NO_DISPLAY) { if (program->egl_display == EGL_NO_DISPLAY) {
log_error("error: glfwGetEGLDisplay no EGLDisplay returned"); log_error("error: glfwGetEGLDisplay no EGLDisplay returned");
program->error = true; program->error = true;
return; return false;
} }
gladLoadEGL(program->egl_display, glfwGetProcAddress); gladLoadEGL(program->egl_display, glfwGetProcAddress);
#endif /* VIDEO_IN */ #endif /* VIDEO_IN */
return !check_glerror(program, "init_gl") &&
!check_eglerror(program, "init_gl");
} }
static void init_textures(ShaderProgram *program, static bool init_textures(ShaderProgram *program,
const SharedContext *context) { const SharedContext *context) {
glGenTextures(program->tex_count, program->textures); glGenTextures(program->tex_count, program->textures);
if (check_glerror(program, "init_textures/glGenTextures")) {
return false;
}
for (unsigned int i = 0; i < program->tex_count; i++) { for (unsigned int i = 0; i < program->tex_count; i++) {
// selects which texture unit subsequent texture state calls will affect // selects which texture unit subsequent texture state calls will affect
glActiveTexture(GL_TEXTURE0 + i); glActiveTexture(GL_TEXTURE0 + i);
if (check_glerror(program, "init_textures/glActiveTexture")) {
return false;
}
glBindTexture(GL_TEXTURE_2D, program->textures[i]); glBindTexture(GL_TEXTURE_2D, program->textures[i]);
if (check_glerror(program, "init_textures/glBindTexture")) {
return false;
}
glDisable(GL_DEPTH_TEST); glDisable(GL_DEPTH_TEST);
glDisable(GL_BLEND); glDisable(GL_BLEND);
if (check_glerror(program, "init_textures/glDisable")) {
return false;
}
// define texture image as empty // define texture image as empty
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, context->tex_resolution[0], glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, context->tex_resolution[0],
context->tex_resolution[1], 0, GL_RGB, GL_UNSIGNED_BYTE, 0); context->tex_resolution[1], 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
if (check_glerror(program, "init_textures/glTexImage2D")) {
return false;
}
// setup mipmap context // setup mipmap context
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
if (check_glerror(program, "init_textures/glTexParameteri")) {
return false;
}
log_info("Texture %d initialized", i); log_info("Texture %d initialized", i);
} }
return true;
} }
static void rebind_textures(const ShaderProgram *program) { static void rebind_textures(const ShaderProgram *program) {
for (unsigned int i = 0; i < program->tex_count; i++) { for (unsigned int i = 0; i < program->tex_count; i++) {
glActiveTexture(GL_TEXTURE0 + i); glActiveTexture(GL_TEXTURE0 + i);
check_glerror_ro("rebind_textures/glActiveTexture");
glBindTexture(GL_TEXTURE_2D, program->textures[i]); glBindTexture(GL_TEXTURE_2D, program->textures[i]);
check_glerror_ro("rebind_textures/glBindTexture");
} }
} }
#ifdef VIDEO_IN #ifdef VIDEO_IN
static void link_input_to_texture(ShaderProgram *program, VideoCapture *input, static bool link_input_to_texture(ShaderProgram *program, VideoCapture *input,
unsigned int texture_index, bool swap) { unsigned int input_index,
unsigned int texture_index, bool swap,
bool reload) {
if (reload) {
glDeleteTextures(1, program->textures + texture_index);
if (check_glerror(program, "link_input_to_texture/glDeleteTextures")) {
return false;
}
glGenTextures(1, program->textures + texture_index);
if (check_glerror(program, "link_input_to_texture/glGenTextures")) {
return false;
}
}
EGLImageKHR dma_image; EGLImageKHR dma_image;
dma_image = EGL_NO_IMAGE_KHR; dma_image = EGL_NO_IMAGE_KHR;
@@ -110,59 +179,81 @@ static void link_input_to_texture(ShaderProgram *program, VideoCapture *input,
dma_image = eglCreateImageKHR(program->egl_display, EGL_NO_CONTEXT, dma_image = eglCreateImageKHR(program->egl_display, EGL_NO_CONTEXT,
EGL_LINUX_DMA_BUF_EXT, NULL, attrib_list); EGL_LINUX_DMA_BUF_EXT, NULL, attrib_list);
if (check_eglerror(program, "link_input_to_texture/eglCreateImageKHR")) {
if (swap) { return false;
input->dma_image_swap = dma_image;
} else {
input->dma_image = dma_image;
} }
if (dma_image == EGL_NO_IMAGE_KHR) { if (swap) {
log_error("(%s) eglCreateImageKHR failed %04x", input->name, eglGetError()); program->dma_images_swap[input_index] = dma_image;
return; } else {
program->dma_images[input_index] = dma_image;
} }
glActiveTexture(GL_TEXTURE0 + texture_index); glActiveTexture(GL_TEXTURE0 + texture_index);
if (check_glerror(program, "link_input_to_texture/glActiveTexture")) {
return false;
}
glBindTexture(GL_TEXTURE_2D, program->textures[texture_index]); glBindTexture(GL_TEXTURE_2D, program->textures[texture_index]);
if (check_glerror(program, "link_input_to_texture/glBindTexture")) {
return false;
}
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, input->width, input->height, 0, GL_RGB, glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, input->width, input->height, 0, GL_RGB,
GL_UNSIGNED_BYTE, 0); GL_UNSIGNED_BYTE, 0);
if (check_glerror(program, "link_input_to_texture/glTexImage2D")) {
// https://registry.khronos.org/OpenGL/extensions/EXT/EXT_EGL_image_storage.txt return false;
glEGLImageTargetTexStorageEXT(GL_TEXTURE_2D, input->dma_image, NULL);
log_info("Texture %d linked to %s", texture_index, input->name);
} }
static void init_input(ShaderProgram *program, const ConfigFile *config, // https://registry.khronos.org/OpenGL/extensions/EXT/EXT_EGL_image_storage.txt
VideoCaptureArray *inputs) { glEGLImageTargetTexStorageEXT(GL_TEXTURE_2D, dma_image, NULL);
if (check_eglerror(program,
"link_input_to_texture/glEGLImageTargetTexStorageEXT")) {
return false;
}
log_info("Texture %d linked to %s", texture_index, input->name);
return true;
}
static bool init_input(ShaderProgram *program, const ConfigFile *config,
VideoCaptureArray *inputs, unsigned int i, bool reload) {
unsigned int tex_i; unsigned int tex_i;
char name[STR_LEN]; char name[STR_LEN];
for (unsigned int i = 0; i < program->in_count; i++) {
if (i < inputs->length && !inputs->values[i].error) { if (i < inputs->length && !inputs->values[i].error) {
snprintf(name, STR_LEN, "IN_%d_OUT", i + 1); snprintf(name, STR_LEN, "IN_%d_OUT", i + 1);
tex_i = config_file_get_int(config, name, 0); tex_i = config_file_get_int(config, name, 0);
link_input_to_texture(program, &inputs->values[i], tex_i, false); if (!link_input_to_texture(program, &inputs->values[i], i, tex_i, false,
reload)) {
return false;
}
if (inputs->values[i].with_swap) { if (inputs->values[i].with_swap) {
snprintf(name, STR_LEN, "IN_%d_SWAP_OUT", i + 1); snprintf(name, STR_LEN, "IN_%d_SWAP_OUT", i + 1);
tex_i = config_file_get_int(config, name, 0); tex_i = config_file_get_int(config, name, 0);
link_input_to_texture(program, &inputs->values[i], tex_i, true); if (!link_input_to_texture(program, &inputs->values[i], i, tex_i, true,
reload)) {
return false;
}
} }
} else { } else {
log_warn("Cannot link input %d", i + 1); log_warn("Cannot link input %d", i + 1);
} }
} return true;
} }
#endif /* VIDEO_IN */ #endif /* VIDEO_IN */
static void init_framebuffers(ShaderProgram *program, static bool init_framebuffers(ShaderProgram *program,
const ConfigFile *config) { const ConfigFile *config) {
unsigned int tex_i; unsigned int tex_i;
char name[STR_LEN]; char name[STR_LEN];
GLenum framebuffer_status;
glGenFramebuffers(program->frag_count, program->frame_buffers); glGenFramebuffers(program->frag_count, program->frame_buffers);
if (check_glerror(program, "init_framebuffers/glGenFramebuffers")) {
return false;
}
for (unsigned int i = 0; i < program->frag_count; i++) { for (unsigned int i = 0; i < program->frag_count; i++) {
if (i == program->frag_output_index || i == program->frag_monitor_index) { if (i == program->frag_output_index || i == program->frag_monitor_index) {
@@ -170,50 +261,89 @@ static void init_framebuffers(ShaderProgram *program,
} }
glBindFramebuffer(GL_FRAMEBUFFER, program->frame_buffers[i]); glBindFramebuffer(GL_FRAMEBUFFER, program->frame_buffers[i]);
if (check_glerror(program, "init_framebuffers/glBindFramebuffer")) {
return false;
}
snprintf(name, STR_LEN, "FRAG_%d_OUT", i + 1); snprintf(name, STR_LEN, "FRAG_%d_OUT", i + 1);
tex_i = config_file_get_int(config, name, 0); tex_i = config_file_get_int(config, name, 0);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
program->textures[tex_i], 0); program->textures[tex_i], 0);
if (check_glerror(program, "init_framebuffers/glFramebufferTexture2D")) {
return false;
}
// check framebuffer status // check framebuffer status
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) { framebuffer_status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
log_error("Framebuffer %d is KO: %x", i + 1, if (check_glerror(program, "init_framebuffers/glCheckFramebufferStatus")) {
glCheckFramebufferStatus(GL_FRAMEBUFFER)); return false;
}
if (framebuffer_status != GL_FRAMEBUFFER_COMPLETE) {
log_error("Framebuffer %d is KO: %x", i + 1, framebuffer_status);
program->error = true; program->error = true;
return false;
return;
} }
log_info("Framebuffer %d initialized", i); log_info("Framebuffer %d initialized", i);
} }
return; return true;
} }
static void init_vertices(ShaderProgram *program) { static bool init_vertices(ShaderProgram *program) {
glGenBuffers(1, &program->vertex_buffer); glGenBuffers(1, &program->vertex_buffer);
glBindBuffer(GL_ARRAY_BUFFER, program->vertex_buffer); if (check_glerror(program, "init_vertices/glGenBuffers")) {
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW); return false;
} }
static void bind_vertices(ShaderProgram *program, unsigned int index) {
glBindBuffer(GL_ARRAY_BUFFER, program->vertex_buffer); glBindBuffer(GL_ARRAY_BUFFER, program->vertex_buffer);
if (check_glerror(program, "init_vertices/glBindBuffer")) {
return false;
}
glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);
if (check_glerror(program, "init_vertices/glBufferData")) {
return false;
}
return true;
}
static bool bind_vertices(ShaderProgram *program, unsigned int index) {
glBindBuffer(GL_ARRAY_BUFFER, program->vertex_buffer);
if (check_glerror(program, "bind_vertices/glBindBuffer")) {
return false;
}
glGenVertexArrays(1, &program->vertex_array[index]); glGenVertexArrays(1, &program->vertex_array[index]);
if (check_glerror(program, "bind_vertices/glGenVertexArrays")) {
return false;
}
glBindVertexArray(program->vertex_array[index]); glBindVertexArray(program->vertex_array[index]);
if (check_glerror(program, "bind_vertices/glBindVertexArray")) {
return false;
}
for (unsigned int i = 0; i < program->frag_count; i++) { for (unsigned int i = 0; i < program->frag_count; i++) {
// enable attribute pointer // enable attribute pointer
glEnableVertexAttribArray(program->vpos_locations[i]); glEnableVertexAttribArray(program->vpos_locations[i]);
if (check_glerror(program, "bind_vertices/glEnableVertexAttribArray")) {
return false;
}
// specify the location and data format of the array of generic vertex // specify the location and data format of the array of generic vertex
// attributes to use when rendering // attributes to use when rendering
glVertexAttribPointer(program->vpos_locations[i], 2, GL_FLOAT, GL_FALSE, glVertexAttribPointer(program->vpos_locations[i], 2, GL_FLOAT, GL_FALSE,
sizeof(Vertex), (void *)offsetof(Vertex, pos)); sizeof(Vertex), (void *)offsetof(Vertex, pos));
if (check_glerror(program, "bind_vertices/glVertexAttribPointer")) {
return false;
} }
} }
return true;
}
static bool compile_shader(GLuint shader_id, const char *name, static bool compile_shader(GLuint shader_id, const char *name,
const char *source_code) { const char *source_code) {
GLint status_params; GLint status_params;
@@ -223,13 +353,18 @@ static bool compile_shader(GLuint shader_id, const char *name,
// update shader source code // update shader source code
glShaderSource(shader_id, 1, &source_code, NULL); glShaderSource(shader_id, 1, &source_code, NULL);
check_glerror_ro("compile_shader/glShaderSource");
// compile shader // compile shader
glCompileShader(shader_id); glCompileShader(shader_id);
check_glerror_ro("compile_shader/glCompileShader");
// get compilation status // get compilation status
glGetShaderiv(shader_id, GL_COMPILE_STATUS, &status_params); glGetShaderiv(shader_id, GL_COMPILE_STATUS, &status_params);
check_glerror_ro("compile_shader/glGetShaderiv");
glGetShaderInfoLog(shader_id, 1024, NULL, (GLchar *)&log); glGetShaderInfoLog(shader_id, 1024, NULL, (GLchar *)&log);
check_glerror_ro("compile_shader/glGetShaderInfoLog");
if (status_params == GL_FALSE) { if (status_params == GL_FALSE) {
log_error("Failed to compile\n%s", log); log_error("Failed to compile\n%s", log);
@@ -241,9 +376,13 @@ static bool compile_shader(GLuint shader_id, const char *name,
return status_params == GL_TRUE; return status_params == GL_TRUE;
} }
static void init_shaders(ShaderProgram *program, const Project *project) { static bool init_shaders(ShaderProgram *program, const Project *project) {
// compile vertex shader // compile vertex shader
program->vertex_shader = glCreateShader(GL_VERTEX_SHADER); program->vertex_shader = glCreateShader(GL_VERTEX_SHADER);
if (check_glerror(program, "init_shaders/glCreateShader")) {
return false;
}
program->error = program->error || !compile_shader(program->vertex_shader, program->error = program->error || !compile_shader(program->vertex_shader,
"internal vertex shader", "internal vertex shader",
vertex_shader_text); vertex_shader_text);
@@ -251,18 +390,24 @@ static void init_shaders(ShaderProgram *program, const Project *project) {
// compile fragment shaders // compile fragment shaders
for (unsigned int i = 0; i < program->frag_count; i++) { for (unsigned int i = 0; i < program->frag_count; i++) {
program->fragment_shaders[i] = glCreateShader(GL_FRAGMENT_SHADER); program->fragment_shaders[i] = glCreateShader(GL_FRAGMENT_SHADER);
if (check_glerror(program, "init_shaders/glCreateShader")) {
return false;
}
program->error = program->error || program->error = program->error ||
!compile_shader(program->fragment_shaders[i], !compile_shader(program->fragment_shaders[i],
project->fragment_shaders[i][0].path, project->fragment_shaders[i][0].path,
project->fragment_shaders[i][0].content); project->fragment_shaders[i][0].content);
if (program->error) { if (program->error) {
return; return false;
}
} }
} }
static void init_single_program(ShaderProgram *program, unsigned int i, return true;
}
static bool init_single_program(ShaderProgram *program, unsigned int i,
const ConfigFile *config, const ConfigFile *config,
const StateConfig *state_config) { const StateConfig *state_config) {
unsigned int index1; unsigned int index1;
@@ -271,10 +416,20 @@ static void init_single_program(ShaderProgram *program, unsigned int i,
const char *prefix; const char *prefix;
program->programs[i] = glCreateProgram(); program->programs[i] = glCreateProgram();
if (check_glerror(program, "init_single_program/glCreateProgram")) {
return false;
}
glAttachShader(program->programs[i], program->vertex_shader); glAttachShader(program->programs[i], program->vertex_shader);
glAttachShader(program->programs[i], program->fragment_shaders[i]); glAttachShader(program->programs[i], program->fragment_shaders[i]);
if (check_glerror(program, "init_single_program/glAttachShader")) {
return false;
}
glLinkProgram(program->programs[i]); glLinkProgram(program->programs[i]);
if (check_glerror(program, "init_single_program/glLinkProgram")) {
return false;
}
// create uniforms pointers // create uniforms pointers
program->itime_locations[i] = glGetUniformLocation( program->itime_locations[i] = glGetUniformLocation(
@@ -354,18 +509,6 @@ static void init_single_program(ShaderProgram *program, unsigned int i,
glGetUniformLocation(program->programs[i], name); glGetUniformLocation(program->programs[i], name);
} }
for (unsigned int j = 0; j < program->sub_type_count; j++) {
snprintf(name, STR_LEN, "SUB_%d_PREFIX", j + 1);
prefix = config_file_get_str(config, name, 0);
for (unsigned int k = 0; k < program->sub_variant_count; k++) {
snprintf(name, STR_LEN, "%s%d", prefix, k + 1);
program->sub_locations[i * program->sub_variant_count *
program->sub_type_count +
j * program->sub_variant_count + k] =
glGetSubroutineIndex(program->programs[i], GL_FRAGMENT_SHADER, name);
}
}
prefix = config_file_get_str(config, "UNIFORM_ACTIVE_PREFIX", "iActive"); prefix = config_file_get_str(config, "UNIFORM_ACTIVE_PREFIX", "iActive");
for (unsigned int j = 0; j < program->active_count; j++) { for (unsigned int j = 0; j < program->active_count; j++) {
snprintf(name, STR_LEN, "%s%d", prefix, j + 1); snprintf(name, STR_LEN, "%s%d", prefix, j + 1);
@@ -404,20 +547,50 @@ static void init_single_program(ShaderProgram *program, unsigned int i,
glGetUniformLocation(program->programs[i], name); glGetUniformLocation(program->programs[i], name);
} }
if (check_glerror(program, "init_single_program/glGetUniformLocation")) {
return false;
}
for (unsigned int j = 0; j < program->sub_type_count; j++) {
snprintf(name, STR_LEN, "SUB_%d_PREFIX", j + 1);
prefix = config_file_get_str(config, name, 0);
for (unsigned int k = 0; k < program->sub_variant_count; k++) {
snprintf(name, STR_LEN, "%s%d", prefix, k + 1);
program->sub_locations[i * program->sub_variant_count *
program->sub_type_count +
j * program->sub_variant_count + k] =
glGetSubroutineIndex(program->programs[i], GL_FRAGMENT_SHADER, name);
}
}
if (check_glerror(program, "init_single_program/glGetSubroutineIndex")) {
return false;
}
// create attribute pointer // create attribute pointer
program->vpos_locations[i] = program->vpos_locations[i] =
glGetAttribLocation(program->programs[i], "vPos"); glGetAttribLocation(program->programs[i], "vPos");
log_info("Program %d initialized", i + 1); if (check_glerror(program, "init_single_program/glGetAttribLocation")) {
return false;
} }
static void init_programs(ShaderProgram *program, const ConfigFile *config, log_info("Program %d initialized", i + 1);
return true;
}
static bool init_programs(ShaderProgram *program, const ConfigFile *config,
const StateConfig *state_config) { const StateConfig *state_config) {
for (unsigned int i = 0; i < program->frag_count; i++) { for (unsigned int i = 0; i < program->frag_count; i++) {
init_single_program(program, i, config, state_config); if (!init_single_program(program, i, config, state_config)) {
return false;
} }
} }
return true;
}
static void update_viewport(ShaderProgram *program, static void update_viewport(ShaderProgram *program,
const SharedContext *context) { const SharedContext *context) {
// viewport changed // viewport changed
@@ -426,8 +599,12 @@ static void update_viewport(ShaderProgram *program,
// clean and resize all textures // clean and resize all textures
for (unsigned int i = 0; i < program->tex_count; i++) { for (unsigned int i = 0; i < program->tex_count; i++) {
glActiveTexture(GL_TEXTURE0 + i); glActiveTexture(GL_TEXTURE0 + i);
check_glerror_ro("update_viewport/glActiveTexture");
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, context->tex_resolution[0], glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, context->tex_resolution[0],
context->tex_resolution[1], 0, GL_RGB, GL_UNSIGNED_BYTE, 0); context->tex_resolution[1], 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
check_glerror_ro(
"update_viewport/glTexImage2D"); // TODO dont remap input textures
} }
program->last_resolution[0] = context->resolution[0]; program->last_resolution[0] = context->resolution[0];
program->last_resolution[1] = context->resolution[1]; program->last_resolution[1] = context->resolution[1];
@@ -507,11 +684,11 @@ static void use_program(const ShaderProgram *program, int i, bool output,
write_uniform_2f(program->iinres_locations[i * program->in_count + j], write_uniform_2f(program->iinres_locations[i * program->in_count + j],
&context->input_resolutions[j]); &context->input_resolutions[j]);
write_uniform_1i(program->iinfmt_locations[i * program->in_count + j], write_uniform_1i(program->iinfmt_locations[i * program->in_count + j],
context->input_formats[j]); context->inputs.values[j].pixelformat);
write_uniform_1i(program->iinfps_locations[i * program->in_count + j], write_uniform_1i(program->iinfps_locations[i * program->in_count + j],
context->input_fps[j]); context->inputs.values[j].fps);
write_uniform_1i(program->iinswap_locations[i * program->in_count + j], write_uniform_1i(program->iinswap_locations[i * program->in_count + j],
context->input_swap[j] ? 1 : 0); context->inputs.values[j].swap ? 1 : 0);
} }
// set seeds uniforms // set seeds uniforms
@@ -581,71 +758,64 @@ void shaders_init(ShaderProgram *program, const Project *project,
program->active_count = project->state_config.midi_active_counts.length; program->active_count = project->state_config.midi_active_counts.length;
program->midi_lengths.length = 0; program->midi_lengths.length = 0;
init_gl(program); #ifdef VIDEO_IN
memset(program->dma_images, 0, sizeof(program->dma_images));
memset(program->dma_images_swap, 0, sizeof(program->dma_images_swap));
#endif /* VIDEO_IN */
if (check_glerror(program)) { if (!init_gl(program)) {
return; return;
} }
init_shaders(program, project); if (!init_shaders(program, project)) {
if (program->error || check_glerror(program)) {
return; return;
} }
init_textures(program, context); if (!init_textures(program, context)) {
if (check_glerror(program)) {
return; return;
} }
init_framebuffers(program, &project->config); if (!init_framebuffers(program, &project->config)) {
if (check_glerror(program)) {
return; return;
} }
init_programs(program, &project->config, &project->state_config); if (!init_programs(program, &project->config, &project->state_config)) {
if (check_glerror(program)) {
return; return;
} }
init_vertices(program); if (!init_vertices(program)) {
if (check_glerror(program)) {
return; return;
} }
} }
bind_vertices(program, rebind ? 1 : 0); if (!bind_vertices(program, rebind ? 1 : 0)) {
if (check_glerror(program)) {
return; return;
} }
} }
void shaders_relink_input(ShaderProgram *program, const Project *project,
VideoCaptureArray *inputs, unsigned int i) {
#ifdef VIDEO_IN
// shaders_free_input(program, i);
init_input(program, &project->config, inputs, i, true);
#endif /* VIDEO_IN */
}
void shaders_link_inputs(ShaderProgram *program, const Project *project, void shaders_link_inputs(ShaderProgram *program, const Project *project,
VideoCaptureArray *inputs) { VideoCaptureArray *inputs) {
#ifdef VIDEO_IN #ifdef VIDEO_IN
init_input(program, &project->config, inputs); for (unsigned int i = 0; i < program->in_count; i++) {
init_input(program, &project->config, inputs, i, false);
if (check_glerror(program)) {
return;
} }
#endif /* VIDEO_IN */ #endif /* VIDEO_IN */
} }
void shaders_update(ShaderProgram *program, const File *fragment_shader, void shaders_update(ShaderProgram *program, const File *fragment_shader,
unsigned int i, const Project *project) { unsigned int i, const Project *project) {
bool result; if (compile_shader(program->fragment_shaders[i], fragment_shader->path,
fragment_shader->content) &&
result = compile_shader(program->fragment_shaders[i], fragment_shader->path, init_single_program(program, i, &project->config,
fragment_shader->content); &project->state_config)) {
if (result) {
init_single_program(program, i, &project->config, &project->state_config);
log_info("Program %d updated", i + 1); log_info("Program %d updated", i + 1);
} }
} }
@@ -672,30 +842,42 @@ void shaders_compute(ShaderProgram *program, const SharedContext *context,
monitor ? program->frag_monitor_index monitor ? program->frag_monitor_index
: program->frag_output_index, : program->frag_output_index,
true, context); true, context);
check_glerror(program, "shaders_compute");
} }
void shaders_free(const ShaderProgram *program) { void shaders_free(const ShaderProgram *program) {
for (unsigned int i = 0; i < program->frag_count; i++) { for (unsigned int i = 0; i < program->frag_count; i++) {
glDeleteProgram(program->programs[i]); glDeleteProgram(program->programs[i]);
} }
check_glerror_ro("shaders_free/glDeleteProgram");
glDeleteFramebuffers(program->frag_count, program->frame_buffers); glDeleteFramebuffers(program->frag_count, program->frame_buffers);
check_glerror_ro("shaders_free/glDeleteFramebuffers");
glDeleteTextures(program->tex_count, program->textures); glDeleteTextures(program->tex_count, program->textures);
check_glerror_ro("shaders_free/glDeleteTextures");
glDeleteBuffers(1, &program->vertex_buffer); glDeleteBuffers(1, &program->vertex_buffer);
check_glerror_ro("shaders_free/glDeleteBuffers");
} }
void shaders_free_window(const ShaderProgram *program, bool secondary) { void shaders_free_window(const ShaderProgram *program, bool secondary) {
glDeleteVertexArrays(1, &program->vertex_array[secondary ? 1 : 0]); glDeleteVertexArrays(1, &program->vertex_array[secondary ? 1 : 0]);
check_glerror_ro("shaders_free_window/glDeleteVertexArrays");
} }
void shaders_free_input(const ShaderProgram *program, void shaders_free_input(ShaderProgram *program, unsigned int input_index) {
const VideoCapture *input) {
#ifdef VIDEO_IN #ifdef VIDEO_IN
if (!input->error && input->dma_image != EGL_NO_IMAGE_KHR) { if (program->dma_images[input_index] != EGL_NO_IMAGE_KHR) {
eglDestroyImageKHR(program->egl_display, input->dma_image); eglDestroyImageKHR(program->egl_display, program->dma_images[input_index]);
program->dma_images[input_index] = EGL_NO_IMAGE_KHR;
} }
if (!input->error && input->dma_image_swap != EGL_NO_IMAGE_KHR) { if (program->dma_images_swap[input_index] != EGL_NO_IMAGE_KHR) {
eglDestroyImageKHR(program->egl_display, input->dma_image_swap); eglDestroyImageKHR(program->egl_display,
program->dma_images_swap[input_index]);
program->dma_images_swap[input_index] = EGL_NO_IMAGE_KHR;
} }
check_eglerror_ro("shaders_free_input/eglDestroyImageKHR");
#endif /* VIDEO_IN */ #endif /* VIDEO_IN */
} }
+4 -2
View File
@@ -6,6 +6,9 @@
void shaders_init(ShaderProgram *program, const Project *project, void shaders_init(ShaderProgram *program, const Project *project,
const SharedContext *context, bool rebind); const SharedContext *context, bool rebind);
void shaders_relink_input(ShaderProgram *program, const Project *project,
VideoCaptureArray *inputs, unsigned int i);
void shaders_link_inputs(ShaderProgram *program, const Project *project, void shaders_link_inputs(ShaderProgram *program, const Project *project,
VideoCaptureArray *inputs); VideoCaptureArray *inputs);
@@ -19,7 +22,6 @@ void shaders_free(const ShaderProgram *program);
void shaders_free_window(const ShaderProgram *program, bool secondary); void shaders_free_window(const ShaderProgram *program, bool secondary);
void shaders_free_input(const ShaderProgram *program, void shaders_free_input(ShaderProgram *program, unsigned int input_index);
const VideoCapture *input);
#endif /* SHADERS_H */ #endif /* SHADERS_H */
+2 -1
View File
@@ -1,5 +1,6 @@
#include <log.h> #include <log.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include "types.h" #include "types.h"
@@ -619,7 +620,7 @@ bool state_background_write(SharedContext *context,
} }
log_info("(state) background writing stopped by main thread (pid: %d)", pid); log_info("(state) background writing stopped by main thread (pid: %d)", pid);
return false; exit(EXIT_SUCCESS);
} }
void state_init(SharedContext *context, const StateConfig *state_config, void state_init(SharedContext *context, const StateConfig *state_config,
-1
View File
@@ -1,4 +1,3 @@
#include <bsd/string.h>
#include <stdbool.h> #include <stdbool.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
+12 -7
View File
@@ -8,7 +8,6 @@
#include <hashmap.h> #include <hashmap.h>
#include <linmath.h> #include <linmath.h>
#include <stdbool.h> #include <stdbool.h>
#include <sys/time.h>
#include <time.h> #include <time.h>
#include "config.h" #include "config.h"
@@ -49,6 +48,7 @@ typedef struct Parameters {
StringArray video_in; StringArray video_in;
unsigned int video_size; unsigned int video_size;
unsigned int internal_size; unsigned int internal_size;
bool video_reconnect;
bool load_state; bool load_state;
bool save_state; bool save_state;
bool trace_midi; bool trace_midi;
@@ -130,6 +130,8 @@ typedef struct ShaderProgram {
unsigned int in_count; unsigned int in_count;
#ifdef VIDEO_IN #ifdef VIDEO_IN
EGLDisplay egl_display; EGLDisplay egl_display;
EGLImageKHR dma_images[MAX_VIDEO];
EGLImageKHR dma_images_swap[MAX_VIDEO];
#endif /* VIDEO_IN */ #endif /* VIDEO_IN */
} ShaderProgram; } ShaderProgram;
@@ -138,7 +140,11 @@ typedef struct ShaderProgram {
typedef struct VideoCapture { typedef struct VideoCapture {
char name[STR_LEN]; char name[STR_LEN];
bool error; bool error;
bool disconnected;
bool needs_reload;
bool with_swap; bool with_swap;
bool swap;
unsigned int fps;
int fd; int fd;
int exp_fd; int exp_fd;
int exp_fd_swap; int exp_fd_swap;
@@ -149,12 +155,13 @@ typedef struct VideoCapture {
#ifdef VIDEO_IN #ifdef VIDEO_IN
struct v4l2_buffer buf; struct v4l2_buffer buf;
struct v4l2_buffer buf_swap; struct v4l2_buffer buf_swap;
EGLImageKHR dma_image;
EGLImageKHR dma_image_swap;
#endif /* VIDEO_IN */ #endif /* VIDEO_IN */
} VideoCapture; } VideoCapture;
typedef ARRAY(VideoCaptureArray, VideoCapture); typedef struct VideoCaptureArray {
VideoCapture values[MAX_VIDEO];
unsigned int length;
} VideoCaptureArray;
// window.c // window.c
@@ -193,9 +200,7 @@ typedef struct SharedContext {
unsigned int auto_random_cycle; unsigned int auto_random_cycle;
unsigned int seeds[MAX_FRAG]; unsigned int seeds[MAX_FRAG];
unsigned int fps; unsigned int fps;
unsigned int input_formats[MAX_VIDEO]; VideoCaptureArray inputs;
unsigned int input_fps[MAX_VIDEO];
bool input_swap[MAX_VIDEO];
bool stop; bool stop;
} SharedContext; } SharedContext;
+14 -12
View File
@@ -1,6 +1,5 @@
#ifdef VIDEO_IN #ifdef VIDEO_IN
#include <bsd/string.h>
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include <linux/videodev2.h> #include <linux/videodev2.h>
@@ -286,22 +285,25 @@ static void close_stream(const VideoCapture *video_capture) {
ioctl(video_capture->fd, VIDIOC_STREAMOFF, &buf_type); ioctl(video_capture->fd, VIDIOC_STREAMOFF, &buf_type);
} }
static unsigned int read_video(const VideoCapture *video_capture, bool swap) { static unsigned int read_video(VideoCapture *video_capture) {
int result; unsigned int result;
struct v4l2_capability cap;
result = 0; result = 0;
if ((swap || !video_capture->with_swap) && if ((video_capture->swap || !video_capture->with_swap) &&
ioctl(video_capture->fd, VIDIOC_DQBUF, &video_capture->buf) != -1) { ioctl(video_capture->fd, VIDIOC_DQBUF, &video_capture->buf) != -1) {
ioctl(video_capture->fd, VIDIOC_QBUF, &video_capture->buf); ioctl(video_capture->fd, VIDIOC_QBUF, &video_capture->buf);
result = 1; result = 1;
} else if (!swap && video_capture->with_swap && } else if (!video_capture->swap && video_capture->with_swap &&
ioctl(video_capture->fd, VIDIOC_DQBUF, &video_capture->buf_swap) != ioctl(video_capture->fd, VIDIOC_DQBUF, &video_capture->buf_swap) !=
-1) { -1) {
ioctl(video_capture->fd, VIDIOC_QBUF, &video_capture->buf_swap); ioctl(video_capture->fd, VIDIOC_QBUF, &video_capture->buf_swap);
result = 2; result = 2;
} else if (ioctl(video_capture->fd, VIDIOC_QUERYCAP, &cap) == -1) {
video_capture->error = true;
} }
return result; return result;
@@ -361,18 +363,18 @@ bool video_background_read(VideoCapture *video_capture, SharedContext *context,
pid); pid);
timer_init(&timer, 30); timer_init(&timer, 30);
while (!context->stop) { while (!context->stop && !video_capture->error) {
video_result = read_video(video_capture, context->input_swap[input_index]); video_result = read_video(video_capture);
if (video_result > 0 && timer_inc(&timer)) { if (video_result > 0 && timer_inc(&timer)) {
fps = timer_reset(&timer); fps = timer_reset(&timer);
context->input_fps[input_index] = (unsigned int)round(fps); context->inputs.values[input_index].fps = (unsigned int)round(fps);
if (trace_fps) { if (trace_fps) {
log_trace("(%s) %.2ffps", video_capture->name, fps); log_trace("(%s) %.2ffps", video_capture->name, fps);
} }
} }
if (video_result > 0) { if (video_result > 0) {
context->input_swap[input_index] = video_result == 2; video_capture->swap = video_result == 2;
} }
} }
if (context->stop) { if (context->stop) {
@@ -381,15 +383,15 @@ bool video_background_read(VideoCapture *video_capture, SharedContext *context,
} else { } else {
log_info("(%s) background acquisition stopped after error (pid: %d)", log_info("(%s) background acquisition stopped after error (pid: %d)",
video_capture->name, pid); video_capture->name, pid);
video_capture->disconnected = true;
video_capture->pixelformat = 0;
video_free(video_capture);
} }
exit(context->stop ? EXIT_SUCCESS : EXIT_FAILURE); exit(context->stop ? EXIT_SUCCESS : EXIT_FAILURE);
return false;
} }
void video_free(const VideoCapture *video_capture) { void video_free(const VideoCapture *video_capture) {
if (!video_capture->error) {
close_stream(video_capture); close_stream(video_capture);
}
if (video_capture->exp_fd != -1) { if (video_capture->exp_fd != -1) {
close(video_capture->exp_fd); close(video_capture->exp_fd);
} }