EngoEngine / engo

Engo is an open-source 2D game engine written in Go.
https://engoengine.github.io
MIT License
1.75k stars 136 forks source link

About performance #769

Closed kayon closed 2 years ago

kayon commented 3 years ago

When rendering multiple images, for example, 100 PNG images, just static rendering, no change, use 50% of cpu. Using gomobile bind on Android, Use only common.Text Show fps, fps cannot be stabilized at 60. Is there any way to improve performance?

Asday commented 3 years ago

Please provide a short, self-contained correct example.

kayon commented 3 years ago
func (*DefaultScene) Setup(u engo.Updater) {
    w, _ := u.(*ecs.World)
    common.SetBackground(color.White)
    w.AddSystem(&common.RenderSystem{})

    var x, y float32
    for i := 0; i < 100; i++ {
        tex, _ := common.LoadedSprite(fmt.Sprintf("_%d.svg", i))
        guy := Guy{BasicEntity: ecs.NewBasic()}
        guy.RenderComponent = common.RenderComponent{
            Drawable: tex,
            Scale:    engo.Point{X: 50 / tex.Width(), Y: 50 / tex.Height()},
        }
        guy.SpaceComponent = common.SpaceComponent{
            Position: engo.Point{x, y},
            Width:    50,
            Height:   50,
        }
        if x > 450 {
            x = 0
            y += 50
        } else {
            x += 50
        }
        for _, system := range w.Systems() {
            switch sys := system.(type) {
            case *common.RenderSystem:
                sys.Add(&guy.BasicEntity, &guy.RenderComponent, &guy.SpaceComponent)
            }
        }
    }
}
01

02

Noofbiz commented 3 years ago

It has to empty the vertex buffer every time you switch textures. If you use a sprite sheet it’ll vastly improve the cpu usage. I don’t think there’s a way to do that with svg at the moment, unfortunately. It would be easiest to save them all to a single png file and use the common.Spritesheet stuff

Noofbiz commented 3 years ago

I'll look into the mobile building fps issue as soon as I can, not sure what the problem there could be, but I haven't done mobile in a long time so it probably needs some work. Were you using the fps system in common?

kayon commented 3 years ago

I didn't use the FPS system in the common package, because this can't be customized, I made a similar one. It's not running on a simulator, processors is snapdragon 845.

Noofbiz commented 3 years ago

Hello! I'm using something similar to https://gist.github.com/Noofbiz/af17111cc0cc197c6bd631134b9d02e0 for testing. If I adjust the number of textures it generates to 100, so that's like reading 100 different textures off disk, it idles at about 5-10% of my CPU. If you start printing them to the screen, you get a quick jump to about 60% CPU usage at around 500. It then stays there until about 3-4k sprites where it finally hits about 90% and starts to slow down the FPS.

This was on my 2017 MacBook Pro, so your results may vary. But I don't think having 100 textures and printing 3-4k to the screen at any time (considering view culling is part of the default shader, this should be very hard to do in practice) is much to worry about. I'm going to try a variation of this for Android / iOS as well. I wouldn't be surprised if things could be optimized there ^_^

argigeek commented 2 years ago

Hi, friend! I see you used SVG. This isn't recommended for games because it's bad for performance.