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
55 changes: 40 additions & 15 deletions src/startup_stubs/d3d12_draw.inl
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@
// buffers named by the hardware descriptors in its tables, textures behind view tokens) into an
// upload ring / a private descriptor heap.

// A pixel-stage image store (#1996 item F): tools/startup/gfx2hlsl.py writes 'uav N tex2d table P O' for the
// RWTexture2D<float4> at u<N>, whose descriptor is at byte O of the title's root table P. Only that form is
// read; a view qualifier or a 'channels' field, as in the compute line, is refused.
// 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
// root table P. Only that form is read; a view qualifier or a 'channels' field, as in the compute line, is refused.
struct PixelUav {
unsigned index = 0, parameter = 0, offset = 0;
bool array = false;
};
constexpr unsigned pixel_uav_limit = 8;

Expand All @@ -34,7 +35,8 @@ const char* read_pixel_uav_line(const std::string& line, PixelUav& out)
if (w.size() < 2 || w[0] != "uav" || !number(w[1], out.index)) return "malformed view index";
if (w.size() < 3) return "missing view kind";
if (w[2] == "raw" || w[2] == "table") return "buffer view (raw uav) is not bound";
if (w[2] != "tex2d") return "only tex2d views are bound";
if (w[2] != "tex2d" && w[2] != "tex2da") return "only tex2d and tex2da views are bound";
out.array = w[2] == "tex2da";
if (w.size() > 3 && (w[3] == "float" || w[3] == "uint" || w[3] == "sint")) return "a view qualifier is not read for pixel stores";
if (w.size() < 4 || w[3] != "table") return "view source is not a table";
if (w.size() < 6) return "missing table parameter or offset";
Expand Down Expand Up @@ -84,18 +86,29 @@ bool srgb_uav_format(DXGI_FORMAT format)
format == DXGI_FORMAT_B8G8R8X8_UNORM_SRGB;
}

