From 590f0f18bc2a417e3e4c43009a613d4ebcb1a986 Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Sun, 25 Apr 2021 12:22:54 +0900 Subject: [PATCH] [vulkan] Check leaf sky visibility for suns And fix some out-by-one errors for leaf visibility (pvs index 0 is actually leaf index 1) --- include/QF/model.h | 1 + libs/models/brush/model_brush.c | 17 +++++++++++++++++ libs/video/renderer/vulkan/vulkan_lighting.c | 19 ++++++++++++++----- 3 files changed, 32 insertions(+), 5 deletions(-) diff --git a/include/QF/model.h b/include/QF/model.h index 1c5969820..295035f3d 100644 --- a/include/QF/model.h +++ b/include/QF/model.h @@ -272,6 +272,7 @@ typedef struct mod_brush_s { mnode_t **node_parents; mnode_t **leaf_parents; + int *leaf_flags; // union of surf flags for surfs in leaf unsigned int checksum; unsigned int checksum2; diff --git a/libs/models/brush/model_brush.c b/libs/models/brush/model_brush.c index db9298ad5..74b6e7a48 100644 --- a/libs/models/brush/model_brush.c +++ b/libs/models/brush/model_brush.c @@ -651,6 +651,20 @@ Mod_SetParent (mod_brush_t *brush, mnode_t *node, mnode_t *parent) Mod_SetParent (brush, node->children[1], node); } +static void +Mod_SetLeafFlags (mod_brush_t *brush) +{ + for (int i = 0; i < brush->numleafs; i++) { + int flags = 0; + mleaf_t *leaf = &brush->leafs[i]; + for (int j = 0; j < leaf->nummarksurfaces; j++) { + msurface_t *surf = leaf->firstmarksurface[j]; + flags |= surf->flags; + } + brush->leaf_flags[i] = flags; + } +} + static void Mod_LoadNodes (model_t *mod, bsp_t *bsp) { @@ -704,9 +718,12 @@ Mod_LoadNodes (model_t *mod, bsp_t *bsp) } size_t size = (brush->numleafs + brush->numnodes) * sizeof (mnode_t *); + size += brush->numleafs * sizeof (int); brush->node_parents = Hunk_AllocName (size, mod->name); brush->leaf_parents = brush->node_parents + brush->numnodes; + brush->leaf_flags = (int *) (brush->leaf_parents + brush->numleafs); Mod_SetParent (brush, brush->nodes, NULL); // sets nodes and leafs + Mod_SetLeafFlags (brush); } static void diff --git a/libs/video/renderer/vulkan/vulkan_lighting.c b/libs/video/renderer/vulkan/vulkan_lighting.c index 8fd57078e..ec72f38b5 100644 --- a/libs/video/renderer/vulkan/vulkan_lighting.c +++ b/libs/video/renderer/vulkan/vulkan_lighting.c @@ -81,12 +81,19 @@ find_visible_lights (vulkan_ctx_t *ctx) if (leaf != lframe->leaf) { //double start = Sys_DoubleTime (); byte pvs[MAP_PVS_BYTES]; + int flags = 0; Mod_LeafPVS_set (leaf, model, 0, pvs); memcpy (lframe->pvs, pvs, sizeof (pvs)); for (int i = 0; i < model->brush.numleafs; i++) { if (pvs[i / 8] & (1 << (i % 8))) { - Mod_LeafPVS_mix (model->brush.leafs + i, model, 0, lframe->pvs); + Mod_LeafPVS_mix (model->brush.leafs + i + 1, model, 0, + lframe->pvs); + } + } + for (int i = 0; i < model->brush.numleafs; i++) { + if (lframe->pvs[i / 8] & (1 << (i % 8))) { + flags |= model->brush.leaf_flags[i + 1]; } } lframe->leaf = leaf; @@ -98,8 +105,8 @@ find_visible_lights (vulkan_ctx_t *ctx) memset (lframe->lightvis.a, 0, lframe->lightvis.size * sizeof (byte)); for (size_t i = 0; i < lctx->lightleafs.size; i++) { int l = lctx->lightleafs.a[i]; - //FIXME -1 needs check for sky - if (l == -1 || lframe->pvs[l / 8] & (1 << (l % 8))) { + if ((l == -1 && (flags & SURF_DRAWSKY)) + || lframe->pvs[l / 8] & (1 << (l % 8))) { lframe->lightvis.a[i] = 1; visible++; } @@ -646,8 +653,10 @@ Vulkan_LoadLights (model_t *model, const char *entity_data, vulkan_ctx_t *ctx) DARRAY_APPEND (&lctx->lights, light); mleaf_t *leaf = Mod_PointInLeaf (&light.position[0], model); - DARRAY_APPEND (&lctx->lightleafs, leaf - model->brush.leafs); - dump_light (&light, leaf - model->brush.leafs); + DARRAY_APPEND (&lctx->lightleafs, + leaf - model->brush.leafs - 1); + dump_light (&light, + lctx->lightleafs.a[lctx->lightleafs.size - 1]); } } for (size_t i = 0; i < ctx->frames.size; i++) {