OLD | NEW |
(Empty) | |
| 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 |
| 5 #include "gpu/vulkan/vulkan_command_buffer.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "gpu/vulkan/vulkan_command_pool.h" |
| 9 #include "gpu/vulkan/vulkan_implementation.h" |
| 10 |
| 11 namespace gpu { |
| 12 |
| 13 VulkanCommandBuffer::VulkanCommandBuffer(VulkanCommandPool* command_pool, |
| 14 bool primary) |
| 15 : primary_(primary), command_pool_(command_pool) { |
| 16 command_pool_->IncrementCommandBufferCount(); |
| 17 } |
| 18 |
| 19 VulkanCommandBuffer::~VulkanCommandBuffer() { |
| 20 DCHECK_EQ(static_cast<VkCommandBuffer>(VK_NULL_HANDLE), command_buffer_); |
| 21 DCHECK_EQ(static_cast<VkFence>(VK_NULL_HANDLE), submission_fence_); |
| 22 DCHECK(!recording_); |
| 23 command_pool_->DecrementCommandBufferCount(); |
| 24 } |
| 25 |
| 26 bool VulkanCommandBuffer::Initialize() { |
| 27 VkResult result = VK_SUCCESS; |
| 28 VkDevice device = GetVulkanDevice(); |
| 29 |
| 30 VkCommandBufferAllocateInfo command_buffer_info = {}; |
| 31 command_buffer_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; |
| 32 command_buffer_info.commandPool = command_pool_->handle(); |
| 33 command_buffer_info.level = primary_ ? VK_COMMAND_BUFFER_LEVEL_PRIMARY |
| 34 : VK_COMMAND_BUFFER_LEVEL_SECONDARY; |
| 35 command_buffer_info.commandBufferCount = 1; |
| 36 |
| 37 result = |
| 38 vkAllocateCommandBuffers(device, &command_buffer_info, &command_buffer_); |
| 39 if (VK_SUCCESS != result) { |
| 40 DLOG(ERROR) << "vkAllocateCommandBuffers() failed: " << result; |
| 41 return false; |
| 42 } |
| 43 |
| 44 VkFenceCreateInfo fence_create_info = {}; |
| 45 fence_create_info.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO; |
| 46 fence_create_info.flags = VK_FENCE_CREATE_SIGNALED_BIT; |
| 47 |
| 48 result = |
| 49 vkCreateFence(device, &fence_create_info, nullptr, &submission_fence_); |
| 50 if (VK_SUCCESS != result) { |
| 51 DLOG(ERROR) << "vkCreateFence(submission) failed: " << result; |
| 52 return false; |
| 53 } |
| 54 |
| 55 record_type_ = RECORD_TYPE_EMPTY; |
| 56 return true; |
| 57 } |
| 58 |
| 59 void VulkanCommandBuffer::Destroy() { |
| 60 VkDevice device = GetVulkanDevice(); |
| 61 if (VK_NULL_HANDLE != submission_fence_) { |
| 62 DCHECK(SubmissionFinished()); |
| 63 vkDestroyFence(device, submission_fence_, nullptr); |
| 64 submission_fence_ = VK_NULL_HANDLE; |
| 65 } |
| 66 |
| 67 if (VK_NULL_HANDLE != command_buffer_) { |
| 68 vkFreeCommandBuffers(device, command_pool_->handle(), 1, &command_buffer_); |
| 69 command_buffer_ = VK_NULL_HANDLE; |
| 70 } |
| 71 } |
| 72 |
| 73 bool VulkanCommandBuffer::Submit(VkQueue queue, |
| 74 uint32_t num_wait_semaphores, |
| 75 VkSemaphore* wait_semaphores, |
| 76 uint32_t num_signal_semaphores, |
| 77 VkSemaphore* signal_semaphores) { |
| 78 DCHECK(primary_); |
| 79 VkSubmitInfo submit_info = {}; |
| 80 submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; |
| 81 submit_info.commandBufferCount = 1; |
| 82 submit_info.pCommandBuffers = &command_buffer_; |
| 83 submit_info.waitSemaphoreCount = num_wait_semaphores; |
| 84 submit_info.pWaitSemaphores = wait_semaphores; |
| 85 submit_info.signalSemaphoreCount = num_signal_semaphores; |
| 86 submit_info.pSignalSemaphores = signal_semaphores; |
| 87 |
| 88 vkResetFences(GetVulkanDevice(), 1, &submission_fence_); |
| 89 VkResult result = vkQueueSubmit(queue, 1, &submit_info, submission_fence_); |
| 90 PostExecution(); |
| 91 if (VK_SUCCESS != result) { |
| 92 DLOG(ERROR) << "vkQueueSubmit() failed: " << result; |
| 93 return false; |
| 94 } |
| 95 |
| 96 return true; |
| 97 } |
| 98 |
| 99 void VulkanCommandBuffer::Enqueue(VkCommandBuffer primary_command_buffer) { |
| 100 DCHECK(!primary_); |
| 101 vkCmdExecuteCommands(primary_command_buffer, 1, &command_buffer_); |
| 102 PostExecution(); |
| 103 } |
| 104 |
| 105 void VulkanCommandBuffer::Clear() { |
| 106 // Mark to reset upon next use. |
| 107 if (record_type_ != RECORD_TYPE_EMPTY) |
| 108 record_type_ = RECORD_TYPE_DIRTY; |
| 109 } |
| 110 |
| 111 void VulkanCommandBuffer::Wait(uint64_t timeout) { |
| 112 vkWaitForFences(GetVulkanDevice(), 1, &submission_fence_, true, timeout); |
| 113 } |
| 114 |
| 115 bool VulkanCommandBuffer::SubmissionFinished() { |
| 116 return VK_SUCCESS == vkGetFenceStatus(GetVulkanDevice(), submission_fence_); |
| 117 } |
| 118 |
| 119 void VulkanCommandBuffer::PostExecution() { |
| 120 if (record_type_ == RECORD_TYPE_SINGLE_USE) { |
| 121 // Clear upon next use. |
| 122 record_type_ = RECORD_TYPE_DIRTY; |
| 123 } else if (record_type_ == RECORD_TYPE_MULTI_USE) { |
| 124 // Can no longer record new items unless marked as clear. |
| 125 record_type_ = RECORD_TYPE_RECORDED; |
| 126 } |
| 127 } |
| 128 |
| 129 void VulkanCommandBuffer::ResetIfDirty() { |
| 130 DCHECK(!recording_); |
| 131 if (record_type_ == RECORD_TYPE_DIRTY) { |
| 132 // Block if command buffer is still in use. This can be externally avoided |
| 133 // using the asynchronous SubmissionFinished() function. |
| 134 vkWaitForFences(GetVulkanDevice(), 1, &submission_fence_, true, UINT64_MAX); |
| 135 |
| 136 vkResetCommandBuffer(command_buffer_, 0); |
| 137 record_type_ = RECORD_TYPE_EMPTY; |
| 138 } |
| 139 } |
| 140 |
| 141 CommandBufferRecorderBase::~CommandBufferRecorderBase() { |
| 142 vkEndCommandBuffer(handle_); |
| 143 }; |
| 144 |
| 145 ScopedMultiUseCommandBufferRecorder::ScopedMultiUseCommandBufferRecorder( |
| 146 VulkanCommandBuffer& command_buffer) |
| 147 : CommandBufferRecorderBase(command_buffer) { |
| 148 ValidateMultiUse(command_buffer); |
| 149 VkCommandBufferBeginInfo begin_info = {}; |
| 150 begin_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; |
| 151 vkBeginCommandBuffer(handle_, &begin_info); |
| 152 } |
| 153 |
| 154 ScopedSingleUseCommandBufferRecorder::ScopedSingleUseCommandBufferRecorder( |
| 155 VulkanCommandBuffer& command_buffer) |
| 156 : CommandBufferRecorderBase(command_buffer) { |
| 157 ValidateSingleUse(command_buffer); |
| 158 VkCommandBufferBeginInfo begin_info = {}; |
| 159 begin_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; |
| 160 begin_info.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT; |
| 161 vkBeginCommandBuffer(handle_, &begin_info); |
| 162 } |
| 163 |
| 164 } // namespace gpu |
OLD | NEW |