From 534d82c17d4691b9af5612e2e60e802e2c781cbf Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 2 Oct 2022 23:24:15 +0900 Subject: [PATCH] internal/builtinshader: move Filter and Address from internal/graphicsdriver --- graphics.go | 5 +-- image.go | 24 +++++++------- internal/builtinshader/shader.go | 45 +++++++++++++++++--------- internal/graphicscommand/image_test.go | 2 +- internal/graphicsdriver/filter.go | 30 ----------------- internal/restorable/shader.go | 5 ++- shader.go | 13 ++++---- 7 files changed, 53 insertions(+), 71 deletions(-) delete mode 100644 internal/graphicsdriver/filter.go diff --git a/graphics.go b/graphics.go index 1d46157d0..0c6df5119 100644 --- a/graphics.go +++ b/graphics.go @@ -15,6 +15,7 @@ package ebiten import ( + "github.com/hajimehoshi/ebiten/v2/internal/builtinshader" "github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver" "github.com/hajimehoshi/ebiten/v2/internal/ui" ) @@ -24,10 +25,10 @@ type Filter int const ( // FilterNearest represents nearest (crisp-edged) filter - FilterNearest Filter = Filter(graphicsdriver.FilterNearest) + FilterNearest Filter = Filter(builtinshader.FilterNearest) // FilterLinear represents linear filter - FilterLinear Filter = Filter(graphicsdriver.FilterLinear) + FilterLinear Filter = Filter(builtinshader.FilterLinear) ) // CompositeMode represents Porter-Duff composition mode. diff --git a/image.go b/image.go index 6cddaa17b..02c7d36c6 100644 --- a/image.go +++ b/image.go @@ -97,8 +97,8 @@ func (i *Image) Fill(clr color.Color) { i.image.Fill(crf, cgf, cbf, caf, x, y, b.Dx(), b.Dy()) } -func canSkipMipmap(geom GeoM, filter graphicsdriver.Filter) bool { - if filter != graphicsdriver.FilterLinear { +func canSkipMipmap(geom GeoM, filter builtinshader.Filter) bool { + if filter != builtinshader.FilterLinear { return true } return geom.det2x2() >= 0.999 @@ -216,7 +216,7 @@ func (i *Image) DrawImage(img *Image, options *DrawImageOptions) { } mode := graphicsdriver.CompositeMode(options.CompositeMode) - filter := graphicsdriver.Filter(options.Filter) + filter := builtinshader.Filter(options.Filter) if offsetX, offsetY := i.adjustPosition(0, 0); offsetX != 0 || offsetY != 0 { options.GeoM.Translate(float64(offsetX), float64(offsetY)) @@ -233,7 +233,7 @@ func (i *Image) DrawImage(img *Image, options *DrawImageOptions) { srcs := [graphics.ShaderImageCount]*ui.Image{img.image} useColorM := !colorm.IsIdentity() - shader := builtinShader(graphicsdriver.Filter(filter), graphicsdriver.AddressUnsafe, useColorM) + shader := builtinShader(builtinshader.Filter(filter), builtinshader.AddressUnsafe, useColorM) var uniforms [][]float32 if useColorM { var body [16]float32 @@ -281,13 +281,13 @@ type Address int const ( // AddressUnsafe means there is no guarantee when the texture coodinates are out of range. - AddressUnsafe Address = Address(graphicsdriver.AddressUnsafe) + AddressUnsafe Address = Address(builtinshader.AddressUnsafe) // AddressClampToZero means that out-of-range texture coordinates return 0 (transparent). - AddressClampToZero Address = Address(graphicsdriver.AddressClampToZero) + AddressClampToZero Address = Address(builtinshader.AddressClampToZero) // AddressRepeat means that texture coordinates wrap to the other side of the texture. - AddressRepeat Address = Address(graphicsdriver.AddressRepeat) + AddressRepeat Address = Address(builtinshader.AddressRepeat) ) // FillRule is the rule whether an overlapped region is rendered with DrawTriangles(Shader). @@ -398,13 +398,13 @@ func (i *Image) DrawTriangles(vertices []Vertex, indices []uint16, img *Image, o mode := graphicsdriver.CompositeMode(options.CompositeMode) - address := graphicsdriver.Address(options.Address) + address := builtinshader.Address(options.Address) var sr graphicsdriver.Region - if address != graphicsdriver.AddressUnsafe { + if address != builtinshader.AddressUnsafe { sr = img.adjustedRegion() } - filter := graphicsdriver.Filter(options.Filter) + filter := builtinshader.Filter(options.Filter) colorm, cr, cg, cb, ca := colorMToScale(options.ColorM.affineColorM()) @@ -443,7 +443,7 @@ func (i *Image) DrawTriangles(vertices []Vertex, indices []uint16, img *Image, o srcs := [graphics.ShaderImageCount]*ui.Image{img.image} useColorM := !colorm.IsIdentity() - shader := builtinShader(graphicsdriver.Filter(filter), graphicsdriver.Address(address), useColorM) + shader := builtinShader(builtinshader.Filter(filter), builtinshader.Address(address), useColorM) var uniforms [][]float32 if useColorM { var body [16]float32 @@ -455,7 +455,7 @@ func (i *Image) DrawTriangles(vertices []Vertex, indices []uint16, img *Image, o }) } - i.image.DrawTriangles(srcs, vs, is, mode, i.adjustedRegion(), sr, [graphics.ShaderImageCount - 1][2]float32{}, shader.shader, uniforms, options.FillRule == EvenOdd, filter != graphicsdriver.FilterLinear) + i.image.DrawTriangles(srcs, vs, is, mode, i.adjustedRegion(), sr, [graphics.ShaderImageCount - 1][2]float32{}, shader.shader, uniforms, options.FillRule == EvenOdd, filter != builtinshader.FilterLinear) } // DrawTrianglesShaderOptions represents options for DrawTrianglesShader. diff --git a/internal/builtinshader/shader.go b/internal/builtinshader/shader.go index 35c12aced..0cecf0908 100644 --- a/internal/builtinshader/shader.go +++ b/internal/builtinshader/shader.go @@ -19,8 +19,21 @@ import ( "fmt" "sync" "text/template" +) - "github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver" +type Filter int + +const ( + FilterNearest Filter = iota + FilterLinear +) + +type Address int + +const ( + AddressUnsafe Address = iota + AddressClampToZero + AddressRepeat ) const ( @@ -29,8 +42,8 @@ const ( ) type key struct { - Filter graphicsdriver.Filter - Address graphicsdriver.Address + Filter Filter + Address Address UseColorM bool } @@ -117,7 +130,7 @@ func Fragment(position vec4, texCoord vec2, color vec4) vec4 { // Shader returns the built-in shader based on the given parameters. // // The returned shader always uses a color matrix so far. -func Shader(filter graphicsdriver.Filter, address graphicsdriver.Address, useColorM bool) []byte { +func Shader(filter Filter, address Address, useColorM bool) []byte { shadersM.Lock() defer shadersM.Unlock() @@ -132,22 +145,22 @@ func Shader(filter graphicsdriver.Filter, address graphicsdriver.Address, useCol var buf bytes.Buffer if err := tmpl.Execute(&buf, struct { - Filter graphicsdriver.Filter - FilterNearest graphicsdriver.Filter - FilterLinear graphicsdriver.Filter - Address graphicsdriver.Address - AddressUnsafe graphicsdriver.Address - AddressClampToZero graphicsdriver.Address - AddressRepeat graphicsdriver.Address + Filter Filter + FilterNearest Filter + FilterLinear Filter + Address Address + AddressUnsafe Address + AddressClampToZero Address + AddressRepeat Address UseColorM bool }{ Filter: filter, - FilterNearest: graphicsdriver.FilterNearest, - FilterLinear: graphicsdriver.FilterLinear, + FilterNearest: FilterNearest, + FilterLinear: FilterLinear, Address: address, - AddressUnsafe: graphicsdriver.AddressUnsafe, - AddressClampToZero: graphicsdriver.AddressClampToZero, - AddressRepeat: graphicsdriver.AddressRepeat, + AddressUnsafe: AddressUnsafe, + AddressClampToZero: AddressClampToZero, + AddressRepeat: AddressRepeat, UseColorM: useColorM, }); err != nil { panic(fmt.Sprintf("builtinshader: tmpl.Execute failed: %v", err)) diff --git a/internal/graphicscommand/image_test.go b/internal/graphicscommand/image_test.go index 5681203d8..3fa3ab0b5 100644 --- a/internal/graphicscommand/image_test.go +++ b/internal/graphicscommand/image_test.go @@ -30,7 +30,7 @@ import ( var nearestFilterShader *graphicscommand.Shader func init() { - ir, err := graphics.CompileShader([]byte(builtinshader.Shader(graphicsdriver.FilterNearest, graphicsdriver.AddressUnsafe, false))) + ir, err := graphics.CompileShader([]byte(builtinshader.Shader(builtinshader.FilterNearest, builtinshader.AddressUnsafe, false))) if err != nil { panic(fmt.Sprintf("graphicscommand: compiling the nearest shader failed: %v", err)) } diff --git a/internal/graphicsdriver/filter.go b/internal/graphicsdriver/filter.go deleted file mode 100644 index f2143c252..000000000 --- a/internal/graphicsdriver/filter.go +++ /dev/null @@ -1,30 +0,0 @@ -// 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. - -package graphicsdriver - -type Filter int - -const ( - FilterNearest Filter = iota - FilterLinear -) - -type Address int - -const ( - AddressUnsafe Address = iota - AddressClampToZero - AddressRepeat -) diff --git a/internal/restorable/shader.go b/internal/restorable/shader.go index 85e404bb0..be1c9d4ba 100644 --- a/internal/restorable/shader.go +++ b/internal/restorable/shader.go @@ -20,7 +20,6 @@ import ( "github.com/hajimehoshi/ebiten/v2/internal/builtinshader" "github.com/hajimehoshi/ebiten/v2/internal/graphics" "github.com/hajimehoshi/ebiten/v2/internal/graphicscommand" - "github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver" "github.com/hajimehoshi/ebiten/v2/internal/shaderir" ) @@ -56,14 +55,14 @@ var ( func init() { { - ir, err := graphics.CompileShader([]byte(builtinshader.Shader(graphicsdriver.FilterNearest, graphicsdriver.AddressUnsafe, false))) + ir, err := graphics.CompileShader([]byte(builtinshader.Shader(builtinshader.FilterNearest, builtinshader.AddressUnsafe, false))) if err != nil { panic(fmt.Sprintf("restorable: compiling the nearest shader failed: %v", err)) } NearestFilterShader = NewShader(ir) } { - ir, err := graphics.CompileShader([]byte(builtinshader.Shader(graphicsdriver.FilterLinear, graphicsdriver.AddressUnsafe, false))) + ir, err := graphics.CompileShader([]byte(builtinshader.Shader(builtinshader.FilterLinear, builtinshader.AddressUnsafe, false))) if err != nil { panic(fmt.Sprintf("restorable: compiling the linear shader failed: %v", err)) } diff --git a/shader.go b/shader.go index a8dc0e5ab..5871256ae 100644 --- a/shader.go +++ b/shader.go @@ -20,7 +20,6 @@ import ( "github.com/hajimehoshi/ebiten/v2/internal/builtinshader" "github.com/hajimehoshi/ebiten/v2/internal/graphics" - "github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver" "github.com/hajimehoshi/ebiten/v2/internal/ui" ) @@ -58,8 +57,8 @@ func (s *Shader) convertUniforms(uniforms map[string]interface{}) [][]float32 { } type builtinShaderKey struct { - filter graphicsdriver.Filter - address graphicsdriver.Address + filter builtinshader.Filter + address builtinshader.Address useColorM bool } @@ -68,7 +67,7 @@ var ( builtinShadersM sync.Mutex ) -func builtinShader(filter graphicsdriver.Filter, address graphicsdriver.Address, useColorM bool) *Shader { +func builtinShader(filter builtinshader.Filter, address builtinshader.Address, useColorM bool) *Shader { builtinShadersM.Lock() defer builtinShadersM.Unlock() @@ -82,11 +81,11 @@ func builtinShader(filter graphicsdriver.Filter, address graphicsdriver.Address, } var shader *Shader - if address == graphicsdriver.AddressUnsafe && !useColorM { + if address == builtinshader.AddressUnsafe && !useColorM { switch filter { - case graphicsdriver.FilterNearest: + case builtinshader.FilterNearest: shader = &Shader{shader: ui.NearestFilterShader} - case graphicsdriver.FilterLinear: + case builtinshader.FilterLinear: shader = &Shader{shader: ui.LinearFilterShader} } } else {