-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsk_gpu.h
6732 lines (5695 loc) · 247 KB
/
sk_gpu.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
/*Licensed under MIT. See bottom of file for details.
Author - Nick Klingensmith - @koujaku - https://twitter.com/koujaku
Repository - https://github.com/StereoKit/sk_gpu
sk_gpu.h
Docs are not yet here as the project is still somewhat in flight, but check
out some of the example projects in the repository for reference! They're
pretty clean as is :)
Note: this is currently an amalgamated file, so it's best to modify the
individual files in the repository if making modifications.
*/
#pragma once
// You can force sk_gpu to use a specific API, but if you don't, it'll pick
// an API appropriate for the platform it's being compiled for!
//
//#define SKG_FORCE_DIRECT3D11
//#define SKG_FORCE_OPENGL
// You can disable use of D3DCompile to make building this easier sometimes,
// since D3DCompile is primarily used to catch .sks shader files built from
// Linux to run on Windows, and this may not be critical in all cases.
//#define SKG_NO_D3DCOMPILER
// For OpenGL, sk_gpu caches GL state to reduce the number of state switches
// executed using GL's API. When debugging with a tool like RenderDoc, this can
// obscure the draw calls a bit. You can define this to turn off this
// optimization for clearer debug information.
//#define SKG_GL_EXPLICIT_STATE
#if defined( SKG_FORCE_NULL )
#define SKG_NULL
#elif defined( SKG_FORCE_DIRECT3D11 )
#define SKG_DIRECT3D11
#elif defined( SKG_FORCE_OPENGL )
#define SKG_OPENGL
#elif defined( _WIN32 )
#define SKG_DIRECT3D11
#else
#define SKG_OPENGL
#endif
// If we're using OpenGL, set up some additional defines so we know what
// flavor of GL is being used, and how to load it.
#ifdef SKG_OPENGL
#if defined(__EMSCRIPTEN__)
#define _SKG_GL_WEB
#define _SKG_GL_LOAD_EMSCRIPTEN
#elif defined(__ANDROID__)
#define _SKG_GL_ES
#define _SKG_GL_LOAD_EGL
#define _SKG_GL_MAKE_FUNCTIONS
#elif defined(__linux__)
#if defined(SKG_LINUX_EGL)
#define _SKG_GL_ES
#define _SKG_GL_LOAD_EGL
#define _SKG_GL_MAKE_FUNCTIONS
#else
#define _SKG_GL_DESKTOP
#define _SKG_GL_LOAD_GLX
#define _SKG_GL_MAKE_FUNCTIONS
#endif
#elif defined(_WIN32)
#define _SKG_GL_DESKTOP
#define _SKG_GL_LOAD_WGL
#define _SKG_GL_MAKE_FUNCTIONS
#endif
#endif
// Add definitions for how/if we want the functions exported
#ifdef __GNUC__
#define SKG_API
#else
#if defined(SKG_LIB_EXPORT)
#define SKG_API __declspec(dllexport)
#elif defined(SKG_LIB_IMPORT)
#define SKG_API __declspec(dllimport)
#else
#define SKG_API
#endif
#endif
#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
///////////////////////////////////////////
typedef enum skg_buffer_type_ {
skg_buffer_type_vertex,
skg_buffer_type_index,
skg_buffer_type_constant,
skg_buffer_type_compute,
} skg_buffer_type_;
typedef enum skg_tex_type_ {
skg_tex_type_image,
skg_tex_type_cubemap,
skg_tex_type_rendertarget,
skg_tex_type_depth,
} skg_tex_type_;
typedef enum skg_use_ {
skg_use_static = 1 << 0,
skg_use_dynamic = 1 << 2,
skg_use_compute_read = 1 << 3,
skg_use_compute_write = 1 << 4,
skg_use_compute_readwrite = skg_use_compute_read | skg_use_compute_write
} skg_use_;
typedef enum skg_read_ {
skg_read_only,
skg_read_write,
} skg_read_;
typedef enum skg_mip_ {
skg_mip_generate,
skg_mip_none,
} skg_mip_;
typedef enum skg_tex_address_ {
skg_tex_address_repeat,
skg_tex_address_clamp,
skg_tex_address_mirror,
} skg_tex_address_;
typedef enum skg_tex_sample_ {
skg_tex_sample_linear,
skg_tex_sample_point,
skg_tex_sample_anisotropic
} skg_tex_sample_;
typedef enum skg_tex_fmt_ {
skg_tex_fmt_none = 0,
skg_tex_fmt_rgba32,
skg_tex_fmt_rgba32_linear,
skg_tex_fmt_bgra32,
skg_tex_fmt_bgra32_linear,
skg_tex_fmt_rg11b10,
skg_tex_fmt_rgb10a2,
skg_tex_fmt_rgba64u,
skg_tex_fmt_rgba64s,
skg_tex_fmt_rgba64f,
skg_tex_fmt_rgba128,
skg_tex_fmt_r8,
skg_tex_fmt_r16u,
skg_tex_fmt_r16s,
skg_tex_fmt_r16f,
skg_tex_fmt_r32,
skg_tex_fmt_depthstencil,
skg_tex_fmt_depth32,
skg_tex_fmt_depth16,
skg_tex_fmt_r8g8,
skg_tex_fmt_bc1_rgb_srgb,
skg_tex_fmt_bc1_rgb,
skg_tex_fmt_bc3_rgba_srgb,
skg_tex_fmt_bc3_rgba,
skg_tex_fmt_bc4_r,
skg_tex_fmt_bc5_rg,
skg_tex_fmt_bc7_rgba_srgb,
skg_tex_fmt_bc7_rgba,
skg_tex_fmt_etc1_rgb,
skg_tex_fmt_etc2_rgba_srgb,
skg_tex_fmt_etc2_rgba,
skg_tex_fmt_etc2_r11,
skg_tex_fmt_etc2_rg11,
skg_tex_fmt_pvrtc1_rgb_srgb,
skg_tex_fmt_pvrtc1_rgb,
skg_tex_fmt_pvrtc1_rgba_srgb,
skg_tex_fmt_pvrtc1_rgba,
skg_tex_fmt_pvrtc2_rgba_srgb,
skg_tex_fmt_pvrtc2_rgba,
skg_tex_fmt_astc4x4_rgba_srgb,
skg_tex_fmt_astc4x4_rgba,
skg_tex_fmt_atc_rgb,
skg_tex_fmt_atc_rgba,
skg_tex_fmt_max,
skg_tex_fmt_compressed_start = skg_tex_fmt_bc1_rgb_srgb,
} skg_tex_fmt_;
typedef enum skg_ind_fmt_ {
skg_ind_fmt_u32,
skg_ind_fmt_u16,
skg_ind_fmt_u8,
} skg_ind_fmt_;
typedef enum skg_el_semantic_ {
skg_el_semantic_none,
skg_el_semantic_position,
skg_el_semantic_normal,
skg_el_semantic_texcoord,
skg_el_semantic_color,
skg_el_semantic_target_index,
} skg_el_semantic_;
typedef enum skg_stage_ {
skg_stage_vertex = 1 << 0,
skg_stage_pixel = 1 << 1,
skg_stage_compute = 1 << 2,
} skg_stage_;
typedef enum skg_register_ {
skg_register_default,
skg_register_vertex,
skg_register_index,
skg_register_constant,
skg_register_resource,
skg_register_readwrite,
} skg_register_;
typedef enum skg_shader_var_ {
skg_shader_var_none,
skg_shader_var_int,
skg_shader_var_uint,
skg_shader_var_uint8,
skg_shader_var_float,
skg_shader_var_double,
} skg_shader_var_;
typedef enum skg_transparency_ {
skg_transparency_none = 1,
skg_transparency_alpha_to_coverage,
skg_transparency_blend,
skg_transparency_add,
} skg_transparency_;
typedef enum skg_cull_ {
skg_cull_back = 0,
skg_cull_front,
skg_cull_none,
} skg_cull_;
typedef enum skg_depth_test_ {
skg_depth_test_less = 0,
skg_depth_test_less_or_eq,
skg_depth_test_greater,
skg_depth_test_greater_or_eq,
skg_depth_test_equal,
skg_depth_test_not_equal,
skg_depth_test_always,
skg_depth_test_never,
} skg_depth_test_;
typedef enum skg_log_ {
skg_log_info,
skg_log_warning,
skg_log_critical,
} skg_log_;
typedef enum skg_cap_ {
skg_cap_tex_layer_select = 1,
skg_cap_wireframe,
skg_cap_tiled_multisample,
skg_cap_fmt_pvrtc1,
skg_cap_fmt_pvrtc2,
skg_cap_fmt_astc,
skg_cap_fmt_atc,
skg_cap_max,
} skg_cap_;
typedef struct {
uint8_t r, g, b, a;
} skg_color32_t;
typedef struct {
float r, g, b, a;
} skg_color128_t;
typedef enum skg_fmt_ {
skg_fmt_none,
skg_fmt_f64,
skg_fmt_f32,
skg_fmt_f16,
skg_fmt_i32,
skg_fmt_i16,
skg_fmt_i8,
skg_fmt_i32_normalized,
skg_fmt_i16_normalized,
skg_fmt_i8_normalized,
skg_fmt_ui32,
skg_fmt_ui16,
skg_fmt_ui8,
skg_fmt_ui32_normalized,
skg_fmt_ui16_normalized,
skg_fmt_ui8_normalized,
} skg_fmt_;
typedef enum skg_semantic_ {
skg_semantic_none,
skg_semantic_position,
skg_semantic_texcoord,
skg_semantic_normal,
skg_semantic_binormal,
skg_semantic_tangent,
skg_semantic_color,
skg_semantic_psize,
skg_semantic_blendweight,
skg_semantic_blendindices,
} skg_semantic_;
typedef struct skg_vert_component_t {
skg_fmt_ format;
uint8_t count;
skg_semantic_ semantic;
uint8_t semantic_slot;
} skg_vert_component_t;
typedef struct skg_vert_t {
float pos [3];
float norm[3];
float uv [2];
skg_color32_t col;
} skg_vert_t;
typedef struct skg_bind_t {
uint16_t slot;
uint8_t stage_bits;
uint8_t register_type;
} skg_bind_t;
typedef struct skg_shader_var_t {
char name [32];
uint64_t name_hash;
char extra[64];
uint32_t offset;
uint32_t size;
uint16_t type;
uint16_t type_count;
} skg_shader_var_t;
typedef struct skg_shader_buffer_t {
char name[32];
uint64_t name_hash;
skg_bind_t bind;
uint32_t size;
void *defaults;
uint32_t var_count;
skg_shader_var_t *vars;
} skg_shader_buffer_t;
typedef struct skg_shader_resource_t {
char name [32];
uint64_t name_hash;
char value[64];
char tags [64];
skg_bind_t bind;
} skg_shader_resource_t;
typedef struct skg_shader_ops_t {
int32_t total;
int32_t tex_read;
int32_t dynamic_flow;
} skg_shader_ops_t;
typedef struct skg_shader_meta_t {
char name[256];
uint32_t buffer_count;
skg_shader_buffer_t *buffers;
uint32_t resource_count;
skg_shader_resource_t *resources;
int32_t references;
int32_t global_buffer_id;
skg_vert_component_t *vertex_inputs;
int32_t vertex_input_count;
skg_shader_ops_t ops_vertex;
skg_shader_ops_t ops_pixel;
} skg_shader_meta_t;
///////////////////////////////////////////
#if defined(SKG_DIRECT3D11)
// Forward declare our D3D structs, so we don't have to pay the cost for
// including the d3d11 header in every file that includes this one.
struct ID3D11Buffer;
struct ID3D11ShaderResourceView;
struct ID3D11UnorderedAccessView;
struct ID3D11InputLayout;
struct ID3D11VertexShader;
struct ID3D11PixelShader;
struct ID3D11ComputeShader;
struct ID3D11BlendState;
struct ID3D11RasterizerState;
struct ID3D11DepthStencilState;
struct ID3D11SamplerState;
struct ID3D11RenderTargetView;
struct ID3D11DepthStencilView;
struct IDXGISwapChain1;
struct ID3D11Texture2D;
///////////////////////////////////////////
typedef struct skg_buffer_t {
skg_use_ use;
skg_buffer_type_ type;
uint32_t stride;
ID3D11Buffer *_buffer;
ID3D11ShaderResourceView *_resource;
ID3D11UnorderedAccessView *_unordered;
} skg_buffer_t;
typedef struct skg_computebuffer_t {
skg_read_ read_write;
skg_buffer_t buffer;
ID3D11ShaderResourceView *_resource;
ID3D11UnorderedAccessView *_unordered;
} skg_computebuffer_t;
typedef struct skg_mesh_t {
ID3D11Buffer* _ind_buffer;
ID3D11Buffer* _vert_buffer;
} skg_mesh_t;
typedef struct skg_shader_stage_t {
skg_stage_ type;
void *_shader;
ID3D11InputLayout *_layout;
} skg_shader_stage_t;
typedef struct skg_shader_t {
skg_shader_meta_t *meta;
ID3D11VertexShader *_vertex;
ID3D11PixelShader *_pixel;
ID3D11ComputeShader *_compute;
ID3D11InputLayout *_layout;
} skg_shader_t;
typedef struct skg_pipeline_t {
skg_transparency_ transparency;
skg_cull_ cull;
bool wireframe;
bool depth_write;
bool scissor;
skg_depth_test_ depth_test;
skg_shader_meta_t *meta;
ID3D11VertexShader *_vertex;
ID3D11PixelShader *_pixel;
ID3D11InputLayout *_layout;
ID3D11BlendState *_blend;
ID3D11RasterizerState *_rasterize;
ID3D11DepthStencilState *_depth;
} skg_pipeline_t;
typedef struct skg_tex_t {
int32_t width;
int32_t height;
int32_t array_count;
int32_t array_start;
int32_t multisample;
skg_use_ use;
skg_tex_type_ type;
skg_tex_fmt_ format;
skg_mip_ mips;
ID3D11Texture2D *_texture;
ID3D11SamplerState *_sampler;
ID3D11ShaderResourceView *_resource;
ID3D11UnorderedAccessView *_unordered;
ID3D11RenderTargetView *_target_view;
ID3D11RenderTargetView **_target_array_view;
ID3D11DepthStencilView *_depth_view;
} skg_tex_t;
typedef struct skg_swapchain_t {
int32_t width;
int32_t height;
skg_tex_t _target;
skg_tex_t _depth;
IDXGISwapChain1 *_swapchain;
} skg_swapchain_t;
typedef struct skg_platform_data_t {
void *_d3d11_device;
void *_d3d11_deferred_context;
void *_d3d_deferred_mtx;
uint32_t _d3d_main_thread_id;
} skg_platform_data_t;
#elif defined(SKG_DIRECT3D12)
#elif defined(SKG_VULKAN)
#elif defined(SKG_OPENGL)
#define SKG_MANUAL_SRGB
///////////////////////////////////////////
typedef struct skg_buffer_t {
skg_use_ use;
skg_buffer_type_ type;
uint32_t stride;
uint32_t _target;
uint32_t _buffer;
} skg_buffer_t;
typedef struct skg_mesh_t {
uint32_t _ind_buffer;
uint32_t _vert_buffer;
uint32_t _layout;
} skg_mesh_t;
typedef struct skg_shader_stage_t {
skg_stage_ type;
uint32_t _shader;
} skg_shader_stage_t;
typedef struct skg_shader_t {
skg_shader_meta_t *meta;
uint32_t _vertex;
uint32_t _pixel;
uint32_t _program;
uint32_t _compute;
} skg_shader_t;
typedef struct skg_pipeline_t {
skg_transparency_ transparency;
skg_cull_ cull;
bool wireframe;
bool depth_write;
bool scissor;
skg_depth_test_ depth_test;
skg_shader_meta_t*meta;
skg_shader_t _shader;
} skg_pipeline_t;
typedef struct skg_tex_t {
int32_t width;
int32_t height;
int32_t array_count;
int32_t array_start;
int32_t multisample;
skg_use_ use;
skg_tex_type_ type;
skg_tex_fmt_ format;
skg_mip_ mips;
uint32_t _texture;
uint32_t _framebuffer;
uint32_t *_framebuffer_layers;
uint32_t _physical_multisample;
uint32_t _target;
uint32_t _access;
uint32_t _format;
skg_tex_address_ _address;
skg_tex_sample_ _sample;
int32_t _anisotropy;
} skg_tex_t;
typedef struct skg_swapchain_t {
int32_t width;
int32_t height;
#if defined(_SKG_GL_LOAD_WGL)
void *_hdc;
void *_hwnd;
#elif defined(_SKG_GL_LOAD_EGL)
void *_egl_surface;
#elif defined(_SKG_GL_LOAD_GLX)
void *_x_window;
#elif defined(_SKG_GL_LOAD_EMSCRIPTEN) && defined(SKG_MANUAL_SRGB)
skg_tex_t _surface;
skg_tex_t _surface_depth;
skg_shader_t _convert_shader;
skg_pipeline_t _convert_pipe;
skg_buffer_t _quad_vbuff;
skg_buffer_t _quad_ibuff;
skg_mesh_t _quad_mesh;
#endif
} skg_swapchain_t;
typedef struct skg_platform_data_t {
#if defined(_SKG_GL_LOAD_EGL)
void *_egl_display;
void *_egl_config;
void *_egl_context;
#elif defined(_SKG_GL_LOAD_WGL)
void *_gl_hdc;
void *_gl_hrc;
#elif defined(_SKG_GL_LOAD_GLX)
void *_x_display;
void *_visual_id;
void *_glx_fb_config;
void *_glx_drawable;
void *_glx_context;
#endif
} skg_platform_data_t;
#elif defined(SKG_NULL)
///////////////////////////////////////////
typedef struct skg_buffer_t {
skg_use_ use;
skg_buffer_type_ type;
uint32_t stride;
} skg_buffer_t;
typedef struct skg_computebuffer_t {
skg_read_ read_write;
skg_buffer_t buffer;
} skg_computebuffer_t;
typedef struct skg_mesh_t {
} skg_mesh_t;
typedef struct skg_shader_stage_t {
skg_stage_ type;
} skg_shader_stage_t;
typedef struct skg_shader_t {
skg_shader_meta_t* meta;
} skg_shader_t;
typedef struct skg_pipeline_t {
skg_transparency_ transparency;
skg_cull_ cull;
bool wireframe;
bool depth_write;
bool scissor;
skg_depth_test_ depth_test;
skg_shader_meta_t* meta;
} skg_pipeline_t;
typedef struct skg_tex_t {
int32_t width;
int32_t height;
int32_t array_count;
int32_t array_start;
int32_t multisample;
skg_use_ use;
skg_tex_type_ type;
skg_tex_fmt_ format;
skg_mip_ mips;
} skg_tex_t;
typedef struct skg_swapchain_t {
int32_t width;
int32_t height;
} skg_swapchain_t;
typedef struct skg_platform_data_t {
} skg_platform_data_t;
#endif
///////////////////////////////////////////
SKG_API void skg_setup_xlib (void *dpy, void *vi, void *fbconfig, void *drawable);
SKG_API int32_t skg_init (const char *app_name, void *adapter_id);
SKG_API const char* skg_adapter_name ();
SKG_API void skg_shutdown ();
SKG_API void skg_callback_log (void (*callback)(skg_log_ level, const char *text));
SKG_API void skg_callback_file_read (bool (*callback)(const char *filename, void **out_data, size_t *out_size));
SKG_API skg_platform_data_t skg_get_platform_data ();
SKG_API bool skg_capability (skg_cap_ capability);
SKG_API void skg_event_begin (const char *name);
SKG_API void skg_event_end ();
SKG_API void skg_draw_begin ();
SKG_API void skg_draw (int32_t index_start, int32_t index_base, int32_t index_count, int32_t instance_count);
SKG_API void skg_compute (uint32_t thread_count_x, uint32_t thread_count_y, uint32_t thread_count_z);
SKG_API void skg_viewport (const int32_t *xywh);
SKG_API void skg_viewport_get (int32_t *out_xywh);
SKG_API void skg_scissor (const int32_t *xywh);
SKG_API void skg_target_clear (bool depth, const float *clear_color_4);
SKG_API skg_buffer_t skg_buffer_create (const void *data, uint32_t size_count, uint32_t size_stride, skg_buffer_type_ type, skg_use_ use);
SKG_API void skg_buffer_name ( skg_buffer_t *buffer, const char* name);
SKG_API bool skg_buffer_is_valid (const skg_buffer_t *buffer);
SKG_API void skg_buffer_set_contents ( skg_buffer_t *buffer, const void *data, uint32_t size_bytes);
SKG_API void skg_buffer_get_contents (const skg_buffer_t *buffer, void *ref_buffer, uint32_t buffer_size);
SKG_API void skg_buffer_bind (const skg_buffer_t *buffer, skg_bind_t slot_vc);
SKG_API void skg_buffer_clear ( skg_bind_t bind);
SKG_API void skg_buffer_destroy ( skg_buffer_t *buffer);
SKG_API skg_mesh_t skg_mesh_create (const skg_buffer_t *vert_buffer, const skg_buffer_t *ind_buffer);
SKG_API void skg_mesh_name ( skg_mesh_t *mesh, const char* name);
SKG_API void skg_mesh_set_verts ( skg_mesh_t *mesh, const skg_buffer_t *vert_buffer);
SKG_API void skg_mesh_set_inds ( skg_mesh_t *mesh, const skg_buffer_t *ind_buffer);
SKG_API void skg_mesh_bind (const skg_mesh_t *mesh);
SKG_API void skg_mesh_destroy ( skg_mesh_t *mesh);
SKG_API skg_shader_stage_t skg_shader_stage_create (const void *shader_data, size_t shader_size, skg_stage_ type);
SKG_API void skg_shader_stage_destroy (skg_shader_stage_t *stage);
SKG_API skg_shader_t skg_shader_create_file (const char *sks_filename);
SKG_API skg_shader_t skg_shader_create_memory (const void *sks_memory, size_t sks_memory_size);
SKG_API skg_shader_t skg_shader_create_manual (skg_shader_meta_t *meta, skg_shader_stage_t v_shader, skg_shader_stage_t p_shader, skg_shader_stage_t c_shader);
SKG_API void skg_shader_name ( skg_shader_t *shader, const char* name);
SKG_API bool skg_shader_is_valid (const skg_shader_t *shader);
SKG_API void skg_shader_compute_bind (const skg_shader_t *shader);
SKG_API skg_bind_t skg_shader_get_bind (const skg_shader_t *shader, const char *name);
SKG_API int32_t skg_shader_get_var_count (const skg_shader_t *shader);
SKG_API int32_t skg_shader_get_var_index (const skg_shader_t *shader, const char *name);
SKG_API int32_t skg_shader_get_var_index_h (const skg_shader_t *shader, uint64_t name_hash);
SKG_API const skg_shader_var_t *skg_shader_get_var_info (const skg_shader_t *shader, int32_t var_index);
SKG_API void skg_shader_destroy ( skg_shader_t *shader);
SKG_API skg_pipeline_t skg_pipeline_create (skg_shader_t *shader);
SKG_API void skg_pipeline_name ( skg_pipeline_t *pipeline, const char* name);
SKG_API void skg_pipeline_bind (const skg_pipeline_t *pipeline);
SKG_API void skg_pipeline_set_transparency( skg_pipeline_t *pipeline, skg_transparency_ transparency);
SKG_API skg_transparency_ skg_pipeline_get_transparency(const skg_pipeline_t *pipeline);
SKG_API void skg_pipeline_set_cull ( skg_pipeline_t *pipeline, skg_cull_ cull);
SKG_API skg_cull_ skg_pipeline_get_cull (const skg_pipeline_t *pipeline);
SKG_API void skg_pipeline_set_wireframe ( skg_pipeline_t *pipeline, bool wireframe);
SKG_API bool skg_pipeline_get_wireframe (const skg_pipeline_t *pipeline);
SKG_API void skg_pipeline_set_depth_write ( skg_pipeline_t *pipeline, bool write);
SKG_API bool skg_pipeline_get_depth_write (const skg_pipeline_t *pipeline);
SKG_API void skg_pipeline_set_depth_test ( skg_pipeline_t *pipeline, skg_depth_test_ test);
SKG_API skg_depth_test_ skg_pipeline_get_depth_test (const skg_pipeline_t *pipeline);
SKG_API void skg_pipeline_set_scissor ( skg_pipeline_t *pipeline, bool enable);
SKG_API bool skg_pipeline_get_scissor (const skg_pipeline_t *pipeline);
SKG_API void skg_pipeline_destroy ( skg_pipeline_t *pipeline);
SKG_API skg_swapchain_t skg_swapchain_create (void *hwnd, skg_tex_fmt_ format, skg_tex_fmt_ depth_format, int32_t requested_width, int32_t requested_height);
SKG_API void skg_swapchain_resize ( skg_swapchain_t *swapchain, int32_t width, int32_t height);
SKG_API void skg_swapchain_present ( skg_swapchain_t *swapchain);
SKG_API void skg_swapchain_bind ( skg_swapchain_t *swapchain);
SKG_API void skg_swapchain_destroy ( skg_swapchain_t *swapchain);
SKG_API skg_tex_t skg_tex_create_from_existing (void *native_tex, skg_tex_type_ type, skg_tex_fmt_ format, int32_t width, int32_t height, int32_t array_count, int32_t multisample, int32_t framebuffer_multisample);
SKG_API skg_tex_t skg_tex_create_from_layer (void *native_tex, skg_tex_type_ type, skg_tex_fmt_ format, int32_t width, int32_t height, int32_t array_layer);
SKG_API skg_tex_t skg_tex_create (skg_tex_type_ type, skg_use_ use, skg_tex_fmt_ format, skg_mip_ mip_maps);
SKG_API void skg_tex_name ( skg_tex_t *tex, const char* name);
SKG_API bool skg_tex_is_valid (const skg_tex_t *tex);
SKG_API void skg_tex_copy_to (const skg_tex_t *tex, int32_t tex_surface, skg_tex_t *destination, int32_t dest_surface);
SKG_API void skg_tex_copy_to_swapchain (const skg_tex_t *tex, skg_swapchain_t *destination);
SKG_API void skg_tex_attach_depth ( skg_tex_t *tex, skg_tex_t *depth);
SKG_API void skg_tex_settings ( skg_tex_t *tex, skg_tex_address_ address, skg_tex_sample_ sample, int32_t anisotropy);
SKG_API void skg_tex_set_contents ( skg_tex_t *tex, const void *data, int32_t width, int32_t height);
SKG_API void skg_tex_set_contents_arr ( skg_tex_t *tex, const void**array_data, int32_t array_count, int32_t mip_count, int32_t width, int32_t height, int32_t multisample);
SKG_API bool skg_tex_get_contents ( skg_tex_t *tex, void *ref_data, size_t data_size);
SKG_API bool skg_tex_get_mip_contents ( skg_tex_t *tex, int32_t mip_level, void *ref_data, size_t data_size);
SKG_API bool skg_tex_get_mip_contents_arr ( skg_tex_t *tex, int32_t mip_level, int32_t arr_index, void *ref_data, size_t data_size);
SKG_API bool skg_tex_gen_mips ( skg_tex_t *tex);
SKG_API void* skg_tex_get_native (const skg_tex_t *tex);
SKG_API void skg_tex_bind (const skg_tex_t *tex, skg_bind_t bind);
SKG_API void skg_tex_clear (skg_bind_t bind);
SKG_API void skg_tex_target_bind ( skg_tex_t *render_target, int32_t layer_idx, int32_t mip_level);
SKG_API skg_tex_t *skg_tex_target_get ();
SKG_API void skg_tex_destroy ( skg_tex_t *tex);
SKG_API int64_t skg_tex_fmt_to_native (skg_tex_fmt_ format);
SKG_API skg_tex_fmt_ skg_tex_fmt_from_native (int64_t format);
SKG_API uint32_t skg_tex_fmt_memory (skg_tex_fmt_ format, int32_t width, int32_t height);
SKG_API uint32_t skg_tex_fmt_block_px (skg_tex_fmt_ format);
SKG_API uint32_t skg_tex_fmt_block_size (skg_tex_fmt_ format);
SKG_API uint32_t skg_tex_fmt_pitch (skg_tex_fmt_ format, int32_t width);
SKG_API bool skg_tex_fmt_supported (skg_tex_fmt_ format);
SKG_API bool skg_tex_fmt_is_compressed (skg_tex_fmt_ format);
///////////////////////////////////////////
// API independant functions //
///////////////////////////////////////////
typedef enum {
skg_shader_lang_hlsl,
skg_shader_lang_spirv,
skg_shader_lang_glsl,
skg_shader_lang_glsl_es,
skg_shader_lang_glsl_web,
} skg_shader_lang_;
typedef struct {
skg_shader_lang_ language;
skg_stage_ stage;
uint32_t code_size;
void *code;
} skg_shader_file_stage_t;
typedef struct {
skg_shader_meta_t *meta;
uint32_t stage_count;
skg_shader_file_stage_t *stages;
} skg_shader_file_t;
///////////////////////////////////////////
SKG_API void skg_log (skg_log_ level, const char *text);
SKG_API void skg_logf (skg_log_ level, const char *text, ...);
SKG_API bool skg_read_file (const char *filename, void **out_data, size_t *out_size);
SKG_API uint64_t skg_hash (const char *string);
SKG_API uint32_t skg_mip_count (int32_t width, int32_t height);
SKG_API void skg_mip_dimensions (int32_t width, int32_t height, int32_t mip_level, int32_t *out_width, int32_t *out_height);
SKG_API int32_t skg_fmt_size (skg_fmt_ format);
SKG_API skg_color32_t skg_col_hsv32 (float hue, float saturation, float value, float alpha);
SKG_API skg_color128_t skg_col_hsv128 (float hue, float saturation, float value, float alpha);
SKG_API skg_color32_t skg_col_hsl32 (float hue, float saturation, float lightness, float alpha);
SKG_API skg_color128_t skg_col_hsl128 (float hue, float saturation, float lightness, float alpha);
SKG_API skg_color32_t skg_col_hcy32 (float hue, float chroma, float lightness, float alpha);
SKG_API skg_color128_t skg_col_hcy128 (float hue, float chroma, float lightness, float alpha);
SKG_API skg_color32_t skg_col_lch32 (float hue, float chroma, float lightness, float alpha);
SKG_API skg_color128_t skg_col_lch128 (float hue, float chroma, float lightness, float alpha);
SKG_API skg_color32_t skg_col_helix32 (float hue, float saturation, float lightness, float alpha);
SKG_API skg_color128_t skg_col_helix128 (float hue, float saturation, float lightness, float alpha);
SKG_API skg_color32_t skg_col_jab32 (float j, float a, float b, float alpha);
SKG_API skg_color128_t skg_col_jab128 (float j, float a, float b, float alpha);
SKG_API skg_color32_t skg_col_jsl32 (float hue, float saturation, float lightness, float alpha);
SKG_API skg_color128_t skg_col_jsl128 (float hue, float saturation, float lightness, float alpha);
SKG_API skg_color32_t skg_col_lab32 (float l, float a, float b, float alpha);
SKG_API skg_color128_t skg_col_lab128 (float l, float a, float b, float alpha);
SKG_API skg_color128_t skg_col_rgb_to_lab128 (skg_color128_t rgb);
SKG_API skg_color128_t skg_col_to_srgb (skg_color128_t rgb_linear);
SKG_API skg_color128_t skg_col_to_linear (skg_color128_t srgb);
SKG_API bool skg_shader_file_verify (const void *file_memory, size_t file_size, uint16_t *out_version, char *out_name, size_t out_name_size);
SKG_API bool skg_shader_file_load_memory (const void *file_memory, size_t file_size, skg_shader_file_t *out_file);
SKG_API bool skg_shader_file_load (const char *file, skg_shader_file_t *out_file);
SKG_API skg_shader_stage_t skg_shader_file_create_stage (const skg_shader_file_t *file, skg_stage_ stage);
SKG_API void skg_shader_file_destroy ( skg_shader_file_t *file);
SKG_API skg_bind_t skg_shader_meta_get_bind (const skg_shader_meta_t *meta, const char *name);
SKG_API int32_t skg_shader_meta_get_var_count (const skg_shader_meta_t *meta);
SKG_API int32_t skg_shader_meta_get_var_index (const skg_shader_meta_t *meta, const char *name);
SKG_API int32_t skg_shader_meta_get_var_index_h(const skg_shader_meta_t *meta, uint64_t name_hash);
SKG_API const skg_shader_var_t *skg_shader_meta_get_var_info (const skg_shader_meta_t *meta, int32_t var_index);
SKG_API void skg_shader_meta_reference (skg_shader_meta_t *meta);
SKG_API void skg_shader_meta_release (skg_shader_meta_t *meta);
///////////////////////////////////////////
// Implementations! //
///////////////////////////////////////////
#ifdef SKG_IMPL
#ifdef SKG_DIRECT3D11
///////////////////////////////////////////
// Direct3D11 Implementation //
///////////////////////////////////////////
#pragma comment(lib,"D3D11.lib")
#pragma comment(lib,"Dxgi.lib")
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
#include <d3d11.h>
#include <dxgi1_6.h>
#if !defined(SKG_NO_D3DCOMPILER)
#pragma comment(lib,"d3dcompiler.lib")
#include <d3dcompiler.h>
#endif
#include <math.h>
#include <stdio.h>
// Manually defining this lets us skip d3dcommon.h and dxguid.lib
const GUID WKPDID_D3DDebugObjectName = { 0x429b8c22, 0x9188, 0x4b0c, { 0x87,0x42,0xac,0xb0,0xbf,0x85,0xc2,0x00 } };
///////////////////////////////////////////
ID3D11Device *d3d_device = nullptr;
ID3D11DeviceContext *d3d_context = nullptr;
ID3D11InfoQueue *d3d_info = nullptr;
ID3D11RasterizerState *d3d_rasterstate = nullptr;
ID3D11DepthStencilState *d3d_depthstate = nullptr;
skg_tex_t *d3d_active_rendertarget = nullptr;
char *d3d_adapter_name = nullptr;
ID3D11DeviceContext *d3d_deferred = nullptr;
HANDLE d3d_deferred_mtx= nullptr;
DWORD d3d_main_thread = 0;
#if defined(_DEBUG)
#include <d3d11_1.h>
ID3DUserDefinedAnnotation *d3d_annotate = nullptr;
#endif
///////////////////////////////////////////
bool skg_tex_make_view(skg_tex_t *tex, uint32_t mip_count, uint32_t array_start, bool use_in_shader);
DXGI_FORMAT skg_ind_to_dxgi (skg_ind_fmt_ format);
template <typename T>
void skg_downsample_1(T *data, int32_t width, int32_t height, T **out_data, int32_t *out_width, int32_t *out_height);
template <typename T>
void skg_downsample_4(T *data, T data_max, int32_t width, int32_t height, T **out_data, int32_t *out_width, int32_t *out_height);
///////////////////////////////////////////
int32_t skg_init(const char *, void *adapter_id) {
UINT creation_flags = D3D11_CREATE_DEVICE_BGRA_SUPPORT;
#if defined(_DEBUG)
creation_flags |= D3D11_CREATE_DEVICE_DEBUG;
skg_log(skg_log_info, "Requesting debug Direct3D context.");
#endif
// Find the right adapter to use:
IDXGIAdapter1 *final_adapter = nullptr;
IDXGIAdapter1 *curr_adapter = nullptr;
IDXGIFactory1 *dxgi_factory = nullptr;
int curr = 0;
SIZE_T video_mem = 0;
CreateDXGIFactory1(__uuidof(IDXGIFactory1), (void **)(&dxgi_factory));
while (dxgi_factory->EnumAdapters1(curr++, &curr_adapter) == S_OK) {
DXGI_ADAPTER_DESC1 adapterDesc;
curr_adapter->GetDesc1(&adapterDesc);
// By default, we pick the adapter that has the most available memory
if (adapterDesc.DedicatedVideoMemory > video_mem) {
video_mem = adapterDesc.DedicatedVideoMemory;
if (final_adapter != nullptr) final_adapter->Release();
final_adapter = curr_adapter;
final_adapter->AddRef();
}
// If the user asks for a specific device though, return it right away!
if (adapter_id != nullptr && memcmp(&adapterDesc.AdapterLuid, adapter_id, sizeof(LUID)) == 0) {
if (final_adapter != nullptr) final_adapter->Release();
final_adapter = curr_adapter;
final_adapter->AddRef();
break;
}
curr_adapter->Release();
}
dxgi_factory->Release();
// Create the interface to the graphics card
D3D_FEATURE_LEVEL feature_levels[] = { D3D_FEATURE_LEVEL_11_1, D3D_FEATURE_LEVEL_11_0 };
HRESULT hr = D3D11CreateDevice(final_adapter, final_adapter == nullptr ? D3D_DRIVER_TYPE_HARDWARE : D3D_DRIVER_TYPE_UNKNOWN, 0, creation_flags, feature_levels, _countof(feature_levels), D3D11_SDK_VERSION, &d3d_device, nullptr, &d3d_context);
if (FAILED(hr)) {
// Message that we failed to initialize with the selected adapter.
if (final_adapter != nullptr) {
DXGI_ADAPTER_DESC1 final_adapter_info;
final_adapter->GetDesc1(&final_adapter_info);
skg_logf(skg_log_critical, "Failed starting Direct3D 11 adapter '%ls': 0x%08X", &final_adapter_info.Description, hr);
final_adapter->Release();
} else {
skg_logf(skg_log_critical, "Failed starting Direct3D 11 adapter 'Default adapter': 0x%08X", hr);
}
// Get a human readable description of that error message.
char *error_text = NULL;
FormatMessageA(
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
hr,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(char*)&error_text, 0,
NULL);
skg_log(skg_log_critical, error_text);
LocalFree(error_text);
return 0;
}
// Create a deferred context for making some multithreaded context calls.
hr = d3d_device->CreateDeferredContext(0, &d3d_deferred);
if (FAILED(hr)) {
skg_logf(skg_log_critical, "Failed to create a deferred context: 0x%08X", hr);
}
d3d_deferred_mtx = CreateMutex(nullptr, false, nullptr);
d3d_main_thread = GetCurrentThreadId();
// Notify what device and API we're using
if (final_adapter != nullptr) {
DXGI_ADAPTER_DESC1 final_adapter_info;
final_adapter->GetDesc1(&final_adapter_info);
int32_t utf8_size = WideCharToMultiByte(CP_UTF8, 0, final_adapter_info.Description, -1, NULL, 0, NULL, NULL);
d3d_adapter_name = (char*)malloc(utf8_size * sizeof(char));
WideCharToMultiByte(CP_UTF8, 0, final_adapter_info.Description, -1, d3d_adapter_name, utf8_size, NULL, NULL);
skg_logf(skg_log_info, "Using Direct3D 11: vendor 0x%04X, device 0x%04X", final_adapter_info.VendorId, final_adapter_info.DeviceId);
final_adapter->Release();
} else {
const char default_name[] = "Default Device";
d3d_adapter_name = (char*)malloc(sizeof(default_name));
memcpy(d3d_adapter_name, default_name, sizeof(default_name));
}
skg_logf(skg_log_info, "Device: %s", d3d_adapter_name);
// Hook into debug information
ID3D11Debug *d3d_debug = nullptr;
if (SUCCEEDED(d3d_device->QueryInterface(__uuidof(ID3D11Debug), (void**)&d3d_debug))) {
d3d_info = nullptr;
if (SUCCEEDED(d3d_debug->QueryInterface(__uuidof(ID3D11InfoQueue), (void**)&d3d_info))) {
D3D11_MESSAGE_ID hide[] = {
D3D11_MESSAGE_ID_SETPRIVATEDATA_CHANGINGPARAMS,
(D3D11_MESSAGE_ID)351,
};
D3D11_INFO_QUEUE_FILTER filter = {};
filter.DenyList.NumIDs = _countof(hide);
filter.DenyList.pIDList = hide;