-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.js
165 lines (153 loc) · 4.61 KB
/
test.js
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
var transpile = require('./')
var tokenize = require('glsl-tokenizer')
var test = require('tape')
var stringify = require('glsl-token-string')
test('transpiles GLSL tokens from v100 to v300 es', function (t) {
t.throws(function () {
transpile.vertex(tokenize('#version 300\nvoid main(){}'))
}, 'cannot handle non-es version')
t.throws(function () {
transpile.vertex(tokenize('attribute vec3 texture;\nvoid main(){}'))
}, 'cannot transpile reserved words in attribuets')
t.doesNotThrow(function () {
transpile.vertex(tokenize('attribute vec3 foo;\nvoid main(){ float texture = 1.0; }'))
}, 'support reserved word in vert shader when not attribute')
var shader, result
shader = tokenize([
'#version 100',
'#extension GL_OES_standard_derivatives : enable',
'#extension GL_EXT_draw_buffers:enable',
'#extension GL_EXT_fancy_dancy : enable',
'void main() {}'
].join('\n'))
result = stringify(transpile.vertex(shader))
t.equal(result, [
'#version 300 es',
'#extension GL_EXT_fancy_dancy : enable',
'void main() {}'
].join('\n'), 'removes core extensions')
shader = tokenize([
'#extension GL_EXT_shader_texture_lod : enable',
'uniform samplerCube envMap;',
'varying vec3 input;',
'void main() { gl_FragColor = textureCubeLodEXT(envMap, input, 0.0); }'
].join('\n'))
result = stringify(transpile.fragment(shader))
t.equal(result, [
'#version 300 es',
'out vec4 fragColor;',
'uniform samplerCube envMap;',
'in vec3 input;',
'void main() { fragColor = textureLod(envMap, input, 0.0); }'
].join('\n'), 'handles EXT_shader_texture_lod')
shader = tokenize([
'vec3 transpose () { return vec3(1.0); }',
'void main() {',
'gl_FragColor = vec4(transpose(), 1.0);',
'}'
].join('\n'))
result = stringify(transpile.fragment(shader))
t.equal(result, [
'#version 300 es',
'out vec4 fragColor;',
'vec3 transpose_0 () { return vec3(1.0); }',
'void main() {',
'fragColor = vec4(transpose_0(), 1.0);',
'}'
].join('\n'), 'mangles function names that are matching builtins')
shader = tokenize([
'#version 100',
'varying vec3 sample;',
'void main() {',
'float centroid = 1.0;',
'gl_FragColor = vec4(sample, centroid);',
'}'
].join('\n'))
result = stringify(transpile.fragment(shader))
t.equal(result, [
'#version 300 es',
'out vec4 fragColor;',
'in vec3 sample_0;',
'void main() {',
'float centroid_0 = 1.0;',
'fragColor = vec4(sample_0, centroid_0);',
'}'
].join('\n'), 'handles 300es keywords')
shader = tokenize([
'#version 100',
'#define FOO',
'attribute vec4 position;',
'attribute vec3 texcoords;',
'varying vec2 vUv;',
'void main() {',
'gl_Position = position;',
'}'
].join('\n'))
result = stringify(transpile.vertex(shader))
t.equal(result, [
'#version 300 es',
'#define FOO',
'in vec4 position;',
'in vec3 texcoords;',
'out vec2 vUv;',
'void main() {',
'gl_Position = position;',
'}'
].join('\n'), 'vertex shader')
shader = tokenize([
'#version 100',
'precision mediump float;',
'precision mediump int;',
'void main() {',
'gl_FragColor = vec4(1.0);',
'}'
].join('\n'))
result = stringify(transpile.fragment(shader))
t.equal(result, [
'#version 300 es',
'precision mediump float;',
'precision mediump int;',
'out vec4 fragColor;',
'void main() {',
'fragColor = vec4(1.0);',
'}'
].join('\n'), 'handles precision nicely')
shader = tokenize([
'#version 100',
'#define FOO',
'#extension GL_BLAH_BLAH : enable',
'vec4 foo (in vec3 blah) { return vec4(1.0); }',
'varying vec4 vUv;',
'uniform float fragColor;',
'uniform float fragColor_0;',
'uniform float fragColor_3;',
'void main() {',
'vec4 fragColor_1 = vec4(1.0);',
'gl_FragColor.x *= gl_FragColor.g;',
'gl_FragColor = texture2D(tex, uv);',
'gl_FragColor = textureCube(tex, uv);',
'gl_FragDepth = 2.0;',
'}'
].join('\n'))
result = stringify(transpile.fragment(shader))
t.equal(result, [
'#version 300 es',
'#define FOO',
'#extension GL_BLAH_BLAH : enable',
'out float fragDepth;',
'out vec4 fragColor_2;',
'vec4 foo (in vec3 blah) { return vec4(1.0); }',
'in vec4 vUv;',
'uniform float fragColor;',
'uniform float fragColor_0;',
'uniform float fragColor_3;',
'void main() {',
'vec4 fragColor_1 = vec4(1.0);',
'fragColor_2.x *= fragColor_2.g;',
'fragColor_2 = texture(tex, uv);',
'fragColor_2 = texture(tex, uv);',
'fragDepth = 2.0;',
'}'
].join('\n'), 'fragment shader')
t.end()
})