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
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1136,10 +1136,14 @@ foreach(backend IN ITEMS warp hardware)
add_test(NAME startup_indirect_draw_indexed_${backend} COMMAND startup_indirect_draw_indexed ${backend})
add_test(NAME startup_indirect_draw_${backend} COMMAND startup_indirect_draw_indexed ${backend} draw)
add_test(NAME startup_indirect_translated_draw_${backend} COMMAND startup_indirect_draw_indexed ${backend} translated)
add_test(NAME startup_indirect_copy_provenance_${backend} COMMAND startup_indirect_draw_indexed ${backend} copy-provenance)
add_test(NAME startup_indirect_copy_provenance_off_${backend} COMMAND startup_indirect_draw_indexed ${backend} copy-provenance-off)
add_test(NAME startup_indirect_mesh_${backend} COMMAND startup_indirect_draw_indexed ${backend} mesh)
set_tests_properties(startup_indirect_draw_indexed_${backend} startup_indirect_draw_${backend} PROPERTIES TIMEOUT 30)
set_tests_properties(startup_indirect_mesh_${backend} PROPERTIES TIMEOUT 30 SKIP_RETURN_CODE 77)
set_tests_properties(startup_indirect_translated_draw_${backend} PROPERTIES TIMEOUT 60 SKIP_RETURN_CODE 77)
set_tests_properties(startup_indirect_copy_provenance_${backend} startup_indirect_copy_provenance_off_${backend}
PROPERTIES TIMEOUT 60 SKIP_RETURN_CODE 77)
endforeach()
set_tests_properties(startup_indirect_draw_indexed_hardware startup_indirect_draw_hardware PROPERTIES SKIP_RETURN_CODE 77)
# Negative control for the x2w-render indirect line: one forwarded call too many must be reported as a mismatch.
Expand Down
19 changes: 19 additions & 0 deletions docs/experiments.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,25 @@ The table's nonzero offset and stride select the second colour, the short view r
non-aliased placement is refused. A raw descriptor over a recording-owned upload page reads the
same colour and refuses a view that crosses its allocation.

### Same-recording GPU copies used by an indirect draw

`X2W_TRANSLATED_INDIRECT_COPY_RANGES=1` enables a bounded handoff from slot 15
`CopyBufferRegion` to the translated indirect draw. The latter still requires
`X2W_TRANSLATED_INDIRECT_DRAW=1`. A copied placed buffer qualifies only when one
validated GPU copy in the current recording completely supplied the requested view.
The source is a same-device desktop resource with no title shadow, a unique alias,
or an already covered range. Later overlapping copies invalidate earlier coverage.
The recording discards this proof at Close, so another recording needs its own writer.

The source-built fixture reproduces a 68-byte request shape with source offset `0xbfffa0`.
Its compute writer fills the source with contrasting colours. The consumer reads red
from the second DWORD in the destination while the title-side green sentinel stays
unchanged. The disabled feature, a new recording using the same destination, a copy
of only 64 bytes, and an unproven overlapping write all leave the target clear.

Run `cmake --build --preset release --target startup_indirect_draw_indexed` and
`ctest --preset release -j1 --no-tests=error -R '^startup_indirect_copy_provenance_' -V`.

### Packed pixel position in a source-built shader

