mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-10 04:57:26 +01:00
graphics: Create framebuffers lazily
This commit is contained in:
parent
924c55ff5c
commit
15f7a605c5
@ -145,7 +145,11 @@ type fillCommand struct {
|
||||
}
|
||||
|
||||
func (c *fillCommand) Exec(context *opengl.Context, indexOffsetInBytes int) error {
|
||||
if err := c.dst.framebuffer.setAsViewport(context); err != nil {
|
||||
f, err := c.dst.createFramebufferIfNeeded(context)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := f.setAsViewport(context); err != nil {
|
||||
return err
|
||||
}
|
||||
cr, cg, cb, ca := c.color.R, c.color.G, c.color.B, c.color.A
|
||||
@ -170,7 +174,11 @@ func QuadVertexSizeInBytes() int {
|
||||
}
|
||||
|
||||
func (c *drawImageCommand) Exec(context *opengl.Context, indexOffsetInBytes int) error {
|
||||
if err := c.dst.framebuffer.setAsViewport(context); err != nil {
|
||||
f, err := c.dst.createFramebufferIfNeeded(context)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := f.setAsViewport(context); err != nil {
|
||||
return err
|
||||
}
|
||||
context.BlendFunc(c.mode)
|
||||
@ -180,7 +188,7 @@ func (c *drawImageCommand) Exec(context *opengl.Context, indexOffsetInBytes int)
|
||||
return nil
|
||||
}
|
||||
_, h := c.dst.Size()
|
||||
proj := c.dst.framebuffer.projectionMatrix(h)
|
||||
proj := f.projectionMatrix(h)
|
||||
p := programContext{
|
||||
state: &theOpenGLState,
|
||||
program: theOpenGLState.programTexture,
|
||||
@ -234,7 +242,11 @@ type replacePixelsCommand struct {
|
||||
}
|
||||
|
||||
func (c *replacePixelsCommand) Exec(context *opengl.Context, indexOffsetInBytes int) error {
|
||||
if err := c.dst.framebuffer.setAsViewport(context); err != nil {
|
||||
f, err := c.dst.createFramebufferIfNeeded(context)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := f.setAsViewport(context); err != nil {
|
||||
return err
|
||||
}
|
||||
// Filling with non black or white color is required here for glTexSubImage2D.
|
||||
@ -314,10 +326,6 @@ func (c *newImageFromImageCommand) Exec(context *opengl.Context, indexOffsetInBy
|
||||
c.result.texture = &texture{
|
||||
native: native,
|
||||
}
|
||||
c.result.framebuffer, err = newFramebufferFromTexture(context, c.result.texture)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -344,10 +352,6 @@ func (c *newImageCommand) Exec(context *opengl.Context, indexOffsetInBytes int)
|
||||
c.result.texture = &texture{
|
||||
native: native,
|
||||
}
|
||||
c.result.framebuffer, err = newFramebufferFromTexture(context, c.result.texture)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
@ -114,7 +114,10 @@ func (i *Image) Pixels(context *opengl.Context) ([]uint8, error) {
|
||||
if err := theCommandQueue.Flush(context); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
f := i.framebuffer
|
||||
f, err := i.createFramebufferIfNeeded(context)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return context.FramebufferPixels(f.native, i.width, i.height)
|
||||
}
|
||||
|
||||
@ -132,3 +135,15 @@ func (i *Image) ReplacePixels(p []uint8) error {
|
||||
func (i *Image) IsInvalidated(context *opengl.Context) bool {
|
||||
return !context.IsTexture(i.texture.native)
|
||||
}
|
||||
|
||||
func (i *Image) createFramebufferIfNeeded(context *opengl.Context) (*framebuffer, error) {
|
||||
if i.framebuffer != nil {
|
||||
return i.framebuffer, nil
|
||||
}
|
||||
f, err := newFramebufferFromTexture(context, i.texture)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
i.framebuffer = f
|
||||
return i.framebuffer, nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user