feat: spectral film preview
This commit is contained in:
1 parent
1697f67d9d
commit
4e65802100
11 files changed
+261
-26
No files matched your search
@@ -1,6 +1,7 @@
|
||||
package com.hinnka.mycamera.lut
|
||||
|
||||
import android.opengl.GLES30
|
||||
import com.hinnka.mycamera.raw.SpectralFilmLut
|
||||
import com.hinnka.mycamera.utils.PLog
|
||||
|
||||
internal data class ColorPassLocations(
|
||||
@@ -56,6 +57,8 @@ internal data class ColorPassLocations(
|
||||
val uTexelSizeLocation: Int,
|
||||
val uCurveTextureLocation: Int,
|
||||
val uCurveEnabledLocation: Int,
|
||||
/** Null when the variant does not include the Spektrafilm preview stage. */
|
||||
val spectralFilmLocations: SpectralFilmPreviewLocations?,
|
||||
val aPositionLocation: Int,
|
||||
val aTexCoordLocation: Int,
|
||||
)
|
||||
@@ -63,11 +66,16 @@ internal data class ColorPassLocations(
|
||||
internal class PreviewColorProgramCache {
|
||||
private val programs = mutableMapOf<PreviewColorShaderVariant, ColorPassLocations>()
|
||||
private val logInput = LogInputGl()
|
||||
private val spectralFilm = SpectralFilmPreviewGl()
|
||||
|
||||
fun bindLogInput(locations: ColorPassLocations) {
|
||||
logInput.bind(locations.uInverseAcr3TextureLocation, textureUnit = 4)
|
||||
}
|
||||
|
||||
fun bindSpectralFilm(locations: ColorPassLocations, lut: SpectralFilmLut?) {
|
||||
spectralFilm.bind(locations.spectralFilmLocations, lut)
|
||||
}
|
||||
|
||||
fun get(variant: PreviewColorShaderVariant): ColorPassLocations? {
|
||||
programs[variant]?.let { return it }
|
||||
|
||||
@@ -91,7 +99,7 @@ internal class PreviewColorProgramCache {
|
||||
return null
|
||||
}
|
||||
|
||||
val locations = queryLocations(programId)
|
||||
val locations = queryLocations(programId, variant)
|
||||
programs[variant] = locations
|
||||
PLog.d(TAG, "Compiled preview color shader variant: $variant")
|
||||
return locations
|
||||
@@ -99,16 +107,18 @@ internal class PreviewColorProgramCache {
|
||||
|
||||
fun release() {
|
||||
logInput.release()
|
||||
spectralFilm.release()
|
||||
programs.values.forEach { GlUtils.deleteProgram(it.programId) }
|
||||
programs.clear()
|
||||
}
|
||||
|
||||
fun reset() {
|
||||
logInput.reset()
|
||||
spectralFilm.reset()
|
||||
programs.clear()
|
||||
}
|
||||
|
||||
private fun queryLocations(program: Int): ColorPassLocations {
|
||||
private fun queryLocations(program: Int, variant: PreviewColorShaderVariant): ColorPassLocations {
|
||||
return ColorPassLocations(
|
||||
programId = program,
|
||||
uMVPMatrixLocation = GLES30.glGetUniformLocation(program, "uMVPMatrix"),
|
||||
@@ -165,6 +175,11 @@ internal class PreviewColorProgramCache {
|
||||
uTexelSizeLocation = GLES30.glGetUniformLocation(program, "uTexelSize"),
|
||||
uCurveTextureLocation = GLES30.glGetUniformLocation(program, "uCurveTexture"),
|
||||
uCurveEnabledLocation = GLES30.glGetUniformLocation(program, "uCurveEnabled"),
|
||||
spectralFilmLocations = if (variant.includeSpectralFilm) {
|
||||
SpectralFilmPreviewLocations.query(program)
|
||||
} else {
|
||||
null
|
||||
},
|
||||
aPositionLocation = GLES30.glGetAttribLocation(program, "aPosition"),
|
||||
aTexCoordLocation = GLES30.glGetAttribLocation(program, "aTexCoord"),
|
||||
)
|
||||
|
||||
@@ -25,6 +25,7 @@ import java.nio.ByteOrder
|
||||
import java.util.ArrayDeque
|
||||
import java.util.concurrent.ExecutorService
|
||||
import java.util.concurrent.Executors
|
||||
import com.hinnka.mycamera.raw.SpectralFilmLut
|
||||
import java.util.concurrent.atomic.AtomicBoolean
|
||||
import javax.microedition.khronos.egl.EGLConfig
|
||||
import javax.microedition.khronos.opengles.GL10
|
||||
@@ -347,6 +348,10 @@ class LutRenderer(context: Context) : GLSurfaceView.Renderer {
|
||||
@Volatile
|
||||
var baselineLutEnabled: Boolean = false
|
||||
|
||||
/** RAW 渲染引擎为 Spektrafilm 时的取景器胶片模拟;null 表示不模拟。 */
|
||||
@Volatile
|
||||
private var spectralFilmPreviewLut: SpectralFilmLut? = null
|
||||
|
||||
// 色彩配方参数
|
||||
@Volatile
|
||||
var colorRecipeEnabled: Boolean = false
|
||||
@@ -771,6 +776,7 @@ class LutRenderer(context: Context) : GLSurfaceView.Renderer {
|
||||
lutEnabled: Boolean,
|
||||
params: com.hinnka.mycamera.model.ColorRecipeParams,
|
||||
enableVideoLog: Boolean,
|
||||
spectralFilmEnabled: Boolean,
|
||||
): ColorPassLocations? {
|
||||
val variant = PreviewColorShaderVariant.forPass(
|
||||
textureSource = textureSource,
|
||||
@@ -778,6 +784,7 @@ class LutRenderer(context: Context) : GLSurfaceView.Renderer {
|
||||
lutConfig = lutConfig,
|
||||
lutEnabled = lutEnabled && lutConfig != null,
|
||||
videoLogEnabled = enableVideoLog && videoLogProfile.isEnabled,
|
||||
spectralFilmEnabled = spectralFilmEnabled,
|
||||
)
|
||||
return colorProgramCache.get(variant)
|
||||
}
|
||||
@@ -800,12 +807,14 @@ class LutRenderer(context: Context) : GLSurfaceView.Renderer {
|
||||
curveTextureId: Int,
|
||||
curveEnabled: Boolean,
|
||||
enableVideoLog: Boolean,
|
||||
spectralFilmLut: SpectralFilmLut?,
|
||||
) {
|
||||
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, targetFboId)
|
||||
GLES30.glViewport(0, 0, width, height)
|
||||
GLES30.glClear(GLES30.GL_COLOR_BUFFER_BIT)
|
||||
GLES30.glUseProgram(locations.programId)
|
||||
colorProgramCache.bindLogInput(locations)
|
||||
colorProgramCache.bindSpectralFilm(locations, spectralFilmLut)
|
||||
|
||||
GLES30.glActiveTexture(GLES30.GL_TEXTURE0)
|
||||
GLES30.glBindTexture(sourceTextureTarget, sourceTextureId)
|
||||
@@ -1087,6 +1096,8 @@ class LutRenderer(context: Context) : GLSurfaceView.Renderer {
|
||||
lutEnabled = lutEnabled && currentLutConfig != null,
|
||||
params = creativeParams,
|
||||
enableVideoLog = false,
|
||||
// 第二层输入已是首个 pass 的渲染结果,引擎模拟只能在采样相机帧时执行一次。
|
||||
spectralFilmEnabled = false,
|
||||
) ?: run {
|
||||
return
|
||||
}
|
||||
@@ -1108,6 +1119,7 @@ class LutRenderer(context: Context) : GLSurfaceView.Renderer {
|
||||
curveTextureId = curveTextureId,
|
||||
curveEnabled = curveEnabled && curveTextureId != 0,
|
||||
enableVideoLog = false,
|
||||
spectralFilmLut = null,
|
||||
)
|
||||
currentTexId = stackTextureId
|
||||
}
|
||||
@@ -1347,8 +1359,10 @@ class LutRenderer(context: Context) : GLSurfaceView.Renderer {
|
||||
targetMvpMatrix: FloatArray = mvpMatrix,
|
||||
preferBaselineLayer: Boolean = false,
|
||||
suppressBaselineLayer: Boolean = false,
|
||||
suppressCreativeLayer: Boolean = false
|
||||
suppressCreativeLayer: Boolean = false,
|
||||
suppressSpectralFilm: Boolean = false,
|
||||
) {
|
||||
val spectralFilmLut = if (suppressSpectralFilm) null else spectralFilmPreviewLut
|
||||
val baselineLayerAvailable = hasBaselineLayer() && !suppressBaselineLayer
|
||||
val creativeLayerAvailable = hasCreativeLayer() && !suppressCreativeLayer
|
||||
val useCreativeLayer = creativeLayerAvailable && (!preferBaselineLayer || !baselineLayerAvailable)
|
||||
@@ -1391,6 +1405,7 @@ class LutRenderer(context: Context) : GLSurfaceView.Renderer {
|
||||
lutEnabled = layerLutEnabled,
|
||||
params = layerParams,
|
||||
enableVideoLog = enableVideoLog,
|
||||
spectralFilmEnabled = spectralFilmLut != null,
|
||||
) ?: return
|
||||
drawColorPass(
|
||||
locations = locations,
|
||||
@@ -1410,6 +1425,7 @@ class LutRenderer(context: Context) : GLSurfaceView.Renderer {
|
||||
curveTextureId = layerCurveTextureId,
|
||||
curveEnabled = layerCurveEnabled,
|
||||
enableVideoLog = enableVideoLog,
|
||||
spectralFilmLut = spectralFilmLut,
|
||||
)
|
||||
|
||||
// 测光和直方图(按需)
|
||||
@@ -2227,6 +2243,11 @@ class LutRenderer(context: Context) : GLSurfaceView.Renderer {
|
||||
setLutInternal(lutConfig)
|
||||
}
|
||||
|
||||
fun setSpectralFilmPreview(lut: SpectralFilmLut?) {
|
||||
// 纹理由 GL 线程在下一次绘制时按 sourceKey 惰性上传,此处只替换不可变选择。
|
||||
spectralFilmPreviewLut = lut
|
||||
}
|
||||
|
||||
fun setBaselineLut(lutConfig: LutConfig?) {
|
||||
if (!surfaceReady) {
|
||||
currentBaselineLutConfig = lutConfig
|
||||
@@ -2686,7 +2707,8 @@ class LutRenderer(context: Context) : GLSurfaceView.Renderer {
|
||||
height = targetHeight,
|
||||
targetMvpMatrix = buildMvpMatrix(targetWidth, targetHeight),
|
||||
suppressBaselineLayer = suppressColorLayers,
|
||||
suppressCreativeLayer = suppressColorLayers
|
||||
suppressCreativeLayer = suppressColorLayers,
|
||||
suppressSpectralFilm = suppressColorLayers,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -2801,6 +2823,7 @@ class LutRenderer(context: Context) : GLSurfaceView.Renderer {
|
||||
targetMvpMatrix = buildMvpMatrix(METERING_SIZE, METERING_SIZE),
|
||||
suppressBaselineLayer = true,
|
||||
suppressCreativeLayer = true,
|
||||
suppressSpectralFilm = true,
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -14,7 +14,9 @@ internal object PreviewColorShader {
|
||||
}
|
||||
val needsOklab = variant.includeOklchDensity || variant.includeLchMixer
|
||||
val needsClassifiers = variant.includeLchMixer || variant.includeLutMask
|
||||
val needsLogInput = variant.includeVideoLog || variant.includeExtendedLutCurves
|
||||
val needsLogInput = variant.includeVideoLog ||
|
||||
variant.includeExtendedLutCurves ||
|
||||
variant.includeSpectralFilm
|
||||
return """
|
||||
#version 300 es
|
||||
${extension}
|
||||
@@ -86,6 +88,7 @@ internal object PreviewColorShader {
|
||||
|
||||
${PreviewColorShaderModules.COLOR_TRANSFER_CORE}
|
||||
${if (needsLogInput) LogInputGl.GLSL else ""}
|
||||
${if (variant.includeSpectralFilm) SpectralFilmPreviewGl.GLSL else ""}
|
||||
${PreviewColorShaderModules.EXPOSURE}
|
||||
${ContrastShader.GLSL}
|
||||
${DirectFlashShader.GLSL}
|
||||
@@ -175,6 +178,11 @@ internal object PreviewColorShader {
|
||||
color.rgb = sanitizeColor(color.rgb);
|
||||
""" else ""}
|
||||
|
||||
${if (variant.includeSpectralFilm) """
|
||||
// RAW 渲染引擎模拟:先于基准层与创意层,与 RAW 管线中引擎所处位置一致。
|
||||
color.rgb = sanitizeColor(applySpectralFilmPreview(color.rgb));
|
||||
""" else ""}
|
||||
|
||||
if (uColorRecipeEnabled) {
|
||||
if (abs(uExposure) > 0.001) {
|
||||
color.rgb = applyExposureInLinearSpace(color.rgb, uExposure);
|
||||
|
||||
@@ -20,6 +20,7 @@ internal data class PreviewColorShaderVariant(
|
||||
val includeSpatialRecipeEffects: Boolean = false,
|
||||
val includeVideoLog: Boolean = false,
|
||||
val includeSharpening: Boolean = false,
|
||||
val includeSpectralFilm: Boolean = false,
|
||||
) {
|
||||
companion object {
|
||||
fun forPass(
|
||||
@@ -28,6 +29,7 @@ internal data class PreviewColorShaderVariant(
|
||||
lutConfig: LutConfig?,
|
||||
lutEnabled: Boolean,
|
||||
videoLogEnabled: Boolean,
|
||||
spectralFilmEnabled: Boolean,
|
||||
): PreviewColorShaderVariant {
|
||||
val lutCurve = lutConfig?.curve ?: TransferCurve.SRGB
|
||||
return PreviewColorShaderVariant(
|
||||
@@ -43,6 +45,7 @@ internal data class PreviewColorShaderVariant(
|
||||
includeVideoLog = videoLogEnabled,
|
||||
// Match the shader's activation threshold, including negative (softening) values.
|
||||
includeSharpening = abs(params.sharpness) > 0.0001f,
|
||||
includeSpectralFilm = spectralFilmEnabled,
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -385,6 +385,7 @@ class RealtimeVideoRenderer(
|
||||
lutConfig = layer.lutConfig,
|
||||
lutEnabled = lutEnabled,
|
||||
videoLogEnabled = enableVideoLog,
|
||||
spectralFilmEnabled = false,
|
||||
)
|
||||
val locations = colorProgramCache.get(variant)
|
||||
?: throw IllegalStateException("Cannot create realtime video color shader: $variant")
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
package com.hinnka.mycamera.lut
|
||||
|
||||
import android.opengl.GLES30
|
||||
import com.hinnka.mycamera.raw.ColorSpace
|
||||
import com.hinnka.mycamera.raw.RawRenderingEngine
|
||||
import com.hinnka.mycamera.raw.RawToneMappingGl
|
||||
import com.hinnka.mycamera.raw.SpectralFilmLut
|
||||
import com.hinnka.mycamera.raw.SpectralFilmStageTextures
|
||||
import com.hinnka.mycamera.raw.SpectralFilmUniformLocations
|
||||
import com.hinnka.mycamera.raw.SpektrafilmToneShader
|
||||
import kotlin.math.pow
|
||||
|
||||
internal data class SpectralFilmPreviewLocations(
|
||||
val stages: SpectralFilmUniformLocations,
|
||||
val inputTransform: Int,
|
||||
val outputTransform: Int,
|
||||
val exposureGain: Int,
|
||||
) {
|
||||
companion object {
|
||||
fun query(program: Int) = SpectralFilmPreviewLocations(
|
||||
stages = SpectralFilmUniformLocations.query(program),
|
||||
inputTransform = GLES30.glGetUniformLocation(program, "uSpectralPreviewInputTransform"),
|
||||
outputTransform = GLES30.glGetUniformLocation(program, "uSpectralPreviewOutputTransform"),
|
||||
exposureGain = GLES30.glGetUniformLocation(program, "uSpectralPreviewExposureGain"),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Realtime Spektrafilm rendering of the ISP's display-referred sRGB preview.
|
||||
*
|
||||
* The ISP output is brought back to an approximate scene-linear engine input
|
||||
* (sRGB decode, inverse ACR3 tone curve, Spektrafilm's default linear exposure
|
||||
* compensation, sRGB -> ProPhoto) and then rendered with the same film/print
|
||||
* stages as the RAW engine before returning to display sRGB.
|
||||
*/
|
||||
internal class SpectralFilmPreviewGl {
|
||||
// Units 0..4 are owned by the preview color pass (camera, LUT, curve, basic tone, inverse ACR3).
|
||||
private val stages = SpectralFilmStageTextures(filmUnit = 5, printUnit = 6)
|
||||
|
||||
fun bind(locations: SpectralFilmPreviewLocations?, lut: SpectralFilmLut?) {
|
||||
if (locations == null) return
|
||||
stages.bind(locations.stages, lut)
|
||||
GLES30.glUniformMatrix3fv(locations.inputTransform, 1, false, SRGB_TO_PROPHOTO, 0)
|
||||
GLES30.glUniformMatrix3fv(locations.outputTransform, 1, false, PROPHOTO_TO_SRGB, 0)
|
||||
GLES30.glUniform1f(locations.exposureGain, EXPOSURE_GAIN)
|
||||
}
|
||||
|
||||
fun release() = stages.release()
|
||||
|
||||
fun reset() = stages.reset()
|
||||
|
||||
companion object {
|
||||
private val EXPOSURE_GAIN = 2f.pow(RawRenderingEngine.Spektrafilm.defaultExposureCompensationEv)
|
||||
|
||||
// Row-major transforms, transposed for column-major GL upload.
|
||||
private val SRGB_TO_PROPHOTO = RawToneMappingGl.transposeMatrix3x3(
|
||||
RawToneMappingGl.computeWorkingToOutputTransform(ColorSpace.SRGB, ColorSpace.ProPhoto)
|
||||
)
|
||||
private val PROPHOTO_TO_SRGB = RawToneMappingGl.transposeMatrix3x3(
|
||||
RawToneMappingGl.computeWorkingToOutputTransform(ColorSpace.ProPhoto, ColorSpace.SRGB)
|
||||
)
|
||||
|
||||
/** Requires [PreviewColorShaderModules.COLOR_TRANSFER_CORE] and [LogInputGl.GLSL]. */
|
||||
val GLSL = """
|
||||
${SpektrafilmToneShader.SPECTRAL_FILM_UNIFORMS}
|
||||
uniform mat3 uSpectralPreviewInputTransform;
|
||||
uniform mat3 uSpectralPreviewOutputTransform;
|
||||
uniform float uSpectralPreviewExposureGain;
|
||||
|
||||
${SpektrafilmToneShader.SPECTRAL_FILM_FUNCTIONS}
|
||||
|
||||
vec3 applySpectralFilmPreview(vec3 srgbColor) {
|
||||
vec3 displayLinear = srgbToLinear(clamp(srgbColor, 0.0, 1.0));
|
||||
vec3 sceneLinear = vec3(
|
||||
inverseAcr3(displayLinear.r),
|
||||
inverseAcr3(displayLinear.g),
|
||||
inverseAcr3(displayLinear.b)
|
||||
) * uSpectralPreviewExposureGain;
|
||||
vec3 proPhotoLinear = uSpectralPreviewInputTransform * sceneLinear;
|
||||
vec3 filmLinear = uSpectralPreviewOutputTransform * applySpectralFilm(proPhotoLinear);
|
||||
return linearToSrgb(clamp(filmLinear, 0.0, 1.0));
|
||||
}
|
||||
""".trimIndent()
|
||||
}
|
||||
}
|
||||
@@ -6,17 +6,19 @@ import java.nio.ByteBuffer
|
||||
import java.nio.ByteOrder
|
||||
|
||||
internal object SpektrafilmToneShader {
|
||||
val SPECTRAL_FILM_COMBINED_UNIFORMS = """
|
||||
/** Film/print stage resources shared by the RAW engine and the realtime viewfinder. */
|
||||
val SPECTRAL_FILM_UNIFORMS = """
|
||||
uniform highp sampler3D uSpectralFilmTexture;
|
||||
uniform highp sampler3D uSpectralPrintTexture;
|
||||
uniform mat3 uOutputTransform;
|
||||
uniform int uSpectralFilmSize;
|
||||
uniform int uSpectralPrintSize;
|
||||
uniform float uSpectralInputScale;
|
||||
uniform float uSpectralPrintInputScale;
|
||||
uniform vec3 uSpectralNegativeDensityGains;
|
||||
""".trimIndent()
|
||||
val SPECTRAL_FILM_COMBINED_FUNCTIONS = """
|
||||
|
||||
/** Maps linear ProPhoto RGB through the film (and print) stages back to linear ProPhoto RGB. */
|
||||
val SPECTRAL_FILM_FUNCTIONS = """
|
||||
vec3 linearToProPhoto(vec3 color) {
|
||||
vec3 clamped = max(color, vec3(0.0));
|
||||
vec3 isHigh = step(vec3(0.001953125), clamped);
|
||||
@@ -62,6 +64,14 @@ internal object SpektrafilmToneShader {
|
||||
// encoded ProPhoto RGB and share the same linear output contract.
|
||||
return proPhotoToLinear(filmResult);
|
||||
}
|
||||
""".trimIndent()
|
||||
|
||||
val SPECTRAL_FILM_COMBINED_UNIFORMS = """
|
||||
$SPECTRAL_FILM_UNIFORMS
|
||||
uniform mat3 uOutputTransform;
|
||||
""".trimIndent()
|
||||
val SPECTRAL_FILM_COMBINED_FUNCTIONS = """
|
||||
$SPECTRAL_FILM_FUNCTIONS
|
||||
|
||||
vec3 applyEngineTone(vec3 color) {
|
||||
return uOutputTransform * applySpectralFilm(color);
|
||||
@@ -79,37 +89,80 @@ internal class SpektrafilmToneAlgorithm(quad: RawFullscreenQuad) :
|
||||
RawRenderingEngineToneAlgorithm(quad, SpektrafilmToneShader.DEFINITION) {
|
||||
// Shared tone resources occupy 0 (input), 7 (gain table), 2 (HDR coordinate),
|
||||
// and 4 (HDR base curve). Keep both 3D samplers explicit on every draw.
|
||||
private val filmTexture = LutTexture(6, "film")
|
||||
private val printTexture = LutTexture(5, "print")
|
||||
private val stages = SpectralFilmStageTextures(filmUnit = 6, printUnit = 5)
|
||||
|
||||
override fun bindEngineResources(program: Int, input: RawEngineTonePass.Input) {
|
||||
super.bindEngineResources(program, input)
|
||||
val lut = input.spectralFilmLut
|
||||
filmTexture.bind(program, "uSpectralFilmTexture", lut?.filmTable)
|
||||
printTexture.bind(program, "uSpectralPrintTexture", lut?.printTable)
|
||||
GLES30.glUniform1i(GLES30.glGetUniformLocation(program, "uSpectralFilmSize"), lut?.size ?: 1)
|
||||
GLES30.glUniform1i(GLES30.glGetUniformLocation(program, "uSpectralPrintSize"), lut?.printTable?.size ?: 1)
|
||||
GLES30.glUniform1f(GLES30.glGetUniformLocation(program, "uSpectralInputScale"), lut?.inputScale ?: 1f)
|
||||
GLES30.glUniform1f(GLES30.glGetUniformLocation(program, "uSpectralPrintInputScale"), lut?.printInputScale ?: 1f)
|
||||
val gains = lut?.negativeDensityGains
|
||||
GLES30.glUniform3f(
|
||||
GLES30.glGetUniformLocation(program, "uSpectralNegativeDensityGains"),
|
||||
gains?.get(0) ?: 1f, gains?.get(1) ?: 1f, gains?.get(2) ?: 1f,
|
||||
)
|
||||
stages.bind(SpectralFilmUniformLocations.query(program), input.spectralFilmLut)
|
||||
RawGlesProgram.logErrors("SpektrafilmToneAlgorithm.bindEngineResources")
|
||||
}
|
||||
|
||||
override fun releaseEngineResources() {
|
||||
stages.release()
|
||||
}
|
||||
}
|
||||
|
||||
internal data class SpectralFilmUniformLocations(
|
||||
val filmTexture: Int,
|
||||
val printTexture: Int,
|
||||
val filmSize: Int,
|
||||
val printSize: Int,
|
||||
val inputScale: Int,
|
||||
val printInputScale: Int,
|
||||
val negativeDensityGains: Int,
|
||||
) {
|
||||
companion object {
|
||||
fun query(program: Int) = SpectralFilmUniformLocations(
|
||||
filmTexture = GLES30.glGetUniformLocation(program, "uSpectralFilmTexture"),
|
||||
printTexture = GLES30.glGetUniformLocation(program, "uSpectralPrintTexture"),
|
||||
filmSize = GLES30.glGetUniformLocation(program, "uSpectralFilmSize"),
|
||||
printSize = GLES30.glGetUniformLocation(program, "uSpectralPrintSize"),
|
||||
inputScale = GLES30.glGetUniformLocation(program, "uSpectralInputScale"),
|
||||
printInputScale = GLES30.glGetUniformLocation(program, "uSpectralPrintInputScale"),
|
||||
negativeDensityGains = GLES30.glGetUniformLocation(program, "uSpectralNegativeDensityGains"),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Owns the film and print 3D textures of one GL context. Stages are uploaded only when
|
||||
* their source asset changes; density tuning only updates uniforms.
|
||||
*/
|
||||
internal class SpectralFilmStageTextures(filmUnit: Int, printUnit: Int) {
|
||||
private val filmTexture = LutTexture(filmUnit, "film")
|
||||
private val printTexture = LutTexture(printUnit, "print")
|
||||
|
||||
fun bind(locations: SpectralFilmUniformLocations, lut: SpectralFilmLut?) {
|
||||
filmTexture.bind(locations.filmTexture, lut?.filmTable)
|
||||
printTexture.bind(locations.printTexture, lut?.printTable)
|
||||
GLES30.glUniform1i(locations.filmSize, lut?.size ?: 1)
|
||||
GLES30.glUniform1i(locations.printSize, lut?.printTable?.size ?: 1)
|
||||
GLES30.glUniform1f(locations.inputScale, lut?.inputScale ?: 1f)
|
||||
GLES30.glUniform1f(locations.printInputScale, lut?.printInputScale ?: 1f)
|
||||
val gains = lut?.negativeDensityGains
|
||||
GLES30.glUniform3f(
|
||||
locations.negativeDensityGains,
|
||||
gains?.get(0) ?: 1f, gains?.get(1) ?: 1f, gains?.get(2) ?: 1f,
|
||||
)
|
||||
}
|
||||
|
||||
fun release() {
|
||||
filmTexture.release()
|
||||
printTexture.release()
|
||||
}
|
||||
|
||||
/** Forget handles after the owning EGL context has been lost. */
|
||||
fun reset() {
|
||||
filmTexture.reset()
|
||||
printTexture.reset()
|
||||
}
|
||||
|
||||
private class LutTexture(private val unit: Int, private val stage: String) {
|
||||
private var textureId = 0
|
||||
private var textureKey: String? = null
|
||||
|
||||
fun bind(program: Int, uniform: String, table: SpectralLutTable?) {
|
||||
GLES30.glUniform1i(GLES30.glGetUniformLocation(program, uniform), unit)
|
||||
fun bind(samplerLocation: Int, table: SpectralLutTable?) {
|
||||
GLES30.glUniform1i(samplerLocation, unit)
|
||||
GLES30.glActiveTexture(GLES30.GL_TEXTURE0 + unit)
|
||||
val key = table?.sourceKey ?: FALLBACK_KEY
|
||||
if (textureId == 0 || textureKey != key) upload(table, key)
|
||||
@@ -119,8 +172,12 @@ internal class SpektrafilmToneAlgorithm(quad: RawFullscreenQuad) :
|
||||
fun release() {
|
||||
if (textureId != 0) {
|
||||
GLES30.glDeleteTextures(1, intArrayOf(textureId), 0)
|
||||
textureId = 0
|
||||
}
|
||||
reset()
|
||||
}
|
||||
|
||||
fun reset() {
|
||||
textureId = 0
|
||||
textureKey = null
|
||||
}
|
||||
|
||||
@@ -172,7 +229,7 @@ internal class SpektrafilmToneAlgorithm(quad: RawFullscreenQuad) :
|
||||
}
|
||||
|
||||
private companion object {
|
||||
const val TAG = "SpektrafilmToneAlgorithm"
|
||||
const val TAG = "SpectralFilmStageTextures"
|
||||
const val FALLBACK_KEY = "fallback"
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,7 @@ import com.hinnka.mycamera.livephoto.LivePhotoRecorder
|
||||
import com.hinnka.mycamera.camera.MeteringMode
|
||||
import com.hinnka.mycamera.lut.LutConfig
|
||||
import com.hinnka.mycamera.lut.LutRenderer
|
||||
import com.hinnka.mycamera.raw.SpectralFilmLut
|
||||
import com.hinnka.mycamera.preview.EyeFocusPreviewFrame
|
||||
import com.hinnka.mycamera.preview.EyeFocusProcessingTiming
|
||||
import com.hinnka.mycamera.lut.PreviewCaptureSource
|
||||
@@ -224,6 +225,11 @@ class CameraGLSurfaceView @JvmOverloads constructor(
|
||||
}
|
||||
}
|
||||
|
||||
fun setSpectralFilmPreview(lut: SpectralFilmLut?) {
|
||||
renderer.setSpectralFilmPreview(lut)
|
||||
requestRender()
|
||||
}
|
||||
|
||||
fun setBaselineLut(lutConfig: LutConfig?) {
|
||||
queueEvent {
|
||||
renderer.setBaselineLut(lutConfig)
|
||||
|
||||
@@ -22,6 +22,7 @@ import com.hinnka.mycamera.livephoto.LivePhotoRecorder
|
||||
import com.hinnka.mycamera.camera.FocusPointSource
|
||||
import com.hinnka.mycamera.camera.MeteringMode
|
||||
import com.hinnka.mycamera.lut.LutConfig
|
||||
import com.hinnka.mycamera.raw.SpectralFilmLut
|
||||
import com.hinnka.mycamera.model.ColorRecipeParams
|
||||
import com.hinnka.mycamera.preview.EyeFocusPreviewFrame
|
||||
import com.hinnka.mycamera.stabilization.DEFAULT_VIDEO_STABILIZATION_LOOKAHEAD
|
||||
@@ -48,6 +49,7 @@ fun CameraPreviewGL(
|
||||
calibrationOffset: Int,
|
||||
baselineLut: LutConfig?,
|
||||
currentLut: LutConfig?,
|
||||
spectralFilmPreviewLut: SpectralFilmLut?,
|
||||
baselineColorRecipeParams: ColorRecipeParams,
|
||||
colorRecipeParams: ColorRecipeParams,
|
||||
focusPoint: Pair<Float, Float>?,
|
||||
@@ -292,6 +294,7 @@ fun CameraPreviewGL(
|
||||
val colorRecipeEnabled = !colorRecipeParams.isDefault()
|
||||
val baselineColorRecipeEnabled = !baselineColorRecipeParams.isDefault()
|
||||
// 更新 LUT 设置
|
||||
glSurfaceView.setSpectralFilmPreview(spectralFilmPreviewLut)
|
||||
glSurfaceView.setBaselineLut(baselineLut)
|
||||
glSurfaceView.setBaselineLutEnabled(baselineLut != null)
|
||||
glSurfaceView.setLut(currentLut)
|
||||
|
||||
@@ -1182,6 +1182,7 @@ fun CameraScreen(
|
||||
calibrationOffset = calibrationOffset,
|
||||
baselineLut = viewModel.currentBaselineLutConfig,
|
||||
currentLut = viewModel.currentLutConfig,
|
||||
spectralFilmPreviewLut = viewModel.currentSpectralFilmPreviewLut,
|
||||
baselineColorRecipeParams = currentBaselineRecipeParams,
|
||||
colorRecipeParams = previewRecipeParamsOverride ?: currentRecipeParams,
|
||||
focusPoint = state.focusPoint,
|
||||
|
||||
@@ -91,6 +91,8 @@ import com.hinnka.mycamera.raw.RawToneMappingParameters
|
||||
import com.hinnka.mycamera.raw.RawNoiseProfileInfo
|
||||
import com.hinnka.mycamera.raw.RawNoiseProfileManager
|
||||
import com.hinnka.mycamera.raw.RawWhiteLevelCorrection
|
||||
import com.hinnka.mycamera.raw.SpectralFilmLut
|
||||
import com.hinnka.mycamera.raw.SpectralFilmProfile
|
||||
import com.hinnka.mycamera.raw.SpectralFilmSelection
|
||||
import com.hinnka.mycamera.raw.SpectralFilmTuning
|
||||
import com.hinnka.mycamera.screencapture.PhantomPipCrop
|
||||
@@ -613,6 +615,10 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
|
||||
var currentBaselineLutConfig: LutConfig? by mutableStateOf(null)
|
||||
private set
|
||||
|
||||
/** RAW 引擎为 Spektrafilm 时用于取景器实时预览的胶片/相纸阶段。 */
|
||||
var currentSpectralFilmPreviewLut: SpectralFilmLut? by mutableStateOf(null)
|
||||
private set
|
||||
|
||||
var currentLutId = MutableStateFlow("standard")
|
||||
private set
|
||||
|
||||
@@ -2358,6 +2364,24 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
|
||||
}
|
||||
}
|
||||
|
||||
viewModelScope.launch {
|
||||
userPreferencesRepository.userPreferences
|
||||
.map { resolvePreviewSpectralFilmSettings(it) }
|
||||
.distinctUntilChanged()
|
||||
.collectLatest { settings ->
|
||||
val stock = settings?.stock
|
||||
val print = settings?.print
|
||||
currentSpectralFilmPreviewLut = if (stock != null && print != null) {
|
||||
// 与 RAW 处理共享 SpectralFilmProfile 的选择缓存与磁盘 LUT 缓存。
|
||||
withContext(Dispatchers.IO) {
|
||||
SpectralFilmProfile.loadCombinedLut(getApplication(), stock, print, settings.tuning)
|
||||
}
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 加载用户偏好设置
|
||||
viewModelScope.launch {
|
||||
val prefs = userPreferencesRepository.userPreferences.firstOrNull()
|
||||
@@ -2976,6 +3000,7 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
|
||||
cameraController.setLogLutActive(loadedLut?.curve?.isLog == true)
|
||||
glSurfaceView?.let { view ->
|
||||
val currentState = state.value
|
||||
view.setSpectralFilmPreview(currentSpectralFilmPreviewLut)
|
||||
view.setBaselineLut(currentBaselineLutConfig)
|
||||
view.setBaselineLutEnabled(currentBaselineLutConfig != null)
|
||||
view.setBaselineParams(currentBaselineRecipeParams.value)
|
||||
@@ -3117,6 +3142,13 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
|
||||
)
|
||||
}
|
||||
|
||||
private fun resolvePreviewSpectralFilmSettings(userPrefs: UserPreferences): RawSpectralFilmSettings? {
|
||||
// 取景器只在 RAW 拍照预览中模拟引擎,条件与 RAW 基准色彩层一致。
|
||||
if (resolvePreviewBaselineTarget(userPrefs) == null) return null
|
||||
if (userPrefs.rawRenderingEngine != RawRenderingEngine.Spektrafilm) return null
|
||||
return resolveRawSpectralFilmSettings(userPrefs)
|
||||
}
|
||||
|
||||
private fun resolvePreviewBaselineLut(userPrefs: UserPreferences): LutConfig? {
|
||||
val baselineLutId = userPrefs.rawBaselineLutId
|
||||
.takeIf { resolvePreviewBaselineTarget(userPrefs) != null }
|
||||
|
||||
Reference in new issue
Block a user