Repository: godotengine/godot
File: servers/rendering/rendering_light_culler.cpp (C++)

Code before:
			n--;

#ifdef LIGHT_CULLER_DEBUG_REGULAR_LIGHT
			data.regular_rejected_count++;
#endif
		}
	}

#ifdef LIGHT_CULLER_DEBUG_LOGGING
	uint32_t removed = r_instance_shadow_cull_result.size() - count_before;
	if (removed) {
		if (((data.debug_count) % 60) == 0) {
			print_line("[" + itos(data.debug_count) + "] linear cull before " + itos(count_before) + " after " + itos(r_instance_shadow_cull_result.size()));
		}
	}
#endif
}

void RenderingLightCuller::LightCullPlanes::add_cull_plane(const Plane &p) {
	ERR_FAIL_COND(num_cull_planes >= MAX_CULL_PLANES);
	cull_planes[num_cull_planes++] = p;
}

// Directional lights are different to points, as the origin is infinitely in the distance, so the plane third
// points are derived differently.
bool RenderingLightCuller::add_light_camera_planes_directional(LightCullPlanes &r_cull_planes, const LightSource &p_light_source, const CullParameters &params) {
	uint32_t lookup = 0;
	r_cull_planes.num_cull_planes = 0;

	const Plane *const cull_frustum_planes = params.frustum_planes;

	// Directional light, we will use dot against the light direction to determine back facing planes.
	for (int n = 0; n < 6; n++) {
		float dot = cull_frustum_planes[n].normal.dot(p_light_source.dir);
		if (dot > 0.0f) {
			lookup |= 1 << n;

			// Add backfacing camera frustum planes.
			r_cull_planes.add_cull_plane(cull_frustum_planes[n]);
		}
	}

	ERR_FAIL_COND_V(lookup >= LUT_SIZE, true);

	// Deal with special case... if the light is INSIDE the view frustum (i.e. all planes face away)
	// then we will add the camera frustum planes to clip the light volume .. there is no need to
	// render shadow casters outside the frustum as shadows can never re-enter the frustum.

	// Should never happen with directional light?? This may be able to be removed.
	if (lookup == 63) {
		r_cull_planes.num_cull_planes = 0;

Review comment (suggestion):
```suggestion
bool RenderingLightCuller::add_light_camera_planes_directional(LightCullPlanes &r_cull_planes, const LightSource &p_light_source, const CullParameters &p_params) {
```

Code after:
		if (!show) {
			list.remove_at_unordered(n);

			// Repeat this element next iteration of the loop as it has been removed and replaced by the last.
			n--;

#ifdef LIGHT_CULLER_DEBUG_REGULAR_LIGHT
			data.regular_rejected_count++;
#endif
		}
	}

#ifdef LIGHT_CULLER_DEBUG_LOGGING
	uint32_t removed = r_instance_shadow_cull_result.size() - count_before;
	if (removed) {
		if (((data.debug_count) % 60) == 0) {
			print_line("[" + itos(data.debug_count) + "] linear cull before " + itos(count_before) + " after " + itos(r_instance_shadow_cull_result.size()));
		}
	}
#endif
}

void RenderingLightCuller::LightCullPlanes::add_cull_plane(const Plane &p) {
	ERR_FAIL_COND(num_cull_planes >= MAX_CULL_PLANES);
	cull_planes[num_cull_planes++] = p;
}

// Directional lights are different to points, as the origin is infinitely in the distance, so the plane third
// points are derived differently.
bool RenderingLightCuller::add_light_camera_planes_directional(LightCullPlanes &r_cull_planes, const LightSource &p_light_source, const CullFrustumData &p_cull_frustum) {
	uint32_t lookup = 0;
	r_cull_planes.num_cull_planes = 0;

	const Plane *const cull_frustum_planes = p_cull_frustum.frustum_planes;

	// Directional light, we will use dot against the light direction to determine back facing planes.
	for (int n = 0; n < 6; n++) {
		float dot = cull_frustum_planes[n].normal.dot(p_light_source.dir);
		if (dot > 0.0f) {
			lookup |= 1 << n;

			// Add backfacing camera frustum planes.
			r_cull_planes.add_cull_plane(cull_frustum_planes[n]);
		}
	}

	ERR_FAIL_COND_V(lookup >= LUT_SIZE, true);

	// Deal with special case... if the light is INSIDE the view frustum (i.e. all planes face away)
	// then we will add the camera frustum planes to clip the light volume .. there is no need to
	// render shadow casters outside the frustum as shadows can never re-enter the frustum.