SDL_CreateTexture()
使用 SDL_TEXTUREACCESS_STREAMING
来创建纹理,并配合正确的像素格式,似乎能够良好地工作。
在我的系统上使用默认渲染器:
Renderer name: direct3d
Texture formats:
SDL_PIXELFORMAT_ARGB8888
SDL_PIXELFORMAT_YV12
SDL_PIXELFORMAT_IYUV
< p >(尽管 < code > opengl 的信息是相同的:)
Renderer name: opengl
Texture formats:
SDL_PIXELFORMAT_ARGB8888
SDL_PIXELFORMAT_YV12
SDL_PIXELFORMAT_IYUV
SDL_PIXELFORMAT_ARGB8888
可以让我每帧运行大约 1 毫秒:
#include <SDL.h>
#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <chrono>
void UpdateFrameTiming( 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();
auto seconds = std::chrono::duration< float >( end - start ).count();
if( seconds >= period )
{
os
<< frames << " frames in "
<< std::setprecision( 1 ) << std::fixed << seconds << " seconds = "
<< std::setprecision( 1 ) << std::fixed << frames / seconds << " FPS ("
<< std::setprecision( 3 ) << std::fixed << seconds / frames * 1000.0 << " 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" );
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';
}
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;
bool running = true;
while( running )
{
SDL_SetRenderDrawColor( renderer, 0, 0, 0, SDL_ALPHA_OPAQUE );
SDL_RenderClear( renderer );
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';
}
}
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;
pixels[ offset + 1 ] = rand() % 256;
pixels[ offset + 2 ] = rand() % 256;
pixels[ offset + 3 ] = SDL_ALPHA_OPAQUE;
}
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 );
UpdateFrameTiming();
}
SDL_DestroyRenderer( renderer );
SDL_DestroyWindow( window );
SDL_Quit();
return 0;
}
确保您没有启用垂直同步(在驱动程序中强制启用、运行合成器等),否则所有帧时间都将为约16ms(或您的显示器刷新率设置为其他值)。
SDL_Texture
来提高性能。你尝试过吗?你已经对你的代码进行了性能分析吗? - SgaTEXTURE_STREAMING
的SDL_Texture
,然后锁定纹理,在获取到的像素数组上进行操作,完成后解锁它。 - Nidhoegger