Cleanup of filamat (#1157)

* Cleanup of filamat

* Add missing include
This commit is contained in:
Romain Guy
2019-04-30 16:39:22 -07:00
committed by GitHub
parent 5b4ec6fd7d
commit 97850c62c7
14 changed files with 147 additions and 204 deletions
-1
View File
@@ -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
+2 -4
View File
@@ -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 {
-18
View File
@@ -19,24 +19,6 @@
#include <assert.h>
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<uint32_t>& vblob) noexcept {
std::string blob((char*) vblob.data(), vblob.size() * 4);
+11 -8
View File
@@ -17,6 +17,7 @@
#ifndef TNT_FILAMAT_BLOBDICTIONARY_H
#define TNT_FILAMAT_BLOBDICTIONARY_H
#include <cassert>
#include <string>
#include <unordered_map>
#include <vector>
@@ -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<uint32_t>& 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<std::string, size_t> mBlobIndices;
+2 -5
View File
@@ -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
} // namespace filamat
+5 -4
View File
@@ -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;
+5 -3
View File
@@ -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;
};
+4 -2
View File
@@ -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;
};
@@ -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;
};
+4 -2
View File
@@ -26,10 +26,12 @@ namespace filamat {
class MaterialSpirvChunk final : public Chunk {
public:
MaterialSpirvChunk(const std::vector<SpirvEntry>& entries);
explicit MaterialSpirvChunk(const std::vector<SpirvEntry>& entries);
~MaterialSpirvChunk() = default;
void flatten(Flattener& f) override;
private:
void flatten(Flattener& f) override;
const std::vector<SpirvEntry>& mEntries;
};
+82 -7
View File
@@ -18,20 +18,95 @@
namespace filamat {
MaterialTextChunk::MaterialTextChunk(const std::vector<TextEntry> &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<uint32_t>(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<uint16_t>(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<std::string, size_t> 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
+19 -6
View File
@@ -20,20 +20,33 @@
#include <vector>
#include "Chunk.h"
#include "TextChunk.h"
#include "LineDictionary.h"
#include "ShaderEntry.h"
namespace filamat {
class MaterialTextChunk final : public TextChunk<TextEntry> {
class MaterialTextChunk final : public Chunk {
public:
MaterialTextChunk(const std::vector<TextEntry> &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<ShaderAttribute> mDuplicateMap;
const std::vector<TextEntry>& mEntries;
const LineDictionary& mDictionary;
};
} // namespace filamat
+5 -3
View File
@@ -23,12 +23,14 @@
namespace filamat {
template <class T>
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;
};
-135
View File
@@ -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 <vector>
#include "Chunk.h"
#include "Flattener.h"
#include "LineDictionary.h"
#include <utils/Panic.h>
using namespace utils;
namespace filamat {
template <class T>
class TextChunk : public Chunk {
public:
virtual ~TextChunk() = default;
protected:
TextChunk(ChunkType type, const std::vector<T>& 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<uint32_t>(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<uint16_t>(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<std::string, size_t> 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<T>& mEntries;
const LineDictionary& mDictionary;
// Structure to keep track of duplicates.
struct ShaderAttribute{
bool isDup = false;
size_t dupOfIndex = 0;
};
std::vector<ShaderAttribute> mDuplicateMap;
};
} // namespace filamat
#endif // TNT_FILAMAT_TEXT_CHUNK_H