ebiten/internal/graphicscommand/command.go

410 lines
11 KiB
Go
Raw Normal View History

2016-06-02 19:34:34 +02:00
// Copyright 2016 Hajime Hoshi
//
// 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.
2018-10-28 12:10:05 +01:00
package graphicscommand
2016-06-02 19:34:34 +02:00
import (
"fmt"
"image"
"math"
"strings"
2016-06-02 19:34:34 +02:00
2020-10-03 19:35:13 +02:00
"github.com/hajimehoshi/ebiten/v2/internal/graphics"
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
"github.com/hajimehoshi/ebiten/v2/internal/shaderir"
2016-06-02 19:34:34 +02:00
)
2017-09-11 20:12:17 +02:00
// command represents a drawing command.
//
// A command for drawing that is created when Image functions are called like DrawTriangles,
2017-09-11 20:12:17 +02:00
// or Fill.
// A command is not immediately executed after created. Instead, it is queued after created,
2017-09-11 20:12:17 +02:00
// and executed only when necessary.
2016-06-02 19:34:34 +02:00
type command interface {
fmt.Stringer
Exec(commandQueue *commandQueue, graphicsDriver graphicsdriver.Graphics, indexOffset int) error
NeedsSync() bool
2016-06-02 19:34:34 +02:00
}
type drawTrianglesCommandPool struct {
pool []*drawTrianglesCommand
}
func (p *drawTrianglesCommandPool) get() *drawTrianglesCommand {
if len(p.pool) == 0 {
return &drawTrianglesCommand{}
}
v := p.pool[len(p.pool)-1]
p.pool[len(p.pool)-1] = nil
p.pool = p.pool[:len(p.pool)-1]
return v
}
func (p *drawTrianglesCommandPool) put(v *drawTrianglesCommand) {
if len(p.pool) >= 1024 {
return
}
p.pool = append(p.pool, v)
}
// drawTrianglesCommand represents a drawing command to draw an image on another image.
type drawTrianglesCommand struct {
dst *Image
srcs [graphics.ShaderImageCount]*Image
vertices []float32
blend graphicsdriver.Blend
dstRegions []graphicsdriver.DstRegion
shader *Shader
uniforms []uint32
fillRule graphicsdriver.FillRule
2016-06-02 19:34:34 +02:00
}
func (c *drawTrianglesCommand) String() string {
// TODO: Improve readability
blend := fmt.Sprintf("{src-color: %d, src-alpha: %d, dst-color: %d, dst-alpha: %d, op-color: %d, op-alpha: %d}",
c.blend.BlendFactorSourceRGB,
c.blend.BlendFactorSourceAlpha,
c.blend.BlendFactorDestinationRGB,
c.blend.BlendFactorDestinationAlpha,
c.blend.BlendOperationRGB,
c.blend.BlendOperationAlpha)
dst := fmt.Sprintf("%d", c.dst.id)
if c.dst.screen {
dst += " (screen)"
}
var srcstrs [graphics.ShaderImageCount]string
for i, src := range c.srcs {
if src == nil {
srcstrs[i] = "(nil)"
continue
}
srcstrs[i] = fmt.Sprintf("%d", src.id)
if src.screen {
srcstrs[i] += " (screen)"
}
}
return fmt.Sprintf("draw-triangles: dst: %s <- src: [%s], num of dst regions: %d, num of indices: %d, blend: %s, fill rule: %s", dst, strings.Join(srcstrs[:], ", "), len(c.dstRegions), c.numIndices(), blend, c.fillRule)
}
// Exec executes the drawTrianglesCommand.
func (c *drawTrianglesCommand) Exec(commandQueue *commandQueue, graphicsDriver graphicsdriver.Graphics, indexOffset int) error {
// TODO: Is it ok not to bind any framebuffer here?
if len(c.dstRegions) == 0 {
return nil
}
var imgs [graphics.ShaderImageCount]graphicsdriver.ImageID
for i, src := range c.srcs {
if src == nil {
imgs[i] = graphicsdriver.InvalidImageID
continue
}
imgs[i] = src.image.ID()
}
return graphicsDriver.DrawTriangles(c.dst.image.ID(), imgs, c.shader.shader.ID(), c.dstRegions, indexOffset, c.blend, c.uniforms, c.fillRule)
2016-06-02 19:34:34 +02:00
}
func (c *drawTrianglesCommand) NeedsSync() bool {
return false
}
func (c *drawTrianglesCommand) numVertices() int {
return len(c.vertices)
}
func (c *drawTrianglesCommand) numIndices() int {
var nindices int
for _, dstRegion := range c.dstRegions {
nindices += dstRegion.IndexCount
}
return nindices
}
func (c *drawTrianglesCommand) setVertices(vertices []float32) {
c.vertices = vertices
2018-03-18 11:58:32 +01:00
}
2019-11-13 16:08:44 +01:00
// CanMergeWithDrawTrianglesCommand returns a boolean value indicating whether the other drawTrianglesCommand can be merged
// with the drawTrianglesCommand c.
func (c *drawTrianglesCommand) CanMergeWithDrawTrianglesCommand(dst *Image, srcs [graphics.ShaderImageCount]*Image, vertices []float32, blend graphicsdriver.Blend, shader *Shader, uniforms []uint32, fillRule graphicsdriver.FillRule) bool {
if c.shader != shader {
return false
}
if len(c.uniforms) != len(uniforms) {
return false
}
for i := range c.uniforms {
if c.uniforms[i] != uniforms[i] {
return false
}
}
if c.dst != dst {
return false
}
if c.srcs != srcs {
return false
}
if c.blend != blend {
return false
}
if c.fillRule != fillRule {
2022-11-12 14:26:28 +01:00
return false
}
if c.fillRule == graphicsdriver.EvenOdd && mightOverlapDstRegions(c.vertices, vertices) {
return false
}
return true
}
var (
posInf32 = float32(math.Inf(1))
negInf32 = float32(math.Inf(-1))
)
func dstRegionFromVertices(vertices []float32) (minX, minY, maxX, maxY float32) {
minX = posInf32
minY = posInf32
maxX = negInf32
maxY = negInf32
for i := 0; i < len(vertices)/graphics.VertexFloatCount; i++ {
x := vertices[graphics.VertexFloatCount*i]
y := vertices[graphics.VertexFloatCount*i+1]
if x < minX {
minX = x
}
if y < minY {
minY = y
}
if maxX < x {
maxX = x
}
if maxY < y {
maxY = y
}
}
return
}
2021-07-05 16:32:19 +02:00
func mightOverlapDstRegions(vertices1, vertices2 []float32) bool {
minX1, minY1, maxX1, maxY1 := dstRegionFromVertices(vertices1)
minX2, minY2, maxX2, maxY2 := dstRegionFromVertices(vertices2)
const mergin = 1
return minX1 < maxX2+mergin && minX2 < maxX1+mergin && minY1 < maxY2+mergin && minY2 < maxY1+mergin
}
// writePixelsCommand represents a command to replace pixels of an image.
type writePixelsCommand struct {
dst *Image
args []writePixelsCommandArgs
}
type writePixelsCommandArgs struct {
pixels *graphics.ManagedBytes
region image.Rectangle
2016-06-02 19:34:34 +02:00
}
func (c *writePixelsCommand) String() string {
return fmt.Sprintf("write-pixels: dst: %d, len(args): %d", c.dst.id, len(c.args))
}
// Exec executes the writePixelsCommand.
func (c *writePixelsCommand) Exec(commandQueue *commandQueue, graphicsDriver graphicsdriver.Graphics, indexOffset int) error {
if len(c.args) == 0 {
return nil
}
args := make([]graphicsdriver.PixelsArgs, 0, len(c.args))
for _, a := range c.args {
pix, f := a.pixels.GetAndRelease()
// A finalizer is executed when flushing the queue at the end of the frame.
// At the end of the frame, the last command is rendering triangles onto the screen,
// so the bytes are already sent to GPU and synced.
// TODO: This might be fragile. When is the better time to call finalizers by a command queue?
commandQueue.addFinalizer(f)
args = append(args, graphicsdriver.PixelsArgs{
Pixels: pix,
Region: a.region,
})
}
if err := c.dst.image.WritePixels(args); err != nil {
return err
}
2016-06-02 19:34:34 +02:00
return nil
}
2016-06-11 15:52:07 +02:00
func (c *writePixelsCommand) NeedsSync() bool {
return false
}
type readPixelsCommand struct {
img *Image
args []graphicsdriver.PixelsArgs
}
// Exec executes a readPixelsCommand.
func (c *readPixelsCommand) Exec(commandQueue *commandQueue, graphicsDriver graphicsdriver.Graphics, indexOffset int) error {
if err := c.img.image.ReadPixels(c.args); err != nil {
return err
}
return nil
}
func (c *readPixelsCommand) NeedsSync() bool {
return true
}
func (c *readPixelsCommand) String() string {
return fmt.Sprintf("read-pixels: image: %d", c.img.id)
}
// disposeImageCommand represents a command to dispose an image.
type disposeImageCommand struct {
target *Image
2016-06-11 15:52:07 +02:00
}
func (c *disposeImageCommand) String() string {
return fmt.Sprintf("dispose-image: target: %d", c.target.id)
}
// Exec executes the disposeImageCommand.
func (c *disposeImageCommand) Exec(commandQueue *commandQueue, graphicsDriver graphicsdriver.Graphics, indexOffset int) error {
c.target.image.Dispose()
2016-06-11 15:52:07 +02:00
return nil
}
func (c *disposeImageCommand) NeedsSync() bool {
return false
}
// disposeShaderCommand represents a command to dispose a shader.
type disposeShaderCommand struct {
target *Shader
}
func (c *disposeShaderCommand) String() string {
return "dispose-shader: target"
}
// Exec executes the disposeShaderCommand.
func (c *disposeShaderCommand) Exec(commandQueue *commandQueue, graphicsDriver graphicsdriver.Graphics, indexOffset int) error {
c.target.shader.Dispose()
return nil
}
func (c *disposeShaderCommand) NeedsSync() bool {
return false
}
2017-09-19 18:35:56 +02:00
// newImageCommand represents a command to create an empty image with given width and height.
type newImageCommand struct {
result *Image
width int
height int
screen bool
}
func (c *newImageCommand) String() string {
return fmt.Sprintf("new-image: result: %d, width: %d, height: %d, screen: %t", c.result.id, c.width, c.height, c.screen)
}
2017-09-19 18:35:56 +02:00
// Exec executes a newImageCommand.
func (c *newImageCommand) Exec(commandQueue *commandQueue, graphicsDriver graphicsdriver.Graphics, indexOffset int) error {
2018-11-11 15:57:23 +01:00
var err error
if c.screen {
c.result.image, err = graphicsDriver.NewScreenFramebufferImage(c.width, c.height)
} else {
c.result.image, err = graphicsDriver.NewImage(c.width, c.height)
}
2018-11-11 15:57:23 +01:00
return err
}
func (c *newImageCommand) NeedsSync() bool {
return true
}
// newShaderCommand is a command to create a shader.
type newShaderCommand struct {
result *Shader
ir *shaderir.Program
}
func (c *newShaderCommand) String() string {
return "new-shader"
}
// Exec executes a newShaderCommand.
func (c *newShaderCommand) Exec(commandQueue *commandQueue, graphicsDriver graphicsdriver.Graphics, indexOffset int) error {
s, err := graphicsDriver.NewShader(c.ir)
if err != nil {
return err
}
c.result.shader = s
return nil
}
func (c *newShaderCommand) NeedsSync() bool {
return true
}
type isInvalidatedCommand struct {
result bool
image *Image
}
func (c *isInvalidatedCommand) String() string {
return fmt.Sprintf("is-invalidated: image: %d", c.image.id)
}
func (c *isInvalidatedCommand) Exec(commandQueue *commandQueue, graphicsDriver graphicsdriver.Graphics, indexOffset int) error {
c.result = c.image.image.IsInvalidated()
return nil
}
func (c *isInvalidatedCommand) NeedsSync() bool {
return true
}
// InitializeGraphicsDriverState initialize the current graphics driver state.
func InitializeGraphicsDriverState(graphicsDriver graphicsdriver.Graphics) (err error) {
runOnRenderThread(func() {
err = graphicsDriver.Initialize()
}, true)
return
}
// ResetGraphicsDriverState resets the current graphics driver state.
// If the graphics driver doesn't have an API to reset, ResetGraphicsDriverState does nothing.
func ResetGraphicsDriverState(graphicsDriver graphicsdriver.Graphics) (err error) {
if r, ok := graphicsDriver.(graphicsdriver.Resetter); ok {
runOnRenderThread(func() {
err = r.Reset()
}, true)
}
return nil
}
// MaxImageSize returns the maximum size of an image.
func MaxImageSize(graphicsDriver graphicsdriver.Graphics) int {
var size int
runOnRenderThread(func() {
size = graphicsDriver.MaxImageSize()
}, true)
return size
}