c++ - 使用 SDL 逐像素绘图会导致性能不佳?

标签 c++ performance sdl

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

它有效,但我得到了大约 1fps。我犯了任何明显的错误吗?这是我正在执行的操作量(50 点,680x480 像素)的预期速度吗?

主要.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;
}

最佳答案

简介

找出性能问题的最佳方法是使用分析器,这样您就可以知道时间实际花在了哪里。您可以使部分代码非常快,但如果这不是瓶颈,那也无济于事。

有根据的猜测

有了图形,数字会迅速增加。 640×480×50单帧计算超过1500万次。这可能是问题所在。解决这个问题的方法是找到一种更好的算法,它可以用更少的计算完成同样的工作。根据你的描述,听起来你正在创建一个 Voronoi 图,所以你可以搜索看看是否有更快的方法来实现它。您可能会考虑使用空间分区结构,这可以帮助您找到最近的点,而无需每次都检查每个点的距离。

一般来说,sqrtpow 可能是昂贵的函数,所以这可能是问题所在。正如其他人在评论中指出的那样,您实际上并不需要 sqrt 只是为了找到最接近的。还不清楚您的编译器的优化器是否足够聪明,可以将您的 pow 调用转换为(更快的)乘法。您确实启用了优化器,对吧?

或者,一次设置一个像素可能不是一种非常有效的处理方式。对于许 multimap 形 API,这是您可以做的最慢的事情。但我并不特别了解 SDL,所以这可能是问题,也可能不是问题。在许多情况下,在普通内存中构建位图然后只发出一个图形 API 调用以立即传输完成的位图会更快。使用着色器程序在 GPU 上完成所有计算甚至更快,这样 GPU 就可以并行化计算,因此图像不必从系统内存传输到图形内存。

没有分析器?

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

关于c++ - 使用 SDL 逐像素绘图会导致性能不佳?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68168217/

相关文章:

c++ - Eratosthenes C++代码筛在连续运行中会加快速度-为什么?

mysql - 包括数据库在内的单元测试应用程序太慢

c++ - OpenGL:没有绘制

c++ - Qt Creator 的项目搞砸了

c++ - 在 for 循环中声明 vector

sql - 为什么我的不相关子查询这么慢?

c - SDL C 鼠标/键盘输出

c++ - 使用 C++ 将 ASCII 转换为 unsigned int 形式的十六进制

c++ - 这个计算是如何工作的?

c++ - SDL 项目 CMake 构建失败