Skip to content

Commit 882375e

Browse files
committed
added flipSign func that copies sign of rhs
1 parent fd128e6 commit 882375e

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

include/nbl/builtin/hlsl/bxdf/cook_torrance_base.hlsl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ struct SCookTorrance
185185
fresnel::OrientedEtaRcps<monochrome_type> rcpEta = fresnel.getOrientedEtaRcps();
186186

187187
const vector3_type localV = interaction.getTangentSpaceV();
188-
const vector3_type upperHemisphereV = hlsl::mix(localV, -localV, interaction.getNdotV() < scalar_type(0.0));
188+
const vector3_type upperHemisphereV = ieee754::flipSignIfRHSNegative<vector3_type>(localV, hlsl::promote<vector3_type>(interaction.getNdotV())); //hlsl::mix(localV, -localV, interaction.getNdotV() < scalar_type(0.0));
189189
const vector3_type localH = ndf.generateH(upperHemisphereV, u.xy);
190190

191191
const scalar_type VdotH = hlsl::dot(localV, localH);

include/nbl/builtin/hlsl/ieee754.hlsl

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,19 @@ struct flipSign_helper<FloatingPoint, Bool NBL_PARTIAL_REQ_BOT(concepts::Floatin
166166
}
167167
};
168168

169+
template <typename FloatingPoint>
170+
NBL_PARTIAL_REQ_TOP(concepts::FloatingPointLikeScalar<FloatingPoint>)
171+
struct flipSign_helper<FloatingPoint, FloatingPoint NBL_PARTIAL_REQ_BOT(concepts::FloatingPointLikeScalar<FloatingPoint>) >
172+
{
173+
static FloatingPoint __call(FloatingPoint val, FloatingPoint flip)
174+
{
175+
using AsFloat = typename float_of_size<sizeof(FloatingPoint)>::type;
176+
using AsUint = typename unsigned_integer_of_size<sizeof(FloatingPoint)>::type;
177+
const AsUint asUint = ieee754::impl::bitCastToUintType(val);
178+
return bit_cast<FloatingPoint>(asUint ^ (ieee754::traits<AsFloat>::signMask && flip));
179+
}
180+
};
181+
169182
template <typename Vectorial, typename Bool>
170183
NBL_PARTIAL_REQ_TOP(concepts::FloatingPointLikeVectorial<Vectorial> && concepts::BooleanScalar<Bool>)
171184
struct flipSign_helper<Vectorial, Bool NBL_PARTIAL_REQ_BOT(concepts::FloatingPointLikeVectorial<Vectorial> && concepts::BooleanScalar<Bool>) >
@@ -203,6 +216,24 @@ struct flipSign_helper<Vectorial, BoolVector NBL_PARTIAL_REQ_BOT(concepts::Float
203216
return output;
204217
}
205218
};
219+
220+
template <typename Vectorial>
221+
NBL_PARTIAL_REQ_TOP(concepts::FloatingPointLikeVectorial<Vectorial>)
222+
struct flipSign_helper<Vectorial, Vectorial NBL_PARTIAL_REQ_BOT(concepts::FloatingPointLikeVectorial<Vectorial>) >
223+
{
224+
static Vectorial __call(Vectorial val, Vectorial flip)
225+
{
226+
using traits_v = hlsl::vector_traits<Vectorial>;
227+
array_get<Vectorial, typename traits_v::scalar_type> getter_v;
228+
array_set<Vectorial, typename traits_v::scalar_type> setter;
229+
230+
Vectorial output;
231+
for (uint32_t i = 0; i < traits_v::Dimension; ++i)
232+
setter(output, i, flipSign_helper<typename traits_v::scalar_type, typename traits_v::scalar_type>::__call(getter_v(val, i), getter_v(flip, i)));
233+
234+
return output;
235+
}
236+
};
206237
}
207238

208239
template <typename T, typename U>
@@ -211,6 +242,12 @@ NBL_CONSTEXPR_INLINE_FUNC T flipSign(T val, U flip)
211242
return impl::flipSign_helper<T, U>::__call(val, flip);
212243
}
213244

245+
template <typename T>
246+
NBL_CONSTEXPR_INLINE_FUNC T flipSignIfRHSNegative(T val, T flip)
247+
{
248+
return impl::flipSign_helper<T, T>::__call(val, flip);
249+
}
250+
214251
}
215252
}
216253
}

0 commit comments

Comments
 (0)