flatten state

This commit is contained in:
2025-09-28 19:05:05 +02:00
parent 9c50495c85
commit add1e4c875
7 changed files with 49 additions and 160 deletions
+3 -2
View File
@@ -33,10 +33,11 @@ FRAG_7_OUT=9
FRAG_8_OUT=0
# TODO not like that
SUB_TYPE_COUNT=3
SUB_TYPE_COUNT=2
SUB_1_PREFIX=src_
SUB_2_PREFIX=fx_
SUB_3_PREFIX=mix_
# TODO remove
SUB_VARIANT_COUNT=15
# MIDI
+13 -132
View File
@@ -24,12 +24,12 @@ uniform int seed6;
uniform int seed7;
uniform int seed8;
uniform int state3_1;
uniform int state4_1;
uniform int state5_2;
uniform int state6_2;
uniform int state7_3;
uniform int state8_2;
uniform int state3;
uniform int state4;
uniform int state5;
uniform int state6;
uniform int state7;
uniform int state8;
// 2. textures
// ---------------
@@ -1243,16 +1243,16 @@ subroutine(src_stage_sub) vec4 src_16(vec2 vUV, int seed)
int page = 0; // TODO debug page
int selected_src = 0; // TODO debug selected
int selected_fx = 0; // TODO debug selected
int selected_srca = state3_1;
int selected_srcb = state4_1;
int selected_fxa = state5_2;
int selected_fxb = state6_2;
int selected_mfx = state8_2;
int selected_srca = state3;
int selected_srcb = state4;
int selected_fxa = state5;
int selected_fxb = state6;
int selected_mfx = state8;
float fxa_value = magic(seed5);
float fxb_value = magic(seed6);
float mfx_value = magic(seed8);
float mix_value = magic(seed7);
int mix_type = state7_3 % 2;
bool mix_type = magic_trigger(seed7 + 10);
// logic
@@ -1342,7 +1342,7 @@ subroutine(src_stage_sub) vec4 src_16(vec2 vUV, int seed)
f = mix(f, 1 - f, rect(uv2, vec2(-2, -2.9 + 0.9 * mfx_value), vec2(0.9, 0.9 * mfx_value)));
// show mix
f += char_at(uv2, vec2(1.55, -0.6), mix_type > 0 ? 0x4B : 0x4D);
f += char_at(uv2, vec2(1.55, -0.6), mix_type ? 0x4B : 0x4D);
f = mix(f, 1 - f, rect(uv2, vec2(2, -0.9 + 0.9 * mix_value), vec2(0.9, 0.9 * mix_value)));
// show debug info
@@ -1819,125 +1819,6 @@ subroutine(fx_stage_sub) vec4 fx_16(vec2 vUV, sampler2D previous, sampler2D feed
return vec4(mix(c0, c, fx), 1.0);
}
// 7. mix
// ----------
subroutine vec4 mix_stage_sub(vec2 vUV, sampler2D tex_a, sampler2D tex_a, int seed);
subroutine uniform mix_stage_sub mix_stage;
// MIX 1 : mix
subroutine(mix_stage_sub) vec4 mix_1(vec2 vUV, sampler2D ta, sampler2D tb, int seed)
{
// start
vec2 uv0 = vUV.st;
// controls
float mix_src = magic(seed);
// logic
vec4 color_a = texture(ta, vUV);
vec4 color_b = texture(tb, vUV);
return mix(color_b, color_a, mix_src);
}
// MIX 2 : luminance key
subroutine(mix_stage_sub) vec4 mix_2(vec2 vUV, sampler2D ta, sampler2D tb, int seed)
{
// start
vec2 uv0 = vUV.st;
// controls
float mix_src = magic(seed);
// logic
vec4 color_a = texture(ta, vUV);
vec4 color_b = texture(tb, vUV);
float k = mean(color_a);
return mix(color_b, color_a, step(mix_src, k));
}
// alternate mix 1 / 2 for random selection
subroutine(mix_stage_sub) vec4 mix_3(vec2 vUV, sampler2D ta, sampler2D tb, int seed)
{
return mix_1(vUV, ta, tb, seed);
}
subroutine(mix_stage_sub) vec4 mix_4(vec2 vUV, sampler2D ta, sampler2D tb, int seed)
{
return mix_2(vUV, ta, tb, seed);
}
subroutine(mix_stage_sub) vec4 mix_5(vec2 vUV, sampler2D ta, sampler2D tb, int seed)
{
return mix_1(vUV, ta, tb, seed);
}
subroutine(mix_stage_sub) vec4 mix_6(vec2 vUV, sampler2D ta, sampler2D tb, int seed)
{
return mix_2(vUV, ta, tb, seed);
}
subroutine(mix_stage_sub) vec4 mix_7(vec2 vUV, sampler2D ta, sampler2D tb, int seed)
{
return mix_1(vUV, ta, tb, seed);
}
subroutine(mix_stage_sub) vec4 mix_8(vec2 vUV, sampler2D ta, sampler2D tb, int seed)
{
return mix_2(vUV, ta, tb, seed);
}
subroutine(mix_stage_sub) vec4 mix_9(vec2 vUV, sampler2D ta, sampler2D tb, int seed)
{
return mix_1(vUV, ta, tb, seed);
}
subroutine(mix_stage_sub) vec4 mix_10(vec2 vUV, sampler2D ta, sampler2D tb, int seed)
{
return mix_2(vUV, ta, tb, seed);
}
subroutine(mix_stage_sub) vec4 mix_11(vec2 vUV, sampler2D ta, sampler2D tb, int seed)
{
return mix_1(vUV, ta, tb, seed);
}
subroutine(mix_stage_sub) vec4 mix_12(vec2 vUV, sampler2D ta, sampler2D tb, int seed)
{
return mix_2(vUV, ta, tb, seed);
}
subroutine(mix_stage_sub) vec4 mix_13(vec2 vUV, sampler2D ta, sampler2D tb, int seed)
{
return mix_1(vUV, ta, tb, seed);
}
subroutine(mix_stage_sub) vec4 mix_14(vec2 vUV, sampler2D ta, sampler2D tb, int seed)
{
return mix_2(vUV, ta, tb, seed);
}
subroutine(mix_stage_sub) vec4 mix_15(vec2 vUV, sampler2D ta, sampler2D tb, int seed)
{
return mix_1(vUV, ta, tb, seed);
}
subroutine(mix_stage_sub) vec4 mix_16(vec2 vUV, sampler2D ta, sampler2D tb, int seed)
{
return mix_2(vUV, ta, tb, seed);
}
const mat3x3 yuv_to_rgb = {{1,1,1},{0,-0.39465,2.03211},{1.13983,-0.5806,0}};
const int YUYV_FOURCC = 1448695129;
+8 -6
View File
@@ -34,11 +34,9 @@ void main() {
vec4 c = vec4(0);
c += s(uv2,0,2) * texture(tex3, uv2);
c += s(uv2,1,2) * texture(tex5, uv2);
c += s(uv2,2,2) * texture(tex7, uv2);
c += s(uv2,0,1) * texture(tex4, uv2);
c += s(uv2,1,1) * texture(tex6, uv2);
c += s(uv2,2,1) * texture(tex8, uv2);
@@ -49,16 +47,20 @@ void main() {
float f = 0;
float t = 0;
f += rect(uv3, vec2(-51, 28.5), vec2(2.1, 0.7));
t += write_5(uv3, vec2(-53,28), texts[0]);
if (iInputResolution1.x > 0) {
c += s(uv2,0,2) * texture(tex3, uv2);
f += rect(uv3, vec2(-51, 28.5), vec2(2.1, 0.7));
t += write_5(uv3, vec2(-53,28), texts[0]);
f += rect(uv3, vec2(-50.4, 26.5), vec2(2.8, 0.7));
t += write_int(uv3, vec2(-53,26), iInputFPS1, 2);
t += write_5(uv3, vec2(-50.5,26), texts[8]);
}
f += rect(uv3, vec2(-51, 8.5), vec2(2.1, 0.7));
t += write_5(uv3, vec2(-53,8), texts[1]);
if (iInputResolution2.x > 0) {
c += s(uv2,0,1) * texture(tex4, uv2);
f += rect(uv3, vec2(-51, 8.5), vec2(2.1, 0.7));
t += write_5(uv3, vec2(-53,8), texts[1]);
f += rect(uv3, vec2(-50.4, 6.5), vec2(2.8, 0.7));
t += write_int(uv3, vec2(-53,6), iInputFPS2, 2);
t += write_5(uv3, vec2(-50.5,6), texts[8]);
+9 -1
View File
@@ -8,5 +8,13 @@ in vec2 vUV;
out vec4 fragColor;
void main() {
fragColor = mix_stage(vUV, tex7, tex8, seed7);
float mix_src = magic(seed7);
bool key = magic_trigger(seed7 + 10);
vec4 color_a = texture(tex7, vUV);
vec4 color_b = texture(tex8, vUV);
float k = mean(color_a);
fragColor = mix(color_b, color_a, key ? step(mix_src, k) : mix_src);
}
+6 -3
View File
@@ -51,11 +51,12 @@ static void compute_fps() {
}
}
// TODO put in state file
static void randomize_context_state() {
unsigned int i;
for (i = 0; i < program.frag_count * program.sub_type_count; i++) {
context->sub_state[i] = rand_uint(program.sub_variant_count);
for (i = 0; i < program.frag_count; i++) {
context->state[i] = rand_uint(program.sub_variant_count);
}
}
static void init_context(Parameters params) {
@@ -65,7 +66,7 @@ static void init_context(Parameters params) {
context->demo = params.demo;
context->monitor = params.monitor;
memset(context->sub_state, 0, sizeof(context->sub_state));
memset(context->state, 0, sizeof(context->state));
if (params.demo) {
randomize_context_state();
@@ -215,6 +216,7 @@ static void key_callback(Window *window, int key,
static void midi_callback(unsigned char code, float value) {
log_debug("midi: %d %.2f", code, value);
midi_write(midi, code, value);
// TODO treat in state file
}
static void loop(bool hr) {
@@ -307,6 +309,7 @@ void forge_run(Parameters params) {
init_context(params);
if (program.error) {
context->stop = true;
window_terminate();
exit(EXIT_FAILURE);
}
+9 -15
View File
@@ -300,13 +300,9 @@ static void init_single_program(ShaderProgram *program, unsigned int i,
prefix = config_file_get_str(config, "UNIFORM_STATE_PREFIX", "state");
for (j = 0; j < program->frag_count; j++) {
for (k = 0; k < program->sub_type_count; k++) {
sprintf(name, "%s%d_%d", prefix, j + 1, k + 1);
program
->istate_locations[i * program->frag_count * program->sub_type_count +
j * program->sub_type_count + k] =
glGetUniformLocation(program->programs[i], name);
}
sprintf(name, "%s%d", prefix, j + 1);
program->istate_locations[i * program->frag_count + j] =
glGetUniformLocation(program->programs[i], name);
}
for (j = 0; j < program->sub_type_count; j++) {
@@ -510,18 +506,16 @@ static void use_program(ShaderProgram program, int i, bool output,
}
// set subroutines for fragment and update state uniforms
k = context->state[i];
for (j = 0; j < program.sub_type_count; j++) {
k = context->sub_state[i * program.sub_type_count + j];
subroutines[j] = program.sub_locations[i * program.sub_type_count *
program.sub_variant_count +
j * program.sub_variant_count + k];
for (k = 0; k < program.frag_count; k++) {
glUniform1i(
program.istate_locations[i * program.frag_count *
program.sub_type_count +
k * program.sub_type_count + j],
(const GLint)context->sub_state[k * program.sub_type_count + j]);
}
}
for (j = 0; j < program.frag_count; j++) {
glUniform1i(program.istate_locations[i * program.frag_count + j],
(const GLint)context->state[j]);
}
glUniformSubroutinesuiv(GL_FRAGMENT_SHADER, program.sub_type_count,
+1 -1
View File
@@ -125,7 +125,7 @@ typedef struct SharedContext {
double time;
unsigned int fps;
float tempo;
unsigned int sub_state[MAX_FRAG * MAX_FRAG];
unsigned int state[MAX_FRAG];
bool demo;
unsigned int seeds[MAX_FRAG];
bool monitor;