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
8 changes: 8 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1967,6 +1967,14 @@ if(TARGET compile_dxil AND EXISTS "${DXC_BIN_DIR}/dxcompiler.dll" AND EXISTS "${
add_test(NAME native_direct_container
COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_SOURCE_DIR}/tests/native_direct_container_check.py"
"$<TARGET_FILE:compile_dxil>" "${DXC_BIN_DIR}")
foreach(adapter IN ITEMS warp hardware)
foreach(case IN ITEMS r rg rgb array volume rgba sparse)
add_test(NAME native_image_store_exact_width_${adapter}_${case}
COMMAND "${INTAKE_PYTHON}" -B "${CMAKE_SOURCE_DIR}/tests/native_image_store_exact_width_check.py"
"$<TARGET_FILE:compile_dxil>" "${DXC_BIN_DIR}" "$<TARGET_FILE:startup_stream_consumer>" ${adapter} ${case})
set_tests_properties(native_image_store_exact_width_${adapter}_${case} PROPERTIES TIMEOUT 90 SKIP_RETURN_CODE 77)
endforeach()
endforeach()
add_test(NAME native_dpp_mirror_hardware COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_SOURCE_DIR}/tests/native_dpp_mirror_check.py" "$<TARGET_FILE:compile_dxil>"
"$<TARGET_FILE:native_store_replay>" "$<TARGET_FILE:offscreen_check>" "${DXC_BIN_DIR}")
Expand Down
26 changes: 26 additions & 0 deletions docs/experiments/direct-compute-exact-width-image-store.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Exact-width image stores in direct compute

The direct gfx1013 compute translator lowers non-D16 `image_store` with
`dmask:0x1`, `0x3`, or `0x7` on 2D, 2D array, and 3D images. It consumes
one, two, or three consecutive VDATA VGPRs and declares a typed float,
float2, or float3 UAV. The DXIL store uses the full typed-store mask.
The narrower resource type determines which components reach the texture.
The `unorm` instruction modifier continues to leave the integer texel
coordinates unchanged.

Manifest version 2 describes these outputs with
`uav <index> <tex2d|tex2da|tex3d> float table <parameter> <byte> channels <1|2|3>`.
The in-process path gets the same width from the translated binding.
The runtime requires the view and resource formats to have the specified
component count and checks adapter support for typed stores on the resource
format. It creates the UAV with that format and retains the bound subresource
range. This makes each accepted store a whole texel write. Sparse masks still
receive the translator's descriptor modifier refusal.

