Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 101 additions & 3 deletions runtime/test/d3d12_heap_properties_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
// the node masks and DENY bits of a few committed resources; this test covers the rest. Every value is recorded,
// with whether the call wrote its outputs (they start as a sentinel), and the two records must be the same. A
// resource one device refuses to create is reported and its steps aren't compared. No shaders or game data. Skips
// (77) without a Vulkan GPU or the standalone device; without a host D3D12 device only the layer's own record is
// printed. The host runs on the layer's adapter (host_adapter.h; LEONIDA_HOST_ADAPTER picks another, "warp" WARP).
// (77) without a Vulkan GPU or the standalone device. The layer's record is first checked on its own (layer_wrong:
// what D3D12 documents for the call), on every host; without a host D3D12 device that is all that is checked, and so
// it is under Wine, whose host is vkd3d and reports other heap flags (host_wine.h). The host runs on
// the layer's adapter (host_adapter.h; LEONIDA_HOST_ADAPTER picks another, "warp" WARP).
// Set LEONIDA_D3D49_VERBOSE=1 to print every value.
#include "../shims/d3d12_com.h"
#include "host_adapter.h"
#include "host_wine.h"

#include <cstdint>
#include <cstdio>
Expand Down Expand Up @@ -227,6 +230,85 @@ void run(ID3D12Device* device, Record& out) {
}
}

// What the layer has to answer whatever the host is, checked on its record alone so that a run with no host to
// compare with (no host device, or Wine's) still fails on a wrong answer. D3D12 documents GetHeapProperties for
// committed and placed resources, with either output optional, and not for reserved ones. So for every committed or
// placed resource the layer created: the call succeeds with both outputs, with the properties alone and with the
// flags alone; Type is the heap type it was created with and, for a custom heap, so are the CPU page property and the
// memory pool; and a call asking for one output reports what the call asking for both does, and every output asked
// for is written. For a reserved resource the call fails. The values D3D12 derives itself (the flags a committed
// resource reports, the node masks) are the host comparison's. Returns the number of wrong answers; `checked` counts
// the resources looked at.
int layer_wrong(const Record& ours, int* checked) {
int wrong = 0;
const auto find = [&](const std::string& name, std::uint64_t* value) {
for (std::size_t i = 0; i < ours.names.size(); ++i)
if (ours.names[i] == name) {
*value = ours.values[i];
return true;
}
return false;
};
const auto expect = [&](const std::string& name, std::uint64_t want, const char* why) {
std::uint64_t got = 0;
const bool found = find(name, &got);
if (found && got == want) return;
if (found)
std::printf("heap properties %s: the layer answers 0x%llx, 0x%llx expected (%s)\n", name.c_str(),
static_cast<unsigned long long>(got), static_cast<unsigned long long>(want), why);
else
std::printf("heap properties %s: missing from the layer's record (%s)\n", name.c_str(), why);
++wrong;
};
// A value from a call asking for one output: it has to be what the call asking for both gave, and the call has
// to have written it (the outputs start as the sentinel), which holds the other call to having written it too.
const auto same = [&](const std::string& one, const std::string& both) {
std::uint64_t a = 0, b = 0;
const bool found = find(one, &a) && find(both, &b);
if (found && a == b && std::uint32_t(a) != kSentinel) return;
if (found && a == b)
std::printf("heap properties %s: the layer did not write it, with both outputs asked for either\n",
one.c_str());
else
std::printf("heap properties %s: the layer answers 0x%llx where the call with both outputs gave "
"0x%llx\n", one.c_str(), static_cast<unsigned long long>(a),
static_cast<unsigned long long>(b));
++wrong;
};
static const char* const kPropertyFields[] = {"Type", "CPUPageProperty", "MemoryPoolPreference",
"CreationNodeMask", "VisibleNodeMask"};
for (const Case& c : cases()) {
const std::string name = c.name;
std::uint64_t created = 0;
if (!find(name + ": created", &created) || FAILED(HRESULT(std::uint32_t(created)))) continue;
if (c.kind == Kind::reserved) {
for (const char* shape : {"", ", properties only", ", flags only"}) {
std::uint64_t hr = 0;
if (find(name + shape + ": hr", &hr) && FAILED(HRESULT(std::uint32_t(hr)))) continue;
std::printf("heap properties %s%s: the layer answers 0x%llx for a reserved resource, a failure "
"expected\n", name.c_str(), shape, static_cast<unsigned long long>(hr));
++wrong;
}
continue;
}
++*checked;
for (const char* shape : {"", ", properties only", ", flags only"})
expect(name + shape + ": hr", std::uint32_t(S_OK), "a committed or placed resource has heap properties");
for (const char* shape : {"", ", properties only"}) {
expect(name + shape + ": Type", std::uint32_t(c.props.Type), "the heap type it was created with");
if (c.props.Type != D3D12_HEAP_TYPE_CUSTOM) continue;
expect(name + shape + ": CPUPageProperty", std::uint32_t(c.props.CPUPageProperty),
"the custom heap's CPU page property");
expect(name + shape + ": MemoryPoolPreference", std::uint32_t(c.props.MemoryPoolPreference),
"the custom heap's memory pool");
}
for (const char* field : kPropertyFields)
same(name + ", properties only: " + field, name + ": " + field);
same(name + ", flags only: flags", name + ": flags");
}
return wrong;
}

} // namespace

