Move CompositeMode to graphics package

This commit is contained in:
Hajime Hoshi 2018-10-28 20:42:57 +09:00
parent 06f2052817
commit ff54d3b681
14 changed files with 186 additions and 136 deletions

View File

@ -16,7 +16,6 @@ package ebiten
import ( import (
"github.com/hajimehoshi/ebiten/internal/graphics" "github.com/hajimehoshi/ebiten/internal/graphics"
"github.com/hajimehoshi/ebiten/internal/opengl"
) )
// Filter represents the type of texture filter to be used when an image is maginified or minified. // Filter represents the type of texture filter to be used when an image is maginified or minified.
@ -46,42 +45,42 @@ type CompositeMode int
const ( const (
// Regular alpha blending // Regular alpha blending
// c_out = c_src + c_dst × (1 - α_src) // c_out = c_src + c_dst × (1 - α_src)
CompositeModeSourceOver CompositeMode = CompositeMode(opengl.CompositeModeSourceOver) CompositeModeSourceOver CompositeMode = CompositeMode(graphics.CompositeModeSourceOver)
// c_out = 0 // c_out = 0
CompositeModeClear CompositeMode = CompositeMode(opengl.CompositeModeClear) CompositeModeClear CompositeMode = CompositeMode(graphics.CompositeModeClear)
// c_out = c_src // c_out = c_src
CompositeModeCopy CompositeMode = CompositeMode(opengl.CompositeModeCopy) CompositeModeCopy CompositeMode = CompositeMode(graphics.CompositeModeCopy)
// c_out = c_dst // c_out = c_dst
CompositeModeDestination CompositeMode = CompositeMode(opengl.CompositeModeDestination) CompositeModeDestination CompositeMode = CompositeMode(graphics.CompositeModeDestination)
// c_out = c_src × (1 - α_dst) + c_dst // c_out = c_src × (1 - α_dst) + c_dst
CompositeModeDestinationOver CompositeMode = CompositeMode(opengl.CompositeModeDestinationOver) CompositeModeDestinationOver CompositeMode = CompositeMode(graphics.CompositeModeDestinationOver)
// c_out = c_src × α_dst // c_out = c_src × α_dst
CompositeModeSourceIn CompositeMode = CompositeMode(opengl.CompositeModeSourceIn) CompositeModeSourceIn CompositeMode = CompositeMode(graphics.CompositeModeSourceIn)
// c_out = c_dst × α_src // c_out = c_dst × α_src
CompositeModeDestinationIn CompositeMode = CompositeMode(opengl.CompositeModeDestinationIn) CompositeModeDestinationIn CompositeMode = CompositeMode(graphics.CompositeModeDestinationIn)
// c_out = c_src × (1 - α_dst) // c_out = c_src × (1 - α_dst)
CompositeModeSourceOut CompositeMode = CompositeMode(opengl.CompositeModeSourceOut) CompositeModeSourceOut CompositeMode = CompositeMode(graphics.CompositeModeSourceOut)
// c_out = c_dst × (1 - α_src) // c_out = c_dst × (1 - α_src)
CompositeModeDestinationOut CompositeMode = CompositeMode(opengl.CompositeModeDestinationOut) CompositeModeDestinationOut CompositeMode = CompositeMode(graphics.CompositeModeDestinationOut)
// c_out = c_src × α_dst + c_dst × (1 - α_src) // c_out = c_src × α_dst + c_dst × (1 - α_src)
CompositeModeSourceAtop CompositeMode = CompositeMode(opengl.CompositeModeSourceAtop) CompositeModeSourceAtop CompositeMode = CompositeMode(graphics.CompositeModeSourceAtop)
// c_out = c_src × (1 - α_dst) + c_dst × α_src // c_out = c_src × (1 - α_dst) + c_dst × α_src
CompositeModeDestinationAtop CompositeMode = CompositeMode(opengl.CompositeModeDestinationAtop) CompositeModeDestinationAtop CompositeMode = CompositeMode(graphics.CompositeModeDestinationAtop)
// c_out = c_src × (1 - α_dst) + c_dst × (1 - α_src) // c_out = c_src × (1 - α_dst) + c_dst × (1 - α_src)
CompositeModeXor CompositeMode = CompositeMode(opengl.CompositeModeXor) CompositeModeXor CompositeMode = CompositeMode(graphics.CompositeModeXor)
// Sum of source and destination (a.k.a. 'plus' or 'additive') // Sum of source and destination (a.k.a. 'plus' or 'additive')
// c_out = c_src + c_dst // c_out = c_src + c_dst
CompositeModeLighter CompositeMode = CompositeMode(opengl.CompositeModeLighter) CompositeModeLighter CompositeMode = CompositeMode(graphics.CompositeModeLighter)
) )

View File

