[vulkan] Implement lightmap updates for dlights

GL-Quake all over again, but the world is so much more alive with moving
lights, even if... rather stylized.

Closes #73.
This commit is contained in:
Bill Currie 2024-01-17 19:52:21 +09:00
parent 8c6bfe0984
commit 1e7cb8ee17
6 changed files with 83 additions and 114 deletions

View file

@ -268,6 +268,7 @@ typedef struct bsp_instance_s {
typedef struct bsp_pass_s { typedef struct bsp_pass_s {
vec4f_t position; ///< view position vec4f_t position; ///< view position
const vec4f_t *transform; ///< transform for current model
const struct mod_brush_s *brush;///< data for current model const struct mod_brush_s *brush;///< data for current model
struct bspctx_s *bsp_context; ///< owning bsp context struct bspctx_s *bsp_context; ///< owning bsp context
struct entqueue_s *entqueue; ///< entities to render this pass struct entqueue_s *entqueue; ///< entities to render this pass

View file

@ -44,7 +44,7 @@ typedef struct subpic_s subpic_t;
void Vulkan_lightmap_init (struct vulkan_ctx_s *ctx); void Vulkan_lightmap_init (struct vulkan_ctx_s *ctx);
void Vulkan_BuildLightmaps (model_t **models, int num_models, vulkan_ctx_t *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)); VkImageView Vulkan_LightmapImageView (struct vulkan_ctx_s *ctx) __attribute__((pure));
void Vulkan_FlushLightmaps (struct vulkan_ctx_s *ctx); void Vulkan_FlushLightmaps (struct vulkan_ctx_s *ctx);

View file

@ -111,10 +111,10 @@ typedef struct {
} medge_t; } medge_t;
typedef struct { typedef struct {
float vecs[2][4]; vec4f_t vecs[2];
float mipadjust; float mipadjust;
texture_t *texture; int flags;
int flags; texture_t *texture;
} mtexinfo_t; } mtexinfo_t;
#define VERTEXSIZE 7 #define VERTEXSIZE 7

View file

@ -112,8 +112,8 @@ D_CalcGradients (msurface_t *pface)
mipscale = 1.0 / (float) (1 << miplevel); mipscale = 1.0 / (float) (1 << miplevel);
TransformVector (pface->texinfo->vecs[0], p_saxis); TransformVector ((vec_t*)&pface->texinfo->vecs[0], p_saxis);//FIXME
TransformVector (pface->texinfo->vecs[1], p_taxis); TransformVector ((vec_t*)&pface->texinfo->vecs[1], p_taxis);//FIXME
t = xscaleinv * mipscale; t = xscaleinv * mipscale;
d_sdivzstepu = p_saxis[0] * t; d_sdivzstepu = p_saxis[0] * t;

View file

@ -679,6 +679,8 @@ R_DrawBrushModel (entity_t ent, bsp_pass_t *pass, vulkan_ctx_t *ctx)
} }
auto animation = Entity_GetAnimation (ent); auto animation = Entity_GetAnimation (ent);
pass->transform = Transform_GetWorldMatrixPtr (Entity_Transform (ent));
pass->brush = &model->brush;
pass->ent_frame = animation->frame & 1; pass->ent_frame = animation->frame & 1;
pass->inst_id = model->render_id; pass->inst_id = model->render_id;
pass->inst_id |= renderer->colormod[3] < 1 ? INST_ALPHA : 0; 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); chain_surface (face, pass, bctx);
} }
} }
pass->transform = nullptr;
DARRAY_APPEND (&pass->instances[model->render_id].entities, DARRAY_APPEND (&pass->instances[model->render_id].entities,
renderer->render_id); renderer->render_id);
return 1; 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) { if ((surf->dlightframe == r_framecount) || surf->cached_dlight) {
dynamic: dynamic:
Vulkan_BuildLightMap (nulltransform, pass->brush, surf, Vulkan_BuildLightMap (pass->transform, pass->brush, surf,
bctx->vulkan_ctx); bctx->vulkan_ctx);
} }
} }

View file

