Skip to content

Commit 28b7c49

Browse files
committed
Adding swizzling for 1-channel and KTX images + check for valid glTF scenes
Script sync from SHA: 92d978025e38b8744bc05a83d3342e73a1a5e09e
1 parent 2915dc9 commit 28b7c49

File tree

4 files changed

+56
-13
lines changed

4 files changed

+56
-13
lines changed

nvvkgltf/scene.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019-2025, NVIDIA CORPORATION. All rights reserved.
2+
* Copyright (c) 2019-2026, NVIDIA CORPORATION. All rights reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*
16-
* SPDX-FileCopyrightText: Copyright (c) 2019-2025, NVIDIA CORPORATION.
16+
* SPDX-FileCopyrightText: Copyright (c) 2019-2026, NVIDIA CORPORATION.
1717
* SPDX-License-Identifier: Apache-2.0
1818
*/
1919

@@ -90,6 +90,8 @@ bool nvvkgltf::Scene::load(const std::filesystem::path& filename)
9090
const std::string filenameUtf8 = nvutils::utf8FromPath(filename);
9191
LOGI("%s%s\n", nvutils::ScopedTimer::indent().c_str(), filenameUtf8.c_str());
9292

93+
m_validSceneParsed = false;
94+
9395
m_filename = filename;
9496
m_model = {};
9597
tinygltf::TinyGLTF tcontext;
@@ -288,7 +290,9 @@ bool nvvkgltf::Scene::load(const std::filesystem::path& filename)
288290
m_currentVariant = 0; // Default KHR_materials_variants
289291
parseScene();
290292

291-
return result;
293+
m_validSceneParsed = !m_renderNodes.empty();
294+
295+
return m_validSceneParsed;
292296
}
293297

294298
bool nvvkgltf::Scene::save(const std::filesystem::path& filename)
@@ -609,7 +613,8 @@ void nvvkgltf::Scene::destroy()
609613
{
610614
clearParsedData();
611615
m_filename.clear();
612-
m_model = {};
616+
m_validSceneParsed = false;
617+
m_model = {};
613618
}
614619

615620

nvvkgltf/scene.hpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019-2025, NVIDIA CORPORATION. All rights reserved.
2+
* Copyright (c) 2019-2026, NVIDIA CORPORATION. All rights reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*
16-
* SPDX-FileCopyrightText: Copyright (c) 2019-2025, NVIDIA CORPORATION.
16+
* SPDX-FileCopyrightText: Copyright (c) 2019-2026, NVIDIA CORPORATION.
1717
* SPDX-License-Identifier: Apache-2.0
1818
*/
1919

@@ -164,7 +164,7 @@ class Scene
164164
// Getters
165165
const tinygltf::Model& getModel() const { return m_model; }
166166
tinygltf::Model& getModel() { return m_model; }
167-
bool valid() const { return !m_renderNodes.empty(); }
167+
bool valid() const { return m_validSceneParsed; }
168168

169169
// Animation Management
170170
void updateRenderNodes(); // Update the render nodes matrices and materials
@@ -299,6 +299,8 @@ class Scene
299299
int m_sceneRootNode = -1; // Node index of the root
300300
int m_sceneCameraNode = -1; // Node index of the camera
301301
nvutils::Bbox m_sceneBounds; // Scene bounds
302+
303+
bool m_validSceneParsed = false;
302304
};
303305

304306
} // namespace nvvkgltf

nvvkgltf/scene_vk.cpp

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2022-2025, NVIDIA CORPORATION. All rights reserved.
2+
* Copyright (c) 2022-2026, NVIDIA CORPORATION. All rights reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*
16-
* SPDX-FileCopyrightText: Copyright (c) 2022-2025, NVIDIA CORPORATION.
16+
* SPDX-FileCopyrightText: Copyright (c) 2022-2026, NVIDIA CORPORATION.
1717
* SPDX-License-Identifier: Apache-2.0
1818
*/
1919

