mirror of
				https://github.com/stevenrobertson/cuburn.git
				synced 2025-11-03 09:50:53 -05:00 
			
		
		
		
	Fix Pyglet output (and improve speed!)
Instead of using Pyglet's image uploading process, we write data straight to an OpenGL texture.
This commit is contained in:
		
							
								
								
									
										20
									
								
								main.py
									
									
									
									
									
								
							
							
						
						
									
										20
									
								
								main.py
									
									
									
									
									
								
							@ -65,9 +65,11 @@ def main(args, prof):
 | 
				
			|||||||
        return
 | 
					        return
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    import pyglet
 | 
					    import pyglet
 | 
				
			||||||
 | 
					    import pyglet.gl as gl
 | 
				
			||||||
    w, h = gprof.width, gprof.height
 | 
					    w, h = gprof.width, gprof.height
 | 
				
			||||||
    window = pyglet.window.Window(w, h, vsync=False)
 | 
					    window = pyglet.window.Window(w, h, vsync=False)
 | 
				
			||||||
    image = pyglet.image.CheckerImagePattern().create_image(w, h)
 | 
					    image = pyglet.image.CheckerImagePattern().create_image(w, h)
 | 
				
			||||||
 | 
					    tex = image.texture
 | 
				
			||||||
    label = pyglet.text.Label('Rendering first frame', x=5, y=h-5,
 | 
					    label = pyglet.text.Label('Rendering first frame', x=5, y=h-5,
 | 
				
			||||||
                              width=w, anchor_y='top', font_size=16,
 | 
					                              width=w, anchor_y='top', font_size=16,
 | 
				
			||||||
                              bold=True, multiline=True)
 | 
					                              bold=True, multiline=True)
 | 
				
			||||||
@ -75,7 +77,7 @@ def main(args, prof):
 | 
				
			|||||||
    @window.event
 | 
					    @window.event
 | 
				
			||||||
    def on_draw():
 | 
					    def on_draw():
 | 
				
			||||||
        window.clear()
 | 
					        window.clear()
 | 
				
			||||||
        image.texture.blit(0, 0)
 | 
					        tex.blit(0, 0, 0)
 | 
				
			||||||
        label.draw()
 | 
					        label.draw()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @window.event
 | 
					    @window.event
 | 
				
			||||||
@ -101,8 +103,20 @@ def main(args, prof):
 | 
				
			|||||||
            real_dt = time.time() - last_time[0]
 | 
					            real_dt = time.time() - last_time[0]
 | 
				
			||||||
            last_time[0] = time.time()
 | 
					            last_time[0] = time.time()
 | 
				
			||||||
            save(out)
 | 
					            save(out)
 | 
				
			||||||
            imgbuf = np.uint8(out.buf.flatten() * 255)
 | 
					            if out.buf.dtype == np.uint8:
 | 
				
			||||||
            image.set_data('RGBA', -w*4, imgbuf.tostring())
 | 
					                fmt = gl.GL_UNSIGNED_BYTE
 | 
				
			||||||
 | 
					            elif out.buf.dtype == np.uint16:
 | 
				
			||||||
 | 
					                fmt = gl.GL_UNSIGNED_SHORT
 | 
				
			||||||
 | 
					            else:
 | 
				
			||||||
 | 
					                label.text = 'Unsupported format: ' + out.buf.dtype
 | 
				
			||||||
 | 
					                return
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            h, w, ch = out.buf.shape
 | 
				
			||||||
 | 
					            gl.glEnable(tex.target)
 | 
				
			||||||
 | 
					            gl.glBindTexture(tex.target, tex.id)
 | 
				
			||||||
 | 
					            gl.glTexImage2D(tex.target, 0, gl.GL_RGB8, w, h, 0, gl.GL_RGBA,
 | 
				
			||||||
 | 
					                            fmt, out.buf.tostring())
 | 
				
			||||||
 | 
					            gl.glDisable(tex.target)
 | 
				
			||||||
            label.text = '%s (%g fps)' % (out.idx, 1./real_dt)
 | 
					            label.text = '%s (%g fps)' % (out.idx, 1./real_dt)
 | 
				
			||||||
        else:
 | 
					        else:
 | 
				
			||||||
            label.text += '.'
 | 
					            label.text += '.'
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user