Skip to content
Closed
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
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1577,6 +1577,11 @@ add_test(NAME startup_translated_sampler_bindings COMMAND startup_translated_sam
add_test(NAME startup_translated_sampler_bindings_shared_axis_control
COMMAND startup_translated_sampler_bindings_check --mutate shared-axis)
set_tests_properties(startup_translated_sampler_bindings_shared_axis_control PROPERTIES WILL_FAIL TRUE)
# Root descriptor views set through list slots 41, 43, 45 and 46 (src/startup_stubs/root_view_store.h), host-only.
add_executable(startup_root_view_store_check tests/startup_root_view_store.cpp)
target_include_directories(startup_root_view_store_check PRIVATE src)
target_compile_features(startup_root_view_store_check PRIVATE cxx_std_17)
add_test(NAME startup_root_view_store COMMAND startup_root_view_store_check)
Comment thread
tedin7 marked this conversation as resolved.
Resolved
# Real-draw decision for title root signatures that index descriptor heaps directly
# (src/startup_stubs/heap_indexed_root.h), without a device. The mutated runs are the negative controls.
# Limits of the slot 70 refusal and "token" null census (src/startup_stubs/memory_srv_census.h), without a device.
Expand Down
14 changes: 14 additions & 0 deletions docs/experiments.md
Original file line number Diff line number Diff line change
Expand Up @@ -14204,6 +14204,20 @@ The [MASM fixture](../tests/cpu/switch_sal.asm) emits explicit `/6` byte sequenc

Not covered by this experiment: `SAL` outside leaf/switch-lifted functions, and title CPU code execution.

## Root descriptor views on command-list slots 41, 43, 45 and 46

Build and run the host-only check with
`cmake --build --preset dev --target startup_root_view_store_check` and
`ctest --preset dev --no-tests=error -R '^startup_root_view_store$' -V`.
It covers the store only: a view is accepted when a root signature is set, the parameter is below 16 and the
root signature declares it with the same view type; otherwise it is refused and the store keeps its old state.

Implementation boundary:
- Compute CBV (slot 41): the bytes at the title address (at most 64 KiB, the CBV limit) are copied to desktop memory and the copy is bound.
- Compute SRV (slot 43): an address in this recording's own upload pages or in a live aliased placed buffer (tracked from COMMON, 16-byte aligned offset) is bound at its desktop address; any other address is recorded only, because a root SRV has no 64 KiB bound and a prefix copy would let reads past it return unrelated memory.
- Compute UAV (slot 45): bound only when the address lies in a live aliased placed buffer that allows unordered access, so writes reach the title-visible memory at the known offset; otherwise recorded only. Graphics UAV (slot 46): recorded only.
- Not yet measured on a live compute dispatch. The acceptance check (fenced UAV readback at a nonzero offset, and a Dispatch refused while a declared root is unbound, using `RootViewStore::covers`) needs the direct Dispatch of #1974 and the root constants and views of #1975 (see #1441).

## Non-constant scalar symbols at branch targets in the startup translator

Follow-up to #2279, which drops scalar constants at a join that another path writes, over the textual range between
Expand Down
118 changes: 102 additions & 16 deletions src/startup_stubs/d3d12_xs_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include "startup_stubs/placement_publish.h"
#include "startup_stubs/placed_build.h"
#include "startup_stubs/placed_buffer_range.h"
#include "startup_stubs/root_view_store.h"
#include "startup_stubs/memory_srv_report.h"
#include "startup_stubs/memory_srv_census.h"
#include "startup_stubs/memory_srv_buffer.h"
Expand Down Expand Up @@ -2161,6 +2162,8 @@ struct Pass {
std::array<D3D12_GPU_VIRTUAL_ADDRESS, 64> desktop_compute_upload_srvs{};
const std::vector<RootParameter>* compute_params = nullptr;
std::unique_ptr<ComputeBindingTrace> compute_trace;
x2w_startup::RootViewStore compute_views; // slots 41 / 43 / 45
x2w_startup::RootViewStore graphics_uav; // slot 46
IndirectGraphicsPipeline indirect_graphics;
ID3D12RootSignature* graphics_root = nullptr;
bool targets_bound = false;
Expand Down Expand Up @@ -2342,6 +2345,7 @@ void __stdcall list_compute_root(Wrapper* self, void* root)
pass.compute_trace = std::make_unique<ComputeBindingTrace>();
pass.compute_trace->root = w;
}
pass.compute_views.clear();
if (pass.compute_root) {
if (pass.desktop_compute_root.Get() != pass.compute_root)
pass.desktop_compute_upload_srvs.fill(0);
Expand Down Expand Up @@ -2714,6 +2718,7 @@ void __stdcall list_graphics_root(Wrapper* self, void* root)
std::memset(pass.constant_words_bound, 0, sizeof(pass.constant_words_bound));
}
pass.root = params;
pass.graphics_uav.clear();
pass.srv_bound = 0;
const bool real = w && w->own().kind == root_signature_kind && w->own().real;
pass.root_heap_flags = real ? root_heap_flags(w) : 0;
Expand Down Expand Up @@ -2967,21 +2972,6 @@ void __stdcall list_stencil_ref(Wrapper* self, UINT reference)
list->OMSetStencilRef(reference);
}
}
// List slot 43 (desktop 39 + 4), set a compute root SRV backed by this recording's upload pages.
void __stdcall list_compute_upload_srv(Wrapper* self, UINT parameter, UINT64 address)
{
auto* list = recording(self);
auto& pass = pass_of(self);
const auto* params = pass.compute_params;
if (!list || !params || parameter >= params->size() || parameter >= pass.desktop_compute_upload_srvs.size() ||
(*params)[parameter].type != D3D12_ROOT_PARAMETER_TYPE_SRV) return;
const auto gpu = list_upload_gpu(self, address, 1);
if (gpu && !(gpu % D3D12_RAW_UAV_SRV_BYTE_ALIGNMENT)) {
pass.desktop_compute_upload_srvs[parameter] = gpu;
list->SetComputeRootShaderResourceView(parameter, gpu);
}
}