@@ -50,6 +50,34 @@ namespace {
5050
constexpr std::string_view kMemCategoryGeometry = "Geometry";
5151
constexpr std::string_view kMemCategorySceneData = "SceneData";
5252
constexpr std::string_view kMemCategoryImages = "Images";
53+
54+
// Convert KTX swizzle to Vulkan component swizzle
55+
VkComponentSwizzle ktxSwizzleToVk(nv_ktx::KTX_SWIZZLE swizzle)
56+
{
57+
switch(swizzle)
58+
{
59+
case nv_ktx::KTX_SWIZZLE::ZERO:
60+
return VK_COMPONENT_SWIZZLE_ZERO;
61+
case nv_ktx::KTX_SWIZZLE::ONE:
62+
return VK_COMPONENT_SWIZZLE_ONE;
63+
case nv_ktx::KTX_SWIZZLE::R:
64+
return VK_COMPONENT_SWIZZLE_R;
65+
case nv_ktx::KTX_SWIZZLE::G:
66+
return VK_COMPONENT_SWIZZLE_G;
67+
case nv_ktx::KTX_SWIZZLE::B:
68+
return VK_COMPONENT_SWIZZLE_B;
69+
case nv_ktx::KTX_SWIZZLE::A:
70+
return VK_COMPONENT_SWIZZLE_A;
71+
default:
72+
return VK_COMPONENT_SWIZZLE_IDENTITY;
73+
}
74+
}
75+
76+
// Convert KTX swizzle array to VkComponentMapping
77+
VkComponentMapping ktxSwizzleToVkComponentMapping(const std::array<nv_ktx::KTX_SWIZZLE, 4>& swizzle)
78+
{
79+
return {ktxSwizzleToVk(swizzle[0]), ktxSwizzleToVk(swizzle[1]), ktxSwizzleToVk(swizzle[2]), ktxSwizzleToVk(swizzle[3])};
80+
}
5381
} // namespace
5482

5583
//--------------------------------------------------------------------------------------------------
@@ -1211,7 +1239,8 @@ void nvvkgltf::SceneVk::loadImage(const std::filesystem::path& basedir, const ti
12111239
LOGW("This KTX image had %u array elements, but loadImage() cannot handle array textures.\n", ktxImage.num_layers_possibly_0);
12121240
return;
12131241
}
1214-
image.format = texture_formats::tryForceVkFormatTransferFunction(ktxImage.format, image.srgb);
1242+
image.format = texture_formats::tryForceVkFormatTransferFunction(ktxImage.format, image.srgb);
1243+
image.componentMapping = ktxSwizzleToVkComponentMapping(ktxImage.swizzle);
12151244

12161245
// Add all mip-levels. We don't need the ktxImage after this so we can move instead of copy.
12171246
for(uint32_t i = 0; i < ktxImage.num_mips; i++)
@@ -1268,6 +1297,8 @@ void nvvkgltf::SceneVk::loadImage(const std::filesystem::path& basedir, const ti
12681297
{
12691298
case 1:
12701299
image.format = is16Bit ? VK_FORMAT_R16_UNORM : VK_FORMAT_R8_UNORM;
1300+
// For 1-component textures, expand the single channel to RGB for proper grayscale display
1301+
image.componentMapping = {VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_R, VK_COMPONENT_SWIZZLE_ONE};
12711302
break;
12721303
case 4:
12731304
image.format = is16Bit ? VK_FORMAT_R16G16B16A16_UNORM :
@@ -1327,8 +1358,12 @@ bool nvvkgltf::SceneVk::createImage(const VkCommandBuffer& cmd, nvvk::StagingUpl
13271358
imageCreateInfo.mipLevels = nvvk::mipLevels(imgSize);
13281359
}
13291360

1361+
// Use custom view info with component mapping (e.g. for grayscale textures)
1362+
VkImageViewCreateInfo imageViewCreateInfo = DEFAULT_VkImageViewCreateInfo;
1363+
imageViewCreateInfo.components = image.componentMapping;
1364+
13301365
nvvk::Image resultImage;
1331-
NVVK_CHECK(m_alloc->createImage(resultImage, imageCreateInfo, DEFAULT_VkImageViewCreateInfo));
1366+
NVVK_CHECK(m_alloc->createImage(resultImage, imageCreateInfo, imageViewCreateInfo));
13321367
NVVK_DBG_NAME(resultImage.image);
13331368
NVVK_DBG_NAME(resultImage.descriptor.imageView);
13341369

nvvkgltf/scene_vk.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2022-2025, NVIDIA CORPORATION. All rights reserved.
2+
* Copyright (c) 2022-2026, NVIDIA CORPORATION. All rights reserved.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*
16-
* SPDX-FileCopyrightText: Copyright (c) 2022-2025, NVIDIA CORPORATION.
16+
* SPDX-FileCopyrightText: Copyright (c) 2022-2026, NVIDIA CORPORATION.
1717
* SPDX-License-Identifier: Apache-2.0
1818
*/
1919

@@ -123,6 +123,7 @@ class SceneVk
123123
std::string imgName{};
124124
VkExtent2D size{0, 0};
125125
VkFormat format{VK_FORMAT_UNDEFINED};
126+
VkComponentMapping componentMapping{}; // Component swizzle for image view (e.g. grayscale expansion)
126127
std::vector<std::vector<char>> mipData{};
127128
};
128129

0 commit comments

Comments
 (0)