]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - r_shadow.c
implemented Shadow Volume BSP based culling of lit surfaces, this is slightly better...
[xonotic/darkplaces.git] / r_shadow.c
index 69ffabf3ecbb2f061c98c26e2748954017b3f9e4..76ed116fdea7eec7811dac0f75140beab139eea8 100644 (file)
@@ -146,6 +146,7 @@ typedef enum r_shadow_rendermode_e
 {
        R_SHADOW_RENDERMODE_NONE,
        R_SHADOW_RENDERMODE_STENCIL,
+       R_SHADOW_RENDERMODE_SEPARATESTENCIL,
        R_SHADOW_RENDERMODE_STENCILTWOSIDE,
        R_SHADOW_RENDERMODE_LIGHT_VERTEX,
        R_SHADOW_RENDERMODE_LIGHT_DOT3,
@@ -184,6 +185,10 @@ int r_shadow_buffer_numsurfacepvsbytes;
 unsigned char *r_shadow_buffer_surfacepvs;
 int *r_shadow_buffer_surfacelist;
 
+// current light's cull box (copied out of an rtlight or calculated by GetLightInfo)
+vec3_t r_shadow_rtlight_cullmins;
+vec3_t r_shadow_rtlight_cullmaxs;
+
 rtexturepool_t *r_shadow_texturepool;
 rtexture_t *r_shadow_attenuation2dtexture;
 rtexture_t *r_shadow_attenuation3dtexture;
@@ -208,17 +213,19 @@ cvar_t r_shadow_portallight = {0, "r_shadow_portallight", "1", "use portal culli
 cvar_t r_shadow_projectdistance = {0, "r_shadow_projectdistance", "1000000", "how far to cast shadows"};
 cvar_t r_shadow_realtime_dlight = {CVAR_SAVE, "r_shadow_realtime_dlight", "1", "enables rendering of dynamic lights such as explosions and rocket light"};
 cvar_t r_shadow_realtime_dlight_shadows = {CVAR_SAVE, "r_shadow_realtime_dlight_shadows", "1", "enables rendering of shadows from dynamic lights"};
-cvar_t r_shadow_realtime_dlight_portalculling = {0, "r_shadow_realtime_dlight_portalculling", "0", "enables portal culling optimizations on dynamic lights (slow!  you probably don't want this!)"};
+cvar_t r_shadow_realtime_dlight_svbspculling = {0, "r_shadow_realtime_dlight_svbspculling", "0", "enables svbsp optimization on dynamic lights (very slow!)"};
 cvar_t r_shadow_realtime_world = {CVAR_SAVE, "r_shadow_realtime_world", "0", "enables rendering of full world lighting (whether loaded from the map, or a .rtlights file, or a .ent file, or a .lights file produced by hlight)"};
 cvar_t r_shadow_realtime_world_dlightshadows = {CVAR_SAVE, "r_shadow_realtime_world_dlightshadows", "1", "enables shadows from dynamic lights when using full world lighting"};
 cvar_t r_shadow_realtime_world_lightmaps = {CVAR_SAVE, "r_shadow_realtime_world_lightmaps", "0", "brightness to render lightmaps when using full world lighting, try 0.5 for a tenebrae-like appearance"};
 cvar_t r_shadow_realtime_world_shadows = {CVAR_SAVE, "r_shadow_realtime_world_shadows", "1", "enables rendering of shadows from world lights"};
 cvar_t r_shadow_realtime_world_compile = {0, "r_shadow_realtime_world_compile", "1", "enables compilation of world lights for higher performance rendering"};
 cvar_t r_shadow_realtime_world_compileshadow = {0, "r_shadow_realtime_world_compileshadow", "1", "enables compilation of shadows from world lights for higher performance rendering"};
+cvar_t r_shadow_realtime_world_compilesvbsp = {0, "r_shadow_realtime_world_compilesvbsp", "1", "enables svbsp optimization during compilation"};
 cvar_t r_shadow_scissor = {0, "r_shadow_scissor", "1", "use scissor optimization of light rendering (restricts rendering to the portion of the screen affected by the light)"};
 cvar_t r_shadow_shadow_polygonfactor = {0, "r_shadow_shadow_polygonfactor", "0", "how much to enlarge shadow volume polygons when rendering (should be 0!)"};
 cvar_t r_shadow_shadow_polygonoffset = {0, "r_shadow_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 r_glsl lighting)"};
+cvar_t gl_ext_separatestencil = {0, "gl_ext_separatetencil", "1", "make use of OpenGL 2.0 glStencilOpSeparate or GL_ATI_separate_stencil extension"};
 cvar_t gl_ext_stenciltwoside = {0, "gl_ext_stenciltwoside", "1", "make use of GL_EXT_stenciltwoside extension (NVIDIA only)"};
 cvar_t r_editlights = {0, "r_editlights", "0", "enables .rtlights file editing mode"};
 cvar_t r_editlights_cursordistance = {0, "r_editlights_cursordistance", "1024", "maximum distance of cursor from eye"};
@@ -360,7 +367,6 @@ void R_Shadow_Help_f(void)
 "r_shadow_projectdistance : shadow volume projection distance\n"
 "r_shadow_realtime_dlight : use high quality dynamic lights in normal mode\n"
 "r_shadow_realtime_dlight_shadows : cast shadows from dlights\n"
-"r_shadow_realtime_dlight_portalculling : work hard to reduce graphics work\n"
 "r_shadow_realtime_world : use high quality world lighting mode\n"
 "r_shadow_realtime_world_dlightshadows : cast shadows from dlights\n"
 "r_shadow_realtime_world_lightmaps : use lightmaps in addition to lights\n"
@@ -394,17 +400,19 @@ void R_Shadow_Init(void)
        Cvar_RegisterVariable(&r_shadow_projectdistance);
        Cvar_RegisterVariable(&r_shadow_realtime_dlight);
        Cvar_RegisterVariable(&r_shadow_realtime_dlight_shadows);
-       Cvar_RegisterVariable(&r_shadow_realtime_dlight_portalculling);
+       Cvar_RegisterVariable(&r_shadow_realtime_dlight_svbspculling);
        Cvar_RegisterVariable(&r_shadow_realtime_world);
        Cvar_RegisterVariable(&r_shadow_realtime_world_dlightshadows);
        Cvar_RegisterVariable(&r_shadow_realtime_world_lightmaps);
        Cvar_RegisterVariable(&r_shadow_realtime_world_shadows);
        Cvar_RegisterVariable(&r_shadow_realtime_world_compile);
        Cvar_RegisterVariable(&r_shadow_realtime_world_compileshadow);
+       Cvar_RegisterVariable(&r_shadow_realtime_world_compilesvbsp);
        Cvar_RegisterVariable(&r_shadow_scissor);
        Cvar_RegisterVariable(&r_shadow_shadow_polygonfactor);
        Cvar_RegisterVariable(&r_shadow_shadow_polygonoffset);
        Cvar_RegisterVariable(&r_shadow_texture3d);
+       Cvar_RegisterVariable(&gl_ext_separatestencil);
        Cvar_RegisterVariable(&gl_ext_stenciltwoside);
        if (gamemode == GAME_TENEBRAE)
        {
@@ -853,6 +861,8 @@ void R_Shadow_ValidateCvars(void)
 {
        if (r_shadow_texture3d.integer && !gl_texture3d)
                Cvar_SetValueQuick(&r_shadow_texture3d, 0);
+       if (gl_ext_separatestencil.integer && !gl_support_separatestencil)
+               Cvar_SetValueQuick(&gl_ext_separatestencil, 0);
        if (gl_ext_stenciltwoside.integer && !gl_support_stenciltwoside)
                Cvar_SetValueQuick(&gl_ext_stenciltwoside, 0);
 }
@@ -885,13 +895,16 @@ void R_Shadow_RenderMode_Begin(void)
        R_Mesh_ColorPointer(NULL);
        R_Mesh_ResetTextureState();
        GL_BlendFunc(GL_ONE, GL_ZERO);
+       GL_DepthTest(true);
        GL_DepthMask(false);
        GL_Color(0, 0, 0, 1);
        GL_Scissor(r_view.x, r_view.y, r_view.width, r_view.height);
 
        r_shadow_rendermode = R_SHADOW_RENDERMODE_NONE;
 
-       if (gl_ext_stenciltwoside.integer)
+       if (gl_ext_separatestencil.integer)
+               r_shadow_shadowingrendermode = R_SHADOW_RENDERMODE_SEPARATESTENCIL;
+       else if (gl_ext_stenciltwoside.integer)
                r_shadow_shadowingrendermode = R_SHADOW_RENDERMODE_STENCILTWOSIDE;
        else
                r_shadow_shadowingrendermode = R_SHADOW_RENDERMODE_STENCIL;
@@ -922,22 +935,36 @@ void R_Shadow_RenderMode_Reset(void)
        }
        R_Mesh_ColorPointer(NULL);
        R_Mesh_ResetTextureState();
+       GL_DepthTest(true);
+       GL_DepthMask(false);
+       qglDepthFunc(GL_LEQUAL);CHECKGLERROR
+       qglPolygonOffset(r_refdef.polygonfactor, r_refdef.polygonoffset);CHECKGLERROR
+       qglDisable(GL_STENCIL_TEST);CHECKGLERROR
+       qglStencilMask(~0);CHECKGLERROR
+       qglStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);CHECKGLERROR
+       qglStencilFunc(GL_ALWAYS, 128, ~0);CHECKGLERROR
+       GL_CullFace(GL_FRONT); // quake is backwards, this culls back faces
+       GL_Color(1, 1, 1, 1);
+       GL_ColorMask(r_view.colormask[0], r_view.colormask[1], r_view.colormask[2], 1);
+       GL_BlendFunc(GL_ONE, GL_ZERO);
 }
 
 void R_Shadow_RenderMode_StencilShadowVolumes(void)
 {
        CHECKGLERROR
        R_Shadow_RenderMode_Reset();
-       GL_Color(1, 1, 1, 1);
        GL_ColorMask(0, 0, 0, 0);
-       GL_BlendFunc(GL_ONE, GL_ZERO);
-       GL_DepthMask(false);
        qglPolygonOffset(r_refdef.shadowpolygonfactor, r_refdef.shadowpolygonoffset);CHECKGLERROR
        qglDepthFunc(GL_LESS);CHECKGLERROR
        qglEnable(GL_STENCIL_TEST);CHECKGLERROR
-       qglStencilFunc(GL_ALWAYS, 128, ~0);CHECKGLERROR
        r_shadow_rendermode = r_shadow_shadowingrendermode;
-       if (r_shadow_rendermode == R_SHADOW_RENDERMODE_STENCILTWOSIDE)
+       if (r_shadow_rendermode == R_SHADOW_RENDERMODE_SEPARATESTENCIL)
+       {
+               GL_CullFace(GL_NONE);
+               qglStencilOpSeparate(GL_BACK, GL_KEEP, GL_INCR, GL_KEEP);CHECKGLERROR // quake is backwards, this is front faces
+               qglStencilOpSeparate(GL_FRONT, GL_KEEP, GL_DECR, GL_KEEP);CHECKGLERROR // quake is backwards, this is back faces
+       }
+       else if (r_shadow_rendermode == R_SHADOW_RENDERMODE_STENCILTWOSIDE)
        {
                GL_CullFace(GL_NONE);
                qglEnable(GL_STENCIL_TEST_TWO_SIDE_EXT);CHECKGLERROR
@@ -948,13 +975,6 @@ void R_Shadow_RenderMode_StencilShadowVolumes(void)
                qglStencilMask(~0);CHECKGLERROR
                qglStencilOp(GL_KEEP, GL_DECR, GL_KEEP);CHECKGLERROR
        }
-       else
-       {
-               GL_CullFace(GL_FRONT); // quake is backwards, this culls back faces
-               qglStencilMask(~0);CHECKGLERROR
-               // this is changed by every shadow render so its value here is unimportant
-               qglStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);CHECKGLERROR
-       }
        GL_Clear(GL_STENCIL_BUFFER_BIT);
        r_refdef.stats.lights_clears++;
 }
