vk_scheduler.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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 <stack>
  10. #include <thread>
  11. #include <utility>
  12. #include "common/alignment.h"
  13. #include "common/common_types.h"
  14. #include "common/threadsafe_queue.h"
  15. #include "video_core/vulkan_common/vulkan_wrapper.h"
  16. namespace Vulkan {
  17. class CommandPool;
  18. class Device;
  19. class Framebuffer;
  20. class MasterSemaphore;
  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. /// Returns the current command buffer tick.
  30. [[nodiscard]] u64 CurrentTick() const noexcept;
  31. /// Returns true when a tick has been triggered by the GPU.
  32. [[nodiscard]] bool IsFree(u64 tick) const noexcept;
  33. /// Waits for the given tick to trigger on the GPU.
  34. void Wait(u64 tick);
  35. /// Sends the current execution context to the GPU.
  36. void Flush(VkSemaphore semaphore = nullptr);
  37. /// Sends the current execution context to the GPU and waits for it to complete.
  38. void Finish(VkSemaphore semaphore = nullptr);
  39. /// Waits for the worker thread to finish executing everything. After this function returns it's
  40. /// safe to touch worker resources.
  41. void WaitWorker();
  42. /// Sends currently recorded work to the worker thread.
  43. void DispatchWork();
  44. /// Requests to begin a renderpass.
  45. void RequestRenderpass(const Framebuffer* framebuffer);
  46. /// Requests the current executino context to be able to execute operations only allowed outside
  47. /// of a renderpass.
  48. void RequestOutsideRenderPassOperationContext();
  49. /// Binds a pipeline to the current execution context.
  50. void BindGraphicsPipeline(VkPipeline pipeline);
  51. /// Invalidates current command buffer state except for render passes
  52. void InvalidateState();
  53. /// Assigns the query cache.
  54. void SetQueryCache(VKQueryCache& query_cache_) {
  55. query_cache = &query_cache_;
  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 master timeline semaphore.
  67. [[nodiscard]] MasterSemaphore& GetMasterSemaphore() const noexcept {
  68. return *master_semaphore;
  69. }
  70. private:
  71. class Command {
  72. public:
  73. virtual ~Command() = default;
  74. virtual void Execute(vk::CommandBuffer cmdbuf) const = 0;
  75. Command* GetNext() const {
  76. return next;
  77. }
  78. void SetNext(Command* next_) {
  79. next = next_;
  80. }
  81. private:
  82. Command* next = nullptr;
  83. };
  84. template <typename T>
  85. class TypedCommand final : public Command {
  86. public:
  87. explicit TypedCommand(T&& command_) : command{std::move(command_)} {}
  88. ~TypedCommand() override = default;
  89. TypedCommand(TypedCommand&&) = delete;
  90. TypedCommand& operator=(TypedCommand&&) = delete;
  91. void Execute(vk::CommandBuffer cmdbuf) const override {
  92. command(cmdbuf);
  93. }
  94. private:
  95. T command;
  96. };
  97. class CommandChunk final {
  98. public:
  99. void ExecuteAll(vk::CommandBuffer cmdbuf);
  100. template <typename T>
  101. bool Record(T& command) {
  102. using FuncType = TypedCommand<T>;
  103. static_assert(sizeof(FuncType) < sizeof(data), "Lambda is too large");
  104. command_offset = Common::AlignUp(command_offset, alignof(FuncType));
  105. if (command_offset > sizeof(data) - sizeof(FuncType)) {
  106. return false;
  107. }
  108. Command* const current_last = last;
  109. last = new (data.data() + command_offset) FuncType(std::move(command));
  110. if (current_last) {
  111. current_last->SetNext(last);
  112. } else {
  113. first = last;
  114. }
  115. command_offset += sizeof(FuncType);
  116. return true;
  117. }
  118. bool Empty() const {
  119. return command_offset == 0;
  120. }
  121. private:
  122. Command* first = nullptr;
  123. Command* last = nullptr;
  124. size_t command_offset = 0;
  125. alignas(std::max_align_t) std::array<u8, 0x8000> data{};
  126. };
  127. struct State {
  128. VkRenderPass renderpass = nullptr;
  129. VkFramebuffer framebuffer = nullptr;
  130. VkExtent2D render_area = {0, 0};
  131. VkPipeline graphics_pipeline = nullptr;
  132. };
  133. void WorkerThread();
  134. void SubmitExecution(VkSemaphore semaphore);
  135. void AllocateNewContext();
  136. void EndPendingOperations();
  137. void EndRenderPass();
  138. void AcquireNewChunk();
  139. const Device& device;
  140. StateTracker& state_tracker;
  141. std::unique_ptr<MasterSemaphore> master_semaphore;
  142. std::unique_ptr<CommandPool> command_pool;
  143. VKQueryCache* query_cache = nullptr;
  144. vk::CommandBuffer current_cmdbuf;
  145. std::unique_ptr<CommandChunk> chunk;
  146. std::thread worker_thread;
  147. State state;
  148. u32 num_renderpass_images = 0;
  149. std::array<VkImage, 9> renderpass_images{};
  150. std::array<VkImageSubresourceRange, 9> renderpass_image_ranges{};
  151. Common::SPSCQueue<std::unique_ptr<CommandChunk>> chunk_queue;
  152. Common::SPSCQueue<std::unique_ptr<CommandChunk>> chunk_reserve;
  153. std::mutex mutex;
  154. std::condition_variable cv;
  155. bool quit = false;
  156. };
  157. } // namespace Vulkan