]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - r_shadow.c
Reworked v_isometric code significantly, it now defaults to a proper isometric view...
[xonotic/darkplaces.git] / r_shadow.c
index 91b704c10d78e3263d3c4e4b844fc1ec7ef8e03e..b880ab00795cd6a7c271ca664345cc68a890afda 100644 (file)
@@ -269,10 +269,13 @@ rtexture_t *r_shadow_prepassgeometrynormalmaptexture;
 rtexture_t *r_shadow_prepasslightingdiffusetexture;
 rtexture_t *r_shadow_prepasslightingspeculartexture;
 
-// keep track of the provided framebuffer info
-static int r_shadow_fb_fbo;
-static rtexture_t *r_shadow_fb_depthtexture;
-static rtexture_t *r_shadow_fb_colortexture;
+int r_shadow_viewfbo;
+rtexture_t *r_shadow_viewdepthtexture;
+rtexture_t *r_shadow_viewcolortexture;
+int r_shadow_viewx;
+int r_shadow_viewy;
+int r_shadow_viewwidth;
+int r_shadow_viewheight;
 
 // lights are reloaded when this changes
 char r_shadow_mapname[MAX_QPATH];
@@ -314,18 +317,18 @@ 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"};
-cvar_t r_shadow_shadowmapping_bordersize = {CVAR_SAVE, "r_shadow_shadowmapping_bordersize", "4", "shadowmap size bias for filtering"};
+cvar_t r_shadow_shadowmapping_bordersize = {CVAR_SAVE, "r_shadow_shadowmapping_bordersize", "5", "shadowmap size bias for filtering"};
 cvar_t r_shadow_shadowmapping_nearclip = {CVAR_SAVE, "r_shadow_shadowmapping_nearclip", "1", "shadowmap nearclip in world units"};
 cvar_t r_shadow_shadowmapping_bias = {CVAR_SAVE, "r_shadow_shadowmapping_bias", "0.03", "shadowmap bias parameter (this is multiplied by nearclip * 1024 / lodsize)"};
 cvar_t r_shadow_shadowmapping_polygonfactor = {CVAR_SAVE, "r_shadow_shadowmapping_polygonfactor", "2", "slope-dependent shadowmapping bias"};
@@ -334,19 +337,28 @@ 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", "make light bounds bigger by *(1.0+enlarge)"};
+cvar_t r_shadow_culllights_trace_expand = {CVAR_SAVE, "r_shadow_culllights_trace_expand", "8", "make light bounds bigger by this many units"};
+cvar_t r_shadow_culllights_trace_pad = {CVAR_SAVE, "r_shadow_culllights_trace_expand", "8", "accept traces that hit within this many units of the light bounds"};
+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.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 +366,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 +476,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 +568,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 +669,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 +744,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);
@@ -786,19 +816,28 @@ 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_expand);
+       Cvar_RegisterVariable(&r_shadow_culllights_trace_pad);
+       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);
+       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 +845,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 +1628,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++)
@@ -2074,7 +2118,7 @@ void R_Shadow_RenderMode_ActiveLight(const rtlight_t *rtlight)
 void R_Shadow_RenderMode_Reset(void)
 {
        R_Mesh_ResetTextureState();
-       R_Mesh_SetRenderTargets(r_shadow_fb_fbo, r_shadow_fb_depthtexture, r_shadow_fb_colortexture, NULL, NULL, NULL);
+       R_Mesh_SetRenderTargets(r_shadow_viewfbo, r_shadow_viewdepthtexture, r_shadow_viewcolortexture, NULL, NULL, NULL);
        R_SetViewport(&r_refdef.view.viewport);
        GL_Scissor(r_shadow_lightscissor[0], r_shadow_lightscissor[1], r_shadow_lightscissor[2], r_shadow_lightscissor[3]);
        GL_DepthRange(0, 1);
@@ -2408,11 +2452,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 +2510,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 +2538,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 +2581,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 +2602,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 +2647,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 +2779,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 +2802,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 +2822,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 +2844,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;
@@ -2757,12 +2867,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))
+                               && 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_shadow_culllights_trace_expand.value, r_shadow_culllights_trace_pad.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;
                }
@@ -2771,9 +2895,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 +2908,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 +2960,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 +2979,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 +2998,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 +3049,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 +3145,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 +3226,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 +3280,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 +3344,24 @@ 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 skipmaterialflagsmask;
        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 +3369,23 @@ 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;
+               hitsupercontentsmask = SUPERCONTENTS_SOLID;// | SUPERCONTENTS_LIQUIDSMASK;
        else
                hitsupercontentsmask = SUPERCONTENTS_SOLID;// | SUPERCONTENTS_LIQUIDSMASK;
+       skipsupercontentsmask = 0;
+       skipmaterialflagsmask = MATERIALFLAGMASK_TRANSLUCENT;
        maxbounce = settings.maxbounce;
 
        for (lightindex = 0;lightindex < range2;lightindex++)
@@ -3234,50 +3402,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, skipmaterialflagsmask, 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, skipmaterialflagsmask);
                                }
                                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 +3520,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 +3554,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 +3573,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 +3605,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 +3621,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)
@@ -3643,10 +3886,10 @@ static void R_Shadow_RenderLighting_VisibleLighting(int texturenumsurfaces, cons
        RSurf_DrawBatch();
 }
 