@@ -964,32 +984,17 @@ void R_Shadow_RenderMode_Lighting(qboolean stenciltest, qboolean transparent)
        CHECKGLERROR
        R_Shadow_RenderMode_Reset();
        GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
-       GL_DepthMask(false);
-       qglPolygonOffset(r_refdef.polygonfactor, r_refdef.polygonoffset);CHECKGLERROR
-       //qglDisable(GL_POLYGON_OFFSET_FILL);CHECKGLERROR
-       GL_Color(1, 1, 1, 1);
-       GL_ColorMask(r_view.colormask[0], r_view.colormask[1], r_view.colormask[2], 1);
-       if (transparent)
-       {
-               qglDepthFunc(GL_LEQUAL);CHECKGLERROR
-       }
-       else
+       if (!transparent)
        {
                qglDepthFunc(GL_EQUAL);CHECKGLERROR
        }
        if (stenciltest)
        {
                qglEnable(GL_STENCIL_TEST);CHECKGLERROR
+               // only draw light where this geometry was already rendered AND the
+               // stencil is 128 (values other than this mean shadow)
+               qglStencilFunc(GL_EQUAL, 128, ~0);CHECKGLERROR
        }
-       else
-       {
-               qglDisable(GL_STENCIL_TEST);CHECKGLERROR
-       }
-       qglStencilMask(~0);CHECKGLERROR
-       qglStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);CHECKGLERROR
-       // only draw light where this geometry was already rendered AND the
-       // stencil is 128 (values other than this mean shadow)
-       qglStencilFunc(GL_EQUAL, 128, ~0);CHECKGLERROR
        r_shadow_rendermode = r_shadow_lightingrendermode;
        // do global setup needed for the chosen lighting mode
        if (r_shadow_rendermode == R_SHADOW_RENDERMODE_LIGHT_GLSL)
