在屏幕上绘制二维颜色三元组的最快方法是什么?

发布于 2024-07-13 05:58:57 字数 169 浏览 16 评论 0原文

目标语言是C/C++,程序只需在Linux上运行,但显然首选独立于平台的解决方案。 我运行Xorg,XVideo和OpenGL都可用。

在配备英特尔显卡的英特尔酷睿 2 双核处理器上,我在 1024x768 上的预期帧速率是多少? (仅绘图计数,考虑 RAM 中准备就绪的阵列;不需要精确的预测)

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.

How many FPS can I expect on 1024x768 on an Intel Core 2 Duo with Intel graphics? (ONLY drawing counts, consider the arrays to be ready in RAM; no precise prognosis needed)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

恋竹姑娘 2024-07-20 05:58:57

绘制颜色三元组的二维数组的最快方法:

  1. 使用浮点(不是字节,不是双精度)存储。 每个三元组由 3 个浮点数组成,每个浮点数的值从 0.0 到 1.0。 这是 GPU 实现的最佳格式(但是当您不需要色调时,请使用灰度 GL_LUMINANCE 存储 - 速度更快!)
  2. glTexImage2D 将数组上传到纹理
  3. 使用 确保 GL_TEXTURE_MIN_FILTER 纹理参数设置为 GL_NEAREST
  4. 将纹理映射到适当的四边形。

此方法比 glDrawPixels 稍快(由于某种原因往往实现得很糟糕),并且比使用平台的本机位块传输快很多

此外,当您的像素图未更改时,它还允许您重复执行步骤 4 而无需执行步骤 2,这当然要快得多。

仅提供慢速本机 blitting 的库包括:

  • X11 上的Windows GDI
  • SDL(在 Windows 上,使用 HW_SURFACE 时,它提供快速 opengl 后端)
  • Qt

至于您可以预期的 FPS,在具有 Intel 显卡的 Intel Core 2 Duo 上绘制 1024x768 纹理:如果纹理每帧都发生变化,则约为 60FPS;如果没有,则 >100FPS。

但你自己做一下看看吧;)

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 ;)

深者入戏 2024-07-20 05:58:57

我不久前使用 C 和 OpenGL 完成了此操作,通过创建全屏大小的四边形,然后使用纹理映射将位图传输到四边形的表面,获得了非常好的性能。

下面是一些示例代码,希望您能使用。

#include <GL/glut.h>
#include <GL/glut.h>

#define WIDTH 1024
#define HEIGHT 768

unsigned char texture[WIDTH][HEIGHT][3];             

void renderScene() {    

    // render the texture here

    glEnable (GL_TEXTURE_2D);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

    glTexImage2D (
        GL_TEXTURE_2D,
        0,
        GL_RGB,
        WIDTH,
        HEIGHT,
        0,
        GL_RGB,
        GL_UNSIGNED_BYTE,
        &texture[0][0][0]
    );

    glBegin(GL_QUADS);
        glTexCoord2f(0.0f, 0.0f); glVertex2f(-1.0, -1.0);
        glTexCoord2f(1.0f, 0.0f); glVertex2f( 1.0, -1.0);
        glTexCoord2f(1.0f, 1.0f); glVertex2f( 1.0,  1.0);
        glTexCoord2f(0.0f, 1.0f); glVertex2f(-1.0,  1.0);
    glEnd();

    glFlush();
    glutSwapBuffers();
}

int main(int argc, char **argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);

    glutInitWindowPosition(100, 100);
    glutInitWindowSize(WIDTH, HEIGHT);
    glutCreateWindow(" ");

    glutDisplayFunc(renderScene);

    glutMainLoop();

    return 0;
}

I did this a while back using C and OpenGL, and got very good performance by creating a full screen sized quad, and then use texture mapping to transfer the bitmap onto the face of the quad.

Here's some example code, hope you can make use of it.

#include <GL/glut.h>
#include <GL/glut.h>

#define WIDTH 1024
#define HEIGHT 768

unsigned char texture[WIDTH][HEIGHT][3];             

