internal/builtinshader: move Filter and Address from internal/graphicsdriver

This commit is contained in:
Hajime Hoshi 2022-10-02 23:24:15 +09:00
parent 9c07b20f2b
commit 534d82c17d
7 changed files with 53 additions and 71 deletions

View File

@ -15,6 +15,7 @@
package ebiten package ebiten
import ( import (
"github.com/hajimehoshi/ebiten/v2/internal/builtinshader"
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver" "github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
"github.com/hajimehoshi/ebiten/v2/internal/ui" "github.com/hajimehoshi/ebiten/v2/internal/ui"
) )
@ -24,10 +25,10 @@ type Filter int
const ( const (
// FilterNearest represents nearest (crisp-edged) filter // FilterNearest represents nearest (crisp-edged) filter
FilterNearest Filter = Filter(graphicsdriver.FilterNearest) FilterNearest Filter = Filter(builtinshader.FilterNearest)
// FilterLinear represents linear filter // FilterLinear represents linear filter
FilterLinear Filter = Filter(graphicsdriver.FilterLinear) FilterLinear Filter = Filter(builtinshader.FilterLinear)
) )
// CompositeMode represents Porter-Duff composition mode. // CompositeMode represents Porter-Duff composition mode.

View File

@ -97,8 +97,8 @@ func (i *Image) Fill(clr color.Color) {
i.image.Fill(crf, cgf, cbf, caf, x, y, b.Dx(), b.Dy()) i.image.Fill(crf, cgf, cbf, caf, x, y, b.Dx(), b.Dy())
} }
func canSkipMipmap(geom GeoM, filter graphicsdriver.Filter) bool { func canSkipMipmap(geom GeoM, filter builtinshader.Filter) bool {
if filter != graphicsdriver.FilterLinear { if filter != builtinshader.FilterLinear {
return true return true
} }
return geom.det2x2() >= 0.999 return geom.det2x2() >= 0.999
@ -216,7 +216,7 @@ func (i *Image) DrawImage(img *Image, options *DrawImageOptions) {
} }
mode := graphicsdriver.CompositeMode(options.CompositeMode) 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 { if offsetX, offsetY := i.adjustPosition(0, 0); offsetX != 0 || offsetY != 0 {
options.GeoM.Translate(float64(offsetX), float64(offsetY)) 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} srcs := [graphics.ShaderImageCount]*ui.Image{img.image}
useColorM := !colorm.IsIdentity() useColorM := !colorm.IsIdentity()
shader := builtinShader(graphicsdriver.Filter(filter), graphicsdriver.AddressUnsafe, useColorM) shader := builtinShader(builtinshader.Filter(filter), builtinshader.AddressUnsafe, useColorM)
var uniforms [][]float32 var uniforms [][]float32
if useColorM { if useColorM {
var body [16]float32 var body [16]float32
@ -281,13 +281,13 @@ type Address int
const ( const (
// AddressUnsafe means there is no guarantee when the texture coodinates are out of range. // 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 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 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). // 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) mode := graphicsdriver.CompositeMode(options.CompositeMode)
address := graphicsdriver.Address(options.Address) address := builtinshader.Address(options.Address)
var sr graphicsdriver.Region var sr graphicsdriver.Region
if address != graphicsdriver.AddressUnsafe { if address != builtinshader.AddressUnsafe {
sr = img.adjustedRegion() sr = img.adjustedRegion()
} }
filter := graphicsdriver.Filter(options.Filter) filter := builtinshader.Filter(options.Filter)
colorm, cr, cg, cb, ca := colorMToScale(options.ColorM.affineColorM()) 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} srcs := [graphics.ShaderImageCount]*ui.Image{img.image}
useColorM := !colorm.IsIdentity() useColorM := !colorm.IsIdentity()
shader := builtinShader(graphicsdriver.Filter(filter), graphicsdriver.Address(address), useColorM) shader := builtinShader(builtinshader.Filter(filter), builtinshader.Address(address), useColorM)
var uniforms [][]float32 var uniforms [][]float32
if useColorM { if useColorM {
var body [16]float32 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. // DrawTrianglesShaderOptions represents options for DrawTrianglesShader.

View File

@ -19,8 +19,21 @@ import (
"fmt" "fmt"
"sync" "sync"
"text/template" "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 ( const (
@ -29,8 +42,8 @@ const (
) )
type key struct { type key struct {
Filter graphicsdriver.Filter Filter Filter
Address graphicsdriver.Address Address Address
UseColorM bool 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. // Shader returns the built-in shader based on the given parameters.
// //
// The returned shader always uses a color matrix so far. // 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() shadersM.Lock()
defer shadersM.Unlock() defer shadersM.Unlock()
@ -132,22 +145,22 @@ func Shader(filter graphicsdriver.Filter, address graphicsdriver.Address, useCol
var buf bytes.Buffer var buf bytes.Buffer
if err := tmpl.Execute(&buf, struct { if err := tmpl.Execute(&buf, struct {
Filter graphicsdriver.Filter Filter Filter
FilterNearest graphicsdriver.Filter FilterNearest Filter
FilterLinear graphicsdriver.Filter FilterLinear Filter
Address graphicsdriver.Address Address Address
AddressUnsafe graphicsdriver.Address AddressUnsafe Address
AddressClampToZero graphicsdriver.Address AddressClampToZero Address
AddressRepeat graphicsdriver.Address AddressRepeat Address
UseColorM bool UseColorM bool
}{ }{
Filter: filter, Filter: filter,
FilterNearest: graphicsdriver.FilterNearest, FilterNearest: FilterNearest,
FilterLinear: graphicsdriver.FilterLinear, FilterLinear: FilterLinear,
Address: address, Address: address,
AddressUnsafe: graphicsdriver.AddressUnsafe, AddressUnsafe: AddressUnsafe,
AddressClampToZero: graphicsdriver.AddressClampToZero, AddressClampToZero: AddressClampToZero,
AddressRepeat: graphicsdriver.AddressRepeat, AddressRepeat: AddressRepeat,
UseColorM: useColorM, UseColorM: useColorM,
}); err != nil { }); err != nil {
panic(fmt.Sprintf("builtinshader: tmpl.Execute failed: %v", err)) panic(fmt.Sprintf("builtinshader: tmpl.Execute failed: %v", err))

View File

@ -30,7 +30,7 @@ import (
var nearestFilterShader *graphicscommand.Shader var nearestFilterShader *graphicscommand.Shader
func init() { 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 { if err != nil {
panic(fmt.Sprintf("graphicscommand: compiling the nearest shader failed: %v", err)) panic(fmt.Sprintf("graphicscommand: compiling the nearest shader failed: %v", err))
} }

View File

@ -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
)

View File

@ -20,7 +20,6 @@ import (
"github.com/hajimehoshi/ebiten/v2/internal/builtinshader" "github.com/hajimehoshi/ebiten/v2/internal/builtinshader"
"github.com/hajimehoshi/ebiten/v2/internal/graphics" "github.com/hajimehoshi/ebiten/v2/internal/graphics"
"github.com/hajimehoshi/ebiten/v2/internal/graphicscommand" "github.com/hajimehoshi/ebiten/v2/internal/graphicscommand"
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
"github.com/hajimehoshi/ebiten/v2/internal/shaderir" "github.com/hajimehoshi/ebiten/v2/internal/shaderir"
) )
@ -56,14 +55,14 @@ var (
func init() { 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 { if err != nil {
panic(fmt.Sprintf("restorable: compiling the nearest shader failed: %v", err)) panic(fmt.Sprintf("restorable: compiling the nearest shader failed: %v", err))
} }
NearestFilterShader = NewShader(ir) 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 { if err != nil {
panic(fmt.Sprintf("restorable: compiling the linear shader failed: %v", err)) panic(fmt.Sprintf("restorable: compiling the linear shader failed: %v", err))
} }

View File

@ -20,7 +20,6 @@ import (
"github.com/hajimehoshi/ebiten/v2/internal/builtinshader" "github.com/hajimehoshi/ebiten/v2/internal/builtinshader"
"github.com/hajimehoshi/ebiten/v2/internal/graphics" "github.com/hajimehoshi/ebiten/v2/internal/graphics"
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
"github.com/hajimehoshi/ebiten/v2/internal/ui" "github.com/hajimehoshi/ebiten/v2/internal/ui"
) )
@ -58,8 +57,8 @@ func (s *Shader) convertUniforms(uniforms map[string]interface{}) [][]float32 {
} }
type builtinShaderKey struct { type builtinShaderKey struct {
filter graphicsdriver.Filter filter builtinshader.Filter
address graphicsdriver.Address address builtinshader.Address
useColorM bool useColorM bool
} }
@ -68,7 +67,7 @@ var (
builtinShadersM sync.Mutex 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() builtinShadersM.Lock()
defer builtinShadersM.Unlock() defer builtinShadersM.Unlock()
@ -82,11 +81,11 @@ func builtinShader(filter graphicsdriver.Filter, address graphicsdriver.Address,
} }
var shader *Shader var shader *Shader
if address == graphicsdriver.AddressUnsafe && !useColorM { if address == builtinshader.AddressUnsafe && !useColorM {
switch filter { switch filter {
case graphicsdriver.FilterNearest: case builtinshader.FilterNearest:
shader = &Shader{shader: ui.NearestFilterShader} shader = &Shader{shader: ui.NearestFilterShader}
case graphicsdriver.FilterLinear: case builtinshader.FilterLinear:
shader = &Shader{shader: ui.LinearFilterShader} shader = &Shader{shader: ui.LinearFilterShader}
} }
} else { } else {