@@ -1016,19 +1021,10 @@ void R_Shadow_RenderMode_VisibleShadowVolumes(void)
        CHECKGLERROR
        R_Shadow_RenderMode_Reset();
        GL_BlendFunc(GL_ONE, GL_ONE);
-       GL_DepthMask(false);
-       qglPolygonOffset(r_refdef.polygonfactor, r_refdef.polygonoffset);CHECKGLERROR
+       GL_DepthTest(r_showshadowvolumes.integer < 2);
        GL_Color(0.0, 0.0125 * r_view.colorscale, 0.1 * r_view.colorscale, 1);
-       GL_ColorMask(r_view.colormask[0], r_view.colormask[1], r_view.colormask[2], 1);
-       if (r_showshadowvolumes.integer >= 2)
-       {
-               qglDepthFunc(GL_ALWAYS);CHECKGLERROR
-       }
-       else
-       {
-               qglDepthFunc(GL_GEQUAL);CHECKGLERROR
-       }
-       qglDisable(GL_STENCIL_TEST);CHECKGLERROR
+       qglPolygonOffset(r_refdef.shadowpolygonfactor, r_refdef.shadowpolygonoffset);CHECKGLERROR
+       GL_CullFace(GL_NONE);
        r_shadow_rendermode = R_SHADOW_RENDERMODE_VISIBLEVOLUMES;
 }
 
