Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decode BasisU textures to BCn format instead of RGBA8 #474

Merged
merged 7 commits into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log

### v0.12.0 - 2023-11-01

* Basis Universal textures are now decoded to the native BCn texture format instead of RGBA8 in Kit 105.1 and above.

### v0.11.0 - 2023-10-02

* **Breaking change:** Cesium for Omniverse now requires Kit 105.1 or above (USD Composer 2023.2.0 or above).
Expand Down
43 changes: 38 additions & 5 deletions src/core/src/FabricTexture.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "cesium/omniverse/FabricTexture.h"

#include "cesium/omniverse/LoggerSink.h"
#include "cesium/omniverse/UsdUtil.h"

#include <CesiumGltf/ImageCesium.h>
Expand All @@ -10,6 +11,27 @@

namespace cesium::omniverse {

namespace {
carb::Format getCompressedImageFormat(CesiumGltf::GpuCompressedPixelFormat pixelFormat) {
switch (pixelFormat) {
case CesiumGltf::GpuCompressedPixelFormat::BC1_RGB:
return carb::Format::eBC1_RGBA_SRGB;
case CesiumGltf::GpuCompressedPixelFormat::BC3_RGBA:
return carb::Format::eBC3_RGBA_SRGB;
case CesiumGltf::GpuCompressedPixelFormat::BC4_R:
return carb::Format::eBC4_R_UNORM;
case CesiumGltf::GpuCompressedPixelFormat::BC5_RG:
return carb::Format::eBC5_RG_UNORM;
case CesiumGltf::GpuCompressedPixelFormat::BC7_RGBA:
return carb::Format::eBC7_RGBA_SRGB;
default:
// Unsupported compressed texture format.
return carb::Format::eUnknown;
};
}

} // namespace

FabricTexture::FabricTexture(const std::string& name)
: _texture(std::make_unique<omni::ui::DynamicTextureProvider>(name))
, _assetPathToken(UsdUtil::getDynamicTextureProviderAssetPathToken(name)) {
Expand All @@ -35,11 +57,22 @@ void FabricTexture::reset() {
}

void FabricTexture::setImage(const CesiumGltf::ImageCesium& image) {
_texture->setBytesData(
reinterpret_cast<const uint8_t*>(image.pixelData.data()),
carb::Uint2{static_cast<uint32_t>(image.width), static_cast<uint32_t>(image.height)},
omni::ui::kAutoCalculateStride,
carb::Format::eRGBA8_SRGB);
auto imageFormat = carb::Format::eRGBA8_SRGB;

if (image.compressedPixelFormat != CesiumGltf::GpuCompressedPixelFormat::NONE) {
imageFormat = getCompressedImageFormat(image.compressedPixelFormat);
}

if (imageFormat == carb::Format::eUnknown) {
CESIUM_LOG_WARN("Invalid image format");
} else {
// As of Kit 105.1, omni::ui::kAutoCalculateStride doesn't work for compressed textures. This value somehow works.
const auto stride = 4ULL * static_cast<uint64_t>(image.width);
const auto data = reinterpret_cast<const uint8_t*>(image.pixelData.data());
const auto dimensions = carb::Uint2{static_cast<uint32_t>(image.width), static_cast<uint32_t>(image.height)};

_texture->setBytesData(data, dimensions, stride, imageFormat);
}
}

} // namespace cesium::omniverse
20 changes: 20 additions & 0 deletions src/core/src/OmniTileset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,26 @@ void OmniTileset::reload() {
CESIUM_LOG_ERROR(error.message);
};

CesiumGltf::SupportedGpuCompressedPixelFormats supportedFormats;

// Only BCN compressed texture formats are supported in Omniverse
supportedFormats.ETC1_RGB = false;
supportedFormats.ETC2_RGBA = false;
supportedFormats.BC1_RGB = true;
supportedFormats.BC3_RGBA = true;
supportedFormats.BC4_R = true;
supportedFormats.BC5_RG = true;
supportedFormats.BC7_RGBA = true;
supportedFormats.PVRTC1_4_RGB = false;
supportedFormats.PVRTC1_4_RGBA = false;
supportedFormats.ASTC_4x4_RGBA = false;
supportedFormats.PVRTC2_4_RGB = false;
supportedFormats.PVRTC2_4_RGBA = false;
supportedFormats.ETC2_EAC_R11 = false;
supportedFormats.ETC2_EAC_RG11 = false;

options.contentOptions.ktx2TranscodeTargets = CesiumGltf::Ktx2TranscodeTargets(supportedFormats, false);

_pViewUpdateResult = nullptr;
_extentSet = false;
_activeLoading = false;
Expand Down