-static void R_Shadow_RenderLighting_Light_GLSL(int texturenumsurfaces, const msurface_t **texturesurfacelist, const vec3_t lightcolor, float ambientscale, float diffusescale, float specularscale)
+static void R_Shadow_RenderLighting_Light_GLSL(int texturenumsurfaces, const msurface_t **texturesurfacelist, const float ambientcolor[3], const float diffusecolor[3], const float specularcolor[3])
 {
        // ARB2 GLSL shader path (GFFX5200, Radeon 9500)
-       R_SetupShader_Surface(lightcolor, false, ambientscale, diffusescale, specularscale, RSURFPASS_RTLIGHT, texturenumsurfaces, texturesurfacelist, NULL, false);
+       R_SetupShader_Surface(ambientcolor, diffusecolor, specularcolor, RSURFPASS_RTLIGHT, texturenumsurfaces, texturesurfacelist, NULL, false);
        RSurf_DrawBatch();
 }
 
@@ -3739,29 +3982,26 @@ static void R_Shadow_RenderLighting_Light_Vertex_Pass(int firstvertex, int numve
        }
 }
 
-static void R_Shadow_RenderLighting_Light_Vertex(int texturenumsurfaces, const msurface_t **texturesurfacelist, const vec3_t lightcolor, float ambientscale, float diffusescale)
+static void R_Shadow_RenderLighting_Light_Vertex(int texturenumsurfaces, const msurface_t **texturesurfacelist, const float ambientcolor[3], const float diffusecolor[3])
 {
        // OpenGL 1.1 path (anything)
        float ambientcolorbase[3], diffusecolorbase[3];
        float ambientcolorpants[3], diffusecolorpants[3];
        float ambientcolorshirt[3], diffusecolorshirt[3];
-       const float *surfacecolor = rsurface.texture->dlightcolor;
-       const float *surfacepants = rsurface.colormap_pantscolor;
-       const float *surfaceshirt = rsurface.colormap_shirtcolor;
+       const float *surfacepants = rsurface.texture->render_colormap_pants;
+       const float *surfaceshirt = rsurface.texture->render_colormap_shirt;
        rtexture_t *basetexture = rsurface.texture->basetexture;
        rtexture_t *pantstexture = rsurface.texture->pantstexture;
        rtexture_t *shirttexture = rsurface.texture->shirttexture;
        qboolean dopants = pantstexture && VectorLength2(surfacepants) >= (1.0f / 1048576.0f);
        qboolean doshirt = shirttexture && VectorLength2(surfaceshirt) >= (1.0f / 1048576.0f);
-       ambientscale *= 2 * r_refdef.view.colorscale;
-       diffusescale *= 2 * r_refdef.view.colorscale;
-       ambientcolorbase[0] = lightcolor[0] * ambientscale * surfacecolor[0];ambientcolorbase[1] = lightcolor[1] * ambientscale * surfacecolor[1];ambientcolorbase[2] = lightcolor[2] * ambientscale * surfacecolor[2];
-       diffusecolorbase[0] = lightcolor[0] * diffusescale * surfacecolor[0];diffusecolorbase[1] = lightcolor[1] * diffusescale * surfacecolor[1];diffusecolorbase[2] = lightcolor[2] * diffusescale * surfacecolor[2];
+       VectorCopy(ambientcolor, ambientcolorbase);
+       VectorCopy(diffusecolor, diffusecolorbase);
        ambientcolorpants[0] = ambientcolorbase[0] * surfacepants[0];ambientcolorpants[1] = ambientcolorbase[1] * surfacepants[1];ambientcolorpants[2] = ambientcolorbase[2] * surfacepants[2];
        diffusecolorpants[0] = diffusecolorbase[0] * surfacepants[0];diffusecolorpants[1] = diffusecolorbase[1] * surfacepants[1];diffusecolorpants[2] = diffusecolorbase[2] * surfacepants[2];
        ambientcolorshirt[0] = ambientcolorbase[0] * surfaceshirt[0];ambientcolorshirt[1] = ambientcolorbase[1] * surfaceshirt[1];ambientcolorshirt[2] = ambientcolorbase[2] * surfaceshirt[2];
        diffusecolorshirt[0] = diffusecolorbase[0] * surfaceshirt[0];diffusecolorshirt[1] = diffusecolorbase[1] * surfaceshirt[1];diffusecolorshirt[2] = diffusecolorbase[2] * surfaceshirt[2];
-       RSurf_PrepareVerticesForBatch(BATCHNEED_ARRAY_VERTEX | (diffusescale > 0 ? BATCHNEED_ARRAY_NORMAL : 0) | BATCHNEED_ARRAY_TEXCOORD | BATCHNEED_NOGAPS, texturenumsurfaces, texturesurfacelist);
+       RSurf_PrepareVerticesForBatch(BATCHNEED_ARRAY_VERTEX | (VectorLength2(diffusecolor) > 0 ? BATCHNEED_ARRAY_NORMAL : 0) | BATCHNEED_ARRAY_TEXCOORD | BATCHNEED_NOGAPS, texturenumsurfaces, texturesurfacelist);
        rsurface.passcolor4f = (float *)R_FrameData_Alloc((rsurface.batchfirstvertex + rsurface.batchnumvertices) * sizeof(float[4]));
        R_Mesh_VertexPointer(3, GL_FLOAT, sizeof(float[3]), rsurface.batchvertex3f, rsurface.batchvertex3f_vertexbuffer, rsurface.batchvertex3f_bufferoffset);
        R_Mesh_ColorPointer(4, GL_FLOAT, sizeof(float[4]), rsurface.passcolor4f, 0, 0);
@@ -3811,25 +4051,28 @@ static void R_Shadow_RenderLighting_Light_Vertex(int texturenumsurfaces, const m
 extern cvar_t gl_lightmaps;
 void R_Shadow_RenderLighting(int texturenumsurfaces, const msurface_t **texturesurfacelist)
 {
-       float ambientscale, diffusescale, specularscale;
        qboolean negated;
-       float lightcolor[3];
-       VectorCopy(rsurface.rtlight->currentcolor, lightcolor);
-       ambientscale = rsurface.rtlight->ambientscale + rsurface.texture->rtlightambient;
-       diffusescale = rsurface.rtlight->diffusescale * max(0, 1.0 - rsurface.texture->rtlightambient);
-       specularscale = rsurface.rtlight->specularscale * rsurface.texture->specularscale;
+       float ambientcolor[3], diffusecolor[3], specularcolor[3];
+       VectorM(rsurface.rtlight->ambientscale + rsurface.texture->rtlightambient, rsurface.texture->render_rtlight_diffuse, ambientcolor);
+       VectorM(rsurface.rtlight->diffusescale * max(0, 1.0 - rsurface.texture->rtlightambient), rsurface.texture->render_rtlight_diffuse, diffusecolor);
+       VectorM(rsurface.rtlight->specularscale, rsurface.texture->render_rtlight_specular, specularcolor);
        if (!r_shadow_usenormalmap.integer)
        {
-               ambientscale += 1.0f * diffusescale;
-               diffusescale = 0;
-               specularscale = 0;
+               VectorMAM(1.0f, ambientcolor, 1.0f, diffusecolor, ambientcolor);
+               VectorClear(diffusecolor);
+               VectorClear(specularcolor);
        }
-       if ((ambientscale + diffusescale) * VectorLength2(lightcolor) + specularscale * VectorLength2(lightcolor) < (1.0f / 1048576.0f))
+       VectorMultiply(ambientcolor, rsurface.rtlight->currentcolor, ambientcolor);
+       VectorMultiply(diffusecolor, rsurface.rtlight->currentcolor, diffusecolor);
+       VectorMultiply(specularcolor, rsurface.rtlight->currentcolor, specularcolor);
+       if (VectorLength2(ambientcolor) + VectorLength2(diffusecolor) + VectorLength2(specularcolor) < (1.0f / 1048576.0f))
                return;
-       negated = (lightcolor[0] + lightcolor[1] + lightcolor[2] < 0) && vid.support.ext_blend_subtract;
+       negated = (rsurface.rtlight->currentcolor[0] + rsurface.rtlight->currentcolor[1] + rsurface.rtlight->currentcolor[2] < 0) && vid.support.ext_blend_subtract;
        if(negated)
        {
-               VectorNegate(lightcolor, lightcolor);
+               VectorNegate(ambientcolor, ambientcolor);
+               VectorNegate(diffusecolor, diffusecolor);
+               VectorNegate(specularcolor, specularcolor);
                GL_BlendEquationSubtract(true);
        }
        RSurf_SetupDepthAndCulling();
@@ -3840,13 +4083,13 @@ void R_Shadow_RenderLighting(int texturenumsurfaces, const msurface_t **textures
                R_Shadow_RenderLighting_VisibleLighting(texturenumsurfaces, texturesurfacelist);
                break;
        case R_SHADOW_RENDERMODE_LIGHT_GLSL:
-               R_Shadow_RenderLighting_Light_GLSL(texturenumsurfaces, texturesurfacelist, lightcolor, ambientscale, diffusescale, specularscale);
+               R_Shadow_RenderLighting_Light_GLSL(texturenumsurfaces, texturesurfacelist, ambientcolor, diffusecolor, specularcolor);
                break;
        case R_SHADOW_RENDERMODE_LIGHT_VERTEX3DATTEN:
        case R_SHADOW_RENDERMODE_LIGHT_VERTEX2D1DATTEN:
        case R_SHADOW_RENDERMODE_LIGHT_VERTEX2DATTEN:
        case R_SHADOW_RENDERMODE_LIGHT_VERTEX:
-               R_Shadow_RenderLighting_Light_Vertex(texturenumsurfaces, texturesurfacelist, lightcolor, ambientscale, diffusescale);
+               R_Shadow_RenderLighting_Light_Vertex(texturenumsurfaces, texturesurfacelist, ambientcolor, diffusecolor);
                break;
        default:
                Con_Printf("R_Shadow_RenderLighting: unknown r_shadow_rendermode %i\n", r_shadow_rendermode);
@@ -3932,7 +4175,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;
@@ -4223,7 +4466,7 @@ static void R_Shadow_DrawWorldShadow_ShadowMap(int numsurfaces, int *surfacelist
 {
        shadowmesh_t *mesh;
 
-       RSurf_ActiveWorldEntity();
+       RSurf_ActiveModelEntity(r_refdef.scene.worldentity, false, false, false);
 
        if (rsurface.rtlight->compiled && r_shadow_realtime_world_compile.integer && r_shadow_realtime_world_compileshadow.integer)
        {
@@ -4243,7 +4486,7 @@ static void R_Shadow_DrawWorldShadow_ShadowMap(int numsurfaces, int *surfacelist
        else if (r_refdef.scene.worldentity->model)
                r_refdef.scene.worldmodel->DrawShadowMap(r_shadow_shadowmapside, r_refdef.scene.worldentity, rsurface.rtlight->shadoworigin, NULL, rsurface.rtlight->radius, numsurfaces, surfacelist, surfacesides, rsurface.rtlight->cached_cullmins, rsurface.rtlight->cached_cullmaxs);
 
-       rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveWorldEntity/RSurf_ActiveModelEntity
+       rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveModelEntity
 }
 
 static void R_Shadow_DrawWorldShadow_ShadowVolume(int numsurfaces, int *surfacelist, const unsigned char *trispvs)
@@ -4258,7 +4501,7 @@ static void R_Shadow_DrawWorldShadow_ShadowVolume(int numsurfaces, int *surfacel
        if (r_refdef.scene.worldmodel->brush.shadowmesh ? !r_refdef.scene.worldmodel->brush.shadowmesh->neighbor3i : !r_refdef.scene.worldmodel->surfmesh.data_neighbor3i)
                return;
 
-       RSurf_ActiveWorldEntity();
+       RSurf_ActiveModelEntity(r_refdef.scene.worldentity, false, false, false);
 
        if (rsurface.rtlight->compiled && r_shadow_realtime_world_compile.integer && r_shadow_realtime_world_compileshadow.integer)
        {
@@ -4315,7 +4558,7 @@ static void R_Shadow_DrawWorldShadow_ShadowVolume(int numsurfaces, int *surfacel
                r_refdef.scene.worldmodel->DrawShadowVolume(r_refdef.scene.worldentity, rsurface.rtlight->shadoworigin, NULL, rsurface.rtlight->radius, numsurfaces, surfacelist, rsurface.rtlight->cached_cullmins, rsurface.rtlight->cached_cullmaxs);
        }
 
-       rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveWorldEntity/RSurf_ActiveModelEntity
+       rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveModelEntity
 }
 
 static void R_Shadow_DrawEntityShadow(entity_render_t *ent)
@@ -4341,7 +4584,7 @@ static void R_Shadow_DrawEntityShadow(entity_render_t *ent)
                ent->model->DrawShadowVolume(ent, relativeshadoworigin, NULL, relativeshadowradius, ent->model->nummodelsurfaces, ent->model->sortedmodelsurfaces, relativeshadowmins, relativeshadowmaxs);
                break;
        }
-       rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveWorldEntity/RSurf_ActiveModelEntity
+       rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveModelEntity
 }
 
 void R_Shadow_SetupEntityLight(const entity_render_t *ent)
@@ -4360,7 +4603,7 @@ static void R_Shadow_DrawWorldLight(int numsurfaces, int *surfacelist, const uns
                return;
 
        // set up properties for rendering light onto this entity
-       RSurf_ActiveWorldEntity();
+       RSurf_ActiveModelEntity(r_refdef.scene.worldentity, false, false, false);
        rsurface.entitytolight = rsurface.rtlight->matrix_worldtolight;
        Matrix4x4_Concat(&rsurface.entitytoattenuationxyz, &matrix_attenuationxyz, &rsurface.entitytolight);
        Matrix4x4_Concat(&rsurface.entitytoattenuationz, &matrix_attenuationz, &rsurface.entitytolight);
@@ -4368,7 +4611,7 @@ static void R_Shadow_DrawWorldLight(int numsurfaces, int *surfacelist, const uns
 
        r_refdef.scene.worldmodel->DrawLight(r_refdef.scene.worldentity, numsurfaces, surfacelist, lighttrispvs);
 
-       rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveWorldEntity/RSurf_ActiveModelEntity
+       rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveModelEntity
 }
 
 static void R_Shadow_DrawEntityLight(entity_render_t *ent)
@@ -4381,7 +4624,7 @@ static void R_Shadow_DrawEntityLight(entity_render_t *ent)
 
        model->DrawLight(ent, model->nummodelsurfaces, model->sortedmodelsurfaces, NULL);
 
-       rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveWorldEntity/RSurf_ActiveModelEntity
+       rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveModelEntity
 }
 
 static void R_Shadow_PrepareLight(rtlight_t *rtlight)
@@ -4450,7 +4693,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;
 
@@ -4458,10 +4701,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_shadow_culllights_trace_expand.value, r_shadow_culllights_trace_pad.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);
 
@@ -4488,7 +4748,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;
@@ -5064,10 +5324,11 @@ void R_Shadow_DrawPrepass(void)
 #define MAX_SCENELIGHTS 65536
 qboolean R_Shadow_PrepareLights_AddSceneLight(rtlight_t *rtlight)
 {
-       if (r_shadow_scenenumlights >= MAX_SCENELIGHTS)
-               return false;
-       if (r_shadow_scenemaxlights <= r_shadow_scenenumlights && r_shadow_scenemaxlights < MAX_SCENELIGHTS)
+       if (r_shadow_scenemaxlights <= r_shadow_scenenumlights)
        {
+               if (r_shadow_scenenumlights >= MAX_SCENELIGHTS)
+                       return false;
+               r_shadow_scenemaxlights *= 2;
                r_shadow_scenemaxlights = bound(1024, r_shadow_scenemaxlights, MAX_SCENELIGHTS);
                r_shadow_scenelightlist = (rtlight_t **)Mem_Realloc(r_main_mempool, r_shadow_scenelightlist, r_shadow_scenemaxlights * sizeof(rtlight_t *));
        }
@@ -5076,7 +5337,7 @@ qboolean R_Shadow_PrepareLights_AddSceneLight(rtlight_t *rtlight)
 }
 
 void R_Shadow_DrawLightSprites(void);
-void R_Shadow_PrepareLights(int fbo, rtexture_t *depthtexture, rtexture_t *colortexture)
+void R_Shadow_PrepareLights(void)
 {
        int flag;
        int lnum;
@@ -5100,10 +5361,6 @@ void R_Shadow_PrepareLights(int fbo, rtexture_t *depthtexture, rtexture_t *color
                r_shadow_shadowmapdepthtexture != r_fb.usedepthtextures)
                R_Shadow_FreeShadowMaps();
 
-       r_shadow_fb_fbo = fbo;
-       r_shadow_fb_depthtexture = depthtexture;
-       r_shadow_fb_colortexture = colortexture;
-
        r_shadow_usingshadowmaportho = false;
 
        switch (vid.renderpath)
@@ -5313,7 +5570,7 @@ void R_Shadow_PrepareModelShadows(void)
        r_shadow_nummodelshadows = 0;
        r_shadow_shadowmapatlas_modelshadows_size = 0;
 
-       if (!r_refdef.scene.numentities || r_refdef.lightmapintensity <= 0.0f || r_shadows.integer <= 0)
+       if (!r_refdef.scene.numentities || r_refdef.scene.lightmapintensity <= 0.0f || r_shadows.integer <= 0)
                return;
 
        switch (r_shadow_shadowmode)
@@ -5341,7 +5598,7 @@ void R_Shadow_PrepareModelShadows(void)
                return;
        }
 
-       size = 2 * r_shadow_shadowmapmaxsize;
+       size = r_shadow_shadowmaptexturesize / 4;
        scale = r_shadow_shadowmapping_precision.value * r_shadows_shadowmapscale.value;
        radius = 0.5f * size / scale;
 
@@ -5439,7 +5696,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);
@@ -5481,7 +5738,7 @@ static void R_Shadow_DrawModelShadowMaps(void)
                relativeshadowmaxs[2] = relativelightorigin[2] + r_shadows_throwdistance.value * fabs(relativelightdirection[2]) + radius * (fabs(relativeforward[2]) + fabs(relativeright[2]));
                RSurf_ActiveModelEntity(ent, false, false, false);
                ent->model->DrawShadowMap(0, ent, relativelightorigin, relativelightdirection, relativethrowdistance, ent->model->nummodelsurfaces, ent->model->sortedmodelsurfaces, NULL, relativeshadowmins, relativeshadowmaxs);
-               rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveWorldEntity/RSurf_ActiveModelEntity
+               rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveModelEntity
        }
 
 #if 0
@@ -5499,7 +5756,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);
@@ -5545,15 +5802,15 @@ void R_Shadow_DrawModelShadows(void)
        if (!r_shadow_nummodelshadows || (r_shadow_shadowmode != R_SHADOW_SHADOWMODE_STENCIL && r_shadows.integer != 1))
                return;
 
-       R_ResetViewRendering3D(r_shadow_fb_fbo, r_shadow_fb_depthtexture, r_shadow_fb_colortexture);
+       R_ResetViewRendering3D(r_shadow_viewfbo, r_shadow_viewdepthtexture, r_shadow_viewcolortexture, r_shadow_viewx, r_shadow_viewy, r_shadow_viewwidth, r_shadow_viewheight);
        //GL_Scissor(r_refdef.view.viewport.x, r_refdef.view.viewport.y, r_refdef.view.viewport.width, r_refdef.view.viewport.height);
        //GL_Scissor(r_refdef.view.x, vid.height - r_refdef.view.height - r_refdef.view.y, r_refdef.view.width, r_refdef.view.height);
        R_Shadow_RenderMode_Begin();
        R_Shadow_RenderMode_ActiveLight(NULL);
-       r_shadow_lightscissor[0] = r_refdef.view.x;
-       r_shadow_lightscissor[1] = vid.height - r_refdef.view.y - r_refdef.view.height;
-       r_shadow_lightscissor[2] = r_refdef.view.width;
-       r_shadow_lightscissor[3] = r_refdef.view.height;
+       r_shadow_lightscissor[0] = r_shadow_viewx;
+       r_shadow_lightscissor[1] = (r_shadow_viewfbo ? r_shadow_viewheight : vid.height) - r_shadow_viewy - r_shadow_viewheight;
+       r_shadow_lightscissor[2] = r_shadow_viewwidth;
+       r_shadow_lightscissor[3] = r_shadow_viewheight;
        R_Shadow_RenderMode_StencilShadowVolumes(false);
 
        // get shadow dir
@@ -5578,45 +5835,14 @@ void R_Shadow_DrawModelShadows(void)
                        Matrix4x4_Transform3x3(&ent->inversematrix, shadowdir, relativelightdirection);
                else
                {
-                       if(ent->entitynumber != 0)
-                       {
-                               if(ent->entitynumber >= MAX_EDICTS) // csqc entity
-                               {
-                                       // FIXME handle this
-                                       VectorNegate(ent->modellight_lightdir, relativelightdirection);
-                               }
-                               else
-                               {
-                                       // networked entity - might be attached in some way (then we should use the parent's light direction, to not tear apart attached entities)
-                                       int entnum, entnum2, recursion;
-                                       entnum = entnum2 = ent->entitynumber;
-                                       for(recursion = 32; recursion > 0; --recursion)
-                                       {
-                                               entnum2 = cl.entities[entnum].state_current.tagentity;
-                                               if(entnum2 >= 1 && entnum2 < cl.num_entities && cl.entities_active[entnum2])
-                                                       entnum = entnum2;
-                                               else
-                                                       break;
-                                       }
-                                       if(recursion && recursion != 32) // if we followed a valid non-empty attachment chain
-                                       {
-                                               VectorNegate(cl.entities[entnum].render.modellight_lightdir, relativelightdirection);
-                                               // transform into modelspace of OUR entity
-                                               Matrix4x4_Transform3x3(&cl.entities[entnum].render.matrix, relativelightdirection, tmp);
-                                               Matrix4x4_Transform3x3(&ent->inversematrix, tmp, relativelightdirection);
-                                       }
-                                       else
-                                               VectorNegate(ent->modellight_lightdir, relativelightdirection);
-                               }
-                       }
-                       else
-                               VectorNegate(ent->modellight_lightdir, relativelightdirection);
+                       VectorNegate(ent->render_modellight_lightdir, tmp);
+                       Matrix4x4_Transform3x3(&ent->inversematrix, tmp, relativelightdirection);
                }
 
                VectorScale(relativelightdirection, -relativethrowdistance, relativelightorigin);
                RSurf_ActiveModelEntity(ent, false, false, false);
                ent->model->DrawShadowVolume(ent, relativelightorigin, relativelightdirection, relativethrowdistance, ent->model->nummodelsurfaces, ent->model->sortedmodelsurfaces, relativeshadowmins, relativeshadowmaxs);
-               rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveWorldEntity/RSurf_ActiveModelEntity
+               rsurface.entity = NULL; // used only by R_GetCurrentTexture and RSurf_ActiveModelEntity
        }
 
        // not really the right mode, but this will disable any silly stencil features
@@ -5628,7 +5854,7 @@ void R_Shadow_DrawModelShadows(void)
        //GL_ScissorTest(true);
        //R_EntityMatrix(&identitymatrix);
        //R_Mesh_ResetTextureState();
-       R_ResetViewRendering2D(r_shadow_fb_fbo, r_shadow_fb_depthtexture, r_shadow_fb_colortexture);
+       R_ResetViewRendering2D(r_shadow_viewfbo, r_shadow_viewdepthtexture, r_shadow_viewcolortexture, r_shadow_viewx, r_shadow_viewy, r_shadow_viewwidth, r_shadow_viewheight);
 
        // set up a darkening blend on shadowed areas
        GL_BlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
@@ -5728,13 +5954,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)))
@@ -5749,13 +5972,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;
@@ -5780,8 +6013,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_Cache_TraceLineSurfaces(r_refdef.view.origin, rtlight->shadoworigin, MOVE_NORMAL, SUPERCONTENTS_SOLID, 0, MATERIALFLAGMASK_TRANSLUCENT).fraction < 1)
                        return;
        }
        VectorScale(rtlight->currentcolor, cscale, color);
