From 6a14269682189626a2228a8b77c5e9056dc849d4 Mon Sep 17 00:00:00 2001 From: Elias Naur Date: Wed, 13 Apr 2022 17:14:06 +0200 Subject: [PATCH] gpu/internal/opengl: add fallback for sparse OpenGL ES 2.0 ReadPixels OpenGL ES 2.0 doesn't support GL_PACK_ROW_LENGTH, so this change implements a fallback using a temporary buffer. Signed-off-by: Elias Naur --- gpu/internal/opengl/opengl.go | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/gpu/internal/opengl/opengl.go b/gpu/internal/opengl/opengl.go index 8dc92421..73b9fffd 100644 --- a/gpu/internal/opengl/opengl.go +++ b/gpu/internal/opengl/opengl.go @@ -1156,17 +1156,25 @@ func (b *Backend) CopyTexture(dst driver.Texture, dstOrigin image.Point, src dri func (t *texture) ReadPixels(src image.Rectangle, pixels []byte, stride int) error { glErr(t.backend.funcs) t.backend.glstate.bindFramebuffer(t.backend.funcs, gl.FRAMEBUFFER, t.ensureFBO()) - if len(pixels) < src.Dx()*src.Dy()*4 { + w, h := src.Dx(), src.Dy() + if len(pixels) < w*h*4 { return errors.New("unexpected RGBA size") } - w, h := src.Dx(), src.Dy() - // WebGL 1 doesn't support PACK_ROW_LENGTH != 0. Avoid it if possible. + // OpenGL ES 2 doesn't support PACK_ROW_LENGTH != 0. Avoid it if possible. rowLen := 0 if n := stride / 4; n != w { rowLen = n } - t.backend.glstate.pixelStorei(t.backend.funcs, gl.PACK_ROW_LENGTH, rowLen) - t.backend.funcs.ReadPixels(src.Min.X, src.Min.Y, w, h, gl.RGBA, gl.UNSIGNED_BYTE, pixels) + if rowLen == 0 || t.backend.glver[0] > 2 { + t.backend.glstate.pixelStorei(t.backend.funcs, gl.PACK_ROW_LENGTH, rowLen) + t.backend.funcs.ReadPixels(src.Min.X, src.Min.Y, w, h, gl.RGBA, gl.UNSIGNED_BYTE, pixels) + } else { + tmp := make([]byte, w*h*4) + t.backend.funcs.ReadPixels(src.Min.X, src.Min.Y, w, h, gl.RGBA, gl.UNSIGNED_BYTE, tmp) + for y := 0; y < h; y++ { + copy(pixels[y*stride:], tmp[y*w*4:]) + } + } return glErr(t.backend.funcs) }