diff --git a/libs/filamat/CMakeLists.txt b/libs/filamat/CMakeLists.txt index 2b59013f10..8a863627b7 100644 --- a/libs/filamat/CMakeLists.txt +++ b/libs/filamat/CMakeLists.txt @@ -41,6 +41,7 @@ set(COMMON_SRCS src/shaders/ShaderGenerator.cpp src/Enums.cpp src/MaterialBuilder.cpp + src/MaterialVariants.cpp src/PostprocessMaterialBuilder.cpp) # Sources and headers for filamat diff --git a/libs/filamat/src/MaterialBuilder.cpp b/libs/filamat/src/MaterialBuilder.cpp index 1b7e5a9915..7e3ab97dda 100644 --- a/libs/filamat/src/MaterialBuilder.cpp +++ b/libs/filamat/src/MaterialBuilder.cpp @@ -581,8 +581,10 @@ bool MaterialBuilder::generateShaders(const std::vector& variants, Chun // Emit GLSL chunks (TextDictionaryReader and MaterialTextChunk). if (!glslEntries.empty()) { - container.addChild(glslDictionary, ChunkType::DictionaryGlsl); - container.addChild(std::move(glslEntries), glslDictionary, ChunkType::MaterialGlsl); + const auto& dictionaryChunk = container.addChild( + std::move(glslDictionary), ChunkType::DictionaryGlsl); + container.addChild(std::move(glslEntries), + dictionaryChunk.getDictionary(), ChunkType::MaterialGlsl); } // Emit SPIRV chunks (SpirvDictionaryReader and MaterialSpirvChunk). @@ -594,8 +596,10 @@ bool MaterialBuilder::generateShaders(const std::vector& variants, Chun // Emit Metal chunks (MetalDictionaryReader and MaterialMetalChunk). if (!metalEntries.empty()) { - container.addChild(metalDictionary, ChunkType::DictionaryMetal); - container.addChild(std::move(metalEntries), metalDictionary, ChunkType::MaterialMetal); + const auto& dictionaryChunk = container.addChild( + std::move(metalDictionary), ChunkType::DictionaryMetal); + container.addChild(std::move(metalEntries), + dictionaryChunk.getDictionary(), ChunkType::MaterialMetal); } #endif @@ -636,8 +640,7 @@ Package MaterialBuilder::build() noexcept { bool success = generateShaders(variants, container, info); // Flatten all chunks in the container into a Package. - size_t packageSize = container.getSize(); - Package package(packageSize); + Package package(container.getSize()); Flattener f(package); container.flatten(f); package.setValid(success); diff --git a/libs/filamat/src/MaterialVariants.cpp b/libs/filamat/src/MaterialVariants.cpp new file mode 100644 index 0000000000..9e380ee6b4 --- /dev/null +++ b/libs/filamat/src/MaterialVariants.cpp @@ -0,0 +1,45 @@ +/* + * Copyright (C) 2017 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 "MaterialVariants.h" + +namespace filamat { + +std::vector determineVariants(uint8_t variantFilter, bool isLit, + bool shadowMultiplier) { + std::vector variants; + uint8_t variantMask = ~variantFilter; + for (uint8_t k = 0; k < filament::VARIANT_COUNT; k++) { + if (filament::Variant::isReserved(k)) { + continue; + } + + // Remove variants for unlit materials + uint8_t v = filament::Variant::filterVariant( + k & variantMask, isLit || shadowMultiplier); + + if (filament::Variant::filterVariantVertex(v) == k) { + variants.emplace_back(k, filament::backend::ShaderType::VERTEX); + } + + if (filament::Variant::filterVariantFragment(v) == k) { + variants.emplace_back(k, filament::backend::ShaderType::FRAGMENT); + } + } + return variants; +} + +} // namespace filamat diff --git a/libs/filamat/src/MaterialVariants.h b/libs/filamat/src/MaterialVariants.h index 08fc154947..e6da7aa315 100644 --- a/libs/filamat/src/MaterialVariants.h +++ b/libs/filamat/src/MaterialVariants.h @@ -19,6 +19,8 @@ #include +#include + #include namespace filamat { @@ -32,29 +34,7 @@ struct Variant { Stage stage; }; -std::vector determineVariants(uint8_t variantFilter, bool isLit, - bool shadowMultiplier) { - std::vector variants; - uint8_t variantMask = ~variantFilter; - for (uint8_t k = 0; k < filament::VARIANT_COUNT; k++) { - if (filament::Variant::isReserved(k)) { - continue; - } - - // Remove variants for unlit materials - uint8_t v = filament::Variant::filterVariant( - k & variantMask, isLit || shadowMultiplier); - - if (filament::Variant::filterVariantVertex(v) == k) { - variants.emplace_back(k, filament::backend::ShaderType::VERTEX); - } - - if (filament::Variant::filterVariantFragment(v) == k) { - variants.emplace_back(k, filament::backend::ShaderType::FRAGMENT); - } - } - return variants; -} +std::vector determineVariants(uint8_t variantFilter, bool isLit, bool shadowMultiplier); } // namespace filamat diff --git a/libs/filamat/src/PostprocessMaterialBuilder.cpp b/libs/filamat/src/PostprocessMaterialBuilder.cpp index 1f350eedf9..67a87afccc 100644 --- a/libs/filamat/src/PostprocessMaterialBuilder.cpp +++ b/libs/filamat/src/PostprocessMaterialBuilder.cpp @@ -196,8 +196,10 @@ Package PostprocessMaterialBuilder::build() { // Emit GLSL chunks if (!glslEntries.empty()) { - container.addChild(std::move(glslDictionary), ChunkType::DictionaryGlsl); - container.addChild(std::move(glslEntries), std::move(glslDictionary), ChunkType::MaterialGlsl); + const auto& dictionaryChunk = container.addChild( + std::move(glslDictionary), ChunkType::DictionaryGlsl); + container.addChild(std::move(glslEntries), + dictionaryChunk.getDictionary(), ChunkType::MaterialGlsl); } #ifndef FILAMAT_LITE @@ -209,14 +211,15 @@ Package PostprocessMaterialBuilder::build() { // Emit Metal chunks if (!metalEntries.empty()) { - container.addChild(std::move(metalDictionary), ChunkType::DictionaryMetal); - container.addChild(std::move(metalEntries), std::move(metalDictionary), ChunkType::MaterialMetal); + const auto& dictionaryChunk = container.addChild( + std::move(metalDictionary), ChunkType::DictionaryMetal); + container.addChild(std::move(metalEntries), + dictionaryChunk.getDictionary(), ChunkType::MaterialMetal); } #endif // Flatten all chunks in the container into a Package. - size_t packageSize = container.getSize(); - Package package(packageSize); + Package package(container.getSize()); Flattener f(package); container.flatten(f); package.setValid(!errorOccured); diff --git a/libs/filamat/src/eiff/ChunkContainer.h b/libs/filamat/src/eiff/ChunkContainer.h index 90eb04dc99..dae814320b 100644 --- a/libs/filamat/src/eiff/ChunkContainer.h +++ b/libs/filamat/src/eiff/ChunkContainer.h @@ -37,14 +37,16 @@ public: template ::value, int> = 0, typename... Args> - void addChild(Args&&... args) { - mChildren.emplace_back(new T(std::forward(args)...)); + const T& addChild(Args&&... args) { + T* chunk = new T(std::forward(args)...); + mChildren.emplace_back(chunk); + return *chunk; } // Helper method to add a SimpleFieldChunk to this ChunkContainer. template - void addSimpleChild(Args&&... args) { - addChild>(std::forward(args)...); + const SimpleFieldChunk& addSimpleChild(Args&&... args) { + return addChild>(std::forward(args)...); } size_t getSize() const; diff --git a/libs/filamat/src/eiff/DictionaryTextChunk.cpp b/libs/filamat/src/eiff/DictionaryTextChunk.cpp index 27d69461aa..9365e065a5 100644 --- a/libs/filamat/src/eiff/DictionaryTextChunk.cpp +++ b/libs/filamat/src/eiff/DictionaryTextChunk.cpp @@ -18,7 +18,7 @@ namespace filamat { -DictionaryTextChunk::DictionaryTextChunk(const LineDictionary& dictionary, ChunkType chunkType) : +DictionaryTextChunk::DictionaryTextChunk(LineDictionary&& dictionary, ChunkType chunkType) : Chunk(chunkType), mDictionary(dictionary) { } diff --git a/libs/filamat/src/eiff/DictionaryTextChunk.h b/libs/filamat/src/eiff/DictionaryTextChunk.h index f2c199619e..d22873d32e 100644 --- a/libs/filamat/src/eiff/DictionaryTextChunk.h +++ b/libs/filamat/src/eiff/DictionaryTextChunk.h @@ -28,13 +28,15 @@ namespace filamat { class DictionaryTextChunk final : public Chunk { public: - DictionaryTextChunk(const LineDictionary& dictionary, ChunkType chunkType); + DictionaryTextChunk(LineDictionary&& dictionary, ChunkType chunkType); ~DictionaryTextChunk() = default; + const LineDictionary& getDictionary() const noexcept { return mDictionary; } + private: void flatten(Flattener& f) override; - LineDictionary mDictionary; + const LineDictionary mDictionary; }; } // namespace filamat diff --git a/libs/filamat/src/eiff/LineDictionary.h b/libs/filamat/src/eiff/LineDictionary.h index 74fb0f032f..96e15a2286 100644 --- a/libs/filamat/src/eiff/LineDictionary.h +++ b/libs/filamat/src/eiff/LineDictionary.h @@ -49,7 +49,7 @@ private: std::unordered_map mLineIndices; std::vector mStrings; - size_t mStorageSize; + size_t mStorageSize = 0; }; } // namespace filamat diff --git a/libs/filamat/src/eiff/MaterialTextChunk.h b/libs/filamat/src/eiff/MaterialTextChunk.h index f7c1900783..145836407d 100644 --- a/libs/filamat/src/eiff/MaterialTextChunk.h +++ b/libs/filamat/src/eiff/MaterialTextChunk.h @@ -46,7 +46,7 @@ private: std::vector mDuplicateMap; const std::vector mEntries; - const LineDictionary mDictionary; + const LineDictionary& mDictionary; }; } // namespace filamat