]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - r_shadow.c
added R_FrameData_Alloc and Store functions (a per-frame heap allocator
[xonotic/darkplaces.git] / r_shadow.c
index 4413c062fef52ed3ac04e2f7907143dbd2686886..7674fb506bc666bcd498218f258168e7db593741 100644 (file)
@@ -154,7 +154,9 @@ typedef enum r_shadow_rendermode_e
        R_SHADOW_RENDERMODE_ZFAIL_SEPARATESTENCIL,
        R_SHADOW_RENDERMODE_ZFAIL_STENCILTWOSIDE,
        R_SHADOW_RENDERMODE_LIGHT_VERTEX,
-       R_SHADOW_RENDERMODE_LIGHT_DOT3,
+       R_SHADOW_RENDERMODE_LIGHT_VERTEX2DATTEN,
+       R_SHADOW_RENDERMODE_LIGHT_VERTEX2D1DATTEN,
+       R_SHADOW_RENDERMODE_LIGHT_VERTEX3DATTEN,
        R_SHADOW_RENDERMODE_LIGHT_GLSL,
        R_SHADOW_RENDERMODE_VISIBLEVOLUMES,
        R_SHADOW_RENDERMODE_VISIBLELIGHTING,
@@ -260,7 +262,6 @@ rtexturepool_t *r_shadow_filters_texturepool;
 cvar_t r_shadow_bumpscale_basetexture = {0, "r_shadow_bumpscale_basetexture", "0", "generate fake bumpmaps from diffuse textures at this bumpyness, try 4 to match tenebrae, higher values increase depth, requires r_restart to take effect"};
 cvar_t r_shadow_bumpscale_bumpmap = {0, "r_shadow_bumpscale_bumpmap", "4", "what magnitude to interpret _bump.tga textures as, higher values increase depth, requires r_restart to take effect"};
 cvar_t r_shadow_debuglight = {0, "r_shadow_debuglight", "-1", "renders only one light, for level design purposes or debugging"};
-cvar_t r_shadow_dot3 = {CVAR_SAVE, "r_shadow_dot3", "0", "enables use of (slow) per pixel lighting on GL1.3 hardware"};
 cvar_t r_shadow_usenormalmap = {CVAR_SAVE, "r_shadow_usenormalmap", "1", "enables use of directional shading on lights"};
 cvar_t r_shadow_gloss = {CVAR_SAVE, "r_shadow_gloss", "1", "0 disables gloss (specularity) rendering, 1 uses gloss if textures are found, 2 forces a flat metallic specular effect on everything without textures (similar to tenebrae)"};
 cvar_t r_shadow_gloss2intensity = {0, "r_shadow_gloss2intensity", "0.125", "how bright the forced flat gloss should look if r_shadow_gloss is 2"};
@@ -305,7 +306,7 @@ cvar_t r_shadow_shadowmapping_polygonoffset = {CVAR_SAVE, "r_shadow_shadowmappin
 cvar_t r_shadow_culltriangles = {0, "r_shadow_culltriangles", "1", "performs more expensive tests to remove unnecessary triangles of lit surfaces"};
 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 r_glsl lighting)"};
+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)"};
 cvar_t r_coronas = {CVAR_SAVE, "r_coronas", "1", "brightness of corona flare effects around certain lights, 0 disables corona effects"};
 cvar_t r_coronas_occlusionsizescale = {CVAR_SAVE, "r_coronas_occlusionsizescale", "0.1", "size of light source for corona occlusion checksm the proportion of hidden pixels controls corona intensity"};
 cvar_t r_coronas_occlusionquery = {CVAR_SAVE, "r_coronas_occlusionquery", "1", "use GL_ARB_occlusion_query extension if supported (fades coronas according to visibility)"};
@@ -365,11 +366,10 @@ skinframe_t *r_editlights_sprnoshadowlight;
 skinframe_t *r_editlights_sprcubemaplight;
 skinframe_t *r_editlights_sprcubemapnoshadowlight;
 skinframe_t *r_editlights_sprselection;
-extern cvar_t gl_max_size;
 
 void R_Shadow_SetShadowMode(void)
 {
-       r_shadow_shadowmapmaxsize = bound(1, r_shadow_shadowmapping_maxsize.integer, gl_max_size.integer / 4);
+       r_shadow_shadowmapmaxsize = bound(1, r_shadow_shadowmapping_maxsize.integer, (int)vid.maxtexturesize_2d / 4);
        r_shadow_shadowmapvsdct = r_shadow_shadowmapping_vsdct.integer != 0;
        r_shadow_shadowmapfilterquality = r_shadow_shadowmapping_filterquality.integer;
        r_shadow_shadowmaptexturetype = r_shadow_shadowmapping_texturetype.integer;
@@ -380,61 +380,70 @@ void R_Shadow_SetShadowMode(void)
        r_shadow_shadowmapsampler = false;
        r_shadow_shadowmappcf = 0;
        r_shadow_shadowmode = R_SHADOW_SHADOWMODE_STENCIL;
-       if(r_shadow_shadowmapping.integer && r_glsl.integer && gl_support_fragment_shader && gl_support_ext_framebuffer_object)
+       switch(vid.renderpath)
        {
-               if(r_shadow_shadowmapfilterquality < 0)
+       case RENDERPATH_GL20:
+               if(r_shadow_shadowmapping.integer && vid.support.ext_framebuffer_object)
                {
-                       if(strstr(gl_vendor, "NVIDIA")) 
+                       if(r_shadow_shadowmapfilterquality < 0)
                        {
-                               r_shadow_shadowmapsampler = gl_support_arb_shadow;
-                               r_shadow_shadowmappcf = 1;
+                               if(strstr(gl_vendor, "NVIDIA")) 
+                               {
+                                       r_shadow_shadowmapsampler = vid.support.arb_shadow;
+                                       r_shadow_shadowmappcf = 1;
+                               }
+                               else if(vid.support.amd_texture_texture4 || vid.support.arb_texture_gather) 
+                                       r_shadow_shadowmappcf = 1;
+                               else if(strstr(gl_vendor, "ATI")) 
+                                       r_shadow_shadowmappcf = 1;
+                               else 
+                                       r_shadow_shadowmapsampler = vid.support.arb_shadow;
                        }
-                       else if(gl_support_amd_texture_texture4 || gl_support_arb_texture_gather) 
-                               r_shadow_shadowmappcf = 1;
-                       else if(strstr(gl_vendor, "ATI")) 
-                               r_shadow_shadowmappcf = 1;
                        else 
-                               r_shadow_shadowmapsampler = gl_support_arb_shadow;
-               }
-               else 
-               {
-                       switch (r_shadow_shadowmapfilterquality)
                        {
+                               switch (r_shadow_shadowmapfilterquality)
+                               {
+                               case 1:
+                                       r_shadow_shadowmapsampler = vid.support.arb_shadow;
+                                       break;
+                               case 2:
+                                       r_shadow_shadowmapsampler = vid.support.arb_shadow;
+                                       r_shadow_shadowmappcf = 1;
+                                       break;
+                               case 3:
+                                       r_shadow_shadowmappcf = 1;
+                                       break;
+                               case 4:
+                                       r_shadow_shadowmappcf = 2;
+                                       break;
+                               }
+                       }
+                       switch (r_shadow_shadowmaptexturetype)
+                       {
+                       case 0:
+                               r_shadow_shadowmode = R_SHADOW_SHADOWMODE_SHADOWMAP2D;
+                               break;
                        case 1:
-                               r_shadow_shadowmapsampler = gl_support_arb_shadow;
+                               r_shadow_shadowmode = R_SHADOW_SHADOWMODE_SHADOWMAPRECTANGLE;
                                break;
                        case 2:
-                               r_shadow_shadowmapsampler = gl_support_arb_shadow;
-                               r_shadow_shadowmappcf = 1;
-                               break;
-                       case 3:
-                               r_shadow_shadowmappcf = 1;
+                               r_shadow_shadowmode = R_SHADOW_SHADOWMODE_SHADOWMAPCUBESIDE;
                                break;
-                       case 4:
-                               r_shadow_shadowmappcf = 2;
+                       default:
+                               if((vid.support.amd_texture_texture4 || vid.support.arb_texture_gather) && r_shadow_shadowmappcf && !r_shadow_shadowmapsampler)
+                                       r_shadow_shadowmode = R_SHADOW_SHADOWMODE_SHADOWMAP2D;
+                               else if(vid.support.arb_texture_rectangle) 
+                                       r_shadow_shadowmode = R_SHADOW_SHADOWMODE_SHADOWMAPRECTANGLE;
+                               else
+                                       r_shadow_shadowmode = R_SHADOW_SHADOWMODE_SHADOWMAP2D;
                                break;
                        }
                }
-        switch (r_shadow_shadowmaptexturetype)
-        {
-        case 0:
-            r_shadow_shadowmode = R_SHADOW_SHADOWMODE_SHADOWMAP2D;
-            break;
-        case 1:
-            r_shadow_shadowmode = R_SHADOW_SHADOWMODE_SHADOWMAPRECTANGLE;
-            break;
-        case 2:
-            r_shadow_shadowmode = R_SHADOW_SHADOWMODE_SHADOWMAPCUBESIDE;
-            break;
-        default:
-                       if((gl_support_amd_texture_texture4 || gl_support_arb_texture_gather) && r_shadow_shadowmappcf && !r_shadow_shadowmapsampler)
-                               r_shadow_shadowmode = R_SHADOW_SHADOWMODE_SHADOWMAP2D;
-                       else if(gl_texturerectangle) 
-                               r_shadow_shadowmode = R_SHADOW_SHADOWMODE_SHADOWMAPRECTANGLE;
-                       else
-                               r_shadow_shadowmode = R_SHADOW_SHADOWMODE_SHADOWMAP2D;
-            break;
-               }
+               break;
+       case RENDERPATH_GL13:
+               break;
+       case RENDERPATH_GL11:
+               break;
        }
 }
 
@@ -444,20 +453,22 @@ void R_Shadow_FreeShadowMaps(void)
 
        R_Shadow_SetShadowMode();
 
+       if (!vid.support.ext_framebuffer_object || !vid.support.arb_fragment_shader)
+               return;
+
+       CHECKGLERROR
+
        if (r_shadow_fborectangle)
-               qglDeleteFramebuffersEXT(1, &r_shadow_fborectangle);
+               qglDeleteFramebuffersEXT(1, &r_shadow_fborectangle);CHECKGLERROR
        r_shadow_fborectangle = 0;
-       CHECKGLERROR
 
        if (r_shadow_fbo2d)
-               qglDeleteFramebuffersEXT(1, &r_shadow_fbo2d);
+               qglDeleteFramebuffersEXT(1, &r_shadow_fbo2d);CHECKGLERROR
        r_shadow_fbo2d = 0;
-       CHECKGLERROR
        for (i = 0;i < R_SHADOW_SHADOWMAP_NUMCUBEMAPS;i++)
                if (r_shadow_fbocubeside[i])
-                       qglDeleteFramebuffersEXT(1, &r_shadow_fbocubeside[i]);
+                       qglDeleteFramebuffersEXT(1, &r_shadow_fbocubeside[i]);CHECKGLERROR
        memset(r_shadow_fbocubeside, 0, sizeof(r_shadow_fbocubeside));
-       CHECKGLERROR
 
        if (r_shadow_shadowmaprectangletexture)
                R_FreeTexture(r_shadow_shadowmaprectangletexture);
@@ -628,46 +639,10 @@ void r_shadow_newmap(void)
                R_Shadow_EditLights_Reload_f();
 }
 
-void R_Shadow_Help_f(void)
-{
-       Con_Printf(
-"Documentation on r_shadow system:\n"
-"Settings:\n"
-"r_shadow_bumpscale_basetexture : base texture as bumpmap with this scale\n"
-"r_shadow_bumpscale_bumpmap : depth scale for bumpmap conversion\n"
-"r_shadow_debuglight : render only this light number (-1 = all)\n"
-"r_shadow_gloss 0/1/2 : no gloss, gloss textures only, force gloss\n"
-"r_shadow_gloss2intensity : brightness of forced gloss\n"
-"r_shadow_glossintensity : brightness of textured gloss\n"
-"r_shadow_lightattenuationlinearscale : used to generate attenuation texture\n"
-"r_shadow_lightattenuationdividebias : used to generate attenuation texture\n"
-"r_shadow_lightintensityscale : scale rendering brightness of all lights\n"
-"r_shadow_lightradiusscale : scale rendering radius of all lights\n"
-"r_shadow_portallight : use portal visibility for static light precomputation\n"
-"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_world : use high quality world lighting mode\n"
-"r_shadow_realtime_world_lightmaps : use lightmaps in addition to lights\n"
-"r_shadow_realtime_world_shadows : cast shadows from world lights\n"
-"r_shadow_realtime_world_compile : compile surface/visibility information\n"
-"r_shadow_realtime_world_compileshadow : compile shadow geometry\n"
-"r_shadow_scissor : use scissor optimization\n"
-"r_shadow_polygonfactor : nudge shadow volumes closer/further\n"
-"r_shadow_polygonoffset : nudge shadow volumes closer/further\n"
-"r_shadow_texture3d : use 3d attenuation texture (if hardware supports)\n"
-"r_showlighting : useful for performance testing; bright = slow!\n"
-"r_showshadowvolumes : useful for performance testing; bright = slow!\n"
-"Commands:\n"
-"r_shadow_help : this help\n"
-       );
-}
-
 void R_Shadow_Init(void)
 {
        Cvar_RegisterVariable(&r_shadow_bumpscale_basetexture);
        Cvar_RegisterVariable(&r_shadow_bumpscale_bumpmap);
-       Cvar_RegisterVariable(&r_shadow_dot3);
        Cvar_RegisterVariable(&r_shadow_usenormalmap);
        Cvar_RegisterVariable(&r_shadow_debuglight);
        Cvar_RegisterVariable(&r_shadow_gloss);
@@ -725,7 +700,6 @@ void R_Shadow_Init(void)
                Cvar_SetValue("r_shadow_gloss", 2);
                Cvar_SetValue("r_shadow_bumpscale_basetexture", 4);
        }
-       Cmd_AddCommand("r_shadow_help", R_Shadow_Help_f, "prints documentation on console commands and variables used by realtime lighting and shadowing system");
        R_Shadow_EditLights_Init();
        Mem_ExpandableArray_NewArray(&r_shadow_worldlightsarray, r_main_mempool, sizeof(dlight_t), 128);
        maxshadowtriangles = 0;
@@ -1315,6 +1289,12 @@ void R_Shadow_VolumeFromList(int numverts, int numtris, const float *invertex3f,
                tris = R_Shadow_ConstructShadowVolume_ZFail(numverts, numtris, elements, neighbors, invertex3f, &outverts, shadowelements, shadowvertex3f, projectorigin, projectdirection, projectdistance, nummarktris, marktris);
                Mod_ShadowMesh_AddMesh(r_main_mempool, r_shadow_compilingrtlight->static_meshchain_shadow_zfail, NULL, NULL, NULL, shadowvertex3f, NULL, NULL, NULL, NULL, tris, shadowelements);
        }
