2019-07-12 04:15:25 +00:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
# include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_MATH_H
|
2022-03-30 06:54:07 +00:00
|
|
|
//# include <math.h>
|
2019-07-12 04:15:25 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "QF/cvar.h"
|
|
|
|
#include "QF/mathlib.h"
|
2022-09-26 04:04:56 +00:00
|
|
|
#include "QF/va.h"
|
2022-03-30 06:54:07 +00:00
|
|
|
|
2022-09-26 04:04:56 +00:00
|
|
|
#include "QF/Vulkan/debug.h"
|
2019-07-12 04:15:25 +00:00
|
|
|
#include "QF/Vulkan/device.h"
|
2020-02-17 11:29:35 +00:00
|
|
|
#include "QF/Vulkan/image.h"
|
2019-07-12 04:15:25 +00:00
|
|
|
#include "QF/Vulkan/instance.h"
|
|
|
|
#include "QF/Vulkan/swapchain.h"
|
|
|
|
|
|
|
|
#include "vid_vulkan.h"
|
|
|
|
|
|
|
|
qfv_swapchain_t *
|
|
|
|
QFV_CreateSwapchain (vulkan_ctx_t *ctx, VkSwapchainKHR old_swapchain)
|
|
|
|
{
|
|
|
|
qfv_instfuncs_t *ifuncs = ctx->instance->funcs;
|
|
|
|
qfv_devfuncs_t *dfuncs = ctx->device->funcs;
|
2019-07-23 03:37:47 +00:00
|
|
|
qfv_queue_t *queue = &ctx->device->queue;
|
2020-02-06 10:04:28 +00:00
|
|
|
VkPhysicalDevice physDev = ctx->device->physDev->dev;
|
2019-07-12 04:15:25 +00:00
|
|
|
|
|
|
|
VkBool32 supported;
|
2020-02-06 10:04:28 +00:00
|
|
|
ifuncs->vkGetPhysicalDeviceSurfaceSupportKHR (physDev,
|
2019-07-23 03:37:47 +00:00
|
|
|
queue->queueFamily,
|
2019-07-12 04:15:25 +00:00
|
|
|
ctx->surface,
|
|
|
|
&supported);
|
|
|
|
if (!supported) {
|
|
|
|
Sys_Error ("unsupported surface for swapchain");
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t numModes;
|
|
|
|
VkPresentModeKHR *modes;
|
2021-01-09 11:42:23 +00:00
|
|
|
VkPresentModeKHR useMode = VK_PRESENT_MODE_FIFO_KHR;
|
2020-02-06 10:04:28 +00:00
|
|
|
ifuncs->vkGetPhysicalDeviceSurfacePresentModesKHR (physDev,
|
2019-07-12 04:15:25 +00:00
|
|
|
ctx->surface,
|
|
|
|
&numModes, 0);
|
|
|
|
modes = alloca (numModes * sizeof (VkPresentModeKHR));
|
2020-02-06 10:04:28 +00:00
|
|
|
ifuncs->vkGetPhysicalDeviceSurfacePresentModesKHR (physDev,
|
2019-07-12 04:15:25 +00:00
|
|
|
ctx->surface,
|
|
|
|
&numModes, modes);
|
|
|
|
for (uint32_t i = 0; i < numModes; i++) {
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
if ((int) modes[i] == vulkan_presentation_mode) {
|
2019-07-12 04:15:25 +00:00
|
|
|
useMode = modes[i];
|
|
|
|
}
|
|
|
|
}
|
2021-03-29 10:58:00 +00:00
|
|
|
Sys_MaskPrintf (SYS_vulkan, "presentation mode: %d (%d)\n", useMode,
|
[cvar] Make cvars properly typed
This is an extremely extensive patch as it hits every cvar, and every
usage of the cvars. Cvars no longer store the value they control,
instead, they use a cexpr value object to reference the value and
specify the value's type (currently, a null type is used for strings).
Non-string cvars are passed through cexpr, allowing expressions in the
cvars' settings. Also, cvars have returned to an enhanced version of the
original (id quake) registration scheme.
As a minor benefit, relevant code having direct access to the
cvar-controlled variables is probably a slight optimization as it
removed a pointer dereference, and the variables can be located for data
locality.
The static cvar descriptors are made private as an additional safety
layer, though there's nothing stopping external modification via
Cvar_FindVar (which is needed for adding listeners).
While not used yet (partly due to working out the design), cvars can
have a validation function.
Registering a cvar allows a primary listener (and its data) to be
specified: it will always be called first when the cvar is modified. The
combination of proper listeners and direct access to the controlled
variable greatly simplifies the more complex cvar interactions as much
less null checking is required, and there's no need for one cvar's
callback to call another's.
nq-x11 is known to work at least well enough for the demos. More testing
will come.
2022-04-23 03:22:45 +00:00
|
|
|
vulkan_presentation_mode);
|
2019-07-12 04:15:25 +00:00
|
|
|
|
|
|
|
VkSurfaceCapabilitiesKHR surfCaps;
|
2020-02-06 10:04:28 +00:00
|
|
|
ifuncs->vkGetPhysicalDeviceSurfaceCapabilitiesKHR (physDev,
|
2019-07-12 04:15:25 +00:00
|
|
|
ctx->surface,
|
|
|
|
&surfCaps);
|
|
|
|
uint32_t numImages = surfCaps.minImageCount + 1;
|
|
|
|
if (surfCaps.maxImageCount > 0 && numImages > surfCaps.maxImageCount) {
|
|
|
|
numImages = surfCaps.maxImageCount;
|
|
|
|
}
|
|
|
|
|
2022-04-01 11:34:41 +00:00
|
|
|
VkExtent2D imageSize = {ctx->window_width, ctx->window_height};
|
2019-07-12 04:15:25 +00:00
|
|
|
if (surfCaps.currentExtent.width == ~0u) {
|
|
|
|
imageSize.width = bound (surfCaps.minImageExtent.width,
|
|
|
|
imageSize.width,
|
|
|
|
surfCaps.maxImageExtent.width);
|
|
|
|
imageSize.height = bound (surfCaps.minImageExtent.height,
|
|
|
|
imageSize.height,
|
|
|
|
surfCaps.maxImageExtent.height);
|
|
|
|
} else {
|
|
|
|
imageSize = surfCaps.currentExtent;
|
|
|
|
}
|
2022-05-09 13:57:41 +00:00
|
|
|
Sys_MaskPrintf (SYS_vulkan, "swapchain: %d [%d, %d]\n", numImages,
|
2019-07-12 04:15:25 +00:00
|
|
|
imageSize.width, imageSize.height);
|
|
|
|
|
|
|
|
VkImageUsageFlags imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT;
|
2021-03-24 10:20:53 +00:00
|
|
|
imageUsage |= VK_IMAGE_USAGE_TRANSFER_SRC_BIT;
|
2019-07-12 04:15:25 +00:00
|
|
|
imageUsage &= surfCaps.supportedUsageFlags;
|
2022-05-09 13:57:41 +00:00
|
|
|
Sys_MaskPrintf (SYS_vulkan, " usage:%x supported:%x\n", imageUsage,
|
2021-03-24 10:20:53 +00:00
|
|
|
surfCaps.supportedUsageFlags);
|
2019-07-12 04:15:25 +00:00
|
|
|
|
2020-02-17 11:29:35 +00:00
|
|
|
VkSurfaceTransformFlagBitsKHR surfTransform
|
|
|
|
= VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
|
2019-07-12 04:15:25 +00:00
|
|
|
|
|
|
|
uint32_t numFormats;
|
2020-02-06 10:04:28 +00:00
|
|
|
ifuncs->vkGetPhysicalDeviceSurfaceFormatsKHR (physDev,
|
2019-07-12 04:15:25 +00:00
|
|
|
ctx->surface,
|
|
|
|
&numFormats, 0);
|
|
|
|
VkSurfaceFormatKHR *formats = alloca (numFormats * sizeof (*formats));
|
2020-02-06 10:04:28 +00:00
|
|
|
ifuncs->vkGetPhysicalDeviceSurfaceFormatsKHR (physDev,
|
2019-07-12 04:15:25 +00:00
|
|
|
ctx->surface,
|
|
|
|
&numFormats, formats);
|
2022-11-25 03:57:56 +00:00
|
|
|
VkSurfaceFormatKHR useFormat = {VK_FORMAT_B8G8R8A8_UNORM,
|
2019-07-12 04:15:25 +00:00
|
|
|
VK_COLOR_SPACE_SRGB_NONLINEAR_KHR};
|
|
|
|
if (numFormats > 1) {
|
|
|
|
uint32_t i;
|
|
|
|
for (i = 0; i < numFormats; i++) {
|
|
|
|
if (formats[i].format == useFormat.format
|
|
|
|
&& formats[i].colorSpace == useFormat.colorSpace) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (i == numFormats) {
|
|
|
|
useFormat = formats[0];
|
|
|
|
}
|
|
|
|
} else if (numFormats == 1 && formats[0].format != VK_FORMAT_UNDEFINED) {
|
|
|
|
useFormat = formats[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
VkSwapchainCreateInfoKHR createInfo = {
|
|
|
|
VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR, 0, 0,
|
|
|
|
ctx->surface,
|
|
|
|
numImages,
|
|
|
|
useFormat.format, useFormat.colorSpace,
|
|
|
|
imageSize,
|
|
|
|
1, // array layers
|
|
|
|
imageUsage,
|
|
|
|
VK_SHARING_MODE_EXCLUSIVE,
|
|
|
|
0, 0,
|
|
|
|
surfTransform,
|
|
|
|
VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR,
|
|
|
|
useMode,
|
|
|
|
VK_TRUE,
|
|
|
|
old_swapchain
|
|
|
|
};
|
|
|
|
|
2020-02-17 11:29:35 +00:00
|
|
|
VkDevice dev = ctx->device->dev;
|
2019-07-12 04:15:25 +00:00
|
|
|
VkSwapchainKHR swapchain;
|
2020-02-17 11:29:35 +00:00
|
|
|
dfuncs->vkCreateSwapchainKHR (dev, &createInfo, 0, &swapchain);
|
2019-07-12 04:15:25 +00:00
|
|
|
|
|
|
|
if (old_swapchain != swapchain) {
|
2020-02-17 11:29:35 +00:00
|
|
|
dfuncs->vkDestroySwapchainKHR (dev, old_swapchain, 0);
|
2019-07-12 04:15:25 +00:00
|
|
|
}
|
|
|
|
|
2020-02-17 11:29:35 +00:00
|
|
|
dfuncs->vkGetSwapchainImagesKHR (dev, swapchain, &numImages, 0);
|
|
|
|
qfv_swapchain_t *sc = malloc (sizeof (qfv_swapchain_t));
|
2020-02-07 02:45:05 +00:00
|
|
|
sc->device = ctx->device;
|
2019-07-12 04:15:25 +00:00
|
|
|
sc->surface = ctx->surface;
|
|
|
|
sc->swapchain = swapchain;
|
2020-02-17 11:29:35 +00:00
|
|
|
sc->format = useFormat.format;
|
|
|
|
sc->extent = imageSize;
|
2019-07-12 04:15:25 +00:00
|
|
|
sc->numImages = numImages;
|
2021-03-24 10:20:53 +00:00
|
|
|
sc->usage = imageUsage;
|
2020-02-17 11:29:35 +00:00
|
|
|
sc->images = DARRAY_ALLOCFIXED (qfv_imageset_t, numImages, malloc);
|
|
|
|
sc->imageViews = DARRAY_ALLOCFIXED (qfv_imageviewset_t, numImages, malloc);
|
|
|
|
dfuncs->vkGetSwapchainImagesKHR (dev, swapchain, &numImages, sc->images->a);
|
|
|
|
for (uint32_t i = 0; i < numImages; i++) {
|
|
|
|
sc->imageViews->a[i]
|
|
|
|
= QFV_CreateImageView (ctx->device, sc->images->a[i],
|
|
|
|
VK_IMAGE_VIEW_TYPE_2D, sc->format,
|
|
|
|
VK_IMAGE_ASPECT_COLOR_BIT);
|
|
|
|
}
|
2019-07-12 04:15:25 +00:00
|
|
|
return sc;
|
|
|
|
}
|
2019-07-12 15:36:21 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
QFV_DestroySwapchain (qfv_swapchain_t *swapchain)
|
|
|
|
{
|
2020-02-07 02:45:05 +00:00
|
|
|
qfv_device_t *device = swapchain->device;
|
|
|
|
VkDevice dev = device->dev;
|
|
|
|
qfv_devfuncs_t *dfunc = device->funcs;
|
2020-02-17 11:29:35 +00:00
|
|
|
for (size_t i = 0; i < swapchain->imageViews->size; i++) {
|
|
|
|
dfunc->vkDestroyImageView (dev, swapchain->imageViews->a[i], 0);
|
|
|
|
}
|
|
|
|
free (swapchain->images);
|
|
|
|
free (swapchain->imageViews);
|
2020-02-07 02:45:05 +00:00
|
|
|
dfunc->vkDestroySwapchainKHR (dev, swapchain->swapchain, 0);
|
2019-07-12 15:36:21 +00:00
|
|
|
free (swapchain);
|
|
|
|
}
|
2020-02-14 02:11:27 +00:00
|
|
|
|
|
|
|
int
|
2020-02-17 11:29:35 +00:00
|
|
|
QFV_AcquireNextImage (qfv_swapchain_t *swapchain, VkSemaphore semaphore,
|
|
|
|
VkFence fence, uint32_t *imageIndex)
|
2020-02-14 02:11:27 +00:00
|
|
|
{
|
|
|
|
qfv_device_t *device = swapchain->device;
|
|
|
|
VkDevice dev = device->dev;
|
|
|
|
qfv_devfuncs_t *dfunc = device->funcs;
|
|
|
|
uint64_t timeout = 2000000000;
|
|
|
|
*imageIndex = ~0u;
|
|
|
|
VkResult res = dfunc->vkAcquireNextImageKHR (dev, swapchain->swapchain,
|
2020-02-17 11:29:35 +00:00
|
|
|
timeout, semaphore, fence,
|
2020-02-14 02:11:27 +00:00
|
|
|
imageIndex);
|
|
|
|
switch (res) {
|
|
|
|
case VK_SUCCESS:
|
|
|
|
case VK_TIMEOUT:
|
|
|
|
case VK_NOT_READY:
|
|
|
|
return 1;
|
|
|
|
case VK_SUBOPTIMAL_KHR:
|
|
|
|
case VK_ERROR_OUT_OF_DATE_KHR:
|
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
Sys_Error ("vkAcquireNextImageKHR failed: %d", res);
|
|
|
|
}
|
|
|
|
}
|