Skip to content

Bring PS5 GTA6 to 120FPS with DLSS 5. An advanced Windows video player and capture enhancement tool featuring real-time preview for videos, images, and capture card sources. Combine AI-powered Super Resolution, DLSS5 image enhancement, and Frame Generation to deliver a next-generation visual experience.

C++

hdr: static curve tuning + axes presets, dials, recommendation and live coverage (pure functions + tests)

georgeg911bd005a4d8e7b
diff --git a/include/veyra/engine/HdrOutputTuning.h b/include/veyra/engine/HdrOutputTuning.h
index e9a308d3..2bf9efc8 100644
--- a/include/veyra/engine/HdrOutputTuning.h
+++ b/include/veyra/engine/HdrOutputTuning.h
@@ -2,6 +2,7 @@
#include <algorithm>
#include <cmath>
+#include <string_view>
#include <cstddef>
#include <cstdint>
#include <cstring>
@@ -131,6 +132,75 @@ inline const HdrTuningPreset* findHdrTuningPreset(const char* id) {
return nullptr;
}
+// ---------------------------------------------------------------------------
+// One-click recommendation and the highlight warning (custom).
+//
+// The file itself declares how bright it is: mastering display peak, the
+// brightest single frame (MaxCLL) and the average of the brightest frames
+// (MaxFALL). Those numbers arrive with the container, so they exist even for
+// hardware-decoded files - unlike a measured histogram, which the D3D12 decode
+// path never fills (it has no CPU luma at all).
+//
+// Both helpers below are pure functions of those numbers and the display peak,
+// so the UI button, the tests and this comment cannot drift apart.
+// ---------------------------------------------------------------------------
+struct HdrTuningRecommendation {
+ const char* presetId = "standard";
+ unsigned strengthPercent = 100;
+ unsigned displayPeakNits = 0;
+ // Translation key for the "why this preset" line:
+ // dark-content / bright-content / narrow-range / balanced / no-metadata.
+ const char* reasonKey = "balanced";
+ unsigned cllNits = 0;
+ unsigned fallNits = 0;
+ bool available = false;
+};
+
+inline HdrTuningRecommendation recommendHdrTuning(unsigned masteringPeakNits, unsigned maxCllNits,
+ unsigned maxFallNits, unsigned displayPeakNits) {
+ HdrTuningRecommendation r;
+ r.displayPeakNits = displayPeakNits;
+ const unsigned cll = maxCllNits ? maxCllNits : masteringPeakNits;
+ // No metadata at all (screen capture, some streams): say so instead of guessing.
+ if (cll == 0) {
+ r.reasonKey = "no-metadata";
+ return r;
+ }
+ const unsigned fall = maxFallNits ? std::min(maxFallNits, cll) : cll;
+ r.cllNits = cll;
+ r.fallNits = fall;
+ r.available = true;
+ // MaxFALL/MaxCLL describes the shape of the film. A low ratio means long dark
+ // stretches with a few bright highlights (night exteriors, space, thrillers);
+ // a high ratio means the bright end is in use all the time (sports, fireworks,
+ // animation). A very high MaxCLL alone means the highlight range is extreme
+ // enough that protection matters regardless of the average.
+ const double ratio = double(fall) / double(cll);
+ // The display's own ceiling matters as much as the film's peak: content reaching far
+ // beyond what the panel can show needs the highlight ceiling even when its average is
+ // ordinary. A displayPeakNits of 0 means "unknown" (the caller could not tell), so the
+ // branch is skipped rather than guessed at.
+ const bool beyondDisplay = displayPeakNits > 0 && cll > displayPeakNits + displayPeakNits / 2;
+ // Order matters: a dark film wants the lift even when its ceiling is low, and a low
+ // ceiling means "punch" before it means "protect the highlights" - an old transfer has
+ // no extreme highlights left to protect.
+ if (ratio <= 0.20) {
+ // A dark film on a dim display needs both ends: lift the shadows and cap the top.
+ r.presetId = beyondDisplay ? "darkAndBright" : "darkLift";
+ r.reasonKey = beyondDisplay ? "dark-and-bright" : "dark-content";
+ } else if (masteringPeakNits && masteringPeakNits < 600) {
+ r.presetId = "punch";
+ r.reasonKey = "narrow-range";
+ } else if (beyondDisplay || cll >= 4000 || ratio >= 0.45) {
+ r.presetId = "highlightGuard";
+ r.reasonKey = beyondDisplay ? "beyond-display" : "bright-content";
+ } else {
+ r.presetId = "standard";
+ r.reasonKey = "balanced";
+ }
+ return r;
+}
+
// Does the stored baseline still match this preset? The strength dial and the
// display peak are deliberately ignored: they only scale the baseline when it is
// applied, so moving either of them keeps you on the same preset. Editing the
@@ -208,6 +278,314 @@ inline void resolveHdrTuning(const HdrTuningPreset& preset, unsigned strengthPer
resolveHdrTuning(base, preset.metadata, curve, metadata);
}
+// The curve's effective hard cap after the display-peak rescale and the strength
+// dial (0 = the curve caps nothing). The highlight warning compares this with the
+// source's own peak, which is what "how much of this film gets clipped" means.
+inline unsigned hdrEffectivePeakCapNits(const HdrCurveSettings& base, const HdrMetadataSettings& baseMetadata) {
+ HdrCurveSettings curve;
+ HdrMetadataSettings metadata;
+ resolveHdrTuning(base, baseMetadata, curve, metadata);
+ return (curve.enabled && curve.strengthPercent > 0) ? curve.peakCapNits : 0u;
+}
+
+// The highlight warning as a pure function, so the guard below is covered by the
+// unit tests instead of only by the UI. A source without metadata reports peak 0
+// while an enabled curve can still carry a cap: dividing there killed the UI
+// thread once (0xC0000094), which is exactly what this keeps from coming back.
+struct HdrPeakWarning {
+ bool valid = false;
+ unsigned sourcePeakNits = 0;
+ unsigned capNits = 0;
+ unsigned keptPercent = 100;
+ bool clipped = false;
+};
+
+inline HdrPeakWarning hdrPeakWarning(unsigned sourcePeakNits, bool sourceIsHdr, bool curveEnabled,
+ const HdrCurveSettings& curve, const HdrMetadataSettings& metadata) {
+ HdrPeakWarning w;
+ w.sourcePeakNits = sourcePeakNits;
+ w.capNits = hdrEffectivePeakCapNits(curve, metadata);
+ w.valid = sourceIsHdr && sourcePeakNits > 0 && curveEnabled;
+ w.keptPercent = (w.capNits && sourcePeakNits)
+ ? unsigned(std::min<unsigned long long>(100, w.capNits * 100ull / sourcePeakNits))
+ : 100;
+ w.clipped = w.valid && w.capNits > 0 && w.capNits < sourcePeakNits;
+ return w;
+}
+
+// ---------------------------------------------------------------------------
+// Content x environment presets and the plain-language dials (custom).
+//
+// The legacy seven presets stay exactly as they are (kHdrTuningPresets); this is
+// the second, more explainable way to pick a curve: the content axis says what the
+// film looks like, the environment axis says where you watch. Environment only
+// moves the mid-gray reference - the highlight behaviour belongs to the content,
+// so one axis can never undo the other.
+//
+// The three dials are the same idea for manual tuning: each level maps onto the
+// existing parameters, and the inverse lookup picks the nearest level, so the
+// panel can show "暗部提亮 3" instead of "shadowLiftEv100 30". The exact technical
+// values stay visible under the advanced section.
+// ---------------------------------------------------------------------------
+struct HdrContentPreset {
+ const char* id;
+ HdrCurveSettings curve; // strengthPercent/displayPeakNits are filled by the caller
+};
+
+inline constexpr HdrContentPreset kHdrContentPresets[] = {
+ // Curves carried over from the legacy presets so the two systems agree on what
+ // "dark" or "highlight" means; the environment adds only the mid-gray shift.
+ {"standard", {true, 0, 10, 203, 0, 100, 0}},
+ {"dark", {true, 50, 35, 225, 0, 100, 0}},
+ {"highlight", {true, 0, 10, 203, 450, 120, 1000}},
+ {"darkAndBright",{true, 50, 35, 220, 450, 120, 1000}},
+ {"punch", {true, 0, 10, 175, 450, 110, 1200}},
+};
+
+// The environment axis now names what it does rather than a time of day: the ids are
+// the action ("compensate" raises the picture for a bright room, "plain" leaves the
+// content's own baseline alone). The old ids are still mapped below so a preference
+// stored by an earlier build keeps its meaning.
+inline constexpr const char* kHdrEnvironmentIds[] = {"compensate", "plain"};
+inline constexpr unsigned kHdrEnvironmentBrightShift = 45; // mid-gray up in daylight
+inline constexpr unsigned kHdrEnvironmentDarkShift = 0; // no compensation: the content baseline stands
+// Ambient light raises the display black level (veiling glare), which buries dark
+// detail, so daylight also gets a small range-limited shadow lift - this mirrors what
+// ambient-adaptive HDR does (a very dark scene in a very bright room gets the most
+// correction, a dark room needs little). It stays at the bottom end on purpose: the
+// highlight fields remain the content axis business.
+// Matched to the legacy "明亮环境" preset (0.30 EV over 30 nits) so the two preset
+// systems describe a bright room the same way.
+inline constexpr int kHdrEnvironmentBrightLiftEv100 = 30;
+inline constexpr unsigned kHdrEnvironmentBrightLiftRangeNits = 30;
+
+inline const HdrContentPreset* findHdrContentPreset(const char* id) {
+ if (!id) return nullptr;
+ for (const auto& p : kHdrContentPresets)
+ if (std::string_view(p.id) == id) return &p;
+ return nullptr;
+}
+
+// The curve for one axis pair. Unknown ids fall back to the neutral content, which
+// keeps the UI honest if a stored choice disappears in a later version.
+inline HdrCurveSettings hdrAxesCurve(const char* content, const char* environment) {
+ const HdrContentPreset* preset = findHdrContentPreset(content);
+ if (!preset) preset = findHdrContentPreset("standard");
+ HdrCurveSettings curve = preset->curve;
+ // Legacy ids: "bright" was the compensation and "dark" the no-compensation case
+ // before the rename, so both keep their old meaning here.
+ std::string_view env = environment ? std::string_view(environment) : std::string_view("compensate");
+ if (env == "bright") env = "compensate";
+ else if (env == "dark") env = "plain";
+ const bool compensate = env != "plain";
+ const int shift = compensate ? int(kHdrEnvironmentBrightShift) : -int(kHdrEnvironmentDarkShift);
+ curve.midGrayNits = unsigned(std::clamp(int(curve.midGrayNits) + shift, 100, 500));
+ if (compensate) {
+ curve.shadowLiftEv100 = std::clamp(curve.shadowLiftEv100 + kHdrEnvironmentBrightLiftEv100, -200, 200);
+ curve.shadowRangeNits = std::clamp(std::max(curve.shadowRangeNits, kHdrEnvironmentBrightLiftRangeNits), 1u, 100u);
+ }
+ return curve;
+}
+
+// The recommendation names a legacy preset, but in the content x environment mode
+// the same finding has to name a content. One mapping, so the two systems can never
+// disagree about which finding means what. The environment presets carry no content
+// statement, so they map to the neutral content.
+inline const char* hdrAxesContentForPreset(const char* presetId) {
+ if (!presetId) return "standard";
+ const std::string_view id(presetId);
+ if (id == "darkLift") return "dark";
+ if (id == "highlightGuard") return "highlight";
+ if (id == "darkAndBright") return "darkAndBright";
+ if (id == "punch") return "punch";
+ return "standard";
+}
+
+// ------------------------------------------------------------------ the dials
+// Step tables: the inverse lookup below finds the nearest entry, so a curve edited
+// with the technical sliders still shows a sensible dial position.
+struct HdrShadowLiftStep { unsigned level; int liftEv100; unsigned rangeNits; };
+inline constexpr HdrShadowLiftStep kHdrShadowLiftSteps[] = {
+ {0, 0, 10}, {1, 10, 23}, {2, 20, 26}, {3, 30, 29}, {4, 40, 32}, {5, 50, 35},
+ {6, 60, 38}, {7, 70, 41}, {8, 80, 44}, {9, 90, 47}, {10, 100, 50},
+};
+inline constexpr unsigned kHdrBrightnessStepCount = 11; // level 0..10 -> 100..500 nit
+inline constexpr unsigned kHdrHighlightStepCount = 4; // off / weak / medium / strong
+
+struct HdrHighlightStep { unsigned level; unsigned startNits; unsigned rollOffPercent; unsigned capNits; };
+inline constexpr HdrHighlightStep kHdrHighlightSteps[] = {
+ {0, 0, 100, 0}, {1, 600, 110, 1200}, {2, 450, 120, 1000}, {3, 300, 130, 800},
+};
+
+inline unsigned hdrBrightnessNits(unsigned level) {
+ return std::min(500u, 100u + std::min(level, kHdrBrightnessStepCount - 1) * 40u);
+}
+
+inline unsigned hdrNearestLevel(unsigned value, unsigned count, const unsigned* values) {
+ unsigned best = 0, bestDistance = ~0u;
+ for (unsigned level = 0; level < count; ++level) {
+ const unsigned distance = value > values[level] ? value - values[level] : values[level] - value;
+ if (distance < bestDistance) { bestDistance = distance; best = level; }
+ }
+ return best;
+}
+
+inline unsigned hdrShadowLiftLevel(const HdrCurveSettings& curve) {
+ unsigned values[std::size(kHdrShadowLiftSteps)];
+ for (std::size_t i = 0; i < std::size(kHdrShadowLiftSteps); ++i)
+ values[i] = unsigned(std::max(0, kHdrShadowLiftSteps[i].liftEv100));
+ return hdrNearestLevel(unsigned(std::max(0, curve.shadowLiftEv100)), unsigned(std::size(values)), values);
+}
+
+inline void setHdrShadowLiftLevel(HdrCurveSettings& curve, unsigned level) {
+ const auto& step = kHdrShadowLiftSteps[std::min(level, unsigned(std::size(kHdrShadowLiftSteps)) - 1)];
+ curve.shadowLiftEv100 = step.liftEv100;
+ curve.shadowRangeNits = step.rangeNits;
+}
+
+inline unsigned hdrBrightnessLevel(const HdrCurveSettings& curve) {
+ unsigned values[kHdrBrightnessStepCount];
+ for (unsigned i = 0; i < kHdrBrightnessStepCount; ++i) values[i] = hdrBrightnessNits(i);
+ return hdrNearestLevel(curve.midGrayNits, kHdrBrightnessStepCount, values);
+}
+
+inline void setHdrBrightnessLevel(HdrCurveSettings& curve, unsigned level) {
+ curve.midGrayNits = hdrBrightnessNits(level);
+}
+
+inline unsigned hdrHighlightLevel(const HdrCurveSettings& curve) {
+ // Matched on the pair that gives the step its character: the ceiling and the
+ // roll-off distance. A curve with no ceiling is "off" by definition.
+ unsigned best = 0, bestDistance = ~0u;
+ for (const auto& step : kHdrHighlightSteps) {
+ const unsigned distance = (curve.peakCapNits > step.capNits ? curve.peakCapNits - step.capNits
+ : step.capNits - curve.peakCapNits) +
+ (curve.rollOffPercent > step.rollOffPercent
+ ? curve.rollOffPercent - step.rollOffPercent
+ : step.rollOffPercent - curve.rollOffPercent);
+ if (distance < bestDistance) { bestDistance = distance; best = step.level; }
+ }
+ return best;
+}
+
+inline void setHdrHighlightLevel(HdrCurveSettings& curve, unsigned level) {
+ for (const auto& step : kHdrHighlightSteps)
+ if (step.level == std::min(level, kHdrHighlightStepCount - 1)) {
+ curve.highlightStartNits = step.startNits;
+ curve.rollOffPercent = step.rollOffPercent;
+ curve.peakCapNits = step.capNits;
+ return;
+ }
+}
+
+// ---------------------------------------------------------------------------
+// Curve sampling for the panel graph (custom).
+//
+// This is a line-by-line port of HdrCurveLuminance in shaders/PresentBlit.hlsl, so
+// the drawn curve cannot promise something the GPU does not do. Keep the two in
+// step: every branch here mirrors the shader's branch, including the defaults
+// (lift 0, mid 203, start 0, cap 0 all being no-ops).
+// ---------------------------------------------------------------------------
+inline double hdrCurveRollOffScaleNits(const HdrCurveSettings& curve) {
+ // Mirrors the presenter: peak - start, scaled by the roll-off percentage.
+ const double peak = curve.peakCapNits ? double(curve.peakCapNits) : 1000.0;
+ const double start = double(curve.highlightStartNits);
+ return std::max(1.0, peak - start) * double(curve.rollOffPercent) / 100.0;
+}
+
+inline double hdrCurveLuminance(const HdrCurveSettings& curve, double nits) {
+ const double liftEv = double(curve.shadowLiftEv100) / 100.0;
+ const double range = double(curve.shadowRangeNits);
+ const double mid = double(curve.midGrayNits);
+ const double start = double(curve.highlightStartNits);
+ const double scale = hdrCurveRollOffScaleNits(curve);
+ const double cap = double(curve.peakCapNits);
+ if (liftEv != 0.0 && range > 0.0) {
+ const double w = std::clamp(1.0 - nits / range, 0.0, 1.0);
+ nits *= 1.0 + w * w * (std::exp2(liftEv) - 1.0);
+ }
+ if (mid > 0.0 && std::abs(mid - 203.0) > 0.5) {
+ const double peak = cap > 0.0 ? cap : 1000.0;
+ if (peak > mid + 1.0) {
+ const double g = std::log(mid / peak) / std::log(203.0 / peak);
+ nits = peak * std::pow(std::max(nits / peak, 0.0), g);
+ }
+ }
+ if (start > 0.0 && scale > 0.0 && nits > start)
+ nits = start + (nits - start) / (1.0 + (nits - start) / scale);
+ if (cap > 0.0) nits = std::min(nits, cap);
+ return nits;
+}
+
+// Sample points for the panel graph: a fixed logarithmic sweep of the input, low to
+// high, so the graph shows the same shape regardless of the panel size.
+inline constexpr unsigned kHdrCurveGraphPoints = 48;
+inline constexpr double kHdrCurveGraphMinNits = 0.5;
+inline constexpr double kHdrCurveGraphMaxNits = 4000.0;
+
+inline double hdrCurveGraphInputNits(unsigned index) {
+ const double t = double(index) / double(kHdrCurveGraphPoints - 1);
+ return kHdrCurveGraphMinNits * std::pow(kHdrCurveGraphMaxNits / kHdrCurveGraphMinNits, t);
+}
+// ---------------------------------------------------------------------------
+// Live coverage of one measured frame (custom).
+//
+// This is what the panel's live meter shows: of the luminances just measured (the
+// presenter feeds it a 64x36 probe of the frame), how many are above the curve's
+// ceiling (the picture is being crushed flat there), how many are inside the
+// roll-off (compressed but not flat) and how many are inside the shadow-lift range
+// (being raised). Callers pass an already resolved curve, so the display rescale
+// and the strength dial are included in the thresholds.
+//
+// Pure on purpose: the numbers the panel displays are defined here and covered by
+// the unit tests, not computed inside the render loop.
+// ---------------------------------------------------------------------------
+struct HdrCurveCoverage {
+ unsigned samples = 0;
+ unsigned aboveCeiling = 0;
+ unsigned inRollOff = 0;
+ unsigned inLift = 0;
+ bool valid = false;
+ double aboveCeilingPercent() const { return samples ? 100.0 * double(aboveCeiling) / double(samples) : 0.0; }
+ double inRollOffPercent() const { return samples ? 100.0 * double(inRollOff) / double(samples) : 0.0; }
+ double inLiftPercent() const { return samples ? 100.0 * double(inLift) / double(samples) : 0.0; }
+};
+
+inline HdrCurveCoverage hdrCurveCoverage(const double* nits, unsigned count, const HdrCurveSettings& curve) {
+ HdrCurveCoverage coverage;
+ coverage.valid = curve.enabled && nits != nullptr && count > 0;
+ if (!coverage.valid) return coverage;
+ coverage.samples = count;
+ const double cap = double(curve.peakCapNits);
+ const double start = double(curve.highlightStartNits);
+ const double range = double(curve.shadowRangeNits);
+ for (unsigned i = 0; i < count; ++i) {
+ const double value = nits[i];
+ if (!std::isfinite(value) || value <= 0.0) continue;
+ if (cap > 0.0 && value > cap) ++coverage.aboveCeiling;
+ else if (start > 0.0 && value > start) ++coverage.inRollOff;
+ if (range > 0.0 && value < range) ++coverage.inLift;
+ }
+ return coverage;
+}
+// SMPTE ST 2084 (PQ) EOTF: an encoded code value back to absolute nits. The coverage
+// probe renders into the swapchain's 10-bit UNORM format, which can only carry PQ
+// code values - writing linear nits there clamped everything above 1.0 (= 80 nits)
+// flat, so no cycle could ever report a highlight bucket. Keeping the encode on and
+// decoding here preserves the whole 0..10000 nit range.
+inline double pqCodeToNits(double normalized) {
+ const double n = std::clamp(normalized, 0.0, 1.0);
+ const double m1 = 2610.0 / 16384.0;
+ const double m2 = 2523.0 / 4096.0 * 128.0;
+ const double c1 = 3424.0 / 4096.0;
+ const double c2 = 2413.0 / 4096.0 * 32.0;
+ const double c3 = 2392.0 / 4096.0 * 32.0;
+ const double p = std::pow(n, 1.0 / m2);
+ const double numerator = std::max(p - c1, 0.0);
+ const double denominator = c2 - c3 * p;
+ if (denominator <= 0.0) return 10000.0;
+ return 10000.0 * std::pow(numerator / denominator, 1.0 / m1);
+}
// Optional per-frame metadata is sticky within one source epoch only. The
// presenter owns this state; reopening it or switching sources must clear it.
struct HdrSourceMetadata {
@@ -239,4 +617,21 @@ inline HdrMetadataLuminance resolveHdrMetadataLuminance(const HdrMetadataSetting
return result;
}
+// The brightness curve runs in the present shader, so what a display should be told is
+// the *output*, not the source: with a cap active the source peak describes a level the
+// picture no longer reaches, and the display's own tone mapping would then work from a
+// value that is too high. Bound the content light levels by the cap the shader really
+// applies (hdrEffectivePeakCapNits resolves the same curve and strength, and returns 0
+// when the curve is inactive, in which case the source values stand as they are).
+inline HdrMetadataLuminance clampMetadataToCurve(HdrMetadataLuminance light,
+ const HdrCurveSettings& curve,
+ const HdrMetadataSettings& settings) {
+ const unsigned cap=hdrEffectivePeakCapNits(curve,settings);
+ if(cap){
+ light.cll=std::min(light.cll,cap);
+ light.fall=std::min(light.fall,light.cll);
+ }
+ return light;
+}
+
} // namespace veyra::engine
diff --git a/include/veyra/pipeline/EnhanceGraph.h b/include/veyra/pipeline/EnhanceGraph.h
index b561e75b..e1d6d16c 100644
--- a/include/veyra/pipeline/EnhanceGraph.h
+++ b/include/veyra/pipeline/EnhanceGraph.h
@@ -540,6 +540,10 @@ public:
bool nrEnabled() const { return nrEnabled_; }
bool fgEnabled() const { return fgEnabled_; }
bool hdrOutput() const { return desc_.hdrOutput; }
+ // Whether the *source* is HDR (PQ/HLG ingress). The brightness curve is restricted
+ // to real HDR sources: an SDR source has no true highlights, so the roll-off and
+ // the ceiling could never engage and the control would silently do nothing.
+ bool hdrInput() const { return desc_.hdrInput; }
bool videoHdrActive() const {return desc_.convertVideoHdr();}
// Custom: static HDR output tuning. The curve is read by the present blit,
// the metadata is sent to the display.
diff --git a/tests/unit/HdrOutputTuningTests.cpp b/tests/unit/HdrOutputTuningTests.cpp
index 69167405..62f6a07f 100644
--- a/tests/unit/HdrOutputTuningTests.cpp
+++ b/tests/unit/HdrOutputTuningTests.cpp
@@ -225,9 +225,249 @@ int main() {
check(resolveHdrMetadataLuminance({},source).fall==800,"metadata: MaxFALL never exceeds MaxCLL");
source.observe(11,false,4000,2000,800);
check(resolveHdrMetadataLuminance({},source)==HdrMetadataLuminance{},"metadata: SDR clears HDR source values");
+ // ③: an SDR clip must not be described by the generic HDR10 fallback, and the
+ // clamp must leave values alone while the curve is inactive.
+ const auto fallback=resolveHdrMetadataLuminance({},HdrSourceMetadata{});
+ {
+ HdrCurveSettings offCurve;
+ check(fallback.peak==1000&&fallback.cll==1000,
+ "metadata: an SDR clip resolves to the generic 1000-nit fallback (hence 3 gates it out)");
+ check(clampMetadataToCurve(fallback,offCurve,{})==fallback,
+ "metadata: an inactive curve leaves the reported values untouched");
+ }
+ // ①: with a cap the reported content light levels must not exceed what the
+ // present shader really outputs, and MaxFALL must stay under MaxCLL.
+ {
+ HdrCurveSettings capped;capped.enabled=true;capped.peakCapNits=300;capped.strengthPercent=100;
+ const auto bounded=clampMetadataToCurve(fallback,capped,{});
+ check(bounded.cll==300,"metadata: a 300-nit cap bounds MaxCLL");
+ check(bounded.fall<=bounded.cll,"metadata: MaxFALL stays within MaxCLL after clamping");
+ check(bounded.peak==fallback.peak,"metadata: the mastering peak keeps describing the master");
+ HdrCurveSettings zeroStrength=capped;zeroStrength.strengthPercent=0;
+ check(clampMetadataToCurve(fallback,zeroStrength,{})==fallback,
+ "metadata: a curve at zero strength does not rewrite the hint");
+ }
HdrMetadataSettings manual{true,700,800,900,50};
const auto light=resolveHdrMetadataLuminance(manual,source);
check(light.peak==700&&light.minX10000==50&&light.fall==800,"metadata: whole-nit maximum, 0.0001-nit minimum and bounded FALL");
+ // Custom: the one-click recommendation is a pure function, so the table the UI
+ // shows and the tests below cannot drift apart.
+ {
+ const auto dark=recommendHdrTuning(1000,1000,150,1000);
+ check(dark.available&&std::string(dark.presetId)=="darkLift"&&std::string(dark.reasonKey)=="dark-content",
+ "recommend: a dark film with few bright frames maps to darkLift");
+ const auto narrow=recommendHdrTuning(400,600,320,1000);
+ check(std::string(narrow.presetId)=="punch"&&std::string(narrow.reasonKey)=="narrow-range",
+ "recommend: a low mastering ceiling maps to punch before highlight protection");
+ const auto bright=recommendHdrTuning(4000,9978,4800,1000);
+ check(std::string(bright.presetId)=="highlightGuard"&&std::string(bright.reasonKey)=="beyond-display",
+ "recommend: an extremely bright film on a 1000-nit display is named as beyond it");
+ const auto extremeBright=recommendHdrTuning(4000,4000,2400,10000);
+ check(std::string(extremeBright.presetId)=="highlightGuard"&&std::string(extremeBright.reasonKey)=="bright-content",
+ "recommend: extreme content on a display that can show it keeps the bright-content reason");
+ const auto balanced=recommendHdrTuning(1000,1000,380,1000);
+ check(std::string(balanced.presetId)=="standard"&&std::string(balanced.reasonKey)=="balanced",
+ "recommend: a balanced film keeps the standard curve");
+ // The display's ceiling must take part in the decision: content far beyond the
+ // panel needs the highlight ceiling even when its average is ordinary, and a dark
+ // film on a dim display needs both ends at once. displayPeak 0 means "unknown".
+ {
+ const auto dimDark=recommendHdrTuning(1000,1000,150,300);
+ check(std::string(dimDark.presetId)=="darkAndBright"&&std::string(dimDark.reasonKey)=="dark-and-bright",
+ "recommend: a dark film on a dim display gets both ends");
+ const auto dimBright=recommendHdrTuning(4000,2000,800,300);
+ check(std::string(dimBright.presetId)=="highlightGuard"&&std::string(dimBright.reasonKey)=="beyond-display",
+ "recommend: content beyond the panel maps to highlight protection, named as such");
+ const auto unknownDisplay=recommendHdrTuning(1000,1000,150,0);
+ check(std::string(unknownDisplay.presetId)=="darkLift",
+ "recommend: an unknown display peak skips the beyond-display branch");
+ const auto brightDisplay=recommendHdrTuning(1000,1000,150,2000);
+ check(std::string(brightDisplay.presetId)=="darkLift"&&std::string(brightDisplay.reasonKey)=="dark-content",
+ "recommend: a display above the content peak needs no extra protection");
+ }
+ const auto missing=recommendHdrTuning(0,0,0,1000);
+ check(!missing.available&&std::string(missing.reasonKey)=="no-metadata",
+ "recommend: a source without metadata is reported instead of guessed");
+ check(recommendHdrTuning(1000,3000,150,1000).fallNits==150&&recommendHdrTuning(1000,3000,150,1000).cllNits==3000,
+ "recommend: MaxCLL and MaxFALL are carried through for the hint text");
+ // The renamed environment ids state the action; the old ones must keep working.
+ {
+ const auto plain=hdrAxesCurve("standard","plain");
+ const auto compensate=hdrAxesCurve("standard","compensate");
+ check(plain.midGrayNits==203&&plain.shadowLiftEv100==0,
+ "environment: plain leaves the neutral content untouched (203 nit, no lift)");
+ check(compensate.midGrayNits==203+kHdrEnvironmentBrightShift&&compensate.shadowLiftEv100>0,
+ "environment: compensate raises the mid-gray and the shadows");
+ const auto legacyDark=hdrAxesCurve("standard","dark");
+ const auto legacyBright=hdrAxesCurve("standard","bright");
+ check(legacyDark.midGrayNits==plain.midGrayNits&&legacyDark.shadowLiftEv100==plain.shadowLiftEv100,
+ "environment: the legacy dark id still means plain");
+ check(legacyBright.midGrayNits==compensate.midGrayNits,
+ "environment: the legacy bright id still means compensate");
+ }
+ // The two preset systems must agree on what a finding means.
+ check(std::string(hdrAxesContentForPreset("darkLift"))=="dark"&&
+ std::string(hdrAxesContentForPreset("highlightGuard"))=="highlight"&&
+ std::string(hdrAxesContentForPreset("punch"))=="punch"&&
+ std::string(hdrAxesContentForPreset("standard"))=="standard",
+ "mapping: every recommended legacy preset names a content");
+ check(std::string(hdrAxesContentForPreset("brightRoom"))=="standard"&&
+ std::string(hdrAxesContentForPreset(nullptr))=="standard",
+ "mapping: environment-only presets and null fall back to the neutral content");
+ // The effective cap follows the display rescale and the strength dial: it is
+ // what the highlight warning compares the source peak against.
+ HdrCurveSettings curve;HdrMetadataSettings metadata;
+ curve.enabled=true;curve.peakCapNits=1000;curve.displayPeakNits=0;curve.strengthPercent=100;
+ check(hdrEffectivePeakCapNits(curve,metadata)==1000,"cap: 100% strength keeps the authored ceiling");
+ curve.displayPeakNits=500;
+ check(hdrEffectivePeakCapNits(curve,metadata)==500,"cap: a 500 nit display halves the authored ceiling");
+ curve.displayPeakNits=0;curve.enabled=false;
+ check(hdrEffectivePeakCapNits(curve,metadata)==0,"cap: a disabled curve caps nothing");
+ // The warning must survive a source with no metadata at all: an enabled
+ // curve with a cap and a peak of 0 divided by zero and crashed the UI thread.
+ HdrCurveSettings warnCurve;HdrMetadataSettings warnMetadata;
+ warnCurve.enabled=true;warnCurve.peakCapNits=1000;warnCurve.displayPeakNits=0;warnCurve.strengthPercent=100;
+ const auto noSource=hdrPeakWarning(0,false,true,warnCurve,warnMetadata);
+ check(!noSource.valid&&noSource.keptPercent==100&&!noSource.clipped&&noSource.capNits==1000,
+ "warning: a source without metadata is inert instead of dividing by zero");
+ // The graph is a port of the shader, so the defaults must be exact no-ops
+ // and the headline behaviours must match the HLSL branch for branch.
+ {
+ const HdrCurveSettings identity; // lift 0, mid 203, start 0, cap 0
+ check(std::abs(hdrCurveLuminance(identity,100.0)-100.0)<1e-6&&
+ std::abs(hdrCurveLuminance(identity,0.25)-0.25)<1e-6,
+ "graph: the default curve is an exact identity");
+ // The live meter's numbers must be defined by tests, not by the render loop.
+ {
+ HdrCurveSettings curve;curve.enabled=true;curve.peakCapNits=1000;
+ curve.highlightStartNits=450;curve.shadowRangeNits=35;
+ const double frame[]={10.0,20.0,30.0,100.0,500.0,900.0,2000.0};
+ const auto coverage=hdrCurveCoverage(frame,7,curve);
+ check(coverage.valid&&coverage.samples==7,
+ "coverage: a valid curve counts every finite sample");
+ // The probe carries PQ code values; the decode must land on the published
+ // reference points, otherwise every threshold comparison is meaningless.
+ {
+ check(pqCodeToNits(0.0)==0.0,"pq: code zero is zero nits");
+ check(std::abs(pqCodeToNits(0.5081)-100.0)<2.0,
+ "pq: the published 100-nit code lands on 100 nits");
+ check(std::abs(pqCodeToNits(0.7518)-1000.0)<20.0,
+ "pq: the published 1000-nit code lands on 1000 nits");
+ check(std::abs(pqCodeToNits(1.0)-10000.0)<1.0,"pq: full scale is 10000 nits");
+ double previous=-1.0;bool rising=true;
+ for(unsigned i=0;i<=100;++i){
+ const double value=pqCodeToNits(double(i)/100.0);
+ if(value<previous)rising=false;
+ previous=value;
+ }
+ check(rising,"pq: the decode is monotonic");
+ }
+ check(coverage.aboveCeiling==1&&std::abs(coverage.aboveCeilingPercent()-100.0/7.0)<1e-9,
+ "coverage: only the sample above the ceiling is reported as crushed");
+ check(coverage.inRollOff==2,
+ "coverage: samples between the highlight start and the ceiling are roll-off");
+ check(coverage.inLift==3,
+ "coverage: samples below the lift range are counted as being raised");
+ const auto disabled=hdrCurveCoverage(frame,7,HdrCurveSettings{});
+ check(!disabled.valid&&disabled.samples==0&&disabled.aboveCeilingPercent()==0.0,
+ "coverage: a disabled curve reports nothing instead of zeroes that look real");
+ const auto empty=hdrCurveCoverage(nullptr,0,curve);
+ check(!empty.valid,"coverage: no samples means no reading");
+ // 20 nit sits inside the 35-nit lift range; 50 would not.
+ const double garbage[]={0.0,-5.0,std::nan(""),20.0};
+ const auto filtered=hdrCurveCoverage(garbage,4,curve);
+ check(filtered.samples==4&&filtered.aboveCeiling==0&&filtered.inRollOff==0&&filtered.inLift==1,
+ "coverage: zero, negative and non-finite samples are ignored by the buckets");
+ }
+ HdrCurveSettings lift;lift.shadowLiftEv100=50;lift.shadowRangeNits=35;
+ check(hdrCurveLuminance(lift,5.0)>5.0&&hdrCurveLuminance(lift,200.0)==200.0,
+ "graph: the shadow lift raises dark values and leaves bright ones alone");
+ HdrCurveSettings capped;capped.peakCapNits=1000;capped.highlightStartNits=450;
+ capped.rollOffPercent=120;
+ check(hdrCurveLuminance(capped,8000.0)==1000.0,
+ "graph: the hard cap is a hard cap");
+ check(hdrCurveLuminance(capped,600.0)<600.0,
+ "graph: the roll-off already compresses above the highlight start");
+ HdrCurveSettings mid;mid.midGrayNits=250;
+ // By construction the mapping is the identity at the peak (1000 nit with no cap);
+ // a raised mid-gray lifts below it and pulls above it down.
+ check(hdrCurveLuminance(mid,250.0)>250.0&&hdrCurveLuminance(mid,1000.0)==1000.0&&
+ hdrCurveLuminance(mid,2000.0)<2000.0,
+ "graph: a raised mid-gray lifts the middle, pivots at the peak and pulls the top down");
+ double previous=0.0;bool monotonic=true;
+ HdrCurveSettings all;all.enabled=true;all.shadowLiftEv100=40;all.shadowRangeNits=35;
+ all.midGrayNits=225;all.highlightStartNits=450;all.rollOffPercent=120;all.peakCapNits=1000;
+ for (unsigned i=1;i<kHdrCurveGraphPoints;++i) {
+ const double value=hdrCurveLuminance(all,hdrCurveGraphInputNits(i));
+ if (value+1e-9<previous) monotonic=false;
+ previous=value;
+ }
+ check(monotonic,"graph: every sample of a full curve stays monotonic");
+ check(hdrCurveGraphInputNits(0)==kHdrCurveGraphMinNits&&
+ std::abs(hdrCurveGraphInputNits(kHdrCurveGraphPoints-1)-kHdrCurveGraphMaxNits)<1e-6,
+ "graph: the sweep spans the advertised input range");
+ }
+ const auto clipped=hdrPeakWarning(4000,true,true,warnCurve,warnMetadata);
+ check(clipped.valid&&clipped.clipped&&clipped.keptPercent==25,
+ "warning: a 4000 nit source under a 1000 nit ceiling keeps a quarter of its peak");
+ const auto covered=hdrPeakWarning(800,true,true,warnCurve,warnMetadata);
+ check(covered.valid&&!covered.clipped&&covered.keptPercent==100,
+ "warning: a source below the ceiling is reported as safe");
+ const auto noCap=hdrPeakWarning(4000,true,true,HdrCurveSettings{},warnMetadata);
+ check(!noCap.clipped&&noCap.capNits==0,
+ "warning: a curve without a ceiling never reports clipping");
+ // Custom: the content x environment axes and the three plain dials. Both are
+ // pure mappings, so the UI can show readable controls without a second
+ // interpretation of the same numbers living in QML.
+ {
+ const auto darkNight=hdrAxesCurve("dark","dark");
+ const auto darkDay=hdrAxesCurve("dark","bright");
+ check(darkNight.shadowLiftEv100==50&&darkNight.highlightStartNits==0,
+ "axes: the dark content keeps its lift and adds no highlight ceiling");
+ check(darkDay.midGrayNits==225+kHdrEnvironmentBrightShift&&darkNight.midGrayNits==225-kHdrEnvironmentDarkShift,
+ "axes: the environment axis moves only the mid-gray reference");
+ check(darkNight.highlightStartNits==darkDay.highlightStartNits&&
+ darkNight.peakCapNits==darkDay.peakCapNits&&darkNight.rollOffPercent==darkDay.rollOffPercent,
+ "axes: the environment axis never touches the highlight behaviour");
+ check(darkDay.shadowLiftEv100==darkNight.shadowLiftEv100+kHdrEnvironmentBrightLiftEv100,
+ "axes: daylight lifts the shadows by the documented amount");
+ const auto standardDay=hdrAxesCurve("standard","bright");
+ check(standardDay.shadowLiftEv100>0&&standardDay.shadowRangeNits>=kHdrEnvironmentBrightLiftRangeNits,
+ "axes: even the neutral content gets a usable daylight shadow lift");
+ const auto highlight=hdrAxesCurve("highlight","bright");
+ check(highlight.peakCapNits==1000&&highlight.rollOffPercent==120&&highlight.highlightStartNits==450,
+ "axes: the highlight content carries its ceiling through both environments");
+ const auto unknown=hdrAxesCurve("does-not-exist","dark");
+ const auto standard=hdrAxesCurve("standard","dark");
+ check(unknown.shadowLiftEv100==standard.shadowLiftEv100&&unknown.midGrayNits==standard.midGrayNits,
+ "axes: an unknown content id falls back to the neutral baseline");
+ for (const auto& preset : kHdrContentPresets) {
+ const auto day=hdrAxesCurve(preset.id,"bright");
+ const auto night=hdrAxesCurve(preset.id,"dark");
+ check(day.midGrayNits>=100&&day.midGrayNits<=500&&night.midGrayNits>=100&&night.midGrayNits<=500,
+ "axes: every combination stays inside the valid mid-gray range");
+ }
+ HdrCurveSettings curve;
+ for (unsigned level=0;level<=10;++level) {
+ setHdrShadowLiftLevel(curve,level);
+ check(hdrShadowLiftLevel(curve)==level,"dials: the shadow-lift dial round-trips at every step");
+ }
+ for (unsigned level=0;level<=10;++level) {
+ setHdrBrightnessLevel(curve,level);
+ check(hdrBrightnessLevel(curve)==level,"dials: the brightness dial round-trips at every step");
+ }
+ for (unsigned level=0;level<kHdrHighlightStepCount;++level) {
+ setHdrHighlightLevel(curve,level);
+ check(hdrHighlightLevel(curve)==level,"dials: the highlight dial round-trips at every step");
+ }
+ check(hdrBrightnessLevel(hdrAxesCurve("standard","bright"))>=
+ hdrBrightnessLevel(hdrAxesCurve("standard","dark")),
+ "dials: the daylight curve reads brighter than the night one");
+ HdrCurveSettings edited;edited.shadowLiftEv100=31;edited.shadowRangeNits=29;
+ check(hdrShadowLiftLevel(edited)==3,"dials: an off-grid lift picks the nearest step");
+ }
+ }
+
}
// Real public persistence paths: old v10 Flow data is preserved and the new