int main() {
Expand All @@ -238,6 +320,20 @@ int main() {
const bool verbose = GetEnvironmentVariableA("LEONIDA_D3D49_VERBOSE", nullptr, 0) > 0;
Record ours;
run(device.p, ours);
// The layer's own answers first, on every host: the comparison below needs a host, and Wine's is not one.
int layer_checked = 0;
const int layer_wrong_answers = layer_wrong(ours, &layer_checked);
std::printf("heap properties: the layer's answers for %d committed and placed resources checked on their own, "
"%d wrong\n", layer_checked, layer_wrong_answers);
EXPECT(layer_checked > 0);
EXPECT(layer_wrong_answers == 0);
// With no host to compare with, LEONIDA_D3D49_VERBOSE still prints what the layer recorded.
const auto print_ours = [&] {
if (!verbose) return;
for (std::size_t i = 0; i < ours.names.size(); ++i)
std::printf("heap properties %s: ours 0x%llx\n", ours.names[i].c_str(),
static_cast<unsigned long long>(ours.values[i]));
};

const HMODULE d3d12 = LoadLibraryExA("d3d12.dll", nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32);
const auto create_host = d3d12 ? reinterpret_cast<PFN_D3D12_CREATE_DEVICE>(
Expand All @@ -251,15 +347,17 @@ int main() {
// No factory, or no adapter for the pass (LEONIDA_HOST_ADAPTER=warp without WARP): don't compare, rather than
// fall back to the default adapter and compare with a GPU the layer doesn't run on.
const bool have_adapter =
SUCCEEDED(CreateDXGIFactory1(IID_PPV_ARGS(&factory.p))) &&
!hostwine::host_is_wine() && SUCCEEDED(CreateDXGIFactory1(IID_PPV_ARGS(&factory.p))) &&
hostadapter::reference(factory.p, &luid, hostadapter::wanted() == "warp", &adapter.p, host_name);
Ref<ID3D12Device> host;
if (!have_adapter) {
std::printf("heap properties: no host adapter to compare with; %zu values recorded, not compared\n",
ours.values.size());
print_ours();
} else if (!create_host || FAILED(create_host(adapter.p, D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&host.p)))) {
std::printf("heap properties: no host D3D12 device to compare with; %zu values recorded, not compared\n",
ours.values.size());
print_ours();
} else {
Record theirs;
run(host.p, theirs);
Expand Down
56 changes: 36 additions & 20 deletions runtime/test/d3d12_indirect_dispatch_count_test.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// D3D12 ExecuteIndirect with a GPU count buffer and one DISPATCH argument per record.
// The same synthetic workload runs on native WARP and on the Vulkan layer, with actual GPU readback.
// Under Wine the WARP runs are skipped (host_wine.h) and the layer is checked against the expected result alone.
// Set LEONIDA_VK_VALIDATION=1 to enable Vulkan core and synchronization validation.
#include "vk_test_device.h"
#include "host_wine.h"
#include "shader_fixture_path.h"
#include "d3d12_test_shaders.h"
#include "../shims/d3d12_com.h"
Expand Down Expand Up @@ -177,24 +179,35 @@ std::string module_version(const wchar_t* name) {
} // namespace

int main() {
using Compile = decltype(&D3DCompile);
const HMODULE compiler = LoadLibraryExW(L"d3dcompiler_47.dll", nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32);
if (!compiler) return 77;
const auto compile = reinterpret_cast<Compile>(GetProcAddress(compiler, "D3DCompile"));
if (!compile) return 77;
Ref<ID3DBlob> native, errors;
if (FAILED(compile(native_shader, std::strlen(native_shader), nullptr, nullptr, nullptr,
"main", "cs_5_0", 0, 0, &native.p, &errors.p)) || !native.p)
return 1;
const auto* native_bytes = static_cast<const std::uint8_t*>(native->GetBufferPointer());
const std::vector<std::uint8_t> native_code(native_bytes, native_bytes + native->GetBufferSize());
// Wine has no WARP (its host is vkd3d on the GPU), and its D3DCompile has no RWByteAddressBuffer::InterlockedAdd
// (E5005 on wine-staging 11.21), so the reference shader did not build and the test ended before the layer ran.
// The host runs are skipped there; the layer is still checked against expected().
const bool compare_host = !hostwine::host_is_wine();
std::vector<std::uint8_t> native_code;
Ref<IDXGIFactory4> factory;
Ref<IDXGIAdapter> warp;
Ref<ID3D12Device> host;
if (FAILED(CreateDXGIFactory1(IID_PPV_ARGS(&factory.p))) ||
FAILED(factory->EnumWarpAdapter(IID_PPV_ARGS(&warp.p))) ||
FAILED(D3D12CreateDevice(warp.p, D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&host.p))))
return 77;
if (compare_host) {
using Compile = decltype(&D3DCompile);
const HMODULE compiler = LoadLibraryExW(L"d3dcompiler_47.dll", nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32);
if (!compiler) return 77;
const auto compile = reinterpret_cast<Compile>(GetProcAddress(compiler, "D3DCompile"));
if (!compile) return 77;
Ref<ID3DBlob> native, errors;
if (FAILED(compile(native_shader, std::strlen(native_shader), nullptr, nullptr, nullptr,
"main", "cs_5_0", 0, 0, &native.p, &errors.p)) || !native.p) {
std::fprintf(stderr, "D3DCompile of the reference shader failed%s%.*s\n", errors.p ? ": " : "",
errors.p ? int(errors->GetBufferSize()) : 0,
errors.p ? static_cast<const char*>(errors->GetBufferPointer()) : "");
return 1;
}
const auto* native_bytes = static_cast<const std::uint8_t*>(native->GetBufferPointer());
native_code.assign(native_bytes, native_bytes + native->GetBufferSize());
if (FAILED(CreateDXGIFactory1(IID_PPV_ARGS(&factory.p))) ||
FAILED(factory->EnumWarpAdapter(IID_PPV_ARGS(&warp.p))) ||
FAILED(D3D12CreateDevice(warp.p, D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&host.p))))
return 77;
}

vktest::Device vk;
vk.request_buffer_device_address = true;
Expand Down Expand Up @@ -237,12 +250,13 @@ int main() {
for (bool update : {false, true}) {
std::array<UINT, 2> native_result{}, layer_result{};
const auto want = expected(count, update);
if (!run(host.p, native_code, count, update, native_result, false) ||
if (!compare_host) native_result = want; // no host run: the layer alone is held to `want`
if ((compare_host && !run(host.p, native_code, count, update, native_result, false)) ||
!run(layer.p, container, count, update, layer_result, true) || layer_result != want ||
(native_result != want && !(update && native_result == expected(count, update, count)))) {
std::fprintf(stderr, "count %u update %d: host %u/%u, layer %u/%u, expected %u/%u\n",
std::fprintf(stderr, "count %u update %d: host %u/%u%s, layer %u/%u, expected %u/%u\n",
count, int(update), native_result[0], native_result[1],
layer_result[0], layer_result[1], want[0], want[1]);
compare_host ? "" : " (not run)", layer_result[0], layer_result[1], want[0], want[1]);
return 1;
}
if (native_result != want) {
Expand All @@ -262,7 +276,9 @@ int main() {
std::fprintf(stderr, "validation errors: %u\n", vk.validation_errors());
return 1;
}
std::printf("counted ExecuteIndirect(DISPATCH): %u native WARP / Vulkan GPU comparisons passed; "
"count=0,1,max,>max, nonzero offsets and GPU count overwrite, no validation errors\n", cases);
std::printf("counted ExecuteIndirect(DISPATCH): %u %s passed; "
"count=0,1,max,>max, nonzero offsets and GPU count overwrite, no validation errors\n", cases,
compare_host ? "native WARP / Vulkan GPU comparisons"
: "Vulkan GPU results against the expected values (no WARP runs under Wine)");
return 0;
}
16 changes: 11 additions & 5 deletions runtime/test/d3d12_noop_copy_test.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// Legal D3D12 no-op copies must not poison Close or emit invalid Vulkan copy commands.
// Synthetic resources and native host WARP comparator; no game content.
// Synthetic resources and native host WARP comparator; no game content. Under Wine the comparator is skipped
// (host_wine.h) and the layer is still checked.
#include "../shims/d3d12_com.h"
#include "host_wine.h"
#include <dxgi1_4.h>
#include <cstdio>
#include <cstring>
Expand Down Expand Up @@ -118,10 +120,14 @@ int main() {
Ref<IDXGIFactory4> factory;
Ref<IDXGIAdapter> warp;
Ref<ID3D12Device> host;
if (FAILED(CreateDXGIFactory1(IID_PPV_ARGS(&factory.p))) ||
FAILED(factory->EnumWarpAdapter(IID_PPV_ARGS(&warp.p))) ||
FAILED(D3D12CreateDevice(warp.p, D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&host.p)))) return 77;
if (check(host.p, "native WARP")) return 1;
// Wine's host is vkd3d, which accepts the upload-heap self-copy Microsoft's runtime refuses: check() would stop
// there, before the layer is looked at.
if (!hostwine::host_is_wine()) {
if (FAILED(CreateDXGIFactory1(IID_PPV_ARGS(&factory.p))) ||
FAILED(factory->EnumWarpAdapter(IID_PPV_ARGS(&warp.p))) ||
FAILED(D3D12CreateDevice(warp.p, D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&host.p)))) return 77;
if (check(host.p, "native WARP")) return 1;
}
Ref<ID3D12Device> layer;
if (FAILED(rt::d3d12::create_vulkan_device_standalone(IID_PPV_ARGS(&layer.p)))) return 77;
return check(layer.p, "Leonida Vulkan");
Expand Down
Loading
Loading