feat: photon pgtm highlight optimize
This commit is contained in:
1 parent
a0a727951c
commit
bb6c3b288b
5 files changed
+232
-19
No files matched your search
@@ -402,7 +402,11 @@ internal object DngHdrProfileGainTableGpuShaders {
|
||||
return clamp(min(enter, exit), 0.0, 1.0);
|
||||
}
|
||||
|
||||
float localContrastWarp(float exposedInput, float contrastExponent) {
|
||||
float localContrastWarp(
|
||||
float exposedInput,
|
||||
float contrastExponent,
|
||||
float highlightRecovery
|
||||
) {
|
||||
float endpoint = max(uExposureGain, uExposedPivot + CURVE_EPS);
|
||||
if (exposedInput <= uExposedPivot) {
|
||||
float normalized = clamp(
|
||||
@@ -413,13 +417,19 @@ internal object DngHdrProfileGainTableGpuShaders {
|
||||
return uExposedPivot * pow(normalized, contrastExponent);
|
||||
}
|
||||
float normalized = clamp(
|
||||
(endpoint - exposedInput) /
|
||||
(exposedInput - uExposedPivot) /
|
||||
max(endpoint - uExposedPivot, CURVE_EPS),
|
||||
0.0,
|
||||
1.0
|
||||
);
|
||||
return endpoint -
|
||||
(endpoint - uExposedPivot) * pow(normalized, contrastExponent);
|
||||
float contrasted = 1.0 - pow(1.0 - normalized, contrastExponent);
|
||||
// Move the crowded shoulder midrange down while preserving both the gray pivot and
|
||||
// source-white endpoint. k < 1 keeps this quadratic warp strictly monotonic and its
|
||||
// finite endpoint slope avoids amplifying clipped/noisy samples.
|
||||
float recovered = contrasted -
|
||||
highlightRecovery * contrasted * (1.0 - contrasted);
|
||||
return uExposedPivot +
|
||||
(endpoint - uExposedPivot) * clamp(recovered, 0.0, 1.0);
|
||||
}
|
||||
|
||||
float lowCurve(float exposedInput) {
|
||||
@@ -460,7 +470,9 @@ internal object DngHdrProfileGainTableGpuShaders {
|
||||
int cellIndex = int(gl_GlobalInvocationID.x);
|
||||
if (cellIndex >= uCellCount) return;
|
||||
|
||||
float contrastExponent = clamp(curvePlans[cellIndex], 1.0, 1.18);
|
||||
int planOffset = cellIndex * 2;
|
||||
float contrastExponent = clamp(curvePlans[planOffset], 1.0, 1.18);
|
||||
float highlightRecovery = clamp(curvePlans[planOffset + 1], 0.0, 1.0);
|
||||
float previousFinalOutput = 0.0;
|
||||
int outputOffset = cellIndex * uPointCount;
|
||||
for (int point = 0; point < uPointCount; ++point) {
|
||||
@@ -469,7 +481,11 @@ internal object DngHdrProfileGainTableGpuShaders {
|
||||
: float(point) / float(uPointCount);
|
||||
float sourceInput = pow(max(tableInput, 0.0), 1.0 / uMapGamma);
|
||||
float exposedInput = uExposureGain * sourceInput;
|
||||
float warpedInput = localContrastWarp(exposedInput, contrastExponent);
|
||||
float warpedInput = localContrastWarp(
|
||||
exposedInput,
|
||||
contrastExponent,
|
||||
highlightRecovery
|
||||
);
|
||||
float mappedOutput = globalCurve(warpedInput);
|
||||
float trueGain = exposedInput <= CURVE_EPS
|
||||
? clamp(uLowSlope, uMinTableGain, uMaxTableGain)
|
||||
|
||||
@@ -21,6 +21,9 @@ internal object DngPhotonProfileGainTableGenerator {
|
||||
private const val LOW_CURVE_LIFT = 1.50f
|
||||
private const val MAX_LOCAL_CONTRAST = 0.18f
|
||||
private const val MAX_LOCAL_CONTRAST_EXPONENT = 1.18f
|
||||
private const val MAX_LOCAL_HIGHLIGHT_RECOVERY = 0.42f
|
||||
private const val LOCAL_LEVEL_EPS = 0.006f
|
||||
private const val RECOVERY_EDGE_SIGMA_EV = 0.75f
|
||||
|
||||
// Invert the exact LUT used by the renderer so PGTM + ProfileToneCurve, as a pair,
|
||||
// places exposed 18% gray at +1.1 EV.
|
||||
@@ -139,7 +142,10 @@ internal object DngPhotonProfileGainTableGenerator {
|
||||
noiseSlope: Float,
|
||||
noiseOffset: Float,
|
||||
): Array<PhotonPgtmCellPlan> {
|
||||
val rawStrength = FloatArray(cells.size) { index ->
|
||||
val cellLevelEv = FloatArray(cells.size) { index ->
|
||||
log2((cells[index] ?: global).p50 + LOCAL_LEVEL_EPS)
|
||||
}
|
||||
val rawContrast = FloatArray(cells.size) { index ->
|
||||
val cell = cells[index] ?: global
|
||||
val signal = cell.p50.coerceAtLeast(0f)
|
||||
val variance = max(noiseSlope * signal + noiseOffset, 1e-12f)
|
||||
@@ -147,21 +153,54 @@ internal object DngPhotonProfileGainTableGenerator {
|
||||
val snrGate = smoothStep(3f, 16f, snr)
|
||||
val rangeEv = log2((cell.p90 + 0.006f) / (cell.p10 + 0.006f))
|
||||
val lowContrastOpportunity = 1f - smoothStep(2.5f, 6f, rangeEv)
|
||||
val highlightGate = 1f - 0.80f * smoothStep(
|
||||
// Highlight-rich cells still need texture separation. Reducing this to 20% made
|
||||
// foliage and architecture behind a bright window collapse into one pale band.
|
||||
val highlightDetailGate = 1f - 0.35f * smoothStep(
|
||||
0.08f,
|
||||
0.55f,
|
||||
cell.highlightFraction,
|
||||
)
|
||||
((0.08f + 0.10f * lowContrastOpportunity) * snrGate * highlightGate)
|
||||
((0.08f + 0.10f * lowContrastOpportunity) * snrGate * highlightDetailGate)
|
||||
.coerceIn(0f, MAX_LOCAL_CONTRAST)
|
||||
}
|
||||
val smoothedContrast = smoothScalarField(rawStrength, grid)
|
||||
val rawHighlightRecovery = FloatArray(cells.size) { index ->
|
||||
val cell = cells[index] ?: global
|
||||
val signal = cell.p50.coerceAtLeast(0f)
|
||||
val variance = max(noiseSlope * signal + noiseOffset, 1e-12f)
|
||||
val snr = signal / sqrt(variance)
|
||||
val snrGate = smoothStep(3f, 16f, snr)
|
||||
val medianOffsetEv = log2(
|
||||
(cell.p50 + LOCAL_LEVEL_EPS) / (global.p50 + LOCAL_LEVEL_EPS)
|
||||
)
|
||||
val upperOffsetEv = log2(
|
||||
(cell.p90 + LOCAL_LEVEL_EPS) / (global.p90 + LOCAL_LEVEL_EPS)
|
||||
)
|
||||
val brightRegion = smoothStep(
|
||||
0.20f,
|
||||
1.25f,
|
||||
max(medianOffsetEv, 0.5f * upperOffsetEv),
|
||||
)
|
||||
val highlightOccupancy = smoothStep(0.08f, 0.65f, cell.highlightFraction)
|
||||
(MAX_LOCAL_HIGHLIGHT_RECOVERY * brightRegion * highlightOccupancy * snrGate)
|
||||
.coerceIn(0f, MAX_LOCAL_HIGHLIGHT_RECOVERY)
|
||||
}
|
||||
val smoothedContrast = smoothScalarField(rawContrast, grid)
|
||||
// Bilateral weighting prevents the recovery field from spilling across a strong window
|
||||
// boundary. The DNG renderer supplies the final continuous interpolation between cells.
|
||||
val smoothedHighlightRecovery = smoothScalarField(
|
||||
source = rawHighlightRecovery,
|
||||
grid = grid,
|
||||
guide = cellLevelEv,
|
||||
edgeSigma = RECOVERY_EDGE_SIGMA_EV,
|
||||
)
|
||||
return Array(cells.size) { index ->
|
||||
val contrastExponent = (
|
||||
1f + smoothedContrast[index].coerceIn(0f, MAX_LOCAL_CONTRAST)
|
||||
).coerceIn(1f, MAX_LOCAL_CONTRAST_EXPONENT)
|
||||
PhotonPgtmCellPlan(
|
||||
contrastExponent = contrastExponent,
|
||||
highlightRecovery = smoothedHighlightRecovery[index]
|
||||
.coerceIn(0f, MAX_LOCAL_HIGHLIGHT_RECOVERY),
|
||||
)
|
||||
}
|
||||
}
|
||||
@@ -169,6 +208,8 @@ internal object DngPhotonProfileGainTableGenerator {
|
||||
private fun smoothScalarField(
|
||||
source: FloatArray,
|
||||
grid: HdrPgtmGrid,
|
||||
guide: FloatArray? = null,
|
||||
edgeSigma: Float = 1f,
|
||||
): FloatArray = FloatArray(source.size) { index ->
|
||||
val x = index % grid.mapPointsH
|
||||
val y = index / grid.mapPointsH
|
||||
@@ -183,11 +224,21 @@ internal object DngPhotonProfileGainTableGenerator {
|
||||
dx == 0 || dy == 0 -> 2f
|
||||
else -> 1f
|
||||
}
|
||||
weightedSum += source[yy * grid.mapPointsH + xx] * weight
|
||||
weightSum += weight
|
||||
val neighbor = yy * grid.mapPointsH + xx
|
||||
val edgeWeight = if (guide == null) {
|
||||
1f
|
||||
} else {
|
||||
exp(
|
||||
(-kotlin.math.abs(guide[neighbor] - guide[index]) /
|
||||
edgeSigma.coerceAtLeast(SOURCE_EPS)).toDouble()
|
||||
).toFloat()
|
||||
}
|
||||
val combinedWeight = weight * edgeWeight
|
||||
weightedSum += source[neighbor] * combinedWeight
|
||||
weightSum += combinedWeight
|
||||
}
|
||||
}
|
||||
weightedSum / weightSum
|
||||
weightedSum / weightSum.coerceAtLeast(SOURCE_EPS)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -261,7 +312,10 @@ internal data class PhotonPgtmPlan(
|
||||
require(shoulderParameter.isFinite())
|
||||
require(minTableGain > 0f && maxTableGain >= minTableGain)
|
||||
val invalidCell = cellPlans.indexOfFirst { cellPlan ->
|
||||
!cellPlan.contrastExponent.isFinite() || cellPlan.contrastExponent !in 1f..1.18f
|
||||
!cellPlan.contrastExponent.isFinite() ||
|
||||
cellPlan.contrastExponent !in 1f..1.18f ||
|
||||
!cellPlan.highlightRecovery.isFinite() ||
|
||||
cellPlan.highlightRecovery !in 0f..0.42f
|
||||
}
|
||||
require(invalidCell < 0) {
|
||||
"Invalid Photon PGTM cell[$invalidCell]=${cellPlans.getOrNull(invalidCell)} " +
|
||||
@@ -272,4 +326,5 @@ internal data class PhotonPgtmPlan(
|
||||
|
||||
internal data class PhotonPgtmCellPlan(
|
||||
val contrastExponent: Float,
|
||||
val highlightRecovery: Float,
|
||||
)
|
||||
@@ -2439,7 +2439,7 @@ class RawDemosaicProcessor {
|
||||
|
||||
val curvePlanFloatCount = when (plan.curveModel) {
|
||||
HdrPgtmCurveModel.GOOGLE -> cellCount * 3
|
||||
HdrPgtmCurveModel.PHOTON -> cellCount
|
||||
HdrPgtmCurveModel.PHOTON -> cellCount * 2
|
||||
}
|
||||
val curvePlanBuffer = ByteBuffer.allocateDirect(
|
||||
curvePlanFloatCount * Float.SIZE_BYTES
|
||||
@@ -2460,6 +2460,7 @@ class RawDemosaicProcessor {
|
||||
val photonPlan = requireNotNull(plan.photonPlan)
|
||||
photonPlan.cellPlans.forEach { cellPlan ->
|
||||
curvePlanBuffer.put(cellPlan.contrastExponent)
|
||||
curvePlanBuffer.put(cellPlan.highlightRecovery)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -2606,11 +2607,19 @@ class RawDemosaicProcessor {
|
||||
val curvesGpuReadyNs = System.nanoTime()
|
||||
val map = DngHdrProfileGainTableGenerator.mapFromGpuGains(plan, gains) ?: return null
|
||||
val photonSummary = plan.photonPlan?.let { photonPlan ->
|
||||
val recoveryMean = photonPlan.cellPlans
|
||||
.sumOf { it.highlightRecovery.toDouble() } /
|
||||
photonPlan.cellPlans.size.coerceAtLeast(1)
|
||||
val recoveryMax = photonPlan.cellPlans.maxOfOrNull {
|
||||
it.highlightRecovery
|
||||
} ?: 0f
|
||||
" photonBaselineGain=${photonPlan.exposureGain} " +
|
||||
"photonPivotSource=${photonPlan.sourcePivot} " +
|
||||
"photonFixedGray=${photonPlan.usesFixedGray} " +
|
||||
"photonGamma=${plan.gamma} " +
|
||||
"photonEndpoint=${photonPlan.endpointOutput} "
|
||||
"photonEndpoint=${photonPlan.endpointOutput} " +
|
||||
"photonRecoveryMean=$recoveryMean " +
|
||||
"photonRecoveryMax=$recoveryMax "
|
||||
}.orEmpty()
|
||||
PLog.d(
|
||||
TAG,
|
||||
|
||||
@@ -72,6 +72,7 @@ internal object DngHdrProfileGainTableCpuReference {
|
||||
val warpedInput = localContrastWarp(
|
||||
exposedInput = exposedInput,
|
||||
contrastExponent = cellPlan.contrastExponent,
|
||||
highlightRecovery = cellPlan.highlightRecovery,
|
||||
photonPlan = photonPlan,
|
||||
)
|
||||
val output = globalCurve(warpedInput, photonPlan)
|
||||
@@ -107,6 +108,7 @@ internal object DngHdrProfileGainTableCpuReference {
|
||||
private fun localContrastWarp(
|
||||
exposedInput: Float,
|
||||
contrastExponent: Float,
|
||||
highlightRecovery: Float,
|
||||
photonPlan: PhotonPgtmPlan,
|
||||
): Float {
|
||||
val pivot = photonPlan.exposedPivot
|
||||
@@ -116,9 +118,12 @@ internal object DngHdrProfileGainTableCpuReference {
|
||||
pivot * normalized.pow(contrastExponent)
|
||||
} else {
|
||||
val normalized = (
|
||||
(endpoint - exposedInput) / max(endpoint - pivot, CURVE_EPS)
|
||||
(exposedInput - pivot) / max(endpoint - pivot, CURVE_EPS)
|
||||
).coerceIn(0f, 1f)
|
||||
endpoint - (endpoint - pivot) * normalized.pow(contrastExponent)
|
||||
val contrasted = 1f - (1f - normalized).pow(contrastExponent)
|
||||
val recovered = contrasted -
|
||||
highlightRecovery * contrasted * (1f - contrasted)
|
||||
pivot + (endpoint - pivot) * recovered.coerceIn(0f, 1f)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+129
-1
@@ -160,11 +160,13 @@ class DngPhotonProfileGainTableGeneratorTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun localPlansUseBoundedSContrast() {
|
||||
fun localPlansUseBoundedSContrastAndHighlightRecovery() {
|
||||
val plan = photonPlan(3f)
|
||||
val cellPlans = requireNotNull(plan.photonPlan).cellPlans
|
||||
assertTrue(cellPlans.all { it.contrastExponent in 1f..1.18f })
|
||||
assertTrue(cellPlans.all { it.highlightRecovery in 0f..0.42f })
|
||||
assertTrue(cellPlans.any { it.contrastExponent > 1.01f })
|
||||
assertTrue(cellPlans.all { it.highlightRecovery == 0f })
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -182,6 +184,126 @@ class DngPhotonProfileGainTableGeneratorTest {
|
||||
)
|
||||
val cellPlans = requireNotNull(maximumContrast.photonPlan).cellPlans
|
||||
assertTrue(cellPlans.all { it.contrastExponent in 1f..1.18f })
|
||||
assertTrue(cellPlans.all { it.highlightRecovery in 0f..0.42f })
|
||||
}
|
||||
|
||||
@Test
|
||||
fun locallyBrightBroadHighlightsRecoverShoulderSeparationWithoutMovingAnchors() {
|
||||
val grid = DngHdrProfileGainTableGenerator.gridSizeFor(WIDTH, HEIGHT)
|
||||
val brightStart = grid[0] / 2
|
||||
val plan = photonPlanByCell(baselineExposureEv = 3f) { cell ->
|
||||
val x = cell % grid[0]
|
||||
if (x >= brightStart) {
|
||||
Stats(
|
||||
p10 = 0.16f,
|
||||
p50 = 0.46f,
|
||||
p90 = 0.78f,
|
||||
p98 = 0.94f,
|
||||
p999 = 0.99f,
|
||||
highlightFraction = 0.82f,
|
||||
)
|
||||
} else {
|
||||
Stats(
|
||||
p10 = 0.015f,
|
||||
p50 = 0.07f,
|
||||
p90 = 0.18f,
|
||||
p98 = 0.28f,
|
||||
p999 = 0.42f,
|
||||
highlightFraction = 0.02f,
|
||||
)
|
||||
}
|
||||
}
|
||||
val photonPlan = requireNotNull(plan.photonPlan)
|
||||
val brightCell = brightStart + grid[0] * (grid[1] / 2)
|
||||
val cellPlan = photonPlan.cellPlans[brightCell]
|
||||
assertTrue("recovery=${cellPlan.highlightRecovery}", cellPlan.highlightRecovery > 0.10f)
|
||||
|
||||
val gains = DngHdrProfileGainTableCpuReference.generate(plan)
|
||||
val withoutRecoveryPlan = plan.copy(
|
||||
photonPlan = photonPlan.copy(
|
||||
cellPlans = photonPlan.cellPlans.copyOf().also { cellPlans ->
|
||||
cellPlans[brightCell] = cellPlan.copy(highlightRecovery = 0f)
|
||||
}
|
||||
)
|
||||
)
|
||||
val gainsWithoutRecovery = DngHdrProfileGainTableCpuReference.generate(
|
||||
withoutRecoveryPlan
|
||||
)
|
||||
val shoulderInput = 1.25f
|
||||
val recoveredOutput = samplePhysicalOutput(
|
||||
plan,
|
||||
gains,
|
||||
cell = brightCell,
|
||||
exposedInput = shoulderInput,
|
||||
)
|
||||
val unrecoveredOutput = samplePhysicalOutput(
|
||||
withoutRecoveryPlan,
|
||||
gainsWithoutRecovery,
|
||||
cell = brightCell,
|
||||
exposedInput = shoulderInput,
|
||||
)
|
||||
assertTrue(
|
||||
"recovered=$recoveredOutput unrecovered=$unrecoveredOutput",
|
||||
recoveredOutput < unrecoveredOutput - 0.015f,
|
||||
)
|
||||
val recoveredSpan = samplePhysicalOutput(
|
||||
plan,
|
||||
gains,
|
||||
cell = brightCell,
|
||||
exposedInput = 1.6f,
|
||||
) - samplePhysicalOutput(
|
||||
plan,
|
||||
gains,
|
||||
cell = brightCell,
|
||||
exposedInput = 0.8f,
|
||||
)
|
||||
val unrecoveredSpan = samplePhysicalOutput(
|
||||
withoutRecoveryPlan,
|
||||
gainsWithoutRecovery,
|
||||
cell = brightCell,
|
||||
exposedInput = 1.6f,
|
||||
) - samplePhysicalOutput(
|
||||
withoutRecoveryPlan,
|
||||
gainsWithoutRecovery,
|
||||
cell = brightCell,
|
||||
exposedInput = 0.8f,
|
||||
)
|
||||
assertTrue(
|
||||
"recoveredSpan=$recoveredSpan unrecoveredSpan=$unrecoveredSpan",
|
||||
recoveredSpan >= 0.95f * unrecoveredSpan,
|
||||
)
|
||||
assertEquals(
|
||||
photonPlan.pivotOutput,
|
||||
samplePhysicalOutput(
|
||||
plan,
|
||||
gains,
|
||||
cell = brightCell,
|
||||
exposedInput = photonPlan.exposedPivot,
|
||||
),
|
||||
2e-3f,
|
||||
)
|
||||
assertEquals(
|
||||
photonPlan.endpointOutput,
|
||||
samplePhysicalOutput(
|
||||
plan,
|
||||
gains,
|
||||
cell = brightCell,
|
||||
exposedInput = photonPlan.exposureGain,
|
||||
),
|
||||
2e-5f,
|
||||
)
|
||||
var previousOutput = 0f
|
||||
repeat(plan.pointCount) { point ->
|
||||
val tableInput = tableInput(point, plan.pointCount)
|
||||
val sourceInput = tableInput.pow(1f / plan.gamma)
|
||||
val exposedInput = photonPlan.exposureGain * sourceInput
|
||||
val output = exposedInput * gains[brightCell * plan.pointCount + point]
|
||||
assertTrue(
|
||||
"bright-cell reversal at point=$point output=$output previous=$previousOutput",
|
||||
output + 2e-5f >= previousOutput,
|
||||
)
|
||||
previousOutput = output
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -202,6 +324,11 @@ class DngPhotonProfileGainTableGeneratorTest {
|
||||
private fun photonPlan(
|
||||
baselineExposureEv: Float,
|
||||
stats: Stats = Stats(),
|
||||
): HdrProfileGainTablePlan = photonPlanByCell(baselineExposureEv) { stats }
|
||||
|
||||
private fun photonPlanByCell(
|
||||
baselineExposureEv: Float,
|
||||
statsForCell: (Int) -> Stats,
|
||||
): HdrProfileGainTablePlan {
|
||||
val grid = DngHdrProfileGainTableGenerator.gridSizeFor(WIDTH, HEIGHT)
|
||||
val cellCount = grid[0] * grid[1]
|
||||
@@ -209,6 +336,7 @@ class DngPhotonProfileGainTableGeneratorTest {
|
||||
cellCount * DngHdrProfileGainTableGenerator.CELL_STATS_FLOAT_STRIDE
|
||||
)
|
||||
repeat(cellCount) { cell ->
|
||||
val stats = statsForCell(cell)
|
||||
val offset = cell * DngHdrProfileGainTableGenerator.CELL_STATS_FLOAT_STRIDE
|
||||
packed[offset] = stats.p10
|
||||
packed[offset + 1] = stats.p50
|
||||
|
||||
Reference in new issue
Block a user