@ -22,7 +22,6 @@ import (
"github.com/hajimehoshi/ebiten/internal/graphics" "github.com/hajimehoshi/ebiten/internal/graphics"
"github.com/hajimehoshi/ebiten/internal/graphicsutil" "github.com/hajimehoshi/ebiten/internal/graphicsutil"
"github.com/hajimehoshi/ebiten/internal/opengl"
"github.com/hajimehoshi/ebiten/internal/shareable" "github.com/hajimehoshi/ebiten/internal/shareable"
) )
@ -92,7 +91,7 @@ func (m *mipmap) level(r image.Rectangle, level int) *shareable.Image {
vs = src.QuadVertices(0, 0, w, h, 0.5, 0, 0, 0.5, 0, 0, 1, 1, 1, 1) vs = src.QuadVertices(0, 0, w, h, 0.5, 0, 0, 0.5, 0, 0, 1, 1, 1, 1)
} }
is := graphicsutil.QuadIndices() is := graphicsutil.QuadIndices()
s.DrawImage(src, vs, is, nil, opengl.CompositeModeCopy, graphics.FilterLinear) s.DrawImage(src, vs, is, nil, graphics.CompositeModeCopy, graphics.FilterLinear)
imgs = append(imgs, s) imgs = append(imgs, s)
w = w2 w = w2
h = h2 h = h2
@ -374,7 +373,7 @@ func (i *Image) drawImage(img *Image, options *DrawImageOptions) {
geom.Concat(options.GeoM) geom.Concat(options.GeoM)
} }
mode := opengl.CompositeMode(options.CompositeMode) mode := graphics.CompositeMode(options.CompositeMode)
filter := graphics.FilterNearest filter := graphics.FilterNearest
if options.Filter != FilterDefault { if options.Filter != FilterDefault {
@ -509,7 +508,7 @@ func (i *Image) DrawTriangles(vertices []Vertex, indices []uint16, img *Image, o
options = &DrawTrianglesOptions{} options = &DrawTrianglesOptions{}
} }
mode := opengl.CompositeMode(options.CompositeMode) mode := graphics.CompositeMode(options.CompositeMode)
filter := graphics.FilterNearest filter := graphics.FilterNearest
if options.Filter != FilterDefault { if options.Filter != FilterDefault {

View File

@ -0,0 +1,78 @@
// Copyright 2018 The Ebiten Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package graphics
type CompositeMode int
const (
CompositeModeSourceOver CompositeMode = iota // This value must be 0 (= initial value)
CompositeModeClear
CompositeModeCopy
CompositeModeDestination
CompositeModeDestinationOver
CompositeModeSourceIn
CompositeModeDestinationIn
CompositeModeSourceOut
CompositeModeDestinationOut
CompositeModeSourceAtop
CompositeModeDestinationAtop
CompositeModeXor
CompositeModeLighter
CompositeModeUnknown
)
type Operation int
const (
Zero Operation = iota
One
SrcAlpha
DstAlpha
OneMinusSrcAlpha
OneMinusDstAlpha
)
func (c CompositeMode) Operations() (src Operation, dst Operation) {
switch c {
case CompositeModeSourceOver:
return One, OneMinusSrcAlpha
case CompositeModeClear:
return Zero, Zero
case CompositeModeCopy:
return One, Zero
case CompositeModeDestination:
return Zero, One
case CompositeModeDestinationOver:
return OneMinusDstAlpha, One
case CompositeModeSourceIn:
return DstAlpha, Zero
case CompositeModeDestinationIn:
return Zero, SrcAlpha
case CompositeModeSourceOut:
return OneMinusDstAlpha, Zero
case CompositeModeDestinationOut:
return Zero, OneMinusSrcAlpha
case CompositeModeSourceAtop:
return DstAlpha, OneMinusSrcAlpha
case CompositeModeDestinationAtop:
return OneMinusDstAlpha, SrcAlpha
case CompositeModeXor:
return OneMinusDstAlpha, OneMinusSrcAlpha
case CompositeModeLighter:
return One, One
default:
panic("not reached")
}
}

View File

@ -37,7 +37,7 @@ type command interface {
NumIndices() int NumIndices() int
AddNumVertices(n int) AddNumVertices(n int)
AddNumIndices(n int) AddNumIndices(n int)
CanMerge(dst, src *Image, color *affine.ColorM, mode opengl.CompositeMode, filter graphics.Filter) bool CanMerge(dst, src *Image, color *affine.ColorM, mode graphics.CompositeMode, filter graphics.Filter) bool
} }
// commandQueue is a command queue for drawing commands. // commandQueue is a command queue for drawing commands.
@ -89,7 +89,7 @@ func (q *commandQueue) appendIndices(indices []uint16, offset uint16) {
q.nindices += len(indices) q.nindices += len(indices)
} }
func (q *commandQueue) doEnqueueDrawImageCommand(dst, src *Image, nvertices, nindices int, color *affine.ColorM, mode opengl.CompositeMode, filter graphics.Filter, forceNewCommand bool) { func (q *commandQueue) doEnqueueDrawImageCommand(dst, src *Image, nvertices, nindices int, color *affine.ColorM, mode graphics.CompositeMode, filter graphics.Filter, forceNewCommand bool) {
if nindices > indicesNum { if nindices > indicesNum {
panic("not implemented for too many indices") panic("not implemented for too many indices")
} }
@ -113,7 +113,7 @@ func (q *commandQueue) doEnqueueDrawImageCommand(dst, src *Image, nvertices, nin
} }
// EnqueueDrawImageCommand enqueues a drawing-image command. // EnqueueDrawImageCommand enqueues a drawing-image command.
func (q *commandQueue) EnqueueDrawImageCommand(dst, src *Image, vertices []float32, indices []uint16, color *affine.ColorM, mode opengl.CompositeMode, filter graphics.Filter) { func (q *commandQueue) EnqueueDrawImageCommand(dst, src *Image, vertices []float32, indices []uint16, color *affine.ColorM, mode graphics.CompositeMode, filter graphics.Filter) {
if len(indices) > indicesNum { if len(indices) > indicesNum {
panic("not reached") panic("not reached")
} }
@ -221,7 +221,7 @@ type drawImageCommand struct {
nvertices int nvertices int
nindices int nindices int
color *affine.ColorM color *affine.ColorM
mode opengl.CompositeMode mode graphics.CompositeMode
filter graphics.Filter filter graphics.Filter
} }
@ -276,7 +276,7 @@ func (c *drawImageCommand) AddNumIndices(n int) {
// CanMerge returns a boolean value indicating whether the other drawImageCommand can be merged // CanMerge returns a boolean value indicating whether the other drawImageCommand can be merged
// with the drawImageCommand c. // with the drawImageCommand c.
func (c *drawImageCommand) CanMerge(dst, src *Image, color *affine.ColorM, mode opengl.CompositeMode, filter graphics.Filter) bool { func (c *drawImageCommand) CanMerge(dst, src *Image, color *affine.ColorM, mode graphics.CompositeMode, filter graphics.Filter) bool {
if c.dst != dst { if c.dst != dst {
return false return false
} }
@ -333,7 +333,7 @@ func (c *replacePixelsCommand) AddNumVertices(n int) {
func (c *replacePixelsCommand) AddNumIndices(n int) { func (c *replacePixelsCommand) AddNumIndices(n int) {
} }
func (c *replacePixelsCommand) CanMerge(dst, src *Image, color *affine.ColorM, mode opengl.CompositeMode, filter graphics.Filter) bool { func (c *replacePixelsCommand) CanMerge(dst, src *Image, color *affine.ColorM, mode graphics.CompositeMode, filter graphics.Filter) bool {
return false return false
} }
@ -374,7 +374,7 @@ func (c *pixelsCommand) AddNumVertices(n int) {
func (c *pixelsCommand) AddNumIndices(n int) { func (c *pixelsCommand) AddNumIndices(n int) {
} }
func (c *pixelsCommand) CanMerge(dst, src *Image, color *affine.ColorM, mode opengl.CompositeMode, filter graphics.Filter) bool { func (c *pixelsCommand) CanMerge(dst, src *Image, color *affine.ColorM, mode graphics.CompositeMode, filter graphics.Filter) bool {
return false return false
} }
@ -413,7 +413,7 @@ func (c *disposeCommand) AddNumVertices(n int) {
func (c *disposeCommand) AddNumIndices(n int) { func (c *disposeCommand) AddNumIndices(n int) {
} }
func (c *disposeCommand) CanMerge(dst, src *Image, color *affine.ColorM, mode opengl.CompositeMode, filter graphics.Filter) bool { func (c *disposeCommand) CanMerge(dst, src *Image, color *affine.ColorM, mode graphics.CompositeMode, filter graphics.Filter) bool {
return false return false
} }
@ -473,7 +473,7 @@ func (c *newImageCommand) AddNumVertices(n int) {
func (c *newImageCommand) AddNumIndices(n int) { func (c *newImageCommand) AddNumIndices(n int) {
} }
func (c *newImageCommand) CanMerge(dst, src *Image, color *affine.ColorM, mode opengl.CompositeMode, filter graphics.Filter) bool { func (c *newImageCommand) CanMerge(dst, src *Image, color *affine.ColorM, mode graphics.CompositeMode, filter graphics.Filter) bool {
return false return false
} }
@ -512,6 +512,6 @@ func (c *newScreenFramebufferImageCommand) AddNumVertices(n int) {
func (c *newScreenFramebufferImageCommand) AddNumIndices(n int) { func (c *newScreenFramebufferImageCommand) AddNumIndices(n int) {
} }
func (c *newScreenFramebufferImageCommand) CanMerge(dst, src *Image, color *affine.ColorM, mode opengl.CompositeMode, filter graphics.Filter) bool { func (c *newScreenFramebufferImageCommand) CanMerge(dst, src *Image, color *affine.ColorM, mode graphics.CompositeMode, filter graphics.Filter) bool {
return false return false
} }

View File

@ -87,7 +87,7 @@ func (i *Image) Size() (int, int) {
return i.width, i.height return i.width, i.height
} }
func (i *Image) DrawImage(src *Image, vertices []float32, indices []uint16, clr *affine.ColorM, mode opengl.CompositeMode, filter graphics.Filter) { func (i *Image) DrawImage(src *Image, vertices []float32, indices []uint16, clr *affine.ColorM, mode graphics.CompositeMode, filter graphics.Filter) {
theCommandQueue.EnqueueDrawImageCommand(i, src, vertices, indices, clr, mode, filter) theCommandQueue.EnqueueDrawImageCommand(i, src, vertices, indices, clr, mode, filter)
} }

View File

@ -16,6 +16,8 @@ package opengl
import ( import (
"math" "math"
"github.com/hajimehoshi/ebiten/internal/graphics"
) )
var ( var (
@ -43,6 +45,25 @@ var (
oneMinusDstAlpha operation oneMinusDstAlpha operation
) )
func convertOperation(op graphics.Operation) operation {
switch op {
case graphics.Zero:
return zero
case graphics.One:
return one
case graphics.SrcAlpha:
return srcAlpha
case graphics.DstAlpha:
return dstAlpha
case graphics.OneMinusSrcAlpha:
return oneMinusSrcAlpha
case graphics.OneMinusDstAlpha:
return oneMinusDstAlpha
default:
panic("not reached")
}
}
type Context struct { type Context struct {
locationCache *locationCache locationCache *locationCache
screenFramebuffer Framebuffer // This might not be the default frame buffer '0' (e.g. iOS). screenFramebuffer Framebuffer // This might not be the default frame buffer '0' (e.g. iOS).
@ -50,7 +71,7 @@ type Context struct {
lastTexture Texture lastTexture Texture
lastViewportWidth int lastViewportWidth int
lastViewportHeight int lastViewportHeight int
lastCompositeMode CompositeMode lastCompositeMode graphics.CompositeMode
maxTextureSize int maxTextureSize int
context context
} }

View File

@ -24,6 +24,8 @@ import (
"fmt" "fmt"
"github.com/go-gl/gl/v2.1/gl" "github.com/go-gl/gl/v2.1/gl"
"github.com/hajimehoshi/ebiten/internal/graphics"
) )
type ( type (
@ -105,12 +107,12 @@ func (c *Context) Reset() error {
c.lastFramebuffer = invalidFramebuffer c.lastFramebuffer = invalidFramebuffer
c.lastViewportWidth = 0 c.lastViewportWidth = 0
c.lastViewportHeight = 0 c.lastViewportHeight = 0
c.lastCompositeMode = CompositeModeUnknown c.lastCompositeMode = graphics.CompositeModeUnknown
_ = c.runOnContextThread(func() error { _ = c.runOnContextThread(func() error {
gl.Enable(gl.BLEND) gl.Enable(gl.BLEND)
return nil return nil
}) })
c.BlendFunc(CompositeModeSourceOver) c.BlendFunc(graphics.CompositeModeSourceOver)
_ = c.runOnContextThread(func() error { _ = c.runOnContextThread(func() error {
f := int32(0) f := int32(0)
gl.GetIntegerv(gl.FRAMEBUFFER_BINDING, &f) gl.GetIntegerv(gl.FRAMEBUFFER_BINDING, &f)
@ -120,14 +122,15 @@ func (c *Context) Reset() error {
return nil return nil
} }
func (c *Context) BlendFunc(mode CompositeMode) { func (c *Context) BlendFunc(mode graphics.CompositeMode) {
_ = c.runOnContextThread(func() error { _ = c.runOnContextThread(func() error {
if c.lastCompositeMode == mode { if c.lastCompositeMode == mode {
return nil return nil
} }
c.lastCompositeMode = mode c.lastCompositeMode = mode
s, d := operations(mode) s, d := mode.Operations()
gl.BlendFunc(uint32(s), uint32(d)) s2, d2 := convertOperation(s), convertOperation(d)
gl.BlendFunc(uint32(s2), uint32(d2))
return nil return nil
}) })
} }

View File

@ -21,6 +21,8 @@ import (
"fmt" "fmt"
"github.com/gopherjs/gopherwasm/js" "github.com/gopherjs/gopherwasm/js"
"github.com/hajimehoshi/ebiten/internal/graphics"
) )
type ( type (
@ -149,23 +151,24 @@ func (c *Context) Reset() error {
c.lastFramebuffer = Framebuffer(js.Null()) c.lastFramebuffer = Framebuffer(js.Null())
c.lastViewportWidth = 0 c.lastViewportWidth = 0
c.lastViewportHeight = 0 c.lastViewportHeight = 0
c.lastCompositeMode = CompositeModeUnknown c.lastCompositeMode = graphics.CompositeModeUnknown
gl := c.gl gl := c.gl
gl.Call("enable", blend) gl.Call("enable", blend)
c.BlendFunc(CompositeModeSourceOver) c.BlendFunc(graphics.CompositeModeSourceOver)
f := gl.Call("getParameter", framebufferBinding) f := gl.Call("getParameter", framebufferBinding)
c.screenFramebuffer = Framebuffer(f) c.screenFramebuffer = Framebuffer(f)
return nil return nil
} }
func (c *Context) BlendFunc(mode CompositeMode) { func (c *Context) BlendFunc(mode graphics.CompositeMode) {
if c.lastCompositeMode == mode { if c.lastCompositeMode == mode {
return return
} }
c.lastCompositeMode = mode c.lastCompositeMode = mode
s, d := operations(mode) s, d := mode.Operations()
s2, d2 := convertOperation(s), convertOperation(d)
gl := c.gl gl := c.gl
gl.Call("blendFunc", int(s), int(d)) gl.Call("blendFunc", int(s2), int(d2))
} }
func (c *Context) NewTexture(width, height int) (Texture, error) { func (c *Context) NewTexture(width, height int) (Texture, error) {

View File

@ -21,6 +21,8 @@ import (
"fmt" "fmt"
mgl "golang.org/x/mobile/gl" mgl "golang.org/x/mobile/gl"
"github.com/hajimehoshi/ebiten/internal/graphics"
) )
type ( type (
@ -111,23 +113,24 @@ func (c *Context) Reset() error {
c.lastFramebuffer = invalidFramebuffer c.lastFramebuffer = invalidFramebuffer
c.lastViewportWidth = 0 c.lastViewportWidth = 0
c.lastViewportHeight = 0 c.lastViewportHeight = 0
c.lastCompositeMode = CompositeModeUnknown c.lastCompositeMode = graphics.CompositeModeUnknown
c.gl.Enable(mgl.BLEND) c.gl.Enable(mgl.BLEND)
c.BlendFunc(CompositeModeSourceOver) c.BlendFunc(graphics.CompositeModeSourceOver)
f := c.gl.GetInteger(mgl.FRAMEBUFFER_BINDING) f := c.gl.GetInteger(mgl.FRAMEBUFFER_BINDING)
c.screenFramebuffer = Framebuffer(mgl.Framebuffer{uint32(f)}) c.screenFramebuffer = Framebuffer(mgl.Framebuffer{uint32(f)})
// TODO: Need to update screenFramebufferWidth/Height? // TODO: Need to update screenFramebufferWidth/Height?
return nil return nil
} }
func (c *Context) BlendFunc(mode CompositeMode) { func (c *Context) BlendFunc(mode graphics.CompositeMode) {
gl := c.gl gl := c.gl
if c.lastCompositeMode == mode { if c.lastCompositeMode == mode {
return return
} }
c.lastCompositeMode = mode c.lastCompositeMode = mode
s, d := operations(mode) s, d := mode.Operations()
gl.BlendFunc(mgl.Enum(s), mgl.Enum(d)) s2, d2 := convertOperation(s), convertOperation(d)
gl.BlendFunc(mgl.Enum(s2), mgl.Enum(d2))
} }
func (c *Context) NewTexture(width, height int) (Texture, error) { func (c *Context) NewTexture(width, height int) (Texture, error) {

View File

@ -22,58 +22,6 @@ type (
operation int operation int
) )
type CompositeMode int
const (
CompositeModeSourceOver CompositeMode = iota // This value must be 0 (= initial value)
CompositeModeClear
CompositeModeCopy
CompositeModeDestination
CompositeModeDestinationOver
CompositeModeSourceIn
CompositeModeDestinationIn
CompositeModeSourceOut
CompositeModeDestinationOut
CompositeModeSourceAtop
CompositeModeDestinationAtop
CompositeModeXor
CompositeModeLighter
CompositeModeUnknown
)
func operations(mode CompositeMode) (src operation, dst operation) {
switch mode {
case CompositeModeSourceOver:
return one, oneMinusSrcAlpha
case CompositeModeClear:
return zero, zero
case CompositeModeCopy:
return one, zero
case CompositeModeDestination:
return zero, one
case CompositeModeDestinationOver:
return oneMinusDstAlpha, one
case CompositeModeSourceIn:
return dstAlpha, zero
case CompositeModeDestinationIn:
return zero, srcAlpha
case CompositeModeSourceOut:
return oneMinusDstAlpha, zero
case CompositeModeDestinationOut:
return zero, oneMinusSrcAlpha
case CompositeModeSourceAtop:
return dstAlpha, oneMinusSrcAlpha
case CompositeModeDestinationAtop:
return oneMinusDstAlpha, srcAlpha
case CompositeModeXor:
return oneMinusDstAlpha, oneMinusSrcAlpha
case CompositeModeLighter:
return one, one
default:
panic("not reached")
}
}
type DataType int type DataType int
func (d DataType) SizeInBytes() int { func (d DataType) SizeInBytes() int {

View File

@ -24,7 +24,6 @@ import (
"github.com/hajimehoshi/ebiten/internal/graphicscommand" "github.com/hajimehoshi/ebiten/internal/graphicscommand"
"github.com/hajimehoshi/ebiten/internal/graphicsutil" "github.com/hajimehoshi/ebiten/internal/graphicsutil"
"github.com/hajimehoshi/ebiten/internal/math" "github.com/hajimehoshi/ebiten/internal/math"
"github.com/hajimehoshi/ebiten/internal/opengl"
) )
// drawImageHistoryItem is an item for history of draw-image commands. // drawImageHistoryItem is an item for history of draw-image commands.
@ -33,7 +32,7 @@ type drawImageHistoryItem struct {
vertices []float32 vertices []float32
indices []uint16 indices []uint16
colorm *affine.ColorM colorm *affine.ColorM
mode opengl.CompositeMode mode graphics.CompositeMode
filter graphics.Filter filter graphics.Filter
} }
@ -167,7 +166,7 @@ func (i *Image) ReplacePixels(pixels []byte, x, y, width, height int) {
float32(x), float32(y), float32(x), float32(y),
1, 1, 1, 1) 1, 1, 1, 1)
is := graphicsutil.QuadIndices() is := graphicsutil.QuadIndices()
i.image.DrawImage(dummyImage.image, vs, is, colorm, opengl.CompositeModeCopy, graphics.FilterNearest) i.image.DrawImage(dummyImage.image, vs, is, colorm, graphics.CompositeModeCopy, graphics.FilterNearest)
} }
if x == 0 && y == 0 && width == w && height == h { if x == 0 && y == 0 && width == w && height == h {
@ -204,7 +203,7 @@ func (i *Image) ReplacePixels(pixels []byte, x, y, width, height int) {
} }
// DrawImage draws a given image img to the image. // DrawImage draws a given image img to the image.
func (i *Image) DrawImage(img *Image, vertices []float32, indices []uint16, colorm *affine.ColorM, mode opengl.CompositeMode, filter graphics.Filter) { func (i *Image) DrawImage(img *Image, vertices []float32, indices []uint16, colorm *affine.ColorM, mode graphics.CompositeMode, filter graphics.Filter) {
if len(vertices) == 0 { if len(vertices) == 0 {
return return
} }
@ -219,7 +218,7 @@ func (i *Image) DrawImage(img *Image, vertices []float32, indices []uint16, colo
} }
// appendDrawImageHistory appends a draw-image history item to the image. // appendDrawImageHistory appends a draw-image history item to the image.
func (i *Image) appendDrawImageHistory(image *Image, vertices []float32, indices []uint16, colorm *affine.ColorM, mode opengl.CompositeMode, filter graphics.Filter) { func (i *Image) appendDrawImageHistory(image *Image, vertices []float32, indices []uint16, colorm *affine.ColorM, mode graphics.CompositeMode, filter graphics.Filter) {
if i.stale || i.volatile || i.screen { if i.stale || i.volatile || i.screen {
return return
} }

View File

@ -24,7 +24,6 @@ import (
"github.com/hajimehoshi/ebiten" "github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/internal/graphics" "github.com/hajimehoshi/ebiten/internal/graphics"
"github.com/hajimehoshi/ebiten/internal/graphicsutil" "github.com/hajimehoshi/ebiten/internal/graphicsutil"
"github.com/hajimehoshi/ebiten/internal/opengl"
. "github.com/hajimehoshi/ebiten/internal/restorable" . "github.com/hajimehoshi/ebiten/internal/restorable"
"github.com/hajimehoshi/ebiten/internal/testflock" "github.com/hajimehoshi/ebiten/internal/testflock"
) )
@ -118,7 +117,7 @@ func TestRestoreChain(t *testing.T) {
w, h := imgs[i].Size() w, h := imgs[i].Size()
vs := graphicsutil.QuadVertices(w, h, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1) vs := graphicsutil.QuadVertices(w, h, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1)
is := graphicsutil.QuadIndices() is := graphicsutil.QuadIndices()
imgs[i+1].DrawImage(imgs[i], vs, is, nil, opengl.CompositeModeCopy, graphics.FilterNearest) imgs[i+1].DrawImage(imgs[i], vs, is, nil, graphics.CompositeModeCopy, graphics.FilterNearest)
} }
ResolveStaleImages() ResolveStaleImages()
if err := Restore(); err != nil { if err := Restore(); err != nil {
@ -160,10 +159,10 @@ func TestRestoreChain2(t *testing.T) {
vs := graphicsutil.QuadVertices(w, h, 0, 0, w, h, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1) vs := graphicsutil.QuadVertices(w, h, 0, 0, w, h, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1)
is := graphicsutil.QuadIndices() is := graphicsutil.QuadIndices()
imgs[8].DrawImage(imgs[7], vs, is, nil, opengl.CompositeModeCopy, graphics.FilterNearest) imgs[8].DrawImage(imgs[7], vs, is, nil, graphics.CompositeModeCopy, graphics.FilterNearest)
imgs[9].DrawImage(imgs[8], vs, is, nil, opengl.CompositeModeCopy, graphics.FilterNearest) imgs[9].DrawImage(imgs[8], vs, is, nil, graphics.CompositeModeCopy, graphics.FilterNearest)
for i := 0; i < 7; i++ { for i := 0; i < 7; i++ {
imgs[i+1].DrawImage(imgs[i], vs, is, nil, opengl.CompositeModeCopy, graphics.FilterNearest) imgs[i+1].DrawImage(imgs[i], vs, is, nil, graphics.CompositeModeCopy, graphics.FilterNearest)
} }
ResolveStaleImages() ResolveStaleImages()
@ -206,10 +205,10 @@ func TestRestoreOverrideSource(t *testing.T) {
fill(img1, clr0.R, clr0.G, clr0.B, clr0.A) fill(img1, clr0.R, clr0.G, clr0.B, clr0.A)
vs := graphicsutil.QuadVertices(w, h, 0, 0, w, h, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1) vs := graphicsutil.QuadVertices(w, h, 0, 0, w, h, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1)
is := graphicsutil.QuadIndices() is := graphicsutil.QuadIndices()
img2.DrawImage(img1, vs, is, nil, opengl.CompositeModeSourceOver, graphics.FilterNearest) img2.DrawImage(img1, vs, is, nil, graphics.CompositeModeSourceOver, graphics.FilterNearest)
img3.DrawImage(img2, vs, is, nil, opengl.CompositeModeSourceOver, graphics.FilterNearest) img3.DrawImage(img2, vs, is, nil, graphics.CompositeModeSourceOver, graphics.FilterNearest)
fill(img0, clr1.R, clr1.G, clr1.B, clr1.A) fill(img0, clr1.R, clr1.G, clr1.B, clr1.A)
img1.DrawImage(img0, vs, is, nil, opengl.CompositeModeSourceOver, graphics.FilterNearest) img1.DrawImage(img0, vs, is, nil, graphics.CompositeModeSourceOver, graphics.FilterNearest)
ResolveStaleImages() ResolveStaleImages()
if err := Restore(); err != nil { if err := Restore(); err != nil {
t.Fatal(err) t.Fatal(err)
@ -291,23 +290,23 @@ func TestRestoreComplexGraph(t *testing.T) {
}() }()
vs := graphicsutil.QuadVertices(w, h, 0, 0, w, h, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1) vs := graphicsutil.QuadVertices(w, h, 0, 0, w, h, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1)
is := graphicsutil.QuadIndices() is := graphicsutil.QuadIndices()
img3.DrawImage(img0, vs, is, nil, opengl.CompositeModeSourceOver, graphics.FilterNearest) img3.DrawImage(img0, vs, is, nil, graphics.CompositeModeSourceOver, graphics.FilterNearest)
vs = graphicsutil.QuadVertices(w, h, 0, 0, w, h, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1) vs = graphicsutil.QuadVertices(w, h, 0, 0, w, h, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1)
img3.DrawImage(img1, vs, is, nil, opengl.CompositeModeSourceOver, graphics.FilterNearest) img3.DrawImage(img1, vs, is, nil, graphics.CompositeModeSourceOver, graphics.FilterNearest)
vs = graphicsutil.QuadVertices(w, h, 0, 0, w, h, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1) vs = graphicsutil.QuadVertices(w, h, 0, 0, w, h, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1)
img4.DrawImage(img1, vs, is, nil, opengl.CompositeModeSourceOver, graphics.FilterNearest) img4.DrawImage(img1, vs, is, nil, graphics.CompositeModeSourceOver, graphics.FilterNearest)
vs = graphicsutil.QuadVertices(w, h, 0, 0, w, h, 1, 0, 0, 1, 2, 0, 1, 1, 1, 1) vs = graphicsutil.QuadVertices(w, h, 0, 0, w, h, 1, 0, 0, 1, 2, 0, 1, 1, 1, 1)
img4.DrawImage(img2, vs, is, nil, opengl.CompositeModeSourceOver, graphics.FilterNearest) img4.DrawImage(img2, vs, is, nil, graphics.CompositeModeSourceOver, graphics.FilterNearest)
vs = graphicsutil.QuadVertices(w, h, 0, 0, w, h, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1) vs = graphicsutil.QuadVertices(w, h, 0, 0, w, h, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1)
img5.DrawImage(img3, vs, is, nil, opengl.CompositeModeSourceOver, graphics.FilterNearest) img5.DrawImage(img3, vs, is, nil, graphics.CompositeModeSourceOver, graphics.FilterNearest)
vs = graphicsutil.QuadVertices(w, h, 0, 0, w, h, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1) vs = graphicsutil.QuadVertices(w, h, 0, 0, w, h, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1)
img6.DrawImage(img3, vs, is, nil, opengl.CompositeModeSourceOver, graphics.FilterNearest) img6.DrawImage(img3, vs, is, nil, graphics.CompositeModeSourceOver, graphics.FilterNearest)
vs = graphicsutil.QuadVertices(w, h, 0, 0, w, h, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1) vs = graphicsutil.QuadVertices(w, h, 0, 0, w, h, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1)
img6.DrawImage(img4, vs, is, nil, opengl.CompositeModeSourceOver, graphics.FilterNearest) img6.DrawImage(img4, vs, is, nil, graphics.CompositeModeSourceOver, graphics.FilterNearest)
vs = graphicsutil.QuadVertices(w, h, 0, 0, w, h, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1) vs = graphicsutil.QuadVertices(w, h, 0, 0, w, h, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1)
img7.DrawImage(img2, vs, is, nil, opengl.CompositeModeSourceOver, graphics.FilterNearest) img7.DrawImage(img2, vs, is, nil, graphics.CompositeModeSourceOver, graphics.FilterNearest)
vs = graphicsutil.QuadVertices(w, h, 0, 0, w, h, 1, 0, 0, 1, 2, 0, 1, 1, 1, 1) vs = graphicsutil.QuadVertices(w, h, 0, 0, w, h, 1, 0, 0, 1, 2, 0, 1, 1, 1, 1)
img7.DrawImage(img3, vs, is, nil, opengl.CompositeModeSourceOver, graphics.FilterNearest) img7.DrawImage(img3, vs, is, nil, graphics.CompositeModeSourceOver, graphics.FilterNearest)
ResolveStaleImages() ResolveStaleImages()
if err := Restore(); err != nil { if err := Restore(); err != nil {
t.Fatal(err) t.Fatal(err)
@ -399,8 +398,8 @@ func TestRestoreRecursive(t *testing.T) {
}() }()
vs := graphicsutil.QuadVertices(w, h, 0, 0, w, h, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1) vs := graphicsutil.QuadVertices(w, h, 0, 0, w, h, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1)
is := graphicsutil.QuadIndices() is := graphicsutil.QuadIndices()
img1.DrawImage(img0, vs, is, nil, opengl.CompositeModeSourceOver, graphics.FilterNearest) img1.DrawImage(img0, vs, is, nil, graphics.CompositeModeSourceOver, graphics.FilterNearest)
img0.DrawImage(img1, vs, is, nil, opengl.CompositeModeSourceOver, graphics.FilterNearest) img0.DrawImage(img1, vs, is, nil, graphics.CompositeModeSourceOver, graphics.FilterNearest)
ResolveStaleImages() ResolveStaleImages()
if err := Restore(); err != nil { if err := Restore(); err != nil {
t.Fatal(err) t.Fatal(err)
@ -487,7 +486,7 @@ func TestDrawImageAndReplacePixels(t *testing.T) {
vs := graphicsutil.QuadVertices(1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1) vs := graphicsutil.QuadVertices(1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1)
is := graphicsutil.QuadIndices() is := graphicsutil.QuadIndices()
img1.DrawImage(img0, vs, is, nil, opengl.CompositeModeCopy, graphics.FilterNearest) img1.DrawImage(img0, vs, is, nil, graphics.CompositeModeCopy, graphics.FilterNearest)
img1.ReplacePixels([]byte{0xff, 0xff, 0xff, 0xff}, 1, 0, 1, 1) img1.ReplacePixels([]byte{0xff, 0xff, 0xff, 0xff}, 1, 0, 1, 1)
ResolveStaleImages() ResolveStaleImages()
@ -519,8 +518,8 @@ func TestDispose(t *testing.T) {
vs := graphicsutil.QuadVertices(1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1) vs := graphicsutil.QuadVertices(1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1)
is := graphicsutil.QuadIndices() is := graphicsutil.QuadIndices()
img1.DrawImage(img2, vs, is, nil, opengl.CompositeModeCopy, graphics.FilterNearest) img1.DrawImage(img2, vs, is, nil, graphics.CompositeModeCopy, graphics.FilterNearest)
img0.DrawImage(img1, vs, is, nil, opengl.CompositeModeCopy, graphics.FilterNearest) img0.DrawImage(img1, vs, is, nil, graphics.CompositeModeCopy, graphics.FilterNearest)
img1.Dispose() img1.Dispose()
ResolveStaleImages() ResolveStaleImages()
@ -547,7 +546,7 @@ func TestDoubleResolve(t *testing.T) {
vs := graphicsutil.QuadVertices(1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1) vs := graphicsutil.QuadVertices(1, 1, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1)
is := graphicsutil.QuadIndices() is := graphicsutil.QuadIndices()
img0.DrawImage(img1, vs, is, nil, opengl.CompositeModeCopy, graphics.FilterNearest) img0.DrawImage(img1, vs, is, nil, graphics.CompositeModeCopy, graphics.FilterNearest)
img0.ReplacePixels([]uint8{0x00, 0xff, 0x00, 0xff}, 1, 1, 1, 1) img0.ReplacePixels([]uint8{0x00, 0xff, 0x00, 0xff}, 1, 1, 1, 1)
// Now img0 is stale. // Now img0 is stale.
ResolveStaleImages() ResolveStaleImages()

View File

@ -24,7 +24,6 @@ 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/graphicsutil" "github.com/hajimehoshi/ebiten/internal/graphicsutil"
"github.com/hajimehoshi/ebiten/internal/opengl"
"github.com/hajimehoshi/ebiten/internal/packing" "github.com/hajimehoshi/ebiten/internal/packing"
"github.com/hajimehoshi/ebiten/internal/restorable" "github.com/hajimehoshi/ebiten/internal/restorable"
) )
@ -65,7 +64,7 @@ func (b *backend) TryAlloc(width, height int) (*packing.Node, bool) {
w, h := oldImg.Size() w, h := oldImg.Size()
vs := graphicsutil.QuadVertices(w, h, 0, 0, w, h, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1) vs := graphicsutil.QuadVertices(w, h, 0, 0, w, h, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1)
is := graphicsutil.QuadIndices() is := graphicsutil.QuadIndices()
newImg.DrawImage(oldImg, vs, is, nil, opengl.CompositeModeCopy, graphics.FilterNearest) newImg.DrawImage(oldImg, vs, is, nil, graphics.CompositeModeCopy, graphics.FilterNearest)
oldImg.Dispose() oldImg.Dispose()
b.restorable = newImg b.restorable = newImg
@ -131,7 +130,7 @@ func (i *Image) ensureNotShared() {
vw, vh := i.backend.restorable.Size() vw, vh := i.backend.restorable.Size()
vs := graphicsutil.QuadVertices(vw, vh, x, y, x+w, y+h, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1) vs := graphicsutil.QuadVertices(vw, vh, x, y, x+w, y+h, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1)
is := graphicsutil.QuadIndices() is := graphicsutil.QuadIndices()
newImg.DrawImage(i.backend.restorable, vs, is, nil, opengl.CompositeModeCopy, graphics.FilterNearest) newImg.DrawImage(i.backend.restorable, vs, is, nil, graphics.CompositeModeCopy, graphics.FilterNearest)
i.dispose(false) i.dispose(false)
i.backend = &backend{ i.backend = &backend{
@ -204,7 +203,7 @@ func (i *Image) Vertex(dx, dy, sx, sy float32, cr, cg, cb, ca float32) []float32
const MaxCountForShare = 10 const MaxCountForShare = 10
func (i *Image) DrawImage(img *Image, vertices []float32, indices []uint16, colorm *affine.ColorM, mode opengl.CompositeMode, filter graphics.Filter) { func (i *Image) DrawImage(img *Image, vertices []float32, indices []uint16, colorm *affine.ColorM, mode graphics.CompositeMode, filter graphics.Filter) {
backendsM.Lock() backendsM.Lock()
defer backendsM.Unlock() defer backendsM.Unlock()

View File

@ -24,7 +24,6 @@ import (
"github.com/hajimehoshi/ebiten" "github.com/hajimehoshi/ebiten"
"github.com/hajimehoshi/ebiten/internal/graphics" "github.com/hajimehoshi/ebiten/internal/graphics"
"github.com/hajimehoshi/ebiten/internal/graphicsutil" "github.com/hajimehoshi/ebiten/internal/graphicsutil"
"github.com/hajimehoshi/ebiten/internal/opengl"
. "github.com/hajimehoshi/ebiten/internal/shareable" . "github.com/hajimehoshi/ebiten/internal/shareable"
"github.com/hajimehoshi/ebiten/internal/testflock" "github.com/hajimehoshi/ebiten/internal/testflock"
) )
@ -88,7 +87,7 @@ func TestEnsureNotShared(t *testing.T) {
// img4.ensureNotShared() should be called. // img4.ensureNotShared() should be called.
vs := img3.QuadVertices(0, 0, size/2, size/2, 1, 0, 0, 1, size/4, size/4, 1, 1, 1, 1) vs := img3.QuadVertices(0, 0, size/2, size/2, 1, 0, 0, 1, size/4, size/4, 1, 1, 1, 1)
is := graphicsutil.QuadIndices() is := graphicsutil.QuadIndices()
img4.DrawImage(img3, vs, is, nil, opengl.CompositeModeCopy, graphics.FilterNearest) img4.DrawImage(img3, vs, is, nil, graphics.CompositeModeCopy, graphics.FilterNearest)
want := false want := false
if got := img4.IsSharedForTesting(); got != want { if got := img4.IsSharedForTesting(); got != want {
t.Errorf("got: %v, want: %v", got, want) t.Errorf("got: %v, want: %v", got, want)
@ -110,7 +109,7 @@ func TestEnsureNotShared(t *testing.T) {
// Check further drawing doesn't cause panic. // Check further drawing doesn't cause panic.
// This bug was fixed by 03dcd948. // This bug was fixed by 03dcd948.
img4.DrawImage(img3, vs, is, nil, opengl.CompositeModeCopy, graphics.FilterNearest) img4.DrawImage(img3, vs, is, nil, graphics.CompositeModeCopy, graphics.FilterNearest)
} }
func Disabled_TestReshared(t *testing.T) { func Disabled_TestReshared(t *testing.T) {
@ -152,7 +151,7 @@ func Disabled_TestReshared(t *testing.T) {
// Use img1 as a render target. // Use img1 as a render target.
vs := img2.QuadVertices(0, 0, size, size, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1) vs := img2.QuadVertices(0, 0, size, size, 1, 0, 0, 1, 0, 0, 1, 1, 1, 1)
is := graphicsutil.QuadIndices() is := graphicsutil.QuadIndices()
img1.DrawImage(img2, vs, is, nil, opengl.CompositeModeCopy, graphics.FilterNearest) img1.DrawImage(img2, vs, is, nil, graphics.CompositeModeCopy, graphics.FilterNearest)
want = false want = false
if got := img1.IsSharedForTesting(); got != want { if got := img1.IsSharedForTesting(); got != want {
t.Errorf("got: %v, want: %v", got, want) t.Errorf("got: %v, want: %v", got, want)
@ -160,7 +159,7 @@ func Disabled_TestReshared(t *testing.T) {
// Use img1 as a render source. // Use img1 as a render source.
for i := 0; i < MaxCountForShare-1; i++ { for i := 0; i < MaxCountForShare-1; i++ {
img0.DrawImage(img1, vs, is, nil, opengl.CompositeModeCopy, graphics.FilterNearest) img0.DrawImage(img1, vs, is, nil, graphics.CompositeModeCopy, graphics.FilterNearest)
want := false want := false
if got := img1.IsSharedForTesting(); got != want { if got := img1.IsSharedForTesting(); got != want {
t.Errorf("got: %v, want: %v", got, want) t.Errorf("got: %v, want: %v", got, want)
@ -177,7 +176,7 @@ func Disabled_TestReshared(t *testing.T) {
} }
} }
img0.DrawImage(img1, vs, is, nil, opengl.CompositeModeCopy, graphics.FilterNearest) img0.DrawImage(img1, vs, is, nil, graphics.CompositeModeCopy, graphics.FilterNearest)
want = true want = true
if got := img1.IsSharedForTesting(); got != want { if got := img1.IsSharedForTesting(); got != want {
t.Errorf("got: %v, want: %v", got, want) t.Errorf("got: %v, want: %v", got, want)
@ -195,7 +194,7 @@ func Disabled_TestReshared(t *testing.T) {
// Use img3 as a render source. img3 never uses a shared texture. // Use img3 as a render source. img3 never uses a shared texture.
for i := 0; i < MaxCountForShare*2; i++ { for i := 0; i < MaxCountForShare*2; i++ {
img0.DrawImage(img3, vs, is, nil, opengl.CompositeModeCopy, graphics.FilterNearest) img0.DrawImage(img3, vs, is, nil, graphics.CompositeModeCopy, graphics.FilterNearest)
want := false want := false
if got := img3.IsSharedForTesting(); got != want { if got := img3.IsSharedForTesting(); got != want {
t.Errorf("got: %v, want: %v", got, want) t.Errorf("got: %v, want: %v", got, want)