Skip to content

Commit

Permalink
Fix mipmap generation
Browse files Browse the repository at this point in the history
  • Loading branch information
azrogers committed Sep 19, 2024
1 parent 6b43702 commit 6fcce71
Showing 1 changed file with 23 additions and 14 deletions.
37 changes: 23 additions & 14 deletions Source/CesiumRuntime/Private/CesiumTextureUtility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -762,7 +762,8 @@ TextureAddress convertGltfWrapTToUnreal(int32_t wrapT) {
}
}

CesiumAsync::SharedFuture<CesiumGltf::ImageCesium*> createMipMapsForSampler(
std::optional<CesiumAsync::SharedFuture<CesiumGltf::ImageCesium*>>
createMipMapsForSampler(
const CesiumAsync::AsyncSystem& asyncSystem,
const CesiumGltf::Sampler& sampler,
CesiumGltf::ImageCesium& image) {
Expand All @@ -776,13 +777,6 @@ CesiumAsync::SharedFuture<CesiumGltf::ImageCesium*> createMipMapsForSampler(
return extension.preprocessFuture.value();
}

CesiumAsync::Promise<CesiumGltf::ImageCesium*> promise =
asyncSystem.createPromise<CesiumGltf::ImageCesium*>();

extension.preprocessFuture = promise.getFuture().share();

lock.unlock();

// Generate mipmaps if needed.
// An image needs mipmaps generated for it if:
// 1. It is used by a Texture that has a Sampler with a mipmap filtering
Expand All @@ -806,10 +800,19 @@ CesiumAsync::SharedFuture<CesiumGltf::ImageCesium*> createMipMapsForSampler(
}

if (!needsMipmaps || image.pixelData.empty()) {
promise.resolve(&image);
return *extension.preprocessFuture;
// If we don't need mipmaps, we don't want to create a future for them.
// This allows a future sampler using this image that does need mipmaps to
// generate them.
return std::nullopt;
}

CesiumAsync::Promise<CesiumGltf::ImageCesium*> promise =
asyncSystem.createPromise<CesiumGltf::ImageCesium*>();

extension.preprocessFuture = promise.getFuture().share();

lock.unlock();

// We need mipmaps generated.
std::optional<std::string> errorMessage =
CesiumGltfReader::GltfReader::generateMipMaps(image);
Expand All @@ -829,10 +832,16 @@ CesiumAsync::SharedFuture<void> createMipMapsForAllTextures(
CesiumGltf::Model& model) {
std::vector<CesiumAsync::SharedFuture<CesiumGltf::ImageCesium*>> futures;
for (const Texture& texture : model.textures) {
futures.push_back(createMipMapsForSampler(
asyncSystem,
model.getSafe(model.samplers, texture.sampler),
*model.images[texture.source].cesium));
const CesiumGltf::Sampler& sampler =
model.getSafe(model.samplers, texture.sampler);
std::optional<CesiumAsync::SharedFuture<CesiumGltf::ImageCesium*>>
optionalFuture = createMipMapsForSampler(
asyncSystem,
sampler,
*model.images[texture.source].cesium);
if (optionalFuture.has_value()) {
futures.push_back(optionalFuture.value());
}
}

return asyncSystem.all(std::move(futures))
Expand Down

0 comments on commit 6fcce71

Please sign in to comment.