Skip to content

Commit 2f5f692

Browse files
committed
add a new create() that takes r0 and extents, also use * rsqrt instead of / sqrt
1 parent 6ee9227 commit 2f5f692

3 files changed

Lines changed: 28 additions & 2 deletions

File tree

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,32 @@ struct SphericalRectangle
5959
return retval;
6060
}
6161

62+
// Create directly from a local-frame corner position and rectangle extents.
63+
// Use when you already know r0 (e.g. from a gnomonic projection) and don't
64+
// need the shapes::SphericalRectangle + solidAngle(observer) roundtrip.
65+
static SphericalRectangle<T> create(const vector3_type _r0, const vector2_type _extents)
66+
{
67+
// Same math as shapes::SphericalRectangle::solidAngle() but without
68+
// the mul(basis, origin - observer) step since we already have r0.
69+
typename shapes::SphericalRectangle<T>::solid_angle_type sa;
70+
sa.r0 = _r0;
71+
72+
const scalar_type zSq = _r0.z * _r0.z;
73+
const vector4_type denorm_n_z = vector4_type(-_r0.y, _r0.x + _extents.x, _r0.y + _extents.y, -_r0.x);
74+
sa.n_z = denorm_n_z * hlsl::rsqrt<vector4_type>(hlsl::promote<vector4_type>(zSq) + denorm_n_z * denorm_n_z);
75+
sa.cosGamma = vector4_type(
76+
-sa.n_z[0] * sa.n_z[1], -sa.n_z[1] * sa.n_z[2],
77+
-sa.n_z[2] * sa.n_z[3], -sa.n_z[3] * sa.n_z[0]);
78+
79+
math::sincos_accumulator<scalar_type> acc = math::sincos_accumulator<scalar_type>::create(sa.cosGamma[0]);
80+
acc.addCosine(sa.cosGamma[1]);
81+
acc.addCosine(sa.cosGamma[2]);
82+
acc.addCosine(sa.cosGamma[3]);
83+
sa.value = acc.getSumOfArccos() - scalar_type(2.0) * numbers::pi<scalar_type>;
84+
85+
return create(sa, _extents);
86+
}
87+
6288
// shared core of generate and generateSurfaceOffset
6389
// returns (xu, hv, d) packed into a vector3; caller derives either 2D offset or 3D direction
6490
vector3_type __generate(const domain_type u) NBL_CONST_MEMBER_FUNC

include/nbl/builtin/hlsl/shapes/spherical_rectangle.hlsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ struct SphericalRectangle
112112
result.r0 = hlsl::mul(basis, origin - observer);
113113

114114
const vector4_type denorm_n_z = vector4_type(-result.r0.y, result.r0.x + extents.x, result.r0.y + extents.y, -result.r0.x);
115-
result.n_z = denorm_n_z / nbl::hlsl::sqrt(hlsl::promote<vector4_type>(result.r0.z * result.r0.z) + denorm_n_z * denorm_n_z);
115+
result.n_z = denorm_n_z * hlsl::rsqrt<vector4_type>(hlsl::promote<vector4_type>(result.r0.z * result.r0.z) + denorm_n_z * denorm_n_z);
116116
result.cosGamma = vector4_type(
117117
-result.n_z[0] * result.n_z[1],
118118
-result.n_z[1] * result.n_z[2],

0 commit comments

Comments
 (0)