upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHadagalberto Junior <hadagalberto.junior1@gmail.com>2026-11-22 11:52:20 -0300
committerHadagalberto Junior <hadagalberto.junior1@gmail.com>2026-11-22 11:52:23 -0300
commit0bde4c82d7bb26a255aa2e8a403c982f08ea2aa5 (patch)
tree7ca4d7e8ccad7a9ab53f7510a309e3ab2cde143f
parent6a6704df1592e1dc4a3215ddf1423a78adb52045 (diff)
Runtime: split pipeline statistics around predication and footprint helpers (D3D-162)
Replay refused SetPredication, predicated buffer copies and D24/RGB96 footprint conversions while a pipeline-statistics query was active (#1624 added the last), because their compute dispatches would count in the application's CSInvocations. They now get the segment split #1519 gave resolves: the statistics query ends its current physical segment right before the helper dispatch and begins the next right after it, and its result is the per-counter sum of the segments. Query resolves and these helpers share one segment numbering. The markers for non-query commands live on detail::Prepared, and emit() places them around capture_predicate, emit_predicated_copy and the depth24 conversion parts, outside any rendering scope. Without a statistics segment pool (the single-list replay(), or a queue without the summation helper), the three commands are still refused while statistics are active, before any helper scratch is allocated (prepare() takes statistics_helpers, false by default) and again in QueryReplay::plan. Tests (Khronos validation on): vk_predicated_copy and depth24_convert run their helpers inside an otherwise empty statistics query. Their results stay byte-exact and all 11 counters are 0. d3d12_predicated_copy compares with WARP the same way. replay_query_predication checks the emitted order for a capture, a predicated copy, a footprint copy and a resolve in one query, and the refusal without a pool. predicated_copy and typeless_rtv check both preparation modes. Co-Authored-By: OpenAI Codex <noreply@openai.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
-rw-r--r--docs/d3d12_vulkan.md32
-rw-r--r--runtime/src/vulkan_backend.h2
-rw-r--r--runtime/src/vulkan_replay.h77
-rw-r--r--runtime/test/d3d12_predicated_copy_test.cpp35
-rw-r--r--runtime/test/depth24_convert_test.cpp29
-rw-r--r--runtime/test/predicated_copy_test.cpp6
-rw-r--r--runtime/test/replay_query_predication_test.cpp126
-rw-r--r--runtime/test/typeless_rtv_test.cpp2
-rw-r--r--runtime/test/vk_predicated_copy_test.cpp30
9 files changed, 296 insertions, 43 deletions
diff --git a/docs/d3d12_vulkan.md b/docs/d3d12_vulkan.md
index 6834672b7..3f165ab3e 100644
--- a/docs/d3d12_vulkan.md
+++ b/docs/d3d12_vulkan.md
@@ -2030,16 +2030,21 @@ Implemented (`QueryHeap` in `runtime/shims/d3d12_vulkan_device.cpp`, the query c
2030 overwrites can be recorded before the earlier commands complete. `vk_overlapping_occlusion` checks crossing 2030 overwrites can be recorded before the earlier commands complete. `vk_overlapping_occlusion` checks crossing
2031 and nested scopes, more than one scratch chunk, alternating binary and ordinary resolves under a predicate, 2031 and nested scopes, more than one scratch chunk, alternating binary and ordinary resolves under a predicate,
2032 and gated submissions on a GPU. 2032 and gated submissions on a GPU.
2033- **Resolves during a pipeline-statistics query** (D3D-162). The helper dispatches above (binary normalisation, 2033- **Helper dispatches during a pipeline-statistics query** (D3D-162). The helper dispatches above (binary
2034 occlusion sums) and the statistics sum below would count in the application's `CSInvocations` if they ran inside 2034 normalisation, occlusion sums), statistics sums, `SetPredication` captures, predicated buffer copies and D24/RGB96 footprint
2035 its query. So a statistics query that contains a resolve other than a timestamp copy is split. Its counters go to 2035 conversions would count in the application's `CSInvocations` if they ran inside its query. Such a query is split.
2036 physical `PIPELINE_STATISTICS` segments in a pool of the submission's own; a segment ends before each such resolve 2036 Its counters go to physical `PIPELINE_STATISTICS` segments in a pool of the submission's own; a segment ends
2037 and a new one begins after it. Its result is the sum of its segments, added per counter by a compute dispatch 2037 before each helper and a new one begins after it. Non-query helpers and resolves share the segment numbering.
2038 (`statistics_sum.comp`) on the same scratch buffer, which is itself kept out of any statistics query still running. 2038 Its result is the sum of its segments, added per counter by a compute dispatch (`statistics_sum.comp`) on the
2039 same scratch buffer, which is itself kept out of any statistics query still running.
2039 Whether a resolve dispatches is only known once the list is planned against the heaps, so every such resolve 2040 Whether a resolve dispatches is only known once the list is planned against the heaps, so every such resolve
2040 splits the query. `vk_binary_occlusion` checks that a binary resolve and a statistics sum inside statistics queries 2041 splits the query. `vk_binary_occlusion` checks that a binary resolve and a statistics sum inside statistics queries
2041 leave every counter at 0. `vk_overlapping_occlusion` puts a native draw in each segment and checks the split sums 2042 leave every counter at 0. `vk_overlapping_occlusion` puts a native draw in each segment and checks the split sums
2042 against unsplit queries with the same draws, word by word. 2043 against unsplit queries with the same draws, word by word. `vk_predicated_copy` checks captures, rebindings and
2044 predicated copies; `depth24_convert` checks both conversion directions for D24 and RGB96. Their byte-exact
2045 results still match and all 11 statistics counters are zero. Without a statistics pool (single-list `replay()` or
2046 a queue without the summation helper), these three commands are refused before helper scratch allocation or
2047 recording. The resolve fallback is unchanged: only resolves needing dispatches are refused there.
2043- **Timestamps.** `GetTimestampFrequency` is `1e9 / timestampPeriod` (1 GHz on the test GPU). Every D3D12 queue 2048- **Timestamps.** `GetTimestampFrequency` is `1e9 / timestampPeriod` (1 GHz on the test GPU). Every D3D12 queue
2044 shares one `VkQueue` for now (question 12), so they share one frequency. 2049 shares one `VkQueue` for now (question 12), so they share one frequency.
2045 2050
@@ -2047,10 +2052,6 @@ Not implemented yet:
2047 2052
2048- **Overlapping queries.** Replay refuses overlapping pipeline-statistics queries or occlusion overlaps involving 2053- **Overlapping queries.** Replay refuses overlapping pipeline-statistics queries or occlusion overlaps involving
2049 a binary query with `ReplayResult::unsupported`. 2054 a binary query with `ReplayResult::unsupported`.
2050- **Other helper dispatches during a pipeline-statistics query.** Replay still refuses `SetPredication` and
2051 predicated copies and D24/RGB96 footprint conversions while pipeline statistics are active, since their
2052 dispatches would change the reported compute-invocation count. The single-list `replay()` helper, which has no
2053 segment pool, keeps refusing the occlusion resolves above that need a dispatch there.
2054- Stream-output and mesh-shader statistics. (`GetClockCalibration` is implemented: see section 4.4, "Fences, 2055- Stream-output and mesh-shader statistics. (`GetClockCalibration` is implemented: see section 4.4, "Fences,
2055 queues and frames in flight".) 2056 queues and frames in flight".)
2056- Xbox-specific query heap types (if any) are **Open**; unknown types are logged. 2057- Xbox-specific query heap types (if any) are **Open**; unknown types are logged.
@@ -2091,9 +2092,9 @@ Not implemented yet:
2091 CPU readback or per-predicate wait is used. This helper requires a compute-capable configured queue family; 2092 CPU readback or per-predicate wait is used. This helper requires a compute-capable configured queue family;
2092 unsupported queues fail preparation instead of falling back to the low word. The synthetic GLSL source and 2093 unsupported queues fail preparation instead of falling back to the low word. The synthetic GLSL source and
2093 embedded SPIR-V are shipped together; normal builds do not need a shader compiler. 2094 embedded SPIR-V are shipped together; normal builds do not need a shader compiler.
2094 Capturing a new predicate inside an active pipeline-statistics query is rejected as unsupported, because the 2095 Capturing a new predicate inside an active pipeline-statistics query splits the query around the capture
2095 helper's compute invocation would otherwise pollute the application's result; capturing before the query is 2096 dispatch (D3D-162, section 4.6), keeping its invocation out of the application's result. Without a statistics
2096 allowed. Virtualising that query is not part of this change. 2097 segment pool, preparation still refuses the capture before allocating its scratch.
2097 Integration notes for #291 and D3D-27: the capture helper binds its own compute pipeline, descriptor set 0 2098 Integration notes for #291 and D3D-27: the capture helper binds its own compute pipeline, descriptor set 0
2098 and push constants. The compute path must restore those bindings afterward, or bind its application state 2099 and push constants. The compute path must restore those bindings afterward, or bind its application state
2099 before each dispatch. Copies and compute dispatches cannot execute inside dynamic rendering, so render-pass 2100 before each dispatch. Copies and compute dispatches cannot execute inside dynamic rendering, so render-pass
@@ -2112,7 +2113,8 @@ Not implemented yet:
2112 for ordinary copies; mixed lists accumulate both before main's queue-specific host-stage filtering. 2113 for ordinary copies; mixed lists accumulate both before main's queue-specific host-stage filtering.
2113 This helper also binds compute pipeline/set 0/push constants, so the 2114 This helper also binds compute pipeline/set 0/push constants, so the
2114 application-state restoration requirement above applies. A predicated copy inside an active pipeline-statistics 2115 application-state restoration requirement above applies. A predicated copy inside an active pipeline-statistics
2115 query is rejected to avoid counting internal compute work. 2116 query splits that query around the helper dispatches (D3D-162); without a statistics segment pool it is refused
2117 before copy scratch allocation.
2116 Synthetic checks: `predicated_copy_test` covers preparation and rejection paths without a GPU; 2118 Synthetic checks: `predicated_copy_test` covers preparation and rejection paths without a GPU;
2117 `d3d12_predicated_copy_test` compares 180 byte-exact cases against WARP and an independent literal oracle, 2119 `d3d12_predicated_copy_test` compares 180 byte-exact cases against WARP and an independent literal oracle,
2118 plus 16 query-to-copy cases: real empty ordinary/binary occlusion queries resolve zero into a default-heap 2120 plus 16 query-to-copy cases: real empty ordinary/binary occlusion queries resolve zero into a default-heap
diff --git a/runtime/src/vulkan_backend.h b/runtime/src/vulkan_backend.h
index e577e6af0..490108f04 100644
--- a/runtime/src/vulkan_backend.h
+++ b/runtime/src/vulkan_backend.h
@@ -1355,7 +1355,7 @@ public:
1355 std::uint32_t segments = 0, statistics_segments = 0; 1355 std::uint32_t segments = 0, statistics_segments = 0;
1356 bool has_queries = false; 1356 bool has_queries = false;
1357 for (std::size_t i = 0; i < lists.size(); ++i) { 1357 for (std::size_t i = 0; i < lists.size(); ++i) {
1358 const auto result = prepare(*lists[i], resolve, replay, prepared[i]); 1358 const auto result = prepare(*lists[i], resolve, replay, prepared[i], can_resolve_occlusion);
1359 if (result != ReplayResult::success) return result; 1359 if (result != ReplayResult::success) return result;
1360 if (prepared[i].segment_count > UINT32_MAX - segments || 1360 if (prepared[i].segment_count > UINT32_MAX - segments ||
1361 prepared[i].statistics_segment_count > UINT32_MAX - statistics_segments) 1361 prepared[i].statistics_segment_count > UINT32_MAX - statistics_segments)
diff --git a/runtime/src/vulkan_replay.h b/runtime/src/vulkan_replay.h
index 0b46198a9..17feae0a7 100644
--- a/runtime/src/vulkan_replay.h
+++ b/runtime/src/vulkan_replay.h
@@ -841,7 +841,7 @@ struct PreparedQuery {
841 std::uint32_t end_segment = no_segment; 841 std::uint32_t end_segment = no_segment;
842 std::uint32_t first_result = 0; 842 std::uint32_t first_result = 0;
843 std::uint32_t result_count = 0; 843 std::uint32_t result_count = 0;
844 // A pipeline-statistics query split around the resolves it contains (plan_statistics_segments): the physical 844 // A pipeline-statistics query split around the helpers it contains (plan_statistics_segments): the physical
845 // statistics segment this command ends or begins, and for its EndQuery the segments that hold its result. 845 // statistics segment this command ends or begins, and for its EndQuery the segments that hold its result.
846 std::uint32_t statistics_end = no_segment; 846 std::uint32_t statistics_end = no_segment;
847 std::uint32_t statistics_begin = no_segment; 847 std::uint32_t statistics_begin = no_segment;
@@ -851,6 +851,9 @@ struct PreparedQuery {
851}; 851};
852 852
853struct Prepared { 853struct Prepared {
854 // Helper commands split the active statistics query just like query resolves.
855 std::uint32_t statistics_end = PreparedQuery::no_segment;
856 std::uint32_t statistics_begin = PreparedQuery::no_segment;
854 PredicateCapture predicate; 857 PredicateCapture predicate;
855 std::vector<PredicatedCopyDispatch> predicated_copy; 858 std::vector<PredicatedCopyDispatch> predicated_copy;
856 ResourceBinding binding; 859 ResourceBinding binding;
@@ -1902,20 +1905,25 @@ inline ReplayResult plan_occlusion_segments(PreparedList& out) {
1902 return active.empty() ? ReplayResult::success : ReplayResult::invalid_query; 1905 return active.empty() ? ReplayResult::success : ReplayResult::invalid_query;
1903} 1906}
1904 1907
1905// A resolve inside a pipeline-statistics query can record a helper dispatch (the binary-occlusion normalisation, an 1908// Helper dispatches must not contribute to the application's CSInvocations (D3D-162). Query resolves and
1906// occlusion or a statistics sum), and Vulkan would count its invocations in the application's CSInvocations. Such a 1909// ordinary helper commands share one sequence of physical statistics segments. Resolve dispatch requirements
1907// query is therefore split: its counters go to physical segments that end before each resolve inside it and begin 1910// depend on heap state at planning time, so every non-timestamp resolve splits conservatively.
1908// again after it, and its result is the sum of those segments (D3D-162). Whether a resolve dispatches is only known
1909// when the list is planned against the heaps' state, so every resolve except a timestamp copy splits the query.
1910// A BeginQuery without its EndQuery in the list is left as it was.
1911inline void plan_statistics_segments(PreparedList& out) { 1911inline void plan_statistics_segments(PreparedList& out) {
1912 detail::PreparedQuery* begin = nullptr; 1912 detail::PreparedQuery* begin = nullptr;
1913 std::vector<detail::PreparedQuery*> inner; 1913 struct Split { std::uint32_t* end; std::uint32_t* begin; };
1914 for (auto& query : out.queries) { 1914 std::vector<Split> inner;
1915 const Command& command = out.list->commands()[query.command]; 1915 std::size_t next_query = 0;
1916 for (std::size_t i = 0; i < out.commands.size(); ++i) {
1917 auto& p = out.commands[i];
1918 if (begin && (p.predicate.scratch || !p.predicated_copy.empty() || p.has_depth24))
1919 inner.push_back({&p.statistics_end, &p.statistics_begin});
1920 if (next_query == out.queries.size() || out.queries[next_query].command != i) continue;
1921 auto& query = out.queries[next_query++];
1922 const Command& command = out.list->commands()[i];
1916 const auto range = query_range(command); 1923 const auto range = query_range(command);
1917 if (std::holds_alternative<ResolveQueries>(command)) { 1924 if (std::holds_alternative<ResolveQueries>(command)) {
1918 if (begin && range.type != d3d12_query::timestamp) inner.push_back(&query); 1925 if (begin && range.type != d3d12_query::timestamp)
1926 inner.push_back({&query.statistics_end, &query.statistics_begin});
1919 } else if (range.type == d3d12_query::pipeline_statistics) { 1927 } else if (range.type == d3d12_query::pipeline_statistics) {
1920 if (std::holds_alternative<BeginQuery>(command)) { 1928 if (std::holds_alternative<BeginQuery>(command)) {
1921 begin = &query; 1929 begin = &query;
@@ -1924,9 +1932,9 @@ inline void plan_statistics_segments(PreparedList& out) {
1924 if (!inner.empty()) { 1932 if (!inner.empty()) {
1925 const std::uint32_t first = out.statistics_segment_count; 1933 const std::uint32_t first = out.statistics_segment_count;
1926 begin->statistics_begin = out.statistics_segment_count++; 1934 begin->statistics_begin = out.statistics_segment_count++;
1927 for (auto* resolve : inner) { 1935 for (auto split : inner) {
1928 resolve->statistics_end = out.statistics_segment_count - 1; 1936 *split.end = out.statistics_segment_count - 1;
1929 resolve->statistics_begin = out.statistics_segment_count++; 1937 *split.begin = out.statistics_segment_count++;
1930 } 1938 }
1931 query.statistics_end = out.statistics_segment_count - 1; 1939 query.statistics_end = out.statistics_segment_count - 1;
1932 query.statistics_first = first; 1940 query.statistics_first = first;
@@ -1989,8 +1997,11 @@ inline bool zero_clear_unavailable(const PreparedList& prepared, const ReplayFun
1989 1997
1990// Resolves and checks every command. Nothing is written to a command buffer. 1998// Resolves and checks every command. Nothing is written to a command buffer.
1991// Resolve maps a ResourceId to a const ResourceBinding* (nullptr: unknown). 1999// Resolve maps a ResourceId to a const ResourceBinding* (nullptr: unknown).
2000// statistics_helpers promises a statistics segment pool at QueryReplay::plan(). Keep the default for replay()
2001// and callers without a summation helper: reject before allocating any helper scratch.
1992template<typename Resolve> 2002template<typename Resolve>
1993ReplayResult prepare(const CommandList& list, Resolve&& resolve, const ReplayFunctions& vk, PreparedList& out) { 2003ReplayResult prepare(const CommandList& list, Resolve&& resolve, const ReplayFunctions& vk, PreparedList& out,
2004 bool statistics_helpers = false) {
1994 if (!list.closed()) return ReplayResult::list_open; 2005 if (!list.closed()) return ReplayResult::list_open;
1995 if (list.queries_open()) return ReplayResult::invalid_query; // a Vulkan query must end in its command buffer 2006 if (list.queries_open()) return ReplayResult::invalid_query; // a Vulkan query must end in its command buffer
1996 const auto commands = list.commands(); 2007 const auto commands = list.commands();
@@ -2158,7 +2169,7 @@ ReplayResult prepare(const CommandList& list, Resolve&& resolve, const ReplayFun
2158 if (p.source.buffer == p.binding.buffer && src < dst + buffer_copy->size && dst < src + buffer_copy->size) 2169 if (p.source.buffer == p.binding.buffer && src < dst + buffer_copy->size && dst < src + buffer_copy->size)
2159 return ReplayResult::invalid_copy; 2170 return ReplayResult::invalid_copy;
2160 if (predicated) { 2171 if (predicated) {
2161 if (statistics_active || !vk.prepare_predicated_copy || !vk.emit_predicated_copy || 2172 if ((statistics_active && !statistics_helpers) || !vk.prepare_predicated_copy || !vk.emit_predicated_copy ||
2162 !vk.prepare_predicated_copy(p.source.buffer, p.binding.buffer, src, dst, buffer_copy->size, p.predicated_copy)) 2173 !vk.prepare_predicated_copy(p.source.buffer, p.binding.buffer, src, dst, buffer_copy->size, p.predicated_copy))
2163 return ReplayResult::unsupported; 2174 return ReplayResult::unsupported;
2164 } else p.buffer_copies.push_back({src, dst, buffer_copy->size}); 2175 } else p.buffer_copies.push_back({src, dst, buffer_copy->size});
@@ -2172,9 +2183,8 @@ ReplayResult prepare(const CommandList& list, Resolve&& resolve, const ReplayFun
2172 p.buffer_image_copies[0].imageSubresource.aspectMask == VK_IMAGE_ASPECT_DEPTH_BIT; 2183 p.buffer_image_copies[0].imageSubresource.aspectMask == VK_IMAGE_ASPECT_DEPTH_BIT;
2173 const bool rgb96_footprint = result == ReplayResult::success && image.rgb96 && 2184 const bool rgb96_footprint = result == ReplayResult::success && image.rgb96 &&
2174 !p.buffer_image_copies.empty(); 2185 !p.buffer_image_copies.empty();
2175 // These footprint conversions emit compute dispatches in emit(). Refuse them before allocating their 2186 // Without a segment pool, refuse before allocating conversion scratch/descriptors.
2176 // conversion scratch/descriptors because an active statistics query would include their CSInvocations. 2187 if (statistics_active && !statistics_helpers && (depth24_footprint || rgb96_footprint)) return ReplayResult::unsupported;
2177 if (statistics_active && (depth24_footprint || rgb96_footprint)) return ReplayResult::unsupported;
2178 if (depth24_footprint) { 2188 if (depth24_footprint) {
2179 const VkBufferImageCopy& region = p.buffer_image_copies[0]; 2189 const VkBufferImageCopy& region = p.buffer_image_copies[0];
2180 const ResourceBinding& buffer = upload ? p.source : p.binding; 2190 const ResourceBinding& buffer = upload ? p.source : p.binding;
@@ -2315,9 +2325,8 @@ ReplayResult prepare(const CommandList& list, Resolve&& resolve, const ReplayFun
2315 predicated = predication->buffer != 0; 2325 predicated = predication->buffer != 0;
2316 if (!predicated) continue; 2326 if (!predicated) continue;
2317 if (!conditional) return ReplayResult::unsupported; 2327 if (!conditional) return ReplayResult::unsupported;
2318 // The internal dispatch must not appear in the application's pipeline-statistics result. Virtualising 2328 // Without physical segments the capture dispatch would pollute CSInvocations.
2319 // that query is separate work; reject the combination before emitting any commands instead of lying. 2329 if (statistics_active && !statistics_helpers) return ReplayResult::unsupported;
2320 if (statistics_active) return ReplayResult::unsupported;
2321 if (!bind(predication->buffer)) return ReplayResult::unresolved_resource; 2330 if (!bind(predication->buffer)) return ReplayResult::unresolved_resource;
2322 if (!p.binding.buffer || !detail::within(predication->offset, 8, p.binding.size) || 2331 if (!p.binding.buffer || !detail::within(predication->offset, 8, p.binding.size) ||
2323 predication->offset > UINT64_MAX - p.binding.buffer_offset) 2332 predication->offset > UINT64_MAX - p.binding.buffer_offset)
@@ -2431,7 +2440,7 @@ ReplayResult prepare(const CommandList& list, Resolve&& resolve, const ReplayFun
2431class QueryReplay { 2440class QueryReplay {
2432public: 2441public:
2433 // `statistics_pool` holds the list's pipeline-statistics segments (plan_statistics_segments). Without one, the 2442 // `statistics_pool` holds the list's pipeline-statistics segments (plan_statistics_segments). Without one, the
2434 // list's statistics queries are not split, and a resolve inside one that needs a helper dispatch is refused. 2443 // list's statistics queries are not split: ordinary helper commands are refused, as are resolves that dispatch.
2435 ReplayResult plan(PreparedList& list, const std::shared_ptr<const OcclusionSegmentPool>& pool = {}, 2444 ReplayResult plan(PreparedList& list, const std::shared_ptr<const OcclusionSegmentPool>& pool = {},
2436 std::uint32_t base = 0, bool can_sum = false, 2445 std::uint32_t base = 0, bool can_sum = false,
2437 const std::shared_ptr<const OcclusionSegmentPool>& statistics_pool = {}, 2446 const std::shared_ptr<const OcclusionSegmentPool>& statistics_pool = {},
@@ -2448,6 +2457,9 @@ public:
2448 list.statistics_pool = statistics_pool->pool; 2457 list.statistics_pool = statistics_pool->pool;
2449 list.statistics_base = statistics_base; 2458 list.statistics_base = statistics_base;
2450 } else { 2459 } else {
2460 for (const auto& command : list.commands)
2461 if (command.statistics_end != detail::PreparedQuery::no_segment)
2462 return ReplayResult::unsupported;
2451 for (auto& query : list.queries) { 2463 for (auto& query : list.queries) {
2452 query.statistics_end = query.statistics_begin = detail::PreparedQuery::no_segment; 2464 query.statistics_end = query.statistics_begin = detail::PreparedQuery::no_segment;
2453 query.statistics_count = 0; 2465 query.statistics_count = 0;
@@ -2646,12 +2658,25 @@ inline void emit(const PreparedList& prepared, VkCommandBuffer command_buffer, c
2646 emit_query(prepared, prepared.queries[next_query++], command_buffer, vk, predicate); 2658 emit_query(prepared, prepared.queries[next_query++], command_buffer, vk, predicate);
2647 continue; 2659 continue;
2648 } 2660 }
2661 // Helpers run outside the per-command rendering scopes; both segment endpoints stay outside too.
2662 const auto end_statistics = [&] {
2663 if (p.statistics_end != detail::PreparedQuery::no_segment)
2664 vk.end_query(command_buffer, prepared.statistics_pool, prepared.statistics_base + p.statistics_end);
2665 };
2666 const auto begin_statistics = [&] {
2667 if (p.statistics_begin == detail::PreparedQuery::no_segment) return;
2668 const auto index = prepared.statistics_base + p.statistics_begin;
2669 vk.reset_query_pool(command_buffer, prepared.statistics_pool, index, 1);
2670 vk.begin_query(command_buffer, prepared.statistics_pool, index, 0);
2671 };
2649 if (const auto* predication = std::get_if<Predication>(&command)) { 2672 if (const auto* predication = std::get_if<Predication>(&command)) {
2650 // Conditional rendering cannot nest, so a new predicate ends the previous one first. 2673 // Conditional rendering cannot nest, so a new predicate ends the previous one first.
2651 if (predicate.buffer) vk.end_conditional_rendering(command_buffer); 2674 if (predicate.buffer) vk.end_conditional_rendering(command_buffer);
2652 predicate.buffer = predication->buffer ? p.predicate.scratch : VK_NULL_HANDLE; 2675 predicate.buffer = predication->buffer ? p.predicate.scratch : VK_NULL_HANDLE;
2653 if (predicate.buffer) { 2676 if (predicate.buffer) {
2677 end_statistics();
2654 vk.capture_predicate(command_buffer, p.predicate); 2678 vk.capture_predicate(command_buffer, p.predicate);
2679 begin_statistics();
2655 predicate.offset = p.predicate.offset + 8; 2680 predicate.offset = p.predicate.offset + 8;
2656 // Vulkan discards the commands when the word is 0, which is EQUAL_ZERO; INVERTED gives NOT_EQUAL_ZERO. 2681 // Vulkan discards the commands when the word is 0, which is EQUAL_ZERO; INVERTED gives NOT_EQUAL_ZERO.
2657 predicate.flags = predication->op == d3d12_predication::not_equal_zero 2682 predicate.flags = predication->op == d3d12_predication::not_equal_zero
@@ -2889,7 +2914,9 @@ inline void emit(const PreparedList& prepared, VkCommandBuffer command_buffer, c
2889 std::holds_alternative<CopyResource>(command)) { 2914 std::holds_alternative<CopyResource>(command)) {
2890 const auto [dst_id, src_id] = copy_ids(command); 2915 const auto [dst_id, src_id] = copy_ids(command);
2891 if (!p.predicated_copy.empty()) { 2916 if (!p.predicated_copy.empty()) {
2917 end_statistics();
2892 vk.emit_predicated_copy(command_buffer, p.predicated_copy); 2918 vk.emit_predicated_copy(command_buffer, p.predicated_copy);
2919 begin_statistics();
2893 } else if (!p.buffer_copies.empty()) { 2920 } else if (!p.buffer_copies.empty()) {
2894 vk.copy_buffer(command_buffer, p.source.buffer, p.binding.buffer, std::uint32_t(p.buffer_copies.size()), 2921 vk.copy_buffer(command_buffer, p.source.buffer, p.binding.buffer, std::uint32_t(p.buffer_copies.size()),
2895 p.buffer_copies.data()); 2922 p.buffer_copies.data());
@@ -2944,7 +2971,9 @@ inline void emit(const PreparedList& prepared, VkCommandBuffer command_buffer, c
2944 detail::memory_barrier(vk, command_buffer, VK_ACCESS_MEMORY_WRITE_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, 2971 detail::memory_barrier(vk, command_buffer, VK_ACCESS_MEMORY_WRITE_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
2945 VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT, 2972 VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT,
2946 VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT); 2973 VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT);
2974 end_statistics();
2947 for (const auto& part : p.depth24_parts) run_depth24(part); 2975 for (const auto& part : p.depth24_parts) run_depth24(part);
2976 begin_statistics();
2948 detail::memory_barrier(vk, command_buffer, VK_ACCESS_SHADER_WRITE_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 2977 detail::memory_barrier(vk, command_buffer, VK_ACCESS_SHADER_WRITE_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
2949 VK_ACCESS_TRANSFER_READ_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT); 2978 VK_ACCESS_TRANSFER_READ_BIT, VK_PIPELINE_STAGE_TRANSFER_BIT);
2950 converted = p.buffer_image_copies; 2979 converted = p.buffer_image_copies;
@@ -2976,7 +3005,9 @@ inline void emit(const PreparedList& prepared, VkCommandBuffer command_buffer, c
2976 detail::memory_barrier(vk, command_buffer, VK_ACCESS_MEMORY_WRITE_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT, 3005 detail::memory_barrier(vk, command_buffer, VK_ACCESS_MEMORY_WRITE_BIT, VK_PIPELINE_STAGE_ALL_COMMANDS_BIT,
2977 VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT, 3006 VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT,
2978 VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT); 3007 VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT);
3008 end_statistics();
2979 for (const auto& part : p.depth24_parts) run_depth24(part); 3009 for (const auto& part : p.depth24_parts) run_depth24(part);
3010 begin_statistics();
2980 detail::memory_barrier(vk, command_buffer, VK_ACCESS_SHADER_WRITE_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 3011 detail::memory_barrier(vk, command_buffer, VK_ACCESS_SHADER_WRITE_BIT, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
2981 VK_ACCESS_MEMORY_READ_BIT | VK_ACCESS_MEMORY_WRITE_BIT, 3012 VK_ACCESS_MEMORY_READ_BIT | VK_ACCESS_MEMORY_WRITE_BIT,
2982 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT); 3013 VK_PIPELINE_STAGE_ALL_COMMANDS_BIT);
diff --git a/runtime/test/d3d12_predicated_copy_test.cpp b/runtime/test/d3d12_predicated_copy_test.cpp
index 37206ecc0..9b61ed0a4 100644
--- a/runtime/test/d3d12_predicated_copy_test.cpp
+++ b/runtime/test/d3d12_predicated_copy_test.cpp
@@ -44,7 +44,7 @@ void transition(ID3D12GraphicsCommandList* list, ID3D12Resource* resource,
44 list->ResourceBarrier(1, &barrier); 44 list->ResourceBarrier(1, &barrier);
45} 45}
46bool run(ID3D12Device* device, const Case& c, UINT64 predicate, D3D12_PREDICATION_OP op, 46bool run(ID3D12Device* device, const Case& c, UINT64 predicate, D3D12_PREDICATION_OP op,
47 bool enabled, std::vector<unsigned char>& actual) { 47 bool enabled, std::vector<unsigned char>& actual, bool statistics_query = false) {
48 Ref<ID3D12Resource> source, destination, seed, condition, readback; 48 Ref<ID3D12Resource> source, destination, seed, condition, readback;
49 CHECK(buffer(device,c.source_bytes,D3D12_HEAP_TYPE_UPLOAD,D3D12_RESOURCE_STATE_GENERIC_READ,source)); 49 CHECK(buffer(device,c.source_bytes,D3D12_HEAP_TYPE_UPLOAD,D3D12_RESOURCE_STATE_GENERIC_READ,source));
50 CHECK(buffer(device,c.destination_bytes,D3D12_HEAP_TYPE_DEFAULT,D3D12_RESOURCE_STATE_COPY_DEST,destination)); 50 CHECK(buffer(device,c.destination_bytes,D3D12_HEAP_TYPE_DEFAULT,D3D12_RESOURCE_STATE_COPY_DEST,destination));
@@ -72,14 +72,37 @@ bool run(ID3D12Device* device, const Case& c, UINT64 predicate, D3D12_PREDICATIO
72 list->ResourceBarrier(1,&seed_barrier); 72 list->ResourceBarrier(1,&seed_barrier);
73 std::swap(seed_barrier.Transition.StateBefore,seed_barrier.Transition.StateAfter); 73 std::swap(seed_barrier.Transition.StateBefore,seed_barrier.Transition.StateAfter);
74 list->ResourceBarrier(1,&seed_barrier); 74 list->ResourceBarrier(1,&seed_barrier);
75 Ref<ID3D12QueryHeap> statistics;
76 Ref<ID3D12Resource> counters;
77 if (statistics_query) {
78 const D3D12_QUERY_HEAP_DESC query_desc{D3D12_QUERY_HEAP_TYPE_PIPELINE_STATISTICS,1,0};
79 CHECK(SUCCEEDED(device->CreateQueryHeap(&query_desc,IID_PPV_ARGS(&statistics.p))));
80 CHECK(buffer(device,88,D3D12_HEAP_TYPE_READBACK,D3D12_RESOURCE_STATE_COPY_DEST,counters));
81 list->BeginQuery(statistics.p,D3D12_QUERY_TYPE_PIPELINE_STATISTICS,0);
82 }
75 if(enabled) list->SetPredication(condition.p,8,op); 83 if(enabled) list->SetPredication(condition.p,8,op);
76 list->CopyBufferRegion(destination.p,c.destination_offset,source.p,c.source_offset,c.bytes); 84 list->CopyBufferRegion(destination.p,c.destination_offset,source.p,c.source_offset,c.bytes);
77 list->SetPredication(nullptr,0,D3D12_PREDICATION_OP_EQUAL_ZERO); 85 list->SetPredication(nullptr,0,D3D12_PREDICATION_OP_EQUAL_ZERO);
86 if (statistics_query) {
87 list->EndQuery(statistics.p,D3D12_QUERY_TYPE_PIPELINE_STATISTICS,0);
88 list->ResolveQueryData(statistics.p,D3D12_QUERY_TYPE_PIPELINE_STATISTICS,0,1,counters.p,0);
89 }
78 D3D12_RESOURCE_BARRIER barrier{}; barrier.Type=D3D12_RESOURCE_BARRIER_TYPE_TRANSITION; 90 D3D12_RESOURCE_BARRIER barrier{}; barrier.Type=D3D12_RESOURCE_BARRIER_TYPE_TRANSITION;
79 barrier.Transition.pResource=destination.p; barrier.Transition.Subresource=D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES; 91 barrier.Transition.pResource=destination.p; barrier.Transition.Subresource=D3D12_RESOURCE_BARRIER_ALL_SUBRESOURCES;
80 barrier.Transition.StateBefore=D3D12_RESOURCE_STATE_COPY_DEST; barrier.Transition.StateAfter=D3D12_RESOURCE_STATE_COPY_SOURCE; 92 barrier.Transition.StateBefore=D3D12_RESOURCE_STATE_COPY_DEST; barrier.Transition.StateAfter=D3D12_RESOURCE_STATE_COPY_SOURCE;
81 list->ResourceBarrier(1,&barrier); list->CopyBufferRegion(readback.p,0,destination.p,0,c.destination_bytes); 93 list->ResourceBarrier(1,&barrier); list->CopyBufferRegion(readback.p,0,destination.p,0,c.destination_bytes);
82 CHECK(finish(list.p, queue.p, fence.p)); 94 CHECK(finish(list.p, queue.p, fence.p));
95 if (statistics_query) {
96 void* values=nullptr; const D3D12_RANGE range{0,88}, none{};
97 CHECK(SUCCEEDED(counters->Map(0,&range,&values)) && values);
98 bool zero=true;
99 for (unsigned word=0; word<11; ++word) {
100 UINT64 value=UINT64_MAX;
101 std::memcpy(&value,static_cast<const char*>(values)+8*word,8);
102 if (value) { std::fprintf(stderr,"statistics word %u: %llu, expected zero\n",word,value); zero=false; }
103 }
104 counters->Unmap(0,&none); CHECK(zero);
105 }
83 void* mapped=nullptr; const D3D12_RANGE range{0,SIZE_T(c.destination_bytes)}; 106 void* mapped=nullptr; const D3D12_RANGE range{0,SIZE_T(c.destination_bytes)};
84 CHECK(SUCCEEDED(readback->Map(0,&range,&mapped))&&mapped); 107 CHECK(SUCCEEDED(readback->Map(0,&range,&mapped))&&mapped);
85 actual.assign(static_cast<unsigned char*>(mapped),static_cast<unsigned char*>(mapped)+c.destination_bytes); 108 actual.assign(static_cast<unsigned char*>(mapped),static_cast<unsigned char*>(mapped)+c.destination_bytes);
@@ -211,6 +234,7 @@ int main(int argc,char** argv) {
211 info.conditional_rendering=vk.conditional_rendering; 234 info.conditional_rendering=vk.conditional_rendering;
212 info.storage_buffer_8bit=vk.storage_buffer_8bit; 235 info.storage_buffer_8bit=vk.storage_buffer_8bit;
213 info.occlusion_query_precise=vk.occlusion_query_precise; 236 info.occlusion_query_precise=vk.occlusion_query_precise;
237 info.pipeline_statistics_query=vk.pipeline_statistics_query;
214 if(FAILED(rt::d3d12::create_vulkan_device(info,IID_PPV_ARGS(&ours.p))))return 1; 238 if(FAILED(rt::d3d12::create_vulkan_device(info,IID_PPV_ARGS(&ours.p))))return 1;
215 } 239 }
216 const Case cases[]={{1,1,0,0,1},{2,3,1,2,1},{7,11,1,3,5},{13,17,3,1,7},{33,35,0,3,32}, 240 const Case cases[]={{1,1,0,0,1},{2,3,1,2,1},{7,11,1,3,5},{13,17,3,1,7},{33,35,0,3,32},
@@ -252,6 +276,15 @@ int main(int argc,char** argv) {
252 ++query_cases; 276 ++query_cases;
253 } 277 }
254 } 278 }
279 // D3D-162: capture and byte-copy helpers are invisible in every statistics counter, whether executed or skipped.
280 if (!native_only && vk.pipeline_statistics_query) {
281 for (UINT64 value : {UINT64_C(0),UINT64_C(1)<<32})
282 for (auto op : {D3D12_PREDICATION_OP_EQUAL_ZERO,D3D12_PREDICATION_OP_NOT_EQUAL_ZERO}) {
283 std::vector<unsigned char> expected,actual;
284 if (!run(host.p,cases[3],value,op,true,expected,true) ||
285 !run(ours.p,cases[3],value,op,true,actual,true) || actual!=expected) return 1;
286 }
287 }
255 if(!native_only&&vk.validation_errors())return 1; 288 if(!native_only&&vk.validation_errors())return 1;
256 std::printf("query-to-predicated-copy: %u exact cases, %u recapture controls; GPU overwrite and predicate guards exact\n",query_cases,recapture_controls); 289 std::printf("query-to-predicated-copy: %u exact cases, %u recapture controls; GPU overwrite and predicate guards exact\n",query_cases,recapture_controls);
257 std::printf("predicated buffer copies: %u exact cases (%s)\n",count,native_only?"WARP + literal byte oracle":"Vulkan + WARP + literal byte oracle"); 290 std::printf("predicated buffer copies: %u exact cases (%s)\n",count,native_only?"WARP + literal byte oracle":"Vulkan + WARP + literal byte oracle");
diff --git a/runtime/test/depth24_convert_test.cpp b/runtime/test/depth24_convert_test.cpp
index 89e98b091..dd8d35b85 100644
--- a/runtime/test/depth24_convert_test.cpp
+++ b/runtime/test/depth24_convert_test.cpp
@@ -253,6 +253,7 @@ int main() {
253 VulkanContext context(gpu.device_proc, gpu.device, true); 253 VulkanContext context(gpu.device_proc, gpu.device, true);
254 EXPECT(context.loaded()); 254 EXPECT(context.loaded());
255 context.enable_depth24(gpu.device_proc, alloc_device_local); 255 context.enable_depth24(gpu.device_proc, alloc_device_local);
256 context.set_occlusion_resolve_memory(mem_props);
256 257
257 constexpr int kIters = 16; 258 constexpr int kIters = 16;
258 constexpr std::uint32_t tex_w = 64, tex_h = 16; 259 constexpr std::uint32_t tex_w = 64, tex_h = 16;
@@ -475,7 +476,23 @@ int main() {
475 rgb96_b.rgb96 = true; 476 rgb96_b.rgb96 = true;
476 context.add_resource(id_rgb96_img, rgb96_b, VK_IMAGE_LAYOUT_UNDEFINED); 477 context.add_resource(id_rgb96_img, rgb96_b, VK_IMAGE_LAYOUT_UNDEFINED);
477 478
479 // D3D-162: both conversion directions and formats must leave all 11 application counters zero.
480 VkQueryPool statistics = VK_NULL_HANDLE;
481 void* statistics_data = nullptr;
482 if (gpu.pipeline_statistics_query) {
483 VkQueryPoolCreateInfo info{VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO};
484 info.queryType = VK_QUERY_TYPE_PIPELINE_STATISTICS;
485 info.queryCount = 1; info.pipelineStatistics = 0x7ff;
486 EXPECT(gpu.fn<PFN_vkCreateQueryPool>("vkCreateQueryPool")(gpu.device, &info, nullptr, &statistics) == VK_SUCCESS);
487 context.add_query_pool(100, {statistics, d3d12_query_heap::pipeline_statistics, 1, false, {false}, {}});
488 ResourceBinding counters{}; counters.size = 88; counters.host_readable = true;
489 counters.buffer = gpu.buffer(88, VK_BUFFER_USAGE_TRANSFER_DST_BIT, &statistics_data);
490 EXPECT(counters.buffer && statistics_data);
491 std::memset(statistics_data, 0xcd, 88);
492 context.add_resource(100, counters);
493 }
478 CommandList slice_list; 494 CommandList slice_list;
495 if (statistics) EXPECT(slice_list.begin_query({100, d3d12_query::pipeline_statistics, 0}));
479 EXPECT(slice_list.copy_resource({id_copy, id_src})); 496 EXPECT(slice_list.copy_resource({id_copy, id_src}));
480 const CopyLocation fp_src{id_src, true, 0, 0, w, h, 1, w * 4}; 497 const CopyLocation fp_src{id_src, true, 0, 0, w, h, 1, w * 4};
481 const CopyLocation fp_rgba_rb{id_rgba_rb, true, 0, 0, w, h, 1, w * 4}; 498 const CopyLocation fp_rgba_rb{id_rgba_rb, true, 0, 0, w, h, 1, w * 4};
@@ -491,6 +508,10 @@ int main() {
491 EXPECT(slice_list.copy_texture({fp_d24_rb, 0, 0, 0, loc_d24, false, {}})); 508 EXPECT(slice_list.copy_texture({fp_d24_rb, 0, 0, 0, loc_d24, false, {}}));
492 EXPECT(slice_list.copy_texture({loc_rgb96, 0, 0, 0, fp_rgb96_src, false, {}})); 509 EXPECT(slice_list.copy_texture({loc_rgb96, 0, 0, 0, fp_rgb96_src, false, {}}));
493 EXPECT(slice_list.copy_texture({fp_rgb96_rb, 0, 0, 0, loc_rgb96, false, {}})); 510 EXPECT(slice_list.copy_texture({fp_rgb96_rb, 0, 0, 0, loc_rgb96, false, {}}));
511 if (statistics) {
512 EXPECT(slice_list.end_query({100, d3d12_query::pipeline_statistics, 0}));
513 EXPECT(slice_list.resolve_queries({100, d3d12_query::pipeline_statistics, 0, 1, 100, 0}));
514 }
494 EXPECT(slice_list.close()); 515 EXPECT(slice_list.close());
495 516
496 const CommandList* ptr = &slice_list; 517 const CommandList* ptr = &slice_list;
@@ -531,6 +552,14 @@ int main() {
531 for (std::size_t i = 0; i < arena_bytes / 4; ++i) 552 for (std::size_t i = 0; i < arena_bytes / 4; ++i)
532 if (!in_written(i)) EXPECT(arena_ptr[i] == guard_word); 553 if (!in_written(i)) EXPECT(arena_ptr[i] == guard_word);
533 554
555 if (statistics) {
556 for (unsigned word = 0; word < 11; ++word) {
557 std::uint64_t value = UINT64_MAX;
558 std::memcpy(&value, static_cast<const char*>(statistics_data) + 8 * word, 8);
559 EXPECT(value == 0);
560 }
561 gpu.fn<PFN_vkDestroyQueryPool>("vkDestroyQueryPool")(gpu.device, statistics, nullptr);
562 }
534 std::printf("depth24: shared-buffer offsets (CopyResource, RGBA8, depth24 and rgb96 CopyTexture) ok\n"); 563 std::printf("depth24: shared-buffer offsets (CopyResource, RGBA8, depth24 and rgb96 CopyTexture) ok\n");
535 564
536 // Host-readback barrier observation across logical queue kinds: compute-converted depth24 and rgb96 565 // Host-readback barrier observation across logical queue kinds: compute-converted depth24 and rgb96
diff --git a/runtime/test/predicated_copy_test.cpp b/runtime/test/predicated_copy_test.cpp
index 95e8da685..3eaf0b161 100644
--- a/runtime/test/predicated_copy_test.cpp
+++ b/runtime/test/predicated_copy_test.cpp
@@ -72,7 +72,11 @@ int main(){
72 EXPECT(statistics.begin_query({1,d3d12_query::pipeline_statistics,0})); 72 EXPECT(statistics.begin_query({1,d3d12_query::pipeline_statistics,0}));
73 EXPECT(statistics.copy_buffer({2,0,1,0,1})); 73 EXPECT(statistics.copy_buffer({2,0,1,0,1}));
74 EXPECT(statistics.end_query({1,d3d12_query::pipeline_statistics,0})&&statistics.close()); 74 EXPECT(statistics.end_query({1,d3d12_query::pipeline_statistics,0})&&statistics.close());
75 EXPECT(prepare(statistics,resolve,vk,result)==ReplayResult::unsupported); 75 const auto before=prepared;
76 EXPECT(prepare(statistics,resolve,vk,result)==ReplayResult::unsupported && prepared==before);
77 EXPECT(prepare(statistics,resolve,vk,result,true)==ReplayResult::success && prepared==before+1);
78 EXPECT(result.statistics_segment_count==2);
79 EXPECT(result.commands[2].statistics_end==0 && result.commands[2].statistics_begin==1);
76 // Readback visibility follows the actual producer, including mixed command lists. 80 // Readback visibility follows the actual producer, including mixed command lists.
77 destination.host_readable = false; 81 destination.host_readable = false;
78 EXPECT(check({2,0,1,0,1})==ReplayResult::success && !result.wrote_readback && !result.readback_src_stages); 82 EXPECT(check({2,0,1,0,1})==ReplayResult::success && !result.wrote_readback && !result.readback_src_stages);
diff --git a/runtime/test/replay_query_predication_test.cpp b/runtime/test/replay_query_predication_test.cpp
index 6c74e7ed9..621fb4eee 100644
--- a/runtime/test/replay_query_predication_test.cpp
+++ b/runtime/test/replay_query_predication_test.cpp
@@ -2,7 +2,8 @@
2// Windows Vulkan driver, vkCmdFillBuffer after PredicateReducer::capture's compute dispatch is suppressed inside 2// Windows Vulkan driver, vkCmdFillBuffer after PredicateReducer::capture's compute dispatch is suppressed inside
3// an active conditional rendering scope (D3D-148). Check that emit() suspends conditional rendering for the 3// an active conditional rendering scope (D3D-148). Check that emit() suspends conditional rendering for the
4// entire ResolveQueryData loop (not only before helper dispatches) and restores it afterward. Cases 5 and 6 check 4// entire ResolveQueryData loop (not only before helper dispatches) and restores it afterward. Cases 5 and 6 check
5// how a resolve inside a pipeline-statistics query is replayed (D3D-162). No GPU or game data. 5// how resolves inside a pipeline-statistics query are replayed; cases 7 and 8 cover the other helper dispatches
6// and their no-pool fallback (D3D-162). No GPU or game data.
6#include "../src/vulkan_replay.h" 7#include "../src/vulkan_replay.h"
7#include <cstdint> 8#include <cstdint>
8#include <cstdio> 9#include <cstdio>
@@ -246,6 +247,129 @@ int main() {
246 "split list: the heap keeps the query's segments for later resolves"); 247 "split list: the heap keeps the query's segments for later resolves");
247 } 248 }
248 249
250 // 7. All non-query helpers and a resolve share the segment numbering. Exercise both footprint
251 // directions/formats and multiple conversion dispatches under an active predicate.
252 for (bool rgb96 : {false, true}) for (bool upload : {false, true}) {
253 Trace trace;
254 g_trace = &trace;
255 auto helpers = vk;
256 unsigned allocations = 0;
257 helpers.prepare_predicate = [&](VkBuffer source, VkDeviceSize offset, PredicateCapture& out) {
258 ++allocations;
259 out = {source, offset, handle<VkBuffer>(4), 0, handle<VkDescriptorSet>(5)};
260 return true;
261 };
262 helpers.prepare_predicated_copy = [&](VkBuffer, VkBuffer, VkDeviceSize, VkDeviceSize, VkDeviceSize,
263 std::vector<PredicatedCopyDispatch>& jobs) {
264 ++allocations;
265 jobs.push_back({});
266 return true;
267 };
268 helpers.emit_predicated_copy = [](VkCommandBuffer, const std::vector<PredicatedCopyDispatch>&) {
269 expect(g_trace->cond_active, "copy helper remains predicated");
270 g_trace->events.push_back("predicated_copy");
271 };
272 helpers.depth24_prepare = [&](Depth24Job& job, std::vector<Depth24Job>& parts) {
273 ++allocations;
274 job.scratch = handle<VkBuffer>(10);
275 parts.assign(2, job);
276 return true;
277 };
278 helpers.depth24_run = [](VkCommandBuffer, const Depth24Job&) {
279 expect(!g_trace->cond_active, "footprint helper is unconditional");
280 g_trace->events.push_back("footprint");
281 };
282 helpers.pipeline_barrier = +[](VkCommandBuffer, VkPipelineStageFlags, VkPipelineStageFlags,
283 VkDependencyFlags, std::uint32_t, const VkMemoryBarrier*, std::uint32_t,
284 const VkBufferMemoryBarrier*, std::uint32_t, const VkImageMemoryBarrier*) {};
285 helpers.copy_buffer = +[](VkCommandBuffer, VkBuffer, VkBuffer, std::uint32_t, const VkBufferCopy*) {};
286 helpers.copy_image = +[](VkCommandBuffer, VkImage, VkImageLayout, VkImage, VkImageLayout,
287 std::uint32_t, const VkImageCopy*) {};
288 helpers.copy_buffer_to_image = +[](VkCommandBuffer, VkBuffer, VkImage, VkImageLayout,
289 std::uint32_t, const VkBufferImageCopy*) {
290 g_trace->events.push_back("upload");
291 };
292 helpers.copy_image_to_buffer = +[](VkCommandBuffer, VkImage, VkImageLayout, VkBuffer,
293 std::uint32_t, const VkBufferImageCopy*) {
294 g_trace->events.push_back("readback");
295 };
296 ResourceBinding buffer = dst;
297 buffer.buffer_offset = 0;
298 buffer.size = 512;
299 ResourceBinding image{};
300 image.image = handle<VkImage>(11);
301 image.format = rgb96 ? VK_FORMAT_R32G32B32A32_UINT : VK_FORMAT_D32_SFLOAT_S8_UINT;
302 image.aspects = rgb96 ? VK_IMAGE_ASPECT_COLOR_BIT : VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT;
303 image.width = image.height = image.depth = image.mip_levels = image.array_layers = image.samples = 1;
304 image.rgb96 = rgb96;
305 image.depth24 = image.depth24_as_float = !rgb96;
306 const auto resources = [&](ResourceId id) -> const ResourceBinding* {
307 return id == 1 ? &buffer : id == 2 ? &pred : id == 3 ? &image : nullptr;
308 };
309 const CopyLocation texture{3, false, 0, 0, 0, 0, 0, 0};
310 const CopyLocation footprint{1, true, 0, 0, 1, 1, 1, 256};
311 CommandList list;
312 expect(list.begin_query({6, d3d12_query::pipeline_statistics, 0}), "mixed begin");
313 expect(list.set_predication({2, 0, d3d12_predication::equal_zero}), "mixed capture");
314 expect(list.resolve_queries({1, d3d12_query::binary_occlusion, 1, 1, 1, 0}), "mixed resolve");
315 expect(list.copy_buffer({1, 0, 2, 0, 8}), "mixed predicated copy");
316 expect(list.copy_texture({upload ? texture : footprint, 0, 0, 0,
317 upload ? footprint : texture, false, {}}), "mixed footprint copy");
318 expect(list.end_query({6, d3d12_query::pipeline_statistics, 0}), "mixed end");
319 expect(list.resolve_queries({6, d3d12_query::pipeline_statistics, 0, 1, 1, 128}), "mixed sum");
320 expect(list.close(), "close mixed list");
321 PreparedList prepared;
322 expect(prepare(list, resources, helpers, prepared, true) == ReplayResult::success, "prepare mixed helpers");
323 expect(prepared.statistics_segment_count == 5 && allocations == 3, "five shared segments");
324 QueryReplay no_pool;
325 expect(no_pool.plan(prepared, {}, 0, true) == ReplayResult::unsupported,
326 "plan refuses helper markers without statistics pool");
327 expect(trace.events.empty(), "no-pool plan emits nothing");
328 auto pool = std::make_shared<OcclusionSegmentPool>();
329 pool->pool = handle<VkQueryPool>(9);
330 pool->destroy = mock_destroy_pool;
331 QueryReplay queries;
332 expect(queries.plan(prepared, {}, 0, true, pool, 4) == ReplayResult::success, "plan mixed helpers");
333 expect(queries.needs_binary && queries.needs_statistics_sum, "mixed helpers require statistics sum");
334 emit(prepared, VK_NULL_HANDLE, helpers);
335 std::vector<std::string> want{
336 "begin_query 9:4", "end_query 9:4", "capture", "begin_query 9:5", "begin_cond",
337 "end_query 9:5", "end_cond", "binary", "begin_cond", "begin_query 9:6",
338 "end_query 9:6", "predicated_copy", "begin_query 9:7"};
339 if (!upload) want.push_back("readback");
340 const std::vector<std::string> conversion{
341 "end_query 9:7", "end_cond", "footprint", "begin_cond", "end_cond", "footprint", "begin_cond",
342 "begin_query 9:8"};
343 want.insert(want.end(), conversion.begin(), conversion.end());
344 if (upload) want.push_back("upload");
345 const std::vector<std::string> tail{
346 "end_query 9:8", "end_cond", "sum_statistics 9:4+5", "begin_cond", "end_cond"};
347 want.insert(want.end(), tail.begin(), tail.end());
348 expect(trace.events == want, "mixed helpers: exact segment and conditional scope order");
349
350 // 8. Fallback checks each helper separately, before its scratch allocation and before emission.
351 for (unsigned kind = 0; kind != 3; ++kind) {
352 CommandList fallback;
353 if (kind == 1) expect(fallback.set_predication({2, 0, 0}), "fallback pre-query predicate");
354 expect(fallback.begin_query({6, d3d12_query::pipeline_statistics, 0}), "fallback begin");
355 if (kind == 0) expect(fallback.set_predication({2, 0, 0}), "fallback capture");
356 if (kind == 1) expect(fallback.copy_buffer({1, 0, 2, 0, 8}), "fallback copy");
357 if (kind == 2) expect(fallback.copy_texture({upload ? texture : footprint, 0, 0, 0,
358 upload ? footprint : texture, false, {}}), "fallback footprint");
359 expect(fallback.end_query({6, d3d12_query::pipeline_statistics, 0}) && fallback.close(), "fallback end");
360 allocations = 0;
361 trace.events.clear();
362 expect(replay(fallback, VK_NULL_HANDLE, resources, helpers) == ReplayResult::unsupported,
363 "single-list fallback refuses helper");
364 expect(trace.events.empty() && allocations == (kind == 1 ? 1u : 0u),
365 "fallback refuses before helper scratch and emits nothing");
366 expect(prepare(fallback, resources, helpers, prepared, true) == ReplayResult::success,
367 "prepare fallback for explicit pool check");
368 QueryReplay missing;
369 expect(missing.plan(prepared) == ReplayResult::unsupported, "each helper needs a statistics pool");
370 }
371 }
372
249 if (failures) return 1; 373 if (failures) return 1;
250 std::puts("replay_query_predication_test: query resolves suspend conditional rendering around fills, copies and dispatches"); 374 std::puts("replay_query_predication_test: query resolves suspend conditional rendering around fills, copies and dispatches");
251 return 0; 375 return 0;
diff --git a/runtime/test/typeless_rtv_test.cpp b/runtime/test/typeless_rtv_test.cpp
index be935c123..9885acdc1 100644
--- a/runtime/test/typeless_rtv_test.cpp
+++ b/runtime/test/typeless_rtv_test.cpp
@@ -173,6 +173,8 @@ int main() {
173 const auto resolve = [&](ResourceId id) { return id == 2 ? &predicate : &binding; }; 173 const auto resolve = [&](ResourceId id) { return id == 2 ? &predicate : &binding; };
174 EXPECT(prepare(list, resolve, queries, prepared) == 174 EXPECT(prepare(list, resolve, queries, prepared) ==
175 (inside ? ReplayResult::unsupported : ReplayResult::success)); 175 (inside ? ReplayResult::unsupported : ReplayResult::success));
176 EXPECT(prepare(list, resolve, queries, prepared, true) == ReplayResult::success);
177 EXPECT(prepared.statistics_segment_count == (inside ? 2u : 0u));
176 ++checks; 178 ++checks;
177 } 179 }
178 } 180 }
diff --git a/runtime/test/vk_predicated_copy_test.cpp b/runtime/test/vk_predicated_copy_test.cpp
index fd214ae0d..f77eb1aac 100644
--- a/runtime/test/vk_predicated_copy_test.cpp
+++ b/runtime/test/vk_predicated_copy_test.cpp
@@ -33,6 +33,7 @@ int main() {
33 limits.maxStorageBufferRange=static_cast<std::uint32_t>(std::max<VkDeviceSize>(256,limits.minStorageBufferOffsetAlignment)); 33 limits.maxStorageBufferRange=static_cast<std::uint32_t>(std::max<VkDeviceSize>(256,limits.minStorageBufferOffsetAlignment));
34 limits.maxComputeWorkGroupCount[0]=1; 34 limits.maxComputeWorkGroupCount[0]=1;
35 VulkanContext context(d.device_proc,d.device,true,true); 35 VulkanContext context(d.device_proc,d.device,true,true);
36 context.set_occlusion_resolve_memory(memory);
36 context.configure_predication(d.device_proc,memory,properties.limits.maxStorageBufferRange,d.family); 37 context.configure_predication(d.device_proc,memory,properties.limits.maxStorageBufferRange,d.family);
37 VulkanQueue queue(context,d.queue,d.family); 38 VulkanQueue queue(context,d.queue,d.family);
38 VulkanFence gate(context,0); 39 VulkanFence gate(context,0);
@@ -51,7 +52,21 @@ int main() {
51 ResourceBinding b{};b.buffer=dst;b.buffer_offset=3;b.size=bytes-3;b.host_readable=true; 52 ResourceBinding b{};b.buffer=dst;b.buffer_offset=3;b.size=bytes-3;b.host_readable=true;
52 ResourceBinding p{};p.buffer=pred;p.size=16; 53 ResourceBinding p{};p.buffer=pred;p.size=16;
53 context.add_resource(1,a);context.add_resource(2,b);context.add_resource(3,p); 54 context.add_resource(1,a);context.add_resource(2,b);context.add_resource(3,p);
55 // D3D-162: capture/rebind and executing/suppressed copy dispatches inside one empty statistics query.
56 VkQueryPool statistics = VK_NULL_HANDLE;
57 void* statistics_data = nullptr;
58 if (d.pipeline_statistics_query) {
59 VkQueryPoolCreateInfo info{VK_STRUCTURE_TYPE_QUERY_POOL_CREATE_INFO};
60 info.queryType = VK_QUERY_TYPE_PIPELINE_STATISTICS; info.queryCount = 1; info.pipelineStatistics = 0x7ff;
61 EXPECT(d.fn<PFN_vkCreateQueryPool>("vkCreateQueryPool")(d.device, &info, nullptr, &statistics) == VK_SUCCESS);
62 context.add_query_pool(4, {statistics, d3d12_query_heap::pipeline_statistics, 1, false, {false}, {}});
63 ResourceBinding counters{}; counters.size = 88; counters.host_readable = true;
64 counters.buffer = d.buffer(counters.size, VK_BUFFER_USAGE_TRANSFER_DST_BIT, &statistics_data);
65 EXPECT(counters.buffer && statistics_data);
66 std::memset(statistics_data, 0xcd, 88); context.add_resource(5, counters);
67 }
54 CommandList list; 68 CommandList list;
69 if (statistics) EXPECT(list.begin_query({4,d3d12_query::pipeline_statistics,0}));
55 EXPECT(list.set_predication({3,0,d3d12_predication::equal_zero})); 70 EXPECT(list.set_predication({3,0,d3d12_predication::equal_zero}));
56 EXPECT(list.copy_buffer({2,4,1,2,4096})); // high-word-only condition; offsets include arena prefixes 71 EXPECT(list.copy_buffer({2,4,1,2,4096})); // high-word-only condition; offsets include arena prefixes
57 EXPECT(list.copy_buffer({2,6000,2,4,33})); // dependent, non-overlapping copy within one VkBuffer 72 EXPECT(list.copy_buffer({2,6000,2,4,33})); // dependent, non-overlapping copy within one VkBuffer
@@ -61,6 +76,10 @@ int main() {
61 EXPECT(list.copy_buffer({2,7000,1,0,37}));// suppressed: high word must not be truncated 76 EXPECT(list.copy_buffer({2,7000,1,0,37}));// suppressed: high word must not be truncated
62 EXPECT(list.set_predication({0,0,0})); 77 EXPECT(list.set_predication({0,0,0}));
63 EXPECT(list.copy_buffer({2,5000,1,0,17}));// reset restores ordinary transfer path 78 EXPECT(list.copy_buffer({2,5000,1,0,17}));// reset restores ordinary transfer path
79 if (statistics) {
80 EXPECT(list.end_query({4,d3d12_query::pipeline_statistics,0}));
81 EXPECT(list.resolve_queries({4,d3d12_query::pipeline_statistics,0,1,5,0}));
82 }
64 EXPECT(list.close()); 83 EXPECT(list.close());
65 std::memcpy(expected.data()+7,input.data()+3,4096); 84 std::memcpy(expected.data()+7,input.data()+3,4096);
66 std::memcpy(expected.data()+6003,expected.data()+7,33); 85 std::memcpy(expected.data()+6003,expected.data()+7,33);
@@ -99,13 +118,22 @@ int main() {
99 EXPECT(host_stages==VK_PIPELINE_STAGE_2_COPY_BIT); 118 EXPECT(host_stages==VK_PIPELINE_STAGE_2_COPY_BIT);
100 } else { 119 } else {
101 EXPECT(typed.execute({&pointer,1})==ReplayResult::success); 120 EXPECT(typed.execute({&pointer,1})==ReplayResult::success);
102 EXPECT(host_stages==(VK_PIPELINE_STAGE_2_COMPUTE_SHADER_BIT|VK_PIPELINE_STAGE_2_COPY_BIT)); 121 EXPECT(host_stages==(VK_PIPELINE_STAGE_2_COMPUTE_SHADER_BIT|VK_PIPELINE_STAGE_2_COPY_BIT|
122 (statistics ? VK_PIPELINE_STAGE_2_TRANSFER_BIT : 0)));
103 } 123 }
104 EXPECT(typed.idle() && host_barriers==1); 124 EXPECT(typed.idle() && host_barriers==1);
105 EXPECT((host_stages & ~VulkanQueue::queue_supported_stages(kind))==0); 125 EXPECT((host_stages & ~VulkanQueue::queue_supported_stages(kind))==0);
106 EXPECT(std::memcmp(dst_data,expected.data(),bytes)==0); 126 EXPECT(std::memcmp(dst_data,expected.data(),bytes)==0);
107 } 127 }
108 context.vk.vkCmdPipelineBarrier2=real_barrier2; 128 context.vk.vkCmdPipelineBarrier2=real_barrier2;
129 if (statistics) {
130 for (unsigned word=0; word<11; ++word) {
131 std::uint64_t value=UINT64_MAX;
132 std::memcpy(&value, static_cast<const char*>(statistics_data)+8*word, 8);
133 EXPECT(value==0);
134 }
135 d.fn<PFN_vkDestroyQueryPool>("vkDestroyQueryPool")(d.device,statistics,nullptr);
136 }
109 EXPECT(d.validation_errors()==0); 137 EXPECT(d.validation_errors()==0);
110 std::puts("predicated copy integration: failed-submit cleanup and direct/compute/copy host stages passed"); 138 std::puts("predicated copy integration: failed-submit cleanup and direct/compute/copy host stages passed");
111 std::puts("predicated copy: 32 gated + 32 reused submissions; byte offsets, forced windows, copy dependency, rebinding/reset and untouched bytes passed"); 139 std::puts("predicated copy: 32 gated + 32 reused submissions; byte offsets, forced windows, copy dependency, rebinding/reset and untouched bytes passed");