gpt4 book ai didi

c++ - SDL2 : Fast Pixel Manipulation

转载 作者:IT老高 更新时间:2023-10-28 23:18:42 28 4
gpt4 key购买 nike

我想在显示器上绘制在某些参数后经常变化的像素。例如。如果红色和绿色像素碰撞,它们都会消失,等等。

在每一帧中,我必须处理大约 100 - 1000 个像素。我在这里有一个多线程方法,它不会给我 30FPS(我想要的)。目前我在 RAM 中存储了一个像素数组,其中包含所有像素并有一个 SDL_Surface。当数组中的一个像素发生变化时,它也会在 Surface 中发生变化,然后在所有操作完成后被 blitted 到屏幕上。我目前的方法太慢了,我考虑了如何提高速度。

我目前的想法是:

  • 使用 OpenGL 直接在 GPU 上进行像素操作,一些论坛告诉我,这比我目前的方法慢得多,因为“这不是 GPU 的工作方式”
  • 不要存储像素阵列,直接将 BMP 存储在 RAM 中,对其进行操作,然后将其移动到 SDL_SurfaceSDL_Texture

还有其他方法可以快速操作像素吗?

最佳答案

SDL_CreateTexture() w/SDL_TEXTUREACCESS_STREAMING + SDL_UpdateTexture() 似乎在正确的像素格式下工作得很好。

在我的系统上使用默认渲染器:

Renderer name: direct3d
Texture formats:
SDL_PIXELFORMAT_ARGB8888
SDL_PIXELFORMAT_YV12
SDL_PIXELFORMAT_IYUV

(虽然 opengl 信息是一样的:)

Renderer name: opengl
Texture formats:
SDL_PIXELFORMAT_ARGB8888
SDL_PIXELFORMAT_YV12
SDL_PIXELFORMAT_IYUV

SDL_PIXELFORMAT_ARGB8888 给我约 1 毫秒/帧:

// g++ main.cpp `pkg-config --cflags --libs sdl2`
#include <SDL.h>
#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <chrono>

void PrintFrameTiming(std::ostream& os = std::cout, float period = 2.0f)
{
static unsigned int frames = 0;
frames++;
static auto start = std::chrono::steady_clock::now();
auto end = std::chrono::steady_clock::now();

float seconds = std::chrono::duration_cast< std::chrono::duration<float> >(end - start).count();
if( seconds > period )
{
float spf = seconds / frames;
os
<< frames << " frames in "
<< std::setprecision(1) << std::fixed << seconds << " seconds = "
<< std::setprecision(1) << std::fixed << 1.0f / spf << " FPS ("
<< std::setprecision(3) << std::fixed << spf * 1000.0f << " ms/frame)\n";
frames = 0;
start = end;
}
}

int main( int, char** )
{
SDL_Init( SDL_INIT_EVERYTHING );
SDL_Window* window = SDL_CreateWindow( "SDL", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 600, 600, SDL_WINDOW_SHOWN );
SDL_Renderer* renderer = SDL_CreateRenderer( window, -1, SDL_RENDERER_ACCELERATED );
SDL_SetHint( SDL_HINT_RENDER_SCALE_QUALITY, "1" );

// dump renderer info
SDL_RendererInfo info;
SDL_GetRendererInfo( renderer, &info );
std::cout << "Renderer name: " << info.name << '\n';
std::cout << "Texture formats: " << '\n';
for( Uint32 i = 0; i < info.num_texture_formats; i++ )
{
std::cout << SDL_GetPixelFormatName( info.texture_formats[i] ) << '\n';
}

// create texture
const unsigned int texWidth = 1024;
const unsigned int texHeight = 1024;
SDL_Texture* texture = SDL_CreateTexture( renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, texWidth, texHeight );
std::vector< unsigned char > pixels( texWidth * texHeight * 4, 0 );

bool useLocktexture = false;

// main loop
bool running = true;
while( running )
{
SDL_SetRenderDrawColor( renderer, 0, 0, 0, SDL_ALPHA_OPAQUE );
SDL_RenderClear( renderer );

// handle events
SDL_Event ev;
while( SDL_PollEvent( &ev ) )
{
if( ( SDL_QUIT == ev.type ) ||
( SDL_KEYDOWN == ev.type && SDL_SCANCODE_ESCAPE == ev.key.keysym.scancode ) )
{
running = false;
break;
}

if( SDL_KEYDOWN == ev.type && SDL_SCANCODE_L == ev.key.keysym.scancode )
{
useLocktexture = !useLocktexture;
std::cout << "Using " << ( useLocktexture ? "SDL_LockTexture() + std::copy_n()" : "SDL_UpdateTexture()" ) << '\n';
}
}

// splat down some random pixels
for( unsigned int i = 0; i < 1000; i++ )
{
const unsigned int x = rand() % texWidth;
const unsigned int y = rand() % texHeight;

const unsigned int offset = ( texWidth * y * 4 ) + x * 4;
pixels[ offset + 0 ] = rand() % 256; // b
pixels[ offset + 1 ] = rand() % 256; // g
pixels[ offset + 2 ] = rand() % 256; // r
pixels[ offset + 3 ] = SDL_ALPHA_OPAQUE; // a
}

// update texture
if( useLocktexture )
{
unsigned char* lockedPixels = nullptr;
int pitch = 0;
SDL_LockTexture( texture, nullptr, reinterpret_cast< void** >( &lockedPixels ), &pitch );
std::copy_n( pixels.data(), pixels.size(), lockedPixels );
SDL_UnlockTexture( texture );
}
else
{
SDL_UpdateTexture( texture, nullptr, pixels.data(), texWidth * 4 );
}

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

PrintFrameTiming();
}

SDL_DestroyRenderer( renderer );
SDL_DestroyWindow( window );
SDL_Quit();

return 0;
}

确保您没有启用 vsync(在驱动程序中强制执行、运行合成器等),否则 所有您的帧时间将约为 16 毫秒(或者您的显示刷新是什么)设置为)。

关于c++ - SDL2 : Fast Pixel Manipulation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33304351/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com