Monday, 8 April 2019

Why are pyglet.image and texture so heavy?

Envrionment:

  • python: 3.6.6
  • pyglet: 1.3.2

Here is my code and results:

import pyglet

images = []
textures = []

with_textures = True
count = 10
for x in range(count):
    image = pyglet.image.load("big.png")  # 2.1 Mb source 2400*2400px
    images.append(image)
    if with_textures:
        texture_grid = pyglet.image.ImageGrid(image, 10, 10).get_texture_sequence()
        textures.append(texture_grid)

# RES in htop result without textures
# count = 10 - 300Mb
# count = 20 - 553Mb
# count = 30 - 753Mb
# count = 40 - 973Mb
# ~23Mb just for each Image

# RES in htop result with textures
# count = 10 - 996Mb
# count = 20 - 1878Mb
# count = 30 - 2716Mb
# count = 40 - 3597Mb
# ~86Mb for Image and prepared grid


input("Press enter to exit")

Questions:

  1. Why each 2.1Mb file leads to 23Mb of memory usage with pyglet.image.AbstractImage?
    • If ImageGrid is used for creating sprite sheet -> it leads to additional ~60Mb
  2. How to deal with it? Because if game contains 50+ big sprites it would be not real to dedicate such many memory only for textures.
  3. Maybe there is some other approach in creating games which is used sprites? Or I should change my stack technology(pyglet as main library, also was trying with pygame) for client side?

PS: First time I've rewritten my application from pygame to pyglet, because I didn't consider some aspects of event loop in pygame, and now I hadn't test resource usage of pyglet library for my use-cases.

Update/clarification:

I'm using ImageGrid as for 3d part in vertices as for 2d part in pyglet.sprite.Sprite

Example of using in 3D part:

# texture_group is created once for each sprite sheet, same as texture_grid
texture_group = pyglet.graphics.TextureGroup(texture_grid, order_group)
...

tex_map = texture_grid[self.texture_grid_index].texture.tex_coords
tex_coords = ('t3f', tex_map)
self.entity = self.batch.add(
    4, pyglet.gl.GL_QUADS,
    texture_group,
    ('v3f', (x, y, z,
             x_, y, z,
             x_, y_, z,
             x, y_, z)
     ),
    tex_coords)

Example of using in 2D part:

pyglet.sprite.Sprite(img=texture_grid[self.texture_grid_index], x=0, y=0,
                     batch=self.batch, group=some_order_group)



from Why are pyglet.image and texture so heavy?

No comments:

Post a Comment