Skip to content

Commit a4a89af

Browse files
AssetLoader: split GLTF document loading
Move GLTF document loading, image callbacks, and texture source queries into GLTFDocument.
1 parent 641e02e commit a4a89af

7 files changed

Lines changed: 1134 additions & 953 deletions

File tree

AssetLoader/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ set(INCLUDE
77
)
88

99
set(INTERFACE
10+
interface/GLTFDocument.hpp
1011
interface/GLTFLoader.hpp
1112
interface/GLTFBuilder.hpp
1213
interface/TinyGltfModelView.hpp
@@ -17,6 +18,7 @@ set(INTERFACE
1718
)
1819

1920
set(SOURCE
21+
src/GLTFDocument.cpp
2022
src/GLTFLoader.cpp
2123
src/GLTFBuilder.cpp
2224
src/GLTFUtilities.cpp
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
/*
2+
* Copyright 2026 Diligent Graphics LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* In no event and under no legal theory, whether in tort (including negligence),
17+
* contract, or otherwise, unless required by applicable law (such as deliberate
18+
* and grossly negligent acts) or agreed to in writing, shall any Contributor be
19+
* liable for any damages, including any direct, indirect, special, incidental,
20+
* or consequential damages of any character arising as a result of this License or
21+
* out of the use or inability to use the software (including but not limited to damages
22+
* for loss of goodwill, work stoppage, computer failure or malfunction, or any and
23+
* all other commercial damages or losses), even if such Contributor has been advised
24+
* of the possibility of such damages.
25+
*/
26+
27+
#pragma once
28+
29+
/// \file
30+
/// GLTF document loading and metadata queries.
31+
32+
#include <functional>
33+
#include <memory>
34+
#include <string>
35+
#include <unordered_map>
36+
#include <vector>
37+
38+
#include "../../../DiligentCore/Platforms/interface/PlatformMisc.hpp"
39+
#include "../../../DiligentCore/Common/interface/RefCntAutoPtr.hpp"
40+
#include "../../../DiligentCore/Common/interface/SharedMutex.hpp"
41+
42+
namespace tinygltf
43+
{
44+
45+
class Model;
46+
struct Texture;
47+
48+
} // namespace tinygltf
49+
50+
namespace Diligent
51+
{
52+
53+
struct ITexture;
54+
55+
namespace GLTF
56+
{
57+
58+
class ResourceManager;
59+
60+
/// Texture cache used by the GLTF loader.
61+
struct TextureCacheType
62+
{
63+
Threading::SharedMutex TexturesMtx;
64+
65+
std::unordered_map<std::string, RefCntWeakPtr<ITexture>> Textures;
66+
};
67+
68+
/// GLTF document load information.
69+
struct DocumentLoadInfo
70+
{
71+
using FileExistsCallbackType = std::function<bool(const char* FilePath)>;
72+
using ReadWholeFileCallbackType = std::function<bool(const char* FilePath, std::vector<unsigned char>& Data, std::string& Error)>;
73+
74+
/// File name.
75+
const char* FileName = nullptr;
76+
77+
/// Optional callback function that will be called by the loader to check if the file exists.
78+
FileExistsCallbackType FileExistsCallback = nullptr;
79+
80+
/// Optional callback function that will be called by the loader to read the whole file.
81+
ReadWholeFileCallbackType ReadWholeFileCallback = nullptr;
82+
83+
/// Optional texture cache to use when loading images referenced by the document.
84+
TextureCacheType* pTextureCache = nullptr;
85+
86+
/// Optional resource manager to use when loading images referenced by the document.
87+
ResourceManager* pResourceManager = nullptr;
88+
89+
/// Whether image data should be decoded into pixels while loading the document.
90+
///
91+
/// When this is false, the document still parses image and texture metadata.
92+
/// Images stored in buffer views remain addressable through tinygltf::Image::bufferView
93+
/// without duplicating their bytes in tinygltf::Image::image. External image files with
94+
/// known image extensions are not read by the document loader and remain addressable
95+
/// through tinygltf::Image::uri. Embedded data URI images are copied into
96+
/// tinygltf::Image::image as encoded image bytes because tinygltf supplies them
97+
/// through temporary callback storage.
98+
bool DecodeImages = true;
99+
};
100+
101+
/// Resolved texture source referenced by a GLTF texture.
102+
struct TextureSourceInfo
103+
{
104+
/// Index in tinygltf::Model::textures.
105+
Uint32 TextureIndex = 0;
106+
107+
/// Index in tinygltf::Model::images.
108+
int ImageIndex = -1;
109+
110+
/// Index in tinygltf::Model::samplers.
111+
int SamplerIndex = -1;
112+
113+
/// Resolved external image URI. Empty for embedded image data.
114+
std::string URI;
115+
116+
/// Pointer to encoded image data for embedded buffer-view or data URI images.
117+
const void* pData = nullptr;
118+
119+
/// Size of encoded image data in bytes.
120+
Uint64 DataSize = 0;
121+
};
122+
123+
/// Parsed GLTF document.
124+
class Document
125+
{
126+
public:
127+
explicit Document(const DocumentLoadInfo& LoadInfo);
128+
~Document();
129+
130+
// clang-format off
131+
Document (const Document&) = delete;
132+
Document& operator=(const Document&) = delete;
133+
// clang-format on
134+
135+
const tinygltf::Model& GetModel() const noexcept;
136+
const std::string& GetBaseDir() const noexcept;
137+
138+
/// Returns the number of textures in the document.
139+
Uint32 GetTextureCount() const;
140+
141+
/// Resolves a GLTF texture to either an external URI or an embedded encoded-data span.
142+
///
143+
/// Embedded buffer-view spans are owned by the document buffers. Embedded data URI
144+
/// spans are owned by tinygltf::Image::image. In both cases the returned pointer
145+
/// remains valid only while the document is alive and unchanged.
146+
bool GetTextureSourceInfo(Uint32 TextureIndex, TextureSourceInfo& Source) const;
147+
148+
private:
149+
std::string m_FileName;
150+
std::string m_BaseDir;
151+
152+
std::vector<RefCntAutoPtr<IObject>> m_TexturesHold;
153+
std::unique_ptr<tinygltf::Model> m_pModel;
154+
};
155+
156+
int GetTextureImageIndex(const tinygltf::Model& GltfModel,
157+
const tinygltf::Texture& GltfTexture);
158+
159+
} // namespace GLTF
160+
161+
} // namespace Diligent

AssetLoader/interface/GLTFLoader.hpp

Lines changed: 1 addition & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
#include "../../../DiligentCore/Common/interface/AdvancedMath.hpp"
5151
#include "../../../DiligentCore/Common/interface/SharedMutex.hpp"
5252
#include "../../../DiligentCore/Common/interface/STDAllocator.hpp"
53+
#include "GLTFDocument.hpp"
5354
#include "GLTFResourceManager.hpp"
5455

5556
namespace tinygltf
@@ -76,9 +77,6 @@ class MeshLoader;
7677
class ModelBuilder;
7778
class MaterialBuilder;
7879

79-
int GetTextureImageIndex(const tinygltf::Model& GltfModel,
80-
const tinygltf::Texture& GltfTexture);
81-
8280
/// Texture attribute description.
8381
struct TextureAttributeDesc
8482
{
@@ -729,101 +727,6 @@ static constexpr std::array<VertexAttributeDesc, 8> DefaultVertexAttributes =
729727
InputLayoutDescX VertexAttributesToInputLayout(const VertexAttributeDesc* pAttributes, size_t NumAttributes);
730728

731729

732-
struct TextureCacheType
733-
{
734-
Threading::SharedMutex TexturesMtx;
735-
736-
std::unordered_map<std::string, RefCntWeakPtr<ITexture>> Textures;
737-
};
738-
739-
/// GLTF document load information.
740-
struct DocumentLoadInfo
741-
{
742-
using FileExistsCallbackType = std::function<bool(const char* FilePath)>;
743-
using ReadWholeFileCallbackType = std::function<bool(const char* FilePath, std::vector<unsigned char>& Data, std::string& Error)>;
744-
745-
/// File name.
746-
const char* FileName = nullptr;
747-
748-
/// Optional callback function that will be called by the loader to check if the file exists.
749-
FileExistsCallbackType FileExistsCallback = nullptr;
750-
751-
/// Optional callback function that will be called by the loader to read the whole file.
752-
ReadWholeFileCallbackType ReadWholeFileCallback = nullptr;
753-
754-
/// Optional texture cache to use when loading images referenced by the document.
755-
TextureCacheType* pTextureCache = nullptr;
756-
757-
/// Optional resource manager to use when loading images referenced by the document.
758-
ResourceManager* pResourceManager = nullptr;
759-
760-
/// Whether image data should be decoded into pixels while loading the document.
761-
///
762-
/// When this is false, the document still parses image and texture metadata.
763-
/// Images stored in buffer views remain addressable through tinygltf::Image::bufferView
764-
/// without duplicating their bytes in tinygltf::Image::image. External image files with
765-
/// known image extensions are not read by the document loader and remain addressable
766-
/// through tinygltf::Image::uri. Embedded data URI images are copied into
767-
/// tinygltf::Image::image as encoded image bytes because tinygltf supplies them
768-
/// through temporary callback storage.
769-
bool DecodeImages = true;
770-
};
771-
772-
/// Resolved texture source referenced by a GLTF texture.
773-
struct TextureSourceInfo
774-
{
775-
/// Index in tinygltf::Model::textures.
776-
Uint32 TextureIndex = 0;
777-
778-
/// Index in tinygltf::Model::images.
779-
int ImageIndex = -1;
780-
781-
/// Index in tinygltf::Model::samplers.
782-
int SamplerIndex = -1;
783-
784-
/// Resolved external image URI. Empty for embedded image data.
785-
std::string URI;
786-
787-
/// Pointer to encoded image data for embedded buffer-view or data URI images.
788-
const void* pData = nullptr;
789-
790-
/// Size of encoded image data in bytes.
791-
Uint64 DataSize = 0;
792-
};
793-
794-
/// Parsed GLTF document.
795-
class Document
796-
{
797-
public:
798-
explicit Document(const DocumentLoadInfo& LoadInfo);
799-
~Document();
800-
801-
// clang-format off
802-
Document (const Document&) = delete;
803-
Document& operator=(const Document&) = delete;
804-
// clang-format on
805-
806-
const tinygltf::Model& GetModel() const noexcept;
807-
const std::string& GetBaseDir() const noexcept;
808-
809-
/// Returns the number of textures in the document.
810-
Uint32 GetTextureCount() const;
811-
812-
/// Resolves a GLTF texture to either an external URI or an embedded encoded-data span.
813-
///
814-
/// Embedded buffer-view spans are owned by the document buffers. Embedded data URI
815-
/// spans are owned by tinygltf::Image::image. In both cases the returned pointer
816-
/// remains valid only while the document is alive and unchanged.
817-
bool GetTextureSourceInfo(Uint32 TextureIndex, TextureSourceInfo& Source) const;
818-
819-
private:
820-
std::string m_FileName;
821-
std::string m_BaseDir;
822-
823-
std::vector<RefCntAutoPtr<IObject>> m_TexturesHold;
824-
std::unique_ptr<tinygltf::Model> m_pModel;
825-
};
826-
827730
/// Model create information
828731
struct ModelCreateInfo
829732
{

0 commit comments

Comments
 (0)