// ============================================= // Aster: global.h // Copyright (c) 2020-2024 Anish Bhobe // ============================================= #pragma once #include "config.h" #include "constants.h" #include "logger.h" #include #include // Macros that can collide with functions. #if defined(max) #undef max #endif #if defined(min) #undef min #endif #if !defined(NDEBUG) #define VULKAN_HPP_ASSERT(expr) DEBUG_IF(!(expr), "Vulkan assert failed") #endif #include #include #include #include constexpr u32 ASTER_API_VERSION = VK_API_VERSION_1_3; #define CODE_LOC " @ " __FILE__ ":" VULKAN_HPP_STRINGIFY(__LINE__) #define DISALLOW_COPY_AND_ASSIGN(CLASS_NAME) \ CLASS_NAME(const CLASS_NAME &other) = delete; \ CLASS_NAME &operator=(const CLASS_NAME &other) = delete #define Take(ELEMENT) eastl::exchange(ELEMENT, {}) #define TODO(MSG) assert(false && ("Unimplemented: " MSG)) [[nodiscard]] inline bool Failed(const vk::Result result) { return result != vk::Result::eSuccess; } using NameString = eastl::fixed_string; template struct eastl::hash> // NOLINT(*-dcl58-cpp) { [[nodiscard]] usize operator()(const vk::Flags &val) { return std::hash()(Cast(val)); } }; template [[nodiscard]] usize HashAny(const T &val) { return eastl::hash>()(val); } [[nodiscard]] inline usize HashCombine(const usize hash0, const usize hash1) { constexpr usize saltValue = 0x9e3779b9; const usize tempVar = hash1 + saltValue + (hash0 << 6) + (hash0 >> 2); return hash0 ^ tempVar; } [[nodiscard]] constexpr usize ClosestMultiple(const usize val, const usize of) { return of * ((val + of - 1) / of); } [[nodiscard]] constexpr u32 ClosestMultiple(const u32 val, const u32 of) { return of * ((val + of - 1) / of); } [[nodiscard]] constexpr bool IsPowerOfTwo(const usize val) { return val && !(val & (val - 1)); } [[nodiscard]] constexpr bool IsPowerOfTwo(const u32 val) { return val && !(val & (val - 1)); } [[nodiscard]] constexpr usize ClosestLargerPowerOfTwo(usize val) { val--; val |= val >> 1; val |= val >> 2; val |= val >> 4; val |= val >> 8; val |= val >> 16; val |= val >> 32; val++; return val; } [[nodiscard]] constexpr usize ClosestPowerOfTwo(const usize val) { const usize largerPo2 = ClosestLargerPowerOfTwo(val); const usize smallerPo2 = largerPo2 >> 1; return (smallerPo2 + largerPo2 <= (val << 1)) ? largerPo2 : smallerPo2; } [[nodiscard]] constexpr u32 ClosestLargerPowerOfTwo(u32 val) { val--; val |= val >> 1; val |= val >> 2; val |= val >> 4; val |= val >> 8; val |= val >> 16; val++; return val; } [[nodiscard]] constexpr u32 ClosestPowerOfTwo(const u32 val) { const u32 largerPo2 = ClosestLargerPowerOfTwo(val); const u32 smallerPo2 = largerPo2 >> 1; return (smallerPo2 + largerPo2 <= (val << 1)) ? largerPo2 : smallerPo2; } 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 v11::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 v11::format_to(out, "{}", nested(str.c_str())); }); } };