vulkan_command_buffer.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // Copyright (c) 2016 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef GPU_VULKAN_VULKAN_COMMAND_BUFFER_H_
  5. #define GPU_VULKAN_VULKAN_COMMAND_BUFFER_H_
  6. #include <vulkan/vulkan_core.h>
  7. #include "base/check.h"
  8. #include "base/component_export.h"
  9. #include "base/memory/raw_ptr.h"
  10. #include "gpu/vulkan/vulkan_fence_helper.h"
  11. namespace gpu {
  12. class VulkanCommandPool;
  13. class VulkanDeviceQueue;
  14. class COMPONENT_EXPORT(VULKAN) VulkanCommandBuffer {
  15. public:
  16. VulkanCommandBuffer(VulkanDeviceQueue* device_queue,
  17. VulkanCommandPool* command_pool,
  18. bool primary);
  19. VulkanCommandBuffer(const VulkanCommandBuffer&) = delete;
  20. VulkanCommandBuffer& operator=(const VulkanCommandBuffer&) = delete;
  21. ~VulkanCommandBuffer();
  22. bool Initialize();
  23. // Destroy() should be called when all related GPU tasks have been finished.
  24. void Destroy();
  25. // Submit primary command buffer to the queue.
  26. bool Submit(uint32_t num_wait_semaphores,
  27. VkSemaphore* wait_semaphores,
  28. uint32_t num_signal_semaphores,
  29. VkSemaphore* signal_semaphores);
  30. // Enqueue secondary command buffer within a primary command buffer.
  31. void Enqueue(VkCommandBuffer primary_command_buffer);
  32. void Clear();
  33. // This blocks until the commands from the previous submit are done.
  34. void Wait(uint64_t timeout);
  35. // This simply tests asynchronously if the commands from the previous submit
  36. // is finished.
  37. bool SubmissionFinished();
  38. void TransitionImageLayout(
  39. VkImage image,
  40. VkImageLayout old_layout,
  41. VkImageLayout new_layout,
  42. uint32_t src_queue_family_index = VK_QUEUE_FAMILY_IGNORED,
  43. uint32_t dst_queue_family_index = VK_QUEUE_FAMILY_IGNORED);
  44. void CopyBufferToImage(VkBuffer buffer,
  45. VkImage image,
  46. uint32_t buffer_width,
  47. uint32_t buffer_height,
  48. uint32_t width,
  49. uint32_t height);
  50. void CopyImageToBuffer(VkBuffer buffer,
  51. VkImage image,
  52. uint32_t buffer_width,
  53. uint32_t buffer_height,
  54. uint32_t width,
  55. uint32_t height);
  56. private:
  57. friend class CommandBufferRecorderBase;
  58. enum RecordType {
  59. // Nothing has been recorded yet.
  60. RECORD_TYPE_EMPTY,
  61. // Recorded for single use, will be reset upon submission.
  62. RECORD_TYPE_SINGLE_USE,
  63. // Recording for multi use, once submitted it can't be modified until reset.
  64. RECORD_TYPE_MULTI_USE,
  65. // Recorded for multi-use, can no longer be modified unless reset.
  66. RECORD_TYPE_RECORDED,
  67. // Dirty, should be cleared before use. This assumes its externally
  68. // synchronized and the command buffer is no longer in use.
  69. RECORD_TYPE_DIRTY,
  70. };
  71. void PostExecution();
  72. void ResetIfDirty();
  73. const bool primary_;
  74. bool recording_ = false;
  75. RecordType record_type_ = RECORD_TYPE_EMPTY;
  76. raw_ptr<VulkanDeviceQueue> device_queue_;
  77. raw_ptr<VulkanCommandPool> command_pool_;
  78. VkCommandBuffer command_buffer_ = VK_NULL_HANDLE;
  79. VulkanFenceHelper::FenceHandle submission_fence_;
  80. };
  81. class COMPONENT_EXPORT(VULKAN) CommandBufferRecorderBase {
  82. public:
  83. VkCommandBuffer handle() const { return handle_; }
  84. protected:
  85. CommandBufferRecorderBase(VulkanCommandBuffer& command_buffer)
  86. : handle_(command_buffer.command_buffer_) {
  87. command_buffer.ResetIfDirty();
  88. }
  89. virtual ~CommandBufferRecorderBase();
  90. void ValidateSingleUse(VulkanCommandBuffer& command_buffer) {
  91. DCHECK((VulkanCommandBuffer::RECORD_TYPE_SINGLE_USE ==
  92. command_buffer.record_type_) ||
  93. (VulkanCommandBuffer::RECORD_TYPE_EMPTY ==
  94. command_buffer.record_type_));
  95. command_buffer.record_type_ = VulkanCommandBuffer::RECORD_TYPE_SINGLE_USE;
  96. }
  97. void ValidateMultiUse(VulkanCommandBuffer& command_buffer) {
  98. DCHECK((VulkanCommandBuffer::RECORD_TYPE_MULTI_USE ==
  99. command_buffer.record_type_) ||
  100. (VulkanCommandBuffer::RECORD_TYPE_EMPTY ==
  101. command_buffer.record_type_));
  102. command_buffer.record_type_ = VulkanCommandBuffer::RECORD_TYPE_MULTI_USE;
  103. }
  104. VkCommandBuffer handle_;
  105. };
  106. class COMPONENT_EXPORT(VULKAN) ScopedMultiUseCommandBufferRecorder
  107. : public CommandBufferRecorderBase {
  108. public:
  109. ScopedMultiUseCommandBufferRecorder(VulkanCommandBuffer& command_buffer);
  110. ScopedMultiUseCommandBufferRecorder(
  111. const ScopedMultiUseCommandBufferRecorder&) = delete;
  112. ScopedMultiUseCommandBufferRecorder& operator=(
  113. const ScopedMultiUseCommandBufferRecorder&) = delete;
  114. ~ScopedMultiUseCommandBufferRecorder() override {}
  115. };
  116. class COMPONENT_EXPORT(VULKAN) ScopedSingleUseCommandBufferRecorder
  117. : public CommandBufferRecorderBase {
  118. public:
  119. ScopedSingleUseCommandBufferRecorder(VulkanCommandBuffer& command_buffer);
  120. ScopedSingleUseCommandBufferRecorder(
  121. const ScopedSingleUseCommandBufferRecorder&) = delete;
  122. ScopedSingleUseCommandBufferRecorder& operator=(
  123. const ScopedSingleUseCommandBufferRecorder&) = delete;
  124. ~ScopedSingleUseCommandBufferRecorder() override {}
  125. };
  126. } // namespace gpu
  127. #endif // GPU_VULKAN_VULKAN_COMMAND_BUFFER_H_