-
Notifications
You must be signed in to change notification settings - Fork 76
/
gs_idraw.h
2689 lines (2265 loc) · 96.9 KB
/
gs_idraw.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
* GSIDraw: Immediate Mode Drawing Util for Gunslinger
* File: gs_idraw.h
All Rights Reserved
=================================================================*/
#ifndef GS_IDRAW_H
#define GS_IDRAW_H
/*
USAGE: (IMPORTANT)
=================================================================================================================
Before including, define the gunslinger immediate draw implementation like this:
#define GS_IMMEDIATE_DRAW_IMPL
in EXACTLY ONE C or C++ file that includes this header, BEFORE the
include, like this:
#define GS_IMMEDIATE_DRAW_IMPL
#include "gs_idraw.h"
All other files should just #include "gs_idraw.h" without the #define.
MUST include "gs.h" and declare GS_IMPL BEFORE this file, since this file relies on that:
#define GS_IMPL
#include "gs.h"
#define GS_IMMEDIATE_DRAW_IMPL
#include "gs_idraw.h"
TODO (john):
* Convert flush command to push back commands
* On final flush, request update for vertex/index buffer data
* Then iterate commands to submit pipelines + state to gfx backend
================================================================================================================
*/
/*==== Interface ====*/
gs_enum_decl(gsi_matrix_type,
GSI_MATRIX_MODELVIEW,
GSI_MATRIX_PROJECTION
);
typedef enum {
GSI_VATTR_POSITION = 0x00,
GSI_VATTR_UV,
GSI_VATTR_COLOR
} gsi_vattr_type;
gs_enum_decl(gsi_layout_type,
GSI_LAYOUT_VATTR, // Using vattr type directly
GSI_LAYOUT_MESH // Using asset mesh types indirectly to be converted internally
);
// Need a configurable pipeline matrix
/*
depth | stencil | face cull | blend | prim
e/d e/d e/d e/d l/t
2 ^ 5 = 32 generated pipeline choices.
*/
enum {
GSI_FLAG_NO_BIND_UNIFORMS = (1 << 0),
GSI_FLAG_NO_BIND_CACHED_PIPELINES = (1 << 1)
};
// Hash bytes of state attr struct to get index key for pipeline
typedef struct gsi_pipeline_state_attr_t
{
uint16_t depth_enabled;
uint16_t stencil_enabled;
uint16_t blend_enabled;
uint16_t face_cull_enabled;
uint16_t prim_type;
} gsi_pipeline_state_attr_t;
typedef struct gs_immediate_vert_t
{
gs_vec3 position;
gs_vec2 uv;
gs_color_t color;
} gs_immediate_vert_t;
typedef struct gs_immediate_cache_t
{
gs_dyn_array(gs_handle(gs_graphics_pipeline_t)) pipelines;
gs_dyn_array(gs_mat4) modelview;
gs_dyn_array(gs_mat4) projection;
gs_dyn_array(gsi_matrix_type) modes;
gs_vec2 uv;
gs_color_t color;
gs_handle(gs_graphics_texture_t) texture;
gsi_pipeline_state_attr_t pipeline;
} gs_immediate_cache_t;
typedef struct gs_immediate_draw_static_data_t
{
gs_handle(gs_graphics_texture_t) tex_default;
gs_asset_font_t font_default;
gs_hash_table(gsi_pipeline_state_attr_t, gs_handle(gs_graphics_pipeline_t)) pipeline_table;
gs_handle(gs_graphics_uniform_t) uniform;
gs_handle(gs_graphics_uniform_t) sampler;
gs_handle(gs_graphics_vertex_buffer_t) vbo;
gs_handle(gs_graphics_index_buffer_t) ibo;
} gs_immediate_draw_static_data_t;
typedef struct gs_immediate_draw_t
{
gs_byte_buffer_t vertices;
gs_dyn_array(uint16_t) indices;
gs_dyn_array(gsi_vattr_type) vattributes;
gs_immediate_cache_t cache;
gs_command_buffer_t commands;
uint32_t window_handle;
gs_immediate_draw_static_data_t* data;
uint32_t flags;
} gs_immediate_draw_t;
#ifndef GS_NO_SHORT_NAME
typedef gs_immediate_draw_t gsid;
#define gsi_create gs_immediate_draw_new
#define gsi_free gs_immediate_draw_free
#endif
// Create / Init / Shutdown / Free
GS_API_DECL gs_immediate_draw_t gs_immediate_draw_new();
GS_API_DECL void gs_immediate_draw_free(gs_immediate_draw_t* gsi);
GS_API_DECL void gs_immediate_draw_static_data_set(gs_immediate_draw_static_data_t* data);
// Get pipeline from state
GS_API_DECL gs_handle(gs_graphics_pipeline_t) gsi_get_pipeline(gs_immediate_draw_t* gsi, gsi_pipeline_state_attr_t state);
// Get default font asset pointer
GS_API_DECL gs_asset_font_t* gsi_default_font();
// Core Vertex Functions
GS_API_DECL void gsi_begin(gs_immediate_draw_t* gsi, gs_graphics_primitive_type type);
GS_API_DECL void gsi_end(gs_immediate_draw_t* gsi);
GS_API_DECL void gsi_tc2f(gs_immediate_draw_t* gsi, float u, float v);
GS_API_DECL void gsi_tc2fv(gs_immediate_draw_t* gsi, gs_vec2 uv);
GS_API_DECL void gsi_c4ub(gs_immediate_draw_t* gsi, uint8_t r, uint8_t g, uint8_t b, uint8_t a);
GS_API_DECL void gsi_c4ubv(gs_immediate_draw_t* gsi, gs_color_t c);
GS_API_DECL void gsi_v2f(gs_immediate_draw_t* gsi, float x, float y);
GS_API_DECL void gsi_v2fv(gs_immediate_draw_t* gsi, gs_vec2 v);
GS_API_DECL void gsi_v3f(gs_immediate_draw_t* gsi, float x, float y, float z);
GS_API_DECL void gsi_v3fv(gs_immediate_draw_t* gsi, gs_vec3 v);
GS_API_DECL void gsi_flush(gs_immediate_draw_t* gsi);
GS_API_DECL void gsi_texture(gs_immediate_draw_t* gsi, gs_handle(gs_graphics_texture_t) texture);
// Core pipeline functions
GS_API_DECL void gsi_blend_enabled(gs_immediate_draw_t* gsi, bool enabled);
GS_API_DECL void gsi_depth_enabled(gs_immediate_draw_t* gsi, bool enabled);
GS_API_DECL void gsi_stencil_enabled(gs_immediate_draw_t* gsi, bool enabled);
GS_API_DECL void gsi_face_cull_enabled(gs_immediate_draw_t* gsi, bool enabled);
GS_API_DECL void gsi_defaults(gs_immediate_draw_t* gsi);
GS_API_DECL void gsi_pipeline_set(gs_immediate_draw_t* gsi, gs_handle(gs_graphics_pipeline_t) pipeline); // Binds custom user pipeline, sets flag GSI_FLAG_NO_BIND_CACHED_PIPELINES
GS_API_DECL void gsi_vattr_list(gs_immediate_draw_t* gsi, gsi_vattr_type* layout, size_t sz); // Sets user vertex attribute list for custom bound pipeline
GS_API_DECL void gsi_vattr_list_mesh(gs_immediate_draw_t* gsi, gs_asset_mesh_layout_t* layout, size_t sz); // Same as above but uses mesh layout to determine which vertex attributes to bind and in what order
// View/Scissor commands
GS_API_DECL void gsi_set_view_scissor(gs_immediate_draw_t* gsi, uint32_t x, uint32_t y, uint32_t w, uint32_t h);
// Final Submit / Merge
GS_API_DECL void gsi_draw(gs_immediate_draw_t* gsi, gs_command_buffer_t* cb);
GS_API_DECL void gsi_renderpass_submit(gs_immediate_draw_t* gsi, gs_command_buffer_t* cb, gs_vec4 viewport, gs_color_t clear_color);
GS_API_DECL void gsi_renderpass_submit_ex(gs_immediate_draw_t* gsi, gs_command_buffer_t* cb, gs_vec4 viewport, gs_graphics_clear_action_t* action);
// Core Matrix Functions
GS_API_DECL void gsi_push_matrix(gs_immediate_draw_t* gsi, gsi_matrix_type type);
GS_API_DECL void gsi_push_matrix_ex(gs_immediate_draw_t* gsi, gsi_matrix_type type, bool flush);
GS_API_DECL void gsi_pop_matrix(gs_immediate_draw_t* gsi);
GS_API_DECL void gsi_pop_matrix_ex(gs_immediate_draw_t* gsi, bool flush);
GS_API_DECL void gsi_matrix_mode(gs_immediate_draw_t* gsi, gsi_matrix_type type);
GS_API_DECL void gsi_load_matrix(gs_immediate_draw_t* gsi, gs_mat4 m);
GS_API_DECL void gsi_mul_matrix(gs_immediate_draw_t* gsi, gs_mat4 m);
GS_API_DECL void gsi_perspective(gs_immediate_draw_t* gsi, float fov, float aspect, float near, float far);
GS_API_DECL void gsi_ortho(gs_immediate_draw_t* gsi, float left, float right, float bottom, float top, float near, float far);
GS_API_DECL void gsi_rotatef(gs_immediate_draw_t* gsi, float angle, float x, float y, float z);
GS_API_DECL void gsi_rotatev(gs_immediate_draw_t* gsi, float angle, gs_vec3 v);
GS_API_DECL void gsi_translatef(gs_immediate_draw_t* gsi, float x, float y, float z);
GS_API_DECL void gsi_translatev(gs_immediate_draw_t* gsi, gs_vec3 v);
GS_API_DECL void gsi_scalef(gs_immediate_draw_t* gsi, float x, float y, float z);
// Camera Utils
GS_API_DECL void gsi_camera(gs_immediate_draw_t* gsi, gs_camera_t* cam, uint32_t width, uint32_t height);
GS_API_DECL void gsi_camera2D(gs_immediate_draw_t* gsi, uint32_t width, uint32_t height);
GS_API_DECL void gsi_camera3D(gs_immediate_draw_t* gsi, uint32_t width, uint32_t height);
// Primitive Drawing Util
GS_API_DECL void gsi_triangle(gs_immediate_draw_t* gsi, float x0, float y0, float x1, float y1, float x2, float y2, uint8_t r, uint8_t g, uint8_t b, uint8_t a, gs_graphics_primitive_type type);
GS_API_DECL void gsi_trianglev(gs_immediate_draw_t* gsi, gs_vec2 a, gs_vec2 b, gs_vec2 c, gs_color_t color, gs_graphics_primitive_type type);
GS_API_DECL void gsi_trianglex(gs_immediate_draw_t* gsi, float x0, float y0, float z0, float x1, float y1, float z1, float x2, float y2, float z2, float u0, float v0, float u1, float v1, float u2, float v2, uint8_t r, uint8_t g, uint8_t b, uint8_t a, gs_graphics_primitive_type type);
GS_API_DECL void gsi_trianglevx(gs_immediate_draw_t* gsi, gs_vec3 v0, gs_vec3 v1, gs_vec3 v2, gs_vec2 uv0, gs_vec2 uv1, gs_vec2 uv2, gs_color_t color, gs_graphics_primitive_type type);
GS_API_DECL void gsi_trianglevxmc(gs_immediate_draw_t* gsi, gs_vec3 v0, gs_vec3 v1, gs_vec3 v2, gs_vec2 uv0, gs_vec2 uv1, gs_vec2 uv2, gs_color_t c0, gs_color_t c1, gs_color_t c2, gs_graphics_primitive_type type);
GS_API_DECL void gsi_line(gs_immediate_draw_t* gsi, float x0, float y0, float x1, float y1, uint8_t r, uint8_t g, uint8_t b, uint8_t a);
GS_API_DECL void gsi_linev(gs_immediate_draw_t* gsi, gs_vec2 v0, gs_vec2 v1, gs_color_t c);
GS_API_DECL void gsi_line3D(gs_immediate_draw_t* gsi, float x0, float y0, float z0, float x1, float y1, float z1, uint8_t r, uint8_t g, uint8_t b, uint8_t a);
GS_API_DECL void gsi_line3Dv(gs_immediate_draw_t* gsi, gs_vec3 s, gs_vec3 e, gs_color_t color);
GS_API_DECL void gsi_line3Dmc(gs_immediate_draw_t* gsi, float x0, float y0, float z0, float x1, float y1, float z1, uint8_t r0, uint8_t g0, uint8_t b0, uint8_t a0, uint8_t r1, uint8_t g1, uint8_t b1, uint8_t a1);
// Shape Drawing Util
GS_API_DECL void gsi_rect(gs_immediate_draw_t* gsi, float x0, float y0, float x1, float y1, uint8_t r, uint8_t g, uint8_t b, uint8_t a, gs_graphics_primitive_type type);
GS_API_DECL void gsi_rectv(gs_immediate_draw_t* gsi, gs_vec2 bl, gs_vec2 tr, gs_color_t color, gs_graphics_primitive_type type);
GS_API_DECL void gsi_rectx(gs_immediate_draw_t* gsi, float l, float b, float r, float t, float u0, float v0, float u1, float v1, uint8_t _r, uint8_t _g, uint8_t _b, uint8_t _a, gs_graphics_primitive_type type);
GS_API_DECL void gsi_rectvx(gs_immediate_draw_t* gsi, gs_vec2 bl, gs_vec2 tr, gs_vec2 uv0, gs_vec2 uv1, gs_color_t color, gs_graphics_primitive_type type);
GS_API_DECL void gsi_rectvd(gs_immediate_draw_t* gsi, gs_vec2 xy, gs_vec2 wh, gs_vec2 uv0, gs_vec2 uv1, gs_color_t color, gs_graphics_primitive_type type);
GS_API_DECL void gsi_rect3Dv(gs_immediate_draw_t* gsi, gs_vec3 min, gs_vec3 max, gs_vec2 uv0, gs_vec2 uv1, gs_color_t color, gs_graphics_primitive_type type);
GS_API_DECL void gsi_rect3Dvd(gs_immediate_draw_t* gsi, gs_vec3 xyz, gs_vec3 whd, gs_vec2 uv0, gs_vec2 uv1, gs_color_t c, gs_graphics_primitive_type type);
GS_API_DECL void gsi_circle(gs_immediate_draw_t* gsi, float cx, float cy, float radius, int32_t segments, uint8_t r, uint8_t g, uint8_t b, uint8_t a, gs_graphics_primitive_type type);
GS_API_DECL void gsi_circlevx(gs_immediate_draw_t* gsi, gs_vec3 c, float radius, int32_t segments, gs_color_t color, gs_graphics_primitive_type type);
GS_API_DECL void gsi_circle_sector(gs_immediate_draw_t* gsi, float cx, float cy, float radius, int32_t start_angle, int32_t end_angle, int32_t segments, uint8_t r, uint8_t g, uint8_t b, uint8_t a, gs_graphics_primitive_type type);
GS_API_DECL void gsi_circle_sectorvx(gs_immediate_draw_t* gsi, gs_vec3 c, float radius, int32_t start_angle, int32_t end_angle, int32_t segments, gs_color_t color, gs_graphics_primitive_type type);
GS_API_DECL void gsi_arc(gs_immediate_draw_t* gsi, float cx, float cy, float radius_inner, float radius_outer, float start_angle, float end_angle, int32_t segments, uint8_t r, uint8_t g, uint8_t b, uint8_t a,
gs_graphics_primitive_type type);
GS_API_DECL void gsi_box(gs_immediate_draw_t* gsi, float x0, float y0, float z0, float hx, float hy, float hz, uint8_t r, uint8_t g, uint8_t b, uint8_t a, gs_graphics_primitive_type type);
GS_API_DECL void gsi_sphere(gs_immediate_draw_t* gsi, float cx, float cy, float cz, float radius, uint8_t r, uint8_t g, uint8_t b, uint8_t a, gs_graphics_primitive_type type);
GS_API_DECL void gsi_bezier(gs_immediate_draw_t* gsi, float x0, float y0, float x1, float y1, uint8_t r, uint8_t g, uint8_t b, uint8_t a);
GS_API_DECL void gsi_cylinder(gs_immediate_draw_t* gsi, float x, float y, float z, float r_top, float r_bottom, float height, int32_t sides, uint8_t r, uint8_t g, uint8_t b, uint8_t a, gs_graphics_primitive_type type);
GS_API_DECL void gsi_cone(gs_immediate_draw_t* gsi, float x, float y, float z, float radius, float height, int32_t sides, uint8_t r, uint8_t g, uint8_t b, uint8_t a, gs_graphics_primitive_type type);
// Draw planes/poly groups
// Text Drawing Util
GS_API_DECL void gsi_text(gs_immediate_draw_t* gsi, float x, float y, const char* text, const gs_asset_font_t* fp, bool32_t flip_vertical, uint8_t r, uint8_t g, uint8_t b, uint8_t a);
// Paths
/*
GS_API_DECL void gsi_path_begin(gs_immediate_draw_t* gsi);
GS_API_DECL void gsi_path_end(gs_immediate_draw_t* gsi);
GS_API_DECL void gsi_path_moveto(gs_immediate_draw_t* gsi, float x, float y, float z);
GS_API_DECL void gsi_path_lineto(gs_immediate_draw_t* gsi, float x, float y, float z);
GS_API_DECL void gsi_path_arcto(gs_immediate_draw_t* gsi, float x, float y, float z);
*/
// Private Internal Utilities (Not user facing)
GS_API_DECL const char* GSGetDefaultCompressedFontDataTTFBase85();
GS_API_DECL void GSDecode85(const unsigned char* src, unsigned char* dst);
GS_API_DECL unsigned int GSDecode85Byte(char c);
GS_API_DECL unsigned int gs_decompress_length(const unsigned char* input);
GS_API_DECL unsigned int gs_decompress(unsigned char* output, unsigned char* input, unsigned int length);
/*==== Implementation ====*/
#ifdef GS_IMMEDIATE_DRAW_IMPL
// Global instance of immediate draw static data
gs_immediate_draw_static_data_t* g_gsi = NULL;
#define GSI() g_gsi
#ifndef gsi_smooth_circle_error_rate
#define gsi_smooth_circle_error_rate 0.5f
#endif
const f32 gsi_deg2rad = (f32)GS_PI / 180.f;
// Shaders
#if (defined GS_PLATFORM_WEB || defined GS_PLATFORM_ANDROID)
#define GSI_GL_VERSION_STR "#version 300 es\n"
#else
#define GSI_GL_VERSION_STR "#version 330 core\n"
#endif
#ifndef GSI_UNIFORM_MVP_MATRIX
#define GSI_UNIFORM_MVP_MATRIX "u_mvp"
#endif
#ifndef GSI_UNIFORM_TEXTURE2D
#define GSI_UNIFORM_TEXTURE2D "u_tex"
#endif
const char* gsi_v_fillsrc =
GSI_GL_VERSION_STR
"precision mediump float;\n"
"layout(location = 0) in vec3 a_position;\n"
"layout(location = 1) in vec2 a_uv;\n"
"layout(location = 2) in vec4 a_color;\n"
"uniform mat4 "
GSI_UNIFORM_MVP_MATRIX
";\n"
"out vec2 uv;\n"
"out vec4 color;\n"
"void main() {\n"
" gl_Position = "
GSI_UNIFORM_MVP_MATRIX
"* vec4(a_position, 1.0);\n"
" uv = a_uv;\n"
" color = a_color;\n"
"}\n";
const char* gsi_f_fillsrc =
GSI_GL_VERSION_STR
"precision mediump float;\n"
"in vec2 uv;\n"
"in vec4 color;\n"
"uniform sampler2D "
GSI_UNIFORM_TEXTURE2D
";\n"
"out vec4 frag_color;\n"
"void main() {\n"
" vec4 tex_col = texture("
GSI_UNIFORM_TEXTURE2D
", uv);\n"
" if (tex_col.a < 0.5) discard;\n"
" frag_color = color * tex_col;\n"
"}\n";
gsi_pipeline_state_attr_t gsi_pipeline_state_default()
{
gsi_pipeline_state_attr_t attr = gs_default_val();
attr.depth_enabled = false;
attr.stencil_enabled = false;
attr.blend_enabled = true;
attr.face_cull_enabled = false;
attr.prim_type = (uint16_t)GS_GRAPHICS_PRIMITIVE_TRIANGLES;
return attr;
}
void gsi_reset(gs_immediate_draw_t* gsi)
{
gs_command_buffer_clear(&gsi->commands);
gs_byte_buffer_clear(&gsi->vertices);
gs_dyn_array_clear(gsi->indices);
gs_dyn_array_clear(gsi->cache.modelview);
gs_dyn_array_clear(gsi->cache.projection);
gs_dyn_array_clear(gsi->cache.pipelines);
gs_dyn_array_clear(gsi->cache.modes);
gs_dyn_array_clear(gsi->vattributes);
gs_dyn_array_push(gsi->cache.modelview, gs_mat4_identity());
gs_dyn_array_push(gsi->cache.projection, gs_mat4_identity());
gs_dyn_array_push(gsi->cache.modes, GSI_MATRIX_MODELVIEW);
gsi->cache.texture = GSI()->tex_default;
gsi->cache.pipeline = gsi_pipeline_state_default();
gsi->cache.pipeline.prim_type = 0x00;
gsi->cache.uv = gs_v2(0.f, 0.f);
gsi->cache.color = GS_COLOR_WHITE;
gsi->flags = 0x00;
}
void gs_immediate_draw_static_data_init()
{
GSI() = gs_malloc_init(gs_immediate_draw_static_data_t);
// Create uniform buffer
gs_graphics_uniform_layout_desc_t uldesc = gs_default_val();
uldesc.type = GS_GRAPHICS_UNIFORM_MAT4;
gs_graphics_uniform_desc_t udesc = gs_default_val();
memcpy(udesc.name, GSI_UNIFORM_MVP_MATRIX, sizeof(GSI_UNIFORM_MVP_MATRIX));
udesc.layout = &uldesc;
GSI()->uniform = gs_graphics_uniform_create(&udesc);
// Create sampler buffer
gs_graphics_uniform_layout_desc_t sldesc = gs_default_val();
sldesc.type = GS_GRAPHICS_UNIFORM_SAMPLER2D;
gs_graphics_uniform_desc_t sbdesc = gs_default_val();
memcpy(sbdesc.name, GSI_UNIFORM_TEXTURE2D, sizeof(GSI_UNIFORM_TEXTURE2D));
sbdesc.layout = &sldesc;
GSI()->sampler = gs_graphics_uniform_create(&sbdesc);
// Create default texture (4x4 white)
gs_color_t pixels[16] = gs_default_val();
memset(pixels, 255, 16 * sizeof(gs_color_t));
gs_graphics_texture_desc_t tdesc = gs_default_val();
tdesc.width = 4;
tdesc.height = 4;
tdesc.format = GS_GRAPHICS_TEXTURE_FORMAT_RGBA8;
tdesc.min_filter = GS_GRAPHICS_TEXTURE_FILTER_NEAREST;
tdesc.mag_filter = GS_GRAPHICS_TEXTURE_FILTER_NEAREST;
*tdesc.data = pixels;
GSI()->tex_default = gs_graphics_texture_create(&tdesc);
// Create shader
gs_graphics_shader_source_desc_t vsrc; vsrc.type = GS_GRAPHICS_SHADER_STAGE_VERTEX; vsrc.source = gsi_v_fillsrc;
gs_graphics_shader_source_desc_t fsrc; fsrc.type = GS_GRAPHICS_SHADER_STAGE_FRAGMENT; fsrc.source = gsi_f_fillsrc;
gs_graphics_shader_source_desc_t gsi_sources[] = {
vsrc, fsrc
};
gs_graphics_shader_desc_t sdesc = gs_default_val();
sdesc.sources = gsi_sources;
sdesc.size = sizeof(gsi_sources);
memcpy(sdesc.name, "gs_immediate_default_fill_shader", sizeof("gs_immediate_default_fill_shader"));
// Vertex attr layout
gs_graphics_vertex_attribute_desc_t gsi_vattrs[3] = gs_default_val();
gsi_vattrs[0].format = GS_GRAPHICS_VERTEX_ATTRIBUTE_FLOAT3; memcpy(gsi_vattrs[0].name, "a_position", sizeof("a_position"));
gsi_vattrs[1].format = GS_GRAPHICS_VERTEX_ATTRIBUTE_FLOAT2; memcpy(gsi_vattrs[1].name, "a_uv", sizeof("a_uv"));
gsi_vattrs[2].format = GS_GRAPHICS_VERTEX_ATTRIBUTE_BYTE4; memcpy(gsi_vattrs[2].name, "a_color", sizeof("a_color"));
// Iterate through attribute list, then create custom pipelines requested.
gs_handle(gs_graphics_shader_t) shader = gs_graphics_shader_create(&sdesc);
// Pipelines
for (uint16_t d = 0; d < 2; ++d) // Depth
for (uint16_t s = 0; s < 2; ++s) // Stencil
for (uint16_t b = 0; b < 2; ++b) // Blend
for (uint16_t f = 0; f < 2; ++f) // Face Cull
for (uint16_t p = 0; p < 2; ++p) // Prim Type
{
gsi_pipeline_state_attr_t attr = gs_default_val();
attr.depth_enabled = d;
attr.stencil_enabled = s;
attr.blend_enabled = b;
attr.face_cull_enabled = f;
attr.prim_type = p ? (uint16_t)GS_GRAPHICS_PRIMITIVE_TRIANGLES : (uint16_t)GS_GRAPHICS_PRIMITIVE_LINES;
// Create new pipeline based on this arrangement
gs_graphics_pipeline_desc_t pdesc = gs_default_val();
pdesc.raster.shader = shader;
pdesc.raster.index_buffer_element_size = sizeof(uint16_t);
pdesc.raster.face_culling = attr.face_cull_enabled ? GS_GRAPHICS_FACE_CULLING_BACK : (gs_graphics_face_culling_type)0x00;
pdesc.raster.primitive = (gs_graphics_primitive_type)attr.prim_type;
pdesc.blend.func = attr.blend_enabled ? GS_GRAPHICS_BLEND_EQUATION_ADD : (gs_graphics_blend_equation_type)0x00;
pdesc.blend.src = GS_GRAPHICS_BLEND_MODE_SRC_ALPHA;
pdesc.blend.dst = GS_GRAPHICS_BLEND_MODE_ONE_MINUS_SRC_ALPHA;
pdesc.depth.func = d ? GS_GRAPHICS_DEPTH_FUNC_LESS : (gs_graphics_depth_func_type)0x00;
pdesc.stencil.func = s ? GS_GRAPHICS_STENCIL_FUNC_ALWAYS : (gs_graphics_stencil_func_type)0x00;
pdesc.stencil.ref = s ? 1 : 0x00;
pdesc.stencil.comp_mask = s ? 0xFF : 0x00;
pdesc.stencil.write_mask = s ? 0xFF : 0x00;
pdesc.stencil.sfail = s ? GS_GRAPHICS_STENCIL_OP_KEEP : (gs_graphics_stencil_op_type)0x00;
pdesc.stencil.dpfail = s ? GS_GRAPHICS_STENCIL_OP_KEEP : (gs_graphics_stencil_op_type)0x00;
pdesc.stencil.dppass = s ? GS_GRAPHICS_STENCIL_OP_REPLACE : (gs_graphics_stencil_op_type)0x00;
pdesc.layout.attrs = gsi_vattrs;
pdesc.layout.size = sizeof(gsi_vattrs);
gs_handle(gs_graphics_pipeline_t) hndl = gs_graphics_pipeline_create(&pdesc);
gs_hash_table_insert(GSI()->pipeline_table, attr, hndl);
}
// Create default font
gs_asset_font_t* f = &GSI()->font_default;
stbtt_fontinfo font = gs_default_val();
const char* compressed_ttf_data_base85 = GSGetDefaultCompressedFontDataTTFBase85();
s32 compressed_ttf_size = (((s32)strlen(compressed_ttf_data_base85) + 4) / 5) * 4;
void* compressed_ttf_data = gs_malloc((usize)compressed_ttf_size);
GSDecode85((const unsigned char*)compressed_ttf_data_base85, (unsigned char*)compressed_ttf_data);
const u32 buf_decompressed_size = gs_decompress_length((unsigned char*)compressed_ttf_data);
unsigned char* buf_decompressed_data = (unsigned char*)gs_malloc(buf_decompressed_size);
gs_decompress(buf_decompressed_data, (unsigned char*)compressed_ttf_data, (u32)compressed_ttf_size);
const u32 w = 512;
const u32 h = 512;
const u32 num_comps = 4;
u8* alpha_bitmap = (u8*)gs_malloc(w * h);
u8* flipmap = (u8*)gs_malloc(w * h * num_comps);
memset(alpha_bitmap, 0, w * h);
memset(flipmap, 0, w * h * num_comps);
s32 v = stbtt_BakeFontBitmap((u8*)buf_decompressed_data, 0, 13.f, alpha_bitmap, w, h, 32, 96, (stbtt_bakedchar*)f->glyphs); // no guarantee this fits!
// Flip texture
u32 r = h - 1;
for (u32 i = 0; i < h; ++i)
{
for (u32 j = 0; j < w; ++j)
{
u32 i0 = i * w + j;
u32 i1 = i * w * num_comps + j * num_comps;
u8 a = alpha_bitmap[i0];
flipmap[i1 + 0] = 255;
flipmap[i1 + 1] = 255;
flipmap[i1 + 2] = 255;
flipmap[i1 + 3] = a;
}
r--;
}
gs_graphics_texture_desc_t desc = gs_default_val();
desc.width = w;
desc.height = h;
*desc.data = flipmap;
desc.format = GS_GRAPHICS_TEXTURE_FORMAT_RGBA8;
desc.min_filter = GS_GRAPHICS_TEXTURE_FILTER_NEAREST;
desc.mag_filter = GS_GRAPHICS_TEXTURE_FILTER_NEAREST;
// Generate atlas texture for bitmap with bitmap data
f->texture.hndl = gs_graphics_texture_create(&desc);
f->texture.desc = desc;
*f->texture.desc.data = NULL;
// Create vertex buffer
gs_graphics_vertex_buffer_desc_t vdesc = gs_default_val();
vdesc.data = NULL;
vdesc.size = 0;
vdesc.usage = GS_GRAPHICS_BUFFER_USAGE_STREAM;
GSI()->vbo = gs_graphics_vertex_buffer_create(&vdesc);
gs_free(compressed_ttf_data);
gs_free(buf_decompressed_data);
gs_free(alpha_bitmap);
gs_free(flipmap);
}
GS_API_DECL void
gs_immediate_draw_static_data_set(gs_immediate_draw_static_data_t* data)
{
g_gsi = data;
}
// Create / Init / Shutdown / Free
gs_immediate_draw_t gs_immediate_draw_new()
{
if (!GSI())
{
// Construct GSI
gs_immediate_draw_static_data_init();
}
gs_immediate_draw_t gsi = gs_default_val();
memset(&gsi, 0, sizeof(gsi));
// Set gsi static data
gsi.data = g_gsi;
// Init cache
gsi.cache.color = GS_COLOR_WHITE;
// Init command buffer
gsi.commands = gs_command_buffer_new(); // Not totally sure on the syntax for new vs. create
gsi.vertices = gs_byte_buffer_new();
// Set up cache
gsi_reset(&gsi);
return gsi;
}
GS_API_DECL void
gs_immediate_draw_free(gs_immediate_draw_t* ctx)
{
gs_byte_buffer_free(&ctx->vertices);
gs_dyn_array_free(ctx->indices);
gs_dyn_array_free(ctx->vattributes);
gs_command_buffer_free(&ctx->commands);
gs_dyn_array_free(ctx->cache.pipelines);
gs_dyn_array_free(ctx->cache.modelview);
gs_dyn_array_free(ctx->cache.projection);
gs_dyn_array_free(ctx->cache.modes);
}
GS_API_DECL gs_asset_font_t*
gsi_default_font()
{
if (GSI()) return &GSI()->font_default;
return NULL;
}
GS_API_DECL gs_handle(gs_graphics_pipeline_t)
gsi_get_pipeline(gs_immediate_draw_t* gsi, gsi_pipeline_state_attr_t state)
{
// Bind pipeline
gs_assert(gs_hash_table_key_exists(GSI()->pipeline_table, state));
return gs_hash_table_get(GSI()->pipeline_table, state);
}
void gs_immediate_draw_set_pipeline(gs_immediate_draw_t* gsi)
{
if (gsi->flags & GSI_FLAG_NO_BIND_CACHED_PIPELINES)
{
return;
}
// Check validity
if (gsi->cache.pipeline.prim_type != (uint16_t)GS_GRAPHICS_PRIMITIVE_TRIANGLES && gsi->cache.pipeline.prim_type != (uint16_t)GS_GRAPHICS_PRIMITIVE_LINES)
{
gsi->cache.pipeline.prim_type = (uint16_t)GS_GRAPHICS_PRIMITIVE_TRIANGLES;
}
gsi->cache.pipeline.depth_enabled = gs_clamp(gsi->cache.pipeline.depth_enabled, 0, 1);
gsi->cache.pipeline.stencil_enabled = gs_clamp(gsi->cache.pipeline.stencil_enabled, 0, 1);
gsi->cache.pipeline.face_cull_enabled = gs_clamp(gsi->cache.pipeline.face_cull_enabled, 0, 1);
gsi->cache.pipeline.blend_enabled = gs_clamp(gsi->cache.pipeline.blend_enabled, 0, 1);
// Bind pipeline
gs_assert(gs_hash_table_key_exists(GSI()->pipeline_table, gsi->cache.pipeline));
gs_graphics_pipeline_bind(&gsi->commands, gs_hash_table_get(GSI()->pipeline_table, gsi->cache.pipeline));
}
/* Core Vertex Functions */
void gsi_begin(gs_immediate_draw_t* gsi, gs_graphics_primitive_type type)
{
switch (type) {
default:
case GS_GRAPHICS_PRIMITIVE_TRIANGLES: type = GS_GRAPHICS_PRIMITIVE_TRIANGLES; break;
case GS_GRAPHICS_PRIMITIVE_LINES: type = GS_GRAPHICS_PRIMITIVE_LINES; break;
}
// Push a new pipeline?
if (gsi->cache.pipeline.prim_type == type) {
return;
}
// Otherwise, we need to flush previous content
gsi_flush(gsi);
// Set primitive type
gsi->cache.pipeline.prim_type = type;
// Bind pipeline
gs_immediate_draw_set_pipeline(gsi);
}
void gsi_end(gs_immediate_draw_t* gsi)
{
// Not sure what to do here...
}
gs_mat4 gsi_get_modelview_matrix(gs_immediate_draw_t* gsi)
{
return gsi->cache.modelview[gs_dyn_array_size(gsi->cache.modelview) - 1];
}
gs_mat4 gsi_get_projection_matrix(gs_immediate_draw_t* gsi)
{
return gsi->cache.projection[gs_dyn_array_size(gsi->cache.projection) - 1];
}
gs_mat4 gsi_get_mvp_matrix(gs_immediate_draw_t* gsi)
{
gs_mat4* mv = &gsi->cache.modelview[gs_dyn_array_size(gsi->cache.modelview) - 1];
gs_mat4* proj = &gsi->cache.projection[gs_dyn_array_size(gsi->cache.projection) - 1];
return gs_mat4_mul(*proj, *mv);
}
void gsi_flush(gs_immediate_draw_t* gsi)
{
// Don't flush if verts empty
if (gs_byte_buffer_empty(&gsi->vertices)) {
return;
}
// Set up mvp matrix
gs_mat4 mv = gsi->cache.modelview[gs_dyn_array_size(gsi->cache.modelview) - 1];
gs_mat4 proj = gsi->cache.projection[gs_dyn_array_size(gsi->cache.projection) - 1];
gs_mat4 mvp = gs_mat4_mul(proj, mv);
// Update vertex buffer (command buffer version)
gs_graphics_vertex_buffer_desc_t vdesc = gs_default_val();
vdesc.data = gsi->vertices.data;
vdesc.size = gs_byte_buffer_size(&gsi->vertices);
vdesc.usage = GS_GRAPHICS_BUFFER_USAGE_STREAM;
gs_graphics_vertex_buffer_request_update(&gsi->commands, GSI()->vbo, &vdesc);
// Calculate draw count
size_t vsz = sizeof(gs_immediate_vert_t);
if (gs_dyn_array_size(gsi->vattributes))
{
// Calculate vertex stride
size_t stride = 0;
for (uint32_t i = 0; i < gs_dyn_array_size( gsi->vattributes ); ++i)
{
gsi_vattr_type type = gsi->vattributes[i];
switch (type)
{
default: break;
case GSI_VATTR_POSITION: stride += sizeof(gs_vec3); break;
case GSI_VATTR_COLOR: stride += sizeof(gs_color_t); break;
case GSI_VATTR_UV: stride += sizeof(gs_vec2); break;
}
}
vsz = stride;
}
uint32_t ct = gs_byte_buffer_size(&gsi->vertices) / vsz;
// Set up all binding data
gs_graphics_bind_vertex_buffer_desc_t vbuffer = gs_default_val();
vbuffer.buffer = GSI()->vbo;
gs_graphics_bind_uniform_desc_t ubinds[2] = gs_default_val();
ubinds[0].uniform = GSI()->uniform; ubinds[0].data = &mvp;
ubinds[1].uniform = GSI()->sampler; ubinds[1].data = &gsi->cache.texture; ubinds[1].binding = 0;
// Bindings for all buffers: vertex, uniform, sampler
gs_graphics_bind_desc_t binds = gs_default_val();
binds.vertex_buffers.desc = &vbuffer;
// if (gs_dyn_array_empty(gsi->vattributes))
if (~gsi->flags & GSI_FLAG_NO_BIND_UNIFORMS)
{
binds.uniforms.desc = ubinds;
binds.uniforms.size = sizeof(ubinds);
}
// Bind bindings
gs_graphics_apply_bindings(&gsi->commands, &binds);
// Submit draw
gs_graphics_draw_desc_t draw = gs_default_val();
draw.start = 0; draw.count = ct;
gs_graphics_draw(&gsi->commands, &draw);
// Clear data
gs_byte_buffer_clear(&gsi->vertices);
}
// Core pipeline functions
void gsi_blend_enabled(gs_immediate_draw_t* gsi, bool enabled)
{
// Push a new pipeline?
if (gsi->flags & GSI_FLAG_NO_BIND_CACHED_PIPELINES || gsi->cache.pipeline.blend_enabled == enabled) {
return;
}
// Otherwise, we need to flush previous content
gsi_flush(gsi);
// Set primitive type
gsi->cache.pipeline.blend_enabled = enabled;
// Bind pipeline
gs_immediate_draw_set_pipeline(gsi);
}
// Core pipeline functions
void gsi_depth_enabled(gs_immediate_draw_t* gsi, bool enabled)
{
// Push a new pipeline?
if (gsi->flags & GSI_FLAG_NO_BIND_CACHED_PIPELINES || gsi->cache.pipeline.depth_enabled == (uint16_t)enabled) {
return;
}
// Otherwise, we need to flush previous content
gsi_flush(gsi);
// Set primitive type
gsi->cache.pipeline.depth_enabled = (uint16_t)enabled;
// Bind pipeline
gs_immediate_draw_set_pipeline(gsi);
}
void gsi_stencil_enabled(gs_immediate_draw_t* gsi, bool enabled)
{
// Push a new pipeline?
if (gsi->flags & GSI_FLAG_NO_BIND_CACHED_PIPELINES || gsi->cache.pipeline.stencil_enabled == (uint16_t)enabled) {
return;
}
// Otherwise, we need to flush previous content
gsi_flush(gsi);
// Set primitive type
gsi->cache.pipeline.stencil_enabled = (uint16_t)enabled;
// Bind pipeline
gs_immediate_draw_set_pipeline(gsi);
}
void gsi_face_cull_enabled(gs_immediate_draw_t* gsi, bool enabled)
{
// Push a new pipeline?
if (gsi->flags & GSI_FLAG_NO_BIND_CACHED_PIPELINES || gsi->cache.pipeline.face_cull_enabled == (uint16_t)enabled) {
return;
}
// Otherwise, we need to flush previous content
gsi_flush(gsi);
// Set primitive type
gsi->cache.pipeline.face_cull_enabled = (uint16_t)enabled;
// Bind pipeline
gs_immediate_draw_set_pipeline(gsi);
}
GS_API_DECL void gsi_texture(gs_immediate_draw_t* gsi, gs_handle(gs_graphics_texture_t) texture)
{
// Push a new pipeline?
if (gsi->cache.texture.id == texture.id) {
return;
}
// Otherwise, we need to flush previous content
gsi_flush(gsi);
// Set texture
gsi->cache.texture = texture.id && texture.id != UINT32_MAX ? texture : GSI()->tex_default;
}
GS_API_DECL void
gsi_pipeline_set(gs_immediate_draw_t* gsi, gs_handle(gs_graphics_pipeline_t) pipeline)
{
gsi_flush(gsi);
// Bind if valid
if (pipeline.id)
{
gs_graphics_pipeline_bind(&gsi->commands, pipeline);
gsi->flags |= GSI_FLAG_NO_BIND_CACHED_PIPELINES;
}
// Otherwise we set back to cache, clear vattributes, clear flag
else
{
gsi->flags &= ~GSI_FLAG_NO_BIND_CACHED_PIPELINES;
gs_dyn_array_clear(gsi->vattributes);
gs_immediate_draw_set_pipeline(gsi);
}
}
GS_API_DECL void
gsi_vattr_list(gs_immediate_draw_t* gsi, gsi_vattr_type* list, size_t sz)
{
gsi_flush(gsi);
gs_dyn_array_clear(gsi->vattributes);
uint32_t ct = sz / sizeof(gsi_vattr_type);
for (uint32_t i = 0; i < ct; ++i)
{
gs_dyn_array_push(gsi->vattributes, list[i]);
}
}
GS_API_DECL void
gsi_vattr_list_mesh(gs_immediate_draw_t* gsi, gs_asset_mesh_layout_t* layout, size_t sz)
{
gsi_flush(gsi);
gs_dyn_array_clear(gsi->vattributes);
#define VATTR_PUSH(TYPE)\
do {\
gs_dyn_array_push(gsi->vattributes, TYPE);\
} while (0)
uint32_t ct = sz / sizeof(gs_asset_mesh_layout_t);
for (uint32_t i = 0; i < ct; ++i)
{
gs_asset_mesh_attribute_type type = layout[i].type;
switch (type)
{
default:
case GS_ASSET_MESH_ATTRIBUTE_TYPE_POSITION: VATTR_PUSH(GSI_VATTR_POSITION); break;
case GS_ASSET_MESH_ATTRIBUTE_TYPE_TEXCOORD: VATTR_PUSH(GSI_VATTR_UV); break;
case GS_ASSET_MESH_ATTRIBUTE_TYPE_COLOR: VATTR_PUSH(GSI_VATTR_COLOR); break;
}
}
}
GS_API_DECL void gsi_defaults(gs_immediate_draw_t* gsi)
{
gsi_flush(gsi);
// Set defaults for cache
gsi->cache.texture = GSI()->tex_default;
gsi->cache.pipeline = gsi_pipeline_state_default();
gsi->cache.pipeline.prim_type = 0x00;
gsi->cache.uv = gs_v2(0.f, 0.f);
gsi->cache.color = GS_COLOR_WHITE;
gs_dyn_array_clear(gsi->vattributes);
// Reset flags
gsi->flags = 0x00;
gs_immediate_draw_set_pipeline(gsi);
}
GS_API_DECL void gsi_tc2fv(gs_immediate_draw_t* gsi, gs_vec2 uv)
{
// Set cache register
gsi->cache.uv = uv;
}
void gsi_tc2f(gs_immediate_draw_t* gsi, float u, float v)
{
// Set cache register
gsi_tc2fv(gsi, gs_v2(u, v));
}
void gsi_c4ub(gs_immediate_draw_t* gsi, uint8_t r, uint8_t g, uint8_t b, uint8_t a)
{
// Set cache color
gsi->cache.color = gs_color(r, g, b, a);
}
GS_API_DECL void gsi_c4ubv(gs_immediate_draw_t* gsi, gs_color_t c)
{
gsi_c4ub(gsi, c.r, c.g, c.b, c.a);
}
void gsi_v3fv(gs_immediate_draw_t* gsi, gs_vec3 p)
{
if (gs_dyn_array_size(gsi->vattributes))
{
// Iterate through attributes and push into stream
for (uint32_t i = 0; i < gs_dyn_array_size(gsi->vattributes); ++i)
{
gsi_vattr_type type = gsi->vattributes[i];
switch (type)
{
default:
{
} break;
case GSI_VATTR_POSITION:
{
gs_byte_buffer_write(&gsi->vertices, gs_vec3, p);
} break;
case GSI_VATTR_COLOR:
{
gs_byte_buffer_write(&gsi->vertices, gs_color_t, gsi->cache.color);
} break;
case GSI_VATTR_UV:
{
gs_byte_buffer_write(&gsi->vertices, gs_vec2, gsi->cache.uv);
} break;
}
}
}
else
{
gs_immediate_vert_t v = gs_default_val();
v.position = p;
v.uv = gsi->cache.uv;
v.color = gsi->cache.color;
gs_byte_buffer_write(&gsi->vertices, gs_immediate_vert_t, v);
}
}
void gsi_v3f(gs_immediate_draw_t* gsi, float x, float y, float z)
{
// Push vert
gsi_v3fv(gsi, gs_v3(x, y, z));
}
void gsi_v2f(gs_immediate_draw_t* gsi, float x, float y)
{
// Push vert
gsi_v3f(gsi, x, y, 0.f);
}
void gsi_v2fv(gs_immediate_draw_t* gsi, gs_vec2 v)
{
// Push vert
gsi_v3f(gsi, v.x, v.y, 0.f);
}
void gsi_push_matrix_ex(gs_immediate_draw_t* gsi, gsi_matrix_type type, bool flush)
{
// Flush
if (flush)
{
gsi_flush(gsi);
}
// Push mode
gs_dyn_array_push(gsi->cache.modes, type);
// Pop matrix off of stack
switch (gs_dyn_array_back(gsi->cache.modes))
{
case GSI_MATRIX_MODELVIEW:
{
gs_dyn_array_push(gsi->cache.modelview, gs_dyn_array_back(gsi->cache.modelview));
} break;
case GSI_MATRIX_PROJECTION:
{
gs_dyn_array_push(gsi->cache.projection, gs_dyn_array_back(gsi->cache.projection));
} break;
}
}
void gsi_push_matrix(gs_immediate_draw_t* gsi, gsi_matrix_type type)
{
gsi_push_matrix_ex(gsi, type, true);
}
void gsi_pop_matrix_ex(gs_immediate_draw_t* gsi, bool flush)
{
// Flush
if (flush)
{
gsi_flush(gsi);
}
// Pop matrix off of stack
switch (gs_dyn_array_back(gsi->cache.modes))
{
case GSI_MATRIX_MODELVIEW: {
if (gs_dyn_array_size(gsi->cache.modelview) > 1) {
gs_dyn_array_pop(gsi->cache.modelview);