@@ -1037,29 +1033,16 @@ void R_Shadow_RenderMode_VisibleLighting(qboolean stenciltest, qboolean transpar
        CHECKGLERROR
        R_Shadow_RenderMode_Reset();
        GL_BlendFunc(GL_ONE, GL_ONE);
-       GL_DepthMask(false);
-       qglPolygonOffset(r_refdef.polygonfactor, r_refdef.polygonoffset);CHECKGLERROR
+       GL_DepthTest(r_showlighting.integer < 2);
        GL_Color(0.1 * r_view.colorscale, 0.0125 * r_view.colorscale, 0, 1);
-       GL_ColorMask(r_view.colormask[0], r_view.colormask[1], r_view.colormask[2], 1);
-       if (r_showshadowvolumes.integer >= 2)
-       {
-               qglDepthFunc(GL_ALWAYS);CHECKGLERROR
-       }
-       else if (transparent)
-       {
-               qglDepthFunc(GL_LEQUAL);CHECKGLERROR
-       }
-       else
+       if (!transparent)
        {
                qglDepthFunc(GL_EQUAL);CHECKGLERROR
        }
        if (stenciltest)
        {
                qglEnable(GL_STENCIL_TEST);CHECKGLERROR
-       }
-       else
-       {
-               qglDisable(GL_STENCIL_TEST);CHECKGLERROR
+               qglStencilFunc(GL_EQUAL, 128, ~0);CHECKGLERROR
        }
        r_shadow_rendermode = R_SHADOW_RENDERMODE_VISIBLELIGHTING;
 }
