Skip to content

Commit

Permalink
Move format conversion function inside the ktx texture loader
Browse files Browse the repository at this point in the history
Summary: ^^

Reviewed By: rokuz, corporateshark

Differential Revision: D63038310

fbshipit-source-id: 3f7f100a5b5d71d4bcb634d3f6b1ca6f349f4758
  • Loading branch information
francoiscoulombe authored and facebook-github-bot committed Sep 19, 2024
1 parent 4f6bb7e commit e799f09
Show file tree
Hide file tree
Showing 22 changed files with 729 additions and 290 deletions.
14 changes: 6 additions & 8 deletions IGLU/texture_loader/ktx2/TextureLoaderFactory.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
#include <IGLU/texture_loader/ktx2/TextureLoaderFactory.h>

#include <IGLU/texture_loader/ktx2/Header.h>
#include <IGLU/texture_loader/util/VkTextureFormat.h>
#include <igl/IGLSafeC.h>
#include <igl/vulkan/util/TextureFormatInt.h>
#include <ktx.h>
#include <numeric>
#include <vector>
Expand Down Expand Up @@ -48,9 +48,8 @@ bool TextureLoaderFactory::canCreateInternal(DataReader headerReader,
// vkFormat = 0 means basis universal or some non-Vulkan format.
// In either case, we need to process the DFD to understand whether we can really handle the
// format or not.
if (header->vkFormat != 0 &&
igl::vulkan::util::intVkTextureFormatToTextureFormat(
static_cast<int32_t>(header->vkFormat)) == igl::TextureFormat::Invalid) {
if (header->vkFormat != 0 && util::vkTextureFormatToTextureFormat(static_cast<int32_t>(
header->vkFormat)) == igl::TextureFormat::Invalid) {
igl::Result::setResult(
outResult, igl::Result::Code::InvalidOperation, "Unrecognized texture format.");
return false;
Expand Down Expand Up @@ -93,8 +92,8 @@ bool TextureLoaderFactory::validate(DataReader reader,
}

if (header->vkFormat != 0u) {
const auto format = igl::vulkan::util::intVkTextureFormatToTextureFormat(
static_cast<int32_t>(header->vkFormat));
const auto format =
util::vkTextureFormatToTextureFormat(static_cast<int32_t>(header->vkFormat));
const auto properties = igl::TextureFormatProperties::fromTextureFormat(format);

const uint32_t mipLevelAlignment =
Expand Down Expand Up @@ -178,8 +177,7 @@ bool TextureLoaderFactory::validate(DataReader reader,
igl::TextureFormat TextureLoaderFactory::textureFormat(const ktxTexture* texture) const noexcept {
if (texture->classId == ktxTexture2_c) {
const auto* texture2 = reinterpret_cast<const ktxTexture2*>(texture);
return igl::vulkan::util::intVkTextureFormatToTextureFormat(
static_cast<int32_t>(texture2->vkFormat));
return util::vkTextureFormatToTextureFormat(static_cast<int32_t>(texture2->vkFormat));
}

return igl::TextureFormat::Invalid;
Expand Down
493 changes: 493 additions & 0 deletions IGLU/texture_loader/util/VkTextureFormat.cpp

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
#include <cstdint>
#include <igl/TextureFormat.h>

namespace igl::vulkan::util {

TextureFormat intVkTextureFormatToTextureFormat(int32_t vkFormat);
} // namespace igl::vulkan::util
namespace iglu::textureloader::util {
igl::TextureFormat vkTextureFormatToTextureFormat(int32_t vkFormat);
} // namespace iglu::textureloader::util
3 changes: 0 additions & 3 deletions samples/desktop/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,3 @@ add_demo("Tiny_MeshLarge")

target_sources(Tiny_MeshLarge
PUBLIC "${IGL_ROOT_DIR}/third-party/deps/src/3D-Graphics-Rendering-Cookbook/shared/UtilsCubemap.cpp")
if(NOT IGL_WITH_VULKAN)
target_sources(Tiny_MeshLarge PUBLIC "${IGL_ROOT_DIR}/src/igl/vulkan/util/TextureFormat.cpp")
endif()
15 changes: 8 additions & 7 deletions samples/desktop/Tiny/Tiny_MeshLarge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@

#include <igl/FPSCounter.h>
#include <igl/IGL.h>
#include <igl/vulkan/util/TextureFormatInt.h>

#include <IGLU/texture_loader/util/VkTextureFormat.h>

#include <glm/ext.hpp>
#include <glm/glm.hpp>
Expand Down Expand Up @@ -2294,12 +2295,12 @@ void loadCubemapTexture(const std::string& fileNameKTX, std::shared_ptr<ITexture
texRefRange.numMipLevels = TextureDesc::calcNumMipLevels(texRefRange.width, texRefRange.height);
}

auto desc =
TextureDesc::newCube(igl::vulkan::util::intVkTextureFormatToTextureFormat(texture->vkFormat),
texRefRange.width,
texRefRange.height,
TextureDesc::TextureUsageBits::Sampled,
fileNameKTX.c_str());
auto desc = TextureDesc::newCube(
iglu::textureloader::util::vkTextureFormatToTextureFormat(texture->vkFormat),
texRefRange.width,
texRefRange.height,
TextureDesc::TextureUsageBits::Sampled,
fileNameKTX.c_str());
desc.numMipLevels = texRefRange.numMipLevels;
tex = device_->createTexture(desc, nullptr);
IGL_ASSERT(tex);
Expand Down
4 changes: 2 additions & 2 deletions shell/openxr/mobile/vulkan/XrSwapchainProviderImplVulkan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
#include <fmt/core.h>
#include <igl/vulkan/Device.h>
#include <igl/vulkan/Texture.h>
#include <igl/vulkan/TextureFormat.h>
#include <igl/vulkan/VulkanContext.h>
#include <igl/vulkan/VulkanDevice.h>
#include <igl/vulkan/VulkanImage.h>
#include <igl/vulkan/VulkanImageView.h>
#include <igl/vulkan/util/TextureFormat.h>

#include <shell/openxr/XrLog.h>

Expand Down Expand Up @@ -98,7 +98,7 @@ std::shared_ptr<igl::ITexture> getSurfaceTexture(
}

const auto& actualDevice = static_cast<igl::vulkan::Device&>(device);
const auto iglFormat = vulkan::util::vkTextureFormatToTextureFormat(externalTextureFormat);
const auto iglFormat = vulkan::vkTextureFormatToTextureFormat(externalTextureFormat);
const auto texture = inOutTextures[imageIndex];
// allocate new drawable textures if its null or mismatches in size or format
if (!texture || swapchainImageInfo.imageWidth != texture->getSize().width ||
Expand Down
11 changes: 4 additions & 7 deletions src/igl/tests/iglu/texture_loader/Ktx2TextureLoaderTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@

#include <IGLU/texture_loader/ktx2/Header.h>
#include <IGLU/texture_loader/ktx2/TextureLoaderFactory.h>
#include <IGLU/texture_loader/util/VkTextureFormat.h>
#include <cstring>
#include <igl/vulkan/util/TextureFormatInt.h>
#include <numeric>
#include <vector>

Expand Down Expand Up @@ -59,8 +59,7 @@ uint32_t getTotalDataSize(uint32_t vkFormat,
uint32_t width,
uint32_t height,
uint32_t numMipLevels) {
const auto format =
igl::vulkan::util::intVkTextureFormatToTextureFormat(static_cast<int32_t>(vkFormat));
const auto format = iglu::textureloader::util::vkTextureFormatToTextureFormat(vkFormat);
const auto properties = igl::TextureFormatProperties::fromTextureFormat(format);

const auto range = igl::TextureRangeDesc::new2D(0, 0, std::max(width, 1u), std::max(height, 1u));
Expand Down Expand Up @@ -89,8 +88,7 @@ void putDfd(std::vector<uint8_t>& buffer, uint32_t vkFormat, uint32_t numMipLeve
ASSERT_TRUE(vkFormat == VK_FORMAT_PVRTC1_2BPP_UNORM_BLOCK_IMG ||
vkFormat == VK_FORMAT_ETC2_R8G8B8_UNORM_BLOCK);

const auto format =
igl::vulkan::util::intVkTextureFormatToTextureFormat(static_cast<int32_t>(vkFormat));
const auto format = iglu::textureloader::util::vkTextureFormatToTextureFormat(vkFormat);
const auto properties = igl::TextureFormatProperties::fromTextureFormat(format);

const uint16_t descriptorType = 0;
Expand Down Expand Up @@ -154,8 +152,7 @@ void putDfd(std::vector<uint8_t>& buffer, uint32_t vkFormat, uint32_t numMipLeve

void putMipLevel(std::vector<uint8_t>& buffer, uint32_t mipLevel, uint32_t imageSize) {
const auto* header = reinterpret_cast<const iglu::textureloader::ktx2::Header*>(buffer.data());
const auto format =
igl::vulkan::util::intVkTextureFormatToTextureFormat(static_cast<int32_t>(header->vkFormat));
const auto format = iglu::textureloader::util::vkTextureFormatToTextureFormat(header->vkFormat);
const auto properties = igl::TextureFormatProperties::fromTextureFormat(format);

const auto range = igl::TextureRangeDesc::new2D(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <IGLU/texture_loader/stb_hdr/Header.h>
#include <IGLU/texture_loader/stb_hdr/TextureLoaderFactory.h>
#include <cstring>
#include <igl/vulkan/util/TextureFormat.h>
#include <igl/vulkan/TextureFormat.h>
#include <numeric>
#include <vector>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <IGLU/texture_loader/stb_jpeg/Header.h>
#include <IGLU/texture_loader/stb_jpeg/TextureLoaderFactory.h>
#include <cstring>
#include <igl/vulkan/util/TextureFormat.h>
#include <igl/vulkan/TextureFormat.h>
#include <numeric>
#include <vector>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <IGLU/texture_loader/stb_png/Header.h>
#include <IGLU/texture_loader/stb_png/TextureLoaderFactory.h>
#include <cstring>
#include <igl/vulkan/util/TextureFormat.h>
#include <igl/vulkan/TextureFormat.h>
#include <numeric>
#include <vector>

Expand Down
Loading

0 comments on commit e799f09

Please sign in to comment.