Skip to content

Commit

Permalink
internal/builtinshader: add shader constants for shaderlister
Browse files Browse the repository at this point in the history
Updates #3157
  • Loading branch information
hajimehoshi committed Nov 16, 2024
1 parent 864f826 commit d021209
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 6 deletions.
2 changes: 1 addition & 1 deletion gameforui.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ var (
// in your implementation of [FinalScreenDrawer], for example.
func DefaultDrawFinalScreen(screen FinalScreen, offscreen *Image, geoM GeoM) {
theScreenShaderOnce.Do(func() {
s, err := newShader(builtinshader.ScreenShaderSource, "screen")
s, err := newShader([]byte(builtinshader.ScreenShaderSource), "screen")
if err != nil {
panic(fmt.Sprintf("ebiten: compiling the screen shader failed: %v", err))
}
Expand Down
53 changes: 53 additions & 0 deletions internal/builtinshader/defs.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

84 changes: 84 additions & 0 deletions internal/builtinshader/gen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// Copyright 2024 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.

//go:build ignore

package main

import (
"bufio"
"fmt"
"os"

"github.com/hajimehoshi/ebiten/v2/internal/builtinshader"
)

func main() {
if err := xmain(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}

const license = `// Copyright 2024 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.
`

func xmain() error {
f, err := os.Create("defs.go")
if err != nil {
return err
}
defer f.Close()

w := bufio.NewWriter(f)

if _, err := w.WriteString("// Code generated by gen.go using 'go generate'. DO NOT EDIT.\n\n"); err != nil {
return err
}
if _, err := w.WriteString(license); err != nil {
return err
}
if _, err := w.WriteString("\npackage builtinshader\n"); err != nil {
return err
}

for _, s := range builtinshader.AppendShaderSources(nil) {
if _, err := w.WriteString("\n"); err != nil {
return err
}
if _, err := w.WriteString("//ebitengine:shader\n"); err != nil {
return err
}
if _, err := fmt.Fprintf(w, "const _ = %q\n", s); err != nil {
return err
}
}

if err := w.Flush(); err != nil {
return err
}
return nil
}
14 changes: 9 additions & 5 deletions internal/builtinshader/shader.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//go:generate go run gen.go
//go:generate gofmt -s -w .

package builtinshader

import (
Expand Down Expand Up @@ -166,7 +169,8 @@ func ShaderSource(filter Filter, address Address, useColorM bool) []byte {
return b
}

var ScreenShaderSource = []byte(`//kage:unit pixels
//ebitengine:shader
const ScreenShaderSource = `//kage:unit pixels
package main
Expand All @@ -185,23 +189,23 @@ func Fragment(dstPos vec4, srcPos vec2) vec4 {
rate := clamp(fract(p1)*scale, 0, 1)
return mix(mix(c0, c1, rate.x), mix(c2, c3, rate.x), rate.y)
}
`)
`

var ClearShaderSource = []byte(`//kage:unit pixels
//ebitengine:shader
const ClearShaderSource = `//kage:unit pixels
package main
func Fragment() vec4 {
return vec4(0)
}
`)
`

func AppendShaderSources(sources [][]byte) [][]byte {
for filter := Filter(0); filter < FilterCount; filter++ {
for address := Address(0); address < AddressCount; address++ {
sources = append(sources, ShaderSource(filter, address, false), ShaderSource(filter, address, true))
}
}
sources = append(sources, ScreenShaderSource, ClearShaderSource)
return sources
}

0 comments on commit d021209

Please sign in to comment.