fix: spatial rejection geometry alignment

This commit is contained in:
zhoubinjia committed 2026-09-15 21:26:31 +08:00
1 parent a0c9f16768
commit a6b60ca836
8 files changed
+79 -14

No files matched your search

@@ -1037,8 +1037,10 @@ int ComputeSpatialStrengthMap(
(height + (is_bayer ? 7 : 15)) / (is_bayer ? 8 : 16);
if (alignment_width != expected_alignment_width ||
alignment_height != expected_alignment_height ||
rejection_width != (width + 3) / 4 ||
rejection_height != (height + 3) / 4) {
// V25 passes the unpadded RAW/4 acceptance domain to the noise AOT.
// This is independent of the padded 16x16 signal allocation below.
rejection_width != width / 4 ||
rejection_height != height / 4) {
return -1;
}
@@ -191,10 +191,10 @@ Java_com_hinnka_mycamera_processor_MgcSpatialRgbMerger_nativeMerge(
(static_cast<int64_t>(output_width) + 15) / 16;
const int64_t expected_alignment_height =
(static_cast<int64_t>(output_height) + 15) / 16;
const int64_t expected_rejection_width =
(static_cast<int64_t>(output_width) + 3) / 4;
const int64_t expected_rejection_height =
(static_cast<int64_t>(output_height) + 3) / 4;
// V25's rejection slices describe complete RAW/4 cells, not padded output
// storage. MergeRgb clamps sampling to the supplied rejection extents.
const int64_t expected_rejection_width = raw_width / 4;
const int64_t expected_rejection_height = raw_height / 4;
if (frame_count <= 0 || raw_offsets_array == nullptr ||
raw_row_strides_array == nullptr || alignment_buffer == nullptr ||
rejection_buffer == nullptr || output_buffer == nullptr ||
@@ -5741,6 +5741,13 @@ internal class GlesMgcRawSpatialStacker(
) {
require(frameIndex in 0 until capture.frameCount)
require(!capture.captured[frameIndex])
if (capture.outputMode == MgcSpatialOutputMode.RGB && !identityRejection) {
val source = checkNotNull(textureSpecs[rejectionTexture])
check(source.width == capture.rejectionWidth && source.height == capture.rejectionHeight) {
"MGC RGB acceptance slice ${source.width}x${source.height} does not match " +
"AOT domain ${capture.rejectionWidth}x${capture.rejectionHeight}"
}
}
for (component in 0 until 2) {
val slot = component * capture.frameCount + frameIndex
val outputOriginX = capture.alignmentLayout.originX(slot)
@@ -8530,18 +8537,21 @@ internal class GlesMgcRawSpatialStacker(
private fun mapSpatialStrengthToOutputCoordinates(
source: MgcSpatialStrengthMap,
): MgcSpatialStrengthMap {
val coveredSource = source.withIdentityBorder(width, height)
val targetWidth = ceilDiv(outputWidth, 4)
val targetHeight = ceilDiv(outputHeight, 4)
if (source.width == targetWidth && source.height == targetHeight) return source
if (coveredSource.width == targetWidth && coveredSource.height == targetHeight) {
return coveredSource
}
val startNs = System.nanoTime()
val mapped = MgcSpatialStrengthMapScaler.scaleBilinear(
source = source,
source = coveredSource,
targetWidth = targetWidth,
targetHeight = targetHeight,
)
PLog.i(
TAG,
"MGC Spatial strength coordinates mapped ${source.width}x${source.height} -> " +
"MGC Spatial strength coordinates mapped ${coveredSource.width}x${coveredSource.height} -> " +
"${targetWidth}x$targetHeight for ${normalizedOutputScale}x RGB output " +
"backend=native-openmp took=" +
"${(System.nanoTime() - startNs) / 1_000_000L}ms",
@@ -18,7 +18,7 @@ internal fun mgcSpatialDiagnosticGeometry(
imageWidth: Int,
imageHeight: Int,
): MgcSpatialDiagnosticGeometry {
require(imageWidth > 0 && imageHeight > 0)
require(imageWidth >= 4 && imageHeight >= 4)
val isBayer = outputMode == MgcSpatialOutputMode.BAYER
val alignmentTileSize = if (isBayer) 8 else 16
val fixed16Width = if (isBayer) {
@@ -37,8 +37,11 @@ internal fun mgcSpatialDiagnosticGeometry(
imageHeight = imageHeight,
alignmentWidth = ceilDiv(imageWidth, alignmentTileSize),
alignmentHeight = ceilDiv(imageHeight, alignmentTileSize),
rejectionWidth = ceilDiv(imageWidth, 4),
rejectionHeight = ceilDiv(imageHeight, 4),
// V25 PrepareAccumulation requires RAW.width()/4 by RAW.height()/4.
// Only the signal allocation is padded to complete AOT tiles. Rounding the
// acceptance domain up makes the RGB atlas read past every source slice.
rejectionWidth = imageWidth / 4,
rejectionHeight = imageHeight / 4,
fixed16Width = fixed16Width,
fixed16Height = fixed16Height,
fixed16SampleCount = fixed16Width.toLong() * fixed16Height * fixed16Channels,
@@ -32,8 +32,8 @@ internal fun mgcSpatialRejectionGeometry(
require(filterDownsample > 0)
val guideWidth = ceilDiv(imageWidth, 2)
val guideHeight = ceilDiv(imageHeight, 2)
val mergeWeightWidth = guideWidth / 2
val mergeWeightHeight = guideHeight / 2
val mergeWeightWidth = imageWidth / 4
val mergeWeightHeight = imageHeight / 4
require(mergeWeightWidth > 0 && mergeWeightHeight > 0)
return MgcSpatialRejectionGeometry(
bayerQuadWidth = ceilDiv(imageWidth, 2),
@@ -29,6 +29,24 @@ class MgcSpatialStrengthMap(
return result
}
/**
* V25 SpatialMerge::Finish keeps the AOT's complete RAW/4 cells in place and
* initializes strength outside that region to Q8 identity (256). The extra
* partial cell needed by our full-resolution denoiser is storage coverage,
* not a change of image scale.
*/
fun withIdentityBorder(fullWidth: Int, fullHeight: Int): MgcSpatialStrengthMap {
require(width == fullWidth / 4 && height == fullHeight / 4)
val coveredWidth = (fullWidth + 3) / 4
val coveredHeight = (fullHeight + 3) / 4
if (width == coveredWidth && height == coveredHeight) return this
val covered = identityForFullResolution(fullWidth, fullHeight)
for (y in 0 until height) {
q8.copyInto(covered.q8, y * coveredWidth, y * width, (y + 1) * width)
}
return covered
}
companion object {
/** Q8 identity multiplier used by MGC before a Spatial model is available. */
private const val IDENTITY_Q8 = 256
@@ -4,6 +4,20 @@ import org.junit.Assert.assertEquals
import org.junit.Test
class MgcSpatialDiagnosticGeometryTest {
@Test
fun croppedRawAcceptanceMatchesV25ProducerWithoutPadding() {
for ((width, height) in listOf(2862 to 2146, 2862 to 2144, 2864 to 2146)) {
val producer = mgcSpatialRejectionGeometry(width, height, 4)
for (mode in MgcSpatialOutputMode.entries) {
val consumer = mgcSpatialDiagnosticGeometry(mode, width, height)
assertEquals(width / 4, consumer.rejectionWidth)
assertEquals(height / 4, consumer.rejectionHeight)
assertEquals(producer.mergeWeightWidth, consumer.rejectionWidth)
assertEquals(producer.mergeWeightHeight, consumer.rejectionHeight)
}
}
}
@Test
fun rgbFixed16PlanesCoverTheFinalCompleteAotTile() {
val geometry = mgcSpatialDiagnosticGeometry(
@@ -1,10 +1,28 @@
package com.hinnka.mycamera.raw
import org.junit.Assert.assertArrayEquals
import org.junit.Assert.assertEquals
import org.junit.Assert.assertSame
import org.junit.Assert.assertTrue
import org.junit.Test
class MgcSpatialStrengthMapTest {
@Test
fun partialRawCellsUseIdentityWithoutRescalingValidAotSamples() {
val values = shortArrayOf(37, 129, 411, 87, 303, 902)
val native = MgcSpatialStrengthMap(3, 2, values)
val covered = native.withIdentityBorder(fullWidth = 14, fullHeight = 10)
assertEquals(4, covered.width)
assertEquals(3, covered.height)
assertArrayEquals(
shortArrayOf(37, 129, 411, 256, 87, 303, 902, 256, 256, 256, 256, 256),
covered.q8,
)
assertArrayEquals(values, native.q8)
assertSame(native, native.withIdentityBorder(fullWidth = 12, fullHeight = 8))
}
@Test
fun identityMapUsesQuarterResolutionQ8Defaults() {
val strengthMap = MgcSpatialStrengthMap.identityForFullResolution(