+ int smax, tmax, i, j, size, size3, shift, maps, stride, l;
+ unsigned int *bl, scale;
+ qbyte *lightmap, *out, *stain;
+
+ // update cached lighting info
+ surf->cached_dlight = 0;
+ surf->cached_lightmapscalebit = r_lightmapscalebit;
+ surf->cached_ambient = r_ambient.value;
+ surf->cached_light[0] = d_lightstylevalue[surf->styles[0]];
+ surf->cached_light[1] = d_lightstylevalue[surf->styles[1]];
+ surf->cached_light[2] = d_lightstylevalue[surf->styles[2]];
+ surf->cached_light[3] = d_lightstylevalue[surf->styles[3]];
+
+ smax = (surf->extents[0]>>4)+1;
+ tmax = (surf->extents[1]>>4)+1;
+ size = smax*tmax;
+ size3 = size*3;
+ lightmap = surf->samples;
+
+ // set to full bright if no light data
+ bl = intblocklights;
+ if ((ent->effects & EF_FULLBRIGHT) || !ent->model->lightdata)
+ {
+ for (i = 0;i < size3;i++)
+ bl[i] = 255*256;
+ }
+ else
+ {
+ // clear to no light
+ j = r_ambient.value * 512.0f; // would be 128.0f logically, but using 512.0f to match winquake style
+ if (j)
+ {
+ for (i = 0;i < size3;i++)
+ *bl++ = j;
+ }
+ else
+ memset(bl, 0, size*3*sizeof(unsigned int));
+
+ if (surf->dlightframe == r_framecount && r_dlightmap.integer)
+ {
+ surf->cached_dlight = R_IntAddDynamicLights(&ent->inversematrix, surf);
+ if (surf->cached_dlight)
+ c_light_polys++;
+ else if (dlightchanged)
+ return; // don't upload if only updating dlights and none mattered
+ }
+
+ // add all the lightmaps
+ if (lightmap)
+ {
+ bl = intblocklights;
+ for (maps = 0;maps < MAXLIGHTMAPS && surf->styles[maps] != 255;maps++, lightmap += size3)
+ for (scale = d_lightstylevalue[surf->styles[maps]], i = 0;i < size3;i++)
+ bl[i] += lightmap[i] * scale;
+ }
+ }
+
+ stain = surf->stainsamples;
+ bl = intblocklights;
+ out = templight;
+ // deal with lightmap brightness scale
+ shift = 7 + r_lightmapscalebit + 8;
+ if (ent->model->lightmaprgba)