// List slot 44 (desktop 40 + 4), SetGraphicsRootShaderResourceView(parameter, title GPU address).
void __stdcall list_root_srv(Wrapper* self, UINT parameter, u64 address)
{
Expand Down Expand Up @@ -3031,6 +3021,99 @@ void __stdcall list_root_cbv(Wrapper* self, UINT parameter, u64 address)
}
pass.root_address[parameter] = (address && readable_window(address, 1)) ? address : 0;
}
// Declared type of a root parameter, or ~0u when the root signature has no such parameter.
unsigned declared_root_type(const std::vector<RootParameter>* params, UINT parameter)
{
return params && parameter < params->size() ? static_cast<unsigned>((*params)[parameter].type) : ~0u;
}
void log_root_view_refusal(const char* what, UINT parameter, x2w_startup::RootViewResult result)
{
static LONG refused;
if (first_calls(refused, 6)) log("x2w-render %s %u ignored: %s", what, parameter, x2w_startup::root_view_reason(result));
}
// Desktop GPU address of a title address inside a live aliased placed buffer that is tracked from COMMON and
// allows unordered access, or 0. Reads and writes through it reach the same memory the title sees, at the
// offset of the title address in the buffer; the extent is the rest of that buffer.
D3D12_GPU_VIRTUAL_ADDRESS placed_root_gpu(Wrapper* self, u64 address, bool writable)
{
if (!address) return 0;
const auto placed = placed_buffer_range(self->own().list_device, address, 1);
if (placed.reject || !placed.owner || !placed.alias) return 0;
const auto* info = resource_info(placed.owner);
if (!info || info->initial_state != D3D12_RESOURCE_STATE_COMMON) return 0;
const auto base = reinterpret_cast<u64>(placed.owner->own().title_memory);
if (!base || address < base) return 0;
const u64 offset = address - base;
if (offset % D3D12_RAW_UAV_SRV_BYTE_ALIGNMENT || !InterlockedCompareExchange(&placed.owner->own().refs, 0, 0)) return 0;
const D3D12_RESOURCE_DESC desc = placed.alias->GetDesc();
if (desc.Dimension != D3D12_RESOURCE_DIMENSION_BUFFER || offset >= desc.Width) return 0;
if (writable && !(desc.Flags & D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS)) return 0;
return placed.alias->GetGPUVirtualAddress() + offset;
}
// List slots 41 / 43 (desktop 37 / 39 + 4), SetComputeRootConstantBufferView / SetComputeRootShaderResourceView.
// A CBV never exceeds 64 KiB, so its bytes at the title address are copied to desktop memory and the copy is
// bound, as for slot 44. A root SRV has no such bound (a shader may read any offset of the buffer behind it) and
// the buffer extent is not known here, so an SRV is only recorded: a prefix copy would let reads past it return
// unrelated ring memory. The view is dropped from the store when no copy can be made.
void compute_read_view(Wrapper* self, UINT parameter, u64 address, unsigned wanted, const char* what)
{
auto* list = recording(self);
if (!list) return;
Pass& pass = pass_of(self);
const unsigned declared = declared_root_type(pass.compute_params, parameter);
const bool copies = wanted == x2w_startup::root_type_cbv;
const std::size_t bytes = (copies && address) ? readable_window(address, 0x10000) : 0;
// An SRV address inside this recording's own upload pages already has a desktop address: bind it directly.
// Any other SRV address is only recorded.
const UINT64 own_page = wanted == x2w_startup::root_type_srv ? list_upload_gpu(self, address, 1) : 0;
const bool direct = own_page && !(own_page % D3D12_RAW_UAV_SRV_BYTE_ALIGNMENT);
const u64 kept = copies ? (bytes ? address : 0) : address;
const auto result = pass.compute_views.set(pass.compute_root, declared, wanted, parameter, kept);
if (result != x2w_startup::RootViewResult::stored) return log_root_view_refusal(what, parameter, result);
if (direct) {
if (parameter < pass.desktop_compute_upload_srvs.size()) pass.desktop_compute_upload_srvs[parameter] = own_page;
list->SetComputeRootShaderResourceView(parameter, own_page);
return;
}
if (wanted == x2w_startup::root_type_srv) {
// A live placed buffer keeps its known offset and extent; any other address stays recorded only.
if (const auto gpu = placed_root_gpu(self, address, false)) list->SetComputeRootShaderResourceView(parameter, gpu);
return;
}
if (!bytes) return;
const D3D12_GPU_VIRTUAL_ADDRESS desktop = upload_from_title(address, bytes);
if (desktop) list->SetComputeRootConstantBufferView(parameter, desktop);
}
void __stdcall list_compute_root_cbv(Wrapper* self, UINT parameter, u64 address)
{
compute_read_view(self, parameter, address, x2w_startup::root_type_cbv, "compute root CBV");
}
void __stdcall list_compute_root_srv(Wrapper* self, UINT parameter, u64 address)
{
compute_read_view(self, parameter, address, x2w_startup::root_type_srv, "compute root SRV");
}
// List slots 45 / 46 (desktop 41 / 42 + 4), SetComputeRootUnorderedAccessView / SetGraphicsRootUnorderedAccessView.
// Always recorded. Compute is forwarded when the address lies in a live aliased placed buffer (placed_root_gpu);
// graphics UAVs are recorded only.
void __stdcall list_compute_root_uav(Wrapper* self, UINT parameter, u64 address)
{
if (!recording(self)) return;
Pass& pass = pass_of(self);
const auto result = pass.compute_views.set(pass.compute_root, declared_root_type(pass.compute_params, parameter),
x2w_startup::root_type_uav, parameter, address);
if (result != x2w_startup::RootViewResult::stored) return log_root_view_refusal("compute root UAV", parameter, result);
// Only a live placed buffer is forwarded; a copy would lose the shader's writes.
if (auto* list = recording(self))
if (const auto gpu = placed_root_gpu(self, address, true)) list->SetComputeRootUnorderedAccessView(parameter, gpu);
}
void __stdcall list_graphics_root_uav(Wrapper* self, UINT parameter, u64 address)
{
if (!recording(self)) return;
Pass& pass = pass_of(self);
const auto result = pass.graphics_uav.set(pass.root != nullptr, declared_root_type(pass.root, parameter),
x2w_startup::root_type_uav, parameter, address);
if (result != x2w_startup::RootViewResult::stored) log_root_view_refusal("graphics root UAV", parameter, result);
}
extern LONG translated_draws, untranslated_draws, frame_number;
bool draw_translated(Wrapper* self, bool indexed, UINT count, UINT instances, UINT start, INT base_vertex, UINT start_instance);
// List slot 12, DrawInstanced.
Expand Down Expand Up @@ -4709,9 +4792,12 @@ void build_tables()
list_typed[32] = reinterpret_cast<ListTyped>(&list_set_descriptor_heaps);
list_typed[33] = reinterpret_cast<ListTyped>(&list_compute_root);
list_typed[34] = reinterpret_cast<ListTyped>(&list_graphics_root);
list_typed[43] = reinterpret_cast<ListTyped>(&list_compute_upload_srv);
list_typed[44] = reinterpret_cast<ListTyped>(&list_root_srv);
list_typed[42] = reinterpret_cast<ListTyped>(&list_root_cbv);
list_typed[41] = reinterpret_cast<ListTyped>(&list_compute_root_cbv);
list_typed[43] = reinterpret_cast<ListTyped>(&list_compute_root_srv);
list_typed[45] = reinterpret_cast<ListTyped>(&list_compute_root_uav);
list_typed[46] = reinterpret_cast<ListTyped>(&list_graphics_root_uav);
list_typed[50] = reinterpret_cast<ListTyped>(&list_targets);
list_typed[13] = reinterpret_cast<ListTyped>(&list_draw_indexed);
list_typed[36] = reinterpret_cast<ListTyped>(&list_table);
Expand Down
58 changes: 58 additions & 0 deletions src/startup_stubs/root_view_store.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#pragma once

