+ // count this light in the r_speeds
+ r_refdef.stats.lights++;
+
+ // flag it as worth drawing later
+ rtlight->draw = true;
+
+ // cache all the animated entities that cast a shadow but are not visible
+ for (i = 0;i < numshadowentities;i++)
+ if (shadowentities[i]->animcacheindex < 0)
+ R_AnimCache_GetEntity(shadowentities[i], false, false);
+ for (i = 0;i < numshadowentities_noselfshadow;i++)
+ if (shadowentities_noselfshadow[i]->animcacheindex < 0)
+ R_AnimCache_GetEntity(shadowentities_noselfshadow[i], false, false);
+
+ // allocate some temporary memory for rendering this light later in the frame
+ // reusable buffers need to be copied, static data can be used as-is
+ rtlight->cached_numlightentities = numlightentities;
+ rtlight->cached_numlightentities_noselfshadow = numlightentities_noselfshadow;
+ rtlight->cached_numshadowentities = numshadowentities;
+ rtlight->cached_numshadowentities_noselfshadow = numshadowentities_noselfshadow;
+ rtlight->cached_numsurfaces = numsurfaces;
+ rtlight->cached_lightentities = (entity_render_t**)R_FrameData_Store(numlightentities*sizeof(entity_render_t*), (void*)lightentities);
+ rtlight->cached_lightentities_noselfshadow = (entity_render_t**)R_FrameData_Store(numlightentities_noselfshadow*sizeof(entity_render_t*), (void*)lightentities_noselfshadow);
+ rtlight->cached_shadowentities = (entity_render_t**)R_FrameData_Store(numshadowentities*sizeof(entity_render_t*), (void*)shadowentities);
+ rtlight->cached_shadowentities_noselfshadow = (entity_render_t**)R_FrameData_Store(numshadowentities_noselfshadow*sizeof(entity_render_t *), (void*)shadowentities_noselfshadow);
+ if (shadowtrispvs == r_shadow_buffer_shadowtrispvs)
+ {
+ rtlight->cached_shadowtrispvs = (unsigned char *)R_FrameData_Store(r_refdef.scene.worldmodel->brush.shadowmesh ? r_refdef.scene.worldmodel->brush.shadowmesh->numtriangles : r_refdef.scene.worldmodel->surfmesh.num_triangles, shadowtrispvs);
+ rtlight->cached_lighttrispvs = (unsigned char *)R_FrameData_Store(r_refdef.scene.worldmodel->surfmesh.num_triangles, lighttrispvs);
+ rtlight->cached_surfacelist = (int*)R_FrameData_Store(numsurfaces*sizeof(int), (void*)surfacelist);
+ }
+ else
+ {
+ // compiled light data
+ rtlight->cached_shadowtrispvs = shadowtrispvs;
+ rtlight->cached_lighttrispvs = lighttrispvs;
+ rtlight->cached_surfacelist = surfacelist;
+ }
+}
+
+void R_DrawRTLight(rtlight_t *rtlight, qboolean visible)
+{
+ int i;
+ int numsurfaces;
+ unsigned char *shadowtrispvs, *lighttrispvs, *surfacesides;
+ int numlightentities;
+ int numlightentities_noselfshadow;
+ int numshadowentities;
+ int numshadowentities_noselfshadow;
+ entity_render_t **lightentities;
+ entity_render_t **lightentities_noselfshadow;
+ entity_render_t **shadowentities;
+ entity_render_t **shadowentities_noselfshadow;
+ int *surfacelist;
+ static unsigned char entitysides[MAX_EDICTS];
+ static unsigned char entitysides_noselfshadow[MAX_EDICTS];
+ vec3_t nearestpoint;
+ vec_t distance;
+ qboolean castshadows;
+ int lodlinear;
+
+ // check if we cached this light this frame (meaning it is worth drawing)
+ if (!rtlight->draw)
+ return;
+
+ // if R_FrameData_Store ran out of space we skip anything dependent on it
+ if (r_framedata_failed)
+ return;
+
+ numlightentities = rtlight->cached_numlightentities;
+ numlightentities_noselfshadow = rtlight->cached_numlightentities_noselfshadow;
+ numshadowentities = rtlight->cached_numshadowentities;
+ numshadowentities_noselfshadow = rtlight->cached_numshadowentities_noselfshadow;
+ numsurfaces = rtlight->cached_numsurfaces;
+ lightentities = rtlight->cached_lightentities;
+ lightentities_noselfshadow = rtlight->cached_lightentities_noselfshadow;
+ shadowentities = rtlight->cached_shadowentities;
+ shadowentities_noselfshadow = rtlight->cached_shadowentities_noselfshadow;
+ shadowtrispvs = rtlight->cached_shadowtrispvs;
+ lighttrispvs = rtlight->cached_lighttrispvs;
+ surfacelist = rtlight->cached_surfacelist;
+
+ // set up a scissor rectangle for this light
+ if (R_Shadow_ScissorForBBox(rtlight->cached_cullmins, rtlight->cached_cullmaxs))
+ return;
+