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
41 changes: 35 additions & 6 deletions src/startup_stubs/d3d12_compute_dispatch.inl
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ struct ComputeTranslation {
int state = 0; // 0 not loaded, 1 ready, 2 unavailable
x2w_startup::ManifestRetry manifest_retry; // absent and stale manifests can become ready later
std::vector<Slot> uds, srvs, uavs;
UINT heap_srvs = 0;
x2w_startup::TranslatedSamplers samplers; // static ones only
x2w_startup::ComPtr<ID3D12RootSignature> root;
x2w_startup::ComPtr<ID3D12PipelineState> pipeline;
Expand Down Expand Up @@ -92,7 +93,7 @@ const char* create_compute_pipeline(ComputeTranslation& tr, const void* code, st
{
// Parameter 0 keeps one constant for a program without entry values: a constants parameter cannot be empty.
if (FAILED(x2w_startup::create_translated_root(device_of(the_device), static_cast<UINT>(tr.uds.empty() ? 1 : tr.uds.size()),
static_cast<UINT>(tr.srvs.size()), tr.samplers, &tr.root, static_cast<UINT>(tr.uavs.size()))))
static_cast<UINT>(tr.srvs.size()), tr.samplers, &tr.root, static_cast<UINT>(tr.uavs.size()), tr.heap_srvs)))
return "root signature creation failed";
D3D12_COMPUTE_PIPELINE_STATE_DESC desc{};
desc.pRootSignature = tr.root.Get();
Expand Down Expand Up @@ -203,6 +204,11 @@ const char* load_compute_translation(ComputeTranslation& tr)
}
else if (std::sscanf(line.c_str(), "srv %u tex2d table %d %d%n", &i, &a, &b, &used) == 3 && whole() && i == tr.srvs.size())
tr.srvs.push_back({a, b, true});
else if (std::sscanf(line.c_str(), "resheap %u %u%n", &x, &y, &used) == 2 && whole()) {
if (tr.heap_srvs || !x || x > x2w_startup::max_translated_resheap || y != x2w_startup::translated_resheap_space)
return "a resource heap range outside the supported layout";
tr.heap_srvs = x;
}
// A static sampler, decoded as the draw path does: plain (smp), with the descriptor's LOD clamp (smpl) or a
// comparison one (smpc). Not modelled here: the run-time sampler heap table (smpheap).
else if (line.rfind("smpheap", 0) == 0) return "a sampler from the sampler heap";
Expand Down Expand Up @@ -403,7 +409,7 @@ const char* record_translated_dispatch(Wrapper* self, const TranslatedDispatch&
Wrapper* wrapper;
std::uint32_t words[4]; // a buffer's descriptor words as it was bound from, and its stride
UINT stride;
} views[40]{};
};
static_assert(sizeof(D3D12_SHADER_RESOURCE_VIEW_DESC) >= ViewRecord::srv_desc_bytes);
static_assert(sizeof(D3D12_UNORDERED_ACCESS_VIEW_DESC) >= ViewRecord::uav_desc_bytes);
const auto type = list->GetType();
Expand Down Expand Up @@ -437,6 +443,9 @@ const char* record_translated_dispatch(Wrapper* self, const TranslatedDispatch&
if (tr->uds[i].source == ComputeTranslation::root_constant && !pass.compute_constants.is_bound(tr->uds[i].a, tr->uds[i].b, 1))
reason = "an entry value was not set since the compute root";
const std::size_t inputs = reason ? 0 : tr->srvs.size(), count = reason ? 0 : inputs + tr->uavs.size();
std::vector<View> views(reason ? 0 : count + tr->heap_srvs);
if (!reason && dispatch.signature && tr->heap_srvs)
reason = "translated indirect Dispatch requires fixed-table raw buffers";
for (std::size_t i = 0; i < count && !reason; ++i) {
const bool output = i >= inputs;
const ComputeTranslation::Slot slot = output ? tr->uavs[i - inputs] : tr->srvs[i];
Expand Down Expand Up @@ -480,7 +489,8 @@ const char* record_translated_dispatch(Wrapper* self, const TranslatedDispatch&
}
// A heap record sits at the 32-bit byte offset the program computes, the index shifted left by 5, in the
// title memory of the list's view heap: a literal byte offset, as a table's is.
const u64 entry = slot.heap ? (slot.word_param >= 0 || slot.a >= 0 ? pass.title_view_heap.record(std::uint32_t{index << 5}, 16) : 0)
const u64 entry = slot.heap ? ((slot.word_param >= 0 || slot.a >= 0) && index < pass.title_view_heap_entries
? pass.title_view_heap.record(std::uint32_t{index << 5}, 16) : 0)
: slot.a < 0 || slot.b < 0 ? 0 : pass.compute_tables.table(slot.a).record(static_cast<u64>(slot.b), slot.texture ? 32 : 16);
const std::size_t entry_bytes = slot.texture ? 32 : 16;
if (!entry || !x2w_local::readable(reinterpret_cast<const void*>(entry), entry_bytes)) {
Expand Down Expand Up @@ -639,6 +649,25 @@ const char* record_translated_dispatch(Wrapper* self, const TranslatedDispatch&
views[i] = {placed.alias, offset, bytes, true};
std::memcpy(views[i].words, words, sizeof(views[i].words));
}
for (UINT r = 0; r < tr->heap_srvs && !reason; ++r) {
std::memset(words, 0, sizeof(words));
const u64 entry = r < pass.title_view_heap_entries ? pass.title_view_heap.record(u64{r} << 5, 16) : 0;
if (!entry || !x2w_local::readable(reinterpret_cast<const void*>(entry), 16)) {
reason = "a heap record is outside the list's view heap";
break;
}
std::memcpy(words, reinterpret_cast<const void*>(entry), 16);
const IndirectBufferView view = indirect_table_buffer(self, words, false, &reason, 16u << 20);
if (view.resource && view.offset % D3D12_RAW_UAV_SRV_BYTE_ALIGNMENT)
reason = "an input buffer range is not 16-byte aligned in its resource";
if (reason && !tr->first_refusal)
log("x2w-draw compute %016llx first refused heap record %u, descriptor at 0x%llx", key, r, entry);
for (std::size_t output = inputs; output < count && !reason; ++output)
if (views[output].resource == view.resource) reason = "an output buffer is also an input";
views[count + r] = {view.resource, view.offset, view.bytes, !view.upload};
views[count + r].stride = view.stride;
std::memcpy(views[count + r].words, words, sizeof(views[count + r].words));
}
if (dispatch.argument)
for (std::size_t i = 0; i < count && !reason; ++i)
if (views[i].resource == dispatch.argument) reason = "the indirect argument buffer is also a bound shader resource";
Expand Down Expand Up @@ -684,14 +713,14 @@ const char* record_translated_dispatch(Wrapper* self, const TranslatedDispatch&
log("x2w-draw compute %016llx Dispatch(%u, %u, %u) refused: %s", key, x, y, z, reason);
return reason;
}
const UINT n = static_cast<UINT>(count);
const UINT n = static_cast<UINT>(count + tr->heap_srvs);
UINT first = static_cast<UINT>(InterlockedExchangeAdd(&draw_heap_cursor, static_cast<LONG>(n))) % draw_heap_size;
if (first + n > draw_heap_size) {
first = 0;
InterlockedExchange(&draw_heap_cursor, static_cast<LONG>(n));
}
D3D12_CPU_DESCRIPTOR_HANDLE dest{draw_heap->GetCPUDescriptorHandleForHeapStart().ptr + std::size_t{first} * draw_heap_increment};
for (std::size_t i = 0; i < count; ++i, dest.ptr += draw_heap_increment) {
for (std::size_t i = 0; i < n; ++i, dest.ptr += draw_heap_increment) {
const View& view = views[i];
if (i < inputs && view.texture) {
// A compute list accepts no pixel-shader or depth state in a transition.
Expand All @@ -701,7 +730,7 @@ const char* record_translated_dispatch(Wrapper* self, const TranslatedDispatch&
view.described ? &view.desc.srv : nullptr, dest);
continue;
}
if (i < inputs) {
if (i < inputs || i >= count) {
if (view.transition) require_state(self, view.resource, D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE);
buffer_view(dest, view.resource, view.offset, static_cast<std::size_t>(view.bytes));
continue;
Expand Down
6 changes: 5 additions & 1 deletion src/startup_stubs/d3d12_draw.inl
Original file line number Diff line number Diff line change
Expand Up @@ -3337,6 +3337,7 @@ void __stdcall list_set_descriptor_heaps(Wrapper* self, UINT count, void* const*
x2w_startup::SamplerSource source;
u64 sampler_wrapper = 0;
x2w_startup::ViewTable heap_records;
UINT heap_entries = 0;
x2w_startup::ViewHeapSelection view_heaps;
if (count && count <= 4 && x2w_local::readable(heaps, std::size_t{count} * sizeof(*heaps))) {
AcquireSRWLockShared(&note_lock);
Expand All @@ -3346,9 +3347,11 @@ void __stdcall list_set_descriptor_heaps(Wrapper* self, UINT count, void* const*
heap.type == D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV)
view_heaps.note(heap.wrapper, heap.desktop_gpu != 0, heap.logical_count, heap.count);
if (heap.wrapper == reinterpret_cast<u64>(heaps[i]) &&
heap.type == D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV)
heap.type == D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV) {
heap_records = x2w_startup::resolve_view_table(heap.desktop_gpu, heap.title_cpu, heap.logical_count,
heap.increment, heap.desktop_gpu);
if (heap_records.address) heap_entries = heap.logical_count;
}
if (heap.wrapper == reinterpret_cast<u64>(heaps[i]) &&
heap.type == D3D12_DESCRIPTOR_HEAP_TYPE_SAMPLER && heap.desktop_gpu) {
sampler_wrapper = heap.wrapper;
Expand All @@ -3367,6 +3370,7 @@ void __stdcall list_set_descriptor_heaps(Wrapper* self, UINT count, void* const*
pass.title_samplers = source;
pass.sampler_heap_wrapper = sampler_wrapper;
pass.title_view_heap = heap_records;
pass.title_view_heap_entries = heap_entries;
}

// The table at a title GPU handle, looked up in the title's view heap (the one set by slot 32 when known).
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 @@ -2883,6 +2883,7 @@ struct Pass {
x2w_startup::ComputeTables compute_tables; // slot 35, read by a Dispatch handler
u64 source_view_heap = 0;
x2w_startup::ViewTable title_view_heap; // slot 32: the title memory of the list's view heap (heap records)
UINT title_view_heap_entries = 0; // logical descriptor count of title_view_heap (indices 0..count-1)
u64 root_address[16] = {}; // title address of each root descriptor
std::uint32_t constants[16][8] = {};
unsigned char constant_words_bound[16] = {};
Expand Down
24 changes: 16 additions & 8 deletions src/startup_stubs/translated_sampler_bindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ constexpr UINT translated_sampler_count = D3D12_MAX_SHADER_VISIBLE_SAMPLER_HEAP_
constexpr UINT translated_sampler_space = 1;
constexpr UINT translated_sampler_fallback = translated_sampler_count - 1;
constexpr UINT title_sampler_stride = sizeof(SamplerWords);
constexpr UINT translated_resheap_space = 2;
constexpr UINT max_translated_resheap = 1024;

// comparison: the sampler is read through a SamplerComparisonState (manifest line smpc). SQ_IMG_SAMP_WORD0 bits
// 14:12 hold SQ_TEX_DEPTH_COMPARE, which lists NEVER..ALWAYS in the order of D3D12_COMPARISON_FUNC minus one.
Expand Down Expand Up @@ -115,22 +117,28 @@ inline std::vector<D3D12_STATIC_SAMPLER_DESC> decode_statics(const TranslatedSam
// views: the SRVs and UAVs of the view table together.
inline UINT sampler_root_parameter(UINT views) { return views ? 2u : 1u; }

// Parameter 1, when there are views, is one table: SRVs t0.. first, then UAVs u0.. right after them.
// Parameter 1, when there are views, is one table: SRVs t0.. first, then UAVs u0.., then bounded resheap SRVs (space 2).
inline HRESULT create_translated_root(ID3D12Device* device, UINT constants, UINT srvs,
const TranslatedSamplers& samplers, ID3D12RootSignature** out, UINT uavs = 0)
const TranslatedSamplers& samplers, ID3D12RootSignature** out, UINT uavs = 0,
UINT heap_srvs = 0)
{
D3D12_ROOT_PARAMETER parameters[3]{};
parameters[0].ParameterType = D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS;
parameters[0].Constants.Num32BitValues = constants;
// A compute program's outputs (u0..) follow its resources in the same table.
const D3D12_DESCRIPTOR_RANGE view_ranges[2]{{D3D12_DESCRIPTOR_RANGE_TYPE_SRV, srvs, 0, 0, 0},
{D3D12_DESCRIPTOR_RANGE_TYPE_UAV, uavs, 0, 0, srvs}};
// A compute program's outputs (u0..) follow its resources in the same table; bounded heap SRVs follow in space 2.
D3D12_DESCRIPTOR_RANGE view_ranges[3]{};
UINT range_count = 0;
if (srvs) view_ranges[range_count++] = {D3D12_DESCRIPTOR_RANGE_TYPE_SRV, srvs, 0, 0, 0};
if (uavs) view_ranges[range_count++] = {D3D12_DESCRIPTOR_RANGE_TYPE_UAV, uavs, 0, 0, srvs};
if (heap_srvs) {
view_ranges[range_count++] = {
D3D12_DESCRIPTOR_RANGE_TYPE_SRV, heap_srvs, 0, translated_resheap_space, srvs + uavs};
}
parameters[1].ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE;
// A range of no descriptors does not serialize: a program without resources starts the table at its outputs.
parameters[1].DescriptorTable = {(srvs ? 1u : 0u) + (uavs ? 1u : 0u), srvs ? view_ranges : view_ranges + 1};
parameters[1].DescriptorTable = {range_count, view_ranges};
const D3D12_DESCRIPTOR_RANGE sampler_range{
D3D12_DESCRIPTOR_RANGE_TYPE_SAMPLER, translated_sampler_count, 0, translated_sampler_space, 0};
UINT count = sampler_root_parameter(srvs + uavs);
UINT count = sampler_root_parameter(srvs + uavs + heap_srvs);
if (samplers.heap) {
parameters[count].ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE;
parameters[count].DescriptorTable = {1, &sampler_range};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tests/startup_compute_waterfall_heap_check.py
12 changes: 12 additions & 0 deletions tests/cmake/startup_compute_waterfall_heap.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
if(EXISTS "${INTAKE_PYTHON}" AND TARGET startup_stream_consumer AND TARGET offscreen_check)
foreach(adapter IN ITEMS warp hardware)
add_test(NAME startup_compute_waterfall_heap_oracle_${adapter} COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_compute_waterfall_heap_check.py"
--oracle "${DXC_EXECUTABLE}" "$<TARGET_FILE:offscreen_check>" ${adapter})
set_tests_properties(startup_compute_waterfall_heap_oracle_${adapter} PROPERTIES TIMEOUT 90 SKIP_RETURN_CODE 77)
add_test(NAME startup_compute_waterfall_heap_dispatch_${adapter} COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_compute_waterfall_heap_check.py"
--dispatch "$<TARGET_FILE:startup_stream_consumer>" "${DXC_EXECUTABLE}" ${adapter})
set_tests_properties(startup_compute_waterfall_heap_dispatch_${adapter} PROPERTIES TIMEOUT 120 SKIP_RETURN_CODE 77)
endforeach()
endif()
Loading
Loading