Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion engine/native/core/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
add_modules_library(definitions PIC)
add_modules_library(math)
add_modules_library(memory)
target_link_libraries(math PUBLIC definitions)
target_link_libraries(math PUBLIC definitions)
target_link_libraries(memory PUBLIC definitions)
4 changes: 2 additions & 2 deletions engine/native/core/definitions/definitions.cppm
Original file line number Diff line number Diff line change
Expand Up @@ -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<typename T>
concept arithmetic = std::is_arithmetic_v<T>;

Expand All @@ -26,4 +26,4 @@ export namespace draco {

template <typename T>
concept zero_constructible = is_zero_constructible_v<T>;
}
}
33 changes: 33 additions & 0 deletions engine/native/core/definitions/stdtypes.cppm
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module;

#include <cstddef>
#include <cstdint>

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;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if ssize would be a better name (like std::ssize_t), but that's not terribly important.
Not worth going back and renaming everything for this.

using usize = std::size_t;

using rawptr = void *;
using uintptr = uintptr_t;
using ptrdiff = ptrdiff_t;

// UTF-32 type
using rune = u32;

} // namespace draco
34 changes: 17 additions & 17 deletions engine/native/core/definitions/version.cppm
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
module;

#include <cstdint>
#include <format>

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};
Comment thread
coderabbitai[bot] marked this conversation as resolved.
} // namespace draco

export namespace std {
template<> struct formatter<draco::Version> {
constexpr auto parse(std::format_parse_context& ctx) {
return ctx.begin(); // Accept any format spec (or parse custom ones)
}
template <> struct formatter<draco::Version> {
constexpr auto parse(std::format_parse_context &ctx) {
return ctx.begin(); // Accept any format spec (or parse custom ones)
}
Comment thread
OldDev78 marked this conversation as resolved.

auto format(const draco::Version& v, std::format_context& ctx) const {
return std::format_to(ctx.out(), "{}.{}.{}", v.major, v.minor, v.patch);
}
};
}
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
43 changes: 22 additions & 21 deletions engine/native/core/math/constants.cppm
Original file line number Diff line number Diff line change
Expand Up @@ -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<float>;
constexpr float SQRT3 = std::numbers::sqrt3_v<float>;
constexpr float SQRT12 = 1. / SQRT2;
constexpr float SQRT13 = std::numbers::inv_sqrt3_v<float>;
constexpr float LN2 = std::numbers::ln2_v<float>;
constexpr float LN10 = std::numbers::ln10_v<float>;
constexpr float PI = std::numbers::pi_v<float>;
constexpr float PI2 = PI * .5;
constexpr float TAU = 2. * PI;
constexpr float E = std::numbers::e_v<float>;
constexpr float INF = std::numeric_limits<float>::infinity();
constexpr float NaN = std::numeric_limits<float>::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<uint32_t>::max();
constexpr float DECIMAL_LIMIT_F = 8388608.0f;
constexpr f32 SQRT2 = std::numbers::sqrt2_v<f32>;
constexpr f32 SQRT3 = std::numbers::sqrt3_v<f32>;
constexpr f32 SQRT12 = 1. / SQRT2;
constexpr f32 SQRT13 = std::numbers::inv_sqrt3_v<f32>;
constexpr f32 LN2 = std::numbers::ln2_v<f32>;
constexpr f32 LN10 = std::numbers::ln10_v<f32>;
constexpr f32 PI = std::numbers::pi_v<f32>;
constexpr f32 PI2 = PI * .5;
constexpr f32 TAU = 2. * PI;
constexpr f32 E = std::numbers::e_v<f32>;
constexpr f32 INF = std::numeric_limits<f32>::infinity();
constexpr f32 NaN = std::numeric_limits<f32>::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<u32>::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;
}
17 changes: 9 additions & 8 deletions engine/native/core/math/functions.cppm
Original file line number Diff line number Diff line change
Expand Up @@ -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 <arithmetic T>
Expand All @@ -32,7 +33,7 @@ export namespace draco::math {
template <arithmetic T>
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<T>) {
return value < T{0} ? -value : value;
} else if constexpr (std::signed_integral<T>) {
Expand Down Expand Up @@ -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<int>(value);
const f32 truncated = static_cast<i32>(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<int>(value);
return static_cast<i32>(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);
}

Expand Down
1 change: 1 addition & 0 deletions engine/native/core/math/math.cppm
Original file line number Diff line number Diff line change
Expand Up @@ -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;
30 changes: 16 additions & 14 deletions engine/native/core/math/math.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
Loading