Skip to content
Open
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
6 changes: 5 additions & 1 deletion docs/experiments/compute-output-buffer-reasons.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,11 @@ byte 32, as a title copies descriptors.
A native slot 67 request with a `half_buffer_uav` now publishes
`uav <register> bufhalf table <parameter> <byte> bytes <stride>`. The
stride selects a typed buffer view of `R16_FLOAT`, `R16G16_FLOAT` or
`R16G16B16A16_FLOAT` for 2, 4 or 8 bytes respectively. The runtime
`R16G16B16A16_FLOAT` for 2, 4 or 8 bytes respectively. A one-component
D16 store is `packed_d16_x_uav` instead: it publishes
`uav <register> packed_d16_x table <parameter> <byte>`, and the runtime
binds `R16_FLOAT` and `R16_UINT` as `R16_UINT` and `R32_UINT` as
`R32_UINT`. The half view
requires the matching gfx10 buffer format, identity selects over the
written components and structured out-of-bounds mode. It binds the
descriptor's record count and element-aligned offset into an aliased
Expand Down
44 changes: 44 additions & 0 deletions docs/experiments/packed-d16-x-uav-manifest.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Published manifest for a one-component packed D16 buffer store

A direct compute `buffer_store_format` with the D16 bit and one component,
including the same store reached through an MTBUF opcode, is
`packed_d16_x_uav`. The integer store is `value & 65535` then
`bufferStore.i32`. `format_compute_runtime_manifest` publishes

`uav <register> packed_d16_x table <parameter> <byte>`

when that binding is one register and the payload is two bytes.
`load_compute_translation` reads the line. The placed buffer view is checked
with `native_buffer_store_view`: `R16_FLOAT` and `R16_UINT` are bound as
`R16_UINT`, and `R32_UINT` stays `R32_UINT` with a 4-byte element. A
descriptor that matches none of those keeps the refusal
`a half buffer store requires matching format, stride and structured bounds`.

Two- and four-component D16 stores stay `half_buffer_uav` and publish
`bufhalf`.

The source-built fixture is `tests/startup_compute_packed_d16_x_uav_check.py`.
It has no title data. Thirteen lanes write `0x3c00 + lane` at byte 8. An
`R32_UINT` view writes those values zero-extended to dwords. A stride other
than the element size, and a descriptor with the swizzle flag set, must refuse
by the name above and leave the buffer untouched. Before the manifest exists
the dispatch is `no translation`. The one-channel half-buffer fixture, MUBUF
and both MTBUF forms, expects the same manifest line.

On Linux the reference half of the fixture runs with no compiler and no device:

```bash
./out/pe-env/bin/python -B tests/startup_compute_packed_d16_x_uav_check.py
```

The compile and dispatch halves need Windows, DXC and the stand-in.

```powershell
ctest --preset dev --no-tests=error -R 'native_compute_manifest_reasons|startup_compute_half_buffer_uav.*|native_compile_dxil.*|.*packed_d16.*|.*store_d16_x.*' -V
```

