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
26 changes: 26 additions & 0 deletions docs/experiments/agility-compute-views.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,29 @@ WARP 1.0.22 with the Agility headers.

The [WARP setup report](https://github.com/R0Y00/Xbox2Windows/issues/3160#issuecomment-7226553102)
records the two header sizes.

## Device view slots

The device's view slots (18 to 21, `CreateShaderResourceView` through
`CreateDepthStencilView`) take the same title record. A description that ends
exactly where a readable page ends used to be handled at the desktop header's
width: the 48-byte readability test failed, the 3D mip range check was skipped,
and the title's 40-byte record was forwarded for the desktop to read at its own
width. The shim now sizes its own checks by the record (40 bytes for slots 18
and 19, 24 for a render target and 24 for a depth-stencil view), copies the
record into a zero-filled desktop-sized buffer, and forwards that buffer.

`startup_view_dimension_*` measures it by placing the description at the end of
a committed page with an unreadable page after it:

| Case | Before | After |
|---|---|---|
| Out-of-range 3D mip at an ordinary address | refused, descriptor kept | refused, descriptor kept |
| Out-of-range 3D mip at a page boundary | device removed, `0x887a0001` | refused, descriptor kept |
| Valid 3D mip at a page boundary | device removed, `0x887a0001` | created, device alive |

