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): TPDF output dither on the 8/10-bit preview and export writes (verified 16px -> 5px banding)

Veyra Goal Agentea96bbf6582f
diff --git a/docs/WORKLOG.md b/docs/WORKLOG.md
index 12dba579..d24068b7 100644
--- a/docs/WORKLOG.md
+++ b/docs/WORKLOG.md
@@ -1,5 +1,29 @@
# 2026-09-11 继续修复目标模式执行中
+## 2026-09-17 色彩页 P1:输出抖动(平面断层 → 量化噪声)
+
+方案 §4.1 第 4 条要求“进 8 位 SDR 呈现/导出前加三角抖动”。实现:
+
+- `HdrColor.hlsli` 新增 `OutputDither(pixel, step)`:两次独立哈希得到 TPDF(三角分布)噪声,
+ 按键于**目标像素**,不做可见的有序图案;`step` 为目标量化步长(8 位 1/255、10 位 1/1023),0 = 关闭;
+- 预览/会签路径(`ScaleBlit.hlsl`):在**编码之后**抖动,SDR 写 R8G8B8A8 时按 1/255,
+ HDR10 写 R10G10B10A2(PQ)时按 1/1023;FP16 scRGB 目标不抖动(没有量化);
+- 导出路径(`RgbToNv12.hlsl`):`reserved.y` 传抖动步长,亮度与色度平面各自抖动
+ (NVENC 与 Media Foundation 两个编码器都接上,保证预览/导出/截图一致);
+- **只在真的调色时抖动**:`EnhanceGraph::outputDitherStep()` 在总开关关闭**或表是中性**时返回 0,
+ 所以“关闭 / 开启但中性 = 与旧链路逐字节一致”的合同不被破坏(GPU 合同测试仍在守这条);
+ 诊断/测试可用 `EnhanceGraphDesc::outputDitherStep` 强制指定(-1 = 自动)。
+
+**验收(真机 GPU,`veyra_color_grade_gpu_tests`)**:把 112→128 的灰阶斜坡压过 `contrast=-100` 的调色,
+理想输出每像素只前进约 0.25 个码值;同一张图渲染两次:
+
+- 不抖动:最长连续相同码值 **16 px**(就是肉眼看到的色带);
+- 抖动 1 LSB:最长连续相同码值 **5 px**,全部检查 exit 0。
+
+三入口一致性复测(`scripts/acceptance/color-three-entry-consistency.ps1`):
+截图 vs 导出帧 49.29 → **51.90 dB**(两边抖动用同一套像素键,量化误差一致),
+关调色基线 50.30 dB 不变,调色前后差异仍然可见(19.61 / 18.29 dB)。
+
## 2026-09-17 色彩页 P1:黑白混色器(T4 补完)+ 修“改了 shader 却不重编译”的构建漏洞
**黑白混色器现在真的能用**。之前色彩页里那 8 条“黑白”滑块是**死控件**:模型、预设、UI 都在,
diff --git a/include/veyra/pipeline/EnhanceGraph.h b/include/veyra/pipeline/EnhanceGraph.h
index b0821209..9f00734a 100644
--- a/include/veyra/pipeline/EnhanceGraph.h
+++ b/include/veyra/pipeline/EnhanceGraph.h
@@ -105,6 +105,11 @@ struct EnhanceGraphDesc {
std::wstring runtimeAbsPath; // absolute runtime_local/nvidia path
// Optional stage instrumentation hook (GPU timing experiments).
std::function<void(const char*)> stageMark;
+ // Output dither step for the 8/10-bit write paths, in coded units. -1 keeps
+ // the product default (one LSB while the colour grade is active, nothing
+ // otherwise) so ungraded output stays byte-identical; tests and diagnostics
+ // can force a value (0 disables it).
+ float outputDitherStep=-1.0f;
// Isolated contract probes only. Unset by every product entry point.
// Caller retains any supplied resources until graph drain/shutdown.
std::function<void(NVSDK_NGX_Parameter*,ID3D12Resource*,ID3D12Resource*,uint32_t,uint32_t)> nrParameterProbe;
@@ -243,6 +248,11 @@ public:
// does not match the content domain). The engine surfaces this in the
// status panel so the refusal is visible, not only logged.
const std::wstring& colorLutNotice() const { return colorLutNotice_; }
+ // True when the colour stage exists in this graph (master switch on and not
+ // neutral). The encoder uses it to dither its 8/10-bit conversion output.
+ bool colorGradeActive() const { return colorActive_; }
+ // Effective dither step for the 8/10-bit output paths (0 = no dither).
+ float outputDitherStep() const;
// Isolated diagnostics only: existing constant guidance, borrowed lifetime.
// A caller writing before the first real frame must restore COMMON state.
ID3D12Resource* diagnosticDepthResource(bool frameGeneration) const { return frameGeneration?depthTex_.Get():nrZeroDepth_.Get(); }
diff --git a/shaders/HdrColor.hlsli b/shaders/HdrColor.hlsli
index 455bb63f..a0b1eca7 100644
--- a/shaders/HdrColor.hlsli
+++ b/shaders/HdrColor.hlsli
@@ -29,3 +29,20 @@ float3 HdrRestore(float3 base,float3 proxy,float3 enhanced) {
float gate=smoothstep(0,.02,y)*(1-smoothstep(.75,.995,peak));
return base+clamp(delta,-.5,.5)*(203.0/80.0)*gate;
}
+
+// Triangular (TPDF) output dither keyed by the destination pixel. Two
+// independent uniform hashes give a triangular distribution instead of a
+// visible ordered pattern, so the quantisation error of an 8/10-bit write
+// becomes uncorrelated noise instead of banding. `step` is the target's
+// quantisation step (1/255, 1/1023, ...); 0 disables the dither entirely.
+float OutputDither(uint2 pixel, float step)
+{
+ if (step <= 0.0) return 0.0;
+ uint h1 = pixel.x * 1973u + pixel.y * 9277u + 26699u;
+ h1 ^= h1 >> 16u; h1 *= 0x7feb352du; h1 ^= h1 >> 15u; h1 *= 0x846ca68bu; h1 ^= h1 >> 16u;
+ uint h2 = pixel.x * 1327u + pixel.y * 4591u + 101u;
+ h2 ^= h2 >> 15u; h2 *= 0x2c1b3c6du; h2 ^= h2 >> 12u; h2 *= 0x297a2d39u; h2 ^= h2 >> 15u;
+ const float r1 = float(h1 & 0xFFFFFFu) / 16777215.0;
+ const float r2 = float(h2 & 0xFFFFFFu) / 16777215.0;
+ return (r1 - r2) * step;
+}
diff --git a/shaders/RgbToNv12.hlsl b/shaders/RgbToNv12.hlsl
index 26308622..8ed8b4f8 100644
--- a/shaders/RgbToNv12.hlsl
+++ b/shaders/RgbToNv12.hlsl
@@ -3,6 +3,9 @@ Texture2D<float4> rgb : register(t0);
RWTexture2D<float> yPlane : register(u0);
RWTexture2D<float2> uvPlane : register(u1);
cbuffer Constants : register(b0) { uint width; uint height; uint2 reserved; float4 padding; };
+// reserved.x = HDR/PQ target, reserved.y = output dither step in coded units
+// (asfloat), so the colour grade's quantisation error becomes noise instead of
+// banding. 0 keeps the exact legacy behaviour for every ungraded path.
float3 convert(float3 c) {
if(reserved.x){
float y=dot(c,float3(.2627,.6780,.0593));
@@ -15,9 +18,10 @@ float3 inputColor(uint2 p){float3 c=rgb[p].rgb;return reserved.x==1?HdrEncodePq(
[numthreads(16,16,1)]
void main(uint3 id:SV_DispatchThreadID){
if(id.x>=width||id.y>=height)return;
- yPlane[id.xy]=convert(inputColor(id.xy)).x;
+ const float ditherStep=asfloat(reserved.y);
+ yPlane[id.xy]=convert(inputColor(id.xy)).x+OutputDither(id.xy,ditherStep);
if((id.x&1)==0&&(id.y&1)==0){
float3 c=(inputColor(id.xy)+inputColor(id.xy+uint2(1,0))+inputColor(id.xy+uint2(0,1))+inputColor(id.xy+uint2(1,1)))*0.25;
- uvPlane[id.xy/2]=convert(saturate(c)).yz;
+ uvPlane[id.xy/2]=convert(saturate(c)).yz+OutputDither(id.xy/2,ditherStep);
}
}
diff --git a/shaders/ScaleBlit.hlsl b/shaders/ScaleBlit.hlsl
index da944330..448c84cc 100644
--- a/shaders/ScaleBlit.hlsl
+++ b/shaders/ScaleBlit.hlsl
@@ -13,7 +13,7 @@ cbuffer ScaleBlitConstants : register(b0)
uint outputWidth;
uint outputHeight;
float encodeSrgb; // 1 only at linear working -> SDR output boundary
- float _pad1;
+ float ditherStep; // 1 LSB of the target (0 = no dither)
float _pad2;
float _pad3;
};
@@ -53,10 +53,12 @@ void main(uint3 groupId : SV_GroupID, uint3 localId : SV_GroupThreadID, uint3 gl
value = lerp(top, bottom, fy);
}
if(encodeSrgb>2.5){value.rgb=HdrProxy(value.rgb);}
- if(encodeSrgb>1.5&&encodeSrgb<2.5){outputTex[globalId.xy]=float4(HdrEncodePq(value.rgb),value.a);return;}
+ // The dither runs in the *coded* domain, so it must be applied after the
+ // transfer encode of whichever 8/10-bit target the graph is writing.
+ if(encodeSrgb>1.5&&encodeSrgb<2.5){outputTex[globalId.xy]=float4(HdrEncodePq(value.rgb)+OutputDither(globalId.xy,ditherStep),value.a);return;}
if (encodeSrgb > 0.5) {
const float3 c = max(value.rgb, 0.0);
- value.rgb = select(c <= 0.0031308, c * 12.92, 1.055 * pow(c, 1.0/2.4) - 0.055);
+ value.rgb = select(c <= 0.0031308, c * 12.92, 1.055 * pow(c, 1.0/2.4) - 0.055) + OutputDither(globalId.xy, ditherStep);
}
if(encodeSrgb < -0.5){const float3 c=max(value.rgb,0.0);value.rgb=select(c<=0.04045,c/12.92,pow((c+0.055)/1.055,2.4));}
outputTex[globalId.xy] = value;
diff --git a/src/pipeline/EnhanceGraph.cpp b/src/pipeline/EnhanceGraph.cpp
index 3eed3cce..76e6d05d 100644
--- a/src/pipeline/EnhanceGraph.cpp
+++ b/src/pipeline/EnhanceGraph.cpp
@@ -153,6 +153,18 @@ bool EnhanceGraph::createColorResources(){
FAILED(upColorLum_->Map(0,nullptr,reinterpret_cast<void**>(&mappedColorLum_))))return false;
return true;
}
+// Product default: dither exactly one LSB of whatever the graph is writing while
+// the colour grade is active, and nothing at all when it is not - every ungraded
+// output path therefore stays byte-identical to the pre-colour build.
+float EnhanceGraph::outputDitherStep() const{
+ if(desc_.outputDitherStep>=0.0f)return desc_.outputDitherStep;
+ // A master switch that is on but neutral renders exactly like no grading at
+ // all (contract), so it must not add noise either - only a real grade does.
+ if(!colorActive_||colorTables_.identity)return 0.0f;
+ if(hdr10Output())return 1.0f/1023.0f;
+ if(desc_.hdrOutput)return 0.0f; // FP16 target: nothing to quantise
+ return 1.0f/255.0f;
+}
void EnhanceGraph::refreshColorTables(){
colorTables_=ColorGradeTables::bake(desc_.color);
if(!colorActive_||!mappedColorCurve_||!mappedColorHue_||!mappedColorLum_)return;
@@ -1765,7 +1777,7 @@ bool EnhanceGraph::process(const AVFrame* frame, double ptsMs, bool reset, Frame
tracker_.transition(list, videoFrame_[parity].Get(), D3D12_RESOURCE_STATE_UNORDERED_ACCESS);
const float constants[8] = {
uintBits(workW_), uintBits(workH_),
- uintBits(workW_), uintBits(workH_), hdr10Output()?2.0f:desc_.hdrOutput?0.0f:1.0f, 0, 0, 0 };
+ uintBits(workW_), uintBits(workH_), hdr10Output()?2.0f:desc_.hdrOutput?0.0f:1.0f, outputDitherStep(), 0, 0 };
blitPass_.bind(list, constants, gpuHandleOf(blitPass_, srcSlot).ptr, gpuHandleOf(blitPass_, uavSlot).ptr);
list->Dispatch((workW_ + 15) / 16, (workH_ + 15) / 16, 1);
tracker_.uavBarrier(list, videoFrame_[parity].Get());
diff --git a/src/sink/MfVideoEncoder.cpp b/src/sink/MfVideoEncoder.cpp
index 8268c75d..6b49fabe 100644
--- a/src/sink/MfVideoEncoder.cpp
+++ b/src/sink/MfVideoEncoder.cpp
@@ -529,7 +529,10 @@ bool MfVideoEncoder::convertToNv12(unsigned frameSlot,bool generated){
states_.transition(list,color,D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE);
states_.transition(list,y_.Get(),D3D12_RESOURCE_STATE_UNORDERED_ACCESS);
states_.transition(list,uv_.Get(),D3D12_RESOURCE_STATE_UNORDERED_ACCESS);
- const float dims[8]={std::bit_cast<float>(width_),std::bit_cast<float>(height_),0,0,0,0,0,0};
+ // reserved.y = output dither step (see NvencD3D12Encoder): the Media
+ // Foundation encoder must dither a graded frame exactly like the NVENC path
+ // so preview/export stay consistent.
+ const float dims[8]={std::bit_cast<float>(width_),std::bit_cast<float>(height_),0,std::bit_cast<float>(graph_?graph_->outputDitherStep():0.0f),0,0,0,0};
convert_.bind(list,dims,gpuHandleOf(convert_,frameSlot+(generated?2:0)).ptr,gpuHandleOf(convert_,8).ptr);
list->Dispatch((width_+15)/16,(height_+15)/16,1);
states_.uavBarrier(list,y_.Get());
diff --git a/src/sink/NvencD3D12Encoder.cpp b/src/sink/NvencD3D12Encoder.cpp
index fe8629fa..36a461a3 100644
--- a/src/sink/NvencD3D12Encoder.cpp
+++ b/src/sink/NvencD3D12Encoder.cpp
@@ -148,7 +148,11 @@ bool NvencD3D12Encoder::encode(unsigned frameSlot,bool generated,int64_t pts){au
Status st=Status::Ok;uint32_t slot=0;auto* list=p.ring->acquireNext(slot,st);if(!list)return false;
auto* color=generated?p.graph->generatedFrameResource(frameSlot):p.graph->videoFrameResource(frameSlot);
p.states.transition(list,color,D3D12_RESOURCE_STATE_NON_PIXEL_SHADER_RESOURCE);p.states.transition(list,p.y.Get(),D3D12_RESOURCE_STATE_UNORDERED_ACCESS);p.states.transition(list,p.uv.Get(),D3D12_RESOURCE_STATE_UNORDERED_ACCESS);
- const float dims[8]={std::bit_cast<float>(p.w),std::bit_cast<float>(p.h),std::bit_cast<float>(p.hdr?(p.graph->hdr10Output()?2u:1u):0u),0,0,0,0,0};p.convert.bind(list,dims,gpuHandleOf(p.convert,frameSlot+(generated?2:0)).ptr,gpuHandleOf(p.convert,8).ptr);list->Dispatch((p.w+15)/16,(p.h+15)/16,1);
+ // reserved.y carries the output dither step so a graded export does not add
+ // banding at the 8-bit (or 10-bit HDR) conversion; ungraded exports keep the
+ // exact legacy bytes because the graph reports a zero step.
+ const float dither=p.graph?p.graph->outputDitherStep():0.0f;
+ const float dims[8]={std::bit_cast<float>(p.w),std::bit_cast<float>(p.h),std::bit_cast<float>(p.hdr?(p.graph->hdr10Output()?2u:1u):0u),std::bit_cast<float>(dither),0,0,0,0};p.convert.bind(list,dims,gpuHandleOf(p.convert,frameSlot+(generated?2:0)).ptr,gpuHandleOf(p.convert,8).ptr);list->Dispatch((p.w+15)/16,(p.h+15)/16,1);
p.states.uavBarrier(list,p.y.Get());p.states.uavBarrier(list,p.uv.Get());p.states.transition(list,p.y.Get(),D3D12_RESOURCE_STATE_COPY_SOURCE);p.states.transition(list,p.uv.Get(),D3D12_RESOURCE_STATE_COPY_SOURCE);p.states.transition(list,s.input.Get(),D3D12_RESOURCE_STATE_COPY_DEST);
for(unsigned plane=0;plane<2;++plane){D3D12_TEXTURE_COPY_LOCATION a{},b{};a.pResource=plane?p.uv.Get():p.y.Get();a.Type=D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;b.pResource=s.input.Get();b.Type=D3D12_TEXTURE_COPY_TYPE_SUBRESOURCE_INDEX;b.SubresourceIndex=plane;list->CopyTextureRegion(&b,0,0,0,&a,nullptr);}
p.states.transition(list,s.input.Get(),D3D12_RESOURCE_STATE_COMMON);p.states.transition(list,color,D3D12_RESOURCE_STATE_COMMON);if(!p.ring->submitAndSignal(slot))return false;
diff --git a/tests/integration/ColorGradeGpuTests.cpp b/tests/integration/ColorGradeGpuTests.cpp
index d8856e70..c64723b3 100644
--- a/tests/integration/ColorGradeGpuTests.cpp
+++ b/tests/integration/ColorGradeGpuTests.cpp
@@ -49,6 +49,20 @@ struct Frame {
}
return true;
}
+ // Horizontal ramp used by the banding check: the grade compresses it so the
+ // ideal 8-bit output advances by a fraction of a code per pixel.
+ bool makeRamp(uint8_t from,uint8_t to){
+ frame=av_frame_alloc();
+ frame->format=AV_PIX_FMT_BGR0;frame->width=frame->height=kSize;
+ frame->color_range=AVCOL_RANGE_UNSPECIFIED;frame->color_trc=AVCOL_TRC_IEC61966_2_1;frame->colorspace=AVCOL_SPC_RGB;
+ if(av_frame_get_buffer(frame,32)<0)return false;
+ for(int y=0;y<kSize;++y)for(int x=0;x<kSize;++x){
+ const int value=from+int(std::lround(double(to-from)*double(x)/double(kSize-1)));
+ auto* p=frame->data[0]+std::size_t(y)*frame->linesize[0]+std::size_t(x)*4;
+ p[0]=uint8_t(value);p[1]=uint8_t(value);p[2]=uint8_t(value);p[3]=255;
+ }
+ return true;
+ }
};
struct Graph {
gfx::D3D12DeviceContext ctx;gfx::CommandSlotRing ring;
@@ -77,6 +91,18 @@ struct Graph {
gd.color.lutInputSpace=inputSpace;
return g.initialize(gd)&&g.createViews();
}
+ bool startWithDither(float ditherStep){
+ gfx::DeviceContextDesc device;Status st;
+ if(!ctx.initialize(device,st)||!ring.initialize(ctx.device(),ctx.directQueue(),ctx.fence(),ctx.fenceEvent(),4,st))return false;
+ up=true;
+ pipeline::EnhanceGraphDesc gd;
+ gd.sourceWidth=gd.sourceHeight=gd.workWidth=gd.workHeight=kSize;
+ gd.rgbInput=true;gd.stillImage=true;gd.noFeatures=true;gd.enableNr=false;gd.enableSr=false;gd.enableFg=false;
+ // A compressing grade: the ramp must be flattened by the tone curve so
+ // plateaus are long enough to measure the dither's effect.
+ gd.color.enabled=true;gd.color.contrast=-100.0f;gd.outputDitherStep=ditherStep;
+ return g.initialize(gd)&&g.createViews();
+ }
bool apply(const engine::EnhancementSettings& settings){return g.applySettings(settings);}
bool render(Frame& frame,sink::RgbaImage& out){
pipeline::EnhanceGraph::FrameOutputs outputs;
@@ -182,7 +208,36 @@ int wmain(){
}
}
}
- // 3. A real .cube file, imported into runtime_local/luts and resolved by name
+ // 3. Output dither (plan section 4.1): a smooth ramp pushed through a
+ // compressing grade must not turn into long flat plateaus at the 8-bit
+ // write. The same graph is rendered with the dither off and at one LSB.
+ {
+ Graph plain,ditheredGraph;
+ const bool started=plain.startWithDither(0.0f)&&ditheredGraph.startWithDither(1.0f/255.0f);
+ check(started,"the dither probe graphs initialise");
+ if(started){
+ Frame rampA,rampB;
+ sink::RgbaImage noDither,dithered;
+ const bool rendered=rampA.makeRamp(112,128)&&rampB.makeRamp(112,128)&&
+ plain.render(rampA,noDither)&&ditheredGraph.render(rampB,dithered);
+ check(rendered,"the compressing ramp renders through both graphs");
+ if(rendered&&!noDither.pixels.empty()&&!dithered.pixels.empty()){
+ const auto longestRun=[&](const sink::RgbaImage& image){
+ int longest=0,current=0,previous=-1;
+ for(int x=8;x<kSize-8;++x){
+ const int value=image.pixels[(std::size_t(kSize/2)*image.width+std::size_t(x))*4];
+ if(value==previous)++current;else current=1;
+ previous=value;longest=std::max(longest,current);
+ }
+ return longest;
+ };
+ const int without=longestRun(noDither),with=longestRun(dithered);
+ check(with<without,std::format("the output dither breaks the banding plateaus apart ({} -> {} px)",without,with));
+ check(with<=6,std::format("dither keeps the longest identical 8-bit run short ({} px, without dither {} px)",with,without));
+ }
+ }
+ }
+ // 4. A real .cube file, imported into runtime_local/luts and resolved by name
// when the graph is built - the same path the UI importer uses.
{
const engine::ColorLutStore store(veyra::runtime::localDataDirectory());