2019-07-23 03:28:57 +00:00
|
|
|
/*
|
|
|
|
vid_common_vulkan.c
|
|
|
|
|
|
|
|
Common Vulkan video driver functions
|
|
|
|
|
|
|
|
Copyright (C) 2019 Bill Currie <bill@taniwha.org>
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
|
|
|
See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to:
|
|
|
|
|
|
|
|
Free Software Foundation, Inc.
|
|
|
|
59 Temple Place - Suite 330
|
|
|
|
Boston, MA 02111-1307, USA
|
|
|
|
|
|
|
|
*/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
2023-06-22 08:00:10 +00:00
|
|
|
#include "QF/qtypes.h"
|
2019-07-23 03:28:57 +00:00
|
|
|
#include "QF/Vulkan/command.h"
|
|
|
|
#include "QF/Vulkan/device.h"
|
|
|
|
|
2023-06-22 08:00:10 +00:00
|
|
|
qfv_cmdpoolmgr_t *
|
|
|
|
QFV_CmdPoolManager_Init (qfv_cmdpoolmgr_t *manager, qfv_device_t *device)
|
|
|
|
{
|
|
|
|
*manager = (qfv_cmdpoolmgr_t) {
|
|
|
|
.primary = DARRAY_STATIC_INIT (16),
|
|
|
|
.secondary = DARRAY_STATIC_INIT (16),
|
|
|
|
.device = device,
|
|
|
|
};
|
|
|
|
auto dfunc = device->funcs;
|
|
|
|
|
|
|
|
VkCommandPoolCreateInfo poolCInfo = {
|
|
|
|
.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO,
|
|
|
|
.flags = VK_COMMAND_POOL_CREATE_TRANSIENT_BIT,
|
|
|
|
.queueFamilyIndex = device->queue.queueFamily,
|
|
|
|
};
|
|
|
|
dfunc->vkCreateCommandPool (device->dev, &poolCInfo, 0, &manager->pool);
|
|
|
|
return manager;
|
|
|
|
}
|
|
|
|
|
|
|
|
qfv_cmdpoolmgr_t *
|
|
|
|
QFV_CmdPoolManager_New (qfv_device_t *device)
|
|
|
|
{
|
|
|
|
return QFV_CmdPoolManager_Init (malloc (sizeof (qfv_cmdpoolmgr_t)), device);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QFV_CmdPoolManager_Shutdown (qfv_cmdpoolmgr_t *manager)
|
|
|
|
{
|
|
|
|
auto device = manager->device;
|
|
|
|
auto dfunc = device->funcs;
|
|
|
|
dfunc->vkDestroyCommandPool (device->dev, manager->pool, 0);
|
|
|
|
DARRAY_CLEAR (&manager->primary);
|
|
|
|
DARRAY_CLEAR (&manager->secondary);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QFV_CmdPoolManager_Delete (qfv_cmdpoolmgr_t *manager)
|
|
|
|
{
|
|
|
|
QFV_CmdPoolManager_Shutdown (manager);
|
|
|
|
free (manager);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QFV_CmdPoolManager_Reset (qfv_cmdpoolmgr_t *manager)
|
|
|
|
{
|
|
|
|
auto device = manager->device;
|
|
|
|
auto dfunc = device->funcs;
|
|
|
|
dfunc->vkResetCommandPool (device->dev, manager->pool, 0);
|
|
|
|
manager->active_primary = 0;
|
|
|
|
manager->active_secondary = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
VkCommandBuffer
|
|
|
|
QFV_CmdPoolManager_CmdBuffer (qfv_cmdpoolmgr_t *manager, bool secondary)
|
|
|
|
{
|
|
|
|
auto device = manager->device;
|
|
|
|
auto dfunc = device->funcs;
|
|
|
|
|
|
|
|
if (secondary) {
|
|
|
|
if (manager->active_secondary < manager->secondary.size) {
|
|
|
|
return manager->secondary.a[manager->active_secondary++];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (manager->active_primary < manager->primary.size) {
|
|
|
|
return manager->primary.a[manager->active_primary++];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
VkCommandBufferAllocateInfo cinfo = {
|
|
|
|
.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO,
|
|
|
|
.commandPool = manager->pool,
|
|
|
|
.level = secondary ? VK_COMMAND_BUFFER_LEVEL_SECONDARY
|
|
|
|
: VK_COMMAND_BUFFER_LEVEL_PRIMARY,
|
|
|
|
.commandBufferCount = 1,
|
|
|
|
};
|
|
|
|
VkCommandBuffer cmd;
|
|
|
|
dfunc->vkAllocateCommandBuffers (device->dev, &cinfo, &cmd);
|
|
|
|
if (secondary) {
|
|
|
|
DARRAY_APPEND (&manager->secondary, cmd);
|
2023-06-22 10:06:12 +00:00
|
|
|
manager->active_secondary++;
|
2023-06-22 08:00:10 +00:00
|
|
|
} else {
|
|
|
|
DARRAY_APPEND (&manager->primary, cmd);
|
2023-06-22 10:06:12 +00:00
|
|
|
manager->active_primary++;
|
2023-06-22 08:00:10 +00:00
|
|
|
}
|
|
|
|
return cmd;
|
|
|
|
}
|
|
|
|
|
2020-02-17 11:29:35 +00:00
|
|
|
VkCommandPool
|
2019-07-23 03:28:57 +00:00
|
|
|
QFV_CreateCommandPool (qfv_device_t *device, uint32_t queueFamily,
|
|
|
|
int transient, int reset)
|
|
|
|
{
|
|
|
|
VkDevice dev = device->dev;
|
|
|
|
qfv_devfuncs_t *dfunc = device->funcs;
|
|
|
|
uint32_t flags = 0;
|
|
|
|
if (transient) {
|
|
|
|
flags |= VK_COMMAND_POOL_CREATE_TRANSIENT_BIT;
|
|
|
|
}
|
|
|
|
if (reset) {
|
|
|
|
flags |= VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
|
|
|
|
}
|
|
|
|
VkCommandPoolCreateInfo createInfo = {
|
|
|
|
VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO, 0,
|
|
|
|
flags,
|
|
|
|
queueFamily
|
|
|
|
};
|
2020-02-17 11:29:35 +00:00
|
|
|
VkCommandPool pool;
|
|
|
|
dfunc->vkCreateCommandPool (dev, &createInfo, 0, &pool);
|
|
|
|
return pool;
|
2019-07-23 04:05:32 +00:00
|
|
|
}
|
|
|
|
|
2021-01-15 13:45:49 +00:00
|
|
|
int
|
2020-02-17 11:29:35 +00:00
|
|
|
QFV_AllocateCommandBuffers (qfv_device_t *device, VkCommandPool pool,
|
2021-01-15 13:45:49 +00:00
|
|
|
int secondary, qfv_cmdbufferset_t *bufferset)
|
2019-07-23 03:28:57 +00:00
|
|
|
{
|
2020-02-07 02:45:05 +00:00
|
|
|
VkDevice dev = device->dev;
|
|
|
|
qfv_devfuncs_t *dfunc = device->funcs;
|
2019-07-23 03:28:57 +00:00
|
|
|
uint32_t level = VK_COMMAND_BUFFER_LEVEL_PRIMARY;
|
|
|
|
if (secondary) {
|
|
|
|
level = VK_COMMAND_BUFFER_LEVEL_SECONDARY;
|
|
|
|
}
|
|
|
|
VkCommandBufferAllocateInfo allocInfo = {
|
|
|
|
VK_STRUCTURE_TYPE_COMMAND_BUFFER_ALLOCATE_INFO, 0,
|
2021-01-15 13:45:49 +00:00
|
|
|
pool, level, bufferset->size
|
2019-07-23 03:28:57 +00:00
|
|
|
};
|
2021-01-15 13:45:49 +00:00
|
|
|
int ret = dfunc->vkAllocateCommandBuffers (dev, &allocInfo, bufferset->a);
|
|
|
|
return ret == VK_SUCCESS;
|
2019-07-23 03:28:57 +00:00
|
|
|
}
|
|
|
|
|
2020-02-17 11:29:35 +00:00
|
|
|
VkSemaphore
|
2019-07-23 03:28:57 +00:00
|
|
|
QFV_CreateSemaphore (qfv_device_t *device)
|
|
|
|
{
|
|
|
|
VkDevice dev = device->dev;
|
|
|
|
qfv_devfuncs_t *dfunc = device->funcs;
|
|
|
|
|
|
|
|
VkSemaphoreCreateInfo createInfo = {
|
|
|
|
VK_STRUCTURE_TYPE_SEMAPHORE_CREATE_INFO, 0,
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
2020-02-17 11:29:35 +00:00
|
|
|
VkSemaphore semaphore;
|
|
|
|
dfunc->vkCreateSemaphore (dev, &createInfo, 0, &semaphore);
|
2019-07-23 03:28:57 +00:00
|
|
|
return semaphore;
|
|
|
|
}
|
|
|
|
|
2020-02-17 11:29:35 +00:00
|
|
|
VkFence
|
2019-07-23 03:28:57 +00:00
|
|
|
QFV_CreateFence (qfv_device_t *device, int signaled)
|
|
|
|
{
|
|
|
|
VkDevice dev = device->dev;
|
|
|
|
qfv_devfuncs_t *dfunc = device->funcs;
|
|
|
|
|
|
|
|
VkFenceCreateInfo createInfo = {
|
|
|
|
VK_STRUCTURE_TYPE_FENCE_CREATE_INFO, 0,
|
|
|
|
signaled ? VK_FENCE_CREATE_SIGNALED_BIT : 0,
|
|
|
|
};
|
|
|
|
|
2020-02-17 11:29:35 +00:00
|
|
|
VkFence fence;
|
|
|
|
dfunc->vkCreateFence (dev, &createInfo, 0, &fence);
|
2019-07-23 03:28:57 +00:00
|
|
|
return fence;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2019-07-23 03:37:47 +00:00
|
|
|
QFV_QueueSubmit (qfv_queue_t *queue, qfv_semaphoreset_t *waitSemaphores,
|
2020-02-17 11:29:35 +00:00
|
|
|
VkPipelineStageFlags *stages,
|
2019-07-23 03:28:57 +00:00
|
|
|
qfv_cmdbufferset_t *buffers,
|
2020-02-17 11:29:35 +00:00
|
|
|
qfv_semaphoreset_t *signalSemaphores, VkFence fence)
|
2019-07-23 03:28:57 +00:00
|
|
|
{
|
2020-02-07 02:45:05 +00:00
|
|
|
qfv_device_t *device = queue->device;
|
|
|
|
qfv_devfuncs_t *dfunc = device->funcs;
|
2019-07-23 03:28:57 +00:00
|
|
|
VkSubmitInfo submitInfo = {
|
|
|
|
VK_STRUCTURE_TYPE_SUBMIT_INFO, 0,
|
2020-02-17 11:29:35 +00:00
|
|
|
waitSemaphores->size, waitSemaphores->a, stages,
|
|
|
|
buffers->size, buffers->a,
|
|
|
|
signalSemaphores->size, signalSemaphores->a
|
2019-07-23 03:28:57 +00:00
|
|
|
};
|
|
|
|
//FIXME multi-batch
|
2020-02-17 11:29:35 +00:00
|
|
|
return dfunc->vkQueueSubmit (queue->queue, 1, &submitInfo, fence)
|
|
|
|
== VK_SUCCESS;
|
2019-07-23 03:28:57 +00:00
|
|
|
}
|
2019-07-23 04:05:32 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
QFV_QueueWaitIdle (qfv_queue_t *queue)
|
|
|
|
{
|
2020-02-07 02:45:05 +00:00
|
|
|
qfv_device_t *device = queue->device;
|
|
|
|
qfv_devfuncs_t *dfunc = device->funcs;
|
2019-07-23 04:05:32 +00:00
|
|
|
return dfunc->vkQueueWaitIdle (queue->queue) == VK_SUCCESS;
|
|
|
|
}
|
2021-12-17 05:54:02 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
QFV_PushConstants (qfv_device_t *device, VkCommandBuffer cmd,
|
|
|
|
VkPipelineLayout layout, uint32_t numPC,
|
|
|
|
const qfv_push_constants_t *constants)
|
|
|
|
{
|
|
|
|
qfv_devfuncs_t *dfunc = device->funcs;
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < numPC; i++) {
|
|
|
|
dfunc->vkCmdPushConstants (cmd, layout, constants[i].stageFlags,
|
|
|
|
constants[i].offset, constants[i].size,
|
|
|
|
constants[i].data);
|
|
|
|
}
|
|
|
|
}
|