2022-05-04 04:59:38 +00:00
|
|
|
/*
|
|
|
|
resource.c
|
|
|
|
|
|
|
|
Vulkan resource functions
|
|
|
|
|
|
|
|
Copyright (C) 2022 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-03-09 01:19:24 +00:00
|
|
|
#include "QF/mathlib.h"
|
2022-05-04 04:59:38 +00:00
|
|
|
#include "QF/va.h"
|
|
|
|
|
|
|
|
#include "QF/Vulkan/buffer.h"
|
|
|
|
#include "QF/Vulkan/debug.h"
|
|
|
|
#include "QF/Vulkan/device.h"
|
|
|
|
#include "QF/Vulkan/image.h"
|
|
|
|
#include "QF/Vulkan/instance.h"
|
|
|
|
#include "QF/Vulkan/resource.h"
|
|
|
|
|
2022-10-27 04:26:00 +00:00
|
|
|
static void
|
|
|
|
create_image (qfv_device_t *device, qfv_resobj_t *image_obj)
|
|
|
|
{
|
|
|
|
qfv_devfuncs_t *dfunc = device->funcs;
|
|
|
|
__auto_type image = &image_obj->image;
|
2023-02-19 03:25:13 +00:00
|
|
|
if (image->image) {
|
|
|
|
// the image was created externally and is being
|
|
|
|
return;
|
|
|
|
}
|
2022-10-27 04:26:00 +00:00
|
|
|
VkImageCreateInfo createInfo = {
|
|
|
|
VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO, 0,
|
2023-02-19 03:25:13 +00:00
|
|
|
.flags = image->flags,
|
|
|
|
.imageType = image->type,
|
|
|
|
.format = image->format,
|
|
|
|
.extent = image->extent,
|
|
|
|
.mipLevels = image->num_mipmaps,
|
|
|
|
.arrayLayers = image->num_layers,
|
|
|
|
.samples = image->samples,
|
|
|
|
.tiling = image->tiling,
|
|
|
|
.usage = image->usage,
|
|
|
|
.sharingMode = image->sharing,
|
|
|
|
.queueFamilyIndexCount = image->num_queue_inds,
|
|
|
|
.pQueueFamilyIndices = image->queue_inds,
|
|
|
|
.initialLayout = image->initialLayout,
|
2022-10-27 04:26:00 +00:00
|
|
|
};
|
|
|
|
dfunc->vkCreateImage (device->dev, &createInfo, 0, &image->image);
|
|
|
|
}
|
|
|
|
|
2022-11-20 06:31:23 +00:00
|
|
|
static void
|
|
|
|
create_image_view (qfv_device_t *device, qfv_resobj_t *imgview_obj,
|
|
|
|
qfv_resobj_t *imgobj)
|
|
|
|
{
|
|
|
|
qfv_devfuncs_t *dfunc = device->funcs;
|
|
|
|
__auto_type view = &imgview_obj->image_view;
|
2023-02-19 03:25:13 +00:00
|
|
|
VkImage image = view->external_image ? view->external_image
|
|
|
|
: imgobj->image.image;
|
2022-11-20 06:31:23 +00:00
|
|
|
|
|
|
|
VkImageViewCreateInfo createInfo = {
|
|
|
|
VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, 0,
|
2023-02-19 03:25:13 +00:00
|
|
|
.flags = view->flags,
|
|
|
|
.image = image,
|
2022-11-20 06:31:23 +00:00
|
|
|
.viewType = view->type,
|
|
|
|
.format = view->format,
|
|
|
|
.components = view->components,
|
2023-02-19 03:25:13 +00:00
|
|
|
.subresourceRange = view->subresourceRange,
|
2022-11-20 06:31:23 +00:00
|
|
|
};
|
|
|
|
dfunc->vkCreateImageView (device->dev, &createInfo, 0, &view->view);
|
|
|
|
}
|
|
|
|
|
2022-05-04 04:59:38 +00:00
|
|
|
int
|
|
|
|
QFV_CreateResource (qfv_device_t *device, qfv_resource_t *resource)
|
|
|
|
{
|
|
|
|
qfv_devfuncs_t *dfunc = device->funcs;
|
|
|
|
qfv_physdev_t *physdev = device->physDev;
|
2023-03-09 01:19:24 +00:00
|
|
|
size_t atom = physdev->properties->limits.nonCoherentAtomSize;
|
2022-05-04 04:59:38 +00:00
|
|
|
VkPhysicalDeviceMemoryProperties *memprops = &physdev->memory_properties;
|
|
|
|
VkMemoryRequirements req;
|
|
|
|
VkDeviceSize size = 0;
|
|
|
|
|
2023-03-09 01:19:24 +00:00
|
|
|
if (!(resource->memory_properties
|
|
|
|
& (VK_MEMORY_PROPERTY_HOST_CACHED_BIT
|
|
|
|
| VK_MEMORY_PROPERTY_HOST_COHERENT_BIT
|
|
|
|
| VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT))) {
|
|
|
|
// if the memory isn't host visible then there's no need to worry about
|
|
|
|
// alignment with nonCoherentAtomSize
|
|
|
|
atom = 0;
|
|
|
|
}
|
2022-05-04 04:59:38 +00:00
|
|
|
for (unsigned i = 0; i < resource->num_objects; i++) {
|
|
|
|
__auto_type obj = &resource->objects[i];
|
|
|
|
switch (obj->type) {
|
|
|
|
case qfv_res_buffer:
|
|
|
|
{
|
|
|
|
__auto_type buffer = &obj->buffer;
|
|
|
|
buffer->buffer = QFV_CreateBuffer (device,
|
|
|
|
buffer->size,
|
|
|
|
buffer->usage);
|
|
|
|
QFV_duSetObjectName (device, VK_OBJECT_TYPE_BUFFER,
|
2022-10-02 06:07:04 +00:00
|
|
|
buffer->buffer,
|
|
|
|
va (resource->va_ctx, "buffer:%s:%s",
|
|
|
|
resource->name, obj->name));
|
2022-05-04 04:59:38 +00:00
|
|
|
dfunc->vkGetBufferMemoryRequirements (device->dev,
|
|
|
|
buffer->buffer, &req);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case qfv_res_buffer_view:
|
|
|
|
{
|
|
|
|
__auto_type buffview = &obj->buffer_view;
|
|
|
|
__auto_type buffobj = &resource->objects[buffview->buffer];
|
|
|
|
if (buffview->buffer >= resource->num_objects
|
|
|
|
|| buffobj->type != qfv_res_buffer) {
|
|
|
|
Sys_Error ("%s:%s invalid buffer for view",
|
|
|
|
resource->name, obj->name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case qfv_res_image:
|
|
|
|
{
|
2022-10-27 04:26:00 +00:00
|
|
|
create_image (device, obj);
|
2022-05-04 04:59:38 +00:00
|
|
|
__auto_type image = &obj->image;
|
|
|
|
QFV_duSetObjectName (device, VK_OBJECT_TYPE_IMAGE,
|
2022-10-02 06:07:04 +00:00
|
|
|
image->image,
|
|
|
|
va (resource->va_ctx, "image:%s:%s",
|
|
|
|
resource->name, obj->name));
|
2022-05-04 04:59:38 +00:00
|
|
|
dfunc->vkGetImageMemoryRequirements (device->dev,
|
|
|
|
image->image, &req);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case qfv_res_image_view:
|
|
|
|
{
|
|
|
|
__auto_type imgview = &obj->image_view;
|
|
|
|
__auto_type imgobj = &resource->objects[imgview->image];
|
2023-02-19 03:25:13 +00:00
|
|
|
if (!imgview->external_image
|
|
|
|
&& (imgview->image >= resource->num_objects
|
|
|
|
|| imgobj->type != qfv_res_image)) {
|
2022-05-04 04:59:38 +00:00
|
|
|
Sys_Error ("%s:%s invalid image for view",
|
|
|
|
resource->name, obj->name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
Sys_Error ("%s:%s invalid resource type %d",
|
|
|
|
resource->name, obj->name, obj->type);
|
|
|
|
}
|
2023-03-09 01:19:24 +00:00
|
|
|
req.alignment = max (req.alignment, atom);
|
2022-05-04 04:59:38 +00:00
|
|
|
size = QFV_NextOffset (size, &req);
|
2022-10-14 10:36:21 +00:00
|
|
|
size += QFV_NextOffset (req.size, &req);
|
2022-05-04 04:59:38 +00:00
|
|
|
}
|
|
|
|
VkMemoryPropertyFlags properties = resource->memory_properties;
|
|
|
|
for (uint32_t type = 0; type < memprops->memoryTypeCount; type++) {
|
|
|
|
if ((req.memoryTypeBits & (1 << type))
|
|
|
|
&& ((memprops->memoryTypes[type].propertyFlags & properties)
|
|
|
|
== properties)) {
|
|
|
|
VkMemoryAllocateInfo allocate_info = {
|
|
|
|
.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO,
|
|
|
|
.allocationSize = size,
|
|
|
|
.memoryTypeIndex = type,
|
|
|
|
};
|
|
|
|
VkResult res = dfunc->vkAllocateMemory (device->dev, &allocate_info,
|
|
|
|
0, &resource->memory);
|
|
|
|
if (res == VK_SUCCESS) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-10-27 02:32:00 +00:00
|
|
|
resource->size = size;
|
2022-05-04 04:59:38 +00:00
|
|
|
QFV_duSetObjectName (device, VK_OBJECT_TYPE_DEVICE_MEMORY,
|
|
|
|
resource->memory, va (resource->va_ctx, "memory:%s",
|
|
|
|
resource->name));
|
|
|
|
|
|
|
|
VkDeviceSize offset = 0;
|
|
|
|
for (unsigned i = 0; i < resource->num_objects; i++) {
|
|
|
|
__auto_type obj = &resource->objects[i];
|
|
|
|
switch (obj->type) {
|
|
|
|
case qfv_res_buffer:
|
|
|
|
{
|
|
|
|
__auto_type buffer = &obj->buffer;
|
|
|
|
dfunc->vkGetBufferMemoryRequirements (device->dev,
|
|
|
|
buffer->buffer, &req);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case qfv_res_image:
|
|
|
|
{
|
|
|
|
__auto_type image = &obj->image;
|
|
|
|
dfunc->vkGetImageMemoryRequirements (device->dev,
|
|
|
|
image->image, &req);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case qfv_res_buffer_view:
|
|
|
|
case qfv_res_image_view:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-03-09 01:19:24 +00:00
|
|
|
req.alignment = max (req.alignment, atom);
|
2022-05-04 04:59:38 +00:00
|
|
|
offset = QFV_NextOffset (offset, &req);
|
|
|
|
switch (obj->type) {
|
|
|
|
case qfv_res_buffer:
|
|
|
|
{
|
|
|
|
__auto_type buffer = &obj->buffer;
|
|
|
|
QFV_BindBufferMemory (device, buffer->buffer,
|
|
|
|
resource->memory, offset);
|
2022-10-03 00:14:29 +00:00
|
|
|
buffer->offset = offset;
|
2022-05-04 04:59:38 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case qfv_res_image:
|
|
|
|
{
|
|
|
|
__auto_type image = &obj->image;
|
|
|
|
QFV_BindImageMemory (device, image->image,
|
|
|
|
resource->memory, offset);
|
2022-10-03 00:14:29 +00:00
|
|
|
image->offset = offset;
|
2022-05-04 04:59:38 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case qfv_res_buffer_view:
|
|
|
|
case qfv_res_image_view:
|
|
|
|
break;
|
|
|
|
}
|
2023-03-09 01:19:24 +00:00
|
|
|
req.alignment = max (req.alignment, atom);
|
2022-10-14 10:36:21 +00:00
|
|
|
offset = QFV_NextOffset (offset, &req);
|
|
|
|
offset += QFV_NextOffset (req.size, &req);
|
2022-05-04 04:59:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < resource->num_objects; i++) {
|
|
|
|
__auto_type obj = &resource->objects[i];
|
|
|
|
switch (obj->type) {
|
|
|
|
case qfv_res_buffer:
|
|
|
|
case qfv_res_image:
|
|
|
|
break;
|
|
|
|
case qfv_res_buffer_view:
|
|
|
|
{
|
|
|
|
__auto_type buffview = &obj->buffer_view;
|
|
|
|
__auto_type buffobj = &resource->objects[buffview->buffer];
|
|
|
|
__auto_type buffer = &buffobj->buffer;
|
|
|
|
buffview->view = QFV_CreateBufferView (device,
|
|
|
|
buffer->buffer,
|
|
|
|
buffview->format,
|
|
|
|
buffview->offset,
|
|
|
|
buffview->size);
|
|
|
|
QFV_duSetObjectName (device, VK_OBJECT_TYPE_BUFFER_VIEW,
|
2022-10-02 06:07:04 +00:00
|
|
|
buffview->view,
|
|
|
|
va (resource->va_ctx, "bview:%s:%s",
|
|
|
|
resource->name, obj->name));
|
2022-05-04 04:59:38 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case qfv_res_image_view:
|
|
|
|
{
|
|
|
|
__auto_type imgview = &obj->image_view;
|
|
|
|
__auto_type imgobj = &resource->objects[imgview->image];
|
2022-11-20 06:31:23 +00:00
|
|
|
create_image_view (device, obj, imgobj);
|
2022-05-04 04:59:38 +00:00
|
|
|
QFV_duSetObjectName (device, VK_OBJECT_TYPE_IMAGE_VIEW,
|
2022-10-02 06:07:04 +00:00
|
|
|
imgview->view,
|
|
|
|
va (resource->va_ctx, "iview:%s:%s",
|
|
|
|
resource->name, obj->name));
|
2022-05-04 04:59:38 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QFV_DestroyResource (qfv_device_t *device, qfv_resource_t *resource)
|
|
|
|
{
|
|
|
|
qfv_devfuncs_t *dfunc = device->funcs;
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < resource->num_objects; i++) {
|
|
|
|
__auto_type obj = &resource->objects[i];
|
|
|
|
switch (obj->type) {
|
|
|
|
case qfv_res_buffer:
|
|
|
|
case qfv_res_image:
|
|
|
|
break;
|
|
|
|
case qfv_res_buffer_view:
|
|
|
|
{
|
|
|
|
__auto_type buffview = &obj->buffer_view;
|
|
|
|
dfunc->vkDestroyBufferView (device->dev, buffview->view, 0);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case qfv_res_image_view:
|
|
|
|
{
|
|
|
|
__auto_type imgview = &obj->image_view;
|
|
|
|
dfunc->vkDestroyImageView (device->dev, imgview->view, 0);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (unsigned i = 0; i < resource->num_objects; i++) {
|
|
|
|
__auto_type obj = &resource->objects[i];
|
|
|
|
switch (obj->type) {
|
|
|
|
case qfv_res_buffer:
|
|
|
|
{
|
|
|
|
__auto_type buffer = &obj->buffer;
|
|
|
|
dfunc->vkDestroyBuffer (device->dev, buffer->buffer, 0);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case qfv_res_image:
|
|
|
|
{
|
|
|
|
__auto_type image = &obj->image;
|
|
|
|
dfunc->vkDestroyImage (device->dev, image->image, 0);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case qfv_res_buffer_view:
|
|
|
|
case qfv_res_image_view:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dfunc->vkFreeMemory (device->dev, resource->memory, 0);
|
|
|
|
}
|
2022-10-02 06:24:38 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
QFV_ResourceInitTexImage (qfv_resobj_t *image, const char *name,
|
|
|
|
int mips, const tex_t *tex)
|
|
|
|
{
|
|
|
|
*image = (qfv_resobj_t) {
|
|
|
|
.name = name,
|
|
|
|
.type = qfv_res_image,
|
|
|
|
.image = {
|
|
|
|
.type = VK_IMAGE_TYPE_2D,
|
|
|
|
.format = QFV_ImageFormat (tex->format, 0),
|
|
|
|
.extent = {
|
|
|
|
.width = tex->width,
|
|
|
|
.height = tex->height,
|
|
|
|
.depth = 1,
|
|
|
|
},
|
|
|
|
.num_mipmaps = mips ? QFV_MipLevels (tex->width, tex->height) : 1,
|
|
|
|
.num_layers = 1,
|
|
|
|
.samples = VK_SAMPLE_COUNT_1_BIT,
|
|
|
|
.usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT
|
|
|
|
| VK_IMAGE_USAGE_TRANSFER_SRC_BIT
|
|
|
|
| VK_IMAGE_USAGE_SAMPLED_BIT,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|