quakeforge/libs/video/renderer/vulkan/vulkan_vid_common.c

154 lines
3.8 KiB
C
Raw Normal View History

/*
vid_common_vulkan.c
Common Vulkan video driver functions
Copyright (C) 1996-1997 Id Software, Inc.
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
#ifdef HAVE_STRING_H
# include <string.h>
#endif
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif
#include "QF/cvar.h"
#include "QF/dstring.h"
#include "QF/input.h"
2019-07-10 15:58:14 +00:00
#include "QF/mathlib.h"
#include "QF/qargs.h"
#include "QF/quakefs.h"
#include "QF/sys.h"
#include "QF/va.h"
#include "QF/vid.h"
#include "QF/Vulkan/qf_vid.h"
#include "QF/Vulkan/device.h"
#include "QF/Vulkan/instance.h"
#include "QF/Vulkan/swapchain.h"
#include "compat.h"
#include "d_iface.h"
#include "r_internal.h"
#include "vid_vulkan.h"
#include "util.h"
cvar_t *vulkan_presentation_mode;
2019-07-10 15:58:14 +00:00
static void
vulkan_presentation_mode_f (cvar_t *var)
{
if (!strcmp (var->string, "immediate")) {
var->int_val = VK_PRESENT_MODE_IMMEDIATE_KHR;
} else if (!strcmp (var->string, "fifo")) {
var->int_val = VK_PRESENT_MODE_FIFO_KHR;
} else if (!strcmp (var->string, "relaxed")) {
var->int_val = VK_PRESENT_MODE_FIFO_RELAXED_KHR;
} else if (!strcmp (var->string, "mailbox")) {
var->int_val = VK_PRESENT_MODE_MAILBOX_KHR;
} else {
Sys_Printf ("Invalid presentation mode, using fifo\n");
var->int_val = VK_PRESENT_MODE_FIFO_KHR;
}
}
static void
Vulkan_Init_Cvars (void)
{
vulkan_use_validation = Cvar_Get ("vulkan_use_validation", "1", CVAR_NONE,
0,
"enable LunarG Standard Validation "
"Layer if available (requires instance "
"restart).");
2019-07-10 15:58:14 +00:00
// FIXME implement fallback choices (instead of just fifo)
vulkan_presentation_mode = Cvar_Get ("vulkan_presentation_mode", "mailbox",
CVAR_NONE, vulkan_presentation_mode_f,
"desired presentation mode (may fall "
"back to fifo).");
}
static const char *instance_extensions[] = {
VK_KHR_SURFACE_EXTENSION_NAME,
0,
};
static const char *device_extensions[] = {
VK_KHR_SWAPCHAIN_EXTENSION_NAME,
0,
};
void
Vulkan_Init_Common (vulkan_ctx_t *ctx)
{
Sys_Printf ("Vulkan_Init_Common\n");
Vulkan_Init_Cvars ();
ctx->instance = QFV_CreateInstance (ctx, PACKAGE_STRING, 0x000702ff, 0, instance_extensions);//FIXME version
}
void
Vulkan_Shutdown_Common (vulkan_ctx_t *ctx)
{
if (ctx->swapchain) {
QFV_DestroySwapchain (ctx->swapchain);
}
2019-07-12 16:20:02 +00:00
ctx->instance->funcs->vkDestroySurfaceKHR (ctx->instance->instance,
ctx->surface, 0);
if (ctx->device) {
QFV_DestroyDevice (ctx->device);
}
if (ctx->instance) {
QFV_DestroyInstance (ctx->instance);
}
ctx->instance = 0;
ctx->unload_vulkan (ctx);
}
void
Vulkan_CreateDevice (vulkan_ctx_t *ctx)
{
ctx->device = QFV_CreateDevice (ctx, device_extensions);
}
2019-07-10 15:58:14 +00:00
void
Vulkan_CreateSwapchain (vulkan_ctx_t *ctx)
{
VkSwapchainKHR old_swapchain = 0;
if (ctx->swapchain) {
old_swapchain = ctx->swapchain->swapchain;
free (ctx->swapchain);
}
ctx->swapchain = QFV_CreateSwapchain (ctx, old_swapchain);
if (ctx->swapchain->swapchain == old_swapchain) {
ctx->device->funcs->vkDestroySwapchainKHR (ctx->device->dev,
old_swapchain, 0);
}
2019-07-10 15:58:14 +00:00
}