@@ -5845,7 +6077,7 @@ void R_Shadow_DrawCoronas(void)
                                qglGenQueriesARB(r_maxqueries - i, r_queries + i);
                                CHECKGLERROR
                        }
-                       RSurf_ActiveWorldEntity();
+                       RSurf_ActiveModelEntity(r_refdef.scene.worldentity, false, false, false);
                        GL_BlendFunc(GL_ONE, GL_ZERO);
                        GL_CullFace(GL_NONE);
                        GL_DepthMask(false);
@@ -6114,7 +6346,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_NORMAL, NULL, SUPERCONTENTS_SOLID, 0, MATERIALFLAGMASK_TRANSLUCENT, collision_extendmovelength.value, true, false, NULL, false, true).fraction == 1.0f)
                        {
                                bestrating = rating;
                                best = light;
@@ -6564,7 +6796,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_NORMAL, NULL, SUPERCONTENTS_SOLID, 0, MATERIALFLAGMASK_TRANSLUCENT, collision_extendmovelength.value, true, false, NULL, false, true);
        if (trace.fraction < 1)
        {
                dist = trace.fraction * r_editlights_cursordistance.value;
@@ -7044,9 +7276,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
@@ -7079,6 +7311,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)
@@ -7298,141 +7541,48 @@ LIGHT SAMPLING
 =============================================================================
 */
 
