graphicsdriver/opengl: Use glBufferSubData instead of glMapBuffer

We have confirmed that this does not slow down ReplacePixels.

Fixes #993
Fixes #1040
This commit is contained in:
Hajime Hoshi 2020-01-01 02:24:18 +09:00
parent d2251c42c3
commit 38815ba801
2 changed files with 12 additions and 36 deletions

View File

@ -548,22 +548,19 @@ func (c *context) newPixelBufferObject(width, height int) buffer {
return bf return bf
} }
func (c *context) mapPixelBuffer(buffer buffer) uintptr { func (c *context) replacePixelsWithPBO(buffer buffer, t textureNative, width, height int, args []*driver.ReplacePixelsArgs) {
var ptr uintptr
_ = c.t.Call(func() error {
gl.BindBuffer(gl.PIXEL_UNPACK_BUFFER, uint32(buffer))
// Even though the buffer is partly updated, GL_WRITE_ONLY is fine.
// https://stackoverflow.com/questions/30248594/write-only-glmapbuffer-what-if-i-dont-write-it-all
ptr = gl.MapBuffer(gl.PIXEL_UNPACK_BUFFER, gl.WRITE_ONLY)
return nil
})
return ptr
}
func (c *context) unmapPixelBuffer(buffer buffer, t textureNative, width, height int) {
c.bindTexture(t) c.bindTexture(t)
_ = c.t.Call(func() error { _ = c.t.Call(func() error {
gl.UnmapBuffer(gl.PIXEL_UNPACK_BUFFER) gl.BindBuffer(gl.PIXEL_UNPACK_BUFFER, uint32(buffer))
stride := 4 * width
for _, a := range args {
offset := 4 * (a.Y*width + a.X)
for j := 0; j < a.Height; j++ {
gl.BufferSubData(gl.PIXEL_UNPACK_BUFFER, offset+stride*j, 4*a.Width, gl.Ptr(a.Pixels[4*a.Width*j:4*a.Width*(j+1)]))
}
}
gl.TexSubImage2D(gl.TEXTURE_2D, 0, 0, 0, int32(width), int32(height), gl.RGBA, gl.UNSIGNED_BYTE, nil) gl.TexSubImage2D(gl.TEXTURE_2D, 0, 0, 0, int32(width), int32(height), gl.RGBA, gl.UNSIGNED_BYTE, nil)
gl.BindBuffer(gl.PIXEL_UNPACK_BUFFER, 0) gl.BindBuffer(gl.PIXEL_UNPACK_BUFFER, 0)
return nil return nil

View File

@ -20,9 +20,6 @@
package opengl package opengl
import ( import (
"reflect"
"unsafe"
"github.com/hajimehoshi/ebiten/internal/driver" "github.com/hajimehoshi/ebiten/internal/driver"
) )
@ -37,23 +34,5 @@ func drawPixelsWithPBO(img *Image, args []*driver.ReplacePixelsArgs) {
panic("opengl: newPixelBufferObject failed") panic("opengl: newPixelBufferObject failed")
} }
mappedPBO := img.driver.context.mapPixelBuffer(img.pbo) img.driver.context.replacePixelsWithPBO(img.pbo, img.textureNative, w, h, args)
if mappedPBO == 0 {
panic("opengl: mapPixelBuffer failed")
}
var mapped []byte
sh := (*reflect.SliceHeader)(unsafe.Pointer(&mapped))
sh.Data = mappedPBO
sh.Len = 4 * w * h
sh.Cap = 4 * w * h
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)])
}
}
img.driver.context.unmapPixelBuffer(img.pbo, img.textureNative, w, h)
} }