Add FPS counter
Added console FPS counter. Thise required me to create some time functions and remove FPS cap from Vulkan Signed-off-by: Piotr Krygier <piotrkrygier@everyonecancode@xyz>
This commit is contained in:
@@ -6,7 +6,6 @@
|
|||||||
#include <cglm/util.h>
|
#include <cglm/util.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <time.h>
|
|
||||||
#include <vulkan/vulkan_core.h>
|
#include <vulkan/vulkan_core.h>
|
||||||
|
|
||||||
#include "descriptor_builder.h"
|
#include "descriptor_builder.h"
|
||||||
@@ -18,6 +17,7 @@
|
|||||||
#include "src/vulkan_buffers.h"
|
#include "src/vulkan_buffers.h"
|
||||||
#include "utilities/commons.h"
|
#include "utilities/commons.h"
|
||||||
#include "utilities/errors_common.h"
|
#include "utilities/errors_common.h"
|
||||||
|
#include "utilities/time_utils.h"
|
||||||
#include "vulkan_base.h"
|
#include "vulkan_base.h"
|
||||||
#include "vulkan_commons.h"
|
#include "vulkan_commons.h"
|
||||||
#include "vulkan_image.h"
|
#include "vulkan_image.h"
|
||||||
@@ -38,9 +38,6 @@ static void model_view_projection_update(struct graphics_context_t* context)
|
|||||||
float fovy;
|
float fovy;
|
||||||
float z_near;
|
float z_near;
|
||||||
float z_far;
|
float z_far;
|
||||||
double diff_time = 0.0;
|
|
||||||
struct timespec now = {0};
|
|
||||||
static struct timespec last_time = {0};
|
|
||||||
|
|
||||||
eye[2] = -5.0f;
|
eye[2] = -5.0f;
|
||||||
|
|
||||||
@@ -50,17 +47,9 @@ static void model_view_projection_update(struct graphics_context_t* context)
|
|||||||
z_near = 0.1f;
|
z_near = 0.1f;
|
||||||
z_far = 20.0f;
|
z_far = 20.0f;
|
||||||
|
|
||||||
if (last_time.tv_nsec == 0) {
|
|
||||||
timespec_get(&last_time, TIME_UTC);
|
|
||||||
}
|
|
||||||
|
|
||||||
timespec_get(&now, TIME_UTC);
|
|
||||||
|
|
||||||
diff_time = NS_TO_S((double)(now.tv_nsec - last_time.tv_nsec)) + (now.tv_sec - last_time.tv_sec);
|
|
||||||
|
|
||||||
glm_mat4_identity(context->mvp_data.model);
|
glm_mat4_identity(context->mvp_data.model);
|
||||||
|
|
||||||
glm_rotate(context->mvp_data.model, diff_time * glm_rad(90.0f), rotation_vec);
|
glm_rotate(context->mvp_data.model, time_get_sec_from_start() * glm_rad(90.0f), rotation_vec);
|
||||||
glm_lookat(eye, center, up, context->mvp_data.view);
|
glm_lookat(eye, center, up, context->mvp_data.view);
|
||||||
glm_perspective(
|
glm_perspective(
|
||||||
fovy,
|
fovy,
|
||||||
|
|||||||
@@ -52,7 +52,7 @@ static rse_err_t create_swapchain(struct graphics_context_t* context)
|
|||||||
create_info.pQueueFamilyIndices = context->queue_family_indices;
|
create_info.pQueueFamilyIndices = context->queue_family_indices;
|
||||||
create_info.preTransform = physical_device_surface_capabilities.currentTransform;
|
create_info.preTransform = physical_device_surface_capabilities.currentTransform;
|
||||||
create_info.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
|
create_info.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
|
||||||
create_info.presentMode = VK_PRESENT_MODE_FIFO_KHR;
|
create_info.presentMode = VK_PRESENT_MODE_IMMEDIATE_KHR;
|
||||||
create_info.clipped = VK_TRUE;
|
create_info.clipped = VK_TRUE;
|
||||||
create_info.oldSwapchain = VK_NULL_HANDLE;
|
create_info.oldSwapchain = VK_NULL_HANDLE;
|
||||||
|
|
||||||
|
|||||||
@@ -20,9 +20,25 @@
|
|||||||
|
|
||||||
#include "utilities/commons.h"
|
#include "utilities/commons.h"
|
||||||
#include "utilities/errors_common.h"
|
#include "utilities/errors_common.h"
|
||||||
|
#include "utilities/time_utils.h"
|
||||||
#include "vulkan/vulkan_core.h"
|
#include "vulkan/vulkan_core.h"
|
||||||
#include "vulkan_base.h"
|
#include "vulkan_base.h"
|
||||||
|
|
||||||
|
static void fps_print(double* start_time)
|
||||||
|
{
|
||||||
|
static uint32_t frame_counts = 0U;
|
||||||
|
|
||||||
|
double now = time_get_sec_from_start();
|
||||||
|
|
||||||
|
frame_counts++;
|
||||||
|
if ((now - *start_time) > 1.0) {
|
||||||
|
printf("\rFPS: %u", frame_counts);
|
||||||
|
fflush(stdout);
|
||||||
|
frame_counts = 0;
|
||||||
|
*start_time += 1.0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
rse_err_t window_init(SDL_Window** window_handle, bool* is_framebuffer_resized)
|
rse_err_t window_init(SDL_Window** window_handle, bool* is_framebuffer_resized)
|
||||||
{
|
{
|
||||||
SDL_Init(SDL_INIT_VIDEO); // TODO: Move to initial phase of RSE initialization
|
SDL_Init(SDL_INIT_VIDEO); // TODO: Move to initial phase of RSE initialization
|
||||||
@@ -43,6 +59,9 @@ rse_err_t window_loop(struct graphics_context_t* context)
|
|||||||
{
|
{
|
||||||
uint8_t done = 0;
|
uint8_t done = 0;
|
||||||
SDL_Event event;
|
SDL_Event event;
|
||||||
|
double start_time;
|
||||||
|
|
||||||
|
start_time = time_get_sec_from_start();
|
||||||
|
|
||||||
while (0 == done) {
|
while (0 == done) {
|
||||||
while (SDL_PollEvent(&event)) {
|
while (SDL_PollEvent(&event)) {
|
||||||
@@ -52,6 +71,7 @@ rse_err_t window_loop(struct graphics_context_t* context)
|
|||||||
}
|
}
|
||||||
|
|
||||||
draw_frame(context);
|
draw_frame(context);
|
||||||
|
fps_print(&start_time);
|
||||||
}
|
}
|
||||||
|
|
||||||
vkDeviceWaitIdle(context->vulkan_handles.device);
|
vkDeviceWaitIdle(context->vulkan_handles.device);
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
rse_utilities_srcs = [
|
rse_utilities_srcs = [
|
||||||
'src/file_utils.c'
|
'src/file_utils.c',
|
||||||
|
'src/time_utils.c'
|
||||||
]
|
]
|
||||||
|
|
||||||
cc = meson.get_compiler('c')
|
cc = meson.get_compiler('c')
|
||||||
|
|||||||
@@ -0,0 +1,9 @@
|
|||||||
|
#include "time_utils.h"
|
||||||
|
#include <stdint.h>
|
||||||
|
#include "SDL3/SDL_timer.h"
|
||||||
|
|
||||||
|
#define NS_TO_S(time) (time/1000000000)
|
||||||
|
double time_get_sec_from_start(void)
|
||||||
|
{
|
||||||
|
return NS_TO_S((double)SDL_GetTicksNS());
|
||||||
|
}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
/**
|
||||||
|
* @file file_utils.h
|
||||||
|
* @author Piotr Krygier (everyonecancode@gmail.com)
|
||||||
|
* @brief Common functionality for all modules
|
||||||
|
* @version 0.1
|
||||||
|
* @date 2026-02-11
|
||||||
|
*
|
||||||
|
* @copyright Copyright (c) 2023
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef TIME_UTILS_H_
|
||||||
|
#define TIME_UTILS_H_
|
||||||
|
|
||||||
|
double time_get_sec_from_start(void);
|
||||||
|
|
||||||
|
#endif // !TIME_UTILS_H_
|
||||||
Reference in New Issue
Block a user