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

Reduce VRAM usage #274

Closed
Kvel2D opened this issue Nov 14, 2023 · 6 comments
Closed

Reduce VRAM usage #274

Kvel2D opened this issue Nov 14, 2023 · 6 comments
Labels
enhancement New feature or request
Milestone

Comments

@Kvel2D
Copy link
Collaborator

Kvel2D commented Nov 14, 2023

VRAM usage has went up a lot since version 0.7, because of new tilesets. It was 0.8GB in version 0.7 and now it's 1.4GB in version 0.8. Should work on reducing it where possible.

For example, high VRAM usage causes crashes for some players: #273

Texture compression

2023-11-14 06_51_49-(_) GameScene tscn - YouTD 2 - Godot Engine
None of the compression options are of any help.

  • Lossless - this is the option we currently use for tilesheets.
  • Lossy - doesn't reduce VRAM usage, only affects the size of the executable/pckfile.
  • VRAM Compressed - would reduce VRAM usage by a lot BUT, it's not usable for textures used by tilesets. You will see Godot output errors like this "Can't set_pixel() on compressed image, sorry".
  • VRAM Uncompressed - same VRAM usage as Lossless.
  • Basis Universal - same as VRAM Compressed, not usable for tilesets.

Online tools like TinyPNG is same thing as Lossy compression - only reduces size on disk, doesn't reduce VRAM usage.

Empty space in tilesheets

We currently have a lot of unused space in tilesheets.
2023-11-14 07_02_39-GraphicsGale ver 2 08 24

The first thing we should do is remove equal amounts of space above all tile, using a script. This way, the tilesizes will still be equal to each other and setting up tilesets will still be easy.
Later we can look into packing tilesheets tightly - but that will make setting up tilesets more complicated.

Also some tilesheets have empty tiles - should remove them.

2023-11-14 07_11_20-GraphicsGale ver 2 08 24

Packing tilesheets

Decoration tilesheets can be packed like creep spritesheets:
2023-11-14 07_35_40-folder
Use PackSpriteSheet.gd script for that. Note that you need to fix the tileset after doing this.

Tileset duplication

We have two tilemap nodes - "Map" and "Decorations". We need two separate tilemaps because "Map" has tile size set to 256x128 and "Decorations" has tile size set to 64x32. This is so that tiles can be placed on the grid in "Map" but you can also do more flexible positioning of decoration tiles in "Decorations" tilemap.

When two different tilesets reference the same png, the png will be stored in VRAM twice.
For example, the "Floor.png" has two tilesets and it's memory usage is doubled.
2023-11-14 07_05_20-Map tscn - YouTD 2 - Godot Engine
Should avoid situations like this where possible.

@Kvel2D Kvel2D added the enhancement New feature or request label Nov 14, 2023
@Kvel2D
Copy link
Collaborator Author

Kvel2D commented Nov 14, 2023

Disabling texture padding reduced VRAM usage by 50%: ddea2fd
Before: 1.5GB
After: 0.75GB

Godot documentation says:

Disabling this setting might lead a small performance improvement
https://docs.godotengine.org/en/stable/classes/class_tilesetatlassource.html#class-tilesetatlassource-property-use-texture-padding
But for some reason, in our case it leads to huge performance improvement.

@Kvel2D
Copy link
Collaborator Author

Kvel2D commented Nov 16, 2023

Tried out prerendering the background here:
#276

This reduced VRAM usage: 0.75MB->0.4MB

Note that I had to restore foreground map which made VRAM usage go up to 650MB again. WIll need to dedicate more time to removing unused tiles from foreground tileset.

Breakdown of current VRAM usage (using prerendered background):

  • 30MB prerendered background
  • 30MB foreground tilemap
  • 10MB buildable area
  • 20MB bottom menu bar
  • 10MB upper bar
  • 40MB horadric menu theme
  • 40MB horadric menu + unit menu
  • 10MB shaders
  • 145MB creep sprites
    • 10MB air
    • 25MB mass
    • 25MB champion
    • 30MB normal
    • 55MB boss
  • 50MB preloaded scenes in Globals.gd
  • 30MB unaccounted, I didn't spend time on figuring out what uses this portion

Also note that tower sprites are dynamically loaded while the game is played and will consume VRAM.
For reference, a single tower family (Tiny Shrub tier 1 through 6 for example) will consume around 3 MB with Lossless compression of the atlas.
With VRAM compressed it will consume 1MB.
Should think about switching to VRAM compression for all tower atlases but need to confirm if they look ok in game that way.

@Kvel2D
Copy link
Collaborator Author

Kvel2D commented Nov 17, 2023

Enabled VRAM compression for creep atlases:
9199a88
234bc26

This reduced VRAM usage by creep sprites from 145MB to 45MB. Almost 4 times reduction, as expected.

@Kvel2D
Copy link
Collaborator Author

Kvel2D commented Nov 17, 2023

Had to disable VRAM compression for creep atlases - there are problems on Web export.
Tried to fix it but couldn't figure it out.
With "High Quality" enabled, you get "failed to load resource" errors in console and sprites don't draw at all.
With "High Quality" disabled, resources appear to load but creep sprites draw only one frame and then disappear.
There are also other settings related to VRAM compression in project settings and export config and I tried them but couldn't find a solution.

I also looked into using "Basis Universal" type of compression. It causes console to print these messages (not errors):

Basis universal cannot unpack level 1.

There is some discussion online but nothing conclusive. Decided not to proceed with this compression type because I don't understand the issue.

@Kvel2D
Copy link
Collaborator Author

Kvel2D commented Nov 17, 2023

Closing this because the biggest sources of VRAM usage were fixed and the game is back to VRAM usage levels from 0.7.
Can tackle specific VRAM usage sources separately later, as needed.

@Kvel2D Kvel2D closed this as completed Nov 17, 2023
@Praytic Praytic added this to the 0.9 milestone Dec 7, 2023
@Kvel2D
Copy link
Collaborator Author

Kvel2D commented Jul 6, 2024

I've looked into Basis Universal again and it seems usable.

There is one caveat.

When using Basis Universal for textures used in tilesets, you need to disable "Use Texture Padding" option. Otherwise, the texture will appear empty and you will get spammed with console errors.
2024-07-06 21_04_39-map tscn - YouTD 2 - Godot Engine

Turning on Basis Universal for tilesheets also causes these console errors:

Basis universal cannot unpack level 1.
These errors appear to be harmless and can be ignored.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants