2020-08-25 19:49:43 +02:00
|
|
|
// Copyright 2020 The Ebiten 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.
|
|
|
|
|
|
|
|
package ebiten_test
|
|
|
|
|
|
|
|
import (
|
2022-01-20 16:50:21 +01:00
|
|
|
"fmt"
|
2020-09-19 21:21:38 +02:00
|
|
|
"image"
|
2020-08-25 19:49:43 +02:00
|
|
|
"image/color"
|
|
|
|
"testing"
|
|
|
|
|
2021-10-02 12:58:48 +02:00
|
|
|
"github.com/hajimehoshi/ebiten/v2"
|
2020-08-25 19:49:43 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestShaderFill(t *testing.T) {
|
|
|
|
const w, h = 16, 16
|
|
|
|
|
2021-10-02 12:58:48 +02:00
|
|
|
dst := ebiten.NewImage(w, h)
|
|
|
|
s, err := ebiten.NewShader([]byte(`package main
|
2020-08-25 19:49:43 +02:00
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
return vec4(1, 0, 0, 1)
|
|
|
|
}
|
|
|
|
`))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-08-25 20:11:18 +02:00
|
|
|
dst.DrawRectShader(w/2, h/2, s, nil)
|
2020-08-25 19:49:43 +02:00
|
|
|
|
2020-08-26 19:00:20 +02:00
|
|
|
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{0xff, 0, 0, 0xff}
|
|
|
|
}
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("dst.At(%d, %d): got: %v, want: %v", i, j, got, want)
|
|
|
|
}
|
|
|
|
}
|
2020-08-25 19:49:43 +02:00
|
|
|
}
|
2020-08-26 19:00:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestShaderFillWithDrawImage(t *testing.T) {
|
|
|
|
const w, h = 16, 16
|
|
|
|
|
2021-10-02 12:58:48 +02:00
|
|
|
dst := ebiten.NewImage(w, h)
|
|
|
|
s, err := ebiten.NewShader([]byte(`package main
|
2020-08-26 19:00:20 +02:00
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
return vec4(1, 0, 0, 1)
|
|
|
|
}
|
|
|
|
`))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-10-02 12:58:48 +02:00
|
|
|
src := ebiten.NewImage(w/2, h/2)
|
|
|
|
op := &ebiten.DrawRectShaderOptions{}
|
2020-09-20 11:13:57 +02:00
|
|
|
op.Images[0] = src
|
|
|
|
dst.DrawRectShader(w/2, h/2, s, op)
|
2020-08-25 19:49:43 +02:00
|
|
|
|
2020-08-26 19:00:20 +02:00
|
|
|
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{0xff, 0, 0, 0xff}
|
|
|
|
}
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("dst.At(%d, %d): got: %v, want: %v", i, j, got, want)
|
|
|
|
}
|
|
|
|
}
|
2020-08-25 19:49:43 +02:00
|
|
|
}
|
|
|
|
}
|
2020-08-25 20:10:24 +02:00
|
|
|
|
2020-09-19 17:49:57 +02:00
|
|
|
func TestShaderFillWithDrawTriangles(t *testing.T) {
|
|
|
|
const w, h = 16, 16
|
|
|
|
|
2021-10-02 12:58:48 +02:00
|
|
|
dst := ebiten.NewImage(w, h)
|
|
|
|
s, err := ebiten.NewShader([]byte(`package main
|
2020-09-19 17:49:57 +02:00
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
return vec4(1, 0, 0, 1)
|
|
|
|
}
|
|
|
|
`))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-10-02 12:58:48 +02:00
|
|
|
src := ebiten.NewImage(w/2, h/2)
|
|
|
|
op := &ebiten.DrawTrianglesShaderOptions{}
|
2020-09-20 11:32:18 +02:00
|
|
|
op.Images[0] = src
|
|
|
|
|
2021-10-02 12:58:48 +02:00
|
|
|
vs := []ebiten.Vertex{
|
2020-09-19 17:49:57 +02:00
|
|
|
{
|
|
|
|
DstX: 0,
|
|
|
|
DstY: 0,
|
|
|
|
SrcX: 0,
|
|
|
|
SrcY: 0,
|
|
|
|
ColorR: 1,
|
|
|
|
ColorG: 1,
|
|
|
|
ColorB: 1,
|
|
|
|
ColorA: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
DstX: w,
|
|
|
|
DstY: 0,
|
|
|
|
SrcX: w / 2,
|
|
|
|
SrcY: 0,
|
|
|
|
ColorR: 1,
|
|
|
|
ColorG: 1,
|
|
|
|
ColorB: 1,
|
|
|
|
ColorA: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
DstX: 0,
|
|
|
|
DstY: h,
|
|
|
|
SrcX: 0,
|
|
|
|
SrcY: h / 2,
|
|
|
|
ColorR: 1,
|
|
|
|
ColorG: 1,
|
|
|
|
ColorB: 1,
|
|
|
|
ColorA: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
DstX: w,
|
|
|
|
DstY: h,
|
|
|
|
SrcX: w / 2,
|
|
|
|
SrcY: h / 2,
|
|
|
|
ColorR: 1,
|
|
|
|
ColorG: 1,
|
|
|
|
ColorB: 1,
|
|
|
|
ColorA: 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
is := []uint16{0, 1, 2, 1, 2, 3}
|
2020-09-20 11:32:18 +02:00
|
|
|
|
|
|
|
dst.DrawTrianglesShader(vs, is, s, op)
|
2020-09-19 17:49:57 +02:00
|
|
|
|
|
|
|
for j := 0; j < h; j++ {
|
|
|
|
for i := 0; i < w; i++ {
|
|
|
|
got := dst.At(i, j).(color.RGBA)
|
|
|
|
want := color.RGBA{0xff, 0, 0, 0xff}
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("dst.At(%d, %d): got: %v, want: %v", i, j, got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-25 20:10:24 +02:00
|
|
|
func TestShaderFunction(t *testing.T) {
|
|
|
|
const w, h = 16, 16
|
|
|
|
|
2021-10-02 12:58:48 +02:00
|
|
|
dst := ebiten.NewImage(w, h)
|
|
|
|
s, err := ebiten.NewShader([]byte(`package main
|
2020-08-25 20:10:24 +02:00
|
|
|
|
|
|
|
func clr(red float) (float, float, float, float) {
|
|
|
|
return red, 0, 0, 1
|
|
|
|
}
|
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
return vec4(clr(1))
|
|
|
|
}
|
|
|
|
`))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
dst.DrawRectShader(w, h, s, nil)
|
|
|
|
|
|
|
|
for j := 0; j < h; j++ {
|
|
|
|
for i := 0; i < w; i++ {
|
|
|
|
got := dst.At(i, j).(color.RGBA)
|
|
|
|
want := color.RGBA{0xff, 0, 0, 0xff}
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("dst.At(%d, %d): got: %v, want: %v", i, j, got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-08-30 14:11:27 +02:00
|
|
|
|
|
|
|
func TestShaderShadowing(t *testing.T) {
|
2021-10-02 12:58:48 +02:00
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
2020-08-30 14:11:27 +02:00
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
var position vec4
|
|
|
|
return position
|
|
|
|
}
|
2020-08-31 17:28:24 +02:00
|
|
|
`)); err == nil {
|
2020-08-30 14:11:27 +02:00
|
|
|
t.Errorf("error must be non-nil but was nil")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestShaderDuplicatedVariables(t *testing.T) {
|
2021-10-02 12:58:48 +02:00
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
2020-08-30 14:11:27 +02:00
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
var foo vec4
|
|
|
|
var foo vec4
|
|
|
|
return foo
|
|
|
|
}
|
2020-08-31 17:28:24 +02:00
|
|
|
`)); err == nil {
|
2020-08-30 14:11:27 +02:00
|
|
|
t.Errorf("error must be non-nil but was nil")
|
|
|
|
}
|
|
|
|
|
2021-10-02 12:58:48 +02:00
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
2020-08-30 14:11:27 +02:00
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
var foo, foo vec4
|
|
|
|
return foo
|
|
|
|
}
|
2020-08-31 17:28:24 +02:00
|
|
|
`)); err == nil {
|
2020-08-30 14:11:27 +02:00
|
|
|
t.Errorf("error must be non-nil but was nil")
|
|
|
|
}
|
|
|
|
|
2021-10-02 12:58:48 +02:00
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
2020-08-30 14:11:27 +02:00
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
var foo vec4
|
|
|
|
foo := vec4(0)
|
|
|
|
return foo
|
|
|
|
}
|
2020-08-31 17:28:24 +02:00
|
|
|
`)); err == nil {
|
2020-08-30 14:11:27 +02:00
|
|
|
t.Errorf("error must be non-nil but was nil")
|
|
|
|
}
|
|
|
|
|
2021-10-02 12:58:48 +02:00
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
2020-08-30 14:11:27 +02:00
|
|
|
|
|
|
|
func Foo() (vec4, vec4) {
|
|
|
|
return vec4(0), vec4(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
foo, foo := Foo()
|
|
|
|
return foo
|
|
|
|
}
|
2020-08-31 17:28:24 +02:00
|
|
|
`)); err == nil {
|
2020-08-30 14:11:27 +02:00
|
|
|
t.Errorf("error must be non-nil but was nil")
|
|
|
|
}
|
|
|
|
}
|
2020-08-31 17:37:14 +02:00
|
|
|
|
2021-02-07 14:24:20 +01:00
|
|
|
func TestShaderDuplicatedFunctions(t *testing.T) {
|
2021-10-02 12:58:48 +02:00
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
2021-02-07 14:24:20 +01:00
|
|
|
|
|
|
|
func Foo() {
|
|
|
|
}
|
|
|
|
|
|
|
|
func Foo() {
|
|
|
|
}
|
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
return vec4(0)
|
|
|
|
}
|
|
|
|
`)); err == nil {
|
|
|
|
t.Errorf("error must be non-nil but was nil")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-31 17:37:14 +02:00
|
|
|
func TestShaderNoMain(t *testing.T) {
|
2021-10-02 12:58:48 +02:00
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
2020-08-31 17:37:14 +02:00
|
|
|
`)); err == nil {
|
|
|
|
t.Errorf("error must be non-nil but was nil")
|
|
|
|
}
|
|
|
|
}
|
2020-09-02 18:15:37 +02:00
|
|
|
|
|
|
|
func TestShaderNoNewVariables(t *testing.T) {
|
2021-10-02 12:58:48 +02:00
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
2020-09-02 18:15:37 +02:00
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
_ := 1
|
2020-09-06 14:30:01 +02:00
|
|
|
return vec4(0)
|
2020-09-02 18:15:37 +02:00
|
|
|
}
|
|
|
|
`)); err == nil {
|
|
|
|
t.Errorf("error must be non-nil but was nil")
|
|
|
|
}
|
|
|
|
|
2021-10-02 12:58:48 +02:00
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
2020-09-02 18:15:37 +02:00
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
_, _ := 1, 1
|
2020-09-06 14:30:01 +02:00
|
|
|
return vec4(0)
|
2020-09-02 18:15:37 +02:00
|
|
|
}
|
|
|
|
`)); err == nil {
|
|
|
|
t.Errorf("error must be non-nil but was nil")
|
|
|
|
}
|
|
|
|
|
2021-10-02 12:58:48 +02:00
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
2020-09-02 18:15:37 +02:00
|
|
|
|
|
|
|
func Foo() (int, int) {
|
|
|
|
return 1, 1
|
|
|
|
}
|
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
_, _ := Foo()
|
2020-09-06 14:30:01 +02:00
|
|
|
return vec4(0)
|
2020-09-02 18:15:37 +02:00
|
|
|
}
|
|
|
|
`)); err == nil {
|
|
|
|
t.Errorf("error must be non-nil but was nil")
|
|
|
|
}
|
|
|
|
|
2021-10-02 12:58:48 +02:00
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
2020-09-02 18:15:37 +02:00
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
a, _ := 1, 1
|
|
|
|
_ = a
|
2020-09-06 14:30:01 +02:00
|
|
|
return vec4(0)
|
2020-09-02 18:15:37 +02:00
|
|
|
}
|
|
|
|
`)); err != nil {
|
2020-09-13 17:32:47 +02:00
|
|
|
t.Error(err)
|
2020-09-02 18:15:37 +02:00
|
|
|
}
|
|
|
|
|
2021-10-02 12:58:48 +02:00
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
2020-09-02 18:15:37 +02:00
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
_, a := 1, 1
|
|
|
|
_ = a
|
2020-09-06 14:30:01 +02:00
|
|
|
return vec4(0)
|
2020-09-02 18:15:37 +02:00
|
|
|
}
|
|
|
|
`)); err != nil {
|
2020-09-13 17:32:47 +02:00
|
|
|
t.Error(err)
|
2020-09-02 18:15:37 +02:00
|
|
|
}
|
|
|
|
}
|
2020-09-06 15:08:57 +02:00
|
|
|
|
|
|
|
func TestShaderWrongReturn(t *testing.T) {
|
2021-10-02 12:58:48 +02:00
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
2020-09-06 15:08:57 +02:00
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
2020-09-12 11:36:22 +02:00
|
|
|
return 0.0
|
2020-09-06 15:08:57 +02:00
|
|
|
}
|
2020-09-06 15:12:49 +02:00
|
|
|
`)); err == nil {
|
|
|
|
t.Errorf("error must be non-nil but was nil")
|
|
|
|
}
|
|
|
|
|
2021-10-02 12:58:48 +02:00
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
2020-09-06 15:12:49 +02:00
|
|
|
|
|
|
|
func Foo() (float, float) {
|
2020-09-12 11:36:22 +02:00
|
|
|
return 0
|
2020-09-06 15:12:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
2020-09-12 11:36:22 +02:00
|
|
|
return vec4(0)
|
2020-09-06 15:12:49 +02:00
|
|
|
}
|
2020-09-06 15:33:19 +02:00
|
|
|
`)); err == nil {
|
|
|
|
t.Errorf("error must be non-nil but was nil")
|
|
|
|
}
|
|
|
|
|
2021-10-02 12:58:48 +02:00
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
2020-09-06 15:33:19 +02:00
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
}
|
|
|
|
`)); err == nil {
|
|
|
|
t.Errorf("error must be non-nil but was nil")
|
|
|
|
}
|
|
|
|
|
2021-10-02 12:58:48 +02:00
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
2020-09-06 15:33:19 +02:00
|
|
|
|
|
|
|
func Foo() float {
|
|
|
|
}
|
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
return vec4(0)
|
|
|
|
}
|
2020-09-06 15:08:57 +02:00
|
|
|
`)); err == nil {
|
|
|
|
t.Errorf("error must be non-nil but was nil")
|
|
|
|
}
|
|
|
|
}
|
2020-09-12 11:54:36 +02:00
|
|
|
|
|
|
|
func TestShaderMultipleValueReturn(t *testing.T) {
|
2021-10-02 12:58:48 +02:00
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
2020-09-12 11:54:36 +02:00
|
|
|
|
|
|
|
func Foo() (float, float) {
|
|
|
|
return 0.0
|
|
|
|
}
|
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
return vec4(0)
|
|
|
|
}
|
|
|
|
`)); err == nil {
|
|
|
|
t.Errorf("error must be non-nil but was nil")
|
|
|
|
}
|
|
|
|
|
2021-10-02 12:58:48 +02:00
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
2020-09-12 11:54:36 +02:00
|
|
|
|
|
|
|
func Foo() float {
|
|
|
|
return 0.0, 0.0
|
|
|
|
}
|
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
return vec4(0)
|
|
|
|
}
|
|
|
|
`)); err == nil {
|
|
|
|
t.Errorf("error must be non-nil but was nil")
|
|
|
|
}
|
|
|
|
|
2021-10-02 12:58:48 +02:00
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
2020-09-12 11:54:36 +02:00
|
|
|
|
|
|
|
func Foo() (float, float, float) {
|
|
|
|
return 0.0, 0.0
|
|
|
|
}
|
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
return vec4(0)
|
|
|
|
}
|
|
|
|
`)); err == nil {
|
|
|
|
t.Errorf("error must be non-nil but was nil")
|
|
|
|
}
|
|
|
|
|
2021-10-02 12:58:48 +02:00
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
2020-09-12 11:54:36 +02:00
|
|
|
|
|
|
|
func Foo() (float, float) {
|
|
|
|
return 0.0, 0.0
|
|
|
|
}
|
|
|
|
|
|
|
|
func Foo2() (float, float, float) {
|
|
|
|
return Foo()
|
|
|
|
}
|
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
return vec4(0)
|
|
|
|
}
|
|
|
|
`)); err == nil {
|
|
|
|
t.Errorf("error must be non-nil but was nil")
|
|
|
|
}
|
|
|
|
|
2021-10-02 12:58:48 +02:00
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
2020-09-12 11:54:36 +02:00
|
|
|
|
|
|
|
func Foo() (float, float, float) {
|
|
|
|
return 0.0, 0.0, 0.0
|
|
|
|
}
|
|
|
|
|
|
|
|
func Foo2() (float, float, float) {
|
|
|
|
return Foo()
|
|
|
|
}
|
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
return vec4(0.0)
|
|
|
|
}
|
|
|
|
`)); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|
2020-09-12 12:50:20 +02:00
|
|
|
|
|
|
|
func TestShaderInit(t *testing.T) {
|
2021-10-02 12:58:48 +02:00
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
2020-09-12 12:50:20 +02:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
}
|
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
return vec4(0)
|
|
|
|
}
|
|
|
|
`)); err == nil {
|
|
|
|
t.Errorf("error must be non-nil but was nil")
|
|
|
|
}
|
|
|
|
}
|
2020-09-12 13:16:03 +02:00
|
|
|
|
|
|
|
func TestShaderUnspportedSyntax(t *testing.T) {
|
2021-10-02 12:58:48 +02:00
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
2020-09-12 13:16:03 +02:00
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
x := func() {
|
|
|
|
}
|
|
|
|
_ = x
|
|
|
|
return vec4(0)
|
|
|
|
}
|
|
|
|
`)); err == nil {
|
|
|
|
t.Errorf("error must be non-nil but was nil")
|
|
|
|
}
|
|
|
|
|
2021-10-02 12:58:48 +02:00
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
2020-09-12 13:16:03 +02:00
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
go func() {
|
|
|
|
}()
|
|
|
|
return vec4(0)
|
|
|
|
}
|
|
|
|
`)); err == nil {
|
|
|
|
t.Errorf("error must be non-nil but was nil")
|
|
|
|
}
|
|
|
|
|
2021-10-02 12:58:48 +02:00
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
2020-09-12 13:16:03 +02:00
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
ch := make(chan int)
|
|
|
|
_ = ch
|
|
|
|
return vec4(0)
|
|
|
|
}
|
|
|
|
`)); err == nil {
|
|
|
|
t.Errorf("error must be non-nil but was nil")
|
|
|
|
}
|
|
|
|
|
2021-10-02 12:58:48 +02:00
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
2020-09-12 13:16:03 +02:00
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
x := 1i
|
|
|
|
_ = x
|
|
|
|
return vec4(0)
|
|
|
|
}
|
2020-09-13 17:21:40 +02:00
|
|
|
`)); err == nil {
|
|
|
|
t.Errorf("error must be non-nil but was nil")
|
|
|
|
}
|
|
|
|
|
2021-10-02 12:58:48 +02:00
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
2020-09-13 17:21:40 +02:00
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
var x [4]float
|
|
|
|
y := x[1:2]
|
|
|
|
_ = y
|
|
|
|
return vec4(0)
|
|
|
|
}
|
|
|
|
`)); err == nil {
|
|
|
|
t.Errorf("error must be non-nil but was nil")
|
|
|
|
}
|
|
|
|
|
2021-10-02 12:58:48 +02:00
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
2020-09-13 17:21:40 +02:00
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
var x [4]float
|
|
|
|
y := x[1:2:3]
|
|
|
|
_ = y
|
|
|
|
return vec4(0)
|
|
|
|
}
|
2020-09-12 13:16:03 +02:00
|
|
|
`)); err == nil {
|
|
|
|
t.Errorf("error must be non-nil but was nil")
|
|
|
|
}
|
|
|
|
}
|
2020-09-12 21:29:28 +02:00
|
|
|
|
|
|
|
func TestShaderUninitializedUniformVariables(t *testing.T) {
|
|
|
|
const w, h = 16, 16
|
|
|
|
|
2021-10-02 12:58:48 +02:00
|
|
|
dst := ebiten.NewImage(w, h)
|
|
|
|
s, err := ebiten.NewShader([]byte(`package main
|
2020-09-12 21:29:28 +02:00
|
|
|
|
|
|
|
var U vec4
|
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
return U
|
|
|
|
}
|
|
|
|
`))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
dst.DrawRectShader(w, h, 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 got != want {
|
|
|
|
t.Errorf("dst.At(%d, %d): got: %v, want: %v", i, j, got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-12 21:59:04 +02:00
|
|
|
|
|
|
|
func TestShaderForbidAssigningSpecialVariables(t *testing.T) {
|
2021-10-02 12:58:48 +02:00
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
2020-09-12 21:59:04 +02:00
|
|
|
|
|
|
|
var U vec4
|
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
U = vec4(0)
|
|
|
|
return vec4(0)
|
|
|
|
}
|
|
|
|
`)); err == nil {
|
|
|
|
t.Errorf("error must be non-nil but was nil")
|
|
|
|
}
|
|
|
|
|
2021-10-02 12:58:48 +02:00
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
2020-09-12 21:59:04 +02:00
|
|
|
|
|
|
|
var U vec4
|
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
U.x = 0
|
|
|
|
return vec4(0)
|
|
|
|
}
|
|
|
|
`)); err == nil {
|
|
|
|
t.Errorf("error must be non-nil but was nil")
|
|
|
|
}
|
|
|
|
|
2021-10-02 12:58:48 +02:00
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
2020-09-12 21:59:04 +02:00
|
|
|
|
|
|
|
var U [2]vec4
|
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
U[0] = vec4(0)
|
|
|
|
return vec4(0)
|
|
|
|
}
|
|
|
|
`)); err == nil {
|
|
|
|
t.Errorf("error must be non-nil but was nil")
|
|
|
|
}
|
|
|
|
|
2021-10-02 12:58:48 +02:00
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
2020-09-12 21:59:04 +02:00
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
texCoord = vec2(0)
|
|
|
|
return vec4(0)
|
|
|
|
}
|
|
|
|
`)); err == nil {
|
|
|
|
t.Errorf("error must be non-nil but was nil")
|
|
|
|
}
|
|
|
|
|
2021-10-02 12:58:48 +02:00
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
2020-09-12 21:59:04 +02:00
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
texCoord.x = 0
|
|
|
|
return vec4(0)
|
|
|
|
}
|
|
|
|
`)); err == nil {
|
|
|
|
t.Errorf("error must be non-nil but was nil")
|
|
|
|
}
|
|
|
|
}
|
2020-09-13 13:38:51 +02:00
|
|
|
|
|
|
|
func TestShaderBoolLiteral(t *testing.T) {
|
2021-10-02 12:58:48 +02:00
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
2020-09-13 13:38:51 +02:00
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
true := vec4(0)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
`)); err != nil {
|
|
|
|
t.Errorf("error must be nil but was non-nil")
|
|
|
|
}
|
|
|
|
}
|
2020-09-13 13:13:30 +02:00
|
|
|
|
|
|
|
func TestShaderUnusedVariable(t *testing.T) {
|
2021-10-02 12:58:48 +02:00
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
2020-09-13 13:13:30 +02:00
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
x := 0
|
|
|
|
return vec4(0)
|
|
|
|
}
|
2020-09-13 15:47:00 +02:00
|
|
|
`)); err == nil {
|
|
|
|
t.Errorf("error must be non-nil but was nil")
|
|
|
|
}
|
|
|
|
|
2021-10-02 12:58:48 +02:00
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
2020-09-13 15:47:00 +02:00
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
x := 0
|
|
|
|
x = 1
|
|
|
|
return vec4(0)
|
|
|
|
}
|
|
|
|
`)); err == nil {
|
|
|
|
t.Errorf("error must be non-nil but was nil")
|
|
|
|
}
|
|
|
|
|
2021-10-02 12:58:48 +02:00
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
2020-09-13 15:47:00 +02:00
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
x := vec4(0)
|
|
|
|
x.x = 1
|
|
|
|
return vec4(0)
|
|
|
|
}
|
2020-09-13 20:41:25 +02:00
|
|
|
`)); err != nil {
|
|
|
|
t.Error(err)
|
2020-09-13 15:47:00 +02:00
|
|
|
}
|
|
|
|
|
2020-09-13 17:32:47 +02:00
|
|
|
// Increment statement treats a variable 'used'.
|
|
|
|
// https://play.golang.org/p/2RuYMrSLjt3
|
2021-10-02 12:58:48 +02:00
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
2020-09-13 15:47:00 +02:00
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
x := 0
|
|
|
|
x++
|
|
|
|
return vec4(0)
|
|
|
|
}
|
2020-09-13 17:32:47 +02:00
|
|
|
`)); err != nil {
|
|
|
|
t.Error(err)
|
2020-09-13 13:13:30 +02:00
|
|
|
}
|
2020-09-17 10:29:31 +02:00
|
|
|
|
2021-10-02 12:58:48 +02:00
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
2020-09-17 10:29:31 +02:00
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
var a int
|
|
|
|
return vec4(0)
|
|
|
|
}
|
|
|
|
`)); err == nil {
|
|
|
|
t.Errorf("error must be non-nil but was nil")
|
|
|
|
}
|
|
|
|
|
2021-10-02 12:58:48 +02:00
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
2020-09-17 10:29:31 +02:00
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
var a, b int
|
|
|
|
return vec4(0)
|
|
|
|
}
|
|
|
|
`)); err == nil {
|
|
|
|
t.Errorf("error must be non-nil but was nil")
|
|
|
|
}
|
2020-09-13 13:13:30 +02:00
|
|
|
}
|
2020-09-13 16:48:12 +02:00
|
|
|
|
2020-09-13 20:41:25 +02:00
|
|
|
func TestShaderBlankLhs(t *testing.T) {
|
2021-10-02 12:58:48 +02:00
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
2020-09-13 16:48:12 +02:00
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
x := _
|
|
|
|
_ = x
|
|
|
|
return vec4(0)
|
|
|
|
}
|
|
|
|
`)); err == nil {
|
|
|
|
t.Errorf("error must be non-nil but was nil")
|
|
|
|
}
|
|
|
|
|
2021-10-02 12:58:48 +02:00
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
2020-09-13 16:48:12 +02:00
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
var x int = _
|
|
|
|
_ = x
|
|
|
|
return vec4(0)
|
|
|
|
}
|
|
|
|
`)); err == nil {
|
|
|
|
t.Errorf("error must be non-nil but was nil")
|
|
|
|
}
|
|
|
|
|
2021-10-02 12:58:48 +02:00
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
2020-09-13 16:48:12 +02:00
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
x := 1
|
|
|
|
x = _
|
|
|
|
_ = x
|
|
|
|
return vec4(0)
|
|
|
|
}
|
|
|
|
`)); err == nil {
|
|
|
|
t.Errorf("error must be non-nil but was nil")
|
|
|
|
}
|
|
|
|
|
2021-10-02 12:58:48 +02:00
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
2020-09-13 16:48:12 +02:00
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
x := 1 + _
|
|
|
|
_ = x
|
|
|
|
return vec4(0)
|
|
|
|
}
|
2020-09-13 17:32:47 +02:00
|
|
|
`)); err == nil {
|
|
|
|
t.Errorf("error must be non-nil but was nil")
|
|
|
|
}
|
|
|
|
|
2021-10-02 12:58:48 +02:00
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
2020-09-13 17:32:47 +02:00
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
_++
|
|
|
|
return vec4(0)
|
|
|
|
}
|
2020-09-13 17:51:27 +02:00
|
|
|
`)); err == nil {
|
|
|
|
t.Errorf("error must be non-nil but was nil")
|
|
|
|
}
|
|
|
|
|
2021-10-02 12:58:48 +02:00
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
2020-09-13 17:51:27 +02:00
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
_ += 1
|
|
|
|
return vec4(0)
|
|
|
|
}
|
2020-09-13 20:41:25 +02:00
|
|
|
`)); err == nil {
|
|
|
|
t.Errorf("error must be non-nil but was nil")
|
|
|
|
}
|
|
|
|
|
2021-10-02 12:58:48 +02:00
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
2020-09-13 20:41:25 +02:00
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
_.x = 1
|
|
|
|
return vec4(0)
|
|
|
|
}
|
2020-09-13 16:48:12 +02:00
|
|
|
`)); err == nil {
|
|
|
|
t.Errorf("error must be non-nil but was nil")
|
|
|
|
}
|
|
|
|
}
|
2020-09-17 10:15:16 +02:00
|
|
|
|
2021-04-08 17:12:14 +02:00
|
|
|
func TestShaderDuplicatedVarsAndConstants(t *testing.T) {
|
2021-10-02 12:58:48 +02:00
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
2021-04-08 17:12:14 +02:00
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
var a = 0
|
|
|
|
const a = 0
|
|
|
|
return vec4(a)
|
|
|
|
}
|
|
|
|
`)); err == nil {
|
|
|
|
t.Errorf("error must be non-nil but was nil")
|
|
|
|
}
|
|
|
|
|
2021-10-02 12:58:48 +02:00
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
2021-04-08 17:12:14 +02:00
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
const a = 0
|
|
|
|
var a = 0
|
|
|
|
return vec4(a)
|
|
|
|
}
|
|
|
|
`)); err == nil {
|
|
|
|
t.Errorf("error must be non-nil but was nil")
|
|
|
|
}
|
|
|
|
|
2021-10-02 12:58:48 +02:00
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
2021-04-08 17:12:14 +02:00
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
const a = 0
|
|
|
|
const a = 0
|
|
|
|
return vec4(a)
|
|
|
|
}
|
|
|
|
`)); err == nil {
|
|
|
|
t.Errorf("error must be non-nil but was nil")
|
|
|
|
}
|
|
|
|
|
2021-10-02 12:58:48 +02:00
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
2021-04-08 17:12:14 +02:00
|
|
|
|
|
|
|
const U0 = 0
|
|
|
|
var U0 float
|
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
return vec4(a)
|
|
|
|
}
|
|
|
|
`)); err == nil {
|
|
|
|
t.Errorf("error must be non-nil but was nil")
|
|
|
|
}
|
|
|
|
|
2021-10-02 12:58:48 +02:00
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
2021-04-08 17:12:14 +02:00
|
|
|
|
|
|
|
var U0 float
|
|
|
|
const U0 = 0
|
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
return vec4(a)
|
|
|
|
}
|
|
|
|
`)); err == nil {
|
|
|
|
t.Errorf("error must be non-nil but was nil")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-17 10:15:16 +02:00
|
|
|
func TestShaderMatrix(t *testing.T) {
|
|
|
|
const w, h = 16, 16
|
|
|
|
|
2021-10-02 12:58:48 +02:00
|
|
|
dst := ebiten.NewImage(w, h)
|
|
|
|
s, err := ebiten.NewShader([]byte(`package main
|
2020-09-17 10:15:16 +02:00
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
var a, b mat4
|
|
|
|
a[0] = vec4(0.125, 0.0625, 0.0625, 0.0625)
|
|
|
|
a[1] = vec4(0.25, 0.25, 0.0625, 0.1875)
|
|
|
|
a[2] = vec4(0.1875, 0.125, 0.25, 0.25)
|
|
|
|
a[3] = vec4(0.0625, 0.1875, 0.125, 0.25)
|
|
|
|
b[0] = vec4(0.0625, 0.125, 0.0625, 0.125)
|
|
|
|
b[1] = vec4(0.125, 0.1875, 0.25, 0.0625)
|
|
|
|
b[2] = vec4(0.125, 0.125, 0.1875, 0.1875)
|
|
|
|
b[3] = vec4(0.25, 0.0625, 0.125, 0.0625)
|
|
|
|
return vec4((a * b * vec4(1, 1, 1, 1)).xyz, 1)
|
|
|
|
}
|
|
|
|
`))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-10-02 12:58:48 +02:00
|
|
|
src := ebiten.NewImage(w, h)
|
|
|
|
op := &ebiten.DrawRectShaderOptions{}
|
2020-09-20 11:13:57 +02:00
|
|
|
op.Images[0] = src
|
|
|
|
dst.DrawRectShader(w, h, s, op)
|
2020-09-17 10:15:16 +02:00
|
|
|
|
|
|
|
for j := 0; j < h; j++ {
|
|
|
|
for i := 0; i < w; i++ {
|
|
|
|
got := dst.At(i, j).(color.RGBA)
|
|
|
|
want := color.RGBA{87, 82, 71, 255}
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("dst.At(%d, %d): got: %v, want: %v", i, j, got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-19 21:21:38 +02:00
|
|
|
|
|
|
|
func TestShaderSubImage(t *testing.T) {
|
|
|
|
const w, h = 16, 16
|
|
|
|
|
2021-10-02 12:58:48 +02:00
|
|
|
s, err := ebiten.NewShader([]byte(`package main
|
2020-09-19 21:21:38 +02:00
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
r := imageSrc0At(texCoord).r
|
|
|
|
g := imageSrc1At(texCoord).g
|
|
|
|
return vec4(r, g, 0, 1)
|
|
|
|
}
|
|
|
|
`))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-10-02 12:58:48 +02:00
|
|
|
src0 := ebiten.NewImage(w, h)
|
2020-09-19 21:21:38 +02:00
|
|
|
pix0 := make([]byte, 4*w*h)
|
|
|
|
for j := 0; j < h; j++ {
|
|
|
|
for i := 0; i < w; i++ {
|
2020-09-20 18:52:21 +02:00
|
|
|
if 2 <= i && i < 10 && 3 <= j && j < 11 {
|
2020-09-19 21:21:38 +02:00
|
|
|
pix0[4*(j*w+i)] = 0xff
|
|
|
|
pix0[4*(j*w+i)+1] = 0
|
|
|
|
pix0[4*(j*w+i)+2] = 0
|
|
|
|
pix0[4*(j*w+i)+3] = 0xff
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
src0.ReplacePixels(pix0)
|
2021-10-02 12:58:48 +02:00
|
|
|
src0 = src0.SubImage(image.Rect(2, 3, 10, 11)).(*ebiten.Image)
|
2020-09-19 21:21:38 +02:00
|
|
|
|
2021-10-02 12:58:48 +02:00
|
|
|
src1 := ebiten.NewImage(w, h)
|
2020-09-19 21:21:38 +02:00
|
|
|
pix1 := make([]byte, 4*w*h)
|
|
|
|
for j := 0; j < h; j++ {
|
|
|
|
for i := 0; i < w; i++ {
|
2020-09-20 18:52:21 +02:00
|
|
|
if 6 <= i && i < 14 && 8 <= j && j < 16 {
|
2020-09-19 21:21:38 +02:00
|
|
|
pix1[4*(j*w+i)] = 0
|
|
|
|
pix1[4*(j*w+i)+1] = 0xff
|
|
|
|
pix1[4*(j*w+i)+2] = 0
|
|
|
|
pix1[4*(j*w+i)+3] = 0xff
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
src1.ReplacePixels(pix1)
|
2021-10-02 12:58:48 +02:00
|
|
|
src1 = src1.SubImage(image.Rect(6, 8, 14, 16)).(*ebiten.Image)
|
2020-09-19 21:21:38 +02:00
|
|
|
|
2021-10-02 12:58:48 +02:00
|
|
|
testPixels := func(testname string, dst *ebiten.Image) {
|
2020-09-19 21:21:38 +02:00
|
|
|
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{0xff, 0xff, 0, 0xff}
|
|
|
|
}
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("%s dst.At(%d, %d): got: %v, want: %v", testname, i, j, got, want)
|
|
|
|
}
|
2020-09-19 21:21:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-09-19 21:21:38 +02:00
|
|
|
|
|
|
|
t.Run("DrawRectShader", func(t *testing.T) {
|
2021-10-02 12:58:48 +02:00
|
|
|
dst := ebiten.NewImage(w, h)
|
|
|
|
op := &ebiten.DrawRectShaderOptions{}
|
2020-09-19 21:21:38 +02:00
|
|
|
op.Images[0] = src0
|
|
|
|
op.Images[1] = src1
|
|
|
|
dst.DrawRectShader(w/2, h/2, s, op)
|
|
|
|
testPixels("DrawRectShader", dst)
|
|
|
|
})
|
|
|
|
|
2020-09-20 11:32:18 +02:00
|
|
|
t.Run("DrawTrianglesShader", func(t *testing.T) {
|
2021-10-02 12:58:48 +02:00
|
|
|
dst := ebiten.NewImage(w, h)
|
|
|
|
vs := []ebiten.Vertex{
|
2020-09-19 21:21:38 +02:00
|
|
|
{
|
|
|
|
DstX: 0,
|
|
|
|
DstY: 0,
|
|
|
|
SrcX: 2,
|
2020-09-20 18:52:21 +02:00
|
|
|
SrcY: 3,
|
2020-09-19 21:21:38 +02:00
|
|
|
ColorR: 1,
|
|
|
|
ColorG: 1,
|
|
|
|
ColorB: 1,
|
|
|
|
ColorA: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
DstX: w / 2,
|
|
|
|
DstY: 0,
|
|
|
|
SrcX: 10,
|
2020-09-20 18:52:21 +02:00
|
|
|
SrcY: 3,
|
2020-09-19 21:21:38 +02:00
|
|
|
ColorR: 1,
|
|
|
|
ColorG: 1,
|
|
|
|
ColorB: 1,
|
|
|
|
ColorA: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
DstX: 0,
|
|
|
|
DstY: h / 2,
|
|
|
|
SrcX: 2,
|
2020-09-20 18:52:21 +02:00
|
|
|
SrcY: 11,
|
2020-09-19 21:21:38 +02:00
|
|
|
ColorR: 1,
|
|
|
|
ColorG: 1,
|
|
|
|
ColorB: 1,
|
|
|
|
ColorA: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
DstX: w / 2,
|
|
|
|
DstY: h / 2,
|
|
|
|
SrcX: 10,
|
2020-09-20 18:52:21 +02:00
|
|
|
SrcY: 11,
|
2020-09-19 21:21:38 +02:00
|
|
|
ColorR: 1,
|
|
|
|
ColorG: 1,
|
|
|
|
ColorB: 1,
|
|
|
|
ColorA: 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
is := []uint16{0, 1, 2, 1, 2, 3}
|
|
|
|
|
2021-10-02 12:58:48 +02:00
|
|
|
op := &ebiten.DrawTrianglesShaderOptions{}
|
2020-09-20 11:32:18 +02:00
|
|
|
op.Images[0] = src0
|
|
|
|
op.Images[1] = src1
|
|
|
|
dst.DrawTrianglesShader(vs, is, s, op)
|
|
|
|
testPixels("DrawTrianglesShader", dst)
|
2020-09-19 21:21:38 +02:00
|
|
|
})
|
2020-09-19 21:21:38 +02:00
|
|
|
}
|
2020-10-26 02:33:11 +01:00
|
|
|
|
|
|
|
// Issue #1404
|
|
|
|
func TestShaderDerivatives(t *testing.T) {
|
|
|
|
const w, h = 16, 16
|
|
|
|
|
2021-10-02 12:58:48 +02:00
|
|
|
s, err := ebiten.NewShader([]byte(`package main
|
2020-10-26 02:33:11 +01:00
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
p := imageSrc0At(texCoord)
|
|
|
|
return vec4(abs(dfdx(p.r)), abs(dfdy(p.g)), 0, 1)
|
|
|
|
}
|
|
|
|
`))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-10-02 12:58:48 +02:00
|
|
|
dst := ebiten.NewImage(w, h)
|
|
|
|
src := ebiten.NewImage(w, h)
|
2020-10-26 02:33:11 +01:00
|
|
|
pix := make([]byte, 4*w*h)
|
|
|
|
for j := 0; j < h; j++ {
|
|
|
|
for i := 0; i < w; i++ {
|
|
|
|
if i < w/2 {
|
|
|
|
pix[4*(j*w+i)] = 0xff
|
|
|
|
}
|
|
|
|
if j < h/2 {
|
|
|
|
pix[4*(j*w+i)+1] = 0xff
|
|
|
|
}
|
|
|
|
pix[4*(j*w+i)+3] = 0xff
|
|
|
|
}
|
|
|
|
}
|
|
|
|
src.ReplacePixels(pix)
|
|
|
|
|
2021-10-02 12:58:48 +02:00
|
|
|
op := &ebiten.DrawRectShaderOptions{}
|
2020-10-26 02:33:11 +01:00
|
|
|
op.Images[0] = src
|
|
|
|
dst.DrawRectShader(w, h, s, op)
|
|
|
|
|
|
|
|
// The results of the edges might be unreliable. Skip the edges.
|
|
|
|
for j := 1; j < h-1; j++ {
|
|
|
|
for i := 1; i < w-1; i++ {
|
|
|
|
got := dst.At(i, j).(color.RGBA)
|
|
|
|
want := color.RGBA{0, 0, 0, 0xff}
|
|
|
|
if i == w/2-1 || i == w/2 {
|
|
|
|
want.R = 0xff
|
|
|
|
}
|
|
|
|
if j == h/2-1 || j == h/2 {
|
|
|
|
want.G = 0xff
|
|
|
|
}
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("dst.At(%d, %d): got: %v, want: %v", i, j, got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-07-09 11:11:56 +02:00
|
|
|
|
|
|
|
// Issue #1701
|
|
|
|
func TestShaderDerivatives2(t *testing.T) {
|
|
|
|
const w, h = 16, 16
|
|
|
|
|
2021-10-02 12:58:48 +02:00
|
|
|
s, err := ebiten.NewShader([]byte(`package main
|
2021-07-09 11:11:56 +02:00
|
|
|
|
|
|
|
// This function uses dfdx and then should not be in GLSL's vertex shader (#1701).
|
|
|
|
func Foo(p vec4) vec4 {
|
|
|
|
return vec4(abs(dfdx(p.r)), abs(dfdy(p.g)), 0, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
p := imageSrc0At(texCoord)
|
|
|
|
return Foo(p)
|
|
|
|
}
|
|
|
|
`))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-10-02 12:58:48 +02:00
|
|
|
dst := ebiten.NewImage(w, h)
|
|
|
|
src := ebiten.NewImage(w, h)
|
2021-07-09 11:11:56 +02:00
|
|
|
pix := make([]byte, 4*w*h)
|
|
|
|
for j := 0; j < h; j++ {
|
|
|
|
for i := 0; i < w; i++ {
|
|
|
|
if i < w/2 {
|
|
|
|
pix[4*(j*w+i)] = 0xff
|
|
|
|
}
|
|
|
|
if j < h/2 {
|
|
|
|
pix[4*(j*w+i)+1] = 0xff
|
|
|
|
}
|
|
|
|
pix[4*(j*w+i)+3] = 0xff
|
|
|
|
}
|
|
|
|
}
|
|
|
|
src.ReplacePixels(pix)
|
|
|
|
|
2021-10-02 12:58:48 +02:00
|
|
|
op := &ebiten.DrawRectShaderOptions{}
|
2021-07-09 11:11:56 +02:00
|
|
|
op.Images[0] = src
|
|
|
|
dst.DrawRectShader(w, h, s, op)
|
|
|
|
|
|
|
|
// The results of the edges might be unreliable. Skip the edges.
|
|
|
|
for j := 1; j < h-1; j++ {
|
|
|
|
for i := 1; i < w-1; i++ {
|
|
|
|
got := dst.At(i, j).(color.RGBA)
|
|
|
|
want := color.RGBA{0, 0, 0, 0xff}
|
|
|
|
if i == w/2-1 || i == w/2 {
|
|
|
|
want.R = 0xff
|
|
|
|
}
|
|
|
|
if j == h/2-1 || j == h/2 {
|
|
|
|
want.G = 0xff
|
|
|
|
}
|
|
|
|
if got != want {
|
|
|
|
t.Errorf("dst.At(%d, %d): got: %v, want: %v", i, j, got, want)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-08-15 08:46:24 +02:00
|
|
|
|
|
|
|
// Issue #1754
|
|
|
|
func TestShaderUniformFirstElement(t *testing.T) {
|
|
|
|
shaders := []struct {
|
|
|
|
Name string
|
|
|
|
Shader string
|
|
|
|
Uniforms map[string]interface{}
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
Name: "float array",
|
|
|
|
Shader: `package main
|
|
|
|
|
|
|
|
var C [2]float
|
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
return vec4(C[0], 1, 1, 1)
|
|
|
|
}`,
|
|
|
|
Uniforms: map[string]interface{}{
|
|
|
|
"C": []float32{1, 1},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "float one-element array",
|
|
|
|
Shader: `package main
|
|
|
|
|
|
|
|
var C [1]float
|
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
return vec4(C[0], 1, 1, 1)
|
|
|
|
}`,
|
|
|
|
Uniforms: map[string]interface{}{
|
|
|
|
"C": []float32{1},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: "matrix array",
|
|
|
|
Shader: `package main
|
|
|
|
|
|
|
|
var C [2]mat2
|
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
return vec4(C[0][0][0], 1, 1, 1)
|
|
|
|
}`,
|
|
|
|
Uniforms: map[string]interface{}{
|
|
|
|
"C": []float32{1, 0, 0, 0, 0, 0, 0, 0},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, shader := range shaders {
|
|
|
|
shader := shader
|
|
|
|
t.Run(shader.Name, func(t *testing.T) {
|
|
|
|
const w, h = 1, 1
|
|
|
|
|
2021-10-02 12:58:48 +02:00
|
|
|
dst := ebiten.NewImage(w, h)
|
|
|
|
s, err := ebiten.NewShader([]byte(shader.Shader))
|
2021-08-15 08:46:24 +02:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2021-10-02 12:58:48 +02:00
|
|
|
op := &ebiten.DrawRectShaderOptions{}
|
2021-08-15 08:46:24 +02:00
|
|
|
op.Uniforms = shader.Uniforms
|
|
|
|
dst.DrawRectShader(w, h, s, op)
|
|
|
|
if got, want := dst.At(0, 0), (color.RGBA{0xff, 0xff, 0xff, 0xff}); got != want {
|
|
|
|
t.Errorf("got: %v, want: %v", got, want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2021-11-14 09:34:06 +01:00
|
|
|
|
|
|
|
func TestShaderUnmatchedArgs(t *testing.T) {
|
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
|
|
|
|
|
|
|
func Foo() {
|
|
|
|
}
|
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
Foo(1)
|
|
|
|
return position
|
|
|
|
}
|
2021-11-14 10:50:20 +01:00
|
|
|
`)); err == nil {
|
|
|
|
t.Errorf("error must be non-nil but was nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
|
|
|
|
|
|
|
func Foo(x float) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
Foo()
|
|
|
|
return position
|
|
|
|
}
|
|
|
|
`)); err == nil {
|
|
|
|
t.Errorf("error must be non-nil but was nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
|
|
|
|
|
|
|
func Foo(x, y float) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func Bar() (float, float, float) {
|
|
|
|
return 0, 1
|
|
|
|
}
|
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
Foo(Bar())
|
|
|
|
return position
|
|
|
|
}
|
|
|
|
`)); err == nil {
|
|
|
|
t.Errorf("error must be non-nil but was nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
|
|
|
|
|
|
|
func Foo(x, y, z float) {
|
|
|
|
}
|
|
|
|
|
|
|
|
func Bar() (float, float) {
|
|
|
|
return 0, 1
|
|
|
|
}
|
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
Foo(Bar())
|
|
|
|
return position
|
|
|
|
}
|
2021-11-14 09:34:06 +01:00
|
|
|
`)); err == nil {
|
|
|
|
t.Errorf("error must be non-nil but was nil")
|
|
|
|
}
|
|
|
|
}
|
2021-12-27 14:05:55 +01:00
|
|
|
|
|
|
|
// Issue #1898
|
|
|
|
func TestShaderMeaninglessSentence(t *testing.T) {
|
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
|
|
|
|
|
|
|
var Time float
|
|
|
|
var ScreenSize vec2
|
|
|
|
|
2021-12-27 15:14:05 +01:00
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
position
|
|
|
|
return position
|
|
|
|
}`)); err == nil {
|
|
|
|
t.Errorf("error must be non-nil but was nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
|
|
|
|
|
|
|
var Time float
|
|
|
|
var ScreenSize vec2
|
|
|
|
|
2021-12-27 14:05:55 +01:00
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
vec2(position)
|
|
|
|
return position
|
|
|
|
}`)); err == nil {
|
|
|
|
t.Errorf("error must be non-nil but was nil")
|
|
|
|
}
|
|
|
|
}
|
2022-01-11 15:33:40 +01:00
|
|
|
|
|
|
|
// Issue #1947
|
|
|
|
func TestShaderOperatorMod(t *testing.T) {
|
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
a := 2.0 % 0.5
|
|
|
|
return vec4(a)
|
|
|
|
}`)); err == nil {
|
|
|
|
t.Errorf("error must be non-nil but was nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
|
|
|
|
2022-01-13 19:50:30 +01:00
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
a := int(2) % 0.5
|
|
|
|
return vec4(a)
|
|
|
|
}`)); err == nil {
|
|
|
|
t.Errorf("error must be non-nil but was nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
a := int(2) % 1.0
|
|
|
|
return vec4(a)
|
|
|
|
}`)); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
|
|
|
|
2022-01-11 15:33:40 +01:00
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
a := 2.0
|
|
|
|
b := 0.5
|
|
|
|
return vec4(a % b)
|
|
|
|
}`)); err == nil {
|
|
|
|
t.Errorf("error must be non-nil but was nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
a := 2
|
|
|
|
b := 0.5
|
|
|
|
return vec4(a % b)
|
|
|
|
}`)); err == nil {
|
|
|
|
t.Errorf("error must be non-nil but was nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
a := 2.5
|
|
|
|
b := 1
|
|
|
|
return vec4(a % b)
|
|
|
|
}`)); err == nil {
|
|
|
|
t.Errorf("error must be non-nil but was nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
a := 2
|
|
|
|
b := 1
|
|
|
|
return vec4(a % b)
|
|
|
|
}`)); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
a := 2
|
|
|
|
return vec4(a % 1)
|
|
|
|
}`)); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
a := 1
|
|
|
|
return vec4(2 % a)
|
|
|
|
}`)); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
a := 2
|
|
|
|
a %= 1
|
|
|
|
return vec4(a)
|
|
|
|
}`)); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
|
|
|
|
2022-01-11 16:23:55 +01:00
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
a := 2
|
|
|
|
a %= 1.0
|
|
|
|
return vec4(a)
|
|
|
|
}`)); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
|
|
|
|
2022-01-11 15:33:40 +01:00
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
a := 2
|
|
|
|
a %= 0.5
|
|
|
|
return vec4(a)
|
|
|
|
}`)); err == nil {
|
|
|
|
t.Errorf("error must be non-nil but was nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
a := 2.0
|
|
|
|
a %= 1
|
|
|
|
return vec4(a)
|
|
|
|
}`)); err == nil {
|
|
|
|
t.Errorf("error must be non-nil but was nil")
|
|
|
|
}
|
|
|
|
}
|
2022-01-11 16:23:55 +01:00
|
|
|
|
|
|
|
func TestShaderOperatorAssign(t *testing.T) {
|
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
a := 1.0
|
|
|
|
a += 2
|
|
|
|
return vec4(a)
|
|
|
|
}`)); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
a := 1.0
|
|
|
|
a += 2.0
|
|
|
|
return vec4(a)
|
|
|
|
}`)); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
a := 1.0
|
|
|
|
a += 2.1
|
|
|
|
return vec4(a)
|
|
|
|
}`)); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
a := 1
|
|
|
|
a += 2
|
|
|
|
return vec4(a)
|
|
|
|
}`)); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
a := 1
|
|
|
|
a += 2.0
|
|
|
|
return vec4(a)
|
|
|
|
}`)); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
a := 1
|
|
|
|
a += 2.1
|
|
|
|
return vec4(a)
|
|
|
|
}`)); err == nil {
|
|
|
|
t.Errorf("error must be non-nil but was nil")
|
|
|
|
}
|
|
|
|
}
|
2022-01-17 12:52:41 +01:00
|
|
|
|
|
|
|
// Issue #1963
|
|
|
|
func TestShaderOperatorVecAndNumber(t *testing.T) {
|
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
|
|
|
|
2022-01-17 12:53:58 +01:00
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
a := vec2(1) + 2
|
|
|
|
return a.xxyy
|
|
|
|
}`)); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
a := vec2(1) + 2.1
|
|
|
|
return a.xxyy
|
|
|
|
}`)); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
a := vec2(1) % 2
|
|
|
|
return a.xxyy
|
|
|
|
}`)); err == nil {
|
|
|
|
t.Errorf("error must be non-nil but was nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
a := vec2(1) % 2.1
|
|
|
|
return a.xxyy
|
|
|
|
}`)); err == nil {
|
|
|
|
t.Errorf("error must be non-nil but was nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
|
|
|
|
2022-01-17 12:52:41 +01:00
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
a := vec2(1)
|
|
|
|
a += 2
|
|
|
|
return a.xxyy
|
|
|
|
}`)); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
a := vec2(1)
|
|
|
|
a += 2.1
|
|
|
|
return a.xxyy
|
|
|
|
}`)); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
a := vec2(1)
|
|
|
|
a %= 2
|
|
|
|
return a.xxyy
|
|
|
|
}`)); err == nil {
|
|
|
|
t.Errorf("error must be non-nil but was nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := ebiten.NewShader([]byte(`package main
|
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
a := vec2(1)
|
|
|
|
a %= 2.1
|
|
|
|
return a.xxyy
|
|
|
|
}`)); err == nil {
|
|
|
|
t.Errorf("error must be non-nil but was nil")
|
|
|
|
}
|
|
|
|
}
|
2022-01-20 16:50:21 +01:00
|
|
|
|
|
|
|
// Issue #1971
|
|
|
|
func TestShaderOperatorMultiply(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
stmt string
|
|
|
|
err bool
|
|
|
|
}{
|
|
|
|
{stmt: "a := 1 * vec2(2); _ = a", err: false},
|
|
|
|
{stmt: "a := int(1) * vec2(2); _ = a", err: true},
|
|
|
|
{stmt: "a := 1.0 * vec2(2); _ = a", err: false},
|
|
|
|
{stmt: "a := 1 + vec2(2); _ = a", err: false},
|
|
|
|
{stmt: "a := int(1) + vec2(2); _ = a", err: true},
|
|
|
|
{stmt: "a := 1.0 + vec2(2); _ = a", err: false},
|
|
|
|
{stmt: "a := 1 * vec3(2); _ = a", err: false},
|
|
|
|
{stmt: "a := 1.0 * vec3(2); _ = a", err: false},
|
|
|
|
{stmt: "a := 1 * vec4(2); _ = a", err: false},
|
|
|
|
{stmt: "a := 1.0 * vec4(2); _ = a", err: false},
|
|
|
|
{stmt: "a := 1 * mat2(2); _ = a", err: false},
|
|
|
|
{stmt: "a := 1.0 * mat2(2); _ = a", err: false},
|
|
|
|
{stmt: "a := 1 * mat3(2); _ = a", err: false},
|
|
|
|
{stmt: "a := 1.0 * mat3(2); _ = a", err: false},
|
|
|
|
{stmt: "a := 1 * mat4(2); _ = a", err: false},
|
|
|
|
{stmt: "a := 1.0 * mat4(2); _ = a", err: false},
|
|
|
|
{stmt: "a := vec2(1) * 2; _ = a", err: false},
|
|
|
|
{stmt: "a := vec2(1) * 2.0; _ = a", err: false},
|
|
|
|
{stmt: "a := vec2(1) * int(2); _ = a", err: true},
|
|
|
|
{stmt: "a := vec2(1) * vec2(2); _ = a", err: false},
|
|
|
|
{stmt: "a := vec2(1) + vec2(2); _ = a", err: false},
|
|
|
|
{stmt: "a := vec2(1) * vec3(2); _ = a", err: true},
|
|
|
|
{stmt: "a := vec2(1) * vec4(2); _ = a", err: true},
|
|
|
|
{stmt: "a := vec2(1) * mat2(2); _ = a", err: false},
|
2022-01-20 17:47:21 +01:00
|
|
|
{stmt: "a := vec2(1) + mat2(2); _ = a", err: true},
|
2022-01-20 16:50:21 +01:00
|
|
|
{stmt: "a := vec2(1) * mat3(2); _ = a", err: true},
|
|
|
|
{stmt: "a := vec2(1) * mat4(2); _ = a", err: true},
|
|
|
|
{stmt: "a := mat2(1) * 2; _ = a", err: false},
|
|
|
|
{stmt: "a := mat2(1) * 2.0; _ = a", err: false},
|
|
|
|
{stmt: "a := mat2(1) * int(2); _ = a", err: true},
|
|
|
|
{stmt: "a := mat2(1) + 2.0; _ = a", err: false},
|
|
|
|
{stmt: "a := mat2(1) * vec2(2); _ = a", err: false},
|
|
|
|
{stmt: "a := mat2(1) + vec2(2); _ = a", err: true},
|
|
|
|
{stmt: "a := mat2(1) * vec3(2); _ = a", err: true},
|
|
|
|
{stmt: "a := mat2(1) * vec4(2); _ = a", err: true},
|
|
|
|
{stmt: "a := mat2(1) * mat2(2); _ = a", err: false},
|
|
|
|
{stmt: "a := mat2(1) * mat3(2); _ = a", err: true},
|
|
|
|
{stmt: "a := mat2(1) * mat4(2); _ = a", err: true},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
_, err := ebiten.NewShader([]byte(fmt.Sprintf(`package main
|
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
%s
|
|
|
|
return position
|
|
|
|
}`, c.stmt)))
|
|
|
|
if err == nil && c.err {
|
|
|
|
t.Errorf("%s must return an error but does not", c.stmt)
|
|
|
|
} else if err != nil && !c.err {
|
|
|
|
t.Errorf("%s must not return nil but returned %v", c.stmt, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-01-20 17:59:54 +01:00
|
|
|
|
|
|
|
// Issue #1971
|
|
|
|
func TestShaderOperatorMultiplyAssign(t *testing.T) {
|
|
|
|
cases := []struct {
|
|
|
|
stmt string
|
|
|
|
err bool
|
|
|
|
}{
|
|
|
|
{stmt: "a := 1.0; a *= 2", err: false},
|
|
|
|
{stmt: "a := 1.0; a *= 2.0", err: false},
|
|
|
|
{stmt: "a := 1.0; a *= int(2)", err: true},
|
|
|
|
{stmt: "a := 1.0; a *= vec2(2)", err: true},
|
|
|
|
{stmt: "a := 1.0; a *= vec3(2)", err: true},
|
|
|
|
{stmt: "a := 1.0; a *= vec4(2)", err: true},
|
|
|
|
{stmt: "a := 1.0; a *= mat2(2)", err: true},
|
|
|
|
{stmt: "a := 1.0; a *= mat3(2)", err: true},
|
|
|
|
{stmt: "a := 1.0; a *= mat4(2)", err: true},
|
|
|
|
{stmt: "a := vec2(1); a *= 2", err: false},
|
|
|
|
{stmt: "a := vec2(1); a *= 2.0", err: false},
|
|
|
|
{stmt: "a := vec2(1); a *= int(2)", err: true},
|
|
|
|
{stmt: "a := vec2(1); a *= vec2(2)", err: false},
|
|
|
|
{stmt: "a := vec2(1); a += vec2(2)", err: false},
|
|
|
|
{stmt: "a := vec2(1); a *= vec3(2)", err: true},
|
|
|
|
{stmt: "a := vec2(1); a *= vec4(2)", err: true},
|
|
|
|
{stmt: "a := vec2(1); a *= mat2(2)", err: false},
|
|
|
|
{stmt: "a := vec2(1); a += mat2(2)", err: true},
|
|
|
|
{stmt: "a := vec2(1); a *= mat3(2)", err: true},
|
|
|
|
{stmt: "a := vec2(1); a *= mat4(2)", err: true},
|
|
|
|
{stmt: "a := mat2(1); a *= 2", err: false},
|
|
|
|
{stmt: "a := mat2(1); a *= 2.0", err: false},
|
|
|
|
{stmt: "a := mat2(1); a *= int(2)", err: true},
|
|
|
|
{stmt: "a := mat2(1); a *= vec2(2)", err: false},
|
|
|
|
{stmt: "a := mat2(1); a += vec2(2)", err: true},
|
|
|
|
{stmt: "a := mat2(1); a *= vec3(2)", err: true},
|
|
|
|
{stmt: "a := mat2(1); a *= vec4(2)", err: true},
|
|
|
|
{stmt: "a := mat2(1); a *= mat2(2)", err: false},
|
|
|
|
{stmt: "a := mat2(1); a += mat2(2)", err: false},
|
|
|
|
{stmt: "a := mat2(1); a *= mat3(2)", err: true},
|
|
|
|
{stmt: "a := mat2(1); a *= mat4(2)", err: true},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, c := range cases {
|
|
|
|
_, err := ebiten.NewShader([]byte(fmt.Sprintf(`package main
|
|
|
|
|
|
|
|
func Fragment(position vec4, texCoord vec2, color vec4) vec4 {
|
|
|
|
%s
|
|
|
|
return position
|
|
|
|
}`, c.stmt)))
|
|
|
|
if err == nil && c.err {
|
|
|
|
t.Errorf("%s must return an error but does not", c.stmt)
|
|
|
|
} else if err != nil && !c.err {
|
|
|
|
t.Errorf("%s must not return nil but returned %v", c.stmt, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|