From 67e892f1869870a3f4aeb158ee87900c1949e864 Mon Sep 17 00:00:00 2001 From: Denis Pauk Date: Mon, 2 Oct 2023 14:58:20 +0300 Subject: [PATCH] Sync R_RecursiveLightPoint implementation between renders --- src/client/refresh/gl1/gl1_light.c | 47 ++++++------ src/client/refresh/gl3/gl3_light.c | 50 ++++++------- src/client/refresh/gl4/gl4_light.c | 50 ++++++------- src/client/refresh/soft/sw_light.c | 116 +++++++++++++++++------------ src/client/refresh/vk/vk_light.c | 22 ++++-- 5 files changed, 159 insertions(+), 126 deletions(-) diff --git a/src/client/refresh/gl1/gl1_light.c b/src/client/refresh/gl1/gl1_light.c index 9b310caa..e8b20b43 100644 --- a/src/client/refresh/gl1/gl1_light.c +++ b/src/client/refresh/gl1/gl1_light.c @@ -181,20 +181,22 @@ R_PushDlights(void) } } -int -R_RecursiveLightPoint(mnode_t *node, vec3_t start, vec3_t end) +static int +R_RecursiveLightPoint(const msurface_t *surfaces, const mnode_t *node, + const lightstyle_t *lightstyles, const vec3_t start, const vec3_t end, + vec3_t pointcolor, vec3_t lightspot) { - float front, back, frac; - int side; - cplane_t *plane; - vec3_t mid; - msurface_t *surf; - int s, t, ds, dt; - int i; - mtexinfo_t *tex; - byte *lightmap; - int maps; - int r; + float front, back, frac; + int side; + cplane_t *plane; + vec3_t mid; + const msurface_t *surf; + int s, t, ds, dt; + int i; + mtexinfo_t *tex; + byte *lightmap; + int maps; + int r; if (node->contents != CONTENTS_NODE) { @@ -209,7 +211,8 @@ R_RecursiveLightPoint(mnode_t *node, vec3_t start, vec3_t end) if ((back < 0) == side) { - return R_RecursiveLightPoint(node->children[side], start, end); + return R_RecursiveLightPoint(surfaces, node->children[side], + lightstyles, start, end, pointcolor, lightspot); } frac = front / (front - back); @@ -218,8 +221,8 @@ R_RecursiveLightPoint(mnode_t *node, vec3_t start, vec3_t end) mid[2] = start[2] + (end[2] - start[2]) * frac; /* go down front side */ - r = R_RecursiveLightPoint(node->children[side], start, mid); - + r = R_RecursiveLightPoint(surfaces, node->children[side], + lightstyles, start, mid, pointcolor, lightspot); if (r >= 0) { return r; /* hit something */ @@ -232,10 +235,8 @@ R_RecursiveLightPoint(mnode_t *node, vec3_t start, vec3_t end) /* check for impact on this node */ VectorCopy(mid, lightspot); - lightplane = plane; - - surf = r_worldmodel->surfaces + node->firstsurface; + surf = surfaces + node->firstsurface; for (i = 0; i < node->numsurfaces; i++, surf++) { if (surf->flags & (SURF_DRAWTURB | SURF_DRAWSKY)) @@ -280,7 +281,7 @@ R_RecursiveLightPoint(mnode_t *node, vec3_t start, vec3_t end) const float *rgb; int j; - rgb = r_newrefdef.lightstyles[surf->styles[maps]].rgb; + rgb = lightstyles[surf->styles[maps]].rgb; /* Apply light level to models */ for (j = 0; j < 3; j++) @@ -299,7 +300,8 @@ R_RecursiveLightPoint(mnode_t *node, vec3_t start, vec3_t end) } /* go down back side */ - return R_RecursiveLightPoint(node->children[!side], mid, end); + return R_RecursiveLightPoint(surfaces, node->children[!side], + lightstyles, mid, end, pointcolor, lightspot); } void @@ -321,7 +323,8 @@ R_LightPoint(entity_t *currententity, vec3_t p, vec3_t color) end[1] = p[1]; end[2] = p[2] - 2048; - r = R_RecursiveLightPoint(r_worldmodel->nodes, p, end); + r = R_RecursiveLightPoint(r_worldmodel->surfaces, r_worldmodel->nodes, + r_newrefdef.lightstyles, p, end, pointcolor, lightspot); if (r == -1) { diff --git a/src/client/refresh/gl3/gl3_light.c b/src/client/refresh/gl3/gl3_light.c index 895cdb91..555e380b 100644 --- a/src/client/refresh/gl3/gl3_light.c +++ b/src/client/refresh/gl3/gl3_light.c @@ -31,7 +31,6 @@ extern gl3lightmapstate_t gl3_lms; int r_dlightframecount; static vec3_t pointcolor; -static cplane_t *lightplane; /* used as shadow plane */ vec3_t lightspot; void @@ -108,19 +107,21 @@ GL3_PushDlights(void) } static int -RecursiveLightPoint(mnode_t *node, vec3_t start, vec3_t end) +R_RecursiveLightPoint(const msurface_t *surfaces, const mnode_t *node, + const lightstyle_t *lightstyles, const vec3_t start, const vec3_t end, + vec3_t pointcolor, vec3_t lightspot) { - float front, back, frac; - int side; - cplane_t *plane; - vec3_t mid; - msurface_t *surf; - int s, t, ds, dt; - int i; - mtexinfo_t *tex; - byte *lightmap; - int maps; - int r; + float front, back, frac; + int side; + cplane_t *plane; + vec3_t mid; + const msurface_t *surf; + int s, t, ds, dt; + int i; + mtexinfo_t *tex; + byte *lightmap; + int maps; + int r; if (node->contents != CONTENTS_NODE) { @@ -135,7 +136,8 @@ RecursiveLightPoint(mnode_t *node, vec3_t start, vec3_t end) if ((back < 0) == side) { - return RecursiveLightPoint(node->children[side], start, end); + return R_RecursiveLightPoint(surfaces, node->children[side], + lightstyles, start, end, pointcolor, lightspot); } frac = front / (front - back); @@ -144,8 +146,8 @@ RecursiveLightPoint(mnode_t *node, vec3_t start, vec3_t end) mid[2] = start[2] + (end[2] - start[2]) * frac; /* go down front side */ - r = RecursiveLightPoint(node->children[side], start, mid); - + r = R_RecursiveLightPoint(surfaces, node->children[side], + lightstyles, start, mid, pointcolor, lightspot); if (r >= 0) { return r; /* hit something */ @@ -158,10 +160,8 @@ RecursiveLightPoint(mnode_t *node, vec3_t start, vec3_t end) /* check for impact on this node */ VectorCopy(mid, lightspot); - lightplane = plane; - - surf = gl3_worldmodel->surfaces + node->firstsurface; + surf = surfaces + node->firstsurface; for (i = 0; i < node->numsurfaces; i++, surf++) { if (surf->flags & (SURF_DRAWTURB | SURF_DRAWSKY)) @@ -201,13 +201,12 @@ RecursiveLightPoint(mnode_t *node, vec3_t start, vec3_t end) lightmap += 3 * (dt * ((surf->extents[0] >> surf->lmshift) + 1) + ds); - for (maps = 0; maps < MAX_LIGHTMAPS_PER_SURFACE && surf->styles[maps] != 255; - maps++) + for (maps = 0; maps < MAXLIGHTMAPS && surf->styles[maps] != 255; maps++) { const float *rgb; int j; - rgb = gl3_newrefdef.lightstyles[surf->styles[maps]].rgb; + rgb = lightstyles[surf->styles[maps]].rgb; /* Apply light level to models */ for (j = 0; j < 3; j++) @@ -226,7 +225,8 @@ RecursiveLightPoint(mnode_t *node, vec3_t start, vec3_t end) } /* go down back side */ - return RecursiveLightPoint(node->children[!side], mid, end); + return R_RecursiveLightPoint(surfaces, node->children[!side], + lightstyles, mid, end, pointcolor, lightspot); } void @@ -251,8 +251,8 @@ GL3_LightPoint(entity_t *currententity, vec3_t p, vec3_t color) // TODO: don't just aggregate the color, but also save position of brightest+nearest light // for shadow position and maybe lighting on model? - - r = RecursiveLightPoint(gl3_worldmodel->nodes, p, end); + r = R_RecursiveLightPoint(gl3_worldmodel->surfaces, gl3_worldmodel->nodes, + gl3_newrefdef.lightstyles, p, end, pointcolor, lightspot); if (r == -1) { diff --git a/src/client/refresh/gl4/gl4_light.c b/src/client/refresh/gl4/gl4_light.c index cd47e43e..9287703d 100644 --- a/src/client/refresh/gl4/gl4_light.c +++ b/src/client/refresh/gl4/gl4_light.c @@ -31,7 +31,6 @@ extern gl4lightmapstate_t gl4_lms; int r_dlightframecount; static vec3_t pointcolor; -static cplane_t *lightplane; /* used as shadow plane */ vec3_t lightspot; void @@ -108,19 +107,21 @@ GL4_PushDlights(void) } static int -RecursiveLightPoint(mnode_t *node, vec3_t start, vec3_t end) +R_RecursiveLightPoint(const msurface_t *surfaces, const mnode_t *node, + const lightstyle_t *lightstyles, const vec3_t start, const vec3_t end, + vec3_t pointcolor, vec3_t lightspot) { - float front, back, frac; - int side; - cplane_t *plane; - vec3_t mid; - msurface_t *surf; - int s, t, ds, dt; - int i; - mtexinfo_t *tex; - byte *lightmap; - int maps; - int r; + float front, back, frac; + int side; + cplane_t *plane; + vec3_t mid; + const msurface_t *surf; + int s, t, ds, dt; + int i; + mtexinfo_t *tex; + byte *lightmap; + int maps; + int r; if (node->contents != CONTENTS_NODE) { @@ -135,7 +136,8 @@ RecursiveLightPoint(mnode_t *node, vec3_t start, vec3_t end) if ((back < 0) == side) { - return RecursiveLightPoint(node->children[side], start, end); + return R_RecursiveLightPoint(surfaces, node->children[side], + lightstyles, start, end, pointcolor, lightspot); } frac = front / (front - back); @@ -144,8 +146,8 @@ RecursiveLightPoint(mnode_t *node, vec3_t start, vec3_t end) mid[2] = start[2] + (end[2] - start[2]) * frac; /* go down front side */ - r = RecursiveLightPoint(node->children[side], start, mid); - + r = R_RecursiveLightPoint(surfaces, node->children[side], + lightstyles, start, mid, pointcolor, lightspot); if (r >= 0) { return r; /* hit something */ @@ -158,10 +160,8 @@ RecursiveLightPoint(mnode_t *node, vec3_t start, vec3_t end) /* check for impact on this node */ VectorCopy(mid, lightspot); - lightplane = plane; - - surf = gl4_worldmodel->surfaces + node->firstsurface; + surf = surfaces + node->firstsurface; for (i = 0; i < node->numsurfaces; i++, surf++) { if (surf->flags & (SURF_DRAWTURB | SURF_DRAWSKY)) @@ -201,13 +201,12 @@ RecursiveLightPoint(mnode_t *node, vec3_t start, vec3_t end) lightmap += 3 * (dt * ((surf->extents[0] >> surf->lmshift) + 1) + ds); - for (maps = 0; maps < MAX_LIGHTMAPS_PER_SURFACE && surf->styles[maps] != 255; - maps++) + for (maps = 0; maps < MAXLIGHTMAPS && surf->styles[maps] != 255; maps++) { const float *rgb; int j; - rgb = gl4_newrefdef.lightstyles[surf->styles[maps]].rgb; + rgb = lightstyles[surf->styles[maps]].rgb; /* Apply light level to models */ for (j = 0; j < 3; j++) @@ -226,7 +225,8 @@ RecursiveLightPoint(mnode_t *node, vec3_t start, vec3_t end) } /* go down back side */ - return RecursiveLightPoint(node->children[!side], mid, end); + return R_RecursiveLightPoint(surfaces, node->children[!side], + lightstyles, mid, end, pointcolor, lightspot); } void @@ -251,8 +251,8 @@ GL4_LightPoint(entity_t *currententity, vec3_t p, vec3_t color) // TODO: don't just aggregate the color, but also save position of brightest+nearest light // for shadow position and maybe lighting on model? - - r = RecursiveLightPoint(gl4_worldmodel->nodes, p, end); + r = R_RecursiveLightPoint(gl4_worldmodel->surfaces, gl4_worldmodel->nodes, + gl4_newrefdef.lightstyles, p, end, pointcolor, lightspot); if (r == -1) { diff --git a/src/client/refresh/soft/sw_light.c b/src/client/refresh/soft/sw_light.c index 822b3d58..98278b68 100644 --- a/src/client/refresh/soft/sw_light.c +++ b/src/client/refresh/soft/sw_light.c @@ -76,86 +76,106 @@ LIGHT SAMPLING ============================================================================= */ static int -RecursiveLightPoint (mnode_t *node, vec3_t start, vec3_t end, vec3_t pointcolor) +R_RecursiveLightPoint(const msurface_t *surfaces, const mnode_t *node, + const lightstyle_t *lightstyles, const vec3_t start, const vec3_t end, + vec3_t pointcolor, vec3_t lightspot) { float front, back, frac; - qboolean side; + int side; cplane_t *plane; vec3_t mid; - msurface_t *surf; - int s, t, ds, dt; - int i; + const msurface_t *surf; + int s, t, ds, dt; + int i; mtexinfo_t *tex; - int maps; - int r; + byte *lightmap; + int maps; + int r; if (node->contents != CONTENTS_NODE) - return -1; // didn't hit anything + { + return -1; /* didn't hit anything */ + } - // calculate mid point - - // FIXME: optimize for axial + /* calculate mid point */ plane = node->plane; - front = DotProduct (start, plane->normal) - plane->dist; - back = DotProduct (end, plane->normal) - plane->dist; + front = DotProduct(start, plane->normal) - plane->dist; + back = DotProduct(end, plane->normal) - plane->dist; side = front < 0; - if ( (back < 0) == side) - return RecursiveLightPoint (node->children[side], start, end, pointcolor); - - frac = front / (front-back); - mid[0] = start[0] + (end[0] - start[0])*frac; - mid[1] = start[1] + (end[1] - start[1])*frac; - mid[2] = start[2] + (end[2] - start[2])*frac; - if (plane->type < 3) // axial planes - mid[plane->type] = plane->dist; - - // go down front side - r = RecursiveLightPoint (node->children[side], start, mid, pointcolor); - if (r >= 0) - return r; // hit something - - // check for impact on this node - surf = r_worldmodel->surfaces + node->firstsurface; - for (i=0 ; inumsurfaces ; i++, surf++) + if ((back < 0) == side) { - byte *lightmap; + return R_RecursiveLightPoint(surfaces, node->children[side], + lightstyles, start, end, pointcolor, lightspot); + } - if (surf->flags&(SURF_DRAWTURB|SURF_DRAWSKY)) - continue; // no lightmaps + frac = front / (front - back); + mid[0] = start[0] + (end[0] - start[0]) * frac; + mid[1] = start[1] + (end[1] - start[1]) * frac; + mid[2] = start[2] + (end[2] - start[2]) * frac; + + /* go down front side */ + r = R_RecursiveLightPoint(surfaces, node->children[side], + lightstyles, start, mid, pointcolor, lightspot); + if (r >= 0) + { + return r; /* hit something */ + } + + if ((back < 0) == side) + { + return -1; /* didn't hit anuthing */ + } + + /* check for impact on this node */ + VectorCopy(mid, lightspot); + + surf = surfaces + node->firstsurface; + for (i = 0; i < node->numsurfaces; i++, surf++) + { + if (surf->flags & (SURF_DRAWTURB | SURF_DRAWSKY)) + { + continue; /* no lightmaps */ + } tex = surf->texinfo; - s = DotProduct (mid, tex->vecs[0]) + tex->vecs[0][3]; - t = DotProduct (mid, tex->vecs[1]) + tex->vecs[1][3]; - if (s < surf->texturemins[0] || - t < surf->texturemins[1]) + s = DotProduct(mid, tex->vecs[0]) + tex->vecs[0][3]; + t = DotProduct(mid, tex->vecs[1]) + tex->vecs[1][3]; + + if ((s < surf->texturemins[0]) || + (t < surf->texturemins[1])) + { continue; + } ds = s - surf->texturemins[0]; dt = t - surf->texturemins[1]; - if ( ds > surf->extents[0] || dt > surf->extents[1] ) + if ((ds > surf->extents[0]) || (dt > surf->extents[1])) + { continue; + } if (!surf->samples) + { return 0; + } ds >>= surf->lmshift; dt >>= surf->lmshift; lightmap = surf->samples; - VectorCopy (vec3_origin, pointcolor); + VectorCopy(vec3_origin, pointcolor); lightmap += 3 * (dt * ((surf->extents[0] >> surf->lmshift) + 1) + ds); - for (maps = 0 ; maps < MAXLIGHTMAPS && surf->styles[maps] != 255 ; - maps++) + for (maps = 0; maps < MAXLIGHTMAPS && surf->styles[maps] != 255; maps++) { const float *rgb; int j; - rgb = r_newrefdef.lightstyles[surf->styles[maps]].rgb; + rgb = lightstyles[surf->styles[maps]].rgb; /* Apply light level to models */ for (j = 0; j < 3; j++) @@ -173,10 +193,13 @@ RecursiveLightPoint (mnode_t *node, vec3_t start, vec3_t end, vec3_t pointcolor) return 1; } - // go down back side - return RecursiveLightPoint (node->children[!side], mid, end, pointcolor); + /* go down back side */ + return R_RecursiveLightPoint(surfaces, node->children[!side], + lightstyles, mid, end, pointcolor, lightspot); } +vec3_t lightspot; + /* =============== R_LightPoint @@ -202,7 +225,8 @@ R_LightPoint (const entity_t *currententity, vec3_t p, vec3_t color) end[1] = p[1]; end[2] = p[2] - 2048; - r = RecursiveLightPoint (r_worldmodel->nodes, p, end, pointcolor); + r = R_RecursiveLightPoint(r_worldmodel->surfaces, r_worldmodel->nodes, + r_newrefdef.lightstyles, p, end, pointcolor, lightspot); if (r == -1) { diff --git a/src/client/refresh/vk/vk_light.c b/src/client/refresh/vk/vk_light.c index 7d77aa90..1ccc8bf4 100644 --- a/src/client/refresh/vk/vk_light.c +++ b/src/client/refresh/vk/vk_light.c @@ -174,13 +174,15 @@ LIGHT SAMPLING vec3_t lightspot; static int -RecursiveLightPoint(mnode_t *node, vec3_t start, vec3_t end, vec3_t pointcolor) +R_RecursiveLightPoint(const msurface_t *surfaces, const mnode_t *node, + const lightstyle_t *lightstyles, const vec3_t start, const vec3_t end, + vec3_t pointcolor, vec3_t lightspot) { float front, back, frac; int side; cplane_t *plane; vec3_t mid; - msurface_t *surf; + const msurface_t *surf; int s, t, ds, dt; int i; mtexinfo_t *tex; @@ -201,7 +203,8 @@ RecursiveLightPoint(mnode_t *node, vec3_t start, vec3_t end, vec3_t pointcolor) if ((back < 0) == side) { - return RecursiveLightPoint(node->children[side], start, end, pointcolor); + return R_RecursiveLightPoint(surfaces, node->children[side], + lightstyles, start, end, pointcolor, lightspot); } frac = front / (front - back); @@ -210,7 +213,8 @@ RecursiveLightPoint(mnode_t *node, vec3_t start, vec3_t end, vec3_t pointcolor) mid[2] = start[2] + (end[2] - start[2]) * frac; /* go down front side */ - r = RecursiveLightPoint(node->children[side], start, mid, pointcolor); + r = R_RecursiveLightPoint(surfaces, node->children[side], + lightstyles, start, mid, pointcolor, lightspot); if (r >= 0) { return r; /* hit something */ @@ -224,7 +228,7 @@ RecursiveLightPoint(mnode_t *node, vec3_t start, vec3_t end, vec3_t pointcolor) /* check for impact on this node */ VectorCopy(mid, lightspot); - surf = r_worldmodel->surfaces + node->firstsurface; + surf = surfaces + node->firstsurface; for (i = 0; i < node->numsurfaces; i++, surf++) { if (surf->flags & (SURF_DRAWTURB | SURF_DRAWSKY)) @@ -269,7 +273,7 @@ RecursiveLightPoint(mnode_t *node, vec3_t start, vec3_t end, vec3_t pointcolor) const float *rgb; int j; - rgb = r_newrefdef.lightstyles[surf->styles[maps]].rgb; + rgb = lightstyles[surf->styles[maps]].rgb; /* Apply light level to models */ for (j = 0; j < 3; j++) @@ -288,7 +292,8 @@ RecursiveLightPoint(mnode_t *node, vec3_t start, vec3_t end, vec3_t pointcolor) } /* go down back side */ - return RecursiveLightPoint(node->children[!side], mid, end, pointcolor); + return R_RecursiveLightPoint(surfaces, node->children[!side], + lightstyles, mid, end, pointcolor, lightspot); } void @@ -309,7 +314,8 @@ R_LightPoint(vec3_t p, vec3_t color, entity_t *currententity) end[1] = p[1]; end[2] = p[2] - 2048; - r = RecursiveLightPoint(r_worldmodel->nodes, p, end, pointcolor); + r = R_RecursiveLightPoint(r_worldmodel->surfaces, r_worldmodel->nodes, + r_newrefdef.lightstyles, p, end, pointcolor, lightspot); if (r == -1) {