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
10 changes: 10 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2472,6 +2472,16 @@ endforeach()
add_test(NAME startup_present_view_srgb_control COMMAND startup_present_view_check warp --mutate srgb)
set_tests_properties(startup_present_view_identity_control startup_present_view_srgb_control PROPERTIES WILL_FAIL TRUE)
set_tests_properties(startup_present_view_hardware PROPERTIES SKIP_RETURN_CODE 77)
# Input view-token refusal table (src/startup_stubs/input_view_token.h). Device-free. The mutated
# runs expect a guard to be gone and must fail.
add_executable(startup_input_view_token_check tests/startup_input_view_token_check.cpp)
target_include_directories(startup_input_view_token_check PRIVATE src)
target_compile_features(startup_input_view_token_check PRIVATE cxx_std_17)
add_test(NAME startup_input_view_token COMMAND startup_input_view_token_check)
foreach(mutation IN ITEMS short-srv short-cbv wrapper placement depth wrong-kind bind)
add_test(NAME startup_input_view_token_${mutation}_control COMMAND startup_input_view_token_check --mutate ${mutation})
set_tests_properties(startup_input_view_token_${mutation}_control PROPERTIES WILL_FAIL TRUE)
endforeach()
# Buffer memory SRV validation for device slot 70 (src/startup_stubs/memory_srv_buffer.h); the mutated
# runs are negative controls.
add_executable(startup_memory_srv_buffer_check tests/startup_memory_srv_buffer_check.cpp)
Expand Down
67 changes: 67 additions & 0 deletions docs/experiments/compute-input-view-token.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Compute input view tokens

A translated compute dispatch binds a view token as a buffer input only when the token is a
constant-buffer view, or a buffer shader-resource view of a desktop resource. Every other token
used to share one census sentence, `an input view token is not a buffer view of a desktop resource`.
`indirect_table_buffer` now refuses that token with the record it resolved to, in this order:

1. `an input view token is unknown`
2. `an input view token is a sampler`
3. `an input view token is an unordered access view`
4. `an input view token is a render or depth view`
5. `an input view token has a truncated constant buffer view`
6. `an input view token has an unrecognized slot`
7. `an input view token has a truncated shader resource view`
8. `an input view token is not a buffer view of a desktop resource` (a shader resource view whose
dimension is not a buffer; a texture token keeps this historical sentence)
9. `an input view token's resource is not a wrapper`
10. `an input view token names an object that is not a resource`
11. `an input view token's placed or copied resource has no desktop backing`
12. `an input view token has no desktop resource`

The census still keeps the first reason of each key. A buffer view that passes this gate is then
refused, in this order, for element size, an empty or oversized range, a placed span or alias, a
null address, alignment, a released resource, a resource not created in the common state, or a
resource on another device. The next check is the desktop width,
`an input range is outside its desktop buffer`. A resource with no title memory is refused for its
heap type only after that, with
`an input buffer view token's resource is not on a default or upload heap`. A view recorded
without a desktop descriptor still names its resource, so a range past that resource's width uses
the width sentence.

A typed or raw buffer view whose resource has a desktop object passes this gate. The fixture binds
two of those on the placed input buffer that aliases title memory, an `R32_UINT` view and a raw
`R32_TYPELESS` view, and reads all 320 words of case A back. A placed buffer the desktop cannot
alias is still refused with `an input is in a placed buffer that does not alias title memory`. A
placed or copied resource with no desktop object stays refused; the fixture does not construct one,
and the Linux reference is what checks that sentence. The sampler refusal copies a token out of a
one-entry sampler heap. The compute table is a CBV/SRV/UAV heap, and a sampler descriptor written
there faults in the desktop runtime before the dispatch is recorded. Slot 18 still forwards every shader-resource view to the desktop, graphics included. A buffer view
past the end of its resource is one of those calls, and the desktop runtime can remove the device
for it. The fixture does not make that call. It records the description of a custom-heap buffer
with no title memory, four elements past the buffer, and the width sentence refuses it. Removing
that check leaves the heap-type sentence, and the control fails. The sentences in the table above
are checked by [startup_input_view_token_check.cpp](../../tests/startup_input_view_token_check.cpp),
including a bind that is not refused.

`tests/startup_compute_dispatch_input_check.py` still expects the historical sentence for
`dispatch-input-token`. This note does not change that test.

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

That command runs the reference and the controls. It does not dispatch.

On the Windows test PC:

```powershell
ctest --preset dev --no-tests=error -R "^startup_compute_(input_view_token_|dispatch_input_)" -V
```

