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
28 changes: 16 additions & 12 deletions src/startup_stubs/d3d12_memory_srv.inl
Original file line number Diff line number Diff line change
Expand Up @@ -542,19 +542,20 @@ const char* console_address_hold(Wrapper* device, u64 address, const D3D12_RESOU

// Why publish_tiled_texture (or the lookup of a live resource) gave no texture: the causes the census keeps apart
// (x2w_startup::texture_unavailable_cause).
const char* texture_unavailable_reason(Wrapper* device, u64 address, const D3D12_RESOURCE_DESC& desc, bool published)
const char* texture_unavailable_reason(Wrapper* device, const D3D12_RESOURCE_DESC& desc, bool published, bool unreadable)
{
const u64 bytes = published ? address_texture_bytes(device, desc) : 0;
return x2w_startup::texture_unavailable_cause(published, bytes, [&] {
return x2w_local::readable(reinterpret_cast<const void*>(address), bytes);
});
return x2w_startup::texture_unavailable_cause(published, bytes, [unreadable] { return !unreadable; });
}

Wrapper* publish_tiled_texture(Wrapper* device, u64 address, const D3D12_RESOURCE_DESC& desc)
Wrapper* publish_tiled_texture(Wrapper* device, u64 address, const D3D12_RESOURCE_DESC& desc, bool& unreadable)
{
unreadable = false;
const auto bytes = address_texture_bytes(device, desc);
// The transient upload ring accepts individual copies up to 64 MiB.
if (bytes > x2w_startup::max_published_texture_bytes || !x2w_local::readable(reinterpret_cast<const void*>(address), bytes)) return nullptr;
if (bytes > x2w_startup::max_published_texture_bytes) return nullptr;
unreadable = !x2w_local::readable(reinterpret_cast<const void*>(address), bytes);
if (unreadable) return nullptr;
std::lock_guard lock(tiled_srv_mutex);
const auto [first, last] = tiled_srv_addresses.equal_range(address);
for (auto it = first; it != last; ++it) {
Expand Down Expand Up @@ -706,17 +707,18 @@ void __stdcall device_memory_srv(Wrapper* self, const D3D12_RESOURCE_DESC* resou
refuse_memory_srv(refused, "unsupported memory SRV arguments", reason, resource, input, destination);
return;
}
bool unreadable = false;
// A single-element texture is published by address like a tiled view: its texel is read at offset 0.
if (x2w_startup::supports_single_element_srv(*resource, input->view)) {
if (gpu_written_overlap(input->address, x2w_startup::single_element_bytes(*resource))) {
refuse_memory_srv(refused, "GPU-written resource at the supplied address", "gpu_written_resource", resource, input,
destination);
} else if (Wrapper* texture = publish_tiled_texture(self, input->address, *resource)) {
} else if (Wrapper* texture = publish_tiled_texture(self, input->address, *resource, unreadable)) {
view_slot<18, 2>(self, reinterpret_cast<u64>(texture), reinterpret_cast<u64>(&input->view), destination, 0);
note_memory_srv_write(resource, input, destination);
} else {
refuse_memory_srv(refused, "unavailable texture at the supplied base and layout",
texture_unavailable_reason(self, input->address, *resource, true), resource, input, destination);
texture_unavailable_reason(self, *resource, true, unreadable), resource, input, destination);
}
return;
}
Expand Down Expand Up @@ -764,7 +766,8 @@ void __stdcall device_memory_srv(Wrapper* self, const D3D12_RESOURCE_DESC* resou
held = "partial_tile_beyond_xg_size";
}
Wrapper* texture = held ? nullptr
: (tiled_bc_srv(*resource) || x2w_startup::bc64::supports(*resource)) ? publish_tiled_texture(self, input->address, *resource)
: (tiled_bc_srv(*resource) || x2w_startup::bc64::supports(*resource))
? publish_tiled_texture(self, input->address, *resource, unreadable)
: existing_memory_texture(input->address, *resource);
if (first) log_partial_tile_view(*resource, input->address, held, texture != nullptr);
// A new console-layout view reads the address only when there is no matching live resource.
Expand All @@ -778,14 +781,15 @@ void __stdcall device_memory_srv(Wrapper* self, const D3D12_RESOURCE_DESC* resou
held = console_address_hold(self, input->address, *resource);
if (!held && address_resource_overlap(input->address, address_texture_bytes(self, *resource)))
held = "overlapping_resource";
texture = held ? nullptr : publish_tiled_texture(self, input->address, *resource);
texture = held ? nullptr : publish_tiled_texture(self, input->address, *resource, unreadable);
outcome = "published console layout";
}
}
// A publication was attempted for a BC / console-layout view; any other view only looks up a live resource.
const char* unavailable = texture || held ? nullptr
: texture_unavailable_reason(self, input->address, *resource,
tiled_bc_srv(*resource) || x2w_startup::bc64::supports(*resource) || console_layout_srv(*resource));
: texture_unavailable_reason(self, *resource,
tiled_bc_srv(*resource) || x2w_startup::bc64::supports(*resource) || console_layout_srv(*resource),
unreadable);
if (!texture) outcome = held ? held : unavailable;
log_address_kind(outcome, *resource, input->view, input->address);
if (!texture) {
Expand Down
49 changes: 49 additions & 0 deletions tests/startup_memory_srv_diag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,18 @@ struct ViewAt {
Word tail[2];
};
static_assert(offsetof(ViewAt, address) == 0x28 && sizeof(ViewAt) == 0x40);

thread_local ULONG_PTR reserved_texel_address;
thread_local unsigned reserved_texel_reads;

LONG CALLBACK count_reserved_texel_reads(EXCEPTION_POINTERS* exception)
{
const auto& record = *exception->ExceptionRecord;
if (record.ExceptionCode == EXCEPTION_ACCESS_VIOLATION && record.NumberParameters >= 2 &&
record.ExceptionInformation[0] == 0 && record.ExceptionInformation[1] == reserved_texel_address)
++reserved_texel_reads;
return EXCEPTION_CONTINUE_SEARCH;
}
} // namespace

int draw_memory_srv_diagnostics(ID3D12Device* desktop, void* guest, void* list)
Expand Down Expand Up @@ -108,6 +120,43 @@ int draw_memory_srv_diagnostics(ID3D12Device* desktop, void* guest, void* list)
invoke(guest, 70, ptr(&r8), 0, cpu);
if (!has_report_prefix("x2w-d3d12 memory SRV refusals (slot 70): total 12; "
"null_destination 1, unreadable_resource 1, unreadable_view 1,")) return 55;

// Each readability probe of this reserved, one-texel range faults on its first read.
const D3D12_RESOURCE_DESC texel{D3D12_RESOURCE_DIMENSION_TEXTURE2D, 0, 1, 1, 1, 1,
DXGI_FORMAT_R8G8B8A8_UNORM, {1, 0}, D3D12_TEXTURE_LAYOUT_UNKNOWN, D3D12_RESOURCE_FLAG_NONE};
void* reserved = VirtualAlloc(nullptr, 4096, MEM_RESERVE, PAGE_READWRITE);
if (!reserved) return 56;
ViewAt unreadable = v;
unreadable.view.Format = texel.Format;
unreadable.address = ptr(reserved);
reserved_texel_address = unreadable.address;
reserved_texel_reads = 0;
const auto handler = AddVectoredExceptionHandler(1, count_reserved_texel_reads);
unsigned logged_reads = 0;
if (handler) {
invoke(guest, 70, ptr(&texel), ptr(&unreadable), cpu);
logged_reads = reserved_texel_reads;
reserved_texel_reads = 0;
invoke(guest, 70, ptr(&texel), ptr(&unreadable), cpu);
}
const bool removed = handler && RemoveVectoredExceptionHandler(handler);
reserved_texel_address = 0;
VirtualFree(reserved, 0, MEM_RELEASE);
if (!removed) return 57;
// A logged refusal faults in publish_tiled_texture's probe and the 1-byte probe in
// log_refused_memory_srv. Main had three faults because the reason function probed again.
// The repeat uses the same shape after the log limit, so only the publication probe faults.
if (logged_reads != 2 || reserved_texel_reads != 1) {
std::fprintf(stderr, "Expected 2 logged and 1 repeat reserved texel probe faults, observed %u and %u\n",
logged_reads, reserved_texel_reads);
return 58;
}
x2w_d3d12_census_report(report.data(), report.size());
if (!std::strstr(report.data(), "texture_unreadable 2,") ||
!std::strstr(report.data(), "reason=texture_unreadable ")) {
std::fprintf(stderr, "Expected two texture_unreadable refusals and their shape:\n%s", report.data());
return 59;
}
}
return 0;
}
8 changes: 4 additions & 4 deletions tests/startup_memory_srv_diag_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ def check(directory, consumer, backend, control=False):
log = trap_log.read_text(errors="replace").splitlines()
slot70 = [line for line in log if "Device::slot70" in line]
reasons = [re.search(r"reason=(\S+)", line).group(1) for line in slot70]
if reasons != ["null_address"] * 8 + ["unsupported_description"]:
raise AssertionError("slot 70 lines (the ninth is past the line limit):\n" + "\n".join(slot70))
if "fmt=71 size=2048x2048x1 mips=10" not in slot70[-1]:
raise AssertionError(f"the ninth slot 70 line does not name the BC1 shape:\n{slot70[-1]}")
if reasons != ["null_address"] * 8 + ["unsupported_description", "texture_unreadable"]:
raise AssertionError("slot 70 lines (new shapes log past the line limit):\n" + "\n".join(slot70))
if "fmt=71 size=2048x2048x1 mips=10" not in slot70[8]:
raise AssertionError(f"the ninth slot 70 line does not name the BC1 shape:\n{slot70[8]}")
written = [line.rstrip() for line in log if "memory SRV written" in line]
if len(written) != 1 or not re.fullmatch(WRITTEN, written[0]):
raise AssertionError("slot 70 write lines (only the R8 publish writes):\n" + "\n".join(written))
Expand Down
Loading