Skip to content

Commit 7b5eec0

Browse files
committed
Add saturate() function
* Also min and max IOR.
1 parent a125f4a commit 7b5eec0

6 files changed

Lines changed: 57 additions & 15 deletions

File tree

include/math/brdf.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,4 +175,7 @@ static float reflectanceToIOR(float reflectance) noexcept
175175
return (1.0f + sqrtF0) / (1.0f - sqrtF0);
176176
}
177177

178+
static const float minIOR = reflectanceToIOR(0.0f); /**< Minimal supported IOR value. */
179+
static const float maxIOR = reflectanceToIOR(1.0f); /**< Maximal supported IOR value. */
180+
178181
} // namespace math::brdf

include/math/color.hpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ struct [[nodiscard]] Color
7171
* @param a alpha channel color value (transparency)
7272
*/
7373
constexpr Color(Color rgb, float a) noexcept : r(rgb.r), g(rgb.g), b(rgb.b),
74-
a(std::clamp(a, 0.0f, 1.0f) * 255.0f + 0.5f) { }
74+
a(saturate(a) * 255.0f + 0.5f) { }
7575

7676
/**
7777
* @brief Creates a new sRGB color structure from the binary data.
@@ -133,7 +133,7 @@ struct [[nodiscard]] Color
133133
*/
134134
constexpr explicit Color(float2 normRg) noexcept
135135
{
136-
auto c = clamp(normRg, float2(0.0f), float2(1.0f)) * float2(255.0f) + float2(0.5f);
136+
auto c = saturate(normRg) * float2(255.0f) + float2(0.5f);
137137
r = (uint8)c.x, g = (uint8)c.y, b = 0; a = 255;
138138
}
139139
/**
@@ -142,7 +142,7 @@ struct [[nodiscard]] Color
142142
*/
143143
constexpr explicit Color(float3 normRgb) noexcept
144144
{
145-
auto c = clamp(normRgb, float3(0.0f), float3(1.0f)) * float3(255.0f) + float3(0.5f);
145+
auto c = saturate(normRgb) * float3(255.0f) + float3(0.5f);
146146
r = (uint8)c.x, g = (uint8)c.y, b = (uint8)c.z, a = 255;
147147
}
148148
/**
@@ -151,7 +151,7 @@ struct [[nodiscard]] Color
151151
*/
152152
constexpr explicit Color(float4 normRgba) noexcept
153153
{
154-
auto c = clamp(normRgba, float4(0.0f), float4(1.0f)) * float4(255.0f) + float4(0.5f);
154+
auto c = saturate(normRgba) * float4(255.0f) + float4(0.5f);
155155
r = (uint8)c.x, g = (uint8)c.y, b = (uint8)c.z, a = (uint8)c.w;
156156
}
157157
/**
@@ -160,7 +160,7 @@ struct [[nodiscard]] Color
160160
*/
161161
explicit Color(f32x4 normRgba) noexcept
162162
{
163-
auto c = fma(clamp(normRgba, f32x4::zero, f32x4::one), f32x4(255.0f), f32x4(0.5f));
163+
auto c = fma(saturate(normRgba), f32x4(255.0f), f32x4(0.5f));
164164
r = (uint8)c.getX(), g = (uint8)c.getY(), b = (uint8)c.getZ(), a = (uint8)c.getW();
165165
}
166166

