Skip to content

Implement per-kind light budgeting with device capability limits - #2238

Merged
4ian merged 1 commit into
claude/threejs-gdevelop-lighting-3h4nkafrom
claude/threejs-gdevelop-lighting-review-7xhj52
Aug 23, 2026
Merged

Implement per-kind light budgeting with device capability limits#2238
4ian merged 1 commit into
claude/threejs-gdevelop-lighting-3h4nkafrom
claude/threejs-gdevelop-lighting-review-7xhj52

Conversation

@4ian

@4ian 4ian commented Aug 23, 2026

Copy link
Copy Markdown
Collaborator

Summary

This PR refactors the 3D light management system to budget lights separately by kind (point lights, spot lights, spot lights with projected textures) and enforce device-specific hardware limits. This prevents shader compilation failures when exceeding device capabilities.

Key Changes

  • Device Budget Computation: Added computeDeviceLightBudget() function that calculates per-device limits based on WebGL capabilities (texture units, varyings, uniform vectors) rather than using fixed limits.

  • Per-Kind Light Budgeting: Refactored LightManager to maintain separate visible and shadow light lists for each light kind, allowing the budget to be shared intelligently across kinds while keeping individual kind counts stable for shader compilation.

  • Capacity Capping: Introduced makeDeviceCappedCapacity() to create capacities that respect both game-requested maximums and device limits, with dynamic getters to handle runtime changes.

  • Improved CappedLightList:

    • Replaced weight-based system with count-based capacity to prevent shader recompilation from changing light counts
    • Removed __cameraDistance property from light objects (now stored in list entries)
    • Simplified insertion logic with trimToCapacity() method
    • Changed from weighted cost to simple distance-based sorting
  • Light Kind Detection: Added LightManager.getLightKind() to distinguish between point lights, spot lights, and spot lights with projected textures, since three.js compiles these separately into shaders.

  • Logging: Added logger for informing users when device limits are lower than requested.

  • Documentation: Updated parameter descriptions to clarify that light counts are per-kind and subject to device limits.

Notable Implementation Details

  • The device budget is recomputed when a new light kind is first encountered, allowing the budget to shrink and evict lights if necessary
  • Spot lights with projected textures share the tighter of two budgets (count and map count)
  • The system maintains shader stability by keeping the number of lights of each kind constant once a capacity is reached
  • Three.js texture unit and varying constraints are the primary limiting factors on most devices

https://claude.ai/code/session_01GX7gMRj2UUgEFkBV4vpajs

- Evict lights that no longer fit the moment the device budget is shared
  with a new light kind, so a frame never renders over the budget.
- Read the renderer on every budget recomputation instead of capturing it
  once, so a scene created before the renderer exists is not stuck on the
  guaranteed minimums.
- Say in the max lights count descriptions that the caps are per light
  kind and that devices can enforce a lower limit.
- Drop a stale mention of distance inflation for ties in insertByDistance.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01GX7gMRj2UUgEFkBV4vpajs
@4ian
4ian requested a review from a team as a code owner August 23, 2026 15:13
@4ian
4ian changed the base branch from main to claude/threejs-gdevelop-lighting-3h4nka August 23, 2026 15:26
@4ian
4ian merged commit 1a84312 into claude/threejs-gdevelop-lighting-3h4nka Aug 23, 2026
5 checks passed
@4ian
4ian deleted the claude/threejs-gdevelop-lighting-review-7xhj52 branch August 23, 2026 15:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants