-
Notifications
You must be signed in to change notification settings - Fork 0
/
portablegl.go
4831 lines (4777 loc) · 143 KB
/
portablegl.go
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
/*
Package pgl is a cpu implementation of OpenGL 3.3ish written entirely in Go
MIT License
Copyright (c) 2011-2022 Robert Winkler
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.
Clipping code copyright (c) Fabrice Bellard from TinyGL
https://bellard.org/TinyGL/
(C) 1997-1998 Fabrice Bellard
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product and its documentation
*is* required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
If you redistribute modified sources, I would appreciate that you
include in the files history information documenting your changes.
*/
package pgl
import (
"encoding/binary"
"github.com/chewxy/math32"
"github.com/gotranspile/cxgo/runtime/cmath"
"math"
"unsafe"
)
const RM_PI = 3.14159265358979323846
const RM_2PI = 2.0 * RM_PI
const PI_DIV_180 = 0.017453292519943296
const INV_PI_DIV_180 = 57.2957795130823229
const FALSE = 0
const TRUE = 1
const MAX_VERTICES = 500000
const MAX_VERTEX_ATTRIBS = 16
const MAX_VERTEX_OUTPUT_COMPONENTS = 64
const MAX_DRAW_BUFFERS = 8
const CLIP_EPSILON = 1e-5
type GLuint uint32
type GLint int32
type GLint64 int64
type GLuint64 uint64
type GLushort uint16
type GLshort int16
type GLubyte uint8
type GLbyte int8
type GLchar int8
type GLsizei int32
type GLenum int64
type GLbitfield int64
type GLfloat float32
type GLclampf float32
type GLdouble float64
type GLboolean uint8
const (
NO_ERROR = 0
INVALID_ENUM = 1
INVALID_VALUE = 2
INVALID_OPERATION = 3
INVALID_FRAMEBUFFER_OPERATION = 4
OUT_OF_MEMORY = 5
ARRAY_BUFFER = 6
COPY_READ_BUFFER = 7
COPY_WRITE_BUFFER = 8
ELEMENT_ARRAY_BUFFER = 9
PIXEL_PACK_BUFFER = 10
PIXEL_UNPACK_BUFFER = 11
TEXTURE_BUFFER = 12
TRANSFORM_FEEDBACK_BUFFER = 13
UNIFORM_BUFFER = 14
NUM_BUFFER_TYPES = 15
STREAM_DRAW = 16
STREAM_READ = 17
STREAM_COPY = 18
STATIC_DRAW = 19
STATIC_READ = 20
STATIC_COPY = 21
DYNAMIC_DRAW = 22
DYNAMIC_READ = 23
DYNAMIC_COPY = 24
READ_ONLY = 25
WRITE_ONLY = 26
READ_WRITE = 27
POINT = 28
LINE = 29
FILL = 30
POINTS = 31
LINES = 32
LINE_STRIP = 33
LINE_LOOP = 34
TRIANGLES = 35
TRIANGLE_STRIP = 36
TRIANGLE_FAN = 37
LINE_STRIP_AJACENCY = 38
LINES_AJACENCY = 39
TRIANGLES_AJACENCY = 40
TRIANGLE_STRIP_AJACENCY = 41
LESS = 42
LEQUAL = 43
GREATER = 44
GEQUAL = 45
EQUAL = 46
NOTEQUAL = 47
ALWAYS = 48
NEVER = 49
ZERO = 50
ONE = 51
SRC_COLOR = 52
ONE_MINUS_SRC_COLOR = 53
DST_COLOR = 54
ONE_MINUS_DST_COLOR = 55
SRC_ALPHA = 56
ONE_MINUS_SRC_ALPHA = 57
DST_ALPHA = 58
ONE_MINUS_DST_ALPHA = 59
CONSTANT_COLOR = 60
ONE_MINUS_CONSTANT_COLOR = 61
CONSTANT_ALPHA = 62
ONE_MINUS_CONSTANT_ALPHA = 63
SRC_ALPHA_SATURATE = 64
NUM_BLEND_FUNCS = 65
SRC1_COLOR = 66
ONE_MINUS_SRC1_COLOR = 67
SRC1_ALPHA = 68
ONE_MINUS_SRC1_ALPHA = 69
FUNC_ADD = 70
FUNC_SUBTRACT = 71
FUNC_REVERSE_SUBTRACT = 72
MIN = 73
MAX = 74
NUM_BLEND_EQUATIONS = 75
TEXTURE_UNBOUND = 76
TEXTURE_1D = 77
TEXTURE_2D = 78
TEXTURE_3D = 79
TEXTURE_1D_ARRAY = 80
TEXTURE_2D_ARRAY = 81
TEXTURE_RECTANGLE = 82
TEXTURE_CUBE_MAP = 83
NUM_TEXTURE_TYPES = 84
TEXTURE_CUBE_MAP_POSITIVE_X = 85
TEXTURE_CUBE_MAP_NEGATIVE_X = 86
TEXTURE_CUBE_MAP_POSITIVE_Y = 87
TEXTURE_CUBE_MAP_NEGATIVE_Y = 88
TEXTURE_CUBE_MAP_POSITIVE_Z = 89
TEXTURE_CUBE_MAP_NEGATIVE_Z = 90
TEXTURE_BASE_LEVEL = 91
TEXTURE_BORDER_COLOR = 92
TEXTURE_COMPARE_FUNC = 93
TEXTURE_COMPARE_MODE = 94
TEXTURE_LOD_BIAS = 95
TEXTURE_MIN_FILTER = 96
TEXTURE_MAG_FILTER = 97
TEXTURE_MIN_LOD = 98
TEXTURE_MAX_LOD = 99
TEXTURE_MAX_LEVEL = 100
TEXTURE_SWIZZLE_R = 101
TEXTURE_SWIZZLE_G = 102
TEXTURE_SWIZZLE_B = 103
TEXTURE_SWIZZLE_A = 104
TEXTURE_SWIZZLE_RGBA = 105
TEXTURE_WRAP_S = 106
TEXTURE_WRAP_T = 107
TEXTURE_WRAP_R = 108
REPEAT = 109
CLAMP_TO_EDGE = 110
CLAMP_TO_BORDER = 111
MIRRORED_REPEAT = 112
NEAREST = 113
LINEAR = 114
NEAREST_MIPMAP_NEAREST = 115
NEAREST_MIPMAP_LINEAR = 116
LINEAR_MIPMAP_NEAREST = 117
LINEAR_MIPMAP_LINEAR = 118
RED = 119
RG = 120
RGB = 121
BGR = 122
RGBA = 123
BGRA = 124
COMPRESSED_RED = 125
COMPRESSED_RG = 126
COMPRESSED_RGB = math.MaxInt8
COMPRESSED_RGBA = 128
UNPACK_ALIGNMENT = 129
PACK_ALIGNMENT = 130
TEXTURE0 = 131
TEXTURE1 = 132
TEXTURE2 = 133
TEXTURE3 = 134
TEXTURE4 = 135
TEXTURE5 = 136
TEXTURE6 = 137
TEXTURE7 = 138
CULL_FACE = 139
DEPTH_TEST = 140
DEPTH_CLAMP = 141
LINE_SMOOTH = 142
BLEND = 143
COLOR_LOGIC_OP = 144
POLYGON_OFFSET_FILL = 145
SCISSOR_TEST = 146
STENCIL_TEST = 147
FIRST_VERTEX_CONVENTION = 148
LAST_VERTEX_CONVENTION = 149
POINT_SPRITE_COORD_ORIGIN = 150
UPPER_LEFT = 151
LOWER_LEFT = 152
FRONT = 153
BACK = 154
FRONT_AND_BACK = 155
CCW = 156
CW = 157
CLEAR = 158
SET = 159
COPY = 160
COPY_INVERTED = 161
NOOP = 162
AND = 163
NAND = 164
OR = 165
NOR = 166
XOR = 167
EQUIV = 168
AND_REVERSE = 169
AND_INVERTED = 170
OR_REVERSE = 171
OR_INVERTED = 172
INVERT = 173
KEEP = 174
REPLACE = 175
INCR = 176
INCR_WRAP = 177
DECR = 178
DECR_WRAP = 179
UNSIGNED_BYTE = 180
BYTE = 181
BITMAP = 182
UNSIGNED_SHORT = 183
SHORT = 184
UNSIGNED_INT = 185
INT = 186
FLOAT = 187
VENDOR = 188
RENDERER = 189
VERSION = 190
SHADING_LANGUAGE_VERSION = 191
POLYGON_OFFSET_FACTOR = 192
POLYGON_OFFSET_UNITS = 193
POINT_SIZE = 194
DEPTH_CLEAR_VALUE = 195
DEPTH_RANGE = 196
STENCIL_WRITE_MASK = 197
STENCIL_REF = 198
STENCIL_VALUE_MASK = 199
STENCIL_FUNC = 200
STENCIL_FAIL = 201
STENCIL_PASS_DEPTH_FAIL = 202
STENCIL_PASS_DEPTH_PASS = 203
STENCIL_BACK_WRITE_MASK = 204
STENCIL_BACK_REF = 205
STENCIL_BACK_VALUE_MASK = 206
STENCIL_BACK_FUNC = 207
STENCIL_BACK_FAIL = 208
STENCIL_BACK_PASS_DEPTH_FAIL = 209
STENCIL_BACK_PASS_DEPTH_PASS = 210
LOGIC_OP_MODE = 211
BLEND_SRC_RGB = 212
BLEND_SRC_ALPHA = 213
BLEND_DST_RGB = 214
BLEND_DST_ALPHA = 215
BLEND_EQUATION_RGB = 216
BLEND_EQUATION_ALPHA = 217
CULL_FACE_MODE = 218
FRONT_FACE = 219
DEPTH_FUNC = 220
PROVOKING_VERTEX = 221
POLYGON_MODE = 222
COMPUTE_SHADER = 223
VERTEX_SHADER = 224
TESS_CONTROL_SHADER = 225
TESS_EVALUATION_SHADER = 226
GEOMETRY_SHADER = 227
FRAGMENT_SHADER = 228
INFO_LOG_LENGTH = 229
COMPILE_STATUS = 230
LINK_STATUS = 231
COLOR_BUFFER_BIT = 1 << 10
DEPTH_BUFFER_BIT = 1 << 11
STENCIL_BUFFER_BIT = 1 << 12
)
const (
SMOOTH = iota
FLAT
NOPERSPECTIVE
)
type PerVertex struct {
Gl_Position Vec4
Gl_PointSize float32
Gl_ClipDistance [6]float32
}
type Shader_Builtins struct {
Gl_Position Vec4
Gl_InstanceID GLint
Gl_PointCoord vec2
Gl_FrontFacing GLboolean
Gl_FragCoord Vec4
Gl_FragColor Vec4
Gl_FragDepth float32
Discard GLboolean
}
type vert_func func(vs_output *float32, vertex_attribs unsafe.Pointer, builtins *Shader_Builtins, uniforms interface{})
type frag_func func(fs_input *float32, builtins *Shader_Builtins, uniforms interface{})
type glProgram struct {
Vertex_shader vert_func
Fragment_shader frag_func
Uniform interface{}
Vs_output_size int64
Interpolation [MAX_VERTEX_OUTPUT_COMPONENTS]GLenum
Fragdepth_or_discard GLboolean
Deleted GLboolean
}
type glBuffer struct {
Size GLsizei
Type GLenum
Data []u8
Deleted bool
User_owned bool
}
type glVertex_Attrib struct {
Size GLint
Type GLenum
Stride GLsizei
Offset GLsizei
Normalized bool
Buf uint64
Enabled bool
Divisor GLuint
}
type glVertex_Array struct {
Vertex_attribs [MAX_VERTEX_ATTRIBS]glVertex_Attrib
Element_buffer GLuint
Deleted bool
}
type glTexture struct {
W uint64
H uint64
D uint64
Base_level int64
Mag_filter GLenum
Min_filter GLenum
Wrap_s GLenum
Wrap_t GLenum
Wrap_r GLenum
Format GLenum
Type GLenum
Deleted GLboolean
User_owned GLboolean
Data []u8
}
type glVertex struct {
Clip_space Vec4
Screen_space Vec4
Clip_code int64
Edge_flag int64
Vs_out []float32
}
type glFramebuffer struct {
Buf []u8
Lastrow []u8
W uint64
H uint64
}
type Vertex_Shader_output struct {
Size int64
Interpolation []GLenum
Output_buf []float32
}
type draw_triangle_func func(v0 *glVertex, v1 *glVertex, v2 *glVertex, provoke uint64)
type GlContext struct {
Vp_mat Mat4
X_min int64
Y_min int64
X_max uint64
Y_max uint64
Vertex_arrays []glVertex_Array
Buffers []glBuffer
Textures []glTexture
Programs []glProgram
Cur_vertex_array GLuint
Bound_buffers [9]GLuint
Bound_textures [7]GLuint
Cur_texture2D GLuint
Cur_program GLuint
Error GLenum
Vertex_attribs_vs [MAX_VERTEX_ATTRIBS]Vec4
Builtins Shader_Builtins
Vs_output Vertex_Shader_output
Fs_input [MAX_VERTEX_OUTPUT_COMPONENTS]float32
Depth_test GLboolean
Line_smooth GLboolean
Cull_face GLboolean
Fragdepth_or_discard GLboolean
Depth_clamp GLboolean
Depth_mask GLboolean
Blend GLboolean
Logic_ops GLboolean
Poly_offset GLboolean
Scissor_test GLboolean
Stencil_test GLboolean
Stencil_writemask GLuint
Stencil_writemask_back GLuint
Stencil_ref GLint
Stencil_ref_back GLint
Stencil_valuemask GLuint
Stencil_valuemask_back GLuint
Stencil_func GLenum
Stencil_func_back GLenum
Stencil_sfail GLenum
Stencil_dpfail GLenum
Stencil_dppass GLenum
Stencil_sfail_back GLenum
Stencil_dpfail_back GLenum
Stencil_dppass_back GLenum
Logic_func GLenum
Blend_sfactor GLenum
Blend_dfactor GLenum
Blend_equation GLenum
Cull_mode GLenum
Front_face GLenum
Poly_mode_front GLenum
Poly_mode_back GLenum
Depth_func GLenum
Point_spr_origin GLenum
Provoking_vert GLenum
Poly_factor GLfloat
Poly_units GLfloat
Scissor_lx GLint
Scissor_ly GLint
Scissor_ux GLsizei
Scissor_uy GLsizei
Unpack_alignment GLint
Pack_alignment GLint
Clear_stencil GLint
Clear_color Color
Blend_color Vec4
Point_size GLfloat
Clear_depth GLfloat
Depth_range_near GLfloat
Depth_range_far GLfloat
Draw_triangle_front draw_triangle_func
Draw_triangle_back draw_triangle_func
Zbuf glFramebuffer
Back_buffer glFramebuffer
Stencil_buf glFramebuffer
User_alloced_backbuf int64
Bitdepth int64
Rmask U32
Gmask U32
Bmask U32
Amask U32
Rshift int64
Gshift int64
Bshift int64
Ashift int64
Glverts []glVertex
}
func load_rotation_mat3(mat *mat3, v Vec3, angle float32) {
var (
s float32
c float32
xx float32
yy float32
zz float32
xy float32
yz float32
zx float32
xs float32
ys float32
zs float32
one_c float32
)
s = float32(math.Sin(float64(angle)))
c = float32(math.Cos(float64(angle)))
normalize_vec3(&v)
xx = v.X * v.X
yy = v.Y * v.Y
zz = v.Z * v.Z
xy = v.X * v.Y
yz = v.Y * v.Z
zx = v.Z * v.X
xs = v.X * s
ys = v.Y * s
zs = v.Z * s
one_c = float32(1.0 - float64(c))
mat[0] = (one_c * xx) + c
mat[3] = (one_c * xy) - zs
mat[6] = (one_c * zx) + ys
mat[1] = (one_c * xy) + zs
mat[4] = (one_c * yy) + c
mat[7] = (one_c * yz) - xs
mat[2] = (one_c * zx) - ys
mat[5] = (one_c * yz) + xs
mat[8] = (one_c * zz) + c
}
func Mult_mat4_mat4(c *Mat4, a, b Mat4) {
c[0] = a[0]*b[0] + a[4]*b[1] + a[8]*b[2] + a[12]*b[3]
c[4] = a[0]*b[4] + a[4]*b[5] + a[8]*b[6] + a[12]*b[7]
c[8] = a[0]*b[8] + a[4]*b[9] + a[8]*b[10] + a[12]*b[11]
c[12] = a[0]*b[12] + a[4]*b[13] + a[8]*b[14] + a[12]*b[15]
c[1] = a[1]*b[0] + a[5]*b[1] + a[9]*b[2] + a[13]*b[3]
c[5] = a[1]*b[4] + a[5]*b[5] + a[9]*b[6] + a[13]*b[7]
c[9] = a[1]*b[8] + a[5]*b[9] + a[9]*b[10] + a[13]*b[11]
c[13] = a[1]*b[12] + a[5]*b[13] + a[9]*b[14] + a[13]*b[15]
c[2] = a[2]*b[0] + a[6]*b[1] + a[10]*b[2] + a[14]*b[3]
c[6] = a[2]*b[4] + a[6]*b[5] + a[10]*b[6] + a[14]*b[7]
c[10] = a[2]*b[8] + a[6]*b[9] + a[10]*b[10] + a[14]*b[11]
c[14] = a[2]*b[12] + a[6]*b[13] + a[10]*b[14] + a[14]*b[15]
c[3] = a[3]*b[0] + a[7]*b[1] + a[11]*b[2] + a[15]*b[3]
c[7] = a[3]*b[4] + a[7]*b[5] + a[11]*b[6] + a[15]*b[7]
c[11] = a[3]*b[8] + a[7]*b[9] + a[11]*b[10] + a[15]*b[11]
c[15] = a[3]*b[12] + a[7]*b[13] + a[11]*b[14] + a[15]*b[15]
}
func Load_rotation_mat4(mat *Mat4, v Vec3, angle float32) {
var (
s float32
c float32
xx float32
yy float32
zz float32
xy float32
yz float32
zx float32
xs float32
ys float32
zs float32
one_c float32
)
s = float32(math.Sin(float64(angle)))
c = float32(math.Cos(float64(angle)))
normalize_vec3(&v)
xx = v.X * v.X
yy = v.Y * v.Y
zz = v.Z * v.Z
xy = v.X * v.Y
yz = v.Y * v.Z
zx = v.Z * v.X
xs = v.X * s
ys = v.Y * s
zs = v.Z * s
one_c = float32(1.0 - float64(c))
mat[0] = (one_c * xx) + c
mat[4] = (one_c * xy) - zs
mat[8] = (one_c * zx) + ys
mat[12] = 0.0
mat[1] = (one_c * xy) + zs
mat[5] = (one_c * yy) + c
mat[9] = (one_c * yz) - xs
mat[13] = 0.0
mat[2] = (one_c * zx) - ys
mat[6] = (one_c * yz) + xs
mat[10] = (one_c * zz) + c
mat[14] = 0.0
mat[3] = 0.0
mat[7] = 0.0
mat[11] = 0.0
mat[15] = 1.0
}
func make_viewport_matrix(mat *Mat4, x int64, y int64, width uint64, height uint64, opengl int64) {
var (
w float32
h float32
l float32
t float32
b float32
r float32
)
if opengl != 0 {
w = float32(width)
h = float32(height)
l = float32(x)
b = float32(y)
r = float32(float64(l+w) - 0.01)
t = float32(float64(b+h) - 0.01)
mat[0] = (r - l) / 2
mat[4] = 0
mat[8] = 0
mat[12] = (l + r) / 2
mat[1] = 0
mat[5] = (t - b) / 2
mat[9] = 0
mat[13] = (b + t) / 2
mat[2] = 0
mat[6] = 0
mat[10] = 1
mat[14] = 0
mat[3] = 0
mat[7] = 0
mat[11] = 0
mat[15] = 1
} else {
w = float32(width)
h = float32(height)
l = float32(float64(x) - 0.5)
b = float32(float64(y) - 0.5)
r = l + w
t = b + h
mat[0] = (r - l) / 2
mat[4] = 0
mat[8] = 0
mat[12] = (l + r) / 2
mat[1] = 0
mat[5] = (t - b) / 2
mat[9] = 0
mat[13] = (b + t) / 2
mat[2] = 0
mat[6] = 0
mat[10] = 1
mat[14] = 0
mat[3] = 0
mat[7] = 0
mat[11] = 0
mat[15] = 1
}
}
func make_pers_matrix(mat *Mat4, z_near float32, z_far float32) {
mat[0] = z_near
mat[4] = 0
mat[8] = 0
mat[12] = 0
mat[1] = 0
mat[5] = z_near
mat[9] = 0
mat[13] = 0
mat[2] = 0
mat[6] = 0
mat[10] = z_near + z_far
mat[14] = z_far * z_near
mat[3] = 0
mat[7] = 0
mat[11] = float32(-1)
mat[15] = 0
}
func Make_perspective_matrix(mat *Mat4, fov float32, aspect float32, n float32, f float32) {
var (
t float32 = n * float32(math.Tan(float64(fov)*0.5))
b float32 = -t
l float32 = b * aspect
r float32 = -l
)
make_perspective_proj_matrix(mat, l, r, b, t, n, f)
}
func make_perspective_proj_matrix(mat *Mat4, l float32, r float32, b float32, t float32, n float32, f float32) {
mat[0] = float32((float64(n) * 2.0) / float64(r-l))
mat[4] = 0.0
mat[8] = (r + l) / (r - l)
mat[12] = 0.0
mat[1] = 0.0
mat[5] = float32((float64(n) * 2.0) / float64(t-b))
mat[9] = (t + b) / (t - b)
mat[13] = 0.0
mat[2] = 0.0
mat[6] = 0.0
mat[10] = -((f + n) / (f - n))
mat[14] = float32(-((float64(f*n) * 2.0) / float64(f-n)))
mat[3] = 0.0
mat[7] = 0.0
mat[11] = -1.0
mat[15] = 0.0
}
func make_orthographic_matrix(mat *Mat4, l float32, r float32, b float32, t float32, n float32, f float32) {
mat[0] = float32(2.0 / float64(r-l))
mat[4] = 0
mat[8] = 0
mat[12] = -((r + l) / (r - l))
mat[1] = 0
mat[5] = float32(2.0 / float64(t-b))
mat[9] = 0
mat[13] = -((t + b) / (t - b))
mat[2] = 0
mat[6] = 0
mat[10] = float32(2.0 / float64(f-n))
mat[14] = -((n + f) / (f - n))
mat[3] = 0
mat[7] = 0
mat[11] = 0
mat[15] = 1
}
func lookAt(mat *Mat4, eye Vec3, center Vec3, up Vec3) {
*mat = Mat4{}
mat[0] = 1
mat[5] = 1
mat[10] = 1
mat[15] = 1
var f Vec3 = Norm_vec3(sub_vec3s(center, eye))
var s Vec3 = Norm_vec3(cross_product(f, up))
var u Vec3 = cross_product(s, f)
setx_mat4v3(mat, s)
sety_mat4v3(mat, u)
setz_mat4v3(mat, negate_vec3(f))
setc4_mat4v3(mat, make_vec3(-Dot_vec3s(s, eye), -Dot_vec3s(u, eye), Dot_vec3s(f, eye)))
}
const CVEC_float_SZ uint64 = 50
var c *GlContext
func boolToInt(b bool) int {
if b {
return 1
}
return 0
}
func gl_clipcode(pt Vec4) int64 {
var w float32
w = float32(float64(pt.W) * (1.0 + 1e-05))
return ((int64(boolToInt(pt.Z < -w)) | int64(boolToInt(pt.Z > w))<<1) & (int64(boolToInt(c.Depth_clamp == 0)) | int64(boolToInt(c.Depth_clamp == 0))<<1)) | int64(boolToInt(pt.X < -w))<<2 | int64(boolToInt(pt.X > w))<<3 | int64(boolToInt(pt.Y < -w))<<4 | int64(boolToInt(pt.Y > w))<<5
}
func is_front_facing(v0 *glVertex, v1 *glVertex, v2 *glVertex) int64 {
var (
normal Vec3
tmpvec3 = Vec3{X: 0, Y: 0, Z: 1}
p0 = vec4_to_vec3h(v0.Screen_space)
p1 = vec4_to_vec3h(v1.Screen_space)
p2 = vec4_to_vec3h(v2.Screen_space)
)
normal = cross_product(sub_vec3s(p1, p0), sub_vec3s(p2, p0))
if c.Front_face == GLenum(CW) {
normal = negate_vec3(normal)
}
if Dot_vec3s(normal, tmpvec3) <= 0 {
return 0
}
return 1
}
func do_vertex(v []glVertex_Attrib, enabled []int64, num_enabled uint64, i uint64, vert uint64) {
var (
buf GLuint
buf_pos []u8
tmpvec4 Vec4
)
for j := int64(0); uint64(j) < num_enabled; j++ {
buf = GLuint(v[enabled[j]].Buf)
buf_pos = c.Buffers[buf].Data[v[enabled[j]].Offset+GLsizei(uint64(v[enabled[j]].Stride)*i):]
tmpvec4.X = 0.0
tmpvec4.Y = 0.0
tmpvec4.Z = 0.0
tmpvec4.W = 1.0
var b = *(*[]byte)(unsafe.Pointer(&buf_pos))
switch v[enabled[j]].Size {
case 4:
tmpvec4.W = math.Float32frombits(binary.LittleEndian.Uint32(b[12:]))
fallthrough
case 3:
tmpvec4.Z = math.Float32frombits(binary.LittleEndian.Uint32(b[8:]))
fallthrough
case 2:
tmpvec4.Y = math.Float32frombits(binary.LittleEndian.Uint32(b[4:]))
fallthrough
case 1:
tmpvec4.X = math.Float32frombits(binary.LittleEndian.Uint32(b))
}
c.Vertex_attribs_vs[enabled[j]] = tmpvec4
}
var vs_out = &c.Vs_output.Output_buf[vert*uint64(c.Vs_output.Size)]
c.Programs[c.Cur_program].Vertex_shader(vs_out, unsafe.Pointer(&c.Vertex_attribs_vs[0]), &c.Builtins, c.Programs[c.Cur_program].Uniform)
c.Glverts[vert].Vs_out = unsafe.Slice(vs_out, c.Vs_output.Size)
c.Glverts[vert].Clip_space = c.Builtins.Gl_Position
c.Glverts[vert].Edge_flag = 1
c.Glverts[vert].Clip_code = gl_clipcode(c.Builtins.Gl_Position)
}
func vertex_stage(first GLint, count GLsizei, instance_id GLsizei, base_instance GLuint, use_elements GLboolean) {
var (
i uint64
j uint64
vert uint64
num_enabled uint64
tmpvec4 Vec4
buf_pos []u8
vec4_init = Vec4{0.0, 0.0, 0.0, 1.0}
enabled [MAX_VERTEX_ATTRIBS]int64
)
var v = c.Vertex_arrays[c.Cur_vertex_array].Vertex_attribs[:]
var elem_buffer = c.Vertex_arrays[c.Cur_vertex_array].Element_buffer
for i, j = 0, 0; i < MAX_VERTEX_ATTRIBS; i++ {
c.Vertex_attribs_vs[i] = vec4_init
if v[i].Enabled {
if v[i].Divisor == 0 {
enabled[j] = int64(i)
j++
} else if (instance_id % GLsizei(v[i].Divisor)) == 0 {
var n = int64(instance_id/GLsizei(v[i].Divisor) + GLsizei(base_instance))
buf_pos = c.Buffers[v[i].Buf].Data[v[i].Offset+v[i].Stride*GLsizei(n):]
tmpvec4.X = 0.0
tmpvec4.Y = 0.0
tmpvec4.Z = 0.0
tmpvec4.W = 1.0
var b = *(*[]byte)(unsafe.Pointer(&buf_pos))
switch v[enabled[j]].Size {
case 4:
tmpvec4.W = math.Float32frombits(binary.LittleEndian.Uint32(b[12:]))
fallthrough
case 3:
tmpvec4.Z = math.Float32frombits(binary.LittleEndian.Uint32(b[8:]))
fallthrough
case 2:
tmpvec4.Y = math.Float32frombits(binary.LittleEndian.Uint32(b[4:]))
fallthrough
case 1:
tmpvec4.X = math.Float32frombits(binary.LittleEndian.Uint32(b))
}
c.Vertex_attribs_vs[i] = tmpvec4
}
}
}
num_enabled = j
if GLsizei(len(c.Glverts)) < count {
var tmp = make([]glVertex, count)
copy(tmp, c.Glverts)
c.Glverts = tmp
}
c.Builtins.Gl_InstanceID = GLint(instance_id)
if use_elements == 0 {
for vert, i = 0, uint64(first); i < uint64(first+GLint(count)); vert, i = vert+1, i+1 {
do_vertex(v, enabled[:], num_enabled, i, vert)
}
} else {
var (
uint_array []GLuint = unsafe.Slice((*GLuint)(unsafe.Pointer(&c.Buffers[elem_buffer].Data[0])), first+GLint(count))
ushort_array []GLushort = unsafe.Slice((*GLushort)(unsafe.Pointer(&c.Buffers[elem_buffer].Data[0])), first+GLint(count))
ubyte_array []GLubyte = unsafe.Slice((*GLubyte)(unsafe.Pointer(&c.Buffers[elem_buffer].Data[0])), first+GLint(count))
)
if c.Buffers[elem_buffer].Type == GLenum(UNSIGNED_BYTE) {
for vert, i = 0, uint64(0); i < uint64(first+GLint(count)); vert, i = vert+1, i+1 {
do_vertex(v, enabled[:], num_enabled, uint64(ubyte_array[i]), vert)
}
} else if c.Buffers[elem_buffer].Type == GLenum(UNSIGNED_SHORT) {
for vert, i = 0, uint64(0); i < uint64(first+GLint(count)); vert, i = vert+1, i+1 {
do_vertex(v, enabled[:], num_enabled, uint64(ushort_array[i]), vert)
}
} else {
for vert, i = 0, uint64(0); i < uint64(first+GLint(count)); vert, i = vert+1, i+1 {
do_vertex(v, enabled[:], num_enabled, uint64(uint_array[i]), vert)
}
}
}
}
func draw_point(vert *glVertex) {
var (
fs_input [MAX_VERTEX_OUTPUT_COMPONENTS]float32
point Vec3 = vec4_to_vec3h(vert.Screen_space)
)
point.Z = float32((float64(point.Z)-(-1.0))/(1.0-(-1.0))*float64(c.Depth_range_far-c.Depth_range_near) + float64(c.Depth_range_near))
if c.Depth_clamp != 0 {
point.Z = clampf_01(point.Z)
}
copy(fs_input[:], vert.Vs_out[:c.Vs_output.Size])
var x float32 = float32(float64(point.X) + 0.5)
var y float32 = float32(float64(point.Y) + 0.5)
var p_size float32 = float32(c.Point_size)
var origin float32
if c.Point_spr_origin == GLenum(UPPER_LEFT) {
origin = -1.0
} else {
origin = 1.0
}
if p_size <= 1 {
if x < 0 || y < 0 || x >= float32(c.Back_buffer.W) || y >= float32(c.Back_buffer.H) {
return
}
}
for i := float32(y - p_size/2); i < y+p_size/2; i++ {
if i < 0 || i >= float32(c.Back_buffer.H) {
continue
}
for j := float32(x - p_size/2); j < x+p_size/2; j++ {
if j < 0 || j >= float32(c.Back_buffer.W) {
continue
}
c.Builtins.Gl_PointCoord.X = float32((float64(int64(j))+0.5-float64(point.X))/float64(p_size) + 0.5)
c.Builtins.Gl_PointCoord.Y = float32(float64(origin)*(float64(int64(i))+0.5-float64(point.Y))/float64(p_size) + 0.5)
c.Builtins.Gl_FragCoord.X = j
c.Builtins.Gl_FragCoord.Y = i
c.Builtins.Gl_FragCoord.Z = point.Z
c.Builtins.Gl_FragCoord.W = 1 / vert.Screen_space.W
c.Builtins.Discard = FALSE
c.Builtins.Gl_FragDepth = point.Z
c.Programs[c.Cur_program].Fragment_shader(&fs_input[0], &c.Builtins, c.Programs[c.Cur_program].Uniform)
if c.Builtins.Discard == 0 {
draw_pixel(c.Builtins.Gl_FragColor, int64(j), int64(i))
}
}
}
}
func run_pipeline(mode GLenum, first GLint, count GLsizei, instance GLsizei, base_instance GLuint, use_elements GLboolean) {
var (
i uint64
vert uint64
provoke int64
)
if count > MAX_VERTICES {
panic("assert failed")
}
vertex_stage(first, count, instance, base_instance, use_elements)
if mode == GLenum(POINTS) {
for vert, i = 0, uint64(first); i < uint64(first+GLint(count)); i, vert = i+1, vert+1 {
if c.Glverts[vert].Clip_code != 0 {
continue
}
c.Glverts[vert].Screen_space = Mult_mat4_vec4(c.Vp_mat, c.Glverts[vert].Clip_space)
draw_point((*glVertex)(unsafe.Add(unsafe.Pointer(&c.Glverts[0]), unsafe.Sizeof(glVertex{})*uintptr(vert))))
}
} else if mode == GLenum(LINES) {
for vert, i = 0, uint64(first); i < uint64(first+GLint(count)-1); func() uint64 {
i += 2
return func() uint64 {
vert += 2
return vert
}()
}() {
draw_line_clip((*glVertex)(unsafe.Add(unsafe.Pointer(&c.Glverts[0]), unsafe.Sizeof(glVertex{})*uintptr(vert))), (*glVertex)(unsafe.Add(unsafe.Pointer(&c.Glverts[0]), unsafe.Sizeof(glVertex{})*uintptr(vert+1))))
}
} else if mode == GLenum(LINE_STRIP) {
for vert, i = 0, uint64(first); i < uint64(first+GLint(count)-1); func() uint64 {
i++
return func() uint64 {
p := &vert
x := *p
*p++
return x
}()
}() {
draw_line_clip((*glVertex)(unsafe.Add(unsafe.Pointer(&c.Glverts[0]), unsafe.Sizeof(glVertex{})*uintptr(vert))), (*glVertex)(unsafe.Add(unsafe.Pointer(&c.Glverts[0]), unsafe.Sizeof(glVertex{})*uintptr(vert+1))))
}
} else if mode == GLenum(LINE_LOOP) {
for vert, i = 0, uint64(first); i < uint64(first+GLint(count)-1); func() uint64 {
i++
return func() uint64 {
p := &vert
x := *p
*p++
return x
}()
}() {
draw_line_clip((*glVertex)(unsafe.Add(unsafe.Pointer(&c.Glverts[0]), unsafe.Sizeof(glVertex{})*uintptr(vert))), (*glVertex)(unsafe.Add(unsafe.Pointer(&c.Glverts[0]), unsafe.Sizeof(glVertex{})*uintptr(vert+1))))
}
draw_line_clip((*glVertex)(unsafe.Add(unsafe.Pointer(&c.Glverts[0]), unsafe.Sizeof(glVertex{})*uintptr(count-1))), (*glVertex)(unsafe.Add(unsafe.Pointer(&c.Glverts[0]), unsafe.Sizeof(glVertex{})*0)))
} else if mode == GLenum(TRIANGLES) {
if c.Provoking_vert == GLenum(LAST_VERTEX_CONVENTION) {
provoke = 2
} else {
provoke = 0
}
for vert, i = 0, uint64(first); i < uint64(first+GLint(count)-2); i, vert = i+3, vert+3 {
draw_triangle(&c.Glverts[vert], &c.Glverts[vert+1], &c.Glverts[vert+2], vert+uint64(provoke))
}
} else if mode == GLenum(TRIANGLE_STRIP) {
var (
a uint64 = 0
b uint64 = 1
toggle uint64 = 0
)
if c.Provoking_vert == GLenum(LAST_VERTEX_CONVENTION) {
provoke = 0
} else {
provoke = -2
}
for vert = 2; vert < uint64(count); vert++ {
draw_triangle(&c.Glverts[a], &c.Glverts[b], &c.Glverts[vert], vert+uint64(provoke))
if toggle == 0 {
a = vert
} else {
b = vert