`startup_compute_input_view_token_warp` and `_hardware` skip with 77 when the adapter is not
available. A skip is not a pass. This note has no Windows readback. Nothing here shows that a
title starts or runs.

The decision table is [input_view_token.h](../../src/startup_stubs/input_view_token.h). The fixture
is [startup_compute_input_view_token_check.py](../../tests/startup_compute_input_view_token_check.py).
55 changes: 43 additions & 12 deletions src/startup_stubs/d3d12_draw.inl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@
// upload ring / a private descriptor heap.

#include "startup_stubs/typed_uav_load.h"
#include "startup_stubs/input_view_token.h"

static_assert(D3D12_SRV_DIMENSION_BUFFER == 1, "input view token facts use 1 for a buffer SRV");
static_assert(static_cast<unsigned>(Placement::none) == 0, "input view token facts use 0 for no placement");
static_assert(static_cast<unsigned>(Placement::aliased_buffer) == 1, "input view token facts use 1 for an aliased buffer");
static_assert(static_cast<unsigned>(Placement::copied_buffer) == 2, "input view token facts use 2 for a copied buffer");

// A pixel-stage image store (#1996 item F, #2742): tools/startup/gfx2hlsl.py writes 'uav N tex2d|tex2da table P O'
// for the RWTexture2D<float4> or RWTexture2DArray<float4> at u<N>, whose descriptor is at byte O of the title's
Expand Down Expand Up @@ -2551,21 +2557,38 @@ IndirectBufferView indirect_table_buffer(Wrapper* self, const std::uint32_t* wor
if (why) *why = nullptr;
if (words[0] == view_token_magic) {
ViewRecord record{};
token_view(words, record);
const bool known = token_view(words, record);
D3D12_SHADER_RESOURCE_VIEW_DESC view{};
const bool described = known && record.slot == 18 && record.desc_bytes >= 40;
if (described) std::memcpy(&view, record.desc, 40);
Wrapper* const token_owner = described ? as_wrapper(reinterpret_cast<const void*>(record.resource)) : nullptr;
const bool token_resource = token_owner && token_owner->own().kind == resource_kind;
const ResourceInfo* token_info = token_resource ? resource_info(token_owner) : nullptr;
InputViewTokenSource source;
source.known = known;
source.slot = record.slot;
source.desc_bytes = record.desc_bytes;
source.view_dimension = described ? static_cast<std::uint32_t>(view.ViewDimension) : 0;
source.resource_pointer = record.resource != 0;
source.owner_is_wrapper = token_owner != nullptr;
source.owner_is_resource = token_resource;
source.desktop = token_resource && token_owner->own().real != nullptr;
source.placement = token_info ? static_cast<std::uint32_t>(token_info->placement) : 0;
const InputViewTokenFacts facts = input_view_token_facts(source);
// A byte-window load asks for identity mapping before the token's shape is named. The flag is
// off for every other caller, so the token table is unchanged for them.
if (described && requires_identity_dst_sel &&
view.Shader4ComponentMapping != D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING)
return refuse("an MTBUF byte window requires identity component mapping");
if (const char* token_reason = input_view_token_refusal(facts)) return refuse(token_reason);
if (record.slot == 17 && record.desc_bytes >= 12) {
if (requires_identity_dst_sel) return refuse("an MTBUF byte window requires a buffer SRV");
UINT size = 0;
std::memcpy(&address, record.desc, 8);
std::memcpy(&size, record.desc + 8, 4);
bytes = size;
} else if (record.slot == 18 && record.desc_bytes >= 40) {
D3D12_SHADER_RESOURCE_VIEW_DESC view{};
std::memcpy(&view, record.desc, 40);
if (requires_identity_dst_sel && view.Shader4ComponentMapping != D3D12_DEFAULT_SHADER_4_COMPONENT_MAPPING)
return refuse("an MTBUF byte window requires identity component mapping");
owner = as_wrapper(reinterpret_cast<const void*>(record.resource));
if (!owner || owner->own().kind != resource_kind || !owner->own().real ||
view.ViewDimension != D3D12_SRV_DIMENSION_BUFFER) return refuse("an input view token is not a buffer view of a desktop resource");
} else if (record.slot == 18 && record.desc_bytes >= 40 && view.ViewDimension == D3D12_SRV_DIMENSION_BUFFER) {
owner = token_owner;
const bool raw = view.Buffer.Flags == D3D12_BUFFER_SRV_FLAG_RAW && !view.Buffer.StructureByteStride &&
view.Format == DXGI_FORMAT_R32_TYPELESS;
const bool structured = view.Buffer.Flags == D3D12_BUFFER_SRV_FLAG_NONE && view.Buffer.StructureByteStride &&
Expand All @@ -2590,7 +2613,7 @@ IndirectBufferView indirect_table_buffer(Wrapper* self, const std::uint32_t* wor
if (!own_memory) address = base + offset;
}
} else {
return refuse("an input view token is not a buffer view of a desktop resource");
return refuse("an input view token has an unrecognized slot");
}
} else {
if (requires_identity_dst_sel && (words[3] & 0xfffu) != 0xfacu)
Expand All @@ -2615,6 +2638,7 @@ IndirectBufferView indirect_table_buffer(Wrapper* self, const std::uint32_t* wor
: "an input buffer range exceeds the D3D12 raw SRV limit or is not whole dwords");
ID3D12Resource* native = nullptr;
bool upload = false;
bool reject_heap = false;
int input_source = 0;
Wrapper* device = self->own().list_device;
if (address) {
Expand Down Expand Up @@ -2674,8 +2698,9 @@ IndirectBufferView indirect_table_buffer(Wrapper* self, const std::uint32_t* wor
D3D12_HEAP_FLAGS flags{};
if (FAILED(native->GetHeapProperties(&props, &flags)) ||
(props.Type != D3D12_HEAP_TYPE_DEFAULT && props.Type != D3D12_HEAP_TYPE_UPLOAD))
return refuse("an input buffer view token's resource is not on a default or upload heap");
upload = props.Type == D3D12_HEAP_TYPE_UPLOAD;
reject_heap = true; // after the desktop range check: a range past the resource wins
else
upload = props.Type == D3D12_HEAP_TYPE_UPLOAD;
}
if (!native) return refuse("an input buffer descriptor has a null address");
if (offset % alignment) return refuse(alignment == 4 ? "typed input range is not dword aligned" :
Expand All @@ -2688,6 +2713,12 @@ IndirectBufferView indirect_table_buffer(Wrapper* self, const std::uint32_t* wor
if (FAILED(native->GetDevice(IID_PPV_ARGS(native_device.GetAddressOf()))) || native_device.Get() != device_of(device))
return refuse("an input buffer's resource is on another device");
const auto desc = native->GetDesc();
// A view recorded without a desktop descriptor still names this resource. Its byte range is decided
// before the heap type, so a range past the end is not reported as the wrong kind of heap.
if (desc.Dimension == D3D12_RESOURCE_DIMENSION_BUFFER && !(desc.Flags & D3D12_RESOURCE_FLAG_DENY_SHADER_RESOURCE) &&
(offset > desc.Width || bytes > desc.Width - offset))
return refuse("an input range is outside its desktop buffer");
if (reject_heap) return refuse("an input buffer view token's resource is not on a default or upload heap");
if (desc.Dimension != D3D12_RESOURCE_DIMENSION_BUFFER || (desc.Flags & D3D12_RESOURCE_FLAG_DENY_SHADER_RESOURCE) ||
offset > desc.Width || bytes > desc.Width - offset) return refuse("an input range is outside its desktop buffer");
trace_compute_output_use(self, OutputUse::shader_gpu_read, "raw-SRV-range-resolved", owner, 0, offset, bytes);
Expand Down
14 changes: 14 additions & 0 deletions src/startup_stubs/d3d12_xs_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6601,6 +6601,20 @@ struct ViewStoreProbe {
unsigned long long records, materialized, ids, records_capacity, materialized_capacity;
unsigned shadow, view_refs, retire_when_idle;
};
// Fixture only (dispatch-input-token-past). Records a buffer shader-resource view without calling
// CreateShaderResourceView. Slot 18 still forwards every shader-resource view to the desktop,
// including one whose range runs past the resource; that desktop call can remove the device.
extern "C" __declspec(dllexport) BOOL x2w_d3d12_record_uncreated_buffer_srv(void* resource, const void* desc,
UINT desc_bytes, UINT64 title_cpu)
{
if (!resource || !desc || desc_bytes < ViewRecord::srv_desc_bytes || !title_cpu) return FALSE;
if (!x2w_local::readable(desc, ViewRecord::srv_desc_bytes)) return FALSE;
const u64 desktop = desktop_cpu_handle(title_cpu);
if (!desktop || desktop == title_cpu) return FALSE;
note_view(18, reinterpret_cast<u64>(resource), 0, desc, ViewRecord::srv_desc_bytes, title_cpu, desktop, false);
return TRUE;
}

extern "C" __declspec(dllexport) void x2w_d3d12_view_probe(unsigned long long title_handle, void* resource, ViewStoreProbe* out)
{
*out = {};
Expand Down
78 changes: 78 additions & 0 deletions src/startup_stubs/input_view_token.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// What an input view token resolves to, before a compute or indirect buffer bind.
// nullptr means the token is a constant-buffer view, or a buffer shader-resource view of a
// desktop resource: the caller keeps its range, alignment and alias checks. Every other
// token is refused with the shape that failed, so one census reason is not shared by all of them.
// A non-buffer shader resource view keeps the historical sentence. Placement is the Placement
// enumeration: 0 none, 1 aliased buffer, 2 copied buffer.
#pragma once

#include <cstdint>

struct InputViewTokenFacts {
bool known = false;
std::uint32_t slot = 0;
std::uint32_t desc_bytes = 0;
std::uint32_t view_dimension = 0;
bool resource_pointer = false;
bool wrapper = false;
bool resource = false;
bool desktop = false;
std::uint32_t placement = 0;
};

// The fields a view record can show, before they are combined. owner_is_resource is meaningful
// only together with owner_is_wrapper: a wrapper of another kind is not a resource.
struct InputViewTokenSource {
bool known = false;
std::uint32_t slot = 0;
std::uint32_t desc_bytes = 0;
std::uint32_t view_dimension = 0;
bool resource_pointer = false;
bool owner_is_wrapper = false;
bool owner_is_resource = false;
bool desktop = false;
std::uint32_t placement = 0;
};

inline InputViewTokenFacts input_view_token_facts(const InputViewTokenSource& source)
{
InputViewTokenFacts facts;
facts.known = source.known;
facts.slot = source.slot;
facts.desc_bytes = source.desc_bytes;
facts.view_dimension = source.view_dimension;
facts.resource_pointer = source.resource_pointer;
facts.wrapper = source.owner_is_wrapper;
facts.resource = source.owner_is_wrapper && source.owner_is_resource;
facts.desktop = facts.resource && source.desktop;
facts.placement = source.placement;
return facts;
}

inline const char* input_view_token_refusal(const InputViewTokenFacts& facts)
{
constexpr std::uint32_t buffer_dimension = 1; // D3D12_SRV_DIMENSION_BUFFER
constexpr std::uint32_t shader_resource = 18;
constexpr std::uint32_t shader_resource_bytes = 40;
constexpr std::uint32_t constant_buffer_bytes = 12;
if (!facts.known) return "an input view token is unknown";
if (facts.slot == 22) return "an input view token is a sampler";
if (facts.slot == 19) return "an input view token is an unordered access view";
if (facts.slot == 20 || facts.slot == 21) return "an input view token is a render or depth view";
if (facts.slot == 17) {
if (facts.desc_bytes < constant_buffer_bytes) return "an input view token has a truncated constant buffer view";
return nullptr;
}
if (facts.slot != shader_resource) return "an input view token has an unrecognized slot";
if (facts.desc_bytes < shader_resource_bytes) return "an input view token has a truncated shader resource view";
if (facts.view_dimension != buffer_dimension)
return "an input view token is not a buffer view of a desktop resource";
if (!facts.resource_pointer || !facts.wrapper) return "an input view token's resource is not a wrapper";
if (!facts.resource) return "an input view token names an object that is not a resource";
if (!facts.desktop) {
if (facts.placement == 1 || facts.placement == 2)
return "an input view token's placed or copied resource has no desktop backing";
return "an input view token has no desktop resource";
}
return nullptr;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tests/startup_compute_input_view_token_check.py
18 changes: 18 additions & 0 deletions tests/cmake/startup_compute_input_view_token.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Compute input view tokens. reference and controls are pure Python. The readback needs a
# desktop device and otherwise reports a skip (77).
if(EXISTS "${INTAKE_PYTHON}")
foreach(mode IN ITEMS reference controls)
add_test(NAME startup_compute_input_view_token_${mode} COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_SOURCE_DIR}/tests/startup_compute_input_view_token_check.py" ${mode})
set_tests_properties(startup_compute_input_view_token_${mode} PROPERTIES TIMEOUT 60)
endforeach()
endif()
if(TARGET startup_stream_consumer AND EXISTS "${INTAKE_PYTHON}" AND EXISTS "${DXC_EXECUTABLE}")
foreach(adapter IN ITEMS warp hardware)
add_test(NAME startup_compute_input_view_token_${adapter} COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_SOURCE_DIR}/tests/startup_compute_input_view_token_check.py"
"$<TARGET_FILE:startup_stream_consumer>" "${DXC_EXECUTABLE}" ${adapter})
set_tests_properties(startup_compute_input_view_token_${adapter} PROPERTIES
TIMEOUT 120 SKIP_RETURN_CODE 77)
endforeach()
endif()
Loading
Loading