Alexis Hetu | 767b41b | 2018-09-26 11:25:46 -0400 | [diff] [blame] | 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 | |
Nicolas Capens | d689d1c | 2018-11-19 16:02:36 -0500 | [diff] [blame] | 15 | #include "VkDevice.hpp" |
| 16 | |
Alexis Hetu | 767b41b | 2018-09-26 11:25:46 -0400 | [diff] [blame] | 17 | #include "VkConfig.h" |
| 18 | #include "VkDebug.hpp" |
Alexis Hetu | 048974f | 2019-02-15 15:28:37 -0500 | [diff] [blame] | 19 | #include "VkDescriptorSetLayout.hpp" |
Alexis Hetu | e1f51b9 | 2019-04-23 15:34:34 -0400 | [diff] [blame] | 20 | #include "VkFence.hpp" |
Alexis Hetu | 9e4d040 | 2018-10-16 15:44:12 -0400 | [diff] [blame] | 21 | #include "VkQueue.hpp" |
Alexis Hetu | 0da99f5 | 2019-02-27 12:54:52 -0500 | [diff] [blame] | 22 | #include "Device/Blitter.hpp" |
Nicolas Capens | d689d1c | 2018-11-19 16:02:36 -0500 | [diff] [blame] | 23 | |
Alexis Hetu | e1f51b9 | 2019-04-23 15:34:34 -0400 | [diff] [blame] | 24 | #include <chrono> |
| 25 | #include <climits> |
Alexis Hetu | 767b41b | 2018-09-26 11:25:46 -0400 | [diff] [blame] | 26 | #include <new> // Must #include this to use "placement new" |
| 27 | |
Alexis Hetu | e1f51b9 | 2019-04-23 15:34:34 -0400 | [diff] [blame] | 28 | namespace |
| 29 | { |
| 30 | std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds> now() |
| 31 | { |
| 32 | return std::chrono::time_point_cast<std::chrono::nanoseconds>(std::chrono::system_clock::now()); |
| 33 | } |
| 34 | } |
| 35 | |
Alexis Hetu | 767b41b | 2018-09-26 11:25:46 -0400 | [diff] [blame] | 36 | namespace vk |
| 37 | { |
| 38 | |
Ben Clayton | 6897e9b | 2019-07-16 17:27:27 +0100 | [diff] [blame] | 39 | std::shared_ptr<rr::Routine> Device::SamplingRoutineCache::query(const vk::Device::SamplingRoutineCache::Key& key) const |
Alexis Hetu | 6448bd6 | 2019-06-11 15:58:59 -0400 | [diff] [blame] | 40 | { |
Ben Clayton | f046402 | 2019-08-08 18:33:41 +0100 | [diff] [blame] | 41 | return cache.query(key); |
Alexis Hetu | 6448bd6 | 2019-06-11 15:58:59 -0400 | [diff] [blame] | 42 | } |
| 43 | |
Ben Clayton | 6897e9b | 2019-07-16 17:27:27 +0100 | [diff] [blame] | 44 | void Device::SamplingRoutineCache::add(const vk::Device::SamplingRoutineCache::Key& key, const std::shared_ptr<rr::Routine>& routine) |
Alexis Hetu | 6448bd6 | 2019-06-11 15:58:59 -0400 | [diff] [blame] | 45 | { |
| 46 | ASSERT(routine); |
Ben Clayton | f046402 | 2019-08-08 18:33:41 +0100 | [diff] [blame] | 47 | cache.add(key, routine); |
Alexis Hetu | 6448bd6 | 2019-06-11 15:58:59 -0400 | [diff] [blame] | 48 | } |
| 49 | |
Ben Clayton | 03c2aea | 2019-07-29 19:04:53 +0100 | [diff] [blame] | 50 | rr::Routine* Device::SamplingRoutineCache::queryConst(const vk::Device::SamplingRoutineCache::Key& key) const |
Alexis Hetu | 3575550 | 2019-07-22 13:51:49 -0400 | [diff] [blame] | 51 | { |
Ben Clayton | f046402 | 2019-08-08 18:33:41 +0100 | [diff] [blame] | 52 | return cache.queryConstCache(key).get(); |
Alexis Hetu | 3575550 | 2019-07-22 13:51:49 -0400 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | void Device::SamplingRoutineCache::updateConstCache() |
| 56 | { |
| 57 | cache.updateConstCache(); |
| 58 | } |
| 59 | |
Ben Clayton | b6c572d | 2019-09-07 11:41:39 +0000 | [diff] [blame^] | 60 | Device::Device(const VkDeviceCreateInfo* pCreateInfo, void* mem, PhysicalDevice *physicalDevice, const VkPhysicalDeviceFeatures *enabledFeatures, yarn::Scheduler *scheduler) |
Nicolas Capens | 0c73680 | 2019-05-27 12:53:31 -0400 | [diff] [blame] | 61 | : physicalDevice(physicalDevice), |
Alexis Hetu | 352791e | 2019-05-17 16:42:34 -0400 | [diff] [blame] | 62 | queues(reinterpret_cast<Queue*>(mem)), |
Nicolas Capens | a29aa77 | 2019-06-26 00:36:28 -0400 | [diff] [blame] | 63 | enabledExtensionCount(pCreateInfo->enabledExtensionCount), |
| 64 | enabledFeatures(enabledFeatures ? *enabledFeatures : VkPhysicalDeviceFeatures{}) // "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." |
Alexis Hetu | 767b41b | 2018-09-26 11:25:46 -0400 | [diff] [blame] | 65 | { |
Alexis Hetu | 767b41b | 2018-09-26 11:25:46 -0400 | [diff] [blame] | 66 | for(uint32_t i = 0; i < pCreateInfo->queueCreateInfoCount; i++) |
| 67 | { |
| 68 | const VkDeviceQueueCreateInfo& queueCreateInfo = pCreateInfo->pQueueCreateInfos[i]; |
Alexis Hetu | e70c351 | 2018-10-17 13:18:04 -0400 | [diff] [blame] | 69 | queueCount += queueCreateInfo.queueCount; |
Alexis Hetu | 767b41b | 2018-09-26 11:25:46 -0400 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | uint32_t queueID = 0; |
| 73 | for(uint32_t i = 0; i < pCreateInfo->queueCreateInfoCount; i++) |
| 74 | { |
| 75 | const VkDeviceQueueCreateInfo& queueCreateInfo = pCreateInfo->pQueueCreateInfos[i]; |
| 76 | |
| 77 | for(uint32_t j = 0; j < queueCreateInfo.queueCount; j++, queueID++) |
| 78 | { |
Ben Clayton | d6c6136 | 2019-08-14 18:16:01 +0100 | [diff] [blame] | 79 | new (&queues[queueID]) Queue(this, scheduler); |
Alexis Hetu | 767b41b | 2018-09-26 11:25:46 -0400 | [diff] [blame] | 80 | } |
| 81 | } |
Nicolas Capens | d689d1c | 2018-11-19 16:02:36 -0500 | [diff] [blame] | 82 | |
Alexis Hetu | 352791e | 2019-05-17 16:42:34 -0400 | [diff] [blame] | 83 | extensions = reinterpret_cast<ExtensionName*>(static_cast<uint8_t*>(mem) + (sizeof(Queue) * queueCount)); |
| 84 | for(uint32_t i = 0; i < enabledExtensionCount; i++) |
| 85 | { |
| 86 | strncpy(extensions[i], pCreateInfo->ppEnabledExtensionNames[i], VK_MAX_EXTENSION_NAME_SIZE); |
| 87 | } |
| 88 | |
Nicolas Capens | d689d1c | 2018-11-19 16:02:36 -0500 | [diff] [blame] | 89 | if(pCreateInfo->enabledLayerCount) |
| 90 | { |
| 91 | // "The ppEnabledLayerNames and enabledLayerCount members of VkDeviceCreateInfo are deprecated and their values must be ignored by implementations." |
Ben Clayton | 00424c1 | 2019-03-17 17:29:30 +0000 | [diff] [blame] | 92 | UNIMPLEMENTED("enabledLayerCount"); // TODO(b/119321052): UNIMPLEMENTED() should be used only for features that must still be implemented. Use a more informational macro here. |
Nicolas Capens | d689d1c | 2018-11-19 16:02:36 -0500 | [diff] [blame] | 93 | } |
Alexis Hetu | 0da99f5 | 2019-02-27 12:54:52 -0500 | [diff] [blame] | 94 | |
Alexis Hetu | 1a9714a | 2019-04-12 11:48:12 -0400 | [diff] [blame] | 95 | // FIXME (b/119409619): use an allocator here so we can control all memory allocations |
Alexis Hetu | 6448bd6 | 2019-06-11 15:58:59 -0400 | [diff] [blame] | 96 | blitter.reset(new sw::Blitter()); |
Alexis Hetu | 3575550 | 2019-07-22 13:51:49 -0400 | [diff] [blame] | 97 | samplingRoutineCache.reset(new SamplingRoutineCache()); |
Alexis Hetu | 767b41b | 2018-09-26 11:25:46 -0400 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | void Device::destroy(const VkAllocationCallbacks* pAllocator) |
| 101 | { |
Alexis Hetu | af3c102 | 2018-12-12 13:26:15 -0500 | [diff] [blame] | 102 | for(uint32_t i = 0; i < queueCount; i++) |
| 103 | { |
Ben Clayton | 7e0a036 | 2019-05-20 11:32:35 +0100 | [diff] [blame] | 104 | queues[i].~Queue(); |
Alexis Hetu | af3c102 | 2018-12-12 13:26:15 -0500 | [diff] [blame] | 105 | } |
| 106 | |
Alexis Hetu | 767b41b | 2018-09-26 11:25:46 -0400 | [diff] [blame] | 107 | vk::deallocate(queues, pAllocator); |
| 108 | } |
| 109 | |
Nicolas Capens | 0c73680 | 2019-05-27 12:53:31 -0400 | [diff] [blame] | 110 | size_t Device::ComputeRequiredAllocationSize(const VkDeviceCreateInfo* pCreateInfo) |
Alexis Hetu | 767b41b | 2018-09-26 11:25:46 -0400 | [diff] [blame] | 111 | { |
| 112 | uint32_t queueCount = 0; |
Nicolas Capens | 0c73680 | 2019-05-27 12:53:31 -0400 | [diff] [blame] | 113 | for(uint32_t i = 0; i < pCreateInfo->queueCreateInfoCount; i++) |
Alexis Hetu | 767b41b | 2018-09-26 11:25:46 -0400 | [diff] [blame] | 114 | { |
Nicolas Capens | 0c73680 | 2019-05-27 12:53:31 -0400 | [diff] [blame] | 115 | queueCount += pCreateInfo->pQueueCreateInfos[i].queueCount; |
Alexis Hetu | 767b41b | 2018-09-26 11:25:46 -0400 | [diff] [blame] | 116 | } |
| 117 | |
Nicolas Capens | 0c73680 | 2019-05-27 12:53:31 -0400 | [diff] [blame] | 118 | return (sizeof(Queue) * queueCount) + (pCreateInfo->enabledExtensionCount * sizeof(ExtensionName)); |
Alexis Hetu | 352791e | 2019-05-17 16:42:34 -0400 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | bool Device::hasExtension(const char* extensionName) const |
| 122 | { |
| 123 | for(uint32_t i = 0; i < enabledExtensionCount; i++) |
| 124 | { |
| 125 | if(strncmp(extensions[i], extensionName, VK_MAX_EXTENSION_NAME_SIZE) == 0) |
| 126 | { |
| 127 | return true; |
| 128 | } |
| 129 | } |
| 130 | return false; |
Alexis Hetu | 767b41b | 2018-09-26 11:25:46 -0400 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | VkQueue Device::getQueue(uint32_t queueFamilyIndex, uint32_t queueIndex) const |
| 134 | { |
| 135 | ASSERT(queueFamilyIndex == 0); |
| 136 | |
| 137 | return queues[queueIndex]; |
| 138 | } |
| 139 | |
Alexis Hetu | e1f51b9 | 2019-04-23 15:34:34 -0400 | [diff] [blame] | 140 | VkResult Device::waitForFences(uint32_t fenceCount, const VkFence* pFences, VkBool32 waitAll, uint64_t timeout) |
Alexis Hetu | c4bd9df | 2018-12-07 11:28:40 -0500 | [diff] [blame] | 141 | { |
Ben Clayton | 6779e5e | 2019-05-20 11:07:58 +0100 | [diff] [blame] | 142 | using time_point = std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds>; |
Alexis Hetu | e1f51b9 | 2019-04-23 15:34:34 -0400 | [diff] [blame] | 143 | const time_point start = now(); |
| 144 | const uint64_t max_timeout = (LLONG_MAX - start.time_since_epoch().count()); |
| 145 | bool infiniteTimeout = (timeout > max_timeout); |
| 146 | const time_point end_ns = start + std::chrono::nanoseconds(std::min(max_timeout, timeout)); |
Nicolas Capens | 85035be | 2019-06-05 13:54:18 -0400 | [diff] [blame] | 147 | |
| 148 | if(waitAll != VK_FALSE) // All fences must be signaled |
Alexis Hetu | e1f51b9 | 2019-04-23 15:34:34 -0400 | [diff] [blame] | 149 | { |
| 150 | for(uint32_t i = 0; i < fenceCount; i++) |
| 151 | { |
| 152 | if(timeout == 0) |
| 153 | { |
| 154 | if(Cast(pFences[i])->getStatus() != VK_SUCCESS) // At least one fence is not signaled |
| 155 | { |
| 156 | return VK_TIMEOUT; |
| 157 | } |
| 158 | } |
| 159 | else if(infiniteTimeout) |
| 160 | { |
| 161 | if(Cast(pFences[i])->wait() != VK_SUCCESS) // At least one fence is not signaled |
| 162 | { |
| 163 | return VK_TIMEOUT; |
| 164 | } |
| 165 | } |
| 166 | else |
| 167 | { |
Ben Clayton | 6779e5e | 2019-05-20 11:07:58 +0100 | [diff] [blame] | 168 | if(Cast(pFences[i])->wait(end_ns) != VK_SUCCESS) // At least one fence is not signaled |
Alexis Hetu | e1f51b9 | 2019-04-23 15:34:34 -0400 | [diff] [blame] | 169 | { |
| 170 | return VK_TIMEOUT; |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | return VK_SUCCESS; |
| 176 | } |
| 177 | else // At least one fence must be signaled |
| 178 | { |
| 179 | // Start by quickly checking the status of all fences, as only one is required |
| 180 | for(uint32_t i = 0; i < fenceCount; i++) |
| 181 | { |
| 182 | if(Cast(pFences[i])->getStatus() == VK_SUCCESS) // At least one fence is signaled |
| 183 | { |
| 184 | return VK_SUCCESS; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | if(timeout > 0) |
| 189 | { |
| 190 | for(uint32_t i = 0; i < fenceCount; i++) |
| 191 | { |
| 192 | if(infiniteTimeout) |
| 193 | { |
| 194 | if(Cast(pFences[i])->wait() == VK_SUCCESS) // At least one fence is signaled |
| 195 | { |
| 196 | return VK_SUCCESS; |
| 197 | } |
| 198 | } |
| 199 | else |
| 200 | { |
Ben Clayton | 6779e5e | 2019-05-20 11:07:58 +0100 | [diff] [blame] | 201 | if(Cast(pFences[i])->wait(end_ns) == VK_SUCCESS) // At least one fence is signaled |
Alexis Hetu | e1f51b9 | 2019-04-23 15:34:34 -0400 | [diff] [blame] | 202 | { |
| 203 | return VK_SUCCESS; |
| 204 | } |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | return VK_TIMEOUT; |
| 210 | } |
Alexis Hetu | c4bd9df | 2018-12-07 11:28:40 -0500 | [diff] [blame] | 211 | } |
| 212 | |
Alexis Hetu | e1f51b9 | 2019-04-23 15:34:34 -0400 | [diff] [blame] | 213 | VkResult Device::waitIdle() |
Ben Clayton | 00424c1 | 2019-03-17 17:29:30 +0000 | [diff] [blame] | 214 | { |
| 215 | for(uint32_t i = 0; i < queueCount; i++) |
| 216 | { |
| 217 | queues[i].waitIdle(); |
| 218 | } |
Alexis Hetu | e1f51b9 | 2019-04-23 15:34:34 -0400 | [diff] [blame] | 219 | |
| 220 | return VK_SUCCESS; |
Alexis Hetu | cda0cf9 | 2019-01-24 15:48:55 -0500 | [diff] [blame] | 221 | } |
| 222 | |
Alexis Hetu | 9e4d040 | 2018-10-16 15:44:12 -0400 | [diff] [blame] | 223 | void Device::getDescriptorSetLayoutSupport(const VkDescriptorSetLayoutCreateInfo* pCreateInfo, |
| 224 | VkDescriptorSetLayoutSupport* pSupport) const |
| 225 | { |
| 226 | // Mark everything as unsupported |
| 227 | pSupport->supported = VK_FALSE; |
| 228 | } |
| 229 | |
Alexis Hetu | 048974f | 2019-02-15 15:28:37 -0500 | [diff] [blame] | 230 | void Device::updateDescriptorSets(uint32_t descriptorWriteCount, const VkWriteDescriptorSet* pDescriptorWrites, |
| 231 | uint32_t descriptorCopyCount, const VkCopyDescriptorSet* pDescriptorCopies) |
| 232 | { |
| 233 | for(uint32_t i = 0; i < descriptorWriteCount; i++) |
| 234 | { |
Alexis Hetu | 6448bd6 | 2019-06-11 15:58:59 -0400 | [diff] [blame] | 235 | DescriptorSetLayout::WriteDescriptorSet(this, pDescriptorWrites[i]); |
Alexis Hetu | 048974f | 2019-02-15 15:28:37 -0500 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | for(uint32_t i = 0; i < descriptorCopyCount; i++) |
| 239 | { |
| 240 | DescriptorSetLayout::CopyDescriptorSet(pDescriptorCopies[i]); |
| 241 | } |
| 242 | } |
| 243 | |
Alexis Hetu | 3575550 | 2019-07-22 13:51:49 -0400 | [diff] [blame] | 244 | Device::SamplingRoutineCache* Device::getSamplingRoutineCache() const |
Alexis Hetu | 6448bd6 | 2019-06-11 15:58:59 -0400 | [diff] [blame] | 245 | { |
Alexis Hetu | 6448bd6 | 2019-06-11 15:58:59 -0400 | [diff] [blame] | 246 | return samplingRoutineCache.get(); |
| 247 | } |
| 248 | |
Ben Clayton | 03c2aea | 2019-07-29 19:04:53 +0100 | [diff] [blame] | 249 | rr::Routine* Device::findInConstCache(const SamplingRoutineCache::Key& key) const |
Alexis Hetu | 3575550 | 2019-07-22 13:51:49 -0400 | [diff] [blame] | 250 | { |
| 251 | return samplingRoutineCache->queryConst(key); |
| 252 | } |
| 253 | |
| 254 | void Device::updateSamplingRoutineConstCache() |
| 255 | { |
| 256 | std::unique_lock<std::mutex> lock(samplingRoutineCacheMutex); |
| 257 | samplingRoutineCache->updateConstCache(); |
| 258 | } |
| 259 | |
Alexis Hetu | 6448bd6 | 2019-06-11 15:58:59 -0400 | [diff] [blame] | 260 | std::mutex& Device::getSamplingRoutineCacheMutex() |
| 261 | { |
| 262 | return samplingRoutineCacheMutex; |
| 263 | } |
| 264 | |
Alexis Hetu | 767b41b | 2018-09-26 11:25:46 -0400 | [diff] [blame] | 265 | } // namespace vk |