Loading...
Searching...
No Matches
CaptureAdjust.hpp
Go to the documentation of this file.
1#pragma once
2
25#include <Gfx/Graph/Scale.hpp>
26
27#include <algorithm>
28#include <atomic>
29#include <cmath>
30#include <cstddef>
31#include <cstdint>
32#include <mutex>
33
34namespace score::gfx
35{
36
39{
43 float blackLevel[3]{0.f, 0.f, 0.f};
44
47 float whiteBalance[3]{1.f, 1.f, 1.f};
48
50 float exposure{1.f};
51
54 float gamma{1.f};
55
57 float saturation{1.f};
58
64 ScaleMode scaleMode{ScaleMode::Stretch};
65
66 friend bool operator==(const CaptureAdjust&, const CaptureAdjust&) noexcept
67 = default;
68};
69
80{
81public:
83 void set(const CaptureAdjust& v)
84 {
85 {
86 std::lock_guard lk{m_mutex};
87 if(m_value == v)
88 return;
89 m_value = v;
90 }
91 m_generation.fetch_add(1, std::memory_order_release);
92 }
93
96 bool poll(CaptureAdjust& out, std::uint32_t& lastSeen) const
97 {
98 const auto gen = m_generation.load(std::memory_order_acquire);
99 if(gen == lastSeen)
100 return false;
101 {
102 std::lock_guard lk{m_mutex};
103 out = m_value;
104 }
105 lastSeen = gen;
106 return true;
107 }
108
109 CaptureAdjust value() const
110 {
111 std::lock_guard lk{m_mutex};
112 return m_value;
113 }
114
115 std::uint32_t generation() const noexcept
116 {
117 return m_generation.load(std::memory_order_acquire);
118 }
119
120private:
121 mutable std::mutex m_mutex;
122 CaptureAdjust m_value;
123 std::atomic<std::uint32_t> m_generation{0};
124};
125
126#pragma pack(push, 1)
142{
143 float scale[2]{1.f, 1.f};
144 float textureSize[2]{1.f, 1.f};
145 float blackLevel[4]{0.f, 0.f, 0.f, 0.f};
146 float whiteBalance[4]{1.f, 1.f, 1.f, 0.f};
148 float params[4]{1.f, 1.f, 1.f, 0.f};
149};
150#pragma pack(pop)
151
152static_assert(sizeof(CaptureMaterialUBO) == 64);
153static_assert(offsetof(CaptureMaterialUBO, blackLevel) == 16);
154static_assert(offsetof(CaptureMaterialUBO, whiteBalance) == 32);
155static_assert(offsetof(CaptureMaterialUBO, params) == 48);
156
158inline void applyTo(const CaptureAdjust& a, CaptureMaterialUBO& ubo) noexcept
159{
160 for(int i = 0; i < 3; ++i)
161 {
162 ubo.blackLevel[i] = a.blackLevel[i];
163 ubo.whiteBalance[i] = a.whiteBalance[i];
164 }
165 ubo.params[0] = a.exposure;
166 ubo.params[1] = a.gamma;
167 ubo.params[2] = a.saturation;
168}
169
178inline void adjustCaptureReference(const CaptureAdjust& a, float rgb[3]) noexcept
179{
180 constexpr float eps = 1.f / 65535.f;
181
182 for(int i = 0; i < 3; ++i)
183 {
184 const float bl = a.blackLevel[i];
185 float c = (rgb[i] - bl) / std::max(eps, 1.f - bl);
186 c *= a.whiteBalance[i];
187 c *= a.exposure;
188 rgb[i] = std::max(0.f, c);
189 }
190
191 const float l
192 = 0.2126f * rgb[0] + 0.7152f * rgb[1] + 0.0722f * rgb[2];
193 for(int i = 0; i < 3; ++i)
194 rgb[i] = std::max(0.f, l + (rgb[i] - l) * a.saturation);
195
196 const float inv = 1.f / std::max(a.gamma, eps);
197 for(int i = 0; i < 3; ++i)
198 rgb[i] = std::clamp(std::pow(rgb[i], inv), 0.f, 1.f);
199}
200
201} // namespace score::gfx
Cross-thread handoff for CaptureAdjust.
Definition CaptureAdjust.hpp:80
bool poll(CaptureAdjust &out, std::uint32_t &lastSeen) const
Definition CaptureAdjust.hpp:96
void set(const CaptureAdjust &v)
Writer side. Any thread.
Definition CaptureAdjust.hpp:83
Graphics rendering pipeline for ossia score.
Definition Filter/PreviewWidget.hpp:11
void adjustCaptureReference(const CaptureAdjust &a, float rgb[3]) noexcept
The corrections, on the CPU, in the same order the shader applies them.
Definition CaptureAdjust.hpp:178
ScaleMode
How to resize a texture to adapt it to a viewport.
Definition Scale.hpp:10
void applyTo(const CaptureAdjust &a, CaptureMaterialUBO &ubo) noexcept
Fills the correction half of ubo. The geometry half is the caller's.
Definition CaptureAdjust.hpp:158
Sensor-side corrections, in the order the shader applies them.
Definition CaptureAdjust.hpp:39
float saturation
0 collapses to luma, 1 leaves it alone.
Definition CaptureAdjust.hpp:57
float gamma
Definition CaptureAdjust.hpp:54
float exposure
Linear multiplier, after white balance.
Definition CaptureAdjust.hpp:50
ScaleMode scaleMode
Definition CaptureAdjust.hpp:64
float whiteBalance[3]
Definition CaptureAdjust.hpp:47
float blackLevel[3]
Definition CaptureAdjust.hpp:43
The capture path's material block: the shared one, plus corrections.
Definition CaptureAdjust.hpp:142
float params[4]
exposure, gamma, saturation, unused
Definition CaptureAdjust.hpp:148