forked from Oncorporation/obs-shaderfilter
-
-
Notifications
You must be signed in to change notification settings - Fork 42
/
obs-shaderfilter.c
3017 lines (2694 loc) · 96.3 KB
/
obs-shaderfilter.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Version 2.0 by Exeldro https://github.com/exeldro/obs-shaderfilter
// Version 1.21 by Charles Fettinger https://github.com/Oncorporation/obs-shaderfilter
// original version by nleseul https://github.com/nleseul/obs-shaderfilter
#include <obs-module.h>
#include <graphics/graphics.h>
#include <graphics/image-file.h>
#include <graphics/math-extra.h>
#include <util/base.h>
#include <util/dstr.h>
#include <util/darray.h>
#include <util/platform.h>
#include <float.h>
#include <limits.h>
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <util/threading.h>
#include "version.h"
#define nullptr ((void *)0)
static const char *effect_template_begin = "\
uniform float4x4 ViewProj;\n\
uniform texture2d image;\n\
\n\
uniform float2 uv_offset;\n\
uniform float2 uv_scale;\n\
uniform float2 uv_pixel_interval;\n\
uniform float2 uv_size;\n\
uniform float rand_f;\n\
uniform float rand_instance_f;\n\
uniform float rand_activation_f;\n\
uniform float elapsed_time;\n\
uniform int loops;\n\
uniform float local_time;\n\
\n\
sampler_state textureSampler{\n\
Filter = Linear;\n\
AddressU = Border;\n\
AddressV = Border;\n\
BorderColor = 00000000;\n\
};\n\
\n\
struct VertData {\n\
float4 pos : POSITION;\n\
float2 uv : TEXCOORD0;\n\
};\n\
\n\
VertData mainTransform(VertData v_in)\n\
{\n\
VertData vert_out;\n\
vert_out.pos = mul(float4(v_in.pos.xyz, 1.0), ViewProj);\n\
vert_out.uv = v_in.uv * uv_scale + uv_offset;\n\
return vert_out;\n\
}\n\
\n\
float srgb_nonlinear_to_linear_channel(float u)\n\
{\n\
return (u <= 0.04045) ? (u / 12.92) : pow((u + 0.055) / 1.055, 2.4);\n\
}\n\
\n\
float3 srgb_nonlinear_to_linear(float3 v)\n\
{\n\
return float3(srgb_nonlinear_to_linear_channel(v.r),\n\
srgb_nonlinear_to_linear_channel(v.g),\n\
srgb_nonlinear_to_linear_channel(v.b));\n\
}\n\
\n";
static const char *effect_template_default_image_shader = "\n\
float4 mainImage(VertData v_in) : TARGET\n\
{\n\
return image.Sample(textureSampler, v_in.uv);\n\
}\n\
";
static const char *effect_template_default_transition_image_shader = "\n\
uniform texture2d image_a;\n\
uniform texture2d image_b;\n\
uniform float transition_time = 0.5;\n\
uniform bool convert_linear = true;\n\
\n\
float4 mainImage(VertData v_in) : TARGET\n\
{\n\
float4 a_val = image_a.Sample(textureSampler, v_in.uv);\n\
float4 b_val = image_b.Sample(textureSampler, v_in.uv);\n\
float4 rgba = lerp(a_val, b_val, transition_time);\n\
if (convert_linear)\n\
rgba.rgb = srgb_nonlinear_to_linear(rgba.rgb);\n\
return rgba;\n\
}\n\
";
static const char *effect_template_end = "\n\
technique Draw\n\
{\n\
pass\n\
{\n\
vertex_shader = mainTransform(v_in);\n\
pixel_shader = mainImage(v_in);\n\
}\n\
}\n";
struct effect_param_data {
struct dstr name;
struct dstr display_name;
struct dstr widget_type;
struct dstr description;
struct dstr group;
struct dstr path;
DARRAY(int) option_values;
DARRAY(struct dstr) option_labels;
enum gs_shader_param_type type;
gs_eparam_t *param;
gs_image_file_t *image;
gs_texrender_t *render;
obs_weak_source_t *source;
union {
long long i;
double f;
char *string;
struct vec2 vec2;
struct vec3 vec3;
struct vec4 vec4;
} value;
char *label;
union {
long long i;
double f;
} minimum;
union {
long long i;
double f;
} maximum;
union {
long long i;
double f;
} step;
};
struct shader_filter_data {
obs_source_t *context;
gs_effect_t *effect;
gs_effect_t *output_effect;
gs_texrender_t *input_texrender;
gs_texrender_t *output_texrender;
gs_eparam_t *param_output_image;
bool reload_effect;
struct dstr last_path;
bool last_from_file;
bool transition;
bool transitioning;
bool prev_transitioning;
bool use_pm_alpha;
bool rendered;
float shader_start_time;
gs_eparam_t *param_uv_offset;
gs_eparam_t *param_uv_scale;
gs_eparam_t *param_uv_pixel_interval;
gs_eparam_t *param_uv_size;
gs_eparam_t *param_elapsed_time;
gs_eparam_t *param_loops;
gs_eparam_t *param_local_time;
gs_eparam_t *param_rand_f;
gs_eparam_t *param_rand_instance_f;
gs_eparam_t *param_rand_activation_f;
gs_eparam_t *param_image;
gs_eparam_t *param_image_a;
gs_eparam_t *param_image_b;
gs_eparam_t *param_transition_time;
gs_eparam_t *param_convert_linear;
int expand_left;
int expand_right;
int expand_top;
int expand_bottom;
int total_width;
int total_height;
bool use_shader_elapsed_time;
bool no_repeat;
bool rendering;
struct vec2 uv_offset;
struct vec2 uv_scale;
struct vec2 uv_pixel_interval;
struct vec2 uv_size;
float elapsed_time;
float elapsed_time_loop;
int loops;
float local_time;
float rand_f;
float rand_instance_f;
float rand_activation_f;
DARRAY(struct effect_param_data) stored_param_list;
};
static unsigned int rand_interval(unsigned int min, unsigned int max)
{
unsigned int r;
const unsigned int range = 1 + max - min;
const unsigned int buckets = RAND_MAX / range;
const unsigned int limit = buckets * range;
/* Create equal size buckets all in a row, then fire randomly towards
* the buckets until you land in one of them. All buckets are equally
* likely. If you land off the end of the line of buckets, try again. */
do {
r = rand();
} while (r >= limit);
return min + (r / buckets);
}
static char *load_shader_from_file(const char *file_name) // add input of visited files
{
char *file_ptr = os_quick_read_utf8_file(file_name);
if (file_ptr == NULL)
return NULL;
char **lines = strlist_split(file_ptr, '\n', true);
struct dstr shader_file;
dstr_init(&shader_file);
size_t line_i = 0;
while (lines[line_i] != NULL) {
char *line = lines[line_i];
line_i++;
if (strncmp(line, "#include", 8) == 0) {
// Open the included file, place contents here.
char *pos = strrchr(file_name, '/');
const size_t length = pos - file_name + 1;
struct dstr include_path = {0};
dstr_ncopy(&include_path, file_name, length);
char *start = strchr(line, '"') + 1;
char *end = strrchr(line, '"');
dstr_ncat(&include_path, start, end - start);
char *abs_include_path = os_get_abs_path_ptr(include_path.array);
char *file_contents = load_shader_from_file(abs_include_path);
dstr_cat(&shader_file, file_contents);
dstr_cat(&shader_file, "\n");
bfree(abs_include_path);
bfree(file_contents);
dstr_free(&include_path);
} else {
// else place current line here.
dstr_cat(&shader_file, line);
dstr_cat(&shader_file, "\n");
}
}
// Add file_name to visited files
// Do stuff with the file, and populate shader_file
/*
for line in file:
if line starts with #include
get path
if path is not in visited files
include_file_contents = load_shader_from_file(path)
concat include_file_contents onto shader_file
else
concat line onto shader_file
*/
bfree(file_ptr);
strlist_free(lines);
return shader_file.array;
}
static void shader_filter_clear_params(struct shader_filter_data *filter)
{
filter->param_elapsed_time = NULL;
filter->param_uv_offset = NULL;
filter->param_uv_pixel_interval = NULL;
filter->param_uv_scale = NULL;
filter->param_uv_size = NULL;
filter->param_rand_f = NULL;
filter->param_rand_activation_f = NULL;
filter->param_rand_instance_f = NULL;
filter->param_loops = NULL;
filter->param_local_time = NULL;
filter->param_image = NULL;
filter->param_image_a = NULL;
filter->param_image_b = NULL;
filter->param_transition_time = NULL;
filter->param_convert_linear = NULL;
size_t param_count = filter->stored_param_list.num;
for (size_t param_index = 0; param_index < param_count; param_index++) {
struct effect_param_data *param = (filter->stored_param_list.array + param_index);
if (param->image) {
obs_enter_graphics();
gs_image_file_free(param->image);
obs_leave_graphics();
bfree(param->image);
param->image = NULL;
}
if (param->source) {
obs_source_t *source = obs_weak_source_get_source(param->source);
if (source) {
if ((!filter->transition || filter->prev_transitioning) && obs_source_active(filter->context))
obs_source_dec_active(source);
if ((!filter->transition || filter->prev_transitioning) && obs_source_showing(filter->context))
obs_source_dec_showing(source);
obs_source_release(source);
}
obs_weak_source_release(param->source);
param->source = NULL;
}
if (param->render) {
obs_enter_graphics();
gs_texrender_destroy(param->render);
obs_leave_graphics();
param->render = NULL;
}
dstr_free(¶m->name);
dstr_free(¶m->display_name);
dstr_free(¶m->widget_type);
dstr_free(¶m->description);
dstr_free(¶m->group);
dstr_free(¶m->path);
da_free(param->option_values);
for (size_t i = 0; i < param->option_labels.num; i++) {
dstr_free(¶m->option_labels.array[i]);
}
da_free(param->option_labels);
}
da_free(filter->stored_param_list);
}
static void load_output_effect(struct shader_filter_data *filter)
{
if (filter->output_effect != NULL) {
obs_enter_graphics();
gs_effect_destroy(filter->output_effect);
filter->output_effect = NULL;
obs_leave_graphics();
}
char *shader_text = NULL;
struct dstr filename = {0};
dstr_cat(&filename, obs_get_module_data_path(obs_current_module()));
dstr_cat(&filename, "/internal/render_output.effect");
shader_text = load_shader_from_file(filename.array);
char *errors = NULL;
dstr_free(&filename);
obs_enter_graphics();
filter->output_effect = gs_effect_create(shader_text, NULL, &errors);
obs_leave_graphics();
bfree(shader_text);
if (filter->output_effect == NULL) {
blog(LOG_WARNING, "[obs-shaderfilter] Unable to load output.effect file. Errors:\n%s",
(errors == NULL || strlen(errors) == 0 ? "(None)" : errors));
bfree(errors);
} else {
size_t effect_count = gs_effect_get_num_params(filter->output_effect);
for (size_t effect_index = 0; effect_index < effect_count; effect_index++) {
gs_eparam_t *param = gs_effect_get_param_by_idx(filter->output_effect, effect_index);
struct gs_effect_param_info info;
gs_effect_get_param_info(param, &info);
if (strcmp(info.name, "output_image") == 0) {
filter->param_output_image = param;
}
}
}
}
static void shader_filter_reload_effect(struct shader_filter_data *filter)
{
obs_data_t *settings = obs_source_get_settings(filter->context);
// First, clean up the old effect and all references to it.
filter->shader_start_time = 0.0;
shader_filter_clear_params(filter);
if (filter->effect != NULL) {
obs_enter_graphics();
gs_effect_destroy(filter->effect);
filter->effect = NULL;
obs_leave_graphics();
}
// Load text and build the effect from the template, if necessary.
char *shader_text = NULL;
bool use_template = !obs_data_get_bool(settings, "override_entire_effect");
if (obs_data_get_bool(settings, "from_file")) {
const char *file_name = obs_data_get_string(settings, "shader_file_name");
shader_text = load_shader_from_file(file_name);
} else {
shader_text = bstrdup(obs_data_get_string(settings, "shader_text"));
use_template = true;
}
struct dstr effect_text = {0};
if (use_template) {
dstr_cat(&effect_text, effect_template_begin);
}
if (shader_text) {
dstr_cat(&effect_text, shader_text);
bfree(shader_text);
}
if (use_template) {
dstr_cat(&effect_text, effect_template_end);
}
// Create the effect.
char *errors = NULL;
obs_enter_graphics();
int device_type = gs_get_device_type();
if (device_type == GS_DEVICE_OPENGL) {
dstr_replace(&effect_text, "[loop]", "");
dstr_insert(&effect_text, 0, "#define OPENGL 1\n");
}
if (effect_text.len && dstr_find(&effect_text, "#define USE_PM_ALPHA 1")) {
filter->use_pm_alpha = true;
} else {
filter->use_pm_alpha = false;
}
if (filter->effect)
gs_effect_destroy(filter->effect);
filter->effect = gs_effect_create(effect_text.array, NULL, &errors);
obs_leave_graphics();
if (filter->effect == NULL) {
blog(LOG_WARNING, "[obs-shaderfilter] Unable to create effect. Errors returned from parser:\n%s",
(errors == NULL || strlen(errors) == 0 ? "(None)" : errors));
if (errors && strlen(errors)) {
obs_data_set_string(settings, "last_error", errors);
} else {
obs_data_set_string(settings, "last_error", obs_module_text("ShaderFilter.Unknown"));
}
dstr_free(&effect_text);
bfree(errors);
goto end;
} else {
dstr_free(&effect_text);
obs_data_unset_user_value(settings, "last_error");
}
// Store references to the new effect's parameters.
da_free(filter->stored_param_list);
size_t effect_count = gs_effect_get_num_params(filter->effect);
for (size_t effect_index = 0; effect_index < effect_count; effect_index++) {
gs_eparam_t *param = gs_effect_get_param_by_idx(filter->effect, effect_index);
struct gs_effect_param_info info;
gs_effect_get_param_info(param, &info);
if (strcmp(info.name, "uv_offset") == 0) {
filter->param_uv_offset = param;
} else if (strcmp(info.name, "uv_scale") == 0) {
filter->param_uv_scale = param;
} else if (strcmp(info.name, "uv_pixel_interval") == 0) {
filter->param_uv_pixel_interval = param;
} else if (strcmp(info.name, "uv_size") == 0) {
filter->param_uv_size = param;
} else if (strcmp(info.name, "elapsed_time") == 0) {
filter->param_elapsed_time = param;
} else if (strcmp(info.name, "rand_f") == 0) {
filter->param_rand_f = param;
} else if (strcmp(info.name, "rand_activation_f") == 0) {
filter->param_rand_activation_f = param;
} else if (strcmp(info.name, "rand_instance_f") == 0) {
filter->param_rand_instance_f = param;
} else if (strcmp(info.name, "loops") == 0) {
filter->param_loops = param;
} else if (strcmp(info.name, "local_time") == 0) {
filter->param_local_time = param;
} else if (strcmp(info.name, "ViewProj") == 0) {
// Nothing.
} else if (strcmp(info.name, "image") == 0) {
filter->param_image = param;
} else if (filter->transition && strcmp(info.name, "image_a") == 0) {
filter->param_image_a = param;
} else if (filter->transition && strcmp(info.name, "image_b") == 0) {
filter->param_image_b = param;
} else if (filter->transition && strcmp(info.name, "transition_time") == 0) {
filter->param_transition_time = param;
} else if (filter->transition && strcmp(info.name, "convert_linear") == 0) {
filter->param_convert_linear = param;
} else {
struct effect_param_data *cached_data = da_push_back_new(filter->stored_param_list);
dstr_copy(&cached_data->name, info.name);
cached_data->type = info.type;
cached_data->param = param;
da_init(cached_data->option_values);
da_init(cached_data->option_labels);
const size_t annotation_count = gs_param_get_num_annotations(param);
for (size_t annotation_index = 0; annotation_index < annotation_count; annotation_index++) {
gs_eparam_t *annotation = gs_param_get_annotation_by_idx(param, annotation_index);
void *annotation_default = gs_effect_get_default_val(annotation);
gs_effect_get_param_info(annotation, &info);
if (strcmp(info.name, "name") == 0 && info.type == GS_SHADER_PARAM_STRING) {
dstr_copy(&cached_data->display_name, (const char *)annotation_default);
} else if (strcmp(info.name, "label") == 0 && info.type == GS_SHADER_PARAM_STRING) {
dstr_copy(&cached_data->display_name, (const char *)annotation_default);
} else if (strcmp(info.name, "widget_type") == 0 && info.type == GS_SHADER_PARAM_STRING) {
dstr_copy(&cached_data->widget_type, (const char *)annotation_default);
} else if (strcmp(info.name, "group") == 0 && info.type == GS_SHADER_PARAM_STRING) {
dstr_copy(&cached_data->group, (const char *)annotation_default);
} else if (strcmp(info.name, "minimum") == 0) {
if (info.type == GS_SHADER_PARAM_FLOAT || info.type == GS_SHADER_PARAM_VEC2 ||
info.type == GS_SHADER_PARAM_VEC3 || info.type == GS_SHADER_PARAM_VEC4) {
cached_data->minimum.f = *(float *)annotation_default;
} else if (info.type == GS_SHADER_PARAM_INT) {
cached_data->minimum.i = *(int *)annotation_default;
}
} else if (strcmp(info.name, "maximum") == 0) {
if (info.type == GS_SHADER_PARAM_FLOAT || info.type == GS_SHADER_PARAM_VEC2 ||
info.type == GS_SHADER_PARAM_VEC3 || info.type == GS_SHADER_PARAM_VEC4) {
cached_data->maximum.f = *(float *)annotation_default;
} else if (info.type == GS_SHADER_PARAM_INT) {
cached_data->maximum.i = *(int *)annotation_default;
}
} else if (strcmp(info.name, "step") == 0) {
if (info.type == GS_SHADER_PARAM_FLOAT || info.type == GS_SHADER_PARAM_VEC2 ||
info.type == GS_SHADER_PARAM_VEC3 || info.type == GS_SHADER_PARAM_VEC4) {
cached_data->step.f = *(float *)annotation_default;
} else if (info.type == GS_SHADER_PARAM_INT) {
cached_data->step.i = *(int *)annotation_default;
}
} else if (strncmp(info.name, "option_", 7) == 0) {
int id = atoi(info.name + 7);
if (info.type == GS_SHADER_PARAM_INT) {
int val = *(int *)annotation_default;
int *cd = da_insert_new(cached_data->option_values, id);
*cd = val;
} else if (info.type == GS_SHADER_PARAM_STRING) {
struct dstr val = {0};
dstr_copy(&val, (const char *)annotation_default);
struct dstr *cs = da_insert_new(cached_data->option_labels, id);
*cs = val;
}
}
bfree(annotation_default);
}
}
}
end:
obs_data_release(settings);
}
static const char *shader_filter_get_name(void *unused)
{
UNUSED_PARAMETER(unused);
return obs_module_text("ShaderFilter");
}
static void *shader_filter_create(obs_data_t *settings, obs_source_t *source)
{
struct shader_filter_data *filter = bzalloc(sizeof(struct shader_filter_data));
filter->context = source;
filter->reload_effect = true;
dstr_init(&filter->last_path);
dstr_copy(&filter->last_path, obs_data_get_string(settings, "shader_file_name"));
filter->last_from_file = obs_data_get_bool(settings, "from_file");
filter->rand_instance_f = (float)((double)rand_interval(0, 10000) / (double)10000);
filter->rand_activation_f = (float)((double)rand_interval(0, 10000) / (double)10000);
da_init(filter->stored_param_list);
load_output_effect(filter);
obs_source_update(source, settings);
return filter;
}
static void shader_filter_destroy(void *data)
{
struct shader_filter_data *filter = data;
shader_filter_clear_params(filter);
obs_enter_graphics();
if (filter->effect)
gs_effect_destroy(filter->effect);
if (filter->output_effect)
gs_effect_destroy(filter->output_effect);
if (filter->input_texrender)
gs_texrender_destroy(filter->input_texrender);
if (filter->output_texrender)
gs_texrender_destroy(filter->output_texrender);
obs_leave_graphics();
dstr_free(&filter->last_path);
da_free(filter->stored_param_list);
bfree(filter);
}
static bool shader_filter_from_file_changed(obs_properties_t *props, obs_property_t *p, obs_data_t *settings)
{
UNUSED_PARAMETER(p);
struct shader_filter_data *filter = obs_properties_get_param(props);
bool from_file = obs_data_get_bool(settings, "from_file");
obs_property_set_visible(obs_properties_get(props, "shader_text"), !from_file);
obs_property_set_visible(obs_properties_get(props, "shader_file_name"), from_file);
if (from_file != filter->last_from_file) {
filter->reload_effect = true;
}
filter->last_from_file = from_file;
return true;
}
static bool shader_filter_text_changed(obs_properties_t *props, obs_property_t *p, obs_data_t *settings)
{
UNUSED_PARAMETER(p);
struct shader_filter_data *filter = obs_properties_get_param(props);
if (!filter)
return false;
const char *shader_text = obs_data_get_string(settings, "shader_text");
bool can_convert = strstr(shader_text, "void mainImage( out vec4") || strstr(shader_text, "void mainImage(out vec4") ||
strstr(shader_text, "void main()") || strstr(shader_text, "vec4 effect(vec4");
obs_property_t *shader_convert = obs_properties_get(props, "shader_convert");
bool visible = obs_property_visible(obs_properties_get(props, "shader_text"));
if (obs_property_visible(shader_convert) != (can_convert && visible)) {
obs_property_set_visible(shader_convert, can_convert && visible);
return true;
}
return false;
}
static bool shader_filter_file_name_changed(obs_properties_t *props, obs_property_t *p, obs_data_t *settings)
{
struct shader_filter_data *filter = obs_properties_get_param(props);
const char *new_file_name = obs_data_get_string(settings, obs_property_name(p));
if ((dstr_is_empty(&filter->last_path) && strlen(new_file_name)) ||
(filter->last_path.array && dstr_cmp(&filter->last_path, new_file_name) != 0)) {
filter->reload_effect = true;
dstr_copy(&filter->last_path, new_file_name);
size_t l = strlen(new_file_name);
if (l > 7 && strncmp(new_file_name + l - 7, ".effect", 7) == 0) {
obs_data_set_bool(settings, "override_entire_effect", true);
} else if (l > 7 && strncmp(new_file_name + l - 7, ".shader", 7) == 0) {
obs_data_set_bool(settings, "override_entire_effect", false);
}
}
return false;
}
static bool use_shader_elapsed_time_changed(obs_properties_t *props, obs_property_t *p, obs_data_t *settings)
{
UNUSED_PARAMETER(p);
struct shader_filter_data *filter = obs_properties_get_param(props);
bool use_shader_elapsed_time = obs_data_get_bool(settings, "use_shader_elapsed_time");
if (use_shader_elapsed_time != filter->use_shader_elapsed_time) {
filter->reload_effect = true;
}
filter->use_shader_elapsed_time = use_shader_elapsed_time;
return false;
}
static bool shader_filter_reload_effect_clicked(obs_properties_t *props, obs_property_t *property, void *data)
{
UNUSED_PARAMETER(props);
UNUSED_PARAMETER(property);
struct shader_filter_data *filter = data;
filter->reload_effect = true;
obs_source_update(filter->context, NULL);
return false;
}
static bool add_source_to_list(void *data, obs_source_t *source)
{
obs_property_t *p = data;
const char *name = obs_source_get_name(source);
size_t count = obs_property_list_item_count(p);
size_t idx = 0;
while (idx < count && strcmp(name, obs_property_list_item_string(p, idx)) > 0)
idx++;
obs_property_list_insert_string(p, idx, name, name);
return true;
}
static bool is_var_char(char ch)
{
return (ch >= '0' && ch <= '9') || (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || ch == '_';
}
static void convert_if_defined(struct dstr *effect_text)
{
char *pos = strstr(effect_text->array, "#if defined(");
while (pos) {
size_t diff = pos - effect_text->array;
char *ch = strstr(effect_text->array + diff + 12, ")\n");
pos = strstr(effect_text->array + diff + 12, "#if defined(");
if (ch && (!pos || ch < pos)) {
*ch = ' ';
dstr_remove(effect_text, diff, 12);
dstr_insert(effect_text, diff, "#ifdef ");
pos = strstr(effect_text->array + diff + 5, "#if defined(");
}
}
}
static void convert_atan(struct dstr *effect_text)
{
char *pos = strstr(effect_text->array, "atan(");
while (pos) {
if (is_var_char(*(pos - 1))) {
pos = strstr(pos + 5, "atan(");
continue;
}
size_t diff = pos - effect_text->array;
char *comma = strstr(pos + 5, ",");
char *divide = strstr(pos + 5, "/");
if (!comma && !divide)
return;
int depth = 0;
char *open = strstr(pos + 5, "(");
char *close = strstr(pos + 5, ")");
if (!close)
return;
do {
while (open && open < close) {
depth++;
open = strstr(open + 1, "(");
}
while (depth > 0 && close && (!open || close < open)) {
depth--;
if (depth == 0 && (!comma || comma < close))
comma = strstr(close + 1, ",");
if (depth == 0 && (!divide || divide < close))
divide = strstr(close + 1, "/");
if (comma && open && comma > open && comma < close)
comma = NULL;
if (divide && open && divide > open && divide < close)
divide = NULL;
open = strstr(close + 1, "(");
close = strstr(close + 1, ")");
}
} while (depth > 0 && close);
if (close && comma && comma < close && (!open || comma < open)) {
//*comma = '/';
dstr_insert(effect_text, diff + 4, "2");
}
if (close && divide && divide < close && (!open || divide < open)) {
*divide = ',';
dstr_insert(effect_text, diff + 4, "2");
}
pos = strstr(effect_text->array + diff + 5, "atan(");
}
}
static void insert_begin_of_block(struct dstr *effect_text, size_t diff, char *insert)
{
int depth = 0;
char *ch = effect_text->array + diff;
while (ch > effect_text->array && (*ch == ' ' || *ch == '\t'))
ch--;
while (ch > effect_text->array) {
while (*ch != '=' && *ch != '(' && *ch != ')' && *ch != '+' && *ch != '-' && *ch != '/' && *ch != '*' &&
*ch != ' ' && *ch != '\t' && ch > effect_text->array)
ch--;
if (*ch == '(') {
if (depth == 0) {
dstr_insert(effect_text, ch - effect_text->array + 1, insert);
return;
}
ch--;
depth--;
} else if (*ch == ')') {
ch--;
depth++;
} else if (*ch == '=') {
dstr_insert(effect_text, ch - effect_text->array + 1, insert);
return;
} else if (*ch == '+' || *ch == '-' || *ch == '/' || *ch == '*' || *ch == ' ' || *ch == '\t') {
if (depth == 0) {
dstr_insert(effect_text, ch - effect_text->array + 1, insert);
return;
}
ch--;
}
}
}
static void insert_end_of_block(struct dstr *effect_text, size_t diff, char *insert)
{
int depth = 0;
char *ch = effect_text->array + diff;
while (*ch == ' ' || *ch == '\t')
ch++;
while (*ch != 0) {
while (*ch != ';' && *ch != '(' && *ch != ')' && *ch != '+' && *ch != '-' && *ch != '/' && *ch != '*' &&
*ch != ' ' && *ch != '\t' && *ch != ',' && *ch != 0)
ch++;
if (*ch == '(') {
ch++;
depth++;
} else if (*ch == ')') {
if (depth == 0) {
dstr_insert(effect_text, ch - effect_text->array, insert);
return;
}
ch++;
depth--;
} else if (*ch == ';') {
dstr_insert(effect_text, ch - effect_text->array, insert);
return;
} else if (*ch == '+' || *ch == '-' || *ch == '/' || *ch == '*' || *ch == ' ' || *ch == '\t' || *ch == ',') {
if (depth == 0) {
dstr_insert(effect_text, ch - effect_text->array, insert);
return;
}
ch++;
}
}
}
static void convert_mat_mul_var(struct dstr *effect_text, struct dstr *var_function_name)
{
char *pos = strstr(effect_text->array, var_function_name->array);
while (pos) {
if (is_var_char(*(pos - 1)) || is_var_char(*(pos + var_function_name->len))) {
pos = strstr(pos + var_function_name->len, var_function_name->array);
continue;
}
size_t diff = pos - effect_text->array;
char *ch = pos + var_function_name->len;
while (*ch == ' ' || *ch == '\t')
ch++;
if (*ch == '*' && *(ch + 1) != '=') {
size_t diff3 = ch - effect_text->array + 1;
*ch = ',';
insert_end_of_block(effect_text, diff3, ")");
dstr_insert(effect_text, diff, "mul(");
pos = strstr(effect_text->array + diff + 4 + var_function_name->len, var_function_name->array);
continue;
}
ch = pos - 1;
while ((*ch == ' ' || *ch == '\t') && ch > effect_text->array)
ch--;
if (*ch == '=' && *(ch - 1) == '*') {
char *end = ch - 2;
while ((*end == ' ' || *end == '\t') && end > effect_text->array)
end--;
char *start = end;
while ((is_var_char(*start) || *start == '.') && start > effect_text->array)
start--;
start++;
end++;
diff = ch - 1 - effect_text->array;
struct dstr insert = {0};
dstr_init_copy(&insert, "= mul(");
dstr_ncat(&insert, start, end - start);
dstr_cat(&insert, ",");
dstr_remove(effect_text, diff, 2);
dstr_insert(effect_text, diff, insert.array);
char *line_end = effect_text->array + diff;
while (*line_end != ';' && *line_end != 0)
line_end++;
dstr_insert(effect_text, line_end - effect_text->array, ")");
pos = strstr(effect_text->array + diff + insert.len + 1 + var_function_name->len, var_function_name->array);
dstr_free(&insert);
continue;
} else if (*ch == '*') {
size_t diff2 = ch - effect_text->array - 1;
*ch = ',';
insert_end_of_block(effect_text, diff + var_function_name->len, ")");
insert_begin_of_block(effect_text, diff2, "mul(");
pos = strstr(effect_text->array + diff + 4 + var_function_name->len, var_function_name->array);
continue;
}
pos = strstr(effect_text->array + diff + var_function_name->len, var_function_name->array);
}
}
static void convert_mat_mul(struct dstr *effect_text, char *var_type)
{
size_t len = strlen(var_type);
char *pos = strstr(effect_text->array, var_type);
while (pos) {
if (is_var_char(*(pos - 1))) {
pos = strstr(pos + len, var_type);
continue;
}
size_t diff = pos - effect_text->array;
char *begin = pos + len;
if (*begin == '(') {
char *ch = pos - 1;
while ((*ch == ' ' || *ch == '\t') && ch > effect_text->array)
ch--;
if (*ch == '=' && *(ch - 1) == '*') {
// mat constructor with *= in front of it
char *end = ch - 2;
while ((*end == ' ' || *end == '\t') && end > effect_text->array)
end--;
char *start = end;
while ((is_var_char(*start) || *start == '.') && start > effect_text->array)
start--;
start++;
end++;
size_t diff2 = ch - effect_text->array - 1;
struct dstr insert = {0};
dstr_init_copy(&insert, "= mul(");
dstr_ncat(&insert, start, end - start);
dstr_cat(&insert, ",");
dstr_remove(effect_text, diff2, 2);
dstr_insert(effect_text, diff2, insert.array);
char *line_end = effect_text->array + diff2;
while (*line_end != ';' && *line_end != 0)
line_end++;
dstr_insert(effect_text, line_end - effect_text->array, ")");
pos = strstr(effect_text->array + diff + insert.len + len + 1, var_type);
dstr_free(&insert);
continue;
} else if (*ch == '*') {
// mat constructor with * in front of it
size_t diff2 = ch - effect_text->array - 1;
*ch = ',';
insert_end_of_block(effect_text, diff + len, ")");
insert_begin_of_block(effect_text, diff2, "mul(");
pos = strstr(effect_text->array + diff + len + 4, var_type);
continue;
}
int depth = 1;
ch = begin + 1;
while (*ch != 0) {
while (*ch != ';' && *ch != '(' && *ch != ')' && *ch != '+' && *ch != '-' && *ch != '/' &&
*ch != '*' && *ch != 0)
ch++;
if (*ch == '(') {
ch++;
depth++;
} else if (*ch == ')') {
if (depth == 0) {
break;
}
ch++;
depth--;
} else if (*ch == '*') {
if (depth == 0) {
//mat constructor follow by *
*ch = ',';
insert_end_of_block(effect_text, ch - effect_text->array + 1, ")");
dstr_insert(effect_text, diff, "mul(");
break;
}
ch++;
} else if (*ch == ';') {