Skip to content

Commit 2291b7d

Browse files
committed
fixed compartor float vs bool warning, no more spherical triangle boilerplate, addressed comments
1 parent 45a68a6 commit 2291b7d

13 files changed

Lines changed: 475 additions & 478 deletions

examples_tests

Submodule examples_tests updated 55 files

include/nbl/builtin/hlsl/algorithm.hlsl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,9 @@ struct bound_t
142142

143143
void comp_step(NBL_REF_ARG(Accessor) accessor, const uint32_t testPoint, const uint32_t rightBegin)
144144
{
145-
if (compare(accessor[testPoint],value))
145+
typename Accessor::value_type val;
146+
accessor.get(testPoint, val);
147+
if (compare(val,value))
146148
it = rightBegin;
147149
}
148150
void comp_step(NBL_REF_ARG(Accessor) accessor, const uint32_t testPoint)

include/nbl/builtin/hlsl/functional.hlsl

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,12 +89,23 @@ struct reference_wrapper : enable_if_t<
8989
return lhs OP rhs; \
9090
}
9191

92+
#define ALIAS_STD_CMP(NAME,OP) template<typename T NBL_STRUCT_CONSTRAINABLE > struct NAME { \
93+
using type_t = T; \
94+
\
95+
bool operator()(NBL_CONST_REF_ARG(T) lhs, NBL_CONST_REF_ARG(T) rhs) \
96+
{ \
97+
return lhs OP rhs; \
98+
}
99+
92100

93101
#else // CPP
94102

95103
#define ALIAS_STD(NAME,OP) template<typename T> struct NAME : std::NAME<T> { \
96104
using type_t = T;
97105

106+
#define ALIAS_STD_CMP(NAME,OP) template<typename T> struct NAME : std::NAME<T> { \
107+
using type_t = T;
108+
98109
#endif
99110

100111
ALIAS_STD(bit_and,&)
@@ -136,14 +147,15 @@ ALIAS_STD(divides,/)
136147
};
137148

138149

139-
ALIAS_STD(equal_to, ==) };
140-
ALIAS_STD(not_equal_to, !=) };
141-
ALIAS_STD(greater, >) };
142-
ALIAS_STD(less, <) };
143-
ALIAS_STD(greater_equal, >=) };
144-
ALIAS_STD(less_equal, <=) };
150+
ALIAS_STD_CMP(equal_to, ==) };
151+
ALIAS_STD_CMP(not_equal_to, !=) };
152+
ALIAS_STD_CMP(greater, >) };
153+
ALIAS_STD_CMP(less, <) };
154+
ALIAS_STD_CMP(greater_equal, >=) };
155+
ALIAS_STD_CMP(less_equal, <=) };
145156

146157
#undef ALIAS_STD
158+
#undef ALIAS_STD_CMP
147159

148160
// The above comparison operators return bool on STD, but in HLSL they're supposed to yield bool vectors, so here's a specialization so that they return `vector<bool, N>` for vectorial types
149161

include/nbl/builtin/hlsl/ies/sampler.hlsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ struct CandelaSampler
8585
const angle_t vAngle = degrees(polar.theta);
8686
const angle_t hAngle = degrees(__wrapPhi(polar.phi, symmetry));
8787

88-
#define NBL_IES_DEF_ANGLE_ACC(T, EXPR) struct T { using value_type = angle_t; accessor_t acc; value_type operator[](uint32_t idx) NBL_CONST_MEMBER_FUNC { return EXPR; } };
88+
#define NBL_IES_DEF_ANGLE_ACC(T, EXPR) struct T { using value_type = angle_t; accessor_t acc; value_type operator[](uint32_t idx) NBL_CONST_MEMBER_FUNC { return EXPR; } void get(uint32_t idx, NBL_REF_ARG(value_type) val) NBL_CONST_MEMBER_FUNC { val = EXPR; } };
8989

9090
NBL_IES_DEF_ANGLE_ACC(VAcc, acc.vAngle(idx))
9191
NBL_IES_DEF_ANGLE_ACC(HAcc, acc.hAngle(idx))

include/nbl/builtin/hlsl/math/functions.hlsl

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,22 @@ scalar_type_t<T> lpNorm(NBL_CONST_REF_ARG(T) v)
9393

9494

9595
// valid only for `theta` in [-PI,PI]
96-
template <typename T NBL_FUNC_REQUIRES(concepts::FloatingPointLikeScalar<T>)
96+
// UseRealSinCos=true -> back-to-back sin + cos. Saturates the special-function pipeline, enables vendor sincos fusion, full precision near multiples of pi.
97+
// UseRealSinCos=false -> cos + sqrt(1-c*c) with sign recovered from theta. Saves one special-function op when cos alone is cheaper than sin+cos, but suffers catastrophic cancellation as |c| -> 1.
98+
template <typename T, bool UseRealSinCos = true NBL_FUNC_REQUIRES(concepts::FloatingPointLikeScalar<T>)
9799
void sincos(T theta, NBL_REF_ARG(T) s, NBL_REF_ARG(T) c)
98100
{
99-
s = sin<T>(theta);
100-
c = cos<T>(theta);
101-
// s = sqrt<T>(T(NBL_FP64_LITERAL(1.0))-c*c);
102-
// s = ieee754::flipSign(s, theta < T(NBL_FP64_LITERAL(0.0)));
101+
if (UseRealSinCos)
102+
{
103+
s = sin<T>(theta);
104+
c = cos<T>(theta);
105+
}
106+
else
107+
{
108+
c = cos<T>(theta);
109+
s = sqrt<T>(T(NBL_FP64_LITERAL(1.0))-c*c);
110+
s = ieee754::flipSign(s, theta < T(NBL_FP64_LITERAL(0.0)));
111+
}
103112
}
104113

