2021-01-19 16:25:54 +00:00
|
|
|
/*
|
|
|
|
vulkan_lightmap.c
|
|
|
|
|
|
|
|
surface-related refresh code
|
|
|
|
|
|
|
|
Copyright (C) 1996-1997 Id Software, Inc.
|
|
|
|
Copyright (C) 2000 Joseph Carter <knghtbrd@debian.org>
|
|
|
|
Copyright (C) 2021 Bill Currie <bill@taniwha.org>
|
|
|
|
|
|
|
|
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 <math.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include "QF/cvar.h"
|
2021-03-19 11:18:45 +00:00
|
|
|
#include "QF/entity.h"
|
2021-01-19 16:25:54 +00:00
|
|
|
#include "QF/render.h"
|
|
|
|
#include "QF/sys.h"
|
|
|
|
#include "QF/Vulkan/qf_bsp.h"
|
|
|
|
#include "QF/Vulkan/qf_lightmap.h"
|
|
|
|
#include "QF/Vulkan/qf_main.h"
|
2021-01-21 07:39:11 +00:00
|
|
|
#include "QF/Vulkan/scrap.h"
|
2021-01-19 16:25:54 +00:00
|
|
|
|
|
|
|
#include "compat.h"
|
|
|
|
#include "r_internal.h"
|
|
|
|
#include "vid_vulkan.h"
|
|
|
|
|
|
|
|
#define LUXEL_SIZE 4
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
add_dynamic_lights (msurface_t *surf, float *block)
|
|
|
|
{
|
|
|
|
unsigned lnum;
|
|
|
|
int sd, td;
|
|
|
|
float dist, rad, minlight;
|
|
|
|
vec3_t impact, local, lightorigin;
|
2021-03-19 11:18:45 +00:00
|
|
|
vec4f_t entorigin = { 0, 0, 0, 1 };
|
2021-01-19 16:25:54 +00:00
|
|
|
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;
|
|
|
|
|
2021-03-19 11:18:45 +00:00
|
|
|
if (currententity->transform) {
|
|
|
|
//FIXME give world entity a transform
|
|
|
|
entorigin = Transform_GetWorldPosition (currententity->transform);
|
|
|
|
}
|
|
|
|
|
2021-01-19 16:25:54 +00:00
|
|
|
for (lnum = 0; lnum < r_maxdlights; lnum++) {
|
|
|
|
if (!(surf->dlightbits[lnum / 32] & (1 << (lnum % 32))))
|
|
|
|
continue; // not lit by this light
|
|
|
|
|
|
|
|
dlight_t *light = &r_dlights[lnum];
|
|
|
|
|
2021-03-19 11:18:45 +00:00
|
|
|
VectorSubtract (light->origin, entorigin, lightorigin);
|
2021-01-19 16:25:54 +00:00
|
|
|
rad = light->radius;
|
|
|
|
dist = DotProduct (lightorigin, plane->normal) - plane->dist;
|
|
|
|
rad -= fabs (dist);
|
|
|
|
|
|
|
|
minlight = light->minlight;
|
|
|
|
if (rad < minlight) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
VectorMultSub (light->origin, dist, plane->normal, impact);
|
|
|
|
|
|
|
|
local[0] = DotProduct (impact, tex->vecs[0]) + tex->vecs[0][3];
|
|
|
|
local[1] = DotProduct (impact, tex->vecs[1]) + tex->vecs[1][3];
|
|
|
|
|
|
|
|
local[0] -= surf->texturemins[0];
|
|
|
|
local[1] -= surf->texturemins[1];
|
|
|
|
|
|
|
|
for (t = 0; t < tmax; t++) {
|
|
|
|
td = local[1] - t * 16;
|
|
|
|
if (td < 0) {
|
|
|
|
td = -td;
|
|
|
|
}
|
|
|
|
for (s = 0; s < smax; s++) {
|
|
|
|
sd = local[0] - s * 16;
|
|
|
|
if (sd < 0) {
|
|
|
|
sd = -sd;
|
|
|
|
}
|
|
|
|
if (sd > td) {
|
|
|
|
dist = sd + (td >> 1);
|
|
|
|
} else {
|
|
|
|
dist = td + (sd >> 1);
|
|
|
|
}
|
|
|
|
if (dist < minlight) {
|
|
|
|
float *out = block + (t * smax + s) * LUXEL_SIZE;
|
2021-01-23 15:26:38 +00:00
|
|
|
float l = (rad - dist);
|
2021-01-19 16:25:54 +00:00
|
|
|
VectorMultAdd (out, l, light->color, out);
|
|
|
|
out[3] = 1;
|
|
|
|
out += LUXEL_SIZE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2021-02-01 10:31:11 +00:00
|
|
|
Vulkan_BuildLightMap (mod_brush_t *brush, msurface_t *surf, vulkan_ctx_t *ctx)
|
2021-01-19 16:25:54 +00:00
|
|
|
{
|
|
|
|
bspctx_t *bctx = ctx->bsp_context;
|
|
|
|
int smax, tmax, size;
|
|
|
|
unsigned scale;
|
|
|
|
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
|
2021-02-01 10:31:11 +00:00
|
|
|
if (!brush->lightdata) {
|
2021-01-19 16:25:54 +00:00
|
|
|
out = block;
|
|
|
|
while (size-- > 0) {
|
|
|
|
*out++ = 1;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// clear to no light
|
|
|
|
memset (block, 0, size * sizeof(float));
|
|
|
|
|
|
|
|
// add all the lightmaps
|
|
|
|
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++) {
|
2021-01-21 12:24:54 +00:00
|
|
|
*out++ += *lightmap++ * scale / 65536.0;
|
|
|
|
*out++ += *lightmap++ * scale / 65536.0;
|
|
|
|
*out++ += *lightmap++ * scale / 65536.0;
|
2021-01-22 04:08:33 +00:00
|
|
|
out++;
|
2021-01-19 16:25:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// add all the dynamic lights
|
|
|
|
if (surf->dlightframe == r_framecount) {
|
|
|
|
add_dynamic_lights (surf, block);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Vulkan_CalcLightmaps (vulkan_ctx_t *ctx)
|
|
|
|
{
|
|
|
|
/* int i;
|
|
|
|
|
|
|
|
for (i = 0; i < MAX_LIGHTMAPS; i++) {
|
|
|
|
if (!gl_lightmap_polys[i])
|
|
|
|
continue;
|
|
|
|
if (gl_lightmap_modified[i]) {
|
|
|
|
qfglBindTexture (GL_TEXTURE_2D, gl_lightmap_textures + i);
|
|
|
|
GL_UploadLightmap (i);
|
|
|
|
gl_lightmap_modified[i] = false;
|
|
|
|
}
|
|
|
|
}*/
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
vulkan_create_surf_lightmap (msurface_t *surf, vulkan_ctx_t *ctx)
|
|
|
|
{
|
|
|
|
bspctx_t *bctx = ctx->bsp_context;
|
|
|
|
int smax, tmax;
|
|
|
|
|
|
|
|
smax = (surf->extents[0] >> 4) + 1;
|
|
|
|
tmax = (surf->extents[1] >> 4) + 1;
|
|
|
|
|
|
|
|
surf->lightpic = QFV_ScrapSubpic (bctx->light_scrap, smax, tmax);
|
|
|
|
if (!surf->lightpic) {
|
|
|
|
Sys_Error ("FIXME taniwha is being lazy");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
GL_BuildLightmaps
|
|
|
|
|
|
|
|
Builds the lightmap texture with all the surfaces from all brush models
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
Vulkan_BuildLightmaps (model_t **models, int num_models, vulkan_ctx_t *ctx)
|
|
|
|
{
|
|
|
|
bspctx_t *bctx = ctx->bsp_context;
|
|
|
|
int i, j;
|
|
|
|
model_t *m;
|
2021-02-01 10:31:11 +00:00
|
|
|
mod_brush_t *brush;
|
2021-01-19 16:25:54 +00:00
|
|
|
|
|
|
|
QFV_ScrapClear (bctx->light_scrap);
|
|
|
|
|
|
|
|
r_framecount = 1; // no dlightcache
|
|
|
|
|
|
|
|
for (j = 1; j < num_models; j++) {
|
|
|
|
m = models[j];
|
|
|
|
if (!m)
|
|
|
|
break;
|
2021-02-01 10:31:11 +00:00
|
|
|
if (m->path[0] == '*' || m->type != mod_brush) {
|
2021-01-19 16:25:54 +00:00
|
|
|
// sub model surfaces are processed as part of the main model
|
|
|
|
continue;
|
|
|
|
}
|
2021-02-01 10:31:11 +00:00
|
|
|
brush = &m->brush;
|
2021-01-19 16:25:54 +00:00
|
|
|
// non-bsp models don't have surfaces.
|
2021-02-01 10:31:11 +00:00
|
|
|
for (i = 0; i < brush->numsurfaces; i++) {
|
|
|
|
msurface_t *surf = brush->surfaces + i;
|
2021-01-19 16:25:54 +00:00
|
|
|
surf->lightpic = 0; // paranoia
|
|
|
|
if (surf->flags & SURF_DRAWTURB) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (surf->flags & SURF_DRAWSKY) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
vulkan_create_surf_lightmap (surf, ctx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (j = 1; j < num_models; j++) {
|
|
|
|
m = models[j];
|
|
|
|
if (!m) {
|
|
|
|
break;
|
|
|
|
}
|
2021-02-01 10:31:11 +00:00
|
|
|
if (m->path[0] == '*' || m->type != mod_brush) {
|
2021-01-19 16:25:54 +00:00
|
|
|
// sub model surfaces are processed as part of the main model
|
|
|
|
continue;
|
|
|
|
}
|
2021-02-01 10:31:11 +00:00
|
|
|
brush = &m->brush;
|
2021-01-19 16:25:54 +00:00
|
|
|
// non-bsp models don't have surfaces.
|
2021-02-01 10:31:11 +00:00
|
|
|
for (i = 0; i < brush->numsurfaces; i++) {
|
|
|
|
msurface_t *surf = brush->surfaces + i;
|
2021-01-19 16:25:54 +00:00
|
|
|
if (surf->lightpic) {
|
2021-02-01 10:31:11 +00:00
|
|
|
Vulkan_BuildLightMap (brush, surf, ctx);
|
2021-01-19 16:25:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
VkImageView
|
|
|
|
Vulkan_LightmapImageView (vulkan_ctx_t *ctx)
|
|
|
|
{
|
|
|
|
bspctx_t *bctx = ctx->bsp_context;
|
|
|
|
return QFV_ScrapImageView (bctx->light_scrap);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Vulkan_FlushLightmaps (vulkan_ctx_t *ctx)
|
|
|
|
{
|
|
|
|
bspctx_t *bctx = ctx->bsp_context;
|
|
|
|
QFV_ScrapFlush (bctx->light_scrap);
|
|
|
|
}
|