| 1 |
/* |
| 2 |
* Copyright (C) 2019 The Android Open Source Project |
| 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 |
|
| 17 |
#ifndef GLTFIO_FFILAMENTASSET_H |
| 18 |
#define GLTFIO_FFILAMENTASSET_H |
| 19 |
|
| 20 |
#include <gltfio/FilamentAsset.h> |
| 21 |
|
| 22 |
#include <filament/Engine.h> |
| 23 |
#include <filament/IndexBuffer.h> |
| 24 |
#include <filament/MaterialInstance.h> |
| 25 |
#include <filament/RenderableManager.h> |
| 26 |
#include <filament/Texture.h> |
| 27 |
#include <filament/TextureSampler.h> |
| 28 |
#include <filament/TransformManager.h> |
| 29 |
#include <filament/VertexBuffer.h> |
| 30 |
|
| 31 |
#include <gltfio/MaterialProvider.h> |
| 32 |
|
| 33 |
#include <math/mat4.h> |
| 34 |
|
| 35 |
#include <utils/Entity.h> |
| 36 |
|
| 37 |
#include <cgltf.h> |
| 38 |
|
| 39 |
#include "upcast.h" |
| 40 |
#include "DependencyGraph.h" |
| 41 |
#include "DracoCache.h" |
| 42 |
#include "FFilamentInstance.h" |
| 43 |
|
| 44 |
#include <tsl/robin_map.h> |
| 45 |
#include <tsl/htrie_map.h> |
| 46 |
|
| 47 |
#include <vector> |
| 48 |
|
| 49 |
#ifdef NDEBUG |
| 50 |
#define GLTFIO_VERBOSE 0 |
| 51 |
#define GLTFIO_WARN(msg) |
| 52 |
#else |
| 53 |
#define GLTFIO_VERBOSE 1 |
| 54 |
#define GLTFIO_WARN(msg) slog.w << msg << io::endl |
| 55 |
#endif |
| 56 |
|
| 57 |
namespace utils { |
| 58 |
class NameComponentManager; |
| 59 |
class EntityManager; |
| 60 |
} |
| 61 |
|
| 62 |
namespace gltfio { |
| 63 |
|
| 64 |
class Animator; |
| 65 |
class Wireframe; |
| 66 |
|
| 67 |
// Encapsulates VertexBuffer::setBufferAt() or IndexBuffer::setBuffer(). |
| 68 |
struct BufferSlot { |
| 69 |
const cgltf_accessor* accessor; |
| 70 |
cgltf_attribute_type attribute; |
| 71 |
int bufferIndex; // for vertex buffers only |
| 72 |
int morphTarget; // 0 if no morphing, otherwise 1-based index |
| 73 |
filament::VertexBuffer* vertexBuffer; |
| 74 |
filament::IndexBuffer* indexBuffer; |
| 75 |
}; |
| 76 |
|
| 77 |
// Encapsulates a connection between Texture and MaterialInstance. |
| 78 |
struct TextureSlot { |
| 79 |
const cgltf_texture* texture; |
| 80 |
filament::MaterialInstance* materialInstance; |
| 81 |
const char* materialParameter; |
| 82 |
filament::TextureSampler sampler; |
| 83 |
bool srgb; |
| 84 |
}; |
| 85 |
|
| 86 |
// MeshCache |
| 87 |
// --------- |
| 88 |
// If a given glTF mesh is referenced by multiple glTF nodes, then it generates a separate Filament |
| 89 |
// renderable for each of those nodes. All renderables generated by a given mesh share a common set |
| 90 |
// of VertexBuffer and IndexBuffer objects. To achieve the sharing behavior, the loader maintains a |
| 91 |
// small cache. The cache keys are glTF mesh definitions and the cache entries are lists of |
| 92 |
// primitives, where a "primitive" is a reference to a Filament VertexBuffer and IndexBuffer. |
| 93 |
struct Primitive { |
| 94 |
filament::VertexBuffer* vertices = nullptr; |
| 95 |
filament::IndexBuffer* indices = nullptr; |
| 96 |
filament::Aabb aabb; // object-space bounding box |
| 97 |
UvMap uvmap; // small mapping from each glTF UV set to either UV0 or UV1 |
| 98 |
}; |
| 99 |
using MeshCache = tsl::robin_map<const cgltf_mesh*, std::vector<Primitive>>; |
| 100 |
|
| 101 |
// MatInstanceCache |
| 102 |
// ---------------- |
| 103 |
// Each glTF material definition corresponds to a single filament::MaterialInstance, which are |
| 104 |
// temporarily cached during loading. The filament::Material objects that are used to create instances are |
| 105 |
// cached in MaterialProvider. If a given glTF material is referenced by multiple glTF meshes, then |
| 106 |
// their corresponding filament primitives will share the same Filament MaterialInstance and UvMap. |
| 107 |
// The UvMap is a mapping from each texcoord slot in glTF to one of Filament's 2 texcoord sets. |
| 108 |
struct MaterialEntry { |
| 109 |
filament::MaterialInstance* instance; |
| 110 |
UvMap uvmap; |
| 111 |
}; |
| 112 |
using MatInstanceCache = tsl::robin_map<intptr_t, MaterialEntry>; |
| 113 |
|
| 114 |
struct FFilamentAsset : public FilamentAsset { |
| 115 |
FFilamentAsset(filament::Engine* engine, utils::NameComponentManager* names, |
| 116 |
utils::EntityManager* entityManager, const cgltf_data* srcAsset) : |
| 117 |
mEngine(engine), mNameManager(names), mEntityManager(entityManager) { |
| 118 |
mSourceAsset.reset(new SourceAsset {(cgltf_data*)srcAsset}); |
| 119 |
} |
| 120 |
|
| 121 |
~FFilamentAsset(); |
| 122 |
|
| 123 |
size_t getEntityCount() const noexcept { |
| 124 |
return mEntities.size(); |
| 125 |
} |
| 126 |
|
| 127 |
const utils::Entity* getEntities() const noexcept { |
| 128 |
return mEntities.empty() ? nullptr : mEntities.data(); |
| 129 |
} |
| 130 |
|
| 131 |
const utils::Entity* getLightEntities() const noexcept { |
| 132 |
return mLightEntities.empty() ? nullptr : mLightEntities.data(); |
| 133 |
} |
| 134 |
|
| 135 |
size_t getLightEntityCount() const noexcept { |
| 136 |
return mLightEntities.size(); |
| 137 |
} |
| 138 |
|
| 139 |
const utils::Entity* getCameraEntities() const noexcept { |
| 140 |
return mCameraEntities.empty() ? nullptr : mCameraEntities.data(); |
| 141 |
} |
| 142 |
|
| 143 |
size_t getCameraEntityCount() const noexcept { |
| 144 |
return mCameraEntities.size(); |
| 145 |
} |
| 146 |
|
| 147 |
utils::Entity getRoot() const noexcept { |
| 148 |
return mRoot; |
| 149 |
} |
| 150 |
|
| 151 |
size_t popRenderables(utils::Entity* entities, size_t count) noexcept { |
| 152 |
return mDependencyGraph.popRenderables(entities, count); |
| 153 |
} |
| 154 |
|
| 155 |
size_t getMaterialInstanceCount() const noexcept { |
| 156 |
return mMaterialInstances.size(); |
| 157 |
} |
| 158 |
|
| 159 |
const filament::MaterialInstance* const* getMaterialInstances() const noexcept { |
| 160 |
return mMaterialInstances.data(); |
| 161 |
} |
| 162 |
|
| 163 |
filament::MaterialInstance* const* getMaterialInstances() noexcept { |
| 164 |
return mMaterialInstances.data(); |
| 165 |
} |
| 166 |
|
| 167 |
size_t getResourceUriCount() const noexcept { |
| 168 |
return mResourceUris.size(); |
| 169 |
} |
| 170 |
|
| 171 |
const char* const* getResourceUris() const noexcept { |
| 172 |
return mResourceUris.data(); |
| 173 |
} |
| 174 |
|
| 175 |
filament::Aabb getBoundingBox() const noexcept { |
| 176 |
return mBoundingBox; |
| 177 |
} |
| 178 |
|
| 179 |
const char* getName(utils::Entity entity) const noexcept; |
| 180 |
|
| 181 |
utils::Entity getFirstEntityByName(const char* name) noexcept; |
| 182 |
|
| 183 |
size_t getEntitiesByName(const char* name, utils::Entity* entities, |
| 184 |
size_t maxCount) const noexcept; |
| 185 |
|
| 186 |
size_t getEntitiesByPrefix(const char* prefix, utils::Entity* entities, |
| 187 |
size_t maxCount) const noexcept; |
| 188 |
|
| 189 |
Animator* getAnimator() noexcept; |
| 190 |
|
| 191 |
utils::Entity getWireframe() noexcept; |
| 192 |
|
| 193 |
filament::Engine* getEngine() const noexcept { |
| 194 |
return mEngine; |
| 195 |
} |
| 196 |
|
| 197 |
void releaseSourceData() noexcept; |
| 198 |
|
| 199 |
const void* getSourceAsset() const noexcept { |
| 200 |
return mSourceAsset.get() ? mSourceAsset->hierarchy : nullptr; |
| 201 |
} |
| 202 |
|
| 203 |
FilamentInstance** getAssetInstances() noexcept { |
| 204 |
return (FilamentInstance**) mInstances.data(); |
| 205 |
} |
| 206 |
|
| 207 |
size_t getAssetInstanceCount() const noexcept { |
| 208 |
return mInstances.size(); |
| 209 |
} |
| 210 |
|
| 211 |
void takeOwnership(filament::Texture* texture) { |
| 212 |
mTextures.push_back(texture); |
| 213 |
} |
| 214 |
|
| 215 |
void bindTexture(const TextureSlot& tb, filament::Texture* texture) { |
| 216 |
tb.materialInstance->setParameter(tb.materialParameter, texture, tb.sampler); |
| 217 |
mDependencyGraph.addEdge(texture, tb.materialInstance, tb.materialParameter); |
| 218 |
} |
| 219 |
|
| 220 |
bool isInstanced() const { |
| 221 |
return mInstances.size() > 0; |
| 222 |
} |
| 223 |
|
| 224 |
filament::Engine* mEngine; |
| 225 |
utils::NameComponentManager* mNameManager; |
| 226 |
utils::EntityManager* mEntityManager; |
| 227 |
std::vector<utils::Entity> mEntities; |
| 228 |
std::vector<utils::Entity> mLightEntities; |
| 229 |
std::vector<utils::Entity> mCameraEntities; |
| 230 |
std::vector<filament::MaterialInstance*> mMaterialInstances; |
| 231 |
std::vector<filament::VertexBuffer*> mVertexBuffers; |
| 232 |
std::vector<filament::BufferObject*> mBufferObjects; |
| 233 |
std::vector<filament::IndexBuffer*> mIndexBuffers; |
| 234 |
std::vector<filament::Texture*> mTextures; |
| 235 |
filament::Aabb mBoundingBox; |
| 236 |
utils::Entity mRoot; |
| 237 |
std::vector<FFilamentInstance*> mInstances; |
| 238 |
SkinVector mSkins; // unused for instanced assets |
| 239 |
Animator* mAnimator = nullptr; |
| 240 |
Wireframe* mWireframe = nullptr; |
| 241 |
bool mResourcesLoaded = false; |
| 242 |
DependencyGraph mDependencyGraph; |
| 243 |
tsl::htrie_map<char, std::vector<utils::Entity>> mNameToEntity; |
| 244 |
|
| 245 |
// Sentinels for situations where ResourceLoader needs to generate data. |
| 246 |
const cgltf_accessor mGenerateNormals = {}; |
| 247 |
const cgltf_accessor mGenerateTangents = {}; |
| 248 |
|
| 249 |
// Encapsulates reference-counted source data, which includes the cgltf hierachy |
| 250 |
// and potentially also includes buffer data that can be uploaded to the GPU. |
| 251 |
struct SourceAsset { |
| 252 |
~SourceAsset() { cgltf_free(hierarchy); } |
| 253 |
cgltf_data* hierarchy; |
| 254 |
DracoCache dracoCache; |
| 255 |
std::vector<uint8_t> glbData; |
| 256 |
}; |
| 257 |
|
| 258 |
// We used shared ownership for the raw cgltf data in order to permit ResourceLoader to |
| 259 |
// complete various asynchronous work (e.g. uploading buffers to the GPU) even after the asset |
| 260 |
// or ResourceLoader have been destroyed. |
| 261 |
using SourceHandle = std::shared_ptr<SourceAsset>; |
| 262 |
SourceHandle mSourceAsset; |
| 263 |
|
| 264 |
// Transient source data that can freed via releaseSourceData: |
| 265 |
std::vector<BufferSlot> mBufferSlots; |
| 266 |
std::vector<TextureSlot> mTextureSlots; |
| 267 |
std::vector<const char*> mResourceUris; |
| 268 |
NodeMap mNodeMap; // unused for instanced assets |
| 269 |
std::vector<std::pair<const cgltf_primitive*, filament::VertexBuffer*> > mPrimitives; |
| 270 |
MatInstanceCache mMatInstanceCache; |
| 271 |
MeshCache mMeshCache; |
| 272 |
}; |
| 273 |
|
| 274 |
FILAMENT_UPCAST(FilamentAsset) |
| 275 |
|
| 276 |
} // namespace gltfio |
| 277 |
|
| 278 |
#endif // GLTFIO_FFILAMENTASSET_H |