@@ -205,19 +205,19 @@ struct [[nodiscard]] Color
205205
/**
206206
* @brief Sets sRGB color normalizer R channel. (Red)
207207
*/
208-
constexpr void setNormR(float r) noexcept { this->r = std::clamp(r, 0.0f, 1.0f) * 255.0f + 0.5f; }
208+
constexpr void setNormR(float r) noexcept { this->r = saturate(r) * 255.0f + 0.5f; }
209209
/**
210210
* @brief Sets sRGB color normalizer G channel. (Green)
211211
*/
212-
constexpr void setNormG(float g) noexcept { this->g = std::clamp(g, 0.0f, 1.0f) * 255.0f + 0.5f; }
212+
constexpr void setNormG(float g) noexcept { this->g = saturate(g) * 255.0f + 0.5f; }
213213
/**
214214
* @brief Sets sRGB color normalizer B channel. (Blue)
215215
*/
216-
constexpr void setNormB(float b) noexcept { this->b = std::clamp(b, 0.0f, 1.0f) * 255.0f + 0.5f; }
216+
constexpr void setNormB(float b) noexcept { this->b = saturate(b) * 255.0f + 0.5f; }
217217
/**
218218
* @brief Sets sRGB color normalizer A channel. (Alpha)
219219
*/
220-
constexpr void setNormA(float a) noexcept { this->a = std::clamp(a, 0.0f, 1.0f) * 255.0f + 0.5f; }
220+
constexpr void setNormA(float a) noexcept { this->a = saturate(a) * 255.0f + 0.5f; }
221221

222222
/*******************************************************************************************************************
223223
* @brief Converts sRGB color to the string. (Space separated)

include/math/common.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@ static constexpr int32 min(int32 a, int32 b, int32 c) { return std::min(std::min
6161
*/
6262
static constexpr int32 max(int32 a, int32 b, int32 c) { return std::max(std::max(a, b), c); }
6363

64+
/**
65+
* @brief Clamps float value between the 0.0f and 1.0f. (Inclusive range)
66+
* @param v target float value to saturate
67+
*/
68+
static constexpr float saturate(float v) { return std::clamp(v, 0.0f, 1.0f); }
69+
6470
/**
6571
* @brief Returns specified floating point value sign.
6672
* @param v target value

include/math/line.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ static f32x4 closestPoint(const Line& line, f32x4 point) noexcept
8282
{
8383
auto a = line.start, d = line.getDirection(false);
8484
auto t = dot3(point - a, d) / dot3(d, d);
85-
return fma(d, f32x4(std::clamp(t, 0.0f, 1.0f)), a);
85+
return fma(d, f32x4(saturate(t)), a);
8686
}
8787
/**
8888
* @brief Returns closest point on line to the specified one in 3D space.
@@ -95,7 +95,7 @@ static f32x4 closestPoint(const Line& line, f32x4 point, float& t) noexcept
9595
{
9696
auto a = line.start, d = line.getDirection(false);
9797
t = dot3(point - a, d) / dot3(d, d);
98-
return fma(d, f32x4(std::clamp(t, 0.0f, 1.0f)), a);
98+
return fma(d, f32x4(saturate(t)), a);
9999
}
100100

101101
} // namespace math

include/math/simd/vector/float.hpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -841,7 +841,7 @@ static float max3(f32x4 v) noexcept
841841
}
842842

843843
/**
844-
* @brief Clamps SIMD vector components between min and max values.
844+
* @brief Clamps SIMD vector components between min and max values. (Inclusive range)
845845
*
846846
* @param v target SIMD vector to clamp
847847
* @param min SIMD vector with minimum values
@@ -851,6 +851,14 @@ static f32x4 clamp(f32x4 v, f32x4 min, f32x4 max) noexcept
851851
{
852852
return math::max(math::min(v, max), min);
853853
}
854+
/**
855+
* @brief Clamps SIMD vector components between the 0.0f and 1.0f. (Inclusive range)
856+
* @param v target SIMD vector to saturate
857+
*/
858+
static f32x4 saturate(f32x4 v) noexcept
859+
{
860+
return math::max(math::min(v, f32x4::one), f32x4::zero);
861+
}
854862

