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
12 changes: 12 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1869,6 +1869,14 @@ target_include_directories(startup_bc64_supports_check PRIVATE src)
add_test(NAME startup_bc64_supports COMMAND startup_bc64_supports_check)
add_test(NAME startup_bc64_supports_layout_control COMMAND startup_bc64_supports_check --mutate layout)
set_tests_properties(startup_bc64_supports_layout_control PROPERTIES WILL_FAIL TRUE)
# Which descriptions take the single-element memory SRV path (src/startup_stubs/single_element_srv.h), without
# a device. The mutated run expects an XOR layout to be accepted and is the negative control.
add_executable(startup_single_element_srv_check tests/startup_single_element_srv_check.cpp)
target_compile_features(startup_single_element_srv_check PRIVATE cxx_std_17)
target_include_directories(startup_single_element_srv_check PRIVATE src)
add_test(NAME startup_single_element_srv COMMAND startup_single_element_srv_check)
add_test(NAME startup_single_element_srv_xor_control COMMAND startup_single_element_srv_check --mutate xor)
set_tests_properties(startup_single_element_srv_xor_control PROPERTIES WILL_FAIL TRUE)
# Slot-10 tile plan for layout 0x10a (src/startup_stubs/tile64_plan.h) without a device: tile shapes,
# accepted descriptions and hand-worked plans. The mutated runs are negative controls.
add_executable(startup_tile64_plan_check tests/startup_tile64_plan_check.cpp)
Expand Down Expand Up @@ -2469,6 +2477,10 @@ if(EXISTS "${INTAKE_PYTHON}")
"${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_address_srv_check.py"
"$<TARGET_FILE:startup_stream_consumer>" "${backend}")
set_tests_properties(startup_address_srv_${backend} PROPERTIES TIMEOUT 90 SKIP_RETURN_CODE 77)
add_test(NAME startup_address_single_element_${backend} COMMAND "${CMAKE_COMMAND}" -E env "X2W_FXC=${STARTUP_FXC}"
"${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_single_element_srv_check.py"
"$<TARGET_FILE:startup_stream_consumer>" "${backend}")
set_tests_properties(startup_address_single_element_${backend} PROPERTIES TIMEOUT 90 SKIP_RETURN_CODE 77)
add_test(NAME startup_address_buffer_srv_${backend} COMMAND "${CMAKE_COMMAND}" -E env "X2W_FXC=${STARTUP_FXC}"
"${INTAKE_PYTHON}" -B "${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_address_buffer_srv_check.py"
"$<TARGET_FILE:startup_stream_consumer>" "${backend}")
Expand Down
26 changes: 26 additions & 0 deletions docs/experiments.md
Original file line number Diff line number Diff line change
Expand Up @@ -11750,6 +11750,32 @@ The fixture checks the XG layout, tile spans, block offsets, copy in and out, an
ctest --preset release --no-tests=error --output-on-failure -R '^startup_xg_bc_chain_'
```

### Single-element 2D and 3D textures published by address

Device slot 70 publishes a 1×1 2D or 1×1×1 3D texture by address, without a resource object. It must have one mip, one sample, default or 64 KiB alignment, and a plain colour format whose element is one texel of 1 to 16 bytes. R8_UNORM keeps its placed-texture path. The view must name the whole texture with the default mapping. The only element is read from offset 0 of the guest memory. That holds for layout UNKNOWN, the value seen on such views, and for linear (`0x100`). For the swizzle modes AMD addrlib defines without pipe/bank XOR (`0x101`, `0x102`, `0x105`, `0x106`, `0x109`, `0x10A`) it is inferred from the source, not run: a swizzle address is a block offset plus a bit pattern of the coordinates, both zero at the origin, and a single mip sits at offset 0 (`Gfx10Lib::ComputeSurfaceInfoMacroTiled` at the pinned [revision](https://gitlab.freedesktop.org/mesa/mesa/-/tree/5c142e46f3f6e752ed745fd48912ebb8fad67145/src/amd/addrlib)). Reserved modes (`0x103`, `0x104`, `0x107`, `0x108`, `0x10B`) and XOR modes (`0x110` and up) stay refused. So do larger shapes, mip chains, arrays, multisampling, other alignments, block-compressed formats and render-target, depth or UAV flags. A view over a live render-target, depth or UAV resource that covers the address (its base, or inside its known allocation size) is refused with `reason=gpu_written_resource`, because that resource is written on the desktop side. The translated draw uploads the texel through the footprint path and rehashes it on every bind, so a write between two draws is seen.

Limits: the GPU-written check runs when the view is published, so it does not see a resource created later. The check compares the entire texel with the live resource range (only its base byte if the allocation size is unknown). Published address-only views are never evicted (as for the BC views above).

`startup_single_element_srv` checks 50 descriptions without a device; its `xor_control` expects layout `0x115` to be accepted and must fail. `startup_address_single_element_{warp,hardware}` draws eight columns per dimension from a translated shader that loads texel zero. The columns are:

- RGBA8 at layouts 0 and `0x105`;
- the first texel rewritten and published again;
- the texel rewritten without a new publication, in the same frame;
- B8G8R8A8 and R16G16B16A16_FLOAT;
- 64 KiB alignment.

Before the fourth column, fifteen refused calls name memory holding another texel; the previous view must stay. On Windows 11 build 26300 with the debug layer off, RTX 5090 (32.0.16.2135) and WARP (10.0.26100.10068) matched all 32 RGBA pixels per dimension. Each of these changes fails the WARP draw:

- reverting the runtime change, which makes column 0 read zero;
- accepting 2x1, two mips or layout `0x115` (column 3 reads the other texel);
- dropping the per-bind rehash (column 3 reads the previous texel);
- checking only the base of a GPU-written resource (column 3 reads the other texel).

```powershell
cmake --build --preset release --target startup_stream_consumer startup_single_element_srv_check d3d12_xs_shim
ctest --preset release --no-tests=error --output-on-failure -R '^startup_(single_element_srv|address_single_element_)'
```

### Placed vertex buffers consumed by a translated draw

`startup_placed_vertex_{hardware,warp}` opens two real slot-133 buffers on CPU-visible allocations with bases separated by 1 TiB. Slot 48 supplies the full guest address and byte length to a translated vertex-stream binding. The shader can read from the registered GPU buffer directly when the resource aliases the guest memory, the length covers whole DWORDs and the start is aligned to 16 bytes. Views crossing a registered boundary or overlapping multiple live placements on the list's device produce a null descriptor before staging. Final wrapper release removes the placement from binding lookup.
Expand Down
15 changes: 9 additions & 6 deletions src/startup_stubs/d3d12_draw.inl
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,9 @@ void upload_texture(Wrapper* self, Wrapper* resource, ID3D12Resource* texture)
auto* const memory = static_cast<const unsigned char*>(resource->own().title_memory);
auto* list = recording(self);
const D3D12_RESOURCE_DESC desc = texture->GetDesc();
const bool uploadable = desc.Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE2D ||
// An address-only single-element texture (2D or 3D) uploads its one texel through the footprint path.
const bool single = resource->own().tiled_address_srv && x2w_startup::single_element_bytes(desc) != 0;
const bool uploadable = desc.Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE2D || single ||
(desc.Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE1D && desc.DepthOrArraySize == 1 && desc.Height == 1);
{
static LONG reasons;
Expand Down Expand Up @@ -729,13 +731,14 @@ void upload_texture(Wrapper* self, Wrapper* resource, ID3D12Resource* texture)
desc.Format != DXGI_FORMAT_BC7_UNORM || desc.Width != surface.columns * surface.block_texels ||
desc.Height != surface.rows * surface.block_texels) return;
}
if (tiled && !tiled_chain && (plan.count != 1 || plan.rows[0] != surface.rows ||
if (tiled && !tiled_chain && !single && (plan.count != 1 || plan.rows[0] != surface.rows ||
plan.row_bytes[0] != u64{surface.columns} * surface.block_bytes)) return;
const u64 total = tiled_chain ? x2w_startup::bc64::Chain(original->console).bytes() : tiled ? surface.size() : stride * (slices - 1) + slice_total;
const u64 total = tiled_chain ? x2w_startup::bc64::Chain(original->console).bytes() : tiled && !single ? surface.size() : stride * (slices - 1) + slice_total;
const bool announced = state.generation != generation;
const UINT elapsed = static_cast<UINT>(frame_number) - static_cast<UINT>(state.checked_frame);
const bool recheck = (tiled_address || tiled_chain) ? elapsed != 0
: x2w_startup::recheck_cpu_texture(desc, total, elapsed);
// A single texel (at most 16 bytes) is rehashed on every bind, so a write between two draws is seen.
const bool recheck = single || ((tiled_address || tiled_chain) ? elapsed != 0
: x2w_startup::recheck_cpu_texture(desc, total, elapsed));
if (state.done && !announced && !recheck) return;
if (readable_window(reinterpret_cast<u64>(memory), static_cast<std::size_t>(total)) < total) {
if (!state.done) InterlockedIncrement(&textures_unreadable);
Expand Down Expand Up @@ -766,7 +769,7 @@ void upload_texture(Wrapper* self, Wrapper* resource, ID3D12Resource* texture)
plan.upload(list, texture, upload_ring, mips, slices, slice, i, offset);
}
}
} else if (tiled) {
} else if (tiled && !single) {
u64 offset = 0;
const auto bytes = plan.bytes(0);
auto* rows = reserve_upload_ring(static_cast<std::size_t>(bytes), &offset);
Expand Down
37 changes: 36 additions & 1 deletion src/startup_stubs/d3d12_memory_srv.inl
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ bool supports_memory_srv(const D3D12_RESOURCE_DESC& r, const D3D12_SHADER_RESOUR
v.Texture2D.MipLevels <= r.MipLevels - v.Texture2D.MostDetailedMip &&
!v.Texture2D.PlaneSlice && v.Texture2D.ResourceMinLODClamp == 0;
}
if (x2w_startup::supports_single_element_srv(r, v)) return true;
return r.Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE2D && r.Width && r.Height &&
r.DepthOrArraySize == 1 && r.MipLevels == 1 && r.SampleDesc.Count == 1 && r.SampleDesc.Quality == 0 &&
(r.Format == DXGI_FORMAT_R8_UNORM || tiled_bc_srv(r)) && v.Format == r.Format && !(r.Flags & D3D12_RESOURCE_FLAG_DENY_SHADER_RESOURCE) &&
Expand Down Expand Up @@ -85,6 +86,28 @@ Wrapper* existing_memory_texture(u64 address, const D3D12_RESOURCE_DESC& desc)
return match;
}

// A live render-target, depth or UAV resource overlapping the texel is written on the desktop side, so its
// title memory does not hold the texel. For a resource of unknown size, its base is the only known byte.
bool gpu_written_overlap(u64 address, UINT texel_bytes)
{
constexpr UINT gpu_written = D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET | D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL |
D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS;
bool found = false;
AcquireSRWLockShared(&note_lock);
for (const auto& [wrapper, info] : resource_infos()) {
const u64 base = reinterpret_cast<u64>(wrapper->own().title_memory);
const u64 bytes = info.bytes ? info.bytes : info.console.Dimension == D3D12_RESOURCE_DIMENSION_BUFFER ? info.console.Width : 0;
const bool overlap = address <= base ? base - address < texel_bytes : address - base < bytes;
if (base && overlap &&
(static_cast<UINT>(info.console.Flags) & gpu_written) && InterlockedCompareExchange(&wrapper->own().refs, 0, 0) > 0) {
found = true;
break;
}
}
ReleaseSRWLockShared(&note_lock);
return found;
}

Wrapper* existing_memory_buffer(Wrapper* device, u64 address, u64 bytes)
{
if (!address || !bytes) return nullptr;
Expand Down Expand Up @@ -135,7 +158,8 @@ std::unordered_multimap<u64, Wrapper*> tiled_srv_addresses;

Wrapper* publish_tiled_texture(Wrapper* device, u64 address, const D3D12_RESOURCE_DESC& desc)
{
const auto bytes = x2w_startup::bc64::supports(desc) ? x2w_startup::bc64::Chain(desc).bytes() : x2w_startup::Bc16Surface(desc).size();
const UINT element = x2w_startup::single_element_bytes(desc);
const auto bytes = element ? u64{element} : x2w_startup::bc64::supports(desc) ? x2w_startup::bc64::Chain(desc).bytes() : x2w_startup::Bc16Surface(desc).size();
// The transient upload ring accepts individual copies up to 64 MiB.
if (bytes > (64ull << 20) || !x2w_local::readable(reinterpret_cast<const void*>(address), bytes)) return nullptr;
std::lock_guard lock(tiled_srv_mutex);
Expand Down Expand Up @@ -213,6 +237,17 @@ void __stdcall device_memory_srv(Wrapper* self, const D3D12_RESOURCE_DESC* resou
if (first_calls(refused, 8)) log_refused_memory_srv("unsupported memory SRV arguments", reason, resource, input, destination);
return;
}
// 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))) {
if (first_calls(refused, 8))
log_refused_memory_srv("GPU-written resource at the supplied address", "gpu_written_resource", resource, input, destination);
} else if (Wrapper* texture = publish_tiled_texture(self, input->address, *resource))
view_slot<18, 2>(self, reinterpret_cast<u64>(texture), reinterpret_cast<u64>(&input->view), destination, 0);
else if (first_calls(refused, 8))
log_refused_memory_srv("unavailable texture at the supplied base and layout", "texture_unavailable", resource, input, destination);
return;
}
if (resource->Dimension == D3D12_RESOURCE_DIMENSION_BUFFER) {
const u64 elem_bytes = x2w_startup::buffer_srv_element_bytes(input->view);
const u64 span_bytes = (input->view.Buffer.FirstElement + input->view.Buffer.NumElements) * elem_bytes;
Expand Down
1 change: 1 addition & 0 deletions src/startup_stubs/d3d12_xs_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "startup_stubs/placed_buffer_range.h"
#include "startup_stubs/memory_srv_report.h"
#include "startup_stubs/memory_srv_buffer.h"
#include "startup_stubs/single_element_srv.h"

#include "startup_stubs/slice_footprints.h"
#include "startup_stubs/present_signature_sampler.h"
Expand Down
63 changes: 63 additions & 0 deletions src/startup_stubs/single_element_srv.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#pragma once

#include <d3d12.h>

namespace x2w_startup {
// One texel, one mip, one sample, 2D or 3D. In every layout accepted here the only element sits at offset 0
// of the title memory: UNKNOWN, linear (0x100) and the GFX10 swizzle modes without pipe/bank XOR that AMD addrlib
// defines (256B_S/D 0x101/0x102, 4KB_S/D 0x105/0x106, 64KB_S/D 0x109/0x10A; a swizzle address is block index *
// block size + a bit pattern of the coordinates, both zero at the origin). Reserved modes (0x103, 0x104, 0x107,
// 0x108, 0x10B) and XOR modes (0x110 and up, which can move the origin by the surface's pipe/bank XOR) are
// refused, as is an alignment other than the default or 64 KiB, which the desktop texture cannot take. Formats are
// plain colour formats whose element is one texel with the same bytes on desktop; R8_UNORM keeps its placed-
// texture path, and block-compressed, planar, depth and packed console formats are refused. Returns the
// element size in bytes, or 0 when the description is not such a texture.
inline UINT single_element_bytes(const D3D12_RESOURCE_DESC& r)
{
const UINT layout = static_cast<UINT>(r.Layout);
if ((r.Dimension != D3D12_RESOURCE_DIMENSION_TEXTURE2D && r.Dimension != D3D12_RESOURCE_DIMENSION_TEXTURE3D) ||
r.Width != 1 || r.Height != 1 || r.DepthOrArraySize != 1 || r.MipLevels != 1 || r.SampleDesc.Count != 1 ||
r.SampleDesc.Quality != 0 || (r.Alignment != 0 && r.Alignment != 0x10000))
return 0;
switch (layout) {
case 0: case 0x100: case 0x101: case 0x102: case 0x105: case 0x106: case 0x109: case 0x10A:
break;
default:
return 0;
}
// GPU-written textures are never uploaded from title memory.
if (r.Flags & (D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET | D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL |
D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS | D3D12_RESOURCE_FLAG_DENY_SHADER_RESOURCE))
return 0;
switch (r.Format) {
case DXGI_FORMAT_R32G32B32A32_FLOAT:
return 16;
case DXGI_FORMAT_R16G16B16A16_FLOAT: case DXGI_FORMAT_R16G16B16A16_UNORM:
return 8;
case DXGI_FORMAT_R10G10B10A2_UNORM: case DXGI_FORMAT_R11G11B10_FLOAT: case DXGI_FORMAT_R8G8B8A8_UNORM:
case DXGI_FORMAT_R8G8B8A8_UNORM_SRGB: case DXGI_FORMAT_R8G8B8A8_UINT: case DXGI_FORMAT_R8G8B8A8_SNORM:
case DXGI_FORMAT_R8G8B8A8_SINT: case DXGI_FORMAT_R16G16_FLOAT: case DXGI_FORMAT_R16G16_UNORM:
case DXGI_FORMAT_R32_FLOAT: case DXGI_FORMAT_B8G8R8A8_UNORM: case DXGI_FORMAT_B8G8R8A8_UNORM_SRGB:
return 4;
case DXGI_FORMAT_R8G8_UNORM: case DXGI_FORMAT_R16_FLOAT: case DXGI_FORMAT_R16_UNORM:
return 2;
case DXGI_FORMAT_A8_UNORM:
return 1;
default:
return 0;
}
}

// The view must name the whole texture with its own format, dimension and the default mapping.
inline bool supports_single_element_srv(const D3D12_RESOURCE_DESC& r, const D3D12_SHADER_RESOURCE_VIEW_DESC& v)
{
if (!single_element_bytes(r) || v.Format != r.Format ||
v.Shader4ComponentMapping != D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING)
return false;
if (r.Dimension == D3D12_RESOURCE_DIMENSION_TEXTURE3D)
return v.ViewDimension == D3D12_SRV_DIMENSION_TEXTURE3D && v.Texture3D.MostDetailedMip == 0 &&
v.Texture3D.MipLevels == 1 && v.Texture3D.ResourceMinLODClamp == 0.0f;
return v.ViewDimension == D3D12_SRV_DIMENSION_TEXTURE2D && v.Texture2D.MostDetailedMip == 0 &&
v.Texture2D.MipLevels == 1 && v.Texture2D.PlaneSlice == 0 && v.Texture2D.ResourceMinLODClamp == 0.0f;
}
} // namespace x2w_startup
Loading
Loading