mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-10 04:57:26 +01:00
parent
5df69f6dcc
commit
961c34efb5
106
image_test.go
106
image_test.go
@ -3865,11 +3865,15 @@ func TestImageBlendFactor(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestImageAntiAliasAndBlend(t *testing.T) {
|
func TestImageAntiAlias(t *testing.T) {
|
||||||
const w, h = 16, 16
|
// This value depends on internal/ui.bigOffscreenScale. Sync this.
|
||||||
|
const bigOffscreenScale = 2
|
||||||
|
|
||||||
|
const w, h = 272, 208
|
||||||
|
|
||||||
dst0 := ebiten.NewImage(w, h)
|
dst0 := ebiten.NewImage(w, h)
|
||||||
dst1 := ebiten.NewImage(w, h)
|
dst1 := ebiten.NewImage(w, h)
|
||||||
|
tmp := ebiten.NewImage(w*bigOffscreenScale, h*bigOffscreenScale)
|
||||||
src := ebiten.NewImage(w, h)
|
src := ebiten.NewImage(w, h)
|
||||||
|
|
||||||
for _, blend := range []ebiten.Blend{
|
for _, blend := range []ebiten.Blend{
|
||||||
@ -3877,19 +3881,23 @@ func TestImageAntiAliasAndBlend(t *testing.T) {
|
|||||||
ebiten.BlendClear,
|
ebiten.BlendClear,
|
||||||
ebiten.BlendCopy,
|
ebiten.BlendCopy,
|
||||||
ebiten.BlendSourceOver,
|
ebiten.BlendSourceOver,
|
||||||
|
ebiten.BlendDestinationOver,
|
||||||
|
ebiten.BlendXor,
|
||||||
ebiten.BlendLighter,
|
ebiten.BlendLighter,
|
||||||
} {
|
} {
|
||||||
dst0.Fill(color.RGBA{R: 0x24, G: 0x3f, B: 0x6a, A: 0x88})
|
dst0.Fill(color.RGBA{R: 0x24, G: 0x3f, B: 0x6a, A: 0x88})
|
||||||
dst1.Fill(color.RGBA{R: 0x24, G: 0x3f, B: 0x6a, A: 0x88})
|
dst1.Fill(color.RGBA{R: 0x24, G: 0x3f, B: 0x6a, A: 0x88})
|
||||||
|
tmp.Clear()
|
||||||
src.Fill(color.RGBA{R: 0x85, G: 0xa3, B: 0x08, A: 0xd3})
|
src.Fill(color.RGBA{R: 0x85, G: 0xa3, B: 0x08, A: 0xd3})
|
||||||
|
|
||||||
op0 := &ebiten.DrawTrianglesOptions{}
|
// Create an actual result.
|
||||||
op0.Blend = blend
|
op := &ebiten.DrawTrianglesOptions{}
|
||||||
op0.AntiAlias = true
|
op.Blend = blend
|
||||||
vs := []ebiten.Vertex{
|
op.AntiAlias = true
|
||||||
|
vs0 := []ebiten.Vertex{
|
||||||
{
|
{
|
||||||
DstX: 0,
|
DstX: w / 4,
|
||||||
DstY: 0,
|
DstY: h / 4,
|
||||||
SrcX: 0,
|
SrcX: 0,
|
||||||
SrcY: 0,
|
SrcY: 0,
|
||||||
ColorR: 1,
|
ColorR: 1,
|
||||||
@ -3898,8 +3906,8 @@ func TestImageAntiAliasAndBlend(t *testing.T) {
|
|||||||
ColorA: 1,
|
ColorA: 1,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
DstX: w,
|
DstX: 2 * w / 4,
|
||||||
DstY: 0,
|
DstY: h / 4,
|
||||||
SrcX: w,
|
SrcX: w,
|
||||||
SrcY: 0,
|
SrcY: 0,
|
||||||
ColorR: 1,
|
ColorR: 1,
|
||||||
@ -3908,8 +3916,23 @@ func TestImageAntiAliasAndBlend(t *testing.T) {
|
|||||||
ColorA: 1,
|
ColorA: 1,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
DstX: 0,
|
DstX: w / 4,
|
||||||
DstY: h,
|
DstY: 2 * h / 4,
|
||||||
|
SrcX: 0,
|
||||||
|
SrcY: h,
|
||||||
|
ColorR: 1,
|
||||||
|
ColorG: 1,
|
||||||
|
ColorB: 1,
|
||||||
|
ColorA: 1,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
is := []uint16{0, 1, 2}
|
||||||
|
dst0.DrawTriangles(vs0, is, src, op)
|
||||||
|
|
||||||
|
vs1 := []ebiten.Vertex{
|
||||||
|
{
|
||||||
|
DstX: 2 * w / 4,
|
||||||
|
DstY: 3 * h / 4,
|
||||||
SrcX: 0,
|
SrcX: 0,
|
||||||
SrcY: h,
|
SrcY: h,
|
||||||
ColorR: 1,
|
ColorR: 1,
|
||||||
@ -3918,8 +3941,18 @@ func TestImageAntiAliasAndBlend(t *testing.T) {
|
|||||||
ColorA: 1,
|
ColorA: 1,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
DstX: w,
|
DstX: 3 * w / 4,
|
||||||
DstY: h,
|
DstY: 2 * h / 4,
|
||||||
|
SrcX: w,
|
||||||
|
SrcY: 0,
|
||||||
|
ColorR: 1,
|
||||||
|
ColorG: 1,
|
||||||
|
ColorB: 1,
|
||||||
|
ColorA: 1,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
DstX: 3 * w / 4,
|
||||||
|
DstY: 3 * h / 4,
|
||||||
SrcX: w,
|
SrcX: w,
|
||||||
SrcY: h,
|
SrcY: h,
|
||||||
ColorR: 1,
|
ColorR: 1,
|
||||||
@ -3928,17 +3961,44 @@ func TestImageAntiAliasAndBlend(t *testing.T) {
|
|||||||
ColorA: 1,
|
ColorA: 1,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
is := []uint16{0, 1, 2, 1, 2, 3}
|
dst0.DrawTriangles(vs1, is, src, op)
|
||||||
dst0.DrawTriangles(vs, is, src, op0)
|
|
||||||
got := dst0.At(0, 0).(color.RGBA)
|
|
||||||
|
|
||||||
op1 := &ebiten.DrawImageOptions{}
|
// Create an expected result.
|
||||||
op1.Blend = blend
|
// Copy an enlarged destination image to the offscreen.
|
||||||
dst1.DrawImage(src, op1)
|
opCopy := &ebiten.DrawImageOptions{}
|
||||||
want := dst1.At(0, 0).(color.RGBA)
|
opCopy.GeoM.Scale(bigOffscreenScale, bigOffscreenScale)
|
||||||
|
opCopy.Blend = ebiten.BlendCopy
|
||||||
|
tmp.DrawImage(dst1, opCopy)
|
||||||
|
|
||||||
if got != want {
|
// Render the vertices onto the offscreen.
|
||||||
t.Errorf("blend: %v, got: %v, want: %v", blend, got, want)
|
for i := range vs0 {
|
||||||
|
vs0[i].DstX *= 2
|
||||||
|
vs0[i].DstY *= 2
|
||||||
|
}
|
||||||
|
for i := range vs1 {
|
||||||
|
vs1[i].DstX *= 2
|
||||||
|
vs1[i].DstY *= 2
|
||||||
|
}
|
||||||
|
op = &ebiten.DrawTrianglesOptions{}
|
||||||
|
op.Blend = blend
|
||||||
|
tmp.DrawTriangles(vs0, is, src, op)
|
||||||
|
tmp.DrawTriangles(vs1, is, src, op)
|
||||||
|
|
||||||
|
// Render a shrunk offscreen image onto the destination.
|
||||||
|
opShrink := &ebiten.DrawImageOptions{}
|
||||||
|
opShrink.GeoM.Scale(1.0/bigOffscreenScale, 1.0/bigOffscreenScale)
|
||||||
|
opShrink.Filter = ebiten.FilterLinear
|
||||||
|
opShrink.Blend = ebiten.BlendCopy
|
||||||
|
dst1.DrawImage(tmp, opShrink)
|
||||||
|
|
||||||
|
for j := 0; j < h; j++ {
|
||||||
|
for i := 0; i < w; i++ {
|
||||||
|
got := dst0.At(i, j).(color.RGBA)
|
||||||
|
want := dst1.At(i, j).(color.RGBA)
|
||||||
|
if !sameColors(got, want, 2) {
|
||||||
|
t.Errorf("At(%d, %d), blend: %v, got: %v, want: %v", i, j, blend, got, want)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -16,6 +16,7 @@ package ui
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"math"
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten/v2/internal/atlas"
|
"github.com/hajimehoshi/ebiten/v2/internal/atlas"
|
||||||
"github.com/hajimehoshi/ebiten/v2/internal/graphics"
|
"github.com/hajimehoshi/ebiten/v2/internal/graphics"
|
||||||
@ -308,8 +309,7 @@ type bigOffscreenImage struct {
|
|||||||
imageType atlas.ImageType
|
imageType atlas.ImageType
|
||||||
|
|
||||||
image *Image
|
image *Image
|
||||||
width int
|
region graphicsdriver.Region
|
||||||
height int
|
|
||||||
|
|
||||||
blend graphicsdriver.Blend
|
blend graphicsdriver.Blend
|
||||||
dirty bool
|
dirty bool
|
||||||
@ -334,18 +334,42 @@ func (i *bigOffscreenImage) markDisposed() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (i *bigOffscreenImage) drawTriangles(srcs [graphics.ShaderImageCount]*Image, vertices []float32, indices []uint16, blend graphicsdriver.Blend, dstRegion, srcRegion graphicsdriver.Region, subimageOffsets [graphics.ShaderImageCount - 1][2]float32, shader *Shader, uniforms []uint32, evenOdd bool, canSkipMipmap bool, antialias bool) {
|
func (i *bigOffscreenImage) drawTriangles(srcs [graphics.ShaderImageCount]*Image, vertices []float32, indices []uint16, blend graphicsdriver.Blend, dstRegion, srcRegion graphicsdriver.Region, subimageOffsets [graphics.ShaderImageCount - 1][2]float32, shader *Shader, uniforms []uint32, evenOdd bool, canSkipMipmap bool, antialias bool) {
|
||||||
if i.image == nil {
|
|
||||||
// TODO: Start with a samller size (#2399)
|
|
||||||
i.image = NewImage(i.orig.width*bigOffscreenScale, i.orig.height*bigOffscreenScale, i.imageType)
|
|
||||||
i.width = i.orig.width
|
|
||||||
i.height = i.orig.height
|
|
||||||
}
|
|
||||||
|
|
||||||
if i.blend != blend {
|
if i.blend != blend {
|
||||||
i.flush()
|
i.flush()
|
||||||
}
|
}
|
||||||
i.blend = blend
|
i.blend = blend
|
||||||
|
|
||||||
|
// If the new region doesn't match with the current region, extend the buffer image.
|
||||||
|
if r := i.requiredRegion(vertices); i.region != r {
|
||||||
|
if i.image != nil {
|
||||||
|
img := i.image
|
||||||
|
i.image = NewImage(int(r.Width)*bigOffscreenScale, int(r.Height)*bigOffscreenScale, i.imageType)
|
||||||
|
|
||||||
|
srcs := [graphics.ShaderImageCount]*Image{img}
|
||||||
|
// TODO: Should we keep vertices like tmpVerticesForCopying?
|
||||||
|
vertices := make([]float32, 4*graphics.VertexFloatCount)
|
||||||
|
graphics.QuadVertices(
|
||||||
|
vertices,
|
||||||
|
0, 0, i.region.Width*bigOffscreenScale, i.region.Height*bigOffscreenScale,
|
||||||
|
1, 0, 0, 1, (i.region.X-r.X)*bigOffscreenScale, (i.region.Y-r.Y)*bigOffscreenScale,
|
||||||
|
1, 1, 1, 1)
|
||||||
|
is := graphics.QuadIndices()
|
||||||
|
dstRegion := graphicsdriver.Region{
|
||||||
|
X: 0,
|
||||||
|
Y: 0,
|
||||||
|
Width: r.Width * bigOffscreenScale,
|
||||||
|
Height: r.Height * bigOffscreenScale,
|
||||||
|
}
|
||||||
|
i.image.DrawTriangles(srcs, vertices, is, graphicsdriver.BlendCopy, dstRegion, graphicsdriver.Region{}, [graphics.ShaderImageCount - 1][2]float32{}, NearestFilterShader, nil, false, true, false)
|
||||||
|
img.MarkDisposed()
|
||||||
|
}
|
||||||
|
i.region = r
|
||||||
|
}
|
||||||
|
|
||||||
|
if i.image == nil {
|
||||||
|
i.image = NewImage(int(i.region.Width)*bigOffscreenScale, int(i.region.Height)*bigOffscreenScale, i.imageType)
|
||||||
|
}
|
||||||
|
|
||||||
// Copy the current rendering result to get the correct blending result.
|
// Copy the current rendering result to get the correct blending result.
|
||||||
if blend != graphicsdriver.BlendSourceOver && !i.dirty {
|
if blend != graphicsdriver.BlendSourceOver && !i.dirty {
|
||||||
srcs := [graphics.ShaderImageCount]*Image{i.orig}
|
srcs := [graphics.ShaderImageCount]*Image{i.orig}
|
||||||
@ -368,9 +392,9 @@ func (i *bigOffscreenImage) drawTriangles(srcs [graphics.ShaderImageCount]*Image
|
|||||||
i.image.DrawTriangles(srcs, i.tmpVerticesForCopying, is, graphicsdriver.BlendCopy, dstRegion, graphicsdriver.Region{}, [graphics.ShaderImageCount - 1][2]float32{}, NearestFilterShader, nil, false, true, false)
|
i.image.DrawTriangles(srcs, i.tmpVerticesForCopying, is, graphicsdriver.BlendCopy, dstRegion, graphicsdriver.Region{}, [graphics.ShaderImageCount - 1][2]float32{}, NearestFilterShader, nil, false, true, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := 0; i < len(vertices); i += graphics.VertexFloatCount {
|
for idx := 0; idx < len(vertices); idx += graphics.VertexFloatCount {
|
||||||
vertices[i] *= bigOffscreenScale
|
vertices[idx] = (vertices[idx] - i.region.X) * bigOffscreenScale
|
||||||
vertices[i+1] *= bigOffscreenScale
|
vertices[idx+1] = (vertices[idx+1] - i.region.Y) * bigOffscreenScale
|
||||||
}
|
}
|
||||||
|
|
||||||
dstRegion.X *= bigOffscreenScale
|
dstRegion.X *= bigOffscreenScale
|
||||||
@ -401,16 +425,11 @@ func (i *bigOffscreenImage) flush() {
|
|||||||
// i.tmpVerticesForFlushing can be reused as this is sent to DrawTriangles in this function.
|
// i.tmpVerticesForFlushing can be reused as this is sent to DrawTriangles in this function.
|
||||||
graphics.QuadVertices(
|
graphics.QuadVertices(
|
||||||
i.tmpVerticesForFlushing,
|
i.tmpVerticesForFlushing,
|
||||||
0, 0, float32(i.width*bigOffscreenScale), float32(i.height*bigOffscreenScale),
|
0, 0, i.region.Width*bigOffscreenScale, i.region.Height*bigOffscreenScale,
|
||||||
1.0/bigOffscreenScale, 0, 0, 1.0/bigOffscreenScale, 0, 0,
|
1.0/bigOffscreenScale, 0, 0, 1.0/bigOffscreenScale, i.region.X, i.region.Y,
|
||||||
1, 1, 1, 1)
|
1, 1, 1, 1)
|
||||||
is := graphics.QuadIndices()
|
is := graphics.QuadIndices()
|
||||||
dstRegion := graphicsdriver.Region{
|
dstRegion := i.region
|
||||||
X: 0,
|
|
||||||
Y: 0,
|
|
||||||
Width: float32(i.width),
|
|
||||||
Height: float32(i.height),
|
|
||||||
}
|
|
||||||
blend := graphicsdriver.BlendSourceOver
|
blend := graphicsdriver.BlendSourceOver
|
||||||
if i.blend != graphicsdriver.BlendSourceOver {
|
if i.blend != graphicsdriver.BlendSourceOver {
|
||||||
blend = graphicsdriver.BlendCopy
|
blend = graphicsdriver.BlendCopy
|
||||||
@ -420,3 +439,116 @@ func (i *bigOffscreenImage) flush() {
|
|||||||
i.image.clear()
|
i.image.clear()
|
||||||
i.dirty = false
|
i.dirty = false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (i *bigOffscreenImage) requiredRegion(vertices []float32) graphicsdriver.Region {
|
||||||
|
if i.blend != graphicsdriver.BlendSourceOver {
|
||||||
|
return graphicsdriver.Region{
|
||||||
|
X: 0,
|
||||||
|
Y: 0,
|
||||||
|
Width: float32(i.orig.width),
|
||||||
|
Height: float32(i.orig.height),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
minX := float32(i.orig.width)
|
||||||
|
minY := float32(i.orig.height)
|
||||||
|
maxX := float32(0)
|
||||||
|
maxY := float32(0)
|
||||||
|
for i := 0; i < len(vertices); i += graphics.VertexFloatCount {
|
||||||
|
dstX := vertices[i]
|
||||||
|
dstY := vertices[i+1]
|
||||||
|
if minX > floor(dstX)-1 {
|
||||||
|
minX = floor(dstX) - 1
|
||||||
|
}
|
||||||
|
if minY > floor(dstY)-1 {
|
||||||
|
minY = floor(dstY) - 1
|
||||||
|
}
|
||||||
|
if maxX < ceil(dstX)+1 {
|
||||||
|
maxX = ceil(dstX) + 1
|
||||||
|
}
|
||||||
|
if maxY < ceil(dstY)+1 {
|
||||||
|
maxY = ceil(dstY) + 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Adjust the granularity of the rectangle.
|
||||||
|
minX = float32(roundDown16(int(minX)))
|
||||||
|
minY = float32(roundDown16(int(minY)))
|
||||||
|
maxX = float32(roundUp16(int(maxX)))
|
||||||
|
maxY = float32(roundUp16(int(maxY)))
|
||||||
|
|
||||||
|
if minX < 0 {
|
||||||
|
minX = 0
|
||||||
|
}
|
||||||
|
if minY < 0 {
|
||||||
|
minY = 0
|
||||||
|
}
|
||||||
|
if maxX > float32(i.orig.width) {
|
||||||
|
maxX = float32(i.orig.width)
|
||||||
|
}
|
||||||
|
if maxY > float32(i.orig.height) {
|
||||||
|
maxY = float32(i.orig.height)
|
||||||
|
}
|
||||||
|
|
||||||
|
r := graphicsdriver.Region{
|
||||||
|
X: minX,
|
||||||
|
Y: minY,
|
||||||
|
Width: maxX - minX,
|
||||||
|
Height: maxY - minY,
|
||||||
|
}
|
||||||
|
if r.Width < 0 || r.Height < 0 {
|
||||||
|
return i.region
|
||||||
|
}
|
||||||
|
|
||||||
|
return union(r, i.region)
|
||||||
|
}
|
||||||
|
|
||||||
|
func floor(x float32) float32 {
|
||||||
|
return float32(math.Floor(float64(x)))
|
||||||
|
}
|
||||||
|
|
||||||
|
func ceil(x float32) float32 {
|
||||||
|
return float32(math.Ceil(float64(x)))
|
||||||
|
}
|
||||||
|
|
||||||
|
func roundDown16(x int) int {
|
||||||
|
return x & ^(0xf)
|
||||||
|
}
|
||||||
|
|
||||||
|
func roundUp16(x int) int {
|
||||||
|
return ((x - 1) & ^(0xf)) + 0x10
|
||||||
|
}
|
||||||
|
|
||||||
|
func min(x, y float32) float32 {
|
||||||
|
if x < y {
|
||||||
|
return x
|
||||||
|
}
|
||||||
|
return y
|
||||||
|
}
|
||||||
|
|
||||||
|
func max(x, y float32) float32 {
|
||||||
|
if x > y {
|
||||||
|
return x
|
||||||
|
}
|
||||||
|
return y
|
||||||
|
}
|
||||||
|
|
||||||
|
func union(r0, r1 graphicsdriver.Region) graphicsdriver.Region {
|
||||||
|
if r0.Width == 0 || r0.Height == 0 {
|
||||||
|
return r1
|
||||||
|
}
|
||||||
|
if r1.Width == 0 || r1.Height == 0 {
|
||||||
|
return r0
|
||||||
|
}
|
||||||
|
|
||||||
|
x0 := min(r0.X, r1.X)
|
||||||
|
y0 := min(r0.Y, r1.Y)
|
||||||
|
x1 := max(r0.X+r0.Width, r1.X+r1.Width)
|
||||||
|
y1 := max(r0.Y+r0.Height, r1.Y+r1.Height)
|
||||||
|
return graphicsdriver.Region{
|
||||||
|
X: x0,
|
||||||
|
Y: y0,
|
||||||
|
Width: x1 - x0,
|
||||||
|
Height: y1 - y0,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user