2021-02-24 10:58:31 +00:00
|
|
|
/*
|
|
|
|
vulkan_lighting.c
|
|
|
|
|
|
|
|
Vulkan lighting pass pipeline
|
|
|
|
|
|
|
|
Copyright (C) 2021 Bill Currie <bill@taniwha.org>
|
|
|
|
|
|
|
|
Author: Bill Currie <bill@taniwha.org>
|
|
|
|
Date: 2021/2/23
|
|
|
|
|
|
|
|
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_STRING_H
|
|
|
|
# include <string.h>
|
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRINGS_H
|
|
|
|
# include <strings.h>
|
|
|
|
#endif
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "qfalloca.h"
|
|
|
|
|
2021-12-02 13:48:50 +00:00
|
|
|
#include "QF/cvar.h"
|
2021-03-03 09:14:16 +00:00
|
|
|
#include "QF/dstring.h"
|
2022-05-10 02:15:52 +00:00
|
|
|
#include "QF/heapsort.h"
|
2021-03-21 07:13:03 +00:00
|
|
|
#include "QF/plist.h"
|
2021-03-03 09:14:16 +00:00
|
|
|
#include "QF/progs.h"
|
|
|
|
#include "QF/script.h"
|
2021-07-26 02:15:51 +00:00
|
|
|
#include "QF/set.h"
|
2021-02-24 10:58:31 +00:00
|
|
|
#include "QF/sys.h"
|
2021-02-25 06:51:54 +00:00
|
|
|
#include "QF/va.h"
|
2021-02-24 10:58:31 +00:00
|
|
|
|
2022-05-05 05:58:47 +00:00
|
|
|
#include "QF/scene/scene.h"
|
2022-05-27 01:19:15 +00:00
|
|
|
#include "QF/ui/view.h"
|
2022-05-05 05:58:47 +00:00
|
|
|
|
2022-05-27 01:19:15 +00:00
|
|
|
#include "QF/Vulkan/qf_draw.h"
|
2021-02-24 10:58:31 +00:00
|
|
|
#include "QF/Vulkan/qf_lighting.h"
|
2022-05-30 03:10:00 +00:00
|
|
|
#include "QF/Vulkan/qf_renderpass.h"
|
2021-04-24 01:40:39 +00:00
|
|
|
#include "QF/Vulkan/qf_texture.h"
|
2021-04-24 06:47:31 +00:00
|
|
|
#include "QF/Vulkan/barrier.h"
|
2021-02-25 06:51:54 +00:00
|
|
|
#include "QF/Vulkan/buffer.h"
|
2021-02-24 10:58:31 +00:00
|
|
|
#include "QF/Vulkan/debug.h"
|
|
|
|
#include "QF/Vulkan/descriptor.h"
|
|
|
|
#include "QF/Vulkan/device.h"
|
|
|
|
#include "QF/Vulkan/image.h"
|
2021-04-29 11:54:38 +00:00
|
|
|
#include "QF/Vulkan/instance.h"
|
|
|
|
#include "QF/Vulkan/projection.h"
|
2023-02-13 15:55:22 +00:00
|
|
|
#include "QF/Vulkan/render.h"
|
2022-10-27 02:02:45 +00:00
|
|
|
#include "QF/Vulkan/resource.h"
|
2021-02-25 06:51:54 +00:00
|
|
|
#include "QF/Vulkan/staging.h"
|
|
|
|
|
|
|
|
#include "compat.h"
|
2021-02-24 10:58:31 +00:00
|
|
|
|
|
|
|
#include "r_internal.h"
|
|
|
|
#include "vid_vulkan.h"
|
2022-05-31 03:43:04 +00:00
|
|
|
#include "vkparse.h"
|
2021-02-24 10:58:31 +00:00
|
|
|
|
2021-02-25 06:51:54 +00:00
|
|
|
static void
|
|
|
|
update_lights (vulkan_ctx_t *ctx)
|
|
|
|
{
|
|
|
|
qfv_device_t *device = ctx->device;
|
|
|
|
qfv_devfuncs_t *dfunc = device->funcs;
|
|
|
|
lightingctx_t *lctx = ctx->lighting_context;
|
2022-05-05 05:58:47 +00:00
|
|
|
lightingdata_t *ldata = lctx->ldata;
|
2021-02-25 06:51:54 +00:00
|
|
|
lightingframe_t *lframe = &lctx->frames.a[ctx->curFrame];
|
|
|
|
|
2022-05-05 05:58:47 +00:00
|
|
|
Light_FindVisibleLights (ldata);
|
2021-03-20 07:08:44 +00:00
|
|
|
|
2021-04-29 11:54:38 +00:00
|
|
|
dlight_t *lights[MaxLights];
|
2021-02-25 06:51:54 +00:00
|
|
|
qfv_packet_t *packet = QFV_PacketAcquire (ctx->staging);
|
|
|
|
qfv_light_buffer_t *light_data = QFV_PacketExtend (packet,
|
|
|
|
sizeof (*light_data));
|
|
|
|
|
2022-05-04 23:40:02 +00:00
|
|
|
float style_intensities[NumStyles];
|
2021-04-29 11:54:38 +00:00
|
|
|
for (int i = 0; i < NumStyles; i++) {
|
2022-05-04 23:40:02 +00:00
|
|
|
style_intensities[i] = d_lightstylevalue[i] / 65536.0;
|
2021-03-22 04:13:26 +00:00
|
|
|
}
|
2021-04-25 01:01:57 +00:00
|
|
|
|
2021-02-25 06:51:54 +00:00
|
|
|
light_data->lightCount = 0;
|
2022-03-30 14:50:12 +00:00
|
|
|
R_FindNearLights (r_refdef.frame.position, MaxLights - 1, lights);
|
2021-04-29 11:54:38 +00:00
|
|
|
for (int i = 0; i < MaxLights - 1; i++) {
|
2021-02-25 06:51:54 +00:00
|
|
|
if (!lights[i]) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
light_data->lightCount++;
|
|
|
|
VectorCopy (lights[i]->color, light_data->lights[i].color);
|
2022-05-04 23:40:02 +00:00
|
|
|
// dynamic lights seem a tad faint, so 16x map lights
|
|
|
|
light_data->lights[i].color[3] = lights[i]->radius / 16;
|
2021-02-25 06:51:54 +00:00
|
|
|
VectorCopy (lights[i]->origin, light_data->lights[i].position);
|
2022-05-05 10:55:35 +00:00
|
|
|
// dlights are local point sources
|
|
|
|
light_data->lights[i].position[3] = 1;
|
2022-05-04 23:40:02 +00:00
|
|
|
light_data->lights[i].attenuation =
|
|
|
|
(vec4f_t) { 0, 0, 1, 1/lights[i]->radius };
|
2022-05-05 10:55:35 +00:00
|
|
|
// full sphere, normal light (not ambient)
|
|
|
|
light_data->lights[i].direction = (vec4f_t) { 0, 0, 1, 1 };
|
2021-02-25 06:51:54 +00:00
|
|
|
}
|
2022-05-05 05:58:47 +00:00
|
|
|
for (size_t i = 0; (i < ldata->lightvis.size
|
2021-04-29 11:54:38 +00:00
|
|
|
&& light_data->lightCount < MaxLights); i++) {
|
2022-05-05 05:58:47 +00:00
|
|
|
if (ldata->lightvis.a[i]) {
|
|
|
|
light_t *light = &light_data->lights[light_data->lightCount++];
|
|
|
|
*light = ldata->lights.a[i];
|
|
|
|
light->color[3] *= style_intensities[ldata->lightstyles.a[i]];
|
2021-03-20 07:08:44 +00:00
|
|
|
}
|
|
|
|
}
|
2022-05-27 01:19:15 +00:00
|
|
|
if (developer & SYS_lighting) {
|
2022-09-22 01:33:21 +00:00
|
|
|
Vulkan_Draw_String (vid.width - 32, 8,
|
2022-05-27 01:19:15 +00:00
|
|
|
va (ctx->va_ctx, "%3d", light_data->lightCount),
|
|
|
|
ctx);
|
|
|
|
}
|
2021-02-25 06:51:54 +00:00
|
|
|
|
2021-04-24 06:47:31 +00:00
|
|
|
qfv_bufferbarrier_t bb = bufferBarriers[qfv_BB_Unknown_to_TransferWrite];
|
|
|
|
bb.barrier.buffer = lframe->light_buffer;
|
|
|
|
bb.barrier.size = sizeof (qfv_light_buffer_t);
|
|
|
|
dfunc->vkCmdPipelineBarrier (packet->cmd, bb.srcStages, bb.dstStages,
|
|
|
|
0, 0, 0, 1, &bb.barrier, 0, 0);
|
2021-02-25 06:51:54 +00:00
|
|
|
VkBufferCopy copy_region[] = {
|
|
|
|
{ packet->offset, 0, sizeof (qfv_light_buffer_t) },
|
|
|
|
};
|
|
|
|
dfunc->vkCmdCopyBuffer (packet->cmd, ctx->staging->buffer,
|
|
|
|
lframe->light_buffer, 1, ©_region[0]);
|
2021-04-24 06:47:31 +00:00
|
|
|
bb = bufferBarriers[qfv_BB_TransferWrite_to_UniformRead];
|
|
|
|
bb.barrier.buffer = lframe->light_buffer;
|
|
|
|
bb.barrier.size = sizeof (qfv_light_buffer_t);
|
|
|
|
dfunc->vkCmdPipelineBarrier (packet->cmd, bb.srcStages, bb.dstStages,
|
|
|
|
0, 0, 0, 1, &bb.barrier, 0, 0);
|
2021-02-25 06:51:54 +00:00
|
|
|
QFV_PacketSubmit (packet);
|
|
|
|
}
|
|
|
|
|
2021-02-24 10:58:31 +00:00
|
|
|
void
|
2021-12-02 12:58:29 +00:00
|
|
|
Vulkan_Lighting_Draw (qfv_renderframe_t *rFrame)
|
2021-02-24 10:58:31 +00:00
|
|
|
{
|
2021-12-02 12:58:29 +00:00
|
|
|
vulkan_ctx_t *ctx = rFrame->vulkan_ctx;
|
2021-02-24 10:58:31 +00:00
|
|
|
qfv_device_t *device = ctx->device;
|
|
|
|
qfv_devfuncs_t *dfunc = device->funcs;
|
2023-06-15 04:13:52 +00:00
|
|
|
qfv_orenderpass_t *renderpass = rFrame->renderpass;
|
2022-05-05 05:58:47 +00:00
|
|
|
lightingctx_t *lctx = ctx->lighting_context;
|
2021-02-24 10:58:31 +00:00
|
|
|
|
2022-05-05 05:58:47 +00:00
|
|
|
if (!lctx->scene) {
|
|
|
|
return;
|
|
|
|
}
|
2022-05-05 12:24:22 +00:00
|
|
|
if (lctx->scene->lights) {
|
|
|
|
update_lights (ctx);
|
|
|
|
}
|
2021-02-25 06:51:54 +00:00
|
|
|
|
2021-02-24 10:58:31 +00:00
|
|
|
lightingframe_t *lframe = &lctx->frames.a[ctx->curFrame];
|
|
|
|
VkCommandBuffer cmd = lframe->cmd;
|
|
|
|
|
2021-12-02 12:58:29 +00:00
|
|
|
DARRAY_APPEND (&rFrame->subpassCmdSets[QFV_passLighting], cmd);
|
2021-02-24 10:58:31 +00:00
|
|
|
|
|
|
|
dfunc->vkResetCommandBuffer (cmd, 0);
|
|
|
|
VkCommandBufferInheritanceInfo inherit = {
|
|
|
|
VK_STRUCTURE_TYPE_COMMAND_BUFFER_INHERITANCE_INFO, 0,
|
2021-12-02 12:58:29 +00:00
|
|
|
renderpass->renderpass, QFV_passLighting,
|
2022-11-21 08:25:55 +00:00
|
|
|
rFrame->framebuffer,
|
2021-02-24 10:58:31 +00:00
|
|
|
0, 0, 0,
|
|
|
|
};
|
|
|
|
VkCommandBufferBeginInfo beginInfo = {
|
|
|
|
VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO, 0,
|
|
|
|
VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT
|
|
|
|
| VK_COMMAND_BUFFER_USAGE_RENDER_PASS_CONTINUE_BIT, &inherit,
|
|
|
|
};
|
|
|
|
dfunc->vkBeginCommandBuffer (cmd, &beginInfo);
|
|
|
|
|
2021-04-18 11:40:43 +00:00
|
|
|
QFV_duCmdBeginLabel (device, cmd, "lighting", { 0.6, 0.5, 0.6, 1});
|
|
|
|
|
2021-02-24 10:58:31 +00:00
|
|
|
dfunc->vkCmdBindPipeline (cmd, VK_PIPELINE_BIND_POINT_GRAPHICS,
|
|
|
|
lctx->pipeline);
|
|
|
|
|
2021-02-25 06:51:54 +00:00
|
|
|
lframe->bufferInfo[0].buffer = lframe->light_buffer;
|
2021-12-02 12:58:29 +00:00
|
|
|
lframe->attachInfo[0].imageView
|
|
|
|
= renderpass->attachment_views->a[QFV_attachDepth];
|
|
|
|
lframe->attachInfo[1].imageView
|
|
|
|
= renderpass->attachment_views->a[QFV_attachColor];
|
2021-04-24 01:40:39 +00:00
|
|
|
lframe->attachInfo[2].imageView
|
2021-12-02 12:58:29 +00:00
|
|
|
= renderpass->attachment_views->a[QFV_attachEmission];
|
2021-04-24 01:40:39 +00:00
|
|
|
lframe->attachInfo[3].imageView
|
2021-12-02 12:58:29 +00:00
|
|
|
= renderpass->attachment_views->a[QFV_attachNormal];
|
2021-04-24 01:40:39 +00:00
|
|
|
lframe->attachInfo[4].imageView
|
2021-12-02 12:58:29 +00:00
|
|
|
= renderpass->attachment_views->a[QFV_attachPosition];
|
2021-02-25 06:51:54 +00:00
|
|
|
dfunc->vkUpdateDescriptorSets (device->dev,
|
2021-04-24 01:40:39 +00:00
|
|
|
LIGHTING_DESCRIPTORS,
|
2021-02-25 06:51:54 +00:00
|
|
|
lframe->descriptors, 0, 0);
|
2021-02-24 10:58:31 +00:00
|
|
|
|
|
|
|
VkDescriptorSet sets[] = {
|
2021-04-24 01:40:39 +00:00
|
|
|
lframe->attachWrite[0].dstSet,
|
|
|
|
lframe->bufferWrite[0].dstSet,
|
|
|
|
lframe->shadowWrite.dstSet,
|
2021-02-24 10:58:31 +00:00
|
|
|
};
|
|
|
|
dfunc->vkCmdBindDescriptorSets (cmd, VK_PIPELINE_BIND_POINT_GRAPHICS,
|
2021-04-24 01:40:39 +00:00
|
|
|
lctx->layout, 0, 3, sets, 0, 0);
|
2021-02-24 10:58:31 +00:00
|
|
|
|
2022-04-01 11:34:41 +00:00
|
|
|
dfunc->vkCmdSetViewport (cmd, 0, 1, &rFrame->renderpass->viewport);
|
|
|
|
dfunc->vkCmdSetScissor (cmd, 0, 1, &rFrame->renderpass->scissor);
|
2021-02-24 10:58:31 +00:00
|
|
|
|
2022-04-01 10:50:41 +00:00
|
|
|
dfunc->vkCmdDraw (cmd, 3, 1, 0, 0);
|
2021-02-24 10:58:31 +00:00
|
|
|
|
2021-04-18 11:40:43 +00:00
|
|
|
QFV_duCmdEndLabel (device, cmd);
|
2021-02-24 10:58:31 +00:00
|
|
|
dfunc->vkEndCommandBuffer (cmd);
|
|
|
|
}
|
|
|
|
|
2022-05-31 03:43:04 +00:00
|
|
|
static void
|
|
|
|
lighting_draw_maps (qfv_renderframe_t *rFrame)
|
|
|
|
{
|
|
|
|
vulkan_ctx_t *ctx = rFrame->vulkan_ctx;
|
|
|
|
qfv_device_t *device = ctx->device;
|
|
|
|
qfv_devfuncs_t *dfunc = device->funcs;
|
|
|
|
lightingctx_t *lctx = ctx->lighting_context;
|
|
|
|
|
|
|
|
if (rFrame->subpassCmdSets[0].size) {
|
|
|
|
__auto_type sets = &rFrame->subpassCmdSets[0];
|
|
|
|
dfunc->vkFreeCommandBuffers (device->dev, lctx->cmdpool,
|
|
|
|
sets->size, sets->a);
|
|
|
|
sets->size = 0;
|
|
|
|
}
|
|
|
|
|
2022-10-27 02:02:45 +00:00
|
|
|
if (!lctx->ldata || !lctx->ldata->lights.size) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!lctx->light_renderers.a[0].renderPass) {
|
|
|
|
//FIXME goes away when lighting implemented properly
|
2022-05-31 03:43:04 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
__auto_type bufferset = QFV_AllocCommandBufferSet (1, alloca);
|
|
|
|
QFV_AllocateCommandBuffers (device, lctx->cmdpool, 0, bufferset);
|
|
|
|
VkCommandBuffer cmd = bufferset->a[0];
|
|
|
|
QFV_duSetObjectName (device, VK_OBJECT_TYPE_COMMAND_BUFFER,
|
2022-11-25 07:02:39 +00:00
|
|
|
cmd, va (ctx->va_ctx, "lighting:%d", ctx->curFrame));
|
2022-05-31 03:43:04 +00:00
|
|
|
|
|
|
|
VkCommandBufferBeginInfo beginInfo = {
|
|
|
|
.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
|
|
|
|
};
|
|
|
|
dfunc->vkBeginCommandBuffer (cmd, &beginInfo);
|
|
|
|
|
|
|
|
__auto_type rp = rFrame->renderpass;
|
|
|
|
QFV_CmdBeginLabel (device, cmd, rp->name, rp->color);
|
|
|
|
|
|
|
|
__auto_type lr = &lctx->light_renderers.a[0];
|
|
|
|
VkRenderPassBeginInfo renderPassInfo = {
|
|
|
|
.sType = VK_STRUCTURE_TYPE_RENDER_PASS_BEGIN_INFO,
|
|
|
|
.renderArea = { {0, 0}, {lr->size, lr->size} },
|
|
|
|
.framebuffer = lr->framebuffer,
|
|
|
|
.renderPass = lr->renderPass,
|
|
|
|
.pClearValues = lctx->qfv_renderpass->clearValues->a,
|
|
|
|
};
|
|
|
|
__auto_type subpassContents = VK_SUBPASS_CONTENTS_SECONDARY_COMMAND_BUFFERS;
|
2022-10-22 03:59:18 +00:00
|
|
|
if (renderPassInfo.renderPass) {
|
|
|
|
dfunc->vkCmdBeginRenderPass (cmd, &renderPassInfo, subpassContents);
|
|
|
|
//...
|
|
|
|
dfunc->vkCmdEndRenderPass (cmd);
|
|
|
|
}
|
2022-05-31 03:43:04 +00:00
|
|
|
QFV_CmdEndLabel (device, cmd);
|
|
|
|
dfunc->vkEndCommandBuffer (cmd);
|
|
|
|
|
|
|
|
DARRAY_APPEND (&rFrame->subpassCmdSets[0], cmd);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Vulkan_Lighting_CreateRenderPasses (vulkan_ctx_t *ctx)
|
|
|
|
{
|
|
|
|
lightingctx_t *lctx = calloc (1, sizeof (lightingctx_t));
|
|
|
|
ctx->lighting_context = lctx;
|
|
|
|
|
|
|
|
// extents are dynamic and filled in for each light
|
|
|
|
// frame buffers are highly dynamic
|
2022-11-26 13:23:59 +00:00
|
|
|
__auto_type rp = QFV_RenderPass_New (ctx, "shadow", lighting_draw_maps);
|
|
|
|
QFV_RenderPass_CreateRenderPass (rp);
|
2022-05-31 03:43:04 +00:00
|
|
|
rp->primary_commands = 1;
|
|
|
|
rp->order = QFV_rp_shadowmap;
|
|
|
|
DARRAY_APPEND (&ctx->renderPasses, rp);
|
|
|
|
|
|
|
|
lctx->qfv_renderpass = rp;
|
|
|
|
}
|
|
|
|
|
2021-02-24 10:58:31 +00:00
|
|
|
static VkDescriptorBufferInfo base_buffer_info = {
|
|
|
|
0, 0, VK_WHOLE_SIZE
|
|
|
|
};
|
|
|
|
static VkDescriptorImageInfo base_image_info = {
|
|
|
|
0, 0, VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL
|
|
|
|
};
|
|
|
|
static VkWriteDescriptorSet base_buffer_write = {
|
|
|
|
VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, 0, 0,
|
|
|
|
0, 0, 1,
|
|
|
|
VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER,
|
|
|
|
0, 0, 0
|
|
|
|
};
|
2021-04-24 01:40:39 +00:00
|
|
|
static VkWriteDescriptorSet base_attachment_write = {
|
2021-02-24 10:58:31 +00:00
|
|
|
VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, 0, 0,
|
|
|
|
0, 0, 1,
|
|
|
|
VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT,
|
|
|
|
0, 0, 0
|
|
|
|
};
|
2021-04-24 01:40:39 +00:00
|
|
|
static VkWriteDescriptorSet base_image_write = {
|
|
|
|
VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET, 0, 0,
|
|
|
|
0, 0, 1,
|
|
|
|
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER,
|
|
|
|
0, 0, 0
|
|
|
|
};
|
2021-02-24 10:58:31 +00:00
|
|
|
|
2023-02-13 15:55:22 +00:00
|
|
|
static void
|
|
|
|
lights_draw (const exprval_t **params, exprval_t *result, exprctx_t *ectx)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static exprfunc_t lights_draw_func[] = {
|
|
|
|
{ .func = lights_draw },
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
static exprsym_t lighting_task_syms[] = {
|
|
|
|
{ "lights_draw", &cexpr_function, lights_draw_func },
|
|
|
|
{}
|
|
|
|
};
|
|
|
|
|
2021-02-24 10:58:31 +00:00
|
|
|
void
|
|
|
|
Vulkan_Lighting_Init (vulkan_ctx_t *ctx)
|
|
|
|
{
|
|
|
|
qfv_device_t *device = ctx->device;
|
2021-02-25 06:51:54 +00:00
|
|
|
qfv_devfuncs_t *dfunc = device->funcs;
|
2021-02-24 10:58:31 +00:00
|
|
|
|
2021-12-02 13:48:50 +00:00
|
|
|
qfvPushDebug (ctx, "lighting init");
|
2023-02-13 15:55:22 +00:00
|
|
|
QFV_Render_AddTasks (ctx, lighting_task_syms);
|
2021-12-02 13:48:50 +00:00
|
|
|
|
2022-05-31 03:43:04 +00:00
|
|
|
// lighting_context initialized in Vulkan_Lighting_CreateRenderPasses
|
|
|
|
|
2022-11-24 14:44:07 +00:00
|
|
|
Vulkan_Script_SetOutput (ctx,
|
|
|
|
&(qfv_output_t) { .format = VK_FORMAT_X8_D24_UNORM_PACK32 });
|
2022-05-31 03:43:04 +00:00
|
|
|
lightingctx_t *lctx = ctx->lighting_context;
|
|
|
|
plitem_t *rp_def = lctx->qfv_renderpass->renderpassDef;
|
|
|
|
plitem_t *rp_cfg = PL_ObjectForKey (rp_def, "renderpass_6");
|
|
|
|
lctx->renderpass_6 = QFV_ParseRenderPass (ctx, rp_cfg, rp_def);
|
|
|
|
rp_cfg = PL_ObjectForKey (rp_def, "renderpass_4");
|
|
|
|
lctx->renderpass_4 = QFV_ParseRenderPass (ctx, rp_cfg, rp_def);
|
|
|
|
rp_cfg = PL_ObjectForKey (rp_def, "renderpass_1");
|
|
|
|
lctx->renderpass_1 = QFV_ParseRenderPass (ctx, rp_cfg, rp_def);
|
|
|
|
|
|
|
|
lctx->cmdpool = QFV_CreateCommandPool (device, device->queue.queueFamily,
|
|
|
|
1, 1);
|
2021-02-24 10:58:31 +00:00
|
|
|
|
2022-05-31 03:43:04 +00:00
|
|
|
DARRAY_INIT (&lctx->light_mats, 16);
|
|
|
|
DARRAY_INIT (&lctx->light_images, 16);
|
|
|
|
DARRAY_INIT (&lctx->light_renderers, 16);
|
2021-03-03 09:14:16 +00:00
|
|
|
|
2021-02-24 10:58:31 +00:00
|
|
|
size_t frames = ctx->frames.size;
|
|
|
|
DARRAY_INIT (&lctx->frames, frames);
|
|
|
|
DARRAY_RESIZE (&lctx->frames, frames);
|
|
|
|
lctx->frames.grow = 0;
|
|
|
|
|
2021-12-16 14:00:17 +00:00
|
|
|
lctx->pipeline = Vulkan_CreateGraphicsPipeline (ctx, "lighting");
|
2021-02-24 10:58:31 +00:00
|
|
|
lctx->layout = Vulkan_CreatePipelineLayout (ctx, "lighting_layout");
|
2021-04-24 01:40:39 +00:00
|
|
|
lctx->sampler = Vulkan_CreateSampler (ctx, "shadow_sampler");
|
2021-02-24 10:58:31 +00:00
|
|
|
|
2021-02-25 06:51:54 +00:00
|
|
|
__auto_type lbuffers = QFV_AllocBufferSet (frames, alloca);
|
|
|
|
for (size_t i = 0; i < frames; i++) {
|
|
|
|
lbuffers->a[i] = QFV_CreateBuffer (device, sizeof (qfv_light_buffer_t),
|
|
|
|
VK_BUFFER_USAGE_UNIFORM_BUFFER_BIT
|
|
|
|
| VK_BUFFER_USAGE_TRANSFER_DST_BIT);
|
|
|
|
QFV_duSetObjectName (device, VK_OBJECT_TYPE_BUFFER,
|
|
|
|
lbuffers->a[i],
|
|
|
|
va (ctx->va_ctx, "buffer:lighting:%zd", i));
|
|
|
|
}
|
|
|
|
VkMemoryRequirements requirements;
|
|
|
|
dfunc->vkGetBufferMemoryRequirements (device->dev, lbuffers->a[0],
|
|
|
|
&requirements);
|
2021-12-15 01:45:25 +00:00
|
|
|
size_t light_size = QFV_NextOffset (requirements.size, &requirements);
|
2021-02-25 06:51:54 +00:00
|
|
|
lctx->light_memory = QFV_AllocBufferMemory (device, lbuffers->a[0],
|
2021-11-30 12:00:33 +00:00
|
|
|
VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
|
2021-12-15 01:45:25 +00:00
|
|
|
frames * light_size, 0);
|
2021-02-25 06:51:54 +00:00
|
|
|
QFV_duSetObjectName (device, VK_OBJECT_TYPE_DEVICE_MEMORY,
|
|
|
|
lctx->light_memory, "memory:lighting");
|
|
|
|
|
|
|
|
|
2021-02-24 10:58:31 +00:00
|
|
|
__auto_type cmdSet = QFV_AllocCommandBufferSet (1, alloca);
|
|
|
|
|
|
|
|
__auto_type attach = QFV_AllocDescriptorSetLayoutSet (frames, alloca);
|
|
|
|
__auto_type lights = QFV_AllocDescriptorSetLayoutSet (frames, alloca);
|
2021-04-24 01:40:39 +00:00
|
|
|
__auto_type shadow = QFV_AllocDescriptorSetLayoutSet (frames, alloca);
|
2021-02-24 10:58:31 +00:00
|
|
|
for (size_t i = 0; i < frames; i++) {
|
|
|
|
attach->a[i] = Vulkan_CreateDescriptorSetLayout (ctx,
|
|
|
|
"lighting_attach");
|
|
|
|
lights->a[i] = Vulkan_CreateDescriptorSetLayout (ctx,
|
|
|
|
"lighting_lights");
|
2021-04-24 01:40:39 +00:00
|
|
|
shadow->a[i] = Vulkan_CreateDescriptorSetLayout (ctx,
|
|
|
|
"lighting_shadow");
|
2021-02-24 10:58:31 +00:00
|
|
|
}
|
|
|
|
__auto_type attach_pool = Vulkan_CreateDescriptorPool (ctx,
|
|
|
|
"lighting_attach_pool");
|
|
|
|
__auto_type lights_pool = Vulkan_CreateDescriptorPool (ctx,
|
|
|
|
"lighting_lights_pool");
|
2021-04-24 01:40:39 +00:00
|
|
|
__auto_type shadow_pool = Vulkan_CreateDescriptorPool (ctx,
|
|
|
|
"lighting_shadow_pool");
|
2021-02-24 10:58:31 +00:00
|
|
|
|
|
|
|
__auto_type attach_set = QFV_AllocateDescriptorSet (device, attach_pool,
|
|
|
|
attach);
|
|
|
|
__auto_type lights_set = QFV_AllocateDescriptorSet (device, lights_pool,
|
|
|
|
lights);
|
2021-04-24 01:40:39 +00:00
|
|
|
__auto_type shadow_set = QFV_AllocateDescriptorSet (device, shadow_pool,
|
|
|
|
shadow);
|
2021-11-30 12:00:33 +00:00
|
|
|
VkDeviceSize light_offset = 0;
|
2021-02-24 10:58:31 +00:00
|
|
|
for (size_t i = 0; i < frames; i++) {
|
|
|
|
__auto_type lframe = &lctx->frames.a[i];
|
|
|
|
|
2021-04-24 01:40:39 +00:00
|
|
|
QFV_duSetObjectName (device, VK_OBJECT_TYPE_DESCRIPTOR_SET,
|
|
|
|
attach_set->a[i],
|
|
|
|
va (ctx->va_ctx, "lighting:attach_set:%zd", i));
|
|
|
|
QFV_duSetObjectName (device, VK_OBJECT_TYPE_DESCRIPTOR_SET,
|
|
|
|
lights_set->a[i],
|
|
|
|
va (ctx->va_ctx, "lighting:lights_set:%zd", i));
|
|
|
|
QFV_duSetObjectName (device, VK_OBJECT_TYPE_DESCRIPTOR_SET,
|
|
|
|
shadow_set->a[i],
|
|
|
|
va (ctx->va_ctx, "lighting:shadow_set:%zd", i));
|
|
|
|
|
2021-02-24 10:58:31 +00:00
|
|
|
QFV_AllocateCommandBuffers (device, ctx->cmdpool, 1, cmdSet);
|
|
|
|
lframe->cmd = cmdSet->a[0];
|
|
|
|
|
2021-02-25 06:51:54 +00:00
|
|
|
lframe->light_buffer = lbuffers->a[i];
|
|
|
|
QFV_BindBufferMemory (device, lbuffers->a[i], lctx->light_memory,
|
2021-11-30 12:00:33 +00:00
|
|
|
light_offset);
|
2021-12-15 01:45:25 +00:00
|
|
|
light_offset += light_size;
|
2021-02-25 06:51:54 +00:00
|
|
|
|
2021-02-24 10:58:31 +00:00
|
|
|
QFV_duSetObjectName (device, VK_OBJECT_TYPE_COMMAND_BUFFER,
|
|
|
|
lframe->cmd, "cmd:lighting");
|
|
|
|
for (int j = 0; j < LIGHTING_BUFFER_INFOS; j++) {
|
|
|
|
lframe->bufferInfo[j] = base_buffer_info;
|
2021-04-24 01:40:39 +00:00
|
|
|
lframe->bufferWrite[j] = base_buffer_write;
|
|
|
|
lframe->bufferWrite[j].dstSet = lights_set->a[i];
|
|
|
|
lframe->bufferWrite[j].dstBinding = j;
|
|
|
|
lframe->bufferWrite[j].pBufferInfo = &lframe->bufferInfo[j];
|
|
|
|
}
|
|
|
|
for (int j = 0; j < LIGHTING_ATTACH_INFOS; j++) {
|
|
|
|
lframe->attachInfo[j] = base_image_info;
|
|
|
|
lframe->attachInfo[j].sampler = 0;
|
|
|
|
lframe->attachWrite[j] = base_attachment_write;
|
|
|
|
lframe->attachWrite[j].dstSet = attach_set->a[i];
|
|
|
|
lframe->attachWrite[j].dstBinding = j;
|
|
|
|
lframe->attachWrite[j].pImageInfo = &lframe->attachInfo[j];
|
2021-02-24 10:58:31 +00:00
|
|
|
}
|
2021-04-24 01:40:39 +00:00
|
|
|
for (int j = 0; j < LIGHTING_SHADOW_INFOS; j++) {
|
|
|
|
lframe->shadowInfo[j] = base_image_info;
|
|
|
|
lframe->shadowInfo[j].sampler = lctx->sampler;
|
|
|
|
lframe->shadowInfo[j].imageView = ctx->default_black->view;
|
2021-02-24 10:58:31 +00:00
|
|
|
}
|
2021-04-24 01:40:39 +00:00
|
|
|
lframe->shadowWrite = base_image_write;
|
|
|
|
lframe->shadowWrite.dstSet = shadow_set->a[i];
|
|
|
|
lframe->shadowWrite.dstBinding = 0;
|
2022-11-25 04:35:07 +00:00
|
|
|
lframe->shadowWrite.descriptorCount = LIGHTING_SHADOW_INFOS;
|
2021-04-24 01:40:39 +00:00
|
|
|
lframe->shadowWrite.pImageInfo = lframe->shadowInfo;
|
2021-02-24 10:58:31 +00:00
|
|
|
}
|
2022-05-12 15:55:37 +00:00
|
|
|
free (shadow_set);
|
2021-02-24 10:58:31 +00:00
|
|
|
free (attach_set);
|
|
|
|
free (lights_set);
|
2021-12-02 13:48:50 +00:00
|
|
|
qfvPopDebug (ctx);
|
2021-02-24 10:58:31 +00:00
|
|
|
}
|
|
|
|
|
2021-04-29 11:54:38 +00:00
|
|
|
static void
|
|
|
|
clear_shadows (vulkan_ctx_t *ctx)
|
|
|
|
{
|
|
|
|
qfv_device_t *device = ctx->device;
|
|
|
|
qfv_devfuncs_t *dfunc = device->funcs;
|
|
|
|
lightingctx_t *lctx = ctx->lighting_context;
|
|
|
|
|
2022-05-31 03:43:04 +00:00
|
|
|
for (size_t i = 0; i < lctx->light_renderers.size; i++) {
|
|
|
|
__auto_type lr = &lctx->light_renderers.a[i];
|
|
|
|
dfunc->vkDestroyFramebuffer (device->dev, lr->framebuffer, 0);
|
|
|
|
dfunc->vkDestroyImageView (device->dev, lr->view, 0);
|
2021-04-29 11:54:38 +00:00
|
|
|
}
|
2022-10-27 02:02:45 +00:00
|
|
|
if (lctx->shadow_resources) {
|
|
|
|
QFV_DestroyResource (device, lctx->shadow_resources);
|
2022-10-27 02:23:49 +00:00
|
|
|
free (lctx->shadow_resources);
|
2022-10-27 02:02:45 +00:00
|
|
|
lctx->shadow_resources = 0;
|
2021-04-29 11:54:38 +00:00
|
|
|
}
|
2022-05-31 03:43:04 +00:00
|
|
|
lctx->light_images.size = 0;
|
|
|
|
lctx->light_renderers.size = 0;
|
2021-04-29 11:54:38 +00:00
|
|
|
}
|
|
|
|
|
2021-02-24 10:58:31 +00:00
|
|
|
void
|
|
|
|
Vulkan_Lighting_Shutdown (vulkan_ctx_t *ctx)
|
|
|
|
{
|
|
|
|
qfv_device_t *device = ctx->device;
|
|
|
|
qfv_devfuncs_t *dfunc = device->funcs;
|
|
|
|
lightingctx_t *lctx = ctx->lighting_context;
|
|
|
|
|
2021-04-29 11:54:38 +00:00
|
|
|
clear_shadows (ctx);
|
|
|
|
|
2022-05-31 03:43:04 +00:00
|
|
|
dfunc->vkDestroyCommandPool (device->dev, lctx->cmdpool, 0);
|
|
|
|
dfunc->vkDestroyRenderPass (device->dev, lctx->renderpass_6, 0);
|
|
|
|
dfunc->vkDestroyRenderPass (device->dev, lctx->renderpass_4, 0);
|
|
|
|
dfunc->vkDestroyRenderPass (device->dev, lctx->renderpass_1, 0);
|
|
|
|
|
2021-02-25 06:51:54 +00:00
|
|
|
for (size_t i = 0; i < lctx->frames.size; i++) {
|
|
|
|
lightingframe_t *lframe = &lctx->frames.a[i];
|
|
|
|
dfunc->vkDestroyBuffer (device->dev, lframe->light_buffer, 0);
|
|
|
|
}
|
|
|
|
dfunc->vkFreeMemory (device->dev, lctx->light_memory, 0);
|
2021-02-24 10:58:31 +00:00
|
|
|
dfunc->vkDestroyPipeline (device->dev, lctx->pipeline, 0);
|
2022-05-31 03:43:04 +00:00
|
|
|
DARRAY_CLEAR (&lctx->light_mats);
|
|
|
|
DARRAY_CLEAR (&lctx->light_images);
|
|
|
|
DARRAY_CLEAR (&lctx->light_renderers);
|
2021-02-24 10:58:31 +00:00
|
|
|
free (lctx->frames.a);
|
|
|
|
free (lctx);
|
|
|
|
}
|
2022-05-10 02:15:52 +00:00
|
|
|
|
2022-05-05 05:58:47 +00:00
|
|
|
static vec4f_t ref_direction = { 0, 0, 1, 0 };
|
2021-04-29 11:54:38 +00:00
|
|
|
|
|
|
|
static void
|
|
|
|
create_light_matrices (lightingctx_t *lctx)
|
|
|
|
{
|
2022-05-05 05:58:47 +00:00
|
|
|
lightingdata_t *ldata = lctx->ldata;
|
2022-05-31 03:43:04 +00:00
|
|
|
DARRAY_RESIZE (&lctx->light_mats, ldata->lights.size);
|
2022-05-05 05:58:47 +00:00
|
|
|
for (size_t i = 0; i < ldata->lights.size; i++) {
|
|
|
|
light_t *light = &ldata->lights.a[i];
|
2021-04-29 11:54:38 +00:00
|
|
|
mat4f_t view;
|
|
|
|
mat4f_t proj;
|
2022-05-04 23:40:02 +00:00
|
|
|
int mode = ST_NONE;
|
2021-04-29 11:54:38 +00:00
|
|
|
|
2022-05-04 23:40:02 +00:00
|
|
|
if (!light->position[3]) {
|
|
|
|
mode = ST_CASCADE;
|
|
|
|
} else {
|
|
|
|
if (light->direction[3] > -0.5) {
|
|
|
|
mode = ST_CUBE;
|
|
|
|
} else {
|
|
|
|
mode = ST_PLANE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
switch (mode) {
|
2021-06-01 10:09:03 +00:00
|
|
|
default:
|
2021-04-29 11:54:38 +00:00
|
|
|
case ST_NONE:
|
|
|
|
case ST_CUBE:
|
|
|
|
mat4fidentity (view);
|
|
|
|
break;
|
|
|
|
case ST_CASCADE:
|
|
|
|
case ST_PLANE:
|
|
|
|
//FIXME will fail for -ref_direction
|
2022-05-04 23:40:02 +00:00
|
|
|
vec4f_t dir = light->direction;
|
|
|
|
dir[3] = 0;
|
|
|
|
mat4fquat (view, qrotf (dir, ref_direction));
|
2021-04-29 11:54:38 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
VectorNegate (light->position, view[3]);
|
|
|
|
|
2022-05-04 23:40:02 +00:00
|
|
|
switch (mode) {
|
2021-04-29 11:54:38 +00:00
|
|
|
case ST_NONE:
|
|
|
|
mat4fidentity (proj);
|
|
|
|
break;
|
|
|
|
case ST_CUBE:
|
|
|
|
QFV_PerspectiveTan (proj, 1, 1);
|
|
|
|
break;
|
|
|
|
case ST_CASCADE:
|
|
|
|
// dependent on view fustrum and cascade level
|
|
|
|
mat4fidentity (proj);
|
|
|
|
break;
|
|
|
|
case ST_PLANE:
|
2022-05-04 23:40:02 +00:00
|
|
|
QFV_PerspectiveCos (proj, -light->direction[3]);
|
2021-04-29 11:54:38 +00:00
|
|
|
break;
|
|
|
|
}
|
2022-05-31 03:43:04 +00:00
|
|
|
mmulf (lctx->light_mats.a[i], proj, view);
|
2021-04-29 11:54:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2022-05-10 02:15:52 +00:00
|
|
|
light_compare (const void *_li2, const void *_li1, void *_ldata)
|
2021-04-29 11:54:38 +00:00
|
|
|
{
|
2022-05-10 02:15:52 +00:00
|
|
|
const int *li1 = _li1;
|
|
|
|
const int *li2 = _li2;
|
|
|
|
lightingdata_t *ldata = _ldata;
|
|
|
|
const light_t *l1 = &ldata->lights.a[*li1];
|
|
|
|
const light_t *l2 = &ldata->lights.a[*li2];
|
2022-05-11 04:29:17 +00:00
|
|
|
int s1 = abs ((int) l1->color[3]);
|
|
|
|
int s2 = abs ((int) l2->color[3]);
|
2021-04-29 11:54:38 +00:00
|
|
|
|
2022-05-11 04:29:17 +00:00
|
|
|
if (s1 == s2) {
|
2022-05-04 23:40:02 +00:00
|
|
|
return (l1->position[3] == l2->position[3])
|
|
|
|
&& (l1->direction[3] > -0.5) == (l2->direction[3] > -0.5);
|
2021-04-29 11:54:38 +00:00
|
|
|
}
|
2022-05-11 04:29:17 +00:00
|
|
|
return s1 - s2;
|
2021-04-29 11:54:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static VkImageView
|
2022-05-31 03:43:04 +00:00
|
|
|
create_view (const light_renderer_t *lr, int id, vulkan_ctx_t *ctx)
|
2021-04-29 11:54:38 +00:00
|
|
|
{
|
|
|
|
qfv_device_t *device = ctx->device;
|
|
|
|
qfv_devfuncs_t *dfunc = device->funcs;
|
|
|
|
|
|
|
|
VkImageViewType type = 0;
|
|
|
|
const char *viewtype = 0;
|
|
|
|
|
2022-05-31 03:43:04 +00:00
|
|
|
switch (lr->mode) {
|
2021-04-29 11:54:38 +00:00
|
|
|
case ST_NONE:
|
|
|
|
return 0;
|
|
|
|
case ST_PLANE:
|
|
|
|
type = VK_IMAGE_VIEW_TYPE_2D;
|
|
|
|
viewtype = "plane";
|
|
|
|
break;
|
|
|
|
case ST_CASCADE:
|
|
|
|
type = VK_IMAGE_VIEW_TYPE_2D_ARRAY;
|
|
|
|
viewtype = "cascade";
|
|
|
|
break;
|
|
|
|
case ST_CUBE:
|
|
|
|
type = VK_IMAGE_VIEW_TYPE_CUBE;
|
|
|
|
viewtype = "cube";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
VkImageViewCreateInfo createInfo = {
|
|
|
|
VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO, 0,
|
|
|
|
0,
|
2022-05-31 03:43:04 +00:00
|
|
|
lr->image, type, VK_FORMAT_X8_D24_UNORM_PACK32,
|
2021-04-29 11:54:38 +00:00
|
|
|
{
|
|
|
|
VK_COMPONENT_SWIZZLE_IDENTITY,
|
|
|
|
VK_COMPONENT_SWIZZLE_IDENTITY,
|
|
|
|
VK_COMPONENT_SWIZZLE_IDENTITY,
|
|
|
|
VK_COMPONENT_SWIZZLE_IDENTITY,
|
|
|
|
},
|
2022-05-31 03:43:04 +00:00
|
|
|
{ VK_IMAGE_ASPECT_DEPTH_BIT, 0, 1, lr->layer, lr->numLayers }
|
2021-04-29 11:54:38 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
VkImageView view;
|
|
|
|
dfunc->vkCreateImageView (device->dev, &createInfo, 0, &view);
|
|
|
|
QFV_duSetObjectName (device, VK_OBJECT_TYPE_IMAGE_VIEW, view,
|
|
|
|
va (ctx->va_ctx, "iview:shadowmap:%s:%d",
|
|
|
|
viewtype, id));
|
2021-07-06 02:55:29 +00:00
|
|
|
(void) viewtype;//silence unused warning when vulkan debug disabled
|
2021-04-29 11:54:38 +00:00
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
2022-05-31 03:43:04 +00:00
|
|
|
static VkFramebuffer
|
|
|
|
create_framebuffer (const light_renderer_t *lr, vulkan_ctx_t *ctx)
|
|
|
|
{
|
|
|
|
qfv_device_t *device = ctx->device;
|
|
|
|
qfv_devfuncs_t *dfunc = device->funcs;
|
|
|
|
|
|
|
|
VkFramebuffer framebuffer;
|
|
|
|
VkFramebufferCreateInfo cInfo = {
|
|
|
|
.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO,
|
|
|
|
.renderPass = lr->renderPass,
|
|
|
|
.attachmentCount = 1,
|
|
|
|
.pAttachments = &lr->view,
|
|
|
|
.width = lr->size,
|
|
|
|
.height = lr->size,
|
|
|
|
.layers = 1,
|
|
|
|
};
|
|
|
|
dfunc->vkCreateFramebuffer (device->dev, &cInfo, 0, &framebuffer);
|
|
|
|
return framebuffer;
|
|
|
|
}
|
|
|
|
|
2021-04-29 11:54:38 +00:00
|
|
|
static void
|
|
|
|
build_shadow_maps (lightingctx_t *lctx, vulkan_ctx_t *ctx)
|
|
|
|
{
|
2022-10-27 02:02:45 +00:00
|
|
|
typedef struct {
|
|
|
|
int size;
|
|
|
|
int layers;
|
|
|
|
int cube;
|
|
|
|
} mapdesc_t;
|
|
|
|
|
2021-04-29 11:54:38 +00:00
|
|
|
qfv_device_t *device = ctx->device;
|
|
|
|
qfv_physdev_t *physDev = device->physDev;
|
2022-05-29 13:13:36 +00:00
|
|
|
int maxLayers = physDev->properties->limits.maxImageArrayLayers;
|
2022-05-05 05:58:47 +00:00
|
|
|
lightingdata_t *ldata = lctx->ldata;
|
|
|
|
light_t *lights = ldata->lights.a;
|
|
|
|
int numLights = ldata->lights.size;
|
2021-04-29 11:54:38 +00:00
|
|
|
int size = -1;
|
|
|
|
int numLayers = 0;
|
|
|
|
int totalLayers = 0;
|
|
|
|
int *imageMap = alloca (numLights * sizeof (int));
|
2022-05-10 02:15:52 +00:00
|
|
|
int *lightMap = alloca (numLights * sizeof (int));
|
2022-10-27 02:02:45 +00:00
|
|
|
int numMaps = 0;
|
|
|
|
mapdesc_t *maps = alloca (numLights * sizeof (mapdesc_t));
|
2021-04-29 11:54:38 +00:00
|
|
|
|
2022-05-10 02:15:52 +00:00
|
|
|
for (int i = 0; i < numLights; i++) {
|
|
|
|
lightMap[i] = i;
|
|
|
|
}
|
|
|
|
heapsort_r (lightMap, numLights, sizeof (int), light_compare, ldata);
|
2022-05-31 03:43:04 +00:00
|
|
|
|
|
|
|
DARRAY_RESIZE (&lctx->light_renderers, numLights);
|
2021-04-29 11:54:38 +00:00
|
|
|
for (int i = 0; i < numLights; i++) {
|
|
|
|
int layers = 1;
|
2022-05-10 02:15:52 +00:00
|
|
|
int li = lightMap[i];
|
2022-05-31 03:43:04 +00:00
|
|
|
__auto_type lr = &lctx->light_renderers.a[li];
|
2022-05-31 03:56:59 +00:00
|
|
|
*lr = (light_renderer_t) {};
|
2022-05-04 23:40:02 +00:00
|
|
|
|
2022-05-10 02:15:52 +00:00
|
|
|
if (!lights[li].position[3]) {
|
2022-05-31 03:56:59 +00:00
|
|
|
if (!VectorIsZero (lights[li].direction)) {
|
|
|
|
lr->mode = ST_CASCADE;
|
|
|
|
}
|
2022-05-04 23:40:02 +00:00
|
|
|
} else {
|
2022-05-10 02:15:52 +00:00
|
|
|
if (lights[li].direction[3] > -0.5) {
|
2022-05-31 03:43:04 +00:00
|
|
|
lr->mode = ST_CUBE;
|
2022-05-04 23:40:02 +00:00
|
|
|
} else {
|
2022-05-31 03:43:04 +00:00
|
|
|
lr->mode = ST_PLANE;
|
2022-05-04 23:40:02 +00:00
|
|
|
}
|
|
|
|
}
|
2022-05-31 03:43:04 +00:00
|
|
|
if (lr->mode == ST_CASCADE || lr->mode == ST_NONE) {
|
2021-04-29 11:54:38 +00:00
|
|
|
// cascade shadows will be handled separately, and "none" has no
|
|
|
|
// shadow map at all
|
2022-05-10 02:15:52 +00:00
|
|
|
imageMap[li] = -1;
|
2021-04-29 11:54:38 +00:00
|
|
|
continue;
|
|
|
|
}
|
2022-05-31 03:43:04 +00:00
|
|
|
if (lr->mode == ST_CUBE) {
|
2021-04-29 11:54:38 +00:00
|
|
|
layers = 6;
|
2021-04-25 01:01:57 +00:00
|
|
|
}
|
2022-05-11 04:29:17 +00:00
|
|
|
if (size != abs ((int) lights[li].color[3])
|
2022-05-04 23:40:02 +00:00
|
|
|
|| numLayers + layers > maxLayers) {
|
2021-04-29 11:54:38 +00:00
|
|
|
if (numLayers) {
|
2022-10-27 02:02:45 +00:00
|
|
|
maps[numMaps++] = (mapdesc_t) {
|
|
|
|
.size = size,
|
|
|
|
.layers = numLayers,
|
2022-10-27 04:29:14 +00:00
|
|
|
.cube = 1,
|
2022-10-27 02:02:45 +00:00
|
|
|
};
|
2021-04-29 11:54:38 +00:00
|
|
|
numLayers = 0;
|
|
|
|
}
|
2022-05-11 04:29:17 +00:00
|
|
|
size = abs ((int) lights[li].color[3]);
|
2021-04-29 11:54:38 +00:00
|
|
|
}
|
2022-10-27 04:29:14 +00:00
|
|
|
imageMap[li] = numMaps;
|
2022-05-31 03:43:04 +00:00
|
|
|
lr->size = size;
|
|
|
|
lr->layer = numLayers;
|
|
|
|
lr->numLayers = layers;
|
2021-04-29 11:54:38 +00:00
|
|
|
numLayers += layers;
|
|
|
|
totalLayers += layers;
|
|
|
|
}
|
|
|
|
if (numLayers) {
|
2022-10-27 02:02:45 +00:00
|
|
|
maps[numMaps++] = (mapdesc_t) {
|
|
|
|
.size = size,
|
|
|
|
.layers = numLayers,
|
2022-10-27 04:29:14 +00:00
|
|
|
.cube = 1,
|
2022-10-27 02:02:45 +00:00
|
|
|
};
|
2021-04-29 11:54:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
numLayers = 0;
|
|
|
|
size = 1024;
|
|
|
|
for (int i = 0; i < numLights; i++) {
|
|
|
|
int layers = 4;
|
2022-05-10 02:15:52 +00:00
|
|
|
int li = lightMap[i];
|
2022-05-31 03:43:04 +00:00
|
|
|
__auto_type lr = &lctx->light_renderers.a[li];
|
2022-05-04 23:40:02 +00:00
|
|
|
|
2022-05-31 03:43:04 +00:00
|
|
|
if (lr->mode != ST_CASCADE) {
|
2021-04-29 11:54:38 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (numLayers + layers > maxLayers) {
|
2022-10-27 02:02:45 +00:00
|
|
|
maps[numMaps++] = (mapdesc_t) {
|
|
|
|
.size = size,
|
|
|
|
.layers = numLayers,
|
|
|
|
.cube = 0,
|
|
|
|
};
|
2021-04-29 11:54:38 +00:00
|
|
|
numLayers = 0;
|
|
|
|
}
|
2022-10-27 04:29:14 +00:00
|
|
|
imageMap[li] = numMaps;
|
2022-05-31 03:43:04 +00:00
|
|
|
lr->size = size;
|
|
|
|
lr->layer = numLayers;
|
|
|
|
lr->numLayers = layers;
|
2021-04-29 11:54:38 +00:00
|
|
|
numLayers += layers;
|
|
|
|
totalLayers += layers;
|
|
|
|
}
|
|
|
|
if (numLayers) {
|
2022-10-27 02:02:45 +00:00
|
|
|
maps[numMaps++] = (mapdesc_t) {
|
|
|
|
.size = size,
|
|
|
|
.layers = numLayers,
|
|
|
|
.cube = 0,
|
|
|
|
};
|
2021-04-29 11:54:38 +00:00
|
|
|
}
|
|
|
|
|
2022-10-27 02:02:45 +00:00
|
|
|
if (numMaps) {
|
|
|
|
qfv_resource_t *shad = calloc (1, sizeof (qfv_resource_t)
|
|
|
|
+ numMaps * sizeof (qfv_resobj_t));
|
|
|
|
lctx->shadow_resources = shad;
|
|
|
|
*shad = (qfv_resource_t) {
|
|
|
|
.name = "shadow",
|
|
|
|
.va_ctx = ctx->va_ctx,
|
|
|
|
.memory_properties = VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT,
|
|
|
|
.num_objects = numMaps,
|
|
|
|
.objects = (qfv_resobj_t *) &shad[1],
|
|
|
|
};
|
|
|
|
for (int i = 0; i < numMaps; i++) {
|
2023-02-19 03:25:13 +00:00
|
|
|
int cube = maps[i].layers < 6 ? 0 : maps[i].cube;
|
2022-10-27 02:02:45 +00:00
|
|
|
shad->objects[i] = (qfv_resobj_t) {
|
|
|
|
.name = "map",
|
|
|
|
.type = qfv_res_image,
|
|
|
|
.image = {
|
2023-02-19 03:25:13 +00:00
|
|
|
.flags = cube ? VK_IMAGE_CREATE_CUBE_COMPATIBLE_BIT : 0,
|
2022-10-27 02:02:45 +00:00
|
|
|
.type = VK_IMAGE_TYPE_2D,
|
|
|
|
.format = VK_FORMAT_X8_D24_UNORM_PACK32,
|
|
|
|
.extent = { maps[i].size, maps[i].size, 1 },
|
|
|
|
.num_mipmaps = 1,
|
|
|
|
.num_layers = maps[i].layers,
|
|
|
|
.samples = VK_SAMPLE_COUNT_1_BIT,
|
|
|
|
.usage = VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT
|
|
|
|
| VK_IMAGE_USAGE_SAMPLED_BIT,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
QFV_CreateResource (device, shad);
|
|
|
|
for (int i = 0; i < numMaps; i++) {
|
|
|
|
DARRAY_APPEND (&lctx->light_images, shad->objects[i].image.image);
|
|
|
|
}
|
2021-04-29 11:54:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < numLights; i++) {
|
2022-05-10 02:15:52 +00:00
|
|
|
int li = lightMap[i];
|
2022-05-31 03:43:04 +00:00
|
|
|
__auto_type lr = &lctx->light_renderers.a[li];
|
2022-05-10 02:15:52 +00:00
|
|
|
|
|
|
|
if (imageMap[li] == -1) {
|
2021-04-29 11:54:38 +00:00
|
|
|
continue;
|
|
|
|
}
|
2022-05-04 23:40:02 +00:00
|
|
|
|
2022-05-31 03:43:04 +00:00
|
|
|
switch (lr->numLayers) {
|
|
|
|
case 6:
|
|
|
|
lr->renderPass = lctx->renderpass_6;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
lr->renderPass = lctx->renderpass_4;
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
lr->renderPass = lctx->renderpass_1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
Sys_Error ("build_shadow_maps: invalid light layer count: %u",
|
|
|
|
lr->numLayers);
|
2022-05-04 23:40:02 +00:00
|
|
|
}
|
2022-05-31 03:43:04 +00:00
|
|
|
lr->image = lctx->light_images.a[imageMap[li]];
|
|
|
|
lr->view = create_view (lr, li, ctx);
|
|
|
|
lr->framebuffer = create_framebuffer(lr, ctx);
|
2021-04-29 11:54:38 +00:00
|
|
|
}
|
2022-05-31 03:43:04 +00:00
|
|
|
Sys_MaskPrintf (SYS_vulkan,
|
2022-12-02 08:33:26 +00:00
|
|
|
"shadow maps: %d layers in %zd images: %"PRId64"\n",
|
2022-10-27 02:32:00 +00:00
|
|
|
totalLayers, lctx->light_images.size,
|
|
|
|
lctx->shadow_resources->size);
|
2021-04-29 11:54:38 +00:00
|
|
|
}
|
2021-03-03 09:14:16 +00:00
|
|
|
|
|
|
|
void
|
2022-05-05 05:58:47 +00:00
|
|
|
Vulkan_LoadLights (scene_t *scene, vulkan_ctx_t *ctx)
|
2021-03-03 09:14:16 +00:00
|
|
|
{
|
|
|
|
lightingctx_t *lctx = ctx->lighting_context;
|
|
|
|
|
2022-05-05 05:58:47 +00:00
|
|
|
lctx->scene = scene;
|
2022-05-07 03:12:02 +00:00
|
|
|
lctx->ldata = scene ? scene->lights : 0;
|
2022-05-10 02:15:52 +00:00
|
|
|
|
|
|
|
clear_shadows (ctx);
|
|
|
|
|
|
|
|
if (lctx->ldata && lctx->ldata->lights.size) {
|
|
|
|
build_shadow_maps (lctx, ctx);
|
|
|
|
create_light_matrices (lctx);
|
|
|
|
}
|
2021-03-03 09:14:16 +00:00
|
|
|
}
|