A 900 s start-up run of a retail title on the Windows test PC, runtime built from `main`
`aec2e08`, counted 23 shaders and 86,436 untranslated dispatches (5.7% of that run's
untranslated compute dispatches) whose published-route refusal was
`unsupported startup runtime manifest bindings: UAV kind unknown` for this binding.
Those counts are from that run. Windows results for this manifest line are not in this note.
8 changes: 7 additions & 1 deletion src/direct_compute_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -893,7 +893,10 @@ static const char *view_kind_name(ComputeViewKind kind) {
"heap_stride_srv", "raw_buffer_uav", "typed_buffer_uav", "half_buffer_uav", "root_buffer_srv",
"comparison_texture_srv", "half2_texture_uav", "half4_texture_uav", "cube_sample_srv", "cube_comparison_srv",
"indexed_atomic_uav", "d16_sample_srv", "byte_window_srv", "heap_buffer_srv", "heap_sampler",
"uint_texture_uav", "float_texture_uav", "float2_texture_uav", "float3_texture_uav"};
"uint_texture_uav", "float_texture_uav", "float2_texture_uav", "float3_texture_uav", "packed_d16_x_uav",
"indexed_raw_srv"};
// ponytail: catches a renamed last kind, not one appended after it; extend both together.
static_assert(std::size(names) == static_cast<size_t>(ComputeViewKind::indexed_raw_srv) + 1);
const auto index = static_cast<size_t>(kind);
return index < std::size(names) ? names[index] : "unknown";
}
Expand Down Expand Up @@ -979,6 +982,9 @@ std::optional<std::string> format_compute_runtime_manifest(const NativeComputeSh
(b.required_bytes == 2 || b.required_bytes == 4 || b.required_bytes == 8)) {
uav_lines << "uav " << b.shader_register << " bufhalf table " << b.root_parameter << ' '
<< b.byte_offset << " bytes " << b.required_bytes << '\n';
} else if (b.kind == ComputeViewKind::packed_d16_x_uav && b.array_count == 1 && b.required_bytes == 2) {
uav_lines << "uav " << b.shader_register << " packed_d16_x table " << b.root_parameter << ' '
<< b.byte_offset << '\n';
} else if (b.kind == ComputeViewKind::uint_texture_uav && b.image_dimension == 1 && b.array_count == 1) {
uav_lines << "uav " << b.shader_register << " tex2d uint table " << b.root_parameter << ' '
<< b.byte_offset << " channels 1\n";
Expand Down
85 changes: 79 additions & 6 deletions src/startup_stubs/d3d12_compute_dispatch.inl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
// the key of its published request; gfx2hlsl.py --pending answers the request with <key>.txt (`stage cs`) and
// <key>.cs.cso. Bound from the compute tables recorded by slot 35 and compute root SRVs recorded by slot 43: raw
// buffers (`srv <i> buf table <param> <byte>` and `srv <i> buf root <param>` inputs, `uav <i> table <param> <byte>`
// outputs, or `uav <i> bufhalf table <param> <byte> bytes <stride>` for typed half stores)
// outputs, `uav <i> bufhalf table <param> <byte> bytes <stride>` for typed half stores, or
// `uav <i> packed_d16_x table <param> <byte>` for a one-component packed D16 store)
// named by hardware buffer descriptors or 64-bit root SRV addresses, and textures
// (`srv <i> tex1d|tex2d|tex2da|tex3d table <param> <byte>` inputs, `uav <i> tex1d/tex2d/tex2da/tex3d float table <param> <byte> channels <n>` outputs) named
// by the token of a view the title created (device slots 18 and 19). `smp`, `smpl` and `smpc` lines become static samplers. Entry
Expand Down Expand Up @@ -92,6 +93,49 @@ unsigned float_output_channels(DXGI_FORMAT format)
}
}

// Published `packed_d16_x` stores one low halfword. native_buffer_store_view accepts R16_FLOAT and
// R16_UINT (both rewritten to R16_UINT) and R32_UINT (stride widened to 4). A descriptor that is
// none of those keeps the half-buffer refusal the one-channel fixture already checks.
const char* packed_d16_x_view_refusal(const std::uint32_t* words, UINT64 offset, UINT64 resource_bytes,
D3D12_UNORDERED_ACCESS_VIEW_DESC& out)
{
D3D12_UNORDERED_ACCESS_VIEW_DESC view{};
view.ViewDimension = D3D12_UAV_DIMENSION_BUFFER;
const unsigned stride_field = words[1] >> 16;
const unsigned stride = stride_field & 0x3fffu;
const unsigned word3 = words[3];
const unsigned format = (word3 >> 12) & 127u;
const bool unified = (word3 & 0x01000000u) != 0;
unsigned element = 0;
if (unified && format == 13) {
view.Format = DXGI_FORMAT_R16_FLOAT;
element = 2;
} else if (unified && format == 11) {
view.Format = DXGI_FORMAT_R16_UINT;
element = 2;
} else if (unified && format == 20) {
view.Format = DXGI_FORMAT_R32_UINT;
element = 4;
}
// TYPE, reserved, ADD_TID_ENABLE and INDEX_STRIDE, plus SWIZZLE_ENABLE / CACHE_SWIZZLE and OOB_SELECT.
constexpr unsigned word3_flags = 0xcef80000u;
const bool flags = (stride_field & 0xc000u) != 0 || (word3 & word3_flags) != 0 || ((word3 >> 28) & 3u) != 0;
if (flags)
view.Buffer.Flags = D3D12_BUFFER_UAV_FLAG_RAW;
if (!element || (word3 & 7u) != 4u || stride != element)
view.Format = DXGI_FORMAT_UNKNOWN;
const unsigned step = element ? element : 1u;
if (offset % step)
return "an output packed D16 buffer is not element-aligned in its resource";
view.Buffer.FirstElement = static_cast<UINT>(offset / step);
view.Buffer.NumElements = words[2] ? words[2] : 1u;
const x2w::ComputeViewBinding binding{x2w::ComputeViewKind::packed_d16_x_uav, 0, 0, 0, 2};
if (!x2w::native_buffer_store_view(binding, view, resource_bytes))
return "a half buffer store requires matching format, stride and structured bounds";
out = view;
return nullptr;
}

