Skip to content

Commit

Permalink
search for a queue on vkCreateDevice
Browse files Browse the repository at this point in the history
  • Loading branch information
DadSchoorse committed Jul 19, 2021
1 parent 9573b5d commit b2ad0b6
Showing 1 changed file with 32 additions and 74 deletions.
106 changes: 32 additions & 74 deletions src/basalt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ namespace vkBasalt
const VkAllocationCallbacks* pAllocator,
VkDevice* pDevice)
{
scoped_lock l(globalLock);
Logger::trace("vkCreateDevice");
VkLayerDeviceCreateInfo* layerCreateInfo = (VkLayerDeviceCreateInfo*) pCreateInfo->pNext;

Expand Down Expand Up @@ -232,101 +233,60 @@ namespace vkBasalt

fillDispatchTableDevice(*pDevice, gdpa, &pLogicalDevice->vkd);

// store the table by key
{
scoped_lock l(globalLock);
deviceMap[GetKey(*pDevice)] = pLogicalDevice;
}

return ret;
}

VK_LAYER_EXPORT void VKAPI_CALL vkBasalt_DestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator)
{
scoped_lock l(globalLock);

Logger::trace("vkDestroyDevice");

LogicalDevice* pLogicalDevice = deviceMap[GetKey(device)].get();
if (pLogicalDevice->commandPool != VK_NULL_HANDLE)
{
Logger::debug("DestroyCommandPool");
pLogicalDevice->vkd.DestroyCommandPool(device, pLogicalDevice->commandPool, pAllocator);
}

pLogicalDevice->vkd.DestroyDevice(device, pAllocator);

deviceMap.erase(GetKey(device));
}

static void saveDeviceQueue(LogicalDevice* pLogicalDevice, uint32_t queueFamilyIndex, VkQueue* pQueue)
{
if (pLogicalDevice->queue != VK_NULL_HANDLE)
{
return; // we allready have a queue
}

// Save the first graphic capable queue in our deviceMap
uint32_t count;
VkBool32 graphicsCapable = VK_FALSE;
// TODO also check if the queue is present capable

pLogicalDevice->vki.GetPhysicalDeviceQueueFamilyProperties(pLogicalDevice->physicalDevice, &count, nullptr);

std::vector<VkQueueFamilyProperties> queueProperties(count);

if (count > 0)
pLogicalDevice->vki.GetPhysicalDeviceQueueFamilyProperties(pLogicalDevice->physicalDevice, &count, queueProperties.data());
for (uint32_t i = 0; i < pCreateInfo->queueCreateInfoCount; i++)
{
pLogicalDevice->vki.GetPhysicalDeviceQueueFamilyProperties(pLogicalDevice->physicalDevice, &count, queueProperties.data());
if ((queueProperties[queueFamilyIndex].queueFlags & VK_QUEUE_GRAPHICS_BIT) != 0)
auto& queueInfo = pCreateInfo->pQueueCreateInfos[i];
if ((queueProperties[queueInfo.queueFamilyIndex].queueFlags & VK_QUEUE_GRAPHICS_BIT))
{
graphicsCapable = VK_TRUE;
}
}
else
{
// TODO
graphicsCapable = VK_TRUE;
}
pLogicalDevice->vkd.GetDeviceQueue(pLogicalDevice->device, queueInfo.queueFamilyIndex, 0, &pLogicalDevice->queue);

if (graphicsCapable)
{
VkCommandPoolCreateInfo commandPoolCreateInfo;
commandPoolCreateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
commandPoolCreateInfo.pNext = nullptr;
commandPoolCreateInfo.flags = 0;
commandPoolCreateInfo.queueFamilyIndex = queueFamilyIndex;
VkCommandPoolCreateInfo commandPoolCreateInfo;
commandPoolCreateInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
commandPoolCreateInfo.pNext = nullptr;
commandPoolCreateInfo.flags = 0;
commandPoolCreateInfo.queueFamilyIndex = queueInfo.queueFamilyIndex;

Logger::debug("found graphic capable queue");
pLogicalDevice->vkd.CreateCommandPool(pLogicalDevice->device, &commandPoolCreateInfo, nullptr, &pLogicalDevice->commandPool);
pLogicalDevice->queue = *pQueue;
pLogicalDevice->queueFamilyIndex = queueFamilyIndex;
}
}
Logger::debug("Found graphics capable queue");
pLogicalDevice->vkd.CreateCommandPool(pLogicalDevice->device, &commandPoolCreateInfo, nullptr, &pLogicalDevice->commandPool);
pLogicalDevice->queueFamilyIndex = queueInfo.queueFamilyIndex;

VKAPI_ATTR void VKAPI_CALL vkBasalt_GetDeviceQueue2(VkDevice device, const VkDeviceQueueInfo2* pQueueInfo, VkQueue* pQueue)
{
scoped_lock l(globalLock);
initializeDispatchTable(pLogicalDevice->queue, pLogicalDevice->device);

Logger::trace("vkGetDeviceQueue2");
break;
}
}

LogicalDevice* pLogicalDevice = deviceMap[GetKey(device)].get();
if (!pLogicalDevice->queue)
Logger::err("Did not find a graphics queue!");

pLogicalDevice->vkd.GetDeviceQueue2(device, pQueueInfo, pQueue);
deviceMap[GetKey(*pDevice)] = pLogicalDevice;

saveDeviceQueue(pLogicalDevice, pQueueInfo->queueFamilyIndex, pQueue);
return ret;
}

VKAPI_ATTR void VKAPI_CALL vkBasalt_GetDeviceQueue(VkDevice device, uint32_t queueFamilyIndex, uint32_t queueIndex, VkQueue* pQueue)
VK_LAYER_EXPORT void VKAPI_CALL vkBasalt_DestroyDevice(VkDevice device, const VkAllocationCallbacks* pAllocator)
{
scoped_lock l(globalLock);

Logger::trace("vkGetDeviceQueue");
Logger::trace("vkDestroyDevice");

LogicalDevice* pLogicalDevice = deviceMap[GetKey(device)].get();
if (pLogicalDevice->commandPool != VK_NULL_HANDLE)
{
Logger::debug("DestroyCommandPool");
pLogicalDevice->vkd.DestroyCommandPool(device, pLogicalDevice->commandPool, pAllocator);
}

pLogicalDevice->vkd.GetDeviceQueue(device, queueFamilyIndex, queueIndex, pQueue);
pLogicalDevice->vkd.DestroyDevice(device, pAllocator);

saveDeviceQueue(pLogicalDevice, queueFamilyIndex, pQueue);
deviceMap.erase(GetKey(device));
}

VKAPI_ATTR VkResult VKAPI_CALL vkBasalt_CreateSwapchainKHR(VkDevice device,
Expand Down Expand Up @@ -876,8 +836,6 @@ extern "C"
GETPROCADDR(EnumerateDeviceExtensionProperties); \
GETPROCADDR(CreateDevice); \
GETPROCADDR(DestroyDevice); \
GETPROCADDR(GetDeviceQueue); \
GETPROCADDR(GetDeviceQueue2); \
GETPROCADDR(CreateSwapchainKHR); \
GETPROCADDR(GetSwapchainImagesKHR); \
GETPROCADDR(QueuePresentKHR); \
Expand Down

0 comments on commit b2ad0b6

Please sign in to comment.