@@ -1069,23 +1052,8 @@ void R_Shadow_RenderMode_End(void)
        CHECKGLERROR
        R_Shadow_RenderMode_Reset();
        R_Shadow_RenderMode_ActiveLight(NULL);
-       GL_BlendFunc(GL_ONE, GL_ZERO);
        GL_DepthMask(true);
-       qglPolygonOffset(r_refdef.polygonfactor, r_refdef.polygonoffset);CHECKGLERROR
-       //qglDisable(GL_POLYGON_OFFSET_FILL);CHECKGLERROR
-       GL_Color(1, 1, 1, 1);
-       GL_ColorMask(r_view.colormask[0], r_view.colormask[1], r_view.colormask[2], 1);
        GL_Scissor(r_view.x, r_view.y, r_view.width, r_view.height);
-       qglDepthFunc(GL_LEQUAL);CHECKGLERROR
-       if (gl_support_stenciltwoside)
-       {
-               qglDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);CHECKGLERROR
-       }
-       qglDisable(GL_STENCIL_TEST);CHECKGLERROR
-       qglStencilMask(~0);CHECKGLERROR
-       qglStencilFunc(GL_ALWAYS, 128, ~0);CHECKGLERROR
-       qglStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);CHECKGLERROR
-       GL_CullFace(GL_FRONT); // quake is backwards, this culls back faces
        r_shadow_rendermode = R_SHADOW_RENDERMODE_NONE;
 }
 
@@ -2269,7 +2237,7 @@ void R_Shadow_DrawEntityShadow(entity_render_t *ent, int numsurfaces, int *surfa
                else if (numsurfaces)
                {
                        R_Mesh_Matrix(&ent->matrix);
-                       model->DrawShadowVolume(ent, r_shadow_rtlight->shadoworigin, NULL, r_shadow_rtlight->radius, numsurfaces, surfacelist, r_shadow_rtlight->cullmins, r_shadow_rtlight->cullmaxs);
+                       model->DrawShadowVolume(ent, r_shadow_rtlight->shadoworigin, NULL, r_shadow_rtlight->radius, numsurfaces, surfacelist, r_shadow_rtlight_cullmins, r_shadow_rtlight_cullmaxs);
                }
        }
        else
@@ -2356,6 +2324,9 @@ void R_DrawRTLight(rtlight_t *rtlight, qboolean visible)
        if (R_CullBox(rtlight->cullmins, rtlight->cullmaxs))
                return;
 
