feat: implement reference-frame based robustness for accumulation

This commit is contained in:
Hinnka committed 2026-05-09 22:28:21 +08:00
1 parent 04c5e9c4e5
commit 628447ba35
6 files changed
+131 -52

No files matched your search

+20 -45
View File
@@ -24,6 +24,8 @@ layout(binding = 5, scalar) buffer LocalTileMask {
float weights[];
} localTileMask;
layout(binding = 6) uniform sampler2D referenceSampler;
layout(push_constant, scalar) uniform PushConstants {
float t0, t1, t2, t3, t4, t5;
float offsetX, offsetY;
@@ -125,7 +127,6 @@ void main() {
float motionError = 0.0;
if (pc.isFirstFrame == 0) {
// ... (bilinear sampling of offsets) ...
vec2 gCoord = finalUV * vec2(float(pc.gridW), float(pc.gridH)) - 0.5;
ivec2 g0 = ivec2(floor(gCoord));
vec2 gf = gCoord - vec2(g0);
@@ -139,10 +140,8 @@ void main() {
vec2 f11 = alignmentGrid.offsets[g1.y * pc.gridW + g1.x];
alignment = mix(mix(f00, f10, gf.x), mix(f01, f11, gf.x), gf.y);
// Clamp alignment to prevent extreme smears from failures in flat areas
alignment = clamp(alignment, vec2(-128.0), vec2(128.0));
// Bilinear interpolation of motion prior error
float e00 = motionPrior.errors[g0.y * pc.gridW + g0.x];
float e10 = motionPrior.errors[g0.y * pc.gridW + g1.x];
float e01 = motionPrior.errors[g1.y * pc.gridW + g0.x];
@@ -152,44 +151,40 @@ void main() {
finalUV += alignment / nativeSize;
}
// 4. Kernel Regression Elements
vec2 posInSource = finalUV * nativeSize;
vec2 centerSource = floor(posInSource - 0.5) + 0.5;
// Read Kernel Params for the current output pixel
// [REPLACED BY PREVIOUS DEFINITION AT TOP]
// 1. Read reference accumulator state (used for both center robustness and per-sample checks)
vec4 accumValue = accumulator.data[outIndex];
float currentW = accumValue.a;
float refLuma = 0.0;
float expectedSigma = 0.025;
float centerRobustness = 1.0;
vec3 centerColor = vec3(0.0);
vec3 centerYCC = vec3(0.0);
if (pc.isFirstFrame == 0 && currentW > 0.001) {
refLuma = accumulator.data[outIndex].r / currentW;
if (pc.isFirstFrame == 0) {
centerColor = sampleBicubic(rgbSampler, finalUV, nativeSize).rgb;
centerYCC = rgb2ycbcr(centerColor);
float diff = abs(centerYCC.x - refLuma);
float expectedVar = pc.noiseAlpha * refLuma + pc.noiseBeta;
vec3 refYCC = rgb2ycbcr(sampleBicubic(referenceSampler, finalUV, nativeSize).rgb);
float diff = length((centerYCC - refYCC) * vec3(1.0, 0.5, 0.5));
float expectedVar = pc.noiseAlpha * refYCC.x + pc.noiseBeta;
expectedSigma = sqrt(max(expectedVar, 1e-7));
expectedSigma = max(expectedSigma, 0.025);
float ratio = diff / expectedSigma;
float T1 = 3.0; float T2 = 12.0;
float T1 = 2.5; float T2 = 8.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 motionPenalty = 0.010;
float motionPenalty = 0.015;
centerRobustness *= (1.0 / (1.0 + motionError * motionPenalty));
if (isnan(centerRobustness)) centerRobustness = 0.0;
}
float localTileWeight = sampleLocalTileWeight(finalUV);
float fusionMask = localTileWeight;
int supportRadius = 0;
@@ -197,32 +192,20 @@ void main() {
if (pc.isFirstFrame == 0) {
supportRadius = 2;
float tilePenalty = 1.0 - smoothstep(0.10, 0.55, localTileWeight);
fusionMask *= mix(0.90, 0.55, tilePenalty);
// fusionMask *= 1.0 - smoothstep(0.05, 0.75, fineDetailMask);
fusionMask *= mix(0.95, 0.65, tilePenalty);
}
// Super-res first frame: supportRadius stays 0.
// Bicubic at each 2x pixel's unique sub-pixel position already provides
// distinct values. Kernel regression neighbors only add blur.
// Pre-compute constants (hoisted from loop)
float c_ = kp.z; float s_ = kp.w;
float cosT = sqrt(0.5 * (1.0 + c_));
float sinT = sqrt(0.5 * (1.0 - c_));
if (s_ < 0.0) sinT = -sinT;
// kp.x/kp.y (inverse variances) are tuned for RAW 0.5x coordinate space.
// d is in 1x native space. Base correction: 0.25 (from 0.5x to 1x).
// For super-res, output is at pc.scale × native. To keep the kernel's
// effective sigma constant in OUTPUT space, multiply by scale²:
// non-SR (scale=1): 0.25 * 1 = 0.25 (unchanged)
// SR 2x (scale=2): 0.25 * 4 = 1.0 (4× tighter in source space)
float kernelScale = 0.25 * pc.scale * pc.scale;
float invVarU = kp.x * kernelScale;
float invVarV = kp.y * kernelScale;
float baseWeightScale = pc.frameWeight * fusionMask;
float robustnessFloor = 0.25 * localTileWeight;
float robustnessFloor = 0.01 * localTileWeight;
// Accumulate neighbors
for (int j = -2; j <= 2; ++j) {
for (int i = -2; i <= 2; ++i) {
if (abs(i) > supportRadius || abs(j) > supportRadius) {
@@ -230,8 +213,6 @@ void main() {
}
vec2 d = (centerSource + vec2(float(i), float(j)) - posInSource);
// Compute the anisotropic kernel weight before sampling texture.
// This lets us skip neighbors whose theoretical contribution is already negligible.
float u = d.x * cosT + d.y * sinT;
float v = -d.x * sinT + d.y * cosT;
float w = exp(-0.5 * (u*u * invVarU + v*v * invVarV));
@@ -250,11 +231,9 @@ void main() {
vec2 samplePos = centerSource + vec2(float(i), float(j));
vec2 sampleUV = samplePos / nativeSize;
// Center pixel uses bicubic for max quality;
// neighbors use hardware bilinear (1 texture() vs 16 texelFetch())
vec3 sampleColor;
if (i == 0 && j == 0) {
if (pc.isFirstFrame == 0 && currentW > 0.001) {
if (pc.isFirstFrame == 0) {
sampleColor = centerColor;
} else {
sampleColor = sampleBicubic(rgbSampler, sampleUV, nativeSize).rgb;
@@ -264,21 +243,17 @@ void main() {
}
vec3 sampleYCC = rgb2ycbcr(sampleColor);
// Per-sample robustness
float robustness = centerRobustness;
float sampleFusionMask = fusionMask;
if (pc.isFirstFrame == 0) {
float sampleLumaDiff = abs(sampleYCC.x - refLuma);
float lumaDiffRatio = sampleLumaDiff / expectedSigma;
float sampleLumaRobustness = 1.0 - smoothstep(4.0, 10.0, lumaDiffRatio);
float largeLumaPenalty = smoothstep(5.5, 9.5, lumaDiffRatio);
sampleFusionMask *= mix(1.0, 0.70, largeLumaPenalty);
robustness *= sampleLumaRobustness;
vec3 sampleRefYCC = rgb2ycbcr(texture(referenceSampler, sampleUV).rgb);
float sampleDiff = length((sampleYCC - sampleRefYCC) * vec3(1.0, 0.5, 0.5));
float sampleRatio = sampleDiff / expectedSigma;
float sampleRobustness = 1.0 - smoothstep(3.0, 9.0, sampleRatio);
robustness *= sampleRobustness;
robustness = max(robustness, robustnessFloor);
}
float combinedWeight = w * robustness * pc.frameWeight * sampleFusionMask;
float combinedWeight = w * robustness * pc.frameWeight * fusionMask;
if (isnan(combinedWeight) || isinf(combinedWeight)) combinedWeight = 0.0;
if (combinedWeight <= 1e-6) continue;
@@ -192,7 +192,7 @@ void processPlane(ivec2 samplePlane, uint planeIndex) {
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 inlierWeight = pInlier * pInlier;
float w = inlierWeight *
exp(-0.5 * max(mahalanobis, 0.0) / effectiveSigma2) *
reconWeight * (0.45 + 0.55 * pc.frameWeight);
@@ -171,7 +171,7 @@ void splatSample(int phaseIndex, vec2 outCenter, vec2 refPos, float val,
(OmegaInv.y * d.x * d.y) * 2.0 +
OmegaInv.z * d.y * d.y;
float effectiveSigma2 = 1.0 + 4.0 * flowVar;
float inlierWeight = 0.18 + 0.82 * pInlier;
float inlierWeight = pInlier * pInlier;
float w = inlierWeight *
exp(-0.5 * max(mahalanobis, 0.0) / effectiveSigma2) *
reconWeight * (0.45 + 0.55 * pc.frameWeight);
+1 -1
View File
@@ -29,7 +29,7 @@ constexpr uint32_t kDenseAlignmentGridSpacing = 8;
constexpr int kAlignmentRegularizationPasses = 2;
constexpr float kAlignmentOutlierThreshold = 0.65f;
constexpr bool kEnableRawSuperResPerfLogs = false;
constexpr bool kFastPath = true;
constexpr bool kFastPath = false;
using PerfClock = std::chrono::steady_clock;
+107 -4
View File
@@ -539,6 +539,23 @@ void VulkanImageStacker::initVulkanResources() {
VK_CHECK(vkCreateImageView(device, &viewInfo, nullptr, &rgbFrame.viewY));
// Initialize Reference RGB Image
referenceRgbFrame.width = width;
referenceRgbFrame.height = height;
referenceRgbFrame.format = VK_FORMAT_R16G16B16A16_SFLOAT;
imageInfo.format = referenceRgbFrame.format;
VK_CHECK(vkCreateImage(device, &imageInfo, nullptr, &referenceRgbFrame.image));
vkGetImageMemoryRequirements(device, referenceRgbFrame.image, &memReqs);
allocInfo.allocationSize = memReqs.size;
VK_CHECK(vkAllocateMemory(device, &allocInfo, nullptr, &referenceRgbFrame.memory));
vkBindImageMemory(device, referenceRgbFrame.image, referenceRgbFrame.memory, 0);
viewInfo.image = referenceRgbFrame.image;
viewInfo.format = referenceRgbFrame.format;
VK_CHECK(vkCreateImageView(device, &viewInfo, nullptr, &referenceRgbFrame.viewY));
VkSamplerCreateInfo samplerInfo{};
samplerInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
samplerInfo.magFilter = VK_FILTER_LINEAR;
@@ -549,6 +566,7 @@ void VulkanImageStacker::initVulkanResources() {
samplerInfo.addressModeW = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
VK_CHECK(vkCreateSampler(device, &samplerInfo, nullptr, &rgbFrame.sampler));
VK_CHECK(vkCreateSampler(device, &samplerInfo, nullptr, &referenceRgbFrame.sampler));
}
void VulkanImageStacker::createPipelines(VkSampler sampler) {
@@ -592,17 +610,24 @@ void VulkanImageStacker::createPipelines(VkSampler sampler) {
localMaskBinding.descriptorCount = 1;
localMaskBinding.stageFlags = VK_SHADER_STAGE_COMPUTE_BIT;
VkDescriptorSetLayoutBinding bindingsFinal[6];
VkDescriptorSetLayoutBinding referenceBinding{};
referenceBinding.binding = 6;
referenceBinding.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
referenceBinding.descriptorCount = 1;
referenceBinding.stageFlags = VK_SHADER_STAGE_COMPUTE_BIT;
VkDescriptorSetLayoutBinding bindingsFinal[7];
bindingsFinal[0] = bindings[0];
bindingsFinal[1] = bindings[1];
bindingsFinal[2] = bindings[2];
bindingsFinal[3] = bindings[3];
bindingsFinal[4] = motionPriorBinding;
bindingsFinal[5] = localMaskBinding;
bindingsFinal[6] = referenceBinding;
VkDescriptorSetLayoutCreateInfo layoutInfo{};
layoutInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO;
layoutInfo.bindingCount = 6;
layoutInfo.bindingCount = 7;
layoutInfo.pBindings = bindingsFinal;
vkCreateDescriptorSetLayout(device, &layoutInfo, nullptr,
&descriptorSetLayout);
@@ -862,13 +887,18 @@ bool VulkanImageStacker::processFrame(AHardwareBuffer *buffer, float frameScore,
imageInfo.imageView = rgbFrame.viewY;
imageInfo.sampler = rgbFrame.sampler;
VkDescriptorImageInfo refImageInfo{};
refImageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
refImageInfo.imageView = referenceRgbFrame.viewY;
refImageInfo.sampler = referenceRgbFrame.sampler;
VkDescriptorBufferInfo accumBufferInfo{accumBuffers[i], 0, VK_WHOLE_SIZE};
VkDescriptorBufferInfo alignBufferInfo{alignmentBuffer, 0, VK_WHOLE_SIZE};
VkDescriptorBufferInfo kpBufferInfo{kernelParamsBuffer, 0, VK_WHOLE_SIZE};
VkDescriptorBufferInfo mpBufferInfo{motionPriorBuffer, 0, VK_WHOLE_SIZE};
VkDescriptorBufferInfo lmBufferInfo{localTileMaskBuffer, 0, VK_WHOLE_SIZE};
VkWriteDescriptorSet descriptorWrites[6] = {};
VkWriteDescriptorSet descriptorWrites[7] = {};
descriptorWrites[0].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
descriptorWrites[0].dstSet = accumSets[i];
descriptorWrites[0].dstBinding = 0;
@@ -912,7 +942,15 @@ bool VulkanImageStacker::processFrame(AHardwareBuffer *buffer, float frameScore,
descriptorWrites[5].descriptorCount = 1;
descriptorWrites[5].pBufferInfo = &lmBufferInfo;
vkUpdateDescriptorSets(device, 6, descriptorWrites, 0, nullptr);
descriptorWrites[6].sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
descriptorWrites[6].dstSet = accumSets[i];
descriptorWrites[6].dstBinding = 6;
descriptorWrites[6].descriptorType =
VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
descriptorWrites[6].descriptorCount = 1;
descriptorWrites[6].pImageInfo = &refImageInfo;
vkUpdateDescriptorSets(device, 7, descriptorWrites, 0, nullptr);
}
if (tensorSets[i] == VK_NULL_HANDLE) {
@@ -1195,6 +1233,70 @@ bool VulkanImageStacker::processFrame(AHardwareBuffer *buffer, float frameScore,
VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0, 0, nullptr, 0,
nullptr, 1, &rgbReadBarrier);
// Copy RGB frame to referenceRgbFrame if first frame
if (pc.isFirstFrame) {
VkImageMemoryBarrier copyBarriers[2] = {};
copyBarriers[0].sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
copyBarriers[0].oldLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
copyBarriers[0].newLayout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL;
copyBarriers[0].srcAccessMask = VK_ACCESS_SHADER_READ_BIT;
copyBarriers[0].dstAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
copyBarriers[0].image = rgbFrame.image;
copyBarriers[0].subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
copyBarriers[0].subresourceRange.levelCount = 1;
copyBarriers[0].subresourceRange.layerCount = 1;
copyBarriers[1].sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
copyBarriers[1].oldLayout = VK_IMAGE_LAYOUT_UNDEFINED;
copyBarriers[1].newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
copyBarriers[1].srcAccessMask = 0;
copyBarriers[1].dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
copyBarriers[1].image = referenceRgbFrame.image;
copyBarriers[1].subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
copyBarriers[1].subresourceRange.levelCount = 1;
copyBarriers[1].subresourceRange.layerCount = 1;
vkCmdPipelineBarrier(cb, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, nullptr, 0,
nullptr, 2, copyBarriers);
VkImageCopy copyRegion{};
copyRegion.srcSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
copyRegion.srcSubresource.layerCount = 1;
copyRegion.dstSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
copyRegion.dstSubresource.layerCount = 1;
copyRegion.extent = {width, height, 1};
vkCmdCopyImage(cb, rgbFrame.image, VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL,
referenceRgbFrame.image,
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, &copyRegion);
VkImageMemoryBarrier postCopyBarriers[2] = {};
postCopyBarriers[0].sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
postCopyBarriers[0].oldLayout = VK_IMAGE_LAYOUT_TRANSFER_SRC_OPTIMAL;
postCopyBarriers[0].newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
postCopyBarriers[0].srcAccessMask = VK_ACCESS_TRANSFER_READ_BIT;
postCopyBarriers[0].dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
postCopyBarriers[0].image = rgbFrame.image;
postCopyBarriers[0].subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
postCopyBarriers[0].subresourceRange.levelCount = 1;
postCopyBarriers[0].subresourceRange.layerCount = 1;
postCopyBarriers[1].sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
postCopyBarriers[1].oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
postCopyBarriers[1].newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
postCopyBarriers[1].srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
postCopyBarriers[1].dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
postCopyBarriers[1].image = referenceRgbFrame.image;
postCopyBarriers[1].subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
postCopyBarriers[1].subresourceRange.levelCount = 1;
postCopyBarriers[1].subresourceRange.layerCount = 1;
vkCmdPipelineBarrier(cb, VK_PIPELINE_STAGE_TRANSFER_BIT,
VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0, 0, nullptr, 0,
nullptr, 2, postCopyBarriers);
}
// Clear Accumulators if first frame
if (pc.isFirstFrame) {
for (int i = 0; i < numTiles; ++i) {
@@ -1883,4 +1985,5 @@ void VulkanImageStacker::releaseVulkanResources() {
vkDestroyPipeline(device, yuvToRgbaPipeline, nullptr);
rgbFrame.release(device);
referenceRgbFrame.release(device);
}
+1
View File
@@ -45,6 +45,7 @@ private:
// Pre-conversion resources
VulkanImage rgbFrame;
VulkanImage referenceRgbFrame;
VkDescriptorSetLayout yuvToRgbaLayout = VK_NULL_HANDLE;
VkDescriptorSet yuvToRgbaSet = VK_NULL_HANDLE;
VkPipelineLayout yuvToRgbaPipelineLayout = VK_NULL_HANDLE;