graphicsdriver/metal, graphicsdriver/opengl: Remove the thread usages for performance

Instead, graphicscommand package has a thread.

Updates #1367
This commit is contained in:
Hajime Hoshi 2020-10-13 00:39:45 +09:00
parent b2d618c2be
commit 2942f10d9d
10 changed files with 518 additions and 738 deletions

View File

@ -20,7 +20,6 @@ import (
"github.com/hajimehoshi/ebiten/v2/internal/affine" "github.com/hajimehoshi/ebiten/v2/internal/affine"
"github.com/hajimehoshi/ebiten/v2/internal/graphics" "github.com/hajimehoshi/ebiten/v2/internal/graphics"
"github.com/hajimehoshi/ebiten/v2/internal/shaderir" "github.com/hajimehoshi/ebiten/v2/internal/shaderir"
"github.com/hajimehoshi/ebiten/v2/internal/thread"
) )
type Region struct { type Region struct {
@ -31,7 +30,6 @@ type Region struct {
} }
type Graphics interface { type Graphics interface {
SetThread(thread *thread.Thread)
Begin() Begin()
End() End()
SetTransparent(transparent bool) SetTransparent(transparent bool)

View File

@ -23,6 +23,7 @@ import (
"github.com/hajimehoshi/ebiten/v2/internal/driver" "github.com/hajimehoshi/ebiten/v2/internal/driver"
"github.com/hajimehoshi/ebiten/v2/internal/graphics" "github.com/hajimehoshi/ebiten/v2/internal/graphics"
"github.com/hajimehoshi/ebiten/v2/internal/shaderir" "github.com/hajimehoshi/ebiten/v2/internal/shaderir"
"github.com/hajimehoshi/ebiten/v2/internal/thread"
) )
var theGraphicsDriver driver.Graphics var theGraphicsDriver driver.Graphics
@ -31,6 +32,10 @@ func SetGraphicsDriver(driver driver.Graphics) {
theGraphicsDriver = driver theGraphicsDriver = driver
} }
func SetThread(thread *thread.Thread) {
theCommandQueue.thread = thread
}
func NeedsRestoring() bool { func NeedsRestoring() bool {
if theGraphicsDriver == nil { if theGraphicsDriver == nil {
// This happens on initialization. // This happens on initialization.
@ -86,6 +91,8 @@ type commandQueue struct {
tmpNumIndices int tmpNumIndices int
nextIndex int nextIndex int
thread *thread.Thread
err error err error
} }
@ -200,10 +207,19 @@ func (q *commandQueue) Enqueue(command command) {
// Flush flushes the command queue. // Flush flushes the command queue.
func (q *commandQueue) Flush() error { func (q *commandQueue) Flush() error {
return q.thread.Call(func() error {
return q.flush()
})
}
// flush must be called the main thread.
func (q *commandQueue) flush() error {
if len(q.commands) == 0 { if len(q.commands) == 0 {
return nil return nil
} }
// TODO: Use thread.Call here!
es := q.indices es := q.indices
vs := q.vertices vs := q.vertices
if recordLog() { if recordLog() {
@ -716,10 +732,17 @@ func (c *newShaderCommand) CanMergeWithDrawTrianglesCommand(dst *Image, src [gra
// ResetGraphicsDriverState resets or initializes the current graphics driver state. // ResetGraphicsDriverState resets or initializes the current graphics driver state.
func ResetGraphicsDriverState() error { func ResetGraphicsDriverState() error {
return theCommandQueue.thread.Call(func() error {
return theGraphicsDriver.Reset() return theGraphicsDriver.Reset()
})
} }
// MaxImageSize returns the maximum size of an image. // MaxImageSize returns the maximum size of an image.
func MaxImageSize() int { func MaxImageSize() int {
return theGraphicsDriver.MaxImageSize() var size int
_ = theCommandQueue.thread.Call(func() error {
size = theGraphicsDriver.MaxImageSize()
return nil
})
return size
} }

View File

@ -27,7 +27,6 @@ import (
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/metal/ca" "github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/metal/ca"
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/metal/mtl" "github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver/metal/mtl"
"github.com/hajimehoshi/ebiten/v2/internal/shaderir" "github.com/hajimehoshi/ebiten/v2/internal/shaderir"
"github.com/hajimehoshi/ebiten/v2/internal/thread"
) )
// #cgo CFLAGS: -x objective-c // #cgo CFLAGS: -x objective-c
@ -328,8 +327,6 @@ type Graphics struct {
maxImageSize int maxImageSize int
tmpTexture mtl.Texture tmpTexture mtl.Texture
t *thread.Thread
pool unsafe.Pointer pool unsafe.Pointer
} }
@ -339,36 +336,23 @@ func Get() *Graphics {
return &theGraphics return &theGraphics
} }
func (g *Graphics) SetThread(thread *thread.Thread) {
g.t = thread
}
func (g *Graphics) Begin() { func (g *Graphics) Begin() {
g.t.Call(func() error {
// NSAutoreleasePool is required to release drawable correctly (#847). // NSAutoreleasePool is required to release drawable correctly (#847).
// https://developer.apple.com/library/archive/documentation/3DDrawing/Conceptual/MTLBestPracticesGuide/Drawables.html // https://developer.apple.com/library/archive/documentation/3DDrawing/Conceptual/MTLBestPracticesGuide/Drawables.html
g.pool = C.allocAutoreleasePool() g.pool = C.allocAutoreleasePool()
return nil
})
} }
func (g *Graphics) End() { func (g *Graphics) End() {
g.flushIfNeeded(false, true) g.flushIfNeeded(false, true)
g.t.Call(func() error {
g.screenDrawable = ca.MetalDrawable{} g.screenDrawable = ca.MetalDrawable{}
C.releaseAutoreleasePool(g.pool) C.releaseAutoreleasePool(g.pool)
g.pool = nil g.pool = nil
return nil
})
} }
func (g *Graphics) SetWindow(window uintptr) { func (g *Graphics) SetWindow(window uintptr) {
g.t.Call(func() error {
// Note that [NSApp mainWindow] returns nil when the window is borderless. // Note that [NSApp mainWindow] returns nil when the window is borderless.
// Then the window is needed to be given explicitly. // Then the window is needed to be given explicitly.
g.view.setWindow(window) g.view.setWindow(window)
return nil
})
} }
func (g *Graphics) SetUIView(uiview uintptr) { func (g *Graphics) SetUIView(uiview uintptr) {
@ -377,7 +361,6 @@ func (g *Graphics) SetUIView(uiview uintptr) {
} }
func (g *Graphics) SetVertices(vertices []float32, indices []uint16) { func (g *Graphics) SetVertices(vertices []float32, indices []uint16) {
g.t.Call(func() error {
if g.vb != (mtl.Buffer{}) { if g.vb != (mtl.Buffer{}) {
g.vb.Release() g.vb.Release()
} }
@ -386,14 +369,11 @@ func (g *Graphics) SetVertices(vertices []float32, indices []uint16) {
} }
g.vb = g.view.getMTLDevice().MakeBufferWithBytes(unsafe.Pointer(&vertices[0]), unsafe.Sizeof(vertices[0])*uintptr(len(vertices)), resourceStorageMode) g.vb = g.view.getMTLDevice().MakeBufferWithBytes(unsafe.Pointer(&vertices[0]), unsafe.Sizeof(vertices[0])*uintptr(len(vertices)), resourceStorageMode)
g.ib = g.view.getMTLDevice().MakeBufferWithBytes(unsafe.Pointer(&indices[0]), unsafe.Sizeof(indices[0])*uintptr(len(indices)), resourceStorageMode) g.ib = g.view.getMTLDevice().MakeBufferWithBytes(unsafe.Pointer(&indices[0]), unsafe.Sizeof(indices[0])*uintptr(len(indices)), resourceStorageMode)
return nil
})
} }
func (g *Graphics) flushIfNeeded(wait bool, present bool) { func (g *Graphics) flushIfNeeded(wait bool, present bool) {
g.t.Call(func() error {
if g.cb == (mtl.CommandBuffer{}) { if g.cb == (mtl.CommandBuffer{}) {
return nil return
} }
if present && g.screenDrawable != (ca.MetalDrawable{}) { if present && g.screenDrawable != (ca.MetalDrawable{}) {
@ -405,9 +385,6 @@ func (g *Graphics) flushIfNeeded(wait bool, present bool) {
} }
g.cb = mtl.CommandBuffer{} g.cb = mtl.CommandBuffer{}
return nil
})
} }
func (g *Graphics) checkSize(width, height int) { func (g *Graphics) checkSize(width, height int) {
@ -452,11 +429,7 @@ func (g *Graphics) NewImage(width, height int) (driver.Image, error) {
StorageMode: storageMode, StorageMode: storageMode,
Usage: mtl.TextureUsageShaderRead | mtl.TextureUsageRenderTarget, Usage: mtl.TextureUsageShaderRead | mtl.TextureUsageRenderTarget,
} }
var t mtl.Texture t := g.view.getMTLDevice().MakeTexture(td)
g.t.Call(func() error {
t = g.view.getMTLDevice().MakeTexture(td)
return nil
})
i := &Image{ i := &Image{
id: g.genNextImageID(), id: g.genNextImageID(),
graphics: g, graphics: g,
@ -469,10 +442,7 @@ func (g *Graphics) NewImage(width, height int) (driver.Image, error) {
} }
func (g *Graphics) NewScreenFramebufferImage(width, height int) (driver.Image, error) { func (g *Graphics) NewScreenFramebufferImage(width, height int) (driver.Image, error) {
g.t.Call(func() error {
g.view.setDrawableSize(width, height) g.view.setDrawableSize(width, height)
return nil
})
i := &Image{ i := &Image{
id: g.genNextImageID(), id: g.genNextImageID(),
graphics: g, graphics: g,
@ -524,7 +494,6 @@ func operationToBlendFactor(c driver.Operation) mtl.BlendFactor {
} }
func (g *Graphics) Reset() error { func (g *Graphics) Reset() error {
if err := g.t.Call(func() error {
if g.cq != (mtl.CommandQueue{}) { if g.cq != (mtl.CommandQueue{}) {
g.cq.Release() g.cq.Release()
g.cq = mtl.CommandQueue{} g.cq = mtl.CommandQueue{}
@ -639,11 +608,6 @@ func (g *Graphics) Reset() error {
} }
g.cq = g.view.getMTLDevice().MakeCommandQueue() g.cq = g.view.getMTLDevice().MakeCommandQueue()
return nil
}); err != nil {
return err
}
return nil return nil
} }
@ -733,7 +697,6 @@ func (g *Graphics) Draw(dstID, srcID driver.ImageID, indexLen int, indexOffset i
}] }]
} }
if err := g.t.Call(func() error {
w, h := dst.internalSize() w, h := dst.internalSize()
sourceSize := []float32{0, 0} sourceSize := []float32{0, 0}
if filter != driver.FilterNearest { if filter != driver.FilterNearest {
@ -763,10 +726,6 @@ func (g *Graphics) Draw(dstID, srcID driver.ImageID, indexLen int, indexOffset i
return err return err
} }
return nil return nil
}); err != nil {
return err
}
return nil
} }
func (g *Graphics) SetVsyncEnabled(enabled bool) { func (g *Graphics) SetVsyncEnabled(enabled bool) {
@ -790,8 +749,6 @@ func (g *Graphics) HasHighPrecisionFloat() bool {
} }
func (g *Graphics) MaxImageSize() int { func (g *Graphics) MaxImageSize() int {
m := 0
g.t.Call(func() error {
if g.maxImageSize == 0 { if g.maxImageSize == 0 {
g.maxImageSize = 4096 g.maxImageSize = 4096
// https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf // https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf
@ -820,22 +777,12 @@ func (g *Graphics) MaxImageSize() int {
panic("metal: there is no supported feature set") panic("metal: there is no supported feature set")
} }
} }
m = g.maxImageSize return g.maxImageSize
return nil
})
return m
} }
func (g *Graphics) NewShader(program *shaderir.Program) (driver.Shader, error) { func (g *Graphics) NewShader(program *shaderir.Program) (driver.Shader, error) {
var s *Shader s, err := newShader(g.view.getMTLDevice(), g.genNextShaderID(), program)
if err := g.t.Call(func() error {
var err error
s, err = newShader(g.view.getMTLDevice(), g.genNextShaderID(), program)
if err != nil { if err != nil {
return err
}
return nil
}); err != nil {
return nil, err return nil, err
} }
g.addShader(s) g.addShader(s)
@ -877,13 +824,10 @@ func (i *Image) internalSize() (int, int) {
} }
func (i *Image) Dispose() { func (i *Image) Dispose() {
i.graphics.t.Call(func() error {
if i.texture != (mtl.Texture{}) { if i.texture != (mtl.Texture{}) {
i.texture.Release() i.texture.Release()
i.texture = mtl.Texture{} i.texture = mtl.Texture{}
} }
return nil
})
i.graphics.removeImage(i) i.graphics.removeImage(i)
} }
@ -897,7 +841,6 @@ func (i *Image) IsInvalidated() bool {
func (i *Image) syncTexture() { func (i *Image) syncTexture() {
// Calling SynchronizeTexture is ignored on iOS (see mtl.m), but it looks like committing BliCommandEncoder // Calling SynchronizeTexture is ignored on iOS (see mtl.m), but it looks like committing BliCommandEncoder
// is necessary (#1337). // is necessary (#1337).
i.graphics.t.Call(func() error {
if i.graphics.cb != (mtl.CommandBuffer{}) { if i.graphics.cb != (mtl.CommandBuffer{}) {
panic("metal: command buffer must be empty at syncTexture: flushIfNeeded is not called yet?") panic("metal: command buffer must be empty at syncTexture: flushIfNeeded is not called yet?")
} }
@ -908,8 +851,6 @@ func (i *Image) syncTexture() {
bce.EndEncoding() bce.EndEncoding()
cb.Commit() cb.Commit()
cb.WaitUntilCompleted() cb.WaitUntilCompleted()
return nil
})
} }
func (i *Image) Pixels() ([]byte, error) { func (i *Image) Pixels() ([]byte, error) {
@ -917,12 +858,9 @@ func (i *Image) Pixels() ([]byte, error) {
i.syncTexture() i.syncTexture()
b := make([]byte, 4*i.width*i.height) b := make([]byte, 4*i.width*i.height)
i.graphics.t.Call(func() error {
i.texture.GetBytes(&b[0], uintptr(4*i.width), mtl.Region{ i.texture.GetBytes(&b[0], uintptr(4*i.width), mtl.Region{
Size: mtl.Size{Width: i.width, Height: i.height, Depth: 1}, Size: mtl.Size{Width: i.width, Height: i.height, Depth: 1},
}, 0) }, 0)
return nil
})
return b, nil return b, nil
} }
@ -932,22 +870,17 @@ func (i *Image) ReplacePixels(args []*driver.ReplacePixelsArgs) {
// If the memory is shared (e.g., iOS), texture data doen't have to be synced. Send the data directly. // If the memory is shared (e.g., iOS), texture data doen't have to be synced. Send the data directly.
if storageMode == mtl.StorageModeShared { if storageMode == mtl.StorageModeShared {
g.t.Call(func() error {
for _, a := range args { for _, a := range args {
i.texture.ReplaceRegion(mtl.Region{ i.texture.ReplaceRegion(mtl.Region{
Origin: mtl.Origin{X: a.X, Y: a.Y, Z: 0}, Origin: mtl.Origin{X: a.X, Y: a.Y, Z: 0},
Size: mtl.Size{Width: a.Width, Height: a.Height, Depth: 1}, Size: mtl.Size{Width: a.Width, Height: a.Height, Depth: 1},
}, 0, unsafe.Pointer(&a.Pixels[0]), 4*a.Width) }, 0, unsafe.Pointer(&a.Pixels[0]), 4*a.Width)
} }
return nil
})
return
} }
// If the memory is managed (e.g., macOS), texture data cannot be sent to the destination directly because // If the memory is managed (e.g., macOS), texture data cannot be sent to the destination directly because
// this requires synchronizing data between CPU and GPU. As synchronizing is inefficient, let's send the // this requires synchronizing data between CPU and GPU. As synchronizing is inefficient, let's send the
// data to a temporary texture once, and then copy it in GPU. // data to a temporary texture once, and then copy it in GPU.
g.t.Call(func() error {
w, h := i.texture.Width(), i.texture.Height() w, h := i.texture.Width(), i.texture.Height()
if g.tmpTexture == (mtl.Texture{}) || w > g.tmpTexture.Width() || h > g.tmpTexture.Height() { if g.tmpTexture == (mtl.Texture{}) || w > g.tmpTexture.Width() || h > g.tmpTexture.Height() {
if g.tmpTexture != (mtl.Texture{}) { if g.tmpTexture != (mtl.Texture{}) {
@ -981,9 +914,6 @@ func (i *Image) ReplacePixels(args []*driver.ReplacePixelsArgs) {
bce.CopyFromTexture(g.tmpTexture, 0, 0, o, s, i.texture, 0, 0, o) bce.CopyFromTexture(g.tmpTexture, 0, 0, o, s, i.texture, 0, 0, o)
} }
bce.EndEncoding() bce.EndEncoding()
return nil
})
} }
func (g *Graphics) DrawShader(dstID driver.ImageID, srcIDs [graphics.ShaderImageNum]driver.ImageID, offsets [graphics.ShaderImageNum - 1][2]float32, shader driver.ShaderID, indexLen int, indexOffset int, sourceRegion driver.Region, mode driver.CompositeMode, uniforms []interface{}) error { func (g *Graphics) DrawShader(dstID driver.ImageID, srcIDs [graphics.ShaderImageNum]driver.ImageID, offsets [graphics.ShaderImageNum - 1][2]float32, shader driver.ShaderID, indexLen int, indexOffset int, sourceRegion driver.Region, mode driver.CompositeMode, uniforms []interface{}) error {
@ -993,7 +923,6 @@ func (g *Graphics) DrawShader(dstID driver.ImageID, srcIDs [graphics.ShaderImage
srcs[i] = g.images[srcID] srcs[i] = g.images[srcID]
} }
if err := g.t.Call(func() error {
rps, err := g.shaders[shader].RenderPipelineState(g.view.getMTLDevice(), mode) rps, err := g.shaders[shader].RenderPipelineState(g.view.getMTLDevice(), mode)
if err != nil { if err != nil {
return err return err
@ -1042,8 +971,4 @@ func (g *Graphics) DrawShader(dstID driver.ImageID, srcIDs [graphics.ShaderImage
return err return err
} }
return nil return nil
}); err != nil {
return err
}
return nil
} }

View File

@ -19,7 +19,6 @@ import (
"sync" "sync"
"github.com/hajimehoshi/ebiten/v2/internal/driver" "github.com/hajimehoshi/ebiten/v2/internal/driver"
"github.com/hajimehoshi/ebiten/v2/internal/thread"
) )
func convertOperation(op driver.Operation) operation { func convertOperation(op driver.Operation) operation {
@ -56,8 +55,6 @@ type context struct {
highp bool highp bool
highpOnce sync.Once highpOnce sync.Once
t *thread.Thread
contextImpl contextImpl
} }

View File

@ -101,69 +101,51 @@ type contextImpl struct {
} }
func (c *context) reset() error { func (c *context) reset() error {
if err := c.t.Call(func() error { if !c.init {
if c.init {
return nil
}
// Note that this initialization must be done after Loop is called. // Note that this initialization must be done after Loop is called.
if err := gl.Init(); err != nil { if err := gl.Init(); err != nil {
return fmt.Errorf("opengl: initializing error %v", err) return fmt.Errorf("opengl: initializing error %v", err)
} }
c.init = true c.init = true
return nil
}); err != nil {
return err
} }
c.locationCache = newLocationCache() c.locationCache = newLocationCache()
c.lastTexture = invalidTexture c.lastTexture = invalidTexture
c.lastFramebuffer = invalidFramebuffer c.lastFramebuffer = invalidFramebuffer
c.lastViewportWidth = 0 c.lastViewportWidth = 0
c.lastViewportHeight = 0 c.lastViewportHeight = 0
c.lastCompositeMode = driver.CompositeModeUnknown c.lastCompositeMode = driver.CompositeModeUnknown
_ = c.t.Call(func() error {
gl.Enable(gl.BLEND) gl.Enable(gl.BLEND)
return nil
})
c.blendFunc(driver.CompositeModeSourceOver) c.blendFunc(driver.CompositeModeSourceOver)
_ = c.t.Call(func() error {
f := int32(0) f := int32(0)
gl.GetIntegerv(gl.FRAMEBUFFER_BINDING, &f) gl.GetIntegerv(gl.FRAMEBUFFER_BINDING, &f)
c.screenFramebuffer = framebufferNative(f) c.screenFramebuffer = framebufferNative(f)
return nil return nil
})
return nil
} }
func (c *context) blendFunc(mode driver.CompositeMode) { func (c *context) blendFunc(mode driver.CompositeMode) {
_ = c.t.Call(func() error {
if c.lastCompositeMode == mode { if c.lastCompositeMode == mode {
return nil return
} }
c.lastCompositeMode = mode c.lastCompositeMode = mode
s, d := mode.Operations() s, d := mode.Operations()
s2, d2 := convertOperation(s), convertOperation(d) s2, d2 := convertOperation(s), convertOperation(d)
gl.BlendFunc(uint32(s2), uint32(d2)) gl.BlendFunc(uint32(s2), uint32(d2))
return nil
})
} }
func (c *context) newTexture(width, height int) (textureNative, error) { func (c *context) newTexture(width, height int) (textureNative, error) {
var texture textureNative
if err := c.t.Call(func() error {
var t uint32 var t uint32
gl.GenTextures(1, &t) gl.GenTextures(1, &t)
// TODO: Use gl.IsTexture // TODO: Use gl.IsTexture
if t <= 0 { if t <= 0 {
return errors.New("opengl: creating texture failed") return 0, errors.New("opengl: creating texture failed")
} }
gl.PixelStorei(gl.UNPACK_ALIGNMENT, 4) gl.PixelStorei(gl.UNPACK_ALIGNMENT, 4)
texture = textureNative(t) texture := textureNative(t)
return nil
}); err != nil {
return 0, err
}
c.bindTexture(texture) c.bindTexture(texture)
_ = c.t.Call(func() error {
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE) gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)
@ -171,61 +153,38 @@ func (c *context) newTexture(width, height int) (textureNative, error) {
// If data is nil, this just allocates memory and the content is undefined. // If data is nil, this just allocates memory and the content is undefined.
// https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glTexImage2D.xhtml // https://www.khronos.org/registry/OpenGL-Refpages/gl4/html/glTexImage2D.xhtml
gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, int32(width), int32(height), 0, gl.RGBA, gl.UNSIGNED_BYTE, nil) gl.TexImage2D(gl.TEXTURE_2D, 0, gl.RGBA, int32(width), int32(height), 0, gl.RGBA, gl.UNSIGNED_BYTE, nil)
return nil
})
return texture, nil return texture, nil
} }
func (c *context) bindFramebufferImpl(f framebufferNative) { func (c *context) bindFramebufferImpl(f framebufferNative) {
_ = c.t.Call(func() error {
gl.BindFramebufferEXT(gl.FRAMEBUFFER, uint32(f)) gl.BindFramebufferEXT(gl.FRAMEBUFFER, uint32(f))
return nil
})
} }
func (c *context) framebufferPixels(f *framebuffer, width, height int) ([]byte, error) { func (c *context) framebufferPixels(f *framebuffer, width, height int) []byte {
var pixels []byte
_ = c.t.Call(func() error {
gl.Flush() gl.Flush()
return nil
})
c.bindFramebuffer(f.native) c.bindFramebuffer(f.native)
if err := c.t.Call(func() error { pixels := make([]byte, 4*width*height)
pixels = make([]byte, 4*width*height)
gl.ReadPixels(0, 0, int32(width), int32(height), gl.RGBA, gl.UNSIGNED_BYTE, gl.Ptr(pixels)) gl.ReadPixels(0, 0, int32(width), int32(height), gl.RGBA, gl.UNSIGNED_BYTE, gl.Ptr(pixels))
return nil return pixels
}); err != nil {
return nil, err
}
return pixels, nil
} }
func (c *context) activeTexture(idx int) { func (c *context) activeTexture(idx int) {
_ = c.t.Call(func() error {
gl.ActiveTexture(gl.TEXTURE0 + uint32(idx)) gl.ActiveTexture(gl.TEXTURE0 + uint32(idx))
return nil
})
} }
func (c *context) bindTextureImpl(t textureNative) { func (c *context) bindTextureImpl(t textureNative) {
_ = c.t.Call(func() error {
gl.BindTexture(gl.TEXTURE_2D, uint32(t)) gl.BindTexture(gl.TEXTURE_2D, uint32(t))
return nil
})
} }
func (c *context) deleteTexture(t textureNative) { func (c *context) deleteTexture(t textureNative) {
_ = c.t.Call(func() error {
tt := uint32(t) tt := uint32(t)
if !gl.IsTexture(tt) { if !gl.IsTexture(tt) {
return nil return
} }
if c.lastTexture == t { if c.lastTexture == t {
c.lastTexture = invalidTexture c.lastTexture = invalidTexture
} }
gl.DeleteTextures(1, &tt) gl.DeleteTextures(1, &tt)
return nil
})
} }
func (c *context) isTexture(t textureNative) bool { func (c *context) isTexture(t textureNative) bool {
@ -233,51 +192,35 @@ func (c *context) isTexture(t textureNative) bool {
} }
func (c *context) newFramebuffer(texture textureNative) (framebufferNative, error) { func (c *context) newFramebuffer(texture textureNative) (framebufferNative, error) {
var framebuffer framebufferNative
var f uint32 var f uint32
if err := c.t.Call(func() error {
gl.GenFramebuffersEXT(1, &f) gl.GenFramebuffersEXT(1, &f)
// TODO: Use gl.IsFramebuffer // TODO: Use gl.IsFramebuffer
if f <= 0 { if f <= 0 {
return errors.New("opengl: creating framebuffer failed: gl.IsFramebuffer returns false") return 0, errors.New("opengl: creating framebuffer failed: gl.IsFramebuffer returns false")
}
return nil
}); err != nil {
return 0, err
} }
c.bindFramebuffer(framebufferNative(f)) c.bindFramebuffer(framebufferNative(f))
if err := c.t.Call(func() error {
gl.FramebufferTexture2DEXT(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, uint32(texture), 0) gl.FramebufferTexture2DEXT(gl.FRAMEBUFFER, gl.COLOR_ATTACHMENT0, gl.TEXTURE_2D, uint32(texture), 0)
s := gl.CheckFramebufferStatusEXT(gl.FRAMEBUFFER) s := gl.CheckFramebufferStatusEXT(gl.FRAMEBUFFER)
if s != gl.FRAMEBUFFER_COMPLETE { if s != gl.FRAMEBUFFER_COMPLETE {
if s != 0 { if s != 0 {
return fmt.Errorf("opengl: creating framebuffer failed: %v", s) return 0, fmt.Errorf("opengl: creating framebuffer failed: %v", s)
} }
if e := gl.GetError(); e != gl.NO_ERROR { if e := gl.GetError(); e != gl.NO_ERROR {
return fmt.Errorf("opengl: creating framebuffer failed: (glGetError) %d", e) return 0, fmt.Errorf("opengl: creating framebuffer failed: (glGetError) %d", e)
} }
return fmt.Errorf("opengl: creating framebuffer failed: unknown error") return 0, fmt.Errorf("opengl: creating framebuffer failed: unknown error")
} }
framebuffer = framebufferNative(f) return framebufferNative(f), nil
return nil
}); err != nil {
return 0, err
}
return framebuffer, nil
} }
func (c *context) setViewportImpl(width, height int) { func (c *context) setViewportImpl(width, height int) {
_ = c.t.Call(func() error {
gl.Viewport(0, 0, int32(width), int32(height)) gl.Viewport(0, 0, int32(width), int32(height))
return nil
})
} }
func (c *context) deleteFramebuffer(f framebufferNative) { func (c *context) deleteFramebuffer(f framebufferNative) {
_ = c.t.Call(func() error {
ff := uint32(f) ff := uint32(f)
if !gl.IsFramebufferEXT(ff) { if !gl.IsFramebufferEXT(ff) {
return nil return
} }
if c.lastFramebuffer == f { if c.lastFramebuffer == f {
c.lastFramebuffer = invalidFramebuffer c.lastFramebuffer = invalidFramebuffer
@ -285,16 +228,12 @@ func (c *context) deleteFramebuffer(f framebufferNative) {
c.lastViewportHeight = 0 c.lastViewportHeight = 0
} }
gl.DeleteFramebuffersEXT(1, &ff) gl.DeleteFramebuffersEXT(1, &ff)
return nil
})
} }
func (c *context) newShader(shaderType shaderType, source string) (shader, error) { func (c *context) newShader(shaderType shaderType, source string) (shader, error) {
var sh shader
if err := c.t.Call(func() error {
s := gl.CreateShader(uint32(shaderType)) s := gl.CreateShader(uint32(shaderType))
if s == 0 { if s == 0 {
return fmt.Errorf("opengl: glCreateShader failed: shader type: %d", shaderType) return 0, fmt.Errorf("opengl: glCreateShader failed: shader type: %d", shaderType)
} }
cSources, free := gl.Strs(source + "\x00") cSources, free := gl.Strs(source + "\x00")
gl.ShaderSource(uint32(s), 1, cSources, nil) gl.ShaderSource(uint32(s), 1, cSources, nil)
@ -311,29 +250,19 @@ func (c *context) newShader(shaderType shaderType, source string) (shader, error
log = make([]byte, l) log = make([]byte, l)
gl.GetShaderInfoLog(s, l, nil, (*uint8)(gl.Ptr(log))) gl.GetShaderInfoLog(s, l, nil, (*uint8)(gl.Ptr(log)))
} }
return fmt.Errorf("opengl: shader compile failed: %s", log) return 0, fmt.Errorf("opengl: shader compile failed: %s", log)
} }
sh = shader(s) return shader(s), nil
return nil
}); err != nil {
return 0, err
}
return sh, nil
} }
func (c *context) deleteShader(s shader) { func (c *context) deleteShader(s shader) {
_ = c.t.Call(func() error {
gl.DeleteShader(uint32(s)) gl.DeleteShader(uint32(s))
return nil
})
} }
func (c *context) newProgram(shaders []shader, attributes []string) (program, error) { func (c *context) newProgram(shaders []shader, attributes []string) (program, error) {
var pr program
if err := c.t.Call(func() error {
p := gl.CreateProgram() p := gl.CreateProgram()
if p == 0 { if p == 0 {
return errors.New("opengl: glCreateProgram failed") return 0, errors.New("opengl: glCreateProgram failed")
} }
for _, shader := range shaders { for _, shader := range shaders {
@ -357,31 +286,20 @@ func (c *context) newProgram(shaders []shader, attributes []string) (program, er
log = make([]byte, l) log = make([]byte, l)
gl.GetProgramInfoLog(p, l, nil, (*uint8)(gl.Ptr(log))) gl.GetProgramInfoLog(p, l, nil, (*uint8)(gl.Ptr(log)))
} }
return fmt.Errorf("opengl: program error: %s", log) return 0, fmt.Errorf("opengl: program error: %s", log)
} }
pr = program(p) return program(p), nil
return nil
}); err != nil {
return 0, err
}
return pr, nil
} }
func (c *context) useProgram(p program) { func (c *context) useProgram(p program) {
_ = c.t.Call(func() error {
gl.UseProgram(uint32(p)) gl.UseProgram(uint32(p))
return nil
})
} }
func (c *context) deleteProgram(p program) { func (c *context) deleteProgram(p program) {
_ = c.t.Call(func() error {
if !gl.IsProgram(uint32(p)) { if !gl.IsProgram(uint32(p)) {
return nil return
} }
gl.DeleteProgram(uint32(p)) gl.DeleteProgram(uint32(p))
return nil
})
} }
func (c *context) getUniformLocationImpl(p program, location string) uniformLocation { func (c *context) getUniformLocationImpl(p program, location string) uniformLocation {
@ -392,41 +310,28 @@ func (c *context) getUniformLocationImpl(p program, location string) uniformLoca
} }
func (c *context) uniformInt(p program, location string, v int) bool { func (c *context) uniformInt(p program, location string, v int) bool {
var r bool
_ = c.t.Call(func() error {
l := int32(c.locationCache.GetUniformLocation(c, p, location)) l := int32(c.locationCache.GetUniformLocation(c, p, location))
if l == invalidUniform { if l == invalidUniform {
return nil return false
} }
r = true
gl.Uniform1i(l, int32(v)) gl.Uniform1i(l, int32(v))
return nil return true
})
return r
} }
func (c *context) uniformFloat(p program, location string, v float32) bool { func (c *context) uniformFloat(p program, location string, v float32) bool {
var r bool
_ = c.t.Call(func() error {
l := int32(c.locationCache.GetUniformLocation(c, p, location)) l := int32(c.locationCache.GetUniformLocation(c, p, location))
if l == invalidUniform { if l == invalidUniform {
return nil return false
} }
r = true
gl.Uniform1f(l, v) gl.Uniform1f(l, v)
return nil return true
})
return r
} }
func (c *context) uniformFloats(p program, location string, v []float32, typ shaderir.Type) bool { func (c *context) uniformFloats(p program, location string, v []float32, typ shaderir.Type) bool {
var r bool
_ = c.t.Call(func() error {
l := int32(c.locationCache.GetUniformLocation(c, p, location)) l := int32(c.locationCache.GetUniformLocation(c, p, location))
if l == invalidUniform { if l == invalidUniform {
return nil return false
} }
r = true
base := typ.Main base := typ.Main
len := int32(1) len := int32(1)
@ -453,103 +358,62 @@ func (c *context) uniformFloats(p program, location string, v []float32, typ sha
default: default:
panic(fmt.Sprintf("opengl: unexpected type: %s", typ.String())) panic(fmt.Sprintf("opengl: unexpected type: %s", typ.String()))
} }
return nil return true
})
return r
} }
func (c *context) vertexAttribPointer(p program, index int, size int, dataType dataType, stride int, offset int) { func (c *context) vertexAttribPointer(p program, index int, size int, dataType dataType, stride int, offset int) {
_ = c.t.Call(func() error {
gl.VertexAttribPointer(uint32(index), int32(size), uint32(dataType), false, int32(stride), uintptr(offset)) gl.VertexAttribPointer(uint32(index), int32(size), uint32(dataType), false, int32(stride), uintptr(offset))
return nil
})
} }
func (c *context) enableVertexAttribArray(p program, index int) { func (c *context) enableVertexAttribArray(p program, index int) {
_ = c.t.Call(func() error {
gl.EnableVertexAttribArray(uint32(index)) gl.EnableVertexAttribArray(uint32(index))
return nil
})
} }
func (c *context) disableVertexAttribArray(p program, index int) { func (c *context) disableVertexAttribArray(p program, index int) {
_ = c.t.Call(func() error {
gl.DisableVertexAttribArray(uint32(index)) gl.DisableVertexAttribArray(uint32(index))
return nil
})
} }
func (c *context) newArrayBuffer(size int) buffer { func (c *context) newArrayBuffer(size int) buffer {
var bf buffer
_ = c.t.Call(func() error {
var b uint32 var b uint32
gl.GenBuffers(1, &b) gl.GenBuffers(1, &b)
gl.BindBuffer(uint32(arrayBuffer), b) gl.BindBuffer(uint32(arrayBuffer), b)
gl.BufferData(uint32(arrayBuffer), size, nil, uint32(dynamicDraw)) gl.BufferData(uint32(arrayBuffer), size, nil, uint32(dynamicDraw))
bf = buffer(b) return buffer(b)
return nil
})
return bf
} }
func (c *context) newElementArrayBuffer(size int) buffer { func (c *context) newElementArrayBuffer(size int) buffer {
var bf buffer
_ = c.t.Call(func() error {
var b uint32 var b uint32
gl.GenBuffers(1, &b) gl.GenBuffers(1, &b)
gl.BindBuffer(uint32(elementArrayBuffer), b) gl.BindBuffer(uint32(elementArrayBuffer), b)
gl.BufferData(uint32(elementArrayBuffer), size, nil, uint32(dynamicDraw)) gl.BufferData(uint32(elementArrayBuffer), size, nil, uint32(dynamicDraw))
bf = buffer(b) return buffer(b)
return nil
})
return bf
} }
func (c *context) bindBuffer(bufferType bufferType, b buffer) { func (c *context) bindBuffer(bufferType bufferType, b buffer) {
_ = c.t.Call(func() error {
gl.BindBuffer(uint32(bufferType), uint32(b)) gl.BindBuffer(uint32(bufferType), uint32(b))
return nil
})
} }
func (c *context) arrayBufferSubData(data []float32) { func (c *context) arrayBufferSubData(data []float32) {
_ = c.t.Call(func() error {
gl.BufferSubData(uint32(arrayBuffer), 0, len(data)*4, gl.Ptr(data)) gl.BufferSubData(uint32(arrayBuffer), 0, len(data)*4, gl.Ptr(data))
return nil
})
} }
func (c *context) elementArrayBufferSubData(data []uint16) { func (c *context) elementArrayBufferSubData(data []uint16) {
_ = c.t.Call(func() error {
gl.BufferSubData(uint32(elementArrayBuffer), 0, len(data)*2, gl.Ptr(data)) gl.BufferSubData(uint32(elementArrayBuffer), 0, len(data)*2, gl.Ptr(data))
return nil
})
} }
func (c *context) deleteBuffer(b buffer) { func (c *context) deleteBuffer(b buffer) {
_ = c.t.Call(func() error {
bb := uint32(b) bb := uint32(b)
gl.DeleteBuffers(1, &bb) gl.DeleteBuffers(1, &bb)
return nil
})
} }
func (c *context) drawElements(len int, offsetInBytes int) { func (c *context) drawElements(len int, offsetInBytes int) {
_ = c.t.Call(func() error {
gl.DrawElements(gl.TRIANGLES, int32(len), gl.UNSIGNED_SHORT, uintptr(offsetInBytes)) gl.DrawElements(gl.TRIANGLES, int32(len), gl.UNSIGNED_SHORT, uintptr(offsetInBytes))
return nil
})
} }
func (c *context) maxTextureSizeImpl() int { func (c *context) maxTextureSizeImpl() int {
size := 0
_ = c.t.Call(func() error {
s := int32(0) s := int32(0)
gl.GetIntegerv(gl.MAX_TEXTURE_SIZE, &s) gl.GetIntegerv(gl.MAX_TEXTURE_SIZE, &s)
size = int(s) return int(s)
return nil
})
return size
} }
func (c *context) getShaderPrecisionFormatPrecision() int { func (c *context) getShaderPrecisionFormatPrecision() int {
@ -559,10 +423,7 @@ func (c *context) getShaderPrecisionFormatPrecision() int {
} }
func (c *context) flush() { func (c *context) flush() {
_ = c.t.Call(func() error {
gl.Flush() gl.Flush()
return nil
})
} }
func (c *context) needsRestoring() bool { func (c *context) needsRestoring() bool {
@ -570,42 +431,27 @@ func (c *context) needsRestoring() bool {
} }
func (c *context) canUsePBO() bool { func (c *context) canUsePBO() bool {
var available bool return isPBOAvailable()
_ = c.t.Call(func() error {
available = isPBOAvailable()
return nil
})
return available
} }
func (c *context) texSubImage2D(t textureNative, width, height int, args []*driver.ReplacePixelsArgs) { func (c *context) texSubImage2D(t textureNative, width, height int, args []*driver.ReplacePixelsArgs) {
c.bindTexture(t) c.bindTexture(t)
_ = c.t.Call(func() error {
for _, a := range args { for _, a := range args {
gl.TexSubImage2D(gl.TEXTURE_2D, 0, int32(a.X), int32(a.Y), int32(a.Width), int32(a.Height), gl.RGBA, gl.UNSIGNED_BYTE, gl.Ptr(a.Pixels)) gl.TexSubImage2D(gl.TEXTURE_2D, 0, int32(a.X), int32(a.Y), int32(a.Width), int32(a.Height), gl.RGBA, gl.UNSIGNED_BYTE, gl.Ptr(a.Pixels))
} }
return nil
})
} }
func (c *context) newPixelBufferObject(width, height int) buffer { func (c *context) newPixelBufferObject(width, height int) buffer {
var bf buffer
_ = c.t.Call(func() error {
var b uint32 var b uint32
gl.GenBuffers(1, &b) gl.GenBuffers(1, &b)
gl.BindBuffer(gl.PIXEL_UNPACK_BUFFER, b) gl.BindBuffer(gl.PIXEL_UNPACK_BUFFER, b)
gl.BufferData(gl.PIXEL_UNPACK_BUFFER, 4*width*height, nil, gl.STREAM_DRAW) gl.BufferData(gl.PIXEL_UNPACK_BUFFER, 4*width*height, nil, gl.STREAM_DRAW)
gl.BindBuffer(gl.PIXEL_UNPACK_BUFFER, 0) gl.BindBuffer(gl.PIXEL_UNPACK_BUFFER, 0)
bf = buffer(b) return buffer(b)
return nil
})
return bf
} }
func (c *context) replacePixelsWithPBO(buffer buffer, t textureNative, width, height int, args []*driver.ReplacePixelsArgs) { func (c *context) replacePixelsWithPBO(buffer buffer, t textureNative, width, height int, args []*driver.ReplacePixelsArgs) {
c.bindTexture(t) c.bindTexture(t)
_ = c.t.Call(func() error {
gl.BindBuffer(gl.PIXEL_UNPACK_BUFFER, uint32(buffer)) gl.BindBuffer(gl.PIXEL_UNPACK_BUFFER, uint32(buffer))
stride := 4 * width stride := 4 * width
@ -618,6 +464,4 @@ func (c *context) replacePixelsWithPBO(buffer buffer, t textureNative, width, he
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
})
} }

View File

@ -273,7 +273,7 @@ func (c *context) bindFramebufferImpl(f framebufferNative) {
gl.Call("bindFramebuffer", framebuffer_, js.Value(f)) gl.Call("bindFramebuffer", framebuffer_, js.Value(f))
} }
func (c *context) framebufferPixels(f *framebuffer, width, height int) ([]byte, error) { func (c *context) framebufferPixels(f *framebuffer, width, height int) []byte {
c.ensureGL() c.ensureGL()
gl := c.gl gl := c.gl
@ -282,7 +282,7 @@ func (c *context) framebufferPixels(f *framebuffer, width, height int) ([]byte,
p := jsutil.TemporaryUint8Array(4 * width * height) p := jsutil.TemporaryUint8Array(4 * width * height)
gl.Call("readPixels", 0, 0, width, height, rgba, unsignedByte, p) gl.Call("readPixels", 0, 0, width, height, rgba, unsignedByte, p)
return jsutil.Uint8ArrayToSlice(p), nil return jsutil.Uint8ArrayToSlice(p)
} }
func (c *context) activeTexture(idx int) { func (c *context) activeTexture(idx int) {

View File

@ -148,7 +148,7 @@ func (c *context) bindFramebufferImpl(f framebufferNative) {
gl.BindFramebuffer(mgl.FRAMEBUFFER, mgl.Framebuffer(f)) gl.BindFramebuffer(mgl.FRAMEBUFFER, mgl.Framebuffer(f))
} }
func (c *context) framebufferPixels(f *framebuffer, width, height int) ([]byte, error) { func (c *context) framebufferPixels(f *framebuffer, width, height int) []byte {
gl := c.gl gl := c.gl
gl.Flush() gl.Flush()
@ -156,7 +156,7 @@ func (c *context) framebufferPixels(f *framebuffer, width, height int) ([]byte,
pixels := make([]byte, 4*width*height) pixels := make([]byte, 4*width*height)
gl.ReadPixels(pixels, 0, 0, width, height, mgl.RGBA, mgl.UNSIGNED_BYTE) gl.ReadPixels(pixels, 0, 0, width, height, mgl.RGBA, mgl.UNSIGNED_BYTE)
return pixels, nil return pixels
} }
func (c *context) activeTexture(idx int) { func (c *context) activeTexture(idx int) {

View File

@ -21,7 +21,6 @@ import (
"github.com/hajimehoshi/ebiten/v2/internal/driver" "github.com/hajimehoshi/ebiten/v2/internal/driver"
"github.com/hajimehoshi/ebiten/v2/internal/graphics" "github.com/hajimehoshi/ebiten/v2/internal/graphics"
"github.com/hajimehoshi/ebiten/v2/internal/shaderir" "github.com/hajimehoshi/ebiten/v2/internal/shaderir"
"github.com/hajimehoshi/ebiten/v2/internal/thread"
) )
var theGraphics Graphics var theGraphics Graphics
@ -44,10 +43,6 @@ type Graphics struct {
drawCalled bool drawCalled bool
} }
func (g *Graphics) SetThread(thread *thread.Thread) {
g.context.t = thread
}
func (g *Graphics) Begin() { func (g *Graphics) Begin() {
// Do nothing. // Do nothing.
} }

View File

@ -64,10 +64,7 @@ func (i *Image) Pixels() ([]byte, error) {
if err := i.ensureFramebuffer(); err != nil { if err := i.ensureFramebuffer(); err != nil {
return nil, err return nil, err
} }
p, err := i.graphics.context.framebufferPixels(i.framebuffer, i.width, i.height) p := i.graphics.context.framebufferPixels(i.framebuffer, i.width, i.height)
if err != nil {
return nil, err
}
return p, nil return p, nil
} }

View File

@ -29,6 +29,7 @@ import (
"github.com/hajimehoshi/ebiten/v2/internal/devicescale" "github.com/hajimehoshi/ebiten/v2/internal/devicescale"
"github.com/hajimehoshi/ebiten/v2/internal/driver" "github.com/hajimehoshi/ebiten/v2/internal/driver"
"github.com/hajimehoshi/ebiten/v2/internal/glfw" "github.com/hajimehoshi/ebiten/v2/internal/glfw"
"github.com/hajimehoshi/ebiten/v2/internal/graphicscommand"
"github.com/hajimehoshi/ebiten/v2/internal/hooks" "github.com/hajimehoshi/ebiten/v2/internal/hooks"
"github.com/hajimehoshi/ebiten/v2/internal/thread" "github.com/hajimehoshi/ebiten/v2/internal/thread"
) )
@ -588,7 +589,7 @@ func (u *UserInterface) Run(uicontext driver.UIContext) error {
// Initialize the main thread first so the thread is available at u.run (#809). // Initialize the main thread first so the thread is available at u.run (#809).
u.t = thread.New() u.t = thread.New()
u.Graphics().SetThread(u.t) graphicscommand.SetThread(u.t)
ch := make(chan error, 1) ch := make(chan error, 1)
go func() { go func() {