Skip to content

Commit 3134254

Browse files
committed
precompute values in linear, bilinear sampling; make box muller a struct
1 parent 0305948 commit 3134254

File tree

3 files changed

+35
-16
lines changed

3 files changed

+35
-16
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ struct Bilinear
2828
{
2929
Bilinear<T> retval;
3030
retval.bilinearCoeffs = bilinearCoeffs;
31+
retval.twiceAreasUnderXCurve = vector2_type(bilinearCoeffs[0] + bilinearCoeffs[1], bilinearCoeffs[2] + bilinearCoeffs[3]);
3132
return retval;
3233
}
3334

3435
vector2_type generate(NBL_REF_ARG(scalar_type) rcpPdf, NBL_CONST_REF_ARG(vector2_type) _u)
3536
{
3637
vector2_type u = _u;
37-
const vector2_type twiceAreasUnderXCurve = vector2_type(bilinearCoeffs[0] + bilinearCoeffs[1], bilinearCoeffs[2] + bilinearCoeffs[3]);
3838
Linear<scalar_type> lineary = Linear<scalar_type>::create(twiceAreasUnderXCurve);
3939
u.y = lineary.generate(u.y);
4040

@@ -52,7 +52,10 @@ struct Bilinear
5252
return 4.0 * nbl::hlsl::mix(nbl::hlsl::mix(bilinearCoeffs[0], bilinearCoeffs[1], u.x), nbl::hlsl::mix(bilinearCoeffs[2], bilinearCoeffs[3], u.x), u.y) / (bilinearCoeffs[0] + bilinearCoeffs[1] + bilinearCoeffs[2] + bilinearCoeffs[3]);
5353
}
5454

55-
vector4_type bilinearCoeffs;
55+
// unit square: x0y0 x1y0
56+
// x0y1 x1y1
57+
vector4_type bilinearCoeffs; // (x0y0, x0y1, x1y0, x1y1)
58+
vector2_type twiceAreasUnderXCurve;
5659
};
5760

5861
}

include/nbl/builtin/hlsl/sampling/box_muller_transform.hlsl

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// This file is part of the "Nabla Engine".
33
// For conditions of distribution and use, see copyright notice in nabla.h
44

5-
#ifndef _NBL_BUILTIN_HLSL_BOX_MULLER_TRANSFORM_INCLUDED_
6-
#define _NBL_BUILTIN_HLSL_BOX_MULLER_TRANSFORM_INCLUDED_
5+
#ifndef _NBL_BUILTIN_HLSL_SAMPLING_BOX_MULLER_TRANSFORM_INCLUDED_
6+
#define _NBL_BUILTIN_HLSL_SAMPLING_BOX_MULLER_TRANSFORM_INCLUDED_
77

88
#include "nbl/builtin/hlsl/math/functions.hlsl"
99
#include "nbl/builtin/hlsl/numbers.hlsl"
@@ -12,15 +12,26 @@ namespace nbl
1212
{
1313
namespace hlsl
1414
{
15+
namespace sampling
16+
{
1517

16-
template<typename T>
17-
vector<T,2> boxMullerTransform(vector<T,2> xi, T stddev)
18+
template<typename T NBL_PRIMARY_REQUIRES(concepts::FloatingPointLikeScalar<T>)
19+
struct BoxMullerTransform
1820
{
19-
T sinPhi, cosPhi;
20-
math::sincos<T>(2.0 * numbers::pi<float> * xi.y - numbers::pi<float>, sinPhi, cosPhi);
21-
return vector<T,2>(cosPhi, sinPhi) * nbl::hlsl::sqrt(-2.0 * nbl::hlsl::log(xi.x)) * stddev;
22-
}
21+
using scalar_type = T;
22+
using vector2_type = vector<T,2>;
23+
24+
vector2_type operator()(vector2_type xi)
25+
{
26+
scalar_type sinPhi, cosPhi;
27+
math::sincos<scalar_type>(2.0 * numbers::pi<scalar_type> * xi.y - numbers::pi<scalar_type>, sinPhi, cosPhi);
28+
return vector2_type(cosPhi, sinPhi) * nbl::hlsl::sqrt(-2.0 * nbl::hlsl::log(xi.x)) * stddev;
29+
}
2330

31+
T stddev;
32+
};
33+
34+
}
2435
}
2536
}
2637

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

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,26 @@ struct Linear
2121
using scalar_type = T;
2222
using vector2_type = vector<T, 2>;
2323

24-
static Linear<T> create(NBL_CONST_REF_ARG(vector2_type) linearCoeffs)
24+
static Linear<T> create(NBL_CONST_REF_ARG(vector2_type) linearCoeffs) // start and end importance values (start, end)
2525
{
2626
Linear<T> retval;
27-
retval.linearCoeffs = linearCoeffs;
27+
retval.linearCoeffStart = linearCoeffs[0];
28+
retval.rcpDiff = 1.0 / (linearCoeffs[0] - linearCoeffs[1]);
29+
vector2_type squaredCoeffs = linearCoeffs * linearCoeffs;
30+
retval.squaredCoeffStart = squaredCoeffs[0];
31+
retval.squaredCoeffDiff = squaredCoeffs[1] - squaredCoeffs[0];
2832
return retval;
2933
}
3034

3135
scalar_type generate(scalar_type u)
3236
{
33-
const scalar_type rcpDiff = 1.0 / (linearCoeffs[0] - linearCoeffs[1]);
34-
const vector2_type squaredCoeffs = linearCoeffs * linearCoeffs;
35-
return nbl::hlsl::abs(rcpDiff) < numeric_limits<scalar_type>::max ? (linearCoeffs[0] - nbl::hlsl::sqrt(nbl::hlsl::mix(squaredCoeffs[0], squaredCoeffs[1], u))) * rcpDiff : u;
37+
return hlsl::mix(u, (linearCoeffStart - hlsl::sqrt(squaredCoeffStart + u * squaredCoeffDiff)) * rcpDiff, hlsl::abs(rcpDiff) < numeric_limits<scalar_type>::max);
3638
}
3739

38-
vector2_type linearCoeffs;
40+
scalar_type linearCoeffStart;
41+
scalar_type rcpDiff;
42+
scalar_type squaredCoeffStart;
43+
scalar_type squaredCoeffDiff;
3944
};
4045

4146
}

0 commit comments

Comments
 (0)