diff options
Diffstat (limited to 'runtime/src')
| -rw-r--r-- | runtime/src/vulkan_backend.h | 2 | ||||
| -rw-r--r-- | runtime/src/vulkan_replay.h | 77 |
2 files changed, 55 insertions, 24 deletions
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 | ||
| 853 | struct Prepared { | 853 | struct 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. | ||
| 1911 | inline void plan_statistics_segments(PreparedList& out) { | 1911 | inline 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. | ||
| 1992 | template<typename Resolve> | 2002 | template<typename Resolve> |
| 1993 | ReplayResult prepare(const CommandList& list, Resolve&& resolve, const ReplayFunctions& vk, PreparedList& out) { | 2003 | ReplayResult 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 | |||
| 2431 | class QueryReplay { | 2440 | class QueryReplay { |
| 2432 | public: | 2441 | public: |
| 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); |