feat: radiance (3)

This commit is contained in:
zhoubinjia committed 2026-07-20 14:30:26 +08:00
1 parent 50e2453b31
commit 4d61151058
14 files changed
+1594 -62

No files matched your search

@@ -400,6 +400,7 @@ class Camera2Controller(private val context: Context) {
.get(CaptureResult.CONTROL_POST_RAW_SENSITIVITY_BOOST)
?.coerceAtLeast(1)
?: 100
val channelNoiseProfile = captureChannelNoiseProfile(result)
return CapturedFrameMetadata(
sensorTimestampNs = sensorTimestampNs,
frameNumber = result.frameNumber,
@@ -411,10 +412,27 @@ class Camera2Controller(private val context: Context) {
lensState = result.get(CaptureResult.LENS_STATE),
rollingShutterSkewNs = result.get(CaptureResult.SENSOR_ROLLING_SHUTTER_SKEW),
gyroWindow = burstGyroRecorder.exposureWindow(sensorTimestampNs, exposureTimeNs),
channelNoiseProfile = channelNoiseProfile,
multiFrameCaptureRole = result.request.tag as? MultiFrameCaptureRole,
)
}
private fun captureChannelNoiseProfile(result: CaptureResult): FloatArray? {
return result.get(CaptureResult.SENSOR_NOISE_PROFILE)
?.takeIf { it.isNotEmpty() }
?.let { profile ->
FloatArray(profile.size * 2) { coefficientIndex ->
val pair = profile[coefficientIndex / 2]
val coefficient = if ((coefficientIndex and 1) == 0) {
pair.first
} else {
pair.second
}
coefficient.toFloat().takeIf { it.isFinite() && it >= 0f } ?: 0f
}
}
}
// 快门音效播放回调
var onPlayShutterSound: (() -> Unit)? = null
@@ -5624,6 +5642,91 @@ class Camera2Controller(private val context: Context) {
return shortIso to shortShutter
}
private fun buildMultiFrameLongCaptureRequest(
device: CameraDevice,
reader: ImageReader,
state: CameraState,
baseResult: CaptureResult?,
baseRequest: CaptureRequest,
isRawCapture: Boolean,
): CaptureRequest? {
val builder = device.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE).apply {
addTarget(reader.surface)
applyBaseCameraSettings(this, isCapture = true, isRawCapture = isRawCapture)
set(
CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER,
CaptureRequest.CONTROL_AE_PRECAPTURE_TRIGGER_IDLE,
)
set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_OFF)
copyStillFocusSettingsFromPreview(this)
applyMultiFrameCaptureConsistency(
builder = this,
state = state,
baseResult = baseResult,
lockExposure = true,
isRawCapture = isRawCapture,
)
}
if (!applyMultiFrameLongExposure(builder, state, baseResult, baseRequest)) return null
builder.setTag(MultiFrameCaptureRole.LONG)
return builder.build()
}
private fun applyMultiFrameLongExposure(
builder: CaptureRequest.Builder,
state: CameraState,
baseResult: CaptureResult?,
baseRequest: CaptureRequest,
): Boolean {
val baseIso = baseRequest.get(CaptureRequest.SENSOR_SENSITIVITY)
?: baseResult?.get(CaptureResult.SENSOR_SENSITIVITY)
val baseShutter = baseRequest.get(CaptureRequest.SENSOR_EXPOSURE_TIME)
?: baseResult?.get(CaptureResult.SENSOR_EXPOSURE_TIME)
val canUseManualExposure = isManualSensorSupported &&
availableAeModes.contains(CaptureRequest.CONTROL_AE_MODE_OFF) &&
baseIso != null && baseIso > 0 &&
baseShutter != null && baseShutter > 0L
if (canUseManualExposure) {
val manualBaseIso = checkNotNull(baseIso)
val manualBaseShutter = checkNotNull(baseShutter)
val isoRange = state.getIsoRange()
val shutterRange = state.getShutterSpeedRange()
val plan = runCatching {
MultiFrameExposurePlanner.planLongExposure(
baseIso = manualBaseIso,
baseExposureTimeNs = manualBaseShutter,
isoLower = isoRange.lower,
isoUpper = isoRange.upper,
exposureTimeLowerNs = shutterRange.lower,
exposureTimeUpperNs = shutterRange.upper,
)
}.onFailure { error ->
PLog.e(TAG, "Unable to plan bounded multi-frame long exposure", error)
}.getOrNull()
if (plan != null) {
builder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_OFF)
builder.set(CaptureRequest.CONTROL_AE_LOCK, false)
builder.set(CaptureRequest.SENSOR_SENSITIVITY, plan.sensitivityIso)
builder.set(CaptureRequest.SENSOR_EXPOSURE_TIME, plan.exposureTimeNs)
PLog.i(
TAG,
"Multi-frame long request: base=ISO$manualBaseIso/${manualBaseShutter}ns " +
"long=ISO${plan.sensitivityIso}/${plan.exposureTimeNs}ns " +
"targetEv=${MultiFrameConfig.LONG_FRAME_EXPOSURE_EV} " +
"plannedEv=${plan.plannedDeltaEv} isoUpperLimited=${plan.isoUpperLimited} " +
"shutterUpperLimited=${plan.shutterUpperLimited}",
)
return true
}
}
PLog.w(
TAG,
"Manual sensor exposure unavailable for bounded multi-frame long request; " +
"long slots will remain normal so the 10ms shutter contract is not violated",
)
return false
}
private fun shouldUseDefaultHdrBracketCapture(state: CameraState, isRawCapture: Boolean): Boolean {
return state.captureMode == CaptureMode.PHOTO &&
state.useHdrComposition &&
@@ -5743,6 +5846,7 @@ class Camera2Controller(private val context: Context) {
val requestedFrameCount = currentState.multiFrameCount
val frameCount = MultiFrameConfig.normalizeFrameCount(requestedFrameCount)
val normalFrameCount = MultiFrameConfig.normalFrameCount(frameCount)
val longFrameCount = MultiFrameConfig.longFrameCount(frameCount)
if (frameCount != requestedFrameCount) {
PLog.w(
TAG,
@@ -5759,16 +5863,33 @@ class Camera2Controller(private val context: Context) {
baseRequest = baseRequest,
isRawCapture = isRawCapture,
)
val longRequest = buildMultiFrameLongCaptureRequest(
device = device,
reader = reader,
state = currentState,
baseResult = baseExposureResult,
baseRequest = baseRequest,
isRawCapture = isRawCapture,
)
val requests = buildList(MultiFrameConfig.captureFrameCount(frameCount)) {
repeat(normalFrameCount) {
add(baseRequest)
}
add(shortRequest)
repeat(longFrameCount) {
add(longRequest ?: baseRequest)
}
}
val scheduledLongFrameCount = if (longRequest != null) longFrameCount else 0
PLog.i(
TAG,
"Multi-frame burst plan: normalFrames=$normalFrameCount shortFrames=" +
"${MultiFrameConfig.SHORT_FRAME_COUNT} total=${requests.size}",
"${MultiFrameConfig.SHORT_FRAME_COUNT} " +
"longFrames=$scheduledLongFrameCount " +
"fallbackNormalFrames=${longFrameCount - scheduledLongFrameCount} " +
"longTargetEv=${MultiFrameConfig.LONG_FRAME_EXPOSURE_EV} " +
"longMaxShutterNs=${MultiFrameConfig.LONG_FRAME_MAX_EXPOSURE_TIME_NS} " +
"total=${requests.size}",
)
session.captureBurst(requests, object : CameraCaptureSession.CaptureCallback() {
@@ -6240,6 +6361,8 @@ class Camera2Controller(private val context: Context) {
rollingShutterSkewNs = frameResult.get(CaptureResult.SENSOR_ROLLING_SHUTTER_SKEW),
gyroWindow = frozenMetadata?.gyroWindow
?: burstGyroRecorder.exposureWindow(sensorTimestampNs, exposureTimeNs),
channelNoiseProfile = frozenMetadata?.channelNoiseProfile
?: captureChannelNoiseProfile(frameResult),
multiFrameCaptureRole = frozenMetadata?.multiFrameCaptureRole
?: (frameResult.request.tag as? MultiFrameCaptureRole),
)
@@ -3,6 +3,7 @@ package com.hinnka.mycamera.camera
enum class MultiFrameCaptureRole {
BASE,
SHORT,
LONG,
}
data class CapturedFrameMetadata(
@@ -15,5 +16,6 @@ data class CapturedFrameMetadata(
val lensState: Int?,
val rollingShutterSkewNs: Long?,
val gyroWindow: GyroExposureWindow?,
val channelNoiseProfile: FloatArray? = null,
val multiFrameCaptureRole: MultiFrameCaptureRole? = null,
)
@@ -1,21 +1,34 @@
package com.hinnka.mycamera.camera
object MultiFrameConfig {
const val MIN_FRAME_COUNT = 2
const val MIN_FRAME_COUNT = 3
const val MAX_FRAME_COUNT = 20
const val DEFAULT_FRAME_COUNT = 6
const val SHORT_FRAME_COUNT = 1
const val SHORT_FRAME_EXPOSURE_DIVISOR = 3.0
const val LONG_FRAME_COUNT_DIVISOR = 4
const val MIN_LONG_FRAME_COUNT = 1
const val LONG_FRAME_EXPOSURE_EV = 2.5
const val LONG_FRAME_MAX_EXPOSURE_TIME_NS = 10_000_000L
fun normalizeFrameCount(frameCount: Int): Int {
return frameCount.coerceIn(MIN_FRAME_COUNT, MAX_FRAME_COUNT)
}
fun normalFrameCount(totalFrameCount: Int): Int {
return normalizeFrameCount(totalFrameCount) - SHORT_FRAME_COUNT
val normalizedFrameCount = normalizeFrameCount(totalFrameCount)
return normalizedFrameCount - SHORT_FRAME_COUNT - longFrameCount(normalizedFrameCount)
}
fun longFrameCount(totalFrameCount: Int): Int {
return (normalizeFrameCount(totalFrameCount) / LONG_FRAME_COUNT_DIVISOR)
.coerceAtLeast(MIN_LONG_FRAME_COUNT)
}
fun captureFrameCount(totalFrameCount: Int): Int {
return normalFrameCount(totalFrameCount) + SHORT_FRAME_COUNT
val normalizedFrameCount = normalizeFrameCount(totalFrameCount)
return normalFrameCount(normalizedFrameCount) +
SHORT_FRAME_COUNT +
longFrameCount(normalizedFrameCount)
}
}
@@ -0,0 +1,70 @@
package com.hinnka.mycamera.camera
import kotlin.math.ln
import kotlin.math.min
import kotlin.math.pow
import kotlin.math.roundToInt
import kotlin.math.roundToLong
internal data class MultiFrameLongExposurePlan(
val sensitivityIso: Int,
val exposureTimeNs: Long,
val targetExposureProduct: Double,
val plannedExposureProduct: Double,
val plannedDeltaEv: Double,
val isoUpperLimited: Boolean,
val shutterUpperLimited: Boolean,
)
internal object MultiFrameExposurePlanner {
private val LN_2 = ln(2.0)
fun planLongExposure(
baseIso: Int,
baseExposureTimeNs: Long,
isoLower: Int,
isoUpper: Int,
exposureTimeLowerNs: Long,
exposureTimeUpperNs: Long,
): MultiFrameLongExposurePlan {
require(baseIso > 0 && baseExposureTimeNs > 0L) { "Base exposure must be positive" }
require(isoLower > 0 && isoUpper >= isoLower) { "Invalid ISO range" }
require(exposureTimeLowerNs > 0L && exposureTimeUpperNs >= exposureTimeLowerNs) {
"Invalid exposure-time range"
}
val maximumLongExposureTimeNs = min(
exposureTimeUpperNs,
MultiFrameConfig.LONG_FRAME_MAX_EXPOSURE_TIME_NS,
)
require(maximumLongExposureTimeNs >= exposureTimeLowerNs) {
"Sensor minimum exposure time exceeds the long-frame shutter limit"
}
val exposureMultiplier = 2.0.pow(MultiFrameConfig.LONG_FRAME_EXPOSURE_EV)
val baseExposureProduct = baseIso.toDouble() * baseExposureTimeNs.toDouble()
val targetExposureProduct = baseExposureProduct * exposureMultiplier
val unboundedExposureTimeNs = baseExposureTimeNs.toDouble() * exposureMultiplier
val initialExposureTimeNs = unboundedExposureTimeNs
.roundToLong()
.coerceIn(exposureTimeLowerNs, maximumLongExposureTimeNs)
val sensitivityIso = (targetExposureProduct / initialExposureTimeNs.toDouble())
.roundToInt()
.coerceIn(isoLower, isoUpper)
val exposureTimeNs = (targetExposureProduct / sensitivityIso.toDouble())
.roundToLong()
.coerceIn(exposureTimeLowerNs, maximumLongExposureTimeNs)
val plannedExposureProduct = sensitivityIso.toDouble() * exposureTimeNs.toDouble()
return MultiFrameLongExposurePlan(
sensitivityIso = sensitivityIso,
exposureTimeNs = exposureTimeNs,
targetExposureProduct = targetExposureProduct,
plannedExposureProduct = plannedExposureProduct,
plannedDeltaEv = ln(plannedExposureProduct / baseExposureProduct) / LN_2,
isoUpperLimited = sensitivityIso == isoUpper &&
plannedExposureProduct < targetExposureProduct,
shutterUpperLimited = unboundedExposureTimeNs > maximumLongExposureTimeNs.toDouble(),
)
}
}
@@ -9,13 +9,13 @@ internal data class RawRadianceExposurePlan(
val baseExposureProduct: Double?,
val normalIndices: IntArray,
val shortIndex: Int?,
val longIndices: IntArray,
val excludedIndices: IntArray,
) {
val acceptedIndices: IntArray
get() = if (shortIndex != null) {
intArrayOf(*normalIndices, shortIndex)
} else {
normalIndices
get() {
val shortIndices = shortIndex?.let { intArrayOf(it) } ?: IntArray(0)
return intArrayOf(*normalIndices, *shortIndices, *longIndices)
}
}
@@ -34,22 +34,34 @@ internal object RawRadianceExposurePlanner {
"Exposure products and frame roles must have the same size"
}
if (exposureProducts.isEmpty()) {
return RawRadianceExposurePlan(null, IntArray(0), null, IntArray(0))
return RawRadianceExposurePlan(
baseExposureProduct = null,
normalIndices = IntArray(0),
shortIndex = null,
longIndices = IntArray(0),
excludedIndices = IntArray(0),
)
}
val taggedShortIndices = frameRoles.indices.filter { index ->
frameRoles[index] == RawBurstFrameRole.HIGHLIGHT_SHORT
}
val taggedLongIndices = frameRoles.indices.filter { index ->
frameRoles[index] == RawBurstFrameRole.SHADOW_LONG
}
val taggedShortIndex = taggedShortIndices.firstOrNull()
val validIndices = exposureProducts.indices.filter { index ->
index !in taggedShortIndices &&
index !in taggedShortIndices && index !in taggedLongIndices &&
exposureProducts[index].isFinite() && exposureProducts[index] > 0.0
}
if (validIndices.isEmpty()) {
val normalIndices = exposureProducts.indices.filterNot(taggedShortIndices::contains)
val normalIndices = exposureProducts.indices.filter { index ->
index !in taggedShortIndices && index !in taggedLongIndices
}
return RawRadianceExposurePlan(
baseExposureProduct = null,
normalIndices = normalIndices.toIntArray(),
shortIndex = taggedShortIndex,
longIndices = taggedLongIndices.toIntArray(),
excludedIndices = taggedShortIndices.drop(1).toIntArray(),
)
}
@@ -57,7 +69,7 @@ internal object RawRadianceExposurePlanner {
val sortedExposures = validIndices.map(exposureProducts::get).sorted()
val baseExposure = sortedExposures[sortedExposures.size / 2]
val normalIndices = exposureProducts.indices.filter { index ->
if (index in taggedShortIndices) return@filter false
if (index in taggedShortIndices || index in taggedLongIndices) return@filter false
val exposure = exposureProducts[index]
!exposure.isFinite() || exposure <= 0.0 ||
exposureDeltaEv(exposure, baseExposure) <= NORMAL_EXPOSURE_TOLERANCE_EV
@@ -73,12 +85,13 @@ internal object RawRadianceExposurePlanner {
.minByOrNull { (_, targetDeltaEv) -> targetDeltaEv }
?.first
val excludedIndices = exposureProducts.indices.filter { index ->
index !in normalIndices && index != shortIndex
index !in normalIndices && index != shortIndex && index !in taggedLongIndices
}
return RawRadianceExposurePlan(
baseExposureProduct = baseExposure,
normalIndices = normalIndices.toIntArray(),
shortIndex = shortIndex,
longIndices = taggedLongIndices.toIntArray(),
excludedIndices = excludedIndices.toIntArray(),
)
}
@@ -99,6 +112,15 @@ internal data class RawRadianceHighlightFrame(
val baselineExposureEv: Float,
)
internal data class RawRadianceLongFramePlan(
val sourceFrameIndex: Int,
val longFrame: RawStackFrame,
val anchorFrameIndex: Int,
val exposureRatio: Float,
val exposureScale: Float,
val exposureDeltaEv: Float,
)
internal fun hasRadianceHighlightAlignmentSupport(
validTileCount: Int,
coveredQuadrants: Int,
@@ -109,11 +131,49 @@ internal fun hasRadianceHighlightAlignmentSupport(
coveredQuadrants >= minimumQuadrants.coerceIn(1, 4)
}
internal data class RawRadianceLongAdmission(
val frameWeight: Float,
val eligibleCoverage: Float,
val requiredQuadrants: Int,
)
internal fun planRadianceLongAdmission(
validTileCount: Int,
coveredQuadrants: Int,
eligibleTileCount: Int,
eligibleQuadrants: Int,
minimumValidTiles: Int,
minimumQuadrants: Int,
minimumEligibleCoverage: Float,
): RawRadianceLongAdmission {
val eligibleCoverage = if (eligibleTileCount > 0) {
validTileCount.toFloat() / eligibleTileCount
} else {
0f
}
val normalizedEligibleQuadrants = eligibleQuadrants.coerceIn(0, 4)
val requiredQuadrants = minOf(
minimumQuadrants.coerceIn(1, 4),
normalizedEligibleQuadrants,
).coerceAtLeast(2)
val accepted = normalizedEligibleQuadrants >= 2 &&
validTileCount >= minimumValidTiles.coerceAtLeast(1) &&
coveredQuadrants >= requiredQuadrants &&
eligibleCoverage >= minimumEligibleCoverage.coerceIn(0f, 1f)
return RawRadianceLongAdmission(
frameWeight = if (accepted) 1f else 0f,
eligibleCoverage = eligibleCoverage,
requiredQuadrants = requiredQuadrants,
)
}
/**
* Unified same-exposure RAW fusion.
*
* One optional one-third-exposure frame is identified and reserved for the dedicated
* highlight-reconstruction stage. It must not participate in normal MFNR/MFSR accumulation.
* highlight-reconstruction stage. Tagged long exposures are also isolated and planned against
* the nearest accepted normal anchor. They enter Radiance RGB through an exposure-normalized,
* noise-weighted auxiliary path and may not participate in the normal MFNR/MFSR frame cluster.
*
* The reconstruction algorithm is independent of output scale: every accepted RAW frame
* contributes a wide-kernel denoise estimate and a narrow-kernel detail estimate in the
@@ -152,8 +212,10 @@ class GlesRawRadianceFusion(
frames[index].copy(role = RawBurstFrameRole.NORMAL)
}
val shortFrame = exposurePlan.shortIndex?.let(frames::get)
val longFrames = exposurePlan.longIndices.map(frames::get)
if (fusionFrames.isEmpty()) {
shortFrame?.image?.close()
longFrames.forEach { it.image.close() }
return null
}
if (exposurePlan.excludedIndices.isNotEmpty()) {
@@ -190,6 +252,37 @@ class GlesRawRadianceFusion(
PLog.w(TAG, "Radiance fusion received no valid one-third short frame")
}
val longFramePlans = longFrames.mapIndexedNotNull { planIndex, longFrame ->
val sourceFrameIndex = exposurePlan.longIndices[planIndex]
val longFramePlan = createLongFramePlan(
sourceFrameIndex = sourceFrameIndex,
normalFrames = fusionFrames,
longFrame = longFrame,
baseExposureProduct = exposurePlan.baseExposureProduct,
)
if (longFramePlan != null) {
PLog.i(
TAG,
"Radiance long fusion plan index=$sourceFrameIndex " +
"exposure=${longFramePlan.longFrame.exposureProduct} " +
"base=${exposurePlan.baseExposureProduct} " +
"actualRatio=${longFramePlan.exposureRatio} " +
"exposureScale=${longFramePlan.exposureScale} " +
"actualDeltaEv=${longFramePlan.exposureDeltaEv} " +
"anchor=${longFramePlan.anchorFrameIndex}",
)
longFramePlan
} else {
PLog.w(
TAG,
"Radiance long frame index=$sourceFrameIndex has no usable actual " +
"exposure ratio; excluded",
)
longFrame.image.close()
null
}
}
val tuning = RawStackTuningResolver.resolve(
mode = RawStackMode.MFSR,
frameCount = exposurePlan.normalIndices.size,
@@ -211,7 +304,11 @@ class GlesRawRadianceFusion(
debugConfig = debugConfig,
fusionPipeline = RawFusionPipeline.RADIANCE_RGB,
radianceFusionTuning = fusionTuning,
).processFrames(fusionFrames, highlightFrame)
).processFrames(
frames = fusionFrames,
highlightFrame = highlightFrame,
longFrames = longFramePlans,
)
}
private fun createHighlightFrame(
@@ -243,6 +340,39 @@ class GlesRawRadianceFusion(
)
}
private fun createLongFramePlan(
sourceFrameIndex: Int,
normalFrames: List<RawStackFrame>,
longFrame: RawStackFrame,
baseExposureProduct: Double?,
): RawRadianceLongFramePlan? {
if (normalFrames.isEmpty()) return null
val normalExposure = baseExposureProduct
?.takeIf { it.isFinite() && it > 0.0 }
?: return null
val longExposure = longFrame.exposureProduct
.takeIf { it.isFinite() && it > 0.0 }
?: return null
val exposureRatio = (longExposure / normalExposure).toFloat()
if (!exposureRatio.isFinite() ||
exposureRatio < fusionTuning.longMinExposureRatio ||
exposureRatio > fusionTuning.longMaxExposureRatio
) {
return null
}
val anchorIndex = normalFrames.indices.minByOrNull { index ->
timestampDistance(normalFrames[index].sensorTimestampNs, longFrame.sensorTimestampNs)
} ?: return null
return RawRadianceLongFramePlan(
sourceFrameIndex = sourceFrameIndex,
longFrame = longFrame.copy(role = RawBurstFrameRole.SHADOW_LONG),
anchorFrameIndex = anchorIndex,
exposureRatio = exposureRatio,
exposureScale = 1f / exposureRatio,
exposureDeltaEv = (ln(exposureRatio.toDouble()) / ln(2.0)).toFloat(),
)
}
private fun timestampDistance(first: Long, second: Long): Long {
if (first <= 0L || second <= 0L) return Long.MAX_VALUE
return if (first >= second) first - second else second - first
@@ -276,6 +406,21 @@ data class RawRadianceFusionTuning(
val detailChromaStrength: Float = 0.0f,
val chromaConsistencySigmaStart: Float = 3.0f,
val chromaConsistencySigmaFull: Float = 6.0f,
val longMinExposureRatio: Float = 1.05f,
val longMaxExposureRatio: Float = 64.0f,
val longClipStart: Float = 0.90f,
val longClipFull: Float = 0.985f,
val longPrecisionWeightCap: Float = 24.0f,
val longDetailWeightScale: Float = 0.25f,
val longMergeFactorTarget: Float = 0.70f,
val longFlowFbConsistencyStartPx: Float = 0.75f,
val longFlowFbConsistencyFullPx: Float = 2.0f,
val longFlowMinimumConfidence: Float = 0.01f,
val longFlowMinimumValidTiles: Int = 64,
val longFlowMinimumQuadrants: Int = 3,
val longEligibilityMinimumSupport: Float = 0.02f,
val longEligibilityMinimumTilesPerQuadrant: Int = 16,
val longFlowMinimumEligibleCoverage: Float = 0.01f,
val highlightMinExposureRatio: Float = 1.2f,
val highlightMaxExposureRatio: Float = 8.0f,
val highlightNormalClipStart: Float = 0.90f,
@@ -313,6 +458,79 @@ internal fun radianceDebugNeutralRgb(calculationWbGains: FloatArray): FloatArray
}
internal object GlesRawRadianceFusionShaders {
val longEligibility: String = """
#version 300 es
precision highp float;
precision highp int;
uniform sampler2D uReferenceProxy;
uniform sampler2D uCurrentProxy;
uniform sampler2D uComposedFlow;
uniform ivec2 uGridSize;
uniform ivec2 uPlaneSize;
uniform int uTileSize;
out vec4 fragColor;
vec4 proxyAt(sampler2D proxyTexture, vec2 planePosition) {
vec2 uv = (clamp(
planePosition,
vec2(0.0),
vec2(uPlaneSize - ivec2(1))
) + vec2(0.5)) / vec2(uPlaneSize);
return texture(proxyTexture, uv);
}
bool insidePlane(vec2 planePosition) {
return planePosition.x >= 0.0 && planePosition.y >= 0.0 &&
planePosition.x <= float(uPlaneSize.x - 1) &&
planePosition.y <= float(uPlaneSize.y - 1);
}
void main() {
ivec2 tile = ivec2(gl_FragCoord.xy);
vec2 center = min(
vec2(tile * uTileSize + ivec2(uTileSize / 2)),
vec2(uPlaneSize - ivec2(1))
);
vec2 flow = texelFetch(
uComposedFlow,
clamp(tile, ivec2(0), uGridSize - ivec2(1)),
0
).rg;
int sampleSpacing = max(uTileSize / 4, 1);
float eligibilitySum = 0.0;
float validitySum = 0.0;
float observabilitySum = 0.0;
float insideSum = 0.0;
float sampleCount = 0.0;
for (int y = -2; y <= 2; ++y) {
for (int x = -2; x <= 2; ++x) {
vec2 referencePosition = center +
vec2(x * sampleSpacing, y * sampleSpacing);
vec2 currentPosition = referencePosition + flow;
bool inside = insidePlane(referencePosition) &&
insidePlane(currentPosition);
vec4 referenceSample = proxyAt(uReferenceProxy, referencePosition);
vec4 currentSample = proxyAt(uCurrentProxy, currentPosition);
float validity = inside ?
min(referenceSample.g, currentSample.g) : 0.0;
float observability = inside ?
min(referenceSample.b, currentSample.b) : 0.0;
eligibilitySum += validity * observability;
validitySum += validity;
observabilitySum += observability;
insideSum += inside ? 1.0 : 0.0;
sampleCount += 1.0;
}
}
fragColor = vec4(
eligibilitySum,
validitySum,
observabilitySum,
insideSum
) / max(sampleCount, 1.0);
}
""".trimIndent()
val validateHighlightFlow: String = """
#version 300 es
precision highp float;
@@ -520,7 +738,10 @@ internal object GlesRawRadianceFusionShaders {
}
""".trimIndent()
fun clearAccumulator(trackRejections: Boolean): String {
fun clearAccumulator(
trackRejections: Boolean,
trackLongParticipation: Boolean = false,
): String {
val rejectionDeclaration = if (trackRejections) {
"layout(rgba16f, binding = 6) writeonly uniform highp image2D uFusionRejections;"
} else {
@@ -531,6 +752,16 @@ internal object GlesRawRadianceFusionShaders {
} else {
""
}
val longParticipationDeclaration = if (trackLongParticipation) {
"layout(r32ui, binding = 7) writeonly uniform highp uimage2D uLongParticipation;"
} else {
""
}
val longParticipationClear = if (trackLongParticipation) {
"imageStore(uLongParticipation, p, uvec4(0u));"
} else {
""
}
return """
#version 310 es
precision highp float;
@@ -543,6 +774,7 @@ internal object GlesRawRadianceFusionShaders {
layout(r32ui, binding = 4) writeonly uniform highp uimage2D uDetailSumBw;
layout(r32ui, binding = 5) writeonly uniform highp uimage2D uDetailWeightRg;
$rejectionDeclaration
$longParticipationDeclaration
uniform ivec2 uImageSize;
void main() {
@@ -555,6 +787,7 @@ internal object GlesRawRadianceFusionShaders {
imageStore(uDetailSumBw, p, uvec4(0u));
imageStore(uDetailWeightRg, p, uvec4(0u));
$rejectionClear
$longParticipationClear
}
""".trimIndent()
}
@@ -563,6 +796,7 @@ internal object GlesRawRadianceFusionShaders {
rawCommon: String,
trackRejections: Boolean = false,
trackParticipation: Boolean = false,
trackLongParticipation: Boolean = false,
): String {
val rejectionDeclaration = if (trackRejections) {
"""
@@ -637,6 +871,11 @@ internal object GlesRawRadianceFusionShaders {
} else {
""
}
val longParticipationDeclaration = if (trackLongParticipation) {
"layout(r32ui, binding = 7) writeonly uniform highp uimage2D uLongParticipation;"
} else {
""
}
val participationDeclaration = if (trackParticipation) {
"""
layout(std430, binding = 0) buffer RadianceFusionParticipationStats {
@@ -678,14 +917,14 @@ internal object GlesRawRadianceFusionShaders {
}
atomicAdd(
uFusionParticipationStats[offset + 2],
uint(floor(clamp(meanNrWeight, 0.0, 1.0) * FUSION_WEIGHT_QUANTIZATION + 0.5))
uint(floor(clamp(meanNrWeight, 0.0, 32.0) * FUSION_WEIGHT_QUANTIZATION + 0.5))
);
if (meanDetailWeight > 1e-7) {
atomicAdd(uFusionParticipationStats[offset + 3], 1u);
}
atomicAdd(
uFusionParticipationStats[offset + 4],
uint(floor(clamp(meanDetailWeight, 0.0, 1.0) * FUSION_WEIGHT_QUANTIZATION + 0.5))
uint(floor(clamp(meanDetailWeight, 0.0, 32.0) * FUSION_WEIGHT_QUANTIZATION + 0.5))
);
}
""".trimIndent()
@@ -747,6 +986,13 @@ internal object GlesRawRadianceFusionShaders {
65.0,
1.0 - min(channelConsistency.r, channelConsistency.b)
);
// reason 13: a long-exposure RAW sample is saturated before radiometric scaling.
considerFusionRejection(
fusionRejection,
13.0,
115.0,
uIsLongFrame != 0 ? 1.0 - longClipConfidence : 0.0
);
""".trimIndent()
} else {
""
@@ -767,6 +1013,15 @@ internal object GlesRawRadianceFusionShaders {
} else {
""
}
val longParticipationStore = if (trackLongParticipation) {
"""
if (uIsLongFrame != 0 && any(greaterThan(nrWeight, vec3(1e-7)))) {
imageStore(uLongParticipation, accumulatorP, uvec4(1u));
}
""".trimIndent()
} else {
""
}
return """
#version 310 es
$rawCommon
@@ -778,6 +1033,7 @@ internal object GlesRawRadianceFusionShaders {
uniform sampler2D uKernel;
uniform sampler2D uLensShadingMap;
uniform sampler2D uReferenceBase;
uniform highp usampler2D uRawRegion;
layout(r32ui, binding = 0) uniform highp uimage2D uNrSumRg;
layout(r32ui, binding = 1) uniform highp uimage2D uNrSumBw;
layout(r32ui, binding = 2) uniform highp uimage2D uNrWeightRg;
@@ -785,6 +1041,7 @@ internal object GlesRawRadianceFusionShaders {
layout(r32ui, binding = 4) uniform highp uimage2D uDetailSumBw;
layout(r32ui, binding = 5) uniform highp uimage2D uDetailWeightRg;
$rejectionDeclaration
$longParticipationDeclaration
$participationDeclaration
uniform ivec2 uImageSize;
uniform ivec2 uSourceSize;
@@ -794,15 +1051,25 @@ internal object GlesRawRadianceFusionShaders {
uniform ivec2 uGridSize;
uniform int uTileSize;
uniform int uIsReference;
uniform int uIsLongFrame;
uniform int uSemanticEncoding;
uniform int uCfaPattern;
uniform float uBlackLevel[4];
uniform float uWhiteLevel;
uniform float uNoiseAlphaByChannel[4];
uniform float uNoiseBetaByChannel[4];
uniform float uReferenceNoiseAlpha[4];
uniform float uReferenceNoiseBeta[4];
uniform float uCurrentNoiseAlpha[4];
uniform float uCurrentNoiseBeta[4];
uniform float uFrameWeight;
uniform float uExposureScale;
uniform float uRegistrationNrWeight;
uniform float uRegistrationDetailWeight;
uniform float uLongClipStart;
uniform float uLongClipFull;
uniform float uLongPrecisionWeightCap;
uniform float uLongDetailWeightScale;
uniform float uDenoiseSigmaRawPx;
uniform float uDenoiseSteeringStrength;
uniform float uRobustnessSpatialMix;
@@ -950,22 +1217,59 @@ internal object GlesRawRadianceFusionShaders {
return DualRgb(detail, denoiseSum / max(denoiseWeightSum, 1e-6));
}
float noiseVariance(float signal, int bayerIndex, float lscGainForNoise) {
float alpha = uNoiseAlphaByChannel[bayerIndex];
float beta = uNoiseBetaByChannel[bayerIndex];
float noiseVariance(
float signal,
int bayerIndex,
float lscGainForNoise,
bool referenceModel
) {
float alpha = referenceModel ?
uReferenceNoiseAlpha[bayerIndex] : uCurrentNoiseAlpha[bayerIndex];
float beta = referenceModel ?
uReferenceNoiseBeta[bayerIndex] : uCurrentNoiseBeta[bayerIndex];
if (alpha <= 0.0 && beta <= 0.0) return 1e-10;
float gain = clamp(lscGainForNoise, 1e-3, max(uLscNoiseGainMax, 1.0));
return max(alpha * clamp(signal, 0.0, 1.0) * gain + beta * gain * gain, 1e-10);
}
vec3 cameraNoiseVariance(vec3 signal, vec4 lsc) {
vec3 cameraNoiseVariance(vec3 signal, vec4 lsc, bool referenceModel) {
return vec3(
noiseVariance(signal.r, 0, lsc.r),
noiseVariance(signal.r, 0, lsc.r, referenceModel),
0.5 * (
noiseVariance(signal.g, 1, lsc.g) +
noiseVariance(signal.g, 2, lsc.b)
noiseVariance(signal.g, 1, lsc.g, referenceModel) +
noiseVariance(signal.g, 2, lsc.b, referenceModel)
),
noiseVariance(signal.b, 3, lsc.a)
noiseVariance(signal.b, 3, lsc.a, referenceModel)
);
}
float rawSensorNormAt(ivec2 globalSample) {
globalSample = clamp(globalSample, ivec2(0), uImageSize - ivec2(1));
ivec2 localSample = clamp(
globalSample - uSourceOrigin,
ivec2(0),
uSourceSize - ivec2(1)
);
int bayerIndex = bayerIndexAt(uCfaPattern, globalSample);
float raw = float(texelFetch(uRawRegion, localSample, 0).r);
float range = max(uWhiteLevel - uBlackLevel[bayerIndex], 1.0);
return clamp(max(raw - uBlackLevel[bayerIndex], 0.0) / range, 0.0, 1.0);
}
float longHeadroomConfidence(vec2 sourceRaw) {
if (uIsLongFrame == 0) return 1.0;
ivec2 center = ivec2(round(sourceRaw));
ivec2 phaseOrigin = center - ivec2(center.x & 1, center.y & 1);
float rawPeak = 0.0;
for (int y = 0; y <= 1; ++y) {
for (int x = 0; x <= 1; ++x) {
rawPeak = max(rawPeak, rawSensorNormAt(phaseOrigin + ivec2(x, y)));
}
}
return 1.0 - smoothstep(
min(uLongClipStart, uLongClipFull),
max(uLongClipStart, uLongClipFull),
rawPeak
);
}
@@ -1048,7 +1352,13 @@ internal object GlesRawRadianceFusionShaders {
vec2 kernelUv = (clamp(planePos, vec2(0.0), vec2(uPlaneSize - ivec2(1))) +
vec2(0.5)) / vec2(uPlaneSize);
vec4 steeringKernel = texture(uKernel, kernelUv);
DualRgb rgb = reconstructRgb(sourceRaw, steeringKernel);
DualRgb sourceRgb = reconstructRgb(sourceRaw, steeringKernel);
float longClipConfidence = longHeadroomConfidence(sourceRaw);
float radiometricScale = max(uExposureScale, 1e-4);
DualRgb rgb = DualRgb(
clamp(sourceRgb.detail * radiometricScale, 0.0, 1.0),
clamp(sourceRgb.denoise * radiometricScale, 0.0, 1.0)
);
float signal = dot(rgb.denoise, vec3(0.2126, 0.7152, 0.0722));
ivec2 nearestSource = clamp(ivec2(round(sourceRaw)), ivec2(0), uImageSize - ivec2(1));
vec4 lsc = lscAt(nearestSource);
@@ -1077,14 +1387,29 @@ internal object GlesRawRadianceFusionShaders {
uImageSize - ivec2(1)
);
vec4 referenceLsc = lscAt(nearestReference);
vec3 referenceVariance = cameraNoiseVariance(referenceRgb, referenceLsc);
vec3 referenceVariance = cameraNoiseVariance(
referenceRgb,
referenceLsc,
true
);
// Both estimates use the reference signal so shot-noise weighting cannot be
// biased by the current frame's random positive/negative noise excursion.
vec3 currentExpectedVariance = cameraNoiseVariance(referenceRgb, lsc);
vec3 currentExpectedSignal = clamp(
referenceRgb / max(radiometricScale, 1e-4),
0.0,
1.0
);
vec3 currentExpectedVariance = cameraNoiseVariance(
currentExpectedSignal,
lsc,
false
) * radiometricScale * radiometricScale;
float precisionUpper = uIsLongFrame != 0 ?
max(uLongPrecisionWeightCap, 1.0) : 4.0;
channelPrecision = clamp(
referenceVariance / max(currentExpectedVariance, vec3(1e-10)),
vec3(0.25),
vec3(4.0)
vec3(precisionUpper)
);
vec2 chromaConfidence = opponentChromaConfidence(
referenceRgb,
@@ -1103,25 +1428,29 @@ internal object GlesRawRadianceFusionShaders {
);
float sharedConsistency = min(chromaConfidence.x, chromaConfidence.y);
float sharedHighlight = min(min(highlight.r, highlight.g), highlight.b);
float longNrRoleWeight = uIsLongFrame != 0 ? longClipConfidence : 1.0;
float longDetailRoleWeight = uIsLongFrame != 0 ?
longClipConfidence * clamp(uLongDetailWeightScale, 0.0, 1.0) : 1.0;
float nrCoverage = clamp(tileConfidence.r * robust, 0.0, 1.0) *
clamp(uRegistrationNrWeight, 0.0, 1.0);
float detailCoverage = clamp(tileConfidence.g * robust, 0.0, 1.0);
$rejectionAssignment
float staticConfidence = denoiseStaticConfidence(tileConfidence.r, robust);
float sharedNrWeight = uFrameWeight * sharedPrecision * sharedConsistency *
nrCoverage * sharedHighlight * mix(
nrCoverage * sharedHighlight * longNrRoleWeight * mix(
1.0,
max(uDenoiseNonReferenceWeightBoost, 1.0),
shadow * staticConfidence
);
float sharedDetailWeight = uFrameWeight * sharedPrecision * sharedConsistency *
detailCoverage * sharedHighlight *
clamp(uRegistrationDetailWeight, 0.0, 1.0);
clamp(uRegistrationDetailWeight, 0.0, 1.0) * longDetailRoleWeight;
nrWeight = vec3(sharedNrWeight);
detailWeight = vec3(sharedDetailWeight);
}
$rejectionStore
$participationStore
$longParticipationStore
if (any(greaterThan(nrWeight, vec3(1e-7)))) {
vec2 nrRg = unpackHalf2x16(imageLoad(uNrSumRg, accumulatorP).r);
@@ -1210,6 +1539,7 @@ internal object GlesRawRadianceFusionShaders {
showSrDetail: Boolean = false,
reconstructHighlights: Boolean = false,
showHighlightReconstruction: Boolean = false,
showLongParticipation: Boolean = false,
): String {
val rejectionUniforms = if (showRejections) {
"""
@@ -1225,6 +1555,11 @@ internal object GlesRawRadianceFusionShaders {
} else {
""
}
val longParticipationUniform = if (showLongParticipation) {
"uniform highp usampler2D uLongParticipation;"
} else {
""
}
val highlightDeclarations = if (reconstructHighlights) {
"""
uniform sampler2D uHighlightRgb;
@@ -1469,6 +1804,8 @@ internal object GlesRawRadianceFusionShaders {
displayColor = vec3(0.45, 1.0, 0.0); // highlight suppression: lime
} else if (reasonId < 12.5) {
displayColor = vec3(1.0, 0.0, 1.0); // opponent chroma: magenta
} else if (reasonId < 13.5) {
displayColor = vec3(1.0, 0.55, 0.0); // saturated long RAW: amber
}
if (reasonId > 0.0 && displayStrength > 0.0) {
rgb = mix(
@@ -1492,6 +1829,15 @@ internal object GlesRawRadianceFusionShaders {
} else {
""
}
val longParticipationOverlay = if (showLongParticipation) {
"""
if (texelFetch(uLongParticipation, p, 0).r != 0u) {
rgb = vec3(0.0, 1.0, 0.0);
}
""".trimIndent()
} else {
""
}
return """
#version 300 es
precision highp float;
@@ -1507,6 +1853,7 @@ internal object GlesRawRadianceFusionShaders {
uniform highp usampler2D uDetailWeightRg;
uniform sampler2D uReferenceBase;
$rejectionUniforms
$longParticipationUniform
$debugColorUniform
$highlightDeclarations
uniform float uNoiseAlphaByChannel[4];
@@ -1787,6 +2134,7 @@ internal object GlesRawRadianceFusionShaders {
$highlightComposition
$srDetailOverlay
$rejectionOverlay
$longParticipationOverlay
fragColor = uvec4(
uvec3(floor(rgb * 65535.0 + vec3(0.5))),
65535u
File diff suppressed because it is too large. Load diff
@@ -6,6 +6,7 @@ import com.hinnka.mycamera.model.SafeImage
enum class RawBurstFrameRole {
NORMAL,
HIGHLIGHT_SHORT,
SHADOW_LONG,
HDR_SHORT,
HDR_LONG,
}
@@ -61,6 +62,7 @@ data class RawStackFrame(
val lensState: Int? = null,
val rollingShutterSkewNs: Long? = null,
val gyroWindow: GyroExposureWindow? = null,
val channelNoiseProfile: FloatArray? = null,
val role: RawBurstFrameRole = RawBurstFrameRole.NORMAL,
val preAlignmentToReference: RawFramePreAlignment? = null,
)
@@ -9,6 +9,7 @@ data class RawStackDebugConfig(
val radianceFusionRejectionFrameOrdinal: Int = 0,
val visualizeRadianceSrDetail: Boolean = false,
val visualizeRadianceHighlightReconstruction: Boolean = false,
val visualizeRadianceLongParticipation: Boolean = false,
val logRadianceFusionParticipation: Boolean = false,
val radianceFusionStatsSampleStep: Int = 8,
val sampleStep: Int = 16,
@@ -57,6 +58,8 @@ data class RawStackDebugConfig(
visualizeRadianceSrDetail = ENABLE_RADIANCE_SR_DETAIL_OVERLAY,
visualizeRadianceHighlightReconstruction =
ENABLE_RADIANCE_HIGHLIGHT_RECONSTRUCTION_OVERLAY,
visualizeRadianceLongParticipation =
ENABLE_RADIANCE_LONG_PARTICIPATION_OVERLAY,
logRadianceFusionParticipation = ENABLE_RADIANCE_FUSION_PARTICIPATION_LOG,
)
}
@@ -64,6 +67,7 @@ data class RawStackDebugConfig(
private const val ENABLE_RADIANCE_FUSION_REJECTION_OVERLAY = false
private const val ENABLE_RADIANCE_SR_DETAIL_OVERLAY = false
private const val ENABLE_RADIANCE_HIGHLIGHT_RECONSTRUCTION_OVERLAY = false
private const val ENABLE_RADIANCE_LONG_PARTICIPATION_OVERLAY = false
private const val ENABLE_RADIANCE_FUSION_PARTICIPATION_LOG = true
}
}
@@ -1493,6 +1493,7 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
"accepted=${orderedFrames.size}, " +
"normal=${orderedNormalFrames.size}, " +
"short=${if (exposurePlan.shortIndex != null) 1 else 0}, " +
"long=${exposurePlan.longIndices.size}, " +
"rejected=${gyroSelection.rejectedIndices.joinToString()}, " +
"cost=${referencePlan?.referenceCost}",
)
@@ -5127,11 +5128,13 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
rollingShutterSkewNs = metadata?.rollingShutterSkewNs
?: captureResult?.get(CaptureResult.SENSOR_ROLLING_SHUTTER_SKEW),
gyroWindow = metadata?.gyroWindow,
channelNoiseProfile = metadata?.channelNoiseProfile,
role = when (
metadata?.multiFrameCaptureRole
?: (captureResult?.request?.tag as? MultiFrameCaptureRole)
) {
MultiFrameCaptureRole.SHORT -> RawBurstFrameRole.HIGHLIGHT_SHORT
MultiFrameCaptureRole.LONG -> RawBurstFrameRole.SHADOW_LONG
else -> RawBurstFrameRole.NORMAL
},
)
@@ -1,6 +1,7 @@
package com.hinnka.mycamera.camera
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Test
class MultiFrameConfigTest {
@@ -17,11 +18,12 @@ class MultiFrameConfigTest {
}
@Test
fun configuredFrameCountIncludesOneShortFrame() {
fun configuredFrameCountIsPartitionedWithoutGrowingBurst() {
assertEquals(
MultiFrameConfig.DEFAULT_FRAME_COUNT - 1,
4,
MultiFrameConfig.normalFrameCount(MultiFrameConfig.DEFAULT_FRAME_COUNT),
)
assertEquals(1, MultiFrameConfig.longFrameCount(MultiFrameConfig.DEFAULT_FRAME_COUNT))
assertEquals(
MultiFrameConfig.DEFAULT_FRAME_COUNT,
MultiFrameConfig.captureFrameCount(MultiFrameConfig.DEFAULT_FRAME_COUNT),
@@ -30,5 +32,29 @@ class MultiFrameConfigTest {
MultiFrameConfig.MAX_FRAME_COUNT,
MultiFrameConfig.captureFrameCount(Int.MAX_VALUE),
)
assertEquals(5, MultiFrameConfig.longFrameCount(Int.MAX_VALUE))
assertEquals(14, MultiFrameConfig.normalFrameCount(Int.MAX_VALUE))
}
@Test
fun minimumBurstRetainsOneFrameForEveryExposureRole() {
assertEquals(1, MultiFrameConfig.normalFrameCount(0))
assertEquals(1, MultiFrameConfig.SHORT_FRAME_COUNT)
assertEquals(1, MultiFrameConfig.longFrameCount(0))
assertEquals(3, MultiFrameConfig.captureFrameCount(0))
}
@Test
fun everySupportedCountPreservesTheConfiguredTotal() {
for (frameCount in MultiFrameConfig.MIN_FRAME_COUNT..MultiFrameConfig.MAX_FRAME_COUNT) {
assertEquals(frameCount, MultiFrameConfig.captureFrameCount(frameCount))
assertEquals(
frameCount,
MultiFrameConfig.normalFrameCount(frameCount) +
MultiFrameConfig.SHORT_FRAME_COUNT +
MultiFrameConfig.longFrameCount(frameCount),
)
assertTrue(MultiFrameConfig.normalFrameCount(frameCount) >= 1)
}
}
}
@@ -0,0 +1,44 @@
package com.hinnka.mycamera.camera
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test
class MultiFrameExposurePlannerTest {
@Test
fun longExposureUsesIsoAfterTenMillisecondShutterCap() {
val plan = MultiFrameExposurePlanner.planLongExposure(
baseIso = 100,
baseExposureTimeNs = 3_000_000L,
isoLower = 50,
isoUpper = 6400,
exposureTimeLowerNs = 100_000L,
exposureTimeUpperNs = 1_000_000_000L,
)
assertTrue(plan.exposureTimeNs <= MultiFrameConfig.LONG_FRAME_MAX_EXPOSURE_TIME_NS)
assertEquals(170, plan.sensitivityIso)
assertEquals(MultiFrameConfig.LONG_FRAME_EXPOSURE_EV, plan.plannedDeltaEv, 0.01)
assertFalse(plan.isoUpperLimited)
assertTrue(plan.shutterUpperLimited)
}
@Test
fun isoLimitProducesAnHonestActualExposureDelta() {
val plan = MultiFrameExposurePlanner.planLongExposure(
baseIso = 800,
baseExposureTimeNs = 5_000_000L,
isoLower = 50,
isoUpper = 1600,
exposureTimeLowerNs = 100_000L,
exposureTimeUpperNs = 1_000_000_000L,
)
assertEquals(10_000_000L, plan.exposureTimeNs)
assertEquals(1600, plan.sensitivityIso)
assertEquals(2.0, plan.plannedDeltaEv, 1e-6)
assertTrue(plan.isoUpperLimited)
assertTrue(plan.shutterUpperLimited)
}
}
@@ -1,9 +1,12 @@
package com.hinnka.mycamera.processor
import org.junit.Assert.assertArrayEquals
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Assume.assumeTrue
import org.junit.Test
import java.io.File
class GlesRawRadianceFusionShadersTest {
@Test
@@ -64,6 +67,33 @@ class GlesRawRadianceFusionShadersTest {
assertFalse(productionShader.contains("rgb = vec3(0.0, 1.0, 0.0)"))
}
@Test
fun longParticipationDebugColorsOnlyPositiveFinalNrWeightGreen() {
val clear = GlesRawRadianceFusionShaders.clearAccumulator(
trackRejections = false,
trackLongParticipation = true,
)
val accumulate = GlesRawRadianceFusionShaders.accumulate(
rawCommon = "",
trackLongParticipation = true,
)
val normalize = GlesRawRadianceFusionShaders.normalize(
showRejections = false,
showLongParticipation = true,
)
assertTrue(clear.contains("imageStore(uLongParticipation, p, uvec4(0u))"))
assertTrue(accumulate.contains("uIsLongFrame != 0"))
assertTrue(accumulate.contains("any(greaterThan(nrWeight, vec3(1e-7)))"))
assertTrue(
accumulate.contains(
"imageStore(uLongParticipation, accumulatorP, uvec4(1u))",
),
)
assertTrue(normalize.contains("texelFetch(uLongParticipation, p, 0).r != 0u"))
assertTrue(normalize.contains("rgb = vec3(0.0, 1.0, 0.0)"))
}
@Test
fun debugNeutralIsInverseWbNormalizedToSensorCeiling() {
val neutral = radianceDebugNeutralRgb(floatArrayOf(2f, 1f, 1f, 4f))
@@ -143,6 +173,103 @@ class GlesRawRadianceFusionShadersTest {
assertFalse(shader.contains("float sensorPrecision"))
}
@Test
fun longFramesNormalizeRadianceAndNoiseFromActualExposure() {
val shader = GlesRawRadianceFusionShaders.accumulate("", trackRejections = false)
assertTrue(shader.contains("uniform float uExposureScale"))
assertTrue(shader.contains("sourceRgb.detail * radiometricScale"))
assertTrue(shader.contains("sourceRgb.denoise * radiometricScale"))
assertTrue(shader.contains("uniform float uReferenceNoiseAlpha[4]"))
assertTrue(shader.contains("uniform float uCurrentNoiseAlpha[4]"))
assertTrue(shader.contains("referenceRgb / max(radiometricScale, 1e-4)"))
assertTrue(shader.contains("radiometricScale * radiometricScale"))
assertTrue(shader.contains("uLongPrecisionWeightCap"))
}
@Test
fun saturatedLongRawIsRejectedBeforeExposureScaling() {
val shader = GlesRawRadianceFusionShaders.accumulate("", trackRejections = true)
assertTrue(shader.contains("uniform highp usampler2D uRawRegion"))
assertTrue(shader.contains("float longHeadroomConfidence(vec2 sourceRaw)"))
assertTrue(shader.contains("rawSensorNormAt(phaseOrigin + ivec2(x, y))"))
assertTrue(shader.contains("float longClipConfidence = longHeadroomConfidence(sourceRaw)"))
assertTrue(shader.contains("longClipConfidence * clamp(uLongDetailWeightScale"))
assertTrue(shader.contains("13.0,"))
assertTrue(shader.contains("uIsLongFrame != 0 ? 1.0 - longClipConfidence"))
}
@Test
fun longEligibilityRequiresUnclippedObservableSupportAtComposedFlow() {
val shader = GlesRawRadianceFusionShaders.longEligibility
assertTrue(shader.contains("uniform sampler2D uReferenceProxy"))
assertTrue(shader.contains("uniform sampler2D uCurrentProxy"))
assertTrue(shader.contains("uniform sampler2D uComposedFlow"))
assertTrue(shader.contains("min(referenceSample.g, currentSample.g)"))
assertTrue(shader.contains("min(referenceSample.b, currentSample.b)"))
assertTrue(shader.contains("eligibilitySum += validity * observability"))
}
@Test
fun generatedLongFusionShadersPassAvailableNdkValidator() {
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 rawCommonStub = "int bayerIndexAt(int cfaPattern, ivec2 p) { return 0; }"
val sources = listOf(
"compute" to GlesRawRadianceFusionShaders.accumulate(
rawCommon = rawCommonStub,
trackRejections = true,
trackParticipation = true,
trackLongParticipation = true,
),
"fragment" to GlesRawRadianceFusionShaders.longEligibility,
)
sources.forEach { (stage, source) ->
val sourceFile = File.createTempFile("radiance-long-", ".$stage")
val outputFile = File.createTempFile("radiance-long-", ".spv")
try {
// glslc emits SPIR-V even for syntax-only validation and requires ESSL 3.10.
// The production fragment shader remains ESSL 3.00 for GLES 3.0 compatibility;
// 3.10 does not change any syntax used by this shader.
val validatorSource = if (stage == "fragment") {
source.replaceFirst("#version 300 es", "#version 310 es")
} else {
source
}
sourceFile.writeText(validatorSource)
val process = ProcessBuilder(
checkNotNull(validator).absolutePath,
"--target-env=opengl",
"-fauto-map-locations",
"-fauto-bind-uniforms",
"-fshader-stage=$stage",
sourceFile.absolutePath,
"-o",
outputFile.absolutePath,
).redirectErrorStream(true).start()
val output = process.inputStream.bufferedReader().use { it.readText() }
assertEquals("$stage: $output", 0, process.waitFor())
} finally {
sourceFile.delete()
outputFile.delete()
}
}
}
@Test
fun nonReferenceSemanticPlanesDecodeOnlyAfterCfaIndependentReconstruction() {
val shader = GlesRawRadianceFusionShaders.accumulate("", trackRejections = false)
@@ -240,6 +367,9 @@ class GlesRawRadianceFusionShadersTest {
assertFalse(clear.contains("uFusionRejections"))
assertFalse(accumulate.contains("uFusionRejections"))
assertFalse(accumulate.contains("uFusionParticipationStats"))
assertFalse(clear.contains("uLongParticipation"))
assertFalse(accumulate.contains("uLongParticipation"))
assertFalse(normalize.contains("uLongParticipation"))
assertFalse(normalize.contains("uFusionRejections"))
assertFalse(normalize.contains("uDebugNeutralRgb"))
}
@@ -54,4 +54,42 @@ class RawRadianceExposurePlannerTest {
assertNull(plan.shortIndex)
assertArrayEquals(IntArray(0), plan.excludedIndices)
}
@Test
fun taggedLongFramesArePlannedFromActualExposureWithoutEnteringNormalCluster() {
val plan = RawRadianceExposurePlanner.plan(
exposureProducts = listOf(100.0, 101.0, 33.0, 420.0, 250.0),
frameRoles = listOf(
RawBurstFrameRole.NORMAL,
RawBurstFrameRole.NORMAL,
RawBurstFrameRole.HIGHLIGHT_SHORT,
RawBurstFrameRole.SHADOW_LONG,
RawBurstFrameRole.SHADOW_LONG,
),
)
assertEquals(101.0, plan.baseExposureProduct!!, 1e-6)
assertArrayEquals(intArrayOf(0, 1), plan.normalIndices)
assertEquals(2, plan.shortIndex)
assertArrayEquals(intArrayOf(3, 4), plan.longIndices)
assertArrayEquals(IntArray(0), plan.excludedIndices)
assertArrayEquals(intArrayOf(0, 1, 2, 3, 4), plan.acceptedIndices)
}
@Test
fun taggedLongFrameRemainsLongWhenIsoLimitMakesExposureMatchNormal() {
val plan = RawRadianceExposurePlanner.plan(
exposureProducts = listOf(100.0, 100.0, 33.0, 100.0),
frameRoles = listOf(
RawBurstFrameRole.NORMAL,
RawBurstFrameRole.NORMAL,
RawBurstFrameRole.HIGHLIGHT_SHORT,
RawBurstFrameRole.SHADOW_LONG,
),
)
assertArrayEquals(intArrayOf(0, 1), plan.normalIndices)
assertArrayEquals(intArrayOf(3), plan.longIndices)
assertArrayEquals(IntArray(0), plan.excludedIndices)
}
}
@@ -1,5 +1,6 @@
package com.hinnka.mycamera.processor
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test
@@ -40,4 +41,60 @@ class RawRadianceHighlightAlignmentSupportTest {
),
)
}
@Test
fun acceptedLongFrameKeepsFullGlobalWeightDespiteSparseSeedCoverage() {
val admission = planRadianceLongAdmission(
validTileCount = 230,
coveredQuadrants = 2,
eligibleTileCount = 4_000,
eligibleQuadrants = 2,
minimumValidTiles = 64,
minimumQuadrants = 3,
minimumEligibleCoverage = 0.01f,
)
assertEquals(
1f,
admission.frameWeight,
0f,
)
assertEquals(2, admission.requiredQuadrants)
}
@Test
fun longFrameStillRequiresThreeQuadrantsWhenFourAreEligible() {
val admission = planRadianceLongAdmission(
validTileCount = 230,
coveredQuadrants = 2,
eligibleTileCount = 4_000,
eligibleQuadrants = 4,
minimumValidTiles = 64,
minimumQuadrants = 3,
minimumEligibleCoverage = 0.01f,
)
assertEquals(
0f,
admission.frameWeight,
0f,
)
assertEquals(3, admission.requiredQuadrants)
}
@Test
fun longFrameRejectsInsufficientCoverageOfEligibleTiles() {
val admission = planRadianceLongAdmission(
validTileCount = 64,
coveredQuadrants = 3,
eligibleTileCount = 10_000,
eligibleQuadrants = 3,
minimumValidTiles = 64,
minimumQuadrants = 3,
minimumEligibleCoverage = 0.01f,
)
assertEquals(0f, admission.frameWeight, 0f)
assertEquals(0.0064f, admission.eligibleCoverage, 1e-6f)
}
}