The readback fixture dispatches two X groups of 4x2x1 threads into a 32x8 mip.
It explicitly sets EXEC to `0x55`, writing columns 0, 2, 4, and 6 in rows
0 and 1. The array and volume cases use relative slice 1 of a view that
starts at physical slice 1. Every other texel retains its sentinel.
The format cases use R32_FLOAT, R32G32_FLOAT, and R11G11B10_FLOAT. The
RGBA8 control leaves the complete image untouched and checks the runtime
channel-count refusal.
7 changes: 6 additions & 1 deletion src/direct_compute_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ NativeComputeShader decode(std::string_view bytes) {
unsigned n = in.count(1000000);
shader.bindings.reserve(n);
for (unsigned j = 0; j < n; ++j) {
auto kind = static_cast<ComputeViewKind>(in.count(static_cast<unsigned>(ComputeViewKind::uint_texture_uav)));
auto kind = static_cast<ComputeViewKind>(in.count(static_cast<unsigned>(ComputeViewKind::float3_texture_uav)));
ComputeViewBinding b{kind, in.word(), in.word(), in.word(), in.word(), in.word(), in.word(), in.count(7)};
b.scalar_table = static_cast<ComputeViewBinding::ScalarTable>(in.count(unsigned(ComputeViewBinding::ScalarTable::srv)));
const bool sampler_heap = kind == ComputeViewKind::heap_sampler;
Expand Down Expand Up @@ -775,6 +775,11 @@ std::optional<std::string> format_compute_runtime_manifest(const NativeComputeSh
} 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";
} else if (const auto channels = exact_texture_uav_channels(b.kind); channels && b.array_count == 1 &&
(b.image_dimension == 1 || b.image_dimension == 2 || b.image_dimension == 5)) {
const char *dimension = b.image_dimension == 2 ? "tex3d" : b.image_dimension == 5 ? "tex2da" : "tex2d";
uav_lines << "uav " << b.shader_register << ' ' << dimension << " float table " << b.root_parameter
<< ' ' << b.byte_offset << " channels " << channels << '\n';
} else if ((b.kind == ComputeViewKind::texture_uav || b.kind == ComputeViewKind::half4_texture_uav) &&
(b.image_dimension == 1 || b.image_dimension == 5) && b.array_count == 1) {
uav_lines << "uav " << b.shader_register << (b.image_dimension == 5 ? " tex2da" : " tex2d")
Expand Down
15 changes: 13 additions & 2 deletions src/gfx1013_compute_native.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,19 @@ enum class ComputeViewKind {
// dynamic_sampler() (LOD clamp applied, valid for the explicit-LOD forms translated here); entry 2047 is the runtime
// fallback and never read.
heap_sampler,
uint_texture_uav
uint_texture_uav,
float_texture_uav,
float2_texture_uav,
float3_texture_uav
};
inline unsigned exact_texture_uav_channels(ComputeViewKind kind) {
switch (kind) {
case ComputeViewKind::float_texture_uav: return 1;
case ComputeViewKind::float2_texture_uav: return 2;
case ComputeViewKind::float3_texture_uav: return 3;
default: return 0;
}
}
inline constexpr unsigned compute_resource_heap = ~0u;
// the directly indexed sampler heap; entry n is title byte offset 16 * n
inline constexpr unsigned compute_sampler_heap = ~0u - 1;
Expand All @@ -57,7 +68,7 @@ struct IndexedAtomicView {
inline bool compute_uav(ComputeViewKind kind) {
return kind == ComputeViewKind::texture_uav || kind == ComputeViewKind::half_scalar_uav ||
kind == ComputeViewKind::half2_texture_uav || kind == ComputeViewKind::half4_texture_uav ||
kind == ComputeViewKind::uint_texture_uav || compute_buffer_uav(kind);
kind == ComputeViewKind::uint_texture_uav || exact_texture_uav_channels(kind) || compute_buffer_uav(kind);
}
struct ComputeViewBinding {
ComputeViewKind kind;
Expand Down
9 changes: 7 additions & 2 deletions src/gfx1013_direct_compute.inl
Original file line number Diff line number Diff line change
Expand Up @@ -840,14 +840,16 @@ struct DirectCompute {
constexpr unsigned dx_kind[]{1, 2, 4, 5, 6, 7, 3, 8};
constexpr unsigned coord_count[]{1, 2, 3, 3, 2, 3, 2, 3};
const unsigned coords = coord_count[dim];
const bool exact_store = store && !half && !graphics_input && (dim == 1 || dim == 2 || dim == 5) &&
(mask == 1 || mask == 3 || mask == 7);
if ((i.op != 0 && i.op != 1 && !store && !sample && !gather && !gather_lod && !dimensions) || !mask || (i.a & 0x3e0c0u) ||
(i.b & 0x3c000000u) || (gather && ((mask & (mask - 1)) || (dim != 1 && dim != 3 && dim != 5))) ||
(query && ((dim != 1 && dim != 3) || (mask & ~3u))) ||
(compare && (mask != 1 || (dim != 1 && dim != 3 && dim != 5) || (i.a & 0x1000u))) ||
(compare_gather && dim == 3) ||
((sample || gather) && (i.a & 0x1000u) && gradient) ||
(dimensions && (dim == 4 || dim >= 6 || (i.b & 0x40000000u))) ||
(store && !half && mask != 15) || (half && mask != 1 && mask != 3 && mask != 15) ||
(store && !half && mask != 15 && !exact_store) || (half && mask != 1 && mask != 3 && mask != 15) ||
s.at(base).type != Symbol::Type::descriptor)
refuse(i.pc, "direct image opcode or descriptor modifiers");
if (gather_lod && (dim != 1 || (mask & (mask - 1)) || (i.a & 0x1006u) || (i.b & 0xc0000000u) || i.dst > 252))
Expand All @@ -856,10 +858,13 @@ struct DirectCompute {
(dim >= 6 && (sample || gather || store || lod)))
refuse(i.pc, "unsupported cube or multisample operation");
const bool d16_sample = !store && (i.b >> 31);
const unsigned channels = half && mask == 1 ? 1 : half && mask == 3 ? 2 : 4;
const unsigned channels = (half || exact_store) && mask == 1 ? 1 :
(half || exact_store) && mask == 3 ? 2 : exact_store ? 3 : 4;
const bool cube_sample = dim == 3 && (sample || gather);
const auto kind = half ? (channels == 1 ? ComputeViewKind::half_scalar_uav :
channels == 2 ? ComputeViewKind::half2_texture_uav : ComputeViewKind::half4_texture_uav)
: exact_store ? (channels == 1 ? ComputeViewKind::float_texture_uav :
channels == 2 ? ComputeViewKind::float2_texture_uav : ComputeViewKind::float3_texture_uav)
: store ? ComputeViewKind::texture_uav
: cube_sample ? (compare ? ComputeViewKind::cube_comparison_srv : ComputeViewKind::cube_sample_srv)
: d16_sample ? ComputeViewKind::d16_sample_srv
Expand Down
60 changes: 44 additions & 16 deletions src/startup_stubs/d3d12_compute_dispatch.inl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// <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 textures
// (`srv <i> tex2d table <param> <byte>` inputs, `uav <i> tex1d/tex2d/tex2da/tex3d float table <param> <byte> channels 4` outputs) named
// (`srv <i> tex2d 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
// 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 Down Expand Up @@ -36,6 +36,7 @@ struct ComputeTranslation {
D3D12_UAV_DIMENSION uav_dimension = D3D12_UAV_DIMENSION_UNKNOWN;
bool root = false;
bool atomic_uint_texture = false;
unsigned channels = 4;
};
u64 key = 0;
int state = 0; // 0 not loaded, 1 ready, 2 unavailable
Expand All @@ -60,19 +61,24 @@ struct ComputeTranslation {
}
};

// Formats a `float` typed output may be viewed with: the program stores float4.
bool float_output_format(DXGI_FORMAT format)
// Component counts of the float and normalized formats accepted for typed outputs.
unsigned float_output_channels(DXGI_FORMAT format)
{
switch (format) {
case DXGI_FORMAT_R32G32B32A32_FLOAT: case DXGI_FORMAT_R16G16B16A16_FLOAT: case DXGI_FORMAT_R16G16B16A16_UNORM:
case DXGI_FORMAT_R16G16B16A16_SNORM: case DXGI_FORMAT_R32G32_FLOAT: case DXGI_FORMAT_R10G10B10A2_UNORM:
case DXGI_FORMAT_R11G11B10_FLOAT: case DXGI_FORMAT_R8G8B8A8_UNORM: case DXGI_FORMAT_R8G8B8A8_SNORM:
case DXGI_FORMAT_R16G16_FLOAT: case DXGI_FORMAT_R16G16_UNORM: case DXGI_FORMAT_R16G16_SNORM: case DXGI_FORMAT_R32_FLOAT:
case DXGI_FORMAT_R8G8_UNORM: case DXGI_FORMAT_R8G8_SNORM: case DXGI_FORMAT_R16_FLOAT: case DXGI_FORMAT_R16_UNORM:
case DXGI_FORMAT_R16_SNORM: case DXGI_FORMAT_R8_UNORM: case DXGI_FORMAT_R8_SNORM: case DXGI_FORMAT_B8G8R8A8_UNORM:
return true;
case DXGI_FORMAT_R16G16B16A16_SNORM: case DXGI_FORMAT_R10G10B10A2_UNORM:
case DXGI_FORMAT_R8G8B8A8_UNORM: case DXGI_FORMAT_R8G8B8A8_SNORM: case DXGI_FORMAT_B8G8R8A8_UNORM:
return 4;
case DXGI_FORMAT_R11G11B10_FLOAT:
return 3;
case DXGI_FORMAT_R32G32_FLOAT: case DXGI_FORMAT_R16G16_FLOAT: case DXGI_FORMAT_R16G16_UNORM:
case DXGI_FORMAT_R16G16_SNORM: case DXGI_FORMAT_R8G8_UNORM: case DXGI_FORMAT_R8G8_SNORM:
return 2;
case DXGI_FORMAT_R32_FLOAT: case DXGI_FORMAT_R16_FLOAT: case DXGI_FORMAT_R16_UNORM:
case DXGI_FORMAT_R16_SNORM: case DXGI_FORMAT_R8_UNORM: case DXGI_FORMAT_R8_SNORM:
return 1;
default:
return false;
return 0;
}
}
std::unordered_map<u64, ComputeTranslation>& compute_translations() { static std::unordered_map<u64, ComputeTranslation> m; return m; }
Expand Down Expand Up @@ -186,10 +192,12 @@ const char* load_compute_translation(ComputeTranslation& tr)
tr.uavs.back().uav_dimension = D3D12_UAV_DIMENSION_TEXTURE2D;
tr.uavs.back().atomic_uint_texture = true;
}
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) {
else if (std::sscanf(line.c_str(), "uav %u %15s float table %d %d channels %u%n", &i, name, &a, &b, &x, &used) == 5 &&
whole() && i == tr.uavs.size() && x >= 1 && x <= 4 && uav_dimension() != D3D12_UAV_DIMENSION_UNKNOWN &&
(x == 4 || uav_dimension() != D3D12_UAV_DIMENSION_TEXTURE1D)) {
tr.uavs.push_back({a, b, true});
tr.uavs.back().uav_dimension = uav_dimension();
tr.uavs.back().channels = x;
}
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 Down Expand Up @@ -225,7 +233,7 @@ const char* load_compute_translation(ComputeTranslation& tr)
// X2W_NATIVE_SHADERS=<directory of dxcompiler.dll and dxil.dll>: the first Dispatch of a slot 67 request translates
// it in this process with the C++ compiler (src/gfx1013_compute_native.cpp), before a published <key>.txt is read
// and with no offline translator running. The dispatch path below binds the result, so a program is accepted only
// with root constants, raw table buffers it loads per lane (buffer_load_dword) and raw table buffers it stores to.
// with root constants, raw table buffers and exact-width typed texture outputs.
// A refusal is logged and leaves the key to the published translations. draw_lock held. Returns whether tr is ready.
bool translate_compute_natively(ComputeTranslation& tr)
{
Expand Down Expand Up @@ -254,14 +262,22 @@ bool translate_compute_natively(ComputeTranslation& tr)
std::sort(program.bindings.begin(), program.bindings.end(),
[](const auto& a, const auto& b) { return a.shader_register < b.shader_register; });
for (const auto& view : program.bindings) {
// The two kinds a fixture has run through this path: a raw buffer read per lane, and one written.
const bool output = view.kind == x2w::ComputeViewKind::raw_buffer_uav;
const unsigned channels = x2w::exact_texture_uav_channels(view.kind);
const bool output = channels || view.kind == x2w::ComputeViewKind::raw_buffer_uav;
auto& slots = output ? tr.uavs : tr.srvs;
if ((!output && view.kind != x2w::ComputeViewKind::raw_vector_srv) || view.array_count != 1 ||
view.root_parameter >= 16 || view.shader_register != slots.size())
throw std::runtime_error("view kind " + std::to_string(static_cast<unsigned>(view.kind)) +
" is not bound for a natively translated program");
slots.push_back({static_cast<int>(view.root_parameter), static_cast<int>(view.byte_offset)});
if (channels) {
auto& slot = slots.back();
slot.texture = true;
slot.channels = channels;
slot.uav_dimension = view.image_dimension == 2 ? D3D12_UAV_DIMENSION_TEXTURE3D :
view.image_dimension == 5 ? D3D12_UAV_DIMENSION_TEXTURE2DARRAY :
D3D12_UAV_DIMENSION_TEXTURE2D;
}
}
if (!program.samplers.empty() || tr.uds.size() > 32 || tr.srvs.size() > 32 || tr.uavs.size() > 8)
throw std::runtime_error("a sampler, or more entry values, inputs or outputs than the dispatch path binds");
Expand Down Expand Up @@ -550,7 +566,19 @@ const char* record_translated_dispatch(Wrapper* self, const TranslatedDispatch&
if (!reason && x2w_startup::packed_target_format(format)) reason = "an output texture view has a packed format";
if (!reason && slot.atomic_uint_texture && format != DXGI_FORMAT_R32_UINT)
reason = "an image atomic requires an R32_UINT UAV";
if (!reason && !slot.atomic_uint_texture && !float_output_format(format))
if (!reason && slot.channels < 4) {
if (float_output_channels(format) != slot.channels || float_output_channels(resource.Format) != slot.channels)
reason = "an exact-width image store requires a matching output texture channel count";
else {
D3D12_FEATURE_DATA_FORMAT_SUPPORT support{resource.Format};
if (FAILED(device_of(the_device)->CheckFeatureSupport(D3D12_FEATURE_FORMAT_SUPPORT, &support,
sizeof(support))) ||
!(support.Support2 & D3D12_FORMAT_SUPPORT2_UAV_TYPED_STORE))
reason = "an exact-width image store requires typed UAV store support";
desc.Format = resource.Format;
}
}
if (!reason && !slot.atomic_uint_texture && !float_output_channels(format))
reason = "an output texture format is not a float or normalized one";
// One state per resource here: a texture read and written by the same call is refused. Two outputs
// of one texture (two mips) share the unordered access state.
Expand Down
Loading
Loading