Properly transform light rect and occluder rect to perform Light2D culling in canvas space #100677
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes: #100664
There were two problems reported by #100664:
The crash was caused by incrementing the occluder count after checking if the occluder had a valid polygon. Later, we copy over the transform for each occluder regardless of whether the polygon exists, so the occluder count needs to increment for each occluder, not each valid occluder.
The shadows not appearing came from false negatives when culling occluders against individual lights. The culling was done using the rect of the light against the aabb (rect) of the occluder which are both in their own local spaces. If the item transforms were similar, then this didn't create any issues. But when the transforms differ enough, then the culling fails when it shouldn't. The solution was to transform both rects into canvas space and do culling in canvas space. For the
light->rect_cache
I was able to do the transformation early and save a bunch of transformations later (we were doing the transformation 2 times for each light + once for each light for each canvasitem on screen), now we do it only once per frame per light).This further optimizes the CPU cost of using PointLight2Ds. On my M2 MBP, this increases the FPS in a release build of my light performance benchmark from 550 FPS to 650 FPS and from 400 FPS to 500 FPS in dev builds. The benchmark is from #100302