SDL逐像素绘图导致性能差?

3

我正在测试SDL,并创建了以下测试项目以查看性能。我想在屏幕上随机生成一组点,然后对于每个帧中的每个像素,找到最近的点并将像素的颜色设置为该点的颜色。

它可以工作,但我只能得到大约1帧每秒的速度。我有没有犯任何明显的错误?对于我正在进行的操作数量(50个点,680x480像素),这是否是预期速度?

main.h:

#include <SDL2/SDL.h>
#include <stdio.h>
#include <iostream>
#include <iomanip>
#include <vector>
#include <cstring>
#include <cmath>

const int pointsN = 50;

union Color
{
    struct
    {
        uint8_t r, g, b, a;
    } color;
    uint32_t color_int;
};

struct Point
{
    int x;
    int y;
    Color color;
};

void set_pixel(SDL_Surface *surface, int x, int y, Uint32 pixel)
{
    Uint32 *const target_pixel = (Uint32 *)((Uint8 *)surface->pixels + y * surface->pitch + x * surface->format->BytesPerPixel);
    *target_pixel = pixel;
}

float dist(int x1, int y1, int x2, int y2)
{
    return sqrt(pow(x1 - x2, 2) + pow(y1 - y2, 2));
}

Point nearestPoint(struct Point (& points)[pointsN], int x, int y)
{
    float smallestSize = -1;
    float thisDist;
    int closestIndex;
    for (size_t i = 0; i < pointsN; i++)
    {
        thisDist = dist(x, y, points[i].x, points[i].y);
        if(thisDist < smallestSize || smallestSize == -1)
        {
            closestIndex = i;
            smallestSize = thisDist;
        }
            
        
    }
    return points[closestIndex];
}

main.cpp:

#include "main.h"

int main(int argv, char **args)
{
    if (SDL_Init(SDL_INIT_EVERYTHING) != 0)
    {
        printf("Error");
        return 0;
    }

    const int w = 680;
    const int h = 480;

    SDL_Window *window = SDL_CreateWindow("SDL2 Window",
                                          SDL_WINDOWPOS_CENTERED,
                                          SDL_WINDOWPOS_CENTERED,
                                          w, h,
                                          0);
    if (!window)
    {
        std::cout << "Failed to create window\n";
        return -1;
    }

    /*SDL_Surface *window_surface = SDL_GetWindowSurface(window);

    if (!window_surface)
    {
        std::cout << "Failed to get the surface from the window\n";
        return -1;
    }
    */

    SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);

    SDL_Texture *texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ABGR8888, SDL_TEXTUREACCESS_STREAMING, w, h);

    Uint32 pixels[w*h];

    Point points[pointsN];

    for (size_t i = 0; i < pointsN; i++)
    {
        Point newpoint;
        newpoint.x = rand() % w;
        newpoint.y = rand() % h;
        newpoint.color.color.r = rand() % 255;
        newpoint.color.color.g = rand() % 255;
        newpoint.color.color.b = rand() % 255;
        newpoint.color.color.a = 255;

        points[i] = newpoint;
    }

    SDL_Event e;
    bool keep_window_open = true;

    unsigned int frames = 0;
    Uint64 start = SDL_GetPerformanceCounter();

    while (keep_window_open)
    {

        SDL_SetRenderDrawColor(renderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
        SDL_RenderClear(renderer);

        while (SDL_PollEvent(&e) > 0)
        {
            switch (e.type)
            {
            case SDL_QUIT:
                keep_window_open = false;
                break;
            }
        }

        for (size_t y = 0; y < h; y++)
        {
            for (size_t x = 0; x < w; x++)
            {
                pixels[(w * y) + x] = nearestPoint(points, x, y).color.color_int;
            }
        }

        for (size_t i = 0; i < pointsN; i++)
        {
            points[i].x += (rand() % 50 + 1) - 25;
            points[i].y += (rand() % 50 + 1) - 25;
        }

        SDL_UpdateTexture(
            texture,
            NULL,
            pixels,
            w * sizeof(Uint32));

        SDL_RenderCopy(renderer, texture, NULL, NULL);
        SDL_RenderPresent(renderer);


        frames++;
        const Uint64 end = SDL_GetPerformanceCounter();
        const static Uint64 freq = SDL_GetPerformanceFrequency();
        const double seconds = (end - start) / static_cast<double>(freq);

        printf("FPS: %f\n", frames / seconds);
    }

    SDL_DestroyTexture(texture);
    SDL_DestroyRenderer(renderer);

    SDL_Quit();
    return 0;
}

1
每一帧都需要大量的工作。一个小的改进是在nearestPoint中不使用距离,而是使用距离的平方,这样可以避免计算平方根。使用(x1 - x2) * (x1 - x2)来计算它,而不是调用pow函数,并在nearestPoint中初始化局部变量并以i=1开始循环,这将简化条件。 - 1201ProgramAlarm
4
你是否开启了编译器优化?虽然代码不够高效,但它不应该以每秒1帧的速度运行(假设你使用的是一颗普通的处理器而非较旧的版本)。 - Jérôme Richard
1
在 Ryzen 5900x 上使用 -O2 大约可以获得 30 FPS。当我像 @1201ProgramAlarm 建议的那样将 sqrt() 替换为平方距离时,帧率跃升至约 65 FPS。 - genpfault
使用“nearestPoint”简化后,帧率约为77 FPS。 - genpfault
1
pointsN的大小是多少? - jwezorek
感谢您提供这些想法,已经大大提高了性能。 - AnzeBlaBla
1个回答

3

简介

解决性能问题的最佳方法是使用分析器,以便您知道时间实际上花在哪里。你可以让代码的一部分变得非常快,但如果那不是瓶颈,它是没有帮助的。

有根据的猜测

对于图形处理,数字很快就会增加。 640×480×50 对单个帧而言超过了1500万计算量。 这可能就是问题所在。 解决此问题的方法是找到一个更好的算法,可以用更少的计算来完成相同的工作。 根据您的描述,似乎您正在创建 Voronoi 图,因此您可以搜索看是否有更快的实现方法。 您可以考虑使用空间划分结构,这可以帮助您找到最近的点而无需每次都检查每一个点的距离。

一般来说,sqrtpow 可能是昂贵的函数,这可能就是问题所在。 如评论中其他人所指出的那样,并不需要使用 sqrt 来查找最接近的点。 同时也不清楚您的编译器优化器是否足够聪明,将 pow 调用转换为(更快的)乘法。 您已经启用了优化器,对吧?

或者,逐个设置像素可能不是做事情的一种非常有效的方式。 对于许多图形API来说,这是您可以做的最慢的事情。 但是我不知道 SDL 是否特别存在这个问题。 在许多情况下,更快的方法是在纯内存中构建位图,然后只发出一个图形API调用,立即传输完成的位图。 更快的方法是使用着色器程序在GPU上进行所有计算,以便GPU可以并行处理,并且图像不必从系统内存传输到图形内存。

没有分析器怎么办?

如果无法轻松获取分析器,则可以创建简单的实验来帮助确定瓶颈所在。 例如,编写一个将每个像素设置为相同颜色的程序(而不进行其他操作)。 这将告诉您即使计算可以瞬间完成也可以设置单个像素的速度有多快。 您可以尝试增加 pointsN 并绘制每帧的时间。 它是线性的、二次的还是更糟糕的呢?


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接