feat: ultra hdr gainmap optimize

This commit is contained in:
zhoubinjia committed 2026-07-23 17:50:11 +08:00
1 parent ea1bb6ce74
commit b4cc7f8469
3 files changed
+105 -2

No files matched your search

@@ -203,6 +203,14 @@ class GpuReferenceGainmapProducer : GainmapProducer {
)
GLES30.glUniform1f(GLES30.glGetUniformLocation(rawLumaResidualProgram, "uOffset"), EPSILON)
GLES30.glUniform1f(GLES30.glGetUniformLocation(rawLumaResidualProgram, "uStrength"), strength)
GLES30.glUniform1f(
GLES30.glGetUniformLocation(rawLumaResidualProgram, "uGainStartLuma"),
RAW_GAIN_START_LUMA,
)
GLES30.glUniform1f(
GLES30.glGetUniformLocation(rawLumaResidualProgram, "uGainFullLuma"),
RAW_GAIN_FULL_LUMA,
)
drawQuad(rawLumaResidualProgram)
checkGlError("renderRawLumaResidualPass")
}
@@ -484,6 +492,8 @@ class GpuReferenceGainmapProducer : GainmapProducer {
private const val RAW_DOWNSAMPLE = RawGainmapMath.DOWNSAMPLE
private const val RAW_MIN_GAIN_RATIO = RawGainmapMath.MIN_GAIN_RATIO
private const val RAW_MAX_GAIN_RATIO = RawGainmapMath.MAX_GAIN_RATIO
private const val RAW_GAIN_START_LUMA = RawGainmapMath.GAIN_START_LUMA
private const val RAW_GAIN_FULL_LUMA = RawGainmapMath.GAIN_FULL_LUMA
private const val RAW_LOW_SCENE_MAX_GAIN_RATIO = 1.6033f
private const val EPSILON = RawGainmapMath.OFFSET
private val RAW_CONFIG = Config(
@@ -524,6 +534,8 @@ class GpuReferenceGainmapProducer : GainmapProducer {
uniform float uMaxGainRatio;
uniform float uOffset;
uniform float uStrength;
uniform float uGainStartLuma;
uniform float uGainFullLuma;
float srgbToLinear(float value) {
float safeValue = max(value, 0.0);
@@ -557,11 +569,21 @@ class GpuReferenceGainmapProducer : GainmapProducer {
0.0
);
float targetHdrLuma = lutAdjustedHdrBaseLuma + hdrHeadroomLuma;
float ratio = clamp(
float candidateRatio = clamp(
(targetHdrLuma + uOffset) / (sdrLuma + uOffset),
uMinGainRatio,
uMaxGainRatio
);
float gainParticipation = smoothstep(
uGainStartLuma,
uGainFullLuma,
hdrLuma
);
float ratio = clamp(
mix(1.0, candidateRatio, gainParticipation),
uMinGainRatio,
uMaxGainRatio
);
float normalizedStrength = clamp(uStrength, 0.25, 2.0);
float strengthRatio = clamp(
@@ -9,6 +9,8 @@ internal object RawGainmapMath {
const val MIN_GAIN_RATIO = 0.25f
const val MAX_GAIN_RATIO = 4.0f
const val OFFSET = 1e-4f
const val GAIN_START_LUMA = 0.09f
const val GAIN_FULL_LUMA = 0.18f
private const val LUMA_R = 0.2126f
private const val LUMA_G = 0.7152f
@@ -70,8 +72,14 @@ internal object RawGainmapMath {
val targetHdrLuma = lutLuminanceGain?.let { gain ->
applyLutLuminanceGain(gain, hdrLuma, offset)
} ?: hdrLuma
val ratio = ((targetHdrLuma + offset) / (sdrLuma + offset))
val candidateRatio = ((targetHdrLuma + offset) / (sdrLuma + offset))
.coerceIn(minGainRatio, maxGainRatio)
val ratio = gateRatioByHdrLuminance(
candidateRatio = candidateRatio,
hdrReferenceLuma = hdrLuma,
minGainRatio = minGainRatio,
maxGainRatio = maxGainRatio,
)
val strengthRatio = HdrGainmapStrength.applyToRatio(
ratio = ratio,
minGainRatio = minGainRatio,
@@ -108,6 +116,21 @@ internal object RawGainmapMath {
return adjustedBase + hdrHeadroom
}
fun gateRatioByHdrLuminance(
candidateRatio: Float,
hdrReferenceLuma: Float,
minGainRatio: Float = MIN_GAIN_RATIO,
maxGainRatio: Float = MAX_GAIN_RATIO,
): Float {
val participation = smoothstep(
GAIN_START_LUMA,
GAIN_FULL_LUMA,
hdrReferenceLuma.coerceAtLeast(0f),
)
return (1f + (candidateRatio - 1f) * participation)
.coerceIn(minGainRatio, maxGainRatio)
}
fun reconstructLinear(
sdrEncoded: Float,
encodedGain: Float,
@@ -119,4 +142,9 @@ internal object RawGainmapMath {
(ln(maxGainRatio) - ln(minGainRatio)) * encodedGain.coerceIn(0f, 1f)
return (srgbToLinear(sdrEncoded) + offset) * kotlin.math.exp(logGain) - offset
}
private fun smoothstep(edge0: Float, edge1: Float, value: Float): Float {
val t = ((value - edge0) / (edge1 - edge0)).coerceIn(0f, 1f)
return t * t * (3f - 2f * t)
}
}
@@ -106,4 +106,57 @@ class RawGainmapMathTest {
assertEquals(1.25f, adjustedExtended - adjustedWhite, 0.00001f)
}
@Test
fun keepsZeroGainThroughDarkLuminanceRange() {
assertEquals(
1f,
RawGainmapMath.gateRatioByHdrLuminance(
candidateRatio = RawGainmapMath.MAX_GAIN_RATIO,
hdrReferenceLuma = RawGainmapMath.GAIN_START_LUMA,
),
0.00001f,
)
assertEquals(
1f,
RawGainmapMath.gateRatioByHdrLuminance(
candidateRatio = RawGainmapMath.MIN_GAIN_RATIO,
hdrReferenceLuma = RawGainmapMath.GAIN_START_LUMA,
),
0.00001f,
)
}
@Test
fun smoothlyIntroducesGainBetweenDarkAndFullThresholds() {
val midpoint = (RawGainmapMath.GAIN_START_LUMA + RawGainmapMath.GAIN_FULL_LUMA) * 0.5f
assertEquals(
2f,
RawGainmapMath.gateRatioByHdrLuminance(
candidateRatio = 3f,
hdrReferenceLuma = midpoint,
),
0.00001f,
)
assertEquals(
3f,
RawGainmapMath.gateRatioByHdrLuminance(
candidateRatio = 3f,
hdrReferenceLuma = RawGainmapMath.GAIN_FULL_LUMA,
),
0.00001f,
)
}
@Test
fun lutLiftCannotActivateGainBelowDarkThreshold() {
val encoded = RawGainmapMath.encodeLuminance(
sdrEncodedRgb = floatArrayOf(0.10f, 0.10f, 0.10f),
hdrLinearRgb = floatArrayOf(0.08f, 0.08f, 0.08f),
lutLuminanceGain = 2.0f,
)
assertEquals(0.5f, encoded, 0.00001f)
}
}