internal/shaderir/metal: bug fix: define the 'mod' function correctly

Closes #2006
This commit is contained in:
Hajime Hoshi 2022-03-04 18:05:42 +09:00
parent d4d4b9c070
commit 3c1e64dd67
3 changed files with 40 additions and 2 deletions

View File

@ -52,7 +52,12 @@ const Prelude = `#include <metal_stdlib>
using namespace metal; using namespace metal;
constexpr sampler texture_sampler{filter::nearest};` constexpr sampler texture_sampler{filter::nearest};
template<typename T>
T mod(T x, T y) {
return x - y * floor(x/y);
}`
func Compile(p *shaderir.Program, vertex, fragment string) (shader string) { func Compile(p *shaderir.Program, vertex, fragment string) (shader string) {
c := &compileContext{ c := &compileContext{

View File

@ -103,7 +103,8 @@ func builtinFuncString(f shaderir.BuiltinFunc) string {
case shaderir.Inversesqrt: case shaderir.Inversesqrt:
return "rsqrt" return "rsqrt"
case shaderir.Mod: case shaderir.Mod:
return "fmod" // mod is a special function that Metal doesn't originally has (#2006).
return "mod"
case shaderir.Texture2DF: case shaderir.Texture2DF:
return "?(texture2D)" return "?(texture2D)"
} }

View File

@ -1677,3 +1677,35 @@ func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
} }
} }
} }
// Issue #2006
func TestShaderFuncMod(t *testing.T) {
const w, h = 16, 16
dst := ebiten.NewImage(w, h)
s, err := ebiten.NewShader([]byte(`package main
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
r := mod(-0.25, 1.0)
return vec4(r, 0, 0, 1)
}
`))
if err != nil {
t.Fatal(err)
}
dst.DrawRectShader(w/2, h/2, s, nil)
for j := 0; j < h; j++ {
for i := 0; i < w; i++ {
got := dst.At(i, j).(color.RGBA)
var want color.RGBA
if i < w/2 && j < h/2 {
want = color.RGBA{0xc0, 0, 0, 0xff}
}
if !sameColors(got, want, 2) {
t.Errorf("dst.At(%d, %d): got: %v, want: %v", i, j, got, want)
}
}
}
}