feat: wavelet chroma denoise

This commit is contained in:
Hinnka committed 2026-06-27 11:12:09 +08:00
1 parent e2cb873193
commit a4b97fe38c
3 files changed
+793 -116

No files matched your search

@@ -1,71 +1,192 @@
package com.hinnka.mycamera.lut
/**
* Shared BM3D shader snippets used by bitmap chroma noise reduction.
* Shared shader snippets used by bitmap/RAW chroma noise reduction.
*
* Only pass0 is kept here: it is a cross-bilateral chroma filter guided by luma,
* and does not change the existing denoiseprofile luminance/noise-reduction path.
* The pipeline is chroma-only multi-pass a trous wavelet denoise:
* RGB -> YCbCr, repeated B-spline decompose, reverse soft-threshold synthesize,
* then original luma + filtered chroma back to RGB.
*/
object ChromaDenoiseShaders {
const val SIGMA_STRENGTH_AT_SLIDER_ONE = 6.0f
const val WAVELET_BANDS = 4
val PASS_CHROMA_DENOISE = """
#version 300 es
private val COMMON = """
precision highp float;
in vec2 vTexCoord;
out vec4 fragColor;
uniform sampler2D uInputTexture;
uniform vec2 uTexelSize;
uniform mat4 uTexMatrix;
uniform float uH;
uniform vec2 uNoiseModel;
float safeInvSq(float v) {
return 1.0 / max(v * v, 1e-6);
}
vec3 rgb2ycbcr(vec3 rgb) {
float y = dot(rgb, vec3(0.2126, 0.7152, 0.0722));
return vec3(y,
dot(rgb, vec3(-0.169, -0.331, 0.5 )) + 0.5,
dot(rgb, vec3( 0.5, -0.419, -0.081 )) + 0.5);
dot(rgb, vec3(-0.169, -0.331, 0.5 )),
dot(rgb, vec3( 0.5, -0.419, -0.081 )));
}
vec3 ycbcr2rgb(vec3 yuv) {
float cb = yuv.y - 0.5, cr = yuv.z - 0.5;
return vec3(yuv.x + 1.402 * cr,
yuv.x - 0.3441 * cb - 0.7141 * cr,
yuv.x + 1.772 * cb);
vec3 ycbcr2rgb(vec3 ycc) {
return vec3(ycc.x + 1.402 * ycc.z,
ycc.x - 0.3441 * ycc.y - 0.7141 * ycc.z,
ycc.x + 1.772 * ycc.y);
}
float noiseSigmaForLuma(float luma) {
return sqrt(max(uNoiseModel.x * max(luma, 0.0) + uNoiseModel.y, 1e-10));
float noiseSigmaForLuma(vec2 noiseModel, float luma) {
return sqrt(max(noiseModel.x * max(luma, 0.0) + noiseModel.y, 1e-10));
}
float bSplineTap(int offset) {
int a = offset < 0 ? -offset : offset;
if (a == 0) return 0.375;
if (a == 1) return 0.25;
return 0.0625;
}
vec2 softThreshold(vec2 value, float threshold) {
return sign(value) * max(abs(value) - vec2(threshold), vec2(0.0));
}
float chromaProtection(float chromaLen, float noiseSigma) {
return smoothstep(noiseSigma * 3.5 + 0.008, noiseSigma * 10.0 + 0.035, chromaLen);
}
"""
val PREPARE_YCBCR = """
#version 300 es
$COMMON
uniform sampler2D uInputTexture;
void main() {
vec3 oriRgb = texture(uInputTexture, vTexCoord).rgb;
if (uH <= 0.00001) { fragColor = vec4(oriRgb, 1.0); return; }
vec4 color = texture(uInputTexture, vTexCoord);
fragColor = vec4(rgb2ycbcr(color.rgb), color.a);
}
""".trimIndent()
vec3 yuv = rgb2ycbcr(oriRgb);
vec2 sumUV = vec2(0.0);
val WAVELET_DECOMPOSE = """
#version 300 es
$COMMON
uniform sampler2D uInputTexture;
uniform vec2 uTexelSize;
uniform vec2 uNoiseModel;
uniform float uH;
uniform int uScale;
uniform int uOutputMode;
vec4 sampleYcc(vec2 uv) {
return texture(uInputTexture, uv);
}
vec4 coarseForCenter(vec4 centerYcc) {
vec4 sum = vec4(0.0);
float sumW = 0.0;
float strength01 = clamp(uH / 6.0, 0.0, 1.0);
float noiseSigma = noiseSigmaForLuma(uNoiseModel, centerYcc.x);
float localH = max(uH * noiseSigma, 1e-5);
float scaleF = float(uScale);
float lumaSigma = max(
noiseSigma * mix(1.55, 4.2, strength01) + 0.003 + log2(scaleF + 1.0) * 0.0015,
localH * mix(0.42, 0.72, smoothstep(1.0, 8.0, scaleF))
);
float invLumaSigma2 = safeInvSq(lumaSigma);
float stepScale = 6.5;
float localH = uH * noiseSigmaForLuma(yuv.x);
float invChromaH2 = 1.0 / max((localH * 1.5) * (localH * 1.5), 1e-6);
float invLumaH2 = 1.0 / max((localH * 0.5) * (localH * 0.5), 1e-6);
for (int x = -2; x <= 2; x++) {
for (int y = -2; y <= 2; y++) {
vec2 ofs = vec2(float(x), float(y)) * uTexelSize * stepScale;
vec3 sYuv = rgb2ycbcr(texture(uInputTexture, vTexCoord + ofs).rgb);
float wS = exp(-float(x * x + y * y) * 0.25);
float dL = sYuv.x - yuv.x;
vec2 dC = sYuv.yz - yuv.yz;
float wR = exp(-(dL * dL) * invLumaH2 - dot(dC, dC) * invChromaH2);
float w = wS * wR;
if (x == 0 && y == 0) w = max(w, 0.1);
sumUV += sYuv.yz * w;
for (int y = -2; y <= 2; y++) {
for (int x = -2; x <= 2; x++) {
vec2 ofs = vec2(float(x), float(y)) * uTexelSize * scaleF;
vec4 sYcc = sampleYcc(vTexCoord + ofs);
float kernel = bSplineTap(x) * bSplineTap(y);
float dL = sYcc.x - centerYcc.x;
float guide = exp(-(dL * dL) * invLumaSigma2);
float w = kernel * guide;
sum += sYcc * w;
sumW += w;
}
}
yuv.yz = sumUV / sumW;
fragColor = vec4(ycbcr2rgb(yuv), 1.0);
vec4 coarse = sum / max(sumW, 1e-6);
coarse.a = centerYcc.a;
return coarse;
}
void main() {
vec4 centerYcc = sampleYcc(vTexCoord);
vec4 coarse = coarseForCenter(centerYcc);
fragColor = (uOutputMode == 0) ? coarse : vec4(centerYcc.rgb - coarse.rgb, centerYcc.a);
}
""".trimIndent()
val WAVELET_SYNTHESIZE = """
#version 300 es
$COMMON
uniform sampler2D uCoarseTexture;
uniform sampler2D uDetailTexture;
uniform vec2 uTexelSize;
uniform vec2 uNoiseModel;
uniform float uH;
uniform float uBandScale;
float flatLumaWeight(float centerLuma, float noiseSigma) {
float left = texture(uCoarseTexture, vTexCoord + vec2(-uTexelSize.x, 0.0)).x;
float right = texture(uCoarseTexture, vTexCoord + vec2(uTexelSize.x, 0.0)).x;
float up = texture(uCoarseTexture, vTexCoord + vec2(0.0, -uTexelSize.y)).x;
float down = texture(uCoarseTexture, vTexCoord + vec2(0.0, uTexelSize.y)).x;
float localGradient = max(max(abs(centerLuma - left), abs(centerLuma - right)),
max(abs(centerLuma - up), abs(centerLuma - down)));
float edgeThreshold = noiseSigma * 2.2 + 0.006;
return 1.0 - smoothstep(edgeThreshold * 0.65, edgeThreshold * 2.2, localGradient);
}
float bandThreshold(float luma, vec2 coarseChroma) {
float noiseSigma = noiseSigmaForLuma(uNoiseModel, luma);
float strength01 = clamp(uH / 6.0, 0.0, 1.0);
float localH = max(uH * noiseSigma, 1e-5);
float shadowWeight = (1.0 - smoothstep(0.035, 0.24, luma)) *
smoothstep(0.004, 0.055, localH);
float flatWeight = flatLumaWeight(luma, noiseSigma);
float stableChroma = chromaProtection(length(coarseChroma), noiseSigma);
float base = noiseSigma * mix(0.22, 1.28, strength01) + strength01 * 0.0025;
float areaBoost = 1.0 + 1.2 * flatWeight + 0.75 * shadowWeight;
float protectionScale = mix(1.0, 0.45, stableChroma * (1.0 - shadowWeight * 0.45));
return base * uBandScale * areaBoost * protectionScale;
}
void main() {
vec4 coarse = texture(uCoarseTexture, vTexCoord);
vec4 detail = texture(uDetailTexture, vTexCoord);
vec2 keptChroma = softThreshold(detail.yz, bandThreshold(coarse.x, coarse.yz));
fragColor = vec4(coarse.x, coarse.yz + keptChroma, coarse.a);
}
""".trimIndent()
val COMPOSE_RGB = """
#version 300 es
$COMMON
uniform sampler2D uOriginalTexture;
uniform sampler2D uChromaTexture;
uniform vec2 uNoiseModel;
uniform float uH;
void main() {
vec4 original = texture(uOriginalTexture, vTexCoord);
vec3 originalYcc = rgb2ycbcr(original.rgb);
vec2 originalChroma = originalYcc.yz;
vec2 filteredChroma = texture(uChromaTexture, vTexCoord).yz;
float noiseSigma = noiseSigmaForLuma(uNoiseModel, originalYcc.x);
float strength01 = clamp(uH / 6.0, 0.0, 1.0);
float localH = max(uH * noiseSigma, 1e-5);
float shadowWeight = (1.0 - smoothstep(0.035, 0.24, originalYcc.x)) *
smoothstep(0.004, 0.055, localH);
float chromaLen = length(originalChroma);
float stableChroma = chromaProtection(chromaLen, noiseSigma);
float maxDelta = noiseSigma * mix(2.4, 7.5, strength01) * mix(1.0, 1.65, shadowWeight) +
chromaLen * mix(0.10, 0.30, strength01);
vec2 delta = filteredChroma - originalChroma;
filteredChroma = originalChroma + delta * min(1.0, maxDelta / max(length(delta), 1e-6));
float filteredLen = length(filteredChroma);
float minProtectedLen = chromaLen * (1.0 - mix(0.08, 0.22, strength01) * stableChroma);
if (stableChroma > 0.001 && filteredLen < minProtectedLen) {
filteredChroma *= minProtectedLen / max(filteredLen, 1e-6);
}
vec3 rgb = ycbcr2rgb(vec3(originalYcc.x, filteredChroma));
fragColor = vec4(rgb, original.a);
}
""".trimIndent()
}
@@ -80,11 +80,16 @@ class LutImageProcessor {
private var bitmapDenoiseNlmFinishProgram = 0
private var bitmapDenoisePassthroughProgram = 0
private var naturalLightSrgbToLinearProgram = 0
private var bitmapChromaDenoiseProgram = 0
private var bitmapChromaPrepareProgram = 0
private var bitmapChromaDecomposeProgram = 0
private var bitmapChromaSynthesizeProgram = 0
private var bitmapChromaComposeProgram = 0
private var lutSharpenProgram = 0
private val bitmapDenoiseTexId = IntArray(2)
private val bitmapDenoiseFboId = IntArray(2)
private val bitmapChromaDetailTexId = IntArray(ChromaDenoiseShaders.WAVELET_BANDS)
private val bitmapChromaDetailFboId = IntArray(ChromaDenoiseShaders.WAVELET_BANDS)
private var bitmapDenoiseNlmU2BufferId = 0
private var bitmapDenoiseNlmBufferPixels = 0
private var bitmapDenoiseWidth = 0
@@ -1487,7 +1492,10 @@ class LutImageProcessor {
bitmapDenoiseNlmFinishProgram = compileComputeProgram(DenoiseProfileShaders.FINISH_V2, "BitmapDenoise_NLM_FinishV2")
bitmapDenoisePassthroughProgram = createFragmentProgram(IMAGE_VERTEX_SHADER, TEXTURE_PASSTHROUGH_SHADER, "BitmapDenoise_Passthrough")
naturalLightSrgbToLinearProgram = createFragmentProgram(IMAGE_VERTEX_SHADER, SRGB_TO_LINEAR_SHADER, "NaturalLight_SrgbToLinear")
bitmapChromaDenoiseProgram = createFragmentProgram(IMAGE_VERTEX_SHADER, ChromaDenoiseShaders.PASS_CHROMA_DENOISE, "BitmapChromaDenoise_BM3DPass0")
bitmapChromaPrepareProgram = createFragmentProgram(IMAGE_VERTEX_SHADER, ChromaDenoiseShaders.PREPARE_YCBCR, "BitmapChromaDenoise_PrepareYcc")
bitmapChromaDecomposeProgram = createFragmentProgram(IMAGE_VERTEX_SHADER, ChromaDenoiseShaders.WAVELET_DECOMPOSE, "BitmapChromaDenoise_Decompose")
bitmapChromaSynthesizeProgram = createFragmentProgram(IMAGE_VERTEX_SHADER, ChromaDenoiseShaders.WAVELET_SYNTHESIZE, "BitmapChromaDenoise_Synthesize")
bitmapChromaComposeProgram = createFragmentProgram(IMAGE_VERTEX_SHADER, ChromaDenoiseShaders.COMPOSE_RGB, "BitmapChromaDenoise_ComposeRgb")
lutSharpenProgram = createFragmentProgram(IMAGE_VERTEX_SHADER, RawShaders.SHARPEN_FRAGMENT_SHADER, "LutSharpen")
PLog.d(
TAG,
@@ -1495,7 +1503,8 @@ class LutImageProcessor {
"init=$bitmapDenoiseNlmInitProgram fusedAccu=$bitmapDenoiseNlmFusedAccuProgram " +
"finish=$bitmapDenoiseNlmFinishProgram " +
"pass=$bitmapDenoisePassthroughProgram linearize=$naturalLightSrgbToLinearProgram " +
"chroma=$bitmapChromaDenoiseProgram " +
"chromaPrepare=$bitmapChromaPrepareProgram chromaDecompose=$bitmapChromaDecomposeProgram " +
"chromaSynthesize=$bitmapChromaSynthesizeProgram chromaCompose=$bitmapChromaComposeProgram " +
"sharpen=$lutSharpenProgram"
)
}
@@ -1534,7 +1543,12 @@ class LutImageProcessor {
}
private fun setupBitmapDenoiseFramebuffers(width: Int, height: Int) {
if (bitmapDenoiseWidth == width && bitmapDenoiseHeight == height && bitmapDenoiseTexId[0] != 0) return
if (
bitmapDenoiseWidth == width &&
bitmapDenoiseHeight == height &&
bitmapDenoiseTexId[0] != 0 &&
bitmapChromaDetailTexId[0] != 0
) return
bitmapDenoiseWidth = width
bitmapDenoiseHeight = height
@@ -1542,6 +1556,12 @@ class LutImageProcessor {
if (bitmapDenoiseTexId[i] != 0) GLES30.glDeleteTextures(1, intArrayOf(bitmapDenoiseTexId[i]), 0)
if (bitmapDenoiseFboId[i] != 0) GLES30.glDeleteFramebuffers(1, intArrayOf(bitmapDenoiseFboId[i]), 0)
}
for (i in bitmapChromaDetailTexId.indices) {
if (bitmapChromaDetailTexId[i] != 0) GLES30.glDeleteTextures(1, intArrayOf(bitmapChromaDetailTexId[i]), 0)
if (bitmapChromaDetailFboId[i] != 0) GLES30.glDeleteFramebuffers(1, intArrayOf(bitmapChromaDetailFboId[i]), 0)
bitmapChromaDetailTexId[i] = 0
bitmapChromaDetailFboId[i] = 0
}
for (i in 0..1) {
val t = IntArray(1)
@@ -1570,6 +1590,33 @@ class LutImageProcessor {
PLog.e(TAG, "Bitmap denoiseprofile FBO $i incomplete: $status")
}
}
for (i in bitmapChromaDetailTexId.indices) {
val t = IntArray(1)
val f = IntArray(1)
GLES30.glGenTextures(1, t, 0)
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, t[0])
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.glGenFramebuffers(1, f, 0)
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, f[0])
GLES30.glFramebufferTexture2D(
GLES30.GL_FRAMEBUFFER,
GLES30.GL_COLOR_ATTACHMENT0,
GLES30.GL_TEXTURE_2D,
t[0],
0
)
bitmapChromaDetailTexId[i] = t[0]
bitmapChromaDetailFboId[i] = f[0]
val status = GLES30.glCheckFramebufferStatus(GLES30.GL_FRAMEBUFFER)
if (status != GLES30.GL_FRAMEBUFFER_COMPLETE) {
PLog.e(TAG, "Bitmap chroma wavelet detail FBO $i incomplete: $status")
}
}
setupBitmapDenoiseResources(width, height)
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, 0)
}
@@ -1615,41 +1662,208 @@ class LutImageProcessor {
val strength = chromaNoiseReduction.coerceIn(0f, 1f)
val target = targetIndex.coerceIn(0, bitmapDenoiseTexId.lastIndex)
if (strength <= 0f || bitmapChromaDenoiseProgram == 0) {
if (strength <= 0f || !isBitmapChromaDenoiseReady()) {
renderTexturePassthrough(sourceTextureId, bitmapDenoiseFboId[target], width, height)
return
}
val identityMatrix = FloatArray(16)
android.opengl.Matrix.setIdentityM(identityMatrix, 0)
val h = strength * strength * ChromaDenoiseShaders.SIGMA_STRENGTH_AT_SLIDER_ONE
val noiseA = BITMAP_DENOISE_A * 2f
val noiseB = BITMAP_DENOISE_B * 2f
val initialIndex = 1 - target
var currentIndex = initialIndex
var nextIndex = target
GLES30.glUseProgram(bitmapChromaDenoiseProgram)
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, bitmapDenoiseFboId[target])
renderBitmapChromaPrepare(sourceTextureId, bitmapDenoiseFboId[currentIndex], width, height)
for (band in 0 until ChromaDenoiseShaders.WAVELET_BANDS) {
val scale = 1 shl band
val currentTextureId = bitmapDenoiseTexId[currentIndex]
renderBitmapChromaDecompose(
currentTextureId,
bitmapChromaDetailFboId[band],
width,
height,
h,
noiseA,
noiseB,
scale,
outputMode = 1
)
renderBitmapChromaDecompose(
currentTextureId,
bitmapDenoiseFboId[nextIndex],
width,
height,
h,
noiseA,
noiseB,
scale,
outputMode = 0
)
currentIndex = nextIndex
nextIndex = 1 - currentIndex
}
for (band in ChromaDenoiseShaders.WAVELET_BANDS - 1 downTo 0) {
renderBitmapChromaSynthesize(
bitmapDenoiseTexId[currentIndex],
bitmapChromaDetailTexId[band],
bitmapDenoiseFboId[nextIndex],
width,
height,
h,
noiseA,
noiseB,
chromaWaveletBandScale(band)
)
currentIndex = nextIndex
nextIndex = 1 - currentIndex
}
if (currentIndex == target) {
val spareIndex = 1 - target
renderTexturePassthrough(bitmapDenoiseTexId[currentIndex], bitmapDenoiseFboId[spareIndex], width, height)
currentIndex = spareIndex
}
renderBitmapChromaCompose(
sourceTextureId,
bitmapDenoiseTexId[currentIndex],
bitmapDenoiseFboId[target],
width,
height,
h,
noiseA,
noiseB
)
checkGlError("renderBitmapChromaDenoise")
}
private fun isBitmapChromaDenoiseReady(): Boolean {
return bitmapChromaPrepareProgram != 0 &&
bitmapChromaDecomposeProgram != 0 &&
bitmapChromaSynthesizeProgram != 0 &&
bitmapChromaComposeProgram != 0
}
private fun renderBitmapChromaPrepare(sourceTextureId: Int, targetFboId: Int, width: Int, height: Int) {
GLES30.glUseProgram(bitmapChromaPrepareProgram)
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, targetFboId)
GLES30.glViewport(0, 0, width, height)
GLES30.glActiveTexture(GLES30.GL_TEXTURE0)
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, sourceTextureId)
GLES30.glUniform1i(GLES30.glGetUniformLocation(bitmapChromaDenoiseProgram, "uInputTexture"), 0)
GLES30.glUniform1i(GLES30.glGetUniformLocation(bitmapChromaPrepareProgram, "uInputTexture"), 0)
setBitmapFragmentMatrix(bitmapChromaPrepareProgram)
drawQuad(bitmapChromaPrepareProgram)
}
private fun renderBitmapChromaDecompose(
sourceTextureId: Int,
targetFboId: Int,
width: Int,
height: Int,
h: Float,
noiseA: Float,
noiseB: Float,
scale: Int,
outputMode: Int
) {
GLES30.glUseProgram(bitmapChromaDecomposeProgram)
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, targetFboId)
GLES30.glViewport(0, 0, width, height)
GLES30.glActiveTexture(GLES30.GL_TEXTURE0)
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, sourceTextureId)
GLES30.glUniform1i(GLES30.glGetUniformLocation(bitmapChromaDecomposeProgram, "uInputTexture"), 0)
GLES30.glUniform2f(
GLES30.glGetUniformLocation(bitmapChromaDenoiseProgram, "uTexelSize"),
GLES30.glGetUniformLocation(bitmapChromaDecomposeProgram, "uTexelSize"),
1.0f / width,
1.0f / height
)
GLES30.glUniformMatrix4fv(
GLES30.glGetUniformLocation(bitmapChromaDenoiseProgram, "uMVPMatrix"),
1,
false,
identityMatrix,
0
)
GLES30.glUniform1f(GLES30.glGetUniformLocation(bitmapChromaDenoiseProgram, "uH"), h)
GLES30.glUniform1f(GLES30.glGetUniformLocation(bitmapChromaDecomposeProgram, "uH"), h)
GLES30.glUniform2f(
GLES30.glGetUniformLocation(bitmapChromaDenoiseProgram, "uNoiseModel"),
BITMAP_DENOISE_A * 2f,
BITMAP_DENOISE_B * 2f
GLES30.glGetUniformLocation(bitmapChromaDecomposeProgram, "uNoiseModel"),
noiseA,
noiseB
)
drawQuad(bitmapChromaDenoiseProgram)
checkGlError("renderBitmapChromaDenoise")
GLES30.glUniform1i(GLES30.glGetUniformLocation(bitmapChromaDecomposeProgram, "uScale"), scale)
GLES30.glUniform1i(GLES30.glGetUniformLocation(bitmapChromaDecomposeProgram, "uOutputMode"), outputMode)
setBitmapFragmentMatrix(bitmapChromaDecomposeProgram)
drawQuad(bitmapChromaDecomposeProgram)
}
private fun renderBitmapChromaSynthesize(
coarseTextureId: Int,
detailTextureId: Int,
targetFboId: Int,
width: Int,
height: Int,
h: Float,
noiseA: Float,
noiseB: Float,
bandScale: Float
) {
GLES30.glUseProgram(bitmapChromaSynthesizeProgram)
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, targetFboId)
GLES30.glViewport(0, 0, width, height)
GLES30.glActiveTexture(GLES30.GL_TEXTURE0)
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, coarseTextureId)
GLES30.glUniform1i(GLES30.glGetUniformLocation(bitmapChromaSynthesizeProgram, "uCoarseTexture"), 0)
GLES30.glActiveTexture(GLES30.GL_TEXTURE1)
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, detailTextureId)
GLES30.glUniform1i(GLES30.glGetUniformLocation(bitmapChromaSynthesizeProgram, "uDetailTexture"), 1)
GLES30.glUniform2f(
GLES30.glGetUniformLocation(bitmapChromaSynthesizeProgram, "uTexelSize"),
1.0f / width,
1.0f / height
)
GLES30.glUniform1f(GLES30.glGetUniformLocation(bitmapChromaSynthesizeProgram, "uH"), h)
GLES30.glUniform2f(GLES30.glGetUniformLocation(bitmapChromaSynthesizeProgram, "uNoiseModel"), noiseA, noiseB)
GLES30.glUniform1f(GLES30.glGetUniformLocation(bitmapChromaSynthesizeProgram, "uBandScale"), bandScale)
setBitmapFragmentMatrix(bitmapChromaSynthesizeProgram)
drawQuad(bitmapChromaSynthesizeProgram)
}
private fun renderBitmapChromaCompose(
originalTextureId: Int,
chromaTextureId: Int,
targetFboId: Int,
width: Int,
height: Int,
h: Float,
noiseA: Float,
noiseB: Float
) {
GLES30.glUseProgram(bitmapChromaComposeProgram)
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, targetFboId)
GLES30.glViewport(0, 0, width, height)
GLES30.glActiveTexture(GLES30.GL_TEXTURE0)
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, originalTextureId)
GLES30.glUniform1i(GLES30.glGetUniformLocation(bitmapChromaComposeProgram, "uOriginalTexture"), 0)
GLES30.glActiveTexture(GLES30.GL_TEXTURE1)
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, chromaTextureId)
GLES30.glUniform1i(GLES30.glGetUniformLocation(bitmapChromaComposeProgram, "uChromaTexture"), 1)
GLES30.glUniform1f(GLES30.glGetUniformLocation(bitmapChromaComposeProgram, "uH"), h)
GLES30.glUniform2f(GLES30.glGetUniformLocation(bitmapChromaComposeProgram, "uNoiseModel"), noiseA, noiseB)
setBitmapFragmentMatrix(bitmapChromaComposeProgram)
drawQuad(bitmapChromaComposeProgram)
}
private fun setBitmapFragmentMatrix(program: Int) {
val matrixLocation = GLES30.glGetUniformLocation(program, "uMVPMatrix")
if (matrixLocation < 0) return
val identityMatrix = FloatArray(16)
android.opengl.Matrix.setIdentityM(identityMatrix, 0)
GLES30.glUniformMatrix4fv(matrixLocation, 1, false, identityMatrix, 0)
}
private fun chromaWaveletBandScale(band: Int): Float {
return when (band) {
0 -> 0.58f
1 -> 0.82f
2 -> 1.08f
else -> 1.35f
}
}
private fun resolvePreToneMapChromaDenoise(
@@ -2681,7 +2895,10 @@ class LutImageProcessor {
if (bitmapDenoiseNlmFinishProgram != 0) GLES31.glDeleteProgram(bitmapDenoiseNlmFinishProgram)
if (bitmapDenoisePassthroughProgram != 0) GLES30.glDeleteProgram(bitmapDenoisePassthroughProgram)
if (naturalLightSrgbToLinearProgram != 0) GLES30.glDeleteProgram(naturalLightSrgbToLinearProgram)
if (bitmapChromaDenoiseProgram != 0) GLES30.glDeleteProgram(bitmapChromaDenoiseProgram)
if (bitmapChromaPrepareProgram != 0) GLES30.glDeleteProgram(bitmapChromaPrepareProgram)
if (bitmapChromaDecomposeProgram != 0) GLES30.glDeleteProgram(bitmapChromaDecomposeProgram)
if (bitmapChromaSynthesizeProgram != 0) GLES30.glDeleteProgram(bitmapChromaSynthesizeProgram)
if (bitmapChromaComposeProgram != 0) GLES30.glDeleteProgram(bitmapChromaComposeProgram)
if (lutSharpenProgram != 0) {
GLES30.glDeleteProgram(lutSharpenProgram)
lutSharpenProgram = 0
@@ -2690,6 +2907,12 @@ class LutImageProcessor {
if (bitmapDenoiseTexId[i] != 0) GLES30.glDeleteTextures(1, intArrayOf(bitmapDenoiseTexId[i]), 0)
if (bitmapDenoiseFboId[i] != 0) GLES30.glDeleteFramebuffers(1, intArrayOf(bitmapDenoiseFboId[i]), 0)
}
for (i in bitmapChromaDetailTexId.indices) {
if (bitmapChromaDetailTexId[i] != 0) GLES30.glDeleteTextures(1, intArrayOf(bitmapChromaDetailTexId[i]), 0)
if (bitmapChromaDetailFboId[i] != 0) GLES30.glDeleteFramebuffers(1, intArrayOf(bitmapChromaDetailFboId[i]), 0)
bitmapChromaDetailTexId[i] = 0
bitmapChromaDetailFboId[i] = 0
}
releaseLutSharpenFramebuffer()
if (bitmapDenoiseNlmU2BufferId != 0) {
GLES31.glDeleteBuffers(1, intArrayOf(bitmapDenoiseNlmU2BufferId), 0)
@@ -217,7 +217,10 @@ class RawDemosaicProcessor {
private var sharpenProgram = 0
private var passthroughProgram = 0
private var hdrReferenceProgram = 0
private var chromaDenoiseProgram = 0
private var chromaDenoisePrepareProgram = 0
private var chromaDenoiseDecomposeProgram = 0
private var chromaDenoiseSynthesizeProgram = 0
private var chromaDenoiseComposeProgram = 0
private var loggedShadowsHighlightsUniforms = false
// RCD Compute Shader Programs
@@ -297,6 +300,10 @@ class RawDemosaicProcessor {
private var gfFboId = intArrayOf(0, 0)
private var gfWidth = 0
private var gfHeight = 0
private val chromaDenoiseDetailTexId = IntArray(ChromaDenoiseShaders.WAVELET_BANDS)
private val chromaDenoiseDetailFboId = IntArray(ChromaDenoiseShaders.WAVELET_BANDS)
private var chromaDenoiseDetailWidth = 0
private var chromaDenoiseDetailHeight = 0
suspend fun prewarmDepthEstimator(context: Context) = withContext(Dispatchers.Default) {
val start = System.currentTimeMillis()
@@ -1604,7 +1611,7 @@ class RawDemosaicProcessor {
// 初始化着色器和缓冲区
initShaderProgram()
if (sharpenProgram == 0 || passthroughProgram == 0 ||
chromaDenoiseProgram == 0 ||
!isChromaDenoiseReady() ||
rcdPopulateProgram == 0 || rcdStep1Program == 0 || rcdStep2Program == 0 ||
rcdStep3Program == 0 || rcdStep40Program == 0 || rcdStep41Program == 0 ||
rcdStep42Program == 0 || rcdStep43Program == 0 || rcdWriteOutputProgram == 0 ||
@@ -1615,7 +1622,10 @@ class RawDemosaicProcessor {
PLog.e(
TAG, "Critical shader programs failed to compile or link. " +
"sharpen=$sharpenProgram pass=$passthroughProgram " +
"chromaDenoise=$chromaDenoiseProgram " +
"chromaPrepare=$chromaDenoisePrepareProgram " +
"chromaDecompose=$chromaDenoiseDecomposeProgram " +
"chromaSynthesize=$chromaDenoiseSynthesizeProgram " +
"chromaCompose=$chromaDenoiseComposeProgram " +
"populate=$rcdPopulateProgram step1=$rcdStep1Program step2=$rcdStep2Program " +
"step3=$rcdStep3Program step40=$rcdStep40Program step41=$rcdStep41Program " +
"step42=$rcdStep42Program step43=$rcdStep43Program write=$rcdWriteOutputProgram " +
@@ -1700,25 +1710,8 @@ class RawDemosaicProcessor {
// 2.7 NLM Programs
initNLMPrograms(vShader)
// 2.75 RAW 默认色度降噪 Program
val fShaderChromaDenoise =
compileShader(
GLES30.GL_FRAGMENT_SHADER,
ChromaDenoiseShaders.PASS_CHROMA_DENOISE,
"rawChromaDenoiseFragment"
)
if (vShader != 0 && fShaderChromaDenoise != 0) {
chromaDenoiseProgram = GLES30.glCreateProgram()
GLES30.glAttachShader(chromaDenoiseProgram, vShader)
GLES30.glAttachShader(chromaDenoiseProgram, fShaderChromaDenoise)
val linkStart = System.currentTimeMillis()
GLES30.glLinkProgram(chromaDenoiseProgram)
if (!logProgramLinkResult(chromaDenoiseProgram, "rawChromaDenoiseProgram", linkStart)) {
chromaDenoiseProgram = 0
}
GLES30.glDeleteShader(fShaderChromaDenoise)
}
// 2.75 RAW 默认色度降噪 Programs
initRawChromaDenoisePrograms(vShader)
// 3. Passthrough Program
val fShaderPass =
@@ -1750,6 +1743,54 @@ class RawDemosaicProcessor {
)
}
private fun initRawChromaDenoisePrograms(vShader: Int) {
chromaDenoisePrepareProgram = createRawFragmentProgram(
vShader = vShader,
fragmentSource = ChromaDenoiseShaders.PREPARE_YCBCR,
label = "rawChromaDenoisePrepare"
)
chromaDenoiseDecomposeProgram = createRawFragmentProgram(
vShader = vShader,
fragmentSource = ChromaDenoiseShaders.WAVELET_DECOMPOSE,
label = "rawChromaDenoiseDecompose"
)
chromaDenoiseSynthesizeProgram = createRawFragmentProgram(
vShader = vShader,
fragmentSource = ChromaDenoiseShaders.WAVELET_SYNTHESIZE,
label = "rawChromaDenoiseSynthesize"
)
chromaDenoiseComposeProgram = createRawFragmentProgram(
vShader = vShader,
fragmentSource = ChromaDenoiseShaders.COMPOSE_RGB,
label = "rawChromaDenoiseCompose"
)
PLog.d(
TAG,
"RAW chroma denoise programs: prepare=$chromaDenoisePrepareProgram " +
"decompose=$chromaDenoiseDecomposeProgram " +
"synthesize=$chromaDenoiseSynthesizeProgram compose=$chromaDenoiseComposeProgram"
)
}
private fun createRawFragmentProgram(
vShader: Int,
fragmentSource: String,
label: String
): Int {
if (vShader == 0) return 0
val fShader = compileShader(GLES30.GL_FRAGMENT_SHADER, fragmentSource, "${label}Fragment")
if (fShader == 0) return 0
val program = GLES30.glCreateProgram()
GLES30.glAttachShader(program, vShader)
GLES30.glAttachShader(program, fShader)
val linkStart = System.currentTimeMillis()
GLES30.glLinkProgram(program)
val linked = logProgramLinkResult(program, "${label}Program", linkStart)
GLES30.glDeleteShader(fShader)
return if (linked) program else 0
}
private fun getOrCreateCombinedProgram(colorEngine: RawRenderingEngine): Int {
val cachedProgram = combinedPrograms[colorEngine.ordinal]
if (cachedProgram != 0) return cachedProgram
@@ -2083,6 +2124,81 @@ class RawDemosaicProcessor {
checkGlError("setupGuidedFilterFramebuffers")
}
private fun setupChromaDenoiseResources(width: Int, height: Int) {
setupNLMFramebuffers(width, height)
if (
chromaDenoiseDetailWidth == width &&
chromaDenoiseDetailHeight == height &&
chromaDenoiseDetailTexId.all { it != 0 }
) {
return
}
releaseChromaDenoiseDetailResources()
chromaDenoiseDetailWidth = width
chromaDenoiseDetailHeight = height
for (i in chromaDenoiseDetailTexId.indices) {
val texture = IntArray(1)
val framebuffer = IntArray(1)
GLES30.glGenTextures(1, texture, 0)
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, texture[0])
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.glGenFramebuffers(1, framebuffer, 0)
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, framebuffer[0])
GLES30.glFramebufferTexture2D(
GLES30.GL_FRAMEBUFFER,
GLES30.GL_COLOR_ATTACHMENT0,
GLES30.GL_TEXTURE_2D,
texture[0],
0
)
val status = GLES30.glCheckFramebufferStatus(GLES30.GL_FRAMEBUFFER)
if (status != GLES30.GL_FRAMEBUFFER_COMPLETE) {
PLog.e(TAG, "RAW chroma wavelet detail FBO $i incomplete: $status")
}
chromaDenoiseDetailTexId[i] = texture[0]
chromaDenoiseDetailFboId[i] = framebuffer[0]
}
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, 0)
checkGlError("setupChromaDenoiseResources")
}
private fun releaseChromaDenoiseDetailResources() {
for (i in chromaDenoiseDetailTexId.indices) {
if (chromaDenoiseDetailTexId[i] != 0) {
GLES30.glDeleteTextures(1, intArrayOf(chromaDenoiseDetailTexId[i]), 0)
chromaDenoiseDetailTexId[i] = 0
}
if (chromaDenoiseDetailFboId[i] != 0) {
GLES30.glDeleteFramebuffers(1, intArrayOf(chromaDenoiseDetailFboId[i]), 0)
chromaDenoiseDetailFboId[i] = 0
}
}
chromaDenoiseDetailWidth = 0
chromaDenoiseDetailHeight = 0
}
private fun setupDenoiseProfileResources(width: Int, height: Int) {
val pixelCount = width * height
if (
@@ -2125,13 +2241,14 @@ class RawDemosaicProcessor {
return sourceTextureId
}
if (chromaDenoiseProgram == 0 || linearOutputFramebufferId == 0 || linearOutputTextureId == 0) {
if (!isChromaDenoiseReady() || linearOutputFramebufferId == 0 || linearOutputTextureId == 0) {
PLog.w(
TAG,
"RAW chroma denoise program not initialized, falling back to denoiseprofile source"
)
return sourceTextureId
}
setupChromaDenoiseResources(width, height)
val profileGain =
(metadata.iso / 100.0f * metadata.postRawSensitivityBoost).coerceAtLeast(1f)
@@ -2140,41 +2257,253 @@ class RawDemosaicProcessor {
val identityMatrix = FloatArray(16)
GlMatrix.setIdentityM(identityMatrix, 0)
GLES30.glUseProgram(chromaDenoiseProgram)
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, linearOutputFramebufferId)
GLES30.glViewport(0, 0, width, height)
GLES30.glActiveTexture(GLES30.GL_TEXTURE0)
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, sourceTextureId)
GLES30.glUniform1i(GLES30.glGetUniformLocation(chromaDenoiseProgram, "uInputTexture"), 0)
GLES30.glUniform2f(
GLES30.glGetUniformLocation(chromaDenoiseProgram, "uTexelSize"),
1.0f / width,
1.0f / height
var currentIndex = 0
var nextIndex = 1
renderRawChromaPrepare(
sourceTextureId,
gfFboId[currentIndex],
width,
height,
identityMatrix
)
GLES30.glUniformMatrix4fv(
GLES30.glGetUniformLocation(chromaDenoiseProgram, "uTexMatrix"),
1,
false,
identityMatrix,
0
for (band in 0 until ChromaDenoiseShaders.WAVELET_BANDS) {
val scale = 1 shl band
val currentTextureId = gfTexId[currentIndex]
renderRawChromaDecompose(
currentTextureId,
chromaDenoiseDetailFboId[band],
width,
height,
h,
noiseA,
noiseB,
scale,
outputMode = 1,
texMatrix = identityMatrix
)
renderRawChromaDecompose(
currentTextureId,
gfFboId[nextIndex],
width,
height,
h,
noiseA,
noiseB,
scale,
outputMode = 0,
texMatrix = identityMatrix
)
currentIndex = nextIndex
nextIndex = 1 - currentIndex
}
for (band in ChromaDenoiseShaders.WAVELET_BANDS - 1 downTo 0) {
renderRawChromaSynthesize(
gfTexId[currentIndex],
chromaDenoiseDetailTexId[band],
gfFboId[nextIndex],
width,
height,
h,
noiseA,
noiseB,
chromaWaveletBandScale(band),
identityMatrix
)
currentIndex = nextIndex
nextIndex = 1 - currentIndex
}
renderRawChromaCompose(
originalTextureId = sourceTextureId,
chromaTextureId = gfTexId[currentIndex],
targetFboId = linearOutputFramebufferId,
width = width,
height = height,
h = h,
noiseA = noiseA,
noiseB = noiseB,
texMatrix = identityMatrix
)
GLES30.glUniform1f(GLES30.glGetUniformLocation(chromaDenoiseProgram, "uH"), h)
GLES30.glUniform2f(
GLES30.glGetUniformLocation(chromaDenoiseProgram, "uNoiseModel"),
noiseA,
noiseB
)
drawQuad(chromaDenoiseProgram)
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, 0)
checkGlError("RAW chroma denoise before denoiseprofile")
checkGlError("RAW chroma wavelet denoise before denoiseprofile")
releaseChromaDenoiseDetailResources()
PLog.d(
TAG,
"RAW chroma denoise before denoiseprofile: strength=$strength h=$h a=$noiseA b=$noiseB"
"RAW chroma wavelet denoise before denoiseprofile: strength=$strength h=$h " +
"a=$noiseA b=$noiseB bands=${ChromaDenoiseShaders.WAVELET_BANDS}"
)
return linearOutputTextureId
}
private fun isChromaDenoiseReady(): Boolean {
return chromaDenoisePrepareProgram != 0 &&
chromaDenoiseDecomposeProgram != 0 &&
chromaDenoiseSynthesizeProgram != 0 &&
chromaDenoiseComposeProgram != 0
}
private fun renderRawChromaPrepare(
sourceTextureId: Int,
targetFboId: Int,
width: Int,
height: Int,
texMatrix: FloatArray
) {
GLES30.glUseProgram(chromaDenoisePrepareProgram)
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, targetFboId)
GLES30.glViewport(0, 0, width, height)
GLES30.glActiveTexture(GLES30.GL_TEXTURE0)
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, sourceTextureId)
GLES30.glUniform1i(
GLES30.glGetUniformLocation(chromaDenoisePrepareProgram, "uInputTexture"),
0
)
setRawFragmentMatrix(chromaDenoisePrepareProgram, texMatrix)
drawQuad(chromaDenoisePrepareProgram)
}
private fun renderRawChromaDecompose(
sourceTextureId: Int,
targetFboId: Int,
width: Int,
height: Int,
h: Float,
noiseA: Float,
noiseB: Float,
scale: Int,
outputMode: Int,
texMatrix: FloatArray
) {
GLES30.glUseProgram(chromaDenoiseDecomposeProgram)
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, targetFboId)
GLES30.glViewport(0, 0, width, height)
GLES30.glActiveTexture(GLES30.GL_TEXTURE0)
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, sourceTextureId)
GLES30.glUniform1i(
GLES30.glGetUniformLocation(chromaDenoiseDecomposeProgram, "uInputTexture"),
0
)
GLES30.glUniform2f(
GLES30.glGetUniformLocation(chromaDenoiseDecomposeProgram, "uTexelSize"),
1.0f / width,
1.0f / height
)
GLES30.glUniform1f(GLES30.glGetUniformLocation(chromaDenoiseDecomposeProgram, "uH"), h)
GLES30.glUniform2f(
GLES30.glGetUniformLocation(chromaDenoiseDecomposeProgram, "uNoiseModel"),
noiseA,
noiseB
)
GLES30.glUniform1i(GLES30.glGetUniformLocation(chromaDenoiseDecomposeProgram, "uScale"), scale)
GLES30.glUniform1i(
GLES30.glGetUniformLocation(chromaDenoiseDecomposeProgram, "uOutputMode"),
outputMode
)
setRawFragmentMatrix(chromaDenoiseDecomposeProgram, texMatrix)
drawQuad(chromaDenoiseDecomposeProgram)
}
private fun renderRawChromaSynthesize(
coarseTextureId: Int,
detailTextureId: Int,
targetFboId: Int,
width: Int,
height: Int,
h: Float,
noiseA: Float,
noiseB: Float,
bandScale: Float,
texMatrix: FloatArray
) {
GLES30.glUseProgram(chromaDenoiseSynthesizeProgram)
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, targetFboId)
GLES30.glViewport(0, 0, width, height)
GLES30.glActiveTexture(GLES30.GL_TEXTURE0)
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, coarseTextureId)
GLES30.glUniform1i(
GLES30.glGetUniformLocation(chromaDenoiseSynthesizeProgram, "uCoarseTexture"),
0
)
GLES30.glActiveTexture(GLES30.GL_TEXTURE1)
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, detailTextureId)
GLES30.glUniform1i(
GLES30.glGetUniformLocation(chromaDenoiseSynthesizeProgram, "uDetailTexture"),
1
)
GLES30.glUniform2f(
GLES30.glGetUniformLocation(chromaDenoiseSynthesizeProgram, "uTexelSize"),
1.0f / width,
1.0f / height
)
GLES30.glUniform1f(GLES30.glGetUniformLocation(chromaDenoiseSynthesizeProgram, "uH"), h)
GLES30.glUniform2f(
GLES30.glGetUniformLocation(chromaDenoiseSynthesizeProgram, "uNoiseModel"),
noiseA,
noiseB
)
GLES30.glUniform1f(
GLES30.glGetUniformLocation(chromaDenoiseSynthesizeProgram, "uBandScale"),
bandScale
)
setRawFragmentMatrix(chromaDenoiseSynthesizeProgram, texMatrix)
drawQuad(chromaDenoiseSynthesizeProgram)
}
private fun renderRawChromaCompose(
originalTextureId: Int,
chromaTextureId: Int,
targetFboId: Int,
width: Int,
height: Int,
h: Float,
noiseA: Float,
noiseB: Float,
texMatrix: FloatArray
) {
GLES30.glUseProgram(chromaDenoiseComposeProgram)
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, targetFboId)
GLES30.glViewport(0, 0, width, height)
GLES30.glActiveTexture(GLES30.GL_TEXTURE0)
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, originalTextureId)
GLES30.glUniform1i(
GLES30.glGetUniformLocation(chromaDenoiseComposeProgram, "uOriginalTexture"),
0
)
GLES30.glActiveTexture(GLES30.GL_TEXTURE1)
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, chromaTextureId)
GLES30.glUniform1i(
GLES30.glGetUniformLocation(chromaDenoiseComposeProgram, "uChromaTexture"),
1
)
GLES30.glUniform1f(GLES30.glGetUniformLocation(chromaDenoiseComposeProgram, "uH"), h)
GLES30.glUniform2f(
GLES30.glGetUniformLocation(chromaDenoiseComposeProgram, "uNoiseModel"),
noiseA,
noiseB
)
setRawFragmentMatrix(chromaDenoiseComposeProgram, texMatrix)
drawQuad(chromaDenoiseComposeProgram)
}
private fun setRawFragmentMatrix(program: Int, texMatrix: FloatArray) {
val matrixLocation = GLES30.glGetUniformLocation(program, "uTexMatrix")
if (matrixLocation >= 0) {
GLES30.glUniformMatrix4fv(matrixLocation, 1, false, texMatrix, 0)
}
}
private fun chromaWaveletBandScale(band: Int): Float {
return when (band) {
0 -> 0.58f
1 -> 0.82f
2 -> 1.08f
else -> 1.35f
}
}
/**
* 渲染 darktable denoiseprofile NLM 降噪。
@@ -5250,7 +5579,10 @@ class RawDemosaicProcessor {
if (sharpenProgram != 0) GLES30.glDeleteProgram(sharpenProgram)
if (passthroughProgram != 0) GLES30.glDeleteProgram(passthroughProgram)
if (hdrReferenceProgram != 0) GLES30.glDeleteProgram(hdrReferenceProgram)
if (chromaDenoiseProgram != 0) GLES30.glDeleteProgram(chromaDenoiseProgram)
if (chromaDenoisePrepareProgram != 0) GLES30.glDeleteProgram(chromaDenoisePrepareProgram)
if (chromaDenoiseDecomposeProgram != 0) GLES30.glDeleteProgram(chromaDenoiseDecomposeProgram)
if (chromaDenoiseSynthesizeProgram != 0) GLES30.glDeleteProgram(chromaDenoiseSynthesizeProgram)
if (chromaDenoiseComposeProgram != 0) GLES30.glDeleteProgram(chromaDenoiseComposeProgram)
// RCD Compute Programs
if (rcdPopulateProgram != 0) GLES31.glDeleteProgram(rcdPopulateProgram)
@@ -5279,6 +5611,7 @@ class RawDemosaicProcessor {
if (gfTexId[i] != 0) GLES30.glDeleteTextures(1, intArrayOf(gfTexId[i]), 0)
if (gfFboId[i] != 0) GLES30.glDeleteFramebuffers(1, intArrayOf(gfFboId[i]), 0)
}
releaseChromaDenoiseDetailResources()
if (denoiseNlmU2BufferId != 0) {
GLES31.glDeleteBuffers(1, intArrayOf(denoiseNlmU2BufferId), 0)
denoiseNlmU2BufferId = 0