refactor: remove legacy RAW denoisers and implement fallback noise model

This commit is contained in:
zhoubinjia committed 2026-11-09 13:41:45 +08:00
1 parent a57a1877bf
commit d185a1961d
10 files changed
+380 -1086

No files matched your search

@@ -5790,6 +5790,7 @@ object GalleryManager {
rawBlackPointCorrection = updatedMetadata?.rawBlackPointCorrection ?: 0f,
rawWhitePointCorrection = updatedMetadata?.rawWhitePointCorrection ?: 0f,
applyLensShadingCorrection = resolveRawLensShadingCorrectionEnabled(context, updatedMetadata),
mgcTuningSnr = RawMgcTuningSnr.read(rawMetadata.customProperties),
rawBlackLevelMode = updatedMetadata?.rawBlackLevelMode,
rawCustomBlackLevel = updatedMetadata?.rawCustomBlackLevel,
rawWhiteLevelMode = updatedMetadata?.rawWhiteLevelMode,
@@ -1,10 +1,5 @@
package com.hinnka.mycamera.lut
import android.opengl.GLES30
import android.opengl.Matrix
import com.hinnka.mycamera.raw.RawFullscreenQuad
import com.hinnka.mycamera.raw.RawGlesProgram
import com.hinnka.mycamera.utils.PLog
/**
* Shared multi-scale edge-guided chroma denoise shader.
@@ -296,156 +291,3 @@ object ChromaDenoiseShaders {
}
""".trimIndent()
}
/** Complete edge-guide + chroma-filter algorithm shared by RAW and bitmap pipelines. */
internal class ChromaDenoiseAlgorithm(
private val quad: RawFullscreenQuad,
) {
data class NoiseModel(
val redSlope: Float,
val redOffset: Float,
val greenSlope: Float,
val greenOffset: Float,
val blueSlope: Float,
val blueOffset: Float,
)
data class Input(
val sourceTextureId: Int,
val guideFramebufferId: Int,
val guideTextureId: Int,
val outputFramebufferId: Int,
val outputTextureId: Int,
val width: Int,
val height: Int,
val strength: Float,
val cameraRgbInput: Boolean,
val noiseModel: NoiseModel,
)
data class Output(val textureId: Int, val width: Int, val height: Int)
private var guideProgram = 0
private var filterProgram = 0
fun initialize(): Boolean {
if (guideProgram == 0) {
guideProgram = quad.createProgram(
ChromaDenoiseShaders.PASS_EDGE_GUIDE,
"ChromaDenoiseEdgeGuide",
)
}
if (filterProgram == 0) {
filterProgram = quad.createProgram(
ChromaDenoiseShaders.PASS_CHROMA_DENOISE,
"ChromaDenoiseFilter",
)
}
return guideProgram != 0 && filterProgram != 0
}
fun execute(input: Input): Output? {
if (!initialize()) return null
require(input.width > 0 && input.height > 0)
renderGuide(input)
renderFilter(input)
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, 0)
RawGlesProgram.logErrors("ChromaDenoiseAlgorithm")
PLog.d(
TAG,
"complete: size=${input.width}x${input.height} strength=${input.strength} " +
"cameraRgb=${input.cameraRgbInput}",
)
return Output(input.outputTextureId, input.width, input.height)
}
fun release() {
if (guideProgram != 0) GLES30.glDeleteProgram(guideProgram)
if (filterProgram != 0) GLES30.glDeleteProgram(filterProgram)
guideProgram = 0
filterProgram = 0
}
private fun renderGuide(input: Input) {
GLES30.glUseProgram(guideProgram)
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, input.guideFramebufferId)
GLES30.glViewport(0, 0, input.width, input.height)
bindTexture(guideProgram, "uInputTexture", 0, input.sourceTextureId)
GLES30.glUniform2f(
GLES30.glGetUniformLocation(guideProgram, "uTexelSize"),
1f / input.width,
1f / input.height,
)
GLES30.glUniform1i(
GLES30.glGetUniformLocation(guideProgram, "uCameraRgbInput"),
if (input.cameraRgbInput) 1 else 0,
)
bindIdentityTextureMatrix(guideProgram)
quad.draw(guideProgram)
}
private fun renderFilter(input: Input) {
GLES30.glUseProgram(filterProgram)
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, input.outputFramebufferId)
GLES30.glViewport(0, 0, input.width, input.height)
bindTexture(filterProgram, "uInputTexture", 0, input.sourceTextureId)
bindTexture(filterProgram, "uGuideTexture", 1, input.guideTextureId)
bindIdentityTextureMatrix(filterProgram)
GLES30.glUniform2f(
GLES30.glGetUniformLocation(filterProgram, "uTexelSize"),
1f / input.width,
1f / input.height,
)
GLES30.glUniform1f(
GLES30.glGetUniformLocation(filterProgram, "uOutputStrength"),
ChromaDenoiseDefaults.outputStrength(input.strength),
)
GLES30.glUniform1f(
GLES30.glGetUniformLocation(filterProgram, "uEdgeGuidanceRelaxation"),
ChromaDenoiseDefaults.edgeGuidanceRelaxation(input.strength),
)
GLES30.glUniform1i(
GLES30.glGetUniformLocation(filterProgram, "uCameraRgbInput"),
if (input.cameraRgbInput) 1 else 0,
)
GLES30.glUniform1f(
GLES30.glGetUniformLocation(filterProgram, "uH"),
ChromaDenoiseDefaults.noiseBandwidth(input.strength),
)
GLES30.glUniform4f(
GLES30.glGetUniformLocation(filterProgram, "uNoiseModelRB"),
input.noiseModel.redSlope,
input.noiseModel.redOffset,
input.noiseModel.blueSlope,
input.noiseModel.blueOffset,
)
GLES30.glUniform2f(
GLES30.glGetUniformLocation(filterProgram, "uNoiseModelG"),
input.noiseModel.greenSlope,
input.noiseModel.greenOffset,
)
quad.draw(filterProgram)
}
private fun bindTexture(program: Int, uniform: String, unit: Int, textureId: Int) {
GLES30.glActiveTexture(GLES30.GL_TEXTURE0 + unit)
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, textureId)
GLES30.glUniform1i(GLES30.glGetUniformLocation(program, uniform), unit)
}
private fun bindIdentityTextureMatrix(program: Int) {
val identity = FloatArray(16)
Matrix.setIdentityM(identity, 0)
GLES30.glUniformMatrix4fv(
GLES30.glGetUniformLocation(program, "uTexMatrix"),
1,
false,
identity,
0,
)
}
private companion object {
const val TAG = "ChromaDenoise"
}
}
@@ -1,692 +0,0 @@
package com.hinnka.mycamera.raw
import android.opengl.GLES30
import android.opengl.GLES31
import com.hinnka.mycamera.processor.DenoiseStrength
import com.hinnka.mycamera.processor.GlesComputeWorkGroup
import com.hinnka.mycamera.processor.GlesGpuScheduler
import com.hinnka.mycamera.utils.PLog
import kotlin.math.ln
import kotlin.math.max
import kotlin.math.pow
/**
* OpenGL ES 3.1 compute port of darktable's denoiseprofile NLM path.
*
* 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 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 {
const val SEARCH_RADIUS = 5
const val PATCH_RADIUS = 1
// darktable's neutral bias setting makes finish_v2 the exact inverse when NLM leaves a
// transformed sample unchanged. This is required to keep sensor black and channel ratios.
const val BLACK_PRESERVING_BIAS = 0.0f
const val IMAGE_LOCAL_X = GlesComputeWorkGroup.IMAGE_TILE_SIZE
const val IMAGE_LOCAL_Y = GlesComputeWorkGroup.IMAGE_TILE_SIZE
private const val FUSED_TILE_X = IMAGE_LOCAL_X + SEARCH_RADIUS + 2 * PATCH_RADIUS
private const val FUSED_TILE_Y = IMAGE_LOCAL_Y + SEARCH_RADIUS + 2 * PATCH_RADIUS
private const val COMMON = """
precision highp float;
precision highp int;
vec4 readPixel(sampler2D image, ivec2 coord, ivec2 size) {
ivec2 c = clamp(coord, ivec2(0), size - ivec2(1));
return texelFetch(image, c, 0);
}
vec4 dtPow(vec4 a, vec4 b) {
return pow(a, b);
}
float fastMexp2(float x) {
const float i1 = float(0x3f800000u);
const float i2 = float(0x3f000000u);
float k0 = i1 + x * (i2 - i1);
uint bits = (k0 >= float(0x800000u)) ? uint(k0) : 0u;
return uintBitsToFloat(bits);
}
float ddirac(ivec2 q) {
return (q.x != 0 || q.y != 0) ? 1.0 : 0.0;
}
int pixelIndex(ivec2 coord, ivec2 size) {
return coord.y * size.x + coord.x;
}
"""
val PRECONDITION_V2 = """
#version 310 es
$COMMON
layout(local_size_x = $IMAGE_LOCAL_X, local_size_y = $IMAGE_LOCAL_Y) in;
layout(binding = 0) uniform highp sampler2D uInput;
layout(rgba16f, binding = 1) writeonly uniform highp image2D uOutput;
uniform ivec2 uImageSize;
uniform vec4 uA;
uniform vec4 uP;
uniform vec4 uB;
uniform vec4 uSignalScale;
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)
);
}
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()
val INIT = """
#version 310 es
$COMMON
layout(local_size_x = $IMAGE_LOCAL_X, local_size_y = $IMAGE_LOCAL_Y) in;
layout(std430, binding = 0) buffer AccuBuffer { vec4 u2[]; };
uniform ivec2 uImageSize;
uniform int uStripeRowCount;
void main() {
ivec2 stripeCoord = ivec2(gl_GlobalInvocationID.xy);
if (stripeCoord.x >= uImageSize.x || stripeCoord.y >= uStripeRowCount) return;
u2[stripeCoord.y * uImageSize.x + stripeCoord.x] = vec4(0.0);
}
""".trimIndent()
val FUSED_ACCU = """
#version 310 es
$COMMON
layout(local_size_x = $IMAGE_LOCAL_X, local_size_y = $IMAGE_LOCAL_Y) in;
layout(binding = 0) uniform highp sampler2D uInput;
layout(std430, binding = 0) buffer AccuBuffer { vec4 u2[]; };
shared vec2 sDistance[$FUSED_TILE_X * $FUSED_TILE_Y];
uniform ivec2 uImageSize;
uniform int uStripeRowOffset;
uniform int uStripeRowCount;
uniform ivec2 uQ;
uniform float uExpectedFineDistance;
uniform float uExpectedGuideDistance;
uniform float uInverseBandwidth;
uniform float uCoarseGuideWeight;
uniform float uCentralPixelWeight;
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 vec2(0.0);
vec4 p1 = readPixel(uInput, coord, uImageSize);
vec4 p2 = readPixel(uInput, shifted, uImageSize);
vec4 tmp = (p1 - p2) * (p1 - p2);
return vec2(tmp.x + tmp.y + tmp.z, tmp.a);
}
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) {
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(
center + ivec2(pi, pj),
ivec2(0),
uImageSize - ivec2(1)
);
distacc += tileDistance(patchCoord, groupOrigin, tileMin, tileWidth);
}
}
float patchPixels = float((2 * $PATCH_RADIUS + 1) * (2 * $PATCH_RADIUS + 1));
distacc += tileDistance(center, groupOrigin, tileMin, tileWidth) *
patchPixels * uCentralPixelWeight;
distacc /= 1.0 + uCentralPixelWeight;
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() {
ivec2 groupOrigin = ivec2(
int(gl_WorkGroupID.x) * $IMAGE_LOCAL_X,
uStripeRowOffset + int(gl_WorkGroupID.y) * $IMAGE_LOCAL_Y
);
ivec2 tileMin = min(ivec2(-$PATCH_RADIUS), -uQ - ivec2($PATCH_RADIUS));
ivec2 tileMax = max(ivec2($PATCH_RADIUS), -uQ + ivec2($PATCH_RADIUS));
int tileWidth = $IMAGE_LOCAL_X + tileMax.x - tileMin.x;
int tileHeight = $IMAGE_LOCAL_Y + tileMax.y - tileMin.y;
int tilePixels = tileWidth * tileHeight;
int localIndex = int(gl_LocalInvocationID.y) * $IMAGE_LOCAL_X +
int(gl_LocalInvocationID.x);
int localCount = $IMAGE_LOCAL_X * $IMAGE_LOCAL_Y;
for (int i = localIndex; i < tilePixels; i += localCount) {
int tileX = i - (i / tileWidth) * tileWidth;
int tileY = i / tileWidth;
ivec2 imageCoord = groupOrigin + ivec2(tileX, tileY) + tileMin;
imageCoord = clamp(imageCoord, ivec2(0), uImageSize - ivec2(1));
sDistance[i] = pixelDistance(imageCoord, uQ);
}
memoryBarrierShared();
barrier();
ivec2 stripeCoord = ivec2(gl_GlobalInvocationID.xy);
if (stripeCoord.x >= uImageSize.x || stripeCoord.y >= uStripeRowCount) return;
ivec2 coord = stripeCoord + ivec2(0, uStripeRowOffset);
ivec2 plusCoord = coord + uQ;
ivec2 minusCoord = coord - uQ;
bool plusInBounds = plusCoord.x >= 0 && plusCoord.x < uImageSize.x &&
plusCoord.y >= 0 && plusCoord.y < uImageSize.y;
bool minusInBounds = minusCoord.x >= 0 && minusCoord.x < uImageSize.x &&
minusCoord.y >= 0 && minusCoord.y < uImageSize.y;
float weight = plusInBounds ? patchWeight(coord, groupOrigin, tileMin, tileWidth) : 0.0;
float weightMinus = (minusInBounds && ddirac(uQ) > 0.0)
? patchWeight(minusCoord, groupOrigin, tileMin, tileWidth)
: 0.0;
vec4 u1Pq = plusInBounds ? readPixel(uInput, plusCoord, uImageSize) : vec4(0.0);
vec4 u1Mq = minusInBounds ? readPixel(uInput, minusCoord, uImageSize) : vec4(0.0);
vec4 accu = weight * u1Pq + weightMinus * u1Mq;
accu.a = weight + weightMinus;
int idx = stripeCoord.y * uImageSize.x + stripeCoord.x;
u2[idx] = u2[idx] + accu;
}
""".trimIndent()
val FINISH_V2 = """
#version 310 es
$COMMON
layout(local_size_x = $IMAGE_LOCAL_X, local_size_y = $IMAGE_LOCAL_Y) in;
layout(binding = 0) uniform highp sampler2D uInput;
layout(std430, binding = 0) readonly buffer AccuBuffer { vec4 u2[]; };
layout(rgba16f, binding = 1) writeonly uniform highp image2D uOutput;
uniform ivec2 uImageSize;
uniform int uStripeRowOffset;
uniform int uStripeRowCount;
uniform vec4 uA;
uniform vec4 uP;
uniform vec4 uB;
uniform float uBias;
uniform float uDenoiseMix;
uniform vec4 uSignalScale;
void main() {
ivec2 stripeCoord = ivec2(gl_GlobalInvocationID.xy);
if (stripeCoord.x >= uImageSize.x || stripeCoord.y >= uStripeRowCount) return;
ivec2 coord = stripeCoord + ivec2(0, uStripeRowOffset);
int idx = stripeCoord.y * uImageSize.x + stripeCoord.x;
vec4 accu = u2[idx];
vec4 original = readPixel(uInput, coord, uImageSize);
vec4 px = accu.a > 0.0 ? accu / accu.a : vec4(0.0);
vec4 delta = px * px + vec4(uBias);
vec4 denominator = 4.0 / (sqrt(uA) * (2.0 - uP));
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.rgb = mix(original.rgb, px.rgb, clamp(uDenoiseMix, 0.0, 1.0));
px.a = original.a;
imageStore(uOutput, coord, px);
}
""".trimIndent()
}
/** Complete darktable denoiseprofile variance-transform + striped NLM algorithm. */
internal class DenoiseProfileAlgorithm {
data class Input(
val sourceTextureId: Int,
val width: Int,
val height: Int,
val strength: Float,
val noiseSlope: Float,
val noiseOffset: Float,
val adaptiveWhiteBalance: FloatArray,
)
data class Output(val textureId: Int, val width: Int, val height: Int)
private data class Params(
val strength: Float,
val bias: Float,
val scale: Float,
val expectedFineDistance: Float,
val expectedGuideDistance: Float,
val inverseBandwidth: Float,
val coarseGuideWeight: Float,
val centralPixelWeight: Float,
val p: FloatArray,
val signalScale: FloatArray,
val aa: FloatArray,
val bb: FloatArray,
)
private var preconditionProgram = 0
private var initProgram = 0
private var fusedAccumulateProgram = 0
private var finishProgram = 0
private val textureIds = intArrayOf(0, 0)
private var textureWidth = 0
private var textureHeight = 0
private var accumulatorBufferId = 0
private var accumulatorWidth = 0
private var accumulatorRows = 0
private var maxSsboBytes = 0L
fun initialize(): Boolean {
if (preconditionProgram == 0) {
preconditionProgram = RawGlesProgram.compileCompute(
DenoiseProfileShaders.PRECONDITION_V2,
"DENOISE_PROFILE_PRECONDITION",
)
}
if (initProgram == 0) {
initProgram = RawGlesProgram.compileCompute(
DenoiseProfileShaders.INIT,
"DENOISE_PROFILE_INIT",
)
}
if (fusedAccumulateProgram == 0) {
fusedAccumulateProgram = RawGlesProgram.compileCompute(
DenoiseProfileShaders.FUSED_ACCU,
"DENOISE_PROFILE_FUSED_ACCU",
)
}
if (finishProgram == 0) {
finishProgram = RawGlesProgram.compileCompute(
DenoiseProfileShaders.FINISH_V2,
"DENOISE_PROFILE_FINISH",
)
}
return programsReady()
}
fun execute(input: Input): Output? {
if (input.strength <= 0f || input.width * input.height < 2) {
return Output(input.sourceTextureId, input.width, input.height)
}
require(input.adaptiveWhiteBalance.size >= 3)
if (!initialize() || !ensureResources(input.width, input.height)) return null
val params = buildParams(input)
PLog.d(
TAG,
"execute: size=${input.width}x${input.height} strength=${params.strength} " +
"slope=${input.noiseSlope} offset=${input.noiseOffset} " +
"stripeRows=$accumulatorRows wb=${input.adaptiveWhiteBalance.contentToString()}",
)
dispatchPrecondition(input, params)
dispatchNlm(input, params)
RawGlesProgram.logErrors("DenoiseProfile execute")
return Output(textureIds[1], input.width, input.height)
}
fun release() {
intArrayOf(
preconditionProgram,
initProgram,
fusedAccumulateProgram,
finishProgram,
).forEach { program ->
if (program != 0) GLES31.glDeleteProgram(program)
}
preconditionProgram = 0
initProgram = 0
fusedAccumulateProgram = 0
finishProgram = 0
releaseTextures()
releaseAccumulator()
maxSsboBytes = 0L
}
private fun programsReady(): Boolean =
preconditionProgram != 0 && initProgram != 0 &&
fusedAccumulateProgram != 0 && finishProgram != 0
private fun ensureResources(width: Int, height: Int): Boolean {
if (textureWidth != width || textureHeight != height || textureIds.any { it == 0 }) {
releaseTextures()
GLES30.glGenTextures(textureIds.size, textureIds, 0)
textureIds.forEach { textureId ->
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, textureId)
GLES30.glTexStorage2D(
GLES30.GL_TEXTURE_2D,
1,
GLES30.GL_RGBA16F,
width,
height,
)
GLES30.glTexParameteri(
GLES30.GL_TEXTURE_2D,
GLES30.GL_TEXTURE_MIN_FILTER,
GLES30.GL_NEAREST,
)
GLES30.glTexParameteri(
GLES30.GL_TEXTURE_2D,
GLES30.GL_TEXTURE_MAG_FILTER,
GLES30.GL_NEAREST,
)
GLES30.glTexParameteri(
GLES30.GL_TEXTURE_2D,
GLES30.GL_TEXTURE_WRAP_S,
GLES30.GL_CLAMP_TO_EDGE,
)
GLES30.glTexParameteri(
GLES30.GL_TEXTURE_2D,
GLES30.GL_TEXTURE_WRAP_T,
GLES30.GL_CLAMP_TO_EDGE,
)
}
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, 0)
textureWidth = width
textureHeight = height
}
ensureAccumulator(width, height)
RawGlesProgram.logErrors("DenoiseProfile resources")
return textureIds.all { it != 0 } && accumulatorBufferId != 0 && accumulatorRows > 0
}
private fun ensureAccumulator(width: Int, height: Int) {
if (accumulatorWidth == width && accumulatorRows > 0 && accumulatorBufferId != 0) return
releaseAccumulator()
var rows = DenoiseProfileStripePlanner.capacityRows(width, height, queryMaxSsboBytes())
if (rows <= 0) return
val buffers = IntArray(1)
GLES31.glGenBuffers(1, buffers, 0)
accumulatorBufferId = buffers[0]
GLES31.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, accumulatorBufferId)
while (rows > 0) {
val bytes = DenoiseProfileStripePlanner.requiredBytes(width, rows)
GLES31.glBufferData(
GLES31.GL_SHADER_STORAGE_BUFFER,
bytes.toInt(),
null,
GLES31.GL_DYNAMIC_DRAW,
)
val error = GLES30.glGetError()
if (error == GLES30.GL_NO_ERROR) {
accumulatorWidth = width
accumulatorRows = rows
break
}
PLog.w(TAG, "accumulator ${width}x$rows allocation failed: glError=$error")
rows = nextSmallerStripeRows(rows)
}
GLES31.glBindBuffer(GLES31.GL_SHADER_STORAGE_BUFFER, 0)
if (accumulatorRows == 0) releaseAccumulator()
}
private fun queryMaxSsboBytes(): Long {
if (maxSsboBytes > 0L) return maxSsboBytes
val value = LongArray(1)
GLES30.glGetInteger64v(GLES31.GL_MAX_SHADER_STORAGE_BLOCK_SIZE, value, 0)
val error = GLES30.glGetError()
maxSsboBytes = if (error == GLES30.GL_NO_ERROR && value[0] > 0L) {
value[0]
} else {
PLog.w(TAG, "GL_MAX_SHADER_STORAGE_BLOCK_SIZE unavailable; using GLES minimum")
GLES31_MIN_SSBO_BYTES
}
return maxSsboBytes
}
private fun nextSmallerStripeRows(currentRows: Int): Int {
if (currentRows <= 1) return 0
val half = currentRows / 2
val workgroupRows = DenoiseProfileShaders.IMAGE_LOCAL_Y
return if (half >= workgroupRows) half - half % workgroupRows else half
}
private fun buildParams(input: Input): Params {
val strength = DenoiseStrength.clamp(input.strength)
val varianceScale = DenoiseStrength.noiseVarianceScale(strength)
val a = (input.noiseSlope * varianceScale).coerceAtLeast(1e-10f)
val b = (input.noiseOffset * varianceScale).coerceAtLeast(1e-10f)
val scale = 1f
val shadows = max(0.1f - 0.1f * ln(a), 0.7f).coerceAtMost(1.8f)
val p = FloatArray(4) { index ->
if (index == 3) 1f else {
max(shadows + 0.1f * ln(scale / input.adaptiveWhiteBalance[index]), 0f)
}
}
val compensateP = 0.05f / 0.05f.pow(shadows)
val tuning = DenoiseProfileNlmConfig.weightTuning(DenoiseProfileShaders.PATCH_RADIUS)
return Params(
strength = strength,
bias = DenoiseProfileShaders.BLACK_PRESERVING_BIAS,
scale = scale,
expectedFineDistance = tuning.expectedFineDistance,
expectedGuideDistance = tuning.expectedGuideDistance,
inverseBandwidth = tuning.inverseBandwidth,
coarseGuideWeight = tuning.coarseGuideWeight,
centralPixelWeight = 0.1f * scale,
p = p,
signalScale = floatArrayOf(scale, scale, scale, 1f),
aa = floatArrayOf(a * compensateP, a * compensateP, a * compensateP, 1f),
bb = floatArrayOf(b, b, b, 1f),
)
}
private fun dispatchPrecondition(input: Input, params: Params) {
GLES31.glUseProgram(preconditionProgram)
bindSampler(preconditionProgram, "uInput", 0, input.sourceTextureId)
GLES31.glBindImageTexture(
1,
textureIds[0],
0,
false,
0,
GLES31.GL_WRITE_ONLY,
GLES30.GL_RGBA16F,
)
setCommonUniforms(preconditionProgram, input.width, input.height, params)
dispatchImage(input.width, input.height, "precondition")
}
private fun dispatchNlm(input: Input, params: Params) {
DenoiseProfileStripePlanner.plan(input.height, accumulatorRows).forEach { stripe ->
dispatchInit(input.width, input.height, stripe)
DenoiseProfileNlmConfig.searchOffsets.forEach { offset ->
dispatchAccumulate(input.width, input.height, stripe, offset.x, offset.y, params)
}
dispatchFinish(input, stripe, params)
GlesGpuScheduler.yieldToUiRenderer()
}
}
private fun dispatchInit(width: Int, height: Int, stripe: DenoiseProfileStripe) {
GLES31.glUseProgram(initProgram)
GLES31.glBindBufferBase(GLES31.GL_SHADER_STORAGE_BUFFER, 0, accumulatorBufferId)
GLES31.glUniform2i(GLES31.glGetUniformLocation(initProgram, "uImageSize"), width, height)
GLES31.glUniform1i(
GLES31.glGetUniformLocation(initProgram, "uStripeRowCount"),
stripe.rowCount,
)
dispatchImage(width, stripe.rowCount, "init row=${stripe.rowOffset}")
GLES31.glMemoryBarrier(GLES31.GL_SHADER_STORAGE_BARRIER_BIT)
}
private fun dispatchAccumulate(
width: Int,
height: Int,
stripe: DenoiseProfileStripe,
qx: Int,
qy: Int,
params: Params,
) {
GLES31.glUseProgram(fusedAccumulateProgram)
bindSampler(fusedAccumulateProgram, "uInput", 0, textureIds[0])
GLES31.glBindBufferBase(GLES31.GL_SHADER_STORAGE_BUFFER, 0, accumulatorBufferId)
GLES31.glUniform2i(
GLES31.glGetUniformLocation(fusedAccumulateProgram, "uImageSize"),
width,
height,
)
setStripeUniforms(fusedAccumulateProgram, stripe)
GLES31.glUniform2i(GLES31.glGetUniformLocation(fusedAccumulateProgram, "uQ"), qx, qy)
GLES31.glUniform1f(
GLES31.glGetUniformLocation(fusedAccumulateProgram, "uExpectedFineDistance"),
params.expectedFineDistance,
)
GLES31.glUniform1f(
GLES31.glGetUniformLocation(fusedAccumulateProgram, "uExpectedGuideDistance"),
params.expectedGuideDistance,
)
GLES31.glUniform1f(
GLES31.glGetUniformLocation(fusedAccumulateProgram, "uInverseBandwidth"),
params.inverseBandwidth,
)
GLES31.glUniform1f(
GLES31.glGetUniformLocation(fusedAccumulateProgram, "uCoarseGuideWeight"),
params.coarseGuideWeight,
)
GLES31.glUniform1f(
GLES31.glGetUniformLocation(fusedAccumulateProgram, "uCentralPixelWeight"),
params.centralPixelWeight,
)
dispatchImage(width, stripe.rowCount, "accumulate row=${stripe.rowOffset} q=($qx,$qy)")
GLES31.glMemoryBarrier(GLES31.GL_SHADER_STORAGE_BARRIER_BIT)
}
private fun dispatchFinish(input: Input, stripe: DenoiseProfileStripe, params: Params) {
GLES31.glUseProgram(finishProgram)
bindSampler(finishProgram, "uInput", 0, input.sourceTextureId)
GLES31.glBindBufferBase(GLES31.GL_SHADER_STORAGE_BUFFER, 0, accumulatorBufferId)
GLES31.glBindImageTexture(
1,
textureIds[1],
0,
false,
0,
GLES31.GL_WRITE_ONLY,
GLES30.GL_RGBA16F,
)
setCommonUniforms(finishProgram, input.width, input.height, params)
setStripeUniforms(finishProgram, stripe)
GLES31.glUniform1f(
GLES31.glGetUniformLocation(finishProgram, "uBias"),
params.bias - 0.5f * ln(params.scale),
)
GLES31.glUniform1f(
GLES31.glGetUniformLocation(finishProgram, "uDenoiseMix"),
DenoiseStrength.outputMix(params.strength),
)
dispatchImage(input.width, stripe.rowCount, "finish row=${stripe.rowOffset}")
}
private fun setCommonUniforms(program: Int, width: Int, height: Int, params: Params) {
GLES31.glUniform2i(GLES31.glGetUniformLocation(program, "uImageSize"), width, height)
GLES31.glUniform4fv(GLES31.glGetUniformLocation(program, "uA"), 1, params.aa, 0)
GLES31.glUniform4fv(GLES31.glGetUniformLocation(program, "uP"), 1, params.p, 0)
GLES31.glUniform4fv(GLES31.glGetUniformLocation(program, "uB"), 1, params.bb, 0)
GLES31.glUniform4fv(
GLES31.glGetUniformLocation(program, "uSignalScale"),
1,
params.signalScale,
0,
)
}
private fun setStripeUniforms(program: Int, stripe: DenoiseProfileStripe) {
GLES31.glUniform1i(
GLES31.glGetUniformLocation(program, "uStripeRowOffset"),
stripe.rowOffset,
)
GLES31.glUniform1i(
GLES31.glGetUniformLocation(program, "uStripeRowCount"),
stripe.rowCount,
)
}
private fun bindSampler(program: Int, name: String, unit: Int, textureId: Int) {
GLES31.glActiveTexture(GLES31.GL_TEXTURE0 + unit)
GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, textureId)
GLES31.glUniform1i(GLES31.glGetUniformLocation(program, name), unit)
}
private fun dispatchImage(width: Int, height: Int, label: String) {
GLES31.glDispatchCompute(
GlesComputeWorkGroup.imageGroupCount(width),
GlesComputeWorkGroup.imageGroupCount(height),
1,
)
GLES31.glMemoryBarrier(
GLES31.GL_SHADER_IMAGE_ACCESS_BARRIER_BIT or
GLES31.GL_TEXTURE_FETCH_BARRIER_BIT or
GLES31.GL_FRAMEBUFFER_BARRIER_BIT,
)
RawGlesProgram.logErrors("$TAG $label")
}
private fun releaseTextures() {
val live = textureIds.filter { it != 0 }.toIntArray()
if (live.isNotEmpty()) GLES30.glDeleteTextures(live.size, live, 0)
textureIds.fill(0)
textureWidth = 0
textureHeight = 0
}
private fun releaseAccumulator() {
if (accumulatorBufferId != 0) {
GLES31.glDeleteBuffers(1, intArrayOf(accumulatorBufferId), 0)
accumulatorBufferId = 0
}
accumulatorWidth = 0
accumulatorRows = 0
}
private companion object {
const val TAG = "DenoiseProfile"
const val GLES31_MIN_SSBO_BYTES = 128L * 1024L * 1024L
}
}
@@ -0,0 +1,276 @@
package com.hinnka.mycamera.raw
import com.hinnka.mycamera.processor.GlesComputeWorkGroup
/**
* OpenGL ES 3.1 compute port of darktable's denoiseprofile NLM path.
*
* 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 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 {
const val SEARCH_RADIUS = 5
const val PATCH_RADIUS = 1
// darktable's neutral bias setting makes finish_v2 the exact inverse when NLM leaves a
// transformed sample unchanged. This is required to keep sensor black and channel ratios.
const val BLACK_PRESERVING_BIAS = 0.0f
const val IMAGE_LOCAL_X = GlesComputeWorkGroup.IMAGE_TILE_SIZE
const val IMAGE_LOCAL_Y = GlesComputeWorkGroup.IMAGE_TILE_SIZE
private const val FUSED_TILE_X = IMAGE_LOCAL_X + SEARCH_RADIUS + 2 * PATCH_RADIUS
private const val FUSED_TILE_Y = IMAGE_LOCAL_Y + SEARCH_RADIUS + 2 * PATCH_RADIUS
private const val COMMON = """
precision highp float;
precision highp int;
vec4 readPixel(sampler2D image, ivec2 coord, ivec2 size) {
ivec2 c = clamp(coord, ivec2(0), size - ivec2(1));
return texelFetch(image, c, 0);
}
vec4 dtPow(vec4 a, vec4 b) {
return pow(a, b);
}
float fastMexp2(float x) {
const float i1 = float(0x3f800000u);
const float i2 = float(0x3f000000u);
float k0 = i1 + x * (i2 - i1);
uint bits = (k0 >= float(0x800000u)) ? uint(k0) : 0u;
return uintBitsToFloat(bits);
}
float ddirac(ivec2 q) {
return (q.x != 0 || q.y != 0) ? 1.0 : 0.0;
}
int pixelIndex(ivec2 coord, ivec2 size) {
return coord.y * size.x + coord.x;
}
"""
val PRECONDITION_V2 = """
#version 310 es
$COMMON
layout(local_size_x = $IMAGE_LOCAL_X, local_size_y = $IMAGE_LOCAL_Y) in;
layout(binding = 0) uniform highp sampler2D uInput;
layout(rgba16f, binding = 1) writeonly uniform highp image2D uOutput;
uniform ivec2 uImageSize;
uniform vec4 uA;
uniform vec4 uP;
uniform vec4 uB;
uniform vec4 uSignalScale;
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)
);
}
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()
val INIT = """
#version 310 es
$COMMON
layout(local_size_x = $IMAGE_LOCAL_X, local_size_y = $IMAGE_LOCAL_Y) in;
layout(std430, binding = 0) buffer AccuBuffer { vec4 u2[]; };
uniform ivec2 uImageSize;
uniform int uStripeRowCount;
void main() {
ivec2 stripeCoord = ivec2(gl_GlobalInvocationID.xy);
if (stripeCoord.x >= uImageSize.x || stripeCoord.y >= uStripeRowCount) return;
u2[stripeCoord.y * uImageSize.x + stripeCoord.x] = vec4(0.0);
}
""".trimIndent()
val FUSED_ACCU = """
#version 310 es
$COMMON
layout(local_size_x = $IMAGE_LOCAL_X, local_size_y = $IMAGE_LOCAL_Y) in;
layout(binding = 0) uniform highp sampler2D uInput;
layout(std430, binding = 0) buffer AccuBuffer { vec4 u2[]; };
shared vec2 sDistance[$FUSED_TILE_X * $FUSED_TILE_Y];
uniform ivec2 uImageSize;
uniform int uStripeRowOffset;
uniform int uStripeRowCount;
uniform ivec2 uQ;
uniform float uExpectedFineDistance;
uniform float uExpectedGuideDistance;
uniform float uInverseBandwidth;
uniform float uCoarseGuideWeight;
uniform float uCentralPixelWeight;
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 vec2(0.0);
vec4 p1 = readPixel(uInput, coord, uImageSize);
vec4 p2 = readPixel(uInput, shifted, uImageSize);
vec4 tmp = (p1 - p2) * (p1 - p2);
return vec2(tmp.x + tmp.y + tmp.z, tmp.a);
}
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) {
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(
center + ivec2(pi, pj),
ivec2(0),
uImageSize - ivec2(1)
);
distacc += tileDistance(patchCoord, groupOrigin, tileMin, tileWidth);
}
}
float patchPixels = float((2 * $PATCH_RADIUS + 1) * (2 * $PATCH_RADIUS + 1));
distacc += tileDistance(center, groupOrigin, tileMin, tileWidth) *
patchPixels * uCentralPixelWeight;
distacc /= 1.0 + uCentralPixelWeight;
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() {
ivec2 groupOrigin = ivec2(
int(gl_WorkGroupID.x) * $IMAGE_LOCAL_X,
uStripeRowOffset + int(gl_WorkGroupID.y) * $IMAGE_LOCAL_Y
);
ivec2 tileMin = min(ivec2(-$PATCH_RADIUS), -uQ - ivec2($PATCH_RADIUS));
ivec2 tileMax = max(ivec2($PATCH_RADIUS), -uQ + ivec2($PATCH_RADIUS));
int tileWidth = $IMAGE_LOCAL_X + tileMax.x - tileMin.x;
int tileHeight = $IMAGE_LOCAL_Y + tileMax.y - tileMin.y;
int tilePixels = tileWidth * tileHeight;
int localIndex = int(gl_LocalInvocationID.y) * $IMAGE_LOCAL_X +
int(gl_LocalInvocationID.x);
int localCount = $IMAGE_LOCAL_X * $IMAGE_LOCAL_Y;
for (int i = localIndex; i < tilePixels; i += localCount) {
int tileX = i - (i / tileWidth) * tileWidth;
int tileY = i / tileWidth;
ivec2 imageCoord = groupOrigin + ivec2(tileX, tileY) + tileMin;
imageCoord = clamp(imageCoord, ivec2(0), uImageSize - ivec2(1));
sDistance[i] = pixelDistance(imageCoord, uQ);
}
memoryBarrierShared();
barrier();
ivec2 stripeCoord = ivec2(gl_GlobalInvocationID.xy);
if (stripeCoord.x >= uImageSize.x || stripeCoord.y >= uStripeRowCount) return;
ivec2 coord = stripeCoord + ivec2(0, uStripeRowOffset);
ivec2 plusCoord = coord + uQ;
ivec2 minusCoord = coord - uQ;
bool plusInBounds = plusCoord.x >= 0 && plusCoord.x < uImageSize.x &&
plusCoord.y >= 0 && plusCoord.y < uImageSize.y;
bool minusInBounds = minusCoord.x >= 0 && minusCoord.x < uImageSize.x &&
minusCoord.y >= 0 && minusCoord.y < uImageSize.y;
float weight = plusInBounds ? patchWeight(coord, groupOrigin, tileMin, tileWidth) : 0.0;
float weightMinus = (minusInBounds && ddirac(uQ) > 0.0)
? patchWeight(minusCoord, groupOrigin, tileMin, tileWidth)
: 0.0;
vec4 u1Pq = plusInBounds ? readPixel(uInput, plusCoord, uImageSize) : vec4(0.0);
vec4 u1Mq = minusInBounds ? readPixel(uInput, minusCoord, uImageSize) : vec4(0.0);
vec4 accu = weight * u1Pq + weightMinus * u1Mq;
accu.a = weight + weightMinus;
int idx = stripeCoord.y * uImageSize.x + stripeCoord.x;
u2[idx] = u2[idx] + accu;
}
""".trimIndent()
val FINISH_V2 = """
#version 310 es
$COMMON
layout(local_size_x = $IMAGE_LOCAL_X, local_size_y = $IMAGE_LOCAL_Y) in;
layout(binding = 0) uniform highp sampler2D uInput;
layout(std430, binding = 0) readonly buffer AccuBuffer { vec4 u2[]; };
layout(rgba16f, binding = 1) writeonly uniform highp image2D uOutput;
uniform ivec2 uImageSize;
uniform int uStripeRowOffset;
uniform int uStripeRowCount;
uniform vec4 uA;
uniform vec4 uP;
uniform vec4 uB;
uniform float uBias;
uniform float uDenoiseMix;
uniform vec4 uSignalScale;
void main() {
ivec2 stripeCoord = ivec2(gl_GlobalInvocationID.xy);
if (stripeCoord.x >= uImageSize.x || stripeCoord.y >= uStripeRowCount) return;
ivec2 coord = stripeCoord + ivec2(0, uStripeRowOffset);
int idx = stripeCoord.y * uImageSize.x + stripeCoord.x;
vec4 accu = u2[idx];
vec4 original = readPixel(uInput, coord, uImageSize);
vec4 px = accu.a > 0.0 ? accu / accu.a : vec4(0.0);
vec4 delta = px * px + vec4(uBias);
vec4 denominator = 4.0 / (sqrt(uA) * (2.0 - uP));
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.rgb = mix(original.rgb, px.rgb, clamp(uDenoiseMix, 0.0, 1.0));
px.a = original.a;
imageStore(uOutput, coord, px);
}
""".trimIndent()
}
@@ -1,52 +0,0 @@
package com.hinnka.mycamera.raw
internal data class DenoiseProfileStripe(
val rowOffset: Int,
val rowCount: Int,
)
/**
* Bounds the NLM accumulator SSBO while keeping input sampling in full-image coordinates.
* A vec4 accumulator costs 16 bytes per output pixel.
*/
internal object DenoiseProfileStripePlanner {
const val ACCUMULATOR_BYTES_PER_PIXEL = 16L
const val PREFERRED_STRIPE_ROWS = 256
const val TARGET_BUFFER_BYTES = 64L * 1024L * 1024L
fun capacityRows(
imageWidth: Int,
imageHeight: Int,
maxShaderStorageBlockBytes: Long,
): Int {
if (imageWidth <= 0 || imageHeight <= 0 || maxShaderStorageBlockBytes <= 0L) return 0
val rowBytes = imageWidth.toLong() * ACCUMULATOR_BYTES_PER_PIXEL
if (rowBytes > maxShaderStorageBlockBytes) return 0
val byteBudget = minOf(maxShaderStorageBlockBytes, TARGET_BUFFER_BYTES)
val rowsByBudget = (byteBudget / rowBytes).coerceAtMost(Int.MAX_VALUE.toLong()).toInt()
val candidate = minOf(imageHeight, PREFERRED_STRIPE_ROWS, rowsByBudget)
val workgroupRows = DenoiseProfileShaders.IMAGE_LOCAL_Y
return if (candidate >= workgroupRows) {
candidate - candidate % workgroupRows
} else {
candidate
}
}
fun requiredBytes(imageWidth: Int, capacityRows: Int): Long =
imageWidth.toLong() * capacityRows.toLong() * ACCUMULATOR_BYTES_PER_PIXEL
fun plan(imageHeight: Int, capacityRows: Int): List<DenoiseProfileStripe> {
require(imageHeight > 0) { "Image height must be positive" }
require(capacityRows > 0) { "Stripe capacity must be positive" }
return buildList {
var rowOffset = 0
while (rowOffset < imageHeight) {
val rowCount = minOf(capacityRows, imageHeight - rowOffset)
add(DenoiseProfileStripe(rowOffset, rowCount))
rowOffset += rowCount
}
}
}
}
@@ -255,9 +255,12 @@ internal object MgcFullResolutionDenoise {
return false
}
} else {
val resolvedNoise = resolveUserAdjustmentCameraRgbNoise(metadata)
if (resolvedNoise == null) {
PLog.w(TAG, "MGC denoise skipped: RAW noise profile is unavailable")
val resolvedNoise = resolveUserAdjustmentCameraRgbNoise(metadata) ?: run {
PLog.e(
TAG,
"MGC denoise rejected missing RAW noise model: " +
"layout=${metadata.noiseProfileLayout}",
)
return false
}
rgbShot = resolvedNoise.shot
@@ -431,29 +434,49 @@ internal object MgcFullResolutionDenoise {
values.size == 128 && values.all { it.isFinite() && it >= 0f }
} == true
internal fun resolveUserAdjustmentCameraRgbNoise(
metadata: RawMetadata,
): NormalizedCameraRgbNoise? {
/** Canonical (R, G1, G2, B) normalized Bayer noise used by the MGC render tuning. */
internal data class CanonicalBayerNoise(
val shot: FloatArray,
val read: FloatArray,
)
/**
* The RAW's noise model in canonical Bayer order, or null when it has no usable one.
* RawMetadata.withFallbackNoiseProfile() supplies the app's calibrated fallback profile
* before rendering, so a render source always carries a model.
*/
internal fun resolveCanonicalBayerNoise(metadata: RawMetadata): CanonicalBayerNoise? {
val rawNoise = when (metadata.noiseProfileLayout) {
RawNoiseProfileLayout.CAMERA2_CFA ->
RawNoiseModel.fromCamera2NoiseProfile(metadata.channelNoiseProfile)
RawNoiseProfileLayout.DNG_RGB ->
RawNoiseModel.fromDngNoiseProfile(metadata.channelNoiseProfile)
RawNoiseProfileLayout.CANONICAL_BAYER -> {
if (metadata.channelNoiseProfile.size < 8) return null
RawNoiseModel.fromCanonicalBayerChannels(
shotNoise = FloatArray(4) { metadata.channelNoiseProfile[it * 2] },
readNoise = FloatArray(4) { metadata.channelNoiseProfile[it * 2 + 1] },
)
}
RawNoiseProfileLayout.CANONICAL_BAYER ->
if (metadata.channelNoiseProfile.size < 8) {
RawNoiseModel.EMPTY
} else {
RawNoiseModel.fromCanonicalBayerChannels(
shotNoise = FloatArray(4) { metadata.channelNoiseProfile[it * 2] },
readNoise = FloatArray(4) { metadata.channelNoiseProfile[it * 2 + 1] },
)
}
RawNoiseProfileLayout.NONE -> RawNoiseModel.EMPTY
}
val bayerShot = rawNoise.normalizedShotNoiseForShader(metadata.cfaPattern)
val bayerRead = rawNoise.normalizedReadNoiseForShader(metadata.cfaPattern)
if (bayerShot.none { it > 0f } && bayerRead.none { it > 0f }) return null
val shot = rawNoise.normalizedShotNoiseForShader(metadata.cfaPattern)
val read = rawNoise.normalizedReadNoiseForShader(metadata.cfaPattern)
val usable = shot.all { it.isFinite() && it >= 0f } &&
read.all { it.isFinite() && it >= 0f } &&
(shot.any { it > 0f } || read.any { it > 0f })
return if (usable) CanonicalBayerNoise(shot = shot, read = read) else null
}
internal fun resolveUserAdjustmentCameraRgbNoise(
metadata: RawMetadata,
): NormalizedCameraRgbNoise? {
val bayer = resolveCanonicalBayerNoise(metadata) ?: return null
return NormalizedCameraRgbNoise(
read = RawNoiseModel.bayerNoiseModelToRgb(bayerRead),
shot = RawNoiseModel.bayerNoiseModelToRgb(bayerShot),
read = RawNoiseModel.bayerNoiseModelToRgb(bayer.read),
shot = RawNoiseModel.bayerNoiseModelToRgb(bayer.shot),
)
}
@@ -24,7 +24,6 @@ import com.hinnka.mycamera.camera.AspectRatio
import com.hinnka.mycamera.camera.CameraState
import com.hinnka.mycamera.camera.RawBlackBorderCrop
import com.hinnka.mycamera.data.ContentRepository
import com.hinnka.mycamera.lut.ChromaDenoiseAlgorithm
import com.hinnka.mycamera.ml.SharedDepthEstimator
import com.hinnka.mycamera.processor.GlesGpuCompletion
import com.hinnka.mycamera.processor.GlesGpuScheduler
@@ -1396,8 +1395,6 @@ class RawDemosaicProcessor {
// Fullscreen passes own their shader programs and lifecycle.
private val fullscreenQuad = RawFullscreenQuad()
private val chromaDenoiseAlgorithm = ChromaDenoiseAlgorithm(fullscreenQuad)
private val denoiseProfileAlgorithm = DenoiseProfileAlgorithm()
private val profileGainTableAlgorithm = DngPhotonProfileGainTableAlgorithm()
private val filmicHighlightReconstructionAlgorithm =
DarktableFilmicHighlightReconstructionAlgorithm(fullscreenQuad)
@@ -2577,6 +2574,13 @@ class RawDemosaicProcessor {
)
PLog.i(TAG, "MGC tuning SNR restored $mgcTuningSnr")
}
// A source left without a usable noise model (no NoiseProfile and no evaluable selected
// profile) takes the app's calibrated fallback profile at its own ISO.
actualMetadata = actualMetadata?.withFallbackNoiseProfile(
ContentRepository.getInstance(context.applicationContext)
.rawNoiseProfileManager
.fallbackProfile,
)
val captureProfilePass = sceneExposureRequest != null ||
legacyAutoExposureRequest != null || captureProfilePreparationRequested
if (!captureProfilePass) {
@@ -3906,22 +3910,7 @@ class RawDemosaicProcessor {
globalOriginY = 0,
fullImageWidth = actualWidth,
fullImageHeight = actualHeight,
) ?: run {
val fallbackChromaTextureId = renderDefaultChromaDenoise(
sourceTextureId = demosaicTextureId,
width = actualWidth,
height = actualHeight,
metadata = actualMetadata,
chromaDenoiseValue = chromaDenoiseValue,
)
renderDenoiseProfilePass(
sourceTextureId = fallbackChromaTextureId,
width = actualWidth,
height = actualHeight,
metadata = actualMetadata,
denoiseValue = denoiseValue,
)
}
)
// AdobeCurve keeps BaselineExposure for the DNG SDK exposure ramp. HNCS consumes
// it through ColorCorrectAll's camera-domain inputEV; other linear engines retain
// the exact post-matrix 2^EV gain.
@@ -4571,22 +4560,7 @@ class RawDemosaicProcessor {
globalOriginY = working.top,
fullImageWidth = config.fullWidth,
fullImageHeight = config.fullHeight,
) ?: run {
val fallbackChromaTextureId = renderDefaultChromaDenoise(
sourceTextureId = demosaicTextureId,
width = workWidth,
height = workHeight,
metadata = config.metadata,
chromaDenoiseValue = config.chromaDenoiseValue,
)
renderDenoiseProfilePass(
sourceTextureId = fallbackChromaTextureId,
width = workWidth,
height = workHeight,
metadata = config.metadata,
denoiseValue = config.denoiseValue,
)
}
)
if (usesGuidedToneUpsample(config.outputGeometry, config.colorEngine)) {
val resolution = config.outputGeometry.mgcFinishResolution
check(working.left % resolution.processingPeriod == 0 &&
@@ -5431,48 +5405,6 @@ class RawDemosaicProcessor {
gfHeight = 0
}
private fun renderDefaultChromaDenoise(
sourceTextureId: Int,
width: Int,
height: Int,
metadata: RawMetadata,
chromaDenoiseValue: Float?,
): Int {
val strength = DenoiseStrength.clamp(chromaDenoiseValue)
if (strength <= 0f || width * height < 2) return sourceTextureId
if (linearOutputFramebufferId == 0 || linearOutputTextureId == 0) {
PLog.w(TAG, "RAW chroma denoise target is unavailable")
return sourceTextureId
}
setupNLMFramebuffers(width, height, setupLegacyAccumulator = false)
val profileGain =
(metadata.iso / 100f * metadata.postRawSensitivityBoost).coerceAtLeast(1f)
val noise = resolveChromaDenoiseNoiseModel(metadata, profileGain)
val output = chromaDenoiseAlgorithm.execute(
ChromaDenoiseAlgorithm.Input(
sourceTextureId = sourceTextureId,
guideFramebufferId = gfFboId[0],
guideTextureId = gfTexId[0],
outputFramebufferId = linearOutputFramebufferId,
outputTextureId = linearOutputTextureId,
width = width,
height = height,
strength = strength,
cameraRgbInput = true,
noiseModel = ChromaDenoiseAlgorithm.NoiseModel(
redSlope = noise.redSlope,
redOffset = noise.redOffset,
greenSlope = noise.greenSlope,
greenOffset = noise.greenOffset,
blueSlope = noise.blueSlope,
blueOffset = noise.blueOffset,
),
),
) ?: return sourceTextureId
return output.textureId
}
/**
* Executes the extracted non-AI MGC chroma pyramid and/or Pecan luma stage.
*
@@ -5493,7 +5425,7 @@ class RawDemosaicProcessor {
globalOriginY: Int,
fullImageWidth: Int,
fullImageHeight: Int,
): Int? {
): Int {
val lumaStrength = DenoiseStrength.clamp(denoiseValue)
val chromaStrength = DenoiseStrength.clamp(chromaDenoiseValue)
val lumaEnabled = lumaStrength > 0f
@@ -5502,20 +5434,20 @@ class RawDemosaicProcessor {
if (!enabled || width * height < 2) {
return sourceTextureId
}
if (!MgcFullResolutionDenoise.ensureInitialized(context)) {
PLog.w(TAG, "MGC RunFullResolutionDenoise is not initialized")
return null
}
// withMgcRenderTuning() evaluates the source's own noise model (single-frame Camera2
// profile, or a merged DNG's output model) at its measured green signal; a merged
// capture restores the SNR its merge produced. Without a noise model there is no SNR.
// withMgcRenderTuning() evaluates the source's noise model (its own, or the app's
// calibrated fallback profile when it carries none) at its measured green signal; a merged capture restores
// the SNR its merge produced. MGC is the only RAW denoiser: without an SNR coordinate
// the tuning curve cannot be indexed and the source is rendered undenoised.
val tuningSnr = metadata.mgcDenoiseTuningSnr ?: run {
PLog.w(
TAG,
"MGC USER_ADJUSTMENT has no tuning SNR: layout=${metadata.noiseProfileLayout} " +
"frames=${metadata.frameCount}",
)
return null
return sourceTextureId
}
check(MgcFullResolutionDenoise.ensureInitialized(context)) {
"MGC RunFullResolutionDenoise is not initialized"
}
setupNLMFramebuffers(
width,
@@ -5550,7 +5482,10 @@ class RawDemosaicProcessor {
)
nativeMs = (System.nanoTime() - nativeStartNs) / 1_000_000L
}
if (!succeeded) return null
check(succeeded) {
"MGC USER_ADJUSTMENT denoise failed: size=${width}x$height " +
"origin=($globalOriginX,$globalOriginY)"
}
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, 0)
denoiseTransfer.upload(gfTexId[0], width, height)
renderPassthroughToTexture(
@@ -5586,39 +5521,6 @@ class RawDemosaicProcessor {
}
}
/**
* 渲染 darktable denoiseprofile NLM 降噪。
*
* 管线: 未白平衡 camera RGB → variance-stabilizing transform → NLM accumulate
* → inverse transform → 未白平衡 camera RGB (gfFboId[1])。
*/
private fun renderDenoiseProfilePass(
sourceTextureId: Int,
width: Int,
height: Int,
metadata: RawMetadata,
denoiseValue: Float?,
): Int {
val strength = DenoiseStrength.clamp(denoiseValue)
if (strength <= 0f || width * height < 2) return sourceTextureId
val profileGain =
(metadata.iso / 100f * metadata.postRawSensitivityBoost).coerceAtLeast(1f)
val (noiseSlope, noiseOffset) =
resolveDenoiseProfileNoiseModel(metadata, profileGain)
val wb = demosaicCalculationWbGains(metadata)
return denoiseProfileAlgorithm.execute(
DenoiseProfileAlgorithm.Input(
sourceTextureId = sourceTextureId,
width = width,
height = height,
strength = strength,
noiseSlope = noiseSlope,
noiseOffset = noiseOffset,
adaptiveWhiteBalance = floatArrayOf(wb[0], 1f, wb[3]),
),
)?.textureId ?: sourceTextureId
}
private fun renderPassthroughToTexture(
sourceTextureId: Int,
width: Int,
@@ -7937,24 +7839,10 @@ class RawDemosaicProcessor {
// Photon does not carry MGC's unapplied gain; do not substitute display EV.
val signal = measuredSignal ?: 0.18f
val signalElapsedMs = (System.nanoTime() - signalStartNs) / 1_000_000L
val noiseModel = when (noiseProfileLayout) {
RawNoiseProfileLayout.CAMERA2_CFA ->
RawNoiseModel.fromCamera2NoiseProfile(channelNoiseProfile)
RawNoiseProfileLayout.CANONICAL_BAYER -> {
val shot = FloatArray(4) { channel ->
channelNoiseProfile.getOrElse(channel * 2) { 0f }
}
val read = FloatArray(4) { channel ->
channelNoiseProfile.getOrElse(channel * 2 + 1) { 0f }
}
RawNoiseModel.fromCanonicalBayerChannels(shot, read)
}
RawNoiseProfileLayout.DNG_RGB ->
RawNoiseModel.fromDngNoiseProfile(channelNoiseProfile)
RawNoiseProfileLayout.NONE -> RawNoiseModel.EMPTY
}
val shot = noiseModel.normalizedShotNoiseForShader(cfaPattern)
val read = noiseModel.normalizedReadNoiseForShader(cfaPattern)
// The same model MGC denoise consumes.
val noiseModel = MgcFullResolutionDenoise.resolveCanonicalBayerNoise(this)
val shot = noiseModel?.shot ?: FloatArray(4)
val read = noiseModel?.read ?: FloatArray(4)
val greenShot = 0.5f * (shot[1] + shot[2])
val greenRead = 0.5f * (read[1] + read[2])
val variance = greenShot * signal + greenRead
@@ -7971,6 +7859,7 @@ class RawDemosaicProcessor {
TAG,
"MGC RAW render tuning signal=$signal snr=$snr " +
"greenShot=$greenShot greenRead=$greenRead " +
"noiseModel=${if (noiseModel == null) "NONE" else noiseProfileLayout.name} " +
"singleFrameDenoise=$needsSingleFrameDenoiseSnr " +
"sharpen=$needsSharpenSnr " +
"signalSource=${if (measuredSignal != null) "NATIVE_OMP" else "MGC_MISSING_MEAN"} " +
@@ -9424,7 +9313,6 @@ class RawDemosaicProcessor {
raisrTransfer.release()
outputPass.release()
hdrReferencePass.release()
chromaDenoiseAlgorithm.release()
filmicHighlightReconstructionAlgorithm.release()
demosaicNoisePropagationCalibrator.release()
@@ -9439,7 +9327,6 @@ class RawDemosaicProcessor {
linearUintToFloatPass.release()
linearFloatToUintPass.release()
linearRgbExpandPass.release()
denoiseProfileAlgorithm.release()
releaseDenoiseProfileFramebuffers()
if (exportedStackTextureIds.isNotEmpty()) {
@@ -10,6 +10,7 @@ import android.util.Log
import android.util.Rational
import com.hinnka.mycamera.camera.readMetadataOrNull
import com.hinnka.mycamera.camera.readMetadataOrThrow
import com.hinnka.mycamera.processor.CalibratedRawNoiseProfile
import com.hinnka.mycamera.processor.RawNoiseModel
import com.hinnka.mycamera.processor.RawNoiseProfileSelection
import com.hinnka.mycamera.utils.PLog
@@ -236,6 +237,36 @@ data class RawMetadata(
}
}
/**
* Keeps a usable source noise model; otherwise evaluates [profile] at this capture's ISO
* (its lowest compatible ISO when the source reports none).
*/
fun withFallbackNoiseProfile(profile: CalibratedRawNoiseProfile): RawMetadata {
if (hasUsableSourceNoiseProfile()) return this
val sensitivity = iso.takeIf { it > 0 } ?: profile.minimumCompatibleSensitivity
// The fallback must evaluate from ISO alone; a profile that needs camera gain limits
// cannot serve as the fallback.
val model = checkNotNull(
profile.evaluate(
sensitivity = sensitivity,
minimumSensitivityIso = minimumSensitivityIso,
maximumAnalogSensitivityIso = maxAnalogSensitivity,
),
) {
"RAW fallback noise profile ${profile.id} did not evaluate: iso=$sensitivity " +
"minIso=$minimumSensitivityIso maxAnalogIso=$maxAnalogSensitivity"
}
PLog.i(
TAG,
"RAW source has no usable noise model ($noiseProfileLayout); " +
"using fallback profile ${profile.id} at iso=$sensitivity",
)
return copy(
channelNoiseProfile = model.canonicalChannelPairs(cfaPattern),
noiseProfileLayout = RawNoiseProfileLayout.CANONICAL_BAYER,
)
}
private fun hasUsableSourceNoiseProfile(): Boolean = when (noiseProfileLayout) {
RawNoiseProfileLayout.CAMERA2_CFA,
RawNoiseProfileLayout.CANONICAL_BAYER ->
@@ -48,6 +48,10 @@ class RawNoiseProfileManager(context: Context) {
}
}
/** Calibrated profile used for a RAW whose source carries no usable noise model. */
val fallbackProfile: CalibratedRawNoiseProfile
get() = systemCamera2Selection.fallbackProfile
fun getAvailableProfiles(): List<RawNoiseProfileInfo> =
(BUILT_IN_PROFILES + customImportManager.getCustomRawNoiseProfiles())
.distinctBy(RawNoiseProfileInfo::id)
@@ -252,32 +252,6 @@ class RawNoiseProfileSelectionTest {
assertEquals(input, restored, 1e-6f)
}
@Test
fun fiftyMegapixelNlmAccumulatorIsSplitIntoBoundedStripes() {
val width = 6144
val height = 8192
val maxSsboBytes = 128L * 1024L * 1024L
val capacityRows = DenoiseProfileStripePlanner.capacityRows(
imageWidth = width,
imageHeight = height,
maxShaderStorageBlockBytes = maxSsboBytes,
)
val stripes = DenoiseProfileStripePlanner.plan(height, capacityRows)
assertEquals(256, capacityRows)
assertEquals(32, stripes.size)
assertEquals(
24L * 1024L * 1024L,
DenoiseProfileStripePlanner.requiredBytes(width, capacityRows),
)
assertEquals(0, stripes.first().rowOffset)
assertEquals(height, stripes.last().rowOffset + stripes.last().rowCount)
stripes.zipWithNext().forEach { (left, right) ->
assertEquals(left.rowOffset + left.rowCount, right.rowOffset)
}
}
@Test
fun nlmStripeShaderKeepsSamplingInFullImageCoordinates() {
val shader = DenoiseProfileShaders.FUSED_ACCU + DenoiseProfileShaders.FINISH_V2