fix: denoise error

This commit is contained in:
Hinnka committed 2026-07-17 22:28:58 +08:00
1 parent 9bbe4b9acb
commit 150bfe51f8
4 files changed
+30 -60

No files matched your search

@@ -4,7 +4,7 @@ object ChromaDenoiseDefaults {
const val RAW_CAPTURE_DEFAULT_STRENGTH = 0.25f
private const val MIN_ACTIVE_NOISE_BANDWIDTH = 1.0f
private const val MAX_NOISE_BANDWIDTH = 8.0f
private const val FULL_OUTPUT_STRENGTH_POINT = 0.25f
private const val FULL_OUTPUT_STRENGTH_POINT = 0.5f
private const val EDGE_GUIDANCE_START_POINT = 0.75f
fun forRawCapture(requested: Float): Float =
@@ -141,7 +141,6 @@ object ChromaDenoiseShaders {
float centerSignal,
vec2 baseChroma,
vec2 chromaGuide,
float chromaGuidance,
float invGuideH2,
vec2 invChromaH2,
out vec2 filteredChroma,
@@ -175,13 +174,7 @@ object ChromaDenoiseShaders {
vec2 chromaDelta = sampleValue.yz - chromaGuide;
float chromaDistance =
dot(chromaDelta * chromaDelta, invChromaH2);
// A noisy center chroma must not select only samples with the
// same noise color. In low-SNR shadows chroma is the value
// being estimated, never an edge guide; the prefiltered
// luminance remains responsible for structure. RAW uses the
// symmetric RGB mean rather than a single-channel edge veto.
float chromaWeight =
exp(-chromaDistance * chromaGuidance);
float chromaWeight = exp(-chromaDistance);
float weight = spatialWeight * guideWeight * chromaWeight;
sumChroma += sampleValue.yz * weight;
@@ -222,41 +215,36 @@ object ChromaDenoiseShaders {
vec3 centerValue = rgbToFilterSpace(source.rgb);
vec2 noiseSigma = chromaNoiseSigma(source.rgb);
vec2 localH = max(uH * noiseSigma, vec2(1e-5));
float centerGuide = texture(uGuideTexture, vTexCoord).r;
float guideSigma = guideNoiseSigma(source.rgb);
float signalSnr = max(centerGuide, 0.0) / max(guideSigma, 1e-6);
// Below the useful chroma SNR, disable chroma self-guidance so an
// isolated purple/green/blue noise sample cannot preserve itself by
// rejecting the rest of the neighborhood. Restore it smoothly only
// where real color edges are measurable.
float chromaGuidance = smoothstep(4.0, 12.0, signalSnr);
float shadowFactor = 1.0 - chromaGuidance;
float guideBandwidthScale =
mix(1.0, 4.0, uEdgeGuidanceRelaxation);
// Deep-shadow luminance is itself noisy. Keep its guide permissive
// until signal confidence rises, otherwise a purple RGB impulse can
// preserve itself merely by also changing the local luminance.
float guideH =
guideSigma * mix(10.0, 4.0, chromaGuidance) *
guideBandwidthScale;
guideSigma * 4.0 * guideBandwidthScale;
float invGuideH2 = 1.0 / max(guideH * guideH, 1e-8);
// Denoise strength may relax the range threshold slightly, but every
// spatial scale shares the same bounded color-edge bandwidth. Large
// radii must never gain permission to cross a red/blue boundary.
float strengthT = clamp((uH - 1.0) / 7.0, 0.0, 1.0);
float chromaEdgeSigmaMultiplier = mix(2.75, 3.5, strengthT);
vec2 chromaEdgeH =
max(noiseSigma * chromaEdgeSigmaMultiplier, vec2(1e-5));
vec2 invChromaEdgeH2 =
1.0 / max(chromaEdgeH * chromaEdgeH, vec2(1e-8));
vec2 fineCandidate;
float fineSupport;
// Radius one directly covers the one-to-two-pixel sensor grain that
// was skipped by the previous sparse fine scale.
vec2 fineH = localH * 4.0;
filterScale(
1.0,
centerGuide,
centerValue.x,
centerValue.yz,
centerValue.yz,
chromaGuidance,
invGuideH2,
1.0 / max(fineH * fineH, vec2(1e-8)),
invChromaEdgeH2,
fineCandidate,
fineSupport
);
@@ -265,16 +253,14 @@ object ChromaDenoiseShaders {
vec2 mediumCandidate;
float mediumSupport;
vec2 mediumH = localH * 1.8;
filterScale(
4.0,
centerGuide,
centerValue.x,
fineChroma,
fineChroma,
chromaGuidance,
invGuideH2,
1.0 / max(mediumH * mediumH, vec2(1e-8)),
invChromaEdgeH2,
mediumCandidate,
mediumSupport
);
@@ -284,16 +270,14 @@ object ChromaDenoiseShaders {
vec2 coarseCandidate;
float coarseSupport;
vec2 coarseH = localH * 2.1;
filterScale(
14.0,
centerGuide,
centerValue.x,
mediumChroma,
mediumChroma,
chromaGuidance,
invGuideH2,
1.0 / max(coarseH * coarseH, vec2(1e-8)),
invChromaEdgeH2,
coarseCandidate,
coarseSupport
);
@@ -301,30 +285,7 @@ object ChromaDenoiseShaders {
uOutputStrength * featheredEdgeSupport(coarseSupport, 2.0);
vec2 coarseChroma = mix(mediumChroma, coarseCandidate, coarseMix);
// Low-frequency chroma clouds need samples well outside the grain
// neighborhood. The radius and bandwidth expand only in shadows;
// guide support rejects cross-structure fusion while it is active,
// while the chroma bandwidth remains wide enough to treat a cast as noise.
vec2 cloudCandidate;
float cloudSupport;
vec2 cloudH = localH * mix(2.5, 3.5, shadowFactor);
float cloudRadius = 56.0 * mix(1.0, 1.25, shadowFactor);
filterScale(
cloudRadius,
centerGuide,
centerValue.x,
coarseChroma,
coarseChroma,
chromaGuidance,
invGuideH2,
1.0 / max(cloudH * cloudH, vec2(1e-8)),
cloudCandidate,
cloudSupport
);
float cloudMix =
uOutputStrength * featheredEdgeSupport(cloudSupport, 1.5);
centerValue.yz = mix(coarseChroma, cloudCandidate, cloudMix);
centerValue.yz = coarseChroma;
fragColor = vec4(filterSpaceToRgb(centerValue), source.a);
}
""".trimIndent()
@@ -2059,6 +2059,7 @@ class LutImageProcessor {
GLES31.glUseProgram(bitmapDenoiseNlmInitProgram)
GLES31.glBindBufferBase(GLES31.GL_SHADER_STORAGE_BUFFER, 0, bitmapDenoiseNlmU2BufferId)
GLES31.glUniform2i(GLES31.glGetUniformLocation(bitmapDenoiseNlmInitProgram, "uImageSize"), width, height)
setBitmapDenoiseStripeUniforms(bitmapDenoiseNlmInitProgram, height)
dispatchBitmapDenoiseImage(width, height, "BitmapDenoise NLM init")
GLES31.glMemoryBarrier(GLES31.GL_SHADER_STORAGE_BARRIER_BIT)
}
@@ -2075,6 +2076,7 @@ class LutImageProcessor {
bindComputeSampler(bitmapDenoiseNlmFusedAccuProgram, "uInput", 0, input)
GLES31.glBindBufferBase(GLES31.GL_SHADER_STORAGE_BUFFER, 0, bitmapDenoiseNlmU2BufferId)
GLES31.glUniform2i(GLES31.glGetUniformLocation(bitmapDenoiseNlmFusedAccuProgram, "uImageSize"), width, height)
setBitmapDenoiseStripeUniforms(bitmapDenoiseNlmFusedAccuProgram, height)
GLES31.glUniform2i(GLES31.glGetUniformLocation(bitmapDenoiseNlmFusedAccuProgram, "uQ"), qx, qy)
GLES31.glUniform1f(GLES31.glGetUniformLocation(bitmapDenoiseNlmFusedAccuProgram, "uNorm"), params.norm)
GLES31.glUniform1f(
@@ -2091,10 +2093,16 @@ class LutImageProcessor {
GLES31.glBindBufferBase(GLES31.GL_SHADER_STORAGE_BUFFER, 0, bitmapDenoiseNlmU2BufferId)
GLES31.glBindImageTexture(1, output, 0, false, 0, GLES31.GL_WRITE_ONLY, GLES31.GL_RGBA16F)
setBitmapDenoiseCommonUniforms(bitmapDenoiseNlmFinishProgram, width, height, params)
setBitmapDenoiseStripeUniforms(bitmapDenoiseNlmFinishProgram, height)
GLES31.glUniform1f(GLES31.glGetUniformLocation(bitmapDenoiseNlmFinishProgram, "uBias"), params.bias - 0.5f * ln(params.scale))
dispatchBitmapDenoiseImage(width, height, "BitmapDenoise NLM finish")
}
private fun setBitmapDenoiseStripeUniforms(program: Int, height: Int) {
GLES31.glUniform1i(GLES31.glGetUniformLocation(program, "uStripeRowOffset"), 0)
GLES31.glUniform1i(GLES31.glGetUniformLocation(program, "uStripeRowCount"), height)
}
private fun setBitmapDenoiseCommonUniforms(program: Int, width: Int, height: Int, params: BitmapDenoiseParams) {
GLES31.glUniform2i(GLES31.glGetUniformLocation(program, "uImageSize"), width, height)
GLES31.glUniform4fv(GLES31.glGetUniformLocation(program, "uA"), 1, params.aa, 0)
@@ -37,13 +37,14 @@ class ChromaDenoiseShadersTest {
}
@Test
fun lowSnrChromaCannotUseItsOwnNoiseColorAsAnEdgeGuide() {
fun everyScaleUsesTheSameBoundedChromaEdgeThreshold() {
val shader = ChromaDenoiseShaders.PASS_CHROMA_DENOISE
assertTrue(shader.contains("smoothstep(4.0, 12.0, signalSnr)"))
assertTrue(shader.contains("exp(-chromaDistance * chromaGuidance)"))
assertTrue(shader.contains("float shadowFactor = 1.0 - chromaGuidance;"))
assertTrue(shader.contains("mix(10.0, 4.0, chromaGuidance)"))
assertTrue(shader.contains("exp(-chromaDistance)"))
assertTrue(shader.contains("float chromaEdgeSigmaMultiplier"))
assertFalse(shader.contains("signalSnr"))
assertFalse(shader.contains("cloudRadius"))
assertFalse(shader.contains("vec2 cloudH"))
}
@Test