SDL3 入门(4):挑选图形引擎
SDL2 创立烘托器时只能指定运用软件烘托仍是硬件加速,无法挑选运用哪种图形引擎完成硬件加速。SDL3 对此做了优化,能够在创立烘托器时指定 rendering driver 也便是图形引擎,比如在 Windows 平台下能够指定运用 D3D11 也能够指定运用 OpenGL 或许 Vulkan。
指定图形引擎
SDL_CreateRenderer
函数的第二个参数 name
表明指定运用的 rendering driver name,传 NULL
表明运用第一个支撑的 rendering driver,在 Windows 体系下通常是 D3D11。
SDL_Renderer * SDLCALL SDL_CreateRenderer(SDL_Window *window, const char *name)
SDL3 接口文件中没有预界说 rendering driver name,能够经过 SDL_GetNumRenderDrivers
和 SDL_GetRenderDriver
两个函数枚举当前所支撑的图形引擎:
int count = SDL_GetNumRenderDrivers();
for (int i = 0; i < count; ++i) {
const char* name = SDL_GetRenderDriver(i);
SDL_Log("Render driver[%d]: %s", i, name);
}
在 Windows 体系下履行成果如下:
INFO: Render driver[0]: direct3d11
INFO: Render driver[1]: direct3d12
INFO: Render driver[2]: direct3d
INFO: Render driver[3]: opengl
INFO: Render driver[4]: opengles2
INFO: Render driver[5]: vulkan
INFO: Render driver[6]: software
其间 direct3d 指的是 D3D9,software 指软件烘托,咱们能够经过这些姓名指定烘托器运用的烘托引擎。
烘托功能测验
完成一个简略的类用来测验烘托帧率
// performance.h
...
class Performance final
{
public:
Performance();
~Performance();
void Reset();
void IncreaseFrameCount();
void PrintEverySecond();
private:
using Clock = std::chrono::high_resolution_clock;
using TimePoint = std::chrono::time_point<Clock>;
TimePoint start_time_;
uint64_t frame_count_ = 0;
TimePoint last_print_time_;
uint64_t last_frame_count_ = 0;
};
// performance.cpp
...
Performance::Performance()
{
Reset();
}
Performance::~Performance() {}
void Performance::Reset()
{
start_time_ = Clock::now();
last_print_time_ = start_time_;
frame_count_ = 0;
}
void Performance::IncreaseFrameCount()
{
frame_count_++;
}
void Performance::PrintEverySecond()
{
assert(start_time_.time_since_epoch().count() > 0);
assert(last_print_time_ >= start_time_);
auto now = Clock::now();
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(now - last_print_time_);
if (duration.count() >= 1000) {
double elapsed_seconds =
std::chrono::duration_cast<std::chrono::milliseconds>(now - start_time_).count() /
1000.0;
double average_fps = frame_count_ / elapsed_seconds;
double realtime_fps = (frame_count_ - last_frame_count_) / (duration.count() / 1000.0);
last_print_time_ = now;
last_frame_count_ = frame_count_;
fprintf(stderr, "Performance: FPS(AVR|RT): %.2f|%.2f \r", average_fps, realtime_fps);
}
}
咱们先测验 D3D11 的烘托帧率:
INFO: Created renderer: direct3d11
INFO: VSync: 0
Performance: FPS(AVR|RT): 32805.16|34470.00
3 万多帧,还不错。这是全力烘托的成果,没有敞开笔直同步,能够调用 SDL_SetRenderVSync
函数设置笔直同步:
int vsync = disable_vsync ? 0 : 1;
SDL_SetRenderVSync(renderer, vsync);
SDL_Log("VSync: %d", vsync);
再次测验烘托帧率,60fps,和屏幕刷新率共同:
INFO: Created renderer: direct3d11
INFO: VSync: 1
Performance: FPS(AVR|RT): 59.95|60.04
封闭笔直同步,对各烘托引擎别离进行帧率测验,成果如下:
direct3d11 | direct3d12 | direct3d | opengl | opengles2 | vulkan | software |
---|---|---|---|---|---|---|
33113.44 | 1155.43 | 1729.66 | 1673.66 | 1716.95 | 1565.44 | 1668.42 |
D3D11 一骑绝尘,看来优化的不错。
留意这仅仅一个简略的测验,功能瓶颈首要在从 CPU 提交烘托指令到 GPU 的进程,所以不代表 D3D11 的烘托功能和其他图形引擎真的有这么大的距离。实际上关于杂乱的图形烘托,除软件烘托外一切根据 GPU 的烘托功能上不会有太大的距离。