light: clean up LightFace() function

This commit is contained in:
Eric Wasylishen 2017-02-09 15:19:38 -07:00
parent d30c970d8b
commit 71a3dd769a
4 changed files with 36 additions and 66 deletions

View File

@ -150,15 +150,6 @@ typedef struct {
lightmapdict_t lightmapsByStyle; lightmapdict_t lightmapsByStyle;
} lightsurf_t; } lightsurf_t;
struct ltface_ctx
{
const bsp2_t *bsp;
lightsurf_t *lightsurf;
const globalconfig_t *cfg;
};
extern struct ltface_ctx *ltface_ctxs;
/* debug */ /* debug */
typedef enum { typedef enum {

View File

@ -49,8 +49,6 @@ vec_t GetLightValue(const globalconfig_t &cfg, const light_t *entity, vec_t dist
void GetDirectLighting(const globalconfig_t &cfg, raystream_t *rs, const vec3_t origin, const vec3_t normal, vec3_t colorout); void GetDirectLighting(const globalconfig_t &cfg, raystream_t *rs, const vec3_t origin, const vec3_t normal, vec3_t colorout);
void SetupDirt(globalconfig_t &cfg); void SetupDirt(globalconfig_t &cfg);
float DirtAtPoint(const globalconfig_t &cfg, raystream_t *rs, const vec3_t point, const vec3_t normal, const dmodel_t *selfshadow); float DirtAtPoint(const globalconfig_t &cfg, raystream_t *rs, const vec3_t point, const vec3_t normal, const dmodel_t *selfshadow);
void LightFaceInit(const bsp2_t *bsp, struct ltface_ctx *ctx); void LightFace(const bsp2_t *bsp, bsp2_dface_t *face, facesup_t *facesup, const globalconfig_t &cfg);
void LightFaceShutdown(struct ltface_ctx *ctx);
void LightFace(bsp2_dface_t *face, facesup_t *facesup, const modelinfo_t *modelinfo, struct ltface_ctx *ctx);
#endif /* __LIGHT_LTFACE_H__ */ #endif /* __LIGHT_LTFACE_H__ */

View File

@ -99,8 +99,6 @@ uint64_t *extended_texinfo_flags = NULL;
char mapfilename[1024]; char mapfilename[1024];
struct ltface_ctx *ltface_ctxs;
int dump_facenum = -1; int dump_facenum = -1;
bool dump_face; bool dump_face;
vec3_t dump_face_point = {0,0,0}; vec3_t dump_face_point = {0,0,0};
@ -228,10 +226,7 @@ const modelinfo_t *ModelInfoForFace(const bsp2_t *bsp, int facenum)
static void * static void *
LightThread(void *arg) LightThread(void *arg)
{ {
int facenum, i;
const bsp2_t *bsp = (const bsp2_t *)arg; const bsp2_t *bsp = (const bsp2_t *)arg;
const modelinfo_t *face_modelinfo;
struct ltface_ctx *ctx;
#ifdef HAVE_EMBREE #ifdef HAVE_EMBREE
_MM_SET_FLUSH_ZERO_MODE(_MM_FLUSH_ZERO_ON); _MM_SET_FLUSH_ZERO_MODE(_MM_FLUSH_ZERO_ON);
@ -239,17 +234,14 @@ LightThread(void *arg)
#endif #endif
while (1) { while (1) {
facenum = GetThreadWork(); const int facenum = GetThreadWork();
if (facenum == -1) if (facenum == -1)
break; break;
ctx = &ltface_ctxs[facenum]; bsp2_dface_t *f = &bsp->dfaces[facenum];
LightFaceInit(bsp, ctx);
ctx->cfg = &cfg_static;
/* Find the correct model offset */ /* Find the correct model offset */
face_modelinfo = ModelInfoForFace(bsp, facenum); const modelinfo_t *face_modelinfo = ModelInfoForFace(bsp, facenum);
if (face_modelinfo == NULL) { if (face_modelinfo == NULL) {
// ericw -- silenced this warning becasue is causes spam when "skip" faces are used // ericw -- silenced this warning becasue is causes spam when "skip" faces are used
//logprint("warning: no model has face %d\n", facenum); //logprint("warning: no model has face %d\n", facenum);
@ -257,27 +249,25 @@ LightThread(void *arg)
} }
if (!faces_sup) if (!faces_sup)
LightFace(bsp->dfaces + facenum, NULL, face_modelinfo, ctx); LightFace(bsp, f, nullptr, cfg_static);
else if (scaledonly) else if (scaledonly)
{ {
bsp->dfaces[facenum].lightofs = -1; f->lightofs = -1;
bsp->dfaces[facenum].styles[0] = 255; f->styles[0] = 255;
LightFace(bsp->dfaces + facenum, faces_sup + facenum, face_modelinfo, ctx); LightFace(bsp, f, faces_sup + facenum, cfg_static);
} }
else if (faces_sup[facenum].lmscale == face_modelinfo->lightmapscale) else if (faces_sup[facenum].lmscale == face_modelinfo->lightmapscale)
{ {
LightFace(bsp->dfaces + facenum, NULL, face_modelinfo, ctx); LightFace(bsp, f, nullptr, cfg_static);
faces_sup[facenum].lightofs = bsp->dfaces[facenum].lightofs; faces_sup[facenum].lightofs = f->lightofs;
for (i = 0; i < MAXLIGHTMAPS; i++) for (int i = 0; i < MAXLIGHTMAPS; i++)
faces_sup[facenum].styles[i] = bsp->dfaces[facenum].styles[i]; faces_sup[facenum].styles[i] = f->styles[i];
} }
else else
{ {
LightFace(bsp->dfaces + facenum, NULL, face_modelinfo, ctx); LightFace(bsp, f, nullptr, cfg_static);
LightFace(bsp->dfaces + facenum, faces_sup + facenum, face_modelinfo, ctx); LightFace(bsp, f, faces_sup + facenum, cfg_static);
} }
LightFaceShutdown(ctx);
} }
return NULL; return NULL;
@ -420,9 +410,6 @@ LightWorld(bspdata_t *bspdata, qboolean forcedscale)
EdgeToFaceMap = MakeEdgeToFaceMap(bsp); EdgeToFaceMap = MakeEdgeToFaceMap(bsp);
CalcualateVertexNormals(bsp); CalcualateVertexNormals(bsp);
/* ericw -- alloc memory */
ltface_ctxs = (struct ltface_ctx *)calloc(bsp->numfaces, sizeof(struct ltface_ctx));
#if 0 #if 0
lightbatchthread_info_t info; lightbatchthread_info_t info;
info.all_batches = MakeLightingBatches(bsp); info.all_batches = MakeLightingBatches(bsp);

View File

@ -2330,30 +2330,20 @@ WriteLightmaps(const bsp2_t *bsp, bsp2_dface_t *face, facesup_t *facesup, const
} }
} }
void LightFaceInit(const bsp2_t *bsp, struct ltface_ctx *ctx) static void LightFaceShutdown(lightsurf_t *lightsurf)
{ {
memset(ctx, 0, sizeof(*ctx)); for (auto &lm : lightsurf->lightmapsByStyle) {
ctx->bsp = bsp;
}
void LightFaceShutdown(struct ltface_ctx *ctx)
{
if (!ctx->lightsurf)
return;
for (auto &lm : ctx->lightsurf->lightmapsByStyle) {
free(lm.samples); free(lm.samples);
} }
free(ctx->lightsurf->points); free(lightsurf->points);
free(ctx->lightsurf->normals); free(lightsurf->normals);
free(ctx->lightsurf->occlusion); free(lightsurf->occlusion);
free(ctx->lightsurf->occluded); free(lightsurf->occluded);
delete ctx->lightsurf->stream; delete lightsurf->stream;
delete ctx->lightsurf; delete lightsurf;
} }
/* /*
@ -2362,9 +2352,14 @@ void LightFaceShutdown(struct ltface_ctx *ctx)
* ============ * ============
*/ */
void void
LightFace(bsp2_dface_t *face, facesup_t *facesup, const modelinfo_t *modelinfo, struct ltface_ctx *ctx) LightFace(const bsp2_t *bsp, bsp2_dface_t *face, facesup_t *facesup, const globalconfig_t &cfg)
{ {
const bsp2_t *bsp = ctx->bsp; /* Find the correct model offset */
const modelinfo_t *modelinfo = ModelInfoForFace(bsp, Face_GetNum(bsp, face));
if (modelinfo == nullptr) {
return;
}
const char *texname = Face_TextureName(bsp, face); const char *texname = Face_TextureName(bsp, face);
/* One extra lightmap is allocated to simplify handling overflow */ /* One extra lightmap is allocated to simplify handling overflow */
@ -2394,11 +2389,8 @@ LightFace(bsp2_dface_t *face, facesup_t *facesup, const modelinfo_t *modelinfo,
return; return;
/* all good, this face is going to be lightmapped. */ /* all good, this face is going to be lightmapped. */
ctx->lightsurf = new lightsurf_t {}; lightsurf_t *lightsurf = new lightsurf_t {};
lightsurf_t *lightsurf = ctx->lightsurf; lightsurf->cfg = &cfg;
lightsurf->cfg = ctx->cfg;
const globalconfig_t &cfg = *lightsurf->cfg;
/* if liquid doesn't have the TEX_SPECIAL flag set, the map was qbsp'ed with /* if liquid doesn't have the TEX_SPECIAL flag set, the map was qbsp'ed with
* lit water in mind. In that case receive light from both top and bottom. * lit water in mind. In that case receive light from both top and bottom.
@ -2439,7 +2431,7 @@ LightFace(bsp2_dface_t *face, facesup_t *facesup, const modelinfo_t *modelinfo,
LightFace_Sky (&sun, lightsurf, lightmaps); LightFace_Sky (&sun, lightsurf, lightmaps);
/* add indirect lighting */ /* add indirect lighting */
LightFace_Bounce(ctx->bsp, face, lightsurf, lightmaps); LightFace_Bounce(bsp, face, lightsurf, lightmaps);
} }
/* minlight - Use the greater of global or model minlight. */ /* minlight - Use the greater of global or model minlight. */
@ -2471,7 +2463,7 @@ LightFace(bsp2_dface_t *face, facesup_t *facesup, const modelinfo_t *modelinfo,
/* bounce debug */ /* bounce debug */
// TODO: add a BounceDebug function that clear the lightmap to make the code more clear // TODO: add a BounceDebug function that clear the lightmap to make the code more clear
if (debugmode == debugmode_bounce) if (debugmode == debugmode_bounce)
LightFace_Bounce(ctx->bsp, face, lightsurf, lightmaps); LightFace_Bounce(bsp, face, lightsurf, lightmaps);
/* replace lightmaps with AO for debugging */ /* replace lightmaps with AO for debugging */
if (debugmode == debugmode_dirt) if (debugmode == debugmode_dirt)
@ -2494,4 +2486,6 @@ LightFace(bsp2_dface_t *face, facesup_t *facesup, const modelinfo_t *modelinfo,
} }
WriteLightmaps(bsp, face, facesup, lightsurf, lightmaps); WriteLightmaps(bsp, face, facesup, lightsurf, lightmaps);
LightFaceShutdown(lightsurf);
} }