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
11 changes: 11 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1380,6 +1380,7 @@ foreach(mutation IN ITEMS sampler-alone bind-unrestorable)
set_tests_properties(startup_heap_indexed_root_${mutation}_control PROPERTIES WILL_FAIL TRUE)
endforeach()
target_sources(offscreen_check PRIVATE tests/startup_image_3d.cpp)
target_sources(offscreen_check PRIVATE tests/startup_image_store.cpp)
target_sources(offscreen_check PRIVATE tests/startup_flat_param.cpp)
# The storage root handed to the executable ends in a separator and is never cut off
# (src/startup_stubs/storage_root.h); the mutated run is the negative control.
Expand Down Expand Up @@ -2693,6 +2694,16 @@ if(EXISTS "${INTAKE_PYTHON}")
"$<TARGET_FILE:offscreen_check>" ${adapter})
set_tests_properties(startup_image_load_mip_${adapter} PROPERTIES TIMEOUT 120 SKIP_RETURN_CODE 77)
endforeach()
# gfx2hlsl pixel-stage image_store (#1996 item F): translation controls and refusals, then a desktop draw
# that reads every texel of the stored image back.
add_test(NAME startup_pixel_image_store_controls COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_SOURCE_DIR}/tests/startup_pixel_image_store_check.py")
foreach(adapter IN ITEMS warp hardware)
add_test(NAME startup_pixel_image_store_${adapter} COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_SOURCE_DIR}/tests/startup_pixel_image_store_check.py" draw "${DXC_EXECUTABLE}"
"$<TARGET_FILE:offscreen_check>" ${adapter})
set_tests_properties(startup_pixel_image_store_${adapter} PROPERTIES TIMEOUT 120 SKIP_RETURN_CODE 77)
endforeach()
# gfx2hlsl 2D array images: translation controls, then a desktop draw that names the slice's texel.
add_test(NAME startup_image_2d_array_controls COMMAND "${INTAKE_PYTHON}" -B
"${CMAKE_SOURCE_DIR}/tests/startup_image_2d_array_check.py")
Expand Down
11 changes: 11 additions & 0 deletions docs/startup-runtime.md
Original file line number Diff line number Diff line change
Expand Up @@ -1492,6 +1492,17 @@ These are measured on one executable; treat them as hypotheses for others.
a `float3` of the three address registers in order; 1D images are refused. A source-built pixel
stage draws one texel of a 2x2x2 texture on WARP and hardware with DXC and FXC
(`ctest --preset release --no-tests=error -R "^startup_image_3d_"`).
- **Pixel-stage image stores.** In a pixel stage, `image_store` with `dim:SQ_RSRC_IMG_2D` and
`dmask:0xf` becomes a typed store to a `RWTexture2D<float4>` at its own `u` register, under the
lane's exec bit, and the manifest becomes version 2 with a
`uav <index> tex2d table <table> <offset>` line. The cache-policy bits are accepted. A partial
channel mask (a typed store writes all four channels), other dimensions, `d16`, `a16`, `tfe`,
`lwe`, `r128`, `unorm`, `image_store_mip`, a store outside a pixel stage, and a program that loads
and stores the same image are refused by name. The runtime does not bind UAVs on the translated
graphics path yet: it logs and does not use a translation whose manifest has a `uav` line. A
hand-assembled program stores four distinct values at texel (1, 2). 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`.
- **Explicit mip loads.** For 2D textures, 2D arrays and 3D textures, `image_load_mip` passes the final
address register to `Texture.Load` as the mip level (`int4(x, y, slice, mip)` for an array, whose four
registers are x, y, slice and mip). Ordinary `image_load` still reads mip zero.
Expand Down
6 changes: 6 additions & 0 deletions src/startup_stubs/d3d12_draw.inl
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,12 @@ void load_translation(Translation& tr, const std::string& rtv_request = {})
log("x2w-draw invalid d16srv manifest line for %016llx: %s", tr.key, line.c_str());
return;
}
} else if (line.compare(0, 4, "uav ") == 0) {
// tools/startup/gfx2hlsl.py lowers a pixel-stage image_store to a u-register store, but the
// translated root signature has no UAV range and no view is made from the title's descriptor yet.
log("x2w-draw pipeline %016llx stores to an image (%s); pixel-stage UAVs are not bound yet, translation not used",
tr.key, line.c_str());
return;
} else if (!tr.samplers.read_manifest_line(line.c_str())) {
log("x2w-draw invalid sampler heap manifest for %016llx", tr.key);
return;
Expand Down
2 changes: 2 additions & 0 deletions tests/desktop_gpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ void check_startup_image_3d(ID3D12Device* device, const char* vertex_path, const
bool one_dimensional = false, bool two_dimensional = false);
void check_startup_image_load_d16(ID3D12Device* device, const char* vertex_path, const char* pixel_path,
bool depth16, std::uint32_t words[64]);
int check_startup_image_store(ID3D12Device* device, const char* vertex_path, const char* pixel_path, UINT x, UINT y,
const std::uint32_t expected[4]);
void check_startup_flat_param(ID3D12Device* device, const char* vertex_path, const char* pixel_path,
std::uint32_t* words, UINT width = 1, UINT height = 1);
void dump_raw_uav(ID3D12Device* device, const char* shader_path, const char* groups,
Expand Down
15 changes: 15 additions & 0 deletions tests/offscreen_check.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1260,6 +1260,20 @@ OperationResult run_startup_image_load_d16(const OperationContext& ctx)
return {OutcomeKind::pass, nullptr};
}

// --startup-image-store <vs.cso> <ps.cso> <x,y> <expected rgba bits>: the translated pixel stage stores to
// u0 (tests/startup_image_store.cpp); texel (x, y) must hold the four hex words and every other texel -1.0.
OperationResult run_startup_image_store(const OperationContext& ctx)
{
unsigned x = 0, y = 0;
std::uint32_t expected[4]{};
if (std::sscanf(ctx.argv[5], "%u,%u", &x, &y) != 2) return {OutcomeKind::fail, "texel is not x,y"};
if (std::sscanf(ctx.argv[6], "%8x,%8x,%8x,%8x", &expected[0], &expected[1], &expected[2], &expected[3]) != 4)
return {OutcomeKind::fail, "expected value is not four hex words"};
if (check_startup_image_store(ctx.device, ctx.argv[3], ctx.argv[4], x, y, expected))
return {OutcomeKind::fail, "image store readback differs"};
return {OutcomeKind::pass, nullptr};
}

OperationResult run_startup_packed_position(const OperationContext& ctx)
{
constexpr UINT width = 13, height = 7;
Expand Down Expand Up @@ -1450,6 +1464,7 @@ const OperationSpec kOperations[] = {
{"--startup-image-1d", 7, 8, nullptr, kNoGate, run_startup_image_3d},
{"--startup-image-2d", 7, 8, nullptr, kNoGate, run_startup_image_3d},
{"--startup-image-3d", 7, 8, nullptr, kNoGate, run_startup_image_3d},
{"--startup-image-store", 7, 7, nullptr, kNoGate, run_startup_image_store},
{"--startup-image-2d-array", 7, 8, nullptr, kNoGate, run_startup_image_3d},
{"--startup-image-2d-array-mips", 7, 8, nullptr, kNoGate, run_startup_image_3d},
{"--startup-image-load-d16", 7, 7, nullptr, kNoGate, run_startup_image_load_d16},
Expand Down
7 changes: 7 additions & 0 deletions tests/startup_gfx2hlsl_compute_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,14 @@ def patch(words, old, new):

def check_controls():
a = translate('A')
# The compute stage keeps its buffer stores in self.uavs as descriptor key -> u index, while the pixel stage's
# image stores (#2155) are listed by stored_images() through the shared common() and manifest(). A compute
# program must list no image stores: no RWTexture2D declaration and no `uav <i> tex2d` line.
assert a.uavs and a.stored_images() == {}, (a.uavs, a.stored_images())
hlsl, manifest = a.hlsl_cs(), a.manifest()
assert not any(line.startswith('RWTexture2D') for line in hlsl.splitlines()), hlsl
assert manifest.splitlines()[:2] == ['x2w-manifest 2', 'stage cs'], manifest
assert [line for line in manifest.splitlines() if line.startswith('uav ')] == ['uav 0 table 1 16'], manifest
for line in ('[numthreads(2, 3, 4)]', 'void main(uint3 gid : SV_GroupID, uint3 tid : SV_GroupThreadID) {',
' s7 = gid.x;', ' s8 = gid.y;', ' s9 = gid.z;', ' v0 = tid.x;', ' v1 = tid.y;',
' v2 = tid.z;', 'ByteAddressBuffer r0 : register(t0);', 'RWByteAddressBuffer u0 : register(u0);',
Expand Down
166 changes: 166 additions & 0 deletions tests/startup_image_store.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
// Desktop readback for tools/startup/gfx2hlsl.py's pixel-stage image_store (#1996 item F).
//
// The translated pixel shader stores to w0 : register(u0), a RWTexture2D<float4>, and reads its entry
// constants from cbuffer entry : register(b0). This draws one pixel with that shader and a source-built
// full-screen vertex shader into a 4x4 R32G32B32A32_FLOAT texture whose texels all start as -1.0, then
// reads every texel back. The caller names the one texel it expects the store to change and its value;
// every other texel must still hold -1.0.
#include "desktop_gpu.h"

#include <cstdint>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iterator>
#include <stdexcept>
#include <string>
#include <vector>

using Microsoft::WRL::ComPtr;

namespace {
constexpr UINT entry_constants = 4;
constexpr UINT side = 4;
constexpr std::uint32_t untouched = 0xbf800000; // -1.0f

std::vector<char> read_shader(const char* path)
{
std::ifstream file(path, std::ios::binary);
std::vector<char> bytes((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
if (bytes.empty()) throw std::runtime_error(std::string("Cannot read shader ") + path);
return bytes;
}

ComPtr<ID3D12RootSignature> store_root(ID3D12Device* device)
{
D3D12_DESCRIPTOR_RANGE range{D3D12_DESCRIPTOR_RANGE_TYPE_UAV, 1, 0, 0, 0};
D3D12_ROOT_PARAMETER params[2]{};
params[0].ParameterType = D3D12_ROOT_PARAMETER_TYPE_32BIT_CONSTANTS;
params[0].Constants = {0, 0, entry_constants};
params[0].ShaderVisibility = D3D12_SHADER_VISIBILITY_ALL;
params[1].ParameterType = D3D12_ROOT_PARAMETER_TYPE_DESCRIPTOR_TABLE;
params[1].DescriptorTable = {1, &range};
params[1].ShaderVisibility = D3D12_SHADER_VISIBILITY_PIXEL;
const D3D12_ROOT_SIGNATURE_DESC desc{2, params, 0, nullptr, D3D12_ROOT_SIGNATURE_FLAG_NONE};
ComPtr<ID3DBlob> blob, errors;
check(D3D12SerializeRootSignature(&desc, D3D_ROOT_SIGNATURE_VERSION_1, &blob, &errors), "Serialize image store root");
ComPtr<ID3D12RootSignature> root;
check(device->CreateRootSignature(0, blob->GetBufferPointer(), blob->GetBufferSize(), IID_PPV_ARGS(&root)),
"Create image store root");
return root;
}
} // namespace

// Returns 0 when texel (x, y) holds expected and every other texel holds -1.0; otherwise prints the first
// differing texel as "texel_mismatch x=.. y=.. expected=.. actual=.." and returns 1.
int check_startup_image_store(ID3D12Device* device, const char* vertex_path, const char* pixel_path, UINT x, UINT y,
const std::uint32_t expected[4])
{
if (x >= side || y >= side) throw std::runtime_error("Expected a texel inside the 4x4 image");
const auto vs = read_shader(vertex_path);
const auto ps = read_shader(pixel_path);
const auto root = store_root(device);

D3D12_GRAPHICS_PIPELINE_STATE_DESC pd{};
pd.pRootSignature = root.Get();
pd.VS = {vs.data(), vs.size()};
pd.PS = {ps.data(), ps.size()};
pd.BlendState.RenderTarget[0].RenderTargetWriteMask = D3D12_COLOR_WRITE_ENABLE_ALL;
pd.SampleMask = UINT_MAX;
pd.RasterizerState.FillMode = D3D12_FILL_MODE_SOLID;
pd.RasterizerState.CullMode = D3D12_CULL_MODE_NONE;
pd.RasterizerState.DepthClipEnable = TRUE;
pd.PrimitiveTopologyType = D3D12_PRIMITIVE_TOPOLOGY_TYPE_TRIANGLE;
pd.NumRenderTargets = 1;
pd.RTVFormats[0] = DXGI_FORMAT_R32G32B32A32_FLOAT;
pd.SampleDesc.Count = 1;
ComPtr<ID3D12PipelineState> pipeline;
check(device->CreateGraphicsPipelineState(&pd, IID_PPV_ARGS(&pipeline)), "Create image store PSO");

D3D12_RESOURCE_DESC td{};
td.Dimension = D3D12_RESOURCE_DIMENSION_TEXTURE2D;
td.Width = side;
td.Height = side;
td.DepthOrArraySize = 1;
td.MipLevels = 1;
td.Format = DXGI_FORMAT_R32G32B32A32_FLOAT;
td.SampleDesc.Count = 1;
td.Flags = D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS;
const auto image = resource(device, td, D3D12_HEAP_TYPE_DEFAULT, D3D12_RESOURCE_STATE_COPY_DEST);
D3D12_PLACED_SUBRESOURCE_FOOTPRINT footprint{};
UINT64 bytes = 0;
device->GetCopyableFootprints(&td, 0, 1, 0, &footprint, nullptr, nullptr, &bytes);
const auto upload = make_buffer(device, bytes, D3D12_HEAP_TYPE_UPLOAD, D3D12_RESOURCE_STATE_GENERIC_READ);
const auto readback = make_buffer(device, bytes, D3D12_HEAP_TYPE_READBACK, D3D12_RESOURCE_STATE_COPY_DEST);
void* mapped = nullptr;
const D3D12_RANGE none{0, 0};
check(upload->Map(0, &none, &mapped), "Map image store upload");
for (UINT row = 0; row < side; ++row)
for (UINT word = 0; word < side * 4; ++word)
std::memcpy(static_cast<std::uint8_t*>(mapped) + footprint.Offset + row * footprint.Footprint.RowPitch + 4 * word,
&untouched, 4);
upload->Unmap(0, nullptr);

D3D12_RESOURCE_DESC rd = td;
rd.Width = rd.Height = 1;
rd.Flags = D3D12_RESOURCE_FLAG_ALLOW_RENDER_TARGET;
const D3D12_CLEAR_VALUE clear{DXGI_FORMAT_R32G32B32A32_FLOAT, {0.0f, 0.0f, 0.0f, 0.0f}};
const auto target = resource(device, rd, D3D12_HEAP_TYPE_DEFAULT, D3D12_RESOURCE_STATE_RENDER_TARGET, &clear);

D3D12_DESCRIPTOR_HEAP_DESC hd{D3D12_DESCRIPTOR_HEAP_TYPE_CBV_SRV_UAV, 1, D3D12_DESCRIPTOR_HEAP_FLAG_SHADER_VISIBLE};
ComPtr<ID3D12DescriptorHeap> views, rtvs;
check(device->CreateDescriptorHeap(&hd, IID_PPV_ARGS(&views)), "Create image store view heap");
device->CreateUnorderedAccessView(image.Get(), nullptr, nullptr, views->GetCPUDescriptorHandleForHeapStart());
hd = {D3D12_DESCRIPTOR_HEAP_TYPE_RTV, 1, D3D12_DESCRIPTOR_HEAP_FLAG_NONE};
check(device->CreateDescriptorHeap(&hd, IID_PPV_ARGS(&rtvs)), "Create image store RTV heap");
const auto rtv = rtvs->GetCPUDescriptorHandleForHeapStart();
device->CreateRenderTargetView(target.Get(), nullptr, rtv);

ComputeCommands commands(device, pipeline.Get(), D3D12_COMMAND_LIST_TYPE_DIRECT);
auto* list = commands.list.Get();
D3D12_TEXTURE_COPY_LOCATION texture_location{image.Get(), D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX};
D3D12_TEXTURE_COPY_LOCATION upload_location{upload.Get(), D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT};
upload_location.PlacedFootprint = footprint;
list->CopyTextureRegion(&texture_location, 0, 0, 0, &upload_location, nullptr);
transition(list, image.Get(), D3D12_RESOURCE_STATE_COPY_DEST, D3D12_RESOURCE_STATE_UNORDERED_ACCESS);
list->ClearRenderTargetView(rtv, clear.Color, 0, nullptr);
list->SetGraphicsRootSignature(root.Get());
ID3D12DescriptorHeap* heaps[]{views.Get()};
list->SetDescriptorHeaps(1, heaps);
const std::uint32_t constants[entry_constants] = {0, 0, 0, 0}; // ud0 = 0: plain triangles
list->SetGraphicsRoot32BitConstants(0, entry_constants, constants, 0);
list->SetGraphicsRootDescriptorTable(1, views->GetGPUDescriptorHandleForHeapStart());
const D3D12_VIEWPORT viewport{0, 0, 1, 1, 0, 1};
const D3D12_RECT scissor{0, 0, 1, 1};
list->RSSetViewports(1, &viewport);
list->RSSetScissorRects(1, &scissor);
list->OMSetRenderTargets(1, &rtv, FALSE, nullptr);
list->IASetPrimitiveTopology(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST);
list->DrawInstanced(3, 1, 0, 0);
transition(list, image.Get(), D3D12_RESOURCE_STATE_UNORDERED_ACCESS, D3D12_RESOURCE_STATE_COPY_SOURCE);
D3D12_TEXTURE_COPY_LOCATION readback_location{readback.Get(), D3D12_TEXTURE_COPY_TYPE_PLACED_FOOTPRINT};
readback_location.PlacedFootprint = footprint;
list->CopyTextureRegion(&readback_location, 0, 0, 0, &texture_location, nullptr);
commands.submit_and_wait(device);

const D3D12_RANGE read{0, static_cast<SIZE_T>(bytes)};
check(readback->Map(0, &read, &mapped), "Map image store readback");
int result = 0;
for (UINT ty = 0; ty < side && !result; ++ty)
for (UINT tx = 0; tx < side && !result; ++tx) {
std::uint32_t texel[4];
std::memcpy(texel, static_cast<const std::uint8_t*>(mapped) + footprint.Offset + ty * footprint.Footprint.RowPitch +
16 * tx, sizeof(texel));
const bool stored = tx == x && ty == y;
const std::uint32_t want[4] = {stored ? expected[0] : untouched, stored ? expected[1] : untouched,
stored ? expected[2] : untouched, stored ? expected[3] : untouched};
if (std::memcmp(texel, want, sizeof(texel)) != 0) {
std::fprintf(stderr, "texel_mismatch x=%u y=%u expected=%08x,%08x,%08x,%08x actual=%08x,%08x,%08x,%08x\n",
tx, ty, want[0], want[1], want[2], want[3], texel[0], texel[1], texel[2], texel[3]);
result = 1;
}
}
readback->Unmap(0, &none);
if (!result) std::printf("image store: texel (%u, %u) written, %u others untouched\n", x, y, side * side - 1);
return result;
}
Loading
Loading