1 /*
2  * Copyright (c) 2024 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include "gltf2.h"
16 
17 #include <3d/gltf/gltf.h>
18 #include <3d/intf_graphics_context.h>
19 #include <core/ecs/intf_ecs.h>
20 #include <core/ecs/intf_entity_manager.h>
21 #include <core/intf_engine.h>
22 #include <core/io/intf_file_manager.h>
23 #include <core/log.h>
24 #include <core/plugin/intf_plugin_register.h>
25 #include <render/device/intf_gpu_resource_manager.h>
26 #include <render/intf_render_context.h>
27 
28 #include "data.h"
29 #include "gltf2_exporter.h"
30 #include "gltf2_importer.h"
31 #include "gltf2_loader.h"
32 
33 CORE3D_BEGIN_NAMESPACE()
34 using namespace BASE_NS;
35 using namespace CORE_NS;
36 
Gltf2(IGraphicsContext & graphicsContext)37 Gltf2::Gltf2(IGraphicsContext& graphicsContext)
38     : engine_(&(graphicsContext.GetRenderContext().GetEngine())), renderContext_(&graphicsContext.GetRenderContext()),
39       fileManager_(engine_->GetFileManager())
40 {}
41 
Gltf2(IFileManager & fileManager)42 Gltf2::Gltf2(IFileManager& fileManager) : fileManager_(fileManager) {}
43 
44 // Internal helper.
LoadGLTF(IFileManager & fileManager,const string_view uri)45 GLTFLoadResult LoadGLTF(IFileManager& fileManager, const string_view uri)
46 {
47     auto loadResult = GLTF2::LoadGLTF(fileManager, uri);
48     GLTFLoadResult result;
49     result.error = move(loadResult.error);
50     result.success = loadResult.success;
51     result.data = IGLTFData::Ptr { loadResult.data.release() };
52 
53     return result;
54 }
55 
56 // Api loading function.
LoadGLTF(const string_view uri)57 GLTFLoadResult Gltf2::LoadGLTF(const string_view uri)
58 {
59     return CORE3D_NS::LoadGLTF(fileManager_, uri);
60 }
61 
LoadGLTF(array_view<uint8_t const> data)62 GLTFLoadResult Gltf2::LoadGLTF(array_view<uint8_t const> data)
63 {
64     auto loadResult = GLTF2::LoadGLTF(fileManager_, data);
65     GLTFLoadResult result;
66     result.error = move(loadResult.error);
67     result.success = loadResult.success;
68     result.data = IGLTFData::Ptr { loadResult.data.release() };
69     return result;
70 }
71 
72 // Api import functions
ImportGltfScene(size_t sceneIndex,const IGLTFData & gltfData,const GLTFResourceData & gltfResourceData,IEcs & ecs,Entity rootEntity,GltfSceneImportFlags flags)73 Entity Gltf2::ImportGltfScene(size_t sceneIndex, const IGLTFData& gltfData, const GLTFResourceData& gltfResourceData,
74     IEcs& ecs, Entity rootEntity, GltfSceneImportFlags flags)
75 {
76     CORE_ASSERT(renderContext_);
77     if (renderContext_) {
78         const GLTF2::Data& data = static_cast<const GLTF2::Data&>(gltfData);
79         return ImportScene(renderContext_->GetDevice(), sceneIndex, data, gltfResourceData, ecs, rootEntity, flags);
80     }
81     return {};
82 }
83 
CreateGLTF2Importer(IEcs & ecs)84 IGLTF2Importer::Ptr Gltf2::CreateGLTF2Importer(IEcs& ecs)
85 {
86     CORE_ASSERT(engine_ && renderContext_);
87     if (engine_ && renderContext_) {
88         if (auto pool = ecs.GetThreadPool(); pool) {
89             return CreateGLTF2Importer(ecs, *pool);
90         }
91         return IGLTF2Importer::Ptr { new GLTF2::GLTF2Importer(*engine_, *renderContext_, ecs) };
92     }
93     return nullptr;
94 }
95 
CreateGLTF2Importer(IEcs & ecs,IThreadPool & pool)96 IGLTF2Importer::Ptr Gltf2::CreateGLTF2Importer(IEcs& ecs, IThreadPool& pool)
97 {
98     CORE_ASSERT(engine_ && renderContext_);
99     if (engine_ && renderContext_) {
100         return IGLTF2Importer::Ptr { new GLTF2::GLTF2Importer(*engine_, *renderContext_, ecs, pool) };
101     }
102     return nullptr;
103 }
104 
Load(string_view uri)105 ISceneLoader::Result Gltf2::Load(string_view uri)
106 {
107     ISceneLoader::Result sceneResult;
108     auto loadResult = GLTF2::LoadGLTF(fileManager_, uri);
109     sceneResult.error = loadResult.success ? 0 : 1;
110     sceneResult.message = BASE_NS::move(loadResult.error);
111     sceneResult.data.reset(new GLTF2::SceneData(BASE_NS::move(loadResult.data)));
112     return sceneResult;
113 }
114 
CreateSceneImporter(IEcs & ecs)115 ISceneImporter::Ptr Gltf2::CreateSceneImporter(IEcs& ecs)
116 {
117     CORE_ASSERT(engine_ && renderContext_);
118     if (engine_ && renderContext_) {
119         return ISceneImporter::Ptr { new GLTF2::Gltf2SceneImporter(*engine_, *renderContext_, ecs) };
120     }
121     return nullptr;
122 }
123 
CreateSceneImporter(IEcs & ecs,IThreadPool & pool)124 ISceneImporter::Ptr Gltf2::CreateSceneImporter(IEcs& ecs, IThreadPool& pool)
125 {
126     CORE_ASSERT(engine_ && renderContext_);
127     if (engine_ && renderContext_) {
128         return ISceneImporter::Ptr { new GLTF2::Gltf2SceneImporter(*engine_, *renderContext_, ecs, pool) };
129     }
130     return nullptr;
131 }
132 
GetSupportedExtensions() const133 array_view<const string_view> Gltf2::GetSupportedExtensions() const
134 {
135     static constexpr string_view extensions[] = { "gltf", "glb" };
136     return extensions;
137 }
138 
139 // IInterface
GetInterface(const Uid & uid) const140 const IInterface* Gltf2::GetInterface(const Uid& uid) const
141 {
142     if (uid == ISceneLoader::UID) {
143         return static_cast<const ISceneLoader*>(this);
144     }
145     if (uid == IInterface::UID) {
146         return static_cast<const IInterface*>(this);
147     }
148     return nullptr;
149 }
150 
GetInterface(const Uid & uid)151 IInterface* Gltf2::GetInterface(const Uid& uid)
152 {
153     if (uid == ISceneLoader::UID) {
154         return static_cast<ISceneLoader*>(this);
155     }
156     if (uid == IInterface::UID) {
157         return static_cast<IInterface*>(this);
158     }
159     return nullptr;
160 }
161 
Ref()162 void Gltf2::Ref() {}
163 
Unref()164 void Gltf2::Unref() {}
165 
166 // Api exporting function.
SaveGLTF(IEcs & ecs,const string_view uri)167 bool Gltf2::SaveGLTF(IEcs& ecs, const string_view uri)
168 {
169     CORE_ASSERT(engine_);
170     if (engine_) {
171         if (auto result = GLTF2::ExportGLTF(*engine_, ecs); result.success) {
172             if (auto file = fileManager_.CreateFile(uri); file) {
173                 auto const ext = uri.rfind('.');
174                 auto const extension = string_view(uri.data() + ext + 1);
175                 if (extension == "gltf") {
176                     for (auto const& buffer : result.data->buffers) {
177                         string dataFileUri = uri.substr(0, ext) + ".bin";
178                         if (auto dataFile = fileManager_.CreateFile(dataFileUri); dataFile) {
179                             dataFile->Write(buffer->data.data(), buffer->data.size());
180                             if (auto const path = dataFileUri.rfind('/'); path != string::npos) {
181                                 dataFileUri.erase(0, path + 1);
182                             }
183                             buffer->uri = dataFileUri;
184                         }
185                     }
186                     GLTF2::SaveGLTF(*result.data, *file, engine_->GetVersion());
187                 } else {
188                     GLTF2::SaveGLB(*result.data, *file, engine_->GetVersion());
189                 }
190             } else {
191                 result.error += "Failed to create file: " + uri;
192                 result.success = false;
193             }
194             return result.success;
195         } else {
196             return result.success;
197         }
198     }
199     return false;
200 }
201 CORE3D_END_NAMESPACE()
202