graphicsdriver/opengl: Refactoring

This commit is contained in:
Hajime Hoshi 2020-01-01 01:57:12 +09:00
parent 815afe6670
commit 51f4ba1320
3 changed files with 19 additions and 49 deletions

View File

@ -112,11 +112,7 @@ func (i *Image) ReplacePixels(args []*driver.ReplacePixelsArgs) {
return return
} }
thePBOState.mapPBO(i) drawPixelsWithPBO(i, args)
for _, a := range args {
thePBOState.draw(a.Pixels, a.X, a.Y, a.Width, a.Height)
}
thePBOState.unmapPBO()
} }
func (i *Image) SetAsSource() { func (i *Image) SetAsSource() {

View File

@ -22,19 +22,13 @@ package opengl
import ( import (
"reflect" "reflect"
"unsafe" "unsafe"
"github.com/hajimehoshi/ebiten/internal/driver"
) )
const canUsePBO = true const canUsePBO = true
type pboState struct { func drawPixelsWithPBO(img *Image, args []*driver.ReplacePixelsArgs) {
image *Image
mappedPBO uintptr
mapped []byte
}
var thePBOState pboState
func (s *pboState) mapPBO(img *Image) {
w, h := img.width, img.height w, h := img.width, img.height
if img.pbo == *new(buffer) { if img.pbo == *new(buffer) {
img.pbo = img.driver.context.newPixelBufferObject(w, h) img.pbo = img.driver.context.newPixelBufferObject(w, h)
@ -43,35 +37,23 @@ func (s *pboState) mapPBO(img *Image) {
panic("opengl: newPixelBufferObject failed") panic("opengl: newPixelBufferObject failed")
} }
s.image = img mappedPBO := img.driver.context.mapPixelBuffer(img.pbo)
s.mappedPBO = img.driver.context.mapPixelBuffer(img.pbo) if mappedPBO == 0 {
if s.mappedPBO == 0 {
panic("opengl: mapPixelBuffer failed") panic("opengl: mapPixelBuffer failed")
} }
var mapped []byte var mapped []byte
sh := (*reflect.SliceHeader)(unsafe.Pointer(&mapped)) sh := (*reflect.SliceHeader)(unsafe.Pointer(&mapped))
sh.Data = s.mappedPBO sh.Data = mappedPBO
sh.Len = 4 * w * h sh.Len = 4 * w * h
sh.Cap = 4 * w * h sh.Cap = 4 * w * h
s.mapped = mapped
}
func (s *pboState) draw(pix []byte, x, y, width, height int) { for _, a := range args {
w := s.image.width stride := 4 * w
stride := 4 * w offset := 4 * (a.Y*w + a.X)
offset := 4 * (y*w + x) for j := 0; j < a.Height; j++ {
for j := 0; j < height; j++ { copy(mapped[offset+stride*j:offset+stride*j+4*a.Width], a.Pixels[4*a.Width*j:4*a.Width*(j+1)])
copy(s.mapped[offset+stride*j:offset+stride*j+4*width], pix[4*width*j:4*width*(j+1)]) }
} }
} img.driver.context.unmapPixelBuffer(img.pbo, img.textureNative, w, h)
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
} }

View File

@ -16,20 +16,12 @@
package opengl package opengl
import (
"github.com/hajimehoshi/ebiten/internal/driver"
)
const canUsePBO = false const canUsePBO = false
type pboState struct{} func drawPixelsWithPBO(img *Image, args []*driver.ReplacePixelsArgs) {
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() {
panic("opengl: PBO is not available in this environment") panic("opengl: PBO is not available in this environment")
} }