-void R_LightPoint(float *color, const vec3_t p, const int flags)
+void R_CompleteLightPoint(float *ambient, float *diffuse, float *lightdir, const vec3_t p, const int flags, float lightmapintensity, float ambientintensity)
 {
-       int i, numlights, flag;
-       float f, relativepoint[3], dist, dist2, lightradius2;
-       vec3_t diffuse, n;
-       rtlight_t *light;
-       dlight_t *dlight;
-
-       if (r_fullbright.integer)
-       {
-               VectorSet(color, 1, 1, 1);
-               return;
-       }
-
-       VectorClear(color);
-
-       if (flags & LP_LIGHTMAP)
-       {
-               if (!r_fullbright.integer && r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->lit && r_refdef.scene.worldmodel->brush.LightPoint)
-               {
-                       VectorClear(diffuse);
-                       r_refdef.scene.worldmodel->brush.LightPoint(r_refdef.scene.worldmodel, p, color, diffuse, n);
-                       VectorAdd(color, diffuse, color);
-               }
-               else
-                       VectorSet(color, 1, 1, 1);
-               color[0] += r_refdef.scene.ambient;
-               color[1] += r_refdef.scene.ambient;
-               color[2] += r_refdef.scene.ambient;
-       }
-
-       if (flags & LP_RTWORLD)
-       {
-               flag = r_refdef.scene.rtworld ? LIGHTFLAG_REALTIMEMODE : LIGHTFLAG_NORMALMODE;
-               numlights = (int)Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray);
-               for (i = 0; i < numlights; i++)
-               {
-                       dlight = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, i);
-                       if (!dlight)
-                               continue;
-                       light = &dlight->rtlight;
-                       if (!(light->flags & flag))
-                               continue;
-                       // sample
-                       lightradius2 = light->radius * light->radius;
-                       VectorSubtract(light->shadoworigin, p, relativepoint);
-                       dist2 = VectorLength2(relativepoint);
-                       if (dist2 >= lightradius2)
-                               continue;
-                       dist = sqrt(dist2) / light->radius;
-                       f = dist < 1 ? (r_shadow_lightintensityscale.value * ((1.0f - dist) * r_shadow_lightattenuationlinearscale.value / (r_shadow_lightattenuationdividebias.value + dist*dist))) : 0;
-                       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)
-                               VectorMA(color, f, light->currentcolor, color);
-               }
-       }
-       if (flags & LP_DYNLIGHT)
-       {
-               // sample dlights
-               for (i = 0;i < r_refdef.scene.numlights;i++)
-               {
-                       light = r_refdef.scene.lights[i];
-                       // sample
-                       lightradius2 = light->radius * light->radius;
-                       VectorSubtract(light->shadoworigin, p, relativepoint);
-                       dist2 = VectorLength2(relativepoint);
-                       if (dist2 >= lightradius2)
-                               continue;
-                       dist = sqrt(dist2) / light->radius;
-                       f = dist < 1 ? (r_shadow_lightintensityscale.value * ((1.0f - dist) * r_shadow_lightattenuationlinearscale.value / (r_shadow_lightattenuationdividebias.value + dist*dist))) : 0;
-                       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)
-                               VectorMA(color, f, light->color, color);
-               }
-       }
-}
-
-void R_CompleteLightPoint(vec3_t ambient, vec3_t diffuse, vec3_t lightdir, const vec3_t p, const int flags)
-{
-       int i, numlights, flag;
+       int i, numlights, flag, q;
        rtlight_t *light;
        dlight_t *dlight;
        float relativepoint[3];
        float color[3];
-       float dir[3];
        float dist;
        float dist2;
        float intensity;
-       float sample[5*3];
+       float sa[3], sx[3], sy[3], sz[3], sd[3];
        float lightradius2;
 
-       if (r_fullbright.integer)
-       {
-               VectorSet(ambient, 1, 1, 1);
-               VectorClear(diffuse);
-               VectorClear(lightdir);
-               return;
-       }
+       // use first order spherical harmonics to combine directional lights
+       for (q = 0; q < 3; q++)
+               sa[q] = sx[q] = sy[q] = sz[q] = sd[q] = 0;
 
-       if (flags == LP_LIGHTMAP)
+       if (flags & LP_LIGHTMAP)
        {
-               VectorSet(ambient, r_refdef.scene.ambient, r_refdef.scene.ambient, r_refdef.scene.ambient);
-               VectorClear(diffuse);
-               VectorClear(lightdir);
                if (r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->lit && r_refdef.scene.worldmodel->brush.LightPoint)
-                       r_refdef.scene.worldmodel->brush.LightPoint(r_refdef.scene.worldmodel, p, ambient, diffuse, lightdir);
+               {
+                       float tempambient[3];
+                       for (q = 0; q < 3; q++)
+                               tempambient[q] = color[q] = relativepoint[q] = 0;
+                       r_refdef.scene.worldmodel->brush.LightPoint(r_refdef.scene.worldmodel, p, tempambient, color, relativepoint);
+                       // calculate a weighted average light direction as well
+                       intensity = VectorLength(color);
+                       for (q = 0; q < 3; q++)
+                       {
+                               sa[q] += (0.5f * color[q] + tempambient[q]) * lightmapintensity;
+                               sx[q] += (relativepoint[0] * color[q]) * lightmapintensity;
+                               sy[q] += (relativepoint[1] * color[q]) * lightmapintensity;
+                               sz[q] += (relativepoint[2] * color[q]) * lightmapintensity;
+                               sd[q] += (intensity * relativepoint[q]) * lightmapintensity;
+                       }
+               }
                else
-                       VectorSet(ambient, 1, 1, 1);
-               return;
-       }
-
-       memset(sample, 0, sizeof(sample));
-       VectorSet(sample, r_refdef.scene.ambient, r_refdef.scene.ambient, r_refdef.scene.ambient);
-
-       if ((flags & LP_LIGHTMAP) && r_refdef.scene.worldmodel && r_refdef.scene.worldmodel->lit && r_refdef.scene.worldmodel->brush.LightPoint)
-       {
-               vec3_t tempambient;
-               VectorClear(tempambient);
-               VectorClear(color);
-               VectorClear(relativepoint);
-               r_refdef.scene.worldmodel->brush.LightPoint(r_refdef.scene.worldmodel, p, tempambient, color, relativepoint);
-               VectorScale(tempambient, r_refdef.lightmapintensity, tempambient);
-               VectorScale(color, r_refdef.lightmapintensity, color);
-               VectorAdd(sample, tempambient, sample);
-               VectorMA(sample    , 0.5f            , color, sample    );
-               VectorMA(sample + 3, relativepoint[0], color, sample + 3);
-               VectorMA(sample + 6, relativepoint[1], color, sample + 6);
-               VectorMA(sample + 9, relativepoint[2], color, sample + 9);
-               // calculate a weighted average light direction as well
-               intensity = VectorLength(color);
-               VectorMA(sample + 12, intensity, relativepoint, sample + 12);
+               {
+                       // unlit map - fullbright but scaled by lightmapintensity
+                       for (q = 0; q < 3; q++)
+                               sa[q] += lightmapintensity;
+               }
        }
 
        if (flags & LP_RTWORLD)
@@ -7457,19 +7607,20 @@ 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, 0, MATERIALFLAGMASK_TRANSLUCENT, collision_extendmovelength.value, true, false, NULL, false, true).fraction < 1)
                                continue;
