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
1 change: 1 addition & 0 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ python3 -m venv out/pe-env
./out/pe-env/bin/python -B tests/startup_compute_image_store_check.py
./out/pe-env/bin/python -B tests/startup_pixel_image_store_check.py
./out/pe-env/bin/python -B tests/startup_compute_image_store_d16_check.py
./out/pe-env/bin/python -B tests/startup_gfx2hlsl_image_store_3d1d_check.py
./out/pe-env/bin/python -B tests/startup_gfx2hlsl_heap_record_check.py
./out/pe-env/bin/python -B tests/startup_compute_store_format_xyzw_check.py
./out/pe-env/bin/python -B tests/startup_gfx2hlsl_heap_word_check.py
Expand Down
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3351,6 +3351,8 @@ if(EXISTS "${INTAKE_PYTHON}")
"${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_compute_image_store_check.py")
add_test(NAME startup_compute_image_store_d16 COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_compute_image_store_d16_check.py")
add_test(NAME startup_gfx2hlsl_image_store_3d1d COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_gfx2hlsl_image_store_3d1d_check.py")
add_test(NAME startup_gfx2hlsl_heap_record COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_CURRENT_SOURCE_DIR}/tests/startup_gfx2hlsl_heap_record_check.py")
add_test(NAME startup_gfx2hlsl_heap_word COMMAND "${INTAKE_PYTHON}" -B
Expand Down
14 changes: 14 additions & 0 deletions docs/experiments/startup-compute-volume-line-uav.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Startup compute stores to 3D and 1D UAVs

The startup translator lowers full-channel compute `image_store` to
`RWTexture3D<float4>` with an integer XYZ address or `RWTexture1D<float4>`
with a scalar integer address. A 3D address can be a VGPR tuple or an NSA list.
Both forms use the fixed table descriptor and emit a `tex3d` or `tex1d` UAV
entry. Requests refused before this change are retried by `--pending`, so
the compute cache revision is unchanged.

Dispatch uses the title's view at its selected mip. For 3D the shader's Z
coordinate is relative to `FirstWSlice`, and a W size of all ones selects
the remaining slices. The Dispatch fixture compares all mip-one texels
against a CPU reference and checks that a 2D view or an out-of-range W
selection leaves the texels unchanged.
4 changes: 4 additions & 0 deletions docs/startup-runtime.md
Original file line number Diff line number Diff line change
Expand Up @@ -2392,6 +2392,10 @@ These are measured on one executable; treat them as hypotheses for others.
z must fail. The desktop draw reads all 16 texels of a 4x4 image back; deleting the store or
exchanging x and y must fail. Run
`ctest --preset release --no-tests=error -R '^startup_pixel_image_store_' -V`.
- **Compute stores to 3D and 1D images.** A compute `image_store` with `dmask:0xf` on a 3D or 1D image
becomes a store to `RWTexture3D<float4>` or `RWTexture1D<float4>`, and Dispatch binds the title's
3D or 1D view after checking its mip and, for 3D, its W range. See
[Startup compute stores to 3D and 1D UAVs](experiments/startup-compute-volume-line-uav.md).
- **Binding pixel-stage image stores.** A translated draw binds each `uav N tex2d|tex2da table P O` line
as a UAV range `u0..` placed after the SRVs in the translated root signature's view table. The view is made from the unordered-access
view the title created at that table entry (`resolve_pixel_uavs` in `d3d12_draw.inl`), on
Expand Down
56 changes: 44 additions & 12 deletions src/startup_stubs/d3d12_compute_dispatch.inl
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +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) named by hardware buffer descriptors or 64-bit root SRV addresses, and 2D textures
// (`srv <i> tex2d table <param> <byte>` inputs, `uav <i> tex2d/tex2da float table <param> <byte> channels 4` outputs) named
// outputs) named by hardware buffer descriptors or 64-bit root SRV addresses, and textures
// (`srv <i> tex2d table <param> <byte>` inputs, `uav <i> tex1d/tex2d/tex2da/tex3d float table <param> <byte> channels 4` outputs) named
// by the token of a view the title created (device slots 18 and 19). `smp` lines become static samplers. Entry
// values come from the compute root constants of slots 37 and 39, or from the descriptor a buffer input or output is
// bound from: its stride and word 3 (`stride`, `descword3`), and word 3, the upper half of word 1 and the record
Expand All @@ -12,6 +12,15 @@
// binds nothing (the upload of an input texture the title filled in memory may already be recorded: it only
// syncs content).

const char* volume_uav_range_error(const D3D12_RESOURCE_DESC& resource, const D3D12_TEX3D_UAV& view)
{
if (view.MipSlice >= resource.MipLevels) return "an output 3D texture mip is outside the resource";
const UINT depth = (std::max)(1u, UINT{resource.DepthOrArraySize} >> (std::min)(view.MipSlice, 15u));
if (view.FirstWSlice >= depth || !view.WSize || (view.WSize != ~0u && view.WSize > depth - view.FirstWSlice))
return "an output 3D texture W range is outside the mip";
return nullptr;
}

struct ComputeTranslation {
// Root parameter and byte offset (views), or parameter and word (entry values). A heap view names the root
// constant (parameter and word) that indexes its 32-byte record in the list's view heap, or, with word_param
Expand All @@ -24,7 +33,7 @@ struct ComputeTranslation {
int word_param = -1, word_byte = 0;
std::uint32_t at = 0, times = 0;
Source source = root_constant;
bool array = false;
D3D12_UAV_DIMENSION uav_dimension = D3D12_UAV_DIMENSION_UNKNOWN;
bool root = false;
};
u64 key = 0;
Expand Down Expand Up @@ -105,6 +114,13 @@ const char* load_compute_translation(ComputeTranslation& tr)
if (!std::strcmp(name, names[s])) return static_cast<ComputeTranslation::Source>(s + 1);
return ComputeTranslation::root_constant;
};
const auto uav_dimension = [&] {
if (!std::strcmp(name, "tex1d")) return D3D12_UAV_DIMENSION_TEXTURE1D;
if (!std::strcmp(name, "tex2d")) return D3D12_UAV_DIMENSION_TEXTURE2D;
if (!std::strcmp(name, "tex2da")) return D3D12_UAV_DIMENSION_TEXTURE2DARRAY;
if (!std::strcmp(name, "tex3d")) return D3D12_UAV_DIMENSION_TEXTURE3D;
return D3D12_UAV_DIMENSION_UNKNOWN;
};
if (line.empty() || (std::sscanf(line.c_str(), "group %u %u %u%n", &x, &y, &z, &used) == 3 && whole())) continue;
if (std::sscanf(line.c_str(), "ud %u%n", &declared_uds, &used) == 1 && whole()) continue;
if (std::sscanf(line.c_str(), "udsrc %u rootconst %d %d%n", &i, &a, &b, &used) == 3 && whole() && i == tr.uds.size())
Expand All @@ -130,13 +146,10 @@ const char* load_compute_translation(ComputeTranslation& tr)
tr.srvs.push_back({c, d, false, true, a, b, x, y});
else if (std::sscanf(line.c_str(), "uav %u table %d %d%n", &i, &a, &b, &used) == 3 && whole() && i == tr.uavs.size())
tr.uavs.push_back({a, b});
else if (std::sscanf(line.c_str(), "uav %u tex2d float table %d %d channels 4%n", &i, &a, &b, &used) == 3 && whole() &&
i == tr.uavs.size())
tr.uavs.push_back({a, b, true});
else if (std::sscanf(line.c_str(), "uav %u tex2da float table %d %d channels 4%n", &i, &a, &b, &used) == 3 && whole() &&
i == tr.uavs.size()) {
else if (std::sscanf(line.c_str(), "uav %u %15s float table %d %d channels 4%n", &i, name, &a, &b, &used) == 4 && whole() &&
i == tr.uavs.size() && uav_dimension() != D3D12_UAV_DIMENSION_UNKNOWN) {
tr.uavs.push_back({a, b, true});
tr.uavs.back().array = true;
tr.uavs.back().uav_dimension = uav_dimension();
}
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});
Expand All @@ -149,7 +162,7 @@ const char* load_compute_translation(ComputeTranslation& tr)
i == tr.samplers.statics.size())
tr.samplers.read_manifest_line(line.c_str());
else return line.rfind("srv", 0) == 0 ? "a resource that is not a raw table buffer or a 2D texture"
: line.rfind("uav", 0) == 0 ? "an output that is not a raw table buffer or a 2D/2D-array float texture"
: line.rfind("uav", 0) == 0 ? "an output that is not a raw table buffer or a 1D/2D/2D-array/3D float texture"
: line.rfind("udsrc", 0) == 0 ? "an entry value that is not a root constant or a buffer descriptor word"
: line.rfind("smp", 0) == 0 ? "a sampler that is not a plain static one" : "an unknown manifest line";
}
Expand Down Expand Up @@ -388,9 +401,28 @@ const char* record_translated_dispatch(Wrapper* self, const TranslatedDispatch&
const DXGI_FORMAT format = desc.Format ? desc.Format : resource.Format;
words[3] = static_cast<std::uint32_t>(format) | static_cast<std::uint32_t>(desc.ViewDimension) << 16;
if (!known || record.slot != 19) reason = "an output texture entry is not the token of an unordered access view";
else if (!created || !texture || !(resource.Flags & D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS))
else if (slot.uav_dimension == D3D12_UAV_DIMENSION_TEXTURE3D &&
texture && (resource.Flags & D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS)) {
const auto& view = desc.Texture3D;
if (!described || desc.ViewDimension != slot.uav_dimension || resource.Dimension != D3D12_RESOURCE_DIMENSION_TEXTURE3D)
reason = "an output texture view or resource is not 3D";
else {
// Slot 19 can retain a rejected range as an unmaterialized view.
reason = volume_uav_range_error(resource, view);
if (!reason && !created) reason = "an output texture view was not created on the desktop";
}
} else if (slot.uav_dimension == D3D12_UAV_DIMENSION_TEXTURE1D &&
texture && (resource.Flags & D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS)) {
// Checked before `created`, as for 3D: slot 19 records a 1D view with an out-of-range mip as refused.
if (!described || desc.ViewDimension != slot.uav_dimension || resource.Dimension != D3D12_RESOURCE_DIMENSION_TEXTURE1D)
reason = "an output texture view or resource is not 1D";
else if (desc.Texture1D.MipSlice >= resource.MipLevels)
reason = "an output 1D texture mip is outside the resource";
else if (!created)
reason = "an output texture view was not created on the desktop";
} else if (!created || !texture || !(resource.Flags & D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS))
reason = "an output texture view was not created on the desktop";
else if (slot.array) {
else if (slot.uav_dimension == D3D12_UAV_DIMENSION_TEXTURE2DARRAY) {
// The layer in HLSL is relative to FirstArraySlice: retain the caller's complete view.
const auto& a = desc.Texture2DArray;
if (!described || desc.ViewDimension != D3D12_UAV_DIMENSION_TEXTURE2DARRAY)
Expand Down
20 changes: 17 additions & 3 deletions src/startup_stubs/d3d12_xs_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4741,14 +4741,14 @@ __declspec(noinline) u64 view_slot(Wrapper* self, u64 a, u64 b, u64 c, u64 d)
};
const char* skip = nullptr;
bool default_view = false;
bool uav_denied = false; // a UAV of a resource without unordered access: recorded as not created, as before #2649
bool record_refused_uav = false;
if (N == 17) skip = "constant buffer view (title GPU address)";
if (N >= 18 && N <= 21 && args[0]) {
const UINT flags = reinterpret_cast<ID3D12Resource*>(args[0])->GetDesc().Flags;
if (N == 18 && (flags & D3D12_RESOURCE_FLAG_DENY_SHADER_RESOURCE)) skip = "resource denies shader resource views";
if (N == 19 && !(flags & D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS)) {
skip = "resource lacks unordered access";
uav_denied = true;
record_refused_uav = true;
}
if (N == 20 && !(flags & D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET)) skip = "resource lacks render target";
if (N == 21 && !(flags & D3D12_RESOURCE_FLAG_ALLOW_DEPTH_STENCIL)) skip = "resource lacks depth stencil";
Expand Down Expand Up @@ -4783,6 +4783,20 @@ __declspec(noinline) u64 view_slot(Wrapper* self, u64 a, u64 b, u64 c, u64 d)
if (first_calls(mismatched, 6))
log("x2w-d3d12 Device::slot%d view dimension %u needs resource dimension %u, resource has %u", N, view_dimension,
x2w_view_dimension::needed(N, view_dimension), dimension);
} else if constexpr (N == 19) {
if (view_dimension == D3D12_UAV_DIMENSION_TEXTURE3D &&
x2w_local::readable(reinterpret_cast<const void*>(described), sizeof(D3D12_UNORDERED_ACCESS_VIEW_DESC))) {
D3D12_UNORDERED_ACCESS_VIEW_DESC view{};
std::memcpy(&view, reinterpret_cast<const void*>(described), sizeof(view));
skip = volume_uav_range_error(resource_desc, view.Texture3D);
record_refused_uav = skip != nullptr;
} else if (view_dimension == D3D12_UAV_DIMENSION_TEXTURE1D &&
x2w_local::readable(reinterpret_cast<const void*>(described), sizeof(D3D12_UNORDERED_ACCESS_VIEW_DESC))) {
D3D12_UNORDERED_ACCESS_VIEW_DESC view{};
std::memcpy(&view, reinterpret_cast<const void*>(described), sizeof(view));
if (view.Texture1D.MipSlice >= resource_desc.MipLevels) skip = "an output 1D texture mip is outside the resource";
record_refused_uav = skip != nullptr;
}
}
}
}
Expand All @@ -4801,7 +4815,7 @@ __declspec(noinline) u64 view_slot(Wrapper* self, u64 a, u64 b, u64 c, u64 d)
} else if constexpr (N == 19) {
// A translated compute Dispatch tells this refusal from a descriptor that holds no view by the record.
// A refused creation leaves a descriptor that still holds a live view as it is.
if (uav_denied && !descriptor_has_live_view(args[D], title_dest)) record_view(false);
if (record_refused_uav && !descriptor_has_live_view(args[D], title_dest)) record_view(false);
}
};
if (skip) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tests/startup_gfx2hlsl_image_store_3d1d_check.py --llvm-mc llvm-mc-15
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tests/startup_gfx2hlsl_image_store_3d1d_check.py
52 changes: 52 additions & 0 deletions tests/startup_compute_dispatch_texture_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
4. texture-store-denied (a texture without unordered access, whose view the stand-in does not create): refused
with one logged reason, every half keeps its sentinel.
A fifth run binds the same program under a manifest edited to declare no entry values.
The 1D and 3D float texture cases check mip-one readback, a mismatched view, and an invalid 3D W range.

