diff --git a/include/QF/Vulkan/qf_bsp.h b/include/QF/Vulkan/qf_bsp.h index b4f1d0adb..c2119eb15 100644 --- a/include/QF/Vulkan/qf_bsp.h +++ b/include/QF/Vulkan/qf_bsp.h @@ -268,6 +268,7 @@ typedef struct bsp_instance_s { typedef struct bsp_pass_s { vec4f_t position; ///< view position + const vec4f_t *transform; ///< transform for current model const struct mod_brush_s *brush;///< data for current model struct bspctx_s *bsp_context; ///< owning bsp context struct entqueue_s *entqueue; ///< entities to render this pass diff --git a/include/QF/Vulkan/qf_lightmap.h b/include/QF/Vulkan/qf_lightmap.h index f11128b91..a6b973d66 100644 --- a/include/QF/Vulkan/qf_lightmap.h +++ b/include/QF/Vulkan/qf_lightmap.h @@ -44,7 +44,7 @@ typedef struct subpic_s subpic_t; void Vulkan_lightmap_init (struct vulkan_ctx_s *ctx); void Vulkan_BuildLightmaps (model_t **models, int num_models, vulkan_ctx_t *ctx); -void Vulkan_BuildLightMap (transform_t transform, const mod_brush_t *brush, msurface_t *surf, vulkan_ctx_t *ctx); +void Vulkan_BuildLightMap (const vec4f_t *transform, const mod_brush_t *brush, msurface_t *surf, vulkan_ctx_t *ctx); VkImageView Vulkan_LightmapImageView (struct vulkan_ctx_s *ctx) __attribute__((pure)); void Vulkan_FlushLightmaps (struct vulkan_ctx_s *ctx); diff --git a/include/QF/model.h b/include/QF/model.h index fc35f5d0e..349e564b3 100644 --- a/include/QF/model.h +++ b/include/QF/model.h @@ -111,10 +111,10 @@ typedef struct { } medge_t; typedef struct { - float vecs[2][4]; - float mipadjust; - texture_t *texture; - int flags; + vec4f_t vecs[2]; + float mipadjust; + int flags; + texture_t *texture; } mtexinfo_t; #define VERTEXSIZE 7 diff --git a/libs/video/renderer/sw/d_edge.c b/libs/video/renderer/sw/d_edge.c index ea64dbfc4..a69f9a7fe 100644 --- a/libs/video/renderer/sw/d_edge.c +++ b/libs/video/renderer/sw/d_edge.c @@ -112,8 +112,8 @@ D_CalcGradients (msurface_t *pface) mipscale = 1.0 / (float) (1 << miplevel); - TransformVector (pface->texinfo->vecs[0], p_saxis); - TransformVector (pface->texinfo->vecs[1], p_taxis); + TransformVector ((vec_t*)&pface->texinfo->vecs[0], p_saxis);//FIXME + TransformVector ((vec_t*)&pface->texinfo->vecs[1], p_taxis);//FIXME t = xscaleinv * mipscale; d_sdivzstepu = p_saxis[0] * t; diff --git a/libs/video/renderer/vulkan/vulkan_bsp.c b/libs/video/renderer/vulkan/vulkan_bsp.c index 1d721100d..62abf2355 100644 --- a/libs/video/renderer/vulkan/vulkan_bsp.c +++ b/libs/video/renderer/vulkan/vulkan_bsp.c @@ -679,6 +679,8 @@ R_DrawBrushModel (entity_t ent, bsp_pass_t *pass, vulkan_ctx_t *ctx) } auto animation = Entity_GetAnimation (ent); + pass->transform = Transform_GetWorldMatrixPtr (Entity_Transform (ent)); + pass->brush = &model->brush; pass->ent_frame = animation->frame & 1; pass->inst_id = model->render_id; pass->inst_id |= renderer->colormod[3] < 1 ? INST_ALPHA : 0; @@ -690,6 +692,7 @@ R_DrawBrushModel (entity_t ent, bsp_pass_t *pass, vulkan_ctx_t *ctx) chain_surface (face, pass, bctx); } } + pass->transform = nullptr; DARRAY_APPEND (&pass->instances[model->render_id].entities, renderer->render_id); return 1; @@ -923,7 +926,7 @@ update_lightmap (bsp_pass_t *pass, const bspctx_t *bctx, instface_t is) } if ((surf->dlightframe == r_framecount) || surf->cached_dlight) { dynamic: - Vulkan_BuildLightMap (nulltransform, pass->brush, surf, + Vulkan_BuildLightMap (pass->transform, pass->brush, surf, bctx->vulkan_ctx); } } diff --git a/libs/video/renderer/vulkan/vulkan_lightmap.c b/libs/video/renderer/vulkan/vulkan_lightmap.c index 88c415ad9..626accb5a 100644 --- a/libs/video/renderer/vulkan/vulkan_lightmap.c +++ b/libs/video/renderer/vulkan/vulkan_lightmap.c @@ -53,55 +53,57 @@ #include "r_internal.h" #include "vid_vulkan.h" +#define s_dynlight (r_refdef.scene->base + scene_dynlight) #define LUXEL_SIZE 4 static inline void -add_dynamic_lights (transform_t transform, msurface_t *surf, - float *block) +add_dynamic_lights (const vec4f_t *transform, msurface_t *surf, vec4f_t *block) { -#if 0 - unsigned lnum; + qfZoneScoped (true); int sd, td; - float dist, rad, minlight; - vec3_t impact, local, lightorigin; - vec4f_t entorigin = { 0, 0, 0, 1 }; int smax, tmax; int s, t; mtexinfo_t *tex; - plane_t *plane; smax = (surf->extents[0] >> 4) + 1; tmax = (surf->extents[1] >> 4) + 1; tex = surf->texinfo; - plane = surf->plane; + auto p = surf->plane; + vec4f_t plane = { VectorExpand (p->normal), -p->dist }; + vec4f_t entorigin = { 0, 0, 0, 1 }; if (transform) { //FIXME give world entity a transform - entorigin = Transform_GetWorldPosition (transform); + entorigin = transform[3]; } - for (lnum = 0; lnum < r_maxdlights; lnum++) { - if (!(surf->dlightbits[lnum / 32] & (1 << (lnum % 32)))) + auto dlight_pool = &r_refdef.registry->comp_pools[s_dynlight]; + auto dlight_data = (dlight_t *) dlight_pool->data; + for (uint32_t i = 0; i < dlight_pool->count; i++) { + auto dlight = &dlight_data[i]; + if (!(surf->dlightbits[i / 32] & (1 << (i % 32)))) continue; // not lit by this light - dlight_t *light = &r_dlights[lnum]; + vec4f_t lightorigin = dlight->origin - entorigin; + lightorigin[3] = 1; + float rad = dlight->radius; + vec4f_t dist = dotf (lightorigin, plane); + dist[3] = 0; + rad -= fabs (dist[0]); - VectorSubtract (light->origin, entorigin, lightorigin); - rad = light->radius; - dist = DotProduct (lightorigin, plane->normal) - plane->dist; - rad -= fabs (dist); - - minlight = light->minlight; + float minlight = dlight->minlight; if (rad < minlight) { continue; } - VectorMultSub (light->origin, dist, plane->normal, impact); + vec4f_t impact = dlight->origin - dist * plane; - local[0] = DotProduct (impact, tex->vecs[0]) + tex->vecs[0][3]; - local[1] = DotProduct (impact, tex->vecs[1]) + tex->vecs[1][3]; + vec4f_t local = { + dotf (impact, tex->vecs[0])[0] - surf->texturemins[0], + dotf (impact, tex->vecs[1])[0] - surf->texturemins[1], + }; - local[0] -= surf->texturemins[0]; - local[1] -= surf->texturemins[1]; + vec4f_t color = { VectorExpand (dlight->color), 0 }; + color *= dlight->radius / 4096; for (t = 0; t < tmax; t++) { td = local[1] - t * 16; @@ -113,73 +115,69 @@ add_dynamic_lights (transform_t transform, msurface_t *surf, if (sd < 0) { sd = -sd; } + float d; if (sd > td) { - dist = sd + (td >> 1); + d = sd + (td >> 1); } else { - dist = td + (sd >> 1); + d = td + (sd >> 1); } - if (dist < minlight) { - float *out = block + (t * smax + s) * LUXEL_SIZE; - float l = (rad - dist); - VectorMultAdd (out, l, light->color, out); - out[3] = 1; - out += LUXEL_SIZE; + float l = dlight->radius - d; + if (l > minlight) { + block[t * smax + s] += l * color; } } } } -#endif } -void -Vulkan_BuildLightMap (transform_t transform, const mod_brush_t *brush, - msurface_t *surf, vulkan_ctx_t *ctx) +static void +vulkan_build_lightmap (const mod_brush_t *brush, msurface_t *surf, + vec4f_t *block) { qfZoneScoped (true); - bspctx_t *bctx = ctx->bsp_context; - int smax, tmax, size; - unsigned scale; - int i; - float *out, *block; + int smax = (surf->extents[0] >> 4) + 1; + int tmax = (surf->extents[1] >> 4) + 1; + int size = smax * tmax; - surf->cached_dlight = (surf->dlightframe == r_framecount); - - smax = (surf->extents[0] >> 4) + 1; - tmax = (surf->extents[1] >> 4) + 1; - size = smax * tmax * LUXEL_SIZE; - - block = QFV_SubpicBatch (surf->lightpic, bctx->light_stage); - - // set to full bright if no light data if (!brush->lightdata) { - out = block; - while (size-- > 0) { - *out++ = 1; + for (int i = 0; i < size; i++) { + block[i] = (vec4f_t) {1, 1, 1, 1}; } return; } - // clear to no light - memset (block, 0, size * sizeof(float)); - - // add all the lightmaps + for (int i = 0; i < size; i++) { + block[i] = (vec4f_t) {0, 0, 0, 1}; + } if (surf->samples) { - byte *lightmap; - - lightmap = surf->samples; - for (int maps = 0; maps < MAXLIGHTMAPS && surf->styles[maps] != 255; - maps++) { - scale = d_lightstylevalue[surf->styles[maps]]; - surf->cached_light[maps] = scale; // 8.8 fraction - out = block; - for (i = 0; i < smax * tmax; i++) { - *out++ += *lightmap++ * scale / 65536.0; - *out++ += *lightmap++ * scale / 65536.0; - *out++ += *lightmap++ * scale / 65536.0; - out++; + byte *lightmap = surf->samples; + for (int map = 0; map < MAXLIGHTMAPS && surf->styles[map] != 255; + map++) { + surf->cached_light[map] = d_lightstylevalue[surf->styles[map]]; + float scale = surf->cached_light[map] / 65536.0; + auto bl = block; + for (int i = 0; i < size; i++) { + vec4f_t val = { VectorExpand (lightmap), 0 }; + val *= scale; + lightmap += 3; + *bl++ = val; } } } +} + +void +Vulkan_BuildLightMap (const vec4f_t *transform, const mod_brush_t *brush, + msurface_t *surf, vulkan_ctx_t *ctx) +{ + qfZoneScoped (true); + bspctx_t *bctx = ctx->bsp_context; + + surf->cached_dlight = (surf->dlightframe == r_framecount); + + vec4f_t *block = QFV_SubpicBatch (surf->lightpic, bctx->light_stage); + vulkan_build_lightmap (brush, surf, block); + // add all the dynamic lights if (surf->dlightframe == r_framecount) { add_dynamic_lights (transform, surf, block); @@ -201,40 +199,6 @@ vulkan_create_surf_lightmap (msurface_t *surf, vulkan_ctx_t *ctx) } } -static void -vulkan_build_lightmap (mod_brush_t *brush, msurface_t *surf, vulkan_ctx_t *ctx) -{ - int smax = (surf->extents[0] >> 4) + 1; - int tmax = (surf->extents[1] >> 4) + 1; - int size = smax * tmax; - - vec4f_t *blocklights = QFV_SubpicBatch (surf->lightpic, ctx->staging); - if (!brush->lightdata) { - for (int i = 0; i < size; i++) { - blocklights[i] = (vec4f_t) {1, 1, 1, 1}; - } - return; - } - - memset (blocklights, 0, sizeof (vec4f_t[size])); - if (surf->samples) { - byte *lightmap = surf->samples; - for (int map = 0; map < MAXLIGHTMAPS && surf->styles[map] != 255; - map++) { - surf->cached_light[map] = d_lightstylevalue[surf->styles[map]]; - float scale = surf->cached_light[map] / 65536.0; - auto bl = blocklights; - for (int i = 0; i < size; i++) { - vec4f_t val = { VectorExpand (lightmap), 0 }; - val *= scale; - val[3] = 1; - lightmap += 3; - *bl++ = val; - } - } - } -} - /* GL_BuildLightmaps @@ -269,7 +233,8 @@ Vulkan_BuildLightmaps (model_t **models, int num_models, vulkan_ctx_t *ctx) continue; } vulkan_create_surf_lightmap (surf, ctx); - vulkan_build_lightmap (brush, surf, ctx); + vec4f_t *block = QFV_SubpicBatch (surf->lightpic, ctx->staging); + vulkan_build_lightmap (brush, surf, block); } } @@ -287,7 +252,7 @@ Vulkan_BuildLightmaps (model_t **models, int num_models, vulkan_ctx_t *ctx) for (uint32_t i = 0; i < brush->numsurfaces; i++) { msurface_t *surf = brush->surfaces + i; if (surf->lightpic) { - Vulkan_BuildLightMap (nulltransform, brush, surf, ctx); + Vulkan_BuildLightMap (nullptr, brush, surf, ctx); } } }