fix: gles compatibility

This commit is contained in:
zhoubinjia committed 2026-07-27 12:49:24 +08:00
1 parent 43e4d89fe3
commit b9479a70e5
3 files changed
+178 -4

No files matched your search

@@ -1,5 +1,7 @@
package com.hinnka.mycamera.processor
import android.opengl.GLES30
import android.opengl.GLES31
import androidx.test.ext.junit.runners.AndroidJUnit4
import java.nio.ByteBuffer
import java.nio.ByteOrder
@@ -30,6 +32,7 @@ class GlesRawRadianceStackerShaderTest {
visualizeRadianceFusionRejections = true,
visualizeRadianceSrDetail = true,
),
exportGpuLinearRgbSource = true,
)
try {
invokePrivate(stacker, "initEgl")
@@ -117,13 +120,145 @@ class GlesRawRadianceStackerShaderTest {
)
val chromaOutput = ByteBuffer.allocateDirect(96 * 96 * 3 * 2)
.order(ByteOrder.nativeOrder())
assertNotNull(invokePrivate(postprocessor, "processAndReadback", chromaOutput))
val chromaResult = invokePrivate(
postprocessor,
"processAndReadback",
chromaOutput,
) as GlesRadianceVgnChromaPostprocessor.ProcessResult
assertNotNull(chromaResult)
assertEquals(0, chromaOutput.position())
assertTrue(chromaResult.exportedTextureId != 0)
try {
assertGpuExportVisibleToSampler(
stacker = stacker,
sourceTexture = chromaResult.exportedTextureId,
width = 96,
height = 96,
)
} finally {
GLES30.glDeleteTextures(1, intArrayOf(chromaResult.exportedTextureId), 0)
}
} finally {
invokePrivate(stacker, "release")
}
}
private fun assertGpuExportVisibleToSampler(
stacker: GlesRawRadianceStacker,
sourceTexture: Int,
width: Int,
height: Int,
) {
val copyProgram = invokePrivate(
stacker,
"linkComputeProgram",
"""
#version 310 es
precision highp int;
precision highp usampler2D;
precision highp uimage2D;
layout(local_size_x = 16, local_size_y = 16) in;
uniform highp usampler2D uSource;
layout(rgba16ui, binding = 0) writeonly uniform highp uimage2D uDestination;
uniform ivec2 uImageSize;
void main() {
ivec2 p = ivec2(gl_GlobalInvocationID.xy);
if (any(greaterThanEqual(p, uImageSize))) return;
imageStore(uDestination, p, texelFetch(uSource, p, 0));
}
""".trimIndent(),
"instrumentation_radiance_export_sampler_copy",
) as Int
val textures = IntArray(1)
val framebuffers = IntArray(1)
GLES30.glGenTextures(1, textures, 0)
GLES30.glGenFramebuffers(1, framebuffers, 0)
try {
GLES30.glBindTexture(GLES30.GL_TEXTURE_2D, textures[0])
GLES30.glTexStorage2D(
GLES30.GL_TEXTURE_2D,
1,
GLES30.GL_RGBA16UI,
width,
height,
)
GLES31.glUseProgram(copyProgram)
GLES31.glActiveTexture(GLES31.GL_TEXTURE0)
GLES31.glBindTexture(GLES31.GL_TEXTURE_2D, sourceTexture)
GLES31.glUniform1i(GLES31.glGetUniformLocation(copyProgram, "uSource"), 0)
GLES31.glUniform2i(
GLES31.glGetUniformLocation(copyProgram, "uImageSize"),
width,
height,
)
GLES31.glBindImageTexture(
0,
textures[0],
0,
false,
0,
GLES31.GL_WRITE_ONLY,
GLES30.GL_RGBA16UI,
)
GLES31.glDispatchCompute((width + 15) / 16, (height + 15) / 16, 1)
GLES31.glMemoryBarrier(
GLES31.GL_SHADER_IMAGE_ACCESS_BARRIER_BIT or
GLES31.GL_FRAMEBUFFER_BARRIER_BIT,
)
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, framebuffers[0])
GLES30.glFramebufferTexture2D(
GLES30.GL_FRAMEBUFFER,
GLES30.GL_COLOR_ATTACHMENT0,
GLES30.GL_TEXTURE_2D,
textures[0],
0,
)
GLES30.glReadBuffer(GLES30.GL_COLOR_ATTACHMENT0)
assertEquals(
GLES30.GL_FRAMEBUFFER_COMPLETE,
GLES30.glCheckFramebufferStatus(GLES30.GL_FRAMEBUFFER),
)
val pixel = ByteBuffer.allocateDirect(4 * Short.SIZE_BYTES)
.order(ByteOrder.nativeOrder())
listOf(
0 to 0,
width / 2 to height / 2,
width - 1 to height - 1,
).forEach { (x, y) ->
pixel.clear()
GLES30.glReadPixels(
x,
y,
1,
1,
GLES30.GL_RGBA_INTEGER,
GLES30.GL_UNSIGNED_SHORT,
pixel,
)
assertEquals(
"Radiance GPU export alpha at ($x, $y)",
65535,
pixel.order(ByteOrder.nativeOrder()).asShortBuffer().get(3).toInt() and 0xFFFF,
)
}
} finally {
GLES31.glBindImageTexture(
0,
0,
0,
false,
0,
GLES31.GL_READ_ONLY,
GLES30.GL_RGBA16UI,
)
GLES30.glBindFramebuffer(GLES30.GL_FRAMEBUFFER, 0)
GLES30.glDeleteFramebuffers(1, framebuffers, 0)
GLES30.glDeleteTextures(1, textures, 0)
}
}
private fun invokePrivate(target: Any, methodName: String, vararg args: Any): Any? {
return target.javaClass.declaredMethods.first {
it.name == methodName && it.parameterCount == args.size
@@ -436,7 +436,17 @@ internal class GlesRadianceVgnChromaPostprocessor(
} else {
bindArrayImage(1, destination, GLES31.GL_WRITE_ONLY)
}
dispatchImage(finalProgram, "final camera RGB")
val nextAccessBarrier = if (fullSize) {
// The full-size texture leaves this component and is consumed as both an FBO
// attachment (CPU readback) and a sampler by the persistent RAW renderer. Declaring
// only image access visibility can leave stale texture-cache blocks on some drivers.
GLES31.GL_SHADER_IMAGE_ACCESS_BARRIER_BIT or
GLES31.GL_FRAMEBUFFER_BARRIER_BIT or
GLES31.GL_TEXTURE_FETCH_BARRIER_BIT
} else {
GLES31.GL_SHADER_IMAGE_ACCESS_BARRIER_BIT
}
dispatchImage(finalProgram, "final camera RGB", nextAccessBarrier)
}
private fun runIirRgb(
@@ -515,9 +525,13 @@ internal class GlesRadianceVgnChromaPostprocessor(
GLES31.glUniform4fv(backend.uniformLocation(program, "uBDyn2"), 1, pass.bDyn2, 0)
}
private fun dispatchImage(program: Int, label: String) {
private fun dispatchImage(
program: Int,
label: String,
barrierBits: Int = GLES31.GL_SHADER_IMAGE_ACCESS_BARRIER_BIT,
) {
GLES31.glDispatchCompute(groupCount(imageWidth), groupCount(imageHeight), 1)
GLES31.glMemoryBarrier(GLES31.GL_SHADER_IMAGE_ACCESS_BARRIER_BIT)
GLES31.glMemoryBarrier(barrierBits)
backend.checkGlError("Radiance VGN chroma $label")
backend.yieldToUiRenderer()
}