-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathluma_meter.hlsl
113 lines (93 loc) · 3.95 KB
/
luma_meter.hlsl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright (C) 2018-2024 - DevSH Graphics Programming Sp. z O.O.
// This file is part of the "Nabla Engine".
// For conditions of distribution and use, see copyright notice in nabla.h
#ifndef _NBL_BUILTIN_HLSL_LUMA_METER_INCLUDED_
#define _NBL_BUILTIN_HLSL_LUMA_METER_INCLUDED_
#include "nbl/builtin/hlsl/cpp_compat.hlsl"
#include "nbl/builtin/hlsl/glsl_compat/core.hlsl"
#include "nbl/builtin/hlsl/glsl_compat/subgroup_basic.hlsl"
#include "nbl/builtin/hlsl/workgroup/basic.hlsl"
#include "nbl/builtin/hlsl/workgroup/arithmetic.hlsl"
#include "nbl/builtin/hlsl/type_traits.hlsl"
#include "nbl/builtin/hlsl/math/morton.hlsl"
#include "nbl/builtin/hlsl/colorspace/EOTF.hlsl"
#include "nbl/builtin/hlsl/colorspace/OETF.hlsl"
#include "nbl/builtin/hlsl/colorspace/encodeCIEXYZ.hlsl"
namespace nbl
{
namespace hlsl
{
namespace luma_meter
{
struct LumaMeteringWindow
{
float32_t2 meteringWindowScale;
float32_t2 meteringWindowOffset;
};
template<uint32_t GroupSize, typename ValueAccessor, typename SharedAccessor, typename TexAccessor>
struct geom_luma_meter {
using this_t = geom_luma_meter<GroupSize, ValueAccessor, SharedAccessor, TexAccessor>;
static this_t create(NBL_REF_ARG(LumaMeteringWindow) window, float32_t lumaMinimum, float32_t lumaMaximum)
{
this_t retval;
retval.window = window;
retval.minLuma = lumaMinimum;
retval.maxLuma = lumaMaximum;
return retval;
}
float32_t reduction(float32_t value, NBL_REF_ARG(SharedAccessor) sdata)
{
return workgroup::reduction < plus < float32_t >, GroupSize >::
template __call <SharedAccessor>(value, sdata);
}
float32_t computeLuma(
NBL_REF_ARG(TexAccessor) tex,
uint32_t2 sampleCount,
uint32_t2 sampleIndex,
float32_t2 viewportSize
)
{
float32_t2 stride = window.meteringWindowScale / (sampleCount + float32_t2(1.0f, 1.0f));
float32_t2 samplePos = stride * sampleIndex;
float32_t2 uvPos = (samplePos + float32_t2(0.5f, 0.5f)) / viewportSize;
float32_t3 color = colorspace::oetf::sRGB(tex.get(uvPos));
float32_t luma = dot(colorspace::sRGBtoXYZ[1], color);
luma = clamp(luma, minLuma, maxLuma);
return log2(luma / minLuma) / log2(maxLuma / minLuma);
}
void gatherLuma(
NBL_REF_ARG(ValueAccessor) val,
NBL_REF_ARG(TexAccessor) tex,
NBL_REF_ARG(SharedAccessor) sdata,
uint32_t2 sampleCount,
float32_t2 viewportSize
)
{
uint32_t2 coord = {
morton2d_decode_x(glsl::gl_LocalInvocationIndex()),
morton2d_decode_y(glsl::gl_LocalInvocationIndex())
};
uint32_t tid = workgroup::SubgroupContiguousIndex();
uint32_t2 sampleIndex = coord * GroupSize + float32_t2(glsl::gl_SubgroupID() + 1, glsl::gl_SubgroupInvocationID() + 1);
float32_t luma = 0.0f;
if (sampleIndex.x <= sampleCount.x && sampleIndex.y <= sampleCount.y) {
luma = computeLuma(tex, sampleCount, sampleIndex, viewportSize);
float32_t lumaSum = reduction(luma, sdata);
sdata.workgroupExecutionAndMemoryBarrier();
if (tid == GroupSize - 1) {
uint32_t3 workGroupCount = glsl::gl_NumWorkGroups();
uint32_t fixedPointBitsLeft = 32 - uint32_t(ceil(log2(workGroupCount.x * workGroupCount.y * workGroupCount.z))) + glsl::gl_SubgroupSizeLog2();
uint32_t lumaSumBitPattern = uint32_t(clamp((lumaSum - log2(minLuma)) * (log2(maxLuma) - log2(minLuma)), 0.f, float32_t((1 << fixedPointBitsLeft) - 1)));
uint32_t3 workgroupSize = glsl::gl_WorkGroupSize();
uint32_t workgroupIndex = dot(uint32_t3(workgroupSize.y * workgroupSize.z, workgroupSize.z, 1), glsl::gl_WorkGroupID());
val.atomicAdd(workgroupIndex & ((1 << glsl::gl_SubgroupSizeLog2()) - 1), lumaSumBitPattern);
}
}
}
LumaMeteringWindow window;
float32_t minLuma, maxLuma;
};
}
}
}
#endif