@ -53,55 +53,57 @@
#include "r_internal.h" #include "r_internal.h"
#include "vid_vulkan.h" #include "vid_vulkan.h"
#define s_dynlight (r_refdef.scene->base + scene_dynlight)
#define LUXEL_SIZE 4 #define LUXEL_SIZE 4
static inline void static inline void
add_dynamic_lights (transform_t transform, msurface_t *surf, add_dynamic_lights (const vec4f_t *transform, msurface_t *surf, vec4f_t *block)
float *block)
{ {
#if 0 qfZoneScoped (true);
unsigned lnum;
int sd, td; int sd, td;
float dist, rad, minlight;
vec3_t impact, local, lightorigin;
vec4f_t entorigin = { 0, 0, 0, 1 };
int smax, tmax; int smax, tmax;
int s, t; int s, t;
mtexinfo_t *tex; mtexinfo_t *tex;
plane_t *plane;
smax = (surf->extents[0] >> 4) + 1; smax = (surf->extents[0] >> 4) + 1;
tmax = (surf->extents[1] >> 4) + 1; tmax = (surf->extents[1] >> 4) + 1;
tex = surf->texinfo; 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) { if (transform) {
//FIXME give world entity a transform //FIXME give world entity a transform
entorigin = Transform_GetWorldPosition (transform); entorigin = transform[3];
} }
for (lnum = 0; lnum < r_maxdlights; lnum++) { auto dlight_pool = &r_refdef.registry->comp_pools[s_dynlight];
if (!(surf->dlightbits[lnum / 32] & (1 << (lnum % 32)))) 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 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); float minlight = dlight->minlight;
rad = light->radius;
dist = DotProduct (lightorigin, plane->normal) - plane->dist;
rad -= fabs (dist);
minlight = light->minlight;
if (rad < minlight) { if (rad < minlight) {
continue; 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]; vec4f_t local = {
local[1] = DotProduct (impact, tex->vecs[1]) + tex->vecs[1][3]; dotf (impact, tex->vecs[0])[0] - surf->texturemins[0],
dotf (impact, tex->vecs[1])[0] - surf->texturemins[1],
};
local[0] -= surf->texturemins[0]; vec4f_t color = { VectorExpand (dlight->color), 0 };
local[1] -= surf->texturemins[1]; color *= dlight->radius / 4096;
for (t = 0; t < tmax; t++) { for (t = 0; t < tmax; t++) {
td = local[1] - t * 16; td = local[1] - t * 16;
@ -113,73 +115,69 @@ add_dynamic_lights (transform_t transform, msurface_t *surf,
if (sd < 0) { if (sd < 0) {
sd = -sd; sd = -sd;
} }
float d;
if (sd > td) { if (sd > td) {
dist = sd + (td >> 1); d = sd + (td >> 1);
} else { } else {
dist = td + (sd >> 1); d = td + (sd >> 1);
} }
if (dist < minlight) { float l = dlight->radius - d;
float *out = block + (t * smax + s) * LUXEL_SIZE; if (l > minlight) {
float l = (rad - dist); block[t * smax + s] += l * color;
VectorMultAdd (out, l, light->color, out);
out[3] = 1;
out += LUXEL_SIZE;
} }
} }
} }
} }
#endif
} }
void static void
Vulkan_BuildLightMap (transform_t transform, const mod_brush_t *brush, vulkan_build_lightmap (const mod_brush_t *brush, msurface_t *surf,
msurface_t *surf, vulkan_ctx_t *ctx) vec4f_t *block)
{ {
qfZoneScoped (true); qfZoneScoped (true);
bspctx_t *bctx = ctx->bsp_context; int smax = (surf->extents[0] >> 4) + 1;
int smax, tmax, size; int tmax = (surf->extents[1] >> 4) + 1;
unsigned scale; int size = smax * tmax;
int i;
float *out, *block;
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) { if (!brush->lightdata) {
out = block; for (int i = 0; i < size; i++) {
while (size-- > 0) { block[i] = (vec4f_t) {1, 1, 1, 1};
*out++ = 1;
} }
return; return;
} }
// clear to no light for (int i = 0; i < size; i++) {
memset (block, 0, size * sizeof(float)); block[i] = (vec4f_t) {0, 0, 0, 1};
}
// add all the lightmaps
if (surf->samples) { if (surf->samples) {
byte *lightmap; byte *lightmap = surf->samples;
for (int map = 0; map < MAXLIGHTMAPS && surf->styles[map] != 255;
lightmap = surf->samples; map++) {
for (int maps = 0; maps < MAXLIGHTMAPS && surf->styles[maps] != 255; surf->cached_light[map] = d_lightstylevalue[surf->styles[map]];
maps++) { float scale = surf->cached_light[map] / 65536.0;
scale = d_lightstylevalue[surf->styles[maps]]; auto bl = block;
surf->cached_light[maps] = scale; // 8.8 fraction for (int i = 0; i < size; i++) {
out = block; vec4f_t val = { VectorExpand (lightmap), 0 };
for (i = 0; i < smax * tmax; i++) { val *= scale;
*out++ += *lightmap++ * scale / 65536.0; lightmap += 3;
*out++ += *lightmap++ * scale / 65536.0; *bl++ = val;
*out++ += *lightmap++ * scale / 65536.0;
out++;
} }
} }
} }
}
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 // add all the dynamic lights
if (surf->dlightframe == r_framecount) { if (surf->dlightframe == r_framecount) {
add_dynamic_lights (transform, surf, block); 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 GL_BuildLightmaps
@ -269,7 +233,8 @@ Vulkan_BuildLightmaps (model_t **models, int num_models, vulkan_ctx_t *ctx)
continue; continue;
} }
vulkan_create_surf_lightmap (surf, ctx); 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++) { for (uint32_t i = 0; i < brush->numsurfaces; i++) {
msurface_t *surf = brush->surfaces + i; msurface_t *surf = brush->surfaces + i;
if (surf->lightpic) { if (surf->lightpic) {
Vulkan_BuildLightMap (nulltransform, brush, surf, ctx); Vulkan_BuildLightMap (nullptr, brush, surf, ctx);
} }
} }
} }