From 622d1becc37aca0c5b2a1b212dbf7817e676b73c Mon Sep 17 00:00:00 2001 From: zhoubinjia Date: Fri, 15 May 2026 17:01:47 +0800 Subject: [PATCH] feat: improve edge preservation and coordinate mapping in RAW compute shaders --- .../main/cpp/shaders/color_scatter_raw.comp | 22 ++++++- .../main/cpp/shaders/green_scatter_raw.comp | 30 ++++++--- .../main/cpp/shaders/normalize_color_hr.comp | 14 +++-- .../main/cpp/shaders/normalize_green_hr.comp | 5 +- .../main/cpp/shaders/reference_prior_raw.comp | 61 ++++++++++++++++++- 5 files changed, 114 insertions(+), 18 deletions(-) diff --git a/app/src/main/cpp/shaders/color_scatter_raw.comp b/app/src/main/cpp/shaders/color_scatter_raw.comp index 7278b1f9..5dc30f73 100644 --- a/app/src/main/cpp/shaders/color_scatter_raw.comp +++ b/app/src/main/cpp/shaders/color_scatter_raw.comp @@ -64,6 +64,18 @@ float inlierDecisionGate(float inlierPosterior) { return (inlierPosterior >= 0.5) ? 1.0 : 0.0; } +vec2 sourceToOutputPlaneCenter(vec2 sourcePlanePos, ivec2 planeOffset) { + vec2 sourceSensorPos = vec2(planeOffset) + sourcePlanePos * 2.0; + vec2 outputSensorPos = (sourceSensorPos + vec2(0.5)) * pc.scale - vec2(0.5); + return (outputSensorPos - vec2(planeOffset)) * 0.5; +} + +vec2 outputToSourcePlaneCenter(vec2 outputPlanePos, ivec2 planeOffset) { + vec2 outputSensorPos = vec2(planeOffset) + outputPlanePos * 2.0; + vec2 sourceSensorPos = (outputSensorPos + vec2(0.5)) / pc.scale - vec2(0.5); + return (sourceSensorPos - vec2(planeOffset)) * 0.5; +} + vec3 getKernelMatrix(vec4 params) { float k1 = params.x; float k2 = params.y; @@ -168,7 +180,7 @@ void processPlane(ivec2 samplePlane, uint planeIndex) { vec2 samplePlanePos = vec2(samplePlane); vec2 flow = (pc.isFirstFrame == 0u) ? sampleFlow(samplePlanePos) : vec2(0.0); vec2 refPlanePos = samplePlanePos - flow; - vec2 outCenter = refPlanePos * pc.scale; + vec2 outCenter = sourceToOutputPlaneCenter(refPlanePos, sampleOffset); ivec2 base = ivec2(floor(outCenter)); float raw = texelFetch(inputRawSampler, samplePos, 0).r; @@ -206,7 +218,7 @@ void processPlane(ivec2 samplePlane, uint planeIndex) { vec2 d_hr = vec2(outPos) - outCenter; float reconWeight = cubicWeight(d_hr.x) * cubicWeight(d_hr.y); if (reconWeight <= 0.0) continue; - vec2 d = (vec2(outPos) / pc.scale) - refPlanePos; + vec2 d = outputToSourcePlaneCenter(vec2(outPos), sampleOffset) - refPlanePos; float mahalanobis = OmegaInv.x * d.x * d.x + 2.0 * OmegaInv.y * d.x * d.y + OmegaInv.z * d.y * d.y; float effectiveSigma2 = 1.0 + 4.0 * flowVar; float geometricLikelihood = @@ -236,7 +248,11 @@ void main() { uint localY = gl_GlobalInvocationID.y; if (localX >= pc.tileW || localY >= pc.tileH) return; - ivec2 samplePlane = ivec2(int(pc.tileX + localX), int(pc.tileY + localY)); + ivec2 sourceStart = ivec2(int(pc.tileX), int(pc.tileY)); + if (pc.scale > 1.001) { + sourceStart = ivec2(floor(vec2(float(pc.tileX), float(pc.tileY)) / pc.scale)) - ivec2(2); + } + ivec2 samplePlane = sourceStart + ivec2(int(localX), int(localY)); processPlane(samplePlane, 0u); processPlane(samplePlane, 3u); } diff --git a/app/src/main/cpp/shaders/green_scatter_raw.comp b/app/src/main/cpp/shaders/green_scatter_raw.comp index 07a577b6..9c838503 100644 --- a/app/src/main/cpp/shaders/green_scatter_raw.comp +++ b/app/src/main/cpp/shaders/green_scatter_raw.comp @@ -71,6 +71,18 @@ float inlierDecisionGate(float inlierPosterior) { return (inlierPosterior >= 0.5) ? 1.0 : 0.0; } +vec2 sourceToOutputPlaneCenter(vec2 sourcePlanePos, ivec2 planeOffset) { + vec2 sourceSensorPos = vec2(planeOffset) + sourcePlanePos * 2.0; + vec2 outputSensorPos = (sourceSensorPos + vec2(0.5)) * pc.scale - vec2(0.5); + return (outputSensorPos - vec2(planeOffset)) * 0.5; +} + +vec2 outputToSourcePlaneCenter(vec2 outputPlanePos, ivec2 planeOffset) { + vec2 outputSensorPos = vec2(planeOffset) + outputPlanePos * 2.0; + vec2 sourceSensorPos = (outputSensorPos + vec2(0.5)) / pc.scale - vec2(0.5); + return (sourceSensorPos - vec2(planeOffset)) * 0.5; +} + vec3 getKernelMatrix(vec4 params) { float k1 = params.x; float k2 = params.y; @@ -160,8 +172,8 @@ float sampleLocalTileWeight(ivec2 planePos) { return clamp(localTileMask.weights[ty * tilesX + tx], 0.0, 1.0); } -void splatSample(int phaseIndex, vec2 outCenter, vec2 refPos, float val, - float pInlier, float flowVar) { +void splatSample(int phaseIndex, ivec2 planeOffset, vec2 outCenter, + vec2 refPos, float val, float pInlier, float flowVar) { vec2 kernelPlanePos = clamp(refPos, vec2(0.0), vec2(float(pc.planeWidth - 1u), float(pc.planeHeight - 1u))); ivec2 planeIdx = ivec2(round(kernelPlanePos)); vec4 kParams = kernelParams.data[planeIdx.y * int(pc.planeWidth) + planeIdx.x]; @@ -182,7 +194,7 @@ void splatSample(int phaseIndex, vec2 outCenter, vec2 refPos, float val, float reconWeight = cubicWeight(d_hr.x) * cubicWeight(d_hr.y); if (reconWeight <= 0.0) continue; - vec2 d = (vec2(outPos) / pc.scale) - refPos; + vec2 d = outputToSourcePlaneCenter(vec2(outPos), planeOffset) - refPos; float mahalanobis = OmegaInv.x * d.x * d.x + (OmegaInv.y * d.x * d.y) * 2.0 + OmegaInv.z * d.y * d.y; @@ -235,7 +247,7 @@ void processGreenPlane(ivec2 samplePlane, int planeIndex) { flow = sampleFlow(planePos); } vec2 refPos = planePos - flow; - vec2 outCenter = refPos * pc.scale; + vec2 outCenter = sourceToOutputPlaneCenter(refPos, sampleOffset); float raw = texelFetch(inputRawSampler, samplePos, 0).r; int bayerIndex = getBayerIndex(int(pc.cfaPattern), samplePos); @@ -257,8 +269,8 @@ void processGreenPlane(ivec2 samplePlane, int planeIndex) { flowVar = flowVarianceBuffer.data[confidenceIdx]; pInlier *= sampleLocalTileWeight(ivec2(rX, rY)); } - splatSample(family, outCenter, refPos, val, pInlier * highlightSuppression, - flowVar); + splatSample(family, sampleOffset, outCenter, refPos, val, + pInlier * highlightSuppression, flowVar); } void main() { @@ -266,7 +278,11 @@ void main() { uint localY = gl_GlobalInvocationID.y; if (localX >= pc.tileW || localY >= pc.tileH) return; - ivec2 samplePlane = ivec2(int(pc.tileX + localX), int(pc.tileY + localY)); + ivec2 sourceStart = ivec2(int(pc.tileX), int(pc.tileY)); + if (pc.scale > 1.001) { + sourceStart = ivec2(floor(vec2(float(pc.tileX), float(pc.tileY)) / pc.scale)) - ivec2(2); + } + ivec2 samplePlane = sourceStart + ivec2(int(localX), int(localY)); processGreenPlane(samplePlane, 1); processGreenPlane(samplePlane, 2); } diff --git a/app/src/main/cpp/shaders/normalize_color_hr.comp b/app/src/main/cpp/shaders/normalize_color_hr.comp index 9550a66a..84451799 100644 --- a/app/src/main/cpp/shaders/normalize_color_hr.comp +++ b/app/src/main/cpp/shaders/normalize_color_hr.comp @@ -188,8 +188,12 @@ void main() { observationSupport = confidence; if (neighborSupport > 0.0 && confidence < 0.999) { float highlightBlend = smoothstep(0.70, 0.96, max(val, neighborVal)); - float preserveCenter = confidence * (1.0 - 0.55 * highlightBlend); - val = mix(neighborVal, val, preserveCenter); + float edgeDelta = abs(neighborVal - val); + float edgeKeep = smoothstep(0.025, 0.090, edgeDelta); + float neighborBlend = + (1.0 - confidence) * (1.0 - 0.55 * highlightBlend) * + (1.0 - edgeKeep); + val = mix(val, neighborVal, neighborBlend); observationSupport = max(observationSupport, 0.30 * neighborSupport); } hasEstimate = true; @@ -237,8 +241,10 @@ void main() { float neighLocalSupport = clamp(float(neighAccum.y) / 768.0, 0.0, 1.0); if (neighLocalSupport <= 0.0) continue; - uint neighGlobalX = uint(offset.x) + uint(clamp(nx, 0, int(pc.tileW) - 1)) * 2u; - uint neighGlobalY = uint(offset.y) + uint(clamp(ny, 0, int(pc.tileH) - 1)) * 2u; + uint neighGlobalX = uint(offset.x) + + (pc.tileX + uint(clamp(nx, 0, int(pc.tileW) - 1))) * 2u; + uint neighGlobalY = uint(offset.y) + + (pc.tileY + uint(clamp(ny, 0, int(pc.tileH) - 1))) * 2u; float neighNorm = computeObservationNorm(accumMean(neighAccum), bayerIndex); float neighGreen = readGuidedGreenNorm(neighGlobalX, neighGlobalY); float neighDiff = neighNorm - neighGreen; diff --git a/app/src/main/cpp/shaders/normalize_green_hr.comp b/app/src/main/cpp/shaders/normalize_green_hr.comp index f1933f56..a73fab47 100644 --- a/app/src/main/cpp/shaders/normalize_green_hr.comp +++ b/app/src/main/cpp/shaders/normalize_green_hr.comp @@ -223,7 +223,10 @@ void main() { float val = (primary.y > 0u) ? primaryVal : neighborVal; if (neighborWeight > 0.0 && primaryConfidence < 0.99) { - val = mix(neighborVal, val, primaryConfidence); + float edgeDelta = abs(neighborVal - val); + float edgeKeep = smoothstep(0.025, 0.085, edgeDelta); + float neighborBlend = (1.0 - primaryConfidence) * (1.0 - edgeKeep); + val = mix(val, neighborVal, neighborBlend); } observationNorm = computeObservationNorm(val, bayerIndex); } diff --git a/app/src/main/cpp/shaders/reference_prior_raw.comp b/app/src/main/cpp/shaders/reference_prior_raw.comp index 246726e6..48a6b49f 100644 --- a/app/src/main/cpp/shaders/reference_prior_raw.comp +++ b/app/src/main/cpp/shaders/reference_prior_raw.comp @@ -79,6 +79,30 @@ float fetchNormalizedRaw(int bayerIndex, ivec2 samplePos) { return float(normalizedReferenceBuffer.data[idx]) / 65535.0; } +float fetchReferenceNorm(ivec2 samplePos) { + samplePos.x = clamp(samplePos.x, 0, int(pc.sensorWidth) - 1); + samplePos.y = clamp(samplePos.y, 0, int(pc.sensorHeight) - 1); + uint idx = uint(samplePos.y) * pc.sensorWidth + uint(samplePos.x); + return float(normalizedReferenceBuffer.data[idx]) / 65535.0; +} + +float referenceGuide(ivec2 samplePos) { + float sum = 0.0; + float weight = 0.0; + for (int oy = -1; oy <= 1; ++oy) { + for (int ox = -1; ox <= 1; ++ox) { + float w = (ox == 0 && oy == 0) ? 1.0 : 0.45; + sum += fetchReferenceNorm(samplePos + ivec2(ox, oy)) * w; + weight += w; + } + } + return sum / max(weight, 1e-5); +} + +vec2 outputToSourceCenter(vec2 outputPos) { + return (outputPos + vec2(0.5)) / max(pc.scale, 1e-5) - vec2(0.5); +} + void samplePrior(int bayerIndex, vec2 sourcePos, out float normalizedRaw, out float support) { ivec2 offset = getBayerOffset(pc.cfaPattern, bayerIndex); @@ -98,11 +122,42 @@ void samplePrior(int bayerIndex, vec2 sourcePos, float vx0 = mix(v00, v10, frac.x); float vx1 = mix(v01, v11, frac.x); - normalizedRaw = mix(vx0, vx1, frac.y); + float bilinearRaw = mix(vx0, vx1, frac.y); + + float g00 = referenceGuide(p00); + float g10 = referenceGuide(p10); + float g01 = referenceGuide(p01); + float g11 = referenceGuide(p11); + float gx0 = mix(g00, g10, frac.x); + float gx1 = mix(g01, g11, frac.x); + float guideCenter = mix(gx0, gx1, frac.y); + ivec2 nearestPos = (frac.x < 0.5) + ? ((frac.y < 0.5) ? p00 : p01) + : ((frac.y < 0.5) ? p10 : p11); + float nearestGuide = referenceGuide(nearestPos); + float guideTarget = mix(guideCenter, nearestGuide, 0.65); + + vec4 spatialWeights = vec4((1.0 - frac.x) * (1.0 - frac.y), + frac.x * (1.0 - frac.y), + (1.0 - frac.x) * frac.y, + frac.x * frac.y); + vec4 guideVals = vec4(g00, g10, g01, g11); + vec4 rawVals = vec4(v00, v10, v01, v11); + vec4 rangeWeights = exp(-pow(guideVals - vec4(guideTarget), vec4(2.0)) / + vec4(2.0 * 0.028 * 0.028)); + vec4 weights = max(spatialWeights, vec4(0.015)) * rangeWeights; + float edgeWeightSum = weights.x + weights.y + weights.z + weights.w; + float edgeRaw = dot(rawVals, weights) / max(edgeWeightSum, 1e-5); + + float guideMin = min(min(g00, g10), min(g01, g11)); + float guideMax = max(max(g00, g10), max(g01, g11)); + float edgeStrength = smoothstep(0.035, 0.14, guideMax - guideMin); + normalizedRaw = mix(bilinearRaw, edgeRaw, edgeStrength); float sampleMin = min(min(v00, v10), min(v01, v11)); float sampleMax = max(max(v00, v10), max(v01, v11)); - float coherence = 1.0 - smoothstep(0.04, 0.18, sampleMax - sampleMin); + float coherence = 1.0 - smoothstep(0.04, 0.18, + max(sampleMax - sampleMin, guideMax - guideMin)); float interpolationPenalty = 0.5 * (4.0 * frac.x * (1.0 - frac.x) + 4.0 * frac.y * (1.0 - frac.y)); float exactness = 1.0 - interpolationPenalty; @@ -121,7 +176,7 @@ void main() { uint outIndex = y * pc.width + x; int bayerIndex = getBayerIndex(pc.cfaPattern, uvec2(x, y)); - vec2 sourcePos = vec2(x, y) / max(pc.scale, 1e-5); + vec2 sourcePos = outputToSourceCenter(vec2(x, y)); float normalizedRaw = 0.0; float support = 0.0; samplePrior(bayerIndex, sourcePos, normalizedRaw, support);