Lighting performance tweaks (#3687)

changes:

Lighting overlays are no longer queued if nothing was going to change anyways (lum delta of 0).
Lighting corners now use 4 vars instead of a list to track masters.
This commit is contained in:
Lohikar
2017-10-29 06:29:31 -05:00
committed by Erki
parent e3fa277f1b
commit a20e9ec1bc

View File

@@ -6,8 +6,15 @@
// This list is what the code that assigns corners listens to, the order in this list is the order in which corners are added to the /turf/corners list.
/var/list/LIGHTING_CORNER_DIAGONAL = list(NORTHEAST, SOUTHEAST, SOUTHWEST, NORTHWEST)
// This is the reverse of the above - the position in the array is a dir. Update this if the above changes.
var/list/REVERSE_LIGHTING_CORNER_DIAGONAL = list(0, 0, 0, 0, 3, 4, 0, 0, 2, 1)
/datum/lighting_corner
var/list/turf/masters
var/turf/t1 // These are in no particular order.
var/turf/t2
var/turf/t3
var/turf/t4
var/list/datum/light_source/affecting // Light sources affecting us.
var/active = FALSE // TRUE if one of our masters has dynamic lighting.
@@ -28,13 +35,9 @@
var/cache_mx = 0
/datum/lighting_corner/New(var/turf/new_turf, var/diagonal)
. = ..()
SSlighting.lighting_corners += src
masters = list()
masters[new_turf] = turn(diagonal, 180)
t1 = new_turf
z = new_turf.z
var/vertical = diagonal & ~(diagonal - 1) // The horizontal directions (4 and 8) are bigger than the vertical ones (1 and 2), so we can reliably say the lsb is the horizontal direction.
@@ -55,8 +58,8 @@
if (!T.corners)
T.corners = list(null, null, null, null)
masters[T] = diagonal
i = LIGHTING_CORNER_DIAGONAL.Find(turn(diagonal, 180))
t2 = T
i = REVERSE_LIGHTING_CORNER_DIAGONAL[diagonal]
T.corners[i] = src
// Now the horizontal one.
@@ -65,8 +68,8 @@
if (!T.corners)
T.corners = list(null, null, null, null)
masters[T] = ((T.x > x) ? EAST : WEST) | ((T.y > y) ? NORTH : SOUTH) // Get the dir based on coordinates.
i = LIGHTING_CORNER_DIAGONAL.Find(turn(masters[T], 180))
t3 = T
i = REVERSE_LIGHTING_CORNER_DIAGONAL[((T.x > x) ? EAST : WEST) | ((T.y > y) ? NORTH : SOUTH)] // Get the dir based on coordinates.
T.corners[i] = src
// And finally the vertical one.
@@ -75,21 +78,21 @@
if (!T.corners)
T.corners = list(null, null, null, null)
masters[T] = ((T.x > x) ? EAST : WEST) | ((T.y > y) ? NORTH : SOUTH) // Get the dir based on coordinates.
i = LIGHTING_CORNER_DIAGONAL.Find(turn(masters[T], 180))
t4 = T
i = REVERSE_LIGHTING_CORNER_DIAGONAL[((T.x > x) ? EAST : WEST) | ((T.y > y) ? NORTH : SOUTH)] // Get the dir based on coordinates.
T.corners[i] = src
update_active()
#define OVERLAY_PRESENT(T) (T && T.lighting_overlay)
/datum/lighting_corner/proc/update_active()
active = FALSE
var/turf/T
var/thing
for (thing in masters)
T = thing
if (T.lighting_overlay)
active = TRUE
break
if (OVERLAY_PRESENT(t1) || OVERLAY_PRESENT(t2) || OVERLAY_PRESENT(t3) || OVERLAY_PRESENT(t4))
active = TRUE
#undef OVERLAY_PRESENT
// God that was a mess, now to do the rest of the corner code! Hooray!
/datum/lighting_corner/proc/update_lumcount(var/delta_r, var/delta_g, var/delta_b, var/delta_u, var/now = FALSE)
@@ -98,7 +101,7 @@
lum_b += delta_b
lum_u += delta_u
if (needs_update)
if (needs_update || !(delta_r + delta_g + delta_b)) // Don't check u since the overlay doesn't care about it.
return
if (!now)
@@ -108,8 +111,18 @@
else
update_overlays(TRUE)
/datum/lighting_corner/proc/update_overlays(var/now = FALSE)
#define UPDATE_MASTER(T) \
if (T && T.lighting_overlay) { \
if (now) { \
T.lighting_overlay.update_overlay(); \
} \
else if (!T.lighting_overlay.needs_update) { \
T.lighting_overlay.needs_update = TRUE; \
SSlighting.overlay_queue += T.lighting_overlay; \
} \
}
/datum/lighting_corner/proc/update_overlays(var/now = FALSE)
// Cache these values a head of time so 4 individual lighting overlays don't all calculate them individually.
var/mx = max(lum_r, lum_g, lum_b) // Scale it so 1 is the strongest lum, if it is above 1.
. = 1 // factor
@@ -124,14 +137,12 @@
cache_b = round(lum_b * ., LIGHTING_ROUND_VALUE) || LIGHTING_SOFT_THRESHOLD
cache_mx = round(mx, LIGHTING_ROUND_VALUE)
for (var/TT in masters)
var/turf/T = TT
if (T.lighting_overlay)
if (now)
T.lighting_overlay.update_overlay()
else if (!T.lighting_overlay.needs_update)
T.lighting_overlay.needs_update = TRUE
SSlighting.overlay_queue += T.lighting_overlay
UPDATE_MASTER(t1)
UPDATE_MASTER(t2)
UPDATE_MASTER(t3)
UPDATE_MASTER(t4)
#undef UPDATE_MASTER
/datum/lighting_corner/Destroy(force = FALSE)
crash_with("Some fuck [force ? "force-" : ""]deleted a lighting corner.")