// Root descriptor views (CBV, SRV, UAV) a title sets on a command list, for the slots that carry them:
// compute CBV 41, compute SRV 43, compute UAV 45 and graphics UAV 46 (desktop 37, 39, 41, 42 plus four).
// Plain header with no Windows types so the host-only test can build it.
//
// A view is accepted only when a root signature of the matching kind is set, the parameter is below
// root_view_parameters and the root signature declares that parameter with the same view type. Anything
// else is refused and the store is left as it was. Setting a new root signature clears the store.

#include <cstdint>

namespace x2w_startup {

constexpr unsigned root_view_parameters = 16;

// D3D12_ROOT_PARAMETER_TYPE values.
constexpr unsigned root_type_cbv = 2;
constexpr unsigned root_type_srv = 3;
constexpr unsigned root_type_uav = 4;

enum class RootViewResult { stored, no_root, parameter_out_of_range, wrong_type };

inline const char* root_view_reason(RootViewResult result)
{
switch (result) {
case RootViewResult::stored: return "stored";
case RootViewResult::no_root: return "no root signature set";
case RootViewResult::parameter_out_of_range: return "parameter out of range";
case RootViewResult::wrong_type: return "parameter declared as another type";
}
return "unknown";
}

struct RootViewStore {
std::uint64_t address[root_view_parameters] = {}; // title address, 0 when never set or the address was unreadable
std::uint32_t bound = 0; // bit per parameter that holds a view

void clear() { *this = RootViewStore{}; }

// True when every parameter in required (one bit each) holds a view. A Dispatch that needs these roots
// must be recorded only when this holds.
bool covers(std::uint32_t required) const { return (bound & required) == required; }

// declared: the type the root signature gives parameter, or ~0u when it has no such parameter.
RootViewResult set(bool root_set, unsigned declared, unsigned wanted, unsigned parameter, std::uint64_t value)
{
if (!root_set) return RootViewResult::no_root;
if (parameter >= root_view_parameters || declared == ~0u) return RootViewResult::parameter_out_of_range;
if (declared != wanted) return RootViewResult::wrong_type;
address[parameter] = value;
if (value) bound |= 1u << parameter;
else bound &= ~(1u << parameter);
return RootViewResult::stored;
}
};

} // namespace x2w_startup
49 changes: 49 additions & 0 deletions tests/startup_root_view_store.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Host-only check of src/startup_stubs/root_view_store.h: which root view calls a list accepts and what the
// store holds afterwards.
#include "startup_stubs/root_view_store.h"