The three rows agree on both WARP and a physical device; the control row is what
separates a working range check from a skipped one. In the in-box configuration
the desktop struct is already 40 bytes, so every row passes before and after the
change: the case is only measurable with the Agility headers, which is why both
Windows CI jobs cannot see it.
39 changes: 25 additions & 14 deletions src/startup_stubs/d3d12_xs_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5751,7 +5751,8 @@ __declspec(noinline) u64 view_slot(Wrapper* self, u64 a, u64 b, u64 c, u64 d)
const u64 raw[4] = {a, b, c, d};
const int described = N == 19 ? 2 : (N == 17 || N == 22) ? 0 : 1;
const std::size_t described_bytes = N == 17 ? 16 : N == 22 ? 52 : N == 18 ? ViewRecord::srv_desc_bytes :
N == 19 ? ViewRecord::uav_desc_bytes : N == 20 ? 20 : 16;
N == 19 ? ViewRecord::uav_desc_bytes : N == 20 ? sizeof(D3D12_RENDER_TARGET_VIEW_DESC) :
sizeof(D3D12_DEPTH_STENCIL_VIEW_DESC);
note_view(N, (N >= 18 && N <= 21) ? raw[0] : 0, N == 19 ? raw[1] : 0, reinterpret_cast<const void*>(raw[described]),
described_bytes, title_dest, args[D], materialized);
};
Expand Down Expand Up @@ -5801,15 +5802,15 @@ __declspec(noinline) u64 view_slot(Wrapper* self, u64 a, u64 b, u64 c, u64 d)
x2w_view_dimension::needed(N, view_dimension), dimension);
} else if constexpr (N == 19) {
if (view_dimension == D3D12_UAV_DIMENSION_TEXTURE3D &&
x2w_local::readable(reinterpret_cast<const void*>(described), sizeof(D3D12_UNORDERED_ACCESS_VIEW_DESC))) {
x2w_local::readable(reinterpret_cast<const void*>(described), ViewRecord::uav_desc_bytes)) {
D3D12_UNORDERED_ACCESS_VIEW_DESC view{};
std::memcpy(&view, reinterpret_cast<const void*>(described), sizeof(view));
std::memcpy(&view, reinterpret_cast<const void*>(described), ViewRecord::uav_desc_bytes);
skip = volume_uav_range_error(resource_desc, view.Texture3D);
record_refused_uav = skip != nullptr;
} else if (view_dimension == D3D12_UAV_DIMENSION_TEXTURE1D &&
x2w_local::readable(reinterpret_cast<const void*>(described), sizeof(D3D12_UNORDERED_ACCESS_VIEW_DESC))) {
x2w_local::readable(reinterpret_cast<const void*>(described), ViewRecord::uav_desc_bytes)) {
D3D12_UNORDERED_ACCESS_VIEW_DESC view{};
std::memcpy(&view, reinterpret_cast<const void*>(described), sizeof(view));
std::memcpy(&view, reinterpret_cast<const void*>(described), ViewRecord::uav_desc_bytes);
if (view.Texture1D.MipSlice >= resource_desc.MipLevels) skip = "an output 1D texture mip is outside the resource";
record_refused_uav = skip != nullptr;
}
Expand Down Expand Up @@ -5840,14 +5841,24 @@ __declspec(noinline) u64 view_slot(Wrapper* self, u64 a, u64 b, u64 c, u64 d)
if (first_calls(skipped, 6)) log("x2w-d3d12 Device::slot%d view skipped: %s", N, skip);
return 0;
}
// The view descriptions of slots 18-21 start with a DXGI_FORMAT.
alignas(D3D12_SHADER_RESOURCE_VIEW_DESC) unsigned char desc_copy[64];
// The view descriptions of slots 18-21 start with a DXGI_FORMAT. The title supplies its own record width
// (ViewRecord::*), 40 bytes for slots 18 and 19, where the desktop struct is 48 under the Agility SDK:
// copy the record into a zero-filled desktop-sized buffer and forward that, so the desktop never reads
// past the title's record and the shim's checks are sized by the record rather than the desktop header.
alignas(D3D12_SHADER_RESOURCE_VIEW_DESC) unsigned char desc_copy[64]{};
const int desc_index = N == 19 ? 2 : 1;
constexpr std::size_t view_bytes = N == 18 ? sizeof(D3D12_SHADER_RESOURCE_VIEW_DESC) :
N == 19 ? sizeof(D3D12_UNORDERED_ACCESS_VIEW_DESC) :
constexpr std::size_t view_bytes = N == 18 ? ViewRecord::srv_desc_bytes :
N == 19 ? ViewRecord::uav_desc_bytes :
N == 20 ? sizeof(D3D12_RENDER_TARGET_VIEW_DESC) : sizeof(D3D12_DEPTH_STENCIL_VIEW_DESC);
if (N >= 18 && N <= 21 && args[desc_index] && x2w_local::readable(reinterpret_cast<const void*>(args[desc_index]), view_bytes)) {
static_assert(sizeof(D3D12_SHADER_RESOURCE_VIEW_DESC) >= ViewRecord::srv_desc_bytes);
static_assert(sizeof(D3D12_UNORDERED_ACCESS_VIEW_DESC) >= ViewRecord::uav_desc_bytes);
static_assert(sizeof(D3D12_RENDER_TARGET_VIEW_DESC) == 24);
static_assert(sizeof(D3D12_DEPTH_STENCIL_VIEW_DESC) == 24);
const bool desc_copied = N >= 18 && N <= 21 && args[desc_index] &&
x2w_local::readable(reinterpret_cast<const void*>(args[desc_index]), view_bytes);
if (desc_copied) {
std::memcpy(desc_copy, reinterpret_cast<const void*>(args[desc_index]), view_bytes);
args[desc_index] = reinterpret_cast<u64>(desc_copy);
DXGI_FORMAT format;
std::memcpy(&format, desc_copy, sizeof(format));
DXGI_FORMAT mapped = desktop_format(format);
Expand All @@ -5874,15 +5885,15 @@ __declspec(noinline) u64 view_slot(Wrapper* self, u64 a, u64 b, u64 c, u64 d)
D3D12_UNORDERED_ACCESS_VIEW_DESC null_uav{};
D3D12_RENDER_TARGET_VIEW_DESC null_rtv{};
if constexpr (N == 19) {
if (c && x2w_local::readable(reinterpret_cast<const void*>(c), sizeof(null_uav)) &&
if (desc_copied &&
x2w_local::adapt_null_view(device_of(self), a != 0, b != 0,
*reinterpret_cast<const D3D12_UNORDERED_ACCESS_VIEW_DESC*>(c), null_uav))
*reinterpret_cast<const D3D12_UNORDERED_ACCESS_VIEW_DESC*>(desc_copy), null_uav))
args[2] = reinterpret_cast<u64>(&null_uav);
}
if constexpr (N == 20) {
if (b && x2w_local::readable(reinterpret_cast<const void*>(b), sizeof(null_rtv)) &&
if (desc_copied &&
x2w_local::adapt_null_view(device_of(self), a != 0,
*reinterpret_cast<const D3D12_RENDER_TARGET_VIEW_DESC*>(b), null_rtv))
*reinterpret_cast<const D3D12_RENDER_TARGET_VIEW_DESC*>(desc_copy), null_rtv))
args[1] = reinterpret_cast<u64>(&null_rtv);
}
if (default_view) args[desc_index] = 0;
Expand Down
87 changes: 87 additions & 0 deletions tests/startup_view_dimension_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
// SRV of a buffer. The device is not removed and the slot reads zero: the refusal leaves a null SRV, not the
// old descriptor. A CopyDescriptorsSimple copy of the slot over another valid view reads zero too.
// (d) a refused UAV replacement leaves the prior descriptor and its title token intact.
// (e) an out-of-range 3D mip is still refused when the 40-byte title description ends exactly at a readable
// page boundary, so the check cannot be sized by the desktop header's 48 bytes.
// (f) a valid description at the same boundary is created and leaves the device alive.
//
// `off` is the negative control and needs X2W_D3D12_VIEW_DEFAULTS=off: (a) must then fail, printing
// "FAIL: (a) default view reads 0.000 0.000 0.000 0.000 ... device not removed", and nothing else may fail.
Expand Down Expand Up @@ -413,6 +416,90 @@ int main(int argc, char** argv)
std::puts("(d) refused UAV replacement kept the live descriptor token");
}

