]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - r_shadow.c
Add r_shadow_culllights_trace feature, this culls rtlights by traceline checks simila...
[xonotic/darkplaces.git] / r_shadow.c
index d300d50fcf32ff4118acbce1f01ea3f469c02904..b774a5c325cbe02b68643ac989a9f0329edb0e83 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"};
@@ -334,20 +334,26 @@ cvar_t r_shadow_sortsurfaces = {0, "r_shadow_sortsurfaces", "1", "improve perfor
 cvar_t r_shadow_polygonfactor = {0, "r_shadow_polygonfactor", "0", "how much to enlarge shadow volume polygons when rendering (should be 0!)"};
 cvar_t r_shadow_polygonoffset = {0, "r_shadow_polygonoffset", "1", "how much to push shadow volumes into the distance when rendering, to reduce chances of zfighting artifacts (should not be less than 0)"};
 cvar_t r_shadow_texture3d = {0, "r_shadow_texture3d", "1", "use 3D voxel textures for spherical attenuation rather than cylindrical (does not affect OpenGL 2.0 render path)"};
+cvar_t r_shadow_culllights_pvs = {CVAR_SAVE, "r_shadow_culllights_pvs", "1", "check if light overlaps any visible bsp leafs when determining if the light is visible"};
+cvar_t r_shadow_culllights_trace = {CVAR_SAVE, "r_shadow_culllights_trace", "1", "use raytraces from the eye to random places within light bounds to determine if the light is visible"};
+cvar_t r_shadow_culllights_trace_eyejitter = {CVAR_SAVE, "r_shadow_culllights_trace_eyejitter", "16", "offset eye location randomly by this much"};
+cvar_t r_shadow_culllights_trace_enlarge = {CVAR_SAVE, "r_shadow_culllights_trace_enlarge", "0.1", "make light bounds bigger by *1.0+enlarge"};
+cvar_t r_shadow_culllights_trace_samples = {CVAR_SAVE, "r_shadow_culllights_trace_samples", "16", "use this many traces to random positions (in addition to center trace)"};
+cvar_t r_shadow_culllights_trace_tempsamples = {CVAR_SAVE, "r_shadow_culllights_trace_tempsamples", "16", "use this many traces if the light was created by csqc (no inter-frame caching), -1 disables the check (to avoid flicker entirely)"};
+cvar_t r_shadow_culllights_trace_delay = {CVAR_SAVE, "r_shadow_culllights_trace_delay", "1", "light will be considered visible for this many seconds after any trace connects"};
 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.01", "stop bouncing once intensity drops below this fraction of the original particle color" };
+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"};
@@ -355,17 +361,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"};
@@ -461,19 +471,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)
@@ -801,6 +811,13 @@ void R_Shadow_Init(void)
        Cvar_RegisterVariable(&r_shadow_polygonfactor);
        Cvar_RegisterVariable(&r_shadow_polygonoffset);
        Cvar_RegisterVariable(&r_shadow_texture3d);
+       Cvar_RegisterVariable(&r_shadow_culllights_pvs);
+       Cvar_RegisterVariable(&r_shadow_culllights_trace);
+       Cvar_RegisterVariable(&r_shadow_culllights_trace_eyejitter);
+       Cvar_RegisterVariable(&r_shadow_culllights_trace_enlarge);
+       Cvar_RegisterVariable(&r_shadow_culllights_trace_samples);
+       Cvar_RegisterVariable(&r_shadow_culllights_trace_tempsamples);
+       Cvar_RegisterVariable(&r_shadow_culllights_trace_delay);
        Cvar_RegisterVariable(&r_shadow_bouncegrid);
        Cvar_RegisterVariable(&r_shadow_bouncegrid_blur);
        Cvar_RegisterVariable(&r_shadow_bouncegrid_bounceanglediffuse);
@@ -809,12 +826,11 @@ void R_Shadow_Init(void)
        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);
@@ -822,17 +838,21 @@ 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_bounceminimumintensity);
        Cvar_RegisterVariable(&r_shadow_bouncegrid_static_directionalshading);
-       Cvar_RegisterVariable(&r_shadow_bouncegrid_static_energyperphoton);
        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_quality);
        Cvar_RegisterVariable(&r_shadow_bouncegrid_static_spacing);
        Cvar_RegisterVariable(&r_coronas);
        Cvar_RegisterVariable(&r_coronas_occlusionsizescale);
@@ -1601,7 +1621,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++)
@@ -2425,11 +2445,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;
@@ -2509,6 +2531,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;
 }
