Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly transform light rect and occluder rect to perform Light2D culling in canvas space #100677

Merged
merged 1 commit into from
Dec 21, 2024

Conversation

clayjohn
Copy link
Member

Fixes: #100664

There were two problems reported by #100664:

  1. Weird crash when adding a light occluder
  2. Shadows not appearing

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

@akien-mga akien-mga changed the title Properly transform light rect and occluder rect to perform light2d culling in canvas space Properly transform light rect and occluder rect to perform Light2D culling in canvas space Dec 21, 2024
@akien-mga akien-mga merged commit f7d6924 into godotengine:master Dec 21, 2024
20 checks passed
@akien-mga
Copy link
Member

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Weird PointLight2D LightOccluder2D behavior and crash
2 participants