-
Notifications
You must be signed in to change notification settings - Fork 3.5k
/
Megatexture.js
487 lines (435 loc) · 13.6 KB
/
Megatexture.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
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
import Cartesian2 from "../Core/Cartesian2.js";
import Cartesian3 from "../Core/Cartesian3.js";
import ComponentDatatype from "../Core/ComponentDatatype.js";
import ContextLimits from "../Renderer/ContextLimits.js";
import defaultValue from "../Core/defaultValue.js";
import defined from "../Core/defined.js";
import destroyObject from "../Core/destroyObject.js";
import DeveloperError from "../Core/DeveloperError.js";
import CesiumMath from "../Core/Math.js";
import MetadataComponentType from "./MetadataComponentType.js";
import PixelDatatype from "../Renderer/PixelDatatype.js";
import PixelFormat from "../Core/PixelFormat.js";
import RuntimeError from "../Core/RuntimeError.js";
import Sampler from "../Renderer/Sampler.js";
import Texture from "../Renderer/Texture.js";
import TextureMagnificationFilter from "../Renderer/TextureMagnificationFilter.js";
import TextureMinificationFilter from "../Renderer/TextureMinificationFilter.js";
import TextureWrap from "../Renderer/TextureWrap.js";
/**
* @alias Megatexture
* @constructor
*
* @param {Context} context
* @param {Cartesian3} dimensions
* @param {number} channelCount
* @param {MetadataComponentType} componentType
* @param {number} [textureMemoryByteLength]
*
* @private
*/
function Megatexture(
context,
dimensions,
channelCount,
componentType,
textureMemoryByteLength,
) {
// TODO there are a lot of texture packing rules, see https://github.com/CesiumGS/cesium/issues/9572
// Unsigned short textures not allowed in webgl 1, so treat as float
if (componentType === MetadataComponentType.UNSIGNED_SHORT) {
componentType = MetadataComponentType.FLOAT32;
}
const supportsFloatingPointTexture = context.floatingPointTexture;
if (
componentType === MetadataComponentType.FLOAT32 &&
!supportsFloatingPointTexture
) {
throw new RuntimeError("Floating point texture not supported");
}
// TODO support more
let pixelType;
if (
componentType === MetadataComponentType.FLOAT32 ||
componentType === MetadataComponentType.FLOAT64
) {
pixelType = PixelDatatype.FLOAT;
} else if (componentType === MetadataComponentType.UINT8) {
pixelType = PixelDatatype.UNSIGNED_BYTE;
}
let pixelFormat;
if (channelCount === 1) {
pixelFormat = context.webgl2 ? PixelFormat.RED : PixelFormat.LUMINANCE;
} else if (channelCount === 2) {
pixelFormat = context.webgl2 ? PixelFormat.RG : PixelFormat.LUMINANCE_ALPHA;
} else if (channelCount === 3) {
pixelFormat = PixelFormat.RGB;
} else if (channelCount === 4) {
pixelFormat = PixelFormat.RGBA;
}
const maximumTextureMemoryByteLength = 512 * 1024 * 1024;
const defaultTextureMemoryByteLength = 128 * 1024 * 1024;
textureMemoryByteLength = Math.min(
defaultValue(textureMemoryByteLength, defaultTextureMemoryByteLength),
maximumTextureMemoryByteLength,
);
const maximumTextureDimensionContext = ContextLimits.maximumTextureSize;
const componentTypeByteLength =
MetadataComponentType.getSizeInBytes(componentType);
const texelCount = Math.floor(
textureMemoryByteLength / (channelCount * componentTypeByteLength),
);
const textureDimension = Math.min(
maximumTextureDimensionContext,
CesiumMath.previousPowerOfTwo(Math.floor(Math.sqrt(texelCount))),
);
const sliceCountPerRegionX = Math.ceil(Math.sqrt(dimensions.x));
const sliceCountPerRegionY = Math.ceil(dimensions.z / sliceCountPerRegionX);
const voxelCountPerRegionX = sliceCountPerRegionX * dimensions.x;
const voxelCountPerRegionY = sliceCountPerRegionY * dimensions.y;
const regionCountPerMegatextureX = Math.floor(
textureDimension / voxelCountPerRegionX,
);
const regionCountPerMegatextureY = Math.floor(
textureDimension / voxelCountPerRegionY,
);
if (regionCountPerMegatextureX === 0 || regionCountPerMegatextureY === 0) {
throw new RuntimeError("Tileset is too large to fit into megatexture");
}
/**
* @type {number}
* @readonly
*/
this.channelCount = channelCount;
/**
* @type {MetadataComponentType}
* @readonly
*/
this.componentType = componentType;
/**
* @type {Cartesian3}
* @readonly
*/
this.voxelCountPerTile = Cartesian3.clone(dimensions, new Cartesian3());
/**
* @type {number}
* @readonly
*/
this.maximumTileCount =
regionCountPerMegatextureX * regionCountPerMegatextureY;
/**
* @type {Cartesian2}
* @readonly
*/
this.regionCountPerMegatexture = new Cartesian2(
regionCountPerMegatextureX,
regionCountPerMegatextureY,
);
/**
* @type {Cartesian2}
* @readonly
*/
this.voxelCountPerRegion = new Cartesian2(
voxelCountPerRegionX,
voxelCountPerRegionY,
);
/**
* @type {Cartesian2}
* @readonly
*/
this.sliceCountPerRegion = new Cartesian2(
sliceCountPerRegionX,
sliceCountPerRegionY,
);
/**
* @type {Cartesian2}
* @readonly
*/
this.voxelSizeUv = new Cartesian2(
1.0 / textureDimension,
1.0 / textureDimension,
);
/**
* @type {Cartesian2}
* @readonly
*/
this.sliceSizeUv = new Cartesian2(
dimensions.x / textureDimension,
dimensions.y / textureDimension,
);
/**
* @type {Cartesian2}
* @readonly
*/
this.regionSizeUv = new Cartesian2(
voxelCountPerRegionX / textureDimension,
voxelCountPerRegionY / textureDimension,
);
/**
* @type {Texture}
* @readonly
*/
this.texture = new Texture({
context: context,
pixelFormat: pixelFormat,
pixelDatatype: pixelType,
flipY: false,
width: textureDimension,
height: textureDimension,
sampler: new Sampler({
wrapS: TextureWrap.CLAMP_TO_EDGE,
wrapT: TextureWrap.CLAMP_TO_EDGE,
minificationFilter: TextureMinificationFilter.LINEAR,
magnificationFilter: TextureMagnificationFilter.LINEAR,
}),
});
const componentDatatype =
MetadataComponentType.toComponentDatatype(componentType);
/**
* @type {Array}
*/
this.tileVoxelDataTemp = ComponentDatatype.createTypedArray(
componentDatatype,
voxelCountPerRegionX * voxelCountPerRegionY * channelCount,
);
/**
* @type {MegatextureNode[]}
* @readonly
*/
this.nodes = new Array(this.maximumTileCount);
for (let tileIndex = 0; tileIndex < this.maximumTileCount; tileIndex++) {
this.nodes[tileIndex] = new MegatextureNode(tileIndex);
}
for (let tileIndex = 0; tileIndex < this.maximumTileCount; tileIndex++) {
const node = this.nodes[tileIndex];
node.previousNode = tileIndex > 0 ? this.nodes[tileIndex - 1] : undefined;
node.nextNode =
tileIndex < this.maximumTileCount - 1
? this.nodes[tileIndex + 1]
: undefined;
}
/**
* @type {MegatextureNode}
* @readonly
*/
this.occupiedList = undefined;
/**
* @type {MegatextureNode}
* @readonly
*/
this.emptyList = this.nodes[0];
/**
* @type {number}
* @readonly
*/
this.occupiedCount = 0;
}
/**
* @alias MegatextureNode
* @constructor
*
* @param {number} index
*
* @private
*/
function MegatextureNode(index) {
/**
* @type {number}
*/
this.index = index;
/**
* @type {MegatextureNode}
*/
this.nextNode = undefined;
/**
* @type {MegatextureNode}
*/
this.previousNode = undefined;
}
/**
* @param {Array} data
* @returns {number}
*/
Megatexture.prototype.add = function (data) {
if (this.isFull()) {
throw new DeveloperError("Trying to add when there are no empty spots");
}
// remove head of empty list
const node = this.emptyList;
this.emptyList = this.emptyList.nextNode;
if (defined(this.emptyList)) {
this.emptyList.previousNode = undefined;
}
// make head of occupied list
node.nextNode = this.occupiedList;
if (defined(node.nextNode)) {
node.nextNode.previousNode = node;
}
this.occupiedList = node;
const index = node.index;
this.writeDataToTexture(index, data);
this.occupiedCount++;
return index;
};
/**
* @param {number} index
*/
Megatexture.prototype.remove = function (index) {
if (index < 0 || index >= this.maximumTileCount) {
throw new DeveloperError("Megatexture index out of bounds");
}
// remove from list
const node = this.nodes[index];
if (defined(node.previousNode)) {
node.previousNode.nextNode = node.nextNode;
}
if (defined(node.nextNode)) {
node.nextNode.previousNode = node.previousNode;
}
// make head of empty list
node.nextNode = this.emptyList;
if (defined(node.nextNode)) {
node.nextNode.previousNode = node;
}
node.previousNode = undefined;
this.emptyList = node;
this.occupiedCount--;
};
/**
* @returns {boolean}
*/
Megatexture.prototype.isFull = function () {
return this.emptyList === undefined;
};
/**
* @param {number} tileCount
* @param {Cartesian3} dimensions
* @param {number} channelCount number of channels in the metadata. Must be 1 to 4.
* @param {MetadataComponentType} componentType
* @returns {number}
*/
Megatexture.getApproximateTextureMemoryByteLength = function (
tileCount,
dimensions,
channelCount,
componentType,
) {
// TODO there's a lot of code duplicate with Megatexture constructor
// Unsigned short textures not allowed in webgl 1, so treat as float
if (componentType === MetadataComponentType.UNSIGNED_SHORT) {
componentType = MetadataComponentType.FLOAT32;
}
const datatypeSizeInBytes =
MetadataComponentType.getSizeInBytes(componentType);
const voxelCountTotal =
tileCount * dimensions.x * dimensions.y * dimensions.z;
const sliceCountPerRegionX = Math.ceil(Math.sqrt(dimensions.x));
const sliceCountPerRegionY = Math.ceil(dimensions.z / sliceCountPerRegionX);
const voxelCountPerRegionX = sliceCountPerRegionX * dimensions.x;
const voxelCountPerRegionY = sliceCountPerRegionY * dimensions.y;
// Find the power of two that can fit all tile data, accounting for slices.
// There's probably a non-iterative solution for this, but this is good enough for now.
let textureDimension = CesiumMath.previousPowerOfTwo(
Math.floor(Math.sqrt(voxelCountTotal)),
);
for (;;) {
const regionCountX = Math.floor(textureDimension / voxelCountPerRegionX);
const regionCountY = Math.floor(textureDimension / voxelCountPerRegionY);
const regionCount = regionCountX * regionCountY;
if (regionCount >= tileCount) {
break;
} else {
textureDimension *= 2;
}
}
const textureMemoryByteLength =
textureDimension * textureDimension * channelCount * datatypeSizeInBytes;
return textureMemoryByteLength;
};
/**
* @param {number} index
* @param {Float32Array|Uint16Array|Uint8Array} data
*/
Megatexture.prototype.writeDataToTexture = function (index, data) {
// Unsigned short textures not allowed in webgl 1, so treat as float
const tileData =
data.constructor === Uint16Array ? new Float32Array(data) : data;
const voxelDimensionsPerTile = this.voxelCountPerTile;
const sliceDimensionsPerRegion = this.sliceCountPerRegion;
const voxelDimensionsPerRegion = this.voxelCountPerRegion;
const channelCount = this.channelCount;
const tileVoxelData = this.tileVoxelDataTemp;
for (let z = 0; z < voxelDimensionsPerTile.z; z++) {
const sliceVoxelOffsetX =
(z % sliceDimensionsPerRegion.x) * voxelDimensionsPerTile.x;
const sliceVoxelOffsetY =
Math.floor(z / sliceDimensionsPerRegion.x) * voxelDimensionsPerTile.y;
for (let y = 0; y < voxelDimensionsPerTile.y; y++) {
for (let x = 0; x < voxelDimensionsPerTile.x; x++) {
const readIndex =
z * voxelDimensionsPerTile.y * voxelDimensionsPerTile.x +
y * voxelDimensionsPerTile.x +
x;
const writeIndex =
(sliceVoxelOffsetY + y) * voxelDimensionsPerRegion.x +
(sliceVoxelOffsetX + x);
for (let c = 0; c < channelCount; c++) {
tileVoxelData[writeIndex * channelCount + c] =
tileData[readIndex * channelCount + c];
}
}
}
}
const regionDimensionsPerMegatexture = this.regionCountPerMegatexture;
const voxelWidth = voxelDimensionsPerRegion.x;
const voxelHeight = voxelDimensionsPerRegion.y;
const voxelOffsetX =
(index % regionDimensionsPerMegatexture.x) * voxelDimensionsPerRegion.x;
const voxelOffsetY =
Math.floor(index / regionDimensionsPerMegatexture.x) *
voxelDimensionsPerRegion.y;
const source = {
arrayBufferView: tileVoxelData,
width: voxelWidth,
height: voxelHeight,
};
const copyOptions = {
source: source,
xOffset: voxelOffsetX,
yOffset: voxelOffsetY,
};
this.texture.copyFrom(copyOptions);
};
/**
* Returns true if this object was destroyed; otherwise, false.
* <br /><br />
* If this object was destroyed, it should not be used; calling any function other than
* <code>isDestroyed</code> will result in a {@link DeveloperError} exception.
*
* @returns {boolean} <code>true</code> if this object was destroyed; otherwise, <code>false</code>.
*
* @see Megatexture#destroy
*/
Megatexture.prototype.isDestroyed = function () {
return false;
};
/**
* Destroys the WebGL resources held by this object. Destroying an object allows for deterministic
* release of WebGL resources, instead of relying on the garbage collector to destroy this object.
* <br /><br />
* Once an object is destroyed, it should not be used; calling any function other than
* <code>isDestroyed</code> will result in a {@link DeveloperError} exception. Therefore,
* assign the return value (<code>undefined</code>) to the object as done in the example.
*
* @exception {DeveloperError} This object was destroyed, i.e., destroy() was called.
*
* @see Megatexture#isDestroyed
*
* @example
* megatexture = megatexture && megatexture.destroy();
*/
Megatexture.prototype.destroy = function () {
this.texture = this.texture && this.texture.destroy();
return destroyObject(this);
};
export default Megatexture;