105114
template <typename T NBL_FUNC_REQUIRES(vector_traits<T>::Dimension == 3)

include/nbl/builtin/hlsl/sampling/bilinear.hlsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ struct Bilinear
6565

6666
// bilinear PDF = marginal_y_pdf * conditional_x_pdf; reuse both linear caches
6767
const scalar_type yPdf = lineary.forwardPdf(u.y, linearYCache);
68-
cache.normalizedStart = yPdf * linearx.linearCoeffStart;
68+
cache.normalizedStart = yPdf * linearx.normalizedCoeffStart;
6969
cache.linearXCache.diffTimesX *= yPdf;
7070
return p;
7171
}

include/nbl/builtin/hlsl/sampling/cos_weighted_spheres.hlsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ struct ProjectedSphere
9393
static codomain_type __generate(NBL_REF_ARG(domain_type) u)
9494
{
9595
vector_t3 retval = hemisphere_t::__generate(u.xy);
96-
const bool chooseLower = u.z > T(0.5);
96+
const bool chooseLower = u.z > scalar_type(0.5);
9797
retval.z = chooseLower ? (-retval.z) : retval.z;
9898
if (chooseLower)
9999
u.z -= T(0.5);

include/nbl/builtin/hlsl/sampling/linear.hlsl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@ struct Linear
3939
// add min to both coefficients so (0,0) input produces a valid uniform sampler
4040
// instead of inf normalization (2/0) leading to NaN; negligible for normal inputs
4141
const vector2_type safeCoeffs = linearCoeffs + vector2_type(hlsl::numeric_limits<scalar_type>::min, hlsl::numeric_limits<scalar_type>::min);
42-
// normalize coefficients so that the PDF is simply linearCoeffStart + linearCoeffDiff * x
42+
// normalize coefficients so that the PDF is simply normalizedCoeffStart + linearCoeffDiff * x
4343
const scalar_type normFactor = scalar_type(2.0) / (safeCoeffs[0] + safeCoeffs[1]);
4444
const vector2_type normalized = safeCoeffs * normFactor;
45-
retval.linearCoeffStart = normalized[0];
46-
retval.linearCoeffEnd = normalized[1];
45+
retval.normalizedCoeffStart = normalized[0];
46+
retval.normalizedCoeffEnd = normalized[1];
4747
// precompute for the stable quadratic in generate()
4848
retval.squaredCoeffStart = normalized[0] * normalized[0];
4949
retval.twoTimesDiff = scalar_type(2.0) * (normalized[1] - normalized[0]);
@@ -57,18 +57,18 @@ struct Linear
5757
// Quadratic (1-start)*x^2 + start*x - u = 0; since start >= 0 the stable root is
5858
// x = 2u / (start + sqrt(start^2 + 2*diff*u)), which never cancels.
5959
const scalar_type sqrtTerm = sqrt(squaredCoeffStart + twoTimesDiff * u);
60-
const scalar_type denom = linearCoeffStart + sqrtTerm;
60+
const scalar_type denom = normalizedCoeffStart + sqrtTerm;
6161
// NOTE: floating point can make x slightly > 1 when u~1 and diff < 0; callers needing
6262
// non-negative PDF at the boundary should clamp with min(x, 1).
6363
const codomain_type x = (u + u) / denom;
6464
// diff*x == sqrtTerm - start algebraically (conjugate identity), saves 1 mul
65-
cache.diffTimesX = sqrtTerm - linearCoeffStart;
65+
cache.diffTimesX = sqrtTerm - normalizedCoeffStart;
6666
return x;
6767
}
6868

6969
density_type forwardPdf(const domain_type u, const cache_type cache) NBL_CONST_MEMBER_FUNC
7070
{
71-
return linearCoeffStart + cache.diffTimesX;
71+
return normalizedCoeffStart + cache.diffTimesX;
7272
}
7373

7474
weight_type forwardWeight(const domain_type u, const cache_type cache) NBL_CONST_MEMBER_FUNC
@@ -83,16 +83,16 @@ struct Linear
8383
density_type backwardPdf(const codomain_type x) NBL_CONST_MEMBER_FUNC
8484
{
8585
assert(x >= scalar_type(0.0) && x <= scalar_type(1.0));
86-
return hlsl::mix(linearCoeffStart, linearCoeffEnd, x);
86+
return hlsl::mix(normalizedCoeffStart, normalizedCoeffEnd, x);
8787
}
8888

8989
weight_type backwardWeight(const codomain_type x) NBL_CONST_MEMBER_FUNC
9090
{
9191
return backwardPdf(x);
9292
}
9393

94-
scalar_type linearCoeffStart;
95-
scalar_type linearCoeffEnd;
94+
scalar_type normalizedCoeffStart;
95+
scalar_type normalizedCoeffEnd;
9696
scalar_type squaredCoeffStart;
9797
scalar_type twoTimesDiff;
9898
};

0 commit comments

Comments
 (0)