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
36 changes: 36 additions & 0 deletions docs/experiments/compute-output-buffer-reasons.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,42 @@ the 12 translated keys, and the first refused dispatch of each of the other 11,
of a desktop resource. That run made 2,453,727 compute dispatches, so dispatch counts compare only within one run.
Whether the 12 translated keys write the right values was not checked.

## Slot-19 output view intervals

A slot-19 output token (#3094) names a buffer UAV. The byte interval of that view was refused with one message,
`an output UAV interval is unaligned, oversized or outside its buffer`. It is now split into the condition that
failed, checked in this order:

1. `an output UAV interval is empty`
2. `an output UAV interval is not whole dwords`
3. `an output UAV interval is over 16 MiB`
4. `an output UAV interval is not 16-byte aligned in its buffer`
5. `an output UAV interval starts past its buffer`
6. `an output UAV interval crosses the end of its buffer`

Only the reason changes. The once-per-key `x2w-draw compute UAV` line already logs the view's format, first
element, element count, stride, flags, backing width, byte offset and byte count.

`tests/startup_compute_token_interval_check.py` publishes case A with 32-byte table records through
`gfx2hlsl.py --pending` and binds a raw slot-19 view at byte 16 of the placed output buffer. Words 4-315 match.
Conditions 2, 3 and 4 then each come from one replaced view description (an R16_UINT view of one element, a raw
view of 16 MiB + 16 bytes of a committed buffer 64 KiB over 16 MiB, an R32_UINT view at byte 4), and each dispatch
refuses with its own reason in the log and the census and leaves every word at its sentinel. Conditions 1, 5 and 6
need a view that D3D12 rejects when it is created: with the debug layer on, the device is removed at
`CreateUnorderedAccessView`, so no fixture creates one.

```powershell
ctest --preset dev --no-tests=error -R "^startup_compute_token_interval_(warp|hardware)$" -V
```

Both tests pass on WARP (10.0.26100.9818) and on an RTX 4090 (driver 32.0.16.1088), Windows 11 build 26300,
debug layer on. With only the split reverted, the bound view still matches and the three cases are still refused,
each with the old reason, so all three expectations fail.

On WARP, byte 32 of the consumer's four-descriptor heap is not a descriptor: a view created there ends the
consumer with exit 2173. The `dispatch-uav-token-*` modes therefore create the view at byte 64 and copy its token to
byte 32, as a title copies descriptors.

## Published half-width buffer stores

A native slot 67 request with a `half_buffer_uav` now publishes
Expand Down
11 changes: 7 additions & 4 deletions src/startup_stubs/d3d12_compute_dispatch.inl
Original file line number Diff line number Diff line change
Expand Up @@ -514,10 +514,13 @@ const char* compute_token_output(Wrapper* list, const std::uint32_t* words, Comp
return "an output UAV has no supported byte interval";
output.offset = view.FirstElement * element;
output.bytes = u64{view.NumElements} * element;
if (!output.bytes || output.bytes % 4 || output.bytes > (16u << 20) ||
output.offset % D3D12_RAW_UAV_SRV_BYTE_ALIGNMENT || output.offset > output.backing.Width ||
output.bytes > output.backing.Width - output.offset)
return "an output UAV interval is unaligned, oversized or outside its buffer";
// Each refused interval has its own reason. The width test comes first: the end test subtracts the offset.
if (!output.bytes) return "an output UAV interval is empty";
if (output.bytes % 4) return "an output UAV interval is not whole dwords";
if (output.bytes > (16u << 20)) return "an output UAV interval is over 16 MiB";
if (output.offset % D3D12_RAW_UAV_SRV_BYTE_ALIGNMENT) return "an output UAV interval is not 16-byte aligned in its buffer";
if (output.offset > output.backing.Width) return "an output UAV interval starts past its buffer";
if (output.bytes > output.backing.Width - output.offset) return "an output UAV interval crosses the end of its buffer";
const auto base = reinterpret_cast<u64>(owner->own().title_memory);
if (base) {
if (output.offset > UINT64_MAX - base) return "an output UAV placement address overflows";
Expand Down
8 changes: 8 additions & 0 deletions tests/cmake/startup_compute_token_interval.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
if(EXISTS "${INTAKE_PYTHON}" AND TARGET startup_stream_consumer)
foreach(adapter IN ITEMS warp hardware)
add_test(NAME startup_compute_token_interval_${adapter} COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_compute_token_interval_check.py" "$<TARGET_FILE:startup_stream_consumer>"
"${DXC_EXECUTABLE}" ${adapter})
set_tests_properties(startup_compute_token_interval_${adapter} PROPERTIES TIMEOUT 120 SKIP_RETURN_CODE 77)
endforeach()
endif()
90 changes: 90 additions & 0 deletions tests/startup_compute_token_interval_check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
"""Named reasons for a slot-19 output view whose byte interval the translated dispatch refuses.

usage: startup_compute_token_interval_check.py <startup_stream_consumer> <dxc> <warp|hardware>

The program is case A of startup_gfx2hlsl_compute_check.py with its two buffer descriptors loaded from 32-byte table
records, as in startup_compute_dispatch_native_check.py. The consumer's dispatch-uav-token-raw mode writes, at table
byte 32, the token of a raw slot-19 view of the 1280-byte placed output buffer: first element 4, 312 elements. After
gfx2hlsl.py --pending with DXC that view is bound and words 4-315 match the case's expectation. Then the view is
replaced through X2W_TEST_DISPATCH_UAV, one condition at a time, and each dispatch is refused with its own reason in
the log and the census and writes no output word:

- an R16_UINT view of one element (2 bytes) at byte 16;
- a raw view of 4 Mi + 4 elements (16 MiB + 16 bytes) of a committed buffer 64 KiB over 16 MiB;
- an R32_UINT view at byte 4, inside the buffer.

The other three reasons (an empty interval, one that starts past the buffer and one that crosses its end) need a
view that D3D12 itself rejects when it is created; with the debug layer on, that removes the device. Exit 77 when
the adapter is not available.
"""
from pathlib import Path
import os
import struct
import subprocess
import sys
import tempfile

import startup_gfx2hlsl_compute_check as C
from startup_compute_dispatch_check import dispatch, lines, prepare_case
from startup_compute_dispatch_native_check import LOAD_X8, LOADS_X4
from startup_stream_check import gfx2hlsl as G

RAW, R16_UINT, R32_UINT = 39, 57, 42 # DXGI_FORMAT values


def view(first, elements, fmt=RAW):
"""D3D12_UNORDERED_ACCESS_VIEW_DESC of a buffer: format, dimension, first, count, stride, counter, flags."""
return struct.pack('<IIQIIQI4x', fmt, 1, first, elements, 0, 0, 1 if fmt == RAW else 0)


CONTROLS = (
('raw', view(8, 1, R16_UINT), 'an output UAV interval is not whole dwords'),
('large', view(0, (4 << 20) + 4), 'an output UAV interval is over 16 MiB'),
('raw', view(1, 300, R32_UINT), 'an output UAV interval is not 16-byte aligned in its buffer'),
)


def main(consumer, dxc, adapter):
with tempfile.TemporaryDirectory() as temporary:
directory = Path(temporary)
cache, env = prepare_case(directory, adapter)
env.pop('X2W_NATIVE_SHADERS', None) # Exercise published translations.
program = directory / 'program'
case_a = program.read_bytes()
assert case_a.count(LOAD_X8) == 1
program.write_bytes(case_a.replace(LOAD_X8, LOADS_X4))
untouched, expected = [C.SENTINEL] * C.WORDS, C.expected('A')

got, result = dispatch(directory, consumer, env, 'dispatch-uav-token-raw', probe=True)
print(result.stdout, end='')
assert got == untouched and len(lines(result, 'refused: no translation')) == 1, result.stderr[-3000:]
published = lines(result, 'x2w-draw compute request')
assert published and ' published ' in published[0], result.stderr[-3000:]
key = int(published[0].split()[3], 16)
out = subprocess.run([sys.executable, '-B', G.__file__, '--pending', cache], check=True, capture_output=True,
text=True, env=dict(os.environ, X2W_DXC=dxc)).stdout
assert f'{key:016x}: ok' in out, out

got, result = dispatch(directory, consumer, env, 'dispatch-uav-token-raw')
bound = untouched[:4] + expected[:-8] + untouched[-4:]
assert got == bound and not lines(result, 'refused'), result.stderr[-3000:]
assert lines(result, 'offset=16 bytes=1248 result=bound'), result.stderr[-3000:]

for backing, desc, reason in CONTROLS:
(directory / 'uav').write_bytes(desc)
got, result = dispatch(directory, consumer, dict(env, X2W_TEST_DISPATCH_UAV=str(directory / 'uav')),
f'dispatch-uav-token-{backing}')
name = f'{backing} {struct.unpack("<IIQIIQI4x", desc)[:4]}'
assert got == untouched, f'{name}: a refused Dispatch wrote the output'
refused = lines(result, 'refused: ')
census = lines(result, 'x2w-render compute census')
assert len(refused) == 1 and refused[0].endswith('refused: ' + reason), f'{name}:\n{result.stderr[-3000:]}'
assert len(census) == 3 and census[1].endswith(f'reason "{reason}": 1 keys, 1 dispatches'), (name, census)
print(f'startup_compute_token_interval: PASS on {adapter} (a raw slot-19 view bound, words 4-{C.WORDS - 5} '
f'match; {len(CONTROLS)} intervals refused, each with its own reason and an untouched output)')


if __name__ == '__main__':
if len(sys.argv) != 4 or sys.argv[3] not in ('warp', 'hardware'):
sys.exit(__doc__)
main(*sys.argv[1:])
13 changes: 12 additions & 1 deletion tests/startup_stream_consumer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,8 @@ static void* make_committed(void* device, D3D12_HEAP_TYPE type, const D3D12_RESO
// For any mode, X2W_TEST_DISPATCH_IN names 1280 bytes that replace the input words, and X2W_TEST_DISPATCH_FIELDS
// names six words that replace, for the input and then the output descriptor, the stride (word 1 bits 16-31), the
// record count (word 2) and word 3, which are otherwise 4, 320 and 0.
// dispatch-uav-token-* writes the token of a slot-19 view of the output buffer at table byte 32. X2W_TEST_DISPATCH_UAV
// names a D3D12_UNORDERED_ACCESS_VIEW_DESC that replaces the view; dispatch-uav-token-large views a committed buffer.
// (The long case puts its input at 0x28000, not at a 64 KiB multiple like 0x20000: there the placed buffer would alias title memory
// and the input would be read in place, never snapshotted, which is how an earlier version of this case passed without a snapshot.)
// dispatch-input-snapshot-long: dispatch-input-snapshot with an 8 MiB input range (the descriptor declares 2M records; the program
Expand Down Expand Up @@ -424,15 +426,24 @@ static int dispatch_slot67(IDXGIAdapter* adapter, void* device, const std::strin
view.Format = DXGI_FORMAT_R32_UINT;
view.Buffer.Flags = D3D12_BUFFER_UAV_FLAG_NONE;
}
if (!from_file("X2W_TEST_DISPATCH_UAV", &view, sizeof(view))) return 2;
void* output = placed[1];
if (mode == "dispatch-uav-token-large") { // a committed buffer 64 KiB over 16 MiB, with no title memory
const D3D12_RESOURCE_DESC desc{D3D12_RESOURCE_DIMENSION_BUFFER, 0, (16u << 20) + 0x10000, 1, 1, 1,
DXGI_FORMAT_UNKNOWN, {1, 0}, D3D12_TEXTURE_LAYOUT_ROW_MAJOR, D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS};
if (!(output = make_committed(device, D3D12_HEAP_TYPE_DEFAULT, desc, D3D12_RESOURCE_STATE_COMMON))) return 71;
}
if (mode == "dispatch-uav-token-copied") {
const D3D12_RESOURCE_DESC desc{D3D12_RESOURCE_DIMENSION_BUFFER, 0, words * sizeof(UINT), 1, 1, 1,
DXGI_FORMAT_UNKNOWN, {1, 0}, D3D12_TEXTURE_LAYOUT_ROW_MAJOR, D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS};
if (FAILED(static_cast<HRESULT>(invoke(device, 133, between, word(&desc), D3D12_RESOURCE_STATE_COMMON, 0,
word(&__uuidof(ID3D12Resource)), word(&output))))) return 71;
}
if (mode == "dispatch-uav-token-copied") copied_token_output = output;
invoke(device, 19, word(output), 0, word(&view), cpu + 32);
// Byte 32 is not a descriptor of the heap on every adapter (WARP): the view is created at byte 64 and its
// token copied to byte 32, as a title copies descriptors.
invoke(device, 19, word(output), 0, word(&view), cpu + 64);
std::memcpy(reinterpret_cast<void*>(cpu + 32), reinterpret_cast<const void*>(cpu + 64), 16);
if (mode == "dispatch-uav-token-wrong-slot")
reinterpret_cast<UINT*>(cpu + 32)[2] = 18;
}
Expand Down
Loading