Texture input: case A of startup_gfx2hlsl_compute_check.py with a level-zero sample of a 2D texture XORed into the
stored word (a static sampler, a texture view at table byte 64). The consumer adds a 1x1 RGBA8 texture whose red
Expand All @@ -34,6 +35,7 @@

import startup_compute_image_store_d16_check as S
import startup_gfx2hlsl_compute_check as C
import startup_gfx2hlsl_image_store_3d1d_check as V
from startup_compute_dispatch_check import lines
from startup_compute_request_check import DATA, prepare
from startup_compute_translate_check import state_a
Expand Down Expand Up @@ -148,6 +150,54 @@ def store(directory, consumer, dxc, env):
f'untouched before translation, without a Dispatch and for a refused view; debug layer: {layer}')


def store_3d1d(directory, consumer, dxc, env, form):
cache, base = prepare(directory)
env = dict(base, **env)
(directory / 'compute_root').write_bytes(C.root_blob(3, table_range=True))
env['X2W_TEST_COMPUTE_ROOT'] = str(directory / 'compute_root')
(directory / 'program').write_bytes(V.program(form))
(directory / 'data').write_bytes(DATA)
(directory / 'state').write_bytes(state_a(V.group(form), V.A.RSRC2))

volume = form != 'line'
mode = 'texture-volume' if volume else 'texture-line'
width, height, depth = 32, 8 if volume else 1, 6 if volume else 1
plane_words = width * height * 4
initial = [0xbf000000 | (z << 12) | i for z in range(depth) for i in range(plane_words)]
expected = initial.copy()
for y in range(V.group(form)[1]):
# The 3D view starts at W slice 1 and the shader writes relative z=1.
start = (2 * plane_words if volume else 0) + y * width * 4
expected[start:start + 32] = V.VALUES * 8
layout = '<%dI' % len(initial)
got, result = run(directory, consumer, env, mode, layout)
assert got == initial and len(lines(result, 'Dispatch(2, 1, 1) refused: no translation')) == 1
key = int(lines(result, 'x2w-draw compute request')[0].split()[3], 16)
translate(cache, dxc, key)
manifest = (cache / f'{key:016x}.txt').read_text().splitlines()
dim = '3d' if volume else '1d'
assert f'uav 0 tex{dim} float table 1 64 channels 4' in manifest, manifest
got, result = run(directory, consumer, env, mode, layout)
assert len(lines(result, 'Dispatch(2, 1, 1) translated')) == 1, result.stderr[-3000:]
assert got == expected, (form, sum(a != b for a, b in zip(got, expected, strict=True)))
clean(result)

