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++

feat(color): T2b-a master switch semantics and CPU-baked colour tables (23 CPU checks)

Veyra Goal Agent2668cdb58f80
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 160c1768..3fe519de 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -669,6 +669,13 @@ if(VEYRA_FFMPEG_ROOT AND EXISTS "${VEYRA_FFMPEG_ROOT}/include/libavformat/avform
target_compile_definitions(veyra_engine PUBLIC VEYRA_PROJECT_ROOT="${CMAKE_CURRENT_SOURCE_DIR}" VEYRA_SHADER_DIR="${CMAKE_BINARY_DIR}/shaders")
target_link_libraries(veyra_engine PUBLIC veyra_pipeline veyra_sources veyra_sinks veyra_ngx veyra_nvof)
add_executable(veyra_repair_preset_tests tests/unit/RepairPresetTests.cpp src/engine/PresetStore.cpp)
+ # CPU bake of the colour grade: no GPU, no engine link - the tables the ingest
+ # shader reads are pure maths and must be verifiable without a device.
+ add_executable(veyra_color_grade_tests tests/unit/ColorGradeTableTests.cpp src/pipeline/ColorGradeTables.cpp)
+ target_compile_features(veyra_color_grade_tests PRIVATE cxx_std_20)
+ target_compile_options(veyra_color_grade_tests PRIVATE /utf-8)
+ target_compile_definitions(veyra_color_grade_tests PRIVATE NOMINMAX WIN32_LEAN_AND_MEAN)
+ target_include_directories(veyra_color_grade_tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/include)
target_compile_features(veyra_repair_preset_tests PRIVATE cxx_std_20)
target_compile_options(veyra_repair_preset_tests PRIVATE /utf-8)
target_compile_definitions(veyra_repair_preset_tests PRIVATE NOMINMAX WIN32_LEAN_AND_MEAN)
diff --git a/docs/WORKLOG.md b/docs/WORKLOG.md
index 1686852b..b7ae221e 100644
--- a/docs/WORKLOG.md
+++ b/docs/WORKLOG.md
@@ -1,5 +1,39 @@
# 2026-09-11 继续修复目标模式执行中
+## 2026-09-17 色彩页 P1:T2b-a 总开关语义 + CPU 烘焙表(调色前移到源头)
+
+按 v4 计划(调色链**全部前移到所有效果器之前**、单一链路、可完全关闭)落地第一批:
+
+- `ColorSettings::enabled`(总开关,默认关);`neutral()` 忽略开关,含义是"渲染结果等于没调色",
+ 所以"开着但全中性"必须是视觉无操作;
+- `EnhancementSettings::sameVideoConfiguration()`:色彩**参数**仍是排除项(改参数不重建图),
+ 但 **总开关参与比较** —— 它是链路形状变化,允许重建(与 NR/超分开关同级);
+- 新增 `include/veyra/pipeline/ColorGradeTables.h` + `src/pipeline/ColorGradeTables.cpp`:把整条调色
+ 在 CPU 端烘焙成三条小表 + 一个 3×3 矩阵,供 ingest shader 读取(**不新增 pass**):
+ - 曲线表 1024×RGBA16F:对数域(10 档,围绕 18% 灰)里的"分区色调 + 对比度 + 4 区参数曲线 +
+ RGB/单通道点曲线",解码回线性;
+ - 色相表 256×RGBA16F:8 段混色器的(色相偏移 / 饱和度 / 明度)响应;
+ - 亮度表 256×RGBA16F:颜色分级四个色轮 + 校准阴影色调的按区增益;
+ - 3×3 线性矩阵:白平衡(Planck 轨迹 + Bradford CAT,方向按"滑块声明光源"惯例)
+ 叠加校准原色近似;
+- `tests/unit/ColorGradeTableTests.cpp`(新目标 `veyra_color_grade_tests`,纯 CPU、不依赖 GPU):
+ 23 项断言全部 PASS —— 关/全中性恒等、对数域往返、白平衡冷暖方向与中性不动、黑白场符号、
+ 曲线单调与落点、单通道只动自己、混色器波段、分级分区、阴影色调只动暗部、LUT 未选时为惰性。
+
+修正记录(烘焙数学,两处方向性错误,测试抓到后修正):
+
+1. 白平衡 CAT 方向写反(`D65→目标` 应为 `目标→D65`),导致正温度变冷;
+2. 阴影/黑场符号写反(正值应为提亮),导致"负 black 变亮"。
+
+命令与结果:构建 exit 0;`veyra_color_grade_tests` → `PASS: colour grade bake tables ...`(exit 0);
+`veyra_repair_preset_tests`(66 组迁移 + v18 往返,schema 追加总开关字段)exit 0;
+`veyra_ui_contract_tests` exit 0;`veyra_repair_contract_tests` 191/0;
+`scripts/gates/delivery.ps1` → `DELIVERY SHORT GATE PASS`
+(`logs/delivery/682bb5d4f300439588ae65eb211ac450/result.json`)。
+
+未完成:T2b-b(把烘焙表接进 ingest shader:源头单一 pass、`GpuStage::Grade` 计时、
+关开关与旧链路逐像素一致的 GPU 对比),然后 T3–T6。
+
## 2026-09-17 色彩页 P1:T2a 色彩数据模型 + 预设 schema v18
- 新增 `include/veyra/engine/ColorSettings.h`:LR 对齐的 P1 色彩模型(白平衡、亮、存在感、参数/点曲线 5 通道、
diff --git a/include/veyra/engine/ColorSettings.h b/include/veyra/engine/ColorSettings.h
index 4a5f8537..8addb89c 100644
--- a/include/veyra/engine/ColorSettings.h
+++ b/include/veyra/engine/ColorSettings.h
@@ -44,6 +44,11 @@ struct ColorGradingWheel {
};
struct ColorSettings {
+ // Master switch (v4 plan): when false the colour stage does not exist at all
+ // - no pass, no tables, no dispatch - so users who want the absolute lowest
+ // latency pay nothing. Toggling it changes the graph shape (like NR/SR),
+ // while changing the parameters below stays a live uniform update.
+ bool enabled=false;
// White balance: relative to the source metadata (capture has no as-shot WB,
// so the UI must label these as relative values - plan section 5).
float temperature=0,tint=0;
@@ -84,7 +89,14 @@ struct ColorSettings {
int lutInputSpace=0;
bool operator==(const ColorSettings&)const=default;
- bool neutral()const{return *this==ColorSettings{};}
+ // Neutral means "renders exactly like no grading at all", ignoring the
+ // master switch: enabled+neutral must be a visual no-op (verified by test),
+ // which lets the stage early-out instead of running an identity transform.
+ bool neutral()const{
+ auto copy=*this;
+ copy.enabled=false;
+ return copy==ColorSettings{};
+ }
// P1 knows only these two LUT spaces plus "no LUT"; anything else is a bug.
static constexpr int kLutInputCineon=0,kLutInputSrgb=1,kLutInputPq=2;
@@ -140,7 +152,7 @@ struct ColorSettings {
// narrow UTF-8 (PresetStore owns the wide/UTF-8 conversion of lutPath) so this
// header stays free of platform includes.
inline void writeColorSettings(std::ostream& out,const ColorSettings& c,const std::string& lutPathUtf8){
- out<<c.temperature<<' '<<c.tint<<' '<<c.exposure<<' '<<c.contrast<<' '<<c.highlights<<' '<<c.shadows<<' '<<c.whites<<' '<<c.blacks<<' '
+ out<<(c.enabled?1:0)<<' '<<c.temperature<<' '<<c.tint<<' '<<c.exposure<<' '<<c.contrast<<' '<<c.highlights<<' '<<c.shadows<<' '<<c.whites<<' '<<c.blacks<<' '
<<c.texture<<' '<<c.clarity<<' '<<c.dehaze<<' '<<c.vibrance<<' '<<c.saturation<<' '
<<c.paramHighlights<<' '<<c.paramLights<<' '<<c.paramDarks<<' '<<c.paramShadows<<' '
<<c.splitHighlights<<' '<<c.splitMidtones<<' '<<c.splitShadows<<' '
@@ -159,13 +171,14 @@ inline void writeColorSettings(std::ostream& out,const ColorSettings& c,const st
for(float v:c.calibrationSaturation)out<<' '<<v;
}
inline bool readColorSettings(std::istream& in,ColorSettings& c,std::string& lutPathUtf8){
- int blackWhite=0;
- if(!(in>>c.temperature>>c.tint>>c.exposure>>c.contrast>>c.highlights>>c.shadows>>c.whites>>c.blacks
+ int blackWhite=0,enabled=0;
+ if(!(in>>enabled>>c.temperature>>c.tint>>c.exposure>>c.contrast>>c.highlights>>c.shadows>>c.whites>>c.blacks
>>c.texture>>c.clarity>>c.dehaze>>c.vibrance>>c.saturation
>>c.paramHighlights>>c.paramLights>>c.paramDarks>>c.paramShadows
>>c.splitHighlights>>c.splitMidtones>>c.splitShadows
>>blackWhite>>c.gradingBlending>>c.gradingBalance>>c.calibrationShadowTint
- >>c.lutStrength>>c.lutInputSpace>>std::quoted(lutPathUtf8))||(blackWhite!=0&&blackWhite!=1))return false;
+ >>c.lutStrength>>c.lutInputSpace>>std::quoted(lutPathUtf8))||(blackWhite!=0&&blackWhite!=1)||(enabled!=0&&enabled!=1))return false;
+ c.enabled=enabled!=0;
c.blackWhite=blackWhite!=0;
for(auto& curve:c.curves){
if(!(in>>curve.count))return false;
diff --git a/include/veyra/engine/EnhancementSettings.h b/include/veyra/engine/EnhancementSettings.h
index cdf6431d..d85c3ed5 100644
--- a/include/veyra/engine/EnhancementSettings.h
+++ b/include/veyra/engine/EnhancementSettings.h
@@ -150,8 +150,10 @@ struct EnhancementSettings {
// Export-only fields: changing the bitrate must never invalidate the
// running preview graph (the controller would otherwise rebuild it).
video.exportBitrateMbps=other.exportBitrateMbps;
- // Colour grade: uniform-only update, never a graph rebuild.
+ // Colour grade: parameters are uniform-only (never a rebuild), but the
+ // master switch changes the graph shape, so it stays in the comparison.
video.color=other.color;
+ video.color.enabled=color.enabled;
return video==other;
}
void rejectVideoRequest(const EnhancementSettings& attempted,const EnhancementSettings& previous) {
diff --git a/include/veyra/pipeline/ColorGradeTables.h b/include/veyra/pipeline/ColorGradeTables.h
new file mode 100644
index 00000000..bdad2ab5
--- /dev/null
+++ b/include/veyra/pipeline/ColorGradeTables.h
@@ -0,0 +1,36 @@
+#pragma once
+#include <array>
+#include <cmath>
+#include "veyra/engine/ColorSettings.h"
+namespace veyra::pipeline {
+// CPU bake of the colour grade into the small tables the ingest shader reads.
+// Everything expensive - chromatic adaptation, tone response, curve evaluation,
+// per-band mixer weights, grading zones - happens here once per settings change,
+// so per-pixel work stays a handful of lookups. Deliberately free of D3D12 so
+// the maths can be unit-tested without a GPU.
+//
+// Curve domain: the working texture is scene-linear, so tonal operations are
+// evaluated in a Cineon-style log domain (10 stops around 18% grey) and decoded
+// back to linear. That matches the plan's "curves live in a log/display-referred
+// domain" rule instead of bending linear values directly.
+struct ColorGradeTables {
+ static constexpr int kCurveEntries=1024; // log-domain input -> linear rgb response
+ static constexpr int kHueEntries=256; // hue -> (shift, saturation scale, luminance scale)
+ static constexpr int kLumEntries=256; // tone zone -> (r,g,b) gain for colour grading
+ std::array<float,kCurveEntries*4> curve{};
+ std::array<float,kHueEntries*4> hue{};
+ std::array<float,kLumEntries*4> lum{};
+ // Linear-domain 3x3 (row-major): white balance + calibration, applied once
+ // before the tone curve.
+ std::array<float,9> matrix{1,0,0, 0,1,0, 0,0,1};
+ float exposure=0; // EV, applied as a linear multiply
+ float saturation=0,vibrance=0; // -100..100, applied in the shader
+ float lutStrength=0; // 0..1 (0 disables the 3D LUT lookup)
+ int lutInputSpace=0;
+ bool identity=true; // master switch off or every parameter neutral
+ static ColorGradeTables bake(const engine::ColorSettings& settings);
+ // Exposed for the tests and for callers that need the same log mapping.
+ static float encodeLog(float linear);
+ static float decodeLog(float encoded);
+};
+} // namespace veyra::pipeline
diff --git a/src/pipeline/ColorGradeTables.cpp b/src/pipeline/ColorGradeTables.cpp
new file mode 100644
index 00000000..2827a17f
--- /dev/null
+++ b/src/pipeline/ColorGradeTables.cpp
@@ -0,0 +1,203 @@
+#include "veyra/pipeline/ColorGradeTables.h"
+#include <algorithm>
+namespace veyra::pipeline {
+namespace {
+constexpr float kMidGrey=0.18f;
+constexpr float kLogStops=10.0f; // -5..+5 EV around 18% grey
+constexpr double kD65x=0.3127,kD65y=0.3290;
+using Mat3=std::array<double,9>;
+
+// Planckian locus, Kim et al. cubic approximation (valid 1667..25000 K):
+// x is a cubic in 1/T, y is a cubic in x.
+void planckianXy(double kelvin,double& x,double& y){
+ const double t=std::clamp(kelvin,1667.0,25000.0);
+ if(t<=4000.0){
+ x=-0.2661239e9/(t*t*t)-0.2343589e6/(t*t)+0.8776956e3/t+0.179910;
+ y=-0.9549476*x*x*x-1.37418593*x*x+2.09137015*x-0.0316772744;
+ }else{
+ x=-3.0258469e9/(t*t*t)+2.1070379e6/(t*t)+0.2226347e3/t+0.240390;
+ y=3.0817580*x*x*x-5.87338670*x*x+3.75112997*x-0.37001483;
+ }
+}
+Mat3 mul(const Mat3& a,const Mat3& b){
+ Mat3 out{};
+ for(int r=0;r<3;++r)for(int c=0;c<3;++c)for(int k=0;k<3;++k)out[std::size_t(r*3+c)]+=a[std::size_t(r*3+k)]*b[std::size_t(k*3+c)];
+ return out;
+}
+// Bradford chromatic adaptation: source white -> target white, in XYZ.
+Mat3 bradford(const std::array<double,3>& src,const std::array<double,3>& dst){
+ constexpr Mat3 m{0.8951,0.2664,-0.1614, -0.7502,1.7135,0.0367, 0.0389,-0.0685,1.0296};
+ constexpr Mat3 inv{0.9869929,-0.1470543,0.1599627, 0.4323053,0.5183603,0.0492912, -0.0085287,0.0400428,0.9684867};
+ const auto response=[&](const std::array<double,3>& v){
+ return std::array<double,3>{
+ m[0]*v[0]+m[1]*v[1]+m[2]*v[2],
+ m[3]*v[0]+m[4]*v[1]+m[5]*v[2],
+ m[6]*v[0]+m[7]*v[1]+m[8]*v[2]};
+ };
+ const auto s=response(src),d=response(dst);
+ const Mat3 scale{d[0]/s[0],0,0, 0,d[1]/s[1],0, 0,0,d[2]/s[2]};
+ return mul(inv,mul(scale,m));
+}
+std::array<double,3> whiteXyz(double x,double y){return {x/y,1.0,(1.0-x-y)/y};}
+constexpr Mat3 kRgbToXyz{0.4123908,0.3575843,0.1804808, 0.2126390,0.7151687,0.0721923, 0.0193308,0.1191948,0.9505322};
+constexpr Mat3 kXyzToRgb{3.2409699,-1.5373832,-0.4986108, -0.9692436,1.8759675,0.0415551, 0.0556301,-0.2039770,1.0569715};
+
+float smoothstepf(float edge0,float edge1,float x){
+ const float t=std::clamp((x-edge0)/std::max(1e-6f,edge1-edge0),0.0f,1.0f);
+ return t*t*(3.0f-2.0f*t);
+}
+// Piecewise-linear point curve on the 0..1 tonal domain (P1 cut; the data model
+// already stores up to eight points, so a monotone cubic can replace this later
+// without touching the format).
+float pointCurve(const engine::ColorCurve& curve,float x){
+ if(curve.count<2)return x;
+ for(int i=0;i<curve.count-1;++i){
+ const auto& a=curve.points[std::size_t(i)];
+ const auto& b=curve.points[std::size_t(i+1)];
+ if(x<=b.x||i==curve.count-2){
+ const float t=std::clamp((x-a.x)/std::max(1e-6f,b.x-a.x),0.0f,1.0f);
+ return a.y+(b.y-a.y)*t;
+ }
+ }
+ return x;
+}
+// Parametric regions: the three splitters divide the tonal range and each region
+// slider shifts its own band (Lightroom-style).
+float parametricShift(const engine::ColorSettings& s,float t){
+ const float sh=std::clamp(0.5f+0.35f*s.splitShadows/100.0f,0.05f,0.90f);
+ const float sm=std::clamp(0.5f+0.35f*s.splitMidtones/100.0f,0.10f,0.95f);
+ const float shi=std::clamp(0.5f+0.35f*s.splitHighlights/100.0f,0.10f,0.95f);
+ const float lo=std::min(sh,sm),hi=std::max(sm,shi);
+ const float wShadows=1.0f-smoothstepf(0.0f,sh,t);
+ const float wDarks=smoothstepf(0.0f,sh,t)*(1.0f-smoothstepf(lo,hi,t));
+ const float wLights=smoothstepf(lo,hi,t)*(1.0f-smoothstepf(sm,shi,t));
+ const float wHighlights=smoothstepf(sm,shi,t);
+ return (s.paramShadows*wShadows+s.paramDarks*wDarks+s.paramLights*wLights+s.paramHighlights*wHighlights)/100.0f*0.25f;
+}
+}
+
+float ColorGradeTables::encodeLog(float linear){
+ const float safe=std::max(linear,1e-5f);
+ return std::clamp(std::log2(safe/kMidGrey)/kLogStops+0.5f,0.0f,1.0f);
+}
+float ColorGradeTables::decodeLog(float encoded){
+ return kMidGrey*std::exp2((std::clamp(encoded,0.0f,1.0f)-0.5f)*kLogStops);
+}
+
+ColorGradeTables ColorGradeTables::bake(const engine::ColorSettings& s){
+ ColorGradeTables out;
+ out.exposure=s.exposure;
+ out.saturation=s.saturation;
+ out.vibrance=s.vibrance;
+ out.lutStrength=s.hasLut()?std::clamp(s.lutStrength/100.0f,0.0f,1.0f):0.0f;
+ out.lutInputSpace=s.lutInputSpace;
+ if(!s.enabled||s.neutral()){
+ for(int i=0;i<kCurveEntries;++i){
+ const float t=float(i)/float(kCurveEntries-1);
+ const auto base=std::size_t(i)*4;
+ out.curve[base]=out.curve[base+1]=out.curve[base+2]=t;
+ }
+ for(int i=0;i<kHueEntries;++i)out.hue[std::size_t(i)*4+1]=1.0f;
+ for(int i=0;i<kLumEntries;++i){const auto base=std::size_t(i)*4;out.lum[base]=out.lum[base+1]=out.lum[base+2]=1.0f;}
+ out.identity=true;
+ return out;
+ }
+ out.identity=false;
+
+ // --- white balance + calibration -> one linear 3x3 ---------------------
+ double wx=kD65x,wy=kD65y;
+ if(s.temperature!=0||s.tint!=0){
+ // Temperature is relative: +100 assumes a much cooler illuminant, which
+ // makes the image warmer (Lightroom's slider direction).
+ planckianXy(6500.0*std::exp2(double(s.temperature)/100.0),wx,wy);
+ wy=std::clamp(wy+double(s.tint)*0.00025,0.05,0.95);
+ }
+ // Adaption direction: the slider states which illuminant the image was shot
+ // under, so the matrix maps that illuminant onto D65 (positive temperature =
+ // assume a cooler source = the picture looks warmer, Lightroom's direction).
+ Mat3 combined=mul(kXyzToRgb,mul(bradford(whiteXyz(wx,wy),whiteXyz(kD65x,kD65y)),kRgbToXyz));
+ Mat3 calib{1,0,0, 0,1,0, 0,0,1};
+ for(int i=0;i<3;++i){
+ const double gain=1.0+double(s.calibrationSaturation[std::size_t(i)])/100.0;
+ const double leak=double(s.calibrationHue[std::size_t(i)])/100.0*0.25;
+ calib[std::size_t(i*3+i)]*=gain;
+ calib[std::size_t(i*3+(i+1)%3)]+=leak*gain;
+ }
+ combined=mul(calib,combined);
+ for(int i=0;i<9;++i)out.matrix[std::size_t(i)]=float(combined[std::size_t(i)]);
+
+ // --- tone response (log domain) ---------------------------------------
+ for(int i=0;i<kCurveEntries;++i){
+ const float in=float(i)/float(kCurveEntries-1);
+ float t=in;
+ t+=s.highlights/100.0f*0.25f*smoothstepf(0.5f,1.0f,t);
+ t+=s.shadows/100.0f*0.25f*(1.0f-smoothstepf(0.0f,0.5f,t));
+ t+=s.whites/100.0f*0.20f*std::pow(std::clamp(t,0.0f,1.0f),3.0f);
+ t+=s.blacks/100.0f*0.20f*std::pow(1.0f-std::clamp(t,0.0f,1.0f),3.0f);
+ t=0.5f+(t-0.5f)*(1.0f+s.contrast/100.0f*0.75f);
+ t+=parametricShift(s,t);
+ t=std::clamp(t,0.0f,1.0f);
+ const auto base=std::size_t(i)*4;
+ // Per-channel point curves, then the RGB master curve on top.
+ out.curve[base]=pointCurve(s.curves[0],pointCurve(s.curves[1],t));
+ out.curve[base+1]=pointCurve(s.curves[0],pointCurve(s.curves[2],t));
+ out.curve[base+2]=pointCurve(s.curves[0],pointCurve(s.curves[3],t));
+ }
+
+ // --- hue response (mixer) --------------------------------------------
+ for(int i=0;i<kHueEntries;++i){
+ const float hue=360.0f*float(i)/float(kHueEntries-1);
+ float shift=0,sat=1,lum=1;
+ constexpr float centres[engine::kColorMixerBands]={0,30,60,120,180,240,280,320};
+ for(int b=0;b<engine::kColorMixerBands;++b){
+ float d=std::abs(hue-centres[b]);
+ d=std::min(d,360.0f-d);
+ const float w=std::max(0.0f,1.0f-d/45.0f);
+ shift+=w*s.mixerHue[std::size_t(b)]*0.3f;
+ sat*=1.0f+w*s.mixerSaturation[std::size_t(b)]/100.0f;
+ lum*=1.0f+w*s.mixerLuminance[std::size_t(b)]/100.0f;
+ }
+ const auto base=std::size_t(i)*4;
+ out.hue[base]=shift;out.hue[base+1]=std::max(0.0f,sat);out.hue[base+2]=std::max(0.0f,lum);
+ }
+
+ // --- luminance response (colour grading + shadow tint) ----------------
+ const float tint=std::clamp(s.calibrationShadowTint/100.0f,-1.0f,1.0f);
+ for(int i=0;i<kLumEntries;++i){
+ const float t=float(i)/float(kLumEntries-1);
+ float gains[3]={1,1,1};
+ const float wS=1.0f-smoothstepf(0.15f,0.5f,t);
+ const float wH=smoothstepf(0.5f,0.85f,t);
+ const float wM=std::max(0.0f,1.0f-wS-wH);
+ const float zones[3]={wS,wM,wH};
+ for(int z=0;z<3;++z){
+ const auto& wheel=s.grading[std::size_t(z)];
+ const float sat=wheel.saturation/100.0f*zones[z];
+ const float lum=1.0f+wheel.luminance/100.0f*zones[z];
+ const float rad=wheel.hue*3.14159265f/180.0f;
+ gains[0]*=std::max(0.0f,1.0f+sat*std::cos(rad))*lum;
+ gains[1]*=std::max(0.0f,1.0f+sat*std::cos(rad-2.0943951f))*lum;
+ gains[2]*=std::max(0.0f,1.0f+sat*std::cos(rad+2.0943951f))*lum;
+ }
+ const auto& global=s.grading[3];
+ const float gSat=global.saturation/100.0f;
+ const float gLum=1.0f+global.luminance/100.0f;
+ const float gRad=global.hue*3.14159265f/180.0f;
+ gains[0]*=std::max(0.0f,1.0f+gSat*std::cos(gRad))*gLum;
+ gains[1]*=std::max(0.0f,1.0f+gSat*std::cos(gRad-2.0943951f))*gLum;
+ gains[2]*=std::max(0.0f,1.0f+gSat*std::cos(gRad+2.0943951f))*gLum;
+ // Shadow tint is a dark-end green/magenta balance, so it lives here.
+ if(tint!=0.0f){
+ const float w=wS*0.25f*tint;
+ gains[0]*=1.0f-w*0.5f;
+ gains[1]*=1.0f+w;
+ gains[2]*=1.0f-w*0.5f;
+ }
+ const auto base=std::size_t(i)*4;
+ out.lum[base]=std::max(0.0f,gains[0]);
+ out.lum[base+1]=std::max(0.0f,gains[1]);
+ out.lum[base+2]=std::max(0.0f,gains[2]);
+ }
+ return out;
+}
+} // namespace veyra::pipeline
diff --git a/tests/unit/ColorGradeTableTests.cpp b/tests/unit/ColorGradeTableTests.cpp
new file mode 100644
index 00000000..45195793
--- /dev/null
+++ b/tests/unit/ColorGradeTableTests.cpp
@@ -0,0 +1,136 @@
+// CPU bake of the colour grade (T2b). No GPU: verifies the tables the ingest
+// shader will read, plus the "enabled but neutral must be a visual no-op"
+// contract the master switch relies on.
+#include "veyra/pipeline/ColorGradeTables.h"
+#include "veyra/engine/ColorSettings.h"
+#include <cmath>
+#include <cstdio>
+#include <string>
+namespace {
+int failures=0;
+void check(bool ok,const std::string& label){
+ std::printf("%s %s\n",ok?"PASS":"FAIL",label.c_str());
+ if(!ok)++failures;
+}
+constexpr int kN=veyra::pipeline::ColorGradeTables::kCurveEntries;
+constexpr int kH=veyra::pipeline::ColorGradeTables::kHueEntries;
+constexpr int kL=veyra::pipeline::ColorGradeTables::kLumEntries;
+float curveAt(const veyra::pipeline::ColorGradeTables& t,int index,int channel){
+ return t.curve[std::size_t(index)*4+std::size_t(channel)];
+}
+float hueAt(const veyra::pipeline::ColorGradeTables& t,int index,int channel){
+ return t.hue[std::size_t(index)*4+std::size_t(channel)];
+}
+float lumAt(const veyra::pipeline::ColorGradeTables& t,int index,int channel){
+ return t.lum[std::size_t(index)*4+std::size_t(channel)];
+}
+std::array<float,3> applyMatrix(const veyra::pipeline::ColorGradeTables& t,std::array<float,3> v){
+ std::array<float,3> out{};
+ for(int r=0;r<3;++r)out[std::size_t(r)]=t.matrix[std::size_t(r*3)]*v[0]+t.matrix[std::size_t(r*3+1)]*v[1]+t.matrix[std::size_t(r*3+2)]*v[2];
+ return out;
+}
+}
+int main(){
+ using veyra::engine::ColorSettings;
+ using veyra::pipeline::ColorGradeTables;
+
+ // 1. Off, and on-but-neutral, are both visual no-ops.
+ {
+ const auto off=ColorGradeTables::bake(ColorSettings{});
+ ColorSettings onNeutral;onNeutral.enabled=true;
+ const auto on=ColorGradeTables::bake(onNeutral);
+ bool ramp=true,mat=true,hueLum=true;
+ for(int i=0;i<kN;++i)for(int c=0;c<3;++c)if(std::abs(curveAt(on,i,c)-float(i)/float(kN-1))>1e-6f)ramp=false;
+ for(int i=0;i<9;++i)if(std::abs(on.matrix[std::size_t(i)]-(i%4==0?1.0f:0.0f))>1e-6f)mat=false;
+ for(int i=0;i<kH;++i)if(std::abs(on.hue[std::size_t(i)*4+1]-1.0f)>1e-6f)hueLum=false;
+ for(int i=0;i<kL;++i)for(int c=0;c<3;++c)if(std::abs(on.lum[std::size_t(i)*4+std::size_t(c)]-1.0f)>1e-6f)hueLum=false;
+ check(off.identity&&on.identity,"master off and enabled-neutral both bake as identity");
+ check(ramp&&mat&&hueLum,"identity tables are a linear ramp, unit matrix, unit gains");
+ }
+ // 2. Any real parameter clears the identity flag.
+ {
+ ColorSettings s;s.enabled=true;s.exposure=0.5f;
+ check(!ColorGradeTables::bake(s).identity,"a real parameter clears the identity flag");
+ }
+ // 3. Log domain round trip (the curve table's indexing domain).
+ {
+ bool ok=true;
+ // The table window is -5..+5 stops around 18% grey; values outside it are
+ // passed through untouched by the shader (checked in the GPU test).
+ for(float v:{0.01f,0.02f,0.18f,0.5f,1.0f,4.0f})ok=ok&&std::abs(ColorGradeTables::decodeLog(ColorGradeTables::encodeLog(v))-v)<v*1e-3f;
+ check(ok,"log encode/decode round trips inside the 10-stop window");
+ }
+ // 4. White balance direction and neutral identity.
+ {
+ ColorSettings neutral;neutral.enabled=true;
+ const auto mid=applyMatrix(ColorGradeTables::bake(neutral),{0.18f,0.18f,0.18f});
+ check(std::abs(mid[0]-0.18f)<1e-4f&&std::abs(mid[2]-0.18f)<1e-4f,"neutral white balance leaves mid grey untouched");
+ ColorSettings warm;warm.enabled=true;warm.temperature=80;
+ ColorSettings cool;cool.enabled=true;cool.temperature=-80;
+ const auto w=applyMatrix(ColorGradeTables::bake(warm),{0.18f,0.18f,0.18f});
+ const auto c=applyMatrix(ColorGradeTables::bake(cool),{0.18f,0.18f,0.18f});
+ check(w[0]>w[2]*1.01f,"positive temperature warms the image (red above blue)");
+ check(c[0]<c[2]*0.99f,"negative temperature cools the image (blue above red)");
+ ColorSettings green;green.enabled=true;green.tint=80;
+ const auto g=applyMatrix(ColorGradeTables::bake(green),{0.18f,0.18f,0.18f});
+ check(g[1]<g[0]&&g[1]<g[2],"positive tint shifts mid grey towards magenta");
+ }
+ // 5. Tone controls move the right end of the curve and stay monotone.
+ {
+ const auto neutral=ColorGradeTables::bake(ColorSettings{});
+ ColorSettings blacks;blacks.enabled=true;blacks.blacks=-50;
+ ColorSettings whites;whites.enabled=true;whites.whites=50;
+ const auto b=ColorGradeTables::bake(blacks);
+ const auto w=ColorGradeTables::bake(whites);
+ check(curveAt(b,8,0)<curveAt(neutral,8,0)-1e-3f,"negative blacks darkens the shadow end");
+ check(curveAt(w,kN-9,0)>curveAt(neutral,kN-9,0)+1e-3f,"positive whites brightens the highlight end");
+ bool monotone=true;
+ for(int i=1;i<kN;++i)if(curveAt(b,i,0)<curveAt(b,i-1,0)-1e-6f)monotone=false;
+ check(monotone,"baked curve stays monotone");
+ }
+ // 6. A point curve lands where the data model says it does.
+ {
+ ColorSettings s;s.enabled=true;
+ s.curves[0].count=3;s.curves[0].points[1]={0.25f,0.35f};s.curves[0].points[2]={1,1};
+ const auto t=ColorGradeTables::bake(s);
+ const int index=int(0.25f*float(kN-1)+0.5f);
+ check(std::abs(curveAt(t,index,0)-0.35f)<2e-3f,"RGB point curve is honoured at its control point");
+ check(std::abs(curveAt(t,index,1)-0.35f)<2e-3f,"RGB master curve applies to every channel");
+ }
+ // 7. Per-channel curve only moves its own channel.
+ {
+ ColorSettings s;s.enabled=true;
+ s.curves[2].count=3;s.curves[2].points[1]={0.5f,0.6f};s.curves[2].points[2]={1,1};
+ const auto t=ColorGradeTables::bake(s);
+ check(curveAt(t,kN/2,1)>curveAt(t,kN/2,0)+1e-3f,"green point curve only lifts green");
+ }
+ // 8. Mixer bands, grading zones and the shadow tint hit their own regions.
+ {
+ ColorSettings mixer;mixer.enabled=true;mixer.mixerSaturation[3]=100; // green band
+ const auto m=ColorGradeTables::bake(mixer);
+ const int greenIndex=int(120.0f/360.0f*float(kH-1)+0.5f);
+ check(hueAt(m,greenIndex,1)>1.5f,"green mixer band raises saturation at the green hue");
+ check(std::abs(hueAt(m,kH/2,1)-1.0f)<0.35f,"a hue far from the band stays close to neutral");
+ ColorSettings grade;grade.enabled=true;grade.grading[1].hue=0;grade.grading[1].saturation=100;
+ const auto g=ColorGradeTables::bake(grade);
+ check(lumAt(g,kL/2,0)>lumAt(g,kL/2,1)+0.05f&&lumAt(g,kL/2,0)>lumAt(g,kL/2,2)+0.05f,"mid-tone grading wheel pushes mid tones towards red");
+ check(std::abs(lumAt(g,kL-1,0)-1.0f)<0.05f&&std::abs(lumAt(g,kL-1,2)-1.0f)<0.05f,"highlight end is untouched by the mid-tone wheel");
+ ColorSettings tint;tint.enabled=true;tint.calibrationShadowTint=100;
+ const auto st=ColorGradeTables::bake(tint);
+ check(lumAt(st,0,1)>lumAt(st,0,0)&&lumAt(st,0,1)>lumAt(st,0,2),"shadow tint lifts green in the dark end");
+ check(std::abs(lumAt(st,kL-1,1)-1.0f)<0.02f,"shadow tint leaves the bright end alone");
+ }
+ // 9. Constants pass through, and a LUT without a name is inert.
+ {
+ ColorSettings s;s.enabled=true;s.saturation=-100;s.vibrance=42;s.lutStrength=30;
+ const auto t=ColorGradeTables::bake(s);
+ check(t.saturation==-100&&t.vibrance==42,"saturation and vibrance pass through as constants");
+ check(t.lutStrength==0.0f,"lut strength stays inert while no lut is selected");
+ s.setLutName(L"film.cube");s.lutStrength=75;
+ const auto withLut=ColorGradeTables::bake(s);
+ check(std::abs(withLut.lutStrength-0.75f)<1e-6f,"lut strength normalises to 0..1 when a lut is selected");
+ }
+ if(failures){std::printf("FAIL: colour grade bake (%d checks)\n",failures);return 1;}
+ std::printf("PASS: colour grade bake tables (identity, white balance, tone, curves, mixer, grading, lut)\n");
+ return 0;
+}