855863
/***********************************************************************************************************************
856864
* @brief Fused multiply add, calculates: mul1 * mul2 + add

include/math/vector/float.hpp

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ static constexpr float2 min(float2 a, float2 b, float2 c) noexcept { return min(
629629
static constexpr float2 max(float2 a, float2 b, float2 c) noexcept { return max(max(a, b), c); }
630630

631631
/**
632-
* @brief Clamps vector components between min and max values.
632+
* @brief Clamps vector components between min and max values. (Inclusive range)
633633
*
634634
* @param v target vector to clamp
635635
* @param min vector with minimum values
@@ -639,6 +639,14 @@ static constexpr float2 clamp(float2 v, float2 min, float2 max) noexcept
639639
{
640640
return float2(std::clamp(v.x, min.x, max.x), std::clamp(v.y, min.y, max.y));
641641
}
642+
/**
643+
* @brief Clamps vector components between the 0.0f and 1.0f. (Inclusive range)
644+
* @param v target vector to saturate
645+
*/
646+
static constexpr float2 saturate(float2 v) noexcept
647+
{
648+
return float2(std::clamp(v.x, 0.0f, 1.0f), std::clamp(v.y, 0.0f, 1.0f));
649+
}
642650

643651
/***********************************************************************************************************************
644652
* @brief Fused multiply add, calculates: mul1 * mul2 + add
@@ -916,7 +924,7 @@ static constexpr float3 min(float3 a, float3 b, float3 c) noexcept { return min(
916924
static constexpr float3 max(float3 a, float3 b, float3 c) noexcept { return max(max(a, b), c); }
917925

918926
/**
919-
* @brief Clamps vector components between min and max values.
927+
* @brief Clamps vector components between min and max values. (Inclusive range)
920928
*
921929
* @param v target vector to clamp
922930
* @param min vector with minimum values
@@ -926,6 +934,14 @@ static constexpr float3 clamp(float3 v, float3 min, float3 max) noexcept
926934
{
927935
return float3(std::clamp(v.x, min.x, max.x), std::clamp(v.y, min.y, max.y), std::clamp(v.z, min.z, max.z));
928936
}
937+
/**
938+
* @brief Clamps vector components between the 0.0f and 1.0f. (Inclusive range)
939+
* @param v target vector to saturate
940+
*/
941+
static constexpr float3 saturate(float3 v) noexcept
942+
{
943+
return float3(std::clamp(v.x, 0.0f, 1.0f), std::clamp(v.y, 0.0f, 1.0f), std::clamp(v.z, 0.0f, 1.0f));
944+
}
929945

930946
/***********************************************************************************************************************
931947
* @brief Fused multiply add, calculates: mul1 * mul2 + add
@@ -1225,7 +1241,7 @@ static constexpr float4 min(float4 a, float4 b, float4 c) noexcept { return min(
12251241
static constexpr float4 max(float4 a, float4 b, float4 c) noexcept { return max(max(a, b), c); }
12261242

12271243
/**
1228-
* @brief Clamps vector components between min and max values.
1244+
* @brief Clamps vector components between min and max values. (Inclusive range)
12291245
*
12301246
* @param v target vector to clamp
12311247
* @param min vector with minimum values
@@ -1236,6 +1252,15 @@ static constexpr float4 clamp(float4 v, float4 min, float4 max) noexcept
12361252
return float4(std::clamp(v.x, min.x, max.x), std::clamp(v.y, min.y, max.y),
12371253
std::clamp(v.z, min.z, max.z), std::clamp(v.w, min.w, max.w));
12381254
}
1255+
/**
1256+
* @brief Clamps vector components between the 0.0f and 1.0f. (Inclusive range)
1257+
* @param v target vector to saturate
1258+
*/
1259+
static constexpr float4 saturate(float4 v) noexcept
1260+
{
1261+
return float4(std::clamp(v.x, 0.0f, 1.0f), std::clamp(v.y, 0.0f, 1.0f),
1262+
std::clamp(v.z, 0.0f, 1.0f), std::clamp(v.w, 0.0f, 1.0f));
1263+
}
12391264

12401265
/***********************************************************************************************************************
12411266
* @brief Fused multiply add, calculates: mul1 * mul2 + add

0 commit comments

Comments
 (0)