91 lines
2.4 KiB
C++
91 lines
2.4 KiB
C++
// =============================================
|
|
// Aster: global.h
|
|
// Copyright (c) 2020-2024 Anish Bhobe
|
|
// =============================================
|
|
|
|
#pragma once
|
|
|
|
#include "config.h"
|
|
#include "constants.h"
|
|
#include "logger.h"
|
|
|
|
#include <GLFW/glfw3.h>
|
|
#include <fmt/core.h>
|
|
#include <glm/glm.hpp>
|
|
#include <glm/gtc/matrix_transform.hpp>
|
|
#include <string>
|
|
|
|
#define VULKAN_HPP_ASSERT(expr) DEBUG_IF(!(expr), "Vulkan assert failed")
|
|
#include <vk_mem_alloc.h>
|
|
#include <vulkan/vulkan_raii.hpp>
|
|
|
|
#define CODE_LOC " @ " __FILE__ ":" VULKAN_HPP_STRINGIFY(__LINE__)
|
|
[[nodiscard]] inline bool failed(const vk::Result _result) {
|
|
return _result != vk::Result::eSuccess;
|
|
}
|
|
|
|
template <typename T>
|
|
concept IsVkEnum = requires(T _t) {
|
|
{ std::is_enum_v<T> };
|
|
{ vk::to_string(_t) } -> std::same_as<std::string>;
|
|
};
|
|
|
|
template <typename T>
|
|
requires IsVkEnum<T> [[nodiscard]] const char *to_cstr(const T &_val) {
|
|
static std::string buffer;
|
|
buffer = vk::to_string(_val);
|
|
return buffer.c_str();
|
|
}
|
|
|
|
// TODO: Check why inline namespaces aren't working in MSVC 19.27.29110
|
|
using namespace std::literals::string_literals;
|
|
using namespace std::literals::string_view_literals;
|
|
|
|
template <typename T>
|
|
requires vk::isVulkanHandleType<T>::value [[nodiscard]] constexpr u64
|
|
get_vk_handle(const T &_vk_handle) noexcept {
|
|
return reinterpret_cast<u64>(cast<typename T::CType>(_vk_handle));
|
|
}
|
|
|
|
template <typename F>
|
|
struct std::hash<vk::Flags<F>> {
|
|
[[nodiscard]] usize operator()(const vk::Flags<F> &_val) {
|
|
return std::hash<u32>()(cast<u32>(_val));
|
|
}
|
|
};
|
|
|
|
template <typename T>
|
|
[[nodiscard]] usize hash_any(const T &_val) {
|
|
return std::hash<std::remove_cvref_t<T>>()(_val);
|
|
}
|
|
|
|
[[nodiscard]] inline usize hash_combine(const usize _hash0,
|
|
const usize _hash1) {
|
|
constexpr usize salt_value = 0x9e3779b9;
|
|
return _hash0 ^ (_hash1 + salt_value + (_hash0 << 6) + (_hash0 >> 2));
|
|
}
|
|
|
|
struct Time {
|
|
static constexpr f64 max_delta = 0.1;
|
|
|
|
inline static f64 elapsed{ qnan<f64> };
|
|
inline static f64 delta{ qnan<f64> };
|
|
|
|
static void init() {
|
|
WARN_IF(!std::isnan(elapsed), "Time already init.");
|
|
elapsed = glfwGetTime();
|
|
delta = 1.0 / 60.0;
|
|
}
|
|
|
|
static void update() {
|
|
ERROR_IF(std::isnan(elapsed), "Time not init.");
|
|
const auto new_elapsed = glfwGetTime();
|
|
delta = std::clamp(new_elapsed - elapsed, 0.0, max_delta);
|
|
elapsed = new_elapsed;
|
|
}
|
|
};
|
|
|
|
[[nodiscard]] inline usize closest_multiple(const usize _val, const usize _of) {
|
|
return _of * ((_val + _of - 1) / _of);
|
|
}
|