forked from floooh/sokol
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsokol_gfx.h
8963 lines (8216 loc) · 356 KB
/
sokol_gfx.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
#pragma once
/*
sokol_gfx.h -- simple 3D API wrapper
Do this:
#define SOKOL_IMPL
before you include this file in *one* C or C++ file to create the
implementation.
In the same place define one of the following to select the rendering
backend:
#define SOKOL_GLCORE33
#define SOKOL_GLES2
#define SOKOL_GLES3
#define SOKOL_D3D11
#define SOKOL_METAL
I.e. for the GL 3.3 Core Profile it should look like this:
#include ...
#include ...
#define SOKOL_IMPL
#define SOKOL_GLCORE33
#include "sokol_gfx.h"
To enable shader compilation support in the D3D11 backend:
#define SOKOL_D3D11_SHADER_COMPILER
If SOKOL_D3D11_SHADER_COMPILER is enabled, the executable will link against
d3dcompiler.lib (d3dcompiler_47.dll).
Optionally provide the following defines with your own implementations:
SOKOL_ASSERT(c) - your own assert macro (default: assert(c))
SOKOL_MALLOC(s) - your own malloc function (default: malloc(s))
SOKOL_FREE(p) - your own free function (default: free(p))
SOKOL_LOG(msg) - your own logging function (default: puts(msg))
SOKOL_UNREACHABLE() - a guard macro for unreachable code (default: assert(false))
API usage validation macros:
SOKOL_VALIDATE_BEGIN() - begin a validation block (default:_sg_validate_begin())
SOKOL_VALIDATE(cond, err) - like assert but for API validation (default: _sg_validate(cond, err))
SOKOL_VALIDATE_END() - end a validation block, return true if all checks in block passed (default: bool _sg_validate())
If you don't want validation errors to be fatal, define SOKOL_VALIDATE_NON_FATAL,
be aware though that this may spam SOKOL_LOG messages.
Optionally define the following to force debug checks and validations
even in release mode:
SOKOL_DEBUG - by default this is defined if _DEBUG is defined
sokol_gfx DOES NOT:
===================
- create a window or the 3D-API context/device, you must do this
before sokol_gfx is initialized, and pass any required information
(like 3D device pointers) to the sokol_gfx initialization call
- present the rendered frame, how this is done exactly usually depends
on how the window and 3D-API context/device was created
- provide a unified shader language, instead 3D-API-specific shader
source-code or shader-bytecode must be provided
For complete code examples using the various backend 3D-APIs, see:
https://github.com/floooh/sokol-samples
STEP BY STEP
============
--- to initialize sokol_gfx, after creating a window and a 3D-API
context/device, call:
sg_setup(const sg_desc*)
--- create resource objects (at least buffers, shaders and pipelines,
and optionally images and passes):
sg_buffer sg_make_buffer(const sg_buffer_desc*)
sg_image sg_make_image(const sg_image_desc*)
sg_shader sg_make_shader(const sg_shader_desc*)
sg_pipeline sg_make_pipeline(const sg_pipeline_desc*)
sg_pass sg_make_pass(const sg_pass_desc*)
--- start rendering to the default frame buffer with:
sg_begin_default_pass(const sg_pass_action* actions, int width, int height)
--- or start rendering to an offscreen framebuffer with:
sg_begin_pass(sg_pass pass, const sg_pass_action* actions)
--- fill an sg_draw_state struct with the resource bindings for the next
draw call (one pipeline object, 1..N vertex buffers, 0 or 1
index buffer, 0..N image objects to use as textures each on
the vertex-shader- and fragment-shader-stage and then call
sg_apply_draw_state(const sg_draw_state* draw_state)
to update the resource bindings
--- optionally update shader uniform data with:
sg_apply_uniform_block(sg_shader_stage stage, int ub_index, const void* data, int num_bytes)
--- kick off a draw call with:
sg_draw(int base_element, int num_elements, int num_instances)
--- finish the current rendering pass with:
sg_end_pass()
--- when done with the current frame, call
sg_commit()
--- at the end of your program, shutdown sokol_gfx with:
sg_shutdown()
--- if you need to destroy resources before sg_shutdown(), call:
sg_destroy_buffer(sg_buffer buf)
sg_destroy_image(sg_image img)
sg_destroy_shader(sg_shader shd)
sg_destroy_pipeline(sg_pipeline pip)
sg_destroy_pass(sg_pass pass)
--- to set a new viewport rectangle, call
sg_apply_viewport(int x, int y, int width, int height, bool origin_top_left)
--- to set a new scissor rect, call:
sg_apply_scissor_rect(int x, int y, int width, int height, bool origin_top_left)
both sg_apply_viewport() and sg_apply_scissor_rect() must be called
inside a rendering pass
beginning a pass will reset the viewport to the size of the framebuffer used
in the new pass,
--- to update the content of buffer and image resources, call:
sg_update_buffer(sg_buffer buf, const void* ptr, int num_bytes)
sg_update_image(sg_image img, const sg_image_content* content)
buffers and images to be updated must have been created with
SG_USAGE_DYNAMIC or SG_USAGE_STREAM
--- to check for support of optional features:
bool sg_query_feature(sg_feature feature)
--- if you need to call into the underlying 3D-API directly, you must call:
sg_reset_state_cache()
...before calling sokol_gfx functions again
BACKEND-SPECIFIC TOPICS:
========================
--- the GL backends need to know about the internal structure of uniform
blocks, and the texture sampler-name and -type:
typedef struct {
float mvp[16]; // model-view-projection matrix
float offset0[2]; // some 2D vectors
float offset1[2];
float offset2[2];
} params_t;
// uniform block structure and texture image definition in sg_shader_desc:
sg_shader_desc desc = {
// uniform block description (size and internal structure)
.vs.uniform_blocks[0] = {
.size = sizeof(params_t),
.uniforms = {
[0] = { .name="mvp", .type=SG_UNIFORMTYPE_MAT4 },
[1] = { .name="offset0", .type=SG_UNIFORMTYPE_VEC2 },
...
}
},
// one texture on the fragment-shader-stage, GLES2/WebGL needs name and image type
.fs.images[0] = { .name="tex", .type=SG_IMAGETYPE_ARRAY }
...
};
--- the Metal and D3D11 backends only need to know the size of uniform blocks,
not their internal member structure, and they only need to know
the type of a texture sampler, not its name:
sg_shader_desc desc = {
.vs.uniform_blocks[0].size = sizeof(params_t),
.fs.images[0].type = SG_IMAGETYPE_ARRAY,
...
};
--- when creating a pipeline object, GLES2/WebGL need to know the vertex
attribute names as used in the vertex shader when describing vertex
layouts:
sg_pipeline_desc desc = {
.layout = {
.attrs = {
[0] = { .name="position", .format=SG_VERTEXFORMAT_FLOAT3 },
[1] = { .name="color1", .format=SG_VERTEXFORMAT_FLOAT4 }
}
}
};
--- on D3D11 you need to provide a semantic name and semantic index in the
vertex attribute definition instead (see the D3D11 documentation on
D3D11_INPUT_ELEMENT_DESC for details):
sg_pipeline_desc desc = {
.layout = {
.attrs = {
[0] = { .sem_name="POSITION", .sem_index=0, .format=SG_VERTEXFORMAT_FLOAT3 },
[1] = { .sem_name="COLOR", .sem_index=1, .format=SG_VERTEXFORMAT_FLOAT4 }
}
}
};
--- on Metal, GL 3.3 or GLES3/WebGL2, you don't need to provide an attribute
name or semantic name, since vertex attributes can be bound by their slot index
(this is mandatory in Metal, and optional in GL):
sg_pipeline_desc desc = {
.layout = {
.attrs = {
[0] = { .format=SG_VERTEXFORMAT_FLOAT3 },
[1] = { .format=SG_VERTEXFORMAT_FLOAT4 }
}
}
};
WORKING WITH CONTEXTS
=====================
sokol-gfx allows to switch between different rendering contexts and
associate resource objects with contexts. This is useful to
create GL applications that render into multiple windows.
A rendering context keeps track of all resources created while
the context is active. When the context is destroyed, all resources
"belonging to the context" are destroyed as well.
A default context will be created and activated implicitely in
sg_setup(), and destroyed in sg_shutdown(). So for a typical application
which *doesn't* use multiple contexts, nothing changes, and calling
the context functions isn't necessary.
Three functions have been added to work with contexts:
--- sg_context sg_setup_context():
This must be called once after a GL context has been created and
made active.
--- void sg_activate_context(sg_context ctx)
This must be called after making a different GL context active.
Apart from 3D-API-specific actions, the call to sg_activate_context()
will internally call sg_reset_state_cache().
--- void sg_discard_context(sg_context ctx)
This must be called right before a GL context is destroyed and
will destroy all resources associated with the context (that
have been created while the context was active) The GL context must be
active at the time sg_discard_context(sg_context ctx) is called.
Also note that resources (buffers, images, shaders and pipelines) must
only be used or destroyed while the same GL context is active that
was also active while the resource was created (an exception is
resource sharing on GL, such resources can be used while
another context is active, but must still be destroyed under
the same context that was active during creation).
For more information, check out the the multiwindow-glfw sample:
https://github.com/floooh/sokol-samples/blob/master/glfw/multiwindow-glfw.c
TODO:
====
- talk about asynchronous resource creation
zlib/libpng license
Copyright (c) 2018 Andre Weissflog
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 <stdint.h>
#include <stdbool.h>
#ifdef __cplusplus
extern "C" {
#endif
/*
Resource id typedefs:
sg_buffer: vertex- and index-buffers
sg_image: textures and render targets
sg_shader: vertex- and fragment-shaders, uniform blocks
sg_pipeline: associated shader and vertex-layouts, and render states
sg_pass: a bundle of render targets and actions on them
sg_context: a 'context handle' for switching between 3D-API contexts
Instead of pointers, resource creation functions return a 32-bit
number which uniquely identifies the resource object.
The 32-bit resource id is split into a 16-bit pool index in the lower bits,
and a 16-bit 'unique counter' in the upper bits. The index allows fast
pool lookups, and combined with the unique-mask it allows to detect
'dangling accesses' (trying to use an object which no longer exists, and
its pool slot has been reused for a new object)
The resource ids are wrapped into a struct so that the compiler
can complain when the wrong resource type is used.
*/
typedef struct { uint32_t id; } sg_buffer;
typedef struct { uint32_t id; } sg_image;
typedef struct { uint32_t id; } sg_shader;
typedef struct { uint32_t id; } sg_pipeline;
typedef struct { uint32_t id; } sg_pass;
typedef struct { uint32_t id; } sg_context;
/*
various compile-time constants
FIXME: it may make sense to convert some of those into defines so
that the user code can override them.
*/
enum {
SG_INVALID_ID = 0,
SG_NUM_SHADER_STAGES = 2,
SG_NUM_INFLIGHT_FRAMES = 2,
SG_MAX_COLOR_ATTACHMENTS = 4,
SG_MAX_SHADERSTAGE_BUFFERS = 4,
SG_MAX_SHADERSTAGE_IMAGES = 12,
SG_MAX_SHADERSTAGE_UBS = 4,
SG_MAX_UB_MEMBERS = 16,
SG_MAX_VERTEX_ATTRIBUTES = 16,
SG_MAX_MIPMAPS = 16,
SG_MAX_TEXTUREARRAY_LAYERS = 128
};
/*
sg_feature
These are optional features, use the function
sg_query_feature() to check whether the feature is supported.
*/
typedef enum {
SG_FEATURE_INSTANCING,
SG_FEATURE_TEXTURE_COMPRESSION_DXT,
SG_FEATURE_TEXTURE_COMPRESSION_PVRTC,
SG_FEATURE_TEXTURE_COMPRESSION_ATC,
SG_FEATURE_TEXTURE_COMPRESSION_ETC2,
SG_FEATURE_TEXTURE_FLOAT,
SG_FEATURE_TEXTURE_HALF_FLOAT,
SG_FEATURE_ORIGIN_BOTTOM_LEFT,
SG_FEATURE_ORIGIN_TOP_LEFT,
SG_FEATURE_MSAA_RENDER_TARGETS,
SG_FEATURE_PACKED_VERTEX_FORMAT_10_2,
SG_FEATURE_MULTIPLE_RENDER_TARGET,
SG_FEATURE_IMAGETYPE_3D,
SG_FEATURE_IMAGETYPE_ARRAY,
SG_NUM_FEATURES
} sg_feature;
/*
sg_resource_state
The current state of a resource in its resource pool.
Resources start in the INITIAL state, which means the
pool slot is unoccupied and can be allocated. When a resource is
created, first an id is allocated, and the resource pool slot
is set to state ALLOC. After allocation, the resource is
initialized, which may result in the VALID or FAILED state. The
reason why allocation and initialization are separate is because
some resource types (e.g. buffers and images) might be asynchronously
initialized by the user application. If a resource which is not
in the VALID state is attempted to be used for rendering, rendering
operations will silently be dropped.
The special INVALID state is returned in sg_query_xxx_state() if no
resource object exists for the provided resource id.
*/
typedef enum {
SG_RESOURCESTATE_INITIAL,
SG_RESOURCESTATE_ALLOC,
SG_RESOURCESTATE_VALID,
SG_RESOURCESTATE_FAILED,
SG_RESOURCESTATE_INVALID,
_SG_RESOURCESTATE_FORCE_U32 = 0x7FFFFFFF
} sg_resource_state;
/*
sg_usage
A resource usage hint describing the update strategy of
buffers and images. This is used in the sg_buffer_desc.usage
and sg_image_desc.usage members when creating buffers
and images:
SG_USAGE_IMMUTABLE: the resource will never be updated with
new data, instead the data content of the
resource must be provided on creation
SG_USAGE_DYNAMIC: the resource will be updated infrequently
with new data (this could range from "once
after creation", to "quite often but not
every frame")
SG_USAGE_STREAM: the resource will be updated each frame
with new content
The rendering backends use this hint to prevent that the
CPU needs to wait for the GPU when attempting to update
a resource that might be currently accessed by the GPU.
Resource content is updated with the function sg_update_buffer() for
buffer objects, and sg_update_image() for image objects. Only
one update is allowed per frame and resource object. The
application must update all data required for rendering (this
means that the update data can be smaller than the resource size,
if only a part of the overall resource size is used for rendering,
you only need to make sure that the data that *is* used is valid.
The default usage is SG_USAGE_IMMUTABLE.
*/
typedef enum {
_SG_USAGE_DEFAULT, /* value 0 reserved for default-init */
SG_USAGE_IMMUTABLE,
SG_USAGE_DYNAMIC,
SG_USAGE_STREAM,
_SG_USAGE_NUM,
_SG_USAGE_FORCE_U32 = 0x7FFFFFFF
} sg_usage;
/*
sg_buffer_type
This indicates whether a buffer contains vertex- or index-data,
used in the sg_buffer_desc.type member when creating a buffer.
The default value is SG_BUFFERTYPE_VERTEXBUFFER.
*/
typedef enum {
_SG_BUFFERTYPE_DEFAULT, /* value 0 reserved for default-init */
SG_BUFFERTYPE_VERTEXBUFFER,
SG_BUFFERTYPE_INDEXBUFFER,
_SG_BUFFERTYPE_NUM,
_SG_BUFFERTYPE_FORCE_U32 = 0x7FFFFFFF
} sg_buffer_type;
/*
sg_index_type
Indicates whether indexed rendering (fetching vertex-indices from an
index buffer) is used, and if yes, the index data type (16- or 32-bits).
This is used in the sg_pipeline_desc.index_type member when creating a
pipeline object.
The default index type is SG_INDEXTYPE_NONE.
*/
typedef enum {
_SG_INDEXTYPE_DEFAULT, /* value 0 reserved for default-init */
SG_INDEXTYPE_NONE,
SG_INDEXTYPE_UINT16,
SG_INDEXTYPE_UINT32,
_SG_INDEXTYPE_NUM,
_SG_INDEXTYPE_FORCE_U32 = 0x7FFFFFFF
} sg_index_type;
/*
sg_image_type
Indicates the basic image type (2D-texture, cubemap, 3D-texture
or 2D-array-texture). 3D- and array-textures are not supported
on the GLES2/WebGL backend. The image type is used in the
sg_image_desc.type member when creating an image.
The default image type when creating an image is SG_IMAGETYPE_2D.
*/
typedef enum {
_SG_IMAGETYPE_DEFAULT, /* value 0 reserved for default-init */
SG_IMAGETYPE_2D,
SG_IMAGETYPE_CUBE,
SG_IMAGETYPE_3D,
SG_IMAGETYPE_ARRAY,
_SG_IMAGETYPE_NUM,
_SG_IMAGETYPE_FORCE_U32 = 0x7FFFFFFF
} sg_image_type;
/*
sg_cube_face
The cubemap faces. Use these as indices in the sg_image_desc.content
array.
*/
typedef enum {
SG_CUBEFACE_POS_X,
SG_CUBEFACE_NEG_X,
SG_CUBEFACE_POS_Y,
SG_CUBEFACE_NEG_Y,
SG_CUBEFACE_POS_Z,
SG_CUBEFACE_NEG_Z,
SG_CUBEFACE_NUM,
_SG_CUBEFACE_FORCE_U32 = 0x7FFFFFFF
} sg_cube_face;
/*
sg_shader_stage
There are 2 shader stages: vertex- and fragment-shader-stage.
Each shader stage consists of:
- one slot for a shader function (provided as source- or byte-code)
- SG_MAX_SHADERSTAGE_UBS slots for uniform blocks
- SG_MAX_SHADERSTAGE_IMAGES slots for images used as textures by
the shader function
*/
typedef enum {
SG_SHADERSTAGE_VS,
SG_SHADERSTAGE_FS,
_SG_SHADERSTAGE_FORCE_U32 = 0x7FFFFFFF
} sg_shader_stage;
/*
sg_pixel_format
This is a common subset of useful and widely supported pixel formats. The
pixel format enum is mainly used when creating an image object in the
sg_image_desc.pixel_format member.
The default pixel format when creating an image is SG_PIXELFORMAT_RGBA8.
*/
typedef enum {
_SG_PIXELFORMAT_DEFAULT, /* value 0 reserved for default-init */
SG_PIXELFORMAT_NONE,
SG_PIXELFORMAT_RGBA8,
SG_PIXELFORMAT_RGB8,
SG_PIXELFORMAT_RGBA4,
SG_PIXELFORMAT_R5G6B5,
SG_PIXELFORMAT_R5G5B5A1,
SG_PIXELFORMAT_R10G10B10A2,
SG_PIXELFORMAT_RGBA32F,
SG_PIXELFORMAT_RGBA16F,
SG_PIXELFORMAT_R32F,
SG_PIXELFORMAT_R16F,
SG_PIXELFORMAT_L8,
SG_PIXELFORMAT_DXT1,
SG_PIXELFORMAT_DXT3,
SG_PIXELFORMAT_DXT5,
SG_PIXELFORMAT_DEPTH,
SG_PIXELFORMAT_DEPTHSTENCIL,
SG_PIXELFORMAT_PVRTC2_RGB,
SG_PIXELFORMAT_PVRTC4_RGB,
SG_PIXELFORMAT_PVRTC2_RGBA,
SG_PIXELFORMAT_PVRTC4_RGBA,
SG_PIXELFORMAT_ETC2_RGB8,
SG_PIXELFORMAT_ETC2_SRGB8,
_SG_PIXELFORMAT_NUM,
_SG_PIXELFORMAT_FORCE_U32 = 0x7FFFFFFF
} sg_pixel_format;
/*
sg_primitive_type
This is the common subset of 3D primitive types supported across all 3D
APIs. This is used in the sg_pipeline_desc.primitive_type member when
creating a pipeline object.
The default primitive type is SG_PRIMITIVETYPE_TRIANGLES.
*/
typedef enum {
_SG_PRIMITIVETYPE_DEFAULT, /* value 0 reserved for default-init */
SG_PRIMITIVETYPE_POINTS,
SG_PRIMITIVETYPE_LINES,
SG_PRIMITIVETYPE_LINE_STRIP,
SG_PRIMITIVETYPE_TRIANGLES,
SG_PRIMITIVETYPE_TRIANGLE_STRIP,
_SG_PRIMITIVETYPE_NUM,
_SG_PRIMITIVETYPE_FORCE_U32 = 0x7FFFFFFF
} sg_primitive_type;
/*
sg_filter
The filtering mode when sampling a texture image. This is
used in the sg_image_desc.min_filter and sg_image_desc.mag_filter
members when creating an image object.
The default filter mode is SG_FILTER_NEAREST.
*/
typedef enum {
_SG_FILTER_DEFAULT, /* value 0 reserved for default-init */
SG_FILTER_NEAREST,
SG_FILTER_LINEAR,
SG_FILTER_NEAREST_MIPMAP_NEAREST,
SG_FILTER_NEAREST_MIPMAP_LINEAR,
SG_FILTER_LINEAR_MIPMAP_NEAREST,
SG_FILTER_LINEAR_MIPMAP_LINEAR,
_SG_FILTER_NUM,
_SG_FILTER_FORCE_U32 = 0x7FFFFFFF
} sg_filter;
/*
sg_wrap
The texture coordinates wrapping mode when sampling a texture
image. This is used in the sg_image_desc.wrap_u, .wrap_v
and .wrap_w members when creating an image.
The default wrap mode is SG_WRAP_REPEAT.
*/
typedef enum {
_SG_WRAP_DEFAULT, /* value 0 reserved for default-init */
SG_WRAP_REPEAT,
SG_WRAP_CLAMP_TO_EDGE,
SG_WRAP_MIRRORED_REPEAT,
_SG_WRAP_NUM,
_SG_WRAP_FORCE_U32 = 0x7FFFFFFF
} sg_wrap;
/*
sg_vertex_format
The data type of a vertex component. This is used to describe
the layout of vertex data when creating a pipeline object.
*/
typedef enum {
SG_VERTEXFORMAT_INVALID,
SG_VERTEXFORMAT_FLOAT,
SG_VERTEXFORMAT_FLOAT2,
SG_VERTEXFORMAT_FLOAT3,
SG_VERTEXFORMAT_FLOAT4,
SG_VERTEXFORMAT_BYTE4,
SG_VERTEXFORMAT_BYTE4N,
SG_VERTEXFORMAT_UBYTE4,
SG_VERTEXFORMAT_UBYTE4N,
SG_VERTEXFORMAT_SHORT2,
SG_VERTEXFORMAT_SHORT2N,
SG_VERTEXFORMAT_SHORT4,
SG_VERTEXFORMAT_SHORT4N,
SG_VERTEXFORMAT_UINT10_N2,
_SG_VERTEXFORMAT_NUM,
_SG_VERTEXFORMAT_FORCE_U32 = 0x7FFFFFFF
} sg_vertex_format;
/*
sg_vertex_step
Defines whether the input pointer of a vertex input stream is advanced
'per vertex' or 'per instance'. The default step-func is
SG_VERTEXSTEP_PER_VERTEX. SG_VERTEXSTEP_PER_INSTANCE is used with
instanced-rendering.
The vertex-step is part of the vertex-layout definition
when creating pipeline objects.
*/
typedef enum {
_SG_VERTEXSTEP_DEFAULT, /* value 0 reserved for default-init */
SG_VERTEXSTEP_PER_VERTEX,
SG_VERTEXSTEP_PER_INSTANCE,
_SG_VERTEXSTEP_NUM,
_SG_VERTEXSTEP_FORCE_U32 = 0x7FFFFFFF
} sg_vertex_step;
/*
sg_uniform_type
The data type of a uniform block member. This is used to
describe the internal layout of uniform blocks when creating
a shader object.
*/
typedef enum {
SG_UNIFORMTYPE_INVALID,
SG_UNIFORMTYPE_FLOAT,
SG_UNIFORMTYPE_FLOAT2,
SG_UNIFORMTYPE_FLOAT3,
SG_UNIFORMTYPE_FLOAT4,
SG_UNIFORMTYPE_MAT4,
_SG_UNIFORMTYPE_NUM,
_SG_UNIFORMTYPE_FORCE_U32 = 0x7FFFFFFF
} sg_uniform_type;
/*
sg_cull_mode
The face-culling mode, this is used in the
sg_pipeline_desc.rasterizer.cull_mode member when creating a
pipeline object.
The default cull mode is SG_CULLMODE_NONE
*/
typedef enum {
_SG_CULLMODE_DEFAULT, /* value 0 reserved for default-init */
SG_CULLMODE_NONE,
SG_CULLMODE_FRONT,
SG_CULLMODE_BACK,
_SG_CULLMODE_NUM,
_SG_CULLMODE_FORCE_U32 = 0x7FFFFFFF
} sg_cull_mode;
/*
sg_face_winding
The vertex-winding rule that determines a front-facing primitive. This
is used in the member sg_pipeline_desc.rasterizer.face_winding
when creating a pipeline object.
The default winding is SG_FACEWINDING_CW (clockwise)
*/
typedef enum {
_SG_FACEWINDING_DEFAULT, /* value 0 reserved for default-init */
SG_FACEWINDING_CCW,
SG_FACEWINDING_CW,
_SG_FACEWINDING_NUM,
_SG_FACEWINDING_FORCE_U32 = 0x7FFFFFFF
} sg_face_winding;
/*
sg_compare_func
The compare-function for depth- and stencil-ref tests.
This is used when creating pipeline objects in the members:
sg_pipeline_desc
.depth_stencil
.depth_compare_func
.stencil_front.compare_func
.stencil_back.compare_func
The default compare func for depth- and stencil-tests is
SG_COMPAREFUNC_ALWAYS.
*/
typedef enum {
_SG_COMPAREFUNC_DEFAULT, /* value 0 reserved for default-init */
SG_COMPAREFUNC_NEVER,
SG_COMPAREFUNC_LESS,
SG_COMPAREFUNC_EQUAL,
SG_COMPAREFUNC_LESS_EQUAL,
SG_COMPAREFUNC_GREATER,
SG_COMPAREFUNC_NOT_EQUAL,
SG_COMPAREFUNC_GREATER_EQUAL,
SG_COMPAREFUNC_ALWAYS,
_SG_COMPAREFUNC_NUM,
_SG_COMPAREFUNC_FORCE_U32 = 0x7FFFFFFF
} sg_compare_func;
/*
sg_stencil_op
The operation performed on a currently stored stencil-value when a
comparison test passes or fails. This is used when creating a pipeline
object in the members:
sg_pipeline_desc
.depth_stencil
.stencil_front
.fail_op
.depth_fail_op
.pass_op
.stencil_back
.fail_op
.depth_fail_op
.pass_op
The default value is SG_STENCILOP_KEEP.
*/
typedef enum {
_SG_STENCILOP_DEFAULT, /* value 0 reserved for default-init */
SG_STENCILOP_KEEP,
SG_STENCILOP_ZERO,
SG_STENCILOP_REPLACE,
SG_STENCILOP_INCR_CLAMP,
SG_STENCILOP_DECR_CLAMP,
SG_STENCILOP_INVERT,
SG_STENCILOP_INCR_WRAP,
SG_STENCILOP_DECR_WRAP,
_SG_STENCILOP_NUM,
_SG_STENCILOP_FORCE_U32 = 0x7FFFFFFF
} sg_stencil_op;
/*
sg_blend_factor
The source and destination factors in blending operations.
This is used in the following members when creating a pipeline object:
sg_pipeline_desc
.blend
.src_factor_rgb
.dst_factor_rgb
.src_factor_alpha
.dst_factor_alpha
The default value is SG_BLENDFACTOR_ONE for source
factors, and SG_BLENDFACTOR_ZERO for destination factors.
*/
typedef enum {
_SG_BLENDFACTOR_DEFAULT, /* value 0 reserved for default-init */
SG_BLENDFACTOR_ZERO,
SG_BLENDFACTOR_ONE,
SG_BLENDFACTOR_SRC_COLOR,
SG_BLENDFACTOR_ONE_MINUS_SRC_COLOR,
SG_BLENDFACTOR_SRC_ALPHA,
SG_BLENDFACTOR_ONE_MINUS_SRC_ALPHA,
SG_BLENDFACTOR_DST_COLOR,
SG_BLENDFACTOR_ONE_MINUS_DST_COLOR,
SG_BLENDFACTOR_DST_ALPHA,
SG_BLENDFACTOR_ONE_MINUS_DST_ALPHA,
SG_BLENDFACTOR_SRC_ALPHA_SATURATED,
SG_BLENDFACTOR_BLEND_COLOR,
SG_BLENDFACTOR_ONE_MINUS_BLEND_COLOR,
SG_BLENDFACTOR_BLEND_ALPHA,
SG_BLENDFACTOR_ONE_MINUS_BLEND_ALPHA,
_SG_BLENDFACTOR_NUM,
_SG_BLENDFACTOR_FORCE_U32 = 0x7FFFFFFF
} sg_blend_factor;
/*
sg_blend_op
Describes how the source and destination values are combined in the
fragment blending operation. It is used in the following members when
creating a pipeline object:
sg_pipeline_desc
.blend
.op_rgb
.op_alpha
The default value is SG_BLENDOP_ADD.
*/
typedef enum {
_SG_BLENDOP_DEFAULT, /* value 0 reserved for default-init */
SG_BLENDOP_ADD,
SG_BLENDOP_SUBTRACT,
SG_BLENDOP_REVERSE_SUBTRACT,
_SG_BLENDOP_NUM,
_SG_BLENDOP_FORCE_U32 = 0x7FFFFFFF
} sg_blend_op;
/*
sg_color_mask
Selects the color channels when writing a fragment color to the
framebuffer. This is used in the members
sg_pipeline_desc.blend.color_write_mask when creating a pipeline object.
The default colormask is SG_COLORMASK_RGBA (write all colors channels)
*/
typedef enum {
_SG_COLORMASK_DEFAULT = 0, /* value 0 reserved for default-init */
SG_COLORMASK_NONE = (0x10), /* special value for 'all channels disabled */
SG_COLORMASK_R = (1<<0),
SG_COLORMASK_G = (1<<1),
SG_COLORMASK_B = (1<<2),
SG_COLORMASK_A = (1<<3),
SG_COLORMASK_RGB = 0x7,
SG_COLORMASK_RGBA = 0xF,
_SG_COLORMASK_FORCE_U32 = 0x7FFFFFFF
} sg_color_mask;
/*
sg_action
Defines what action should be performed at the start of a render pass:
SG_ACTION_CLEAR: clear the render target image
SG_ACTION_LOAD: load the previous content of the render target image
SG_ACTION_DONTCARE: leave the render target image content undefined
This is used in the sg_pass_action structure.
The default action for all pass attachments is SG_ACTION_CLEAR, with the
clear color rgba = {0.5f, 0.5f, 0.5f, 1.0f], depth=1.0 and stencil=0.
If you want to override the default behaviour, it is important to not
only set the clear color, but the 'action' field as well (as long as this
is in its _SG_ACTION_DEFAULT, the value fields will be ignored).
*/
typedef enum {
_SG_ACTION_DEFAULT,
SG_ACTION_CLEAR,
SG_ACTION_LOAD,
SG_ACTION_DONTCARE,
_SG_ACTION_NUM,
_SG_ACTION_FORCE_U32 = 0x7FFFFFFF
} sg_action;
/*
sg_pass_action
The sg_pass_action struct defines the actions to be performed
at the start of a rendering pass in the functions sg_begin_pass()
and sg_begin_default_pass().
A separate action and clear values can be defined for each
color attachment, and for the depth-stencil attachment.
The default clear values are defined by the macros:
- SG_DEFAULT_CLEAR_RED: 0.5f
- SG_DEFAULT_CLEAR_GREEN: 0.5f
- SG_DEFAULT_CLEAR_BLUE: 0.5f
- SG_DEFAULT_CLEAR_ALPHA: 1.0f
- SG_DEFAULT_CLEAR_DEPTH: 1.0f
- SG_DEFAULT_CLEAR_STENCIL: 0
*/
typedef struct {
sg_action action;
float val[4];
} sg_color_attachment_action;
typedef struct {
sg_action action;
float val;
} sg_depth_attachment_action;
typedef struct {
sg_action action;
uint8_t val;
} sg_stencil_attachment_action;
typedef struct {
uint32_t _start_canary;
sg_color_attachment_action colors[SG_MAX_COLOR_ATTACHMENTS];
sg_depth_attachment_action depth;
sg_stencil_attachment_action stencil;
uint32_t _end_canary;
} sg_pass_action;
/*
sg_draw_state
The sg_draw_state structure defines the resource binding slots
of the sokol_gfx render pipeline, used as argument to the
sg_apply_draw_state() function.
A draw state contains:
- 1 pipeline object
- 1..N vertex buffers
- 0..N vertex buffer offsets
- 0..1 index buffers
- 0..1 index buffer offsets
- 0..N vertex shader stage images
- 0..N fragment shader stage images
The max number of vertex buffer and shader stage images
are defined by the SG_MAX_SHADERSTAGE_BUFFERS and
SG_MAX_SHADERSTAGE_IMAGES configuration constants.
The optional buffer offsets can be used to group different chunks
of vertex- and/or index-data into the same buffer objects.
*/
typedef struct {
uint32_t _start_canary;
sg_pipeline pipeline;
sg_buffer vertex_buffers[SG_MAX_SHADERSTAGE_BUFFERS];
uint32_t vertex_buffer_offsets[SG_MAX_SHADERSTAGE_BUFFERS];
sg_buffer index_buffer;
uint32_t index_buffer_offset;
sg_image vs_images[SG_MAX_SHADERSTAGE_IMAGES];
sg_image fs_images[SG_MAX_SHADERSTAGE_IMAGES];
uint32_t _end_canary;
} sg_draw_state;
/*
sg_desc
The sg_desc struct contains configuration values for sokol_gfx,
it is used as parameter to the sg_setup() call.
The default configuration is: