-
-
Notifications
You must be signed in to change notification settings - Fork 132
Expand file tree
/
Copy pathrenderer.c
More file actions
4439 lines (3798 loc) · 157 KB
/
Copy pathrenderer.c
File metadata and controls
4439 lines (3798 loc) · 157 KB
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
/*
* This file is part of libplacebo.
*
* libplacebo is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* libplacebo 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with libplacebo. If not, see <http://www.gnu.org/licenses/>.
*/
#include <math.h>
#include "common.h"
#include "filters.h"
#include "hash.h"
#include "shaders.h"
#include "dispatch.h"
#include <libplacebo/renderer.h>
struct cached_frame {
uint64_t signature;
uint64_t params_hash; // for detecting `pl_render_params` changes
struct pl_color_space color;
struct pl_color_repr repr;
struct pl_icc_profile profile;
pl_rect2df crop;
pl_tex tex;
int comps;
bool evict; // for garbage collection
};
struct sampler {
pl_shader_obj upscaler_state;
pl_shader_obj downscaler_state;
};
struct osd_vertex {
float pos[2];
float coord[2];
float color[4];
};
struct icc_state {
pl_icc_object icc;
uint64_t error; // set to profile signature on failure
};
struct pl_renderer_t {
pl_gpu gpu;
pl_dispatch dp;
pl_log log;
// Cached feature checks (inverted)
enum pl_render_error errors;
// List containing signatures of disabled hooks
PL_ARRAY(uint64_t) disabled_hooks;
// Shader resource objects and intermediate textures (FBOs)
pl_shader_obj tone_map_state;
pl_shader_obj dither_state;
pl_shader_obj grain_state[4];
pl_shader_obj lut_state[3];
pl_shader_obj icc_state[2];
PL_ARRAY(pl_tex) fbos[PL_RENDER_STAGE_COUNT];
struct sampler sampler_main;
struct sampler sampler_contrast;
struct sampler samplers_src[4];
struct sampler samplers_dst[4];
struct sampler samplers_el[4];
// Temporary storage for vertex/index data
PL_ARRAY(struct osd_vertex) osd_vertices;
PL_ARRAY(uint32_t) osd_indices;
struct pl_vertex_attrib osd_attribs[3];
// Frame cache (for frame mixing / interpolation)
PL_ARRAY(struct cached_frame) frames;
PL_ARRAY(pl_tex) frame_fbos;
// For debugging / logging purposes
int prev_dither;
// For backwards compatibility
struct icc_state icc_fallback[2];
};
enum {
// Index into `lut_state`
LUT_IMAGE,
LUT_TARGET,
LUT_PARAMS,
};
enum {
// Index into `icc_state`
ICC_IMAGE,
ICC_TARGET
};
pl_renderer pl_renderer_create(pl_log log, pl_gpu gpu)
{
pl_renderer rr = pl_alloc_ptr(NULL, rr);
*rr = (struct pl_renderer_t) {
.gpu = gpu,
.log = log,
.dp = pl_dispatch_create(log, gpu),
.osd_attribs = {
{
.name = "pos",
.offset = offsetof(struct osd_vertex, pos),
.fmt = pl_find_vertex_fmt(gpu, PL_FMT_FLOAT, 2),
}, {
.name = "coord",
.offset = offsetof(struct osd_vertex, coord),
.fmt = pl_find_vertex_fmt(gpu, PL_FMT_FLOAT, 2),
}, {
.name = "osd_color",
.offset = offsetof(struct osd_vertex, color),
.fmt = pl_find_vertex_fmt(gpu, PL_FMT_FLOAT, 4),
}
},
};
assert(rr->dp);
return rr;
}
static void sampler_destroy(pl_renderer rr, struct sampler *sampler)
{
pl_shader_obj_destroy(&sampler->upscaler_state);
pl_shader_obj_destroy(&sampler->downscaler_state);
}
void pl_renderer_destroy(pl_renderer *p_rr)
{
pl_renderer rr = *p_rr;
if (!rr)
return;
// Free all intermediate FBOs
for (int n = 0; n < PL_ARRAY_SIZE(rr->fbos); n++) {
for (int i = 0; i < rr->fbos[n].num; i++)
pl_tex_destroy(rr->gpu, &rr->fbos[n].elem[i]);
}
for (int i = 0; i < rr->frames.num; i++)
pl_tex_destroy(rr->gpu, &rr->frames.elem[i].tex);
for (int i = 0; i < rr->frame_fbos.num; i++)
pl_tex_destroy(rr->gpu, &rr->frame_fbos.elem[i]);
// Free all shader resource objects
pl_shader_obj_destroy(&rr->tone_map_state);
pl_shader_obj_destroy(&rr->dither_state);
for (int i = 0; i < PL_ARRAY_SIZE(rr->lut_state); i++)
pl_shader_obj_destroy(&rr->lut_state[i]);
for (int i = 0; i < PL_ARRAY_SIZE(rr->grain_state); i++)
pl_shader_obj_destroy(&rr->grain_state[i]);
for (int i = 0; i < PL_ARRAY_SIZE(rr->icc_state); i++)
pl_shader_obj_destroy(&rr->icc_state[i]);
// Free all samplers
sampler_destroy(rr, &rr->sampler_main);
sampler_destroy(rr, &rr->sampler_contrast);
for (int i = 0; i < PL_ARRAY_SIZE(rr->samplers_src); i++)
sampler_destroy(rr, &rr->samplers_src[i]);
for (int i = 0; i < PL_ARRAY_SIZE(rr->samplers_dst); i++)
sampler_destroy(rr, &rr->samplers_dst[i]);
for (int i = 0; i < PL_ARRAY_SIZE(rr->samplers_el); i++)
sampler_destroy(rr, &rr->samplers_el[i]);
// Free fallback ICC profiles
for (int i = 0; i < PL_ARRAY_SIZE(rr->icc_fallback); i++)
pl_icc_close(&rr->icc_fallback[i].icc);
pl_dispatch_destroy(&rr->dp);
pl_free_ptr(p_rr);
}
size_t pl_renderer_save(pl_renderer rr, uint8_t *out)
{
return pl_cache_save(pl_gpu_cache(rr->gpu), out, out ? SIZE_MAX : 0);
}
void pl_renderer_load(pl_renderer rr, const uint8_t *cache)
{
pl_cache_load(pl_gpu_cache(rr->gpu), cache, SIZE_MAX);
}
void pl_renderer_flush_cache(pl_renderer rr)
{
for (int i = 0; i < rr->frames.num; i++)
pl_tex_destroy(rr->gpu, &rr->frames.elem[i].tex);
rr->frames.num = 0;
pl_reset_detected_peak(rr->tone_map_state);
}
const struct pl_render_params pl_render_fast_params = { PL_RENDER_DEFAULTS };
const struct pl_render_params pl_render_default_params = {
PL_RENDER_DEFAULTS
.upscaler = &pl_filter_lanczos,
.downscaler = &pl_filter_hermite,
.frame_mixer = &pl_filter_oversample,
.sigmoid_params = &pl_sigmoid_default_params,
.dither_params = &pl_dither_default_params,
.peak_detect_params = &pl_peak_detect_default_params,
};
const struct pl_render_params pl_render_high_quality_params = {
PL_RENDER_DEFAULTS
.upscaler = &pl_filter_ewa_lanczossharp,
.downscaler = &pl_filter_hermite,
.frame_mixer = &pl_filter_oversample,
.sigmoid_params = &pl_sigmoid_default_params,
.peak_detect_params = &pl_peak_detect_high_quality_params,
.color_map_params = &pl_color_map_high_quality_params,
.dither_params = &pl_dither_default_params,
.deband_params = &pl_deband_default_params,
};
const struct pl_filter_preset pl_frame_mixers[] = {
{ "none", NULL, "No frame mixing" },
{ "linear", &pl_filter_bilinear, "Linear frame mixing" },
{ "oversample", &pl_filter_oversample, "Oversample (AKA SmoothMotion)" },
{ "mitchell_clamp", &pl_filter_mitchell_clamp, "Clamped Mitchell spline" },
{ "hermite", &pl_filter_hermite, "Cubic spline (Hermite)" },
{0}
};
const int pl_num_frame_mixers = PL_ARRAY_SIZE(pl_frame_mixers) - 1;
const struct pl_filter_preset pl_scale_filters[] = {
{"none", NULL, "Built-in sampling"},
{"oversample", &pl_filter_oversample, "Oversample (Aspect-preserving NN)"},
COMMON_FILTER_PRESETS,
{0}
};
const int pl_num_scale_filters = PL_ARRAY_SIZE(pl_scale_filters) - 1;
// Represents a "in-flight" image, which is either a shader that's in the
// process of producing some sort of image, or a texture that needs to be
// sampled from
struct img {
// Effective texture size, always set
int w, h;
// Recommended format (falls back to fbofmt otherwise), only for shaders
pl_fmt fmt;
// Exactly *one* of these two is set:
pl_shader sh;
pl_tex tex;
// If true, created shaders will be set to unique
bool unique;
// Information about what to log/disable/fallback to if the shader fails
const char *err_msg;
enum pl_render_error err_enum;
pl_tex err_tex;
// Current effective source area, will be sampled by the main scaler
pl_rect2df rect;
// The current effective colorspace
struct pl_color_repr repr;
struct pl_color_space color;
int comps;
};
// Plane 'type', ordered by incrementing priority
enum plane_type {
PLANE_INVALID = 0,
PLANE_ALPHA,
PLANE_CHROMA,
PLANE_LUMA,
PLANE_RGB,
PLANE_XYZ,
};
static inline enum plane_type detect_plane_type(const struct pl_plane *plane,
const struct pl_color_repr *repr)
{
if (pl_color_system_is_ycbcr_like(repr->sys)) {
int t = PLANE_INVALID;
for (int c = 0; c < plane->components; c++) {
switch (plane->component_mapping[c]) {
case PL_CHANNEL_Y: t = PL_MAX(t, PLANE_LUMA); continue;
case PL_CHANNEL_A: t = PL_MAX(t, PLANE_ALPHA); continue;
case PL_CHANNEL_CB:
case PL_CHANNEL_CR:
t = PL_MAX(t, PLANE_CHROMA);
continue;
default: continue;
}
}
pl_assert(t);
return t;
}
// Extra test for exclusive / separated alpha plane
if (plane->components == 1 && plane->component_mapping[0] == PL_CHANNEL_A)
return PLANE_ALPHA;
switch (repr->sys) {
case PL_COLOR_SYSTEM_UNKNOWN: // fall through to RGB
case PL_COLOR_SYSTEM_RGB: return PLANE_RGB;
case PL_COLOR_SYSTEM_XYZ: return PLANE_XYZ;
// For the switch completeness check
case PL_COLOR_SYSTEM_BT_601:
case PL_COLOR_SYSTEM_BT_709:
case PL_COLOR_SYSTEM_SMPTE_240M:
case PL_COLOR_SYSTEM_BT_2020_NC:
case PL_COLOR_SYSTEM_BT_2020_C:
case PL_COLOR_SYSTEM_BT_2100_PQ:
case PL_COLOR_SYSTEM_BT_2100_HLG:
case PL_COLOR_SYSTEM_DOLBYVISION:
case PL_COLOR_SYSTEM_YCGCO:
case PL_COLOR_SYSTEM_YCGCO_RE:
case PL_COLOR_SYSTEM_YCGCO_RO:
case PL_COLOR_SYSTEM_COUNT:
break;
}
pl_unreachable();
}
struct pass_state {
void *tmp;
pl_renderer rr;
const struct pl_render_params *params;
struct pl_render_info info; // for info callback
// Represents the "current" image which we're in the process of rendering.
// This is initially set by pass_read_image, and all of the subsequent
// rendering steps will mutate this in-place.
struct img img;
// Represents the "reference rect". Canonically, this is functionally
// equivalent to `image.crop`, but also updates as the refplane evolves
// (e.g. due to user hook prescalers)
pl_rect2df ref_rect;
// Integer version of `target.crop`. Semantically identical.
pl_rect2d dst_rect;
// Logical end-to-end rotation
pl_rotation rotation;
// Cached copies of the `image` / `target` for this rendering pass,
// corrected to make sure all rects etc. are properly defaulted/inferred.
struct pl_frame image;
struct pl_frame target;
// Cached copies of the `prev` / `next` frames, for deinterlacing.
struct pl_frame prev, next;
// Cached copy of the `image->enhancement_layer`, so we can acquire/release it.
struct pl_frame enhancement_layer;
// Some extra plane metadata, inferred from `planes`
enum plane_type src_type[4];
int src_ref, dst_ref; // index into `planes`
// Metadata for `rr->fbos`
pl_fmt fbofmt[5];
bool *fbos_used;
bool need_peak_fbo; // need indirection for peak detection
// Map of acquired frames
struct {
bool target, image, prev, next, enhancement_layer;
} acquired;
};
static void find_fbo_format(struct pass_state *pass)
{
const struct pl_render_params *params = pass->params;
pl_renderer rr = pass->rr;
if (params->disable_fbos || (rr->errors & PL_RENDER_ERR_FBO) || pass->fbofmt[4])
return;
struct {
enum pl_fmt_type type;
int depth;
enum pl_fmt_caps caps;
} configs[] = {
// Prefer floating point formats first
{PL_FMT_FLOAT, 16, PL_FMT_CAP_LINEAR},
{PL_FMT_FLOAT, 16, PL_FMT_CAP_SAMPLEABLE},
// Otherwise, fall back to unorm/snorm, preferring linearly sampleable
{PL_FMT_UNORM, 16, PL_FMT_CAP_LINEAR},
{PL_FMT_SNORM, 16, PL_FMT_CAP_LINEAR},
{PL_FMT_UNORM, 16, PL_FMT_CAP_SAMPLEABLE},
{PL_FMT_SNORM, 16, PL_FMT_CAP_SAMPLEABLE},
// As a final fallback, allow 8-bit FBO formats (for UNORM only)
{PL_FMT_UNORM, 8, PL_FMT_CAP_LINEAR},
{PL_FMT_UNORM, 8, PL_FMT_CAP_SAMPLEABLE},
};
pl_fmt fmt = NULL;
for (int i = 0; i < PL_ARRAY_SIZE(configs); i++) {
if (params->force_low_bit_depth_fbos && configs[i].depth > 8)
continue;
fmt = pl_find_fmt(rr->gpu, configs[i].type, 4, configs[i].depth, 0,
PL_FMT_CAP_RENDERABLE | configs[i].caps);
if (!fmt)
continue;
pass->fbofmt[4] = fmt;
// Probe the right variant for each number of channels, falling
// back to the next biggest format
for (int c = 3; c >= 1; c--) {
pass->fbofmt[c] = pl_find_fmt(rr->gpu, configs[i].type, c,
configs[i].depth, 0, fmt->caps);
pass->fbofmt[c] = PL_DEF(pass->fbofmt[c], pass->fbofmt[c+1]);
}
return;
}
PL_WARN(rr, "Found no renderable FBO format! Most features disabled");
rr->errors |= PL_RENDER_ERR_FBO;
}
static void info_callback(void *priv, const struct pl_dispatch_info *dinfo)
{
struct pass_state *pass = priv;
const struct pl_render_params *params = pass->params;
if (!params->info_callback)
return;
pass->info.pass = dinfo;
params->info_callback(params->info_priv, &pass->info);
pass->info.index++;
}
static pl_tex get_fbo(struct pass_state *pass, int w, int h, pl_fmt fmt,
int comps, pl_debug_tag debug_tag)
{
pl_renderer rr = pass->rr;
const int n = pass->info.stage;
comps = PL_DEF(comps, 4);
fmt = PL_DEF(fmt, pass->fbofmt[comps]);
if (!fmt)
return NULL;
pl_assert(w && h);
struct pl_tex_params params = {
.w = w,
.h = h,
.format = fmt,
.sampleable = true,
.renderable = true,
.blit_src = fmt->caps & PL_FMT_CAP_BLITTABLE,
.storable = fmt->caps & PL_FMT_CAP_STORABLE,
.debug_tag = debug_tag,
};
int best_idx = -1;
int best_diff = 0;
// Find the best-fitting texture out of rr->fbos
for (int i = 0; i < rr->fbos[n].num; i++) {
if (pass->fbos_used[i])
continue;
// Orthogonal distance, with penalty for format mismatches
int diff = abs(rr->fbos[n].elem[i]->params.w - w) +
abs(rr->fbos[n].elem[i]->params.h - h) +
((rr->fbos[n].elem[i]->params.format != fmt) ? 1000 : 0);
if (best_idx < 0 || diff < best_diff) {
best_idx = i;
best_diff = diff;
}
}
// No texture found at all, add a new one
if (best_idx < 0) {
best_idx = rr->fbos[n].num;
PL_ARRAY_APPEND(rr, rr->fbos[n], NULL);
pl_grow(pass->tmp, &pass->fbos_used, rr->fbos[n].num * sizeof(bool));
pass->fbos_used[best_idx] = false;
}
if (!pl_tex_recreate(rr->gpu, &rr->fbos[n].elem[best_idx], ¶ms))
return NULL;
pass->fbos_used[best_idx] = true;
return rr->fbos[n].elem[best_idx];
}
// Forcibly convert an img to `tex`, dispatching where necessary
static pl_tex _img_tex(struct pass_state *pass, struct img *img, pl_debug_tag tag)
{
if (img->tex) {
pl_assert(!img->sh);
return img->tex;
}
pl_renderer rr = pass->rr;
pl_tex tex = get_fbo(pass, img->w, img->h, img->fmt, img->comps, tag);
img->fmt = NULL;
if (!tex) {
PL_ERR(rr, "Failed creating FBO texture! Disabling advanced rendering..");
memset(pass->fbofmt, 0, sizeof(pass->fbofmt));
pl_dispatch_abort(rr->dp, &img->sh);
rr->errors |= PL_RENDER_ERR_FBO;
return img->err_tex;
}
pl_assert(img->sh);
bool ok = pl_dispatch_finish(rr->dp, pl_dispatch_params(
.shader = &img->sh,
.target = tex,
));
const char *err_msg = img->err_msg;
enum pl_render_error err_enum = img->err_enum;
pl_tex err_tex = img->err_tex;
img->err_msg = NULL;
img->err_enum = PL_RENDER_ERR_NONE;
img->err_tex = NULL;
if (!ok) {
PL_ERR(rr, "%s", PL_DEF(err_msg, "Failed dispatching intermediate pass!"));
rr->errors |= err_enum;
img->sh = pl_dispatch_begin(rr->dp);
img->tex = err_tex;
return img->tex;
}
img->tex = tex;
return img->tex;
}
#define img_tex(pass, img) _img_tex(pass, img, PL_DEBUG_TAG)
// Forcibly convert an img to `sh`, sampling where necessary
static pl_shader img_sh(struct pass_state *pass, struct img *img)
{
if (img->sh) {
pl_assert(!img->tex);
return img->sh;
}
pl_assert(img->tex);
img->sh = pl_dispatch_begin_ex(pass->rr->dp, img->unique);
pl_shader_sample_direct(img->sh, pl_sample_src( .tex = img->tex ));
img->tex = NULL;
return img->sh;
}
enum sampler_type {
SAMPLER_DIRECT, // pick based on texture caps
SAMPLER_NEAREST, // direct sampling, force nearest
SAMPLER_BICUBIC, // fast bicubic scaling
SAMPLER_HERMITE, // fast hermite scaling
SAMPLER_GAUSSIAN, // fast gaussian scaling
SAMPLER_COMPLEX, // complex custom filters
SAMPLER_OVERSAMPLE,
};
enum sampler_dir {
SAMPLER_NOOP, // 1:1 scaling
SAMPLER_UP, // upscaling
SAMPLER_DOWN, // downscaling
};
enum sampler_usage {
SAMPLER_MAIN,
SAMPLER_PLANE,
SAMPLER_LOWPASS,
};
struct sampler_info {
const struct pl_filter_config *config; // if applicable
enum sampler_usage usage;
enum sampler_type type;
enum sampler_dir dir;
enum sampler_dir dir_sep[2];
};
static struct sampler_info sample_src_info(struct pass_state *pass,
const struct pl_sample_src *src,
enum sampler_usage usage)
{
const struct pl_render_params *params = pass->params;
struct sampler_info info = { .usage = usage };
pl_renderer rr = pass->rr;
float rx = src->new_w / fabsf(pl_rect_w(src->rect));
if (rx < 1.0 - 1e-6) {
info.dir_sep[0] = SAMPLER_DOWN;
} else if (rx > 1.0 + 1e-6) {
info.dir_sep[0] = SAMPLER_UP;
}
float ry = src->new_h / fabsf(pl_rect_h(src->rect));
if (ry < 1.0 - 1e-6) {
info.dir_sep[1] = SAMPLER_DOWN;
} else if (ry > 1.0 + 1e-6) {
info.dir_sep[1] = SAMPLER_UP;
}
if (params->correct_subpixel_offsets) {
if (!info.dir_sep[0] && fabsf(src->rect.x0) > 1e-6f)
info.dir_sep[0] = SAMPLER_UP;
if (!info.dir_sep[1] && fabsf(src->rect.y0) > 1e-6f)
info.dir_sep[1] = SAMPLER_UP;
}
// We use PL_MAX so downscaling overrides upscaling when choosing scalers
info.dir = PL_MAX(info.dir_sep[0], info.dir_sep[1]);
switch (info.dir) {
case SAMPLER_DOWN:
if (usage == SAMPLER_LOWPASS) {
info.config = &pl_filter_bicubic;
} else if (usage == SAMPLER_PLANE && params->plane_downscaler) {
info.config = params->plane_downscaler;
} else {
info.config = params->downscaler;
}
break;
case SAMPLER_UP:
if (usage == SAMPLER_PLANE && params->plane_upscaler) {
info.config = params->plane_upscaler;
} else {
pl_assert(usage != SAMPLER_LOWPASS);
info.config = params->upscaler;
}
break;
case SAMPLER_NOOP:
info.type = SAMPLER_NEAREST;
return info;
}
if ((rr->errors & PL_RENDER_ERR_SAMPLING) || !info.config) {
info.type = SAMPLER_DIRECT;
} else if (info.config->kernel == &pl_filter_function_oversample) {
info.type = SAMPLER_OVERSAMPLE;
} else {
info.type = SAMPLER_COMPLEX;
// Try using faster replacements for GPU built-in scalers
pl_fmt texfmt = src->tex ? src->tex->params.format : pass->fbofmt[4];
bool can_linear = texfmt->caps & PL_FMT_CAP_LINEAR;
bool can_fast = info.dir == SAMPLER_UP || params->skip_anti_aliasing;
if (can_fast && !params->disable_builtin_scalers) {
if (can_linear && pl_filter_config_eq(info.config, &pl_filter_bicubic))
info.type = SAMPLER_BICUBIC;
if (can_linear && pl_filter_config_eq(info.config, &pl_filter_hermite))
info.type = SAMPLER_HERMITE;
if (can_linear && pl_filter_config_eq(info.config, &pl_filter_gaussian))
info.type = SAMPLER_GAUSSIAN;
if (can_linear && pl_filter_config_eq(info.config, &pl_filter_bilinear))
info.type = SAMPLER_DIRECT;
if (pl_filter_config_eq(info.config, &pl_filter_nearest))
info.type = can_linear ? SAMPLER_NEAREST : SAMPLER_DIRECT;
}
}
// Disable advanced scaling without FBOs
if (!pass->fbofmt[4] && info.type == SAMPLER_COMPLEX)
info.type = SAMPLER_DIRECT;
return info;
}
static void dispatch_sampler(struct pass_state *pass, pl_shader sh,
struct sampler *sampler, enum sampler_usage usage,
pl_tex target_tex, const struct pl_sample_src *src)
{
const struct pl_render_params *params = pass->params;
if (!sampler)
goto fallback;
pl_renderer rr = pass->rr;
struct sampler_info info = sample_src_info(pass, src, usage);
pl_shader_obj *lut = NULL;
switch (info.dir) {
case SAMPLER_NOOP:
goto fallback;
case SAMPLER_DOWN:
lut = &sampler->downscaler_state;
break;
case SAMPLER_UP:
lut = &sampler->upscaler_state;
break;
}
switch (info.type) {
case SAMPLER_DIRECT:
goto fallback;
case SAMPLER_NEAREST:
pl_shader_sample_nearest(sh, src);
return;
case SAMPLER_OVERSAMPLE:
pl_shader_sample_oversample(sh, src, info.config->kernel->params[0]);
return;
case SAMPLER_BICUBIC:
pl_shader_sample_bicubic(sh, src);
return;
case SAMPLER_HERMITE:
pl_shader_sample_hermite(sh, src);
return;
case SAMPLER_GAUSSIAN:
pl_shader_sample_gaussian(sh, src);
return;
case SAMPLER_COMPLEX:
break; // continue below
}
pl_assert(lut);
struct pl_sample_filter_params fparams = {
.filter = *info.config,
.antiring = params->antiringing_strength,
.no_widening = params->skip_anti_aliasing && usage != SAMPLER_LOWPASS,
.lut = lut,
};
if (target_tex) {
fparams.no_compute = !target_tex->params.storable;
} else {
fparams.no_compute = !(pass->fbofmt[4]->caps & PL_FMT_CAP_STORABLE);
}
bool ok;
if (info.config->polar) {
// Polar samplers are always a single function call
ok = pl_shader_sample_polar(sh, src, &fparams);
} else if (info.dir_sep[0] && info.dir_sep[1]) {
// Scaling is needed in both directions
struct pl_sample_src src1 = *src, src2 = *src;
src1.new_w = src->tex->params.w;
src1.rect.x0 = 0;
src1.rect.x1 = src1.new_w;;
src2.rect.y0 = 0;
src2.rect.y1 = src1.new_h;
pl_shader tsh = pl_dispatch_begin(rr->dp);
ok = pl_shader_sample_ortho2(tsh, &src1, &fparams);
if (!ok) {
pl_dispatch_abort(rr->dp, &tsh);
goto done;
}
struct img img = {
.sh = tsh,
.w = src1.new_w,
.h = src1.new_h,
.comps = src->components,
};
src2.tex = img_tex(pass, &img);
src2.scale = 1.0;
ok = src2.tex && pl_shader_sample_ortho2(sh, &src2, &fparams);
} else {
// Scaling is needed only in one direction
ok = pl_shader_sample_ortho2(sh, src, &fparams);
}
done:
if (!ok) {
PL_ERR(rr, "Failed dispatching scaler.. disabling");
rr->errors |= PL_RENDER_ERR_SAMPLING;
goto fallback;
}
return;
fallback:
// If all else fails, fall back to auto sampling
pl_shader_sample_direct(sh, src);
}
// also clamps to the range implied by `repr`
static void swizzle_color(pl_shader sh, int comps, const int comp_map[4],
bool force_alpha, const struct pl_color_repr *repr)
{
ident_t orig = sh_fresh(sh, "orig_color");
GLSL("vec4 "$" = color; \n"
"color = vec4(0.0, 0.0, 0.0, 1.0); \n", orig);
float min[4], max[4];
pl_color_repr_limits(repr, min, max);
static const int def_map[4] = {0, 1, 2, 3};
comp_map = PL_DEF(comp_map, def_map);
for (int c = 0; c < comps; c++) {
const int idx = comp_map[c];
if (idx < 0)
continue;
GLSL("color[%d] = clamp("$"[%d], "$", "$"); \n",
c, orig, idx, SH_FLOAT(min[idx]), SH_FLOAT(max[idx]));
}
if (force_alpha)
GLSL("color.a = clamp("$".a, 0.0, 1.0); \n", orig);
}
// `scale` adapts from `pass->dst_rect` to the plane being rendered to
static void draw_overlays(struct pass_state *pass, pl_tex fbo,
int comps, const int comp_map[4],
const struct pl_overlay *overlays, int num,
struct pl_color_space color,
const struct pl_color_repr repr,
const pl_transform2x2 *output_shift)
{
pl_renderer rr = pass->rr;
if (num <= 0 || (rr->errors & PL_RENDER_ERR_OVERLAY))
return;
enum pl_fmt_caps caps = fbo->params.format->caps;
if (!(rr->errors & PL_RENDER_ERR_BLENDING) &&
!(caps & PL_FMT_CAP_BLENDABLE))
{
PL_WARN(rr, "Trying to draw an overlay to a non-blendable target. "
"Alpha blending is disabled, results may be incorrect!");
rr->errors |= PL_RENDER_ERR_BLENDING;
}
const struct pl_frame *image = pass->src_ref >= 0 ? &pass->image : NULL;
pl_transform2x2 src_to_dst;
if (image) {
float rx = pl_rect_w(pass->dst_rect) / pl_rect_w(image->crop);
float ry = pl_rect_h(pass->dst_rect) / pl_rect_h(image->crop);
src_to_dst = (pl_transform2x2) {
.mat.m = {{ rx, 0 }, { 0, ry }},
.c = {
pass->dst_rect.x0 - rx * image->crop.x0,
pass->dst_rect.y0 - ry * image->crop.y0,
},
};
if (pass->rotation % PL_ROTATION_180 == PL_ROTATION_90) {
PL_SWAP(src_to_dst.c[0], src_to_dst.c[1]);
src_to_dst.mat = (pl_matrix2x2) {{{ 0, ry }, { rx, 0 }}};
}
}
const struct pl_frame *target = &pass->target;
pl_rect2df dst_crop = target->crop;
pl_rect2df_rotate(&dst_crop, -pass->rotation);
pl_rect2df_normalize(&dst_crop);
for (int n = 0; n < num; n++) {
struct pl_overlay ol = overlays[n];
if (!ol.num_parts)
continue;
if (!ol.coords) {
ol.coords = overlays == target->overlays
? PL_OVERLAY_COORDS_DST_FRAME
: PL_OVERLAY_COORDS_SRC_FRAME;
}
pl_transform2x2 tf = pl_transform2x2_identity;
switch (ol.coords) {
case PL_OVERLAY_COORDS_SRC_CROP:
if (!image)
continue;
tf.c[0] = image->crop.x0;
tf.c[1] = image->crop.y0;
// fall through
case PL_OVERLAY_COORDS_SRC_FRAME:
if (!image)
continue;
pl_transform2x2_rmul(&src_to_dst, &tf);
break;
case PL_OVERLAY_COORDS_DST_CROP:
tf.c[0] = dst_crop.x0;
tf.c[1] = dst_crop.y0;
break;
case PL_OVERLAY_COORDS_DST_FRAME:
break;
case PL_OVERLAY_COORDS_AUTO:
case PL_OVERLAY_COORDS_COUNT:
pl_unreachable();
}
if (output_shift)
pl_transform2x2_rmul(output_shift, &tf);
// Construct vertex/index buffers
rr->osd_vertices.num = 0;
rr->osd_indices.num = 0;
for (int i = 0; i < ol.num_parts; i++) {
const struct pl_overlay_part *part = &ol.parts[i];
#define EMIT_VERT(x, y) \
do { \
float pos[2] = { part->dst.x, part->dst.y }; \
pl_transform2x2_apply(&tf, pos); \
PL_ARRAY_APPEND(rr, rr->osd_vertices, (struct osd_vertex) { \
.pos = { \
2.0 * (pos[0] / fbo->params.w) - 1.0, \
2.0 * (pos[1] / fbo->params.h) - 1.0, \
}, \
.coord = { \
part->src.x / ol.tex->params.w, \
part->src.y / ol.tex->params.h, \
}, \
.color = { \
part->color[0], part->color[1], \
part->color[2], part->color[3], \
}, \
}); \
} while (0)
int idx_base = rr->osd_vertices.num;
EMIT_VERT(x0, y0); // idx 0: top left
EMIT_VERT(x1, y0); // idx 1: top right
EMIT_VERT(x0, y1); // idx 2: bottom left
EMIT_VERT(x1, y1); // idx 3: bottom right
PL_ARRAY_APPEND(rr, rr->osd_indices, idx_base + 0);
PL_ARRAY_APPEND(rr, rr->osd_indices, idx_base + 1);
PL_ARRAY_APPEND(rr, rr->osd_indices, idx_base + 2);
PL_ARRAY_APPEND(rr, rr->osd_indices, idx_base + 2);
PL_ARRAY_APPEND(rr, rr->osd_indices, idx_base + 1);
PL_ARRAY_APPEND(rr, rr->osd_indices, idx_base + 3);
}
// Draw parts
pl_shader sh = pl_dispatch_begin(rr->dp);
ident_t tex = sh_desc(sh, (struct pl_shader_desc) {
.desc = {
.name = "osd_tex",
.type = PL_DESC_SAMPLED_TEX,
},
.binding = {
.object = ol.tex,
.sample_mode = (ol.tex->params.format->caps & PL_FMT_CAP_LINEAR)
? PL_TEX_SAMPLE_LINEAR
: PL_TEX_SAMPLE_NEAREST,
},
});
sh_describe(sh, "overlay");
GLSL("// overlay \n");
switch (ol.mode) {
case PL_OVERLAY_NORMAL:
GLSL("vec4 color = textureLod("$", coord, 0.0); \n", tex);
break;
case PL_OVERLAY_MONOCHROME:
GLSL("vec4 color = osd_color; \n");
break;
case PL_OVERLAY_MODE_COUNT:
pl_unreachable();
};
static const struct pl_color_map_params osd_params = {
PL_COLOR_MAP_DEFAULTS
.tone_mapping_function = &pl_tone_map_linear,
.gamut_mapping = &pl_gamut_map_saturation,
};
sh->output = PL_SHADER_SIG_COLOR;
pl_shader_decode_color_ex(sh, pl_color_decode_args( .repr = &ol.repr ));
if (target->icc)
color.transfer = PL_COLOR_TRC_LINEAR;
// Copy overlay color to infer it only if matching with the target video
struct pl_color_space ol_color = ol.color;
struct pl_color_space target_color = pass->target.color;
pl_color_space_infer_map(&ol_color, &target_color);
if (image && pl_color_space_equal(&ol_color, &image->color)) {
const struct pl_color_map_params *cmp = pass->params->color_map_params;
pl_shader_color_map_ex(sh, cmp, pl_color_map_args(
.src = ol_color,
.dst = color,
.state = &rr->tone_map_state,
));
} else {
pl_shader_color_map_ex(sh, &osd_params, pl_color_map_args(ol.color, color));
}
if (target->icc)
pl_icc_encode(sh, target->icc, &rr->icc_state[ICC_TARGET]);