R_LightPoint Interpolation Code - Broken?
Moderator: InsideQC Admins
14 posts
• Page 1 of 1
R_LightPoint Interpolation Code - Broken?
This one's just come up with RMQ and is worth sharing.
I suspect that the commonly-used R_LightPoint (strictly speaking, RecursiveLightPoint) "enhanced to interpolate lighting" code is both broken and - potentially - dangerous.
First of all, broken because it can sample from up to 3 points outside of the surface's lightmap data. These would be the 3 points in the lightmap data for the next surface according to the order in which light.exe built the data (in some cases - if the next surface's smax * tmax was lower than the current surface's smax, it may even go beyond that and into the one after). This became obvious when we recently moved to RGB10 lightmaps and LIT files (for 4x the dynamic range of traditional lightmaps) and compiled some test maps - lighting on models as they moved (particularly noticeable with the gun) began to get really really weird, with sudden ultra-bright flashes.
Dangerous because 1 or more of those 3 points may go off the end of the loadmodel->lightdata buffer. With Hunk_Alloc you'll just read into the memory for whatever was allocated next, if you're using a different allocator you may crash.
I suspect that the commonly-used R_LightPoint (strictly speaking, RecursiveLightPoint) "enhanced to interpolate lighting" code is both broken and - potentially - dangerous.
First of all, broken because it can sample from up to 3 points outside of the surface's lightmap data. These would be the 3 points in the lightmap data for the next surface according to the order in which light.exe built the data (in some cases - if the next surface's smax * tmax was lower than the current surface's smax, it may even go beyond that and into the one after). This became obvious when we recently moved to RGB10 lightmaps and LIT files (for 4x the dynamic range of traditional lightmaps) and compiled some test maps - lighting on models as they moved (particularly noticeable with the gun) began to get really really weird, with sudden ultra-bright flashes.
Dangerous because 1 or more of those 3 points may go off the end of the loadmodel->lightdata buffer. With Hunk_Alloc you'll just read into the memory for whatever was allocated next, if you're using a different allocator you may crash.
We had the power, we had the space, we had a sense of time and place
We knew the words, we knew the score, we knew what we were fighting for
We knew the words, we knew the score, we knew what we were fighting for
-

mh - Posts: 2292
- Joined: Sat Jan 12, 2008 1:38 am
Re: R_LightPoint Interpolation Code - Broken?
I can confirm that the interpolation code does read samples outside of the current lightmap. So that indeed could cause a memory access fault.
However when this happens, the fraction for those samples is 0 and hence they never influence the lighting result. So the logic itself is not broken.
However when this happens, the fraction for those samples is 0 and hence they never influence the lighting result. So the logic itself is not broken.
- andrewj
- Posts: 133
- Joined: Mon Aug 30, 2010 3:29 pm
- Location: Australia
Re: R_LightPoint Interpolation Code - Broken?
If this came from fitzquake, i probably wrote this code. There's a lot of code from my first year on that which is pretty newbie stuff now.
There's definitely a bug that sometimes causes models near walls to appear black or very dark; that may be a side effect of the flaw you describe.
There's definitely a bug that sometimes causes models near walls to appear black or very dark; that may be a side effect of the flaw you describe.
- metlslime
- Posts: 316
- Joined: Tue Feb 05, 2008 11:03 pm
Re: R_LightPoint Interpolation Code - Broken?
metlslime wrote:If this came from fitzquake, i probably wrote this code. There's a lot of code from my first year on that which is pretty newbie stuff now.![]()
The interpolation part is by Lord Havoc.
- andrewj
- Posts: 133
- Joined: Mon Aug 30, 2010 3:29 pm
- Location: Australia
Re: R_LightPoint Interpolation Code - Broken?
What is the proposed solution? (I am not much familiar with those parts of the engine.)
- szo
- Posts: 132
- Joined: Mon Dec 06, 2010 4:42 pm
Re: R_LightPoint Interpolation Code - Broken?
i had a few instances where it behaved weirdly but i allways assumed the error was on my part, i newer suspected it might be broken tbh.
Productivity is a state of mind.
-

