[vulkan] Move swap chain image acquisition to the output module

Swap chain acquisition is part of final output handling. However, as the
correct frame buffers are required for the render passes, the
acquisition needs to be performed during the preoutput render pass.
Window resize is still broken, but this is a big step towards fixing it.
This commit is contained in:
Bill Currie 2022-11-25 16:08:15 +09:00
parent 92b36582ca
commit 0fdba75a6e
2 changed files with 50 additions and 31 deletions

View File

@ -297,7 +297,6 @@ vulkan_Draw_FontString (int x, int y, int fontid, const char *str)
static void
vulkan_begin_frame (void)
{
uint32_t imageIndex = 0;
qfv_device_t *device = vulkan_ctx->device;
qfv_devfuncs_t *dfunc = device->funcs;
VkDevice dev = device->dev;
@ -305,43 +304,16 @@ vulkan_begin_frame (void)
__auto_type frame = &vulkan_ctx->frames.a[vulkan_ctx->curFrame];
dfunc->vkWaitForFences (dev, 1, &frame->fence, VK_TRUE, 2000000000);
if (!QFV_AcquireNextImage (vulkan_ctx->swapchain,
frame->imageAvailableSemaphore,
0, &imageIndex)) {
QFV_DeviceWaitIdle (device);
if (vulkan_ctx->capture) {
QFV_DestroyCapture (vulkan_ctx->capture);
}
Vulkan_CreateSwapchain (vulkan_ctx);
Vulkan_CreateCapture (vulkan_ctx);
//FIXME
qfv_output_t output = {
.extent = vulkan_ctx->swapchain->extent,
.view = vulkan_ctx->swapchain->imageViews->a[0],
.format = vulkan_ctx->swapchain->format,
.view_list = vulkan_ctx->swapchain->imageViews->a,
};
vulkan_ctx->output_renderpass->viewport.width = output.extent.width;
vulkan_ctx->output_renderpass->viewport.height = output.extent.height;
vulkan_ctx->output_renderpass->scissor.extent = output.extent;
Vulkan_Script_SetOutput (vulkan_ctx, &output);
Vulkan_CreateAttachments (vulkan_ctx, vulkan_ctx->output_renderpass);
QFV_AcquireNextImage (vulkan_ctx->swapchain,
frame->imageAvailableSemaphore,
0, &imageIndex);
}
vulkan_ctx->swapImageIndex = imageIndex;
}
static void
vulkan_render_view (void)
{
uint32_t imageIndex = vulkan_ctx->swapImageIndex;
for (size_t i = 0; i < vulkan_ctx->renderPasses.size; i++) {
__auto_type rp = vulkan_ctx->renderPasses.a[i];
__auto_type rpFrame = &rp->frames.a[vulkan_ctx->curFrame];
// swapImageIndex may be updated by a render pass
uint32_t imageIndex = vulkan_ctx->swapImageIndex;
if (rp->framebuffers) {
rpFrame->framebuffer = rp->framebuffers->a[imageIndex];
}

View File

@ -46,6 +46,7 @@
#include "QF/Vulkan/qf_output.h"
#include "QF/Vulkan/qf_renderpass.h"
#include "QF/Vulkan/qf_vid.h"
#include "QF/Vulkan/capture.h"
#include "QF/Vulkan/debug.h"
#include "QF/Vulkan/descriptor.h"
#include "QF/Vulkan/device.h"
@ -56,6 +57,45 @@
#include "vid_vulkan.h"
#include "vkparse.h"//FIXME
static void
acquire_image (qfv_renderframe_t *rFrame)
{
vulkan_ctx_t *ctx = rFrame->vulkan_ctx;
qfv_device_t *device = ctx->device;
//qfv_devfuncs_t *dfunc = device->funcs;
__auto_type frame = &ctx->frames.a[ctx->curFrame];
//outputctx_t *octx = ctx->output_context;
//uint32_t curFrame = ctx->curFrame;
//outputframe_t *oframe = &octx->frames.a[curFrame];
//qfv_renderpass_t *rp = ctx->output_renderpass;
uint32_t imageIndex = 0;
while (!QFV_AcquireNextImage (ctx->swapchain,
frame->imageAvailableSemaphore,
0, &imageIndex)) {
QFV_DeviceWaitIdle (device);
if (ctx->capture) {
QFV_DestroyCapture (ctx->capture);
}
Vulkan_CreateSwapchain (ctx);
Vulkan_CreateCapture (ctx);
//FIXME
qfv_output_t output = {
.extent = ctx->swapchain->extent,
.view = ctx->swapchain->imageViews->a[0],
.format = ctx->swapchain->format,
.view_list = ctx->swapchain->imageViews->a,
};
ctx->output_renderpass->viewport.width = output.extent.width;
ctx->output_renderpass->viewport.height = output.extent.height;
ctx->output_renderpass->scissor.extent = output.extent;
Vulkan_Script_SetOutput (ctx, &output);
Vulkan_CreateAttachments (ctx, ctx->output_renderpass);
}
ctx->swapImageIndex = imageIndex;
}
static void
update_input (qfv_renderframe_t *rFrame)
{
@ -85,6 +125,13 @@ update_input (qfv_renderframe_t *rFrame)
dfunc->vkUpdateDescriptorSets (device->dev, 1, write, 0, 0);
}
static void
preoutput_draw (qfv_renderframe_t *rFrame)
{
acquire_image (rFrame);
update_input (rFrame);
}
static void
output_draw (qfv_renderframe_t *rFrame)
{
@ -164,7 +211,7 @@ Vulkan_Output_CreateRenderPasses (vulkan_ctx_t *ctx)
DARRAY_APPEND (&ctx->renderPasses, out);
__auto_type pre = Vulkan_CreateFunctionPass (ctx, "preoutput",
update_input);
preoutput_draw);
pre->order = QFV_rp_preoutput;
DARRAY_APPEND (&ctx->renderPasses, pre);
}