vk_scheduler.h 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. // Copyright 2019 yuzu Emulator Project
  2. // Licensed under GPLv2 or any later version
  3. // Refer to the license.txt file included.
  4. #pragma once
  5. #include <atomic>
  6. #include <condition_variable>
  7. #include <cstddef>
  8. #include <memory>
  9. #include <thread>
  10. #include <utility>
  11. #include <queue>
  12. #include "common/alignment.h"
  13. #include "common/common_types.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 VKQueryCache;
  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 VKScheduler {
  26. public:
  27. explicit VKScheduler(const Device& device, StateTracker& state_tracker);
  28. ~VKScheduler();
  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. /// Invalidates current command buffer state except for render passes
  46. void InvalidateState();
  47. /// Assigns the query cache.
  48. void SetQueryCache(VKQueryCache& query_cache_) {
  49. query_cache = &query_cache_;
  50. }
  51. /// Send work to a separate thread.
  52. template <typename T>
  53. void Record(T&& command) {
  54. if (chunk->Record(command)) {
  55. return;
  56. }
  57. DispatchWork();
  58. (void)chunk->Record(command);
  59. }
  60. /// Returns the current command buffer tick.
  61. [[nodiscard]] u64 CurrentTick() const noexcept {
  62. return master_semaphore->CurrentTick();
  63. }
  64. /// Returns true when a tick has been triggered by the GPU.
  65. [[nodiscard]] bool IsFree(u64 tick) const noexcept {
  66. return master_semaphore->IsFree(tick);
  67. }
  68. /// Waits for the given tick to trigger on the GPU.
  69. void Wait(u64 tick) {
  70. if (tick >= master_semaphore->CurrentTick()) {
  71. // Make sure we are not waiting for the current tick without signalling
  72. Flush();
  73. }
  74. master_semaphore->Wait(tick);
  75. }
  76. /// Returns the master timeline semaphore.
  77. [[nodiscard]] MasterSemaphore& GetMasterSemaphore() const noexcept {
  78. return *master_semaphore;
  79. }
  80. private:
  81. class Command {
  82. public:
  83. virtual ~Command() = default;
  84. virtual void Execute(vk::CommandBuffer cmdbuf) const = 0;
  85. Command* GetNext() const {
  86. return next;
  87. }
  88. void SetNext(Command* next_) {
  89. next = next_;
  90. }
  91. private:
  92. Command* next = nullptr;
  93. };
  94. template <typename T>
  95. class TypedCommand final : public Command {
  96. public:
  97. explicit TypedCommand(T&& command_) : command{std::move(command_)} {}
  98. ~TypedCommand() override = default;
  99. TypedCommand(TypedCommand&&) = delete;
  100. TypedCommand& operator=(TypedCommand&&) = delete;
  101. void Execute(vk::CommandBuffer cmdbuf) const override {
  102. command(cmdbuf);
  103. }
  104. private:
  105. T command;
  106. };
  107. class CommandChunk final {
  108. public:
  109. void ExecuteAll(vk::CommandBuffer cmdbuf);
  110. template <typename T>
  111. bool Record(T& command) {
  112. using FuncType = TypedCommand<T>;
  113. static_assert(sizeof(FuncType) < sizeof(data), "Lambda is too large");
  114. command_offset = Common::AlignUp(command_offset, alignof(FuncType));
  115. if (command_offset > sizeof(data) - sizeof(FuncType)) {
  116. return false;
  117. }
  118. Command* const current_last = last;
  119. last = new (data.data() + command_offset) FuncType(std::move(command));
  120. if (current_last) {
  121. current_last->SetNext(last);
  122. } else {
  123. first = last;
  124. }
  125. command_offset += sizeof(FuncType);
  126. return true;
  127. }
  128. void MarkSubmit() {
  129. submit = true;
  130. }
  131. bool Empty() const {
  132. return command_offset == 0;
  133. }
  134. bool HasSubmit() const {
  135. return submit;
  136. }
  137. private:
  138. Command* first = nullptr;
  139. Command* last = nullptr;
  140. size_t command_offset = 0;
  141. bool submit = false;
  142. alignas(std::max_align_t) std::array<u8, 0x8000> data{};
  143. };
  144. struct State {
  145. VkRenderPass renderpass = nullptr;
  146. VkFramebuffer framebuffer = nullptr;
  147. VkExtent2D render_area = {0, 0};
  148. GraphicsPipeline* graphics_pipeline = nullptr;
  149. };
  150. void WorkerThread();
  151. void AllocateWorkerCommandBuffer();
  152. void SubmitExecution(VkSemaphore signal_semaphore, VkSemaphore wait_semaphore);
  153. void AllocateNewContext();
  154. void EndPendingOperations();
  155. void EndRenderPass();
  156. void AcquireNewChunk();
  157. const Device& device;
  158. StateTracker& state_tracker;
  159. std::unique_ptr<MasterSemaphore> master_semaphore;
  160. std::unique_ptr<CommandPool> command_pool;
  161. VKQueryCache* query_cache = nullptr;
  162. vk::CommandBuffer current_cmdbuf;
  163. std::unique_ptr<CommandChunk> chunk;
  164. std::thread worker_thread;
  165. State state;
  166. u32 num_renderpass_images = 0;
  167. std::array<VkImage, 9> renderpass_images{};
  168. std::array<VkImageSubresourceRange, 9> renderpass_image_ranges{};
  169. std::queue<std::unique_ptr<CommandChunk>> work_queue;
  170. std::vector<std::unique_ptr<CommandChunk>> chunk_reserve;
  171. std::mutex reserve_mutex;
  172. std::mutex work_mutex;
  173. std::condition_variable work_cv;
  174. std::condition_variable wait_cv;
  175. std::atomic_bool quit{};
  176. };
  177. } // namespace Vulkan