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 | |
Nicolas Capens | 0c73680 | 2019-05-27 12:53:31 -0400 | [diff] [blame] | 39 | Device::Device(const VkDeviceCreateInfo* pCreateInfo, void* mem, PhysicalDevice *physicalDevice) |
| 40 | : physicalDevice(physicalDevice), |
Alexis Hetu | 352791e | 2019-05-17 16:42:34 -0400 | [diff] [blame] | 41 | queues(reinterpret_cast<Queue*>(mem)), |
Nicolas Capens | 0c73680 | 2019-05-27 12:53:31 -0400 | [diff] [blame] | 42 | enabledExtensionCount(pCreateInfo->enabledExtensionCount) |
Alexis Hetu | 767b41b | 2018-09-26 11:25:46 -0400 | [diff] [blame] | 43 | { |
Alexis Hetu | 767b41b | 2018-09-26 11:25:46 -0400 | [diff] [blame] | 44 | for(uint32_t i = 0; i < pCreateInfo->queueCreateInfoCount; i++) |
| 45 | { |
| 46 | const VkDeviceQueueCreateInfo& queueCreateInfo = pCreateInfo->pQueueCreateInfos[i]; |
Alexis Hetu | e70c351 | 2018-10-17 13:18:04 -0400 | [diff] [blame] | 47 | queueCount += queueCreateInfo.queueCount; |
Alexis Hetu | 767b41b | 2018-09-26 11:25:46 -0400 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | uint32_t queueID = 0; |
| 51 | for(uint32_t i = 0; i < pCreateInfo->queueCreateInfoCount; i++) |
| 52 | { |
| 53 | const VkDeviceQueueCreateInfo& queueCreateInfo = pCreateInfo->pQueueCreateInfos[i]; |
| 54 | |
| 55 | for(uint32_t j = 0; j < queueCreateInfo.queueCount; j++, queueID++) |
| 56 | { |
Alexis Hetu | e1f51b9 | 2019-04-23 15:34:34 -0400 | [diff] [blame] | 57 | new (&queues[queueID]) Queue(); |
Alexis Hetu | 767b41b | 2018-09-26 11:25:46 -0400 | [diff] [blame] | 58 | } |
| 59 | } |
Nicolas Capens | d689d1c | 2018-11-19 16:02:36 -0500 | [diff] [blame] | 60 | |
Alexis Hetu | 352791e | 2019-05-17 16:42:34 -0400 | [diff] [blame] | 61 | extensions = reinterpret_cast<ExtensionName*>(static_cast<uint8_t*>(mem) + (sizeof(Queue) * queueCount)); |
| 62 | for(uint32_t i = 0; i < enabledExtensionCount; i++) |
| 63 | { |
| 64 | strncpy(extensions[i], pCreateInfo->ppEnabledExtensionNames[i], VK_MAX_EXTENSION_NAME_SIZE); |
| 65 | } |
| 66 | |
Nicolas Capens | d689d1c | 2018-11-19 16:02:36 -0500 | [diff] [blame] | 67 | if(pCreateInfo->enabledLayerCount) |
| 68 | { |
| 69 | // "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] | 70 | 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] | 71 | } |
Alexis Hetu | 0da99f5 | 2019-02-27 12:54:52 -0500 | [diff] [blame] | 72 | |
Alexis Hetu | 1a9714a | 2019-04-12 11:48:12 -0400 | [diff] [blame] | 73 | // FIXME (b/119409619): use an allocator here so we can control all memory allocations |
Alexis Hetu | 0da99f5 | 2019-02-27 12:54:52 -0500 | [diff] [blame] | 74 | blitter = new sw::Blitter(); |
Alexis Hetu | 767b41b | 2018-09-26 11:25:46 -0400 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | void Device::destroy(const VkAllocationCallbacks* pAllocator) |
| 78 | { |
Alexis Hetu | af3c102 | 2018-12-12 13:26:15 -0500 | [diff] [blame] | 79 | for(uint32_t i = 0; i < queueCount; i++) |
| 80 | { |
Ben Clayton | 7e0a036 | 2019-05-20 11:32:35 +0100 | [diff] [blame] | 81 | queues[i].~Queue(); |
Alexis Hetu | af3c102 | 2018-12-12 13:26:15 -0500 | [diff] [blame] | 82 | } |
| 83 | |
Alexis Hetu | 767b41b | 2018-09-26 11:25:46 -0400 | [diff] [blame] | 84 | vk::deallocate(queues, pAllocator); |
Alexis Hetu | 0da99f5 | 2019-02-27 12:54:52 -0500 | [diff] [blame] | 85 | |
| 86 | delete blitter; |
Alexis Hetu | 767b41b | 2018-09-26 11:25:46 -0400 | [diff] [blame] | 87 | } |
| 88 | |
Nicolas Capens | 0c73680 | 2019-05-27 12:53:31 -0400 | [diff] [blame] | 89 | size_t Device::ComputeRequiredAllocationSize(const VkDeviceCreateInfo* pCreateInfo) |
Alexis Hetu | 767b41b | 2018-09-26 11:25:46 -0400 | [diff] [blame] | 90 | { |
| 91 | uint32_t queueCount = 0; |
Nicolas Capens | 0c73680 | 2019-05-27 12:53:31 -0400 | [diff] [blame] | 92 | for(uint32_t i = 0; i < pCreateInfo->queueCreateInfoCount; i++) |
Alexis Hetu | 767b41b | 2018-09-26 11:25:46 -0400 | [diff] [blame] | 93 | { |
Nicolas Capens | 0c73680 | 2019-05-27 12:53:31 -0400 | [diff] [blame] | 94 | queueCount += pCreateInfo->pQueueCreateInfos[i].queueCount; |
Alexis Hetu | 767b41b | 2018-09-26 11:25:46 -0400 | [diff] [blame] | 95 | } |
| 96 | |
Nicolas Capens | 0c73680 | 2019-05-27 12:53:31 -0400 | [diff] [blame] | 97 | return (sizeof(Queue) * queueCount) + (pCreateInfo->enabledExtensionCount * sizeof(ExtensionName)); |
Alexis Hetu | 352791e | 2019-05-17 16:42:34 -0400 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | bool Device::hasExtension(const char* extensionName) const |
| 101 | { |
| 102 | for(uint32_t i = 0; i < enabledExtensionCount; i++) |
| 103 | { |
| 104 | if(strncmp(extensions[i], extensionName, VK_MAX_EXTENSION_NAME_SIZE) == 0) |
| 105 | { |
| 106 | return true; |
| 107 | } |
| 108 | } |
| 109 | return false; |
Alexis Hetu | 767b41b | 2018-09-26 11:25:46 -0400 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | VkQueue Device::getQueue(uint32_t queueFamilyIndex, uint32_t queueIndex) const |
| 113 | { |
| 114 | ASSERT(queueFamilyIndex == 0); |
| 115 | |
| 116 | return queues[queueIndex]; |
| 117 | } |
| 118 | |
Alexis Hetu | e1f51b9 | 2019-04-23 15:34:34 -0400 | [diff] [blame] | 119 | 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] | 120 | { |
Ben Clayton | 6779e5e | 2019-05-20 11:07:58 +0100 | [diff] [blame] | 121 | 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] | 122 | const time_point start = now(); |
| 123 | const uint64_t max_timeout = (LLONG_MAX - start.time_since_epoch().count()); |
| 124 | bool infiniteTimeout = (timeout > max_timeout); |
| 125 | const time_point end_ns = start + std::chrono::nanoseconds(std::min(max_timeout, timeout)); |
| 126 | if(waitAll) // All fences must be signaled |
| 127 | { |
| 128 | for(uint32_t i = 0; i < fenceCount; i++) |
| 129 | { |
| 130 | if(timeout == 0) |
| 131 | { |
| 132 | if(Cast(pFences[i])->getStatus() != VK_SUCCESS) // At least one fence is not signaled |
| 133 | { |
| 134 | return VK_TIMEOUT; |
| 135 | } |
| 136 | } |
| 137 | else if(infiniteTimeout) |
| 138 | { |
| 139 | if(Cast(pFences[i])->wait() != VK_SUCCESS) // At least one fence is not signaled |
| 140 | { |
| 141 | return VK_TIMEOUT; |
| 142 | } |
| 143 | } |
| 144 | else |
| 145 | { |
Ben Clayton | 6779e5e | 2019-05-20 11:07:58 +0100 | [diff] [blame] | 146 | 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] | 147 | { |
| 148 | return VK_TIMEOUT; |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | return VK_SUCCESS; |
| 154 | } |
| 155 | else // At least one fence must be signaled |
| 156 | { |
| 157 | // Start by quickly checking the status of all fences, as only one is required |
| 158 | for(uint32_t i = 0; i < fenceCount; i++) |
| 159 | { |
| 160 | if(Cast(pFences[i])->getStatus() == VK_SUCCESS) // At least one fence is signaled |
| 161 | { |
| 162 | return VK_SUCCESS; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | if(timeout > 0) |
| 167 | { |
| 168 | for(uint32_t i = 0; i < fenceCount; i++) |
| 169 | { |
| 170 | if(infiniteTimeout) |
| 171 | { |
| 172 | if(Cast(pFences[i])->wait() == VK_SUCCESS) // At least one fence is signaled |
| 173 | { |
| 174 | return VK_SUCCESS; |
| 175 | } |
| 176 | } |
| 177 | else |
| 178 | { |
Ben Clayton | 6779e5e | 2019-05-20 11:07:58 +0100 | [diff] [blame] | 179 | 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] | 180 | { |
| 181 | return VK_SUCCESS; |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | return VK_TIMEOUT; |
| 188 | } |
Alexis Hetu | c4bd9df | 2018-12-07 11:28:40 -0500 | [diff] [blame] | 189 | } |
| 190 | |
Alexis Hetu | e1f51b9 | 2019-04-23 15:34:34 -0400 | [diff] [blame] | 191 | VkResult Device::waitIdle() |
Ben Clayton | 00424c1 | 2019-03-17 17:29:30 +0000 | [diff] [blame] | 192 | { |
| 193 | for(uint32_t i = 0; i < queueCount; i++) |
| 194 | { |
| 195 | queues[i].waitIdle(); |
| 196 | } |
Alexis Hetu | e1f51b9 | 2019-04-23 15:34:34 -0400 | [diff] [blame] | 197 | |
| 198 | return VK_SUCCESS; |
Alexis Hetu | cda0cf9 | 2019-01-24 15:48:55 -0500 | [diff] [blame] | 199 | } |
| 200 | |
Alexis Hetu | 9e4d040 | 2018-10-16 15:44:12 -0400 | [diff] [blame] | 201 | void Device::getDescriptorSetLayoutSupport(const VkDescriptorSetLayoutCreateInfo* pCreateInfo, |
| 202 | VkDescriptorSetLayoutSupport* pSupport) const |
| 203 | { |
| 204 | // Mark everything as unsupported |
| 205 | pSupport->supported = VK_FALSE; |
| 206 | } |
| 207 | |
Alexis Hetu | 048974f | 2019-02-15 15:28:37 -0500 | [diff] [blame] | 208 | void Device::updateDescriptorSets(uint32_t descriptorWriteCount, const VkWriteDescriptorSet* pDescriptorWrites, |
| 209 | uint32_t descriptorCopyCount, const VkCopyDescriptorSet* pDescriptorCopies) |
| 210 | { |
| 211 | for(uint32_t i = 0; i < descriptorWriteCount; i++) |
| 212 | { |
| 213 | DescriptorSetLayout::WriteDescriptorSet(pDescriptorWrites[i]); |
| 214 | } |
| 215 | |
| 216 | for(uint32_t i = 0; i < descriptorCopyCount; i++) |
| 217 | { |
| 218 | DescriptorSetLayout::CopyDescriptorSet(pDescriptorCopies[i]); |
| 219 | } |
| 220 | } |
| 221 | |
Alexis Hetu | 767b41b | 2018-09-26 11:25:46 -0400 | [diff] [blame] | 222 | } // namespace vk |