+       else if (r_shadow_rendermode == R_SHADOW_RENDERMODE_VISIBLEVOLUMES)
+       {
+               tris = R_Shadow_ConstructShadowVolume_ZFail(numverts, numtris, elements, neighbors, invertex3f, &outverts, shadowelements, shadowvertex3f, projectorigin, projectdirection, projectdistance, nummarktris, marktris);
+               R_Mesh_VertexPointer(shadowvertex3f, 0, 0);
+               R_Mesh_Draw(0, outverts, 0, tris, shadowelements, NULL, 0, 0);
+       }
        else
        {
                // decide which type of shadow to generate and set stencil mode
@@ -1722,7 +1702,7 @@ static void R_Shadow_MakeTextures(void)
                        data[y*ATTEN2DSIZE+x] = R_Shadow_MakeTextures_SamplePoint(((x + 0.5f) * (2.0f / ATTEN2DSIZE) - 1.0f) * (1.0f / 0.9375), ((y + 0.5f) * (2.0f / ATTEN2DSIZE) - 1.0f) * (1.0f / 0.9375), 0);
        r_shadow_attenuation2dtexture = R_LoadTexture2D(r_shadow_texturepool, "attenuation2d", ATTEN2DSIZE, ATTEN2DSIZE, (unsigned char *)data, TEXTYPE_BGRA, TEXF_PRECACHE | TEXF_CLAMP | TEXF_ALPHA | TEXF_FORCELINEAR, NULL);
        // 3D sphere texture
-       if (r_shadow_texture3d.integer && gl_texture3d)
+       if (r_shadow_texture3d.integer && vid.support.ext_texture_3d)
        {
                for (z = 0;z < ATTEN3DSIZE;z++)
                        for (y = 0;y < ATTEN3DSIZE;y++)
@@ -1849,11 +1829,11 @@ static void R_Shadow_MakeTextures(void)
 
 void R_Shadow_ValidateCvars(void)
 {
-       if (r_shadow_texture3d.integer && !gl_texture3d)
+       if (r_shadow_texture3d.integer && !vid.support.ext_texture_3d)
                Cvar_SetValueQuick(&r_shadow_texture3d, 0);
-       if (gl_ext_separatestencil.integer && !gl_support_separatestencil)
+       if (gl_ext_separatestencil.integer && !vid.support.ati_separate_stencil)
                Cvar_SetValueQuick(&gl_ext_separatestencil, 0);
-       if (gl_ext_stenciltwoside.integer && !gl_support_stenciltwoside)
+       if (gl_ext_stenciltwoside.integer && !vid.support.ext_stencil_two_side)
                Cvar_SetValueQuick(&gl_ext_stenciltwoside, 0);
 }
 
@@ -1884,12 +1864,12 @@ void R_Shadow_RenderMode_Begin(void)
 
        r_shadow_rendermode = R_SHADOW_RENDERMODE_NONE;
 
-       if (gl_ext_separatestencil.integer)
+       if (gl_ext_separatestencil.integer && vid.support.ati_separate_stencil)
        {
                r_shadow_shadowingrendermode_zpass = R_SHADOW_RENDERMODE_ZPASS_SEPARATESTENCIL;
                r_shadow_shadowingrendermode_zfail = R_SHADOW_RENDERMODE_ZFAIL_SEPARATESTENCIL;
        }
-       else if (gl_ext_stenciltwoside.integer)
+       else if (gl_ext_stenciltwoside.integer && vid.support.ext_stencil_two_side)
        {
                r_shadow_shadowingrendermode_zpass = R_SHADOW_RENDERMODE_ZPASS_STENCILTWOSIDE;
                r_shadow_shadowingrendermode_zfail = R_SHADOW_RENDERMODE_ZFAIL_STENCILTWOSIDE;
@@ -1900,12 +1880,23 @@ void R_Shadow_RenderMode_Begin(void)
                r_shadow_shadowingrendermode_zfail = R_SHADOW_RENDERMODE_ZFAIL_STENCIL;
        }
 
-       if (r_glsl.integer && gl_support_fragment_shader)
+       switch(vid.renderpath)
+       {
+       case RENDERPATH_GL20:
                r_shadow_lightingrendermode = R_SHADOW_RENDERMODE_LIGHT_GLSL;
-       else if (gl_dot3arb && gl_texturecubemap && r_shadow_dot3.integer && gl_stencil)
-               r_shadow_lightingrendermode = R_SHADOW_RENDERMODE_LIGHT_DOT3;
-       else
-               r_shadow_lightingrendermode = R_SHADOW_RENDERMODE_LIGHT_VERTEX;
+               break;
+       case RENDERPATH_GL13:
+       case RENDERPATH_GL11:
+               if (r_textureunits.integer >= 2 && vid.texunits >= 2 && r_shadow_texture3d.integer && r_shadow_attenuation3dtexture)
+                       r_shadow_lightingrendermode = R_SHADOW_RENDERMODE_LIGHT_VERTEX3DATTEN;
+               else if (r_textureunits.integer >= 3 && vid.texunits >= 3)
+                       r_shadow_lightingrendermode = R_SHADOW_RENDERMODE_LIGHT_VERTEX2D1DATTEN;
+               else if (r_textureunits.integer >= 2 && vid.texunits >= 2)
+                       r_shadow_lightingrendermode = R_SHADOW_RENDERMODE_LIGHT_VERTEX2DATTEN;
+               else
+                       r_shadow_lightingrendermode = R_SHADOW_RENDERMODE_LIGHT_VERTEX;
+               break;
+       }
 
        CHECKGLERROR
 #if 0
@@ -1930,7 +1921,7 @@ void R_Shadow_RenderMode_Reset(void)
        {
                qglDisable(GL_STENCIL_TEST_TWO_SIDE_EXT);CHECKGLERROR
        }
-       if (gl_support_ext_framebuffer_object)
+       if (vid.support.ext_framebuffer_object)
        {
                qglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);CHECKGLERROR
        }
@@ -2070,7 +2061,7 @@ void R_Shadow_RenderMode_ShadowMap(int side, qboolean clear, int size)
                if (!r_shadow_shadowmap2dtexture)
                {
 #if 1
-                       int w = maxsize*2, h = gl_support_arb_texture_non_power_of_two ? maxsize*3 : maxsize*4;
+                       int w = maxsize*2, h = vid.support.arb_texture_non_power_of_two ? maxsize*3 : maxsize*4;
                        r_shadow_shadowmap2dtexture = R_LoadTextureShadowMap2D(r_shadow_texturepool, "shadowmap", w, h, r_shadow_shadowmapdepthbits, r_shadow_shadowmapsampler);
                        qglGenFramebuffersEXT(1, &r_shadow_fbo2d);CHECKGLERROR
                        qglBindFramebufferEXT(GL_FRAMEBUFFER_EXT, r_shadow_fbo2d);CHECKGLERROR
@@ -2223,7 +2214,7 @@ void R_Shadow_RenderMode_Lighting(qboolean stenciltest, qboolean transparent, qb
        // do global setup needed for the chosen lighting mode
        if (r_shadow_rendermode == R_SHADOW_RENDERMODE_LIGHT_GLSL)
        {
-               R_Mesh_TexBindCubeMap(GL20TU_CUBE, R_GetTexture(rsurface.rtlight->currentcubemap)); // light filter
+               R_Mesh_TexBindAll(GL20TU_CUBE, 0, 0, R_GetTexture(rsurface.rtlight->currentcubemap), 0); // light filter
                GL_ColorMask(r_refdef.view.colormask[0], r_refdef.view.colormask[1], r_refdef.view.colormask[2], 0);
                CHECKGLERROR
                if (shadowmapping)
@@ -2237,25 +2228,24 @@ void R_Shadow_RenderMode_Lighting(qboolean stenciltest, qboolean transparent, qb
                        else if (r_shadow_shadowmode == R_SHADOW_SHADOWMODE_SHADOWMAPRECTANGLE)
                        {
                                r_shadow_usingshadowmaprect = true;
-                               R_Mesh_TexBindRectangle(GL20TU_SHADOWMAPRECT, R_GetTexture(r_shadow_shadowmaprectangletexture));
+                               R_Mesh_TexBindAll(GL20TU_SHADOWMAPRECT, 0, 0, 0, R_GetTexture(r_shadow_shadowmaprectangletexture));
                                CHECKGLERROR
                        }
                        else if (r_shadow_shadowmode == R_SHADOW_SHADOWMODE_SHADOWMAPCUBESIDE)
                        {
                                r_shadow_usingshadowmapcube = true;
-                               R_Mesh_TexBindCubeMap(GL20TU_SHADOWMAPCUBE, R_GetTexture(r_shadow_shadowmapcubetexture[r_shadow_shadowmaplod]));
+                               R_Mesh_TexBindAll(GL20TU_SHADOWMAPCUBE, 0, 0, R_GetTexture(r_shadow_shadowmapcubetexture[r_shadow_shadowmaplod]), 0);
                                CHECKGLERROR
                        }
 
                        if (r_shadow_shadowmapvsdct && (r_shadow_usingshadowmap2d || r_shadow_usingshadowmaprect))
                        {
-                               R_Mesh_TexBindCubeMap(GL20TU_CUBEPROJECTION, R_GetTexture(r_shadow_shadowmapvsdcttexture));
+                               R_Mesh_TexBindAll(GL20TU_CUBEPROJECTION, 0, 0, R_GetTexture(r_shadow_shadowmapvsdcttexture), 0);
                                CHECKGLERROR
                        }
                }
        }
-       else if (r_shadow_rendermode == R_SHADOW_RENDERMODE_LIGHT_VERTEX)
-               R_Mesh_ColorPointer(rsurface.array_color4f, 0, 0);
+       R_Mesh_ColorPointer(rsurface.array_color4f, 0, 0);
        //GL_BlendFunc(GL_SRC_ALPHA, GL_ONE);
        CHECKGLERROR
 }
@@ -2443,8 +2433,10 @@ static void R_Shadow_RenderLighting_Light_Vertex_Shading(int firstvertex, int nu
        const float *normal3f = rsurface.normal3f + 3 * firstvertex;
        float *color4f = rsurface.array_color4f + 4 * firstvertex;
        float dist, dot, distintensity, shadeintensity, v[3], n[3];
-       if (r_textureunits.integer >= 3)
+       switch (r_shadow_rendermode)
        {
+       case R_SHADOW_RENDERMODE_LIGHT_VERTEX3DATTEN:
+       case R_SHADOW_RENDERMODE_LIGHT_VERTEX2D1DATTEN:
                if (VectorLength2(diffusecolor) > 0)
                {
                        for (;numverts > 0;numverts--, vertex3f += 3, normal3f += 3, color4f += 4)
@@ -2482,9 +2474,8 @@ static void R_Shadow_RenderLighting_Light_Vertex_Shading(int firstvertex, int nu
                                color4f[3] = 1;
                        }
                }
-       }
-       else if (r_textureunits.integer >= 2)
-       {
+               break;
+       case R_SHADOW_RENDERMODE_LIGHT_VERTEX2DATTEN:
                if (VectorLength2(diffusecolor) > 0)
                {
                        for (;numverts > 0;numverts--, vertex3f += 3, normal3f += 3, color4f += 4)
@@ -2540,9 +2531,8 @@ static void R_Shadow_RenderLighting_Light_Vertex_Shading(int firstvertex, int nu
                                color4f[3] = 1;
                        }
                }
-       }
-       else
-       {
+               break;
+       case R_SHADOW_RENDERMODE_LIGHT_VERTEX:
                if (VectorLength2(diffusecolor) > 0)
                {
                        for (;numverts > 0;numverts--, vertex3f += 3, normal3f += 3, color4f += 4)
@@ -2600,50 +2590,9 @@ static void R_Shadow_RenderLighting_Light_Vertex_Shading(int firstvertex, int nu
                                color4f[3] = 1;
                        }
                }
-       }
-}
-
-// TODO: use glTexGen instead of feeding vertices to texcoordpointer?
-
-static void R_Shadow_GenTexCoords_Diffuse_NormalCubeMap(int firstvertex, int numvertices, int numtriangles, const int *element3i)
-{
-       int i;
-       float       *out3f     = rsurface.array_texcoord3f + 3 * firstvertex;
-       const float *vertex3f  = rsurface.vertex3f         + 3 * firstvertex;
-       const float *svector3f = rsurface.svector3f        + 3 * firstvertex;
-       const float *tvector3f = rsurface.tvector3f        + 3 * firstvertex;
-       const float *normal3f  = rsurface.normal3f         + 3 * firstvertex;
-       float lightdir[3];
-       for (i = 0;i < numvertices;i++, vertex3f += 3, svector3f += 3, tvector3f += 3, normal3f += 3, out3f += 3)
-       {
-               VectorSubtract(rsurface.entitylightorigin, vertex3f, lightdir);
-               // the cubemap normalizes this for us
-               out3f[0] = DotProduct(svector3f, lightdir);
-               out3f[1] = DotProduct(tvector3f, lightdir);
-               out3f[2] = DotProduct(normal3f, lightdir);
-       }
-}
-
-static void R_Shadow_GenTexCoords_Specular_NormalCubeMap(int firstvertex, int numvertices, int numtriangles, const int *element3i)
-{
-       int i;
-       float       *out3f     = rsurface.array_texcoord3f + 3 * firstvertex;
-       const float *vertex3f  = rsurface.vertex3f         + 3 * firstvertex;
-       const float *svector3f = rsurface.svector3f        + 3 * firstvertex;
-       const float *tvector3f = rsurface.tvector3f        + 3 * firstvertex;
-       const float *normal3f  = rsurface.normal3f         + 3 * firstvertex;
-       float lightdir[3], eyedir[3], halfdir[3];
-       for (i = 0;i < numvertices;i++, vertex3f += 3, svector3f += 3, tvector3f += 3, normal3f += 3, out3f += 3)
-       {
-               VectorSubtract(rsurface.entitylightorigin, vertex3f, lightdir);
-               VectorNormalize(lightdir);
-               VectorSubtract(rsurface.localvieworigin, vertex3f, eyedir);
-               VectorNormalize(eyedir);
-               VectorAdd(lightdir, eyedir, halfdir);
-               // the cubemap normalizes this for us
-               out3f[0] = DotProduct(svector3f, halfdir);
-               out3f[1] = DotProduct(tvector3f, halfdir);
-               out3f[2] = DotProduct(normal3f, halfdir);
+               break;
+       default:
+               break;
        }
 }
 
@@ -2673,7 +2622,7 @@ static void R_Shadow_RenderLighting_Light_GLSL(int firstvertex, int numvertices,
                R_Mesh_TexBind(GL20TU_SECONDARY_GLOSS, R_GetTexture(rsurface.texture->backgroundglosstexture));
                R_Mesh_TexBind(GL20TU_SECONDARY_GLOW, R_GetTexture(rsurface.texture->backgroundcurrentskinframe->glow));
        }
-       //R_Mesh_TexBindCubeMap(GL20TU_CUBE, R_GetTexture(rsurface.rtlight->currentcubemap));
+       //R_Mesh_TexBindAll(GL20TU_CUBE, 0, 0, R_GetTexture(rsurface.rtlight->currentcubemap), 0);
        R_Mesh_TexBind(GL20TU_FOGMASK, R_GetTexture(r_texture_fogattenuation));
        if(rsurface.texture->colormapping)
        {
@@ -2696,601 +2645,6 @@ static void R_Shadow_RenderLighting_Light_GLSL(int firstvertex, int numvertices,
        }
 }
 
-static void R_Shadow_RenderLighting_Light_Dot3_Finalize(int firstvertex, int numvertices, int firsttriangle, int numtriangles, const int *element3i, const unsigned short *element3s, int element3i_bufferobject, int element3s_bufferobject, float r, float g, float b)
-{
-       // shared final code for all the dot3 layers
-       int renders;
-       GL_ColorMask(r_refdef.view.colormask[0], r_refdef.view.colormask[1], r_refdef.view.colormask[2], 0);
-       for (renders = 0;renders < 64 && (r > 0 || g > 0 || b > 0);renders++, r--, g--, b--)
-       {
-               GL_Color(bound(0, r, 1), bound(0, g, 1), bound(0, b, 1), 1);
-               R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
-       }
-}
-
-static void R_Shadow_RenderLighting_Light_Dot3_AmbientPass(int firstvertex, int numvertices, int firsttriangle, int numtriangles, const int *element3i, const unsigned short *element3s, int element3i_bufferobject, int element3s_bufferobject, const vec3_t lightcolorbase, rtexture_t *basetexture, float colorscale)
-{
-       rmeshstate_t m;
-       // colorscale accounts for how much we multiply the brightness
-       // during combine.
-       //
-       // mult is how many times the final pass of the lighting will be
-       // performed to get more brightness than otherwise possible.
-       //
-       // Limit mult to 64 for sanity sake.
-       GL_Color(1,1,1,1);
-       if (r_shadow_texture3d.integer && rsurface.rtlight->currentcubemap != r_texture_whitecube && r_textureunits.integer >= 4)
-       {
-               // 3 3D combine path (Geforce3, Radeon 8500)
-               memset(&m, 0, sizeof(m));
-               m.tex3d[0] = R_GetTexture(r_shadow_attenuation3dtexture);
-               m.pointer_texcoord3f[0] = rsurface.vertex3f;
-               m.pointer_texcoord_bufferobject[0] = rsurface.vertex3f_bufferobject;
-               m.pointer_texcoord_bufferoffset[0] = rsurface.vertex3f_bufferoffset;
-               m.texmatrix[0] = rsurface.entitytoattenuationxyz;
-               m.tex[1] = R_GetTexture(basetexture);
-               m.pointer_texcoord[1] = rsurface.texcoordtexture2f;
-               m.pointer_texcoord_bufferobject[1] = rsurface.texcoordtexture2f_bufferobject;
-               m.pointer_texcoord_bufferoffset[1] = rsurface.texcoordtexture2f_bufferoffset;
-               m.texmatrix[1] = rsurface.texture->currenttexmatrix;
-               m.texcubemap[2] = R_GetTexture(rsurface.rtlight->currentcubemap);
-               m.pointer_texcoord3f[2] = rsurface.vertex3f;
-               m.pointer_texcoord_bufferobject[2] = rsurface.vertex3f_bufferobject;
-               m.pointer_texcoord_bufferoffset[2] = rsurface.vertex3f_bufferoffset;
-               m.texmatrix[2] = rsurface.entitytolight;
-               GL_BlendFunc(GL_ONE, GL_ONE);
-       }
-       else if (r_shadow_texture3d.integer && rsurface.rtlight->currentcubemap == r_texture_whitecube && r_textureunits.integer >= 2)
-       {
-               // 2 3D combine path (Geforce3, original Radeon)
-               memset(&m, 0, sizeof(m));
-               m.tex3d[0] = R_GetTexture(r_shadow_attenuation3dtexture);
-               m.pointer_texcoord3f[0] = rsurface.vertex3f;
-               m.pointer_texcoord_bufferobject[0] = rsurface.vertex3f_bufferobject;
-               m.pointer_texcoord_bufferoffset[0] = rsurface.vertex3f_bufferoffset;
-               m.texmatrix[0] = rsurface.entitytoattenuationxyz;
-               m.tex[1] = R_GetTexture(basetexture);
-               m.pointer_texcoord[1] = rsurface.texcoordtexture2f;
-               m.pointer_texcoord_bufferobject[1] = rsurface.texcoordtexture2f_bufferobject;
-               m.pointer_texcoord_bufferoffset[1] = rsurface.texcoordtexture2f_bufferoffset;
-               m.texmatrix[1] = rsurface.texture->currenttexmatrix;
-               GL_BlendFunc(GL_ONE, GL_ONE);
-       }
-       else if (r_textureunits.integer >= 4 && rsurface.rtlight->currentcubemap != r_texture_whitecube)
-       {
-               // 4 2D combine path (Geforce3, Radeon 8500)
-               memset(&m, 0, sizeof(m));
-               m.tex[0] = R_GetTexture(r_shadow_attenuation2dtexture);
-               m.pointer_texcoord3f[0] = rsurface.vertex3f;
-               m.pointer_texcoord_bufferobject[0] = rsurface.vertex3f_bufferobject;
-               m.pointer_texcoord_bufferoffset[0] = rsurface.vertex3f_bufferoffset;
-               m.texmatrix[0] = rsurface.entitytoattenuationxyz;
-               m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
-               m.pointer_texcoord3f[1] = rsurface.vertex3f;
-               m.pointer_texcoord_bufferobject[1] = rsurface.vertex3f_bufferobject;
-               m.pointer_texcoord_bufferoffset[1] = rsurface.vertex3f_bufferoffset;
-               m.texmatrix[1] = rsurface.entitytoattenuationz;
-               m.tex[2] = R_GetTexture(basetexture);
-               m.pointer_texcoord[2] = rsurface.texcoordtexture2f;
-               m.pointer_texcoord_bufferobject[2] = rsurface.texcoordtexture2f_bufferobject;
-               m.pointer_texcoord_bufferoffset[2] = rsurface.texcoordtexture2f_bufferoffset;
-               m.texmatrix[2] = rsurface.texture->currenttexmatrix;
-               if (rsurface.rtlight->currentcubemap != r_texture_whitecube)
-               {
-                       m.texcubemap[3] = R_GetTexture(rsurface.rtlight->currentcubemap);
-                       m.pointer_texcoord3f[3] = rsurface.vertex3f;
-                       m.pointer_texcoord_bufferobject[3] = rsurface.vertex3f_bufferobject;
-                       m.pointer_texcoord_bufferoffset[3] = rsurface.vertex3f_bufferoffset;
-                       m.texmatrix[3] = rsurface.entitytolight;
-               }
-               GL_BlendFunc(GL_ONE, GL_ONE);
-       }
-       else if (r_textureunits.integer >= 3 && rsurface.rtlight->currentcubemap == r_texture_whitecube)
-       {
-               // 3 2D combine path (Geforce3, original Radeon)
-               memset(&m, 0, sizeof(m));
-               m.tex[0] = R_GetTexture(r_shadow_attenuation2dtexture);
-               m.pointer_texcoord3f[0] = rsurface.vertex3f;
-               m.pointer_texcoord_bufferobject[0] = rsurface.vertex3f_bufferobject;
-               m.pointer_texcoord_bufferoffset[0] = rsurface.vertex3f_bufferoffset;
-               m.texmatrix[0] = rsurface.entitytoattenuationxyz;
-               m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
-               m.pointer_texcoord3f[1] = rsurface.vertex3f;
-               m.pointer_texcoord_bufferobject[1] = rsurface.vertex3f_bufferobject;
-               m.pointer_texcoord_bufferoffset[1] = rsurface.vertex3f_bufferoffset;
-               m.texmatrix[1] = rsurface.entitytoattenuationz;
-               m.tex[2] = R_GetTexture(basetexture);
-               m.pointer_texcoord[2] = rsurface.texcoordtexture2f;
-               m.pointer_texcoord_bufferobject[2] = rsurface.texcoordtexture2f_bufferobject;
-               m.pointer_texcoord_bufferoffset[2] = rsurface.texcoordtexture2f_bufferoffset;
-               m.texmatrix[2] = rsurface.texture->currenttexmatrix;
-               GL_BlendFunc(GL_ONE, GL_ONE);
-       }
-       else
-       {
-               // 2/2/2 2D combine path (any dot3 card)
-               memset(&m, 0, sizeof(m));
-               m.tex[0] = R_GetTexture(r_shadow_attenuation2dtexture);
-               m.pointer_texcoord3f[0] = rsurface.vertex3f;
-               m.pointer_texcoord_bufferobject[0] = rsurface.vertex3f_bufferobject;
-               m.pointer_texcoord_bufferoffset[0] = rsurface.vertex3f_bufferoffset;
-               m.texmatrix[0] = rsurface.entitytoattenuationxyz;
-               m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
-               m.pointer_texcoord3f[1] = rsurface.vertex3f;
-               m.pointer_texcoord_bufferobject[1] = rsurface.vertex3f_bufferobject;
-               m.pointer_texcoord_bufferoffset[1] = rsurface.vertex3f_bufferoffset;
-               m.texmatrix[1] = rsurface.entitytoattenuationz;
-               R_Mesh_TextureState(&m);
-               GL_ColorMask(0,0,0,1);
-               GL_BlendFunc(GL_ONE, GL_ZERO);
-               R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
-
-               // second pass
-               memset(&m, 0, sizeof(m));
-               m.tex[0] = R_GetTexture(basetexture);
-               m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
-               m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
-               m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
-               m.texmatrix[0] = rsurface.texture->currenttexmatrix;
-               if (rsurface.rtlight->currentcubemap != r_texture_whitecube)
-               {
-                       m.texcubemap[1] = R_GetTexture(rsurface.rtlight->currentcubemap);
-                       m.pointer_texcoord3f[1] = rsurface.vertex3f;
-                       m.pointer_texcoord_bufferobject[1] = rsurface.vertex3f_bufferobject;
-                       m.pointer_texcoord_bufferoffset[1] = rsurface.vertex3f_bufferoffset;
-                       m.texmatrix[1] = rsurface.entitytolight;
-               }
-               GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
-       }
-       // this final code is shared
-       R_Mesh_TextureState(&m);
-       R_Shadow_RenderLighting_Light_Dot3_Finalize(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject, lightcolorbase[0] * colorscale, lightcolorbase[1] * colorscale, lightcolorbase[2] * colorscale);
-}
-
-static void R_Shadow_RenderLighting_Light_Dot3_DiffusePass(int firstvertex, int numvertices, int firsttriangle, int numtriangles, const int *element3i, const unsigned short *element3s, int element3i_bufferobject, int element3s_bufferobject, const vec3_t lightcolorbase, rtexture_t *basetexture, rtexture_t *normalmaptexture, float colorscale)
-{
-       rmeshstate_t m;
-       // colorscale accounts for how much we multiply the brightness
-       // during combine.
-       //
-       // mult is how many times the final pass of the lighting will be
-       // performed to get more brightness than otherwise possible.
-       //
-       // Limit mult to 64 for sanity sake.
-       GL_Color(1,1,1,1);
-       // generate normalization cubemap texcoords
-       R_Shadow_GenTexCoords_Diffuse_NormalCubeMap(firstvertex, numvertices, numtriangles, element3i);
-       if (r_shadow_texture3d.integer && r_textureunits.integer >= 4)
-       {
-               // 3/2 3D combine path (Geforce3, Radeon 8500)
-               memset(&m, 0, sizeof(m));
-               m.tex[0] = R_GetTexture(normalmaptexture);
-               m.texcombinergb[0] = GL_REPLACE;
-               m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
-               m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
-               m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
-               m.texmatrix[0] = rsurface.texture->currenttexmatrix;
-               m.texcubemap[1] = R_GetTexture(r_texture_normalizationcube);
-               m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
-               m.pointer_texcoord3f[1] = rsurface.array_texcoord3f;
-               m.pointer_texcoord_bufferobject[1] = 0;
-               m.pointer_texcoord_bufferoffset[1] = 0;
-               m.tex3d[2] = R_GetTexture(r_shadow_attenuation3dtexture);
-               m.pointer_texcoord3f[2] = rsurface.vertex3f;
-               m.pointer_texcoord_bufferobject[2] = rsurface.vertex3f_bufferobject;
-               m.pointer_texcoord_bufferoffset[2] = rsurface.vertex3f_bufferoffset;
-               m.texmatrix[2] = rsurface.entitytoattenuationxyz;
-               R_Mesh_TextureState(&m);
-               GL_ColorMask(0,0,0,1);
-               GL_BlendFunc(GL_ONE, GL_ZERO);
-               R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
-
-               // second pass
-               memset(&m, 0, sizeof(m));
-               m.tex[0] = R_GetTexture(basetexture);
-               m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
-               m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
-               m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
-               m.texmatrix[0] = rsurface.texture->currenttexmatrix;
-               if (rsurface.rtlight->currentcubemap != r_texture_whitecube)
-               {
-                       m.texcubemap[1] = R_GetTexture(rsurface.rtlight->currentcubemap);
-                       m.pointer_texcoord3f[1] = rsurface.vertex3f;
-                       m.pointer_texcoord_bufferobject[1] = rsurface.vertex3f_bufferobject;
-                       m.pointer_texcoord_bufferoffset[1] = rsurface.vertex3f_bufferoffset;
-                       m.texmatrix[1] = rsurface.entitytolight;
-               }
-               GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
-       }
-       else if (r_shadow_texture3d.integer && r_textureunits.integer >= 2 && rsurface.rtlight->currentcubemap != r_texture_whitecube)
-       {
-               // 1/2/2 3D combine path (original Radeon)
-               memset(&m, 0, sizeof(m));
-               m.tex3d[0] = R_GetTexture(r_shadow_attenuation3dtexture);
-               m.pointer_texcoord3f[0] = rsurface.vertex3f;
-               m.pointer_texcoord_bufferobject[0] = rsurface.vertex3f_bufferobject;
-               m.pointer_texcoord_bufferoffset[0] = rsurface.vertex3f_bufferoffset;
-               m.texmatrix[0] = rsurface.entitytoattenuationxyz;
-               R_Mesh_TextureState(&m);
-               GL_ColorMask(0,0,0,1);
-               GL_BlendFunc(GL_ONE, GL_ZERO);
-               R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
-
-               // second pass
-               memset(&m, 0, sizeof(m));
-               m.tex[0] = R_GetTexture(normalmaptexture);
-               m.texcombinergb[0] = GL_REPLACE;
-               m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
-               m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
-               m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
-               m.texmatrix[0] = rsurface.texture->currenttexmatrix;
-               m.texcubemap[1] = R_GetTexture(r_texture_normalizationcube);
-               m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
-               m.pointer_texcoord3f[1] = rsurface.array_texcoord3f;
-               m.pointer_texcoord_bufferobject[1] = 0;
-               m.pointer_texcoord_bufferoffset[1] = 0;
-               R_Mesh_TextureState(&m);
-               GL_BlendFunc(GL_DST_ALPHA, GL_ZERO);
-               R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
-
-               // second pass
-               memset(&m, 0, sizeof(m));
-               m.tex[0] = R_GetTexture(basetexture);
-               m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
-               m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
-               m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
-               m.texmatrix[0] = rsurface.texture->currenttexmatrix;
-               if (rsurface.rtlight->currentcubemap != r_texture_whitecube)
-               {
-                       m.texcubemap[1] = R_GetTexture(rsurface.rtlight->currentcubemap);
-                       m.pointer_texcoord3f[1] = rsurface.vertex3f;
-                       m.pointer_texcoord_bufferobject[1] = rsurface.vertex3f_bufferobject;
-                       m.pointer_texcoord_bufferoffset[1] = rsurface.vertex3f_bufferoffset;
-                       m.texmatrix[1] = rsurface.entitytolight;
-               }
-               GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
-       }
-       else if (r_shadow_texture3d.integer && r_textureunits.integer >= 2 && rsurface.rtlight->currentcubemap == r_texture_whitecube)
-       {
-               // 2/2 3D combine path (original Radeon)
-               memset(&m, 0, sizeof(m));
-               m.tex[0] = R_GetTexture(normalmaptexture);
-               m.texcombinergb[0] = GL_REPLACE;
-               m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
-               m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
-               m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
-               m.texmatrix[0] = rsurface.texture->currenttexmatrix;
-               m.texcubemap[1] = R_GetTexture(r_texture_normalizationcube);
-               m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
-               m.pointer_texcoord3f[1] = rsurface.array_texcoord3f;
-               m.pointer_texcoord_bufferobject[1] = 0;
-               m.pointer_texcoord_bufferoffset[1] = 0;
-               R_Mesh_TextureState(&m);
-               GL_ColorMask(0,0,0,1);
-               GL_BlendFunc(GL_ONE, GL_ZERO);
-               R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
-
-               // second pass
-               memset(&m, 0, sizeof(m));
-               m.tex[0] = R_GetTexture(basetexture);
-               m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
-               m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
-               m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
-               m.texmatrix[0] = rsurface.texture->currenttexmatrix;
-               m.tex3d[1] = R_GetTexture(r_shadow_attenuation3dtexture);
-               m.pointer_texcoord3f[1] = rsurface.vertex3f;
-               m.pointer_texcoord_bufferobject[1] = rsurface.vertex3f_bufferobject;
-               m.pointer_texcoord_bufferoffset[1] = rsurface.vertex3f_bufferoffset;
-               m.texmatrix[1] = rsurface.entitytoattenuationxyz;
-               GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
-       }
-       else if (r_textureunits.integer >= 4)
-       {
-               // 4/2 2D combine path (Geforce3, Radeon 8500)
-               memset(&m, 0, sizeof(m));
-               m.tex[0] = R_GetTexture(normalmaptexture);
-               m.texcombinergb[0] = GL_REPLACE;
-               m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
-               m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
-               m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
-               m.texmatrix[0] = rsurface.texture->currenttexmatrix;
-               m.texcubemap[1] = R_GetTexture(r_texture_normalizationcube);
-               m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
-               m.pointer_texcoord3f[1] = rsurface.array_texcoord3f;
-               m.pointer_texcoord_bufferobject[1] = 0;
-               m.pointer_texcoord_bufferoffset[1] = 0;
-               m.tex[2] = R_GetTexture(r_shadow_attenuation2dtexture);
-               m.pointer_texcoord3f[2] = rsurface.vertex3f;
-               m.pointer_texcoord_bufferobject[2] = rsurface.vertex3f_bufferobject;
-               m.pointer_texcoord_bufferoffset[2] = rsurface.vertex3f_bufferoffset;
-               m.texmatrix[2] = rsurface.entitytoattenuationxyz;
-               m.tex[3] = R_GetTexture(r_shadow_attenuation2dtexture);
-               m.pointer_texcoord3f[3] = rsurface.vertex3f;
-               m.pointer_texcoord_bufferobject[3] = rsurface.vertex3f_bufferobject;
-               m.pointer_texcoord_bufferoffset[3] = rsurface.vertex3f_bufferoffset;
-               m.texmatrix[3] = rsurface.entitytoattenuationz;
-               R_Mesh_TextureState(&m);
-               GL_ColorMask(0,0,0,1);
-               GL_BlendFunc(GL_ONE, GL_ZERO);
-               R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
-
-               // second pass
-               memset(&m, 0, sizeof(m));
-               m.tex[0] = R_GetTexture(basetexture);
-               m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
-               m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
-               m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
-               m.texmatrix[0] = rsurface.texture->currenttexmatrix;
-               if (rsurface.rtlight->currentcubemap != r_texture_whitecube)
-               {
-                       m.texcubemap[1] = R_GetTexture(rsurface.rtlight->currentcubemap);
-                       m.pointer_texcoord3f[1] = rsurface.vertex3f;
-                       m.pointer_texcoord_bufferobject[1] = rsurface.vertex3f_bufferobject;
-                       m.pointer_texcoord_bufferoffset[1] = rsurface.vertex3f_bufferoffset;
-                       m.texmatrix[1] = rsurface.entitytolight;
-               }
-               GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
-       }
-       else
-       {
-               // 2/2/2 2D combine path (any dot3 card)
-               memset(&m, 0, sizeof(m));
-               m.tex[0] = R_GetTexture(r_shadow_attenuation2dtexture);
-               m.pointer_texcoord3f[0] = rsurface.vertex3f;
-               m.pointer_texcoord_bufferobject[0] = rsurface.vertex3f_bufferobject;
-               m.pointer_texcoord_bufferoffset[0] = rsurface.vertex3f_bufferoffset;
-               m.texmatrix[0] = rsurface.entitytoattenuationxyz;
-               m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
-               m.pointer_texcoord3f[1] = rsurface.vertex3f;
-               m.pointer_texcoord_bufferobject[0] = rsurface.vertex3f_bufferobject;
-               m.pointer_texcoord_bufferoffset[0] = rsurface.vertex3f_bufferoffset;
-               m.texmatrix[1] = rsurface.entitytoattenuationz;
-               R_Mesh_TextureState(&m);
-               GL_ColorMask(0,0,0,1);
-               GL_BlendFunc(GL_ONE, GL_ZERO);
-               R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
-
-               // second pass
-               memset(&m, 0, sizeof(m));
-               m.tex[0] = R_GetTexture(normalmaptexture);
-               m.texcombinergb[0] = GL_REPLACE;
-               m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
-               m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
-               m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
-               m.texmatrix[0] = rsurface.texture->currenttexmatrix;
-               m.texcubemap[1] = R_GetTexture(r_texture_normalizationcube);
-               m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
-               m.pointer_texcoord3f[1] = rsurface.array_texcoord3f;
-               m.pointer_texcoord_bufferobject[1] = 0;
-               m.pointer_texcoord_bufferoffset[1] = 0;
-               R_Mesh_TextureState(&m);
-               GL_BlendFunc(GL_DST_ALPHA, GL_ZERO);
-               R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
-
-               // second pass
-               memset(&m, 0, sizeof(m));
-               m.tex[0] = R_GetTexture(basetexture);
-               m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
-               m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
-               m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
-               m.texmatrix[0] = rsurface.texture->currenttexmatrix;
-               if (rsurface.rtlight->currentcubemap != r_texture_whitecube)
-               {
-                       m.texcubemap[1] = R_GetTexture(rsurface.rtlight->currentcubemap);
-                       m.pointer_texcoord3f[1] = rsurface.vertex3f;
-                       m.pointer_texcoord_bufferobject[1] = rsurface.vertex3f_bufferobject;
-                       m.pointer_texcoord_bufferoffset[1] = rsurface.vertex3f_bufferoffset;
-                       m.texmatrix[1] = rsurface.entitytolight;
-               }
-               GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
-       }
-       // this final code is shared
-       R_Mesh_TextureState(&m);
-       R_Shadow_RenderLighting_Light_Dot3_Finalize(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject, lightcolorbase[0] * colorscale, lightcolorbase[1] * colorscale, lightcolorbase[2] * colorscale);
-}
-
-static void R_Shadow_RenderLighting_Light_Dot3_SpecularPass(int firstvertex, int numvertices, int firsttriangle, int numtriangles, const int *element3i, const unsigned short *element3s, int element3i_bufferobject, int element3s_bufferobject, const vec3_t lightcolorbase, rtexture_t *glosstexture, rtexture_t *normalmaptexture, float colorscale)
-{
-       float glossexponent;
-       rmeshstate_t m;
-       // FIXME: detect blendsquare!
-       //if (!gl_support_blendsquare)
-       //      return;
-       GL_Color(1,1,1,1);
-       // generate normalization cubemap texcoords
-       R_Shadow_GenTexCoords_Specular_NormalCubeMap(firstvertex, numvertices, numtriangles, element3i);
-       if (r_shadow_texture3d.integer && r_textureunits.integer >= 2 && rsurface.rtlight->currentcubemap != r_texture_whitecube)
-       {
-               // 2/0/0/1/2 3D combine blendsquare path
-               memset(&m, 0, sizeof(m));
-               m.tex[0] = R_GetTexture(normalmaptexture);
-               m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
-               m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
-               m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
-               m.texmatrix[0] = rsurface.texture->currenttexmatrix;
-               m.texcubemap[1] = R_GetTexture(r_texture_normalizationcube);
-               m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
-               m.pointer_texcoord3f[1] = rsurface.array_texcoord3f;
-               m.pointer_texcoord_bufferobject[1] = 0;
-               m.pointer_texcoord_bufferoffset[1] = 0;
-               R_Mesh_TextureState(&m);
-               GL_ColorMask(0,0,0,1);
-               // this squares the result
-               GL_BlendFunc(GL_SRC_ALPHA, GL_ZERO);
-               R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
-
-               // second and third pass
-               R_Mesh_ResetTextureState();
-               // square alpha in framebuffer a few times to make it shiny
-               GL_BlendFunc(GL_ZERO, GL_DST_ALPHA);
-               for (glossexponent = 2;glossexponent * 2 <= r_shadow_glossexponent.value;glossexponent *= 2)
-                       R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
-
-               // fourth pass
-               memset(&m, 0, sizeof(m));
-               m.tex3d[0] = R_GetTexture(r_shadow_attenuation3dtexture);
-               m.pointer_texcoord3f[0] = rsurface.vertex3f;
-               m.pointer_texcoord_bufferobject[0] = rsurface.vertex3f_bufferobject;
-               m.pointer_texcoord_bufferoffset[0] = rsurface.vertex3f_bufferoffset;
-               m.texmatrix[0] = rsurface.entitytoattenuationxyz;
-               R_Mesh_TextureState(&m);
-               GL_BlendFunc(GL_DST_ALPHA, GL_ZERO);
-               R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
-
-               // fifth pass
-               memset(&m, 0, sizeof(m));
-               m.tex[0] = R_GetTexture(glosstexture);
-               m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
-               m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
-               m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
-               m.texmatrix[0] = rsurface.texture->currenttexmatrix;
-               if (rsurface.rtlight->currentcubemap != r_texture_whitecube)
-               {
-                       m.texcubemap[1] = R_GetTexture(rsurface.rtlight->currentcubemap);
-                       m.pointer_texcoord3f[1] = rsurface.vertex3f;
-                       m.pointer_texcoord_bufferobject[1] = rsurface.vertex3f_bufferobject;
-                       m.pointer_texcoord_bufferoffset[1] = rsurface.vertex3f_bufferoffset;
-                       m.texmatrix[1] = rsurface.entitytolight;
-               }
-               GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
-       }
-       else if (r_shadow_texture3d.integer && r_textureunits.integer >= 2 && rsurface.rtlight->currentcubemap == r_texture_whitecube /* && gl_support_blendsquare*/) // FIXME: detect blendsquare!
-       {
-               // 2/0/0/2 3D combine blendsquare path
-               memset(&m, 0, sizeof(m));
-               m.tex[0] = R_GetTexture(normalmaptexture);
-               m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
-               m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
-               m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
-               m.texmatrix[0] = rsurface.texture->currenttexmatrix;
-               m.texcubemap[1] = R_GetTexture(r_texture_normalizationcube);
-               m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
-               m.pointer_texcoord3f[1] = rsurface.array_texcoord3f;
-               m.pointer_texcoord_bufferobject[1] = 0;
-               m.pointer_texcoord_bufferoffset[1] = 0;
-               R_Mesh_TextureState(&m);
-               GL_ColorMask(0,0,0,1);
-               // this squares the result
-               GL_BlendFunc(GL_SRC_ALPHA, GL_ZERO);
-               R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
-
-               // second and third pass
-               R_Mesh_ResetTextureState();
-               // square alpha in framebuffer a few times to make it shiny
-               GL_BlendFunc(GL_ZERO, GL_DST_ALPHA);
-               for (glossexponent = 2;glossexponent * 2 <= r_shadow_glossexponent.value;glossexponent *= 2)
-                       R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
-
-               // fourth pass
-               memset(&m, 0, sizeof(m));
-               m.tex[0] = R_GetTexture(glosstexture);
-               m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
-               m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
-               m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
-               m.texmatrix[0] = rsurface.texture->currenttexmatrix;
-               m.tex3d[1] = R_GetTexture(r_shadow_attenuation3dtexture);
-               m.pointer_texcoord3f[1] = rsurface.vertex3f;
-               m.pointer_texcoord_bufferobject[1] = rsurface.vertex3f_bufferobject;
-               m.pointer_texcoord_bufferoffset[1] = rsurface.vertex3f_bufferoffset;
-               m.texmatrix[1] = rsurface.entitytoattenuationxyz;
-               GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
-       }
-       else
-       {
-               // 2/0/0/2/2 2D combine blendsquare path
-               memset(&m, 0, sizeof(m));
-               m.tex[0] = R_GetTexture(normalmaptexture);
-               m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
-               m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
-               m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
-               m.texmatrix[0] = rsurface.texture->currenttexmatrix;
-               m.texcubemap[1] = R_GetTexture(r_texture_normalizationcube);
-               m.texcombinergb[1] = GL_DOT3_RGBA_ARB;
-               m.pointer_texcoord3f[1] = rsurface.array_texcoord3f;
-               m.pointer_texcoord_bufferobject[1] = 0;
-               m.pointer_texcoord_bufferoffset[1] = 0;
-               R_Mesh_TextureState(&m);
-               GL_ColorMask(0,0,0,1);
-               // this squares the result
-               GL_BlendFunc(GL_SRC_ALPHA, GL_ZERO);
-               R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
-
-               // second and third pass
-               R_Mesh_ResetTextureState();
-               // square alpha in framebuffer a few times to make it shiny
-               GL_BlendFunc(GL_ZERO, GL_DST_ALPHA);
-               for (glossexponent = 2;glossexponent * 2 <= r_shadow_glossexponent.value;glossexponent *= 2)
-                       R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
-
-               // fourth pass
-               memset(&m, 0, sizeof(m));
-               m.tex[0] = R_GetTexture(r_shadow_attenuation2dtexture);
-               m.pointer_texcoord3f[0] = rsurface.vertex3f;
-               m.pointer_texcoord_bufferobject[0] = rsurface.vertex3f_bufferobject;
-               m.pointer_texcoord_bufferoffset[0] = rsurface.vertex3f_bufferoffset;
-               m.texmatrix[0] = rsurface.entitytoattenuationxyz;
-               m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
-               m.pointer_texcoord3f[1] = rsurface.vertex3f;
-               m.pointer_texcoord_bufferobject[1] = rsurface.vertex3f_bufferobject;
-               m.pointer_texcoord_bufferoffset[1] = rsurface.vertex3f_bufferoffset;
-               m.texmatrix[1] = rsurface.entitytoattenuationz;
-               R_Mesh_TextureState(&m);
-               GL_BlendFunc(GL_DST_ALPHA, GL_ZERO);
-               R_Mesh_Draw(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject);
-
-               // fifth pass
-               memset(&m, 0, sizeof(m));
-               m.tex[0] = R_GetTexture(glosstexture);
-               m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
-               m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
-               m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
-               m.texmatrix[0] = rsurface.texture->currenttexmatrix;
-               if (rsurface.rtlight->currentcubemap != r_texture_whitecube)
-               {
-                       m.texcubemap[1] = R_GetTexture(rsurface.rtlight->currentcubemap);
-                       m.pointer_texcoord3f[1] = rsurface.vertex3f;
-                       m.pointer_texcoord_bufferobject[1] = rsurface.vertex3f_bufferobject;
-                       m.pointer_texcoord_bufferoffset[1] = rsurface.vertex3f_bufferoffset;
-                       m.texmatrix[1] = rsurface.entitytolight;
-               }
-               GL_BlendFunc(GL_DST_ALPHA, GL_ONE);
-       }
-       // this final code is shared
-       R_Mesh_TextureState(&m);
-       R_Shadow_RenderLighting_Light_Dot3_Finalize(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject, lightcolorbase[0] * colorscale, lightcolorbase[1] * colorscale, lightcolorbase[2] * colorscale);
-}
-
-static void R_Shadow_RenderLighting_Light_Dot3(int firstvertex, int numvertices, int firsttriangle, int numtriangles, const int *element3i, const unsigned short *element3s, int element3i_bufferobject, int element3s_bufferobject, const vec3_t lightcolorbase, const vec3_t lightcolorpants, const vec3_t lightcolorshirt, rtexture_t *basetexture, rtexture_t *pantstexture, rtexture_t *shirttexture, rtexture_t *normalmaptexture, rtexture_t *glosstexture, float ambientscale, float diffusescale, float specularscale, qboolean dopants, qboolean doshirt)
-{
-       // ARB path (any Geforce, any Radeon)
-       qboolean doambient = ambientscale > 0;
-       qboolean dodiffuse = diffusescale > 0;
-       qboolean dospecular = specularscale > 0;
-       if (!doambient && !dodiffuse && !dospecular)
-               return;
-       R_Mesh_ColorPointer(NULL, 0, 0);
-       if (doambient)
-               R_Shadow_RenderLighting_Light_Dot3_AmbientPass(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject, lightcolorbase, basetexture, ambientscale * r_refdef.view.colorscale);
-       if (dodiffuse)
-               R_Shadow_RenderLighting_Light_Dot3_DiffusePass(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject, lightcolorbase, basetexture, normalmaptexture, diffusescale * r_refdef.view.colorscale);
-       if (dopants)
-       {
-               if (doambient)
-                       R_Shadow_RenderLighting_Light_Dot3_AmbientPass(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject, lightcolorpants, pantstexture, ambientscale * r_refdef.view.colorscale);
-               if (dodiffuse)
-                       R_Shadow_RenderLighting_Light_Dot3_DiffusePass(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject, lightcolorpants, pantstexture, normalmaptexture, diffusescale * r_refdef.view.colorscale);
-       }
-       if (doshirt)
-       {
-               if (doambient)
-                       R_Shadow_RenderLighting_Light_Dot3_AmbientPass(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject, lightcolorshirt, shirttexture, ambientscale * r_refdef.view.colorscale);
-               if (dodiffuse)
-                       R_Shadow_RenderLighting_Light_Dot3_DiffusePass(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject, lightcolorshirt, shirttexture, normalmaptexture, diffusescale * r_refdef.view.colorscale);
-       }
-       if (dospecular)
-               R_Shadow_RenderLighting_Light_Dot3_SpecularPass(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject, lightcolorbase, glosstexture, normalmaptexture, specularscale * r_refdef.view.colorscale);
-}
-
 static void R_Shadow_RenderLighting_Light_Vertex_Pass(int firstvertex, int numvertices, int numtriangles, const int *element3i, vec3_t diffusecolor2, vec3_t ambientcolor2)
 {
        int renders;
@@ -3393,31 +2747,67 @@ static void R_Shadow_RenderLighting_Light_Vertex(int firstvertex, int numvertice
        VectorScale(lightcolorpants, diffusescale * 2 * r_refdef.view.colorscale, diffusecolorpants);
        VectorScale(lightcolorshirt, ambientscale * 2 * r_refdef.view.colorscale, ambientcolorshirt);
        VectorScale(lightcolorshirt, diffusescale * 2 * r_refdef.view.colorscale, diffusecolorshirt);
-       memset(&m, 0, sizeof(m));
-       m.tex[0] = R_GetTexture(basetexture);
-       m.texmatrix[0] = rsurface.texture->currenttexmatrix;
-       m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
-       m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
-       m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
-       if (r_textureunits.integer >= 2)
-       {
-               // voodoo2 or TNT
+       switch(r_shadow_rendermode)
+       {
+       case R_SHADOW_RENDERMODE_LIGHT_VERTEX3DATTEN:
+               memset(&m, 0, sizeof(m));
+               m.tex[0] = R_GetTexture(basetexture);
+               m.texmatrix[0] = rsurface.texture->currenttexmatrix;
+               m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
+               m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
+               m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
+               m.tex3d[1] = R_GetTexture(r_shadow_attenuation3dtexture);
+               m.texmatrix[1] = rsurface.entitytoattenuationxyz;
+               m.pointer_texcoord3f[1] = rsurface.vertex3f;
+               m.pointer_texcoord_bufferobject[1] = rsurface.vertex3f_bufferobject;
+               m.pointer_texcoord_bufferoffset[1] = rsurface.vertex3f_bufferoffset;
+               R_Mesh_TextureState(&m);
+               break;
+       case R_SHADOW_RENDERMODE_LIGHT_VERTEX2D1DATTEN:
+               memset(&m, 0, sizeof(m));
+               m.tex[0] = R_GetTexture(basetexture);
+               m.texmatrix[0] = rsurface.texture->currenttexmatrix;
+               m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
+               m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
+               m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
                m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
                m.texmatrix[1] = rsurface.entitytoattenuationxyz;
                m.pointer_texcoord3f[1] = rsurface.vertex3f;
                m.pointer_texcoord_bufferobject[1] = rsurface.vertex3f_bufferobject;
                m.pointer_texcoord_bufferoffset[1] = rsurface.vertex3f_bufferoffset;
-               if (r_textureunits.integer >= 3)
-               {
-                       // Voodoo4 or Kyro (or Geforce3/Radeon with r_shadow_dot3 off)
-                       m.tex[2] = R_GetTexture(r_shadow_attenuation2dtexture);
-                       m.texmatrix[2] = rsurface.entitytoattenuationz;
-                       m.pointer_texcoord3f[2] = rsurface.vertex3f;
-                       m.pointer_texcoord_bufferobject[2] = rsurface.vertex3f_bufferobject;
-                       m.pointer_texcoord_bufferoffset[2] = rsurface.vertex3f_bufferoffset;
-               }
+               m.tex[2] = R_GetTexture(r_shadow_attenuation2dtexture);
+               m.texmatrix[2] = rsurface.entitytoattenuationz;
+               m.pointer_texcoord3f[2] = rsurface.vertex3f;
+               m.pointer_texcoord_bufferobject[2] = rsurface.vertex3f_bufferobject;
+               m.pointer_texcoord_bufferoffset[2] = rsurface.vertex3f_bufferoffset;
+               R_Mesh_TextureState(&m);
+               break;
+       case R_SHADOW_RENDERMODE_LIGHT_VERTEX2DATTEN:
+               memset(&m, 0, sizeof(m));
+               m.tex[0] = R_GetTexture(basetexture);
+               m.texmatrix[0] = rsurface.texture->currenttexmatrix;
+               m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
+               m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
+               m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
+               m.tex[1] = R_GetTexture(r_shadow_attenuation2dtexture);
+               m.texmatrix[1] = rsurface.entitytoattenuationxyz;
+               m.pointer_texcoord3f[1] = rsurface.vertex3f;
+               m.pointer_texcoord_bufferobject[1] = rsurface.vertex3f_bufferobject;
+               m.pointer_texcoord_bufferoffset[1] = rsurface.vertex3f_bufferoffset;
+               R_Mesh_TextureState(&m);
+               break;
+       case R_SHADOW_RENDERMODE_LIGHT_VERTEX:
+               memset(&m, 0, sizeof(m));
+               m.tex[0] = R_GetTexture(basetexture);
+               m.texmatrix[0] = rsurface.texture->currenttexmatrix;
+               m.pointer_texcoord[0] = rsurface.texcoordtexture2f;
+               m.pointer_texcoord_bufferobject[0] = rsurface.texcoordtexture2f_bufferobject;
+               m.pointer_texcoord_bufferoffset[0] = rsurface.texcoordtexture2f_bufferoffset;
+               R_Mesh_TextureState(&m);
+               break;
+       default:
+               break;
        }
-       R_Mesh_TextureState(&m);
        //R_Mesh_TexBind(0, R_GetTexture(basetexture));
        R_Shadow_RenderLighting_Light_Vertex_Pass(firstvertex, numvertices, numtriangles, element3i, diffusecolorbase, ambientcolorbase);
        if (dopants)
@@ -3454,7 +2844,7 @@ void R_Shadow_RenderLighting(int firstvertex, int numvertices, int firsttriangle
        }
        if ((ambientscale + diffusescale) * VectorLength2(lightcolorbase) + specularscale * VectorLength2(lightcolorbase) < (1.0f / 1048576.0f))
                return;
-       negated = (lightcolorbase[0] + lightcolorbase[1] + lightcolorbase[2] < 0) && gl_support_ext_blend_subtract;
+       negated = (lightcolorbase[0] + lightcolorbase[1] + lightcolorbase[2] < 0) && vid.support.ext_blend_subtract;
        if(negated)
        {
                VectorNegate(lightcolorbase, lightcolorbase);
@@ -3493,9 +2883,9 @@ void R_Shadow_RenderLighting(int firstvertex, int numvertices, int firsttriangle
                case R_SHADOW_RENDERMODE_LIGHT_GLSL:
                        R_Shadow_RenderLighting_Light_GLSL(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject, lightcolorbase, lightcolorpants, lightcolorshirt, rsurface.texture->basetexture, rsurface.texture->currentskinframe->pants, rsurface.texture->currentskinframe->shirt, nmap, rsurface.texture->glosstexture, ambientscale, diffusescale, specularscale, dopants, doshirt);
                        break;
-               case R_SHADOW_RENDERMODE_LIGHT_DOT3:
-                       R_Shadow_RenderLighting_Light_Dot3(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject, lightcolorbase, lightcolorpants, lightcolorshirt, rsurface.texture->basetexture, rsurface.texture->currentskinframe->pants, rsurface.texture->currentskinframe->shirt, nmap, rsurface.texture->glosstexture, ambientscale, diffusescale, specularscale, dopants, doshirt);
-                       break;
+               case R_SHADOW_RENDERMODE_LIGHT_VERTEX3DATTEN:
+               case R_SHADOW_RENDERMODE_LIGHT_VERTEX2D1DATTEN:
+               case R_SHADOW_RENDERMODE_LIGHT_VERTEX2DATTEN:
                case R_SHADOW_RENDERMODE_LIGHT_VERTEX:
                        R_Shadow_RenderLighting_Light_Vertex(firstvertex, numvertices, numtriangles, element3i + firsttriangle * 3, lightcolorbase, lightcolorpants, lightcolorshirt, rsurface.texture->basetexture, rsurface.texture->currentskinframe->pants, rsurface.texture->currentskinframe->shirt, nmap, rsurface.texture->glosstexture, ambientscale, diffusescale, specularscale, dopants, doshirt);
                        break;
@@ -3515,9 +2905,9 @@ void R_Shadow_RenderLighting(int firstvertex, int numvertices, int firsttriangle
                case R_SHADOW_RENDERMODE_LIGHT_GLSL:
                        R_Shadow_RenderLighting_Light_GLSL(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject, lightcolorbase, vec3_origin, vec3_origin, rsurface.texture->basetexture, r_texture_black, r_texture_black, nmap, rsurface.texture->glosstexture, ambientscale, diffusescale, specularscale, false, false);
                        break;
-               case R_SHADOW_RENDERMODE_LIGHT_DOT3:
-                       R_Shadow_RenderLighting_Light_Dot3(firstvertex, numvertices, firsttriangle, numtriangles, element3i, element3s, element3i_bufferobject, element3s_bufferobject, lightcolorbase, vec3_origin, vec3_origin, rsurface.texture->basetexture, r_texture_black, r_texture_black, nmap, rsurface.texture->glosstexture, ambientscale, diffusescale, specularscale, false, false);
-                       break;
+               case R_SHADOW_RENDERMODE_LIGHT_VERTEX3DATTEN:
+               case R_SHADOW_RENDERMODE_LIGHT_VERTEX2D1DATTEN:
+               case R_SHADOW_RENDERMODE_LIGHT_VERTEX2DATTEN:
                case R_SHADOW_RENDERMODE_LIGHT_VERTEX:
                        R_Shadow_RenderLighting_Light_Vertex(firstvertex, numvertices, numtriangles, element3i + firsttriangle * 3, lightcolorbase, vec3_origin, vec3_origin, rsurface.texture->basetexture, r_texture_black, r_texture_black, nmap, rsurface.texture->glosstexture, ambientscale, diffusescale, specularscale, false, false);
                        break;
@@ -3605,8 +2995,7 @@ void R_RTLight_Compile(rtlight_t *rtlight)
        {
                // this variable must be set for the CompileShadowVolume/CompileShadowMap code
                r_shadow_compilingrtlight = rtlight;
-               R_Shadow_EnlargeLeafSurfaceTrisBuffer(model->brush.num_leafs, model->num_surfaces, model->brush.shadowmesh ? model->brush.shadowmesh->numtriangles : model->surfmesh.num_triangles, model->surfmesh.num_triangles);
-               model->GetLightInfo(ent, 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_shadow_buffer_shadowtrispvs, r_shadow_buffer_lighttrispvs, r_shadow_buffer_visitingleafpvs);
+               model->GetLightInfo(ent, 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_shadow_buffer_shadowtrispvs, r_shadow_buffer_lighttrispvs, r_shadow_buffer_visitingleafpvs, 0, NULL);
                numleafpvsbytes = (model->brush.num_leafs + 7) >> 3;
                numshadowtrispvsbytes = ((model->brush.shadowmesh ? model->brush.shadowmesh->numtriangles : model->surfmesh.num_triangles) + 7) >> 3;
                numlighttrispvsbytes = (model->surfmesh.num_triangles + 7) >> 3;
@@ -3728,9 +3117,9 @@ void R_Shadow_ComputeShadowCasterCullingPlanes(rtlight_t *rtlight)
        int i, j;
        mplane_t plane;
        // reset the count of frustum planes
-       // see rsurface.rtlight_frustumplanes definition for how much this array
+       // see rtlight->cached_frustumplanes definition for how much this array
        // can hold
-       rsurface.rtlight_numfrustumplanes = 0;
+       rtlight->cached_numfrustumplanes = 0;
 
        // haven't implemented a culling path for ortho rendering
        if (!r_refdef.view.useperspective)
@@ -3741,7 +3130,7 @@ void R_Shadow_ComputeShadowCasterCullingPlanes(rtlight_t *rtlight)
                                break;
                if (i == 4)
                        for (i = 0;i < 4;i++)
-                               rsurface.rtlight_frustumplanes[rsurface.rtlight_numfrustumplanes++] = r_refdef.view.frustum[i];
+                               rtlight->cached_frustumplanes[rtlight->cached_numfrustumplanes++] = r_refdef.view.frustum[i];
                return;
        }
 
@@ -3763,11 +3152,11 @@ void R_Shadow_ComputeShadowCasterCullingPlanes(rtlight_t *rtlight)
                if (PlaneDiff(rtlight->shadoworigin, &r_refdef.view.frustum[i]) < -0.03125)
                        continue;
                // copy the plane
-               rsurface.rtlight_frustumplanes[rsurface.rtlight_numfrustumplanes++] = r_refdef.view.frustum[i];
+               rtlight->cached_frustumplanes[rtlight->cached_numfrustumplanes++] = r_refdef.view.frustum[i];
        }
        // if all the standard frustum planes were accepted, the light is onscreen
        // otherwise we need to generate some more planes below...
-       if (rsurface.rtlight_numfrustumplanes < 4)
+       if (rtlight->cached_numfrustumplanes < 4)
        {
                // at least one of the stock frustum planes failed, so we need to
                // create one or two custom planes to enclose the light origin
@@ -3798,12 +3187,12 @@ void R_Shadow_ComputeShadowCasterCullingPlanes(rtlight_t *rtlight)
                        // we have created a valid plane, compute extra info
                        PlaneClassify(&plane);
                        // copy the plane
-                       rsurface.rtlight_frustumplanes[rsurface.rtlight_numfrustumplanes++] = plane;
+                       rtlight->cached_frustumplanes[rtlight->cached_numfrustumplanes++] = plane;
 #if 1
                        // if we've found 5 frustum planes then we have constructed a
                        // proper split-side case and do not need to keep searching for
                        // planes to enclose the light origin
-                       if (rsurface.rtlight_numfrustumplanes == 5)
+                       if (rtlight->cached_numfrustumplanes == 5)
                                break;
 #endif
                }
@@ -3811,9 +3200,9 @@ void R_Shadow_ComputeShadowCasterCullingPlanes(rtlight_t *rtlight)
 #endif
 
 #if 0
-       for (i = 0;i < rsurface.rtlight_numfrustumplanes;i++)
+       for (i = 0;i < rtlight->cached_numfrustumplanes;i++)
        {
-               plane = rsurface.rtlight_frustumplanes[i];
+               plane = rtlight->cached_frustumplanes[i];
                Con_Printf("light %p plane #%i %f %f %f : %f (%f %f %f %f %f)\n", rtlight, i, plane.normal[0], plane.normal[1], plane.normal[2], plane.dist, PlaneDiff(r_refdef.view.frustumcorner[0], &plane), PlaneDiff(r_refdef.view.frustumcorner[1], &plane), PlaneDiff(r_refdef.view.frustumcorner[2], &plane), PlaneDiff(r_refdef.view.frustumcorner[3], &plane), PlaneDiff(rtlight->shadoworigin, &plane));
        }
 #endif
@@ -3833,7 +3222,7 @@ void R_Shadow_ComputeShadowCasterCullingPlanes(rtlight_t *rtlight)
                        VectorSubtract(plane.normal, rtlight->shadoworigin, plane.normal);
                        plane.dist = VectorNormalizeLength(plane.normal);
                        plane.dist += DotProduct(plane.normal, rtlight->shadoworigin);
-                       rsurface.rtlight_frustumplanes[rsurface.rtlight_numfrustumplanes++] = plane;
+                       rtlight->cached_frustumplanes[rtlight->cached_numfrustumplanes++] = plane;
                }
        }
 #endif
@@ -3844,8 +3233,8 @@ void R_Shadow_ComputeShadowCasterCullingPlanes(rtlight_t *rtlight)
        {
                VectorClear(plane.normal);
                plane.normal[i >> 1] = (i & 1) ? -1 : 1;
-               plane.dist = (i & 1) ? -rsurface.rtlight_cullmaxs[i >> 1] : rsurface.rtlight_cullmins[i >> 1];
-               rsurface.rtlight_frustumplanes[rsurface.rtlight_numfrustumplanes++] = plane;
+               plane.dist = (i & 1) ? -rtlight->cached_cullmaxs[i >> 1] : rtlight->cached_cullmins[i >> 1];
+               rtlight->cached_frustumplanes[rtlight->cached_numfrustumplanes++] = plane;
        }
 #endif
 
@@ -3856,33 +3245,33 @@ void R_Shadow_ComputeShadowCasterCullingPlanes(rtlight_t *rtlight)
        vec_t bestdist;
        // reduce all plane distances to tightly fit the rtlight cull box, which
        // is in worldspace
-       VectorSet(points[0], rsurface.rtlight_cullmins[0], rsurface.rtlight_cullmins[1], rsurface.rtlight_cullmins[2]);
-       VectorSet(points[1], rsurface.rtlight_cullmaxs[0], rsurface.rtlight_cullmins[1], rsurface.rtlight_cullmins[2]);
-       VectorSet(points[2], rsurface.rtlight_cullmins[0], rsurface.rtlight_cullmaxs[1], rsurface.rtlight_cullmins[2]);
-       VectorSet(points[3], rsurface.rtlight_cullmaxs[0], rsurface.rtlight_cullmaxs[1], rsurface.rtlight_cullmins[2]);
-       VectorSet(points[4], rsurface.rtlight_cullmins[0], rsurface.rtlight_cullmins[1], rsurface.rtlight_cullmaxs[2]);
-       VectorSet(points[5], rsurface.rtlight_cullmaxs[0], rsurface.rtlight_cullmins[1], rsurface.rtlight_cullmaxs[2]);
-       VectorSet(points[6], rsurface.rtlight_cullmins[0], rsurface.rtlight_cullmaxs[1], rsurface.rtlight_cullmaxs[2]);
-       VectorSet(points[7], rsurface.rtlight_cullmaxs[0], rsurface.rtlight_cullmaxs[1], rsurface.rtlight_cullmaxs[2]);
-       oldnum = rsurface.rtlight_numfrustumplanes;
-       rsurface.rtlight_numfrustumplanes = 0;
+       VectorSet(points[0], rtlight->cached_cullmins[0], rtlight->cached_cullmins[1], rtlight->cached_cullmins[2]);
+       VectorSet(points[1], rtlight->cached_cullmaxs[0], rtlight->cached_cullmins[1], rtlight->cached_cullmins[2]);
+       VectorSet(points[2], rtlight->cached_cullmins[0], rtlight->cached_cullmaxs[1], rtlight->cached_cullmins[2]);
+       VectorSet(points[3], rtlight->cached_cullmaxs[0], rtlight->cached_cullmaxs[1], rtlight->cached_cullmins[2]);
+       VectorSet(points[4], rtlight->cached_cullmins[0], rtlight->cached_cullmins[1], rtlight->cached_cullmaxs[2]);
+       VectorSet(points[5], rtlight->cached_cullmaxs[0], rtlight->cached_cullmins[1], rtlight->cached_cullmaxs[2]);
+       VectorSet(points[6], rtlight->cached_cullmins[0], rtlight->cached_cullmaxs[1], rtlight->cached_cullmaxs[2]);
+       VectorSet(points[7], rtlight->cached_cullmaxs[0], rtlight->cached_cullmaxs[1], rtlight->cached_cullmaxs[2]);
+       oldnum = rtlight->cached_numfrustumplanes;
+       rtlight->cached_numfrustumplanes = 0;
        for (j = 0;j < oldnum;j++)
        {
                // find the nearest point on the box to this plane
-               bestdist = DotProduct(rsurface.rtlight_frustumplanes[j].normal, points[0]);
+               bestdist = DotProduct(rtlight->cached_frustumplanes[j].normal, points[0]);
                for (i = 1;i < 8;i++)
                {
-                       dist = DotProduct(rsurface.rtlight_frustumplanes[j].normal, points[i]);
+                       dist = DotProduct(rtlight->cached_frustumplanes[j].normal, points[i]);
                        if (bestdist > dist)
                                bestdist = dist;
                }
-               Con_Printf("light %p %splane #%i %f %f %f : %f < %f\n", rtlight, rsurface.rtlight_frustumplanes[j].dist < bestdist + 0.03125 ? "^2" : "^1", j, rsurface.rtlight_frustumplanes[j].normal[0], rsurface.rtlight_frustumplanes[j].normal[1], rsurface.rtlight_frustumplanes[j].normal[2], rsurface.rtlight_frustumplanes[j].dist, bestdist);
+               Con_Printf("light %p %splane #%i %f %f %f : %f < %f\n", rtlight, rtlight->cached_frustumplanes[j].dist < bestdist + 0.03125 ? "^2" : "^1", j, rtlight->cached_frustumplanes[j].normal[0], rtlight->cached_frustumplanes[j].normal[1], rtlight->cached_frustumplanes[j].normal[2], rtlight->cached_frustumplanes[j].dist, bestdist);
                // if the nearest point is near or behind the plane, we want this
                // plane, otherwise the plane is useless as it won't cull anything
-               if (rsurface.rtlight_frustumplanes[j].dist < bestdist + 0.03125)
+               if (rtlight->cached_frustumplanes[j].dist < bestdist + 0.03125)
                {
-                       PlaneClassify(&rsurface.rtlight_frustumplanes[j]);
-                       rsurface.rtlight_frustumplanes[rsurface.rtlight_numfrustumplanes++] = rsurface.rtlight_frustumplanes[j];
+                       PlaneClassify(&rtlight->cached_frustumplanes[j]);
+                       rtlight->cached_frustumplanes[rtlight->cached_numfrustumplanes++] = rtlight->cached_frustumplanes[j];
                }
        }
        }
@@ -3911,14 +3300,14 @@ void R_Shadow_DrawWorldShadow_ShadowMap(int numsurfaces, int *surfacelist, const
         CHECKGLERROR
     }
        else if (r_refdef.scene.worldentity->model)
-               r_refdef.scene.worldmodel->DrawShadowMap(r_shadow_shadowmapside, r_refdef.scene.worldentity, rsurface.rtlight->shadoworigin, NULL, rsurface.rtlight->radius, numsurfaces, surfacelist, surfacesides, rsurface.rtlight_cullmins, rsurface.rtlight_cullmaxs);
+               r_refdef.scene.worldmodel->DrawShadowMap(r_shadow_shadowmapside, r_refdef.scene.worldentity, rsurface.rtlight->shadoworigin, NULL, rsurface.rtlight->radius, numsurfaces, surfacelist, surfacesides, rsurface.rtlight->cached_cullmins, rsurface.rtlight->cached_cullmaxs);
 
        rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveWorldEntity/RSurf_ActiveModelEntity
 }
 
 void R_Shadow_DrawWorldShadow_ShadowVolume(int numsurfaces, int *surfacelist, const unsigned char *trispvs)
 {
-       qboolean zpass;
+       qboolean zpass = false;
        shadowmesh_t *mesh;
        int t, tend;
        int surfacelistindex;
@@ -3929,8 +3318,11 @@ void R_Shadow_DrawWorldShadow_ShadowVolume(int numsurfaces, int *surfacelist, co
        if (rsurface.rtlight->compiled && r_shadow_realtime_world_compile.integer && r_shadow_realtime_world_compileshadow.integer)
        {
                CHECKGLERROR
-               zpass = R_Shadow_UseZPass(r_refdef.scene.worldmodel->normalmins, r_refdef.scene.worldmodel->normalmaxs);
-               R_Shadow_RenderMode_StencilShadowVolumes(zpass);
+               if (r_shadow_rendermode != R_SHADOW_RENDERMODE_VISIBLEVOLUMES)
+               {
+                       zpass = R_Shadow_UseZPass(r_refdef.scene.worldmodel->normalmins, r_refdef.scene.worldmodel->normalmaxs);
+                       R_Shadow_RenderMode_StencilShadowVolumes(zpass);
+               }
                mesh = zpass ? rsurface.rtlight->static_meshchain_shadow_zpass : rsurface.rtlight->static_meshchain_shadow_zfail;
                for (;mesh;mesh = mesh->next)
                {
@@ -3975,7 +3367,7 @@ void R_Shadow_DrawWorldShadow_ShadowVolume(int numsurfaces, int *surfacelist, co
                R_Shadow_VolumeFromList(r_refdef.scene.worldmodel->brush.shadowmesh->numverts, r_refdef.scene.worldmodel->brush.shadowmesh->numtriangles, r_refdef.scene.worldmodel->brush.shadowmesh->vertex3f, r_refdef.scene.worldmodel->brush.shadowmesh->element3i, r_refdef.scene.worldmodel->brush.shadowmesh->neighbor3i, rsurface.rtlight->shadoworigin, NULL, rsurface.rtlight->radius + r_refdef.scene.worldmodel->radius*2 + r_shadow_projectdistance.value, numshadowmark, shadowmarklist, r_refdef.scene.worldmodel->normalmins, r_refdef.scene.worldmodel->normalmaxs);
        }
        else if (numsurfaces)
-               r_refdef.scene.worldmodel->DrawShadowVolume(r_refdef.scene.worldentity, rsurface.rtlight->shadoworigin, NULL, rsurface.rtlight->radius, numsurfaces, surfacelist, rsurface.rtlight_cullmins, rsurface.rtlight_cullmaxs);
+               r_refdef.scene.worldmodel->DrawShadowVolume(r_refdef.scene.worldentity, rsurface.rtlight->shadoworigin, NULL, rsurface.rtlight->radius, numsurfaces, surfacelist, rsurface.rtlight->cached_cullmins, rsurface.rtlight->cached_cullmaxs);
 
        rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveWorldEntity/RSurf_ActiveModelEntity
 }
@@ -4011,8 +3403,14 @@ void R_Shadow_SetupEntityLight(const entity_render_t *ent)
        Matrix4x4_Concat(&rsurface.entitytoattenuationxyz, &matrix_attenuationxyz, &rsurface.entitytolight);
        Matrix4x4_Concat(&rsurface.entitytoattenuationz, &matrix_attenuationz, &rsurface.entitytolight);
        Matrix4x4_Transform(&ent->inversematrix, rsurface.rtlight->shadoworigin, rsurface.entitylightorigin);
-       if (r_shadow_lightingrendermode == R_SHADOW_RENDERMODE_LIGHT_GLSL)
+       switch(r_shadow_lightingrendermode)
+       {
+       case R_SHADOW_RENDERMODE_LIGHT_GLSL:
                R_Mesh_TexMatrix(3, &rsurface.entitytolight);
+               break;
+       default:
+               break;
+       }
 }
 
 void R_Shadow_DrawWorldLight(int numsurfaces, int *surfacelist, const unsigned char *trispvs)
@@ -4027,8 +3425,14 @@ void R_Shadow_DrawWorldLight(int numsurfaces, int *surfacelist, const unsigned c
        Matrix4x4_Concat(&rsurface.entitytoattenuationxyz, &matrix_attenuationxyz, &rsurface.entitytolight);
        Matrix4x4_Concat(&rsurface.entitytoattenuationz, &matrix_attenuationz, &rsurface.entitytolight);
        VectorCopy(rsurface.rtlight->shadoworigin, rsurface.entitylightorigin);
-       if (r_shadow_lightingrendermode == R_SHADOW_RENDERMODE_LIGHT_GLSL)
+       switch(r_shadow_lightingrendermode)
+       {
+       case R_SHADOW_RENDERMODE_LIGHT_GLSL:
                R_Mesh_TexMatrix(3, &rsurface.entitytolight);
+               break;
+       default:
+               break;
+       }
 
        r_refdef.scene.worldmodel->DrawLight(r_refdef.scene.worldentity, numsurfaces, surfacelist, trispvs);
 
@@ -4048,7 +3452,7 @@ void R_Shadow_DrawEntityLight(entity_render_t *ent)
        rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveWorldEntity/RSurf_ActiveModelEntity
 }
 
-void R_DrawRTLight(rtlight_t *rtlight, qboolean visible)
+void R_CacheRTLight(rtlight_t *rtlight)
 {
        int i;
        float f;
@@ -4060,14 +3464,11 @@ void R_DrawRTLight(rtlight_t *rtlight, qboolean visible)
        int numshadowentities;
        int numshadowentities_noselfshadow;
        static entity_render_t *lightentities[MAX_EDICTS];
+       static entity_render_t *lightentities_noselfshadow[MAX_EDICTS];
        static entity_render_t *shadowentities[MAX_EDICTS];
-       static unsigned char entitysides[MAX_EDICTS];
-       int lightentities_noselfshadow;
-       int shadowentities_noselfshadow;
-       vec3_t nearestpoint;
-       vec_t distance;
-       qboolean castshadows;
-       int lodlinear;
+       static entity_render_t *shadowentities_noselfshadow[MAX_EDICTS];
+
+       rtlight->draw = false;
 
        // skip lights that don't light because of ambientscale+diffusescale+specularscale being 0 (corona only lights)
        // skip lights that are basically invisible (color 0 0 0)
@@ -4107,8 +3508,10 @@ void R_DrawRTLight(rtlight_t *rtlight, qboolean visible)
        if (R_CullBox(rtlight->cullmins, rtlight->cullmaxs))
                return;
 
-       VectorCopy(rtlight->cullmins, rsurface.rtlight_cullmins);
-       VectorCopy(rtlight->cullmaxs, rsurface.rtlight_cullmaxs);
+       VectorCopy(rtlight->cullmins, rtlight->cached_cullmins);
+       VectorCopy(rtlight->cullmaxs, rtlight->cached_cullmaxs);
+
+       R_Shadow_ComputeShadowCasterCullingPlanes(rtlight);
 
        if (rtlight->compiled && r_shadow_realtime_world_compile.integer)
        {
@@ -4127,8 +3530,8 @@ void R_DrawRTLight(rtlight_t *rtlight, qboolean visible)
        {
                // dynamic light, world available and can receive realtime lighting
                // calculate lit surfaces and leafs
-               R_Shadow_EnlargeLeafSurfaceTrisBuffer(r_refdef.scene.worldmodel->brush.num_leafs, r_refdef.scene.worldmodel->num_surfaces, r_refdef.scene.worldmodel->brush.shadowmesh ? r_refdef.scene.worldmodel->brush.shadowmesh->numtriangles : r_refdef.scene.worldmodel->surfmesh.num_triangles, r_refdef.scene.worldmodel->surfmesh.num_triangles);
-               r_refdef.scene.worldmodel->GetLightInfo(r_refdef.scene.worldentity, rtlight->shadoworigin, rtlight->radius, rsurface.rtlight_cullmins, rsurface.rtlight_cullmaxs, r_shadow_buffer_leaflist, r_shadow_buffer_leafpvs, &numleafs, r_shadow_buffer_surfacelist, r_shadow_buffer_surfacepvs, &numsurfaces, r_shadow_buffer_shadowtrispvs, r_shadow_buffer_lighttrispvs, r_shadow_buffer_visitingleafpvs);
+               r_refdef.scene.worldmodel->GetLightInfo(r_refdef.scene.worldentity, rtlight->shadoworigin, rtlight->radius, rtlight->cached_cullmins, rtlight->cached_cullmaxs, r_shadow_buffer_leaflist, r_shadow_buffer_leafpvs, &numleafs, r_shadow_buffer_surfacelist, r_shadow_buffer_surfacepvs, &numsurfaces, r_shadow_buffer_shadowtrispvs, r_shadow_buffer_lighttrispvs, r_shadow_buffer_visitingleafpvs, rtlight->cached_numfrustumplanes, rtlight->cached_frustumplanes);
+               R_Shadow_ComputeShadowCasterCullingPlanes(rtlight);
                leaflist = r_shadow_buffer_leaflist;
                leafpvs = r_shadow_buffer_leafpvs;
                surfacelist = r_shadow_buffer_surfacelist;
@@ -4136,7 +3539,7 @@ void R_DrawRTLight(rtlight_t *rtlight, qboolean visible)
                shadowtrispvs = r_shadow_buffer_shadowtrispvs;
                lighttrispvs = r_shadow_buffer_lighttrispvs;
                // if the reduced leaf bounds are offscreen, skip it
-               if (R_CullBox(rsurface.rtlight_cullmins, rsurface.rtlight_cullmaxs))
+               if (R_CullBox(rtlight->cached_cullmins, rtlight->cached_cullmaxs))
                        return;
        }
        else
@@ -4160,19 +3563,12 @@ void R_DrawRTLight(rtlight_t *rtlight, qboolean visible)
                if (i == numleafs)
                        return;
        }
-       // set up a scissor rectangle for this light
-       if (R_Shadow_ScissorForBBox(rsurface.rtlight_cullmins, rsurface.rtlight_cullmaxs))
-               return;
-
-       R_Shadow_ComputeShadowCasterCullingPlanes(rtlight);
 
        // make a list of lit entities and shadow casting entities
        numlightentities = 0;
        numlightentities_noselfshadow = 0;
-       lightentities_noselfshadow = sizeof(lightentities)/sizeof(lightentities[0]) - 1;
        numshadowentities = 0;
        numshadowentities_noselfshadow = 0;
-       shadowentities_noselfshadow = sizeof(shadowentities)/sizeof(shadowentities[0]) - 1;
 
        // add dynamic entities that are lit by the light
        if (r_drawentities.integer)
@@ -4182,11 +3578,11 @@ void R_DrawRTLight(rtlight_t *rtlight, qboolean visible)
                        dp_model_t *model;
                        entity_render_t *ent = r_refdef.scene.entities[i];
                        vec3_t org;
-                       if (!BoxesOverlap(ent->mins, ent->maxs, rsurface.rtlight_cullmins, rsurface.rtlight_cullmaxs))
+                       if (!BoxesOverlap(ent->mins, ent->maxs, rtlight->cached_cullmins, rtlight->cached_cullmaxs))
                                continue;
                        // skip the object entirely if it is not within the valid
                        // shadow-casting region (which includes the lit region)
-                       if (R_CullBoxCustomPlanes(ent->mins, ent->maxs, rsurface.rtlight_numfrustumplanes, rsurface.rtlight_frustumplanes))
+                       if (R_CullBoxCustomPlanes(ent->mins, ent->maxs, rtlight->cached_numfrustumplanes, rtlight->cached_frustumplanes))
                                continue;
                        if (!(model = ent->model))
                                continue;
@@ -4199,7 +3595,7 @@ void R_DrawRTLight(rtlight_t *rtlight, qboolean visible)
                                if (r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->brush.BoxTouchingLeafPVS && !r_refdef.scene.worldmodel->brush.BoxTouchingLeafPVS(r_refdef.scene.worldmodel, leafpvs, ent->mins, ent->maxs))
                                        continue;
                                if (ent->flags & RENDER_NOSELFSHADOW)
-                                       lightentities[lightentities_noselfshadow - numlightentities_noselfshadow++] = ent;
+                                       lightentities_noselfshadow[numlightentities_noselfshadow++] = ent;
                                else
                                        lightentities[numlightentities++] = ent;
                                // since it is lit, it probably also casts a shadow...
@@ -4214,7 +3610,7 @@ void R_DrawRTLight(rtlight_t *rtlight, qboolean visible)
                                        // RENDER_NOSELFSHADOW entities such as the gun
                                        // (very weird, but keeps the player shadow off the gun)
                                        if (ent->flags & (RENDER_NOSELFSHADOW | RENDER_EXTERIORMODEL))
-                                               shadowentities[shadowentities_noselfshadow - numshadowentities_noselfshadow++] = ent;
+                                               shadowentities_noselfshadow[numshadowentities_noselfshadow++] = ent;
                                        else
                                                shadowentities[numshadowentities++] = ent;
                                }
@@ -4232,7 +3628,7 @@ void R_DrawRTLight(rtlight_t *rtlight, qboolean visible)
                                if ((ent->flags & RENDER_SHADOW) && model->DrawShadowVolume && VectorDistance2(org, rtlight->shadoworigin) > 0.1)
                                {
                                        if (ent->flags & (RENDER_NOSELFSHADOW | RENDER_EXTERIORMODEL))
-                                               shadowentities[shadowentities_noselfshadow - numshadowentities_noselfshadow++] = ent;
+                                               shadowentities_noselfshadow[numshadowentities_noselfshadow++] = ent;
                                        else
                                                shadowentities[numshadowentities++] = ent;
                                }
@@ -4241,7 +3637,93 @@ void R_DrawRTLight(rtlight_t *rtlight, qboolean visible)
        }
 
        // return if there's nothing at all to light
-       if (!numlightentities && !numsurfaces)
+       if (numsurfaces + numlightentities + numlightentities_noselfshadow == 0)
+               return;
+
+       // count this light in the r_speeds
+       r_refdef.stats.lights++;
+
+       // flag it as worth drawing later
+       rtlight->draw = true;
+
+       // cache all the animated entities that cast a shadow but are not visible
+       for (i = 0;i < numshadowentities;i++)
+               if (shadowentities[i]->animcacheindex < 0)
+                       R_AnimCache_GetEntity(shadowentities[i], false, false);
+       for (i = 0;i < numshadowentities_noselfshadow;i++)
+               if (shadowentities_noselfshadow[i]->animcacheindex < 0)
+                       R_AnimCache_GetEntity(shadowentities_noselfshadow[i], false, false);
+
+       // allocate some temporary memory for rendering this light later in the frame
+       // reusable buffers need to be copied, static data can be used as-is
+       rtlight->cached_numlightentities               = numlightentities;
+       rtlight->cached_numlightentities_noselfshadow  = numlightentities_noselfshadow;
+       rtlight->cached_numshadowentities              = numshadowentities;
+       rtlight->cached_numshadowentities_noselfshadow = numshadowentities_noselfshadow;
+       rtlight->cached_numsurfaces                    = numsurfaces;
+       rtlight->cached_lightentities                  = (entity_render_t**)R_FrameData_Store(numlightentities*sizeof(entity_render_t*), (void*)lightentities);
+       rtlight->cached_lightentities_noselfshadow     = (entity_render_t**)R_FrameData_Store(numlightentities_noselfshadow*sizeof(entity_render_t*), (void*)lightentities_noselfshadow);
+       rtlight->cached_shadowentities                 = (entity_render_t**)R_FrameData_Store(numshadowentities*sizeof(entity_render_t*), (void*)shadowentities);
+       rtlight->cached_shadowentities_noselfshadow    = (entity_render_t**)R_FrameData_Store(numshadowentities_noselfshadow*sizeof(entity_render_t *), (void*)shadowentities_noselfshadow);
+       if (shadowtrispvs == r_shadow_buffer_shadowtrispvs)
+       {
+               rtlight->cached_shadowtrispvs                  =   (unsigned char *)R_FrameData_Store(r_refdef.scene.worldmodel->brush.shadowmesh ? r_refdef.scene.worldmodel->brush.shadowmesh->numtriangles : r_refdef.scene.worldmodel->surfmesh.num_triangles, shadowtrispvs);
+               rtlight->cached_lighttrispvs                   =   (unsigned char *)R_FrameData_Store(r_refdef.scene.worldmodel->surfmesh.num_triangles, lighttrispvs);
+               rtlight->cached_surfacelist                    =              (int*)R_FrameData_Store(numsurfaces*sizeof(int), (void*)surfacelist);
+       }
+       else
+       {
+               // compiled light data
+               rtlight->cached_shadowtrispvs = shadowtrispvs;
+               rtlight->cached_lighttrispvs = lighttrispvs;
+               rtlight->cached_surfacelist = surfacelist;
+       }
+}
+
+void R_DrawRTLight(rtlight_t *rtlight, qboolean visible)
+{
+       int i;
+       int numsurfaces;
+       unsigned char *shadowtrispvs, *lighttrispvs, *surfacesides;
+       int numlightentities;
+       int numlightentities_noselfshadow;
+       int numshadowentities;
+       int numshadowentities_noselfshadow;
+       entity_render_t **lightentities;
+       entity_render_t **lightentities_noselfshadow;
+       entity_render_t **shadowentities;
+       entity_render_t **shadowentities_noselfshadow;
+       int *surfacelist;
+       static unsigned char entitysides[MAX_EDICTS];
+       static unsigned char entitysides_noselfshadow[MAX_EDICTS];
+       vec3_t nearestpoint;
+       vec_t distance;
+       qboolean castshadows;
+       int lodlinear;
+
+       // check if we cached this light this frame (meaning it is worth drawing)
+       if (!rtlight->draw)
+               return;
+
+       // if R_FrameData_Store ran out of space we skip anything dependent on it
+       if (r_framedata_failed)
+               return;
+
+       numlightentities = rtlight->cached_numlightentities;
+       numlightentities_noselfshadow = rtlight->cached_numlightentities_noselfshadow;
+       numshadowentities = rtlight->cached_numshadowentities;
+       numshadowentities_noselfshadow = rtlight->cached_numshadowentities_noselfshadow;
+       numsurfaces = rtlight->cached_numsurfaces;
+       lightentities = rtlight->cached_lightentities;
+       lightentities_noselfshadow = rtlight->cached_lightentities_noselfshadow;
+       shadowentities = rtlight->cached_shadowentities;
+       shadowentities_noselfshadow = rtlight->cached_shadowentities_noselfshadow;
+       shadowtrispvs = rtlight->cached_shadowtrispvs;
+       lighttrispvs = rtlight->cached_lighttrispvs;
+       surfacelist = rtlight->cached_surfacelist;
+
+       // set up a scissor rectangle for this light
+       if (R_Shadow_ScissorForBBox(rtlight->cached_cullmins, rtlight->cached_cullmaxs))
                return;
 
        // don't let sound skip if going slow
@@ -4250,8 +3732,6 @@ void R_DrawRTLight(rtlight_t *rtlight, qboolean visible)
 
        // make this the active rtlight for rendering purposes
        R_Shadow_RenderMode_ActiveLight(rtlight);
-       // count this light in the r_speeds
-       r_refdef.stats.lights++;
 
        if (r_showshadowvolumes.integer && r_refdef.view.showdebug && numsurfaces + numshadowentities + numshadowentities_noselfshadow && rtlight->shadow && (rtlight->isstatic ? r_refdef.scene.rtworldshadows : r_refdef.scene.rtdlightshadows))
        {
@@ -4263,7 +3743,8 @@ void R_DrawRTLight(rtlight_t *rtlight, qboolean visible)
                for (i = 0;i < numshadowentities;i++)
                        R_Shadow_DrawEntityShadow(shadowentities[i]);
                for (i = 0;i < numshadowentities_noselfshadow;i++)
-                       R_Shadow_DrawEntityShadow(shadowentities[shadowentities_noselfshadow - i]);
+                       R_Shadow_DrawEntityShadow(shadowentities_noselfshadow[i]);
+               R_Shadow_RenderMode_VisibleLighting(false, false);
        }
 
        if (r_showlighting.integer && r_refdef.view.showdebug && numsurfaces + numlightentities + numlightentities_noselfshadow)
@@ -4276,7 +3757,7 @@ void R_DrawRTLight(rtlight_t *rtlight, qboolean visible)
                for (i = 0;i < numlightentities;i++)
                        R_Shadow_DrawEntityLight(lightentities[i]);
                for (i = 0;i < numlightentities_noselfshadow;i++)
-                       R_Shadow_DrawEntityLight(lightentities[lightentities_noselfshadow - i]);
+                       R_Shadow_DrawEntityLight(lightentities_noselfshadow[i]);
        }
 
        castshadows = numsurfaces + numshadowentities + numshadowentities_noselfshadow > 0 && rtlight->shadow && (rtlight->isstatic ? r_refdef.scene.rtworldshadows : r_refdef.scene.rtdlightshadows);
@@ -4312,6 +3793,7 @@ void R_DrawRTLight(rtlight_t *rtlight, qboolean visible)
                        
                borderbias = r_shadow_shadowmapborder / (float)(size - r_shadow_shadowmapborder);
 
+               surfacesides = NULL;
                if (numsurfaces)
                {
                        if (rtlight->compiled && r_shadow_realtime_world_compile.integer && r_shadow_realtime_world_compileshadow.integer)
@@ -4321,6 +3803,7 @@ void R_DrawRTLight(rtlight_t *rtlight, qboolean visible)
                        }
                        else
                        {
+                               surfacesides = r_shadow_buffer_surfacesides;
                                for(i = 0;i < numsurfaces;i++)
                                {
                                        msurface_t *surface = r_refdef.scene.worldmodel->data_surfaces + surfacelist[i];
@@ -4336,7 +3819,7 @@ void R_DrawRTLight(rtlight_t *rtlight, qboolean visible)
                                receivermask |= R_Shadow_CalcEntitySideMask(lightentities[i], &rtlight->matrix_worldtolight, &radiustolight, borderbias);
                        if (receivermask < 0x3F)
                                for(i = 0; i < numlightentities_noselfshadow;i++)
-                                       receivermask |= R_Shadow_CalcEntitySideMask(lightentities[lightentities_noselfshadow - i], &rtlight->matrix_worldtolight, &radiustolight, borderbias);
+                                       receivermask |= R_Shadow_CalcEntitySideMask(lightentities_noselfshadow[i], &rtlight->matrix_worldtolight, &radiustolight, borderbias);
                }
 
                receivermask &= R_Shadow_CullFrustumSides(rtlight, size, r_shadow_shadowmapborder);
@@ -4346,7 +3829,7 @@ void R_DrawRTLight(rtlight_t *rtlight, qboolean visible)
                        for (i = 0;i < numshadowentities;i++)
                                castermask |= (entitysides[i] = R_Shadow_CalcEntitySideMask(shadowentities[i], &rtlight->matrix_worldtolight, &radiustolight, borderbias));
                        for (i = 0;i < numshadowentities_noselfshadow;i++)
-                               castermask |= (entitysides[shadowentities_noselfshadow - i] = R_Shadow_CalcEntitySideMask(shadowentities[shadowentities_noselfshadow - i], &rtlight->matrix_worldtolight, &radiustolight, borderbias)); 
+                               castermask |= (entitysides_noselfshadow[i] = R_Shadow_CalcEntitySideMask(shadowentities_noselfshadow[i], &rtlight->matrix_worldtolight, &radiustolight, borderbias)); 
                }
 
                //Con_Printf("distance %f lodlinear %i (lod %i) size %i\n", distance, lodlinear, r_shadow_shadowmaplod, size);
@@ -4368,7 +3851,7 @@ void R_DrawRTLight(rtlight_t *rtlight, qboolean visible)
                        // draw lighting in the unmasked areas
                        R_Shadow_RenderMode_Lighting(false, false, true);
                        for (i = 0;i < numlightentities_noselfshadow;i++)
-                               R_Shadow_DrawEntityLight(lightentities[lightentities_noselfshadow - i]);
+                               R_Shadow_DrawEntityLight(lightentities_noselfshadow[i]);
                }
 
                // render shadow casters into 6 sided depth texture
@@ -4377,8 +3860,8 @@ void R_DrawRTLight(rtlight_t *rtlight, qboolean visible)
                        for (side = 0;side < 6;side++) if ((receivermask & castermask) & (1 << side))
                        {
                                R_Shadow_RenderMode_ShadowMap(side, false, size);
-                               for (i = 0;i < numshadowentities_noselfshadow;i++) if (entitysides[shadowentities_noselfshadow - i] & (1 << side))
-                                       R_Shadow_DrawEntityShadow(shadowentities[shadowentities_noselfshadow - i]);
+                               for (i = 0;i < numshadowentities_noselfshadow;i++) if (entitysides_noselfshadow[i] & (1 << side))
+                                       R_Shadow_DrawEntityShadow(shadowentities_noselfshadow[i]);
                        }
                }
 
@@ -4391,57 +3874,87 @@ void R_DrawRTLight(rtlight_t *rtlight, qboolean visible)
                for (i = 0;i < numlightentities;i++)
                        R_Shadow_DrawEntityLight(lightentities[i]);
        }
