Debian Codegraph @

libs/gltfio/src/DependencyGraph.h

raw
1 /*
2 * Copyright (C) 2020 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_DEPENDENCY_GRAPH_H
18 #define GLTFIO_DEPENDENCY_GRAPH_H
19
20 #include <utils/Entity.h>
21
22 #include <tsl/robin_map.h>
23 #include <tsl/robin_set.h>
24
25 #include <queue>
26 #include <string>
27
28 namespace filament {
29 class MaterialInstance;
30 class Texture;
31 }
32
33 namespace gltfio {
34
35 /**
36 * Internal graph that enables FilamentAsset to discover "ready-to-render" entities by tracking
37 * the loading status of Texture objects that each entity depends on.
38 *
39 * Renderables connect to a set of material instances, which in turn connect to a set of parameter
40 * names, which in turn connect to a set of texture objects. These relationships are not easily
41 * inspectable using the Filament API or ECS.
42 *
43 * One graph corresponds to a single glTF asset. The graph only contains weak references, it does
44 * not have ownership over any Filament objects. Here's an example:
45 *
46 * Entity Entity Entity Entity
47 * | / \ | /
48 * | / \ | /
49 * Material Material Material
50 * / | \ |
51 * / | \ |
52 * Param Param Param Param
53 * \ / | |
54 * \ / | |
55 * Texture Texture Texture
56 *
57 * Note that the left-most entity in the above graph has no textures, so it becomes ready as soon as
58 * finalize is called.
59 */
60 class DependencyGraph {
61 public:
62 using Material = filament::MaterialInstance;
63 using Entity = utils::Entity;
64
65 // Pops up to "count" ready-to-render entities off the queue.
66 // If "result" is non-null, returns the number of written items.
67 // If "result" is null, returns the number of available entities.
68 size_t popRenderables(Entity* result, size_t count) noexcept;
69
70 // These are called during the initial asset loader phase.
71 void addEdge(Entity entity, Material* material);
72 void addEdge(Material* material, const char* parameter);
73
74 // This is called at the end of the initial asset loading phase.
75 // Makes a guarantee that no new material nodes or parameter nodes will be added to the graph.
76 void finalize();
77
78 // This can be called after finalization to allow for dynamic addition of entities.
79 // It is slower than finalize() because it checks the readiness of existing materials.
80 void refinalize();
81
82 // These are called after textures have created and decoded.
83 void addEdge(filament::Texture* texture, Material* material, const char* parameter);
84 void markAsReady(filament::Texture* texture);
85
86 private:
87 struct TextureNode {
88 filament::Texture* texture;
89 bool ready;
90 };
91
92 struct MaterialNode {
93 tsl::robin_map<std::string, TextureNode*> params;
94 };
95
96 struct EntityNode {
97 tsl::robin_set<Material*> materials;
98 size_t numReadyMaterials = 0;
99 };
100
101 void checkReadiness(Material* material);
102 void markAsReady(Material* material);
103 TextureNode* getStatus(filament::Texture* texture);
104
105 // The following maps contain the directed edges in the graph.
106 tsl::robin_map<Entity, EntityNode> mEntityToMaterial;
107 tsl::robin_map<Material*, tsl::robin_set<Entity>> mMaterialToEntity;
108 tsl::robin_map<Material*, MaterialNode> mMaterialToTexture;
109 tsl::robin_map<filament::Texture*, tsl::robin_set<Material*>> mTextureToMaterial;
110
111 // Each texture (and its readiness flag) can be referenced from multiple nodes, so we own
112 // a collection of wrapper objects in the following map. This uses std::unique_ptr to allow
113 // nodes to refer to a texture wrapper using a stable weak pointer.
114 tsl::robin_map<filament::Texture*, std::unique_ptr<TextureNode>> mTextureNodes;
115
116 std::queue<Entity> mReadyRenderables;
117 bool mFinalized = false;
118 };
119
120 } // namespace gltfio
121
122 #endif // GLTFIO_DEPENDENCY_GRAPH_H