Skip to content
Open
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
66 changes: 59 additions & 7 deletions runtime/src/vulkan_replay.h
Original file line number Diff line number Diff line change
Expand Up @@ -682,29 +682,79 @@ class LayoutTracker {
return e.layouts[(e.two_planes ? plane : 0) * per_plane + subresource % per_plane]; // merged planes share
}

// The current layouts over a range: one piece when they all agree, else one per subresource.
// The current layouts over a range: one piece when they all agree, otherwise rectangles of equal layout.
// Out is LayoutPieces or std::vector<LayoutPiece>.
template<typename Out>
bool pieces(ResourceId id, const VkImageSubresourceRange& range, Out& out) const {
const auto it = entries_.find(id);
if (it == entries_.end()) return false;
const Entry& e = it->second;
out.clear();
bool uniform = true;
VkImageLayout first = VK_IMAGE_LAYOUT_UNDEFINED;
bool any = false;
visit(e, range, [&](std::size_t index, VkImageAspectFlags, std::uint32_t, std::uint32_t) {
const bool uniform = visit(e, range, [&](std::size_t index, VkImageAspectFlags, std::uint32_t, std::uint32_t) {
if (!any) first = e.layouts[index];
uniform = uniform && e.layouts[index] == first;
any = true;
return e.layouts[index] == first;
});
if (uniform) {
out.push_back({range, first});
return true;
}
// A mixed range with at most two cells cannot merge. Larger ranges need an initialized
// pair of neighbors in the same aspect before any rectangle can span more than one cell.
// If there is no horizontal pair, any vertical pair can merge as two width-one runs.
const std::uint64_t area = std::uint64_t(range.levelCount) * range.layerCount;
constexpr auto depth_stencil = VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT;
const bool two_aspects = e.two_planes && (range.aspectMask & depth_stencil) == depth_stencil;
if (area <= (two_aspects ? 1u : 2u) ||
visit(e, range, [&](std::size_t index, VkImageAspectFlags, std::uint32_t mip, std::uint32_t layer) {
const auto layout = e.layouts[index];
return layout == VK_IMAGE_LAYOUT_UNDEFINED ||
((mip == range.baseMipLevel || e.layouts[index - 1] != layout) &&
(layer == range.baseArrayLayer || e.layouts[index - e.mips] != layout));
})) {
visit(e, range, [&](std::size_t index, VkImageAspectFlags aspect, std::uint32_t mip, std::uint32_t layer) {
out.push_back({{aspect, mip, 1, layer, 1}, e.layouts[index]});
return true;
});
return true;
}
// A complete mip chain for a uint32_t extent has at most 32 levels. Keep the index plus one
// of the last rectangle starting at each mip, so repeated rows need no temporary heap storage.
std::array<std::size_t, std::numeric_limits<std::uint32_t>::digits> last_run{};
LayoutPiece run{};
const auto flush_run = [&] {
const auto mip = run.range.baseMipLevel;
if (run.layout != VK_IMAGE_LAYOUT_UNDEFINED && mip < last_run.size()) {
if (const auto slot = last_run[mip]) {
auto& previous = out[slot - 1];
auto& prev_range = previous.range;
if (previous.layout == run.layout && prev_range.aspectMask == run.range.aspectMask &&
prev_range.levelCount == run.range.levelCount &&
prev_range.baseArrayLayer + prev_range.layerCount == run.range.baseArrayLayer) {
++prev_range.layerCount;
return;
}
}
last_run[mip] = out.size() + 1;
}
out.push_back(run);
};
visit(e, range, [&](std::size_t index, VkImageAspectFlags aspect, std::uint32_t mip, std::uint32_t layer) {
out.push_back({{aspect, mip, 1, layer, 1}, e.layouts[index]});
const auto layout = e.layouts[index];
// First-use initialization can allocate transfer regions per piece. Keep its existing granularity.
if (run.range.levelCount && layout != VK_IMAGE_LAYOUT_UNDEFINED && run.layout == layout &&
run.range.aspectMask == aspect && run.range.baseArrayLayer == layer &&
run.range.baseMipLevel + run.range.levelCount == mip) {
++run.range.levelCount;
return true;
}
if (run.range.levelCount) flush_run();
run = {{aspect, mip, 1, layer, 1}, layout};
return true;
});
flush_run(); // nonuniform implies the visit produced at least one subresource
return true;
}

Expand All @@ -714,6 +764,7 @@ class LayoutTracker {
Entry& e = it->second;
visit(e, range, [&](std::size_t index, VkImageAspectFlags, std::uint32_t, std::uint32_t) {
e.layouts[index] = layout;
return true;
});
}

Expand All @@ -727,7 +778,7 @@ class LayoutTracker {
};

template<typename F>
static void visit(const Entry& e, const VkImageSubresourceRange& range, F&& f) {
static bool visit(const Entry& e, const VkImageSubresourceRange& range, F&& f) {
const std::uint32_t mip_end = range.levelCount == VK_REMAINING_MIP_LEVELS ? e.mips
: range.baseMipLevel + range.levelCount;
const std::uint32_t layer_end = range.layerCount == VK_REMAINING_ARRAY_LAYERS
Expand All @@ -741,8 +792,9 @@ class LayoutTracker {
}
for (std::uint32_t layer = range.baseArrayLayer; layer < std::min(layer_end, e.layers); ++layer)
for (std::uint32_t mip = range.baseMipLevel; mip < std::min(mip_end, e.mips); ++mip)
f((std::size_t(plane) * e.layers + layer) * e.mips + mip, aspect, mip, layer);
if (!f((std::size_t(plane) * e.layers + layer) * e.mips + mip, aspect, mip, layer)) return false;
}
return true;
}

bool separate_;
Expand Down
39 changes: 31 additions & 8 deletions runtime/test/d3d12_state_map_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -505,24 +505,27 @@ int main() {
CHECK(separate.layout(1, 1) == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL); // depth, mip 1
CHECK(separate.layout(1, 4) == VK_IMAGE_LAYOUT_UNDEFINED); // no plane 2
std::vector<LayoutPiece> pieces;
CHECK(separate.pieces(1, {depth_stencil.aspects, 0, 2, 0, 1}, pieces) && pieces.size() == 4);
CHECK(separate.pieces(1, {depth_stencil.aspects, 0, 2, 0, 1}, pieces) && pieces.size() == 3);
CHECK(pieces[0].range.aspectMask == VK_IMAGE_ASPECT_DEPTH_BIT && pieces[0].range.levelCount == 2);
CHECK(separate.pieces(1, {VK_IMAGE_ASPECT_DEPTH_BIT, 0, 2, 0, 1}, pieces) && pieces.size() == 1);
CHECK(pieces[0].layout == VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL && pieces[0].range.levelCount == 2);
// LayoutPieces (what replay uses): the same pieces, inline for one or two, spilling to the heap for four, and
// LayoutPieces (what replay uses): the same pieces, inline for one or two, spilling to the heap for three, and
// reused after clear() from either state.
{
std::vector<LayoutPiece> four;
std::vector<LayoutPiece> mixed;
LayoutPieces inline_pieces;
CHECK(separate.pieces(1, {depth_stencil.aspects, 0, 2, 0, 1}, four) && four.size() == 4);
CHECK(separate.pieces(1, {depth_stencil.aspects, 0, 2, 0, 1}, inline_pieces) && inline_pieces.size() == 4);
CHECK(separate.pieces(1, {depth_stencil.aspects, 0, 2, 0, 1}, mixed) && mixed.size() == 3);
CHECK(separate.pieces(1, {depth_stencil.aspects, 0, 2, 0, 1}, inline_pieces) && inline_pieces.size() == 3);
bool same = true;
std::size_t index = 0;
for (const LayoutPiece& piece : inline_pieces) {
same = same && piece.layout == four[index].layout && piece.range.aspectMask == four[index].range.aspectMask &&
piece.range.baseMipLevel == four[index].range.baseMipLevel;
same = same && piece.layout == mixed[index].layout &&
piece.range.aspectMask == mixed[index].range.aspectMask &&
piece.range.baseMipLevel == mixed[index].range.baseMipLevel &&
piece.range.levelCount == mixed[index].range.levelCount;
++index;
}
CHECK(same && index == 4);
CHECK(same && index == 3);
CHECK(separate.pieces(1, {VK_IMAGE_ASPECT_DEPTH_BIT, 0, 2, 0, 1}, inline_pieces) && inline_pieces.size() == 1);
CHECK(inline_pieces[0].range.levelCount == 2 && inline_pieces.end() - inline_pieces.begin() == 1);
inline_pieces.clear();
Expand All @@ -538,6 +541,26 @@ int main() {
merged.forget(1);
CHECK(!merged.tracked(1));

// A mixed range merges the initialized rectangle across mips and layers, while all first-use
// cells keep individual pieces even when they are adjacent.
{
ResourceBinding layered = color;
layered.mip_levels = 4;
layered.array_layers = 4;
const VkImageSubresourceRange stripe{VK_IMAGE_ASPECT_COLOR_BIT, 0, 2, 1, 2};
LayoutTracker tracker(false);
tracker.track(3, layered, VK_IMAGE_LAYOUT_UNDEFINED);
tracker.set(3, stripe, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL);
CHECK(tracker.pieces(3, {VK_IMAGE_ASPECT_COLOR_BIT, 0, 4, 1, 2}, pieces) && pieces.size() == 5);
CHECK(pieces[0].layout == VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL &&
pieces[0].range.baseMipLevel == 0 && pieces[0].range.levelCount == 2 &&
pieces[0].range.baseArrayLayer == 1 && pieces[0].range.layerCount == 2);
CHECK(std::count_if(pieces.begin(), pieces.end(), [](const auto& piece) {
return piece.layout == VK_IMAGE_LAYOUT_UNDEFINED && piece.range.levelCount == 1 &&
piece.range.layerCount == 1;
}) == 4);
}

// DiscardResource by subresource range (D3D-26), through prepare(): each listed subresource becomes one range,
// and the command loop goes on to the next command after a discard's own loop.
{
Expand Down
Loading