diff --git a/app/src/main/java/com/hinnka/mycamera/processor/GlesGpuCompletion.kt b/app/src/main/java/com/hinnka/mycamera/processor/GlesGpuCompletion.kt index bd085f61..d1dbbc9f 100644 --- a/app/src/main/java/com/hinnka/mycamera/processor/GlesGpuCompletion.kt +++ b/app/src/main/java/com/hinnka/mycamera/processor/GlesGpuCompletion.kt @@ -8,7 +8,7 @@ internal enum class GpuStackCompletionStage(val logKey: String) { LONG_ALIGNMENT("longAlignment"), HIGHLIGHT_ALIGNMENT("highlightAlignment"), TILED_RECONSTRUCTION("tiledReconstruction"), - CHROMA_POSTPROCESS("chromaPostprocess"), + OUTPUT_HANDOFF("outputHandoff"), FINAL_EXPORT("finalExport"), } diff --git a/app/src/main/java/com/hinnka/mycamera/processor/GlesMgcRawSpatialStacker.kt b/app/src/main/java/com/hinnka/mycamera/processor/GlesMgcRawSpatialStacker.kt index 7e5f4902..463f57bc 100644 --- a/app/src/main/java/com/hinnka/mycamera/processor/GlesMgcRawSpatialStacker.kt +++ b/app/src/main/java/com/hinnka/mycamera/processor/GlesMgcRawSpatialStacker.kt @@ -434,7 +434,7 @@ internal class GlesMgcRawSpatialStacker( private var spatialRgbAccumulateProgram = 0 private var spatialRgbResolveProgram = 0 private var copyRgb16ToFloatProgram = 0 - private var rgbChromaPostprocessor: GlesMgcSpatialRgbChromaPostprocessor? = null + private var rgbOutputAssembler: GlesMgcSpatialRgbOutputAssembler? = null private var supportsComputePrograms = false private var supportsComputeReadback = false private var maxShaderStorageBlockBytes = 0L @@ -2256,11 +2256,11 @@ internal class GlesMgcRawSpatialStacker( val lensShadingTexture = createLensShadingTexture() val fullOutput = MgcSpatialRgbRect(0, 0, width, height) val fullOutputTile = MgcSpatialRgbTile(index = 0, outputCore = fullOutput) - val chromaPostprocessor = checkNotNull(rgbChromaPostprocessor) { - "MGC Sabre VGN color noise/IIR postprocessor is not initialized" + val outputAssembler = checkNotNull(rgbOutputAssembler) { + "MGC Sabre VGN output assembler is not initialized" } - chromaPostprocessor.initStorage(listOf(fullOutputTile)) - val resolvedRgb = chromaPostprocessor.normalizationTargetTexture() + outputAssembler.initStorage(listOf(fullOutputTile)) + val resolvedRgb = outputAssembler.normalizationTargetTexture() val resolveStartNs = System.nanoTime() renderSabreResolve( accumulatedColor = accumulatedColor, @@ -2276,7 +2276,7 @@ internal class GlesMgcRawSpatialStacker( SABRE_TAG, "MGC Sabre Resolve GPU submit=${elapsedMs(resolveStartNs)}ms", ) - chromaPostprocessor.markTileWritten(fullOutputTile) + outputAssembler.markTileWritten(fullOutputTile) releaseSabreMergePhaseTextures(resolvedRgb) if (!exportGpuLinearRgbSource) { val outputBytes = width.toLong() * height * 3L * Short.SIZE_BYTES @@ -2286,31 +2286,30 @@ internal class GlesMgcRawSpatialStacker( ) } GlesGpuScheduler.memoryBarrier() - val onChromaFinalSubmitted: (() -> Unit)? = when { + val onOutputSubmitted: (() -> Unit)? = when { exportGpuLinearRgbSource -> { - { completionRecorder.mark(GpuStackCompletionStage.CHROMA_POSTPROCESS) } + { completionRecorder.mark(GpuStackCompletionStage.OUTPUT_HANDOFF) } } else -> null } - val chromaResult = chromaPostprocessor.process( + outputAssembler.process( obtainOutputBuffer = { checkNotNull(cpuOutput) }, deferFullSizeReadback = exportGpuLinearRgbSource, - onFinalSubmitted = onChromaFinalSubmitted, - ) - postprocessedUi = chromaResult.exportedTextureId + onFinalSubmitted = onOutputSubmitted, + ).also { postprocessedUi = it.exportedTextureId } if (exportGpuLinearRgbSource) { check(postprocessedUi != 0) { - "MGC Sabre VGN color noise/IIR did not export its filtered texture" + "MGC Sabre VGN output did not export its resolved texture" } exportedTexture = if (gpuLinearRgbStorage == GpuLinearRgbStorage.RGBA16UI) { postprocessedUi.also { postprocessedUi = 0 } } else { - // The chroma component leaves its final image pass behind a deferred export - // checkpoint. Complete that ownership handoff before submitting a dependent - // conversion and deleting the RGBA16UI source. This mirrors Spatial RGB and - // avoids exposing partially retired VGN work groups on affected Mali queues. + // Resolve leaves its final pass behind a deferred export checkpoint. Complete + // that ownership handoff before submitting a dependent conversion and deleting + // the RGBA16UI source. This mirrors Spatial RGB and avoids exposing partially + // retired work groups on affected Mali queues. GlesGpuCompletion.awaitSubmittedWork( - label = "MGC Sabre VGN IIR-to-float handoff", + label = "MGC Sabre VGN RGB16-to-float handoff", checkGlError = ::checkGlError, ) createTexture( @@ -2335,15 +2334,13 @@ internal class GlesMgcRawSpatialStacker( textureSpecs.remove(exportedTexture) } } - checkGlError("MGC Sabre Resolve/VGN color noise") + checkGlError("MGC Sabre Resolve/VGN output") PLog.i( SABRE_TAG, "MGC Sabre complete frames=${frames.size} native=${width}x$height " + "output=${outputWidth}x$outputHeight " + "referenceSnr=${kernelTuning.referenceSnr} " + "result=${if (exportedTexture != 0) gpuLinearRgbStorage.name + "_GPU" else "RGB16_CPU"} " + - "colorNoiseIir=${chromaResult.chromaSubmissionMs}ms " + - "chromaFinal=${chromaResult.finalSubmissionMs}ms " + "total=${elapsedMs(processStartNs)}ms", ) val result = RawStackResult( @@ -2765,7 +2762,7 @@ internal class GlesMgcRawSpatialStacker( private fun initSabrePrograms() { check(supportsComputePrograms) { - "MGC Sabre VGN color noise/IIR requires the GLES 3.1 compute baseline" + "MGC Sabre VGN requires the GLES 3.1 compute baseline" } sabreExtractBayerProgram = linkProgram( GlesMgcRawSabreShaders.extractBayer, @@ -2847,17 +2844,17 @@ internal class GlesMgcRawSpatialStacker( ) { copyRgb16ToFloatProgram = linkComputeProgram( GlesMgcRawSpatialShaders.copyRgb16ToFloat, - "mgc_sabre_chroma_to_rgba16f", + "mgc_sabre_rgb16_to_rgba16f", ) } - // ResolveSabre and VGN stay on the native grid. A requested larger output is a separate - // display transform after FinishRaw/GuidedUpsample in RawDemosaicProcessor. - rgbChromaPostprocessor = createRgbChromaPostprocessor( + // ResolveSabre stays on the native grid. A requested larger output is a separate display + // transform after FinishRaw/GuidedUpsample in RawDemosaicProcessor. + rgbOutputAssembler = createRgbOutputAssembler( imageWidth = width, imageHeight = height, exportFullSizeTexture = exportGpuLinearRgbSource || outputWidth != width || outputHeight != height, - ).also { it.initPrograms() } + ) } private fun initMergeUpsampleAlignment(): Int { @@ -3017,7 +3014,7 @@ internal class GlesMgcRawSpatialStacker( } if (outputMode == MgcSpatialOutputMode.RGB) { check(supportsComputePrograms) { - "MGC Spatial RGB color noise/IIR requires the GLES 3.1 compute baseline" + "MGC Spatial RGB requires the GLES 3.1 compute baseline" } spatialRgbConvertProgram = linkProgram( GlesMgcGreenDemosaicShaders.convertRaw16, @@ -3035,7 +3032,7 @@ internal class GlesMgcRawSpatialStacker( GlesMgcSpatialRgbMergeShaders.accumulateFrame, "mgc_spatial_v25_rgb_accumulate", ) - // Color noise/IIR owns a fixed RGBA16UI boundary, matching the VGN/Radiance + // The output assembler owns a fixed RGBA16UI boundary, matching the VGN/Radiance // contract. The merged F16 output enters that boundary exactly once. spatialRgbResolveProgram = linkProgram( GlesMgcSpatialRgbMergeShaders.resolve, @@ -3046,12 +3043,10 @@ internal class GlesMgcRawSpatialStacker( ) { copyRgb16ToFloatProgram = linkComputeProgram( GlesMgcRawSpatialShaders.copyRgb16ToFloat, - "mgc_spatial_rgb_chroma_to_rgba16f", + "mgc_spatial_rgb16_to_rgba16f", ) } - rgbChromaPostprocessor = createRgbChromaPostprocessor().also { - it.initPrograms() - } + rgbOutputAssembler = createRgbOutputAssembler() } normalizeBayerProgram = linkProgram( GlesMgcRawSpatialShaders.normalizeBayer, @@ -3059,21 +3054,16 @@ internal class GlesMgcRawSpatialStacker( ) } - private fun createRgbChromaPostprocessor( + private fun createRgbOutputAssembler( imageWidth: Int = outputWidth, imageHeight: Int = outputHeight, exportFullSizeTexture: Boolean = exportGpuLinearRgbSource, - ): GlesMgcSpatialRgbChromaPostprocessor { - return GlesMgcSpatialRgbChromaPostprocessor( + ): GlesMgcSpatialRgbOutputAssembler { + return GlesMgcSpatialRgbOutputAssembler( imageWidth = imageWidth, imageHeight = imageHeight, - calculationWbGains = calculationWhiteBalance, - outputScale = 1f, exportFullSizeTexture = exportFullSizeTexture, - backend = object : GlesMgcSpatialRgbChromaPostprocessor.Backend { - override fun linkComputeProgram(source: String, name: String): Int = - this@GlesMgcRawSpatialStacker.linkComputeProgram(source, name) - + backend = object : GlesMgcSpatialRgbOutputAssembler.Backend { override fun createRgba16UiTexture( width: Int, height: Int, @@ -3094,9 +3084,6 @@ internal class GlesMgcRawSpatialStacker( this@GlesMgcRawSpatialStacker.transferOwnedTexture(texture, label) } - override fun uniformLocation(program: Int, name: String): Int = - this@GlesMgcRawSpatialStacker.uniformLocation(program, name) - override fun checkGlError(label: String) { this@GlesMgcRawSpatialStacker.checkGlError(label) } @@ -5613,9 +5600,6 @@ internal class GlesMgcRawSpatialStacker( GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, 0) } - private fun rgbIirWorkingSetBytes(): Long = - MgcSpatialRgbMemoryPolicy.iirWorkingSetBytes(outputWidth, outputHeight) - /** * MGC 9.7.047 V25's Spatial RGB merge (MergeRgbRaw16F16TileSize16Halide) on the GPU, see * [GlesMgcSpatialRgbMergeShaders]. Each burst frame's DemosaicGreenRaw16 guide feeds the same @@ -5676,10 +5660,10 @@ internal class GlesMgcRawSpatialStacker( "Unable to allocate MGC V25 Spatial RGB16 output", ) } - val chromaPostprocessor = checkNotNull(rgbChromaPostprocessor) { - "MGC Spatial RGB chroma postprocessor is not initialized" + val outputAssembler = checkNotNull(rgbOutputAssembler) { + "MGC Spatial RGB output assembler is not initialized" } - chromaPostprocessor.initStorage(listOf(fullOutputTile)) + outputAssembler.initStorage(listOf(fullOutputTile)) val lensShadingTexture = createLensShadingTexture() val mergeStartNs = System.nanoTime() mergeSpatialRgbFrames( @@ -5689,7 +5673,7 @@ internal class GlesMgcRawSpatialStacker( capture = capture, referenceBlack = referenceBlack, lensShadingTexture = lensShadingTexture, - target = chromaPostprocessor.normalizationTargetTexture(), + target = outputAssembler.normalizationTargetTexture(), outputExposureScale = outputExposureScale, cameraWhite = mergeCameraWhite, ultrashortWeight = ultrashortWeight, @@ -5699,32 +5683,31 @@ internal class GlesMgcRawSpatialStacker( "merge=${mergeCameraWhite.contentToString()} normalization=$outputExposureScale " + "referenceFallbackWhiteScale=$outputExposureScale " + "hasShort=${ultrashortWeight != 0} stage=before-rgb16") - chromaPostprocessor.markTileWritten(fullOutputTile) + outputAssembler.markTileWritten(fullOutputTile) releaseRgbFusionPhaseTextures( - retainedTexture = chromaPostprocessor.normalizationTargetTexture(), + retainedTexture = outputAssembler.normalizationTargetTexture(), label = "V25 merge", ) GlesGpuScheduler.memoryBarrier() - val chromaResult = chromaPostprocessor.process( + outputAssembler.process( obtainOutputBuffer = { checkNotNull(cpuOutput) }, deferFullSizeReadback = exportGpuLinearRgbSource, onFinalSubmitted = if (exportGpuLinearRgbSource) { - { completionRecorder.mark(GpuStackCompletionStage.CHROMA_POSTPROCESS) } + { completionRecorder.mark(GpuStackCompletionStage.OUTPUT_HANDOFF) } } else { null }, - ) - postprocessedUi = chromaResult.exportedTextureId + ).also { postprocessedUi = it.exportedTextureId } if (exportGpuLinearRgbSource) { check(postprocessedUi != 0) { - "MGC Spatial RGB chroma did not export its filtered texture" + "MGC Spatial RGB output did not export its merged texture" } gpuOutput = if (gpuLinearRgbStorage == GpuLinearRgbStorage.RGBA16UI) { postprocessedUi.also { postprocessedUi = 0 } } else { GlesGpuCompletion.awaitSubmittedWork( - label = "MGC Spatial V25 merge IIR-to-float handoff", + label = "MGC Spatial V25 merge RGB16-to-float handoff", checkGlError = ::checkGlError, ) createTexture( @@ -5755,9 +5738,7 @@ internal class GlesMgcRawSpatialStacker( "MGC Spatial V25 RGB merge frames=${frames.size} " + "native=${width}x$height " + "output=${outputWidth}x$outputHeight rawScale=$sabreResolveRawScale " + - "mergeSharpness=$mergeSharpness merge=${mergeMs}ms " + - "colorNoiseIir=${chromaResult.chromaSubmissionMs}ms " + - "chromaFinal=${chromaResult.finalSubmissionMs}ms", + "mergeSharpness=$mergeSharpness merge=${mergeMs}ms", ) return RgbMergeOutput( cpuBuffer = cpuOutput, @@ -6034,7 +6015,7 @@ internal class GlesMgcRawSpatialStacker( GLES31.GL_WRITE_ONLY, GLES30.GL_RGBA16F, ) - checkGlError("MGC Spatial RGB chroma RGBA16F handoff") + checkGlError("MGC Spatial RGB RGBA16F handoff") } private fun readRgbTile( @@ -6483,9 +6464,9 @@ internal class GlesMgcRawSpatialStacker( } /** - * ResolveSabre has consumed every merge-stage texture. Retire that arena before the VGN - * IIR surfaces are allocated; only the Resolve result in the chroma stage's assembled RGB - * crosses this phase boundary. + * ResolveSabre has consumed every merge-stage texture. Retire that arena before the output + * handoff; only the Resolve result in the output assembler's RGB texture crosses this phase + * boundary. */ private fun releaseSabreMergePhaseTextures(resolvedRgb: Int) { check(resolvedRgb in textures) { "Sabre Resolve phase must retain its resolved RGB" } @@ -6544,16 +6525,16 @@ internal class GlesMgcRawSpatialStacker( } /** - * Closes RGB fusion before the recursive chroma stage allocates its two additional full-size - * surfaces. The assembled RGBA16UI input becomes IIR scratch after seeding; every other fusion - * texture is dead at this boundary and must not inflate the IIR allocation peak. + * Closes RGB fusion before the output handoff. Only the assembled RGBA16UI output survives; + * every other fusion texture is dead at this boundary and must not overlap the export or + * full-size readback. */ private fun releaseRgbFusionPhaseTextures( retainedTexture: Int, label: String, ) { check(retainedTexture != 0 && textures.contains(retainedTexture)) { - "MGC Spatial RGB $label IIR input is not owned by the stacker" + "MGC Spatial RGB $label output is not owned by the stacker" } val beforeBytes = estimatedOwnedTextureBytes() val waitMs = GlesGpuCompletion.awaitSubmittedWork( @@ -6575,7 +6556,7 @@ internal class GlesMgcRawSpatialStacker( TAG, "MGC Spatial RGB $label fusion arena released textures=${releasedTextures.size} " + "releasedBytes=$releasedBytes retainedBytes=${estimatedOwnedTextureBytes()} " + - "iirWorkingSetBytes=${rgbIirWorkingSetBytes()} beforeBytes=$beforeBytes " + + "beforeBytes=$beforeBytes " + "gpuWait=${waitMs}ms", ) } @@ -7119,8 +7100,8 @@ internal class GlesMgcRawSpatialStacker( noiseLutCache.clear() globalAlignment?.release() globalAlignment = null - rgbChromaPostprocessor?.release() - rgbChromaPostprocessor = null + rgbOutputAssembler?.release() + rgbOutputAssembler = null if (programs.isNotEmpty()) { for (program in programs) GLES30.glDeleteProgram(program) } diff --git a/app/src/main/java/com/hinnka/mycamera/processor/GlesMgcSpatialRgbChromaPostprocessor.kt b/app/src/main/java/com/hinnka/mycamera/processor/GlesMgcSpatialRgbChromaPostprocessor.kt deleted file mode 100644 index b8b38e47..00000000 --- a/app/src/main/java/com/hinnka/mycamera/processor/GlesMgcSpatialRgbChromaPostprocessor.kt +++ /dev/null @@ -1,1431 +0,0 @@ -package com.hinnka.mycamera.processor - -import android.opengl.GLES30 -import android.opengl.GLES31 -import com.hinnka.mycamera.utils.DirectBufferPixelPacker -import com.hinnka.mycamera.utils.LargeDirectBuffer -import java.nio.ByteBuffer -import kotlin.math.acos -import kotlin.math.cos -import kotlin.math.max -import kotlin.math.sin - -/** - * Post-fusion VGN chroma filtering for MGC Spatial RGB. - * - * MGC Spatial RGB tiles are assembled into one full-size 2D texture before chroma filtering. All - * later shaders use direct global coordinates over that contiguous surface, so local kernels cross - * tile seams naturally and every IIR invocation scans one physical output row or column without - * per-sample tile lookup. - * - * Input and output are un-white-balanced camera RGB. LSC is already part of reconstructed RGB and - * is deliberately untouched here. The seed shader enters calculation-WB RGB/YCCD once, and the - * final shader applies the inverse calculation WB once. - */ -internal class GlesMgcSpatialRgbChromaPostprocessor( - private val imageWidth: Int, - private val imageHeight: Int, - calculationWbGains: FloatArray, - outputScale: Float, - private val backend: Backend, - private val exportFullSizeTexture: Boolean = false, -) { - interface Backend { - fun linkComputeProgram(source: String, name: String): Int - - fun createRgba16UiTexture(width: Int, height: Int, label: String): Int - - fun deleteTexture(texture: Int, label: String) - - fun transferTextureOwnership(texture: Int, label: String) - - fun uniformLocation(program: Int, name: String): Int - - fun checkGlError(label: String) - - fun yieldToUiRenderer() - } - - data class ReadbackTiming( - val elapsedMs: Long, - val gpuQueueWaitMs: Long, - val pixelTransferMs: Long, - val cpuPackMs: Long, - val allocMs: Long, - ) - - data class ProcessResult( - val readbackTiming: ReadbackTiming, - val exportedTextureId: Int, - val chromaSubmissionMs: Long, - val finalSubmissionMs: Long, - val cpuBufferPopulated: Boolean, - ) - - private val calculationRgbGains = floatArrayOf( - calculationWbGains.getOrElse(0) { 1f }, - 1f, - calculationWbGains.getOrElse(3) { 1f }, - ).also { gains -> - require(gains.all { it.isFinite() && it > 0f }) { - "MGC Spatial RGB chroma calculation gains must be finite and positive" - } - } - private val safeOutputScale = outputScale.takeIf { it.isFinite() && it > 0f } ?: 1f - private val coefficients = MgcSpatialRgbChromaIirCoefficients.forOutputScale(safeOutputScale) - private val passWindow = GlesGpuScheduler.PassWindow("GlesMgcSpatialRgbChroma", 2) - private var tiles: List = emptyList() - private var readbackTileWidth = 0 - private var readbackTileHeight = 0 - - private var seedProgram = 0 - private var colorNoise1Program = 0 - private var colorNoise2Program = 0 - private var colorNoise3SmoothProgram = 0 - private var restoreOriginalProgram = 0 - private var iirRgbProgram = 0 - private var calculateErrorProgram = 0 - private var iirErrorProgram = 0 - private var colorNoiseFilterProgram = 0 - private var finalProgram = 0 - - private var assembledRgb = 0 - private var originalYccd = 0 - private var smoothYccd = 0 - private var writtenTileCount = 0 - private var readbackFbo = 0 - private val ownedTextures = ArrayList(3) - private val pendingImageReads = ArrayList(3) - private val pendingImageWrites = ArrayList(3) - - init { - require(imageWidth > 0 && imageHeight > 0) - } - - private fun validateAndSetTiles(newTiles: List) { - require(newTiles.isNotEmpty()) - check(assembledRgb == 0) { "MGC Spatial RGB chroma storage is already active" } - val tiles = newTiles.toList() - val rows = tiles.groupBy { it.outputCore.top }.toSortedMap().values.toList() - var expectedTop = 0 - var expectedIndex = 0 - rows.forEachIndexed { rowIndex, row -> - val orderedRow = row.sortedBy { it.outputCore.left } - require(orderedRow.first().outputCore.top == expectedTop) { - "MGC Spatial RGB tile row $rowIndex starts at ${orderedRow.first().outputCore.top}, " + - "expected $expectedTop" - } - val rowBottom = orderedRow.first().outputCore.bottom - var expectedLeft = 0 - orderedRow.forEach { tile -> - require(tile.index == expectedIndex++) { - "MGC Spatial RGB tiles must be row-major" - } - require(tile.outputCore.left == expectedLeft && tile.outputCore.top == expectedTop) { - "MGC Spatial RGB tile ${tile.index} leaves a gap or overlaps another tile" - } - require(tile.outputCore.bottom == rowBottom) { - "MGC Spatial RGB tile row $rowIndex has inconsistent heights" - } - expectedLeft = tile.outputCore.right - } - require(expectedLeft == imageWidth) { - "MGC Spatial RGB tile row $rowIndex covers width $expectedLeft, expected $imageWidth" - } - expectedTop = rowBottom - } - require(expectedTop == imageHeight) { - "MGC Spatial RGB tiles cover height $expectedTop, expected $imageHeight" - } - readbackTileWidth = tiles.maxOf { it.outputCore.width } - readbackTileHeight = tiles.maxOf { it.outputCore.height } - this.tiles = tiles - } - - fun initPrograms() { - if (seedProgram != 0) return - seedProgram = backend.linkComputeProgram( - GlesMgcSpatialRgbChromaShaders.seed, - "mgc_spatial_rgb_chroma_seed", - ) - colorNoise1Program = backend.linkComputeProgram( - GlesMgcSpatialRgbChromaShaders.colorNoise1, - "mgc_spatial_rgb_chroma_noise_1", - ) - colorNoise2Program = backend.linkComputeProgram( - GlesMgcSpatialRgbChromaShaders.colorNoise2, - "mgc_spatial_rgb_chroma_noise_2", - ) - colorNoise3SmoothProgram = backend.linkComputeProgram( - GlesMgcSpatialRgbChromaShaders.colorNoise3Smooth, - "mgc_spatial_rgb_chroma_noise_3_smooth", - ) - restoreOriginalProgram = backend.linkComputeProgram( - GlesMgcSpatialRgbChromaShaders.restoreOriginal, - "mgc_spatial_rgb_chroma_restore_original", - ) - iirRgbProgram = backend.linkComputeProgram( - GlesMgcSpatialRgbChromaShaders.iirRgb, - "mgc_spatial_rgb_chroma_iir_rgb", - ) - calculateErrorProgram = backend.linkComputeProgram( - GlesMgcSpatialRgbChromaShaders.calculateColorNoiseError, - "mgc_spatial_rgb_chroma_error", - ) - iirErrorProgram = backend.linkComputeProgram( - GlesMgcSpatialRgbChromaShaders.iirError, - "mgc_spatial_rgb_chroma_iir_error", - ) - colorNoiseFilterProgram = backend.linkComputeProgram( - GlesMgcSpatialRgbChromaShaders.colorNoiseFilter, - "mgc_spatial_rgb_chroma_filter", - ) - finalProgram = backend.linkComputeProgram( - GlesMgcSpatialRgbChromaShaders.finalCameraRgb, - "mgc_spatial_rgb_chroma_final_camera_rgb_2d", - ) - } - - fun initStorage(tiles: List) { - check(seedProgram != 0) { "MGC Spatial RGB chroma programs are not initialized" } - validateAndSetTiles(tiles) - val maxTextureSize = IntArray(1) - GLES30.glGetIntegerv(GLES30.GL_MAX_TEXTURE_SIZE, maxTextureSize, 0) - require(imageWidth <= maxTextureSize[0] && imageHeight <= maxTextureSize[0]) { - "MGC Spatial RGB chroma requires ${imageWidth}x$imageHeight 2D textures; " + - "GLES supports up to ${maxTextureSize[0]}" - } - assembledRgb = createFullSizeTexture("assembled camera RGB") - writtenTileCount = 0 - backend.checkGlError("MGC Spatial RGB chroma storage") - } - - /** - * Full-size integer render target owned by this stage. - * - * The normalize fragment pass renders each tile directly into its global viewport on this - * texture. Keeping assembly as framebuffer writes removes the former tile texture -> compute - * image copy while preserving the exact RGBA16UI boundary between fusion and chroma. - */ - fun normalizationTargetTexture(): Int { - check(assembledRgb != 0) { "MGC Spatial RGB chroma storage is not initialized" } - return assembledRgb - } - - fun markTileWritten(tile: MgcSpatialRgbTile) { - check(writtenTileCount < tiles.size) { "All MGC Spatial RGB tiles are already assembled" } - check(tiles[writtenTileCount].index == tile.index) { - "MGC Spatial RGB tile ${tile.index} was written out of row-major order; " + - "expected ${tiles[writtenTileCount].index}" - } - writtenTileCount += 1 - } - - fun process( - obtainOutputBuffer: () -> ByteBuffer, - deferFullSizeReadback: Boolean, - onChromaSubmitted: (() -> Unit)? = null, - onFinalSubmitted: (() -> Unit)? = null, - ): ProcessResult { - check(assembledRgb != 0) { "MGC Spatial RGB chroma has no assembled fusion output" } - check(writtenTileCount == tiles.size) { - "MGC Spatial RGB chroma input is incomplete: $writtenTileCount/${tiles.size} tiles" - } - val chromaSubmissionStartNs = System.nanoTime() - originalYccd = createFullSizeTexture("original YCCD") - smoothYccd = createFullSizeTexture("smooth YCCD") - clearImageBindings() - - dispatchSeed() - dispatchLocal(colorNoise1Program, originalYccd, assembledRgb, "color noise 1") - dispatchLocal(colorNoise2Program, assembledRgb, smoothYccd, "color noise 2") - dispatchColorNoise3() - dispatchRestoreOriginal() - - var scratch = assembledRgb - runIirRgb( - source = smoothYccd, - destination = scratch, - pass = coefficients.pass1, - filterLuma = true, - label = "IIR1", - ).also { result -> - smoothYccd = result.first - scratch = result.second - } - dispatchCalculateError(scratch) - scratch = smoothYccd.also { smoothYccd = scratch } - runIirError( - source = smoothYccd, - destination = scratch, - a10 = coefficients.pass1.a10, - b10 = coefficients.pass1.b10, - ).also { result -> - smoothYccd = result.first - scratch = result.second - } - dispatchColorNoiseFilter(scratch) - scratch = originalYccd.also { originalYccd = scratch } - runIirRgb( - source = originalYccd, - destination = scratch, - pass = coefficients.pass3, - filterLuma = false, - label = "IIR3", - ).also { result -> - originalYccd = result.first - scratch = result.second - } - val chromaSubmissionMs = (System.nanoTime() - chromaSubmissionStartNs) / 1_000_000L - onChromaSubmitted?.invoke() - val finalSubmissionStartNs = System.nanoTime() - dispatchFinal(scratch) - val finalSubmissionMs = - (System.nanoTime() - finalSubmissionStartNs) / 1_000_000L - onFinalSubmitted?.invoke() - - val cpuBufferPopulated = !exportFullSizeTexture || !deferFullSizeReadback - val timing = if (cpuBufferPopulated) { - readbackFullSize(scratch, obtainOutputBuffer()).also { - passWindow.clearAfterCheckpoint() - } - } else { - check(onFinalSubmitted != null) { - "Deferred MGC Spatial RGB chroma export requires a completion checkpoint" - } - // The callback inserted a later command-stream fence whose ownership leaves this - // component with the exported texture. Its checkpoint supersedes the local fences. - passWindow.clearAfterCheckpoint() - ReadbackTiming( - elapsedMs = 0L, - gpuQueueWaitMs = 0L, - pixelTransferMs = 0L, - cpuPackMs = 0L, - allocMs = 0L, - ) - } - - val exportedTextureId = if (exportFullSizeTexture) scratch else 0 - if (exportedTextureId != 0) { - backend.transferTextureOwnership( - exportedTextureId, - "filtered camera RGB export", - ) - ownedTextures.removeAll { it == exportedTextureId } - } - for (unit in 0..2) { - GLES31.glBindImageTexture( - unit, - 0, - 0, - false, - 0, - GLES31.GL_READ_ONLY, - GLES30.GL_RGBA16UI, - ) - } - setOf(assembledRgb, originalYccd, smoothYccd, scratch) - .filter { it != 0 && it != exportedTextureId } - .forEach(::deleteTexture) - assembledRgb = 0 - originalYccd = 0 - smoothYccd = 0 - writtenTileCount = 0 - tiles = emptyList() - readbackTileWidth = 0 - readbackTileHeight = 0 - return ProcessResult( - readbackTiming = timing, - exportedTextureId = exportedTextureId, - chromaSubmissionMs = chromaSubmissionMs, - finalSubmissionMs = finalSubmissionMs, - cpuBufferPopulated = cpuBufferPopulated, - ) - } - - fun release() { - passWindow.drain("MGC Spatial RGB chroma release") - ownedTextures.toList().forEach(::deleteTexture) - assembledRgb = 0 - originalYccd = 0 - smoothYccd = 0 - writtenTileCount = 0 - tiles = emptyList() - readbackTileWidth = 0 - readbackTileHeight = 0 - if (readbackFbo != 0) { - GLES30.glDeleteFramebuffers(1, intArrayOf(readbackFbo), 0) - readbackFbo = 0 - } - } - - private fun dispatchSeed() { - GLES31.glUseProgram(seedProgram) - setImageUniforms(seedProgram) - GLES31.glUniform3fv( - backend.uniformLocation(seedProgram, "uCalculationGains"), - 1, - calculationRgbGains, - 0, - ) - GLES31.glUniform1f( - backend.uniformLocation(seedProgram, "uMinimumDirectionGradient"), - 8f, - ) - bindImage(0, assembledRgb, GLES31.GL_READ_ONLY) - bindImage(1, originalYccd, GLES31.GL_WRITE_ONLY) - dispatchImage(seedProgram, "seed") - } - - private fun dispatchLocal(program: Int, source: Int, destination: Int, label: String) { - GLES31.glUseProgram(program) - setImageUniforms(program) - bindImage(0, source, GLES31.GL_READ_ONLY) - bindImage(1, destination, GLES31.GL_WRITE_ONLY) - dispatchImage(program, label) - } - - private fun dispatchColorNoise3() { - GLES31.glUseProgram(colorNoise3SmoothProgram) - setImageUniforms(colorNoise3SmoothProgram) - bindImage(0, assembledRgb, GLES31.GL_READ_ONLY) - bindImage(1, smoothYccd, GLES31.GL_READ_ONLY) - bindImage(2, originalYccd, GLES31.GL_WRITE_ONLY) - dispatchImage(colorNoise3SmoothProgram, "color noise 3 smooth") - } - - private fun dispatchRestoreOriginal() { - GLES31.glUseProgram(restoreOriginalProgram) - setImageUniforms(restoreOriginalProgram) - bindImage(0, originalYccd, GLES31.GL_READ_ONLY) - bindImage(1, assembledRgb, GLES31.GL_READ_ONLY) - bindImage(2, smoothYccd, GLES31.GL_WRITE_ONLY) - dispatchImage(restoreOriginalProgram, "restore directional YCCD") - val temporary = originalYccd - originalYccd = smoothYccd - smoothYccd = temporary - } - - private fun dispatchCalculateError(destination: Int) { - GLES31.glUseProgram(calculateErrorProgram) - setImageUniforms(calculateErrorProgram) - bindImage(0, originalYccd, GLES31.GL_READ_ONLY) - bindImage(1, smoothYccd, GLES31.GL_READ_ONLY) - bindImage(2, destination, GLES31.GL_WRITE_ONLY) - dispatchImage(calculateErrorProgram, "calculate color noise error") - } - - private fun dispatchColorNoiseFilter(destination: Int) { - GLES31.glUseProgram(colorNoiseFilterProgram) - setImageUniforms(colorNoiseFilterProgram) - bindImage(0, originalYccd, GLES31.GL_READ_ONLY) - bindImage(1, smoothYccd, GLES31.GL_READ_ONLY) - bindImage(2, destination, GLES31.GL_WRITE_ONLY) - dispatchImage(colorNoiseFilterProgram, "color noise filter") - } - - private fun dispatchFinal(destination: Int) { - GLES31.glUseProgram(finalProgram) - setImageUniforms(finalProgram) - GLES31.glUniform3fv( - backend.uniformLocation(finalProgram, "uCalculationGains"), - 1, - calculationRgbGains, - 0, - ) - bindImage(0, originalYccd, GLES31.GL_READ_ONLY) - bindImage(1, destination, GLES31.GL_WRITE_ONLY) - dispatchImage(finalProgram, "final camera RGB") - } - - private fun runIirRgb( - source: Int, - destination: Int, - pass: MgcSpatialRgbChromaIirCoefficients.Pass, - filterLuma: Boolean, - label: String, - ): Pair { - val directions = arrayOf( - intArrayOf(0, 0), - intArrayOf(1, 0), - intArrayOf(0, 1), - intArrayOf(1, 1), - ) - var input = source - var output = destination - directions.forEachIndexed { index, direction -> - GLES31.glUseProgram(iirRgbProgram) - setImageUniforms(iirRgbProgram) - setIirCoefficients(iirRgbProgram, pass) - GLES31.glUniform1i(backend.uniformLocation(iirRgbProgram, "uFilterLuma"), if (filterLuma) 1 else 0) - GLES31.glUniform1i(backend.uniformLocation(iirRgbProgram, "uDirection"), direction[0]) - GLES31.glUniform1i(backend.uniformLocation(iirRgbProgram, "uAxis"), direction[1]) - bindImage(0, input, GLES31.GL_READ_ONLY) - bindImage(1, output, GLES31.GL_WRITE_ONLY) - dispatchIir(direction[1], "$label direction $index") - input = output.also { output = input } - } - return input to output - } - - private fun runIirError( - source: Int, - destination: Int, - a10: FloatArray, - b10: FloatArray, - ): Pair { - val directions = arrayOf( - intArrayOf(0, 0), - intArrayOf(1, 0), - intArrayOf(0, 1), - intArrayOf(1, 1), - ) - var input = source - var output = destination - directions.forEachIndexed { index, direction -> - GLES31.glUseProgram(iirErrorProgram) - setImageUniforms(iirErrorProgram) - GLES31.glUniform4fv(backend.uniformLocation(iirErrorProgram, "uA10"), 1, a10, 0) - GLES31.glUniform4fv(backend.uniformLocation(iirErrorProgram, "uB10"), 1, b10, 0) - GLES31.glUniform1i(backend.uniformLocation(iirErrorProgram, "uDirection"), direction[0]) - GLES31.glUniform1i(backend.uniformLocation(iirErrorProgram, "uAxis"), direction[1]) - bindImage(0, input, GLES31.GL_READ_ONLY) - bindImage(1, output, GLES31.GL_WRITE_ONLY) - dispatchIir(direction[1], "IIR2 direction $index") - input = output.also { output = input } - } - return input to output - } - - private fun setIirCoefficients(program: Int, pass: MgcSpatialRgbChromaIirCoefficients.Pass) { - GLES31.glUniform4fv(backend.uniformLocation(program, "uA10"), 1, pass.a10, 0) - GLES31.glUniform4fv(backend.uniformLocation(program, "uB10"), 1, pass.b10, 0) - GLES31.glUniform4fv(backend.uniformLocation(program, "uADyn1"), 1, pass.aDyn1, 0) - GLES31.glUniform4fv(backend.uniformLocation(program, "uBDyn1"), 1, pass.bDyn1, 0) - GLES31.glUniform4fv(backend.uniformLocation(program, "uADyn2"), 1, pass.aDyn2, 0) - GLES31.glUniform4fv(backend.uniformLocation(program, "uBDyn2"), 1, pass.bDyn2, 0) - } - - private fun dispatchImage( - program: Int, - label: String, - ) { - beginTrackedPass(label) - GLES31.glDispatchCompute(groupCount(imageWidth), groupCount(imageHeight), 1) - GlesGpuScheduler.memoryBarrier() - backend.checkGlError("MGC Spatial RGB chroma $label") - clearImageBindings() - passWindow.endPass() - } - - private fun dispatchIir(axis: Int, label: String) { - beginTrackedPass(label) - if (axis == 0) { - GLES31.glDispatchCompute(1, imageHeight, 1) - } else { - GLES31.glDispatchCompute(imageWidth, 1, 1) - } - GlesGpuScheduler.memoryBarrier() - backend.checkGlError("MGC Spatial RGB chroma $label") - clearImageBindings() - passWindow.endPass() - } - - private fun setImageUniforms(program: Int) { - GLES31.glUniform2i(backend.uniformLocation(program, "uImageSize"), imageWidth, imageHeight) - } - - private fun bindImage(unit: Int, texture: Int, access: Int) { - val resource = GlesGpuScheduler.textureResource(texture) - if (access != GLES31.GL_WRITE_ONLY) { - pendingImageReads += resource - } - if (access != GLES31.GL_READ_ONLY) { - pendingImageWrites += resource - } - GLES31.glBindImageTexture( - unit, - texture, - 0, - false, - 0, - access, - GLES30.GL_RGBA16UI, - ) - } - - private fun beginTrackedPass(label: String) { - passWindow.beginPass( - label, - reads = pendingImageReads.toLongArray(), - writes = pendingImageWrites.toLongArray(), - ) - pendingImageReads.clear() - pendingImageWrites.clear() - } - - private fun clearImageBindings() { - for (unit in 0..2) { - GLES31.glBindImageTexture( - unit, - 0, - 0, - false, - 0, - GLES31.GL_READ_ONLY, - GLES30.GL_RGBA16UI, - ) - } - } - - private fun createFullSizeTexture(label: String): Int { - val texture = backend.createRgba16UiTexture( - imageWidth, - imageHeight, - label, - ) - ownedTextures += texture - backend.checkGlError("MGC Spatial RGB chroma $label ${imageWidth}x$imageHeight") - return texture - } - - private fun deleteTexture(texture: Int) { - if (texture == 0) return - backend.deleteTexture(texture, "chroma work texture") - ownedTextures.removeAll { it == texture } - } - - private fun readbackFullSize(texture: Int, outputBuffer: ByteBuffer): ReadbackTiming { - val gpuQueueWaitMs = GlesGpuCompletion.awaitSubmittedWork( - label = "MGC Spatial RGB full-size output", - checkGlError = backend::checkGlError, - ) - ensureReadbackFramebuffer() - val allocationStart = System.currentTimeMillis() - val scratchBytes = readbackTileWidth.toLong() * readbackTileHeight.toLong() * 8L - val scratch = LargeDirectBuffer.allocate( - scratchBytes, - "MGC Spatial RGB full-size readback", - ) - ?: throw IllegalStateException("Unable to allocate full-size MGC Spatial RGB readback scratch") - val allocationMs = System.currentTimeMillis() - allocationStart - outputBuffer.clear() - var glReadMs = 0L - var copyMs = 0L - GLES31.glMemoryBarrier( - GLES31.GL_SHADER_IMAGE_ACCESS_BARRIER_BIT or GLES31.GL_FRAMEBUFFER_BARRIER_BIT, - ) - GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, readbackFbo) - try { - GLES30.glFramebufferTexture2D( - GLES30.GL_FRAMEBUFFER, - GLES30.GL_COLOR_ATTACHMENT0, - GLES30.GL_TEXTURE_2D, - texture, - 0, - ) - GLES30.glReadBuffer(GLES30.GL_COLOR_ATTACHMENT0) - val status = GLES30.glCheckFramebufferStatus(GLES30.GL_FRAMEBUFFER) - check(status == GLES30.GL_FRAMEBUFFER_COMPLETE) { - "Full-size MGC Spatial RGB framebuffer incomplete: 0x${status.toString(16)}" - } - GLES30.glPixelStorei(GLES30.GL_PACK_ALIGNMENT, 8) - tiles.forEach { tile -> - scratch.clear() - val readStart = System.currentTimeMillis() - GLES30.glReadPixels( - tile.outputCore.left, - tile.outputCore.top, - tile.outputCore.width, - tile.outputCore.height, - GLES30.GL_RGBA_INTEGER, - GLES30.GL_UNSIGNED_SHORT, - scratch, - ) - glReadMs += System.currentTimeMillis() - readStart - backend.checkGlError("Full-size MGC Spatial RGB read tile ${tile.index}") - val copyStart = System.currentTimeMillis() - check( - DirectBufferPixelPacker.unpackRgba16TileToRgb16( - source = scratch, - sourceWidth = tile.outputCore.width, - sourceHeight = tile.outputCore.height, - destination = outputBuffer, - destinationWidth = imageWidth, - destinationHeight = imageHeight, - destinationLeft = tile.outputCore.left, - destinationTop = tile.outputCore.top, - ), - ) { "Unable to pack full-size MGC Spatial RGB tile ${tile.index}" } - copyMs += System.currentTimeMillis() - copyStart - backend.yieldToUiRenderer() - } - } finally { - GLES30.glFramebufferTexture2D( - GLES30.GL_FRAMEBUFFER, - GLES30.GL_COLOR_ATTACHMENT0, - GLES30.GL_TEXTURE_2D, - 0, - 0, - ) - GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, 0) - LargeDirectBuffer.free(scratch) - } - outputBuffer.rewind() - return ReadbackTiming( - elapsedMs = gpuQueueWaitMs + allocationMs + glReadMs + copyMs, - gpuQueueWaitMs = gpuQueueWaitMs, - pixelTransferMs = glReadMs, - cpuPackMs = copyMs, - allocMs = allocationMs, - ) - } - - private fun ensureReadbackFramebuffer() { - if (readbackFbo != 0) return - val ids = IntArray(1) - GLES30.glGenFramebuffers(1, ids, 0) - readbackFbo = ids[0] - check(readbackFbo != 0) { "Failed to allocate MGC Spatial RGB chroma readback framebuffer" } - backend.checkGlError("MGC Spatial RGB chroma readback framebuffer") - } - - private fun groupCount(value: Int): Int = - GlesComputeWorkGroup.imageGroupCount(value) -} - -internal data class MgcSpatialRgbChromaIirCoefficients( - val pass1: Pass, - val pass3: Pass, -) { - data class Pass( - val a10: FloatArray, - val b10: FloatArray, - val aDyn1: FloatArray, - val bDyn1: FloatArray, - val aDyn2: FloatArray, - val bDyn2: FloatArray, - ) - - companion object { - private val pass1Base = Pass( - a10 = floatArrayOf(0.0674552768f, 0.134910554f, 0.0674552768f, 0f), - b10 = floatArrayOf(1f, -1.14298046f, 0.412801594f, 0f), - aDyn1 = floatArrayOf(0.00580812711f, 0.0116162542f, 0.00580812711f, 0f), - bDyn1 = floatArrayOf(1f, -1.86380053f, 0.887032986f, 0f), - aDyn2 = floatArrayOf(0.00537849404f, 0.0107569881f, 0.00537849404f, 0f), - bDyn2 = floatArrayOf(1f, -1.72593343f, 0.747447371f, 0f), - ) - private val pass3Base = Pass( - a10 = pass1Base.a10, - b10 = pass1Base.b10, - aDyn1 = floatArrayOf(0.0331984349f, 0.0663968697f, 0.0331984349f, 0f), - bDyn1 = floatArrayOf(1f, -1.61172712f, 0.744520843f, 0f), - aDyn2 = floatArrayOf(0.0281187538f, 0.0562375076f, 0.0281187538f, 0f), - bDyn2 = floatArrayOf(1f, -1.36511719f, 0.47759226f, 0f), - ) - - fun forOutputScale(outputScale: Float): MgcSpatialRgbChromaIirCoefficients { - val scale = outputScale.takeIf { it.isFinite() && it > 0f }?.coerceAtLeast(1f) ?: 1f - return MgcSpatialRgbChromaIirCoefficients( - pass1 = scalePass(pass1Base, scale), - pass3 = scalePass(pass3Base, scale), - ) - } - - private fun scalePass(pass: Pass, scale: Float): Pass { - val (a10, b10) = scaleLowPass(pass.a10, pass.b10, scale) - val (aDyn1, bDyn1) = scaleLowPass(pass.aDyn1, pass.bDyn1, scale) - val (aDyn2, bDyn2) = scaleLowPass(pass.aDyn2, pass.bDyn2, scale) - return Pass(a10, b10, aDyn1, bDyn1, aDyn2, bDyn2) - } - - /** Keeps the same RAW-space cutoff when the MGC Spatial RGB sample grid is upscaled. */ - private fun scaleLowPass( - numerator: FloatArray, - denominator: FloatArray, - scale: Float, - ): Pair { - if (scale == 1f) return numerator.copyOf() to denominator.copyOf() - val a1 = denominator[1].toDouble() - val a2 = denominator[2].toDouble() - val alpha = (1.0 - a2) / (1.0 + a2) - val cosOmega = (-a1 * (1.0 + alpha) * 0.5).coerceIn(-1.0, 1.0) - val omega = acos(cosOmega) - val sinOmega = sin(omega) - val q = if (alpha > 1e-9) sinOmega / (2.0 * alpha) else 0.7071067811865476 - val scaledOmega = (omega / scale.toDouble()).coerceIn(1e-5, Math.PI - 1e-5) - val scaledAlpha = sin(scaledOmega) / (2.0 * max(q, 1e-6)) - val norm = 1.0 / (1.0 + scaledAlpha) - val b0 = (1.0 - cos(scaledOmega)) * 0.5 * norm - val b1 = (1.0 - cos(scaledOmega)) * norm - val scaledA1 = -2.0 * cos(scaledOmega) * norm - val scaledA2 = (1.0 - scaledAlpha) * norm - return floatArrayOf(b0.toFloat(), b1.toFloat(), b0.toFloat(), 0f) to - floatArrayOf(1f, scaledA1.toFloat(), scaledA2.toFloat(), 0f) - } - } -} - -internal object GlesMgcSpatialRgbChromaShaders { - private val imageCommon = """ - uniform ivec2 uImageSize; - - ivec2 imagePosition(ivec2 p) { - return clamp(p, ivec2(0), uImageSize - ivec2(1)); - } - - int chromaSigned(uint c) { - int value = int(c); - return value > 32767 ? value - 65536 : value; - } - - uint chromaUnsigned(int value) { - return uint(value & 0xFFFF); - } - - float uint16ToFloat(uint value) { - uint high8 = value >> 8u; - uint low8 = value & 255u; - // Some Mali drivers lower a direct uint16 -> float conversion to FP16. Preserve the - // complete code ordering while mapping the top code to FP16's largest finite value. - const float finiteScale = 65504.0 / 65535.0; - return float(high8) * (256.0 * finiteScale) + float(low8) * finiteScale; - } - """.trimIndent() - - val seed = """ - #version 310 es - precision highp float; - precision highp int; - precision highp uimage2D; - layout(local_size_x = 8, local_size_y = 8) in; - - layout(rgba16ui, binding = 0) readonly uniform highp uimage2D uCameraRgb; - layout(rgba16ui, binding = 1) writeonly uniform highp uimage2D uYccd; - uniform vec3 uCalculationGains; - uniform float uMinimumDirectionGradient; - - $imageCommon - - vec3 calculationRgbAt(ivec2 p) { - uvec3 encoded = imageLoad(uCameraRgb, imagePosition(p)).rgb; - vec3 cameraRgb = vec3( - uint16ToFloat(encoded.r), - uint16ToFloat(encoded.g), - uint16ToFloat(encoded.b) - ); - return clamp(cameraRgb * uCalculationGains, vec3(0.0), vec3(65504.0)); - } - - float yAt(ivec2 p) { - vec3 rgb = calculationRgbAt(p); - return dot(rgb, vec3(0.25, 0.5, 0.25)); - } - - uint directionMaskAt(ivec2 p) { - const ivec2 directions[8] = ivec2[8]( - ivec2(0, -1), - ivec2(1, 0), - ivec2(0, 1), - ivec2(-1, 0), - ivec2(1, -1), - ivec2(1, 1), - ivec2(-1, 1), - ivec2(-1, -1) - ); - float center = yAt(p); - float gradients[8]; - float minGradient = 65504.0; - float maxGradient = 0.0; - for (int i = 0; i < 8; ++i) { - float first = yAt(p + directions[i]); - float second = yAt(p + directions[i] * 2); - float rgbGradient = abs(center - first) + 0.5 * abs(first - second); - gradients[i] = rgbGradient; - minGradient = min(minGradient, gradients[i]); - maxGradient = max(maxGradient, gradients[i]); - } - float threshold = max( - uMinimumDirectionGradient, - 1.5 * minGradient + 0.09375 * (maxGradient - minGradient) - ); - int mask = 0; - int count = 0; - for (int i = 0; i < 8; ++i) { - if (gradients[i] <= threshold) { - mask |= 1 << i; - count += 1; - } - } - if (count == 0) { - mask = 0xFF; - count = 8; - } - return uint(mask | (count << 8)); - } - - uvec4 rgbToYccd(vec3 rgb, uint directionMask) { - float sum = rgb.r + 2.0 * rgb.g + rgb.b + 1.0; - vec2 chroma = (rgb.rb - vec2(rgb.g)) * (32768.0 / sum); - ivec2 signedChroma = ivec2(clamp(chroma, vec2(-32768.0), vec2(32767.0))); - return uvec4( - uint(clamp(0.25 * (sum - 1.0), 0.0, 65504.0)), - chromaUnsigned(signedChroma.x), - chromaUnsigned(signedChroma.y), - directionMask - ); - } - - void main() { - ivec2 p = ivec2(gl_GlobalInvocationID.xy); - if (any(greaterThanEqual(p, uImageSize))) return; - imageStore(uYccd, imagePosition(p), rgbToYccd(calculationRgbAt(p), directionMaskAt(p))); - } - """.trimIndent() - - val colorNoise1 = """ - #version 310 es - precision highp float; - precision highp int; - precision highp uimage2D; - layout(local_size_x = 8, local_size_y = 8) in; - - layout(rgba16ui, binding = 0) readonly uniform highp uimage2D uInput; - layout(rgba16ui, binding = 1) writeonly uniform highp uimage2D uOutput; - - $imageCommon - - uint yAt(ivec2 p) { - return imageLoad(uInput, imagePosition(p)).r; - } - - void main() { - ivec2 p = ivec2(gl_GlobalInvocationID.xy); - if (any(greaterThanEqual(p, uImageSize))) return; - uvec4 result = imageLoad(uInput, imagePosition(p)); - uint yMin = 65535u; - uint yMax = 0u; - for (int y = -1; y <= 1; ++y) { - for (int x = -1; x <= 1; ++x) { - if (x == 0 && y == 0) continue; - uint value = yAt(p + ivec2(x, y)); - yMin = min(yMin, value); - yMax = max(yMax, value); - } - } - result.r = clamp(result.r, yMin, yMax); - imageStore(uOutput, imagePosition(p), result); - } - """.trimIndent() - - val colorNoise2 = """ - #version 310 es - precision highp float; - precision highp int; - precision highp uimage2D; - layout(local_size_x = 8, local_size_y = 8) in; - - layout(rgba16ui, binding = 0) readonly uniform highp uimage2D uInput; - layout(rgba16ui, binding = 1) writeonly uniform highp uimage2D uOutput; - - $imageCommon - - ivec3 signedYccdAt(ivec2 p) { - uvec4 value = imageLoad(uInput, imagePosition(p)); - return ivec3(int(value.r), chromaSigned(value.g), chromaSigned(value.b)); - } - - void main() { - ivec2 p = ivec2(gl_GlobalInvocationID.xy); - if (any(greaterThanEqual(p, uImageSize))) return; - - ivec3 row0 = signedYccdAt(p + ivec2(-1, -1)); - ivec3 row1 = signedYccdAt(p + ivec2(0, -1)); - ivec3 row2 = signedYccdAt(p + ivec2(1, -1)); - ivec2 rowChroma = row0.yz + row1.yz + row2.yz - - min(min(row0.yz, row1.yz), row2.yz) - max(max(row0.yz, row1.yz), row2.yz); - ivec2 chromaMin = rowChroma; - ivec2 chromaMax = rowChroma; - ivec2 chromaSum = rowChroma; - int ySum = row0.x + 2 * row1.x + row2.x; - - row0 = signedYccdAt(p + ivec2(-1, 0)); - row1 = signedYccdAt(p); - row2 = signedYccdAt(p + ivec2(1, 0)); - rowChroma = row0.yz + row1.yz + row2.yz - - min(min(row0.yz, row1.yz), row2.yz) - max(max(row0.yz, row1.yz), row2.yz); - chromaSum += rowChroma; - chromaMin = min(chromaMin, rowChroma); - chromaMax = max(chromaMax, rowChroma); - ySum += 2 * row0.x + 4 * row1.x + 2 * row2.x; - - row0 = signedYccdAt(p + ivec2(-1, 1)); - row1 = signedYccdAt(p + ivec2(0, 1)); - row2 = signedYccdAt(p + ivec2(1, 1)); - rowChroma = row0.yz + row1.yz + row2.yz - - min(min(row0.yz, row1.yz), row2.yz) - max(max(row0.yz, row1.yz), row2.yz); - chromaSum += rowChroma - min(chromaMin, rowChroma) - max(chromaMax, rowChroma); - ySum += row0.x + 2 * row1.x + row2.x; - - uvec4 center = imageLoad(uInput, imagePosition(p)); - imageStore(uOutput, imagePosition(p), uvec4( - uint(max(ySum / 16, 0)), - chromaUnsigned(chromaSum.x), - chromaUnsigned(chromaSum.y), - center.a - )); - } - """.trimIndent() - - val colorNoise3Smooth = """ - #version 310 es - precision highp float; - precision highp int; - precision highp uimage2D; - layout(local_size_x = 8, local_size_y = 8) in; - - layout(rgba16ui, binding = 0) readonly uniform highp uimage2D uYccd; - layout(rgba16ui, binding = 1) readonly uniform highp uimage2D uSmooth; - layout(rgba16ui, binding = 2) writeonly uniform highp uimage2D uOutputSmooth; - - $imageCommon - - vec3 smoothAt(ivec2 p) { - uvec4 value = imageLoad(uSmooth, imagePosition(p)); - return vec3( - uint16ToFloat(value.r), - float(chromaSigned(value.g)), - float(chromaSigned(value.b)) - ); - } - - vec4 colorFilter3(uvec4 encoded, ivec2 p) { - vec3 negative[4]; - vec3 positive[4]; - negative[0] = smoothAt(p + ivec2(-1, 0)); - positive[0] = smoothAt(p + ivec2(1, 0)); - negative[1] = smoothAt(p + ivec2(0, -1)); - positive[1] = smoothAt(p + ivec2(0, 1)); - negative[2] = smoothAt(p + ivec2(1, -1)); - positive[2] = smoothAt(p + ivec2(-1, 1)); - negative[3] = smoothAt(p + ivec2(-1, -1)); - positive[3] = smoothAt(p + ivec2(1, 1)); - - vec3 center = vec3( - uint16ToFloat(encoded.r), - float(chromaSigned(encoded.g)), - float(chromaSigned(encoded.b)) - ); - vec4 filteredCr; - vec4 filteredCb; - vec4 scale; - for (int i = 0; i < 4; ++i) { - vec3 a = negative[i]; - vec3 b = positive[i]; - float differenceA = dot(abs(center - a), vec3(1.0 / 6.0)); - float differenceB = dot(abs(center - b), vec3(1.0 / 6.0)); - float total = differenceA + differenceB; - vec3 directional = total != 0.0 ? - (a * differenceB + b * differenceA) / total : (a + b) * 0.5; - scale[i] = min(abs(center.x - directional.x) / - max(center.x + directional.x, 1.0) * 2.0, 1.0); - filteredCr[i] = mix(center.y, directional.y, scale[i]); - filteredCb[i] = mix(center.z, directional.z, scale[i]); - } - - vec3 selected = vec3(0.0); - int count = 0; - int direction = int(encoded.a); - if ((direction & (1 << 0)) != 0) { selected += vec3(filteredCr[0], filteredCb[0], scale[0]); count++; } - if ((direction & (1 << 1)) != 0) { selected += vec3(filteredCr[0], filteredCb[0], scale[0]); count++; } - if ((direction & (1 << 2)) != 0) { selected += vec3(filteredCr[1], filteredCb[1], scale[1]); count++; } - if ((direction & (1 << 3)) != 0) { selected += vec3(filteredCr[1], filteredCb[1], scale[1]); count++; } - if ((direction & (1 << 4)) != 0) { selected += vec3(filteredCr[2], filteredCb[2], scale[2]); count++; } - if ((direction & (1 << 5)) != 0) { selected += vec3(filteredCr[2], filteredCb[2], scale[2]); count++; } - if ((direction & (1 << 6)) != 0) { selected += vec3(filteredCr[3], filteredCb[3], scale[3]); count++; } - if ((direction & (1 << 7)) != 0) { selected += vec3(filteredCr[3], filteredCb[3], scale[3]); count++; } - if (count > 0) selected /= float(count); - - float minimumScale = min(min(scale.x, scale.y), min(scale.z, scale.w)); - float yScale = clamp(1.0 - center.x / 16384.0 * minimumScale, 0.0, 1.0); - return vec4(center.x, yScale * selected.x, yScale * selected.y, selected.z * 65504.0); - } - - void main() { - ivec2 p = ivec2(gl_GlobalInvocationID.xy); - if (any(greaterThanEqual(p, uImageSize))) return; - uvec4 encoded = imageLoad(uYccd, imagePosition(p)); - vec4 filtered = vec4( - uint16ToFloat(encoded.r), - float(chromaSigned(encoded.g)), - float(chromaSigned(encoded.b)), - 0.0 - ); - if (encoded.r != 0u && encoded.a != 0u) { - filtered = colorFilter3(encoded, p); - } - uint error = uint(clamp(filtered.w, 0.0, 65504.0)); - - uvec4 smoothValue = imageLoad(uSmooth, imagePosition(p)); - ivec2 smoothChroma = ivec2(chromaSigned(smoothValue.g), chromaSigned(smoothValue.b)); - ivec2 filteredChroma = ivec2(filtered.yz); - if (abs(smoothChroma.x) + abs(smoothChroma.y) < - abs(filteredChroma.x) + abs(filteredChroma.y)) { - filteredChroma = smoothChroma; - } - imageStore(uOutputSmooth, imagePosition(p), uvec4( - uint(clamp(filtered.x, 0.0, 65504.0)), - chromaUnsigned(filteredChroma.x), - chromaUnsigned(filteredChroma.y), - error - )); - } - """.trimIndent() - - val restoreOriginal = """ - #version 310 es - precision highp int; - precision highp uimage2D; - layout(local_size_x = 8, local_size_y = 8) in; - - layout(rgba16ui, binding = 0) readonly uniform highp uimage2D uSmooth; - layout(rgba16ui, binding = 1) readonly uniform highp uimage2D uDirectionalSource; - layout(rgba16ui, binding = 2) writeonly uniform highp uimage2D uOriginal; - - $imageCommon - - void main() { - ivec2 p = ivec2(gl_GlobalInvocationID.xy); - if (any(greaterThanEqual(p, uImageSize))) return; - ivec2 storage = p; - uvec4 smoothValue = imageLoad(uSmooth, storage); - uint direction = imageLoad(uDirectionalSource, storage).a; - imageStore(uOriginal, storage, uvec4(smoothValue.rgb, direction)); - } - """.trimIndent() - - val iirRgb = """ - #version 310 es - precision highp float; - precision highp int; - precision highp uimage2D; - layout(local_size_x = 1, local_size_y = 1) in; - - layout(rgba16ui, binding = 0) readonly uniform highp uimage2D uInput; - layout(rgba16ui, binding = 1) writeonly uniform highp uimage2D uOutput; - uniform vec4 uA10; - uniform vec4 uB10; - uniform vec4 uADyn1; - uniform vec4 uBDyn1; - uniform vec4 uADyn2; - uniform vec4 uBDyn2; - uniform int uDirection; - uniform int uAxis; - uniform int uFilterLuma; - - $imageCommon - - struct Delayer { - float x0; - float x1; - float y0; - float y1; - }; - - float applyFilter(inout Delayer state, float value, vec4 a, vec4 b, bool clampUnsigned) { - float result = a[0] * value + a[1] * state.x0 + a[2] * state.x1 - - b[1] * state.y0 - b[2] * state.y1; - state.x1 = state.x0; - state.y1 = state.y0; - state.x0 = value; - state.y0 = clampUnsigned ? clamp(result, 0.0, 65504.0) : result; - return state.y0; - } - - float steadyOutput(float value, vec4 a, vec4 b, bool clampUnsigned) { - float result = value * (a[0] + a[1] + a[2]) / - (1.0 + b[1] + b[2]); - return clampUnsigned ? clamp(result, 0.0, 65504.0) : result; - } - - Delayer steadyState(float sourceValue, float outputValue) { - return Delayer(sourceValue, sourceValue, outputValue, outputValue); - } - - ivec2 linePosition(int inner, int outer) { - return uAxis == 0 ? ivec2(inner, outer) : ivec2(outer, inner); - } - - void filterSample( - uvec4 pixel, - inout Delayer yState, - inout Delayer crState1, - inout Delayer cbState1, - inout Delayer crState2, - inout Delayer cbState2, - out float y, - out float cr, - out float cb - ) { - y = uFilterLuma != 0 ? - applyFilter(yState, uint16ToFloat(pixel.r), uA10, uB10, true) : - uint16ToFloat(pixel.r); - cr = applyFilter(crState1, float(chromaSigned(pixel.g)), uADyn1, uBDyn1, false); - cb = applyFilter(cbState1, float(chromaSigned(pixel.b)), uADyn1, uBDyn1, false); - cr = applyFilter(crState2, cr, uADyn2, uBDyn2, false); - cb = applyFilter(cbState2, cb, uADyn2, uBDyn2, false); - } - - void main() { - int outer = uAxis == 0 ? int(gl_GlobalInvocationID.y) : int(gl_GlobalInvocationID.x); - int outerLimit = uAxis == 0 ? uImageSize.y : uImageSize.x; - int innerSize = uAxis == 0 ? uImageSize.x : uImageSize.y; - if (outer >= outerLimit) return; - int start = uDirection == 0 ? 0 : innerSize - 1; - int step = uDirection == 0 ? 1 : -1; - - uvec4 boundary = imageLoad(uInput, linePosition(start, outer)); - float boundaryY = uint16ToFloat(boundary.r); - float boundaryCr = float(chromaSigned(boundary.g)); - float boundaryCb = float(chromaSigned(boundary.b)); - float steadyY = steadyOutput(boundaryY, uA10, uB10, true); - float steadyCr1 = steadyOutput(boundaryCr, uADyn1, uBDyn1, false); - float steadyCb1 = steadyOutput(boundaryCb, uADyn1, uBDyn1, false); - float steadyCr2 = steadyOutput(steadyCr1, uADyn2, uBDyn2, false); - float steadyCb2 = steadyOutput(steadyCb1, uADyn2, uBDyn2, false); - Delayer yState = steadyState(boundaryY, steadyY); - Delayer crState1 = steadyState(boundaryCr, steadyCr1); - Delayer cbState1 = steadyState(boundaryCb, steadyCb1); - Delayer crState2 = steadyState(steadyCr1, steadyCr2); - Delayer cbState2 = steadyState(steadyCb1, steadyCb2); - float y; - float cr; - float cb; - for (int i = 0; i < innerSize; ++i) { - ivec2 p = linePosition(start + i * step, outer); - ivec2 storage = p; - uvec4 pixel = imageLoad(uInput, storage); - filterSample( - pixel, - yState, - crState1, - cbState1, - crState2, - cbState2, - y, - cr, - cb - ); - imageStore(uOutput, storage, uvec4( - uint(clamp(y, 0.0, 65504.0)), - chromaUnsigned(int(cr)), - chromaUnsigned(int(cb)), - pixel.a - )); - } - } - """.trimIndent() - - val iirError = """ - #version 310 es - precision highp float; - precision highp int; - precision highp uimage2D; - layout(local_size_x = 1, local_size_y = 1) in; - - layout(rgba16ui, binding = 0) readonly uniform highp uimage2D uInput; - layout(rgba16ui, binding = 1) writeonly uniform highp uimage2D uOutput; - uniform vec4 uA10; - uniform vec4 uB10; - uniform int uDirection; - uniform int uAxis; - - $imageCommon - - struct Delayer { - float x0; - float x1; - float y0; - float y1; - }; - - float applyFilter(inout Delayer state, float value) { - float result = uA10[0] * value + uA10[1] * state.x0 + uA10[2] * state.x1 - - uB10[1] * state.y0 - uB10[2] * state.y1; - state.x1 = state.x0; - state.y1 = state.y0; - state.x0 = value; - state.y0 = clamp(result, 0.0, 65504.0); - return state.y0; - } - - Delayer steadyState(float value) { - float outputValue = value * (uA10[0] + uA10[1] + uA10[2]) / - (1.0 + uB10[1] + uB10[2]); - outputValue = clamp(outputValue, 0.0, 65504.0); - return Delayer(value, value, outputValue, outputValue); - } - - ivec2 linePosition(int inner, int outer) { - return uAxis == 0 ? ivec2(inner, outer) : ivec2(outer, inner); - } - - void main() { - int outer = uAxis == 0 ? int(gl_GlobalInvocationID.y) : int(gl_GlobalInvocationID.x); - int outerLimit = uAxis == 0 ? uImageSize.y : uImageSize.x; - int innerSize = uAxis == 0 ? uImageSize.x : uImageSize.y; - if (outer >= outerLimit) return; - int start = uDirection == 0 ? 0 : innerSize - 1; - int step = uDirection == 0 ? 1 : -1; - uvec4 boundary = imageLoad(uInput, linePosition(start, outer)); - Delayer state = steadyState(uint16ToFloat(boundary.a)); - for (int i = 0; i < innerSize; ++i) { - ivec2 p = linePosition(start + i * step, outer); - ivec2 storage = p; - uvec4 pixel = imageLoad(uInput, storage); - pixel.a = uint(applyFilter(state, uint16ToFloat(pixel.a))); - imageStore(uOutput, storage, pixel); - } - } - """.trimIndent() - - val calculateColorNoiseError = """ - #version 310 es - precision highp float; - precision highp int; - precision highp uimage2D; - layout(local_size_x = 8, local_size_y = 8) in; - - layout(rgba16ui, binding = 0) readonly uniform highp uimage2D uOriginal; - layout(rgba16ui, binding = 1) readonly uniform highp uimage2D uSmooth; - layout(rgba16ui, binding = 2) writeonly uniform highp uimage2D uOutput; - - $imageCommon - - int directionCount(int encoded) { - return (encoded >> 8) & 0x0F; - } - - void main() { - ivec2 p = ivec2(gl_GlobalInvocationID.xy); - if (any(greaterThanEqual(p, uImageSize))) return; - ivec2 storage = p; - uvec4 original = imageLoad(uOriginal, storage); - uvec4 smoothValue = imageLoad(uSmooth, storage); - if (p.x == 0 || p.y == 0 || p.x + 1 >= uImageSize.x || p.y + 1 >= uImageSize.y) { - imageStore(uOutput, storage, smoothValue); - return; - } - - const ivec2 directions[8] = ivec2[8]( - ivec2(0, -1), - ivec2(1, 0), - ivec2(0, 1), - ivec2(-1, 0), - ivec2(1, -1), - ivec2(1, 1), - ivec2(-1, 1), - ivec2(-1, -1) - ); - int encodedDirection = int(original.a); - int ySum = 0; - int crSum = 0; - int cbSum = 0; - for (int i = 0; i < 8; ++i) { - if ((encodedDirection & (1 << i)) != 0) { - uvec4 neighbor = imageLoad(uOriginal, p + directions[i]); - ySum += int(original.r) - int(neighbor.r); - crSum += chromaSigned(original.g) - chromaSigned(neighbor.g); - cbSum += chromaSigned(original.b) - chromaSigned(neighbor.b); - } - } - int count = directionCount(encodedDirection); - int directionBits = encodedDirection & 0xFF; - int newError = (directionBits == 0x50 || directionBits == 0xA0) ? - abs(crSum + cbSum) : 0; - if (count > 0) newError = (newError + abs(ySum)) / count; - float minimumLevel = 0.05 * uint16ToFloat(smoothValue.r); - float scaledError = uint16ToFloat(smoothValue.a) * - clamp(float(newError) - minimumLevel, 0.0, 1.0); - smoothValue.a = uint(clamp(scaledError, 0.0, 65504.0)); - imageStore(uOutput, storage, smoothValue); - } - """.trimIndent() - - val colorNoiseFilter = """ - #version 310 es - precision highp float; - precision highp int; - precision highp uimage2D; - layout(local_size_x = 8, local_size_y = 8) in; - - layout(rgba16ui, binding = 0) readonly uniform highp uimage2D uOriginal; - layout(rgba16ui, binding = 1) readonly uniform highp uimage2D uSmooth; - layout(rgba16ui, binding = 2) writeonly uniform highp uimage2D uOutput; - - $imageCommon - - float minMaxScale(float low, float value, float high) { - return (clamp(value, low, high) - low) / (high - low); - } - - void main() { - ivec2 p = ivec2(gl_GlobalInvocationID.xy); - if (any(greaterThanEqual(p, uImageSize))) return; - ivec2 storage = p; - uvec4 original = imageLoad(uOriginal, storage); - uvec4 smoothValue = imageLoad(uSmooth, storage); - int cr = chromaSigned(original.g); - int cb = chromaSigned(original.b); - int smoothCr = chromaSigned(smoothValue.g); - int smoothCb = chromaSigned(smoothValue.b); - float errorScale = minMaxScale(100.0, uint16ToFloat(smoothValue.a) * 0.25, 300.0); - float smoothSaturation = 1.0 + float(abs(smoothCr)) + float(abs(smoothCb)); - float normalSaturation = float(abs(cr)) + float(abs(cb)); - float saturationScale = minMaxScale(0.5, normalSaturation / smoothSaturation, 1.0); - float factor = errorScale * saturationScale; - original.g = chromaUnsigned(int(mix(float(cr), float(smoothCr), factor))); - original.b = chromaUnsigned(int(mix(float(cb), float(smoothCb), factor))); - original.a = smoothValue.a; - imageStore(uOutput, storage, original); - } - """.trimIndent() - - val finalCameraRgb = """ - #version 310 es - precision highp float; - precision highp int; - precision highp uimage2D; - layout(local_size_x = 8, local_size_y = 8) in; - - layout(rgba16ui, binding = 0) readonly uniform highp uimage2D uInput; - layout(rgba16ui, binding = 1) writeonly uniform highp uimage2D uOutput; - uniform vec3 uCalculationGains; - - $imageCommon - - vec3 yccdToCalculationRgb(uvec4 encoded) { - float y = clamp(uint16ToFloat(encoded.r), 0.0, 65504.0); - float cr = float(chromaSigned(encoded.g)); - float cb = float(chromaSigned(encoded.b)); - float red = clamp(y * (1.0 + (3.0 * cr - cb) / 32768.0), 0.0, 65504.0); - float blue = clamp(y * (1.0 + (3.0 * cb - cr) / 32768.0), 0.0, 65504.0); - float green = clamp((4.0 * y - red - blue) * 0.5, 0.0, 65504.0); - return vec3(red, green, blue); - } - - void main() { - ivec2 p = ivec2(gl_GlobalInvocationID.xy); - if (any(greaterThanEqual(p, uImageSize))) return; - uvec4 encoded = imageLoad(uInput, p); - vec3 cameraRgb = yccdToCalculationRgb(encoded) / - max(uCalculationGains, vec3(1e-6)); - uvec4 outputPixel = uvec4( - uvec3(clamp(cameraRgb, vec3(0.0), vec3(65504.0)) + vec3(0.5)), - 65535u - ); - imageStore(uOutput, p, outputPixel); - } - """.trimIndent() -} diff --git a/app/src/main/java/com/hinnka/mycamera/processor/GlesMgcSpatialRgbOutputAssembler.kt b/app/src/main/java/com/hinnka/mycamera/processor/GlesMgcSpatialRgbOutputAssembler.kt new file mode 100644 index 00000000..76a44055 --- /dev/null +++ b/app/src/main/java/com/hinnka/mycamera/processor/GlesMgcSpatialRgbOutputAssembler.kt @@ -0,0 +1,287 @@ +package com.hinnka.mycamera.processor + +import android.opengl.GLES30 +import android.opengl.GLES31 +import com.hinnka.mycamera.utils.DirectBufferPixelPacker +import com.hinnka.mycamera.utils.LargeDirectBuffer +import java.nio.ByteBuffer + +/** + * Full-size RGBA16UI output boundary for MGC Spatial RGB and Sabre VGN. + * + * Fusion/Resolve tiles are assembled into one full-size 2D texture owned by this stage. The merged + * un-white-balanced camera RGB is handed off unchanged: either exported as a GPU texture or read + * back into the packed RGB16 CPU buffer. No post-fusion color noise or IIR filtering runs here. + */ +internal class GlesMgcSpatialRgbOutputAssembler( + private val imageWidth: Int, + private val imageHeight: Int, + private val backend: Backend, + private val exportFullSizeTexture: Boolean = false, +) { + interface Backend { + fun createRgba16UiTexture(width: Int, height: Int, label: String): Int + + fun deleteTexture(texture: Int, label: String) + + fun transferTextureOwnership(texture: Int, label: String) + + fun checkGlError(label: String) + + fun yieldToUiRenderer() + } + + data class ReadbackTiming( + val elapsedMs: Long, + val gpuQueueWaitMs: Long, + val pixelTransferMs: Long, + val cpuPackMs: Long, + val allocMs: Long, + ) + + data class ProcessResult( + val readbackTiming: ReadbackTiming, + val exportedTextureId: Int, + val cpuBufferPopulated: Boolean, + ) + + private var tiles: List = emptyList() + private var readbackTileWidth = 0 + private var readbackTileHeight = 0 + + private var assembledRgb = 0 + private var writtenTileCount = 0 + private var readbackFbo = 0 + + init { + require(imageWidth > 0 && imageHeight > 0) + } + + private fun validateAndSetTiles(newTiles: List) { + require(newTiles.isNotEmpty()) + check(assembledRgb == 0) { "MGC Spatial RGB output storage is already active" } + val tiles = newTiles.toList() + val rows = tiles.groupBy { it.outputCore.top }.toSortedMap().values.toList() + var expectedTop = 0 + var expectedIndex = 0 + rows.forEachIndexed { rowIndex, row -> + val orderedRow = row.sortedBy { it.outputCore.left } + require(orderedRow.first().outputCore.top == expectedTop) { + "MGC Spatial RGB tile row $rowIndex starts at ${orderedRow.first().outputCore.top}, " + + "expected $expectedTop" + } + val rowBottom = orderedRow.first().outputCore.bottom + var expectedLeft = 0 + orderedRow.forEach { tile -> + require(tile.index == expectedIndex++) { + "MGC Spatial RGB tiles must be row-major" + } + require(tile.outputCore.left == expectedLeft && tile.outputCore.top == expectedTop) { + "MGC Spatial RGB tile ${tile.index} leaves a gap or overlaps another tile" + } + require(tile.outputCore.bottom == rowBottom) { + "MGC Spatial RGB tile row $rowIndex has inconsistent heights" + } + expectedLeft = tile.outputCore.right + } + require(expectedLeft == imageWidth) { + "MGC Spatial RGB tile row $rowIndex covers width $expectedLeft, expected $imageWidth" + } + expectedTop = rowBottom + } + require(expectedTop == imageHeight) { + "MGC Spatial RGB tiles cover height $expectedTop, expected $imageHeight" + } + readbackTileWidth = tiles.maxOf { it.outputCore.width } + readbackTileHeight = tiles.maxOf { it.outputCore.height } + this.tiles = tiles + } + + fun initStorage(tiles: List) { + validateAndSetTiles(tiles) + val maxTextureSize = IntArray(1) + GLES30.glGetIntegerv(GLES30.GL_MAX_TEXTURE_SIZE, maxTextureSize, 0) + require(imageWidth <= maxTextureSize[0] && imageHeight <= maxTextureSize[0]) { + "MGC Spatial RGB output requires ${imageWidth}x$imageHeight 2D textures; " + + "GLES supports up to ${maxTextureSize[0]}" + } + assembledRgb = backend.createRgba16UiTexture(imageWidth, imageHeight, "assembled camera RGB") + backend.checkGlError("MGC Spatial RGB output storage ${imageWidth}x$imageHeight") + writtenTileCount = 0 + } + + /** + * Full-size integer render target owned by this stage. + * + * The normalize/Resolve fragment pass renders each tile directly into its global viewport on + * this texture, which is then exported or read back without another image copy. + */ + fun normalizationTargetTexture(): Int { + check(assembledRgb != 0) { "MGC Spatial RGB output storage is not initialized" } + return assembledRgb + } + + fun markTileWritten(tile: MgcSpatialRgbTile) { + check(writtenTileCount < tiles.size) { "All MGC Spatial RGB tiles are already assembled" } + check(tiles[writtenTileCount].index == tile.index) { + "MGC Spatial RGB tile ${tile.index} was written out of row-major order; " + + "expected ${tiles[writtenTileCount].index}" + } + writtenTileCount += 1 + } + + fun process( + obtainOutputBuffer: () -> ByteBuffer, + deferFullSizeReadback: Boolean, + onFinalSubmitted: (() -> Unit)? = null, + ): ProcessResult { + check(assembledRgb != 0) { "MGC Spatial RGB output has no assembled fusion output" } + check(writtenTileCount == tiles.size) { + "MGC Spatial RGB output is incomplete: $writtenTileCount/${tiles.size} tiles" + } + onFinalSubmitted?.invoke() + + val cpuBufferPopulated = !exportFullSizeTexture || !deferFullSizeReadback + val timing = if (cpuBufferPopulated) { + readbackFullSize(assembledRgb, obtainOutputBuffer()) + } else { + check(onFinalSubmitted != null) { + "Deferred MGC Spatial RGB export requires a completion checkpoint" + } + ReadbackTiming( + elapsedMs = 0L, + gpuQueueWaitMs = 0L, + pixelTransferMs = 0L, + cpuPackMs = 0L, + allocMs = 0L, + ) + } + + val exportedTextureId = if (exportFullSizeTexture) assembledRgb else 0 + if (exportedTextureId != 0) { + backend.transferTextureOwnership(exportedTextureId, "camera RGB export") + } else { + backend.deleteTexture(assembledRgb, "assembled camera RGB") + } + assembledRgb = 0 + writtenTileCount = 0 + tiles = emptyList() + readbackTileWidth = 0 + readbackTileHeight = 0 + return ProcessResult( + readbackTiming = timing, + exportedTextureId = exportedTextureId, + cpuBufferPopulated = cpuBufferPopulated, + ) + } + + fun release() { + if (assembledRgb != 0) { + backend.deleteTexture(assembledRgb, "assembled camera RGB") + assembledRgb = 0 + } + writtenTileCount = 0 + tiles = emptyList() + readbackTileWidth = 0 + readbackTileHeight = 0 + if (readbackFbo != 0) { + GLES30.glDeleteFramebuffers(1, intArrayOf(readbackFbo), 0) + readbackFbo = 0 + } + } + + private fun readbackFullSize(texture: Int, outputBuffer: ByteBuffer): ReadbackTiming { + val gpuQueueWaitMs = GlesGpuCompletion.awaitSubmittedWork( + label = "MGC Spatial RGB full-size output", + checkGlError = backend::checkGlError, + ) + ensureReadbackFramebuffer() + val allocationStart = System.currentTimeMillis() + val scratchBytes = readbackTileWidth.toLong() * readbackTileHeight.toLong() * 8L + val scratch = LargeDirectBuffer.allocate( + scratchBytes, + "MGC Spatial RGB full-size readback", + ) + ?: throw IllegalStateException("Unable to allocate full-size MGC Spatial RGB readback scratch") + val allocationMs = System.currentTimeMillis() - allocationStart + outputBuffer.clear() + var glReadMs = 0L + var copyMs = 0L + GLES31.glMemoryBarrier( + GLES31.GL_SHADER_IMAGE_ACCESS_BARRIER_BIT or GLES31.GL_FRAMEBUFFER_BARRIER_BIT, + ) + GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, readbackFbo) + try { + GLES30.glFramebufferTexture2D( + GLES30.GL_FRAMEBUFFER, + GLES30.GL_COLOR_ATTACHMENT0, + GLES30.GL_TEXTURE_2D, + texture, + 0, + ) + GLES30.glReadBuffer(GLES30.GL_COLOR_ATTACHMENT0) + val status = GLES30.glCheckFramebufferStatus(GLES30.GL_FRAMEBUFFER) + check(status == GLES30.GL_FRAMEBUFFER_COMPLETE) { + "Full-size MGC Spatial RGB framebuffer incomplete: 0x${status.toString(16)}" + } + GLES30.glPixelStorei(GLES30.GL_PACK_ALIGNMENT, 8) + tiles.forEach { tile -> + scratch.clear() + val readStart = System.currentTimeMillis() + GLES30.glReadPixels( + tile.outputCore.left, + tile.outputCore.top, + tile.outputCore.width, + tile.outputCore.height, + GLES30.GL_RGBA_INTEGER, + GLES30.GL_UNSIGNED_SHORT, + scratch, + ) + glReadMs += System.currentTimeMillis() - readStart + backend.checkGlError("Full-size MGC Spatial RGB read tile ${tile.index}") + val copyStart = System.currentTimeMillis() + check( + DirectBufferPixelPacker.unpackRgba16TileToRgb16( + source = scratch, + sourceWidth = tile.outputCore.width, + sourceHeight = tile.outputCore.height, + destination = outputBuffer, + destinationWidth = imageWidth, + destinationHeight = imageHeight, + destinationLeft = tile.outputCore.left, + destinationTop = tile.outputCore.top, + ), + ) { "Unable to pack full-size MGC Spatial RGB tile ${tile.index}" } + copyMs += System.currentTimeMillis() - copyStart + backend.yieldToUiRenderer() + } + } finally { + GLES30.glFramebufferTexture2D( + GLES30.GL_FRAMEBUFFER, + GLES30.GL_COLOR_ATTACHMENT0, + GLES30.GL_TEXTURE_2D, + 0, + 0, + ) + GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, 0) + LargeDirectBuffer.free(scratch) + } + outputBuffer.rewind() + return ReadbackTiming( + elapsedMs = gpuQueueWaitMs + allocationMs + glReadMs + copyMs, + gpuQueueWaitMs = gpuQueueWaitMs, + pixelTransferMs = glReadMs, + cpuPackMs = copyMs, + allocMs = allocationMs, + ) + } + + private fun ensureReadbackFramebuffer() { + if (readbackFbo != 0) return + val ids = IntArray(1) + GLES30.glGenFramebuffers(1, ids, 0) + readbackFbo = ids[0] + check(readbackFbo != 0) { "Failed to allocate MGC Spatial RGB output readback framebuffer" } + backend.checkGlError("MGC Spatial RGB output readback framebuffer") + } +} diff --git a/app/src/main/java/com/hinnka/mycamera/processor/MgcSpatialRgbMemoryPolicy.kt b/app/src/main/java/com/hinnka/mycamera/processor/MgcSpatialRgbMemoryPolicy.kt index be4309cb..9013aa4d 100644 --- a/app/src/main/java/com/hinnka/mycamera/processor/MgcSpatialRgbMemoryPolicy.kt +++ b/app/src/main/java/com/hinnka/mycamera/processor/MgcSpatialRgbMemoryPolicy.kt @@ -8,9 +8,6 @@ package com.hinnka.mycamera.processor * threshold, the smallest estimated candidate remains runnable. */ internal object MgcSpatialRgbMemoryPolicy { - private const val RGBA16_BYTES_PER_PIXEL = 8L - private const val IIR_SURFACE_COUNT = 3L - fun selectAdvisoryPlanIndex( projectedBytes: List, advisoryBytes: Long, @@ -25,9 +22,4 @@ internal object MgcSpatialRgbMemoryPolicy { projectedBytes.indices.minBy { projectedBytes[it] } } } - - fun iirWorkingSetBytes(width: Int, height: Int): Long { - require(width > 0 && height > 0) - return width.toLong() * height * RGBA16_BYTES_PER_PIXEL * IIR_SURFACE_COUNT - } } diff --git a/app/src/main/java/com/hinnka/mycamera/raw/denoise/PhotonDenoiseShaders.kt b/app/src/main/java/com/hinnka/mycamera/raw/denoise/PhotonDenoiseShaders.kt index 8fdea180..d747bf6a 100644 --- a/app/src/main/java/com/hinnka/mycamera/raw/denoise/PhotonDenoiseShaders.kt +++ b/app/src/main/java/com/hinnka/mycamera/raw/denoise/PhotonDenoiseShaders.kt @@ -35,7 +35,9 @@ internal object PhotonDenoiseShaders { layout(local_size_x = $LOCAL, local_size_y = $LOCAL) in; - ivec2 groupId() { return ivec2(gl_WorkGroupID.xy); } + // Work-group offset of a partial dispatch (row bands of the synthesis); 0 for a whole level. + uniform ivec2 uGroupOffset; + ivec2 groupId() { return ivec2(gl_WorkGroupID.xy) + uGroupOffset; } ivec2 globalId() { return groupId() * $LOCAL + ivec2(gl_LocalInvocationID.xy); } const float LUMA_TAPS[7] = float[7]( diff --git a/app/src/main/java/com/hinnka/mycamera/raw/denoise/PhotonMultiScaleDenoiser.kt b/app/src/main/java/com/hinnka/mycamera/raw/denoise/PhotonMultiScaleDenoiser.kt index ad98b19d..01c0457a 100644 --- a/app/src/main/java/com/hinnka/mycamera/raw/denoise/PhotonMultiScaleDenoiser.kt +++ b/app/src/main/java/com/hinnka/mycamera/raw/denoise/PhotonMultiScaleDenoiser.kt @@ -167,9 +167,8 @@ internal class PhotonMultiScaleDenoiser { bindSynthesize(synthesizeProgram, inputTexture, lscTexture, mergeTexture, gaussians, denoisedCoarse, sizes, level, lumaEnabled, chromaEnabled, bilateralEnabled, params) bindImage(0, result, GLES30.GL_RGBA16F) - dispatch(synthesizeProgram, size) + dispatchBands(synthesizeProgram, size) } - GlesGpuScheduler.yieldToUiRenderer() sink?.onPass(if (level == 0) "output" else "denoised$level", result, size[0], size[1], false) if (timing.ablation) { synthesizeAblation(timing, inputTexture, lscTexture, mergeTexture, gaussians, denoisedCoarse, @@ -235,9 +234,8 @@ internal class PhotonMultiScaleDenoiser { bindSynthesize(synthesizeProgram, inputTexture, lscTexture, mergeTexture, gaussians, denoisedCoarse, sizes, level, variant.luma, variant.chroma, variant.bilateral, params) bindImage(0, scratch, GLES30.GL_RGBA16F) - dispatch(synthesizeProgram, size) + dispatchBands(synthesizeProgram, size) } - GlesGpuScheduler.yieldToUiRenderer() } } } finally { @@ -454,6 +452,31 @@ internal class PhotonMultiScaleDenoiser { ) } + /** + * The fused synthesis in row bands of [SYNTHESIS_BAND_GROUPS] work-group rows, each flushed with + * a yield so the UI renderer gets GPU slots inside a level (L0 alone is ~300ms in one dispatch). + * Tiles read only the inputs and write disjoint pixels, so bands need no barrier between them + * and the result is the same as one whole-level dispatch. + */ + private fun dispatchBands(program: Int, size: IntArray) { + val groupsX = GlesComputeWorkGroup.imageGroupCount(size[0]) + val groupsY = GlesComputeWorkGroup.imageGroupCount(size[1]) + val offset = location(program, "uGroupOffset") + var row = 0 + while (row < groupsY) { + val rows = minOf(SYNTHESIS_BAND_GROUPS, groupsY - row) + GLES31.glUniform2i(offset, 0, row) + GLES31.glDispatchCompute(groupsX, rows, 1) + GlesGpuScheduler.yieldToUiRenderer() + row += rows + } + GLES31.glUniform2i(offset, 0, 0) + GLES31.glMemoryBarrier( + GLES31.GL_SHADER_IMAGE_ACCESS_BARRIER_BIT or GLES31.GL_TEXTURE_FETCH_BARRIER_BIT or + GLES31.GL_FRAMEBUFFER_BARRIER_BIT, + ) + } + private fun bindImage(unit: Int, texture: Int, format: Int) { GLES31.glBindImageTexture(unit, texture, 0, false, 0, GLES31.GL_WRITE_ONLY, format) } @@ -476,6 +499,7 @@ internal class PhotonMultiScaleDenoiser { private companion object { const val ABLATION_REPEATS = 3 + const val SYNTHESIS_BAND_GROUPS = 64 const val UNIT_INPUT = 0 const val UNIT_LSC = 1 const val UNIT_FINE = 2 diff --git a/app/src/test/java/com/hinnka/mycamera/processor/GlesMgcSpatialRgbChromaPostprocessorTest.kt b/app/src/test/java/com/hinnka/mycamera/processor/GlesMgcSpatialRgbChromaPostprocessorTest.kt deleted file mode 100644 index e4e880b5..00000000 --- a/app/src/test/java/com/hinnka/mycamera/processor/GlesMgcSpatialRgbChromaPostprocessorTest.kt +++ /dev/null @@ -1,154 +0,0 @@ -package com.hinnka.mycamera.processor - -import java.io.File -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 - -class GlesMgcSpatialRgbChromaPostprocessorTest { - @Test - fun postprocessorKeepsWbPairedUsesRgbDirectionAndDoesNotReapplyLensShading() { - val seed = GlesMgcSpatialRgbChromaShaders.seed - val finish = GlesMgcSpatialRgbChromaShaders.finalCameraRgb - - assertTrue(seed.contains("cameraRgb * uCalculationGains")) - assertTrue(finish.contains("/\n max(uCalculationGains")) - assertTrue(seed.contains("directionMaskAt(p)")) - assertTrue(seed.contains("float rgbGradient")) - assertTrue(seed.contains("gradients[i] = rgbGradient")) - assertFalse(seed.contains("directionMomentAt")) - assertFalse(seed.contains("doubledDirectionAxes")) - assertTrue(seed.contains("count << 8")) - assertTrue(seed.contains("const float finiteScale = 65504.0 / 65535.0")) - assertTrue(!seed.contains("vec3(imageLoad(uCameraRgb")) - assertTrue(!seed.contains("LensShading")) - assertTrue(!finish.contains("LensShading")) - } - - @Test - fun recursiveFiltersUseAnalyticConstantBoundaryState() { - val rgb = GlesMgcSpatialRgbChromaShaders.iirRgb - val error = GlesMgcSpatialRgbChromaShaders.iirError - - assertTrue(rgb.contains("float steadyOutput")) - assertTrue(rgb.contains("1.0 + b[1] + b[2]")) - assertTrue(rgb.contains("steadyState(boundaryY, steadyY)")) - assertTrue(error.contains("Delayer steadyState(float value)")) - assertTrue(error.contains("1.0 + uB10[1] + uB10[2]")) - assertTrue(!rgb.contains("uBoundaryWarmup")) - assertTrue(!error.contains("uBoundaryWarmup")) - } - - @Test - fun everyFilterUsesDirectCoordinatesOverFullSize2dStorage() { - val globalShaders = listOf( - GlesMgcSpatialRgbChromaShaders.seed, - GlesMgcSpatialRgbChromaShaders.colorNoise1, - GlesMgcSpatialRgbChromaShaders.colorNoise2, - GlesMgcSpatialRgbChromaShaders.colorNoise3Smooth, - GlesMgcSpatialRgbChromaShaders.restoreOriginal, - GlesMgcSpatialRgbChromaShaders.iirRgb, - GlesMgcSpatialRgbChromaShaders.calculateColorNoiseError, - GlesMgcSpatialRgbChromaShaders.iirError, - GlesMgcSpatialRgbChromaShaders.colorNoiseFilter, - GlesMgcSpatialRgbChromaShaders.finalCameraRgb, - ) - - globalShaders.forEach { shader -> - assertTrue(shader.contains("uimage2D")) - assertTrue(!shader.contains("uimage2DArray")) - assertTrue(!shader.contains("uTileLefts")) - assertTrue(!shader.contains("uTileTops")) - assertTrue(!shader.contains("tiledPosition")) - } - assertTrue(GlesMgcSpatialRgbChromaShaders.iirRgb.contains("innerSize = uAxis == 0 ? uImageSize.x")) - assertTrue(GlesMgcSpatialRgbChromaShaders.iirRgb.contains("for (int i = 0; i < innerSize; ++i)")) - assertTrue(GlesMgcSpatialRgbChromaShaders.iirRgb.contains("ivec2 storage = p;")) - assertTrue(GlesMgcSpatialRgbChromaShaders.iirRgb.contains("imageStore(uOutput, storage")) - assertTrue(GlesMgcSpatialRgbChromaShaders.iirError.contains("for (int i = 0; i < innerSize; ++i)")) - } - - @Test - fun finalHandoffUsesOneGlobal2dSurface() { - val finish = GlesMgcSpatialRgbChromaShaders.finalCameraRgb - - assertTrue(finish.contains("uimage2D uInput")) - assertTrue(finish.contains("uimage2D uOutput")) - assertTrue(finish.contains("imageStore(uOutput, p, outputPixel)")) - assertTrue(!finish.contains("uimage2DArray")) - } - - @Test - fun nativeScaleRetainsReferenceVgnCoefficientsAndSrScaleLowersDigitalCutoff() { - val native = MgcSpatialRgbChromaIirCoefficients.forOutputScale(1f) - val sr = MgcSpatialRgbChromaIirCoefficients.forOutputScale(1.5f) - - assertArrayEquals( - floatArrayOf(0.0674552768f, 0.134910554f, 0.0674552768f, 0f), - native.pass1.a10, - 0f, - ) - assertArrayEquals( - floatArrayOf(1f, -1.14298046f, 0.412801594f, 0f), - native.pass1.b10, - 0f, - ) - assertTrue(sr.pass1.a10[0] < native.pass1.a10[0]) - assertEquals(2f * sr.pass1.a10[0], sr.pass1.a10[1], 1e-6f) - } - - @Test - fun generatedPostprocessorShadersPassAvailableNdkValidator() { - 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 sources = listOf( - GlesMgcSpatialRgbChromaShaders.seed, - GlesMgcSpatialRgbChromaShaders.colorNoise1, - GlesMgcSpatialRgbChromaShaders.colorNoise2, - GlesMgcSpatialRgbChromaShaders.colorNoise3Smooth, - GlesMgcSpatialRgbChromaShaders.restoreOriginal, - GlesMgcSpatialRgbChromaShaders.iirRgb, - GlesMgcSpatialRgbChromaShaders.calculateColorNoiseError, - GlesMgcSpatialRgbChromaShaders.iirError, - GlesMgcSpatialRgbChromaShaders.colorNoiseFilter, - GlesMgcSpatialRgbChromaShaders.finalCameraRgb, - ) - sources.forEachIndexed { index, source -> - val sourceFile = File.createTempFile("mgc-spatial-rgb-chroma-$index-", ".compute") - val outputFile = File.createTempFile("mgc-spatial-rgb-chroma-$index-", ".spv") - try { - sourceFile.writeText(source) - 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("shader $index: $output", 0, process.waitFor()) - } finally { - sourceFile.delete() - outputFile.delete() - } - } - } -} diff --git a/app/src/test/java/com/hinnka/mycamera/processor/GpuStackQueueTimingTest.kt b/app/src/test/java/com/hinnka/mycamera/processor/GpuStackQueueTimingTest.kt index ff1e9921..ce5e12a5 100644 --- a/app/src/test/java/com/hinnka/mycamera/processor/GpuStackQueueTimingTest.kt +++ b/app/src/test/java/com/hinnka/mycamera/processor/GpuStackQueueTimingTest.kt @@ -12,7 +12,7 @@ class GpuStackQueueTimingTest { GpuStackStageWait(GpuStackCompletionStage.LONG_ALIGNMENT, 7L), GpuStackStageWait(GpuStackCompletionStage.HIGHLIGHT_ALIGNMENT, 11L), GpuStackStageWait(GpuStackCompletionStage.TILED_RECONSTRUCTION, 29L), - GpuStackStageWait(GpuStackCompletionStage.CHROMA_POSTPROCESS, 2_137L), + GpuStackStageWait(GpuStackCompletionStage.OUTPUT_HANDOFF, 2_137L), GpuStackStageWait(GpuStackCompletionStage.FINAL_EXPORT, 35L), ), ) @@ -20,7 +20,7 @@ class GpuStackQueueTimingTest { assertEquals(2_219L, timing.totalWaitMs) assertEquals( "normalAlignment=0ms longAlignment=7ms highlightAlignment=11ms " + - "tiledReconstruction=29ms chromaPostprocess=2137ms finalExport=35ms", + "tiledReconstruction=29ms outputHandoff=2137ms finalExport=35ms", timing.logFields(), ) } diff --git a/app/src/test/java/com/hinnka/mycamera/processor/MgcSpatialRgbMemoryPolicyTest.kt b/app/src/test/java/com/hinnka/mycamera/processor/MgcSpatialRgbMemoryPolicyTest.kt index d1cfbcef..b7d32d1e 100644 --- a/app/src/test/java/com/hinnka/mycamera/processor/MgcSpatialRgbMemoryPolicyTest.kt +++ b/app/src/test/java/com/hinnka/mycamera/processor/MgcSpatialRgbMemoryPolicyTest.kt @@ -1,7 +1,6 @@ package com.hinnka.mycamera.processor import org.junit.Assert.assertEquals -import org.junit.Assert.assertTrue import org.junit.Test class MgcSpatialRgbMemoryPolicyTest { @@ -32,12 +31,4 @@ class MgcSpatialRgbMemoryPolicyTest { ), ) } - - @Test - fun twoTimesSpatialOutputKeepsOnlyThreeFullSizeIirSurfaces() { - val workingSet = MgcSpatialRgbMemoryPolicy.iirWorkingSetBytes(8192, 6144) - - assertEquals(1_207_959_552L, workingSet) - assertTrue(workingSet > 640L * 1024L * 1024L) - } }