1 // Copyright 2018 The SwiftShader Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "VkQueue.hpp"
16 #include "VkCommandBuffer.hpp"
17 #include "VkFence.hpp"
18 #include "VkSemaphore.hpp"
19 #include "Device/Renderer.hpp"
20 #include "WSI/VkSwapchainKHR.hpp"
21
22 #include "marl/defer.h"
23 #include "marl/scheduler.h"
24 #include "marl/thread.h"
25 #include "marl/trace.h"
26
27 #include <cstring>
28
29 namespace {
30
DeepCopySubmitInfo(uint32_t submitCount,const VkSubmitInfo * pSubmits)31 VkSubmitInfo *DeepCopySubmitInfo(uint32_t submitCount, const VkSubmitInfo *pSubmits)
32 {
33 size_t submitSize = sizeof(VkSubmitInfo) * submitCount;
34 size_t totalSize = submitSize;
35 for(uint32_t i = 0; i < submitCount; i++)
36 {
37 totalSize += pSubmits[i].waitSemaphoreCount * sizeof(VkSemaphore);
38 totalSize += pSubmits[i].waitSemaphoreCount * sizeof(VkPipelineStageFlags);
39 totalSize += pSubmits[i].signalSemaphoreCount * sizeof(VkSemaphore);
40 totalSize += pSubmits[i].commandBufferCount * sizeof(VkCommandBuffer);
41 }
42
43 uint8_t *mem = static_cast<uint8_t *>(
44 vk::allocate(totalSize, vk::REQUIRED_MEMORY_ALIGNMENT, vk::DEVICE_MEMORY, vk::Fence::GetAllocationScope()));
45
46 auto submits = new(mem) VkSubmitInfo[submitCount];
47 memcpy(mem, pSubmits, submitSize);
48 mem += submitSize;
49
50 for(uint32_t i = 0; i < submitCount; i++)
51 {
52 size_t size = pSubmits[i].waitSemaphoreCount * sizeof(VkSemaphore);
53 submits[i].pWaitSemaphores = reinterpret_cast<const VkSemaphore *>(mem);
54 memcpy(mem, pSubmits[i].pWaitSemaphores, size);
55 mem += size;
56
57 size = pSubmits[i].waitSemaphoreCount * sizeof(VkPipelineStageFlags);
58 submits[i].pWaitDstStageMask = reinterpret_cast<const VkPipelineStageFlags *>(mem);
59 memcpy(mem, pSubmits[i].pWaitDstStageMask, size);
60 mem += size;
61
62 size = pSubmits[i].signalSemaphoreCount * sizeof(VkSemaphore);
63 submits[i].pSignalSemaphores = reinterpret_cast<const VkSemaphore *>(mem);
64 memcpy(mem, pSubmits[i].pSignalSemaphores, size);
65 mem += size;
66
67 size = pSubmits[i].commandBufferCount * sizeof(VkCommandBuffer);
68 submits[i].pCommandBuffers = reinterpret_cast<const VkCommandBuffer *>(mem);
69 memcpy(mem, pSubmits[i].pCommandBuffers, size);
70 mem += size;
71 }
72
73 return submits;
74 }
75
76 } // anonymous namespace
77
78 namespace vk {
79
Queue(Device * device,marl::Scheduler * scheduler)80 Queue::Queue(Device *device, marl::Scheduler *scheduler)
81 : device(device)
82 {
83 queueThread = std::thread(&Queue::taskLoop, this, scheduler);
84 }
85
~Queue()86 Queue::~Queue()
87 {
88 Task task;
89 task.type = Task::KILL_THREAD;
90 pending.put(task);
91
92 queueThread.join();
93 ASSERT_MSG(pending.count() == 0, "queue has work after worker thread shutdown");
94
95 garbageCollect();
96 }
97
submit(uint32_t submitCount,const VkSubmitInfo * pSubmits,Fence * fence)98 VkResult Queue::submit(uint32_t submitCount, const VkSubmitInfo *pSubmits, Fence *fence)
99 {
100 garbageCollect();
101
102 Task task;
103 task.submitCount = submitCount;
104 task.pSubmits = DeepCopySubmitInfo(submitCount, pSubmits);
105 task.events = fence;
106
107 if(task.events)
108 {
109 task.events->start();
110 }
111
112 pending.put(task);
113
114 return VK_SUCCESS;
115 }
116
submitQueue(const Task & task)117 void Queue::submitQueue(const Task &task)
118 {
119 if(renderer == nullptr)
120 {
121 renderer.reset(new sw::Renderer(device));
122 }
123
124 for(uint32_t i = 0; i < task.submitCount; i++)
125 {
126 auto &submitInfo = task.pSubmits[i];
127 for(uint32_t j = 0; j < submitInfo.waitSemaphoreCount; j++)
128 {
129 vk::Cast(submitInfo.pWaitSemaphores[j])->wait(submitInfo.pWaitDstStageMask[j]);
130 }
131
132 {
133 CommandBuffer::ExecutionState executionState;
134 executionState.renderer = renderer.get();
135 executionState.events = task.events;
136 for(uint32_t j = 0; j < submitInfo.commandBufferCount; j++)
137 {
138 vk::Cast(submitInfo.pCommandBuffers[j])->submit(executionState);
139 }
140 }
141
142 for(uint32_t j = 0; j < submitInfo.signalSemaphoreCount; j++)
143 {
144 vk::Cast(submitInfo.pSignalSemaphores[j])->signal();
145 }
146 }
147
148 if(task.pSubmits)
149 {
150 toDelete.put(task.pSubmits);
151 }
152
153 if(task.events)
154 {
155 // TODO: fix renderer signaling so that work submitted separately from (but before) a fence
156 // is guaranteed complete by the time the fence signals.
157 renderer->synchronize();
158 task.events->finish();
159 }
160 }
161
taskLoop(marl::Scheduler * scheduler)162 void Queue::taskLoop(marl::Scheduler *scheduler)
163 {
164 marl::Thread::setName("Queue<%p>", this);
165 scheduler->bind();
166 defer(scheduler->unbind());
167
168 while(true)
169 {
170 Task task = pending.take();
171
172 switch(task.type)
173 {
174 case Task::KILL_THREAD:
175 ASSERT_MSG(pending.count() == 0, "queue has remaining work!");
176 return;
177 case Task::SUBMIT_QUEUE:
178 submitQueue(task);
179 break;
180 default:
181 UNREACHABLE("task.type %d", static_cast<int>(task.type));
182 break;
183 }
184 }
185 }
186
waitIdle()187 VkResult Queue::waitIdle()
188 {
189 // Wait for task queue to flush.
190 sw::WaitGroup wg;
191 wg.add();
192
193 Task task;
194 task.events = &wg;
195 pending.put(task);
196
197 wg.wait();
198
199 garbageCollect();
200
201 return VK_SUCCESS;
202 }
203
garbageCollect()204 void Queue::garbageCollect()
205 {
206 while(true)
207 {
208 auto v = toDelete.tryTake();
209 if(!v.second) { break; }
210 vk::deallocate(v.first, DEVICE_MEMORY);
211 }
212 }
213
214 #ifndef __ANDROID__
present(const VkPresentInfoKHR * presentInfo)215 VkResult Queue::present(const VkPresentInfoKHR *presentInfo)
216 {
217 // This is a hack to deal with screen tearing for now.
218 // Need to correctly implement threading using VkSemaphore
219 // to get rid of it. b/132458423
220 waitIdle();
221
222 for(uint32_t i = 0; i < presentInfo->waitSemaphoreCount; i++)
223 {
224 vk::Cast(presentInfo->pWaitSemaphores[i])->wait();
225 }
226
227 VkResult commandResult = VK_SUCCESS;
228
229 for(uint32_t i = 0; i < presentInfo->swapchainCount; i++)
230 {
231 VkResult perSwapchainResult = vk::Cast(presentInfo->pSwapchains[i])->present(presentInfo->pImageIndices[i]);
232
233 if(presentInfo->pResults)
234 {
235 presentInfo->pResults[i] = perSwapchainResult;
236 }
237
238 // Keep track of the worst result code. VK_SUBOPTIMAL_KHR is a success code so it should
239 // not override failure codes, but should not get replaced by a VK_SUCCESS result itself.
240 if(perSwapchainResult != VK_SUCCESS)
241 {
242 if(commandResult == VK_SUCCESS || commandResult == VK_SUBOPTIMAL_KHR)
243 {
244 commandResult = perSwapchainResult;
245 }
246 }
247 }
248
249 return commandResult;
250 }
251 #endif
252
253 } // namespace vk
254