yzt
2023-05-26 2f70f6727314edd84d8ec2bfe3ce832803f1ea77
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
import { modernizeShader } from "../../Source/Cesium.js";
 
describe("Renderer/modernizeShader", function () {
  it("adds version string", function () {
    var simple =
      "#define OUTPUT_DECLARATION \n" + "void main() \n" + "{ \n" + "} \n";
    var output = modernizeShader(simple, true);
    var expected = "#version 300 es\n#define WEBGL_2";
    expect(output.startsWith(expected)).toBe(true);
  });
 
  it("replace existing version string", function () {
    var simple =
      "#version 200 es" +
      "#define OUTPUT_DECLARATION \n" +
      "void main() \n" +
      "{ \n" +
      "} \n";
    var output = modernizeShader(simple, true);
    var expected = "#version 300 es\n#define WEBGL_2";
    expect(output.startsWith(expected)).toBe(true);
  });
 
  it("removes extensions", function () {
    var extensions =
      "#define OUTPUT_DECLARATION \n" +
      "#extension GL_EXT_draw_buffers : enable \n" +
      "void main() \n" +
      "{ \n" +
      "} \n";
    var output = modernizeShader(extensions, true);
    var notExpected = "#extension GL_EXT_draw_buffers : enable \n";
    expect(output).not.toContain(notExpected);
  });
 
  it("replace extension macro check with WEBGL_2", function () {
    var extensions =
      "#define OUTPUT_DECLARATION \n" +
      "#extension GL_EXT_draw_buffers : enable \n" +
      "void main() \n" +
      "{ \n" +
      "    #ifdef GL_EXT_draw_buffers\n" +
      "    #endif //GL_EXT_draw_buffers\n" +
      "    #if defined(GL_EXT_draw_buffers)\n" +
      "    #endif //GL_EXT_draw_buffers\n" +
      "} \n";
    var output = modernizeShader(extensions, true);
    var notExpected = "#extension GL_EXT_draw_buffers : enable \n";
    var expected =
      "    #ifdef WEBGL_2\n" +
      "    #endif //WEBGL_2\n" +
      "    #if defined(WEBGL_2)\n" +
      "    #endif //WEBGL_2\n";
    expect(output).not.toContain(notExpected);
    expect(output).toContain(expected);
  });
 
  it("throws exception if no output declaration", function () {
    var noOutputDeclaration = "void main() \n" + "{ \n" + "} \n";
    var runFunc = function () {
      modernizeShader(noOutputDeclaration, true);
    };
    expect(runFunc).toThrowDeveloperError();
  });
 
  it("creates layout qualifier for gl_FragColor", function () {
    var noQualifiers =
      "#define OUTPUT_DECLARATION \n" +
      "void main() \n" +
      "{ \n" +
      "    gl_FragColor = vec4(0.0); \n" +
      "} \n";
    var output = modernizeShader(noQualifiers, true);
    var expected = "layout(location = 0) out vec4 czm_fragColor;";
    expect(output).toContain(expected);
  });
 
  it("creates layout qualifier for gl_FragData", function () {
    var noQualifiers =
      "#define OUTPUT_DECLARATION \n" +
      "void main() \n" +
      "{ \n" +
      "    gl_FragData[0] = vec4(0.0); \n" +
      "} \n";
    var output = modernizeShader(noQualifiers, true);
    var expected = "layout(location = 0) out vec4 czm_out0;";
    expect(output).toContain(expected);
  });
 
  it("creates layout qualifier for MRT", function () {
    var noQualifiers =
      "#define OUTPUT_DECLARATION \n" +
      "#extension GL_EXT_draw_buffers : enable \n" +
      "void main() \n" +
      "{ \n" +
      "    gl_FragData[0] = vec4(0.0); \n" +
      "    gl_FragData[1] = vec4(1.0); \n" +
      "} \n";
    var output = modernizeShader(noQualifiers, true);
    var expected0 = "layout(location = 0) out vec4 czm_out0;";
    var expected1 = "layout(location = 1) out vec4 czm_out1;";
    expect(output).toContain(expected0);
    expect(output).toContain(expected1);
  });
 
  it("does not create layout qualifier for reserved word lookalike variables", function () {
    var noQualifiers =
      "#define OUTPUT_DECLARATION \n" +
      "uniform sampler2D example; \n" +
      "void main() \n" +
      "{ \n" +
      "    vec4 gl_FragData_ = vec4(0.0); \n" +
      "    vec4 a_gl_FragData = vec4(0.0); \n" +
      "    vec2 thisIsNotAGoodNameForAtexture2D = vec2(0.0); \n" +
      "    vec4 gl_FragColor = texture2D(example, thisIsNotAGoodNameForAtexture2D); \n" +
      "} \n";
    var output = modernizeShader(noQualifiers, true);
    var expectedBadName = "vec2 thisIsNotAGoodNameForAtexture2D";
    var expectedVariable = "vec4 a_gl_FragData = vec4(0.0);";
    var expectedTextureCall =
      "texture(example, thisIsNotAGoodNameForAtexture2D)";
    var notExpectedLayout = "layout(location = 0) out czm_out";
    expect(output).toContain(expectedBadName);
    expect(output).toContain(expectedVariable);
    expect(output).toContain(expectedTextureCall);
    expect(output).not.toContain(notExpectedLayout);
  });
 
  it("creates layout qualifier with swizzle", function () {
    var noQualifiers =
      "#define OUTPUT_DECLARATION \n" +
      "#extension GL_EXT_draw_buffers : enable \n" +
      "void main() \n" +
      "{ \n" +
      "    gl_FragData[0] = vec4(0.0); \n" +
      "    gl_FragData[1].xyz = vec3(1.0); \n" +
      "} \n";
    var output = modernizeShader(noQualifiers, true);
    var expected0 = "layout(location = 0) out vec4 czm_out0;";
    var expected1 = "layout(location = 1) out vec4 czm_out1;";
    expect(output).toContain(expected0);
    expect(output).toContain(expected1);
  });
 
  it("removes old functions/variables from fragment shader", function () {
    var old_fragment =
      "#define OUTPUT_DECLARATION \n" +
      "#extension GL_EXT_draw_buffers : enable \n" +
      "uniform sampler2D example; \n" +
      "uniform sampler2D exampleCube; \n" +
      "uniform sampler2D example3D; \n" +
      "varying vec2 v_textureCoordinates; \n" +
      "void main() \n" +
      "{ \n" +
      "    gl_FragData[0] = texture2D(example, v_textureCoordinates); \n" +
      "    gl_FragData[1] = textureCube(exampleCube, v_textureCoordinates); \n" +
      "    gl_FragData[2] = texture3D(example3D, v_textureCoordinates); \n" +
      "    gl_FragDepthEXT = 0.0; \n" +
      "} \n";
 
    var output = modernizeShader(old_fragment, true);
 
    var expectedDepth = "gl_FragDepth = 0.0;";
    var expectedTexture2D = "texture(example, v_textureCoordinates);";
    var expectedTextureCube = "texture(exampleCube, v_textureCoordinates);";
    var expectedTexture3D = "texture(example3D, v_textureCoordinates);";
    var expectedIn = "in vec2 v_textureCoordinates;";
 
    var notExpectedDepth = "gl_FragDepthEXT = 0.0;";
    var notExpectedTexture2D = "texture2D(example, v_textureCoordinates);";
    var notExpectedTextureCube =
      "textureCube(exampleCube, v_textureCoordinates);";
    var notExpectedTexture3D = "texture3D(example3D, v_textureCoordinates);";
    var notExpectedVarying = "varying vec2 v_textureCoordinates;";
 
    expect(output).toContain(expectedDepth);
    expect(output).toContain(expectedTexture2D);
    expect(output).toContain(expectedTextureCube);
    expect(output).toContain(expectedTexture3D);
    expect(output).toContain(expectedIn);
 
    expect(output).not.toContain(notExpectedDepth);
    expect(output).not.toContain(notExpectedTexture2D);
    expect(output).not.toContain(notExpectedTextureCube);
    expect(output).not.toContain(notExpectedTexture3D);
    expect(output).not.toContain(notExpectedVarying);
  });
 
  it("removes old functions/variables from vertex shader", function () {
    var old_vertex =
      "#define OUTPUT_DECLARATION \n" +
      "attribute vec4 position; \n" +
      "varying vec4 varyingVariable; \n" +
      "void main() \n" +
      "{ \n" +
      "    gl_Position = position; \n" +
      "    varyingVariable = position.wzyx; \n" +
      "} \n";
 
    var output = modernizeShader(old_vertex, false);
 
    var expectedOut = "out vec4 varyingVariable;";
    var expectedIn = "in vec4 position;";
 
    var notExpectedAttribute = "attribute vec4 varyingVariable;";
    var notExpectedVarying = "varying vec2 varyingVariable;";
 
    expect(output).toContain(expectedOut);
    expect(output).toContain(expectedIn);
 
    expect(output).not.toContain(notExpectedAttribute);
    expect(output).not.toContain(notExpectedVarying);
  });
 
  it("creates single layout qualifier under single branch, single condition", function () {
    var noQualifiers =
      "#define OUTPUT_DECLARATION \n" +
      "#define EXAMPLE_BRANCH \n" +
      "void main() \n" +
      "{ \n" +
      "    #ifdef EXAMPLE_BRANCH \n" +
      "    gl_FragData[0] = vec4(0.0); \n" +
      "    #endif //EXAMPLE_BRANCH \n" +
      "} \n";
    var output = modernizeShader(noQualifiers, true);
    var expected =
      "#ifdef EXAMPLE_BRANCH \nlayout(location = 0) out vec4 czm_out0;\n#endif";
    expect(output).toContain(expected);
  });
 
  it("creates multiple layout qualifiers under single branch, single condition", function () {
    var noQualifiers =
      "#define OUTPUT_DECLARATION \n" +
      "#define EXAMPLE_BRANCH \n" +
      "void main() \n" +
      "{ \n" +
      "    #ifdef EXAMPLE_BRANCH \n" +
      "    gl_FragData[0] = vec4(0.0); \n" +
      "    gl_FragData[1] = vec4(1.0); \n" +
      "    #endif //EXAMPLE_BRANCH \n" +
      "} \n";
    var output = modernizeShader(noQualifiers, true);
    var expected0 =
      "#ifdef EXAMPLE_BRANCH \nlayout(location = 0) out vec4 czm_out0;\n#endif";
    var expected1 =
      "#ifdef EXAMPLE_BRANCH \nlayout(location = 1) out vec4 czm_out1;\n#endif";
    expect(output).toContain(expected0);
    expect(output).toContain(expected1);
  });
 
  it("creates multiple layout qualifiers under multiple branches, single condition (cancels)", function () {
    var noQualifiers =
      "#define OUTPUT_DECLARATION \n" +
      "#define EXAMPLE_BRANCH \n" +
      "void main() \n" +
      "{ \n" +
      "    #ifdef EXAMPLE_BRANCH \n" +
      "    gl_FragData[0] = vec4(0.0); \n" +
      "    gl_FragData[1] = vec4(1.0); \n" +
      "    #endif //EXAMPLE_BRANCH \n" +
      "    #ifndef EXAMPLE_BRANCH \n" +
      "    gl_FragData[0] = vec4(0.0); \n" +
      "    gl_FragData[1] = vec4(1.0); \n" +
      "    #endif //!EXAMPLE_BRANCH \n" +
      "} \n";
    var output = modernizeShader(noQualifiers, true);
    var notExpected =
      "#ifdef EXAMPLE_BRANCH \nlayout(location = 0) out vec4 czm_out0;\n#endif";
    expect(output).not.toContain(notExpected);
  });
 
  it("creates single layout qualifier under multiple branches, multiple conditions (cancels)", function () {
    var noQualifiers =
      "#define OUTPUT_DECLARATION \n" +
      "#define EXAMPLE_BRANCH \n" +
      "#define EXAMPLE_BRANCH1 \n" +
      "void main() \n" +
      "{ \n" +
      "    #ifdef EXAMPLE_BRANCH \n" +
      "    gl_FragData[0] = vec4(0.0); \n" +
      "    #endif //EXAMPLE_BRANCH \n" +
      "    #ifdef EXAMPLE_BRANCH1 \n" +
      "    gl_FragData[0] = vec4(0.0); \n" +
      "    #endif //EXAMPLE_BRANCH1 \n" +
      "} \n";
    var output = modernizeShader(noQualifiers, true);
    var notExpected =
      "#ifdef EXAMPLE_BRANCH \nlayout(location = 0) out vec4 czm_out0;\n#endif";
    expect(output).not.toContain(notExpected);
  });
 
  it("creates multiple layout qualifiers under multiple branches, multiple conditions (cascades)", function () {
    var noQualifiers =
      "#define OUTPUT_DECLARATION \n" +
      "#define EXAMPLE_BRANCH \n" +
      "#define EXAMPLE_BRANCH1 \n" +
      "void main() \n" +
      "{ \n" +
      "    #ifdef EXAMPLE_BRANCH \n" +
      "    gl_FragData[0] = vec4(0.0); \n" +
      "    #ifdef EXAMPLE_BRANCH1 \n" +
      "    gl_FragData[1] = vec4(1.0); \n" +
      "    #endif //EXAMPLE_BRANCH1 \n" +
      "    #endif //EXAMPLE_BRANCH \n" +
      "} \n";
    var output = modernizeShader(noQualifiers, true);
    var expected0 =
      "#ifdef EXAMPLE_BRANCH \nlayout(location = 0) out vec4 czm_out0;\n#endif";
    var expected1 = /#ifdef (EXAMPLE_BRANCH|EXAMPLE_BRANCH1)\s*\n\s*#ifdef (EXAMPLE_BRANCH1|EXAMPLE_BRANCH)\s*\n\s*layout\(location = 1\) out vec4 czm_out1;/g;
    var containsExpected0 = expected1.test(output);
    expect(output).toContain(expected0);
    expect(containsExpected0).toBe(true);
  });
 
  it("creates single layout qualifier under multiple branches, single condition (else)", function () {
    var noQualifiers =
      "#define OUTPUT_DECLARATION \n" +
      "#define EXAMPLE_BRANCH \n" +
      "void main() \n" +
      "{ \n" +
      "    #ifdef EXAMPLE_BRANCH \n" +
      "    gl_FragData[0] = vec4(0.0); \n" +
      "    #else \n" +
      "    gl_FragData[0] = vec4(1.0); \n" +
      "    #endif //EXAMPLE_BRANCH \n" +
      "} \n";
    var output = modernizeShader(noQualifiers, true);
    var notExpected =
      "#ifdef EXAMPLE_BRANCH \nlayout(location = 0) out vec4 czm_out0;\n#endif";
    expect(output).not.toContain(notExpected);
  });
 
  it("creates branched layout qualifiers for gl_FragColor and gl_FragData", function () {
    var noQualifiers =
      "#define OUTPUT_DECLARATION \n" +
      "#define EXAMPLE_BRANCH \n" +
      "void main() \n" +
      "{ \n" +
      "    #ifdef EXAMPLE_BRANCH \n" +
      "    gl_FragData[0] = vec4(0.0); \n" +
      "    #else \n" +
      "    gl_FragColor = vec4(1.0); \n" +
      "    #endif //EXAMPLE_BRANCH \n" +
      "} \n";
    var output = modernizeShader(noQualifiers, true);
    var expected0 =
      "#ifdef EXAMPLE_BRANCH \nlayout(location = 0) out vec4 czm_out0;\n#endif";
    var expected1 =
      "#ifndef EXAMPLE_BRANCH \nlayout(location = 0) out vec4 czm_fragColor;\n#endif";
    expect(output).toContain(expected0);
    expect(output).toContain(expected1);
  });
});