feat: optimize lut rendering with float16 and remove hlg10 support

This commit is contained in:
zhoubinjia committed 2026-04-10 17:05:04 +08:00
1 parent 51cf975861
commit a34eb765c2
6 files changed
+48 -64

No files matched your search

+2 -1
View File
@@ -4,7 +4,8 @@
"Bash(wc -l:*)",
"Bash(./gradlew :app:compileDefaultDebugKotlin)",
"Bash(./gradlew :app:compileDebugKotlin)",
"Bash(grep:*)"
"Bash(grep:*)",
"Bash(./gradlew compileDebugKotlin)"
]
}
}
@@ -202,12 +202,6 @@ class Camera2Controller(private val context: Context) {
// canRetry: 是否可以重试打开相机
var onCameraError: ((errorCode: Int, message: String, canRetry: Boolean) -> Unit)? = null
private val linearToneMapCurve = TonemapCurve(
floatArrayOf(0f, 0f, 1f, 1f),
floatArrayOf(0f, 0f, 1f, 1f),
floatArrayOf(0f, 0f, 1f, 1f)
)
fun onImageRelease() {
val count = openImagesCount.decrementAndGet()
if (MAX_IMAGES - count >= state.value.multiFrameCount) {
@@ -1229,34 +1223,19 @@ class Camera2Controller(private val context: Context) {
}
private fun applyToneMapSettings(builder: CaptureRequest.Builder, state: CameraState, isCapture: Boolean) {
if (state.captureMode != CaptureMode.VIDEO || !state.videoConfig.logProfile.isEnabled) {
val preferredTonemapMode = when {
isCapture && state.captureMode == CaptureMode.PHOTO &&
val preferredTonemapMode = when {
isCapture && state.captureMode == CaptureMode.PHOTO &&
availableTonemapModes.contains(CaptureRequest.TONEMAP_MODE_HIGH_QUALITY) -> {
CaptureRequest.TONEMAP_MODE_HIGH_QUALITY
}
availableTonemapModes.contains(CaptureRequest.TONEMAP_MODE_FAST) -> {
CaptureRequest.TONEMAP_MODE_FAST
}
else -> null
}
preferredTonemapMode?.let { builder.set(CaptureRequest.TONEMAP_MODE, it) }
return
}
when {
availableTonemapModes.contains(CaptureRequest.TONEMAP_MODE_CONTRAST_CURVE) -> {
builder.set(CaptureRequest.TONEMAP_MODE, CaptureRequest.TONEMAP_MODE_CONTRAST_CURVE)
builder.set(CaptureRequest.TONEMAP_CURVE, linearToneMapCurve)
}
availableTonemapModes.contains(CaptureRequest.TONEMAP_MODE_GAMMA_VALUE) -> {
builder.set(CaptureRequest.TONEMAP_MODE, CaptureRequest.TONEMAP_MODE_GAMMA_VALUE)
builder.set(CaptureRequest.TONEMAP_GAMMA, 1.0f)
CaptureRequest.TONEMAP_MODE_HIGH_QUALITY
}
availableTonemapModes.contains(CaptureRequest.TONEMAP_MODE_FAST) -> {
CaptureRequest.TONEMAP_MODE_FAST
}
else -> null
}
preferredTonemapMode?.let { builder.set(CaptureRequest.TONEMAP_MODE, it) }
}
/**
@@ -590,8 +590,8 @@ class LutRenderer : GLSurfaceView.Renderer {
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, fboTextureId)
GLES30.glTexImage2D(
GLES30.GL_TEXTURE_2D, 0, GLES30.GL_RGBA, width, height,
0, GLES30.GL_RGBA, GLES30.GL_UNSIGNED_BYTE, null
GLES30.GL_TEXTURE_2D, 0, GLES30.GL_RGBA16F, width, height,
0, GLES30.GL_RGBA, GLES30.GL_HALF_FLOAT, null
)
GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MIN_FILTER, GLES30.GL_LINEAR)
GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MAG_FILTER, GLES30.GL_LINEAR)
@@ -618,8 +618,8 @@ class LutRenderer : GLSurfaceView.Renderer {
stackTextureId = ids[0]
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, stackTextureId)
GLES30.glTexImage2D(
GLES30.GL_TEXTURE_2D, 0, GLES30.GL_RGBA, width, height,
0, GLES30.GL_RGBA, GLES30.GL_UNSIGNED_BYTE, null
GLES30.GL_TEXTURE_2D, 0, GLES30.GL_RGBA16F, width, height,
0, GLES30.GL_RGBA, GLES30.GL_HALF_FLOAT, null
)
GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MIN_FILTER, GLES30.GL_LINEAR)
GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MAG_FILTER, GLES30.GL_LINEAR)
@@ -1402,12 +1402,12 @@ class LutRenderer : GLSurfaceView.Renderer {
GLES30.glTexImage2D(
GLES30.GL_TEXTURE_2D,
0,
GLES30.GL_RGBA,
GLES30.GL_RGBA16F,
dsW,
dsH,
0,
GLES30.GL_RGBA,
GLES30.GL_UNSIGNED_BYTE,
GLES30.GL_HALF_FLOAT,
null
)
GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MIN_FILTER, GLES30.GL_LINEAR)
@@ -1637,8 +1637,8 @@ class LutRenderer : GLSurfaceView.Renderer {
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, recordTextureId)
GLES30.glTexImage2D(
GLES30.GL_TEXTURE_2D, 0, GLES30.GL_RGBA,
width, height, 0, GLES30.GL_RGBA, GLES30.GL_UNSIGNED_BYTE, null
GLES30.GL_TEXTURE_2D, 0, GLES30.GL_RGBA16F,
width, height, 0, GLES30.GL_RGBA, GLES30.GL_HALF_FLOAT, null
)
GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MIN_FILTER, GLES30.GL_LINEAR)
GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MAG_FILTER, GLES30.GL_LINEAR)
@@ -2634,7 +2634,7 @@ class LutRenderer : GLSurfaceView.Renderer {
bokehTextureId = tex[0]
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, bokehTextureId)
GLES30.glTexImage2D(GLES30.GL_TEXTURE_2D, 0, GLES30.GL_RGBA, width, height, 0, GLES30.GL_RGBA, GLES30.GL_UNSIGNED_BYTE, null)
GLES30.glTexImage2D(GLES30.GL_TEXTURE_2D, 0, GLES30.GL_RGBA16F, width, height, 0, GLES30.GL_RGBA, GLES30.GL_HALF_FLOAT, null)
GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MIN_FILTER, GLES30.GL_LINEAR)
GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MAG_FILTER, GLES30.GL_LINEAR)
GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_S, GLES30.GL_CLAMP_TO_EDGE)
@@ -269,15 +269,16 @@ object Shaders {
}
vec3 applyExposureInLinearSpace(vec3 srgbColor, float exposureEv) {
vec3 linearColor = srgbToLinear(clamp(srgbColor, 0.0, 1.0));
vec3 linearColor = srgbToLinear(max(srgbColor, vec3(0.0)));
linearColor *= exp2(exposureEv);
return linearToSrgb(linearColor);
}
// NaN 保护
float sanitizeFloat(float value) {
if (value != value) return 0.0;
if (value > 1.0) return 1.0;
if (value < 0.0) return 0.0;
// if (value > 1.0) return 1.0;
// if (value < 0.0) return 0.0;
return value;
}
@@ -314,14 +315,14 @@ object Shaders {
if (abs(toe) < 0.001 && abs(shoulder) < 0.001 && abs(pivot) < 0.001) {
return color;
}
vec3 safeColor = clamp(color, 0.0, 1.0);
vec3 safeColor = clamp(color, 0.0, 1.0); // 曲线数学需要 [0,1] 输入
float luma = dot(safeColor, W);
float curvedLuma = applyToneCurveToLuma(luma, toe, shoulder, pivot);
if (luma < 0.0001) {
return safeColor;
}
vec3 scaled = safeColor * (curvedLuma / luma);
return clamp(mix(vec3(curvedLuma), scaled, 0.92), 0.0, 1.0);
return mix(vec3(curvedLuma), scaled, 0.92);
}
bool isLogLutCurve(int curveType) {
@@ -329,8 +330,8 @@ object Shaders {
}
vec3 bt709Gamma24ToSrgb(vec3 gammaColor) {
vec3 linearColor = pow(clamp(gammaColor, 0.0, 1.0), vec3(2.4));
return clamp(linearToSrgb(linearColor), 0.0, 1.0);
vec3 linearColor = pow(max(gammaColor, vec3(0.0)), vec3(2.4));
return linearToSrgb(linearColor);
}
vec3 linearRgbToOklab(vec3 c) {
@@ -391,7 +392,7 @@ object Shaders {
return srgbColor;
}
vec3 linearColor = srgbToLinear(clamp(srgbColor, 0.0, 1.0));
vec3 linearColor = srgbToLinear(max(srgbColor, vec3(0.0)));
vec3 lab = linearRgbToOklab(linearColor);
float chroma = length(lab.yz);
float hue = atan(lab.z, lab.y);
@@ -399,14 +400,14 @@ object Shaders {
float densityScale = max(0.0, 1.0 + density * CHROMA_BIAS);
float newChroma = chroma * densityScale;
const float DENSITY_K = 1.85;
float newLightness = clamp(lab.x * exp(-DENSITY_K * density * chroma), 0.0, 1.0);
float newLightness = clamp(lab.x * exp(-DENSITY_K * density * chroma), 0.0, 1.0); // OkLab L ∈ [0,1]
vec3 denseLab = vec3(newLightness, cos(hue) * newChroma, sin(hue) * newChroma);
vec3 denseLinear = max(oklabToLinearRgb(denseLab), vec3(0.0));
return clamp(linearToSrgb(denseLinear), 0.0, 1.0);
return linearToSrgb(denseLinear);
}
vec3 applyLchColorMixer(vec3 srgbColor) {
vec3 linearColor = srgbToLinear(clamp(srgbColor, 0.0, 1.0));
vec3 linearColor = srgbToLinear(max(srgbColor, vec3(0.0)));
vec3 lab = linearRgbToOklab(linearColor);
float chroma = length(lab.yz);
float hue = atan(lab.z, lab.y);
@@ -462,10 +463,10 @@ object Shaders {
float newHue = hue + hueShift;
float newChroma = max(0.0, chroma * max(0.0, chromaScale));
float newLightness = clamp(lab.x + lightnessShift, 0.0, 1.0);
float newLightness = clamp(lab.x + lightnessShift, 0.0, 1.0); // OkLab L ∈ [0,1]
vec3 mixedLab = vec3(newLightness, cos(newHue) * newChroma, sin(newHue) * newChroma);
vec3 mixedLinear = max(oklabToLinearRgb(mixedLab), vec3(0.0));
return clamp(linearToSrgb(mixedLinear), 0.0, 1.0);
return linearToSrgb(mixedLinear);
}
vec3 applyPrimaryCalibration(vec3 color) {
@@ -513,7 +514,7 @@ object Shaders {
float bL = uPrimaryLightness.z * 0.5;
vec3 light_add = vec3(rgb.r * rL + rgb.g * gL + rgb.b * bL);
return clamp(vec3(minC) + mixed + light_add, 0.0, 1.0);
return vec3(minC) + mixed + light_add;
}
vec3 applyLutCurve(vec3 l, int curveType) {
@@ -767,20 +768,22 @@ object Shaders {
// === 曲线调整(色彩配方之后、LUT 之前) ===
if (uCurveEnabled) {
float r = texture(uCurveTexture, vec2(color.r, 0.5)).r;
float g = texture(uCurveTexture, vec2(color.g, 0.5)).g;
float b = texture(uCurveTexture, vec2(color.b, 0.5)).b;
vec3 clamped = clamp(color.rgb, 0.0, 1.0); // 纹理坐标需要 [0,1]
float r = texture(uCurveTexture, vec2(clamped.r, 0.5)).r;
float g = texture(uCurveTexture, vec2(clamped.g, 0.5)).g;
float b = texture(uCurveTexture, vec2(clamped.b, 0.5)).b;
color.rgb = sanitizeColor(vec3(r, g, b));
}
if (uVideoLogEnabled) {
vec3 outputColorSpace = applyLutColorSpace(color.rgb, uVideoColorSpace);
vec3 linearColor = srgbToLinear(max(color.rgb, vec3(0.0)));
vec3 outputColorSpace = applyLutColorSpace(linearColor, uVideoColorSpace);
color.rgb = sanitizeColor(applyLutCurve(outputColorSpace, uVideoLogCurve));
}
// === LUT 处理(在色彩配方之后) ===
if (!uVideoLogEnabled && uLutEnabled && uLutIntensity > 0.0) {
vec3 linearRGB = srgbToLinear(color.rgb);
vec3 linearRGB = srgbToLinear(max(color.rgb, vec3(0.0)));
vec3 colorSpaceRGB = applyLutColorSpace(linearRGB, uLutColorSpace);
vec3 lutInColor = applyLutCurve(colorSpaceRGB, uLutCurve);
float scale = (uLutSize - 1.0) / uLutSize;
@@ -803,7 +806,7 @@ object Shaders {
color.rgb = sanitizeColor(color.rgb);
}
fragColor = vec4(sanitizeColor(color.rgb), color.a);
fragColor = vec4(clamp(sanitizeColor(color.rgb), 0.0, 1.0), color.a);
}
""".trimIndent()
@@ -908,7 +908,7 @@ fun SettingsScreen(
if (isHdrSettingsSupported) {
if (state.isHlg10Supported) {
/*if (state.isHlg10Supported) {
HorizontalDivider(
color = Color.White.copy(alpha = 0.1f),
modifier = Modifier.padding(vertical = 12.dp)
@@ -920,7 +920,7 @@ fun SettingsScreen(
checked = useHlg10,
onCheckedChange = { viewModel.setUseHlg10(it) }
)
}
}*/
HorizontalDivider(
color = Color.White.copy(alpha = 0.1f),
@@ -484,7 +484,8 @@ class CameraViewModel(application: Application) : AndroidViewModel(application)
// 同步 P010 设置到相机控制器
cameraController.setUseP010(it.useP010)
// 同步 HLG10 设置到相机控制器
cameraController.setUseHlg10(it.useHlg10)
// cameraController.setUseHlg10(it.useHlg10)
cameraController.setUseHlg10(false)
// 同步 P3 色域设置到相机控制器
cameraController.setUseP3ColorSpace(it.useP3ColorSpace)
}