-
-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Expand file tree
/
Copy pathrlsw.h
More file actions
6108 lines (5220 loc) · 220 KB
/
Copy pathrlsw.h
File metadata and controls
6108 lines (5220 loc) · 220 KB
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
/**********************************************************************************************
*
* rlsw v1.5 - An OpenGL 1.1-style software renderer implementation
*
* DESCRIPTION:
* rlsw is a custom OpenGL 1.1-style implementation on software, intended to provide all
* functionality available on rlgl.h library used by raylib, becoming a direct software
* rendering replacement for OpenGL 1.1 backend and allowing to run raylib on GPU-less
* devices when required
*
* FEATURES:
* - Rendering to custom internal framebuffer with multiple color modes supported:
* - Color buffer: RGB - 8-bit (3:3:2) | RGB - 16-bit (5:6:5) | RGB - 24-bit (8:8:8)
* - Depth buffer: D - 8-bit (unorm) | D - 16-bit (unorm) | D - 24-bit (unorm)
* - Rendering modes supported: POINT, LINES, TRIANGLE, QUADS
* - Additional features: Polygon modes, Point width, Line width
* - Clipping support for all rendering modes
* - Texture features supported:
* - All uncompressed texture formats supported by raylib
* - Texture Minification/Magnification checks
* - Point and Bilinear filtering
* - Texture Wrap Modes with separate checks for S/T coordinates
* - Vertex Arrays support with direct primitive drawing mode
* - Matrix Stack support (Matrix Push/Pop)
* - Other GL misc features:
* - GL-style getter functions
* - Framebuffer resizing
* - Perspective correction
* - Scissor clipping
* - Depth testing
* - Blend modes
* - Face culling
*
* ADDITIONAL NOTES:
* Check PR for more info: https://github.com/raysan5/raylib/pull/4832
*
* CONFIGURATION:
* #define RLSW_IMPLEMENTATION
* Generates the implementation of the library into the included file
* If not defined, the library is in header only mode and can be included in other headers
* or source files without problems. But only ONE file should hold the implementation
*
* #define RLSW_USE_SIMD_INTRINSICS
* Detect and use SIMD intrinsics on the host compilation platform
* SIMD could improve rendering considerable vectorizing some raster operations
* but the target platforms running the compiled program with SIMD enabled
* must support the SIMD the program has been built for, making them only
* recommended under specific situations and only if the developers know
* what are they doing; this flag is not defined by default
*
* rlsw capabilities could be customized defining some internal
* values before library inclusion (default values listed):
*
* #define SW_FRAMEBUFFER_OUTPUT_BGRA true
* #define SW_FRAMEBUFFER_COLOR_TYPE R8G8B8A8
* #define SW_FRAMEBUFFER_DEPTH_TYPE D32
* #define SW_MAX_PROJECTION_STACK_SIZE 2
* #define SW_MAX_MODELVIEW_STACK_SIZE 8
* #define SW_MAX_TEXTURE_STACK_SIZE 2
* #define SW_MAX_TEXTURES 128
*
*
* LICENSE: MIT
*
* Copyright (c) 2025-2026 Le Juez Victor (@Bigfoot71), reviewed by Ramon Santamaria (@raysan5)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
**********************************************************************************************/
#ifndef RLSW_H
#define RLSW_H
#define RLSW_VERSION "1.5"
#include <stdbool.h>
#include <stdint.h>
//----------------------------------------------------------------------------------
// Defines and Macros
//----------------------------------------------------------------------------------
// Function specifiers definition
#ifndef SWAPI
#define SWAPI // Functions defined as 'extern' by default (implicit specifiers)
#endif
#ifndef SW_MALLOC
#define SW_MALLOC(sz) malloc(sz)
#endif
#ifndef SW_CALLOC
#define SW_CALLOC(n,sz) calloc(n,sz)
#endif
#ifndef SW_REALLOC
#define SW_REALLOC(ptr, newSz) realloc(ptr, newSz)
#endif
#ifndef SW_FREE
#define SW_FREE(ptr) free(ptr)
#endif
#ifndef SW_RESTRICT
#ifdef _MSC_VER
#define SW_RESTRICT __restrict
#else
#define SW_RESTRICT restrict
#endif
#endif
#ifndef SW_FRAMEBUFFER_OUTPUT_BGRA
#define SW_FRAMEBUFFER_OUTPUT_BGRA true
#endif
#ifndef SW_FRAMEBUFFER_COLOR_TYPE
#define SW_FRAMEBUFFER_COLOR_TYPE R8G8B8A8 // Or R5G6B5, R3G3B2, etc; see `sw_pixelformat_t`
#endif
#ifndef SW_FRAMEBUFFER_DEPTH_TYPE
#define SW_FRAMEBUFFER_DEPTH_TYPE D32 // Or D16, D8
#endif
#ifndef SW_MAX_PROJECTION_STACK_SIZE
#define SW_MAX_PROJECTION_STACK_SIZE 2
#endif
#ifndef SW_MAX_MODELVIEW_STACK_SIZE
#define SW_MAX_MODELVIEW_STACK_SIZE 8
#endif
#ifndef SW_MAX_TEXTURE_STACK_SIZE
#define SW_MAX_TEXTURE_STACK_SIZE 2
#endif
#ifndef SW_MAX_FRAMEBUFFERS
#define SW_MAX_FRAMEBUFFERS 8
#endif
#ifndef SW_MAX_TEXTURES
#define SW_MAX_TEXTURES 128
#endif
// Enable the use of a lookup table for uint8_t to float conversion
// Requires an additional 1KB of global memory
// Disabled when SIMD intrinsics are enabled
#ifndef SW_USE_COLOR_LUT
#if RLSW_USE_SIMD_INTRINSICS
#define SW_USE_COLOR_LUT false
#else
#define SW_USE_COLOR_LUT true
#endif
#endif
// Full NPOT texture support (enabled by default)
// When disabled, SW_REPEAT requires POT on its axis (fast bitmask wrap)
// SW_CLAMP remains supported for any dimension, per-axis
#define SW_SUPPORT_NPOT_TEXTURE true
//----------------------------------------------------------------------------------
// OpenGL Compatibility Types
//----------------------------------------------------------------------------------
typedef unsigned int GLenum;
typedef unsigned char GLboolean;
typedef unsigned int GLbitfield;
typedef void GLvoid;
typedef signed char GLbyte;
typedef short GLshort;
typedef int GLint;
typedef unsigned char GLubyte;
typedef unsigned short GLushort;
typedef unsigned int GLuint;
typedef int GLsizei;
typedef float GLfloat;
typedef float GLclampf;
typedef double GLdouble;
typedef double GLclampd;
//----------------------------------------------------------------------------------
// OpenGL Definitions
// NOTE: Not used/supported definitions are commented
//----------------------------------------------------------------------------------
#define GL_FALSE 0
#define GL_TRUE 1
#define GL_SCISSOR_TEST 0x0C11
#define GL_TEXTURE_2D 0x0DE1
#define GL_DEPTH_TEST 0x0B71
#define GL_CULL_FACE 0x0B44
#define GL_BLEND 0x0BE2
#define GL_VENDOR 0x1F00
#define GL_RENDERER 0x1F01
#define GL_VERSION 0x1F02
#define GL_EXTENSIONS 0x1F03
#define GL_SHADING_LANGUAGE_VERSION 0x8B8C
//#define GL_ATTRIB_STACK_DEPTH 0x0BB0
//#define GL_CLIENT_ATTRIB_STACK_DEPTH 0x0BB1
#define GL_COLOR_CLEAR_VALUE 0x0C22
#define GL_DEPTH_CLEAR_VALUE 0x0B73
//#define GL_COLOR_WRITEMASK 0x0C23
//#define GL_CURRENT_INDEX 0x0B01
#define GL_CURRENT_COLOR 0x0B00
//#define GL_CURRENT_NORMAL 0x0B02
//#define GL_CURRENT_RASTER_COLOR 0x0B04
//#define GL_CURRENT_RASTER_DISTANCE 0x0B09
//#define GL_CURRENT_RASTER_INDEX 0x0B05
//#define GL_CURRENT_RASTER_POSITION 0x0B07
//#define GL_CURRENT_RASTER_TEXTURE_COORDS 0x0B06
//#define GL_CURRENT_RASTER_POSITION_VALID 0x0B08
#define GL_CURRENT_TEXTURE_COORDS 0x0B03
#define GL_POINT_SIZE 0x0B11
#define GL_LINE_WIDTH 0x0B21
//#define GL_INDEX_CLEAR_VALUE 0x0C20
//#define GL_INDEX_MODE 0x0C30
//#define GL_INDEX_WRITEMASK 0x0C21
#define GL_MODELVIEW_MATRIX 0x0BA6
#define GL_MODELVIEW_STACK_DEPTH 0x0BA3
//#define GL_NAME_STACK_DEPTH 0x0D70
#define GL_PROJECTION_MATRIX 0x0BA7
#define GL_PROJECTION_STACK_DEPTH 0x0BA4
//#define GL_RENDER_MODE 0x0C40
//#define GL_RGBA_MODE 0x0C31
#define GL_TEXTURE_MATRIX 0x0BA8
#define GL_TEXTURE_STACK_DEPTH 0x0BA5
#define GL_VIEWPORT 0x0BA2
#define GL_COLOR_BUFFER_BIT 0x00004000
#define GL_DEPTH_BUFFER_BIT 0x00000100
#define GL_MODELVIEW 0x1700
#define GL_PROJECTION 0x1701
#define GL_TEXTURE 0x1702
#define GL_VERTEX_ARRAY 0x8074
#define GL_NORMAL_ARRAY 0x8075 // WARNING: Not implemented (defined for RLGL)
#define GL_COLOR_ARRAY 0x8076
//#define GL_INDEX_ARRAY 0x8077
#define GL_TEXTURE_COORD_ARRAY 0x8078
#define GL_POINTS 0x0000
#define GL_LINES 0x0001
//#define GL_LINE_LOOP 0x0002
//#define GL_LINE_STRIP 0x0003
#define GL_TRIANGLES 0x0004
//#define GL_TRIANGLE_STRIP 0x0005
//#define GL_TRIANGLE_FAN 0x0006
#define GL_QUADS 0x0007
//#define GL_QUAD_STRIP 0x0008
//#define GL_POLYGON 0x0009
#define GL_POINT 0x1B00
#define GL_LINE 0x1B01
#define GL_FILL 0x1B02
#define GL_FRONT 0x0404
#define GL_BACK 0x0405
#define GL_ZERO 0
#define GL_ONE 1
#define GL_SRC_COLOR 0x0300
#define GL_ONE_MINUS_SRC_COLOR 0x0301
#define GL_SRC_ALPHA 0x0302
#define GL_ONE_MINUS_SRC_ALPHA 0x0303
#define GL_DST_ALPHA 0x0304
#define GL_ONE_MINUS_DST_ALPHA 0x0305
#define GL_DST_COLOR 0x0306
#define GL_ONE_MINUS_DST_COLOR 0x0307
#define GL_SRC_ALPHA_SATURATE 0x0308
#define GL_NEAREST 0x2600
#define GL_LINEAR 0x2601
#define GL_REPEAT 0x2901
#define GL_CLAMP 0x2900
#define GL_TEXTURE_MAG_FILTER 0x2800
#define GL_TEXTURE_MIN_FILTER 0x2801
#define GL_TEXTURE_WRAP_S 0x2802
#define GL_TEXTURE_WRAP_T 0x2803
#define GL_NO_ERROR 0
#define GL_INVALID_ENUM 0x0500
#define GL_INVALID_VALUE 0x0501
#define GL_INVALID_OPERATION 0x0502
#define GL_STACK_OVERFLOW 0x0503
#define GL_STACK_UNDERFLOW 0x0504
#define GL_OUT_OF_MEMORY 0x0505
#define GL_ALPHA 0x1906
#define GL_LUMINANCE 0x1909
#define GL_LUMINANCE_ALPHA 0x190A
#define GL_RGB 0x1907
#define GL_RGBA 0x1908
#define GL_DEPTH_COMPONENT 0x1902
#define GL_BYTE 0x1400
#define GL_UNSIGNED_BYTE 0x1401
#define GL_SHORT 0x1402
#define GL_UNSIGNED_SHORT 0x1403
#define GL_INT 0x1404
#define GL_UNSIGNED_INT 0x1405
#define GL_FLOAT 0x1406
#define GL_UNSIGNED_BYTE_3_3_2 0x8032
#define GL_UNSIGNED_SHORT_5_6_5 0x8363
#define GL_UNSIGNED_SHORT_4_4_4_4 0x8033
#define GL_UNSIGNED_SHORT_5_5_5_1 0x8034
// OpenGL internal formats (not all are supported; see SWinternalformat)
#define GL_ALPHA4 0x803B
#define GL_ALPHA8 0x803C
#define GL_ALPHA12 0x803D
#define GL_ALPHA16 0x803E
#define GL_LUMINANCE4 0x803F
#define GL_LUMINANCE8 0x8040
#define GL_LUMINANCE12 0x8041
#define GL_LUMINANCE16 0x8042
#define GL_LUMINANCE4_ALPHA4 0x8043
#define GL_LUMINANCE6_ALPHA2 0x8044
#define GL_LUMINANCE8_ALPHA8 0x8045
#define GL_LUMINANCE12_ALPHA4 0x8046
#define GL_LUMINANCE12_ALPHA12 0x8047
#define GL_LUMINANCE16_ALPHA16 0x8048
#define GL_INTENSITY 0x8049
#define GL_INTENSITY4 0x804A
#define GL_INTENSITY8 0x804B
#define GL_INTENSITY12 0x804C
#define GL_INTENSITY16 0x804D
#define GL_R3_G3_B2 0x2A10
#define GL_RGB4 0x804F
#define GL_RGB5 0x8050
#define GL_RGB8 0x8051
#define GL_RGB10 0x8052
#define GL_RGB12 0x8053
#define GL_RGB16 0x8054
#define GL_RGBA2 0x8055
#define GL_RGBA4 0x8056
#define GL_RGB5_A1 0x8057
#define GL_RGBA8 0x8058
#define GL_RGB10_A2 0x8059
#define GL_RGBA12 0x805A
#define GL_RGBA16 0x805B
// OpenGL internal formats extension
#define GL_DEPTH_COMPONENT16 0x81A5
#define GL_DEPTH_COMPONENT24 0x81A6
#define GL_DEPTH_COMPONENT32 0x81A7
#define GL_DEPTH_COMPONENT32F 0x8CAC
#define GL_R16F 0x822D
#define GL_RGB16F 0x881B
#define GL_RGBA16F 0x881A
#define GL_R32F 0x822E
#define GL_RGB32F 0x8815
#define GL_RGBA32F 0x8814
// OpenGL GL_EXT_framebuffer_object
#define GL_DRAW_FRAMEBUFFER_BINDING 0x8CA6
#define GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE 0x8CD0
#define GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME 0x8CD1
#define GL_FRAMEBUFFER_COMPLETE 0x8CD5
#define GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT 0x8CD6
#define GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT 0x8CD7
#define GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS 0x8CD9
#define GL_FRAMEBUFFER_UNSUPPORTED 0x8CDD
#define GL_COLOR_ATTACHMENT0 0x8CE0
#define GL_DEPTH_ATTACHMENT 0x8D00
#define GL_STENCIL_ATTACHMENT 0x8D20 // WARNING: Not implemented (defined for RLGL)
// OpenGL Definitions NOT USED
#define GL_PERSPECTIVE_CORRECTION_HINT 0x0C50
#define GL_PACK_ALIGNMENT 0x0D05
#define GL_UNPACK_ALIGNMENT 0x0CF5
#define GL_LINE_SMOOTH 0x0B20
#define GL_SMOOTH 0x1D01
#define GL_NICEST 0x1102
#define GL_CCW 0x0901
#define GL_CW 0x0900
#define GL_NEVER 0x0200
#define GL_LESS 0x0201
#define GL_EQUAL 0x0202
#define GL_LEQUAL 0x0203
#define GL_GREATER 0x0204
#define GL_NOTEQUAL 0x0205
#define GL_GEQUAL 0x0206
#define GL_ALWAYS 0x0207
#define GL_RENDERBUFFER 0x8D41
#define GL_TEXTURE_CUBE_MAP_NEGATIVE_X 0x8516
#define GL_TEXTURE_CUBE_MAP_NEGATIVE_Y 0x8518
#define GL_TEXTURE_CUBE_MAP_NEGATIVE_Z 0x851A
#define GL_TEXTURE_CUBE_MAP_POSITIVE_X 0x8515
#define GL_TEXTURE_CUBE_MAP_POSITIVE_Y 0x8517
#define GL_TEXTURE_CUBE_MAP_POSITIVE_Z 0x8519
//----------------------------------------------------------------------------------
// OpenGL Bindings to rlsw
//----------------------------------------------------------------------------------
#define glReadPixels(x, y, w, h, f, t, p) swReadPixels((x), (y), (w), (h), (f), (t), (p))
#define glEnable(state) swEnable((state))
#define glDisable(state) swDisable((state))
#define glGetIntegerv(pname, params) swGetIntegerv((pname), (params))
#define glGetFloatv(pname, params) swGetFloatv((pname), (params))
#define glGetString(pname) swGetString((pname))
#define glGetError() swGetError()
#define glViewport(x, y, w, h) swViewport((x), (y), (w), (h))
#define glScissor(x, y, w, h) swScissor((x), (y), (w), (h))
#define glClearColor(r, g, b, a) swClearColor((r), (g), (b), (a))
#define glClearDepth(d) swClearDepth((d))
#define glClear(bitmask) swClear((bitmask))
#define glBlendFunc(sfactor, dfactor) swBlendFunc((sfactor), (dfactor))
#define glPolygonMode(face, mode) swPolygonMode((mode))
#define glCullFace(face) swCullFace((face))
#define glPointSize(size) swPointSize((size))
#define glLineWidth(width) swLineWidth((width))
#define glMatrixMode(mode) swMatrixMode((mode))
#define glPushMatrix() swPushMatrix()
#define glPopMatrix() swPopMatrix()
#define glLoadIdentity() swLoadIdentity()
#define glTranslatef(x, y, z) swTranslatef((x), (y), (z))
#define glRotatef(a, x, y, z) swRotatef((a), (x), (y), (z))
#define glScalef(x, y, z) swScalef((x), (y), (z))
#define glMultMatrixf(v) swMultMatrixf((v))
#define glFrustum(l, r, b, t, n, f) swFrustum((l), (r), (b), (t), (n), (f))
#define glOrtho(l, r, b, t, n, f) swOrtho((l), (r), (b), (t), (n), (f))
#define glBegin(mode) swBegin((mode))
#define glEnd() swEnd()
#define glVertex2i(x, y) swVertex2i((x), (y))
#define glVertex2f(x, y) swVertex2f((x), (y))
#define glVertex2fv(v) swVertex2fv((v))
#define glVertex3i(x, y, z) swVertex3i((x), (y), (z))
#define glVertex3f(x, y, z) swVertex3f((x), (y), (z))
#define glvertex3fv(v) swVertex3fv((v))
#define glVertex4i(x, y, z, w) swVertex4i((x), (y), (z), (w))
#define glVertex4f(x, y, z, w) swVertex4f((x), (y), (z), (w))
#define glVertex4fv(v) swVertex4fv((v))
#define glColor3ub(r, g, b) swColor3ub((r), (g), (b))
#define glColor3ubv(v) swColor3ubv((v))
#define glColor3f(r, g, b) swColor3f((r), (g), (b))
#define glColor3fv(v) swColor3fv((v))
#define glColor4ub(r, g, b, a) swColor4ub((r), (g), (b), (a))
#define glColor4ubv(v) swColor4ubv((v))
#define glColor4f(r, g, b, a) swColor4f((r), (g), (b), (a))
#define glColor4fv(v) swColor4fv((v))
#define glTexCoord2f(u, v) swTexCoord2f((u), (v))
#define glTexCoord2fv(v) swTexCoord2fv((v))
#define glEnableClientState(t) ((void)(t))
#define glDisableClientState(t) swBindArray((t), 0)
#define glVertexPointer(sz, t, s, p) swBindArray(SW_VERTEX_ARRAY, (p))
#define glTexCoordPointer(sz, t, s, p) swBindArray(SW_TEXTURE_COORD_ARRAY, (p))
#define glColorPointer(sz, t, s, p) swBindArray(SW_COLOR_ARRAY, (p))
#define glDrawArrays(m, o, c) swDrawArrays((m), (o), (c))
#define glDrawElements(m,c,t,i) swDrawElements((m),(c),(t),(i))
#define glGenTextures(c, v) swGenTextures((c), (v))
#define glDeleteTextures(c, v) swDeleteTextures((c), (v))
#define glBindTexture(tr, id) swBindTexture((id))
#define glTexImage2D(tr, l, if, w, h, b, f, t, p) swTexImage2D((w), (h), (f), (t), (p))
#define glTexSubImage2D(tr, l, x, y, w, h, f, t, p) swTexSubImage2D((x), (y), (w), (h), (f), (t), (p));
#define glTexParameteri(tr, pname, param) swTexParameteri((pname), (param))
// OpenGL GL_EXT_framebuffer_object
#define glGenFramebuffers(c, v) swGenFramebuffers((c), (v))
#define glDeleteFramebuffers(c, v) swDeleteFramebuffers((c), (v))
#define glBindFramebuffer(tr, id) swBindFramebuffer((id))
#define glFramebufferTexture2D(tr, a, t, tex, ml) swFramebufferTexture2D((a), (tex))
#define glFramebufferRenderbuffer(tr, a, rbtr, rb) swFramebufferTexture2D((a), (rb))
#define glCheckFramebufferStatus(tr) swCheckFramebufferStatus()
#define glGetFramebufferAttachmentParameteriv(tr, a, p, v) swGetFramebufferAttachmentParameteriv((a), (p), (v))
#define glGenRenderbuffers(c, v) swGenTextures((c), (v))
#define glDeleteRenderbuffers(c, v) swDeleteTextures((c), (v))
#define glBindRenderbuffer(tr, rb) swBindTexture((rb))
#define glRenderbufferStorage(tr, f, w, h) swTexStorage2D((w), (h), (f))
// OpenGL functions NOT IMPLEMENTED by rlsw
#define glDepthMask(X) ((void)(X))
#define glColorMask(X,Y,Z,W) ((void)(X),(void)(Y),(void)(Z),(void)(W))
#define glPixelStorei(X,Y) ((void)(X),(void)(Y))
#define glHint(X,Y) ((void)(X),(void)(Y))
#define glShadeModel(X) ((void)(X))
#define glFrontFace(X) ((void)(X))
#define glDepthFunc(X) ((void)(X))
#define glGetTexImage(X,Y,Z,W,A) ((void)(X),(void)(Y),(void)(Z),(void)(W),(void)(A))
#define glNormal3f(X,Y,Z) ((void)(X),(void)(Y),(void)(Z))
#define glNormal3fv(X) ((void)(X))
#define glNormalPointer(X,Y,Z) ((void)(X),(void)(Y),(void)(Z))
//----------------------------------------------------------------------------------
// Types and Structures Definition
//----------------------------------------------------------------------------------
typedef enum {
SW_SCISSOR_TEST = GL_SCISSOR_TEST,
SW_TEXTURE_2D = GL_TEXTURE_2D,
SW_DEPTH_TEST = GL_DEPTH_TEST,
SW_CULL_FACE = GL_CULL_FACE,
SW_BLEND = GL_BLEND
} SWstate;
typedef enum {
SW_VENDOR = GL_VENDOR,
SW_RENDERER = GL_RENDERER,
SW_VERSION = GL_VERSION,
SW_EXTENSIONS = GL_EXTENSIONS,
SW_SHADING_LANGUAGE_VERSION = GL_SHADING_LANGUAGE_VERSION,
SW_COLOR_CLEAR_VALUE = GL_COLOR_CLEAR_VALUE,
SW_DEPTH_CLEAR_VALUE = GL_DEPTH_CLEAR_VALUE,
SW_CURRENT_COLOR = GL_CURRENT_COLOR,
SW_CURRENT_TEXTURE_COORDS = GL_CURRENT_TEXTURE_COORDS,
SW_POINT_SIZE = GL_POINT_SIZE,
SW_LINE_WIDTH = GL_LINE_WIDTH,
SW_MODELVIEW_MATRIX = GL_MODELVIEW_MATRIX,
SW_MODELVIEW_STACK_DEPTH = GL_MODELVIEW_STACK_DEPTH,
SW_PROJECTION_MATRIX = GL_PROJECTION_MATRIX,
SW_PROJECTION_STACK_DEPTH = GL_PROJECTION_STACK_DEPTH,
SW_TEXTURE_MATRIX = GL_TEXTURE_MATRIX,
SW_TEXTURE_STACK_DEPTH = GL_TEXTURE_STACK_DEPTH,
SW_VIEWPORT = GL_VIEWPORT,
SW_DRAW_FRAMEBUFFER_BINDING = GL_DRAW_FRAMEBUFFER_BINDING,
} SWget;
typedef enum {
SW_COLOR_BUFFER_BIT = GL_COLOR_BUFFER_BIT,
SW_DEPTH_BUFFER_BIT = GL_DEPTH_BUFFER_BIT
} SWbuffer;
typedef enum {
SW_PROJECTION = GL_PROJECTION,
SW_MODELVIEW = GL_MODELVIEW,
SW_TEXTURE = GL_TEXTURE
} SWmatrix;
typedef enum {
SW_VERTEX_ARRAY = GL_VERTEX_ARRAY,
SW_TEXTURE_COORD_ARRAY = GL_TEXTURE_COORD_ARRAY,
SW_COLOR_ARRAY = GL_COLOR_ARRAY
} SWarray;
typedef enum {
SW_DRAW_INVALID = -1,
SW_POINTS = GL_POINTS,
SW_LINES = GL_LINES,
SW_TRIANGLES = GL_TRIANGLES,
SW_QUADS = GL_QUADS
} SWdraw;
typedef enum {
SW_POINT = GL_POINT,
SW_LINE = GL_LINE,
SW_FILL = GL_FILL
} SWpoly;
typedef enum {
SW_FRONT = GL_FRONT,
SW_BACK = GL_BACK,
} SWface;
typedef enum {
SW_ZERO = GL_ZERO,
SW_ONE = GL_ONE,
SW_SRC_COLOR = GL_SRC_COLOR,
SW_ONE_MINUS_SRC_COLOR = GL_ONE_MINUS_SRC_COLOR,
SW_SRC_ALPHA = GL_SRC_ALPHA,
SW_ONE_MINUS_SRC_ALPHA = GL_ONE_MINUS_SRC_ALPHA,
SW_DST_ALPHA = GL_DST_ALPHA,
SW_ONE_MINUS_DST_ALPHA = GL_ONE_MINUS_DST_ALPHA,
SW_DST_COLOR = GL_DST_COLOR,
SW_ONE_MINUS_DST_COLOR = GL_ONE_MINUS_DST_COLOR,
SW_SRC_ALPHA_SATURATE = GL_SRC_ALPHA_SATURATE
} SWfactor;
typedef enum {
SW_LUMINANCE = GL_LUMINANCE,
SW_LUMINANCE_ALPHA = GL_LUMINANCE_ALPHA,
SW_RGB = GL_RGB,
SW_RGBA = GL_RGBA,
SW_DEPTH_COMPONENT = GL_DEPTH_COMPONENT,
} SWformat;
typedef enum {
SW_UNSIGNED_BYTE = GL_UNSIGNED_BYTE,
SW_UNSIGNED_BYTE_3_3_2 = GL_UNSIGNED_BYTE_3_3_2,
SW_BYTE = GL_BYTE,
SW_UNSIGNED_SHORT = GL_UNSIGNED_SHORT,
SW_UNSIGNED_SHORT_5_6_5 = GL_UNSIGNED_SHORT_5_6_5,
SW_UNSIGNED_SHORT_4_4_4_4 = GL_UNSIGNED_SHORT_4_4_4_4,
SW_UNSIGNED_SHORT_5_5_5_1 = GL_UNSIGNED_SHORT_5_5_5_1,
SW_SHORT = GL_SHORT,
SW_UNSIGNED_INT = GL_UNSIGNED_INT,
SW_INT = GL_INT,
SW_FLOAT = GL_FLOAT
} SWtype;
typedef enum {
SW_LUMINANCE8 = GL_LUMINANCE8,
SW_LUMINANCE8_ALPHA8 = GL_LUMINANCE8_ALPHA8,
SW_R3_G3_B2 = GL_R3_G3_B2,
SW_RGB8 = GL_RGB8,
SW_RGBA4 = GL_RGBA4,
SW_RGB5_A1 = GL_RGB5_A1,
SW_RGBA8 = GL_RGBA8,
SW_R16F = GL_R16F,
SW_RGB16F = GL_RGB16F,
SW_RGBA16F = GL_RGBA16F,
SW_R32F = GL_R32F,
SW_RGB32F = GL_RGB32F,
SW_RGBA32F = GL_RGBA32F,
SW_DEPTH_COMPONENT16 = GL_DEPTH_COMPONENT16,
SW_DEPTH_COMPONENT24 = GL_DEPTH_COMPONENT24,
SW_DEPTH_COMPONENT32 = GL_DEPTH_COMPONENT32,
SW_DEPTH_COMPONENT32F = GL_DEPTH_COMPONENT32F,
//SW_R5_G6_B5, // Not defined by OpenGL
} SWinternalformat;
typedef enum {
SW_NEAREST = GL_NEAREST,
SW_LINEAR = GL_LINEAR
} SWfilter;
typedef enum {
SW_REPEAT = GL_REPEAT,
SW_CLAMP = GL_CLAMP,
} SWwrap;
typedef enum {
SW_TEXTURE_MIN_FILTER = GL_TEXTURE_MIN_FILTER,
SW_TEXTURE_MAG_FILTER = GL_TEXTURE_MAG_FILTER,
SW_TEXTURE_WRAP_S = GL_TEXTURE_WRAP_S,
SW_TEXTURE_WRAP_T = GL_TEXTURE_WRAP_T
} SWtexparam;
typedef enum {
SW_COLOR_ATTACHMENT = GL_COLOR_ATTACHMENT0,
SW_DEPTH_ATTACHMENT = GL_DEPTH_ATTACHMENT,
} SWattachment;
typedef enum {
SW_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME = GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME,
SW_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE = GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE,
} SWattachget;
typedef enum {
SW_FRAMEBUFFER_COMPLETE = GL_FRAMEBUFFER_COMPLETE,
SW_FRAMEBUFFER_INCOMPLETE_ATTACHMENT = GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT,
SW_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT = GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT,
SW_FRAMEBUFFER_INCOMPLETE_DIMENSIONS = GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS,
} SWfbstatus;
typedef enum {
SW_NO_ERROR = GL_NO_ERROR,
SW_INVALID_ENUM = GL_INVALID_ENUM,
SW_INVALID_VALUE = GL_INVALID_VALUE,
SW_STACK_OVERFLOW = GL_STACK_OVERFLOW,
SW_STACK_UNDERFLOW = GL_STACK_UNDERFLOW,
SW_INVALID_OPERATION = GL_INVALID_OPERATION,
SW_OUT_OF_MEMORY = GL_OUT_OF_MEMORY,
} SWerrcode;
//------------------------------------------------------------------------------------
// Functions Declaration - Public API
//------------------------------------------------------------------------------------
SWAPI bool swInit(int w, int h);
SWAPI void swClose(void);
SWAPI bool swResize(int w, int h);
SWAPI void swReadPixels(int x, int y, int w, int h, SWformat format, SWtype type, void *pixels);
SWAPI void swBlitPixels(int xDst, int yDst, int wDst, int hDst, int xSrc, int ySrc, int wSrc, int hSrc, SWformat format, SWtype type, void *pixels);
SWAPI void *swGetColorBuffer(int *width, int *height); // Restored for ESP-IDF compatibility
SWAPI void swEnable(SWstate state);
SWAPI void swDisable(SWstate state);
SWAPI void swGetFloatv(SWget name, float *v);
SWAPI const char *swGetString(SWget name);
SWAPI SWerrcode swGetError(void);
SWAPI void swViewport(int x, int y, int width, int height);
SWAPI void swScissor(int x, int y, int width, int height);
SWAPI void swClearColor(float r, float g, float b, float a);
SWAPI void swClearDepth(float depth);
SWAPI void swClear(uint32_t bitmask);
SWAPI void swBlendFunc(SWfactor sfactor, SWfactor dfactor);
SWAPI void swPolygonMode(SWpoly mode);
SWAPI void swCullFace(SWface face);
SWAPI void swPointSize(float size);
SWAPI void swLineWidth(float width);
SWAPI void swMatrixMode(SWmatrix mode);
SWAPI void swPushMatrix(void);
SWAPI void swPopMatrix(void);
SWAPI void swLoadIdentity(void);
SWAPI void swTranslatef(float x, float y, float z);
SWAPI void swRotatef(float angle, float x, float y, float z);
SWAPI void swScalef(float x, float y, float z);
SWAPI void swMultMatrixf(const float *mat);
SWAPI void swFrustum(double left, double right, double bottom, double top, double znear, double zfar);
SWAPI void swOrtho(double left, double right, double bottom, double top, double znear, double zfar);
SWAPI void swBegin(SWdraw mode);
SWAPI void swEnd(void);
SWAPI void swVertex2i(int x, int y);
SWAPI void swVertex2f(float x, float y);
SWAPI void swVertex2fv(const float *v);
SWAPI void swVertex3i(int x, int y, int z);
SWAPI void swVertex3f(float x, float y, float z);
SWAPI void swVertex3fv(const float *v);
SWAPI void swVertex4i(int x, int y, int z, int w);
SWAPI void swVertex4f(float x, float y, float z, float w);
SWAPI void swVertex4fv(const float *v);
SWAPI void swColor3ub(uint8_t r, uint8_t g, uint8_t b);
SWAPI void swColor3ubv(const uint8_t *v);
SWAPI void swColor3f(float r, float g, float b);
SWAPI void swColor3fv(const float *v);
SWAPI void swColor4ub(uint8_t r, uint8_t g, uint8_t b, uint8_t a);
SWAPI void swColor4ubv(const uint8_t *v);
SWAPI void swColor4f(float r, float g, float b, float a);
SWAPI void swColor4fv(const float *v);
SWAPI void swTexCoord2f(float u, float v);
SWAPI void swTexCoord2fv(const float *v);
SWAPI void swBindArray(SWarray type, void *buffer);
SWAPI void swDrawArrays(SWdraw mode, int offset, int count);
SWAPI void swDrawElements(SWdraw mode, int count, int type, const void *indices);
SWAPI void swGenTextures(int count, uint32_t *textures);
SWAPI void swDeleteTextures(int count, uint32_t *textures);
SWAPI void swBindTexture(uint32_t id);
SWAPI void swTexImage2D(int width, int height, SWformat format, SWtype type, const void *data);
SWAPI void swTexSubImage2D(GLint x, GLint y, GLsizei width, GLsizei height, GLenum format, GLenum type, const void *pixels);
SWAPI void swTexParameteri(int param, int value);
SWAPI void swGenFramebuffers(int count, uint32_t *framebuffers);
SWAPI void swDeleteFramebuffers(int count, uint32_t *framebuffers);
SWAPI void swBindFramebuffer(uint32_t id);
SWAPI void swFramebufferTexture2D(SWattachment attach, uint32_t texture);
SWAPI SWfbstatus swCheckFramebufferStatus(void);
SWAPI void swGetFramebufferAttachmentParameteriv(SWattachment attachment, SWattachget property, int *v);
#endif // RLSW_H
/***********************************************************************************
*
* RLSW IMPLEMENTATION
*
************************************************************************************/
#if defined(RLSW_IMPLEMENTATION)
#undef RLSW_IMPLEMENTATION // Undef to allow template expanding without implementation redefinition
#include <stdlib.h> // Required for: malloc(), free()
#include <stddef.h> // Required for: NULL, size_t, uint8_t, uint16_t, uint32_t...
#include <math.h> // Required for: sinf(), cosf(), floorf(), fabsf(), sqrtf(), roundf()
// Simple log system to avoid printf() calls if required
// NOTE: Avoiding those calls, also avoids const strings memory usage
#define SW_SUPPORT_LOG_INFO
#if defined(SW_SUPPORT_LOG_INFO) //&& defined(_DEBUG) // WARNING: LOG() output required for this tool
#include <stdio.h>
#define SW_LOG(...) printf(__VA_ARGS__)
#else
#define SW_LOG(...)
#endif
#if defined(_MSC_VER)
#define SW_ALIGN(x) __declspec(align(x))
#elif defined(__GNUC__) || defined(__clang__)
#define SW_ALIGN(x) __attribute__((aligned(x)))
#else
#define SW_ALIGN(x) // Do nothing if not available
#endif
#if defined(_M_X64) || defined(__x86_64__)
#define SW_ARCH_X86_64
#elif defined(_M_IX86) || defined(__i386__)
#define SW_ARCH_X86
#elif defined(_M_ARM) || defined(__arm__)
#define SW_ARCH_ARM32
#elif defined(_M_ARM64) || defined(__aarch64__)
#define SW_ARCH_ARM64
#elif defined(__riscv)
#define SW_ARCH_RISCV
#endif
#if RLSW_USE_SIMD_INTRINSICS
// Check for SIMD vector instructions
// NOTE: Compiler is responsible to enable required flags for host device,
// supported features are detected at compiler init but varies depending on compiler
// TODO: This logic must be reviewed to avoid the inclusion of multiple headers
// and enable the higher level of SIMD available
#if defined(__FMA__) && defined(__AVX2__)
#define SW_HAS_FMA_AVX2
#include <immintrin.h>
#elif defined(__FMA__) && defined(__AVX__)
#define SW_HAS_FMA_AVX
#include <immintrin.h>
#elif defined(__AVX2__)
#define SW_HAS_AVX2
#include <immintrin.h>
#elif defined(__AVX__)
#define SW_HAS_AVX
#include <immintrin.h>
#endif
#if defined(__SSE4_2__)
#define SW_HAS_SSE42
#include <nmmintrin.h>
#elif defined(__SSE4_1__)
#define SW_HAS_SSE41
#include <smmintrin.h>
#elif defined(__SSSE3__)
#define SW_HAS_SSSE3
#include <tmmintrin.h>
#elif defined(__SSE3__)
#define SW_HAS_SSE3
#include <pmmintrin.h>
#elif defined(__SSE2__) || (defined(_M_AMD64) || defined(_M_X64)) // SSE2 x64
#define SW_HAS_SSE2
#include <emmintrin.h>
#elif defined(__SSE__)
#define SW_HAS_SSE
#include <xmmintrin.h>
#endif
#if defined(__ARM_NEON) || defined(__aarch64__)
#if defined(__ARM_FEATURE_FMA)
#define SW_HAS_NEON_FMA
#else
#define SW_HAS_NEON
#endif
#include <arm_neon.h>
#endif
#if defined(__riscv_vector)
// NOTE: Requires compilation flags: -march=rv64gcv -mabi=lp64d
#define SW_HAS_RVV
#include <riscv_vector.h>
#endif
#endif
// ESP-DSP acceleration: ESP-IDF ships an optimized math library that includes
// `dspm_mult_4x4x4_f32` (4x4 matrix multiply) and `dspm_mult_4x4x1_f32` (matrix * vector)
// These are S3-tuned hand-vectorized kernels that beat the scalar versions for both throughput and code-size
// Detection is opt-in to keep the dependency optional: define SW_USE_ESP_DSP from your build system
#if defined(ESP_PLATFORM) && defined(SW_USE_ESP_DSP)
#define SW_HAS_ESP_DSP
#include "dspm_mult.h"
#endif
#ifdef __cplusplus
#define SW_CURLY_INIT(name) name
#else
#define SW_CURLY_INIT(name) (name)
#endif
//----------------------------------------------------------------------------------
// Defines and Macros
//----------------------------------------------------------------------------------
#define SW_PI 3.14159265358979323846f
#define SW_INV_255 0.00392156862745098f // 1.0f/255.0f
#define SW_DEG2RAD (SW_PI/180.0f)
#define SW_RAD2DEG (180.0f/SW_PI)
// When clipping a convex polygon against a plane, at most one vertex is added
// Starting from a quadrilateral (4 vertices), clipped sequentially against
// the frustum (6 planes) then the scissor rectangle (4 planes):
// 4 + 6 + 4 = 14 vertices maximum
#define SW_MAX_CLIPPED_POLYGON_VERTICES 14
#define SW_CLIP_EPSILON 1e-4f
#define SW_HANDLE_NULL 0u
#define SW_POOL_SLOT_LIVE 0x80u // bit7 of the generation byte
#define SW_POOL_SLOT_VER_MASK 0x7Fu // bits6:0 = anti-ABA counter
#define SW_CONCAT(a, b) a##b
#define SW_CONCATX(a, b) SW_CONCAT(a, b)
#define SW_FRAMEBUFFER_COLOR8_GET(c,p,o) SW_CONCATX(sw_pixel_read_color8_, SW_FRAMEBUFFER_COLOR_TYPE)((c),(p),(o))
#define SW_FRAMEBUFFER_COLOR_GET(c,p,o) SW_CONCATX(sw_pixel_read_color_, SW_FRAMEBUFFER_COLOR_TYPE)((c),(p),(o))
#define SW_FRAMEBUFFER_COLOR_SET(p,c,o) SW_CONCATX(sw_pixel_write_color_, SW_FRAMEBUFFER_COLOR_TYPE)((p),(c),(o))
#define SW_FRAMEBUFFER_DEPTH_GET(p,o) SW_CONCATX(sw_pixel_read_depth_, SW_FRAMEBUFFER_DEPTH_TYPE)((p),(o))
#define SW_FRAMEBUFFER_DEPTH_SET(p,d,o) SW_CONCATX(sw_pixel_write_depth_, SW_FRAMEBUFFER_DEPTH_TYPE)((p),(d),(o))
#define SW_FRAMEBUFFER_COLOR_FORMAT SW_CONCATX(SW_PIXELFORMAT_COLOR_, SW_FRAMEBUFFER_COLOR_TYPE)
#define SW_FRAMEBUFFER_DEPTH_FORMAT SW_CONCATX(SW_PIXELFORMAT_DEPTH_, SW_FRAMEBUFFER_DEPTH_TYPE)
#define SW_FRAMEBUFFER_COLOR_SIZE SW_PIXELFORMAT_SIZE[SW_FRAMEBUFFER_COLOR_FORMAT]
#define SW_FRAMEBUFFER_DEPTH_SIZE SW_PIXELFORMAT_SIZE[SW_FRAMEBUFFER_DEPTH_FORMAT]
#define SW_STATE_SCISSOR_TEST (1 << 0)
#define SW_STATE_TEXTURE_2D (1 << 1)
#define SW_STATE_DEPTH_TEST (1 << 2)
#define SW_STATE_CULL_FACE (1 << 3)
#define SW_STATE_BLEND (1 << 4)
#define SW_BLEND_FLAG_NOOP (1 << 0)
#define SW_BLEND_FLAG_NEEDS_ALPHA (1 << 1)
//----------------------------------------------------------------------------------
// Module Types and Structures Definition
//----------------------------------------------------------------------------------
// Aliases types
typedef uint32_t sw_handle_t;
typedef uint16_t sw_half_t;
// Pixel data format type
typedef enum {
SW_PIXELFORMAT_UNKNOWN = 0,
SW_PIXELFORMAT_COLOR_GRAYSCALE, // 8 bit per pixel (no alpha)
SW_PIXELFORMAT_COLOR_GRAYALPHA, // 8*2 bpp (2 channels)
SW_PIXELFORMAT_COLOR_R3G3B2, // 8 bpp
SW_PIXELFORMAT_COLOR_R5G6B5, // 16 bpp
SW_PIXELFORMAT_COLOR_R8G8B8, // 24 bpp
SW_PIXELFORMAT_COLOR_R5G5B5A1, // 16 bpp (1 bit alpha)
SW_PIXELFORMAT_COLOR_R4G4B4A4, // 16 bpp (4 bit alpha)
SW_PIXELFORMAT_COLOR_R8G8B8A8, // 32 bpp
SW_PIXELFORMAT_COLOR_R32, // 32 bpp (1 channel - float)
SW_PIXELFORMAT_COLOR_R32G32B32, // 32*3 bpp (3 channels - float)
SW_PIXELFORMAT_COLOR_R32G32B32A32, // 32*4 bpp (4 channels - float)
SW_PIXELFORMAT_COLOR_R16, // 16 bpp (1 channel - half float)
SW_PIXELFORMAT_COLOR_R16G16B16, // 16*3 bpp (3 channels - half float)
SW_PIXELFORMAT_COLOR_R16G16B16A16, // 16*4 bpp (4 channels - half float)
SW_PIXELFORMAT_DEPTH_D8, // 1 bpp
SW_PIXELFORMAT_DEPTH_D16, // 2 bpp
SW_PIXELFORMAT_DEPTH_D32, // 4 bpp
SW_PIXELFORMAT_COUNT
} sw_pixelformat_t;
typedef enum {
SW_PIXEL_ALPHA_NONE = 0, // No transparency
SW_PIXEL_ALPHA_BIN, // Binary transparency
SW_PIXEL_ALPHA_YES, // Contains transparency
} sw_pixel_alpha_t;
// Forward declarations
typedef struct sw_vertex sw_vertex_t;
// Pixel getter functions
typedef void (*sw_pixel_read_color8_f)(uint8_t *SW_RESTRICT, const void *SW_RESTRICT, uint32_t);
typedef void (*sw_pixel_read_color_f)(float *SW_RESTRICT, const void *SW_RESTRICT, uint32_t);
// Pixel setter functions
typedef void (*sw_pixel_write_color8_f)(void *SW_RESTRICT, const uint8_t *SW_RESTRICT, uint32_t);
typedef void (*sw_pixel_write_color_f)(void *SW_RESTRICT, const float *SW_RESTRICT, uint32_t);
// Color blending function
typedef void (*sw_blend_f)(float *SW_RESTRICT, const float *SW_RESTRICT);
// Rasterizer functions
typedef void (*sw_raster_triangle_f)(const sw_vertex_t*, const sw_vertex_t*, const sw_vertex_t*);
typedef void (*sw_raster_quad_f)(const sw_vertex_t*, const sw_vertex_t*, const sw_vertex_t*, const sw_vertex_t*);
typedef void (*sw_raster_line_f)(const sw_vertex_t*, const sw_vertex_t*);
typedef void (*sw_raster_point_f)(const sw_vertex_t*);
typedef float sw_matrix_t[4*4];
typedef struct sw_vertex {
float position[4]; // Clip space (x,y,z,w) -> NDC (after /w) -> screen space (x,y,z,1/w)
float color[4]; // Color value (RGBA)
float texcoord[2]; // Texture coordinates
} sw_vertex_t;
typedef struct {
void *pixels; // Texture pixels
sw_pixel_read_color8_f readColor8; // Texel read RGBA8
sw_pixel_read_color_f readColor; // Texel read RGBA32F
sw_pixelformat_t format; // Texture format
sw_pixel_alpha_t alpha; // Texture alpha mode
int width, height; // Dimensions of the texture
int wMinus1, hMinus1; // Dimensions minus one
int allocSz; // Allocated size
SWfilter minFilter; // Minification filter
SWfilter magFilter; // Magnification filter
SWwrap sWrap; // Wrap mode for texcoord.x
SWwrap tWrap; // Wrap mode for texcoord.y
float tx; // Texel width
float ty; // Texel height
} sw_texture_t;
typedef struct {
sw_texture_t color; // Default framebuffer color texture
sw_texture_t depth; // Default framebuffer depth texture
} sw_default_framebuffer_t;
typedef struct {
sw_handle_t colorAttachment; // Framebuffer color attachment id