-
-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy pathportablegl.h
More file actions
13119 lines (10892 loc) · 380 KB
/
portablegl.h
File metadata and controls
13119 lines (10892 loc) · 380 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
/*
PortableGL 0.101.0 MIT licensed software renderer that closely mirrors OpenGL 3.x
portablegl.com
robertwinkler.com
Do this:
#define PORTABLEGL_IMPLEMENTATION
before you include this file in *one* C or C++ file to create the implementation.
If you plan on using your own 3D vector/matrix library rather than crsw_math
that is built into PortableGL and your names are the standard glsl vec[2-4],
mat[3-4] etc., define PGL_PREFIX_TYPES too before including portablegl to
prefix all those builtin types with pgl_ to avoid the clash. Note, if
you use PGL_PREFIX_TYPES and write your own shaders, the type for vertex_attribs
is also affected, changing from vec4* to pgl_vec4*.
You can check all the C++ examples and demos, I use my C++ rsw_math library.
// i.e. it should look like this:
#include ...
#include ...
#include ...
// if required
#define PGL_PREFIX_TYPES
#define PORTABLEGL_IMPLEMENTATION
#include "portablegl.h"
You can define PGL_ASSERT before the #include to avoid using assert.h.
You can define PGL_MALLOC, PGL_REALLOC, and PGL_FREE to avoid using malloc,
realloc, and free.
You can define PGL_MEMMOVE to avoid using memmove.
However, even if you define all of those before including portablegl, you
will still be using the standard library (math.h, string.h, stdlib.h, stdio.h
stdint.h, possibly others). It's not worth removing PortableGL's dependency on
the C standard library as it would make it far larger and more complicated
for no real benefit.
QUICK NOTES:
Primarily of interest to game/graphics developers and other people who
just want to play with the graphics pipeline and don't need peak
performance or the the entirety of OpenGL or Vulkan features.
For textures, GL_UNSIGNED_BYTE is the only supported type.
Internally, GL_RGBA is the only supported format, however other formats
are converted automatically to RGBA unless PGL_DONT_CONVERT_TEXTURES is
defined (in which case a format other than GL_RGBA is a GL_INVALID_ENUM
error). The argument internalformat is ignored to ease porting.
Only GL_TEXTURE_MAG_FILTER is actually used internally but you can set the
MIN_FILTER for a texture. Mipmaps are not supported (GenerateMipMap is
a stub and the level argument is ignored/assumed 0) and *MIPMAP* filter
settings are silently converted to NEAREST or LINEAR.
The framebuffer format is a compile time setting which defaults
to 32-bit RGBA memory order, though PGL supports any 32 or 16 bit pixel format
as long as the appropriate macros are defined.
Several variants are predefined for easy use, named for the integer order
not the memory order:
PGL_RGBA32: RGBA memory order on MSB architecture
PGL_ABGR32: RGBA memory order on LSB architecture, default
PGL_ARGB32/PGL_BGRA32: Two other common 32-bit formats
PGL_RGB565/PGL_BGR565: Very common 16 bit formats
PGL_RGBA5551/PGL_ABGR1555: Less common 16 bit formats
Search PGL for those macros to see how to set up the controlling macros
for other formats; it's pretty self explanatory though I may try to improve
it in the future.
The depth and stencil buffer defaults to a combined buffer with the high
24 bits used for the depth value and the low 8 bits used for the stencil.
This format is called PGL_D24S8 internally. The only other format supported
is a 16 bit depth buffer and a separate 8-bit buffer for the stencil. This
is selected by defining PGL_D16 before including PGL.
If you define PGL_D16, you may also define PGL_NO_STENCIL to disable the
stencil buffer entirely to save a bit more memory. However if you define
PGL_NO_STENCIL you must define PGL_D16 as it makes no sense with
the default PGL_D24S8.
Lastly, you can define PGL_NO_DEPTH_NO_STENCIL which will of course
disable both the depth and stencil buffers entirely.
There are several predefined configuration depending on how much memory
you want to/can use that select settings for the framebuffer formats and
the vertex scratch space size:
PGL_TINY_MEM: RGB565, D16, NO_STENCIL, 4 vertex attribs, 80 KB scratch space
PGL_SMALL_MEM: Same as TINY but 800 KB scratch space
PGL_MED_MEM: RGB565, 4 vertex attribs, 1.6 MB scratch space
default: ABGR32, D24S8, 8 vertex attribs, 16 MB scratch space
Obviously most of the time the default is fine, and if none of the
presets match what you want you can mix and match and adjust any of
the finer grained options individually, but don't define a preset *and*
define individual settings as that will cause problems.
DOCUMENTATION
=============
Any PortableGL program has roughly this structure, with some things
possibly declared globally or passed around in function parameters
as needed:
#define WIDTH 640
#define HEIGHT 480
// shaders are functions matching these prototypes
void smooth_vs(float* vs_output, vec4* vertex_attribs, Shader_Builtins* builtins, void* uniforms);
void smooth_fs(float* fs_input, Shader_Builtins* builtins, void* uniforms);
typedef struct My_Uniforms {
mat4 mvp_mat;
vec4 v_color;
} My_Uniforms;
pix_t* backbuf = NULL;
glContext the_context;
if (!init_glContext(&the_context, &backbuf, WIDTH, HEIGHT)) {
puts("Failed to initialize glContext");
exit(0);
}
// interpolation is an array with an entry of PGL_SMOOTH, PGL_FLAT or
// PGL_NOPERSPECTIVE for each float being interpolated between the
// vertex and fragment shaders. Convenience macros are available
// for 2, 3, and 4 components, ie
// PGL_FLAT3 expands to PGL_FLAT, PGL_FLAT, PGL_FLAT
// the last parameter is whether the fragment shader writes to
// gl_FragDepth or discard. When it is false, PGL may do early
// fragment processing (scissor, depth, stencil etc) for a minor
// performance boost but canonicaly these happen after the frag
// shader
GLenum interpolation[4] = { PGL_SMOOTH4 };
GLuint myshader = pglCreateProgram(smooth_vs, smooth_fs, 4, interpolation, GL_FALSE);
glUseProgram(myshader);
// v_color is not actually used since we're using per vert color
My_Uniform the_uniforms = { IDENTITY_MAT4() };
pglSetUniform(&the_uniforms);
// Your standard OpenGL buffer setup etc. here
// Like the compatibility profile, we allow/enable a default
// VAO. We also have a default shader program for the same reason,
// something to fill index 0.
// see implementation of init_glContext for details
while (1) {
// standard glDraw calls, switching shaders etc.
// use backbuf however you want, whether that's blitting
// it to some framebuffer in your GUI system, or even writing
// it out to disk with something like stb_image_write.
}
free_glContext(&the_context);
// compare with equivalent glsl below
void smooth_vs(float* vs_output, vec4* vertex_attribs, Shader_Builtins* builtins, void* uniforms)
{
((vec4*)vs_output)[0] = vertex_attribs[1]; //color
builtins->gl_Position = mult_mat4_vec4(*((mat4*)uniforms), vertex_attribs[0]);
}
void smooth_fs(float* fs_input, Shader_Builtins* builtins, void* uniforms)
{
builtins->gl_FragColor = ((vec4*)fs_input)[0];
}
// note smooth is the default so this is the same as smooth out vec4 vary_color
// https://www.khronos.org/opengl/wiki/Type_Qualifier_(GLSL)#Interpolation_qualifiers
uniform mvp_mat
layout (location = 0) in vec4 in_vertex;
layout (location = 1) in vec4 in_color;
out vec4 vary_color;
void main(void)
{
vary_color = in_color;
gl_Position = mvp_mat * in_vertex;
}
in vec4 vary_color;
out vec4 frag_color;
void main(void)
{
frag_color = vary_color;
}
// You might also want to resize the framebuffer if your window is resizable
// instead of just letting your GUI system scale the output in which case when
// you handle a resize event you would do something like this:
pglResizeFramebuffer(new_width, new_height);
backbuf = (pix_t*)pglGetBackBuffer();
glViewport(0, 0, new_width, new_height);
// anything else you need for your particular GUI/windowing system here
// alternatively, if your backbuffer (ie color buffer) is changing
// ie, you're switching between rendering to a texture and the normal
// backbuffer, you would call pglSetBackBuffer() and pglSetTexBackBuffer()
// as needed:
pglSetTexBackBuffer(tex_handle);
pglSetBackBuffer(backbuf, width, height);
// A few important things to note about these functions:
// 1. The framebuffer pixel format must be 32-bit RGBA (this is the default
// PGL_ABGR32 aka RGBA32 on LSB) if you want to do render-to-texture
// because that's the only texture format supported. I have ideas for loosening
// this restriction at least a little in the future.
//
// 2. Neither function changes the the depth/stencil buffers so assuming
// you have depth and/or stencil, the only way to use these safely is
// to make sure the texture is the same dimensions or you provide the same
// width and height to pglSetBackBuffer().
//
// 3. PGLSetBackBuffer() does not change the ownership of the framebuffer memory,
// nor does it free the existing framebuffer even if it did own it.
// If the backbuffer was not user owned before the call, it will still
// not be after the call. If you didn't already get a pointer to the pixels
// (via pglGetBackBuffer() for example) you will have created a memory leak.
// Though this behavior may seem counter-intuitive, it is to support the most
// common use case more easily, where you let PGL handle the allocations and
// resizing but you hold a pointer so you can switch back and forth.
//
// 4. PGLSetTexBackBuffer() does change the owership of the framebuffer
// to match the texture used. Since this is almost always PGL owned
// it is usually a non-issue.
//
// See the lesson16 example for examples of these functions in action.
That's basically it. There are some other non-standard features like
pglSetInterp that lets you change the interpolation of a shader
whenever you want. In real OpenGL you'd have to have 2 (or more) separate
but almost identical shaders to do that.
ADDITIONAL CONFIGURATION
========================
We've already mentioned several configuration macros above but here are
all of the non-framebuffer/memory related ones:
PGL_UNSAFE
This replaces the old portablegl_unsafe.h
It turns off all error checking and debug message/logging the same way
NDEBUG turns off assert(). By default PGL is a GL_DEBUG_CONTEXT with
GL_DEBUG_OUTPUT on and a default callback function printing to stdout.
You can use Enable/Disable and DebugMessageCallback to turn it on/off
or use your own callback function like normal. However with PGL_UNSAFE
defined, there's nothing compiled in at all so I would only use it
when you're pushing for every ounce of perf.
PGL_PREFIX_TYPES
This prefixes the standard glsl types (and a couple other internal types)
with pgl_ (ie vec2 becomes pgl_vec2)
PGL_ASSERT
PGL_MALLOC/PGL_REALLOC/PGL_FREE
PGL_MEMMOVE
These overrride the standard functions of the same names
PGL_DONT_CONVERT_TEXTURES
This makes passing PGL a texture with a format other than GL_RGBA an error.
By default other types are automatically converted. You can perform the
conversion manually using the function convert_format_to_packed_rgba().
The included function convert_grayscale_to_rgba() is also useful,
especially for font textures.
PGL_PREFIX_GLSL or PGL_SUFFIX_GLSL
These replace PGL_EXCLUDE_GLSL. Since PGL depends on at least a few
glsl functions and potentially more in the future it doesn't make
sense to exclude GLSL entirely, especially since they're all inline so
it really doesn't save you anything in the final executable.
Instead, using one of these two macros you can change the handful of
functions that are likely to cause a conflict with an external
math library like glm (with a using declaration/directive of course).
So smoothstep() would become either pgl_smoothstep() or smoothstepf(). So far it is less than
10 functions that are modified but feel free to add more.
PGL_HERMITE_SMOOTHING
Turn on hermite smoothing when doing linear interpolation of textures.
It is not required by the spec and it does slow it down but it does
look smoother so it's worth trying if you're curious. Note, most
implementations do not use it.
PGL_BETTER_THICK_LINES
If defined, use a more mathematically correct thick line drawing algorithm
than the one in the official OpenGL spec. It is about 15-17% slower but
has the correct width. The default draws exactly width pixels in the
minor axis, which results in only horizontal and vertical lines being
correct. It also means the ends are not perpendicular to the line which
looks worse the thicker the line. The better algorithm is about what is
specified for GL_LINE_SMOOTH/AA lines except without the actual
anti-aliasing (ie no changes to the alpha channel).
PGL_DISABLE_COLOR_MASK
If defined, color masking (which is set using glColorMask()) is ignored
which provides some performance benefit though it varies depending on
what you're doing.
PGL_EXCLUDE_STUBS
If defined, PGL will exclude stubs for dozens of OpenGL functions that
make porting existing OpenGL projects and reusing existing OpenGL
helper/library code with PortableGL much easier. This might make
sense to define if you're starting a PGL project from scratch.
PGL_ENABLE_CLAMP_TO_BORDER
By default it's ignored and treated the same as CLAMP_TO_EDGE because
I can only think of two ways to implement it. The first way was to
manually add a 1 pixel border around textures which was far more
painful and ugly that it sounds to make work and means I can't have
mapped texture data (ie pglTexImage2D that uses the pointer passed in)
as well as making any future render to texture functionality more complicated.
Th second way is with a bunch of extra if statements in the texture sampling
code which slows down all accesses regardless of if they're using a border
or not. So it's off by default and you can turn it on with this macro.
There are also several predefined maximums which you can change.
However, considering the performance limitations of PortableGL, the defaults
are probably more than enough, and in fact you might want to decrease PGL_MAX_VERTICES
and GL_MAX_VERTEX_ATTRIBS to save memory, see PGL memory presets in QUICK_NOTES above.
MAX_DRAW_BUFFERS, MAX_COLOR_ATTACHMENTS aren't used since those features aren't implemented.
PGL_MAX_VERTICES refers to the number of output vertices of a single draw call.
#define GL_MAX_VERTEX_ATTRIBS 8
#define GL_MAX_VERTEX_OUTPUT_COMPONENTS (4*GL_MAX_VERTEX_ATTRIBS)
#define GL_MAX_DRAW_BUFFERS 4
#define GL_MAX_COLOR_ATTACHMENTS 4
#define PGL_MAX_VERTICES 500000
#define PGL_MAX_ALIASED_WIDTH 2048.0f
#define PGL_MAX_TEXTURE_SIZE 16384
#define PGL_MAX_3D_TEXTURE_SIZE 8192
#define PGL_MAX_ARRAY_TEXTURE_LAYERS 8192
MIT License
Copyright (c) 2011-2025 Robert Winkler
Copyright (c) 1997-2025 Fabrice Bellard (clipping code from TinyGL)
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.
*/
#ifdef PGL_PREFIX_TYPES
#define vec2 pgl_vec2
#define vec3 pgl_vec3
#define vec4 pgl_vec4
#define ivec2 pgl_ivec2
#define ivec3 pgl_ivec3
#define ivec4 pgl_ivec4
#define uvec2 pgl_uvec2
#define uvec3 pgl_uvec3
#define uvec4 pgl_uvec4
#define bvec2 pgl_bvec2
#define bvec3 pgl_bvec3
#define bvec4 pgl_bvec4
#define mat2 pgl_mat2
#define mat3 pgl_mat3
#define mat4 pgl_mat4
#define Color pgl_Color
#define Line pgl_Line
#define Plane pgl_Plane
#endif
// I really need to think about these
// Maybe suffixes should just be the default since I already give many glsl
// functions suffixes but then we still have the problem if I ever want
// to support doubles with no suffix like C math funcs..
//
// For now it's just functions that are used inside PortableGL itself
// as that is what will definitely break without them if these macros
// are used
// Add/remove as needed as long as you also modify
// matching undef section in close_pgl.h
#ifdef PGL_PREFIX_GLSL
#define smoothstep pgl_smoothstep
#define clamp_01 pgl_clamp_01
#define clamp_01_v4 pgl_clamp_01_v4
#define clamp pgl_clamp
#define clampi pgl_clampi
#elif defined(PGL_SUFFIX_GLSL)
#define smoothstep smoothstepf
#define clamp_01 clampf_01
#define clamp_01_v4 clampf_01_v4
#define clamp clampf
#define clampi clampi
#endif
#ifndef GL_H
#define GL_H
#ifdef __cplusplus
extern "C" {
#endif
#ifndef PGLDEF
#ifdef PGL_STATIC
#define PGLDEF static
#else
#define PGLDEF extern
#endif
#endif
#ifndef PGL_ASSERT
#include <assert.h>
#define PGL_ASSERT(x) assert(x)
#endif
#ifndef CVEC_ASSERT
#define CVEC_ASSERT(x) PGL_ASSERT(x)
#endif
#if defined(PGL_MALLOC) && defined(PGL_FREE) && defined(PGL_REALLOC)
/* ok */
#elif !defined(PGL_MALLOC) && !defined(PGL_FREE) && !defined(PGL_REALLOC)
/* ok */
#else
#error "Must define all or none of PGL_MALLOC, PGL_FREE, and PGL_REALLOC."
#endif
#ifndef PGL_MALLOC
#define PGL_MALLOC(sz) malloc(sz)
#define PGL_REALLOC(p, sz) realloc(p, sz)
#define PGL_FREE(p) free(p)
#endif
#define CVEC_MALLOC(sz) PGL_MALLOC(sz)
#define CVEC_REALLOC(p, sz) PGL_REALLOC(p, sz)
#define CVEC_FREE(p) PGL_FREE(p)
#ifndef PGL_MEMMOVE
#include <string.h>
#define PGL_MEMMOVE(dst, src, sz) memmove(dst, src, sz)
#else
#define CVEC_MEMMOVE(dst, src, sz) PGL_MEMMOVE(dst, src, sz)
#endif
// Get rid of signed/unsigned comparison warnings when looping through vectors
#ifndef CVEC_SIZE_T
#define CVEC_SIZE_T i64
#endif
// Feel free to change these presets
// Do not use one of these combined with individual settings; you can cause problems
// if multiple selections within the same category are defined. I guard for
// depth/stencil settings but not framebuffer settings
#if !defined(PGL_D16) && !defined(PGL_D24S8) && !defined(PGL_NO_DEPTH_NO_STENCIL)
# ifdef PGL_TINY_MEM
// framebuffer mem use = 4*w*h
# define PGL_RGB565
# define PGL_D16
# define PGL_NO_STENCIL
# elif defined(PGL_SMALL_MEM)
// 4*w*h
# define PGL_RGB565
# define PGL_D16
# define PGL_NO_STENCIL
# elif defined(PGL_MED_MEM)
// 6*w*h
# define PGL_RGB565
# define PGL_D24S8
# else
// 8*w*h
// defining this just for users convenience to detect different builds
// though if you manually select smaller framebuffers this becomes confusing
# define PGL_NORMAL_MEM
# define PGL_D24S8
// pixel format default to PGL_ABGR32 set below if no other defined
# endif
#endif
// depth settings check
#ifdef PGL_NO_DEPTH_NO_STENCIL
# if defined(PGL_D16) || defined(PGL_D24S8)
# error "PGL_D16 and PGL_D24S8 are incompatible with PGL_NO_DEPTH_NO_STENCIL"
# endif
# ifdef PGL_NO_STENCIL
//#warning is technically not standard till C23 and C++23 but supported by most compilers
# warning "You don't need to define PGL_NO_STENCIL if you defined PGL_NO_DEPTH_NO_STENCIL"
# else
# define PGL_NO_STENCIL
# endif
#endif
#if defined(PGL_AMASK) && defined(PGL_RMASK) && defined(PGL_GMASK) && defined(PGL_BMASK) && \
defined(PGL_ASHIFT) && defined(PGL_RSHIFT) && defined(PGL_GSHIFT) && defined(PGL_BSHIFT) && \
defined(PGL_RMAX) && defined(PGL_GMAX) && defined(PGL_BMAX) && defined(PGL_AMAX) && defined(PGL_BITDEPTH)
/* ok */
#elif !defined(PGL_AMASK) && !defined(PGL_RMASK) && !defined(PGL_GMASK) && !defined(PGL_BMASK) && \
!defined(PGL_ASHIFT) && !defined(PGL_RSHIFT) && !defined(PGL_GSHIFT) && !defined(PGL_BSHIFT) && \
!defined(PGL_RMAX) && !defined(PGL_GMAX) && !defined(PGL_BMAX) && !defined(PGL_AMAX) && !defined(PGL_BITDEPTH)
/* ok */
#else
#error "Must define all PGL_(RGBA)MASK, PGL_(RGBA)SHIFT, PGL_(RGBA)MAX, and PGL_BITDEPTH or none (which will give default PGL_AGBR32 format)"
#endif
// TODO more 32-bit formats
#ifdef PGL_RGBA32
#define PGL_RMASK 0xFF000000
#define PGL_GMASK 0x00FF0000
#define PGL_BMASK 0x0000FF00
#define PGL_AMASK 0x000000FF
#define PGL_RSHIFT 24
#define PGL_GSHIFT 16
#define PGL_BSHIFT 8
#define PGL_ASHIFT 0
#define PGL_RMAX 255
#define PGL_GMAX 255
#define PGL_BMAX 255
#define PGL_AMAX 255
#define PGL_BITDEPTH 32
#define PGL_PIX_STR "RGBA32"
#elif defined(PGL_ARGB32)
#define PGL_AMASK 0xFF000000
#define PGL_RMASK 0x00FF0000
#define PGL_GMASK 0x0000FF00
#define PGL_BMASK 0x000000FF
#define PGL_ASHIFT 24
#define PGL_RSHIFT 16
#define PGL_GSHIFT 8
#define PGL_BSHIFT 0
#define PGL_RMAX 255
#define PGL_GMAX 255
#define PGL_BMAX 255
#define PGL_AMAX 255
#define PGL_BITDEPTH 32
#define PGL_PIX_STR "ARGB32"
#elif defined(PGL_BGRA32)
#define PGL_BMASK 0xFF000000
#define PGL_GMASK 0x00FF0000
#define PGL_RMASK 0x0000FF00
#define PGL_AMASK 0x000000FF
#define PGL_BSHIFT 24
#define PGL_GSHIFT 16
#define PGL_RSHIFT 8
#define PGL_ASHIFT 0
#define PGL_RMAX 255
#define PGL_GMAX 255
#define PGL_BMAX 255
#define PGL_AMAX 255
#define PGL_BITDEPTH 32
#define PGL_PIX_STR "BGRA32"
#elif defined(PGL_RGB565)
#define PGL_RMASK 0xF800
#define PGL_GMASK 0x07E0
#define PGL_BMASK 0x001F
#define PGL_AMASK 0x0000
#define PGL_RSHIFT 11
#define PGL_GSHIFT 5
#define PGL_BSHIFT 0
#define PGL_ASHIFT 0
#define PGL_RMAX 31
#define PGL_GMAX 63
#define PGL_BMAX 31
#define PGL_AMAX 0
#define PGL_BITDEPTH 16
#define PGL_PIX_STR "RGB565"
#elif defined(PGL_BGR565)
#define PGL_BMASK 0xF800
#define PGL_GMASK 0x07E0
#define PGL_RMASK 0x001F
#define PGL_AMASK 0x0000
#define PGL_BSHIFT 11
#define PGL_GSHIFT 5
#define PGL_RSHIFT 0
#define PGL_ASHIFT 0
#define PGL_RMAX 31
#define PGL_GMAX 63
#define PGL_BMAX 31
#define PGL_AMAX 0
#define PGL_BITDEPTH 16
#define PGL_PIX_STR "BGR565"
#elif defined(PGL_RGBA5551)
#define PGL_RMASK 0xF800
#define PGL_GMASK 0x07C0
#define PGL_BMASK 0x003E
#define PGL_AMASK 0x0001
#define PGL_RSHIFT 11
#define PGL_GSHIFT 6
#define PGL_BSHIFT 1
#define PGL_ASHIFT 0
#define PGL_RMAX 31
#define PGL_GMAX 31
#define PGL_BMAX 31
#define PGL_AMAX 1
#define PGL_BITDEPTH 16
#define PGL_PIX_STR "RGBA5551"
#elif defined(PGL_ABGR1555)
#define PGL_AMASK 0x8000
#define PGL_BMASK 0x7C00
#define PGL_GMASK 0x03E0
#define PGL_RMASK 0x001F
#define PGL_ASHIFT 15
#define PGL_BSHIFT 10
#define PGL_GSHIFT 5
#define PGL_RSHIFT 0
#define PGL_RMAX 31
#define PGL_GMAX 31
#define PGL_BMAX 31
#define PGL_AMAX 1
#define PGL_BITDEPTH 16
#define PGL_PIX_STR "ABGR1555"
#else
// default to PGL_ABGR32 for RGBA memory order on LSB
#define PGL_ABGR32
#define PGL_AMASK 0xFF000000
#define PGL_BMASK 0x00FF0000
#define PGL_GMASK 0x0000FF00
#define PGL_RMASK 0x000000FF
#define PGL_ASHIFT 24
#define PGL_BSHIFT 16
#define PGL_GSHIFT 8
#define PGL_RSHIFT 0
#define PGL_RMAX 255
#define PGL_GMAX 255
#define PGL_BMAX 255
#define PGL_AMAX 255
#define PGL_BITDEPTH 32
#define PGL_PIX_STR "ABGR32"
#endif
// for now all 32 bit pixel types are 8888, no weird 10,10,10,2
#if PGL_BITDEPTH == 32
#define RGBA_TO_PIXEL(r,g,b,a) ((u32)(a) << PGL_ASHIFT | (u32)(r) << PGL_RSHIFT | (u32)(g) << PGL_GSHIFT | (u32)(b) << PGL_BSHIFT)
#define PIXEL_TO_COLOR(p) make_Color(((p) & PGL_RMASK) >> PGL_RSHIFT, ((p) & PGL_GMASK) >> PGL_GSHIFT, ((p) & PGL_BMASK) >> PGL_BSHIFT, ((p) & PGL_AMASK) >> PGL_ASHIFT)
#define pix_t u32
#define COLOR_TO_VEC4(c) Color_to_v4(c)
#define VEC4_TO_COLOR(v) v4_to_Color(v)
#elif PGL_BITDEPTH == 16
#if PGL_AMASK == 0
#define RGBA_TO_PIXEL(r,g,b,a) ((int)(r) << PGL_RSHIFT | (int)(g) << PGL_GSHIFT | (int)(b) << PGL_BSHIFT)
#define PIXEL_TO_COLOR(p) make_Color(((p) & PGL_RMASK) >> PGL_RSHIFT, ((p) & PGL_GMASK) >> PGL_GSHIFT, ((p) & PGL_BMASK) >> PGL_BSHIFT, 255)
#else
#define RGBA_TO_PIXEL(r,g,b,a) ((int)(a) << PGL_ASHIFT | (int)(r) << PGL_RSHIFT | (int)(g) << PGL_GSHIFT | (int)(b) << PGL_BSHIFT)
#define PIXEL_TO_COLOR(p) make_Color(((p) & PGL_RMASK) >> PGL_RSHIFT, ((p) & PGL_GMASK) >> PGL_GSHIFT, ((p) & PGL_BMASK) >> PGL_BSHIFT, ((p) & PGL_AMASK) >> PGL_ASHIFT)
#endif
#define pix_t u16
#define PIXEL_TO_VEC4(p) make_v4((((p) & PGL_RMASK) >> PGL_RSHIFT)/(float)PGL_RMAX, (((p) & PGL_GMASK) >> PGL_GSHIFT)/(float)PGL_GMAX, (((p) & PGL_BMASK) >> PGL_BSHIFT)/(float)PGL_BMAX, (((p) & PGL_AMASK) >> PGL_ASHIFT)/(float)PGL_AMAX)
#define COLOR_TO_VEC4(c) make_v4((c).r/(float)PGL_RMAX, (c).g/(float)PGL_GMAX, (c).b/(float)PGL_BMAX, (c).a/(float)PGL_AMAX)
#define VEC4_TO_COLOR(v) make_Color(v.x*PGL_RMAX, v.y*PGL_GMAX, v.z*PGL_BMAX, v.w*PGL_AMAX)
#endif
// TODO these are messy. It makes the code cleaner but I should try to simplify
// these some more. It's the different needs of glClear() vs fragment_processing()
// combined with the different formats that makes it a headache.
#ifdef PGL_D16
#define PGL_MAX_Z 0xFFFF
#define PGL_ZSHIFT 0
#define GET_ZPIX(i) ((u16*)c->zbuf.lastrow)[(i)]
#define GET_ZPIX_TOP(i) ((u16*)c->zbuf.buf)[(i)]
#define GET_Z(i) GET_ZPIX(i)
#define SET_Z_PRESHIFTED(i, v) GET_ZPIX(i) = (v)
#define SET_Z_PRESHIFTED_TOP(i, v) GET_ZPIX_TOP(i) = (v)
//#define SET_Z(i, orig_zpix, v) GET_ZPIX(i) = (v)
#define SET_Z(i, v) GET_ZPIX(i) = (v)
#define stencil_pix_t u8
#define GET_STENCIL_PIX(i) c->stencil_buf.lastrow[(i)]
#define GET_STENCIL_PIX_TOP(i) c->stencil_buf.buf[(i)]
#define EXTRACT_STENCIL(stencil_pix) (stencil_pix)
#define GET_STENCIL(i) GET_STENCIL_PIX(i)
#define GET_STENCIL_TOP(i) GET_STENCIL_PIX_TOP(i)
#define SET_STENCIL(i, v) GET_STENCIL_PIX(i) = (v)
#define SET_STENCIL_TOP(i, v) GET_STENCIL_PIX_TOP(i) = (v)
#elif defined(PGL_D24S8)
#ifdef PGL_NO_STENCIL
#error "PGL_NO_STENCIL is incompatible with PGL_D24S8 format, use with PGL_D16"
#endif
#define PGL_MAX_Z 0xFFFFFF
// could use GL_STENCIL_BITS..?
#define PGL_ZSHIFT 8
#define GET_ZPIX(i) ((u32*)c->zbuf.lastrow)[(i)]
#define GET_ZPIX_TOP(i) ((u32*)c->zbuf.buf)[(i)]
#define GET_Z(i) (GET_ZPIX(i) >> PGL_ZSHIFT)
#define SET_Z_PRESHIFTED(i, v) \
GET_ZPIX(i) &= PGL_STENCIL_MASK; \
GET_ZPIX(i) |= (v)
#define SET_Z_PRESHIFTED_TOP(i, v) \
GET_ZPIX_TOP(i) &= PGL_STENCIL_MASK; \
GET_ZPIX_TOP(i) |= (v)
// TO use this method I need to refactor to have the stencil val *after*
// the stencil test/op run, returned from stencil_op() perhaps.
// TODO compare perf eventually
/*
#define SET_Z(i, stencil_val, v) \
GET_ZPIX(i) = ((stencil_val) & PGL_STENCIL_MASK) | ((v) << PGL_ZSHIFT);
*/
#define SET_Z(i, v) \
GET_ZPIX(i) &= PGL_STENCIL_MASK; \
GET_ZPIX(i) |= ((v) << PGL_ZSHIFT)
#define stencil_pix_t u32
#define GET_STENCIL_PIX(i) ((stencil_pix_t*)c->stencil_buf.lastrow)[(i)]
#define GET_STENCIL_PIX_TOP(i) ((stencil_pix_t*)c->stencil_buf.buf)[(i)]
#define EXTRACT_STENCIL(stencil_pix) ((stencil_pix) & PGL_STENCIL_MASK)
#define GET_STENCIL(i) (GET_STENCIL_PIX(i) & PGL_STENCIL_MASK)
#define GET_STENCIL_TOP(i) (GET_STENCIL_PIX_TOP(i) & PGL_STENCIL_MASK)
#define SET_STENCIL(i, v) \
GET_STENCIL_PIX(i) &= ~PGL_STENCIL_MASK; \
GET_STENCIL_PIX(i) |= (v)
#define SET_STENCIL_TOP(i, v) \
GET_STENCIL_PIX_TOP(i) &= ~PGL_STENCIL_MASK; \
GET_STENCIL_PIX_TOP(i) |= (v)
#elif defined(PGL_NO_DEPTH_NO_STENCIL)
/* ok */
#else
#error "Must define one of PGL_D16, PGL_D24S8, PGL_NO_DEPTH_NO_STENCIL"
#endif
#ifndef CRSW_MATH_H
#define CRSW_MATH_H
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
// Unfortunately this is not supported in gcc even though
// it's in the C99+ spec. Have to use compiler option
// -ffp-contract=off for gcc (which defaults to =fast)
// unlike clang
//
// https://stackoverflow.com/questions/43352510/difference-in-gcc-ffp-contract-options
#pragma STDC FP_CONTRACT OFF
#define RM_PI (3.14159265358979323846)
#define RM_2PI (2.0 * RM_PI)
#define PI_DIV_180 (0.017453292519943296)
#define INV_PI_DIV_180 (57.2957795130823229)
#define DEG_TO_RAD(x) ((x)*PI_DIV_180)
#define RAD_TO_DEG(x) ((x)*INV_PI_DIV_180)
/* Hour angles */
#define HR_TO_DEG(x) ((x) * (1.0 / 15.0))
#define HR_TO_RAD(x) DEG_TO_RAD(HR_TO_DEG(x))
#define DEG_TO_HR(x) ((x) * 15.0)
#define RAD_TO_HR(x) DEG_TO_HR(RAD_TO_DEG(x))
// TODO rename RM_MAX/RSW_MAX? make proper inline functions?
#ifndef MAX
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#endif
#ifndef MIN
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#endif
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
typedef int8_t i8;
typedef int16_t i16;
typedef int32_t i32;
typedef int64_t i64;
typedef struct vec2
{
float x;
float y;
} vec2;
#define SET_V2(v, _x, _y) \
do {\
(v).x = _x;\
(v).y = _y;\
} while (0)
inline vec2 make_v2(float x, float y)
{
vec2 v = { x, y };
return v;
}
inline vec2 neg_v2(vec2 v)
{
vec2 r = { -v.x, -v.y };
return r;
}
inline void fprint_v2(FILE* f, vec2 v, const char* append)
{
fprintf(f, "(%f, %f)%s", v.x, v.y, append);
}
inline void print_v2(vec2 v, const char* append)
{
printf("(%f, %f)%s", v.x, v.y, append);
}
inline int fread_v2(FILE* f, vec2* v)
{
int tmp = fscanf(f, " (%f, %f)", &v->x, &v->y);
return (tmp == 2);
}
inline float len_v2(vec2 a)
{
return sqrt(a.x * a.x + a.y * a.y);
}
inline vec2 norm_v2(vec2 a)
{
float l = len_v2(a);
vec2 c = { a.x/l, a.y/l };
return c;
}
inline void normalize_v2(vec2* a)
{
float l = len_v2(*a);
a->x /= l;
a->y /= l;
}
inline vec2 add_v2s(vec2 a, vec2 b)
{
vec2 c = { a.x + b.x, a.y + b.y };
return c;
}
inline vec2 sub_v2s(vec2 a, vec2 b)
{
vec2 c = { a.x - b.x, a.y - b.y };
return c;
}
inline vec2 mult_v2s(vec2 a, vec2 b)
{
vec2 c = { a.x * b.x, a.y * b.y };
return c;
}
inline vec2 div_v2s(vec2 a, vec2 b)
{
vec2 c = { a.x / b.x, a.y / b.y };
return c;
}
inline float dot_v2s(vec2 a, vec2 b)
{
return a.x*b.x + a.y*b.y;
}
inline vec2 scale_v2(vec2 a, float s)
{
vec2 b = { a.x * s, a.y * s };
return b;
}
inline int equal_v2s(vec2 a, vec2 b)
{
return (a.x == b.x && a.y == b.y);
}
inline int equal_epsilon_v2s(vec2 a, vec2 b, float epsilon)
{
return (fabs(a.x-b.x) < epsilon && fabs(a.y - b.y) < epsilon);
}
inline float cross_v2s(vec2 a, vec2 b)
{
return a.x * b.y - a.y * b.x;
}
inline float angle_v2s(vec2 a, vec2 b)
{
return acos(dot_v2s(a, b) / (len_v2(a) * len_v2(b)));
}
typedef struct vec3
{
float x;
float y;
float z;
} vec3;
#define SET_V3(v, _x, _y, _z) \
do {\
(v).x = _x;\
(v).y = _y;\
(v).z = _z;\
} while (0)
inline vec3 make_v3(float x, float y, float z)
{
vec3 v = { x, y, z };
return v;
}
inline vec3 neg_v3(vec3 v)
{
vec3 r = { -v.x, -v.y, -v.z };
return r;
}
inline void fprint_v3(FILE* f, vec3 v, const char* append)
{
fprintf(f, "(%f, %f, %f)%s", v.x, v.y, v.z, append);
}
inline void print_v3(vec3 v, const char* append)
{
printf("(%f, %f, %f)%s", v.x, v.y, v.z, append);
}
inline int fread_v3(FILE* f, vec3* v)
{
int tmp = fscanf(f, " (%f, %f, %f)", &v->x, &v->y, &v->z);
return (tmp == 3);
}
inline float len_v3(vec3 a)
{
return sqrt(a.x * a.x + a.y * a.y + a.z * a.z);
}
inline vec3 norm_v3(vec3 a)
{
float l = len_v3(a);
vec3 c = { a.x/l, a.y/l, a.z/l };
return c;
}
inline void normalize_v3(vec3* a)
{
float l = len_v3(*a);
a->x /= l;
a->y /= l;
a->z /= l;
}
inline vec3 add_v3s(vec3 a, vec3 b)
{
vec3 c = { a.x + b.x, a.y + b.y, a.z + b.z };
return c;
}
inline vec3 sub_v3s(vec3 a, vec3 b)
{
vec3 c = { a.x - b.x, a.y - b.y, a.z - b.z };
return c;
}
inline vec3 mult_v3s(vec3 a, vec3 b)
{
vec3 c = { a.x * b.x, a.y * b.y, a.z * b.z };
return c;
}