Merge pull request #1987 from Thatters

This commit is contained in:
ItsNotRexed
2026-11-22 10:40:08 -05:00
2 changed files with 75 additions and 11 deletions

View File

@ -17,8 +17,13 @@ std::vector<char> pipeline_cache_snapshot(GetData get) {
if (size < sizeof(VkPipelineCacheHeaderVersionOne))
throw std::runtime_error("cache size is smaller than its header");
// Workers keep adding pipelines between the size query and the read, so read into a buffer with room to
// spare: 1/16 of the queried size on the first attempt, doubling on each retry (1/8, 1/4, 1/2). A buffer
// larger than the data is fine; the driver writes the whole cache and reports the bytes it wrote.
const size_t capacity = size + (size >> (4 - attempt));
// Destroy each incomplete snapshot before allocating its replacement.
std::vector<char> data(size);
std::vector<char> data(capacity);
size = capacity;
result = get(&size, data.data());
if (result == VK_INCOMPLETE) continue;
if (result != VK_SUCCESS)

View File

@ -11,24 +11,83 @@
} \
} while (0)
int main() {
namespace {
// A cache that workers keep growing: every call (size query or read) first adds `growth` bytes, or doubles the
// cache when `growth` is 0. A read into a buffer smaller than the cache writes a prefix and returns VK_INCOMPLETE;
// otherwise it writes the whole cache and reports its size, as vkGetPipelineCacheData does.
struct BusyCache {
size_t bytes, growth;
int queries = 0, reads = 0;
const auto growing = [&](size_t* size, void* data) {
VkResult operator()(size_t* size, void* data) {
bytes += growth ? growth : bytes;
if (!data) {
*size = (++queries == 1) ? 32 : 64;
++queries;
*size = bytes;
return VK_SUCCESS;
}
if (++reads == 1) {
++reads;
if (*size < bytes) {
std::memset(data, 'p', *size);
return VK_INCOMPLETE;
}
if (*size != 64) return VK_ERROR_UNKNOWN;
*size = 48; // A successful read can return less than the size query.
*size = bytes;
std::memset(data, 's', *size);
return VK_SUCCESS;
};
CHECK(pipeline_cache_snapshot(growing) == std::vector<char>(48, 's'));
CHECK(queries == 2 && reads == 2);
}
};
// The snapshot, or an empty vector if it threw, so a failing case is reported by CHECK instead of ending the test.
std::vector<char> snapshot(BusyCache& cache) {
try {
return pipeline_cache_snapshot([&](size_t* size, void* d) { return cache(size, d); });
} catch (const std::exception&) {
return {};
}
}
} // namespace
int main() {
// A busy cache that grows less than 1/16 between the size query and the read: one attempt is enough. Reading
// into exactly the queried size returned VK_INCOMPLETE here on every attempt.
{
BusyCache cache{4096, 128};
CHECK(snapshot(cache) == std::vector<char>(4096 + 2 * 128, 's'));
CHECK(cache.queries == 1 && cache.reads == 1);
}
// Growth past the first attempt's headroom: the retry asks for more room and succeeds.
{
BusyCache cache{4096, 400};
CHECK(snapshot(cache) == std::vector<char>(4096 + 4 * 400, 's'));
CHECK(cache.queries == 2 && cache.reads == 2);
}
// A cache that doubles between every call outgrows even the last attempt's 1/2 headroom: the four-attempt error.
{
BusyCache cache{4096, 0};
CHECK(snapshot(cache).empty() && cache.queries == 4 && cache.reads == 4);
}
// A successful read can return less than the size query.
{
int queries = 0, reads = 0;
const auto shrinking = [&](size_t* size, void* data) {
if (!data) {
++queries;
*size = 64;
return VK_SUCCESS;
}
++reads;
*size = 48;
std::memset(data, 's', *size);
return VK_SUCCESS;
};
CHECK(pipeline_cache_snapshot(shrinking) == std::vector<char>(48, 's'));
CHECK(queries == 1 && reads == 1);
}
auto rejected = [](VkResult query_result, size_t available, VkResult read_result, size_t written) {
int calls = 0;
@ -50,7 +109,7 @@ int main() {
CHECK(rejected(VK_SUCCESS, 32, VK_ERROR_DEVICE_LOST, 32) == 2);
CHECK(rejected(VK_SUCCESS, 32, VK_SUCCESS, 0) == 2);
CHECK(rejected(VK_SUCCESS, 32, VK_SUCCESS, 16) == 2);
CHECK(rejected(VK_SUCCESS, 32, VK_SUCCESS, 33) == 2);
CHECK(rejected(VK_SUCCESS, 32, VK_SUCCESS, 32 + 32 / 16 + 1) == 2); // more than the buffer it was given
CHECK(rejected(VK_SUCCESS, 32, VK_INCOMPLETE, 32) == 8);
return 0;
}