Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

text/v2: improve the performance with GoXFace #3149

Closed
11 tasks
hajimehoshi opened this issue Oct 27, 2024 · 3 comments
Closed
11 tasks

text/v2: improve the performance with GoXFace #3149

hajimehoshi opened this issue Oct 27, 2024 · 3 comments
Milestone

Comments

@hajimehoshi
Copy link
Owner

Operating System

  • Windows
  • macOS
  • Linux
  • FreeBSD
  • OpenBSD
  • Android
  • iOS
  • Nintendo Switch
  • PlayStation 5
  • Xbox
  • Web Browsers

What feature would you like to be added?

For example, run this program on browsers

package main

import (
	"fmt"
	"math/rand/v2"

	"github.com/hajimehoshi/bitmapfont/v3"

	"github.com/hajimehoshi/ebiten/v2"
	"github.com/hajimehoshi/ebiten/v2/ebitenutil"
	"github.com/hajimehoshi/ebiten/v2/text/v2"
)

const (
	screenWidth  = 640
	screenHeight = 480
	lineHeight   = 12
)

type Game struct {
	face text.Face
	text string
}

func (g *Game) Update() error {
	if g.face == nil {
		g.face = text.NewGoXFace(bitmapfont.Face)
	}
	if g.text == "" {
		const alphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
		xNum := screenWidth / 6
		yNum := (screenHeight / lineHeight) - 1
		for j := 0; j < yNum; j++ {
			for i := 0; i < xNum; i++ {
				g.text += string(alphabets[rand.IntN(len(alphabets))])
			}
			g.text += "\n"
		}
	}
	return nil
}

func (g *Game) Draw(screen *ebiten.Image) {
	ebitenutil.DebugPrint(screen, fmt.Sprintf("FPS: %0.2f, TPS: %0.2f", ebiten.ActualFPS(), ebiten.ActualTPS()))
	op := &text.DrawOptions{}
	op.GeoM.Translate(0, lineHeight)
	op.LineSpacing = 12
	text.Draw(screen, g.text, g.face, op)
}

func (g *Game) Layout(width, height int) (int, int) {
	return screenWidth, screenHeight
}

func main() {
	if err := ebiten.RunGame(&Game{}); err != nil {
		panic(err)
	}
}

image

This reached 120 FPS on desktop, but doesn't reach 120 FPS on browsers:

                     ..'          hajimehoshi@Hajimes-MacBook-Pro
                 ,xNMM.           -------------------------------
               .OMMMMo            OS: macOS 15.0.1 arm64
               lMM"               Host: MacBook Pro (14-inch, Nov 2023, Three Thunderbolt 4 ports)
     .;loddo:.  .olloddol;.       Kernel: 24.0.0
   cKMMMMMMMMMMNWMMMMMMMMMM0:     Uptime: 1 day, 17 hours, 26 mins
 .KMMMMMMMMMMMMMMMMMMMMMMMWd.     Packages: 143 (brew), 2 (brew-cask)
 XMMMMMMMMMMMMMMMMMMMMMMMX.       Shell: zsh 5.9
;MMMMMMMMMMMMMMMMMMMMMMMM:        Display (Color LCD): 3600x2338 @ 120Hz (as 1800x1169) [Built-in]
:MMMMMMMMMMMMMMMMMMMMMMMM:        DE: Aqua
.MMMMMMMMMMMMMMMMMMMMMMMMX.       WM: Quartz Compositor
 kMMMMMMMMMMMMMMMMMMMMMMMMWd.     WM Theme: Multicolor (Light)
 'XMMMMMMMMMMMMMMMMMMMMMMMMMMk    Font: .AppleSystemUIFont [System], Helvetica [User]
  'XMMMMMMMMMMMMMMMMMMMMMMMMK.    Cursor: Fill - Black, Outline - White (32px)
    kMMMMMMMMMMMMMMMMMMMMMMd      Terminal: Code Helper 1.94.2
     ;KMMMMMMMWXXWMMMMMMMk.       CPU: Apple M3 Pro (12) @ 4.06 GHz
       "cooc*"    "*coo'"         GPU: Apple M3 Pro (18) [Integrated]
                                  Memory: 15.13 GiB / 18.00 GiB (84%)
                                  Swap: 7.16 GiB / 8.00 GiB (89%)
                                  Disk (/): 311.66 GiB / 926.35 GiB (34%) - apfs [Read-only]
                                  Local IP (en0): 192.168.0.10/24 *
                                  Battery: 100% [AC connected]
                                  Power Adapter: 96W USB-C Power Adapter
                                  Locale: en_US.UTF-8

Here is the performance result:

image

Why is this needed?

No response

@hajimehoshi hajimehoshi added this to the v2.9.0 milestone Oct 27, 2024
@hajimehoshi
Copy link
Owner Author

GoTextFace has g.outputCache, while GoXFace doesn't have a counterpart.

@hajimehoshi
Copy link
Owner Author

Now the bottleneck is moved from font parsing to rendering, so I'm satisfied.

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants
@hajimehoshi and others