| 1 |
/* |
| 2 |
* Copyright (C) 2019 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 <matdbg/ShaderExtractor.h> |
| 18 |
|
| 19 |
#include <filaflat/BlobDictionary.h> |
| 20 |
#include <filaflat/ChunkContainer.h> |
| 21 |
#include <filaflat/DictionaryReader.h> |
| 22 |
#include <filaflat/MaterialChunk.h> |
| 23 |
#include <filaflat/ShaderBuilder.h> |
| 24 |
#include <filaflat/Unflattener.h> |
| 25 |
|
| 26 |
#include <filament/MaterialChunkType.h> |
| 27 |
#include <filament/MaterialEnums.h> |
| 28 |
|
| 29 |
#include <backend/DriverEnums.h> |
| 30 |
|
| 31 |
#include <spirv_glsl.hpp> |
| 32 |
#include <spirv-tools/libspirv.h> |
| 33 |
|
| 34 |
using namespace filament; |
| 35 |
using namespace backend; |
| 36 |
using namespace filaflat; |
| 37 |
using namespace filamat; |
| 38 |
using namespace std; |
| 39 |
using namespace utils; |
| 40 |
|
| 41 |
namespace filament { |
| 42 |
namespace matdbg { |
| 43 |
|
| 44 |
ShaderExtractor::ShaderExtractor(Backend backend, const void* data, size_t size) |
| 45 |
: mChunkContainer(data, size), mMaterialChunk(mChunkContainer) { |
| 46 |
switch (backend) { |
| 47 |
case Backend::OPENGL: |
| 48 |
mMaterialTag = ChunkType::MaterialGlsl; |
| 49 |
mDictionaryTag = ChunkType::DictionaryText; |
| 50 |
break; |
| 51 |
case Backend::METAL: |
| 52 |
mMaterialTag = ChunkType::MaterialMetal; |
| 53 |
mDictionaryTag = ChunkType::DictionaryText; |
| 54 |
break; |
| 55 |
case Backend::VULKAN: |
| 56 |
mMaterialTag = ChunkType::MaterialSpirv; |
| 57 |
mDictionaryTag = ChunkType::DictionarySpirv; |
| 58 |
break; |
| 59 |
default: |
| 60 |
break; |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
bool ShaderExtractor::parse() noexcept { |
| 65 |
if (mChunkContainer.parse()) { |
| 66 |
return mMaterialChunk.readIndex(mMaterialTag); |
| 67 |
} |
| 68 |
return false; |
| 69 |
} |
| 70 |
|
| 71 |
bool ShaderExtractor::getDictionary(BlobDictionary& dictionary) noexcept { |
| 72 |
return DictionaryReader::unflatten(mChunkContainer, mDictionaryTag, dictionary); |
| 73 |
} |
| 74 |
|
| 75 |
bool ShaderExtractor::getShader(ShaderModel shaderModel, |
| 76 |
uint8_t variant, ShaderType stage, ShaderBuilder& shader) noexcept { |
| 77 |
|
| 78 |
ChunkContainer const& cc = mChunkContainer; |
| 79 |
if (!cc.hasChunk(mMaterialTag) || !cc.hasChunk(mDictionaryTag)) { |
| 80 |
return false; |
| 81 |
} |
| 82 |
|
| 83 |
BlobDictionary blobDictionary; |
| 84 |
if (!DictionaryReader::unflatten(cc, mDictionaryTag, blobDictionary)) { |
| 85 |
return false; |
| 86 |
} |
| 87 |
|
| 88 |
return mMaterialChunk.getShader(shader, blobDictionary, (uint8_t)shaderModel, variant, stage); |
| 89 |
} |
| 90 |
|
| 91 |
CString ShaderExtractor::spirvToGLSL(const uint32_t* data, size_t wordCount) { |
| 92 |
using namespace spirv_cross; |
| 93 |
|
| 94 |
CompilerGLSL::Options emitOptions; |
| 95 |
emitOptions.es = true; |
| 96 |
emitOptions.vulkan_semantics = true; |
| 97 |
|
| 98 |
vector<uint32_t> spirv(data, data + wordCount); |
| 99 |
CompilerGLSL glslCompiler(move(spirv)); |
| 100 |
glslCompiler.set_common_options(emitOptions); |
| 101 |
|
| 102 |
return CString(glslCompiler.compile().c_str()); |
| 103 |
} |
| 104 |
|
| 105 |
// If desired feel free to locally replace this with the glslang disassembler (spv::Disassemble) |
| 106 |
// but please do not submit. We prefer to use the syntax that the standalone "spirv-dis" tool |
| 107 |
// uses, which lets us easily generate test cases for the spirv-cross project. |
| 108 |
CString ShaderExtractor::spirvToText(const uint32_t* begin, size_t wordCount) { |
| 109 |
spv_context context = spvContextCreate(SPV_ENV_UNIVERSAL_1_0); |
| 110 |
if (SPV_SUCCESS != spvValidateBinary(context, begin, wordCount, nullptr)) { |
| 111 |
spvContextDestroy(context); |
| 112 |
return CString("Validation failure."); |
| 113 |
} |
| 114 |
|
| 115 |
spv_text text = nullptr; |
| 116 |
const uint32_t options = SPV_BINARY_TO_TEXT_OPTION_INDENT | |
| 117 |
SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES; |
| 118 |
|
| 119 |
spvBinaryToText(context, begin, wordCount, options, &text, nullptr); |
| 120 |
CString result(text->str); |
| 121 |
spvTextDestroy(text); |
| 122 |
spvContextDestroy(context); |
| 123 |
return result; |
| 124 |
} |
| 125 |
|
| 126 |
} // namespace matdbg |
| 127 |
} // namespace filament |