-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebgl.n
More file actions
1307 lines (1129 loc) · 35.8 KB
/
webgl.n
File metadata and controls
1307 lines (1129 loc) · 35.8 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
import math
import track
vec3Rect( x, y, w, h, z = 0 ){
return vec3[6](
x,y,z, x+w,y,z, x,y+h,z
x+w,y,z, x+w,y+h,z, x,y+h,z
)
}
vec4Rect( x, y, w, h, z = 0, ww = 1 ){
return vec4[6](
x,y,z,ww, x+w,y,z,ww, x,y+h,z,ww
x+w,y,z,ww, x+w,y+h,z,ww, x,y+h,z,ww
)
}
class GLNode extends Proxy{
// make a singleton copy of Time
this.Time = Time{}
clearAll:( vec3 col ){
gl.clearColor(col[0], col[1], col[2], 1.0)
gl.colorMask(true, true, true, true)
gl.clear(gl.COLOR_BUFFER_BIT|gl.DEPTH_BUFFER_BIT)
gl.enable(gl.DEPTH_TEST)
gl.depthFunc(gl.LESS)
}
// default draw
draw:(){
draw(this)
}
init:(){
if(!owner.child) owner.child = []
owner.child.push(this)
}
// the proxy code generator
proxy(){
// so lets stringify it.
var code = draw.proxy_code
if(code) return Proxy::proxy() + code
code = ''
var ast = draw.value
// make a fresh scope and signals store
var js = this.GenDrawJS
js.new_state()
// plug the module of the ast node
js.module = ast.module
js.context = this
js.proxy_props = proxy_props = {}
js.proxy_refs = proxy_refs = {'owner':1}
var proxy_bind = js.proxy_bind = {}
js.uid = 0
if(ast.type != 'Function') throw new Error('GL Draw not a function')
var flags = js.pull_flags(ast)
code += 'this.draw = ' + js.Function(ast, undefined, ['gl']) + '\n'
// we have to generate redraw binding code for proxy_bind
var rem = 'this.remove = function(){\n'
for(var obj in proxy_bind){
var objprops = proxy_bind[obj]
for(var prop in objprops){
var pname = 'this.' + obj + '.' + prop
code += 'if(' + pname + '_hook)' + pname + '_hook(this)\n'
rem += '\tif(' + pname + '_unhook)' + pname + '_unhook(this)\n'
}
}
rem += '}\n'
code += rem
if(flags.indexOf('js') != -1) out(code)
// prepend static type_methods
for(var k in js.type_methods){
code = js.type_methods[k] + code
}
draw.proxy_remote = code
return Proxy::proxy() + code
}
type: triangle
var glsl_variables = {
gl_PointCoord:vec2
gl_FrontFacing:bool
gl_FragCoord:vec4
gl_Position:vec4
gl_PointSize:float
gl_ClipDistance:float
gl_VertexID:int
gl_InstanceID:int
gl_MaxVertexAttribs:int
gl_MaxVertexUniformVectors:int
gl_MaxVaryingVectors:int
gl_MaxVertexTextureImageUnits:int
gl_MaxCombinedTextureImageUnits:int
gl_MaxTextureImageUnits:int
gl_MaxFragmentUniformVectors:int
gl_MaxDrawBuffers:int
}
enum glsl_types{
half, float, double, short, long
mat2, mat3, mat4
vec2, vec3, vec4
ivec2, ivec3, ivec4
bvec2, bvec3, bvec4
hvec2, hvec3, hvec4
dvec2, dvec3, dvec4
fvec2, fvec3, fvec4
sampler2D
samplerCube
}
enum glsl_functions{
sizeof, radians, degrees
sin, cos, tan
asin, acos, atan,
pow, exp, log, exp2, log2
sqrt, inversesqrt
abs, sign, floor, ceil, fract
mod, min, max, clamp
mix, step, smoothstep
length, distance
dot, cross, normalize
faceforward, reflect, refract
matrixCompMult
lessThan, lessThanEqual
greaterThan, greaterThanEqual
equal, notEqual
any, all, not
texture2DLod
texture2DProjLod
textureCubeLod
texture2D
texture2DProj
textureCube
}
var glc = {
DEPTH_BUFFER_BIT:0x100,STENCIL_BUFFER_BIT:0x400,COLOR_BUFFER_BIT:0x4000,
POINTS:0x0,LINES:0x1,LINE_LOOP:0x2,LINE_STRIP:0x3,TRIANGLES:0x4,TRIANGLE_STRIP:0x5,TRIANGLE_FAN:0x6,
ZERO:0x0,ONE:0x1,SRC_COLOR:0x300,ONE_MINUS_SRC_COLOR:0x301,SRC_ALPHA:0x302,ONE_MINUS_SRC_ALPHA:0x303,DST_ALPHA:0x304,ONE_MINUS_DST_ALPHA:0x305,
DST_COLOR:0x306,ONE_MINUS_DST_COLOR:0x307,SRC_ALPHA_SATURATE:0x308,FUNC_ADD:0x8006,BLEND_EQUATION:0x8009,
BLEND_EQUATION_RGB:0x8009,BLEND_EQUATION_ALPHA:0x883d,FUNC_SUBTRACT:0x800a,FUNC_REVERSE_SUBTRACT:0x800b,
BLEND_DST_RGB:0x80c8,BLEND_SRC_RGB:0x80c9,BLEND_DST_ALPHA:0x80ca,BLEND_SRC_ALPHA:0x80cb,CONSTANT_COLOR:0x8001,
ONE_MINUS_CONSTANT_COLOR:0x8002,CONSTANT_ALPHA:0x8003,ONE_MINUS_CONSTANT_ALPHA:0x8004,BLEND_COLOR:0x8005,
ARRAY_BUFFER:0x8892,ELEMENT_ARRAY_BUFFER:0x8893,ARRAY_BUFFER_BINDING:0x8894,ELEMENT_ARRAY_BUFFER_BINDING:0x8895,
STREAM_DRAW:0x88e0,STATIC_DRAW:0x88e4,DYNAMIC_DRAW:0x88e8,BUFFER_SIZE:0x8764,BUFFER_USAGE:0x8765,
CURRENT_VERTEX_ATTRIB:0x8626,FRONT:0x404,BACK:0x405,FRONT_AND_BACK:0x408,
TEXTURE_2D:0xde1,CULL_FACE:0xb44,
BLEND:0xbe2,DITHER:0xbd0,STENCIL_TEST:0xb90,DEPTH_TEST:0xb71,SCISSOR_TEST:0xc11,POLYGON_OFFSET_FILL:0x8037,
SAMPLE_ALPHA_TO_COVERAGE:0x809e,SAMPLE_COVERAGE:0x80a0,NO_ERROR:0x0,
INVALID_ENUM:0x500,INVALID_VALUE:0x501,
INVALID_OPERATION:0x502,OUT_OF_MEMORY:0x505,
CW:0x900,CCW:0x901,LINE_WIDTH:0xb21,ALIASED_POINT_SIZE_RANGE:0x846d,
ALIASED_LINE_WIDTH_RANGE:0x846e,
CULL_FACE_MODE:0xb45,FRONT_FACE:0xb46,
DEPTH_RANGE:0xb70,DEPTH_WRITEMASK:0xb72,
DEPTH_CLEAR_VALUE:0xb73,DEPTH_FUNC:0xb74,
STENCIL_CLEAR_VALUE:0xb91,STENCIL_FUNC:0xb92,STENCIL_FAIL:0xb94,
STENCIL_PASS_DEPTH_FAIL:0xb95,STENCIL_PASS_DEPTH_PASS:0xb96,STENCIL_REF:0xb97,STENCIL_VALUE_MASK:0xb93,
STENCIL_WRITEMASK:0xb98,STENCIL_BACK_FUNC:0x8800,STENCIL_BACK_FAIL:0x8801,STENCIL_BACK_PASS_DEPTH_FAIL:0x8802,
STENCIL_BACK_PASS_DEPTH_PASS:0x8803,STENCIL_BACK_REF:0x8ca3,STENCIL_BACK_VALUE_MASK:0x8ca4,
STENCIL_BACK_WRITEMASK:0x8ca5,
VIEWPORT:0xba2,SCISSOR_BOX:0xc10,COLOR_CLEAR_VALUE:0xc22,COLOR_WRITEMASK:0xc23,
UNPACK_ALIGNMENT:0xcf5,PACK_ALIGNMENT:0xd05,
MAX_TEXTURE_SIZE:0xd33,MAX_VIEWPORT_DIMS:0xd3a,
SUBPIXEL_BITS:0xd50,RED_BITS:0xd52,GREEN_BITS:0xd53,BLUE_BITS:0xd54,ALPHA_BITS:0xd55,DEPTH_BITS:0xd56,STENCIL_BITS:0xd57,
POLYGON_OFFSET_UNITS:0x2a00,POLYGON_OFFSET_FACTOR:0x8038,TEXTURE_BINDING_2D:0x8069,
SAMPLE_BUFFERS:0x80a8,SAMPLES:0x80a9,SAMPLE_COVERAGE_VALUE:0x80aa,SAMPLE_COVERAGE_INVERT:0x80ab,
COMPRESSED_TEXTURE_FORMATS:0x86a3,
DONT_CARE:0x1100,FASTEST:0x1101,NICEST:0x1102,GENERATE_MIPMAP_HINT:0x8192,
BYTE:0x1400,UNSIGNED_BYTE:0x1401,
SHORT:0x1402,UNSIGNED_SHORT:0x1403,INT:0x1404,UNSIGNED_INT:0x1405,FLOAT:0x1406,DEPTH_COMPONENT:0x1902,
ALPHA:0x1906,RGB:0x1907,RGBA:0x1908,LUMINANCE:0x1909,LUMINANCE_ALPHA:0x190a,UNSIGNED_SHORT_4_4_4_4:0x8033,
UNSIGNED_SHORT_5_5_5_1:0x8034,UNSIGNED_SHORT_5_6_5:0x8363,
FRAGMENT_SHADER:0x8b30,VERTEX_SHADER:0x8b31,
MAX_VERTEX_ATTRIBS:0x8869,MAX_VERTEX_UNIFORM_VECTORS:0x8dfb,MAX_VARYING_VECTORS:0x8dfc,
MAX_COMBINED_TEXTURE_IMAGE_UNITS:0x8b4d,MAX_VERTEX_TEXTURE_IMAGE_UNITS:0x8b4c,MAX_TEXTURE_IMAGE_UNITS:0x8872,
MAX_FRAGMENT_UNIFORM_VECTORS:0x8dfd,SHADER_TYPE:0x8b4f,DELETE_STATUS:0x8b80,LINK_STATUS:0x8b82,
VALIDATE_STATUS:0x8b83,ATTACHED_SHADERS:0x8b85,ACTIVE_UNIFORMS:0x8b86,ACTIVE_ATTRIBUTES:0x8b89,
SHADING_LANGUAGE_VERSION:0x8b8c,CURRENT_PROGRAM:0x8b8d,NEVER:0x200,LESS:0x201,EQUAL:0x202,LEQUAL:0x203,
GREATER:0x204,NOTEQUAL:0x205,GEQUAL:0x206,ALWAYS:0x207,KEEP:0x1e00,REPLACE:0x1e01,INCR:0x1e02,DECR:0x1e03,
INVERT:0x150a,INCR_WRAP:0x8507,DECR_WRAP:0x8508,VENDOR:0x1f00,RENDERER:0x1f01,VERSION:0x1f02,NEAREST:0x2600,
LINEAR:0x2601,NEAREST_MIPMAP_NEAREST:0x2700,LINEAR_MIPMAP_NEAREST:0x2701,NEAREST_MIPMAP_LINEAR:0x2702,
LINEAR_MIPMAP_LINEAR:0x2703,TEXTURE_MAG_FILTER:0x2800,TEXTURE_MIN_FILTER:0x2801,TEXTURE_WRAP_S:0x2802,
TEXTURE_WRAP_T:0x2803,TEXTURE:0x1702,TEXTURE_CUBE_MAP:0x8513,TEXTURE_BINDING_CUBE_MAP:0x8514,
TEXTURE_CUBE_MAP_POSITIVE_X:0x8515,TEXTURE_CUBE_MAP_NEGATIVE_X:0x8516,TEXTURE_CUBE_MAP_POSITIVE_Y:0x8517,
TEXTURE_CUBE_MAP_NEGATIVE_Y:0x8518,TEXTURE_CUBE_MAP_POSITIVE_Z:0x8519,TEXTURE_CUBE_MAP_NEGATIVE_Z:0x851a,
MAX_CUBE_MAP_TEXTURE_SIZE:0x851c,TEXTURE0:0x84c0,TEXTURE1:0x84c1,TEXTURE2:0x84c2,TEXTURE3:0x84c3,TEXTURE4:0x84c4,
TEXTURE5:0x84c5,TEXTURE6:0x84c6,TEXTURE7:0x84c7,TEXTURE8:0x84c8,TEXTURE9:0x84c9,TEXTURE10:0x84ca,TEXTURE11:0x84cb,
TEXTURE12:0x84cc,TEXTURE13:0x84cd,TEXTURE14:0x84ce,TEXTURE15:0x84cf,TEXTURE16:0x84d0,TEXTURE17:0x84d1,
TEXTURE18:0x84d2,TEXTURE19:0x84d3,TEXTURE20:0x84d4,TEXTURE21:0x84d5,TEXTURE22:0x84d6,TEXTURE23:0x84d7,
TEXTURE24:0x84d8,TEXTURE25:0x84d9,TEXTURE26:0x84da,TEXTURE27:0x84db,TEXTURE28:0x84dc,TEXTURE29:0x84dd,
TEXTURE30:0x84de,TEXTURE31:0x84df,ACTIVE_TEXTURE:0x84e0,REPEAT:0x2901,CLAMP_TO_EDGE:0x812f,MIRRORED_REPEAT:0x8370,
FLOAT_VEC2:0x8b50,FLOAT_VEC3:0x8b51,FLOAT_VEC4:0x8b52,INT_VEC2:0x8b53,INT_VEC3:0x8b54,INT_VEC4:0x8b55,BOOL:0x8b56,
BOOL_VEC2:0x8b57,BOOL_VEC3:0x8b58,BOOL_VEC4:0x8b59,FLOAT_MAT2:0x8b5a,FLOAT_MAT3:0x8b5b,FLOAT_MAT4:0x8b5c,
SAMPLER_2D:0x8b5e,SAMPLER_CUBE:0x8b60,VERTEX_ATTRIB_ARRAY_ENABLED:0x8622,VERTEX_ATTRIB_ARRAY_SIZE:0x8623,
VERTEX_ATTRIB_ARRAY_STRIDE:0x8624,VERTEX_ATTRIB_ARRAY_TYPE:0x8625,VERTEX_ATTRIB_ARRAY_NORMALIZED:0x886a,
VERTEX_ATTRIB_ARRAY_POINTER:0x8645,VERTEX_ATTRIB_ARRAY_BUFFER_BINDING:0x889f,COMPILE_STATUS:0x8b81,LOW_FLOAT:0x8df0,
MEDIUM_FLOAT:0x8df1,HIGH_FLOAT:0x8df2,LOW_INT:0x8df3,MEDIUM_INT:0x8df4,HIGH_INT:0x8df5,FRAMEBUFFER:0x8d40,
RENDERBUFFER:0x8d41,RGBA4:0x8056,RGB5_A1:0x8057,RGB565:0x8d62,DEPTH_COMPONENT16:0x81a5,STENCIL_INDEX:0x1901,
STENCIL_INDEX8:0x8d48,DEPTH_STENCIL:0x84f9,
RENDERBUFFER_WIDTH:0x8d42,RENDERBUFFER_HEIGHT:0x8d43,
RENDERBUFFER_INTERNAL_FORMAT:0x8d44,RENDERBUFFER_RED_SIZE:0x8d50,RENDERBUFFER_GREEN_SIZE:0x8d51,
RENDERBUFFER_BLUE_SIZE:0x8d52,RENDERBUFFER_ALPHA_SIZE:0x8d53,RENDERBUFFER_DEPTH_SIZE:0x8d54,
RENDERBUFFER_STENCIL_SIZE:0x8d55,
FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE:0x8cd0,FRAMEBUFFER_ATTACHMENT_OBJECT_NAME:0x8cd1,
FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL:0x8cd2,FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE:0x8cd3,
COLOR_ATTACHMENT0:0x8ce0,DEPTH_ATTACHMENT:0x8d00,STENCIL_ATTACHMENT:0x8d20,DEPTH_STENCIL_ATTACHMENT:0x821a,
NONE:0x0,FRAMEBUFFER_COMPLETE:0x8cd5,FRAMEBUFFER_INCOMPLETE_ATTACHMENT:0x8cd6,
FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT:0x8cd7,FRAMEBUFFER_INCOMPLETE_DIMENSIONS:0x8cd9,
FRAMEBUFFER_UNSUPPORTED:0x8cdd,FRAMEBUFFER_BINDING:0x8ca6,RENDERBUFFER_BINDING:0x8ca7,
MAX_RENDERBUFFER_SIZE:0x84e8,INVALID_FRAMEBUFFER_OPERATION:0x506,UNPACK_FLIP_Y_WEBGL:0x9240,
UNPACK_PREMULTIPLY_ALPHA_WEBGL:0x9241,CONTEXT_LOST_WEBGL:0x9242,UNPACK_COLORSPACE_CONVERSION_WEBGL:0x9243,
BROWSER_DEFAULT_WEBGL:0x9244
}
var glsl_uniform_map = {
float:a -> 'uniform1f(' + a + ',v)'
vec2: a -> 'uniform2fv(' + a + ',v)'
vec3: a -> 'uniform3fv(' + a + ',v)'
vec4: a -> 'uniform4fv(' + a + ',v)'
mat4: a -> 'uniformMatrix4fv(' + a + ',false,v)'
}
class DepTraceGLSL extends AST.Walk{
// causes dependency marks to travel up the tree
// uniform has mark 1
// attribute has mark 2
// pixel has mark 3
Pre( n ){
n.infer = undefined
n.mark = undefined
}
Post( n, omark ){
// lets propagate up our mark to our parent
var parent = n.parent
var nmark = omark || n.mark
if(parent){
if(nmark){
var pmark = parent.mark
if(!pmark) parent.mark = nmark
else if(pmark < nmark) parent.mark = nmark
}
if(!parent.infer) parent.infer = n.infer
}
}
// i must dry implement Do
Do( n, parent ){
n.parent = parent
Pre(n)
AST.Walk::Do(n, parent)
// fuse mark properly
if(n.arg.mark > n.call.mark) n.call.mark = n.arg.mark
}
Id( n, parent ){
n.parent = parent
Pre(n)
var name = n.name
if(n.flag == 35){
n.infer = vec3
return Post(n)// is color
}
if(macro_args && macro_args[name]){
var type = macro_args[name].infer
n.infer = type
return Post(n)
}
if(scope && scope[name]){
var type = scope[name]
n.infer = type
return Post(n)
}
var type = glsl_variables[name]
if(type){
if(name == 'gl_FragCoord') n.mark = 3
n.infer = type
return Post(n)
}
// static type method or property?
var type = find_type(name)
if(type){
n.infer = type
return Post(n)
}
// check if we are a macro?
var def = this.find_define(name)
if(def){
this[def.type](def, n)
return Post(n)
}
// lets resolve name
var prop = context[name]
if(prop === undefined) throw new Error('Cannot resolve ' + name)
// we are a reference to a remote object
if(prop.proxy_uid){
throw new Error('Cannot reference bare remote objects in GLSL')
}
if(prop._signal_){
// what if we are a normal property with a value?..
// we will need to
if(!prop.value._ast_) throw new Error('invalid property ' + name)
// we have a bind
// check if its a value, or a fully static type constructor
if(prop.value.type == 'Value'){
if(prop.value.kind != 'num') throw new Error('invalid type in DepTraceGLSL ' + prop.value.kind)
n.mark = prop.value.mark = 1
n.infer = prop.value.infer = float
// lets replace the property with the actual value.
prop.value = prop.value.value
// unbind the expression
//prop.value = undefined
}
else {
// if we reference a property more than once
// we need to store it in a temp variable
if(this.refcount[name]) this.refcount[name]++
else this.refcount[name] = 1
var mod = this.module
this.module = prop.value.module
this[prop.value.type](prop.value, n)
this.module = mod
}
}
else {
//TODO turn properties into signals here!
//log(prop)
if(typeof prop == 'object' && typeof prop.t == 'object'){
n.mark = 1
n.infer = prop.t
}
else if(typeof prop == 'number'){
n.mark = 1
n.infer = float
}
else throw new Error('Add other property types ' + name)
}
if(n.kind){
// lets set our 'infer' to this type
if(n.kind.name == 'pixel'){
n.mark = 3 // mark us as pixel
}
else {
// where do we get our typemap?
n.infer = module.types[n.kind.name]
if(!n.infer) throw new Error('cannot find type ' + n.kind.name)
}
}
Post(n)
}
this.Function( n, parent ){
// we should only do our body, not our params
n.parent = parent
//Pre(n)
var body = n.body
if(body) this[body.type](body, n)
//Post(n)
}
Def( n, parent ){
n.parent = parent
var type
if(n.init) this[n.init.type](n.init, n)
if(n.parent.type == 'Var'){
if(!n.init) throw new Error('Cannot use uninitialized var')
type = n.init.infer
}
else if(n.parent.type === 'TypeVar'){
var kind = n.parent.kind
if(kind.type == 'Index'){
var name = kind.object.name
var type = this.find_type(name)
type = Object.create(type)
type.dim = 1
}
else{
var name = kind.name
type = this.find_type(name)
}
}
else throw new Error('Unsupported Def encountered')
n.infer = scope[n.id.name] = type
}
Return( n, parent ){
// set return type
n.parent = parent
Pre(n)
if(n.arg){
this[n.arg.type](n.arg, n)
}
if(return_type && return_type != n.infer){
throw new Error('Multiple returntypes detected '+return_type.name + ' and ' + n.infer)
}
return_type = n.infer
Post(n)
}
// if we get a return
// we should
// type inference calls
Call( n, parent ){
n.parent = parent
Pre(n)
// alright. lets check if we have a type Id
if(n.fn.type == 'Id'){
// check
var name = n.fn.name
var type = find_type(name)
// type infer the args
for(var arg from n.args) this[arg.type](arg, n)
if(type){
n.infer = type
// lets do the args
return Post(n)
}
// lets check if we are calling a macro
n.args.expanded = true
//console.log(n.args, name)
var [macro, macro_generics] = find_macro(n, name, n.args)
if(macro){
// we are a macro
if(macro.type == 'Function'){
// lets type infer trace an actual macro call
var old_scope = scope
var old_return = return_type
var old_module = this.module
var old_generics = generics
generics = macro_generics
return_type = undefined
var mparams = macro.params
scope = Object.create(null)
if(macro.module) this.module = macro.module
for(var i, arg from n.args){
if(!arg.infer) throw new Error('Cannot infer arg in macro '+name)
scope[mparams[i].id.name] = arg.infer
}
// lets set the arguments on the scope
this[macro.type](macro, n)
this.module = old_module
generics = old_generics
scope = old_scope
n.infer = return_type
return_type = old_return
}
else if(macro.type == 'Call'){
var old_args = macro_args
old_generics = generics
generics = macro_generics
macro_args = Object.create(null)
var margs = macro.args
for(var i, arg from n.args){
macro_args[margs[i].name] = arg
}
var astnode = macro.parent.value
// type infer trace a macro!
this[astnode.type](astnode, n)
macro_args = old_args
}
else throw new Error('Macro called but not a function '+name)
// we have to figure out a return-type trace on our macro
return Post(n)
}
}
// static type methods
if(n.fn.type == 'Key'){
var obj = n.fn.object
if(obj.type == 'Id'){
var name = obj.name
if(name == 'Math'){
n.infer = float
return Post(n)
}
var type = this.find_type(name)
if(type){
// static method call
for(var arg from n.args) this[arg.type](arg, n)
n.infer = type
return Post(n)
}
}
}
// otherwise it may be a type method, we need to trace it
return AST.Walk::Call(n, parent)
}
Index( n, parent ){
n.parent = parent
Pre(n)
if(!n.index){
var nmark = n.mark = 2 // mark as attribute
if(n.object.kind && n.object.kind.name == 'pixel')
nmark = 3
// what is our our type however?..
var attrib = context[n.object.name]
if(!attrib) throw new Error('cannot fetch attribute '+n.object.name)
n.infer = attrib.t
return Post(n, nmark)
}
return AST.Walk::Index(n)
}
Key( n, parent ){
n.parent = parent
Pre(n)
// we are a *.key
// what we dont want to do is resolve the key.
// we only want to walk the object.
if(n.object.type == 'Id'){
var objname = n.object.name
// check if we are a different object
var obj = context[objname]
if(typeof obj == 'object' && !obj.t){
if(obj.proxy_uid){
// we are depending on another object
var propname = n.key.name
var prop = obj[propname]
// mark signal in our proxy table
n.infer = prop.value && prop.value.t || float
n.mark = 1 // uniform
proxy_refs[objname] = 1
var bind = proxy_bind[objname] || (proxy_bind[objname] = {})
bind[propname] = 1
return Post(n)
}
else if(obj._signal_){
// we might want the other one
// okay so.
}
else throw new Error('Dont know what to do in key deptrace with '+objname)
}
}
// only try to walk/resolve the object
this[n.object.type](n.object, n)
// we have to have an inferred type on object now
if(!n.infer) throw new Error('key access without type')
var fields = n.infer.fields
if(!fields) throw new Error('key access on type without fields')
var type = fields[n.key.name]
if(!type) throw new Error('type '+n.infer.name+' has no field '+n.key.name)
n.infer = type
return Post(n)
}
find_define = AST.ToJS.find_define
find_type = AST.ToJS.find_type
find_macro = AST.ToJS.find_macro
macro_match_args = AST.ToJS.macro_match_args
}
// generate the uniform computation JS
class GenUniformJS extends AST.ToJS{
skip = 1
Id( n ){
var name = n.name
if(n.flag == 35){ // is a color
return 'this.color("' + name + '")'
}
if(name in glsl_functions || name in glsl_variables){
return name
}
var prop = context[name]
if(prop === undefined || this.scope[name]) return this.resolve(n.name, n)
if(prop.value && prop.value._ast_){
var mod = this.module
this.module = prop.value.module
var ret = expand(prop.value, n)
this.module = mod
return ret
}
if(typeof prop == 'object'){
//!TODO convert to signal
proxy_props[name] = 1
return 'this.' + name
}
else if(typeof prop == 'number'){
//!TODO convert to signal
proxy_props[name] = 1
return 'this.' + name
}
else throw new Error('Cant generate uniformJS for '+name)
}
// what if we are a Key and its another objects' signal we need to depend on?
// we need to send over a dependency list
Key( n ){
if(n.object.type == 'Id'){
var objname = n.object.name
// alright so
// we are depending on something
}
return AST.ToJS::Key(n)
}
}
// generate the GLSL
class GenGLSL extends AST.ToCode{
term = ';'
// otherwise if we are not 'pixel' we need to spit out varyings.
expand( n, parent ){
//log(n.type)
if(n.mark == 1){ // we are a uniform dependent expression
// okay
if(!n.infer) throw new Error('Failed to infer type '+n.toDump())
// okay so, we are a uniform. we splice in
// a temp uniform, and we define it
var uni = {
module: this.module
name: (pixel?'pix_':'vtx_') + n.infer.name + '_' + (uniform_id++)
node: n
}
uniforms.push(uni)
return uni.name
}
// output vertex varying
if(pixel && n.mark == 2){
// alright we need to output a vertex varying
var vary = {
name: 'var_'+n.infer.name+'_' + (varying_id++)
node: n
}
varyings.push(vary)
return vary.name
//log('here', n.toDump())
}
// otherwise output self
return AST.ToCode::expand(n, parent)
}
// Id resolver
Id( n ){
var name = n.name
// if we are # bla lets resolve the color and inline.
if(n.flag == 35){ // is a color
vec4 col = ONE.color(name)
return 'vec3(' + col.x + ',' + col.y + ',' + col.z + ')'
}
if(name in glsl_functions || name in glsl_variables){
return name
}
// check macro args
if(macro_args && name in macro_args){
return '('+macro_args[name]+')'
}
if(scope && name in scope){
return name
}
var prop = context[name]
if(prop === undefined) throw new Error("cannot resolve " + name)
// temporary variable generation
if(prop._signal_ && prop.value._ast_){
var node = prop.value
var type = node.type
var old_module = this.module
if(type == 'Id' || type == 'Value' ||
type == 'Key' || (type == 'Index' &&
(!node.index || node.index.type == 'Value'))){
this.module = prop.value.module
var ret = expand(prop.value, n)
this.module = old_module
return ret
}
// use a tempvar
var count = refcount[name]
if(typeof count == 'object'){
// we already have it defined
tmpvars.splice(tmpvars.indexOf(count), 1)
tmpvars.push(count)
return count.name
}
else if(count>1){ // define it
var tmp = {
name:'tmp_' + n.infer.name + '_' + (uniform_id++),
type:n.infer.name,
value:expand(prop.value, n)
}
refcount[name] = tmp
tmpvars.push(tmp)
return tmp.name
}
this.module = prop.value.module
var ret = expand(prop.value, n)
this.module = old_module
return ret
}
throw new Error('Dont know what to do with '+name)
return ''
}
// resolve index
Index( n ){
// so we got a bla[]
if(!n.index){
if(n.object.type != 'Id') throw new Error('dont support property attributes')
var name = n.object.name
var obj = context[name]
if(!obj) throw new Error('Cannot find vertex attribute[] ' + name)
var attr = attribs[name]
if(attr && attr !== obj) throw new Error('Cannot redefine vertex attribute[] ' + name)
attribs[name] = obj
return name // return name
}
return AST.ToCode::Index(n)
}
// we have to resolve Id's
Key( n ){
if(n.object.type == 'Id'){
var objname = n.object.name
if(objname in glsl_variables){
return objname + '.' + n.key.name
}
}
// lets only resolve the object, en just postfix the key
return this.expand(n.object, n) + '.' + n.key.name
}
// type infer int * float
Binary( n ){
if(n.left.type == 'Value' &&
n.left.kind == 'num' &&
n.right.infer.name == 'float' &&
n.left.raw.indexOf('.') == -1){
n.left.raw += '.'
}
if(n.right.type == 'Value' &&
n.right.kind == 'num' &&
n.left.infer.name == 'float' &&
n.right.raw.indexOf('.') == -1){
n.right.raw += '.'
}
return AST.ToCode::Binary(n)
}
Var( n ){
var ret = ''
for(var v from n.defs){
ret += this.Def(v)
}
return ret
}
Def( n ){
var name = n.id.name
scope[name] = n.infer
if(n.parent.type == 'Var'){
return n.infer.name + ' ' + name + ' = ' + this.expand(n.init, n) + this.term + this.newline + this.indent
if(!n.init) throw new Error('Cannot use uninitialized var')
type = n.init.infer
}
else if(n.parent.type === 'TypeVar'){
return name
}
else throw new Error('Unsupported Def encountered')
}
// Function!
this.Function( n, name, argdef ){
// allright lets generate a function!
var ret = n.infer.name + ' ' + name + '(' + argdef + ')'
var old_depth = this.depth
this.depth += this.indent
ret += this.expand(n.body, n)
this.depth = old_depth
return ret
return AST.ToCode::Function(n)
}
// Do calls
Call( n ){
if(n.fn.type == 'Id'){
// check
var name = n.fn.name
var type = find_type(name)
// type constructor
if(type){
var ret = name + '('
for(var i, arg from n.args){
if(i) ret += ','
ret += this[arg.type](arg, n)
}
ret += ')'
return ret
}
// lets check our name against built in
if(name in glsl_functions)
return AST.ToCode::Call(n)
var [macro, macro_generics] = find_macro(n, name, n.args)
if(macro){
if(macro.type == 'Function'){
// alright! we are going to generate an actual
// GLSL function!. huzzah.
var params = macro.params
var gen = 'macro_' + name
var args = ''
var argdef = ''
var old_scope = scope
scope = Object.create(null)
for(var i, arg from n.args){
if(i) args += ', ', argdef += ', '
args += this[arg.type](arg, n)
var type_name = arg.infer.name
var param_name = params[i].id.name
scope[param_name] = arg.infer
argdef += type_name + ' ' + param_name
gen += '_' + type_name
}
if(!type_methods[gen]){
var old_depth = depth
var old_args = macro_args
var old_module = module
var old_generics = generics
generics = macro_generics
macro_args = undefined
depth = '\t\t\t'
if(macro.module) this.module = macro.module
type_methods[gen] = this.Function(macro, gen, argdef)
depth = old_depth
generics = old_generics
macro_args = old_args
this.module = old_module
}
scope = old_scope
return gen + '(' + args + ')'
}
else if(macro.type == 'Call'){
var old_args = macro_args
var old_module = this.module
var old_generics = generics
generics = macro_generics
macro_args = Object.create(null)
var margs = macro.args
for(var i, arg from n.args){
macro_args[margs[i].name] = this[arg.type](arg, n)
}
var astnode = macro.parent.value
this.module = astnode.module
var ret = this[astnode.type](astnode, n)
this.module = old_module
generics = old_generics
macro_args = old_args
return ret
}
}
}
return AST.ToCode::Call(n)
}
macro_match_args = AST.ToJS.macro_match_args
find_macro = AST.ToJS.find_macro
find_define = AST.ToJS.find_define
find_type = AST.ToJS.find_type
}
class GenDrawJS extends AST.ToJS{
context_resolve( name, n ){
if(typeof context[name] !== 'undefined'){
proxy_props[name] = 1
n.infer = context[name].t || float
return 'this.' + name
}
}
_compile_draw( n ){
var uid = 'd' + this.uid++
// forward module and context
GenGLSL.context = context
var attribs = GenGLSL.attribs = Object.create(null)
DepTraceGLSL.context = context
var pix = context.pixel
if(!pix.value || !pix.value._ast_) throw new Error('Unexpected pixel type')
// deptrace the pixelshader
GenGLSL.pixel = DepTraceGLSL.pixel = 1
DepTraceGLSL.proxy_bind = proxy_bind
DepTraceGLSL.proxy_refs = proxy_refs
DepTraceGLSL.module = pix.value.module
GenGLSL.refcount = DepTraceGLSL.refcount = {}
GenGLSL.module = pix.value.module
DepTraceGLSL[pix.value.type](pix.value)
//log(pix.bind.toDump())
// codegen the pixelshader
GenGLSL.uniform_id = 0
GenGLSL.varying_id = 0
GenGLSL.tmpvar_id = 0
var pix_type_methods = GenGLSL.type_methods = {}
var pix_uniforms = GenGLSL.uniforms = []
var varyings = GenGLSL.varyings = []
var pix_tmpvars = GenGLSL.tmpvars = []
var signal_deps = GenGLSL.sigdeps = []
var pix_expr = GenGLSL.expand(pix.value)
// pull the vertex property from our context
var vtx = context.vertex
if(!vtx.value || !vtx.value._ast_) throw new Error('Unexpected vertex type')
// dependency trace vertex shader
GenGLSL.pixel = DepTraceGLSL.pixel = 0