| 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 <filaflat/ChunkContainer.h> |
| 18 |
|
| 19 |
#include <filament/MaterialEnums.h> |
| 20 |
|
| 21 |
#include <backend/DriverEnums.h> |
| 22 |
|
| 23 |
#include <matdbg/JsonWriter.h> |
| 24 |
#include <matdbg/ShaderInfo.h> |
| 25 |
|
| 26 |
#include "CommonWriter.h" |
| 27 |
|
| 28 |
#include <private/filament/Variant.h> |
| 29 |
|
| 30 |
#include <iomanip> |
| 31 |
#include <sstream> |
| 32 |
|
| 33 |
using namespace filament; |
| 34 |
using namespace backend; |
| 35 |
using namespace filaflat; |
| 36 |
using namespace filamat; |
| 37 |
using namespace std; |
| 38 |
using namespace utils; |
| 39 |
|
| 40 |
namespace filament { |
| 41 |
namespace matdbg { |
| 42 |
|
| 43 |
static std::string arraySizeToString(uint64_t size) { |
| 44 |
if (size > 1) { |
| 45 |
std::string s = "["; |
| 46 |
s += size; |
| 47 |
s += "]"; |
| 48 |
return s; |
| 49 |
} |
| 50 |
return ""; |
| 51 |
} |
| 52 |
|
| 53 |
template<typename T, typename V> |
| 54 |
static void printChunk(ostream& json, const ChunkContainer& container, ChunkType type, |
| 55 |
const char* title) { |
| 56 |
T value; |
| 57 |
if (read(container, type, reinterpret_cast<V*>(&value))) { |
| 58 |
json << "\"" << title << "\": \"" << toString(value) << "\",\n"; |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
static void printFloatChunk(ostream& json, const ChunkContainer& container, ChunkType type, |
| 63 |
const char* title) { |
| 64 |
float value; |
| 65 |
if (read(container, type, &value)) { |
| 66 |
json << "\"" << title << "\": " << setprecision(2) << value << ",\n"; |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
static void printUint32Chunk(ostream& json, const ChunkContainer& container, |
| 71 |
ChunkType type, const char* title) { |
| 72 |
uint32_t value; |
| 73 |
if (read(container, type, &value)) { |
| 74 |
json << "\"" << title << "\": " << value << ",\n"; |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
static void printStringChunk(ostream& json, const ChunkContainer& container, |
| 79 |
ChunkType type, const char* title) { |
| 80 |
CString value; |
| 81 |
if (read(container, type, &value)) { |
| 82 |
json << "\"" << title << "\": \"" << value.c_str_safe() << "\",\n"; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
static bool printMaterial(ostream& json, const ChunkContainer& container) { |
| 87 |
printStringChunk(json, container, MaterialName, "name"); |
| 88 |
printUint32Chunk(json, container, MaterialVersion, "version"); |
| 89 |
json << "\"shading\": {\n"; |
| 90 |
printChunk<Shading, uint8_t>(json, container, MaterialShading, "model"); |
| 91 |
printChunk<MaterialDomain, uint8_t>(json, container, ChunkType::MaterialDomain, "material_domain"); |
| 92 |
printChunk<VertexDomain, uint8_t>(json, container, MaterialVertexDomain, "vertex_domain"); |
| 93 |
printChunk<Interpolation, uint8_t>(json, container, MaterialInterpolation, "interpolation"); |
| 94 |
printChunk<bool, bool>(json, container, MaterialShadowMultiplier, "shadow_multiply"); |
| 95 |
printChunk<bool, bool>(json, container, MaterialSpecularAntiAliasing, "specular_antialiasing"); |
| 96 |
printFloatChunk(json, container, MaterialSpecularAntiAliasingVariance, "variance"); |
| 97 |
printFloatChunk(json, container, MaterialSpecularAntiAliasingThreshold, "threshold"); |
| 98 |
printChunk<bool, bool>(json, container, MaterialClearCoatIorChange, "clear_coat_IOR_change"); |
| 99 |
json << "\"_\": 0 },\n"; |
| 100 |
json << "\"raster\": {\n"; |
| 101 |
printChunk<BlendingMode, uint8_t>(json, container, MaterialBlendingMode, "blending"); |
| 102 |
printFloatChunk(json, container, MaterialMaskThreshold, "mask_threshold"); |
| 103 |
printChunk<bool, bool>(json, container, MaterialColorWrite, "color_write"); |
| 104 |
printChunk<bool, bool>(json, container, MaterialDepthWrite, "depth_write"); |
| 105 |
printChunk<bool, bool>(json, container, MaterialDepthTest, "depth_test"); |
| 106 |
printChunk<bool, bool>(json, container, MaterialDoubleSided, "double_sided"); |
| 107 |
printChunk<CullingMode, uint8_t>(json, container, MaterialCullingMode, "culling"); |
| 108 |
printChunk<TransparencyMode, uint8_t>(json, container, MaterialTransparencyMode, "transparency"); |
| 109 |
json << "\"_\": 0 },\n"; |
| 110 |
return true; |
| 111 |
} |
| 112 |
|
| 113 |
static bool printParametersInfo(ostream& json, const ChunkContainer& container) { |
| 114 |
// TODO |
| 115 |
return true; |
| 116 |
} |
| 117 |
|
| 118 |
static void printShaderInfo(ostream& json, const vector<ShaderInfo>& info, const ChunkContainer& container) { |
| 119 |
MaterialDomain domain = MaterialDomain::SURFACE; |
| 120 |
read(container, ChunkType::MaterialDomain, reinterpret_cast<uint8_t*>(&domain)); |
| 121 |
for (uint64_t i = 0; i < info.size(); ++i) { |
| 122 |
const auto& item = info[i]; |
| 123 |
string variantString = formatVariantString(item.variant, domain); |
| 124 |
string ps = (item.pipelineStage == backend::ShaderType::VERTEX) ? "vertex " : "fragment"; |
| 125 |
json |
| 126 |
<< " {" |
| 127 |
<< "\"index\": \"" << std::setw(2) << i << "\", " |
| 128 |
<< "\"shaderModel\": \"" << toString(item.shaderModel) << "\", " |
| 129 |
<< "\"pipelineStage\": \"" << ps << "\", " |
| 130 |
<< "\"variantString\": \"" << variantString << "\", " |
| 131 |
<< "\"variant\": \"" << std::hex << int(item.variant) << std::dec << "\" }" |
| 132 |
<< ((i == info.size() - 1) ? "\n" : ",\n"); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
static bool printGlslInfo(ostream& json, const ChunkContainer& container) { |
| 137 |
std::vector<ShaderInfo> info; |
| 138 |
info.resize(getShaderCount(container, ChunkType::MaterialGlsl)); |
| 139 |
if (!getGlShaderInfo(container, info.data())) { |
| 140 |
return false; |
| 141 |
} |
| 142 |
json << "\"opengl\": [\n"; |
| 143 |
printShaderInfo(json, info, container); |
| 144 |
json << "],\n"; |
| 145 |
return true; |
| 146 |
} |
| 147 |
|
| 148 |
static bool printVkInfo(ostream& json, const ChunkContainer& container) { |
| 149 |
std::vector<ShaderInfo> info; |
| 150 |
info.resize(getShaderCount(container, ChunkType::MaterialSpirv)); |
| 151 |
if (!getVkShaderInfo(container, info.data())) { |
| 152 |
return false; |
| 153 |
} |
| 154 |
json << "\"vulkan\": [\n"; |
| 155 |
printShaderInfo(json, info, container); |
| 156 |
json << "],\n"; |
| 157 |
return true; |
| 158 |
} |
| 159 |
|
| 160 |
static bool printMetalInfo(ostream& json, const ChunkContainer& container) { |
| 161 |
std::vector<ShaderInfo> info; |
| 162 |
info.resize(getShaderCount(container, ChunkType::MaterialMetal)); |
| 163 |
if (!getMetalShaderInfo(container, info.data())) { |
| 164 |
return false; |
| 165 |
} |
| 166 |
json << "\"metal\": [\n"; |
| 167 |
printShaderInfo(json, info, container); |
| 168 |
json << "],\n"; |
| 169 |
return true; |
| 170 |
} |
| 171 |
|
| 172 |
bool JsonWriter::writeMaterialInfo(const filaflat::ChunkContainer& container) { |
| 173 |
ostringstream json; |
| 174 |
if (!printMaterial(json, container)) { |
| 175 |
return false; |
| 176 |
} |
| 177 |
if (!printParametersInfo(json, container)) { |
| 178 |
return false; |
| 179 |
} |
| 180 |
if (!printGlslInfo(json, container)) { |
| 181 |
return false; |
| 182 |
} |
| 183 |
if (!printVkInfo(json, container)) { |
| 184 |
return false; |
| 185 |
} |
| 186 |
if (!printMetalInfo(json, container)) { |
| 187 |
return false; |
| 188 |
} |
| 189 |
|
| 190 |
json << "\"required_attributes\": [\n"; |
| 191 |
uint32_t requiredAttributes; |
| 192 |
if (read(container, MaterialRequiredAttributes, &requiredAttributes)) { |
| 193 |
string comma; |
| 194 |
AttributeBitset bitset; |
| 195 |
bitset.setValue(requiredAttributes); |
| 196 |
if (bitset.count() > 0) { |
| 197 |
for (size_t i = 0; i < bitset.size(); i++) { |
| 198 |
if (bitset.test(i)) { |
| 199 |
json << comma << "\"" << toString(static_cast<VertexAttribute>(i)) << "\""; |
| 200 |
comma = ","; |
| 201 |
} |
| 202 |
} |
| 203 |
} |
| 204 |
} |
| 205 |
json << "]\n"; |
| 206 |
|
| 207 |
mJsonString = CString(json.str().c_str()); |
| 208 |
return true; |
| 209 |
} |
| 210 |
|
| 211 |
const char* JsonWriter::getJsonString() const { |
| 212 |
return mJsonString.c_str(); |
| 213 |
} |
| 214 |
|
| 215 |
size_t JsonWriter::getJsonSize() const { |
| 216 |
return mJsonString.size(); |
| 217 |
} |
| 218 |
|
| 219 |
bool JsonWriter::writeActiveInfo(const filaflat::ChunkContainer& package, |
| 220 |
Backend backend, uint64_t activeVariants) { |
| 221 |
vector<ShaderInfo> shaders; |
| 222 |
ostringstream json; |
| 223 |
json << "[\""; |
| 224 |
switch (backend) { |
| 225 |
case Backend::OPENGL: |
| 226 |
shaders.resize(getShaderCount(package, ChunkType::MaterialGlsl)); |
| 227 |
getGlShaderInfo(package, shaders.data()); |
| 228 |
json << "opengl"; |
| 229 |
break; |
| 230 |
case Backend::VULKAN: |
| 231 |
shaders.resize(getShaderCount(package, ChunkType::MaterialSpirv)); |
| 232 |
getVkShaderInfo(package, shaders.data()); |
| 233 |
json << "vulkan"; |
| 234 |
break; |
| 235 |
case Backend::METAL: |
| 236 |
shaders.resize(getShaderCount(package, ChunkType::MaterialMetal)); |
| 237 |
getMetalShaderInfo(package, shaders.data()); |
| 238 |
json << "metal"; |
| 239 |
break; |
| 240 |
default: |
| 241 |
return false; |
| 242 |
} |
| 243 |
json << "\""; |
| 244 |
for (uint8_t variant = 0; variant < VARIANT_COUNT; variant++) { |
| 245 |
if (activeVariants & (1 << variant)) { |
| 246 |
int shaderIndex = 0; |
| 247 |
for (const auto& info : shaders) { |
| 248 |
if (info.variant == variant) { |
| 249 |
json << ", " << shaderIndex; |
| 250 |
} |
| 251 |
shaderIndex++; |
| 252 |
} |
| 253 |
} |
| 254 |
} |
| 255 |
json << "]"; |
| 256 |
mJsonString = CString(json.str().c_str()); |
| 257 |
return true; |
| 258 |
} |
| 259 |
|
| 260 |
} // namespace matdbg |
| 261 |
} // namespace filament |