Skip to content

Improved Scan #855

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
d4e3738
intial changes
keptsecret Mar 18, 2025
10d9c39
subgroup2 implementations
keptsecret Mar 27, 2025
f2a281c
some fixes, example
keptsecret Mar 27, 2025
4622f1f
changed template parameters
keptsecret Mar 28, 2025
abfaf67
working subgroup2 template and funcs
keptsecret Mar 31, 2025
f2d6d8a
fix reduction bug
keptsecret Mar 31, 2025
eeec20a
minor fix
keptsecret Apr 1, 2025
53ffc60
latest example
keptsecret Apr 2, 2025
0efeb8d
merge master, fix conflicts
keptsecret Apr 7, 2025
1478837
new example number
keptsecret Apr 7, 2025
e88f51a
partial spec for items per invoc =1
keptsecret Apr 9, 2025
a8e02a3
changes to Params, Config handling types
keptsecret Apr 10, 2025
237ac09
rework specializations for native, emulated funcs
keptsecret Apr 10, 2025
859c313
added OpSelect intrinsic for mix, fix mix behavior with bool
keptsecret Apr 4, 2025
c5a3223
use mix instead of ternary op
keptsecret Apr 11, 2025
87bca2b
fixes to subgroup2 funcs
keptsecret Apr 11, 2025
49fd605
changes to handle coalesced data loads
keptsecret Apr 21, 2025
4ae51a1
merge master, fix example conflicts
keptsecret Apr 21, 2025
609ad85
fixes to inclusive_scan for coalesced
keptsecret Apr 21, 2025
6b692f4
removed redundant code
keptsecret Apr 21, 2025
d0acb31
enabled handling vectors in spirv group ops with templates and enable_if
keptsecret Apr 23, 2025
fc92538
added impl component wise inclusive scan for inclusive scan
keptsecret Apr 23, 2025
8ad4843
revert to scans using consecutive data loads
keptsecret Apr 25, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
192 changes: 192 additions & 0 deletions include/nbl/builtin/hlsl/bxdf/fresnel.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,20 @@ struct orientedEtas<float>
rcpOrientedEta = backside ? eta : rcpEta;
return backside;
}

static T diffuseFresnelCorrectionFactor(T n, T n2)
{
// assert(n*n==n2);
vector<bool,vector_traits<T>::Dimension> TIR = n < (T)1.0;
T invdenum = nbl::hlsl::mix<T>(hlsl::promote<T>(1.0), hlsl::promote<T>(1.0) / (n2 * n2 * (hlsl::promote<T>(554.33) - 380.7 * n)), TIR);
T num = n * nbl::hlsl::mix<T>(hlsl::promote<T>(0.1921156102251088), n * 298.25 - 261.38 * n2 + 138.43, TIR);
num += nbl::hlsl::mix<T>(hlsl::promote<T>(0.8078843897748912), hlsl::promote<T>(-1.67), TIR);
return num * invdenum;
}

T value;
T rcp;
bool backside;
};

template<>
Expand Down Expand Up @@ -140,6 +154,184 @@ struct refract
scalar_type rcpOrientedEta2;
};

template<typename T NBL_PRIMARY_REQUIRES(concepts::Vectorial<T> && vector_traits<T>::Dimension == 3)
struct ReflectRefract
{
using this_t = ReflectRefract<T>;
using vector_type = T;
using scalar_type = typename vector_traits<T>::scalar_type;

static this_t create(bool refract, NBL_CONST_REF_ARG(vector_type) I, NBL_CONST_REF_ARG(vector_type) N, scalar_type NdotI, scalar_type NdotTorR, scalar_type rcpOrientedEta)
{
this_t retval;
retval.refract = refract;
retval.I = I;
retval.N = N;
retval.NdotI = NdotI;
retval.NdotTorR = NdotTorR;
retval.rcpOrientedEta = rcpOrientedEta;
return retval;
}

static this_t create(bool r, NBL_CONST_REF_ARG(Refract<vector_type>) refract)
{
this_t retval;
retval.refract = r;
retval.I = refract.I;
retval.N = refract.N;
retval.NdotI = refract.NdotI;
retval.NdotTorR = r ? Refract<vector_type>::computeNdotT(refract.backside, refract.NdotI2, refract.rcpOrientedEta2) : refract.NdotI;
retval.rcpOrientedEta = refract.rcpOrientedEta;
return retval;
}

vector_type operator()()
{
return N * (NdotI * (hlsl::mix<scalar_type>(1.0f, rcpOrientedEta, refract)) + NdotTorR) - I * (hlsl::mix<scalar_type>(1.0f, rcpOrientedEta, refract));
}

bool refract;
vector_type I;
vector_type N;
scalar_type NdotI;
scalar_type NdotTorR;
scalar_type rcpOrientedEta;
};


