Skip to content

Commit

Permalink
internal/shaderir/glsl: bug fix: % was not available on old GLSLs
Browse files Browse the repository at this point in the history
Use a new utility function modInt instead.

Closes hajimehoshi#1951
  • Loading branch information
hajimehoshi committed Jan 11, 2022
1 parent d110716 commit 08ddb42
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
14 changes: 11 additions & 3 deletions internal/shader/shader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,15 @@ import (
"github.com/hajimehoshi/ebiten/v2/internal/shaderir/metal"
)

func glslNormalize(str string) string {
func glslVertexNormalize(str string) string {
p := glsl.VertexPrelude(glsl.GLSLVersionDefault)
if strings.HasPrefix(str, p) {
str = str[len(p):]
}
return strings.TrimSpace(str)
}

func glslFragmentNormalize(str string) string {
p := glsl.FragmentPrelude(glsl.GLSLVersionDefault)
if strings.HasPrefix(str, p) {
str = str[len(p):]
Expand Down Expand Up @@ -158,11 +166,11 @@ func TestCompile(t *testing.T) {

// GLSL
vs, fs := glsl.Compile(s, glsl.GLSLVersionDefault)
if got, want := glslNormalize(vs), glslNormalize(string(tc.VS)); got != want {
if got, want := glslVertexNormalize(vs), glslVertexNormalize(string(tc.VS)); got != want {
compare(t, "GLSL Vertex", got, want)
}
if tc.FS != nil {
if got, want := glslNormalize(fs), glslNormalize(string(tc.FS)); got != want {
if got, want := glslFragmentNormalize(fs), glslFragmentNormalize(string(tc.FS)); got != want {
compare(t, "GLSL Fragment", got, want)
}
}
Expand Down
22 changes: 20 additions & 2 deletions internal/shaderir/glsl/glsl.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,18 @@ const (
GLSLVersionES300
)

// utilFunctions is GLSL utility functions for old GLSL versions.
const utilFunctions = `int modInt(int x, int y) {
return x - y*(x/y);
}`

func VertexPrelude(version GLSLVersion) string {
if version == GLSLVersionES300 {
switch version {
case GLSLVersionDefault:
return utilFunctions
case GLSLVersionES100:
return utilFunctions
case GLSLVersionES300:
return `#version 300 es`
}
return ""
Expand All @@ -48,13 +58,17 @@ func FragmentPrelude(version GLSLVersion) string {
case GLSLVersionES300:
prefix = `#version 300 es` + "\n\n"
}
return prefix + `#if defined(GL_ES)
prelude := prefix + `#if defined(GL_ES)
precision highp float;
#else
#define lowp
#define mediump
#define highp
#endif`
if version == GLSLVersionDefault || version == GLSLVersionES100 {
prelude += "\n\n" + utilFunctions
}
return prelude
}

type compileContext struct {
Expand Down Expand Up @@ -496,6 +510,10 @@ func (c *compileContext) glslBlock(p *shaderir.Program, topBlock, block *shaderi
}
return fmt.Sprintf("%s(%s)", op, glslExpr(&e.Exprs[0]))
case shaderir.Binary:
if e.Op == shaderir.ModOp && (c.version == GLSLVersionDefault || c.version == GLSLVersionES100) {
// '%' is not defined.
return fmt.Sprintf("modInt((%s), (%s))", glslExpr(&e.Exprs[0]), glslExpr(&e.Exprs[1]))
}
return fmt.Sprintf("(%s) %s (%s)", glslExpr(&e.Exprs[0]), e.Op, glslExpr(&e.Exprs[1]))
case shaderir.Selection:
return fmt.Sprintf("(%s) ? (%s) : (%s)", glslExpr(&e.Exprs[0]), glslExpr(&e.Exprs[1]), glslExpr(&e.Exprs[2]))
Expand Down

0 comments on commit 08ddb42

Please sign in to comment.