From 8276a53dd6f64ba64442326260a6f4e54858ecea Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 23 Jul 2023 16:42:39 +0900 Subject: [PATCH] ebiten: add TestShaderIVec --- shader_test.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/shader_test.go b/shader_test.go index 51fdd17d1..434d92697 100644 --- a/shader_test.go +++ b/shader_test.go @@ -1812,3 +1812,45 @@ func Fragment(position vec4, texCoord vec2, color vec4) vec4 { }) } } + +func TestShaderIVec(t *testing.T) { + const w, h = 16, 16 + dst := ebiten.NewImage(w, h) + src := ebiten.NewImage(w, h) + + pix := make([]byte, 4*w*h) + for j := 0; j < h; j++ { + for i := 0; i < w; i++ { + pix[4*(j*w+i)] = byte(i) + pix[4*(j*w+i)+1] = byte(j) + pix[4*(j*w+i)+3] = 0xff + } + } + src.WritePixels(pix) + + // Test that ivec2 can take any float values that can be casted to integers. + // This seems the common behavior in shading languages like GLSL, Metal, and HLSL. + shader, err := ebiten.NewShader([]byte(`//kage:unit pixel + +package main + +func Fragment(position vec4, texCoord vec2, color vec4) vec4 { + orig, _ := imageSrcRegionOnTexture() + pos := ivec2(3.1, 4.2) + return imageSrc0At(vec2(pos.x, pos.y) + orig) +} +`)) + if err != nil { + t.Fatal(err) + } + + op := &ebiten.DrawRectShaderOptions{} + op.Images[0] = src + dst.DrawRectShader(w, h, shader, op) + + got := dst.At(0, 0).(color.RGBA) + want := color.RGBA{3, 4, 0, 0xff} + if got != want { + t.Errorf("got: %v, want: %v", got, want) + } +}