mismatch = initial[:32 * 8 * 4] if volume else [0xbf000000 | i for i in range(32 * 8 * 4)]
cases = [('texture-array-2d', mismatch, f'an output texture view or resource is not {dim.upper()}')]
if volume:
# Manual mutant: skipping the mip-relative W-range check must fail the required refusal.
cases.append(('texture-volume-bad-w', initial, 'an output 3D texture W range is outside the mip'))
else:
# Manual mutant: dropping the 1D mip check before `created` must fail this required refusal.
cases.append(('texture-line-bad-mip', initial, 'an output 1D texture mip is outside the resource'))
for bad_mode, untouched, reason in cases:
got, result = run(directory, consumer, env, bad_mode, '<%dI' % len(untouched))
refusal = lines(result, 'Dispatch(2, 1, 1) refused:')
assert len(refusal) == 1 and refusal[0].endswith(reason), result.stderr[-3000:]
assert got == untouched and not lines(result, 'Dispatch(2, 1, 1) translated')
print(f'{form}: {len(expected)} mip-one words match; incompatible views refused with sentinels intact')


# v_xor_b32 v5, v5, v12: the assembled `v_xor_b32 v5, v5, v6` of case A with its second source register changed
# (not assembled by llvm-mc here; the translation below is checked to read it as written).
XOR = 'v_xor_b32 v5, v5, v12'
Expand Down Expand Up @@ -269,6 +319,8 @@ def main(consumer, dxc, adapter):
env['X2W_TEST_DISPATCH_HARDWARE'] = '1'
with tempfile.TemporaryDirectory() as temporary:
store(Path(temporary) / 'store', consumer, dxc, env)
for form in V.FORMS:
store_3d1d(Path(temporary) / form, consumer, dxc, env, form)
sample(Path(temporary) / 'sample', consumer, dxc, env)
filtered(Path(temporary) / 'filtered', consumer, dxc, env)
print(f'startup_compute_dispatch_texture: PASS on {adapter}')
Expand Down
Loading
Loading