Skip to content
Merged
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
70 changes: 66 additions & 4 deletions runtime/test/d3d12_texture_tiles_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@

#include <algorithm>
#include <array>
#include <cctype>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>

#define EXPECT(condition) do { \
Expand Down Expand Up @@ -248,14 +250,55 @@ bool run(ID3D12Device& device, UINT width, UINT height, UINT64 tile_copy_offset,
return true;
}

ComPtr<ID3D12Device> native_device() {
// D3D12 on the adapter `luid` names, when it has tiled resources tier 2 or more; null otherwise.
ComPtr<ID3D12Device> tiled_device(IDXGIFactory4& factory, LUID luid, bool print = true) {
ComPtr<IDXGIAdapter1> adapter;
if (!luid.LowPart && !luid.HighPart) return {};
if (FAILED(factory.EnumAdapterByLuid(luid, IID_PPV_ARGS(&adapter)))) return {};
DXGI_ADAPTER_DESC1 desc{};
ComPtr<ID3D12Device> device;
D3D12_FEATURE_DATA_D3D12_OPTIONS options{};
if (FAILED(adapter->GetDesc1(&desc)) || (desc.Flags & DXGI_ADAPTER_FLAG_SOFTWARE) ||
FAILED(D3D12CreateDevice(adapter.Get(), D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&device))) ||
FAILED(device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS, &options, sizeof(options))) ||
options.TiledResourcesTier < D3D12_TILED_RESOURCES_TIER_2)
return {};
if (print)
std::printf("Native D3D12: %ls (the layer's adapter), tiled resources tier %u\n", desc.Description,
unsigned(options.TiledResourcesTier));
return device;
}

// The host adapter for the native half. LEONIDA_HOST_ADAPTER picks it as in d3d12_copy_compare.h: "warp", or part of
// an adapter name (case-insensitive); a value that names no adapter with tiled resources tier 2 skips the native half
// rather than testing another GPU. Unset: the adapter behind the layer's Vulkan device (`layer_luid`, as
// host_adapter.h picks it), so a PC with two GPUs compares the layer with its own GPU's driver; when that adapter
// has no tier 2, the first hardware adapter with tier 2 or more.
ComPtr<ID3D12Device> native_device(LUID layer_luid) {
ComPtr<IDXGIFactory4> factory;
if (FAILED(CreateDXGIFactory1(IID_PPV_ARGS(&factory)))) return {};
char want[128] = {};
GetEnvironmentVariableA("LEONIDA_HOST_ADAPTER", want, sizeof(want));
std::string needle(want);
std::transform(needle.begin(), needle.end(), needle.begin(), [](unsigned char c) { return char(std::tolower(c)); });
if (needle.empty())
if (auto device = tiled_device(*factory.Get(), layer_luid)) return device;
for (UINT i = 0;; ++i) {
ComPtr<IDXGIAdapter1> adapter;
if (factory->EnumAdapters1(i, &adapter) != S_OK) return {};
if (needle == "warp") {
if (i > 0 || FAILED(factory->EnumWarpAdapter(IID_PPV_ARGS(&adapter)))) break;
} else if (factory->EnumAdapters1(i, &adapter) != S_OK) {
break;
}
DXGI_ADAPTER_DESC1 desc{};
if (FAILED(adapter->GetDesc1(&desc)) || (desc.Flags & DXGI_ADAPTER_FLAG_SOFTWARE)) continue;
if (FAILED(adapter->GetDesc1(&desc))) continue;
if (needle.empty()) {
if (desc.Flags & DXGI_ADAPTER_FLAG_SOFTWARE) continue;
} else if (needle != "warp") {
std::string name;
for (const wchar_t* c = desc.Description; *c; ++c) name += char(std::tolower(*c < 128 ? *c : '?'));
if (name.find(needle) == std::string::npos) continue;
}
ComPtr<ID3D12Device> device;
if (FAILED(D3D12CreateDevice(adapter.Get(), D3D_FEATURE_LEVEL_11_0, IID_PPV_ARGS(&device)))) continue;
D3D12_FEATURE_DATA_D3D12_OPTIONS options{};
Expand All @@ -264,6 +307,10 @@ ComPtr<ID3D12Device> native_device() {
std::printf("Native D3D12: %ls, tiled resources tier %u\n", desc.Description, unsigned(options.TiledResourcesTier));
return device;
}
if (!needle.empty())
std::printf("Native D3D12: no adapter matching LEONIDA_HOST_ADAPTER=%s with tiled resources tier 2 or more; "
"native half skipped\n", want);
return {};
}

} // namespace
Expand All @@ -283,7 +330,22 @@ int main() {
std::fputs("A texture with multiple mips did not use the committed fallback\n", stderr);
return 1;
}
auto native = native_device();
const LUID layer_luid = vulkan->GetAdapterLuid();
auto native = native_device(layer_luid);
if (native) {
// Unset LEONIDA_HOST_ADAPTER means the layer's own GPU whenever its driver can run the cases: comparing with
// another GPU's driver measures that driver, not the layer.
char want[2] = {};
const LUID host_luid = native->GetAdapterLuid();
ComPtr<IDXGIFactory4> factory;
if (!GetEnvironmentVariableA("LEONIDA_HOST_ADAPTER", want, sizeof(want)) &&
SUCCEEDED(CreateDXGIFactory1(IID_PPV_ARGS(&factory))) && tiled_device(*factory.Get(), layer_luid, false) &&
(host_luid.LowPart != layer_luid.LowPart || host_luid.HighPart != layer_luid.HighPart)) {
std::fprintf(stderr, "Native D3D12 runs on another adapter than the layer's, which has tiled resources "
"tier 2 or more\n");
return 1;
}
}
const auto* info = rt::d3d12::vulkan_device_info(vulkan.Get());
const bool dedicated_copy = info && !info->copy_queues.empty() && info->copy_family != info->queue_family;
struct ReplayCase {
Expand Down
Loading