From 97850c62c77430bed42a2cb32ee1ed6fc0bfd3e4 Mon Sep 17 00:00:00 2001 From: Romain Guy Date: Tue, 30 Apr 2019 16:39:22 -0700 Subject: [PATCH] Cleanup of filamat (#1157) * Cleanup of filamat * Add missing include --- libs/filamat/CMakeLists.txt | 1 - libs/filamat/include/filamat/Package.h | 6 +- libs/filamat/src/eiff/BlobDictionary.cpp | 18 --- libs/filamat/src/eiff/BlobDictionary.h | 19 +-- libs/filamat/src/eiff/Chunk.cpp | 7 +- libs/filamat/src/eiff/Chunk.h | 9 +- libs/filamat/src/eiff/DictionarySpirvChunk.h | 8 +- libs/filamat/src/eiff/DictionaryTextChunk.h | 6 +- .../src/eiff/MaterialInterfaceBlockChunk.h | 14 +- libs/filamat/src/eiff/MaterialSpirvChunk.h | 6 +- libs/filamat/src/eiff/MaterialTextChunk.cpp | 89 +++++++++++- libs/filamat/src/eiff/MaterialTextChunk.h | 25 +++- libs/filamat/src/eiff/SimpleFieldChunk.h | 8 +- libs/filamat/src/eiff/TextChunk.h | 135 ------------------ 14 files changed, 147 insertions(+), 204 deletions(-) delete mode 100644 libs/filamat/src/eiff/TextChunk.h diff --git a/libs/filamat/CMakeLists.txt b/libs/filamat/CMakeLists.txt index 9029817eb1..343b2346b0 100644 --- a/libs/filamat/CMakeLists.txt +++ b/libs/filamat/CMakeLists.txt @@ -20,7 +20,6 @@ set(PRIVATE_HDRS src/eiff/DictionaryTextChunk.h src/eiff/DictionarySpirvChunk.h src/eiff/Flattener.h - src/eiff/TextChunk.h src/eiff/LineDictionary.h src/eiff/MaterialTextChunk.h src/eiff/MaterialInterfaceBlockChunk.h diff --git a/libs/filamat/include/filamat/Package.h b/libs/filamat/include/filamat/Package.h index 95f76041ef..719717c9e4 100644 --- a/libs/filamat/include/filamat/Package.h +++ b/libs/filamat/include/filamat/Package.h @@ -30,13 +30,11 @@ namespace filamat { class UTILS_PUBLIC Package { public: - Package() = default; // Regular constructor explicit Package(size_t size) : mSize(size) { - mPayload = (uint8_t*) calloc(1, size); - assert(mPayload != nullptr); + mPayload = new uint8_t[size]; } Package(const void* src, size_t size) : Package(size) { @@ -63,7 +61,7 @@ public: Package(const Package& other) = delete; ~Package() { - free(mPayload); + delete[] mPayload; } uint8_t* getData() const noexcept { diff --git a/libs/filamat/src/eiff/BlobDictionary.cpp b/libs/filamat/src/eiff/BlobDictionary.cpp index daf85d6701..767b7c604b 100644 --- a/libs/filamat/src/eiff/BlobDictionary.cpp +++ b/libs/filamat/src/eiff/BlobDictionary.cpp @@ -19,24 +19,6 @@ #include namespace filamat { - BlobDictionary::BlobDictionary() : mStorageSize(0) { -} - -const std::string& BlobDictionary::getBlob(size_t index) const noexcept { - assert(index < mBlobs.size()); - return mBlobs[index]; -} - -size_t BlobDictionary::getBlobCount() const { - return mBlobs.size(); -} - -size_t BlobDictionary::getIndex(const std::string& s) const noexcept { - if (mBlobIndices.find(s) == mBlobIndices.end()) { - return SIZE_MAX; - } - return mBlobIndices.at(s); -} size_t BlobDictionary::addBlob(const std::vector& vblob) noexcept { std::string blob((char*) vblob.data(), vblob.size() * 4); diff --git a/libs/filamat/src/eiff/BlobDictionary.h b/libs/filamat/src/eiff/BlobDictionary.h index e975949739..53a4d1ee60 100644 --- a/libs/filamat/src/eiff/BlobDictionary.h +++ b/libs/filamat/src/eiff/BlobDictionary.h @@ -17,6 +17,7 @@ #ifndef TNT_FILAMAT_BLOBDICTIONARY_H #define TNT_FILAMAT_BLOBDICTIONARY_H +#include #include #include #include @@ -26,13 +27,17 @@ namespace filamat { // Establish a blob <-> id mapping. Note that std::string may binary data with null characters. class BlobDictionary { public: - BlobDictionary(); + BlobDictionary() : mStorageSize(0) { + } + ~BlobDictionary() = default; // Adds a blob if it's not already a duplicate and returns its index. size_t addBlob(const std::vector& blob) noexcept; - size_t getBlobCount() const; + size_t getBlobCount() const noexcept { + return mBlobs.size(); + } // Returns the total storage size, assuming that each blob is prefixed with a 64-bit size. constexpr size_t getSize() const noexcept { @@ -43,12 +48,10 @@ public: return mBlobs.size() == 0; } - const std::string& getBlob(size_t index) const noexcept; - - // TODO: Do we really need this method here? It was added for parity with LineDictionary, - // but these blobs are potentially quite big so we might want to avoid hashing them. - // At the very least we should do some performance analysis here. - size_t getIndex(const std::string& s) const noexcept; + const std::string& getBlob(size_t index) const noexcept { + assert(index < mBlobs.size()); + return mBlobs[index]; + } private: std::unordered_map mBlobIndices; diff --git a/libs/filamat/src/eiff/Chunk.cpp b/libs/filamat/src/eiff/Chunk.cpp index c51b6048ec..d99f1baac0 100644 --- a/libs/filamat/src/eiff/Chunk.cpp +++ b/libs/filamat/src/eiff/Chunk.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2017 The Android Open Source Project + * Copyright (C) 2018 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. @@ -18,9 +18,6 @@ namespace filamat { -Chunk::Chunk(ChunkType type) : mType(type), mFlattenedSize(0) { -} - Chunk::~Chunk() = default; -} // namespace filamat \ No newline at end of file +} // namespace filamat diff --git a/libs/filamat/src/eiff/Chunk.h b/libs/filamat/src/eiff/Chunk.h index a232578163..205137f47f 100644 --- a/libs/filamat/src/eiff/Chunk.h +++ b/libs/filamat/src/eiff/Chunk.h @@ -29,22 +29,23 @@ class Chunk{ public: virtual ~Chunk(); - ChunkType getType() { + ChunkType getType() const noexcept { return mType; } - size_t getFlattenedSize() { + size_t getFlattenedSize() const noexcept { return mFlattenedSize; } - void setFlattenedSize(size_t s) { + void setFlattenedSize(size_t s) noexcept { mFlattenedSize = s; } virtual void flatten(Flattener &f) = 0; protected: - Chunk(ChunkType type); + Chunk(ChunkType type) : mType(type), mFlattenedSize(0) { + } private: ChunkType mType; diff --git a/libs/filamat/src/eiff/DictionarySpirvChunk.h b/libs/filamat/src/eiff/DictionarySpirvChunk.h index 566c91dfa3..2828f1801b 100644 --- a/libs/filamat/src/eiff/DictionarySpirvChunk.h +++ b/libs/filamat/src/eiff/DictionarySpirvChunk.h @@ -26,12 +26,14 @@ namespace filamat { -class DictionarySpirvChunk : public Chunk { +class DictionarySpirvChunk final : public Chunk { public: - DictionarySpirvChunk(BlobDictionary& dictionary); + explicit DictionarySpirvChunk(BlobDictionary& dictionary); ~DictionarySpirvChunk() = default; - virtual void flatten(Flattener& f); + private: + void flatten(Flattener& f) override; + BlobDictionary& mDictionary; }; diff --git a/libs/filamat/src/eiff/DictionaryTextChunk.h b/libs/filamat/src/eiff/DictionaryTextChunk.h index e929edb6dd..ed4fb25b6d 100644 --- a/libs/filamat/src/eiff/DictionaryTextChunk.h +++ b/libs/filamat/src/eiff/DictionaryTextChunk.h @@ -26,12 +26,14 @@ namespace filamat { -class DictionaryTextChunk : public Chunk { +class DictionaryTextChunk final : public Chunk { public: DictionaryTextChunk(LineDictionary& dictionary, ChunkType chunkType); ~DictionaryTextChunk() = default; - virtual void flatten(Flattener& f); + private: + void flatten(Flattener& f) override; + LineDictionary& mDictionary; }; diff --git a/libs/filamat/src/eiff/MaterialInterfaceBlockChunk.h b/libs/filamat/src/eiff/MaterialInterfaceBlockChunk.h index 1e3c6303af..110174dba8 100644 --- a/libs/filamat/src/eiff/MaterialInterfaceBlockChunk.h +++ b/libs/filamat/src/eiff/MaterialInterfaceBlockChunk.h @@ -24,23 +24,25 @@ namespace filamat { -class MaterialUniformInterfaceBlockChunk : public Chunk { +class MaterialUniformInterfaceBlockChunk final : public Chunk { public: explicit MaterialUniformInterfaceBlockChunk(filament::UniformInterfaceBlock& uib); - ~MaterialUniformInterfaceBlockChunk() override = default; + ~MaterialUniformInterfaceBlockChunk() = default; - void flatten(Flattener &) override; private: + void flatten(Flattener &) override; + filament::UniformInterfaceBlock& mUib; }; -class MaterialSamplerInterfaceBlockChunk : public Chunk { +class MaterialSamplerInterfaceBlockChunk final : public Chunk { public: explicit MaterialSamplerInterfaceBlockChunk(filament::SamplerInterfaceBlock& sib); - ~MaterialSamplerInterfaceBlockChunk() override = default; + ~MaterialSamplerInterfaceBlockChunk() = default; - void flatten(Flattener &) override; private: + void flatten(Flattener &) override; + filament::SamplerInterfaceBlock& mSib; }; diff --git a/libs/filamat/src/eiff/MaterialSpirvChunk.h b/libs/filamat/src/eiff/MaterialSpirvChunk.h index c081efa0d6..c58c1d6752 100644 --- a/libs/filamat/src/eiff/MaterialSpirvChunk.h +++ b/libs/filamat/src/eiff/MaterialSpirvChunk.h @@ -26,10 +26,12 @@ namespace filamat { class MaterialSpirvChunk final : public Chunk { public: - MaterialSpirvChunk(const std::vector& entries); + explicit MaterialSpirvChunk(const std::vector& entries); ~MaterialSpirvChunk() = default; - void flatten(Flattener& f) override; + private: + void flatten(Flattener& f) override; + const std::vector& mEntries; }; diff --git a/libs/filamat/src/eiff/MaterialTextChunk.cpp b/libs/filamat/src/eiff/MaterialTextChunk.cpp index 505f2d28f4..fcf46d21e7 100644 --- a/libs/filamat/src/eiff/MaterialTextChunk.cpp +++ b/libs/filamat/src/eiff/MaterialTextChunk.cpp @@ -18,20 +18,95 @@ namespace filamat { -MaterialTextChunk::MaterialTextChunk(const std::vector &entries, - LineDictionary &dictionary, ChunkType chunkType) : - TextChunk(chunkType, entries, dictionary) { -} - -void MaterialTextChunk::writeEntryAttributes(size_t entryIndex, Flattener& f) { +void MaterialTextChunk::writeEntryAttributes(size_t entryIndex, Flattener& f) const noexcept { const TextEntry& entry = mEntries[entryIndex]; f.writeUint8(entry.shaderModel); f.writeUint8(entry.variant); f.writeUint8(entry.stage); } -const char* MaterialTextChunk::getShaderText(size_t entryIndex) const { +const char* MaterialTextChunk::getShaderText(size_t entryIndex) const noexcept { return mEntries[entryIndex].shader; } +void compressShader(const char *s, Flattener &f, const LineDictionary& dictionary) { + f.writeUint32(static_cast(strlen(s) + 1)); + f.writeValuePlaceholder(); + + size_t numLines = 0; + + size_t cur = 0; + + while (s[cur] != '\0') { + size_t pos = cur; + size_t len = 0; + + while (s[cur] != '\n') { + cur++; + len++; + } + + std::string newLine(s, pos, len); + + size_t index = dictionary.getIndex(newLine); + if (index > UINT16_MAX) { + slog.e << "Dictionary returned line index > UINT16_MAX" << io::endl; + continue; + } + + f.writeUint16(static_cast(index)); + numLines += 1; + + cur++; + } + f.writePlaceHoldValue(numLines); +} + +void MaterialTextChunk::flatten(Flattener& f) { + f.resetOffsets(); + + // Avoid detecting duplicate twice (once for dry run and once for actual flattening). + if (mDuplicateMap.empty()) { + mDuplicateMap.resize(mEntries.size()); + + // Detect duplicate; + std::unordered_map stringToIndex; + for (size_t i = 0; i < mEntries.size(); i++) { + if (stringToIndex.find(getShaderText(i)) == stringToIndex.end()) { // New + stringToIndex[getShaderText(i)] = i; + mDuplicateMap[i].isDup = false; + } else { // Dup + mDuplicateMap[i].isDup = true; + mDuplicateMap[i].dupOfIndex = stringToIndex[mEntries[i].shader]; + } + } + } + + // All offsets expressed later will start at the current flattener cursor position + f.markOffsetBase(); + + // Write how many shaders we have + f.writeUint64(mEntries.size()); + + // Write all indexes. + for (size_t i = 0; i < mEntries.size(); i++) { + writeEntryAttributes(i, f); + + // Try to reuse a shader if this is a dup. + if (mDuplicateMap[i].isDup) { + f.writeOffsetplaceholder(mDuplicateMap[i].dupOfIndex); + } else { + f.writeOffsetplaceholder(i); + } + } + + // Write all strings + for (size_t i = 0; i < mEntries.size(); i++) { + if (mDuplicateMap[i].isDup) + continue; + f.writeOffsets(i); + compressShader(getShaderText(i), f, mDictionary); + } +} + } // namespace filamat diff --git a/libs/filamat/src/eiff/MaterialTextChunk.h b/libs/filamat/src/eiff/MaterialTextChunk.h index c845368ab3..966327bfad 100644 --- a/libs/filamat/src/eiff/MaterialTextChunk.h +++ b/libs/filamat/src/eiff/MaterialTextChunk.h @@ -20,20 +20,33 @@ #include #include "Chunk.h" -#include "TextChunk.h" #include "LineDictionary.h" #include "ShaderEntry.h" namespace filamat { -class MaterialTextChunk final : public TextChunk { +class MaterialTextChunk final : public Chunk { public: MaterialTextChunk(const std::vector &entries, LineDictionary &dictionary, - ChunkType chunkType); + ChunkType type) : Chunk(type), mDictionary(dictionary), mEntries(entries) { + } ~MaterialTextChunk() = default; -protected: - virtual const char* getShaderText(size_t entryIndex) const override; - virtual void writeEntryAttributes(size_t entryIndex, Flattener& f) override; + +private: + void flatten(Flattener& f) override; + + const char* getShaderText(size_t entryIndex) const noexcept; + void writeEntryAttributes(size_t entryIndex, Flattener& f) const noexcept; + + // Structure to keep track of duplicates. + struct ShaderAttribute{ + bool isDup = false; + size_t dupOfIndex = 0; + }; + std::vector mDuplicateMap; + + const std::vector& mEntries; + const LineDictionary& mDictionary; }; } // namespace filamat diff --git a/libs/filamat/src/eiff/SimpleFieldChunk.h b/libs/filamat/src/eiff/SimpleFieldChunk.h index 4c0daf10f6..458879d254 100644 --- a/libs/filamat/src/eiff/SimpleFieldChunk.h +++ b/libs/filamat/src/eiff/SimpleFieldChunk.h @@ -23,12 +23,14 @@ namespace filamat { template -class SimpleFieldChunk : public Chunk { +class SimpleFieldChunk final : public Chunk { public: SimpleFieldChunk(ChunkType type, T value) : Chunk(type), t(value) {} - virtual ~SimpleFieldChunk() = default; - virtual void flatten(Flattener &f) override ; + ~SimpleFieldChunk() = default; + private: + void flatten(Flattener &f) override; + T t; }; diff --git a/libs/filamat/src/eiff/TextChunk.h b/libs/filamat/src/eiff/TextChunk.h deleted file mode 100644 index 482b84c791..0000000000 --- a/libs/filamat/src/eiff/TextChunk.h +++ /dev/null @@ -1,135 +0,0 @@ -/* - * 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. - */ - -#ifndef TNT_FILAMAT_TEXT_CHUNK_H -#define TNT_FILAMAT_TEXT_CHUNK_H - -#include - -#include "Chunk.h" -#include "Flattener.h" -#include "LineDictionary.h" - -#include - -using namespace utils; - -namespace filamat { - -template -class TextChunk : public Chunk { -public: - virtual ~TextChunk() = default; -protected: - TextChunk(ChunkType type, const std::vector& entries, const LineDictionary& dictionary) : - Chunk(type), mDictionary(dictionary), mEntries(entries) { - - } - virtual void writeEntryAttributes(size_t entryIndex, Flattener& f) = 0; - virtual const char* getShaderText(size_t entryIndex) const = 0; - - void compressShader(const char *s, Flattener &f, const LineDictionary& dictionary) const { - f.writeUint32(static_cast(strlen(s) + 1)); - f.writeValuePlaceholder(); - - size_t numLines = 0; - - size_t cur = 0; - size_t pos = 0; - size_t len = 0; - - while (s[cur] != '\0') { - pos = cur; - len = 0; - while (s[cur] != '\n') { - cur++; - len++; - } - std::string newLine(s, pos, len); - size_t index = dictionary.getIndex(newLine); - if (index > UINT16_MAX) { - slog.e << "Dictionary returned line index beyond what is representable on 16 bits (" - << UINT16_MAX << ")." << io::endl; - exit(0); - } - f.writeUint16(static_cast(index)); - numLines += 1; - cur++; - } - f.writePlaceHoldValue(numLines); - }; - - virtual void flatten(Flattener& f) override { - f.resetOffsets(); - - // Avoid detecting duplicate twice (once for dry run and once for actual flattening). - if (mDuplicateMap.empty()) { - mDuplicateMap.resize(mEntries.size()); - - // Detect duplicate; - std::unordered_map stringToIndex; - for (size_t i = 0; i < mEntries.size(); i++) { - if (stringToIndex.find(getShaderText(i)) == stringToIndex.end()) { // New - stringToIndex[getShaderText(i)] = i; - mDuplicateMap[i].isDup = false; - } else { // Dup - mDuplicateMap[i].isDup = true; - mDuplicateMap[i].dupOfIndex = stringToIndex[mEntries[i].shader]; - } - } - } - - // All offsets expressed later will start at the current flattener cursor position - f.markOffsetBase(); - - // Write how many shaders we have - f.writeUint64(mEntries.size()); - - // Write all indexes. - for (size_t i = 0; i < mEntries.size(); i++) { - writeEntryAttributes(i, f); - - // Try to reuse a shader if this is a dup. - if (mDuplicateMap[i].isDup) { - f.writeOffsetplaceholder(mDuplicateMap[i].dupOfIndex); - } else { - f.writeOffsetplaceholder(i); - } - } - - // Write all strings - for (size_t i = 0; i < mEntries.size(); i++) { - if (mDuplicateMap[i].isDup) - continue; - f.writeOffsets(i); - compressShader(getShaderText(i), f, mDictionary); - } - } - - const std::vector& mEntries; - const LineDictionary& mDictionary; - - // Structure to keep track of duplicates. - struct ShaderAttribute{ - bool isDup = false; - size_t dupOfIndex = 0; - }; - std::vector mDuplicateMap; -}; - -} // namespace filamat - -#endif // TNT_FILAMAT_TEXT_CHUNK_H