-                       // scale down intensity to add to both ambient and diffuse
-                       //intensity *= 0.5f;
+                       for (q = 0; q < 3; q++)
+                               color[q] = light->currentcolor[q] * intensity;
+                       intensity = VectorLength(color);
                        VectorNormalize(relativepoint);
-                       VectorScale(light->currentcolor, intensity, color);
-                       VectorMA(sample    , 0.5f            , color, sample    );
-                       VectorMA(sample + 3, relativepoint[0], color, sample + 3);
-                       VectorMA(sample + 6, relativepoint[1], color, sample + 6);
-                       VectorMA(sample + 9, relativepoint[2], color, sample + 9);
-                       // calculate a weighted average light direction as well
-                       intensity *= VectorLength(color);
-                       VectorMA(sample + 12, intensity, relativepoint, sample + 12);
+                       for (q = 0; q < 3; q++)
+                       {
+                               sa[q] += 0.5f * color[q];
+                               sx[q] += relativepoint[0] * color[q];
+                               sy[q] += relativepoint[1] * color[q];
+                               sz[q] += relativepoint[2] * color[q];
+                               sd[q] += intensity * relativepoint[q];
+                       }
                }
                // FIXME: sample bouncegrid too!
        }
@@ -7490,32 +7641,32 @@ 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, 0, MATERIALFLAGMASK_TRANSLUCENT, collision_extendmovelength.value, true, false, NULL, false, true).fraction < 1)
                                continue;
