fix: photon curve dark
This commit is contained in:
1 parent
ba67a18bb4
commit
c988de0f25
4 files changed
+187
-9
No files matched your search
@@ -695,13 +695,23 @@ internal object DngPhotonLocalToneMapGpuShaders {
|
||||
if (index >= uCoefficientCount) return;
|
||||
int offset = index * 5;
|
||||
float sumXX = blurred[offset];
|
||||
float sumWeight = blurred[offset + 2];
|
||||
float sumYX = blurred[offset + 3];
|
||||
// ProfileGainTableMap can only express y = gain * x. Fit that model
|
||||
// directly instead of fitting an affine intercept that would later
|
||||
// become the unstable term intercept / x.
|
||||
coefficients[index] =
|
||||
(sumYX + uRegularization * uIdentitySlope) /
|
||||
(sumXX + uRegularization);
|
||||
//
|
||||
// Halide's regularizer represents one tenth of an identity sample.
|
||||
// Scale that synthetic sample by this cell's mean input energy;
|
||||
// adding the raw lambda would be an x=1 sample that dominates dark cells.
|
||||
if (sumXX > 0.0 && sumWeight > 0.0) {
|
||||
float identityPriorXX = uRegularization * (sumXX / sumWeight);
|
||||
coefficients[index] =
|
||||
(sumYX + identityPriorXX * uIdentitySlope) /
|
||||
(sumXX + identityPriorXX);
|
||||
} else {
|
||||
coefficients[index] = uIdentitySlope;
|
||||
}
|
||||
}
|
||||
""".trimIndent()
|
||||
|
||||
|
||||
@@ -450,13 +450,16 @@ internal object DngPhotonLocalToneMapper {
|
||||
rangePlanes = rangePlaneCount,
|
||||
)
|
||||
val sumXX = blurred[sourceOffset]
|
||||
val sumWeight = blurred[sourceOffset + 2]
|
||||
val sumYX = blurred[sourceOffset + 3]
|
||||
val coefficientOffset = (y * gridWidth + x) * rangePlaneCount + z
|
||||
val gain = (
|
||||
sumYX + lambda * identitySlope
|
||||
) / (
|
||||
sumXX + lambda
|
||||
)
|
||||
val gain = solveRegularizedScalarGain(
|
||||
sumXX = sumXX,
|
||||
sumWeight = sumWeight,
|
||||
sumYX = sumYX,
|
||||
identitySlope = identitySlope,
|
||||
regularization = lambda,
|
||||
)
|
||||
require(gain.isFinite() && gain >= 0f) {
|
||||
"Invalid scalar BGU gain at ($x,$y,$z): $gain"
|
||||
}
|
||||
@@ -474,6 +477,34 @@ internal object DngPhotonLocalToneMapper {
|
||||
)
|
||||
}
|
||||
|
||||
/**
|
||||
* Solves `y=gain*x` with Halide's [regularization]-sample identity prior.
|
||||
*
|
||||
* A synthetic identity observation at the cell's RMS input contributes
|
||||
* `regularization * mean(x²)` to both normal-equation sides. Adding the raw regularization
|
||||
* instead would be an `x=1` observation and would overwhelm real evidence in dark cells.
|
||||
*/
|
||||
internal fun solveRegularizedScalarGain(
|
||||
sumXX: Float,
|
||||
sumWeight: Float,
|
||||
sumYX: Float,
|
||||
identitySlope: Float,
|
||||
regularization: Float,
|
||||
): Float {
|
||||
require(sumXX.isFinite() && sumXX >= 0f)
|
||||
require(sumWeight.isFinite() && sumWeight >= 0f)
|
||||
require(sumYX.isFinite() && sumYX >= 0f)
|
||||
require(identitySlope.isFinite() && identitySlope > 0f)
|
||||
require(regularization.isFinite() && regularization > 0f)
|
||||
if (sumXX <= 0f || sumWeight <= 0f) return identitySlope
|
||||
val identityPriorXX = regularization * (sumXX / sumWeight)
|
||||
return (
|
||||
sumYX + identityPriorXX * identitySlope
|
||||
) / (
|
||||
sumXX + identityPriorXX
|
||||
)
|
||||
}
|
||||
|
||||
private fun buildGainCurves(
|
||||
plan: HdrProfileGainTablePlan,
|
||||
photonPlan: PhotonPgtmPlan,
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
package com.hinnka.mycamera.raw
|
||||
|
||||
import java.io.File
|
||||
import kotlin.math.ln
|
||||
import org.junit.Assert.assertEquals
|
||||
import org.junit.Assert.assertTrue
|
||||
import org.junit.Assume.assumeTrue
|
||||
import org.junit.Test
|
||||
|
||||
class DngPhotonProfileGainTableGeneratorTest {
|
||||
@@ -90,6 +93,140 @@ class DngPhotonProfileGainTableGeneratorTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun scalarGainBilateralGridPreservesDarkGainWithSampleScaledRegularization() {
|
||||
val gridWidth = 1
|
||||
val gridHeight = 1
|
||||
val width = DngPhotonLocalToneMapper.SAMPLES_PER_CELL_SIDE
|
||||
val height = DngPhotonLocalToneMapper.SAMPLES_PER_CELL_SIDE
|
||||
val sourceLevel = 0.01f
|
||||
val expectedGain = 4f
|
||||
val source = FloatArray(width * height) { sourceLevel }
|
||||
val target = FloatArray(width * height) { sourceLevel * expectedGain }
|
||||
|
||||
val grid = DngPhotonLocalToneMapper.fitScalarGainBilateralGrid(
|
||||
source = source,
|
||||
target = target,
|
||||
width = width,
|
||||
height = height,
|
||||
gridWidth = gridWidth,
|
||||
gridHeight = gridHeight,
|
||||
parameters = PhotonLocalToneMappingParameters(),
|
||||
)
|
||||
|
||||
// The 0.1-sample prior may pull the fitted gain slightly towards one, but its relative
|
||||
// influence must remain below 0.1% even at a 1% scene-linear input.
|
||||
assertEquals(
|
||||
expectedGain,
|
||||
grid.gain(cell = 0, source = sourceLevel),
|
||||
expectedGain * 0.001f,
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun scalarGainRegularizationRepresentsOneTenthOfAnRmsInputSample() {
|
||||
val sampleCount = 256f
|
||||
val sourceLevel = 0.01f
|
||||
val targetGain = 4f
|
||||
val regularization = 0.1f
|
||||
val sumXX = sampleCount * sourceLevel * sourceLevel
|
||||
val sumYX = sampleCount * sourceLevel * (sourceLevel * targetGain)
|
||||
val expected = (
|
||||
sampleCount * targetGain + regularization
|
||||
) / (
|
||||
sampleCount + regularization
|
||||
)
|
||||
|
||||
val actual = DngPhotonLocalToneMapper.solveRegularizedScalarGain(
|
||||
sumXX = sumXX,
|
||||
sumWeight = sampleCount,
|
||||
sumYX = sumYX,
|
||||
identitySlope = 1f,
|
||||
regularization = regularization,
|
||||
)
|
||||
|
||||
assertEquals(expected, actual, 1e-6f)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun scalarGainBilateralGridUsesIdentityForUnobservableBlackCells() {
|
||||
val gridWidth = 1
|
||||
val gridHeight = 1
|
||||
val width = DngPhotonLocalToneMapper.SAMPLES_PER_CELL_SIDE
|
||||
val height = DngPhotonLocalToneMapper.SAMPLES_PER_CELL_SIDE
|
||||
val source = FloatArray(width * height)
|
||||
val unrepresentableTarget = FloatArray(width * height) { 0.1f }
|
||||
|
||||
val grid = DngPhotonLocalToneMapper.fitScalarGainBilateralGrid(
|
||||
source = source,
|
||||
target = unrepresentableTarget,
|
||||
width = width,
|
||||
height = height,
|
||||
gridWidth = gridWidth,
|
||||
gridHeight = gridHeight,
|
||||
parameters = PhotonLocalToneMappingParameters(),
|
||||
)
|
||||
|
||||
listOf(0f, 0.125f, 0.5f, 1f).forEach { guide ->
|
||||
assertEquals(1f, grid.gain(cell = 0, source = guide), 0f)
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun gpuBguSolveUsesSampleScaledIdentityRegularization() {
|
||||
val shader = DngPhotonLocalToneMapGpuShaders.sources[
|
||||
DngPhotonLocalToneMapGpuShaders.Pass.BGU_SOLVE.ordinal
|
||||
]
|
||||
|
||||
assertTrue(shader.contains("float sumWeight = blurred[offset + 2];"))
|
||||
assertTrue(
|
||||
shader.contains(
|
||||
"float identityPriorXX = uRegularization * (sumXX / sumWeight);"
|
||||
)
|
||||
)
|
||||
assertTrue(shader.contains("coefficients[index] = uIdentitySlope;"))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun gpuBguSolvePassesAvailableNdkValidator() {
|
||||
val sdkRoot = System.getenv("ANDROID_SDK_ROOT") ?: System.getenv("ANDROID_HOME")
|
||||
val validator = sdkRoot?.let(::File)
|
||||
?.resolve("ndk")
|
||||
?.listFiles()
|
||||
?.sortedByDescending { it.name }
|
||||
?.asSequence()
|
||||
?.mapNotNull { ndk ->
|
||||
ndk.resolve("shader-tools")
|
||||
.walkTopDown()
|
||||
.firstOrNull { it.name == "glslc" && it.canExecute() }
|
||||
}
|
||||
?.firstOrNull()
|
||||
assumeTrue("Android NDK glslc is unavailable", validator != null)
|
||||
val shader = DngPhotonLocalToneMapGpuShaders.sources[
|
||||
DngPhotonLocalToneMapGpuShaders.Pass.BGU_SOLVE.ordinal
|
||||
]
|
||||
val sourceFile = File.createTempFile("photon-bgu-solve-", ".compute")
|
||||
val outputFile = File.createTempFile("photon-bgu-solve-", ".spv")
|
||||
try {
|
||||
sourceFile.writeText(shader)
|
||||
val process = ProcessBuilder(
|
||||
checkNotNull(validator).absolutePath,
|
||||
"--target-env=opengl",
|
||||
"-fauto-map-locations",
|
||||
"-fauto-bind-uniforms",
|
||||
"-fshader-stage=compute",
|
||||
sourceFile.absolutePath,
|
||||
"-o",
|
||||
outputFile.absolutePath,
|
||||
).redirectErrorStream(true).start()
|
||||
val output = process.inputStream.bufferedReader().use { it.readText() }
|
||||
assertEquals(output, 0, process.waitFor())
|
||||
} finally {
|
||||
sourceFile.delete()
|
||||
outputFile.delete()
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun localLaplacianClampsSparsePeakBeforeNonlinearProfileStages() {
|
||||
val width = 16
|
||||
|
||||
Reference in new issue
Block a user