fix: ultra hdr
This commit is contained in:
1 parent
d111636819
commit
6b6362fe74
7 files changed
+395
-27
No files matched your search
@@ -26,18 +26,23 @@ internal object DngProfileGainTableRenderShader {
|
||||
return mix(gain0, gain1, amount);
|
||||
}
|
||||
|
||||
float profileGainTableInput(vec3 profileRgb) {
|
||||
float profileGainTableLinearInput(vec3 profileRgb) {
|
||||
float rgbMin = min(profileRgb.r, min(profileRgb.g, profileRgb.b));
|
||||
float rgbMax = max(profileRgb.r, max(profileRgb.g, profileRgb.b));
|
||||
float weighted = dot(vec4(profileRgb, rgbMin), uProfileGainWeights0) +
|
||||
rgbMax * uProfileGainWeightMax;
|
||||
// dng_render applies PGTM before its exposure ramp and scales the five-element
|
||||
// MapInputWeights result by TotalBaselineExposure to preserve post-exposure lookup.
|
||||
return pow(clamp(weighted * uProfileGainBaselineGain, 0.0, 1.0), uProfileGainGamma);
|
||||
return max(weighted * uProfileGainBaselineGain, 0.0);
|
||||
}
|
||||
|
||||
vec3 applyProfileGainTableMap(vec3 profileRgb) {
|
||||
if (uProfileGainEnabled == 0) return profileRgb;
|
||||
float profileGainTableInput(vec3 profileRgb) {
|
||||
// dng_render applies PGTM before its exposure ramp and scales the five-element
|
||||
// MapInputWeights result by TotalBaselineExposure to preserve post-exposure lookup.
|
||||
return pow(clamp(profileGainTableLinearInput(profileRgb), 0.0, 1.0),
|
||||
uProfileGainGamma);
|
||||
}
|
||||
|
||||
float profileGainTableGainAtInput(float normalizedInput) {
|
||||
if (uProfileGainEnabled == 0) return 1.0;
|
||||
int mapH = max(uProfileGainTableSize.x, 1);
|
||||
int mapV = max(uProfileGainTableSize.y, 1);
|
||||
vec2 spacing = max(uProfileGainGrid.zw, vec2(1e-8));
|
||||
@@ -52,14 +57,50 @@ internal object DngProfileGainTableRenderShader {
|
||||
float ty = position.y - float(y0);
|
||||
// Match Adobe DNG SDK RefBaselineProfileGainTableMap exactly: weightScaled =
|
||||
// weight * tableSize, followed by clamping integer indices to tableSize - 1.
|
||||
float tableIndex = profileGainTableInput(profileRgb) *
|
||||
float tableIndex = clamp(normalizedInput, 0.0, 1.0) *
|
||||
float(max(uProfileGainTableSize.z, 1));
|
||||
float gain00 = profileGainTableValue(x0, y0, tableIndex);
|
||||
float gain10 = profileGainTableValue(x1, y0, tableIndex);
|
||||
float gain01 = profileGainTableValue(x0, y1, tableIndex);
|
||||
float gain11 = profileGainTableValue(x1, y1, tableIndex);
|
||||
float gain = mix(mix(gain00, gain10, tx), mix(gain01, gain11, tx), ty);
|
||||
return profileRgb * max(gain, 0.0);
|
||||
return max(mix(mix(gain00, gain10, tx), mix(gain01, gain11, tx), ty), 0.0);
|
||||
}
|
||||
|
||||
vec3 applyProfileGainTableMap(vec3 profileRgb) {
|
||||
if (uProfileGainEnabled == 0) return profileRgb;
|
||||
float gain = profileGainTableGainAtInput(profileGainTableInput(profileRgb));
|
||||
return profileRgb * gain;
|
||||
}
|
||||
|
||||
vec3 applyProfileGainTableMapWithLinearHighlights(
|
||||
vec3 profileRgb,
|
||||
float linearStart
|
||||
) {
|
||||
if (uProfileGainEnabled == 0) return profileRgb;
|
||||
// PGTM is a scalar local response, but a compressed table does not imply that its
|
||||
// gain itself decreases. Build the extension in the pre-gamma linear input domain:
|
||||
// preserve the exact mapped value at the shoulder, reach an unattenuated (or locally
|
||||
// boosted) white, then continue with that positive linear white slope.
|
||||
float linearInput = profileGainTableLinearInput(profileRgb);
|
||||
float tableInput = pow(clamp(linearInput, 0.0, 1.0), uProfileGainGamma);
|
||||
float shoulderTableInput = clamp(linearStart, 0.0, 1.0);
|
||||
if (tableInput <= shoulderTableInput) {
|
||||
return profileRgb * profileGainTableGainAtInput(tableInput);
|
||||
}
|
||||
|
||||
float inverseGamma = 1.0 / max(uProfileGainGamma, 0.000001);
|
||||
float shoulderLinearInput = pow(shoulderTableInput, inverseGamma);
|
||||
float shoulderGain = profileGainTableGainAtInput(shoulderTableInput);
|
||||
float shoulderOutput = shoulderLinearInput * shoulderGain;
|
||||
float mappedWhiteGain = profileGainTableGainAtInput(1.0);
|
||||
float whiteGain = max(max(shoulderGain, mappedWhiteGain), 1.0);
|
||||
float recoverySlope = (whiteGain - shoulderOutput) /
|
||||
max(1.0 - shoulderLinearInput, 0.000001);
|
||||
float extendedOutput = linearInput <= 1.0
|
||||
? shoulderOutput + recoverySlope * (linearInput - shoulderLinearInput)
|
||||
: linearInput * whiteGain;
|
||||
float extendedGain = extendedOutput / max(linearInput, 0.000001);
|
||||
return profileRgb * max(extendedGain, 0.0);
|
||||
}
|
||||
""".trimIndent()
|
||||
}
|
||||
@@ -3758,7 +3758,17 @@ class RawDemosaicProcessor {
|
||||
profileToEngineTransform = combinedProfileToEngineTransform,
|
||||
profileExposureUniforms = combinedProfileExposureUniforms,
|
||||
rawToneMappingParameters = rawToneMappingParameters,
|
||||
applyProfileGainTableMap = false,
|
||||
applyProfileGainTableMap = hasProfileGainTableMap,
|
||||
coordinateInput = if (hasProfileGainTableMap) {
|
||||
RawEngineTonePass.HdrCoordinateInput(
|
||||
textureId = demosaicTextureId,
|
||||
profileToEngineTransform = profileToEngineTransform,
|
||||
profileExposureLinearGain =
|
||||
profileExposureUniforms.linearGain,
|
||||
)
|
||||
} else {
|
||||
null
|
||||
},
|
||||
)
|
||||
true
|
||||
} catch (cancellation: CancellationException) {
|
||||
@@ -7237,6 +7247,7 @@ class RawDemosaicProcessor {
|
||||
profileExposureUniforms: ProfileExposureUniforms,
|
||||
rawToneMappingParameters: RawToneMappingParameters,
|
||||
applyProfileGainTableMap: Boolean,
|
||||
coordinateInput: RawEngineTonePass.HdrCoordinateInput? = null,
|
||||
globalOriginX: Int = 0,
|
||||
globalOriginY: Int = 0,
|
||||
fullImageWidth: Int = metadata.width,
|
||||
@@ -7283,6 +7294,7 @@ class RawDemosaicProcessor {
|
||||
},
|
||||
),
|
||||
sdrLinearTextureId = sdrLinearTextureId,
|
||||
coordinateInput = coordinateInput,
|
||||
),
|
||||
),
|
||||
) { "RAW HDR reference pass failed" }
|
||||
|
||||
@@ -32,6 +32,13 @@ internal class RawEngineTonePass(
|
||||
|
||||
data class Output(val textureId: Int, val width: Int, val height: Int)
|
||||
|
||||
/** Raw profile-domain source used when the main HDR input is already prepared engine RGB. */
|
||||
data class HdrCoordinateInput(
|
||||
val textureId: Int,
|
||||
val profileToEngineTransform: FloatArray,
|
||||
val profileExposureLinearGain: Float,
|
||||
)
|
||||
|
||||
private val adobeCurveAlgorithm = AdobeCurveToneAlgorithm(quad, dcpTextures, curveTextures)
|
||||
private val agxAlgorithm = AgXToneAlgorithm(quad)
|
||||
private val spektrafilmAlgorithm = SpektrafilmToneAlgorithm(quad)
|
||||
@@ -44,9 +51,11 @@ internal class RawEngineTonePass(
|
||||
fun renderHdrReference(
|
||||
input: Input,
|
||||
sdrLinearTextureId: Int,
|
||||
coordinateInput: HdrCoordinateInput?,
|
||||
): Output? = algorithmFor(input.colorEngine).renderHdrReference(
|
||||
input = input,
|
||||
sdrLinearTextureId = sdrLinearTextureId,
|
||||
coordinateInput = coordinateInput,
|
||||
)
|
||||
|
||||
fun release() {
|
||||
@@ -126,6 +135,10 @@ internal class RawEngineTonePass(
|
||||
|
||||
uniform sampler2D uHdrSdrLinearTexture;
|
||||
uniform sampler2D uHdrBaseCurveTexture;
|
||||
uniform sampler2D uHdrCoordinateTexture;
|
||||
uniform int uHdrInputIsPreparedEngineRgb;
|
||||
uniform mat3 uHdrCoordinateProfileToEngineTransform;
|
||||
uniform float uHdrCoordinateExposureGain;
|
||||
uniform float uHdrCurveJoinInput;
|
||||
uniform float uHdrCurveJoinOutput;
|
||||
uniform float uHdrCurveJoinSlope;
|
||||
@@ -136,6 +149,8 @@ internal class RawEngineTonePass(
|
||||
const float HDR_SCENE_WHITE = 1.0;
|
||||
const float HDR_EPSILON = 0.000001;
|
||||
const float HDR_BASE_CURVE_SIZE = ${RawHdrReferenceMath.BASE_CURVE_SAMPLE_COUNT}.0;
|
||||
const float HDR_PGTM_LINEAR_START =
|
||||
${RawHdrReferenceMath.PGTM_LINEAR_EXTENSION_START};
|
||||
|
||||
float sampleHdrBaseCurve(float inputValue) {
|
||||
float normalized = clamp(inputValue, 0.0, HDR_SCENE_WHITE);
|
||||
@@ -164,17 +179,45 @@ internal class RawEngineTonePass(
|
||||
|
||||
void main() {
|
||||
vec3 profileColor = texture(uInputTexture, vTexCoord).rgb;
|
||||
profileColor = applyProfileGainTableMap(profileColor);
|
||||
vec3 engineColor = prepareEngineInput(profileColor);
|
||||
vec3 sdrLinear = max(texture(uHdrSdrLinearTexture, vTexCoord).rgb, vec3(0.0));
|
||||
float toneInput = max(max(engineColor.r, engineColor.g), engineColor.b);
|
||||
if (toneInput <= uHdrCurveJoinInput) {
|
||||
fragColor = vec4(sdrLinear, 1.0);
|
||||
return;
|
||||
vec3 sdrEngineColor;
|
||||
vec3 hdrEngineColor;
|
||||
if (uHdrInputIsPreparedEngineRgb != 0) {
|
||||
// Filmic highlight reconstruction has already baked PGTM, exposure and the
|
||||
// profile transform into the main input. Rebuild only the HDR coordinate
|
||||
// from its raw profile-domain source so PGTM can be extended exactly once.
|
||||
sdrEngineColor = prepareEngineInput(profileColor);
|
||||
vec3 coordinateProfileColor = texture(uHdrCoordinateTexture, vTexCoord).rgb;
|
||||
coordinateProfileColor = applyProfileGainTableMapWithLinearHighlights(
|
||||
coordinateProfileColor,
|
||||
HDR_PGTM_LINEAR_START
|
||||
);
|
||||
hdrEngineColor = uHdrCoordinateProfileToEngineTransform *
|
||||
(coordinateProfileColor * uHdrCoordinateExposureGain);
|
||||
} else {
|
||||
sdrEngineColor = prepareEngineInput(applyProfileGainTableMap(profileColor));
|
||||
hdrEngineColor = prepareEngineInput(
|
||||
applyProfileGainTableMapWithLinearHighlights(
|
||||
profileColor,
|
||||
HDR_PGTM_LINEAR_START
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
float baseCurveOutput = max(sampleHdrBaseCurve(toneInput), HDR_EPSILON);
|
||||
float extendedCurveOutput = max(applyHdrExtendedCurve(toneInput), 0.0);
|
||||
vec3 sdrLinear = max(texture(uHdrSdrLinearTexture, vTexCoord).rgb, vec3(0.0));
|
||||
float sdrToneInput = max(
|
||||
max(sdrEngineColor.r, sdrEngineColor.g),
|
||||
sdrEngineColor.b
|
||||
);
|
||||
float hdrToneInput = max(
|
||||
max(hdrEngineColor.r, hdrEngineColor.g),
|
||||
hdrEngineColor.b
|
||||
);
|
||||
float baseCurveOutput = max(sampleHdrBaseCurve(sdrToneInput), HDR_EPSILON);
|
||||
// The exact SDR is still the color base. This scalar only replaces PGTM's
|
||||
// highlight shoulder and the engine shoulder; it can never reduce SDR output.
|
||||
float extendedCurveOutput = max(
|
||||
applyHdrExtendedCurve(hdrToneInput),
|
||||
baseCurveOutput
|
||||
);
|
||||
float gain = extendedCurveOutput / baseCurveOutput;
|
||||
fragColor = vec4(sdrLinear * gain, 1.0);
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ package com.hinnka.mycamera.raw
|
||||
|
||||
import kotlin.math.ceil
|
||||
import kotlin.math.max
|
||||
import kotlin.math.pow
|
||||
|
||||
/**
|
||||
* Builds the highlight extension used by the RAW HDR reference.
|
||||
@@ -9,16 +10,21 @@ import kotlin.math.max
|
||||
* This follows Phocus' HDR-gradation construction in normalized coordinates. The selected
|
||||
* engine's neutral SDR response is the base curve. The HDR curve follows that curve through the
|
||||
* low and middle range, leaves the SDR shoulder at a tangent-compatible point, reaches +0.5 stop
|
||||
* at scene white, and then keeps the endpoint tangent for over-range highlights.
|
||||
* at scene white, and then keeps the endpoint tangent for over-range highlights. Independently,
|
||||
* the rendering pass keeps PGTM unchanged through the reference search shoulder, linearly returns
|
||||
* an attenuated shoulder to uncompressed white, and continues linearly above white. PGTM
|
||||
* compression is therefore removed before the measured engine curve reaches its own
|
||||
* tangent-compatible join; HDR does not switch to a different non-PGTM rendering.
|
||||
*
|
||||
* Phocus constructs its LUT with `BaseCurve(index * gain)` and later reads that LUT at
|
||||
* `sceneValue / gain`; those factors cancel in scene coordinates. The implementation below works
|
||||
* directly in those scene coordinates, so exposure/PGTM gain is neither omitted nor applied twice.
|
||||
* directly in those scene coordinates, so exposure gain is neither omitted nor applied twice.
|
||||
*/
|
||||
object RawHdrReferenceMath {
|
||||
const val BASE_CURVE_SAMPLE_COUNT = 1024
|
||||
const val SCENE_WHITE = 1f
|
||||
const val SEARCH_START = 20_000f / 65_535f
|
||||
const val PGTM_LINEAR_EXTENSION_START = SEARCH_START
|
||||
const val HDR_WHITE_MULTIPLIER = 1.41421356237f
|
||||
|
||||
private const val MIN_INTERVAL = 1e-4f
|
||||
@@ -126,12 +132,44 @@ object RawHdrReferenceMath {
|
||||
return lower + (upper - lower) * fraction
|
||||
}
|
||||
|
||||
private fun sanitizeCurve(baseCurve: FloatArray): FloatArray {
|
||||
/** Produces the curve uploaded to the HDR pass, including the monotonicity contract. */
|
||||
internal fun sanitizeCurve(baseCurve: FloatArray): FloatArray {
|
||||
var previous = 0f
|
||||
return FloatArray(baseCurve.size) { index ->
|
||||
baseCurve[index].finiteOr(previous).coerceAtLeast(0f).also { previous = it }
|
||||
baseCurve[index].finiteOr(previous).coerceAtLeast(previous).also { previous = it }
|
||||
}
|
||||
}
|
||||
|
||||
/** CPU reference for the shader's PGTM highlight extension. */
|
||||
internal fun pgtmHighlightGain(
|
||||
linearInput: Float,
|
||||
gamma: Float = 1f,
|
||||
sampleGain: (Float) -> Float,
|
||||
): Float {
|
||||
val input = linearInput.finiteOr(0f).coerceAtLeast(0f)
|
||||
val safeGamma = gamma.finiteOr(1f).coerceIn(0.125f, 8f)
|
||||
val tableInput = input.coerceAtMost(1f).pow(safeGamma)
|
||||
if (tableInput <= PGTM_LINEAR_EXTENSION_START) {
|
||||
return sampleGain(tableInput).finiteOr(0f).coerceAtLeast(0f)
|
||||
}
|
||||
|
||||
val shoulderLinearInput =
|
||||
PGTM_LINEAR_EXTENSION_START.pow(1f / safeGamma)
|
||||
val shoulderGain = sampleGain(PGTM_LINEAR_EXTENSION_START)
|
||||
.finiteOr(0f)
|
||||
.coerceAtLeast(0f)
|
||||
val shoulderOutput = shoulderLinearInput * shoulderGain
|
||||
val mappedWhiteGain = sampleGain(1f).finiteOr(0f).coerceAtLeast(0f)
|
||||
val whiteGain = max(max(shoulderGain, mappedWhiteGain), 1f)
|
||||
val recoverySlope = (whiteGain - shoulderOutput) /
|
||||
max(1f - shoulderLinearInput, 1e-6f)
|
||||
val extendedOutput = if (input <= 1f) {
|
||||
shoulderOutput + recoverySlope * (input - shoulderLinearInput)
|
||||
} else {
|
||||
input * whiteGain
|
||||
}
|
||||
return (extendedOutput / max(input, 1e-6f)).finiteOr(0f).coerceAtLeast(0f)
|
||||
}
|
||||
|
||||
private fun Float.finiteOr(fallback: Float): Float = if (isFinite()) this else fallback
|
||||
}
|
||||
@@ -12,6 +12,7 @@ internal class RawHdrReferencePass(
|
||||
data class Input(
|
||||
val engineInput: RawEngineTonePass.Input,
|
||||
val sdrLinearTextureId: Int,
|
||||
val coordinateInput: RawEngineTonePass.HdrCoordinateInput? = null,
|
||||
)
|
||||
|
||||
data class Output(val textureId: Int, val width: Int, val height: Int)
|
||||
@@ -22,6 +23,7 @@ internal class RawHdrReferencePass(
|
||||
return engineTonePass.renderHdrReference(
|
||||
input = input.engineInput,
|
||||
sdrLinearTextureId = input.sdrLinearTextureId,
|
||||
coordinateInput = input.coordinateInput,
|
||||
)?.let { Output(it.textureId, it.width, it.height) }
|
||||
}
|
||||
|
||||
|
||||
@@ -78,6 +78,7 @@ internal abstract class RawRenderingEngineToneAlgorithm(
|
||||
fun renderHdrReference(
|
||||
input: RawEngineTonePass.Input,
|
||||
sdrLinearTextureId: Int,
|
||||
coordinateInput: RawEngineTonePass.HdrCoordinateInput?,
|
||||
): RawEngineTonePass.Output? {
|
||||
val activeProgram = ensureHdrReferenceProgram()
|
||||
if (activeProgram == 0) return null
|
||||
@@ -103,6 +104,7 @@ internal abstract class RawRenderingEngineToneAlgorithm(
|
||||
HDR_BASE_CURVE_TEXTURE_UNIT,
|
||||
)
|
||||
bindSharedPipelineResources(activeProgram, input)
|
||||
bindHdrCoordinateInput(activeProgram, input, coordinateInput)
|
||||
GLES30.glUniform1f(
|
||||
GLES30.glGetUniformLocation(activeProgram, "uHdrCurveJoinInput"),
|
||||
curveExtension.joinInput,
|
||||
@@ -143,11 +145,74 @@ internal abstract class RawRenderingEngineToneAlgorithm(
|
||||
GLES30.glGetUniformLocation(activeProgram, "uHdrSdrLinearTexture"),
|
||||
HDR_SDR_LINEAR_TEXTURE_UNIT,
|
||||
)
|
||||
if (coordinateInput != null) {
|
||||
GLES30.glActiveTexture(GLES30.GL_TEXTURE0 + HDR_COORDINATE_TEXTURE_UNIT)
|
||||
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, coordinateInput.textureId)
|
||||
GLES30.glUniform1i(
|
||||
GLES30.glGetUniformLocation(activeProgram, "uHdrCoordinateTexture"),
|
||||
HDR_COORDINATE_TEXTURE_UNIT,
|
||||
)
|
||||
}
|
||||
quad.draw(activeProgram)
|
||||
RawGlesProgram.logErrors("${javaClass.simpleName}.renderHdrReference")
|
||||
return RawEngineTonePass.Output(input.targetTextureId, input.width, input.height)
|
||||
}
|
||||
|
||||
private fun bindHdrCoordinateInput(
|
||||
program: Int,
|
||||
input: RawEngineTonePass.Input,
|
||||
coordinateInput: RawEngineTonePass.HdrCoordinateInput?,
|
||||
) {
|
||||
GLES30.glUniform1i(
|
||||
GLES30.glGetUniformLocation(program, "uHdrInputIsPreparedEngineRgb"),
|
||||
if (coordinateInput != null) 1 else 0,
|
||||
)
|
||||
if (coordinateInput == null) {
|
||||
// Keep the inactive sampler explicit and valid on drivers that validate every
|
||||
// declared sampler regardless of the uniform branch taken by the draw.
|
||||
GLES30.glUniform1i(
|
||||
GLES30.glGetUniformLocation(program, "uHdrCoordinateTexture"),
|
||||
0,
|
||||
)
|
||||
GLES30.glUniformMatrix3fv(
|
||||
GLES30.glGetUniformLocation(program, "uHdrCoordinateProfileToEngineTransform"),
|
||||
1,
|
||||
false,
|
||||
transpose3x3(input.profileToEngineTransform),
|
||||
0,
|
||||
)
|
||||
GLES30.glUniform1f(
|
||||
GLES30.glGetUniformLocation(program, "uHdrCoordinateExposureGain"),
|
||||
input.profileExposure.linearGain,
|
||||
)
|
||||
return
|
||||
}
|
||||
|
||||
require(coordinateInput.textureId != 0) { "HDR coordinate texture is unavailable" }
|
||||
require(coordinateInput.profileToEngineTransform.size == 9) {
|
||||
"HDR coordinate transform must be 3x3"
|
||||
}
|
||||
GLES30.glActiveTexture(GLES30.GL_TEXTURE0 + HDR_COORDINATE_TEXTURE_UNIT)
|
||||
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, coordinateInput.textureId)
|
||||
GLES30.glUniform1i(
|
||||
GLES30.glGetUniformLocation(program, "uHdrCoordinateTexture"),
|
||||
HDR_COORDINATE_TEXTURE_UNIT,
|
||||
)
|
||||
GLES30.glUniformMatrix3fv(
|
||||
GLES30.glGetUniformLocation(program, "uHdrCoordinateProfileToEngineTransform"),
|
||||
1,
|
||||
false,
|
||||
transpose3x3(coordinateInput.profileToEngineTransform),
|
||||
0,
|
||||
)
|
||||
GLES30.glUniform1f(
|
||||
GLES30.glGetUniformLocation(program, "uHdrCoordinateExposureGain"),
|
||||
coordinateInput.profileExposureLinearGain
|
||||
.takeIf { it.isFinite() && it > 0f }
|
||||
?: 1f,
|
||||
)
|
||||
}
|
||||
|
||||
private fun analyzeHdrBaseCurve(
|
||||
input: RawEngineTonePass.Input,
|
||||
): RawHdrReferenceMath.CurveExtension? {
|
||||
@@ -195,9 +260,11 @@ internal abstract class RawRenderingEngineToneAlgorithm(
|
||||
)
|
||||
return null
|
||||
}
|
||||
val samples = FloatArray(sampleCount) { index ->
|
||||
val measuredSamples = FloatArray(sampleCount) { index ->
|
||||
Half.toFloat(readback.get(index * 4))
|
||||
}
|
||||
val samples = RawHdrReferenceMath.sanitizeCurve(measuredSamples)
|
||||
if (!uploadHdrBaseCurve(samples)) return null
|
||||
return runCatching { RawHdrReferenceMath.solve(samples) }
|
||||
.onFailure { PLog.e(TAG, "Unable to solve ${javaClass.simpleName} HDR curve", it) }
|
||||
.getOrNull()
|
||||
@@ -212,6 +279,42 @@ internal abstract class RawRenderingEngineToneAlgorithm(
|
||||
}
|
||||
}
|
||||
|
||||
private fun uploadHdrBaseCurve(samples: FloatArray): Boolean {
|
||||
val upload = ByteBuffer.allocateDirect(samples.size * 4 * Float.SIZE_BYTES)
|
||||
.order(ByteOrder.nativeOrder())
|
||||
.asFloatBuffer()
|
||||
samples.forEach { sample ->
|
||||
upload.put(sample)
|
||||
upload.put(0f)
|
||||
upload.put(0f)
|
||||
upload.put(1f)
|
||||
}
|
||||
upload.position(0)
|
||||
GLES30.glActiveTexture(GLES30.GL_TEXTURE0 + HDR_BASE_CURVE_TEXTURE_UNIT)
|
||||
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, hdrBaseCurveTextureId)
|
||||
GLES30.glTexSubImage2D(
|
||||
GLES30.GL_TEXTURE_2D,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
samples.size,
|
||||
1,
|
||||
GLES30.GL_RGBA,
|
||||
GLES30.GL_FLOAT,
|
||||
upload,
|
||||
)
|
||||
val error = GLES30.glGetError()
|
||||
if (error != GLES30.GL_NO_ERROR) {
|
||||
PLog.e(
|
||||
TAG,
|
||||
"Unable to upload monotonic HDR base curve: " +
|
||||
"glError=0x${Integer.toHexString(error)}",
|
||||
)
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
private fun ensureHdrBaseCurveTarget(): Boolean {
|
||||
if (hdrBaseCurveTextureId != 0 && hdrBaseCurveFramebufferId != 0) return true
|
||||
|
||||
@@ -350,9 +453,10 @@ internal abstract class RawRenderingEngineToneAlgorithm(
|
||||
}
|
||||
|
||||
companion object {
|
||||
// GLES 3.0 guarantees at least eight fragment texture units. Existing engine resources
|
||||
// use 0..3 and 6; the shared ProfileGainTableMap uses 7.
|
||||
// Unit 2 is reused for the separate coordinate source only by Filmic, whose engine has no
|
||||
// texture resource there. Existing engines otherwise use 0..3 and 6; PGTM uses 7.
|
||||
private const val TAG = "RawRenderingEngineToneAlgorithm"
|
||||
private const val HDR_COORDINATE_TEXTURE_UNIT = 2
|
||||
private const val HDR_BASE_CURVE_TEXTURE_UNIT = 4
|
||||
private const val HDR_SDR_LINEAR_TEXTURE_UNIT = 5
|
||||
}
|
||||
|
||||
@@ -79,6 +79,134 @@ class RawHdrReferenceMathTest {
|
||||
assertTrue(extension.evaluate(2f, base).isFinite())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun sanitizesBaseCurveReversalsBeforeGpuSampling() {
|
||||
val base = sampledCurve { x -> x }
|
||||
base[512] = base[511] - 0.2f
|
||||
base[700] = Float.NaN
|
||||
|
||||
val sanitized = RawHdrReferenceMath.sanitizeCurve(base)
|
||||
|
||||
for (index in 1 until sanitized.size) {
|
||||
assertTrue(sanitized[index] >= sanitized[index - 1])
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun keepsPgtmThroughTheShoulderThenUsesItsLocalGainForLinearHighlights() {
|
||||
val shoulder = RawHdrReferenceMath.PGTM_LINEAR_EXTENSION_START
|
||||
val pgtmGain = { input: Float -> 1.35f - 0.8f * input }
|
||||
val anchorGain = pgtmGain(shoulder)
|
||||
|
||||
assertEquals(
|
||||
pgtmGain(shoulder * 0.75f),
|
||||
RawHdrReferenceMath.pgtmHighlightGain(
|
||||
shoulder * 0.75f,
|
||||
sampleGain = pgtmGain,
|
||||
),
|
||||
0.000001f,
|
||||
)
|
||||
assertEquals(
|
||||
anchorGain,
|
||||
RawHdrReferenceMath.pgtmHighlightGain(0.75f, sampleGain = pgtmGain),
|
||||
0.000001f,
|
||||
)
|
||||
assertEquals(
|
||||
anchorGain,
|
||||
RawHdrReferenceMath.pgtmHighlightGain(1f, sampleGain = pgtmGain),
|
||||
0.000001f,
|
||||
)
|
||||
|
||||
// Recovery starts immediately above the shoulder instead of waiting for near-white.
|
||||
val moderateHighlight = shoulder + (1f - shoulder) * 0.1f
|
||||
val compressedModerate = moderateHighlight * pgtmGain(moderateHighlight)
|
||||
val extendedModerate = moderateHighlight *
|
||||
RawHdrReferenceMath.pgtmHighlightGain(
|
||||
moderateHighlight,
|
||||
sampleGain = pgtmGain,
|
||||
)
|
||||
assertTrue(extendedModerate > compressedModerate)
|
||||
|
||||
val epsilon = 0.00001f
|
||||
val atJoin = shoulder * anchorGain
|
||||
val rightOfJoin = (shoulder + epsilon) *
|
||||
RawHdrReferenceMath.pgtmHighlightGain(
|
||||
shoulder + epsilon,
|
||||
sampleGain = pgtmGain,
|
||||
)
|
||||
assertEquals(atJoin, rightOfJoin, 0.00002f)
|
||||
|
||||
var previous = atJoin
|
||||
repeat(128) { index ->
|
||||
val input = shoulder + (1f - shoulder) * (index + 1f) / 128f
|
||||
val output = input * RawHdrReferenceMath.pgtmHighlightGain(
|
||||
input,
|
||||
sampleGain = pgtmGain,
|
||||
)
|
||||
assertTrue(output >= previous)
|
||||
previous = output
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
fun recoversPgtmAttenuationWhenTableGainRisesTowardWhite() {
|
||||
val shoulder = RawHdrReferenceMath.PGTM_LINEAR_EXTENSION_START
|
||||
val pgtmGain = { input: Float -> 0.45f + 0.4f * input }
|
||||
val shoulderGain = pgtmGain(shoulder)
|
||||
|
||||
assertEquals(
|
||||
shoulderGain,
|
||||
RawHdrReferenceMath.pgtmHighlightGain(shoulder, sampleGain = pgtmGain),
|
||||
0.000001f,
|
||||
)
|
||||
|
||||
val moderateHighlight = shoulder + (1f - shoulder) * 0.25f
|
||||
val originalOutput = moderateHighlight * pgtmGain(moderateHighlight)
|
||||
val recoveredOutput = moderateHighlight * RawHdrReferenceMath.pgtmHighlightGain(
|
||||
moderateHighlight,
|
||||
sampleGain = pgtmGain,
|
||||
)
|
||||
assertTrue(recoveredOutput > originalOutput)
|
||||
assertEquals(
|
||||
1f,
|
||||
RawHdrReferenceMath.pgtmHighlightGain(1f, sampleGain = pgtmGain),
|
||||
0.000001f,
|
||||
)
|
||||
assertEquals(
|
||||
1f,
|
||||
RawHdrReferenceMath.pgtmHighlightGain(1.25f, sampleGain = pgtmGain),
|
||||
0.000001f,
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun neverDropsBelowAPgtmWhiteBoost() {
|
||||
val pgtmGain = { input: Float -> 0.8f + 0.4f * input }
|
||||
|
||||
assertEquals(
|
||||
pgtmGain(1f),
|
||||
RawHdrReferenceMath.pgtmHighlightGain(1f, sampleGain = pgtmGain),
|
||||
0.000001f,
|
||||
)
|
||||
assertEquals(
|
||||
pgtmGain(1f),
|
||||
RawHdrReferenceMath.pgtmHighlightGain(1.5f, sampleGain = pgtmGain),
|
||||
0.000001f,
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun hdrShaderUsesMappedSdrAndLinearHighlightPgtmCoordinatesWithoutOldEarlyExit() {
|
||||
val shader = RawEngineTonePass.hdrReferenceFragmentShaderFor(
|
||||
RawRenderingEngine.AdobeCurve,
|
||||
)
|
||||
|
||||
assertTrue(shader.contains("applyProfileGainTableMap(profileColor)"))
|
||||
assertTrue(shader.contains("applyProfileGainTableMapWithLinearHighlights("))
|
||||
assertTrue(shader.contains("max(\n applyHdrExtendedCurve(hdrToneInput),"))
|
||||
assertTrue(!shader.contains("if (toneInput <= uHdrCurveJoinInput)"))
|
||||
}
|
||||
|
||||
private fun sampledCurve(transform: (Float) -> Float): FloatArray {
|
||||
return FloatArray(RawHdrReferenceMath.BASE_CURVE_SAMPLE_COUNT) { index ->
|
||||
transform(index.toFloat() / (RawHdrReferenceMath.BASE_CURVE_SAMPLE_COUNT - 1))
|
||||
|
||||
Reference in new issue
Block a user