Repository navigation
Expand file tree
/
Copy pathBdaResources.cpp
More file actions
363 lines (336 loc) · 18.7 KB
/
Copy pathBdaResources.cpp
File metadata and controls
363 lines (336 loc) · 18.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#include "prx/libSceAgcDriver/Graphics/include/BdaResources.hpp"
#include "prx/libSceAgcDriver/Execution/include/Mutex.hpp"
#include "prx/libSceAgcDriver/Graphics/include/MultisampleTarget.hpp"
#include "ImageTableAbi.hpp"
#include "prx/libSceAgcDriver/Execution/include/GuestMemory.hpp"
#include <algorithm>
#include <atomic>
#include <bit>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <limits>
#include <list>
#include <mutex>
#include <iomanip>
#include <sstream>
namespace AgcDriver::Graphics {
namespace {
struct TableEntry {
std::uint64_t hash;
std::vector<ShaderRecompiler::BdaAbi::Range> ranges;
std::weak_ptr<Buffer> buffer;
std::vector<std::pair<std::uint64_t, std::uint64_t>> spaceKeys;
};
struct TableCache {
AgcDriver::Mutex mutex;
VkDevice device = VK_NULL_HANDLE;
std::list<TableEntry> entries;
std::uint64_t hits = 0;
std::uint64_t misses = 0;
BdaResources::TableCacheStats classes;
};
bool sameRanges(const std::vector<ShaderRecompiler::BdaAbi::Range>& left, const std::vector<ShaderRecompiler::BdaAbi::Range>& right);
void classifyFirstEntry(TableCache& cache, std::uint64_t hash, const std::vector<ShaderRecompiler::BdaAbi::Range>& ranges) {
static const bool profile = std::getenv("APS5_PROFILE_DRAW") != nullptr;
if (!profile) return;
auto& classes = cache.classes;
if (cache.entries.empty()) {
++classes.firstEmpty;
return;
}
const auto& first = cache.entries.front();
if (first.hash == hash) {
if (first.buffer.expired()) ++classes.firstExpired;
else if (!sameRanges(first.ranges, ranges)) ++classes.firstSameHash;
return;
}
if (first.buffer.expired()) {
++classes.firstExpired;
return;
}
const auto common = std::min(first.ranges.size(), ranges.size());
std::size_t at = 0;
while (at < common && std::memcmp(&first.ranges[at], &ranges[at], sizeof(ranges[at])) == 0) ++at;
const auto begin = at < ranges.size() ? ranges[at].begin : at < first.ranges.size() ? first.ranges[at].begin : 0;
++(begin < 0x10000000ull ? classes.firstDiffersLow : classes.firstDiffersHeap);
}
TableCache& Tables() {
static TableCache cache;
return cache;
}
bool tableCacheEnabled() {
static const bool disabled = std::getenv("APS5_NO_BDA_TABLE_CACHE") != nullptr;
return !disabled;
}
std::size_t tableCacheEntries() {
static const std::size_t entries = [] {
const char* value = std::getenv("APS5_BDA_TABLE_CACHE_ENTRIES");
const auto parsed = value != nullptr ? std::strtoull(value, nullptr, 10) : 8ull;
return static_cast<std::size_t>(std::clamp<unsigned long long>(parsed, 1, 64));
}();
return entries;
}
bool sameRanges(const std::vector<ShaderRecompiler::BdaAbi::Range>& left, const std::vector<ShaderRecompiler::BdaAbi::Range>& right) {
return left.size() == right.size() && (left.empty() || std::memcmp(left.data(), right.data(), left.size() * sizeof(left.front())) == 0);
}
// A 64-bit mix of the table's words: a table is ~1200 ranges of four words, and comparing the bytes
// against every held table would cost as much as building it.
std::uint64_t hashRanges(const std::vector<ShaderRecompiler::BdaAbi::Range>& ranges) {
static_assert(sizeof(ShaderRecompiler::BdaAbi::Range) % sizeof(std::uint64_t) == 0);
std::uint64_t hash = 0x9e3779b97f4a7c15ull ^ ranges.size();
for (const auto& range : ranges) {
std::uint64_t words[sizeof(range) / sizeof(std::uint64_t)];
std::memcpy(words, &range, sizeof(range));
for (const auto word : words) {
hash ^= word;
hash *= 0xff51afd7ed558ccdull;
hash ^= hash >> 33u;
}
}
return hash;
}
}
BdaResources::BdaResources(const Context& context) {
static_assert(std::endian::native == std::endian::little);
Require(ShaderRecompiler::BdaAbi::FaultBufferBytes <= context.limits.maxStorageBufferRange, "BDA fault buffer exceeds storage buffer range limit");
fault = std::make_unique<Buffer>(context, ShaderRecompiler::BdaAbi::FaultBufferBytes, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT);
std::memset(fault->Bytes().data(), 0, ShaderRecompiler::BdaAbi::WrittenListWord * sizeof(std::uint32_t));
std::memset(fault->Bytes().data() + ShaderRecompiler::BdaAbi::SrtGuardWord * sizeof(std::uint32_t), 0, sizeof(std::uint32_t));
}
BdaResources::BdaResources(const Context& context, const GuestBufferMemory& memory) : BdaResources(context) {
const auto cached = memory.CachedAddressTable();
std::vector<ShaderRecompiler::BdaAbi::Range> built;
if (!cached.has_value()) built = memory.AddressRanges();
const auto& addressRanges = cached.has_value() ? *cached->ranges : built;
const auto serial = cached.has_value() ? cached->serial : 0;
auto guardSerial = MultisampleTargetRangeGeneration();
if (tableCacheEnabled() && serial != 0) {
auto& cache = Tables();
std::lock_guard lock(cache.mutex);
if (cache.device != context.device) {
cache.entries.clear();
cache.device = context.device;
}
for (auto it = cache.entries.begin(); it != cache.entries.end(); ++it) {
const auto key = std::pair{serial, guardSerial};
if (std::find(it->spaceKeys.begin(), it->spaceKeys.end(), key) == it->spaceKeys.end()) continue;
if (auto shared = it->buffer.lock(); shared != nullptr) {
tableBytes = shared->Bytes().size();
++cache.hits;
++cache.classes.spaceTables;
table = std::move(shared);
cache.entries.splice(cache.entries.begin(), cache.entries, it);
return;
}
cache.entries.erase(it);
break;
}
}
auto filtered = ExcludeMultisampleTargetRanges(context.device, addressRanges, guardSerial);
const auto& ranges = filtered.has_value() ? *filtered : addressRanges;
Require(ranges.size() <= std::numeric_limits<std::uint32_t>::max(), "BDA table range count overflow");
Require(ranges.size() <= (std::numeric_limits<std::size_t>::max() - sizeof(ShaderRecompiler::BdaAbi::Header)) / sizeof(ShaderRecompiler::BdaAbi::Range), "BDA table size overflow");
tableBytes = sizeof(ShaderRecompiler::BdaAbi::Header) + ranges.size() * sizeof(ShaderRecompiler::BdaAbi::Range);
Require(tableBytes <= context.limits.maxStorageBufferRange && ShaderRecompiler::BdaAbi::FaultBufferBytes <= context.limits.maxStorageBufferRange, "BDA descriptors exceed storage buffer range limit");
auto& cache = Tables();
std::uint64_t hash = 0;
if (tableCacheEnabled()) {
std::lock_guard lock(cache.mutex);
// Another device's tables are not this one's (their buffers are dead or foreign).
if (cache.device != context.device) {
cache.entries.clear();
cache.device = context.device;
}
hash = hashRanges(ranges);
classifyFirstEntry(cache, hash, ranges);
for (auto it = cache.entries.begin(); it != cache.entries.end();) {
// Compare the hash before taking a strong reference: locking a non-matching entry's
// buffer could make this thread its last owner (a concurrent reap releasing it) and
// run the pool release under the cache mutex; expired() takes no reference.
if (it->hash != hash) {
if (it->buffer.expired()) {
it = cache.entries.erase(it);
} else {
++it;
}
continue;
}
auto shared = it->buffer.lock();
if (shared == nullptr) {
// Every build that owned the table is gone, and its buffer with it.
it = cache.entries.erase(it);
continue;
}
if (sameRanges(it->ranges, ranges)) {
++cache.hits;
if (serial != 0) {
const auto key = std::pair{serial, guardSerial};
auto found = std::find(it->spaceKeys.begin(), it->spaceKeys.end(), key);
if (found != it->spaceKeys.end()) it->spaceKeys.erase(found);
it->spaceKeys.push_back(key);
if (it->spaceKeys.size() > tableCacheEntries()) it->spaceKeys.erase(it->spaceKeys.begin());
}
table = std::move(shared);
cache.entries.splice(cache.entries.begin(), cache.entries, it);
return;
}
++it;
}
}
Require(ShaderRecompiler::BdaAbi::RangesOrdered(ranges.data(), ranges.size()), "BDA table ranges do not ascend by begin");
for (const auto& range : ranges) Require((range.permissions & ShaderRecompiler::BdaAbi::Write) == 0 || range.end <= ShaderRecompiler::BdaAbi::WrittenAddressLimit, "a writable BDA range ends past the addresses the written-page record can hold");
table = std::make_shared<Buffer>(context, tableBytes, VK_BUFFER_USAGE_STORAGE_BUFFER_BIT);
const ShaderRecompiler::BdaAbi::Header header{ShaderRecompiler::BdaAbi::Version, static_cast<std::uint32_t>(ranges.size()), sizeof(ShaderRecompiler::BdaAbi::Range), 0};
std::memcpy(table->Bytes().data(), &header, sizeof(header));
if (!ranges.empty()) std::memcpy(table->Bytes().data() + sizeof(header), ranges.data(), ranges.size() * sizeof(ranges.front()));
if (tableCacheEnabled()) {
std::lock_guard lock(cache.mutex);
++cache.misses;
if (cache.device != context.device) {
cache.entries.clear();
cache.device = context.device;
}
auto cachedRanges = filtered.has_value() ? std::move(*filtered) : cached.has_value() ? *cached->ranges : std::move(built);
cache.entries.push_front({hash, std::move(cachedRanges), table, {}});
if (serial != 0) cache.entries.front().spaceKeys.emplace_back(serial, guardSerial);
while (cache.entries.size() > tableCacheEntries()) cache.entries.pop_back();
}
}
BdaResources::TableCacheStats BdaResources::TableCacheCounters() {
auto& cache = Tables();
std::lock_guard lock(cache.mutex);
auto stats = cache.classes;
stats.hits = cache.hits;
stats.misses = cache.misses;
stats.held = cache.entries.size();
return stats;
}
VkDescriptorBufferInfo BdaResources::Table() const {
Require(table != nullptr, "BDA page table was not requested");
return {table->Handle(), 0, tableBytes};
}
VkDescriptorBufferInfo BdaResources::Fault() const {
return {fault->Handle(), 0, ShaderRecompiler::BdaAbi::FaultBufferBytes};
}
namespace {
std::atomic<bool> loopGuardTripped{false};
}
bool LoopGuardTripped() {
return loopGuardTripped.load(std::memory_order_relaxed);
}
void BdaResources::CheckFault() const {
markWrittenPages();
ShaderRecompiler::BdaAbi::Fault report{};
std::memcpy(&report, fault->Bytes().data(), sizeof(report));
if (report.state == ShaderRecompiler::BdaAbi::FaultState::Empty) {
Require(static_cast<std::uint32_t>(report.reason) == 0 && report.address == 0 && report.bytes == 0 && report.stage == 0 && report.instruction == 0 && report.otherFaults == 0, "BDA fault record has data without publication");
checkSrtGuard();
return;
}
Require(report.state == ShaderRecompiler::BdaAbi::FaultState::Ready && report.otherFaults <= 1, "incomplete or invalid BDA fault record");
Require(report.reason != ShaderRecompiler::BdaAbi::FaultReason::InvalidRectangle, "rect-list requires finite nondegenerate axis-aligned positions with equal positive W");
if (report.reason == ShaderRecompiler::BdaAbi::FaultReason::LoopLimit) {
// APS5_LOOP_GUARD: the shader left a loop that ran past the guard; the dispatch result is kept.
loopGuardTripped.store(true, std::memory_order_relaxed);
std::fprintf(stderr, "[gpu] loop guard: the loop exit at pc 0x%x of shader 0x%llx ran past %u evaluations\n", report.instruction, static_cast<unsigned long long>(report.address), report.bytes);
std::memset(fault->Bytes().data(), 0, fault->Bytes().size());
return;
}
if (report.reason == ShaderRecompiler::BdaAbi::FaultReason::ImageTable) {
const auto table = std::find_if(imageTables.begin(), imageTables.end(), [&](const ImageTableFaults& candidate) { return candidate.stage == report.stage; });
const auto* tables = table != imageTables.end() ? &*table : imageTables.size() == 1 ? &imageTables.front() : nullptr;
std::ostringstream message;
message << "image table: shader 0x" << std::hex << (tables != nullptr ? tables->shader : 0u) << std::dec << " stage " << report.stage << " pc 0x" << std::hex << report.instruction << " selected the descriptor at 0x" << report.address << std::dec;
if (tables != nullptr && report.bytes < tables->poison.size()) {
const auto& poison = tables->poison[report.bytes];
if (poison.dwordCount != 0) {
message << " (words";
for (std::uint32_t index = 0; index < poison.dwordCount && index < poison.words.size(); ++index) message << ' ' << std::hex << std::setw(8) << std::setfill('0') << poison.words[index];
message << std::dec << ")";
}
message << ": " << ShaderRecompiler::ImageTableAbi::PoisonReasonName(static_cast<ShaderRecompiler::ImageTableAbi::PoisonReason>(poison.reason));
if (report.bytes < tables->messages.size() && tables->messages[report.bytes] != nullptr && !tables->messages[report.bytes]->empty()) message << ": " << *tables->messages[report.bytes];
} else {
message << ": poison entry " << report.bytes;
}
throw std::runtime_error(message.str());
}
std::ostringstream message;
if (report.reason == ShaderRecompiler::BdaAbi::FaultReason::UnlinkedCall) {
message << "shader stage " << report.stage << " called address 0x" << std::hex << report.address << " at program counter 0x" << report.instruction << ", which no linked callee of that call matches";
throw std::runtime_error(message.str());
}
if (report.reason == ShaderRecompiler::BdaAbi::FaultReason::WindowLaneSpill) {
message << "shader stage " << report.stage << ": a relative write stored " << report.bytes << " in lane " << (report.address >> 32u) << " of vector window slot " << (report.address & 0xffffffffu) << ", which holds a scalar spilled with v_writelane_b32 that a later v_readlane_b32 was replaced with";
throw std::runtime_error(message.str());
}
message << "BDA access failed: address=0x" << std::hex << report.address << " instruction=0x" << report.instruction << std::dec << " bytes=" << report.bytes << " stage=" << report.stage << " reason=" << static_cast<std::uint32_t>(report.reason);
if (report.reason == ShaderRecompiler::BdaAbi::FaultReason::Permission && table != nullptr) {
const auto bytes = table->Bytes();
ShaderRecompiler::BdaAbi::Header header{};
std::memcpy(&header, bytes.data(), sizeof(header));
for (std::uint32_t index = 0; index < header.count; ++index) {
ShaderRecompiler::BdaAbi::Range range{};
std::memcpy(&range, bytes.data() + sizeof(header) + index * sizeof(range), sizeof(range));
if (report.address < range.begin || report.address >= range.end) continue;
message << std::hex << "; the store hit 0x" << range.begin << "+0x" << range.end - range.begin << ", read-only in the BDA table: stores through GPU-selected descriptors and FLAT/GLOBAL addresses reach only writable ranges imported in place, not ones served by a mirror or a copy (past APS5_HOST_IMPORT_MIB, or refused by the driver)";
break;
}
}
if (report.reason == ShaderRecompiler::BdaAbi::FaultReason::Unaligned) {
if (report.otherFaults == 0) throw BdaUnalignedAccess(message.str(), report.instruction);
message << "; a fault of another kind was recorded in the same dispatch";
}
throw std::runtime_error(message.str());
}
}
namespace AgcDriver::Graphics {
void BdaResources::SetImageTables(std::vector<ImageTableFaults> tables, std::vector<std::pair<std::uint64_t, std::uint64_t>> ranges) {
imageTables = std::move(tables);
imageTableRanges = std::move(ranges);
}
void BdaResources::SetSrtGuards(std::vector<SrtGuardFaults> guards) {
srtGuards = std::move(guards);
}
void BdaResources::checkSrtGuard() const {
namespace Abi = ShaderRecompiler::BdaAbi;
std::uint32_t guard = 0;
std::memcpy(&guard, fault->Bytes().data() + Abi::SrtGuardWord * sizeof(std::uint32_t), sizeof(guard));
if (guard == 0) return;
const auto stage = guard >> Abi::SrtGuardStageShift;
const auto record = guard & ((1u << Abi::SrtGuardStageShift) - 1u);
const auto found = std::find_if(srtGuards.begin(), srtGuards.end(), [&](const SrtGuardFaults& candidate) { return candidate.stage == stage; });
const auto* guards = found != srtGuards.end() ? &*found : srtGuards.size() == 1 ? &srtGuards.front() : nullptr;
Require(guards != nullptr && record + 2u < guards->flattenedSrt.size(), "a guarded SRT read faulted without its poison record");
const auto& words = guards->flattenedSrt;
const auto address = static_cast<std::uint64_t>(words[record + 1u]) | (static_cast<std::uint64_t>(words[record + 2u]) << 32u);
std::ostringstream message;
message << "BDA access failed: address=0x" << std::hex << address << " instruction=0x" << words[record] << std::dec << " bytes=4 stage=" << stage << " reason=" << static_cast<std::uint32_t>(Abi::FaultReason::Unmapped);
throw std::runtime_error(message.str());
}
void BdaResources::markWrittenPages() const {
namespace Abi = ShaderRecompiler::BdaAbi;
auto* words = reinterpret_cast<std::uint32_t*>(fault->Bytes().data());
Require(words[Abi::WrittenOverflowWord] == 0, "more than " + std::to_string(Abi::WrittenPageSlots) + " chunks of " + std::to_string(Abi::WrittenChunkPages) + " pages stored to through the BDA table in one use are not implemented");
const auto count = words[Abi::WrittenCountWord];
if (count == 0) return;
Require(Abi::ForEachWrittenRange(words, [&](std::uint64_t begin, std::size_t bytes) {
const auto end = begin + bytes;
for (const auto& [base, size] : imageTableRanges) {
if (size != 0 && begin < base + size && base < end) {
std::ostringstream message;
message << "image table at 0x" << std::hex << base << " is written by its own dispatch";
Require(false, message.str());
}
}
GuestMemory::MarkPossiblyWritten(begin, bytes);
}), "invalid BDA written-page record");
for (std::uint32_t claim = 0; claim < count; ++claim) {
const auto slot = words[Abi::WrittenListWord + claim];
words[Abi::WrittenSlotsWord + 2 * slot] = 0;
words[Abi::WrittenSlotsWord + 2 * slot + 1] = 0;
}
words[Abi::WrittenCountWord] = 0;
}
}