2019-07-06 05:42:53 +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
|
|
|
|
|
|
|
|
#ifdef HAVE_MATH_H
|
|
|
|
# include <math.h>
|
|
|
|
#endif
|
2022-05-08 08:57:40 +00:00
|
|
|
|
|
|
|
#include <string.h>
|
2019-07-06 05:42:53 +00:00
|
|
|
|
2020-12-21 09:38:31 +00:00
|
|
|
#include "QF/cexpr.h"
|
|
|
|
#include "QF/cmem.h"
|
2019-07-06 05:42:53 +00:00
|
|
|
#include "QF/cvar.h"
|
|
|
|
#include "QF/dstring.h"
|
2021-01-05 08:48:00 +00:00
|
|
|
#include "QF/hash.h"
|
2022-05-31 01:41:19 +00:00
|
|
|
#include "QF/heapsort.h"
|
2021-03-21 07:13:03 +00:00
|
|
|
#include "QF/plist.h"
|
2019-07-06 05:42:53 +00:00
|
|
|
#include "QF/va.h"
|
2022-05-07 05:12:02 +00:00
|
|
|
#include "QF/scene/entity.h"
|
2021-03-24 10:20:53 +00:00
|
|
|
#include "QF/Vulkan/capture.h"
|
2022-05-31 01:41:19 +00:00
|
|
|
#include "QF/Vulkan/command.h"
|
2021-01-31 10:58:55 +00:00
|
|
|
#include "QF/Vulkan/debug.h"
|
2019-07-12 04:15:25 +00:00
|
|
|
#include "QF/Vulkan/device.h"
|
|
|
|
#include "QF/Vulkan/instance.h"
|
2021-01-10 16:24:15 +00:00
|
|
|
#include "QF/Vulkan/staging.h"
|
2019-07-12 05:09:12 +00:00
|
|
|
#include "QF/Vulkan/swapchain.h"
|
2019-07-06 05:42:53 +00:00
|
|
|
|
2021-12-02 12:58:29 +00:00
|
|
|
#include "QF/Vulkan/qf_lighting.h"
|
|
|
|
#include "QF/Vulkan/qf_main.h"
|
2022-11-22 08:47:36 +00:00
|
|
|
#include "QF/Vulkan/qf_output.h"
|
2022-05-30 03:10:00 +00:00
|
|
|
#include "QF/Vulkan/qf_renderpass.h"
|
2022-05-08 08:57:40 +00:00
|
|
|
#include "QF/Vulkan/qf_vid.h"
|
2021-12-02 12:58:29 +00:00
|
|
|
|
2019-07-06 05:42:53 +00:00
|
|
|
#include "r_internal.h"
|
2019-07-08 16:00:47 +00:00
|
|
|
#include "vid_vulkan.h"
|
2020-12-21 09:38:31 +00:00
|
|
|
#include "vkparse.h"
|
2019-07-06 05:42:53 +00:00
|
|
|
|
2019-07-06 16:28:05 +00:00
|
|
|
static const char *instance_extensions[] = {
|
2019-07-07 07:27:55 +00:00
|
|
|
VK_KHR_SURFACE_EXTENSION_NAME,
|
2021-01-21 03:39:08 +00:00
|
|
|
VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME,
|
2019-07-06 05:42:53 +00:00
|
|
|
0,
|
|
|
|
};
|
|
|
|
|
2019-07-06 16:28:05 +00:00
|
|
|
static const char *device_extensions[] = {
|
2019-07-09 07:33:44 +00:00
|
|
|
VK_KHR_SWAPCHAIN_EXTENSION_NAME,
|
2019-07-06 16:28:05 +00:00
|
|
|
0,
|
|
|
|
};
|
|
|
|
|
2019-07-09 07:33:44 +00:00
|
|
|
void
|
|
|
|
Vulkan_Init_Common (vulkan_ctx_t *ctx)
|
|
|
|
{
|
2021-03-29 10:58:00 +00:00
|
|
|
Sys_MaskPrintf (SYS_vulkan, "Vulkan_Init_Common\n");
|
2021-02-14 02:35:06 +00:00
|
|
|
|
2019-07-09 07:33:44 +00:00
|
|
|
Vulkan_Init_Cvars ();
|
2022-11-24 14:44:07 +00:00
|
|
|
R_Init_Cvars ();
|
|
|
|
Vulkan_Script_Init (ctx);
|
2021-03-23 04:04:22 +00:00
|
|
|
ctx->instance = QFV_CreateInstance (ctx, PACKAGE_STRING, 0x000702ff, 0,
|
|
|
|
instance_extensions);//FIXME version
|
2021-12-19 00:14:11 +00:00
|
|
|
DARRAY_INIT (&ctx->renderPasses, 4);
|
2019-07-09 07:33:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Vulkan_Shutdown_Common (vulkan_ctx_t *ctx)
|
2019-07-06 16:28:05 +00:00
|
|
|
{
|
2021-03-24 10:20:53 +00:00
|
|
|
if (ctx->capture) {
|
|
|
|
QFV_DestroyCapture (ctx->capture);
|
|
|
|
}
|
2021-02-05 01:22:32 +00:00
|
|
|
if (ctx->frames.size) {
|
2021-02-14 02:35:06 +00:00
|
|
|
Vulkan_DestroyFrames (ctx);
|
2020-02-18 08:18:37 +00:00
|
|
|
}
|
2019-07-12 16:14:21 +00:00
|
|
|
if (ctx->swapchain) {
|
|
|
|
QFV_DestroySwapchain (ctx->swapchain);
|
|
|
|
}
|
2021-01-05 10:55:17 +00:00
|
|
|
ctx->instance->funcs->vkDestroySurfaceKHR (ctx->instance->instance,
|
|
|
|
ctx->surface, 0);
|
2022-11-24 14:44:07 +00:00
|
|
|
Vulkan_Script_Shutdown (ctx);
|
2019-07-12 16:14:21 +00:00
|
|
|
if (ctx->device) {
|
|
|
|
QFV_DestroyDevice (ctx->device);
|
|
|
|
}
|
|
|
|
if (ctx->instance) {
|
|
|
|
QFV_DestroyInstance (ctx->instance);
|
|
|
|
}
|
2019-07-12 04:15:25 +00:00
|
|
|
ctx->instance = 0;
|
2019-07-09 07:33:44 +00:00
|
|
|
ctx->unload_vulkan (ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Vulkan_CreateDevice (vulkan_ctx_t *ctx)
|
|
|
|
{
|
2019-07-12 04:15:25 +00:00
|
|
|
ctx->device = QFV_CreateDevice (ctx, device_extensions);
|
2021-02-14 02:35:06 +00:00
|
|
|
|
|
|
|
//FIXME msaa and deferred rendering...
|
|
|
|
//also, location
|
|
|
|
ctx->msaaSamples = 1;
|
[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
|
|
|
/*ctx->msaaSamples = min ((VkSampleCountFlagBits) msaaSamples,
|
2021-02-14 02:35:06 +00:00
|
|
|
QFV_GetMaxSampleCount (device->physDev));
|
|
|
|
if (ctx->msaaSamples > 1) {
|
|
|
|
name = "renderpass_msaa";
|
|
|
|
}*/
|
2019-07-06 05:42:53 +00:00
|
|
|
}
|
2019-07-10 15:58:14 +00:00
|
|
|
|
2021-01-10 16:24:15 +00:00
|
|
|
void
|
|
|
|
Vulkan_CreateStagingBuffers (vulkan_ctx_t *ctx)
|
|
|
|
{
|
2021-08-02 06:12:29 +00:00
|
|
|
// FIXME configurable?
|
2021-01-31 10:58:55 +00:00
|
|
|
ctx->staging = QFV_CreateStagingBuffer (ctx->device, "vulkan_ctx",
|
2021-08-02 06:12:29 +00:00
|
|
|
32*1024*1024, ctx->cmdpool);
|
2021-01-10 16:24:15 +00:00
|
|
|
}
|
|
|
|
|
2019-07-10 15:58:14 +00:00
|
|
|
void
|
|
|
|
Vulkan_CreateSwapchain (vulkan_ctx_t *ctx)
|
|
|
|
{
|
2019-07-12 05:09:12 +00:00
|
|
|
VkSwapchainKHR old_swapchain = 0;
|
|
|
|
if (ctx->swapchain) {
|
2022-09-26 04:04:56 +00:00
|
|
|
//FIXME this shouldn't be here
|
|
|
|
qfv_device_t *device = ctx->swapchain->device;
|
|
|
|
VkDevice dev = device->dev;
|
|
|
|
qfv_devfuncs_t *dfunc = device->funcs;
|
2019-07-12 05:09:12 +00:00
|
|
|
old_swapchain = ctx->swapchain->swapchain;
|
2022-09-26 04:04:56 +00:00
|
|
|
for (size_t i = 0; i < ctx->swapchain->imageViews->size; i++) {
|
|
|
|
dfunc->vkDestroyImageView(dev, ctx->swapchain->imageViews->a[i], 0);
|
|
|
|
}
|
|
|
|
free (ctx->swapchain->images);
|
|
|
|
free (ctx->swapchain->imageViews);
|
2019-07-12 05:09:12 +00:00
|
|
|
free (ctx->swapchain);
|
|
|
|
}
|
|
|
|
ctx->swapchain = QFV_CreateSwapchain (ctx, old_swapchain);
|
2019-07-10 15:58:14 +00:00
|
|
|
}
|
2020-02-18 05:28:28 +00:00
|
|
|
|
2022-05-31 01:41:19 +00:00
|
|
|
static int
|
|
|
|
renderpass_cmp (const void *_a, const void *_b)
|
2021-12-02 12:58:29 +00:00
|
|
|
{
|
2022-11-21 08:29:26 +00:00
|
|
|
__auto_type a = (const qfv_renderpass_t **) _a;
|
|
|
|
__auto_type b = (const qfv_renderpass_t **) _b;
|
|
|
|
return (*a)->order - (*b)->order;
|
2021-12-02 12:58:29 +00:00
|
|
|
}
|
|
|
|
|
2020-02-18 05:28:28 +00:00
|
|
|
void
|
2022-05-30 06:58:22 +00:00
|
|
|
Vulkan_CreateRenderPasses (vulkan_ctx_t *ctx)
|
2020-02-18 05:28:28 +00:00
|
|
|
{
|
2022-11-22 08:47:36 +00:00
|
|
|
Vulkan_Output_CreateRenderPasses (ctx);
|
2022-05-31 01:41:19 +00:00
|
|
|
Vulkan_Main_CreateRenderPasses (ctx);
|
2022-05-31 03:43:04 +00:00
|
|
|
Vulkan_Lighting_CreateRenderPasses (ctx);
|
2022-05-31 01:41:19 +00:00
|
|
|
|
|
|
|
heapsort (ctx->renderPasses.a, ctx->renderPasses.size,
|
|
|
|
sizeof (qfv_renderpass_t *), renderpass_cmp);
|
2021-02-14 02:35:06 +00:00
|
|
|
}
|
2020-02-18 05:28:28 +00:00
|
|
|
|
2021-02-14 02:35:06 +00:00
|
|
|
void
|
2021-12-02 12:58:29 +00:00
|
|
|
Vulkan_DestroyRenderPasses (vulkan_ctx_t *ctx)
|
2021-02-14 02:35:06 +00:00
|
|
|
{
|
2021-12-02 12:58:29 +00:00
|
|
|
for (size_t i = 0; i < ctx->renderPasses.size; i++) {
|
2022-05-30 06:58:22 +00:00
|
|
|
Vulkan_DestroyRenderPass (ctx, ctx->renderPasses.a[i]);
|
2021-12-02 12:58:29 +00:00
|
|
|
}
|
2020-02-18 05:28:28 +00:00
|
|
|
}
|
2020-02-18 08:18:37 +00:00
|
|
|
|
|
|
|
void
|
2021-02-14 02:35:06 +00:00
|
|
|
Vulkan_CreateFrames (vulkan_ctx_t *ctx)
|
2020-02-18 08:18:37 +00:00
|
|
|
{
|
|
|
|
qfv_device_t *device = ctx->device;
|
|
|
|
VkCommandPool cmdpool = ctx->cmdpool;
|
|
|
|
|
2021-02-05 01:22:32 +00:00
|
|
|
if (!ctx->frames.grow) {
|
|
|
|
DARRAY_INIT (&ctx->frames, 4);
|
2020-02-18 08:18:37 +00:00
|
|
|
}
|
|
|
|
|
[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
|
|
|
DARRAY_RESIZE (&ctx->frames, vulkan_frame_count);
|
2020-02-18 08:18:37 +00:00
|
|
|
|
2021-02-05 01:22:32 +00:00
|
|
|
__auto_type cmdBuffers = QFV_AllocCommandBufferSet (ctx->frames.size,
|
2021-01-15 13:45:49 +00:00
|
|
|
alloca);
|
|
|
|
QFV_AllocateCommandBuffers (device, cmdpool, 0, cmdBuffers);
|
2020-02-18 08:18:37 +00:00
|
|
|
|
2021-02-05 01:22:32 +00:00
|
|
|
for (size_t i = 0; i < ctx->frames.size; i++) {
|
|
|
|
__auto_type frame = &ctx->frames.a[i];
|
2020-02-18 08:18:37 +00:00
|
|
|
frame->fence = QFV_CreateFence (device, 1);
|
|
|
|
frame->imageAvailableSemaphore = QFV_CreateSemaphore (device);
|
|
|
|
frame->renderDoneSemaphore = QFV_CreateSemaphore (device);
|
|
|
|
frame->cmdBuffer = cmdBuffers->a[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-24 10:20:53 +00:00
|
|
|
void
|
|
|
|
Vulkan_CreateCapture (vulkan_ctx_t *ctx)
|
|
|
|
{
|
|
|
|
ctx->capture = QFV_CreateCapture (ctx->device, ctx->frames.size,
|
|
|
|
ctx->swapchain, ctx->cmdpool);
|
|
|
|
}
|
|
|
|
|
2020-02-18 08:18:37 +00:00
|
|
|
void
|
2021-02-14 02:35:06 +00:00
|
|
|
Vulkan_DestroyFrames (vulkan_ctx_t *ctx)
|
2020-02-18 08:18:37 +00:00
|
|
|
{
|
|
|
|
qfv_device_t *device = ctx->device;
|
|
|
|
qfv_devfuncs_t *df = device->funcs;
|
|
|
|
VkDevice dev = device->dev;
|
|
|
|
|
2021-02-05 01:22:32 +00:00
|
|
|
for (size_t i = 0; i < ctx->frames.size; i++) {
|
|
|
|
__auto_type frame = &ctx->frames.a[i];
|
2020-02-18 08:18:37 +00:00
|
|
|
df->vkDestroyFence (dev, frame->fence, 0);
|
|
|
|
df->vkDestroySemaphore (dev, frame->imageAvailableSemaphore, 0);
|
|
|
|
df->vkDestroySemaphore (dev, frame->renderDoneSemaphore, 0);
|
|
|
|
}
|
2020-02-18 08:48:22 +00:00
|
|
|
|
2021-02-05 01:22:32 +00:00
|
|
|
DARRAY_CLEAR (&ctx->frames);
|
2021-02-14 02:35:06 +00:00
|
|
|
}
|
2022-05-07 05:12:02 +00:00
|
|
|
|
|
|
|
void
|
[scene] Make entity_t just an entity id for ECS
This puts the hierarchy (transform) reference, animation, visibility,
renderer, active, and old_origin data in separate components. There are
a few bugs (crashes on grenade explosions in gl/glsl/vulkan, immediately
in sw, reasons known, missing brush models in vulkan).
While quake doesn't really need an ECS, the direction I want to take QF
does, and it does seem to have improved memory bandwidth a little
(uncertain). However, there's a lot more work to go (especially fixing
the above bugs), but this seems to be a good start.
2022-10-23 01:32:09 +00:00
|
|
|
Vulkan_BeginEntityLabel (vulkan_ctx_t *ctx, VkCommandBuffer cmd, entity_t ent)
|
2022-05-07 05:12:02 +00:00
|
|
|
{
|
|
|
|
qfv_device_t *device = ctx->device;
|
[scene] Make entity_t just an entity id for ECS
This puts the hierarchy (transform) reference, animation, visibility,
renderer, active, and old_origin data in separate components. There are
a few bugs (crashes on grenade explosions in gl/glsl/vulkan, immediately
in sw, reasons known, missing brush models in vulkan).
While quake doesn't really need an ECS, the direction I want to take QF
does, and it does seem to have improved memory bandwidth a little
(uncertain). However, there's a lot more work to go (especially fixing
the above bugs), but this seems to be a good start.
2022-10-23 01:32:09 +00:00
|
|
|
uint32_t entgen = Ent_Generation (ent.id);
|
|
|
|
uint32_t entind = Ent_Index (ent.id);
|
|
|
|
transform_t transform = Entity_Transform (ent);
|
|
|
|
vec4f_t pos = Transform_GetWorldPosition (transform);
|
2022-05-07 05:12:02 +00:00
|
|
|
vec4f_t dir = normalf (pos - (vec4f_t) { 0, 0, 0, 1 });
|
|
|
|
vec4f_t color = 0.5 * dir + (vec4f_t) {0.5, 0.5, 0.5, 1 };
|
|
|
|
|
|
|
|
QFV_CmdBeginLabel (device, cmd,
|
[scene] Make entity_t just an entity id for ECS
This puts the hierarchy (transform) reference, animation, visibility,
renderer, active, and old_origin data in separate components. There are
a few bugs (crashes on grenade explosions in gl/glsl/vulkan, immediately
in sw, reasons known, missing brush models in vulkan).
While quake doesn't really need an ECS, the direction I want to take QF
does, and it does seem to have improved memory bandwidth a little
(uncertain). However, there's a lot more work to go (especially fixing
the above bugs), but this seems to be a good start.
2022-10-23 01:32:09 +00:00
|
|
|
va (ctx->va_ctx, "ent %03x.%05x [%g, %g, %g]",
|
|
|
|
entgen, entind, VectorExpand (pos)), color);
|
2022-05-07 05:12:02 +00:00
|
|
|
}
|