opengl: Move ensureFramebuffer to opengl.Image

This commit is contained in:
Hajime Hoshi 2018-11-04 19:06:13 +09:00
parent 95a925ec9a
commit 2a7caf7755
5 changed files with 54 additions and 32 deletions

View File

@ -228,21 +228,18 @@ func (c *drawImageCommand) String() string {
// Exec executes the drawImageCommand. // Exec executes the drawImageCommand.
func (c *drawImageCommand) Exec(indexOffsetInBytes int) error { func (c *drawImageCommand) Exec(indexOffsetInBytes int) error {
f, err := c.dst.ensureFramebuffer()
if err != nil {
return err
}
// On some environments, viewport size must be within the framebuffer size. // On some environments, viewport size must be within the framebuffer size.
// e.g. Edge (#71), Chrome on GPD Pocket (#420), macOS Mojave (#691). // e.g. Edge (#71), Chrome on GPD Pocket (#420), macOS Mojave (#691).
// Use the same size of the framebuffer here. // Use the same size of the framebuffer here.
opengl.GetContext().SetViewport(f) if err := c.dst.image.SetViewport(); err != nil {
return err
}
opengl.GetContext().BlendFunc(c.mode) opengl.GetContext().BlendFunc(c.mode)
if c.nindices == 0 { if c.nindices == 0 {
return nil return nil
} }
proj := f.ProjectionMatrix() proj := c.dst.image.ProjectionMatrix()
dw, dh := c.dst.Size() dw, dh := c.dst.Size()
sw, sh := c.src.Size() sw, sh := c.src.Size()
opengl.UseProgram(proj, c.src.image, dw, dh, sw, sh, c.color, c.filter) opengl.UseProgram(proj, c.src.image, dw, dh, sw, sh, c.color, c.filter)
@ -340,12 +337,7 @@ type pixelsCommand struct {
// Exec executes a pixelsCommand. // Exec executes a pixelsCommand.
func (c *pixelsCommand) Exec(indexOffsetInBytes int) error { func (c *pixelsCommand) Exec(indexOffsetInBytes int) error {
f, err := c.img.ensureFramebuffer() p, err := c.img.image.Pixels()
if err != nil {
return err
}
w, h := c.img.Size()
p, err := opengl.GetContext().FramebufferPixels(f, w, h)
if err != nil { if err != nil {
return err return err
} }

View File

@ -17,7 +17,6 @@ package graphicscommand
import ( import (
"github.com/hajimehoshi/ebiten/internal/affine" "github.com/hajimehoshi/ebiten/internal/affine"
"github.com/hajimehoshi/ebiten/internal/graphics" "github.com/hajimehoshi/ebiten/internal/graphics"
"github.com/hajimehoshi/ebiten/internal/math"
"github.com/hajimehoshi/ebiten/internal/opengl" "github.com/hajimehoshi/ebiten/internal/opengl"
) )
@ -115,16 +114,3 @@ func (i *Image) ReplacePixels(p []byte, x, y, width, height int) {
func (i *Image) IsInvalidated() bool { func (i *Image) IsInvalidated() bool {
return i.image.IsInvalidated() return i.image.IsInvalidated()
} }
func (i *Image) ensureFramebuffer() (*opengl.Framebuffer, error) {
if i.image.Framebuffer != nil {
return i.image.Framebuffer, nil
}
w, h := i.image.Size()
f, err := opengl.NewFramebufferFromTexture(i.image.Texture, math.NextPowerOf2Int(w), math.NextPowerOf2Int(h))
if err != nil {
return nil, err
}
i.image.Framebuffer = f
return i.image.Framebuffer, nil
}

View File

@ -96,7 +96,7 @@ func (c *Context) bindFramebuffer(f framebufferNative) {
c.lastFramebuffer = f c.lastFramebuffer = f
} }
func (c *Context) SetViewport(f *Framebuffer) { func (c *Context) setViewport(f *Framebuffer) {
c.bindFramebuffer(f.native) c.bindFramebuffer(f.native)
if c.lastViewportWidth != f.width || c.lastViewportHeight != f.height { if c.lastViewportWidth != f.width || c.lastViewportHeight != f.height {
c.setViewportImpl(f.width, f.height) c.setViewportImpl(f.width, f.height)

View File

@ -46,12 +46,12 @@ func NewScreenFramebuffer(width, height int) *Framebuffer {
} }
} }
// ProjectionMatrix returns a projection matrix of the framebuffer. // projectionMatrix returns a projection matrix of the framebuffer.
// //
// A projection matrix converts the coodinates on the framebuffer // A projection matrix converts the coodinates on the framebuffer
// (0, 0) - (viewport width, viewport height) // (0, 0) - (viewport width, viewport height)
// to the normalized device coodinates (-1, -1) - (1, 1) with adjustment. // to the normalized device coodinates (-1, -1) - (1, 1) with adjustment.
func (f *Framebuffer) ProjectionMatrix() []float32 { func (f *Framebuffer) projectionMatrix() []float32 {
if f.proMatrix != nil { if f.proMatrix != nil {
return f.proMatrix return f.proMatrix
} }
@ -59,7 +59,7 @@ func (f *Framebuffer) ProjectionMatrix() []float32 {
return f.proMatrix return f.proMatrix
} }
func (f *Framebuffer) Delete() { func (f *Framebuffer) delete() {
if f.native != theContext.getScreenFramebuffer() { if f.native != theContext.getScreenFramebuffer() {
theContext.deleteFramebuffer(f.native) theContext.deleteFramebuffer(f.native)
} }

View File

@ -14,6 +14,10 @@
package opengl package opengl
import (
"github.com/hajimehoshi/ebiten/internal/math"
)
type Image struct { type Image struct {
Texture Texture Texture Texture
Framebuffer *Framebuffer Framebuffer *Framebuffer
@ -38,9 +42,49 @@ func (i *Image) IsInvalidated() bool {
func (i *Image) Delete() { func (i *Image) Delete() {
if i.Framebuffer != nil { if i.Framebuffer != nil {
i.Framebuffer.Delete() i.Framebuffer.delete()
} }
if i.Texture != *new(Texture) { if i.Texture != *new(Texture) {
theContext.deleteTexture(i.Texture) theContext.deleteTexture(i.Texture)
} }
} }
func (i *Image) SetViewport() error {
if err := i.ensureFramebuffer(); err != nil {
return err
}
theContext.setViewport(i.Framebuffer)
return nil
}
func (i *Image) Pixels() ([]byte, error) {
if err := i.ensureFramebuffer(); err != nil {
return nil, err
}
w, h := i.Size()
p, err := theContext.FramebufferPixels(i.Framebuffer, w, h)
if err != nil {
return nil, err
}
return p, nil
}
func (i *Image) ProjectionMatrix() []float32 {
if i.Framebuffer == nil {
panic("not reached")
}
return i.Framebuffer.projectionMatrix()
}
func (i *Image) ensureFramebuffer() error {
if i.Framebuffer != nil {
return nil
}
w, h := i.Size()
f, err := NewFramebufferFromTexture(i.Texture, math.NextPowerOf2Int(w), math.NextPowerOf2Int(h))
if err != nil {
return err
}
i.Framebuffer = f
return nil
}