Compare commits

..

3 Commits

Author SHA1 Message Date
Mykhailo Lohachov
1586c6764a
internal/shaderir: Kage update support for shift operators (#2916)
Closes #2755
2024-03-22 18:14:03 +09:00
Hajime Hoshi
86f53b73e3 examples/dragmascot: add a comment
Updates #2783
2024-03-22 12:41:15 +09:00
Hajime Hoshi
f2f037ebda examples: add an example to drag a desktop mascot
Closes #2783
2024-03-22 12:23:55 +09:00
5 changed files with 341 additions and 6 deletions

131
examples/dragmascot/main.go Normal file
View File

@ -0,0 +1,131 @@
// Copyright 2024 The Ebitengine 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.
// Mascot is a desktop mascot on cross platforms.
// This is inspired by mattn's gopher (https://github.com/mattn/gopher).
package main
import (
"bytes"
"image"
_ "image/png"
"log"
"github.com/hajimehoshi/ebiten/v2"
rmascot "github.com/hajimehoshi/ebiten/v2/examples/resources/images/mascot"
"github.com/hajimehoshi/ebiten/v2/inpututil"
)
const (
width = 200
height = 200
)
var (
gopher1 *ebiten.Image
gopher2 *ebiten.Image
gopher3 *ebiten.Image
)
func init() {
// Decode an image from the image file's byte slice.
img1, _, err := image.Decode(bytes.NewReader(rmascot.Out01_png))
if err != nil {
log.Fatal(err)
}
gopher1 = ebiten.NewImageFromImage(img1)
img2, _, err := image.Decode(bytes.NewReader(rmascot.Out02_png))
if err != nil {
log.Fatal(err)
}
gopher2 = ebiten.NewImageFromImage(img2)
img3, _, err := image.Decode(bytes.NewReader(rmascot.Out03_png))
if err != nil {
log.Fatal(err)
}
gopher3 = ebiten.NewImageFromImage(img3)
}
type mascot struct {
count int
dragging bool
dragStartWindowX int
dragStartWindowY int
dragStartCursorX int
dragStartCursorY int
cursorToWindowX float64
cursorToWindowY float64
}
func (m *mascot) Layout(outsideWidth, outsideHeight int) (int, int) {
// The cursor position is in a "logical" coordinate, which is determined by the game width and height.
// Calculate the factors to convert a cursor position to a window position.
m.cursorToWindowX = float64(outsideWidth) / float64(width)
m.cursorToWindowY = float64(outsideHeight) / float64(height)
return width, height
}
func (m *mascot) Update() error {
if !ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) {
m.dragging = false
}
if !m.dragging && inpututil.IsMouseButtonJustPressed(ebiten.MouseButtonLeft) {
m.dragging = true
m.dragStartWindowX, m.dragStartWindowY = ebiten.WindowPosition()
m.dragStartCursorX, m.dragStartCursorY = ebiten.CursorPosition()
}
if m.dragging {
// Move the window only by the delta of the cursor.
cx, cy := ebiten.CursorPosition()
dx := int(float64(cx-m.dragStartCursorX) * m.cursorToWindowX)
dy := int(float64(cy-m.dragStartCursorY) * m.cursorToWindowY)
wx, wy := ebiten.WindowPosition()
ebiten.SetWindowPosition(wx+dx, wy+dy)
m.count++
}
return nil
}
func (m *mascot) Draw(screen *ebiten.Image) {
img := gopher1
if m.dragging {
switch (m.count / 3) % 4 {
case 0:
img = gopher1
case 1, 3:
img = gopher2
case 2:
img = gopher3
}
}
screen.DrawImage(img, nil)
}
func main() {
ebiten.SetWindowDecorated(false)
ebiten.SetWindowFloating(true)
ebiten.SetWindowSize(width, height)
op := &ebiten.RunGameOptions{}
op.ScreenTransparent = true
op.SkipTaskbar = true
if err := ebiten.RunGameWithOptions(&mascot{}, op); err != nil {
log.Fatal(err)
}
}

View File

