Repository navigation
Expand file tree
/
Copy pathSDL_gpu.h
More file actions
4606 lines (4365 loc) · 178 KB
/
Copy pathSDL_gpu.h
File metadata and controls
4606 lines (4365 loc) · 178 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.
*/
/* WIKI CATEGORY: GPU */
/**
* # CategoryGPU
*
* The GPU API offers a cross-platform way for apps to talk to modern graphics
* hardware. It offers both 3D graphics and compute support, in the style of
* Metal, Vulkan, and Direct3D 12.
*
* A basic workflow might be something like this:
*
* The app creates a GPU device with SDL_CreateGPUDevice(), and assigns it to
* a window with SDL_ClaimWindowForGPUDevice()--although strictly speaking you
* can render offscreen entirely, perhaps for image processing, and not use a
* window at all.
*
* Next, the app prepares static data (things that are created once and used
* over and over). For example:
*
* - Shaders (programs that run on the GPU): use SDL_CreateGPUShader().
* - Vertex buffers (arrays of geometry data) and other rendering data: use
* SDL_CreateGPUBuffer() and SDL_UploadToGPUBuffer().
* - Textures (images): use SDL_CreateGPUTexture() and
* SDL_UploadToGPUTexture().
* - Samplers (how textures should be read from): use SDL_CreateGPUSampler().
* - Render pipelines (precalculated rendering state): use
* SDL_CreateGPUGraphicsPipeline()
*
* To render, the app creates one or more command buffers, with
* SDL_AcquireGPUCommandBuffer(). Command buffers collect rendering
* instructions that will be submitted to the GPU in batch. Complex scenes can
* use multiple command buffers, maybe configured across multiple threads in
* parallel, as long as they are submitted in the correct order, but many apps
* will just need one command buffer per frame.
*
* Rendering can happen to a texture (what other APIs call a "render target")
* or it can happen to the swapchain texture (which is just a special texture
* that represents a window's contents). The app can use
* SDL_WaitAndAcquireGPUSwapchainTexture() to render to the window.
*
* Rendering actually happens in a Render Pass, which is encoded into a
* command buffer. One can encode multiple render passes (or alternate between
* render and compute passes) in a single command buffer, but many apps might
* simply need a single render pass in a single command buffer. Render Passes
* can render to up to four color textures and one depth texture
* simultaneously. If the set of textures being rendered to needs to change,
* the Render Pass must be ended and a new one must be begun.
*
* The app calls SDL_BeginGPURenderPass(). Then it sets states it needs for
* each draw:
*
* - SDL_BindGPUGraphicsPipeline()
* - SDL_SetGPUViewport()
* - SDL_BindGPUVertexBuffers()
* - SDL_BindGPUVertexSamplers()
* - etc
*
* Then, make the actual draw commands with these states:
*
* - SDL_DrawGPUPrimitives()
* - SDL_DrawGPUPrimitivesIndirect()
* - SDL_DrawGPUIndexedPrimitivesIndirect()
* - etc
*
* After all the drawing commands for a pass are complete, the app should call
* SDL_EndGPURenderPass(). Once a render pass ends all render-related state is
* reset.
*
* The app can begin new Render Passes and make new draws in the same command
* buffer until the entire scene is rendered.
*
* Once all of the render commands for the scene are complete, the app calls
* SDL_SubmitGPUCommandBuffer() to send it to the GPU for processing.
*
* If the app needs to read back data from texture or buffers, the API has an
* efficient way of doing this, provided that the app is willing to tolerate
* some latency. When the app uses SDL_DownloadFromGPUTexture() or
* SDL_DownloadFromGPUBuffer(), submitting the command buffer with
* SDL_SubmitGPUCommandBufferAndAcquireFence() will return a fence handle that
* the app can poll or wait on in a thread. Once the fence indicates that the
* command buffer is done processing, it is safe to read the downloaded data.
* Make sure to call SDL_ReleaseGPUFence() when done with the fence.
*
* The API also has "compute" support. The app calls SDL_BeginGPUComputePass()
* with compute-writeable textures and/or buffers, which can be written to in
* a compute shader. Then it sets states it needs for the compute dispatches:
*
* - SDL_BindGPUComputePipeline()
* - SDL_BindGPUComputeStorageBuffers()
* - SDL_BindGPUComputeStorageTextures()
*
* Then, dispatch compute work:
*
* - SDL_DispatchGPUCompute()
*
* For advanced users, this opens up powerful GPU-driven workflows.
*
* Graphics and compute pipelines require the use of shaders, which as
* mentioned above are small programs executed on the GPU. Each backend
* (Vulkan, Metal, D3D12) requires a different shader format. When the app
* creates the GPU device, the app lets the device know which shader formats
* the app can provide. It will then select the appropriate backend depending
* on the available shader formats and the backends available on the platform.
* When creating shaders, the app must provide the correct shader format for
* the selected backend. If you would like to learn more about why the API
* works this way, there is a detailed
* [blog post](https://moonside.games/posts/layers-all-the-way-down/)
* explaining this situation.
*
* It is optimal for apps to pre-compile the shader formats they might use,
* but for ease of use SDL provides a separate project,
* [SDL_shadercross](https://github.com/libsdl-org/SDL_shadercross)
* , for performing runtime shader cross-compilation. It also has a CLI
* interface for offline precompilation as well.
*
* This is an extremely quick overview that leaves out several important
* details. Already, though, one can see that GPU programming can be quite
* complex! If you just need simple 2D graphics, the
* [Render API](https://wiki.libsdl.org/SDL3/CategoryRender)
* is much easier to use but still hardware-accelerated. That said, even for
* 2D applications the performance benefits and expressiveness of the GPU API
* are significant.
*
* The GPU API targets a feature set with a wide range of hardware support and
* ease of portability. It is designed so that the app won't have to branch
* itself by querying feature support. If you need cutting-edge features with
* limited hardware support, this API is probably not for you.
*
* Examples demonstrating proper usage of this API can be found
* [here](https://github.com/TheSpydog/SDL_gpu_examples)
* .
*
* ## Performance considerations
*
* Here are some basic tips for maximizing your rendering performance.
*
* - Beginning a new render pass is relatively expensive. Use as few render
* passes as you can.
* - Minimize the amount of state changes. For example, binding a pipeline is
* relatively cheap, but doing it hundreds of times when you don't need to
* will slow the performance significantly.
* - Perform your data uploads as early as possible in the frame.
* - Don't churn resources. Creating and releasing resources is expensive.
* It's better to create what you need up front and cache it.
* - Don't use uniform buffers for large amounts of data (more than a matrix
* or so). Use a storage buffer instead.
* - Use cycling correctly. There is a detailed explanation of cycling further
* below.
* - Use culling techniques to minimize pixel writes. The less writing the GPU
* has to do the better. Culling can be a very advanced topic but even
* simple culling techniques can boost performance significantly.
*
* In general try to remember the golden rule of performance: doing things is
* more expensive than not doing things. Don't Touch The Driver!
*
* ## FAQ
*
* **Question: When are you adding more advanced features, like ray tracing or
* mesh shaders?**
*
* Answer: We don't have immediate plans to add more bleeding-edge features,
* but we certainly might in the future, when these features prove worthwhile,
* and reasonable to implement across several platforms and underlying APIs.
* So while these things are not in the "never" category, they are definitely
* not "near future" items either.
*
* **Question: Why is my shader not working?**
*
* Answer: A common oversight when using shaders is not properly laying out
* the shader resources/registers correctly. The GPU API is very strict with
* how it wants resources to be laid out and it's difficult for the API to
* automatically validate shaders to see if they have a compatible layout. See
* the documentation for SDL_CreateGPUShader() and
* SDL_CreateGPUComputePipeline() for information on the expected layout.
*
* Another common issue is not setting the correct number of samplers,
* textures, and buffers in SDL_GPUShaderCreateInfo. If possible use shader
* reflection to extract the required information from the shader
* automatically instead of manually filling in the struct's values.
*
* **Question: My application isn't performing very well. Is this the GPU
* API's fault?**
*
* Answer: No. Long answer: The GPU API is a relatively thin layer over the
* underlying graphics API. While it's possible that we have done something
* inefficiently, it's very unlikely especially if you are relatively
* inexperienced with GPU rendering. Please see the performance tips above and
* make sure you are following them. Additionally, tools like
* [RenderDoc](https://renderdoc.org/)
* can be very helpful for diagnosing incorrect behavior and performance
* issues.
*
* ## System Requirements
*
* ### Vulkan
*
* SDL driver name: "vulkan" (for use in SDL_CreateGPUDevice() and
* SDL_PROP_GPU_DEVICE_CREATE_NAME_STRING)
*
* Supported on Windows, Linux, Nintendo Switch, and certain Android devices.
* Requires Vulkan 1.0 with the following extensions and device features:
*
* - `VK_KHR_swapchain`
* - `VK_KHR_maintenance1`
* - `independentBlend`
* - `imageCubeArray`
* - `depthClamp`
* - `shaderClipDistance`
* - `drawIndirectFirstInstance`
* - `sampleRateShading`
*
* You can remove some of these requirements to increase compatibility with
* Android devices by using these properties when creating the GPU device with
* SDL_CreateGPUDeviceWithProperties():
*
* - SDL_PROP_GPU_DEVICE_CREATE_FEATURE_CLIP_DISTANCE_BOOLEAN
* - SDL_PROP_GPU_DEVICE_CREATE_FEATURE_DEPTH_CLAMPING_BOOLEAN
* - SDL_PROP_GPU_DEVICE_CREATE_FEATURE_INDIRECT_DRAW_FIRST_INSTANCE_BOOLEAN
* - SDL_PROP_GPU_DEVICE_CREATE_FEATURE_ANISOTROPY_BOOLEAN
*
* ### D3D12
*
* SDL driver name: "direct3d12"
*
* Supported on Windows 10 or newer, Xbox One (GDK), and Xbox Series X|S
* (GDK). Requires a GPU that supports DirectX 12 Feature Level 11_0 and
* Resource Binding Tier 2 or above.
*
* You can remove the Tier 2 resource binding requirement to support Intel
* Haswell and Broadwell GPUs by using this property when creating the GPU
* device with SDL_CreateGPUDeviceWithProperties():
*
* - SDL_PROP_GPU_DEVICE_CREATE_D3D12_ALLOW_FEWER_RESOURCE_SLOTS_BOOLEAN
*
* ### Metal
*
* SDL driver name: "metal"
*
* Supported on macOS 10.14+ and iOS/tvOS 13.0+. Hardware requirements vary by
* operating system:
*
* - macOS requires an Apple Silicon or
* [Intel Mac2 family](https://developer.apple.com/documentation/metal/mtlfeatureset/mtlfeatureset_macos_gpufamily2_v1?language=objc)
* GPU
* - iOS/tvOS requires an A9 GPU or newer
* - iOS Simulator and tvOS Simulator are unsupported
*
* ## Coordinate System
*
* The GPU API uses a left-handed coordinate system, following the convention
* of D3D12 and Metal. Specifically:
*
* - **Normalized Device Coordinates:** The lower-left corner has an x,y
* coordinate of `(-1.0, -1.0)`. The upper-right corner is `(1.0, 1.0)`. Z
* values range from `[0.0, 1.0]` where 0 is the near plane.
* - **Viewport Coordinates:** The top-left corner has an x,y coordinate of
* `(0, 0)` and extends to the bottom-right corner at `(viewportWidth,
* viewportHeight)`. +Y is down.
* - **Texture Coordinates:** The top-left corner has an x,y coordinate of
* `(0, 0)` and extends to the bottom-right corner at `(1.0, 1.0)`. +Y is
* down.
*
* If the backend driver differs from this convention (e.g. Vulkan, which has
* an NDC that assumes +Y is down), SDL will automatically convert the
* coordinate system behind the scenes, so you don't need to perform any
* coordinate flipping logic in your shaders.
*
* ## Uniform Data
*
* Uniforms are for passing data to shaders. The uniform data will be constant
* across all executions of the shader.
*
* There are 4 available uniform slots per shader stage (where the stages are
* vertex, fragment, and compute). Uniform data pushed to a slot on a stage
* keeps its value throughout the command buffer until you call the relevant
* Push function on that slot again.
*
* For example, you could write your vertex shaders to read a camera matrix
* from uniform binding slot 0, push the camera matrix at the start of the
* command buffer, and that data will be used for every subsequent draw call.
*
* It is valid to push uniform data during a render or compute pass.
*
* Uniforms are best for pushing small amounts of data. If you are pushing
* more than a matrix or two per call you should consider using a storage
* buffer instead.
*
* ## A Note On Cycling
*
* When using a command buffer, operations do not occur immediately - they
* occur some time after the command buffer is submitted.
*
* When a resource is used in a pending or active command buffer, it is
* considered to be "bound". When a resource is no longer used in any pending
* or active command buffers, it is considered to be "unbound".
*
* If data resources are bound, it is unspecified when that data will be
* unbound unless you acquire a fence when submitting the command buffer and
* wait on it. However, this doesn't mean you need to track resource usage
* manually.
*
* All of the functions and structs that involve writing to a resource have a
* "cycle" bool. SDL_GPUTransferBuffer, SDL_GPUBuffer, and SDL_GPUTexture all
* effectively function as ring buffers on internal resources. When cycle is
* true, if the resource is bound, the cycle rotates to the next unbound
* internal resource, or if none are available, a new one is created. This
* means you don't have to worry about complex state tracking and
* synchronization as long as cycling is correctly employed.
*
* For example: you can call SDL_MapGPUTransferBuffer(), write texture data,
* SDL_UnmapGPUTransferBuffer(), and then SDL_UploadToGPUTexture(). The next
* time you write texture data to the transfer buffer, if you set the cycle
* param to true, you don't have to worry about overwriting any data that is
* not yet uploaded.
*
* Another example: If you are using a texture in a render pass every frame,
* this can cause a data dependency between frames. If you set cycle to true
* in the SDL_GPUColorTargetInfo struct, you can prevent this data dependency.
*
* Cycling will never undefine already bound data. When cycling, all data in
* the resource is considered to be undefined for subsequent commands until
* that data is written again. You must take care not to read undefined data.
*
* Note that when cycling a texture, the entire texture will be cycled, even
* if only part of the texture is used in the call, so you must consider the
* entire texture to contain undefined data after cycling.
*
* You must also take care not to overwrite a section of data that has been
* referenced in a command without cycling first. It is OK to overwrite
* unreferenced data in a bound resource without cycling, but overwriting a
* section of data that has already been referenced will produce unexpected
* results.
*
* ## Debugging
*
* At some point of your GPU journey, you will probably encounter issues that
* are not traceable with regular debugger - for example, your code compiles
* but you get an empty screen, or your shader fails in runtime.
*
* For debugging such cases, there are tools that allow visually inspecting
* the whole GPU frame, every drawcall, every bound resource, memory buffers,
* etc. They are the following, per platform:
*
* * For Windows/Linux, use
* [RenderDoc](https://renderdoc.org/)
* * For MacOS (Metal), use Xcode built-in debugger (Open XCode, go to Debug >
* Debug Executable..., selec
10BC8
t your application, set "GPU Frame Capture" to
* "Metal" in scheme "Options" window, run your app, and click the small
* Metal icon on the bottom to capture a frame)
*
* Aside from that, you may want to enable additional debug layers to receive
* more detailed error messages, based on your GPU backend:
*
* * For D3D12, the debug layer is an optional feature that can be installed
* via "Windows Settings -> System -> Optional features" and adding the
* "Graphics Tools" optional feature.
* * For Vulkan, you will need to install Vulkan SDK on Windows, and on Linux,
* you usually have some sort of `vulkan-validation-layers` system package
* that should be installed.
* * For Metal, it should be enough just to run the application from XCode to
* receive detailed errors or warnings in the output.
*
* Don't hesitate to use tools as RenderDoc when encountering runtime issues
* or unexpected output on screen, quick GPU frame inspection can usually help
* you fix the majority of such problems.
*/
#ifndef SDL_gpu_h_
#define SDL_gpu_h_
#include <SDL3/SDL_stdinc.h>
#include <SDL3/SDL_pixels.h>
#include <SDL3/SDL_properties.h>
#include <SDL3/SDL_rect.h>
#include <SDL3/SDL_surface.h>
#include <SDL3/SDL_video.h>
#include <SDL3/SDL_begin_code.h>
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
/* Type Declarations */
/**
* An opaque handle representing the SDL_GPU context.
*
* \since This struct is available since SDL 3.2.0.
*/
typedef struct SDL_GPUDevice SDL_GPUDevice;
/**
* An opaque handle representing a buffer.
*
* Used for vertices, indices, indirect draw commands, and general compute
* data.
*
* \since This struct is available since SDL 3.2.0.
*
* \sa SDL_CreateGPUBuffer
* \sa SDL_UploadToGPUBuffer
* \sa SDL_DownloadFromGPUBuffer
* \sa SDL_CopyGPUBufferToBuffer
* \sa SDL_BindGPUVertexBuffers
* \sa SDL_BindGPUIndexBuffer
* \sa SDL_BindGPUVertexStorageBuffers
* \sa SDL_BindGPUFragmentStorageBuffers
* \sa SDL_DrawGPUPrimitivesIndirect
* \sa SDL_DrawGPUIndexedPrimitivesIndirect
* \sa SDL_BindGPUComputeStorageBuffers
* \sa SDL_DispatchGPUComputeIndirect
* \sa SDL_ReleaseGPUBuffer
*/
typedef struct SDL_GPUBuffer SDL_GPUBuffer;
/**
* An opaque handle representing a transfer buffer.
*
* Used for transferring data to and from the device.
*
* \since This struct is available since SDL 3.2.0.
*
* \sa SDL_CreateGPUTransferBuffer
* \sa SDL_MapGPUTransferBuffer
* \sa SDL_UnmapGPUTransferBuffer
* \sa SDL_UploadToGPUBuffer
* \sa SDL_UploadToGPUTexture
* \sa SDL_DownloadFromGPUBuffer
* \sa SDL_DownloadFromGPUTexture
* \sa SDL_ReleaseGPUTransferBuffer
*/
typedef struct SDL_GPUTransferBuffer SDL_GPUTransferBuffer;
/**
* An opaque handle representing a texture.
*
* \since This struct is available since SDL 3.2.0.
*
* \sa SDL_CreateGPUTexture
* \sa SDL_UploadToGPUTexture
* \sa SDL_DownloadFromGPUTexture
* \sa SDL_CopyGPUTextureToTexture
* \sa SDL_BindGPUVertexSamplers
* \sa SDL_BindGPUVertexStorageTextures
* \sa SDL_BindGPUFragmentSamplers
* \sa SDL_BindGPUFragmentStorageTextures
* \sa SDL_BindGPUComputeStorageTextures
* \sa SDL_GenerateMipmapsForGPUTexture
* \sa SDL_BlitGPUTexture
* \sa SDL_ReleaseGPUTexture
*/
typedef struct SDL_GPUTexture SDL_GPUTexture;
/**
* An opaque handle representing a sampler.
*
* \since This struct is available since SDL 3.2.0.
*
* \sa SDL_CreateGPUSampler
* \sa SDL_BindGPUVertexSamplers
* \sa SDL_BindGPUFragmentSamplers
* \sa SDL_ReleaseGPUSampler
*/
typedef struct SDL_GPUSampler SDL_GPUSampler;
/**
* An opaque handle representing a compiled shader object.
*
* \since This struct is available since SDL 3.2.0.
*
* \sa SDL_CreateGPUShader
* \sa SDL_CreateGPUGraphicsPipeline
* \sa SDL_ReleaseGPUShader
*/
typedef struct SDL_GPUShader SDL_GPUShader;
/**
* An opaque handle representing a compute pipeline.
*
* Used during compute passes.
*
* \since This struct is available since SDL 3.2.0.
*
* \sa SDL_CreateGPUComputePipeline
* \sa SDL_BindGPUComputePipeline
* \sa SDL_ReleaseGPUComputePipeline
*/
typedef struct SDL_GPUComputePipeline SDL_GPUComputePipeline;
/**
* An opaque handle representing a graphics pipeline.
*
* Used during render passes.
*
* \since This struct is available since SDL 3.2.0.
*
* \sa SDL_CreateGPUGraphicsPipeline
* \sa SDL_BindGPUGraphicsPipeline
* \sa SDL_ReleaseGPUGraphicsPipeline
*/
typedef struct SDL_GPUGraphicsPipeline SDL_GPUGraphicsPipeline;
/**
* An opaque handle representing a command buffer.
*
* Most state is managed via command buffers. When setting state using a
* command buffer, that state is local to the command buffer.
*
* Commands only begin execution on the GPU once SDL_SubmitGPUCommandBuffer is
* called. Once the command buffer is submitted, it is no longer valid to use
* it.
*
* Command buffers are executed in submission order. If you submit command
* buffer A and then command buffer B all commands in A will begin executing
* before any command in B begins executing.
*
* In multi-threading scenarios, you should only access a command buffer on
* the thread you acquired it from.
*
* \since This struct is available since SDL 3.2.0.
*
* \sa SDL_AcquireGPUCommandBuffer
* \sa SDL_SubmitGPUCommandBuffer
* \sa SDL_SubmitGPUCommandBufferAndAcquireFence
*/
typedef struct SDL_GPUCommandBuffer SDL_GPUCommandBuffer;
/**
* An opaque handle representing a render pass.
*
* This handle is transient and should not be held or referenced after
* SDL_EndGPURenderPass is called.
*
* \since This struct is available since SDL 3.2.0.
*
* \sa SDL_BeginGPURenderPass
* \sa SDL_EndGPURenderPass
*/
typedef struct SDL_GPURenderPass SDL_GPURenderPass;
/**
* An opaque handle representing a compute pass.
*
* This handle is transient and should not be held or referenced after
* SDL_EndGPUComputePass is called.
*
* \since This struct is available since SDL 3.2.0.
*
* \sa SDL_BeginGPUComputePass
* \sa SDL_EndGPUComputePass
*/
typedef struct SDL_GPUComputePass SDL_GPUComputePass;
/**
* An opaque handle representing a copy pass.
*
* This handle is transient and should not be held or referenced after
* SDL_EndGPUCopyPass is called.
*
* \since This struct is available since SDL 3.2.0.
*
* \sa SDL_BeginGPUCopyPass
* \sa SDL_EndGPUCopyPass
*/
typedef struct SDL_GPUCopyPass SDL_GPUCopyPass;
/**
* An opaque handle representing a fence.
*
* \since This struct is available since SDL 3.2.0.
*
* \sa SDL_SubmitGPUCommandBufferAndAcquireFence
* \sa SDL_QueryGPUFence
* \sa SDL_WaitForGPUFences
* \sa SDL_ReleaseGPUFence
*/
typedef struct SDL_GPUFence SDL_GPUFence;
/**
* Specifies the primitive topology of a graphics pipeline.
*
* If you are using POINTLIST you must include a point size output in the
* vertex shader.
*
* - For HLSL compiling to SPIRV you must decorate a float output with
* [[vk::builtin("PointSize")]].
* - For GLSL you must set the gl_PointSize builtin.
* - For MSL you must include a float output with the [[point_size]]
* decorator.
*
* Note that sized point topology is totally unsupported on D3D12. Any size
* other than 1 will be ignored. In general, you should avoid using point
* topology for both compatibility and performance reasons. You WILL regret
* using it.
*
* \since This enum is available since SDL 3.2.0.
*
* \sa SDL_CreateGPUGraphicsPipeline
*/
typedef enum SDL_GPUPrimitiveType
{
SDL_GPU_PRIMITIVETYPE_TRIANGLELIST, /**< A series of separate triangles. */
SDL_GPU_PRIMITIVETYPE_TRIANGLESTRIP, /**< A series of connected triangles. */
SDL_GPU_PRIMITIVETYPE_LINELIST, /**< A series of separate lines. */
SDL_GPU_PRIMITIVETYPE_LINESTRIP, /**< A series of connected lines. */
SDL_GPU_PRIMITIVETYPE_POINTLIST /**< A series of separate points. */
} SDL_GPUPrimitiveType;
/**
* Specifies how the contents of a texture attached to a render pass are
* treated at the beginning of the render pass.
*
* \since This enum is available since SDL 3.2.0.
*
* \sa SDL_BeginGPURenderPass
*/
typedef enum SDL_GPULoadOp
{
SDL_GPU_LOADOP_LOAD, /**< The previous contents of the texture will be preserved. */
SDL_GPU_LOADOP_CLEAR, /**< The contents of the texture will be cleared to a color. */
SDL_GPU_LOADOP_DONT_CARE /**< The previous contents of the texture need not be preserved. The contents will be undefined. */
} SDL_GPULoadOp;
/**
* Specifies how the contents of a texture attached to a render pass are
* treated at the end of the render pass.
*
* \since This enum is available since SDL 3.2.0.
*
* \sa SDL_BeginGPURenderPass
*/
typedef enum SDL_GPUStoreOp
{
SDL_GPU_STOREOP_STORE, /**< The contents generated during the render pass will be written to memory. */
SDL_GPU_STOREOP_DONT_CARE, /**< The contents generated during the render pass are not needed and may be discarded. The contents will be undefined. */
SDL_GPU_STOREOP_RESOLVE, /**< The multisample contents generated during the render pass will be resolved to a non-multisample texture. The contents in the multisample texture may then be discarded and will be undefined. */
SDL_GPU_STOREOP_RESOLVE_AND_STORE /**< The multisample contents generated during the render pass will be resolved to a non-multisample texture. The contents in the multisample texture will be written to memory. */
} SDL_GPUStoreOp;
/**
* Specifies the size of elements in an index buffer.
*
* \since This enum is available since SDL 3.2.0.
*
* \sa SDL_CreateGPUGraphicsPipeline
*/
typedef enum SDL_GPUIndexElementSize
{
SDL_GPU_INDEXELEMENTSIZE_16BIT, /**< The index elements are 16-bit. */
SDL_GPU_INDEXELEMENTSIZE_32BIT /**< The index elements are 32-bit. */
} SDL_GPUIndexElementSize;
/**
* Specifies the pixel format of a texture.
*
* Texture format support varies depending on driver, hardware, and usage
* flags. In general, you should use SDL_GPUTextureSupportsFormat to query if
* a format is supported before using it. However, there are a few guaranteed
* formats.
*
* FIXME: Check universal support for 32-bit component formats FIXME: Check
* universal support for SIMULTANEOUS_READ_WRITE
*
* For SAMPLER usage, the following formats are universally supported:
*
* - R8G8B8A8_UNORM
* - B8G8R8A8_UNORM
* - R8_UNORM
* - R8_SNORM
* - R8G8_UNORM
* - R8G8_SNORM
* - R8G8B8A8_SNORM
* - R16_FLOAT
* - R16G16_FLOAT
* - R16G16B16A16_FLOAT
* - R32_FLOAT
* - R32G32_FLOAT
* - R32G32B32A32_FLOAT
* - R11G11B10_UFLOAT
* - R8G8B8A8_UNORM_SRGB
* - B8G8R8A8_UNORM_SRGB
* - D16_UNORM
*
* For COLOR_TARGET usage, the following formats are universally supported:
*
* - R8G8B8A8_UNORM
* - B8G8R8A8_UNORM
* - R8_UNORM
* - R16_FLOAT
* - R16G16_FLOAT
* - R16G16B16A16_FLOAT
* - R32_FLOAT
* - R32G32_FLOAT
* - R32G32B32A32_FLOAT
* - R8_UINT
* - R8G8_UINT
* - R8G8B8A8_UINT
* - R16_UINT
* - R16G16_UINT
* - R16G16B16A16_UINT
* - R8_INT
* - R8G8_INT
* - R8G8B8A8_INT
* - R16_INT
* - R16G16_INT
* - R16G16B16A16_INT
* - R8G8B8A8_UNORM_SRGB
* - B8G8R8A8_UNORM_SRGB
*
* For STORAGE usages, the following formats are universally supported:
*
* - R8G8B8A8_UNORM
* - R8G8B8A8_SNORM
* - R16G16B16A16_FLOAT
* - R32_FLOAT
* - R32G32_FLOAT
* - R32G32B32A32_FLOAT
* - R8G8B8A8_UINT
* - R16G16B16A16_UINT
* - R8G8B8A8_INT
* - R16G16B16A16_INT
*
* For DEPTH_STENCIL_TARGET usage, the following formats are universally
* supported:
*
* - D16_UNORM
* - Either (but not necessarily both!) D24_UNORM or D32_FLOAT
* - Either (but not necessarily both!) D24_UNORM_S8_UINT or D32_FLOAT_S8_UINT
*
* Unless D16_UNORM is sufficient for your purposes, always check which of
* D24/D32 is supported before creating a depth-stencil texture!
*
* \since This enum is available since SDL 3.2.0.
*
* \sa SDL_CreateGPUTexture
* \sa SDL_GPUTextureSupportsFormat
*/
typedef enum SDL_GPUTextureFormat
{
SDL_GPU_TEXTUREFORMAT_INVALID,
/* Unsigned Normalized Float Color Formats */
SDL_GPU_TEXTUREFORMAT_A8_UNORM,
SDL_GPU_TEXTUREFORMAT_R8_UNORM,
SDL_GPU_TEXTUREFORMAT_R8G8_UNORM,
SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM,
SDL_GPU_TEXTUREFORMAT_R16_UNORM,
SDL_GPU_TEXTUREFORMAT_R16G16_UNORM,
SDL_GPU_TEXTUREFORMAT_R16G16B16A16_UNORM,
SDL_GPU_TEXTUREFORMAT_R10G10B10A2_UNORM,
SDL_GPU_TEXTUREFORMAT_B5G6R5_UNORM,
SDL_GPU_TEXTUREFORMAT_B5G5R5A1_UNORM,
SDL_GPU_TEXTUREFORMAT_B4G4R4A4_UNORM,
SDL_GPU_TEXTUREFORMAT_B8G8R8A8_UNORM,
/* Compressed Unsigned Normalized Float Color Formats */
SDL_GPU_TEXTUREFORMAT_BC1_RGBA_UNORM,
SDL_GPU_TEXTUREFORMAT_BC2_RGBA_UNORM,
SDL_GPU_TEXTUREFORMAT_BC3_RGBA_UNORM,
SDL_GPU_TEXTUREFORMAT_BC4_R_UNORM,
SDL_GPU_TEXTUREFORMAT_BC5_RG_UNORM,
SDL_GPU_TEXTUREFORMAT_BC7_RGBA_UNORM,
/* Compressed Signed Float Color Formats */
SDL_GPU_TEXTUREFORMAT_BC6H_RGB_FLOAT,
/* Compressed Unsigned Float Color Formats */
SDL_GPU_TEXTUREFORMAT_BC6H_RGB_UFLOAT,
/* Signed Normalized Float Color Formats */
SDL_GPU_TEXTUREFORMAT_R8_SNORM,
SDL_GPU_TEXTUREFORMAT_R8G8_SNORM,
SDL_GPU_TEXTUREFORMAT_R8G8B8A8_SNORM,
SDL_GPU_TEXTUREFORMAT_R16_SNORM,
SDL_GPU_TEXTUREFORMAT_R16G16_SNORM,
SDL_GPU_TEXTUREFORMAT_R16G16B16A16_SNORM,
/* Signed Float Color Formats */
SDL_GPU_TEXTUREFORMAT_R16_FLOAT,
SDL_GPU_TEXTUREFORMAT_R16G16_FLOAT,
SDL_GPU_TEXTUREFORMAT_R16G16B16A16_FLOAT,
SDL_GPU_TEXTUREFORMAT_R32_FLOAT,
SDL_GPU_TEXTUREFORMAT_R32G32_FLOAT,
SDL_GPU_TEXTUREFORMAT_R32G32B32A32_FLOAT,
/* Unsigned Float Color Formats */
SDL_GPU_TEXTUREFORMAT_R11G11B10_UFLOAT,
/* Unsigned Integer Color Formats */
SDL_GPU_TEXTUREFORMAT_R8_UINT,
SDL_GPU_TEXTUREFORMAT_R8G8_UINT,
SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UINT,
SDL_GPU_TEXTUREFORMAT_R16_UINT,
SDL_GPU_TEXTUREFORMAT_R16G16_UINT,
SDL_GPU_TEXTUREFORMAT_R16G16B16A16_UINT,
SDL_GPU_TEXTUREFORMAT_R32_UINT,
SDL_GPU_TEXTUREFORMAT_R32G32_UINT,
SDL_GPU_TEXTUREFORMAT_R32G32B32A32_UINT,
/* Signed Integer Color Formats */
SDL_GPU_TEXTUREFORMAT_R8_INT,
SDL_GPU_TEXTUREFORMAT_R8G8_INT,
SDL_GPU_TEXTUREFORMAT_R8G8B8A8_INT,
SDL_GPU_TEXTUREFORMAT_R16_INT,
SDL_GPU_TEXTUREFORMAT_R16G16_INT,
SDL_GPU_TEXTUREFORMAT_R16G16B16A16_INT,
SDL_GPU_TEXTUREFORMAT_R32_INT,
SDL_GPU_TEXTUREFORMAT_R32G32_INT,
SDL_GPU_TEXTUREFORMAT_R32G32B32A32_INT,
/* SRGB Unsigned Normalized Color Formats */
SDL_GPU_TEXTUREFORMAT_R8G8B8A8_UNORM_SRGB,
SDL_GPU_TEXTUREFORMAT_B8G8R8A8_UNORM_SRGB,
/* Compressed SRGB Unsigned Normalized Color Formats */
SDL_GPU_TEXTUREFORMAT_BC1_RGBA_UNORM_SRGB,
SDL_GPU_TEXTUREFORMAT_BC2_RGBA_UNORM_SRGB,
SDL_GPU_TEXTUREFORMAT_BC3_RGBA_UNORM_SRGB,
SDL_GPU_TEXTUREFORMAT_BC7_RGBA_UNORM_SRGB,
/* Depth Formats */
SDL_GPU_TEXTUREFORMAT_D16_UNORM,
SDL_GPU_TEXTUREFORMAT_D24_UNORM,
SDL_GPU_TEXTUREFORMAT_D32_FLOAT,
SDL_GPU_TEXTUREFORMAT_D24_UNORM_S8_UINT,
SDL_GPU_TEXTUREFORMAT_D32_FLOAT_S8_UINT,
/* Compressed ASTC Normalized Float Color Formats*/
SDL_GPU_TEXTUREFORMAT_ASTC_4x4_UNORM,
SDL_GPU_TEXTUREFORMAT_ASTC_5x4_UNORM,
SDL_GPU_TEXTUREFORMAT_ASTC_5x5_UNORM,
SDL_GPU_TEXTUREFORMAT_ASTC_6x5_UNORM,
SDL_GPU_TEXTUREFORMAT_ASTC_6x6_UNORM,
SDL_GPU_TEXTUREFORMAT_ASTC_8x5_UNORM,
SDL_GPU_TEXTUREFORMAT_ASTC_8x6_UNORM,
SDL_GPU_TEXTUREFORMAT_ASTC_8x8_UNORM,
SDL_GPU_TEXTUREFORMAT_ASTC_10x5_UNORM,
SDL_GPU_TEXTUREFORMAT_ASTC_10x6_UNORM,
SDL_GPU_TEXTUREFORMAT_ASTC_10x8_UNORM,
SDL_GPU_TEXTUREFORMAT_ASTC_10x10_UNORM,
SDL_GPU_TEXTUREFORMAT_ASTC_12x10_UNORM,
SDL_GPU_TEXTUREFORMAT_ASTC_12x12_UNORM,
/* Compressed SRGB ASTC Normalized Float Color Formats*/
SDL_GPU_TEXTUREFORMAT_ASTC_4x4_UNORM_SRGB,
SDL_GPU_TEXTUREFORMAT_ASTC_5x4_UNORM_SRGB,
SDL_GPU_TEXTUREFORMAT_ASTC_5x5_UNORM_SRGB,
SDL_GPU_TEXTUREFORMAT_ASTC_6x5_UNORM_SRGB,
SDL_GPU_TEXTUREFORMAT_ASTC_6x6_UNORM_SRGB,
SDL_GPU_TEXTUREFORMAT_ASTC_8x5_UNORM_SRGB,
SDL_GPU_TEXTUREFORMAT_ASTC_8x6_UNORM_SRGB,
SDL_GPU_TEXTUREFORMAT_ASTC_8x8_UNORM_SRGB,
SDL_GPU_TEXTUREFORMAT_ASTC_10x5_UNORM_SRGB,
SDL_GPU_TEXTUREFORMAT_ASTC_10x6_UNORM_SRGB,
SDL_GPU_TEXTUREFORMAT_ASTC_10x8_UNORM_SRGB,
SDL_GPU_TEXTUREFORMAT_ASTC_10x10_UNORM_SRGB,
SDL_GPU_TEXTUREFORMAT_ASTC_12x10_UNORM_SRGB,
SDL_GPU_TEXTUREFORMAT_ASTC_12x12_UNORM_SRGB,
/* Compressed ASTC Signed Float Color Formats*/
SDL_GPU_TEXTUREFORMAT_ASTC_4x4_FLOAT,
SDL_GPU_TEXTUREFORMAT_ASTC_5x4_FLOAT,
SDL_GPU_TEXTUREFORMAT_ASTC_5x5_FLOAT,
SDL_GPU_TEXTUREFORMAT_ASTC_6x5_FLOAT,
SDL_GPU_TEXTUREFORMAT_ASTC_6x6_FLOAT,
SDL_GPU_TEXTUREFORMAT_ASTC_8x5_FLOAT,
SDL_GPU_TEXTUREFORMAT_ASTC_8x6_FLOAT,
SDL_GPU_TEXTUREFORMAT_ASTC_8x8_FLOAT,
SDL_GPU_TEXTUREFORMAT_ASTC_10x5_FLOAT,
SDL_GPU_TEXTUREFORMAT_ASTC_10x6_FLOAT,
SDL_GPU_TEXTUREFORMAT_ASTC_10x8_FLOAT,
SDL_GPU_TEXTUREFORMAT_ASTC_10x10_FLOAT,
SDL_GPU_TEXTUREFORMAT_ASTC_12x10_FLOAT,
SDL_GPU_TEXTUREFORMAT_ASTC_12x12_FLOAT
} SDL_GPUTextureFormat;
/**
* Specifies how a texture is intended to be used by the client.
*
* A texture must have at least one usage flag. Note that some usage flag
* combinations are invalid.
*
* With regards to compute storage usage, READ | WRITE means that you can have
* shader A that only writes into the texture and shader B that only reads
* from the texture and bind the same texture to either shader respectively.
* SIMULTANEOUS means that you can do reads and writes within the same shader
* or compute pass. It also implies that atomic ops can be used, since those
* are read-modify-write operations. If you use SIMULTANEOUS, you are
* responsible for avoiding data races, as there is no data synchronization
* within a compute pass. Note that SIMULTANEOUS usage is only supported by a
* limited number of texture formats.
*
* \since This datatype is available since SDL 3.2.0.
*
* \sa SDL_CreateGPUTexture
*/
typedef Uint32 SDL_GPUTextureUsageFlags;
#define SDL_GPU_TEXTUREUSAGE_SAMPLER (1u << 0) /**< Texture supports sampling. */
#define SDL_GPU_TEXTUREUSAGE_COLOR_TARGET (1u << 1) /**< Texture is a color render target. */
#define SDL_GPU_TEXTUREUSAGE_DEPTH_STENCIL_TARGET (1u << 2) /**< Texture is a depth stencil target. */
#define SDL_GPU_TEXTUREUSAGE_GRAPHICS_STORAGE_READ (1u << 3) /**< Texture supports storage reads in graphics stages. */
#define SDL_GPU_TEXTUREUSAGE_COMPUTE_STORAGE_READ (1u << 4) /**< Texture supports storage reads in the compute stage. */
#define SDL_GPU_TEXTUREUSAGE_COMPUTE_STORAGE_WRITE (1u << 5) /**< Texture supports storage writes in the compute stage. */
#define SDL_GPU_TEXTUREUSAGE_COMPUTE_STORAGE_SIMULTANEOUS_READ_WRITE (1u << 6) /**< Texture supports reads and writes in the same compute shader. This is NOT equivalent to READ | WRITE. */
/**
* Specifies the type of a texture.
*
* \since This enum is available since SDL 3.2.0.
*
* \sa SDL_CreateGPUTexture
*/
typedef enum SDL_GPUTextureType
{
SDL_GPU_TEXTURETYPE_2D, /**< The texture is a 2-dimensional image. */
SDL_GPU_TEXTURETYPE_2D_ARRAY, /**< The texture is a 2-dimensional array image. */
SDL_GPU_TEXTURETYPE_3D, /**< The texture is a 3-dimensional image. */
SDL_GPU_TEXTURETYPE_CUBE, /**< The texture is a cube image. */
SDL_GPU_TEXTURETYPE_CUBE_ARRAY /**< The texture is a cube array image. */
} SDL_GPUTextureType;
/**
* Specifies the sample count of a texture.
*
* Used in multisampling. Note that this value only applies when the texture
* is used as a render target.
*
* \since This enum is available since SDL 3.2.0.
*
* \sa SDL_CreateGPUTexture
* \sa SDL_GPUTextureSupportsSampleCount
*/
typedef enum SDL_GPUSampleCount
{
SDL_GPU_SAMPLECOUNT_1, /**< No multisampling. */
SDL_GPU_SAMPLECOUNT_2, /**< MSAA 2x */
SDL_GPU_SAMPLECOUNT_4, /**< MSAA 4x */
SDL_GPU_SAMPLECOUNT_8 /**< MSAA 8x */
} SDL_GPUSampleCount;
/**
* Specifies the face of a cube map.
*
* Can be passed in as the layer field in texture-related structs.
*
* \since This enum is available since SDL 3.2.0.
*/
typedef enum SDL_GPUCubeMapFace
{
SDL_GPU_CUBEMAPFACE_POSITIVEX,
SDL_GPU_CUBEMAPFACE_NEGATIVEX,
SDL_GPU_CUBEMAPFACE_POSITIVEY,
SDL_GPU_CUBEMAPFACE_NEGATIVEY,
SDL_GPU_CUBEMAPFACE_POSITIVEZ,
SDL_GPU_CUBEMAPFACE_NEGATIVEZ
} SDL_GPUCubeMapFace;
/**
* Specifies how a buffer is intended to be used by the client.
*
* A buffer must have at least one usage flag. Note that some usage flag
* combinations are invalid.
*
* Unlike textures, READ | WRITE can be used for simultaneous read-write
* usage. The same data synchronization concerns as textures apply.
*
* If you use a STORAGE flag, the data in the buffer must respect std140
* layout conventions. In practical terms this means you must ensure that vec3
* and vec4 fields are 16-byte aligned.
*
* \since This datatype is available since SDL 3.2.0.
*
* \sa SDL_CreateGPUBuffer
*/
typedef Uint32 SDL_GPUBufferUsageFlags;
#define SDL_GPU_BUFFERUSAGE_VERTEX (1u << 0) /**< Buffer is a vertex buffer. */
#define SDL_GPU_BUFFERUSAGE_INDEX (1u << 1) /**< Buffer is an index buffer. */
#define SDL_GPU_BUFFERUSAGE_INDIRECT (1u << 2) /**< Buffer is an indirect buffer. */
#define SDL_GPU_BUFFERUSAGE_GRAPHICS_STORAGE_READ (1u << 3) /**< Buffer supports storage reads in graphics stages. */
#define SDL_GPU_BUFFERUSAGE_COMPUTE_STORAGE_READ (1u << 4) /**< Buffer supports storage reads in the compute stage. */
#define SDL_GPU_BUFFERUSAGE_COMPUTE_STORAGE_WRITE (1u << 5) /**< Buffer supports storage writes in the compute stage. */
/**
* Specifies how a transfer buffer is intended to be used by the client.
*
* Note that mapping and copying FROM an upload transfer buffer or TO a
* download transfer buffer is undefined behavior.
*
* \since This enum is available since SDL 3.2.0.
*