Skip to content

Commit 0305948

Browse files
committed
make PartitionRandVar in struct and templated
1 parent 4a283ba commit 0305948

File tree

6 files changed

+54
-19
lines changed

6 files changed

+54
-19
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "nbl/builtin/hlsl/bxdf/config.hlsl"
99
#include "nbl/builtin/hlsl/bxdf/ndf.hlsl"
1010
#include "nbl/builtin/hlsl/bxdf/fresnel.hlsl"
11+
#include "nbl/builtin/hlsl/sampling/basic.hlsl"
1112
#include "nbl/builtin/hlsl/bxdf/ndf/microfacet_to_light_transform.hlsl"
1213

1314
namespace nbl
@@ -302,7 +303,8 @@ struct SCookTorrance
302303

303304
scalar_type rcpChoiceProb;
304305
scalar_type z = u.z;
305-
bool transmitted = math::partitionRandVariable(reflectance, z, rcpChoiceProb);
306+
sampling::PartitionRandVariable<scalar_type> partitionRandVariable;
307+
bool transmitted = partitionRandVariable(reflectance, z, rcpChoiceProb);
306308

307309
const scalar_type LdotH = hlsl::mix(VdotH, ieee754::copySign(hlsl::sqrt(rcpEta.value2[0]*VdotH*VdotH + scalar_type(1.0) - rcpEta.value2[0]), -VdotH), transmitted);
308310
bool valid;

include/nbl/builtin/hlsl/bxdf/transmission/smooth_dielectric.hlsl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include "nbl/builtin/hlsl/bxdf/common.hlsl"
88
#include "nbl/builtin/hlsl/bxdf/bxdf_traits.hlsl"
9+
#include "nbl/builtin/hlsl/sampling/basic.hlsl"
910
#include "nbl/builtin/hlsl/sampling/cos_weighted_spheres.hlsl"
1011

1112
namespace nbl
@@ -39,7 +40,8 @@ struct SSmoothDielectric
3940
const scalar_type reflectance = fresnel::Dielectric<monochrome_type>::__call(orientedEta.value*orientedEta.value, interaction.getNdotV(_clamp))[0];
4041

4142
scalar_type rcpChoiceProb;
42-
bool transmitted = math::partitionRandVariable(reflectance, u.z, rcpChoiceProb);
43+
sampling::PartitionRandVariable<scalar_type> partitionRandVariable;
44+
bool transmitted = partitionRandVariable(reflectance, u.z, rcpChoiceProb);
4345

4446
ray_dir_info_type V = interaction.getV();
4547
Refract<scalar_type> r = Refract<scalar_type>::create(V.getDirection(), interaction.getN());
@@ -125,7 +127,8 @@ struct SThinSmoothDielectric
125127

126128
scalar_type rcpChoiceProb;
127129
scalar_type z = u.z;
128-
const bool transmitted = math::partitionRandVariable(reflectionProb, z, rcpChoiceProb);
130+
sampling::PartitionRandVariable<scalar_type> partitionRandVariable;
131+
const bool transmitted = partitionRandVariable(reflectionProb, z, rcpChoiceProb);
129132
remainderMetadata = hlsl::mix(reflectance, hlsl::promote<spectral_type>(1.0) - reflectance, transmitted) * rcpChoiceProb;
130133

131134
ray_dir_info_type V = interaction.getV();

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

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -120,21 +120,6 @@ void frisvad(NBL_CONST_REF_ARG(T) normal, NBL_REF_ARG(T) tangent, NBL_REF_ARG(T)
120120
}
121121
}
122122

123-
bool partitionRandVariable(float leftProb, NBL_REF_ARG(float) xi, NBL_REF_ARG(float) rcpChoiceProb)
124-
{
125-
const float32_t NEXT_ULP_AFTER_UNITY = bit_cast<float32_t>(0x3f800001u);
126-
const bool pickRight = xi >= leftProb * NEXT_ULP_AFTER_UNITY;
127-
128-
// This is all 100% correct taking into account the above NEXT_ULP_AFTER_UNITY
129-
xi -= pickRight ? leftProb : 0.0f;
130-
131-
rcpChoiceProb = 1.0f / (pickRight ? (1.0f - leftProb) : leftProb);
132-
xi *= rcpChoiceProb;
133-
134-
return pickRight;
135-
}
136-
137-
138123
namespace impl
139124
{
140125
template <typename T NBL_STRUCT_CONSTRAINABLE>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright (C) 2018-2025 - DevSH Graphics Programming Sp. z O.O.
2+
// This file is part of the "Nabla Engine".
3+
// For conditions of distribution and use, see copyright notice in nabla.h
4+
5+
#ifndef _NBL_BUILTIN_HLSL_SAMPLING_BASIC_INCLUDED_
6+
#define _NBL_BUILTIN_HLSL_SAMPLING_BASIC_INCLUDED_
7+
8+
#include <nbl/builtin/hlsl/cpp_compat.hlsl>
9+
#include <nbl/builtin/hlsl/ieee754.hlsl>
10+
11+
namespace nbl
12+
{
13+
namespace hlsl
14+
{
15+
namespace sampling
16+
{
17+
18+
template<typename T NBL_PRIMARY_REQUIRES(concepts::FloatingPointLikeScalar<T>)
19+
struct PartitionRandVariable
20+
{
21+
using floating_point_type = T;
22+
using uint_type = typename unsigned_integer_of_size<sizeof(floating_point_type)>::type;
23+
24+
bool operator()(floating_point_type leftProb, NBL_REF_ARG(floating_point_type) xi, NBL_REF_ARG(floating_point_type) rcpChoiceProb)
25+
{
26+
const floating_point_type NEXT_ULP_AFTER_UNITY = bit_cast<floating_point_type>(bit_cast<uint_type>(floating_point_type(1.0)) + uint_type(1u));
27+
const bool pickRight = xi >= leftProb * NEXT_ULP_AFTER_UNITY;
28+
29+
// This is all 100% correct taking into account the above NEXT_ULP_AFTER_UNITY
30+
xi -= pickRight ? leftProb : floating_point_type(0.0);
31+
32+
rcpChoiceProb = floating_point_type(1.0) / (pickRight ? (floating_point_type(1.0) - leftProb) : leftProb);
33+
xi *= rcpChoiceProb;
34+
35+
return pickRight;
36+
}
37+
};
38+
39+
40+
}
41+
}
42+
}
43+
44+
#endif

src/nbl/builtin/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/shapes/spherical_triangle.hls
253253
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/shapes/spherical_rectangle.hlsl")
254254
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/shapes/aabb.hlsl")
255255
#sampling
256+
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/sampling/basic.hlsl")
256257
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/sampling/linear.hlsl")
257258
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/sampling/bilinear.hlsl")
258259
LIST_BUILTIN_RESOURCE(NBL_RESOURCES_TO_EMBED "hlsl/sampling/concentric_mapping.hlsl")

0 commit comments

Comments
 (0)