ebiten: reduce duplicated shader compilations

This commit is contained in:
Hajime Hoshi 2022-10-02 20:33:43 +09:00
parent d4e7676daa
commit 0b45ca7057
3 changed files with 25 additions and 13 deletions

View File

@ -67,8 +67,8 @@ func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
var ( var (
screenShader *Shader screenShader *Shader
nearestFilterShader = &Shader{shader: mipmap.NearestFilterShader} NearestFilterShader = &Shader{shader: mipmap.NearestFilterShader}
linearFilterShader = &Shader{shader: mipmap.LinearFilterShader} LinearFilterShader = &Shader{shader: mipmap.LinearFilterShader}
) )
func init() { func init() {
@ -242,15 +242,15 @@ func (c *context) drawGame(graphicsDriver graphicsdriver.Graphics) {
var shader *Shader var shader *Shader
switch { switch {
case !theGlobalState.isScreenFilterEnabled(): case !theGlobalState.isScreenFilterEnabled():
shader = nearestFilterShader shader = NearestFilterShader
case math.Floor(s) == s: case math.Floor(s) == s:
shader = nearestFilterShader shader = NearestFilterShader
case s > 1: case s > 1:
shader = screenShader shader = 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).
shader = linearFilterShader shader = LinearFilterShader
} }
dstRegion := graphicsdriver.Region{ dstRegion := graphicsdriver.Region{

View File

@ -196,7 +196,7 @@ func (i *Image) resolveDotsCacheIfNeeded() {
Width: float32(i.width), Width: float32(i.width),
Height: float32(i.height), Height: float32(i.height),
} }
i.mipmap.DrawTriangles(srcs, vs, is, affine.ColorMIdentity{}, graphicsdriver.CompositeModeCopy, graphicsdriver.FilterNearest, graphicsdriver.AddressUnsafe, dr, graphicsdriver.Region{}, [graphics.ShaderImageCount - 1][2]float32{}, nearestFilterShader.shader, nil, false, true) i.mipmap.DrawTriangles(srcs, vs, is, affine.ColorMIdentity{}, graphicsdriver.CompositeModeCopy, graphicsdriver.FilterNearest, graphicsdriver.AddressUnsafe, dr, graphicsdriver.Region{}, [graphics.ShaderImageCount - 1][2]float32{}, NearestFilterShader.shader, nil, false, true)
} }
func DumpImages(dir string) (string, error) { func DumpImages(dir string) (string, error) {
@ -236,5 +236,5 @@ func (i *Image) Fill(r, g, b, a float32, x, y, width, height int) {
srcs := [graphics.ShaderImageCount]*Image{emptyImage} srcs := [graphics.ShaderImageCount]*Image{emptyImage}
i.DrawTriangles(srcs, vs, is, affine.ColorMIdentity{}, graphicsdriver.CompositeModeCopy, graphicsdriver.FilterNearest, graphicsdriver.AddressUnsafe, dstRegion, graphicsdriver.Region{}, [graphics.ShaderImageCount - 1][2]float32{}, nearestFilterShader, nil, false, true) i.DrawTriangles(srcs, vs, is, affine.ColorMIdentity{}, graphicsdriver.CompositeModeCopy, graphicsdriver.FilterNearest, graphicsdriver.AddressUnsafe, dstRegion, graphicsdriver.Region{}, [graphics.ShaderImageCount - 1][2]float32{}, NearestFilterShader, nil, false, true)
} }

View File

@ -81,11 +81,23 @@ func builtinShader(filter graphicsdriver.Filter, address graphicsdriver.Address,
return s return s
} }
src := builtinshader.Shader(filter, address, useColorM) var shader *Shader
s, err := NewShader(src) if address == graphicsdriver.AddressUnsafe && !useColorM {
if err != nil { switch filter {
panic(fmt.Sprintf("ebiten: NewShader for a built-in shader failed: %v", err)) case graphicsdriver.FilterNearest:
shader = &Shader{shader: ui.NearestFilterShader}
case graphicsdriver.FilterLinear:
shader = &Shader{shader: ui.LinearFilterShader}
}
} else {
src := builtinshader.Shader(filter, address, useColorM)
s, err := NewShader(src)
if err != nil {
panic(fmt.Sprintf("ebiten: NewShader for a built-in shader failed: %v", err))
}
shader = s
} }
builtinShaders[key] = s
return s builtinShaders[key] = shader
return shader
} }