From b6f0e9e6f7f3072fd8ae731a88e8c82d952a1b55 Mon Sep 17 00:00:00 2001 From: marat569 Date: Mon, 14 Oct 2024 22:47:07 -0400 Subject: [PATCH] Add Safe Sqrt (#57) --- src/shaders/math.hlsl | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/shaders/math.hlsl b/src/shaders/math.hlsl index a8f25933..590bee69 100644 --- a/src/shaders/math.hlsl +++ b/src/shaders/math.hlsl @@ -40,6 +40,17 @@ POWSAFE_FUNCTION_GENERATOR(float3); POWSAFE_FUNCTION_GENERATOR(float4); #undef POWSAFE_FUNCTION_GENERATOR +#define SQRTSAFE_FUNCTION_GENERATOR(struct) \ + struct SqrtSafe(struct x) { \ + return Sign(x) * sqrt(abs(x)); \ + } + +SQRTSAFE_FUNCTION_GENERATOR(float); +SQRTSAFE_FUNCTION_GENERATOR(float2); +SQRTSAFE_FUNCTION_GENERATOR(float3); +SQRTSAFE_FUNCTION_GENERATOR(float4); +#undef SQRTSAFE_FUNCTION_GENERATOR + float Average(float3 color) { return (color.x + color.y + color.z) / 3.f; }