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
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.

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())
}
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.

View File

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

View File

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

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/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))
}

View File

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