@@ -12,38 +12,45 @@ namespace hlsl
1212namespace rwmc
1313{
1414
15- template<typename CascadeLayerType, uint32_t CascadeCountValue , typename SampleCountType = uint16_t NBL_PRIMARY_REQUIRES (concepts::Vector<CascadeLayerType> && concepts::UnsignedIntegralScalar<SampleCountType>)
15+ template<typename CascadeLayerType, uint16_t CascadeCount , typename SampleCountType = uint16_t NBL_PRIMARY_REQUIRES (concepts::Vector<CascadeLayerType> && concepts::UnsignedIntegralScalar<SampleCountType>)
1616struct DefaultCascades
1717{
18+ // required public interfaces (TODO: a concept for it)
1819 using layer_type = CascadeLayerType;
1920 using sample_count_type = SampleCountType;
20- NBL_CONSTEXPR_STATIC_INLINE uint32_t CascadeCount = CascadeCountValue ;
21+ using weight_t = typename SSplattingParameters::scalar_t ;
2122
22- sample_count_type cascadeSampleCounter[CascadeCount];
23- CascadeLayerType data[CascadeCount];
23+ inline uint16_t getLastCascade () {return CascadeCount-uint16_t (1 );}
2424
25- void clear (uint32_t cascadeIx )
25+ void clear ()
2626 {
27- cascadeSampleCounter[cascadeIx] = sample_count_type (0u);
28- data[cascadeIx] = promote<CascadeLayerType, float32_t>(0.0f );
27+ for (uint16_t i=0u; i<CascadeCount; ++i)
28+ {
29+ __cascadeSampleCounter[i] = sample_count_type (0u);
30+ __data[i] = promote<CascadeLayerType,float32_t>(0.0f );
31+ }
2932 }
3033
31- void addSampleIntoCascadeEntry (layer_type _sample, uint16_t lowerCascadeIndex, SSplattingParameters::scalar_t lowerCascadeLevelWeight, SSplattingParameters::scalar_t higherCascadeLevelWeight, sample_count_type sampleCount)
34+ void addSampleIntoCascadeEntry (const layer_type _sample, const uint16_t lowerCascadeIndex, const weight_t lowerCascadeLevelWeight, const weight_t higherCascadeLevelWeight, const sample_count_type sampleCount)
3235 {
33- const SSplattingParameters::scalar_t reciprocalSampleCount = SSplattingParameters:: scalar_t ( 1.0f ) / SSplattingParameters:: scalar_t (sampleCount);
36+ const weight_t reciprocalSampleCount = _static_cast<weight_t>( 1 )/_static_cast<weight_t> (sampleCount);
3437
35- sample_count_type lowerCascadeSampleCount = cascadeSampleCounter [lowerCascadeIndex];
36- data [lowerCascadeIndex] += (_sample * lowerCascadeLevelWeight - (sampleCount - lowerCascadeSampleCount) * data [lowerCascadeIndex]) * reciprocalSampleCount;
37- cascadeSampleCounter [lowerCascadeIndex] = sampleCount;
38+ sample_count_type lowerCascadeSampleCount = __cascadeSampleCounter [lowerCascadeIndex];
39+ __data [lowerCascadeIndex] += (_sample * lowerCascadeLevelWeight - (sampleCount - lowerCascadeSampleCount) * __data [lowerCascadeIndex]) * reciprocalSampleCount;
40+ __cascadeSampleCounter [lowerCascadeIndex] = sampleCount;
3841
3942 uint16_t higherCascadeIndex = lowerCascadeIndex + uint16_t (1u);
4043 if (higherCascadeIndex < CascadeCount)
4144 {
42- sample_count_type higherCascadeSampleCount = cascadeSampleCounter [higherCascadeIndex];
43- data [higherCascadeIndex] += (_sample * higherCascadeLevelWeight - (sampleCount - higherCascadeSampleCount) * data [higherCascadeIndex]) * reciprocalSampleCount;
44- cascadeSampleCounter [higherCascadeIndex] = sampleCount;
45+ sample_count_type higherCascadeSampleCount = __cascadeSampleCounter [higherCascadeIndex];
46+ __data [higherCascadeIndex] += (_sample * higherCascadeLevelWeight - (sampleCount - higherCascadeSampleCount) * __data [higherCascadeIndex]) * reciprocalSampleCount;
47+ __cascadeSampleCounter [higherCascadeIndex] = sampleCount;
4548 }
4649 }
50+
51+ // private
52+ sample_count_type __cascadeSampleCounter[CascadeCount];
53+ CascadeLayerType __data[CascadeCount];
4754};
4855
4956template<typename CascadesType>
@@ -52,19 +59,18 @@ struct CascadeAccumulator
5259 using scalar_t = typename SSplattingParameters::scalar_t;
5360 using input_sample_type = typename CascadesType::layer_type;
5461 using sample_count_type = typename CascadesType::sample_count_type;
62+ using weight_t = typename CascadesType::weight_t;
5563 using this_t = CascadeAccumulator<CascadesType>;
5664 using cascades_type = CascadesType;
57- NBL_CONSTEXPR_STATIC_INLINE uint16_t CascadeCount = cascades_type::CascadeCount;
58- NBL_CONSTEXPR_STATIC_INLINE scalar_t LastCascade = scalar_t (CascadeCount - 1u);
65+
5966 cascades_type accumulation;
60-
6167 SSplattingParameters splattingParameters;
6268
63- static this_t create (NBL_CONST_REF_ARG (SPackedSplattingParameters) settings)
69+ static this_t create (NBL_CONST_REF_ARG (SPackedSplattingParameters) settings, const bool clear= true )
6470 {
6571 this_t retval;
66- for (uint16_t i = 0u; i < CascadeCount; ++i )
67- retval.accumulation.clear (i );
72+ if (clear )
73+ retval.accumulation.clear ();
6874 retval.splattingParameters = settings.unpack ();
6975
7076 return retval;
@@ -73,25 +79,27 @@ struct CascadeAccumulator
7379 // most of this code is stolen from https://cg.ivd.kit.edu/publications/2018/rwmc/tool/split.cpp
7480 void addSample (const sample_count_type sampleCount, input_sample_type _sample)
7581 {
76- const scalar_t luma = splattingParameters.calcLuma<input_sample_type>(_sample);
77- const scalar_t log2Luma = log2<scalar_t>(luma);
82+ const uint16_t lastCascade = accumulation.getLastCascade ();
83+
84+ const weight_t luma = splattingParameters.calcLuma<input_sample_type>(_sample);
85+ const weight_t log2Luma = hlsl::log2<weight_t>(luma);
7886 const scalar_t cascade = log2Luma * splattingParameters.RcpLog2Base - splattingParameters.Log2BaseRootOfStart;
79- const scalar_t clampedCascade = clamp (cascade, scalar_t (0 ), LastCascade );
80- const scalar_t clampedCascadeFloor = floor<scalar_t> (clampedCascade);
87+ const scalar_t clampedCascade = hlsl:: clamp <scalar_t> (cascade, scalar_t (0 ), lastCascade );
88+ const scalar_t clampedCascadeFloor = hlsl:: floor (clampedCascade);
8189 // c<=0 -> 0, c>=Count-1 -> Count-1
82- uint16_t lowerCascadeIndex = uint16_t (clampedCascadeFloor);
90+ const uint16_t lowerCascadeIndex = _static_cast< uint16_t> (clampedCascadeFloor);
8391 // 0 whenever clamped or `cascade` is integer (when `clampedCascade` is integer)
84- scalar_t higherCascadeWeight = clampedCascade - clampedCascadeFloor;
92+ const weight_t higherCascadeWeight = _static_cast<weight_t>( clampedCascade - clampedCascadeFloor) ;
8593 // never 0 thanks to magic of `1-fract(x)`
86- scalar_t lowerCascadeWeight = scalar_t (1 ) - higherCascadeWeight;
94+ weight_t lowerCascadeWeight = weight_t (1 ) - higherCascadeWeight;
8795
8896 // handle super bright sample case
89- if (cascade > LastCascade )
90- lowerCascadeWeight = exp2 (splattingParameters.BrightSampleLumaBias - log2Luma);
97+ if (cascade > lastCascade )
98+ lowerCascadeWeight = hlsl:: exp2 (_static_cast<weight_t>( splattingParameters.BrightSampleLumaBias - log2Luma) );
9199
92100 accumulation.addSampleIntoCascadeEntry (_sample, lowerCascadeIndex, lowerCascadeWeight, higherCascadeWeight, sampleCount);
93101 }
94-
102+
95103
96104};
97105
0 commit comments