-
Notifications
You must be signed in to change notification settings - Fork 2k
materials: introduce MaterialCache #9205
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7acee6c
materials: introduce MaterialCache
elizagamedev 8080a46
material cache: key on crc32
elizagamedev fd888ef
material cache: make materialParser private
elizagamedev d6cd931
move RefCountedMap to utils and add unit tests
elizagamedev f09fe89
material cache: make create functions private
elizagamedev 5ed999e
material cache: fix broken tests on iOS/web
elizagamedev 28c1031
material cache: address more comments
elizagamedev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright (C) 2025 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include "MaterialCache.h" | ||
#include "MaterialParser.h" | ||
|
||
#include <backend/DriverEnums.h> | ||
|
||
#include <details/Engine.h> | ||
#include <details/Material.h> | ||
|
||
#include <utils/Logger.h> | ||
|
||
namespace filament { | ||
|
||
size_t MaterialCache::Key::Hash::operator()( | ||
filament::MaterialCache::Key const& key) const noexcept { | ||
uint32_t crc; | ||
if (key.parser->getMaterialCrc32(&crc)) { | ||
return size_t(crc); | ||
} | ||
return size_t(key.parser->computeCrc32()); | ||
} | ||
|
||
bool MaterialCache::Key::operator==(Key const& rhs) const noexcept { | ||
return parser == rhs.parser; | ||
} | ||
|
||
MaterialCache::~MaterialCache() { | ||
if (!mDefinitions.empty()) { | ||
LOG(WARNING) << "MaterialCache was destroyed but wasn't empty"; | ||
} | ||
} | ||
|
||
MaterialDefinition* UTILS_NULLABLE MaterialCache::acquire(FEngine& engine, | ||
const void* UTILS_NONNULL data, size_t size) noexcept { | ||
std::unique_ptr<MaterialParser> parser = MaterialDefinition::createParser(engine.getBackend(), | ||
engine.getShaderLanguage(), data, size); | ||
assert_invariant(parser); | ||
|
||
return mDefinitions.acquire(Key{parser.get()}, [&engine, parser = std::move(parser)]() mutable { | ||
return MaterialDefinition::create(engine, std::move(parser)); | ||
}); | ||
} | ||
|
||
void MaterialCache::release(FEngine& engine, MaterialParser const& parser) noexcept { | ||
mDefinitions.release(Key{&parser}, [&engine](MaterialDefinition& definition) { | ||
definition.terminate(engine); | ||
}); | ||
} | ||
|
||
} // namespace filament |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright (C) 2025 The Android Open Source Project | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
#ifndef TNT_FILAMENT_MATERIALCACHE_H | ||
#define TNT_FILAMENT_MATERIALCACHE_H | ||
|
||
#include "MaterialDefinition.h" | ||
|
||
#include <private/filament/Variant.h> | ||
|
||
#include <backend/CallbackHandler.h> | ||
#include <backend/DriverEnums.h> | ||
#include <backend/Handle.h> | ||
#include <backend/Program.h> | ||
|
||
#include <utils/Invocable.h> | ||
#include <utils/RefCountedMap.h> | ||
|
||
namespace filament { | ||
|
||
class Material; | ||
|
||
class MaterialCache { | ||
// A newtype around a material parser used as a key for the material cache. The material file's | ||
// CRC32 is used as the hash function. | ||
struct Key { | ||
struct Hash { | ||
size_t operator()(Key const& x) const noexcept; | ||
}; | ||
bool operator==(Key const& rhs) const noexcept; | ||
|
||
MaterialParser const* UTILS_NONNULL parser; | ||
}; | ||
|
||
public: | ||
~MaterialCache(); | ||
|
||
/** Acquire or create a new entry in the cache for the given material data. */ | ||
MaterialDefinition* UTILS_NULLABLE acquire(FEngine& engine, const void* UTILS_NONNULL data, | ||
size_t size) noexcept; | ||
|
||
/** Release an entry in the cache, potentially freeing its GPU resources. */ | ||
void release(FEngine& engine, MaterialParser const& parser) noexcept; | ||
|
||
private: | ||
// We use unique_ptr here because we need these pointers to be stable. | ||
// TODO: investigate using a custom allocator here? | ||
utils::RefCountedMap<Key, std::unique_ptr<MaterialDefinition>, Key::Hash> mDefinitions; | ||
}; | ||
|
||
} // namespace filament | ||
|
||
#endif // TNT_FILAMENT_MATERIALCACHE_H |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.