diff --git a/engine/native/core/CMakeLists.txt b/engine/native/core/CMakeLists.txt index 43e4bae..e1d3336 100644 --- a/engine/native/core/CMakeLists.txt +++ b/engine/native/core/CMakeLists.txt @@ -1,4 +1,5 @@ add_modules_library(definitions PIC) add_modules_library(math) add_modules_library(memory) -target_link_libraries(math PUBLIC definitions) \ No newline at end of file +target_link_libraries(math PUBLIC definitions) +target_link_libraries(memory PUBLIC definitions) \ No newline at end of file diff --git a/engine/native/core/definitions/definitions.cppm b/engine/native/core/definitions/definitions.cppm index ef46d48..822c998 100644 --- a/engine/native/core/definitions/definitions.cppm +++ b/engine/native/core/definitions/definitions.cppm @@ -4,11 +4,11 @@ module; export module core.defs; export import core.version; +export import core.stdtypes; static_assert(__cplusplus >= 202207L, "Minimum of C++23 required. Consider upgrading your compiler."); export namespace draco { - template concept arithmetic = std::is_arithmetic_v; @@ -26,4 +26,4 @@ export namespace draco { template concept zero_constructible = is_zero_constructible_v; -} \ No newline at end of file +} diff --git a/engine/native/core/definitions/stdtypes.cppm b/engine/native/core/definitions/stdtypes.cppm new file mode 100644 index 0000000..fb1284b --- /dev/null +++ b/engine/native/core/definitions/stdtypes.cppm @@ -0,0 +1,33 @@ +module; + +#include +#include + +export module core.stdtypes; + +export namespace draco { +using i8 = int8_t; +using i16 = int16_t; +using i32 = int32_t; +using i64 = int64_t; + +using uint = unsigned int; +using u8 = uint8_t; +using u16 = uint16_t; +using u32 = uint32_t; +using u64 = uint64_t; + +using f32 = float; +using f64 = double; + +using isize = int64_t; +using usize = std::size_t; + +using rawptr = void *; +using uintptr = uintptr_t; +using ptrdiff = ptrdiff_t; + +// UTF-32 type +using rune = u32; + +} // namespace draco diff --git a/engine/native/core/definitions/version.cppm b/engine/native/core/definitions/version.cppm index 2318ae5..fd1034d 100644 --- a/engine/native/core/definitions/version.cppm +++ b/engine/native/core/definitions/version.cppm @@ -1,29 +1,29 @@ module; -#include #include export module core.version; +import core.stdtypes; export namespace draco { - struct Version { - std::uint16_t major; - std::uint16_t minor; - std::uint16_t patch; - }; +struct Version { + u16 major; + u16 minor; + u16 patch; +}; - constexpr Version VERSION { .major = 2026, .minor = 0, .patch = 0 }; -} +constexpr Version VERSION{.major = 2026, .minor = 0, .patch = 0}; +} // namespace draco export namespace std { - template<> struct formatter { - constexpr auto parse(std::format_parse_context& ctx) { - return ctx.begin(); // Accept any format spec (or parse custom ones) - } +template <> struct formatter { + constexpr auto parse(std::format_parse_context &ctx) { + return ctx.begin(); // Accept any format spec (or parse custom ones) + } - auto format(const draco::Version& v, std::format_context& ctx) const { - return std::format_to(ctx.out(), "{}.{}.{}", v.major, v.minor, v.patch); - } - }; -} \ No newline at end of file + auto format(const draco::Version &v, std::format_context &ctx) const { + return std::format_to(ctx.out(), "{}.{}.{}", v.major, v.minor, v.patch); + } +}; +} // namespace std diff --git a/engine/native/core/math/constants.cppm b/engine/native/core/math/constants.cppm index 241eff6..633af42 100644 --- a/engine/native/core/math/constants.cppm +++ b/engine/native/core/math/constants.cppm @@ -5,32 +5,33 @@ module; export module core.math.constants; import core.defs; +import core.stdtypes; export namespace draco::math { // Limit the depth of recursive algorithms constexpr int MAX_RECURSIONS = 100; - constexpr float SQRT2 = std::numbers::sqrt2_v; - constexpr float SQRT3 = std::numbers::sqrt3_v; - constexpr float SQRT12 = 1. / SQRT2; - constexpr float SQRT13 = std::numbers::inv_sqrt3_v; - constexpr float LN2 = std::numbers::ln2_v; - constexpr float LN10 = std::numbers::ln10_v; - constexpr float PI = std::numbers::pi_v; - constexpr float PI2 = PI * .5; - constexpr float TAU = 2. * PI; - constexpr float E = std::numbers::e_v; - constexpr float INF = std::numeric_limits::infinity(); - constexpr float NaN = std::numeric_limits::quiet_NaN(); - constexpr float DB_CONVERSION_GAIN = 8.6858896380650365530225783783321; - constexpr float GAIN_CONVERSION_DB = 0.11512925464970228420089957273422; - constexpr float UINT32_MAX_F = 1.f / std::numeric_limits::max(); - constexpr float DECIMAL_LIMIT_F = 8388608.0f; + constexpr f32 SQRT2 = std::numbers::sqrt2_v; + constexpr f32 SQRT3 = std::numbers::sqrt3_v; + constexpr f32 SQRT12 = 1. / SQRT2; + constexpr f32 SQRT13 = std::numbers::inv_sqrt3_v; + constexpr f32 LN2 = std::numbers::ln2_v; + constexpr f32 LN10 = std::numbers::ln10_v; + constexpr f32 PI = std::numbers::pi_v; + constexpr f32 PI2 = PI * .5; + constexpr f32 TAU = 2. * PI; + constexpr f32 E = std::numbers::e_v; + constexpr f32 INF = std::numeric_limits::infinity(); + constexpr f32 NaN = std::numeric_limits::quiet_NaN(); + constexpr f32 DB_CONVERSION_GAIN = 8.6858896380650365530225783783321; + constexpr f32 GAIN_CONVERSION_DB = 0.11512925464970228420089957273422; + constexpr f32 UINT32_MAX_F = 1.f / std::numeric_limits::max(); + constexpr f32 DECIMAL_LIMIT_F = 8388608.0f; - constexpr float CMP_EPSILON = 0.000001f; - constexpr float CMP_EPSILON2 = CMP_EPSILON * CMP_EPSILON; + constexpr f32 CMP_EPSILON = 0.000001f; + constexpr f32 CMP_EPSILON2 = CMP_EPSILON * CMP_EPSILON; - constexpr float CMP_NORMALIZE_TOLERANCE = 0.000001f; - constexpr float CMP_NORMALIZE_TOLERANCE2 = CMP_NORMALIZE_TOLERANCE * CMP_NORMALIZE_TOLERANCE; - constexpr float CMP_POINT_IN_PLANE_EPSILON = 0.00001f; + constexpr f32 CMP_NORMALIZE_TOLERANCE = 0.000001f; + constexpr f32 CMP_NORMALIZE_TOLERANCE2 = CMP_NORMALIZE_TOLERANCE * CMP_NORMALIZE_TOLERANCE; + constexpr f32 CMP_POINT_IN_PLANE_EPSILON = 0.00001f; } diff --git a/engine/native/core/math/functions.cppm b/engine/native/core/math/functions.cppm index f0a69fc..c326009 100644 --- a/engine/native/core/math/functions.cppm +++ b/engine/native/core/math/functions.cppm @@ -8,6 +8,7 @@ module; export module core.math.functions; import core.math.constants; import core.defs; +import core.stdtypes; export namespace draco::math { template @@ -32,7 +33,7 @@ export namespace draco::math { template constexpr T abs(T value) noexcept { // Manually compute abs for signed types. - // Also avoids potential int8_t -> int issues. + // Also avoids potential i8 -> i32 issues. if constexpr (std::floating_point) { return value < T{0} ? -value : value; } else if constexpr (std::signed_integral) { @@ -65,27 +66,27 @@ export namespace draco::math { } } - constexpr float floor(float value) noexcept { + constexpr f32 floor(f32 value) noexcept { if (value != value || abs(value) >= DECIMAL_LIMIT_F) { return value; } - const float truncated = static_cast(value); + const f32 truncated = static_cast(value); return truncated - (value < truncated); } - constexpr float ceil(float value) noexcept { + constexpr f32 ceil(f32 value) noexcept { return -floor(-value); } - constexpr float trunc(float value) noexcept { + constexpr f32 trunc(f32 value) noexcept { if (value != value || abs(value) >= DECIMAL_LIMIT_F) { return value; } - return static_cast(value); + return static_cast(value); } - constexpr float round(float value) noexcept { - const float s = sign(value); + constexpr f32 round(f32 value) noexcept { + const f32 s = sign(value); return s * floor(s * value + 0.5f); } diff --git a/engine/native/core/math/math.cppm b/engine/native/core/math/math.cppm index fe3de8f..517ec40 100644 --- a/engine/native/core/math/math.cppm +++ b/engine/native/core/math/math.cppm @@ -4,3 +4,4 @@ export import core.math.constants; export import core.math.functions; export import core.math.types; export import core.defs; +export import core.stdtypes; diff --git a/engine/native/core/math/math.test.cpp b/engine/native/core/math/math.test.cpp index d538b82..2424f92 100644 --- a/engine/native/core/math/math.test.cpp +++ b/engine/native/core/math/math.test.cpp @@ -31,10 +31,12 @@ import core.math; +using namespace draco; + TEST_SUITE("math") { TEST_CASE("pow") { - float result = draco::math::pow(2.0f, 0.5f); - constexpr float expected = draco::math::SQRT2; + f32 result = draco::math::pow(2.0f, 0.5f); + constexpr f32 expected = draco::math::SQRT2; CHECK_EQ(result, expected); } @@ -308,8 +310,8 @@ TEST_SUITE("vector2") { ); SUBCASE("float") { - static constexpr float smaller_length = 1.0f; - static constexpr float larger_length = 10.0f; + static constexpr f32 smaller_length = 1.0f; + static constexpr f32 larger_length = 10.0f; const Vector2 result_smaller = min_length(a, smaller_length); const Vector2 result_swapped = min_length(smaller_length, a); @@ -357,8 +359,8 @@ TEST_SUITE("vector2") { ); SUBCASE("float") { - static constexpr float smaller_length = 1.0f; - static constexpr float larger_length = 10.0f; + static constexpr f32 smaller_length = 1.0f; + static constexpr f32 larger_length = 10.0f; const Vector2 result_smaller = max_length(a, smaller_length); const Vector2 result_swapped = max_length(larger_length, a); @@ -763,8 +765,8 @@ TEST_SUITE("vector3") { ); SUBCASE("float") { - static constexpr float smaller_length = 1.0f; - static constexpr float larger_length = 10.0f; + static constexpr f32 smaller_length = 1.0f; + static constexpr f32 larger_length = 10.0f; const Vector3 result_smaller = min_length(a, smaller_length); const Vector3 result_swapped = min_length(smaller_length, a); @@ -812,8 +814,8 @@ TEST_SUITE("vector3") { ); SUBCASE("float") { - static constexpr float smaller_length = 1.0f; - static constexpr float larger_length = 10.0f; + static constexpr f32 smaller_length = 1.0f; + static constexpr f32 larger_length = 10.0f; const Vector3 result_smaller = max_length(a, smaller_length); const Vector3 result_swapped = max_length(larger_length, a); @@ -1254,8 +1256,8 @@ TEST_SUITE("vector4") { ); SUBCASE("float") { - static constexpr float smaller_length = 1.0f; - static constexpr float larger_length = 10.0f; + static constexpr f32 smaller_length = 1.0f; + static constexpr f32 larger_length = 10.0f; const Vector4 result_smaller = min_length(a, smaller_length); const Vector4 result_swapped = min_length(smaller_length, a); @@ -1303,8 +1305,8 @@ TEST_SUITE("vector4") { ); SUBCASE("float") { - static constexpr float smaller_length = 1.0f; - static constexpr float larger_length = 10.0f; + static constexpr f32 smaller_length = 1.0f; + static constexpr f32 larger_length = 10.0f; const Vector4 result_smaller = max_length(a, smaller_length); const Vector4 result_swapped = max_length(larger_length, a); diff --git a/engine/native/core/math/types_common.cppm b/engine/native/core/math/types_common.cppm index fe3d77a..089faa2 100644 --- a/engine/native/core/math/types_common.cppm +++ b/engine/native/core/math/types_common.cppm @@ -1,5 +1,6 @@ export module core.math.types:common; import core.defs; +import core.stdtypes; export namespace draco::math { struct Vector2; @@ -7,142 +8,142 @@ export namespace draco::math { struct Vector4; struct alignas(8) Vector2 { - float x, y; + f32 x, y; // constructors [[nodiscard]] constexpr Vector2() noexcept = default; - [[nodiscard]] constexpr explicit Vector2(float n) noexcept; - [[nodiscard]] constexpr Vector2(float x, float y) noexcept; + [[nodiscard]] constexpr explicit Vector2(f32 n) noexcept; + [[nodiscard]] constexpr Vector2(f32 x, f32 y) noexcept; [[nodiscard]] constexpr explicit Vector2(const Vector3& xy) noexcept; [[nodiscard]] constexpr explicit Vector2(const Vector4& xy) noexcept; // static - [[nodiscard]] static constexpr Vector2 x_axis(float x = 1.0f) noexcept; - [[nodiscard]] static constexpr Vector2 y_axis(float y = 1.0f) noexcept; - [[nodiscard]] static Vector2 polar(float angle, float radius = 1.0f) noexcept; + [[nodiscard]] static constexpr Vector2 x_axis(f32 x = 1.0f) noexcept; + [[nodiscard]] static constexpr Vector2 y_axis(f32 y = 1.0f) noexcept; + [[nodiscard]] static Vector2 polar(f32 angle, f32 radius = 1.0f) noexcept; // element access - [[nodiscard]] constexpr float& operator[](int i) noexcept; - [[nodiscard]] constexpr const float& operator[](int i) const noexcept; + [[nodiscard]] constexpr f32& operator[](i32 i) noexcept; + [[nodiscard]] constexpr const f32& operator[](i32 i) const noexcept; // swizzle - [[nodiscard]] constexpr Vector2 operator[](int i0, int i1) noexcept; - [[nodiscard]] constexpr Vector2 operator[](int i0, int i1) const noexcept; - [[nodiscard]] constexpr Vector3 operator[](int i0, int i1, int i2) noexcept; - [[nodiscard]] constexpr Vector3 operator[](int i0, int i1, int i2) const noexcept; - [[nodiscard]] constexpr Vector4 operator[](int i0, int i1, int i2, int i3) noexcept; - [[nodiscard]] constexpr Vector4 operator[](int i0, int i1, int i2, int i3) const noexcept; + [[nodiscard]] constexpr Vector2 operator[](i32 i0, i32 i1) noexcept; + [[nodiscard]] constexpr Vector2 operator[](i32 i0, i32 i1) const noexcept; + [[nodiscard]] constexpr Vector3 operator[](i32 i0, i32 i1, i32 i2) noexcept; + [[nodiscard]] constexpr Vector3 operator[](i32 i0, i32 i1, i32 i2) const noexcept; + [[nodiscard]] constexpr Vector4 operator[](i32 i0, i32 i1, i32 i2, i32 i3) noexcept; + [[nodiscard]] constexpr Vector4 operator[](i32 i0, i32 i1, i32 i2, i32 i3) const noexcept; // operators [[nodiscard]] constexpr Vector2 operator+() const noexcept; [[nodiscard]] constexpr Vector2 operator-() const noexcept; [[nodiscard]] constexpr bool operator==(const Vector2& other) const noexcept = default; constexpr Vector2& operator+=(const Vector2& other) noexcept; - constexpr Vector2& operator+=(float other) noexcept; + constexpr Vector2& operator+=(f32 other) noexcept; constexpr Vector2& operator-=(const Vector2& other) noexcept; - constexpr Vector2& operator-=(float other) noexcept; + constexpr Vector2& operator-=(f32 other) noexcept; constexpr Vector2& operator*=(const Vector2& other) noexcept; - constexpr Vector2& operator*=(float other) noexcept; + constexpr Vector2& operator*=(f32 other) noexcept; constexpr Vector2& operator/=(const Vector2& other) noexcept; - constexpr Vector2& operator/=(float other) noexcept; - constexpr Vector2& operator=(float other) noexcept; + constexpr Vector2& operator/=(f32 other) noexcept; + constexpr Vector2& operator=(f32 other) noexcept; }; struct alignas(16) Vector3 { - float x, y, z; + f32 x, y, z; // constructors [[nodiscard]] constexpr Vector3() noexcept = default; - [[nodiscard]] constexpr explicit Vector3(float n) noexcept; - [[nodiscard]] constexpr Vector3(float x, float y, float z) noexcept; - [[nodiscard]] constexpr explicit Vector3(const Vector2& xy, float z = 0.0f) noexcept; - [[nodiscard]] constexpr Vector3(float x, const Vector2& yz) noexcept; + [[nodiscard]] constexpr explicit Vector3(f32 n) noexcept; + [[nodiscard]] constexpr Vector3(f32 x, f32 y, f32 z) noexcept; + [[nodiscard]] constexpr explicit Vector3(const Vector2& xy, f32 z = 0.0f) noexcept; + [[nodiscard]] constexpr Vector3(f32 x, const Vector2& yz) noexcept; [[nodiscard]] constexpr explicit Vector3(const Vector4& xyz) noexcept; // static - [[nodiscard]] static constexpr Vector3 x_axis(float x = 1.0f) noexcept; - [[nodiscard]] static constexpr Vector3 y_axis(float y = 1.0f) noexcept; - [[nodiscard]] static constexpr Vector3 z_axis(float z = 1.0f) noexcept; - [[nodiscard]] static Vector3 spherical(float azimuth, float inclination, float radius = 1.0f) noexcept; - [[nodiscard]] static Vector3 cylindrical(float angle, float radius = 1.0f, float height = 0.0f) noexcept; + [[nodiscard]] static constexpr Vector3 x_axis(f32 x = 1.0f) noexcept; + [[nodiscard]] static constexpr Vector3 y_axis(f32 y = 1.0f) noexcept; + [[nodiscard]] static constexpr Vector3 z_axis(f32 z = 1.0f) noexcept; + [[nodiscard]] static Vector3 spherical(f32 azimuth, f32 inclination, f32 radius = 1.0f) noexcept; + [[nodiscard]] static Vector3 cylindrical(f32 angle, f32 radius = 1.0f, f32 height = 0.0f) noexcept; // element access - [[nodiscard]] constexpr float& operator[](int i) noexcept; - [[nodiscard]] constexpr const float& operator[](int i) const noexcept; + [[nodiscard]] constexpr f32& operator[](i32 i) noexcept; + [[nodiscard]] constexpr const f32& operator[](i32 i) const noexcept; // swizzle - [[nodiscard]] constexpr Vector2 operator[](int i0, int i1) noexcept; - [[nodiscard]] constexpr Vector2 operator[](int i0, int i1) const noexcept; - [[nodiscard]] constexpr Vector3 operator[](int i0, int i1, int i2) noexcept; - [[nodiscard]] constexpr Vector3 operator[](int i0, int i1, int i2) const noexcept; - [[nodiscard]] constexpr Vector4 operator[](int i0, int i1, int i2, int i3) noexcept; - [[nodiscard]] constexpr Vector4 operator[](int i0, int i1, int i2, int i3) const noexcept; + [[nodiscard]] constexpr Vector2 operator[](i32 i0, i32 i1) noexcept; + [[nodiscard]] constexpr Vector2 operator[](i32 i0, i32 i1) const noexcept; + [[nodiscard]] constexpr Vector3 operator[](i32 i0, i32 i1, i32 i2) noexcept; + [[nodiscard]] constexpr Vector3 operator[](i32 i0, i32 i1, i32 i2) const noexcept; + [[nodiscard]] constexpr Vector4 operator[](i32 i0, i32 i1, i32 i2, i32 i3) noexcept; + [[nodiscard]] constexpr Vector4 operator[](i32 i0, i32 i1, i32 i2, i32 i3) const noexcept; // operators [[nodiscard]] constexpr Vector3 operator+() const noexcept; [[nodiscard]] constexpr Vector3 operator-() const noexcept; [[nodiscard]] constexpr bool operator==(const Vector3& other) const noexcept = default; constexpr Vector3& operator+=(const Vector3& other) noexcept; - constexpr Vector3& operator+=(float other) noexcept; + constexpr Vector3& operator+=(f32 other) noexcept; constexpr Vector3& operator-=(const Vector3& other) noexcept; - constexpr Vector3& operator-=(float other) noexcept; + constexpr Vector3& operator-=(f32 other) noexcept; constexpr Vector3& operator*=(const Vector3& other) noexcept; - constexpr Vector3& operator*=(float other) noexcept; + constexpr Vector3& operator*=(f32 other) noexcept; constexpr Vector3& operator/=(const Vector3& other) noexcept; - constexpr Vector3& operator/=(float other) noexcept; - constexpr Vector3& operator=(float other) noexcept; + constexpr Vector3& operator/=(f32 other) noexcept; + constexpr Vector3& operator=(f32 other) noexcept; }; struct alignas(16) Vector4 { - float x, y, z, w; + f32 x, y, z, w; // constructors [[nodiscard]] constexpr Vector4() noexcept = default; - [[nodiscard]] constexpr explicit Vector4(float n) noexcept; - [[nodiscard]] constexpr Vector4(float x, float y, float z, float w) noexcept; + [[nodiscard]] constexpr explicit Vector4(f32 n) noexcept; + [[nodiscard]] constexpr Vector4(f32 x, f32 y, f32 z, f32 w) noexcept; [[nodiscard]] constexpr explicit Vector4(const Vector2& xy) noexcept; - [[nodiscard]] constexpr Vector4(const Vector2& xy, float z, float w) noexcept; - [[nodiscard]] constexpr Vector4(float x, const Vector2& yz, float w) noexcept; - [[nodiscard]] constexpr Vector4(float x, float y, const Vector2& zw) noexcept; + [[nodiscard]] constexpr Vector4(const Vector2& xy, f32 z, f32 w) noexcept; + [[nodiscard]] constexpr Vector4(f32 x, const Vector2& yz, f32 w) noexcept; + [[nodiscard]] constexpr Vector4(f32 x, f32 y, const Vector2& zw) noexcept; [[nodiscard]] constexpr Vector4(const Vector2& xy, const Vector2& zw) noexcept; - [[nodiscard]] constexpr explicit Vector4(const Vector3& xyz, float w = 0.0f) noexcept; - [[nodiscard]] constexpr Vector4(float x, const Vector3& yzw) noexcept; + [[nodiscard]] constexpr explicit Vector4(const Vector3& xyz, f32 w = 0.0f) noexcept; + [[nodiscard]] constexpr Vector4(f32 x, const Vector3& yzw) noexcept; // static - [[nodiscard]] static constexpr Vector4 x_axis(float x = 1.0f) noexcept; - [[nodiscard]] static constexpr Vector4 y_axis(float y = 1.0f) noexcept; - [[nodiscard]] static constexpr Vector4 z_axis(float z = 1.0f) noexcept; - [[nodiscard]] static constexpr Vector4 w_axis(float w = 1.0f) noexcept; + [[nodiscard]] static constexpr Vector4 x_axis(f32 x = 1.0f) noexcept; + [[nodiscard]] static constexpr Vector4 y_axis(f32 y = 1.0f) noexcept; + [[nodiscard]] static constexpr Vector4 z_axis(f32 z = 1.0f) noexcept; + [[nodiscard]] static constexpr Vector4 w_axis(f32 w = 1.0f) noexcept; // element access - [[nodiscard]] constexpr float& operator[](int i) noexcept; - [[nodiscard]] constexpr const float& operator[](int i) const noexcept; + [[nodiscard]] constexpr f32& operator[](i32 i) noexcept; + [[nodiscard]] constexpr const f32& operator[](i32 i) const noexcept; // swizzle - [[nodiscard]] constexpr Vector2 operator[](int i0, int i1) noexcept; - [[nodiscard]] constexpr Vector2 operator[](int i0, int i1) const noexcept; - [[nodiscard]] constexpr Vector3 operator[](int i0, int i1, int i2) noexcept; - [[nodiscard]] constexpr Vector3 operator[](int i0, int i1, int i2) const noexcept; - [[nodiscard]] constexpr Vector4 operator[](int i0, int i1, int i2, int i3) noexcept; - [[nodiscard]] constexpr Vector4 operator[](int i0, int i1, int i2, int i3) const noexcept; + [[nodiscard]] constexpr Vector2 operator[](i32 i0, i32 i1) noexcept; + [[nodiscard]] constexpr Vector2 operator[](i32 i0, i32 i1) const noexcept; + [[nodiscard]] constexpr Vector3 operator[](i32 i0, i32 i1, i32 i2) noexcept; + [[nodiscard]] constexpr Vector3 operator[](i32 i0, i32 i1, i32 i2) const noexcept; + [[nodiscard]] constexpr Vector4 operator[](i32 i0, i32 i1, i32 i2, i32 i3) noexcept; + [[nodiscard]] constexpr Vector4 operator[](i32 i0, i32 i1, i32 i2, i32 i3) const noexcept; // member operators [[nodiscard]] constexpr Vector4 operator+() const noexcept; [[nodiscard]] constexpr Vector4 operator-() const noexcept; [[nodiscard]] constexpr bool operator==(const Vector4& other) const noexcept = default; constexpr Vector4& operator+=(const Vector4& other) noexcept; - constexpr Vector4& operator+=(float other) noexcept; + constexpr Vector4& operator+=(f32 other) noexcept; constexpr Vector4& operator-=(const Vector4& other) noexcept; - constexpr Vector4& operator-=(float other) noexcept; + constexpr Vector4& operator-=(f32 other) noexcept; constexpr Vector4& operator*=(const Vector4& other) noexcept; - constexpr Vector4& operator*=(float other) noexcept; + constexpr Vector4& operator*=(f32 other) noexcept; constexpr Vector4& operator/=(const Vector4& other) noexcept; - constexpr Vector4& operator/=(float other) noexcept; - constexpr Vector4& operator=(float other) noexcept; + constexpr Vector4& operator/=(f32 other) noexcept; + constexpr Vector4& operator=(f32 other) noexcept; }; } -template consteval T select(const int i, const T v1, const T v2) { +template consteval T select(const draco::i32 i, const T v1, const T v2) { switch (i) { case 0: return v1; case 1: return v2; @@ -150,7 +151,7 @@ template consteval T select(const int i, const T v1, const T v2) { } } -template consteval T select(const int i, const T v1, const T v2, const T v3) { +template consteval T select(const draco::i32 i, const T v1, const T v2, const T v3) { switch (i) { case 0: return v1; case 1: return v2; @@ -159,7 +160,7 @@ template consteval T select(const int i, const T v1, const T v2, con } } -template consteval T select(const int i, const T v1, const T v2, const T v3, const T v4) { +template consteval T select(const draco::i32 i, const T v1, const T v2, const T v3, const T v4) { switch (i) { case 0: return v1; case 1: return v2; diff --git a/engine/native/core/math/vector2.cppm b/engine/native/core/math/vector2.cppm index 5446043..a20b6e3 100644 --- a/engine/native/core/math/vector2.cppm +++ b/engine/native/core/math/vector2.cppm @@ -17,6 +17,7 @@ export import :common; import core.math.constants; import core.math.functions; import core.defs; +import core.stdtypes; export namespace draco::math { // assertions @@ -26,10 +27,10 @@ export namespace draco::math { static_assert(std::is_standard_layout_v, "Vector2 must be standard layout"); // constructors - [[nodiscard]] constexpr Vector2::Vector2(const float n) noexcept + [[nodiscard]] constexpr Vector2::Vector2(const f32 n) noexcept : x{n}, y{n} { } - [[nodiscard]] constexpr Vector2::Vector2(const float x, const float y) noexcept + [[nodiscard]] constexpr Vector2::Vector2(const f32 x, const f32 y) noexcept : x{x}, y{y} { } [[nodiscard]] constexpr Vector2::Vector2(const Vector3& xy) noexcept @@ -39,20 +40,20 @@ export namespace draco::math { : x{xy.x}, y{xy.y} { } // static - [[nodiscard]] constexpr Vector2 Vector2::x_axis(const float x) noexcept { + [[nodiscard]] constexpr Vector2 Vector2::x_axis(const f32 x) noexcept { return { x, 0.0f }; } - [[nodiscard]] constexpr Vector2 Vector2::y_axis(const float y) noexcept { + [[nodiscard]] constexpr Vector2 Vector2::y_axis(const f32 y) noexcept { return { 0.0f, y }; } - [[nodiscard]] Vector2 Vector2::polar(const float angle, const float radius) noexcept { + [[nodiscard]] Vector2 Vector2::polar(const f32 angle, const f32 radius) noexcept { return { radius * std::cos(angle), radius * std::sin(angle) }; } // element access - [[nodiscard]] constexpr float& Vector2::operator[](const int i) noexcept { + [[nodiscard]] constexpr f32& Vector2::operator[](const i32 i) noexcept { if consteval { return i ? y : x; } else { @@ -60,7 +61,7 @@ export namespace draco::math { } } - [[nodiscard]] constexpr const float& Vector2::operator[](const int i) const noexcept { + [[nodiscard]] constexpr const f32& Vector2::operator[](const i32 i) const noexcept { if consteval { return i ? y : x; } else { @@ -69,7 +70,7 @@ export namespace draco::math { } // swizzle - [[nodiscard]] constexpr Vector2 Vector2::operator[](const int i0, const int i1) noexcept { + [[nodiscard]] constexpr Vector2 Vector2::operator[](const i32 i0, const i32 i1) noexcept { if consteval { return { select(i0, x, y), select(i1, x, y) }; } else { @@ -77,7 +78,7 @@ export namespace draco::math { } } - [[nodiscard]] constexpr Vector2 Vector2::operator[](const int i0, const int i1) const noexcept { + [[nodiscard]] constexpr Vector2 Vector2::operator[](const i32 i0, const i32 i1) const noexcept { if consteval { return { select(i0, x, y), select(i1, x, y) }; } else { @@ -85,7 +86,7 @@ export namespace draco::math { } } - [[nodiscard]] constexpr Vector3 Vector2::operator[](const int i0, const int i1, const int i2) noexcept { + [[nodiscard]] constexpr Vector3 Vector2::operator[](const i32 i0, const i32 i1, const i32 i2) noexcept { if consteval { return { select(i0, x, y), select(i1, x, y), select(i2, x, y) }; } else { @@ -93,7 +94,7 @@ export namespace draco::math { } } - [[nodiscard]] constexpr Vector3 Vector2::operator[](const int i0, const int i1, const int i2) const noexcept { + [[nodiscard]] constexpr Vector3 Vector2::operator[](const i32 i0, const i32 i1, const i32 i2) const noexcept { if consteval { return { select(i0, x, y), select(i1, x, y), select(i2, x, y) }; } else { @@ -101,7 +102,7 @@ export namespace draco::math { } } - [[nodiscard]] constexpr Vector4 Vector2::operator[](const int i0, const int i1, const int i2, const int i3) noexcept { + [[nodiscard]] constexpr Vector4 Vector2::operator[](const i32 i0, const i32 i1, const i32 i2, const i32 i3) noexcept { if consteval { return { select(i0, x, y), select(i1, x, y), select(i2, x, y), select(i3, x, y) }; } else { @@ -109,7 +110,7 @@ export namespace draco::math { } } - [[nodiscard]] constexpr Vector4 Vector2::operator[](const int i0, const int i1, const int i2, const int i3) const noexcept { + [[nodiscard]] constexpr Vector4 Vector2::operator[](const i32 i0, const i32 i1, const i32 i2, const i32 i3) const noexcept { if consteval { return { select(i0, x, y), select(i1, x, y), select(i2, x, y), select(i3, x, y) }; } else { @@ -124,7 +125,7 @@ export namespace draco::math { return *this; } - constexpr Vector2& Vector2::operator+=(const float other) noexcept { + constexpr Vector2& Vector2::operator+=(const f32 other) noexcept { x += other; y += other; return *this; @@ -136,7 +137,7 @@ export namespace draco::math { return *this; } - constexpr Vector2& Vector2::operator-=(const float other) noexcept { + constexpr Vector2& Vector2::operator-=(const f32 other) noexcept { x -= other; y -= other; return *this; @@ -148,7 +149,7 @@ export namespace draco::math { return *this; } - constexpr Vector2& Vector2::operator*=(const float other) noexcept { + constexpr Vector2& Vector2::operator*=(const f32 other) noexcept { x *= other; y *= other; return *this; @@ -160,14 +161,14 @@ export namespace draco::math { return *this; } - constexpr Vector2& Vector2::operator/=(const float other) noexcept { - const float inv = 1.0f / other; + constexpr Vector2& Vector2::operator/=(const f32 other) noexcept { + const f32 inv = 1.0f / other; x *= inv; y *= inv; return *this; } - constexpr Vector2& Vector2::operator=(const float other) noexcept { + constexpr Vector2& Vector2::operator=(const f32 other) noexcept { x = other; y = other; return *this; @@ -185,11 +186,11 @@ export namespace draco::math { return { a.x+b.x, a.y+b.y }; } - [[nodiscard]] constexpr Vector2 operator+(const Vector2& a, const float b) noexcept { + [[nodiscard]] constexpr Vector2 operator+(const Vector2& a, const f32 b) noexcept { return { a.x+b, a.y+b }; } - [[nodiscard]] constexpr Vector2 operator+(const float a, const Vector2& b) noexcept { + [[nodiscard]] constexpr Vector2 operator+(const f32 a, const Vector2& b) noexcept { return b+a; } @@ -197,11 +198,11 @@ export namespace draco::math { return { a.x-b.x, a.y-b.y }; } - [[nodiscard]] constexpr Vector2 operator-(const Vector2& a, const float b) noexcept { + [[nodiscard]] constexpr Vector2 operator-(const Vector2& a, const f32 b) noexcept { return { a.x-b, a.y-b }; } - [[nodiscard]] constexpr Vector2 operator-(const float a, const Vector2& b) noexcept { + [[nodiscard]] constexpr Vector2 operator-(const f32 a, const Vector2& b) noexcept { return { a-b.x, a-b.y }; } @@ -209,11 +210,11 @@ export namespace draco::math { return { a.x*b.x, a.y*b.y }; } - [[nodiscard]] constexpr Vector2 operator*(const Vector2& a, const float b) noexcept { + [[nodiscard]] constexpr Vector2 operator*(const Vector2& a, const f32 b) noexcept { return { a.x*b, a.y*b }; } - [[nodiscard]] constexpr Vector2 operator*(const float a, const Vector2& b) noexcept { + [[nodiscard]] constexpr Vector2 operator*(const f32 a, const Vector2& b) noexcept { return b*a; } @@ -221,44 +222,44 @@ export namespace draco::math { return { a.x/b.x, a.y/b.y }; } - [[nodiscard]] constexpr Vector2 operator/(const Vector2& a, const float b) noexcept { + [[nodiscard]] constexpr Vector2 operator/(const Vector2& a, const f32 b) noexcept { return a * (1.0f / b); } - [[nodiscard]] constexpr Vector2 operator/(const float a, const Vector2& b) noexcept { + [[nodiscard]] constexpr Vector2 operator/(const f32 a, const Vector2& b) noexcept { return { a/b.x, a/b.y }; } // functions // Returns dot product - [[nodiscard]] constexpr float dot(const Vector2& a, const Vector2& b) noexcept { + [[nodiscard]] constexpr f32 dot(const Vector2& a, const Vector2& b) noexcept { return a.x*b.x + a.y*b.y; } // Returns squared magnitude - [[nodiscard]] constexpr float length_sq(const Vector2& v) noexcept { + [[nodiscard]] constexpr f32 length_sq(const Vector2& v) noexcept { return dot(v, v); } // Returns magnitude - [[nodiscard]] float length(const Vector2& v) noexcept { + [[nodiscard]] f32 length(const Vector2& v) noexcept { return std::sqrt(length_sq(v)); } // Return squared distance between two vectors - [[nodiscard]] constexpr float distance_sq(const Vector2& a, const Vector2& b) noexcept { + [[nodiscard]] constexpr f32 distance_sq(const Vector2& a, const Vector2& b) noexcept { return length_sq(a - b); } // Returns distance between two vectors - [[nodiscard]] float distance(const Vector2& a, const Vector2& b) noexcept { + [[nodiscard]] f32 distance(const Vector2& a, const Vector2& b) noexcept { return length(a - b); } // Safe normalize, checks length [[nodiscard]] Vector2 normalize(const Vector2& v) noexcept { - const float len = length(v); + const f32 len = length(v); return (len > CMP_NORMALIZE_TOLERANCE) ? v / len : Vector2(); } @@ -280,12 +281,12 @@ export namespace draco::math { } // Returns the angle between two vectors - [[nodiscard]] float angle(const Vector2& a, const Vector2& b) noexcept { + [[nodiscard]] f32 angle(const Vector2& a, const Vector2& b) noexcept { return std::acos(dot(a, b) / (length(a) * length(b))); } - // Returns linear interpolation between two vectors - [[nodiscard]] constexpr Vector2 lerp(const Vector2& from, const Vector2& to, const float weight) noexcept { + // Returns linear i32erpolation between two vectors + [[nodiscard]] constexpr Vector2 lerp(const Vector2& from, const Vector2& to, const f32 weight) noexcept { return { lerp(from.x, to.x, weight), lerp(from.y, to.y, weight) @@ -300,14 +301,14 @@ export namespace draco::math { }; } - [[nodiscard]] constexpr Vector2 min(const Vector2& a, const float b) noexcept { + [[nodiscard]] constexpr Vector2 min(const Vector2& a, const f32 b) noexcept { return { std::min(a.x, b), std::min(a.y, b) }; } - [[nodiscard]] constexpr Vector2 min(const float a, const Vector2& b) noexcept { + [[nodiscard]] constexpr Vector2 min(const f32 a, const Vector2& b) noexcept { return min(b, a); } @@ -317,8 +318,8 @@ export namespace draco::math { } // Returns a vector in the same direction whose length is bounded above by the given value. - [[nodiscard]] Vector2 min_length(const Vector2& a, const float b) noexcept { - const float len_sq = length_sq(a); + [[nodiscard]] Vector2 min_length(const Vector2& a, const f32 b) noexcept { + const f32 len_sq = length_sq(a); if (len_sq > b * b) { return a * (b / std::sqrt(len_sq)); @@ -327,7 +328,7 @@ export namespace draco::math { } } - [[nodiscard]] Vector2 min_length(const float a, const Vector2& b) noexcept { + [[nodiscard]] Vector2 min_length(const f32 a, const Vector2& b) noexcept { return min_length(b, a); } @@ -339,14 +340,14 @@ export namespace draco::math { }; } - [[nodiscard]] constexpr Vector2 max(const Vector2& a, const float b) noexcept { + [[nodiscard]] constexpr Vector2 max(const Vector2& a, const f32 b) noexcept { return { std::max(a.x, b), std::max(a.y, b) }; } - [[nodiscard]] constexpr Vector2 max(const float a, const Vector2& b) noexcept { + [[nodiscard]] constexpr Vector2 max(const f32 a, const Vector2& b) noexcept { return max(b, a); } @@ -356,8 +357,8 @@ export namespace draco::math { } // Returns a vector in the same direction whose length is bounded below by the given value. Returns the 0 vector if the vector is too small to be normalized. - [[nodiscard]] Vector2 max_length(const Vector2& a, const float b) noexcept { - const float len_sq = length_sq(a); + [[nodiscard]] Vector2 max_length(const Vector2& a, const f32 b) noexcept { + const f32 len_sq = length_sq(a); if (len_sq <= CMP_NORMALIZE_TOLERANCE2) { return Vector2(); @@ -368,7 +369,7 @@ export namespace draco::math { } } - [[nodiscard]] Vector2 max_length(const float a, const Vector2& b) noexcept { + [[nodiscard]] Vector2 max_length(const f32 a, const Vector2& b) noexcept { return max_length(b, a); } @@ -377,13 +378,13 @@ export namespace draco::math { return max(x_min, min(x, x_max)); } - [[nodiscard]] constexpr Vector2 clamp(const Vector2& x, const float x_min, const float x_max) noexcept { + [[nodiscard]] constexpr Vector2 clamp(const Vector2& x, const f32 x_min, const f32 x_max) noexcept { return max(x_min, min(x, x_max)); } // Clamps the length of the vector to the range [x_min, x_max]. Presupposes x_min <= x_max. Returns the 0 vector if the vector is too small to be normalized. - [[nodiscard]] Vector2 clamp_length(const Vector2& v, const float x_min, const float x_max) noexcept { - const float len_sq = length_sq(v); + [[nodiscard]] Vector2 clamp_length(const Vector2& v, const f32 x_min, const f32 x_max) noexcept { + const f32 len_sq = length_sq(v); if (len_sq <= CMP_NORMALIZE_TOLERANCE2) { return Vector2(); @@ -451,12 +452,12 @@ export namespace draco::math { } export namespace std { - template<> struct formatter : formatter { + template<> struct formatter : formatter { auto format(const draco::math::Vector2& v, format_context& ctx) const { ctx.advance_to(format_to(ctx.out(), "{{")); - ctx.advance_to(formatter::format(v.x, ctx)); + ctx.advance_to(formatter::format(v.x, ctx)); ctx.advance_to(format_to(ctx.out(), ", ")); - ctx.advance_to(formatter::format(v.y, ctx)); + ctx.advance_to(formatter::format(v.y, ctx)); return format_to(ctx.out(), "}}"); } }; diff --git a/engine/native/core/math/vector3.cppm b/engine/native/core/math/vector3.cppm index 5254e90..05240c0 100644 --- a/engine/native/core/math/vector3.cppm +++ b/engine/native/core/math/vector3.cppm @@ -17,6 +17,7 @@ export import :common; import core.math.constants; import core.math.functions; import core.defs; +import core.stdtypes; export namespace draco::math { // assertions @@ -26,45 +27,45 @@ export namespace draco::math { static_assert(std::is_standard_layout_v, "Vector3 must be standard layout"); // constructors - [[nodiscard]] constexpr Vector3::Vector3(const float n) noexcept + [[nodiscard]] constexpr Vector3::Vector3(const f32 n) noexcept : x{n}, y{n}, z{n} { } - [[nodiscard]] constexpr Vector3::Vector3(const float x, const float y, const float z) noexcept + [[nodiscard]] constexpr Vector3::Vector3(const f32 x, const f32 y, const f32 z) noexcept : x{x}, y{y}, z{z} { } - [[nodiscard]] constexpr Vector3::Vector3(const Vector2& xy, const float z) noexcept + [[nodiscard]] constexpr Vector3::Vector3(const Vector2& xy, const f32 z) noexcept : x{xy.x}, y{xy.y}, z{z} { } - [[nodiscard]] constexpr Vector3::Vector3(const float x, const Vector2& yz) noexcept + [[nodiscard]] constexpr Vector3::Vector3(const f32 x, const Vector2& yz) noexcept : x{x}, y{yz.x}, z{yz.y} { } [[nodiscard]] constexpr Vector3::Vector3(const Vector4& xyz) noexcept : x{xyz.x}, y{xyz.y}, z{xyz.z} { } // static - [[nodiscard]] constexpr Vector3 Vector3::x_axis(const float x) noexcept { + [[nodiscard]] constexpr Vector3 Vector3::x_axis(const f32 x) noexcept { return { x, 0.0f, 0.0f }; } - [[nodiscard]] constexpr Vector3 Vector3::y_axis(const float y) noexcept { + [[nodiscard]] constexpr Vector3 Vector3::y_axis(const f32 y) noexcept { return { 0.0f, y, 0.0f }; } - [[nodiscard]] constexpr Vector3 Vector3::z_axis(const float z) noexcept { + [[nodiscard]] constexpr Vector3 Vector3::z_axis(const f32 z) noexcept { return { 0.0f, 0.0f, z }; } - [[nodiscard]] Vector3 Vector3::spherical(const float azimuth, const float inclination, const float radius) noexcept { - const float sin_incl = radius * std::sin(inclination); + [[nodiscard]] Vector3 Vector3::spherical(const f32 azimuth, const f32 inclination, const f32 radius) noexcept { + const f32 sin_incl = radius * std::sin(inclination); return { sin_incl * std::cos(azimuth), radius * std::cos(inclination), sin_incl * std::sin(azimuth) }; } - [[nodiscard]] Vector3 Vector3::cylindrical(const float angle, const float radius, const float height) noexcept { + [[nodiscard]] Vector3 Vector3::cylindrical(const f32 angle, const f32 radius, const f32 height) noexcept { return { radius * std::cos(angle), height, radius * std::sin(angle) }; } // element access - [[nodiscard]] constexpr float& Vector3::operator[](const int i) noexcept { + [[nodiscard]] constexpr f32& Vector3::operator[](const i32 i) noexcept { if consteval { switch (i) { case 0: return x; @@ -75,7 +76,7 @@ export namespace draco::math { } else { return (&x)[i]; } } - [[nodiscard]] constexpr const float& Vector3::operator[](const int i) const noexcept { + [[nodiscard]] constexpr const f32& Vector3::operator[](const i32 i) const noexcept { if consteval { switch (i) { case 0: return x; @@ -87,7 +88,7 @@ export namespace draco::math { } // swizzle - [[nodiscard]] constexpr Vector2 Vector3::operator[](const int i0, const int i1) noexcept { + [[nodiscard]] constexpr Vector2 Vector3::operator[](const i32 i0, const i32 i1) noexcept { if consteval { return { select(i0, x, y, z), select(i1, x, y, z) }; } else { @@ -95,7 +96,7 @@ export namespace draco::math { } } - [[nodiscard]] constexpr Vector2 Vector3::operator[](const int i0, const int i1) const noexcept { + [[nodiscard]] constexpr Vector2 Vector3::operator[](const i32 i0, const i32 i1) const noexcept { if consteval { return { select(i0, x, y, z), select(i1, x, y, z) }; } else { @@ -103,7 +104,7 @@ export namespace draco::math { } } - [[nodiscard]] constexpr Vector3 Vector3::operator[](const int i0, const int i1, const int i2) noexcept { + [[nodiscard]] constexpr Vector3 Vector3::operator[](const i32 i0, const i32 i1, const i32 i2) noexcept { if consteval { return { select(i0, x, y, z), select(i1, x, y, z), select(i2, x, y, z) }; } else { @@ -111,7 +112,7 @@ export namespace draco::math { } } - [[nodiscard]] constexpr Vector3 Vector3::operator[](const int i0, const int i1, const int i2) const noexcept { + [[nodiscard]] constexpr Vector3 Vector3::operator[](const i32 i0, const i32 i1, const i32 i2) const noexcept { if consteval { return { select(i0, x, y, z), select(i1, x, y, z), select(i2, x, y, z) }; } else { @@ -119,7 +120,7 @@ export namespace draco::math { } } - [[nodiscard]] constexpr Vector4 Vector3::operator[](const int i0, const int i1, const int i2, const int i3) noexcept { + [[nodiscard]] constexpr Vector4 Vector3::operator[](const i32 i0, const i32 i1, const i32 i2, const i32 i3) noexcept { if consteval { return { select(i0, x, y, z), select(i1, x, y, z), select(i2, x, y, z), select(i3, x, y, z) }; } else { @@ -127,7 +128,7 @@ export namespace draco::math { } } - [[nodiscard]] constexpr Vector4 Vector3::operator[](const int i0, const int i1, const int i2, const int i3) const noexcept { + [[nodiscard]] constexpr Vector4 Vector3::operator[](const i32 i0, const i32 i1, const i32 i2, const i32 i3) const noexcept { if consteval { return { select(i0, x, y, z), select(i1, x, y, z), select(i2, x, y, z), select(i3, x, y, z) }; } else { @@ -143,7 +144,7 @@ export namespace draco::math { return *this; } - constexpr Vector3& Vector3::operator+=(const float other) noexcept { + constexpr Vector3& Vector3::operator+=(const f32 other) noexcept { x += other; y += other; z += other; @@ -157,7 +158,7 @@ export namespace draco::math { return *this; } - constexpr Vector3& Vector3::operator-=(const float other) noexcept { + constexpr Vector3& Vector3::operator-=(const f32 other) noexcept { x -= other; y -= other; z -= other; @@ -171,7 +172,7 @@ export namespace draco::math { return *this; } - constexpr Vector3& Vector3::operator*=(const float other) noexcept { + constexpr Vector3& Vector3::operator*=(const f32 other) noexcept { x *= other; y *= other; z *= other; @@ -185,15 +186,15 @@ export namespace draco::math { return *this; } - constexpr Vector3& Vector3::operator/=(const float other) noexcept { - const float inv = 1.0f / other; + constexpr Vector3& Vector3::operator/=(const f32 other) noexcept { + const f32 inv = 1.0f / other; x *= inv; y *= inv; z *= inv; return *this; } - constexpr Vector3& Vector3::operator=(const float other) noexcept { + constexpr Vector3& Vector3::operator=(const f32 other) noexcept { x = other; y = other; z = other; @@ -212,11 +213,11 @@ export namespace draco::math { return { a.x+b.x, a.y+b.y, a.z+b.z }; } - [[nodiscard]] constexpr Vector3 operator+(const Vector3& a, const float b) noexcept { + [[nodiscard]] constexpr Vector3 operator+(const Vector3& a, const f32 b) noexcept { return { a.x+b, a.y+b, a.z+b }; } - [[nodiscard]] constexpr Vector3 operator+(const float a, const Vector3& b) noexcept { + [[nodiscard]] constexpr Vector3 operator+(const f32 a, const Vector3& b) noexcept { return b+a; } @@ -224,11 +225,11 @@ export namespace draco::math { return { a.x-b.x, a.y-b.y, a.z-b.z }; } - [[nodiscard]] constexpr Vector3 operator-(const Vector3& a, const float b) noexcept { + [[nodiscard]] constexpr Vector3 operator-(const Vector3& a, const f32 b) noexcept { return { a.x-b, a.y-b, a.z-b }; } - [[nodiscard]] constexpr Vector3 operator-(const float a, const Vector3& b) noexcept { + [[nodiscard]] constexpr Vector3 operator-(const f32 a, const Vector3& b) noexcept { return { a-b.x, a-b.y, a-b.z }; } @@ -236,11 +237,11 @@ export namespace draco::math { return { a.x*b.x, a.y*b.y, a.z*b.z }; } - [[nodiscard]] constexpr Vector3 operator*(const Vector3& a, const float b) noexcept { + [[nodiscard]] constexpr Vector3 operator*(const Vector3& a, const f32 b) noexcept { return { a.x*b, a.y*b, a.z*b }; } - [[nodiscard]] constexpr Vector3 operator*(const float a, const Vector3& b) noexcept { + [[nodiscard]] constexpr Vector3 operator*(const f32 a, const Vector3& b) noexcept { return b*a; } @@ -248,44 +249,44 @@ export namespace draco::math { return { a.x/b.x, a.y/b.y, a.z/b.z }; } - [[nodiscard]] constexpr Vector3 operator/(const Vector3& a, const float b) noexcept { + [[nodiscard]] constexpr Vector3 operator/(const Vector3& a, const f32 b) noexcept { return a * (1.0f / b); } - [[nodiscard]] constexpr Vector3 operator/(const float a, const Vector3& b) noexcept { + [[nodiscard]] constexpr Vector3 operator/(const f32 a, const Vector3& b) noexcept { return { a/b.x, a/b.y, a/b.z }; } // functions // Returns dot product - [[nodiscard]] constexpr float dot(const Vector3& a, const Vector3& b) noexcept { + [[nodiscard]] constexpr f32 dot(const Vector3& a, const Vector3& b) noexcept { return a.x*b.x + a.y*b.y + a.z*b.z; } // Returns squared magnitude - [[nodiscard]] constexpr float length_sq(const Vector3& v) noexcept { + [[nodiscard]] constexpr f32 length_sq(const Vector3& v) noexcept { return dot(v, v); } // Returns magnitude - [[nodiscard]] float length(const Vector3& v) noexcept { + [[nodiscard]] f32 length(const Vector3& v) noexcept { return std::sqrt(length_sq(v)); } // Return squared distance between two vectors - [[nodiscard]] constexpr float distance_sq(const Vector3& a, const Vector3& b) noexcept { + [[nodiscard]] constexpr f32 distance_sq(const Vector3& a, const Vector3& b) noexcept { return length_sq(a - b); } // Returns distance between two vectors - [[nodiscard]] float distance(const Vector3& a, const Vector3& b) noexcept { + [[nodiscard]] f32 distance(const Vector3& a, const Vector3& b) noexcept { return length(a - b); } // Safe normalize, checks length [[nodiscard]] Vector3 normalize(const Vector3& v) noexcept { - const float len = length(v); + const f32 len = length(v); return (len > CMP_NORMALIZE_TOLERANCE) ? v / len : Vector3(); } @@ -307,12 +308,12 @@ export namespace draco::math { } // Returns the angle between two vectors - [[nodiscard]] float angle(const Vector3& a, const Vector3& b) noexcept { + [[nodiscard]] f32 angle(const Vector3& a, const Vector3& b) noexcept { return std::acos(dot(a, b) / (length(a) * length(b))); } // Returns linear interpolation between two vectors - [[nodiscard]] constexpr Vector3 lerp(const Vector3& from, const Vector3& to, const float weight) noexcept { + [[nodiscard]] constexpr Vector3 lerp(const Vector3& from, const Vector3& to, const f32 weight) noexcept { return { lerp(from.x, to.x, weight), lerp(from.y, to.y, weight), @@ -329,7 +330,7 @@ export namespace draco::math { }; } - [[nodiscard]] constexpr Vector3 min(const Vector3& a, const float b) noexcept { + [[nodiscard]] constexpr Vector3 min(const Vector3& a, const f32 b) noexcept { return { std::min(a.x, b), std::min(a.y, b), @@ -337,7 +338,7 @@ export namespace draco::math { }; } - [[nodiscard]] constexpr Vector3 min(const float a, const Vector3& b) noexcept { + [[nodiscard]] constexpr Vector3 min(const f32 a, const Vector3& b) noexcept { return min(b, a); } @@ -347,8 +348,8 @@ export namespace draco::math { } // Returns a vector in the same direction whose length is bounded above by the given value. - [[nodiscard]] Vector3 min_length(const Vector3& a, const float b) noexcept { - const float len_sq = length_sq(a); + [[nodiscard]] Vector3 min_length(const Vector3& a, const f32 b) noexcept { + const f32 len_sq = length_sq(a); if (len_sq > b * b) { return a * (b / std::sqrt(len_sq)); @@ -357,7 +358,7 @@ export namespace draco::math { } } - [[nodiscard]] Vector3 min_length(const float a, const Vector3& b) noexcept { + [[nodiscard]] Vector3 min_length(const f32 a, const Vector3& b) noexcept { return min_length(b, a); } @@ -370,7 +371,7 @@ export namespace draco::math { }; } - [[nodiscard]] constexpr Vector3 max(const Vector3& a, const float b) noexcept { + [[nodiscard]] constexpr Vector3 max(const Vector3& a, const f32 b) noexcept { return { std::max(a.x, b), std::max(a.y, b), @@ -378,7 +379,7 @@ export namespace draco::math { }; } - [[nodiscard]] constexpr Vector3 max(const float a, const Vector3& b) noexcept { + [[nodiscard]] constexpr Vector3 max(const f32 a, const Vector3& b) noexcept { return max(b, a); } @@ -388,8 +389,8 @@ export namespace draco::math { } // Returns a vector in the same direction whose length is bounded below by the given value. Returns the 0 vector if the vector is too small to be normalized. - [[nodiscard]] Vector3 max_length(const Vector3& a, const float b) noexcept { - const float len_sq = length_sq(a); + [[nodiscard]] Vector3 max_length(const Vector3& a, const f32 b) noexcept { + const f32 len_sq = length_sq(a); if (len_sq <= CMP_NORMALIZE_TOLERANCE2) { return Vector3(); @@ -400,7 +401,7 @@ export namespace draco::math { } } - [[nodiscard]] Vector3 max_length(const float a, const Vector3& b) noexcept { + [[nodiscard]] Vector3 max_length(const f32 a, const Vector3& b) noexcept { return max_length(b, a); } @@ -409,13 +410,13 @@ export namespace draco::math { return max(x_min, min(x, x_max)); } - [[nodiscard]] constexpr Vector3 clamp(const Vector3& x, const float x_min, const float x_max) noexcept { + [[nodiscard]] constexpr Vector3 clamp(const Vector3& x, const f32 x_min, const f32 x_max) noexcept { return max(x_min, min(x, x_max)); } // Clamps the length of the vector to the range [x_min, x_max]. Presupposes x_min <= x_max. Returns the 0 vector if the vector is too small to be normalized. - [[nodiscard]] Vector3 clamp_length(const Vector3& v, const float x_min, const float x_max) noexcept { - const float len_sq = length_sq(v); + [[nodiscard]] Vector3 clamp_length(const Vector3& v, const f32 x_min, const f32 x_max) noexcept { + const f32 len_sq = length_sq(v); if (len_sq <= CMP_NORMALIZE_TOLERANCE2) { return Vector3(); @@ -494,13 +495,13 @@ export namespace draco::math { } export namespace std { - template<> struct formatter : formatter { + template<> struct formatter : formatter { auto format(const draco::math::Vector3& v, format_context& ctx) const { ctx.advance_to(format_to(ctx.out(), "{{")); - for (int i = 0; i < 3; ++i) { + for (draco::i32 i = 0; i < 3; ++i) { if (i) ctx.advance_to(format_to(ctx.out(), ", ")); - ctx.advance_to(formatter::format(v[i], ctx)); + ctx.advance_to(formatter::format(v[i], ctx)); } return format_to(ctx.out(), "}}"); diff --git a/engine/native/core/math/vector4.cppm b/engine/native/core/math/vector4.cppm index fce1cbc..d2ddcd4 100644 --- a/engine/native/core/math/vector4.cppm +++ b/engine/native/core/math/vector4.cppm @@ -17,6 +17,7 @@ export import :common; import core.math.constants; import core.math.functions; import core.defs; +import core.stdtypes; export namespace draco::math { // assertions @@ -26,52 +27,52 @@ export namespace draco::math { static_assert(std::is_standard_layout_v, "Vector4 must be standard layout"); // constructors - [[nodiscard]] constexpr Vector4::Vector4(const float n) noexcept + [[nodiscard]] constexpr Vector4::Vector4(const f32 n) noexcept : x{n}, y{n}, z{n}, w{n} { } - [[nodiscard]] constexpr Vector4::Vector4(const float x, const float y, const float z, const float w) noexcept + [[nodiscard]] constexpr Vector4::Vector4(const f32 x, const f32 y, const f32 z, const f32 w) noexcept : x{x}, y{y}, z{z}, w{w} { } [[nodiscard]] constexpr Vector4::Vector4(const Vector2& xy) noexcept : x{xy.x}, y{xy.y}, z{0.0f}, w{0.0f} { } - [[nodiscard]] constexpr Vector4::Vector4(const Vector2& xy, const float z, const float w) noexcept + [[nodiscard]] constexpr Vector4::Vector4(const Vector2& xy, const f32 z, const f32 w) noexcept : x{xy.x}, y{xy.y}, z{z}, w{w} { } - [[nodiscard]] constexpr Vector4::Vector4(const float x, const Vector2& yz, const float w) noexcept + [[nodiscard]] constexpr Vector4::Vector4(const f32 x, const Vector2& yz, const f32 w) noexcept : x{x}, y{yz.x}, z{yz.y}, w{w} { } - [[nodiscard]] constexpr Vector4::Vector4(const float x, const float y, const Vector2& zw) noexcept + [[nodiscard]] constexpr Vector4::Vector4(const f32 x, const f32 y, const Vector2& zw) noexcept : x{x}, y{y}, z{zw.x}, w{zw.y} { } [[nodiscard]] constexpr Vector4::Vector4(const Vector2& xy, const Vector2& zw) noexcept : x{xy.x}, y{xy.y}, z{zw.x}, w{zw.y} { } - [[nodiscard]] constexpr Vector4::Vector4(const Vector3& xyz, const float w) noexcept + [[nodiscard]] constexpr Vector4::Vector4(const Vector3& xyz, const f32 w) noexcept : x{xyz.x}, y{xyz.y}, z{xyz.z}, w{w} { } - [[nodiscard]] constexpr Vector4::Vector4(const float x, const Vector3& yzw) noexcept + [[nodiscard]] constexpr Vector4::Vector4(const f32 x, const Vector3& yzw) noexcept : x{x}, y{yzw.x}, z{yzw.y}, w{yzw.z} { } // static - [[nodiscard]] constexpr Vector4 Vector4::x_axis(const float x) noexcept { + [[nodiscard]] constexpr Vector4 Vector4::x_axis(const f32 x) noexcept { return { x, 0.0f, 0.0f, 0.0f }; } - [[nodiscard]] constexpr Vector4 Vector4::y_axis(const float y) noexcept { + [[nodiscard]] constexpr Vector4 Vector4::y_axis(const f32 y) noexcept { return { 0.0f, y, 0.0f, 0.0f }; } - [[nodiscard]] constexpr Vector4 Vector4::z_axis(const float z) noexcept { + [[nodiscard]] constexpr Vector4 Vector4::z_axis(const f32 z) noexcept { return { 0.0f, 0.0f, z, 0.0f }; } - [[nodiscard]] constexpr Vector4 Vector4::w_axis(const float w) noexcept { + [[nodiscard]] constexpr Vector4 Vector4::w_axis(const f32 w) noexcept { return { 0.0f, 0.0f, 0.0f, w }; } // element access - [[nodiscard]] constexpr float& Vector4::operator[](const int i) noexcept { + [[nodiscard]] constexpr f32& Vector4::operator[](const i32 i) noexcept { if consteval { switch (i) { case 0: return x; @@ -83,7 +84,7 @@ export namespace draco::math { } else { return (&x)[i]; } } - [[nodiscard]] constexpr const float& Vector4::operator[](const int i) const noexcept { + [[nodiscard]] constexpr const f32& Vector4::operator[](const i32 i) const noexcept { if consteval { switch (i) { case 0: return x; @@ -96,7 +97,7 @@ export namespace draco::math { } // swizzle - [[nodiscard]] constexpr Vector2 Vector4::operator[](const int i0, const int i1) noexcept { + [[nodiscard]] constexpr Vector2 Vector4::operator[](const i32 i0, const i32 i1) noexcept { if consteval { return { select(i0, x, y, z, w), select(i1, x, y, z, w) }; } else { @@ -104,7 +105,7 @@ export namespace draco::math { } } - [[nodiscard]] constexpr Vector2 Vector4::operator[](const int i0, const int i1) const noexcept { + [[nodiscard]] constexpr Vector2 Vector4::operator[](const i32 i0, const i32 i1) const noexcept { if consteval { return { select(i0, x, y, z, w), select(i1, x, y, z, w) }; } else { @@ -112,7 +113,7 @@ export namespace draco::math { } } - [[nodiscard]] constexpr Vector3 Vector4::operator[](const int i0, const int i1, const int i2) noexcept { + [[nodiscard]] constexpr Vector3 Vector4::operator[](const i32 i0, const i32 i1, const i32 i2) noexcept { if consteval { return { select(i0, x, y, z, w), select(i1, x, y, z, w), select(i2, x, y, z, w) }; } else { @@ -120,7 +121,7 @@ export namespace draco::math { } } - [[nodiscard]] constexpr Vector3 Vector4::operator[](const int i0, const int i1, const int i2) const noexcept { + [[nodiscard]] constexpr Vector3 Vector4::operator[](const i32 i0, const i32 i1, const i32 i2) const noexcept { if consteval { return { select(i0, x, y, z, w), select(i1, x, y, z, w), select(i2, x, y, z, w) }; } else { @@ -128,7 +129,7 @@ export namespace draco::math { } } - [[nodiscard]] constexpr Vector4 Vector4::operator[](const int i0, const int i1, const int i2, const int i3) noexcept { + [[nodiscard]] constexpr Vector4 Vector4::operator[](const i32 i0, const i32 i1, const i32 i2, const i32 i3) noexcept { if consteval { return { select(i0, x, y, z, w), select(i1, x, y, z, w), select(i2, x, y, z, w), select(i3, x, y, z, w) }; } else { @@ -136,7 +137,7 @@ export namespace draco::math { } } - [[nodiscard]] constexpr Vector4 Vector4::operator[](const int i0, const int i1, const int i2, const int i3) const noexcept { + [[nodiscard]] constexpr Vector4 Vector4::operator[](const i32 i0, const i32 i1, const i32 i2, const i32 i3) const noexcept { if consteval { return { select(i0, x, y, z, w), select(i1, x, y, z, w), select(i2, x, y, z, w), select(i3, x, y, z, w) }; } else { @@ -153,7 +154,7 @@ export namespace draco::math { return *this; } - constexpr Vector4& Vector4::operator+=(const float other) noexcept { + constexpr Vector4& Vector4::operator+=(const f32 other) noexcept { x += other; y += other; z += other; @@ -169,7 +170,7 @@ export namespace draco::math { return *this; } - constexpr Vector4& Vector4::operator-=(const float other) noexcept { + constexpr Vector4& Vector4::operator-=(const f32 other) noexcept { x -= other; y -= other; z -= other; @@ -185,7 +186,7 @@ export namespace draco::math { return *this; } - constexpr Vector4& Vector4::operator*=(const float other) noexcept { + constexpr Vector4& Vector4::operator*=(const f32 other) noexcept { x *= other; y *= other; z *= other; @@ -201,8 +202,8 @@ export namespace draco::math { return *this; } - constexpr Vector4& Vector4::operator/=(const float other) noexcept { - const float inv = 1.0f / other; + constexpr Vector4& Vector4::operator/=(const f32 other) noexcept { + const f32 inv = 1.0f / other; x *= inv; y *= inv; z *= inv; @@ -210,7 +211,7 @@ export namespace draco::math { return *this; } - constexpr Vector4& Vector4::operator=(const float other) noexcept { + constexpr Vector4& Vector4::operator=(const f32 other) noexcept { x = other; y = other; z = other; @@ -230,11 +231,11 @@ export namespace draco::math { return { a.x+b.x, a.y+b.y, a.z+b.z, a.w+b.w }; } - [[nodiscard]] constexpr Vector4 operator+(const Vector4& a, const float b) noexcept { + [[nodiscard]] constexpr Vector4 operator+(const Vector4& a, const f32 b) noexcept { return { a.x+b, a.y+b, a.z+b, a.w+b }; } - [[nodiscard]] constexpr Vector4 operator+(const float a, const Vector4& b) noexcept { + [[nodiscard]] constexpr Vector4 operator+(const f32 a, const Vector4& b) noexcept { return b+a; } @@ -242,11 +243,11 @@ export namespace draco::math { return { a.x-b.x, a.y-b.y, a.z-b.z, a.w-b.w }; } - [[nodiscard]] constexpr Vector4 operator-(const Vector4& a, const float b) noexcept { + [[nodiscard]] constexpr Vector4 operator-(const Vector4& a, const f32 b) noexcept { return { a.x-b, a.y-b, a.z-b, a.w-b }; } - [[nodiscard]] constexpr Vector4 operator-(const float a, const Vector4& b) noexcept { + [[nodiscard]] constexpr Vector4 operator-(const f32 a, const Vector4& b) noexcept { return { a-b.x, a-b.y, a-b.z, a-b.w }; } @@ -254,11 +255,11 @@ export namespace draco::math { return { a.x*b.x, a.y*b.y, a.z*b.z, a.w*b.w }; } - [[nodiscard]] constexpr Vector4 operator*(const Vector4& a, const float b) noexcept { + [[nodiscard]] constexpr Vector4 operator*(const Vector4& a, const f32 b) noexcept { return { a.x*b, a.y*b, a.z*b, a.w*b }; } - [[nodiscard]] constexpr Vector4 operator*(const float a, const Vector4& b) noexcept { + [[nodiscard]] constexpr Vector4 operator*(const f32 a, const Vector4& b) noexcept { return b*a; } @@ -266,18 +267,18 @@ export namespace draco::math { return { a.x/b.x, a.y/b.y, a.z/b.z, a.w/b.w }; } - [[nodiscard]] constexpr Vector4 operator/(const Vector4& a, const float b) noexcept { + [[nodiscard]] constexpr Vector4 operator/(const Vector4& a, const f32 b) noexcept { return a * (1.0f / b); } - [[nodiscard]] constexpr Vector4 operator/(const float a, const Vector4& b) noexcept { + [[nodiscard]] constexpr Vector4 operator/(const f32 a, const Vector4& b) noexcept { return { a/b.x, a/b.y, a/b.z, a/b.w }; } // functions // Returns dot product - [[nodiscard]] constexpr float dot(const Vector4& a, const Vector4& b) noexcept { + [[nodiscard]] constexpr f32 dot(const Vector4& a, const Vector4& b) noexcept { if !consteval { #if ARCH_X64 // There's only 4 floats, so SSE is what we will use. @@ -306,28 +307,28 @@ export namespace draco::math { } // Returns squared magnitude - [[nodiscard]] constexpr float length_sq(const Vector4& v) noexcept { + [[nodiscard]] constexpr f32 length_sq(const Vector4& v) noexcept { return dot(v, v); } // Returns magnitude - [[nodiscard]] float length(const Vector4& v) noexcept { + [[nodiscard]] f32 length(const Vector4& v) noexcept { return std::sqrt(length_sq(v)); } // Return squared distance between two vectors - [[nodiscard]] constexpr float distance_sq(const Vector4& a, const Vector4& b) noexcept { + [[nodiscard]] constexpr f32 distance_sq(const Vector4& a, const Vector4& b) noexcept { return length_sq(a - b); } // Returns distance between two vectors - [[nodiscard]] float distance(const Vector4& a, const Vector4& b) noexcept { + [[nodiscard]] f32 distance(const Vector4& a, const Vector4& b) noexcept { return length(a - b); } // Safe normalize, checks length [[nodiscard]] Vector4 normalize(const Vector4& v) noexcept { - const float len = length(v); + const f32 len = length(v); return (len > CMP_NORMALIZE_TOLERANCE) ? v / len : Vector4(); } @@ -349,12 +350,12 @@ export namespace draco::math { } // Returns the angle between two vectors - [[nodiscard]] float angle(const Vector4& a, const Vector4& b) noexcept { + [[nodiscard]] f32 angle(const Vector4& a, const Vector4& b) noexcept { return std::acos(dot(a, b) / (length(a) * length(b))); } // Returns linear interpolation between two vectors - [[nodiscard]] constexpr Vector4 lerp(const Vector4& from, const Vector4& to, const float weight) noexcept { + [[nodiscard]] constexpr Vector4 lerp(const Vector4& from, const Vector4& to, const f32 weight) noexcept { return { lerp(from.x, to.x, weight), lerp(from.y, to.y, weight), @@ -373,7 +374,7 @@ export namespace draco::math { }; } - [[nodiscard]] constexpr Vector4 min(const Vector4& a, const float b) noexcept { + [[nodiscard]] constexpr Vector4 min(const Vector4& a, const f32 b) noexcept { return { std::min(a.x, b), std::min(a.y, b), @@ -382,7 +383,7 @@ export namespace draco::math { }; } - [[nodiscard]] constexpr Vector4 min(const float a, const Vector4& b) noexcept { + [[nodiscard]] constexpr Vector4 min(const f32 a, const Vector4& b) noexcept { return min(b, a); } @@ -392,8 +393,8 @@ export namespace draco::math { } // Returns a vector in the same direction whose length is bounded above by the given value. - [[nodiscard]] Vector4 min_length(const Vector4& a, const float b) noexcept { - const float len_sq = length_sq(a); + [[nodiscard]] Vector4 min_length(const Vector4& a, const f32 b) noexcept { + const f32 len_sq = length_sq(a); if (len_sq > b * b) { return a * (b / std::sqrt(len_sq)); @@ -402,7 +403,7 @@ export namespace draco::math { } } - [[nodiscard]] Vector4 min_length(const float a, const Vector4& b) noexcept { + [[nodiscard]] Vector4 min_length(const f32 a, const Vector4& b) noexcept { return min_length(b, a); } @@ -416,7 +417,7 @@ export namespace draco::math { }; } - [[nodiscard]] constexpr Vector4 max(const Vector4& a, const float b) noexcept { + [[nodiscard]] constexpr Vector4 max(const Vector4& a, const f32 b) noexcept { return { std::max(a.x, b), std::max(a.y, b), @@ -425,7 +426,7 @@ export namespace draco::math { }; } - [[nodiscard]] constexpr Vector4 max(const float a, const Vector4& b) noexcept { + [[nodiscard]] constexpr Vector4 max(const f32 a, const Vector4& b) noexcept { return max(b, a); } @@ -435,8 +436,8 @@ export namespace draco::math { } // Returns a vector in the same direction whose length is bounded below by the given value. Returns the 0 vector if the vector is too small to be normalized. - [[nodiscard]] Vector4 max_length(const Vector4& a, const float b) noexcept { - const float len_sq = length_sq(a); + [[nodiscard]] Vector4 max_length(const Vector4& a, const f32 b) noexcept { + const f32 len_sq = length_sq(a); if (len_sq <= CMP_NORMALIZE_TOLERANCE2) { return Vector4(); @@ -447,7 +448,7 @@ export namespace draco::math { } } - [[nodiscard]] Vector4 max_length(const float a, const Vector4& b) noexcept { + [[nodiscard]] Vector4 max_length(const f32 a, const Vector4& b) noexcept { return max_length(b, a); } @@ -456,13 +457,13 @@ export namespace draco::math { return max(x_min, min(x, x_max)); } - [[nodiscard]] constexpr Vector4 clamp(const Vector4& x, const float x_min, const float x_max) noexcept { + [[nodiscard]] constexpr Vector4 clamp(const Vector4& x, const f32 x_min, const f32 x_max) noexcept { return max(x_min, min(x, x_max)); } // Clamps the length of the vector to the range [x_min, x_max]. Presupposes x_min <= x_max. Returns the 0 vector if the vector is too small to be normalized. - [[nodiscard]] Vector4 clamp_length(const Vector4& v, const float x_min, const float x_max) noexcept { - const float len_sq = length_sq(v); + [[nodiscard]] Vector4 clamp_length(const Vector4& v, const f32 x_min, const f32 x_max) noexcept { + const f32 len_sq = length_sq(v); if (len_sq <= CMP_NORMALIZE_TOLERANCE2) { return Vector4(); @@ -542,13 +543,13 @@ export namespace draco::math { } // namespace draco::math export namespace std { - template<> struct formatter : formatter { + template<> struct formatter : formatter { auto format(const draco::math::Vector4& v, format_context& ctx) const { ctx.advance_to(format_to(ctx.out(), "{{")); - for (int i = 0; i < 4; ++i) { + for (draco::i32 i = 0; i < 4; ++i) { if (i) ctx.advance_to(format_to(ctx.out(), ", ")); - ctx.advance_to(formatter::format(v[i], ctx)); + ctx.advance_to(formatter::format(v[i], ctx)); } return format_to(ctx.out(), "}}"); diff --git a/engine/native/core/memory/allocator.cpp b/engine/native/core/memory/allocator.cpp index 9d74a64..015b6fc 100644 --- a/engine/native/core/memory/allocator.cpp +++ b/engine/native/core/memory/allocator.cpp @@ -1,16 +1,15 @@ module; -#include - module core.memory.allocator; +import core.stdtypes; namespace draco::memory { Error nilAlloc( Allocator alloc, Slice *dst, - size_t size, - size_t align + usize size, + usize align ) { return Error::NotImplemented; @@ -26,7 +25,7 @@ namespace draco::memory return Error::NotImplemented; } - void asAllocatorVoid(Allocator *dst, void *alloc, AllocatorVTbl *vtbl) + void asAllocatorVoid(Allocator *dst, rawptr alloc, AllocatorVTbl *vtbl) { dst->allocatorData = (void*)alloc; dst->vtbl = vtbl; diff --git a/engine/native/core/memory/allocator.cppm b/engine/native/core/memory/allocator.cppm index 2298aef..1939a89 100644 --- a/engine/native/core/memory/allocator.cppm +++ b/engine/native/core/memory/allocator.cppm @@ -1,9 +1,8 @@ module; -#include - export module core.memory.allocator; export import core.memory.slice; +export import core.stdtypes; export namespace draco::memory { @@ -30,8 +29,8 @@ export namespace draco::memory using AllocFn = Error (*)( Allocator alloc, Slice *dst, - size_t size, - size_t align + usize size, + usize align ); using FreeFn = Error (*)(Allocator alloc, Slice block); using FreeAllFn = Error (*)(Allocator alloc); @@ -43,13 +42,13 @@ export namespace draco::memory Error nilAlloc( Allocator alloc, Slice *dst, - size_t size, - size_t align + usize size, + usize align ); Error nilFree(Allocator alloc, Slice block); Error nilFreeAll(Allocator alloc); - void asAllocatorVoid(Allocator *dst, void *alloc, AllocatorVTbl *vtbl); + void asAllocatorVoid(Allocator *dst, rawptr alloc, AllocatorVTbl *vtbl); } diff --git a/engine/native/core/memory/bumpAllocator.cpp b/engine/native/core/memory/bumpAllocator.cpp index b88012f..3d8c582 100644 --- a/engine/native/core/memory/bumpAllocator.cpp +++ b/engine/native/core/memory/bumpAllocator.cpp @@ -2,11 +2,10 @@ module; #include #include -#include -#include #include module core.memory.bumpAllocator; +import core.stdtypes; namespace draco::memory::bump { @@ -14,7 +13,7 @@ namespace draco::memory::bump BumpAllocator *alloc, Allocator baseAlloc, // one page by default on unix-like systems - size_t minAllocRequest + usize minAllocRequest ) { memset(alloc, 0, sizeof(BumpAllocator)); @@ -40,19 +39,19 @@ namespace draco::memory::bump } } - Error alloc(Allocator alloc, Slice *dst, size_t size, size_t align) + Error alloc(Allocator alloc, Slice *dst, usize size, usize align) { Error err; BumpAllocator *allocData = (BumpAllocator *)alloc.allocatorData; - uintptr_t alignMask = align - 1; + uintptr alignMask = align - 1; Node **lastNode; Node **node = &(allocData->first); - size_t pos = allocData->allocated; - size_t oldPos = pos; - size_t reqSize = size; - size_t spillover = 0; + usize pos = allocData->allocated; + usize oldPos = pos; + usize reqSize = size; + usize spillover = 0; Slice newBlock; - uintptr_t currentPtr; + uintptr currentPtr; assert(std::popcount(align) == 1); lastNode = node; while (((*node) != nullptr) & (pos > 0)) @@ -63,7 +62,7 @@ namespace draco::memory::bump node = &((*node)->next); } assert(pos == 0); // fraudulent mark provided - currentPtr = ((uintptr_t)(*lastNode)) + sizeof(Node) + oldPos; + currentPtr = ((uintptr)(*lastNode)) + sizeof(Node) + oldPos; reqSize = size + ((align - (currentPtr & alignMask)) & alignMask); if (!(*lastNode) || (reqSize > ((*lastNode)->size - oldPos))) { @@ -89,7 +88,7 @@ namespace draco::memory::bump lastNode = node; oldPos = 0; } - currentPtr = ((uintptr_t)&((*lastNode)->data[oldPos])); + currentPtr = ((uintptr)&((*lastNode)->data[oldPos])); reqSize = size + ((align - (currentPtr & alignMask)) & alignMask); currentPtr = (currentPtr + alignMask) & ~alignMask; allocData->allocated += reqSize + spillover; @@ -105,12 +104,12 @@ namespace draco::memory::bump return Error::Okay; } - size_t saveMark(BumpAllocator *self) + usize saveMark(BumpAllocator *self) { return self->allocated; } - void resumeMark(BumpAllocator *self, size_t mark) + void resumeMark(BumpAllocator *self, usize mark) { self->allocated = mark; } diff --git a/engine/native/core/memory/bumpAllocator.cppm b/engine/native/core/memory/bumpAllocator.cppm index 7923768..73a2534 100644 --- a/engine/native/core/memory/bumpAllocator.cppm +++ b/engine/native/core/memory/bumpAllocator.cppm @@ -1,13 +1,11 @@ module; #include -#include -#include -#include export module core.memory.bumpAllocator; export import core.memory.allocator; export import core.memory.slice; +export import core.stdtypes; export namespace draco::memory { @@ -16,28 +14,28 @@ export namespace draco::memory struct Node { Node *next; - size_t size; - uint8_t data[]; + usize size; + u8 data[]; }; struct BumpAllocator { Allocator base; Node *first; - size_t minAllocRequest; - size_t allocated; + usize minAllocRequest; + usize allocated; }; void init( BumpAllocator *alloc, Allocator baseAlloc, // one page by default on unix-like systems - size_t minAllocRequest = (1 << 12) + usize minAllocRequest = (1 << 12) ); void deinit(BumpAllocator *alloc); - Error alloc(Allocator alloc, Slice *dst, size_t size, size_t align); + Error alloc(Allocator alloc, Slice *dst, usize size, usize align); Error freeAll(Allocator alloc); @@ -47,9 +45,9 @@ export namespace draco::memory .freeAll = freeAll, }; - size_t saveMark(BumpAllocator *self); + usize saveMark(BumpAllocator *self); - void resumeMark(BumpAllocator *self, size_t mark); + void resumeMark(BumpAllocator *self, usize mark); inline void asAllocator(Allocator *dst, BumpAllocator *alloc) { diff --git a/engine/native/core/memory/fixedAllocator.cpp b/engine/native/core/memory/fixedAllocator.cpp index b276895..f175a07 100644 --- a/engine/native/core/memory/fixedAllocator.cpp +++ b/engine/native/core/memory/fixedAllocator.cpp @@ -2,7 +2,6 @@ module; #include #include -#include #include module core.memory.fixedAllocator; @@ -19,13 +18,13 @@ namespace draco::memory::fixed Error alloc( Allocator alloc, Slice *dst, - size_t size, - size_t align + usize size, + usize align ) { FixedAllocator *allocData = (FixedAllocator*)alloc.allocatorData; - size_t alignMask = align - 1; - size_t alignedSize = allocData->size - ( + usize alignMask = align - 1; + usize alignedSize = allocData->size - ( (align - (((uintptr_t)allocData->buffer) & alignMask)) & alignMask ); diff --git a/engine/native/core/memory/fixedAllocator.cppm b/engine/native/core/memory/fixedAllocator.cppm index f6658b0..afc48c1 100644 --- a/engine/native/core/memory/fixedAllocator.cppm +++ b/engine/native/core/memory/fixedAllocator.cppm @@ -1,12 +1,12 @@ module; #include -#include #include export module core.memory.fixedAllocator; export import core.memory.allocator; export import core.memory.slice; +export import core.stdtypes; export namespace draco::memory { @@ -15,13 +15,13 @@ export namespace draco::memory struct FixedAllocator { uint8_t *buffer; - size_t size; + usize size; bool allocated; }; void init(FixedAllocator *alloc, Slice block); - Error alloc(Allocator alloc, Slice *dst, size_t size, size_t align); + Error alloc(Allocator alloc, Slice *dst, usize size, usize align); Error freeAll(Allocator alloc); diff --git a/engine/native/core/memory/pageAllocator.cpp b/engine/native/core/memory/pageAllocator.cpp index 1423845..6acd996 100644 --- a/engine/native/core/memory/pageAllocator.cpp +++ b/engine/native/core/memory/pageAllocator.cpp @@ -1,6 +1,5 @@ module; -#include #ifdef __unix__ #include #include @@ -11,6 +10,7 @@ module; #endif module core.memory.pageAllocator; +import core.stdtypes; namespace draco::memory::page { @@ -18,15 +18,15 @@ namespace draco::memory::page Error alloc( Allocator alloc, Slice *dst, - size_t size, - size_t align + usize size, + usize align ) { int pageSizeSub1 = getpagesize() - 1; // Coderabbit, this is for a 64-bit machine with 48-bit addressing, // if this overflows, the request was never going to fit into // memory to begin with. - size_t reqSize = (size + (pageSizeSub1)) & (~pageSizeSub1); + usize reqSize = (size + (pageSizeSub1)) & (~pageSizeSub1); void *ptr = mmap( nullptr, reqSize, @@ -35,7 +35,7 @@ namespace draco::memory::page -1, 0 ); - if (((ptrdiff_t)ptr) == -1) + if (((ptrdiff)ptr) == -1) { return Error::OutOfMemory; } @@ -55,16 +55,16 @@ namespace draco::memory::page Error alloc( Allocator alloc, Slice *dst, - size_t size, - size_t align + usize size, + usize align ) { SYSTEM_INFO sysinfo; - size_t pageSizeSub1; - size_t reqSize; + usize pageSizeSub1; + usize reqSize; void *ptr; GetSystemInfo(&sysinfo); - pageSizeSub1 = (size_t)(sysinfo.dwAllocationGranularity - 1); + pageSizeSub1 = (usize)(sysinfo.dwAllocationGranularity - 1); // Coderabbit, this is for a 64-bit machine with 48-bit addressing, // if this overflows, the request was never going to fit into // memory to begin with. @@ -87,16 +87,16 @@ namespace draco::memory::page Error allocLargePages( Allocator alloc, Slice *dst, - size_t size, - size_t align + usize size, + usize align ) { - size_t pageSize = GetLargePageMinimum(); - size_t pageSizeSub1 = (pageSize ? pageSize : (4 * 1024)) - 1; + usize pageSize = GetLargePageMinimum(); + usize pageSizeSub1 = (pageSize ? pageSize : (4 * 1024)) - 1; // Coderabbit, this is for a 64-bit machine with 48-bit addressing, // if this overflows, the request was never going to fit into // memory to begin with. - size_t reqSize = (size + (pageSizeSub1)) & (~pageSizeSub1); + usize reqSize = (size + (pageSizeSub1)) & (~pageSizeSub1); void *ptr; ptr = VirtualAlloc( nullptr, diff --git a/engine/native/core/memory/pageAllocator.cppm b/engine/native/core/memory/pageAllocator.cppm index cc36b07..0981377 100644 --- a/engine/native/core/memory/pageAllocator.cppm +++ b/engine/native/core/memory/pageAllocator.cppm @@ -1,16 +1,15 @@ module; -#include - export module core.memory.pageAllocator; export import core.memory.allocator; export import core.memory.slice; +export import core.stdtypes; export namespace draco::memory { namespace page { - Error alloc(Allocator alloc, Slice *dst, size_t size, size_t align); + Error alloc(Allocator alloc, Slice *dst, usize size, usize align); Error free(Allocator alloc, Slice block); diff --git a/engine/native/core/memory/slice.cppm b/engine/native/core/memory/slice.cppm index 0a632d1..552e78f 100644 --- a/engine/native/core/memory/slice.cppm +++ b/engine/native/core/memory/slice.cppm @@ -1,14 +1,13 @@ module; -#include - export module core.memory.slice; +export import core.stdtypes; export namespace draco::memory { struct Slice { - void *data; - size_t size; + rawptr data; + usize size; }; }