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
20 changes: 19 additions & 1 deletion docs/d3d12_vulkan.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ The design is therefore an **IR per command list**:
- `Close` freezes the IR. `ExecuteBundle` is accepted only on a direct list, with an already-closed bundle as its callee; the bundle's recorded commands and calls are replayed at the call site, so the source can be reused or reset without changing the parent. Command-list creation and reset require an allocator of the same type. A bundle cannot be submitted directly to a command queue.
- Bundle restrictions are checked while recording for the IR operations currently implemented here (barriers, clears, copies, resolves, `SetPredication`, `DiscardResource`, the atomic copies, `RSSetViewports`, `RSSetScissorRects`, `OMSetRenderTargets` and `ClearState`; `WriteBufferImmediate` is allowed in a bundle); those calls are not legal inside a D3D12 bundle and make `Close` return `E_INVALIDARG`. `d3d12_bundle_forbidden_test` measured the last four on the PC runtime without the debug layer: the bundle's `Close` fails (`E_FAIL`, where this layer gives `E_INVALIDARG`) on the default adapter and on WARP, and executing a bundle whose `Close` failed crashes the runtime, so the layer refuses it in `ExecuteBundle` instead. A bundle has no render targets, viewport or root signature of its own, so its state calls (pipeline state, root signature and arguments, vertex and index buffers, topology, blend factor, stencil references) and its draws and dispatches are kept as they were called, and `ExecuteBundle` plays them on the parent through the same members the application would have called. A draw then snapshots the parent's state plus what the bundle set, and what the bundle set stays on the parent afterwards. Commands that need no state (markers, immediate writes) are copied between them in order. `d3d12_bundle_test` measured the rule on the PC runtime, where the default adapter (an RTX 4060 Ti) and WARP drew the same picture in every case it ran. A bundle's draws see the parent's render targets, viewport and scissor, root signature and arguments (also when the bundle sets the same root signature first, so those arguments stay), and vertex and index buffers. A bundle starts with the pipeline state it was created or reset with, not the parent's. After `ExecuteBundle` the parent has that pipeline state and whatever else the bundle set: topology, root arguments, vertex and index buffers, and a different root signature with the bundle's arguments in place of the parent's. Topology: the default adapter takes the parent's when the bundle sets none. That is more lenient than Microsoft's stated rule (topology is undefined when a bundle begins), and WARP follows the rule by removing the device when a bundle draws or hands back without one, so a bundle should set it; the layer follows the default adapter. A bundle that sets a root argument before any root signature crashes the NVIDIA driver (the layer applies it to the parent's signature). The deferred calls are checked when they play, against the parent's root signature, so a bad one makes the parent's `Close` fail, not the bundle's. A per-list state setter must go through the deferred path (`defer()` in `d3d12_com.cpp`, replaying through a private member that does not trace) to work in a bundle. Not deferred yet: `ExecuteIndirect` in a bundle, and bundle `Dispatch` has no GPU test.
- What `Close` returns after one recorded call is compared with the PC runtime over WARP (no debug layer) by
`d3d12_close_parity_test`, 113 cases on fresh lists, nothing executed. 82 agree; the other 31 are listed in the
`d3d12_close_parity_test`, 116 cases on fresh lists, nothing executed. 85 agree (three of them enhanced texture
barriers, two into `LAYOUT_UNDEFINED`: section 4.3); the other 31 are listed in the
test as known differences. In 16 the layer fails a list WARP closes: draws and dispatches with no pipeline,
root arguments before a root signature, a `CopyTextureRegion` box that is empty or inverted, footprint to
footprint or at an unaligned footprint offset or pitch, `CopyResource` of a resource onto itself,
Expand Down Expand Up @@ -2730,6 +2731,23 @@ mapped stages/accesses, and if no single layout serves all uses, use `GENERAL` a
`ResolveQueryData`'s never-written queries as `vkCmdFillBuffer`, which synchronization2 runs in the `CLEAR` stage
(D3D-838; `vk_command_replay_test` part K is a read-after-write hazard under synchronization validation without
it).
- `LayoutAfter = UNDEFINED` from a defined layout (D3D-BARRIER-INTO-UNDEFINED) releases the subresources: their
contents are no longer needed, as before their memory is aliased. Vulkan has no transition into `UNDEFINED`
(VUID-VkImageMemoryBarrier2-newLayout-01198), so each piece gets a barrier with `oldLayout = newLayout`, the
layout the tracker has for it: the accesses are ordered and nothing in the image changes. The tracker keeps those
layouts. A piece never used yet gets no barrier and is not zero-filled for it. The next barrier on these
subresources has to start from `UNDEFINED`, which discards from whatever layout they are in, as before. The list
closes, as it does on WARP (measured on Windows 11 26340 in #984). #984 also reports that the retail title ends a
depth buffer's use this way and takes a failed `Close` as fatal; the layer failed such a list's `Close` until
this change. `UNDEFINED` to `UNDEFINED` is a separate case (#600).
- **Tests:** `command_list_test` and `replay_texture_barrier_test` (CPU: the barriers recorded, the kept layouts,
an unused image, the barrier that follows); three rows of `d3d12_close_parity_test`; `vk_command_replay_test`
part L (a colour target, a depth buffer whose planes are in two layouts and an unused image released, the target
used again and read back); and `d3d12_com_test`, where a depth buffer is cleared, released, taken from
`UNDEFINED` again, cleared and read back through the D3D12 list and queue (16 depth and 16 stencil texels
exact). Run on an RTX 4070 Ti (NVIDIA 620.70.06, Linux): part L natively with the validation layer 1.4.363 and
no error, where the same barriers with `newLayout = UNDEFINED` give VUID-VkImageMemoryBarrier2-newLayout-01198
four times; the D3D12 tests under Wine.
- `DiscardResource` (D3D-26): each discarded image subresource gets a barrier from `oldLayout = UNDEFINED` to the
layout the tracker already has for it, so the driver may drop the contents while later commands see no layout
change. The other cases are no-ops, which "contents undefined" allows: buffers, discards with rectangles (only
Expand Down
5 changes: 3 additions & 2 deletions runtime/src/command_list.h
Original file line number Diff line number Diff line change
Expand Up @@ -698,10 +698,11 @@ class CommandList {
return append(barrier);
}

// LayoutAfter UNDEFINED is legal: the subresources' contents are no longer needed, as before their memory is
// aliased (the retail title ends a depth buffer's use this way, #984; WARP closes such a list,
// d3d12_close_parity_test). Replay keeps the image's Vulkan layout and orders the accesses only.
bool barrier(const TextureBarrier& barrier) {
if (!barrier.resource || !valid_sync(barrier.sync_before, barrier.sync_after)) return false;
if (barrier.layout_after == d3d12_layout::undefined && barrier.layout_before != d3d12_layout::undefined)
return false;
if (barrier.flags & ~texture_barrier_discard) return false;
if (barrier.range.mip_count && (!barrier.range.layer_count || !barrier.range.plane_count)) return false;
if (barrier.sync_after == d3d12_sync::split) return !closed_;
Expand Down
27 changes: 19 additions & 8 deletions runtime/src/vulkan_replay.h
Original file line number Diff line number Diff line change
Expand Up @@ -3619,16 +3619,23 @@ inline void emit(const PreparedList& prepared, VkCommandBuffer command_buffer, c
const auto range = detail::planes_for_layout(p.binding, p.range, separate);
const bool discard = (texture->flags & texture_barrier_discard) ||
texture->layout_before == d3d12_layout::undefined;
// LayoutAfter UNDEFINED from a defined layout: the contents are no longer needed (the memory is about to
// be aliased, say). Vulkan has no transition into UNDEFINED (VUID-VkImageMemoryBarrier2-newLayout-01198),
// so each piece keeps the layout it is in and the barrier only orders the accesses; the tracker keeps
// that layout too. D3D12 has the next barrier on these subresources start from UNDEFINED, which discards
// from whatever layout they are in.
const bool release = texture->layout_after == d3d12_layout::undefined &&
texture->layout_before != d3d12_layout::undefined;
const VkImageLayout new_layout =
image_layout(p.binding, vulkan_layout(texture->layout_after, p.binding.presentable));
LayoutPieces pieces;
if (discard)
if (discard && !release)
pieces.push_back({range, VK_IMAGE_LAYOUT_UNDEFINED});
else {
const VkImageLayout before =
image_layout(p.binding, vulkan_layout(texture->layout_before, p.binding.presentable));
pieces = detail::current(tracker, id, range, before);
detail::initialize(vk, command_buffer, p.binding, id, pieces, before, tracker);
if (!release) detail::initialize(vk, command_buffer, p.binding, id, pieces, before, tracker);
}
const BarrierScope src = vulkan_barrier_scope(texture->sync_before, texture->access_before, predicates);
const BarrierScope dst = vulkan_barrier_scope(texture->sync_after, texture->access_after, predicates);
Expand All @@ -3639,26 +3646,30 @@ inline void emit(const PreparedList& prepared, VkCommandBuffer command_buffer, c
VkImageMemoryBarrier2* const barriers = many.empty() ? few.data() : many.data();
std::uint32_t barrier_count = 0;
for (const auto& piece : pieces) {
// Released before it was ever used (the tracker still has it in UNDEFINED): nothing to order.
if (release && piece.layout == VK_IMAGE_LAYOUT_UNDEFINED) continue;
VkImageMemoryBarrier2& b = barriers[barrier_count++];
b = {VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER_2};
b.srcStageMask = src.stages;
b.dstStageMask = dst.stages;
b.srcAccessMask = src.access;
b.dstAccessMask = dst.access;
b.oldLayout = piece.layout;
b.newLayout = new_layout;
b.newLayout = release ? piece.layout : new_layout;
b.srcQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
b.dstQueueFamilyIndex = VK_QUEUE_FAMILY_IGNORED;
b.image = p.binding.image;
b.subresourceRange = detail::layout_range(p.binding, piece.range);
if (b.srcStageMask) detail::restrict_to_queue(vk, b.srcStageMask, b.srcAccessMask);
if (b.dstStageMask) detail::restrict_to_queue(vk, b.dstStageMask, b.dstAccessMask);
}
VkDependencyInfo dependency{VK_STRUCTURE_TYPE_DEPENDENCY_INFO};
dependency.imageMemoryBarrierCount = barrier_count;
dependency.pImageMemoryBarriers = barriers;
vk.pipeline_barrier2(command_buffer, &dependency);
if (tracker) tracker->set(id, range, new_layout);
if (barrier_count) {
VkDependencyInfo dependency{VK_STRUCTURE_TYPE_DEPENDENCY_INFO};
dependency.imageMemoryBarrierCount = barrier_count;
dependency.pImageMemoryBarriers = barriers;
vk.pipeline_barrier2(command_buffer, &dependency);
}
if (tracker && !release) tracker->set(id, range, new_layout);
} else if (const auto* color_clear = std::get_if<ClearRenderTarget>(&command)) {
if (p.skip) continue;
const VulkanAccess state = vulkan_access(d3d12_state::render_target, false);
Expand Down
8 changes: 6 additions & 2 deletions runtime/test/command_list_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,19 @@ int main() {
CHECK(!list.barrier(BufferBarrier{y::copy, y::vertex_shading, a::copy_dest, a::vertex_buffer, 0}));
CHECK(list.barrier(TextureBarrier{y::none, y::render_target, a::no_access, a::render_target, l::undefined,
l::render_target, 9, all, texture_barrier_discard}));
CHECK(!list.barrier(TextureBarrier{y::all, y::all, a::common, a::common, l::common, l::undefined, 9, all, 0}));
// Into UNDEFINED: the contents are no longer needed (WARP closes such a list, d3d12_close_parity_test).
CHECK(list.barrier(TextureBarrier{y::depth_stencil, y::none, a::depth_stencil_write, a::no_access,
l::depth_stencil_write, l::undefined, 9, all, 0}));
CHECK(!list.barrier(TextureBarrier{y::all, y::all, a::common, a::common, l::common, l::common, 9, all, 0x2}));
CHECK(!list.barrier(TextureBarrier{y::all, y::all, a::common, a::common, l::common, l::common, 9,
{0, 1, 0, 0, 0, 1}, 0})); // zero layers
CHECK(list.close());
CHECK(list.commands().size() == 4);
CHECK(list.commands().size() == 5);
CHECK(std::get<GlobalBarrier>(list.commands()[1]).sync_before == y::split);
CHECK(std::get<BufferBarrier>(list.commands()[2]).resource == 7);
CHECK(std::get<TextureBarrier>(list.commands()[3]).flags == texture_barrier_discard);
CHECK(std::get<TextureBarrier>(list.commands()[4]).layout_before == l::depth_stencil_write &&
std::get<TextureBarrier>(list.commands()[4]).layout_after == l::undefined);

// Copies: D3D12's own call rules (aligned footprints; degenerate boxes and DEFAULT-heap
// CopyResource self-copies are accepted as no-ops and must not record Vulkan commands).
Expand Down
55 changes: 54 additions & 1 deletion runtime/test/d3d12_close_parity_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,11 @@ struct Env {
Ref<ID3D12QueryHeap> occlusion;
Ref<ID3D12CommandAllocator> closed_bundle_allocator;
Ref<ID3D12GraphicsCommandList> closed_bundle;
// The list being recorded, and the same list as ID3D12GraphicsCommandList2 (null when not given).
// The list being recorded, and the same list as ID3D12GraphicsCommandList2 and 7 (null when not given; list7
// also when the device has no enhanced barriers).
ID3D12GraphicsCommandList* list = nullptr;
ID3D12GraphicsCommandList2* list2 = nullptr;
ID3D12GraphicsCommandList7* list7 = nullptr;

bool init(ID3D12Device* d) {
device = d;
Expand Down Expand Up @@ -163,6 +165,28 @@ constexpr auto stricter = Known::layer_stricter;
constexpr auto looser = Known::layer_looser;
constexpr auto match = Known::match;

// One enhanced texture barrier over every subresource of `resource`; false when the list has no enhanced barriers.
bool texture_barrier(Env& e, ID3D12Resource* resource, D3D12_BARRIER_SYNC sync_before, D3D12_BARRIER_SYNC sync_after,
D3D12_BARRIER_ACCESS access_before, D3D12_BARRIER_ACCESS access_after,
D3D12_BARRIER_LAYOUT layout_before, D3D12_BARRIER_LAYOUT layout_after) {
if (!e.list7) return false;
D3D12_TEXTURE_BARRIER b{};
b.SyncBefore = sync_before;
b.SyncAfter = sync_after;
b.AccessBefore = access_before;
b.AccessAfter = access_after;
b.LayoutBefore = layout_before;
b.LayoutAfter = layout_after;
b.pResource = resource;
b.Subresources = {0xffffffff, 0, 0, 0, 0, 0}; // every subresource
D3D12_BARRIER_GROUP group{};
group.Type = D3D12_BARRIER_TYPE_TEXTURE;
group.NumBarriers = 1;
group.pTextureBarriers = &b;
e.list7->Barrier(1, &group);
return true;
}

std::vector<Case> cases() {
return {
{"empty list", true, match, [](Env&) { return true; }},
Expand Down Expand Up @@ -200,6 +224,28 @@ std::vector<Case> cases() {
e.list->ResourceBarrier(1, &b))},
{"barrier type 7", false, match,
CALLS(D3D12_RESOURCE_BARRIER b{}; b.Type = D3D12_RESOURCE_BARRIER_TYPE(7); e.list->ResourceBarrier(1, &b))},
// Enhanced barriers (ID3D12GraphicsCommandList7::Barrier). LayoutAfter UNDEFINED says the contents are no
// longer needed; the retail title ends a depth buffer's use that way (sync DEPTH_STENCIL -> NONE, access
// DEPTH_STENCIL_WRITE -> NO_ACCESS), and the layer failed the list's Close for it. The WARP column of these
// three rows was measured on Windows 11 26340 (#984), not on the build the header names.
{"Barrier: depth DEPTH_STENCIL_WRITE -> COPY_SOURCE", true, match,
[](Env& e) {
return texture_barrier(e, e.depth.p, D3D12_BARRIER_SYNC_DEPTH_STENCIL, D3D12_BARRIER_SYNC_COPY,
D3D12_BARRIER_ACCESS_DEPTH_STENCIL_WRITE, D3D12_BARRIER_ACCESS_COPY_SOURCE,
D3D12_BARRIER_LAYOUT_DEPTH_STENCIL_WRITE, D3D12_BARRIER_LAYOUT_COPY_SOURCE);
}},
{"Barrier: depth DEPTH_STENCIL_WRITE -> UNDEFINED, sync NONE after", true, match,
[](Env& e) {
return texture_barrier(e, e.depth.p, D3D12_BARRIER_SYNC_DEPTH_STENCIL, D3D12_BARRIER_SYNC_NONE,
D3D12_BARRIER_ACCESS_DEPTH_STENCIL_WRITE, D3D12_BARRIER_ACCESS_NO_ACCESS,
D3D12_BARRIER_LAYOUT_DEPTH_STENCIL_WRITE, D3D12_BARRIER_LAYOUT_UNDEFINED);
}},
{"Barrier: texture COMMON -> UNDEFINED", true, match,
[](Env& e) {
return texture_barrier(e, e.tex.p, D3D12_BARRIER_SYNC_ALL, D3D12_BARRIER_SYNC_NONE,
D3D12_BARRIER_ACCESS_COMMON, D3D12_BARRIER_ACCESS_NO_ACCESS,
D3D12_BARRIER_LAYOUT_COMMON, D3D12_BARRIER_LAYOUT_UNDEFINED);
}},
// CopyBufferRegion
{"CopyBufferRegion of 0 bytes", true, match, CALLS(e.list->CopyBufferRegion(e.buffer.p, 0, e.upload.p, 0, 0))},
{"CopyBufferRegion past the end of the destination", false, looser,
Expand Down Expand Up @@ -434,17 +480,24 @@ bool run_case(Env& env, const Case& c, HRESULT& close) {
Ref<ID3D12CommandAllocator> allocator;
Ref<ID3D12GraphicsCommandList> list;
Ref<ID3D12GraphicsCommandList2> list2;
Ref<ID3D12GraphicsCommandList7> list7;
CHECK(SUCCEEDED(env.device->CreateCommandAllocator(D3D12_COMMAND_LIST_TYPE_DIRECT, IID_PPV_ARGS(&allocator.p))));
CHECK(SUCCEEDED(env.device->CreateCommandList(0, D3D12_COMMAND_LIST_TYPE_DIRECT, allocator.p, nullptr,
IID_PPV_ARGS(&list.p))));
if (FAILED(list->QueryInterface(IID_PPV_ARGS(&list2.p)))) list2.p = nullptr;
D3D12_FEATURE_DATA_D3D12_OPTIONS12 options12{};
if (FAILED(env.device->CheckFeatureSupport(D3D12_FEATURE_D3D12_OPTIONS12, &options12, sizeof(options12))) ||
!options12.EnhancedBarriersSupported || FAILED(list->QueryInterface(IID_PPV_ARGS(&list7.p))))
list7.p = nullptr;
env.list = list.p;
env.list2 = list2.p;
env.list7 = list7.p;
const bool recorded = c.record(env);
close = recorded ? list->Close() : not_run;
if (!recorded) list->Close();
env.list = nullptr;
env.list2 = nullptr;
env.list7 = nullptr;
return true;
}

Expand Down
Loading
Loading