@ -105,7 +105,7 @@ func (cs *compileState) parseExpr(block *block, fname string, expr ast.Expr, mar
}
// Resolve untyped constants.
l, r, ok := shaderir.ResolveUntypedConstsForBinaryOp(lhs[0].Const, rhs[0].Const, lhst, rhst)
l, r, ok := shaderir.ResolveUntypedConstsForBinaryOp(op2, lhs[0].Const, rhs[0].Const, lhst, rhst)
if !ok {
// TODO: Show a better type name for untyped constants.
cs.addError(e.Pos(), fmt.Sprintf("types don't match: %s %s %s", lhst.String(), op, rhst.String()))
@ -153,6 +153,13 @@ func (cs *compileState) parseExpr(block *block, fname string, expr ast.Expr, mar
v = gconstant.MakeBool(b)
case token.EQL, token.NEQ, token.LSS, token.LEQ, token.GTR, token.GEQ:
v = gconstant.MakeBool(gconstant.Compare(lhs[0].Const, op, rhs[0].Const))
case token.SHL, token.SHR:
shift, ok := gconstant.Int64Val(rhs[0].Const)
if !ok {
cs.addError(e.Pos(), fmt.Sprintf("unexpected %s type for: %s", rhs[0].Const.String(), e.Op))
return nil, nil, nil, false
}
v = gconstant.Shift(lhs[0].Const, op, uint(shift))
default:
v = gconstant.BinaryOp(lhs[0].Const, op, rhs[0].Const)
}

View File

@ -60,7 +60,7 @@ func (cs *compileState) parseStmt(block *block, fname string, stmt ast.Stmt, inP
return nil, false
}
stmts = append(stmts, ss...)
case token.ADD_ASSIGN, token.SUB_ASSIGN, token.MUL_ASSIGN, token.QUO_ASSIGN, token.REM_ASSIGN, token.AND_ASSIGN, token.OR_ASSIGN, token.XOR_ASSIGN, token.AND_NOT_ASSIGN:
case token.ADD_ASSIGN, token.SUB_ASSIGN, token.MUL_ASSIGN, token.QUO_ASSIGN, token.REM_ASSIGN, token.AND_ASSIGN, token.OR_ASSIGN, token.XOR_ASSIGN, token.AND_NOT_ASSIGN, token.SHL_ASSIGN, token.SHR_ASSIGN:
rhs, rts, ss, ok := cs.parseExpr(block, fname, stmt.Rhs[0], true)
if !ok {
return nil, false
@ -100,6 +100,10 @@ func (cs *compileState) parseStmt(block *block, fname string, stmt ast.Stmt, inP
op = shaderir.Or
case token.XOR_ASSIGN:
op = shaderir.Xor
case token.SHL_ASSIGN:
op = shaderir.LeftShift
case token.SHR_ASSIGN:
op = shaderir.RightShift
default:
cs.addError(stmt.Pos(), fmt.Sprintf("unexpected token: %s", stmt.Tok))
return nil, false
@ -110,7 +114,7 @@ func (cs *compileState) parseStmt(block *block, fname string, stmt ast.Stmt, inP
cs.addError(stmt.Pos(), fmt.Sprintf("invalid operation: operator / not defined on %s", rts[0].String()))
return nil, false
}
if op == shaderir.And || op == shaderir.Or || op == shaderir.Xor {
if op == shaderir.And || op == shaderir.Or || op == shaderir.Xor || op == shaderir.LeftShift || op == shaderir.RightShift {
if lts[0].Main != shaderir.Int && !lts[0].IsIntVector() {
cs.addError(stmt.Pos(), fmt.Sprintf("invalid operation: operator %s not defined on %s", stmt.Tok, lts[0].String()))
}
@ -137,7 +141,7 @@ func (cs *compileState) parseStmt(block *block, fname string, stmt ast.Stmt, inP
}
}
case shaderir.Float:
if op == shaderir.And || op == shaderir.Or || op == shaderir.Xor {
if op == shaderir.And || op == shaderir.Or || op == shaderir.Xor || op == shaderir.LeftShift || op == shaderir.RightShift {
cs.addError(stmt.Pos(), fmt.Sprintf("invalid operation: operator %s not defined on %s", stmt.Tok, lts[0].String()))
} else if rhs[0].Const != nil &&
(rts[0].Main == shaderir.None || rts[0].Main == shaderir.Float) &&
@ -148,7 +152,7 @@ func (cs *compileState) parseStmt(block *block, fname string, stmt ast.Stmt, inP
return nil, false
}
case shaderir.Vec2, shaderir.Vec3, shaderir.Vec4, shaderir.Mat2, shaderir.Mat3, shaderir.Mat4:
if op == shaderir.And || op == shaderir.Or || op == shaderir.Xor {
if op == shaderir.And || op == shaderir.Or || op == shaderir.Xor || op == shaderir.LeftShift || op == shaderir.RightShift {
cs.addError(stmt.Pos(), fmt.Sprintf("invalid operation: operator %s not defined on %s", stmt.Tok, lts[0].String()))
} else if (op == shaderir.MatrixMul || op == shaderir.Div) &&
(rts[0].Main == shaderir.Float ||

View File

@ -1314,6 +1314,169 @@ func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
}
}
// Issue: #2755
func TestSyntaxOperatorShift(t *testing.T) {
cases := []struct {
stmt string
err bool
}{
{stmt: "a := 1 << 2; _ = a", err: false},
{stmt: "a := 1 << 2.0; _ = a", err: false},
{stmt: "a := 1.0 << 2; _ = a", err: false},
{stmt: "a := 1.0 << 2.0; _ = a", err: false},
{stmt: "a := 1.0 << int(1); _ = a", err: false},
{stmt: "a := int(1) << 2.0; _ = a", err: false},
{stmt: "a := ivec2(1) << 2.0; _ = a", err: false},
{stmt: "var a = 1; b := a << 2.0; _ = b", err: false},
{stmt: "var a = 1; b := 2.0 << a; _ = b", err: false}, // PR: #2916
{stmt: "a := float(1.0) << 2; _ = a", err: true},
{stmt: "a := 1 << float(2.0); _ = a", err: true},
{stmt: "a := ivec2(1) << 2; _ = a", err: false},
{stmt: "a := 1 << ivec2(2); _ = a", err: true},
{stmt: "a := ivec2(1) << float(2.0); _ = a", err: true},
{stmt: "a := float(1.0) << ivec2(2); _ = a", err: true},
{stmt: "a := ivec2(1) << ivec2(2); _ = a", err: false},
{stmt: "a := ivec3(1) << ivec2(2); _ = a", err: true},
{stmt: "a := ivec2(1) << ivec3(2); _ = a", err: true},
{stmt: "a := 1 << vec2(2); _ = a", err: true},
{stmt: "a := vec2(1) << 2; _ = a", err: true},
{stmt: "a := float(1.0) << vec2(2); _ = a", err: true},
{stmt: "a := vec2(1) << float(2.0); _ = a", err: true},
{stmt: "a := vec2(1) << vec2(2); _ = a", err: true},
{stmt: "a := vec2(1) << vec3(2); _ = a", err: true},
{stmt: "a := vec3(1) << vec2(2); _ = a", err: true},
{stmt: "a := vec2(1) << ivec2(2); _ = a", err: true},
{stmt: "a := ivec2(1) << vec2(2); _ = a", err: true},
{stmt: "a := vec3(1) << ivec2(2); _ = a", err: true},
{stmt: "a := ivec2(1) << vec3(2); _ = a", err: true},
{stmt: "a := 1 >> 2; _ = a", err: false},
{stmt: "a := 1 >> 2.0; _ = a", err: false},
{stmt: "a := 1.0 >> 2; _ = a", err: false},
{stmt: "a := 1.0 >> 2.0; _ = a", err: false},
{stmt: "a := 1.0 >> int(1); _ = a", err: false},
{stmt: "a := int(1) >> 2.0; _ = a", err: false},
{stmt: "a := ivec2(1) >> 2.0; _ = a", err: false},
{stmt: "var a = 1; b := a >> 2.0; _ = b", err: false},
{stmt: "var a = 1; b := 2.0 >> a; _ = b", err: false}, // PR: #2916
{stmt: "a := float(1.0) >> 2; _ = a", err: true},
{stmt: "a := 1 >> float(2.0); _ = a", err: true},
{stmt: "a := ivec2(1) >> 2; _ = a", err: false},
{stmt: "a := 1 >> ivec2(2); _ = a", err: true},
{stmt: "a := ivec2(1) >> float(2.0); _ = a", err: true},
{stmt: "a := float(1.0) >> ivec2(2); _ = a", err: true},
{stmt: "a := ivec2(1) >> ivec2(2); _ = a", err: false},
{stmt: "a := ivec3(1) >> ivec2(2); _ = a", err: true},
{stmt: "a := ivec2(1) >> ivec3(2); _ = a", err: true},
{stmt: "a := 1 >> vec2(2); _ = a", err: true},
{stmt: "a := vec2(1) >> 2; _ = a", err: true},
{stmt: "a := float(1.0) >> vec2(2); _ = a", err: true},
{stmt: "a := vec2(1) >> float(2.0); _ = a", err: true},
{stmt: "a := vec2(1) >> vec2(2); _ = a", err: true},
{stmt: "a := vec2(1) >> vec3(2); _ = a", err: true},
{stmt: "a := vec3(1) >> vec2(2); _ = a", err: true},
{stmt: "a := vec2(1) >> ivec2(2); _ = a", err: true},
{stmt: "a := ivec2(1) >> vec2(2); _ = a", err: true},
{stmt: "a := vec3(1) >> ivec2(2); _ = a", err: true},
{stmt: "a := ivec2(1) >> vec3(2); _ = a", err: true},
}
for _, c := range cases {
_, err := compileToIR([]byte(fmt.Sprintf(`package main
func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
%s
return dstPos
}`, 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)
}
}
}
func TestSyntaxOperatorShiftAssign(t *testing.T) {
cases := []struct {
stmt string
err bool
}{
{stmt: "a := 1; a <<= 2; _ = a", err: false},
{stmt: "a := 1; a <<= 2.0; _ = a", err: false},
{stmt: "a := float(1.0); a <<= 2; _ = a", err: true},
{stmt: "a := 1; a <<= float(2.0); _ = a", err: true},
{stmt: "a := ivec2(1); a <<= 2; _ = a", err: false},
{stmt: "a := 1; a <<= ivec2(2); _ = a", err: true},
{stmt: "a := ivec2(1); a <<= float(2.0); _ = a", err: true},
{stmt: "a := float(1.0); a <<= ivec2(2); _ = a", err: true},
{stmt: "a := ivec2(1); a <<= ivec2(2); _ = a", err: false},
{stmt: "a := ivec3(1); a <<= ivec2(2); _ = a", err: true},
{stmt: "a := ivec2(1); a <<= ivec3(2); _ = a", err: true},
{stmt: "a := 1; a <<= vec2(2); _ = a", err: true},
{stmt: "a := vec2(1); a <<= 2; _ = a", err: true},
{stmt: "a := float(1.0); a <<= vec2(2); _ = a", err: true},
{stmt: "a := vec2(1); a <<= float(2.0); _ = a", err: true},
{stmt: "a := vec2(1); a <<= vec2(2); _ = a", err: true},
{stmt: "a := vec2(1); a <<= vec3(2); _ = a", err: true},
{stmt: "a := vec3(1); a <<= vec2(2); _ = a", err: true},
{stmt: "a := vec2(1); a <<= ivec2(2); _ = a", err: true},
{stmt: "a := ivec2(1); a <<= vec2(2); _ = a", err: true},
{stmt: "a := vec3(1); a <<= ivec2(2); _ = a", err: true},
{stmt: "a := ivec2(1); a <<= vec3(2); _ = a", err: true},
{stmt: "const c = 2; a := 1; a <<= c; _ = a", err: false},
{stmt: "const c = 2.0; a := 1; a <<= c; _ = a", err: false},
{stmt: "const c = 2; a := float(1.0); a <<= c; _ = a", err: true},
{stmt: "const c float = 2; a := 1; a <<= c; _ = a", err: true},
{stmt: "const c float = 2.0; a := 1; a <<= c; _ = a", err: true},
{stmt: "const c int = 2; a := ivec2(1); a <<= c; _ = a", err: false},
{stmt: "const c int = 2; a := vec2(1); a <<= c; _ = a", err: true},
{stmt: "a := 1; a >>= 2; _ = a", err: false},
{stmt: "a := 1; a >>= 2.0; _ = a", err: false},
{stmt: "a := float(1.0); a >>= 2; _ = a", err: true},
{stmt: "a := 1; a >>= float(2.0); _ = a", err: true},
{stmt: "a := ivec2(1); a >>= 2; _ = a", err: false},
{stmt: "a := 1; a >>= ivec2(2); _ = a", err: true},
{stmt: "a := ivec2(1); a >>= float(2.0); _ = a", err: true},
{stmt: "a := float(1.0); a >>= ivec2(2); _ = a", err: true},
{stmt: "a := ivec2(1); a >>= ivec2(2); _ = a", err: false},
{stmt: "a := ivec3(1); a >>= ivec2(2); _ = a", err: true},
{stmt: "a := ivec2(1); a >>= ivec3(2); _ = a", err: true},
{stmt: "a := 1; a >>= vec2(2); _ = a", err: true},
{stmt: "a := vec2(1); a >>= 2; _ = a", err: true},
{stmt: "a := float(1.0); a >>= vec2(2); _ = a", err: true},
{stmt: "a := vec2(1); a >>= float(2.0); _ = a", err: true},
{stmt: "a := vec2(1); a >>= vec2(2); _ = a", err: true},
{stmt: "a := vec2(1); a >>= vec3(2); _ = a", err: true},
{stmt: "a := vec3(1); a >>= vec2(2); _ = a", err: true},
{stmt: "a := vec2(1); a >>= ivec2(2); _ = a", err: true},
{stmt: "a := ivec2(1); a >>= vec2(2); _ = a", err: true},
{stmt: "a := vec3(1); a >>= ivec2(2); _ = a", err: true},
{stmt: "a := ivec2(1); a >>= vec3(2); _ = a", err: true},
{stmt: "const c = 2; a := 1; a >>= c; _ = a", err: false},
{stmt: "const c = 2.0; a := 1; a >>= c; _ = a", err: false},
{stmt: "const c = 2; a := float(1.0); a >>= c; _ = a", err: true},
{stmt: "const c float = 2; a := 1; a >>= c; _ = a", err: true},
{stmt: "const c float = 2.0; a := 1; a >>= c; _ = a", err: true},
{stmt: "const c int = 2; a := ivec2(1); a >>= c; _ = a", err: false},
{stmt: "const c int = 2; a := vec2(1); a >>= c; _ = a", err: true},
}
for _, c := range cases {
_, err := compileToIR([]byte(fmt.Sprintf(`package main
func Fragment(dstPos vec4, srcPos vec2, color vec4) vec4 {
%s
return dstPos
}`, 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)
}
}
}
// Issue #1971
func TestSyntaxOperatorMultiplyAssign(t *testing.T) {
cases := []struct {

View File

@ -18,8 +18,21 @@ import (
"go/constant"
)
func ResolveUntypedConstsForBinaryOp(lhs, rhs constant.Value, lhst, rhst Type) (newLhs, newRhs constant.Value, ok bool) {
func ResolveUntypedConstsForBinaryOp(op Op, lhs, rhs constant.Value, lhst, rhst Type) (newLhs, newRhs constant.Value, ok bool) {
if lhst.Main == None && rhst.Main == None {
if op == LeftShift || op == RightShift {
newLhs = constant.ToInt(lhs)
newRhs = constant.ToInt(rhs)
if newLhs.Kind() == constant.Unknown {
return nil, nil, false
}
if newRhs.Kind() == constant.Unknown {
return nil, nil, false
}
return newLhs, newRhs, true
}
if lhs.Kind() == rhs.Kind() {
return lhs, rhs, true
}
@ -98,6 +111,13 @@ func TypeFromBinaryOp(op Op, lhst, rhst Type, lhsConst, rhsConst constant.Value)
return Type{}, false
}
if op == LeftShift || op == RightShift {
if lhsConst.Kind() == constant.Int && rhsConst.Kind() == constant.Int {
return Type{Main: Int}, true
}
return Type{}, false
}
if op == EqualOp || op == NotEqualOp || op == LessThanOp || op == LessThanEqualOp || op == GreaterThanOp || op == GreaterThanEqualOp {
return Type{Main: Bool}, true
}
@ -195,6 +215,16 @@ func TypeFromBinaryOp(op Op, lhst, rhst Type, lhsConst, rhsConst constant.Value)
return Type{}, false
}
if op == LeftShift || op == RightShift {
if (lhst.Main == Int || lhst.IsIntVector()) && rhst.Main == Int {
return lhst, true
}
if lhst.IsIntVector() && rhst.IsIntVector() && lhst.VectorElementCount() == rhst.VectorElementCount() {
return lhst, true
}
return Type{}, false
}
if lhst.Equal(&rhst) {
if lhst.Main == None {
return rhst, true