// (e) The title's own D3D12_UNORDERED_ACCESS_VIEW_DESC is 40 bytes. A description that ends exactly where
// a readable page ends must still be read as that record: the 3D mip range check then refuses the view. A
// read sized by the desktop header instead (48 bytes under the Agility SDK) fails at the edge, the check is
// skipped, and the description is forwarded unsized to the desktop.
D3D12_RESOURCE_DESC volume3d_desc = uav_desc;
volume3d_desc.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE3D;
volume3d_desc.DepthOrArraySize = 1;
volume3d_desc.MipLevels = 2;
const auto volume3d_texture = c.committed(volume3d_desc, "3D UAV texture");
if (!volume3d_texture) return 4;
D3D12_UNORDERED_ACCESS_VIEW_DESC volume3d{};
volume3d.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
volume3d.ViewDimension = D3D12_UAV_DIMENSION_TEXTURE3D;
volume3d.Texture3D = {0, 0, 1};
invoke<void>(c.guest, 19, volume3d_texture.Get(), static_cast<IUnknown*>(nullptr), &volume3d, uav_slot);
if (!c.device_ok()) return 5;
std::memcpy(before.data(), reinterpret_cast<const void*>(uav_slot), sizeof(before));
if (!before[0] || before[2] != 19) {
std::puts("FAIL: (e) no valid 3D UAV token before the boundary cases");
return 4;
}
volume3d.Texture3D.MipSlice = 2; // the texture has two mips, 0 and 1
// Control: the same description at an ordinary address is refused and leaves the token alone.
invoke<void>(c.guest, 19, volume3d_texture.Get(), static_cast<IUnknown*>(nullptr), &volume3d, uav_slot);
std::memcpy(after.data(), reinterpret_cast<const void*>(uav_slot), sizeof(after));
if (after != before) {
std::puts("FAIL: (e) control: an out-of-range 3D mip at an ordinary address changed the live descriptor token");
failed = true;
} else {
std::puts("(e) control: an out-of-range 3D mip at an ordinary address is refused");
}
// The case: the same 40 bytes end where the readable page ends.
SYSTEM_INFO system{};
GetSystemInfo(&system);
const SIZE_T page = system.dwPageSize;
constexpr std::size_t title_uav_bytes = 40;
auto* pages = static_cast<unsigned char*>(VirtualAlloc(nullptr, 2 * page, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE));
DWORD previous = 0;
if (!pages || static_cast<std::size_t>(page) < title_uav_bytes ||
!VirtualProtect(pages + page, page, PAGE_NOACCESS, &previous)) {
std::puts("FAIL: setup: a two-page region with an unreadable second page");
return 4;
}
unsigned char* const edge = pages + page - title_uav_bytes;
std::memcpy(edge, &volume3d, title_uav_bytes);
std::printf("(e) out-of-range 3D UAV whose 40-byte description ends at a page boundary\n");
invoke<void>(c.guest, 19, volume3d_texture.Get(), static_cast<IUnknown*>(nullptr), edge, uav_slot);
std::memcpy(after.data(), reinterpret_cast<const void*>(uav_slot), sizeof(after));
if (!c.device_ok()) {
std::puts("FAIL: (e) the page-boundary description removed the device");
failed = true;
} else if (after != before) {
std::puts("FAIL: (e) the page-boundary description was created instead of refused");
failed = true;
} else {
std::puts("(e) the page-boundary description is refused like an ordinary one");
}
VirtualFree(pages, 0, MEM_RELEASE);

// (f) The same boundary with a valid mip: the description must still be readable, so the view is created
// and the device survives. A forwarded 40-byte record that the desktop reads at its own width fails here.
auto* valid_pages = static_cast<unsigned char*>(VirtualAlloc(nullptr, 2 * page, MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE));
DWORD valid_previous = 0;
if (!valid_pages || !VirtualProtect(valid_pages + page, page, PAGE_NOACCESS, &valid_previous)) {
std::puts("FAIL: setup: a second two-page region with an unreadable second page");
return 4;
}
volume3d.Texture3D.MipSlice = 1; // valid, and a different view than the slot already holds
unsigned char* const valid_edge = valid_pages + page - title_uav_bytes;
std::memcpy(valid_edge, &volume3d, title_uav_bytes);
std::printf("(f) valid 3D UAV whose 40-byte description ends at a page boundary\n");
invoke<void>(c.guest, 19, volume3d_texture.Get(), static_cast<IUnknown*>(nullptr), valid_edge, uav_slot);
std::memcpy(after.data(), reinterpret_cast<const void*>(uav_slot), sizeof(after));
if (!c.device_ok()) {
std::puts("FAIL: (f) a valid description at the page boundary removed the device");
failed = true;
} else if (after == before) {
std::puts("FAIL: (f) a valid description at the page boundary was refused");
failed = true;
} else {
std::puts("(f) a valid description at the page boundary is created");
}
VirtualFree(valid_pages, 0, MEM_RELEASE);

if (!c.device_ok()) {
std::puts("FAIL: device removed after the cases");
return 5;
Expand Down
Loading