void renderScene() {    

    // render the texture here

    glEnable (GL_TEXTURE_2D);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

    glTexImage2D (
        GL_TEXTURE_2D,
        0,
        GL_RGB,
        WIDTH,
        HEIGHT,
        0,
        GL_RGB,
        GL_UNSIGNED_BYTE,
        &texture[0][0][0]
    );

    glBegin(GL_QUADS);
        glTexCoord2f(0.0f, 0.0f); glVertex2f(-1.0, -1.0);
        glTexCoord2f(1.0f, 0.0f); glVertex2f( 1.0, -1.0);
        glTexCoord2f(1.0f, 1.0f); glVertex2f( 1.0,  1.0);
        glTexCoord2f(0.0f, 1.0f); glVertex2f(-1.0,  1.0);
    glEnd();

    glFlush();
    glutSwapBuffers();
}

int main(int argc, char **argv) {
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);

    glutInitWindowPosition(100, 100);
    glutInitWindowSize(WIDTH, HEIGHT);
    glutCreateWindow(" ");

    glutDisplayFunc(renderScene);

    glutMainLoop();

    return 0;
}
骑趴 2024-07-20 05:58:57

如果您尝试将像素转储到屏幕,您可能需要使用 sdl
“表面”设施。 为了获得最佳性能,请尝试将输入数据安排为与输出表面类似的布局。 如果可能,请避免一次在表面上设置一个像素。

SDL 本身并不是一个硬件接口,而是一个可移植层,可以在许多其他显示层(包括 DirectX、OpenGL、DirectFB 和 xlib)之上很好地工作,因此您可以获得非常好的可移植性,而且它是一个非常薄的层基于这些技术,因此您只需支付很少的性能开销。

If you're trying to dump pixels to screen, you'll probably want to make use of sdl's
'surface' facuility. For the greatest performance, try to arrange for the input data to be in a similar layout to the output surface. If possible, steer clear of setting pixels in the surface one at a time.

SDL is not a hardware interface in its own right, but rather a portability layer that works well on top of many other display layers, including DirectX, OpenGL, DirectFB, and xlib, so you get very good portability, and its a very thin layer on top of those technologies, so you pay very little performance overhead on top of those.

牵强ㄟ 2024-07-20 05:58:57

除了 SDL 之外的其他选项(如上所述)

  • Cairo 表面带有浮华(C 语言,适用于所有平台)但最好在 Linux 中)
  • QT Canvas(C++ 中,多平台)
  • OpenGL 原始 API或 QT OpenGL (你需要了解 openGL)
  • 纯 Xlib/XCB 如果你想考虑非 opengl 平台

我的建议

  1. QT 如果你喜欢 C++
  2. Cairo 如果你喜欢 C

Other options apart from SDL (as mentioned)

  • Cairo surfaces with glitz (in C, works on all plaforms but best in Linux)
  • QT Canvas (in C++, multiplaform)
  • OpenGL raw API or QT OpenGL (You need to know openGL)
  • pure Xlib/XCB if you want to take into account non-opengl plaforms

My suggestion

  1. QT if you prefer C++
  2. Cairo if you prefer C
北陌 2024-07-20 05:58:57

“我可以期望多少帧速率”这个问题无法得到认真的回答。 即使你说出进行处理器布局的人的祖父的名字也不会。 这取决于太多的变量。

  • 您需要渲染多少个三元组?
  • 它们在帧之间变化吗?
  • 以什么速率(如果频率超过每秒 30 次,您将不会注意到变化)?
  • 是所有像素始终发生变化还是仅某些区域的部分像素发生变化?
  • 您查看的像素是否没有任何透视变形?
  • 你总是能看到所有像素吗?
  • 根据 opengl 驱动程序的版本,您将得到不同的结果,

这可能会永远持续下去,答案绝对取决于您的算法。 如果您坚持使用 opengl 方法,您还可以尝试不同的扩展(http://例如,我想到了www.opengl.org/registry/specs/NV/pixel_data_range.txt),看看它是否更适合您的需求; 尽管已经提到的 glTexSubImage() 方法相当快。

the "how many fps can i expect" question can not be answered seriously. not even if you name the grandpa of the guy who did the processor layouting. it depends on tooooo many variables.

  • how many triplets do you need to render?
  • do they change between the frames?
  • at which rate (you wont notice the change if its more often than 30times a sec)?
  • do all of the pixels changes all of the time or just some of the pixels in some areas?
  • do you look at the pixels without any perspective distortion?
  • do you always see all the pixels?
  • depending on the version of the opengl driver you will get different results

this could go on for ever, the answer depends absolutly on your algorithm. if you stick to the opengl approach you could also try different extensions (http://www.opengl.org/registry/specs/NV/pixel_data_range.txt comes to mind for example), to see if it fits your needs better; although the already mentioned glTexSubImage() method is quite fast.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文