bool half_buffer_descriptor_matches(const std::uint32_t* words, unsigned bytes)
{
// gfx10 V# with identity selectors for each stored component and structured
Expand Down Expand Up @@ -223,6 +267,11 @@ const char* load_compute_translation(ComputeTranslation& tr)
tr.uavs.push_back({a, b});
tr.uavs.back().half_buffer_bytes = x;
}
else if (std::sscanf(line.c_str(), "uav %u packed_d16_x table %d %d%n", &i, &a, &b, &used) == 3 &&
whole() && i == tr.uavs.size()) {
tr.uavs.push_back({a, b});
tr.uavs.back().packed_d16_x = true;
}
else if (std::sscanf(line.c_str(), "uav %u tex2d uint table %d %d channels 1%n", &i, &a, &b, &used) == 3 &&
whole() && i == tr.uavs.size()) {
tr.uavs.push_back({a, b, true});
Expand Down Expand Up @@ -823,6 +872,12 @@ const char* record_translated_dispatch(Wrapper* self, const TranslatedDispatch&
reason = "formatted D16 write has an incompatible typed buffer UAV";
}
#endif
if (!reason && slot.packed_d16_x && !slot.half_typed_bytes) {
token.view.Format = desktop_format(token.view.Format);
const x2w::ComputeViewBinding binding{x2w::ComputeViewKind::packed_d16_x_uav, 0, 0, 0, 2};
if (!x2w::native_buffer_store_view(binding, token.view, token.backing.Width))
reason = "formatted D16 write has an incompatible typed buffer UAV";
}
if (reason && token.placement == Placement::copied_buffer &&
trace_copied_compute_output(key, token.guest_resource, token.guest_base, token.offset, token.bytes)) {
log("x2w-compute-output-watch key=%016llx groups=%u,%u,%u indirect=%u owner=0x%llx gpuva=0x%llx",
Expand Down Expand Up @@ -852,7 +907,7 @@ const char* record_translated_dispatch(Wrapper* self, const TranslatedDispatch&
token.description_bytes, token.offset, token.bytes, reason ? reason : "bound");
}
views[i] = {token.resource, token.offset, token.bytes, true};
if (!reason && slot.half_typed_bytes) {
if (!reason && (slot.half_typed_bytes || slot.packed_d16_x)) {
views[i].described = true;
views[i].desc.uav = token.view;
}
Expand All @@ -866,9 +921,10 @@ const char* record_translated_dispatch(Wrapper* self, const TranslatedDispatch&
const u64 address = words[0] | (u64{words[1] & 0xffff} << 32);
const UINT stride = (words[1] >> 16) & 0x3fff;
const unsigned half_bytes = slot.half_buffer_bytes;
const unsigned alignment = half_bytes ? half_bytes : D3D12_RAW_UAV_SRV_BYTE_ALIGNMENT;
const bool packed = slot.packed_d16_x && !slot.half_typed_bytes;
const unsigned alignment = half_bytes ? half_bytes : (packed ? 2u : D3D12_RAW_UAV_SRV_BYTE_ALIGNMENT);
const u64 bytes = u64{stride ? stride : 1} * words[2];
const bool sized = words[0] != view_token_magic && bytes && (half_bytes || bytes % 4 == 0) && bytes <= (16u << 20);
const bool sized = words[0] != view_token_magic && bytes && (half_bytes || packed || bytes % 4 == 0) && bytes <= (16u << 20);
const auto placed = sized ? placed_buffer_range(self->own().list_device, address, bytes) : PlacedVertexView{};
const ResourceInfo* info = placed.alias ? resource_info(placed.owner) : nullptr;
const u64 offset = placed.alias ? address - reinterpret_cast<u64>(placed.owner->own().title_memory) : 0;
Expand All @@ -877,18 +933,23 @@ const char* record_translated_dispatch(Wrapper* self, const TranslatedDispatch&
reason = "a half buffer store requires matching format, stride and structured bounds";
else if (!bytes) reason = "an output buffer descriptor has zero records";
else if (!sized) reason = half_bytes ? "an output half buffer range is over 16 MiB"
: "an output buffer range is over 16 MiB or not whole dwords";
: packed ? "an output packed D16 buffer range is over 16 MiB"
: "an output buffer range is over 16 MiB or not whole dwords";
else if (placed.reject) reason = "an output range crosses the end of a placed buffer";
else if (!placed.owner) reason = "an output is in title memory outside any placed buffer";
else if (!placed.alias) reason = "an output is in a placed buffer that does not alias title memory";
else if (!info || info->initial_state != D3D12_RESOURCE_STATE_COMMON)
reason = "an output buffer's resource was not created in the common state";
else if (offset % alignment) reason = half_bytes ? "an output half buffer is not element-aligned in its resource"
: "an output buffer range is not 16-byte aligned in its resource";
: packed ? "an output packed D16 buffer is not element-aligned in its resource"
: "an output buffer range is not 16-byte aligned in its resource";
else if (!(placed.alias->GetDesc().Flags & D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS))
reason = "an output buffer has no unordered access";
for (std::size_t input = 0; input < inputs && !reason; ++input)
if (views[input].resource == placed.alias) reason = "an output buffer is also an input";
D3D12_UNORDERED_ACCESS_VIEW_DESC packed_desc{};
if (!reason && packed)
reason = packed_d16_x_view_refusal(words, offset, placed.alias->GetDesc().Width, packed_desc);
if (reason && !tr->first_refusal)
log("x2w-draw compute raw UAV %016llx table=%d+%d entry=0x%llx words=%08x/%08x/%08x/%08x "
"stride=%u bytes=%llu result=%s", key, slot.a, slot.b, entry, words[0], words[1], words[2], words[3],
Expand All @@ -912,6 +973,18 @@ const char* record_translated_dispatch(Wrapper* self, const TranslatedDispatch&
views[i].desc.uav = desc;
}
}
if (!reason && packed) {
D3D12_FEATURE_DATA_FORMAT_SUPPORT support{packed_desc.Format};
constexpr UINT required = D3D12_FORMAT_SUPPORT1_BUFFER | D3D12_FORMAT_SUPPORT1_TYPED_UNORDERED_ACCESS_VIEW;
if (FAILED(device_of(the_device)->CheckFeatureSupport(D3D12_FEATURE_FORMAT_SUPPORT, &support, sizeof(support))) ||
(support.Support1 & required) != required ||
!(support.Support2 & D3D12_FORMAT_SUPPORT2_UAV_TYPED_STORE)) {
reason = "an output packed D16 buffer format does not support typed UAV stores";
} else {
views[i].described = true;
views[i].desc.uav = packed_desc;
}
}
std::memcpy(views[i].words, words, sizeof(views[i].words));
}
for (UINT r = 0; r < tr->heap_srvs && !reason; ++r) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tests/startup_compute_packed_d16_x_uav_check.py
22 changes: 22 additions & 0 deletions tests/cmake/startup_compute_packed_d16_x_uav.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
if(EXISTS "${INTAKE_PYTHON}")
add_test(NAME startup_compute_packed_d16_x_uav_reference COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_compute_packed_d16_x_uav_check.py" reference)
set_tests_properties(startup_compute_packed_d16_x_uav_reference PROPERTIES TIMEOUT 60)
endif()
if(TARGET compile_dxil AND EXISTS "${INTAKE_PYTHON}"
AND EXISTS "${DXC_BIN_DIR}/dxcompiler.dll" AND EXISTS "${DXC_BIN_DIR}/dxil.dll")
add_test(NAME startup_compute_packed_d16_x_uav_controls COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_compute_packed_d16_x_uav_check.py" compile
"$<TARGET_FILE:compile_dxil>" "${DXC_BIN_DIR}")
set_tests_properties(startup_compute_packed_d16_x_uav_controls PROPERTIES TIMEOUT 60)
endif()
if(TARGET compile_dxil AND TARGET startup_stream_consumer AND TARGET offscreen_check AND EXISTS "${INTAKE_PYTHON}"
AND EXISTS "${DXC_BIN_DIR}/dxcompiler.dll" AND EXISTS "${DXC_BIN_DIR}/dxil.dll")
foreach(adapter IN ITEMS warp hardware)
add_test(NAME startup_compute_packed_d16_x_uav_${adapter} COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_compute_packed_d16_x_uav_check.py"
"$<TARGET_FILE:compile_dxil>" "$<TARGET_FILE:startup_stream_consumer>" "$<TARGET_FILE:offscreen_check>"
"${DXC_BIN_DIR}" ${adapter})
set_tests_properties(startup_compute_packed_d16_x_uav_${adapter} PROPERTIES TIMEOUT 120 SKIP_RETURN_CODE 77)
endforeach()
endif()
21 changes: 20 additions & 1 deletion tests/native_compute_manifest_reasons.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ int main() {
p.bindings[0].array_count = 4;
}},
{"UAV kind typed_buffer_uav", [](auto &p) { p.bindings[1].kind = K::typed_buffer_uav; }},
{"UAV kind packed_d16_x_uav", [](auto &p) {
p.bindings[1].kind = K::packed_d16_x_uav;
p.bindings[1].required_bytes = 4;
}},
{"UAV kind texture_uav cube", [](auto &p) {
p.bindings[1].kind = K::texture_uav;
p.bindings[1].image_dimension = 3;
Expand Down Expand Up @@ -117,6 +121,21 @@ int main() {
std::printf(" | got: %s\n", manifest ? manifest->c_str() : reason.c_str());
failures += !ok;
}
std::printf("%d of %zu manifest reasons wrong\n", failures, std::size(cases) + std::size(published) + 1);
{
auto program = base();
program.bindings[1].kind = K::packed_d16_x_uav;
program.bindings[1].required_bytes = 2;
reason = "unset";
const auto manifest = format_compute_runtime_manifest(program, &reason);
const char *line = "uav 0 packed_d16_x table 1 32\n";
std::string wanted = expected;
wanted.replace(wanted.find("uav 0 table 1 32\n"), std::strlen("uav 0 table 1 32\n"), line);
const bool ok = manifest && *manifest == wanted && reason == "unset";
std::printf("%s publishes %s", ok ? "ok " : "FAIL", line);
if (!ok)
std::printf(" | got: %s\n", manifest ? manifest->c_str() : reason.c_str());
failures += !ok;
}
std::printf("%d of %zu manifest reasons wrong\n", failures, std::size(cases) + std::size(published) + 2);
return failures ? 1 : 0;
}
6 changes: 5 additions & 1 deletion tests/startup_compute_half_buffer_uav_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,11 @@ def publish(compile_dxil, sdk, cache, key, stride):
manifest = cache / f'{key}.txt'
assert result.returncode == 0 and manifest.is_file(), (result.stdout, result.stderr)
text = manifest.read_text()
assert f'uav 0 bufhalf table 1 32 bytes {stride}' in text.splitlines(), text
# One component is packed_d16_x_uav. Wider D16 stores stay on the half-buffer line.
line = 'uav 0 packed_d16_x table 1 32' if stride == 2 else f'uav 0 bufhalf table 1 32 bytes {stride}'
assert line in text.splitlines(), text
if stride == 2:
assert f'uav 0 bufhalf table 1 32 bytes {stride}' not in text.splitlines(), text
assert not (cache / f'{key}.cs.hlsl').exists(), 'expected native publication'


Expand Down
Loading
Loading