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 "VkDevice.hpp"
16
17 #include "VkConfig.hpp"
18 #include "VkDescriptorSetLayout.hpp"
19 #include "VkFence.hpp"
20 #include "VkQueue.hpp"
21 #include "Debug/Context.hpp"
22 #include "Debug/Server.hpp"
23 #include "Device/Blitter.hpp"
24 #include "System/Debug.hpp"
25
26 #include <chrono>
27 #include <climits>
28 #include <new> // Must #include this to use "placement new"
29
30 namespace {
31
now()32 std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> now()
33 {
34 return std::chrono::time_point_cast<std::chrono::nanoseconds>(std::chrono::system_clock::now());
35 }
36
37 } // anonymous namespace
38
39 namespace vk {
40
updateSnapshot()41 void Device::SamplingRoutineCache::updateSnapshot()
42 {
43 marl::lock lock(mutex);
44
45 if(snapshotNeedsUpdate)
46 {
47 snapshot.clear();
48
49 for(auto it : cache)
50 {
51 snapshot[it.key()] = it.data();
52 }
53
54 snapshotNeedsUpdate = false;
55 }
56 }
57
~SamplerIndexer()58 Device::SamplerIndexer::~SamplerIndexer()
59 {
60 ASSERT(map.empty());
61 }
62
index(const SamplerState & samplerState)63 uint32_t Device::SamplerIndexer::index(const SamplerState &samplerState)
64 {
65 marl::lock lock(mutex);
66
67 auto it = map.find(samplerState);
68
69 if(it != map.end())
70 {
71 it->second.count++;
72 return it->second.id;
73 }
74
75 nextID++;
76
77 map.emplace(samplerState, Identifier{ nextID, 1 });
78
79 return nextID;
80 }
81
remove(const SamplerState & samplerState)82 void Device::SamplerIndexer::remove(const SamplerState &samplerState)
83 {
84 marl::lock lock(mutex);
85
86 auto it = map.find(samplerState);
87 ASSERT(it != map.end());
88
89 auto count = --it->second.count;
90 if(count == 0)
91 {
92 map.erase(it);
93 }
94 }
95
Device(const VkDeviceCreateInfo * pCreateInfo,void * mem,PhysicalDevice * physicalDevice,const VkPhysicalDeviceFeatures * enabledFeatures,const std::shared_ptr<marl::Scheduler> & scheduler)96 Device::Device(const VkDeviceCreateInfo *pCreateInfo, void *mem, PhysicalDevice *physicalDevice, const VkPhysicalDeviceFeatures *enabledFeatures, const std::shared_ptr<marl::Scheduler> &scheduler)
97 : physicalDevice(physicalDevice)
98 , queues(reinterpret_cast<Queue *>(mem))
99 , enabledExtensionCount(pCreateInfo->enabledExtensionCount)
100 , enabledFeatures(enabledFeatures ? *enabledFeatures : VkPhysicalDeviceFeatures{})
101 , // "Setting pEnabledFeatures to NULL and not including a VkPhysicalDeviceFeatures2 in the pNext member of VkDeviceCreateInfo is equivalent to setting all members of the structure to VK_FALSE."
102 scheduler(scheduler)
103 {
104 for(uint32_t i = 0; i < pCreateInfo->queueCreateInfoCount; i++)
105 {
106 const VkDeviceQueueCreateInfo &queueCreateInfo = pCreateInfo->pQueueCreateInfos[i];
107 queueCount += queueCreateInfo.queueCount;
108 }
109
110 uint32_t queueID = 0;
111 for(uint32_t i = 0; i < pCreateInfo->queueCreateInfoCount; i++)
112 {
113 const VkDeviceQueueCreateInfo &queueCreateInfo = pCreateInfo->pQueueCreateInfos[i];
114
115 for(uint32_t j = 0; j < queueCreateInfo.queueCount; j++, queueID++)
116 {
117 new(&queues[queueID]) Queue(this, scheduler.get());
118 }
119 }
120
121 extensions = reinterpret_cast<ExtensionName *>(static_cast<uint8_t *>(mem) + (sizeof(Queue) * queueCount));
122 for(uint32_t i = 0; i < enabledExtensionCount; i++)
123 {
124 strncpy(extensions[i], pCreateInfo->ppEnabledExtensionNames[i], VK_MAX_EXTENSION_NAME_SIZE);
125 }
126
127 if(pCreateInfo->enabledLayerCount)
128 {
129 // "The ppEnabledLayerNames and enabledLayerCount members of VkDeviceCreateInfo are deprecated and their values must be ignored by implementations."
130 UNSUPPORTED("enabledLayerCount");
131 }
132
133 // FIXME (b/119409619): use an allocator here so we can control all memory allocations
134 blitter.reset(new sw::Blitter());
135 samplingRoutineCache.reset(new SamplingRoutineCache());
136 samplerIndexer.reset(new SamplerIndexer());
137
138 #ifdef ENABLE_VK_DEBUGGER
139 static auto port = getenv("VK_DEBUGGER_PORT");
140 if(port)
141 {
142 // Construct the debugger context and server - this may block for a
143 // debugger connection, allowing breakpoints to be set before they're
144 // executed.
145 debugger.context = vk::dbg::Context::create();
146 debugger.server = vk::dbg::Server::create(debugger.context, atoi(port));
147 }
148 #endif // ENABLE_VK_DEBUGGER
149 }
150
destroy(const VkAllocationCallbacks * pAllocator)151 void Device::destroy(const VkAllocationCallbacks *pAllocator)
152 {
153 for(uint32_t i = 0; i < queueCount; i++)
154 {
155 queues[i].~Queue();
156 }
157
158 vk::deallocate(queues, pAllocator);
159 }
160
ComputeRequiredAllocationSize(const VkDeviceCreateInfo * pCreateInfo)161 size_t Device::ComputeRequiredAllocationSize(const VkDeviceCreateInfo *pCreateInfo)
162 {
163 uint32_t queueCount = 0;
164 for(uint32_t i = 0; i < pCreateInfo->queueCreateInfoCount; i++)
165 {
166 queueCount += pCreateInfo->pQueueCreateInfos[i].queueCount;
167 }
168
169 return (sizeof(Queue) * queueCount) + (pCreateInfo->enabledExtensionCount * sizeof(ExtensionName));
170 }
171
hasExtension(const char * extensionName) const172 bool Device::hasExtension(const char *extensionName) const
173 {
174 for(uint32_t i = 0; i < enabledExtensionCount; i++)
175 {
176 if(strncmp(extensions[i], extensionName, VK_MAX_EXTENSION_NAME_SIZE) == 0)
177 {
178 return true;
179 }
180 }
181 return false;
182 }
183
getQueue(uint32_t queueFamilyIndex,uint32_t queueIndex) const184 VkQueue Device::getQueue(uint32_t queueFamilyIndex, uint32_t queueIndex) const
185 {
186 ASSERT(queueFamilyIndex == 0);
187
188 return queues[queueIndex];
189 }
190
waitForFences(uint32_t fenceCount,const VkFence * pFences,VkBool32 waitAll,uint64_t timeout)191 VkResult Device::waitForFences(uint32_t fenceCount, const VkFence *pFences, VkBool32 waitAll, uint64_t timeout)
192 {
193 using time_point = std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds>;
194 const time_point start = now();
195 const uint64_t max_timeout = (LLONG_MAX - start.time_since_epoch().count());
196 bool infiniteTimeout = (timeout > max_timeout);
197 const time_point end_ns = start + std::chrono::nanoseconds(std::min(max_timeout, timeout));
198
199 if(waitAll != VK_FALSE) // All fences must be signaled
200 {
201 for(uint32_t i = 0; i < fenceCount; i++)
202 {
203 if(timeout == 0)
204 {
205 if(Cast(pFences[i])->getStatus() != VK_SUCCESS) // At least one fence is not signaled
206 {
207 return VK_TIMEOUT;
208 }
209 }
210 else if(infiniteTimeout)
211 {
212 if(Cast(pFences[i])->wait() != VK_SUCCESS) // At least one fence is not signaled
213 {
214 return VK_TIMEOUT;
215 }
216 }
217 else
218 {
219 if(Cast(pFences[i])->wait(end_ns) != VK_SUCCESS) // At least one fence is not signaled
220 {
221 return VK_TIMEOUT;
222 }
223 }
224 }
225
226 return VK_SUCCESS;
227 }
228 else // At least one fence must be signaled
229 {
230 marl::containers::vector<marl::Event, 8> events;
231 for(uint32_t i = 0; i < fenceCount; i++)
232 {
233 events.push_back(Cast(pFences[i])->getEvent());
234 }
235
236 auto any = marl::Event::any(events.begin(), events.end());
237
238 if(timeout == 0)
239 {
240 return any.isSignalled() ? VK_SUCCESS : VK_TIMEOUT;
241 }
242 else if(infiniteTimeout)
243 {
244 any.wait();
245 return VK_SUCCESS;
246 }
247 else
248 {
249 return any.wait_until(end_ns) ? VK_SUCCESS : VK_TIMEOUT;
250 }
251 }
252 }
253
waitIdle()254 VkResult Device::waitIdle()
255 {
256 for(uint32_t i = 0; i < queueCount; i++)
257 {
258 queues[i].waitIdle();
259 }
260
261 return VK_SUCCESS;
262 }
263
getDescriptorSetLayoutSupport(const VkDescriptorSetLayoutCreateInfo * pCreateInfo,VkDescriptorSetLayoutSupport * pSupport) const264 void Device::getDescriptorSetLayoutSupport(const VkDescriptorSetLayoutCreateInfo *pCreateInfo,
265 VkDescriptorSetLayoutSupport *pSupport) const
266 {
267 // From Vulkan Spec 13.2.1 Descriptor Set Layout, in description of vkGetDescriptorSetLayoutSupport:
268 // "This command does not consider other limits such as maxPerStageDescriptor*, and so a descriptor
269 // set layout that is supported according to this command must still satisfy the pipeline layout limits
270 // such as maxPerStageDescriptor* in order to be used in a pipeline layout."
271
272 // We have no "strange" limitations to enforce beyond the device limits, so we can safely always claim support.
273 pSupport->supported = VK_TRUE;
274 }
275
updateDescriptorSets(uint32_t descriptorWriteCount,const VkWriteDescriptorSet * pDescriptorWrites,uint32_t descriptorCopyCount,const VkCopyDescriptorSet * pDescriptorCopies)276 void Device::updateDescriptorSets(uint32_t descriptorWriteCount, const VkWriteDescriptorSet *pDescriptorWrites,
277 uint32_t descriptorCopyCount, const VkCopyDescriptorSet *pDescriptorCopies)
278 {
279 for(uint32_t i = 0; i < descriptorWriteCount; i++)
280 {
281 DescriptorSetLayout::WriteDescriptorSet(this, pDescriptorWrites[i]);
282 }
283
284 for(uint32_t i = 0; i < descriptorCopyCount; i++)
285 {
286 DescriptorSetLayout::CopyDescriptorSet(pDescriptorCopies[i]);
287 }
288 }
289
getRequirements(VkMemoryDedicatedRequirements * requirements) const290 void Device::getRequirements(VkMemoryDedicatedRequirements *requirements) const
291 {
292 requirements->prefersDedicatedAllocation = VK_FALSE;
293 requirements->requiresDedicatedAllocation = VK_FALSE;
294 }
295
getSamplingRoutineCache() const296 Device::SamplingRoutineCache *Device::getSamplingRoutineCache() const
297 {
298 return samplingRoutineCache.get();
299 }
300
updateSamplingRoutineSnapshotCache()301 void Device::updateSamplingRoutineSnapshotCache()
302 {
303 samplingRoutineCache->updateSnapshot();
304 }
305
indexSampler(const SamplerState & samplerState)306 uint32_t Device::indexSampler(const SamplerState &samplerState)
307 {
308 return samplerIndexer->index(samplerState);
309 }
310
removeSampler(const SamplerState & samplerState)311 void Device::removeSampler(const SamplerState &samplerState)
312 {
313 samplerIndexer->remove(samplerState);
314 }
315
setDebugUtilsObjectName(const VkDebugUtilsObjectNameInfoEXT * pNameInfo)316 VkResult Device::setDebugUtilsObjectName(const VkDebugUtilsObjectNameInfoEXT *pNameInfo)
317 {
318 // Optionally maps user-friendly name to an object
319 return VK_SUCCESS;
320 }
321
setDebugUtilsObjectTag(const VkDebugUtilsObjectTagInfoEXT * pTagInfo)322 VkResult Device::setDebugUtilsObjectTag(const VkDebugUtilsObjectTagInfoEXT *pTagInfo)
323 {
324 // Optionally attach arbitrary data to an object
325 return VK_SUCCESS;
326 }
327
registerImageView(ImageView * imageView)328 void Device::registerImageView(ImageView *imageView)
329 {
330 if(imageView != nullptr)
331 {
332 marl::lock lock(imageViewSetMutex);
333 imageViewSet.insert(imageView);
334 }
335 }
336
unregisterImageView(ImageView * imageView)337 void Device::unregisterImageView(ImageView *imageView)
338 {
339 if(imageView != nullptr)
340 {
341 marl::lock lock(imageViewSetMutex);
342 auto it = imageViewSet.find(imageView);
343 if(it != imageViewSet.end())
344 {
345 imageViewSet.erase(it);
346 }
347 }
348 }
349
prepareForSampling(ImageView * imageView)350 void Device::prepareForSampling(ImageView *imageView)
351 {
352 if(imageView != nullptr)
353 {
354 marl::lock lock(imageViewSetMutex);
355
356 auto it = imageViewSet.find(imageView);
357 if(it != imageViewSet.end())
358 {
359 imageView->prepareForSampling();
360 }
361 }
362 }
363
contentsChanged(ImageView * imageView)364 void Device::contentsChanged(ImageView *imageView)
365 {
366 if(imageView != nullptr)
367 {
368 marl::lock lock(imageViewSetMutex);
369
370 auto it = imageViewSet.find(imageView);
371 if(it != imageViewSet.end())
372 {
373 imageView->contentsChanged();
374 }
375 }
376 }
377
378 } // namespace vk
379