|
| 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 |
0 commit comments