Optimize BoundingSphere.Intersects to be inlined by the compiler

This commit is contained in:
Wojtek Figat
2024-04-11 15:58:34 +02:00
parent 890b2da108
commit 4e65b76b8c
2 changed files with 8 additions and 4 deletions

View File

@@ -51,7 +51,11 @@ bool BoundingSphere::Intersects(const BoundingBox& box) const
bool BoundingSphere::Intersects(const BoundingSphere& sphere) const bool BoundingSphere::Intersects(const BoundingSphere& sphere) const
{ {
return CollisionsHelper::SphereIntersectsSphere(*this, sphere); const Real radiisum = Radius + sphere.Radius;
const Real x = Center.X - sphere.Center.X;
const Real y = Center.Y - sphere.Center.Y;
const Real z = Center.Z - sphere.Center.Z;
return x * x + y * y + z * z <= radiisum * radiisum;
} }
ContainmentType BoundingSphere::Contains(const Vector3& point) const ContainmentType BoundingSphere::Contains(const Vector3& point) const

View File

@@ -76,7 +76,7 @@ void ForwardShadingFeature::Bind(MaterialShader::BindParameters& params, Span<by
for (int32 i = 0; i < cache->EnvironmentProbes.Count(); i++) for (int32 i = 0; i < cache->EnvironmentProbes.Count(); i++)
{ {
const RenderEnvironmentProbeData& probe = cache->EnvironmentProbes.Get()[i]; const RenderEnvironmentProbeData& probe = cache->EnvironmentProbes.Get()[i];
if (CollisionsHelper::SphereIntersectsSphere(objectBounds, BoundingSphere(probe.Position, probe.Radius))) if (objectBounds.Intersects(BoundingSphere(probe.Position, probe.Radius)))
{ {
noEnvProbe = false; noEnvProbe = false;
probe.SetShaderData(data.EnvironmentProbe); probe.SetShaderData(data.EnvironmentProbe);
@@ -96,7 +96,7 @@ void ForwardShadingFeature::Bind(MaterialShader::BindParameters& params, Span<by
for (int32 i = 0; i < cache->PointLights.Count() && data.LocalLightsCount < MaxLocalLights; i++) for (int32 i = 0; i < cache->PointLights.Count() && data.LocalLightsCount < MaxLocalLights; i++)
{ {
const auto& light = cache->PointLights[i]; const auto& light = cache->PointLights[i];
if (CollisionsHelper::SphereIntersectsSphere(objectBounds, BoundingSphere(light.Position, light.Radius))) if (objectBounds.Intersects(BoundingSphere(light.Position, light.Radius)))
{ {
light.SetShaderData(data.LocalLights[data.LocalLightsCount], false); light.SetShaderData(data.LocalLights[data.LocalLightsCount], false);
data.LocalLightsCount++; data.LocalLightsCount++;
@@ -105,7 +105,7 @@ void ForwardShadingFeature::Bind(MaterialShader::BindParameters& params, Span<by
for (int32 i = 0; i < cache->SpotLights.Count() && data.LocalLightsCount < MaxLocalLights; i++) for (int32 i = 0; i < cache->SpotLights.Count() && data.LocalLightsCount < MaxLocalLights; i++)
{ {
const auto& light = cache->SpotLights[i]; const auto& light = cache->SpotLights[i];
if (CollisionsHelper::SphereIntersectsSphere(objectBounds, BoundingSphere(light.Position, light.Radius))) if (objectBounds.Intersects(BoundingSphere(light.Position, light.Radius)))
{ {
light.SetShaderData(data.LocalLights[data.LocalLightsCount], false); light.SetShaderData(data.LocalLights[data.LocalLightsCount], false);
data.LocalLightsCount++; data.LocalLightsCount++;