diff --git a/app/src/main/java/com/hinnka/mycamera/raw/RawDemosaicProcessor.kt b/app/src/main/java/com/hinnka/mycamera/raw/RawDemosaicProcessor.kt index a1dd37ce..be322e16 100644 --- a/app/src/main/java/com/hinnka/mycamera/raw/RawDemosaicProcessor.kt +++ b/app/src/main/java/com/hinnka/mycamera/raw/RawDemosaicProcessor.kt @@ -208,7 +208,7 @@ class RawDemosaicProcessor { private var eglSurface: EGLSurface = EGL14.EGL_NO_SURFACE // GL 资源 - private var combinedProgram = 0 + private val combinedPrograms = IntArray(RawColorEngine.entries.size) private var sharpenProgram = 0 private var passthroughProgram = 0 private var hdrReferenceProgram = 0 @@ -1161,7 +1161,7 @@ class RawDemosaicProcessor { // 5. 第二步:Combined Pass (HDR Linear -> LDR sRGB + LUT) setupCombinedFramebuffer(actualWidth, actualHeight) val combinedStart = System.currentTimeMillis() - renderCombinedPass( + val combinedRendered = renderCombinedPass( metadata = actualMetadata, inputTextureId = outputTexture, dcpRenderPlan = resolvedDcpRenderPlan, @@ -1171,6 +1171,10 @@ class RawDemosaicProcessor { workingColorSpace = rawWorkingColorSpace, shadowsHighlightsParams = shadowsHighlightsParams ) + if (!combinedRendered) { + PLog.e(TAG, "Combined Pass failed for colorEngine=$colorEngine") + return@withContext null + } PLog.d(TAG, "Combined Pass took: ${System.currentTimeMillis() - combinedStart}ms") // outputTexture 已被 combinedPass 消费,提前释放 if (gfTexId[1] != 0) { @@ -1244,6 +1248,15 @@ class RawDemosaicProcessor { initialize() } + private inline fun measureRawGlInitStep(name: String, block: () -> T): T { + val start = System.currentTimeMillis() + try { + return block() + } finally { + PLog.d(TAG, "RAW GL init $name took: ${System.currentTimeMillis() - start}ms") + } + } + /** * 初始化 EGL 环境 */ @@ -1251,8 +1264,11 @@ class RawDemosaicProcessor { if (isInitialized) return true try { + val initializeStart = System.currentTimeMillis() // 获取 EGL Display - eglDisplay = EGL14.eglGetDisplay(EGL14.EGL_DEFAULT_DISPLAY) + eglDisplay = measureRawGlInitStep("eglGetDisplay") { + EGL14.eglGetDisplay(EGL14.EGL_DEFAULT_DISPLAY) + } if (eglDisplay == EGL14.EGL_NO_DISPLAY) { PLog.e(TAG, "Unable to get EGL display") return false @@ -1260,7 +1276,10 @@ class RawDemosaicProcessor { // 初始化 EGL val version = IntArray(2) - if (!EGL14.eglInitialize(eglDisplay, version, 0, version, 1)) { + val eglInitialized = measureRawGlInitStep("eglInitialize") { + EGL14.eglInitialize(eglDisplay, version, 0, version, 1) + } + if (!eglInitialized) { PLog.e(TAG, "Unable to initialize EGL") return false } @@ -1278,7 +1297,8 @@ class RawDemosaicProcessor { val configs = arrayOfNulls(1) val numConfigs = IntArray(1) - if (!EGL14.eglChooseConfig( + val configChosen = measureRawGlInitStep("eglChooseConfig") { + EGL14.eglChooseConfig( eglDisplay, configAttribs, 0, @@ -1288,7 +1308,8 @@ class RawDemosaicProcessor { numConfigs, 0 ) - ) { + } + if (!configChosen) { PLog.e(TAG, "Unable to choose EGL config") return false } @@ -1300,8 +1321,9 @@ class RawDemosaicProcessor { EGL14.EGL_CONTEXT_CLIENT_VERSION, 3, EGL14.EGL_NONE ) - eglContext = + eglContext = measureRawGlInitStep("eglCreateContext") { EGL14.eglCreateContext(eglDisplay, config, EGL14.EGL_NO_CONTEXT, contextAttribs, 0) + } if (eglContext == EGL14.EGL_NO_CONTEXT) { PLog.e(TAG, "Unable to create EGL context") return false @@ -1313,21 +1335,28 @@ class RawDemosaicProcessor { EGL14.EGL_HEIGHT, 1, EGL14.EGL_NONE ) - eglSurface = EGL14.eglCreatePbufferSurface(eglDisplay, config, surfaceAttribs, 0) + eglSurface = measureRawGlInitStep("eglCreatePbufferSurface") { + EGL14.eglCreatePbufferSurface(eglDisplay, config, surfaceAttribs, 0) + } if (eglSurface == EGL14.EGL_NO_SURFACE) { PLog.e(TAG, "Unable to create EGL surface") return false } // 激活上下文 - if (!EGL14.eglMakeCurrent(eglDisplay, eglSurface, eglSurface, eglContext)) { + val madeCurrent = measureRawGlInitStep("eglMakeCurrent") { + EGL14.eglMakeCurrent(eglDisplay, eglSurface, eglSurface, eglContext) + } + if (!madeCurrent) { PLog.e(TAG, "Unable to make EGL current") return false } // 初始化着色器和缓冲区 - initShaderProgram() - if (combinedProgram == 0 || sharpenProgram == 0 || passthroughProgram == 0 || + measureRawGlInitStep("initShaderProgram") { + initShaderProgram() + } + if (sharpenProgram == 0 || passthroughProgram == 0 || chromaDenoiseProgram == 0 || rcdPopulateProgram == 0 || rcdStep1Program == 0 || rcdStep2Program == 0 || rcdStep3Program == 0 || rcdStep40Program == 0 || rcdStep41Program == 0 || @@ -1336,7 +1365,7 @@ class RawDemosaicProcessor { ) { PLog.e( TAG, "Critical shader programs failed to compile or link. " + - "combined=$combinedProgram sharpen=$sharpenProgram pass=$passthroughProgram " + + "sharpen=$sharpenProgram pass=$passthroughProgram " + "chromaDenoise=$chromaDenoiseProgram " + "populate=$rcdPopulateProgram step1=$rcdStep1Program step2=$rcdStep2Program " + "step3=$rcdStep3Program step40=$rcdStep40Program step41=$rcdStep41Program " + @@ -1345,20 +1374,26 @@ class RawDemosaicProcessor { ) return false } - initBuffers() + measureRawGlInitStep("initBuffers") { + initBuffers() + } // 创建静默遮挡图 - dummyShadingTextureId = createDummyShadingTexture() + dummyShadingTextureId = measureRawGlInitStep("createDummyShadingTexture") { + createDummyShadingTexture() + } // Query hardware texture size limit val maxTexSizeArr = IntArray(1) - GLES30.glGetIntegerv(GLES30.GL_MAX_TEXTURE_SIZE, maxTexSizeArr, 0) + measureRawGlInitStep("queryLimits") { + GLES30.glGetIntegerv(GLES30.GL_MAX_TEXTURE_SIZE, maxTexSizeArr, 0) + } maxTextureSize = maxTexSizeArr[0] PLog.d(TAG, "GL_MAX_TEXTURE_SIZE = $maxTextureSize") logGlResourceLimits() isInitialized = true - PLog.d(TAG, "RawDemosaicProcessor initialized") + PLog.d(TAG, "RawDemosaicProcessor initialized, took=${System.currentTimeMillis() - initializeStart}ms") return true } catch (e: Exception) { @@ -1368,34 +1403,28 @@ class RawDemosaicProcessor { } private fun initShaderProgram() { - val vShader = compileShader(GLES30.GL_VERTEX_SHADER, RawShaders.VERTEX_SHADER) + val vShader = compileShader( + GLES30.GL_VERTEX_SHADER, + RawShaders.VERTEX_SHADER, + "rawVertex" + ) // 1. DHT Multi-Pass Programs (替代旧的单 pass AHD) // initDhtPrograms(vShader) - // 2. Combined Processing Program - val fShaderCombined = - compileShader(GLES30.GL_FRAGMENT_SHADER, RawShaders.COMBINED_FRAGMENT_SHADER) - if (vShader != 0 && fShaderCombined != 0) { - combinedProgram = GLES30.glCreateProgram() - GLES30.glAttachShader(combinedProgram, vShader) - GLES30.glAttachShader(combinedProgram, fShaderCombined) - GLES30.glLinkProgram(combinedProgram) - if (!logProgramLinkResult(combinedProgram, "combinedProgram")) { - combinedProgram = 0 - } - - GLES30.glDeleteShader(fShaderCombined) - } - val fShaderHdrReference = - compileShader(GLES30.GL_FRAGMENT_SHADER, RawShaders.HDR_REFERENCE_FRAGMENT_SHADER) + compileShader( + GLES30.GL_FRAGMENT_SHADER, + RawShaders.HDR_REFERENCE_FRAGMENT_SHADER, + "hdrReferenceFragment" + ) if (vShader != 0 && fShaderHdrReference != 0) { hdrReferenceProgram = GLES30.glCreateProgram() GLES30.glAttachShader(hdrReferenceProgram, vShader) GLES30.glAttachShader(hdrReferenceProgram, fShaderHdrReference) + val linkStart = System.currentTimeMillis() GLES30.glLinkProgram(hdrReferenceProgram) - if (!logProgramLinkResult(hdrReferenceProgram, "hdrReferenceProgram")) { + if (!logProgramLinkResult(hdrReferenceProgram, "hdrReferenceProgram", linkStart)) { hdrReferenceProgram = 0 } @@ -1404,13 +1433,18 @@ class RawDemosaicProcessor { // 2.2 Sharpen Program val fShaderSharpen = - compileShader(GLES30.GL_FRAGMENT_SHADER, RawShaders.SHARPEN_FRAGMENT_SHADER) + compileShader( + GLES30.GL_FRAGMENT_SHADER, + RawShaders.SHARPEN_FRAGMENT_SHADER, + "sharpenFragment" + ) if (vShader != 0 && fShaderSharpen != 0) { sharpenProgram = GLES30.glCreateProgram() GLES30.glAttachShader(sharpenProgram, vShader) GLES30.glAttachShader(sharpenProgram, fShaderSharpen) + val linkStart = System.currentTimeMillis() GLES30.glLinkProgram(sharpenProgram) - if (!logProgramLinkResult(sharpenProgram, "sharpenProgram")) { + if (!logProgramLinkResult(sharpenProgram, "sharpenProgram", linkStart)) { sharpenProgram = 0 } @@ -1422,13 +1456,18 @@ class RawDemosaicProcessor { // 2.75 RAW 默认色度降噪 Program val fShaderChromaDenoise = - compileShader(GLES30.GL_FRAGMENT_SHADER, ChromaDenoiseShaders.PASS_CHROMA_DENOISE) + compileShader( + GLES30.GL_FRAGMENT_SHADER, + ChromaDenoiseShaders.PASS_CHROMA_DENOISE, + "rawChromaDenoiseFragment" + ) if (vShader != 0 && fShaderChromaDenoise != 0) { chromaDenoiseProgram = GLES30.glCreateProgram() GLES30.glAttachShader(chromaDenoiseProgram, vShader) GLES30.glAttachShader(chromaDenoiseProgram, fShaderChromaDenoise) + val linkStart = System.currentTimeMillis() GLES30.glLinkProgram(chromaDenoiseProgram) - if (!logProgramLinkResult(chromaDenoiseProgram, "rawChromaDenoiseProgram")) { + if (!logProgramLinkResult(chromaDenoiseProgram, "rawChromaDenoiseProgram", linkStart)) { chromaDenoiseProgram = 0 } @@ -1437,13 +1476,18 @@ class RawDemosaicProcessor { // 3. Passthrough Program val fShaderPass = - compileShader(GLES30.GL_FRAGMENT_SHADER, RawShaders.PASSTHROUGH_FRAGMENT_SHADER) + compileShader( + GLES30.GL_FRAGMENT_SHADER, + RawShaders.PASSTHROUGH_FRAGMENT_SHADER, + "passthroughFragment" + ) if (vShader != 0 && fShaderPass != 0) { passthroughProgram = GLES30.glCreateProgram() GLES30.glAttachShader(passthroughProgram, vShader) GLES30.glAttachShader(passthroughProgram, fShaderPass) + val linkStart = System.currentTimeMillis() GLES30.glLinkProgram(passthroughProgram) - if (!logProgramLinkResult(passthroughProgram, "passthroughProgram")) { + if (!logProgramLinkResult(passthroughProgram, "passthroughProgram", linkStart)) { passthroughProgram = 0 } @@ -1456,10 +1500,49 @@ class RawDemosaicProcessor { GLES30.glDeleteShader(vShader) PLog.d( TAG, - "Shader programs created: combined=$combinedProgram, passthrough=$passthroughProgram" + "Shader programs created: passthrough=$passthroughProgram" ) } + private fun getOrCreateCombinedProgram(colorEngine: RawColorEngine): Int { + val cachedProgram = combinedPrograms[colorEngine.ordinal] + if (cachedProgram != 0) return cachedProgram + + val vShader = compileShader( + GLES30.GL_VERTEX_SHADER, + RawShaders.VERTEX_SHADER, + "combined${colorEngine.name}Vertex" + ) + val fragmentSource = RawShaders.combinedFragmentShaderFor(colorEngine) + val fShader = compileShader( + GLES30.GL_FRAGMENT_SHADER, + fragmentSource, + "combined${colorEngine.name}Fragment" + ) + if (vShader == 0 || fShader == 0) { + if (vShader != 0) GLES30.glDeleteShader(vShader) + if (fShader != 0) GLES30.glDeleteShader(fShader) + return 0 + } + + val program = GLES30.glCreateProgram() + GLES30.glAttachShader(program, vShader) + GLES30.glAttachShader(program, fShader) + val linkStart = System.currentTimeMillis() + GLES30.glLinkProgram(program) + val linked = logProgramLinkResult( + program, + "combined${colorEngine.name}Program", + linkStart + ) + GLES30.glDeleteShader(vShader) + GLES30.glDeleteShader(fShader) + if (!linked) return 0 + + combinedPrograms[colorEngine.ordinal] = program + return program + } + private val FRAGMENT_SHADER_LINEAR_RCD = """ #version 300 es precision highp float; @@ -1809,13 +1892,18 @@ class RawDemosaicProcessor { rawAeBaseProgram = compileComputeProgram(RAW_AE_BASE_COMPUTE_SHADER, "RAW_AE_BASE") rawAeMertensProgram = compileComputeProgram(RAW_AE_MERTENS_COMPUTE_SHADER, "RAW_AE_MERTENS") - val fShaderLinearRcd = compileShader(GLES30.GL_FRAGMENT_SHADER, FRAGMENT_SHADER_LINEAR_RCD) + val fShaderLinearRcd = compileShader( + GLES30.GL_FRAGMENT_SHADER, + FRAGMENT_SHADER_LINEAR_RCD, + "linearRcdFragment" + ) if (vShader != 0 && fShaderLinearRcd != 0) { linearRcdProgram = GLES30.glCreateProgram() GLES30.glAttachShader(linearRcdProgram, vShader) GLES30.glAttachShader(linearRcdProgram, fShaderLinearRcd) + val linkStart = System.currentTimeMillis() GLES30.glLinkProgram(linearRcdProgram) - if (!logProgramLinkResult(linearRcdProgram, "linearRcdProgram")) { + if (!logProgramLinkResult(linearRcdProgram, "linearRcdProgram", linkStart)) { linearRcdProgram = 0 } GLES30.glDeleteShader(fShaderLinearRcd) @@ -1823,6 +1911,7 @@ class RawDemosaicProcessor { } private fun compileComputeProgram(source: String, name: String): Int { + val compileStart = System.currentTimeMillis() val shader = GLES31.glCreateShader(GLES31.GL_COMPUTE_SHADER) GLES31.glShaderSource(shader, source) GLES31.glCompileShader(shader) @@ -1831,31 +1920,49 @@ class RawDemosaicProcessor { GLES31.glGetShaderiv(shader, GLES31.GL_COMPILE_STATUS, compiled, 0) if (compiled[0] == 0) { val error = GLES31.glGetShaderInfoLog(shader) - PLog.e(TAG, "Compute Shader $name compilation failed: $error") + PLog.e( + TAG, + "Compute Shader $name compilation failed after " + + "${System.currentTimeMillis() - compileStart}ms, chars=${source.length}: $error" + ) GLES31.glDeleteShader(shader) return 0 } + PLog.d( + TAG, + "Compute Shader $name compile ok, chars=${source.length}, " + + "took=${System.currentTimeMillis() - compileStart}ms" + ) val program = GLES31.glCreateProgram() GLES31.glAttachShader(program, shader) + val linkStart = System.currentTimeMillis() GLES31.glLinkProgram(program) val linked = IntArray(1) GLES31.glGetProgramiv(program, GLES31.GL_LINK_STATUS, linked, 0) if (linked[0] == 0) { val error = GLES31.glGetProgramInfoLog(program) - PLog.e(TAG, "Compute Program $name linking failed: $error") + PLog.e( + TAG, + "Compute Program $name linking failed after " + + "${System.currentTimeMillis() - linkStart}ms: $error" + ) GLES31.glDeleteProgram(program) GLES31.glDeleteShader(shader) return 0 } GLES31.glDeleteShader(shader) - PLog.d(TAG, "Compute Program $name created: $program") + PLog.d( + TAG, + "Compute Program $name created: $program, linkTook=${System.currentTimeMillis() - linkStart}ms" + ) return program } - private fun compileShader(type: Int, source: String): Int { + private fun compileShader(type: Int, source: String, name: String = "shader"): Int { + val compileStart = System.currentTimeMillis() val shader = GLES30.glCreateShader(type) GLES30.glShaderSource(shader, source) GLES30.glCompileShader(shader) @@ -1864,10 +1971,19 @@ class RawDemosaicProcessor { GLES30.glGetShaderiv(shader, GLES30.GL_COMPILE_STATUS, compiled, 0) if (compiled[0] == 0) { val error = GLES30.glGetShaderInfoLog(shader) - PLog.e(TAG, "Shader compilation failed: $error") + PLog.e( + TAG, + "Shader $name compilation failed after " + + "${System.currentTimeMillis() - compileStart}ms, type=$type, chars=${source.length}: $error" + ) GLES30.glDeleteShader(shader) return 0 } + PLog.d( + TAG, + "Shader $name compile ok, type=$type, chars=${source.length}, " + + "took=${System.currentTimeMillis() - compileStart}ms" + ) return shader } @@ -3514,21 +3630,21 @@ class RawDemosaicProcessor { return textureId } - private fun bindDcpCombinedResources(dcpRenderPlan: DcpRenderPlan?) { + private fun bindDcpCombinedResources(program: Int, dcpRenderPlan: DcpRenderPlan?) { val hueSatMap = dcpRenderPlan?.hueSatMap?.takeIf { it.isValid } val lookTable = dcpRenderPlan?.lookTable?.takeIf { it.isValid } PLog.d(TAG, "bindDcpCombinedResources: hueSatMap=${hueSatMap?.values?.size}") PLog.d(TAG, "bindDcpCombinedResources: lookTable=${lookTable?.values?.size}") - GLES30.glUniform1i(GLES30.glGetUniformLocation(combinedProgram, "uDcpHueSatTexture"), 2) - GLES30.glUniform1i(GLES30.glGetUniformLocation(combinedProgram, "uDcpLookTableTexture"), 3) + GLES30.glUniform1i(GLES30.glGetUniformLocation(program, "uDcpHueSatTexture"), 2) + GLES30.glUniform1i(GLES30.glGetUniformLocation(program, "uDcpLookTableTexture"), 3) GLES30.glUniform1i( - GLES30.glGetUniformLocation(combinedProgram, "uDcpHueSatEnabled"), + GLES30.glGetUniformLocation(program, "uDcpHueSatEnabled"), if (hueSatMap != null) 1 else 0 ) GLES30.glUniform1i( - GLES30.glGetUniformLocation(combinedProgram, "uDcpLookTableEnabled"), + GLES30.glGetUniformLocation(program, "uDcpLookTableEnabled"), if (lookTable != null) 1 else 0 ) @@ -3538,26 +3654,26 @@ class RawDemosaicProcessor { uploadDcp3DTexture({ dcpHueSatTextureId }, { dcpHueSatTextureId = it }, hueSatMap) GLES30.glBindTexture(GLES30.GL_TEXTURE_3D, textureId) GLES30.glUniform3i( - GLES30.glGetUniformLocation(combinedProgram, "uDcpHueSatDivisions"), + GLES30.glGetUniformLocation(program, "uDcpHueSatDivisions"), hueSatMap.hueDivisions, hueSatMap.satDivisions, hueSatMap.valueDivisions ) GLES30.glUniform1i( - GLES30.glGetUniformLocation(combinedProgram, "uDcpHueSatEncoding"), + GLES30.glGetUniformLocation(program, "uDcpHueSatEncoding"), hueSatMap.encoding ) } else { GLES30.glActiveTexture(GLES30.GL_TEXTURE2) GLES30.glBindTexture(GLES30.GL_TEXTURE_3D, ensureDummyDcp3DTexture()) GLES30.glUniform3i( - GLES30.glGetUniformLocation(combinedProgram, "uDcpHueSatDivisions"), + GLES30.glGetUniformLocation(program, "uDcpHueSatDivisions"), 1, 1, 1 ) GLES30.glUniform1i( - GLES30.glGetUniformLocation(combinedProgram, "uDcpHueSatEncoding"), + GLES30.glGetUniformLocation(program, "uDcpHueSatEncoding"), DcpHueSatMap.ENCODING_LINEAR ) } @@ -3571,26 +3687,26 @@ class RawDemosaicProcessor { ) GLES30.glBindTexture(GLES30.GL_TEXTURE_3D, textureId) GLES30.glUniform3i( - GLES30.glGetUniformLocation(combinedProgram, "uDcpLookTableDivisions"), + GLES30.glGetUniformLocation(program, "uDcpLookTableDivisions"), lookTable.hueDivisions, lookTable.satDivisions, lookTable.valueDivisions ) GLES30.glUniform1i( - GLES30.glGetUniformLocation(combinedProgram, "uDcpLookTableEncoding"), + GLES30.glGetUniformLocation(program, "uDcpLookTableEncoding"), lookTable.encoding ) } else { GLES30.glActiveTexture(GLES30.GL_TEXTURE3) GLES30.glBindTexture(GLES30.GL_TEXTURE_3D, ensureDummyDcp3DTexture()) GLES30.glUniform3i( - GLES30.glGetUniformLocation(combinedProgram, "uDcpLookTableDivisions"), + GLES30.glGetUniformLocation(program, "uDcpLookTableDivisions"), 1, 1, 1 ) GLES30.glUniform1i( - GLES30.glGetUniformLocation(combinedProgram, "uDcpLookTableEncoding"), + GLES30.glGetUniformLocation(program, "uDcpLookTableEncoding"), DcpHueSatMap.ENCODING_LINEAR ) } @@ -3654,10 +3770,10 @@ class RawDemosaicProcessor { return spectralFilmTextureId } - private fun bindSpectralFilmCombinedResource(lut: SpectralFilmLut?) { - GLES30.glUniform1i(GLES30.glGetUniformLocation(combinedProgram, "uSpectralFilmTexture"), 6) + private fun bindSpectralFilmCombinedResource(program: Int, lut: SpectralFilmLut?) { + GLES30.glUniform1i(GLES30.glGetUniformLocation(program, "uSpectralFilmTexture"), 6) GLES30.glUniform1i( - GLES30.glGetUniformLocation(combinedProgram, "uSpectralFilmSize"), + GLES30.glGetUniformLocation(program, "uSpectralFilmSize"), lut?.size ?: 1 ) GLES30.glActiveTexture(GLES30.GL_TEXTURE6) @@ -3719,13 +3835,13 @@ class RawDemosaicProcessor { return agxLutTextureId } - private fun bindAgxCombinedResource(agxLut: AgxLut?) { + private fun bindAgxCombinedResource(program: Int, agxLut: AgxLut?) { GLES30.glUniform1i( - GLES30.glGetUniformLocation(combinedProgram, "uAgxBaseSrgbTexture"), + GLES30.glGetUniformLocation(program, "uAgxBaseSrgbTexture"), AGX_BASE_SRGB_TEXTURE_UNIT ) GLES30.glUniform1i( - GLES30.glGetUniformLocation(combinedProgram, "uAgxLutSize"), + GLES30.glGetUniformLocation(program, "uAgxLutSize"), agxLut?.size ?: 1 ) GLES30.glActiveTexture(GLES30.GL_TEXTURE0 + AGX_BASE_SRGB_TEXTURE_UNIT) @@ -3751,11 +3867,15 @@ class RawDemosaicProcessor { shadowsHighlightsParams: ShadowsHighlightsParams = ShadowsHighlightsParams.NEUTRAL, viewportWidth: Int = metadata.width, viewportHeight: Int = metadata.height - ) { - val baseCurve = dcpRenderPlan?.toneCurveLut ?: ACR3Curve.samples() + ): Boolean { val outputTransform = computeWorkingToOutputTransform(workingColorSpace, ColorSpace.SRGB) + val program = getOrCreateCombinedProgram(colorEngine) + if (program == 0) { + PLog.e(TAG, "Unable to create combined program for colorEngine=$colorEngine") + return false + } - GLES30.glUseProgram(combinedProgram) + GLES30.glUseProgram(program) checkGlError("renderCombinedPass glUseProgram") GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, combinedFramebufferId) checkGlError("renderCombinedPass glBindFramebuffer") @@ -3766,59 +3886,64 @@ class RawDemosaicProcessor { GLES30.glActiveTexture(GLES30.GL_TEXTURE0) GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, inputTextureId) - GLES30.glUniform1i(GLES30.glGetUniformLocation(combinedProgram, "uInputTexture"), 0) + GLES30.glUniform1i(GLES30.glGetUniformLocation(program, "uInputTexture"), 0) - GLES30.glActiveTexture(GLES30.GL_TEXTURE1) - uploadCurveTexture(baseCurve) - GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, curveTextureId) - GLES30.glUniform1i(GLES30.glGetUniformLocation(combinedProgram, "uCurveTexture"), 1) - GLES30.glUniform1f( - GLES30.glGetUniformLocation(combinedProgram, "uCurveSize"), - baseCurve.size.toFloat() - ) - GLES30.glUniform1i( - GLES30.glGetUniformLocation(combinedProgram, "uCurveEnabled"), - 1 - ) GLES30.glUniform2f( - GLES30.glGetUniformLocation(combinedProgram, "uTexelSize"), + GLES30.glGetUniformLocation(program, "uTexelSize"), 1.0f / maxOf(1, viewportWidth).toFloat(), 1.0f / maxOf(1, viewportHeight).toFloat() ) - bindShadowsHighlightsUniforms(shadowsHighlightsParams) + bindShadowsHighlightsUniforms(program, shadowsHighlightsParams) checkGlError("renderCombinedPass base uniforms") - bindDcpCombinedResources(dcpRenderPlan) - bindSpectralFilmCombinedResource( - if (colorEngine == RawColorEngine.SpectralFilm) spectralFilmLut else null - ) - bindAgxCombinedResource( - agxLut = if (colorEngine == RawColorEngine.AgX) agxLut else null - ) - GLES30.glUniform1i( - GLES30.glGetUniformLocation(combinedProgram, "uRawColorEngine"), - colorEngine.shaderId - ) + when (colorEngine) { + RawColorEngine.AdobeCurve -> { + val baseCurve = dcpRenderPlan?.toneCurveLut ?: ACR3Curve.samples() + bindCurveCombinedResource(program, baseCurve) + bindDcpCombinedResources(program, dcpRenderPlan) + } + + RawColorEngine.AgX -> bindAgxCombinedResource(program, agxLut) + RawColorEngine.SpectralFilm -> bindSpectralFilmCombinedResource(program, spectralFilmLut) + RawColorEngine.DarktableSigmoid, + RawColorEngine.DarktableFilmic -> Unit + } GLES30.glUniformMatrix3fv( - GLES30.glGetUniformLocation(combinedProgram, "uOutputTransform"), + GLES30.glGetUniformLocation(program, "uOutputTransform"), 1, false, transposeMatrix3x3(outputTransform), 0 ) val identityMatrix = FloatArray(16) GlMatrix.setIdentityM(identityMatrix, 0) GLES30.glUniformMatrix4fv( - GLES30.glGetUniformLocation(combinedProgram, "uTexMatrix"), + GLES30.glGetUniformLocation(program, "uTexMatrix"), 1, false, identityMatrix, 0 ) checkGlError("renderCombinedPass matrices") - drawQuad(combinedProgram) + drawQuad(program) checkGlError("renderCombinedPass") + return true } - private fun bindShadowsHighlightsUniforms(params: ShadowsHighlightsParams) { - val highlightsLocation = GLES30.glGetUniformLocation(combinedProgram, "uHighlights") - val shadowsLocation = GLES30.glGetUniformLocation(combinedProgram, "uShadows") + private fun bindCurveCombinedResource(program: Int, baseCurve: FloatArray) { + GLES30.glActiveTexture(GLES30.GL_TEXTURE1) + uploadCurveTexture(baseCurve) + GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, curveTextureId) + GLES30.glUniform1i(GLES30.glGetUniformLocation(program, "uCurveTexture"), 1) + GLES30.glUniform1f( + GLES30.glGetUniformLocation(program, "uCurveSize"), + baseCurve.size.toFloat() + ) + GLES30.glUniform1i( + GLES30.glGetUniformLocation(program, "uCurveEnabled"), + 1 + ) + } + + private fun bindShadowsHighlightsUniforms(program: Int, params: ShadowsHighlightsParams) { + val highlightsLocation = GLES30.glGetUniformLocation(program, "uHighlights") + val shadowsLocation = GLES30.glGetUniformLocation(program, "uShadows") ShadowsHighlightsShader.bindUniformLocations( highlightsLocation = highlightsLocation, shadowsLocation = shadowsLocation, @@ -3836,16 +3961,20 @@ class RawDemosaicProcessor { ) } GLES30.glUniform1f( - GLES30.glGetUniformLocation(combinedProgram, "uCurveWhitePoint"), + GLES30.glGetUniformLocation(program, "uCurveWhitePoint"), params.curveWhitePoint ) GLES30.glUniform1f( - GLES30.glGetUniformLocation(combinedProgram, "uCurveShoulderStart"), + GLES30.glGetUniformLocation(program, "uCurveShoulderStart"), 0.4f ) } - private fun logProgramLinkResult(program: Int, name: String): Boolean { + private fun logProgramLinkResult( + program: Int, + name: String, + linkStart: Long = System.currentTimeMillis() + ): Boolean { if (program == 0) { PLog.e(TAG, "$name creation failed") return false @@ -3853,11 +3982,15 @@ class RawDemosaicProcessor { val linked = IntArray(1) GLES30.glGetProgramiv(program, GLES30.GL_LINK_STATUS, linked, 0) if (linked[0] == 0) { - PLog.e(TAG, "$name link failed: ${GLES30.glGetProgramInfoLog(program)}") + PLog.e( + TAG, + "$name link failed after ${System.currentTimeMillis() - linkStart}ms: " + + GLES30.glGetProgramInfoLog(program) + ) GLES30.glDeleteProgram(program) return false } else { - PLog.d(TAG, "$name link ok") + PLog.d(TAG, "$name link ok, took=${System.currentTimeMillis() - linkStart}ms") return true } } @@ -4803,7 +4936,12 @@ class RawDemosaicProcessor { EGL14.eglMakeCurrent(eglDisplay, eglSurface, eglSurface, eglContext) - if (combinedProgram != 0) GLES30.glDeleteProgram(combinedProgram) + for (i in combinedPrograms.indices) { + if (combinedPrograms[i] != 0) { + GLES30.glDeleteProgram(combinedPrograms[i]) + combinedPrograms[i] = 0 + } + } if (sharpenProgram != 0) GLES30.glDeleteProgram(sharpenProgram) if (passthroughProgram != 0) GLES30.glDeleteProgram(passthroughProgram) if (hdrReferenceProgram != 0) GLES30.glDeleteProgram(hdrReferenceProgram) diff --git a/app/src/main/java/com/hinnka/mycamera/raw/RawShaders.kt b/app/src/main/java/com/hinnka/mycamera/raw/RawShaders.kt index 3806692b..ab2d0388 100644 --- a/app/src/main/java/com/hinnka/mycamera/raw/RawShaders.kt +++ b/app/src/main/java/com/hinnka/mycamera/raw/RawShaders.kt @@ -71,18 +71,40 @@ object RawShaders { } """.trimIndent() - /** - * Combined Processing Shader - * - * Process chain: - * 1. Linear RGB input in working space - * 2. DCP hue/sat map and look table - * 3. Shared local shadows/highlights in working RGB, before curve clipping - * 4. Highlight rolloff + DCP tone curve or ACR3 curve - * 5. Working space -> Linear sRGB - * 6. Linear sRGB -> sRGB - */ - val COMBINED_FRAGMENT_SHADER = """ + fun combinedFragmentShaderFor(colorEngine: RawColorEngine): String { + return when (colorEngine) { + RawColorEngine.AgX -> combinedFragmentShader( + engineUniforms = AGX_COMBINED_UNIFORMS, + engineFunctions = "$CUBE_LUT_FUNCTIONS\n$AGX_COMBINED_FUNCTIONS" + ) + + RawColorEngine.AdobeCurve -> combinedFragmentShader( + engineUniforms = ADOBE_COMBINED_UNIFORMS, + engineFunctions = + "$CURVE_COMBINED_FUNCTIONS\n$DCP_COMBINED_FUNCTIONS\n$ADOBE_COMBINED_FUNCTIONS" + ) + + RawColorEngine.SpectralFilm -> combinedFragmentShader( + engineUniforms = SPECTRAL_FILM_COMBINED_UNIFORMS, + engineFunctions = SPECTRAL_FILM_COMBINED_FUNCTIONS + ) + + RawColorEngine.DarktableSigmoid -> combinedFragmentShader( + engineUniforms = OUTPUT_TRANSFORM_COMBINED_UNIFORMS, + engineFunctions = DARKTABLE_SIGMOID_COMBINED_FUNCTIONS + ) + + RawColorEngine.DarktableFilmic -> combinedFragmentShader( + engineUniforms = OUTPUT_TRANSFORM_COMBINED_UNIFORMS, + engineFunctions = DARKTABLE_FILMIC_COMBINED_FUNCTIONS + ) + } + } + + private fun combinedFragmentShader( + engineUniforms: String, + engineFunctions: String + ): String = """ #version 300 es precision highp float; precision highp sampler3D; @@ -91,63 +113,89 @@ object RawShaders { out vec4 fragColor; uniform sampler2D uInputTexture; - uniform sampler2D uCurveTexture; - uniform sampler3D uDcpHueSatTexture; - uniform sampler3D uDcpLookTableTexture; - uniform sampler3D uSpectralFilmTexture; - uniform sampler3D uAgxBaseSrgbTexture; - uniform mat3 uOutputTransform; - uniform float uCurveSize; - uniform bool uCurveEnabled; uniform vec2 uTexelSize; uniform float uHighlights; uniform float uShadows; + + $engineUniforms + + vec3 linearToSrgb(vec3 color) { + vec3 clampedColor = max(color, vec3(0.0)); + vec3 low = clampedColor * 12.92; + vec3 high = 1.055 * pow(clampedColor, vec3(1.0 / 2.4)) - 0.055; + bvec3 useHigh = greaterThan(clampedColor, vec3(0.0031308)); + return vec3( + useHigh.r ? high.r : low.r, + useHigh.g ? high.g : low.g, + useHigh.b ? high.b : low.b + ); + } + + $engineFunctions + + vec3 sampleToneSource(vec2 uv) { + vec3 sampleColor = texture(uInputTexture, clamp(uv, vec2(0.0), vec2(1.0))).rgb; + return applyEngineTone(sampleColor); + } + + vec3 shRgbToXyz(vec3 rgb) { + return mat3( + 0.7976749, 0.2880402, 0.0000000, + 0.1351917, 0.7118741, 0.0000000, + 0.0313534, 0.0000857, 0.8252100 + ) * rgb; + } + + vec3 shXyzToRgb(vec3 xyz) { + return mat3( + 1.3459433, -0.5445989, 0.0000000, + -0.2556075, 1.5081673, 0.0000000, + -0.0511118, 0.0205351, 1.2118128 + ) * xyz; + } + + ${ShadowsHighlightsShader.GLSL} + + void main() { + vec3 color = texture(uInputTexture, vTexCoord).rgb; + color = applyEngineTone(color); + color = applyShadowsHighlights(color, vTexCoord); + color = linearToSrgb(color); + fragColor = vec4(color, 1.0); + } + """.trimIndent() + + private val OUTPUT_TRANSFORM_COMBINED_UNIFORMS = """ + uniform mat3 uOutputTransform; + """.trimIndent() + + private val ADOBE_COMBINED_UNIFORMS = """ + uniform sampler2D uCurveTexture; + uniform sampler3D uDcpHueSatTexture; + uniform sampler3D uDcpLookTableTexture; + uniform mat3 uOutputTransform; + uniform float uCurveSize; + uniform bool uCurveEnabled; uniform bool uDcpHueSatEnabled; uniform bool uDcpLookTableEnabled; uniform ivec3 uDcpHueSatDivisions; uniform ivec3 uDcpLookTableDivisions; uniform int uDcpHueSatEncoding; uniform int uDcpLookTableEncoding; - uniform int uSpectralFilmSize; + """.trimIndent() + + private val AGX_COMBINED_UNIFORMS = """ + uniform sampler3D uAgxBaseSrgbTexture; uniform int uAgxLutSize; - uniform int uRawColorEngine; + """.trimIndent() - const int RAW_COLOR_ENGINE_ADOBE_CURVE = 0; - const int RAW_COLOR_ENGINE_AGX = 1; - const int RAW_COLOR_ENGINE_SPECTRAL_FILM = 2; - const int RAW_COLOR_ENGINE_DARKTABLE_SIGMOID = 3; - const int RAW_COLOR_ENGINE_DARKTABLE_FILMIC = 4; - const float AGX_LOG_MIN = -12.47393; - const float AGX_LOG_MAX = 12.5260688117; - const float DT_SIGMOID_WHITE_TARGET = 1.0; - const float DT_SIGMOID_PAPER_EXPOSURE = 0.354355423; - const float DT_SIGMOID_FILM_FOG = 0.00142637086; - const float DT_SIGMOID_FILM_POWER = 1.5; - const float DT_SIGMOID_PAPER_POWER = 1.0; - const float DT_SIGMOID_HUE_PRESERVATION = 1.0; - const float DT_FILMIC_NORM_MIN = 0.0000152587890625; - const float DT_FILMIC_INPUT_MIN = 0.0009185798271; - const float DT_FILMIC_INPUT_MAX = 4.352042729; - const float DT_FILMIC_GREY_SOURCE = 0.1845; - const float DT_FILMIC_BLACK_SOURCE = -7.65; - const float DT_FILMIC_DYNAMIC_RANGE = 12.21; - const float DT_FILMIC_OUTPUT_POWER = 3.614815775; - const float DT_FILMIC_DISPLAY_BLACK = 0.0001517634; - const float DT_FILMIC_DISPLAY_WHITE = 1.0; - const float DT_FILMIC_LATITUDE_MIN = 0.6264986897; - const float DT_FILMIC_LATITUDE_MAX = 0.6265610375; - const float DISPLAY_HEADROOM_ROLLOFF_DEPTH = 0.005; - const float DISPLAY_HEADROOM_ROLLOFF_RANGE = 0.5; - const vec3 DT_FILMIC_M1 = vec3(0.08781340895, -0.1315144048, -0.271791843); - const vec3 DT_FILMIC_M2 = vec3(0.0, 1.387062713, 1.433801098); - const vec3 DT_FILMIC_M3 = vec3(1.36863996, -1.920153105, 0.0); - const vec3 DT_FILMIC_M4 = vec3(0.7402100865, 4.205175692, 0.0); - const vec3 DT_FILMIC_M5 = vec3(-1.171914069, -2.540570894, 0.0); - - float luminance(vec3 color) { - return max(dot(color, vec3(0.2126, 0.7152, 0.0722)), 1e-4); - } + private val SPECTRAL_FILM_COMBINED_UNIFORMS = """ + uniform sampler3D uSpectralFilmTexture; + uniform mat3 uOutputTransform; + uniform int uSpectralFilmSize; + """.trimIndent() + private val CURVE_COMBINED_FUNCTIONS = """ float sampleCurve(float value) { if (!uCurveEnabled || uCurveSize <= 1.0) { return value; @@ -157,12 +205,6 @@ object RawShaders { return texture(uCurveTexture, vec2(coordX, 0.5)).r; } - vec3 applyCurve(vec3 color) { - float luma = luminance(color); - float scale = sampleCurve(luma) / max(luma, 0.00001); - return clamp(color * scale, 0.0, 1.0); - } - void adobeRgbTone(inout float maxValue, inout float midValue, inout float minValue) { float oldMax = maxValue; float oldMid = midValue; @@ -206,19 +248,9 @@ object RawShaders { return clamp(vec3(r, g, b), 0.0, 1.0); } + """.trimIndent() - vec3 linearToSrgb(vec3 color) { - vec3 clampedColor = max(color, vec3(0.0)); - vec3 low = clampedColor * 12.92; - vec3 high = 1.055 * pow(clampedColor, vec3(1.0 / 2.4)) - 0.055; - bvec3 useHigh = greaterThan(clampedColor, vec3(0.0031308)); - return vec3( - useHigh.r ? high.r : low.r, - useHigh.g ? high.g : low.g, - useHigh.b ? high.b : low.b - ); - } - + private val DCP_COMBINED_FUNCTIONS = """ float encodeValue(float value, int encoding) { value = clamp(value, 0.0, 1.0); if (encoding == 1) { @@ -376,33 +408,46 @@ object RawShaders { return dcpHsvToRgb(hsv); } - vec3 linearToProPhoto(vec3 color) { - vec3 clamped = max(color, vec3(0.0)); - vec3 isHigh = step(vec3(0.001953125), clamped); - vec3 lowPart = 16.0 * clamped; - vec3 highPart = pow(clamped, vec3(1.0 / 1.8)); - return mix(lowPart, highPart, isHigh); - } - - vec3 proPhotoToLinear(vec3 color) { - vec3 clamped = clamp(color, 0.0, 1.0); - vec3 isHigh = step(vec3(0.03125), clamped); - vec3 lowPart = clamped / 16.0; - vec3 highPart = pow(clamped, vec3(1.8)); - return mix(lowPart, highPart, isHigh); - } - - vec3 applySpectralFilm(vec3 color) { - if (uSpectralFilmSize <= 1) { - return color; + vec3 applyDcpMaps(vec3 color) { + if (uDcpHueSatEnabled) { + color = applyDcpHsvMap(color, uDcpHueSatTexture, uDcpHueSatDivisions, uDcpHueSatEncoding); } - vec3 normalizedColor = color / 2.88; - vec3 encodedColor = linearToProPhoto(normalizedColor); - vec3 lutCoord = clamp(encodedColor, 0.0, 1.0); - vec3 lutResult = texture(uSpectralFilmTexture, lutCoord).rgb; - return proPhotoToLinear(lutResult); + if (uDcpLookTableEnabled) { + color = applyDcpHsvMap(color, uDcpLookTableTexture, uDcpLookTableDivisions, uDcpLookTableEncoding); + } + return color; + } + """.trimIndent() + + private val ADOBE_COMBINED_FUNCTIONS = """ + float proPhotoLuminance(vec3 color) { + return max(dot(color, vec3(0.2880402, 0.7118741, 0.0000857)), 1e-5); } + float highlightRolloffLuma(float luma) { + const float rolloffStart = 0.2; + const float rolloffRange = 1.0 - rolloffStart; + if (luma <= rolloffStart) return luma; + + float x = (luma - rolloffStart) / rolloffRange; + return 1.0 - rolloffRange / (1.0 + x); + } + + vec3 highlightRolloff(vec3 color) { + float luma = proPhotoLuminance(color); + float newLuma = highlightRolloffLuma(luma); + return color * (newLuma / max(luma, 1e-6)); + } + + vec3 applyEngineTone(vec3 color) { + color = applyDcpMaps(color); + color = highlightRolloff(color); + color = applyAdobeCurve(color); + return uOutputTransform * color; + } + """.trimIndent() + + private val CUBE_LUT_FUNCTIONS = """ vec3 fetchCubeLut(sampler3D lutTexture, int lutSize, ivec3 coord) { ivec3 maxCoord = ivec3(max(lutSize - 1, 0)); return texelFetch(lutTexture, clamp(coord, ivec3(0), maxCoord), 0).rgb; @@ -446,10 +491,11 @@ object RawShaders { } } } + """.trimIndent() - vec3 sampleAgxBaseSrgb(vec3 coord) { - return sampleCubeLut(uAgxBaseSrgbTexture, uAgxLutSize, coord); - } + private val AGX_COMBINED_FUNCTIONS = """ + const float AGX_LOG_MIN = -12.47393; + const float AGX_LOG_MAX = 12.5260688117; vec3 agxLogEncode(vec3 color) { vec3 safeColor = max(color, vec3(exp2(AGX_LOG_MIN))); @@ -460,15 +506,57 @@ object RawShaders { ); } - vec3 applyAgX(vec3 color) { + vec3 applyEngineTone(vec3 color) { if (uAgxLutSize <= 1) { - return uOutputTransform * applyAdobeCurve(color); + return max(color, vec3(0.0)); } vec3 egamut = max(color, vec3(0.0)); vec3 agxLog = agxLogEncode(egamut); - vec3 rec1886Encoded = sampleAgxBaseSrgb(agxLog); + vec3 rec1886Encoded = sampleCubeLut(uAgxBaseSrgbTexture, uAgxLutSize, agxLog); return pow(max(rec1886Encoded, vec3(0.0)), vec3(2.4)); } + """.trimIndent() + + private val SPECTRAL_FILM_COMBINED_FUNCTIONS = """ + vec3 linearToProPhoto(vec3 color) { + vec3 clamped = max(color, vec3(0.0)); + vec3 isHigh = step(vec3(0.001953125), clamped); + vec3 lowPart = 16.0 * clamped; + vec3 highPart = pow(clamped, vec3(1.0 / 1.8)); + return mix(lowPart, highPart, isHigh); + } + + vec3 proPhotoToLinear(vec3 color) { + vec3 clamped = clamp(color, 0.0, 1.0); + vec3 isHigh = step(vec3(0.03125), clamped); + vec3 lowPart = clamped / 16.0; + vec3 highPart = pow(clamped, vec3(1.8)); + return mix(lowPart, highPart, isHigh); + } + + vec3 applySpectralFilm(vec3 color) { + if (uSpectralFilmSize <= 1) { + return color; + } + vec3 normalizedColor = color / 2.88; + vec3 encodedColor = linearToProPhoto(normalizedColor); + vec3 lutCoord = clamp(encodedColor, 0.0, 1.0); + vec3 lutResult = texture(uSpectralFilmTexture, lutCoord).rgb; + return proPhotoToLinear(lutResult); + } + + vec3 applyEngineTone(vec3 color) { + return uOutputTransform * applySpectralFilm(color); + } + """.trimIndent() + + private val DARKTABLE_SIGMOID_COMBINED_FUNCTIONS = """ + const float DT_SIGMOID_WHITE_TARGET = 1.0; + const float DT_SIGMOID_PAPER_EXPOSURE = 0.354355423; + const float DT_SIGMOID_FILM_FOG = 0.00142637086; + const float DT_SIGMOID_FILM_POWER = 1.5; + const float DT_SIGMOID_PAPER_POWER = 1.0; + const float DT_SIGMOID_HUE_PRESERVATION = 1.0; vec3 desaturateNegativeValues(vec3 color) { float pixelAverage = max((color.r + color.g + color.b) / 3.0, 0.0); @@ -585,6 +673,31 @@ object RawShaders { return preserveSigmoidHueAndEnergy(positiveColor, perChannel); } + vec3 applyEngineTone(vec3 color) { + return uOutputTransform * applyDarktableSigmoid(color); + } + """.trimIndent() + + private val DARKTABLE_FILMIC_COMBINED_FUNCTIONS = """ + const float DT_FILMIC_NORM_MIN = 0.0000152587890625; + const float DT_FILMIC_INPUT_MIN = 0.0009185798271; + const float DT_FILMIC_INPUT_MAX = 4.352042729; + const float DT_FILMIC_GREY_SOURCE = 0.1845; + const float DT_FILMIC_BLACK_SOURCE = -7.65; + const float DT_FILMIC_DYNAMIC_RANGE = 12.21; + const float DT_FILMIC_OUTPUT_POWER = 3.614815775; + const float DT_FILMIC_DISPLAY_BLACK = 0.0001517634; + const float DT_FILMIC_DISPLAY_WHITE = 1.0; + const float DT_FILMIC_LATITUDE_MIN = 0.6264986897; + const float DT_FILMIC_LATITUDE_MAX = 0.6265610375; + const float DISPLAY_HEADROOM_ROLLOFF_DEPTH = 0.005; + const float DISPLAY_HEADROOM_ROLLOFF_RANGE = 0.5; + const vec3 DT_FILMIC_M1 = vec3(0.08781340895, -0.1315144048, -0.271791843); + const vec3 DT_FILMIC_M2 = vec3(0.0, 1.387062713, 1.433801098); + const vec3 DT_FILMIC_M3 = vec3(1.36863996, -1.920153105, 0.0); + const vec3 DT_FILMIC_M4 = vec3(0.7402100865, 4.205175692, 0.0); + const vec3 DT_FILMIC_M5 = vec3(-1.171914069, -2.540570894, 0.0); + float darktableFilmicLogEncode(float value) { float safeValue = max(value, DT_FILMIC_NORM_MIN); return clamp( @@ -650,6 +763,12 @@ object RawShaders { return ratios * darktableFilmicNormScalar(toneNorm); } + vec3 applyDarktableFilmic(vec3 color) { + vec3 naiveRgb = darktableFilmicRgbTone(color); + vec3 maxRgb = darktableFilmicMaxRgbTone(color); + return 0.5 * naiveRgb + 0.5 * maxRgb; + } + float displayHeadroomRolloffScalar(float value) { if (value <= 1.0) { return value; @@ -669,102 +788,8 @@ object RawShaders { return color * (rolledPeak / max(peak, 1e-6)); } - vec3 applyDarktableFilmic(vec3 color) { - vec3 naiveRgb = darktableFilmicRgbTone(color); - vec3 maxRgb = darktableFilmicMaxRgbTone(color); - return 0.5 * naiveRgb + 0.5 * maxRgb; - } - - float proPhotoLuminance(vec3 color) { - return max(dot(color, vec3(0.2880402, 0.7118741, 0.0000857)), 1e-5); - } - - float highlightRolloffLuma(float luma) { - const float rolloffStart = 0.2; - const float rolloffRange = 1.0 - rolloffStart; - if (luma <= rolloffStart) return luma; - - float x = (luma - rolloffStart) / rolloffRange; - return 1.0 - rolloffRange / (1.0 + x); - } - - vec3 highlightRolloff(vec3 color) { - float luma = proPhotoLuminance(color); - float newLuma = highlightRolloffLuma(luma); - return color * (newLuma / max(luma, 1e-6)); - } - - vec3 applyDcpMaps(vec3 color) { - if (uDcpHueSatEnabled) { - color = applyDcpHsvMap(color, uDcpHueSatTexture, uDcpHueSatDivisions, uDcpHueSatEncoding); - } - if (uDcpLookTableEnabled) { - color = applyDcpHsvMap(color, uDcpLookTableTexture, uDcpLookTableDivisions, uDcpLookTableEncoding); - } - return color; - } - - vec3 applyDisplayTone(vec3 color) { - if (uRawColorEngine == RAW_COLOR_ENGINE_AGX) { - return applyAgX(color); - } - if (uRawColorEngine == RAW_COLOR_ENGINE_DARKTABLE_SIGMOID) { - return applyDarktableSigmoid(color); - } - if (uRawColorEngine == RAW_COLOR_ENGINE_DARKTABLE_FILMIC) { - return applyDarktableFilmic(color); - } - if (uRawColorEngine == RAW_COLOR_ENGINE_SPECTRAL_FILM && uSpectralFilmSize > 1) { - return applySpectralFilm(color); - } - color = applyDcpMaps(color); - color = highlightRolloff(color); - return applyAdobeCurve(color); - } - - vec3 applyOutputTransformAfterDisplayTone(vec3 color) { - if (uRawColorEngine == RAW_COLOR_ENGINE_AGX) { - return color; - } - vec3 outputColor = uOutputTransform * color; - if (uRawColorEngine == RAW_COLOR_ENGINE_DARKTABLE_FILMIC) { - return displayHeadroomRolloff(outputColor); - } - return outputColor; - } - - vec3 sampleToneSource(vec2 uv) { - vec3 sampleColor = texture(uInputTexture, clamp(uv, vec2(0.0), vec2(1.0))).rgb; - vec3 color = applyDisplayTone(sampleColor); - color = applyOutputTransformAfterDisplayTone(color); - return color; - } - - vec3 shRgbToXyz(vec3 rgb) { - return mat3( - 0.7976749, 0.2880402, 0.0000000, - 0.1351917, 0.7118741, 0.0000000, - 0.0313534, 0.0000857, 0.8252100 - ) * rgb; - } - - vec3 shXyzToRgb(vec3 xyz) { - return mat3( - 1.3459433, -0.5445989, 0.0000000, - -0.2556075, 1.5081673, 0.0000000, - -0.0511118, 0.0205351, 1.2118128 - ) * xyz; - } - - ${ShadowsHighlightsShader.GLSL} - - void main() { - vec3 color = texture(uInputTexture, vTexCoord).rgb; - color = applyDisplayTone(color); - color = applyOutputTransformAfterDisplayTone(color); - color = applyShadowsHighlights(color, vTexCoord); - color = linearToSrgb(color); - fragColor = vec4(color, 1.0); + vec3 applyEngineTone(vec3 color) { + return displayHeadroomRolloff(uOutputTransform * applyDarktableFilmic(color)); } """.trimIndent()