2023-01-30 04:00:35 +00:00
|
|
|
/*
|
|
|
|
render.c
|
|
|
|
|
|
|
|
Vulkan render manager
|
|
|
|
|
|
|
|
Copyright (C) 2023 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
|
|
|
|
|
2023-02-19 03:53:02 +00:00
|
|
|
#include "QF/cmem.h"
|
2023-02-12 16:43:17 +00:00
|
|
|
#include "QF/hash.h"
|
2023-07-13 09:33:31 +00:00
|
|
|
#include "QF/mathlib.h"
|
2023-02-20 15:44:26 +00:00
|
|
|
#include "QF/va.h"
|
2023-01-30 04:00:35 +00:00
|
|
|
#include "QF/Vulkan/command.h"
|
|
|
|
#include "QF/Vulkan/debug.h"
|
|
|
|
#include "QF/Vulkan/device.h"
|
2023-06-24 05:55:46 +00:00
|
|
|
#include "QF/Vulkan/dsmanager.h"
|
2023-06-20 06:18:58 +00:00
|
|
|
#include "QF/Vulkan/image.h"
|
|
|
|
#include "QF/Vulkan/pipeline.h"
|
2023-01-30 04:00:35 +00:00
|
|
|
#include "QF/Vulkan/render.h"
|
2023-02-19 03:53:02 +00:00
|
|
|
#include "QF/Vulkan/resource.h"
|
2023-06-20 06:18:58 +00:00
|
|
|
#include "QF/Vulkan/swapchain.h"
|
2023-01-30 04:00:35 +00:00
|
|
|
#include "vid_vulkan.h"
|
|
|
|
|
2023-02-10 02:04:11 +00:00
|
|
|
#include "vkparse.h"
|
|
|
|
|
2023-06-15 06:17:39 +00:00
|
|
|
VkCommandBuffer
|
2023-06-22 08:00:10 +00:00
|
|
|
QFV_GetCmdBuffer (vulkan_ctx_t *ctx, bool secondary)
|
2023-03-28 01:28:44 +00:00
|
|
|
{
|
2023-06-22 08:00:10 +00:00
|
|
|
auto rctx = ctx->render_context;
|
|
|
|
auto rframe = &rctx->frames.a[ctx->curFrame];
|
|
|
|
return QFV_CmdPoolManager_CmdBuffer (&rframe->cmdpool, secondary);
|
2023-03-28 01:28:44 +00:00
|
|
|
}
|
|
|
|
|
2023-06-15 06:17:39 +00:00
|
|
|
void
|
|
|
|
QFV_AppendCmdBuffer (vulkan_ctx_t *ctx, VkCommandBuffer cmd)
|
|
|
|
{
|
|
|
|
__auto_type rctx = ctx->render_context;
|
|
|
|
__auto_type job = rctx->job;
|
|
|
|
DARRAY_APPEND (&job->commands, cmd);
|
|
|
|
}
|
|
|
|
|
2023-07-13 09:33:31 +00:00
|
|
|
static void
|
|
|
|
update_time (qfv_time_t *time, int64_t start, int64_t end)
|
|
|
|
{
|
|
|
|
int64_t delta = end - start;
|
|
|
|
time->cur_time = delta;
|
|
|
|
time->min_time = min (time->min_time, delta);
|
|
|
|
time->max_time = max (time->max_time, delta);
|
|
|
|
}
|
|
|
|
|
2023-03-28 01:28:44 +00:00
|
|
|
static void
|
2023-06-15 06:17:39 +00:00
|
|
|
run_tasks (uint32_t task_count, qfv_taskinfo_t *tasks, qfv_taskctx_t *ctx)
|
2023-03-28 01:28:44 +00:00
|
|
|
{
|
|
|
|
for (uint32_t i = 0; i < task_count; i++) {
|
2023-06-15 06:17:39 +00:00
|
|
|
tasks[i].func->func (tasks[i].params, 0, (exprctx_t *) ctx);
|
2023-03-28 01:28:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-30 04:00:35 +00:00
|
|
|
static void
|
2023-06-22 12:36:14 +00:00
|
|
|
run_pipeline (qfv_pipeline_t *pipeline, qfv_taskctx_t *taskctx)
|
2023-01-30 04:00:35 +00:00
|
|
|
{
|
2023-06-26 02:54:28 +00:00
|
|
|
if (pipeline->disabled) {
|
|
|
|
return;
|
|
|
|
}
|
2023-06-22 12:36:14 +00:00
|
|
|
qfv_device_t *device = taskctx->ctx->device;
|
2023-01-30 04:00:35 +00:00
|
|
|
qfv_devfuncs_t *dfunc = device->funcs;
|
2023-06-22 12:36:14 +00:00
|
|
|
auto cmd = taskctx->cmd;
|
2023-01-30 04:00:35 +00:00
|
|
|
dfunc->vkCmdBindPipeline (cmd, pipeline->bindPoint, pipeline->pipeline);
|
|
|
|
dfunc->vkCmdSetViewport (cmd, 0, 1, &pipeline->viewport);
|
|
|
|
dfunc->vkCmdSetScissor (cmd, 0, 1, &pipeline->scissor);
|
2023-03-28 01:28:44 +00:00
|
|
|
|
2023-06-22 12:36:14 +00:00
|
|
|
taskctx->pipeline = pipeline;
|
|
|
|
run_tasks (pipeline->task_count, pipeline->tasks, taskctx);
|
2023-01-30 04:00:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// https://themaister.net/blog/2019/08/14/yet-another-blog-explaining-vulkan-synchronization/
|
|
|
|
static void
|
2023-06-22 12:36:14 +00:00
|
|
|
run_subpass (qfv_subpass_t *sp, qfv_taskctx_t *taskctx)
|
2023-01-30 04:00:35 +00:00
|
|
|
{
|
2023-06-22 12:36:14 +00:00
|
|
|
qfv_device_t *device = taskctx->ctx->device;
|
2023-01-30 04:00:35 +00:00
|
|
|
qfv_devfuncs_t *dfunc = device->funcs;
|
2023-06-22 12:36:14 +00:00
|
|
|
dfunc->vkBeginCommandBuffer (taskctx->cmd, &sp->beginInfo);
|
2023-01-30 04:00:35 +00:00
|
|
|
|
2023-02-20 15:44:26 +00:00
|
|
|
for (uint32_t i = 0; i < sp->pipeline_count; i++) {
|
2023-01-30 04:00:35 +00:00
|
|
|
__auto_type pipeline = &sp->pipelines[i];
|
2023-06-22 12:36:14 +00:00
|
|
|
run_pipeline (pipeline, taskctx);
|
2023-01-30 04:00:35 +00:00
|
|
|
}
|
|
|
|
|
2023-06-22 12:36:14 +00:00
|
|
|
dfunc->vkEndCommandBuffer (taskctx->cmd);
|
2023-01-30 04:00:35 +00:00
|
|
|
}
|
|
|
|
|
2023-03-28 01:28:44 +00:00
|
|
|
static void
|
2023-06-15 04:13:52 +00:00
|
|
|
run_renderpass (qfv_renderpass_t *rp, vulkan_ctx_t *ctx)
|
2023-01-30 04:00:35 +00:00
|
|
|
{
|
|
|
|
qfv_device_t *device = ctx->device;
|
|
|
|
qfv_devfuncs_t *dfunc = device->funcs;
|
2023-06-15 06:17:39 +00:00
|
|
|
__auto_type rctx = ctx->render_context;
|
|
|
|
__auto_type job = rctx->job;
|
2023-01-30 04:00:35 +00:00
|
|
|
|
2023-06-22 08:00:10 +00:00
|
|
|
VkCommandBuffer cmd = QFV_GetCmdBuffer (ctx, false);
|
2023-06-23 18:26:22 +00:00
|
|
|
QFV_duSetObjectName (device, VK_OBJECT_TYPE_COMMAND_BUFFER, cmd,
|
|
|
|
va (ctx->va_ctx, "cmd:render:%s", rp->label.name));
|
2023-01-30 04:00:35 +00:00
|
|
|
VkCommandBufferBeginInfo beginInfo = {
|
|
|
|
VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
|
|
|
|
};
|
|
|
|
dfunc->vkBeginCommandBuffer (cmd, &beginInfo);
|
|
|
|
QFV_duCmdBeginLabel (device, cmd, rp->label.name,
|
|
|
|
{VEC4_EXP (rp->label.color)});
|
|
|
|
dfunc->vkCmdBeginRenderPass (cmd, &rp->beginInfo, rp->subpassContents);
|
|
|
|
for (uint32_t i = 0; i < rp->subpass_count; i++) {
|
|
|
|
__auto_type sp = &rp->subpasses[i];
|
2023-06-22 11:30:10 +00:00
|
|
|
QFV_duCmdBeginLabel (device, cmd, sp->label.name,
|
|
|
|
{VEC4_EXP (sp->label.color)});
|
2023-06-22 12:36:14 +00:00
|
|
|
qfv_taskctx_t taskctx = {
|
|
|
|
.ctx = ctx,
|
|
|
|
.renderpass = rp,
|
|
|
|
.cmd = QFV_GetCmdBuffer (ctx, true),
|
|
|
|
};
|
|
|
|
run_subpass (sp, &taskctx);
|
|
|
|
dfunc->vkCmdExecuteCommands (cmd, 1, &taskctx.cmd);
|
2023-06-22 11:30:10 +00:00
|
|
|
QFV_duCmdEndLabel (device, cmd);
|
2023-01-30 04:00:35 +00:00
|
|
|
//FIXME comment is a bit off as exactly one buffer is always submitted
|
|
|
|
//
|
|
|
|
//Regardless of whether any commands were submitted for this
|
|
|
|
//subpass, must step through each and every subpass, otherwise
|
|
|
|
//the attachments won't be transitioned correctly.
|
|
|
|
//However, only if not the last (or only) subpass.
|
|
|
|
if (i < rp->subpass_count - 1) {
|
|
|
|
dfunc->vkCmdNextSubpass (cmd, rp->subpassContents);
|
|
|
|
}
|
|
|
|
}
|
2023-06-21 05:47:19 +00:00
|
|
|
dfunc->vkCmdEndRenderPass (cmd);
|
2023-01-30 04:00:35 +00:00
|
|
|
QFV_CmdEndLabel (device, cmd);
|
2023-06-21 05:47:19 +00:00
|
|
|
dfunc->vkEndCommandBuffer (cmd);
|
2023-06-15 06:17:39 +00:00
|
|
|
DARRAY_APPEND (&job->commands, cmd);
|
2023-01-30 04:00:35 +00:00
|
|
|
}
|
2023-02-10 02:04:11 +00:00
|
|
|
|
2023-03-28 01:28:44 +00:00
|
|
|
static void
|
|
|
|
run_compute_pipeline (qfv_pipeline_t *pipeline, VkCommandBuffer cmd,
|
|
|
|
vulkan_ctx_t *ctx)
|
|
|
|
{
|
2023-06-26 02:54:28 +00:00
|
|
|
if (pipeline->disabled) {
|
|
|
|
return;
|
|
|
|
}
|
2023-03-28 01:28:44 +00:00
|
|
|
qfv_device_t *device = ctx->device;
|
|
|
|
qfv_devfuncs_t *dfunc = device->funcs;
|
|
|
|
dfunc->vkCmdBindPipeline (cmd, pipeline->bindPoint, pipeline->pipeline);
|
|
|
|
|
|
|
|
qfv_taskctx_t taskctx = {
|
|
|
|
.ctx = ctx,
|
|
|
|
.pipeline = pipeline,
|
2023-06-16 13:34:08 +00:00
|
|
|
.cmd = cmd,
|
2023-03-28 01:28:44 +00:00
|
|
|
};
|
|
|
|
run_tasks (pipeline->task_count, pipeline->tasks, &taskctx);
|
|
|
|
|
2023-06-15 06:17:39 +00:00
|
|
|
vec4u_t d = pipeline->dispatch;
|
2023-06-23 18:26:22 +00:00
|
|
|
if (d[0] && d[1] && d[2]) {
|
|
|
|
dfunc->vkCmdDispatch (cmd, d[0], d[1], d[2]);
|
|
|
|
}
|
2023-03-28 01:28:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2023-06-23 18:26:22 +00:00
|
|
|
run_compute (qfv_compute_t *comp, vulkan_ctx_t *ctx, qfv_step_t *step)
|
2023-03-28 01:28:44 +00:00
|
|
|
{
|
|
|
|
qfv_device_t *device = ctx->device;
|
|
|
|
qfv_devfuncs_t *dfunc = device->funcs;
|
|
|
|
__auto_type rctx = ctx->render_context;
|
|
|
|
__auto_type job = rctx->job;
|
|
|
|
|
2023-06-22 08:00:10 +00:00
|
|
|
VkCommandBuffer cmd = QFV_GetCmdBuffer (ctx, false);
|
2023-06-23 18:26:22 +00:00
|
|
|
QFV_duSetObjectName (device, VK_OBJECT_TYPE_COMMAND_BUFFER, cmd,
|
|
|
|
va (ctx->va_ctx, "cmd:compute:%s", step->label.name));
|
|
|
|
QFV_duCmdBeginLabel (device, cmd, step->label.name,
|
|
|
|
{VEC4_EXP (step->label.color)});
|
2023-03-28 01:28:44 +00:00
|
|
|
|
|
|
|
VkCommandBufferBeginInfo beginInfo = {
|
|
|
|
.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
|
|
|
|
.flags = VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT,
|
|
|
|
};
|
|
|
|
dfunc->vkBeginCommandBuffer (cmd, &beginInfo);
|
|
|
|
for (uint32_t i = 0; i < comp->pipeline_count; i++) {
|
|
|
|
__auto_type pipeline = &comp->pipelines[i];
|
|
|
|
run_compute_pipeline (pipeline, cmd, ctx);
|
|
|
|
}
|
2023-06-23 18:26:22 +00:00
|
|
|
QFV_duCmdEndLabel (device, cmd);
|
2023-03-28 01:28:44 +00:00
|
|
|
dfunc->vkEndCommandBuffer (cmd);
|
|
|
|
DARRAY_APPEND (&job->commands, cmd);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
run_process (qfv_process_t *proc, vulkan_ctx_t *ctx)
|
|
|
|
{
|
|
|
|
qfv_taskctx_t taskctx = {
|
|
|
|
.ctx = ctx,
|
|
|
|
};
|
|
|
|
run_tasks (proc->task_count, proc->tasks, &taskctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QFV_RunRenderJob (vulkan_ctx_t *ctx)
|
|
|
|
{
|
2023-06-21 05:47:19 +00:00
|
|
|
auto rctx = ctx->render_context;
|
|
|
|
auto job = rctx->job;
|
2023-07-13 09:33:31 +00:00
|
|
|
int64_t start = Sys_LongTime ();
|
2023-03-28 01:28:44 +00:00
|
|
|
|
|
|
|
for (uint32_t i = 0; i < job->num_steps; i++) {
|
2023-07-13 09:33:31 +00:00
|
|
|
int64_t step_start = Sys_LongTime ();
|
2023-03-28 01:28:44 +00:00
|
|
|
__auto_type step = &job->steps[i];
|
|
|
|
if (step->render) {
|
|
|
|
run_renderpass (step->render->active, ctx);
|
|
|
|
}
|
|
|
|
if (step->compute) {
|
2023-06-23 18:26:22 +00:00
|
|
|
run_compute (step->compute, ctx, step);
|
2023-03-28 01:28:44 +00:00
|
|
|
}
|
|
|
|
if (step->process) {
|
|
|
|
run_process (step->process, ctx);
|
|
|
|
}
|
2023-07-13 09:33:31 +00:00
|
|
|
update_time (&step->time, step_start, Sys_LongTime ());
|
2023-03-28 01:28:44 +00:00
|
|
|
}
|
2023-06-21 05:47:19 +00:00
|
|
|
|
|
|
|
auto device = ctx->device;
|
|
|
|
auto dfunc = device->funcs;
|
|
|
|
auto queue = &device->queue;
|
2023-06-22 07:44:05 +00:00
|
|
|
auto frame = &rctx->frames.a[ctx->curFrame];
|
2023-06-21 05:47:19 +00:00
|
|
|
VkPipelineStageFlags waitStage
|
|
|
|
= VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
|
|
|
|
VkSubmitInfo submitInfo = {
|
|
|
|
VK_STRUCTURE_TYPE_SUBMIT_INFO, 0,
|
|
|
|
1, &frame->imageAvailableSemaphore, &waitStage,
|
|
|
|
job->commands.size, job->commands.a,
|
|
|
|
1, &frame->renderDoneSemaphore,
|
|
|
|
};
|
|
|
|
dfunc->vkResetFences (device->dev, 1, &frame->fence);
|
|
|
|
dfunc->vkQueueSubmit (queue->queue, 1, &submitInfo, frame->fence);
|
|
|
|
|
|
|
|
VkPresentInfoKHR presentInfo = {
|
|
|
|
VK_STRUCTURE_TYPE_PRESENT_INFO_KHR, 0,
|
|
|
|
1, &frame->renderDoneSemaphore,
|
|
|
|
1, &ctx->swapchain->swapchain, &ctx->swapImageIndex,
|
|
|
|
0
|
|
|
|
};
|
|
|
|
dfunc->vkQueuePresentKHR (queue->queue, &presentInfo);
|
2023-06-23 18:32:21 +00:00
|
|
|
|
|
|
|
if (++ctx->curFrame >= rctx->frames.size) {
|
|
|
|
ctx->curFrame = 0;
|
|
|
|
}
|
2023-07-13 09:33:31 +00:00
|
|
|
update_time (&job->time, start, Sys_LongTime ());
|
2023-03-28 01:28:44 +00:00
|
|
|
}
|
|
|
|
|
2023-07-03 11:05:27 +00:00
|
|
|
static qfv_imageviewinfo_t * __attribute__((pure))
|
|
|
|
find_imageview (qfv_reference_t *ref, qfv_renderpass_t *rp,
|
|
|
|
qfv_renderctx_t *rctx)
|
2023-06-18 09:42:07 +00:00
|
|
|
{
|
2023-07-03 11:05:27 +00:00
|
|
|
auto jinfo = rctx->jobinfo;
|
2023-06-18 09:42:07 +00:00
|
|
|
const char *name = ref->name;
|
|
|
|
|
|
|
|
if (strncmp (name, "$imageviews.", 7) == 0) {
|
|
|
|
name += 7;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (uint32_t i = 0; i < jinfo->num_imageviews; i++) {
|
2023-07-03 11:05:27 +00:00
|
|
|
auto viewinfo = &jinfo->imageviews[i];
|
|
|
|
if (strcmp (name, viewinfo->name) == 0) {
|
|
|
|
return viewinfo;
|
2023-06-18 09:42:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Sys_Error ("%d:invalid imageview: %s", ref->line, ref->name);
|
2023-03-02 09:33:16 +00:00
|
|
|
}
|
|
|
|
|
2023-06-21 05:47:19 +00:00
|
|
|
void
|
2023-07-03 11:05:27 +00:00
|
|
|
QFV_DestroyFramebuffer (vulkan_ctx_t *ctx, qfv_renderpass_t *rp)
|
2023-03-02 09:33:16 +00:00
|
|
|
{
|
2023-07-03 11:05:27 +00:00
|
|
|
if (rp->beginInfo.framebuffer) {
|
|
|
|
auto device = ctx->device;
|
|
|
|
auto dfunc = device->funcs;
|
|
|
|
auto bi = &rp->beginInfo;
|
|
|
|
dfunc->vkDestroyFramebuffer (device->dev, bi->framebuffer, 0);
|
|
|
|
bi->framebuffer = 0;
|
|
|
|
}
|
|
|
|
if (rp->resources && rp->resources->memory) {
|
|
|
|
QFV_DestroyResource (ctx->device, rp->resources);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
QFV_CreateFramebuffer (vulkan_ctx_t *ctx, qfv_renderpass_t *rp,
|
|
|
|
VkExtent2D extent)
|
|
|
|
{
|
|
|
|
auto rctx = ctx->render_context;
|
|
|
|
|
|
|
|
if (rp->resources && !rp->resources->memory) {
|
|
|
|
for (uint32_t i = 0; i < rp->resources->num_objects; i++) {
|
|
|
|
auto obj = &rp->resources->objects[i];
|
|
|
|
if (obj->type == qfv_res_image) {
|
|
|
|
obj->image.extent.width = extent.width;
|
|
|
|
obj->image.extent.height = extent.height;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
QFV_CreateResource (ctx->device, rp->resources);
|
|
|
|
}
|
2023-03-02 09:33:16 +00:00
|
|
|
|
2023-06-18 09:42:07 +00:00
|
|
|
auto fb = rp->framebufferinfo;
|
2023-06-22 12:36:14 +00:00
|
|
|
auto attachments = rp->framebuffer.views;
|
2023-03-02 09:33:16 +00:00
|
|
|
VkFramebufferCreateInfo cInfo = {
|
|
|
|
.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
|
2023-06-18 09:42:07 +00:00
|
|
|
.attachmentCount = fb->num_attachments,
|
2023-03-02 09:33:16 +00:00
|
|
|
.pAttachments = attachments,
|
|
|
|
.renderPass = rp->beginInfo.renderPass,
|
2023-07-03 11:05:27 +00:00
|
|
|
.width = extent.width,
|
|
|
|
.height = extent.height,
|
2023-06-18 09:42:07 +00:00
|
|
|
.layers = fb->layers,
|
2023-03-02 09:33:16 +00:00
|
|
|
};
|
2023-06-18 09:42:07 +00:00
|
|
|
for (uint32_t i = 0; i < fb->num_attachments; i++) {
|
2023-06-20 06:18:58 +00:00
|
|
|
if (fb->attachments[i].external) {
|
|
|
|
attachments[i] = 0;
|
|
|
|
if (!strcmp (fb->attachments[i].external, "$swapchain")) {
|
|
|
|
auto sc = ctx->swapchain;
|
|
|
|
attachments[i] = sc->imageViews->a[ctx->swapImageIndex];
|
|
|
|
cInfo.width = sc->extent.width;
|
|
|
|
cInfo.height = sc->extent.height;
|
|
|
|
}
|
|
|
|
} else {
|
2023-07-03 11:05:27 +00:00
|
|
|
auto viewinfo = find_imageview (&fb->attachments[i].view, rp, rctx);
|
|
|
|
attachments[i] = viewinfo->object->image_view.view;
|
2023-06-22 15:43:34 +00:00
|
|
|
if (rp->outputref.name) {
|
2023-07-03 11:05:27 +00:00
|
|
|
viewinfo = find_imageview (&rp->outputref, rp, rctx);
|
|
|
|
rp->output = viewinfo->object->image_view.view;
|
2023-06-22 15:43:34 +00:00
|
|
|
}
|
2023-06-20 06:18:58 +00:00
|
|
|
}
|
2023-03-02 09:33:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
qfv_device_t *device = ctx->device;
|
|
|
|
qfv_devfuncs_t *dfunc = device->funcs;
|
|
|
|
VkFramebuffer framebuffer;
|
|
|
|
dfunc->vkCreateFramebuffer (device->dev, &cInfo, 0, &framebuffer);
|
|
|
|
QFV_duSetObjectName (device, VK_OBJECT_TYPE_FRAMEBUFFER, framebuffer,
|
2023-06-18 09:42:07 +00:00
|
|
|
va (ctx->va_ctx, "framebuffer:%s", rp->label.name));
|
2023-03-02 09:33:16 +00:00
|
|
|
|
|
|
|
rp->beginInfo.framebuffer = framebuffer;
|
|
|
|
for (uint32_t i = 0; i < rp->subpass_count; i++) {
|
|
|
|
__auto_type sp = &rp->subpasses[i];
|
|
|
|
sp->inherit.framebuffer = framebuffer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-18 09:42:07 +00:00
|
|
|
static void
|
|
|
|
wait_on_fence (const exprval_t **params, exprval_t *result, exprctx_t *ectx)
|
|
|
|
{
|
2023-06-21 05:47:19 +00:00
|
|
|
auto taskctx = (qfv_taskctx_t *) ectx;
|
|
|
|
auto ctx = taskctx->ctx;
|
|
|
|
auto device = ctx->device;
|
|
|
|
auto dfunc = device->funcs;
|
|
|
|
auto dev = device->dev;
|
2023-06-22 07:44:05 +00:00
|
|
|
auto rctx = ctx->render_context;
|
|
|
|
auto frame = &rctx->frames.a[ctx->curFrame];
|
2023-06-18 09:42:07 +00:00
|
|
|
|
|
|
|
dfunc->vkWaitForFences (dev, 1, &frame->fence, VK_TRUE, 2000000000);
|
2023-06-21 05:47:19 +00:00
|
|
|
|
2023-06-22 08:00:10 +00:00
|
|
|
QFV_CmdPoolManager_Reset (&frame->cmdpool);
|
2023-06-21 05:47:19 +00:00
|
|
|
auto job = ctx->render_context->job;
|
2023-06-21 14:31:08 +00:00
|
|
|
DARRAY_RESIZE (&job->commands, 0);
|
2023-06-18 09:42:07 +00:00
|
|
|
}
|
|
|
|
|
2023-06-20 06:18:58 +00:00
|
|
|
static void
|
2023-07-03 11:05:27 +00:00
|
|
|
renderpass_update_viewport_sissor (qfv_renderpass_t *rp,
|
|
|
|
const qfv_output_t *output)
|
2023-06-20 06:18:58 +00:00
|
|
|
{
|
|
|
|
rp->beginInfo.renderArea.extent = output->extent;
|
|
|
|
for (uint32_t i = 0; i < rp->subpass_count; i++) {
|
|
|
|
auto sp = &rp->subpasses[i];
|
|
|
|
for (uint32_t j = 0; j < sp->pipeline_count; j++) {
|
|
|
|
auto pl = &sp->pipelines[j];
|
2023-06-22 15:50:27 +00:00
|
|
|
pl->viewport = (VkViewport) {
|
|
|
|
.width = output->extent.width,
|
|
|
|
.height = output->extent.height,
|
|
|
|
.minDepth = 0,
|
|
|
|
.maxDepth = 1,
|
|
|
|
};
|
2023-06-20 06:18:58 +00:00
|
|
|
pl->scissor.extent = output->extent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
update_viewport_scissor (qfv_render_t *render, const qfv_output_t *output)
|
|
|
|
{
|
|
|
|
for (uint32_t i = 0; i < render->num_renderpasses; i++) {
|
2023-07-03 11:05:27 +00:00
|
|
|
renderpass_update_viewport_sissor (&render->renderpasses[i], output);
|
2023-06-20 06:18:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-18 09:42:07 +00:00
|
|
|
static void
|
|
|
|
update_framebuffer (const exprval_t **params, exprval_t *result,
|
|
|
|
exprctx_t *ectx)
|
|
|
|
{
|
|
|
|
auto taskctx = (qfv_taskctx_t *) ectx;
|
|
|
|
auto ctx = taskctx->ctx;
|
2023-06-20 06:18:58 +00:00
|
|
|
auto job = ctx->render_context->job;
|
|
|
|
auto step = QFV_GetStep (params[0], job);
|
2023-06-18 09:42:07 +00:00
|
|
|
auto render = step->render;
|
|
|
|
auto rp = render->active;
|
|
|
|
|
2023-06-20 06:18:58 +00:00
|
|
|
qfv_output_t output = {};
|
|
|
|
Vulkan_ConfigOutput (ctx, &output);
|
2023-07-03 14:38:57 +00:00
|
|
|
if ((output.extent.width != render->output.extent.width
|
|
|
|
|| output.extent.height != render->output.extent.height)
|
|
|
|
&& (Sys_LongTime () - ctx->render_context->size_time) > 2*1000*1000) {
|
2023-07-03 11:05:27 +00:00
|
|
|
QFV_DestroyFramebuffer (ctx, rp);
|
2023-06-20 06:18:58 +00:00
|
|
|
update_viewport_scissor (render, &output);
|
2023-07-03 11:05:27 +00:00
|
|
|
render->output.extent = output.extent;
|
2023-06-20 06:18:58 +00:00
|
|
|
}
|
|
|
|
|
2023-06-18 09:42:07 +00:00
|
|
|
if (!rp->beginInfo.framebuffer) {
|
2023-07-03 11:05:27 +00:00
|
|
|
QFV_CreateFramebuffer (ctx, rp, render->output.extent);
|
2023-06-18 09:42:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static exprfunc_t wait_on_fence_func[] = {
|
|
|
|
{ .func = wait_on_fence },
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
|
|
|
static exprtype_t *update_framebuffer_params[] = {
|
|
|
|
&cexpr_string,
|
|
|
|
};
|
|
|
|
static exprfunc_t update_framebuffer_func[] = {
|
|
|
|
{ .func = update_framebuffer, .num_params = 1, update_framebuffer_params },
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
static exprsym_t render_task_syms[] = {
|
|
|
|
{ "wait_on_fence", &cexpr_function, wait_on_fence_func },
|
|
|
|
{ "update_framebuffer", &cexpr_function, update_framebuffer_func },
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
2023-02-13 08:45:36 +00:00
|
|
|
void
|
|
|
|
QFV_Render_Init (vulkan_ctx_t *ctx)
|
|
|
|
{
|
|
|
|
qfv_renderctx_t *rctx = calloc (1, sizeof (*rctx));
|
|
|
|
ctx->render_context = rctx;
|
2023-07-03 14:38:57 +00:00
|
|
|
rctx->size_time = -1000*1000*1000;
|
2023-02-13 08:45:36 +00:00
|
|
|
|
|
|
|
exprctx_t ectx = { .hashctx = &rctx->hashctx };
|
|
|
|
exprsym_t syms[] = { {} };
|
|
|
|
rctx->task_functions.symbols = syms;
|
|
|
|
cexpr_init_symtab (&rctx->task_functions, &ectx);
|
|
|
|
rctx->task_functions.symbols = 0;
|
2023-06-18 09:42:07 +00:00
|
|
|
|
|
|
|
QFV_Render_AddTasks (ctx, render_task_syms);
|
2023-06-22 07:44:05 +00:00
|
|
|
|
|
|
|
auto device = ctx->device;
|
|
|
|
size_t frames = vulkan_frame_count;
|
|
|
|
DARRAY_INIT (&rctx->frames, frames);
|
|
|
|
DARRAY_RESIZE (&rctx->frames, frames);
|
|
|
|
for (size_t i = 0; i < rctx->frames.size; i++) {
|
|
|
|
auto frame = &rctx->frames.a[i];
|
|
|
|
frame->fence = QFV_CreateFence (device, 1);
|
|
|
|
frame->imageAvailableSemaphore = QFV_CreateSemaphore (device);
|
|
|
|
QFV_duSetObjectName (device, VK_OBJECT_TYPE_SEMAPHORE,
|
|
|
|
frame->imageAvailableSemaphore,
|
|
|
|
va (ctx->va_ctx, "sc image:%zd", i));
|
|
|
|
frame->renderDoneSemaphore = QFV_CreateSemaphore (device);
|
2023-06-22 11:00:50 +00:00
|
|
|
QFV_CmdPoolManager_Init (&frame->cmdpool, device,
|
|
|
|
va (ctx->va_ctx, "render pool:%zd", i));
|
2023-06-22 07:44:05 +00:00
|
|
|
}
|
2023-02-10 02:04:11 +00:00
|
|
|
}
|
2023-02-13 15:55:22 +00:00
|
|
|
|
2023-02-19 03:53:02 +00:00
|
|
|
void
|
|
|
|
QFV_Render_Shutdown (vulkan_ctx_t *ctx)
|
|
|
|
{
|
2023-02-27 05:15:28 +00:00
|
|
|
qfv_device_t *device = ctx->device;
|
|
|
|
qfv_devfuncs_t *dfunc = device->funcs;
|
2023-02-19 03:53:02 +00:00
|
|
|
__auto_type rctx = ctx->render_context;
|
2023-03-22 10:32:49 +00:00
|
|
|
if (rctx->job) {
|
|
|
|
__auto_type job = rctx->job;
|
|
|
|
for (uint32_t i = 0; i < job->num_renderpasses; i++) {
|
|
|
|
dfunc->vkDestroyRenderPass (device->dev, job->renderpasses[i], 0);
|
2023-02-27 05:15:28 +00:00
|
|
|
}
|
2023-03-22 10:32:49 +00:00
|
|
|
for (uint32_t i = 0; i < job->num_pipelines; i++) {
|
|
|
|
dfunc->vkDestroyPipeline (device->dev, job->pipelines[i], 0);
|
|
|
|
}
|
|
|
|
for (uint32_t i = 0; i < job->num_layouts; i++) {
|
|
|
|
dfunc->vkDestroyPipelineLayout (device->dev, job->layouts[i], 0);
|
2023-02-19 03:53:02 +00:00
|
|
|
}
|
2023-06-21 08:58:15 +00:00
|
|
|
for (uint32_t i = 0; i < job->num_steps; i++) {
|
|
|
|
if (job->steps[i].render) {
|
|
|
|
auto render = job->steps[i].render;
|
|
|
|
for (uint32_t j = 0; j < render->num_renderpasses; j++) {
|
2023-07-03 11:05:27 +00:00
|
|
|
auto rp = &render->renderpasses[j];
|
|
|
|
if (rp->resources && rp->resources->memory) {
|
|
|
|
QFV_DestroyResource (ctx->device, rp->resources);
|
|
|
|
}
|
|
|
|
free (rp->resources);
|
|
|
|
auto bi = &rp->beginInfo;
|
2023-06-21 08:58:15 +00:00
|
|
|
if (bi->framebuffer) {
|
|
|
|
dfunc->vkDestroyFramebuffer (device->dev,
|
|
|
|
bi->framebuffer, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-03-28 01:28:44 +00:00
|
|
|
DARRAY_CLEAR (&job->commands);
|
2023-06-24 05:55:46 +00:00
|
|
|
for (uint32_t i = 0; i < job->num_dsmanagers; i++) {
|
|
|
|
QFV_DSManager_Destroy (job->dsmanager[i]);
|
|
|
|
}
|
2023-03-22 10:32:49 +00:00
|
|
|
free (rctx->job);
|
2023-02-19 03:53:02 +00:00
|
|
|
}
|
2023-06-22 07:44:05 +00:00
|
|
|
|
|
|
|
for (uint32_t i = 0; i < rctx->frames.size; i++) {
|
|
|
|
auto dev = device->dev;
|
|
|
|
auto df = dfunc;
|
|
|
|
auto frame = &rctx->frames.a[i];
|
|
|
|
df->vkDestroyFence (dev, frame->fence, 0);
|
|
|
|
df->vkDestroySemaphore (dev, frame->imageAvailableSemaphore, 0);
|
|
|
|
df->vkDestroySemaphore (dev, frame->renderDoneSemaphore, 0);
|
2023-06-22 08:00:10 +00:00
|
|
|
QFV_CmdPoolManager_Shutdown (&frame->cmdpool);
|
2023-06-21 05:47:19 +00:00
|
|
|
}
|
2023-06-22 07:44:05 +00:00
|
|
|
DARRAY_CLEAR (&rctx->frames);
|
|
|
|
|
2023-03-22 10:32:49 +00:00
|
|
|
if (rctx->jobinfo) {
|
|
|
|
__auto_type jinfo = rctx->jobinfo;
|
2023-06-24 05:55:46 +00:00
|
|
|
for (uint32_t i = 0; i < jinfo->num_dslayouts; i++) {
|
|
|
|
__auto_type setLayout = jinfo->dslayouts[i].setLayout;
|
2023-02-27 05:15:28 +00:00
|
|
|
dfunc->vkDestroyDescriptorSetLayout (device->dev, setLayout, 0);
|
|
|
|
}
|
2023-03-22 10:32:49 +00:00
|
|
|
delete_memsuper (jinfo->memsuper);
|
2023-02-19 03:53:02 +00:00
|
|
|
}
|
|
|
|
if (rctx->task_functions.tab) {
|
|
|
|
Hash_DelTable (rctx->task_functions.tab);
|
|
|
|
}
|
2023-06-25 14:33:47 +00:00
|
|
|
if (rctx->samplerinfo) {
|
|
|
|
auto si = rctx->samplerinfo;
|
|
|
|
for (uint32_t i = 0; i < si->num_samplers; i++) {
|
|
|
|
auto sci = &si->samplers[i];
|
|
|
|
if (sci->sampler) {
|
|
|
|
dfunc->vkDestroySampler (device->dev, sci->sampler, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-03-05 09:19:54 +00:00
|
|
|
Hash_DelContext (rctx->hashctx);
|
2023-07-13 06:21:53 +00:00
|
|
|
|
|
|
|
QFV_Render_UI_Shutdown (ctx);
|
|
|
|
free (rctx);
|
2023-02-19 03:53:02 +00:00
|
|
|
}
|
|
|
|
|
2023-02-13 15:55:22 +00:00
|
|
|
void
|
|
|
|
QFV_Render_AddTasks (vulkan_ctx_t *ctx, exprsym_t *task_syms)
|
|
|
|
{
|
|
|
|
__auto_type rctx = ctx->render_context;
|
|
|
|
exprctx_t ectx = { .hashctx = &rctx->hashctx };
|
|
|
|
for (exprsym_t *sym = task_syms; sym->name; sym++) {
|
|
|
|
Hash_Add (rctx->task_functions.tab, sym);
|
|
|
|
for (exprfunc_t *f = sym->value; f->func; f++) {
|
|
|
|
for (int i = 0; i < f->num_params; i++) {
|
|
|
|
exprenum_t *e = f->param_types[i]->data;
|
|
|
|
if (e && !e->symtab->tab) {
|
|
|
|
cexpr_init_symtab (e->symtab, &ectx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-06-20 06:18:58 +00:00
|
|
|
|
2023-06-21 08:58:15 +00:00
|
|
|
qfv_step_t *
|
|
|
|
QFV_FindStep (const char *name, qfv_job_t *job)
|
|
|
|
{
|
|
|
|
for (uint32_t i = 0; i < job->num_steps; i++) {
|
|
|
|
auto step = &job->steps[i];
|
|
|
|
if (!strcmp (step->label.name, name)) {
|
|
|
|
return step;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-06-20 06:18:58 +00:00
|
|
|
qfv_step_t *
|
|
|
|
QFV_GetStep (const exprval_t *param, qfv_job_t *job)
|
|
|
|
{
|
|
|
|
// this is a little evil, but need to update the type after resolving
|
|
|
|
// the step name
|
|
|
|
auto stepref = (exprval_t *) param;
|
|
|
|
|
|
|
|
// cache the render step referenced, using the parameter type as a flag
|
|
|
|
// for whether the caching has been performed.
|
|
|
|
if (stepref->type == &cexpr_string) {
|
|
|
|
if (cexpr_string.size != cexpr_voidptr.size) {
|
|
|
|
Sys_Error ("string and voidptr incompatible sizes");
|
|
|
|
}
|
|
|
|
auto name = *(const char **)stepref->value;
|
|
|
|
stepref->type = &cexpr_voidptr;
|
2023-06-21 08:58:15 +00:00
|
|
|
*(qfv_step_t **)stepref->value = QFV_FindStep (name, job);
|
2023-06-20 06:18:58 +00:00
|
|
|
}
|
|
|
|
return *(qfv_step_t **)stepref->value;
|
|
|
|
}
|
2023-06-24 12:42:46 +00:00
|
|
|
|
|
|
|
qfv_dsmanager_t *
|
2023-06-25 14:33:47 +00:00
|
|
|
QFV_Render_DSManager (vulkan_ctx_t *ctx, const char *setName)
|
2023-06-24 12:42:46 +00:00
|
|
|
{
|
|
|
|
auto job = ctx->render_context->job;
|
|
|
|
for (uint32_t i = 0; i < job->num_dsmanagers; i++) {
|
|
|
|
auto ds = job->dsmanager[i];
|
|
|
|
if (!strcmp (ds->name, setName)) {
|
|
|
|
return ds;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2023-06-25 14:33:47 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
create_sampler (vulkan_ctx_t *ctx, qfv_samplercreateinfo_t *sampler)
|
|
|
|
{
|
|
|
|
VkSamplerCreateInfo create = {
|
|
|
|
.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO,
|
|
|
|
.flags = sampler->flags,
|
|
|
|
.magFilter = sampler->magFilter,
|
|
|
|
.minFilter = sampler->minFilter,
|
|
|
|
.mipmapMode = sampler->mipmapMode,
|
|
|
|
.addressModeU = sampler->addressModeU,
|
|
|
|
.addressModeV = sampler->addressModeV,
|
|
|
|
.addressModeW = sampler->addressModeW,
|
|
|
|
.mipLodBias = sampler->mipLodBias,
|
|
|
|
.anisotropyEnable = sampler->anisotropyEnable,
|
|
|
|
.maxAnisotropy = sampler->maxAnisotropy,
|
|
|
|
.compareEnable = sampler->compareEnable,
|
|
|
|
.compareOp = sampler->compareOp,
|
|
|
|
.minLod = sampler->minLod,
|
|
|
|
.maxLod = sampler->maxLod,
|
|
|
|
.borderColor = sampler->borderColor,
|
|
|
|
.unnormalizedCoordinates = sampler->unnormalizedCoordinates,
|
|
|
|
};
|
|
|
|
auto device = ctx->device;
|
|
|
|
auto dfunc = device->funcs;
|
|
|
|
dfunc->vkCreateSampler (device->dev, &create, 0, &sampler->sampler);
|
|
|
|
QFV_duSetObjectName (device, VK_OBJECT_TYPE_SAMPLER, sampler->sampler,
|
|
|
|
va (ctx->va_ctx, "sampler:%s", sampler->name));
|
|
|
|
}
|
|
|
|
|
|
|
|
VkSampler
|
|
|
|
QFV_Render_Sampler (vulkan_ctx_t *ctx, const char *name)
|
|
|
|
{
|
|
|
|
auto si = ctx->render_context->samplerinfo;
|
|
|
|
if (!si) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
for (uint32_t i = 0; i < si->num_samplers; i++) {
|
|
|
|
auto sci = &si->samplers[i];
|
|
|
|
if (!strcmp (sci->name, name)) {
|
|
|
|
if (!sci->sampler) {
|
|
|
|
create_sampler (ctx, sci);
|
|
|
|
}
|
|
|
|
return sci->sampler;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf ("sampler %s not found\n", name);
|
|
|
|
return 0;
|
|
|
|
}
|