Debian Codegraph @

libs/matdbg/src/ShaderReplacer.cpp

raw
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/ShaderReplacer.h>
18
19 #include <backend/DriverEnums.h>
20
21 #include <utils/Log.h>
22
23 #include <tsl/robin_map.h>
24
25 #include <sstream>
26
27 #include <string.h>
28
29 namespace filament {
30 namespace matdbg {
31
32 using namespace backend;
33 using namespace filaflat;
34 using namespace std;
35 using namespace tsl;
36 using namespace utils;
37
38 using filamat::ChunkType;
39
40 // Utility class for managing a pair of lists: a list of shader records, and a list of string lines
41 // that can be encoded into an index list. Also knows how to read & write the relevant IFF chunks.
42 class ShaderIndex {
43 public:
44 // Consumes a chunk and builds the string list.
45 void addStringLines(const uint8_t* chunkContent, size_t size);
46
47 // Consumes a chunk and builds the shader records.
48 void addShaderRecords(const uint8_t* chunkContent, size_t size);
49
50 // Produces a chunk holding the string list.
51 void writeLinesChunk(ChunkType tag, ostream& stream) const;
52
53 // Produces a chunk holding the shader records.
54 void writeShadersChunk(ChunkType tag, ostream& stream) const;
55
56 // Replaces the specified shader text with new content.
57 void replaceShader(backend::ShaderModel shaderModel, uint8_t variant,
58 backend::ShaderType stage, const char* source, size_t sourceLength);
59
60 bool isEmpty() const { return mStringLines.size() == 0 && mShaderRecords.size() == 0; }
61
62 private:
63 struct ShaderRecord {
64 uint8_t model;
65 uint8_t variant;
66 uint8_t stage;
67 uint32_t offset;
68 vector<uint16_t> lineIndices;
69 string decodedShaderText;
70 uint32_t stringLength;
71 };
72
73 void decodeShadersFromIndices();
74 void encodeShadersToIndices();
75
76 vector<ShaderRecord> mShaderRecords;
77 vector<string> mStringLines;
78 };
79
80 ShaderReplacer::ShaderReplacer(Backend backend, const void* data, size_t size) :
81 mBackend(backend), mOriginalPackage(data, size) {
82 switch (backend) {
83 case Backend::OPENGL:
84 mMaterialTag = ChunkType::MaterialGlsl;
85 mDictionaryTag = ChunkType::DictionaryText;
86 break;
87 case Backend::METAL:
88 mMaterialTag = ChunkType::MaterialMetal;
89 mDictionaryTag = ChunkType::DictionaryText;
90 break;
91 case Backend::VULKAN:
92 mMaterialTag = ChunkType::MaterialSpirv;
93 mDictionaryTag = ChunkType::DictionarySpirv;
94 break;
95 default:
96 break;
97 }
98 }
99
100 ShaderReplacer::~ShaderReplacer() {
101 delete mEditedPackage;
102 }
103
104 bool ShaderReplacer::replaceShaderSource(backend::ShaderModel shaderModel, uint8_t variant,
105 backend::ShaderType stage, const char* source, size_t sourceLength) {
106 if (!mOriginalPackage.parse()) {
107 return false;
108 }
109
110 ChunkContainer const& cc = mOriginalPackage;
111 if (!cc.hasChunk(mMaterialTag) || !cc.hasChunk(mDictionaryTag)) {
112 return false;
113 }
114
115 if (mDictionaryTag == ChunkType::DictionarySpirv) {
116 slog.e << "SPIR-V editing is not yet supported." << io::endl;
117 return false;
118 }
119
120 // Clone all chunks except Dictionary* and Material*.
121 stringstream sstream(string((const char*) cc.getData(), cc.getSize()));
122 stringstream tstream;
123 ShaderIndex shaderIndex;
124 {
125 uint64_t type;
126 uint32_t size;
127 vector<uint8_t> content;
128 while (sstream) {
129 sstream.read((char*) &type, sizeof(type));
130 sstream.read((char*) &size, sizeof(size));
131 content.resize(size);
132 sstream.read((char*) content.data(), size);
133 if (ChunkType(type) == mDictionaryTag) {
134 shaderIndex.addStringLines(content.data(), size);
135 continue;
136 }
137 if (ChunkType(type) == mMaterialTag) {
138 shaderIndex.addShaderRecords(content.data(), size);
139 continue;
140 }
141 tstream.write((char*) &type, sizeof(type));
142 tstream.write((char*) &size, sizeof(size));
143 tstream.write((char*) content.data(), size);
144 }
145 }
146
147 // Append the new chunks for Dictionary* and Material*.
148 if (!shaderIndex.isEmpty()) {
149 shaderIndex.replaceShader(shaderModel, variant, stage, source, sourceLength);
150 shaderIndex.writeLinesChunk(mDictionaryTag, tstream);
151 shaderIndex.writeShadersChunk(mMaterialTag, tstream);
152 }
153
154 // Copy the new package from the stringstream into a ChunkContainer.
155 const size_t size = tstream.str().size();
156 uint8_t* data = new uint8_t[size];
157 memcpy(data, tstream.str().data(), size);
158 mEditedPackage = new filaflat::ChunkContainer(data, size);
159 return true;
160 }
161
162 const uint8_t* ShaderReplacer::getEditedPackage() const {
163 return (const uint8_t*) mEditedPackage->getData();
164 }
165
166 size_t ShaderReplacer::getEditedSize() const {
167 return mEditedPackage->getSize();
168 }
169
170 void ShaderIndex::addStringLines(const uint8_t* chunkContent, size_t size) {
171 uint32_t count = *((const uint32_t*) chunkContent);
172 mStringLines.resize(count);
173 const uint8_t* ptr = chunkContent + 4;
174 for (uint32_t i = 0; i < count; i++) {
175 mStringLines[i] = string((const char*) ptr);
176 ptr += mStringLines[i].length() + 1;
177 }
178 }
179
180 void ShaderIndex::addShaderRecords(const uint8_t* chunkContent, size_t size) {
181 stringstream stream(string((const char*) chunkContent, size));
182 uint64_t recordCount;
183 stream.read((char*) &recordCount, sizeof(recordCount));
184 mShaderRecords.resize(recordCount);
185 for (auto& record : mShaderRecords) {
186 stream.read((char*) &record.model, sizeof(ShaderRecord::model));
187 stream.read((char*) &record.variant, sizeof(ShaderRecord::variant));
188 stream.read((char*) &record.stage, sizeof(ShaderRecord::stage));
189 stream.read((char*) &record.offset, sizeof(ShaderRecord::offset));
190
191 const uint8_t* linePtr = chunkContent + record.offset;
192 record.stringLength = *((uint32_t*) linePtr);
193 linePtr += sizeof(uint32_t);
194
195 const uint32_t lineCount = *((uint32_t*) linePtr);
196 record.lineIndices.resize(lineCount);
197 linePtr += sizeof(uint32_t);
198
199 memcpy(record.lineIndices.data(), linePtr, lineCount * sizeof(uint16_t));
200 }
201 }
202
203 void ShaderIndex::writeLinesChunk(ChunkType tag, ostream& stream) const {
204 // First perform a prepass to compute chunk size.
205 uint32_t size = sizeof(uint32_t);
206 for (const auto& stringLine : mStringLines) {
207 size += stringLine.length() + 1;
208 }
209
210 // Serialize the chunk.
211 uint64_t type = tag;
212 stream.write((char*) &type, sizeof(type));
213 stream.write((char*) &size, sizeof(size));
214 uint32_t count = mStringLines.size();
215 stream.write((char*) &count, sizeof(count));
216 for (const auto& stringLine : mStringLines) {
217 stream.write(stringLine.c_str(), stringLine.length() + 1);
218 }
219 }
220
221 void ShaderIndex::writeShadersChunk(ChunkType tag, ostream& stream) const {
222 // First perform a prepass to compute chunk size.
223 uint32_t size = sizeof(uint64_t);
224 for (const auto& record : mShaderRecords) {
225 size += sizeof(ShaderRecord::model);
226 size += sizeof(ShaderRecord::variant);
227 size += sizeof(ShaderRecord::stage);
228 size += sizeof(ShaderRecord::offset);
229 }
230 for (const auto& record : mShaderRecords) {
231 size += sizeof(ShaderRecord::stringLength);
232 size += sizeof(uint32_t);
233 size += record.lineIndices.size() * sizeof(uint16_t);
234 }
235
236 // Serialize the chunk.
237 uint64_t type = tag;
238 stream.write((char*) &type, sizeof(type));
239 stream.write((char*) &size, sizeof(size));
240 uint64_t recordCount = mShaderRecords.size();
241 stream.write((char*) &recordCount, sizeof(recordCount));
242 for (const auto& record : mShaderRecords) {
243 stream.write((char*) &record.model, sizeof(ShaderRecord::model));
244 stream.write((char*) &record.variant, sizeof(ShaderRecord::variant));
245 stream.write((char*) &record.stage, sizeof(ShaderRecord::stage));
246 stream.write((char*) &record.offset, sizeof(ShaderRecord::offset));
247 }
248 for (const auto& record : mShaderRecords) {
249 uint32_t lineCount = record.lineIndices.size();
250 stream.write((char*) &record.stringLength, sizeof(ShaderRecord::stringLength));
251 stream.write((char*) &lineCount, sizeof(lineCount));
252 stream.write((char*) record.lineIndices.data(), lineCount * sizeof(uint16_t));
253 }
254 }
255
256 void ShaderIndex::replaceShader(backend::ShaderModel shaderModel, uint8_t variant,
257 backend::ShaderType stage, const char* source, size_t sourceLength) {
258 decodeShadersFromIndices();
259 const uint8_t model = (uint8_t) shaderModel;
260 for (auto& record : mShaderRecords) {
261 if (record.model == model && record.variant == variant && record.stage == stage) {
262 record.decodedShaderText = std::string(source, sourceLength);
263 break;
264 }
265 }
266 encodeShadersToIndices();
267 }
268
269 void ShaderIndex::decodeShadersFromIndices() {
270 for (auto& record : mShaderRecords) {
271 record.decodedShaderText.clear();
272 for (uint16_t index : record.lineIndices) {
273 if (index >= mStringLines.size()) {
274 slog.e << "Internal chunk decoding error." << io::endl;
275 return;
276 }
277 record.decodedShaderText += mStringLines[index] + "\n";
278 }
279 }
280 }
281
282 void ShaderIndex::encodeShadersToIndices() {
283 robin_map<string, uint16_t> table;
284 for (size_t i = 0; i < mStringLines.size(); i++) {
285 table[mStringLines[i]] = uint16_t(i);
286 }
287
288 uint32_t offset = sizeof(uint64_t);
289 for (const auto& record : mShaderRecords) {
290 offset += sizeof(ShaderRecord::model);
291 offset += sizeof(ShaderRecord::variant);
292 offset += sizeof(ShaderRecord::stage);
293 offset += sizeof(ShaderRecord::offset);
294 }
295
296 for (auto& record : mShaderRecords) {
297 record.stringLength = record.decodedShaderText.length() + 1;
298 record.lineIndices.clear();
299 record.offset = offset;
300
301 offset += sizeof(ShaderRecord::stringLength);
302 offset += sizeof(uint32_t);
303
304 const char* const start = record.decodedShaderText.c_str();
305 const size_t length = record.decodedShaderText.length();
306 for (size_t cur = 0; cur < length; cur++) {
307 size_t pos = cur;
308 size_t len = 0;
309 while (start[cur] != '\n' && cur < length) {
310 cur++;
311 len++;
312 }
313 if (pos + len > length) {
314 slog.e << "Internal chunk encoding error." << io::endl;
315 return;
316 }
317 string newLine(start, pos, len);
318 auto iter = table.find(newLine);
319 if (iter == table.end()) {
320 size_t index = mStringLines.size();
321 if (index > UINT16_MAX) {
322 slog.e << "Chunk encoding error: too many unique codelines." << io::endl;
323 return;
324 }
325 record.lineIndices.push_back(index);
326 table[newLine] = index;
327 mStringLines.push_back(newLine);
328 continue;
329 }
330 record.lineIndices.push_back(iter->second);
331 }
332 offset += sizeof(uint16_t) * record.lineIndices.size();
333 }
334 }
335
336
337 } // namespace matdbg
338 } // namespace filament