From 0b45ca7057fe859086491f73c21f63222a0a1b98 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 2 Oct 2022 20:33:43 +0900 Subject: [PATCH] ebiten: reduce duplicated shader compilations --- internal/ui/context.go | 10 +++++----- internal/ui/image.go | 4 ++-- shader.go | 24 ++++++++++++++++++------ 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/internal/ui/context.go b/internal/ui/context.go index 68b5147d1..b98e5d7f7 100644 --- a/internal/ui/context.go +++ b/internal/ui/context.go @@ -67,8 +67,8 @@ func Fragment(position vec4, texCoord vec2, color vec4) vec4 { var ( screenShader *Shader - nearestFilterShader = &Shader{shader: mipmap.NearestFilterShader} - linearFilterShader = &Shader{shader: mipmap.LinearFilterShader} + NearestFilterShader = &Shader{shader: mipmap.NearestFilterShader} + LinearFilterShader = &Shader{shader: mipmap.LinearFilterShader} ) func init() { @@ -242,15 +242,15 @@ func (c *context) drawGame(graphicsDriver graphicsdriver.Graphics) { var shader *Shader switch { case !theGlobalState.isScreenFilterEnabled(): - shader = nearestFilterShader + shader = NearestFilterShader case math.Floor(s) == s: - shader = nearestFilterShader + shader = NearestFilterShader case s > 1: shader = screenShader default: // screenShader works with >=1 scale, but does not well with <1 scale. // Use regular FilterLinear instead so far (#669). - shader = linearFilterShader + shader = LinearFilterShader } dstRegion := graphicsdriver.Region{ diff --git a/internal/ui/image.go b/internal/ui/image.go index 8d821f378..a6d713b36 100644 --- a/internal/ui/image.go +++ b/internal/ui/image.go @@ -196,7 +196,7 @@ func (i *Image) resolveDotsCacheIfNeeded() { Width: float32(i.width), 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) { @@ -236,5 +236,5 @@ func (i *Image) Fill(r, g, b, a float32, x, y, width, height int) { 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) } diff --git a/shader.go b/shader.go index 4e7e8f62c..a8dc0e5ab 100644 --- a/shader.go +++ b/shader.go @@ -81,11 +81,23 @@ func builtinShader(filter graphicsdriver.Filter, address graphicsdriver.Address, return s } - 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)) + var shader *Shader + if address == graphicsdriver.AddressUnsafe && !useColorM { + switch filter { + 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 }