-       else if (castshadows && gl_stencil)
+       else if (castshadows && vid.stencil)
        {
                // draw stencil shadow volumes to mask off pixels that are in shadow
                // so that they won't receive lighting
                GL_Scissor(r_shadow_lightscissor[0], r_shadow_lightscissor[1], r_shadow_lightscissor[2], r_shadow_lightscissor[3]);
                R_Shadow_ClearStencil();
+
                if (numsurfaces)
                        R_Shadow_DrawWorldShadow_ShadowVolume(numsurfaces, surfacelist, shadowtrispvs);
                for (i = 0;i < numshadowentities;i++)
                        R_Shadow_DrawEntityShadow(shadowentities[i]);
-               if (numlightentities_noselfshadow)
-               {
-                       // draw lighting in the unmasked areas
-                       R_Shadow_RenderMode_Lighting(true, false, false);
-                       for (i = 0;i < numlightentities_noselfshadow;i++)
-                               R_Shadow_DrawEntityLight(lightentities[lightentities_noselfshadow - i]);
 
-                       // optionally draw the illuminated areas
-                       // for performance analysis by level designers
-                       if (r_showlighting.integer && r_refdef.view.showdebug)
-                       {
-                               R_Shadow_RenderMode_VisibleLighting(!r_showdisabledepthtest.integer, false);
-                               for (i = 0;i < numlightentities_noselfshadow;i++)
-                                       R_Shadow_DrawEntityLight(lightentities[lightentities_noselfshadow - i]);
-                       }
-               }
+               // draw lighting in the unmasked areas
+               R_Shadow_RenderMode_Lighting(true, false, false);
+               for (i = 0;i < numlightentities_noselfshadow;i++)
+                       R_Shadow_DrawEntityLight(lightentities_noselfshadow[i]);
+
                for (i = 0;i < numshadowentities_noselfshadow;i++)
-                       R_Shadow_DrawEntityShadow(shadowentities[shadowentities_noselfshadow - i]);
+                       R_Shadow_DrawEntityShadow(shadowentities_noselfshadow[i]);
+
+               // draw lighting in the unmasked areas
+               R_Shadow_RenderMode_Lighting(true, false, false);
+               if (numsurfaces)
+                       R_Shadow_DrawWorldLight(numsurfaces, surfacelist, lighttrispvs);
+               for (i = 0;i < numlightentities;i++)
+                       R_Shadow_DrawEntityLight(lightentities[i]);
+       }
+       else
+       {
+               // draw lighting in the unmasked areas
+               R_Shadow_RenderMode_Lighting(false, false, false);
+               if (numsurfaces)
+                       R_Shadow_DrawWorldLight(numsurfaces, surfacelist, lighttrispvs);
+               for (i = 0;i < numlightentities;i++)
+                       R_Shadow_DrawEntityLight(lightentities[i]);
+               for (i = 0;i < numlightentities_noselfshadow;i++)
+                       R_Shadow_DrawEntityLight(lightentities_noselfshadow[i]);
+       }
+}
+
+void R_PrepareRTLights(void)
+{
+       int flag;
+       int lnum;
+       size_t lightindex;
+       dlight_t *light;
+       size_t range;
+       float f;
 
-               if (numsurfaces + numlightentities)
+       R_Shadow_EnlargeLeafSurfaceTrisBuffer(r_refdef.scene.worldmodel->brush.num_leafs, r_refdef.scene.worldmodel->num_surfaces, r_refdef.scene.worldmodel->brush.shadowmesh ? r_refdef.scene.worldmodel->brush.shadowmesh->numtriangles : r_refdef.scene.worldmodel->surfmesh.num_triangles, r_refdef.scene.worldmodel->surfmesh.num_triangles);
+
+       flag = r_refdef.scene.rtworld ? LIGHTFLAG_REALTIMEMODE : LIGHTFLAG_NORMALMODE;
+       if (r_shadow_debuglight.integer >= 0)
+       {
+               lightindex = r_shadow_debuglight.integer;
+               light = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
+               if (light && (light->flags & flag))
+                       R_CacheRTLight(&light->rtlight);
+       }
+       else
+       {
+               range = Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray); // checked
+               for (lightindex = 0;lightindex < range;lightindex++)
                {
-                       // draw lighting in the unmasked areas
-                       R_Shadow_RenderMode_Lighting(true, false, false);
-                       if (numsurfaces)
-                               R_Shadow_DrawWorldLight(numsurfaces, surfacelist, lighttrispvs);
-                       for (i = 0;i < numlightentities;i++)
-                               R_Shadow_DrawEntityLight(lightentities[i]);
+                       light = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
+                       if (light && (light->flags & flag))
+                               R_CacheRTLight(&light->rtlight);
                }
        }
