Debian Codegraph @

libs/gltfio/src/DependencyGraph.cpp

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 #include "DependencyGraph.h"
18
19 using namespace filament;
20 using namespace utils;
21
22 namespace gltfio {
23
24 size_t DependencyGraph::popRenderables(Entity* result, size_t count) noexcept {
25 if (result == nullptr) {
26 return mReadyRenderables.size();
27 }
28 size_t numWritten = 0;
29 while (numWritten < count && !mReadyRenderables.empty()) {
30 result[numWritten++] = mReadyRenderables.front();
31 mReadyRenderables.pop();
32 }
33 return numWritten;
34 }
35
36 void DependencyGraph::addEdge(Entity entity, MaterialInstance* mi) {
37
38 // Permit adding an Entity-Material edge to a finalized graph as long as the material is already
39 // known. Since we already encountered this material instance, we already know what textures it
40 // is associated with.
41 assert(!mFinalized || mMaterialToEntity.find(mi) != mMaterialToEntity.end());
42
43 mMaterialToEntity[mi].insert(entity);
44 mEntityToMaterial[entity].materials.insert(mi);
45 }
46
47 void DependencyGraph::addEdge(MaterialInstance* mi, const char* parameter) {
48 assert(!mFinalized);
49 mMaterialToTexture[mi].params[parameter] = nullptr;
50 }
51
52 // During finalization, the structure of the glTF is known but we have not yet created texture
53 // objects. Find all non-textured entities and immediately add mark them as ready.
54 void DependencyGraph::finalize() {
55 assert(!mFinalized);
56 for (auto pair : mMaterialToEntity) {
57 auto mi = pair.first;
58 if (mMaterialToTexture.find(mi) == mMaterialToTexture.end()) {
59 markAsReady(mi);
60 }
61 }
62 mFinalized = true;
63 }
64
65 void DependencyGraph::refinalize() {
66 assert(mFinalized);
67 for (auto pair : mMaterialToEntity) {
68 auto material = pair.first;
69 if (mMaterialToTexture.find(material) == mMaterialToTexture.end()) {
70 markAsReady(material);
71 } else {
72 checkReadiness(material);
73 }
74 }
75 }
76
77 void DependencyGraph::addEdge(Texture* texture, MaterialInstance* material, const char* parameter) {
78 assert(mFinalized);
79 mTextureToMaterial[texture].insert(material);
80 mMaterialToTexture.at(material).params.at(parameter) = getStatus(texture);
81 }
82
83 void DependencyGraph::checkReadiness(Material* material) {
84 auto& status = mMaterialToTexture.at(material);
85
86 // Check this material's texture parameters, there are 5 in the worst case.
87 bool materialIsReady = true;
88 for (auto pair : status.params) {
89 if (!pair.second->ready) {
90 materialIsReady = false;
91 break;
92 }
93 }
94
95 // If all of its textures are ready, then the material has become ready.
96 if (materialIsReady) {
97 markAsReady(material);
98 }
99 }
100
101 void DependencyGraph::markAsReady(Texture* texture) {
102 assert(texture && mFinalized);
103 mTextureNodes.at(texture)->ready = true;
104
105 // Iterate over the materials associated with this texture to check if any have become ready.
106 // This is O(n2) but the inner loop is always small.
107 auto& materials = mTextureToMaterial.at(texture);
108 for (auto material : materials) {
109 checkReadiness(material);
110 }
111 }
112
113 void DependencyGraph::markAsReady(MaterialInstance* material) {
114 auto& entities = mMaterialToEntity.at(material);
115 for (auto entity : entities) {
116 auto& status = mEntityToMaterial.at(entity);
117 assert(status.numReadyMaterials <= status.materials.size());
118 if (status.numReadyMaterials == status.materials.size()) {
119 continue;
120 }
121 if (++status.numReadyMaterials == status.materials.size()) {
122 mReadyRenderables.push(entity);
123 }
124 }
125 }
126
127 DependencyGraph::TextureNode* DependencyGraph::getStatus(Texture* texture) {
128 auto iter = mTextureNodes.find(texture);
129 if (iter == mTextureNodes.end()) {
130 TextureNode* status = (mTextureNodes[texture] = std::make_unique<TextureNode>()).get();
131 *status = {texture, false};
132 return status;
133 }
134 return iter->second.get();
135 }
136
137 } // namespace gltfio