project-aster/aster/global.h

131 lines
3.3 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 <glm/glm.hpp>
#include <fmt/format.h>
// 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 <EASTL/fixed_string.h>
#include <EASTL/string.h>
#include <vk_mem_alloc.h>
#include <vulkan/vulkan.hpp>
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<char, 32, false>;
template <typename TFlagBits>
struct std::hash<vk::Flags<TFlagBits>> // NOLINT(*-dcl58-cpp)
{
[[nodiscard]] usize
operator()(const vk::Flags<TFlagBits> &val)
{
return std::hash<u32>()(Cast<u32>(val));
}
};
template <typename T>
[[nodiscard]] usize
HashAny(const T &val)
{
return std::hash<std::remove_cvref_t<T>>()(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;
}
struct Time
{
static constexpr f64 MAX_DELTA = 0.1;
inline static f64 m_Elapsed{Qnan<f64>};
inline static f64 m_Delta{Qnan<f64>};
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, MAX_DELTA);
m_Elapsed = newElapsed;
}
};
[[nodiscard]] inline usize
ClosestMultiple(const usize val, const usize of)
{
return of * ((val + of - 1) / of);
}
template <>
struct fmt::formatter<vk::Result> : nested_formatter<std::string>
{
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 <typename TType, usize TCount, bool TOverflow>
struct fmt::formatter<eastl::fixed_string<TType, TCount, TOverflow>> : nested_formatter<cstr>
{
auto
// ReSharper disable once CppInconsistentNaming
format(const eastl::fixed_string<TType, TCount, TOverflow> &str, format_context &ctx) const
{
return write_padded(ctx, [this, str](auto out) { return v10::format_to(out, "{}", nested(str.c_str())); });
}
};