Skip to content

Commit 482ef2e

Browse files
committed
merge rwmc, fix conflicts
2 parents 381bb50 + 2a7db14 commit 482ef2e

File tree

7 files changed

+372
-5
lines changed

7 files changed

+372
-5
lines changed

examples_tests

Submodule examples_tests updated 32 files

include/nbl/builtin/hlsl/concepts/accessors/loadable_image.hlsl

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,15 @@ namespace concepts
1616
{
1717
namespace accessors
1818
{
19+
20+
// concept `LoadableImage` translates to smth like this:
21+
//template<typename U, typename T, int32_t Dims>
22+
//concept LoadableImage = requires(U a, vector<uint16_t, Dims> uv, uint16_t layer) {
23+
// ::nbl::hlsl::is_same_v<decltype(declval<U>().template get<T,Dims>(uv,layer)), vector<T,4>>;
24+
//};
25+
1926
// declare concept
20-
#define NBL_CONCEPT_NAME StorableImage
27+
#define NBL_CONCEPT_NAME LoadableImage
2128
#define NBL_CONCEPT_TPLT_PRM_KINDS (typename)(typename)(int32_t)
2229
#define NBL_CONCEPT_TPLT_PRM_NAMES (U)(T)(Dims)
2330
// not the greatest syntax but works
@@ -26,20 +33,20 @@ namespace accessors
2633
#define NBL_CONCEPT_PARAM_2 (layer,uint16_t)
2734
// start concept
2835
NBL_CONCEPT_BEGIN(3)
29-
// need to be defined AFTER the cocnept begins
36+
// need to be defined AFTER the concept begins
3037
#define a NBL_CONCEPT_PARAM_T NBL_CONCEPT_PARAM_0
3138
#define uv NBL_CONCEPT_PARAM_T NBL_CONCEPT_PARAM_1
3239
#define layer NBL_CONCEPT_PARAM_T NBL_CONCEPT_PARAM_2
3340
NBL_CONCEPT_END(
34-
((NBL_CONCEPT_REQ_EXPR_RET_TYPE)((a.template get<T,Dims>(uv,layer)) , ::nbl::hlsl::is_same_v, vector<T,4>))
41+
((NBL_CONCEPT_REQ_EXPR_RET_TYPE)((a.template get<T,Dims>(uv,layer)), ::nbl::hlsl::is_same_v, vector<T,4>))
3542
);
3643
#undef layer
3744
#undef uv
3845
#undef a
3946
#include <nbl/builtin/hlsl/concepts/__end.hlsl>
4047

4148
// declare concept
42-
#define NBL_CONCEPT_NAME MipmappedStorableImage
49+
#define NBL_CONCEPT_NAME MipmappedLoadableImage
4350
#define NBL_CONCEPT_TPLT_PRM_KINDS (typename)(typename)(int32_t)
4451
#define NBL_CONCEPT_TPLT_PRM_NAMES (U)(T)(Dims)
4552
// not the greatest syntax but works
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#ifndef _NBL_HLSL_RWMC_CASCADE_ACCUMULATOR_INCLUDED_
2+
#define _NBL_HLSL_RWMC_CASCADE_ACCUMULATOR_INCLUDED_
3+
#include <nbl/builtin/hlsl/cpp_compat.hlsl>
4+
#include <nbl/builtin/hlsl/cpp_compat/promote.hlsl>
5+
#include <nbl/builtin/hlsl/vector_utils/vector_traits.hlsl>
6+
#include <nbl/builtin/hlsl/colorspace/encodeCIEXYZ.hlsl>
7+
#include <nbl/builtin/hlsl/rwmc/SplattingParameters.hlsl>
8+
9+
namespace nbl
10+
{
11+
namespace hlsl
12+
{
13+
namespace rwmc
14+
{
15+
16+
template<typename CascadeLayerType, uint32_t CascadeCount NBL_PRIMARY_REQUIRES(concepts::Vector<CascadeLayerType>)
17+
struct CascadeAccumulator
18+
{
19+
struct CascadeEntry
20+
{
21+
uint32_t cascadeSampleCounter[CascadeCount];
22+
CascadeLayerType data[CascadeCount];
23+
24+
void addSampleIntoCascadeEntry(CascadeLayerType _sample, uint32_t lowerCascadeIndex, float lowerCascadeLevelWeight, float higherCascadeLevelWeight, uint32_t sampleCount)
25+
{
26+
const float reciprocalSampleCount = 1.0f / float(sampleCount);
27+
28+
uint32_t lowerCascadeSampleCount = cascadeSampleCounter[lowerCascadeIndex];
29+
data[lowerCascadeIndex] += (_sample * lowerCascadeLevelWeight - (sampleCount - lowerCascadeSampleCount) * data[lowerCascadeIndex]) * reciprocalSampleCount;
30+
cascadeSampleCounter[lowerCascadeIndex] = sampleCount;
31+
32+
uint32_t higherCascadeIndex = lowerCascadeIndex + 1u;
33+
if (higherCascadeIndex < CascadeCount)
34+
{
35+
uint32_t higherCascadeSampleCount = cascadeSampleCounter[higherCascadeIndex];
36+
data[higherCascadeIndex] += (_sample * higherCascadeLevelWeight - (sampleCount - higherCascadeSampleCount) * data[higherCascadeIndex]) * reciprocalSampleCount;
37+
cascadeSampleCounter[higherCascadeIndex] = sampleCount;
38+
}
39+
}
40+
};
41+
42+
using cascade_layer_scalar_type = typename vector_traits<CascadeLayerType>::scalar_type;
43+
using this_t = CascadeAccumulator<CascadeLayerType, CascadeCount>;
44+
using output_storage_type = CascadeEntry;
45+
using initialization_data = SplattingParameters;
46+
output_storage_type accumulation;
47+
48+
SplattingParameters splattingParameters;
49+
50+
static this_t create(NBL_CONST_REF_ARG(SplattingParameters) settings)
51+
{
52+
this_t retval;
53+
for (int i = 0; i < CascadeCount; ++i)
54+
{
55+
retval.accumulation.data[i] = promote<CascadeLayerType, float32_t>(0.0f);
56+
retval.accumulation.cascadeSampleCounter[i] = 0u;
57+
}
58+
retval.splattingParameters = settings;
59+
60+
return retval;
61+
}
62+
63+
cascade_layer_scalar_type getLuma(NBL_CONST_REF_ARG(CascadeLayerType) col)
64+
{
65+
return hlsl::dot<CascadeLayerType>(hlsl::transpose(colorspace::scRGBtoXYZ)[1], col);
66+
}
67+
68+
// most of this code is stolen from https://cg.ivd.kit.edu/publications/2018/rwmc/tool/split.cpp
69+
void addSample(uint32_t sampleCount, CascadeLayerType _sample)
70+
{
71+
const cascade_layer_scalar_type log2Start = splattingParameters.log2Start;
72+
const cascade_layer_scalar_type log2Base = splattingParameters.log2Base;
73+
const cascade_layer_scalar_type luma = getLuma(_sample);
74+
const cascade_layer_scalar_type log2Luma = log2<cascade_layer_scalar_type>(luma);
75+
const cascade_layer_scalar_type cascade = log2Luma * 1.f / log2Base - log2Start / log2Base;
76+
const cascade_layer_scalar_type clampedCascade = clamp(cascade, 0, CascadeCount - 1);
77+
// c<=0 -> 0, c>=Count-1 -> Count-1
78+
uint32_t lowerCascadeIndex = floor<cascade_layer_scalar_type>(cascade);
79+
// 0 whenever clamped or `cascade` is integer (when `clampedCascade` is integer)
80+
cascade_layer_scalar_type higherCascadeWeight = clampedCascade - floor<cascade_layer_scalar_type>(clampedCascade);
81+
// never 0 thanks to magic of `1-fract(x)`
82+
cascade_layer_scalar_type lowerCascadeWeight = cascade_layer_scalar_type(1) - higherCascadeWeight;
83+
84+
// handle super bright sample case
85+
if (cascade > CascadeCount - 1)
86+
lowerCascadeWeight = exp2(log2Start + log2Base * (CascadeCount - 1) - log2Luma);
87+
88+
accumulation.addSampleIntoCascadeEntry(_sample, lowerCascadeIndex, lowerCascadeWeight, higherCascadeWeight, sampleCount);
89+
}
90+
91+
92+
};
93+
94+
}
95+
}
96+
}
97+
98+
#endif
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#ifndef _NBL_BUILTIN_HLSL_RWMC_RESOLVE_PARAMETERS_HLSL_INCLUDED_
2+
#define _NBL_BUILTIN_HLSL_RWMC_RESOLVE_PARAMETERS_HLSL_INCLUDED_
3+
4+
#include "nbl/builtin/hlsl/cpp_compat.hlsl"
5+
6+
namespace nbl
7+
{
8+
namespace hlsl
9+
{
10+
namespace rwmc
11+
{
12+
13+
struct ResolveParameters
14+
{
15+
uint32_t lastCascadeIndex;
16+
float initialEmin; // a minimum image brightness that we always consider reliable
17+
float reciprocalBase;
18+
float reciprocalN;
19+
float reciprocalKappa;
20+
float colorReliabilityFactor;
21+
float NOverKappa;
22+
};
23+
24+
ResolveParameters computeResolveParameters(float base, uint32_t sampleCount, float minReliableLuma, float kappa, uint32_t cascadeSize)
25+
{
26+
ResolveParameters retval;
27+
retval.lastCascadeIndex = cascadeSize - 1u;
28+
retval.initialEmin = minReliableLuma;
29+
retval.reciprocalBase = 1.f / base;
30+
const float N = float(sampleCount);
31+
retval.reciprocalN = 1.f / N;
32+
retval.reciprocalKappa = 1.f / kappa;
33+
// if not interested in exact expected value estimation (kappa!=1.f), can usually accept a bit more variance relative to the image brightness we already have
34+
// allow up to ~<cascadeBase> more energy in one sample to lessen bias in some cases
35+
retval.colorReliabilityFactor = base + (1.f - base) * retval.reciprocalKappa;
36+
retval.NOverKappa = N * retval.reciprocalKappa;
37+
38+
return retval;
39+
}
40+
41+
}
42+
}
43+
}
44+
45+
#endif
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#ifndef _NBL_BUILTIN_HLSL_RWMC_SPLATTING_PARAMETERS_HLSL_INCLUDED_
2+
#define _NBL_BUILTIN_HLSL_RWMC_SPLATTING_PARAMETERS_HLSL_INCLUDED_
3+
4+
#include "nbl/builtin/hlsl/cpp_compat.hlsl"
5+
6+
namespace nbl
7+
{
8+
namespace hlsl
9+
{
10+
namespace rwmc
11+
{
12+
13+
struct SplattingParameters
14+
{
15+
float log2Start;
16+
float log2Base;
17+
};
18+
19+
}
20+
}
21+
}
22+
23+
#endif

0 commit comments

Comments
 (0)