// ============================================= // Aster: global.h // Copyright (c) 2020-2024 Anish Bhobe // ============================================= #pragma once #include "config.h" #include "constants.h" #include "logger.h" #include #include #include // Macros that can collide with functions. #if defined(max) #undef max #endif #if defined(min) #undef min #endif #define VULKAN_HPP_ASSERT(expr) DEBUG_IF(!(expr), "Vulkan assert failed") #include #include #include #include constexpr u32 ASTER_API_VERSION = vk::ApiVersion13; #define CODE_LOC " @ " __FILE__ ":" VULKAN_HPP_STRINGIFY(__LINE__) [[nodiscard]] inline bool failed(const vk::Result result) { return result != vk::Result::eSuccess; } template concept IsVkEnum = requires(T t) { { std::is_enum_v }; { vk::to_string(t) } -> std::same_as; }; template requires IsVkEnum [[nodiscard]] const char * toCstr(const T &val) { static std::string buffer; buffer = vk::to_string(val); return buffer.c_str(); } using NameString = eastl::fixed_string; // 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 requires vk::isVulkanHandleType::value [[nodiscard]] constexpr u64 getVkHandle(const T &vkHandle) noexcept { return reinterpret_cast(cast(vkHandle)); } template struct std::hash> { [[nodiscard]] usize operator()(const vk::Flags &val) { return std::hash()(cast(val)); } }; template [[nodiscard]] usize hashAny(const T &val) { return std::hash>()(val); } [[nodiscard]] inline usize hashCombine(const usize hash0, const usize hash1) { constexpr usize saltValue = 0x9e3779b9; return hash0 ^ (hash1 + saltValue + (hash0 << 6) + (hash0 >> 2)); } struct Time { static constexpr f64 c_MaxDelta = 0.1; inline static f64 m_Elapsed{Qnan}; inline static f64 m_Delta{Qnan}; static void Init() { WARN_IF(!std::isnan(m_Elapsed), "Time already init."); m_Elapsed = glfwGetTime(); m_Delta = 1.0 / 60.0; } static void Update() { ERROR_IF(std::isnan(m_Elapsed), "Time not init."); const auto newElapsed = glfwGetTime(); m_Delta = std::clamp(newElapsed - m_Elapsed, 0.0, c_MaxDelta); m_Elapsed = newElapsed; } }; [[nodiscard]] inline usize closestMultiple(const usize val, const usize of) { return of * ((val + of - 1) / of); } template <> struct fmt::formatter : nested_formatter { auto // ReSharper disable once CppInconsistentNaming format(vk::Result result, format_context &ctx) const { return write_padded(ctx, [this, result](auto out) { return v10::format_to(out, "{}", nested(to_string(result))); }); } }; template struct fmt::formatter> : nested_formatter { auto // ReSharper disable once CppInconsistentNaming format(const eastl::fixed_string &str, format_context &ctx) const { return write_padded(ctx, [this, str](auto out) { return v10::format_to(out, "{}", nested(str.c_str())); }); } };