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..df7fb2bd369d4b72259392d84f511814853c94f6 |
| --- /dev/null |
| +++ b/gpu/vulkan/vulkan_command_buffer.cc |
| @@ -0,0 +1,163 @@ |
| +// 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_EQ(static_cast<VkFence>(VK_NULL_HANDLE), submission_fence_); |
| + DCHECK(!recording_); |
| + command_pool_->DecrementCommandBufferCount(); |
| +} |
| + |
| +bool VulkanCommandBuffer::Initialize() { |
| + VkResult result = VK_SUCCESS; |
| + VkDevice device = GetVulkanDevice(); |
| + |
| + 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; |
| + |
| + result = |
| + vkAllocateCommandBuffers(device, &command_buffer_info, &command_buffer_); |
| + if (VK_SUCCESS != result) { |
| + DLOG(ERROR) << "vkAllocateCommandBuffers() failed: " << result; |
| + return false; |
| + } |
| + |
| + VkFenceCreateInfo fence_create_info = {}; |
| + fence_create_info.sType = VK_STRUCTURE_TYPE_FENCE_CREATE_INFO; |
| + fence_create_info.flags = VK_FENCE_CREATE_SIGNALED_BIT; |
|
piman
2016/03/29 19:09:21
I don't think you want it to be created signaled,
David Yen
2016/03/29 19:27:31
That's interesting that my tests passed. Maybe val
piman
2016/03/29 20:42:39
I think they would have caught it.
David Yen
2016/03/29 21:26:42
It kind of is already like that already, but becau
|
| + |
| + result = |
| + vkCreateFence(device, &fence_create_info, nullptr, &submission_fence_); |
| + if (VK_SUCCESS != result) { |
| + DLOG(ERROR) << "vkCreateFence(submission) failed: " << result; |
| + return false; |
| + } |
| + |
| + record_type_ = RECORD_TYPE_EMPTY; |
| + return true; |
| +} |
| + |
| +void VulkanCommandBuffer::Destroy() { |
| + VkDevice device = GetVulkanDevice(); |
| + if (VK_NULL_HANDLE != submission_fence_) { |
| + DCHECK(SubmissionFinished()); |
| + vkDestroyFence(device, submission_fence_, nullptr); |
| + submission_fence_ = VK_NULL_HANDLE; |
| + } |
| + |
| + if (VK_NULL_HANDLE != command_buffer_) { |
| + vkFreeCommandBuffers(device, 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, submission_fence_); |
|
piman
2016/03/29 19:09:21
Thought for later (nothing to do in this CL, it's
David Yen
2016/03/29 19:27:31
Would we want to submit multiple command buffers w
piman
2016/03/29 20:42:39
Possibly. For example, we'll need one render pass
|
| + 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::Clear() { |
| + // Mark to reset upon next use. |
| + if (record_type_ != RECORD_TYPE_EMPTY) |
| + record_type_ = RECORD_TYPE_DIRTY; |
| +} |
| + |
| +void VulkanCommandBuffer::Wait(uint64_t timeout) { |
| + vkWaitForFences(GetVulkanDevice(), 1, &submission_fence_, true, timeout); |
| +} |
| + |
| +bool VulkanCommandBuffer::SubmissionFinished() { |
| + return VK_SUCCESS == vkGetFenceStatus(GetVulkanDevice(), submission_fence_); |
| +} |
| + |
| +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) { |
| + // Block if command buffer is still in use. This can be externally avoided |
| + // using the asynchronous SubmissionFinished() function. |
| + vkWaitForFences(GetVulkanDevice(), 1, &submission_fence_, true, UINT64_MAX); |
| + vkResetFences(GetVulkanDevice(), 1, &submission_fence_); |
| + |
| + vkResetCommandBuffer(command_buffer_, 0); |
| + 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 |