-
Notifications
You must be signed in to change notification settings - Fork 79
/
Copy pathgs_gfxt.h
3575 lines (3097 loc) · 141 KB
/
gs_gfxt.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
/*================================================================
* Copyright: 2020 John Jackson
* gs_gfxt: Graphics Extension Util for Gunslinger
* File: gs_gfxt.h
All Rights Reserved
=================================================================*/
#ifndef GS_GFXT_H
#define GS_GFXT_H
/*
USAGE: (IMPORTANT)
=================================================================================================================
Before including, define the gunslinger graphics extension implementation like this:
#define GS_GFXT_IMPL
in EXACTLY ONE C or C++ file that includes this header, BEFORE the
include, like this:
#define GS_GFXT_IMPL
#include "gs_gfxt.h"
All other files should just #include "gs_gfxt.h" without the #define.
MUST include "gs.h" and declare GS_IMPL BEFORE this file, since this file relies on gunslinger core:
#define GS_IMPL
#include "gs.h"
#define GS_GFXT_IMPL
#include "gs_gfxt.h"
GS_GFXT_HNDL:
Internally, gfxt uses handles to retrieve raw data for shared types, like materials, meshes, pipelines, etc.
This keeps memory consumption low allowing for trivial copying of references and handles without having to copy raw data.
This handle, by default, is defined to a void* for pointers for raw data storage. This can be defined by the user to whatever
handle type he'd like to use, such as a uint32_t handle to a slot array id. Additionally, all description objects that
operate on shared data pass around a function pointer descriptor for how to retrieve that data - all of which can be user defined.
================================================================================================================
*/
/*==== Interface ====*/
/** @defgroup gs_graphics_extension_util Graphics Extension Util
* Gunslinger Graphics Extension Util
* @{
*/
//=== Raw data retrieval/Handles ===//
#ifndef GS_GFXT_HNDL
#define GS_GFXT_HNDL void* // Default handle will just be a pointer to the data itself
#endif
// Config
#ifndef GS_GFXT_TEX_COORD_MAX
#define GS_GFXT_TEX_COORD_MAX 4
#endif
#ifndef GS_GFXT_COLOR_MAX
#define GS_GFXT_COLOR_MAX 4
#endif
#ifndef GS_GFXT_JOINT_MAX
#define GS_GFXT_JOINT_MAX 4
#endif
#ifndef GS_GFXT_WEIGHT_MAX
#define GS_GFXT_WEIGHT_MAX 4
#endif
// Custom UINT field
#ifndef GS_GFXT_CUSTOM_UINT_MAX
#define GS_GFXT_CUSTOM_UINT_MAX 4
#endif
#ifndef GS_GFXT_INCLUDE_DIR_MAX
#define GS_GFXT_INCLUDE_DIR_MAX 8
#endif
#ifndef GS_GFXT_UNIFORM_VIEW_MATRIX
#define GS_GFXT_UNIFORM_VIEW_MATRIX "U_VIEW_MTX"
#endif
#ifndef GS_GFXT_UNIFORM_PROJECTION_MATRIX
#define GS_GFXT_UNIFORM_PROJECTION_MATRIX "U_PROJECTION_MTX"
#endif
#ifndef GS_GFXT_UNIFORM_VIEW_PROJECTION_MATRIX
#define GS_GFXT_UNIFORM_VIEW_PROJECTION_MATRIX "U_VIEW_PROJECTION_MTX"
#endif
#ifndef GS_GFXT_UNIFORM_MODEL_MATRIX
#define GS_GFXT_UNIFORM_MODEL_MATRIX "U_MODEL_MTX"
#endif
#ifndef GS_GFXT_UNIFORM_INVERSE_MODEL_MATRIX
#define GS_GFXT_UNIFORM_INVERSE_MODEL_MATRIX "U_INVERSE_MODEL_MTX"
#endif
#ifndef GS_GFXT_UNIFORM_VIEW_WORLD_POSITION
#define GS_GFXT_UNIFORM_VIEW_WORLD_POSITION "U_VIEW_WORLD_POSITION"
#endif
#ifndef GS_GFXT_UNIFORM_MODEL_VIEW_PROJECTION_MATRIX
#define GS_GFXT_UNIFORM_MODEL_VIEW_PROJECTION_MATRIX "U_MVP_MTX"
#endif
#ifndef GS_GFXT_UNIFORM_TIME
#define GS_GFXT_UNIFORM_TIME "U_TIME"
#endif
typedef void* (*gs_gfxt_raw_data_func)(GS_GFXT_HNDL hndl, void* user_data);
#define GS_GFXT_RAW_DATA(FUNC_DESC, T)\
((T*)(FUNC_DESC)->func((FUNC_DESC)->hndl, (FUNC_DESC)->user_data))
typedef struct gs_gfxt_raw_data_func_desc_t {
GS_GFXT_HNDL hndl; // Handle used for retrieving data.
gs_gfxt_raw_data_func func; // User defined function for pipeline data retrieval
void* user_data; // Optional user data for function
} gs_gfxt_raw_data_func_desc_t;
//=== Uniforms/Uniform blocks ===//
typedef struct gs_gfxt_uniform_desc_t {
char name[64]; // Name of uniform (for binding to shader)
gs_graphics_uniform_type type; // Type of uniform: GS_GRAPHICS_UNIFORM_VEC2, GS_GRAPHICS_UNIFORM_VEC3, etc.
uint32_t binding; // Binding for this uniform in shader
gs_graphics_shader_stage_type stage; // Shader stage for this uniform
gs_graphics_access_type access_type; // Access type for this uniform (compute only)
} gs_gfxt_uniform_desc_t;
typedef struct gs_gfxt_uniform_t {
gs_handle(gs_graphics_uniform_t) hndl; // Graphics handle resource for actual uniform
uint32_t offset; // Individual offset for this uniform in material byte buffer data
uint32_t binding; // Binding for this uniform
size_t size; // Size of this uniform data in bytes
gs_graphics_uniform_type type; // Type of this uniform
gs_graphics_access_type access_type; // Access type of uniform (compute only)
} gs_gfxt_uniform_t;
typedef struct gs_gfxt_uniform_block_desc_t {
gs_gfxt_uniform_desc_t* layout; // Layout for all uniform data for this block to hold
size_t size; // Size of layout in bytes
} gs_gfxt_uniform_block_desc_t;
typedef struct gs_gfxt_uniform_block_lookup_key_t {
char name[64];
} gs_gfxt_uniform_block_lookup_key_t;
typedef struct gs_gfxt_uniform_block_t {
gs_dyn_array(gs_gfxt_uniform_t) uniforms; // Raw uniform handle array
gs_hash_table(uint64_t, uint32_t) lookup; // Index lookup table (used for byte buffer offsets in material uni. data)
size_t size; // Total size of material data for entire block
} gs_gfxt_uniform_block_t;
//=== Texture ===//
typedef gs_handle(gs_graphics_texture_t) gs_gfxt_texture_t;
//=== Mesh ===//
typedef gs_asset_mesh_attribute_type gs_gfxt_mesh_attribute_type;
typedef gs_asset_mesh_layout_t gs_gfxt_mesh_layout_t;
/*
typedef struct
{
union
{
void* interleave;
struct
{
void* positions;
void* normals;
void* tangents;
void* tex_coords[TEX_COORD_MAX];
void* joints[JOINT_MAX];
void* weights[WEIGHT_MAX];
} non_interleave;
} vertex;
size_t vertex_size;
void* indices;
} gs_gfxt_mesh_primitive_data_t;
*/
typedef struct
{
void* data;
size_t size;
} gs_gfxt_mesh_vertex_attribute_t;
typedef struct
{
gs_gfxt_mesh_vertex_attribute_t positions; // All position data
gs_gfxt_mesh_vertex_attribute_t normals;
gs_gfxt_mesh_vertex_attribute_t tangents;
gs_gfxt_mesh_vertex_attribute_t tex_coords[GS_GFXT_TEX_COORD_MAX];
gs_gfxt_mesh_vertex_attribute_t colors[GS_GFXT_COLOR_MAX];
gs_gfxt_mesh_vertex_attribute_t joints[GS_GFXT_JOINT_MAX];
gs_gfxt_mesh_vertex_attribute_t weights[GS_GFXT_WEIGHT_MAX];
gs_gfxt_mesh_vertex_attribute_t custom_uint[GS_GFXT_CUSTOM_UINT_MAX];
gs_gfxt_mesh_vertex_attribute_t indices;
uint32_t count; // Total count of indices
} gs_gfxt_mesh_vertex_data_t;
// Structured/packed raw mesh data
// TODO(john): Make the primitives array static to avoid heap alloc
typedef struct gs_gfxt_mesh_raw_data_t {
gs_dyn_array(gs_gfxt_mesh_vertex_data_t) primitives; // All primitive data
} gs_gfxt_mesh_raw_data_t;
typedef struct gs_gfxt_mesh_import_options_t {
gs_gfxt_mesh_layout_t* layout; // Mesh attribute layout array
size_t size; // Size of mesh attribute layout array in bytes
size_t index_buffer_element_size; // Size of index data size in bytes
} gs_gfxt_mesh_import_options_t;
GS_API_DECL void gs_gfxt_mesh_import_options_free(gs_gfxt_mesh_import_options_t* opt);
typedef struct gs_gfxt_mesh_desc_s {
gs_gfxt_mesh_raw_data_t* meshes; // Mesh data array
size_t size; // Size of mesh data array in bytes
bool32 keep_data; // Whether or not to free data after use
} gs_gfxt_mesh_desc_t;
typedef struct gs_gfxt_vertex_stream_s
{
gs_handle(gs_graphics_vertex_buffer_t) positions;
gs_handle(gs_graphics_vertex_buffer_t) normals;
gs_handle(gs_graphics_vertex_buffer_t) tangents;
gs_handle(gs_graphics_vertex_buffer_t) colors[GS_GFXT_COLOR_MAX];
gs_handle(gs_graphics_vertex_buffer_t) tex_coords[GS_GFXT_TEX_COORD_MAX];
gs_handle(gs_graphics_vertex_buffer_t) joints[GS_GFXT_JOINT_MAX];
gs_handle(gs_graphics_vertex_buffer_t) weights[GS_GFXT_WEIGHT_MAX];
gs_handle(gs_graphics_vertex_buffer_t) custom_uint[GS_GFXT_CUSTOM_UINT_MAX];
} gs_gfxt_vertex_stream_t;
typedef struct gs_gfxt_mesh_primitive_s {
gs_gfxt_vertex_stream_t stream; // All vertex data streams
gs_handle(gs_graphics_index_buffer_t) indices; // Index buffer
uint32_t count; // Total number of vertices
} gs_gfxt_mesh_primitive_t;
typedef struct gs_gfxt_mesh_s {
gs_dyn_array(gs_gfxt_mesh_primitive_t) primitives;
gs_gfxt_mesh_desc_t desc;
} gs_gfxt_mesh_t;
//=== Pipeline ===//
typedef struct gs_gfxt_pipeline_desc_s {
gs_graphics_pipeline_desc_t pip_desc; // Description for constructing pipeline object
gs_gfxt_uniform_block_desc_t ublock_desc; // Description for constructing uniform block object
} gs_gfxt_pipeline_desc_t;
typedef struct gs_gfxt_pipeline_s {
gs_handle(gs_graphics_pipeline_t) hndl; // Graphics handle resource for actual pipeline
gs_gfxt_uniform_block_t ublock; // Uniform block for holding all uniform data
gs_dyn_array(gs_gfxt_mesh_layout_t) mesh_layout;
gs_graphics_pipeline_desc_t desc;
} gs_gfxt_pipeline_t;
//=== Material ===//
typedef struct gs_gfxt_material_desc_s {
gs_gfxt_raw_data_func_desc_t pip_func; // Description for retrieving raw pipeline pointer data from handle.
} gs_gfxt_material_desc_t;
typedef struct gs_gfxt_material_s {
gs_gfxt_material_desc_t desc; // Material description object
gs_byte_buffer_t uniform_data; // Byte buffer of actual uniform data to send to GPU
gs_byte_buffer_t image_buffer_data; // Image buffer data
} gs_gfxt_material_t;
//=== Renderable ===//
typedef struct gs_gfxt_renderable_desc_s {
gs_gfxt_raw_data_func_desc_t mesh; // Description for retrieving raw mesh pointer data from handle.
gs_gfxt_raw_data_func_desc_t material; // Description for retrieving raw material pointer data from handle.
} gs_gfxt_renderable_desc_t;
typedef struct gs_gfxt_renderable_s {
gs_gfxt_renderable_desc_t desc; // Renderable description object
gs_mat4 model_matrix; // Model matrix for renderable
} gs_gfxt_renderable_t;
//=== Graphics scene ===//
typedef struct gs_gfxt_scene_s {
gs_slot_array(gs_gfxt_renderable_t) renderables;
} gs_gfxt_scene_t;
//==== API =====//
//=== Creation ===//
GS_API_DECL gs_gfxt_pipeline_t gs_gfxt_pipeline_create(const gs_gfxt_pipeline_desc_t* desc);
GS_API_DECL gs_gfxt_material_t gs_gfxt_material_create(gs_gfxt_material_desc_t* desc);
GS_API_DECL gs_gfxt_mesh_t gs_gfxt_mesh_create(const gs_gfxt_mesh_desc_t* desc);
GS_API_DECL void gs_gfxt_mesh_update_or_create(gs_gfxt_mesh_t* mesh, const gs_gfxt_mesh_desc_t* desc);
GS_API_DECL gs_gfxt_renderable_t gs_gfxt_renderable_create(const gs_gfxt_renderable_desc_t* desc);
GS_API_DECL gs_gfxt_uniform_block_t gs_gfxt_uniform_block_create(const gs_gfxt_uniform_block_desc_t* desc);
GS_API_DECL gs_gfxt_texture_t gs_gfxt_texture_create(gs_graphics_texture_desc_t* desc);
//=== Destruction ===//
GS_API_DECL void gs_gfxt_texture_destroy(gs_gfxt_texture_t* texture);
GS_API_DECL void gs_gfxt_material_destroy(gs_gfxt_material_t* material);
GS_API_DECL void gs_gfxt_mesh_destroy(gs_gfxt_mesh_t* mesh);
GS_API_DECL void gs_gfxt_uniform_block_destroy(gs_gfxt_uniform_block_t* ub);
GS_API_DECL void gs_gfxt_pipeline_destroy(gs_gfxt_pipeline_t* pipeline);
//=== Resource Loading ===//
GS_API_DECL gs_gfxt_pipeline_t gs_gfxt_pipeline_load_from_file(const char* path);
GS_API_DECL gs_gfxt_pipeline_t gs_gfxt_pipeline_load_from_memory(const char* data, size_t sz);
GS_API_DECL gs_gfxt_pipeline_t gs_gfxt_pipeline_load_from_memory_ext(const char* data, size_t sz, const char* file_dir);
GS_API_DECL gs_gfxt_texture_t gs_gfxt_texture_load_from_file(const char* path, gs_graphics_texture_desc_t* desc, bool flip, bool keep_data);
GS_API_DECL gs_gfxt_texture_t gs_gfxt_texture_load_from_memory(const char* data, size_t sz, gs_graphics_texture_desc_t* desc, bool flip, bool keep_data);
//=== Copy ===//
GS_API_DECL gs_gfxt_material_t gs_gfxt_material_deep_copy(gs_gfxt_material_t* src);
//=== Pipeline API ===//
GS_API_DECL gs_gfxt_uniform_t* gs_gfxt_pipeline_get_uniform(gs_gfxt_pipeline_t* pip, const char* name);
//=== Material API ===//
GS_API_DECL void gs_gfxt_material_set_uniform(gs_gfxt_material_t* mat, const char* name, const void* data);
GS_API_DECL void gs_gfxt_material_bind(gs_command_buffer_t* cb, gs_gfxt_material_t* mat);
GS_API_DECL void gs_gfxt_material_bind_pipeline(gs_command_buffer_t* cb, gs_gfxt_material_t* mat);
GS_API_DECL void gs_gfxt_material_bind_uniforms(gs_command_buffer_t* cb, gs_gfxt_material_t* mat);
GS_API_DECL gs_gfxt_pipeline_t* gs_gfxt_material_get_pipeline(gs_gfxt_material_t* mat);
//=== Mesh API ===//
GS_API_DECL void gs_gfxt_mesh_draw_pipeline(gs_command_buffer_t* cb, gs_gfxt_mesh_t* mesh, gs_gfxt_pipeline_t* pip);
GS_API_DECL void gs_gfxt_mesh_draw_material(gs_command_buffer_t* cb, gs_gfxt_mesh_t* mesh, gs_gfxt_material_t* mat);
GS_API_DECL void gs_gfxt_mesh_draw_materials(gs_command_buffer_t* cb, gs_gfxt_mesh_t* mesh, gs_gfxt_material_t** mats, size_t mats_size);
GS_API_DECL void gs_gfxt_mesh_draw_layout(gs_command_buffer_t* cb, gs_gfxt_mesh_t* mesh, gs_gfxt_mesh_layout_t* layout, size_t layout_size);
GS_API_DECL gs_gfxt_mesh_t gs_gfxt_mesh_load_from_file(const char* file, gs_gfxt_mesh_import_options_t* options);
GS_API_DECL bool gs_gfxt_load_gltf_data_from_file(const char* path, gs_gfxt_mesh_import_options_t* options, gs_gfxt_mesh_raw_data_t** out, uint32_t* mesh_count);
// Util API
GS_API_DECL void* gs_gfxt_raw_data_default_impl(GS_GFXT_HNDL hndl, void* user_data);
// Mesh Generation API
GS_API_DECL gs_gfxt_mesh_t gs_gfxt_mesh_unit_quad_generate(gs_gfxt_mesh_import_options_t* options);
gs_handle(gs_graphics_texture_t) gs_gfxt_texture_generate_default();
/** @} */ // end of gs_graphics_extension_util
#ifdef GS_GFXT_IMPL
/*==== Implementation ====*/
// Creation/Destruction
GS_API_DECL gs_gfxt_pipeline_t
gs_gfxt_pipeline_create(const gs_gfxt_pipeline_desc_t* desc)
{
gs_gfxt_pipeline_t pip = gs_default_val();
if (!desc) {
gs_assert(false);
return pip;
}
pip.hndl = gs_graphics_pipeline_create(&desc->pip_desc);
pip.ublock = gs_gfxt_uniform_block_create(&desc->ublock_desc);
pip.desc = desc->pip_desc;
pip.desc.layout.attrs = gs_malloc(desc->pip_desc.layout.size);
memcpy(pip.desc.layout.attrs, desc->pip_desc.layout.attrs, desc->pip_desc.layout.size);
return pip;
}
GS_API_DECL gs_gfxt_uniform_block_t
gs_gfxt_uniform_block_create(const gs_gfxt_uniform_block_desc_t* desc)
{
gs_gfxt_uniform_block_t block = gs_default_val();
if (!desc) return block;
// Iterate through layout, construct uniforms, place them into hash table
uint32_t offset = 0;
uint32_t image2D_offset = 0;
uint32_t ct = desc->size / sizeof(gs_gfxt_uniform_desc_t);
for (uint32_t i = 0; i < ct; ++i)
{
gs_gfxt_uniform_desc_t* ud = &desc->layout[i];
gs_gfxt_uniform_t u = gs_default_val();
gs_graphics_uniform_desc_t u_desc = gs_default_val();
gs_graphics_uniform_layout_desc_t u_layout = gs_default_val();
u_layout.type = ud->type;
memcpy(u_desc.name, ud->name, 64);
u_desc.layout = &u_layout;
u.binding = ud->binding;
u.type = ud->type;
// Determine offset/hndl
switch (ud->type)
{
case GS_GRAPHICS_UNIFORM_IMAGE2D_RGBA32F:
{
u.offset = image2D_offset;
} break;
default:
{
u.hndl = gs_graphics_uniform_create(&u_desc);
u.offset = offset;
} break;
}
// Add to data offset based on type
switch (ud->type) {
default:
case GS_GRAPHICS_UNIFORM_FLOAT: offset += sizeof(float); break;
case GS_GRAPHICS_UNIFORM_INT: offset += sizeof(int32_t); break;
case GS_GRAPHICS_UNIFORM_VEC2: offset += sizeof(gs_vec2); break;
case GS_GRAPHICS_UNIFORM_VEC3: offset += sizeof(gs_vec3); break;
case GS_GRAPHICS_UNIFORM_VEC4: offset += sizeof(gs_vec4); break;
case GS_GRAPHICS_UNIFORM_MAT4: offset += sizeof(gs_mat4); break;
case GS_GRAPHICS_UNIFORM_SAMPLER2D: offset += sizeof(gs_handle(gs_graphics_texture_t)); break;
case GS_GRAPHICS_UNIFORM_USAMPLER2D: offset += sizeof(gs_handle(gs_graphics_texture_t)); break;
case GS_GRAPHICS_UNIFORM_IMAGE2D_RGBA32F:
{
image2D_offset += sizeof(gs_handle(gs_graphics_texture_t));
} break;
}
// Add uniform to block with name as key
uint64_t key = gs_hash_str64(ud->name);
gs_dyn_array_push(block.uniforms, u);
gs_hash_table_insert(block.lookup, key, gs_dyn_array_size(block.uniforms) - 1);
}
block.size = offset;
return block;
}
GS_API_DECL gs_gfxt_texture_t
gs_gfxt_texture_create(gs_graphics_texture_desc_t* desc)
{
return gs_graphics_texture_create(desc);
}
GS_API_DECL gs_gfxt_material_t
gs_gfxt_material_create(gs_gfxt_material_desc_t* desc)
{
gs_gfxt_material_t mat = gs_default_val();
if (!desc) {
gs_assert(false);
return mat;
}
// Set desc information to defaults if not provided.
if (!desc->pip_func.func) desc->pip_func.func = gs_gfxt_raw_data_default_impl;
gs_gfxt_pipeline_t* pip = GS_GFXT_RAW_DATA(&desc->pip_func, gs_gfxt_pipeline_t);
gs_assert(pip);
mat.desc = *desc;
mat.uniform_data = gs_byte_buffer_new();
mat.image_buffer_data = gs_byte_buffer_new();
gs_byte_buffer_resize(&mat.uniform_data, pip->ublock.size);
gs_byte_buffer_memset(&mat.uniform_data, 0);
return mat;
}
GS_API_DECL gs_gfxt_mesh_t
gs_gfxt_mesh_create(const gs_gfxt_mesh_desc_t* desc)
{
gs_gfxt_mesh_t mesh = gs_default_val();
if (!desc) {
return mesh;
}
const uint32_t mesh_count = desc->size / sizeof(gs_gfxt_mesh_raw_data_t);
// Process all mesh data, add meshes
for (uint32_t i = 0; i < mesh_count; ++i)
{
gs_gfxt_mesh_raw_data_t* m = &desc->meshes[i];
for (uint32_t p = 0; p < gs_dyn_array_size(m->primitives); ++p)
{
// Get raw vertex data
gs_gfxt_mesh_vertex_data_t* vdata = &m->primitives[p];
// Construct primitive
gs_gfxt_mesh_primitive_t prim = gs_default_val();
prim.count = vdata->count;
// Positions
if (vdata->positions.data)
{
gs_graphics_vertex_buffer_desc_t vdesc = gs_default_val();
vdesc.data = vdata->positions.data;
vdesc.size = vdata->positions.size;
prim.stream.positions = gs_graphics_vertex_buffer_create(&vdesc);
if (!desc->keep_data)
{
gs_free(vdata->positions.data);
}
}
// Normals
if (vdata->normals.data)
{
gs_graphics_vertex_buffer_desc_t vdesc = gs_default_val();
vdesc.data = vdata->normals.data;
vdesc.size = vdata->normals.size;
prim.stream.normals = gs_graphics_vertex_buffer_create(&vdesc);
if (!desc->keep_data)
{
gs_free(vdata->normals.data);
}
}
// Tangents
if (vdata->tangents.data)
{
gs_graphics_vertex_buffer_desc_t vdesc = gs_default_val();
vdesc.data = vdata->tangents.data;
vdesc.size = vdata->tangents.size;
prim.stream.tangents = gs_graphics_vertex_buffer_create(&vdesc);
if (!desc->keep_data)
{
gs_free(vdata->tangents.data);
}
}
// Texcoords
for (uint32_t j = 0; j < GS_GFXT_TEX_COORD_MAX; ++j)
{
if (vdata->tex_coords[j].data)
{
gs_graphics_vertex_buffer_desc_t vdesc = gs_default_val();
vdesc.data = vdata->tex_coords[j].data;
vdesc.size = vdata->tex_coords[j].size;
prim.stream.tex_coords[j] = gs_graphics_vertex_buffer_create(&vdesc);
if (!desc->keep_data)
{
gs_free(vdata->tex_coords[j].data);
}
}
}
// Colors
for (uint32_t j = 0; j < GS_GFXT_COLOR_MAX; ++j)
{
if (vdata->colors[j].data)
{
gs_graphics_vertex_buffer_desc_t vdesc = gs_default_val();
vdesc.data = vdata->colors[j].data;
vdesc.size = vdata->colors[j].size;
prim.stream.colors[j] = gs_graphics_vertex_buffer_create(&vdesc);
if (!desc->keep_data)
{
gs_free(vdata->colors[j].data);
}
}
}
// Joints
for (uint32_t j = 0; j < GS_GFXT_JOINT_MAX; ++j)
{
if (vdata->joints[j].data)
{
gs_graphics_vertex_buffer_desc_t vdesc = gs_default_val();
vdesc.data = vdata->joints[j].data;
vdesc.size = vdata->joints[j].size;
prim.stream.joints[j] = gs_graphics_vertex_buffer_create(&vdesc);
if (!desc->keep_data)
{
gs_free(vdata->joints[j].data);
}
}
}
// Weights
for (uint32_t j = 0; j < GS_GFXT_WEIGHT_MAX; ++j)
{
if (vdata->weights[j].data)
{
gs_graphics_vertex_buffer_desc_t vdesc = gs_default_val();
vdesc.data = vdata->weights[j].data;
vdesc.size = vdata->weights[j].size;
prim.stream.weights[j] = gs_graphics_vertex_buffer_create(&vdesc);
if (!desc->keep_data)
{
gs_free(vdata->weights[j].data);
}
}
}
// Index buffer decl
gs_graphics_index_buffer_desc_t idesc = gs_default_val();
idesc.data = vdata->indices.data;
idesc.size = vdata->indices.size;
// Construct index buffer for primitive
prim.indices = gs_graphics_index_buffer_create(&idesc);
if (!desc->keep_data)
{
gs_free(vdata->indices.data);
}
// Add primitive to mesh
gs_dyn_array_push(mesh.primitives, prim);
}
if (!desc->keep_data)
{
gs_dyn_array_free(m->primitives);
}
}
if (!desc->keep_data)
{
gs_free(desc->meshes);
}
return mesh;
}
GS_API_DECL void
gs_gfxt_mesh_update_or_create(gs_gfxt_mesh_t* mesh, const gs_gfxt_mesh_desc_t* desc)
{
if (!desc || !mesh) {
return;
}
/*
// Need to create mesh if not already done
if (gs_dyn_array_empty(mesh->primitives)) {
*mesh = gs_gfxt_mesh_create(desc);
return;
}
*/
const uint32_t mesh_count = desc->size / sizeof(gs_gfxt_mesh_raw_data_t);
// Process all mesh data, add meshes
for (uint32_t i = 0; i < mesh_count; ++i)
{
gs_gfxt_mesh_raw_data_t* m = &desc->meshes[i];
for (uint32_t p = 0; p < gs_dyn_array_size(m->primitives); ++p)
{
// Get raw vertex data
gs_gfxt_mesh_vertex_data_t* vdata = &m->primitives[p];
// Construct or retrieve mesh primitive
gs_gfxt_mesh_primitive_t* prim = NULL;
if (gs_dyn_array_empty(mesh->primitives) || gs_dyn_array_size(mesh->primitives) < p)
{
gs_gfxt_mesh_primitive_t dprim = gs_default_val();
gs_dyn_array_push(mesh->primitives, dprim);
}
prim = &mesh->primitives[p];
// Set prim count
prim->count = vdata->count;
// Positions
if (vdata->positions.data)
{
gs_graphics_vertex_buffer_desc_t vdesc = gs_default_val();
vdesc.data = vdata->positions.data;
vdesc.size = vdata->positions.size;
// Update
if (prim->stream.positions.id)
{
gs_graphics_vertex_buffer_update(prim->stream.positions, &vdesc);
}
// Create
else
{
prim->stream.positions = gs_graphics_vertex_buffer_create(&vdesc);
}
if (!desc->keep_data)
{
gs_free(vdata->positions.data);
}
}
// Normals
if (vdata->normals.data)
{
gs_graphics_vertex_buffer_desc_t vdesc = gs_default_val();
vdesc.data = vdata->normals.data;
vdesc.size = vdata->normals.size;
// Update
if (prim->stream.normals.id)
{
gs_graphics_vertex_buffer_update(prim->stream.normals, &vdesc);
}
else
{
prim->stream.normals = gs_graphics_vertex_buffer_create(&vdesc);
}
if (!desc->keep_data)
{
gs_free(vdata->normals.data);
}
}
// Tangents
if (vdata->tangents.data)
{
gs_graphics_vertex_buffer_desc_t vdesc = gs_default_val();
vdesc.data = vdata->tangents.data;
vdesc.size = vdata->tangents.size;
if (prim->stream.tangents.id)
{
gs_graphics_vertex_buffer_update(prim->stream.tangents, &vdesc);
}
else
{
prim->stream.tangents = gs_graphics_vertex_buffer_create(&vdesc);
}
if (!desc->keep_data)
{
gs_free(vdata->tangents.data);
}
}
// Texcoords
for (uint32_t j = 0; j < GS_GFXT_TEX_COORD_MAX; ++j)
{
if (vdata->tex_coords[j].data)
{
gs_graphics_vertex_buffer_desc_t vdesc = gs_default_val();
vdesc.data = vdata->tex_coords[j].data;
vdesc.size = vdata->tex_coords[j].size;
if (prim->stream.tex_coords[j].id)
{
gs_graphics_vertex_buffer_update(prim->stream.tex_coords[j], &vdesc);
}
else
{
prim->stream.tex_coords[j] = gs_graphics_vertex_buffer_create(&vdesc);
}
if (!desc->keep_data)
{
gs_free(vdata->tex_coords[j].data);
}
}
}
// Colors
for (uint32_t j = 0; j < GS_GFXT_COLOR_MAX; ++j)
{
if (vdata->colors[j].data)
{
gs_graphics_vertex_buffer_desc_t vdesc = gs_default_val();
vdesc.data = vdata->colors[j].data;
vdesc.size = vdata->colors[j].size;
if (prim->stream.colors[j].id)
{
gs_graphics_vertex_buffer_update(prim->stream.colors[j], &vdesc);
}
else
{
prim->stream.colors[j] = gs_graphics_vertex_buffer_create(&vdesc);
}
if (!desc->keep_data)
{
gs_free(vdata->colors[j].data);
}
}
}
// Joints
for (uint32_t j = 0; j < GS_GFXT_JOINT_MAX; ++j)
{
if (vdata->joints[j].data)
{
gs_graphics_vertex_buffer_desc_t vdesc = gs_default_val();
vdesc.data = vdata->joints[j].data;
vdesc.size = vdata->joints[j].size;
if (prim->stream.joints[j].id)
{
gs_graphics_vertex_buffer_update(prim->stream.joints[j], &vdesc);
}
else
{
prim->stream.joints[j] = gs_graphics_vertex_buffer_create(&vdesc);
}
if (!desc->keep_data)
{
gs_free(vdata->joints[j].data);
}
}
}
// Weights
for (uint32_t j = 0; j < GS_GFXT_WEIGHT_MAX; ++j)
{
if (vdata->weights[j].data)
{
gs_graphics_vertex_buffer_desc_t vdesc = gs_default_val();
vdesc.data = vdata->weights[j].data;
vdesc.size = vdata->weights[j].size;
if (prim->stream.weights[j].id)
{
gs_graphics_vertex_buffer_update(prim->stream.weights[j], &vdesc);
}
else
{
prim->stream.weights[j] = gs_graphics_vertex_buffer_create(&vdesc);
}
if (!desc->keep_data)
{
gs_free(vdata->weights[j].data);
}
}
}
// Custom uint
for (uint32_t j = 0; j < GS_GFXT_CUSTOM_UINT_MAX; ++j)
{
if (vdata->custom_uint[j].data)
{
gs_graphics_vertex_buffer_desc_t vdesc = gs_default_val();
vdesc.data = vdata->custom_uint[j].data;
vdesc.size = vdata->custom_uint[j].size;
if (prim->stream.custom_uint[j].id)
{
gs_graphics_vertex_buffer_update(prim->stream.custom_uint[j], &vdesc);
}
else
{
prim->stream.custom_uint[j] = gs_graphics_vertex_buffer_create(&vdesc);
}
if (!desc->keep_data)
{
gs_free(vdata->custom_uint[j].data);
}
}
}
// Index buffer decl
gs_graphics_index_buffer_desc_t idesc = gs_default_val();
idesc.data = vdata->indices.data;
idesc.size = vdata->indices.size;
// Construct index buffer for primitive
if (prim->indices.id)
{
gs_graphics_index_buffer_update(prim->indices, &idesc);
}
else
{
prim->indices = gs_graphics_index_buffer_create(&idesc);
}
if (!desc->keep_data)
{
gs_free(vdata->indices.data);
}
}
if (!desc->keep_data)
{
gs_dyn_array_free(m->primitives);
}
}
if (!desc->keep_data)
{
gs_free(desc->meshes);
}
}
GS_API_DECL gs_gfxt_renderable_t
gs_gfxt_renderable_create(const gs_gfxt_renderable_desc_t* desc)
{
gs_gfxt_renderable_t rend = gs_default_val();
if (!desc) {
return rend;
}
rend.model_matrix = gs_mat4_identity();
rend.desc = *desc;
return rend;
}
//=== Destruction ===//
GS_API_DECL void
gs_gfxt_texture_destroy(gs_gfxt_texture_t* texture)
{
gs_graphics_texture_destroy(*texture);
}
GS_API_DECL void
gs_gfxt_material_destroy(gs_gfxt_material_t* material)
{
// Destroy all material data
gs_byte_buffer_free(&material->uniform_data);
gs_byte_buffer_free(&material->image_buffer_data);
}
GS_API_DECL void
gs_gfxt_mesh_destroy(gs_gfxt_mesh_t* mesh)
{
// Iterate through all primitives, destroy all vertex and index buffers
for (uint32_t p = 0; p < gs_dyn_array_size(mesh->primitives); ++p)
{
gs_gfxt_mesh_primitive_t* prim = &mesh->primitives[p];
// Free index buffer
if (prim->indices.id) gs_graphics_index_buffer_destroy(prim->indices);
// Free vertex stream
if (prim->stream.positions.id) gs_graphics_vertex_buffer_destroy(prim->stream.positions);
if (prim->stream.normals.id) gs_graphics_vertex_buffer_destroy(prim->stream.normals);
if (prim->stream.tangents.id) gs_graphics_vertex_buffer_destroy(prim->stream.tangents);
for (uint32_t i = 0; i < GS_GFXT_COLOR_MAX; ++i)
{
if (prim->stream.colors[i].id) gs_graphics_vertex_buffer_destroy(prim->stream.colors[i]);
}
for (uint32_t i = 0; i < GS_GFXT_TEX_COORD_MAX; ++i)
{
if (prim->stream.tex_coords[i].id) gs_graphics_vertex_buffer_destroy(prim->stream.tex_coords[i]);
}
for (uint32_t i = 0; i < GS_GFXT_JOINT_MAX; ++i)
{
if (prim->stream.joints[i].id) gs_graphics_vertex_buffer_destroy(prim->stream.joints[i]);
}
for ( uint32_t i = 0; i < GS_GFXT_WEIGHT_MAX; ++i )
{
if (prim->stream.weights[i].id) gs_graphics_vertex_buffer_destroy(prim->stream.weights[i]);
}
}
}
GS_API_DECL void
gs_gfxt_uniform_block_destroy(gs_gfxt_uniform_block_t* ub)
{
for (uint32_t i = 0; i < gs_dyn_array_size(ub->uniforms); ++i)
{
gs_gfxt_uniform_t* u = &ub->uniforms[i];
gs_graphics_uniform_destroy(u->hndl);
}
gs_dyn_array_free(ub->uniforms);
gs_hash_table_free(ub->lookup);
}
GS_API_DECL void
gs_gfxt_pipeline_destroy(gs_gfxt_pipeline_t* pipeline)
{
// Destroy uniform block for pipeline
gs_gfxt_uniform_block_destroy(&pipeline->ublock);
// Free shaders (if responsible for them)
gs_graphics_shader_destroy(pipeline->desc.raster.shader);
// Destroy pipeline
if (pipeline->desc.layout.attrs) gs_free(pipeline->desc.layout.attrs);
if (pipeline->mesh_layout) gs_dyn_array_free(pipeline->mesh_layout);
gs_graphics_pipeline_destroy(pipeline->hndl);
}
//=== Copy API ===//
GS_API_DECL gs_gfxt_material_t gs_gfxt_material_deep_copy(gs_gfxt_material_t* src)
{
gs_gfxt_material_t mat = gs_gfxt_material_create(&src->desc);
gs_byte_buffer_copy_contents(&mat.uniform_data, &src->uniform_data);
return mat;
}
//=== Pipeline API ===//
GS_API_DECL gs_gfxt_uniform_t*
gs_gfxt_pipeline_get_uniform(gs_gfxt_pipeline_t* pip, const char* name)
{
uint64_t key = gs_hash_str64(name);
if (!gs_hash_table_exists(pip->ublock.lookup, key)) {
return NULL;
}
// Based on name, need to get uniform
uint32_t uidx = gs_hash_table_get(pip->ublock.lookup, key);
return &pip->ublock.uniforms[uidx];
}
//=== Material API ===//