forked from libretro/RetroArch
-
Notifications
You must be signed in to change notification settings - Fork 2
/
video_shader_parse.c
1320 lines (1133 loc) · 37 KB
/
video_shader_parse.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
/* RetroArch - A frontend for libretro.
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
* Copyright (C) 2011-2017 - Daniel De Matteis
*
* RetroArch is free software: you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <string.h>
#ifdef HAVE_CONFIG_H
#include "../config.h"
#endif
#include <libretro.h>
#include <compat/posix_string.h>
#include <compat/msvc.h>
#include <compat/strl.h>
#include <file/file_path.h>
#include <rhash.h>
#include <string/stdstring.h>
#include <streams/interface_stream.h>
#include <streams/file_stream.h>
#include <lists/string_list.h>
#include "../verbosity.h"
#include "../configuration.h"
#include "../frontend/frontend_driver.h"
#include "video_driver.h"
#include "video_shader_parse.h"
#if defined(HAVE_SLANG) && defined(HAVE_SPIRV_CROSS)
#include "drivers_shader/slang_preprocess.h"
#endif
static path_change_data_t *file_change_data = NULL;
/**
* wrap_mode_to_str:
* @type : Wrap type.
*
* Translates wrap mode to human-readable string identifier.
*
* Returns: human-readable string identifier of wrap mode.
**/
static const char *wrap_mode_to_str(enum gfx_wrap_type type)
{
switch (type)
{
case RARCH_WRAP_BORDER:
return "clamp_to_border";
case RARCH_WRAP_EDGE:
return "clamp_to_edge";
case RARCH_WRAP_REPEAT:
return "repeat";
case RARCH_WRAP_MIRRORED_REPEAT:
return "mirrored_repeat";
default:
break;
}
return "???";
}
/**
* wrap_str_to_mode:
* @type : Wrap type in human-readable string format.
*
* Translates wrap mode from human-readable string to enum mode value.
*
* Returns: enum mode value of wrap type.
**/
static enum gfx_wrap_type wrap_str_to_mode(const char *wrap_mode)
{
if (string_is_equal(wrap_mode, "clamp_to_border"))
return RARCH_WRAP_BORDER;
else if (string_is_equal(wrap_mode, "clamp_to_edge"))
return RARCH_WRAP_EDGE;
else if (string_is_equal(wrap_mode, "repeat"))
return RARCH_WRAP_REPEAT;
else if (string_is_equal(wrap_mode, "mirrored_repeat"))
return RARCH_WRAP_MIRRORED_REPEAT;
RARCH_WARN("Invalid wrapping type %s. Valid ones are: clamp_to_border"
" (default), clamp_to_edge, repeat and mirrored_repeat. Falling back to default.\n",
wrap_mode);
return RARCH_WRAP_DEFAULT;
}
/**
* video_shader_parse_pass:
* @conf : Preset file to read from.
* @pass : Shader passes handle.
* @i : Index of shader pass.
*
* Parses shader pass from preset file.
*
* Returns: true (1) if successful, otherwise false (0).
**/
static bool video_shader_parse_pass(config_file_t *conf,
struct video_shader_pass *pass, unsigned i)
{
char shader_name[64];
char filter_name_buf[64];
char wrap_name_buf[64];
char wrap_mode[64];
char frame_count_mod_buf[64];
char srgb_output_buf[64];
char fp_fbo_buf[64];
char mipmap_buf[64];
char alias_buf[64];
char scale_name_buf[64];
char attr_name_buf[64];
char scale_type[64];
char scale_type_x[64];
char scale_type_y[64];
char frame_count_mod[64];
size_t path_size = PATH_MAX_LENGTH * sizeof(char);
char *tmp_str = (char*)malloc(PATH_MAX_LENGTH * sizeof(char));
char *tmp_path = NULL;
struct gfx_fbo_scale *scale = NULL;
bool tmp_bool = false;
float fattr = 0.0f;
int iattr = 0;
fp_fbo_buf[0] = mipmap_buf[0] = alias_buf[0] =
scale_name_buf[0] = attr_name_buf[0] = scale_type[0] =
scale_type_x[0] = scale_type_y[0] = frame_count_mod[0] =
tmp_str[0] = shader_name[0] = filter_name_buf[0] =
wrap_name_buf[0] = wrap_mode[0] = frame_count_mod_buf[0] = '\0';
srgb_output_buf[0] = '\0';
/* Source */
snprintf(shader_name, sizeof(shader_name), "shader%u", i);
if (!config_get_path(conf, shader_name, tmp_str, path_size))
{
RARCH_ERR("Couldn't parse shader source (%s).\n", shader_name);
goto error;
}
tmp_path = (char*)malloc(PATH_MAX_LENGTH * sizeof(char));
strlcpy(tmp_path, tmp_str, path_size);
path_resolve_realpath(tmp_path, path_size);
if (!filestream_exists(tmp_path))
strlcpy(pass->source.path, tmp_str, sizeof(pass->source.path));
else
strlcpy(pass->source.path, tmp_path, sizeof(pass->source.path));
free(tmp_path);
/* Smooth */
snprintf(filter_name_buf, sizeof(filter_name_buf), "filter_linear%u", i);
if (config_get_bool(conf, filter_name_buf, &tmp_bool))
{
bool smooth = tmp_bool;
pass->filter = smooth ? RARCH_FILTER_LINEAR : RARCH_FILTER_NEAREST;
}
else
pass->filter = RARCH_FILTER_UNSPEC;
/* Wrapping mode */
snprintf(wrap_name_buf, sizeof(wrap_name_buf), "wrap_mode%u", i);
if (config_get_array(conf, wrap_name_buf, wrap_mode, sizeof(wrap_mode)))
pass->wrap = wrap_str_to_mode(wrap_mode);
/* Frame count mod */
snprintf(frame_count_mod_buf, sizeof(frame_count_mod_buf), "frame_count_mod%u", i);
if (config_get_array(conf, frame_count_mod_buf,
frame_count_mod, sizeof(frame_count_mod)))
pass->frame_count_mod = (unsigned)strtoul(frame_count_mod, NULL, 0);
/* FBO types and mipmapping */
snprintf(srgb_output_buf, sizeof(srgb_output_buf), "srgb_framebuffer%u", i);
if (config_get_bool(conf, srgb_output_buf, &tmp_bool))
pass->fbo.srgb_fbo = tmp_bool;
snprintf(fp_fbo_buf, sizeof(fp_fbo_buf), "float_framebuffer%u", i);
if (config_get_bool(conf, fp_fbo_buf, &tmp_bool))
pass->fbo.fp_fbo = tmp_bool;
snprintf(mipmap_buf, sizeof(mipmap_buf), "mipmap_input%u", i);
if (config_get_bool(conf, mipmap_buf, &tmp_bool))
pass->mipmap = tmp_bool;
snprintf(alias_buf, sizeof(alias_buf), "alias%u", i);
if (!config_get_array(conf, alias_buf, pass->alias, sizeof(pass->alias)))
*pass->alias = '\0';
/* Scale */
scale = &pass->fbo;
snprintf(scale_name_buf, sizeof(scale_name_buf), "scale_type%u", i);
config_get_array(conf, scale_name_buf, scale_type, sizeof(scale_type));
snprintf(scale_name_buf, sizeof(scale_name_buf), "scale_type_x%u", i);
config_get_array(conf, scale_name_buf, scale_type_x, sizeof(scale_type_x));
snprintf(scale_name_buf, sizeof(scale_name_buf), "scale_type_y%u", i);
config_get_array(conf, scale_name_buf, scale_type_y, sizeof(scale_type_y));
if (!*scale_type && !*scale_type_x && !*scale_type_y)
return true;
if (*scale_type)
{
strlcpy(scale_type_x, scale_type, sizeof(scale_type_x));
strlcpy(scale_type_y, scale_type, sizeof(scale_type_y));
}
scale->valid = true;
scale->type_x = RARCH_SCALE_INPUT;
scale->type_y = RARCH_SCALE_INPUT;
scale->scale_x = 1.0;
scale->scale_y = 1.0;
if (*scale_type_x)
{
if (string_is_equal(scale_type_x, "source"))
scale->type_x = RARCH_SCALE_INPUT;
else if (string_is_equal(scale_type_x, "viewport"))
scale->type_x = RARCH_SCALE_VIEWPORT;
else if (string_is_equal(scale_type_x, "absolute"))
scale->type_x = RARCH_SCALE_ABSOLUTE;
else
{
RARCH_ERR("Invalid attribute.\n");
goto error;
}
}
if (*scale_type_y)
{
if (string_is_equal(scale_type_y, "source"))
scale->type_y = RARCH_SCALE_INPUT;
else if (string_is_equal(scale_type_y, "viewport"))
scale->type_y = RARCH_SCALE_VIEWPORT;
else if (string_is_equal(scale_type_y, "absolute"))
scale->type_y = RARCH_SCALE_ABSOLUTE;
else
{
RARCH_ERR("Invalid attribute.\n");
goto error;
}
}
snprintf(attr_name_buf, sizeof(attr_name_buf), "scale%u", i);
if (scale->type_x == RARCH_SCALE_ABSOLUTE)
{
if (config_get_int(conf, attr_name_buf, &iattr))
scale->abs_x = iattr;
else
{
snprintf(attr_name_buf, sizeof(attr_name_buf), "scale_x%u", i);
if (config_get_int(conf, attr_name_buf, &iattr))
scale->abs_x = iattr;
}
}
else
{
if (config_get_float(conf, attr_name_buf, &fattr))
scale->scale_x = fattr;
else
{
snprintf(attr_name_buf, sizeof(attr_name_buf), "scale_x%u", i);
if (config_get_float(conf, attr_name_buf, &fattr))
scale->scale_x = fattr;
}
}
snprintf(attr_name_buf, sizeof(attr_name_buf), "scale%u", i);
if (scale->type_y == RARCH_SCALE_ABSOLUTE)
{
if (config_get_int(conf, attr_name_buf, &iattr))
scale->abs_y = iattr;
else
{
snprintf(attr_name_buf, sizeof(attr_name_buf), "scale_y%u", i);
if (config_get_int(conf, attr_name_buf, &iattr))
scale->abs_y = iattr;
}
}
else
{
if (config_get_float(conf, attr_name_buf, &fattr))
scale->scale_y = fattr;
else
{
snprintf(attr_name_buf, sizeof(attr_name_buf), "scale_y%u", i);
if (config_get_float(conf, attr_name_buf, &fattr))
scale->scale_y = fattr;
}
}
free(tmp_str);
return true;
error:
free(tmp_str);
return false;
}
/**
* video_shader_parse_textures:
* @conf : Preset file to read from.
* @shader : Shader pass handle.
*
* Parses shader textures.
*
* Returns: true (1) if successful, otherwise false (0).
**/
static bool video_shader_parse_textures(config_file_t *conf,
struct video_shader *shader)
{
size_t path_size = PATH_MAX_LENGTH * sizeof(char);
const char *id = NULL;
char *save = NULL;
char *textures = (char*)malloc(1024 * sizeof(char));
textures[0] = '\0';
if (!config_get_array(conf, "textures", textures, 1024 * sizeof(char)))
{
free(textures);
return true;
}
for (id = strtok_r(textures, ";", &save);
id && shader->luts < GFX_MAX_TEXTURES;
shader->luts++, id = strtok_r(NULL, ";", &save))
{
char id_filter[64];
char id_wrap[64];
char wrap_mode[64];
char id_mipmap[64];
bool mipmap = false;
bool smooth = false;
char *tmp_path = NULL;
id_filter[0] = id_wrap[0] = wrap_mode[0] = id_mipmap[0] = '\0';
if (!config_get_array(conf, id, shader->lut[shader->luts].path,
sizeof(shader->lut[shader->luts].path)))
{
RARCH_ERR("Cannot find path to texture \"%s\" ...\n", id);
free(textures);
return false;
}
tmp_path = (char*)malloc(PATH_MAX_LENGTH * sizeof(char));
tmp_path[0] = '\0';
strlcpy(tmp_path, shader->lut[shader->luts].path,
path_size);
path_resolve_realpath(tmp_path, path_size);
if (filestream_exists(tmp_path))
strlcpy(shader->lut[shader->luts].path,
tmp_path, sizeof(shader->lut[shader->luts].path));
free(tmp_path);
strlcpy(shader->lut[shader->luts].id, id,
sizeof(shader->lut[shader->luts].id));
snprintf(id_filter, sizeof(id_filter), "%s_linear", id);
if (config_get_bool(conf, id_filter, &smooth))
shader->lut[shader->luts].filter = smooth ?
RARCH_FILTER_LINEAR : RARCH_FILTER_NEAREST;
else
shader->lut[shader->luts].filter = RARCH_FILTER_UNSPEC;
snprintf(id_wrap, sizeof(id_wrap), "%s_wrap_mode", id);
if (config_get_array(conf, id_wrap, wrap_mode, sizeof(wrap_mode)))
shader->lut[shader->luts].wrap = wrap_str_to_mode(wrap_mode);
snprintf(id_mipmap, sizeof(id_mipmap), "%s_mipmap", id);
if (config_get_bool(conf, id_mipmap, &mipmap))
shader->lut[shader->luts].mipmap = mipmap;
else
shader->lut[shader->luts].mipmap = false;
}
free(textures);
return true;
}
/**
* video_shader_parse_find_parameter:
* @params : Shader parameter handle.
* @num_params : Number of shader params in @params.
* @id : Identifier to search for.
*
* Finds a shader parameter with identifier @id in @params..
*
* Returns: handle to shader parameter if successful, otherwise NULL.
**/
static struct video_shader_parameter *video_shader_parse_find_parameter(
struct video_shader_parameter *params,
unsigned num_params, const char *id)
{
unsigned i;
for (i = 0; i < num_params; i++)
{
if (string_is_equal(params[i].id, id))
return ¶ms[i];
}
return NULL;
}
/**
* video_shader_set_current_parameters:
* @conf : Preset file to read from.
* @shader : Shader passes handle.
*
* Reads the current value for all parameters from config file.
*
* Returns: true (1) if successful, otherwise false (0).
**/
bool video_shader_resolve_current_parameters(config_file_t *conf,
struct video_shader *shader)
{
size_t param_size = 4096 * sizeof(char);
const char *id = NULL;
char *parameters = NULL;
char *save = NULL;
if (!conf)
return false;
parameters = (char*)malloc(4096 * sizeof(char));
parameters[0] = '\0';
/* Read in parameters which override the defaults. */
if (!config_get_array(conf, "parameters",
parameters, param_size))
{
free(parameters);
return true;
}
for (id = strtok_r(parameters, ";", &save); id;
id = strtok_r(NULL, ";", &save))
{
struct video_shader_parameter *parameter =
(struct video_shader_parameter*)
video_shader_parse_find_parameter(
shader->parameters, shader->num_parameters, id);
if (!parameter)
{
RARCH_WARN("[CGP/GLSLP]: Parameter %s is set in the preset,"
" but no shader uses this parameter, ignoring.\n", id);
continue;
}
if (!config_get_float(conf, id, ¶meter->current))
RARCH_WARN("[CGP/GLSLP]: Parameter %s is not set in preset.\n", id);
}
free(parameters);
return true;
}
/**
* video_shader_resolve_parameters:
* @conf : Preset file to read from.
* @shader : Shader passes handle.
*
* Resolves all shader parameters belonging to shaders.
*
* Returns: true (1) if successful, otherwise false (0).
**/
bool video_shader_resolve_parameters(config_file_t *conf,
struct video_shader *shader)
{
unsigned i;
struct video_shader_parameter *param = &shader->parameters[0];
shader->num_parameters = 0;
/* Find all parameters in our shaders. */
for (i = 0; i < shader->passes; i++)
{
intfstream_t *file = NULL;
size_t line_size = 4096 * sizeof(char);
char *line = NULL;
const char *path = shader->pass[i].source.path;
if (string_is_empty(path))
continue;
#if defined(HAVE_SLANG) && defined(HAVE_SPIRV_CROSS)
/* First try to use the more robust slang
* implementation to support #includes. */
/* FIXME: The check for slang can be removed
* if it's sufficiently tested for
* GLSL/Cg as well, it should be the same implementation. */
if (string_is_equal(path_get_extension(path), "slang") &&
slang_preprocess_parse_parameters(path, shader))
continue;
/* If that doesn't work, fallback to the old path.
* Ideally, we'd get rid of this path sooner or later. */
#endif
file = intfstream_open_file(path,
RETRO_VFS_FILE_ACCESS_READ,
RETRO_VFS_FILE_ACCESS_HINT_NONE);
if (!file)
continue;
line = (char*)malloc(4096 * sizeof(char));
line[0] = '\0';
while (shader->num_parameters < ARRAY_SIZE(shader->parameters)
&& intfstream_gets(file, line, line_size))
{
int ret = sscanf(line,
"#pragma parameter %63s \"%63[^\"]\" %f %f %f %f",
param->id, param->desc, ¶m->initial,
¶m->minimum, ¶m->maximum, ¶m->step);
if (ret < 5)
continue;
param->id[63] = '\0';
param->desc[63] = '\0';
if (ret == 5)
param->step = 0.1f * (param->maximum - param->minimum);
RARCH_LOG("Found #pragma parameter %s (%s) %f %f %f %f\n",
param->desc, param->id, param->initial,
param->minimum, param->maximum, param->step);
param->current = param->initial;
shader->num_parameters++;
param++;
}
free(line);
intfstream_close(file);
free(file);
}
if (conf && !video_shader_resolve_current_parameters(conf, shader))
return false;
return true;
}
/**
* video_shader_parse_imports:
* @conf : Preset file to read from.
* @shader : Shader passes handle.
*
* Resolves import parameters belonging to shaders.
*
* Returns: true (1) if successful, otherwise false (0).
**/
static bool video_shader_parse_imports(config_file_t *conf,
struct video_shader *shader)
{
size_t path_size = PATH_MAX_LENGTH * sizeof(char);
const char *id = NULL;
char *save = NULL;
char *tmp_str = NULL;
char *imports = (char*)malloc(1024 * sizeof(char));
imports[0] = '\0';
if (!config_get_array(conf, "imports", imports,
1024 * sizeof(char)))
{
free(imports);
return true;
}
for (id = strtok_r(imports, ";", &save);
id && shader->variables < GFX_MAX_VARIABLES;
shader->variables++, id = strtok_r(NULL, ";", &save))
{
char semantic_buf[64];
char wram_buf[64];
char input_slot_buf[64];
char mask_buf[64];
char equal_buf[64];
char semantic[64];
unsigned addr = 0;
unsigned mask = 0;
unsigned equal = 0;
struct state_tracker_uniform_info *var =
&shader->variable[shader->variables];
semantic_buf[0] = wram_buf[0] = input_slot_buf[0] =
mask_buf[0] = equal_buf[0] = semantic[0] = '\0';
strlcpy(var->id, id, sizeof(var->id));
snprintf(semantic_buf, sizeof(semantic_buf), "%s_semantic", id);
if (!config_get_array(conf, semantic_buf, semantic, sizeof(semantic)))
{
RARCH_ERR("No semantic for import variable.\n");
goto error;
}
snprintf(wram_buf, sizeof(wram_buf), "%s_wram", id);
snprintf(input_slot_buf, sizeof(input_slot_buf), "%s_input_slot", id);
snprintf(mask_buf, sizeof(mask_buf), "%s_mask", id);
snprintf(equal_buf, sizeof(equal_buf), "%s_equal", id);
if (string_is_equal(semantic, "capture"))
var->type = RARCH_STATE_CAPTURE;
else if (string_is_equal(semantic, "transition"))
var->type = RARCH_STATE_TRANSITION;
else if (string_is_equal(semantic, "transition_count"))
var->type = RARCH_STATE_TRANSITION_COUNT;
else if (string_is_equal(semantic, "capture_previous"))
var->type = RARCH_STATE_CAPTURE_PREV;
else if (string_is_equal(semantic, "transition_previous"))
var->type = RARCH_STATE_TRANSITION_PREV;
else if (string_is_equal(semantic, "python"))
var->type = RARCH_STATE_PYTHON;
else
{
RARCH_ERR("Invalid semantic.\n");
goto error;
}
if (var->type != RARCH_STATE_PYTHON)
{
unsigned input_slot = 0;
if (config_get_uint(conf, input_slot_buf, &input_slot))
{
switch (input_slot)
{
case 1:
var->ram_type = RARCH_STATE_INPUT_SLOT1;
break;
case 2:
var->ram_type = RARCH_STATE_INPUT_SLOT2;
break;
default:
RARCH_ERR("Invalid input slot for import.\n");
goto error;
}
}
else if (config_get_hex(conf, wram_buf, &addr))
{
var->ram_type = RARCH_STATE_WRAM;
var->addr = addr;
}
else
{
RARCH_ERR("No address assigned to semantic.\n");
goto error;
}
}
if (config_get_hex(conf, mask_buf, &mask))
var->mask = mask;
if (config_get_hex(conf, equal_buf, &equal))
var->equal = equal;
}
tmp_str = (char*)malloc(PATH_MAX_LENGTH * sizeof(char));
tmp_str[0] = '\0';
if (config_get_path(conf, "import_script", tmp_str, path_size))
strlcpy(shader->script_path, tmp_str, sizeof(shader->script_path));
config_get_array(conf, "import_script_class",
shader->script_class, sizeof(shader->script_class));
free(tmp_str);
free(imports);
return true;
error:
free(imports);
return false;
}
/**
* video_shader_read_conf_cgp:
* @conf : Preset file to read from.
* @shader : Shader passes handle.
*
* Loads preset file and all associated state (passes,
* textures, imports, etc).
*
* Returns: true (1) if successful, otherwise false (0).
**/
bool video_shader_read_conf_cgp(config_file_t *conf,
struct video_shader *shader)
{
unsigned i;
union string_list_elem_attr attr;
unsigned shaders = 0;
settings_t *settings = config_get_ptr();
struct string_list *file_list = NULL;
(void)file_list;
memset(shader, 0, sizeof(*shader));
shader->type = RARCH_SHADER_CG;
if (!config_get_uint(conf, "shaders", &shaders))
{
RARCH_ERR("Cannot find \"shaders\" param.\n");
return false;
}
if (!shaders)
{
RARCH_ERR("Need to define at least 1 shader.\n");
return false;
}
if (!config_get_int(conf, "feedback_pass",
&shader->feedback_pass))
shader->feedback_pass = -1;
shader->passes = MIN(shaders, GFX_MAX_SHADERS);
attr.i = 0;
if (settings->bools.video_shader_watch_files)
{
if (file_change_data)
frontend_driver_watch_path_for_changes(NULL,
0, &file_change_data);
file_change_data = NULL;
file_list = string_list_new();
string_list_append(file_list, conf->path, attr);
}
for (i = 0; i < shader->passes; i++)
{
if (!video_shader_parse_pass(conf, &shader->pass[i], i))
{
if (file_list)
string_list_free(file_list);
return false;
}
if (settings->bools.video_shader_watch_files)
string_list_append(file_list,
shader->pass[i].source.path, attr);
}
if (settings->bools.video_shader_watch_files)
{
int flags = PATH_CHANGE_TYPE_MODIFIED |
PATH_CHANGE_TYPE_WRITE_FILE_CLOSED |
PATH_CHANGE_TYPE_FILE_MOVED |
PATH_CHANGE_TYPE_FILE_DELETED;
frontend_driver_watch_path_for_changes(file_list,
flags, &file_change_data);
string_list_free(file_list);
}
if (!video_shader_parse_textures(conf, shader))
return false;
if (!video_shader_parse_imports(conf, shader))
return false;
return true;
}
/* CGP store */
static const char *scale_type_to_str(enum gfx_scale_type type)
{
switch (type)
{
case RARCH_SCALE_INPUT:
return "source";
case RARCH_SCALE_VIEWPORT:
return "viewport";
case RARCH_SCALE_ABSOLUTE:
return "absolute";
default:
break;
}
return "?";
}
static void shader_write_scale_dim(config_file_t *conf,
const char *dim,
enum gfx_scale_type type, float scale,
unsigned absolute, unsigned i)
{
char key[64];
key[0] = '\0';
snprintf(key, sizeof(key), "scale_type_%s%u", dim, i);
config_set_string(conf, key, scale_type_to_str(type));
snprintf(key, sizeof(key), "scale_%s%u", dim, i);
if (type == RARCH_SCALE_ABSOLUTE)
config_set_int(conf, key, absolute);
else
config_set_float(conf, key, scale);
}
static void shader_write_fbo(config_file_t *conf,
const struct gfx_fbo_scale *fbo, unsigned i)
{
char key[64];
key[0] = '\0';
snprintf(key, sizeof(key), "float_framebuffer%u", i);
config_set_bool(conf, key, fbo->fp_fbo);
snprintf(key, sizeof(key), "srgb_framebuffer%u", i);
config_set_bool(conf, key, fbo->srgb_fbo);
if (!fbo->valid)
return;
shader_write_scale_dim(conf, "x", fbo->type_x,
fbo->scale_x, fbo->abs_x, i);
shader_write_scale_dim(conf, "y", fbo->type_y,
fbo->scale_y, fbo->abs_y, i);
}
/**
* import_semantic_to_string:
* @type : Import semantic type from state tracker.
*
* Translates import semantic to human-readable string identifier.
*
* Returns: human-readable string identifier of import semantic.
**/
static const char *import_semantic_to_str(enum state_tracker_type type)
{
switch (type)
{
case RARCH_STATE_CAPTURE:
return "capture";
case RARCH_STATE_TRANSITION:
return "transition";
case RARCH_STATE_TRANSITION_COUNT:
return "transition_count";
case RARCH_STATE_CAPTURE_PREV:
return "capture_previous";
case RARCH_STATE_TRANSITION_PREV:
return "transition_previous";
case RARCH_STATE_PYTHON:
return "python";
default:
break;
}
return "?";
}
/**
* shader_write_variable:
* @conf : Preset file to read from.
* @info : State tracker uniform info handle.
*
* Writes variable to shader preset file.
**/
static void shader_write_variable(config_file_t *conf,
const struct state_tracker_uniform_info *info)
{
char semantic_buf[64];
char wram_buf[64];
char input_slot_buf[64];
char mask_buf[64];
char equal_buf[64];
const char *id = info->id;
semantic_buf[0] = wram_buf[0] = input_slot_buf[0] =
mask_buf[0] = equal_buf[0] = '\0';
snprintf(semantic_buf, sizeof(semantic_buf), "%s_semantic", id);
snprintf(wram_buf, sizeof(wram_buf), "%s_wram", id);
snprintf(input_slot_buf, sizeof(input_slot_buf), "%s_input_slot", id);
snprintf(mask_buf, sizeof(mask_buf), "%s_mask", id);
snprintf(equal_buf, sizeof(equal_buf), "%s_equal", id);
config_set_string(conf, semantic_buf,
import_semantic_to_str(info->type));
config_set_hex(conf, mask_buf, info->mask);
config_set_hex(conf, equal_buf, info->equal);
switch (info->ram_type)
{
case RARCH_STATE_INPUT_SLOT1:
config_set_int(conf, input_slot_buf, 1);
break;
case RARCH_STATE_INPUT_SLOT2:
config_set_int(conf, input_slot_buf, 2);
break;
case RARCH_STATE_WRAM:
config_set_hex(conf, wram_buf, info->addr);
break;
case RARCH_STATE_NONE:
break;
}
}
/**
* video_shader_write_conf_cgp:
* @conf : Preset file to read from.
* @shader : Shader passes handle.
*
* Saves preset and all associated state (passes,
* textures, imports, etc) to disk.
**/
void video_shader_write_conf_cgp(config_file_t *conf,
struct video_shader *shader)
{
unsigned i;
config_set_int(conf, "shaders", shader->passes);
if (shader->feedback_pass >= 0)
config_set_int(conf, "feedback_pass", shader->feedback_pass);
for (i = 0; i < shader->passes; i++)
{
char key[64];
size_t tmp_size = PATH_MAX_LENGTH * sizeof(char);
char *tmp = (char*)malloc(PATH_MAX_LENGTH * sizeof(char));
const struct video_shader_pass *pass = &shader->pass[i];
key[0] = '\0';
snprintf(key, sizeof(key), "shader%u", i);
strlcpy(tmp, pass->source.path, tmp_size);
if (!path_is_absolute(tmp))
path_resolve_realpath(tmp, tmp_size);
config_set_string(conf, key, tmp);
free(tmp);
if (pass->filter != RARCH_FILTER_UNSPEC)
{
snprintf(key, sizeof(key), "filter_linear%u", i);
config_set_bool(conf, key, pass->filter == RARCH_FILTER_LINEAR);
}
snprintf(key, sizeof(key), "wrap_mode%u", i);
config_set_string(conf, key, wrap_mode_to_str(pass->wrap));
if (pass->frame_count_mod)
{
snprintf(key, sizeof(key), "frame_count_mod%u", i);
config_set_int(conf, key, pass->frame_count_mod);
}
snprintf(key, sizeof(key), "mipmap_input%u", i);
config_set_bool(conf, key, pass->mipmap);
snprintf(key, sizeof(key), "alias%u", i);
config_set_string(conf, key, pass->alias);
shader_write_fbo(conf, &pass->fbo, i);
}
if (shader->num_parameters)
{
size_t param_size = 4096 * sizeof(char);
char *parameters = (char*)malloc(4096 * sizeof(char));
parameters[0] = '\0';
strlcpy(parameters, shader->parameters[0].id, param_size);
for (i = 1; i < shader->num_parameters; i++)
{
/* O(n^2), but number of parameters is very limited. */
strlcat(parameters, ";", param_size);
strlcat(parameters, shader->parameters[i].id, param_size);
}
config_set_string(conf, "parameters", parameters);