-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Expand file tree
/
Copy pathSDL_gpu.c
More file actions
3732 lines (3267 loc) · 123 KB
/
SDL_gpu.c
File metadata and controls
3732 lines (3267 loc) · 123 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
/*
Simple DirectMedia Layer
Copyright (C) 1997-2026 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "SDL_internal.h"
#include "SDL_sysgpu.h"
/* Normally this macro would use something like SDL_IsObjectValid, but in GPU's
* case we can prioritize performance and be more trusting of application
* behavior than, say, SDL_Render, so trust that applications will be careful
* about disposing the device and its resources.
* -flibit
*/
#define CHECK_DEVICE_MAGIC(device, retval) \
CHECK_PARAM(device == NULL) { \
SDL_SetError("Invalid GPU device"); \
return retval; \
}
#define CHECK_COMMAND_BUFFER \
if (((CommandBufferCommonHeader *)command_buffer)->submitted) { \
SDL_assert_release(!"Command buffer already submitted!"); \
return; \
}
#define CHECK_COMMAND_BUFFER_RETURN_FALSE \
if (((CommandBufferCommonHeader *)command_buffer)->submitted) { \
SDL_assert_release(!"Command buffer already submitted!"); \
return false; \
}
#define CHECK_COMMAND_BUFFER_RETURN_NULL \
if (((CommandBufferCommonHeader *)command_buffer)->submitted) { \
SDL_assert_release(!"Command buffer already submitted!"); \
return NULL; \
}
#define CHECK_ANY_PASS_IN_PROGRESS(msg, retval) \
if ( \
((CommandBufferCommonHeader *)command_buffer)->render_pass.in_progress || \
((CommandBufferCommonHeader *)command_buffer)->compute_pass.in_progress || \
((CommandBufferCommonHeader *)command_buffer)->copy_pass.in_progress) { \
SDL_assert_release(!msg); \
return retval; \
}
#define CHECK_RENDERPASS \
if (!((RenderPass *)render_pass)->in_progress) { \
SDL_assert_release(!"Render pass not in progress!"); \
return; \
}
#if 0
// The below validation is too aggressive, since there are advanced situations
// where this is legal. This is being temporarily disabled for further review.
// See: https://github.com/libsdl-org/SDL/issues/13871
#define CHECK_SAMPLER_TEXTURES \
RenderPass *rp = (RenderPass *)render_pass; \
for (Uint32 color_target_index = 0; color_target_index < rp->num_color_targets; color_target_index += 1) { \
for (Uint32 texture_sampler_index = 0; texture_sampler_index < num_bindings; texture_sampler_index += 1) { \
if (rp->color_targets[color_target_index] == texture_sampler_bindings[texture_sampler_index].texture) { \
SDL_assert_release(!"Texture cannot be simultaneously bound as a color target and a sampler!"); \
} \
} \
} \
\
for (Uint32 texture_sampler_index = 0; texture_sampler_index < num_bindings; texture_sampler_index += 1) { \
if (rp->depth_stencil_target != NULL && rp->depth_stencil_target == texture_sampler_bindings[texture_sampler_index].texture) { \
SDL_assert_release(!"Texture cannot be simultaneously bound as a depth stencil target and a sampler!"); \
} \
}
#define CHECK_STORAGE_TEXTURES \
RenderPass *rp = (RenderPass *)render_pass; \
for (Uint32 color_target_index = 0; color_target_index < rp->num_color_targets; color_target_index += 1) { \
for (Uint32 texture_sampler_index = 0; texture_sampler_index < num_bindings; texture_sampler_index += 1) { \
if (rp->color_targets[color_target_index] == storage_textures[texture_sampler_index]) { \
SDL_assert_release(!"Texture cannot be simultaneously bound as a color target and a storage texture!"); \
} \
} \
} \
\
for (Uint32 texture_sampler_index = 0; texture_sampler_index < num_bindings; texture_sampler_index += 1) { \
if (rp->depth_stencil_target != NULL && rp->depth_stencil_target == storage_textures[texture_sampler_index]) { \
SDL_assert_release(!"Texture cannot be simultaneously bound as a depth stencil target and a storage texture!"); \
} \
}
#else
#define CHECK_SAMPLER_TEXTURES
#define CHECK_STORAGE_TEXTURES
#endif
#define CHECK_GRAPHICS_PIPELINE_BOUND \
if (!((RenderPass *)render_pass)->graphics_pipeline) { \
SDL_assert_release(!"Graphics pipeline not bound!"); \
return; \
}
#define CHECK_COMPUTEPASS \
if (!((Pass *)compute_pass)->in_progress) { \
SDL_assert_release(!"Compute pass not in progress!"); \
return; \
}
#define CHECK_COMPUTE_PIPELINE_BOUND \
if (!((ComputePass *)compute_pass)->compute_pipeline) { \
SDL_assert_release(!"Compute pipeline not bound!"); \
return; \
}
#define CHECK_COPYPASS \
if (!((Pass *)copy_pass)->in_progress) { \
SDL_assert_release(!"Copy pass not in progress!"); \
return; \
}
#define CHECK_TEXTUREFORMAT_ENUM_INVALID(enumval, retval) \
if (enumval <= SDL_GPU_TEXTUREFORMAT_INVALID || enumval >= SDL_GPU_TEXTUREFORMAT_MAX_ENUM_VALUE) { \
SDL_assert_release(!"Invalid texture format enum!"); \
return retval; \
}
#define CHECK_VERTEXELEMENTFORMAT_ENUM_INVALID(enumval, retval) \
if (enumval <= SDL_GPU_VERTEXELEMENTFORMAT_INVALID || enumval >= SDL_GPU_VERTEXELEMENTFORMAT_MAX_ENUM_VALUE) { \
SDL_assert_release(!"Invalid vertex format enum!"); \
return retval; \
}
#define CHECK_COMPAREOP_ENUM_INVALID(enumval, retval) \
if (enumval <= SDL_GPU_COMPAREOP_INVALID || enumval >= SDL_GPU_COMPAREOP_MAX_ENUM_VALUE) { \
SDL_assert_release(!"Invalid compare op enum!"); \
return retval; \
}
#define CHECK_STENCILOP_ENUM_INVALID(enumval, retval) \
if (enumval <= SDL_GPU_STENCILOP_INVALID || enumval >= SDL_GPU_STENCILOP_MAX_ENUM_VALUE) { \
SDL_assert_release(!"Invalid stencil op enum!"); \
return retval; \
}
#define CHECK_BLENDOP_ENUM_INVALID(enumval, retval) \
if (enumval <= SDL_GPU_BLENDOP_INVALID || enumval >= SDL_GPU_BLENDOP_MAX_ENUM_VALUE) { \
SDL_assert_release(!"Invalid blend op enum!"); \
return retval; \
}
#define CHECK_BLENDFACTOR_ENUM_INVALID(enumval, retval) \
if (enumval <= SDL_GPU_BLENDFACTOR_INVALID || enumval >= SDL_GPU_BLENDFACTOR_MAX_ENUM_VALUE) { \
SDL_assert_release(!"Invalid blend factor enum!"); \
return retval; \
}
#define CHECK_SWAPCHAINCOMPOSITION_ENUM_INVALID(enumval, retval) \
if (enumval < 0 || enumval >= SDL_GPU_SWAPCHAINCOMPOSITION_MAX_ENUM_VALUE) { \
SDL_assert_release(!"Invalid swapchain composition enum!"); \
return retval; \
}
#define CHECK_PRESENTMODE_ENUM_INVALID(enumval, retval) \
if (enumval < 0 || enumval >= SDL_GPU_PRESENTMODE_MAX_ENUM_VALUE) { \
SDL_assert_release(!"Invalid present mode enum!"); \
return retval; \
}
#define COMMAND_BUFFER_DEVICE \
((CommandBufferCommonHeader *)command_buffer)->device
#define RENDERPASS_COMMAND_BUFFER \
((RenderPass *)render_pass)->command_buffer
#define RENDERPASS_DEVICE \
((CommandBufferCommonHeader *)RENDERPASS_COMMAND_BUFFER)->device
#define RENDERPASS_BOUND_PIPELINE \
((RenderPass *)render_pass)->graphics_pipeline
#define COMPUTEPASS_COMMAND_BUFFER \
((Pass *)compute_pass)->command_buffer
#define COMPUTEPASS_DEVICE \
((CommandBufferCommonHeader *)COMPUTEPASS_COMMAND_BUFFER)->device
#define COMPUTEPASS_BOUND_PIPELINE \
((ComputePass *)compute_pass)->compute_pipeline
#define COPYPASS_COMMAND_BUFFER \
((Pass *)copy_pass)->command_buffer
#define COPYPASS_DEVICE \
((CommandBufferCommonHeader *)COPYPASS_COMMAND_BUFFER)->device
static bool TextureFormatIsComputeWritable[] = {
false, // INVALID
false, // A8_UNORM
true, // R8_UNORM
true, // R8G8_UNORM
true, // R8G8B8A8_UNORM
true, // R16_UNORM
true, // R16G16_UNORM
true, // R16G16B16A16_UNORM
true, // R10G10B10A2_UNORM
false, // B5G6R5_UNORM
false, // B5G5R5A1_UNORM
false, // B4G4R4A4_UNORM
false, // B8G8R8A8_UNORM
false, // BC1_UNORM
false, // BC2_UNORM
false, // BC3_UNORM
false, // BC4_UNORM
false, // BC5_UNORM
false, // BC7_UNORM
false, // BC6H_FLOAT
false, // BC6H_UFLOAT
true, // R8_SNORM
true, // R8G8_SNORM
true, // R8G8B8A8_SNORM
true, // R16_SNORM
true, // R16G16_SNORM
true, // R16G16B16A16_SNORM
true, // R16_FLOAT
true, // R16G16_FLOAT
true, // R16G16B16A16_FLOAT
true, // R32_FLOAT
true, // R32G32_FLOAT
true, // R32G32B32A32_FLOAT
true, // R11G11B10_UFLOAT
true, // R8_UINT
true, // R8G8_UINT
true, // R8G8B8A8_UINT
true, // R16_UINT
true, // R16G16_UINT
true, // R16G16B16A16_UINT
true, // R32_UINT
true, // R32G32_UINT
true, // R32G32B32A32_UINT
true, // R8_INT
true, // R8G8_INT
true, // R8G8B8A8_INT
true, // R16_INT
true, // R16G16_INT
true, // R16G16B16A16_INT
true, // R32_INT
true, // R32G32_INT
true, // R32G32B32A32_INT
false, // R8G8B8A8_UNORM_SRGB
false, // B8G8R8A8_UNORM_SRGB
false, // BC1_UNORM_SRGB
false, // BC3_UNORM_SRGB
false, // BC3_UNORM_SRGB
false, // BC7_UNORM_SRGB
false, // D16_UNORM
false, // D24_UNORM
false, // D32_FLOAT
false, // D24_UNORM_S8_UINT
false, // D32_FLOAT_S8_UINT
false, // ASTC_4x4_UNORM
false, // ASTC_5x4_UNORM
false, // ASTC_5x5_UNORM
false, // ASTC_6x5_UNORM
false, // ASTC_6x6_UNORM
false, // ASTC_8x5_UNORM
false, // ASTC_8x6_UNORM
false, // ASTC_8x8_UNORM
false, // ASTC_10x5_UNORM
false, // ASTC_10x6_UNORM
false, // ASTC_10x8_UNORM
false, // ASTC_10x10_UNORM
false, // ASTC_12x10_UNORM
false, // ASTC_12x12_UNORM
false, // ASTC_4x4_UNORM_SRGB
false, // ASTC_5x4_UNORM_SRGB
false, // ASTC_5x5_UNORM_SRGB
false, // ASTC_6x5_UNORM_SRGB
false, // ASTC_6x6_UNORM_SRGB
false, // ASTC_8x5_UNORM_SRGB
false, // ASTC_8x6_UNORM_SRGB
false, // ASTC_8x8_UNORM_SRGB
false, // ASTC_10x5_UNORM_SRGB
false, // ASTC_10x6_UNORM_SRGB
false, // ASTC_10x8_UNORM_SRGB
false, // ASTC_10x10_UNORM_SRGB
false, // ASTC_12x10_UNORM_SRGB
false, // ASTC_12x12_UNORM_SRGB
false, // ASTC_4x4_FLOAT
false, // ASTC_5x4_FLOAT
false, // ASTC_5x5_FLOAT
false, // ASTC_6x5_FLOAT
false, // ASTC_6x6_FLOAT
false, // ASTC_8x5_FLOAT
false, // ASTC_8x6_FLOAT
false, // ASTC_8x8_FLOAT
false, // ASTC_10x5_FLOAT
false, // ASTC_10x6_FLOAT
false, // ASTC_10x8_FLOAT
false, // ASTC_10x10_FLOAT
false, // ASTC_12x10_FLOAT
false // ASTC_12x12_FLOAT
};
// Drivers
#ifndef SDL_GPU_DISABLED
static const SDL_GPUBootstrap *backends[] = {
#ifdef SDL_GPU_PRIVATE
&PrivateGPUDriver,
#endif
#ifdef SDL_GPU_METAL
&MetalDriver,
#endif
#ifdef SDL_GPU_D3D12
&D3D12Driver,
#endif
#ifdef SDL_GPU_VULKAN
&VulkanDriver,
#endif
NULL
};
#endif // !SDL_GPU_DISABLED
// Internal Utility Functions
SDL_GPUGraphicsPipeline *SDL_GPU_FetchBlitPipeline(
SDL_GPUDevice *device,
SDL_GPUTextureType source_texture_type,
SDL_GPUTextureFormat destination_format,
SDL_GPUShader *blit_vertex_shader,
SDL_GPUShader *blit_from_2d_shader,
SDL_GPUShader *blit_from_2d_array_shader,
SDL_GPUShader *blit_from_3d_shader,
SDL_GPUShader *blit_from_cube_shader,
SDL_GPUShader *blit_from_cube_array_shader,
BlitPipelineCacheEntry **blit_pipelines,
Uint32 *blit_pipeline_count,
Uint32 *blit_pipeline_capacity)
{
SDL_GPUGraphicsPipelineCreateInfo blit_pipeline_create_info;
SDL_GPUColorTargetDescription color_target_desc;
SDL_GPUGraphicsPipeline *pipeline;
if (blit_pipeline_count == NULL) {
// use pre-created, format-agnostic pipelines
return (*blit_pipelines)[source_texture_type].pipeline;
}
for (Uint32 i = 0; i < *blit_pipeline_count; i += 1) {
if ((*blit_pipelines)[i].type == source_texture_type && (*blit_pipelines)[i].format == destination_format) {
return (*blit_pipelines)[i].pipeline;
}
}
// No pipeline found, we'll need to make one!
SDL_zero(blit_pipeline_create_info);
SDL_zero(color_target_desc);
color_target_desc.blend_state.color_write_mask = 0xF;
color_target_desc.format = destination_format;
blit_pipeline_create_info.target_info.color_target_descriptions = &color_target_desc;
blit_pipeline_create_info.target_info.num_color_targets = 1;
blit_pipeline_create_info.target_info.depth_stencil_format = SDL_GPU_TEXTUREFORMAT_D16_UNORM; // arbitrary
blit_pipeline_create_info.target_info.has_depth_stencil_target = false;
blit_pipeline_create_info.vertex_shader = blit_vertex_shader;
if (source_texture_type == SDL_GPU_TEXTURETYPE_CUBE) {
blit_pipeline_create_info.fragment_shader = blit_from_cube_shader;
} else if (source_texture_type == SDL_GPU_TEXTURETYPE_CUBE_ARRAY) {
blit_pipeline_create_info.fragment_shader = blit_from_cube_array_shader;
} else if (source_texture_type == SDL_GPU_TEXTURETYPE_2D_ARRAY) {
blit_pipeline_create_info.fragment_shader = blit_from_2d_array_shader;
} else if (source_texture_type == SDL_GPU_TEXTURETYPE_3D) {
blit_pipeline_create_info.fragment_shader = blit_from_3d_shader;
} else {
blit_pipeline_create_info.fragment_shader = blit_from_2d_shader;
}
blit_pipeline_create_info.rasterizer_state.enable_depth_clip = device->default_enable_depth_clip;
blit_pipeline_create_info.multisample_state.sample_count = SDL_GPU_SAMPLECOUNT_1;
blit_pipeline_create_info.multisample_state.enable_mask = false;
blit_pipeline_create_info.primitive_type = SDL_GPU_PRIMITIVETYPE_TRIANGLELIST;
pipeline = SDL_CreateGPUGraphicsPipeline(
device,
&blit_pipeline_create_info);
if (pipeline == NULL) {
SDL_SetError("Failed to create GPU pipeline for blit");
return NULL;
}
// Cache the new pipeline
EXPAND_ARRAY_IF_NEEDED(
(*blit_pipelines),
BlitPipelineCacheEntry,
*blit_pipeline_count + 1,
*blit_pipeline_capacity,
*blit_pipeline_capacity * 2);
(*blit_pipelines)[*blit_pipeline_count].pipeline = pipeline;
(*blit_pipelines)[*blit_pipeline_count].type = source_texture_type;
(*blit_pipelines)[*blit_pipeline_count].format = destination_format;
*blit_pipeline_count += 1;
return pipeline;
}
void SDL_GPU_BlitCommon(
SDL_GPUCommandBuffer *command_buffer,
const SDL_GPUBlitInfo *info,
SDL_GPUSampler *blit_linear_sampler,
SDL_GPUSampler *blit_nearest_sampler,
SDL_GPUShader *blit_vertex_shader,
SDL_GPUShader *blit_from_2d_shader,
SDL_GPUShader *blit_from_2d_array_shader,
SDL_GPUShader *blit_from_3d_shader,
SDL_GPUShader *blit_from_cube_shader,
SDL_GPUShader *blit_from_cube_array_shader,
BlitPipelineCacheEntry **blit_pipelines,
Uint32 *blit_pipeline_count,
Uint32 *blit_pipeline_capacity)
{
CommandBufferCommonHeader *cmdbufHeader = (CommandBufferCommonHeader *)command_buffer;
SDL_GPURenderPass *render_pass;
TextureCommonHeader *src_header = (TextureCommonHeader *)info->source.texture;
TextureCommonHeader *dst_header = (TextureCommonHeader *)info->destination.texture;
SDL_GPUGraphicsPipeline *blit_pipeline;
SDL_GPUColorTargetInfo color_target_info;
SDL_GPUViewport viewport;
SDL_GPUTextureSamplerBinding texture_sampler_binding;
BlitFragmentUniforms blit_fragment_uniforms;
Uint32 layer_divisor;
blit_pipeline = SDL_GPU_FetchBlitPipeline(
cmdbufHeader->device,
src_header->info.type,
dst_header->info.format,
blit_vertex_shader,
blit_from_2d_shader,
blit_from_2d_array_shader,
blit_from_3d_shader,
blit_from_cube_shader,
blit_from_cube_array_shader,
blit_pipelines,
blit_pipeline_count,
blit_pipeline_capacity);
SDL_assert(blit_pipeline != NULL);
color_target_info.load_op = info->load_op;
color_target_info.clear_color = info->clear_color;
color_target_info.store_op = SDL_GPU_STOREOP_STORE;
color_target_info.texture = info->destination.texture;
color_target_info.mip_level = info->destination.mip_level;
color_target_info.layer_or_depth_plane = info->destination.layer_or_depth_plane;
color_target_info.cycle = info->cycle;
render_pass = SDL_BeginGPURenderPass(
command_buffer,
&color_target_info,
1,
NULL);
viewport.x = (float)info->destination.x;
viewport.y = (float)info->destination.y;
viewport.w = (float)info->destination.w;
viewport.h = (float)info->destination.h;
viewport.min_depth = 0;
viewport.max_depth = 1;
SDL_SetGPUViewport(
render_pass,
&viewport);
SDL_BindGPUGraphicsPipeline(
render_pass,
blit_pipeline);
texture_sampler_binding.texture = info->source.texture;
texture_sampler_binding.sampler =
info->filter == SDL_GPU_FILTER_NEAREST ? blit_nearest_sampler : blit_linear_sampler;
SDL_BindGPUFragmentSamplers(
render_pass,
0,
&texture_sampler_binding,
1);
blit_fragment_uniforms.left = (float)info->source.x / (src_header->info.width >> info->source.mip_level);
blit_fragment_uniforms.top = (float)info->source.y / (src_header->info.height >> info->source.mip_level);
blit_fragment_uniforms.width = (float)info->source.w / (src_header->info.width >> info->source.mip_level);
blit_fragment_uniforms.height = (float)info->source.h / (src_header->info.height >> info->source.mip_level);
blit_fragment_uniforms.mip_level = info->source.mip_level;
layer_divisor = (src_header->info.type == SDL_GPU_TEXTURETYPE_3D) ? src_header->info.layer_count_or_depth : 1;
blit_fragment_uniforms.layer_or_depth = (float)info->source.layer_or_depth_plane / layer_divisor;
if (info->flip_mode & SDL_FLIP_HORIZONTAL) {
blit_fragment_uniforms.left += blit_fragment_uniforms.width;
blit_fragment_uniforms.width *= -1;
}
if (info->flip_mode & SDL_FLIP_VERTICAL) {
blit_fragment_uniforms.top += blit_fragment_uniforms.height;
blit_fragment_uniforms.height *= -1;
}
SDL_PushGPUFragmentUniformData(
command_buffer,
0,
&blit_fragment_uniforms,
sizeof(blit_fragment_uniforms));
SDL_DrawGPUPrimitives(render_pass, 3, 1, 0, 0);
SDL_EndGPURenderPass(render_pass);
}
static void SDL_GPU_CheckGraphicsBindings(SDL_GPURenderPass *render_pass)
{
RenderPass *rp = (RenderPass *)render_pass;
GraphicsPipelineCommonHeader *pipeline = (GraphicsPipelineCommonHeader *)RENDERPASS_BOUND_PIPELINE;
for (Uint32 i = 0; i < pipeline->num_vertex_samplers; i += 1) {
if (!rp->vertex_sampler_bound[i]) {
SDL_assert_release(!"Missing vertex sampler binding!");
}
}
for (Uint32 i = 0; i < pipeline->num_vertex_storage_textures; i += 1) {
if (!rp->vertex_storage_texture_bound[i]) {
SDL_assert_release(!"Missing vertex storage texture binding!");
}
}
for (Uint32 i = 0; i < pipeline->num_vertex_storage_buffers; i += 1) {
if (!rp->vertex_storage_buffer_bound[i]) {
SDL_assert_release(!"Missing vertex storage buffer binding!");
}
}
for (Uint32 i = 0; i < pipeline->num_fragment_samplers; i += 1) {
if (!rp->fragment_sampler_bound[i]) {
SDL_assert_release(!"Missing fragment sampler binding!");
}
}
for (Uint32 i = 0; i < pipeline->num_fragment_storage_textures; i += 1) {
if (!rp->fragment_storage_texture_bound[i]) {
SDL_assert_release(!"Missing fragment storage texture binding!");
}
}
for (Uint32 i = 0; i < pipeline->num_fragment_storage_buffers; i += 1) {
if (!rp->fragment_storage_buffer_bound[i]) {
SDL_assert_release(!"Missing fragment storage buffer binding!");
}
}
}
static void SDL_GPU_CheckComputeBindings(SDL_GPUComputePass *compute_pass)
{
ComputePass *cp = (ComputePass *)compute_pass;
ComputePipelineCommonHeader *pipeline = (ComputePipelineCommonHeader *)COMPUTEPASS_BOUND_PIPELINE;
for (Uint32 i = 0; i < pipeline->numSamplers; i += 1) {
if (!cp->sampler_bound[i]) {
SDL_assert_release(!"Missing compute sampler binding!");
}
}
for (Uint32 i = 0; i < pipeline->numReadonlyStorageTextures; i += 1) {
if (!cp->read_only_storage_texture_bound[i]) {
SDL_assert_release(!"Missing compute readonly storage texture binding!");
}
}
for (Uint32 i = 0; i < pipeline->numReadonlyStorageBuffers; i += 1) {
if (!cp->read_only_storage_buffer_bound[i]) {
SDL_assert_release(!"Missing compute readonly storage buffer binding!");
}
}
for (Uint32 i = 0; i < pipeline->numReadWriteStorageTextures; i += 1) {
if (!cp->read_write_storage_texture_bound[i]) {
SDL_assert_release(!"Missing compute read-write storage texture binding!");
}
}
for (Uint32 i = 0; i < pipeline->numReadWriteStorageBuffers; i += 1) {
if (!cp->read_write_storage_buffer_bound[i]) {
SDL_assert_release(!"Missing compute read-write storage buffer bbinding!");
}
}
}
// Driver Functions
#ifndef SDL_GPU_DISABLED
static const SDL_GPUBootstrap * SDL_GPUSelectBackend(SDL_PropertiesID props)
{
Uint32 i;
const char *gpudriver;
SDL_VideoDevice *_this = SDL_GetVideoDevice();
if (_this == NULL) {
SDL_SetError("Video subsystem not initialized");
return NULL;
}
#ifndef HAVE_GPU_OPENXR
if (SDL_GetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_XR_ENABLE_BOOLEAN, false)) {
SDL_SetError("OpenXR is not enabled in this build of SDL");
return NULL;
}
#endif
gpudriver = SDL_GetHint(SDL_HINT_GPU_DRIVER);
if (gpudriver == NULL) {
gpudriver = SDL_GetStringProperty(props, SDL_PROP_GPU_DEVICE_CREATE_NAME_STRING, NULL);
}
// Environment/Properties override...
if (gpudriver != NULL) {
for (i = 0; backends[i]; i += 1) {
if (SDL_strcasecmp(gpudriver, backends[i]->name) == 0) {
if (backends[i]->PrepareDriver(_this, props)) {
return backends[i];
}
}
}
SDL_SetError("SDL_HINT_GPU_DRIVER %s unsupported!", gpudriver);
return NULL;
}
for (i = 0; backends[i]; i += 1) {
if (backends[i]->PrepareDriver(_this, props)) {
return backends[i];
}
}
SDL_SetError("No supported SDL_GPU backend found!");
return NULL;
}
static void SDL_GPU_FillProperties(
SDL_PropertiesID props,
SDL_GPUShaderFormat format_flags,
bool debug_mode,
const char *name)
{
if (format_flags & SDL_GPU_SHADERFORMAT_PRIVATE) {
SDL_SetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_SHADERS_PRIVATE_BOOLEAN, true);
}
if (format_flags & SDL_GPU_SHADERFORMAT_SPIRV) {
SDL_SetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_SHADERS_SPIRV_BOOLEAN, true);
}
if (format_flags & SDL_GPU_SHADERFORMAT_DXBC) {
SDL_SetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_SHADERS_DXBC_BOOLEAN, true);
}
if (format_flags & SDL_GPU_SHADERFORMAT_DXIL) {
SDL_SetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_SHADERS_DXIL_BOOLEAN, true);
}
if (format_flags & SDL_GPU_SHADERFORMAT_MSL) {
SDL_SetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_SHADERS_MSL_BOOLEAN, true);
}
if (format_flags & SDL_GPU_SHADERFORMAT_METALLIB) {
SDL_SetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_SHADERS_METALLIB_BOOLEAN, true);
}
SDL_SetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_DEBUGMODE_BOOLEAN, debug_mode);
SDL_SetStringProperty(props, SDL_PROP_GPU_DEVICE_CREATE_NAME_STRING, name);
}
#endif // SDL_GPU_DISABLED
bool SDL_GPUSupportsShaderFormats(
SDL_GPUShaderFormat format_flags,
const char *name)
{
#ifndef SDL_GPU_DISABLED
bool result;
SDL_PropertiesID props = SDL_CreateProperties();
SDL_GPU_FillProperties(props, format_flags, false, name);
result = SDL_GPUSupportsProperties(props);
SDL_DestroyProperties(props);
return result;
#else
SDL_SetError("SDL not built with GPU support");
return false;
#endif
}
bool SDL_GPUSupportsProperties(SDL_PropertiesID props)
{
#ifndef SDL_GPU_DISABLED
return (SDL_GPUSelectBackend(props) != NULL);
#else
SDL_SetError("SDL not built with GPU support");
return false;
#endif
}
SDL_GPUDevice *SDL_CreateGPUDevice(
SDL_GPUShaderFormat format_flags,
bool debug_mode,
const char *name)
{
#ifndef SDL_GPU_DISABLED
SDL_GPUDevice *result;
SDL_PropertiesID props = SDL_CreateProperties();
SDL_GPU_FillProperties(props, format_flags, debug_mode, name);
result = SDL_CreateGPUDeviceWithProperties(props);
SDL_DestroyProperties(props);
return result;
#else
SDL_SetError("SDL not built with GPU support");
return NULL;
#endif // SDL_GPU_DISABLED
}
SDL_GPUDevice *SDL_CreateGPUDeviceWithProperties(SDL_PropertiesID props)
{
#ifndef SDL_GPU_DISABLED
bool debug_mode;
bool preferLowPower;
SDL_GPUDevice *result = NULL;
const SDL_GPUBootstrap *selectedBackend;
selectedBackend = SDL_GPUSelectBackend(props);
if (selectedBackend != NULL) {
SDL_DebugLogBackend("gpu", selectedBackend->name);
debug_mode = SDL_GetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_DEBUGMODE_BOOLEAN, true);
preferLowPower = SDL_GetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_PREFERLOWPOWER_BOOLEAN, false);
result = selectedBackend->CreateDevice(debug_mode, preferLowPower, props);
if (result != NULL) {
result->backend = selectedBackend->name;
result->debug_mode = debug_mode;
if (SDL_GetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_FEATURE_DEPTH_CLAMPING_BOOLEAN, true)) {
result->default_enable_depth_clip = false;
} else {
result->default_enable_depth_clip = true;
result->validate_feature_depth_clamp_disabled = true;
}
if (!SDL_GetBooleanProperty(props, SDL_PROP_GPU_DEVICE_CREATE_FEATURE_ANISOTROPY_BOOLEAN, true)) {
result->validate_feature_anisotropy_disabled = true;
}
}
}
return result;
#else
SDL_SetError("SDL not built with GPU support");
return NULL;
#endif // SDL_GPU_DISABLED
}
void SDL_DestroyGPUDevice(SDL_GPUDevice *device)
{
CHECK_DEVICE_MAGIC(device, );
device->DestroyDevice(device);
}
XrResult SDL_DestroyGPUXRSwapchain(SDL_GPUDevice *device, XrSwapchain swapchain, SDL_GPUTexture **swapchainImages)
{
CHECK_DEVICE_MAGIC(device, XR_ERROR_HANDLE_INVALID);
return device->DestroyXRSwapchain(device->driverData, swapchain, swapchainImages);
}
int SDL_GetNumGPUDrivers(void)
{
#ifndef SDL_GPU_DISABLED
return SDL_arraysize(backends) - 1;
#else
return 0;
#endif
}
const char * SDL_GetGPUDriver(int index)
{
CHECK_PARAM(index < 0 || index >= SDL_GetNumGPUDrivers()) {
SDL_InvalidParamError("index");
return NULL;
}
#ifndef SDL_GPU_DISABLED
return backends[index]->name;
#else
return NULL;
#endif
}
const char * SDL_GetGPUDeviceDriver(SDL_GPUDevice *device)
{
CHECK_DEVICE_MAGIC(device, NULL);
return device->backend;
}
SDL_GPUShaderFormat SDL_GetGPUShaderFormats(SDL_GPUDevice *device)
{
CHECK_DEVICE_MAGIC(device, SDL_GPU_SHADERFORMAT_INVALID);
return device->shader_formats;
}
SDL_PropertiesID SDL_GetGPUDeviceProperties(SDL_GPUDevice *device)
{
CHECK_DEVICE_MAGIC(device, 0);
return device->GetDeviceProperties(device);
}
Uint32 SDL_GPUTextureFormatTexelBlockSize(
SDL_GPUTextureFormat format)
{
switch (format) {
case SDL_GPU_TEXTUREFORMAT_BC1_RGBA_UNORM:
case SDL_GPU_TEXTUREFORMAT_BC1_RGBA_UNORM_SRGB:
case SDL_GPU_TEXTUREFORMAT_BC4_R_UNORM:
return 8;
case SDL_GPU_TEXTUREFORMAT_BC2_RGBA_UNORM:
case SDL_GPU_TEXTUREFORMAT_BC3_RGBA_UNORM:
case SDL_GPU_TEXTUREFORMAT_BC5_RG_UNORM:
case SDL_GPU_TEXTUREFORMAT_BC7_RGBA_UNORM:
case SDL_GPU_TEXTUREFORMAT_BC6H_RGB_FLOAT:
case SDL_GPU_TEXTUREFORMAT_BC6H_RGB_UFLOAT:
case SDL_GPU_TEXTUREFORMAT_BC2_RGBA_UNORM_SRGB:
case SDL_GPU_TEXTUREFORMAT_BC3_RGBA_UNORM_SRGB:
case SDL_GPU_TEXTUREFORMAT_BC7_RGBA_UNORM_SRGB:
return 16;
case SDL_GPU_TEXTUREFORMAT_R8_UNORM:
case SDL_GPU_TEXTUREFORMAT_R8_SNORM:
case SDL_GPU_TEXTUREFORMAT_A8_UNORM:
case SDL_GPU_TEXTUREFORMAT_R8_UINT:
case SDL_GPU_TEXTUREFORMAT_R8_INT:
return 1;
case SDL_GPU_TEXTUREFORMAT_B5G6R5_UNORM:
case SDL_GPU_TEXTUREFORMAT_B4G4R4A4_UNORM:
case SDL_GPU_TEXTUREFORMAT_B5G5R5A1_UNORM:
case SDL_GPU_TEXTUREFORMAT_R16_FLOAT:
case SDL_GPU_TEXTUREFORMAT_R8G8_SNORM:
case SDL_GPU_TEXTUREFORMAT_R8G8_UNORM:
case SDL_GPU_TEXTUREFORMAT_R8G8_UINT:
case SDL_GPU_TEXTUREFORMAT_R8G8_INT:
case SDL_GPU_TEXTUREFORMAT_R16_UNORM:
case SDL_GPU_TEXTUREFORMAT_R16_SNORM:
case SDL_GPU_TEXTUREFORMAT_R16_UINT:
case SDL_GPU_TEXTUREFORMAT_R16_INT:
case SDL_GPU_TEXTUREFORMAT_D16_UNORM:
return 2;
case SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM:
case SDL_GPU_TEXTUREFORMAT_B8G8R8A8_UNORM:
case SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM_SRGB:
case SDL_GPU_TEXTUREFORMAT_B8G8R8A8_UNORM_SRGB:
case SDL_GPU_TEXTUREFORMAT_R32_FLOAT:
case SDL_GPU_TEXTUREFORMAT_R16G16_FLOAT:
case SDL_GPU_TEXTUREFORMAT_R11G11B10_UFLOAT:
case SDL_GPU_TEXTUREFORMAT_R8G8B8A8_SNORM:
case SDL_GPU_TEXTUREFORMAT_R10G10B10A2_UNORM:
case SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UINT:
case SDL_GPU_TEXTUREFORMAT_R8G8B8A8_INT:
case SDL_GPU_TEXTUREFORMAT_R16G16_UINT:
case SDL_GPU_TEXTUREFORMAT_R16G16_INT:
case SDL_GPU_TEXTUREFORMAT_R16G16_UNORM:
case SDL_GPU_TEXTUREFORMAT_R16G16_SNORM:
case SDL_GPU_TEXTUREFORMAT_D24_UNORM:
case SDL_GPU_TEXTUREFORMAT_D32_FLOAT:
case SDL_GPU_TEXTUREFORMAT_R32_UINT:
case SDL_GPU_TEXTUREFORMAT_R32_INT:
case SDL_GPU_TEXTUREFORMAT_D24_UNORM_S8_UINT:
return 4;
case SDL_GPU_TEXTUREFORMAT_D32_FLOAT_S8_UINT:
return 5;
case SDL_GPU_TEXTUREFORMAT_R16G16B16A16_FLOAT:
case SDL_GPU_TEXTUREFORMAT_R16G16B16A16_UNORM:
case SDL_GPU_TEXTUREFORMAT_R16G16B16A16_SNORM:
case SDL_GPU_TEXTUREFORMAT_R16G16B16A16_UINT:
case SDL_GPU_TEXTUREFORMAT_R16G16B16A16_INT:
case SDL_GPU_TEXTUREFORMAT_R32G32_FLOAT:
case SDL_GPU_TEXTUREFORMAT_R32G32_UINT:
case SDL_GPU_TEXTUREFORMAT_R32G32_INT:
return 8;
case SDL_GPU_TEXTUREFORMAT_R32G32B32A32_FLOAT:
case SDL_GPU_TEXTUREFORMAT_R32G32B32A32_INT:
case SDL_GPU_TEXTUREFORMAT_R32G32B32A32_UINT:
return 16;
case SDL_GPU_TEXTUREFORMAT_ASTC_4x4_UNORM:
case SDL_GPU_TEXTUREFORMAT_ASTC_5x4_UNORM:
case SDL_GPU_TEXTUREFORMAT_ASTC_5x5_UNORM:
case SDL_GPU_TEXTUREFORMAT_ASTC_6x5_UNORM:
case SDL_GPU_TEXTUREFORMAT_ASTC_6x6_UNORM:
case SDL_GPU_TEXTUREFORMAT_ASTC_8x5_UNORM:
case SDL_GPU_TEXTUREFORMAT_ASTC_8x6_UNORM:
case SDL_GPU_TEXTUREFORMAT_ASTC_8x8_UNORM:
case SDL_GPU_TEXTUREFORMAT_ASTC_10x5_UNORM:
case SDL_GPU_TEXTUREFORMAT_ASTC_10x6_UNORM:
case SDL_GPU_TEXTUREFORMAT_ASTC_10x8_UNORM:
case SDL_GPU_TEXTUREFORMAT_ASTC_10x10_UNORM:
case SDL_GPU_TEXTUREFORMAT_ASTC_12x10_UNORM:
case SDL_GPU_TEXTUREFORMAT_ASTC_12x12_UNORM:
case SDL_GPU_TEXTUREFORMAT_ASTC_4x4_UNORM_SRGB:
case SDL_GPU_TEXTUREFORMAT_ASTC_5x4_UNORM_SRGB:
case SDL_GPU_TEXTUREFORMAT_ASTC_5x5_UNORM_SRGB:
case SDL_GPU_TEXTUREFORMAT_ASTC_6x5_UNORM_SRGB:
case SDL_GPU_TEXTUREFORMAT_ASTC_6x6_UNORM_SRGB:
case SDL_GPU_TEXTUREFORMAT_ASTC_8x5_UNORM_SRGB:
case SDL_GPU_TEXTUREFORMAT_ASTC_8x6_UNORM_SRGB:
case SDL_GPU_TEXTUREFORMAT_ASTC_8x8_UNORM_SRGB:
case SDL_GPU_TEXTUREFORMAT_ASTC_10x5_UNORM_SRGB:
case SDL_GPU_TEXTUREFORMAT_ASTC_10x6_UNORM_SRGB:
case SDL_GPU_TEXTUREFORMAT_ASTC_10x8_UNORM_SRGB:
case SDL_GPU_TEXTUREFORMAT_ASTC_10x10_UNORM_SRGB:
case SDL_GPU_TEXTUREFORMAT_ASTC_12x10_UNORM_SRGB:
case SDL_GPU_TEXTUREFORMAT_ASTC_12x12_UNORM_SRGB:
case SDL_GPU_TEXTUREFORMAT_ASTC_4x4_FLOAT:
case SDL_GPU_TEXTUREFORMAT_ASTC_5x4_FLOAT:
case SDL_GPU_TEXTUREFORMAT_ASTC_5x5_FLOAT:
case SDL_GPU_TEXTUREFORMAT_ASTC_6x5_FLOAT:
case SDL_GPU_TEXTUREFORMAT_ASTC_6x6_FLOAT:
case SDL_GPU_TEXTUREFORMAT_ASTC_8x5_FLOAT:
case SDL_GPU_TEXTUREFORMAT_ASTC_8x6_FLOAT:
case SDL_GPU_TEXTUREFORMAT_ASTC_8x8_FLOAT:
case SDL_GPU_TEXTUREFORMAT_ASTC_10x5_FLOAT:
case SDL_GPU_TEXTUREFORMAT_ASTC_10x6_FLOAT:
case SDL_GPU_TEXTUREFORMAT_ASTC_10x8_FLOAT:
case SDL_GPU_TEXTUREFORMAT_ASTC_10x10_FLOAT:
case SDL_GPU_TEXTUREFORMAT_ASTC_12x10_FLOAT:
case SDL_GPU_TEXTUREFORMAT_ASTC_12x12_FLOAT:
return 16;
default:
SDL_assert_release(!"Unrecognized TextureFormat!");
return 0;
}
}
bool SDL_GPUTextureSupportsFormat(
SDL_GPUDevice *device,
SDL_GPUTextureFormat format,
SDL_GPUTextureType type,
SDL_GPUTextureUsageFlags usage)
{
CHECK_DEVICE_MAGIC(device, false);
if (device->debug_mode) {
CHECK_TEXTUREFORMAT_ENUM_INVALID(format, false);
}
if ((usage & SDL_GPU_TEXTUREUSAGE_COMPUTE_STORAGE_WRITE) ||
(usage & SDL_GPU_TEXTUREUSAGE_COMPUTE_STORAGE_SIMULTANEOUS_READ_WRITE)) {
if (!TextureFormatIsComputeWritable[format]) {
return false;
}
}
return device->SupportsTextureFormat(
device->driverData,
format,
type,
usage);
}
bool SDL_GPUTextureSupportsSampleCount(
SDL_GPUDevice *device,
SDL_GPUTextureFormat format,
SDL_GPUSampleCount sample_count)
{
CHECK_DEVICE_MAGIC(device, 0);
if (device->debug_mode) {
CHECK_TEXTUREFORMAT_ENUM_INVALID(format, 0);
}
return device->SupportsSampleCount(
device->driverData,
format,
sample_count);
}
// State Creation
SDL_GPUComputePipeline *SDL_CreateGPUComputePipeline(
SDL_GPUDevice *device,
const SDL_GPUComputePipelineCreateInfo *createinfo)
{
CHECK_DEVICE_MAGIC(device, NULL);
if (createinfo == NULL) {
SDL_InvalidParamError("createinfo");
return NULL;
}
if (device->debug_mode) {
if (createinfo->format == SDL_GPU_SHADERFORMAT_INVALID) {
SDL_assert_release(!"Shader format cannot be INVALID!");
return NULL;
}
if (!(createinfo->format & device->shader_formats)) {
SDL_assert_release(!"Incompatible shader format for GPU backend");
return NULL;