-       else
+       if (r_refdef.scene.rtdlight)
+       {
+               for (lnum = 0;lnum < r_refdef.scene.numlights;lnum++)
+                       R_CacheRTLight(r_refdef.scene.lights[lnum]);
+       }
+       else if(gl_flashblend.integer)
        {
-               if (numsurfaces + numlightentities)
+               for (lnum = 0;lnum < r_refdef.scene.numlights;lnum++)
                {
-                       // draw lighting in the unmasked areas
-                       R_Shadow_RenderMode_Lighting(false, false, false);
-                       if (numsurfaces)
-                               R_Shadow_DrawWorldLight(numsurfaces, surfacelist, lighttrispvs);
-                       for (i = 0;i < numlightentities;i++)
-                               R_Shadow_DrawEntityLight(lightentities[i]);
-                       for (i = 0;i < numlightentities_noselfshadow;i++)
-                               R_Shadow_DrawEntityLight(lightentities[lightentities_noselfshadow - i]);
+                       rtlight_t *rtlight = r_refdef.scene.lights[lnum];
+                       f = (rtlight->style >= 0 ? r_refdef.scene.lightstylevalue[rtlight->style] : 1) * r_shadow_lightintensityscale.value;
+                       VectorScale(rtlight->color, f, rtlight->currentcolor);
                }
        }
 }
