feat: large raw performance

This commit is contained in:
zhoubinjia committed 2026-07-24 15:39:18 +08:00
1 parent 63a9e7157d
commit 3620beb5ba
5 files changed
+1318 -700

No files matched your search

@@ -73,6 +73,8 @@ object QuadBayerShaders {
layout(std430, binding = 3) buffer RGB2_Buf { float rgb2[]; };
uniform ivec2 uImageSize;
uniform ivec2 uFullImageSize;
uniform ivec2 uGlobalOrigin;
uniform int uCfaPattern;
uniform vec4 uBlackLevel;
uniform float uWhiteLevel;
@@ -102,11 +104,12 @@ object QuadBayerShaders {
if (!uLensShadingEnabled) {
return 1.0;
}
vec2 norm = (vec2(coord) + vec2(0.5)) / vec2(uImageSize);
ivec2 globalCoord = coord + uGlobalOrigin;
vec2 norm = (vec2(globalCoord) + vec2(0.5)) / vec2(uFullImageSize);
vec2 uv = norm;
if (uLensShadingUsesDngGrid) {
vec2 boundsSize = max(uLensShadingBoundsSize, vec2(1.0));
norm = (vec2(coord) + vec2(0.5) - uLensShadingBoundsOrigin) / boundsSize;
norm = (vec2(globalCoord) + vec2(0.5) - uLensShadingBoundsOrigin) / boundsSize;
vec2 origin = uLensShadingGrid.xy;
vec2 spacing = max(uLensShadingGrid.zw, vec2(1e-8));
vec2 mapIndex = (norm - origin) / spacing;
File diff suppressed because it is too large. Load diff
@@ -0,0 +1,238 @@
package com.hinnka.mycamera.raw
internal data class RawTileRect(
val left: Int,
val top: Int,
val right: Int,
val bottom: Int,
) {
init {
require(left >= 0 && top >= 0)
require(right > left && bottom > top)
}
val width: Int get() = right - left
val height: Int get() = bottom - top
fun contains(other: RawTileRect): Boolean {
return left <= other.left && top <= other.top &&
right >= other.right && bottom >= other.bottom
}
}
internal data class RawRenderTile(
val index: Int,
/** Non-overlapping rectangle in the final, rotated output bitmap. */
val outputCore: RawTileRect,
/** [outputCore] mapped back into the unrotated sensor image. */
val sourceCore: RawTileRect,
/** CFA-aligned sensor rectangle uploaded and processed for this tile. */
val sourceWorking: RawTileRect,
)
/**
* Plans Phocus-style bounded-memory RAW rendering.
*
* Tiles own disjoint output cores. The overlap in [RawRenderTile.sourceWorking] is dependency
* data only and is never copied to the destination. Working rectangles use a stable size wherever
* the sensor permits it, so all GPU textures can be retained and reused by the serial tile
* consumer. Their origins are aligned to the sensor CFA period.
*/
internal object RawTilePlanner {
/** Phocus switches OutputImageThreaded above nine million effective output pixels. */
const val THREADED_OUTPUT_THRESHOLD_PIXELS = 9_000_000L
fun shouldTile(outputWidth: Int, outputHeight: Int): Boolean {
return outputWidth.toLong() * outputHeight.toLong() >
THREADED_OUTPUT_THRESHOLD_PIXELS
}
fun plan(
sourceWidth: Int,
sourceHeight: Int,
outputSourceBounds: RawTileRect,
rotation: Int,
coreEdgePx: Int,
supportPx: Int,
cfaPeriod: Int,
): List<RawRenderTile> {
require(sourceWidth > 0 && sourceHeight > 0)
require(
outputSourceBounds.left >= 0 && outputSourceBounds.top >= 0 &&
outputSourceBounds.right <= sourceWidth &&
outputSourceBounds.bottom <= sourceHeight
)
require(rotation in setOf(0, 90, 180, 270))
val phase = cfaPeriod.coerceAtLeast(2)
val coreEdge = alignDown(coreEdgePx.coerceAtLeast(phase), phase).coerceAtLeast(phase)
val support = supportPx.coerceAtLeast(0)
val outputWidth = if (rotation == 90 || rotation == 270) {
outputSourceBounds.height
} else {
outputSourceBounds.width
}
val outputHeight = if (rotation == 90 || rotation == 270) {
outputSourceBounds.width
} else {
outputSourceBounds.height
}
// A cropped core can start at any sensor phase. Reserving phase - 1 additional pixels
// keeps both sides at or above the requested support after the working origin is aligned.
val workWidth = stableWorkingLength(
sourceSize = sourceWidth,
requestedLength = coreEdge + support * 2 + phase - 1,
phase = phase,
)
val workHeight = stableWorkingLength(
sourceSize = sourceHeight,
requestedLength = coreEdge + support * 2 + phase - 1,
phase = phase,
)
val result = ArrayList<RawRenderTile>()
var outputTop = 0
var index = 0
while (outputTop < outputHeight) {
val outputBottom = minOf(outputHeight, outputTop + coreEdge)
var outputLeft = 0
while (outputLeft < outputWidth) {
val outputRight = minOf(outputWidth, outputLeft + coreEdge)
val outputCore = RawTileRect(outputLeft, outputTop, outputRight, outputBottom)
val sourceCore = outputToSource(
outputCore = outputCore,
sourceBounds = outputSourceBounds,
rotation = rotation,
)
result += RawRenderTile(
index = index++,
outputCore = outputCore,
sourceCore = sourceCore,
sourceWorking = fixedWorkingRect(
core = sourceCore,
sourceWidth = sourceWidth,
sourceHeight = sourceHeight,
workWidth = workWidth,
workHeight = workHeight,
support = support,
phase = phase,
),
)
outputLeft = outputRight
}
outputTop = outputBottom
}
return result
}
private fun outputToSource(
outputCore: RawTileRect,
sourceBounds: RawTileRect,
rotation: Int,
): RawTileRect {
return when (rotation) {
0 -> RawTileRect(
sourceBounds.left + outputCore.left,
sourceBounds.top + outputCore.top,
sourceBounds.left + outputCore.right,
sourceBounds.top + outputCore.bottom,
)
90 -> RawTileRect(
sourceBounds.left + outputCore.top,
sourceBounds.bottom - outputCore.right,
sourceBounds.left + outputCore.bottom,
sourceBounds.bottom - outputCore.left,
)
180 -> RawTileRect(
sourceBounds.right - outputCore.right,
sourceBounds.bottom - outputCore.bottom,
sourceBounds.right - outputCore.left,
sourceBounds.bottom - outputCore.top,
)
270 -> RawTileRect(
sourceBounds.right - outputCore.bottom,
sourceBounds.top + outputCore.left,
sourceBounds.right - outputCore.top,
sourceBounds.top + outputCore.right,
)
else -> error("Unsupported rotation: $rotation")
}
}
private fun fixedWorkingRect(
core: RawTileRect,
sourceWidth: Int,
sourceHeight: Int,
workWidth: Int,
workHeight: Int,
support: Int,
phase: Int,
): RawTileRect {
val left = fixedWorkingOrigin(
coreStart = core.left,
coreEnd = core.right,
sourceSize = sourceWidth,
workLength = workWidth,
support = support,
phase = phase,
)
val top = fixedWorkingOrigin(
coreStart = core.top,
coreEnd = core.bottom,
sourceSize = sourceHeight,
workLength = workHeight,
support = support,
phase = phase,
)
return RawTileRect(left, top, left + workWidth, top + workHeight).also { working ->
check(working.contains(core)) {
"Working rectangle $working does not contain tile core $core"
}
}
}
private fun fixedWorkingOrigin(
coreStart: Int,
coreEnd: Int,
sourceSize: Int,
workLength: Int,
support: Int,
phase: Int,
): Int {
if (workLength == sourceSize) return 0
val maximumOrigin = sourceSize - workLength
check(maximumOrigin % phase == 0)
val minimumForContainment = (coreEnd - workLength).coerceAtLeast(0)
val maximumForContainment = coreStart.coerceAtMost(maximumOrigin)
val preferred = (coreStart - support).coerceIn(
minimumForContainment,
maximumForContainment,
)
val alignedDown = alignDown(preferred, phase)
return if (alignedDown >= minimumForContainment) {
alignedDown
} else {
alignUp(minimumForContainment, phase).coerceAtMost(maximumForContainment)
}
}
/**
* Matching the source-size remainder makes `sourceSize - result` CFA-aligned. Therefore a
* fixed-size right/bottom edge tile can still have a CFA-aligned origin.
*/
private fun stableWorkingLength(sourceSize: Int, requestedLength: Int, phase: Int): Int {
if (requestedLength >= sourceSize) return sourceSize
val remainder = floorMod(sourceSize - requestedLength, phase)
return requestedLength + remainder
}
private fun alignDown(value: Int, alignment: Int): Int = value / alignment * alignment
private fun alignUp(value: Int, alignment: Int): Int =
(value + alignment - 1) / alignment * alignment
private fun floorMod(value: Int, divisor: Int): Int = ((value % divisor) + divisor) % divisor
}
@@ -160,6 +160,8 @@ object RcdShaders {
layout(std430, binding = 3) buffer RGB2_Buf { float rgb2[]; }; // B
uniform ivec2 uImageSize;
uniform ivec2 uFullImageSize;
uniform ivec2 uGlobalOrigin;
uniform int uCfaPattern;
uniform vec4 uBlackLevel; // R, Gr, Gb, B 或 [0,1,2,3] 四通道黑电平
uniform float uWhiteLevel;
@@ -231,11 +233,12 @@ object RcdShaders {
if (!uLensShadingEnabled) {
return 1.0;
}
vec2 norm = (vec2(coord) + vec2(0.5)) / vec2(uImageSize);
ivec2 globalCoord = coord + uGlobalOrigin;
vec2 norm = (vec2(globalCoord) + vec2(0.5)) / vec2(uFullImageSize);
vec2 uv = norm;
if (uLensShadingUsesDngGrid) {
vec2 boundsSize = max(uLensShadingBoundsSize, vec2(1.0));
norm = (vec2(coord) + vec2(0.5) - uLensShadingBoundsOrigin) / boundsSize;
norm = (vec2(globalCoord) + vec2(0.5) - uLensShadingBoundsOrigin) / boundsSize;
vec2 origin = uLensShadingGrid.xy;
vec2 spacing = max(uLensShadingGrid.zw, vec2(1e-8));
vec2 mapIndex = (norm - origin) / spacing;
@@ -3540,6 +3543,8 @@ void main() {
layout(rgba16f, binding = 0) writeonly uniform image2D outTex;
uniform ivec2 uImageSize;
uniform ivec2 uFullImageSize;
uniform ivec2 uGlobalOrigin;
uniform ivec2 uPackedSize;
uniform ivec2 uSourceOffset;
uniform int uCfaPattern;
@@ -3607,11 +3612,12 @@ void main() {
float lensShadingGainAt(int channel, ivec2 p) {
if (!uLensShadingEnabled) return 1.0;
vec2 norm = (vec2(p) + vec2(0.5)) / vec2(uImageSize);
ivec2 globalPosition = p + uGlobalOrigin;
vec2 norm = (vec2(globalPosition) + vec2(0.5)) / vec2(uFullImageSize);
vec2 uv = norm;
if (uLensShadingUsesDngGrid) {
vec2 boundsSize = max(uLensShadingBoundsSize, vec2(1.0));
norm = (vec2(p) + vec2(0.5) - uLensShadingBoundsOrigin) / boundsSize;
norm = (vec2(globalPosition) + vec2(0.5) - uLensShadingBoundsOrigin) / boundsSize;
vec2 mapIndex = (norm - uLensShadingGrid.xy) /
max(uLensShadingGrid.zw, vec2(1e-8));
uv = (mapIndex + vec2(0.5)) / max(uLensShadingMapSize, vec2(1.0));
@@ -0,0 +1,121 @@
package com.hinnka.mycamera.raw
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
import org.junit.Test
class RawTilePlannerTest {
@Test
fun hundredMegapixelOutputUsesDisjointBoundedCores() {
val bounds = RawTileRect(0, 0, 11_648, 8_736)
val tiles = RawTilePlanner.plan(
sourceWidth = bounds.width,
sourceHeight = bounds.height,
outputSourceBounds = bounds,
rotation = 0,
coreEdgePx = 2_048,
supportPx = 112,
cfaPeriod = 2,
)
assertEquals(30, tiles.size)
assertEquals(
bounds.width.toLong() * bounds.height,
tiles.sumOf { it.outputCore.width.toLong() * it.outputCore.height },
)
assertTrue(tiles.all { it.sourceWorking.width <= 2_274 })
assertTrue(tiles.all { it.sourceWorking.height <= 2_274 })
assertTrue(tiles.all { it.sourceWorking.contains(it.sourceCore) })
}
@Test
fun allRotationsMapOutputBackToTheSameSourceCrop() {
val sourceWidth = 43
val sourceHeight = 34
val sourceBounds = RawTileRect(1, 2, 41, 32)
for (rotation in listOf(0, 90, 180, 270)) {
val tiles = RawTilePlanner.plan(
sourceWidth = sourceWidth,
sourceHeight = sourceHeight,
outputSourceBounds = sourceBounds,
rotation = rotation,
coreEdgePx = 16,
supportPx = 4,
cfaPeriod = 2,
)
assertEquals(
sourceBounds.width.toLong() * sourceBounds.height,
tiles.sumOf { it.sourceCore.width.toLong() * it.sourceCore.height },
)
assertEquals(sourceBounds.left, tiles.minOf { it.sourceCore.left })
assertEquals(sourceBounds.top, tiles.minOf { it.sourceCore.top })
assertEquals(sourceBounds.right, tiles.maxOf { it.sourceCore.right })
assertEquals(sourceBounds.bottom, tiles.maxOf { it.sourceCore.bottom })
val coverage = IntArray(sourceWidth * sourceHeight)
for (tile in tiles) {
for (y in tile.sourceCore.top until tile.sourceCore.bottom) {
for (x in tile.sourceCore.left until tile.sourceCore.right) {
coverage[y * sourceWidth + x]++
}
}
}
for (y in 0 until sourceHeight) {
for (x in 0 until sourceWidth) {
val expected = if (
x in sourceBounds.left until sourceBounds.right &&
y in sourceBounds.top until sourceBounds.bottom
) 1 else 0
assertEquals(
"rotation=$rotation pixel=[$x,$y]",
expected,
coverage[y * sourceWidth + x],
)
}
}
}
}
@Test
fun workingOriginsPreserveEightByEightCfaPhaseAndReuseDimensions() {
val tiles = RawTilePlanner.plan(
sourceWidth = 11_649,
sourceHeight = 8_737,
outputSourceBounds = RawTileRect(17, 31, 11_601, 8_701),
rotation = 270,
coreEdgePx = 2_048,
supportPx = 112,
cfaPeriod = 8,
)
assertTrue(tiles.all { it.sourceWorking.left % 8 == 0 })
assertTrue(tiles.all { it.sourceWorking.top % 8 == 0 })
assertEquals(1, tiles.map { it.sourceWorking.width }.distinct().size)
assertEquals(1, tiles.map { it.sourceWorking.height }.distinct().size)
assertTrue(tiles.all { it.sourceWorking.contains(it.sourceCore) })
for (tile in tiles) {
assertTrue(
tile.sourceCore.left - tile.sourceWorking.left >=
minOf(112, tile.sourceCore.left),
)
assertTrue(
tile.sourceWorking.right - tile.sourceCore.right >=
minOf(112, 11_649 - tile.sourceCore.right),
)
assertTrue(
tile.sourceCore.top - tile.sourceWorking.top >=
minOf(112, tile.sourceCore.top),
)
assertTrue(
tile.sourceWorking.bottom - tile.sourceCore.bottom >=
minOf(112, 8_737 - tile.sourceCore.bottom),
)
}
}
@Test
fun phocusThresholdUsesEffectiveCropPixels() {
assertTrue(!RawTilePlanner.shouldTile(3_000, 3_000))
assertTrue(RawTilePlanner.shouldTile(3_001, 3_000))
}
}