What is the fastest way to draw a 2D array of color triplets on screen?

前端 未结 6 1416
星月不相逢
星月不相逢 2021-02-14 18:24

The target language is C/C++ and the program has only to work on Linux, but platform independent solutions are preferred obviously. I run Xorg, XVideo and OpenGL are available.<

6条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-14 18:35

    The fastest way to draw a 2D array of color triplets:

    1. Use float (not byte, not double) storage. Each triplet consists of 3 floats from 0.0 to 1.0 each. This is the format implemented most optimally by GPUs (but use greyscale GL_LUMINANCE storage when you don't need hue - much faster!)
    2. Upload the array to a texture with glTexImage2D
    3. Make sure that the GL_TEXTURE_MIN_FILTER texture parameter is set to GL_NEAREST
    4. Map the texture to an appropriate quad.

    This method is slightly faster than glDrawPixels (which for some reason tends to be badly implemented) and a lot faster than using the platform's native blitting.

    Also, it gives you the option to repeatedly do step 4 without step 2 when your pixmap hasn't changed, which of course is much faster.

    Libraries that provide only slow native blitting include:

    • Windows' GDI
    • SDL on X11 (on Windows it provides a fast opengl backend when using HW_SURFACE)
    • Qt

    As to the FPS you can expect, drawing a 1024x768 texture on an Intel Core 2 Duo with Intel graphics: about 60FPS if the texture changes every frame and >100FPS if it doesn't.

    But just do it yourself and see ;)

提交回复
热议问题