Skip to content

Commit 7ae2f2f

Browse files
committed
SDL3_GPU: Measure frame times & export to file
1 parent 5c27e84 commit 7ae2f2f

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

SDL3_GPU/src/main.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ struct MyAppState {
8787
SDL_GPUTexture* texture = nullptr;
8888
SDL_GPUSampler* sampler = nullptr;
8989
MyMesh* mesh = nullptr;
90+
Uint32 frameNumber = 0;
91+
std::array<Uint32, 10'000> frameTimes = {};
9092
};
9193

9294
SDL_GPUShader* LoadShader(
@@ -279,6 +281,28 @@ SDL_AppResult SDL_AppInit(void** appstate, [[maybe_unused]] int argc, [[maybe_un
279281
return SDL_APP_FAILURE;
280282
}
281283

284+
SDL_GPUPresentMode presentMode = SDL_GPU_PRESENTMODE_VSYNC;
285+
if (SDL_WindowSupportsGPUPresentMode(myAppState->device, myAppState->window, SDL_GPU_PRESENTMODE_IMMEDIATE)) {
286+
presentMode = SDL_GPU_PRESENTMODE_IMMEDIATE;
287+
} else if (SDL_WindowSupportsGPUPresentMode(myAppState->device, myAppState->window, SDL_GPU_PRESENTMODE_MAILBOX)) {
288+
presentMode = SDL_GPU_PRESENTMODE_MAILBOX;
289+
}
290+
if (!SDL_SetGPUSwapchainParameters(myAppState->device, myAppState->window, SDL_GPU_SWAPCHAINCOMPOSITION_SDR, presentMode)) {
291+
SDL_Log("Couldn't set swapchain parameters: %s", SDL_GetError());
292+
return SDL_APP_FAILURE;
293+
}
294+
switch (presentMode) {
295+
case SDL_GPU_PRESENTMODE_VSYNC:
296+
SDL_Log("Using present mode: VSYNC");
297+
break;
298+
case SDL_GPU_PRESENTMODE_IMMEDIATE:
299+
SDL_Log("Using present mode: IMMEDIATE");
300+
break;
301+
case SDL_GPU_PRESENTMODE_MAILBOX:
302+
SDL_Log("Using present mode: MAILBOX");
303+
break;
304+
}
305+
282306
// Load mesh
283307
// std::optional<MyMesh> oMesh = ImportMesh("models/container/blender_quad.obj");
284308
std::optional<MyMesh> oMesh = ImportMesh("models/suzanne/suzanne.obj");
@@ -516,6 +540,7 @@ SDL_AppResult SDL_AppEvent([[maybe_unused]] void* appstate, SDL_Event* event) {
516540
SDL_AppResult SDL_AppIterate(void* appstate) {
517541
MyAppState* myAppState = static_cast<MyAppState*>(appstate);
518542

543+
std::chrono::high_resolution_clock::time_point startTime = std::chrono::high_resolution_clock::now();
519544
SDL_GPUCommandBuffer* commandBuffer = SDL_AcquireGPUCommandBuffer(myAppState->device);
520545
if (commandBuffer == nullptr) {
521546
SDL_Log("Couldn't AcquireGPUCommandBuffer: %s", SDL_GetError());
@@ -608,6 +633,16 @@ SDL_AppResult SDL_AppIterate(void* appstate) {
608633
return SDL_APP_FAILURE;
609634
}
610635

636+
std::chrono::high_resolution_clock::time_point endTime = std::chrono::high_resolution_clock::now();
637+
std::chrono::duration<Uint32, std::nano> frameTime = endTime - startTime;
638+
myAppState->frameTimes[myAppState->frameNumber] = frameTime.count();
639+
640+
myAppState->frameNumber++;
641+
if (myAppState->frameNumber >= myAppState->frameTimes.size()) {
642+
return SDL_APP_SUCCESS;
643+
}
644+
645+
SDL_Log("Frame: %u, Time: %u ns", myAppState->frameNumber, myAppState->frameTimes[myAppState->frameNumber - 1]);
611646
return SDL_APP_CONTINUE;
612647
}
613648

@@ -621,5 +656,18 @@ void SDL_AppQuit(void* appstate, [[maybe_unused]] SDL_AppResult result) {
621656
SDL_ReleaseGPUTexture(myAppState->device, myAppState->depthTexture);
622657
SDL_ReleaseGPUSampler(myAppState->device, myAppState->sampler);
623658

659+
if (SDL_IOStream* fileStream = SDL_IOFromFile(QUOTE(MYPROJECT_NAME) "_frameTimes.txt", "w");
660+
fileStream == nullptr) {
661+
SDL_Log("Couldn't open file for writing: %s", SDL_GetError());
662+
} else {
663+
for (const Uint32 frameTime : myAppState->frameTimes) {
664+
std::string frameTimeStr = std::to_string(frameTime) + "\n";
665+
SDL_WriteIO(fileStream, frameTimeStr.c_str(), frameTimeStr.size());
666+
}
667+
if (!SDL_CloseIO(fileStream)) {
668+
SDL_Log("Couldn't close file: %s", SDL_GetError());
669+
}
670+
}
671+
624672
delete myAppState;
625673
}

0 commit comments

Comments
 (0)