@@ -4456,8 +3969,8 @@ void R_ShadowVolumeLighting(qboolean visible)
        size_t range;
        float f;
 
-       if (r_shadow_shadowmapmaxsize != bound(1, r_shadow_shadowmapping_maxsize.integer, gl_max_size.integer / 4) ||
-               (r_shadow_shadowmode != R_SHADOW_SHADOWMODE_STENCIL) != (r_shadow_shadowmapping.integer && r_glsl.integer && gl_support_fragment_shader && gl_support_ext_framebuffer_object) || 
+       if (r_shadow_shadowmapmaxsize != bound(1, r_shadow_shadowmapping_maxsize.integer, (int)vid.maxtexturesize_2d / 4) ||
+               (r_shadow_shadowmode != R_SHADOW_SHADOWMODE_STENCIL) != r_shadow_shadowmapping.integer ||
                r_shadow_shadowmapvsdct != (r_shadow_shadowmapping_vsdct.integer != 0) || 
                r_shadow_shadowmaptexturetype != r_shadow_shadowmapping_texturetype.integer ||
                r_shadow_shadowmapfilterquality != r_shadow_shadowmapping_filterquality.integer || 
@@ -4526,7 +4039,7 @@ void R_DrawModelShadows(void)
        vec3_t relativeshadowmins, relativeshadowmaxs;
        vec3_t tmp, shadowdir;
 
-       if (!r_drawentities.integer || !gl_stencil)
+       if (!r_drawentities.integer || !vid.stencil)
                return;
 
        CHECKGLERROR
@@ -4702,7 +4215,7 @@ void R_DrawCorona(rtlight_t *rtlight, float cscale, float scale)
        if (VectorLength(color) > (1.0f / 256.0f))
        {
                float vertex3f[12];
-               qboolean negated = (color[0] + color[1] + color[2] < 0) && gl_support_ext_blend_subtract;
+               qboolean negated = (color[0] + color[1] + color[2] < 0) && vid.support.ext_blend_subtract;
                if(negated)
                {
                        VectorNegate(color, color);
@@ -4737,7 +4250,7 @@ void R_DrawCoronas(void)
        // use GL_ARB_occlusion_query if available
        // otherwise use raytraces
        r_numqueries = 0;
-       usequery = gl_support_arb_occlusion_query && r_coronas_occlusionquery.integer;
+       usequery = vid.support.arb_occlusion_query && r_coronas_occlusionquery.integer;
        if (usequery)
        {
                GL_ColorMask(0,0,0,0);