From 51f4ba13207e0cd7a04e6ad00e1953a9b70bd23a Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Wed, 1 Jan 2020 01:57:12 +0900 Subject: [PATCH] graphicsdriver/opengl: Refactoring --- internal/graphicsdriver/opengl/image.go | 6 +-- internal/graphicsdriver/opengl/pbo_desktop.go | 44 ++++++------------- .../graphicsdriver/opengl/pbo_notdesktop.go | 18 +++----- 3 files changed, 19 insertions(+), 49 deletions(-) diff --git a/internal/graphicsdriver/opengl/image.go b/internal/graphicsdriver/opengl/image.go index 5e07bc450..0edf0c69a 100644 --- a/internal/graphicsdriver/opengl/image.go +++ b/internal/graphicsdriver/opengl/image.go @@ -112,11 +112,7 @@ func (i *Image) ReplacePixels(args []*driver.ReplacePixelsArgs) { return } - thePBOState.mapPBO(i) - for _, a := range args { - thePBOState.draw(a.Pixels, a.X, a.Y, a.Width, a.Height) - } - thePBOState.unmapPBO() + drawPixelsWithPBO(i, args) } func (i *Image) SetAsSource() { diff --git a/internal/graphicsdriver/opengl/pbo_desktop.go b/internal/graphicsdriver/opengl/pbo_desktop.go index 42623c233..51960f1a7 100644 --- a/internal/graphicsdriver/opengl/pbo_desktop.go +++ b/internal/graphicsdriver/opengl/pbo_desktop.go @@ -22,19 +22,13 @@ package opengl import ( "reflect" "unsafe" + + "github.com/hajimehoshi/ebiten/internal/driver" ) const canUsePBO = true -type pboState struct { - image *Image - mappedPBO uintptr - mapped []byte -} - -var thePBOState pboState - -func (s *pboState) mapPBO(img *Image) { +func drawPixelsWithPBO(img *Image, args []*driver.ReplacePixelsArgs) { w, h := img.width, img.height if img.pbo == *new(buffer) { img.pbo = img.driver.context.newPixelBufferObject(w, h) @@ -43,35 +37,23 @@ func (s *pboState) mapPBO(img *Image) { panic("opengl: newPixelBufferObject failed") } - s.image = img - s.mappedPBO = img.driver.context.mapPixelBuffer(img.pbo) - - if s.mappedPBO == 0 { + mappedPBO := img.driver.context.mapPixelBuffer(img.pbo) + if mappedPBO == 0 { panic("opengl: mapPixelBuffer failed") } var mapped []byte sh := (*reflect.SliceHeader)(unsafe.Pointer(&mapped)) - sh.Data = s.mappedPBO + sh.Data = mappedPBO sh.Len = 4 * w * h sh.Cap = 4 * w * h - s.mapped = mapped -} -func (s *pboState) draw(pix []byte, x, y, width, height int) { - w := s.image.width - stride := 4 * w - offset := 4 * (y*w + x) - for j := 0; j < height; j++ { - copy(s.mapped[offset+stride*j:offset+stride*j+4*width], pix[4*width*j:4*width*(j+1)]) + for _, a := range args { + stride := 4 * w + offset := 4 * (a.Y*w + a.X) + for j := 0; j < a.Height; j++ { + copy(mapped[offset+stride*j:offset+stride*j+4*a.Width], a.Pixels[4*a.Width*j:4*a.Width*(j+1)]) + } } -} - -func (s *pboState) unmapPBO() { - i := s.image - i.driver.context.unmapPixelBuffer(i.pbo, i.textureNative, i.width, i.height) - - s.image = nil - s.mappedPBO = 0 - s.mapped = nil + img.driver.context.unmapPixelBuffer(img.pbo, img.textureNative, w, h) } diff --git a/internal/graphicsdriver/opengl/pbo_notdesktop.go b/internal/graphicsdriver/opengl/pbo_notdesktop.go index c6b132f0a..f6f73a12a 100644 --- a/internal/graphicsdriver/opengl/pbo_notdesktop.go +++ b/internal/graphicsdriver/opengl/pbo_notdesktop.go @@ -16,20 +16,12 @@ package opengl +import ( + "github.com/hajimehoshi/ebiten/internal/driver" +) + const canUsePBO = false -type pboState struct{} - -var thePBOState pboState - -func (s *pboState) mapPBO(img *Image) { - panic("opengl: PBO is not available in this environment") -} - -func (s *pboState) draw(pix []byte, x, y, width, height int) { - panic("opengl: PBO is not available in this environment") -} - -func (s *pboState) unmapPBO() { +func drawPixelsWithPBO(img *Image, args []*driver.ReplacePixelsArgs) { panic("opengl: PBO is not available in this environment") }