fix: sanitize RGB inputs and validate accumulation data in compute shaders

This commit is contained in:
zhoubinjia committed 2026-05-18 16:47:04 +08:00
1 parent c86f743ddb
commit b4a9aaadaf
4 files changed
+52 -12

No files matched your search

+13 -4
View File
@@ -46,6 +46,13 @@ layout(push_constant, scalar) uniform PushConstants {
float frameWeight;
} pc;
vec3 sanitizeRgb(vec3 rgb) {
if (any(isnan(rgb)) || any(isinf(rgb))) {
return vec3(1.0);
}
return clamp(rgb, 0.0, 1.0);
}
vec4 sampleBicubic(sampler2D s, vec2 uv, vec2 size) {
vec2 texelPos = uv * size - 0.5;
ivec2 base = ivec2(floor(texelPos));
@@ -73,14 +80,16 @@ vec4 sampleBicubic(sampler2D s, vec2 uv, vec2 size) {
ivec2 p = base + ivec2(i - 1, j - 1);
p = clamp(p, ivec2(0), ivec2(size) - ivec2(1));
float w = wx[i] * wy[j];
accum += texelFetch(s, p, 0) * w;
accum += vec4(sanitizeRgb(texelFetch(s, p, 0).rgb), 1.0) * w;
weightSum += w;
}
}
return (abs(weightSum) > 1e-6) ? (accum / weightSum) : accum;
vec4 color = (abs(weightSum) > 1e-6) ? (accum / weightSum) : accum;
return vec4(sanitizeRgb(color.rgb), 1.0);
}
vec3 rgb2ycbcr(vec3 rgb) {
rgb = sanitizeRgb(rgb);
float Y = dot(rgb, vec3(0.2126, 0.7152, 0.0722));
float Cb = dot(rgb, vec3(-0.1146, -0.3854, 0.5)) + 0.5;
float Cr = dot(rgb, vec3(0.5, -0.4542, -0.0458)) + 0.5;
@@ -310,14 +319,14 @@ void main() {
sampleColor = sampleBicubic(rgbSampler, sampleUV, nativeSize).rgb;
}
} else {
sampleColor = texture(rgbSampler, sampleUV).rgb;
sampleColor = sanitizeRgb(texture(rgbSampler, sampleUV).rgb);
}
vec3 sampleYCC = rgb2ycbcr(sampleColor);
float robustness = centerRobustness;
if (pc.isFirstFrame == 0) {
vec2 refSampleUV = (samplePos - alignment) / nativeSize;
vec3 sampleRefYCC = rgb2ycbcr(texture(referenceSampler, refSampleUV).rgb);
vec3 sampleRefYCC = rgb2ycbcr(sanitizeRgb(texture(referenceSampler, refSampleUV).rgb));
float sampleDiffSq = yccDiffSq(sampleYCC, sampleRefYCC);
float sampleResidual = max(0.0, sampleDiffSq - 4.5 * expectedSigma * expectedSigma) /
max(expectedSigma * expectedSigma, 1e-10);
+26 -4
View File
@@ -26,18 +26,32 @@ layout(push_constant, scalar) uniform PushConstants {
uint bufferStride;
} pc;
const float MIN_ACCUM_WEIGHT = 1e-4;
vec4 readAccum(int x, int y) {
x = clamp(x, 0, int(pc.tileW) - 1);
y = clamp(y, 0, int(pc.tileH) - 1);
return accumulator.data[uint(y) * pc.bufferStride + uint(x)];
}
bool hasValidAccum(vec4 acc) {
return acc.a > MIN_ACCUM_WEIGHT &&
!any(isnan(acc)) && !any(isinf(acc));
}
vec3 normalizeYCC(vec4 acc) {
float invW = 1.0 / max(acc.a, 0.001);
if (!hasValidAccum(acc)) {
return vec3(0.0, 0.5, 0.5);
}
float invW = 1.0 / acc.a;
float Y = acc.r * invW;
float Cb = acc.g * invW;
float Cr = acc.b * invW;
return vec3(Y, Cb, Cr);
vec3 ycc = vec3(Y, Cb, Cr);
if (any(isnan(ycc)) || any(isinf(ycc))) {
return vec3(0.0, 0.5, 0.5);
}
return clamp(ycc, vec3(0.0, 0.0, 0.0), vec3(1.0, 1.0, 1.0));
}
// YCbCr to Linear RGB (BT.709 inverse, matches accumulate.comp)
@@ -124,6 +138,9 @@ void main() {
int nx = cx + gx * stepScale;
int ny = cy + gy * stepScale;
vec4 nAcc = readAccum(nx, ny);
if (!hasValidAccum(nAcc)) {
continue;
}
vec3 nYCC = normalizeYCC(nAcc);
// Spatial weight
@@ -166,6 +183,9 @@ void main() {
for (int gy = -1; gy <= 1; ++gy) {
for (int gx = -1; gx <= 1; ++gx) {
vec4 nAcc = readAccum(lx + gx, ly + gy);
if (!hasValidAccum(nAcc)) {
continue;
}
float nL = normalizeYCC(nAcc).x;
float wS = (gx == 0 && gy == 0) ? 1.0 : 0.5;
@@ -178,8 +198,10 @@ void main() {
}
}
// Blend denoised luma with original bilinear value to keep sub-pixel detail
float denoisedL = lumaSum / max(lumaW, 0.001);
ycc.x = mix(ycc.x, denoisedL, strength);
if (lumaW > 0.001) {
float denoisedL = lumaSum / lumaW;
ycc.x = mix(ycc.x, denoisedL, strength);
}
}
// ── Luma-only detail recovery ──
@@ -28,6 +28,10 @@ layout(push_constant, scalar) uniform PushConstants {
} pc;
float rgb2gray(vec3 rgb) {
if (any(isnan(rgb)) || any(isinf(rgb))) {
rgb = vec3(1.0);
}
rgb = clamp(rgb, 0.0, 1.0);
return dot(rgb, vec3(0.299, 0.587, 0.114));
}
+9 -4
View File
@@ -10,6 +10,13 @@ layout(push_constant) uniform PushConstants {
uint height;
} pc;
vec3 sanitizeRgb(vec3 rgb) {
if (any(isnan(rgb)) || any(isinf(rgb))) {
return vec3(1.0);
}
return clamp(rgb, 0.0, 1.0);
}
void main() {
uint x = gl_GlobalInvocationID.x;
uint y = gl_GlobalInvocationID.y;
@@ -19,11 +26,9 @@ void main() {
vec2 uv = (vec2(float(x), float(y)) + 0.5) / vec2(float(pc.width), float(pc.height));
vec4 color = texture(yuvSampler, uv);
if (any(isnan(color)) || any(isinf(color))) {
color = vec4(0.0, 0.0, 0.0, 1.0);
}
vec3 rgb = sanitizeRgb(color.rgb);
// Explicitly set alpha to 1.0 as some YUV samplers might return 0
// and storing 0 alpha could cause issues in subsequent blending/weighting.
imageStore(rgbaOutput, ivec2(x, y), vec4(color.rgb, 1.0));
imageStore(rgbaOutput, ivec2(x, y), vec4(rgb, 1.0));
}