feat: denoise detail optimize
This commit is contained in:
1 parent
ac05e2b5b9
commit
d9fd5387e0
5 files changed
+262
-70
No files matched your search
@@ -12,6 +12,7 @@ import android.opengl.GLES31
|
||||
import com.hinnka.mycamera.model.ColorRecipeParams
|
||||
import com.hinnka.mycamera.model.ColorPaletteMapper
|
||||
import com.hinnka.mycamera.lut.ChromaDenoiseShaders
|
||||
import com.hinnka.mycamera.raw.DenoiseProfileNlmConfig
|
||||
import com.hinnka.mycamera.raw.DenoiseProfileShaders
|
||||
import com.hinnka.mycamera.raw.RawProfileExposureGl
|
||||
import com.hinnka.mycamera.raw.RawRenderingEngine
|
||||
@@ -1826,8 +1827,7 @@ class LutImageProcessor {
|
||||
) {
|
||||
setupBitmapDenoiseFramebuffers(width, height)
|
||||
|
||||
val force = noiseReduction.coerceIn(0f, 1f)
|
||||
val strength = force
|
||||
val strength = noiseReduction.coerceIn(0f, 1f)
|
||||
if (strength <= 0f || width * height < 2) {
|
||||
renderTexturePassthrough(sourceTextureId, bitmapDenoiseFboId[1], width, height)
|
||||
return
|
||||
@@ -1842,7 +1842,7 @@ class LutImageProcessor {
|
||||
} else {
|
||||
bitmapDenoiseTexId[0]
|
||||
}
|
||||
val params = buildBitmapDenoiseParams(width, height, strength, force)
|
||||
val params = buildBitmapDenoiseParams(strength)
|
||||
dispatchBitmapDenoisePreconditionV2(sourceTextureId, preconditionTextureId, width, height, params)
|
||||
dispatchBitmapDenoiseNlm(sourceTextureId, preconditionTextureId, bitmapDenoiseTexId[1], width, height, params)
|
||||
checkGlError("renderBitmapDenoiseProfile")
|
||||
@@ -2156,11 +2156,11 @@ class LutImageProcessor {
|
||||
|
||||
private data class BitmapDenoiseParams(
|
||||
val strength: Float,
|
||||
val lumaForce: Float,
|
||||
val chromaForce: Float,
|
||||
val patchRadius: Int,
|
||||
val searchRadius: Int,
|
||||
val norm: Float,
|
||||
val expectedFineDistance: Float,
|
||||
val expectedGuideDistance: Float,
|
||||
val inverseBandwidth: Float,
|
||||
val coarseGuideWeight: Float,
|
||||
val centralPixelWeight: Float,
|
||||
val p: FloatArray,
|
||||
val signalScale: FloatArray,
|
||||
@@ -2202,37 +2202,30 @@ class LutImageProcessor {
|
||||
bitmapDenoiseNlmBufferPixels = pixelCount
|
||||
}
|
||||
|
||||
private fun buildBitmapDenoiseParams(
|
||||
width: Int,
|
||||
height: Int,
|
||||
strengthValue: Float,
|
||||
force: Float
|
||||
): BitmapDenoiseParams {
|
||||
private fun buildBitmapDenoiseParams(strengthValue: Float): BitmapDenoiseParams {
|
||||
val a = BITMAP_DENOISE_A
|
||||
val b = BITMAP_DENOISE_B
|
||||
val scale = 1.0f
|
||||
val shadows = max(0.1f - 0.1f * ln(a), 0.7f).coerceAtMost(1.8f)
|
||||
val bias = -max(5f + 0.5f * ln(a), 0.0f)
|
||||
val compensateP = 0.05f / 0.05f.pow(shadows)
|
||||
val strength = strengthValue.coerceAtLeast(1e-6f)
|
||||
val strength = strengthValue.coerceIn(0f, 1f)
|
||||
val patchRadius = DenoiseProfileShaders.PATCH_RADIUS
|
||||
val searchRadius = DenoiseProfileShaders.SEARCH_RADIUS
|
||||
val patchWidth = 2 * patchRadius + 1
|
||||
val norm = 0.045f / (patchWidth * patchWidth).toFloat()
|
||||
val weightTuning = DenoiseProfileNlmConfig.weightTuning(patchRadius)
|
||||
val centralPixelWeight = 0.1f * scale
|
||||
return BitmapDenoiseParams(
|
||||
strength = strength,
|
||||
lumaForce = force,
|
||||
chromaForce = force,
|
||||
patchRadius = patchRadius,
|
||||
searchRadius = searchRadius,
|
||||
norm = norm,
|
||||
expectedFineDistance = weightTuning.expectedFineDistance,
|
||||
expectedGuideDistance = weightTuning.expectedGuideDistance,
|
||||
inverseBandwidth = weightTuning.inverseBandwidth,
|
||||
coarseGuideWeight = weightTuning.coarseGuideWeight,
|
||||
centralPixelWeight = centralPixelWeight,
|
||||
p = floatArrayOf(shadows, shadows, shadows, 1.0f),
|
||||
signalScale = floatArrayOf(
|
||||
strength * scale,
|
||||
strength * scale,
|
||||
strength * scale,
|
||||
scale,
|
||||
scale,
|
||||
scale,
|
||||
1.0f
|
||||
),
|
||||
aa = floatArrayOf(a * compensateP, a * compensateP, a * compensateP, 1.0f),
|
||||
@@ -2260,10 +2253,15 @@ class LutImageProcessor {
|
||||
) {
|
||||
dispatchBitmapDenoiseNlmInit(width, height)
|
||||
|
||||
for (qy in -params.searchRadius..0) {
|
||||
for (qx in -params.searchRadius..params.searchRadius) {
|
||||
dispatchBitmapDenoiseNlmFusedAccumulate(preconditionedTextureId, width, height, qx, qy, params)
|
||||
}
|
||||
for (offset in DenoiseProfileNlmConfig.searchOffsets) {
|
||||
dispatchBitmapDenoiseNlmFusedAccumulate(
|
||||
preconditionedTextureId,
|
||||
width,
|
||||
height,
|
||||
offset.x,
|
||||
offset.y,
|
||||
params
|
||||
)
|
||||
}
|
||||
|
||||
dispatchBitmapDenoiseNlmFinish(originalTextureId, outputTextureId, width, height, params)
|
||||
@@ -2292,7 +2290,22 @@ class LutImageProcessor {
|
||||
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(
|
||||
GLES31.glGetUniformLocation(bitmapDenoiseNlmFusedAccuProgram, "uExpectedFineDistance"),
|
||||
params.expectedFineDistance
|
||||
)
|
||||
GLES31.glUniform1f(
|
||||
GLES31.glGetUniformLocation(bitmapDenoiseNlmFusedAccuProgram, "uExpectedGuideDistance"),
|
||||
params.expectedGuideDistance
|
||||
)
|
||||
GLES31.glUniform1f(
|
||||
GLES31.glGetUniformLocation(bitmapDenoiseNlmFusedAccuProgram, "uInverseBandwidth"),
|
||||
params.inverseBandwidth
|
||||
)
|
||||
GLES31.glUniform1f(
|
||||
GLES31.glGetUniformLocation(bitmapDenoiseNlmFusedAccuProgram, "uCoarseGuideWeight"),
|
||||
params.coarseGuideWeight
|
||||
)
|
||||
GLES31.glUniform1f(
|
||||
GLES31.glGetUniformLocation(bitmapDenoiseNlmFusedAccuProgram, "uCentralPixelWeight"),
|
||||
params.centralPixelWeight
|
||||
@@ -2309,6 +2322,10 @@ class LutImageProcessor {
|
||||
setBitmapDenoiseCommonUniforms(bitmapDenoiseNlmFinishProgram, width, height, params)
|
||||
setBitmapDenoiseStripeUniforms(bitmapDenoiseNlmFinishProgram, height)
|
||||
GLES31.glUniform1f(GLES31.glGetUniformLocation(bitmapDenoiseNlmFinishProgram, "uBias"), params.bias - 0.5f * ln(params.scale))
|
||||
GLES31.glUniform1f(
|
||||
GLES31.glGetUniformLocation(bitmapDenoiseNlmFinishProgram, "uDenoiseMix"),
|
||||
params.strength
|
||||
)
|
||||
dispatchBitmapDenoiseImage(width, height, "BitmapDenoise NLM finish")
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,72 @@
|
||||
package com.hinnka.mycamera.raw
|
||||
|
||||
import kotlin.math.max
|
||||
|
||||
internal data class DenoiseProfileOffset(
|
||||
val x: Int,
|
||||
val y: Int,
|
||||
)
|
||||
|
||||
internal data class DenoiseProfileWeightTuning(
|
||||
val expectedFineDistance: Float,
|
||||
val expectedGuideDistance: Float,
|
||||
val inverseBandwidth: Float,
|
||||
val coarseGuideWeight: Float,
|
||||
)
|
||||
|
||||
/**
|
||||
* Statistical and traversal configuration shared by the RAW and bitmap NLM hosts.
|
||||
*
|
||||
* The variance-stabilizing transform targets unit noise variance per color channel. The
|
||||
* difference between two independent samples therefore has variance two. Noise-only patch
|
||||
* energy is removed before applying the NLM bandwidth so that the weight curve starts falling
|
||||
* as soon as a candidate contains energy that the sensor noise model cannot explain.
|
||||
*/
|
||||
internal object DenoiseProfileNlmConfig {
|
||||
private const val COLOR_CHANNEL_COUNT = 3
|
||||
private const val DIFFERENCE_VARIANCE = 2.0f
|
||||
|
||||
// Squared-energy of the separable 3x3 Gaussian kernel [1 2 1]^T[1 2 1] / 16.
|
||||
private const val GUIDE_FILTER_ENERGY = 36.0f / 256.0f
|
||||
|
||||
// Coarse guide differences describe structure rather than independent RGB noise. Give them
|
||||
// enough leverage to reject low-contrast contours without turning the decision into a hard
|
||||
// threshold.
|
||||
const val COARSE_GUIDE_WEIGHT = 8.0f
|
||||
|
||||
val searchOffsets: List<DenoiseProfileOffset> = buildSearchOffsets(
|
||||
DenoiseProfileShaders.SEARCH_RADIUS
|
||||
)
|
||||
|
||||
fun buildSearchOffsets(radius: Int): List<DenoiseProfileOffset> {
|
||||
require(radius >= 0) { "radius must be non-negative" }
|
||||
return buildList {
|
||||
for (qy in -radius..0) {
|
||||
// FUSED_ACCU evaluates +q and -q together. At y == 0 only one horizontal
|
||||
// half-axis is needed; visiting both halves would double horizontal weights.
|
||||
val qxEnd = if (qy == 0) 0 else radius
|
||||
for (qx in -radius..qxEnd) {
|
||||
add(DenoiseProfileOffset(qx, qy))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun weightTuning(patchRadius: Int): DenoiseProfileWeightTuning {
|
||||
require(patchRadius >= 0) { "patchRadius must be non-negative" }
|
||||
val patchWidth = 2 * patchRadius + 1
|
||||
val patchPixels = patchWidth * patchWidth
|
||||
val expectedFineDistance =
|
||||
DIFFERENCE_VARIANCE * COLOR_CHANNEL_COUNT * patchPixels.toFloat()
|
||||
val expectedGuideDistance =
|
||||
DIFFERENCE_VARIANCE * GUIDE_FILTER_ENERGY * patchPixels.toFloat()
|
||||
|
||||
return DenoiseProfileWeightTuning(
|
||||
expectedFineDistance = expectedFineDistance,
|
||||
expectedGuideDistance = expectedGuideDistance,
|
||||
// One additional noise-floor worth of unexplained RGB energy halves the weight.
|
||||
inverseBandwidth = 1.0f / max(expectedFineDistance, 1.0f),
|
||||
coarseGuideWeight = COARSE_GUIDE_WEIGHT,
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,8 @@ package com.hinnka.mycamera.raw
|
||||
*
|
||||
* Input and output stay in un-white-balanced camera RGB. The host keeps darktable's
|
||||
* white-balance-adaptive exponent, but removes WB from the reversible signal scale.
|
||||
* The pipeline mirrors darktable process_nlmeans_cl with a mobile fused accumulator:
|
||||
* The pipeline mirrors darktable process_nlmeans_cl with a mobile fused accumulator and a
|
||||
* low-frequency green guide used to keep low-contrast structure out of the noise estimate:
|
||||
* precondition_v2 -> init -> repeated local-tile accu -> finish_v2.
|
||||
*/
|
||||
object DenoiseProfileShaders {
|
||||
@@ -62,18 +63,43 @@ object DenoiseProfileShaders {
|
||||
uniform vec4 uB;
|
||||
uniform vec4 uSignalScale;
|
||||
|
||||
void main() {
|
||||
ivec2 coord = ivec2(gl_GlobalInvocationID.xy);
|
||||
if (coord.x >= uImageSize.x || coord.y >= uImageSize.y) return;
|
||||
|
||||
vec4 pixel = readPixel(uInput, coord, uImageSize);
|
||||
float alpha = pixel.a;
|
||||
vec4 t = max(
|
||||
vec4 preconditionSignal(vec4 pixel) {
|
||||
return max(
|
||||
2.0 * dtPow(max(vec4(0.0), pixel / uSignalScale + uB), 1.0 - uP / 2.0) /
|
||||
((-uP + 2.0) * sqrt(uA)),
|
||||
vec4(0.0)
|
||||
);
|
||||
t.a = alpha;
|
||||
}
|
||||
|
||||
float preconditionGreen(float signal) {
|
||||
return max(
|
||||
2.0 * pow(max(0.0, signal / uSignalScale.g + uB.g), 1.0 - uP.g / 2.0) /
|
||||
((-uP.g + 2.0) * sqrt(uA.g)),
|
||||
0.0
|
||||
);
|
||||
}
|
||||
|
||||
void main() {
|
||||
ivec2 coord = ivec2(gl_GlobalInvocationID.xy);
|
||||
if (coord.x >= uImageSize.x || coord.y >= uImageSize.y) return;
|
||||
|
||||
vec4 t = preconditionSignal(readPixel(uInput, coord, uImageSize));
|
||||
|
||||
// Alpha is scratch space after preconditioning: FUSED_ACCU replaces it with the
|
||||
// accumulated weight and FINISH_V2 restores alpha from the original input. A 3x3
|
||||
// Gaussian green guide expands the effective structural support of the 3x3 RGB
|
||||
// patch without another full-resolution intermediate texture.
|
||||
float guide =
|
||||
preconditionGreen(readPixel(uInput, coord + ivec2(-1, -1), uImageSize).g) +
|
||||
2.0 * preconditionGreen(readPixel(uInput, coord + ivec2(0, -1), uImageSize).g) +
|
||||
preconditionGreen(readPixel(uInput, coord + ivec2(1, -1), uImageSize).g) +
|
||||
2.0 * preconditionGreen(readPixel(uInput, coord + ivec2(-1, 0), uImageSize).g) +
|
||||
4.0 * t.g +
|
||||
2.0 * preconditionGreen(readPixel(uInput, coord + ivec2(1, 0), uImageSize).g) +
|
||||
preconditionGreen(readPixel(uInput, coord + ivec2(-1, 1), uImageSize).g) +
|
||||
2.0 * preconditionGreen(readPixel(uInput, coord + ivec2(0, 1), uImageSize).g) +
|
||||
preconditionGreen(readPixel(uInput, coord + ivec2(1, 1), uImageSize).g);
|
||||
t.a = guide * (1.0 / 16.0);
|
||||
imageStore(uOutput, coord, t);
|
||||
}
|
||||
""".trimIndent()
|
||||
@@ -102,34 +128,37 @@ object DenoiseProfileShaders {
|
||||
layout(binding = 0) uniform highp sampler2D uInput;
|
||||
layout(std430, binding = 0) buffer AccuBuffer { vec4 u2[]; };
|
||||
|
||||
shared float sDistance[$FUSED_TILE_X * $FUSED_TILE_Y];
|
||||
shared vec2 sDistance[$FUSED_TILE_X * $FUSED_TILE_Y];
|
||||
|
||||
uniform ivec2 uImageSize;
|
||||
uniform int uStripeRowOffset;
|
||||
uniform int uStripeRowCount;
|
||||
uniform ivec2 uQ;
|
||||
uniform float uNorm;
|
||||
uniform float uExpectedFineDistance;
|
||||
uniform float uExpectedGuideDistance;
|
||||
uniform float uInverseBandwidth;
|
||||
uniform float uCoarseGuideWeight;
|
||||
uniform float uCentralPixelWeight;
|
||||
|
||||
float pixelDistance(ivec2 coord, ivec2 q) {
|
||||
vec2 pixelDistance(ivec2 coord, ivec2 q) {
|
||||
ivec2 shifted = coord + q;
|
||||
bool inBounds = shifted.x >= 0 && shifted.x < uImageSize.x &&
|
||||
shifted.y >= 0 && shifted.y < uImageSize.y;
|
||||
if (!inBounds) return 0.0;
|
||||
if (!inBounds) return vec2(0.0);
|
||||
|
||||
vec4 p1 = readPixel(uInput, coord, uImageSize);
|
||||
vec4 p2 = readPixel(uInput, shifted, uImageSize);
|
||||
vec4 tmp = (p1 - p2) * (p1 - p2);
|
||||
return tmp.x + tmp.y + tmp.z;
|
||||
return vec2(tmp.x + tmp.y + tmp.z, tmp.a);
|
||||
}
|
||||
|
||||
float tileDistance(ivec2 imageCoord, ivec2 groupOrigin, ivec2 tileMin, int tileWidth) {
|
||||
vec2 tileDistance(ivec2 imageCoord, ivec2 groupOrigin, ivec2 tileMin, int tileWidth) {
|
||||
ivec2 tileCoord = imageCoord - groupOrigin - tileMin;
|
||||
return sDistance[tileCoord.y * tileWidth + tileCoord.x];
|
||||
}
|
||||
|
||||
float patchWeight(ivec2 center, ivec2 groupOrigin, ivec2 tileMin, int tileWidth) {
|
||||
float distacc = 0.0;
|
||||
vec2 distacc = vec2(0.0);
|
||||
for (int pj = -$PATCH_RADIUS; pj <= $PATCH_RADIUS; pj++) {
|
||||
for (int pi = -$PATCH_RADIUS; pi <= $PATCH_RADIUS; pi++) {
|
||||
ivec2 patchCoord = clamp(
|
||||
@@ -145,7 +174,12 @@ object DenoiseProfileShaders {
|
||||
distacc += tileDistance(center, groupOrigin, tileMin, tileWidth) *
|
||||
patchPixels * uCentralPixelWeight;
|
||||
distacc /= 1.0 + uCentralPixelWeight;
|
||||
return fastMexp2(max(0.0, distacc * uNorm - 2.0));
|
||||
|
||||
float unexplainedFine = max(distacc.x - uExpectedFineDistance, 0.0);
|
||||
float unexplainedGuide = max(distacc.y - uExpectedGuideDistance, 0.0);
|
||||
float unexplainedDistance =
|
||||
unexplainedFine + uCoarseGuideWeight * unexplainedGuide;
|
||||
return fastMexp2(unexplainedDistance * uInverseBandwidth);
|
||||
}
|
||||
|
||||
void main() {
|
||||
@@ -214,6 +248,7 @@ object DenoiseProfileShaders {
|
||||
uniform vec4 uP;
|
||||
uniform vec4 uB;
|
||||
uniform float uBias;
|
||||
uniform float uDenoiseMix;
|
||||
uniform vec4 uSignalScale;
|
||||
|
||||
void main() {
|
||||
@@ -223,7 +258,7 @@ object DenoiseProfileShaders {
|
||||
|
||||
int idx = stripeCoord.y * uImageSize.x + stripeCoord.x;
|
||||
vec4 accu = u2[idx];
|
||||
float alpha = readPixel(uInput, coord, uImageSize).a;
|
||||
vec4 original = readPixel(uInput, coord, uImageSize);
|
||||
vec4 px = accu.a > 0.0 ? accu / accu.a : vec4(0.0);
|
||||
|
||||
vec4 delta = px * px + vec4(uBias);
|
||||
@@ -231,7 +266,8 @@ object DenoiseProfileShaders {
|
||||
vec4 z1 = (px + sqrt(max(vec4(0.0), delta))) / denominator;
|
||||
px = max(dtPow(z1, 1.0 / (1.0 - uP / 2.0)) - uB, vec4(0.0));
|
||||
px *= uSignalScale;
|
||||
px.a = alpha;
|
||||
px.rgb = mix(original.rgb, px.rgb, clamp(uDenoiseMix, 0.0, 1.0));
|
||||
px.a = original.a;
|
||||
imageStore(uOutput, coord, px);
|
||||
}
|
||||
""".trimIndent()
|
||||
|
||||
@@ -4079,7 +4079,9 @@ class RawDemosaicProcessor {
|
||||
TAG,
|
||||
"DenoiseProfile NLM: strength=${params.strength} a=${params.a} b=${params.b} " +
|
||||
"shadows=${params.shadows} bias=${params.bias} patch=${params.patchRadius} " +
|
||||
"search=${params.searchRadius} norm=${params.norm} center=${params.centralPixelWeight} " +
|
||||
"search=${params.searchRadius} fineNoise=${params.expectedFineDistance} " +
|
||||
"guideNoise=${params.expectedGuideDistance} bandwidth=${params.inverseBandwidth} " +
|
||||
"guideWeight=${params.coarseGuideWeight} center=${params.centralPixelWeight} " +
|
||||
"greenNoise=true adaptiveWb=${params.adaptiveWb.contentToString()} " +
|
||||
"signalScale=${params.signalScale.contentToString()}"
|
||||
)
|
||||
@@ -4098,7 +4100,10 @@ class RawDemosaicProcessor {
|
||||
val scale: Float,
|
||||
val patchRadius: Int,
|
||||
val searchRadius: Int,
|
||||
val norm: Float,
|
||||
val expectedFineDistance: Float,
|
||||
val expectedGuideDistance: Float,
|
||||
val inverseBandwidth: Float,
|
||||
val coarseGuideWeight: Float,
|
||||
val centralPixelWeight: Float,
|
||||
val p: FloatArray,
|
||||
val adaptiveWb: FloatArray,
|
||||
@@ -4139,14 +4144,12 @@ class RawDemosaicProcessor {
|
||||
val compensateP = 0.05f / 0.05f.pow(shadows)
|
||||
val patchRadius = DenoiseProfileShaders.PATCH_RADIUS
|
||||
val searchRadius = DenoiseProfileShaders.SEARCH_RADIUS
|
||||
val patchWidth = 2 * patchRadius + 1
|
||||
val norm = 0.045f / (patchWidth * patchWidth).toFloat()
|
||||
val weightTuning = DenoiseProfileNlmConfig.weightTuning(patchRadius)
|
||||
val centralPixelWeight = 0.1f * scale
|
||||
val nlmStrength = strength.coerceAtLeast(1e-6f)
|
||||
val signalScale = floatArrayOf(
|
||||
nlmStrength * scale,
|
||||
nlmStrength * scale,
|
||||
nlmStrength * scale,
|
||||
scale,
|
||||
scale,
|
||||
scale,
|
||||
1.0f
|
||||
)
|
||||
val aa = floatArrayOf(a * compensateP, a * compensateP, a * compensateP, 1.0f)
|
||||
@@ -4161,7 +4164,10 @@ class RawDemosaicProcessor {
|
||||
scale = scale,
|
||||
patchRadius = patchRadius,
|
||||
searchRadius = searchRadius,
|
||||
norm = norm,
|
||||
expectedFineDistance = weightTuning.expectedFineDistance,
|
||||
expectedGuideDistance = weightTuning.expectedGuideDistance,
|
||||
inverseBandwidth = weightTuning.inverseBandwidth,
|
||||
coarseGuideWeight = weightTuning.coarseGuideWeight,
|
||||
centralPixelWeight = centralPixelWeight,
|
||||
p = p,
|
||||
adaptiveWb = adaptiveWb,
|
||||
@@ -4215,18 +4221,16 @@ class RawDemosaicProcessor {
|
||||
for (stripe in stripes) {
|
||||
dispatchDenoiseNlmInit(width, height, stripe)
|
||||
|
||||
for (qy in -params.searchRadius..0) {
|
||||
for (qx in -params.searchRadius..params.searchRadius) {
|
||||
dispatchDenoiseNlmFusedAccumulate(
|
||||
preconditionedTextureId,
|
||||
width,
|
||||
height,
|
||||
stripe,
|
||||
qx,
|
||||
qy,
|
||||
params
|
||||
)
|
||||
}
|
||||
for (offset in DenoiseProfileNlmConfig.searchOffsets) {
|
||||
dispatchDenoiseNlmFusedAccumulate(
|
||||
preconditionedTextureId,
|
||||
width,
|
||||
height,
|
||||
stripe,
|
||||
offset.x,
|
||||
offset.y,
|
||||
params
|
||||
)
|
||||
}
|
||||
|
||||
dispatchDenoiseNlmFinish(
|
||||
@@ -4276,7 +4280,22 @@ class RawDemosaicProcessor {
|
||||
GLES31.glUniform2i(GLES31.glGetUniformLocation(denoiseNlmFusedAccuProgram, "uImageSize"), width, height)
|
||||
setDenoiseStripeUniforms(denoiseNlmFusedAccuProgram, stripe)
|
||||
GLES31.glUniform2i(GLES31.glGetUniformLocation(denoiseNlmFusedAccuProgram, "uQ"), qx, qy)
|
||||
GLES31.glUniform1f(GLES31.glGetUniformLocation(denoiseNlmFusedAccuProgram, "uNorm"), params.norm)
|
||||
GLES31.glUniform1f(
|
||||
GLES31.glGetUniformLocation(denoiseNlmFusedAccuProgram, "uExpectedFineDistance"),
|
||||
params.expectedFineDistance
|
||||
)
|
||||
GLES31.glUniform1f(
|
||||
GLES31.glGetUniformLocation(denoiseNlmFusedAccuProgram, "uExpectedGuideDistance"),
|
||||
params.expectedGuideDistance
|
||||
)
|
||||
GLES31.glUniform1f(
|
||||
GLES31.glGetUniformLocation(denoiseNlmFusedAccuProgram, "uInverseBandwidth"),
|
||||
params.inverseBandwidth
|
||||
)
|
||||
GLES31.glUniform1f(
|
||||
GLES31.glGetUniformLocation(denoiseNlmFusedAccuProgram, "uCoarseGuideWeight"),
|
||||
params.coarseGuideWeight
|
||||
)
|
||||
GLES31.glUniform1f(
|
||||
GLES31.glGetUniformLocation(denoiseNlmFusedAccuProgram, "uCentralPixelWeight"),
|
||||
params.centralPixelWeight
|
||||
@@ -4316,6 +4335,10 @@ class RawDemosaicProcessor {
|
||||
GLES31.glGetUniformLocation(program, "uBias"),
|
||||
params.bias - 0.5f * ln(params.scale)
|
||||
)
|
||||
GLES31.glUniform1f(
|
||||
GLES31.glGetUniformLocation(program, "uDenoiseMix"),
|
||||
params.strength.coerceIn(0f, 1f)
|
||||
)
|
||||
dispatchDenoiseImage(
|
||||
width,
|
||||
stripe.rowCount,
|
||||
|
||||
@@ -105,6 +105,50 @@ class RawNoiseProfileSelectionTest {
|
||||
assertFalse(shader.contains("uWb"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun nlmSearchOffsetsCoverSymmetricWindowExactlyOnce() {
|
||||
val radius = DenoiseProfileShaders.SEARCH_RADIUS
|
||||
val candidates = buildList {
|
||||
DenoiseProfileNlmConfig.buildSearchOffsets(radius).forEach { q ->
|
||||
add(q.x to q.y)
|
||||
if (q.x != 0 || q.y != 0) {
|
||||
add(-q.x to -q.y)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
val windowWidth = 2 * radius + 1
|
||||
assertEquals(windowWidth * windowWidth, candidates.size)
|
||||
assertEquals(candidates.size, candidates.toSet().size)
|
||||
assertTrue(candidates.all { (x, y) -> x in -radius..radius && y in -radius..radius })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun nlmWeightTuningSubtractsVarianceStabilizedNoiseFloor() {
|
||||
val tuning = DenoiseProfileNlmConfig.weightTuning(
|
||||
DenoiseProfileShaders.PATCH_RADIUS
|
||||
)
|
||||
|
||||
assertEquals(54f, tuning.expectedFineDistance, 0f)
|
||||
assertEquals(2.53125f, tuning.expectedGuideDistance, 1e-6f)
|
||||
assertEquals(1f / 54f, tuning.inverseBandwidth, 1e-7f)
|
||||
assertEquals(8f, tuning.coarseGuideWeight, 0f)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun nlmShaderUsesCoarseGuideAndExplicitNoiseFloor() {
|
||||
val shader = DenoiseProfileShaders.PRECONDITION_V2 +
|
||||
DenoiseProfileShaders.FUSED_ACCU +
|
||||
DenoiseProfileShaders.FINISH_V2
|
||||
|
||||
assertTrue(shader.contains("t.a = guide"))
|
||||
assertTrue(shader.contains("uExpectedFineDistance"))
|
||||
assertTrue(shader.contains("uExpectedGuideDistance"))
|
||||
assertTrue(shader.contains("uCoarseGuideWeight"))
|
||||
assertTrue(shader.contains("uDenoiseMix"))
|
||||
assertFalse(shader.contains("distacc * uNorm - 2.0"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun zeroBiasBacktransformExactlyPreservesAnUnchangedDarkSample() {
|
||||
val input = 0.003f
|
||||
|
||||
Reference in new issue
Block a user