2022-10-02 07:51:01 +02:00
|
|
|
// Copyright 2022 The Ebitengine 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.
|
|
|
|
|
2024-11-16 07:01:38 +01:00
|
|
|
//go:generate go run gen.go
|
|
|
|
//go:generate gofmt -s -w .
|
|
|
|
|
2022-10-02 07:51:01 +02:00
|
|
|
package builtinshader
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
|
|
|
"text/template"
|
2022-10-02 16:24:15 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type Filter int
|
2022-10-02 07:51:01 +02:00
|
|
|
|
2022-10-02 16:24:15 +02:00
|
|
|
const (
|
|
|
|
FilterNearest Filter = iota
|
|
|
|
FilterLinear
|
|
|
|
)
|
|
|
|
|
2023-08-19 21:58:36 +02:00
|
|
|
const FilterCount = 2
|
|
|
|
|
2022-10-02 16:24:15 +02:00
|
|
|
type Address int
|
|
|
|
|
|
|
|
const (
|
|
|
|
AddressUnsafe Address = iota
|
|
|
|
AddressClampToZero
|
|
|
|
AddressRepeat
|
2022-10-02 07:51:01 +02:00
|
|
|
)
|
|
|
|
|
2023-08-19 21:58:36 +02:00
|
|
|
const AddressCount = 3
|
|
|
|
|
2022-10-02 07:51:01 +02:00
|
|
|
const (
|
|
|
|
UniformColorMBody = "ColorMBody"
|
|
|
|
UniformColorMTranslation = "ColorMTranslation"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2023-08-19 21:58:36 +02:00
|
|
|
shaders [FilterCount][AddressCount][2][]byte
|
2022-10-02 07:51:01 +02:00
|
|
|
shadersM sync.Mutex
|
|
|
|
)
|
|
|
|
|
2023-08-01 04:41:27 +02:00
|
|
|
var tmpl = template.Must(template.New("tmpl").Parse(`//kage:unit pixels
|
2023-04-16 11:56:14 +02:00
|
|
|
|
|
|
|
package main
|
2022-10-02 07:51:01 +02:00
|
|
|
|
|
|
|
{{if .UseColorM}}
|
|
|
|
var ColorMBody mat4
|
|
|
|
var ColorMTranslation vec4
|
|
|
|
{{end}}
|
|
|
|
|
|
|
|
{{if eq .Address .AddressRepeat}}
|
2024-10-24 05:16:05 +02:00
|
|
|
func adjustSrcPosForAddressRepeat(p vec2) vec2 {
|
2023-08-27 20:39:27 +02:00
|
|
|
origin := imageSrc0Origin()
|
|
|
|
size := imageSrc0Size()
|
2022-10-02 07:51:01 +02:00
|
|
|
return mod(p - origin, size) + origin
|
|
|
|
}
|
|
|
|
{{end}}
|
|
|
|
|
2023-09-20 18:33:27 +02:00
|
|
|
func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
|
2022-10-02 07:51:01 +02:00
|
|
|
{{if eq .Filter .FilterNearest}}
|
|
|
|
{{if eq .Address .AddressUnsafe}}
|
2023-09-20 18:33:27 +02:00
|
|
|
clr := imageSrc0UnsafeAt(srcPos)
|
2022-10-02 07:51:01 +02:00
|
|
|
{{else if eq .Address .AddressClampToZero}}
|
2023-09-20 18:33:27 +02:00
|
|
|
clr := imageSrc0At(srcPos)
|
2022-10-02 07:51:01 +02:00
|
|
|
{{else if eq .Address .AddressRepeat}}
|
2024-10-24 05:16:05 +02:00
|
|
|
clr := imageSrc0At(adjustSrcPosForAddressRepeat(srcPos))
|
2022-10-02 07:51:01 +02:00
|
|
|
{{end}}
|
|
|
|
{{else if eq .Filter .FilterLinear}}
|
2023-09-20 18:33:27 +02:00
|
|
|
p0 := srcPos - 1/2.0
|
|
|
|
p1 := srcPos + 1/2.0
|
2022-10-02 07:51:01 +02:00
|
|
|
|
2022-10-02 09:16:40 +02:00
|
|
|
{{if eq .Address .AddressRepeat}}
|
2024-10-24 05:16:05 +02:00
|
|
|
p0 = adjustSrcPosForAddressRepeat(p0)
|
|
|
|
p1 = adjustSrcPosForAddressRepeat(p1)
|
2022-10-02 07:51:01 +02:00
|
|
|
{{end}}
|
|
|
|
|
|
|
|
{{if eq .Address .AddressUnsafe}}
|
|
|
|
c0 := imageSrc0UnsafeAt(p0)
|
|
|
|
c1 := imageSrc0UnsafeAt(vec2(p1.x, p0.y))
|
|
|
|
c2 := imageSrc0UnsafeAt(vec2(p0.x, p1.y))
|
|
|
|
c3 := imageSrc0UnsafeAt(p1)
|
|
|
|
{{else}}
|
|
|
|
c0 := imageSrc0At(p0)
|
|
|
|
c1 := imageSrc0At(vec2(p1.x, p0.y))
|
|
|
|
c2 := imageSrc0At(vec2(p0.x, p1.y))
|
|
|
|
c3 := imageSrc0At(p1)
|
|
|
|
{{end}}
|
|
|
|
|
2023-08-31 08:06:09 +02:00
|
|
|
rate := fract(p1)
|
2022-10-02 07:51:01 +02:00
|
|
|
clr := mix(mix(c0, c1, rate.x), mix(c2, c3, rate.x), rate.y)
|
|
|
|
{{end}}
|
|
|
|
|
|
|
|
{{if .UseColorM}}
|
|
|
|
// Un-premultiply alpha.
|
|
|
|
// When the alpha is 0, 1-sign(alpha) is 1.0, which means division does nothing.
|
|
|
|
clr.rgb /= clr.a + (1-sign(clr.a))
|
2022-10-02 18:09:24 +02:00
|
|
|
// Apply the clr matrix.
|
2022-10-02 07:51:01 +02:00
|
|
|
clr = (ColorMBody * clr) + ColorMTranslation
|
|
|
|
// Premultiply alpha
|
|
|
|
clr.rgb *= clr.a
|
|
|
|
// Apply the color scale.
|
|
|
|
clr *= color
|
|
|
|
// Clamp the output.
|
|
|
|
clr.rgb = min(clr.rgb, clr.a)
|
|
|
|
{{else}}
|
|
|
|
// Apply the color scale.
|
|
|
|
clr *= color
|
|
|
|
{{end}}
|
|
|
|
|
|
|
|
return clr
|
|
|
|
}
|
|
|
|
|
|
|
|
`))
|
|
|
|
|
2024-04-20 14:21:57 +02:00
|
|
|
// ShaderSource returns the built-in shader source based on the given parameters.
|
2022-10-02 07:51:01 +02:00
|
|
|
//
|
|
|
|
// The returned shader always uses a color matrix so far.
|
2024-04-20 14:21:57 +02:00
|
|
|
func ShaderSource(filter Filter, address Address, useColorM bool) []byte {
|
2022-10-02 07:51:01 +02:00
|
|
|
shadersM.Lock()
|
|
|
|
defer shadersM.Unlock()
|
|
|
|
|
2023-08-19 21:58:36 +02:00
|
|
|
var c int
|
|
|
|
if useColorM {
|
|
|
|
c = 1
|
2022-10-02 07:51:01 +02:00
|
|
|
}
|
2023-08-19 21:58:36 +02:00
|
|
|
if s := shaders[filter][address][c]; s != nil {
|
2022-10-02 07:51:01 +02:00
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
2022-10-02 13:17:06 +02:00
|
|
|
if err := tmpl.Execute(&buf, struct {
|
2022-10-02 16:24:15 +02:00
|
|
|
Filter Filter
|
|
|
|
FilterNearest Filter
|
|
|
|
FilterLinear Filter
|
|
|
|
Address Address
|
|
|
|
AddressUnsafe Address
|
|
|
|
AddressClampToZero Address
|
|
|
|
AddressRepeat Address
|
2022-10-02 13:17:06 +02:00
|
|
|
UseColorM bool
|
|
|
|
}{
|
|
|
|
Filter: filter,
|
2022-10-02 16:24:15 +02:00
|
|
|
FilterNearest: FilterNearest,
|
|
|
|
FilterLinear: FilterLinear,
|
2022-10-02 13:17:06 +02:00
|
|
|
Address: address,
|
2022-10-02 16:24:15 +02:00
|
|
|
AddressUnsafe: AddressUnsafe,
|
|
|
|
AddressClampToZero: AddressClampToZero,
|
|
|
|
AddressRepeat: AddressRepeat,
|
2022-10-02 13:17:06 +02:00
|
|
|
UseColorM: useColorM,
|
2022-10-02 07:51:01 +02:00
|
|
|
}); err != nil {
|
|
|
|
panic(fmt.Sprintf("builtinshader: tmpl.Execute failed: %v", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
b := buf.Bytes()
|
2023-08-19 21:58:36 +02:00
|
|
|
shaders[filter][address][c] = b
|
2022-10-02 07:51:01 +02:00
|
|
|
return b
|
|
|
|
}
|
2024-04-20 14:21:57 +02:00
|
|
|
|
2024-11-16 07:01:38 +01:00
|
|
|
//ebitengine:shader
|
|
|
|
const ScreenShaderSource = `//kage:unit pixels
|
2024-04-20 14:21:57 +02:00
|
|
|
|
|
|
|
package main
|
|
|
|
|
2024-08-25 11:57:33 +02:00
|
|
|
func Fragment(dstPos vec4, srcPos vec2) vec4 {
|
2024-04-20 14:21:57 +02:00
|
|
|
// Blend source colors in a square region, which size is 1/scale.
|
|
|
|
scale := imageDstSize()/imageSrc0Size()
|
2024-10-22 07:29:46 +02:00
|
|
|
p0 := srcPos - 1/2.0/scale
|
|
|
|
p1 := srcPos + 1/2.0/scale
|
2024-04-20 14:21:57 +02:00
|
|
|
|
|
|
|
// 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)
|
|
|
|
|
|
|
|
rate := clamp(fract(p1)*scale, 0, 1)
|
|
|
|
return mix(mix(c0, c1, rate.x), mix(c2, c3, rate.x), rate.y)
|
|
|
|
}
|
2024-11-16 07:01:38 +01:00
|
|
|
`
|
2024-05-04 19:18:14 +02:00
|
|
|
|
2024-11-16 07:01:38 +01:00
|
|
|
//ebitengine:shader
|
|
|
|
const ClearShaderSource = `//kage:unit pixels
|
2024-05-04 19:18:14 +02:00
|
|
|
|
|
|
|
package main
|
|
|
|
|
2024-08-25 11:57:33 +02:00
|
|
|
func Fragment() vec4 {
|
2024-05-04 19:18:14 +02:00
|
|
|
return vec4(0)
|
|
|
|
}
|
2024-11-16 07:01:38 +01:00
|
|
|
`
|