Run `ctest --preset dev --no-tests=error -R '^startup_packed_screen_position_(hardware|warp)$' -V`.
Expand Down
5 changes: 3 additions & 2 deletions src/startup_stubs/d3d12_draw.inl
Original file line number Diff line number Diff line change
Expand Up @@ -1468,10 +1468,11 @@ IndirectBufferView indirect_table_buffer(Wrapper* self, const std::uint32_t* wor
return {};
}
const auto placed = placed_buffer_range(device, address, bytes);
if (placed.reject || !placed.alias || (owner && owner != placed.owner)) return {};
if (placed.reject || !placed.owner || (owner && owner != placed.owner)) return {};
owner = placed.owner;
native = placed.alias;
native = static_cast<ID3D12Resource*>(owner->own().real);
offset = address - reinterpret_cast<u64>(owner->own().title_memory);
if (!placed.alias && (!native || !list_copied_gpu_range(self, native, offset, bytes))) return {};
} else if (owner && !owner->own().title_memory) {
native = static_cast<ID3D12Resource*>(owner->own().real);
D3D12_HEAP_PROPERTIES props{};
Expand Down
34 changes: 34 additions & 0 deletions src/startup_stubs/d3d12_xs_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1957,6 +1957,8 @@ std::size_t readable_window(u64 address, std::size_t wanted)

// Per-list state while recording.
struct Pass {
struct CopyRange { ID3D12Resource* resource; u64 offset, bytes; };
std::vector<CopyRange> copied_gpu_ranges; // exact CopyBufferRegion destinations on this recording
bool pipeline_real = false; // the bound pipeline has a desktop pipeline
bool compute_pipeline = false;
ID3D12RootSignature* compute_root = nullptr; // desktop compute root set by slot 33
Expand Down Expand Up @@ -2018,6 +2020,38 @@ Pass& pass_of(Wrapper* self)
if (!self->own().pass) self->own().pass = new Pass;
return *static_cast<Pass*>(self->own().pass);
}

bool indirect_copy_ranges()
{
static const bool enabled = x2w_local::env_on("X2W_TRANSLATED_INDIRECT_COPY_RANGES");
return enabled;
}

bool list_copied_gpu_range(Wrapper* self, ID3D12Resource* resource, u64 offset, u64 bytes)
{
if (!indirect_copy_ranges()) return false;
const auto& ranges = pass_of(self).copied_gpu_ranges;
return std::any_of(ranges.begin(), ranges.end(), [&](const Pass::CopyRange& range) {
return range.resource == resource && offset >= range.offset && bytes <= range.bytes &&
offset - range.offset <= range.bytes - bytes;
});
}

void note_copy_gpu_range(Wrapper* self, ID3D12Resource* resource, u64 offset, u64 bytes, bool authoritative)
{
if (!indirect_copy_ranges()) return;
auto& ranges = pass_of(self).copied_gpu_ranges;
ranges.erase(std::remove_if(ranges.begin(), ranges.end(), [&](const Pass::CopyRange& range) {
if (range.resource != resource) return false;
return range.offset <= offset ? offset - range.offset < range.bytes : range.offset - offset < bytes;
}), ranges.end());
if (authoritative && ranges.size() < 64) {
try { ranges.push_back({resource, offset, bytes}); }
catch (const std::bad_alloc&) { return; }
static LONG reports;
if (first_calls(reports, 16)) log("x2w-render recorded GPU buffer copy range: offset 0x%llx, %llu bytes", offset, bytes);
}
}
// Desktop resources rest in COMMON between lists; a list moves them as it needs and back at Close.
void require_state(Wrapper* self, ID3D12Resource* resource, D3D12_RESOURCE_STATES state)
{
Expand Down
34 changes: 31 additions & 3 deletions src/startup_stubs/list_copy.inl
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,24 @@ x2w_startup::CopyRefusal check_roles_and_sync(Wrapper* self, const CopyEndpoint&
return x2w_startup::CopyRefusal::none;
}

// A covered range in a copied placement is useful only if the recorded copy read an authoritative source.
bool copy_source_authoritative(Wrapper* self, const CopyEndpoint& from, u64 offset, u64 bytes)
{
if (!from.wrapper || !InterlockedCompareExchange(&from.wrapper->own().refs, 0, 0)) return false;
const auto* info = resource_info(from.wrapper);
if (!info || info->initial_state != D3D12_RESOURCE_STATE_COMMON) return false;
x2w_startup::ComPtr<ID3D12Device> device;
if (FAILED(from.resource->GetDevice(IID_PPV_ARGS(device.GetAddressOf()))) || device.Get() != device_of(self->own().list_device))
return false;
if (!from.wrapper->own().title_memory) return true;
if (!info->placement_device || info->placement_device != self->own().list_device) return false;
const auto address = reinterpret_cast<u64>(from.wrapper->own().title_memory);
if (offset > UINT64_MAX - address) return false;
const auto placed = placed_buffer_range(self->own().list_device, address + offset, bytes);
if (placed.reject || placed.owner != from.wrapper) return false;
return placed.alias || list_copied_gpu_range(self, from.resource, offset, bytes);
}

// List slot 15, CopyBufferRegion(destination, destination offset, source, source offset, bytes).
void __stdcall list_copy_buffer_region(Wrapper* self, u64 destination, u64 destination_offset, u64 source, u64 source_offset,
u64 bytes)
Expand All @@ -168,13 +186,21 @@ void __stdcall list_copy_buffer_region(Wrapper* self, u64 destination, u64 desti
auto refusal = x2w_startup::check_copy_buffer_region(to.resource ? &to_desc : nullptr, destination_offset,
from.resource ? &from_desc : nullptr, source_offset, bytes,
to.resource == from.resource);
if (refusal == x2w_startup::CopyRefusal::none) refusal = check_roles_and_sync(self, to, from, true);
if (refusal == x2w_startup::CopyRefusal::none) refusal = check_endpoint_roles(to, from);
const bool authoritative = refusal == x2w_startup::CopyRefusal::none && indirect_copy_ranges() &&
copy_source_authoritative(self, from, source_offset, bytes);
if (refusal == x2w_startup::CopyRefusal::none && !authoritative && !sync_title_source(self, from))
refusal = x2w_startup::CopyRefusal::title_backed_source;
if (refusal != x2w_startup::CopyRefusal::none) return refuse_copy("CopyBufferRegion", refusal);
require_copy_state(self, from.resource, D3D12_RESOURCE_STATE_COPY_SOURCE);
require_copy_state(self, to.resource, D3D12_RESOURCE_STATE_COPY_DEST);
static_cast<ID3D12GraphicsCommandList*>(self->own().real)
->CopyBufferRegion(to.resource, destination_offset, from.resource, source_offset, bytes);
mark_gpu_written(self, to);
if (indirect_copy_ranges()) {
const auto* info = to.wrapper ? resource_info(to.wrapper) : nullptr;
note_copy_gpu_range(self, to.resource, destination_offset, bytes, authoritative && info &&
info->placement == Placement::copied_buffer && info->placement_device == self->own().list_device);
}
InterlockedIncrement(&list_buffer_copies);
}

Expand All @@ -194,7 +220,9 @@ void __stdcall list_copy_resource(Wrapper* self, u64 destination, u64 source)
require_copy_state(self, from.resource, D3D12_RESOURCE_STATE_COPY_SOURCE);
require_copy_state(self, to.resource, D3D12_RESOURCE_STATE_COPY_DEST);
static_cast<ID3D12GraphicsCommandList*>(self->own().real)->CopyResource(to.resource, from.resource);
mark_gpu_written(self, to);
if (to_desc.Dimension == D3D12_RESOURCE_DIMENSION_BUFFER)
note_copy_gpu_range(self, to.resource, 0, to_desc.Width, false);
else mark_gpu_written(self, to);
InterlockedIncrement(&list_resource_copies);
}

Expand Down
Loading
Loading