+static void R_Shadow_UpdateBounceGridTexture(void)
+{
+#define MAXBOUNCEGRIDPARTICLESPERLIGHT 1048576
+ dlight_t *light;
+ int flag = r_refdef.scene.rtworld ? LIGHTFLAG_REALTIMEMODE : LIGHTFLAG_NORMALMODE;
+ int bouncecount;
+ int c[3];
+ int hitsupercontentsmask;
+ int maxbounce;
+ int numpixels;
+ int resolution[3];
+ int shootparticles;
+ int shotparticles;
+ int photoncount;
+ int tex[3];
+ trace_t cliptrace;
+ //trace_t cliptrace2;
+ //trace_t cliptrace3;
+ unsigned char *pixel;
+ unsigned char *pixels;
+ unsigned short *highpixel;
+ unsigned short *highpixels;
+ unsigned int lightindex;
+ unsigned int range;
+ unsigned int range1;
+ unsigned int range2;
+ unsigned int seed = (unsigned int)(realtime * 1000.0f);
+ vec3_t shotcolor;
+ vec3_t baseshotcolor;
+ vec3_t surfcolor;
+ vec3_t clipend;
+ vec3_t clipstart;
+ vec3_t clipdiff;
+ vec3_t ispacing;
+ vec3_t maxs;
+ vec3_t mins;
+ vec3_t size;
+ vec3_t spacing;
+ vec3_t lightcolor;
+ vec_t radius;
+ vec_t s;
+ vec_t lightintensity;
+ vec_t photonscaling;
+ vec_t photonresidual;
+ float m[16];
+ int texlerp[2][3];
+ int splatcolor[3];
+ int pixelindex[8];
+ int pixelweight[8];
+ int corner;
+ qboolean isstatic = r_shadow_bouncegrid_updateinterval.value > 1.0f;
+ rtlight_t *rtlight;
+ if (!r_shadow_bouncegrid.integer || !vid.support.ext_texture_3d)
+ {
+ if (r_shadow_bouncegridtexture)
+ {
+ R_FreeTexture(r_shadow_bouncegridtexture);
+ r_shadow_bouncegridtexture = NULL;
+ }
+ if (r_shadow_bouncegridpixels)
+ Mem_Free(r_shadow_bouncegridpixels);
+ r_shadow_bouncegridpixels = NULL;
+ if (r_shadow_bouncegridhighpixels)
+ Mem_Free(r_shadow_bouncegridhighpixels);
+ r_shadow_bouncegridhighpixels = NULL;
+ r_shadow_bouncegridnumpixels = 0;
+ return;
+ }
+ if (r_refdef.scene.worldmodel && isstatic)
+ {
+ VectorSet(spacing, bound(1, r_shadow_bouncegrid_spacingx.value, 512), bound(1, r_shadow_bouncegrid_spacingy.value, 512), bound(1, r_shadow_bouncegrid_spacingz.value, 512));
+ VectorMA(r_refdef.scene.worldmodel->normalmins, -2.0f, spacing, mins);
+ VectorMA(r_refdef.scene.worldmodel->normalmaxs, 2.0f, spacing, maxs);
+ VectorSubtract(maxs, mins, size);
+ resolution[0] = (int)floor(size[0] / spacing[0] + 0.5f);
+ resolution[1] = (int)floor(size[1] / spacing[1] + 0.5f);
+ resolution[2] = (int)floor(size[2] / spacing[2] + 0.5f);
+ resolution[0] = min(resolution[0], bound(4, r_shadow_bouncegrid_x.integer, (int)vid.maxtexturesize_3d));
+ resolution[1] = min(resolution[1], bound(4, r_shadow_bouncegrid_y.integer, (int)vid.maxtexturesize_3d));
+ resolution[2] = min(resolution[2], bound(4, r_shadow_bouncegrid_z.integer, (int)vid.maxtexturesize_3d));
+ spacing[0] = size[0] / resolution[0];
+ spacing[1] = size[1] / resolution[1];
+ spacing[2] = size[2] / resolution[2];
+ ispacing[0] = 1.0f / spacing[0];
+ ispacing[1] = 1.0f / spacing[1];
+ ispacing[2] = 1.0f / spacing[2];
+ }
+ else
+ {
+ VectorSet(resolution, bound(4, r_shadow_bouncegrid_x.integer, (int)vid.maxtexturesize_3d), bound(4, r_shadow_bouncegrid_y.integer, (int)vid.maxtexturesize_3d), bound(4, r_shadow_bouncegrid_z.integer, (int)vid.maxtexturesize_3d));
+ VectorSet(spacing, bound(1, r_shadow_bouncegrid_spacingx.value, 512), bound(1, r_shadow_bouncegrid_spacingy.value, 512), bound(1, r_shadow_bouncegrid_spacingz.value, 512));
+ VectorMultiply(resolution, spacing, size);
+ ispacing[0] = 1.0f / spacing[0];
+ ispacing[1] = 1.0f / spacing[1];
+ ispacing[2] = 1.0f / spacing[2];
+ mins[0] = floor(r_refdef.view.origin[0] * ispacing[0] + 0.5f) * spacing[0] - 0.5f * size[0];
+ mins[1] = floor(r_refdef.view.origin[1] * ispacing[1] + 0.5f) * spacing[1] - 0.5f * size[1];
+ mins[2] = floor(r_refdef.view.origin[2] * ispacing[2] + 0.5f) * spacing[2] - 0.5f * size[2];
+ VectorAdd(mins, size, maxs);
+ }
+ r_shadow_bouncegridintensity = r_shadow_bouncegrid_intensity.value;
+ if (r_shadow_bouncegridtexture && realtime < r_shadow_bouncegridtime + r_shadow_bouncegrid_updateinterval.value && resolution[0] == r_shadow_bouncegridresolution[0] && resolution[1] == r_shadow_bouncegridresolution[1] && resolution[2] == r_shadow_bouncegridresolution[2])
+ return;
+ // we're going to update the bouncegrid, update the matrix...
+ memset(m, 0, sizeof(m));
+ m[0] = 1.0f / size[0];
+ m[3] = -mins[0] * m[0];
+ m[5] = 1.0f / size[1];
+ m[7] = -mins[1] * m[5];
+ m[10] = 1.0f / size[2];
+ m[11] = -mins[2] * m[10];
+ m[15] = 1.0f;
+ Matrix4x4_FromArrayFloatD3D(&r_shadow_bouncegridmatrix, m);
+ numpixels = resolution[0]*resolution[1]*resolution[2];
+ // reallocate pixels for this update if needed...
+ if (r_shadow_bouncegridnumpixels != numpixels || !r_shadow_bouncegridpixels || !r_shadow_bouncegridhighpixels)
+ {
+ r_shadow_bouncegridpixels = (unsigned char *)Mem_Realloc(r_main_mempool, r_shadow_bouncegridpixels, numpixels * sizeof(unsigned char[4]));
+ r_shadow_bouncegridhighpixels = (unsigned short *)Mem_Realloc(r_main_mempool, r_shadow_bouncegridhighpixels, numpixels * sizeof(unsigned short[4]));
+ }
+ r_shadow_bouncegridnumpixels = numpixels;
+ pixels = r_shadow_bouncegridpixels;
+ highpixels = r_shadow_bouncegridhighpixels;
+ memset(pixels, 0, numpixels * sizeof(unsigned char[4]));
+ memset(highpixels, 0, numpixels * sizeof(unsigned short[3]));
+ // figure out what we want to interact with
+ if (r_shadow_bouncegrid_hitmodels.integer)
+ hitsupercontentsmask = SUPERCONTENTS_SOLID | SUPERCONTENTS_BODY | SUPERCONTENTS_LIQUIDSMASK;
+ else
+ hitsupercontentsmask = SUPERCONTENTS_SOLID | SUPERCONTENTS_LIQUIDSMASK;
+ maxbounce = bound(1, r_shadow_bouncegrid_maxbounce.integer, 16);
+ // iterate world rtlights
+ range = Mem_ExpandableArray_IndexRange(&r_shadow_worldlightsarray); // checked
+ range1 = isstatic ? 0 : r_refdef.scene.numlights;
+ range2 = range + range1;
+ photoncount = 0;
+ for (lightindex = 0;lightindex < range2;lightindex++)
+ {
+ if (isstatic)
+ {
+ light = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
+ if (!light || !(light->flags & flag))
+ continue;
+ rtlight = &light->rtlight;
+ // when static, we skip styled lights because they tend to change...
+ if (rtlight->style > 0)
+ continue;
+ VectorScale(rtlight->color, (rtlight->ambientscale + rtlight->diffusescale + rtlight->specularscale) * (rtlight->style >= 0 ? r_refdef.scene.rtlightstylevalue[rtlight->style] : 1), lightcolor);
+ }
+ else
+ {
+ if (lightindex < range)
+ {
+ light = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
+ rtlight = &light->rtlight;
+ }
+ else
+ rtlight = r_refdef.scene.lights[lightindex - range];
+ // draw only visible lights (major speedup)
+ if (!rtlight->draw)
+ continue;
+ VectorScale(rtlight->currentcolor, rtlight->ambientscale + rtlight->diffusescale + rtlight->specularscale, lightcolor);
+ }
+ if (!VectorLength2(lightcolor))
+ continue;
+ // shoot particles from this light
+ // use a calculation for the number of particles that will not
+ // vary with lightstyle, otherwise we get randomized particle
+ // distribution, the seeded random is only consistent for a
+ // consistent number of particles on this light...
+ radius = rtlight->radius * bound(0.0001f, r_shadow_bouncegrid_lightradiusscale.value, 1024.0f);
+ s = rtlight->radius;
+ lightintensity = VectorLength(rtlight->color) * (rtlight->ambientscale + rtlight->diffusescale + rtlight->specularscale);
+ if (lightindex >= range)
+ lightintensity *= r_shadow_bouncegrid_dlightparticlemultiplier.value;
+ photoncount += max(0.0f, lightintensity * s * s);
+ }
+ photonscaling = bound(1, r_shadow_bouncegrid_photons.value, 1048576) / max(1, photoncount);
+ photonresidual = 0.0f;
+ for (lightindex = 0;lightindex < range2;lightindex++)
+ {
+ if (isstatic)
+ {
+ light = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
+ if (!light || !(light->flags & flag))
+ continue;
+ rtlight = &light->rtlight;
+ // when static, we skip styled lights because they tend to change...
+ if (rtlight->style > 0)
+ continue;
+ VectorScale(rtlight->color, (rtlight->ambientscale + rtlight->diffusescale + rtlight->specularscale) * (rtlight->style >= 0 ? r_refdef.scene.rtlightstylevalue[rtlight->style] : 1), lightcolor);
+ }
+ else
+ {
+ if (lightindex < range)
+ {
+ light = (dlight_t *) Mem_ExpandableArray_RecordAtIndex(&r_shadow_worldlightsarray, lightindex);
+ rtlight = &light->rtlight;
+ }
+ else
+ rtlight = r_refdef.scene.lights[lightindex - range];
+ // draw only visible lights (major speedup)
+ if (!rtlight->draw)
+ continue;
+ VectorScale(rtlight->currentcolor, rtlight->ambientscale + rtlight->diffusescale + rtlight->specularscale, lightcolor);
+ }
+ if (!VectorLength2(lightcolor))
+ continue;
+ // shoot particles from this light
+ // use a calculation for the number of particles that will not
+ // vary with lightstyle, otherwise we get randomized particle
+ // distribution, the seeded random is only consistent for a
+ // consistent number of particles on this light...
+ radius = rtlight->radius * bound(0.0001f, r_shadow_bouncegrid_lightradiusscale.value, 1024.0f);
+ s = rtlight->radius;
+ lightintensity = VectorLength(rtlight->color) * (rtlight->ambientscale + rtlight->diffusescale + rtlight->specularscale);
+ if (lightindex >= range)
+ lightintensity *= r_shadow_bouncegrid_dlightparticlemultiplier.value;
+ photonresidual += lightintensity * s * s * photonscaling;
+ shootparticles = (int)bound(0, photonresidual, MAXBOUNCEGRIDPARTICLESPERLIGHT);
+ if (!shootparticles)
+ continue;
+ photonresidual -= shootparticles;
+ s = 65535.0f * r_shadow_bouncegrid_particleintensity.value / shootparticles;
+ VectorScale(lightcolor, s, baseshotcolor);
+ if (VectorLength2(baseshotcolor) < 3.0f)
+ break;
+ r_refdef.stats.bouncegrid_lights++;
+ r_refdef.stats.bouncegrid_particles += shootparticles;
+ for (shotparticles = 0;shotparticles < shootparticles;shotparticles++)
+ {
+ if (r_shadow_bouncegrid_stablerandom.integer > 0)
+ seed = lightindex * 11937 + shotparticles;
+ VectorCopy(baseshotcolor, shotcolor);
+ VectorCopy(rtlight->shadoworigin, clipstart);
+ if (r_shadow_bouncegrid_stablerandom.integer < 0)
+ VectorRandom(clipend);
+ else
+ VectorCheeseRandom(clipend);
+ VectorMA(clipstart, radius, clipend, clipend);
+ for (bouncecount = 0;;bouncecount++)
+ {
+ r_refdef.stats.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);
+ cliptrace = CL_TraceLine(clipstart, clipend, r_shadow_bouncegrid_hitmodels.integer ? MOVE_HITMODEL : MOVE_NOMONSTERS, NULL, hitsupercontentsmask, true, false, NULL, true, true);
+ //Collision_ClipLineToWorld(&cliptrace, cl.worldmodel, clipstart, clipend, hitsupercontentsmask);
+ if (cliptrace.fraction >= 1.0f)
+ break;
+ r_refdef.stats.bouncegrid_hits++;
+ if (bouncecount > 0)
+ {
+ r_refdef.stats.bouncegrid_splats++;
+ // figure out which texture pixel this is in
+ texlerp[1][0] = (int)(((cliptrace.endpos[0] - mins[0]) * ispacing[0]) * 256.0f);
+ texlerp[1][1] = (int)(((cliptrace.endpos[1] - mins[1]) * ispacing[1]) * 256.0f);
+ texlerp[1][2] = (int)(((cliptrace.endpos[2] - mins[2]) * ispacing[2]) * 256.0f);
+ tex[0] = texlerp[1][0] >> 8;
+ tex[1] = texlerp[1][1] >> 8;
+ tex[2] = texlerp[1][2] >> 8;
+ if (tex[0] >= 1 && tex[1] >= 1 && tex[2] >= 1 && tex[0] < resolution[0] - 2 && tex[1] < resolution[1] - 2 && tex[2] < resolution[2] - 2)
+ {
+ // it is within bounds...
+ splatcolor[0] = (int)shotcolor[2];
+ splatcolor[1] = (int)shotcolor[1];
+ splatcolor[2] = (int)shotcolor[0];
+ // calculate the lerp factors
+ if (r_shadow_bouncegrid_nolerpsplat.integer)
+ {
+ pixelindex[0] = (((tex[2] )*resolution[1]+tex[1] )*resolution[0]+tex[0] );
+ pixel = pixels + 4 * pixelindex[0];
+ highpixel = highpixels + 3 * pixelindex[0];
+ // add to the high precision pixel color
+ c[0] = highpixel[0] + splatcolor[0];
+ c[1] = highpixel[1] + splatcolor[1];
+ c[2] = highpixel[2] + splatcolor[2];
+ highpixel[0] = (unsigned short)min(c[0], 65535);
+ highpixel[1] = (unsigned short)min(c[1], 65535);
+ highpixel[2] = (unsigned short)min(c[2], 65535);
+ // update the low precision pixel color
+ pixel[0] = highpixel[0] >> 8;
+ pixel[1] = highpixel[1] >> 8;
+ pixel[2] = highpixel[2] >> 8;
+ pixel[3] = 255;
+ }
+ else
+ {
+ texlerp[1][0] &= 0xFF;
+ texlerp[1][1] &= 0xFF;
+ texlerp[1][2] &= 0xFF;
+ texlerp[0][0] = 256 - texlerp[1][0];
+ texlerp[0][1] = 256 - texlerp[1][1];
+ texlerp[0][2] = 256 - texlerp[1][2];
+ // calculate individual pixel indexes and weights
+ pixelindex[0] = (((tex[2] )*resolution[1]+tex[1] )*resolution[0]+tex[0] );pixelweight[0] = (texlerp[0][0]*texlerp[0][1]*texlerp[0][2]) >> 16;
+ pixelindex[1] = (((tex[2] )*resolution[1]+tex[1] )*resolution[0]+tex[0]+1);pixelweight[1] = (texlerp[1][0]*texlerp[0][1]*texlerp[0][2]) >> 16;
+ pixelindex[2] = (((tex[2] )*resolution[1]+tex[1]+1)*resolution[0]+tex[0] );pixelweight[2] = (texlerp[0][0]*texlerp[1][1]*texlerp[0][2]) >> 16;
+ pixelindex[3] = (((tex[2] )*resolution[1]+tex[1]+1)*resolution[0]+tex[0]+1);pixelweight[3] = (texlerp[1][0]*texlerp[1][1]*texlerp[0][2]) >> 16;
+ pixelindex[4] = (((tex[2]+1)*resolution[1]+tex[1] )*resolution[0]+tex[0] );pixelweight[4] = (texlerp[0][0]*texlerp[0][1]*texlerp[1][2]) >> 16;
+ pixelindex[5] = (((tex[2]+1)*resolution[1]+tex[1] )*resolution[0]+tex[0]+1);pixelweight[5] = (texlerp[1][0]*texlerp[0][1]*texlerp[1][2]) >> 16;
+ pixelindex[6] = (((tex[2]+1)*resolution[1]+tex[1]+1)*resolution[0]+tex[0] );pixelweight[6] = (texlerp[0][0]*texlerp[1][1]*texlerp[1][2]) >> 16;
+ pixelindex[7] = (((tex[2]+1)*resolution[1]+tex[1]+1)*resolution[0]+tex[0]+1);pixelweight[7] = (texlerp[1][0]*texlerp[1][1]*texlerp[1][2]) >> 16;
+ // update the 8 pixels...
+ for (corner = 0;corner < 8;corner++)
+ {
+ pixel = pixels + 4 * pixelindex[corner];
+ highpixel = highpixels + 3 * pixelindex[corner];
+ // add to the high precision pixel color
+ c[0] = highpixel[0] + ((splatcolor[0]*pixelweight[corner])>>8);
+ c[1] = highpixel[1] + ((splatcolor[1]*pixelweight[corner])>>8);
+ c[2] = highpixel[2] + ((splatcolor[2]*pixelweight[corner])>>8);
+ highpixel[0] = (unsigned short)min(c[0], 65535);
+ highpixel[1] = (unsigned short)min(c[1], 65535);
+ highpixel[2] = (unsigned short)min(c[2], 65535);
+ // update the low precision pixel color
+ pixel[0] = highpixel[0] >> 8;
+ pixel[1] = highpixel[1] >> 8;
+ pixel[2] = highpixel[2] >> 8;
+ pixel[3] = 255;
+ }
+ }
+ }
+ }
+ if (bouncecount >= maxbounce)
+ break;
+ // scale down shot color by bounce intensity and texture color (or 50% if no texture reported)
+ // also clamp the resulting color to never add energy, even if the user requests extreme values
+ if (cliptrace.hittexture && cliptrace.hittexture->currentskinframe)
+ VectorCopy(cliptrace.hittexture->currentskinframe->avgcolor, surfcolor);
+ else
+ VectorSet(surfcolor, 0.5f, 0.5f, 0.5f);
+ VectorScale(surfcolor, r_shadow_bouncegrid_particlebounceintensity.value, surfcolor);
+ surfcolor[0] = min(surfcolor[0], 1.0f);
+ surfcolor[1] = min(surfcolor[1], 1.0f);
+ surfcolor[2] = min(surfcolor[2], 1.0f);
+ VectorMultiply(shotcolor, surfcolor, shotcolor);
+ if (VectorLength2(shotcolor) < 3.0f)
+ break;
+ r_refdef.stats.bouncegrid_bounces++;
+ if (r_shadow_bouncegrid_bounceanglediffuse.integer)
+ {
+ // random direction, primarily along plane normal
+ s = VectorDistance(cliptrace.endpos, clipend);
+ if (r_shadow_bouncegrid_stablerandom.integer < 0)
+ VectorRandom(clipend);
+ else
+ VectorCheeseRandom(clipend);
+ VectorMA(cliptrace.plane.normal, 0.95f, clipend, clipend);
+ VectorNormalize(clipend);
+ VectorScale(clipend, s, clipend);
+ }
+ else
+ {
+ // reflect the remaining portion of the line across plane normal
+ VectorSubtract(clipend, cliptrace.endpos, clipdiff);
+ VectorReflect(clipdiff, 1.0, cliptrace.plane.normal, clipend);
+ }
+ // calculate the new line start and end
+ VectorCopy(cliptrace.endpos, clipstart);
+ VectorAdd(clipstart, clipend, clipend);
+ }
+ }
+ }
+ if (r_shadow_bouncegridtexture && r_shadow_bouncegridresolution[0] == resolution[0] && r_shadow_bouncegridresolution[1] == resolution[1] && r_shadow_bouncegridresolution[2] == resolution[2])
+ R_UpdateTexture(r_shadow_bouncegridtexture, pixels, 0, 0, 0, resolution[0], resolution[1], resolution[2]);
+ else
+ {
+ VectorCopy(resolution, r_shadow_bouncegridresolution);
+ if (r_shadow_bouncegridtexture)
+ R_FreeTexture(r_shadow_bouncegridtexture);
+ r_shadow_bouncegridtexture = R_LoadTexture3D(r_shadow_texturepool, "bouncegrid", resolution[0], resolution[1], resolution[2], pixels, TEXTYPE_BGRA, TEXF_CLAMP | TEXF_ALPHA | TEXF_FORCELINEAR, 0, NULL);
+ }
+ r_shadow_bouncegridtime = realtime;
+}
+