Skip to content

Commit

Permalink
texture memory length equals allocated memory for megatexture
Browse files Browse the repository at this point in the history
  • Loading branch information
lukemckinstry committed Feb 11, 2025
1 parent 1a32118 commit 254ae74
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 21 deletions.
2 changes: 1 addition & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#### Additions :tada:

- Add `ITwinData.loadGeospatialFeatures(iTwinId, collectionId)` function to load data from the [Geospatial Features API](https://developer.bentley.com/apis/geospatial-features/operations/get-features/) [#12449](https://github.com/CesiumGS/cesium/pull/12449)
- Implemented `textureByteLength`, `visited`, and `numberOfTilesWithContentReady` in `VoxelPrimitive.statistics`.
- Implemented `texturesByteLength`, `visited`, and `numberOfTilesWithContentReady` in `VoxelPrimitive.statistics`.

#### Fixes :wrench:

Expand Down
6 changes: 6 additions & 0 deletions packages/engine/Source/Scene/Megatexture.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ function Megatexture(
*/
this.componentType = componentType;

/**
* @type {number}
* @readonly
*/
this.textureMemoryByteLength = textureMemoryByteLength;

/**
* @type {Cartesian3}
* @readonly
Expand Down
3 changes: 3 additions & 0 deletions packages/engine/Source/Scene/VoxelPrimitive.js
Original file line number Diff line number Diff line change
Expand Up @@ -1434,6 +1434,8 @@ function initFromProvider(primitive, provider, context) {

// Create the VoxelTraversal, and set related uniforms
primitive._traversal = setupTraversal(primitive, provider, context);
primitive.statistics.texturesByteLength =
primitive._traversal.textureMemoryByteLength;
setTraversalUniforms(primitive._traversal, uniforms);
}

Expand Down Expand Up @@ -1812,6 +1814,7 @@ VoxelPrimitive.prototype.destroy = function () {

this._pickId = this._pickId && this._pickId.destroy();
this._traversal = this._traversal && this._traversal.destroy();
this.statistics.texturesByteLength = 0;
this._clippingPlanes = this._clippingPlanes && this._clippingPlanes.destroy();

return destroyObject(this);
Expand Down
25 changes: 10 additions & 15 deletions packages/engine/Source/Scene/VoxelTraversal.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Cartesian2 from "../Core/Cartesian2.js";
import Cartesian3 from "../Core/Cartesian3.js";
import CesiumMath from "../Core/Math.js";
import CullingVolume from "../Core/CullingVolume.js";
import defined from "../Core/defined.js";
Expand Down Expand Up @@ -50,6 +49,12 @@ function VoxelTraversal(
*/
this._primitive = primitive;

/**
* @type {number}
* @private
*/
this.textureMemoryByteLength = 0;

/**
* @type {Megatexture[]}
* @readonly
Expand All @@ -69,6 +74,9 @@ function VoxelTraversal(
componentType,
maximumTextureMemoryByteLength,
);

this.textureMemoryByteLength +=
this.megatextures[i].textureMemoryByteLength;
}

const maximumTileCount = this.megatextures[0].maximumTileCount;
Expand Down Expand Up @@ -223,8 +231,6 @@ function VoxelTraversal(
this.leafNodeTexelSizeUv = new Cartesian2();
}

const scratchDimensions = new Cartesian3();

/**
* Finds a keyframe node in the traversal
*
Expand Down Expand Up @@ -382,6 +388,7 @@ VoxelTraversal.prototype.destroy = function () {
for (let i = 0; i < megatextureLength; i++) {
megatextures[i] = megatextures[i] && megatextures[i].destroy();
}
this.textureMemoryByteLength = 0;

this.internalNodeTexture =
this.internalNodeTexture && this.internalNodeTexture.destroy();
Expand Down Expand Up @@ -683,18 +690,6 @@ function loadAndUnload(that, frameState) {
keyframeNodesInMegatexture[addNodeIndex] = highPriorityKeyframeNode;
}
}

const dimensions = Cartesian3.clone(
primitive._provider.dimensions,
scratchDimensions,
);
primitive.statistics.textureByteLength =
VoxelTraversal.getApproximateTextureMemoryByteLength(
that.megatextures[0].occupiedCount,
dimensions,
primitive._provider.types,
primitive._provider.componentTypes,
);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions packages/engine/Specs/Scene/VoxelPrimitiveSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ describe(
expect(spyUpdate.calls.count()).toEqual(1);
});

it("initial tiles loaded and all tiles loaded events are raised", async function () {
it("initial tiles loaded and all tiles loaded events are raised and statistics are updates", async function () {
const spyUpdate1 = jasmine.createSpy("listener");
const spyUpdate2 = jasmine.createSpy("listener");
const primitive = new VoxelPrimitive({ provider });
Expand All @@ -85,7 +85,7 @@ describe(
expect(spyUpdate2.calls.count()).toEqual(1);
expect(primitive.statistics.numberOfTilesWithContentReady).toEqual(1);
expect(primitive.statistics.visited).toEqual(1);
expect(primitive.statistics.textureByteLength).toEqual(64);
expect(primitive.statistics.texturesByteLength).toEqual(134217728);
});

it("tile load, load progress and tile visible events are raised", async function () {
Expand Down Expand Up @@ -266,6 +266,7 @@ describe(
expect(primitive.isDestroyed()).toBe(true);
expect(primitive._pickId).toBeUndefined();
expect(primitive._traversal).toBeUndefined();
expect(primitive.statistics).toBeDefined();
});
},
"WebGL",
Expand Down
13 changes: 10 additions & 3 deletions packages/engine/Specs/Scene/VoxelTraversalSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,12 @@ describe(
expect(traversal.isDestroyed()).toBe(true);
});

it("shows texture memory allocation statistic", function () {
expect(traversal.textureMemoryByteLength).toBe(textureMemory);
traversal.destroy();
expect(traversal.textureMemoryByteLength).toBe(0);
});

it("loads tiles into megatexture", async function () {
const keyFrameLocation = 0;
const recomputeBoundingVolumes = true;
Expand All @@ -161,13 +167,15 @@ describe(
scene.renderForSpecs();
return (
traversal.megatextures[0].occupiedCount > 0 &&
traversal._primitive.statistics.textureByteLength > 0
traversal._primitive.statistics.texturesByteLength > 0
);
});

const megatexture = traversal.megatextures[0];
expect(megatexture.occupiedCount).toBe(1);
expect(traversal._primitive.statistics.textureByteLength).toEqual(64);
expect(traversal._primitive.statistics.texturesByteLength).toEqual(
134217728,
);
});

it("tile failed event is raised", async function () {
Expand Down Expand Up @@ -196,7 +204,6 @@ describe(
traversal._primitive.statistics.numberOfTilesWithContentReady,
).toEqual(0);
expect(traversal._primitive.statistics.visited).toEqual(3);
expect(traversal._primitive.statistics.textureByteLength).toEqual(0);
});

it("finds keyframe node with expected metadata values", async function () {
Expand Down

0 comments on commit 254ae74

Please sign in to comment.