-
Notifications
You must be signed in to change notification settings - Fork 23
/
pico_gfx.h
2466 lines (1986 loc) · 68 KB
/
pico_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
/**
@file pico_gfx.h
@brief A powerful graphics library based on Sokol GFX, written in C99.
----------------------------------------------------------------------------
Licensing information at end of header
----------------------------------------------------------------------------
Features:
---------
- Written in C99
- Two header library for easy build system integration
- Easy to use low-level constructs (buffers, render passes, pipelines, and samplers)
- Flexible pipeline configuration
- Simple state management system (state stack)
- Render to texture
- Custom shaders via the sokol shader compiler
- Simple and concise API
- Permissive license (zlib or public domain)
Summary:
--------
pico_gfx is a thin wrapper for [sokol_gfx](https://github.com/floooh/sokol/blob/master/sokol_gfx.h),
a low-level graphics library that supports OpenGL, Metal, D3D, and WebGPU.
pico_gfx is designed make the common case intuitive and convenient. It
provides access to low-level constructs, such as buffers, render passes and
pipelines, in a way that is easy to use and understand.
pico_gfx comes with three examples; basic quad rendering (to a render
texture and the screen), a scene graph demo, and a particle system demo.
These are the best source of information regadring how to use the API.
In constrast with earlier versions, pico_gfx no longer includes a default
shader or pipeline. This was a hard decision to make, but the header is less
cluttered, and more generic. It is also hard to define what default really
means. Pipeline layouts must now be specified explicitly. The examples
demonstrate several ways of doing this.
Shaders must be compiled with the sokol compiler (`sokol-shdc`). Binary
versions of which can be found [here](https://github.com/floooh/sokol-tools-bin).
The source code for the compiler can be found [here](https://github.com/floooh/sokol-tools).
An example of how to use the compiler can be found in the
`build_pico_gfx_shader` directory. There are also two compiled shaders
included with the examples that are more or less generic for rendering
sprites and particles.
One thing pico_gfx does not support (and neither does sokol_gfx) is window
and graphics context creation. See [here](https://github.com/RandyGaul/cute_framework/tree/master/src/internal)
for some examples. It is worth mentioning that [SDL2](https://www.libsdl.org)
can supply both a window and OpenGL context out of the box. SDL2 is used
in the demos.
Another library that supports window/context creation on all supported
backends is [sokol_app](https://github.com/floooh/sokol/blob/master/sokol_app.h),
but it has yet to be tested with pico_gfx.
The state that pico_gfx manages includes:
- Pipelines/shaders
- Uniform blocks
- Vertex buffers
- Index buffers
- Clear color
- Viewport
- Scissor
- Textures
- Samplers
Most changes to the state can be isolated by using the state stack
(`pg_push_state/pg_pop_state`). Simply push the current state onto the
stack, make some local changes, and then pop the stack to restore the
original state. The exceptions are textures and samplers that are shader
state and not global state.
Shaders expose uniforms in blocks. These blocks must be registered with the
shader by calling `pg_alloc_uniform_block`. They may then be set at will
by calling `pg_set_uniform_block`. These functions operate on structs
supplied by a compiled shader,
Please see the examples for more details.
C++
--------
In this iteration, pico_gfx is a C only library. Proper C++ compatibility
may be introduced in the future.
Build:
--------
To use this library in your project, add
> #define PICO_GFX_IMPLEMENTATION
> #include "pico_gfx.h"
to a source file.
IMPORTANT: sokol_gfx.h must be in the include path!
You must also define one of
#define PICO_GFX_GL
#define PICO_GFX_GLES
#define PICO_GFX_D3D
#define PICO_GFX_METAL
#define PICO_GFX_WEBGPU
before including pico_gfx.h
See the examples for build details.
Constants:
--------
- PICO_GFX_HASHTABLE_KEY_SIZE (default: 16)
- PICO_GFX_STACK_MAX_SIZE (default: 16)
Customization:
--------
- NDEBUG
- PICO_GFX_ASSERT
- PICO_GFX_LOG
- PICO_GFX_MALLOC
- PICO_GFX_REALLOC
- PICO_GFX_FREE
The above (constants/macros) must be defined before PICO_GFX_IMPLEMENTATION
*/
#ifndef PICO_GFX_H
#define PICO_GFX_H
// Backend conversion macros
#if defined (PICO_GFX_GL)
#define SOKOL_GLCORE
#elif defined (PICO_GFX_GLES)
#define SOKOL_GLES3
#elif defined (PICO_GFX_D3D)
#define SOKOL_D3D11
#elif defined (PICO_GFX_METAL)
#define SOKOL_METAL
#elif defined (PICO_GFX_WEBGPU)
#define SOKOL_WGPU
#else
#error "GFX backend must be specified"
#endif
#include "sokol_gfx.h"
#include <stdbool.h>
#include <stddef.h>
#define PG_MAX_VERTEX_ATTRIBUTES SG_MAX_VERTEX_ATTRIBUTES
#define PG_MAX_VERTEX_BUFFERS SG_MAX_VERTEX_BUFFERS
#define PG_MAX_TEXTURE_SLOTS SG_MAX_SHADERSTAGE_IMAGES
#define PG_MAX_SAMPLER_SLOTS SG_MAX_SHADERSTAGE_SAMPLERS
/**
* @brief Graphics backends
*/
typedef enum
{
PG_BACKEND_GL,
PG_BACKEND_GLES,
PG_BACKEND_D3D,
PG_BACKEND_METAL,
PG_BACKEND_WGPU
} pg_backend_t;
/**
* @brief Drawing primitives
*/
typedef enum
{
PG_DEFAULT_PRIMITIVE,
PG_POINTS, //!< Array of points
PG_LINES, //!< Each adjacent pair of points forms a line
PG_LINE_STRIP, //!< Array of points where every pair forms a lines
PG_TRIANGLES, //!< Each adjacent triple forms an individual triangle
PG_TRIANGLE_STRIP, //!< Array of points where every triple forms a triangle
} pg_primitive_t;
/**
* @brief Blend factors
*/
typedef enum
{
PG_DEFAULT_BLEND_FACTOR,
PG_ZERO, //!< (0, 0, 0, 0)
PG_ONE, //!< (1, 1, 1, 1)
PG_SRC_COLOR, //!< (src.r, src.g, src.b, src.a)
PG_ONE_MINUS_SRC_COLOR, //!< (1, 1, 1, 1) - (src.r, src.g, src.b, src.a)
PG_DST_COLOR, //!< (dst.r, dst.g, dst.b, dst.a)
PG_ONE_MINUS_DST_COLOR, //!< (1, 1, 1, 1) - (dst.r, dst.g, dst.b, dst.a)
PG_SRC_ALPHA, //!< (src.a, src.a, src.a, src.a)
PG_ONE_MINUS_SRC_ALPHA, //!< (1, 1, 1, 1) - (src.a, src.a, src.a, src.a)
PG_DST_ALPHA, //!< (dst.a, dst.a, dst.a, dst.a)
PG_ONE_MINUS_DST_ALPHA, //!< (1, 1, 1, 1) - (dst.a, dst.a, dst.a, dst.a)
} pg_blend_factor_t;
/**
* @brief Blend equations
*/
typedef enum
{
PG_DEFAULT_BLEND_EQ,
PG_ADD, //!< result = src * src_factor + dst * dst_factor
PG_SUBTRACT, //!< result = src * src_factor - dst * dst_factor
PG_REVERSE_SUBTRACT, //!< result = dst * dst_factor - src * src_factor
} pg_blend_eq_t;
/**
* @brief Blend mode
*
* Completely describes a blend operation.
*/
typedef struct
{
pg_blend_factor_t color_src; //!< Color source blending factor
pg_blend_factor_t color_dst; //!< Color dsestination blending factor
pg_blend_eq_t color_eq; //!< Equation for blending colors
pg_blend_factor_t alpha_src; //!< Alpha source blending factor
pg_blend_factor_t alpha_dst; //!< Alpha destination blending factor
pg_blend_eq_t alpha_eq; //!< Equation for blending alpha values
} pg_blend_mode_t;
/**
* @brief Shader stage
*/
typedef enum
{
PG_STAGE_VS, //!< Vertex shader stage
PG_STAGE_FS //!< Fragment shader stage
} pg_stage_t;
/**
* @brief Contains core data/state for an instance of the graphics library
*/
typedef struct pg_ctx_t pg_ctx_t;
/**
* @brief Render state information
*/
typedef struct pg_pipeline_t pg_pipeline_t;
/**
* @brief Vertex/fragment shader program
*/
typedef struct pg_shader_t pg_shader_t;
/**
* @brief Represents an image or render target in VRAM
*/
typedef struct pg_texture_t pg_texture_t;
/**
* @brief Represents sampler
*/
typedef struct pg_sampler_t pg_sampler_t;
/**
* @brief A vertex or index array buffer
*/
typedef struct pg_buffer_t pg_buffer_t;
/**
* @brief Loads pico_gfx and sokol_gfx
*
* IMPORTANT: A valid graphics API context (OpenGL, Metal, D3D) must exist for
* this function to succeed. This function must be called before any other
* pico_gfx functions.
*
* NOTE: This function calls `sg_setup`.
*/
void pg_init(void);
/**
* @brief Tears down pico_gfx and sokol_gfx
*
* NOTE: This function calls `sg_shutdown`
*/
void pg_shutdown(void);
/**
* @brief Creates a graphics context
* @param window_width The window width
* @param window_height The window height
*/
pg_ctx_t* pg_create_context(int window_width, int window_height, void* mem_ctx);
/**
* @brief Destroys a graphics context
*/
void pg_destroy_context(pg_ctx_t* ctx);
/**
* @brief Returns the backend in use at runtime
*/
pg_backend_t pg_backend(void);
/**
* @brief Sets the window dimensions
* @param ctx The graphics context
* @param width The window width
* @param height The window height
* @param reset Resets the viewport and scissor if true
*/
void pg_set_window_size(pg_ctx_t* ctx, int width, int height, bool reset);
/**
* @brief Gets the window size
*/
void pg_get_window_size(pg_ctx_t* ctx, int* width, int* height);
/**
* @brief Starts a render pass (mandatory)
*
* NOTE: The default pass should be the last pass of a frame.
*
* @param ctx The graphics context
* @param pass The render pass (NULL for the default pass)
* @param clear Clears the render target or window
*/
void pg_begin_pass(pg_ctx_t* ctx, pg_texture_t* target, bool clear);
/**
* @brief Ends a render pass (mandatory)
*/
void pg_end_pass(pg_ctx_t* ctx);
/**
* @brief Flush commands
*
* Must be called at the end of a frame (after `pg_end_pass`).
*/
void pg_flush(pg_ctx_t* ctx);
/**
* @brief Pushes the active state onto the stack.
*
* State consists of the pipeline, draw color, scissor, viewport, and default
* MVP transform, buffers, textures, and samplers
*/
void pg_push_state(pg_ctx_t* ctx);
/**
* @brief Pops a state off the stack and makes it the active state
*/
void pg_pop_state(pg_ctx_t* ctx);
/**
* @brief Sets the clear color state to be placed at the top of the state stack
*/
void pg_set_clear_color(pg_ctx_t* ctx, float r, float g, float b, float a);
/**
* Resets the clear color
*/
void pg_reset_clear_color(pg_ctx_t* ctx);
/**
* @brief Sets the viewport state to be placed at the top of the state stack
*/
void pg_set_viewport(pg_ctx_t* ctx, int x, int y, int w, int h);
/**
* Resets the viewport
*/
void pg_reset_viewport(pg_ctx_t* ctx);
/**
* @brief Sets the scissor state to be placed at the top of the state stack
*/
void pg_set_scissor(pg_ctx_t* ctx, int x, int y, int w, int h);
/**
* Resets the scissor
*/
void pg_reset_scissor(pg_ctx_t* ctx);
/**
* @brief Sets the pipeline state
* @param ctx The graphics context
* @param pipeline The pipeline to be activated
*/
void pg_set_pipeline(pg_ctx_t* ctx, pg_pipeline_t* pipeline);
/**
* Resets the pipeline
*/
void pg_reset_pipeline(pg_ctx_t* ctx);
/**
* @brief Binds a buffer to the specified slot
*/
void pg_bind_buffer(pg_ctx_t* ctx, int slot, pg_buffer_t* buffer);
/**
* @brief Clears buffer bindings
*/
void pg_reset_buffers(pg_ctx_t* ctx);
/**
* @brief Sets the active index buffer
*
* If an index buffer is set, pg_draw will use indexing.
*/
void pg_set_index_buffer(pg_ctx_t* ctx, pg_buffer_t* buffer);
/**
* @brief Disables indexed rednering
*/
void pg_reset_index_buffer(pg_ctx_t* ctx);
/**
* @brief Binds a texture to a slot in the current state
* @param shader The shader associated with the texture
* @param slot The binding slot
* @param texture The texture to bind
*/
void pg_bind_texture(pg_shader_t* shader, const char* name, pg_texture_t* texture);
/**
* @brief Resets the texture bindings for the current state
*/
void pg_reset_textures(pg_shader_t* shader);
/**
* @brief Binds a sampler to a slot in the current state
* @param shader The shader associated with the sampler
* @param slot The binding slot
* @param sampler The sampler to bind
*/
void pg_bind_sampler(pg_shader_t* shader, const char* name, pg_sampler_t* sampler);
/**
* @brief Resets the sampler bindings for the current state
*/
void pg_reset_samplers(pg_shader_t* shader);
/**
* @brief Resets the active state to defaults
*/
void pg_reset_state(pg_ctx_t* ctx);
/**
* @brief Creates the shader with the given prefix
* The prefix should refer to the shader program name in a shader compiled by
* `sokol-shdc`. For example the sprite shader in the examples would have the
* prefix 'shader' (without quotation marks)
*/
#define pg_create_shader(ctx, prefix) \
pg_create_shader_internal( \
ctx, \
(pg_shader_internal_t) \
{ \
prefix##_shader_desc, \
prefix##_attr_slot, \
prefix##_image_slot, \
prefix##_sampler_slot, \
prefix##_uniformblock_slot, \
prefix##_uniformblock_size, \
} \
)
/**
* @brief Destroys a shader
*/
void pg_destroy_shader(pg_shader_t* shader);
/**
* @brief Returns a shader ID
*/
uint32_t pg_get_shader_id(const pg_shader_t* shader);
/**
* @brief Registers a uniform block (UB)
* @param shader The shader owning the UB
* @param stage The stage (VS or FS) associated with the UB
* @param name The name of the UB as supplied by `sokol_shdc`
*/
void pg_alloc_uniform_block(pg_shader_t* shader, pg_stage_t stage, const char* name);
/**
* @brief Sets a uniform block (UB)
* @param shader The shader owning the UB
* @param name The name of the UB as supplied by `sokol_shdc`
* @param data The data to set (must be the whole UB)
*/
void pg_set_uniform_block(pg_shader_t* shader, const char* name, const void* data);
/**
* @brief Vertex attribute pixel formats
*/
typedef enum
{
PG_VERTEX_FORMAT_INVALID,
PG_VERTEX_FORMAT_FLOAT,
PG_VERTEX_FORMAT_FLOAT2,
PG_VERTEX_FORMAT_FLOAT3,
PG_VERTEX_FORMAT_FLOAT4,
PG_VERTEX_FORMAT_BYTE4,
PG_VERTEX_FORMAT_BYTE4N,
PG_VERTEX_FORMAT_UBYTE4,
PG_VERTEX_FORMAT_UBYTE4N,
PG_VERTEX_FORMAT_SHORT2,
PG_VERTEX_FORMAT_SHORT2N,
PG_VERTEX_FORMAT_USHORT2N,
PG_VERTEX_FORMAT_SHORT4,
PG_VERTEX_FORMAT_SHORT4N,
PG_VERTEX_FORMAT_USHORT4N,
PG_VERTEX_FORMAT_UINT10_N2,
PG_VERTEX_FORMAT_HALF2,
PG_VERTEX_FORMAT_HALF4,
} pg_vertex_format_t;
/**
* @brief Vertex buffer description
*/
typedef struct
{
bool instanced; //!< True if the buffer will be used for instanced rendering
int step; //!< The step rate (default is 1)
} pg_vertex_buf_t;
/**
* @brief Vertex attribute description
*/
typedef struct
{
int buffer_index; //!< The vertex buffer bind slot
pg_vertex_format_t format; //!< Vertex pixel format (see above)
int offset; //!< Attribute offset into the vertex buffer
} pg_vertex_attr_t;
/**
* @brief Pipeline layout
*/
typedef struct
{
pg_vertex_buf_t bufs[PG_MAX_VERTEX_BUFFERS]; //!< Vertex buffer descriptions
pg_vertex_attr_t attrs[PG_MAX_VERTEX_ATTRIBUTES]; //!< Vertex buffer attribute definitions
} pg_pipeline_layout_t;
/**
* @brief Pipeline creation options
*/
typedef struct pg_pipeline_opts_t
{
pg_primitive_t primitive; //!< Rendering primitive
pg_pipeline_layout_t layout; //!< Attribute information
bool target; //!< Drawing to render target
bool indexed; //!< Indexed drawing
bool blend_enabled; //!< Enables blending
pg_blend_mode_t blend; //!< Blend mode
} pg_pipeline_opts_t;
/**
* @brief Creates a rendering pipeline (encapsulates render state)
* @param shader The shader used by this pipeline
* @param opts Pipeline creation options (required!)
* @returns A render pipeline object
*/
pg_pipeline_t* pg_create_pipeline(pg_ctx_t* ctx,
pg_shader_t* shader,
const pg_pipeline_opts_t* opts);
/**
* @brief Destroys a render pipeline
*/
void pg_destroy_pipeline(pg_pipeline_t* pipeline);
/**
* @brief Returns the shader associated with the pipeline
*/
pg_shader_t* pg_get_pipeline_shader(const pg_pipeline_t* pipeline);
/**
* @brief Texture creation options
*/
typedef struct pg_texture_opts_t
{
int mipmaps; //!< Mipmap level
} pg_texture_opts_t;
/**
* @brief Creates a texture from an RGBA8 image
* @param width Image width
* @param height Image height
* @param data Image data (format must be RGBA8)
* @param size Size of the data in bytes
* @param opts Texture creation options (NULL for defaults)
* @returns A texture created from a bitmap
*/
pg_texture_t* pg_create_texture(pg_ctx_t* ctx,
int width, int height,
const uint8_t* data, size_t size,
const pg_texture_opts_t* opts);
/**
* @brief Creates a render target
* @param width Render target width
* @param height Render target height
* @param opts Texture creation options (NULL for defaults)
* @returns A render texture
*/
pg_texture_t* pg_create_render_texture(pg_ctx_t* ctx,
int width, int height,
const pg_texture_opts_t* opts);
/**
* @brief Destroys a texture
*/
void pg_destroy_texture(pg_texture_t* texture);
/**
* @brief Returns a texture ID
*/
uint32_t pg_get_texture_id(const pg_texture_t* texture);
/**
* @brief Gets a texture's dimensions
*/
void pg_get_texture_size(const pg_texture_t* texture, int* width, int* height);
/**
* @brief Updates a texture with the given data. This can only be called once
* per frame
*/
void pg_update_texture(pg_texture_t* texture, char* data, int width, int height);
/**
* @brief Sampler options
*/
typedef struct
{
bool smooth; //!< Linear filtering if true, nearest otherwise
bool repeat_u; //!< Repeat if true, clamp-to-edge otherwise
bool repeat_v; //!< Repeat if true, clamp-to-edge otherwise
} pg_sampler_opts_t;
/**
* @brief Creates a sampler represents an object that can control how shaders
* transform and filter texture resource data.
* @param opts Sampler options
*/
pg_sampler_t* pg_create_sampler(pg_ctx_t* ctx, const pg_sampler_opts_t* opts);
/**
* @brief Destroys a sampler object
*/
void pg_destroy_sampler(pg_sampler_t* sampler);
/**
* @brief Buffer update criteria
*/
typedef enum
{
PG_USAGE_STATIC, //!< Buffer is immutable (cannot be updated)
PG_USAGE_DYNAMIC, //!< Buffer is updated on average less than once per frame
PG_USAGE_STREAM //!< Buffer is updated possibly more than once per frame
} pg_buffer_usage_t;
/**
* @brief Creates a vertex buffer
* @param usage Determines whether the buffer is static, dynamic, or streaming
* @param data Vertex data elements (can be NULL)
* @param count The number of elements (can be zero)
* @param max_elements The maximum number of elements in the buffer
* @param element_size The size (in bytes) of each individual element
*/
pg_buffer_t* pg_create_vertex_buffer(pg_ctx_t* ctx,
pg_buffer_usage_t usage,
const void* data,
size_t count,
size_t max_elements,
size_t element_size);
/**
* @brief Creates a vertex buffer
* @param usage Determines whether the buffer is static, dynamic, or streaming
* @param data Index data (can be NULL)
* @param count The number of indices (can be zero)
* @param max_elements The maximum number of indices in the buffer
*/
pg_buffer_t* pg_create_index_buffer(pg_ctx_t* ctx,
pg_buffer_usage_t usage,
const void* data,
size_t count,
size_t max_elements);
/**
* @brief Destroys a vertex or index buffer
*/
void pg_destroy_buffer(pg_buffer_t* buffer);
/**
* Replaces the data in a buffer. This may only happen once per frame and cannot
* happen after appending data
*/
void pg_update_buffer(pg_buffer_t* buffer, void* data, size_t count);
/**
* @brief Appends data to a buffer. This can happen more than once per frame,
* and cannot happen after an update.
*/
int pg_append_buffer(pg_buffer_t* buffer, void* data, size_t count);
/**
* @brief Returns the buffer offset
*/
int pg_get_buffer_offset(pg_buffer_t* buffer);
/**
* @brief Sets the buffer offset
*/
void pg_set_buffer_offset(pg_buffer_t* buffer, int offset);
/**
* @brief Destroys and recreates buffer
*/
void pg_reset_buffer(pg_buffer_t* buffer);
/**
* @brief Draws from the buffers that are bound to the current state
* @param ctx The graphics context
* @param start The position of the first element
* @param count The number of elements to draw
* @param instances The number of instances
*/
void pg_draw(const pg_ctx_t* ctx, size_t start, size_t count, size_t instances);
/*=============================================================================
* Internals
*============================================================================*/
typedef struct
{
const sg_shader_desc* (*get_shader_desc)(sg_backend backend);
int (*get_attr_slot)(const char* attr_name);
int (*get_img_slot)(sg_shader_stage stage, const char* name);
int (*get_smp_slot)(sg_shader_stage stage, const char* name);
int (*get_uniformblock_slot)(sg_shader_stage stage, const char* ub_name);
size_t (*get_uniformblock_size)(sg_shader_stage stage, const char* ub_name);
} pg_shader_internal_t;
pg_shader_t* pg_create_shader_internal(pg_ctx_t* ctx, pg_shader_internal_t internal);
#endif // PICO_GFX_H
/*=============================================================================
* Implementation
*============================================================================*/
#ifdef PICO_GFX_IMPLEMENTATION
#include <string.h>
/*=============================================================================
* Constants
*============================================================================*/
#ifndef PICO_GFX_STACK_MAX_SIZE
#define PICO_GFX_STACK_MAX_SIZE 16
#endif
#ifndef PICO_GFX_HASHTABLE_KEY_SIZE
#define PICO_GFX_HASHTABLE_KEY_SIZE 16
#endif
/*=============================================================================
* Macros
*============================================================================*/
#ifdef NDEBUG
#define PICO_GFX_ASSERT(expr) ((void)0)
#else
#ifndef PICO_GFX_ASSERT
#include <assert.h>
#define PICO_GFX_ASSERT(expr) (assert(expr))
#endif
#endif
#if !defined(PICO_GFX_MALLOC) || !defined(PICO_GFX_REALLOC) || !defined(PICO_GFX_FREE)
#include <stdlib.h>
#define PICO_GFX_MALLOC(size, ctx) (malloc(size))
#define PICO_GFX_REALLOC(ptr, size, ctx) (realloc(ptr, size))
#define PICO_GFX_FREE(ptr, ctx) (free(ptr))
#endif
#ifndef PICO_GFX_LOG
#include <stdio.h>
#define PICO_GFX_LOG(...) (pg_log(__VA_ARGS__))
#endif
/*=============================================================================
* GFX Static Funtions
*============================================================================*/
typedef enum
{
PG_BUFFER_TYPE_VERTEX,
PG_BUFFER_TYPE_INDEX,
} pg_buffer_type_t;
static sg_primitive_type pg_map_primitive(pg_primitive_t primitive);
static sg_blend_factor pg_map_blend_factor(pg_blend_factor_t factor);
static sg_blend_op pg_map_blend_eq(pg_blend_eq_t eq);
static sg_shader_stage pg_map_stage(pg_stage_t stage);
static sg_vertex_format pg_map_vertex_format(pg_vertex_format_t format);
static sg_usage pg_map_usage(pg_buffer_usage_t format);
static sg_buffer_type pg_map_buffer_type(pg_buffer_type_t type);
static void pg_log_sg(const char* tag, // e.g. 'sg'
uint32_t log_level, // 0=panic, 1=error, 2=warn, 3=info
uint32_t log_item_id, // SG_LOGITEM_*
const char* message_or_null, // a message string, may be nullptr in release mode
uint32_t line_nr, // line number in sokol_gfx.h
const char* filename_or_null, // source filename, may be nullptr in release mode
void* user_data);
static void pg_log(const char* fmt, ...);
/*=============================================================================
* Hashtable Declarations
*============================================================================*/
#ifdef PICO_GFX_32BIT
typedef uint32_t pg_hash_t;
#else
typedef uint64_t pg_hash_t;
#endif
typedef struct pg_hashtable_t pg_hashtable_t;
typedef struct pg_hashtable_iterator_t pg_hashtable_iterator_t;
typedef void (*pg_hashtable_iterator_fn)(pg_hashtable_iterator_t* iterator,
char* key,
void* value);
static pg_hashtable_t* pg_hashtable_new(size_t capacity, size_t key_size,
size_t value_size, void* mem_ctx);
static void pg_hashtable_free(pg_hashtable_t* ht);
static void pg_hashtable_init_iterator(const pg_hashtable_t* ht,
pg_hashtable_iterator_t* iterator);
static bool pg_hashtable_iterator_next(pg_hashtable_iterator_t* iterator,
char** key, void** value);
static void pg_hashtable_put(pg_hashtable_t* ht,
const char* key,
const void* value);
static void* pg_hashtable_get(const pg_hashtable_t* ht, const char* key);
struct pg_hashtable_iterator_t
{
const pg_hashtable_t* ht;
size_t index;
size_t count;
};
/*=============================================================================
* Arena Allocator Declarations
*============================================================================*/
typedef struct pg_arena_t pg_arena_t;
static pg_arena_t* pg_arena_new(size_t size, void* mem_ctx);
static void* pg_arena_alloc(pg_arena_t* arena, size_t size);
static void pg_arena_free(pg_arena_t* arena);
/*=============================================================================
* GFX Public API Implementation
*============================================================================*/
static void* pg_malloc(size_t size, void* ctx)
{
(void)ctx;
return PICO_GFX_MALLOC(size, ctx);
}
static void pg_free(void* ptr, void* ctx)
{
(void)ctx;
PICO_GFX_FREE(ptr, ctx);
}
typedef struct pg_rect_t
{
int x, y, width, height;
} pg_rect_t;
typedef struct pg_state_t
{
sg_color clear_color;
pg_pipeline_t* pipeline;
pg_rect_t viewport;
pg_rect_t scissor;
pg_shader_t* shader;
pg_buffer_t* index_buffer;
pg_buffer_t* buffers[PG_MAX_VERTEX_BUFFERS];
} pg_state_t;
struct pg_ctx_t
{
void* mem_ctx;
sg_swapchain swapchain;
int window_width;
int window_height;
bool indexed;
bool pass_active;
pg_texture_t* target;
sg_pass default_pass;
pg_state_t state;
pg_state_t state_stack[PICO_GFX_STACK_MAX_SIZE];
int stack_size;
};
struct pg_pipeline_t
{
pg_ctx_t* ctx;
sg_pipeline handle;
size_t element_size;
bool indexed;
pg_shader_t* shader;
};
struct pg_shader_t
{
pg_ctx_t* ctx;
const sg_shader_desc* desc;
sg_shader handle;
pg_shader_internal_t internal;
pg_texture_t* textures[PG_MAX_TEXTURE_SLOTS];
pg_sampler_t* samplers[PG_MAX_SAMPLER_SLOTS];
pg_hashtable_t* uniform_blocks;
pg_arena_t* arena;
};
typedef struct
{
int slot;
pg_stage_t stage;
void* data;
size_t size;
} pg_uniform_block_t;
struct pg_texture_t
{
pg_ctx_t* ctx;
int width, height;
bool target;
sg_image handle;
sg_image depth_handle;
sg_attachments attachments;
};
struct pg_sampler_t
{
pg_ctx_t* ctx;
sg_sampler handle;
};
struct pg_buffer_t
{
pg_ctx_t* ctx;
sg_buffer handle;
pg_buffer_type_t type;
pg_buffer_usage_t usage;
size_t count;
size_t element_size;
size_t size;
size_t offset;
};
void pg_init(void)
{
sg_setup(&(sg_desc)
{
.logger.func = pg_log_sg,
.allocator =
{
.alloc_fn = pg_malloc,
.free_fn = pg_free,
.user_data = NULL,
},
.environment.defaults.color_format = SG_PIXELFORMAT_RGBA8,
});
}
void pg_shutdown(void)
{
sg_shutdown();
}