feat: remove Bento tiling area and overlap assessment

This commit is contained in:
zhoubinjia committed 2026-09-07 17:47:59 +08:00
1 parent d5dee12189
commit bea94d9337
3 files changed
+13 -364

No files matched your search

@@ -939,197 +939,6 @@ internal object GlesMgcRawSpatialShaders {
}
""".trimIndent()
/**
* First FindBlockTiles pass. Each output texel represents one 16x16 RAW tile
* (8x8 Bayer quads); RGBA stores the phase-preserving alignment residual on
* the left, right, top, and bottom tile edges.
*/
val findBlockTilesGatherEdges = """
#version 300 es
precision highp float;
precision highp int;
precision highp usampler2D;
uniform usampler2D uBaseRaw;
uniform usampler2D uAltRaw;
uniform sampler2D uFlow;
uniform vec4 uFlowScaleOffset;
uniform ivec2 uRawSize;
uniform ivec2 uBayerSize;
uniform ivec2 uTileGridSize;
uniform int uCfaPattern;
uniform vec4 uBasePhaseGains;
uniform vec4 uBasePhaseBlackTerms;
uniform vec4 uAltPhaseGains;
uniform vec4 uAltPhaseBlackTerms;
layout(location = 0) out vec4 oEdges;
const int TILE_SIZE = 8;
const float SIGNAL_FLOOR = 0.015625;
const float INTERIOR_REJECTION = 0.82;
float normalizedPhase(
usampler2D rawTexture,
ivec2 bayer,
int phase,
vec4 gains,
vec4 blackTerms
) {
ivec2 raw = bayer * 2 + ivec2(phase & 1, phase >> 1);
raw = clamp(raw, ivec2(0), uRawSize - ivec2(1));
return clamp(float(texelFetch(rawTexture, raw, 0).r) * gains[phase] + blackTerms[phase], 0.0, 1.0);
}
float greenAt(
usampler2D rawTexture,
ivec2 bayer,
vec4 gains,
vec4 blackTerms
) {
// RGGB/BGGR have their greens at phases 1/2; GRBG/GBRG at 0/3.
int phase0 = (uCfaPattern == 0 || uCfaPattern == 3) ? 1 : 0;
int phase1 = (uCfaPattern == 0 || uCfaPattern == 3) ? 2 : 3;
return 0.5 * (
normalizedPhase(rawTexture, bayer, phase0, gains, blackTerms) +
normalizedPhase(rawTexture, bayer, phase1, gains, blackTerms)
);
}
float alignedAltGreen(vec2 bayer) {
vec2 clamped = clamp(bayer, vec2(0.0), vec2(uBayerSize - ivec2(1)));
ivec2 p0 = ivec2(floor(clamped));
ivec2 p1 = min(p0 + ivec2(1), uBayerSize - ivec2(1));
vec2 f = fract(clamped);
float v00 = greenAt(uAltRaw, p0, uAltPhaseGains, uAltPhaseBlackTerms);
float v10 = greenAt(uAltRaw, ivec2(p1.x, p0.y), uAltPhaseGains, uAltPhaseBlackTerms);
float v01 = greenAt(uAltRaw, ivec2(p0.x, p1.y), uAltPhaseGains, uAltPhaseBlackTerms);
float v11 = greenAt(uAltRaw, p1, uAltPhaseGains, uAltPhaseBlackTerms);
return mix(mix(v00, v10, f.x), mix(v01, v11, f.x), f.y);
}
void main() {
ivec2 tile = ivec2(gl_FragCoord.xy);
if (any(greaterThanEqual(tile, uTileGridSize))) {
oEdges = vec4(0.0);
return;
}
ivec2 tileOrigin = tile * TILE_SIZE;
vec4 edgeSum = vec4(0.0);
vec4 edgeCount = vec4(0.0);
float tileSum = 0.0;
float tileCount = 0.0;
for (int y = 0; y < TILE_SIZE; ++y) {
for (int x = 0; x < TILE_SIZE; ++x) {
ivec2 bayer = tileOrigin + ivec2(x, y);
if (any(greaterThanEqual(bayer, uBayerSize))) {
continue;
}
vec2 referenceUv =
(vec2(bayer) + vec2(0.5)) / vec2(uBayerSize);
vec2 flowUv =
referenceUv * uFlowScaleOffset.xy +
uFlowScaleOffset.zw;
vec2 flow = texture(uFlow, flowUv).xy * vec2(uBayerSize);
float baseGreen = greenAt(uBaseRaw, bayer, uBasePhaseGains, uBasePhaseBlackTerms);
float altGreen = alignedAltGreen(vec2(bayer) + flow);
float signal = max(0.5 * (baseGreen + altGreen), SIGNAL_FLOOR);
float residual = abs(baseGreen - altGreen) / sqrt(signal);
tileSum += residual;
tileCount += 1.0;
if (x < 2) {
edgeSum.x += residual;
edgeCount.x += 1.0;
}
if (x >= TILE_SIZE - 2) {
edgeSum.y += residual;
edgeCount.y += 1.0;
}
if (y < 2) {
edgeSum.z += residual;
edgeCount.z += 1.0;
}
if (y >= TILE_SIZE - 2) {
edgeSum.w += residual;
edgeCount.w += 1.0;
}
}
}
vec4 edgeMean = edgeSum / max(edgeCount, vec4(1.0));
float interiorMean = tileSum / max(tileCount, 1.0);
oEdges = clamp(edgeMean - vec4(INTERIOR_REJECTION * interiorMean), 0.0, 1.0);
}
""".trimIndent()
/** Pair the two observations of every shared tile edge and convert them to a soft mask. */
val findBlockTilesFilterIntermediate = """
#version 300 es
precision highp float;
precision highp int;
uniform sampler2D uGatheredEdges;
uniform ivec2 uSize;
layout(location = 0) out float oFiltered;
vec4 edgesAt(ivec2 p) {
return texelFetch(uGatheredEdges, clamp(p, ivec2(0), uSize - ivec2(1)), 0);
}
void main() {
ivec2 p = ivec2(gl_FragCoord.xy);
if (any(greaterThanEqual(p, uSize))) {
oFiltered = 0.0;
return;
}
vec4 center = edgesAt(p);
float left = p.x > 0 ? min(center.x, edgesAt(p + ivec2(-1, 0)).y) : 0.0;
float right = p.x + 1 < uSize.x ? min(center.y, edgesAt(p + ivec2(1, 0)).x) : 0.0;
float top = p.y > 0 ? min(center.z, edgesAt(p + ivec2(0, -1)).w) : 0.0;
float bottom = p.y + 1 < uSize.y ? min(center.w, edgesAt(p + ivec2(0, 1)).z) : 0.0;
float pairedEdge = max(max(left, right), max(top, bottom));
oFiltered = smoothstep(0.020, 0.080, pairedEdge);
}
""".trimIndent()
/** Final binary tile mask. Isolated responses are removed before CPU component analysis. */
val findBlockTilesOutput = """
#version 300 es
precision highp float;
precision highp int;
uniform sampler2D uFiltered;
uniform ivec2 uSize;
layout(location = 0) out float oMask;
float valueAt(ivec2 p) {
if (any(lessThan(p, ivec2(0))) || any(greaterThanEqual(p, uSize))) {
return 0.0;
}
return texelFetch(uFiltered, p, 0).r;
}
void main() {
ivec2 p = ivec2(gl_FragCoord.xy);
if (any(greaterThanEqual(p, uSize)) || p.x == 0 || p.y == 0 || p.x == uSize.x - 1 || p.y == uSize.y - 1) {
oMask = 0.0;
return;
}
float center = valueAt(p);
float axialSupport = valueAt(p + ivec2(-1, 0)) + valueAt(p + ivec2(1, 0)) +
valueAt(p + ivec2(0, -1)) + valueAt(p + ivec2(0, 1));
oMask = center >= 0.5 && center + axialSupport >= 1.5 ? 1.0 : 0.0;
}
""".trimIndent()
// Embedded OpenCL source: Mask_GenerateHighlightMask.
val bentoGenerateHighlightMask = """
#version 300 es
@@ -1194,7 +1003,7 @@ internal object GlesMgcRawSpatialShaders {
// Embedded OpenCL sources: GainUp and
// Mask_AdjustHighlightMaskAndGenerateInpaintingMask. The third output is the aligned
// ultrashort clipping mask consumed by Bento's high-overlap fallback predicate.
// ultrashort clipping mask retained for Bento's overlap diagnostics.
val bentoAdjustHighlightMask = """
#version 300 es
precision highp float;
@@ -389,7 +389,6 @@ internal class GlesMgcRawSpatialStacker(
val reason: String,
val clippedPixelRatio: Float,
val largestInpaintingArea: Int,
val largestTilingArea: Int,
val ultrashortClippingOverlap: Float,
)
@@ -516,9 +515,6 @@ internal class GlesMgcRawSpatialStacker(
private var rejectionPostprocessProgram = 0
private var dilationProgram = 0
private var linearKernelMaskProgram = 0
private var findBlockTilesGatherEdgesProgram = 0
private var findBlockTilesFilterIntermediateProgram = 0
private var findBlockTilesOutputProgram = 0
private var bentoHighlightProgram = 0
private var bentoHighlightCountProgram = 0
private var bentoAdjustProgram = 0
@@ -1058,7 +1054,7 @@ internal class GlesMgcRawSpatialStacker(
TAG,
"Bento assessment accepted=false reason=insufficient_clipped_pixels " +
"clippedRatio=$baseHighlightClippedRatio largestInpaintingArea=0 " +
"largestTilingArea=0 ultrashortOverlap=0.0 " +
"ultrashortOverlap=0.0 " +
"exposureRatio=$bentoExposureRatio earlyGate=true",
)
}
@@ -1125,13 +1121,6 @@ internal class GlesMgcRawSpatialStacker(
val postAlignStartNs = System.nanoTime()
val bayerAlignment = alignment.texture
val flow = createConvertedAlignment(alignment)
val tilingMask = renderFindBlockTiles(
baseRaw = referenceRaw,
ultrashortRaw = bentoRaw,
flow = flow,
baseCalibration = referenceCalibration,
ultrashortCalibration = normalizedCalibration,
)
val unscaledCalibration = calibrationForFrame(
ultrashortFrame,
@@ -1197,12 +1186,6 @@ internal class GlesMgcRawSpatialStacker(
ultrashortClippingMask,
"Bento ultrashort clipping mask",
),
tilingMask = readR8Mask(
texture = tilingMask,
label = "Bento FindBlockTiles mask",
maskWidth = bayerAlignmentWidth,
maskHeight = bayerAlignmentHeight,
),
)
PLog.i(
TAG,
@@ -1210,7 +1193,6 @@ internal class GlesMgcRawSpatialStacker(
"reason=${assessment.reason} " +
"clippedRatio=${assessment.clippedPixelRatio} " +
"largestInpaintingArea=${assessment.largestInpaintingArea} " +
"largestTilingArea=${assessment.largestTilingArea} " +
"ultrashortOverlap=${assessment.ultrashortClippingOverlap} " +
"exposureRatio=$exposureRatio earlyGate=false " +
"gpuWait=${assessmentGpuWaitMs}ms " +
@@ -3434,18 +3416,6 @@ internal class GlesMgcRawSpatialStacker(
)
}
if (includeBentoAssessment) {
findBlockTilesGatherEdgesProgram = linkProgram(
GlesMgcRawSpatialShaders.findBlockTilesGatherEdges,
"mgc_find_block_tiles_gather_edges",
)
findBlockTilesFilterIntermediateProgram = linkProgram(
GlesMgcRawSpatialShaders.findBlockTilesFilterIntermediate,
"mgc_find_block_tiles_filter_intermediate",
)
findBlockTilesOutputProgram = linkProgram(
GlesMgcRawSpatialShaders.findBlockTilesOutput,
"mgc_find_block_tiles_output",
)
bentoAdjustProgram = linkProgram(
GlesMgcRawSpatialShaders.bentoAdjustHighlightMask,
"mgc_bento_adjust_mask",
@@ -5369,119 +5339,6 @@ internal class GlesMgcRawSpatialStacker(
)
}
/**
* GLES translation of FindBlockTiles' recovered three-stage contract:
* GatherEdges (RGBA16) -> FilterIntermediate (R8) -> Output (R8).
* The mask remains in the 16x16-RAW tile domain used by Bento's component-area gate.
*/
private fun renderFindBlockTiles(
baseRaw: Int,
ultrashortRaw: Int,
flow: ConvertedAlignment,
baseCalibration: FrameCalibration,
ultrashortCalibration: FrameCalibration,
): Int {
val gatheredEdges = createTexture(
bayerAlignmentWidth,
bayerAlignmentHeight,
GLES30.GL_RGBA16F,
GLES30.GL_NEAREST,
)
GLES30.glUseProgram(findBlockTilesGatherEdgesProgram)
bindTexture(findBlockTilesGatherEdgesProgram, "uBaseRaw", 0, baseRaw)
bindTexture(findBlockTilesGatherEdgesProgram, "uAltRaw", 1, ultrashortRaw)
bindTexture(findBlockTilesGatherEdgesProgram, "uFlow", 2, flow.texture)
uniformFlowScaleOffset(findBlockTilesGatherEdgesProgram, flow)
uniform2i(findBlockTilesGatherEdgesProgram, "uRawSize", width, height)
uniform2i(
findBlockTilesGatherEdgesProgram,
"uBayerSize",
rejectionGeometry.bayerQuadWidth,
rejectionGeometry.bayerQuadHeight,
)
uniform2i(
findBlockTilesGatherEdgesProgram,
"uTileGridSize",
bayerAlignmentWidth,
bayerAlignmentHeight,
)
uniform1i(findBlockTilesGatherEdgesProgram, "uCfaPattern", cfaPattern)
uniform4fv(
findBlockTilesGatherEdgesProgram,
"uBasePhaseGains",
baseCalibration.bayerPhaseGains,
)
uniform4fv(
findBlockTilesGatherEdgesProgram,
"uBasePhaseBlackTerms",
baseCalibration.bayerPhaseBlackTerms,
)
uniform4fv(
findBlockTilesGatherEdgesProgram,
"uAltPhaseGains",
ultrashortCalibration.bayerPhaseGains,
)
uniform4fv(
findBlockTilesGatherEdgesProgram,
"uAltPhaseBlackTerms",
ultrashortCalibration.bayerPhaseBlackTerms,
)
draw(
findBlockTilesGatherEdgesProgram,
bayerAlignmentWidth,
bayerAlignmentHeight,
intArrayOf(gatheredEdges),
)
val filtered = createTexture(
bayerAlignmentWidth,
bayerAlignmentHeight,
GLES30.GL_R8,
GLES30.GL_NEAREST,
)
GLES30.glUseProgram(findBlockTilesFilterIntermediateProgram)
bindTexture(
findBlockTilesFilterIntermediateProgram,
"uGatheredEdges",
0,
gatheredEdges,
)
uniform2i(
findBlockTilesFilterIntermediateProgram,
"uSize",
bayerAlignmentWidth,
bayerAlignmentHeight,
)
draw(
findBlockTilesFilterIntermediateProgram,
bayerAlignmentWidth,
bayerAlignmentHeight,
intArrayOf(filtered),
)
val output = createTexture(
bayerAlignmentWidth,
bayerAlignmentHeight,
GLES30.GL_R8,
GLES30.GL_NEAREST,
)
GLES30.glUseProgram(findBlockTilesOutputProgram)
bindTexture(findBlockTilesOutputProgram, "uFiltered", 0, filtered)
uniform2i(
findBlockTilesOutputProgram,
"uSize",
bayerAlignmentWidth,
bayerAlignmentHeight,
)
draw(
findBlockTilesOutputProgram,
bayerAlignmentWidth,
bayerAlignmentHeight,
intArrayOf(output),
)
return output
}
private fun renderBentoHighlightMask(
baseFrame: Int,
outputMask: Int,
@@ -6696,7 +6553,6 @@ internal class GlesMgcRawSpatialStacker(
baseHighlightMask: ByteArray,
inpaintingMask: ByteArray,
ultrashortClippingMask: ByteArray,
tilingMask: ByteArray,
): BentoAssessment {
val guideMaskSize = guideWidth * guideHeight
require(
@@ -6704,7 +6560,6 @@ internal class GlesMgcRawSpatialStacker(
inpaintingMask.size == guideMaskSize &&
ultrashortClippingMask.size == guideMaskSize,
)
require(tilingMask.size == bayerAlignmentWidth * bayerAlignmentHeight)
var clippedPixels = 0
var clippedByUltrashortPixels = 0
for (index in 0 until guideMaskSize) {
@@ -6715,6 +6570,7 @@ internal class GlesMgcRawSpatialStacker(
}
}
val clippedRatio = clippedPixels.toFloat() / guideMaskSize.toFloat()
// Diagnostic only: remaining clipped highlights do not rule out recovery elsewhere.
val ultrashortOverlap = if (clippedPixels > 0) {
clippedByUltrashortPixels.toFloat() / clippedPixels.toFloat()
} else {
@@ -6725,20 +6581,11 @@ internal class GlesMgcRawSpatialStacker(
guideWidth,
guideHeight,
)
val largestTilingArea = BentoFallbackTopology.largestEightConnectedComponentArea(
tilingMask,
bayerAlignmentWidth,
bayerAlignmentHeight,
)
val reason = when {
clippedRatio <= BENTO_MIN_CLIPPED_PIXEL_RATIO ->
"insufficient_clipped_pixels"
largestInpaintingArea >= BENTO_MAX_INPAINTING_COMPONENT_AREA ->
"large_hole_needing_inpainting"
ultrashortOverlap > BENTO_MAX_ULTRASHORT_CLIPPING_OVERLAP ->
"high_ultrashort_clipping_overlap"
largestTilingArea > BENTO_MAX_TILING_COMPONENT_AREA ->
"tiling_artifacts"
else -> "none"
}
return BentoAssessment(
@@ -6746,7 +6593,6 @@ internal class GlesMgcRawSpatialStacker(
reason = reason,
clippedPixelRatio = clippedRatio,
largestInpaintingArea = largestInpaintingArea,
largestTilingArea = largestTilingArea,
ultrashortClippingOverlap = ultrashortOverlap,
)
}
@@ -10068,8 +9914,6 @@ internal class GlesMgcRawSpatialStacker(
const val BENTO_MIN_RGB_FOR_INPAINTING = 128f
const val BENTO_MIN_CLIPPED_PIXEL_RATIO = 0.00039f
const val BENTO_MAX_INPAINTING_COMPONENT_AREA = 80
const val BENTO_MAX_TILING_COMPONENT_AREA = 5
const val BENTO_MAX_ULTRASHORT_CLIPPING_OVERLAP = 0.62f
const val LONG_FRAME_RAW_CLIPPING_THRESHOLD = 250f / 255f
// The independent classic-Sabre 0x3882c20 table is represented by SABRE_DENOISE_*.
@@ -1,35 +1,31 @@
package com.hinnka.mycamera.processor
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test
class BentoFallbackTopologyTest {
@Test
fun tilingGateUsesEightConnectivityAndStrictMaximumArea() {
val fiveTiles = ByteArray(8 * 8)
fun diagonalPixelsFormOneEightConnectedComponent() {
val fivePixels = ByteArray(8 * 8)
repeat(5) { index ->
fiveTiles[index * 8 + index] = 0xff.toByte()
fivePixels[index * 8 + index] = 0xff.toByte()
}
val sixTiles = fiveTiles.copyOf().also { mask ->
val sixPixels = fivePixels.copyOf().also { mask ->
mask[5 * 8 + 5] = 0xff.toByte()
}
val areaAtLimit = BentoFallbackTopology.largestEightConnectedComponentArea(
mask = fiveTiles,
val fivePixelArea = BentoFallbackTopology.largestEightConnectedComponentArea(
mask = fivePixels,
width = 8,
height = 8,
)
val areaOverLimit = BentoFallbackTopology.largestEightConnectedComponentArea(
mask = sixTiles,
val sixPixelArea = BentoFallbackTopology.largestEightConnectedComponentArea(
mask = sixPixels,
width = 8,
height = 8,
)
assertEquals(5, areaAtLimit)
assertEquals(6, areaOverLimit)
assertFalse(areaAtLimit > 5)
assertTrue(areaOverLimit > 5)
assertEquals(5, fivePixelArea)
assertEquals(6, sixPixelArea)
}
}