Skip to content

Commit 9760d71

Browse files
committed
split diffuse correction factor into own struct
1 parent 1add8a3 commit 9760d71

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

include/nbl/builtin/hlsl/bxdf/fresnel.hlsl

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,23 @@ namespace bxdf
2222
namespace fresnel
2323
{
2424

25+
// derived from the Earl Hammon GDC talk but improved for all IOR not just 1.333 with actual numerical integration and curve fitting
26+
template<typename T NBL_PRIMARY_REQUIRES(concepts::FloatingPointLikeVectorial<T>)
27+
struct DiffuseCorrectionFactor
28+
{
29+
T operator()()
30+
{
31+
vector<bool,vector_traits<T>::Dimension> TIR = orientedEta < hlsl::promote<T>(1.0);
32+
T invdenum = nbl::hlsl::mix<T>(hlsl::promote<T>(1.0), hlsl::promote<T>(1.0) / (orientedEta2 * orientedEta2 * (hlsl::promote<T>(554.33) - hlsl::promote<T>(380.7) * orientedEta)), TIR);
33+
T num = orientedEta * nbl::hlsl::mix<T>(hlsl::promote<T>(0.1921156102251088), orientedEta * hlsl::promote<T>(298.25) - hlsl::promote<T>(261.38) * orientedEta2 + hlsl::promote<T>(138.43), TIR);
34+
num = num + nbl::hlsl::mix<T>(hlsl::promote<T>(0.8078843897748912), hlsl::promote<T>(-1.67), TIR);
35+
return num * invdenum;
36+
}
37+
38+
T orientedEta;
39+
T orientedEta2;
40+
};
41+
2542
template<typename T NBL_PRIMARY_REQUIRES(concepts::FloatingPointLikeVectorial<T>)
2643
struct OrientedEtaRcps
2744
{
@@ -37,17 +54,12 @@ struct OrientedEtaRcps
3754
return retval;
3855
}
3956

40-
T diffuseFresnelCorrectionFactor()
57+
DiffuseCorrectionFactor<T> createDiffuseCorrectionFactor() NBL_CONST_MEMBER_FUNC
4158
{
42-
// assert(n*n==n2);
43-
const T n = hlsl::promote<T>(1.0) / value;
44-
const T n2 = hlsl::promote<T>(1.0) / value2;
45-
46-
vector<bool,vector_traits<T>::Dimension> TIR = n < hlsl::promote<T>(1.0);
47-
T invdenum = nbl::hlsl::mix<T>(hlsl::promote<T>(1.0), hlsl::promote<T>(1.0) / (n2 * n2 * (hlsl::promote<T>(554.33) - hlsl::promote<T>(380.7) * n)), TIR);
48-
T num = n * nbl::hlsl::mix<T>(hlsl::promote<T>(0.1921156102251088), n * hlsl::promote<T>(298.25) - hlsl::promote<T>(261.38) * n2 + hlsl::promote<T>(138.43), TIR);
49-
num += nbl::hlsl::mix<T>(hlsl::promote<T>(0.8078843897748912), hlsl::promote<T>(-1.67), TIR);
50-
return num * invdenum;
59+
DiffuseCorrectionFactor<T> diffuseCorrectionFactor;
60+
diffuseCorrectionFactor.orientedEta = hlsl::promote<T>(1.0) / value;
61+
diffuseCorrectionFactor.orientedEta2 = hlsl::promote<T>(1.0) / value2;
62+
return diffuseCorrectionFactor;
5163
}
5264

5365
T value;

0 commit comments

Comments
 (0)