graphics: Create framebuffers lazily

This commit is contained in:
Hajime Hoshi 2016-12-14 23:40:43 +09:00
parent 924c55ff5c
commit 15f7a605c5
2 changed files with 32 additions and 13 deletions

View File

@ -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
}

View File

@ -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
}