2019-06-26 15:17:45 +02:00
|
|
|
// 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.
|
|
|
|
|
2020-01-07 16:15:46 +01:00
|
|
|
package mipmap
|
2019-06-26 15:17:45 +02:00
|
|
|
|
|
|
|
import (
|
2019-07-30 06:20:41 +02:00
|
|
|
"fmt"
|
2019-09-18 19:27:20 +02:00
|
|
|
"image/color"
|
2019-06-26 15:17:45 +02:00
|
|
|
"math"
|
|
|
|
|
2019-09-19 04:15:14 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/affine"
|
2019-06-26 15:17:45 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/driver"
|
|
|
|
"github.com/hajimehoshi/ebiten/internal/graphics"
|
2020-05-28 21:12:41 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/shaderir"
|
2020-01-07 16:15:46 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/internal/shareable"
|
2019-06-26 15:17:45 +02:00
|
|
|
)
|
|
|
|
|
2020-01-03 15:14:41 +01:00
|
|
|
var graphicsDriver driver.Graphics
|
|
|
|
|
|
|
|
func SetGraphicsDriver(graphics driver.Graphics) {
|
|
|
|
graphicsDriver = graphics
|
|
|
|
}
|
|
|
|
|
2020-01-07 16:15:46 +01:00
|
|
|
func BeginFrame() error {
|
|
|
|
return shareable.BeginFrame()
|
|
|
|
}
|
|
|
|
|
|
|
|
func EndFrame() error {
|
|
|
|
return shareable.EndFrame()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mipmap is a set of shareable.Image sorted by the order of mipmap level.
|
|
|
|
// The level 0 image is a regular image and higher-level images are used for mipmap.
|
|
|
|
type Mipmap struct {
|
2020-07-14 17:22:25 +02:00
|
|
|
width int
|
|
|
|
height int
|
2019-09-21 07:53:52 +02:00
|
|
|
volatile bool
|
2020-01-07 16:15:46 +01:00
|
|
|
orig *shareable.Image
|
2020-07-14 17:22:25 +02:00
|
|
|
imgs map[int]*shareable.Image
|
2019-06-26 15:17:45 +02:00
|
|
|
}
|
|
|
|
|
2020-01-07 16:15:46 +01:00
|
|
|
func New(width, height int, volatile bool) *Mipmap {
|
|
|
|
return &Mipmap{
|
2020-07-14 17:22:25 +02:00
|
|
|
width: width,
|
|
|
|
height: height,
|
2019-09-21 07:53:52 +02:00
|
|
|
volatile: volatile,
|
2020-01-07 16:15:46 +01:00
|
|
|
orig: shareable.NewImage(width, height, volatile),
|
2020-07-14 17:22:25 +02:00
|
|
|
imgs: map[int]*shareable.Image{},
|
2019-09-18 19:13:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-07 16:15:46 +01:00
|
|
|
func NewScreenFramebufferMipmap(width, height int) *Mipmap {
|
|
|
|
return &Mipmap{
|
2020-07-14 17:22:25 +02:00
|
|
|
width: width,
|
|
|
|
height: height,
|
|
|
|
orig: shareable.NewScreenFramebufferImage(width, height),
|
|
|
|
imgs: map[int]*shareable.Image{},
|
2019-06-26 15:17:45 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-05 19:03:38 +01:00
|
|
|
func (m *Mipmap) Dump(name string, blackbg bool) error {
|
|
|
|
return m.orig.Dump(name, blackbg)
|
2019-09-18 19:13:11 +02:00
|
|
|
}
|
|
|
|
|
2020-01-07 16:15:46 +01:00
|
|
|
func (m *Mipmap) Fill(clr color.RGBA) {
|
2019-09-18 19:27:20 +02:00
|
|
|
m.orig.Fill(clr)
|
|
|
|
m.disposeMipmaps()
|
|
|
|
}
|
|
|
|
|
2020-01-07 16:15:46 +01:00
|
|
|
func (m *Mipmap) ReplacePixels(pix []byte) {
|
2019-09-18 19:27:20 +02:00
|
|
|
m.orig.ReplacePixels(pix)
|
|
|
|
m.disposeMipmaps()
|
|
|
|
}
|
|
|
|
|
2020-04-18 13:01:40 +02:00
|
|
|
func (m *Mipmap) Pixels(x, y, width, height int) ([]byte, error) {
|
|
|
|
return m.orig.Pixels(x, y, width, height)
|
2019-09-18 19:27:20 +02:00
|
|
|
}
|
|
|
|
|
2020-07-14 20:33:13 +02:00
|
|
|
func (m *Mipmap) DrawTriangles(src *Mipmap, vertices []float32, indices []uint16, colorm *affine.ColorM, mode driver.CompositeMode, filter driver.Filter, address driver.Address, sourceRegion driver.Region, shader *Shader, uniforms []interface{}, images []*Mipmap) {
|
2020-07-15 15:52:44 +02:00
|
|
|
level := math.MaxInt32
|
|
|
|
for i := 0; i < len(indices)/3; i++ {
|
|
|
|
const n = graphics.VertexFloatNum
|
|
|
|
dx0 := vertices[n*indices[3*i]+0]
|
|
|
|
dy0 := vertices[n*indices[3*i]+1]
|
|
|
|
sx0 := vertices[n*indices[3*i]+2]
|
|
|
|
sy0 := vertices[n*indices[3*i]+3]
|
|
|
|
dx1 := vertices[n*indices[3*i+1]+0]
|
|
|
|
dy1 := vertices[n*indices[3*i+1]+1]
|
|
|
|
sx1 := vertices[n*indices[3*i+1]+2]
|
|
|
|
sy1 := vertices[n*indices[3*i+1]+3]
|
|
|
|
dx2 := vertices[n*indices[3*i+2]+0]
|
|
|
|
dy2 := vertices[n*indices[3*i+2]+1]
|
|
|
|
sx2 := vertices[n*indices[3*i+2]+2]
|
|
|
|
sy2 := vertices[n*indices[3*i+2]+3]
|
2020-07-15 19:43:08 +02:00
|
|
|
if l := src.mipmapLevelFromDistance(dx0, dy0, dx1, dy1, sx0, sy0, sx1, sy1, filter); level > l {
|
2020-07-15 15:52:44 +02:00
|
|
|
level = l
|
|
|
|
}
|
2020-07-15 19:43:08 +02:00
|
|
|
if l := src.mipmapLevelFromDistance(dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, filter); level > l {
|
2020-07-15 15:52:44 +02:00
|
|
|
level = l
|
|
|
|
}
|
2020-07-15 19:43:08 +02:00
|
|
|
if l := src.mipmapLevelFromDistance(dx2, dy2, dx0, dy0, sx2, sy2, sx0, sy0, filter); level > l {
|
2020-07-15 15:52:44 +02:00
|
|
|
level = l
|
|
|
|
}
|
|
|
|
}
|
2020-03-16 17:59:00 +01:00
|
|
|
|
|
|
|
if colorm != nil && colorm.ScaleOnly() {
|
|
|
|
body, _ := colorm.UnsafeElements()
|
|
|
|
cr := body[0]
|
|
|
|
cg := body[5]
|
|
|
|
cb := body[10]
|
|
|
|
ca := body[15]
|
|
|
|
colorm = nil
|
|
|
|
const n = graphics.VertexFloatNum
|
|
|
|
for i := 0; i < len(vertices)/n; i++ {
|
2020-07-02 16:06:58 +02:00
|
|
|
vertices[i*n+4] *= cr
|
|
|
|
vertices[i*n+5] *= cg
|
|
|
|
vertices[i*n+6] *= cb
|
|
|
|
vertices[i*n+7] *= ca
|
2020-03-16 17:59:00 +01:00
|
|
|
}
|
|
|
|
}
|
2020-05-29 20:21:45 +02:00
|
|
|
|
2020-05-28 21:12:41 +02:00
|
|
|
var s *shareable.Shader
|
|
|
|
if shader != nil {
|
|
|
|
s = shader.shader
|
|
|
|
}
|
|
|
|
|
2020-07-15 15:52:44 +02:00
|
|
|
var srcimg *shareable.Image
|
2020-06-03 18:35:35 +02:00
|
|
|
if src != nil {
|
2020-07-15 15:52:44 +02:00
|
|
|
srcimg = src.orig
|
|
|
|
}
|
|
|
|
|
|
|
|
if level != 0 {
|
|
|
|
if img := src.level(level); img != nil {
|
|
|
|
srcimg = img
|
|
|
|
const n = graphics.VertexFloatNum
|
|
|
|
s := float32(pow2(level))
|
|
|
|
for i := 0; i < len(vertices)/n; i++ {
|
|
|
|
vertices[i*n+2] /= s
|
|
|
|
vertices[i*n+3] /= s
|
|
|
|
}
|
|
|
|
}
|
2020-06-03 18:35:35 +02:00
|
|
|
}
|
|
|
|
|
2020-07-15 15:52:44 +02:00
|
|
|
// TODO: Do we need to consider mipmaps here?
|
2020-07-14 20:33:13 +02:00
|
|
|
var imgs []*shareable.Image
|
|
|
|
for _, img := range images {
|
|
|
|
imgs = append(imgs, img.orig)
|
2020-07-05 18:55:46 +02:00
|
|
|
}
|
|
|
|
|
2020-07-15 15:52:44 +02:00
|
|
|
m.orig.DrawTriangles(srcimg, vertices, indices, colorm, mode, filter, address, sourceRegion, s, uniforms, imgs)
|
2019-09-19 04:15:14 +02:00
|
|
|
m.disposeMipmaps()
|
|
|
|
}
|
|
|
|
|
2020-07-14 17:22:25 +02:00
|
|
|
func (m *Mipmap) level(level int) *shareable.Image {
|
2019-07-30 06:20:41 +02:00
|
|
|
if level == 0 {
|
|
|
|
panic("ebiten: level must be non-zero at level")
|
2019-06-26 15:17:45 +02:00
|
|
|
}
|
|
|
|
|
2019-09-21 07:53:52 +02:00
|
|
|
if m.volatile {
|
2019-07-30 10:50:37 +02:00
|
|
|
panic("ebiten: mipmap images for a volatile image is not implemented yet")
|
2019-06-26 15:17:45 +02:00
|
|
|
}
|
|
|
|
|
2020-07-14 17:22:25 +02:00
|
|
|
if img, ok := m.imgs[level]; ok {
|
2019-07-30 10:50:37 +02:00
|
|
|
return img
|
|
|
|
}
|
2019-06-26 15:17:45 +02:00
|
|
|
|
2020-01-07 16:15:46 +01:00
|
|
|
var src *shareable.Image
|
2019-09-20 19:35:18 +02:00
|
|
|
var vs []float32
|
2019-07-30 06:20:41 +02:00
|
|
|
var filter driver.Filter
|
|
|
|
switch {
|
|
|
|
case level == 1:
|
2019-07-30 10:50:37 +02:00
|
|
|
src = m.orig
|
2020-07-15 18:53:46 +02:00
|
|
|
vs = graphics.QuadVertices(0, 0, float32(m.width), float32(m.height), 0.5, 0, 0, 0.5, 0, 0, 1, 1, 1, 1, false)
|
2019-07-30 06:20:41 +02:00
|
|
|
filter = driver.FilterLinear
|
|
|
|
case level > 1:
|
2020-07-14 17:22:25 +02:00
|
|
|
src = m.level(level - 1)
|
2019-07-30 10:50:37 +02:00
|
|
|
if src == nil {
|
2020-07-14 17:22:25 +02:00
|
|
|
m.imgs[level] = nil
|
2019-07-30 10:50:37 +02:00
|
|
|
return nil
|
2019-06-26 15:17:45 +02:00
|
|
|
}
|
2020-07-14 17:22:25 +02:00
|
|
|
w := sizeForLevel(m.width, level-1)
|
|
|
|
h := sizeForLevel(m.height, level-1)
|
2020-07-15 18:53:46 +02:00
|
|
|
vs = graphics.QuadVertices(0, 0, float32(w), float32(h), 0.5, 0, 0, 0.5, 0, 0, 1, 1, 1, 1, false)
|
2019-07-30 06:20:41 +02:00
|
|
|
filter = driver.FilterLinear
|
2019-07-31 02:20:24 +02:00
|
|
|
case level == -1:
|
2019-07-30 06:20:41 +02:00
|
|
|
src = m.orig
|
2020-07-15 18:53:46 +02:00
|
|
|
vs = graphics.QuadVertices(0, 0, float32(m.width), float32(m.height), 2, 0, 0, 2, 0, 0, 1, 1, 1, 1, false)
|
2019-07-31 02:20:24 +02:00
|
|
|
filter = driver.FilterNearest
|
|
|
|
case level < -1:
|
2020-07-14 17:22:25 +02:00
|
|
|
src = m.level(level + 1)
|
2019-07-31 02:20:24 +02:00
|
|
|
if src == nil {
|
2020-07-14 17:22:25 +02:00
|
|
|
m.imgs[level] = nil
|
2019-07-31 02:20:24 +02:00
|
|
|
return nil
|
|
|
|
}
|
2020-07-14 17:22:25 +02:00
|
|
|
w := sizeForLevel(m.width, level-1)
|
|
|
|
h := sizeForLevel(m.height, level-1)
|
2020-07-15 18:53:46 +02:00
|
|
|
vs = graphics.QuadVertices(0, 0, float32(w), float32(h), 2, 0, 0, 2, 0, 0, 1, 1, 1, 1, false)
|
2019-07-30 06:20:41 +02:00
|
|
|
filter = driver.FilterNearest
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("ebiten: invalid level: %d", level))
|
2019-06-26 15:17:45 +02:00
|
|
|
}
|
2019-07-30 10:50:37 +02:00
|
|
|
is := graphics.QuadIndices()
|
2019-07-30 06:20:41 +02:00
|
|
|
|
2020-07-14 17:22:25 +02:00
|
|
|
w2 := sizeForLevel(m.width, level-1)
|
|
|
|
h2 := sizeForLevel(m.height, level-1)
|
2019-09-20 22:56:34 +02:00
|
|
|
if w2 == 0 || h2 == 0 {
|
2020-07-14 17:22:25 +02:00
|
|
|
m.imgs[level] = nil
|
2019-09-20 22:56:34 +02:00
|
|
|
return nil
|
|
|
|
}
|
2020-01-07 16:15:46 +01:00
|
|
|
s := shareable.NewImage(w2, h2, m.volatile)
|
2020-07-05 18:55:46 +02:00
|
|
|
s.DrawTriangles(src, vs, is, nil, driver.CompositeModeCopy, filter, driver.AddressUnsafe, driver.Region{}, nil, nil, nil)
|
2020-07-14 17:22:25 +02:00
|
|
|
m.imgs[level] = s
|
2019-09-20 22:56:34 +02:00
|
|
|
|
2020-07-14 17:22:25 +02:00
|
|
|
return m.imgs[level]
|
2019-09-20 22:56:34 +02:00
|
|
|
}
|
|
|
|
|
2020-07-14 17:22:25 +02:00
|
|
|
func sizeForLevel(x int, level int) int {
|
2019-07-31 02:20:24 +02:00
|
|
|
if level > 0 {
|
|
|
|
for i := 0; i < level; i++ {
|
2020-07-14 17:22:25 +02:00
|
|
|
x /= 2
|
|
|
|
if x == 0 {
|
|
|
|
return 0
|
2019-07-31 02:20:24 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for i := 0; i < -level; i++ {
|
2020-07-14 17:22:25 +02:00
|
|
|
x *= 2
|
2019-07-31 02:20:24 +02:00
|
|
|
}
|
|
|
|
}
|
2020-07-14 17:22:25 +02:00
|
|
|
return x
|
2019-06-26 15:17:45 +02:00
|
|
|
}
|
|
|
|
|
2020-01-07 16:15:46 +01:00
|
|
|
func (m *Mipmap) MarkDisposed() {
|
2019-06-26 15:17:45 +02:00
|
|
|
m.disposeMipmaps()
|
2019-09-21 12:54:39 +02:00
|
|
|
m.orig.MarkDisposed()
|
2019-06-26 15:17:45 +02:00
|
|
|
m.orig = nil
|
|
|
|
}
|
|
|
|
|
2020-01-07 16:15:46 +01:00
|
|
|
func (m *Mipmap) disposeMipmaps() {
|
2020-07-14 17:22:25 +02:00
|
|
|
for _, img := range m.imgs {
|
|
|
|
img.MarkDisposed()
|
2019-06-26 15:17:45 +02:00
|
|
|
}
|
|
|
|
for k := range m.imgs {
|
|
|
|
delete(m.imgs, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-15 15:52:44 +02:00
|
|
|
// mipmapLevel returns an appropriate mipmap level for the given distance.
|
|
|
|
func (m *Mipmap) mipmapLevelFromDistance(dx0, dy0, dx1, dy1, sx0, sy0, sx1, sy1 float32, filter driver.Filter) int {
|
2019-09-28 18:31:01 +02:00
|
|
|
if filter == driver.FilterScreen {
|
|
|
|
return 0
|
|
|
|
}
|
2020-07-15 18:19:57 +02:00
|
|
|
if m.volatile {
|
|
|
|
return 0
|
|
|
|
}
|
2019-09-28 18:31:01 +02:00
|
|
|
|
2020-07-15 15:52:44 +02:00
|
|
|
d := (dx1-dx0)*(dx1-dx0) + (dy1-dy0)*(dy1-dy0)
|
|
|
|
s := (sx1-sx0)*(sx1-sx0) + (sy1-sy0)*(sy1-sy0)
|
2020-07-15 19:59:50 +02:00
|
|
|
if s == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
2020-07-15 15:52:44 +02:00
|
|
|
scale := d / s
|
|
|
|
|
2019-07-30 06:20:41 +02:00
|
|
|
// Use 'negative' mipmap to render edges correctly (#611, #907).
|
2019-07-31 15:01:45 +02:00
|
|
|
// It looks like 128 is the enlargement factor that causes edge missings to pass the test TestImageStretch.
|
2020-01-03 15:14:41 +01:00
|
|
|
var tooBigScale float32 = 128
|
|
|
|
if !graphicsDriver.HasHighPrecisionFloat() {
|
|
|
|
tooBigScale = 4
|
|
|
|
}
|
2020-07-15 17:52:41 +02:00
|
|
|
|
2020-07-15 15:52:44 +02:00
|
|
|
if scale >= tooBigScale*tooBigScale {
|
2019-07-31 18:07:19 +02:00
|
|
|
// If the filter is not nearest, the target needs to be rendered with graduation. Don't use mipmaps.
|
2019-07-30 06:20:41 +02:00
|
|
|
if filter != driver.FilterNearest {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
const mipmapMaxSize = 1024
|
2020-07-15 15:52:44 +02:00
|
|
|
w, h := sx1-sx0, sy1-sy0
|
2019-07-30 06:20:41 +02:00
|
|
|
if w >= mipmapMaxSize || h >= mipmapMaxSize {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
level := 0
|
2020-07-15 15:52:44 +02:00
|
|
|
for scale >= tooBigScale*tooBigScale {
|
2019-07-30 06:20:41 +02:00
|
|
|
level--
|
2020-07-15 15:52:44 +02:00
|
|
|
scale /= 4
|
2019-07-30 06:20:41 +02:00
|
|
|
w *= 2
|
|
|
|
h *= 2
|
|
|
|
if w >= mipmapMaxSize || h >= mipmapMaxSize {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2020-07-15 17:52:41 +02:00
|
|
|
|
|
|
|
// If tooBigScale is 4, level -10 means that the maximum scale is 4 * 2^10 = 4096. This should be
|
|
|
|
// enough.
|
|
|
|
if level < -10 {
|
|
|
|
level = -10
|
|
|
|
}
|
2019-07-30 06:20:41 +02:00
|
|
|
return level
|
2019-06-26 15:17:45 +02:00
|
|
|
}
|
|
|
|
|
2019-07-30 06:20:41 +02:00
|
|
|
if filter != driver.FilterLinear {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2020-07-15 15:52:44 +02:00
|
|
|
level := 0
|
|
|
|
for scale < 0.25 {
|
|
|
|
level++
|
|
|
|
scale *= 4
|
|
|
|
}
|
|
|
|
|
2020-07-15 17:52:41 +02:00
|
|
|
if level > 0 {
|
|
|
|
// If the image can be scaled into 0 size, adjust the level. (#839)
|
2020-07-15 15:52:44 +02:00
|
|
|
w, h := int(sx1-sx0), int(sy1-sy0)
|
2020-07-15 17:52:41 +02:00
|
|
|
for level >= 0 {
|
|
|
|
s := 1 << uint(level)
|
2020-07-15 15:52:44 +02:00
|
|
|
if (w > 0 && w/s == 0) || (h > 0 && h/s == 0) {
|
2020-07-15 17:52:41 +02:00
|
|
|
level--
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if level < 0 {
|
|
|
|
// As the render source is too small, nothing is rendered.
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if level > 6 {
|
|
|
|
level = 6
|
|
|
|
}
|
|
|
|
|
|
|
|
return level
|
2019-07-30 06:20:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func pow2(power int) float32 {
|
|
|
|
if power >= 0 {
|
|
|
|
x := 1
|
|
|
|
return float32(x << uint(power))
|
|
|
|
}
|
|
|
|
|
|
|
|
x := float32(1)
|
|
|
|
for i := 0; i < -power; i++ {
|
|
|
|
x /= 2
|
|
|
|
}
|
|
|
|
return x
|
|
|
|
}
|
|
|
|
|
2020-05-28 21:12:41 +02:00
|
|
|
type Shader struct {
|
|
|
|
shader *shareable.Shader
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewShader(program *shaderir.Program) *Shader {
|
|
|
|
return &Shader{
|
|
|
|
shader: shareable.NewShader(program),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Shader) MarkDisposed() {
|
|
|
|
s.shader.MarkDisposed()
|
|
|
|
s.shader = nil
|
|
|
|
}
|