From 14b5649a2b45037ad244a2d2c2dcae6049fc6ce6 Mon Sep 17 00:00:00 2001 From: havoc Date: Sun, 14 Nov 2010 13:24:27 +0000 Subject: [PATCH] illuminated surfaces are now sorted by texture, giving a good fps increase, especially in realtime world lighting, this can be turned off with the r_shadow_sortsurfaces cvar if desired git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@10599 d7cf8633-e32d-0410-b094-e92efae38249 --- gl_rsurf.c | 22 ++++++++++++++++++++++ r_shadow.c | 9 ++++++--- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/gl_rsurf.c b/gl_rsurf.c index 07cc66a0..33277fa9 100644 --- a/gl_rsurf.c +++ b/gl_rsurf.c @@ -949,6 +949,23 @@ static void R_Q1BSP_CallRecursiveGetLightInfo(r_q1bsp_getlightinfo_t *info, qboo } } +static msurface_t *r_q1bsp_getlightinfo_surfaces; + +int R_Q1BSP_GetLightInfo_comparefunc(const void *ap, const void *bp) +{ + int a = *(int*)ap; + int b = *(int*)bp; + const msurface_t *as = r_q1bsp_getlightinfo_surfaces + a; + const msurface_t *bs = r_q1bsp_getlightinfo_surfaces + b; + if (as->texture < bs->texture) + return -1; + if (as->texture > bs->texture) + return 1; + return a - b; +} + +extern cvar_t r_shadow_sortsurfaces; + void R_Q1BSP_GetLightInfo(entity_render_t *ent, vec3_t relativelightorigin, float lightradius, vec3_t outmins, vec3_t outmaxs, int *outleaflist, unsigned char *outleafpvs, int *outnumleafspointer, int *outsurfacelist, unsigned char *outsurfacepvs, int *outnumsurfacespointer, unsigned char *outshadowtrispvs, unsigned char *outlighttrispvs, unsigned char *visitingleafpvs, int numfrustumplanes, const mplane_t *frustumplanes) { r_q1bsp_getlightinfo_t info; @@ -1027,6 +1044,11 @@ void R_Q1BSP_GetLightInfo(entity_render_t *ent, vec3_t relativelightorigin, floa *outnumleafspointer = info.outnumleafs; *outnumsurfacespointer = info.outnumsurfaces; + + // now sort surfaces by texture for faster rendering + r_q1bsp_getlightinfo_surfaces = info.model->data_surfaces; + if (r_shadow_sortsurfaces.integer) + qsort(info.outsurfacelist, info.outnumsurfaces, sizeof(*info.outsurfacelist), R_Q1BSP_GetLightInfo_comparefunc); } void R_Q1BSP_CompileShadowVolume(entity_render_t *ent, vec3_t relativelightorigin, vec3_t relativelightdirection, float lightradius, int numsurfaces, const int *surfacelist) diff --git a/r_shadow.c b/r_shadow.c index 3952e231..aca5afca 100644 --- a/r_shadow.c +++ b/r_shadow.c @@ -312,6 +312,7 @@ cvar_t r_shadow_shadowmapping_nearclip = {CVAR_SAVE, "r_shadow_shadowmapping_nea cvar_t r_shadow_shadowmapping_bias = {CVAR_SAVE, "r_shadow_shadowmapping_bias", "0.03", "shadowmap bias parameter (this is multiplied by nearclip * 1024 / lodsize)"}; cvar_t r_shadow_shadowmapping_polygonfactor = {CVAR_SAVE, "r_shadow_shadowmapping_polygonfactor", "2", "slope-dependent shadowmapping bias"}; cvar_t r_shadow_shadowmapping_polygonoffset = {CVAR_SAVE, "r_shadow_shadowmapping_polygonoffset", "0", "constant shadowmapping bias"}; +cvar_t r_shadow_sortsurfaces = {0, "r_shadow_sortsurfaces", "1", "improve performance by sorting illuminated surfaces by texture"}; cvar_t r_shadow_polygonfactor = {0, "r_shadow_polygonfactor", "0", "how much to enlarge shadow volume polygons when rendering (should be 0!)"}; cvar_t r_shadow_polygonoffset = {0, "r_shadow_polygonoffset", "1", "how much to push shadow volumes into the distance when rendering, to reduce chances of zfighting artifacts (should not be less than 0)"}; cvar_t r_shadow_texture3d = {0, "r_shadow_texture3d", "1", "use 3D voxel textures for spherical attenuation rather than cylindrical (does not affect OpenGL 2.0 render path)"}; @@ -668,6 +669,7 @@ void R_Shadow_Init(void) Cvar_RegisterVariable(&r_shadow_shadowmapping_bias); Cvar_RegisterVariable(&r_shadow_shadowmapping_polygonfactor); Cvar_RegisterVariable(&r_shadow_shadowmapping_polygonoffset); + Cvar_RegisterVariable(&r_shadow_sortsurfaces); Cvar_RegisterVariable(&r_shadow_polygonfactor); Cvar_RegisterVariable(&r_shadow_polygonoffset); Cvar_RegisterVariable(&r_shadow_texture3d); @@ -3818,7 +3820,7 @@ void R_Shadow_DrawPrepass(void) { lightindex = r_shadow_debuglight.integer; light = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex); - if (light && (light->flags & flag)) + if (light && (light->flags & flag) && light->rtlight.draw) R_Shadow_DrawLight(&light->rtlight); } else @@ -3827,13 +3829,14 @@ void R_Shadow_DrawPrepass(void) for (lightindex = 0;lightindex < range;lightindex++) { light = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex); - if (light && (light->flags & flag)) + if (light && (light->flags & flag) && light->rtlight.draw) R_Shadow_DrawLight(&light->rtlight); } } if (r_refdef.scene.rtdlight) for (lnum = 0;lnum < r_refdef.scene.numlights;lnum++) - R_Shadow_DrawLight(r_refdef.scene.lights[lnum]); + if (r_refdef.scene.lights[lnum]->draw) + R_Shadow_DrawLight(r_refdef.scene.lights[lnum]); R_Mesh_ResetRenderTargets(); -- 2.39.2