mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-25 03:08:54 +01:00
internal/shaderir/metal: bug fix: define the 'mod' function correctly
Closes #2006
This commit is contained in:
parent
d4d4b9c070
commit
3c1e64dd67
@ -52,7 +52,12 @@ const Prelude = `#include <metal_stdlib>
|
||||
|
||||
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) {
|
||||
c := &compileContext{
|
||||
|
@ -103,7 +103,8 @@ func builtinFuncString(f shaderir.BuiltinFunc) string {
|
||||
case shaderir.Inversesqrt:
|
||||
return "rsqrt"
|
||||
case shaderir.Mod:
|
||||
return "fmod"
|
||||
// mod is a special function that Metal doesn't originally has (#2006).
|
||||
return "mod"
|
||||
case shaderir.Texture2DF:
|
||||
return "?(texture2D)"
|
||||
}
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user