+       VectorCopy(rtlight->cullmins, r_shadow_rtlight_cullmins);
+       VectorCopy(rtlight->cullmaxs, r_shadow_rtlight_cullmaxs);
+
        if (rtlight->compiled && r_shadow_realtime_world_compile.integer)
        {
                // compiled light, world available and can receive realtime lighting
@@ -2371,12 +2342,12 @@ void R_DrawRTLight(rtlight_t *rtlight, qboolean visible)
                // dynamic light, world available and can receive realtime lighting
                // calculate lit surfaces and leafs
                R_Shadow_EnlargeLeafSurfaceBuffer(r_refdef.worldmodel->brush.num_leafs, r_refdef.worldmodel->num_surfaces);
-               r_refdef.worldmodel->GetLightInfo(r_refdef.worldentity, rtlight->shadoworigin, rtlight->radius, rtlight->cullmins, rtlight->cullmaxs, r_shadow_buffer_leaflist, r_shadow_buffer_leafpvs, &numleafs, r_shadow_buffer_surfacelist, r_shadow_buffer_surfacepvs, &numsurfaces);
+               r_refdef.worldmodel->GetLightInfo(r_refdef.worldentity, rtlight->shadoworigin, rtlight->radius, r_shadow_rtlight_cullmins, r_shadow_rtlight_cullmaxs, r_shadow_buffer_leaflist, r_shadow_buffer_leafpvs, &numleafs, r_shadow_buffer_surfacelist, r_shadow_buffer_surfacepvs, &numsurfaces);
                leaflist = r_shadow_buffer_leaflist;
                leafpvs = r_shadow_buffer_leafpvs;
                surfacelist = r_shadow_buffer_surfacelist;
                // if the reduced leaf bounds are offscreen, skip it
-               if (R_CullBox(rtlight->cullmins, rtlight->cullmaxs))
+               if (R_CullBox(r_shadow_rtlight_cullmins, r_shadow_rtlight_cullmaxs))
                        return;
        }
        else
@@ -2398,7 +2369,7 @@ void R_DrawRTLight(rtlight_t *rtlight, qboolean visible)
                        return;
        }
        // set up a scissor rectangle for this light
-       if (R_Shadow_ScissorForBBox(rtlight->cullmins, rtlight->cullmaxs))
+       if (R_Shadow_ScissorForBBox(r_shadow_rtlight_cullmins, r_shadow_rtlight_cullmaxs))
                return;
 
        // make a list of lit entities and shadow casting entities
@@ -2417,7 +2388,7 @@ void R_DrawRTLight(rtlight_t *rtlight, qboolean visible)
                {
                        model_t *model;
                        entity_render_t *ent = r_refdef.entities[i];
-                       if (BoxesOverlap(ent->mins, ent->maxs, rtlight->cullmins, rtlight->cullmaxs)
+                       if (BoxesOverlap(ent->mins, ent->maxs, r_shadow_rtlight_cullmins, r_shadow_rtlight_cullmaxs)
                         && (model = ent->model)
                         && !(ent->flags & RENDER_TRANSPARENT)
                         && (r_refdef.worldmodel == NULL || r_refdef.worldmodel->brush.BoxTouchingLeafPVS == NULL || r_refdef.worldmodel->brush.BoxTouchingLeafPVS(r_refdef.worldmodel, leafpvs, ent->mins, ent->maxs)))
@@ -2535,7 +2506,9 @@ void R_DrawModelShadows(void)
 
        r_shadow_rendermode = R_SHADOW_RENDERMODE_NONE;
 
-       if (gl_ext_stenciltwoside.integer)
+       if (gl_ext_separatestencil.integer)
+               r_shadow_shadowingrendermode = R_SHADOW_RENDERMODE_SEPARATESTENCIL;
+       else if (gl_ext_stenciltwoside.integer)
                r_shadow_shadowingrendermode = R_SHADOW_RENDERMODE_STENCILTWOSIDE;
        else
                r_shadow_shadowingrendermode = R_SHADOW_RENDERMODE_STENCIL;
@@ -2593,11 +2566,10 @@ void R_DrawModelShadows(void)
        // apply the blend to the shadowed areas
        R_Mesh_Draw(0, 4, 2, polygonelements);
 
-       // restore perspective view
-       R_SetupView(&r_view.matrix);
+       // restoring the perspective view is done by R_RenderScene
+       //R_SetupView(&r_view.matrix);
 
        // restore other state to normal
-       GL_DepthTest(true);
        R_Shadow_RenderMode_End();
 }