ebiten: move the builtin shader to internal/builtinshader

Updates #2861
This commit is contained in:
Hajime Hoshi 2024-04-20 21:21:57 +09:00
parent 21a906bc82
commit bc9e9d8562
7 changed files with 33 additions and 32 deletions

View File

@ -179,7 +179,7 @@ func builtinShader(filter builtinshader.Filter, address builtinshader.Address) *
return s
}
src := builtinshader.Shader(filter, address, true)
src := builtinshader.ShaderSource(filter, address, true)
s, err := ebiten.NewShader(src)
if err != nil {
panic(fmt.Sprintf("colorm: NewShader for a built-in shader failed: %v", err))

View File

@ -21,32 +21,10 @@ import (
"sync/atomic"
"github.com/hajimehoshi/ebiten/v2/internal/atlas"
"github.com/hajimehoshi/ebiten/v2/internal/builtinshader"
"github.com/hajimehoshi/ebiten/v2/internal/ui"
)
const screenShaderSrc = `//kage:unit pixels
package main
func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
// Blend source colors in a square region, which size is 1/scale.
scale := imageDstSize()/imageSrc0Size()
pos := srcPos
p0 := pos - 1/2.0/scale
p1 := pos + 1/2.0/scale
// Texels must be in the source rect, so it is not necessary to check.
c0 := imageSrc0UnsafeAt(p0)
c1 := imageSrc0UnsafeAt(vec2(p1.x, p0.y))
c2 := imageSrc0UnsafeAt(vec2(p0.x, p1.y))
c3 := imageSrc0UnsafeAt(p1)
// p is the p1 value in one pixel assuming that the pixel's upper-left is (0, 0) and the lower-right is (1, 1).
rate := clamp(fract(p1)*scale, 0, 1)
return mix(mix(c0, c1, rate.x), mix(c2, c3, rate.x), rate.y)
}
`
var screenFilterEnabled = int32(1)
func isScreenFilterEnabled() bool {
@ -76,7 +54,7 @@ func newGameForUI(game Game, transparent bool) *gameForUI {
transparent: transparent,
}
s, err := NewShader([]byte(screenShaderSrc))
s, err := NewShader(builtinshader.ScreenShaderSource)
if err != nil {
panic(fmt.Sprintf("ebiten: compiling the screen shader failed: %v", err))
}

View File

@ -88,7 +88,7 @@ var (
func init() {
var wg errgroup.Group
wg.Go(func() error {
ir, err := graphics.CompileShader([]byte(builtinshader.Shader(builtinshader.FilterNearest, builtinshader.AddressUnsafe, false)))
ir, err := graphics.CompileShader([]byte(builtinshader.ShaderSource(builtinshader.FilterNearest, builtinshader.AddressUnsafe, false)))
if err != nil {
return fmt.Errorf("atlas: compiling the nearest shader failed: %w", err)
}
@ -96,7 +96,7 @@ func init() {
return nil
})
wg.Go(func() error {
ir, err := graphics.CompileShader([]byte(builtinshader.Shader(builtinshader.FilterLinear, builtinshader.AddressUnsafe, false)))
ir, err := graphics.CompileShader([]byte(builtinshader.ShaderSource(builtinshader.FilterLinear, builtinshader.AddressUnsafe, false)))
if err != nil {
return fmt.Errorf("atlas: compiling the linear shader failed: %w", err)
}

View File

@ -123,10 +123,10 @@ func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
`))
// Shader returns the built-in shader based on the given parameters.
// ShaderSource returns the built-in shader source based on the given parameters.
//
// The returned shader always uses a color matrix so far.
func Shader(filter Filter, address Address, useColorM bool) []byte {
func ShaderSource(filter Filter, address Address, useColorM bool) []byte {
shadersM.Lock()
defer shadersM.Unlock()
@ -165,3 +165,26 @@ func Shader(filter Filter, address Address, useColorM bool) []byte {
shaders[filter][address][c] = b
return b
}
var ScreenShaderSource = []byte(`//kage:unit pixels
package main
func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
// Blend source colors in a square region, which size is 1/scale.
scale := imageDstSize()/imageSrc0Size()
pos := srcPos
p0 := pos - 1/2.0/scale
p1 := pos + 1/2.0/scale
// Texels must be in the source rect, so it is not necessary to check.
c0 := imageSrc0UnsafeAt(p0)
c1 := imageSrc0UnsafeAt(vec2(p1.x, p0.y))
c2 := imageSrc0UnsafeAt(vec2(p0.x, p1.y))
c3 := imageSrc0UnsafeAt(p1)
// p is the p1 value in one pixel assuming that the pixel's upper-left is (0, 0) and the lower-right is (1, 1).
rate := clamp(fract(p1)*scale, 0, 1)
return mix(mix(c0, c1, rate.x), mix(c2, c3, rate.x), rate.y)
}
`)

View File

@ -31,7 +31,7 @@ import (
var nearestFilterShader *graphicscommand.Shader
func init() {
ir, err := graphics.CompileShader([]byte(builtinshader.Shader(builtinshader.FilterNearest, builtinshader.AddressUnsafe, false)))
ir, err := graphics.CompileShader([]byte(builtinshader.ShaderSource(builtinshader.FilterNearest, builtinshader.AddressUnsafe, false)))
if err != nil {
panic(fmt.Sprintf("graphicscommand: compiling the nearest shader failed: %v", err))
}

View File

@ -22,7 +22,7 @@ import (
)
func BenchmarkFilter(b *testing.B) {
src := builtinshader.Shader(builtinshader.FilterNearest, builtinshader.AddressUnsafe, false)
src := builtinshader.ShaderSource(builtinshader.FilterNearest, builtinshader.AddressUnsafe, false)
s, err := graphics.CompileShader(src)
if err != nil {
b.Fatal(err)

View File

@ -107,7 +107,7 @@ func builtinShader(filter builtinshader.Filter, address builtinshader.Address, u
shader = &Shader{shader: ui.LinearFilterShader}
}
} else {
src := builtinshader.Shader(filter, address, useColorM)
src := builtinshader.ShaderSource(filter, address, useColorM)
s, err := NewShader(src)
if err != nil {
panic(fmt.Sprintf("ebiten: NewShader for a built-in shader failed: %v", err))