Offscreen rendering and GLSL

393 views
Skip to first unread message

Stéfan van der Walt

unread,
Jan 5, 2009, 5:21:09 AM1/5/09
to pyglet...@googlegroups.com
Hi all,

I would like to develop a SciPy toolkit for GPU-accelerated
computation. Tristam MacDonald recently posted a neat Shader class
exposing GLSL, which provided me with a good starting point. I have a
couple of further questions, and would be glad for any help:

1) How do I implement offscreen rendering? It seems like Framebuffer
Objects provide the best cross-"platform" solution, but Pyglet does
not explicitly provide an API to access those.
2) How do I render a single frame and grab it (instead of rendering 60
fps until I'm reasonably sure the computation is done).
3) Are double-precision buffers available, or can I only do
single-precision fixed-point processing in GLSL?

Thank you for all your hard work on exposing OpenGL to the masses.
Pyglet is great!

Kind regards,
Stéfan

Tristam MacDonald

unread,
Jan 5, 2009, 10:03:30 AM1/5/09
to pyglet...@googlegroups.com
On Mon, Jan 5, 2009 at 6:21 AM, Stéfan van der Walt <sjvd...@gmail.com> wrote:
1) How do I implement offscreen rendering?  It seems like Framebuffer
Objects provide the best cross-"platform" solution, but Pyglet does
not explicitly provide an API to access those.
Although pyglet does not wrap frame buffer objects, you can manage them yourself, as pyglet's opengl wrapper does expose the necessary extensions.

However, frame buffer objects still need a valid opengl context to operate (as do all opengl features). Your best bet would be to create a window with visible=False, and leave it invisible while rendering into your FBO. I can't guarantee this to work, as I haven't tried it, but the theory is sound.

2) How do I render a single frame and grab it (instead of rendering 60
fps until I'm reasonably sure the computation is done).
 Don't use app.run(), or any other typical pyglet run loop, instead manually clear, draw and flip the window.

3) Are double-precision buffers available, or can I only do
single-precision fixed-point processing in GLSL?
Only single precision is available on the GPU (and sometimes less than the full 32-bits on elderly models).

--
Tristam MacDonald
http://swiftcoder.wordpress.com/

Richard Jones

unread,
Jan 5, 2009, 3:21:19 PM1/5/09
to pyglet...@googlegroups.com, Tristam MacDonald
On Tue, 6 Jan 2009, Tristam MacDonald wrote:
> On Mon, Jan 5, 2009 at 6:21 AM, Stéfan van der Walt
<sjvd...@gmail.com>wrote:
> > 1) How do I implement offscreen rendering? It seems like Framebuffer
> > Objects provide the best cross-"platform" solution, but Pyglet does
> > not explicitly provide an API to access those.
>
> Although pyglet does not wrap frame buffer objects, you can manage them
> yourself, as pyglet's opengl wrapper does expose the necessary extensions.

Indeed. Here's some code from my old wydget toolkit in pyglet contrib:

# create our frame buffer
fbo = gl.GLuint()
gl.glGenFramebuffersEXT(1, ctypes.byref(fbo))
gl.glBindFramebufferEXT(gl.GL_FRAMEBUFFER_EXT, fbo)

# allocate a texture and add to the frame buffer
tex = image.Texture.create_for_size(gl.GL_TEXTURE_2D, w, h,
gl.GL_RGBA)
gl.glBindTexture(gl.GL_TEXTURE_2D, tex.id)
gl.glFramebufferTexture2DEXT(gl.GL_FRAMEBUFFER_EXT,
gl.GL_COLOR_ATTACHMENT0_EXT, gl.GL_TEXTURE_2D, tex.id, 0)

status = gl.glCheckFramebufferStatusEXT(gl.GL_FRAMEBUFFER_EXT)
assert status == gl.GL_FRAMEBUFFER_COMPLETE_EXT

# now render
gl.glBindFramebufferEXT(gl.GL_FRAMEBUFFER_EXT, fbo)
function()
gl.glBindFramebufferEXT(gl.GL_FRAMEBUFFER_EXT, 0)

# clean up
gl.glDeleteFramebuffersEXT(1, ctypes.byref(fbo))

where function, w and h are things you need to provide.


> However, frame buffer objects still need a valid opengl context to operate
> (as do all opengl features). Your best bet would be to create a window with
> visible=False, and leave it invisible while rendering into your FBO. I
> can't guarantee this to work, as I haven't tried it, but the theory is
> sound.

pyglet creates a context at startup so that shouldn't be necessary.


Richard

Stéfan van der Walt

unread,
Jan 6, 2009, 8:27:47 AM1/6/09
to pyglet...@googlegroups.com
2009/1/5 Richard Jones <r1char...@gmail.com>:

>> > 1) How do I implement offscreen rendering? It seems like Framebuffer
>> > Objects provide the best cross-"platform" solution, but Pyglet does
>> > not explicitly provide an API to access those.
>>
>> Although pyglet does not wrap frame buffer objects, you can manage them
>> yourself, as pyglet's opengl wrapper does expose the necessary extensions.
>
> Indeed. Here's some code from my old wydget toolkit in pyglet contrib:

Thank you, Tristam and Richard, for your thoughtful answers. These
should get me up and running quickly!

Regards
Stéfan

Reply all
Reply to author
Forward
0 new messages