mirror of
https://github.com/ProjectLeonida/mandy.git
synced 2026-12-06 19:48:37 +00:00
352 lines
18 KiB
C++
352 lines
18 KiB
C++
// The compute pass behind D24_UNORM_S8_UINT on a GPU without D24 (src/depth24_convert.comp, vulkan_depth24.h), checked
|
|
// on the GPU against the C++ reference of depth24.h over the whole 24-bit range:
|
|
// 1. every one of the 16777216 words, upload direction: the float must be the one nearest to d / 16777215
|
|
// (the top byte of the word is junk and must be ignored);
|
|
// 2. the same floats back through the readback direction: every word must return unchanged, with a zero top byte;
|
|
// 3. 16777216 float bit patterns (every 256th pattern of the 32-bit range, offset by a rotating low byte, plus the
|
|
// specials: zeros, denormals, NaNs, infinities, negatives, 1.0 and its neighbours), readback direction, against
|
|
// depth24_quantize;
|
|
// 4. a pitched copy (row and slice strides larger than the extent, an offset into the buffer): only the texels of
|
|
// the region change, to the right values.
|
|
// The pass is tested on the device the Vulkan loader picks (VK_LOADER_DRIVERS_SELECT='*amd*' or '*nv*'); it needs no
|
|
// GPU without D24. Steps 3 and 4 use converters with a 1 MiB and a 1 KiB storage range, so copies split into dispatches.
|
|
// Set LEONIDA_VK_VALIDATION=1 to run under VK_LAYER_KHRONOS_validation; any validation error fails the test.
|
|
#include "vk_test_device.h"
|
|
#include "../src/vulkan_backend.h"
|
|
#include "../src/vulkan_depth24.h"
|
|
|
|
#include <atomic>
|
|
#include <cmath>
|
|
#include <cstdint>
|
|
#include <cstring>
|
|
#include <thread>
|
|
#include <vector>
|
|
|
|
#define EXPECT(condition) do { \
|
|
if (!(condition)) { std::fprintf(stderr, "line %d: %s\n", __LINE__, #condition); return 1; } \
|
|
} while (0)
|
|
|
|
namespace {
|
|
|
|
using namespace leonida;
|
|
|
|
constexpr std::uint32_t words = 1u << 24;
|
|
|
|
float as_float(std::uint32_t bits) {
|
|
float f;
|
|
std::memcpy(&f, &bits, 4);
|
|
return f;
|
|
}
|
|
std::uint32_t as_bits(float f) {
|
|
std::uint32_t bits;
|
|
std::memcpy(&bits, &f, 4);
|
|
return bits;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
int main() {
|
|
vktest::Device gpu;
|
|
if (!gpu.create()) {
|
|
std::printf("depth24_convert_test: no Vulkan device, skipped\n");
|
|
return 0;
|
|
}
|
|
|
|
// The converter allocates nothing here (every job brings its scratch buffer), but the callback must exist.
|
|
Depth24Converter converter(gpu.device_proc, gpu.device, [](const VkMemoryRequirements&) { return VK_NULL_HANDLE; });
|
|
EXPECT(converter.loaded());
|
|
// The same pass with a 1 MiB storage range: a copy must split into slices or rows that each fit it.
|
|
Depth24Converter small(gpu.device_proc, gpu.device, [](const VkMemoryRequirements&) { return VK_NULL_HANDLE; }, 1u << 20);
|
|
Depth24Converter tiny(gpu.device_proc, gpu.device, [](const VkMemoryRequirements&) { return VK_NULL_HANDLE; }, 1024);
|
|
std::size_t last_parts = 0;
|
|
|
|
constexpr VkBufferUsageFlags usage = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_SRC_BIT |
|
|
VK_BUFFER_USAGE_TRANSFER_DST_BIT;
|
|
std::uint32_t *app_words = nullptr, *scratch_words = nullptr;
|
|
const VkBuffer app = gpu.buffer(std::uint64_t(words) * 4 + 4096, usage, reinterpret_cast<void**>(&app_words));
|
|
const VkBuffer scratch = gpu.buffer(std::uint64_t(words) * 4, usage, reinterpret_cast<void**>(&scratch_words));
|
|
EXPECT(app && scratch && app_words && scratch_words);
|
|
|
|
const auto convert = [&](bool to_float, std::uint32_t width, std::uint32_t height, std::uint32_t slices,
|
|
std::uint32_t row_words, std::uint32_t slice_words, std::uint64_t offset, int limited = 0) {
|
|
Depth24Job job;
|
|
job.to_float = to_float;
|
|
job.app = app;
|
|
job.app_offset = offset;
|
|
job.app_size = std::uint64_t(words) * 4 + 4096;
|
|
job.width = width;
|
|
job.height = height;
|
|
job.slices = slices;
|
|
job.app_row_words = row_words;
|
|
job.app_slice_words = slice_words;
|
|
job.scratch = scratch;
|
|
std::vector<Depth24Job> parts;
|
|
if (!(limited == 2 ? tiny : limited ? small : converter).prepare(job, parts)) return false;
|
|
last_parts = parts.size();
|
|
return gpu.run([&](VkCommandBuffer command_buffer) {
|
|
for (const auto& part : parts) converter.run(command_buffer, part);
|
|
VkMemoryBarrier host{VK_STRUCTURE_TYPE_MEMORY_BARRIER};
|
|
host.srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT;
|
|
host.dstAccessMask = VK_ACCESS_HOST_READ_BIT;
|
|
gpu.fn<PFN_vkCmdPipelineBarrier>("vkCmdPipelineBarrier")(command_buffer, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
|
|
VK_PIPELINE_STAGE_HOST_BIT, 0, 1, &host, 0, nullptr,
|
|
0, nullptr);
|
|
return true;
|
|
});
|
|
};
|
|
|
|
// 1. d -> float, for every d, with junk in the top byte.
|
|
for (std::uint32_t d = 0; d < words; ++d) app_words[d] = d | (0xa5u << 24);
|
|
std::memset(scratch_words, 0xcd, std::size_t(words) * 4);
|
|
EXPECT(convert(true, 4096, 4096, 1, 4096, 4096u * 4096u, 0));
|
|
std::uint64_t wrong = 0;
|
|
for (std::uint32_t d = 0; d < words; ++d) {
|
|
const float want = depth24_to_float(d);
|
|
if (as_bits(want) != scratch_words[d] && wrong++ < 8)
|
|
std::fprintf(stderr, "to float: d=%u got %08x want %08x\n", d, scratch_words[d], as_bits(want));
|
|
}
|
|
EXPECT(wrong == 0);
|
|
std::printf("depth24: all %u words to float match the nearest float of d / 16777215\n", words);
|
|
|
|
// 2. the same floats back: every word returns, the top byte becomes 0.
|
|
std::memset(app_words, 0xcd, std::size_t(words) * 4);
|
|
EXPECT(convert(false, 4096, 4096, 1, 4096, 4096u * 4096u, 0));
|
|
wrong = 0;
|
|
for (std::uint32_t d = 0; d < words; ++d)
|
|
if (app_words[d] != d && wrong++ < 8) std::fprintf(stderr, "round trip: d=%u got %08x\n", d, app_words[d]);
|
|
EXPECT(wrong == 0);
|
|
std::printf("depth24: all %u words survive float and back\n", words);
|
|
|
|
// All payloads and both signs with an all-ones exponent. Only positive infinity saturates.
|
|
for (std::uint32_t i = 0; i < words; ++i)
|
|
scratch_words[i] = 0x7f800000u | ((i & 0x800000u) << 8) | (i & 0x7fffffu);
|
|
std::memset(app_words, 0xcd, std::size_t(words) * 4);
|
|
EXPECT(convert(false, 4096, 4096, 1, 4096, words, 0));
|
|
wrong = 0;
|
|
for (std::uint32_t i = 0; i < words; ++i) {
|
|
const std::uint32_t want = i == 0 ? 0xffffffu : 0u;
|
|
if (app_words[i] != want && wrong++ < 8)
|
|
std::fprintf(stderr, "special: f=%08x got %u want %u\n", scratch_words[i], app_words[i], want);
|
|
}
|
|
if (wrong) std::fprintf(stderr, "special: %llu mismatches\n", static_cast<unsigned long long>(wrong));
|
|
EXPECT(wrong == 0);
|
|
std::printf("depth24: both infinities and all NaN payloads quantise correctly\n");
|
|
|
|
// 3. float patterns to words, against the reference.
|
|
{
|
|
std::uint32_t x = 7;
|
|
for (std::uint32_t i = 0; i < words; ++i) {
|
|
x = x * 1664525u + 1013904223u;
|
|
scratch_words[i] = i * 256u + (x >> 24); // every 256th pattern of the 32-bit range, jittered inside it
|
|
}
|
|
const std::uint32_t specials[] = {0x00000000, 0x80000000, 0x00000001, 0x007fffff, 0x00800000, 0x33000000,
|
|
0x33800000, 0x7f800000, 0xff800000, 0x7fc00000, 0xffc00000, 0x7fa00000,
|
|
0x3f800000, 0x3f7fffff, 0x3f800001, 0x3effffff, 0x3f000000, 0x3f000001,
|
|
0xbf800000, 0x40000000, 0x7f7fffff, 0xff7fffff, 0x3f7ffffe, 0x34000000,
|
|
0x34000001, 0x33ffffff, 0x33c00000, 0x33c00001, 0x33bfffff};
|
|
for (std::size_t i = 0; i < sizeof(specials) / 4; ++i) scratch_words[i] = specials[i];
|
|
// Exactly the floats around the half steps, where the tie rule shows.
|
|
std::uint32_t y = 99;
|
|
for (std::uint32_t i = 64; i < 64 + 400000; i += 3) {
|
|
y = y * 1664525u + 1013904223u;
|
|
const float half = float((double(y >> 8) + 0.5) / 16777215.0);
|
|
scratch_words[i] = as_bits(half);
|
|
scratch_words[i + 1] = as_bits(std::nextafter(half, 0.0f));
|
|
scratch_words[i + 2] = as_bits(std::nextafter(half, 1.0f));
|
|
}
|
|
std::memset(app_words, 0xcd, std::size_t(words) * 4);
|
|
std::vector<std::uint32_t> input(scratch_words, scratch_words + words);
|
|
EXPECT(convert(false, 4096, 4096, 1, 4096, 4096u * 4096u, 0, 1));
|
|
EXPECT(last_parts > 32);
|
|
wrong = 0;
|
|
for (std::uint32_t i = 0; i < words; ++i) {
|
|
const std::uint32_t want = depth24_quantize(as_float(input[i]));
|
|
if (app_words[i] != want && wrong++ < 8)
|
|
std::fprintf(stderr, "to words: f=%08x got %u want %u\n", input[i], app_words[i], want);
|
|
}
|
|
EXPECT(wrong == 0);
|
|
std::printf("depth24: %u float patterns (specials, ties and their neighbours included) quantise like the reference\n",
|
|
words);
|
|
}
|
|
|
|
// 4. a pitched copy into the middle of a buffer: words outside the region stay, inside they are converted.
|
|
{
|
|
constexpr std::uint32_t w = 37, h = 5, s = 3, row = 64, slice = 400;
|
|
constexpr std::uint64_t offset = 1100; // bytes: not a multiple of the 256-byte descriptor window
|
|
std::memset(app_words, 0xcd, 4096 * 4);
|
|
for (std::uint32_t z = 0; z < s; ++z)
|
|
for (std::uint32_t y = 0; y < h; ++y)
|
|
for (std::uint32_t x = 0; x < w; ++x)
|
|
scratch_words[(z * h + y) * w + x] = as_bits(depth24_to_float((z * 1000 + y * 100 + x) * 5000u + 1));
|
|
EXPECT(convert(false, w, h, s, row, slice, offset, 2));
|
|
EXPECT(last_parts == 6); // 1 KiB holds three rows of a slice: two parts a slice, three slices
|
|
for (std::uint32_t i = 0; i < 4096; ++i) {
|
|
const std::uint32_t rel = i - std::uint32_t(offset / 4);
|
|
const bool inside = i >= offset / 4 && rel / slice < s && (rel % slice) / row < h && (rel % slice) % row < w;
|
|
const std::uint32_t want = inside ? ((rel / slice * 1000 + (rel % slice) / row * 100 + (rel % slice) % row) * 5000u + 1)
|
|
: 0xcdcdcdcdu;
|
|
if (app_words[i] != want) {
|
|
std::fprintf(stderr, "pitched: word %u got %08x want %08x\n", i, app_words[i], want);
|
|
return 1;
|
|
}
|
|
}
|
|
// And upload from the same layout: the words come out in the scratch, tightly packed.
|
|
std::memset(scratch_words, 0xcd, 4096 * 4);
|
|
EXPECT(convert(true, w, h, s, row, slice, offset, 2));
|
|
for (std::uint32_t z = 0; z < s; ++z)
|
|
for (std::uint32_t y = 0; y < h; ++y)
|
|
for (std::uint32_t x = 0; x < w; ++x) {
|
|
const std::uint32_t d = (z * 1000 + y * 100 + x) * 5000u + 1;
|
|
EXPECT(scratch_words[(z * h + y) * w + x] == as_bits(depth24_to_float(d)));
|
|
}
|
|
std::printf("depth24: a pitched copy at an offset touches only its texels\n");
|
|
}
|
|
|
|
// 5. Concurrent VulkanQueue::execute() with enable_depth24 active: one queue runs depth24_as_float
|
|
// CopyTexture roundtrips (lazy tracker_mutex_ acquisition + collect/retire), while a second queue
|
|
// runs non-depth24 CopyBuffer batches concurrently (parallel prepare()), plus an early-failure batch
|
|
// that triggers Depth24Guard retirement.
|
|
{
|
|
VkPhysicalDeviceMemoryProperties mem_props{};
|
|
gpu.instance_fn<PFN_vkGetPhysicalDeviceMemoryProperties>("vkGetPhysicalDeviceMemoryProperties")(
|
|
gpu.physical, &mem_props);
|
|
const auto alloc_device_local = [&](const VkMemoryRequirements& req) -> VkDeviceMemory {
|
|
for (std::uint32_t i = 0; i < mem_props.memoryTypeCount; ++i) {
|
|
if (!(req.memoryTypeBits & (1u << i))) continue;
|
|
if ((mem_props.memoryTypes[i].propertyFlags & VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT) !=
|
|
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT)
|
|
continue;
|
|
VkMemoryAllocateInfo info{VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO};
|
|
info.allocationSize = req.size;
|
|
info.memoryTypeIndex = i;
|
|
VkDeviceMemory mem = VK_NULL_HANDLE;
|
|
if (gpu.fn<PFN_vkAllocateMemory>("vkAllocateMemory")(gpu.device, &info, nullptr, &mem) == VK_SUCCESS)
|
|
return mem;
|
|
return VK_NULL_HANDLE;
|
|
}
|
|
return VK_NULL_HANDLE;
|
|
};
|
|
|
|
VulkanContext context(gpu.device_proc, gpu.device, true);
|
|
EXPECT(context.loaded());
|
|
context.enable_depth24(gpu.device_proc, alloc_device_local);
|
|
|
|
constexpr int kIters = 16;
|
|
constexpr std::uint32_t tex_w = 64, tex_h = 16;
|
|
constexpr std::uint32_t tex_words = tex_w * tex_h;
|
|
constexpr std::uint64_t tex_bytes = std::uint64_t(tex_words) * 4;
|
|
const VkImage depth_img =
|
|
gpu.image(VK_FORMAT_D32_SFLOAT_S8_UINT, tex_w, tex_h, 1, VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT);
|
|
EXPECT(depth_img != VK_NULL_HANDLE);
|
|
|
|
std::uint32_t *d24_up_ptr = nullptr, *d24_rb_ptr = nullptr;
|
|
std::uint32_t *buf_src_ptr = nullptr, *buf_dst_ptr = nullptr;
|
|
const VkBuffer d24_up = gpu.buffer(tex_bytes, usage, reinterpret_cast<void**>(&d24_up_ptr));
|
|
const VkBuffer d24_rb = gpu.buffer(tex_bytes * kIters, usage, reinterpret_cast<void**>(&d24_rb_ptr));
|
|
const VkBuffer buf_src = gpu.buffer(tex_bytes, usage, reinterpret_cast<void**>(&buf_src_ptr));
|
|
const VkBuffer buf_dst = gpu.buffer(tex_bytes * kIters, usage, reinterpret_cast<void**>(&buf_dst_ptr));
|
|
EXPECT(d24_up && d24_rb && buf_src && buf_dst);
|
|
|
|
for (std::uint32_t i = 0; i < tex_words; ++i) {
|
|
d24_up_ptr[i] = ((i * 15973u + 13u) & depth24_max) | (0x7eu << 24);
|
|
buf_src_ptr[i] = 0x12340000u + i;
|
|
}
|
|
|
|
constexpr ResourceId id_depth = 1, id_d24_up = 2, id_d24_rb = 3, id_buf_src = 4, id_buf_dst = 5;
|
|
ResourceBinding img_b{};
|
|
img_b.image = depth_img;
|
|
img_b.format = VK_FORMAT_D32_SFLOAT_S8_UINT;
|
|
img_b.aspects = VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT;
|
|
img_b.width = tex_w;
|
|
img_b.height = tex_h;
|
|
img_b.mip_levels = 1;
|
|
img_b.array_layers = 1;
|
|
img_b.depth = 1;
|
|
img_b.samples = 1;
|
|
img_b.depth24 = true;
|
|
img_b.depth24_as_float = true;
|
|
context.add_resource(id_depth, img_b, VK_IMAGE_LAYOUT_UNDEFINED);
|
|
|
|
const auto reg_buf = [&](ResourceId id, VkBuffer buf, VkDeviceSize sz, bool readback) {
|
|
ResourceBinding b{};
|
|
b.buffer = buf;
|
|
b.size = sz;
|
|
b.host_readable = readback;
|
|
context.add_resource(id, b);
|
|
};
|
|
reg_buf(id_d24_up, d24_up, tex_bytes, false);
|
|
reg_buf(id_d24_rb, d24_rb, tex_bytes * kIters, true);
|
|
reg_buf(id_buf_src, buf_src, tex_bytes, false);
|
|
reg_buf(id_buf_dst, buf_dst, tex_bytes * kIters, true);
|
|
|
|
VulkanQueue queue_a(context, gpu.queue, gpu.family);
|
|
VulkanQueue queue_b(context, gpu.queue, gpu.family);
|
|
EXPECT(queue_a.ok() && queue_b.ok());
|
|
|
|
std::vector<CommandList> d24_lists(kIters);
|
|
std::vector<CommandList> buf_lists(kIters);
|
|
for (int iter = 0; iter < kIters; ++iter) {
|
|
const std::uint64_t off = std::uint64_t(iter) * tex_bytes;
|
|
CopyLocation fp_up{id_d24_up, true, 0, 0, tex_w, tex_h, 1, tex_w * 4};
|
|
CopyLocation fp_rb{id_d24_rb, true, 0, off, tex_w, tex_h, 1, tex_w * 4};
|
|
CopyLocation tex_loc{id_depth, false, 0, 0, 0, 0, 0, 0};
|
|
EXPECT(d24_lists[iter].copy_texture(CopyTexture{tex_loc, 0, 0, 0, fp_up, false, {}}));
|
|
EXPECT(d24_lists[iter].copy_texture(CopyTexture{fp_rb, 0, 0, 0, tex_loc, false, {}}));
|
|
EXPECT(d24_lists[iter].close());
|
|
|
|
EXPECT(buf_lists[iter].copy_buffer(CopyBuffer{id_buf_dst, off, id_buf_src, 0, tex_bytes}));
|
|
EXPECT(buf_lists[iter].close());
|
|
}
|
|
|
|
// Early-failure batch: valid depth24_as_float copy followed by an unknown resource copy must fail cleanly
|
|
// and retire the uncommitted depth24 scratch via Depth24Guard without corrupting subsequent executions.
|
|
{
|
|
CommandList failing_list;
|
|
CopyLocation fp_up{id_d24_up, true, 0, 0, tex_w, tex_h, 1, tex_w * 4};
|
|
CopyLocation tex_loc{id_depth, false, 0, 0, 0, 0, 0, 0};
|
|
EXPECT(failing_list.copy_texture(CopyTexture{tex_loc, 0, 0, 0, fp_up, false, {}}));
|
|
EXPECT(failing_list.copy_buffer(CopyBuffer{99999, 0, id_buf_src, 0, 4}));
|
|
EXPECT(failing_list.close());
|
|
const CommandList* ptrs[] = {&failing_list};
|
|
EXPECT(queue_a.execute(ptrs) == ReplayResult::unresolved_resource);
|
|
}
|
|
|
|
std::atomic<bool> failed{false};
|
|
std::thread ta([&] {
|
|
for (int iter = 0; iter < kIters; ++iter) {
|
|
const CommandList* ptrs[] = {&d24_lists[iter]};
|
|
if (queue_a.execute(ptrs) != ReplayResult::success) {
|
|
failed.store(true);
|
|
break;
|
|
}
|
|
}
|
|
});
|
|
std::thread tb([&] {
|
|
for (int iter = 0; iter < kIters; ++iter) {
|
|
const CommandList* ptrs[] = {&buf_lists[iter]};
|
|
if (queue_b.execute(ptrs) != ReplayResult::success) {
|
|
failed.store(true);
|
|
break;
|
|
}
|
|
}
|
|
});
|
|
ta.join();
|
|
tb.join();
|
|
EXPECT(!failed.load());
|
|
EXPECT(gpu.fn<PFN_vkDeviceWaitIdle>("vkDeviceWaitIdle")(gpu.device) == VK_SUCCESS);
|
|
|
|
for (int iter = 0; iter < kIters; ++iter) {
|
|
const std::size_t base = std::size_t(iter) * tex_words;
|
|
for (std::uint32_t i = 0; i < tex_words; ++i) {
|
|
EXPECT(d24_rb_ptr[base + i] == (d24_up_ptr[i] & depth24_max));
|
|
EXPECT(buf_dst_ptr[base + i] == buf_src_ptr[i]);
|
|
}
|
|
}
|
|
std::printf("depth24: concurrent VulkanQueue::execute (lazy depth24 lock + parallel non-d24 prepare) ok\n");
|
|
}
|
|
|
|
EXPECT(gpu.validation_errors() == 0);
|
|
std::printf("depth24_convert_test: ok\n");
|
|
return 0;
|
|
}
|