internal/ui: use Kage shaders

Updates #2364
This commit is contained in:
Hajime Hoshi 2022-10-02 15:51:20 +09:00
parent a4e9a05b14
commit 239f9de2ca

View File

@ -23,6 +23,7 @@ import (
"github.com/hajimehoshi/ebiten/v2/internal/affine" "github.com/hajimehoshi/ebiten/v2/internal/affine"
"github.com/hajimehoshi/ebiten/v2/internal/atlas" "github.com/hajimehoshi/ebiten/v2/internal/atlas"
"github.com/hajimehoshi/ebiten/v2/internal/buffered" "github.com/hajimehoshi/ebiten/v2/internal/buffered"
"github.com/hajimehoshi/ebiten/v2/internal/builtinshader"
"github.com/hajimehoshi/ebiten/v2/internal/clock" "github.com/hajimehoshi/ebiten/v2/internal/clock"
"github.com/hajimehoshi/ebiten/v2/internal/debug" "github.com/hajimehoshi/ebiten/v2/internal/debug"
"github.com/hajimehoshi/ebiten/v2/internal/graphics" "github.com/hajimehoshi/ebiten/v2/internal/graphics"
@ -84,6 +85,8 @@ type context struct {
outsideHeight float64 outsideHeight float64
screenShader *Shader screenShader *Shader
nearestShader *Shader
linearShader *Shader
m sync.Mutex m sync.Mutex
} }
@ -155,6 +158,20 @@ func (c *context) updateFrameImpl(graphicsDriver graphicsdriver.Graphics, update
} }
c.screenShader = NewShader(ir) c.screenShader = NewShader(ir)
} }
if c.nearestShader == nil {
ir, err := graphics.CompileShader([]byte(builtinshader.Shader(graphicsdriver.FilterNearest, graphicsdriver.AddressUnsafe, false)))
if err != nil {
return err
}
c.nearestShader = NewShader(ir)
}
if c.linearShader == nil {
ir, err := graphics.CompileShader([]byte(builtinshader.Shader(graphicsdriver.FilterLinear, graphicsdriver.AddressUnsafe, false)))
if err != nil {
return err
}
c.linearShader = NewShader(ir)
}
// ForceUpdate can be invoked even if the context is not initialized yet (#1591). // ForceUpdate can be invoked even if the context is not initialized yet (#1591).
if w, h := c.layoutGame(outsideWidth, outsideHeight, deviceScaleFactor); w == 0 || h == 0 { if w, h := c.layoutGame(outsideWidth, outsideHeight, deviceScaleFactor); w == 0 || h == 0 {
@ -233,19 +250,18 @@ func (c *context) drawGame(graphicsDriver graphicsdriver.Graphics) {
gtx += offsetX gtx += offsetX
gty += offsetY gty += offsetY
var filter graphicsdriver.Filter var shader *Shader
var screenFilter bool
switch { switch {
case !theGlobalState.isScreenFilterEnabled(): case !theGlobalState.isScreenFilterEnabled():
filter = graphicsdriver.FilterNearest shader = c.nearestShader
case math.Floor(s) == s: case math.Floor(s) == s:
filter = graphicsdriver.FilterNearest shader = c.nearestShader
case s > 1: case s > 1:
screenFilter = true shader = c.screenShader
default: default:
// screenShader works with >=1 scale, but does not well with <1 scale. // screenShader works with >=1 scale, but does not well with <1 scale.
// Use regular FilterLinear instead so far (#669). // Use regular FilterLinear instead so far (#669).
filter = graphicsdriver.FilterLinear shader = c.linearShader
} }
dstRegion := graphicsdriver.Region{ dstRegion := graphicsdriver.Region{
@ -263,20 +279,15 @@ func (c *context) drawGame(graphicsDriver graphicsdriver.Graphics) {
srcs := [graphics.ShaderImageCount]*Image{c.offscreen} srcs := [graphics.ShaderImageCount]*Image{c.offscreen}
var shader *Shader
var uniforms [][]float32
if screenFilter {
shader = c.screenShader
dstWidth, dstHeight := c.screen.width, c.screen.height dstWidth, dstHeight := c.screen.width, c.screen.height
srcWidth, srcHeight := c.offscreen.width, c.offscreen.height srcWidth, srcHeight := c.offscreen.width, c.offscreen.height
uniforms = shader.ConvertUniforms(map[string]interface{}{ uniforms := shader.ConvertUniforms(map[string]interface{}{
"Scale": []float32{ "Scale": []float32{
float32(dstWidth) / float32(srcWidth), float32(dstWidth) / float32(srcWidth),
float32(dstHeight) / float32(srcHeight), float32(dstHeight) / float32(srcHeight),
}, },
}) })
} c.screen.DrawTriangles(srcs, vs, is, affine.ColorMIdentity{}, graphicsdriver.CompositeModeCopy, graphicsdriver.FilterNearest, graphicsdriver.AddressUnsafe, dstRegion, graphicsdriver.Region{}, [graphics.ShaderImageCount - 1][2]float32{}, shader, uniforms, false, true)
c.screen.DrawTriangles(srcs, vs, is, affine.ColorMIdentity{}, graphicsdriver.CompositeModeCopy, filter, graphicsdriver.AddressUnsafe, dstRegion, graphicsdriver.Region{}, [graphics.ShaderImageCount - 1][2]float32{}, shader, uniforms, false, true)
} }
func (c *context) layoutGame(outsideWidth, outsideHeight float64, deviceScaleFactor float64) (int, int) { func (c *context) layoutGame(outsideWidth, outsideHeight float64, deviceScaleFactor float64) (int, int) {