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
5 changes: 2 additions & 3 deletions runtime/src/vulkan_backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -3533,9 +3533,8 @@ class VulkanQueue : private QueueHolds<VkSemaphore>::Holder {
if (statistics_segments && can_resolve_occlusion) {
if (!segment_pool(VK_QUERY_TYPE_PIPELINE_STATISTICS, statistics_segments, statistics))
return queue_unsupported(__LINE__);
for (const auto& list : prepared)
statistics->mesh.insert(statistics->mesh.end(), list.statistics_mesh.begin(),
list.statistics_mesh.end());
// This batch's lists only: `prepared` keeps the entries of a larger batch past lists.size().
statistics->mesh = batch_statistics_mesh(std::span<const PreparedList>(prepared).first(lists.size()));
if (std::find(statistics->mesh.begin(), statistics->mesh.end(), 1) == statistics->mesh.end()) {
statistics->mesh.clear();
} else {
Expand Down
14 changes: 11 additions & 3 deletions runtime/src/vulkan_replay.h
Original file line number Diff line number Diff line change
Expand Up @@ -3297,6 +3297,15 @@ inline void plan_statistics_segments(PreparedList& out) {
// while one runs, because its helper dispatch would be counted — closes the one open physical query and opens
// a new one, so each logical query keeps a contiguous slot range and its result is the sum of that range
// (statistics_sum.comp). A Begin or End of another query kind leaves the segments alone.
// The draw kind of every statistics segment of a batch, list after list, as the batch's segment pool numbers them
// (OcclusionSegmentPool::mesh). `lists` is this batch's lists only: a caller that keeps PreparedLists from a larger
// batch past its end (VulkanQueue::execute) passes the first lists.size() of them.
inline std::vector<std::uint8_t> batch_statistics_mesh(std::span<const PreparedList> lists) {
std::vector<std::uint8_t> out;
for (const PreparedList& list : lists) out.insert(out.end(), list.statistics_mesh.begin(), list.statistics_mesh.end());
return out;
}

inline ReplayResult plan_overlapping_statistics_segments(PreparedList& out) {
std::map<std::pair<QueryHeapId, std::uint32_t>, std::uint32_t> active;
std::uint32_t current = detail::PreparedQuery::no_segment;
Expand Down Expand Up @@ -3326,9 +3335,8 @@ inline ReplayResult plan_overlapping_statistics_segments(PreparedList& out) {
p.statistics_end = current;
current = open();
p.statistics_begin = current;
// A clamped draw is also the first vertex draw of this segment.
if (draw_command(commands[i]).state) kind = 0;
continue;
// The clamped call is the first command of the new segment: a draw sets its kind below (vertex or
// mesh, as any draw does), a dispatch leaves it unset.
}
// A draw of the other kind than the open segment's starts a segment in its own pool (D3D-291).
int draw = -1;
Expand Down
75 changes: 75 additions & 0 deletions runtime/test/replay_query_predication_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,81 @@ int main() {
expect(q[3].statistics_first == 1 && q[3].statistics_count == 3, "overlap-mesh: query 1 sums segments 1-3");
}

// 16b. kevincommits' finding (#1608, item 2): a clamped indirect draw under overlapping statistics queries opens a
// segment of its own (the clamp is a helper dispatch), and that segment takes the draw's kind like any other:
// mesh for a clamped indirect mesh draw, so it runs in the mesh pool. A mesh draw after it stays in the same
// segment; a vertex draw after it splits. The non-overlapping planner marks the same draw mesh too.
{
const auto draw = [](bool mesh) {
Draw d{};
d.pipeline = 1;
d.count = d.instances = d.first = 1;
d.mesh = mesh;
return d;
};
CommandList list;
expect(list.begin_query({6, d3d12_query::pipeline_statistics, 0}), "record clamped-mesh begin 0"); // 0
expect(list.begin_query({6, d3d12_query::pipeline_statistics, 1}), "record clamped-mesh begin 1"); // 1
expect(list.draw(draw(true)), "record clamped mesh draw"); // 2
expect(list.draw(draw(true)), "record mesh draw after it"); // 3
expect(list.draw(draw(false)), "record vertex draw after them"); // 4
expect(list.end_query({6, d3d12_query::pipeline_statistics, 0}), "record clamped-mesh end 0"); // 5
expect(list.end_query({6, d3d12_query::pipeline_statistics, 1}), "record clamped-mesh end 1"); // 6
expect(list.close(), "close clamped-mesh list");
const auto plan = [&](bool overlapping) {
PreparedList prepared;
prepared.list = &list;
prepared.commands.assign(list.commands().size(), {});
prepared.commands[2].indirect_clamp.buffer = handle<VkBuffer>(30); // as prepare() leaves a clamped draw
for (std::size_t i : {0, 1, 5, 6}) {
const bool begin = std::holds_alternative<BeginQuery>(list.commands()[i]);
prepared.queries.push_back({i, &statistics_heap, begin});
}
if (overlapping) expect(plan_overlapping_statistics_segments(prepared) == ReplayResult::success,
"plan clamped-mesh list");
else plan_statistics_segments(prepared);
return prepared;
};
const PreparedList prepared = plan(true);
const auto& c = prepared.commands;
constexpr auto none = detail::PreparedQuery::no_segment;
const auto mesh_of = [&](std::uint32_t segment) {
return segment < prepared.statistics_mesh.size() ? int(prepared.statistics_mesh[segment]) : -1;
};
expect(c[2].statistics_begin != none && mesh_of(c[2].statistics_begin) == 1,
"clamped-mesh: the clamped mesh draw's segment is a mesh segment");
expect(c[3].statistics_begin == none, "clamped-mesh: a mesh draw after it stays in its segment");
expect(c[4].statistics_begin != none && mesh_of(c[4].statistics_begin) == 0,
"clamped-mesh: the vertex draw after them splits into a vertex segment");
// The same draw under one query at a time (plan_statistics_segments) is marked mesh as well.
CommandList single;
single.begin_query({6, d3d12_query::pipeline_statistics, 0}); // 0
single.draw(draw(true)); // 1
single.end_query({6, d3d12_query::pipeline_statistics, 0}); // 2
single.close();
PreparedList one;
one.list = &single;
one.commands.assign(single.commands().size(), {});
one.commands[1].indirect_clamp.buffer = handle<VkBuffer>(30);
for (std::size_t i : {0, 2})
one.queries.push_back({i, &statistics_heap, std::holds_alternative<BeginQuery>(single.commands()[i])});
plan_statistics_segments(one);
expect(std::find(one.statistics_mesh.begin(), one.statistics_mesh.end(), std::uint8_t(1)) !=
one.statistics_mesh.end(),
"clamped-mesh: one query at a time, the clamped mesh draw's segment is a mesh segment too");

// kevincommits' item 2, second part: VulkanQueue::execute keeps the PreparedLists of its largest batch. A
// later, smaller batch gathers the segment kinds of its own lists only (batch_statistics_mesh over the first
// lists.size()), so a stale mesh segment past its end does not ask for the mesh pools.
std::vector<PreparedList> kept(2);
kept[0].statistics_mesh = {0, 0};
kept[1].statistics_mesh = {1}; // from an earlier batch of two lists
expect(batch_statistics_mesh(std::span<const PreparedList>(kept).first(1)) == std::vector<std::uint8_t>({0, 0}),
"batch kinds: a batch of one list sees only its own segments");
expect(batch_statistics_mesh(kept) == std::vector<std::uint8_t>({0, 0, 1}),
"batch kinds: a batch of two lists sees both, in list order");
}

// 17. D3D-291: the pool a mesh draw runs in must not enable a counter
// VUID-vkCmdDrawMeshTasksEXT-pipelineStatistics-07076 forbids, or the validation layer reports the VUID on
// every DispatchMesh inside a statistics query. The VUID leaves the fragment and compute invocations
Expand Down
Loading