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-06-26 15:17:45 +02:00
|
|
|
"math"
|
|
|
|
|
2020-10-03 19:35:13 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/affine"
|
2022-06-07 16:57:56 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/atlas"
|
2020-10-03 19:35:13 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/buffered"
|
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphics"
|
2022-02-06 12:41:32 +01:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
|
2022-04-03 19:15:33 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2/internal/shaderir"
|
2019-06-26 15:17:45 +02:00
|
|
|
)
|
|
|
|
|
2022-09-27 20:07:38 +02:00
|
|
|
func canUseMipmap(imageType atlas.ImageType) bool {
|
|
|
|
switch imageType {
|
|
|
|
case atlas.ImageTypeRegular, atlas.ImageTypeUnmanaged:
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-07-26 05:15:23 +02:00
|
|
|
// Mipmap is a set of buffered.Image sorted by the order of mipmap level.
|
2020-01-07 16:15:46 +01:00
|
|
|
// The level 0 image is a regular image and higher-level images are used for mipmap.
|
|
|
|
type Mipmap struct {
|
2022-09-27 20:07:38 +02:00
|
|
|
width int
|
|
|
|
height int
|
|
|
|
imageType atlas.ImageType
|
|
|
|
orig *buffered.Image
|
|
|
|
imgs map[int]*buffered.Image
|
2019-06-26 15:17:45 +02:00
|
|
|
}
|
|
|
|
|
2022-06-07 16:57:56 +02:00
|
|
|
func New(width, height int, imageType atlas.ImageType) *Mipmap {
|
2020-01-07 16:15:46 +01:00
|
|
|
return &Mipmap{
|
2022-09-27 20:07:38 +02:00
|
|
|
width: width,
|
|
|
|
height: height,
|
|
|
|
orig: buffered.NewImage(width, height, imageType),
|
|
|
|
imageType: imageType,
|
2019-09-18 19:13:11 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-31 06:10:41 +02:00
|
|
|
func (m *Mipmap) DumpScreenshot(graphicsDriver graphicsdriver.Graphics, name string, blackbg bool) (string, error) {
|
2022-03-19 15:55:14 +01:00
|
|
|
return m.orig.DumpScreenshot(graphicsDriver, name, blackbg)
|
2019-09-18 19:13:11 +02:00
|
|
|
}
|
|
|
|
|
2022-08-07 20:30:37 +02:00
|
|
|
func (m *Mipmap) WritePixels(pix []byte, x, y, width, height int) {
|
2022-08-07 20:29:12 +02:00
|
|
|
m.orig.WritePixels(pix, x, y, width, height)
|
2019-09-18 19:27:20 +02:00
|
|
|
m.disposeMipmaps()
|
|
|
|
}
|
|
|
|
|
2022-08-05 17:27:05 +02:00
|
|
|
func (m *Mipmap) ReadPixels(graphicsDriver graphicsdriver.Graphics, pixels []byte, x, y, width, height int) error {
|
|
|
|
return m.orig.ReadPixels(graphicsDriver, pixels, x, y, width, height)
|
2019-09-18 19:27:20 +02:00
|
|
|
}
|
|
|
|
|
2022-07-12 18:46:02 +02:00
|
|
|
func (m *Mipmap) DrawTriangles(srcs [graphics.ShaderImageCount]*Mipmap, vertices []float32, indices []uint16, colorm affine.ColorM, mode graphicsdriver.CompositeMode, filter graphicsdriver.Filter, address graphicsdriver.Address, dstRegion, srcRegion graphicsdriver.Region, subimageOffsets [graphics.ShaderImageCount - 1][2]float32, shader *Shader, uniforms [][]float32, evenOdd bool, canSkipMipmap bool) {
|
2020-07-28 07:02:46 +02:00
|
|
|
if len(indices) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-07-16 16:02:33 +02:00
|
|
|
level := 0
|
2020-07-17 18:09:58 +02:00
|
|
|
// TODO: Do we need to check all the sources' states of being volatile?
|
2022-09-27 20:07:38 +02:00
|
|
|
if !canSkipMipmap && srcs[0] != nil && canUseMipmap(srcs[0].imageType) {
|
2020-07-16 16:02:33 +02:00
|
|
|
level = math.MaxInt32
|
|
|
|
for i := 0; i < len(indices)/3; i++ {
|
2022-07-12 18:46:02 +02:00
|
|
|
const n = graphics.VertexFloatCount
|
2020-07-16 16:02:33 +02:00
|
|
|
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]
|
2022-10-02 12:14:23 +02:00
|
|
|
if l := mipmapLevelFromDistance(dx0, dy0, dx1, dy1, sx0, sy0, sx1, sy1); level > l {
|
2020-07-16 16:02:33 +02:00
|
|
|
level = l
|
|
|
|
}
|
2022-10-02 12:14:23 +02:00
|
|
|
if l := mipmapLevelFromDistance(dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2); level > l {
|
2020-07-16 16:02:33 +02:00
|
|
|
level = l
|
|
|
|
}
|
2022-10-02 12:14:23 +02:00
|
|
|
if l := mipmapLevelFromDistance(dx2, dy2, dx0, dy0, sx2, sy2, sx0, sy0); level > l {
|
2020-07-16 16:02:33 +02:00
|
|
|
level = l
|
|
|
|
}
|
2020-07-15 15:52:44 +02:00
|
|
|
}
|
2020-07-28 07:02:46 +02:00
|
|
|
if level == math.MaxInt32 {
|
|
|
|
panic("mipmap: level must be calculated at least once but not")
|
|
|
|
}
|
2020-07-15 15:52:44 +02:00
|
|
|
}
|
2020-03-16 17:59:00 +01:00
|
|
|
|
2020-07-26 05:15:23 +02:00
|
|
|
var s *buffered.Shader
|
2020-05-28 21:12:41 +02:00
|
|
|
if shader != nil {
|
|
|
|
s = shader.shader
|
|
|
|
}
|
|
|
|
|
2022-07-12 18:46:02 +02:00
|
|
|
var imgs [graphics.ShaderImageCount]*buffered.Image
|
2020-07-17 18:09:58 +02:00
|
|
|
for i, src := range srcs {
|
|
|
|
if src == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if level != 0 {
|
|
|
|
if img := src.level(level); img != nil {
|
2022-07-12 18:46:02 +02:00
|
|
|
const n = graphics.VertexFloatCount
|
2020-07-17 18:09:58 +02:00
|
|
|
s := float32(pow2(level))
|
|
|
|
for i := 0; i < len(vertices)/n; i++ {
|
|
|
|
vertices[i*n+2] /= s
|
|
|
|
vertices[i*n+3] /= s
|
|
|
|
}
|
|
|
|
imgs[i] = img
|
|
|
|
continue
|
2020-07-15 15:52:44 +02:00
|
|
|
}
|
|
|
|
}
|
2020-07-17 18:09:58 +02:00
|
|
|
imgs[i] = src.orig
|
2020-06-03 18:35:35 +02:00
|
|
|
}
|
|
|
|
|
2021-07-02 12:26:09 +02:00
|
|
|
m.orig.DrawTriangles(imgs, vertices, indices, colorm, mode, filter, address, dstRegion, srcRegion, subimageOffsets, s, uniforms, evenOdd)
|
2019-09-19 04:15:14 +02:00
|
|
|
m.disposeMipmaps()
|
|
|
|
}
|
|
|
|
|
2021-09-20 16:27:14 +02:00
|
|
|
func (m *Mipmap) setImg(level int, img *buffered.Image) {
|
|
|
|
if m.imgs == nil {
|
|
|
|
m.imgs = map[int]*buffered.Image{}
|
|
|
|
}
|
|
|
|
m.imgs[level] = img
|
|
|
|
}
|
|
|
|
|
2020-07-26 05:15:23 +02:00
|
|
|
func (m *Mipmap) level(level int) *buffered.Image {
|
2019-07-30 06:20:41 +02:00
|
|
|
if level == 0 {
|
2022-04-01 13:11:22 +02:00
|
|
|
panic("mipmap: level must be non-zero at level")
|
2019-06-26 15:17:45 +02:00
|
|
|
}
|
|
|
|
|
2022-09-27 20:07:38 +02:00
|
|
|
if !canUseMipmap(m.imageType) {
|
|
|
|
panic("mipmap: mipmap images for a volatile or a screen 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-07-26 05:15:23 +02:00
|
|
|
var src *buffered.Image
|
2019-09-20 19:35:18 +02:00
|
|
|
var vs []float32
|
2022-10-02 11:19:26 +02:00
|
|
|
shader := NearestFilterShader
|
2019-07-30 06:20:41 +02:00
|
|
|
switch {
|
|
|
|
case level == 1:
|
2019-07-30 10:50:37 +02:00
|
|
|
src = m.orig
|
2021-03-20 08:18:50 +01:00
|
|
|
vs = graphics.QuadVertices(0, 0, float32(m.width), float32(m.height), 0.5, 0, 0, 0.5, 0, 0, 1, 1, 1, 1)
|
2022-10-02 11:19:26 +02:00
|
|
|
shader = LinearFilterShader
|
2019-07-30 06:20:41 +02:00
|
|
|
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 {
|
2021-09-20 16:27:14 +02:00
|
|
|
m.setImg(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)
|
2021-03-20 08:18:50 +01:00
|
|
|
vs = graphics.QuadVertices(0, 0, float32(w), float32(h), 0.5, 0, 0, 0.5, 0, 0, 1, 1, 1, 1)
|
2022-10-02 11:19:26 +02:00
|
|
|
shader = LinearFilterShader
|
2019-07-30 06:20:41 +02:00
|
|
|
default:
|
2022-04-01 13:11:22 +02:00
|
|
|
panic(fmt.Sprintf("mipmap: 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 {
|
2021-09-20 16:27:14 +02:00
|
|
|
m.setImg(level, nil)
|
2019-09-20 22:56:34 +02:00
|
|
|
return nil
|
|
|
|
}
|
2020-10-21 04:28:01 +02:00
|
|
|
// buffered.NewImage panics with a too big size when actual allocation happens.
|
|
|
|
// 4096 should be a safe size in most environments (#1399).
|
|
|
|
// Unfortunately a precise max image size cannot be obtained here since this requires GPU access.
|
|
|
|
if w2 > 4096 || h2 > 4096 {
|
2021-09-20 16:27:14 +02:00
|
|
|
m.setImg(level, nil)
|
2020-10-21 04:28:01 +02:00
|
|
|
return nil
|
|
|
|
}
|
2022-09-27 18:40:34 +02:00
|
|
|
|
2022-09-27 20:07:38 +02:00
|
|
|
s := buffered.NewImage(w2, h2, m.imageType)
|
2020-11-07 11:14:06 +01:00
|
|
|
|
2022-02-06 12:41:32 +01:00
|
|
|
dstRegion := graphicsdriver.Region{
|
2020-11-07 11:14:06 +01:00
|
|
|
X: 0,
|
|
|
|
Y: 0,
|
|
|
|
Width: float32(w2),
|
|
|
|
Height: float32(h2),
|
|
|
|
}
|
2022-10-02 11:19:26 +02:00
|
|
|
s.DrawTriangles([graphics.ShaderImageCount]*buffered.Image{src}, vs, is, affine.ColorMIdentity{}, graphicsdriver.CompositeModeCopy, graphicsdriver.FilterNearest, graphicsdriver.AddressUnsafe, dstRegion, graphicsdriver.Region{}, [graphics.ShaderImageCount - 1][2]float32{}, shader.shader, nil, false)
|
2021-09-20 16:27:14 +02:00
|
|
|
m.setImg(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 {
|
2020-10-30 18:52:38 +01:00
|
|
|
for i := 0; i < level; i++ {
|
|
|
|
x /= 2
|
|
|
|
if x == 0 {
|
|
|
|
return 0
|
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 {
|
2020-10-21 04:28:01 +02:00
|
|
|
if img != nil {
|
|
|
|
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.
|
2022-10-02 12:14:23 +02:00
|
|
|
func mipmapLevelFromDistance(dx0, dy0, dx1, dy1, sx0, sy0, sx1, sy1 float32) int {
|
2020-10-21 04:28:01 +02:00
|
|
|
const maxLevel = 6
|
2020-10-21 03:38:10 +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
|
|
|
|
|
2020-10-21 03:38:10 +02:00
|
|
|
// Scale can be infinite when the specified scale is extremely big (#1398).
|
|
|
|
if math.IsInf(float64(scale), 0) {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scale can be zero when the specified scale is extremely small (#1398).
|
|
|
|
if scale == 0 {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-21 04:28:01 +02:00
|
|
|
if level > maxLevel {
|
|
|
|
level = maxLevel
|
2020-07-15 17:52:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return level
|
2019-07-30 06:20:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func pow2(power int) float32 {
|
2020-10-30 18:52:38 +01:00
|
|
|
x := 1
|
|
|
|
return float32(x << uint(power))
|
2019-07-30 06:20:41 +02:00
|
|
|
}
|
|
|
|
|
2020-05-28 21:12:41 +02:00
|
|
|
type Shader struct {
|
2020-07-26 05:15:23 +02:00
|
|
|
shader *buffered.Shader
|
2020-05-28 21:12:41 +02:00
|
|
|
}
|
|
|
|
|
2022-04-03 19:15:33 +02:00
|
|
|
func NewShader(ir *shaderir.Program) *Shader {
|
2020-05-28 21:12:41 +02:00
|
|
|
return &Shader{
|
2022-04-03 19:15:33 +02:00
|
|
|
shader: buffered.NewShader(ir),
|
2020-05-28 21:12:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Shader) MarkDisposed() {
|
|
|
|
s.shader.MarkDisposed()
|
|
|
|
s.shader = nil
|
|
|
|
}
|
2022-10-02 11:19:26 +02:00
|
|
|
|
|
|
|
var (
|
|
|
|
NearestFilterShader = &Shader{shader: buffered.NearestFilterShader}
|
|
|
|
LinearFilterShader = &Shader{shader: buffered.LinearFilterShader}
|
|
|
|
)
|