fix: rcd border
This commit is contained in:
1 parent
71c3d512de
commit
d5e504f718
6 files changed
+462
-291
No files matched your search
@@ -24,7 +24,8 @@ internal object GlesRawHdrConfig {
|
||||
}
|
||||
|
||||
internal object GlesRawHdrShaders {
|
||||
fun rcdNormalPopulate(rawCommon: String): String = """
|
||||
/** HDR 累加结果到统一 RCD 工作 CFA/种子 RGB 缓冲的输入适配器。 */
|
||||
fun normalCfaInputAdapter(rawCommon: String): String = """
|
||||
#version 310 es
|
||||
$rawCommon
|
||||
layout(local_size_x = 16, local_size_y = 16) in;
|
||||
@@ -75,7 +76,8 @@ internal object GlesRawHdrShaders {
|
||||
}
|
||||
""".trimIndent()
|
||||
|
||||
val rcdStoreRgb: String = """
|
||||
/** RCD 完成后的 HDR 曝光域转换;解马赛克与边界处理不在这里实现。 */
|
||||
val workingRgbStore: String = """
|
||||
#version 310 es
|
||||
precision highp float;
|
||||
precision highp int;
|
||||
|
||||
@@ -245,9 +245,10 @@ class GlesRawStacker(
|
||||
private var rcdStripeStep41Program = 0
|
||||
private var rcdStripeStep42Program = 0
|
||||
private var rcdStripeStep43Program = 0
|
||||
private var rcdStripeBorderPpgProgram = 0
|
||||
private var hdrShortGlobalAlignProgram = 0
|
||||
private var hdrRcdNormalPopulateProgram = 0
|
||||
private var hdrRcdStoreRgbProgram = 0
|
||||
private var hdrNormalCfaInputProgram = 0
|
||||
private var hdrWorkingRgbStoreProgram = 0
|
||||
private var hdrRgbFusionProgram = 0
|
||||
private var pgtmStatsProgram = 0
|
||||
private var diagnosticAlignmentProgram = 0
|
||||
@@ -812,7 +813,10 @@ class GlesRawStacker(
|
||||
SUPER_RESOLUTION_NORMALIZE_RCD_FRAGMENT_SHADER,
|
||||
"raw_sr_normalize",
|
||||
)
|
||||
rcdStripePopulateProgram = linkComputeProgram(RCD_STRIPE_POPULATE_COMPUTE_SHADER, "raw_sr_rcd_populate")
|
||||
rcdStripePopulateProgram = linkComputeProgram(
|
||||
RcdShaders.stripePopulate(RAW_COMMON),
|
||||
"raw_sr_rcd_populate",
|
||||
)
|
||||
rcdStripeStep1Program = linkComputeProgram(RcdShaders.STEP_1, "raw_sr_rcd_step1")
|
||||
rcdStripeStep2Program = linkComputeProgram(RcdShaders.STEP_2, "raw_sr_rcd_step2")
|
||||
rcdStripeStep3Program = linkComputeProgram(RcdShaders.STEP_3, "raw_sr_rcd_step3")
|
||||
@@ -820,6 +824,10 @@ class GlesRawStacker(
|
||||
rcdStripeStep41Program = linkComputeProgram(RcdShaders.STEP_4_1, "raw_sr_rcd_step41")
|
||||
rcdStripeStep42Program = linkComputeProgram(RcdShaders.STEP_4_2, "raw_sr_rcd_step42")
|
||||
rcdStripeStep43Program = linkComputeProgram(RcdShaders.STEP_4_3, "raw_sr_rcd_step43")
|
||||
rcdStripeBorderPpgProgram = linkComputeProgram(
|
||||
RcdShaders.STRIPE_BORDER_PPG,
|
||||
"raw_sr_rcd_border_ppg",
|
||||
)
|
||||
}
|
||||
initDiagnosticPrograms()
|
||||
}
|
||||
@@ -831,13 +839,13 @@ class GlesRawStacker(
|
||||
"raw_hdr_short_global_align",
|
||||
)
|
||||
hdrWeightMap = createHdrWeightMap().also { it.initPrograms() }
|
||||
hdrRcdNormalPopulateProgram = linkComputeProgram(
|
||||
GlesRawHdrShaders.rcdNormalPopulate(RAW_COMMON),
|
||||
"raw_hdr_rcd_normal_populate",
|
||||
hdrNormalCfaInputProgram = linkComputeProgram(
|
||||
GlesRawHdrShaders.normalCfaInputAdapter(RAW_COMMON),
|
||||
"raw_hdr_normal_cfa_input",
|
||||
)
|
||||
hdrRcdStoreRgbProgram = linkComputeProgram(
|
||||
GlesRawHdrShaders.rcdStoreRgb,
|
||||
"raw_hdr_rcd_store_rgb",
|
||||
hdrWorkingRgbStoreProgram = linkComputeProgram(
|
||||
GlesRawHdrShaders.workingRgbStore,
|
||||
"raw_hdr_working_rgb_store",
|
||||
)
|
||||
hdrRgbFusionProgram = linkGraphicsProgram(
|
||||
FULLSCREEN_VERTEX_SHADER,
|
||||
@@ -845,7 +853,10 @@ class GlesRawStacker(
|
||||
"raw_hdr_rgb_fusion",
|
||||
)
|
||||
if (rcdStripePopulateProgram == 0) {
|
||||
rcdStripePopulateProgram = linkComputeProgram(RCD_STRIPE_POPULATE_COMPUTE_SHADER, "raw_hdr_rcd_populate")
|
||||
rcdStripePopulateProgram = linkComputeProgram(
|
||||
RcdShaders.stripePopulate(RAW_COMMON),
|
||||
"raw_hdr_rcd_populate",
|
||||
)
|
||||
rcdStripeStep1Program = linkComputeProgram(RcdShaders.STEP_1, "raw_hdr_rcd_step1")
|
||||
rcdStripeStep2Program = linkComputeProgram(RcdShaders.STEP_2, "raw_hdr_rcd_step2")
|
||||
rcdStripeStep3Program = linkComputeProgram(RcdShaders.STEP_3, "raw_hdr_rcd_step3")
|
||||
@@ -853,6 +864,10 @@ class GlesRawStacker(
|
||||
rcdStripeStep41Program = linkComputeProgram(RcdShaders.STEP_4_1, "raw_hdr_rcd_step41")
|
||||
rcdStripeStep42Program = linkComputeProgram(RcdShaders.STEP_4_2, "raw_hdr_rcd_step42")
|
||||
rcdStripeStep43Program = linkComputeProgram(RcdShaders.STEP_4_3, "raw_hdr_rcd_step43")
|
||||
rcdStripeBorderPpgProgram = linkComputeProgram(
|
||||
RcdShaders.STRIPE_BORDER_PPG,
|
||||
"raw_hdr_rcd_border_ppg",
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1209,10 +1224,11 @@ class GlesRawStacker(
|
||||
GLES31.glDispatchCompute(groupCount(width), groupCount(rowCount), 1)
|
||||
GLES31.glMemoryBarrier(GLES31.GL_SHADER_STORAGE_BARRIER_BIT)
|
||||
|
||||
runRcdSteps(rowCount, label)
|
||||
runRcdSteps(firstRow, rowCount, label)
|
||||
}
|
||||
|
||||
private fun runRcdSteps(rowCount: Int, label: String) {
|
||||
private fun runRcdSteps(firstRow: Int, rowCount: Int, label: String) {
|
||||
require(firstRow % 2 == 0) { "RCD stripe must preserve CFA row parity: $firstRow" }
|
||||
bindRcdStripeBuffers()
|
||||
|
||||
fun dispatch(program: Int, halfWidth: Boolean, step: String) {
|
||||
@@ -1235,7 +1251,29 @@ class GlesRawStacker(
|
||||
dispatch(rcdStripeStep42Program, true, "step42")
|
||||
GLES31.glBindBufferBase(GLES31.GL_SHADER_STORAGE_BUFFER, 4, rcdStripeBuffers[4])
|
||||
dispatch(rcdStripeStep43Program, true, "step43")
|
||||
|
||||
GLES31.glUseProgram(rcdStripeBorderPpgProgram)
|
||||
GLES31.glUniform2i(
|
||||
uniformLocation(rcdStripeBorderPpgProgram, "uStripeSize"),
|
||||
width,
|
||||
rowCount,
|
||||
)
|
||||
GLES31.glUniform2i(
|
||||
uniformLocation(rcdStripeBorderPpgProgram, "uFullImageSize"),
|
||||
width,
|
||||
height,
|
||||
)
|
||||
GLES31.glUniform1i(
|
||||
uniformLocation(rcdStripeBorderPpgProgram, "uGlobalRowOffset"),
|
||||
firstRow,
|
||||
)
|
||||
GLES31.glUniform1i(
|
||||
uniformLocation(rcdStripeBorderPpgProgram, "uCfaPattern"),
|
||||
cfaPattern,
|
||||
)
|
||||
GLES31.glDispatchCompute(groupCount(width), groupCount(rowCount), 1)
|
||||
GLES31.glMemoryBarrier(GLES31.GL_SHADER_STORAGE_BARRIER_BIT)
|
||||
checkGlError("RCD stripe border PPG $label")
|
||||
}
|
||||
|
||||
private fun validExposureProduct(exposureProduct: Double): Double {
|
||||
@@ -3464,23 +3502,23 @@ class GlesRawStacker(
|
||||
referenceExposureScale: Float,
|
||||
) {
|
||||
bindRcdStripeBuffers()
|
||||
GLES31.glUseProgram(hdrRcdNormalPopulateProgram)
|
||||
bindTexture(hdrRcdNormalPopulateProgram, "uAccumulatorValueWeight", 0, accumulatorValueWeightTexture)
|
||||
bindTexture(hdrRcdNormalPopulateProgram, "uReferenceRaw", 1, refRaw)
|
||||
bindTexture(hdrRcdNormalPopulateProgram, "uLensShadingMap", 2, lensShadingTexture)
|
||||
setCommonUniforms(hdrRcdNormalPopulateProgram)
|
||||
GLES31.glUniform2i(uniformLocation(hdrRcdNormalPopulateProgram, "uImageSize"), width, height)
|
||||
GLES31.glUniform2i(uniformLocation(hdrRcdNormalPopulateProgram, "uStripeSize"), width, sourceRowCount)
|
||||
GLES31.glUseProgram(hdrNormalCfaInputProgram)
|
||||
bindTexture(hdrNormalCfaInputProgram, "uAccumulatorValueWeight", 0, accumulatorValueWeightTexture)
|
||||
bindTexture(hdrNormalCfaInputProgram, "uReferenceRaw", 1, refRaw)
|
||||
bindTexture(hdrNormalCfaInputProgram, "uLensShadingMap", 2, lensShadingTexture)
|
||||
setCommonUniforms(hdrNormalCfaInputProgram)
|
||||
GLES31.glUniform2i(uniformLocation(hdrNormalCfaInputProgram, "uImageSize"), width, height)
|
||||
GLES31.glUniform2i(uniformLocation(hdrNormalCfaInputProgram, "uStripeSize"), width, sourceRowCount)
|
||||
GLES31.glUniform1i(
|
||||
uniformLocation(hdrRcdNormalPopulateProgram, "uSourceRowOffset"),
|
||||
uniformLocation(hdrNormalCfaInputProgram, "uSourceRowOffset"),
|
||||
sourceRowOffset,
|
||||
)
|
||||
GLES31.glUniform1f(
|
||||
uniformLocation(hdrRcdNormalPopulateProgram, "uReferenceExposureScale"),
|
||||
uniformLocation(hdrNormalCfaInputProgram, "uReferenceExposureScale"),
|
||||
referenceExposureScale,
|
||||
)
|
||||
GLES31.glUniform4fv(
|
||||
uniformLocation(hdrRcdNormalPopulateProgram, "uCalculationWbGains"),
|
||||
uniformLocation(hdrNormalCfaInputProgram, "uCalculationWbGains"),
|
||||
1,
|
||||
demosaicCalculationWbGains,
|
||||
0,
|
||||
@@ -3497,22 +3535,22 @@ class GlesRawStacker(
|
||||
exposureScale: Float = 1f,
|
||||
desaturateBeforeExposureScale: Boolean = false,
|
||||
) {
|
||||
GLES31.glUseProgram(hdrRcdStoreRgbProgram)
|
||||
GLES31.glUseProgram(hdrWorkingRgbStoreProgram)
|
||||
GLES31.glBindBufferBase(GLES31.GL_SHADER_STORAGE_BUFFER, 1, rcdStripeBuffers[1])
|
||||
GLES31.glBindBufferBase(GLES31.GL_SHADER_STORAGE_BUFFER, 2, rcdStripeBuffers[2])
|
||||
GLES31.glBindBufferBase(GLES31.GL_SHADER_STORAGE_BUFFER, 3, rcdStripeBuffers[3])
|
||||
bindImage(0, targetTexture, GLES31.GL_WRITE_ONLY, GLES30.GL_RGBA16F)
|
||||
GLES31.glUniform2i(uniformLocation(hdrRcdStoreRgbProgram, "uSourceSize"), width, rowCount)
|
||||
GLES31.glUniform2i(uniformLocation(hdrWorkingRgbStoreProgram, "uSourceSize"), width, rowCount)
|
||||
GLES31.glUniform1f(
|
||||
uniformLocation(hdrRcdStoreRgbProgram, "uExposureScale"),
|
||||
uniformLocation(hdrWorkingRgbStoreProgram, "uExposureScale"),
|
||||
exposureScale,
|
||||
)
|
||||
GLES31.glUniform1i(
|
||||
uniformLocation(hdrRcdStoreRgbProgram, "uDesaturateBeforeExposureScale"),
|
||||
uniformLocation(hdrWorkingRgbStoreProgram, "uDesaturateBeforeExposureScale"),
|
||||
if (desaturateBeforeExposureScale) 1 else 0,
|
||||
)
|
||||
GLES31.glUniform3f(
|
||||
uniformLocation(hdrRcdStoreRgbProgram, "uCalculationGains"),
|
||||
uniformLocation(hdrWorkingRgbStoreProgram, "uCalculationGains"),
|
||||
demosaicCalculationWbGains[0],
|
||||
1f,
|
||||
demosaicCalculationWbGains[3],
|
||||
@@ -3589,7 +3627,7 @@ class GlesRawStacker(
|
||||
sourceRowCount = normalBand.rowCount,
|
||||
referenceExposureScale = referenceExposureScale,
|
||||
)
|
||||
runRcdSteps(normalBand.rowCount, "HDR normal")
|
||||
runRcdSteps(normalBand.firstRow, normalBand.rowCount, "HDR normal")
|
||||
storeHdrRcdRgbStripe(hdrNormalRgbStripeTexture, normalBand.rowCount, "HDR normal RGB store")
|
||||
|
||||
uploadRcdRawStripe(referenceImage, normalBand.firstRow, normalBand.rowCount, "HDR reference")
|
||||
@@ -7499,108 +7537,6 @@ class GlesRawStacker(
|
||||
}
|
||||
""".trimIndent()
|
||||
|
||||
private val RCD_STRIPE_POPULATE_COMPUTE_SHADER = """
|
||||
#version 310 es
|
||||
$RAW_COMMON
|
||||
layout(local_size_x = 16, local_size_y = 16) in;
|
||||
uniform highp usampler2D uRawStripe;
|
||||
uniform sampler2D uLensShadingMap;
|
||||
uniform ivec2 uStripeSize;
|
||||
uniform ivec2 uFullImageSize;
|
||||
uniform int uGlobalRowOffset;
|
||||
uniform int uCfaPattern;
|
||||
uniform vec4 uBlackLevel;
|
||||
uniform float uWhiteLevel;
|
||||
uniform vec4 uCalculationWbGains;
|
||||
uniform int uReconstructHighlights;
|
||||
layout(std430, binding = 0) buffer CFA_Buf { float cfa[]; };
|
||||
layout(std430, binding = 1) buffer RGB0_Buf { float rgb0[]; };
|
||||
layout(std430, binding = 2) buffer RGB1_Buf { float rgb1[]; };
|
||||
layout(std430, binding = 3) buffer RGB2_Buf { float rgb2[]; };
|
||||
|
||||
const float HIGHLIGHT_CLIP_THRESHOLD = 0.985;
|
||||
const float HIGHLIGHT_CEILING = 8.0;
|
||||
|
||||
ivec2 clampLocal(ivec2 p) {
|
||||
return clamp(p, ivec2(0), uStripeSize - ivec2(1));
|
||||
}
|
||||
|
||||
ivec2 globalAt(ivec2 local) {
|
||||
local = clampLocal(local);
|
||||
return ivec2(local.x, local.y + uGlobalRowOffset);
|
||||
}
|
||||
|
||||
float sensorNormalizedAt(ivec2 local) {
|
||||
local = clampLocal(local);
|
||||
ivec2 global = globalAt(local);
|
||||
int channel = bayerIndexAt(uCfaPattern, global);
|
||||
float raw = float(texelFetch(uRawStripe, local, 0).r);
|
||||
float range = max(uWhiteLevel - uBlackLevel[channel], 1.0);
|
||||
return max(raw - uBlackLevel[channel], 0.0) / range;
|
||||
}
|
||||
|
||||
float calculationSampleAt(ivec2 local) {
|
||||
local = clampLocal(local);
|
||||
ivec2 global = globalAt(local);
|
||||
int channel = bayerIndexAt(uCfaPattern, global);
|
||||
vec2 uv = (vec2(global) + vec2(0.5)) / vec2(uFullImageSize);
|
||||
vec4 lsc = texture(uLensShadingMap, clamp(uv, vec2(0.0), vec2(1.0)));
|
||||
int lscChannel = lensShadingChannelAt(uCfaPattern, global);
|
||||
float linear = sensorNormalizedAt(local) * lsc[lscChannel] *
|
||||
max(uCalculationWbGains[channel], 1e-6);
|
||||
return clamp(linear, 0.0, HIGHLIGHT_CEILING);
|
||||
}
|
||||
|
||||
int colorAt(ivec2 local) {
|
||||
int channel = bayerIndexAt(uCfaPattern, globalAt(local));
|
||||
return channel == 0 ? 0 : (channel == 3 ? 2 : 1);
|
||||
}
|
||||
|
||||
float estimateOpposedLinear(ivec2 local, int ownColor, float fallback) {
|
||||
vec3 sum = vec3(0.0);
|
||||
vec3 count = vec3(0.0);
|
||||
for (int y = -1; y <= 1; ++y) {
|
||||
for (int x = -1; x <= 1; ++x) {
|
||||
ivec2 sampleLocal = clampLocal(local + ivec2(x, y));
|
||||
int sampleColor = colorAt(sampleLocal);
|
||||
float value = calculationSampleAt(sampleLocal);
|
||||
sum[sampleColor] += value;
|
||||
count[sampleColor] += 1.0;
|
||||
}
|
||||
}
|
||||
const float power = 3.0;
|
||||
vec3 roots = pow(max(sum / max(count, vec3(1.0)), vec3(0.0)),
|
||||
vec3(1.0 / power));
|
||||
float opposedRoot = ownColor == 0 ? 0.5 * (roots.g + roots.b) :
|
||||
(ownColor == 1 ? 0.5 * (roots.r + roots.b) :
|
||||
0.5 * (roots.r + roots.g));
|
||||
return max(pow(max(opposedRoot, 0.0), power), fallback);
|
||||
}
|
||||
|
||||
void main() {
|
||||
ivec2 local = ivec2(gl_GlobalInvocationID.xy);
|
||||
if (local.x >= uStripeSize.x || local.y >= uStripeSize.y) return;
|
||||
ivec2 global = ivec2(local.x, local.y + uGlobalRowOffset);
|
||||
int channel = bayerIndexAt(uCfaPattern, global);
|
||||
float sensor = sensorNormalizedAt(local);
|
||||
float value = calculationSampleAt(local);
|
||||
float clipMask = smoothstep(HIGHLIGHT_CLIP_THRESHOLD, 1.0, sensor);
|
||||
if (uReconstructHighlights != 0 && clipMask > 0.0) {
|
||||
float reconstructed = estimateOpposedLinear(
|
||||
local,
|
||||
colorAt(local),
|
||||
value
|
||||
);
|
||||
value = min(mix(value, reconstructed, clipMask), HIGHLIGHT_CEILING);
|
||||
}
|
||||
int index = local.y * uStripeSize.x + local.x;
|
||||
cfa[index] = value;
|
||||
rgb0[index] = channel == 0 ? value : 0.0;
|
||||
rgb1[index] = (channel == 1 || channel == 2) ? value : 0.0;
|
||||
rgb2[index] = channel == 3 ? value : 0.0;
|
||||
}
|
||||
""".trimIndent()
|
||||
|
||||
private val PGTM_STATS_COMPUTE_SHADER = """
|
||||
#version 310 es
|
||||
$RAW_COMMON
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.hinnka.mycamera.processor
|
||||
|
||||
import com.hinnka.mycamera.raw.RcdShaders
|
||||
import kotlin.math.ceil
|
||||
import kotlin.math.floor
|
||||
import kotlin.math.max
|
||||
@@ -36,7 +37,7 @@ internal object RawSuperResolutionStripePlanner {
|
||||
val safeMaxFlow = maxFlowYPlanePx.takeIf { it.isFinite() } ?: 0f
|
||||
val minFlow = min(safeMinFlow, safeMaxFlow) * 2f
|
||||
val maxFlow = max(safeMinFlow, safeMaxFlow) * 2f
|
||||
val gatherMargin = cfaPeriod.coerceAtLeast(2) + RCD_HALO_ROWS
|
||||
val gatherMargin = cfaPeriod.coerceAtLeast(2) + RcdShaders.STRIPE_HALO_ROWS
|
||||
var first = floor(firstReferenceRaw + minFlow - gatherMargin).toInt().coerceIn(0, rawHeight - 1)
|
||||
// RCD shaders evaluate CFA parity in stripe-local coordinates. Preserve the global phase.
|
||||
if ((first and 1) != 0) first = (first - 1).coerceAtLeast(0)
|
||||
@@ -45,6 +46,4 @@ internal object RawSuperResolutionStripePlanner {
|
||||
.coerceIn(first + 1, rawHeight)
|
||||
return RawSourceRowBand(firstRow = first, rowCount = endExclusive - first)
|
||||
}
|
||||
|
||||
private const val RCD_HALO_ROWS = 10
|
||||
}
|
||||
@@ -196,9 +196,6 @@ class RawDemosaicProcessor {
|
||||
private const val RCD_PQ_WRITE_BINDING = 5
|
||||
private const val RCD_PQ_READ_BINDING = 4
|
||||
private const val RCD_VH_DIR_BINDING = 4
|
||||
private const val RCD_OUTPUT_MARGIN = 9
|
||||
private const val RCD_HIGHLIGHT_RECONSTRUCTION_THRESHOLD = 0.985f
|
||||
private const val RCD_HIGHLIGHT_RECONSTRUCTION_CEILING = 8.0f
|
||||
private const val RCD_HIGHLIGHT_RECONSTRUCTION_MIN_WB_GAIN = 1e-3f
|
||||
private const val RCD_HIGHLIGHT_RECONSTRUCTION_MAX_WB_GAIN = 64.0f
|
||||
private const val FILMIC_GREY_SOURCE = 0.1845f
|
||||
@@ -1523,11 +1520,11 @@ class RawDemosaicProcessor {
|
||||
)
|
||||
GLES31.glUniform1f(
|
||||
GLES31.glGetUniformLocation(rcdPopulateProgram, "uHighlightClipThreshold"),
|
||||
RCD_HIGHLIGHT_RECONSTRUCTION_THRESHOLD
|
||||
RcdShaders.HIGHLIGHT_RECONSTRUCTION_THRESHOLD
|
||||
)
|
||||
GLES31.glUniform1f(
|
||||
GLES31.glGetUniformLocation(rcdPopulateProgram, "uHighlightCeiling"),
|
||||
RCD_HIGHLIGHT_RECONSTRUCTION_CEILING
|
||||
RcdShaders.HIGHLIGHT_RECONSTRUCTION_CEILING
|
||||
)
|
||||
GLES31.glUniform1i(
|
||||
GLES31.glGetUniformLocation(rcdPopulateProgram, "uHighlightReconstructionEnabled"),
|
||||
@@ -1543,8 +1540,8 @@ class RawDemosaicProcessor {
|
||||
"calculationWb=${calculationWbGains.contentToString()} " +
|
||||
"lsc=$lscSize " +
|
||||
"highlightReconstruction=$rawDomainHighlightReconstructionEnabled " +
|
||||
"highlightThreshold=$RCD_HIGHLIGHT_RECONSTRUCTION_THRESHOLD " +
|
||||
"highlightCeiling=$RCD_HIGHLIGHT_RECONSTRUCTION_CEILING " +
|
||||
"highlightThreshold=${RcdShaders.HIGHLIGHT_RECONSTRUCTION_THRESHOLD} " +
|
||||
"highlightCeiling=${RcdShaders.HIGHLIGHT_RECONSTRUCTION_CEILING} " +
|
||||
"blacks=${rawBlackPointCorrection.coerceIn(-1f, 1f)} " +
|
||||
"whites=${rawWhitePointCorrection.coerceIn(-1f, 1f)}"
|
||||
)
|
||||
@@ -1677,10 +1674,6 @@ class RawDemosaicProcessor {
|
||||
GLES31.glGetUniformLocation(rcdWriteOutputProgram, "uCfaPattern"),
|
||||
actualMetadata.cfaPattern
|
||||
)
|
||||
GLES31.glUniform1i(
|
||||
GLES31.glGetUniformLocation(rcdWriteOutputProgram, "uBorder"),
|
||||
RCD_OUTPUT_MARGIN
|
||||
)
|
||||
GLES31.glUniform3f(
|
||||
GLES31.glGetUniformLocation(rcdWriteOutputProgram, "uCalculationGains"),
|
||||
calculationWbGains[0],
|
||||
@@ -4563,11 +4556,11 @@ class RawDemosaicProcessor {
|
||||
GLES31.glUniform1f(GLES31.glGetUniformLocation(rcdPopulateProgram, "uWhiteLevel"), metadata.whiteLevel)
|
||||
GLES31.glUniform1f(
|
||||
GLES31.glGetUniformLocation(rcdPopulateProgram, "uHighlightClipThreshold"),
|
||||
RCD_HIGHLIGHT_RECONSTRUCTION_THRESHOLD
|
||||
RcdShaders.HIGHLIGHT_RECONSTRUCTION_THRESHOLD
|
||||
)
|
||||
GLES31.glUniform1f(
|
||||
GLES31.glGetUniformLocation(rcdPopulateProgram, "uHighlightCeiling"),
|
||||
RCD_HIGHLIGHT_RECONSTRUCTION_CEILING
|
||||
RcdShaders.HIGHLIGHT_RECONSTRUCTION_CEILING
|
||||
)
|
||||
GLES31.glUniform1i(
|
||||
GLES31.glGetUniformLocation(rcdPopulateProgram, "uHighlightReconstructionEnabled"),
|
||||
@@ -4642,7 +4635,6 @@ class RawDemosaicProcessor {
|
||||
GLES31.glUseProgram(rcdWriteOutputProgram)
|
||||
GLES31.glUniform2i(GLES31.glGetUniformLocation(rcdWriteOutputProgram, "uImageSize"), width, height)
|
||||
GLES31.glUniform1i(GLES31.glGetUniformLocation(rcdWriteOutputProgram, "uCfaPattern"), metadata.cfaPattern)
|
||||
GLES31.glUniform1i(GLES31.glGetUniformLocation(rcdWriteOutputProgram, "uBorder"), RCD_OUTPUT_MARGIN)
|
||||
GLES31.glUniform3f(
|
||||
GLES31.glGetUniformLocation(rcdWriteOutputProgram, "uCalculationGains"),
|
||||
calculationWbGains[0],
|
||||
@@ -4740,11 +4732,11 @@ class RawDemosaicProcessor {
|
||||
)
|
||||
GLES31.glUniform1f(
|
||||
GLES31.glGetUniformLocation(quadPopulateProgram, "uHighlightClipThreshold"),
|
||||
RCD_HIGHLIGHT_RECONSTRUCTION_THRESHOLD
|
||||
RcdShaders.HIGHLIGHT_RECONSTRUCTION_THRESHOLD
|
||||
)
|
||||
GLES31.glUniform1f(
|
||||
GLES31.glGetUniformLocation(quadPopulateProgram, "uHighlightCeiling"),
|
||||
RCD_HIGHLIGHT_RECONSTRUCTION_CEILING
|
||||
RcdShaders.HIGHLIGHT_RECONSTRUCTION_CEILING
|
||||
)
|
||||
GLES31.glUniform1i(
|
||||
GLES31.glGetUniformLocation(quadPopulateProgram, "uHighlightReconstructionEnabled"),
|
||||
@@ -4763,8 +4755,8 @@ class RawDemosaicProcessor {
|
||||
"white=${metadata.whiteLevel} metadataWb=${metadataWbGains.contentToString()} " +
|
||||
"calculationWb=${calculationWbGains.contentToString()} lsc=$lscSize " +
|
||||
"highlightReconstruction=$highlightReconstructionEnabled " +
|
||||
"highlightThreshold=$RCD_HIGHLIGHT_RECONSTRUCTION_THRESHOLD " +
|
||||
"highlightCeiling=$RCD_HIGHLIGHT_RECONSTRUCTION_CEILING"
|
||||
"highlightThreshold=${RcdShaders.HIGHLIGHT_RECONSTRUCTION_THRESHOLD} " +
|
||||
"highlightCeiling=${RcdShaders.HIGHLIGHT_RECONSTRUCTION_CEILING}"
|
||||
)
|
||||
GLES31.glDispatchCompute((width + 15) / 16, (height + 15) / 16, 1)
|
||||
GLES31.glMemoryBarrier(GLES31.GL_SHADER_STORAGE_BARRIER_BIT)
|
||||
|
||||
@@ -9,6 +9,139 @@ package com.hinnka.mycamera.raw
|
||||
*/
|
||||
object RcdShaders {
|
||||
|
||||
/** RCD 主体算子不写入可靠结果的照片外圈宽度。 */
|
||||
const val OUTPUT_MARGIN = 9
|
||||
|
||||
/** PPG 最深的递归邻域半径,用于验证条带是否包含完整输入。 */
|
||||
const val PPG_RADIUS = 4
|
||||
|
||||
/** 条带调用方在输出采样范围之外至少保留的 RCD 输入行数。 */
|
||||
const val STRIPE_HALO_ROWS = OUTPUT_MARGIN + 1
|
||||
|
||||
const val HIGHLIGHT_RECONSTRUCTION_THRESHOLD = 0.985f
|
||||
const val HIGHLIGHT_RECONSTRUCTION_CEILING = 8.0f
|
||||
|
||||
/**
|
||||
* RCD 四周统一使用的 PPG 内核。
|
||||
*
|
||||
* 嵌入该内核的 shader 必须先提供 `rawAt(ivec2)` 与 `colorAt(ivec2)`;两者负责
|
||||
* 把虚拟照片坐标映射到实际 CFA 存储。这样完整图与条带图共享同一份 PPG 数学实现,
|
||||
* 只有坐标映射不同。
|
||||
*/
|
||||
private val PPG_KERNEL = """
|
||||
float ppgGreenAt(ivec2 coord) {
|
||||
// Keep the virtual coordinate so recursive PPG estimates use the neighbourhood
|
||||
// around that reflected photosite, rather than shifting the kernel into the image.
|
||||
ivec2 center = coord;
|
||||
|
||||
int ownColor = colorAt(center);
|
||||
float pc = rawAt(center);
|
||||
if (ownColor == GREEN) {
|
||||
return max(0.0, pc);
|
||||
}
|
||||
|
||||
float pym = rawAt(center + ivec2(0, -1));
|
||||
float pym2 = rawAt(center + ivec2(0, -2));
|
||||
float pym3 = rawAt(center + ivec2(0, -3));
|
||||
float pyM = rawAt(center + ivec2(0, 1));
|
||||
float pyM2 = rawAt(center + ivec2(0, 2));
|
||||
float pyM3 = rawAt(center + ivec2(0, 3));
|
||||
float pxm = rawAt(center + ivec2(-1, 0));
|
||||
float pxm2 = rawAt(center + ivec2(-2, 0));
|
||||
float pxm3 = rawAt(center + ivec2(-3, 0));
|
||||
float pxM = rawAt(center + ivec2(1, 0));
|
||||
float pxM2 = rawAt(center + ivec2(2, 0));
|
||||
float pxM3 = rawAt(center + ivec2(3, 0));
|
||||
|
||||
float guessx = (pxm + pc + pxM) * 2.0 - pxM2 - pxm2;
|
||||
float diffx = (abs(pxm2 - pc) + abs(pxM2 - pc) + abs(pxm - pxM)) * 3.0 +
|
||||
(abs(pxM3 - pxM) + abs(pxm3 - pxm)) * 2.0;
|
||||
float guessy = (pym + pc + pyM) * 2.0 - pyM2 - pym2;
|
||||
float diffy = (abs(pym2 - pc) + abs(pyM2 - pc) + abs(pym - pyM)) * 3.0 +
|
||||
(abs(pyM3 - pyM) + abs(pym3 - pym)) * 2.0;
|
||||
|
||||
float green;
|
||||
if (diffx > diffy) {
|
||||
green = clamp(guessy * 0.25, min(pym, pyM), max(pym, pyM));
|
||||
} else {
|
||||
green = clamp(guessx * 0.25, min(pxm, pxM), max(pxm, pxM));
|
||||
}
|
||||
return max(0.0, green);
|
||||
}
|
||||
|
||||
vec3 ppgColorAt(ivec2 coord) {
|
||||
ivec2 center = coord;
|
||||
|
||||
int ownColor = colorAt(center);
|
||||
float pc = max(0.0, rawAt(center));
|
||||
float green = ppgGreenAt(center);
|
||||
vec3 color = vec3(0.0, green, 0.0);
|
||||
|
||||
if (ownColor == RED) {
|
||||
color.r = pc;
|
||||
ivec2 nw = center + ivec2(-1, -1);
|
||||
ivec2 ne = center + ivec2(1, -1);
|
||||
ivec2 sw = center + ivec2(-1, 1);
|
||||
ivec2 se = center + ivec2(1, 1);
|
||||
float diff1 = abs(rawAt(nw) - rawAt(se)) +
|
||||
abs(ppgGreenAt(nw) - green) + abs(ppgGreenAt(se) - green);
|
||||
float guess1 = rawAt(nw) + rawAt(se) + 2.0 * green -
|
||||
ppgGreenAt(nw) - ppgGreenAt(se);
|
||||
float diff2 = abs(rawAt(ne) - rawAt(sw)) +
|
||||
abs(ppgGreenAt(ne) - green) + abs(ppgGreenAt(sw) - green);
|
||||
float guess2 = rawAt(ne) + rawAt(sw) + 2.0 * green -
|
||||
ppgGreenAt(ne) - ppgGreenAt(sw);
|
||||
if (diff1 > diff2) {
|
||||
color.b = guess2 * 0.5;
|
||||
} else if (diff1 < diff2) {
|
||||
color.b = guess1 * 0.5;
|
||||
} else {
|
||||
color.b = (guess1 + guess2) * 0.25;
|
||||
}
|
||||
} else if (ownColor == BLUE) {
|
||||
color.b = pc;
|
||||
ivec2 nw = center + ivec2(-1, -1);
|
||||
ivec2 ne = center + ivec2(1, -1);
|
||||
ivec2 sw = center + ivec2(-1, 1);
|
||||
ivec2 se = center + ivec2(1, 1);
|
||||
float diff1 = abs(rawAt(nw) - rawAt(se)) +
|
||||
abs(ppgGreenAt(nw) - green) + abs(ppgGreenAt(se) - green);
|
||||
float guess1 = rawAt(nw) + rawAt(se) + 2.0 * green -
|
||||
ppgGreenAt(nw) - ppgGreenAt(se);
|
||||
float diff2 = abs(rawAt(ne) - rawAt(sw)) +
|
||||
abs(ppgGreenAt(ne) - green) + abs(ppgGreenAt(sw) - green);
|
||||
float guess2 = rawAt(ne) + rawAt(sw) + 2.0 * green -
|
||||
ppgGreenAt(ne) - ppgGreenAt(sw);
|
||||
if (diff1 > diff2) {
|
||||
color.r = guess2 * 0.5;
|
||||
} else if (diff1 < diff2) {
|
||||
color.r = guess1 * 0.5;
|
||||
} else {
|
||||
color.r = (guess1 + guess2) * 0.25;
|
||||
}
|
||||
} else {
|
||||
color.g = pc;
|
||||
if (colorAt(center + ivec2(1, 0)) == RED) {
|
||||
color.b = (rawAt(center + ivec2(0, -1)) + rawAt(center + ivec2(0, 1)) +
|
||||
2.0 * color.g - ppgGreenAt(center + ivec2(0, -1)) -
|
||||
ppgGreenAt(center + ivec2(0, 1))) * 0.5;
|
||||
color.r = (rawAt(center + ivec2(-1, 0)) + rawAt(center + ivec2(1, 0)) +
|
||||
2.0 * color.g - ppgGreenAt(center + ivec2(-1, 0)) -
|
||||
ppgGreenAt(center + ivec2(1, 0))) * 0.5;
|
||||
} else {
|
||||
color.r = (rawAt(center + ivec2(0, -1)) + rawAt(center + ivec2(0, 1)) +
|
||||
2.0 * color.g - ppgGreenAt(center + ivec2(0, -1)) -
|
||||
ppgGreenAt(center + ivec2(0, 1))) * 0.5;
|
||||
color.b = (rawAt(center + ivec2(-1, 0)) + rawAt(center + ivec2(1, 0)) +
|
||||
2.0 * color.g - ppgGreenAt(center + ivec2(-1, 0)) -
|
||||
ppgGreenAt(center + ivec2(1, 0))) * 0.5;
|
||||
}
|
||||
}
|
||||
|
||||
return max(color, vec3(0.0));
|
||||
}
|
||||
""".trimIndent()
|
||||
|
||||
/**
|
||||
* 1. 初始化与归一化片元导入 (rcd_populate.comp)
|
||||
*/
|
||||
@@ -709,7 +842,8 @@ object RcdShaders {
|
||||
""".trimIndent()
|
||||
|
||||
/**
|
||||
* 9. 合并 RGB 重建结果写出到 RGBA16F 纹理 (rcd_write_output.comp)
|
||||
* 9. 合并 RGB 重建结果写出到 RGBA16F 纹理 (rcd_write_output.comp)。
|
||||
* 照片真实外圈始终由这里内置的 PPG 处理,调用方不能关闭或遗漏。
|
||||
*/
|
||||
val WRITE_OUTPUT = """
|
||||
#version 310 es
|
||||
@@ -718,37 +852,35 @@ object RcdShaders {
|
||||
layout (local_size_x = 16, local_size_y = 16) in;
|
||||
|
||||
layout(std430, binding = 0) buffer CFA_Buf { float cfa[]; };
|
||||
layout(std430, binding = 1) buffer RGB0_Buf { float rgb0[]; }; // R
|
||||
layout(std430, binding = 2) buffer RGB1_Buf { float rgb1[]; }; // G
|
||||
layout(std430, binding = 3) buffer RGB2_Buf { float rgb2[]; }; // B
|
||||
|
||||
layout(std430, binding = 1) buffer RGB0_Buf { float rgb0[]; };
|
||||
layout(std430, binding = 2) buffer RGB1_Buf { float rgb1[]; };
|
||||
layout(std430, binding = 3) buffer RGB2_Buf { float rgb2[]; };
|
||||
layout (rgba16f, binding = 0) writeonly uniform highp image2D uOutputImage;
|
||||
|
||||
uniform ivec2 uImageSize;
|
||||
uniform int uCfaPattern;
|
||||
uniform int uBorder;
|
||||
uniform vec3 uCalculationGains;
|
||||
|
||||
#define RED 0
|
||||
#define GREEN 1
|
||||
#define BLUE 2
|
||||
const int RCD_OUTPUT_MARGIN = $OUTPUT_MARGIN;
|
||||
|
||||
int getBayerColor(int cfaPattern, int col, int row) {
|
||||
int r = row % 2;
|
||||
int c = col % 2;
|
||||
if (cfaPattern == 0) { // RGGB
|
||||
if (r == 0) return (c == 0) ? 0 : 1;
|
||||
else return (c == 0) ? 1 : 2;
|
||||
} else if (cfaPattern == 1) { // GRBG
|
||||
if (r == 0) return (c == 0) ? 1 : 0;
|
||||
else return (c == 0) ? 2 : 1;
|
||||
} else if (cfaPattern == 2) { // GBRG
|
||||
if (r == 0) return (c == 0) ? 1 : 2;
|
||||
else return (c == 0) ? 0 : 1;
|
||||
} else { // BGGR (3)
|
||||
if (r == 0) return (c == 0) ? 2 : 1;
|
||||
else return (c == 0) ? 1 : 0;
|
||||
if (cfaPattern == 0) {
|
||||
if (r == 0) return (c == 0) ? RED : GREEN;
|
||||
return (c == 0) ? GREEN : BLUE;
|
||||
} else if (cfaPattern == 1) {
|
||||
if (r == 0) return (c == 0) ? GREEN : RED;
|
||||
return (c == 0) ? BLUE : GREEN;
|
||||
} else if (cfaPattern == 2) {
|
||||
if (r == 0) return (c == 0) ? GREEN : BLUE;
|
||||
return (c == 0) ? RED : GREEN;
|
||||
}
|
||||
if (r == 0) return (c == 0) ? BLUE : GREEN;
|
||||
return (c == 0) ? GREEN : RED;
|
||||
}
|
||||
|
||||
int mirrorIndex(int value, int size) {
|
||||
@@ -759,9 +891,6 @@ object RcdShaders {
|
||||
return (wrapped < size) ? wrapped : period - wrapped;
|
||||
}
|
||||
|
||||
// Mirror at the photosite center instead of clamping to the last sample.
|
||||
// The reflection preserves x/y parity, so every synthetic neighbour keeps
|
||||
// the same Bayer color it would have had outside the image.
|
||||
ivec2 mirrorCoord(ivec2 coord) {
|
||||
return ivec2(
|
||||
mirrorIndex(coord.x, uImageSize.x),
|
||||
@@ -783,136 +912,228 @@ object RcdShaders {
|
||||
return cfa[indexAt(coord)];
|
||||
}
|
||||
|
||||
float ppgGreenAt(ivec2 coord) {
|
||||
// Keep the virtual coordinate so recursive PPG estimates use the neighbourhood
|
||||
// around that reflected photosite, rather than shifting the kernel into the image.
|
||||
ivec2 center = coord;
|
||||
|
||||
int ownColor = colorAt(center);
|
||||
float pc = rawAt(center);
|
||||
if (ownColor == GREEN) {
|
||||
return max(0.0, pc);
|
||||
}
|
||||
|
||||
float pym = rawAt(center + ivec2(0, -1));
|
||||
float pym2 = rawAt(center + ivec2(0, -2));
|
||||
float pym3 = rawAt(center + ivec2(0, -3));
|
||||
float pyM = rawAt(center + ivec2(0, 1));
|
||||
float pyM2 = rawAt(center + ivec2(0, 2));
|
||||
float pyM3 = rawAt(center + ivec2(0, 3));
|
||||
float pxm = rawAt(center + ivec2(-1, 0));
|
||||
float pxm2 = rawAt(center + ivec2(-2, 0));
|
||||
float pxm3 = rawAt(center + ivec2(-3, 0));
|
||||
float pxM = rawAt(center + ivec2(1, 0));
|
||||
float pxM2 = rawAt(center + ivec2(2, 0));
|
||||
float pxM3 = rawAt(center + ivec2(3, 0));
|
||||
|
||||
float guessx = (pxm + pc + pxM) * 2.0 - pxM2 - pxm2;
|
||||
float diffx = (abs(pxm2 - pc) + abs(pxM2 - pc) + abs(pxm - pxM)) * 3.0 +
|
||||
(abs(pxM3 - pxM) + abs(pxm3 - pxm)) * 2.0;
|
||||
float guessy = (pym + pc + pyM) * 2.0 - pyM2 - pym2;
|
||||
float diffy = (abs(pym2 - pc) + abs(pyM2 - pc) + abs(pym - pyM)) * 3.0 +
|
||||
(abs(pyM3 - pyM) + abs(pym3 - pym)) * 2.0;
|
||||
|
||||
float green;
|
||||
if (diffx > diffy) {
|
||||
green = clamp(guessy * 0.25, min(pym, pyM), max(pym, pyM));
|
||||
} else {
|
||||
green = clamp(guessx * 0.25, min(pxm, pxM), max(pxm, pxM));
|
||||
}
|
||||
return max(0.0, green);
|
||||
}
|
||||
|
||||
vec3 ppgColorAt(ivec2 coord) {
|
||||
ivec2 center = coord;
|
||||
|
||||
int ownColor = colorAt(center);
|
||||
float pc = max(0.0, rawAt(center));
|
||||
float green = ppgGreenAt(center);
|
||||
vec3 color = vec3(0.0, green, 0.0);
|
||||
|
||||
if (ownColor == RED) {
|
||||
color.r = pc;
|
||||
ivec2 nw = center + ivec2(-1, -1);
|
||||
ivec2 ne = center + ivec2(1, -1);
|
||||
ivec2 sw = center + ivec2(-1, 1);
|
||||
ivec2 se = center + ivec2(1, 1);
|
||||
float diff1 = abs(rawAt(nw) - rawAt(se)) +
|
||||
abs(ppgGreenAt(nw) - green) + abs(ppgGreenAt(se) - green);
|
||||
float guess1 = rawAt(nw) + rawAt(se) + 2.0 * green -
|
||||
ppgGreenAt(nw) - ppgGreenAt(se);
|
||||
float diff2 = abs(rawAt(ne) - rawAt(sw)) +
|
||||
abs(ppgGreenAt(ne) - green) + abs(ppgGreenAt(sw) - green);
|
||||
float guess2 = rawAt(ne) + rawAt(sw) + 2.0 * green -
|
||||
ppgGreenAt(ne) - ppgGreenAt(sw);
|
||||
if (diff1 > diff2) {
|
||||
color.b = guess2 * 0.5;
|
||||
} else if (diff1 < diff2) {
|
||||
color.b = guess1 * 0.5;
|
||||
} else {
|
||||
color.b = (guess1 + guess2) * 0.25;
|
||||
}
|
||||
} else if (ownColor == BLUE) {
|
||||
color.b = pc;
|
||||
ivec2 nw = center + ivec2(-1, -1);
|
||||
ivec2 ne = center + ivec2(1, -1);
|
||||
ivec2 sw = center + ivec2(-1, 1);
|
||||
ivec2 se = center + ivec2(1, 1);
|
||||
float diff1 = abs(rawAt(nw) - rawAt(se)) +
|
||||
abs(ppgGreenAt(nw) - green) + abs(ppgGreenAt(se) - green);
|
||||
float guess1 = rawAt(nw) + rawAt(se) + 2.0 * green -
|
||||
ppgGreenAt(nw) - ppgGreenAt(se);
|
||||
float diff2 = abs(rawAt(ne) - rawAt(sw)) +
|
||||
abs(ppgGreenAt(ne) - green) + abs(ppgGreenAt(sw) - green);
|
||||
float guess2 = rawAt(ne) + rawAt(sw) + 2.0 * green -
|
||||
ppgGreenAt(ne) - ppgGreenAt(sw);
|
||||
if (diff1 > diff2) {
|
||||
color.r = guess2 * 0.5;
|
||||
} else if (diff1 < diff2) {
|
||||
color.r = guess1 * 0.5;
|
||||
} else {
|
||||
color.r = (guess1 + guess2) * 0.25;
|
||||
}
|
||||
} else {
|
||||
color.g = pc;
|
||||
if (colorAt(center + ivec2(1, 0)) == RED) {
|
||||
color.b = (rawAt(center + ivec2(0, -1)) + rawAt(center + ivec2(0, 1)) +
|
||||
2.0 * color.g - ppgGreenAt(center + ivec2(0, -1)) -
|
||||
ppgGreenAt(center + ivec2(0, 1))) * 0.5;
|
||||
color.r = (rawAt(center + ivec2(-1, 0)) + rawAt(center + ivec2(1, 0)) +
|
||||
2.0 * color.g - ppgGreenAt(center + ivec2(-1, 0)) -
|
||||
ppgGreenAt(center + ivec2(1, 0))) * 0.5;
|
||||
} else {
|
||||
color.r = (rawAt(center + ivec2(0, -1)) + rawAt(center + ivec2(0, 1)) +
|
||||
2.0 * color.g - ppgGreenAt(center + ivec2(0, -1)) -
|
||||
ppgGreenAt(center + ivec2(0, 1))) * 0.5;
|
||||
color.b = (rawAt(center + ivec2(-1, 0)) + rawAt(center + ivec2(1, 0)) +
|
||||
2.0 * color.g - ppgGreenAt(center + ivec2(-1, 0)) -
|
||||
ppgGreenAt(center + ivec2(1, 0))) * 0.5;
|
||||
}
|
||||
}
|
||||
|
||||
return max(color, vec3(0.0));
|
||||
}
|
||||
$PPG_KERNEL
|
||||
|
||||
void main() {
|
||||
ivec2 coord = ivec2(gl_GlobalInvocationID.xy);
|
||||
if (coord.x >= uImageSize.x || coord.y >= uImageSize.y) return;
|
||||
|
||||
vec3 color;
|
||||
|
||||
if (coord.x >= uBorder && coord.x < uImageSize.x - uBorder &&
|
||||
coord.y >= uBorder && coord.y < uImageSize.y - uBorder) {
|
||||
if (coord.x >= RCD_OUTPUT_MARGIN && coord.x < uImageSize.x - RCD_OUTPUT_MARGIN &&
|
||||
coord.y >= RCD_OUTPUT_MARGIN && coord.y < uImageSize.y - RCD_OUTPUT_MARGIN) {
|
||||
int idx = coord.y * uImageSize.x + coord.x;
|
||||
color = vec3(max(0.0, rgb0[idx]), max(0.0, rgb1[idx]), max(0.0, rgb2[idx]));
|
||||
color = max(vec3(rgb0[idx], rgb1[idx], rgb2[idx]), vec3(0.0));
|
||||
} else {
|
||||
color = ppgColorAt(coord);
|
||||
}
|
||||
|
||||
// Undo the calculation-only white balance. Downstream color conversion
|
||||
// therefore receives the same camera RGB domain as before this RCD pass.
|
||||
color /= max(uCalculationGains, vec3(1e-6));
|
||||
imageStore(uOutputImage, coord, vec4(color, 1.0));
|
||||
}
|
||||
""".trimIndent()
|
||||
|
||||
/**
|
||||
* HDR/MFSR 条带 RCD 的统一照片边界收尾。
|
||||
*
|
||||
* 仅覆盖照片的真实外圈,不把条带边界当作照片边界。位于内部条带 halo 外缘、邻域尚未
|
||||
* 上传完整的行会跳过;它们不属于该条带的有效采样域,并会在相邻条带拥有完整邻域时处理。
|
||||
*/
|
||||
val STRIPE_BORDER_PPG = """
|
||||
#version 310 es
|
||||
precision highp float;
|
||||
precision highp int;
|
||||
layout (local_size_x = 16, local_size_y = 16) in;
|
||||
|
||||
layout(std430, binding = 0) readonly buffer CFA_Buf { float cfa[]; };
|
||||
layout(std430, binding = 1) buffer RGB0_Buf { float rgb0[]; };
|
||||
layout(std430, binding = 2) buffer RGB1_Buf { float rgb1[]; };
|
||||
layout(std430, binding = 3) buffer RGB2_Buf { float rgb2[]; };
|
||||
|
||||
uniform ivec2 uStripeSize;
|
||||
uniform ivec2 uFullImageSize;
|
||||
uniform int uGlobalRowOffset;
|
||||
uniform int uCfaPattern;
|
||||
|
||||
#define RED 0
|
||||
#define GREEN 1
|
||||
#define BLUE 2
|
||||
const int RCD_OUTPUT_MARGIN = $OUTPUT_MARGIN;
|
||||
const int PPG_RADIUS = $PPG_RADIUS;
|
||||
|
||||
int getBayerColor(int cfaPattern, int col, int row) {
|
||||
int r = row % 2;
|
||||
int c = col % 2;
|
||||
if (cfaPattern == 0) {
|
||||
if (r == 0) return (c == 0) ? RED : GREEN;
|
||||
return (c == 0) ? GREEN : BLUE;
|
||||
} else if (cfaPattern == 1) {
|
||||
if (r == 0) return (c == 0) ? GREEN : RED;
|
||||
return (c == 0) ? BLUE : GREEN;
|
||||
} else if (cfaPattern == 2) {
|
||||
if (r == 0) return (c == 0) ? GREEN : BLUE;
|
||||
return (c == 0) ? RED : GREEN;
|
||||
}
|
||||
if (r == 0) return (c == 0) ? BLUE : GREEN;
|
||||
return (c == 0) ? GREEN : RED;
|
||||
}
|
||||
|
||||
int mirrorIndex(int value, int size) {
|
||||
if (size <= 1) return 0;
|
||||
int period = 2 * (size - 1);
|
||||
int wrapped = value % period;
|
||||
if (wrapped < 0) wrapped += period;
|
||||
return (wrapped < size) ? wrapped : period - wrapped;
|
||||
}
|
||||
|
||||
ivec2 mirrorCoord(ivec2 coord) {
|
||||
return ivec2(
|
||||
mirrorIndex(coord.x, uFullImageSize.x),
|
||||
mirrorIndex(coord.y, uFullImageSize.y)
|
||||
);
|
||||
}
|
||||
|
||||
int indexAt(ivec2 coord) {
|
||||
ivec2 safe = mirrorCoord(coord);
|
||||
int localY = safe.y - uGlobalRowOffset;
|
||||
return localY * uStripeSize.x + safe.x;
|
||||
}
|
||||
|
||||
int colorAt(ivec2 coord) {
|
||||
ivec2 safe = mirrorCoord(coord);
|
||||
return getBayerColor(uCfaPattern, safe.x, safe.y);
|
||||
}
|
||||
|
||||
float rawAt(ivec2 coord) {
|
||||
return cfa[indexAt(coord)];
|
||||
}
|
||||
|
||||
$PPG_KERNEL
|
||||
|
||||
void main() {
|
||||
ivec2 local = ivec2(gl_GlobalInvocationID.xy);
|
||||
if (local.x >= uStripeSize.x || local.y >= uStripeSize.y) return;
|
||||
ivec2 global = ivec2(local.x, local.y + uGlobalRowOffset);
|
||||
|
||||
bool horizontalBorder = global.x < RCD_OUTPUT_MARGIN ||
|
||||
global.x >= uFullImageSize.x - RCD_OUTPUT_MARGIN;
|
||||
bool verticalBorder = global.y < RCD_OUTPUT_MARGIN ||
|
||||
global.y >= uFullImageSize.y - RCD_OUTPUT_MARGIN;
|
||||
if (!horizontalBorder && !verticalBorder) return;
|
||||
|
||||
int minRequiredY = uFullImageSize.y - 1;
|
||||
int maxRequiredY = 0;
|
||||
for (int dy = -PPG_RADIUS; dy <= PPG_RADIUS; ++dy) {
|
||||
int sampleY = mirrorIndex(global.y + dy, uFullImageSize.y);
|
||||
minRequiredY = min(minRequiredY, sampleY);
|
||||
maxRequiredY = max(maxRequiredY, sampleY);
|
||||
}
|
||||
int stripeEnd = uGlobalRowOffset + uStripeSize.y;
|
||||
if (minRequiredY < uGlobalRowOffset || maxRequiredY >= stripeEnd) return;
|
||||
|
||||
vec3 color = ppgColorAt(global);
|
||||
int index = local.y * uStripeSize.x + local.x;
|
||||
rgb0[index] = color.r;
|
||||
rgb1[index] = color.g;
|
||||
rgb2[index] = color.b;
|
||||
}
|
||||
""".trimIndent()
|
||||
|
||||
/** 原始 RAW 条带到 RCD CFA/RGB 工作缓冲的统一适配 shader。 */
|
||||
fun stripePopulate(rawCommon: String): String = """
|
||||
#version 310 es
|
||||
$rawCommon
|
||||
layout(local_size_x = 16, local_size_y = 16) in;
|
||||
uniform highp usampler2D uRawStripe;
|
||||
uniform sampler2D uLensShadingMap;
|
||||
uniform ivec2 uStripeSize;
|
||||
uniform ivec2 uFullImageSize;
|
||||
uniform int uGlobalRowOffset;
|
||||
uniform int uCfaPattern;
|
||||
uniform vec4 uBlackLevel;
|
||||
uniform float uWhiteLevel;
|
||||
uniform vec4 uCalculationWbGains;
|
||||
uniform int uReconstructHighlights;
|
||||
layout(std430, binding = 0) buffer CFA_Buf { float cfa[]; };
|
||||
layout(std430, binding = 1) buffer RGB0_Buf { float rgb0[]; };
|
||||
layout(std430, binding = 2) buffer RGB1_Buf { float rgb1[]; };
|
||||
layout(std430, binding = 3) buffer RGB2_Buf { float rgb2[]; };
|
||||
|
||||
const float HIGHLIGHT_CLIP_THRESHOLD = $HIGHLIGHT_RECONSTRUCTION_THRESHOLD;
|
||||
const float HIGHLIGHT_CEILING = $HIGHLIGHT_RECONSTRUCTION_CEILING;
|
||||
|
||||
ivec2 clampLocal(ivec2 p) {
|
||||
return clamp(p, ivec2(0), uStripeSize - ivec2(1));
|
||||
}
|
||||
|
||||
ivec2 globalAt(ivec2 local) {
|
||||
local = clampLocal(local);
|
||||
return ivec2(local.x, local.y + uGlobalRowOffset);
|
||||
}
|
||||
|
||||
float sensorNormalizedAt(ivec2 local) {
|
||||
local = clampLocal(local);
|
||||
ivec2 global = globalAt(local);
|
||||
int channel = bayerIndexAt(uCfaPattern, global);
|
||||
float raw = float(texelFetch(uRawStripe, local, 0).r);
|
||||
float range = max(uWhiteLevel - uBlackLevel[channel], 1.0);
|
||||
return max(raw - uBlackLevel[channel], 0.0) / range;
|
||||
}
|
||||
|
||||
float calculationSampleAt(ivec2 local) {
|
||||
local = clampLocal(local);
|
||||
ivec2 global = globalAt(local);
|
||||
int channel = bayerIndexAt(uCfaPattern, global);
|
||||
vec2 uv = (vec2(global) + vec2(0.5)) / vec2(uFullImageSize);
|
||||
vec4 lsc = texture(uLensShadingMap, clamp(uv, vec2(0.0), vec2(1.0)));
|
||||
int lscChannel = lensShadingChannelAt(uCfaPattern, global);
|
||||
float linear = sensorNormalizedAt(local) * lsc[lscChannel] *
|
||||
max(uCalculationWbGains[channel], 1e-6);
|
||||
return clamp(linear, 0.0, HIGHLIGHT_CEILING);
|
||||
}
|
||||
|
||||
int colorAt(ivec2 local) {
|
||||
int channel = bayerIndexAt(uCfaPattern, globalAt(local));
|
||||
return channel == 0 ? 0 : (channel == 3 ? 2 : 1);
|
||||
}
|
||||
|
||||
float estimateOpposedLinear(ivec2 local, int ownColor, float fallback) {
|
||||
vec3 sum = vec3(0.0);
|
||||
vec3 count = vec3(0.0);
|
||||
for (int y = -1; y <= 1; ++y) {
|
||||
for (int x = -1; x <= 1; ++x) {
|
||||
ivec2 sampleLocal = clampLocal(local + ivec2(x, y));
|
||||
int sampleColor = colorAt(sampleLocal);
|
||||
float value = calculationSampleAt(sampleLocal);
|
||||
sum[sampleColor] += value;
|
||||
count[sampleColor] += 1.0;
|
||||
}
|
||||
}
|
||||
const float power = 3.0;
|
||||
vec3 roots = pow(max(sum / max(count, vec3(1.0)), vec3(0.0)),
|
||||
vec3(1.0 / power));
|
||||
float opposedRoot = ownColor == 0 ? 0.5 * (roots.g + roots.b) :
|
||||
(ownColor == 1 ? 0.5 * (roots.r + roots.b) :
|
||||
0.5 * (roots.r + roots.g));
|
||||
return max(pow(max(opposedRoot, 0.0), power), fallback);
|
||||
}
|
||||
|
||||
void main() {
|
||||
ivec2 local = ivec2(gl_GlobalInvocationID.xy);
|
||||
if (local.x >= uStripeSize.x || local.y >= uStripeSize.y) return;
|
||||
ivec2 global = ivec2(local.x, local.y + uGlobalRowOffset);
|
||||
int channel = bayerIndexAt(uCfaPattern, global);
|
||||
float sensor = sensorNormalizedAt(local);
|
||||
float value = calculationSampleAt(local);
|
||||
float clipMask = smoothstep(HIGHLIGHT_CLIP_THRESHOLD, 1.0, sensor);
|
||||
if (uReconstructHighlights != 0 && clipMask > 0.0) {
|
||||
float reconstructed = estimateOpposedLinear(local, colorAt(local), value);
|
||||
value = min(mix(value, reconstructed, clipMask), HIGHLIGHT_CEILING);
|
||||
}
|
||||
int index = local.y * uStripeSize.x + local.x;
|
||||
cfa[index] = value;
|
||||
rgb0[index] = channel == 0 ? value : 0.0;
|
||||
rgb1[index] = (channel == 1 || channel == 2) ? value : 0.0;
|
||||
rgb2[index] = channel == 3 ? value : 0.0;
|
||||
}
|
||||
""".trimIndent()
|
||||
}
|
||||
@@ -11,7 +11,28 @@ class RcdShadersTest {
|
||||
|
||||
assertTrue(shader.contains("ivec2 mirrorCoord"))
|
||||
assertTrue(shader.contains("color = ppgColorAt(coord);"))
|
||||
assertTrue(shader.contains("const int RCD_OUTPUT_MARGIN = ${RcdShaders.OUTPUT_MARGIN};"))
|
||||
assertFalse(shader.contains("uniform int uBorder"))
|
||||
assertFalse(shader.contains("borderInterpolateAt"))
|
||||
assertFalse(shader.contains("inOutermostBorder"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun stripeRcdUsesTheSamePpgKernelOnTheRealPhotoBorder() {
|
||||
val fullImageShader = RcdShaders.WRITE_OUTPUT
|
||||
val stripeShader = RcdShaders.STRIPE_BORDER_PPG
|
||||
val sharedKernelLines = listOf(
|
||||
"float ppgGreenAt(ivec2 coord)",
|
||||
"vec3 ppgColorAt(ivec2 coord)",
|
||||
"float guessx = (pxm + pc + pxM) * 2.0 - pxM2 - pxm2;",
|
||||
"return max(color, vec3(0.0));",
|
||||
)
|
||||
|
||||
sharedKernelLines.forEach { line ->
|
||||
assertTrue(fullImageShader.contains(line))
|
||||
assertTrue(stripeShader.contains(line))
|
||||
}
|
||||
assertTrue(stripeShader.contains("vec3 color = ppgColorAt(global);"))
|
||||
assertTrue(stripeShader.contains("mirrorIndex(global.y + dy, uFullImageSize.y)"))
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user