Skip to content

Commit 44a66ac

Browse files
committed
Vulkan_Helpers: Measure frame times & export to file, too
1 parent 56e6fdf commit 44a66ac

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

Vulkan_Helpers/src/vk_engine.cpp

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ SDL_AppResult VulkanEngine::CreateSwapchain(const uint32_t width, const uint32_t
199199
vkb::Result<vkb::Swapchain> retVkbSwapchain = swapchainBuilder
200200
// .use_default_format_selection()
201201
.set_desired_format(desiredFormat)
202-
.set_desired_present_mode(VK_PRESENT_MODE_FIFO_RELAXED_KHR)
202+
.set_desired_present_mode(VK_PRESENT_MODE_IMMEDIATE_KHR)
203203
.set_desired_extent(width, height)
204204
.add_image_usage_flags(VK_IMAGE_USAGE_TRANSFER_DST_BIT)
205205
.build();
@@ -861,6 +861,8 @@ void VulkanEngine::DestroyImage(const AllocatedImage& allocatedImage) const {
861861
}
862862

863863
SDL_AppResult VulkanEngine::Draw() {
864+
const std::chrono::high_resolution_clock::time_point startTime = std::chrono::high_resolution_clock::now();
865+
864866
if (resizeRequested) {
865867
if (const SDL_AppResult res = ResizeSwapchain(); res != SDL_APP_CONTINUE) {
866868
return res;
@@ -952,7 +954,14 @@ SDL_AppResult VulkanEngine::Draw() {
952954
return SDL_APP_FAILURE;
953955
}
954956

957+
const std::chrono::high_resolution_clock::time_point endTime = std::chrono::high_resolution_clock::now();
958+
const std::chrono::duration<Uint32, std::nano> frameTime = endTime - startTime;
959+
frameTimes[frameNumber] = frameTime.count();
960+
955961
frameNumber++;
962+
if (frameNumber >= frameTimes.size()) {
963+
return SDL_APP_SUCCESS;
964+
}
956965

957966
return SDL_APP_CONTINUE;
958967
}
@@ -1008,4 +1017,24 @@ void VulkanEngine::Cleanup(const SDL_AppResult result) {
10081017

10091018
SDL_DestroyWindow(window);
10101019
}
1020+
1021+
if (SDL_IOStream* fileStream = SDL_IOFromFile((name + "_frameTimes.txt").c_str(), "w");
1022+
fileStream == nullptr) {
1023+
SDL_Log("Couldn't open file for writing: %s", SDL_GetError());
1024+
} else {
1025+
for (const Uint32 frameTime : frameTimes) {
1026+
std::string frameTimeStr = std::to_string(frameTime) + "\n";
1027+
SDL_WriteIO(fileStream, frameTimeStr.c_str(), frameTimeStr.size());
1028+
}
1029+
if (!SDL_CloseIO(fileStream)) {
1030+
SDL_Log("Couldn't close file: %s", SDL_GetError());
1031+
}
1032+
}
1033+
1034+
Uint64 averageFrameTime = 0;
1035+
for (const Uint32 frameTime : frameTimes) {
1036+
averageFrameTime += frameTime;
1037+
}
1038+
averageFrameTime /= frameTimes.size();
1039+
SDL_Log("Average frame time over %d frames was: %d ns", frameNumber, averageFrameTime);
10111040
}

Vulkan_Helpers/src/vk_engine.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,9 @@ class VulkanEngine {
3838
DescriptorAllocatorGrowable frameDescriptors;
3939
};
4040

41-
unsigned int frameNumber = 0;
41+
Uint32 frameNumber = 0;
42+
std::array<Uint32, FRAME_NUMBERS> frameTimes = {};
43+
4244
std::array<FrameData, 2> frames = {};
4345
FrameData& GetCurrentFrame() { return frames[frameNumber % frames.size()]; }
4446
VkQueue graphicsQueue = nullptr;

0 commit comments

Comments
 (0)