]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - r_shadow.c
Change shadowless rtlights so that they are not occluded by anything - no pvs checks...
[xonotic/darkplaces.git] / r_shadow.c
index f388ce4a0c68f1445e1127fc6ddc47c001267f79..286244744b935d54be3f1d269befdcb586e95987 100644 (file)
@@ -314,14 +314,14 @@ cvar_t r_shadow_realtime_world_compileshadow = {0, "r_shadow_realtime_world_comp
 cvar_t r_shadow_realtime_world_compilesvbsp = {0, "r_shadow_realtime_world_compilesvbsp", "1", "enables svbsp optimization during compilation (slower than compileportalculling but more exact)"};
 cvar_t r_shadow_realtime_world_compileportalculling = {0, "r_shadow_realtime_world_compileportalculling", "1", "enables portal-based culling optimization during compilation (overrides compilesvbsp)"};
 cvar_t r_shadow_scissor = {0, "r_shadow_scissor", "1", "use scissor optimization of light rendering (restricts rendering to the portion of the screen affected by the light)"};
-cvar_t r_shadow_shadowmapping = {CVAR_SAVE, "r_shadow_shadowmapping", "1", "enables use of shadowmapping (depth texture sampling) instead of stencil shadow volumes, requires gl_fbo 1"};
+cvar_t r_shadow_shadowmapping = {CVAR_SAVE, "r_shadow_shadowmapping", "1", "enables use of shadowmapping (depth texture sampling) instead of stencil shadow volumes"};
 cvar_t r_shadow_shadowmapping_filterquality = {CVAR_SAVE, "r_shadow_shadowmapping_filterquality", "-1", "shadowmap filter modes: -1 = auto-select, 0 = no filtering, 1 = bilinear, 2 = bilinear 2x2 blur (fast), 3 = 3x3 blur (moderate), 4 = 4x4 blur (slow)"};
 cvar_t r_shadow_shadowmapping_useshadowsampler = {CVAR_SAVE, "r_shadow_shadowmapping_useshadowsampler", "1", "whether to use sampler2DShadow if available"};
 cvar_t r_shadow_shadowmapping_depthbits = {CVAR_SAVE, "r_shadow_shadowmapping_depthbits", "24", "requested minimum shadowmap texture depth bits"};
 cvar_t r_shadow_shadowmapping_vsdct = {CVAR_SAVE, "r_shadow_shadowmapping_vsdct", "1", "enables use of virtual shadow depth cube texture"};
 cvar_t r_shadow_shadowmapping_minsize = {CVAR_SAVE, "r_shadow_shadowmapping_minsize", "32", "limit of shadowmap side size - must be at least r_shadow_shadowmapping_bordersize+2"};
 cvar_t r_shadow_shadowmapping_maxsize = {CVAR_SAVE, "r_shadow_shadowmapping_maxsize", "512", "limit of shadowmap side size - can not be more than 1/8th of atlassize because lights store 6 sides (2x3 grid) and sometimes 12 sides (4x3 grid for shadows from EF_NOSELFSHADOW entities) and there are multiple lights..."};
-cvar_t r_shadow_shadowmapping_texturesize = { CVAR_SAVE, "r_shadow_shadowmapping_texturesize", "4096", "size of shadowmap atlas texture - all shadowmaps are packed into this texture at frame start"};
+cvar_t r_shadow_shadowmapping_texturesize = { CVAR_SAVE, "r_shadow_shadowmapping_texturesize", "8192", "size of shadowmap atlas texture - all shadowmaps are packed into this texture at frame start"};
 cvar_t r_shadow_shadowmapping_precision = {CVAR_SAVE, "r_shadow_shadowmapping_precision", "1", "makes shadowmaps have a maximum resolution of this number of pixels per light source radius unit such that, for example, at precision 0.5 a light with radius 200 will have a maximum resolution of 100 pixels"};
 //cvar_t r_shadow_shadowmapping_lod_bias = {CVAR_SAVE, "r_shadow_shadowmapping_lod_bias", "16", "shadowmap size bias"};
 //cvar_t r_shadow_shadowmapping_lod_scale = {CVAR_SAVE, "r_shadow_shadowmapping_lod_scale", "128", "shadowmap size scaling parameter"};
@@ -335,18 +335,18 @@ cvar_t r_shadow_polygonfactor = {0, "r_shadow_polygonfactor", "0", "how much to
 cvar_t r_shadow_polygonoffset = {0, "r_shadow_polygonoffset", "1", "how much to push shadow volumes into the distance when rendering, to reduce chances of zfighting artifacts (should not be less than 0)"};
 cvar_t r_shadow_texture3d = {0, "r_shadow_texture3d", "1", "use 3D voxel textures for spherical attenuation rather than cylindrical (does not affect OpenGL 2.0 render path)"};
 cvar_t r_shadow_bouncegrid = {CVAR_SAVE, "r_shadow_bouncegrid", "0", "perform particle tracing for indirect lighting (Global Illumination / radiosity) using a 3D texture covering the scene, only active on levels with realtime lights active (r_shadow_realtime_world is usually required for these)"};
-cvar_t r_shadow_bouncegrid_blur = {CVAR_SAVE, "r_shadow_bouncegrid_blur", "1", "apply a 1-radius blur on bouncegrid to denoise it and deal with boundary issues with surfaces"};
+cvar_t r_shadow_bouncegrid_blur = {CVAR_SAVE, "r_shadow_bouncegrid_blur", "0", "apply a 1-radius blur on bouncegrid to denoise it and deal with boundary issues with surfaces"};
 cvar_t r_shadow_bouncegrid_bounceanglediffuse = {CVAR_SAVE, "r_shadow_bouncegrid_bounceanglediffuse", "0", "use random bounce direction rather than true reflection, makes some corner areas dark"};
+cvar_t r_shadow_bouncegrid_dynamic_bounceminimumintensity = { CVAR_SAVE, "r_shadow_bouncegrid_dynamic_bounceminimumintensity", "0.05", "stop bouncing once intensity drops below this fraction of the original particle color" };
 cvar_t r_shadow_bouncegrid_dynamic_culllightpaths = {CVAR_SAVE, "r_shadow_bouncegrid_dynamic_culllightpaths", "1", "skip accumulating light in the bouncegrid texture where the light paths are out of view (dynamic mode only)"};
-cvar_t r_shadow_bouncegrid_dynamic_dlightparticlemultiplier = {CVAR_SAVE, "r_shadow_bouncegrid_dynamic_dlightparticlemultiplier", "1", "if set to a high value like 16 this can make dlights look great, but 0 is recommended for performance reasons"};
 cvar_t r_shadow_bouncegrid_dynamic_directionalshading = {CVAR_SAVE, "r_shadow_bouncegrid_dynamic_directionalshading", "0", "use diffuse shading rather than ambient, 3D texture becomes 8x as many pixels to hold the additional data"};
+cvar_t r_shadow_bouncegrid_dynamic_dlightparticlemultiplier = {CVAR_SAVE, "r_shadow_bouncegrid_dynamic_dlightparticlemultiplier", "1", "if set to a high value like 16 this can make dlights look great, but 0 is recommended for performance reasons"};
 cvar_t r_shadow_bouncegrid_dynamic_hitmodels = {CVAR_SAVE, "r_shadow_bouncegrid_dynamic_hitmodels", "0", "enables hitting character model geometry (SLOW)"};
-cvar_t r_shadow_bouncegrid_dynamic_energyperphoton = {CVAR_SAVE, "r_shadow_bouncegrid_dynamic_energyperphoton", "10000", "amount of light that one photon should represent"};
-cvar_t r_shadow_bouncegrid_dynamic_lightradiusscale = {CVAR_SAVE, "r_shadow_bouncegrid_dynamic_lightradiusscale", "10", "particles stop at this fraction of light radius (can be more than 1)"};
-cvar_t r_shadow_bouncegrid_dynamic_maxbounce = {CVAR_SAVE, "r_shadow_bouncegrid_dynamic_maxbounce", "5", "maximum number of bounces for a particle (minimum is 0)"};
+cvar_t r_shadow_bouncegrid_dynamic_lightradiusscale = {CVAR_SAVE, "r_shadow_bouncegrid_dynamic_lightradiusscale", "2", "particles stop at this fraction of light radius (can be more than 1)"};
+cvar_t r_shadow_bouncegrid_dynamic_maxbounce = {CVAR_SAVE, "r_shadow_bouncegrid_dynamic_maxbounce", "2", "maximum number of bounces for a particle (minimum is 0)"};
 cvar_t r_shadow_bouncegrid_dynamic_maxphotons = {CVAR_SAVE, "r_shadow_bouncegrid_dynamic_maxphotons", "25000", "upper bound on photons to shoot per update, divided proportionately between lights - normally the number of photons is calculated by energyperphoton"};
+cvar_t r_shadow_bouncegrid_dynamic_quality = {CVAR_SAVE, "r_shadow_bouncegrid_dynamic_quality", "1", "amount of photons that should be fired (this is multiplied by spacing^2 to make it adaptive with spacing changes)"};
 cvar_t r_shadow_bouncegrid_dynamic_spacing = {CVAR_SAVE, "r_shadow_bouncegrid_dynamic_spacing", "64", "unit size of bouncegrid pixel"};
-cvar_t r_shadow_bouncegrid_dynamic_stablerandom = {CVAR_SAVE, "r_shadow_bouncegrid_dynamic_stablerandom", "1", "make particle distribution consistent from frame to frame"};
 cvar_t r_shadow_bouncegrid_dynamic_updateinterval = {CVAR_SAVE, "r_shadow_bouncegrid_dynamic_updateinterval", "0", "update bouncegrid texture once per this many seconds, useful values are 0, 0.05, or 1000000"};
 cvar_t r_shadow_bouncegrid_dynamic_x = {CVAR_SAVE, "r_shadow_bouncegrid_dynamic_x", "64", "maximum texture size of bouncegrid on X axis"};
 cvar_t r_shadow_bouncegrid_dynamic_y = {CVAR_SAVE, "r_shadow_bouncegrid_dynamic_y", "64", "maximum texture size of bouncegrid on Y axis"};
@@ -354,16 +354,21 @@ cvar_t r_shadow_bouncegrid_dynamic_z = {CVAR_SAVE, "r_shadow_bouncegrid_dynamic_
 cvar_t r_shadow_bouncegrid_floatcolors = {CVAR_SAVE, "r_shadow_bouncegrid_floatcolors", "1", "upload texture as RGBA16F (or RGBA32F when set to 2) rather than RGBA8 format - this gives more dynamic range and accuracy"};
 cvar_t r_shadow_bouncegrid_includedirectlighting = {CVAR_SAVE, "r_shadow_bouncegrid_includedirectlighting", "0", "allows direct lighting to be recorded, not just indirect (gives an effect somewhat like r_shadow_realtime_world_lightmaps)"};
 cvar_t r_shadow_bouncegrid_intensity = {CVAR_SAVE, "r_shadow_bouncegrid_intensity", "4", "overall brightness of bouncegrid texture"};
+cvar_t r_shadow_bouncegrid_lightpathsize_conespread = {CVAR_SAVE, "r_shadow_bouncegrid_lightpathsize_conespread", "0.015625", "increase lightpathsize over distance at this rate per grid cell"};
+cvar_t r_shadow_bouncegrid_lightpathsize_initial = {CVAR_SAVE, "r_shadow_bouncegrid_lightpathsize_initial", "0.5", "width (in grid cells) of the light path for accumulation of light in the bouncegrid texture"};
+cvar_t r_shadow_bouncegrid_normalizevectors = { CVAR_SAVE, "r_shadow_bouncegrid_normalizevectors", "1", "normalize random vectors (otherwise their length can vary, which dims the lighting further from the light)" };
 cvar_t r_shadow_bouncegrid_particlebounceintensity = {CVAR_SAVE, "r_shadow_bouncegrid_particlebounceintensity", "2", "amount of energy carried over after each bounce, this is a multiplier of texture color and the result is clamped to 1 or less, to prevent adding energy on each bounce"};
 cvar_t r_shadow_bouncegrid_particleintensity = {CVAR_SAVE, "r_shadow_bouncegrid_particleintensity", "0.25", "brightness of particles contributing to bouncegrid texture"};
+cvar_t r_shadow_bouncegrid_rng_seed = { CVAR_SAVE, "r_shadow_bouncegrid_rng_seed", "0", "0+ = use this number as RNG seed, -1 = use time instead for disco-like craziness in dynamic mode" };
+cvar_t r_shadow_bouncegrid_rng_type = { CVAR_SAVE, "r_shadow_bouncegrid_rng_type", "0", "0 = Lehmer 128bit RNG (slow but high quality), 1 = lhcheeserand 32bit RNG (quick)" };
 cvar_t r_shadow_bouncegrid_sortlightpaths = {CVAR_SAVE, "r_shadow_bouncegrid_sortlightpaths", "1", "sort light paths before accumulating them into the bouncegrid texture, this reduces cpu cache misses"};
-cvar_t r_shadow_bouncegrid_lightpathsize = {CVAR_SAVE, "r_shadow_bouncegrid_lightpathsize", "1", "width of the light path for accumulation of light in the bouncegrid texture"};
 cvar_t r_shadow_bouncegrid_static = {CVAR_SAVE, "r_shadow_bouncegrid_static", "1", "use static radiosity solution (high quality) rather than dynamic (splotchy)"};
+cvar_t r_shadow_bouncegrid_static_bounceminimumintensity = { CVAR_SAVE, "r_shadow_bouncegrid_static_bounceminimumintensity", "0.01", "stop bouncing once intensity drops below this fraction of the original particle color" };
 cvar_t r_shadow_bouncegrid_static_directionalshading = {CVAR_SAVE, "r_shadow_bouncegrid_static_directionalshading", "1", "whether to use directionalshading when in static mode"};
-cvar_t r_shadow_bouncegrid_static_energyperphoton = {CVAR_SAVE, "r_shadow_bouncegrid_static_energyperphoton", "10000", "amount of light that one photon should represent in static mode"};
-cvar_t r_shadow_bouncegrid_static_lightradiusscale = {CVAR_SAVE, "r_shadow_bouncegrid_static_lightradiusscale", "10", "particles stop at this fraction of light radius (can be more than 1) when in static mode"};
+cvar_t r_shadow_bouncegrid_static_lightradiusscale = {CVAR_SAVE, "r_shadow_bouncegrid_static_lightradiusscale", "2", "particles stop at this fraction of light radius (can be more than 1) when in static mode"};
 cvar_t r_shadow_bouncegrid_static_maxbounce = {CVAR_SAVE, "r_shadow_bouncegrid_static_maxbounce", "5", "maximum number of bounces for a particle (minimum is 0) in static mode"};
 cvar_t r_shadow_bouncegrid_static_maxphotons = {CVAR_SAVE, "r_shadow_bouncegrid_static_maxphotons", "250000", "upper bound on photons in static mode"};
+cvar_t r_shadow_bouncegrid_static_quality = { CVAR_SAVE, "r_shadow_bouncegrid_static_quality", "16", "amount of photons that should be fired (this is multiplied by spacing^2 to make it adaptive with spacing changes)" };
 cvar_t r_shadow_bouncegrid_static_spacing = {CVAR_SAVE, "r_shadow_bouncegrid_static_spacing", "64", "unit size of bouncegrid pixel when in static mode"};
 cvar_t r_coronas = {CVAR_SAVE, "r_coronas", "0", "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 checksum the proportion of hidden pixels controls corona intensity"};
@@ -459,19 +464,19 @@ static void R_Shadow_SetShadowMode(void)
                        {
                                if (!r_fb.usedepthtextures)
                                        r_shadow_shadowmappcf = 1;
-                               else if((strstr(gl_vendor, "NVIDIA") || strstr(gl_renderer, "Radeon HD")) && vid.support.arb_shadow && r_shadow_shadowmapshadowsampler) 
+                               else if((strstr(gl_vendor, "NVIDIA") || strstr(gl_renderer, "Radeon HD")) && vid.support.arb_shadow && r_shadow_shadowmapshadowsampler)
                                {
                                        r_shadow_shadowmapsampler = true;
                                        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") || strstr(gl_vendor, "Advanced Micro Devices")) && !strstr(gl_renderer, "Mesa") && !strstr(gl_version, "Mesa")) 
+                               else if((strstr(gl_vendor, "ATI") || strstr(gl_vendor, "Advanced Micro Devices")) && !strstr(gl_renderer, "Mesa") && !strstr(gl_version, "Mesa"))
                                        r_shadow_shadowmappcf = 1;
-                               else 
+                               else
                                        r_shadow_shadowmapsampler = vid.support.arb_shadow && r_shadow_shadowmapshadowsampler;
                        }
-                       else 
+                       else
                        {
                 r_shadow_shadowmapsampler = vid.support.arb_shadow && r_shadow_shadowmapshadowsampler;
                                switch (r_shadow_shadowmapfilterquality)
@@ -551,7 +556,6 @@ static void r_shadow_start(void)
 {
        // allocate vertex processing arrays
        memset(&r_shadow_bouncegrid_state, 0, sizeof(r_shadow_bouncegrid_state));
-       r_shadow_bouncegrid_state.maxsplatpaths = 16384;
        r_shadow_attenuationgradienttexture = NULL;
        r_shadow_attenuation2dtexture = NULL;
        r_shadow_attenuation3dtexture = NULL;
@@ -653,6 +657,13 @@ static void r_shadow_shutdown(void)
        if (r_shadow_scenelightlist)
                Mem_Free(r_shadow_scenelightlist);
        r_shadow_scenelightlist = NULL;
+       r_shadow_bouncegrid_state.highpixels = NULL;
+       if (r_shadow_bouncegrid_state.blurpixels[0]) Mem_Free(r_shadow_bouncegrid_state.blurpixels[0]); r_shadow_bouncegrid_state.blurpixels[0] = NULL;
+       if (r_shadow_bouncegrid_state.blurpixels[1]) Mem_Free(r_shadow_bouncegrid_state.blurpixels[1]); r_shadow_bouncegrid_state.blurpixels[1] = NULL;
+       if (r_shadow_bouncegrid_state.u8pixels) Mem_Free(r_shadow_bouncegrid_state.u8pixels); r_shadow_bouncegrid_state.u8pixels = NULL;
+       if (r_shadow_bouncegrid_state.fp16pixels) Mem_Free(r_shadow_bouncegrid_state.fp16pixels); r_shadow_bouncegrid_state.fp16pixels = NULL;
+       if (r_shadow_bouncegrid_state.splatpaths) Mem_Free(r_shadow_bouncegrid_state.splatpaths); r_shadow_bouncegrid_state.splatpaths = NULL;
+       r_shadow_bouncegrid_state.maxsplatpaths = 0;
        memset(&r_shadow_bouncegrid_state, 0, sizeof(r_shadow_bouncegrid_state));
        r_shadow_attenuationgradienttexture = NULL;
        r_shadow_attenuation2dtexture = NULL;
@@ -721,6 +732,13 @@ static void r_shadow_shutdown(void)
 
 static void r_shadow_newmap(void)
 {
+       r_shadow_bouncegrid_state.highpixels = NULL;
+       if (r_shadow_bouncegrid_state.blurpixels[0]) Mem_Free(r_shadow_bouncegrid_state.blurpixels[0]); r_shadow_bouncegrid_state.blurpixels[0] = NULL;
+       if (r_shadow_bouncegrid_state.blurpixels[1]) Mem_Free(r_shadow_bouncegrid_state.blurpixels[1]); r_shadow_bouncegrid_state.blurpixels[1] = NULL;
+       if (r_shadow_bouncegrid_state.u8pixels) Mem_Free(r_shadow_bouncegrid_state.u8pixels); r_shadow_bouncegrid_state.u8pixels = NULL;
+       if (r_shadow_bouncegrid_state.fp16pixels) Mem_Free(r_shadow_bouncegrid_state.fp16pixels); r_shadow_bouncegrid_state.fp16pixels = NULL;
+       if (r_shadow_bouncegrid_state.splatpaths) Mem_Free(r_shadow_bouncegrid_state.splatpaths); r_shadow_bouncegrid_state.splatpaths = NULL;
+       r_shadow_bouncegrid_state.maxsplatpaths = 0;
        if (r_shadow_bouncegrid_state.texture)    R_FreeTexture(r_shadow_bouncegrid_state.texture);r_shadow_bouncegrid_state.texture = NULL;
        if (r_shadow_lightcorona)                 R_SkinFrame_MarkUsed(r_shadow_lightcorona);
        if (r_editlights_sprcursor)               R_SkinFrame_MarkUsed(r_editlights_sprcursor);
@@ -789,16 +807,16 @@ void R_Shadow_Init(void)
        Cvar_RegisterVariable(&r_shadow_bouncegrid);
        Cvar_RegisterVariable(&r_shadow_bouncegrid_blur);
        Cvar_RegisterVariable(&r_shadow_bouncegrid_bounceanglediffuse);
+       Cvar_RegisterVariable(&r_shadow_bouncegrid_dynamic_bounceminimumintensity);
        Cvar_RegisterVariable(&r_shadow_bouncegrid_dynamic_culllightpaths);
        Cvar_RegisterVariable(&r_shadow_bouncegrid_dynamic_directionalshading);
        Cvar_RegisterVariable(&r_shadow_bouncegrid_dynamic_dlightparticlemultiplier);
        Cvar_RegisterVariable(&r_shadow_bouncegrid_dynamic_hitmodels);
-       Cvar_RegisterVariable(&r_shadow_bouncegrid_dynamic_energyperphoton);
        Cvar_RegisterVariable(&r_shadow_bouncegrid_dynamic_lightradiusscale);
        Cvar_RegisterVariable(&r_shadow_bouncegrid_dynamic_maxbounce);
        Cvar_RegisterVariable(&r_shadow_bouncegrid_dynamic_maxphotons);
+       Cvar_RegisterVariable(&r_shadow_bouncegrid_dynamic_quality);
        Cvar_RegisterVariable(&r_shadow_bouncegrid_dynamic_spacing);
-       Cvar_RegisterVariable(&r_shadow_bouncegrid_dynamic_stablerandom);
        Cvar_RegisterVariable(&r_shadow_bouncegrid_dynamic_updateinterval);
        Cvar_RegisterVariable(&r_shadow_bouncegrid_dynamic_x);
        Cvar_RegisterVariable(&r_shadow_bouncegrid_dynamic_y);
@@ -806,17 +824,22 @@ void R_Shadow_Init(void)
        Cvar_RegisterVariable(&r_shadow_bouncegrid_floatcolors);
        Cvar_RegisterVariable(&r_shadow_bouncegrid_includedirectlighting);
        Cvar_RegisterVariable(&r_shadow_bouncegrid_intensity);
-       Cvar_RegisterVariable(&r_shadow_bouncegrid_lightpathsize);
+       Cvar_RegisterVariable(&r_shadow_bouncegrid_lightpathsize_conespread);
+       Cvar_RegisterVariable(&r_shadow_bouncegrid_lightpathsize_initial);
+       Cvar_RegisterVariable(&r_shadow_bouncegrid_normalizevectors);
        Cvar_RegisterVariable(&r_shadow_bouncegrid_particlebounceintensity);
        Cvar_RegisterVariable(&r_shadow_bouncegrid_particleintensity);
+       Cvar_RegisterVariable(&r_shadow_bouncegrid_rng_seed);
+       Cvar_RegisterVariable(&r_shadow_bouncegrid_rng_type);
        Cvar_RegisterVariable(&r_shadow_bouncegrid_sortlightpaths);
        Cvar_RegisterVariable(&r_shadow_bouncegrid_static);
-       Cvar_RegisterVariable(&r_shadow_bouncegrid_static_spacing);
+       Cvar_RegisterVariable(&r_shadow_bouncegrid_static_bounceminimumintensity);
        Cvar_RegisterVariable(&r_shadow_bouncegrid_static_directionalshading);
        Cvar_RegisterVariable(&r_shadow_bouncegrid_static_lightradiusscale);
        Cvar_RegisterVariable(&r_shadow_bouncegrid_static_maxbounce);
        Cvar_RegisterVariable(&r_shadow_bouncegrid_static_maxphotons);
-       Cvar_RegisterVariable(&r_shadow_bouncegrid_static_energyperphoton);
+       Cvar_RegisterVariable(&r_shadow_bouncegrid_static_quality);
+       Cvar_RegisterVariable(&r_shadow_bouncegrid_static_spacing);
        Cvar_RegisterVariable(&r_coronas);
        Cvar_RegisterVariable(&r_coronas_occlusionsizescale);
        Cvar_RegisterVariable(&r_coronas_occlusionquery);
@@ -1584,7 +1607,7 @@ static int R_Shadow_CullFrustumSides(rtlight_t *rtlight, float size, float borde
        int sides = 0x3F, masks[6] = { 3<<4, 3<<4, 3<<0, 3<<0, 3<<2, 3<<2 };
        float scale = (size - 2*border)/size, len;
        float bias = border / (float)(size - border), dp, dn, ap, an;
-       // check if cone enclosing side would cross frustum plane 
+       // check if cone enclosing side would cross frustum plane
        scale = 2 / (scale*scale + 2);
        Matrix4x4_OriginFromMatrix(&rtlight->matrix_lighttoworld, o);
        for (i = 0;i < 5;i++)
@@ -2408,11 +2431,13 @@ typedef struct r_shadow_bouncegrid_splatpath_s
        vec3_t splatcolor;
        vec3_t splatdir;
        vec_t splatintensity;
+       vec_t splatsize_current;
+       vec_t splatsize_perstep;
        int remainingsplats;
 }
 r_shadow_bouncegrid_splatpath_t;
 
-static void R_Shadow_BounceGrid_AddSplatPath(vec3_t originalstart, vec3_t originalend, vec3_t color)
+static void R_Shadow_BounceGrid_AddSplatPath(vec3_t originalstart, vec3_t originalend, vec3_t color, vec_t distancetraveled)
 {
        int bestaxis;
        int numsplats;
@@ -2464,12 +2489,10 @@ static void R_Shadow_BounceGrid_AddSplatPath(vec3_t originalstart, vec3_t origin
        {
                // double the limit, this will persist from frame to frame so we don't
                // make the same mistake each time
-               r_shadow_bouncegrid_splatpath_t *newpaths;
                r_shadow_bouncegrid_state.maxsplatpaths *= 2;
-               newpaths = (r_shadow_bouncegrid_splatpath_t *)R_FrameData_Alloc(sizeof(r_shadow_bouncegrid_splatpath_t) * r_shadow_bouncegrid_state.maxsplatpaths);
-               if (r_shadow_bouncegrid_state.splatpaths)
-                       memcpy(newpaths, r_shadow_bouncegrid_state.splatpaths, r_shadow_bouncegrid_state.numsplatpaths * sizeof(r_shadow_bouncegrid_splatpath_t));
-               r_shadow_bouncegrid_state.splatpaths = newpaths;
+               if (r_shadow_bouncegrid_state.maxsplatpaths < 16384)
+                       r_shadow_bouncegrid_state.maxsplatpaths = 16384;
+               r_shadow_bouncegrid_state.splatpaths = (r_shadow_bouncegrid_splatpath_t *)Mem_Realloc(r_main_mempool, r_shadow_bouncegrid_state.splatpaths, sizeof(r_shadow_bouncegrid_splatpath_t) * r_shadow_bouncegrid_state.maxsplatpaths);
        }
 
        // divide a series of splats along the length using the maximum axis
@@ -2494,6 +2517,8 @@ static void R_Shadow_BounceGrid_AddSplatPath(vec3_t originalstart, vec3_t origin
        VectorScale(diff, ilen, path->step);
        VectorCopy(color, path->splatcolor);
        VectorCopy(originaldir, path->splatdir);
+       path->splatsize_current = r_shadow_bouncegrid_state.settings.lightpathsize_initial + r_shadow_bouncegrid_state.settings.lightpathsize_conespread * distancetraveled * r_shadow_bouncegrid_state.ispacing[0];
+       path->splatsize_perstep = r_shadow_bouncegrid_state.settings.lightpathsize_conespread;
        path->splatintensity = VectorLength(color);
        path->remainingsplats = numsplats;
 }
@@ -2535,16 +2560,19 @@ static qboolean R_Shadow_BounceGrid_CheckEnable(int flag)
 static void R_Shadow_BounceGrid_GenerateSettings(r_shadow_bouncegrid_settings_t *settings)
 {
        qboolean s = r_shadow_bouncegrid_static.integer != 0;
-       float spacing = s ? r_shadow_bouncegrid_static_spacing.value : r_shadow_bouncegrid_dynamic_spacing.value;
+       float spacing = bound(1.0f, s ? r_shadow_bouncegrid_static_spacing.value : r_shadow_bouncegrid_dynamic_spacing.value, 1024.0f);
+       float quality = bound(0.0001f, (s ? r_shadow_bouncegrid_static_quality.value : r_shadow_bouncegrid_dynamic_quality.value), 1024.0f);
+       float bounceminimumintensity = s ? r_shadow_bouncegrid_static_bounceminimumintensity.value : r_shadow_bouncegrid_dynamic_bounceminimumintensity.value;
 
        // prevent any garbage in alignment padded areas as we'll be using memcmp
-       memset(settings, 0, sizeof(*settings)); 
+       memset(settings, 0, sizeof(*settings));
 
        // build up a complete collection of the desired settings, so that memcmp can be used to compare parameters
        settings->staticmode                    = s;
        settings->blur                          = r_shadow_bouncegrid_blur.integer != 0;
        settings->floatcolors                   = bound(0, r_shadow_bouncegrid_floatcolors.integer, 2);
-       settings->lightpathsize                 = bound(1, r_shadow_bouncegrid_lightpathsize.integer, MAXBOUNCEGRIDSPLATSIZE);
+       settings->lightpathsize_initial         = bound(0.0f, r_shadow_bouncegrid_lightpathsize_initial.value, 1024.0f);
+       settings->lightpathsize_conespread      = bound(0.0f, r_shadow_bouncegrid_lightpathsize_conespread.value, 1024.0f);
        settings->bounceanglediffuse            = r_shadow_bouncegrid_bounceanglediffuse.integer != 0;
        settings->directionalshading            = (s ? r_shadow_bouncegrid_static_directionalshading.integer != 0 : r_shadow_bouncegrid_dynamic_directionalshading.integer != 0) && r_shadow_bouncegrid_state.allowdirectionalshading;
        settings->dlightparticlemultiplier      = s ? 0 : r_shadow_bouncegrid_dynamic_dlightparticlemultiplier.value;
@@ -2553,13 +2581,17 @@ static void R_Shadow_BounceGrid_GenerateSettings(r_shadow_bouncegrid_settings_t
        settings->lightradiusscale              = (s ? r_shadow_bouncegrid_static_lightradiusscale.value : r_shadow_bouncegrid_dynamic_lightradiusscale.value);
        settings->maxbounce                     = (s ? r_shadow_bouncegrid_static_maxbounce.integer : r_shadow_bouncegrid_dynamic_maxbounce.integer);
        settings->particlebounceintensity       = r_shadow_bouncegrid_particlebounceintensity.value;
-       settings->particleintensity             = r_shadow_bouncegrid_particleintensity.value * 16384.0f * (settings->directionalshading ? 4.0f : 1.0f) / (spacing * spacing);
+       settings->particleintensity             = r_shadow_bouncegrid_particleintensity.value * (settings->directionalshading ? 4.0f : 1.0f) * 16384 / (spacing * spacing) / 262144.0f;
        settings->maxphotons                    = s ? r_shadow_bouncegrid_static_maxphotons.integer : r_shadow_bouncegrid_dynamic_maxphotons.integer;
-       settings->energyperphoton            = s ? r_shadow_bouncegrid_static_energyperphoton.integer : r_shadow_bouncegrid_dynamic_energyperphoton.integer;
+       settings->energyperphoton               = spacing * spacing / quality;
        settings->spacing[0]                    = spacing;
        settings->spacing[1]                    = spacing;
        settings->spacing[2]                    = spacing;
-       settings->stablerandom                  = s ? 1 : r_shadow_bouncegrid_dynamic_stablerandom.integer;
+       settings->rng_type                      = r_shadow_bouncegrid_rng_type.integer;
+       settings->rng_seed                      = r_shadow_bouncegrid_rng_seed.integer;
+       settings->bounceminimumintensity2       = bounceminimumintensity * bounceminimumintensity;
+       settings->bounceminimumintensity2       = bounceminimumintensity * bounceminimumintensity;
+       settings->normalizevectors              = r_shadow_bouncegrid_normalizevectors.integer != 0;
 
        // bound the values for sanity
        settings->maxphotons = bound(1, settings->maxphotons, 25000000);
@@ -2594,8 +2626,54 @@ static void R_Shadow_BounceGrid_UpdateSpacing(void)
        // calculate texture size enclosing entire world bounds at the spacing
        if (r_refdef.scene.worldmodel)
        {
-               VectorMA(r_refdef.scene.worldmodel->normalmins, -2.0f, spacing, mins);
-               VectorMA(r_refdef.scene.worldmodel->normalmaxs, 2.0f, spacing, maxs);
+               int lightindex;
+               int range;
+               qboolean bounds_set = false;
+               dlight_t *light;
+               rtlight_t *rtlight;
+
+               // calculate bounds enclosing world lights as they should be noticably tighter 
+               // than the world bounds on maps with unlit monster containers (see e1m7 etc)
+               range = (unsigned int)Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray); // checked
+               for (lightindex = 0;lightindex < range;lightindex++)
+               {
+                       const vec_t *rtlmins, *rtlmaxs;
+
+                       light = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
+                       if (!light)
+                               continue;
+
+                       rtlight = &light->rtlight;
+                       rtlmins = rtlight->cullmins;
+                       rtlmaxs = rtlight->cullmaxs;
+
+                       if (!bounds_set)
+                       {
+                               VectorCopy(rtlmins, mins);
+                               VectorCopy(rtlmaxs, maxs);
+                               bounds_set = true;
+                       }
+                       else
+                       {
+                               mins[0] = min(mins[0], rtlmins[0]);
+                               mins[1] = min(mins[1], rtlmins[1]);
+                               mins[2] = min(mins[2], rtlmins[2]);
+                               maxs[0] = max(maxs[0], rtlmaxs[0]);
+                               maxs[1] = max(maxs[1], rtlmaxs[1]);
+                               maxs[2] = max(maxs[2], rtlmaxs[2]);
+                       }
+               }
+
+               // limit to no larger than the world bounds
+               mins[0] = max(mins[0], r_refdef.scene.worldmodel->normalmins[0]);
+               mins[1] = max(mins[1], r_refdef.scene.worldmodel->normalmins[1]);
+               mins[2] = max(mins[2], r_refdef.scene.worldmodel->normalmins[2]);
+               maxs[0] = min(maxs[0], r_refdef.scene.worldmodel->normalmaxs[0]);
+               maxs[1] = min(maxs[1], r_refdef.scene.worldmodel->normalmaxs[1]);
+               maxs[2] = min(maxs[2], r_refdef.scene.worldmodel->normalmaxs[2]);
+
+               VectorMA(mins, -2.0f, spacing, mins);
+               VectorMA(maxs, 2.0f, spacing, maxs);
        }
        else
        {
@@ -2680,11 +2758,14 @@ static void R_Shadow_BounceGrid_UpdateSpacing(void)
        numpixels = r_shadow_bouncegrid_state.pixelsperband*r_shadow_bouncegrid_state.pixelbands;
        if (r_shadow_bouncegrid_state.numpixels != numpixels)
        {
-               if (r_shadow_bouncegrid_state.texture)
-               {
-                       R_FreeTexture(r_shadow_bouncegrid_state.texture);
-                       r_shadow_bouncegrid_state.texture = NULL;
-               }
+               if (r_shadow_bouncegrid_state.texture) R_FreeTexture(r_shadow_bouncegrid_state.texture);r_shadow_bouncegrid_state.texture = NULL;
+               r_shadow_bouncegrid_state.highpixels = NULL;
+               if (r_shadow_bouncegrid_state.blurpixels[0]) Mem_Free(r_shadow_bouncegrid_state.blurpixels[0]); r_shadow_bouncegrid_state.blurpixels[0] = NULL;
+               if (r_shadow_bouncegrid_state.blurpixels[1]) Mem_Free(r_shadow_bouncegrid_state.blurpixels[1]); r_shadow_bouncegrid_state.blurpixels[1] = NULL;
+               if (r_shadow_bouncegrid_state.u8pixels) Mem_Free(r_shadow_bouncegrid_state.u8pixels); r_shadow_bouncegrid_state.u8pixels = NULL;
+               if (r_shadow_bouncegrid_state.fp16pixels) Mem_Free(r_shadow_bouncegrid_state.fp16pixels); r_shadow_bouncegrid_state.fp16pixels = NULL;
+               if (r_shadow_bouncegrid_state.splatpaths) Mem_Free(r_shadow_bouncegrid_state.splatpaths); r_shadow_bouncegrid_state.splatpaths = NULL;
+               r_shadow_bouncegrid_state.maxsplatpaths = 0;
                r_shadow_bouncegrid_state.numpixels = numpixels;
        }
 
@@ -2700,17 +2781,16 @@ static void R_Shadow_BounceGrid_UpdateSpacing(void)
        Matrix4x4_FromArrayFloatD3D(&r_shadow_bouncegrid_state.matrix, m);
 }
 
-#define MAXBOUNCEGRIDPARTICLESPERLIGHT 1048576
-
 // enumerate world rtlights and sum the overall amount of light in the world,
 // from that we can calculate a scaling factor to fairly distribute photons
 // to all the lights
 //
 // this modifies rtlight->photoncolor and rtlight->photons
-static void R_Shadow_BounceGrid_AssignPhotons(r_shadow_bouncegrid_settings_t *settings, unsigned int range, unsigned int range1, unsigned int range2, int flag, float *photonscaling)
+static void R_Shadow_BounceGrid_AssignPhotons(r_shadow_bouncegrid_settings_t *settings, unsigned int range, unsigned int range1, unsigned int range2, int flag)
 {
        float normalphotonscaling;
-       float maxphotonscaling;
+       float photonscaling;
+       float photonintensity;
        float photoncount = 0.0f;
        float lightintensity;
        float radius;
@@ -2721,16 +2801,20 @@ static void R_Shadow_BounceGrid_AssignPhotons(r_shadow_bouncegrid_settings_t *se
        unsigned int lightindex;
        dlight_t *light;
        rtlight_t *rtlight;
+       normalphotonscaling = 1.0f / max(0.0001f, settings->energyperphoton);
        for (lightindex = 0;lightindex < range2;lightindex++)
        {
                if (lightindex < range)
                {
-                       light = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
+                       light = (dlight_t *)Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
                        if (!light)
                                continue;
                        rtlight = &light->rtlight;
-                       VectorClear(rtlight->photoncolor);
-                       rtlight->photons = 0;
+                       VectorClear(rtlight->bouncegrid_photoncolor);
+                       rtlight->bouncegrid_photons = 0;
+                       rtlight->bouncegrid_hits = 0;
+                       rtlight->bouncegrid_traces = 0;
+                       rtlight->bouncegrid_effectiveradius = 0;
                        if (!(light->flags & flag))
                                continue;
                        if (settings->staticmode)
@@ -2739,12 +2823,17 @@ static void R_Shadow_BounceGrid_AssignPhotons(r_shadow_bouncegrid_settings_t *se
                                if (rtlight->style > 0 && r_shadow_bouncegrid.integer != 2)
                                        continue;
                        }
+                       else if (r_shadow_debuglight.integer >= 0 && (int)lightindex != r_shadow_debuglight.integer)
+                               continue;
                }
                else
                {
                        rtlight = r_refdef.scene.lights[lightindex - range];
-                       VectorClear(rtlight->photoncolor);
-                       rtlight->photons = 0;
+                       VectorClear(rtlight->bouncegrid_photoncolor);
+                       rtlight->bouncegrid_photons = 0;
+                       rtlight->bouncegrid_hits = 0;
+                       rtlight->bouncegrid_traces = 0;
+                       rtlight->bouncegrid_effectiveradius = 0;
                }
                // draw only visible lights (major speedup)
                radius = rtlight->radius * settings->lightradiusscale;
@@ -2771,9 +2860,9 @@ static void R_Shadow_BounceGrid_AssignPhotons(r_shadow_bouncegrid_settings_t *se
                if (rtlight->radius == 0.0f || VectorLength2(rtlight->color) == 0.0f)
                        continue;
                w *= ((rtlight->style >= 0 && rtlight->style < MAX_LIGHTSTYLES) ? r_refdef.scene.rtlightstylevalue[rtlight->style] : 1);
-               VectorScale(rtlight->color, w, rtlight->photoncolor);
+               VectorScale(rtlight->color, w, rtlight->bouncegrid_photoncolor);
                // skip lights that will emit no photons
-               if (!VectorLength2(rtlight->photoncolor))
+               if (!VectorLength2(rtlight->bouncegrid_photoncolor))
                        continue;
                // shoot particles from this light
                // use a calculation for the number of particles that will not
@@ -2784,20 +2873,41 @@ static void R_Shadow_BounceGrid_AssignPhotons(r_shadow_bouncegrid_settings_t *se
                lightintensity = VectorLength(rtlight->color) * (rtlight->ambientscale + rtlight->diffusescale + rtlight->specularscale);
                if (lightindex >= range)
                        lightintensity *= settings->dlightparticlemultiplier;
-               rtlight->photons = bound(0.0f, lightintensity * s * s, MAXBOUNCEGRIDPARTICLESPERLIGHT);
-               photoncount += rtlight->photons;
+               rtlight->bouncegrid_photons = lightintensity * s * s * normalphotonscaling;
+               photoncount += rtlight->bouncegrid_photons;
+               VectorScale(rtlight->bouncegrid_photoncolor, settings->particleintensity * settings->energyperphoton, rtlight->bouncegrid_photoncolor);
                // if the lightstyle happens to be off right now, we can skip actually
                // firing the photons, but we did have to count them in the total.
                //if (VectorLength2(rtlight->photoncolor) == 0.0f)
-               //      rtlight->photons = 0;
+               //      rtlight->bouncegrid_photons = 0;
        }
        // the user provided an energyperphoton value which we try to use
        // if that results in too many photons to shoot this frame, then we cap it
        // which causes photons to appear/disappear from frame to frame, so we don't
        // like doing that in the typical case
-       normalphotonscaling = 1.0f / max(0.0001f, settings->energyperphoton);
-       maxphotonscaling = (float)settings->maxphotons / max(1, photoncount);
-       *photonscaling = min(normalphotonscaling, maxphotonscaling);
+       photonscaling = 1.0f;
+       photonintensity = 1.0f;
+       if (photoncount > settings->maxphotons)
+       {
+               photonscaling = settings->maxphotons / photoncount;
+               photonintensity = 1.0f / photonscaling;
+       }
+
+       // modify the lights to reflect our computed scaling
+       for (lightindex = 0; lightindex < range2; lightindex++)
+       {
+               if (lightindex < range)
+               {
+                       light = (dlight_t *)Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
+                       if (!light)
+                               continue;
+                       rtlight = &light->rtlight;
+               }
+               else
+                       rtlight = r_refdef.scene.lights[lightindex - range];
+               rtlight->bouncegrid_photons *= photonscaling;
+               VectorScale(rtlight->bouncegrid_photoncolor, photonintensity, rtlight->bouncegrid_photoncolor);
+       }
 }
 
 static int R_Shadow_BounceGrid_SplatPathCompare(const void *pa, const void *pb)
@@ -2815,14 +2925,17 @@ static int R_Shadow_BounceGrid_SplatPathCompare(const void *pa, const void *pb)
 static void R_Shadow_BounceGrid_ClearPixels(void)
 {
        // clear the highpixels array we'll be accumulating into
-       r_shadow_bouncegrid_state.highpixels = (float *)R_FrameData_Alloc(r_shadow_bouncegrid_state.numpixels * sizeof(float[4]));
+       if (r_shadow_bouncegrid_state.blurpixels[0] == NULL)
+               r_shadow_bouncegrid_state.blurpixels[0] = (float *)Mem_Alloc(r_main_mempool, r_shadow_bouncegrid_state.numpixels * sizeof(float[4]));
+       if (r_shadow_bouncegrid_state.settings.blur && r_shadow_bouncegrid_state.blurpixels[1] == NULL)
+               r_shadow_bouncegrid_state.blurpixels[1] = (float *)Mem_Alloc(r_main_mempool, r_shadow_bouncegrid_state.numpixels * sizeof(float[4]));
+       r_shadow_bouncegrid_state.highpixels_index = 0;
+       r_shadow_bouncegrid_state.highpixels = r_shadow_bouncegrid_state.blurpixels[r_shadow_bouncegrid_state.highpixels_index];
        memset(r_shadow_bouncegrid_state.highpixels, 0, r_shadow_bouncegrid_state.numpixels * sizeof(float[4]));
 }
 
 static void R_Shadow_BounceGrid_PerformSplats(void)
 {
-       int splatsize = r_shadow_bouncegrid_state.settings.lightpathsize;
-       int splatsize1 = splatsize + 1;
        r_shadow_bouncegrid_splatpath_t *splatpaths = r_shadow_bouncegrid_state.splatpaths;
        r_shadow_bouncegrid_splatpath_t *splatpath;
        float *highpixels = r_shadow_bouncegrid_state.highpixels;
@@ -2831,12 +2944,10 @@ static void R_Shadow_BounceGrid_PerformSplats(void)
        vec3_t steppos;
        vec3_t stepdelta;
        vec3_t dir;
-       float texcorner[3];
-       float texlerp[MAXBOUNCEGRIDSPLATSIZE1][3];
+       vec_t lightpathsize_current;
+       vec_t lightpathsize_perstep;
        float splatcolor[32];
-       float boxweight = 1.0f / (splatsize * splatsize * splatsize);
        int resolution[3];
-       int tex[3];
        int pixelsperband = r_shadow_bouncegrid_state.pixelsperband;
        int pixelbands = r_shadow_bouncegrid_state.pixelbands;
        int numsteps;
@@ -2852,10 +2963,6 @@ static void R_Shadow_BounceGrid_PerformSplats(void)
        if (r_shadow_bouncegrid_sortlightpaths.integer)
                qsort(splatpaths, numsplatpaths, sizeof(*splatpaths), R_Shadow_BounceGrid_SplatPathCompare);
 
-       // the middle row/column/layer of each splat are full intensity
-       for (step = 1;step < splatsize;step++)
-               VectorSet(texlerp[step], 1.0f, 1.0f, 1.0f);
-
        splatpath = splatpaths;
        for (splatindex = 0;splatindex < numsplatpaths;splatindex++, splatpath++)
        {
@@ -2907,59 +3014,64 @@ static void R_Shadow_BounceGrid_PerformSplats(void)
                VectorCopy(splatpath->point, steppos);
                VectorCopy(splatpath->step, stepdelta);
                numsteps = splatpath->remainingsplats;
+               lightpathsize_current = splatpath->splatsize_current + 1.0f; // add 1.0 for the gradient fade around the sphere
+               lightpathsize_perstep = splatpath->splatsize_perstep;
                for (step = 0;step < numsteps;step++)
                {
-                       r_refdef.stats[r_stat_bouncegrid_splats]++;
-                       // figure out the min corner of the pixels we'll need to update
-                       texcorner[0] = steppos[0] - (splatsize1 * 0.5f);
-                       texcorner[1] = steppos[1] - (splatsize1 * 0.5f);
-                       texcorner[2] = steppos[2] - (splatsize1 * 0.5f);
-                       tex[0] = (int)floor(texcorner[0]);
-                       tex[1] = (int)floor(texcorner[1]);
-                       tex[2] = (int)floor(texcorner[2]);
-                       // only update if it is within reasonable bounds
-                       if (tex[0] >= 1
-                        && tex[1] >= 1
-                        && tex[2] >= 1
-                        && tex[0] < resolution[0] - splatsize1
-                        && tex[1] < resolution[1] - splatsize1
-                        && tex[2] < resolution[2] - splatsize1)
+                       // the middle row/column/layer of each splat are full intensity
+                       float splatmins[3];
+                       float splatmaxs[3];
+                       if (lightpathsize_current > MAXBOUNCEGRIDSPLATSIZE)
+                               lightpathsize_current = MAXBOUNCEGRIDSPLATSIZE;
+                       splatmins[0] = max(1.0f, steppos[0] - lightpathsize_current * 0.5f);
+                       splatmins[1] = max(1.0f, steppos[1] - lightpathsize_current * 0.5f);
+                       splatmins[2] = max(1.0f, steppos[2] - lightpathsize_current * 0.5f);
+                       splatmaxs[0] = min(steppos[0] + lightpathsize_current * 0.5f, resolution[0] - 1.0f);
+                       splatmaxs[1] = min(steppos[1] + lightpathsize_current * 0.5f, resolution[1] - 1.0f);
+                       splatmaxs[2] = min(steppos[2] + lightpathsize_current * 0.5f, resolution[2] - 1.0f);
+                       if (splatmaxs[0] > splatmins[0] && splatmaxs[1] > splatmins[1] && splatmaxs[2] > splatmins[2])
                        {
                                // it is within bounds...  do the real work now
-                               int xi, yi, zi;
-
-                               // calculate the antialiased box edges
-                               texlerp[splatsize][0] = texcorner[0] - tex[0];
-                               texlerp[splatsize][1] = texcorner[1] - tex[1];
-                               texlerp[splatsize][2] = texcorner[2] - tex[2];
-                               texlerp[0][0] = 1.0f - texlerp[splatsize][0];
-                               texlerp[0][1] = 1.0f - texlerp[splatsize][1];
-                               texlerp[0][2] = 1.0f - texlerp[splatsize][2];
-
+                               int xi, yi, zi, band, row;
+                               float pixelpos[3];
+                               float w;
+                               float *p;
+                               float colorscale = 1.0f / lightpathsize_current;
+                               r_refdef.stats[r_stat_bouncegrid_splats]++;
                                // accumulate light onto the pixels
-                               for (zi = 0;zi < splatsize1;zi++)
+                               for (zi = (int)floor(splatmins[2]);zi < splatmaxs[2];zi++)
                                {
-                                       for (yi = 0;yi < splatsize1;yi++)
+                                       pixelpos[2] = zi + 0.5f;
+                                       for (yi = (int)floor(splatmins[1]); yi < splatmaxs[1]; yi++)
                                        {
-                                               int index = ((tex[2]+zi)*resolution[1]+tex[1]+yi)*resolution[0]+tex[0];
-                                               for (xi = 0;xi < splatsize1;xi++, index++)
+                                               pixelpos[1] = yi + 0.5f;
+                                               row = (zi*resolution[1] + yi)*resolution[0];
+                                               for (xi = (int)floor(splatmins[0]); xi < splatmaxs[0]; xi++)
                                                {
-                                                       float w = texlerp[xi][0]*texlerp[yi][1]*texlerp[zi][2] * boxweight;
-                                                       int band = 0;
-                                                       float *p = highpixels + 4 * index + band * pixelsperband * 4;
-                                                       for (;band < pixelbands;band++, p += pixelsperband * 4)
+                                                       pixelpos[0] = xi + 0.5f;
+                                                       // simple radial antialiased sphere - linear gradient fade over 1 pixel from the edge
+                                                       w = lightpathsize_current - VectorDistance(pixelpos, steppos);
+                                                       if (w > 0.0f)
                                                        {
-                                                               // add to the pixel color
-                                                               p[0] += splatcolor[band*4+0] * w;
-                                                               p[1] += splatcolor[band*4+1] * w;
-                                                               p[2] += splatcolor[band*4+2] * w;
-                                                               p[3] += splatcolor[band*4+3] * w;
+                                                               if (w > 1.0f)
+                                                                       w = 1.0f;
+                                                               w *= colorscale;
+                                                               p = highpixels + 4 * (row + xi);
+                                                               for (band = 0; band < pixelbands; band++, p += pixelsperband * 4)
+                                                               {
+                                                                       // add to the pixel color
+                                                                       p[0] += splatcolor[band * 4 + 0] * w;
+                                                                       p[1] += splatcolor[band * 4 + 1] * w;
+                                                                       p[2] += splatcolor[band * 4 + 2] * w;
+                                                                       p[3] += splatcolor[band * 4 + 3] * w;
+                                                               }
                                                        }
                                                }
                                        }
                                }
                        }
                        VectorAdd(steppos, stepdelta, steppos);
+                       lightpathsize_current += lightpathsize_perstep;
                }
        }
 }
@@ -2998,22 +3110,29 @@ static void R_Shadow_BounceGrid_BlurPixelsInDirection(const float *inpixels, flo
 
 static void R_Shadow_BounceGrid_BlurPixels(void)
 {
-       float *highpixels = r_shadow_bouncegrid_state.highpixels;
-       float *temppixels1 = (float *)R_FrameData_Alloc(r_shadow_bouncegrid_state.numpixels * sizeof(float[4]));
-       float *temppixels2 = (float *)R_FrameData_Alloc(r_shadow_bouncegrid_state.numpixels * sizeof(float[4]));
+       float *pixels[4];
        unsigned int resolution[3];
 
-       if (!r_shadow_bouncegrid_blur.integer)
+       if (!r_shadow_bouncegrid_state.settings.blur)
                return;
        
        VectorCopy(r_shadow_bouncegrid_state.resolution, resolution);
 
+       pixels[0] = r_shadow_bouncegrid_state.blurpixels[r_shadow_bouncegrid_state.highpixels_index];
+       pixels[1] = r_shadow_bouncegrid_state.blurpixels[r_shadow_bouncegrid_state.highpixels_index ^ 1];
+       pixels[2] = r_shadow_bouncegrid_state.blurpixels[r_shadow_bouncegrid_state.highpixels_index];
+       pixels[3] = r_shadow_bouncegrid_state.blurpixels[r_shadow_bouncegrid_state.highpixels_index ^ 1];
+
        // blur on X
-       R_Shadow_BounceGrid_BlurPixelsInDirection(highpixels, temppixels1, 4);
+       R_Shadow_BounceGrid_BlurPixelsInDirection(pixels[0], pixels[1], 4);
        // blur on Y
-       R_Shadow_BounceGrid_BlurPixelsInDirection(temppixels1, temppixels2, resolution[0] * 4);
+       R_Shadow_BounceGrid_BlurPixelsInDirection(pixels[1], pixels[2], resolution[0] * 4);
        // blur on Z
-       R_Shadow_BounceGrid_BlurPixelsInDirection(temppixels2, highpixels, resolution[0] * resolution[1] * 4);
+       R_Shadow_BounceGrid_BlurPixelsInDirection(pixels[2], pixels[3], resolution[0] * resolution[1] * 4);
+
+       // toggle the state, highpixels now points to pixels[3] result
+       r_shadow_bouncegrid_state.highpixels_index ^= 1;
+       r_shadow_bouncegrid_state.highpixels = r_shadow_bouncegrid_state.blurpixels[r_shadow_bouncegrid_state.highpixels_index];
 }
 
 static void R_Shadow_BounceGrid_ConvertPixelsAndUpload(void)
@@ -3072,7 +3191,9 @@ static void R_Shadow_BounceGrid_ConvertPixelsAndUpload(void)
        switch (floatcolors)
        {
        case 0:
-               pixelsbgra8 = (unsigned char *)R_FrameData_Alloc(r_shadow_bouncegrid_state.numpixels * sizeof(unsigned char[4]));
+               if (r_shadow_bouncegrid_state.u8pixels == NULL)
+                       r_shadow_bouncegrid_state.u8pixels = (unsigned char *)Mem_Alloc(r_main_mempool, r_shadow_bouncegrid_state.numpixels * sizeof(unsigned char[4]));
+               pixelsbgra8 = r_shadow_bouncegrid_state.u8pixels;
                for (pixelband = 0;pixelband < pixelbands;pixelband++)
                {
                        if (pixelband == 1)
@@ -3124,7 +3245,9 @@ static void R_Shadow_BounceGrid_ConvertPixelsAndUpload(void)
                        r_shadow_bouncegrid_state.texture = R_LoadTexture3D(r_shadow_texturepool, "bouncegrid", resolution[0], resolution[1], resolution[2]*pixelbands, pixelsbgra8, TEXTYPE_BGRA, TEXF_CLAMP | TEXF_ALPHA | TEXF_FORCELINEAR, 0, NULL);
                break;
        case 1:
-               pixelsrgba16f = (unsigned short *)R_FrameData_Alloc(r_shadow_bouncegrid_state.numpixels * sizeof(unsigned short[4]));
+               if (r_shadow_bouncegrid_state.fp16pixels == NULL)
+                       r_shadow_bouncegrid_state.fp16pixels = (unsigned short *)Mem_Alloc(r_main_mempool, r_shadow_bouncegrid_state.numpixels * sizeof(unsigned short[4]));
+               pixelsrgba16f = r_shadow_bouncegrid_state.fp16pixels;
                memset(pixelsrgba16f, 0, r_shadow_bouncegrid_state.numpixels * sizeof(unsigned short[4]));
                for (z = 1;z < resolution[2]-1;z++)
                {
@@ -3186,19 +3309,23 @@ static void R_Shadow_BounceGrid_ConvertPixelsAndUpload(void)
        r_shadow_bouncegrid_state.lastupdatetime = realtime;
 }
 
-static void R_Shadow_BounceGrid_TracePhotons(r_shadow_bouncegrid_settings_t settings, unsigned int range, unsigned int range1, unsigned int range2, float photonscaling, int flag)
+static void R_Shadow_BounceGrid_TracePhotons(r_shadow_bouncegrid_settings_t settings, unsigned int range, unsigned int range1, unsigned int range2, int flag)
 {
+       vec3_t bouncerandom[10];
        dlight_t *light;
        int bouncecount;
        int hitsupercontentsmask;
+       int skipsupercontentsmask;
        int maxbounce;
        int shootparticles;
        int shotparticles;
+       float bounceminimumintensity2;
        trace_t cliptrace;
        //trace_t cliptrace2;
        //trace_t cliptrace3;
        unsigned int lightindex;
-       unsigned int seed = (unsigned int)(realtime * 1000.0f);
+       unsigned int seed;
+       randomseed_t randomseed;
        vec3_t shotcolor;
        vec3_t baseshotcolor;
        vec3_t surfcolor;
@@ -3206,18 +3333,22 @@ static void R_Shadow_BounceGrid_TracePhotons(r_shadow_bouncegrid_settings_t sett
        vec3_t clipstart;
        vec3_t clipdiff;
        vec_t radius;
+       vec_t distancetraveled;
        vec_t s;
        rtlight_t *rtlight;
 
-       // we'll need somewhere to store these
+       // compute a seed for the unstable random modes
+       Math_RandomSeed_FromInts(&randomseed, 0, 0, 0, realtime * 1000.0);
+       seed = realtime * 1000.0;
+
        r_shadow_bouncegrid_state.numsplatpaths = 0;
-       r_shadow_bouncegrid_state.splatpaths = (r_shadow_bouncegrid_splatpath_t *)R_FrameData_Alloc(sizeof(r_shadow_bouncegrid_splatpath_t) * r_shadow_bouncegrid_state.maxsplatpaths);
 
        // figure out what we want to interact with
        if (settings.hitmodels)
                hitsupercontentsmask = SUPERCONTENTS_SOLID | SUPERCONTENTS_BODY;// | SUPERCONTENTS_LIQUIDSMASK;
        else
                hitsupercontentsmask = SUPERCONTENTS_SOLID;// | SUPERCONTENTS_LIQUIDSMASK;
+       skipsupercontentsmask = SUPERCONTENTS_SKY; // this allows the e1m5 sky shadow to work by ignoring the sky surfaces
        maxbounce = settings.maxbounce;
 
        for (lightindex = 0;lightindex < range2;lightindex++)
@@ -3234,50 +3365,111 @@ static void R_Shadow_BounceGrid_TracePhotons(r_shadow_bouncegrid_settings_t sett
                // note that this code used to keep track of residual photons and
                // distribute them evenly to achieve exactly a desired photon count,
                // but that caused unwanted flickering in dynamic mode
-               shootparticles = (int)floor(rtlight->photons * photonscaling);
+               shootparticles = (int)floor(rtlight->bouncegrid_photons);
                // skip if we won't be shooting any photons
                if (!shootparticles)
                        continue;
                radius = rtlight->radius * settings.lightradiusscale;
-               s = settings.particleintensity / shootparticles;
-               VectorScale(rtlight->photoncolor, s, baseshotcolor);
+               //s = settings.particleintensity / shootparticles;
+               //VectorScale(rtlight->bouncegrid_photoncolor, s, baseshotcolor);
+               VectorCopy(rtlight->bouncegrid_photoncolor, baseshotcolor);
+               if (VectorLength2(baseshotcolor) <= 0.0f)
+                       continue;
                r_refdef.stats[r_stat_bouncegrid_lights]++;
                r_refdef.stats[r_stat_bouncegrid_particles] += shootparticles;
+               // we stop caring about bounces once the brightness goes below this fraction of the original intensity
+               bounceminimumintensity2 = VectorLength(baseshotcolor) * settings.bounceminimumintensity2;
+
+               // for seeded random we start the RNG with the position of the light
+               if (settings.rng_seed >= 0)
+               {
+                       union
+                       {
+                               unsigned int i[4];
+                               float f[4];
+                       }
+                       u;
+                       u.f[0] = rtlight->shadoworigin[0];
+                       u.f[1] = rtlight->shadoworigin[1];
+                       u.f[2] = rtlight->shadoworigin[2];
+                       u.f[3] = 1;
+                       switch (settings.rng_type)
+                       {
+                       default:
+                       case 0:
+                               // we have to shift the seed provided by the user because the result must be odd
+                               Math_RandomSeed_FromInts(&randomseed, u.i[0], u.i[1], u.i[2], u.i[3] ^ (settings.rng_seed << 1));
+                               break;
+                       case 1:
+                               seed = u.i[0] ^ u.i[1] ^ u.i[2] ^ u.i[3] ^ settings.rng_seed;
+                               break;
+                       }
+               }
+
                for (shotparticles = 0;shotparticles < shootparticles;shotparticles++)
                {
-                       if (settings.stablerandom > 0)
-                               seed = lightindex * 11937 + shotparticles;
                        VectorCopy(baseshotcolor, shotcolor);
                        VectorCopy(rtlight->shadoworigin, clipstart);
-                       if (settings.stablerandom < 0)
-                               VectorRandom(clipend);
-                       else
-                               VectorCheeseRandom(clipend);
+                       switch (settings.rng_type)
+                       {
+                       default:
+                       case 0:
+                               VectorLehmerRandom(&randomseed, clipend);
+                               if (settings.bounceanglediffuse)
+                               {
+                                       // we want random to be stable, so we still have to do all the random we would have done
+                                       for (bouncecount = 0; bouncecount < maxbounce; bouncecount++)
+                                               VectorLehmerRandom(&randomseed, bouncerandom[bouncecount]);
+                               }
+                               break;
+                       case 1:
+                               VectorCheeseRandom(seed, clipend);
+                               if (settings.bounceanglediffuse)
+                               {
+                                       // we want random to be stable, so we still have to do all the random we would have done
+                                       for (bouncecount = 0; bouncecount < maxbounce; bouncecount++)
+                                               VectorCheeseRandom(seed, bouncerandom[bouncecount]);
+                               }
+                               break;
+                       }
+
+                       // we want a uniform distribution spherically, not merely within the sphere
+                       if (settings.normalizevectors)
+                               VectorNormalize(clipend);
+
                        VectorMA(clipstart, radius, clipend, clipend);
+                       distancetraveled = 0.0f;
                        for (bouncecount = 0;;bouncecount++)
                        {
                                r_refdef.stats[r_stat_bouncegrid_traces]++;
+                               rtlight->bouncegrid_traces++;
                                //r_refdef.scene.worldmodel->TraceLineAgainstSurfaces(r_refdef.scene.worldmodel, NULL, NULL, &cliptrace, clipstart, clipend, hitsupercontentsmask);
                                //r_refdef.scene.worldmodel->TraceLine(r_refdef.scene.worldmodel, NULL, NULL, &cliptrace2, clipstart, clipend, hitsupercontentsmask);
-                               if (settings.staticmode)
+                               if (settings.staticmode || settings.rng_seed < 0)
                                {
                                        // static mode fires a LOT of rays but none of them are identical, so they are not cached
-                                       cliptrace = CL_TraceLine(clipstart, clipend, settings.staticmode ? MOVE_WORLDONLY : (settings.hitmodels ? MOVE_HITMODEL : MOVE_NOMONSTERS), NULL, hitsupercontentsmask, collision_extendmovelength.value, true, false, NULL, true, true);
+                                       // non-stable random in dynamic mode also never reuses a direction, so there's no reason to cache it
+                                       cliptrace = CL_TraceLine(clipstart, clipend, settings.staticmode ? MOVE_WORLDONLY : (settings.hitmodels ? MOVE_HITMODEL : MOVE_NOMONSTERS), NULL, hitsupercontentsmask, skipsupercontentsmask, collision_extendmovelength.value, true, false, NULL, true, true);
                                }
                                else
                                {
                                        // dynamic mode fires many rays and most will match the cache from the previous frame
-                                       cliptrace = CL_Cache_TraceLineSurfaces(clipstart, clipend, settings.staticmode ? MOVE_WORLDONLY : (settings.hitmodels ? MOVE_HITMODEL : MOVE_NOMONSTERS), hitsupercontentsmask);
+                                       cliptrace = CL_Cache_TraceLineSurfaces(clipstart, clipend, settings.staticmode ? MOVE_WORLDONLY : (settings.hitmodels ? MOVE_HITMODEL : MOVE_NOMONSTERS), hitsupercontentsmask, skipsupercontentsmask);
                                }
                                if (bouncecount > 0 || settings.includedirectlighting)
                                {
                                        vec3_t hitpos;
                                        VectorCopy(cliptrace.endpos, hitpos);
-                                       R_Shadow_BounceGrid_AddSplatPath(clipstart, hitpos, shotcolor);
+                                       R_Shadow_BounceGrid_AddSplatPath(clipstart, hitpos, shotcolor, distancetraveled);
                                }
+                               distancetraveled += VectorDistance(clipstart, cliptrace.endpos);
+                               s = VectorDistance(rtlight->shadoworigin, cliptrace.endpos);
+                               if (rtlight->bouncegrid_effectiveradius < s)
+                                       rtlight->bouncegrid_effectiveradius = s;
                                if (cliptrace.fraction >= 1.0f)
                                        break;
                                r_refdef.stats[r_stat_bouncegrid_hits]++;
+                               rtlight->bouncegrid_hits++;
                                if (bouncecount >= maxbounce)
                                        break;
                                // scale down shot color by bounce intensity and texture color (or 50% if no texture reported)
@@ -3291,18 +3483,14 @@ static void R_Shadow_BounceGrid_TracePhotons(r_shadow_bouncegrid_settings_t sett
                                surfcolor[1] = min(surfcolor[1], 1.0f);
                                surfcolor[2] = min(surfcolor[2], 1.0f);
                                VectorMultiply(shotcolor, surfcolor, shotcolor);
-                               if (VectorLength2(baseshotcolor) == 0.0f)
+                               if (VectorLength2(shotcolor) <= bounceminimumintensity2)
                                        break;
                                r_refdef.stats[r_stat_bouncegrid_bounces]++;
                                if (settings.bounceanglediffuse)
                                {
                                        // random direction, primarily along plane normal
                                        s = VectorDistance(cliptrace.endpos, clipend);
-                                       if (settings.stablerandom < 0)
-                                               VectorRandom(clipend);
-                                       else
-                                               VectorCheeseRandom(clipend);
-                                       VectorMA(cliptrace.plane.normal, 0.95f, clipend, clipend);
+                                       VectorMA(cliptrace.plane.normal, 0.95f, bouncerandom[bouncecount], clipend);
                                        VectorNormalize(clipend);
                                        VectorScale(clipend, s, clipend);
                                }
@@ -3329,7 +3517,6 @@ void R_Shadow_UpdateBounceGridTexture(void)
        unsigned int range; // number of world lights
        unsigned int range1; // number of dynamic lights (or zero if disabled)
        unsigned int range2; // range+range1
-       float photonscaling;
 
        enable = R_Shadow_BounceGrid_CheckEnable(flag);
        
@@ -3349,6 +3536,13 @@ void R_Shadow_UpdateBounceGridTexture(void)
                        R_FreeTexture(r_shadow_bouncegrid_state.texture);
                        r_shadow_bouncegrid_state.texture = NULL;
                }
+               r_shadow_bouncegrid_state.highpixels = NULL;
+               if (r_shadow_bouncegrid_state.blurpixels[0]) Mem_Free(r_shadow_bouncegrid_state.blurpixels[0]); r_shadow_bouncegrid_state.blurpixels[0] = NULL;
+               if (r_shadow_bouncegrid_state.blurpixels[1]) Mem_Free(r_shadow_bouncegrid_state.blurpixels[1]); r_shadow_bouncegrid_state.blurpixels[1] = NULL;
+               if (r_shadow_bouncegrid_state.u8pixels) Mem_Free(r_shadow_bouncegrid_state.u8pixels); r_shadow_bouncegrid_state.u8pixels = NULL;
+               if (r_shadow_bouncegrid_state.fp16pixels) Mem_Free(r_shadow_bouncegrid_state.fp16pixels); r_shadow_bouncegrid_state.fp16pixels = NULL;
+               if (r_shadow_bouncegrid_state.splatpaths) Mem_Free(r_shadow_bouncegrid_state.splatpaths); r_shadow_bouncegrid_state.splatpaths = NULL;
+               r_shadow_bouncegrid_state.maxsplatpaths = 0;
                r_shadow_bouncegrid_state.numpixels = 0;
                r_shadow_bouncegrid_state.directional = false;
 
@@ -3374,10 +3568,10 @@ void R_Shadow_UpdateBounceGridTexture(void)
        range2 = range + range1;
 
        // calculate weighting factors for distributing photons among the lights
-       R_Shadow_BounceGrid_AssignPhotons(&settings, range, range1, range2, flag, &photonscaling);
+       R_Shadow_BounceGrid_AssignPhotons(&settings, range, range1, range2, flag);
 
        // trace the photons from lights and accumulate illumination
-       R_Shadow_BounceGrid_TracePhotons(settings, range, range1, range2, photonscaling, flag);
+       R_Shadow_BounceGrid_TracePhotons(settings, range, range1, range2, flag);
 
        // clear the texture
        R_Shadow_BounceGrid_ClearPixels();
@@ -3390,6 +3584,18 @@ void R_Shadow_UpdateBounceGridTexture(void)
 
        // convert the pixels to lower precision and upload the texture
        R_Shadow_BounceGrid_ConvertPixelsAndUpload();
+
+       // after we compute the static lighting we don't need to keep the highpixels array around
+       if (settings.staticmode)
+       {
+               r_shadow_bouncegrid_state.highpixels = NULL;
+               if (r_shadow_bouncegrid_state.blurpixels[0]) Mem_Free(r_shadow_bouncegrid_state.blurpixels[0]); r_shadow_bouncegrid_state.blurpixels[0] = NULL;
+               if (r_shadow_bouncegrid_state.blurpixels[1]) Mem_Free(r_shadow_bouncegrid_state.blurpixels[1]); r_shadow_bouncegrid_state.blurpixels[1] = NULL;
+               if (r_shadow_bouncegrid_state.u8pixels) Mem_Free(r_shadow_bouncegrid_state.u8pixels); r_shadow_bouncegrid_state.u8pixels = NULL;
+               if (r_shadow_bouncegrid_state.fp16pixels) Mem_Free(r_shadow_bouncegrid_state.fp16pixels); r_shadow_bouncegrid_state.fp16pixels = NULL;
+               if (r_shadow_bouncegrid_state.splatpaths) Mem_Free(r_shadow_bouncegrid_state.splatpaths); r_shadow_bouncegrid_state.splatpaths = NULL;
+               r_shadow_bouncegrid_state.maxsplatpaths = 0;
+       }
 }
 
 void R_Shadow_RenderMode_VisibleShadowVolumes(void)
@@ -3932,7 +4138,7 @@ void R_RTLight_Compile(rtlight_t *rtlight)
                // this variable must be set for the CompileShadowVolume/CompileShadowMap code
                r_shadow_compilingrtlight = rtlight;
                R_FrameData_SetMark();
-               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);
+               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, rtlight->shadow == 0);
                R_FrameData_ReturnToMark();
                numleafpvsbytes = (model->brush.num_leafs + 7) >> 3;
                numshadowtrispvsbytes = ((model->brush.shadowmesh ? model->brush.shadowmesh->numtriangles : model->surfmesh.num_triangles) + 7) >> 3;
@@ -4488,7 +4694,7 @@ static void R_Shadow_PrepareLight(rtlight_t *rtlight)
        {
                // dynamic light, world available and can receive realtime lighting
                // calculate lit surfaces and leafs
-               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_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, rtlight->shadow == 0);
                R_Shadow_ComputeShadowCasterCullingPlanes(rtlight);
                leaflist = r_shadow_buffer_leaflist;
                leafpvs = r_shadow_buffer_leafpvs;
@@ -5440,7 +5646,7 @@ static void R_Shadow_DrawModelShadowMaps(void)
        VectorAdd(shadoworigin, r_refdef.view.origin, shadoworigin);
        dot1 = DotProduct(r_refdef.view.forward, shadowdir);
        dot2 = DotProduct(r_refdef.view.up, shadowdir);
-       if (fabs(dot1) <= fabs(dot2)) 
+       if (fabs(dot1) <= fabs(dot2))
                VectorMA(r_refdef.view.forward, -dot1, shadowdir, shadowforward);
        else
                VectorMA(r_refdef.view.up, -dot2, shadowdir, shadowforward);
@@ -5500,7 +5706,7 @@ static void R_Shadow_DrawModelShadowMaps(void)
 
        Matrix4x4_Concat(&mvpmatrix, &r_refdef.view.viewport.projectmatrix, &r_refdef.view.viewport.viewmatrix);
        Matrix4x4_Invert_Full(&invmvpmatrix, &mvpmatrix);
-       Matrix4x4_CreateScale3(&scalematrix, size, -size, 1); 
+       Matrix4x4_CreateScale3(&scalematrix, size, -size, 1);
        Matrix4x4_AdjustOrigin(&scalematrix, 0, size, -0.5f * bias);
        Matrix4x4_Concat(&texmatrix, &scalematrix, &shadowmatrix);
        Matrix4x4_Concat(&r_shadow_shadowmapmatrix, &texmatrix, &invmvpmatrix);
@@ -5729,13 +5935,10 @@ static void R_DrawCorona(rtlight_t *rtlight, float cscale, float scale)
        {
                switch(vid.renderpath)
                {
-               case RENDERPATH_GL11:
-               case RENDERPATH_GL13:
                case RENDERPATH_GL20:
                case RENDERPATH_GLES1:
                case RENDERPATH_GLES2:
 #if defined(GL_SAMPLES_PASSED_ARB) && !defined(USE_GLES2)
-                       CHECKGLERROR
                        // See if we can use the GPU-side method to prevent implicit sync
                        if (vid.support.arb_query_buffer_object) {
 #define BUFFER_OFFSET(i)    ((GLint *)((unsigned char*)NULL + (i)))
@@ -5750,13 +5953,23 @@ static void R_DrawCorona(rtlight_t *rtlight, float cscale, float scale)
                                qglGetQueryObjectivARB(rtlight->corona_queryindex_allpixels, GL_QUERY_RESULT_ARB, BUFFER_OFFSET(4));
                                qglBindBufferBase(GL_UNIFORM_BUFFER, 0, r_shadow_occlusion_buf);
                                occlude = MATERIALFLAG_OCCLUDE;
-                       } else {
-                               qglGetQueryObjectivARB(rtlight->corona_queryindex_visiblepixels, GL_QUERY_RESULT_ARB, &visiblepixels);
-                               qglGetQueryObjectivARB(rtlight->corona_queryindex_allpixels, GL_QUERY_RESULT_ARB, &allpixels); 
-                               if (visiblepixels < 1 || allpixels < 1)
-                                       return;
-                               rtlight->corona_visibility *= bound(0, (float)visiblepixels / (float)allpixels, 1);
+                               cscale *= rtlight->corona_visibility;
+                               CHECKGLERROR
+                               break;
                        }
+                       // fallthrough
+#else
+                       return;
+#endif
+               case RENDERPATH_GL11:
+               case RENDERPATH_GL13:
+#if defined(GL_SAMPLES_PASSED_ARB) && !defined(USE_GLES2)
+                       CHECKGLERROR
+                       qglGetQueryObjectivARB(rtlight->corona_queryindex_visiblepixels, GL_QUERY_RESULT_ARB, &visiblepixels);
+                       qglGetQueryObjectivARB(rtlight->corona_queryindex_allpixels, GL_QUERY_RESULT_ARB, &allpixels);
+                       if (visiblepixels < 1 || allpixels < 1)
+                               return;
+                       rtlight->corona_visibility *= bound(0, (float)visiblepixels / (float)allpixels, 1);
                        cscale *= rtlight->corona_visibility;
                        CHECKGLERROR
                        break;
@@ -5782,7 +5995,7 @@ static void R_DrawCorona(rtlight_t *rtlight, float cscale, float scale)
        else
        {
                // FIXME: these traces should scan all render entities instead of cl.world
-               if (CL_TraceLine(r_refdef.view.origin, rtlight->shadoworigin, MOVE_NOMONSTERS, NULL, SUPERCONTENTS_SOLID, collision_extendmovelength.value, true, false, NULL, false, true).fraction < 1)
+               if (CL_TraceLine(r_refdef.view.origin, rtlight->shadoworigin, MOVE_NOMONSTERS, NULL, SUPERCONTENTS_SOLID, SUPERCONTENTS_SKY, collision_extendmovelength.value, true, false, NULL, false, true).fraction < 1)
                        return;
        }
        VectorScale(rtlight->currentcolor, cscale, color);
@@ -6115,7 +6328,7 @@ static void R_Shadow_SelectLightInView(void)
                if (rating >= 0.95)
                {
                        rating /= (1 + 0.0625f * sqrt(DotProduct(temp, temp)));
-                       if (bestrating < rating && CL_TraceLine(light->origin, r_refdef.view.origin, MOVE_NOMONSTERS, NULL, SUPERCONTENTS_SOLID, collision_extendmovelength.value, true, false, NULL, false, true).fraction == 1.0f)
+                       if (bestrating < rating && CL_TraceLine(light->origin, r_refdef.view.origin, MOVE_NOMONSTERS, NULL, SUPERCONTENTS_SOLID, SUPERCONTENTS_SKY, collision_extendmovelength.value, true, false, NULL, false, true).fraction == 1.0f)
                        {
                                bestrating = rating;
                                best = light;
@@ -6565,7 +6778,7 @@ static void R_Shadow_SetCursorLocationForView(void)
        vec3_t dest, endpos;
        trace_t trace;
        VectorMA(r_refdef.view.origin, r_editlights_cursordistance.value, r_refdef.view.forward, dest);
-       trace = CL_TraceLine(r_refdef.view.origin, dest, MOVE_NOMONSTERS, NULL, SUPERCONTENTS_SOLID, collision_extendmovelength.value, true, false, NULL, false, true);
+       trace = CL_TraceLine(r_refdef.view.origin, dest, MOVE_NOMONSTERS, NULL, SUPERCONTENTS_SOLID, SUPERCONTENTS_SKY, collision_extendmovelength.value, true, false, NULL, false, true);
        if (trace.fraction < 1)
        {
                dist = trace.fraction * r_editlights_cursordistance.value;
@@ -7045,9 +7258,9 @@ void R_Shadow_EditLights_DrawSelectedLightProperties(void)
        // draw properties on screen
        if (!r_editlights_drawproperties.integer)
                return;
-       x = vid_conwidth.value - 240;
+       x = vid_conwidth.value - 320;
        y = 5;
-       DrawQ_Pic(x-5, y-5, NULL, 250, 155, 0, 0, 0, 0.75, 0);
+       DrawQ_Pic(x-5, y-5, NULL, 250, 243, 0, 0, 0, 0.75, 0);
        lightnumber = -1;
        lightcount = 0;
        range = Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray); // checked
@@ -7080,6 +7293,17 @@ void R_Shadow_EditLights_DrawSelectedLightProperties(void)
        dpsnprintf(temp, sizeof(temp), "Specular     : %.2f\n", r_shadow_selectedlight->specularscale);DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0, NULL, true, FONT_DEFAULT);y += 8;
        dpsnprintf(temp, sizeof(temp), "NormalMode   : %s\n", (r_shadow_selectedlight->flags & LIGHTFLAG_NORMALMODE) ? "yes" : "no");DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0, NULL, true, FONT_DEFAULT);y += 8;
        dpsnprintf(temp, sizeof(temp), "RealTimeMode : %s\n", (r_shadow_selectedlight->flags & LIGHTFLAG_REALTIMEMODE) ? "yes" : "no");DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0, NULL, true, FONT_DEFAULT);y += 8;
+       y += 8;
+       dpsnprintf(temp, sizeof(temp), "Render stats\n"); DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0, NULL, true, FONT_DEFAULT); y += 8;
+       dpsnprintf(temp, sizeof(temp), "Current color: %.3f %.3f %.3f\n", r_shadow_selectedlight->rtlight.currentcolor[0], r_shadow_selectedlight->rtlight.currentcolor[1], r_shadow_selectedlight->rtlight.currentcolor[2]); DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0, NULL, true, FONT_DEFAULT); y += 8;
+       dpsnprintf(temp, sizeof(temp), "Shadow size  : %ix%ix6\n", r_shadow_selectedlight->rtlight.shadowmapatlassidesize, r_shadow_selectedlight->rtlight.shadowmapatlassidesize); DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0, NULL, true, FONT_DEFAULT); y += 8;
+       dpsnprintf(temp, sizeof(temp), "World surfs  : %i\n", r_shadow_selectedlight->rtlight.cached_numsurfaces); DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0, NULL, true, FONT_DEFAULT); y += 8;
+       dpsnprintf(temp, sizeof(temp), "Shadow ents  : %i + %i noself\n", r_shadow_selectedlight->rtlight.cached_numshadowentities, r_shadow_selectedlight->rtlight.cached_numshadowentities_noselfshadow); DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0, NULL, true, FONT_DEFAULT); y += 8;
+       dpsnprintf(temp, sizeof(temp), "Lit ents     : %i + %i noself\n", r_shadow_selectedlight->rtlight.cached_numlightentities, r_shadow_selectedlight->rtlight.cached_numlightentities_noselfshadow); DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0, NULL, true, FONT_DEFAULT); y += 8;
+       dpsnprintf(temp, sizeof(temp), "BG photons   : %.3f\n", r_shadow_selectedlight->rtlight.bouncegrid_photons); DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0, NULL, true, FONT_DEFAULT); y += 8;
+       dpsnprintf(temp, sizeof(temp), "BG radius    : %.0f\n", r_shadow_selectedlight->rtlight.bouncegrid_effectiveradius); DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0, NULL, true, FONT_DEFAULT); y += 8;
+       dpsnprintf(temp, sizeof(temp), "BG color     : %.3f %.3f %.3f\n", r_shadow_selectedlight->rtlight.bouncegrid_photoncolor[0], r_shadow_selectedlight->rtlight.bouncegrid_photoncolor[1], r_shadow_selectedlight->rtlight.bouncegrid_photoncolor[2]); DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0, NULL, true, FONT_DEFAULT); y += 8;
+       dpsnprintf(temp, sizeof(temp), "BG stats     : %i traces %i hits\n", r_shadow_selectedlight->rtlight.bouncegrid_traces, r_shadow_selectedlight->rtlight.bouncegrid_hits); DrawQ_String(x, y, temp, 0, 8, 8, 1, 1, 1, 1, 0, NULL, true, FONT_DEFAULT); y += 8;
 }
 
 static void R_Shadow_EditLights_ToggleShadow_f(void)
@@ -7353,7 +7577,7 @@ void R_LightPoint(float *color, const vec3_t p, const int flags)
                        if (f <= 0)
                                continue;
                        // todo: add to both ambient and diffuse
-                       if (!light->shadow || CL_TraceLine(p, light->shadoworigin, MOVE_NOMONSTERS, NULL, SUPERCONTENTS_SOLID, collision_extendmovelength.value, true, false, NULL, false, true).fraction == 1)
+                       if (!light->shadow || CL_TraceLine(p, light->shadoworigin, MOVE_NOMONSTERS, NULL, SUPERCONTENTS_SOLID, SUPERCONTENTS_SKY, collision_extendmovelength.value, true, false, NULL, false, true).fraction == 1)
                                VectorMA(color, f, light->currentcolor, color);
                }
        }
@@ -7374,7 +7598,7 @@ void R_LightPoint(float *color, const vec3_t p, const int flags)
                        if (f <= 0)
                                continue;
                        // todo: add to both ambient and diffuse
-                       if (!light->shadow || CL_TraceLine(p, light->shadoworigin, MOVE_NOMONSTERS, NULL, SUPERCONTENTS_SOLID, collision_extendmovelength.value, true, false, NULL, false, true).fraction == 1)
+                       if (!light->shadow || CL_TraceLine(p, light->shadoworigin, MOVE_NOMONSTERS, NULL, SUPERCONTENTS_SOLID, SUPERCONTENTS_SKY, collision_extendmovelength.value, true, false, NULL, false, true).fraction == 1)
                                VectorMA(color, f, light->color, color);
                }
        }
@@ -7458,7 +7682,7 @@ void R_CompleteLightPoint(vec3_t ambient, vec3_t diffuse, vec3_t lightdir, const
                        intensity = min(1.0f, (1.0f - dist) * r_shadow_lightattenuationlinearscale.value / (r_shadow_lightattenuationdividebias.value + dist*dist)) * r_shadow_lightintensityscale.value;
                        if (intensity <= 0.0f)
                                continue;
-                       if (light->shadow && CL_TraceLine(p, light->shadoworigin, MOVE_NOMONSTERS, NULL, SUPERCONTENTS_SOLID, collision_extendmovelength.value, true, false, NULL, false, true).fraction < 1)
+                       if (light->shadow && CL_TraceLine(p, light->shadoworigin, MOVE_NOMONSTERS, NULL, SUPERCONTENTS_SOLID, SUPERCONTENTS_SKY, collision_extendmovelength.value, true, false, NULL, false, true).fraction < 1)
                                continue;
                        // scale down intensity to add to both ambient and diffuse
                        //intensity *= 0.5f;
@@ -7491,7 +7715,7 @@ void R_CompleteLightPoint(vec3_t ambient, vec3_t diffuse, vec3_t lightdir, const
                        intensity = (1.0f - dist) * r_shadow_lightattenuationlinearscale.value / (r_shadow_lightattenuationdividebias.value + dist*dist) * r_shadow_lightintensityscale.value;
                        if (intensity <= 0.0f)
                                continue;
-                       if (light->shadow && CL_TraceLine(p, light->shadoworigin, MOVE_NOMONSTERS, NULL, SUPERCONTENTS_SOLID, collision_extendmovelength.value, true, false, NULL, false, true).fraction < 1)
+                       if (light->shadow && CL_TraceLine(p, light->shadoworigin, MOVE_NOMONSTERS, NULL, SUPERCONTENTS_SOLID, SUPERCONTENTS_SKY, collision_extendmovelength.value, true, false, NULL, false, true).fraction < 1)
                                continue;
                        // scale down intensity to add to both ambient and diffuse
                        //intensity *= 0.5f;