revelator - Posts: 2567
- Joined: Thu Jan 24, 2008 12:04 pm
- Location: inside tha debugger
Re: R_LightPoint Interpolation Code - Broken?
Is this the animated light styles interpolation stuff or something else?
(I'm thinking it couldn't possibly be that, that's like 3 lines of code.)
(I'm thinking it couldn't possibly be that, that's like 3 lines of code.)
The night is young. How else can I annoy the world before sunsrise?
Inquisitive minds want to know ! And if they don't -- well like that ever has stopped me before ..
-

Baker - Posts: 3666
- Joined: Tue Mar 14, 2006 5:15 am
Re: R_LightPoint Interpolation Code - Broken?
The R_LightPoint interpolation attempts to reduce jerky lighting changes on models, especially as a model moves across an extreme light/dark boundary. This is especially noticeable on the viewmodel. Is there any non-computationally-expensive way to detect if a sample is in-bounds?
Animated light style interpolation is different, but R_LightPoint could use a similar idea, by blending the previous lightpoint value with the new value over several frames. Cheesy, but it takes the edge off transitions and it won't break.
Animated light style interpolation is different, but R_LightPoint could use a similar idea, by blending the previous lightpoint value with the new value over several frames. Cheesy, but it takes the edge off transitions and it won't break.
-
qbism - Posts: 1236
- Joined: Thu Nov 04, 2004 5:51 am
Re: R_LightPoint Interpolation Code - Broken?
blending over time instead of lerping over space sounds good, I like the fact that it means that stationary props get lit the same as in a non-lerping engine (of course enough engines lerp now that there is no way to have consistency across all engines.)
Of course if the blend time you choose is faster than the time it takes to cross a luxel boundary (i.e. slow moving objects), you may see periodic plateaus in the light value, which could look weird.
And for fast moving objects, if the blend time is slow, you'd see lagged lighting -- object will have passed into shadow and retain its bright light for too long.
I wonder if it would be best just to pad all lightmaps with an extra row and column of light data in memory, so that you will never go out of bounds from a lerp.
Of course if the blend time you choose is faster than the time it takes to cross a luxel boundary (i.e. slow moving objects), you may see periodic plateaus in the light value, which could look weird.
And for fast moving objects, if the blend time is slow, you'd see lagged lighting -- object will have passed into shadow and retain its bright light for too long.
I wonder if it would be best just to pad all lightmaps with an extra row and column of light data in memory, so that you will never go out of bounds from a lerp.
- metlslime
- Posts: 316
- Joined: Tue Feb 05, 2008 11:03 pm
Re: R_LightPoint Interpolation Code - Broken?
I wondered where those memory leaks came from in engoo. Now I know!
I use that LH Lightpoint too
I use that LH Lightpoint too
i should not be here
- leileilol
- Posts: 2783
- Joined: Fri Oct 15, 2004 3:23 am
Re: R_LightPoint Interpolation Code - Broken?
metlslime wrote:I wonder if it would be best just to pad all lightmaps with an extra row and column of light data in memory, so that you will never go out of bounds from a lerp.
No need to pad every lightmap, just add an extra 400 samples or so at the end of the lightmap data.
- andrewj
- Posts: 133
- Joined: Mon Aug 30, 2010 3:29 pm
- Location: Australia
Re: R_LightPoint Interpolation Code - Broken?
While I don't know of any bugs in my R_LightPoint interpolation, I do accept the possibility.
By design qbsp ensures that all surfaces have an extra column and row of lightmap data for each surface, this is used for lightmap interpolation in software quake when generating surface cache images from the repeating texture and the lightmap (which is scaled up with interpolation and applied).
This data is conveniently always existing, for any point location within the surface bounds there will be 2x2 pixels to read.
But you are right that the extents checks look bugged, >= extents should probably be used instead of > extents.
The bsp tree imposes many restrictions on what values will occur and how the bug would manifest, so it is hard to easily find a test case to verify a fix, but a fix is warranted.
By design qbsp ensures that all surfaces have an extra column and row of lightmap data for each surface, this is used for lightmap interpolation in software quake when generating surface cache images from the repeating texture and the lightmap (which is scaled up with interpolation and applied).
This data is conveniently always existing, for any point location within the surface bounds there will be 2x2 pixels to read.
But you are right that the extents checks look bugged, >= extents should probably be used instead of > extents.
The bsp tree imposes many restrictions on what values will occur and how the bug would manifest, so it is hard to easily find a test case to verify a fix, but a fix is warranted.
- LordHavoc
- Posts: 322
- Joined: Fri Nov 05, 2004 3:12 am
- Location: western Oregon, USA
Re: R_LightPoint Interpolation Code - Broken?
The bsp tree imposes many restrictions on what values will occur and how the bug would manifest, so it is hard to easily find a test case to verify a fix, but a fix is warranted.
When LH speaks, it's like someone just cut the tail off a puppy-Doberman, sad, but TRUE!
- r00k
- Posts: 1110
- Joined: Sat Nov 13, 2004 10:39 pm
Re: R_LightPoint Interpolation Code - Broken?
Seems I fixed this bug in DP in 2004 but did not realize that the bug dated back to the litsupport sample code.
Here's the svn entry:
r4041 | havoc | 2004-03-18 02:49:02 -0800 (Thu, 18 Mar 2004) | 2 lines
fixed 'black models' bugs in RecursiveLightPoint code
The correct change is just to make the extents checks use >= rather than > as comparison operator.
Here's the svn entry:
r4041 | havoc | 2004-03-18 02:49:02 -0800 (Thu, 18 Mar 2004) | 2 lines
fixed 'black models' bugs in RecursiveLightPoint code
The correct change is just to make the extents checks use >= rather than > as comparison operator.
- LordHavoc
- Posts: 322
- Joined: Fri Nov 05, 2004 3:12 am
- Location: western Oregon, USA
14 posts
• Page 1 of 1
Who is online
Users browsing this forum: No registered users and 1 guest