-                       // scale down intensity to add to both ambient and diffuse
-                       //intensity *= 0.5f;
+                       for (q = 0; q < 3; q++)
+                               color[q] = light->currentcolor[q] * intensity;
+                       intensity = VectorLength(color);
                        VectorNormalize(relativepoint);
-                       VectorScale(light->currentcolor, intensity, color);
-                       VectorMA(sample    , 0.5f            , color, sample    );
-                       VectorMA(sample + 3, relativepoint[0], color, sample + 3);
-                       VectorMA(sample + 6, relativepoint[1], color, sample + 6);
-                       VectorMA(sample + 9, relativepoint[2], color, sample + 9);
-                       // calculate a weighted average light direction as well
-                       intensity *= VectorLength(color);
-                       VectorMA(sample + 12, intensity, relativepoint, sample + 12);
+                       for (q = 0; q < 3; q++)
+                       {
+                               sa[q] += 0.5f * color[q];
+                               sx[q] += relativepoint[0] * color[q];
+                               sy[q] += relativepoint[1] * color[q];
+                               sz[q] += relativepoint[2] * color[q];
+                               sd[q] += intensity * relativepoint[q];
+                       }
                }
        }
 
-       // calculate the direction we'll use to reduce the sample to a directional light source
-       VectorCopy(sample + 12, dir);
-       //VectorSet(dir, sample[3] + sample[4] + sample[5], sample[6] + sample[7] + sample[8], sample[9] + sample[10] + sample[11]);
-       VectorNormalize(dir);
-       // extract the diffuse color along the chosen direction and scale it
-       diffuse[0] = (dir[0]*sample[3] + dir[1]*sample[6] + dir[2]*sample[ 9] + sample[ 0]);
-       diffuse[1] = (dir[0]*sample[4] + dir[1]*sample[7] + dir[2]*sample[10] + sample[ 1]);
-       diffuse[2] = (dir[0]*sample[5] + dir[1]*sample[8] + dir[2]*sample[11] + sample[ 2]);
-       // subtract some of diffuse from ambient
-       VectorMA(sample, -0.333f, diffuse, ambient);
-       // store the normalized lightdir
-       VectorCopy(dir, lightdir);
+       // calculate the weighted-average light direction (bentnormal)
+       for (q = 0; q < 3; q++)
+               lightdir[q] = sd[q];
+       VectorNormalize(lightdir);
+       for (q = 0; q < 3; q++)
+       {
+               // extract the diffuse color along the chosen direction and scale it
+               diffuse[q] = (lightdir[0] * sx[q] + lightdir[1] * sy[q] + lightdir[2] * sz[q]);
+               // subtract some of diffuse from ambient
+               ambient[q] = sa[q] + -0.333f * diffuse[q] + ambientintensity;
+       }
 }