Skip to content

Commit cf6760e

Browse files
committed
minor fixes to spherical rect, latest example
1 parent a40a025 commit cf6760e

File tree

2 files changed

+12
-12
lines changed

2 files changed

+12
-12
lines changed

include/nbl/builtin/hlsl/sampling/spherical_rectangle.hlsl

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ struct SphericalRectangle
3535
vector2_type generate(NBL_CONST_REF_ARG(vector2_type) rectangleExtents, NBL_CONST_REF_ARG(vector2_type) uv, NBL_REF_ARG(scalar_type) S)
3636
{
3737
const vector4_type denorm_n_z = vector4_type(-rect.r0.y, rect.r0.x + rectangleExtents.x, rect.r0.y + rectangleExtents.y, -rect.r0.x);
38-
const vector4_type n_z = denorm_n_z / hlsl::sqrt<vector4_type>((vector4_type)(rect.r0.z * rect.r0.z) + denorm_n_z * denorm_n_z);
38+
const vector4_type n_z = denorm_n_z / hlsl::sqrt<vector4_type>(hlsl::promote<vector4_type>(rect.r0.z * rect.r0.z) + denorm_n_z * denorm_n_z);
3939
const vector4_type cosGamma = vector4_type(
4040
-n_z[0] * n_z[1],
4141
-n_z[1] * n_z[2],
@@ -50,32 +50,32 @@ struct SphericalRectangle
5050
angle_adder.addCosine(cosGamma[3]);
5151
scalar_type q = angle_adder.getSumofArccos();
5252

53-
const scalar_type k = 2 * numbers::pi<scalar_type> - q;
53+
const scalar_type k = scalar_type(2.0) * numbers::pi<scalar_type> - q;
5454
const scalar_type b0 = n_z[0];
5555
const scalar_type b1 = n_z[2];
56-
S = p + q - 2 * numbers::pi<scalar_type>;
56+
S = p + q - scalar_type(2.0) * numbers::pi<scalar_type>;
5757

58-
const scalar_type CLAMP_EPS = 1e-5f;
58+
const scalar_type CLAMP_EPS = 1e-5;
5959

60-
// flip z axsis if rect.r0.z > 0
61-
const uint32_t zFlipMask = (bit_cast<uint32_t>(rect.r0.z) ^ 0x80000000u) & 0x80000000u;
62-
rect.r0.z = bit_cast<float32_t>(bit_cast<uint32_t>(rect.r0.z) ^ zFlipMask);
60+
// flip z axis if rect.r0.z > 0
61+
rect.r0.z = ieee754::flipSignIfRHSNegative<scalar_type>(rect.r0.z, -rect.r0.z);
6362
vector3_type r1 = rect.r0 + vector3_type(rectangleExtents.x, rectangleExtents.y, 0);
6463

6564
const scalar_type au = uv.x * S + k;
6665
const scalar_type fu = (hlsl::cos<scalar_type>(au) * b0 - b1) / hlsl::sin<scalar_type>(au);
6766
const scalar_type cu_2 = hlsl::max<scalar_type>(fu * fu + b0 * b0, 1.f); // forces `cu` to be in [-1,1]
68-
const scalar_type cu = bit_cast<float32_t>(bit_cast<uint32_t>(1.0 / hlsl::sqrt<scalar_type>(cu_2)) ^ (bit_cast<uint32_t>(fu) & 0x80000000u));
67+
const scalar_type cu = ieee754::flipSignIfRHSNegative<scalar_type>(scalar_type(1.0) / hlsl::sqrt<scalar_type>(cu_2), fu);
6968

70-
scalar_type xu = -(cu * rect.r0.z) * 1.0 / hlsl::sqrt<scalar_type>(1 - cu * cu);
69+
scalar_type xu = -(cu * rect.r0.z) / hlsl::sqrt<scalar_type>(scalar_type(1.0) - cu * cu);
7170
xu = hlsl::clamp<scalar_type>(xu, rect.r0.x, r1.x); // avoid Infs
7271
const scalar_type d_2 = xu * xu + rect.r0.z * rect.r0.z;
7372
const scalar_type d = hlsl::sqrt<scalar_type>(d_2);
7473

7574
const scalar_type h0 = rect.r0.y / hlsl::sqrt<scalar_type>(d_2 + rect.r0.y * rect.r0.y);
7675
const scalar_type h1 = r1.y / hlsl::sqrt<scalar_type>(d_2 + r1.y * r1.y);
77-
const scalar_type hv = h0 + uv.y * (h1 - h0), hv2 = hv * hv;
78-
const scalar_type yv = (hv2 < 1 - CLAMP_EPS) ? (hv * d) / hlsl::sqrt<scalar_type>(1 - hv2) : r1.y;
76+
const scalar_type hv = h0 + uv.y * (h1 - h0);
77+
const scalar_type hv2 = hv * hv;
78+
const scalar_type yv = hlsl::mix(r1.y, (hv * d) / hlsl::sqrt<scalar_type>(scalar_type(1.0) - hv2), hv2 < scalar_type(1.0) - CLAMP_EPS);
7979

8080
return vector2_type((xu - rect.r0.x) / rectangleExtents.x, (yv - rect.r0.y) / rectangleExtents.y);
8181
}

0 commit comments

Comments
 (0)