Skip to content

Commit

Permalink
gpu/internal/opengl: avoid PACK/UNPACK_ROW_LENGTH on WebGL 1
Browse files Browse the repository at this point in the history
Fixes gio#259

Signed-off-by: Elias Naur <[email protected]>
  • Loading branch information
eliasnaur committed Sep 5, 2021
1 parent 0403b1f commit bde046d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
17 changes: 14 additions & 3 deletions gpu/internal/opengl/opengl.go
Original file line number Diff line number Diff line change
Expand Up @@ -1200,8 +1200,14 @@ func (f *framebuffer) ReadPixels(src image.Rectangle, pixels []byte, stride int)
if len(pixels) < src.Dx()*src.Dy()*4 {
return errors.New("unexpected RGBA size")
}
f.backend.glstate.pixelStorei(f.backend.funcs, gl.PACK_ROW_LENGTH, stride/4)
f.backend.funcs.ReadPixels(src.Min.X, src.Min.Y, src.Dx(), src.Dy(), gl.RGBA, gl.UNSIGNED_BYTE, pixels)
w, h := src.Dx(), src.Dy()
// WebGL 1 doesn't support PACK_ROW_LENGTH != 0. Avoid it if possible.
rowLen := 0
if n := stride / 4; n != w {
rowLen = n
}
f.backend.glstate.pixelStorei(f.backend.funcs, gl.PACK_ROW_LENGTH, rowLen)
f.backend.funcs.ReadPixels(src.Min.X, src.Min.Y, w, h, gl.RGBA, gl.UNSIGNED_BYTE, pixels)
return glErr(f.backend.funcs)
}

Expand Down Expand Up @@ -1262,7 +1268,12 @@ func (t *texture) Upload(offset, size image.Point, pixels []byte, stride int) {
panic(fmt.Errorf("size %d larger than data %d", min, len(pixels)))
}
t.backend.BindTexture(0, t)
t.backend.glstate.pixelStorei(t.backend.funcs, gl.UNPACK_ROW_LENGTH, stride/4)
// WebGL 1 doesn't support UNPACK_ROW_LENGTH != 0. Avoid it if possible.
rowLen := 0
if n := stride / 4; n != size.X {
rowLen = n
}
t.backend.glstate.pixelStorei(t.backend.funcs, gl.UNPACK_ROW_LENGTH, rowLen)
t.backend.funcs.TexSubImage2D(gl.TEXTURE_2D, 0, offset.X, offset.Y, size.X, size.Y, t.triple.format, t.triple.typ, pixels)
}

Expand Down
6 changes: 6 additions & 0 deletions internal/gl/gl_js.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,12 @@ func (f *Functions) GetBindingi(pname Enum, idx int) Object {
return Object(obj)
}
func (f *Functions) GetInteger(pname Enum) int {
if !f.isWebGL2 {
switch pname {
case PACK_ROW_LENGTH, UNPACK_ROW_LENGTH:
return 0 // PACK_ROW_LENGTH and UNPACK_ROW_LENGTH is only available on WebGL 2
}
}
return paramVal(f.Ctx.Call("getParameter", int(pname)))
}
func (f *Functions) GetFloat(pname Enum) float32 {
Expand Down

0 comments on commit bde046d

Please sign in to comment.