vk_scheduler.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. // SPDX-FileCopyrightText: Copyright 2019 yuzu Emulator Project
  2. // SPDX-License-Identifier: GPL-2.0-or-later
  3. #pragma once
  4. #include <condition_variable>
  5. #include <cstddef>
  6. #include <functional>
  7. #include <memory>
  8. #include <thread>
  9. #include <utility>
  10. #include <queue>
  11. #include "common/alignment.h"
  12. #include "common/common_types.h"
  13. #include "common/polyfill_thread.h"
  14. #include "video_core/renderer_vulkan/vk_master_semaphore.h"
  15. #include "video_core/vulkan_common/vulkan_wrapper.h"
  16. namespace Vulkan {
  17. class CommandPool;
  18. class Device;
  19. class Framebuffer;
  20. class GraphicsPipeline;
  21. class StateTracker;
  22. class QueryCache;
  23. /// The scheduler abstracts command buffer and fence management with an interface that's able to do
  24. /// OpenGL-like operations on Vulkan command buffers.
  25. class Scheduler {
  26. public:
  27. explicit Scheduler(const Device& device, StateTracker& state_tracker);
  28. ~Scheduler();
  29. /// Sends the current execution context to the GPU.
  30. void Flush(VkSemaphore signal_semaphore = nullptr, VkSemaphore wait_semaphore = nullptr);
  31. /// Sends the current execution context to the GPU and waits for it to complete.
  32. void Finish(VkSemaphore signal_semaphore = nullptr, VkSemaphore wait_semaphore = nullptr);
  33. /// Waits for the worker thread to finish executing everything. After this function returns it's
  34. /// safe to touch worker resources.
  35. void WaitWorker();
  36. /// Sends currently recorded work to the worker thread.
  37. void DispatchWork();
  38. /// Requests to begin a renderpass.
  39. void RequestRenderpass(const Framebuffer* framebuffer);
  40. /// Requests the current executino context to be able to execute operations only allowed outside
  41. /// of a renderpass.
  42. void RequestOutsideRenderPassOperationContext();
  43. /// Update the pipeline to the current execution context.
  44. bool UpdateGraphicsPipeline(GraphicsPipeline* pipeline);
  45. /// Update the rescaling state. Returns true if the state has to be updated.
  46. bool UpdateRescaling(bool is_rescaling);
  47. /// Invalidates current command buffer state except for render passes
  48. void InvalidateState();
  49. /// Assigns the query cache.
  50. void SetQueryCache(QueryCache& query_cache_) {
  51. query_cache = &query_cache_;
  52. }
  53. // Registers a callback to perform on queue submission.
  54. void RegisterOnSubmit(std::function<void()>&& func) {
  55. on_submit = std::move(func);
  56. }
  57. /// Send work to a separate thread.
  58. template <typename T>
  59. void Record(T&& command) {
  60. if (chunk->Record(command)) {
  61. return;
  62. }
  63. DispatchWork();
  64. (void)chunk->Record(command);
  65. }
  66. /// Returns the current command buffer tick.
  67. [[nodiscard]] u64 CurrentTick() const noexcept {
  68. return master_semaphore->CurrentTick();
  69. }
  70. /// Returns true when a tick has been triggered by the GPU.
  71. [[nodiscard]] bool IsFree(u64 tick) const noexcept {
  72. return master_semaphore->IsFree(tick);
  73. }
  74. /// Waits for the given tick to trigger on the GPU.
  75. void Wait(u64 tick) {
  76. if (tick >= master_semaphore->CurrentTick()) {
  77. // Make sure we are not waiting for the current tick without signalling
  78. Flush();
  79. }
  80. master_semaphore->Wait(tick);
  81. }
  82. /// Returns the master timeline semaphore.
  83. [[nodiscard]] MasterSemaphore& GetMasterSemaphore() const noexcept {
  84. return *master_semaphore;
  85. }
  86. private:
  87. class Command {
  88. public:
  89. virtual ~Command() = default;
  90. virtual void Execute(vk::CommandBuffer cmdbuf) const = 0;
  91. Command* GetNext() const {
  92. return next;
  93. }
  94. void SetNext(Command* next_) {
  95. next = next_;
  96. }
  97. private:
  98. Command* next = nullptr;
  99. };
  100. template <typename T>
  101. class TypedCommand final : public Command {
  102. public:
  103. explicit TypedCommand(T&& command_) : command{std::move(command_)} {}
  104. ~TypedCommand() override = default;
  105. TypedCommand(TypedCommand&&) = delete;
  106. TypedCommand& operator=(TypedCommand&&) = delete;
  107. void Execute(vk::CommandBuffer cmdbuf) const override {
  108. command(cmdbuf);
  109. }
  110. private:
  111. T command;
  112. };
  113. class CommandChunk final {
  114. public:
  115. void ExecuteAll(vk::CommandBuffer cmdbuf);
  116. template <typename T>
  117. bool Record(T& command) {
  118. using FuncType = TypedCommand<T>;
  119. static_assert(sizeof(FuncType) < sizeof(data), "Lambda is too large");
  120. command_offset = Common::AlignUp(command_offset, alignof(FuncType));
  121. if (command_offset > sizeof(data) - sizeof(FuncType)) {
  122. return false;
  123. }
  124. Command* const current_last = last;
  125. last = new (data.data() + command_offset) FuncType(std::move(command));
  126. if (current_last) {
  127. current_last->SetNext(last);
  128. } else {
  129. first = last;
  130. }
  131. command_offset += sizeof(FuncType);
  132. return true;
  133. }
  134. void MarkSubmit() {
  135. submit = true;
  136. }
  137. bool Empty() const {
  138. return command_offset == 0;
  139. }
  140. bool HasSubmit() const {
  141. return submit;
  142. }
  143. private:
  144. Command* first = nullptr;
  145. Command* last = nullptr;
  146. size_t command_offset = 0;
  147. bool submit = false;
  148. alignas(std::max_align_t) std::array<u8, 0x8000> data{};
  149. };
  150. struct State {
  151. VkRenderPass renderpass = nullptr;
  152. VkFramebuffer framebuffer = nullptr;
  153. VkExtent2D render_area = {0, 0};
  154. GraphicsPipeline* graphics_pipeline = nullptr;
  155. bool is_rescaling = false;
  156. bool rescaling_defined = false;
  157. };
  158. void WorkerThread(std::stop_token stop_token);
  159. void AllocateWorkerCommandBuffer();
  160. void SubmitExecution(VkSemaphore signal_semaphore, VkSemaphore wait_semaphore);
  161. void AllocateNewContext();
  162. void EndPendingOperations();
  163. void EndRenderPass();
  164. void AcquireNewChunk();
  165. const Device& device;
  166. StateTracker& state_tracker;
  167. std::unique_ptr<MasterSemaphore> master_semaphore;
  168. std::unique_ptr<CommandPool> command_pool;
  169. QueryCache* query_cache = nullptr;
  170. vk::CommandBuffer current_cmdbuf;
  171. std::unique_ptr<CommandChunk> chunk;
  172. std::function<void()> on_submit;
  173. State state;
  174. u32 num_renderpass_images = 0;
  175. std::array<VkImage, 9> renderpass_images{};
  176. std::array<VkImageSubresourceRange, 9> renderpass_image_ranges{};
  177. std::queue<std::unique_ptr<CommandChunk>> work_queue;
  178. std::vector<std::unique_ptr<CommandChunk>> chunk_reserve;
  179. std::mutex execution_mutex;
  180. std::mutex reserve_mutex;
  181. std::mutex queue_mutex;
  182. std::condition_variable_any event_cv;
  183. std::jthread worker_thread;
  184. };
  185. } // namespace Vulkan