Skip to content

Commit

Permalink
Merge pull request #804 from CesiumGS/image-size-bytes
Browse files Browse the repository at this point in the history
Track image data size independently of the `pixelData` array size
  • Loading branch information
csciguy8 authored Feb 23, 2024
2 parents 916c63e + e16a3e1 commit 028fc7c
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
5 changes: 4 additions & 1 deletion Cesium3DTilesSelection/src/Tile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,10 @@ int64_t Tile::computeByteSize() const noexcept {
bytes -= bufferViews[size_t(bufferView)].byteLength;
}

bytes += int64_t(image.cesium.pixelData.size());
// lastKnownSizeInBytes is set in
// TilesetContentManager::ContentKindSetter, if not sooner (e.g., by the
// renderer implementation).
bytes += image.cesium.lastKnownSizeInBytes;
}
}

Expand Down
11 changes: 11 additions & 0 deletions Cesium3DTilesSelection/src/TilesetContentManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,17 @@ struct ContentKindSetter {
}

void operator()(CesiumGltf::Model&& model) {
for (CesiumGltf::Image& image : model.images) {
// If the image size hasn't been overridden, store the pixelData
// size now. We'll be adding this number to our total memory usage soon,
// and remove it when the tile is later unloaded, and we must use
// the same size in each case.
if (image.cesium.lastKnownSizeInBytes < 0) {
image.cesium.lastKnownSizeInBytes =
int64_t(image.cesium.pixelData.size());
}
}

auto pRenderContent = std::make_unique<TileRenderContent>(std::move(model));
pRenderContent->setRenderResources(pRenderResources);
if (rasterOverlayDetails) {
Expand Down
17 changes: 16 additions & 1 deletion CesiumGltf/include/CesiumGltf/ImageCesium.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ struct CESIUMGLTF_API ImageCesium final {
* Otherwise, this buffer will store the compressed pixel data in the
* specified format.
*
* If mipOffsets is not empty, this buffer will contains multiple mips
* If mipPositions is not empty, this buffer will contains multiple mips
* back-to-back.
*
* When this is an uncompressed texture:
Expand All @@ -95,5 +95,20 @@ struct CESIUMGLTF_API ImageCesium final {
* | 4 | red, green, blue, alpha |
*/
std::vector<std::byte> pixelData;

/**
* @brief The effective size of this image, in bytes, for estimating resource
* usage for caching purposes.
*
* When this value is less than zero (the default), the size of this image
* should be assumed to equal the size of the {@link pixelData} array. When
* it is greater than or equal to zero, the specified size should be used
* instead. For example, the overridden size may account for:
* * The `pixelData` being cleared during the load process in order to save
* memory.
* * The cost of any renderer resources (e.g., GPU textures) created for
* this image.
*/
int64_t lastKnownSizeInBytes = -1;
};
} // namespace CesiumGltf
15 changes: 13 additions & 2 deletions CesiumRasterOverlays/src/RasterOverlayTileProvider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ RasterOverlayTileProvider::getTile(
void RasterOverlayTileProvider::removeTile(RasterOverlayTile* pTile) noexcept {
assert(pTile->getReferenceCount() == 0);

this->_tileDataBytes -= int64_t(pTile->getImage().pixelData.size());
this->_tileDataBytes -= pTile->getImage().lastKnownSizeInBytes;
}

CesiumAsync::Future<TileProviderAndTile>
Expand Down Expand Up @@ -343,7 +343,18 @@ CesiumAsync::Future<TileProviderAndTile> RasterOverlayTileProvider::doLoad(
: RasterOverlayTile::MoreDetailAvailable::No;
pTile->setState(result.state);

thiz->_tileDataBytes += int64_t(pTile->getImage().pixelData.size());
ImageCesium& imageCesium = pTile->getImage();

// If the image size hasn't been overridden, store the pixelData
// size now. We'll add this number to our total memory usage now,
// and remove it when the tile is later unloaded, and we must use
// the same size in each case.
if (imageCesium.lastKnownSizeInBytes < 0) {
imageCesium.lastKnownSizeInBytes =
int64_t(imageCesium.pixelData.size());
}

thiz->_tileDataBytes += imageCesium.lastKnownSizeInBytes;

thiz->finalizeTileLoad(isThrottledLoad);

Expand Down

0 comments on commit 028fc7c

Please sign in to comment.