feat: noise model filtering refinements and high-precision samplers

This commit is contained in:
zhoubinjia committed 2026-09-18 10:57:02 +08:00
1 parent 965d52b60f
commit bcdb078454
7 files changed
+118 -11

No files matched your search

@@ -509,6 +509,7 @@ float DownsamplingEnergy(
bool AdvancePyramidCorrelation(
float spectrum[128],
bool chroma,
float* coefficient_scale) {
// Zero spectral amplitude represents zero noise energy, which has no
// identifiable normalized spectrum. Carry that energy in the coefficients
@@ -521,8 +522,9 @@ bool AdvancePyramidCorrelation(
*coefficient_scale = 0.0f;
return true;
}
// MGC's fixed eight-tap filter at 0x6b20e0/0x6b6200.
constexpr float filter[8] = {
// V25 CreateLumaDenoiseNoiseBuffers (0x4f85664) uses eight taps,
// CreateChromaDenoiseNoiseModelBuffers (0x4c4f084) uses four.
constexpr float luma_filter[8] = {
-3.0f / 128.0f,
-7.0f / 128.0f,
17.0f / 128.0f,
@@ -532,6 +534,9 @@ bool AdvancePyramidCorrelation(
-7.0f / 128.0f,
-3.0f / 128.0f,
};
constexpr float chroma_filter[4] = {0.125f, 0.375f, 0.375f, 0.125f};
const float* filter = chroma ? chroma_filter : luma_filter;
const int filter_size = chroma ? 4 : 8;
float filtered[128] = {};
float energy = 0.0f;
for (int frequency = 0; frequency < 128; ++frequency) {
@@ -544,7 +549,7 @@ bool AdvancePyramidCorrelation(
(static_cast<double>(frequency) + 0.5) *
2.0 * M_PI / 128.0 -
M_PI;
for (int tap = 0; tap < 8; ++tap) {
for (int tap = 0; tap < filter_size; ++tap) {
real += static_cast<double>(filter[tap]) *
std::cos(omega * tap);
imaginary -= static_cast<double>(filter[tap]) *
@@ -561,7 +566,39 @@ bool AdvancePyramidCorrelation(
for (int index = 0; index < 128; ++index) {
spectrum[index] = filtered[index] * inverse_energy;
}
*coefficient_scale = energy * energy;
const float convolution_scale = energy * energy;
// Both V25 builders then call NoiseModel::Downsample(2), 0x5f37dc4.
// Its spectrum resampler (0x5f3bbfc) interpolates the half-bin grid,
// folds the two aliases and restores even symmetry. Merely filtering
// leaves later levels in the previous level's frequency coordinates.
float expanded[256] = {};
for (int index = 64; index < 128; ++index) {
const float left = 0.25f * spectrum[index - 1] + 0.75f * spectrum[index];
const float right = 0.75f * spectrum[index] +
0.25f * spectrum[std::min(index + 1, 127)];
expanded[2 * index] = left;
expanded[2 * index + 1] = right;
expanded[255 - 2 * index] = left;
expanded[254 - 2 * index] = right;
}
for (int index = 64; index < 128; ++index) {
const float value = 0.5f * expanded[(index + 192) & 255] +
0.5f * expanded[index + 64];
spectrum[index] = value;
spectrum[127 - index] = value;
}
float downsample_energy = 0.0f;
for (int index = 0; index < 128; ++index) {
downsample_energy += spectrum[index];
}
downsample_energy *= 1.0f / 128.0f;
if (!(downsample_energy > 0.0f) || !std::isfinite(downsample_energy)) return false;
const float inverse_downsample_energy = 1.0f / downsample_energy;
for (int index = 0; index < 128; ++index) {
spectrum[index] *= inverse_downsample_energy;
}
*coefficient_scale = convolution_scale * (downsample_energy * downsample_energy);
return true;
}
@@ -843,6 +880,7 @@ bool BuildNoiseBuffers(
float coefficient_scale = 0.0f;
if (!AdvancePyramidCorrelation(
current_correlation,
false,
&coefficient_scale)) {
return false;
}
@@ -926,6 +964,7 @@ bool BuildChromaNoiseBuffers(
float coefficient_scale = 0.0f;
if (!AdvancePyramidCorrelation(
current_correlation,
true,
&coefficient_scale)) {
return false;
}
@@ -9010,8 +9010,11 @@ class Camera2Controller(private val context: Context) {
rollingShutterSkewNs = frameResult.get(CaptureResult.SENSOR_ROLLING_SHUTTER_SKEW),
gyroWindow = frozenMetadata?.gyroWindow
?: burstGyroRecorder.exposureWindow(sensorTimestampNs, exposureTimeNs),
channelNoiseProfile = frozenMetadata?.channelNoiseProfile
?: captureChannelNoiseProfile(frameResult),
// Resolve noise from the same result as this RAW's ISO/CFA. The early
// snapshot belongs to the logical result, while dimension matching above
// may select a different physical sensor. If its profile is absent, let
// the noise resolver use its declared fallback, not another sensor's model.
channelNoiseProfile = captureChannelNoiseProfile(frameResult),
multiFrameCaptureRole = frozenMetadata?.multiFrameCaptureRole
?: (frameResult.request.tag as? MultiFrameCaptureRole),
dynamicBlackLevelByCfaPosition = frameResult
@@ -6,11 +6,15 @@ package com.hinnka.mycamera.processor
* Sabre works on an extracted 2x2 Bayer texture, builds its guide and covariance at one sample
* per Bayer quad, accumulates camera RGB and three independent weights in a full-resolution MRT,
* then dehomogenizes the result before the ResolveSabre stage.
*
* Floating samplers explicitly use highp throughout, including sampler function parameters.
* The float default alone does not preserve sampled RAW values, noise variances or subpixel flow.
*/
internal object GlesMgcRawSabreShaders {
val extractBayer = """
#version 300 es
precision highp float;
precision highp sampler2D;
precision highp int;
precision highp usampler2D;
uniform highp usampler2D uRaw;
@@ -38,6 +42,7 @@ internal object GlesMgcRawSabreShaders {
val guideAndCovariance = """
#version 300 es
precision highp float;
precision highp sampler2D;
precision highp int;
uniform sampler2D uExtractedBayer;
uniform sampler2D uNoiseEstimates;
@@ -301,6 +306,7 @@ internal object GlesMgcRawSabreShaders {
val baseFrameReferenceColor = """
#version 300 es
precision highp float;
precision highp sampler2D;
precision highp int;
uniform sampler2D uBaseGuide;
uniform ivec2 uGuideSize;
@@ -319,6 +325,7 @@ internal object GlesMgcRawSabreShaders {
val rejection = """
#version 300 es
precision highp float;
precision highp sampler2D;
precision highp int;
uniform sampler2D uBaseGuide;
uniform sampler2D uAltGuide;
@@ -431,6 +438,7 @@ internal object GlesMgcRawSabreShaders {
val merge = """
#version 300 es
precision highp float;
precision highp sampler2D;
precision highp int;
uniform sampler2D uExtractedBayer;
uniform sampler2D uFlow;
@@ -624,6 +632,7 @@ internal object GlesMgcRawSabreShaders {
val copyMask = """
#version 300 es
precision highp float;
precision highp sampler2D;
uniform sampler2D uRejection;
uniform float uAccumulatedWeightScale;
layout(location = 0) out float oAccumulatedWeight;
@@ -636,6 +645,7 @@ internal object GlesMgcRawSabreShaders {
val copyAlpha = """
#version 300 es
precision highp float;
precision highp sampler2D;
uniform sampler2D uSource;
layout(location = 0) out float oWeight;
void main() {
@@ -654,6 +664,7 @@ internal object GlesMgcRawSabreShaders {
val reciprocalGreenWeight4x4 = """
#version 300 es
precision highp float;
precision highp sampler2D;
precision highp int;
uniform sampler2D uAccumulatedWeightsGb;
uniform ivec2 uInputSize;
@@ -681,6 +692,7 @@ internal object GlesMgcRawSabreShaders {
val dehomogenize = """
#version 300 es
precision highp float;
precision highp sampler2D;
uniform sampler2D uSourceWeightR;
uniform sampler2D uSourceWeightGb;
uniform sampler2D uSourceAlpha;
@@ -700,6 +712,7 @@ internal object GlesMgcRawSabreShaders {
private val outputTransformBody = """
precision highp float;
precision highp sampler2D;
precision highp int;
precision highp usampler2D;
uniform highp usampler2D uResolvedR;
@@ -737,7 +750,7 @@ internal object GlesMgcRawSabreShaders {
val outputTransformUint16 = """
#version 300 es
$outputTransformBody
${outputTransformBody.prependIndent(" ")}
layout(location = 0) out highp uvec4 oResolved;
void main() {
ivec2 p = ivec2(gl_FragCoord.xy);
@@ -748,7 +761,7 @@ internal object GlesMgcRawSabreShaders {
val outputTransformFloat = """
#version 300 es
$outputTransformBody
${outputTransformBody.prependIndent(" ")}
layout(location = 0) out vec4 oResolved;
void main() {
ivec2 p = ivec2(gl_FragCoord.xy);
@@ -6,11 +6,14 @@ package com.hinnka.mycamera.processor
* The guide, rejection, dilation and merge equations below retain the embedded GLSL equations and
* constants. The original program first extracts RAW16 into a half-resolution Bayer texture.
* Photon reads R16UI directly; the RGB branch reconstructs jointly from native CFA observations.
* Float samplers (including helper parameters) must remain highp: the CPU/Halide alignment
* products are Float32, and normalized flow/noise samples cannot use the default lowp precision.
*/
internal object GlesMgcRawSpatialShaders {
val guide = """
#version 300 es
precision highp float;
precision highp sampler2D;
precision highp int;
precision highp usampler2D;
uniform highp usampler2D uRaw;
@@ -114,6 +117,7 @@ internal object GlesMgcRawSpatialShaders {
val rgbChromaGuide = """
#version 300 es
precision highp float;
precision highp sampler2D;
precision highp int;
precision highp usampler2D;
uniform highp usampler2D uRaw;
@@ -225,6 +229,7 @@ internal object GlesMgcRawSpatialShaders {
val covariance = """
#version 300 es
precision highp float;
precision highp sampler2D;
precision highp int;
precision highp usampler2D;
uniform highp usampler2D uRaw;
@@ -428,6 +433,7 @@ internal object GlesMgcRawSpatialShaders {
fun rejectionWithFlowSource(flowSource: String) = """
#version 300 es
precision highp float;
precision highp sampler2D;
precision highp int;
uniform sampler2D uBaseGuide;
uniform sampler2D uAltGuide;
@@ -548,6 +554,7 @@ internal object GlesMgcRawSpatialShaders {
val rejectionAcceptance = """
#version 300 es
precision highp float;
precision highp sampler2D;
precision highp int;
uniform sampler2D uAcceptance;
uniform ivec2 uSize;
@@ -574,6 +581,7 @@ internal object GlesMgcRawSpatialShaders {
val rejectionPixelDifferenceDownsample = """
#version 300 es
precision highp float;
precision highp sampler2D;
precision highp int;
uniform sampler2D uInput;
uniform ivec2 uInputSize;
@@ -608,6 +616,7 @@ internal object GlesMgcRawSpatialShaders {
val clippedGaussianHorizontal = """
#version 300 es
precision highp float;
precision highp sampler2D;
precision highp int;
uniform sampler2D uInput;
uniform ivec2 uSize;
@@ -634,6 +643,7 @@ internal object GlesMgcRawSpatialShaders {
val clippedGaussianVertical = """
#version 300 es
precision highp float;
precision highp sampler2D;
precision highp int;
uniform sampler2D uInput;
uniform ivec2 uSize;
@@ -667,6 +677,7 @@ internal object GlesMgcRawSpatialShaders {
val rejectionFilterDownsample = """
#version 300 es
precision highp float;
precision highp sampler2D;
precision highp int;
precision highp isampler2D;
uniform highp isampler2D uBaseLuma;
@@ -736,6 +747,7 @@ internal object GlesMgcRawSpatialShaders {
val rejectionFilter = """
#version 300 es
precision highp float;
precision highp sampler2D;
precision highp int;
uniform sampler2D uLuma;
uniform sampler2D uRejection;
@@ -817,6 +829,7 @@ internal object GlesMgcRawSpatialShaders {
val rejectionPostprocess = """
#version 300 es
precision highp float;
precision highp sampler2D;
precision highp int;
uniform sampler2D uOriginalWeight;
uniform sampler2D uFilteredWeight;
@@ -856,6 +869,7 @@ internal object GlesMgcRawSpatialShaders {
private fun buildDilateRejection(includePixelDifference: Boolean) = """
#version 300 es
precision highp float;
precision highp sampler2D;
precision highp int;
uniform sampler2D uRejection;
uniform ivec2 uInputSize;
@@ -912,6 +926,7 @@ internal object GlesMgcRawSpatialShaders {
val updateLinearKernelMask = """
#version 300 es
precision highp float;
precision highp sampler2D;
precision highp int;
uniform sampler2D uRejection;
uniform ivec2 uSize;
@@ -945,6 +960,7 @@ internal object GlesMgcRawSpatialShaders {
val bentoGenerateHighlightMask = """
#version 300 es
precision highp float;
precision highp sampler2D;
precision highp int;
uniform sampler2D uBaseFrame;
uniform ivec2 uSize;
@@ -974,6 +990,7 @@ internal object GlesMgcRawSpatialShaders {
val bentoCountHighlightMask = """
#version 310 es
precision highp float;
precision highp sampler2D;
precision highp int;
layout(local_size_x = 8, local_size_y = 8, local_size_z = 1) in;
uniform highp sampler2D uMask;
@@ -1009,6 +1026,7 @@ internal object GlesMgcRawSpatialShaders {
val bentoAdjustHighlightMask = """
#version 300 es
precision highp float;
precision highp sampler2D;
precision highp int;
uniform sampler2D uBaseFrame;
uniform sampler2D uUltrashortFrame;
@@ -1089,6 +1107,7 @@ internal object GlesMgcRawSpatialShaders {
val bentoRewriteWeight = """
#version 300 es
precision highp float;
precision highp sampler2D;
precision highp int;
uniform sampler2D uExistingWeight;
uniform sampler2D uBentoMask;
@@ -1117,6 +1136,7 @@ internal object GlesMgcRawSpatialShaders {
val mergeRgb = """
#version 300 es
precision highp float;
precision highp sampler2D;
precision highp int;
precision highp usampler2D;
uniform highp usampler2D uRaw;
@@ -1339,6 +1359,7 @@ internal object GlesMgcRawSpatialShaders {
val normalizeRgb16 = """
#version 300 es
precision highp float;
precision highp sampler2D;
precision highp int;
uniform sampler2D uColorAndRWeight;
uniform sampler2D uGbWeights;
@@ -1502,6 +1523,7 @@ internal object GlesMgcRawSpatialShaders {
val copyRgb16ToFloat = """
#version 310 es
precision highp float;
precision highp sampler2D;
precision highp int;
precision highp uimage2D;
precision highp image2D;
@@ -1526,6 +1548,7 @@ internal object GlesMgcRawSpatialShaders {
val alignedRawClippingMask = """
#version 300 es
precision highp float;
precision highp sampler2D;
precision highp int;
precision highp usampler2D;
@@ -1595,6 +1618,7 @@ internal object GlesMgcRawSpatialShaders {
val mergeBayer = """
#version 300 es
precision highp float;
precision highp sampler2D;
precision highp int;
precision highp usampler2D;
uniform highp usampler2D uRaw;
@@ -1852,6 +1876,7 @@ internal object GlesMgcRawSpatialShaders {
val sabreMergeBayer = """
#version 300 es
precision highp float;
precision highp sampler2D;
precision highp int;
precision highp usampler2D;
uniform highp usampler2D uRaw;
@@ -1979,6 +2004,7 @@ internal object GlesMgcRawSpatialShaders {
val normalizeBayer = """
#version 300 es
precision highp float;
precision highp sampler2D;
precision highp int;
uniform sampler2D uBayerAndWeight;
uniform ivec2 uOutputSize;
@@ -2012,6 +2038,7 @@ internal object GlesMgcRawSpatialShaders {
val packBayerFixed16 = """
#version 300 es
precision highp float;
precision highp sampler2D;
precision highp int;
uniform sampler2D uBayerAndWeight;
uniform ivec2 uSourceSize;
@@ -2044,6 +2071,7 @@ internal object GlesMgcRawSpatialShaders {
val rawToGray = """
#version 300 es
precision highp float;
precision highp sampler2D;
precision highp int;
precision highp usampler2D;
uniform highp usampler2D uRaw;
@@ -2104,6 +2132,7 @@ internal object GlesMgcRawSpatialShaders {
private fun buildGrayDownsample(linearOutput: Boolean) = """
#version 300 es
precision highp float;
precision highp sampler2D;
precision highp int;
precision highp isampler2D;
uniform highp isampler2D uInput;
@@ -2145,6 +2174,7 @@ internal object GlesMgcRawSpatialShaders {
private fun buildGrayDownsample4(linearOutput: Boolean) = """
#version 300 es
precision highp float;
precision highp sampler2D;
precision highp int;
precision highp isampler2D;
uniform highp isampler2D uInput;
@@ -2187,6 +2217,7 @@ internal object GlesMgcRawSpatialShaders {
private fun buildAlignmentGradientProducts(sparseFineLevels: Boolean) = """
#version 300 es
precision highp float;
precision highp sampler2D;
precision highp int;
precision highp isampler2D;
uniform highp isampler2D uReference;
@@ -2278,6 +2309,7 @@ internal object GlesMgcRawSpatialShaders {
private fun buildUpsampleAlignment(globalCandidateTexture: Boolean, sampleStep: Int = 1) = """
#version 300 es
precision highp float;
precision highp sampler2D;
precision highp int;
precision highp isampler2D;
uniform highp isampler2D uReference;
@@ -2419,6 +2451,7 @@ internal object GlesMgcRawSpatialShaders {
val blockLucasKanade = """
#version 300 es
precision highp float;
precision highp sampler2D;
precision highp int;
precision highp isampler2D;
uniform highp isampler2D uReference;
@@ -2560,6 +2593,7 @@ internal object GlesMgcRawSpatialShaders {
val alignL1 = """
#version 300 es
precision highp float;
precision highp sampler2D;
precision highp int;
precision highp isampler2D;
uniform highp isampler2D uReference;
@@ -2649,6 +2683,7 @@ internal object GlesMgcRawSpatialShaders {
val medianAlignment = """
#version 300 es
precision highp float;
precision highp sampler2D;
precision highp int;
uniform sampler2D uAlignment;
uniform ivec2 uGridSize;
@@ -2697,6 +2732,7 @@ internal object GlesMgcRawSpatialShaders {
val convertAlignment = """
#version 300 es
precision highp float;
precision highp sampler2D;
precision highp int;
uniform sampler2D uAlignment;
uniform ivec2 uGridSize;
@@ -2742,6 +2778,7 @@ internal object GlesMgcRawSpatialShaders {
val strengthAlignment = """
#version 300 es
precision highp float;
precision highp sampler2D;
precision highp int;
uniform sampler2D uAlignment;
uniform ivec2 uOutputSize;
@@ -2758,6 +2795,7 @@ internal object GlesMgcRawSpatialShaders {
val strengthRejection = """
#version 300 es
precision highp float;
precision highp sampler2D;
precision highp int;
uniform sampler2D uRejection;
uniform ivec2 uOutputSize;
@@ -2787,6 +2825,7 @@ internal object GlesMgcRawSpatialShaders {
val unblocker = """
#version 300 es
precision highp float;
precision highp sampler2D;
precision highp int;
precision highp usampler2D;
uniform highp usampler2D uRaw;
@@ -2895,6 +2934,7 @@ internal object GlesMgcRawSpatialShaders {
val unblockerBlur = """
#version 300 es
precision highp float;
precision highp sampler2D;
precision highp int;
uniform sampler2D uPreBlur;
out float oUnblocker;
@@ -9651,6 +9651,7 @@ internal class GlesMgcRawSpatialStacker(
private fun ensureGles3() {
DngCaptureDiagnostics.recordCurrentGl()
DngCaptureDiagnostics.put("merge.floatSamplerPrecision", "highp")
val version = GLES30.glGetString(GLES30.GL_VERSION).orEmpty()
check(version.contains("OpenGL ES 3.")) {
"MGC Spatial merge requires GLES3, got: $version"
@@ -9715,6 +9716,7 @@ internal class GlesMgcRawSpatialStacker(
TAG,
"MGC Spatial GL vendor=${GLES30.glGetString(GLES30.GL_VENDOR).orEmpty()} " +
"renderer=${GLES30.glGetString(GLES30.GL_RENDERER).orEmpty()} version=$version " +
"floatSamplerPrecision=highp " +
"strengthSsboMax=$maxShaderStorageBlockBytes " +
"strengthPackGroups=${maxComputePackGroupsX}x$maxComputePackGroupsY",
)
@@ -19,9 +19,9 @@ import java.nio.ByteBuffer
*
* This class owns luma/chroma protobuf tuning selection and normalized
* noise-model preparation. Spatial consumes its propagated correlation/coefficient/strength
* outputs; classic Sabre consumes its dedicated luma tuning and scales the complete reference
* NoiseModel by its measured merge factor. The native bridge converts normalized
* read/shot/quadratic coefficients exactly once at the Q14 S16 kernel boundary.
* outputs; classic Sabre consumes its dedicated luma tuning and the variance propagated by
* MgcSabreMergedNoiseModel from per-frame models and V25's SNR correction. The native bridge
* converts normalized read/shot/quadratic coefficients once at the Q14 S16 kernel boundary.
*/
internal object MgcFullResolutionDenoise {
private const val TAG = "MgcFullResolutionDenoise"