4248 lines
178 KiB
C++
4248 lines
178 KiB
C++
#include "vulkan_raw_stacker.h"
|
|
#include "align_lk.comp.h"
|
|
#include "common.h"
|
|
#include "color_gather_raw.comp.h"
|
|
#include "green_gather_raw.comp.h"
|
|
#include "normalize_color_hr.comp.h"
|
|
#include "normalize_green_hr.comp.h"
|
|
#include "reference_preprocess_raw.comp.h"
|
|
#include "reference_prior_raw.comp.h"
|
|
#include "raw_structure_tensor.comp.h"
|
|
#include "robustness_raw.comp.h"
|
|
#include <algorithm>
|
|
#include <cmath>
|
|
#include <cstring>
|
|
#include <limits>
|
|
#include <stdexcept>
|
|
#include <sys/resource.h>
|
|
#include <unistd.h>
|
|
|
|
#define VK_CHECK(x) \
|
|
do { \
|
|
VkResult err = x; \
|
|
if (err) { \
|
|
LOGE("Vulkan error: %d at %s:%d", err, __FILE__, __LINE__); \
|
|
} \
|
|
} while (0)
|
|
|
|
constexpr uint32_t kDenseAlignmentGridSpacing = 8;
|
|
constexpr int kAlignmentRegularizationPasses = 2;
|
|
constexpr float kAlignmentOutlierThreshold = 0.65f;
|
|
constexpr bool kEnableRawSuperResPerfLogs = false;
|
|
constexpr bool kEnableRawFlatVarianceLogs = false;
|
|
constexpr bool kFastPath = false;
|
|
|
|
using PerfClock = std::chrono::steady_clock;
|
|
|
|
inline double elapsedMillis(PerfClock::time_point start,
|
|
PerfClock::time_point end) {
|
|
return std::chrono::duration<double, std::milli>(end - start).count();
|
|
}
|
|
|
|
inline double elapsedMillis(PerfClock::time_point start) {
|
|
return elapsedMillis(start, PerfClock::now());
|
|
}
|
|
|
|
inline double averageMillis(double totalMs, size_t count) {
|
|
return count > 0 ? totalMs / static_cast<double>(count) : 0.0;
|
|
}
|
|
|
|
inline void logRawSuperResStage(const char *stage, double elapsedMs) {
|
|
if (!kEnableRawSuperResPerfLogs || elapsedMs < 5.0)
|
|
return;
|
|
LOGI("RawSuperResPerf[%s]: %.3f ms", stage, elapsedMs);
|
|
}
|
|
|
|
#undef TIME_START
|
|
#undef TIME_END
|
|
#define TIME_START(name) const auto start_##name = PerfClock::now()
|
|
#define TIME_END(name) \
|
|
const double elapsed_##name = elapsedMillis(start_##name); \
|
|
logRawSuperResStage(#name, elapsed_##name)
|
|
|
|
struct RawSuperResPerfStats {
|
|
double totalMs = 0.0;
|
|
double scoreCalculationMs = 0.0;
|
|
double descriptorSetupMs = 0.0;
|
|
|
|
double phase1UploadAndPyramidMs = 0.0;
|
|
double proxyBuildMs = 0.0;
|
|
double pyramidBuildMs = 0.0;
|
|
double gpuUploadMs = 0.0;
|
|
|
|
double referencePriorMs = 0.0;
|
|
double structureTensorMs = 0.0;
|
|
|
|
double coarseAlignmentMs = 0.0;
|
|
double coarseAlignmentComputeMs = 0.0;
|
|
|
|
double globalReliabilityMs = 0.0;
|
|
double globalReliabilitySeedMs = 0.0;
|
|
double globalReliabilityLkStage1Ms = 0.0;
|
|
double globalReliabilityRegularize1Ms = 0.0;
|
|
double globalReliabilityRobustness1Ms = 0.0;
|
|
double globalReliabilityLkStage2Ms = 0.0;
|
|
double globalReliabilityRobustness1AndLkStage2Ms = 0.0;
|
|
double globalReliabilityRegularize2Ms = 0.0;
|
|
double globalReliabilityRobustness2Ms = 0.0;
|
|
double globalReliabilityReadbackMs = 0.0;
|
|
|
|
double tileProcessingMs = 0.0;
|
|
double outputClearMs = 0.0;
|
|
double tileClearMs = 0.0;
|
|
double localMaskUploadMs = 0.0;
|
|
double tileRefinedStateUploadMs = 0.0;
|
|
double tileAlignmentSeedMs = 0.0;
|
|
double tileAlignmentLkStage1Ms = 0.0;
|
|
double tileAlignmentRegularize1Ms = 0.0;
|
|
double tileRobustness1Ms = 0.0;
|
|
double tileAlignmentLkStage2Ms = 0.0;
|
|
double tileAlignmentRegularize2Ms = 0.0;
|
|
double tileRobustness2Ms = 0.0;
|
|
double gatherMs = 0.0;
|
|
double normalizeMs = 0.0;
|
|
|
|
double outputReadbackMs = 0.0;
|
|
double cleanupMs = 0.0;
|
|
|
|
size_t totalFrames = 0;
|
|
size_t keptFrames = 0;
|
|
size_t skippedFrames = 0;
|
|
size_t tilesProcessed = 0;
|
|
size_t coarseAlignedFrames = 0;
|
|
size_t globalReliabilityFrames = 0;
|
|
size_t tileAlignedFrameInstances = 0;
|
|
size_t tileScatterFrameInstances = 0;
|
|
|
|
double totalEffectiveFrames = 0.0;
|
|
double avgRobustness = 0.0;
|
|
double avgWeakFraction = 0.0;
|
|
|
|
void logSummary(uint32_t outputW, uint32_t outputH, float scale) const {
|
|
if (!kEnableRawSuperResPerfLogs)
|
|
return;
|
|
LOGI(
|
|
"RawSuperResPerf summary: total=%.3f ms output=%ux%u scale=%.2f "
|
|
"frames=%zu kept=%zu skipped=%zu tiles=%zu robustEffFrames=%.2f avgR=%.3f weakF=%.3f",
|
|
totalMs, outputW, outputH, scale, totalFrames, keptFrames,
|
|
skippedFrames, tilesProcessed, totalEffectiveFrames, avgRobustness, avgWeakFraction);
|
|
LOGI(
|
|
"RawSuperResPerf phases: score=%.3f setup=%.3f upload=%.3f prior=%.3f "
|
|
"tensor=%.3f coarse=%.3f reliability=%.3f tile=%.3f readback=%.3f "
|
|
"cleanup=%.3f",
|
|
scoreCalculationMs, descriptorSetupMs, phase1UploadAndPyramidMs,
|
|
referencePriorMs, structureTensorMs, coarseAlignmentMs,
|
|
globalReliabilityMs, tileProcessingMs, outputReadbackMs, cleanupMs);
|
|
LOGI(
|
|
"RawSuperResPerf upload-detail: proxy=%.3f pyramid=%.3f gpuUpload=%.3f "
|
|
"avgFrame=%.3f",
|
|
proxyBuildMs, pyramidBuildMs, gpuUploadMs,
|
|
averageMillis(phase1UploadAndPyramidMs, totalFrames));
|
|
if (coarseAlignedFrames > 0 || globalReliabilityFrames > 0) {
|
|
LOGI(
|
|
"RawSuperResPerf align-detail: coarse=%.3f avgCoarse=%.3f "
|
|
"seed=%.3f summary=%.3f avgReliabilityFrame=%.3f",
|
|
coarseAlignmentComputeMs,
|
|
averageMillis(coarseAlignmentComputeMs, coarseAlignedFrames),
|
|
globalReliabilitySeedMs,
|
|
globalReliabilityReadbackMs,
|
|
averageMillis(globalReliabilityMs, globalReliabilityFrames));
|
|
}
|
|
if (tilesProcessed > 0) {
|
|
LOGI(
|
|
"RawSuperResPerf tile-detail: clearOut=%.3f clearTile=%.3f "
|
|
"maskUpload=%.3f refinedUpload=%.3f gather=%.3f normalize=%.3f "
|
|
"avgTile=%.3f avgGatherFrame=%.3f avgNonRefFramePrep=%.3f",
|
|
outputClearMs, tileClearMs, localMaskUploadMs,
|
|
tileRefinedStateUploadMs, gatherMs,
|
|
normalizeMs, averageMillis(tileProcessingMs, tilesProcessed),
|
|
averageMillis(gatherMs, tileScatterFrameInstances),
|
|
averageMillis(tileRefinedStateUploadMs, tileAlignedFrameInstances));
|
|
}
|
|
}
|
|
};
|
|
|
|
VulkanRawStacker::VulkanRawStacker(uint32_t w, uint32_t h, bool enableSuperRes,
|
|
float superResScale,
|
|
const float *blackLevel, float whiteLevel,
|
|
const float *wbGains,
|
|
const float *noiseModel,
|
|
const float *lensShadingMap,
|
|
uint32_t lscWidth, uint32_t lscHeight)
|
|
: width(w), height(h), mEnableSuperRes(enableSuperRes),
|
|
mSuperResScale(enableSuperRes ? std::clamp(superResScale, 1.0f, 2.0f)
|
|
: 1.0f),
|
|
mWhiteLevel(whiteLevel), mLscWidth(lscWidth), mLscHeight(lscHeight) {
|
|
|
|
if (blackLevel)
|
|
memcpy(mBlackLevel, blackLevel, 4 * sizeof(float));
|
|
if (wbGains)
|
|
memcpy(mWbGains, wbGains, 4 * sizeof(float));
|
|
if (noiseModel)
|
|
memcpy(mNoiseModel, noiseModel, 2 * sizeof(float));
|
|
if (lensShadingMap && lscWidth > 0 && lscHeight > 0) {
|
|
size_t size = (size_t)lscWidth * lscHeight * 4;
|
|
mLensShadingMap.assign(lensShadingMap, lensShadingMap + size);
|
|
}
|
|
|
|
if (mEnableSuperRes) {
|
|
uint32_t outW = (uint32_t)std::lround(width * mSuperResScale);
|
|
uint32_t outH = (uint32_t)std::lround(height * mSuperResScale);
|
|
const uint32_t MAX_TILE_SIZE = 4096;
|
|
numTilesX = (outW + MAX_TILE_SIZE - 1) / MAX_TILE_SIZE;
|
|
numTilesY = (outH + MAX_TILE_SIZE - 1) / MAX_TILE_SIZE;
|
|
} else {
|
|
numTilesX = 1;
|
|
numTilesY = 1;
|
|
}
|
|
|
|
if (width == 0 || height == 0) {
|
|
throw std::runtime_error("Invalid Vulkan raw stacker dimensions");
|
|
}
|
|
if (!VulkanManager::getInstance().init()) {
|
|
throw std::runtime_error("Failed to initialize VulkanManager");
|
|
}
|
|
initVulkanResources();
|
|
}
|
|
|
|
VulkanRawStacker::~VulkanRawStacker() {
|
|
pendingFrames.clear();
|
|
releaseVulkanResources();
|
|
}
|
|
|
|
void VulkanRawStacker::initVulkanResources() {
|
|
VulkanManager &vm = VulkanManager::getInstance();
|
|
VkDevice device = vm.getDevice();
|
|
if (device == VK_NULL_HANDLE) {
|
|
throw std::runtime_error("VulkanManager is not ready");
|
|
}
|
|
|
|
float scale = mEnableSuperRes ? mSuperResScale : 1.0f;
|
|
uint32_t outW = (uint32_t)std::lround(width * scale);
|
|
uint32_t outH = (uint32_t)std::lround(height * scale);
|
|
|
|
uint32_t planeW = outW;
|
|
uint32_t planeH = outH;
|
|
|
|
// Input dimensions (for Kernel/Robustness/Alignment)
|
|
uint32_t inputW = width / 2;
|
|
uint32_t inputH = height / 2;
|
|
|
|
uint32_t tileW = (planeW + numTilesX - 1) / numTilesX;
|
|
uint32_t tileH = (planeH + numTilesY - 1) / numTilesY;
|
|
uint32_t planeTileW = (tileW + 1) / 2;
|
|
uint32_t planeTileH = (tileH + 1) / 2;
|
|
uint32_t stride = planeTileW + 16;
|
|
VkDeviceSize accumBufferSize =
|
|
(VkDeviceSize)stride * (planeTileH + 16) * sizeof(float) * 2;
|
|
|
|
alignLkSets.resize(1);
|
|
robustnessSets.resize(1);
|
|
|
|
// Host-visible fused Bayer output buffer.
|
|
VkDeviceSize fusedBayerSize =
|
|
(VkDeviceSize)outW * outH * sizeof(uint16_t);
|
|
VkBufferCreateInfo stagingInfo{};
|
|
stagingInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
|
|
stagingInfo.size = fusedBayerSize;
|
|
stagingInfo.usage = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT |
|
|
VK_BUFFER_USAGE_TRANSFER_SRC_BIT |
|
|
VK_BUFFER_USAGE_TRANSFER_DST_BIT;
|
|
stagingInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
|
|
|
VK_CHECK(vkCreateBuffer(device, &stagingInfo, nullptr, &fusedBayerBuffer));
|
|
|
|
VkMemoryRequirements stagingReqs;
|
|
vkGetBufferMemoryRequirements(device, fusedBayerBuffer, &stagingReqs);
|
|
|
|
VkMemoryAllocateInfo stagingAlloc{};
|
|
stagingAlloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
|
|
stagingAlloc.allocationSize = stagingReqs.size;
|
|
stagingAlloc.memoryTypeIndex = vm.findMemoryType(
|
|
stagingReqs.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
|
|
VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
|
|
|
|
VK_CHECK(vkAllocateMemory(device, &stagingAlloc, nullptr, &fusedBayerMemory));
|
|
vkBindBufferMemory(device, fusedBayerBuffer, fusedBayerMemory, 0);
|
|
|
|
// Descriptor Pool — sized for 3 channels, not all tiles
|
|
VkDescriptorPoolSize poolSizes[2] = {};
|
|
poolSizes[0].type = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
|
|
poolSizes[0].descriptorCount = 256;
|
|
poolSizes[1].type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
|
|
poolSizes[1].descriptorCount = 256;
|
|
|
|
VkDescriptorPoolCreateInfo poolInfo{};
|
|
poolInfo.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO;
|
|
poolInfo.flags = VK_DESCRIPTOR_POOL_CREATE_FREE_DESCRIPTOR_SET_BIT;
|
|
poolInfo.maxSets = 256;
|
|
poolInfo.poolSizeCount = 2;
|
|
poolInfo.pPoolSizes = poolSizes;
|
|
VK_CHECK(vkCreateDescriptorPool(device, &poolInfo, nullptr, &descriptorPool));
|
|
|
|
// --- Alignment Buffer (Coarse) ---
|
|
// Denser control lattice for smoother sub-pixel warping. The LK support
|
|
// window stays compact in shader code so GPU cost remains bounded.
|
|
tileSize = (int)kDenseAlignmentGridSpacing;
|
|
gridW = (inputW + tileSize - 1) / tileSize;
|
|
gridH = (inputH + tileSize - 1) / tileSize; // Grid on Plane Resolution
|
|
|
|
VkDeviceSize alignBufferSize = (VkDeviceSize)gridW * gridH * sizeof(Point);
|
|
VkBufferCreateInfo alignInfo{};
|
|
alignInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
|
|
alignInfo.size = alignBufferSize;
|
|
alignInfo.usage = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT;
|
|
alignInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
|
VK_CHECK(vkCreateBuffer(device, &alignInfo, nullptr, &alignmentBuffer));
|
|
|
|
VkMemoryRequirements alignMemReqs;
|
|
vkGetBufferMemoryRequirements(device, alignmentBuffer, &alignMemReqs);
|
|
VkMemoryAllocateInfo alignAlloc{};
|
|
alignAlloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
|
|
alignAlloc.allocationSize = alignMemReqs.size;
|
|
alignAlloc.memoryTypeIndex = vm.findMemoryType(
|
|
alignMemReqs.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
|
|
VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
|
|
VK_CHECK(vkAllocateMemory(device, &alignAlloc, nullptr, &alignmentMemory));
|
|
vkBindBufferMemory(device, alignmentBuffer, alignmentMemory, 0);
|
|
|
|
// --- Intermediate Buffers for Handheld Super-Res ---
|
|
|
|
// 1. Kernel Buffer (vec4 per plane pixel)
|
|
VkDeviceSize kernelSize = (VkDeviceSize)inputW * inputH * sizeof(float) * 4;
|
|
VkBufferCreateInfo kInfo{VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO};
|
|
kInfo.size = kernelSize;
|
|
kInfo.usage = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT;
|
|
kInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
|
VK_CHECK(vkCreateBuffer(device, &kInfo, nullptr, &kernelBuffer));
|
|
|
|
VkMemoryRequirements kReqs;
|
|
vkGetBufferMemoryRequirements(device, kernelBuffer, &kReqs);
|
|
VkMemoryAllocateInfo kAlloc{VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO};
|
|
kAlloc.allocationSize = kReqs.size;
|
|
kAlloc.memoryTypeIndex = vm.findMemoryType(
|
|
kReqs.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
|
|
VK_CHECK(vkAllocateMemory(device, &kAlloc, nullptr, &kernelMemory));
|
|
vkBindBufferMemory(device, kernelBuffer, kernelMemory, 0);
|
|
|
|
// 2. Robustness Buffer (float per plane pixel)
|
|
VkDeviceSize rSize = (VkDeviceSize)inputW * inputH * sizeof(float);
|
|
VkBufferCreateInfo rInfo{VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO};
|
|
rInfo.size = rSize;
|
|
rInfo.usage = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT;
|
|
rInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
|
VK_CHECK(vkCreateBuffer(device, &rInfo, nullptr, &robustnessBuffer));
|
|
|
|
VkMemoryRequirements rReqs;
|
|
vkGetBufferMemoryRequirements(device, robustnessBuffer, &rReqs);
|
|
VkMemoryAllocateInfo rAlloc{VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO};
|
|
rAlloc.allocationSize = rReqs.size;
|
|
rAlloc.memoryTypeIndex = vm.findMemoryType(
|
|
rReqs.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
|
|
VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
|
|
VK_CHECK(vkAllocateMemory(device, &rAlloc, nullptr, &robustnessMemory));
|
|
vkBindBufferMemory(device, robustnessBuffer, robustnessMemory, 0);
|
|
|
|
VkBufferCreateInfo fvInfo{VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO};
|
|
fvInfo.size = rSize;
|
|
fvInfo.usage = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT;
|
|
fvInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
|
VK_CHECK(vkCreateBuffer(device, &fvInfo, nullptr, &flowVarianceBuffer));
|
|
|
|
VkMemoryRequirements fvReqs;
|
|
vkGetBufferMemoryRequirements(device, flowVarianceBuffer, &fvReqs);
|
|
VkMemoryAllocateInfo fvAlloc{VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO};
|
|
fvAlloc.allocationSize = fvReqs.size;
|
|
fvAlloc.memoryTypeIndex = vm.findMemoryType(
|
|
fvReqs.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
|
|
VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
|
|
VK_CHECK(vkAllocateMemory(device, &fvAlloc, nullptr, &flowVarianceMemory));
|
|
vkBindBufferMemory(device, flowVarianceBuffer, flowVarianceMemory, 0);
|
|
|
|
// 2B. Local tile reliability mask (float per 16x16 plane tile)
|
|
uint32_t localTilesX = (inputW + 15) / 16;
|
|
uint32_t localTilesY = (inputH + 15) / 16;
|
|
VkDeviceSize localMaskSize =
|
|
(VkDeviceSize)localTilesX * localTilesY * sizeof(float);
|
|
VkBufferCreateInfo lmInfo{VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO};
|
|
lmInfo.size = localMaskSize;
|
|
lmInfo.usage = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT;
|
|
lmInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
|
VK_CHECK(vkCreateBuffer(device, &lmInfo, nullptr, &localTileMaskBuffer));
|
|
|
|
VkMemoryRequirements lmReqs;
|
|
vkGetBufferMemoryRequirements(device, localTileMaskBuffer, &lmReqs);
|
|
VkMemoryAllocateInfo lmAlloc{VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO};
|
|
lmAlloc.allocationSize = lmReqs.size;
|
|
lmAlloc.memoryTypeIndex = vm.findMemoryType(
|
|
lmReqs.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
|
|
VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
|
|
VK_CHECK(vkAllocateMemory(device, &lmAlloc, nullptr, &localTileMaskMemory));
|
|
vkBindBufferMemory(device, localTileMaskBuffer, localTileMaskMemory, 0);
|
|
|
|
// 3. Green HR phase accumulators: atomic uint(sum, weight) per output pixel
|
|
VkDeviceSize greenAccumSize =
|
|
(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;
|
|
gInfo.usage =
|
|
VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT;
|
|
gInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
|
VK_CHECK(vkCreateBuffer(device, &gInfo, nullptr,
|
|
&greenPhaseAccumBuffers[phase]));
|
|
|
|
VkMemoryRequirements gReqs;
|
|
vkGetBufferMemoryRequirements(device, greenPhaseAccumBuffers[phase],
|
|
&gReqs);
|
|
VkMemoryAllocateInfo gAlloc{VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO};
|
|
gAlloc.allocationSize = gReqs.size;
|
|
gAlloc.memoryTypeIndex = vm.findMemoryType(
|
|
gReqs.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
|
|
VK_CHECK(vkAllocateMemory(device, &gAlloc, nullptr,
|
|
&greenPhaseAccumMemories[phase]));
|
|
vkBindBufferMemory(device, greenPhaseAccumBuffers[phase],
|
|
greenPhaseAccumMemories[phase], 0);
|
|
}
|
|
for (int c = 0; c < 2; ++c) {
|
|
VkBufferCreateInfo gInfo{VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO};
|
|
gInfo.size = greenAccumSize;
|
|
gInfo.usage =
|
|
VK_BUFFER_USAGE_STORAGE_BUFFER_BIT | VK_BUFFER_USAGE_TRANSFER_DST_BIT;
|
|
gInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
|
VK_CHECK(vkCreateBuffer(device, &gInfo, nullptr, &rbGatherAccumBuffers[c]));
|
|
|
|
VkMemoryRequirements gReqs;
|
|
vkGetBufferMemoryRequirements(device, rbGatherAccumBuffers[c], &gReqs);
|
|
VkMemoryAllocateInfo gAlloc{VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO};
|
|
gAlloc.allocationSize = gReqs.size;
|
|
gAlloc.memoryTypeIndex = vm.findMemoryType(
|
|
gReqs.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
|
|
VK_CHECK(
|
|
vkAllocateMemory(device, &gAlloc, nullptr, &rbGatherAccumMemories[c]));
|
|
vkBindBufferMemory(device, rbGatherAccumBuffers[c],
|
|
rbGatherAccumMemories[c], 0);
|
|
}
|
|
|
|
VkDeviceSize priorBufferSize =
|
|
(VkDeviceSize)outW * outH * sizeof(uint16_t);
|
|
VkBufferCreateInfo priorInfo{VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO};
|
|
priorInfo.size = priorBufferSize;
|
|
priorInfo.usage = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT;
|
|
priorInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
|
VK_CHECK(vkCreateBuffer(device, &priorInfo, nullptr, &priorBayerBuffer));
|
|
VK_CHECK(vkCreateBuffer(device, &priorInfo, nullptr, &priorWeightBuffer));
|
|
|
|
VkMemoryRequirements priorReqs;
|
|
vkGetBufferMemoryRequirements(device, priorBayerBuffer, &priorReqs);
|
|
VkMemoryAllocateInfo priorAlloc{VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO};
|
|
priorAlloc.allocationSize = priorReqs.size;
|
|
priorAlloc.memoryTypeIndex = vm.findMemoryType(
|
|
priorReqs.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
|
|
VK_CHECK(vkAllocateMemory(device, &priorAlloc, nullptr, &priorBayerMemory));
|
|
vkBindBufferMemory(device, priorBayerBuffer, priorBayerMemory, 0);
|
|
|
|
vkGetBufferMemoryRequirements(device, priorWeightBuffer, &priorReqs);
|
|
priorAlloc.allocationSize = priorReqs.size;
|
|
priorAlloc.memoryTypeIndex = vm.findMemoryType(
|
|
priorReqs.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
|
|
VK_CHECK(vkAllocateMemory(device, &priorAlloc, nullptr, &priorWeightMemory));
|
|
vkBindBufferMemory(device, priorWeightBuffer, priorWeightMemory, 0);
|
|
|
|
VkDeviceSize normalizedReferenceSize =
|
|
(VkDeviceSize)width * height * sizeof(uint16_t);
|
|
VkBufferCreateInfo normalizedReferenceInfo{
|
|
VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO};
|
|
normalizedReferenceInfo.size = normalizedReferenceSize;
|
|
normalizedReferenceInfo.usage = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT;
|
|
normalizedReferenceInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
|
VK_CHECK(vkCreateBuffer(device, &normalizedReferenceInfo, nullptr,
|
|
&normalizedReferenceBuffer));
|
|
|
|
VkMemoryRequirements normalizedReferenceReqs;
|
|
vkGetBufferMemoryRequirements(device, normalizedReferenceBuffer,
|
|
&normalizedReferenceReqs);
|
|
VkMemoryAllocateInfo normalizedReferenceAlloc{
|
|
VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO};
|
|
normalizedReferenceAlloc.allocationSize = normalizedReferenceReqs.size;
|
|
normalizedReferenceAlloc.memoryTypeIndex = vm.findMemoryType(
|
|
normalizedReferenceReqs.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
|
|
VK_CHECK(vkAllocateMemory(device, &normalizedReferenceAlloc, nullptr,
|
|
&normalizedReferenceMemory));
|
|
vkBindBufferMemory(device, normalizedReferenceBuffer,
|
|
normalizedReferenceMemory, 0);
|
|
|
|
VkCommandBuffer cb = vm.beginSingleTimeCommands();
|
|
for (int phase = 0; phase < 2; ++phase) {
|
|
vkCmdFillBuffer(cb, greenPhaseAccumBuffers[phase], 0, greenAccumSize, 0);
|
|
VkBufferMemoryBarrier barrier{};
|
|
barrier.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER;
|
|
barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
|
|
barrier.dstAccessMask =
|
|
VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT;
|
|
barrier.buffer = greenPhaseAccumBuffers[phase];
|
|
barrier.size = greenAccumSize;
|
|
vkCmdPipelineBarrier(cb, VK_PIPELINE_STAGE_TRANSFER_BIT,
|
|
VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0, 0, nullptr, 1,
|
|
&barrier, 0, nullptr);
|
|
}
|
|
for (int c = 0; c < 2; ++c) {
|
|
vkCmdFillBuffer(cb, rbGatherAccumBuffers[c], 0, greenAccumSize, 0);
|
|
VkBufferMemoryBarrier barrier{};
|
|
barrier.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER;
|
|
barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
|
|
barrier.dstAccessMask =
|
|
VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT;
|
|
barrier.buffer = rbGatherAccumBuffers[c];
|
|
barrier.size = greenAccumSize;
|
|
vkCmdPipelineBarrier(cb, VK_PIPELINE_STAGE_TRANSFER_BIT,
|
|
VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0, 0, nullptr, 1,
|
|
&barrier, 0, nullptr);
|
|
}
|
|
vkCmdFillBuffer(cb, fusedBayerBuffer, 0, fusedBayerSize, 0);
|
|
VkBufferMemoryBarrier fusedBarrier{};
|
|
fusedBarrier.sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER;
|
|
fusedBarrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
|
|
fusedBarrier.dstAccessMask =
|
|
VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT;
|
|
fusedBarrier.buffer = fusedBayerBuffer;
|
|
fusedBarrier.size = fusedBayerSize;
|
|
vkCmdPipelineBarrier(cb, VK_PIPELINE_STAGE_TRANSFER_BIT,
|
|
VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0, 0, nullptr, 1,
|
|
&fusedBarrier, 0, nullptr);
|
|
|
|
// Initial Clear for Alignment Buffer
|
|
void *ptr;
|
|
vkMapMemory(device, alignmentMemory, 0, alignBufferSize, 0, &ptr);
|
|
memset(ptr, 0, (size_t)alignBufferSize);
|
|
vkUnmapMemory(device, alignmentMemory);
|
|
|
|
// --- 3. Lens Shading Map Texture ---
|
|
VkBuffer lscStagingBuffer = VK_NULL_HANDLE;
|
|
VkDeviceMemory lscStagingMemory = VK_NULL_HANDLE;
|
|
|
|
if (mLensShadingMap.empty()) {
|
|
mLscWidth = 1;
|
|
mLscHeight = 1;
|
|
mLensShadingMap = {1.0f, 1.0f, 1.0f, 1.0f};
|
|
}
|
|
|
|
{
|
|
VkImageCreateInfo imageInfo{};
|
|
imageInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
|
|
imageInfo.imageType = VK_IMAGE_TYPE_2D;
|
|
imageInfo.extent.width = mLscWidth;
|
|
imageInfo.extent.height = mLscHeight;
|
|
imageInfo.extent.depth = 1;
|
|
imageInfo.mipLevels = 1;
|
|
imageInfo.arrayLayers = 1;
|
|
imageInfo.format = VK_FORMAT_R32G32B32A32_SFLOAT;
|
|
imageInfo.tiling = VK_IMAGE_TILING_OPTIMAL;
|
|
imageInfo.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
|
imageInfo.usage =
|
|
VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT;
|
|
imageInfo.samples = VK_SAMPLE_COUNT_1_BIT;
|
|
imageInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
|
|
|
VK_CHECK(vkCreateImage(device, &imageInfo, nullptr, &lscImage));
|
|
|
|
VkMemoryRequirements memReqs;
|
|
vkGetImageMemoryRequirements(device, lscImage, &memReqs);
|
|
|
|
VkMemoryAllocateInfo allocInfo{};
|
|
allocInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
|
|
allocInfo.allocationSize = memReqs.size;
|
|
allocInfo.memoryTypeIndex = vm.findMemoryType(
|
|
memReqs.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
|
|
|
|
VK_CHECK(vkAllocateMemory(device, &allocInfo, nullptr, &lscMemory));
|
|
vkBindImageMemory(device, lscImage, lscMemory, 0);
|
|
|
|
// Upload Data using staging buffer
|
|
VkDeviceSize lscSize = mLensShadingMap.size() * sizeof(float);
|
|
VkBufferCreateInfo bufferInfo{};
|
|
bufferInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
|
|
bufferInfo.size = lscSize;
|
|
bufferInfo.usage = VK_BUFFER_USAGE_TRANSFER_SRC_BIT;
|
|
bufferInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
|
vkCreateBuffer(device, &bufferInfo, nullptr, &lscStagingBuffer);
|
|
|
|
vkGetBufferMemoryRequirements(device, lscStagingBuffer, &memReqs);
|
|
allocInfo.allocationSize = memReqs.size;
|
|
allocInfo.memoryTypeIndex = vm.findMemoryType(
|
|
memReqs.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
|
|
VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
|
|
vkAllocateMemory(device, &allocInfo, nullptr, &lscStagingMemory);
|
|
vkBindBufferMemory(device, lscStagingBuffer, lscStagingMemory, 0);
|
|
|
|
void *data;
|
|
vkMapMemory(device, lscStagingMemory, 0, lscSize, 0, &data);
|
|
memcpy(data, mLensShadingMap.data(), (size_t)lscSize);
|
|
vkUnmapMemory(device, lscStagingMemory);
|
|
|
|
// Transition and Copy
|
|
VkImageMemoryBarrier barrier{};
|
|
barrier.sType = VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER;
|
|
barrier.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
|
barrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
|
|
barrier.image = lscImage;
|
|
barrier.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
|
barrier.subresourceRange.levelCount = 1;
|
|
barrier.subresourceRange.layerCount = 1;
|
|
barrier.srcAccessMask = 0;
|
|
barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
|
|
|
|
vkCmdPipelineBarrier(cb, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
|
|
VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, nullptr, 0,
|
|
nullptr, 1, &barrier);
|
|
|
|
VkBufferImageCopy region{};
|
|
region.imageSubresource.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
|
region.imageSubresource.layerCount = 1;
|
|
region.imageExtent = {mLscWidth, mLscHeight, 1};
|
|
vkCmdCopyBufferToImage(cb, lscStagingBuffer, lscImage,
|
|
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, ®ion);
|
|
|
|
barrier.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
|
|
barrier.newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
|
|
barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
|
|
barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
|
|
vkCmdPipelineBarrier(cb, VK_PIPELINE_STAGE_TRANSFER_BIT,
|
|
VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0, 0, nullptr, 0,
|
|
nullptr, 1, &barrier);
|
|
|
|
// Sampler (Linear for smooth interpolation)
|
|
VkSamplerCreateInfo samplerInfo{};
|
|
samplerInfo.sType = VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO;
|
|
samplerInfo.magFilter = VK_FILTER_LINEAR;
|
|
samplerInfo.minFilter = VK_FILTER_LINEAR;
|
|
samplerInfo.addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
|
|
samplerInfo.addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
|
|
samplerInfo.mipmapMode = VK_SAMPLER_MIPMAP_MODE_LINEAR;
|
|
vkCreateSampler(device, &samplerInfo, nullptr, &lscSampler);
|
|
|
|
// View
|
|
VkImageViewCreateInfo viewInfo{};
|
|
viewInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
|
|
viewInfo.image = lscImage;
|
|
viewInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
|
|
viewInfo.format = VK_FORMAT_R32G32B32A32_SFLOAT;
|
|
viewInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
|
|
viewInfo.subresourceRange.levelCount = 1;
|
|
viewInfo.subresourceRange.layerCount = 1;
|
|
vkCreateImageView(device, &viewInfo, nullptr, &lscView);
|
|
}
|
|
|
|
vm.endSingleTimeCommands(cb);
|
|
|
|
if (lscStagingBuffer) {
|
|
vkDestroyBuffer(device, lscStagingBuffer, nullptr);
|
|
vkFreeMemory(device, lscStagingMemory, nullptr);
|
|
}
|
|
}
|
|
|
|
// Helper to create compute pipeline
|
|
VkPipeline createComputePipeline(VkDevice device, VkPipelineLayout layout,
|
|
const uint32_t *shaderCode, size_t size) {
|
|
VkShaderModule shaderModule = VulkanManager::getInstance().createShaderModule(
|
|
std::vector<uint32_t>(shaderCode, shaderCode + size / 4));
|
|
VkComputePipelineCreateInfo pipelineInfo{};
|
|
pipelineInfo.sType = VK_STRUCTURE_TYPE_COMPUTE_PIPELINE_CREATE_INFO;
|
|
pipelineInfo.layout = layout;
|
|
pipelineInfo.stage.sType =
|
|
VK_STRUCTURE_TYPE_PIPELINE_SHADER_STAGE_CREATE_INFO;
|
|
pipelineInfo.stage.stage = VK_SHADER_STAGE_COMPUTE_BIT;
|
|
pipelineInfo.stage.module = shaderModule;
|
|
pipelineInfo.stage.pName = "main";
|
|
VkPipeline pipeline;
|
|
vkCreateComputePipelines(device, VK_NULL_HANDLE, 1, &pipelineInfo, nullptr,
|
|
&pipeline);
|
|
vkDestroyShaderModule(device, shaderModule, nullptr);
|
|
return pipeline;
|
|
}
|
|
|
|
void VulkanRawStacker::createPipelines() {
|
|
VulkanManager &vm = VulkanManager::getInstance();
|
|
VkDevice device = vm.getDevice();
|
|
|
|
// Push constants (Shared)
|
|
VkPushConstantRange pushConstantRange{};
|
|
pushConstantRange.stageFlags = VK_SHADER_STAGE_COMPUTE_BIT;
|
|
pushConstantRange.offset = 0;
|
|
pushConstantRange.size = sizeof(PushConstants);
|
|
|
|
// --- 1. Structure Tensor Pipeline ---
|
|
VkDescriptorSetLayoutBinding stBindings[2] = {};
|
|
stBindings[0] = {0, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1,
|
|
VK_SHADER_STAGE_COMPUTE_BIT, nullptr}; // Input
|
|
stBindings[1] = {1, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1,
|
|
VK_SHADER_STAGE_COMPUTE_BIT, nullptr}; // Output Kernel
|
|
|
|
VkDescriptorSetLayoutCreateInfo stLayoutInfo{
|
|
VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO};
|
|
stLayoutInfo.bindingCount = 2;
|
|
stLayoutInfo.pBindings = stBindings;
|
|
vkCreateDescriptorSetLayout(device, &stLayoutInfo, nullptr,
|
|
&structureTensorSetLayout);
|
|
|
|
VkPipelineLayoutCreateInfo stPLInfo{
|
|
VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO};
|
|
stPLInfo.setLayoutCount = 1;
|
|
stPLInfo.pSetLayouts = &structureTensorSetLayout;
|
|
stPLInfo.pushConstantRangeCount = 1;
|
|
stPLInfo.pPushConstantRanges = &pushConstantRange;
|
|
vkCreatePipelineLayout(device, &stPLInfo, nullptr,
|
|
&structureTensorPipelineLayout);
|
|
|
|
structureTensorPipeline = createComputePipeline(
|
|
device, structureTensorPipelineLayout, raw_structure_tensor_comp_spv,
|
|
raw_structure_tensor_comp_spv_size);
|
|
|
|
// --- 2. LK Alignment Pipeline ---
|
|
VkDescriptorSetLayoutBinding lkBindings[4] = {};
|
|
lkBindings[0] = {0, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1,
|
|
VK_SHADER_STAGE_COMPUTE_BIT, nullptr}; // Ref
|
|
lkBindings[1] = {1, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1,
|
|
VK_SHADER_STAGE_COMPUTE_BIT, nullptr}; // Trg
|
|
lkBindings[2] = {2, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1,
|
|
VK_SHADER_STAGE_COMPUTE_BIT,
|
|
nullptr}; // Align Buffer (In/Out)
|
|
lkBindings[3] = {3, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1,
|
|
VK_SHADER_STAGE_COMPUTE_BIT,
|
|
nullptr}; // Inlier weights
|
|
|
|
VkDescriptorSetLayoutCreateInfo lkLayoutInfo{
|
|
VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO};
|
|
lkLayoutInfo.bindingCount = 4;
|
|
lkLayoutInfo.pBindings = lkBindings;
|
|
vkCreateDescriptorSetLayout(device, &lkLayoutInfo, nullptr,
|
|
&alignLkSetLayout);
|
|
|
|
VkPipelineLayoutCreateInfo lkPLInfo{
|
|
VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO};
|
|
lkPLInfo.setLayoutCount = 1;
|
|
lkPLInfo.pSetLayouts = &alignLkSetLayout;
|
|
lkPLInfo.pushConstantRangeCount = 1;
|
|
lkPLInfo.pPushConstantRanges = &pushConstantRange;
|
|
vkCreatePipelineLayout(device, &lkPLInfo, nullptr, &alignLkPipelineLayout);
|
|
|
|
alignLkPipeline = createComputePipeline(
|
|
device, alignLkPipelineLayout, align_lk_comp_spv, align_lk_comp_spv_size);
|
|
|
|
// --- 3. Robustness Pipeline ---
|
|
VkDescriptorSetLayoutBinding rbBindings[5] = {};
|
|
rbBindings[0] = {0, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1,
|
|
VK_SHADER_STAGE_COMPUTE_BIT, nullptr}; // Ref
|
|
rbBindings[1] = {1, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1,
|
|
VK_SHADER_STAGE_COMPUTE_BIT, nullptr}; // Trg
|
|
rbBindings[2] = {2, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1,
|
|
VK_SHADER_STAGE_COMPUTE_BIT, nullptr}; // Alignment
|
|
rbBindings[3] = {3, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1,
|
|
VK_SHADER_STAGE_COMPUTE_BIT, nullptr}; // Output Map
|
|
rbBindings[4] = {4, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1,
|
|
VK_SHADER_STAGE_COMPUTE_BIT, nullptr}; // Flow variance
|
|
|
|
VkDescriptorSetLayoutCreateInfo rbLayoutInfo{
|
|
VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO};
|
|
rbLayoutInfo.bindingCount = 5;
|
|
rbLayoutInfo.pBindings = rbBindings;
|
|
vkCreateDescriptorSetLayout(device, &rbLayoutInfo, nullptr,
|
|
&robustnessSetLayout);
|
|
|
|
VkPipelineLayoutCreateInfo rbPLInfo{
|
|
VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO};
|
|
rbPLInfo.setLayoutCount = 1;
|
|
rbPLInfo.pSetLayouts = &robustnessSetLayout;
|
|
rbPLInfo.pushConstantRangeCount = 1;
|
|
rbPLInfo.pPushConstantRanges = &pushConstantRange;
|
|
vkCreatePipelineLayout(device, &rbPLInfo, nullptr, &robustnessPipelineLayout);
|
|
|
|
robustnessPipeline = createComputePipeline(device, robustnessPipelineLayout,
|
|
robustness_raw_comp_spv,
|
|
robustness_raw_comp_spv_size);
|
|
|
|
// --- 4. Green HR Gather Pipeline ---
|
|
VkDescriptorSetLayoutBinding gsBindings[9] = {};
|
|
gsBindings[0] = {0, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1,
|
|
VK_SHADER_STAGE_COMPUTE_BIT, nullptr}; // Input RAW
|
|
gsBindings[1] = {1, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1,
|
|
VK_SHADER_STAGE_COMPUTE_BIT, nullptr}; // Align
|
|
gsBindings[2] = {2, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1,
|
|
VK_SHADER_STAGE_COMPUTE_BIT, nullptr}; // Kernel
|
|
gsBindings[3] = {3, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1,
|
|
VK_SHADER_STAGE_COMPUTE_BIT, nullptr}; // pInlier
|
|
gsBindings[4] = {4, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1,
|
|
VK_SHADER_STAGE_COMPUTE_BIT, nullptr}; // Flow variance
|
|
gsBindings[5] = {5, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1,
|
|
VK_SHADER_STAGE_COMPUTE_BIT, nullptr}; // Green phase 0
|
|
gsBindings[6] = {6, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1,
|
|
VK_SHADER_STAGE_COMPUTE_BIT, nullptr}; // Green phase 1
|
|
gsBindings[7] = {7, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1,
|
|
VK_SHADER_STAGE_COMPUTE_BIT, nullptr}; // LSC
|
|
gsBindings[8] = {8, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1,
|
|
VK_SHADER_STAGE_COMPUTE_BIT, nullptr}; // Local tile mask
|
|
|
|
VkDescriptorSetLayoutCreateInfo gsLayoutInfo{
|
|
VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO};
|
|
gsLayoutInfo.bindingCount = 9;
|
|
gsLayoutInfo.pBindings = gsBindings;
|
|
vkCreateDescriptorSetLayout(device, &gsLayoutInfo, nullptr,
|
|
&greenGatherSetLayout);
|
|
|
|
VkPipelineLayoutCreateInfo gsPLInfo{
|
|
VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO};
|
|
gsPLInfo.setLayoutCount = 1;
|
|
gsPLInfo.pSetLayouts = &greenGatherSetLayout;
|
|
gsPLInfo.pushConstantRangeCount = 1;
|
|
gsPLInfo.pPushConstantRanges = &pushConstantRange;
|
|
vkCreatePipelineLayout(device, &gsPLInfo, nullptr,
|
|
&greenGatherPipelineLayout);
|
|
|
|
greenGatherPipeline = createComputePipeline(
|
|
device, greenGatherPipelineLayout, green_gather_raw_comp_spv,
|
|
green_gather_raw_comp_spv_size);
|
|
|
|
// --- 5. Color Gather Pipeline (R/B) ---
|
|
VkDescriptorSetLayoutBinding csBindings[9] = {};
|
|
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,
|
|
VK_SHADER_STAGE_COMPUTE_BIT, nullptr};
|
|
csBindings[2] = {2, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1,
|
|
VK_SHADER_STAGE_COMPUTE_BIT, nullptr};
|
|
csBindings[3] = {3, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1,
|
|
VK_SHADER_STAGE_COMPUTE_BIT, nullptr};
|
|
csBindings[4] = {4, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1,
|
|
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_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};
|
|
csBindings[8] = {8, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1,
|
|
VK_SHADER_STAGE_COMPUTE_BIT, nullptr};
|
|
|
|
VkDescriptorSetLayoutCreateInfo csLayoutInfo{
|
|
VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO};
|
|
csLayoutInfo.bindingCount = 9;
|
|
csLayoutInfo.pBindings = csBindings;
|
|
vkCreateDescriptorSetLayout(device, &csLayoutInfo, nullptr,
|
|
&colorGatherSetLayout);
|
|
|
|
VkPipelineLayoutCreateInfo csPLInfo{
|
|
VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO};
|
|
csPLInfo.setLayoutCount = 1;
|
|
csPLInfo.pSetLayouts = &colorGatherSetLayout;
|
|
csPLInfo.pushConstantRangeCount = 1;
|
|
csPLInfo.pPushConstantRanges = &pushConstantRange;
|
|
vkCreatePipelineLayout(device, &csPLInfo, nullptr,
|
|
&colorGatherPipelineLayout);
|
|
|
|
colorGatherPipeline = createComputePipeline(
|
|
device, colorGatherPipelineLayout, color_gather_raw_comp_spv,
|
|
color_gather_raw_comp_spv_size);
|
|
|
|
// --- 6. Reference Normalize Pipeline ---
|
|
VkDescriptorSetLayoutBinding rnBindings[3] = {};
|
|
rnBindings[0] = {0, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1,
|
|
VK_SHADER_STAGE_COMPUTE_BIT, nullptr};
|
|
rnBindings[1] = {1, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1,
|
|
VK_SHADER_STAGE_COMPUTE_BIT, nullptr};
|
|
rnBindings[2] = {2, VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER, 1,
|
|
VK_SHADER_STAGE_COMPUTE_BIT, nullptr};
|
|
|
|
VkDescriptorSetLayoutCreateInfo rnLayoutInfo{
|
|
VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO};
|
|
rnLayoutInfo.bindingCount = 3;
|
|
rnLayoutInfo.pBindings = rnBindings;
|
|
vkCreateDescriptorSetLayout(device, &rnLayoutInfo, nullptr,
|
|
&referenceNormalizeSetLayout);
|
|
|
|
VkPipelineLayoutCreateInfo rnPLInfo{
|
|
VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO};
|
|
rnPLInfo.setLayoutCount = 1;
|
|
rnPLInfo.pSetLayouts = &referenceNormalizeSetLayout;
|
|
rnPLInfo.pushConstantRangeCount = 1;
|
|
rnPLInfo.pPushConstantRanges = &pushConstantRange;
|
|
vkCreatePipelineLayout(device, &rnPLInfo, nullptr,
|
|
&referenceNormalizePipelineLayout);
|
|
|
|
referenceNormalizePipeline = createComputePipeline(
|
|
device, referenceNormalizePipelineLayout,
|
|
reference_preprocess_raw_comp_spv,
|
|
reference_preprocess_raw_comp_spv_size);
|
|
|
|
// --- 7. Reference Prior Pipeline ---
|
|
VkDescriptorSetLayoutBinding rpBindings[3] = {};
|
|
rpBindings[0] = {0, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1,
|
|
VK_SHADER_STAGE_COMPUTE_BIT, nullptr};
|
|
rpBindings[1] = {1, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1,
|
|
VK_SHADER_STAGE_COMPUTE_BIT, nullptr};
|
|
rpBindings[2] = {2, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1,
|
|
VK_SHADER_STAGE_COMPUTE_BIT, nullptr};
|
|
|
|
VkDescriptorSetLayoutCreateInfo rpLayoutInfo{
|
|
VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO};
|
|
rpLayoutInfo.bindingCount = 3;
|
|
rpLayoutInfo.pBindings = rpBindings;
|
|
vkCreateDescriptorSetLayout(device, &rpLayoutInfo, nullptr,
|
|
&referencePriorSetLayout);
|
|
|
|
VkPipelineLayoutCreateInfo rpPLInfo{
|
|
VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO};
|
|
rpPLInfo.setLayoutCount = 1;
|
|
rpPLInfo.pSetLayouts = &referencePriorSetLayout;
|
|
rpPLInfo.pushConstantRangeCount = 1;
|
|
rpPLInfo.pPushConstantRanges = &pushConstantRange;
|
|
vkCreatePipelineLayout(device, &rpPLInfo, nullptr,
|
|
&referencePriorPipelineLayout);
|
|
|
|
referencePriorPipeline = createComputePipeline(
|
|
device, referencePriorPipelineLayout, reference_prior_raw_comp_spv,
|
|
reference_prior_raw_comp_spv_size);
|
|
|
|
// --- 8. Green Normalize Pipeline ---
|
|
VkDescriptorSetLayoutBinding gnBindings[5] = {};
|
|
gnBindings[0] = {0, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1,
|
|
VK_SHADER_STAGE_COMPUTE_BIT, nullptr}; // Output
|
|
gnBindings[1] = {1, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1,
|
|
VK_SHADER_STAGE_COMPUTE_BIT, nullptr}; // Phase 0
|
|
gnBindings[2] = {2, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1,
|
|
VK_SHADER_STAGE_COMPUTE_BIT, nullptr}; // Phase 1
|
|
gnBindings[3] = {3, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1,
|
|
VK_SHADER_STAGE_COMPUTE_BIT, nullptr}; // Prior value
|
|
gnBindings[4] = {4, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1,
|
|
VK_SHADER_STAGE_COMPUTE_BIT, nullptr}; // Prior weight
|
|
|
|
VkDescriptorSetLayoutCreateInfo gnLayoutInfo{
|
|
VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO};
|
|
gnLayoutInfo.bindingCount = 5;
|
|
gnLayoutInfo.pBindings = gnBindings;
|
|
vkCreateDescriptorSetLayout(device, &gnLayoutInfo, nullptr,
|
|
&greenNormalizeSetLayout);
|
|
|
|
VkPipelineLayoutCreateInfo gnPLInfo{
|
|
VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO};
|
|
gnPLInfo.setLayoutCount = 1;
|
|
gnPLInfo.pSetLayouts = &greenNormalizeSetLayout;
|
|
gnPLInfo.pushConstantRangeCount = 1;
|
|
gnPLInfo.pPushConstantRanges = &pushConstantRange;
|
|
vkCreatePipelineLayout(device, &gnPLInfo, nullptr,
|
|
&greenNormalizePipelineLayout);
|
|
|
|
greenNormalizePipeline = createComputePipeline(
|
|
device, greenNormalizePipelineLayout, normalize_green_hr_comp_spv,
|
|
normalize_green_hr_comp_spv_size);
|
|
|
|
// --- 9. Color Normalize Pipeline ---
|
|
VkDescriptorSetLayoutBinding cnBindings[4] = {};
|
|
cnBindings[0] = {0, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1,
|
|
VK_SHADER_STAGE_COMPUTE_BIT, nullptr};
|
|
cnBindings[1] = {1, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1,
|
|
VK_SHADER_STAGE_COMPUTE_BIT, nullptr};
|
|
cnBindings[2] = {2, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1,
|
|
VK_SHADER_STAGE_COMPUTE_BIT, nullptr};
|
|
cnBindings[3] = {3, VK_DESCRIPTOR_TYPE_STORAGE_BUFFER, 1,
|
|
VK_SHADER_STAGE_COMPUTE_BIT, nullptr};
|
|
|
|
VkDescriptorSetLayoutCreateInfo cnLayoutInfo{
|
|
VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO};
|
|
cnLayoutInfo.bindingCount = 4;
|
|
cnLayoutInfo.pBindings = cnBindings;
|
|
vkCreateDescriptorSetLayout(device, &cnLayoutInfo, nullptr,
|
|
&colorNormalizeSetLayout);
|
|
|
|
VkPipelineLayoutCreateInfo cnPLInfo{
|
|
VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO};
|
|
cnPLInfo.setLayoutCount = 1;
|
|
cnPLInfo.pSetLayouts = &colorNormalizeSetLayout;
|
|
cnPLInfo.pushConstantRangeCount = 1;
|
|
cnPLInfo.pPushConstantRanges = &pushConstantRange;
|
|
vkCreatePipelineLayout(device, &cnPLInfo, nullptr,
|
|
&colorNormalizePipelineLayout);
|
|
|
|
colorNormalizePipeline = createComputePipeline(
|
|
device, colorNormalizePipelineLayout, normalize_color_hr_comp_spv,
|
|
normalize_color_hr_comp_spv_size);
|
|
|
|
LOGD("Pipelines created");
|
|
}
|
|
|
|
bool VulkanRawStacker::addFrame(const uint16_t *rawData, int rowStride,
|
|
int cfaPattern) {
|
|
if (rawData == nullptr)
|
|
return false;
|
|
|
|
// Copy data to internal storage
|
|
FrameData frame;
|
|
frame.cfaPattern = cfaPattern;
|
|
frame.rawData.resize(width * height);
|
|
|
|
// Handle rowStride
|
|
if (rowStride == width * 2) {
|
|
// Simple copy
|
|
memcpy(frame.rawData.data(), rawData, width * height * sizeof(uint16_t));
|
|
} else {
|
|
// Row by row copy
|
|
const uint8_t *src = (const uint8_t *)rawData;
|
|
uint16_t *dst = frame.rawData.data();
|
|
for (uint32_t y = 0; y < height; ++y) {
|
|
memcpy(dst + y * width, src + y * rowStride, width * sizeof(uint16_t));
|
|
}
|
|
}
|
|
|
|
pendingFrames.push_back(std::move(frame));
|
|
return true;
|
|
}
|
|
|
|
// Helper to update descriptor set for image
|
|
void updateImageDescriptorSet(VkDevice device, VkDescriptorSet set,
|
|
VkImageView imageView, VkSampler sampler,
|
|
uint32_t binding) {
|
|
VkDescriptorImageInfo imageInfo{};
|
|
imageInfo.imageLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
|
|
imageInfo.imageView = imageView;
|
|
imageInfo.sampler = sampler;
|
|
|
|
VkWriteDescriptorSet descriptorWrite{};
|
|
descriptorWrite.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
|
|
descriptorWrite.dstSet = set;
|
|
descriptorWrite.dstBinding = binding;
|
|
descriptorWrite.dstArrayElement = 0;
|
|
descriptorWrite.descriptorType = VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
|
|
descriptorWrite.descriptorCount = 1;
|
|
descriptorWrite.pImageInfo = &imageInfo;
|
|
|
|
vkUpdateDescriptorSets(device, 1, &descriptorWrite, 0, nullptr);
|
|
}
|
|
|
|
// Helper to update descriptor set for buffer
|
|
void updateBufferDescriptorSet(VkDevice device, VkDescriptorSet set,
|
|
VkBuffer buffer, VkDeviceSize range,
|
|
uint32_t binding, VkDeviceSize offset = 0) {
|
|
VkDescriptorBufferInfo bufferInfo{};
|
|
bufferInfo.buffer = buffer;
|
|
bufferInfo.offset = offset;
|
|
bufferInfo.range = range;
|
|
|
|
VkWriteDescriptorSet descriptorWrite{};
|
|
descriptorWrite.sType = VK_STRUCTURE_TYPE_WRITE_DESCRIPTOR_SET;
|
|
descriptorWrite.dstSet = set;
|
|
descriptorWrite.dstBinding = binding;
|
|
descriptorWrite.dstArrayElement = 0;
|
|
descriptorWrite.descriptorType = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER;
|
|
descriptorWrite.descriptorCount = 1;
|
|
descriptorWrite.pBufferInfo = &bufferInfo;
|
|
|
|
vkUpdateDescriptorSets(device, 1, &descriptorWrite, 0, nullptr);
|
|
}
|
|
|
|
namespace {
|
|
|
|
inline uint16_t fetchRawClamped(const std::vector<uint16_t> &raw, uint32_t width,
|
|
uint32_t height, int x, int y) {
|
|
x = std::max(0, std::min(x, (int)width - 1));
|
|
y = std::max(0, std::min(y, (int)height - 1));
|
|
return raw[(size_t)y * width + x];
|
|
}
|
|
|
|
inline float fetchGreenProxySample(const std::vector<uint16_t> &raw,
|
|
uint32_t width, uint32_t height,
|
|
int cfaPattern, int planeX, int planeY) {
|
|
int sx = planeX * 2;
|
|
int sy = planeY * 2;
|
|
|
|
float g1 = 0.0f;
|
|
float g2 = 0.0f;
|
|
if (cfaPattern == 0 || cfaPattern == 3) {
|
|
g1 = (float)fetchRawClamped(raw, width, height, sx + 1, sy);
|
|
g2 = (float)fetchRawClamped(raw, width, height, sx, sy + 1);
|
|
} else {
|
|
g1 = (float)fetchRawClamped(raw, width, height, sx, sy);
|
|
g2 = (float)fetchRawClamped(raw, width, height, sx + 1, sy + 1);
|
|
}
|
|
return (g1 + g2) * 0.5f;
|
|
}
|
|
|
|
struct ScalarStats {
|
|
float minValue = 0.0f;
|
|
float maxValue = 0.0f;
|
|
float meanValue = 0.0f;
|
|
float p50 = 0.0f;
|
|
float p90 = 0.0f;
|
|
float p99 = 0.0f;
|
|
};
|
|
|
|
struct FlowSummary {
|
|
ScalarStats magnitude;
|
|
float meanX = 0.0f;
|
|
float meanY = 0.0f;
|
|
};
|
|
|
|
struct RobustnessSummary {
|
|
ScalarStats values;
|
|
float weak015Fraction = 0.0f;
|
|
float weak05Fraction = 0.0f;
|
|
};
|
|
|
|
struct FusionParticipationSummary {
|
|
size_t totalPixels = 0;
|
|
size_t projectedPixels = 0;
|
|
size_t activePixels = 0;
|
|
size_t strongPixels = 0;
|
|
double robustnessWeightedPixels = 0.0;
|
|
|
|
double projectedFraction() const {
|
|
return totalPixels > 0 ? (double)projectedPixels / (double)totalPixels : 0.0;
|
|
}
|
|
|
|
double activeFraction() const {
|
|
return totalPixels > 0 ? (double)activePixels / (double)totalPixels : 0.0;
|
|
}
|
|
|
|
double strongFraction() const {
|
|
return totalPixels > 0 ? (double)strongPixels / (double)totalPixels : 0.0;
|
|
}
|
|
|
|
double weightedFraction() const {
|
|
return totalPixels > 0 ? robustnessWeightedPixels / (double)totalPixels : 0.0;
|
|
}
|
|
};
|
|
|
|
struct LocalReliabilitySummary {
|
|
ScalarStats tileRobustness;
|
|
float edgeTileFraction = 0.0f;
|
|
float edgeGoodFraction = 0.0f;
|
|
float textGoodFraction = 0.0f;
|
|
};
|
|
|
|
struct LocalReliabilityMap {
|
|
uint32_t tilesX = 0;
|
|
uint32_t tilesY = 0;
|
|
std::vector<float> tileWeights;
|
|
LocalReliabilitySummary summary;
|
|
};
|
|
|
|
struct RefinedFrameCache {
|
|
std::vector<Point> alignment;
|
|
std::vector<float> robustness;
|
|
std::vector<float> flowVariance;
|
|
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 RawFlatVarianceChannelStats {
|
|
size_t patches = 0;
|
|
double refMeanSum = 0.0;
|
|
double fusedMeanSum = 0.0;
|
|
double refVarianceSum = 0.0;
|
|
double fusedVarianceSum = 0.0;
|
|
|
|
void add(double refMean, double fusedMean, double refVariance,
|
|
double fusedVariance) {
|
|
++patches;
|
|
refMeanSum += refMean;
|
|
fusedMeanSum += fusedMean;
|
|
refVarianceSum += refVariance;
|
|
fusedVarianceSum += fusedVariance;
|
|
}
|
|
};
|
|
|
|
inline float sampleLensShadingGain(const std::vector<float> &lensShadingMap,
|
|
uint32_t lscWidth, uint32_t lscHeight,
|
|
uint32_t sensorWidth,
|
|
uint32_t sensorHeight, int bayerIndex,
|
|
int x, int y) {
|
|
if (lensShadingMap.empty() || lscWidth == 0 || lscHeight == 0)
|
|
return 1.0f;
|
|
|
|
float u = sensorWidth > 1 ? (float)x / (float)(sensorWidth - 1u) : 0.0f;
|
|
float v = sensorHeight > 1 ? (float)y / (float)(sensorHeight - 1u) : 0.0f;
|
|
float fx = std::clamp(u, 0.0f, 1.0f) * (float)(lscWidth - 1u);
|
|
float fy = std::clamp(v, 0.0f, 1.0f) * (float)(lscHeight - 1u);
|
|
uint32_t x0 = (uint32_t)std::floor(fx);
|
|
uint32_t y0 = (uint32_t)std::floor(fy);
|
|
uint32_t x1 = std::min(x0 + 1u, lscWidth - 1u);
|
|
uint32_t y1 = std::min(y0 + 1u, lscHeight - 1u);
|
|
float tx = fx - (float)x0;
|
|
float ty = fy - (float)y0;
|
|
size_t channel = (size_t)std::clamp(bayerIndex, 0, 3);
|
|
auto read = [&](uint32_t sx, uint32_t sy) -> float {
|
|
size_t idx = ((size_t)sy * lscWidth + sx) * 4u + channel;
|
|
return idx < lensShadingMap.size() ? lensShadingMap[idx] : 1.0f;
|
|
};
|
|
float v00 = read(x0, y0);
|
|
float v10 = read(x1, y0);
|
|
float v01 = read(x0, y1);
|
|
float v11 = read(x1, y1);
|
|
return std::max(0.0f, (1.0f - ty) * ((1.0f - tx) * v00 + tx * v10) +
|
|
ty * ((1.0f - tx) * v01 + tx * v11));
|
|
}
|
|
|
|
inline float normalizedReferenceRawAt(
|
|
const std::vector<uint16_t> &raw, uint32_t sensorWidth,
|
|
uint32_t sensorHeight, const float *blackLevel, float whiteLevel,
|
|
const std::vector<float> &lensShadingMap, uint32_t lscWidth,
|
|
uint32_t lscHeight, int bayerIndex, int x, int y) {
|
|
x = std::clamp(x, 0, (int)sensorWidth - 1);
|
|
y = std::clamp(y, 0, (int)sensorHeight - 1);
|
|
float rawValue = (float)raw[(size_t)y * sensorWidth + (uint32_t)x];
|
|
float val = std::max(0.0f, rawValue - blackLevel[bayerIndex]);
|
|
val *= sampleLensShadingGain(lensShadingMap, lscWidth, lscHeight,
|
|
sensorWidth, sensorHeight, bayerIndex, x, y);
|
|
float whiteRange = std::max(whiteLevel - blackLevel[bayerIndex], 1.0f);
|
|
return std::clamp(val / whiteRange, 0.0f, 1.0f);
|
|
}
|
|
|
|
inline float fusedRawAtPlaneSample(const uint16_t *fused, uint32_t outputW,
|
|
uint32_t outputH, int cfaPattern,
|
|
int bayerIndex, float scale, int planeX,
|
|
int planeY) {
|
|
Point offset = getBayerPlaneOffset(cfaPattern, bayerIndex);
|
|
int outPlaneX = (int)std::lround((float)planeX * scale);
|
|
int outPlaneY = (int)std::lround((float)planeY * scale);
|
|
int outX = (int)offset.x + outPlaneX * 2;
|
|
int outY = (int)offset.y + outPlaneY * 2;
|
|
outX = std::clamp(outX, 0, (int)outputW - 1);
|
|
outY = std::clamp(outY, 0, (int)outputH - 1);
|
|
return (float)fused[(size_t)outY * outputW + (uint32_t)outX] / 65535.0f;
|
|
}
|
|
|
|
inline float greenProxyFlatScore(const GrayImage &proxy, int x, int y) {
|
|
if (proxy.data.empty() || proxy.width <= 4 || proxy.height <= 4)
|
|
return std::numeric_limits<float>::max();
|
|
double sum = 0.0;
|
|
double sumSq = 0.0;
|
|
double gradSum = 0.0;
|
|
int count = 0;
|
|
for (int dy = -2; dy <= 2; ++dy) {
|
|
for (int dx = -2; dx <= 2; ++dx) {
|
|
int px = std::clamp(x + dx, 0, proxy.width - 1);
|
|
int py = std::clamp(y + dy, 0, proxy.height - 1);
|
|
float center = (float)proxy.data[(size_t)py * proxy.width + px] / 255.0f;
|
|
float xp = (float)proxy.data[(size_t)py * proxy.width +
|
|
std::min(px + 1, proxy.width - 1)] /
|
|
255.0f;
|
|
float xm = (float)proxy.data[(size_t)py * proxy.width +
|
|
std::max(px - 1, 0)] /
|
|
255.0f;
|
|
float yp = (float)proxy.data[(size_t)std::min(py + 1, proxy.height - 1) *
|
|
proxy.width +
|
|
px] /
|
|
255.0f;
|
|
float ym = (float)proxy.data[(size_t)std::max(py - 1, 0) * proxy.width +
|
|
px] /
|
|
255.0f;
|
|
sum += center;
|
|
sumSq += center * center;
|
|
gradSum += std::abs(xp - xm) + std::abs(yp - ym);
|
|
++count;
|
|
}
|
|
}
|
|
double mean = sum / std::max(count, 1);
|
|
double variance = std::max(0.0, sumSq / std::max(count, 1) - mean * mean);
|
|
return (float)(variance + 0.25 * gradSum / std::max(count, 1));
|
|
}
|
|
|
|
inline void logRawFlatVarianceDiagnostics(
|
|
const std::vector<uint16_t> &referenceRaw, const uint16_t *fused,
|
|
uint32_t sensorWidth, uint32_t sensorHeight, uint32_t outputW,
|
|
uint32_t outputH, int cfaPattern, float scale, const float *blackLevel,
|
|
float whiteLevel, const std::vector<float> &lensShadingMap,
|
|
uint32_t lscWidth, uint32_t lscHeight, const GrayImage &referenceProxy) {
|
|
if (!kEnableRawFlatVarianceLogs || referenceRaw.empty() || fused == nullptr ||
|
|
referenceProxy.data.empty())
|
|
return;
|
|
|
|
struct Candidate {
|
|
float score;
|
|
int x;
|
|
int y;
|
|
};
|
|
std::vector<Candidate> candidates;
|
|
for (int y = 4; y < referenceProxy.height - 4; y += 8) {
|
|
for (int x = 4; x < referenceProxy.width - 4; x += 8) {
|
|
candidates.push_back({greenProxyFlatScore(referenceProxy, x, y), x, y});
|
|
}
|
|
}
|
|
if (candidates.empty())
|
|
return;
|
|
std::sort(candidates.begin(), candidates.end(),
|
|
[](const Candidate &a, const Candidate &b) {
|
|
return a.score < b.score;
|
|
});
|
|
|
|
size_t targetFlatCount = std::max((size_t)64, candidates.size() / 4);
|
|
size_t flatCount =
|
|
std::min(candidates.size(), std::min(targetFlatCount, (size_t)2048));
|
|
RawFlatVarianceChannelStats channelStats[4];
|
|
constexpr int kPatchRadius = 2;
|
|
for (size_t ci = 0; ci < flatCount; ++ci) {
|
|
int centerPlaneX = candidates[ci].x;
|
|
int centerPlaneY = candidates[ci].y;
|
|
for (int bayerIndex = 0; bayerIndex < 4; ++bayerIndex) {
|
|
Point offset = getBayerPlaneOffset(cfaPattern, bayerIndex);
|
|
double refSum = 0.0;
|
|
double refSumSq = 0.0;
|
|
double fusedSum = 0.0;
|
|
double fusedSumSq = 0.0;
|
|
int count = 0;
|
|
for (int dy = -kPatchRadius; dy <= kPatchRadius; ++dy) {
|
|
for (int dx = -kPatchRadius; dx <= kPatchRadius; ++dx) {
|
|
int planeX = centerPlaneX + dx;
|
|
int planeY = centerPlaneY + dy;
|
|
int sx = (int)offset.x + planeX * 2;
|
|
int sy = (int)offset.y + planeY * 2;
|
|
if (sx < 0 || sy < 0 || sx >= (int)sensorWidth ||
|
|
sy >= (int)sensorHeight) {
|
|
continue;
|
|
}
|
|
float ref = normalizedReferenceRawAt(
|
|
referenceRaw, sensorWidth, sensorHeight, blackLevel, whiteLevel,
|
|
lensShadingMap, lscWidth, lscHeight, bayerIndex, sx, sy);
|
|
float out = fusedRawAtPlaneSample(fused, outputW, outputH, cfaPattern,
|
|
bayerIndex, scale, planeX, planeY);
|
|
refSum += ref;
|
|
refSumSq += ref * ref;
|
|
fusedSum += out;
|
|
fusedSumSq += out * out;
|
|
++count;
|
|
}
|
|
}
|
|
if (count >= 9) {
|
|
double refMean = refSum / count;
|
|
double fusedMean = fusedSum / count;
|
|
double refVar = std::max(0.0, refSumSq / count - refMean * refMean);
|
|
double fusedVar =
|
|
std::max(0.0, fusedSumSq / count - fusedMean * fusedMean);
|
|
channelStats[bayerIndex].add(refMean, fusedMean, refVar, fusedVar);
|
|
}
|
|
}
|
|
}
|
|
|
|
static constexpr const char *kChannelNames[4] = {"R", "Gr", "Gb", "B"};
|
|
double ratioSum = 0.0;
|
|
double gainSum = 0.0;
|
|
size_t ratioCount = 0;
|
|
for (int c = 0; c < 4; ++c) {
|
|
const RawFlatVarianceChannelStats &s = channelStats[c];
|
|
if (s.patches == 0)
|
|
continue;
|
|
double refVar = s.refVarianceSum / (double)s.patches;
|
|
double fusedVar = s.fusedVarianceSum / (double)s.patches;
|
|
double ratio = refVar > 1e-12 ? fusedVar / refVar : 0.0;
|
|
double gain = fusedVar > 1e-12 ? refVar / fusedVar : 0.0;
|
|
double stdRatio = std::sqrt(std::max(ratio, 0.0));
|
|
LOGI("RawDenoiseEffect[%s]: patches=%zu refMean=%.6f fusedMean=%.6f "
|
|
"refVar=%.9f fusedVar=%.9f varRatio=%.3f varGain=%.2f stdRatio=%.3f",
|
|
kChannelNames[c], s.patches, s.refMeanSum / (double)s.patches,
|
|
s.fusedMeanSum / (double)s.patches, refVar, fusedVar, ratio, gain,
|
|
stdRatio);
|
|
if (ratio > 0.0 && gain > 0.0) {
|
|
ratioSum += ratio;
|
|
gainSum += gain;
|
|
++ratioCount;
|
|
}
|
|
}
|
|
if (ratioCount > 0) {
|
|
double avgRatio = ratioSum / (double)ratioCount;
|
|
double avgGain = gainSum / (double)ratioCount;
|
|
LOGI("RawDenoiseEffect summary: channels=%zu avgVarRatio=%.3f "
|
|
"avgVarGain=%.2f avgStdRatio=%.3f",
|
|
ratioCount, avgRatio, avgGain,
|
|
std::sqrt(std::max(avgRatio, 0.0)));
|
|
}
|
|
}
|
|
|
|
struct GreenPhaseSummary {
|
|
ScalarStats phase0Mean;
|
|
ScalarStats phase1Mean;
|
|
float phase0DominantFraction = 0.0f;
|
|
float phase1DominantFraction = 0.0f;
|
|
float conflictFraction = 0.0f;
|
|
};
|
|
|
|
inline ScalarStats computeScalarStats(const float *data, size_t count) {
|
|
ScalarStats stats;
|
|
if (count == 0 || data == nullptr)
|
|
return stats;
|
|
|
|
std::vector<float> sorted(data, data + count);
|
|
std::sort(sorted.begin(), sorted.end());
|
|
double sum = 0.0;
|
|
for (float v : sorted)
|
|
sum += v;
|
|
|
|
auto percentile = [&](float q) -> float {
|
|
size_t idx =
|
|
std::min(sorted.size() - 1, (size_t)std::floor(q * (sorted.size() - 1)));
|
|
return sorted[idx];
|
|
};
|
|
|
|
stats.minValue = sorted.front();
|
|
stats.maxValue = sorted.back();
|
|
stats.meanValue = (float)(sum / (double)sorted.size());
|
|
stats.p50 = percentile(0.50f);
|
|
stats.p90 = percentile(0.90f);
|
|
stats.p99 = percentile(0.99f);
|
|
return stats;
|
|
}
|
|
|
|
inline FlowSummary readFlowSummary(VkDevice device, VkDeviceMemory alignMem,
|
|
uint32_t gridW, uint32_t gridH) {
|
|
FlowSummary summary;
|
|
if (gridW == 0 || gridH == 0)
|
|
return summary;
|
|
|
|
void *alignPtr = nullptr;
|
|
if (vkMapMemory(device, alignMem, 0, VK_WHOLE_SIZE, 0, &alignPtr) !=
|
|
VK_SUCCESS ||
|
|
alignPtr == nullptr) {
|
|
return summary;
|
|
}
|
|
|
|
Point *points = static_cast<Point *>(alignPtr);
|
|
std::vector<float> magnitudes;
|
|
magnitudes.reserve((size_t)gridW * gridH);
|
|
double sumX = 0.0;
|
|
double sumY = 0.0;
|
|
for (size_t i = 0; i < (size_t)gridW * gridH; ++i) {
|
|
sumX += points[i].x;
|
|
sumY += points[i].y;
|
|
magnitudes.push_back(
|
|
std::sqrt(points[i].x * points[i].x + points[i].y * points[i].y));
|
|
}
|
|
summary.magnitude = computeScalarStats(magnitudes.data(), magnitudes.size());
|
|
summary.meanX = (float)(sumX / (double)magnitudes.size());
|
|
summary.meanY = (float)(sumY / (double)magnitudes.size());
|
|
vkUnmapMemory(device, alignMem);
|
|
return summary;
|
|
}
|
|
|
|
inline RobustnessSummary readRobustnessSummary(VkDevice device,
|
|
VkDeviceMemory robustMem,
|
|
uint32_t planeW,
|
|
uint32_t planeH) {
|
|
RobustnessSummary summary;
|
|
if (planeW == 0 || planeH == 0)
|
|
return summary;
|
|
|
|
void *robustPtr = nullptr;
|
|
if (vkMapMemory(device, robustMem, 0, VK_WHOLE_SIZE, 0, &robustPtr) !=
|
|
VK_SUCCESS ||
|
|
robustPtr == nullptr) {
|
|
return summary;
|
|
}
|
|
|
|
size_t count = (size_t)planeW * planeH;
|
|
const float *robustness = static_cast<const float *>(robustPtr);
|
|
summary.values = computeScalarStats(robustness, count);
|
|
size_t weak015 = 0;
|
|
size_t weak05 = 0;
|
|
for (size_t i = 0; i < count; ++i) {
|
|
if (robustness[i] < 0.15f)
|
|
++weak015;
|
|
if (robustness[i] < 0.5f)
|
|
++weak05;
|
|
}
|
|
summary.weak015Fraction = (float)weak015 / (float)count;
|
|
summary.weak05Fraction = (float)weak05 / (float)count;
|
|
vkUnmapMemory(device, robustMem);
|
|
return summary;
|
|
}
|
|
|
|
inline RobustnessSummary readRobustnessSummary(const std::vector<float> &robustness,
|
|
uint32_t planeW,
|
|
uint32_t planeH) {
|
|
RobustnessSummary summary;
|
|
if (planeW == 0 || planeH == 0 || robustness.empty())
|
|
return summary;
|
|
|
|
size_t count = std::min((size_t)planeW * planeH, robustness.size());
|
|
summary.values = computeScalarStats(robustness.data(), count);
|
|
size_t weak015 = 0;
|
|
size_t weak05 = 0;
|
|
for (size_t i = 0; i < count; ++i) {
|
|
if (robustness[i] < 0.15f)
|
|
++weak015;
|
|
if (robustness[i] < 0.5f)
|
|
++weak05;
|
|
}
|
|
summary.weak015Fraction = (float)weak015 / (float)count;
|
|
summary.weak05Fraction = (float)weak05 / (float)count;
|
|
return summary;
|
|
}
|
|
|
|
inline void logAlignmentDiagnostics(const char *label, size_t frameIndex,
|
|
const TileAlignment &alignment) {
|
|
if (!kEnableRawSuperResPerfLogs)
|
|
return;
|
|
ScalarStats errStats = computeScalarStats(alignment.errorMap.data(),
|
|
alignment.errorMap.size());
|
|
LOGI("RawSuperResPerf %s[%zu]: errP50=%.2f errP90=%.2f", label, frameIndex,
|
|
std::sqrt(std::max(errStats.p50, 0.0f)),
|
|
std::sqrt(std::max(errStats.p90, 0.0f)));
|
|
}
|
|
|
|
inline void logRefinedFieldDiagnostics(size_t frameIndex, float frameWeight,
|
|
const RobustnessSummary &rs,
|
|
const FlowSummary &flow) {
|
|
if (!kEnableRawSuperResPerfLogs)
|
|
return;
|
|
LOGI("RawSuperResPerf frame[%zu]: weight=%.3f meanR=%.3f p50R=%.3f weak05F=%.3f "
|
|
"flow90=%.2f contr=%.3f",
|
|
frameIndex, frameWeight, rs.values.meanValue, rs.values.p50,
|
|
rs.weak05Fraction, flow.magnitude.p90, frameWeight * rs.values.meanValue);
|
|
}
|
|
|
|
inline Point sampleAlignmentField(const std::vector<Point> &alignment,
|
|
uint32_t gridW, uint32_t gridH,
|
|
uint32_t tileSize, float planeX,
|
|
float planeY) {
|
|
if (alignment.empty() || gridW == 0 || gridH == 0 || tileSize == 0)
|
|
return {0.0f, 0.0f};
|
|
|
|
float gx = planeX / (float)tileSize - 0.5f;
|
|
float gy = planeY / (float)tileSize - 0.5f;
|
|
int x0 = (int)std::floor(gx);
|
|
int y0 = (int)std::floor(gy);
|
|
float fx = gx - (float)x0;
|
|
float fy = gy - (float)y0;
|
|
int x1 = x0 + 1;
|
|
int y1 = y0 + 1;
|
|
x0 = std::clamp(x0, 0, (int)gridW - 1);
|
|
y0 = std::clamp(y0, 0, (int)gridH - 1);
|
|
x1 = std::clamp(x1, 0, (int)gridW - 1);
|
|
y1 = std::clamp(y1, 0, (int)gridH - 1);
|
|
|
|
auto at = [&](int x, int y) -> Point {
|
|
size_t idx = (size_t)y * gridW + (size_t)x;
|
|
return idx < alignment.size() ? alignment[idx] : Point{0.0f, 0.0f};
|
|
};
|
|
|
|
Point p00 = at(x0, y0);
|
|
Point p10 = at(x1, y0);
|
|
Point p01 = at(x0, y1);
|
|
Point p11 = at(x1, y1);
|
|
Point top{p00.x + (p10.x - p00.x) * fx, p00.y + (p10.y - p00.y) * fx};
|
|
Point bottom{p01.x + (p11.x - p01.x) * fx,
|
|
p01.y + (p11.y - p01.y) * fx};
|
|
return {top.x + (bottom.x - top.x) * fy,
|
|
top.y + (bottom.y - top.y) * fy};
|
|
}
|
|
|
|
inline bool splatTouchesOutput(float outCenterX, float outCenterY,
|
|
uint32_t outputW, uint32_t outputH) {
|
|
int baseX = (int)std::floor(outCenterX);
|
|
int baseY = (int)std::floor(outCenterY);
|
|
return baseX + 2 >= 0 && baseY + 2 >= 0 && baseX - 1 < (int)outputW &&
|
|
baseY - 1 < (int)outputH;
|
|
}
|
|
|
|
inline FusionParticipationSummary computeFusionParticipation(
|
|
uint32_t sensorW, uint32_t sensorH, uint32_t planeW, uint32_t planeH,
|
|
uint32_t outputW, uint32_t outputH, float scale,
|
|
const std::vector<float> &robustness, const std::vector<Point> &alignment,
|
|
uint32_t gridW, uint32_t gridH, uint32_t tileSize, bool isReference) {
|
|
FusionParticipationSummary summary;
|
|
if (sensorW == 0 || sensorH == 0 || planeW == 0 || planeH == 0 ||
|
|
outputW == 0 || outputH == 0)
|
|
return summary;
|
|
|
|
const size_t robustCount = robustness.size();
|
|
for (uint32_t y = 0; y < planeH; ++y) {
|
|
for (uint32_t x = 0; x < planeW; ++x) {
|
|
uint32_t rawPixelCount = 0;
|
|
for (uint32_t dy = 0; dy < 2; ++dy) {
|
|
for (uint32_t dx = 0; dx < 2; ++dx) {
|
|
if (x * 2 + dx < sensorW && y * 2 + dy < sensorH)
|
|
++rawPixelCount;
|
|
}
|
|
}
|
|
if (rawPixelCount == 0)
|
|
continue;
|
|
|
|
summary.totalPixels += rawPixelCount;
|
|
|
|
Point flow{0.0f, 0.0f};
|
|
if (!isReference) {
|
|
flow = sampleAlignmentField(alignment, gridW, gridH, tileSize,
|
|
(float)x, (float)y);
|
|
}
|
|
float refX = (float)x - flow.x;
|
|
float refY = (float)y - flow.y;
|
|
if (!splatTouchesOutput(refX * scale, refY * scale, outputW, outputH))
|
|
continue;
|
|
|
|
summary.projectedPixels += rawPixelCount;
|
|
|
|
float r = 1.0f;
|
|
size_t idx = (size_t)y * planeW + x;
|
|
if (!isReference) {
|
|
r = idx < robustCount ? robustness[idx] : 0.0f;
|
|
if (!std::isfinite(r))
|
|
r = 0.0f;
|
|
r = std::clamp(r, 0.0f, 1.0f);
|
|
}
|
|
summary.robustnessWeightedPixels += (double)rawPixelCount * (double)r;
|
|
if (r >= 0.15f)
|
|
summary.activePixels += rawPixelCount;
|
|
if (r >= 0.5f)
|
|
summary.strongPixels += rawPixelCount;
|
|
}
|
|
}
|
|
return summary;
|
|
}
|
|
|
|
inline void logFusionParticipation(size_t frameIndex, bool skipped,
|
|
float frameWeight,
|
|
const FusionParticipationSummary &summary) {
|
|
if (!kEnableRawSuperResPerfLogs)
|
|
return;
|
|
LOGI("RawSuperResFusionCoverage frame[%zu]: skipped=%d weight=%.3f "
|
|
"projected=%zu/%zu(%.2f%%) activeR015=%zu/%zu(%.2f%%) "
|
|
"strongR05=%zu/%zu(%.2f%%) weightedR=%.2f%%",
|
|
frameIndex, skipped ? 1 : 0, frameWeight, summary.projectedPixels,
|
|
summary.totalPixels, summary.projectedFraction() * 100.0,
|
|
summary.activePixels, summary.totalPixels,
|
|
summary.activeFraction() * 100.0, summary.strongPixels,
|
|
summary.totalPixels, summary.strongFraction() * 100.0,
|
|
summary.weightedFraction() * 100.0);
|
|
}
|
|
|
|
inline void logRefinedFieldDiagnostics(VkDevice device, VkDeviceMemory alignMem,
|
|
VkDeviceMemory robustMem, uint32_t gridW,
|
|
uint32_t gridH, uint32_t planeW,
|
|
uint32_t planeH, size_t frameIndex) {
|
|
if (!kEnableRawSuperResPerfLogs)
|
|
return;
|
|
FlowSummary flow = readFlowSummary(device, alignMem, gridW, gridH);
|
|
RobustnessSummary robustness =
|
|
readRobustnessSummary(device, robustMem, planeW, planeH);
|
|
logRefinedFieldDiagnostics(frameIndex, 1.0f, robustness, flow);
|
|
}
|
|
|
|
inline bool uploadAlignmentField(VkDevice device, VkDeviceMemory alignMem,
|
|
const std::vector<Point> &field) {
|
|
if (field.empty())
|
|
return false;
|
|
|
|
void *mapPtr = nullptr;
|
|
VkResult res = vkMapMemory(device, alignMem, 0,
|
|
(VkDeviceSize)field.size() * sizeof(Point), 0,
|
|
&mapPtr);
|
|
if (res != VK_SUCCESS || mapPtr == nullptr)
|
|
return false;
|
|
|
|
std::memcpy(mapPtr, field.data(), field.size() * sizeof(Point));
|
|
vkUnmapMemory(device, alignMem);
|
|
return true;
|
|
}
|
|
|
|
inline bool uploadFloatBuffer(VkDevice device, VkDeviceMemory memory,
|
|
const std::vector<float> &values) {
|
|
if (values.empty())
|
|
return false;
|
|
|
|
void *mapPtr = nullptr;
|
|
VkResult res = vkMapMemory(device, memory, 0,
|
|
(VkDeviceSize)values.size() * sizeof(float), 0,
|
|
&mapPtr);
|
|
if (res != VK_SUCCESS || mapPtr == nullptr)
|
|
return false;
|
|
|
|
std::memcpy(mapPtr, values.data(), values.size() * sizeof(float));
|
|
vkUnmapMemory(device, memory);
|
|
return true;
|
|
}
|
|
|
|
inline bool fillFloatBuffer(VkDevice device, VkDeviceMemory memory, size_t count,
|
|
float value) {
|
|
if (count == 0)
|
|
return false;
|
|
void *mapPtr = nullptr;
|
|
VkResult res = vkMapMemory(device, memory, 0, (VkDeviceSize)count * sizeof(float),
|
|
0, &mapPtr);
|
|
if (res != VK_SUCCESS || mapPtr == nullptr)
|
|
return false;
|
|
float *dst = static_cast<float *>(mapPtr);
|
|
std::fill(dst, dst + count, value);
|
|
vkUnmapMemory(device, memory);
|
|
return true;
|
|
}
|
|
|
|
inline bool downloadAlignmentField(VkDevice device, VkDeviceMemory alignMem,
|
|
uint32_t gridW, uint32_t gridH,
|
|
std::vector<Point> &field) {
|
|
size_t count = (size_t)gridW * gridH;
|
|
if (count == 0)
|
|
return false;
|
|
|
|
void *mapPtr = nullptr;
|
|
VkResult res =
|
|
vkMapMemory(device, alignMem, 0, (VkDeviceSize)count * sizeof(Point), 0,
|
|
&mapPtr);
|
|
if (res != VK_SUCCESS || mapPtr == nullptr)
|
|
return false;
|
|
|
|
field.resize(count);
|
|
std::memcpy(field.data(), mapPtr, count * sizeof(Point));
|
|
vkUnmapMemory(device, alignMem);
|
|
return true;
|
|
}
|
|
|
|
inline bool downloadFloatBuffer(VkDevice device, VkDeviceMemory memory,
|
|
size_t count, std::vector<float> &values) {
|
|
if (count == 0)
|
|
return false;
|
|
|
|
void *mapPtr = nullptr;
|
|
VkResult res =
|
|
vkMapMemory(device, memory, 0, (VkDeviceSize)count * sizeof(float), 0,
|
|
&mapPtr);
|
|
if (res != VK_SUCCESS || mapPtr == nullptr)
|
|
return false;
|
|
|
|
values.resize(count);
|
|
std::memcpy(values.data(), mapPtr, count * sizeof(float));
|
|
vkUnmapMemory(device, memory);
|
|
return true;
|
|
}
|
|
|
|
inline std::vector<Point>
|
|
buildDenseAlignmentSeed(const TileAlignment &coarseAlign, uint32_t gridW,
|
|
uint32_t gridH, uint32_t tileSize) {
|
|
std::vector<Point> seed((size_t)gridW * gridH);
|
|
for (uint32_t gy = 0; gy < gridH; ++gy) {
|
|
for (uint32_t gx = 0; gx < gridW; ++gx) {
|
|
int cx = (int)(gx * tileSize + tileSize / 2);
|
|
int cy = (int)(gy * tileSize + tileSize / 2);
|
|
seed[(size_t)gy * gridW + gx] = coarseAlign.getOffset(cx, cy);
|
|
}
|
|
}
|
|
return seed;
|
|
}
|
|
|
|
inline float computeProxyDetail(const GrayImage &proxy, int x, int y) {
|
|
if (proxy.data.empty() || proxy.width <= 0 || proxy.height <= 0)
|
|
return 0.0f;
|
|
|
|
x = std::clamp(x, 0, proxy.width - 1);
|
|
y = std::clamp(y, 0, proxy.height - 1);
|
|
int x0 = std::max(0, x - 1);
|
|
int x1 = std::min(proxy.width - 1, x + 1);
|
|
int y0 = std::max(0, y - 1);
|
|
int y1 = std::min(proxy.height - 1, y + 1);
|
|
|
|
float center = (float)proxy.data[(size_t)y * proxy.width + x];
|
|
float xp = (float)proxy.data[(size_t)y * proxy.width + x1];
|
|
float xm = (float)proxy.data[(size_t)y * proxy.width + x0];
|
|
float yp = (float)proxy.data[(size_t)y1 * proxy.width + x];
|
|
float ym = (float)proxy.data[(size_t)y0 * proxy.width + x];
|
|
float gx = std::abs(xp - xm);
|
|
float gy = std::abs(yp - ym);
|
|
float lap = std::abs(4.0f * center - xp - xm - yp - ym);
|
|
return gx + gy + 0.5f * lap;
|
|
}
|
|
|
|
inline void regularizeAlignmentField(std::vector<Point> &field, uint32_t gridW,
|
|
uint32_t gridH, uint32_t tileSize,
|
|
const GrayImage &referenceProxy) {
|
|
if (field.empty() || gridW == 0 || gridH == 0 || referenceProxy.data.empty())
|
|
return;
|
|
|
|
std::vector<Point> scratch(field.size());
|
|
for (int pass = 0; pass < kAlignmentRegularizationPasses; ++pass) {
|
|
const std::vector<Point> &src = (pass & 1) == 0 ? field : scratch;
|
|
std::vector<Point> &dst = (pass & 1) == 0 ? scratch : field;
|
|
|
|
for (uint32_t gy = 0; gy < gridH; ++gy) {
|
|
for (uint32_t gx = 0; gx < gridW; ++gx) {
|
|
size_t idx = (size_t)gy * gridW + gx;
|
|
const Point center = src[idx];
|
|
|
|
int px = (int)(gx * tileSize + tileSize / 2);
|
|
int py = (int)(gy * tileSize + tileSize / 2);
|
|
float detail =
|
|
computeProxyDetail(referenceProxy, px, py) * (1.0f / 96.0f);
|
|
float edgeKeep = std::clamp(detail, 0.0f, 1.0f);
|
|
|
|
float sumW = 0.0f;
|
|
float sumX = 0.0f;
|
|
float sumY = 0.0f;
|
|
float medianBest = std::numeric_limits<float>::max();
|
|
Point medianCandidate = center;
|
|
|
|
for (int oy = -1; oy <= 1; ++oy) {
|
|
int ny = std::clamp((int)gy + oy, 0, (int)gridH - 1);
|
|
for (int ox = -1; ox <= 1; ++ox) {
|
|
int nx = std::clamp((int)gx + ox, 0, (int)gridW - 1);
|
|
const Point neighbor = src[(size_t)ny * gridW + nx];
|
|
float dx = neighbor.x - center.x;
|
|
float dy = neighbor.y - center.y;
|
|
float dist2 = (float)(ox * ox + oy * oy);
|
|
float motion2 = dx * dx + dy * dy;
|
|
float w = std::exp(-0.75f * dist2) *
|
|
std::exp(-motion2 / (0.30f + 0.70f * edgeKeep));
|
|
sumW += w;
|
|
sumX += neighbor.x * w;
|
|
sumY += neighbor.y * w;
|
|
|
|
float candidateCost = 0.0f;
|
|
for (int iy = -1; iy <= 1; ++iy) {
|
|
int sy = std::clamp((int)gy + iy, 0, (int)gridH - 1);
|
|
for (int ix = -1; ix <= 1; ++ix) {
|
|
int sx = std::clamp((int)gx + ix, 0, (int)gridW - 1);
|
|
const Point sample = src[(size_t)sy * gridW + sx];
|
|
float ddx = sample.x - neighbor.x;
|
|
float ddy = sample.y - neighbor.y;
|
|
candidateCost += std::sqrt(ddx * ddx + ddy * ddy);
|
|
}
|
|
}
|
|
if (candidateCost < medianBest) {
|
|
medianBest = candidateCost;
|
|
medianCandidate = neighbor;
|
|
}
|
|
}
|
|
}
|
|
|
|
Point smooth = center;
|
|
if (sumW > 1e-5f) {
|
|
smooth.x = sumX / sumW;
|
|
smooth.y = sumY / sumW;
|
|
}
|
|
|
|
float smoothBlend = 0.70f - 0.45f * edgeKeep;
|
|
Point blended;
|
|
blended.x = center.x + (smooth.x - center.x) * smoothBlend;
|
|
blended.y = center.y + (smooth.y - center.y) * smoothBlend;
|
|
|
|
float mdx = blended.x - medianCandidate.x;
|
|
float mdy = blended.y - medianCandidate.y;
|
|
float medianDist = std::sqrt(mdx * mdx + mdy * mdy);
|
|
if (medianDist > kAlignmentOutlierThreshold) {
|
|
float clampBlend = std::clamp((medianDist - kAlignmentOutlierThreshold) /
|
|
(0.35f + 0.35f * edgeKeep),
|
|
0.0f, 1.0f);
|
|
blended.x += (medianCandidate.x - blended.x) * clampBlend;
|
|
blended.y += (medianCandidate.y - blended.y) * clampBlend;
|
|
}
|
|
|
|
dst[idx] = blended;
|
|
}
|
|
}
|
|
}
|
|
|
|
if ((kAlignmentRegularizationPasses & 1) != 0)
|
|
field.swap(scratch);
|
|
}
|
|
|
|
inline LocalReliabilityMap readLocalReliabilityMap(VkDevice device,
|
|
VkDeviceMemory robustMem,
|
|
uint32_t planeW,
|
|
uint32_t planeH,
|
|
const GrayImage &referenceProxy) {
|
|
LocalReliabilityMap result;
|
|
if (planeW == 0 || planeH == 0 || referenceProxy.data.empty())
|
|
return result;
|
|
|
|
void *robustPtr = nullptr;
|
|
if (vkMapMemory(device, robustMem, 0, VK_WHOLE_SIZE, 0, &robustPtr) !=
|
|
VK_SUCCESS ||
|
|
robustPtr == nullptr) {
|
|
return result;
|
|
}
|
|
|
|
const float *robustness = static_cast<const float *>(robustPtr);
|
|
const uint32_t localTileSize = 16;
|
|
const uint32_t tilesX = (planeW + localTileSize - 1) / localTileSize;
|
|
const uint32_t tilesY = (planeH + localTileSize - 1) / localTileSize;
|
|
result.tilesX = tilesX;
|
|
result.tilesY = tilesY;
|
|
result.tileWeights.resize((size_t)tilesX * tilesY, 1.0f);
|
|
auto saturate = [](float v) { return std::clamp(v, 0.0f, 1.0f); };
|
|
|
|
struct TileMetric {
|
|
float detailScore = 0.0f;
|
|
float meanRobustness = 0.0f;
|
|
float weakFraction = 0.0f;
|
|
};
|
|
|
|
std::vector<TileMetric> tileMetrics;
|
|
tileMetrics.reserve((size_t)tilesX * tilesY);
|
|
std::vector<float> tileDetails;
|
|
std::vector<float> tileRobustnessMeans;
|
|
tileDetails.reserve((size_t)tilesX * tilesY);
|
|
tileRobustnessMeans.reserve((size_t)tilesX * tilesY);
|
|
|
|
for (uint32_t ty = 0; ty < tilesY; ++ty) {
|
|
for (uint32_t tx = 0; tx < tilesX; ++tx) {
|
|
uint32_t x0 = tx * localTileSize;
|
|
uint32_t y0 = ty * localTileSize;
|
|
uint32_t x1 = std::min(x0 + localTileSize, planeW);
|
|
uint32_t y1 = std::min(y0 + localTileSize, planeH);
|
|
|
|
double detailSum = 0.0;
|
|
double robustSum = 0.0;
|
|
size_t weakCount = 0;
|
|
size_t count = 0;
|
|
|
|
for (uint32_t y = y0; y < y1; ++y) {
|
|
for (uint32_t x = x0; x < x1; ++x) {
|
|
size_t idx = (size_t)y * planeW + x;
|
|
float center = (float)referenceProxy.data[idx];
|
|
float xp =
|
|
(float)referenceProxy.data[(size_t)y * planeW + std::min(x + 1, planeW - 1)];
|
|
float xm =
|
|
(float)referenceProxy.data[(size_t)y * planeW + (x > 0 ? x - 1 : 0)];
|
|
float yp =
|
|
(float)referenceProxy.data[(size_t)std::min(y + 1, planeH - 1) * planeW + x];
|
|
float ym =
|
|
(float)referenceProxy.data[(size_t)(y > 0 ? y - 1 : 0) * planeW + x];
|
|
float gx = std::abs(xp - xm);
|
|
float gy = std::abs(yp - ym);
|
|
float lap = std::abs(4.0f * center - xp - xm - yp - ym);
|
|
detailSum += gx + gy + 0.5 * lap;
|
|
|
|
float r = robustness[idx];
|
|
robustSum += r;
|
|
if (r < 0.5f)
|
|
++weakCount;
|
|
++count;
|
|
}
|
|
}
|
|
|
|
TileMetric metric;
|
|
if (count > 0) {
|
|
metric.detailScore = (float)(detailSum / (double)count);
|
|
metric.meanRobustness = (float)(robustSum / (double)count);
|
|
metric.weakFraction = (float)weakCount / (float)count;
|
|
}
|
|
tileMetrics.push_back(metric);
|
|
tileDetails.push_back(metric.detailScore);
|
|
tileRobustnessMeans.push_back(metric.meanRobustness);
|
|
}
|
|
}
|
|
|
|
result.summary.tileRobustness =
|
|
computeScalarStats(tileRobustnessMeans.data(), tileRobustnessMeans.size());
|
|
ScalarStats detailStats =
|
|
computeScalarStats(tileDetails.data(), tileDetails.size());
|
|
float edgeThreshold = std::max(detailStats.p50, detailStats.p90 * 0.65f);
|
|
float textThreshold = std::max(detailStats.p90, detailStats.p99 * 0.75f);
|
|
|
|
size_t edgeTiles = 0;
|
|
size_t edgeGoodTiles = 0;
|
|
size_t textTiles = 0;
|
|
size_t textGoodTiles = 0;
|
|
for (size_t idx = 0; idx < tileMetrics.size(); ++idx) {
|
|
const TileMetric &metric = tileMetrics[idx];
|
|
bool isEdgeTile = metric.detailScore >= edgeThreshold;
|
|
bool isTextTile = metric.detailScore >= textThreshold;
|
|
float robustNorm = saturate((metric.meanRobustness - 0.58f) / 0.24f);
|
|
float weakPenalty = saturate(1.0f - std::max(0.0f, metric.weakFraction - 0.10f) / 0.30f);
|
|
float detailBoost = isTextTile ? 1.0f : (isEdgeTile ? 0.70f : 0.35f);
|
|
float tileWeight = saturate((0.55f * robustNorm + 0.45f * weakPenalty) *
|
|
(0.55f + 0.45f * detailBoost));
|
|
if (isTextTile) {
|
|
tileWeight = std::max(tileWeight, 0.35f);
|
|
} else if (isEdgeTile) {
|
|
tileWeight = std::max(tileWeight, 0.20f);
|
|
}
|
|
result.tileWeights[idx] = tileWeight;
|
|
if (isEdgeTile) {
|
|
++edgeTiles;
|
|
if (metric.meanRobustness >= 0.70f && metric.weakFraction <= 0.22f) {
|
|
++edgeGoodTiles;
|
|
}
|
|
}
|
|
if (isTextTile) {
|
|
++textTiles;
|
|
if (metric.meanRobustness >= 0.74f && metric.weakFraction <= 0.16f) {
|
|
++textGoodTiles;
|
|
}
|
|
}
|
|
}
|
|
|
|
size_t totalTiles = tileMetrics.size();
|
|
if (totalTiles > 0) {
|
|
result.summary.edgeTileFraction = (float)edgeTiles / (float)totalTiles;
|
|
}
|
|
if (edgeTiles > 0) {
|
|
result.summary.edgeGoodFraction = (float)edgeGoodTiles / (float)edgeTiles;
|
|
}
|
|
if (textTiles > 0) {
|
|
result.summary.textGoodFraction = (float)textGoodTiles / (float)textTiles;
|
|
} else {
|
|
result.summary.textGoodFraction = result.summary.edgeGoodFraction;
|
|
}
|
|
|
|
vkUnmapMemory(device, robustMem);
|
|
return result;
|
|
}
|
|
|
|
inline LocalReliabilityMap
|
|
readLocalReliabilityMap(const std::vector<float> &robustness, uint32_t planeW,
|
|
uint32_t planeH, const GrayImage &referenceProxy) {
|
|
LocalReliabilityMap result;
|
|
if (planeW == 0 || planeH == 0 || referenceProxy.data.empty() ||
|
|
robustness.empty())
|
|
return result;
|
|
|
|
const uint32_t localTileSize = 16;
|
|
const uint32_t tilesX = (planeW + localTileSize - 1) / localTileSize;
|
|
const uint32_t tilesY = (planeH + localTileSize - 1) / localTileSize;
|
|
result.tilesX = tilesX;
|
|
result.tilesY = tilesY;
|
|
result.tileWeights.resize((size_t)tilesX * tilesY, 1.0f);
|
|
auto saturate = [](float v) { return std::clamp(v, 0.0f, 1.0f); };
|
|
|
|
struct TileMetric {
|
|
float detailScore = 0.0f;
|
|
float meanRobustness = 0.0f;
|
|
float weakFraction = 0.0f;
|
|
};
|
|
|
|
std::vector<TileMetric> tileMetrics;
|
|
tileMetrics.reserve((size_t)tilesX * tilesY);
|
|
std::vector<float> tileDetails;
|
|
std::vector<float> tileRobustnessMeans;
|
|
tileDetails.reserve((size_t)tilesX * tilesY);
|
|
tileRobustnessMeans.reserve((size_t)tilesX * tilesY);
|
|
|
|
for (uint32_t ty = 0; ty < tilesY; ++ty) {
|
|
for (uint32_t tx = 0; tx < tilesX; ++tx) {
|
|
uint32_t x0 = tx * localTileSize;
|
|
uint32_t y0 = ty * localTileSize;
|
|
uint32_t x1 = std::min(x0 + localTileSize, planeW);
|
|
uint32_t y1 = std::min(y0 + localTileSize, planeH);
|
|
|
|
double detailSum = 0.0;
|
|
double robustSum = 0.0;
|
|
size_t weakCount = 0;
|
|
size_t count = 0;
|
|
|
|
for (uint32_t y = y0; y < y1; ++y) {
|
|
for (uint32_t x = x0; x < x1; ++x) {
|
|
size_t idx = (size_t)y * planeW + x;
|
|
float center = (float)referenceProxy.data[idx];
|
|
float xp = (float)referenceProxy
|
|
.data[(size_t)y * planeW + std::min(x + 1, planeW - 1)];
|
|
float xm = (float)referenceProxy
|
|
.data[(size_t)y * planeW + (x > 0 ? x - 1 : 0)];
|
|
float yp = (float)referenceProxy.data[(size_t)std::min(y + 1, planeH - 1) *
|
|
planeW +
|
|
x];
|
|
float ym = (float)referenceProxy
|
|
.data[(size_t)(y > 0 ? y - 1 : 0) * planeW + x];
|
|
float gx = std::abs(xp - xm);
|
|
float gy = std::abs(yp - ym);
|
|
float lap = std::abs(4.0f * center - xp - xm - yp - ym);
|
|
detailSum += gx + gy + 0.5 * lap;
|
|
|
|
float r = robustness[idx];
|
|
robustSum += r;
|
|
if (r < 0.5f)
|
|
++weakCount;
|
|
++count;
|
|
}
|
|
}
|
|
|
|
TileMetric metric;
|
|
if (count > 0) {
|
|
metric.detailScore = (float)(detailSum / (double)count);
|
|
metric.meanRobustness = (float)(robustSum / (double)count);
|
|
metric.weakFraction = (float)weakCount / (float)count;
|
|
}
|
|
tileMetrics.push_back(metric);
|
|
tileDetails.push_back(metric.detailScore);
|
|
tileRobustnessMeans.push_back(metric.meanRobustness);
|
|
}
|
|
}
|
|
|
|
result.summary.tileRobustness =
|
|
computeScalarStats(tileRobustnessMeans.data(), tileRobustnessMeans.size());
|
|
ScalarStats detailStats =
|
|
computeScalarStats(tileDetails.data(), tileDetails.size());
|
|
float edgeThreshold = std::max(detailStats.p50, detailStats.p90 * 0.65f);
|
|
float textThreshold = std::max(detailStats.p90, detailStats.p99 * 0.75f);
|
|
|
|
size_t edgeTiles = 0;
|
|
size_t edgeGoodTiles = 0;
|
|
size_t textTiles = 0;
|
|
size_t textGoodTiles = 0;
|
|
for (size_t idx = 0; idx < tileMetrics.size(); ++idx) {
|
|
const TileMetric &metric = tileMetrics[idx];
|
|
bool isEdgeTile = metric.detailScore >= edgeThreshold;
|
|
bool isTextTile = metric.detailScore >= textThreshold;
|
|
float robustNorm = saturate((metric.meanRobustness - 0.58f) / 0.24f);
|
|
float weakPenalty =
|
|
saturate(1.0f - std::max(0.0f, metric.weakFraction - 0.10f) / 0.30f);
|
|
float detailBoost = isTextTile ? 1.0f : (isEdgeTile ? 0.70f : 0.35f);
|
|
float tileWeight = saturate((0.55f * robustNorm + 0.45f * weakPenalty) *
|
|
(0.55f + 0.45f * detailBoost));
|
|
if (isTextTile) {
|
|
tileWeight = std::max(tileWeight, 0.35f);
|
|
} else if (isEdgeTile) {
|
|
tileWeight = std::max(tileWeight, 0.20f);
|
|
}
|
|
result.tileWeights[idx] = tileWeight;
|
|
if (isEdgeTile) {
|
|
++edgeTiles;
|
|
if (metric.meanRobustness >= 0.70f && metric.weakFraction <= 0.22f) {
|
|
++edgeGoodTiles;
|
|
}
|
|
}
|
|
if (isTextTile) {
|
|
++textTiles;
|
|
if (metric.meanRobustness >= 0.74f && metric.weakFraction <= 0.16f) {
|
|
++textGoodTiles;
|
|
}
|
|
}
|
|
}
|
|
|
|
size_t totalTiles = tileMetrics.size();
|
|
if (totalTiles > 0) {
|
|
result.summary.edgeTileFraction = (float)edgeTiles / (float)totalTiles;
|
|
}
|
|
if (edgeTiles > 0) {
|
|
result.summary.edgeGoodFraction = (float)edgeGoodTiles / (float)edgeTiles;
|
|
}
|
|
if (textTiles > 0) {
|
|
result.summary.textGoodFraction = (float)textGoodTiles / (float)textTiles;
|
|
} else {
|
|
result.summary.textGoodFraction = result.summary.edgeGoodFraction;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
inline float clamp01(float v) { return std::clamp(v, 0.0f, 1.0f); }
|
|
|
|
inline float computeFusionFrameWeight(const TileAlignment &alignment,
|
|
float sharpnessRatio) {
|
|
if (alignment.offsets.empty() || alignment.errorMap.empty())
|
|
return 1.0f;
|
|
|
|
std::vector<float> magnitudes;
|
|
magnitudes.reserve(alignment.offsets.size());
|
|
for (const auto &p : alignment.offsets) {
|
|
magnitudes.push_back(std::sqrt(p.x * p.x + p.y * p.y));
|
|
}
|
|
|
|
ScalarStats magStats =
|
|
computeScalarStats(magnitudes.data(), magnitudes.size());
|
|
ScalarStats errStats = computeScalarStats(alignment.errorMap.data(),
|
|
alignment.errorMap.size());
|
|
|
|
float motionWeight = clamp01(1.0f - std::max(0.0f, magStats.p90 - 3.0f) / 3.0f);
|
|
float errorWeight = clamp01(1.0f - std::max(0.0f, errStats.p90 - 14.0f) / 14.0f);
|
|
float sharpnessWeight =
|
|
clamp01(0.35f + 0.65f * std::min(std::max(sharpnessRatio, 0.0f), 1.0f));
|
|
|
|
return motionWeight * errorWeight * sharpnessWeight;
|
|
}
|
|
|
|
struct CoarseReliabilitySummary {
|
|
float flatGoodFraction = 0.0f;
|
|
float flatAreaFraction = 0.0f;
|
|
float detailAreaFraction = 0.0f;
|
|
};
|
|
|
|
inline float smoothstepf(float edge0, float edge1, float x) {
|
|
float width = std::max(edge1 - edge0, 1e-6f);
|
|
float t = clamp01((x - edge0) / width);
|
|
return t * t * (3.0f - 2.0f * t);
|
|
}
|
|
|
|
inline float computeSemanticFusionWeight(float errP90Rms,
|
|
float sharpnessRatio,
|
|
float flatGoodFraction,
|
|
float flatAreaFraction,
|
|
float detailAreaFraction) {
|
|
const float errorWeight =
|
|
clamp01(1.0f - std::max(0.0f, errP90Rms - 2.5f) / 5.0f);
|
|
const float sharpnessGuard = clamp01(0.88f + 0.12f * sharpnessRatio);
|
|
const float flatCoverage = clamp01(0.75f + 0.25f * flatAreaFraction);
|
|
const float flatReliability = clamp01(0.44f + 0.55f * flatGoodFraction);
|
|
const float detailPenalty = clamp01(1.0f - 0.15f * detailAreaFraction);
|
|
const float baseWeight =
|
|
0.6f + 0.40f * errorWeight * flatCoverage * flatReliability *
|
|
detailPenalty;
|
|
return clamp01(baseWeight * sharpnessGuard);
|
|
}
|
|
|
|
inline CoarseReliabilitySummary
|
|
buildCoarseReliabilitySummary(const GrayImage &reference,
|
|
const TileAlignment &alignment) {
|
|
CoarseReliabilitySummary summary;
|
|
if (reference.data.empty() || alignment.errorMap.empty() ||
|
|
alignment.gridW <= 0 || alignment.gridH <= 0) {
|
|
return summary;
|
|
}
|
|
|
|
std::vector<float> detailScores;
|
|
detailScores.reserve((size_t)alignment.gridW * alignment.gridH);
|
|
uint32_t tileW =
|
|
((uint32_t)reference.width + (uint32_t)alignment.gridW - 1u) /
|
|
(uint32_t)alignment.gridW;
|
|
uint32_t tileH =
|
|
((uint32_t)reference.height + (uint32_t)alignment.gridH - 1u) /
|
|
(uint32_t)alignment.gridH;
|
|
|
|
for (int gy = 0; gy < alignment.gridH; ++gy) {
|
|
for (int gx = 0; gx < alignment.gridW; ++gx) {
|
|
uint32_t x0 = (uint32_t)gx * tileW;
|
|
uint32_t y0 = (uint32_t)gy * tileH;
|
|
uint32_t x1 =
|
|
std::min<uint32_t>(x0 + tileW, (uint32_t)reference.width);
|
|
uint32_t y1 =
|
|
std::min<uint32_t>(y0 + tileH, (uint32_t)reference.height);
|
|
double detailSum = 0.0;
|
|
size_t count = 0;
|
|
for (uint32_t y = y0; y < y1; ++y) {
|
|
for (uint32_t x = x0; x < x1; ++x) {
|
|
size_t idx = (size_t)y * (size_t)reference.width + x;
|
|
float center = (float)reference.data[idx];
|
|
float xp = (float)reference.data[(size_t)y * (size_t)reference.width +
|
|
std::min<uint32_t>(
|
|
x + 1, (uint32_t)reference.width - 1)];
|
|
float xm = (float)reference.data[(size_t)y * (size_t)reference.width +
|
|
(x > 0 ? x - 1 : 0)];
|
|
float yp =
|
|
(float)reference.data[(size_t)std::min<uint32_t>(
|
|
y + 1, (uint32_t)reference.height - 1) *
|
|
(size_t)reference.width +
|
|
x];
|
|
float ym = (float)reference.data[(size_t)(y > 0 ? y - 1 : 0) *
|
|
(size_t)reference.width +
|
|
x];
|
|
detailSum += std::abs(xp - xm) + std::abs(yp - ym) +
|
|
0.5f * std::abs(4.0f * center - xp - xm - yp - ym);
|
|
++count;
|
|
}
|
|
}
|
|
detailScores.push_back((count > 0) ? (float)(detailSum / (double)count)
|
|
: 0.0f);
|
|
}
|
|
}
|
|
|
|
ScalarStats detailStats =
|
|
computeScalarStats(detailScores.data(), detailScores.size());
|
|
float flatThreshold = detailStats.minValue +
|
|
(detailStats.p50 - detailStats.minValue) * 0.70f;
|
|
float detailThreshold = std::max(
|
|
detailStats.p50, detailStats.minValue +
|
|
(detailStats.p90 - detailStats.minValue) * 0.55f);
|
|
|
|
size_t flatCount = 0;
|
|
size_t flatGood = 0;
|
|
size_t detailCount = 0;
|
|
size_t tileCount =
|
|
std::min(detailScores.size(), alignment.errorMap.size());
|
|
for (size_t idx = 0; idx < tileCount; ++idx) {
|
|
float errRms = std::sqrt(std::max(alignment.errorMap[idx], 0.0f));
|
|
float detailPenalty =
|
|
smoothstepf(flatThreshold, detailThreshold, detailScores[idx]);
|
|
if (detailPenalty > 0.60f) {
|
|
++detailCount;
|
|
} else {
|
|
++flatCount;
|
|
if (errRms < 4.0f)
|
|
++flatGood;
|
|
}
|
|
}
|
|
|
|
float tileCountF = static_cast<float>(tileCount);
|
|
summary.flatGoodFraction =
|
|
(flatCount > 0) ? (float)flatGood / (float)flatCount : 0.0f;
|
|
summary.flatAreaFraction =
|
|
(tileCountF > 0.0f) ? (float)flatCount / tileCountF : 0.0f;
|
|
summary.detailAreaFraction =
|
|
(tileCountF > 0.0f) ? (float)detailCount / tileCountF : 0.0f;
|
|
return summary;
|
|
}
|
|
|
|
inline LocalReliabilityMap
|
|
buildCoarseLocalReliabilityMap(const GrayImage &reference,
|
|
const TileAlignment &alignment,
|
|
uint32_t dstTilesX,
|
|
uint32_t dstTilesY) {
|
|
LocalReliabilityMap result;
|
|
if (reference.data.empty() || alignment.errorMap.empty() ||
|
|
alignment.gridW <= 0 || alignment.gridH <= 0 || dstTilesX == 0 ||
|
|
dstTilesY == 0) {
|
|
return result;
|
|
}
|
|
|
|
result.tilesX = dstTilesX;
|
|
result.tilesY = dstTilesY;
|
|
result.tileWeights.resize((size_t)dstTilesX * dstTilesY, 1.0f);
|
|
|
|
std::vector<float> detailScores;
|
|
detailScores.reserve(result.tileWeights.size());
|
|
uint32_t tileW = ((uint32_t)reference.width + dstTilesX - 1u) / dstTilesX;
|
|
uint32_t tileH = ((uint32_t)reference.height + dstTilesY - 1u) / dstTilesY;
|
|
|
|
for (uint32_t gy = 0; gy < dstTilesY; ++gy) {
|
|
for (uint32_t gx = 0; gx < dstTilesX; ++gx) {
|
|
uint32_t x0 = gx * tileW;
|
|
uint32_t y0 = gy * tileH;
|
|
uint32_t x1 = std::min<uint32_t>(x0 + tileW, (uint32_t)reference.width);
|
|
uint32_t y1 =
|
|
std::min<uint32_t>(y0 + tileH, (uint32_t)reference.height);
|
|
double detailSum = 0.0;
|
|
size_t count = 0;
|
|
for (uint32_t y = y0; y < y1; ++y) {
|
|
for (uint32_t x = x0; x < x1; ++x) {
|
|
size_t idx = (size_t)y * (size_t)reference.width + x;
|
|
float center = (float)reference.data[idx];
|
|
float xp = (float)reference.data[(size_t)y * (size_t)reference.width +
|
|
std::min<uint32_t>(
|
|
x + 1, (uint32_t)reference.width - 1)];
|
|
float xm = (float)reference.data[(size_t)y * (size_t)reference.width +
|
|
(x > 0 ? x - 1 : 0)];
|
|
float yp =
|
|
(float)reference.data[(size_t)std::min<uint32_t>(
|
|
y + 1, (uint32_t)reference.height - 1) *
|
|
(size_t)reference.width +
|
|
x];
|
|
float ym = (float)reference.data[(size_t)(y > 0 ? y - 1 : 0) *
|
|
(size_t)reference.width +
|
|
x];
|
|
detailSum += std::abs(xp - xm) + std::abs(yp - ym) +
|
|
0.5f * std::abs(4.0f * center - xp - xm - yp - ym);
|
|
++count;
|
|
}
|
|
}
|
|
detailScores.push_back((count > 0) ? (float)(detailSum / (double)count)
|
|
: 0.0f);
|
|
}
|
|
}
|
|
|
|
ScalarStats detailStats =
|
|
computeScalarStats(detailScores.data(), detailScores.size());
|
|
float flatThreshold = detailStats.minValue +
|
|
(detailStats.p50 - detailStats.minValue) * 0.70f;
|
|
float detailThreshold = std::max(
|
|
detailStats.p50, detailStats.minValue +
|
|
(detailStats.p90 - detailStats.minValue) * 0.55f);
|
|
|
|
size_t flatCount = 0;
|
|
size_t flatGood = 0;
|
|
size_t detailCount = 0;
|
|
size_t tileCount = detailScores.size();
|
|
for (uint32_t gy = 0; gy < dstTilesY; ++gy) {
|
|
for (uint32_t gx = 0; gx < dstTilesX; ++gx) {
|
|
size_t idx = (size_t)gy * dstTilesX + gx;
|
|
uint32_t srcGX = std::min<uint32_t>(
|
|
(uint32_t)((uint64_t)gx * alignment.gridW / dstTilesX),
|
|
(uint32_t)alignment.gridW - 1u);
|
|
uint32_t srcGY = std::min<uint32_t>(
|
|
(uint32_t)((uint64_t)gy * alignment.gridH / dstTilesY),
|
|
(uint32_t)alignment.gridH - 1u);
|
|
size_t srcIdx = (size_t)srcGY * (size_t)alignment.gridW + srcGX;
|
|
float errRms = std::sqrt(std::max(alignment.errorMap[srcIdx], 0.0f));
|
|
float detailPenalty =
|
|
smoothstepf(flatThreshold, detailThreshold, detailScores[idx]);
|
|
float tileWeight =
|
|
clamp01(1.0f - std::max(0.0f, errRms - 2.0f) / 4.5f);
|
|
|
|
if (detailPenalty > 0.60f) {
|
|
++detailCount;
|
|
} else {
|
|
++flatCount;
|
|
if (errRms < 4.0f)
|
|
++flatGood;
|
|
}
|
|
|
|
if (errRms > 5.5f)
|
|
tileWeight *= 0.60f;
|
|
if (detailPenalty > 0.78f && errRms > 7.5f)
|
|
tileWeight = 0.0f;
|
|
|
|
result.tileWeights[idx] = clamp01(tileWeight);
|
|
}
|
|
}
|
|
|
|
float tileCountF = static_cast<float>(tileCount);
|
|
result.summary.edgeGoodFraction =
|
|
(flatCount > 0) ? (float)flatGood / (float)flatCount : 0.0f;
|
|
result.summary.edgeTileFraction =
|
|
(tileCountF > 0.0f) ? (float)flatCount / tileCountF : 0.0f;
|
|
result.summary.textGoodFraction =
|
|
(tileCountF > 0.0f) ? 1.0f - ((float)detailCount / tileCountF) : 0.0f;
|
|
return result;
|
|
}
|
|
|
|
inline float computeYuvStyleCoarseFrameWeight(
|
|
const TileAlignment &alignment, float sharpnessRatio,
|
|
const CoarseReliabilitySummary &local) {
|
|
if (alignment.errorMap.empty())
|
|
return clamp01(0.75f + 0.25f * sharpnessRatio);
|
|
|
|
ScalarStats errStats = computeScalarStats(alignment.errorMap.data(),
|
|
alignment.errorMap.size());
|
|
const float errP90Rms = std::sqrt(std::max(errStats.p90, 0.0f));
|
|
|
|
// 1. Error Likelihood: More generous exponential decay.
|
|
float errorWeight = std::exp(-0.12f * std::max(0.0f, errP90Rms - 2.0f));
|
|
|
|
// 2. Consistency: Benefit from "flat and good" but don't penalize details too much.
|
|
float consistencyWeight = 0.7f + 0.3f * local.flatGoodFraction;
|
|
|
|
// 3. Sharpness: Preference for sharper frames.
|
|
float sharpnessWeight = 0.5f + 0.5f * clamp01(sharpnessRatio);
|
|
|
|
return clamp01(errorWeight * consistencyWeight * sharpnessWeight);
|
|
}
|
|
|
|
inline float computeRobustnessFrameWeight(const FlowSummary &flow,
|
|
const RobustnessSummary &robustness,
|
|
const LocalReliabilitySummary &local) {
|
|
// "Local-First" Weighting: Frame weight should be high if global alignment is okay.
|
|
// The per-pixel robustness map handles moving objects.
|
|
|
|
// 1. Alignment Likelihood (Global):
|
|
// Use mean robustness as a proxy for registration success.
|
|
float alignmentScore = robustness.values.meanValue;
|
|
|
|
// 2. High confidence floor:
|
|
// If the frame is registered, don't attenuate the static regions.
|
|
float baseWeight = 0.85f + 0.15f * clamp01((alignmentScore - 0.5f) / 0.4f);
|
|
|
|
// 3. Flow Consistency: Still penalize if tracking is erratic.
|
|
float flowConfidence = std::exp(-0.01f * std::max(0.0f, flow.magnitude.p90 - 20.0f));
|
|
|
|
float weight = baseWeight * flowConfidence;
|
|
|
|
// High floor to ensure static areas in aligned frames always fuse.
|
|
return std::max(clamp01(weight), 0.70f);
|
|
}
|
|
|
|
inline GreenPhaseSummary readGreenPhaseSummary(VkDevice device,
|
|
VkDeviceMemory phase0Mem,
|
|
VkDeviceMemory phase1Mem,
|
|
uint32_t stride,
|
|
uint32_t tileW,
|
|
uint32_t tileH) {
|
|
GreenPhaseSummary summary;
|
|
if (tileW == 0 || tileH == 0)
|
|
return summary;
|
|
|
|
void *phase0Ptr = nullptr;
|
|
void *phase1Ptr = nullptr;
|
|
if (vkMapMemory(device, phase0Mem, 0, VK_WHOLE_SIZE, 0, &phase0Ptr) !=
|
|
VK_SUCCESS ||
|
|
vkMapMemory(device, phase1Mem, 0, VK_WHOLE_SIZE, 0, &phase1Ptr) !=
|
|
VK_SUCCESS ||
|
|
phase0Ptr == nullptr || phase1Ptr == nullptr) {
|
|
if (phase0Ptr)
|
|
vkUnmapMemory(device, phase0Mem);
|
|
if (phase1Ptr)
|
|
vkUnmapMemory(device, phase1Mem);
|
|
return summary;
|
|
}
|
|
|
|
const uint32_t *phase0 = static_cast<const uint32_t *>(phase0Ptr);
|
|
const uint32_t *phase1 = static_cast<const uint32_t *>(phase1Ptr);
|
|
std::vector<float> mean0;
|
|
std::vector<float> mean1;
|
|
mean0.reserve((size_t)tileW * tileH);
|
|
mean1.reserve((size_t)tileW * tileH);
|
|
size_t dominant0 = 0;
|
|
size_t dominant1 = 0;
|
|
size_t conflict = 0;
|
|
size_t active = 0;
|
|
|
|
for (uint32_t y = 0; y < tileH; ++y) {
|
|
for (uint32_t x = 0; x < tileW; ++x) {
|
|
size_t idx = ((size_t)y * stride + x) * 2;
|
|
float sum0 = (float)phase0[idx];
|
|
float weight0 = (float)phase0[idx + 1];
|
|
float sum1 = (float)phase1[idx];
|
|
float weight1 = (float)phase1[idx + 1];
|
|
float m0 = (weight0 > 0.0f) ? (sum0 / weight0 / 65535.0f) : 0.0f;
|
|
float m1 = (weight1 > 0.0f) ? (sum1 / weight1 / 65535.0f) : 0.0f;
|
|
mean0.push_back(m0);
|
|
mean1.push_back(m1);
|
|
if (weight0 > 0.0f || weight1 > 0.0f) {
|
|
++active;
|
|
if (weight0 > weight1 * 1.25f)
|
|
++dominant0;
|
|
else if (weight1 > weight0 * 1.25f)
|
|
++dominant1;
|
|
if (weight0 > 0.0f && weight1 > 0.0f && std::abs(m0 - m1) > 0.03f)
|
|
++conflict;
|
|
}
|
|
}
|
|
}
|
|
|
|
summary.phase0Mean = computeScalarStats(mean0.data(), mean0.size());
|
|
summary.phase1Mean = computeScalarStats(mean1.data(), mean1.size());
|
|
if (active > 0) {
|
|
summary.phase0DominantFraction = (float)dominant0 / (float)active;
|
|
summary.phase1DominantFraction = (float)dominant1 / (float)active;
|
|
summary.conflictFraction = (float)conflict / (float)active;
|
|
}
|
|
|
|
vkUnmapMemory(device, phase0Mem);
|
|
vkUnmapMemory(device, phase1Mem);
|
|
return summary;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
GrayImage VulkanRawStacker::buildAlignmentProxy(const FrameData &frame) const {
|
|
GrayImage proxy;
|
|
proxy.width = (int)(width / 2);
|
|
proxy.height = (int)(height / 2);
|
|
proxy.data.resize((size_t)proxy.width * proxy.height);
|
|
|
|
float blackAvg = 0.25f * (mBlackLevel[0] + mBlackLevel[1] + mBlackLevel[2] +
|
|
mBlackLevel[3]);
|
|
float invRange = 255.0f / std::max(1.0f, mWhiteLevel - blackAvg);
|
|
std::vector<float> linearProxy(proxy.data.size(), 0.0f);
|
|
|
|
for (int py = 0; py < proxy.height; ++py) {
|
|
for (int px = 0; px < proxy.width; ++px) {
|
|
float green =
|
|
fetchGreenProxySample(frame.rawData, width, height, frame.cfaPattern,
|
|
px, py);
|
|
float normalized = std::max(0.0f, green - blackAvg) * invRange;
|
|
linearProxy[(size_t)py * proxy.width + px] = normalized;
|
|
}
|
|
}
|
|
|
|
// Mild local-contrast enhancement keeps fine text edges visible for
|
|
// alignment, without changing the external output pipeline.
|
|
for (int py = 0; py < proxy.height; ++py) {
|
|
for (int px = 0; px < proxy.width; ++px) {
|
|
float mean = 0.0f;
|
|
int count = 0;
|
|
for (int dy = -1; dy <= 1; ++dy) {
|
|
for (int dx = -1; dx <= 1; ++dx) {
|
|
int nx = std::max(0, std::min(px + dx, proxy.width - 1));
|
|
int ny = std::max(0, std::min(py + dy, proxy.height - 1));
|
|
mean += linearProxy[(size_t)ny * proxy.width + nx];
|
|
++count;
|
|
}
|
|
}
|
|
mean /= (float)count;
|
|
float center = linearProxy[(size_t)py * proxy.width + px];
|
|
float boosted = center + 0.35f * (center - mean);
|
|
proxy.data[(size_t)py * proxy.width + px] =
|
|
(uint8_t)std::clamp(boosted, 0.0f, 255.0f);
|
|
}
|
|
}
|
|
|
|
return proxy;
|
|
}
|
|
|
|
float VulkanRawStacker::calculateFrameScore(const FrameData &frame) const {
|
|
GrayImage proxy = buildAlignmentProxy(frame);
|
|
double score = 0.0;
|
|
for (int y = 2; y < proxy.height - 2; y += 2) {
|
|
for (int x = 2; x < proxy.width - 2; x += 2) {
|
|
int idx = y * proxy.width + x;
|
|
float c = (float)proxy.data[idx];
|
|
float gx = std::abs(c - (float)proxy.data[idx + 1]);
|
|
float gy = std::abs(c - (float)proxy.data[idx + proxy.width]);
|
|
float lap = std::abs(4.0f * c - (float)proxy.data[idx - 1] -
|
|
(float)proxy.data[idx + 1] -
|
|
(float)proxy.data[idx - proxy.width] -
|
|
(float)proxy.data[idx + proxy.width]);
|
|
score += gx + gy + 0.5 * lap;
|
|
}
|
|
}
|
|
return (float)score;
|
|
}
|
|
|
|
void VulkanRawStacker::selectReferenceFrame() {
|
|
for (auto &frame : pendingFrames) {
|
|
if (frame.score == 0.0f) {
|
|
frame.score = calculateFrameScore(frame);
|
|
}
|
|
}
|
|
|
|
std::sort(
|
|
pendingFrames.begin(), pendingFrames.end(),
|
|
[](const FrameData &a, const FrameData &b) { return a.score > b.score; });
|
|
|
|
if (!pendingFrames.empty()) {
|
|
mCfaPattern = pendingFrames.front().cfaPattern;
|
|
}
|
|
}
|
|
|
|
bool VulkanRawStacker::processStack(uint16_t *outBuffer, size_t bufferSize) {
|
|
if (pendingFrames.empty())
|
|
return false;
|
|
|
|
RawSuperResPerfStats perf;
|
|
perf.totalFrames = pendingFrames.size();
|
|
const auto processStackStart = PerfClock::now();
|
|
|
|
// 1. Calculate scores and sort (Highest score first = Reference)
|
|
TIME_START(scoreCalculation);
|
|
selectReferenceFrame();
|
|
TIME_END(scoreCalculation);
|
|
perf.scoreCalculationMs = elapsed_scoreCalculation;
|
|
|
|
VulkanManager &vm = VulkanManager::getInstance();
|
|
VkDevice device = vm.getDevice();
|
|
|
|
if (structureTensorPipeline == VK_NULL_HANDLE) {
|
|
createPipelines();
|
|
}
|
|
|
|
float scale = mEnableSuperRes ? mSuperResScale : 1.0f;
|
|
uint32_t outputW = (uint32_t)std::lround(width * scale);
|
|
uint32_t outputH = (uint32_t)std::lround(height * scale);
|
|
uint32_t inputW = width / 2;
|
|
uint32_t inputH = height / 2;
|
|
uint32_t localTilesX = (inputW + 15) / 16;
|
|
uint32_t localTilesY = (inputH + 15) / 16;
|
|
std::vector<float> defaultLocalTileMask((size_t)localTilesX * localTilesY,
|
|
1.0f);
|
|
|
|
uint32_t tileW = (outputW + numTilesX - 1) / numTilesX;
|
|
uint32_t tileH = (outputH + numTilesY - 1) / numTilesY;
|
|
|
|
TIME_START(descriptorSetup);
|
|
|
|
// Global Sampler
|
|
VkSampler sampler;
|
|
VkSamplerCreateInfo samplerInfo{VK_STRUCTURE_TYPE_SAMPLER_CREATE_INFO};
|
|
samplerInfo.magFilter = VK_FILTER_NEAREST;
|
|
samplerInfo.minFilter = VK_FILTER_NEAREST;
|
|
samplerInfo.addressModeU = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
|
|
samplerInfo.addressModeV = VK_SAMPLER_ADDRESS_MODE_CLAMP_TO_EDGE;
|
|
samplerInfo.mipmapMode = VK_SAMPLER_MIPMAP_MODE_NEAREST;
|
|
VK_CHECK(vkCreateSampler(device, &samplerInfo, nullptr, &sampler));
|
|
|
|
// Allocate Descriptor Sets
|
|
VkDescriptorSetAllocateInfo allocInfo{
|
|
VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO};
|
|
allocInfo.descriptorPool = descriptorPool;
|
|
allocInfo.descriptorSetCount = 1;
|
|
|
|
allocInfo.pSetLayouts = &structureTensorSetLayout;
|
|
VK_CHECK(vkAllocateDescriptorSets(device, &allocInfo, &structureTensorSet));
|
|
|
|
allocInfo.pSetLayouts = &alignLkSetLayout;
|
|
VK_CHECK(vkAllocateDescriptorSets(device, &allocInfo, &alignLkSets[0]));
|
|
VkDescriptorSet alignSet = alignLkSets[0];
|
|
|
|
allocInfo.pSetLayouts = &robustnessSetLayout;
|
|
VK_CHECK(vkAllocateDescriptorSets(device, &allocInfo, &robustnessSets[0]));
|
|
VkDescriptorSet robustSet = robustnessSets[0];
|
|
|
|
allocInfo.pSetLayouts = &greenGatherSetLayout;
|
|
VK_CHECK(vkAllocateDescriptorSets(device, &allocInfo, &greenGatherSet));
|
|
|
|
allocInfo.pSetLayouts = &colorGatherSetLayout;
|
|
VK_CHECK(vkAllocateDescriptorSets(device, &allocInfo, &colorGatherSets[0]));
|
|
|
|
allocInfo.pSetLayouts = &referenceNormalizeSetLayout;
|
|
VK_CHECK(vkAllocateDescriptorSets(device, &allocInfo, &referenceNormalizeSet));
|
|
|
|
allocInfo.pSetLayouts = &referencePriorSetLayout;
|
|
VK_CHECK(vkAllocateDescriptorSets(device, &allocInfo, &referencePriorSet));
|
|
|
|
allocInfo.pSetLayouts = &greenNormalizeSetLayout;
|
|
VK_CHECK(vkAllocateDescriptorSets(device, &allocInfo, &greenNormalizeSet));
|
|
|
|
allocInfo.pSetLayouts = &colorNormalizeSetLayout;
|
|
for (int c = 0; c < 2; ++c) {
|
|
VK_CHECK(
|
|
vkAllocateDescriptorSets(device, &allocInfo, &colorNormalizeSets[c]));
|
|
}
|
|
|
|
// Pre-update constant bindings for other pipelines
|
|
updateBufferDescriptorSet(device, structureTensorSet, kernelBuffer,
|
|
VK_WHOLE_SIZE, 1);
|
|
updateBufferDescriptorSet(device, alignSet, alignmentBuffer, VK_WHOLE_SIZE,
|
|
2);
|
|
updateBufferDescriptorSet(device, alignSet, robustnessBuffer, VK_WHOLE_SIZE,
|
|
3);
|
|
updateBufferDescriptorSet(device, robustSet, alignmentBuffer, VK_WHOLE_SIZE,
|
|
2);
|
|
updateBufferDescriptorSet(device, robustSet, robustnessBuffer, VK_WHOLE_SIZE,
|
|
3);
|
|
updateBufferDescriptorSet(device, robustSet, flowVarianceBuffer,
|
|
VK_WHOLE_SIZE, 4);
|
|
updateBufferDescriptorSet(device, greenGatherSet, alignmentBuffer,
|
|
VK_WHOLE_SIZE, 1);
|
|
updateBufferDescriptorSet(device, greenGatherSet, kernelBuffer,
|
|
VK_WHOLE_SIZE, 2);
|
|
updateBufferDescriptorSet(device, greenGatherSet, robustnessBuffer,
|
|
VK_WHOLE_SIZE, 3);
|
|
updateBufferDescriptorSet(device, greenGatherSet, flowVarianceBuffer,
|
|
VK_WHOLE_SIZE, 4);
|
|
updateBufferDescriptorSet(device, greenGatherSet, greenPhaseAccumBuffers[0],
|
|
VK_WHOLE_SIZE, 5);
|
|
updateBufferDescriptorSet(device, greenGatherSet, greenPhaseAccumBuffers[1],
|
|
VK_WHOLE_SIZE, 6);
|
|
updateImageDescriptorSet(device, greenGatherSet, lscView, lscSampler, 7);
|
|
updateBufferDescriptorSet(device, greenGatherSet, localTileMaskBuffer,
|
|
VK_WHOLE_SIZE, 8);
|
|
updateBufferDescriptorSet(device, colorGatherSets[0], alignmentBuffer,
|
|
VK_WHOLE_SIZE, 1);
|
|
updateBufferDescriptorSet(device, colorGatherSets[0], kernelBuffer,
|
|
VK_WHOLE_SIZE, 2);
|
|
updateBufferDescriptorSet(device, colorGatherSets[0], robustnessBuffer,
|
|
VK_WHOLE_SIZE, 3);
|
|
updateBufferDescriptorSet(device, colorGatherSets[0],
|
|
rbGatherAccumBuffers[0], VK_WHOLE_SIZE, 4);
|
|
updateBufferDescriptorSet(device, colorGatherSets[0],
|
|
rbGatherAccumBuffers[1], VK_WHOLE_SIZE, 5);
|
|
updateBufferDescriptorSet(device, colorGatherSets[0], flowVarianceBuffer,
|
|
VK_WHOLE_SIZE, 6);
|
|
updateImageDescriptorSet(device, colorGatherSets[0], lscView, lscSampler,
|
|
7);
|
|
updateBufferDescriptorSet(device, colorGatherSets[0], localTileMaskBuffer,
|
|
VK_WHOLE_SIZE, 8);
|
|
updateBufferDescriptorSet(device, referenceNormalizeSet,
|
|
normalizedReferenceBuffer, VK_WHOLE_SIZE, 1);
|
|
updateImageDescriptorSet(device, referenceNormalizeSet, lscView, lscSampler,
|
|
2);
|
|
updateBufferDescriptorSet(device, referencePriorSet,
|
|
normalizedReferenceBuffer, VK_WHOLE_SIZE, 0);
|
|
updateBufferDescriptorSet(device, referencePriorSet, priorBayerBuffer,
|
|
VK_WHOLE_SIZE, 1);
|
|
updateBufferDescriptorSet(device, referencePriorSet, priorWeightBuffer,
|
|
VK_WHOLE_SIZE, 2);
|
|
TIME_END(descriptorSetup);
|
|
perf.descriptorSetupMs = elapsed_descriptorSetup;
|
|
|
|
// =====================================================================
|
|
// Phase 1: Upload all frames to GPU and build proxies/pyramids
|
|
// =====================================================================
|
|
TIME_START(phase1_UploadAndPyramid);
|
|
struct UploadedFrame {
|
|
VkImage image = VK_NULL_HANDLE;
|
|
VkImageView view = VK_NULL_HANDLE;
|
|
VkDeviceMemory mem = VK_NULL_HANDLE;
|
|
GrayImage proxy;
|
|
std::vector<GrayImage> pyramid;
|
|
};
|
|
std::vector<UploadedFrame> uploadedFrames(pendingFrames.size());
|
|
|
|
for (size_t i = 0; i < pendingFrames.size(); ++i) {
|
|
const auto &frame = pendingFrames[i];
|
|
|
|
// Build grayscale proxy
|
|
{
|
|
const auto proxyBuildStart = PerfClock::now();
|
|
uploadedFrames[i].proxy = buildAlignmentProxy(frame);
|
|
perf.proxyBuildMs += elapsedMillis(proxyBuildStart);
|
|
}
|
|
|
|
// Build pyramid
|
|
{
|
|
const auto pyramidBuildStart = PerfClock::now();
|
|
uploadedFrames[i].pyramid = buildPyramid(
|
|
uploadedFrames[i].proxy.data.data(), uploadedFrames[i].proxy.width,
|
|
uploadedFrames[i].proxy.height, 4);
|
|
perf.pyramidBuildMs += elapsedMillis(pyramidBuildStart);
|
|
}
|
|
|
|
if (i == 0) {
|
|
referencePyramid = uploadedFrames[i].pyramid;
|
|
}
|
|
|
|
// Upload frame to GPU
|
|
{
|
|
const auto gpuUploadStart = PerfClock::now();
|
|
VkImageCreateInfo imgInfo{VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO};
|
|
imgInfo.imageType = VK_IMAGE_TYPE_2D;
|
|
imgInfo.format = VK_FORMAT_R16_UNORM;
|
|
imgInfo.extent = {width, height, 1};
|
|
imgInfo.mipLevels = 1;
|
|
imgInfo.arrayLayers = 1;
|
|
imgInfo.samples = VK_SAMPLE_COUNT_1_BIT;
|
|
imgInfo.usage =
|
|
VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT;
|
|
vkCreateImage(device, &imgInfo, nullptr, &uploadedFrames[i].image);
|
|
VkMemoryRequirements reqs;
|
|
vkGetImageMemoryRequirements(device, uploadedFrames[i].image, &reqs);
|
|
VkMemoryAllocateInfo alloc{VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO};
|
|
alloc.allocationSize = reqs.size;
|
|
alloc.memoryTypeIndex = vm.findMemoryType(
|
|
reqs.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT);
|
|
vkAllocateMemory(device, &alloc, nullptr, &uploadedFrames[i].mem);
|
|
vkBindImageMemory(device, uploadedFrames[i].image, uploadedFrames[i].mem,
|
|
0);
|
|
|
|
VkImageViewCreateInfo viewInfo{VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO};
|
|
viewInfo.image = uploadedFrames[i].image;
|
|
viewInfo.viewType = VK_IMAGE_VIEW_TYPE_2D;
|
|
viewInfo.format = VK_FORMAT_R16_UNORM;
|
|
viewInfo.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1};
|
|
vkCreateImageView(device, &viewInfo, nullptr, &uploadedFrames[i].view);
|
|
|
|
VkDeviceSize size = width * height * 2;
|
|
void *map;
|
|
vkMapMemory(device, fusedBayerMemory, 0, size, 0, &map);
|
|
memcpy(map, frame.rawData.data(), size);
|
|
vkUnmapMemory(device, fusedBayerMemory);
|
|
|
|
VkCommandBuffer cb = vm.beginSingleTimeCommands();
|
|
VkImageMemoryBarrier barrier{VK_STRUCTURE_TYPE_IMAGE_MEMORY_BARRIER};
|
|
barrier.oldLayout = VK_IMAGE_LAYOUT_UNDEFINED;
|
|
barrier.newLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
|
|
barrier.image = uploadedFrames[i].image;
|
|
barrier.subresourceRange = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 1, 0, 1};
|
|
barrier.dstAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
|
|
vkCmdPipelineBarrier(cb, VK_PIPELINE_STAGE_TOP_OF_PIPE_BIT,
|
|
VK_PIPELINE_STAGE_TRANSFER_BIT, 0, 0, nullptr, 0,
|
|
nullptr, 1, &barrier);
|
|
|
|
VkBufferImageCopy copyReg{};
|
|
copyReg.imageExtent = {width, height, 1};
|
|
copyReg.imageSubresource = {VK_IMAGE_ASPECT_COLOR_BIT, 0, 0, 1};
|
|
vkCmdCopyBufferToImage(cb, fusedBayerBuffer, uploadedFrames[i].image,
|
|
VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL, 1, ©Reg);
|
|
|
|
barrier.oldLayout = VK_IMAGE_LAYOUT_TRANSFER_DST_OPTIMAL;
|
|
barrier.newLayout = VK_IMAGE_LAYOUT_SHADER_READ_ONLY_OPTIMAL;
|
|
barrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
|
|
barrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
|
|
vkCmdPipelineBarrier(cb, VK_PIPELINE_STAGE_TRANSFER_BIT,
|
|
VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0, 0, nullptr,
|
|
0, nullptr, 1, &barrier);
|
|
|
|
vm.endSingleTimeCommands(cb);
|
|
perf.gpuUploadMs += elapsedMillis(gpuUploadStart);
|
|
}
|
|
}
|
|
TIME_END(phase1_UploadAndPyramid);
|
|
perf.phase1UploadAndPyramidMs = elapsed_phase1_UploadAndPyramid;
|
|
|
|
// =====================================================================
|
|
// 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);
|
|
VkCommandBuffer cb = vm.beginSingleTimeCommands();
|
|
|
|
updateImageDescriptorSet(device, referenceNormalizeSet, uploadedFrames[0].view,
|
|
sampler, 0);
|
|
vkCmdBindPipeline(cb, VK_PIPELINE_BIND_POINT_COMPUTE,
|
|
referenceNormalizePipeline);
|
|
vkCmdBindDescriptorSets(cb, VK_PIPELINE_BIND_POINT_COMPUTE,
|
|
referenceNormalizePipelineLayout, 0, 1,
|
|
&referenceNormalizeSet, 0, nullptr);
|
|
|
|
PushConstants normalizePc{};
|
|
normalizePc.width = outputW;
|
|
normalizePc.height = outputH;
|
|
normalizePc.planeWidth = outputW;
|
|
normalizePc.planeHeight = outputH;
|
|
normalizePc.sensorWidth = width;
|
|
normalizePc.sensorHeight = height;
|
|
normalizePc.scale = scale;
|
|
normalizePc.cfaPattern = (uint32_t)mCfaPattern;
|
|
normalizePc.whiteLevel = mWhiteLevel;
|
|
memcpy(normalizePc.blackLevel, mBlackLevel, 4 * sizeof(float));
|
|
memcpy(normalizePc.wbGains, mWbGains, 4 * sizeof(float));
|
|
vkCmdPushConstants(cb, referenceNormalizePipelineLayout,
|
|
VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof(normalizePc),
|
|
&normalizePc);
|
|
vkCmdDispatch(cb, (width + 15) / 16, (height + 15) / 16, 1);
|
|
|
|
VkBufferMemoryBarrier normalizedReferenceBarrier{
|
|
VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER};
|
|
normalizedReferenceBarrier.srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT;
|
|
normalizedReferenceBarrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
|
|
normalizedReferenceBarrier.buffer = normalizedReferenceBuffer;
|
|
normalizedReferenceBarrier.size = VK_WHOLE_SIZE;
|
|
vkCmdPipelineBarrier(cb, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
|
|
VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0, 0, nullptr,
|
|
1, &normalizedReferenceBarrier, 0, nullptr);
|
|
|
|
vm.endSingleTimeCommands(cb);
|
|
TIME_END(phase1b_ReferencePrior);
|
|
perf.referencePriorMs = elapsed_phase1b_ReferencePrior;
|
|
}
|
|
|
|
// Phase 2: Structure tensor pass on reference frame (global, once)
|
|
// =====================================================================
|
|
TIME_START(phase2_StructureTensor);
|
|
{
|
|
VkCommandBuffer cb = vm.beginSingleTimeCommands();
|
|
|
|
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[0].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.bufferStride = tileW + 16;
|
|
pc.noiseAlpha = mNoiseModel[0] / 65535.0f;
|
|
pc.noiseBeta = mNoiseModel[1] / (65535.0f * 65535.0f);
|
|
pc.baseNoise = pc.noiseBeta;
|
|
pc.tileX = 0;
|
|
pc.tileY = 0;
|
|
pc.tileW = inputW;
|
|
pc.tileH = inputH;
|
|
|
|
vkCmdBindPipeline(cb, VK_PIPELINE_BIND_POINT_COMPUTE,
|
|
structureTensorPipeline);
|
|
updateImageDescriptorSet(device, structureTensorSet, uploadedFrames[0].view,
|
|
sampler, 0);
|
|
vkCmdBindDescriptorSets(cb, VK_PIPELINE_BIND_POINT_COMPUTE,
|
|
structureTensorPipelineLayout, 0, 1,
|
|
&structureTensorSet, 0, nullptr);
|
|
vkCmdPushConstants(cb, structureTensorPipelineLayout,
|
|
VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof(pc), &pc);
|
|
vkCmdDispatch(cb, (inputW + 15) / 16, (inputH + 15) / 16, 1);
|
|
|
|
VkBufferMemoryBarrier bMem{VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER};
|
|
bMem.srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT;
|
|
bMem.dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
|
|
bMem.buffer = kernelBuffer;
|
|
bMem.size = VK_WHOLE_SIZE;
|
|
vkCmdPipelineBarrier(cb, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
|
|
VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0, 0, nullptr, 1,
|
|
&bMem, 0, nullptr);
|
|
|
|
vm.endSingleTimeCommands(cb);
|
|
}
|
|
TIME_END(phase2_StructureTensor);
|
|
perf.structureTensorMs = elapsed_phase2_StructureTensor;
|
|
|
|
// =====================================================================
|
|
// Phase 3: Pre-compute alignment for each non-reference frame
|
|
// =====================================================================
|
|
TIME_START(phase3_CoarseAlignment);
|
|
struct FrameAlignment {
|
|
TileAlignment coarseAlign;
|
|
std::vector<Point> seededFlow;
|
|
};
|
|
std::vector<FrameAlignment> frameAlignments(pendingFrames.size());
|
|
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<CoarseReliabilitySummary> frameCoarseSummaries(
|
|
pendingFrames.size());
|
|
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 =
|
|
computeTileAlignment(referencePyramid, uploadedFrames[i].pyramid, 64,
|
|
tileSize);
|
|
frameAlignments[i].seededFlow = buildDenseAlignmentSeed(
|
|
frameAlignments[i].coarseAlign, (uint32_t)gridW, (uint32_t)gridH,
|
|
(uint32_t)tileSize);
|
|
perf.coarseAlignmentComputeMs += elapsedMillis(coarseAlignStart);
|
|
++perf.coarseAlignedFrames;
|
|
logAlignmentDiagnostics("coarse", i, frameAlignments[i].coarseAlign);
|
|
float sharpnessRatio = pendingFrames[i].score / referenceScore;
|
|
frameCoarseSummaries[i] = buildCoarseReliabilitySummary(
|
|
uploadedFrames[0].proxy, frameAlignments[i].coarseAlign);
|
|
frameLocalMaps[i] = buildCoarseLocalReliabilityMap(
|
|
uploadedFrames[0].proxy, frameAlignments[i].coarseAlign,
|
|
localTilesX, localTilesY);
|
|
float frameWeight = computeYuvStyleCoarseFrameWeight(
|
|
frameAlignments[i].coarseAlign, sharpnessRatio, frameCoarseSummaries[i]);
|
|
frameFusionWeights[i] = frameWeight;
|
|
float coarsePriority =
|
|
0.6f * frameCoarseSummaries[i].flatGoodFraction +
|
|
0.4f * frameCoarseSummaries[i].flatAreaFraction;
|
|
frameRetentionScores[i] = frameWeight * (0.85f + 0.30f * coarsePriority);
|
|
float keepThreshold =
|
|
(coarsePriority > 0.55f) ? 0.12f : 0.16f;
|
|
skipFusionFrame[i] = frameWeight < keepThreshold;
|
|
if (!skipFusionFrame[i]) {
|
|
++coarseEligibleFrames;
|
|
}
|
|
}
|
|
TIME_END(phase3_CoarseAlignment);
|
|
perf.coarseAlignmentMs = elapsed_phase3_CoarseAlignment;
|
|
|
|
if (!kFastPath) {
|
|
std::vector<std::pair<float, size_t>> coarseCandidates;
|
|
coarseCandidates.reserve(coarseEligibleFrames);
|
|
for (size_t i = 1; i < pendingFrames.size(); ++i) {
|
|
if (skipFusionFrame[i]) {
|
|
continue;
|
|
}
|
|
|
|
ScalarStats errStats = computeScalarStats(frameAlignments[i].coarseAlign.errorMap.data(),
|
|
frameAlignments[i].coarseAlign.errorMap.size());
|
|
float errRms = std::sqrt(std::max(errStats.p50, 0.0f));
|
|
if (errRms < 1.15f) {
|
|
frameRetentionScores[i] *= 1.05f;
|
|
}
|
|
coarseCandidates.emplace_back(frameRetentionScores[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);
|
|
if (!kFastPath) {
|
|
// Phase 3b skipped.
|
|
// All candidate frames will be refined during Phase 4 instead,
|
|
// and we avoid the heavy 396ms global robustness readback entirely.
|
|
}
|
|
TIME_END(phase3b_GlobalReliability);
|
|
perf.globalReliabilityMs =
|
|
kFastPath ? 0.0 : 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 = frameAlignments[frameIndex].seededFlow;
|
|
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 < 1; ++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);
|
|
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);
|
|
if (refinedCache.valid) {
|
|
FlowSummary refinedFlow;
|
|
std::vector<float> magnitudes;
|
|
magnitudes.reserve(refinedCache.alignment.size());
|
|
double sumX = 0.0, sumY = 0.0;
|
|
for (const Point &point : refinedCache.alignment) {
|
|
sumX += point.x;
|
|
sumY += point.y;
|
|
magnitudes.push_back(std::sqrt(point.x * point.x + point.y * point.y));
|
|
}
|
|
if (!magnitudes.empty()) {
|
|
refinedFlow.magnitude = computeScalarStats(magnitudes.data(), magnitudes.size());
|
|
refinedFlow.meanX = (float)(sumX / magnitudes.size());
|
|
refinedFlow.meanY = (float)(sumY / magnitudes.size());
|
|
}
|
|
RobustnessSummary robustSummary = readRobustnessSummary(refinedCache.robustness, inputW, inputH);
|
|
LocalReliabilityMap localMap = readLocalReliabilityMap(refinedCache.robustness, inputW, inputH, uploadedFrames[0].proxy);
|
|
LocalReliabilitySummary localSummary = localMap.summary;
|
|
frameLocalMaps[frameIndex] = std::move(localMap);
|
|
frameLocalSummaries[frameIndex] = localSummary;
|
|
float robustWeight = computeRobustnessFrameWeight(refinedFlow, robustSummary, localSummary);
|
|
frameFusionWeights[frameIndex] *= robustWeight;
|
|
}
|
|
return refinedCache.valid;
|
|
};
|
|
|
|
perf.keptFrames = pendingFrames.empty() ? 0 : 1;
|
|
for (size_t i = 1; i < pendingFrames.size(); ++i) {
|
|
if (skipFusionFrame[i]) {
|
|
++perf.skippedFrames;
|
|
std::vector<Point>().swap(frameRefinedCaches[i].alignment);
|
|
std::vector<float>().swap(frameRefinedCaches[i].robustness);
|
|
std::vector<float>().swap(frameRefinedCaches[i].flowVariance);
|
|
frameRefinedCaches[i].valid = false;
|
|
} else {
|
|
++perf.keptFrames;
|
|
}
|
|
}
|
|
|
|
for (size_t i = 1; i < pendingFrames.size(); ++i) {
|
|
if (!kFastPath && !skipFusionFrame[i]) {
|
|
ensureRefinedFrameCache(i);
|
|
}
|
|
}
|
|
|
|
struct FrameMaskBuffer {
|
|
VkBuffer buffer = VK_NULL_HANDLE;
|
|
VkDeviceMemory memory = VK_NULL_HANDLE;
|
|
};
|
|
std::vector<FrameMaskBuffer> frameMaskBuffers(pendingFrames.size());
|
|
const VkDeviceSize frameMaskBufferSize =
|
|
(VkDeviceSize)localTilesX * localTilesY * sizeof(float);
|
|
for (size_t i = 0; i < pendingFrames.size(); ++i) {
|
|
const std::vector<float> &localTileMask =
|
|
(i > 0 && !frameLocalMaps[i].tileWeights.empty())
|
|
? frameLocalMaps[i].tileWeights
|
|
: defaultLocalTileMask;
|
|
|
|
VkBufferCreateInfo maskInfo{};
|
|
maskInfo.sType = VK_STRUCTURE_TYPE_BUFFER_CREATE_INFO;
|
|
maskInfo.size = frameMaskBufferSize;
|
|
maskInfo.usage = VK_BUFFER_USAGE_STORAGE_BUFFER_BIT;
|
|
maskInfo.sharingMode = VK_SHARING_MODE_EXCLUSIVE;
|
|
VK_CHECK(vkCreateBuffer(device, &maskInfo, nullptr,
|
|
&frameMaskBuffers[i].buffer));
|
|
|
|
VkMemoryRequirements maskReqs;
|
|
vkGetBufferMemoryRequirements(device, frameMaskBuffers[i].buffer, &maskReqs);
|
|
VkMemoryAllocateInfo maskAlloc{};
|
|
maskAlloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
|
|
maskAlloc.allocationSize = maskReqs.size;
|
|
maskAlloc.memoryTypeIndex = vm.findMemoryType(
|
|
maskReqs.memoryTypeBits, VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT |
|
|
VK_MEMORY_PROPERTY_HOST_COHERENT_BIT);
|
|
VK_CHECK(
|
|
vkAllocateMemory(device, &maskAlloc, nullptr, &frameMaskBuffers[i].memory));
|
|
vkBindBufferMemory(device, frameMaskBuffers[i].buffer,
|
|
frameMaskBuffers[i].memory, 0);
|
|
|
|
void *maskPtr = nullptr;
|
|
VkResult maskMapRes = vkMapMemory(device, frameMaskBuffers[i].memory, 0,
|
|
frameMaskBufferSize, 0, &maskPtr);
|
|
if (maskMapRes == VK_SUCCESS && maskPtr != nullptr) {
|
|
float *dst = static_cast<float *>(maskPtr);
|
|
std::fill(dst, dst + (size_t)localTilesX * (size_t)localTilesY, 1.0f);
|
|
size_t localMaskCount = std::min(
|
|
localTileMask.size(), (size_t)localTilesX * (size_t)localTilesY);
|
|
std::memcpy(dst, localTileMask.data(), localMaskCount * sizeof(float));
|
|
vkUnmapMemory(device, frameMaskBuffers[i].memory);
|
|
}
|
|
}
|
|
|
|
// Phase 4: Sequential tile processing
|
|
// 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)(maxPlaneTileW + 16) * (maxPlaneTileH + 16) *
|
|
sizeof(uint32_t) * 2;
|
|
VkDeviceSize fusedBayerSize =
|
|
(VkDeviceSize)outputW * outputH * sizeof(uint16_t);
|
|
|
|
{
|
|
const auto outputClearStart = PerfClock::now();
|
|
VkCommandBuffer cb = vm.beginSingleTimeCommands();
|
|
vkCmdFillBuffer(cb, fusedBayerBuffer, 0, fusedBayerSize, 0);
|
|
VkBufferMemoryBarrier outBarrier{
|
|
VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER};
|
|
outBarrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
|
|
outBarrier.dstAccessMask =
|
|
VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT;
|
|
outBarrier.buffer = fusedBayerBuffer;
|
|
outBarrier.size = fusedBayerSize;
|
|
vkCmdPipelineBarrier(cb, VK_PIPELINE_STAGE_TRANSFER_BIT,
|
|
VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0, 0, nullptr,
|
|
1, &outBarrier, 0, nullptr);
|
|
vm.endSingleTimeCommands(cb);
|
|
perf.outputClearMs += elapsedMillis(outputClearStart);
|
|
}
|
|
|
|
for (int ty = 0; ty < numTilesY; ++ty) {
|
|
for (int tx = 0; tx < numTilesX; ++tx) {
|
|
++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
|
|
{
|
|
const auto tileClearStart = PerfClock::now();
|
|
VkCommandBuffer cb = vm.beginSingleTimeCommands();
|
|
for (int phase = 0; phase < 2; ++phase) {
|
|
vkCmdFillBuffer(cb, greenPhaseAccumBuffers[phase], 0,
|
|
greenAccumBufferSize, 0);
|
|
}
|
|
for (int c = 0; c < 2; ++c) {
|
|
vkCmdFillBuffer(cb, rbGatherAccumBuffers[c], 0,
|
|
greenAccumBufferSize, 0);
|
|
}
|
|
VkMemoryBarrier memBarrier{VK_STRUCTURE_TYPE_MEMORY_BARRIER};
|
|
memBarrier.srcAccessMask = VK_ACCESS_TRANSFER_WRITE_BIT;
|
|
memBarrier.dstAccessMask =
|
|
VK_ACCESS_SHADER_READ_BIT | VK_ACCESS_SHADER_WRITE_BIT;
|
|
vkCmdPipelineBarrier(cb, VK_PIPELINE_STAGE_TRANSFER_BIT,
|
|
VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0, 1,
|
|
&memBarrier, 0, nullptr, 0, nullptr);
|
|
vm.endSingleTimeCommands(cb);
|
|
perf.tileClearMs += elapsedMillis(tileClearStart);
|
|
}
|
|
|
|
// Process all frames for this tile
|
|
for (size_t i = 0; i < pendingFrames.size(); ++i) {
|
|
bool isRef = (i == 0);
|
|
const auto &frame = pendingFrames[i];
|
|
|
|
if (!isRef && skipFusionFrame[i]) {
|
|
continue;
|
|
}
|
|
++perf.tileScatterFrameInstances;
|
|
|
|
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)frame.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 = isRef ? 1.0f : frameFusionWeights[i];
|
|
|
|
const auto localMaskUploadStart = PerfClock::now();
|
|
updateBufferDescriptorSet(device, greenGatherSet,
|
|
frameMaskBuffers[i].buffer, VK_WHOLE_SIZE, 8);
|
|
updateBufferDescriptorSet(device, colorGatherSets[0],
|
|
frameMaskBuffers[i].buffer, VK_WHOLE_SIZE, 8);
|
|
perf.localMaskUploadMs += elapsedMillis(localMaskUploadStart);
|
|
if (!kFastPath && !isRef &&
|
|
!frameRefinedCaches[i].valid) {
|
|
ensureRefinedFrameCache(i);
|
|
}
|
|
const RefinedFrameCache *refinedCache =
|
|
(!kFastPath && !isRef &&
|
|
frameRefinedCaches[i].valid)
|
|
? &frameRefinedCaches[i]
|
|
: nullptr;
|
|
|
|
if (!isRef) {
|
|
++perf.tileAlignedFrameInstances;
|
|
if (kFastPath) {
|
|
const auto tileAlignmentSeedStart = PerfClock::now();
|
|
uploadAlignmentField(device, alignmentMemory,
|
|
frameAlignments[i].seededFlow);
|
|
fillFloatBuffer(device, robustnessMemory, (size_t)inputW * inputH,
|
|
1.0f);
|
|
fillFloatBuffer(device, flowVarianceMemory, (size_t)inputW * inputH,
|
|
0.0f);
|
|
perf.tileAlignmentSeedMs += elapsedMillis(tileAlignmentSeedStart);
|
|
} else if (refinedCache != nullptr) {
|
|
const auto tileRefinedStateUploadStart = PerfClock::now();
|
|
uploadAlignmentField(device, alignmentMemory, refinedCache->alignment);
|
|
uploadFloatBuffer(device, robustnessMemory, refinedCache->robustness);
|
|
uploadFloatBuffer(device, flowVarianceMemory,
|
|
refinedCache->flowVariance);
|
|
perf.tileRefinedStateUploadMs +=
|
|
elapsedMillis(tileRefinedStateUploadStart);
|
|
} else {
|
|
const auto tileAlignmentSeedStart = PerfClock::now();
|
|
// Upload coarse alignment for this frame
|
|
std::vector<Point> seededFlow = frameAlignments[i].seededFlow;
|
|
uploadAlignmentField(device, alignmentMemory, seededFlow);
|
|
fillFloatBuffer(device, robustnessMemory, (size_t)inputW * inputH,
|
|
1.0f);
|
|
perf.tileAlignmentSeedMs += elapsedMillis(tileAlignmentSeedStart);
|
|
|
|
// LK alignment + Robustness
|
|
const auto tileLkStage1Start = PerfClock::now();
|
|
VkCommandBuffer cb = vm.beginSingleTimeCommands();
|
|
|
|
// HOST_WRITE -> SHADER barrier
|
|
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);
|
|
|
|
// Set pre-processing tile range (plane resolution)
|
|
pc.tileX = 0;
|
|
pc.tileY = 0;
|
|
pc.tileW = inputW;
|
|
pc.tileH = inputH;
|
|
pc.bufferStride = tileW + 16;
|
|
|
|
// LK Alignment
|
|
vkCmdBindPipeline(cb, VK_PIPELINE_BIND_POINT_COMPUTE,
|
|
alignLkPipeline);
|
|
updateImageDescriptorSet(device, alignSet, uploadedFrames[0].view,
|
|
sampler, 0);
|
|
updateImageDescriptorSet(device, alignSet, uploadedFrames[i].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);
|
|
perf.tileAlignmentLkStage1Ms += elapsedMillis(tileLkStage1Start);
|
|
|
|
const auto tileRegularize1Start = PerfClock::now();
|
|
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);
|
|
}
|
|
perf.tileAlignmentRegularize1Ms +=
|
|
elapsedMillis(tileRegularize1Start);
|
|
|
|
const auto tileRobustness1Start = PerfClock::now();
|
|
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);
|
|
|
|
// Robustness
|
|
vkCmdBindPipeline(cb, VK_PIPELINE_BIND_POINT_COMPUTE,
|
|
robustnessPipeline);
|
|
updateImageDescriptorSet(device, robustSet, uploadedFrames[0].view,
|
|
sampler, 0);
|
|
updateImageDescriptorSet(device, robustSet, uploadedFrames[i].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);
|
|
|
|
vm.endSingleTimeCommands(cb);
|
|
perf.tileRobustness1Ms += elapsedMillis(tileRobustness1Start);
|
|
|
|
const auto tileLkStage2Start = PerfClock::now();
|
|
cb = vm.beginSingleTimeCommands();
|
|
vkCmdBindPipeline(cb, VK_PIPELINE_BIND_POINT_COMPUTE,
|
|
alignLkPipeline);
|
|
updateImageDescriptorSet(device, alignSet, uploadedFrames[0].view,
|
|
sampler, 0);
|
|
updateImageDescriptorSet(device, alignSet, uploadedFrames[i].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);
|
|
perf.tileAlignmentLkStage2Ms += elapsedMillis(tileLkStage2Start);
|
|
|
|
const auto tileRegularize2Start = PerfClock::now();
|
|
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);
|
|
}
|
|
perf.tileAlignmentRegularize2Ms +=
|
|
elapsedMillis(tileRegularize2Start);
|
|
|
|
const auto tileRobustness2Start = PerfClock::now();
|
|
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[i].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);
|
|
perf.tileRobustness2Ms += elapsedMillis(tileRobustness2Start);
|
|
}
|
|
}
|
|
|
|
// Accumulate this frame's contribution for this tile's 3 channels
|
|
{
|
|
const auto gatherStart = PerfClock::now();
|
|
VkCommandBuffer cb = vm.beginSingleTimeCommands();
|
|
VkBufferMemoryBarrier preGatherBarriers[4]{};
|
|
uint32_t preGatherBarrierCount = 0;
|
|
|
|
preGatherBarriers[preGatherBarrierCount].sType =
|
|
VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER;
|
|
preGatherBarriers[preGatherBarrierCount].srcAccessMask =
|
|
VK_ACCESS_HOST_WRITE_BIT;
|
|
preGatherBarriers[preGatherBarrierCount].dstAccessMask =
|
|
VK_ACCESS_SHADER_READ_BIT;
|
|
preGatherBarriers[preGatherBarrierCount].buffer =
|
|
frameMaskBuffers[i].buffer;
|
|
preGatherBarriers[preGatherBarrierCount].size = VK_WHOLE_SIZE;
|
|
++preGatherBarrierCount;
|
|
|
|
if (kFastPath ? !isRef : refinedCache != nullptr) {
|
|
preGatherBarriers[preGatherBarrierCount].sType =
|
|
VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER;
|
|
preGatherBarriers[preGatherBarrierCount].srcAccessMask =
|
|
VK_ACCESS_HOST_WRITE_BIT;
|
|
preGatherBarriers[preGatherBarrierCount].dstAccessMask =
|
|
VK_ACCESS_SHADER_READ_BIT;
|
|
preGatherBarriers[preGatherBarrierCount].buffer = alignmentBuffer;
|
|
preGatherBarriers[preGatherBarrierCount].size = VK_WHOLE_SIZE;
|
|
++preGatherBarrierCount;
|
|
|
|
preGatherBarriers[preGatherBarrierCount].sType =
|
|
VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER;
|
|
preGatherBarriers[preGatherBarrierCount].srcAccessMask =
|
|
VK_ACCESS_HOST_WRITE_BIT;
|
|
preGatherBarriers[preGatherBarrierCount].dstAccessMask =
|
|
VK_ACCESS_SHADER_READ_BIT;
|
|
preGatherBarriers[preGatherBarrierCount].buffer = robustnessBuffer;
|
|
preGatherBarriers[preGatherBarrierCount].size = VK_WHOLE_SIZE;
|
|
++preGatherBarrierCount;
|
|
|
|
preGatherBarriers[preGatherBarrierCount].sType =
|
|
VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER;
|
|
preGatherBarriers[preGatherBarrierCount].srcAccessMask =
|
|
VK_ACCESS_HOST_WRITE_BIT;
|
|
preGatherBarriers[preGatherBarrierCount].dstAccessMask =
|
|
VK_ACCESS_SHADER_READ_BIT;
|
|
preGatherBarriers[preGatherBarrierCount].buffer =
|
|
flowVarianceBuffer;
|
|
preGatherBarriers[preGatherBarrierCount].size = VK_WHOLE_SIZE;
|
|
++preGatherBarrierCount;
|
|
}
|
|
vkCmdPipelineBarrier(cb, VK_PIPELINE_STAGE_HOST_BIT,
|
|
VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0, 0,
|
|
nullptr, preGatherBarrierCount,
|
|
preGatherBarriers, 0, nullptr);
|
|
|
|
updateImageDescriptorSet(device, greenGatherSet,
|
|
uploadedFrames[i].view, sampler, 0);
|
|
vkCmdBindPipeline(cb, VK_PIPELINE_BIND_POINT_COMPUTE,
|
|
greenGatherPipeline);
|
|
vkCmdBindDescriptorSets(cb, VK_PIPELINE_BIND_POINT_COMPUTE,
|
|
greenGatherPipelineLayout, 0, 1,
|
|
&greenGatherSet, 0, nullptr);
|
|
|
|
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);
|
|
|
|
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, greenGatherPipelineLayout,
|
|
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) {
|
|
greenBarriers[phase].sType =
|
|
VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER;
|
|
greenBarriers[phase].srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT;
|
|
greenBarriers[phase].dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
|
|
greenBarriers[phase].buffer = greenPhaseAccumBuffers[phase];
|
|
greenBarriers[phase].size = VK_WHOLE_SIZE;
|
|
}
|
|
vkCmdPipelineBarrier(cb, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
|
|
VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0, 0,
|
|
nullptr, 2, greenBarriers, 0, nullptr);
|
|
|
|
vkCmdBindPipeline(cb, VK_PIPELINE_BIND_POINT_COMPUTE,
|
|
colorGatherPipeline);
|
|
updateImageDescriptorSet(device, colorGatherSets[0],
|
|
uploadedFrames[i].view, sampler, 0);
|
|
vkCmdBindDescriptorSets(cb, VK_PIPELINE_BIND_POINT_COMPUTE,
|
|
colorGatherPipelineLayout, 0, 1,
|
|
&colorGatherSets[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 = 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, colorGatherPipelineLayout,
|
|
VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof(pc),
|
|
&pc);
|
|
vkCmdDispatch(cb, (pc.tileW + 15) / 16, (pc.tileH + 15) / 16, 1);
|
|
}
|
|
|
|
VkBufferMemoryBarrier colorBarriers[2]{};
|
|
for (int c = 0; c < 2; ++c) {
|
|
colorBarriers[c].sType = VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER;
|
|
colorBarriers[c].srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT;
|
|
colorBarriers[c].dstAccessMask = VK_ACCESS_SHADER_READ_BIT;
|
|
colorBarriers[c].buffer = rbGatherAccumBuffers[c];
|
|
colorBarriers[c].size = VK_WHOLE_SIZE;
|
|
}
|
|
vkCmdPipelineBarrier(cb, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
|
|
VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0, 0,
|
|
nullptr, 2, colorBarriers, 0, nullptr);
|
|
|
|
vm.endSingleTimeCommands(cb);
|
|
perf.gatherMs += elapsedMillis(gatherStart);
|
|
++perf.tileScatterFrameInstances; // Keep stats count unchanged for simplicity
|
|
}
|
|
} // end for each frame
|
|
|
|
// Normalize this tile and write to the fused Bayer output buffer.
|
|
// NOTE: Bind staging buffer with a row offset to stay within
|
|
// maxStorageBufferRange. The normalize shaders write local row indices
|
|
// into the bound row band, but still use global output coordinates for
|
|
// CFA parity.
|
|
{
|
|
const auto normalizeStart = PerfClock::now();
|
|
VkCommandBuffer cb = vm.beginSingleTimeCommands();
|
|
|
|
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);
|
|
|
|
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,
|
|
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,
|
|
VK_WHOLE_SIZE, 3);
|
|
updateBufferDescriptorSet(device, greenNormalizeSet, priorWeightBuffer,
|
|
VK_WHOLE_SIZE, 4);
|
|
vkCmdBindPipeline(cb, VK_PIPELINE_BIND_POINT_COMPUTE,
|
|
greenNormalizePipeline);
|
|
vkCmdBindDescriptorSets(cb, VK_PIPELINE_BIND_POINT_COMPUTE,
|
|
greenNormalizePipelineLayout, 0, 1,
|
|
&greenNormalizeSet, 0, nullptr);
|
|
|
|
PushConstants greenPc{};
|
|
greenPc.width = outputW;
|
|
greenPc.height = outputH;
|
|
greenPc.scale = (float)scale;
|
|
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;
|
|
memcpy(greenPc.blackLevel, mBlackLevel, 4 * sizeof(float));
|
|
memcpy(greenPc.wbGains, mWbGains, 4 * sizeof(float));
|
|
|
|
vkCmdPushConstants(cb, greenNormalizePipelineLayout,
|
|
VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof(greenPc),
|
|
&greenPc);
|
|
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);
|
|
}
|
|
}
|
|
|
|
VkBufferMemoryBarrier greenOutputBarrier{
|
|
VK_STRUCTURE_TYPE_BUFFER_MEMORY_BARRIER};
|
|
greenOutputBarrier.srcAccessMask = VK_ACCESS_SHADER_WRITE_BIT;
|
|
greenOutputBarrier.dstAccessMask = VK_ACCESS_SHADER_READ_BIT |
|
|
VK_ACCESS_SHADER_WRITE_BIT;
|
|
greenOutputBarrier.buffer = fusedBayerBuffer;
|
|
greenOutputBarrier.size = VK_WHOLE_SIZE;
|
|
vkCmdPipelineBarrier(cb, VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT,
|
|
VK_PIPELINE_STAGE_COMPUTE_SHADER_BIT, 0, 0,
|
|
nullptr, 1, &greenOutputBarrier, 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, VK_WHOLE_SIZE, 0);
|
|
updateBufferDescriptorSet(device, colorNormalizeSets[colorIdx],
|
|
rbGatherAccumBuffers[colorIdx],
|
|
VK_WHOLE_SIZE, 1);
|
|
updateBufferDescriptorSet(device, colorNormalizeSets[colorIdx],
|
|
priorBayerBuffer, VK_WHOLE_SIZE, 2);
|
|
updateBufferDescriptorSet(device, colorNormalizeSets[colorIdx],
|
|
priorWeightBuffer, VK_WHOLE_SIZE, 3);
|
|
vkCmdBindPipeline(cb, VK_PIPELINE_BIND_POINT_COMPUTE,
|
|
colorNormalizePipeline);
|
|
vkCmdBindDescriptorSets(cb, VK_PIPELINE_BIND_POINT_COMPUTE,
|
|
colorNormalizePipelineLayout, 0, 1,
|
|
&colorNormalizeSets[colorIdx], 0, nullptr);
|
|
|
|
PushConstants colorPc{};
|
|
colorPc.width = outputW;
|
|
colorPc.height = outputH;
|
|
colorPc.scale = (float)scale;
|
|
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));
|
|
memcpy(colorPc.wbGains, mWbGains, 4 * sizeof(float));
|
|
|
|
vkCmdPushConstants(cb, colorNormalizePipelineLayout,
|
|
VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof(colorPc),
|
|
&colorPc);
|
|
vkCmdDispatch(cb, (colorRange.width + 15) / 16,
|
|
(colorRange.height + 15) / 16, 1);
|
|
}
|
|
|
|
vm.endSingleTimeCommands(cb);
|
|
perf.normalizeMs += elapsedMillis(normalizeStart);
|
|
}
|
|
|
|
} // end for tx
|
|
} // end for ty
|
|
TIME_END(phase4_TileProcessing);
|
|
perf.tileProcessingMs = elapsed_phase4_TileProcessing;
|
|
|
|
// =====================================================================
|
|
// Phase 4B: Aggregate fusion statistics
|
|
// =====================================================================
|
|
if (kEnableRawSuperResPerfLogs) {
|
|
perf.totalEffectiveFrames = 1.0; // Reference frame
|
|
double sumRobustness = 1.0;
|
|
double sumWeakFraction = 0.0;
|
|
size_t totalCandidatePixels = 0;
|
|
size_t totalProjectedPixels = 0;
|
|
size_t totalActivePixels = 0;
|
|
size_t totalStrongPixels = 0;
|
|
double totalWeightedPixels = 0.0;
|
|
|
|
FusionParticipationSummary refParticipation = computeFusionParticipation(
|
|
width, height, inputW, inputH, outputW, outputH, (float)scale,
|
|
std::vector<float>(), std::vector<Point>(), (uint32_t)gridW,
|
|
(uint32_t)gridH, (uint32_t)tileSize, true);
|
|
logFusionParticipation(0, false, 1.0f, refParticipation);
|
|
|
|
for (size_t i = 1; i < pendingFrames.size(); ++i) {
|
|
if (!skipFusionFrame[i] && frameRefinedCaches[i].valid) {
|
|
RobustnessSummary rs = readRobustnessSummary(
|
|
frameRefinedCaches[i].robustness, inputW, inputH);
|
|
float contr = frameFusionWeights[i] * rs.values.meanValue;
|
|
perf.totalEffectiveFrames += contr;
|
|
sumRobustness += rs.values.meanValue;
|
|
sumWeakFraction += rs.weak05Fraction;
|
|
|
|
FlowSummary flow;
|
|
if (!frameRefinedCaches[i].alignment.empty()) {
|
|
std::vector<float> magnitudes;
|
|
magnitudes.reserve(frameRefinedCaches[i].alignment.size());
|
|
for (const Point &p : frameRefinedCaches[i].alignment) {
|
|
magnitudes.push_back(std::sqrt(p.x * p.x + p.y * p.y));
|
|
}
|
|
flow.magnitude =
|
|
computeScalarStats(magnitudes.data(), magnitudes.size());
|
|
}
|
|
logRefinedFieldDiagnostics(i, frameFusionWeights[i], rs, flow);
|
|
|
|
FusionParticipationSummary participation = computeFusionParticipation(
|
|
width, height, inputW, inputH, outputW, outputH, (float)scale,
|
|
frameRefinedCaches[i].robustness, frameRefinedCaches[i].alignment,
|
|
(uint32_t)gridW, (uint32_t)gridH, (uint32_t)tileSize, false);
|
|
logFusionParticipation(i, false, frameFusionWeights[i],
|
|
participation);
|
|
totalCandidatePixels += participation.totalPixels;
|
|
totalProjectedPixels += participation.projectedPixels;
|
|
totalActivePixels += participation.activePixels;
|
|
totalStrongPixels += participation.strongPixels;
|
|
totalWeightedPixels += participation.robustnessWeightedPixels;
|
|
} else {
|
|
FusionParticipationSummary participation;
|
|
participation.totalPixels = (size_t)inputW * inputH * 4u;
|
|
logFusionParticipation(i, true, frameFusionWeights[i], participation);
|
|
}
|
|
}
|
|
if (totalCandidatePixels > 0) {
|
|
LOGI("RawSuperResFusionCoverage nonRefTotal: projected=%zu/%zu(%.2f%%) "
|
|
"activeR015=%zu/%zu(%.2f%%) strongR05=%zu/%zu(%.2f%%) "
|
|
"weightedR=%.2f%%",
|
|
totalProjectedPixels, totalCandidatePixels,
|
|
100.0 * (double)totalProjectedPixels /
|
|
(double)totalCandidatePixels,
|
|
totalActivePixels, totalCandidatePixels,
|
|
100.0 * (double)totalActivePixels / (double)totalCandidatePixels,
|
|
totalStrongPixels, totalCandidatePixels,
|
|
100.0 * (double)totalStrongPixels / (double)totalCandidatePixels,
|
|
100.0 * totalWeightedPixels / (double)totalCandidatePixels);
|
|
}
|
|
if (perf.keptFrames > 0) {
|
|
perf.avgRobustness = (double)sumRobustness / (double)perf.keptFrames;
|
|
perf.avgWeakFraction = (double)sumWeakFraction / (double)perf.keptFrames;
|
|
}
|
|
}
|
|
|
|
// =====================================================================
|
|
// Phase 5: Read back result from staging buffer
|
|
// =====================================================================
|
|
{
|
|
TIME_START(phase5_OutputReadback);
|
|
void *mapData;
|
|
VK_CHECK(
|
|
vkMapMemory(device, fusedBayerMemory, 0, VK_WHOLE_SIZE, 0, &mapData));
|
|
size_t reqSize = (size_t)outputW * outputH * sizeof(uint16_t);
|
|
if (bufferSize >= reqSize) {
|
|
memcpy(outBuffer, mapData, reqSize);
|
|
logRawFlatVarianceDiagnostics(
|
|
pendingFrames.front().rawData, static_cast<const uint16_t *>(mapData),
|
|
width, height, outputW, outputH, mCfaPattern, (float)scale,
|
|
mBlackLevel, mWhiteLevel, mLensShadingMap, mLscWidth, mLscHeight,
|
|
uploadedFrames[0].proxy);
|
|
} else {
|
|
LOGE("Buffer too small! Req: %zu, Has: %zu", reqSize, bufferSize);
|
|
}
|
|
vkUnmapMemory(device, fusedBayerMemory);
|
|
TIME_END(phase5_OutputReadback);
|
|
perf.outputReadbackMs = elapsed_phase5_OutputReadback;
|
|
}
|
|
|
|
// =====================================================================
|
|
// Cleanup
|
|
// =====================================================================
|
|
const auto cleanupStart = PerfClock::now();
|
|
for (auto &uf : uploadedFrames) {
|
|
if (uf.view)
|
|
vkDestroyImageView(device, uf.view, nullptr);
|
|
if (uf.image)
|
|
vkDestroyImage(device, uf.image, nullptr);
|
|
if (uf.mem)
|
|
vkFreeMemory(device, uf.mem, nullptr);
|
|
}
|
|
for (auto &mask : frameMaskBuffers) {
|
|
if (mask.buffer != VK_NULL_HANDLE)
|
|
vkDestroyBuffer(device, mask.buffer, nullptr);
|
|
if (mask.memory != VK_NULL_HANDLE)
|
|
vkFreeMemory(device, mask.memory, nullptr);
|
|
}
|
|
|
|
vkDestroySampler(device, sampler, nullptr);
|
|
perf.cleanupMs = elapsedMillis(cleanupStart);
|
|
perf.totalMs = elapsedMillis(processStackStart);
|
|
perf.logSummary(outputW, outputH, scale);
|
|
return true;
|
|
}
|
|
|
|
void VulkanRawStacker::releaseVulkanResources() {
|
|
VkDevice device = VulkanManager::getInstance().getDevice();
|
|
if (device == VK_NULL_HANDLE)
|
|
return;
|
|
|
|
if (descriptorPool != VK_NULL_HANDLE)
|
|
vkDestroyDescriptorPool(device, descriptorPool, nullptr);
|
|
// New Pipelines
|
|
if (structureTensorSetLayout)
|
|
vkDestroyDescriptorSetLayout(device, structureTensorSetLayout, nullptr);
|
|
if (structureTensorPipelineLayout)
|
|
vkDestroyPipelineLayout(device, structureTensorPipelineLayout, nullptr);
|
|
if (structureTensorPipeline)
|
|
vkDestroyPipeline(device, structureTensorPipeline, nullptr);
|
|
|
|
if (alignLkSetLayout)
|
|
vkDestroyDescriptorSetLayout(device, alignLkSetLayout, nullptr);
|
|
if (alignLkPipelineLayout)
|
|
vkDestroyPipelineLayout(device, alignLkPipelineLayout, nullptr);
|
|
if (alignLkPipeline)
|
|
vkDestroyPipeline(device, alignLkPipeline, nullptr);
|
|
|
|
if (robustnessSetLayout)
|
|
vkDestroyDescriptorSetLayout(device, robustnessSetLayout, nullptr);
|
|
if (robustnessPipelineLayout)
|
|
vkDestroyPipelineLayout(device, robustnessPipelineLayout, nullptr);
|
|
if (robustnessPipeline)
|
|
vkDestroyPipeline(device, robustnessPipeline, nullptr);
|
|
|
|
if (greenGatherSetLayout)
|
|
vkDestroyDescriptorSetLayout(device, greenGatherSetLayout, nullptr);
|
|
if (greenGatherPipelineLayout)
|
|
vkDestroyPipelineLayout(device, greenGatherPipelineLayout, nullptr);
|
|
if (greenGatherPipeline)
|
|
vkDestroyPipeline(device, greenGatherPipeline, nullptr);
|
|
|
|
if (colorGatherSetLayout)
|
|
vkDestroyDescriptorSetLayout(device, colorGatherSetLayout, nullptr);
|
|
if (colorGatherPipelineLayout)
|
|
vkDestroyPipelineLayout(device, colorGatherPipelineLayout, nullptr);
|
|
if (colorGatherPipeline)
|
|
vkDestroyPipeline(device, colorGatherPipeline, nullptr);
|
|
|
|
if (referenceNormalizeSetLayout)
|
|
vkDestroyDescriptorSetLayout(device, referenceNormalizeSetLayout, nullptr);
|
|
if (referenceNormalizePipelineLayout)
|
|
vkDestroyPipelineLayout(device, referenceNormalizePipelineLayout, nullptr);
|
|
if (referenceNormalizePipeline)
|
|
vkDestroyPipeline(device, referenceNormalizePipeline, nullptr);
|
|
|
|
if (referencePriorSetLayout)
|
|
vkDestroyDescriptorSetLayout(device, referencePriorSetLayout, nullptr);
|
|
if (referencePriorPipelineLayout)
|
|
vkDestroyPipelineLayout(device, referencePriorPipelineLayout, nullptr);
|
|
if (referencePriorPipeline)
|
|
vkDestroyPipeline(device, referencePriorPipeline, nullptr);
|
|
|
|
if (greenNormalizeSetLayout)
|
|
vkDestroyDescriptorSetLayout(device, greenNormalizeSetLayout, nullptr);
|
|
if (greenNormalizePipelineLayout)
|
|
vkDestroyPipelineLayout(device, greenNormalizePipelineLayout, nullptr);
|
|
if (greenNormalizePipeline)
|
|
vkDestroyPipeline(device, greenNormalizePipeline, nullptr);
|
|
|
|
if (colorNormalizeSetLayout)
|
|
vkDestroyDescriptorSetLayout(device, colorNormalizeSetLayout, nullptr);
|
|
if (colorNormalizePipelineLayout)
|
|
vkDestroyPipelineLayout(device, colorNormalizePipelineLayout, nullptr);
|
|
if (colorNormalizePipeline)
|
|
vkDestroyPipeline(device, colorNormalizePipeline, nullptr);
|
|
|
|
if (fusedBayerBuffer != VK_NULL_HANDLE)
|
|
vkDestroyBuffer(device, fusedBayerBuffer, nullptr);
|
|
if (fusedBayerMemory != VK_NULL_HANDLE)
|
|
vkFreeMemory(device, fusedBayerMemory, nullptr);
|
|
|
|
if (alignmentBuffer != VK_NULL_HANDLE)
|
|
vkDestroyBuffer(device, alignmentBuffer, nullptr);
|
|
if (alignmentMemory != VK_NULL_HANDLE)
|
|
vkFreeMemory(device, alignmentMemory, nullptr);
|
|
|
|
if (kernelBuffer)
|
|
vkDestroyBuffer(device, kernelBuffer, nullptr);
|
|
if (kernelMemory)
|
|
vkFreeMemory(device, kernelMemory, nullptr);
|
|
|
|
if (robustnessBuffer)
|
|
vkDestroyBuffer(device, robustnessBuffer, nullptr);
|
|
if (robustnessMemory)
|
|
vkFreeMemory(device, robustnessMemory, nullptr);
|
|
if (flowVarianceBuffer)
|
|
vkDestroyBuffer(device, flowVarianceBuffer, nullptr);
|
|
if (flowVarianceMemory)
|
|
vkFreeMemory(device, flowVarianceMemory, nullptr);
|
|
if (localTileMaskBuffer)
|
|
vkDestroyBuffer(device, localTileMaskBuffer, nullptr);
|
|
if (localTileMaskMemory)
|
|
vkFreeMemory(device, localTileMaskMemory, nullptr);
|
|
|
|
for (int phase = 0; phase < 2; ++phase) {
|
|
if (greenPhaseAccumBuffers[phase])
|
|
vkDestroyBuffer(device, greenPhaseAccumBuffers[phase], nullptr);
|
|
if (greenPhaseAccumMemories[phase])
|
|
vkFreeMemory(device, greenPhaseAccumMemories[phase], nullptr);
|
|
}
|
|
for (int c = 0; c < 2; ++c) {
|
|
if (rbGatherAccumBuffers[c])
|
|
vkDestroyBuffer(device, rbGatherAccumBuffers[c], nullptr);
|
|
if (rbGatherAccumMemories[c])
|
|
vkFreeMemory(device, rbGatherAccumMemories[c], nullptr);
|
|
}
|
|
if (priorBayerBuffer)
|
|
vkDestroyBuffer(device, priorBayerBuffer, nullptr);
|
|
if (priorBayerMemory)
|
|
vkFreeMemory(device, priorBayerMemory, nullptr);
|
|
if (priorWeightBuffer)
|
|
vkDestroyBuffer(device, priorWeightBuffer, nullptr);
|
|
if (priorWeightMemory)
|
|
vkFreeMemory(device, priorWeightMemory, nullptr);
|
|
if (normalizedReferenceBuffer)
|
|
vkDestroyBuffer(device, normalizedReferenceBuffer, nullptr);
|
|
if (normalizedReferenceMemory)
|
|
vkFreeMemory(device, normalizedReferenceMemory, nullptr);
|
|
|
|
if (lscView)
|
|
vkDestroyImageView(device, lscView, nullptr);
|
|
if (lscImage)
|
|
vkDestroyImage(device, lscImage, nullptr);
|
|
if (lscMemory)
|
|
vkFreeMemory(device, lscMemory, nullptr);
|
|
if (lscSampler)
|
|
vkDestroySampler(device, lscSampler, nullptr);
|
|
}
|