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-11-27 15:52:07 +00:00
|
|
|
#include "QF/Vulkan/qf_particles.h"
|
2022-11-30 18:00:47 +00:00
|
|
|
#include "QF/Vulkan/qf_translucent.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
|
|
|
|
2022-12-02 04:17:07 +00:00
|
|
|
int vulkan_frame_width;
|
|
|
|
static cvar_t vulkan_frame_width_cvar = {
|
|
|
|
.name = "vulkan_frame_width",
|
|
|
|
.description =
|
|
|
|
"Width of 3D view buffer. Set to 0 for automatic sizing.",
|
|
|
|
.default_value = "0",
|
|
|
|
.flags = CVAR_NONE,
|
|
|
|
.value = { .type = &cexpr_int, .value = &vulkan_frame_width },
|
|
|
|
};
|
|
|
|
|
|
|
|
int vulkan_frame_height;
|
|
|
|
static cvar_t vulkan_frame_height_cvar = {
|
|
|
|
.name = "vulkan_frame_height",
|
|
|
|
.description =
|
|
|
|
"Height of 3D view buffer. Set to 0 for automatic sizing.",
|
|
|
|
.default_value = "0",
|
|
|
|
.flags = CVAR_NONE,
|
|
|
|
.value = { .type = &cexpr_int, .value = &vulkan_frame_height },
|
|
|
|
};
|
2022-12-02 04:29:15 +00:00
|
|
|
int vulkan_oit_fragments;
|
|
|
|
static cvar_t vulkan_oit_fragments_cvar = {
|
|
|
|
.name = "vulkan_oit_fragments",
|
|
|
|
.description =
|
|
|
|
"Size of fragment buffer (M) for order independent transparency.",
|
|
|
|
.default_value = "16",
|
|
|
|
.flags = CVAR_ROM,
|
|
|
|
.value = { .type = &cexpr_int, .value = &vulkan_oit_fragments },
|
|
|
|
};
|
2022-12-02 04:17:07 +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
|
|
|
|
2022-12-02 04:17:07 +00:00
|
|
|
Cvar_Register (&vulkan_frame_width_cvar, 0, 0);
|
|
|
|
Cvar_Register (&vulkan_frame_height_cvar, 0, 0);
|
2022-12-02 04:29:15 +00:00
|
|
|
Cvar_Register (&vulkan_oit_fragments_cvar, 0, 0);
|
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
|
|
|
{
|
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-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
|
|
|
}
|
2023-02-19 03:38:46 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
Vulkan_ConfigOutput (vulkan_ctx_t *ctx, qfv_output_t *output)
|
|
|
|
{
|
|
|
|
*output = (qfv_output_t) {
|
|
|
|
.extent = ctx->swapchain->extent,
|
|
|
|
.frames = ctx->swapchain->numImages,
|
|
|
|
};
|
|
|
|
if (vulkan_frame_width > 0) {
|
|
|
|
output->extent.width = vulkan_frame_width;
|
|
|
|
}
|
|
|
|
if (vulkan_frame_height > 0) {
|
|
|
|
output->extent.height = vulkan_frame_height;
|
|
|
|
}
|
2023-06-26 05:03:19 +00:00
|
|
|
if (scr_fisheye) {//FIXME
|
|
|
|
auto w = output->extent.width;
|
|
|
|
auto h = output->extent.height;
|
|
|
|
output->extent.width = min (w, h);
|
|
|
|
output->extent.height = min (w, h);
|
|
|
|
}
|
2023-02-19 03:38:46 +00:00
|
|
|
}
|