Non-casters still undergo each directional-shadow cascade test

perfloop-oss/godot · Inefficient Algorithm

https://perfloop.ai/t/oss/case_fhcpqx3j0y

Verdict

VERIFIED · settled 2026-10-25

What happened: The paired measurements met the required improvement; the proof comes from an earlier attempt and still applies to this one.

Hypothesis

Instances that cannot cast directional shadows still undergo spatial tests for each active cascade before their eligibility is checked. Removing those tests may help camera rendering with many non-casting objects, even when all objects are inside the camera view. The removable CPU share and prevalence of such scenes remain unmeasured.

render_camera reaches _render_scene, which gathers visible shadow-enabled directional lights only when shadows and a valid shadow atlas are available. _light_instance_setup_directional_shadow selects one, two, or four cascades per light. For each non-hidden instance that reaches _scene_cull's loops at lines 3267–3282, cull_directional_light is called once per configured cascade before the geometry-type, FLAG_CAST_SHADOWS, and layer/caster-mask checks. Its normal active path projects bounds against prepared planes until rejection; survivors also undergo InstanceBounds::in_frustum. With no active shadow cascades, this work is absent. These counts describe reached loop bodies, not invocations that return early.

A blanket reorder would not preserve all behavior. VIS_CHECK can call _visibility_range_check<false>, which updates viewport_state and affects visibility-range hysteresis. The initial fast path must retain legacy traversal when that stateful range check can run. cull_directional_light is otherwise read-only in normal builds, but its optional directional rejection counter also requires keeping the legacy diagnostic path. Existing inactive-particle handling and result insertion order remain unchanged.

Low confidence from source. The authored viewport CPU-profile attempt produced no receipt because `scons` was missing. The smallest Case experiment is bounded eligibility gating with a counter for avoided directional-cull calls. Sweep instance count, non-caster fraction, light count, and split count, measuring complete cull CPU including the added predicates. Compare each cascade's caster IDs, mesh-update tracking, and visibility-range viewport_state at transition boundaries. Unchanged call counts or no CPU saving would reject the performance direction; changed membership or hysteresis would reject the implementation.

Change to test: Apply the existing geometry-type and cast-shadow exclusions before directional-shadow traversal, and the existing layer/caster-mask exclusion before each light's cascade loop. Retain the old ordering where VIS_CHECK can update visibility-range state and in rejection-count debug builds, preserving caster lists, mesh-update tracking, and diagnostic behavior.

Where it lives

perfloop-oss/godot · servers/rendering/renderer_scene_cull.cpp

Evidence

camera scene-culling CPU per pass · 11 sample pairs

metric baseline candidate paired median change confidence range required result
scene_cull_process_cpu_ms_per_pass 5.039 1.094 −79.8% (−4.023) −6.641 to −2.773 gain > 0; gain > 0.05 × |baseline median|; arm medians separated PASSED

Timeline