I'm writing a program that calculates some pixel stuff in an OpenGL shader. With that calculation, I get a list containing every pixel that needs to be written to the screen. Is there a way I can draw all of this data at once rather than iterating through the list and drawing every pixel? I ask this because it would be extremely slow to iterate through the list that contains all the million pixels for my screen and individually write these pixels to the screen. Any help would be much appreciated. Thanks.
Asked
Active
Viewed 167 times
1
-
You may be able to do it via image masking, although the might require going through the same list and setting every pixel of the mask. How is this list of pixels being created? – martineau May 16 '22 at 23:21
-
@martineau Thanks, I'll look into that. I'm creating the list with an OpenGL compute shader that writes to it and a Z-buffer. – Matthew May 16 '22 at 23:31
-
Ah yes, sorry, I forgot you said that in your question. If there's some way of getting the pixel data in a non-list format it, might be better. – martineau May 16 '22 at 23:36
-
@martineau Yeah, possibly. What format would be suitable? – Matthew May 16 '22 at 23:38
1 Answers
0
See How to save pygame scene as jpeg?
If the color data is in the framebuffer you can read it with glReadPixels
before the display is updated (before pygame.display.flip()
or pygame.display.update()
). Use pygame.image.fromstring()
to create new Surfaces from the buffer:
size = screen.get_size()
buffer = glReadPixels(0, 0, *size, GL_RGBA, GL_UNSIGNED_BYTE)
pygame.display.flip()
screen_surf = pygame.image.fromstring(buffer, size, "RGBA")
If the data is in a texture (e.g. stored in a compute shader) you can read it with glGetTexImage
:
image_buffer = glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE)
image_surf = pygame.image.fromstring(image_buffer, (width, height), "RGBA")

Rabbid76
- 202,892
- 27
- 131
- 174
-
So in my OpenGL compute shader, I can store all my data in a texture(in RGB format?) and then I can just hand pygame this texture? Is this process fast enough for me to be able to do every tick with a 1280 x 800 resolution and still get an acceptable framerate? Thanks. – Matthew May 17 '22 at 08:47
-
@Matthew It's fast, however I don't know if it's fast enough for you. Why don't you just try it? – Rabbid76 May 17 '22 at 09:17