Skip to content

Commit

Permalink
graphicsdriver/opengl: Use glBufferSubData instead of glMapBuffer
Browse files Browse the repository at this point in the history
We have confirmed that this does not slow down ReplacePixels.

Fixes #993
Fixes #1040
  • Loading branch information
hajimehoshi committed Jan 1, 2020
1 parent d2251c4 commit 38815ba
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 35 deletions.
23 changes: 10 additions & 13 deletions internal/graphicsdriver/opengl/context_desktop.go
Original file line number Diff line number Diff line change
Expand Up @@ -548,22 +548,19 @@ func (c *context) newPixelBufferObject(width, height int) buffer {
return bf
}

func (c *context) mapPixelBuffer(buffer buffer) uintptr {
var ptr uintptr
func (c *context) replacePixelsWithPBO(buffer buffer, t textureNative, width, height int, args []*driver.ReplacePixelsArgs) {
c.bindTexture(t)
_ = 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.t.Call(func() error {
gl.UnmapBuffer(gl.PIXEL_UNPACK_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.BindBuffer(gl.PIXEL_UNPACK_BUFFER, 0)
return nil
Expand Down
23 changes: 1 addition & 22 deletions internal/graphicsdriver/opengl/pbo_desktop.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@
package opengl

import (
"reflect"
"unsafe"

"github.com/hajimehoshi/ebiten/internal/driver"
)

Expand All @@ -37,23 +34,5 @@ func drawPixelsWithPBO(img *Image, args []*driver.ReplacePixelsArgs) {
panic("opengl: newPixelBufferObject failed")
}

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 = 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)
img.driver.context.replacePixelsWithPBO(img.pbo, img.textureNative, w, h, args)
}

0 comments on commit 38815ba

Please sign in to comment.