fix: mfnr/sr
This commit is contained in:
1 parent
7e5d605c3c
commit
d6bff36cd2
9 files changed
+728
-225
No files matched your search
@@ -78,6 +78,10 @@ vec4 sampleBicubic(sampler2D s, vec2 uv, vec2 size) {
|
||||
return (abs(weightSum) > 1e-6) ? (accum / weightSum) : accum;
|
||||
}
|
||||
|
||||
float luminance(vec3 color) {
|
||||
return dot(color, vec3(0.299, 0.587, 0.114));
|
||||
}
|
||||
|
||||
// Compute Anisotropic Gaussian Weight
|
||||
float computeKernelWeight(vec2 diffVec, vec4 kp) {
|
||||
// kp = (k1, k2, cos2theta, sin2theta)
|
||||
@@ -206,47 +210,35 @@ void main() {
|
||||
if (pc.isFirstFrame == 0 && currentW > 0.001) {
|
||||
vec3 refRGB = accumulator.data[outIndex].rgb / currentW;
|
||||
vec3 centerColor = sampleBicubic(rgbSampler, finalUV, nativeSize).rgb;
|
||||
vec3 diffVec = abs(centerColor - refRGB);
|
||||
float diff = max(diffVec.r, max(diffVec.g, diffVec.b));
|
||||
float luma = refRGB.g;
|
||||
float diff = abs(luminance(centerColor) - luminance(refRGB));
|
||||
float luma = luminance(refRGB);
|
||||
// Note: noiseAlpha and noiseBeta are given as Variances in the new model.
|
||||
// We must translate this variance back to Standard Deviation (sqrt) to
|
||||
// compare linearly against the absolute difference `diff`.
|
||||
float expectedVar = pc.noiseAlpha * luma + pc.noiseBeta;
|
||||
float expectedSigma = sqrt(max(expectedVar, 1e-7));
|
||||
expectedSigma = max(expectedSigma, 0.015);
|
||||
expectedSigma = max(expectedSigma, 0.025);
|
||||
|
||||
float ratio = diff / expectedSigma;
|
||||
float T1 = 2.0; float T2 = 8.0;
|
||||
float T1 = 3.0; float T2 = 12.0;
|
||||
if (ratio <= T1) centerRobustness = 1.0;
|
||||
else if (ratio >= T2) centerRobustness = 0.0;
|
||||
else centerRobustness = (T2 - ratio) / (T2 - T1);
|
||||
|
||||
if (isnan(centerRobustness) || isinf(centerRobustness)) centerRobustness = 0.0;
|
||||
|
||||
float kernelDetail = max(kp.x, kp.y);
|
||||
float detailSignal = smoothstep(4.0, 18.0, kernelDetail);
|
||||
float motionPenalty = mix(0.008, 0.028, detailSignal);
|
||||
float motionPenalty = 0.010;
|
||||
centerRobustness *= (1.0 / (1.0 + motionError * motionPenalty));
|
||||
if (isnan(centerRobustness)) centerRobustness = 0.0;
|
||||
}
|
||||
float localTileWeight = sampleLocalTileWeight(finalUV);
|
||||
float detailStrength = smoothstep(4.0, 18.0, max(kp.x, kp.y));
|
||||
float detailSuppression = (pc.isFirstFrame == 0)
|
||||
? (1.0 - detailStrength)
|
||||
: 1.0;
|
||||
float fusionMask = localTileWeight;
|
||||
int supportRadius = 2;
|
||||
int supportRadius = 0;
|
||||
|
||||
if (pc.isFirstFrame == 0) {
|
||||
fusionMask *= (0.06 + 0.94 * detailSuppression * detailSuppression);
|
||||
if (detailStrength > 0.55) {
|
||||
supportRadius = 0;
|
||||
} else if (detailStrength > 0.20) {
|
||||
supportRadius = 1;
|
||||
}
|
||||
} else if (detailStrength > 0.45) {
|
||||
supportRadius = 1;
|
||||
supportRadius = 2;
|
||||
float tilePenalty = 1.0 - smoothstep(0.10, 0.55, localTileWeight);
|
||||
fusionMask *= mix(0.90, 0.55, tilePenalty);
|
||||
}
|
||||
|
||||
// Accumulate neighbors
|
||||
@@ -277,9 +269,24 @@ void main() {
|
||||
// the squared distance (u*u, v*v) grows exactly 4 times faster.
|
||||
// We multiply the inverse variances by 0.25 to normalize the spatial spread.
|
||||
float w = exp(-0.5 * (u*u * kp.x * 0.25 + v*v * kp.y * 0.25));
|
||||
|
||||
|
||||
if (pc.isFirstFrame == 0) {
|
||||
vec3 refRGB = accumulator.data[outIndex].rgb / max(accumulator.data[outIndex].a, 1e-6);
|
||||
float sampleDiff = abs(luminance(sampleColor) - luminance(refRGB));
|
||||
float refLuma = luminance(refRGB);
|
||||
float expectedVar = pc.noiseAlpha * refLuma + pc.noiseBeta;
|
||||
float expectedSigma = max(sqrt(max(expectedVar, 1e-7)), 0.025);
|
||||
float diffRatio = sampleDiff / expectedSigma;
|
||||
float sampleRobustness = 1.0 - smoothstep(4.0, 10.0, diffRatio);
|
||||
float largeDiffPenalty = smoothstep(5.5, 9.5, diffRatio);
|
||||
fusionMask *= mix(1.0, 0.70, largeDiffPenalty);
|
||||
robustness *= sampleRobustness;
|
||||
robustness = max(robustness, 0.25 * localTileWeight);
|
||||
}
|
||||
|
||||
float combinedWeight = w * robustness * pc.frameWeight * fusionMask;
|
||||
if (isnan(combinedWeight) || isinf(combinedWeight)) combinedWeight = 0.0;
|
||||
if (combinedWeight <= 1e-6) continue;
|
||||
|
||||
vec4 increment = vec4(sampleColor * combinedWeight, combinedWeight);
|
||||
if (!any(isnan(increment)) && !any(isinf(increment))) {
|
||||
|
||||
@@ -36,9 +36,10 @@ layout(set = 0, binding = 0) uniform sampler2D inputRawSampler;
|
||||
layout(set = 0, binding = 1, scalar) buffer AlignBuffer { vec2 offsets[]; } alignBuffer;
|
||||
layout(set = 0, binding = 2, scalar) buffer KernelParams { vec4 data[]; } kernelParams;
|
||||
layout(set = 0, binding = 3, scalar) buffer RobustnessBuffer { float data[]; } outRobustness;
|
||||
layout(set = 0, binding = 4, scalar) buffer ColorAccum { uvec2 data[]; } colorAccum;
|
||||
layout(set = 0, binding = 5, scalar) buffer FlowVarianceBuffer { float data[]; } flowVarianceBuffer;
|
||||
layout(set = 0, binding = 6) uniform sampler2D uLensShadingMap;
|
||||
layout(set = 0, binding = 4, scalar) buffer ColorAccum0 { uvec2 data[]; } colorAccum0;
|
||||
layout(set = 0, binding = 5, scalar) buffer ColorAccum1 { uvec2 data[]; } colorAccum1;
|
||||
layout(set = 0, binding = 6, scalar) buffer FlowVarianceBuffer { float data[]; } flowVarianceBuffer;
|
||||
layout(set = 0, binding = 7) uniform sampler2D uLensShadingMap;
|
||||
|
||||
const float kAccumScale = 2048.0;
|
||||
|
||||
@@ -91,38 +92,61 @@ int getBayerIndex(int cfaPattern, ivec2 samplePos) {
|
||||
}
|
||||
}
|
||||
|
||||
bool sampleMatchesTarget(ivec2 pos, out int lscIdx) {
|
||||
ivec2 getBayerOffset(int cfaPattern, int planeIndex) {
|
||||
if (cfaPattern == 0) {
|
||||
const ivec2 map[4] = ivec2[4](ivec2(0, 0), ivec2(1, 0), ivec2(0, 1), ivec2(1, 1));
|
||||
return map[planeIndex];
|
||||
} else if (cfaPattern == 1) {
|
||||
const ivec2 map[4] = ivec2[4](ivec2(1, 0), ivec2(0, 0), ivec2(1, 1), ivec2(0, 1));
|
||||
return map[planeIndex];
|
||||
} else if (cfaPattern == 2) {
|
||||
const ivec2 map[4] = ivec2[4](ivec2(0, 1), ivec2(1, 1), ivec2(0, 0), ivec2(1, 0));
|
||||
return map[planeIndex];
|
||||
} else {
|
||||
const ivec2 map[4] = ivec2[4](ivec2(1, 1), ivec2(0, 1), ivec2(1, 0), ivec2(0, 0));
|
||||
return map[planeIndex];
|
||||
}
|
||||
}
|
||||
|
||||
bool sampleMatchesPlane(ivec2 pos, uint planeIndex, out int lscIdx) {
|
||||
int phase = (pos.y & 1) * 2 + (pos.x & 1);
|
||||
lscIdx = -1;
|
||||
if (pc.cfaPattern == 0u) {
|
||||
if (pc.outputChannel == 0u && phase == 0) { lscIdx = 0; return true; }
|
||||
if (pc.outputChannel == 2u && phase == 3) { lscIdx = 3; return true; }
|
||||
if (planeIndex == 0u && phase == 0) { lscIdx = 0; return true; }
|
||||
if (planeIndex == 3u && phase == 3) { lscIdx = 3; return true; }
|
||||
} else if (pc.cfaPattern == 1u) {
|
||||
if (pc.outputChannel == 0u && phase == 1) { lscIdx = 0; return true; }
|
||||
if (pc.outputChannel == 2u && phase == 2) { lscIdx = 3; return true; }
|
||||
if (planeIndex == 0u && phase == 1) { lscIdx = 0; return true; }
|
||||
if (planeIndex == 3u && phase == 2) { lscIdx = 3; return true; }
|
||||
} else if (pc.cfaPattern == 2u) {
|
||||
if (pc.outputChannel == 0u && phase == 2) { lscIdx = 0; return true; }
|
||||
if (pc.outputChannel == 2u && phase == 1) { lscIdx = 3; return true; }
|
||||
if (planeIndex == 0u && phase == 2) { lscIdx = 0; return true; }
|
||||
if (planeIndex == 3u && phase == 1) { lscIdx = 3; return true; }
|
||||
} else {
|
||||
if (pc.outputChannel == 0u && phase == 3) { lscIdx = 0; return true; }
|
||||
if (pc.outputChannel == 2u && phase == 0) { lscIdx = 3; return true; }
|
||||
if (planeIndex == 0u && phase == 3) { lscIdx = 0; return true; }
|
||||
if (planeIndex == 3u && phase == 0) { lscIdx = 3; return true; }
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void main() {
|
||||
uint x = gl_GlobalInvocationID.x;
|
||||
uint y = gl_GlobalInvocationID.y;
|
||||
if (x >= pc.sensorWidth || y >= pc.sensorHeight) return;
|
||||
void processPlane(ivec2 samplePlane, uint planeIndex) {
|
||||
ivec2 sampleOffset = getBayerOffset(int(pc.cfaPattern), int(planeIndex));
|
||||
if (samplePlane.x < 0 || samplePlane.y < 0 ||
|
||||
samplePlane.x >= int(pc.planeWidth) || samplePlane.y >= int(pc.planeHeight)) {
|
||||
return;
|
||||
}
|
||||
|
||||
ivec2 samplePos = sampleOffset + samplePlane * 2;
|
||||
if (samplePos.x < 0 || samplePos.y < 0 ||
|
||||
samplePos.x >= int(pc.sensorWidth) || samplePos.y >= int(pc.sensorHeight)) {
|
||||
return;
|
||||
}
|
||||
|
||||
ivec2 samplePos = ivec2(x, y);
|
||||
int lscIdx = -1;
|
||||
if (!sampleMatchesTarget(samplePos, lscIdx)) return;
|
||||
if (!sampleMatchesPlane(samplePos, planeIndex, lscIdx)) return;
|
||||
|
||||
vec2 planePos = vec2(samplePos) * 0.5;
|
||||
vec2 flow = (pc.isFirstFrame == 0u) ? sampleFlow(planePos) : vec2(0.0);
|
||||
vec2 refPos = vec2(samplePos) - flow * 2.0;
|
||||
vec2 outCenter = refPos * pc.scale;
|
||||
vec2 samplePlanePos = vec2(samplePlane);
|
||||
vec2 flow = (pc.isFirstFrame == 0u) ? sampleFlow(samplePlanePos) : vec2(0.0);
|
||||
vec2 refPlanePos = samplePlanePos - flow;
|
||||
vec2 outCenter = refPlanePos * pc.scale;
|
||||
ivec2 base = ivec2(floor(outCenter));
|
||||
|
||||
float raw = texelFetch(inputRawSampler, samplePos, 0).r;
|
||||
@@ -144,35 +168,57 @@ void main() {
|
||||
flowVar = flowVarianceBuffer.data[confidenceIdx];
|
||||
}
|
||||
|
||||
vec2 planeRef = clamp(refPos * 0.5, vec2(0.0), vec2(float(pc.planeWidth - 1u), float(pc.planeHeight - 1u)));
|
||||
vec2 planeRef = clamp(refPlanePos, vec2(0.0), vec2(float(pc.planeWidth - 1u), float(pc.planeHeight - 1u)));
|
||||
ivec2 planeIdx = ivec2(round(planeRef));
|
||||
vec3 OmegaInv = getKernelMatrix(kernelParams.data[planeIdx.y * int(pc.planeWidth) + planeIdx.x]);
|
||||
vec4 kParams = kernelParams.data[planeIdx.y * int(pc.planeWidth) + planeIdx.x];
|
||||
vec3 OmegaInv = getKernelMatrix(kParams);
|
||||
float detailRetention = 1.0;
|
||||
if (pc.isFirstFrame == 0u) {
|
||||
float anisotropy = abs(kParams.x - kParams.y) / max(kParams.x + kParams.y, 1e-5);
|
||||
float edgeSuppression = smoothstep(0.20, 0.65, anisotropy);
|
||||
detailRetention = 1.0 - 0.85 * edgeSuppression;
|
||||
}
|
||||
|
||||
for (int oy = -1; oy <= 2; ++oy) {
|
||||
for (int ox = -1; ox <= 2; ++ox) {
|
||||
ivec2 outPos = base + ivec2(ox, oy);
|
||||
if (outPos.x < int(pc.tileX) || outPos.y < int(pc.tileY) ||
|
||||
outPos.x >= int(pc.tileX + pc.tileW) || outPos.y >= int(pc.tileY + pc.tileH) ||
|
||||
outPos.x < 0 || outPos.y < 0 || outPos.x >= int(pc.width) || outPos.y >= int(pc.height)) continue;
|
||||
outPos.x < 0 || outPos.y < 0) continue;
|
||||
|
||||
vec2 d_hr = vec2(outPos) - outCenter;
|
||||
float reconWeight = cubicWeight(d_hr.x) * cubicWeight(d_hr.y);
|
||||
if (reconWeight <= 0.0) continue;
|
||||
vec2 d_raw = (vec2(outPos) / pc.scale) - refPos;
|
||||
vec2 d = d_raw * 0.5;
|
||||
vec2 d = (vec2(outPos) / pc.scale) - 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 inlierWeight = 0.18 + 0.82 * pInlier;
|
||||
float w = inlierWeight *
|
||||
exp(-0.5 * max(mahalanobis, 0.0) / effectiveSigma2) *
|
||||
reconWeight * (0.45 + 0.55 * pc.frameWeight);
|
||||
w *= detailRetention;
|
||||
if (w <= 0.0) continue;
|
||||
|
||||
uint weightFixed = max(1u, uint(clamp(w * kAccumScale, 0.0, 4294967040.0)));
|
||||
uint sumFixed = uint(clamp(val * w * 65535.0 * kAccumScale, 0.0, 4294967040.0));
|
||||
uint idx = uint(outPos.y - int(pc.tileY)) * pc.bufferStride + uint(outPos.x - int(pc.tileX));
|
||||
atomicAdd(colorAccum.data[idx].x, sumFixed);
|
||||
atomicAdd(colorAccum.data[idx].y, weightFixed);
|
||||
if (planeIndex == 0u) {
|
||||
atomicAdd(colorAccum0.data[idx].x, sumFixed);
|
||||
atomicAdd(colorAccum0.data[idx].y, weightFixed);
|
||||
} else {
|
||||
atomicAdd(colorAccum1.data[idx].x, sumFixed);
|
||||
atomicAdd(colorAccum1.data[idx].y, weightFixed);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
uint localX = gl_GlobalInvocationID.x;
|
||||
uint localY = gl_GlobalInvocationID.y;
|
||||
if (localX >= pc.tileW || localY >= pc.tileH) return;
|
||||
|
||||
ivec2 samplePlane = ivec2(int(pc.tileX + localX), int(pc.tileY + localY));
|
||||
processPlane(samplePlane, 0u);
|
||||
processPlane(samplePlane, 3u);
|
||||
}
|
||||
@@ -103,6 +103,22 @@ int getBayerIndex(int cfaPattern, ivec2 samplePos) {
|
||||
}
|
||||
}
|
||||
|
||||
ivec2 getBayerOffset(int cfaPattern, int planeIndex) {
|
||||
if (cfaPattern == 0) {
|
||||
const ivec2 map[4] = ivec2[4](ivec2(0, 0), ivec2(1, 0), ivec2(0, 1), ivec2(1, 1));
|
||||
return map[planeIndex];
|
||||
} else if (cfaPattern == 1) {
|
||||
const ivec2 map[4] = ivec2[4](ivec2(1, 0), ivec2(0, 0), ivec2(1, 1), ivec2(0, 1));
|
||||
return map[planeIndex];
|
||||
} else if (cfaPattern == 2) {
|
||||
const ivec2 map[4] = ivec2[4](ivec2(0, 1), ivec2(1, 1), ivec2(0, 0), ivec2(1, 0));
|
||||
return map[planeIndex];
|
||||
} else {
|
||||
const ivec2 map[4] = ivec2[4](ivec2(1, 1), ivec2(0, 1), ivec2(1, 0), ivec2(0, 0));
|
||||
return map[planeIndex];
|
||||
}
|
||||
}
|
||||
|
||||
int getGreenFamily(int cfaPattern, ivec2 samplePos, out int lscIdx) {
|
||||
int phase = (samplePos.y & 1) * 2 + (samplePos.x & 1);
|
||||
lscIdx = -1;
|
||||
@@ -124,10 +140,16 @@ int getGreenFamily(int cfaPattern, ivec2 samplePos, out int lscIdx) {
|
||||
|
||||
void splatSample(int phaseIndex, vec2 outCenter, vec2 refPos, float val,
|
||||
float pInlier, float flowVar) {
|
||||
vec2 kernelPlanePos = clamp(refPos * 0.5, vec2(0.0), vec2(float(pc.planeWidth - 1u), float(pc.planeHeight - 1u)));
|
||||
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];
|
||||
vec3 OmegaInv = getKernelMatrix(kParams);
|
||||
float detailRetention = 1.0;
|
||||
if (pc.isFirstFrame == 0u) {
|
||||
float anisotropy = abs(kParams.x - kParams.y) / max(kParams.x + kParams.y, 1e-5);
|
||||
float edgeSuppression = smoothstep(0.20, 0.65, anisotropy);
|
||||
detailRetention = 1.0 - 0.85 * edgeSuppression;
|
||||
}
|
||||
|
||||
ivec2 base = ivec2(floor(outCenter));
|
||||
for (int oy = -1; oy <= 2; ++oy) {
|
||||
@@ -136,8 +158,7 @@ void splatSample(int phaseIndex, vec2 outCenter, vec2 refPos, float val,
|
||||
if (outPos.x < int(pc.tileX) || outPos.y < int(pc.tileY) ||
|
||||
outPos.x >= int(pc.tileX + pc.tileW) ||
|
||||
outPos.y >= int(pc.tileY + pc.tileH) ||
|
||||
outPos.x < 0 || outPos.y < 0 ||
|
||||
outPos.x >= int(pc.width) || outPos.y >= int(pc.height)) {
|
||||
outPos.x < 0 || outPos.y < 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@@ -145,8 +166,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_raw = (vec2(outPos) / pc.scale) - refPos;
|
||||
vec2 d = d_raw * 0.5;
|
||||
vec2 d = (vec2(outPos) / pc.scale) - refPos;
|
||||
float mahalanobis = OmegaInv.x * d.x * d.x +
|
||||
(OmegaInv.y * d.x * d.y) * 2.0 +
|
||||
OmegaInv.z * d.y * d.y;
|
||||
@@ -155,6 +175,7 @@ void splatSample(int phaseIndex, vec2 outCenter, vec2 refPos, float val,
|
||||
float w = inlierWeight *
|
||||
exp(-0.5 * max(mahalanobis, 0.0) / effectiveSigma2) *
|
||||
reconWeight * (0.45 + 0.55 * pc.frameWeight);
|
||||
w *= detailRetention;
|
||||
if (w <= 0.0) continue;
|
||||
|
||||
uint weightFixed = max(1u, uint(clamp(w * kAccumScale, 0.0, 4294967040.0)));
|
||||
@@ -174,22 +195,29 @@ void splatSample(int phaseIndex, vec2 outCenter, vec2 refPos, float val,
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
uint x = gl_GlobalInvocationID.x;
|
||||
uint y = gl_GlobalInvocationID.y;
|
||||
if (x >= pc.sensorWidth || y >= pc.sensorHeight) return;
|
||||
void processGreenPlane(ivec2 samplePlane, int planeIndex) {
|
||||
ivec2 sampleOffset = getBayerOffset(int(pc.cfaPattern), planeIndex);
|
||||
if (samplePlane.x < 0 || samplePlane.y < 0 ||
|
||||
samplePlane.x >= int(pc.planeWidth) || samplePlane.y >= int(pc.planeHeight)) {
|
||||
return;
|
||||
}
|
||||
|
||||
ivec2 samplePos = sampleOffset + samplePlane * 2;
|
||||
if (samplePos.x < 0 || samplePos.y < 0 ||
|
||||
samplePos.x >= int(pc.sensorWidth) || samplePos.y >= int(pc.sensorHeight)) {
|
||||
return;
|
||||
}
|
||||
|
||||
ivec2 samplePos = ivec2(x, y);
|
||||
int lscIdx = -1;
|
||||
int family = getGreenFamily(int(pc.cfaPattern), samplePos, lscIdx);
|
||||
if (family < 0) return;
|
||||
if (family < 0 || family + 1 != planeIndex) return;
|
||||
|
||||
vec2 planePos = vec2(samplePos) * 0.5;
|
||||
vec2 planePos = vec2(samplePlane);
|
||||
vec2 flow = vec2(0.0);
|
||||
if (pc.isFirstFrame == 0u) {
|
||||
flow = sampleFlow(planePos);
|
||||
}
|
||||
vec2 refPos = vec2(samplePos) - flow * 2.0;
|
||||
vec2 refPos = planePos - flow;
|
||||
vec2 outCenter = refPos * pc.scale;
|
||||
|
||||
float raw = texelFetch(inputRawSampler, samplePos, 0).r;
|
||||
@@ -214,3 +242,13 @@ void main() {
|
||||
splatSample(family, outCenter, refPos, val, pInlier * highlightSuppression,
|
||||
flowVar);
|
||||
}
|
||||
|
||||
void main() {
|
||||
uint localX = gl_GlobalInvocationID.x;
|
||||
uint localY = gl_GlobalInvocationID.y;
|
||||
if (localX >= pc.tileW || localY >= pc.tileH) return;
|
||||
|
||||
ivec2 samplePlane = ivec2(int(pc.tileX + localX), int(pc.tileY + localY));
|
||||
processGreenPlane(samplePlane, 1);
|
||||
processGreenPlane(samplePlane, 2);
|
||||
}
|
||||
@@ -39,6 +39,22 @@ layout(push_constant, scalar) uniform PushConstants {
|
||||
float frameWeight;
|
||||
} pc;
|
||||
|
||||
ivec2 getBayerOffset(uint cfaPattern, uint planeIndex) {
|
||||
if (cfaPattern == 0u) {
|
||||
const ivec2 map[4] = ivec2[4](ivec2(0, 0), ivec2(1, 0), ivec2(0, 1), ivec2(1, 1));
|
||||
return map[planeIndex];
|
||||
} else if (cfaPattern == 1u) {
|
||||
const ivec2 map[4] = ivec2[4](ivec2(1, 0), ivec2(0, 0), ivec2(1, 1), ivec2(0, 1));
|
||||
return map[planeIndex];
|
||||
} else if (cfaPattern == 2u) {
|
||||
const ivec2 map[4] = ivec2[4](ivec2(0, 1), ivec2(1, 1), ivec2(0, 0), ivec2(1, 0));
|
||||
return map[planeIndex];
|
||||
} else {
|
||||
const ivec2 map[4] = ivec2[4](ivec2(1, 1), ivec2(0, 1), ivec2(1, 0), ivec2(0, 0));
|
||||
return map[planeIndex];
|
||||
}
|
||||
}
|
||||
|
||||
uvec2 readAccum(int x, int y) {
|
||||
x = clamp(x, 0, int(pc.tileW) - 1);
|
||||
y = clamp(y, 0, int(pc.tileH) - 1);
|
||||
@@ -93,29 +109,26 @@ int getBayerIndex(uint cfaPattern, uvec2 pos) {
|
||||
}
|
||||
}
|
||||
|
||||
bool matchesTargetColor(int bayerIndex, uint planeIndex) {
|
||||
if (planeIndex == 0u) return bayerIndex == 0;
|
||||
if (planeIndex == 2u) return bayerIndex == 3;
|
||||
return false;
|
||||
}
|
||||
|
||||
void main() {
|
||||
uint tx = gl_GlobalInvocationID.x;
|
||||
uint ty = gl_GlobalInvocationID.y;
|
||||
uint globalX = pc.tileX + tx;
|
||||
uint globalY = pc.tileY + ty;
|
||||
if (tx >= pc.tileW || ty >= pc.tileH) return;
|
||||
uint planeX = pc.tileX + tx;
|
||||
uint planeY = pc.tileY + ty;
|
||||
|
||||
ivec2 offset = getBayerOffset(pc.cfaPattern, pc.planeIndex);
|
||||
uint globalX = uint(offset.x) + planeX * 2u;
|
||||
uint globalY = uint(offset.y) + planeY * 2u;
|
||||
if (globalX >= pc.width || globalY >= pc.height) return;
|
||||
|
||||
int bayerIndex = getBayerIndex(pc.cfaPattern, uvec2(globalX, globalY));
|
||||
if (!matchesTargetColor(bayerIndex, pc.planeIndex)) return;
|
||||
int bayerIndex = int(pc.planeIndex);
|
||||
|
||||
uvec2 accum = colorAccum.data[ty * pc.bufferStride + tx];
|
||||
float neighborVal = 0.0;
|
||||
float neighborSupport = 0.0;
|
||||
gatherNeighborhoodEstimate(int(tx), int(ty), neighborVal, neighborSupport);
|
||||
|
||||
uint outIndex = ty * pc.width + globalX;
|
||||
uint outIndex = globalY * pc.width + globalX;
|
||||
float priorNorm = float(priorBuffer.data[outIndex]) / 65535.0;
|
||||
float priorSupport = float(priorWeightBuffer.data[outIndex]) / 65535.0;
|
||||
|
||||
|
||||
@@ -59,6 +59,22 @@ layout(push_constant, scalar) uniform PushConstants {
|
||||
float frameWeight;
|
||||
} pc;
|
||||
|
||||
ivec2 getBayerOffset(uint cfaPattern, uint planeIndex) {
|
||||
if (cfaPattern == 0u) {
|
||||
const ivec2 map[4] = ivec2[4](ivec2(0, 0), ivec2(1, 0), ivec2(0, 1), ivec2(1, 1));
|
||||
return map[planeIndex];
|
||||
} else if (cfaPattern == 1u) {
|
||||
const ivec2 map[4] = ivec2[4](ivec2(1, 0), ivec2(0, 0), ivec2(1, 1), ivec2(0, 1));
|
||||
return map[planeIndex];
|
||||
} else if (cfaPattern == 2u) {
|
||||
const ivec2 map[4] = ivec2[4](ivec2(0, 1), ivec2(1, 1), ivec2(0, 0), ivec2(1, 0));
|
||||
return map[planeIndex];
|
||||
} else {
|
||||
const ivec2 map[4] = ivec2[4](ivec2(1, 1), ivec2(0, 1), ivec2(1, 0), ivec2(0, 0));
|
||||
return map[planeIndex];
|
||||
}
|
||||
}
|
||||
|
||||
float accumMean(uvec2 accum) {
|
||||
if (accum.y == 0u) return 0.0;
|
||||
return float(accum.x) / max(float(accum.y), 1.0) / 65535.0;
|
||||
@@ -123,17 +139,16 @@ bool getGreenSite(uint cfaPattern, uvec2 pos, out int familyIndex, out int bayer
|
||||
void main() {
|
||||
uint tx = gl_GlobalInvocationID.x;
|
||||
uint ty = gl_GlobalInvocationID.y;
|
||||
|
||||
uint globalX = pc.tileX + tx;
|
||||
uint globalY = pc.tileY + ty;
|
||||
if (tx >= pc.tileW || ty >= pc.tileH) return;
|
||||
if (globalX >= pc.width || globalY >= pc.height) return;
|
||||
uint planeX = pc.tileX + tx;
|
||||
uint planeY = pc.tileY + ty;
|
||||
|
||||
int familyIndex = -1;
|
||||
int bayerIndex = -1;
|
||||
if (!getGreenSite(pc.cfaPattern, uvec2(globalX, globalY), familyIndex, bayerIndex)) {
|
||||
return;
|
||||
}
|
||||
int familyIndex = int(pc.planeIndex) - 1;
|
||||
int bayerIndex = int(pc.planeIndex);
|
||||
ivec2 offset = getBayerOffset(pc.cfaPattern, pc.planeIndex);
|
||||
uint globalX = uint(offset.x) + planeX * 2u;
|
||||
uint globalY = uint(offset.y) + planeY * 2u;
|
||||
if (globalX >= pc.width || globalY >= pc.height) return;
|
||||
|
||||
uint idx = ty * pc.bufferStride + tx;
|
||||
uvec2 primary = (familyIndex == 0) ? greenPhase0.data[idx] : greenPhase1.data[idx];
|
||||
@@ -147,7 +162,7 @@ void main() {
|
||||
gatherGreenNeighborhood(1 - familyIndex, int(tx), int(ty),
|
||||
secondaryNeighborVal, secondaryNeighborSupport);
|
||||
|
||||
uint outIndex = ty * pc.width + globalX;
|
||||
uint outIndex = globalY * pc.width + globalX;
|
||||
float priorNorm = float(priorBuffer.data[outIndex]) / 65535.0;
|
||||
float priorSupport = float(priorWeightBuffer.data[outIndex]) / 65535.0;
|
||||
|
||||
|
||||
@@ -110,8 +110,12 @@ void samplePrior(int bayerIndex, vec2 sourcePos,
|
||||
}
|
||||
|
||||
void main() {
|
||||
uint x = gl_GlobalInvocationID.x;
|
||||
uint y = gl_GlobalInvocationID.y;
|
||||
uint tx = gl_GlobalInvocationID.x;
|
||||
uint ty = gl_GlobalInvocationID.y;
|
||||
if (tx >= pc.tileW || ty >= pc.tileH) return;
|
||||
|
||||
uint x = pc.tileX + tx;
|
||||
uint y = pc.tileY + ty;
|
||||
if (x >= pc.width || y >= pc.height) return;
|
||||
|
||||
uint outIndex = y * pc.width + x;
|
||||
|
||||
@@ -229,9 +229,11 @@ void VulkanRawStacker::initVulkanResources() {
|
||||
|
||||
uint32_t tileW = (planeW + numTilesX - 1) / numTilesX;
|
||||
uint32_t tileH = (planeH + numTilesY - 1) / numTilesY;
|
||||
uint32_t stride = tileW + 16;
|
||||
uint32_t planeTileW = (tileW + 1) / 2;
|
||||
uint32_t planeTileH = (tileH + 1) / 2;
|
||||
uint32_t stride = planeTileW + 16;
|
||||
VkDeviceSize accumBufferSize =
|
||||
(VkDeviceSize)stride * (tileH + 16) * sizeof(float) * 2;
|
||||
(VkDeviceSize)stride * (planeTileH + 16) * sizeof(float) * 2;
|
||||
|
||||
alignLkSets.resize(1);
|
||||
robustnessSets.resize(1);
|
||||
@@ -379,7 +381,7 @@ void VulkanRawStacker::initVulkanResources() {
|
||||
|
||||
// 3. Green HR phase accumulators: atomic uint(sum, weight) per output pixel
|
||||
VkDeviceSize greenAccumSize =
|
||||
(VkDeviceSize)stride * (tileH + 16) * sizeof(uint32_t) * 2;
|
||||
(VkDeviceSize)stride * (planeTileH + 16) * sizeof(uint32_t) * 2;
|
||||
for (int phase = 0; phase < 2; ++phase) {
|
||||
VkBufferCreateInfo gInfo{VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO};
|
||||
gInfo.size = greenAccumSize;
|
||||
@@ -395,8 +397,7 @@ void VulkanRawStacker::initVulkanResources() {
|
||||
VkMemoryAllocateInfo gAlloc{VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO};
|
||||
gAlloc.allocationSize = gReqs.size;
|
||||
gAlloc.memoryTypeIndex = vm.findMemoryType(
|
||||
gReqs.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
|
||||
VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
|
||||
gReqs.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
|
||||
VK_CHECK(vkAllocateMemory(device, &gAlloc, nullptr,
|
||||
&greenPhaseAccumMemories[phase]));
|
||||
vkBindBufferMemory(device, greenPhaseAccumBuffers[phase],
|
||||
@@ -801,7 +802,7 @@ void VulkanRawStacker::createPipelines() {
|
||||
green_scatter_raw_comp_spv_size);
|
||||
|
||||
// --- 5. Color Scatter Pipeline (R/B) ---
|
||||
VkDescriptorSetLayoutBinding csBindings[7] = {};
|
||||
VkDescriptorSetLayoutBinding csBindings[8] = {};
|
||||
csBindings[0] = {0, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1,
|
||||
VK_SHADER_STAGE_COMPUTE_BIT, nullptr};
|
||||
csBindings[1] = {1, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1,
|
||||
@@ -814,12 +815,14 @@ void VulkanRawStacker::createPipelines() {
|
||||
VK_SHADER_STAGE_COMPUTE_BIT, nullptr};
|
||||
csBindings[5] = {5, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1,
|
||||
VK_SHADER_STAGE_COMPUTE_BIT, nullptr};
|
||||
csBindings[6] = {6, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1,
|
||||
csBindings[6] = {6, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1,
|
||||
VK_SHADER_STAGE_COMPUTE_BIT, nullptr};
|
||||
csBindings[7] = {7, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1,
|
||||
VK_SHADER_STAGE_COMPUTE_BIT, nullptr};
|
||||
|
||||
VkDescriptorSetLayoutCreateInfo csLayoutInfo{
|
||||
VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO};
|
||||
csLayoutInfo.bindingCount = 7;
|
||||
csLayoutInfo.bindingCount = 8;
|
||||
csLayoutInfo.pBindings = csBindings;
|
||||
vkCreateDescriptorSetLayout(device, &csLayoutInfo, nullptr,
|
||||
&colorScatterSetLayout);
|
||||
@@ -1101,6 +1104,69 @@ struct RefinedFrameCache {
|
||||
bool valid = false;
|
||||
};
|
||||
|
||||
struct PlaneTileRange {
|
||||
uint32_t startX = 0;
|
||||
uint32_t startY = 0;
|
||||
uint32_t width = 0;
|
||||
uint32_t height = 0;
|
||||
};
|
||||
|
||||
inline Point getBayerPlaneOffset(int cfaPattern, int planeIndex) {
|
||||
if (cfaPattern == 0) {
|
||||
const Point map[4] = {{0, 0}, {1, 0}, {0, 1}, {1, 1}};
|
||||
return map[planeIndex];
|
||||
} else if (cfaPattern == 1) {
|
||||
const Point map[4] = {{1, 0}, {0, 0}, {1, 1}, {0, 1}};
|
||||
return map[planeIndex];
|
||||
} else if (cfaPattern == 2) {
|
||||
const Point map[4] = {{0, 1}, {1, 1}, {0, 0}, {1, 0}};
|
||||
return map[planeIndex];
|
||||
} else {
|
||||
const Point map[4] = {{1, 1}, {0, 1}, {1, 0}, {0, 0}};
|
||||
return map[planeIndex];
|
||||
}
|
||||
}
|
||||
|
||||
inline uint32_t planeSampleCount(uint32_t extent, int offset) {
|
||||
if (extent <= (uint32_t)offset)
|
||||
return 0;
|
||||
return (extent - 1u - (uint32_t)offset) / 2u + 1u;
|
||||
}
|
||||
|
||||
inline PlaneTileRange computePlaneTileRange(int cfaPattern, int planeIndex,
|
||||
uint32_t tileX, uint32_t tileY,
|
||||
uint32_t tileW, uint32_t tileH,
|
||||
uint32_t outputW,
|
||||
uint32_t outputH) {
|
||||
PlaneTileRange range;
|
||||
const Point offset = getBayerPlaneOffset(cfaPattern, planeIndex);
|
||||
const uint32_t planeW = planeSampleCount(outputW, (int)offset.x);
|
||||
const uint32_t planeH = planeSampleCount(outputH, (int)offset.y);
|
||||
if (planeW == 0 || planeH == 0 || tileW == 0 || tileH == 0)
|
||||
return range;
|
||||
|
||||
const int bayerX0 = (int)tileX;
|
||||
const int bayerY0 = (int)tileY;
|
||||
const int bayerX1 = (int)(tileX + tileW) - 1;
|
||||
const int bayerY1 = (int)(tileY + tileH) - 1;
|
||||
|
||||
const int planeX0 = std::max(0, (bayerX0 - (int)offset.x + 1) / 2);
|
||||
const int planeY0 = std::max(0, (bayerY0 - (int)offset.y + 1) / 2);
|
||||
const int planeX1 =
|
||||
std::min((int)planeW - 1, (bayerX1 - (int)offset.x) / 2);
|
||||
const int planeY1 =
|
||||
std::min((int)planeH - 1, (bayerY1 - (int)offset.y) / 2);
|
||||
|
||||
if (planeX1 < planeX0 || planeY1 < planeY0)
|
||||
return range;
|
||||
|
||||
range.startX = (uint32_t)planeX0;
|
||||
range.startY = (uint32_t)planeY0;
|
||||
range.width = (uint32_t)(planeX1 - planeX0 + 1);
|
||||
range.height = (uint32_t)(planeY1 - planeY0 + 1);
|
||||
return range;
|
||||
}
|
||||
|
||||
struct GreenPhaseSummary {
|
||||
ScalarStats phase0Mean;
|
||||
ScalarStats phase1Mean;
|
||||
@@ -2008,9 +2074,7 @@ bool VulkanRawStacker::processStack(uint16_t *outBuffer, size_t bufferSize) {
|
||||
VK_CHECK(vkAllocateDescriptorSets(device, &allocInfo, &greenScatterSet));
|
||||
|
||||
allocInfo.pSetLayouts = &colorScatterSetLayout;
|
||||
for (int c = 0; c < 2; ++c) {
|
||||
VK_CHECK(vkAllocateDescriptorSets(device, &allocInfo, &colorScatterSets[c]));
|
||||
}
|
||||
VK_CHECK(vkAllocateDescriptorSets(device, &allocInfo, &colorScatterSets[0]));
|
||||
|
||||
allocInfo.pSetLayouts = &referenceNormalizeSetLayout;
|
||||
VK_CHECK(vkAllocateDescriptorSets(device, &allocInfo, &referenceNormalizeSet));
|
||||
@@ -2055,20 +2119,20 @@ bool VulkanRawStacker::processStack(uint16_t *outBuffer, size_t bufferSize) {
|
||||
updateImageDescriptorSet(device, greenScatterSet, lscView, lscSampler, 7);
|
||||
updateBufferDescriptorSet(device, greenScatterSet, localTileMaskBuffer,
|
||||
VK_WHOLE_SIZE, 8);
|
||||
for (int c = 0; c < 2; ++c) {
|
||||
updateBufferDescriptorSet(device, colorScatterSets[c], alignmentBuffer,
|
||||
VK_WHOLE_SIZE, 1);
|
||||
updateBufferDescriptorSet(device, colorScatterSets[c], kernelBuffer,
|
||||
VK_WHOLE_SIZE, 2);
|
||||
updateBufferDescriptorSet(device, colorScatterSets[c], robustnessBuffer,
|
||||
VK_WHOLE_SIZE, 3);
|
||||
updateBufferDescriptorSet(device, colorScatterSets[c],
|
||||
rbScatterAccumBuffers[c], VK_WHOLE_SIZE, 4);
|
||||
updateBufferDescriptorSet(device, colorScatterSets[c], flowVarianceBuffer,
|
||||
VK_WHOLE_SIZE, 5);
|
||||
updateImageDescriptorSet(device, colorScatterSets[c], lscView, lscSampler,
|
||||
6);
|
||||
}
|
||||
updateBufferDescriptorSet(device, colorScatterSets[0], alignmentBuffer,
|
||||
VK_WHOLE_SIZE, 1);
|
||||
updateBufferDescriptorSet(device, colorScatterSets[0], kernelBuffer,
|
||||
VK_WHOLE_SIZE, 2);
|
||||
updateBufferDescriptorSet(device, colorScatterSets[0], robustnessBuffer,
|
||||
VK_WHOLE_SIZE, 3);
|
||||
updateBufferDescriptorSet(device, colorScatterSets[0],
|
||||
rbScatterAccumBuffers[0], VK_WHOLE_SIZE, 4);
|
||||
updateBufferDescriptorSet(device, colorScatterSets[0],
|
||||
rbScatterAccumBuffers[1], VK_WHOLE_SIZE, 5);
|
||||
updateBufferDescriptorSet(device, colorScatterSets[0], flowVarianceBuffer,
|
||||
VK_WHOLE_SIZE, 6);
|
||||
updateImageDescriptorSet(device, colorScatterSets[0], lscView, lscSampler,
|
||||
7);
|
||||
updateBufferDescriptorSet(device, referenceNormalizeSet,
|
||||
normalizedReferenceBuffer, VK_WHOLE_SIZE, 1);
|
||||
updateImageDescriptorSet(device, referenceNormalizeSet, lscView, lscSampler,
|
||||
@@ -2187,7 +2251,8 @@ bool VulkanRawStacker::processStack(uint16_t *outBuffer, size_t bufferSize) {
|
||||
perf.phase1UploadAndPyramidMs = elapsed_phase1_UploadAndPyramid;
|
||||
|
||||
// =====================================================================
|
||||
// Phase 1B: Build dense HR Bayer prior from the reference frame
|
||||
// Phase 1B: Normalize the reference frame once. The HR Bayer prior is now
|
||||
// generated lazily per output tile to avoid a full-frame 6144x4608 pass.
|
||||
// =====================================================================
|
||||
{
|
||||
TIME_START(phase1b_ReferencePrior);
|
||||
@@ -2228,41 +2293,6 @@ bool VulkanRawStacker::processStack(uint16_t *outBuffer, size_t bufferSize) {
|
||||
VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0, 0, nullptr,
|
||||
1, &normalizedReferenceBarrier, 0, nullptr);
|
||||
|
||||
vkCmdBindPipeline(cb, VK_PIPELINE_BIND_POINT_COMPUTE,
|
||||
referencePriorPipeline);
|
||||
vkCmdBindDescriptorSets(cb, VK_PIPELINE_BIND_POINT_COMPUTE,
|
||||
referencePriorPipelineLayout, 0, 1,
|
||||
&referencePriorSet, 0, nullptr);
|
||||
|
||||
PushConstants priorPc{};
|
||||
priorPc.width = outputW;
|
||||
priorPc.height = outputH;
|
||||
priorPc.planeWidth = outputW;
|
||||
priorPc.planeHeight = outputH;
|
||||
priorPc.sensorWidth = width;
|
||||
priorPc.sensorHeight = height;
|
||||
priorPc.scale = scale;
|
||||
priorPc.cfaPattern = (uint32_t)mCfaPattern;
|
||||
priorPc.whiteLevel = mWhiteLevel;
|
||||
memcpy(priorPc.blackLevel, mBlackLevel, 4 * sizeof(float));
|
||||
memcpy(priorPc.wbGains, mWbGains, 4 * sizeof(float));
|
||||
vkCmdPushConstants(cb, referencePriorPipelineLayout,
|
||||
VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof(priorPc),
|
||||
&priorPc);
|
||||
vkCmdDispatch(cb, (outputW + 15) / 16, (outputH + 15) / 16, 1);
|
||||
|
||||
VkBufferMemoryBarrier priorBarriers[2]{};
|
||||
for (int i = 0; i < 2; ++i) {
|
||||
priorBarriers[i].sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER;
|
||||
priorBarriers[i].srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT;
|
||||
priorBarriers[i].dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
|
||||
priorBarriers[i].buffer = (i == 0) ? priorBayerBuffer : priorWeightBuffer;
|
||||
priorBarriers[i].size = VK_WHOLE_SIZE;
|
||||
}
|
||||
vkCmdPipelineBarrier(cb, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
|
||||
VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0, 0, nullptr,
|
||||
2, priorBarriers, 0, nullptr);
|
||||
|
||||
vm.endSingleTimeCommands(cb);
|
||||
TIME_END(phase1b_ReferencePrior);
|
||||
perf.referencePriorMs = elapsed_phase1b_ReferencePrior;
|
||||
@@ -2334,10 +2364,12 @@ bool VulkanRawStacker::processStack(uint16_t *outBuffer, size_t bufferSize) {
|
||||
std::vector<float> frameFusionWeights(pendingFrames.size(), 1.0f);
|
||||
std::vector<float> frameRetentionScores(pendingFrames.size(), 1.0f);
|
||||
std::vector<bool> skipFusionFrame(pendingFrames.size(), false);
|
||||
std::vector<bool> runGlobalReliability(pendingFrames.size(), false);
|
||||
std::vector<LocalReliabilitySummary> frameLocalSummaries(pendingFrames.size());
|
||||
std::vector<LocalReliabilityMap> frameLocalMaps(pendingFrames.size());
|
||||
std::vector<RefinedFrameCache> frameRefinedCaches(pendingFrames.size());
|
||||
const float referenceScore = std::max(pendingFrames.front().score, 1.0f);
|
||||
size_t coarseEligibleFrames = 0;
|
||||
for (size_t i = 1; i < pendingFrames.size(); ++i) {
|
||||
const auto coarseAlignStart = PerfClock::now();
|
||||
frameAlignments[i].coarseAlign =
|
||||
@@ -2350,17 +2382,43 @@ bool VulkanRawStacker::processStack(uint16_t *outBuffer, size_t bufferSize) {
|
||||
sharpnessRatio);
|
||||
frameFusionWeights[i] = frameWeight;
|
||||
skipFusionFrame[i] = frameWeight < 0.12f;
|
||||
if (!skipFusionFrame[i]) {
|
||||
++coarseEligibleFrames;
|
||||
}
|
||||
}
|
||||
TIME_END(phase3_CoarseAlignment);
|
||||
perf.coarseAlignmentMs = elapsed_phase3_CoarseAlignment;
|
||||
|
||||
{
|
||||
std::vector<std::pair<float, size_t>> coarseCandidates;
|
||||
coarseCandidates.reserve(coarseEligibleFrames);
|
||||
for (size_t i = 1; i < pendingFrames.size(); ++i) {
|
||||
if (skipFusionFrame[i]) {
|
||||
continue;
|
||||
}
|
||||
coarseCandidates.emplace_back(frameFusionWeights[i], i);
|
||||
}
|
||||
std::sort(coarseCandidates.begin(), coarseCandidates.end(),
|
||||
[](const auto &lhs, const auto &rhs) {
|
||||
return lhs.first > rhs.first;
|
||||
});
|
||||
|
||||
const size_t refinedBudget = std::min<size_t>(
|
||||
coarseCandidates.size(),
|
||||
std::clamp<size_t>(
|
||||
(size_t)std::ceil((double)coarseCandidates.size() * 0.35), 2, 5));
|
||||
for (size_t rank = 0; rank < refinedBudget; ++rank) {
|
||||
runGlobalReliability[coarseCandidates[rank].second] = true;
|
||||
}
|
||||
}
|
||||
|
||||
// =====================================================================
|
||||
// Phase 3B: Global reliability pre-pass using actual refined flow +
|
||||
// robustness. This lets us keep only the frames that genuinely help.
|
||||
// =====================================================================
|
||||
TIME_START(phase3b_GlobalReliability);
|
||||
for (size_t i = 1; i < pendingFrames.size(); ++i) {
|
||||
if (skipFusionFrame[i]) {
|
||||
if (skipFusionFrame[i] || !runGlobalReliability[i]) {
|
||||
continue;
|
||||
}
|
||||
++perf.globalReliabilityFrames;
|
||||
@@ -2672,6 +2730,212 @@ bool VulkanRawStacker::processStack(uint16_t *outBuffer, size_t bufferSize) {
|
||||
TIME_END(phase3b_GlobalReliability);
|
||||
perf.globalReliabilityMs = elapsed_phase3b_GlobalReliability;
|
||||
|
||||
auto ensureRefinedFrameCache = [&](size_t frameIndex) -> bool {
|
||||
if (frameIndex == 0 || frameIndex >= pendingFrames.size() ||
|
||||
frameRefinedCaches[frameIndex].valid || skipFusionFrame[frameIndex]) {
|
||||
return frameRefinedCaches[frameIndex].valid;
|
||||
}
|
||||
|
||||
PushConstants pc{};
|
||||
pc.width = outputW;
|
||||
pc.height = outputH;
|
||||
pc.planeWidth = inputW;
|
||||
pc.planeHeight = inputH;
|
||||
pc.sensorWidth = width;
|
||||
pc.sensorHeight = height;
|
||||
pc.scale = (float)scale;
|
||||
pc.cfaPattern = (uint32_t)pendingFrames[frameIndex].cfaPattern;
|
||||
memcpy(pc.blackLevel, mBlackLevel, 4 * sizeof(float));
|
||||
pc.whiteLevel = mWhiteLevel;
|
||||
memcpy(pc.wbGains, mWbGains, 4 * sizeof(float));
|
||||
pc.gridW = gridW;
|
||||
pc.gridH = gridH;
|
||||
pc.tileSize = (uint32_t)tileSize;
|
||||
pc.noiseAlpha = mNoiseModel[0] / 65535.0f;
|
||||
pc.noiseBeta = mNoiseModel[1] / (65535.0f * 65535.0f);
|
||||
pc.baseNoise = pc.noiseBeta;
|
||||
pc.frameWeight = frameFusionWeights[frameIndex];
|
||||
pc.tileX = 0;
|
||||
pc.tileY = 0;
|
||||
pc.tileW = inputW;
|
||||
pc.tileH = inputH;
|
||||
pc.bufferStride = tileW + 16;
|
||||
|
||||
const auto &coarseAlign = frameAlignments[frameIndex].coarseAlign;
|
||||
std::vector<Point> seededFlow = buildDenseAlignmentSeed(
|
||||
coarseAlign, (uint32_t)gridW, (uint32_t)gridH, (uint32_t)tileSize);
|
||||
uploadAlignmentField(device, alignmentMemory, seededFlow);
|
||||
fillFloatBuffer(device, robustnessMemory, (size_t)inputW * inputH, 1.0f);
|
||||
|
||||
VkCommandBuffer cb = vm.beginSingleTimeCommands();
|
||||
VkBufferMemoryBarrier hostBarriers[2]{};
|
||||
hostBarriers[0].sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER;
|
||||
hostBarriers[0].srcAccessMask = VK_ACCESS_HOST_WRITE_BIT;
|
||||
hostBarriers[0].dstAccessMask =
|
||||
VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT;
|
||||
hostBarriers[0].buffer = alignmentBuffer;
|
||||
hostBarriers[0].size = VK_WHOLE_SIZE;
|
||||
hostBarriers[1].sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER;
|
||||
hostBarriers[1].srcAccessMask = VK_ACCESS_HOST_WRITE_BIT;
|
||||
hostBarriers[1].dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
|
||||
hostBarriers[1].buffer = robustnessBuffer;
|
||||
hostBarriers[1].size = VK_WHOLE_SIZE;
|
||||
vkCmdPipelineBarrier(cb, VK_PIPELINE_STAGE_HOST_BIT,
|
||||
VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0, 0, nullptr,
|
||||
2, hostBarriers, 0, nullptr);
|
||||
|
||||
vkCmdBindPipeline(cb, VK_PIPELINE_BIND_POINT_COMPUTE, alignLkPipeline);
|
||||
updateImageDescriptorSet(device, alignSet, uploadedFrames[0].view, sampler,
|
||||
0);
|
||||
updateImageDescriptorSet(device, alignSet,
|
||||
uploadedFrames[frameIndex].view, sampler, 1);
|
||||
vkCmdBindDescriptorSets(cb, VK_PIPELINE_BIND_POINT_COMPUTE,
|
||||
alignLkPipelineLayout, 0, 1, &alignSet, 0,
|
||||
nullptr);
|
||||
vkCmdPushConstants(cb, alignLkPipelineLayout, VK_SHADER_STAGE_COMPUTE_BIT,
|
||||
0, sizeof(pc), &pc);
|
||||
for (int iter = 0; iter < 5; ++iter) {
|
||||
vkCmdDispatch(cb, (gridW + 15) / 16, (gridH + 15) / 16, 1);
|
||||
VkBufferMemoryBarrier lkBarrier{
|
||||
VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER};
|
||||
lkBarrier.srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT;
|
||||
lkBarrier.dstAccessMask =
|
||||
VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT;
|
||||
lkBarrier.buffer = alignmentBuffer;
|
||||
lkBarrier.size = VK_WHOLE_SIZE;
|
||||
vkCmdPipelineBarrier(cb, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
|
||||
VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0, 0, nullptr,
|
||||
1, &lkBarrier, 0, nullptr);
|
||||
}
|
||||
vm.endSingleTimeCommands(cb);
|
||||
|
||||
if (downloadAlignmentField(device, alignmentMemory, (uint32_t)gridW,
|
||||
(uint32_t)gridH, seededFlow)) {
|
||||
regularizeAlignmentField(seededFlow, (uint32_t)gridW, (uint32_t)gridH,
|
||||
(uint32_t)tileSize, uploadedFrames[0].proxy);
|
||||
uploadAlignmentField(device, alignmentMemory, seededFlow);
|
||||
}
|
||||
|
||||
cb = vm.beginSingleTimeCommands();
|
||||
VkBufferMemoryBarrier refinedHostBarrier{
|
||||
VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER};
|
||||
refinedHostBarrier.srcAccessMask = VK_ACCESS_HOST_WRITE_BIT;
|
||||
refinedHostBarrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
|
||||
refinedHostBarrier.buffer = alignmentBuffer;
|
||||
refinedHostBarrier.size = VK_WHOLE_SIZE;
|
||||
vkCmdPipelineBarrier(cb, VK_PIPELINE_STAGE_HOST_BIT,
|
||||
VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0, 0, nullptr,
|
||||
1, &refinedHostBarrier, 0, nullptr);
|
||||
|
||||
vkCmdBindPipeline(cb, VK_PIPELINE_BIND_POINT_COMPUTE, robustnessPipeline);
|
||||
updateImageDescriptorSet(device, robustSet, uploadedFrames[0].view, sampler,
|
||||
0);
|
||||
updateImageDescriptorSet(device, robustSet,
|
||||
uploadedFrames[frameIndex].view, sampler, 1);
|
||||
vkCmdBindDescriptorSets(cb, VK_PIPELINE_BIND_POINT_COMPUTE,
|
||||
robustnessPipelineLayout, 0, 1, &robustSet, 0,
|
||||
nullptr);
|
||||
vkCmdPushConstants(cb, robustnessPipelineLayout,
|
||||
VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof(pc), &pc);
|
||||
vkCmdDispatch(cb, (inputW + 15) / 16, (inputH + 15) / 16, 1);
|
||||
|
||||
VkBufferMemoryBarrier rbBarriers[2]{};
|
||||
rbBarriers[0].sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER;
|
||||
rbBarriers[0].srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT;
|
||||
rbBarriers[0].dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
|
||||
rbBarriers[0].buffer = robustnessBuffer;
|
||||
rbBarriers[0].size = VK_WHOLE_SIZE;
|
||||
rbBarriers[1].sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER;
|
||||
rbBarriers[1].srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT;
|
||||
rbBarriers[1].dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
|
||||
rbBarriers[1].buffer = flowVarianceBuffer;
|
||||
rbBarriers[1].size = VK_WHOLE_SIZE;
|
||||
vkCmdPipelineBarrier(cb, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
|
||||
VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0, 0, nullptr, 2,
|
||||
rbBarriers, 0, nullptr);
|
||||
|
||||
vkCmdBindPipeline(cb, VK_PIPELINE_BIND_POINT_COMPUTE, alignLkPipeline);
|
||||
updateImageDescriptorSet(device, alignSet, uploadedFrames[0].view, sampler,
|
||||
0);
|
||||
updateImageDescriptorSet(device, alignSet,
|
||||
uploadedFrames[frameIndex].view, sampler, 1);
|
||||
vkCmdBindDescriptorSets(cb, VK_PIPELINE_BIND_POINT_COMPUTE,
|
||||
alignLkPipelineLayout, 0, 1, &alignSet, 0,
|
||||
nullptr);
|
||||
vkCmdPushConstants(cb, alignLkPipelineLayout, VK_SHADER_STAGE_COMPUTE_BIT,
|
||||
0, sizeof(pc), &pc);
|
||||
for (int iter = 0; iter < 2; ++iter) {
|
||||
vkCmdDispatch(cb, (gridW + 15) / 16, (gridH + 15) / 16, 1);
|
||||
VkBufferMemoryBarrier lkBarrier{
|
||||
VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER};
|
||||
lkBarrier.srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT;
|
||||
lkBarrier.dstAccessMask =
|
||||
VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT;
|
||||
lkBarrier.buffer = alignmentBuffer;
|
||||
lkBarrier.size = VK_WHOLE_SIZE;
|
||||
vkCmdPipelineBarrier(cb, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
|
||||
VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0, 0, nullptr,
|
||||
1, &lkBarrier, 0, nullptr);
|
||||
}
|
||||
vm.endSingleTimeCommands(cb);
|
||||
|
||||
if (downloadAlignmentField(device, alignmentMemory, (uint32_t)gridW,
|
||||
(uint32_t)gridH, seededFlow)) {
|
||||
regularizeAlignmentField(seededFlow, (uint32_t)gridW, (uint32_t)gridH,
|
||||
(uint32_t)tileSize, uploadedFrames[0].proxy);
|
||||
uploadAlignmentField(device, alignmentMemory, seededFlow);
|
||||
}
|
||||
|
||||
cb = vm.beginSingleTimeCommands();
|
||||
VkBufferMemoryBarrier finalAlignBarrier{
|
||||
VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER};
|
||||
finalAlignBarrier.srcAccessMask = VK_ACCESS_HOST_WRITE_BIT;
|
||||
finalAlignBarrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
|
||||
finalAlignBarrier.buffer = alignmentBuffer;
|
||||
finalAlignBarrier.size = VK_WHOLE_SIZE;
|
||||
vkCmdPipelineBarrier(cb, VK_PIPELINE_STAGE_HOST_BIT,
|
||||
VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0, 0, nullptr,
|
||||
1, &finalAlignBarrier, 0, nullptr);
|
||||
|
||||
vkCmdBindPipeline(cb, VK_PIPELINE_BIND_POINT_COMPUTE, robustnessPipeline);
|
||||
updateImageDescriptorSet(device, robustSet, uploadedFrames[0].view, sampler,
|
||||
0);
|
||||
updateImageDescriptorSet(device, robustSet,
|
||||
uploadedFrames[frameIndex].view, sampler, 1);
|
||||
vkCmdBindDescriptorSets(cb, VK_PIPELINE_BIND_POINT_COMPUTE,
|
||||
robustnessPipelineLayout, 0, 1, &robustSet, 0,
|
||||
nullptr);
|
||||
vkCmdPushConstants(cb, robustnessPipelineLayout,
|
||||
VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof(pc), &pc);
|
||||
vkCmdDispatch(cb, (inputW + 15) / 16, (inputH + 15) / 16, 1);
|
||||
|
||||
VkBufferMemoryBarrier finalRbBarriers[2]{};
|
||||
finalRbBarriers[0].sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER;
|
||||
finalRbBarriers[0].srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT;
|
||||
finalRbBarriers[0].dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
|
||||
finalRbBarriers[0].buffer = robustnessBuffer;
|
||||
finalRbBarriers[0].size = VK_WHOLE_SIZE;
|
||||
finalRbBarriers[1].sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER;
|
||||
finalRbBarriers[1].srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT;
|
||||
finalRbBarriers[1].dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
|
||||
finalRbBarriers[1].buffer = flowVarianceBuffer;
|
||||
finalRbBarriers[1].size = VK_WHOLE_SIZE;
|
||||
vkCmdPipelineBarrier(cb, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
|
||||
VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0, 0, nullptr, 2,
|
||||
finalRbBarriers, 0, nullptr);
|
||||
vm.endSingleTimeCommands(cb);
|
||||
|
||||
RefinedFrameCache &refinedCache = frameRefinedCaches[frameIndex];
|
||||
refinedCache.alignment = seededFlow;
|
||||
refinedCache.valid = downloadFloatBuffer(device, robustnessMemory,
|
||||
(size_t)inputW * inputH,
|
||||
refinedCache.robustness) &&
|
||||
downloadFloatBuffer(device, flowVarianceMemory,
|
||||
(size_t)inputW * inputH,
|
||||
refinedCache.flowVariance);
|
||||
return refinedCache.valid;
|
||||
};
|
||||
|
||||
perf.keptFrames = pendingFrames.empty() ? 0 : 1;
|
||||
for (size_t i = 1; i < pendingFrames.size(); ++i) {
|
||||
if (skipFusionFrame[i]) {
|
||||
@@ -2689,8 +2953,11 @@ bool VulkanRawStacker::processStack(uint16_t *outBuffer, size_t bufferSize) {
|
||||
// For each tile: clear accum → accumulate all frames → normalize
|
||||
// =====================================================================
|
||||
TIME_START(phase4_TileProcessing);
|
||||
const uint32_t maxPlaneTileW = (tileW + 1) / 2;
|
||||
const uint32_t maxPlaneTileH = (tileH + 1) / 2;
|
||||
VkDeviceSize greenAccumBufferSize =
|
||||
(VkDeviceSize)(tileW + 16) * (tileH + 16) * sizeof(uint32_t) * 2;
|
||||
(VkDeviceSize)(maxPlaneTileW + 16) * (maxPlaneTileH + 16) *
|
||||
sizeof(uint32_t) * 2;
|
||||
VkDeviceSize fusedBayerSize =
|
||||
(VkDeviceSize)outputW * outputH * sizeof(uint16_t);
|
||||
|
||||
@@ -2717,6 +2984,8 @@ bool VulkanRawStacker::processStack(uint16_t *outBuffer, size_t bufferSize) {
|
||||
++perf.tilesProcessed;
|
||||
uint32_t currentTileW = std::min(tileW, outputW - tx * tileW);
|
||||
uint32_t currentTileH = std::min(tileH, outputH - ty * tileH);
|
||||
const uint32_t tileOriginX = tx * tileW;
|
||||
const uint32_t tileOriginY = ty * tileH;
|
||||
|
||||
// Clear accumulators for this tile
|
||||
{
|
||||
@@ -2786,6 +3055,9 @@ bool VulkanRawStacker::processStack(uint16_t *outBuffer, size_t bufferSize) {
|
||||
vkUnmapMemory(device, localTileMaskMemory);
|
||||
}
|
||||
perf.localMaskUploadMs += elapsedMillis(localMaskUploadStart);
|
||||
if (!isRef && !frameRefinedCaches[i].valid) {
|
||||
ensureRefinedFrameCache(i);
|
||||
}
|
||||
const RefinedFrameCache *refinedCache =
|
||||
(!isRef && frameRefinedCaches[i].valid) ? &frameRefinedCaches[i]
|
||||
: nullptr;
|
||||
@@ -3072,17 +3344,40 @@ bool VulkanRawStacker::processStack(uint16_t *outBuffer, size_t bufferSize) {
|
||||
greenScatterPipelineLayout, 0, 1,
|
||||
&greenScatterSet, 0, nullptr);
|
||||
|
||||
pc.isFirstFrame = isRef ? 1 : 0;
|
||||
pc.outputChannel = 1;
|
||||
pc.bufferStride = tileW + 16;
|
||||
pc.tileX = tx * tileW;
|
||||
pc.tileY = ty * tileH;
|
||||
pc.tileW = currentTileW;
|
||||
pc.tileH = currentTileH;
|
||||
PlaneTileRange greenRange0 = computePlaneTileRange(
|
||||
frame.cfaPattern, 1, tileOriginX, tileOriginY, currentTileW,
|
||||
currentTileH, outputW, outputH);
|
||||
PlaneTileRange greenRange1 = computePlaneTileRange(
|
||||
frame.cfaPattern, 2, tileOriginX, tileOriginY, currentTileW,
|
||||
currentTileH, outputW, outputH);
|
||||
if (greenRange0.width > 0 || greenRange1.width > 0) {
|
||||
uint32_t greenStartX = std::min(
|
||||
greenRange0.width > 0 ? greenRange0.startX : greenRange1.startX,
|
||||
greenRange1.width > 0 ? greenRange1.startX : greenRange0.startX);
|
||||
uint32_t greenStartY = std::min(
|
||||
greenRange0.height > 0 ? greenRange0.startY : greenRange1.startY,
|
||||
greenRange1.height > 0 ? greenRange1.startY : greenRange0.startY);
|
||||
uint32_t greenEndX = std::max(
|
||||
greenRange0.startX + greenRange0.width,
|
||||
greenRange1.startX + greenRange1.width);
|
||||
uint32_t greenEndY = std::max(
|
||||
greenRange0.startY + greenRange0.height,
|
||||
greenRange1.startY + greenRange1.height);
|
||||
|
||||
vkCmdPushConstants(cb, greenScatterPipelineLayout,
|
||||
VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof(pc), &pc);
|
||||
vkCmdDispatch(cb, (width + 15) / 16, (height + 15) / 16, 1);
|
||||
pc.isFirstFrame = isRef ? 1 : 0;
|
||||
pc.outputChannel = 1;
|
||||
pc.planeIndex = 1;
|
||||
pc.bufferStride = (greenEndX - greenStartX) + 16;
|
||||
pc.tileX = greenStartX;
|
||||
pc.tileY = greenStartY;
|
||||
pc.tileW = greenEndX - greenStartX;
|
||||
pc.tileH = greenEndY - greenStartY;
|
||||
|
||||
vkCmdPushConstants(cb, greenScatterPipelineLayout,
|
||||
VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof(pc),
|
||||
&pc);
|
||||
vkCmdDispatch(cb, (pc.tileW + 15) / 16, (pc.tileH + 15) / 16, 1);
|
||||
}
|
||||
|
||||
VkBufferMemoryBarrier greenBarriers[2]{};
|
||||
for (int phase = 0; phase < 2; ++phase) {
|
||||
@@ -3099,26 +3394,42 @@ bool VulkanRawStacker::processStack(uint16_t *outBuffer, size_t bufferSize) {
|
||||
|
||||
vkCmdBindPipeline(cb, VK_PIPELINE_BIND_POINT_COMPUTE,
|
||||
colorScatterPipeline);
|
||||
for (int colorIdx = 0; colorIdx < 2; ++colorIdx) {
|
||||
updateImageDescriptorSet(device, colorScatterSets[colorIdx],
|
||||
uploadedFrames[i].view, sampler, 0);
|
||||
vkCmdBindDescriptorSets(cb, VK_PIPELINE_BIND_POINT_COMPUTE,
|
||||
colorScatterPipelineLayout, 0, 1,
|
||||
&colorScatterSets[colorIdx], 0, nullptr);
|
||||
updateImageDescriptorSet(device, colorScatterSets[0],
|
||||
uploadedFrames[i].view, sampler, 0);
|
||||
vkCmdBindDescriptorSets(cb, VK_PIPELINE_BIND_POINT_COMPUTE,
|
||||
colorScatterPipelineLayout, 0, 1,
|
||||
&colorScatterSets[0], 0, nullptr);
|
||||
|
||||
PlaneTileRange redRange = computePlaneTileRange(
|
||||
frame.cfaPattern, 0, tileOriginX, tileOriginY, currentTileW,
|
||||
currentTileH, outputW, outputH);
|
||||
PlaneTileRange blueRange = computePlaneTileRange(
|
||||
frame.cfaPattern, 3, tileOriginX, tileOriginY, currentTileW,
|
||||
currentTileH, outputW, outputH);
|
||||
if (redRange.width > 0 || blueRange.width > 0) {
|
||||
uint32_t colorStartX = std::min(
|
||||
redRange.width > 0 ? redRange.startX : blueRange.startX,
|
||||
blueRange.width > 0 ? blueRange.startX : redRange.startX);
|
||||
uint32_t colorStartY = std::min(
|
||||
redRange.height > 0 ? redRange.startY : blueRange.startY,
|
||||
blueRange.height > 0 ? blueRange.startY : redRange.startY);
|
||||
uint32_t colorEndX = std::max(redRange.startX + redRange.width,
|
||||
blueRange.startX + blueRange.width);
|
||||
uint32_t colorEndY = std::max(redRange.startY + redRange.height,
|
||||
blueRange.startY + blueRange.height);
|
||||
|
||||
pc.isFirstFrame = isRef ? 1 : 0;
|
||||
pc.outputChannel = (colorIdx == 0) ? 0u : 2u;
|
||||
pc.planeIndex = pc.outputChannel;
|
||||
pc.bufferStride = tileW + 16;
|
||||
pc.tileX = tx * tileW;
|
||||
pc.tileY = ty * tileH;
|
||||
pc.tileW = currentTileW;
|
||||
pc.tileH = currentTileH;
|
||||
|
||||
pc.outputChannel = 0u;
|
||||
pc.planeIndex = 0u;
|
||||
pc.bufferStride = (colorEndX - colorStartX) + 16;
|
||||
pc.tileX = colorStartX;
|
||||
pc.tileY = colorStartY;
|
||||
pc.tileW = colorEndX - colorStartX;
|
||||
pc.tileH = colorEndY - colorStartY;
|
||||
vkCmdPushConstants(cb, colorScatterPipelineLayout,
|
||||
VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof(pc),
|
||||
&pc);
|
||||
vkCmdDispatch(cb, (width + 15) / 16, (height + 15) / 16, 1);
|
||||
vkCmdDispatch(cb, (pc.tileW + 15) / 16, (pc.tileH + 15) / 16, 1);
|
||||
}
|
||||
|
||||
VkBufferMemoryBarrier colorBarriers[2]{};
|
||||
@@ -3147,27 +3458,64 @@ bool VulkanRawStacker::processStack(uint16_t *outBuffer, size_t bufferSize) {
|
||||
const auto normalizeStart = PerfClock::now();
|
||||
VkCommandBuffer cb = vm.beginSingleTimeCommands();
|
||||
|
||||
// Compute the byte offset into staging buffer for this tile's row band
|
||||
VkDeviceSize rowBandOffset =
|
||||
(VkDeviceSize)(ty * tileH) * outputW * sizeof(uint16_t);
|
||||
// Range covers from this row band to the end of the buffer
|
||||
VkDeviceSize stagingTotalSize =
|
||||
(VkDeviceSize)outputW * outputH * sizeof(uint16_t);
|
||||
VkDeviceSize rowBandRange = stagingTotalSize - rowBandOffset;
|
||||
vkCmdBindPipeline(cb, VK_PIPELINE_BIND_POINT_COMPUTE,
|
||||
referencePriorPipeline);
|
||||
vkCmdBindDescriptorSets(cb, VK_PIPELINE_BIND_POINT_COMPUTE,
|
||||
referencePriorPipelineLayout, 0, 1,
|
||||
&referencePriorSet, 0, nullptr);
|
||||
|
||||
PushConstants priorPc{};
|
||||
priorPc.width = outputW;
|
||||
priorPc.height = outputH;
|
||||
priorPc.planeWidth = inputW;
|
||||
priorPc.planeHeight = inputH;
|
||||
priorPc.sensorWidth = width;
|
||||
priorPc.sensorHeight = height;
|
||||
priorPc.scale = scale;
|
||||
priorPc.cfaPattern = (uint32_t)mCfaPattern;
|
||||
priorPc.whiteLevel = mWhiteLevel;
|
||||
memcpy(priorPc.blackLevel, mBlackLevel, 4 * sizeof(float));
|
||||
memcpy(priorPc.wbGains, mWbGains, 4 * sizeof(float));
|
||||
priorPc.tileX = tileOriginX;
|
||||
priorPc.tileY = tileOriginY;
|
||||
priorPc.tileW = currentTileW;
|
||||
priorPc.tileH = currentTileH;
|
||||
vkCmdPushConstants(cb, referencePriorPipelineLayout,
|
||||
VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof(priorPc),
|
||||
&priorPc);
|
||||
vkCmdDispatch(cb, (currentTileW + 15) / 16,
|
||||
(currentTileH + 15) / 16, 1);
|
||||
|
||||
VkBufferMemoryBarrier priorBarriers[2]{};
|
||||
for (int i = 0; i < 2; ++i) {
|
||||
priorBarriers[i].sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER;
|
||||
priorBarriers[i].srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT;
|
||||
priorBarriers[i].dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
|
||||
priorBarriers[i].buffer =
|
||||
(i == 0) ? priorBayerBuffer : priorWeightBuffer;
|
||||
priorBarriers[i].size = VK_WHOLE_SIZE;
|
||||
}
|
||||
vkCmdPipelineBarrier(cb, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
|
||||
VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0, 0,
|
||||
nullptr, 2, priorBarriers, 0, nullptr);
|
||||
|
||||
for (int colorIdx = 0; colorIdx < 2; ++colorIdx) {
|
||||
const uint32_t planeIndex = (colorIdx == 0) ? 0u : 3u;
|
||||
PlaneTileRange colorRange = computePlaneTileRange(
|
||||
mCfaPattern, (int)planeIndex, tileOriginX, tileOriginY,
|
||||
currentTileW, currentTileH, outputW, outputH);
|
||||
if (colorRange.width == 0 || colorRange.height == 0) {
|
||||
continue;
|
||||
}
|
||||
updateBufferDescriptorSet(device, colorNormalizeSets[colorIdx],
|
||||
fusedBayerBuffer, rowBandRange, 0,
|
||||
rowBandOffset);
|
||||
fusedBayerBuffer, VK_WHOLE_SIZE, 0);
|
||||
updateBufferDescriptorSet(device, colorNormalizeSets[colorIdx],
|
||||
rbScatterAccumBuffers[colorIdx],
|
||||
VK_WHOLE_SIZE, 1);
|
||||
updateBufferDescriptorSet(device, colorNormalizeSets[colorIdx],
|
||||
priorBayerBuffer, rowBandRange, 2,
|
||||
rowBandOffset);
|
||||
priorBayerBuffer, VK_WHOLE_SIZE, 2);
|
||||
updateBufferDescriptorSet(device, colorNormalizeSets[colorIdx],
|
||||
priorWeightBuffer, rowBandRange, 3,
|
||||
rowBandOffset);
|
||||
priorWeightBuffer, VK_WHOLE_SIZE, 3);
|
||||
vkCmdBindPipeline(cb, VK_PIPELINE_BIND_POINT_COMPUTE,
|
||||
colorNormalizePipeline);
|
||||
vkCmdBindDescriptorSets(cb, VK_PIPELINE_BIND_POINT_COMPUTE,
|
||||
@@ -3178,14 +3526,14 @@ bool VulkanRawStacker::processStack(uint16_t *outBuffer, size_t bufferSize) {
|
||||
colorPc.width = outputW;
|
||||
colorPc.height = outputH;
|
||||
colorPc.scale = (float)scale;
|
||||
colorPc.planeWidth = outputW;
|
||||
colorPc.planeHeight = outputH;
|
||||
colorPc.bufferStride = tileW + 16;
|
||||
colorPc.tileX = tx * tileW;
|
||||
colorPc.tileY = ty * tileH;
|
||||
colorPc.tileW = currentTileW;
|
||||
colorPc.tileH = currentTileH;
|
||||
colorPc.planeIndex = (colorIdx == 0) ? 0u : 2u;
|
||||
colorPc.planeWidth = inputW;
|
||||
colorPc.planeHeight = inputH;
|
||||
colorPc.bufferStride = colorRange.width + 16;
|
||||
colorPc.tileX = colorRange.startX;
|
||||
colorPc.tileY = colorRange.startY;
|
||||
colorPc.tileW = colorRange.width;
|
||||
colorPc.tileH = colorRange.height;
|
||||
colorPc.planeIndex = planeIndex;
|
||||
colorPc.cfaPattern = (uint32_t)mCfaPattern;
|
||||
colorPc.whiteLevel = mWhiteLevel;
|
||||
memcpy(colorPc.blackLevel, mBlackLevel, 4 * sizeof(float));
|
||||
@@ -3194,20 +3542,27 @@ bool VulkanRawStacker::processStack(uint16_t *outBuffer, size_t bufferSize) {
|
||||
vkCmdPushConstants(cb, colorNormalizePipelineLayout,
|
||||
VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof(colorPc),
|
||||
&colorPc);
|
||||
vkCmdDispatch(cb, (currentTileW + 15) / 16,
|
||||
(currentTileH + 15) / 16, 1);
|
||||
vkCmdDispatch(cb, (colorRange.width + 15) / 16,
|
||||
(colorRange.height + 15) / 16, 1);
|
||||
}
|
||||
|
||||
PlaneTileRange greenRange0 = computePlaneTileRange(
|
||||
mCfaPattern, 1, tileOriginX, tileOriginY, currentTileW, currentTileH,
|
||||
outputW, outputH);
|
||||
PlaneTileRange greenRange1 = computePlaneTileRange(
|
||||
mCfaPattern, 2, tileOriginX, tileOriginY, currentTileW, currentTileH,
|
||||
outputW, outputH);
|
||||
if (greenRange0.width > 0 && greenRange0.height > 0) {
|
||||
updateBufferDescriptorSet(device, greenNormalizeSet, fusedBayerBuffer,
|
||||
rowBandRange, 0, rowBandOffset);
|
||||
VK_WHOLE_SIZE, 0);
|
||||
updateBufferDescriptorSet(device, greenNormalizeSet,
|
||||
greenPhaseAccumBuffers[0], VK_WHOLE_SIZE, 1);
|
||||
updateBufferDescriptorSet(device, greenNormalizeSet,
|
||||
greenPhaseAccumBuffers[1], VK_WHOLE_SIZE, 2);
|
||||
updateBufferDescriptorSet(device, greenNormalizeSet, priorBayerBuffer,
|
||||
rowBandRange, 3, rowBandOffset);
|
||||
VK_WHOLE_SIZE, 3);
|
||||
updateBufferDescriptorSet(device, greenNormalizeSet, priorWeightBuffer,
|
||||
rowBandRange, 4, rowBandOffset);
|
||||
VK_WHOLE_SIZE, 4);
|
||||
vkCmdBindPipeline(cb, VK_PIPELINE_BIND_POINT_COMPUTE,
|
||||
greenNormalizePipeline);
|
||||
vkCmdBindDescriptorSets(cb, VK_PIPELINE_BIND_POINT_COMPUTE,
|
||||
@@ -3218,13 +3573,13 @@ bool VulkanRawStacker::processStack(uint16_t *outBuffer, size_t bufferSize) {
|
||||
greenPc.width = outputW;
|
||||
greenPc.height = outputH;
|
||||
greenPc.scale = (float)scale;
|
||||
greenPc.planeWidth = outputW;
|
||||
greenPc.planeHeight = outputH;
|
||||
greenPc.bufferStride = tileW + 16;
|
||||
greenPc.tileX = tx * tileW;
|
||||
greenPc.tileY = ty * tileH;
|
||||
greenPc.tileW = currentTileW;
|
||||
greenPc.tileH = currentTileH;
|
||||
greenPc.planeWidth = inputW;
|
||||
greenPc.planeHeight = inputH;
|
||||
greenPc.bufferStride = greenRange0.width + 16;
|
||||
greenPc.tileX = greenRange0.startX;
|
||||
greenPc.tileY = greenRange0.startY;
|
||||
greenPc.tileW = greenRange0.width;
|
||||
greenPc.tileH = greenRange0.height;
|
||||
greenPc.planeIndex = 1;
|
||||
greenPc.cfaPattern = (uint32_t)mCfaPattern;
|
||||
greenPc.whiteLevel = mWhiteLevel;
|
||||
@@ -3234,8 +3589,23 @@ bool VulkanRawStacker::processStack(uint16_t *outBuffer, size_t bufferSize) {
|
||||
vkCmdPushConstants(cb, greenNormalizePipelineLayout,
|
||||
VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof(greenPc),
|
||||
&greenPc);
|
||||
vkCmdDispatch(cb, (currentTileW + 15) / 16,
|
||||
(currentTileH + 15) / 16, 1);
|
||||
vkCmdDispatch(cb, (greenRange0.width + 15) / 16,
|
||||
(greenRange0.height + 15) / 16, 1);
|
||||
|
||||
if (greenRange1.width > 0 && greenRange1.height > 0) {
|
||||
greenPc.bufferStride = greenRange1.width + 16;
|
||||
greenPc.tileX = greenRange1.startX;
|
||||
greenPc.tileY = greenRange1.startY;
|
||||
greenPc.tileW = greenRange1.width;
|
||||
greenPc.tileH = greenRange1.height;
|
||||
greenPc.planeIndex = 2;
|
||||
vkCmdPushConstants(cb, greenNormalizePipelineLayout,
|
||||
VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof(greenPc),
|
||||
&greenPc);
|
||||
vkCmdDispatch(cb, (greenRange1.width + 15) / 16,
|
||||
(greenRange1.height + 15) / 16, 1);
|
||||
}
|
||||
}
|
||||
|
||||
vm.endSingleTimeCommands(cb);
|
||||
perf.normalizeMs += elapsedMillis(normalizeStart);
|
||||
|
||||
@@ -120,25 +120,29 @@ LocalReliabilityMap buildLocalReliabilityMap(const GrayImage &reference,
|
||||
for (size_t idx = 0; idx < errorMap.size() && idx < detailScores.size(); ++idx) {
|
||||
float detail = detailScores[idx];
|
||||
float err = errorMap[idx];
|
||||
float errRms = std::sqrt(std::max(err, 0.0f));
|
||||
float detailPenalty = smoothstepf(flatThreshold, detailThreshold, detail);
|
||||
float flatness = 1.0f - detailPenalty;
|
||||
float errorWeight = clamp01(1.0f - std::max(0.0f, err - 8.0f) / 18.0f);
|
||||
float errorWeight = clamp01(1.0f - std::max(0.0f, errRms - 2.0f) / 4.5f);
|
||||
float tileWeight =
|
||||
clamp01(errorWeight * (0.04f + 0.96f * flatness * flatness));
|
||||
clamp01(errorWeight * (0.18f + 0.82f * flatness * flatness));
|
||||
|
||||
if (detailPenalty > 0.60f) {
|
||||
tileWeight *= 0.10f;
|
||||
++detailCount;
|
||||
} else if (detailPenalty > 0.30f) {
|
||||
tileWeight *= 0.35f;
|
||||
tileWeight *= 0.72f;
|
||||
} else {
|
||||
++flatCount;
|
||||
if (err < 12.0f)
|
||||
if (errRms < 4.0f)
|
||||
++flatGood;
|
||||
}
|
||||
|
||||
if (err > 20.0f) {
|
||||
tileWeight *= 0.35f;
|
||||
if (errRms > 5.5f) {
|
||||
tileWeight *= 0.60f;
|
||||
}
|
||||
|
||||
if (detailPenalty > 0.78f && errRms > 7.5f) {
|
||||
tileWeight = 0.0f;
|
||||
}
|
||||
|
||||
result.tileWeights[idx] = clamp01(tileWeight);
|
||||
@@ -157,17 +161,18 @@ float computeFrameFusionWeight(const std::vector<float> &errorMap,
|
||||
float sharpnessRatio,
|
||||
const LocalReliabilityMap &localMap) {
|
||||
if (errorMap.empty())
|
||||
return clamp01(0.18f + 0.22f * sharpnessRatio);
|
||||
return clamp01(0.70f + 0.25f * sharpnessRatio);
|
||||
|
||||
ScalarStats errStats = computeScalarStats(errorMap);
|
||||
float errP90Rms = std::sqrt(std::max(errStats.p90, 0.0f));
|
||||
float errorWeight =
|
||||
clamp01(1.0f - std::max(0.0f, errStats.p90 - 10.0f) / 12.0f);
|
||||
float sharpnessGuard = clamp01(0.80f + 0.20f * sharpnessRatio);
|
||||
float flatCoverage = clamp01(0.10f + 0.90f * localMap.flatAreaFraction);
|
||||
float flatReliability = clamp01(0.15f + 0.85f * localMap.flatGoodFraction);
|
||||
float detailPenalty = clamp01(1.0f - 0.55f * localMap.detailAreaFraction);
|
||||
clamp01(1.0f - std::max(0.0f, errP90Rms - 2.5f) / 5.0f);
|
||||
float sharpnessGuard = clamp01(0.88f + 0.12f * sharpnessRatio);
|
||||
float flatCoverage = clamp01(0.35f + 0.65f * localMap.flatAreaFraction);
|
||||
float flatReliability = clamp01(0.40f + 0.60f * localMap.flatGoodFraction);
|
||||
float detailPenalty = clamp01(1.0f - 0.35f * localMap.detailAreaFraction);
|
||||
float baseWeight =
|
||||
0.06f + 0.44f * errorWeight * flatCoverage * flatReliability * detailPenalty;
|
||||
0.42f + 0.40f * errorWeight * flatCoverage * flatReliability * detailPenalty;
|
||||
return clamp01(baseWeight * sharpnessGuard);
|
||||
}
|
||||
|
||||
@@ -930,6 +935,11 @@ bool VulkanImageStacker::processFrame(AHardwareBuffer *buffer, float frameScore)
|
||||
: 1.0f;
|
||||
frameWeight = computeFrameFusionWeight(alignmentErrorMap, sharpnessRatio,
|
||||
localMap);
|
||||
float p90Err = computeScalarStats(alignmentErrorMap).p90;
|
||||
LOGI("YUV fusion: sharpnessRatio=%.3f frameWeight=%.3f flatGood=%.3f flatArea=%.3f detailArea=%.3f p90Err=%.3f p90ErrRms=%.3f",
|
||||
sharpnessRatio, frameWeight, localMap.flatGoodFraction,
|
||||
localMap.flatAreaFraction, localMap.detailAreaFraction,
|
||||
p90Err, std::sqrt(std::max(p90Err, 0.0f)));
|
||||
}
|
||||
}
|
||||
TIME_END(cpuAlignment);
|
||||
|
||||
@@ -1492,7 +1492,7 @@ object PhotoManager {
|
||||
metadata.cropRegion,
|
||||
rotation,
|
||||
sharpeningValue = 0.4f,
|
||||
denoiseValue = 0f,
|
||||
denoiseValue = 0.2f,
|
||||
chromaDenoiseValue = 0.2f
|
||||
)
|
||||
|
||||
|
||||
Reference in new issue
Block a user