// The view of mip 0 of a 2D texture for a pixel UAV. resource is the desktop description, title_view the title's
// own view (nullptr when it passed none) with console format values already mapped. Returns nullptr and fills
// out, or the reason the binding is refused.
// The view of mip 0 of a 2D or 2D-array texture for a pixel UAV. resource is the desktop description, title_view
// the title's own view (nullptr when it passed none) with console format values already mapped, and array is true
// for a manifest 'tex2da' view. Returns nullptr and fills out, or the reason the binding is refused.
const char* pixel_uav_view(ID3D12Device* device, const D3D12_RESOURCE_DESC& resource,
const D3D12_UNORDERED_ACCESS_VIEW_DESC* title_view, D3D12_UNORDERED_ACCESS_VIEW_DESC& out)
const D3D12_UNORDERED_ACCESS_VIEW_DESC* title_view, bool array,
D3D12_UNORDERED_ACCESS_VIEW_DESC& out)
{
if (resource.Dimension != D3D12_RESOURCE_DIMENSION_TEXTURE2D) return "resource is not a 2D texture";
if (resource.DepthOrArraySize != 1) return "resource is a texture array";
if (!array && resource.DepthOrArraySize != 1) return "resource is a texture array";
if (resource.SampleDesc.Count != 1) return "resource is multisampled";
if (!(resource.Flags & D3D12_RESOURCE_FLAG_ALLOW_UNORDERED_ACCESS)) return "resource lacks ALLOW_UNORDERED_ACCESS";
DXGI_FORMAT format = resource.Format;
if (title_view) {
if (array) {
if (!title_view || title_view->ViewDimension != D3D12_UAV_DIMENSION_TEXTURE2DARRAY)
return "view is not a 2D array texture view";
const auto& a = title_view->Texture2DArray;
if (a.MipSlice != 0) return "view names a mip other than 0";
if (a.PlaneSlice != 0) return "view names a plane other than 0";
if (a.FirstArraySlice >= resource.DepthOrArraySize || !a.ArraySize ||
a.ArraySize > resource.DepthOrArraySize - a.FirstArraySlice)
return "array view slice range is outside the resource";
if (title_view->Format != DXGI_FORMAT_UNKNOWN) format = title_view->Format;
} else if (title_view) {
if (title_view->ViewDimension != D3D12_UAV_DIMENSION_TEXTURE2D) return "view is not a 2D texture view";
if (title_view->Texture2D.MipSlice != 0) return "view names a mip other than 0";
if (title_view->Texture2D.PlaneSlice != 0) return "view names a plane other than 0";
Expand All @@ -110,7 +123,12 @@ const char* pixel_uav_view(ID3D12Device* device, const D3D12_RESOURCE_DESC& reso
return "format has no typed UAV store on this device";
out = {};
out.Format = format;
out.ViewDimension = D3D12_UAV_DIMENSION_TEXTURE2D;
if (array) {
out.ViewDimension = D3D12_UAV_DIMENSION_TEXTURE2DARRAY;
out.Texture2DArray = title_view->Texture2DArray;
} else {
out.ViewDimension = D3D12_UAV_DIMENSION_TEXTURE2D;
}
return nullptr;
}

Expand Down Expand Up @@ -2080,7 +2098,7 @@ bool resolve_pixel_uavs(const Translation& tr, const Pass& pass, PixelUavBinding
title_view.Format = desktop_format(title_view.Format);
}
const D3D12_RESOURCE_DESC desc = texture->GetDesc();
const char* reason = pixel_uav_view(device_of(the_device), desc, described ? &title_view : nullptr, out[i].view);
const char* reason = pixel_uav_view(device_of(the_device), desc, described ? &title_view : nullptr, uav.array, out[i].view);
if (reason) {
static LONG refused_view;
if (first_calls(refused_view, 16))
Expand Down Expand Up @@ -2539,9 +2557,16 @@ bool draw_translated(Wrapper* self, bool indexed, UINT count, UINT instances, UI
require_state(self, uav_bindings[j].resource, D3D12_RESOURCE_STATE_UNORDERED_ACCESS);
device_of(the_device)->CreateUnorderedAccessView(uav_bindings[j].resource, nullptr, &uav_bindings[j].view, dest);
static LONG shown;
if (first_calls(shown, 6))
log("x2w-draw %016llx uav %u: resource %p format %d mip 0", tr->key, j, static_cast<void*>(uav_bindings[j].resource),
uav_bindings[j].view.Format);
if (first_calls(shown, 6)) {
if (uav_bindings[j].view.ViewDimension == D3D12_UAV_DIMENSION_TEXTURE2DARRAY)
log("x2w-draw %016llx uav %u: resource %p format %d array [%u..%u) mip 0", tr->key, j,
static_cast<void*>(uav_bindings[j].resource), uav_bindings[j].view.Format,
uav_bindings[j].view.Texture2DArray.FirstArraySlice,
uav_bindings[j].view.Texture2DArray.FirstArraySlice + uav_bindings[j].view.Texture2DArray.ArraySize);
else
log("x2w-draw %016llx uav %u: resource %p format %d mip 0", tr->key, j,
static_cast<void*>(uav_bindings[j].resource), uav_bindings[j].view.Format);
}
}
static LONG checks;
if ((InterlockedIncrement(&checks) & 63) == 1) check_device("translated draw views", 0);
Expand Down
63 changes: 59 additions & 4 deletions tests/startup_pixel_image_store_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,32 @@
s_endpgm
"""
STORE = bytes.fromhex('080f20f000040400')
ARRAY_STORE = bytes.fromhex('280f20f000040400')
PREFIX = bytes.fromhex('04040cf4000000fa' '8102007e' '8202027e' 'f202087e' 'f4020a7e' 'ff020c7e00004040'
'f6020e7e' '7fc08cbf')
SUFFIX = bytes.fromhex('703f8cbf' '0f1800f804050607' '000081bf')
WORDS = PREFIX + STORE + SUFFIX
STORE_LINE = 'w0[uint2(v0, v1)] = asfloat(uint4(v4, v5, v6, v7));'
GUARDED_LINE = 'if (exec & 1) ' + STORE_LINE
ARRAY_SOURCE = SOURCE.replace(
'image_store v[4:7], v[0:1], s[16:23] dmask:0xf dim:SQ_RSRC_IMG_2D',
'v_mov_b32 v2, 1\nimage_store v[4:7], v[0:2], s[16:23] dmask:0xf dim:SQ_RSRC_IMG_2D_ARRAY',
)
ARRAY_STORE_LINE = 'w0[uint3(v0, v1, v2)] = asfloat(uint4(v4, v5, v6, v7));'
ARRAY_GUARDED_LINE = 'if (exec & 1) ' + ARRAY_STORE_LINE


def mov_v2_inline(constant):
"""v_mov_b32_e32 v2, <constant>: VOP1 opcode 0x01, vdst=2, inline integer 0..64 (0x80 + n)."""
return struct.pack('<I', 0x7e040280 + constant)


def array_program(layer):
return PREFIX + mov_v2_inline(layer) + ARRAY_STORE + SUFFIX


ARRAY_WORDS = array_program(1)
ARRAY_WORDS_LAST = array_program(3)


def cmpx_eq(constant):
Expand All @@ -61,6 +81,8 @@ def cmpx_eq(constant):

EXEC_ON = PREFIX + cmpx_eq(1) + STORE + SUFFIX # v0 is 1: the lane stays active
EXEC_OFF = PREFIX + cmpx_eq(2) + STORE + SUFFIX # v0 is 1: every lane is turned off before the store
ARRAY_EXEC_ON = PREFIX + mov_v2_inline(1) + cmpx_eq(1) + ARRAY_STORE + SUFFIX
ARRAY_EXEC_OFF = PREFIX + mov_v2_inline(1) + cmpx_eq(2) + ARRAY_STORE + SUFFIX


def translate(words):
Expand Down Expand Up @@ -106,18 +128,44 @@ def controls():
assert STORE_LINE in translate(with_store(store)).hlsl_ps(), name
print('glc, slc, dlc and unorm accepted')

# 2D-array pixel store (#2742): three address registers, RWTexture2DArray<float4>, manifest tex2da.
array_text = D.decode_instruction(ARRAY_STORE, 0)[2]
assert array_text == 'image_store v[4:7], v[0:2], s[16:23] dmask:0xf dim:SQ_RSRC_IMG_2D_ARRAY', array_text
array_pipe = translate(ARRAY_WORDS)
array_ps, array_manifest = array_pipe.hlsl_ps(), array_pipe.manifest()
assert 'RWTexture2DArray<float4> w0 : register(u0);' in array_ps, array_ps
assert ' ' + ARRAY_STORE_LINE in array_ps, array_ps
assert 'register(t' not in array_ps, array_ps
assert 'uav 0 tex2da table 0 0' in array_manifest and 'srv ' not in array_manifest, array_manifest
assert array_manifest.startswith('x2w-manifest 2\n'), array_manifest
print('array store: %s manifest uav 0 tex2da table 0 0' % ARRAY_STORE_LINE)

# Refusals, each by name: packed data or addresses, fault-status returns, a 128-bit descriptor,
# a partial channel mask and other image dimensions.
cases = (('d16', patched(7, 0x80)), ('a16', patched(7, 0x40)), ('tfe', patched(2, 0x01)),
('lwe', patched(2, 0x02)), ('r128', patched(1, 0x80)), ('reserved', patched(1, 0x40)),
('SQ_RSRC_IMG_3D', bytes.fromhex('100f20f000040400')),
('SQ_RSRC_IMG_2D_ARRAY', bytes.fromhex('280f20f000040400')),
('SQ_RSRC_IMG_CUBE', bytes.fromhex('180f20f000040400')),
('SQ_RSRC_IMG_1D', bytes.fromhex('000f20f000040400')))
for name, store in cases:
decoded = D.decode_instruction(store, 0)[2]
message = refused(with_store(store), 'image_store')
print('refused %-20s %s' % (name + ':', message))
assert decoded.startswith('image_store'), decoded
# 2D-array requires three address registers and still refuses pixel d16.
try:
array_pipe.ps.op_image_store(['v[4:7]', 'v[0:1]', 's[16:23]'], ['dmask:0xf', 'dim:SQ_RSRC_IMG_2D_ARRAY'])
except G.Unsupported as error:
assert 'x, y and layer expected' in str(error), error
else:
raise AssertionError('2D-array store with two address registers was accepted')
try:
array_pipe.ps.op_image_store(['v[4:5]', 'v[0:2]', 's[16:23]'], ['dmask:0xf', 'dim:SQ_RSRC_IMG_2D_ARRAY', 'd16'])
except G.Unsupported as error:
assert 'modifier d16' in str(error), error
else:
raise AssertionError('pixel 2D-array d16 store was accepted')
print('refused 2D-array with two address registers and pixel d16')
# A partial channel mask, encoded: the decoder sizes VDATA from the mask (v[4:6], v4), so each must be
# refused for the mask itself, which the translator checks before the data registers.
for mask, data in ((0x7, 'v[4:6]'), (0x1, 'v4')):
Expand Down Expand Up @@ -177,6 +225,12 @@ def controls():
assert ps.count(STORE_LINE) == 1, ps
# With exec known (the entry mask) the store is not guarded.
assert GUARDED_LINE not in translate(WORDS).hlsl_ps()
for constant, words in ((1, ARRAY_EXEC_ON), (2, ARRAY_EXEC_OFF)):
ps = translate(words).hlsl_ps()
assert ' exec = ((0x%08xu == v0) ? 1u : 0u);' % constant in ps, ps
assert ' ' + ARRAY_GUARDED_LINE in ps, ps
assert ps.count(ARRAY_STORE_LINE) == 1, ps
assert ARRAY_GUARDED_LINE not in array_ps
print('exec after v_cmpx: %s' % GUARDED_LINE)

# Negative control: without the lowering the program is refused as before.
Expand All @@ -190,9 +244,10 @@ def controls():


def check_binary(llvm_mc):
built = assemble(llvm_mc, SOURCE)
if built != WORDS:
raise AssertionError('assembled %s, recorded %s' % (built.hex(), WORDS.hex()))
for label, src, expected in (('2d', SOURCE, WORDS), ('2d_array', ARRAY_SOURCE, ARRAY_WORDS)):
built = assemble(llvm_mc, src)
if built != expected:
raise AssertionError('%s: assembled %s, recorded %s' % (label, built.hex(), expected.hex()))
print('fixture words match llvm-mc')


Expand Down
Loading
Loading