SFML 怎么这么快?

Tom*_*mas 4 c++ pixel sfml

我需要在窗口上逐个像素地用 C++ 绘制一些图形。为了做到这一点,我创建了一个 SFML 窗口、精灵和纹理。我将所需的图形绘制到 uint8_t 数组,然后用它更新纹理和精灵。这个过程大约需要2500 us。绘制两个填充整个窗口的三角形只需要10 us。这种巨大的差异怎么可能?我已经尝试多线程逐像素绘制,但两个数量级的差异仍然存在。我还尝试使用点图绘制像素,但没有任何改进。我知道 SFML 在后台使用一些 GPU 加速,但简单地循环并将值分配给像素阵列已经需要数百微秒。

有谁知道在窗口中分配像素值的更有效方法?

这是我用来比较三角形和逐像素绘制速度的代码示例:

#include <SFML/Graphics.hpp>
#include <chrono>
using namespace std::chrono;
#include <iostream>
#include<cmath>

uint8_t* pixels;

int main(int, char const**)
{
    const unsigned int width=1200;
    const unsigned int height=1200;
    sf::RenderWindow window(sf::VideoMode(width, height), "MA: Rasterization Test");
    pixels = new uint8_t[width*height*4];
    sf::Texture pixels_texture;
    pixels_texture.create(width, height);
    sf::Sprite pixels_sprite(pixels_texture);
    sf::Clock clock;
    sf::VertexArray triangle(sf::Triangles, 3);

    triangle[0].position = sf::Vector2f(0, height);
    triangle[1].position = sf::Vector2f(width, height);
    triangle[2].position = sf::Vector2f(width/2, height-std::sqrt(std::pow(width,2)-std::pow(width/2,2)));
    triangle[0].color = sf::Color::Red;
    triangle[1].color = sf::Color::Blue;
    triangle[2].color = sf::Color::Green;
    
    
    while (window.isOpen()){
        sf::Event event;
        while (window.pollEvent(event)) {
            if (event.type == sf::Event::Closed) {
                window.close();
            }
            if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape) {
                window.close();
            }
        }

        window.clear(sf::Color(255,255,255,255));

        // Pixel-by-pixel
        int us = duration_cast< microseconds >(system_clock::now().time_since_epoch()).count();
        for(int i=0;i!=width*height*4;++i){
            pixels[i]=255;
        }
        pixels_texture.update(pixels);
        window.draw(pixels_sprite);
        int duration=duration_cast< microseconds >(system_clock::now().time_since_epoch()).count()-us;
        std::cout<<"Background: "<<duration<<" us\n";

        // Triangle
        us = duration_cast< microseconds >(system_clock::now().time_since_epoch()).count();
        window.draw(triangle);
         duration=duration_cast< microseconds >(system_clock::now().time_since_epoch()).count()-us;
        std::cout<<"Triangle: "<<duration<<" us\n";

        window.display();
    }
    return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)

小智 5

使用显卡在现代设备中绘制图形,绘制速度取决于您发送到图形内存的数据中有多少三角形。这就是为什么只画两个三角形很快。

正如您提到的多线程,如果您使用OpenGL(我不记得SFML使用什么,但应该是一样的),您认为您在绘制的基本上是将命令和数据发送到显卡,所以这里的多线程不是很有用,图形卡有自己的线程来执行此操作。

如果您对显卡的工作原理感到好奇,那么教程就是 您应该阅读书。

PS 当你编辑你的问题时,我猜持续时间 2500us vs 10us 是因为你 for 循环创建了一个纹理(即使纹理是纯白色背景)(和 for 循环,你可能需要在 for 循环之后开始计数) , 将纹理发送到显卡需要时间,而绘制三角形只发送几个点。尽管如此,我还是建议阅读教程,逐个像素地创建纹理可能会证明对 GPU 工作原理的误解。