fix: tone base sampler assignment and placeholder binding

This commit is contained in:
zhoubinjia committed 2026-11-04 16:02:40 +08:00
1 parent c2f942b992
commit 536e60c94f
6 files changed
+122 -19

No files matched your search

@@ -837,8 +837,12 @@ class LutImageProcessor(context: Context? = null) {
GLES30.glGetUniformLocation(program, "uToneBaseOnly"),
if (toneBaseOnly) 1 else 0,
)
if (shadowsHighlightsActive && !toneBaseOnly) {
toneBase.bind(program)
if (shadowsHighlightsActive) {
if (toneBaseOnly) {
toneBase.bindPlaceholders(program)
} else {
toneBase.bind(program)
}
}
logInput.bind(GLES30.glGetUniformLocation(program, "uInverseAcr3Texture"), textureUnit = 7)
@@ -881,8 +881,12 @@ class LutRenderer(context: Context) : GLSurfaceView.Renderer {
}
colorProgramCache.bindLogInput(locations)
enginePreview.bind(locations.uEnginePreviewLutLocation)
if (shadowsHighlightsActive && !toneBaseOnly) {
toneBase.bind(locations.programId)
if (shadowsHighlightsActive) {
if (toneBaseOnly) {
toneBase.bindPlaceholders(locations.programId)
} else {
toneBase.bind(locations.programId)
}
}
GLES30.glActiveTexture(GLES30.GL_TEXTURE0)
@@ -484,8 +484,12 @@ class RealtimeVideoRenderer(
GLES30.glUseProgram(locations.programId)
GLES30.glUniform1i(locations.uToneBaseOnlyLocation, if (toneBaseOnly) 1 else 0)
colorProgramCache.bindLogInput(locations)
if (shadowsHighlightsActive && !toneBaseOnly) {
toneBase.bind(locations.programId)
if (shadowsHighlightsActive) {
if (toneBaseOnly) {
toneBase.bindPlaceholders(locations.programId)
} else {
toneBase.bind(locations.programId)
}
}
GLES30.glActiveTexture(GLES30.GL_TEXTURE0)
@@ -33,6 +33,7 @@ internal class ToneBaseGl(private val tag: String) {
private var statsFailed = false
private var statsTexture = 0
private var statsFramebuffer = 0
private var placeholderTexture = 0
private val uniformLocations = mutableMapOf<Int, Int>()
/** Size of level 0. Returns false when the target cannot be allocated. */
@@ -94,25 +95,58 @@ internal class ToneBaseGl(private val tag: String) {
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, 0)
}
/** Binds the 1x1 statistics texture as `uToneStats` (unit 12). */
private fun bindStats(program: Int) {
if (statsTexture == 0) return
GLES30.glActiveTexture(GLES30.GL_TEXTURE0 + STATS_TEXTURE_UNIT)
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, statsTexture)
/**
* Assigns the sampler units without binding the bases.
*
* The host draws level 0 with the same colour program it later uses for the adjusted frame, so
* a program can be sampled before any base exists. A freshly linked program leaves its sampler
* uniforms at the GL default of unit 0, where hosts bind the external camera texture: a
* `sampler2D` reading an external target raises `GL_INVALID_OPERATION` on Adreno, and the
* driver validates every active sampler at draw time whether or not the shader's `uToneBaseOnly`
* branch would read it. The units are therefore part of the program contract and are always
* assigned together with the textures, never by the texture binding alone.
*/
fun bindSamplerUnits(program: Int) {
val units = ShadowsHighlightsShader.TONE_BASE_TEXTURE_UNITS
for (index in 0 until LEVEL_COUNT) {
GLES30.glUniform1i(location(program, "uToneBase${index + 1}"), units[index])
}
GLES30.glUniform1i(location(program, "uToneStats"), STATS_TEXTURE_UNIT)
}
/** Binds levels 0..2 as `uToneBase1..3` on the units declared by the shader object. */
/**
* Binds a 1x1 placeholder on every tone-base unit for the level-0 pass.
*
* That pass writes the target the first base texture aliases, so the bases cannot be bound
* there; leaving the units on whatever the previous frame left behind would either sample the
* render target or an unrelated 2D texture. The shader returns before reading them.
*/
fun bindPlaceholders(program: Int) {
bindSamplerUnits(program)
val placeholder = ensurePlaceholderTarget()
if (placeholder == 0) return
for (unit in ShadowsHighlightsShader.TONE_BASE_TEXTURE_UNITS) {
GLES30.glActiveTexture(GLES30.GL_TEXTURE0 + unit)
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, placeholder)
}
GLES30.glActiveTexture(GLES30.GL_TEXTURE0 + STATS_TEXTURE_UNIT)
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, placeholder)
GLES30.glActiveTexture(GLES30.GL_TEXTURE0)
}
/** Binds levels 0..2 as `uToneBase1..3` and the 1x1 statistics texture as `uToneStats`. */
fun bind(program: Int) {
val units = ShadowsHighlightsShader.TONE_BASE_TEXTURE_UNITS
bindSamplerUnits(program)
for (index in 0 until LEVEL_COUNT) {
val level = levels[index]
val unit = units[index]
GLES30.glActiveTexture(GLES30.GL_TEXTURE0 + unit)
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, level.texture)
GLES30.glUniform1i(location(program, "uToneBase${index + 1}"), unit)
GLES30.glActiveTexture(GLES30.GL_TEXTURE0 + units[index])
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, levels[index].texture)
}
bindStats(program)
GLES30.glActiveTexture(GLES30.GL_TEXTURE0 + STATS_TEXTURE_UNIT)
GLES30.glBindTexture(
GLES30.GL_TEXTURE_2D,
if (statsTexture != 0) statsTexture else ensurePlaceholderTarget(),
)
GLES30.glActiveTexture(GLES30.GL_TEXTURE0)
}
@@ -128,6 +162,7 @@ internal class ToneBaseGl(private val tag: String) {
level.height = 0
}
releaseStatsTarget()
releasePlaceholderTarget()
if (program != 0) {
GLES30.glDeleteProgram(program)
program = 0
@@ -152,6 +187,7 @@ internal class ToneBaseGl(private val tag: String) {
statsProgram = 0
statsTexture = 0
statsFramebuffer = 0
placeholderTexture = 0
uniformLocations.clear()
programFailed = false
statsFailed = false
@@ -184,6 +220,39 @@ internal class ToneBaseGl(private val tag: String) {
drawQuad(activeProgram)
}
/**
* Complete 1x1 2D texture kept only so the tone-base units always reference a valid sampler
* target. Its contents are never read.
*
* Creation restores the 2D binding of the current unit, so hosts may call it between their own
* texture bindings.
*/
private fun ensurePlaceholderTarget(): Int {
if (placeholderTexture != 0) return placeholderTexture
val previous = IntArray(1)
GLES30.glGetIntegerv(GLES30.GL_TEXTURE_BINDING_2D, previous, 0)
val textures = IntArray(1)
GLES30.glGenTextures(1, textures, 0)
val texture = textures[0]
if (texture == 0) return 0
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, texture)
GLES30.glTexStorage2D(GLES30.GL_TEXTURE_2D, 1, GLES30.GL_RGBA8, 1, 1)
GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MIN_FILTER, GLES30.GL_NEAREST)
GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_MAG_FILTER, GLES30.GL_NEAREST)
GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_S, GLES30.GL_CLAMP_TO_EDGE)
GLES30.glTexParameteri(GLES30.GL_TEXTURE_2D, GLES30.GL_TEXTURE_WRAP_T, GLES30.GL_CLAMP_TO_EDGE)
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, previous[0])
placeholderTexture = texture
return texture
}
private fun releasePlaceholderTarget() {
if (placeholderTexture != 0) {
GLES30.glDeleteTextures(1, intArrayOf(placeholderTexture), 0)
placeholderTexture = 0
}
}
private fun ensureStatsTarget(): Boolean {
if (statsTexture != 0 && statsFramebuffer != 0) return true
val textures = IntArray(1)
@@ -69,7 +69,9 @@ internal class RawAdjustmentPass(
GLES30.glGetUniformLocation(activeProgram, "uToneBaseOnly"),
if (toneBaseOnly) 1 else 0,
)
if (!toneBaseOnly) {
if (toneBaseOnly) {
toneBase.bindPlaceholders(activeProgram)
} else {
toneBase.bind(activeProgram)
}
GLES30.glActiveTexture(GLES30.GL_TEXTURE0)
+20
View File
@@ -20,6 +20,7 @@
| MRT attachment 残留 | 已确认,Adreno | render-target 数减少时显式 detach 尾部 attachment |
| `uint16 -> float` | 已确认,Mali | 首次转换走 compute image load/store,不走 fragment sampler 直接转换 |
| 纹理/sampler 状态 | 跨驱动约束 | 每次 draw 显式绑定 program、framebuffer、texture unit 和所有 sampler |
| sampler 未赋值落 unit 0 | 已确认,Adreno 840 | 首个 draw 前显式赋 unit;不能绑定真实纹理时用 1x1 占位,避免读到 unit 0 的外部纹理 |
| 浮点纹理 sampler 精度 | 规范约束 | 线性 RGB 与浮点曲线/增益纹理显式声明 `highp` sampler;`highp float` 不覆盖 sampler |
| `imageStore` 可见性 | 跨驱动约束 | barrier 必须覆盖下一位 consumer,而非只覆盖 producer |
| CPU/GPU Float 除法 | 已确认,Adreno;跨驱动精度约束 | CPU 算法迁到 GPU 时不能假定 `/` 与 JVM Float 逐位一致 |
@@ -166,6 +167,25 @@ shader storage block binding gets value 12, out of range [0 - 7]
- 保持 pass 间颜色空间契约;线性 RGB 不能未经输出转换直接显示或传给要求 sRGB 的 LUT。
- 不依赖 helper 调用顺序或上一帧遗留的 active unit、framebuffer、program、texture binding。
### 未赋值 sampler 落在 unit 0:`sampler2D` 读到外部纹理
**证据**:PMA110 / Adreno 840 / GLES 3.2 V@0842.44。
local Laplacian 高光/阴影的金字塔复用颜色 program:先用该 program 渲染 level 0,再绑定金字塔纹理
渲染调整后的帧。新链接的 program 里 `uToneBase1..3`、`uToneStats` 仍是 GL 默认的 unit 0,而 unit 0
上绑的是相机 `GL_TEXTURE_EXTERNAL_OES`,于是 level-0 pass 的 `glDrawElements` 报
`GL_INVALID_OPERATION (1282)`。shader 在该 pass 由 `uToneBaseOnly` 提前 return、根本不采样这些
sampler,驱动仍按 active sampler 校验对应 unit 上的目标类型。错误只出现在该 program 的第一次
draw:之后 sampler 已被赋到真实 unit,稳态帧干净,因此容易被误判为“偶发”。
实现契约:
- sampler 的 unit 归属与纹理绑定放在同一处,并在任何 draw 之前显式赋值,不依赖 GL 默认值 0。
- 某个 pass 不能绑定真实纹理时(例如它正在写入该纹理),绑定 1x1 占位纹理,避免采样 render target
或上一帧遗留的绑定。
- 颜色 program 变体多,首个 draw 常常正是“辅助 sampler 尚未赋值”的 pass;验证要按 program 变体
检查首个 draw 的 GL error,只看稳态帧会漏掉。
## SSBO 与 image format
### 线性浮点纹理的 sampler 精度