Chromium Code Reviews| Index: gpu/vulkan/vulkan_command_buffer.cc |
| diff --git a/gpu/vulkan/vulkan_command_buffer.cc b/gpu/vulkan/vulkan_command_buffer.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..d56ad33ce154e4f82c73dc8cbeba90326750ed26 |
| --- /dev/null |
| +++ b/gpu/vulkan/vulkan_command_buffer.cc |
| @@ -0,0 +1,122 @@ |
| +// Copyright (c) 2016 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "gpu/vulkan/vulkan_command_buffer.h" |
| + |
| +#include "gpu/vulkan/vulkan_command_pool.h" |
| +#include "gpu/vulkan/vulkan_implementation.h" |
| + |
| +namespace gpu { |
| + |
| +VulkanCommandBuffer::VulkanCommandBuffer(VulkanCommandPool* command_pool, |
| + bool primary) |
| + : primary_(primary), command_pool_(command_pool) { |
| + command_pool_->IncrementCommandBufferCount(); |
| +} |
| + |
| +VulkanCommandBuffer::~VulkanCommandBuffer() { |
| + DCHECK_EQ(static_cast<VkCommandBuffer>(VK_NULL_HANDLE), command_buffer_); |
| + DCHECK(!recording_); |
| + command_pool_->DecrementCommandBufferCount(); |
| +} |
| + |
| +bool VulkanCommandBuffer::Initialize() { |
| + VkCommandBufferAllocateInfo command_buffer_info = {}; |
| + command_buffer_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO; |
| + command_buffer_info.commandPool = command_pool_->handle(); |
| + command_buffer_info.level = primary_ ? VK_COMMAND_BUFFER_LEVEL_PRIMARY |
| + : VK_COMMAND_BUFFER_LEVEL_SECONDARY; |
| + command_buffer_info.commandBufferCount = 1; |
| + |
| + VkResult result = vkAllocateCommandBuffers( |
| + GetVulkanDevice(), &command_buffer_info, &command_buffer_); |
| + if (VK_SUCCESS != result) { |
| + DLOG(ERROR) << "vkAllocateCommandBuffers() failed: " << result; |
| + return false; |
| + } |
| + return true; |
| +} |
| + |
| +void VulkanCommandBuffer::Destroy() { |
| + if (VK_NULL_HANDLE != command_buffer_) { |
| + vkFreeCommandBuffers(GetVulkanDevice(), command_pool_->handle(), 1, |
| + &command_buffer_); |
| + command_buffer_ = VK_NULL_HANDLE; |
| + } |
| +} |
| + |
| +bool VulkanCommandBuffer::Submit(VkQueue queue, |
| + uint32_t num_wait_semaphores, |
| + VkSemaphore* wait_semaphores, |
| + uint32_t num_signal_semaphores, |
| + VkSemaphore* signal_semaphores) { |
| + DCHECK(primary_); |
| + VkSubmitInfo submit_info = {}; |
| + submit_info.sType = VK_STRUCTURE_TYPE_SUBMIT_INFO; |
| + submit_info.commandBufferCount = 1; |
| + submit_info.pCommandBuffers = &command_buffer_; |
| + submit_info.waitSemaphoreCount = num_wait_semaphores; |
| + submit_info.pWaitSemaphores = wait_semaphores; |
| + submit_info.signalSemaphoreCount = num_signal_semaphores; |
| + submit_info.pSignalSemaphores = signal_semaphores; |
| + |
| + VkResult result = vkQueueSubmit(queue, 1, &submit_info, VK_NULL_HANDLE); |
| + PostExecution(); |
| + if (VK_SUCCESS != result) { |
| + DLOG(ERROR) << "vkQueueSubmit() failed: " << result; |
| + return false; |
| + } |
| + |
| + return true; |
| +} |
| + |
| +void VulkanCommandBuffer::Enqueue(VkCommandBuffer primary_command_buffer) { |
| + DCHECK(!primary_); |
| + vkCmdExecuteCommands(primary_command_buffer, 1, &command_buffer_); |
| + PostExecution(); |
| +} |
| + |
| +void VulkanCommandBuffer::PostExecution() { |
| + if (record_type_ == RECORD_TYPE_SINGLE_USE) { |
| + // Clear upon next use. |
| + record_type_ = RECORD_TYPE_DIRTY; |
| + } else if (record_type_ == RECORD_TYPE_MULTI_USE) { |
| + // Can no longer record new items unless marked as clear. |
| + record_type_ = RECORD_TYPE_RECORDED; |
| + } |
| +} |
| + |
| +void VulkanCommandBuffer::ResetIfDirty() { |
| + DCHECK(!recording_); |
| + if (record_type_ == RECORD_TYPE_DIRTY) { |
| + vkResetCommandPool(GetVulkanDevice(), command_pool_->handle(), |
|
jchen10
2016/03/23 07:12:57
Would it be impertinent to ask whether vkResetComm
David Yen
2016/03/23 18:19:29
Good catch, not sure what happened here. Fixed.
|
| + VK_COMMAND_POOL_RESET_RELEASE_RESOURCES_BIT); |
| + record_type_ = RECORD_TYPE_EMPTY; |
| + } |
| +} |
| + |
| +CommandBufferRecorderBase::~CommandBufferRecorderBase() { |
| + vkEndCommandBuffer(handle_); |
| +}; |
| + |
| +ScopedMultiUseCommandBufferRecorder::ScopedMultiUseCommandBufferRecorder( |
| + VulkanCommandBuffer& command_buffer) |
| + : CommandBufferRecorderBase(command_buffer) { |
| + ValidateMultiUse(command_buffer); |
| + VkCommandBufferBeginInfo begin_info = {}; |
| + begin_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; |
| + vkBeginCommandBuffer(handle_, &begin_info); |
| +} |
| + |
| +ScopedSingleUseCommandBufferRecorder::ScopedSingleUseCommandBufferRecorder( |
| + VulkanCommandBuffer& command_buffer) |
| + : CommandBufferRecorderBase(command_buffer) { |
| + ValidateSingleUse(command_buffer); |
| + VkCommandBufferBeginInfo begin_info = {}; |
| + begin_info.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO; |
| + begin_info.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT; |
| + vkBeginCommandBuffer(handle_, &begin_info); |
| +} |
| + |
| +} // namespace gpu |