Fastest, simplest way to blit a block of memory to the screen

Discussion in 'Mac Programming' started by Ian Moody, May 20, 2011.

  1. Ian Moody, May 20, 2011
    Last edited: May 20, 2011

    Ian Moody macrumors newbie

    Sep 9, 2009

    I'm in the process of writing a software 3d engine (just for fun, keeps my mind busy) and I would be very interested to know what the fastest way of blitting my rendered output to the screen would be (I'm of course aware that you can't ACTUALLY just blit something into screen memory directly) .

    I'm currently using a few basic OpenGL / GLUT calls to create a large texture from my rendered bitmap data and then render it into a window (yes, I know, using OpenGL to render bitmap to the screen is a bit silly), updating the texture every frame.

    My application is completely C based, although I would be happy to have one source file in (say) Objective-C if that file could handle the blitting side of things for me.

    So what I need is the simplest way to:

    *) Create a plain window at a specific (content) size (w,h)
    *) Blit a block of memory to that window
    *) Repeat.

    The only user interaction I need is to handle the Quit command.

    Any pointers would be really appreciated :)

    Update:- Reading though some other forum posts it seems like OpenGL / Quartz is the way to do it, but with OpenGL insisting on power of two textures it seems wasteful to have to make and update a 1042 x 1024 texture to update a 600x600 window for example.

    Ian Moody
  2. firewood, May 20, 2011
    Last edited: May 20, 2011

    firewood macrumors 604

    Jul 29, 2003
    Silicon Valley
    One fast way is to assign your arbitrary sized block of memory to a CGBitmapContext, convert that bitmap context to an image, assign that image to the contents of a visible CALayer, then doing a CATransaction commit when the entire frame is ready. Helps to double (or triple) buffer all the bitmaps. But, under the hood, all this might get converted into Open GL texture loads to the GPU anyway; so if you've done that, you're done. All iOS device display memory appears to be on the other side of the GPU, so there's absolutely no way to write to iOS display without going through a GPU texture load or triangle fill.

    One possible optimization for non-power-of-2 blits is to put multiple odd-sized bitmaps inside one larger 2^N texture map atlas.
  3. SidBala macrumors 6502a

    Jun 27, 2010
    Using a OpenGL window to render a bitmap is not at all silly. I do it all the time. You can use the GPU to do your interpolation and resizing. It can even make for a very good zoom window, when you want to magnify parts of the image you are displaying.

    Also, there is an extension that allows non-power of two textures. There is also a texture type - texture_rectangle that allows non-normalized texture coordinates. But I would recommend not using these.

    If you really wanted to just blit to the screen, you can use glDrawPixels. This is a very simply function that just writes directly to the framebuffer.

    glDrawPixels is probably the simplest way to achieve what you want, given that you already have an opengl window running.

Share This Page