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