vk_scheduler.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 "common/common_types.h"
  6. #include "video_core/renderer_vulkan/declarations.h"
  7. namespace Vulkan {
  8. class VKDevice;
  9. class VKFence;
  10. class VKResourceManager;
  11. class VKFenceView {
  12. public:
  13. VKFenceView() = default;
  14. VKFenceView(VKFence* const& fence) : fence{fence} {}
  15. VKFence* operator->() const noexcept {
  16. return fence;
  17. }
  18. operator VKFence&() const noexcept {
  19. return *fence;
  20. }
  21. private:
  22. VKFence* const& fence;
  23. };
  24. class VKCommandBufferView {
  25. public:
  26. VKCommandBufferView() = default;
  27. VKCommandBufferView(const vk::CommandBuffer& cmdbuf) : cmdbuf{cmdbuf} {}
  28. const vk::CommandBuffer* operator->() const noexcept {
  29. return &cmdbuf;
  30. }
  31. operator vk::CommandBuffer() const noexcept {
  32. return cmdbuf;
  33. }
  34. private:
  35. const vk::CommandBuffer& cmdbuf;
  36. };
  37. /// The scheduler abstracts command buffer and fence management with an interface that's able to do
  38. /// OpenGL-like operations on Vulkan command buffers.
  39. class VKScheduler {
  40. public:
  41. explicit VKScheduler(const VKDevice& device, VKResourceManager& resource_manager);
  42. ~VKScheduler();
  43. /// Gets a reference to the current fence.
  44. VKFenceView GetFence() const {
  45. return current_fence;
  46. }
  47. /// Gets a reference to the current command buffer.
  48. VKCommandBufferView GetCommandBuffer() const {
  49. return current_cmdbuf;
  50. }
  51. /// Sends the current execution context to the GPU.
  52. void Flush(bool release_fence = true, vk::Semaphore semaphore = nullptr);
  53. /// Sends the current execution context to the GPU and waits for it to complete.
  54. void Finish(bool release_fence = true, vk::Semaphore semaphore = nullptr);
  55. private:
  56. void SubmitExecution(vk::Semaphore semaphore);
  57. void AllocateNewContext();
  58. const VKDevice& device;
  59. VKResourceManager& resource_manager;
  60. vk::CommandBuffer current_cmdbuf;
  61. VKFence* current_fence = nullptr;
  62. VKFence* next_fence = nullptr;
  63. };
  64. } // namespace Vulkan