yzt
2023-05-08 24e1c6a1c3d5331b5a4f1111dcbae3ef148eda1a
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
import { Cartesian4 } from "../../Source/Cesium.js";
import { ComponentDatatype } from "../../Source/Cesium.js";
import { Math as CesiumMath } from "../../Source/Cesium.js";
import { PixelDatatype } from "../../Source/Cesium.js";
import { Texture } from "../../Source/Cesium.js";
import { BatchTable } from "../../Source/Cesium.js";
import createScene from "../createScene.js";
 
describe(
  "Scene/BatchTable",
  function () {
    var unsignedByteAttributes = [
      {
        functionName: "batchTable_getShow",
        componentDatatype: ComponentDatatype.UNSIGNED_BYTE,
        componentsPerAttribute: 1,
      },
      {
        functionName: "batchTable_getPickColor",
        componentDatatype: ComponentDatatype.UNSIGNED_BYTE,
        componentsPerAttribute: 4,
        normalize: true,
      },
    ];
 
    var floatAttributes = [
      {
        functionName: "batchTable_getShow",
        componentDatatype: ComponentDatatype.UNSIGNED_BYTE,
        componentsPerAttribute: 1,
      },
      {
        functionName: "batchTable_getCenter",
        componentDatatype: ComponentDatatype.FLOAT,
        componentsPerAttribute: 4,
      },
    ];
 
    var batchTable;
    var scene;
    var context;
 
    beforeAll(function () {
      scene = createScene();
      context = scene.context;
    });
 
    afterAll(function () {
      scene.destroyForSpecs();
    });
 
    afterEach(function () {
      batchTable =
        batchTable && !batchTable.isDestroyed() && batchTable.destroy();
    });
 
    it("constructor", function () {
      batchTable = new BatchTable(context, unsignedByteAttributes, 2);
      expect(batchTable.attributes).toBe(unsignedByteAttributes);
      expect(batchTable.numberOfInstances).toEqual(2);
    });
 
    it("constructior throws without context", function () {
      expect(function () {
        batchTable = new BatchTable(undefined, unsignedByteAttributes, 5);
      }).toThrowDeveloperError();
    });
 
    it("constructior throws without attributes", function () {
      expect(function () {
        batchTable = new BatchTable(context, undefined, 5);
      }).toThrowDeveloperError();
    });
 
    it("constructor throws without number of instances", function () {
      expect(function () {
        batchTable = new BatchTable(context, unsignedByteAttributes, undefined);
      }).toThrowDeveloperError();
    });
 
    it("sets and gets entries in the table", function () {
      batchTable = new BatchTable(context, unsignedByteAttributes, 5);
 
      var i;
      var color = new Cartesian4(0, 1, 2, 3);
 
      for (i = 0; i < batchTable.numberOfInstances; ++i) {
        batchTable.setBatchedAttribute(i, 0, 1);
        batchTable.setBatchedAttribute(i, 1, color);
      }
 
      for (i = 0; i < batchTable.numberOfInstances; ++i) {
        expect(batchTable.getBatchedAttribute(3, 0)).toEqual(1);
        expect(batchTable.getBatchedAttribute(3, 1)).toEqual(color);
      }
 
      color = new Cartesian4(4, 5, 6, 7);
      batchTable.setBatchedAttribute(3, 0, 0);
      batchTable.setBatchedAttribute(3, 1, color);
      expect(batchTable.getBatchedAttribute(3, 0)).toEqual(0);
      expect(batchTable.getBatchedAttribute(3, 1)).toEqual(color);
    });
 
    it("sets and gets entries in the table with float attributes", function () {
      var context = {
        floatingPointTexture: true,
      };
      batchTable = new BatchTable(context, floatAttributes, 5);
 
      var i;
      var color = new Cartesian4(0, 1, 2, 3);
 
      for (i = 0; i < batchTable.numberOfInstances; ++i) {
        batchTable.setBatchedAttribute(i, 0, 1);
        batchTable.setBatchedAttribute(i, 1, color);
      }
 
      for (i = 0; i < batchTable.numberOfInstances; ++i) {
        expect(batchTable.getBatchedAttribute(3, 0)).toEqual(1);
        expect(batchTable.getBatchedAttribute(3, 1)).toEqual(color);
      }
 
      color = new Cartesian4(4, 5, 6, 7);
      batchTable.setBatchedAttribute(3, 0, 0);
      batchTable.setBatchedAttribute(3, 1, color);
      expect(batchTable.getBatchedAttribute(3, 0)).toEqual(0);
      expect(batchTable.getBatchedAttribute(3, 1)).toEqual(color);
    });
 
    it("sets and gets entries in the table with float attributes and forced packing", function () {
      var context = {
        floatingPointTexture: false,
      };
      batchTable = new BatchTable(context, floatAttributes, 5);
 
      var i;
      var color = new Cartesian4(
        1.23456e12,
        -2.34567e30,
        3.45678e-6,
        -4.56789e-10
      );
 
      for (i = 0; i < batchTable.numberOfInstances; ++i) {
        batchTable.setBatchedAttribute(i, 0, 1);
        batchTable.setBatchedAttribute(i, 1, color);
      }
 
      var value;
      for (i = 0; i < batchTable.numberOfInstances; ++i) {
        value = batchTable.getBatchedAttribute(3, 0);
        expect(value).toEqual(1);
        value = batchTable.getBatchedAttribute(3, 1);
        expect(value).toEqualEpsilon(color, CesiumMath.EPSILON6);
      }
 
      color = new Cartesian4(
        0,
        Number.MAX_VALUE,
        Number.POSITIVE_INFINITY,
        Number.NEGATIVE_INFINITY
      );
      batchTable.setBatchedAttribute(3, 0, 0);
      batchTable.setBatchedAttribute(3, 1, color);
 
      value = batchTable.getBatchedAttribute(3, 0);
      expect(value).toEqual(0);
      value = batchTable.getBatchedAttribute(3, 1);
      expect(value.x).toEqual(0.0);
      expect(value.y).toEqual(Number.POSITIVE_INFINITY);
      expect(value.z).toEqual(Number.POSITIVE_INFINITY);
      expect(value.w).toEqual(Number.NEGATIVE_INFINITY);
    });
 
    it("gets with result parameter", function () {
      batchTable = new BatchTable(context, unsignedByteAttributes, 5);
      var color = new Cartesian4(0, 1, 2, 3);
      batchTable.setBatchedAttribute(0, 1, color);
 
      var result = new Cartesian4();
      var returndValue = batchTable.getBatchedAttribute(0, 1, result);
      expect(returndValue).toBe(result);
      expect(result).toEqual(color);
    });
 
    it("get entry throws when instance index is out of range", function () {
      batchTable = new BatchTable(context, unsignedByteAttributes, 5);
      expect(function () {
        batchTable.getBatchedAttribute(-1, 0);
      }).toThrowDeveloperError();
      expect(function () {
        batchTable.getBatchedAttribute(100, 0);
      }).toThrowDeveloperError();
    });
 
    it("get entry throws when attribute index is out of range", function () {
      batchTable = new BatchTable(context, unsignedByteAttributes, 5);
      expect(function () {
        batchTable.getBatchedAttribute(0, -1);
      }).toThrowDeveloperError();
      expect(function () {
        batchTable.getBatchedAttribute(0, 100);
      }).toThrowDeveloperError();
    });
 
    it("set entry throws when instance index is out of range", function () {
      batchTable = new BatchTable(context, unsignedByteAttributes, 5);
      expect(function () {
        batchTable.setBatchedAttribute(-1, 0, 0);
      }).toThrowDeveloperError();
      expect(function () {
        batchTable.setBatchedAttribute(100, 0, 1);
      }).toThrowDeveloperError();
    });
 
    it("set entry throws when attribute index is out of range", function () {
      batchTable = new BatchTable(context, unsignedByteAttributes, 5);
      expect(function () {
        batchTable.setBatchedAttribute(0, -1, 1);
      }).toThrowDeveloperError();
      expect(function () {
        batchTable.setBatchedAttribute(0, 100, 1);
      }).toThrowDeveloperError();
    });
 
    it("set entry throws when value is undefined", function () {
      batchTable = new BatchTable(context, unsignedByteAttributes, 5);
      expect(function () {
        batchTable.setBatchedAttribute(0, 0, undefined);
      }).toThrowDeveloperError();
    });
 
    it("creates a uniform callback with unsigned byte texture", function () {
      batchTable = new BatchTable(context, unsignedByteAttributes, 5);
      batchTable.update(scene.frameState);
 
      var uniforms = batchTable.getUniformMapCallback()({});
      expect(uniforms.batchTexture).toBeDefined();
      expect(uniforms.batchTexture()).toBeInstanceOf(Texture);
      expect(uniforms.batchTexture().pixelDatatype).toEqual(
        PixelDatatype.UNSIGNED_BYTE
      );
      expect(uniforms.batchTextureDimensions).toBeDefined();
      expect(uniforms.batchTextureDimensions().x).toBeGreaterThan(0);
      expect(uniforms.batchTextureDimensions().y).toBeGreaterThan(0);
      expect(uniforms.batchTextureStep).toBeDefined();
      expect(uniforms.batchTextureStep().x).toBeGreaterThan(0);
      expect(uniforms.batchTextureStep().y).toBeGreaterThan(0);
      expect(uniforms.batchTextureStep().z).toBeGreaterThan(0);
      expect(uniforms.batchTextureStep().w).toBeGreaterThan(0);
    });
 
    it("creates a uniform callback with float texture", function () {
      if (!context.floatingPointTexture) {
        return;
      }
 
      batchTable = new BatchTable(context, floatAttributes, 5);
      batchTable.update(scene.frameState);
 
      var uniforms = batchTable.getUniformMapCallback()({});
      expect(uniforms.batchTexture).toBeDefined();
      expect(uniforms.batchTexture()).toBeInstanceOf(Texture);
      expect(uniforms.batchTexture().pixelDatatype).toEqual(
        PixelDatatype.FLOAT
      );
      expect(uniforms.batchTextureDimensions).toBeDefined();
      expect(uniforms.batchTextureDimensions().x).toBeGreaterThan(0);
      expect(uniforms.batchTextureDimensions().y).toBeGreaterThan(0);
      expect(uniforms.batchTextureStep).toBeDefined();
      expect(uniforms.batchTextureStep().x).toBeGreaterThan(0);
      expect(uniforms.batchTextureStep().y).toBeGreaterThan(0);
      expect(uniforms.batchTextureStep().z).toBeGreaterThan(0);
      expect(uniforms.batchTextureStep().w).toBeGreaterThan(0);
 
      if (scene.context.floatingPointTexture) {
        expect(uniforms.batchTexture().pixelDatatype).toEqual(
          PixelDatatype.FLOAT
        );
      } else {
        expect(uniforms.batchTexture().pixelDatatype).toEqual(
          PixelDatatype.UNSIGNED_BYTE
        );
      }
    });
 
    it("create shader functions", function () {
      batchTable = new BatchTable(context, unsignedByteAttributes, 5);
 
      var shader = "void main() { gl_Position = vec4(0.0); }";
      var modifiedShader = batchTable.getVertexShaderCallback()(shader);
      expect(
        modifiedShader.indexOf(batchTable.attributes[0].functionName)
      ).not.toEqual(-1);
      expect(
        modifiedShader.indexOf(batchTable.attributes[1].functionName)
      ).not.toEqual(-1);
    });
 
    it("isDestroyed", function () {
      batchTable = new BatchTable(context, unsignedByteAttributes, 5);
      expect(batchTable.isDestroyed()).toEqual(false);
      batchTable.destroy();
      expect(batchTable.isDestroyed()).toEqual(true);
    });
  },
  "WebGL"
);