1 /*------------------------------------------------------------------------
2 * Vulkan Conformance Tests
3 * ------------------------
5 * Copyright (c) 2016 The Khronos Group Inc.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 * \file vktSparseResourcesBase.cpp
21 * \brief Sparse Resources Base Instance
22 *//*--------------------------------------------------------------------*/
24 #include "vktSparseResourcesBase.hpp"
25 #include "vkMemUtil.hpp"
26 #include "vkRefUtil.hpp"
27 #include "vkTypeUtil.hpp"
28 #include "vkQueryUtil.hpp"
39 struct QueueFamilyQueuesCount
41 QueueFamilyQueuesCount() : queueCount(0u) {};
46 static const deUint32 NO_MATCH_FOUND = ~0u;
48 deUint32 findMatchingQueueFamilyIndex (const std::vector<vk::VkQueueFamilyProperties>& queueFamilyProperties,
49 const VkQueueFlags queueFlags,
50 const deUint32 startIndex)
52 for (deUint32 queueNdx = startIndex; queueNdx < queueFamilyProperties.size(); ++queueNdx)
54 if ((queueFamilyProperties[queueNdx].queueFlags & queueFlags) == queueFlags)
58 return NO_MATCH_FOUND;
63 void SparseResourcesBaseInstance::createDeviceSupportingQueues(const QueueRequirementsVec& queueRequirements)
65 typedef std::map<vk::VkQueueFlags, std::vector<Queue> > QueuesMap;
66 typedef std::map<deUint32, QueueFamilyQueuesCount> SelectedQueuesMap;
67 typedef std::map<deUint32, std::vector<float> > QueuePrioritiesMap;
69 const InstanceInterface& instance = m_context.getInstanceInterface();
70 const VkPhysicalDevice physicalDevice = m_context.getPhysicalDevice();
72 deUint32 queueFamilyPropertiesCount = 0u;
73 instance.getPhysicalDeviceQueueFamilyProperties(physicalDevice, &queueFamilyPropertiesCount, DE_NULL);
75 if(queueFamilyPropertiesCount == 0u)
76 TCU_THROW(ResourceError, "Device reports an empty set of queue family properties");
78 std::vector<VkQueueFamilyProperties> queueFamilyProperties;
79 queueFamilyProperties.resize(queueFamilyPropertiesCount);
81 instance.getPhysicalDeviceQueueFamilyProperties(physicalDevice, &queueFamilyPropertiesCount, &queueFamilyProperties[0]);
83 if (queueFamilyPropertiesCount == 0u)
84 TCU_THROW(ResourceError, "Device reports an empty set of queue family properties");
86 SelectedQueuesMap selectedQueueFamilies;
87 QueuePrioritiesMap queuePriorities;
89 for (deUint32 queueReqNdx = 0; queueReqNdx < queueRequirements.size(); ++queueReqNdx)
91 const QueueRequirements& queueRequirement = queueRequirements[queueReqNdx];
93 deUint32 queueFamilyIndex = 0u;
94 deUint32 queuesFoundCount = 0u;
98 queueFamilyIndex = findMatchingQueueFamilyIndex(queueFamilyProperties, queueRequirement.queueFlags, queueFamilyIndex);
100 if (queueFamilyIndex == NO_MATCH_FOUND)
101 TCU_THROW(NotSupportedError, "No match found for queue requirements");
103 const deUint32 queuesPerFamilyCount = deMin32(queueFamilyProperties[queueFamilyIndex].queueCount, queueRequirement.queueCount - queuesFoundCount);
105 selectedQueueFamilies[queueFamilyIndex].queueCount = deMax32(queuesPerFamilyCount, selectedQueueFamilies[queueFamilyIndex].queueCount);
107 for (deUint32 queueNdx = 0; queueNdx < queuesPerFamilyCount; ++queueNdx)
110 queue.queueFamilyIndex = queueFamilyIndex;
111 queue.queueIndex = queueNdx;
113 m_queues[queueRequirement.queueFlags].push_back(queue);
116 queuesFoundCount += queuesPerFamilyCount;
119 } while (queuesFoundCount < queueRequirement.queueCount);
122 std::vector<VkDeviceQueueCreateInfo> queueInfos;
124 for (SelectedQueuesMap::iterator queueFamilyIter = selectedQueueFamilies.begin(); queueFamilyIter != selectedQueueFamilies.end(); ++queueFamilyIter)
126 for (deUint32 queueNdx = 0; queueNdx < queueFamilyIter->second.queueCount; ++queueNdx)
127 queuePriorities[queueFamilyIter->first].push_back(1.0f);
129 const VkDeviceQueueCreateInfo queueInfo =
131 VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, // VkStructureType sType;
132 DE_NULL, // const void* pNext;
133 (VkDeviceQueueCreateFlags)0u, // VkDeviceQueueCreateFlags flags;
134 queueFamilyIter->first, // uint32_t queueFamilyIndex;
135 queueFamilyIter->second.queueCount, // uint32_t queueCount;
136 &queuePriorities[queueFamilyIter->first][0], // const float* pQueuePriorities;
139 queueInfos.push_back(queueInfo);
142 const VkPhysicalDeviceFeatures deviceFeatures = getPhysicalDeviceFeatures(instance, physicalDevice);
143 const VkDeviceCreateInfo deviceInfo =
145 VK_STRUCTURE_TYPE_DEVICE_CREATE_INFO, // VkStructureType sType;
146 DE_NULL, // const void* pNext;
147 (VkDeviceCreateFlags)0, // VkDeviceCreateFlags flags;
148 static_cast<deUint32>(queueInfos.size()), // uint32_t queueCreateInfoCount;
149 &queueInfos[0], // const VkDeviceQueueCreateInfo* pQueueCreateInfos;
150 0u, // uint32_t enabledLayerCount;
151 DE_NULL, // const char* const* ppEnabledLayerNames;
152 0u, // uint32_t enabledExtensionCount;
153 DE_NULL, // const char* const* ppEnabledExtensionNames;
154 &deviceFeatures, // const VkPhysicalDeviceFeatures* pEnabledFeatures;
157 m_logicalDevice = createDevice(instance, physicalDevice, &deviceInfo);
158 m_deviceDriver = de::MovePtr<DeviceDriver>(new DeviceDriver(instance, *m_logicalDevice));
159 m_allocator = de::MovePtr<Allocator>(new SimpleAllocator(*m_deviceDriver, *m_logicalDevice, getPhysicalDeviceMemoryProperties(instance, physicalDevice)));
161 for (QueuesMap::iterator queuesIter = m_queues.begin(); queuesIter != m_queues.end(); ++queuesIter)
163 for (deUint32 queueNdx = 0u; queueNdx < queuesIter->second.size(); ++queueNdx)
165 Queue& queue = queuesIter->second[queueNdx];
167 queue.queueHandle = getDeviceQueue(*m_deviceDriver, *m_logicalDevice, queue.queueFamilyIndex, queue.queueIndex);
172 const Queue& SparseResourcesBaseInstance::getQueue (const VkQueueFlags queueFlags, const deUint32 queueIndex) const
174 return m_queues.find(queueFlags)->second[queueIndex];