#include <cstdio>

using namespace x2w_startup;

namespace {
int failures;
void expect(bool ok, const char* what)
{
if (ok) return;
++failures;
std::printf("FAIL %s\n", what);
}
} // namespace

int main()
{
RootViewStore store;
expect(store.set(false, root_type_cbv, root_type_cbv, 0, 0x1000) == RootViewResult::no_root, "no root refused");
expect(store.bound == 0 && store.address[0] == 0, "no root leaves the store empty");

expect(store.set(true, root_type_cbv, root_type_cbv, 3, 0x2000) == RootViewResult::stored, "cbv stored");
expect(store.address[3] == 0x2000 && store.bound == (1u << 3), "cbv address and bit");

expect(store.set(true, root_type_srv, root_type_uav, 3, 0x3000) == RootViewResult::wrong_type, "uav on srv parameter refused");
expect(store.address[3] == 0x2000, "refused call keeps the old address");

expect(store.set(true, ~0u, root_type_srv, 5, 0x4000) == RootViewResult::parameter_out_of_range, "undeclared parameter refused");
expect(store.set(true, root_type_srv, root_type_srv, root_view_parameters, 0x4000) == RootViewResult::parameter_out_of_range,
"parameter 16 refused");
expect(store.set(true, root_type_srv, root_type_srv, 15, 0x5000) == RootViewResult::stored, "last parameter stored");
expect(store.bound == ((1u << 3) | (1u << 15)), "two bits set");

expect(store.set(true, root_type_cbv, root_type_cbv, 3, 0) == RootViewResult::stored, "zero address stored");
expect(store.bound == (1u << 15) && store.address[3] == 0, "zero address clears the bit");

expect(store.covers(1u << 15) && store.covers(0), "bound parameter covered");
expect(!store.covers((1u << 15) | (1u << 4)), "a missing root is not covered");

store.clear();
expect(store.bound == 0 && store.address[15] == 0, "clear empties the store");

if (failures) return 1;
std::printf("root view store ok\n");
return 0;
}
Loading