// Compare depth/stencil ResolveSubresourceRegion with Windows D3D12 on the default adapter and on WARP, through // clears, resolves, copies and Map on both sides: MIN and MAX of either plane, named by the depth format or the // plane's own format, and AVERAGE of the depth plane by its own format (R32_FLOAT, R24_UNORM_X8_TYPELESS). // Clears write whole pixels, so all samples of a pixel are equal and every mode returns that value: these cases // check planes, formats, rectangles and layouts, not the reduction over differing samples. // Refused resolves must fail Close and leave the destination as it was. AVERAGE by the depth format or of stencil, // and a whole subresource to another offset, the host refuses too (its Close fails); a rectangle to another offset, // a plane format naming the other plane, and DECOMPRESS are refused by the layer and not run on the host. // WARP, measured: a resolve with Format D32_FLOAT removes the device (DXGI_ERROR_DRIVER_INTERNAL_ERROR), and a // D24S8 resolve of one plane writes both. So WARP skips those and is compared on the plane resolved. #include "vk_test_device.h" #include "host_wine.h" #include "../shims/d3d12_com.h" #include #include #include #include #define CHECK(condition) do { \ if (!(condition)) { std::fprintf(stderr, "line %d: %s\n", __LINE__, #condition); return false; } \ } while (0) namespace { template struct Ref { T* p = nullptr; ~Ref() { if (p) p->Release(); } T* operator->() const { return p; } }; struct Event { HANDLE handle = CreateEventW(nullptr, FALSE, FALSE, nullptr); ~Event() { if (handle) CloseHandle(handle); } }; constexpr UINT width = 8, height = 6; constexpr D3D12_RECT clear_a{1, 1, 5, 4}, clear_b{4, 3, 8, 6}, source_rect{2, 1, 7, 5}; struct Case { const char* name; DXGI_FORMAT format; bool stencil; // the format has a stencil plane (subresource 1) UINT plane; // the plane resolved D3D12_RESOLVE_MODE mode; bool rect; // source_rect instead of the whole subresource LONG dst_x = -1, dst_y = -1; // the destination offset; -1: source_rect's corner, or 0 without it bool host_refuses = false; // refused cases: the host's Close fails as well DXGI_FORMAT resource = DXGI_FORMAT_UNKNOWN; // the resources' format, when not `format` DXGI_FORMAT view = DXGI_FORMAT_UNKNOWN; // the DSV format, when not `format` }; struct Planes { HRESULT close = E_FAIL; std::vector depth, stencil; }; // Clears a 4x target with three overlapping values and the 1x destination to values none of them have, resolves // `c` and reads every plane of the destination back. A list whose Close fails is executed only when `run_invalid` // (the layer runs what it recorded; the host runtime must not be given such a list). bool resolve_and_read(ID3D12Device* device, const Case& c, Planes& out, bool run_invalid) { Ref queue; Ref allocator; Ref list; Ref list1; Ref fence; D3D12_COMMAND_QUEUE_DESC queue_desc{}; CHECK(SUCCEEDED(device->CreateCommandQueue(&queue_desc, IID_PPV_ARGS(&queue.p)))); CHECK(SUCCEEDED(device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&allocator.p)))); CHECK(SUCCEEDED(device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, allocator.p, nullptr, IID_PPV_ARGS(&list.p)))); CHECK(SUCCEEDED(list->QueryInterface(IID_PPV_ARGS(&list1.p)))); CHECK(SUCCEEDED(device->CreateFence(0, D3D12_FENCE_FLAG_NONE, IID_PPV_ARGS(&fence.p)))); D3D12_HEAP_PROPERTIES heap_properties{}; heap_properties.Type = D3D12_HEAP_TYPE_DEFAULT; D3D12_RESOURCE_DESC texture{}; texture.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D; texture.Width = width; texture.Height = height; texture.DepthOrArraySize = texture.MipLevels = 1; texture.Format = c.resource ? c.resource : c.format; texture.SampleDesc.Count = 4; texture.Flags = D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL; Ref source, target, readback; CHECK(SUCCEEDED(device->CreateCommittedResource(&heap_properties, D3D12_HEAP_FLAG_NONE, &texture, D3D12_RESOURCE_STATE_DEPTH_WRITE, nullptr, IID_PPV_ARGS(&source.p)))); texture.SampleDesc.Count = 1; CHECK(SUCCEEDED(device->CreateCommittedResource(&heap_properties, D3D12_HEAP_FLAG_NONE, &texture, D3D12_RESOURCE_STATE_DEPTH_WRITE, nullptr, IID_PPV_ARGS(&target.p)))); const UINT planes = c.stencil ? 2u : 1u; D3D12_PLACED_SUBRESOURCE_FOOTPRINT placed[2]{}; UINT64 total = 0; device->GetCopyableFootprints(&texture, 0, planes, 0, placed, nullptr, nullptr, &total); D3D12_RESOURCE_DESC buffer{}; buffer.Dimension = D3D12_RESOURCE_DIMENSION_BUFFER; buffer.Width = total; buffer.Height = buffer.SampleDesc.Count = 1; buffer.DepthOrArraySize = buffer.MipLevels = 1; buffer.Layout = D3D12_TEXTURE_LAYOUT_ROW_MAJOR; heap_properties.Type = D3D12_HEAP_TYPE_READBACK; CHECK(SUCCEEDED(device->CreateCommittedResource(&heap_properties, D3D12_HEAP_FLAG_NONE, &buffer, D3D12_RESOURCE_STATE_COPY_DEST, nullptr, IID_PPV_ARGS(&readback.p)))); Ref heap; const D3D12_DESCRIPTOR_HEAP_DESC desc{D3D12_DESCRIPTOR_HEAP_TYPE_DSV, 2, D3D12_DESCRIPTOR_HEAP_FLAG_NONE, 0}; CHECK(SUCCEEDED(device->CreateDescriptorHeap(&desc, IID_PPV_ARGS(&heap.p)))); auto source_view = heap->GetCPUDescriptorHandleForHeapStart(), target_view = source_view; target_view.ptr += device->GetDescriptorHandleIncrementSize(D3D12_DESCRIPTOR_HEAP_TYPE_DSV); D3D12_DEPTH_STENCIL_VIEW_DESC view{c.view ? c.view : c.format, D3D12_DSV_DIMENSION_TEXTURE2DMS}; device->CreateDepthStencilView(source.p, &view, source_view); view.ViewDimension = D3D12_DSV_DIMENSION_TEXTURE2D; device->CreateDepthStencilView(target.p, &view, target_view); const auto flags = static_cast(D3D12_CLEAR_FLAG_DEPTH | (c.stencil ? D3D12_CLEAR_FLAG_STENCIL : 0)); // No 0.5: it is a 24-bit rounding tie that drivers break differently (docs/d3d12_vulkan.md, D24 clears). list->ClearDepthStencilView(source_view, flags, 0.25f, 0x11, 0, nullptr); list->ClearDepthStencilView(source_view, flags, 0.75f, 0x22, 1, &clear_a); list->ClearDepthStencilView(source_view, flags, 1.0f, 0x33, 1, &clear_b); list->ClearDepthStencilView(target_view, flags, 0.0f, 0x5a, 0, nullptr); D3D12_RESOURCE_BARRIER barriers[2]{}; for (auto& barrier : barriers) { barrier.Type = D3D12_RESOURCE_BARRIER_TYPE_TRANSITION; barrier.Transition.Subresource = D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES; barrier.Transition.StateBefore = D3D12_RESOURCE_STATE_DEPTH_WRITE; } barriers[0].Transition.pResource = source.p; barriers[0].Transition.StateAfter = D3D12_RESOURCE_STATE_RESOLVE_SOURCE; barriers[1].Transition.pResource = target.p; barriers[1].Transition.StateAfter = D3D12_RESOURCE_STATE_RESOLVE_DEST; list->ResourceBarrier(2, barriers); D3D12_RECT rect = source_rect; const UINT x = c.dst_x >= 0 ? UINT(c.dst_x) : c.rect ? UINT(rect.left) : 0; const UINT y = c.dst_y >= 0 ? UINT(c.dst_y) : c.rect ? UINT(rect.top) : 0; list1->ResolveSubresourceRegion(target.p, c.plane, x, y, source.p, c.plane, c.rect ? &rect : nullptr, c.format, c.mode); barriers[1].Transition.StateBefore = D3D12_RESOURCE_STATE_RESOLVE_DEST; barriers[1].Transition.StateAfter = D3D12_RESOURCE_STATE_COPY_SOURCE; list->ResourceBarrier(1, &barriers[1]); for (UINT i = 0; i < planes; ++i) { D3D12_TEXTURE_COPY_LOCATION src{}, dst{}; src.pResource = target.p; src.Type = D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX; src.SubresourceIndex = i; dst.pResource = readback.p; dst.Type = D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT; dst.PlacedFootprint = placed[i]; list->CopyTextureRegion(&dst, 0, 0, 0, &src, nullptr); } out.close = list->Close(); if (FAILED(out.close) && !run_invalid) return true; ID3D12CommandList* lists[] = {list.p}; queue->ExecuteCommandLists(1, lists); CHECK(SUCCEEDED(queue->Signal(fence.p, 1))); Event event; CHECK(event.handle); CHECK(SUCCEEDED(fence->SetEventOnCompletion(1, event.handle))); CHECK(WaitForSingleObject(event.handle, 5000) == WAIT_OBJECT_0); void* mapped = nullptr; const D3D12_RANGE range{0, SIZE_T(total)}; CHECK(SUCCEEDED(readback->Map(0, &range, &mapped)) && mapped); const auto* base = static_cast(mapped); out.depth.resize(width * height * 4); for (UINT row = 0; row < height; ++row) std::memcpy(out.depth.data() + row * width * 4, base + placed[0].Offset + row * placed[0].Footprint.RowPitch, width * 4); if (c.stencil) // D24S8 here for (std::size_t i = 3; i < out.depth.size(); i += 4) out.depth[i] = 0; // the X8 byte is undefined out.stencil.clear(); if (c.stencil) { out.stencil.resize(width * height); for (UINT row = 0; row < height; ++row) std::memcpy(out.stencil.data() + row * width, base + placed[1].Offset + row * placed[1].Footprint.RowPitch, width); } const D3D12_RANGE none{}; readback->Unmap(0, &none); return true; } // What the destination holds after `c` (or, resolved = false, after a refused one): the source's clear values inside // the resolved area of the resolved plane, its own everywhere else. Planes expected_planes(const Case& c, bool resolved = true) { Planes out; out.close = S_OK; const auto in = [](const D3D12_RECT& r, UINT x, UINT y) { return LONG(x) >= r.left && LONG(x) < r.right && LONG(y) >= r.top && LONG(y) < r.bottom; }; const D3D12_RECT area = c.rect ? source_rect : D3D12_RECT{0, 0, LONG(width), LONG(height)}; for (UINT y = 0; y < height; ++y) { for (UINT x = 0; x < width; ++x) { float depth = 0.25f; unsigned char stencil = 0x11; if (in(clear_a, x, y)) depth = 0.75f, stencil = 0x22; if (in(clear_b, x, y)) depth = 1.0f, stencil = 0x33; if (!resolved || !in(area, x, y)) depth = 0.0f, stencil = 0x5a; if (c.plane == 1) depth = 0.0f; else stencil = 0x5a; std::uint32_t bits = 0; if (c.stencil) bits = std::uint32_t(double(depth) * 16777215.0 + 0.5); else std::memcpy(&bits, &depth, 4); for (int i = 0; i < 4; ++i) out.depth.push_back(static_cast(bits >> (8 * i))); if (c.stencil) out.stencil.push_back(stencil); } } return out; } bool same_planes(const Planes& a, const Planes& b, bool only_resolved, UINT plane) { if (only_resolved) return plane ? a.stencil == b.stencil : a.depth == b.depth; return a.depth == b.depth && a.stencil == b.stencil; } const char* mode_name(D3D12_RESOLVE_MODE mode) { switch (mode) { case D3D12_RESOLVE_MODE_MIN: return "MIN"; case D3D12_RESOLVE_MODE_MAX: return "MAX"; case D3D12_RESOLVE_MODE_AVERAGE: return "AVERAGE"; case D3D12_RESOLVE_MODE_DECOMPRESS: return "DECOMPRESS"; default: return "?"; } } } // namespace int main() { vktest::Device vk; const auto device_status = vk.initialize(); if (device_status == vktest::DeviceStatus::unavailable) return 77; // no Vulkan GPU: ctest skips if (device_status != vktest::DeviceStatus::ready) return 1; const unsigned errors_before = vk.validation_errors(); VkPhysicalDeviceDepthStencilResolveProperties resolve{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_DEPTH_STENCIL_RESOLVE_PROPERTIES}; VkPhysicalDeviceProperties2 properties{VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_PROPERTIES_2, &resolve}; vk.instance_fn("vkGetPhysicalDeviceProperties2")(vk.physical, &properties); std::printf("Vulkan resolve modes: depth 0x%x, stencil 0x%x\n", unsigned(resolve.supportedDepthResolveModes), unsigned(resolve.supportedStencilResolveModes)); constexpr auto d32 = DXGI_FORMAT_D32_FLOAT, d24 = DXGI_FORMAT_D24_UNORM_S8_UINT; constexpr auto min = D3D12_RESOLVE_MODE_MIN, max = D3D12_RESOLVE_MODE_MAX, average = D3D12_RESOLVE_MODE_AVERAGE; constexpr auto r32 = DXGI_FORMAT_R32_FLOAT, x8 = DXGI_FORMAT_R24_UNORM_X8_TYPELESS, g8 = DXGI_FORMAT_X24_TYPELESS_G8_UINT; constexpr auto r32_typeless = DXGI_FORMAT_R32_TYPELESS, r24g8_typeless = DXGI_FORMAT_R24G8_TYPELESS; std::vector cases; // resolved, and compared with the host for (const bool rect : {false, true}) { for (const auto mode : {min, max}) { cases.push_back({"D32 depth", d32, false, 0, mode, rect}); cases.push_back({"D24S8 depth", d24, true, 0, mode, rect}); cases.push_back({"D24S8 stencil", d24, true, 1, mode, rect}); cases.push_back({"D24S8 stencil as X24_G8", g8, true, 1, mode, rect, -1, -1, false, d24, d24}); } for (const auto mode : {min, max, average}) { cases.push_back({"D32 depth as R32_FLOAT", r32, false, 0, mode, rect, -1, -1, false, d32, d32}); cases.push_back({"D24S8 depth as R24_X8", x8, true, 0, mode, rect, -1, -1, false, d24, d24}); } } cases.push_back({"R32_TYPELESS depth as D32", d32, false, 0, min, false, -1, -1, false, r32_typeless}); cases.push_back({"R24G8_TYPELESS stencil as D24S8", d24, true, 1, max, true, -1, -1, false, r24g8_typeless}); cases.push_back({"R24G8_TYPELESS depth as R24_X8", x8, true, 0, average, true, -1, -1, false, r24g8_typeless, d24}); const Case refused[] = { {"D32 depth", d32, false, 0, average, false, -1, -1, true}, {"D24S8 depth", d24, true, 0, average, true, -1, -1, true}, {"D24S8 stencil", d24, true, 1, average, false, -1, -1, true}, {"D24S8 stencil as X24_G8", g8, true, 1, average, true, -1, -1, true, d24, d24}, {"D32 depth, whole to another offset", d32, false, 0, max, false, 1, 0, true}, {"D32 depth, rectangle to another offset", d32, false, 0, min, true, 0, 0}, {"D24S8 stencil, rectangle to another offset", d24, true, 1, max, true, 3, 2}, {"D24S8 stencil subresource as R24_X8", x8, true, 1, min, false, -1, -1, false, d24, d24}, {"D32 depth", d32, false, 0, D3D12_RESOLVE_MODE_DECOMPRESS, false}, }; unsigned matched = 0, failures = 0, refusals = 0, references = 0, lacking = 0; const auto run = [&]() -> int { Ref ours; rt::d3d12::VulkanDeviceInfo info{}; info.get_instance_proc = vk.instance_proc; info.instance = vk.instance; info.physical = vk.physical; info.device = vk.device; info.queue = vk.queue; info.queue_family = vk.family; if (FAILED(rt::d3d12::create_vulkan_device(info, IID_PPV_ARGS(&ours.p)))) return 1; std::vector results(cases.size()); for (std::size_t i = 0; i < cases.size(); ++i) { const Case& c = cases[i]; const VkResolveModeFlags supported = c.plane ? resolve.supportedStencilResolveModes : resolve.supportedDepthResolveModes; const bool can = supported & (c.mode == min ? VK_RESOLVE_MODE_MIN_BIT : c.mode == max ? VK_RESOLVE_MODE_MAX_BIT : VK_RESOLVE_MODE_AVERAGE_BIT); if (!resolve_and_read(ours.p, c, results[i], true)) return 1; // A mode the device lacks is refused like the cases below. const Planes expected = expected_planes(c, can); if (results[i].close != (can ? S_OK : E_INVALIDARG) || !same_planes(results[i], expected, false, c.plane)) { std::fprintf(stderr, "%s %s rect %d: Close 0x%08lx, destination %s the clears\n", c.name, mode_name(c.mode), int(c.rect), results[i].close, same_planes(results[i], expected, false, c.plane) ? "matches" : "differs from"); ++failures; } if (!can) { results[i] = {}; // not compared with the host ++lacking; } } for (const Case& c : refused) { Planes p; if (!resolve_and_read(ours.p, c, p, true)) return 1; if (p.close != E_INVALIDARG || !same_planes(p, expected_planes(c, false), false, c.plane)) { std::fprintf(stderr, "refused %s %s rect %d: Close 0x%08lx, destination %s\n", c.name, mode_name(c.mode), int(c.rect), p.close, same_planes(p, expected_planes(c, false), false, c.plane) ? "kept" : "changed"); ++failures; } ++refusals; } Ref factory; if (FAILED(CreateDXGIFactory1(IID_PPV_ARGS(&factory.p)))) { std::fprintf(stderr, "no DXGI factory\n"); return 1; } for (const bool warp : {false, true}) { if (hostwine::host_is_wine()) break; // checked against the clears above Ref adapter; Ref host; if (warp && FAILED(factory->EnumWarpAdapter(IID_PPV_ARGS(&adapter.p)))) continue; if (FAILED(D3D12CreateDevice(adapter.p, D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&host.p)))) continue; const char* name = warp ? "WARP" : "the default adapter"; unsigned same = 0, compared = 0, refused_alike = 0, quirks = 0; for (std::size_t i = 0; i < cases.size(); ++i) { const Case& c = cases[i]; if (results[i].depth.empty() || (warp && c.format == d32)) continue; // see the top of the file Planes expected; if (!resolve_and_read(host.p, c, expected, false)) return 1; ++compared; // The layer's result was checked against the clears above. A host that differs from the clears is a // host quirk (NVIDIA's D3D12 driver on D24S8 rectangle resolves), not a layer bug: report, don't fail. if (SUCCEEDED(expected.close) && !same_planes(expected, expected_planes(c), warp, c.plane)) { std::printf("%s %s rect %d against %s: host quirk: the host differs from the clears; the layer " "matches them\n", c.name, mode_name(c.mode), int(c.rect), name); ++quirks; continue; } if (FAILED(expected.close) || !same_planes(expected, results[i], warp, c.plane)) { std::fprintf(stderr, "%s %s rect %d against %s: host Close 0x%08lx, host %s the clears, " "layer and host %s\n", c.name, mode_name(c.mode), int(c.rect), name, expected.close, same_planes(expected, expected_planes(c), warp, c.plane) ? "matches" : "differs from", same_planes(expected, results[i], warp, c.plane) ? "agree" : "differ"); ++failures; continue; } ++same; } for (const Case& c : refused) { if (!c.host_refuses) continue; Planes expected; if (!resolve_and_read(host.p, c, expected, false)) return 1; if (expected.close != E_INVALIDARG) { std::fprintf(stderr, "refused %s %s rect %d: %s's Close 0x%08lx\n", c.name, mode_name(c.mode), int(c.rect), name, expected.close); ++failures; continue; } ++refused_alike; } std::printf("depth/stencil resolves against %s: %u of %u match, %u host quirks, %u refusals alike\n", name, same, compared, quirks, refused_alike); matched += same; ++references; } return 0; }; // Destroy the D3D12 objects before checking the Vulkan validation counter. const int run_failed = run(); const auto validation_errors = vk.validation_errors() - errors_before; if (validation_errors) std::fprintf(stderr, "d3d12_depth_resolve_test: %u new Vulkan validation error(s)\n", validation_errors); const bool need_host = !hostwine::host_is_wine(); if (need_host && !references) std::fprintf(stderr, "no D3D12 reference device (default adapter or WARP)\n"); if (run_failed || failures || validation_errors || (need_host && (!references || !matched))) return 1; std::printf("depth/stencil resolves: %u host comparisons matched, %u refusals kept the destination, %u modes the " "device lacks were refused\n", matched, refusals, lacking); return 0; }