First of all, you must instead of GLES31.glTexImage2D
create immutable texture to be able a compute shader to write on it:
GLES31.glTexStorage2D(
GLES31.GL_TEXTURE_2D,
1, // 1 level
GLES31.GL_RGBA32F,
1, // Texture width
1, // Texture height
)
Secondly, referring to this answer https://stackoverflow.com/a/53993894/7167920 ES version of OpenGL does not support using texture image data directly, you should use FrameBuffer.
Initialize it when you init:
frameBuffers = IntArray(1)
GLES20.glGenFramebuffers(1, frameBuffers, 0)
And here the working code of readAndLogPixelFromTexture()
:
checkGLError { GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, frameBuffers[0]) }
checkGLError {
GLES31.glFramebufferTexture2D(
GLES31.GL_FRAMEBUFFER,
GLES31.GL_COLOR_ATTACHMENT0,
GLES31.GL_TEXTURE_2D,
textures[texture],
0
)
}
if (GLES31.glCheckFramebufferStatus(GLES31.GL_FRAMEBUFFER) != GLES31.GL_FRAMEBUFFER_COMPLETE) {
throw RuntimeException("Framebuffer is not complete")
}
val pixelData = FloatArray(4)
GLES31.glReadPixels(pixel.first, pixel.second, 1, 1, GLES31.GL_RGBA, GLES31.GL_FLOAT, FloatBuffer.wrap(pixelData))
checkGLError { GLES31.glBindFramebuffer(GLES31.GL_FRAMEBUFFER, 0) }
// Log the color value to verify it
val r = pixelData[0]
val g = pixelData[1]
val b = pixelData[2]
val a = pixelData[3]
Log.d(TAG, "Compute shader result: R: $r, G: $g, B: $b, A: $a")