[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

Re: [pygame] Speed up image.tostring with OpenGL ?



Hi,

try using surfarray.  To avoid converting the data to a python string.
 pyopengl supports numpy arrays to do this.

Also, different graphics cards are optimised for different texture
formats.  It can easily be 2-8x faster to have the correct format for
your driver/card combination.  However modern cards are getting faster
support for more formats.

However, the string conversion is probably the one to get rid of.  I
know for certain this will speed things up a lot.

# something like this... untested.
w,h = surf.get_size()
data = pygame.surfarray.pixels2d(surf)
texture = glGenTextures(1)
glBindTexture(GL_TEXTURE_2D, texture)
glPixelStorei(GL_UNPACK_ALIGNMENT,1)
glTexImage2D(GL_TEXTURE_2D, 0, 3, w, h, 0, GL_RGB, GL_UNSIGNED_BYTE, data)
# ... etc...

Also to speed up image loading/decoding...
    import pygame.threads
    pygame.threads.init(8)
    surfs = pygame.threads.tmap(pygame.image.load, your_images)

Another thing to do is to make sure you are reusing textures (if
possible).  Since allocating a new texture each time can be slow, so
if you application allows it, reuse textures.




cheers,


On Mon, Jan 4, 2010 at 4:06 AM, Alexandre Quessy <alexandre@xxxxxxxxxx> wrote:
> Hello Pygame users and developers !
>
> Is there a way I can speed up the transfert from a surface to an
> OpenGL texture ? Right now, I use the pygame.image.tostring function,
> but it seems like converting the pixels to a Python str creates a
> serious bottle neck in my stop motion application...
>
> I think that it should either be implemented directly in Pygame, (best
> option) or done using something like ctypes in my application. If none
> of these option is not easy enough, I am thinking about switching to
> Gstreamer+GTK with the GdkPixBuf, instead of Pygame surfaces. An other
> option would be to rewrite the whole application in C++...
>
> Any suggestion to fix this in a way that is not too long ?
> My frame rate drops to 4 FPS when there are more than a few surfaces in my list.
> Please try Toonloop to test this out. It works well on Linux. If
> someone get it to work on Mac or Windows, I would need hints for
> packaging it for those OS too. I convert the surfaces to OpenGL
> texture in texture_from_image from the file
> http://bitbucket.org/aalex/toonloop1/src/tip/toon/draw.py on what is
> currently the line 42.
> --
> Alexandre Quessy
> http://alexandre.quessy.net/
>