Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,28 @@ namespace accessors
{
// declare concept
#define NBL_CONCEPT_NAME AnisotropicallySampled
#define NBL_CONCEPT_TPLT_PRM_KINDS (typename)(int32_t)
#define NBL_CONCEPT_TPLT_PRM_NAMES (U)(Dims)
#define NBL_CONCEPT_TPLT_PRM_KINDS (typename)(int32_t)(int32_t)
#define NBL_CONCEPT_TPLT_PRM_NAMES (U)(Dims)(Components)
// not the greatest syntax but works
#define NBL_CONCEPT_PARAM_0 (a,U)
#define NBL_CONCEPT_PARAM_1 (uv,vector<float32_t,Dims>)
#define NBL_CONCEPT_PARAM_2 (layer,uint16_t)
#define NBL_CONCEPT_PARAM_3 (dU,vector<float32_t,Dims>)
#define NBL_CONCEPT_PARAM_4 (dV,vector<float32_t,Dims>)
#define NBL_CONCEPT_PARAM_5 (outVal,vector<float32_t,Components>)
// start concept
NBL_CONCEPT_BEGIN(5)
NBL_CONCEPT_BEGIN(6)
// need to be defined AFTER the cocnept begins
#define a NBL_CONCEPT_PARAM_T NBL_CONCEPT_PARAM_0
#define uv NBL_CONCEPT_PARAM_T NBL_CONCEPT_PARAM_1
#define layer NBL_CONCEPT_PARAM_T NBL_CONCEPT_PARAM_2
#define dU NBL_CONCEPT_PARAM_T NBL_CONCEPT_PARAM_3
#define dV NBL_CONCEPT_PARAM_T NBL_CONCEPT_PARAM_4
#define outVal NBL_CONCEPT_PARAM_T NBL_CONCEPT_PARAM_5
NBL_CONCEPT_END(
((NBL_CONCEPT_REQ_EXPR_RET_TYPE)((a.template get<Dims>(uv,layer,dU,dV)) , ::nbl::hlsl::is_same_v, float32_t4>))
((NBL_CONCEPT_REQ_EXPR_RET_TYPE)((a.template get<Dims>(outVal,uv,layer,dU,dV)) , ::nbl::hlsl::is_same_v, void))
);
#undef outVal
#undef dV
#undef dU
#undef layer
Expand All @@ -47,4 +50,4 @@ NBL_CONCEPT_END(
}
}
}
#endif
#endif
30 changes: 18 additions & 12 deletions include/nbl/builtin/hlsl/concepts/accessors/loadable_image.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -18,52 +18,58 @@ namespace accessors
{

// concept `LoadableImage` translates to smth like this:
//template<typename U, typename T, int32_t Dims>
//concept LoadableImage = requires(U a, vector<uint16_t, Dims> uv, uint16_t layer) {
// ::nbl::hlsl::is_same_v<decltype(declval<U>().template get<T,Dims>(uv,layer)), vector<T,4>>;
//template<typename U, typename T, int32_t Dims, int32_t Components>
//concept LoadableImage = requires(U a, vector<uint16_t, Dims> uv, uint16_t layer, vector<T, Components> outVal) {
// ::nbl::hlsl::is_same_v<decltype(declval<U>().template get<T,Dims>(outVal,uv,layer)), void>;
//};

// declare concept
#define NBL_CONCEPT_NAME LoadableImage
#define NBL_CONCEPT_TPLT_PRM_KINDS (typename)(typename)(int32_t)
#define NBL_CONCEPT_TPLT_PRM_NAMES (U)(T)(Dims)
#define NBL_CONCEPT_TPLT_PRM_KINDS (typename)(typename)(int32_t)(int32_t)
#define NBL_CONCEPT_TPLT_PRM_NAMES (U)(T)(Dims)(Components)
// not the greatest syntax but works
#define NBL_CONCEPT_PARAM_0 (a,U)
#define NBL_CONCEPT_PARAM_1 (uv,vector<uint16_t,Dims>)
#define NBL_CONCEPT_PARAM_2 (layer,uint16_t)
#define NBL_CONCEPT_PARAM_3 (outVal,vector<T,Components>)
// start concept
NBL_CONCEPT_BEGIN(3)
NBL_CONCEPT_BEGIN(4)
// need to be defined AFTER the concept begins
#define a NBL_CONCEPT_PARAM_T NBL_CONCEPT_PARAM_0
#define uv NBL_CONCEPT_PARAM_T NBL_CONCEPT_PARAM_1
#define layer NBL_CONCEPT_PARAM_T NBL_CONCEPT_PARAM_2
#define outVal NBL_CONCEPT_PARAM_T NBL_CONCEPT_PARAM_3
NBL_CONCEPT_END(
((NBL_CONCEPT_REQ_EXPR_RET_TYPE)((a.template get<T,Dims>(uv,layer)), ::nbl::hlsl::is_same_v, vector<T,4>))
((NBL_CONCEPT_REQ_EXPR_RET_TYPE)((a.template get<T,Dims>(outVal,uv,layer)), ::nbl::hlsl::is_same_v, void))
);
#undef outVal
#undef layer
#undef uv
#undef a
#include <nbl/builtin/hlsl/concepts/__end.hlsl>

// declare concept
#define NBL_CONCEPT_NAME MipmappedLoadableImage
#define NBL_CONCEPT_TPLT_PRM_KINDS (typename)(typename)(int32_t)
#define NBL_CONCEPT_TPLT_PRM_NAMES (U)(T)(Dims)
#define NBL_CONCEPT_TPLT_PRM_KINDS (typename)(typename)(int32_t)(int32_t)
#define NBL_CONCEPT_TPLT_PRM_NAMES (U)(T)(Dims)(Components)
// not the greatest syntax but works
#define NBL_CONCEPT_PARAM_0 (a,U)
#define NBL_CONCEPT_PARAM_1 (uv,vector<uint16_t,Dims>)
#define NBL_CONCEPT_PARAM_2 (layer,uint16_t)
#define NBL_CONCEPT_PARAM_3 (level,uint16_t)
#define NBL_CONCEPT_PARAM_4 (outVal,vector<T,Components>)
// start concept
NBL_CONCEPT_BEGIN(4)
NBL_CONCEPT_BEGIN(5)
// need to be defined AFTER the cocnept begins
#define a NBL_CONCEPT_PARAM_T NBL_CONCEPT_PARAM_0
#define uv NBL_CONCEPT_PARAM_T NBL_CONCEPT_PARAM_1
#define layer NBL_CONCEPT_PARAM_T NBL_CONCEPT_PARAM_2
#define level NBL_CONCEPT_PARAM_T NBL_CONCEPT_PARAM_3
#define outVal NBL_CONCEPT_PARAM_T NBL_CONCEPT_PARAM_4
NBL_CONCEPT_END(
((NBL_CONCEPT_REQ_EXPR_RET_TYPE)((a.template get<T,Dims>(uv,layer,level)) , ::nbl::hlsl::is_same_v, vector<T,4>))
((NBL_CONCEPT_REQ_EXPR_RET_TYPE)((a.template get<T,Dims>(outVal,uv,layer,level)) , ::nbl::hlsl::is_same_v, void))
);
#undef outVal
#undef level
#undef layer
#undef uv
Expand All @@ -73,4 +79,4 @@ NBL_CONCEPT_END(
}
}
}
#endif
#endif
9 changes: 6 additions & 3 deletions include/nbl/builtin/hlsl/concepts/accessors/mip_mapped.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,19 @@ namespace accessors
#define NBL_CONCEPT_PARAM_1 (uv,vector<float32_t,Dims>)
#define NBL_CONCEPT_PARAM_2 (layer,uint16_t)
#define NBL_CONCEPT_PARAM_3 (level,float)
#define NBL_CONCEPT_PARAM_4 (outVal,float32_t4)
// start concept
NBL_CONCEPT_BEGIN(4)
NBL_CONCEPT_BEGIN(5)
// need to be defined AFTER the cocnept begins
#define a NBL_CONCEPT_PARAM_T NBL_CONCEPT_PARAM_0
#define uv NBL_CONCEPT_PARAM_T NBL_CONCEPT_PARAM_1
#define layer NBL_CONCEPT_PARAM_T NBL_CONCEPT_PARAM_2
#define level NBL_CONCEPT_PARAM_T NBL_CONCEPT_PARAM_3
#define outVal NBL_CONCEPT_PARAM_T NBL_CONCEPT_PARAM_4
NBL_CONCEPT_END(
((NBL_CONCEPT_REQ_EXPR_RET_TYPE)((a.template get<Dims>(uv,layer,level)) , ::nbl::hlsl::is_same_v, float32_t4>))
((NBL_CONCEPT_REQ_EXPR_RET_TYPE)((a.template get<Dims>(outVal,uv,layer,level)) , ::nbl::hlsl::is_same_v, void))
);
#undef outVal
#undef level
#undef layer
#undef uv
Expand All @@ -44,4 +47,4 @@ NBL_CONCEPT_END(
}
}
}
#endif
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ namespace accessors
{
// declare concept
#define NBL_CONCEPT_NAME StorableImage
#define NBL_CONCEPT_TPLT_PRM_KINDS (typename)(typename)(int32_t)
#define NBL_CONCEPT_TPLT_PRM_NAMES (U)(T)(Dims)
#define NBL_CONCEPT_TPLT_PRM_KINDS (typename)(typename)(int32_t)(int32_t)
#define NBL_CONCEPT_TPLT_PRM_NAMES (U)(T)(Dims)(Components)
// not the greatest syntax but works
#define NBL_CONCEPT_PARAM_0 (a,U)
#define NBL_CONCEPT_PARAM_1 (uv,vector<uint16_t,Dims>)
#define NBL_CONCEPT_PARAM_2 (layer,uint16_t)
#define NBL_CONCEPT_PARAM_3 (data,vector<T,4>)
#define NBL_CONCEPT_PARAM_3 (data,vector<T,Components>)
// start concept
NBL_CONCEPT_BEGIN(4)
// need to be defined AFTER the cocnept begins
Expand Down
109 changes: 55 additions & 54 deletions include/nbl/builtin/hlsl/rwmc/CascadeAccumulator.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include <nbl/builtin/hlsl/cpp_compat.hlsl>
#include <nbl/builtin/hlsl/cpp_compat/promote.hlsl>
#include <nbl/builtin/hlsl/vector_utils/vector_traits.hlsl>
#include <nbl/builtin/hlsl/colorspace/encodeCIEXYZ.hlsl>
#include <nbl/builtin/hlsl/rwmc/SplattingParameters.hlsl>

namespace nbl
Expand All @@ -13,79 +12,81 @@ namespace hlsl
namespace rwmc
{

template<typename CascadeLayerType, uint32_t CascadeCount NBL_PRIMARY_REQUIRES(concepts::Vector<CascadeLayerType>)
struct CascadeAccumulator
template<typename CascadeLayerType, uint32_t CascadeCountValue, typename SampleCountType = uint16_t NBL_PRIMARY_REQUIRES(concepts::Vector<CascadeLayerType> && concepts::UnsignedIntegralScalar<SampleCountType>)
struct DefaultCascades
{
struct CascadeEntry
using layer_type = CascadeLayerType;
using sample_count_type = SampleCountType;
NBL_CONSTEXPR_STATIC_INLINE uint32_t CascadeCount = CascadeCountValue;

sample_count_type cascadeSampleCounter[CascadeCount];
CascadeLayerType data[CascadeCount];

void clear(uint32_t cascadeIx)
{
uint32_t cascadeSampleCounter[CascadeCount];
CascadeLayerType data[CascadeCount];
cascadeSampleCounter[cascadeIx] = sample_count_type(0u);
data[cascadeIx] = promote<CascadeLayerType, float32_t>(0.0f);
}

void addSampleIntoCascadeEntry(CascadeLayerType _sample, uint32_t lowerCascadeIndex, float lowerCascadeLevelWeight, float higherCascadeLevelWeight, uint32_t sampleCount)
void addSampleIntoCascadeEntry(CascadeLayerType _sample, uint16_t lowerCascadeIndex, SSplattingParameters::scalar_t lowerCascadeLevelWeight, SSplattingParameters::scalar_t higherCascadeLevelWeight, uint32_t sampleCount)
{
const SSplattingParameters::scalar_t reciprocalSampleCount = SSplattingParameters::scalar_t(1.0f) / SSplattingParameters::scalar_t(sampleCount);

sample_count_type lowerCascadeSampleCount = cascadeSampleCounter[lowerCascadeIndex];
data[lowerCascadeIndex] += (_sample * lowerCascadeLevelWeight - (sampleCount - lowerCascadeSampleCount) * data[lowerCascadeIndex]) * reciprocalSampleCount;
cascadeSampleCounter[lowerCascadeIndex] = sample_count_type(sampleCount);

uint16_t higherCascadeIndex = lowerCascadeIndex + uint16_t(1u);
if (higherCascadeIndex < CascadeCount)
{
const float reciprocalSampleCount = 1.0f / float(sampleCount);

uint32_t lowerCascadeSampleCount = cascadeSampleCounter[lowerCascadeIndex];
data[lowerCascadeIndex] += (_sample * lowerCascadeLevelWeight - (sampleCount - lowerCascadeSampleCount) * data[lowerCascadeIndex]) * reciprocalSampleCount;
cascadeSampleCounter[lowerCascadeIndex] = sampleCount;

uint32_t higherCascadeIndex = lowerCascadeIndex + 1u;
if (higherCascadeIndex < CascadeCount)
{
uint32_t higherCascadeSampleCount = cascadeSampleCounter[higherCascadeIndex];
data[higherCascadeIndex] += (_sample * higherCascadeLevelWeight - (sampleCount - higherCascadeSampleCount) * data[higherCascadeIndex]) * reciprocalSampleCount;
cascadeSampleCounter[higherCascadeIndex] = sampleCount;
}
sample_count_type higherCascadeSampleCount = cascadeSampleCounter[higherCascadeIndex];
data[higherCascadeIndex] += (_sample * higherCascadeLevelWeight - (sampleCount - higherCascadeSampleCount) * data[higherCascadeIndex]) * reciprocalSampleCount;
cascadeSampleCounter[higherCascadeIndex] = sample_count_type(sampleCount);
}
};

using cascade_layer_scalar_type = typename vector_traits<CascadeLayerType>::scalar_type;
using this_t = CascadeAccumulator<CascadeLayerType, CascadeCount>;
using input_sample_type = CascadeLayerType;
using output_storage_type = CascadeEntry;
using initialization_data = SplattingParameters;
output_storage_type accumulation;
}
};

template<typename CascadesType>
struct CascadeAccumulator
{
using scalar_t = typename SSplattingParameters::scalar_t;
using input_sample_type = typename CascadesType::layer_type;
using this_t = CascadeAccumulator<CascadesType>;
using cascades_type = CascadesType;
NBL_CONSTEXPR_STATIC_INLINE uint32_t CascadeCount = cascades_type::CascadeCount;
NBL_CONSTEXPR_STATIC_INLINE scalar_t LastCascade = scalar_t(CascadeCount - 1u);
cascades_type accumulation;

SplattingParameters splattingParameters;
SSplattingParameters splattingParameters;

static this_t create(NBL_CONST_REF_ARG(SplattingParameters) settings)
static this_t create(NBL_CONST_REF_ARG(SPackedSplattingParameters) settings)
{
this_t retval;
for (int i = 0; i < CascadeCount; ++i)
{
retval.accumulation.data[i] = promote<CascadeLayerType, float32_t>(0.0f);
retval.accumulation.cascadeSampleCounter[i] = 0u;
}
retval.splattingParameters = settings;
for (uint32_t i = 0u; i < CascadeCount; ++i)
retval.accumulation.clear(i);
retval.splattingParameters = settings.unpack();

return retval;
}

cascade_layer_scalar_type getLuma(NBL_CONST_REF_ARG(CascadeLayerType) col)
{
return hlsl::dot<CascadeLayerType>(hlsl::transpose(colorspace::scRGBtoXYZ)[1], col);
}

// most of this code is stolen from https://cg.ivd.kit.edu/publications/2018/rwmc/tool/split.cpp
void addSample(uint32_t sampleCount, input_sample_type _sample)
{
const float32_t2 unpackedParams = hlsl::unpackHalf2x16(splattingParameters.packedLog2);
const cascade_layer_scalar_type log2Start = unpackedParams[0];
const cascade_layer_scalar_type log2Base = unpackedParams[1];
const cascade_layer_scalar_type luma = getLuma(_sample);
const cascade_layer_scalar_type log2Luma = log2<cascade_layer_scalar_type>(luma);
const cascade_layer_scalar_type cascade = log2Luma * 1.f / log2Base - log2Start / log2Base;
const cascade_layer_scalar_type clampedCascade = clamp(cascade, 0, CascadeCount - 1);
const scalar_t luma = splattingParameters.calcLuma<input_sample_type>(_sample);
const scalar_t log2Luma = log2<scalar_t>(luma);
const scalar_t cascade = log2Luma * splattingParameters.RcpLog2Base - splattingParameters.Log2BaseRootOfStart;
const scalar_t clampedCascade = clamp(cascade, scalar_t(0), LastCascade);
const scalar_t clampedCascadeFloor = floor<scalar_t>(clampedCascade);
// c<=0 -> 0, c>=Count-1 -> Count-1
uint32_t lowerCascadeIndex = floor<cascade_layer_scalar_type>(cascade);
uint16_t lowerCascadeIndex = uint16_t(clampedCascadeFloor);
// 0 whenever clamped or `cascade` is integer (when `clampedCascade` is integer)
cascade_layer_scalar_type higherCascadeWeight = clampedCascade - floor<cascade_layer_scalar_type>(clampedCascade);
scalar_t higherCascadeWeight = clampedCascade - clampedCascadeFloor;
// never 0 thanks to magic of `1-fract(x)`
cascade_layer_scalar_type lowerCascadeWeight = cascade_layer_scalar_type(1) - higherCascadeWeight;
scalar_t lowerCascadeWeight = scalar_t(1) - higherCascadeWeight;

// handle super bright sample case
if (cascade > CascadeCount - 1)
lowerCascadeWeight = exp2(log2Start + log2Base * (CascadeCount - 1) - log2Luma);
if (cascade > LastCascade)
lowerCascadeWeight = exp2(splattingParameters.BrightSampleLumaBias - log2Luma);

accumulation.addSampleIntoCascadeEntry(_sample, lowerCascadeIndex, lowerCascadeWeight, higherCascadeWeight, sampleCount);
}
Expand All @@ -97,4 +98,4 @@ struct CascadeAccumulator
}
}

#endif
#endif
Loading
Loading