@@ -2550,17 +2574,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;
@@ -2571,12 +2597,15 @@ static void R_Shadow_BounceGrid_GenerateSettings(r_shadow_bouncegrid_settings_t
        settings->particlebounceintensity       = r_shadow_bouncegrid_particlebounceintensity.value;
        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.value : r_shadow_bouncegrid_dynamic_energyperphoton.value;
+       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);
@@ -2585,13 +2614,6 @@ static void R_Shadow_BounceGrid_GenerateSettings(r_shadow_bouncegrid_settings_t
        settings->spacing[0] = bound(1, settings->spacing[0], 512);
        settings->spacing[1] = bound(1, settings->spacing[1], 512);
        settings->spacing[2] = bound(1, settings->spacing[2], 512);
-
-       // check if the ram requirements for blur would be excessive and disable it (increase lightpathsize to compensate)
-       if (spacing < 32 && settings->blur)
-       {
-               settings->blur = false;
-               settings->lightpathsize += 2;
-       }
 }
 
 static void R_Shadow_BounceGrid_UpdateSpacing(void)
@@ -2618,8 +2640,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
        {
@@ -2748,7 +2816,7 @@ static void R_Shadow_BounceGrid_AssignPhotons(r_shadow_bouncegrid_settings_t *se
        dlight_t *light;
        rtlight_t *rtlight;
        normalphotonscaling = 1.0f / max(0.0001f, settings->energyperphoton);
-       for (lightindex = 0; lightindex < range2; lightindex++)
+       for (lightindex = 0;lightindex < range2;lightindex++)
        {
                if (lightindex < range)
                {
@@ -2792,12 +2860,26 @@ static void R_Shadow_BounceGrid_AssignPhotons(r_shadow_bouncegrid_settings_t *se
                w = r_shadow_lightintensityscale.value * (rtlight->ambientscale + rtlight->diffusescale + rtlight->specularscale);
                if (!settings->staticmode)
                {
-                       if (R_CullBox(cullmins, cullmaxs))
-                               continue;
+                       // skip if the expanded light box does not touch any visible leafs
                        if (r_refdef.scene.worldmodel
                                && r_refdef.scene.worldmodel->brush.BoxTouchingVisibleLeafs
                                && !r_refdef.scene.worldmodel->brush.BoxTouchingVisibleLeafs(r_refdef.scene.worldmodel, r_refdef.viewcache.world_leafvisible, cullmins, cullmaxs))
                                continue;
+                       // skip if the expanded light box is not visible to traceline
+                       // note that PrepareLight already did this check but for a smaller box, so we
+                       // end up casting more traces per frame per light when using bouncegrid, which
+                       // is probably fine (and they use the same timer)
+                       if (r_shadow_culllights_trace.integer)
+                       {
+                               if (rtlight->trace_timer != realtime && R_CanSeeBox(rtlight->trace_timer == 0 ? r_shadow_culllights_trace_tempsamples.integer : r_shadow_culllights_trace_samples.integer, r_shadow_culllights_trace_eyejitter.value, r_shadow_culllights_trace_enlarge.value, r_refdef.view.origin, rtlight->cullmins, rtlight->cullmaxs))
+                                       rtlight->trace_timer = realtime;
+                               if (realtime - rtlight->trace_timer > r_shadow_culllights_trace_delay.value)
+                                       return;
+                       }
+                       // skip if expanded light box is offscreen
+                       if (R_CullBox(cullmins, cullmaxs))
+                               continue;
+                       // skip if overall light intensity is zero
                        if (w * VectorLength2(rtlight->color) == 0.0f)
                                continue;
                }
@@ -2808,7 +2890,7 @@ static void R_Shadow_BounceGrid_AssignPhotons(r_shadow_bouncegrid_settings_t *se
                w *= ((rtlight->style >= 0 && rtlight->style < MAX_LIGHTSTYLES) ? r_refdef.scene.rtlightstylevalue[rtlight->style] : 1);
                VectorScale(rtlight->color, w, rtlight->bouncegrid_photoncolor);
                // skip lights that will emit no photons
-               if (VectorLength2(rtlight->bouncegrid_photoncolor) <= 0.0f)
+               if (!VectorLength2(rtlight->bouncegrid_photoncolor))
                        continue;
                // shoot particles from this light
                // use a calculation for the number of particles that will not
@@ -2882,8 +2964,6 @@ static void R_Shadow_BounceGrid_ClearPixels(void)
 
 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;
@@ -2892,12 +2972,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;
@@ -2913,10 +2991,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++)
        {
@@ -2968,59 +3042,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;
                }
        }
 }
@@ -3273,7 +3352,7 @@ static void R_Shadow_BounceGrid_TracePhotons(r_shadow_bouncegrid_settings_t sett
        //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;
@@ -3282,10 +3361,13 @@ 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;
 
-       Math_RandomSeed_FromInt(&randomseed, seed);
+       // 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;
 
@@ -3294,7 +3376,7 @@ static void R_Shadow_BounceGrid_TracePhotons(r_shadow_bouncegrid_settings_t sett
                hitsupercontentsmask = SUPERCONTENTS_SOLID | SUPERCONTENTS_BODY;// | SUPERCONTENTS_LIQUIDSMASK;
        else
                hitsupercontentsmask = SUPERCONTENTS_SOLID;// | SUPERCONTENTS_LIQUIDSMASK;
-       skipsupercontentsmask = SUPERCONTENTS_SKY; // skipsurfaces with this contents, even if they also have SUPERCONTENTS_SOLID (basically this is for e1m5.bsp sky trick)
+       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++)
@@ -3325,39 +3407,41 @@ static void R_Shadow_BounceGrid_TracePhotons(r_shadow_bouncegrid_settings_t sett
                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;
-               switch (settings.stablerandom)
+
+               // for seeded random we start the RNG with the position of the light
+               if (settings.rng_seed >= 0)
                {
-               default:
-                       break;
-               case 1:
-                       Math_RandomSeed_FromInt(&randomseed, lightindex * 11937);
-                       // prime the random number generator a bit
-                       Math_crandomf(&randomseed);
-                       break;
-               case 2:
-                       seed = lightindex * 11937;
-                       // prime the random number generator a bit
-                       lhcheeserand(seed);
-                       break;
+                       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++)
                {
                        VectorCopy(baseshotcolor, shotcolor);
                        VectorCopy(rtlight->shadoworigin, clipstart);
-                       switch (settings.stablerandom)
+                       switch (settings.rng_type)
                        {
                        default:
                        case 0:
-                               VectorRandom(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++)
-                                               VectorRandom(bouncerandom[bouncecount]);
-                               }
-                               break;
-                       case -1:
-                       case 1:
                                VectorLehmerRandom(&randomseed, clipend);
                                if (settings.bounceanglediffuse)
                                {
@@ -3366,8 +3450,7 @@ static void R_Shadow_BounceGrid_TracePhotons(r_shadow_bouncegrid_settings_t sett
                                                VectorLehmerRandom(&randomseed, bouncerandom[bouncecount]);
                                }
                                break;
-                       case -2:
-                       case 2:
+                       case 1:
                                VectorCheeseRandom(seed, clipend);
                                if (settings.bounceanglediffuse)
                                {
@@ -3377,14 +3460,20 @@ static void R_Shadow_BounceGrid_TracePhotons(r_shadow_bouncegrid_settings_t sett
                                }
                                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 || settings.stablerandom <= 0)
+                               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
                                        // non-stable random in dynamic mode also never reuses a direction, so there's no reason to cache it
@@ -3393,14 +3482,15 @@ static void R_Shadow_BounceGrid_TracePhotons(r_shadow_bouncegrid_settings_t sett
                                else
                                {
                                        // dynamic mode fires many rays and most will match the cache from the previous frame
-                                       cliptrace = CL_Cache_TraceLineSurfaces(clipstart, clipend, settings.hitmodels ? MOVE_HITMODEL : MOVE_NOMONSTERS, hitsupercontentsmask, skipsupercontentsmask);
+                                       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;
@@ -4076,7 +4166,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;
@@ -4594,7 +4684,7 @@ static void R_Shadow_PrepareLight(rtlight_t *rtlight)
        }
        */
 
-       // if lightstyle is currently off, don't draw the light
+       // skip if lightstyle is currently off
        if (VectorLength2(rtlight->currentcolor) < (1.0f / 1048576.0f))
                return;
 
@@ -4602,10 +4692,27 @@ static void R_Shadow_PrepareLight(rtlight_t *rtlight)
        if (nolight)
                return;
 
-       // if the light box is offscreen, skip it
+       // skip if the light box is not touching any visible leafs
+       if (r_shadow_culllights_pvs.integer
+               && r_refdef.scene.worldmodel
+               && r_refdef.scene.worldmodel->brush.BoxTouchingVisibleLeafs
+               && !r_refdef.scene.worldmodel->brush.BoxTouchingVisibleLeafs(r_refdef.scene.worldmodel, r_refdef.viewcache.world_leafvisible, rtlight->cullmins, rtlight->cullmaxs))
+               return;
+
+       // skip if the light box is not visible to traceline
+       if (r_shadow_culllights_trace.integer)
+       {
+               if (rtlight->trace_timer != realtime && R_CanSeeBox(rtlight->trace_timer == 0 ? r_shadow_culllights_trace_tempsamples.integer : r_shadow_culllights_trace_samples.integer, r_shadow_culllights_trace_eyejitter.value, r_shadow_culllights_trace_enlarge.value, r_refdef.view.origin, rtlight->cullmins, rtlight->cullmaxs))
+                       rtlight->trace_timer = realtime;
+               if (realtime - rtlight->trace_timer > r_shadow_culllights_trace_delay.value)
+                       return;
+       }
+
+       // skip if the light box is off screen
        if (R_CullBox(rtlight->cullmins, rtlight->cullmaxs))
                return;
 
+       // in the typical case this will be quickly replaced by GetLightInfo
        VectorCopy(rtlight->cullmins, rtlight->cached_cullmins);
        VectorCopy(rtlight->cullmaxs, rtlight->cached_cullmaxs);
 
@@ -4632,7 +4739,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;
@@ -5584,7 +5691,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);
@@ -5644,7 +5751,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);
@@ -5873,13 +5980,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)))
@@ -5894,13 +5998,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;
@@ -7191,7 +7305,7 @@ void R_Shadow_EditLights_DrawSelectedLightProperties(void)
                return;
        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