namespace fresnel
{

template<typename T NBL_PRIMARY_REQUIRES(is_scalar_v<T> || is_vector_v<T>)
struct Schlick
{
using scalar_type = typename vector_traits<T>::scalar_type;

static Schlick<T> create(NBL_CONST_REF_ARG(T) F0, scalar_type VdotH)
{
Schlick<T> retval;
retval.F0 = F0;
retval.VdotH = VdotH;
return retval;
}

T operator()()
{
T x = 1.0 - VdotH;
return F0 + (1.0 - F0) * x*x*x*x*x;
}

T F0;
scalar_type VdotH;
};

template<typename T NBL_PRIMARY_REQUIRES(is_scalar_v<T> || is_vector_v<T>)
struct Conductor
{
using scalar_type = typename vector_traits<T>::scalar_type;

static Conductor<T> create(NBL_CONST_REF_ARG(T) eta, NBL_CONST_REF_ARG(T) etak, scalar_type cosTheta)
{
Conductor<T> retval;
retval.eta = eta;
retval.etak = etak;
retval.cosTheta = cosTheta;
return retval;
}

T operator()()
{
const scalar_type cosTheta2 = cosTheta * cosTheta;
//const float sinTheta2 = 1.0 - cosTheta2;

const T etaLen2 = eta * eta + etak * etak;
const T etaCosTwice = eta * cosTheta * 2.0f;

const T rs_common = etaLen2 + (T)(cosTheta2);
const T rs2 = (rs_common - etaCosTwice) / (rs_common + etaCosTwice);

const T rp_common = etaLen2 * cosTheta2 + (T)(1.0);
const T rp2 = (rp_common - etaCosTwice) / (rp_common + etaCosTwice);

return (rs2 + rp2) * 0.5f;
}

T eta;
T etak;
scalar_type cosTheta;
};

template<typename T NBL_PRIMARY_REQUIRES(is_scalar_v<T> || is_vector_v<T>)
struct Dielectric
{
using scalar_type = typename vector_traits<T>::scalar_type;

static Dielectric<T> create(NBL_CONST_REF_ARG(T) eta, scalar_type cosTheta)
{
Dielectric<T> retval;
OrientedEtas<T> orientedEta = OrientedEtas<T>::create(cosTheta, eta);
retval.eta2 = orientedEta.value * orientedEta.value;
retval.cosTheta = cosTheta;
return retval;
}

static T __call(NBL_CONST_REF_ARG(T) orientedEta2, scalar_type absCosTheta)
{
const scalar_type sinTheta2 = 1.0 - absCosTheta * absCosTheta;

// the max() clamping can handle TIR when orientedEta2<1.0
const T t0 = hlsl::sqrt<T>(hlsl::max<T>(orientedEta2 - sinTheta2, hlsl::promote<T>(0.0)));
const T rs = (hlsl::promote<T>(absCosTheta) - t0) / (hlsl::promote<T>(absCosTheta) + t0);

const T t2 = orientedEta2 * absCosTheta;
const T rp = (t0 - t2) / (t0 + t2);

return (rs * rs + rp * rp) * 0.5f;
}

T operator()()
{
return __call(eta2, cosTheta);
}

T eta2;
scalar_type cosTheta;
};

template<typename T NBL_PRIMARY_REQUIRES(is_scalar_v<T> || is_vector_v<T>)
struct DielectricFrontFaceOnly
{
using scalar_type = typename vector_traits<T>::scalar_type;

static DielectricFrontFaceOnly<T> create(NBL_CONST_REF_ARG(T) orientedEta2, scalar_type absCosTheta)
{
Dielectric<T> retval;
retval.orientedEta2 = orientedEta2;
retval.absCosTheta = hlsl::abs<T>(absCosTheta);
return retval;
}

T operator()()
{
return Dielectric<T>::__call(orientedEta2, absCosTheta);
}

T orientedEta2;
scalar_type absCosTheta;
};


// gets the sum of all R, T R T, T R^3 T, T R^5 T, ... paths
template<typename T>
struct ThinDielectricInfiniteScatter
{
T operator()(T singleInterfaceReflectance)
{
const T doubleInterfaceReflectance = singleInterfaceReflectance * singleInterfaceReflectance;
return hlsl::mix<T>(hlsl::promote<T>(1.0), (singleInterfaceReflectance - doubleInterfaceReflectance) / (hlsl::promote<T>(1.0) - doubleInterfaceReflectance) * 2.0f, doubleInterfaceReflectance > hlsl::promote<T>(0.9999));
}
};

}

}
Expand Down
Loading