15832144755
2022-01-06 7b4c8991dca9cf2a809a95e239d144697d3afb56
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
import { Color } from "../../Source/Cesium.js";
import { IndexDatatype } from "../../Source/Cesium.js";
import { Buffer } from "../../Source/Cesium.js";
import { BufferUsage } from "../../Source/Cesium.js";
import { Context } from "../../Source/Cesium.js";
import { ContextLimits } from "../../Source/Cesium.js";
import createContext from "../createContext.js";
 
describe(
  "Renderer/Context",
  function () {
    var context;
 
    beforeAll(function () {
      context = createContext();
    });
 
    afterAll(function () {
      context.destroyForSpecs();
    });
 
    it("has a unique ID", function () {
      var c = createContext();
      expect(c.id).toBeDefined();
      expect(c.id).not.toEqual(context.id);
      c.destroyForSpecs();
    });
 
    it("get canvas", function () {
      expect(context.canvas).not.toBeNull();
    });
 
    it("get stencilBits", function () {
      expect(context.stencilBits).toBeGreaterThanOrEqualTo(0);
    });
 
    it("get maximumCombinedTextureImageUnits", function () {
      expect(
        ContextLimits.maximumCombinedTextureImageUnits
      ).toBeGreaterThanOrEqualTo(8);
    });
 
    it("get maximumCubeMapSize", function () {
      expect(ContextLimits.maximumCubeMapSize).toBeGreaterThanOrEqualTo(16);
    });
 
    it("get maximumFragmentUniformVectors", function () {
      expect(
        ContextLimits.maximumFragmentUniformVectors
      ).toBeGreaterThanOrEqualTo(16);
    });
 
    it("get maximumTextureImageUnits", function () {
      expect(ContextLimits.maximumTextureImageUnits).toBeGreaterThanOrEqualTo(
        8
      );
    });
 
    it("get maximumRenderbufferSize", function () {
      expect(ContextLimits.maximumRenderbufferSize).toBeGreaterThanOrEqualTo(1);
    });
 
    it("get maximumTextureSize", function () {
      expect(ContextLimits.maximumTextureSize).toBeGreaterThanOrEqualTo(64);
    });
 
    it("get maximumVaryingVectors", function () {
      expect(ContextLimits.maximumVaryingVectors).toBeGreaterThanOrEqualTo(8);
    });
 
    it("get maximumVertexAttributes", function () {
      expect(ContextLimits.maximumVertexAttributes).toBeGreaterThanOrEqualTo(8);
    });
 
    it("get maximumVertexTextureImageUnits", function () {
      expect(
        ContextLimits.maximumVertexTextureImageUnits
      ).toBeGreaterThanOrEqualTo(0);
    });
 
    it("get maximumVertexUniformVectors", function () {
      expect(
        ContextLimits.maximumVertexUniformVectors
      ).toBeGreaterThanOrEqualTo(1);
    });
 
    it("get minimumAliasedLineWidth", function () {
      expect(ContextLimits.minimumAliasedLineWidth).toBeLessThanOrEqualTo(1);
    });
 
    it("get maximumAliasedLineWidth", function () {
      expect(ContextLimits.maximumAliasedLineWidth).toBeGreaterThanOrEqualTo(1);
    });
 
    it("get minimumAliasedPointSize", function () {
      expect(ContextLimits.minimumAliasedPointSize).toBeLessThanOrEqualTo(1);
    });
 
    it("get maximumAliasedPointSize", function () {
      expect(ContextLimits.maximumAliasedPointSize).toBeGreaterThanOrEqualTo(1);
    });
 
    it("get maximumViewportWidth", function () {
      expect(ContextLimits.maximumViewportWidth).toBeGreaterThan(0);
    });
 
    it("get maximumViewportHeight", function () {
      expect(ContextLimits.maximumViewportHeight).toBeGreaterThan(0);
    });
 
    it("gets antialias", function () {
      var c = createContext({
        webgl: {
          antialias: false,
        },
      });
      expect(c.antialias).toEqual(false);
      c.destroyForSpecs();
    });
 
    it("gets the standard derivatives extension", function () {
      var fs = "";
 
      if (context.standardDerivatives && !context.webgl2) {
        fs += "#extension GL_OES_standard_derivatives : enable\n";
      }
 
      fs += "void main()\n" + "{\n";
 
      if (context.standardDerivatives) {
        fs += "  gl_FragColor = vec4(dFdx(1.0), dFdy(1.0), 1.0, 1.0);\n";
      } else {
        fs += "  gl_FragColor = vec4(1.0);\n";
      }
 
      fs += "}";
 
      var expected = context.standardDerivatives
        ? [0, 0, 255, 255]
        : [255, 255, 255, 255];
      expect({
        context: context,
        fragmentShader: fs,
      }).contextToRender(expected);
    });
 
    it("gets the element index uint extension", function () {
      if (context.elementIndexUint) {
        var buffer = Buffer.createIndexBuffer({
          context: context,
          sizeInBytes: 6,
          usage: BufferUsage.STREAM_DRAW,
          indexDatatype: IndexDatatype.UNSIGNED_INT,
        });
        expect(buffer).toBeDefined();
        buffer.destroy();
      } else {
        expect(function () {
          Buffer.createIndexBuffer({
            context: context,
            sizeInBytes: 6,
            usage: BufferUsage.STREAM_DRAW,
            indexDatatype: IndexDatatype.UNSIGNED_INT,
          });
        }).toThrowDeveloperError();
      }
    });
 
    it("gets the depth texture extension", function () {
      expect(context.depthTexture).toBeDefined();
    });
 
    it("gets the texture float extension", function () {
      expect(context.floatingPointTexture).toBeDefined();
    });
 
    it("gets texture filter anisotropic extension", function () {
      expect(context.textureFilterAnisotropic).toBeDefined();
    });
 
    it("gets texture filter anisotropic extension", function () {
      expect(context.textureFilterAnisotropic).toBeDefined();
    });
 
    it("gets maximum texture filter anisotropy", function () {
      if (context.textureFilterAnisotropic) {
        expect(
          ContextLimits.maximumTextureFilterAnisotropy
        ).toBeGreaterThanOrEqualTo(2);
      } else {
        expect(ContextLimits.maximumTextureFilterAnisotropy).toEqual(1);
      }
    });
 
    it("gets vertex array object extension", function () {
      expect(context.vertexArrayObject).toBeDefined();
    });
 
    it("get the fragment depth extension", function () {
      if (context.fragmentDepth && !context.webgl2) {
        return;
      }
 
      var fs =
        "void main()\n" +
        "{\n" +
        "  gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);\n" +
        "}";
 
      expect({
        context: context,
        fragmentShader: fs,
        depth: 0.5,
      }).contextToRender([255, 0, 0, 255]);
 
      var fsFragDepth = "#extension GL_EXT_frag_depth : enable\n";
 
      fsFragDepth +=
        "void main()\n" +
        "{\n" +
        "    gl_FragColor = vec4(0.0, 1.0, 0.0, 1.0);\n";
 
      if (context.fragmentDepth) {
        fsFragDepth += "    gl_FragDepth";
        if (!context.webgl2) {
          fsFragDepth += "EXT";
        }
        fsFragDepth += " = 0.0;\n";
      }
 
      fsFragDepth += "}\n";
 
      var expected = [0, 255, 0, 255];
      expect({
        context: context,
        fragmentShader: fsFragDepth,
        depth: 1.0,
        clear: false,
      }).contextToRender(expected);
    });
 
    it("get the draw buffers extension", function () {
      expect(context.drawBuffers).toBeDefined();
    });
 
    it("get the maximum number of draw buffers", function () {
      if (context.drawBuffers) {
        expect(ContextLimits.maximumDrawBuffers).toBeGreaterThanOrEqualTo(1);
      } else {
        expect(ContextLimits.maximumDrawBuffers).toEqual(1);
      }
    });
 
    it("get the maximum number of color attachments", function () {
      if (context.drawBuffers) {
        expect(ContextLimits.maximumColorAttachments).toBeGreaterThanOrEqualTo(
          4
        );
      } else {
        expect(ContextLimits.maximumColorAttachments).toEqual(1);
      }
    });
 
    it("can create a pick ID and retrieve an object by pick color", function () {
      var o = {};
      var pickId = context.createPickId(o);
 
      expect(pickId).toBeDefined();
      expect(context.getObjectByPickColor(pickId.color)).toBe(o);
    });
 
    it("throws when creating a pick ID without an object", function () {
      expect(function () {
        context.createPickId(undefined);
      }).toThrowDeveloperError();
    });
 
    it("returns undefined when retrieving an object by unknown pick color", function () {
      expect(context.getObjectByPickColor(Color.WHITE)).toBeUndefined();
    });
 
    it("throws when getObjectByPickColor is called without a color", function () {
      expect(function () {
        context.getObjectByPickColor(undefined);
      }).toThrowDeveloperError();
    });
 
    it("fails to construct (null canvas)", function () {
      expect(function () {
        return new Context();
      }).toThrowDeveloperError();
    });
 
    it("isDestroyed", function () {
      var c = createContext();
      expect(c.isDestroyed()).toEqual(false);
      c.destroyForSpecs();
      expect(c.isDestroyed()).toEqual(true);
    });
 
    it("destroying Context destroys objects in cache", function () {
      var c = createContext();
      var destroyableObject = jasmine.createSpyObj("destroyableObject", [
        "destroy",
      ]);
      c.cache.foo = destroyableObject;
      c.destroyForSpecs();
      expect(destroyableObject.destroy).toHaveBeenCalled();
    });
 
    it("non-destroyable objects are allowed in the cache", function () {
      var c = createContext();
      var nonDestroyableObject = {};
      c.cache.foo = nonDestroyableObject;
      c.destroyForSpecs();
    });
 
    it("returns the underling drawingBufferWidth", function () {
      var c = createContext(undefined, 1024, 768);
      expect(c.drawingBufferWidth).toBe(1024);
      c.destroyForSpecs();
    });
 
    it("returns the underling drawingBufferHeight", function () {
      var c = createContext(undefined, 1024, 768);
      expect(c.drawingBufferHeight).toBe(768);
      c.destroyForSpecs();
    });
  },
  "WebGL"
);