forked from haasn/libplacebo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpu_tests.h
1675 lines (1438 loc) · 59.1 KB
/
gpu_tests.h
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
#include "tests.h"
#include "shaders.h"
#include <libplacebo/renderer.h>
#include <libplacebo/utils/frame_queue.h>
#include <libplacebo/utils/upload.h>
static void pl_buffer_tests(pl_gpu gpu)
{
const size_t buf_size = 1024;
if (buf_size > gpu->limits.max_buf_size)
return;
uint8_t *test_src = malloc(buf_size * 2);
uint8_t *test_dst = test_src + buf_size;
assert(test_src && test_dst);
memset(test_dst, 0, buf_size);
for (int i = 0; i < buf_size; i++)
test_src[i] = RANDOM_U8;
pl_buf buf = NULL, tbuf = NULL;
printf("test buffer static creation and readback\n");
buf = pl_buf_create(gpu, pl_buf_params(
.size = buf_size,
.host_readable = true,
.initial_data = test_src,
));
REQUIRE(buf);
REQUIRE(pl_buf_read(gpu, buf, 0, test_dst, buf_size));
REQUIRE_MEMEQ(test_src, test_dst, buf_size);
pl_buf_destroy(gpu, &buf);
printf("test buffer empty creation, update and readback\n");
memset(test_dst, 0, buf_size);
buf = pl_buf_create(gpu, pl_buf_params(
.size = buf_size,
.host_writable = true,
.host_readable = true,
));
REQUIRE(buf);
pl_buf_write(gpu, buf, 0, test_src, buf_size);
REQUIRE(pl_buf_read(gpu, buf, 0, test_dst, buf_size));
REQUIRE_MEMEQ(test_src, test_dst, buf_size);
pl_buf_destroy(gpu, &buf);
printf("test buffer-buffer copy and readback\n");
memset(test_dst, 0, buf_size);
buf = pl_buf_create(gpu, pl_buf_params(
.size = buf_size,
.initial_data = test_src,
));
tbuf = pl_buf_create(gpu, pl_buf_params(
.size = buf_size,
.host_readable = true,
));
REQUIRE(buf && tbuf);
pl_buf_copy(gpu, tbuf, 0, buf, 0, buf_size);
REQUIRE(pl_buf_read(gpu, tbuf, 0, test_dst, buf_size));
REQUIRE_MEMEQ(test_src, test_dst, buf_size);
pl_buf_destroy(gpu, &buf);
pl_buf_destroy(gpu, &tbuf);
if (buf_size <= gpu->limits.max_mapped_size) {
printf("test host mapped buffer readback\n");
buf = pl_buf_create(gpu, pl_buf_params(
.size = buf_size,
.host_mapped = true,
.initial_data = test_src,
));
REQUIRE(buf);
REQUIRE(!pl_buf_poll(gpu, buf, 0));
REQUIRE_MEMEQ(test_src, buf->data, buf_size);
pl_buf_destroy(gpu, &buf);
}
// `compute_queues` check is to exclude dummy GPUs here
if (buf_size <= gpu->limits.max_ssbo_size && gpu->limits.compute_queues)
{
printf("test endian swapping\n");
buf = pl_buf_create(gpu, pl_buf_params(
.size = buf_size,
.storable = true,
.initial_data = test_src,
));
tbuf = pl_buf_create(gpu, pl_buf_params(
.size = buf_size,
.storable = true,
.host_readable = true,
));
REQUIRE(buf && tbuf);
REQUIRE(pl_buf_copy_swap(gpu, &(struct pl_buf_copy_swap_params) {
.src = buf,
.dst = tbuf,
.size = buf_size,
.wordsize = 2,
}));
REQUIRE(pl_buf_read(gpu, tbuf, 0, test_dst, buf_size));
for (int i = 0; i < buf_size / 2; i++) {
REQUIRE_CMP(test_src[2 * i + 0], ==, test_dst[2 * i + 1], PRIu8);
REQUIRE_CMP(test_src[2 * i + 1], ==, test_dst[2 * i + 0], PRIu8);
}
// test endian swap in-place
REQUIRE(pl_buf_copy_swap(gpu, &(struct pl_buf_copy_swap_params) {
.src = tbuf,
.dst = tbuf,
.size = buf_size,
.wordsize = 4,
}));
REQUIRE(pl_buf_read(gpu, tbuf, 0, test_dst, buf_size));
for (int i = 0; i < buf_size / 4; i++) {
REQUIRE_CMP(test_src[4 * i + 0], ==, test_dst[4 * i + 2], PRIu8);
REQUIRE_CMP(test_src[4 * i + 1], ==, test_dst[4 * i + 3], PRIu8);
REQUIRE_CMP(test_src[4 * i + 2], ==, test_dst[4 * i + 0], PRIu8);
REQUIRE_CMP(test_src[4 * i + 3], ==, test_dst[4 * i + 1], PRIu8);
}
pl_buf_destroy(gpu, &buf);
pl_buf_destroy(gpu, &tbuf);
}
free(test_src);
}
static void test_cb(void *priv)
{
bool *flag = priv;
*flag = true;
}
static void pl_test_roundtrip(pl_gpu gpu, pl_tex tex[2],
uint8_t *src, uint8_t *dst)
{
if (!tex[0] || !tex[1]) {
printf("failed creating test textures... skipping this test\n");
return;
}
int texels = tex[0]->params.w;
texels *= tex[0]->params.h ? tex[0]->params.h : 1;
texels *= tex[0]->params.d ? tex[0]->params.d : 1;
pl_fmt fmt = tex[0]->params.format;
size_t bytes = texels * fmt->texel_size;
memset(src, 0, bytes);
memset(dst, 0, bytes);
for (size_t i = 0; i < bytes; i++)
src[i] = RANDOM_U8;
pl_timer ul, dl;
ul = pl_timer_create(gpu);
dl = pl_timer_create(gpu);
bool ran_ul = false, ran_dl = false;
REQUIRE(pl_tex_upload(gpu, &(struct pl_tex_transfer_params){
.tex = tex[0],
.ptr = src,
.timer = ul,
.callback = gpu->limits.callbacks ? test_cb : NULL,
.priv = &ran_ul,
}));
// Test blitting, if possible for this format
pl_tex dst_tex = tex[0];
if (tex[0]->params.blit_src && tex[1]->params.blit_dst) {
pl_tex_clear_ex(gpu, tex[1], (union pl_clear_color){0}); // for testing
pl_tex_blit(gpu, &(struct pl_tex_blit_params) {
.src = tex[0],
.dst = tex[1],
});
dst_tex = tex[1];
}
REQUIRE(pl_tex_download(gpu, &(struct pl_tex_transfer_params){
.tex = dst_tex,
.ptr = dst,
.timer = dl,
.callback = gpu->limits.callbacks ? test_cb : NULL,
.priv = &ran_dl,
}));
pl_gpu_finish(gpu);
if (gpu->limits.callbacks)
REQUIRE(ran_ul && ran_dl);
if (fmt->emulated && fmt->type == PL_FMT_FLOAT) {
// TODO: can't memcmp here because bits might be lost due to the
// emulated 16/32 bit upload paths, figure out a better way to
// generate data and verify the roundtrip!
} else {
REQUIRE_MEMEQ(src, dst, bytes);
}
// Report timer results
printf("upload time: %"PRIu64", download time: %"PRIu64"\n",
pl_timer_query(gpu, ul), pl_timer_query(gpu, dl));
pl_timer_destroy(gpu, &ul);
pl_timer_destroy(gpu, &dl);
}
static void pl_texture_tests(pl_gpu gpu)
{
const size_t max_size = 16*16*16 * 4 *sizeof(double);
uint8_t *test_src = malloc(max_size * 2);
uint8_t *test_dst = test_src + max_size;
for (int f = 0; f < gpu->num_formats; f++) {
pl_fmt fmt = gpu->formats[f];
if (fmt->opaque || !(fmt->caps & PL_FMT_CAP_HOST_READABLE))
continue;
printf("testing texture roundtrip for format %s\n", fmt->name);
assert(fmt->texel_size <= 4 * sizeof(double));
struct pl_tex_params ref_params = {
.format = fmt,
.blit_src = (fmt->caps & PL_FMT_CAP_BLITTABLE),
.blit_dst = (fmt->caps & PL_FMT_CAP_BLITTABLE),
.host_writable = true,
.host_readable = true,
.debug_tag = PL_DEBUG_TAG,
};
pl_tex tex[2];
if (gpu->limits.max_tex_1d_dim >= 16) {
printf("... 1D\n");
struct pl_tex_params params = ref_params;
params.w = 16;
if (!gpu->limits.blittable_1d_3d)
params.blit_src = params.blit_dst = false;
for (int i = 0; i < PL_ARRAY_SIZE(tex); i++)
tex[i] = pl_tex_create(gpu, ¶ms);
pl_test_roundtrip(gpu, tex, test_src, test_dst);
for (int i = 0; i < PL_ARRAY_SIZE(tex); i++)
pl_tex_destroy(gpu, &tex[i]);
}
if (gpu->limits.max_tex_2d_dim >= 16) {
printf("... 2D\n");
struct pl_tex_params params = ref_params;
params.w = params.h = 16;
for (int i = 0; i < PL_ARRAY_SIZE(tex); i++)
tex[i] = pl_tex_create(gpu, ¶ms);
pl_test_roundtrip(gpu, tex, test_src, test_dst);
for (int i = 0; i < PL_ARRAY_SIZE(tex); i++)
pl_tex_destroy(gpu, &tex[i]);
}
if (gpu->limits.max_tex_3d_dim >= 16) {
printf("... 3D\n");
struct pl_tex_params params = ref_params;
params.w = params.h = params.d = 16;
if (!gpu->limits.blittable_1d_3d)
params.blit_src = params.blit_dst = false;
for (int i = 0; i < PL_ARRAY_SIZE(tex); i++)
tex[i] = pl_tex_create(gpu, ¶ms);
pl_test_roundtrip(gpu, tex, test_src, test_dst);
for (int i = 0; i < PL_ARRAY_SIZE(tex); i++)
pl_tex_destroy(gpu, &tex[i]);
}
}
free(test_src);
}
static void pl_planar_tests(pl_gpu gpu)
{
pl_fmt fmt = pl_find_named_fmt(gpu, "g8_b8_r8_420");
if (!fmt)
return;
REQUIRE_CMP(fmt->num_planes, ==, 3, "d");
const int width = 64, height = 32;
pl_tex tex = pl_tex_create(gpu, pl_tex_params(
.w = width,
.h = height,
.format = fmt,
.blit_dst = true,
.host_readable = true,
));
if (!tex)
return;
for (int i = 0; i < fmt->num_planes; i++)
REQUIRE(tex->planes[i]);
pl_tex plane = tex->planes[1];
uint8_t data[(width * height) >> 2];
REQUIRE_CMP(plane->params.w * plane->params.h, ==, PL_ARRAY_SIZE(data), "d");
pl_tex_clear(gpu, plane, (float[]){ 0.5, 0.0, 0.0, 1.0 });
REQUIRE(pl_tex_download(gpu, pl_tex_transfer_params(
.tex = plane,
.ptr = data,
)));
uint8_t ref[PL_ARRAY_SIZE(data)];
memset(ref, 0x80, sizeof(ref));
REQUIRE_MEMEQ(data, ref, PL_ARRAY_SIZE(data));
pl_tex_destroy(gpu, &tex);
}
static void pl_shader_tests(pl_gpu gpu)
{
if (gpu->glsl.version < 410)
return;
const char *vert_shader =
"#version 410 \n"
"layout(location=0) in vec2 vertex_pos; \n"
"layout(location=1) in vec3 vertex_color; \n"
"layout(location=0) out vec3 frag_color; \n"
"void main() { \n"
" gl_Position = vec4(vertex_pos, 0, 1); \n"
" frag_color = vertex_color; \n"
"}";
const char *frag_shader =
"#version 410 \n"
"layout(location=0) in vec3 frag_color; \n"
"layout(location=0) out vec4 out_color; \n"
"void main() { \n"
" out_color = vec4(frag_color, 1.0); \n"
"}";
pl_fmt fbo_fmt;
enum pl_fmt_caps caps = PL_FMT_CAP_RENDERABLE | PL_FMT_CAP_BLITTABLE |
PL_FMT_CAP_LINEAR;
fbo_fmt = pl_find_fmt(gpu, PL_FMT_FLOAT, 4, 16, 32, caps);
if (!fbo_fmt)
return;
#define FBO_W 16
#define FBO_H 16
pl_tex fbo;
fbo = pl_tex_create(gpu, &(struct pl_tex_params) {
.format = fbo_fmt,
.w = FBO_W,
.h = FBO_H,
.renderable = true,
.storable = !!(fbo_fmt->caps & PL_FMT_CAP_STORABLE),
.host_readable = true,
.blit_dst = true,
});
REQUIRE(fbo);
pl_tex_clear_ex(gpu, fbo, (union pl_clear_color){0});
pl_fmt vert_fmt;
vert_fmt = pl_find_vertex_fmt(gpu, PL_FMT_FLOAT, 3);
REQUIRE(vert_fmt);
static const struct vertex { float pos[2]; float color[3]; } vertices[] = {
{{-1.0, -1.0}, {0, 0, 0}},
{{ 1.0, -1.0}, {1, 0, 0}},
{{-1.0, 1.0}, {0, 1, 0}},
{{ 1.0, 1.0}, {1, 1, 0}},
};
pl_pass pass;
pass = pl_pass_create(gpu, &(struct pl_pass_params) {
.type = PL_PASS_RASTER,
.target_format = fbo_fmt,
.vertex_shader = vert_shader,
.glsl_shader = frag_shader,
.vertex_type = PL_PRIM_TRIANGLE_STRIP,
.vertex_stride = sizeof(struct vertex),
.num_vertex_attribs = 2,
.vertex_attribs = (struct pl_vertex_attrib[]) {{
.name = "vertex_pos",
.fmt = pl_find_vertex_fmt(gpu, PL_FMT_FLOAT, 2),
.location = 0,
.offset = offsetof(struct vertex, pos),
}, {
.name = "vertex_color",
.fmt = pl_find_vertex_fmt(gpu, PL_FMT_FLOAT, 3),
.location = 1,
.offset = offsetof(struct vertex, color),
}},
});
REQUIRE(pass);
if (pass->params.cached_program || pass->params.cached_program_len) {
// Ensure both are set if either one is set
REQUIRE(pass->params.cached_program);
REQUIRE(pass->params.cached_program_len);
}
pl_timer timer = pl_timer_create(gpu);
pl_pass_run(gpu, &(struct pl_pass_run_params) {
.pass = pass,
.target = fbo,
.vertex_count = PL_ARRAY_SIZE(vertices),
.vertex_data = vertices,
.timer = timer,
});
// Wait until this pass is complete and report the timer result
pl_gpu_finish(gpu);
printf("timer query result: %"PRIu64"\n", pl_timer_query(gpu, timer));
pl_timer_destroy(gpu, &timer);
static float data[FBO_H * FBO_W * 4] = {0};
// Test against the known pattern of `src`, only useful for roundtrip tests
#define TEST_FBO_PATTERN(eps, fmt, ...) \
do { \
printf("testing pattern of " fmt "\n", __VA_ARGS__); \
REQUIRE(pl_tex_download(gpu, &(struct pl_tex_transfer_params) { \
.tex = fbo, \
.ptr = data, \
})); \
\
for (int y = 0; y < FBO_H; y++) { \
for (int x = 0; x < FBO_W; x++) { \
float *color = &data[(y * FBO_W + x) * 4]; \
REQUIRE_FEQ(color[0], (x + 0.5) / FBO_W, eps); \
REQUIRE_FEQ(color[1], (y + 0.5) / FBO_H, eps); \
REQUIRE_FEQ(color[2], 0.0, eps); \
REQUIRE_FEQ(color[3], 1.0, eps); \
} \
} \
} while (0)
TEST_FBO_PATTERN(1e-6, "%s", "initial rendering");
if (sizeof(vertices) <= gpu->limits.max_vbo_size) {
// Test the use of an explicit vertex buffer
pl_buf vert = pl_buf_create(gpu, &(struct pl_buf_params) {
.size = sizeof(vertices),
.initial_data = vertices,
.drawable = true,
});
REQUIRE(vert);
pl_pass_run(gpu, &(struct pl_pass_run_params) {
.pass = pass,
.target = fbo,
.vertex_count = sizeof(vertices) / sizeof(struct vertex),
.vertex_buf = vert,
.buf_offset = 0,
});
pl_buf_destroy(gpu, &vert);
TEST_FBO_PATTERN(1e-6, "%s", "using vertex buffer");
}
// Test the use of index buffers
static const uint16_t indices[] = { 3, 2, 1, 0 };
pl_pass_run(gpu, &(struct pl_pass_run_params) {
.pass = pass,
.target = fbo,
.vertex_count = PL_ARRAY_SIZE(indices),
.vertex_data = vertices,
.index_data = indices,
});
pl_pass_destroy(gpu, &pass);
TEST_FBO_PATTERN(1e-6, "%s", "using indexed rendering");
// Test the use of pl_dispatch
pl_dispatch dp = pl_dispatch_create(gpu->log, gpu);
pl_shader sh = pl_dispatch_begin(dp);
REQUIRE(pl_shader_custom(sh, &(struct pl_custom_shader) {
.body = "color = vec4(col, 1.0);",
.input = PL_SHADER_SIG_NONE,
.output = PL_SHADER_SIG_COLOR,
}));
REQUIRE(pl_dispatch_vertex(dp, &(struct pl_dispatch_vertex_params) {
.shader = &sh,
.target = fbo,
.vertex_stride = sizeof(struct vertex),
.vertex_position_idx = 0,
.num_vertex_attribs = 2,
.vertex_attribs = (struct pl_vertex_attrib[]) {{
.name = "pos",
.fmt = pl_find_vertex_fmt(gpu, PL_FMT_FLOAT, 2),
.offset = offsetof(struct vertex, pos),
}, {
.name = "col",
.fmt = pl_find_vertex_fmt(gpu, PL_FMT_FLOAT, 3),
.offset = offsetof(struct vertex, color),
}},
.vertex_type = PL_PRIM_TRIANGLE_STRIP,
.vertex_coords = PL_COORDS_NORMALIZED,
.vertex_count = PL_ARRAY_SIZE(vertices),
.vertex_data = vertices,
}));
TEST_FBO_PATTERN(1e-6, "%s", "using custom vertices");
pl_tex src;
src = pl_tex_create(gpu, &(struct pl_tex_params) {
.format = fbo_fmt,
.w = FBO_W,
.h = FBO_H,
.storable = fbo->params.storable,
.sampleable = true,
.initial_data = data,
});
if (fbo->params.storable) {
// Test 1x1 blit, to make sure the scaling code runs
REQUIRE(pl_tex_blit_compute(gpu, &(struct pl_tex_blit_params) {
.src = src,
.dst = fbo,
.src_rc = {0, 0, 0, 1, 1, 1},
.dst_rc = {0, 0, 0, FBO_W, FBO_H, 1},
.sample_mode = PL_TEX_SAMPLE_NEAREST,
}));
// Test non-resizing blit, which uses the efficient imageLoad path
REQUIRE(pl_tex_blit_compute(gpu, &(struct pl_tex_blit_params) {
.src = src,
.dst = fbo,
.src_rc = {0, 0, 0, FBO_W, FBO_H, 1},
.dst_rc = {0, 0, 0, FBO_W, FBO_H, 1},
.sample_mode = PL_TEX_SAMPLE_NEAREST,
}));
TEST_FBO_PATTERN(1e-6, "%s", "pl_tex_blit_compute");
}
// Test encoding/decoding of all gamma functions, color spaces, etc.
for (enum pl_color_transfer trc = 0; trc < PL_COLOR_TRC_COUNT; trc++) {
sh = pl_dispatch_begin(dp);
pl_shader_sample_nearest(sh, pl_sample_src( .tex = src ));
pl_shader_delinearize(sh, pl_color_space( .transfer = trc ));
pl_shader_linearize(sh, pl_color_space( .transfer = trc ));
REQUIRE(pl_dispatch_finish(dp, pl_dispatch_params(
.shader = &sh,
.target = fbo,
)));
float epsilon = pl_color_transfer_is_hdr(trc) ? 1e-4 : 1e-6;
TEST_FBO_PATTERN(epsilon, "transfer function %d", (int) trc);
}
for (enum pl_color_system sys = 0; sys < PL_COLOR_SYSTEM_COUNT; sys++) {
if (sys == PL_COLOR_SYSTEM_DOLBYVISION)
continue; // requires metadata
sh = pl_dispatch_begin(dp);
pl_shader_sample_nearest(sh, pl_sample_src( .tex = src ));
pl_shader_encode_color(sh, &(struct pl_color_repr) { .sys = sys });
pl_shader_decode_color(sh, &(struct pl_color_repr) { .sys = sys }, NULL);
REQUIRE(pl_dispatch_finish(dp, &(struct pl_dispatch_params) {
.shader = &sh,
.target = fbo,
}));
float epsilon;
switch (sys) {
case PL_COLOR_SYSTEM_BT_2020_C:
epsilon = 1e-5;
break;
case PL_COLOR_SYSTEM_BT_2100_PQ:
case PL_COLOR_SYSTEM_BT_2100_HLG:
// These seem to be horrifically noisy and prone to breaking on
// edge cases for some reason
// TODO: figure out why!
continue;
default: epsilon = 1e-6; break;
}
TEST_FBO_PATTERN(epsilon, "color system %d", (int) sys);
}
// Repeat this a few times to test the caching
for (int i = 0; i < 10; i++) {
if (i == 5) {
printf("Recreating pl_dispatch to test the caching\n");
size_t size = pl_dispatch_save(dp, NULL);
REQUIRE(size);
uint8_t *cache = malloc(size);
REQUIRE(cache);
REQUIRE_CMP(pl_dispatch_save(dp, cache), ==, size, "zu");
pl_dispatch_destroy(&dp);
dp = pl_dispatch_create(gpu->log, gpu);
pl_dispatch_load(dp, cache);
#ifndef MSAN
// Test to make sure the pass regenerates the same cache, but skip
// this on MSAN because it doesn't like it when we read from
// program cache data generated by the non-instrumented GPU driver
uint64_t hash = pl_str_hash((pl_str) { cache, size });
REQUIRE_CMP(pl_dispatch_save(dp, NULL), ==, size, "zu");
REQUIRE_CMP(pl_dispatch_save(dp, cache), ==, size, "zu");
REQUIRE_CMP(pl_str_hash((pl_str) { cache, size }), ==, hash, PRIu64);
#endif
free(cache);
}
sh = pl_dispatch_begin(dp);
// For testing, force the use of CS if possible
if (gpu->glsl.compute) {
sh->type = SH_COMPUTE;
sh->res.compute_group_size[0] = 8;
sh->res.compute_group_size[1] = 8;
}
pl_shader_deband(sh, pl_sample_src( .tex = src ), pl_deband_params(
.iterations = 0,
.grain = 0.0,
));
REQUIRE(pl_dispatch_finish(dp, &(struct pl_dispatch_params) {
.shader = &sh,
.target = fbo,
}));
TEST_FBO_PATTERN(1e-6, "deband iter %d", i);
}
// Test peak detection and readback if possible
sh = pl_dispatch_begin(dp);
pl_shader_sample_nearest(sh, pl_sample_src( .tex = src ));
pl_shader_obj peak_state = NULL;
struct pl_color_space csp_gamma22 = { .transfer = PL_COLOR_TRC_GAMMA22 };
struct pl_peak_detect_params peak_params = { .minimum_peak = 0.01 };
if (pl_shader_detect_peak(sh, csp_gamma22, &peak_state, &peak_params)) {
REQUIRE(pl_dispatch_compute(dp, &(struct pl_dispatch_compute_params) {
.shader = &sh,
.width = fbo->params.w,
.height = fbo->params.h,
}));
float peak, avg;
REQUIRE(pl_get_detected_peak(peak_state, &peak, &avg));
float real_peak = 0, real_avg = 0;
for (int y = 0; y < FBO_H; y++) {
for (int x = 0; x < FBO_W; x++) {
float *color = &data[(y * FBO_W + x) * 4];
float smax = powf(PL_MAX(color[0], color[1]), 2.2);
smax = (1 - 1e-3f) * smax + 1e-3f;
float slog = logf(PL_MAX(smax, 0.001));
real_peak = PL_MAX(smax, real_peak);
real_avg += slog;
}
}
real_avg = expf(real_avg / (FBO_W * FBO_H));
REQUIRE_FEQ(peak, real_peak, 1e-4);
REQUIRE_FEQ(avg, real_avg, 1e-3);
}
pl_dispatch_abort(dp, &sh);
pl_shader_obj_destroy(&peak_state);
// Test film grain synthesis
pl_shader_obj grain = NULL;
struct pl_film_grain_params grain_params = {
.tex = src,
.components = 3,
.component_mapping = { 0, 1, 2},
.repr = &(struct pl_color_repr) {
.sys = PL_COLOR_SYSTEM_BT_709,
.levels = PL_COLOR_LEVELS_LIMITED,
.bits = { .color_depth = 10, .sample_depth = 10 },
},
};
for (int i = 0; i < 2; i++) {
grain_params.data.type = PL_FILM_GRAIN_AV1;
grain_params.data.params.av1 = av1_grain_data;
grain_params.data.params.av1.overlap = !!i;
grain_params.data.seed = rand();
sh = pl_dispatch_begin(dp);
pl_shader_film_grain(sh, &grain, &grain_params);
REQUIRE(pl_dispatch_finish(dp, &(struct pl_dispatch_params) {
.shader = &sh,
.target = fbo,
}));
}
if (gpu->glsl.compute) {
grain_params.data.type = PL_FILM_GRAIN_H274;
grain_params.data.params.h274 = h274_grain_data;
grain_params.data.seed = rand();
sh = pl_dispatch_begin(dp);
pl_shader_film_grain(sh, &grain, &grain_params);
REQUIRE(pl_dispatch_finish(dp, &(struct pl_dispatch_params) {
.shader = &sh,
.target = fbo,
}));
}
pl_shader_obj_destroy(&grain);
// Test custom shaders
struct pl_custom_shader custom = {
.header =
"vec3 invert(vec3 color) \n"
"{ \n"
" return vec3(1.0) - color; \n"
"} \n",
.body =
"color = vec4(gl_FragCoord.xy, 0.0, 1.0); \n"
"color.rgb = invert(color.rgb) + offset; \n",
.input = PL_SHADER_SIG_NONE,
.output = PL_SHADER_SIG_COLOR,
.num_variables = 1,
.variables = &(struct pl_shader_var) {
.var = pl_var_float("offset"),
.data = &(float) { 0.1 },
},
};
sh = pl_dispatch_begin(dp);
REQUIRE(pl_shader_custom(sh, &custom));
REQUIRE(pl_dispatch_finish(dp, &(struct pl_dispatch_params) {
.shader = &sh,
.target = fbo,
}));
// Test dolbyvision
if (gpu->glsl.version >= 130) {
struct pl_color_repr repr = {
.sys = PL_COLOR_SYSTEM_DOLBYVISION,
.dovi = &dovi_meta,
};
sh = pl_dispatch_begin(dp);
pl_shader_sample_direct(sh, pl_sample_src( .tex = src ));
pl_shader_decode_color(sh, &repr, NULL);
REQUIRE(pl_dispatch_finish(dp, &(struct pl_dispatch_params) {
.shader = &sh,
.target = fbo,
}));
}
// Test deinterlacing
sh = pl_dispatch_begin(dp);
pl_shader_deinterlace(sh, pl_deinterlace_source( .cur = pl_field_pair(src) ), NULL);
REQUIRE(pl_dispatch_finish(dp, pl_dispatch_params(
.shader = &sh,
.target = fbo,
)));
// Test error diffusion
if (fbo->params.storable) {
for (int i = 0; i < pl_num_error_diffusion_kernels; i++) {
const struct pl_error_diffusion_kernel *k = pl_error_diffusion_kernels[i];
printf("testing error diffusion kernel '%s'\n", k->name);
sh = pl_dispatch_begin(dp);
bool ok = pl_shader_error_diffusion(sh, pl_error_diffusion_params(
.input_tex = src,
.output_tex = fbo,
.new_depth = 8,
.kernel = k,
));
if (!ok) {
fprintf(stderr, "kernel '%s' exceeds GPU limits, skipping...\n", k->name);
continue;
}
REQUIRE(pl_dispatch_compute(dp, pl_dispatch_compute_params(
.shader = &sh,
.dispatch_size = {1, 1, 1},
)));
}
}
pl_dispatch_destroy(&dp);
pl_tex_destroy(gpu, &src);
pl_tex_destroy(gpu, &fbo);
}
static void pl_scaler_tests(pl_gpu gpu)
{
pl_fmt src_fmt = pl_find_fmt(gpu, PL_FMT_FLOAT, 1, 16, 32, PL_FMT_CAP_LINEAR);
pl_fmt fbo_fmt = pl_find_fmt(gpu, PL_FMT_FLOAT, 1, 16, 32, PL_FMT_CAP_RENDERABLE);
if (!src_fmt || !fbo_fmt)
return;
float *fbo_data = NULL;
pl_shader_obj lut = NULL;
static float data_5x5[5][5] = {
{ 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0 },
{ 0, 0, 1, 0, 0 },
{ 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0 },
};
pl_tex dot5x5 = pl_tex_create(gpu, &(struct pl_tex_params) {
.w = 5,
.h = 5,
.format = src_fmt,
.sampleable = true,
.initial_data = &data_5x5[0][0],
});
struct pl_tex_params fbo_params = {
.w = 100,
.h = 100,
.format = fbo_fmt,
.renderable = true,
.storable = fbo_fmt->caps & PL_FMT_CAP_STORABLE,
.host_readable = fbo_fmt->caps & PL_FMT_CAP_HOST_READABLE,
};
pl_tex fbo = pl_tex_create(gpu, &fbo_params);
pl_dispatch dp = pl_dispatch_create(gpu->log, gpu);
if (!dot5x5 || !fbo || !dp)
goto error;
pl_shader sh = pl_dispatch_begin(dp);
REQUIRE(pl_shader_sample_polar(sh,
pl_sample_src(
.tex = dot5x5,
.new_w = fbo->params.w,
.new_h = fbo->params.h,
),
pl_sample_filter_params(
.filter = pl_filter_ewa_lanczos,
.lut = &lut,
.no_compute = !fbo->params.storable,
)
));
REQUIRE(pl_dispatch_finish(dp, &(struct pl_dispatch_params) {
.shader = &sh,
.target = fbo,
}));
if (fbo->params.host_readable) {
fbo_data = malloc(fbo->params.w * fbo->params.h * sizeof(float));
REQUIRE(pl_tex_download(gpu, &(struct pl_tex_transfer_params) {
.tex = fbo,
.ptr = fbo_data,
}));
int max = 255;
printf("P2\n%d %d\n%d\n", fbo->params.w, fbo->params.h, max);
for (int y = 0; y < fbo->params.h; y++) {
for (int x = 0; x < fbo->params.w; x++) {
float v = fbo_data[y * fbo->params.h + x];
printf("%d ", (int) round(fmin(fmax(v, 0.0), 1.0) * max));
}
printf("\n");
}
}
error:
free(fbo_data);
pl_shader_obj_destroy(&lut);
pl_dispatch_destroy(&dp);
pl_tex_destroy(gpu, &dot5x5);
pl_tex_destroy(gpu, &fbo);
}
static const char *user_shader_tests[] = {
// Test hooking, saving and loading
"// Example of a comment at the beginning \n"
" \n"
"//!HOOK NATIVE \n"
"//!DESC upscale image \n"
"//!BIND HOOKED \n"
"//!WIDTH HOOKED.w 10 * \n"
"//!HEIGHT HOOKED.h 10 * \n"
"//!SAVE NATIVEBIG \n"
"//!WHEN NATIVE.w 500 < \n"
" \n"
"vec4 hook() \n"
"{ \n"
" return HOOKED_texOff(0); \n"
"} \n"
" \n"
"//!HOOK MAIN \n"
"//!DESC downscale bigger image \n"
"//!WHEN NATIVE.w 500 < \n"
"//!BIND NATIVEBIG \n"
" \n"
"vec4 hook() \n"
"{ \n"
" return NATIVEBIG_texOff(0); \n"
"} \n",
// Test use of textures
"//!HOOK MAIN \n"
"//!DESC turn everything into colorful pixels \n"
"//!BIND HOOKED \n"
"//!BIND DISCO \n"
"//!COMPONENTS 3 \n"
" \n"
"vec4 hook() \n"
"{ \n"
" return vec4(DISCO_tex(HOOKED_pos * 10.0).rgb, 1); \n"
"} \n"
" \n"
"//!TEXTURE DISCO \n"
"//!SIZE 3 3 \n"
"//!FORMAT rgba32f \n"
"//!FILTER NEAREST \n"
"//!BORDER REPEAT \n"
"0000803f000000000000000000000000000000000000803f0000000000000000000000000"
"00000000000803f00000000000000000000803f0000803f000000000000803f0000000000"
"00803f000000000000803f0000803f00000000000000009a99993e9a99993e9a99993e000"
"000009a99193F9A99193f9a99193f000000000000803f0000803f0000803f00000000 \n",
// Test use of storage/buffer resources
"//!HOOK MAIN \n"
"//!DESC attach some storage objects \n"
"//!BIND tex_storage \n"
"//!BIND buf_uniform \n"
"//!BIND buf_storage \n"
"//!COMPONENTS 4 \n"
" \n"
"vec4 hook() \n"
"{ \n"
" return vec4(foo, bar, bat); \n"
"} \n"
" \n"
"//!TEXTURE tex_storage \n"
"//!SIZE 100 100 \n"
"//!FORMAT r32f \n"
"//!STORAGE \n"
" \n"
"//!BUFFER buf_uniform \n"
"//!VAR float foo \n"
"//!VAR float bar \n"
"0000000000000000 \n"
" \n"
"//!BUFFER buf_storage \n"
"//!VAR vec2 bat \n"
"//!VAR int big[32]; \n"
"//!STORAGE \n",
// Test custom parameters
"//!PARAM test \n"
"//!DESC test parameter \n"
"//!TYPE DYNAMIC float \n"
"//!MINIMUM 0.0 \n"
"//!MAXIMUM 100.0 \n"
"1.0 \n"
" \n"
"//!PARAM testconst \n"
"//!TYPE CONSTANT uint \n"
"//!MAXIMUM 16 \n"
"3 \n"
" \n"
"//!PARAM testdefine \n"
"//!TYPE DEFINE \n"
"100 \n"
" \n"
"//!HOOK MAIN \n"
"//!WHEN testconst 30 > \n"
"#error should not be run \n",
};
static const char *test_luts[] = {
"TITLE \"1D identity\" \n"
"LUT_1D_SIZE 2 \n"
"0.0 0.0 0.0 \n"
"1.0 1.0 1.0 \n",
"TITLE \"3D identity\" \n"
"LUT_3D_SIZE 2 \n"
"0.0 0.0 0.0 \n"
"1.0 0.0 0.0 \n"
"0.0 1.0 0.0 \n"
"1.0 1.0 0.0 \n"
"0.0 0.0 1.0 \n"
"1.0 0.0 1.0 \n"
"0.0 1.0 1.0 \n"
"1.0 1.0 1.0 \n"
};
static bool frame_passthrough(pl_gpu gpu, pl_tex *tex,
const struct pl_source_frame *src, struct pl_frame *out_frame)
{
const struct pl_frame *frame = src->frame_data;
*out_frame = *frame;