Compare commits

..

10 commits

93 changed files with 1862 additions and 2984 deletions

View file

@ -1,51 +1,47 @@
add_module(NAME logger INTERFACES logger.cppm TESTS logger.test.cpp)
add_module(NAME bitwise INTERFACES operations.cppm)
add_module(NAME env INTERFACES constants.cppm)
add_module(NAME memory INTERFACES null_on_move.cppm reference.cppm scope.cppm
add_module(NAME preliminary INTERFACES module.cppm fundumental_types.cppm assertions.cppm build_constants.cppm)
add_module(NAME logger INTERFACES logger.cppm TESTS logger.test.cpp DEPENDENCIES preliminary)
add_module(NAME tracer INTERFACES tracer.cppm DEPENDENCIES preliminary logger)
add_module(NAME bitwise INTERFACES operations.cppm DEPENDENCIES preliminary)
add_module(NAME memory INTERFACES null_on_move.cppm reference.cppm scope.cppm
DEPENDENCIES
preliminary
logger
)
add_module(NAME time INTERFACES timer.cppm TESTS timer.test.cpp)
add_module(NAME time INTERFACES timer.cppm TESTS timer.test.cpp DEPENDENCIES preliminary)
add_module(
NAME
test
INTERFACES
module.cppm
test.cppm
expects.cppm
registry.cppm
SOURCES
entrypoint.cpp
DEPENDENCIES
preliminary
logger
TESTS
test.test.cpp
)
add_module(
NAME
lt_debug
ROOT_DIR
${CMAKE_CURRENT_SOURCE_DIR}/debug
INTERFACES
instrumentor.cppm
assertions.cppm
DEPENDENCIES
logger
)
add_module(
NAME
math
INTERFACES
algebra.cppm
mat4.cppm
trig.cppm
vec2.cppm
vec3.cppm
vec4.cppm
mat4.cppm
components.cppm
DEPENDENCIES
preliminary
TESTS
vec2.test.cpp
)
add_module(
@ -55,8 +51,8 @@ add_module(
shader.cppm
metadata.cppm
DEPENDENCIES
preliminary
logger
lt_debug
TESTS
shader.test.cpp
)
@ -71,17 +67,15 @@ add_module(
ENTRYPOINT
entrypoint.cpp
DEPENDENCIES
preliminary
assets
logger
lt_debug
TESTS
bakers.test.cpp
)
# add_executable(asset_baker entrypoint.cpp) target_link_libraries(asset_baker
# PRIVATE libasset_baker)
add_module(NAME camera INTERFACES components.cppm DEPENDENCIES math)
add_module(NAME camera INTERFACES components.cppm DEPENDENCIES preliminary math)
add_module(
NAME
@ -90,9 +84,9 @@ add_module(
application.cppm
system.cppm
DEPENDENCIES
preliminary
memory
PRIVATE_DEPENDENCIES
lt_debug
)
add_module(
@ -104,14 +98,13 @@ add_module(
entity.cppm
DEPENDENCIES
logger
lt_debug
memory
TESTS
registry.test.cpp
sparse_set.test.cpp
)
add_module(NAME input_codes INTERFACES input_codes.cppm)
add_module(NAME input_codes INTERFACES input_codes.cppm DEPENDENCIES preliminary)
if(WIN32)
add_module(
@ -124,6 +117,7 @@ if(WIN32)
events.cppm
components.cppm
DEPENDENCIES
preliminary
ecs
app
math
@ -131,7 +125,6 @@ if(WIN32)
input_codes
PRIVATE_DEPENDENCIES
logger
lt_debug
time
TESTS
system.test.cpp
@ -148,6 +141,7 @@ elseif(UNIX)
events.cppm
components.cppm
DEPENDENCIES
preliminary
ecs
app
math
@ -157,7 +151,6 @@ elseif(UNIX)
PRIVATE_DEPENDENCIES
X11
logger
lt_debug
time
TESTS
system.test.cpp
@ -188,6 +181,7 @@ add_module(
components.cppm
events.cppm
DEPENDENCIES
preliminary
input_codes
surface
math
@ -218,6 +212,7 @@ add_module(
vk/renderer.cppm
vk/debugger.cppm
DEPENDENCIES
preliminary
app
ecs
memory
@ -230,12 +225,12 @@ add_module(
PRIVATE_DEPENDENCIES
surface
TESTS
# _tests/buffer.cpp
# _tests/debugger.cpp
# _tests/device.cpp
# _tests/pass.cpp
# _tests/renderer.cpp
# _tests/surface.cpp
_tests/buffer.cpp
_tests/debugger.cpp
_tests/device.cpp
_tests/pass.cpp
_tests/renderer.cpp
_tests/surface.cpp
_tests/system.cpp
TEST_INTERFACES
_tests/utils.cppm
@ -256,25 +251,8 @@ add_module(
surface
renderer
camera
# TESTS system.test.cpp
)
add_executable(exectest ${CMAKE_CURRENT_SOURCE_DIR}/mirror/entrypoint.cpp)
target_link_libraries(
exectest
PRIVATE mirror
app
time
input
surface
renderer
camera
)
# add_executable_module(mirror entrypoint/mirror.cpp)
# target_link_libraries(mirror PRIVATE libmirror input)
if(ENABLE_SANDBOX)
add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/sandbox/)
endif()

View file

@ -1,16 +1,17 @@
export module app;
import preliminary;
import app.system;
import memory.reference;
import memory.scope;
import std;
namespace lt::app {
export namespace lt::app {
/** The main application class.
* Think of this like an aggregate of systems, you register systems through this interface.
* Then they'll tick every "application frame".
*/
export class Application
class Application
{
public:
Application(const Application &) = delete;
@ -54,11 +55,13 @@ void Application::game_loop()
const auto &last_tick = system->get_last_tick_result();
const auto now = std::chrono::steady_clock::now();
system->tick(TickInfo {
system->tick(
TickInfo {
.delta_time = now - last_tick.end_time,
.budget = std::chrono::milliseconds { 10 },
.start_time = now,
});
}
);
}
for (auto &system : m_systems_to_be_registered)

View file

@ -1,17 +1,18 @@
export module app.system;
import logger;
import std;
namespace lt::app {
import preliminary;
import logger;
export namespace lt::app {
/** Information required to tick a system.
* @note May be used across an entire application-frame (consisting of multiple systems ticking)
*/
export struct TickInfo
struct TickInfo
{
using Timepoint_T = std::chrono::time_point<std::chrono::steady_clock>;
using Duration_T = std::chrono::duration<double>;
using Duration_T = std::chrono::duration<f64>;
/** Duration since previous tick's end_time to current tick's start_time. */
Duration_T delta_time {};
@ -30,11 +31,11 @@ export struct TickInfo
};
/** Information about how a system's tick performed */
export struct TickResult
struct TickResult
{
using Timepoint_T = std::chrono::time_point<std::chrono::steady_clock>;
using Duration_T = std::chrono::duration<double>;
using Duration_T = std::chrono::duration<f64>;
/** The info supplied to the system for ticking. */
TickInfo info;
@ -46,9 +47,9 @@ export struct TickResult
Timepoint_T end_time;
};
export struct SystemDiagnosis
struct SystemDiagnosis
{
enum class Severity : std::uint8_t
enum class Severity : u8
{
verbose,
info,
@ -64,7 +65,7 @@ export struct SystemDiagnosis
Severity severity;
};
export class SystemStats
class SystemStats
{
public:
void push_diagnosis(SystemDiagnosis &&diagnosis)
@ -83,7 +84,7 @@ private:
std::vector<SystemDiagnosis> m_diagnosis;
};
export class ISystem
class ISystem
{
public:
ISystem() = default;

View file

@ -1,10 +1,9 @@
export module bakers;
import debug.assertions;
import preliminary;
import assets.metadata;
import assets.shader;
import logger;
import std;
export void bake_shader(
const std::filesystem::path &in_path,
@ -26,16 +25,18 @@ export void bake_shader(
// Don't bother linking to shaderc, just invoke the command with a system call.
// NOLINTNEXTLINE(concurrency-mt-unsafe)
std::system(std::format(
std::system(
std::format(
"glslc --target-env=vulkan1.4 -std=450core -fshader-stage={} {} -o {}",
type == vertex ? "vert" : "frag",
glsl_path,
spv_path
)
.c_str());
.c_str()
);
auto stream = std::ifstream(spv_path, std::ios::binary);
lt::debug::ensure(
ensure(
stream.is_open(),
"Failed to open compiled {} shader at: {}",
type == vertex ? "vert" : "frag",
@ -45,10 +46,9 @@ export void bake_shader(
stream.seekg(0, std::ios::end);
const auto size = stream.tellg();
auto bytes = std::vector<std::byte>(size);
auto bytes = std::vector<byte>(size);
stream.seekg(0, std::ios::beg);
stream.read((char *)bytes.data(), size); // NOLINT
lt::log::debug("BYTES: {}", bytes.size());
stream.close();
std::filesystem::remove(spv_path);

View file

@ -1,9 +1,9 @@
import preliminary;
import assets.shader;
import logger;
import bakers;
import std;
auto main(int argc, char *argv[]) -> std::int32_t
auto main(i32 argc, char *argv[]) -> i32
try
{
if (argc != 2)

View file

@ -1,19 +1,20 @@
export module assets.metadata;
import std;
import preliminary;
export namespace lt::assets {
using Type_T = std::array<const char, 16>;
using Tag_T = std::uint8_t;
using Tag_T = u8;
using Version = std::uint8_t;
using Version = u8;
using Blob = std::vector<std::byte>;
using Blob = std::vector<byte>;
constexpr auto current_version = Version { 1u };
enum class CompressionType : std::uint8_t
enum class CompressionType : u8
{
none,
lz4,
@ -31,13 +32,13 @@ struct BlobMetadata
{
Tag_T tag;
std::size_t offset;
size_t offset;
CompressionType compression_type;
std::size_t compressed_size;
size_t compressed_size;
std::size_t uncompressed_size;
size_t uncompressed_size;
};
} // namespace lt::assets

View file

@ -1,8 +1,8 @@
export module assets.shader;
import assets.metadata;
import debug.assertions;
import std;
import preliminary;
import assets.metadata;
import logger;
export namespace lt::assets {
@ -16,7 +16,7 @@ public:
code,
};
enum class Type : std::uint8_t
enum class Type : u8
{
vertex,
fragment,
@ -38,7 +38,7 @@ public:
ShaderAsset(const std::filesystem::path &path);
void unpack_to(BlobTag tag, std::span<std::byte> destination) const;
void unpack_to(BlobTag tag, std::span<byte> destination) const;
[[nodiscard]] auto unpack(BlobTag tag) const -> Blob;
@ -54,7 +54,7 @@ public:
[[nodiscard]] auto get_blob_metadata(BlobTag tag) const -> const BlobMetadata &
{
debug::ensure(
ensure(
tag == BlobTag::code,
"Invalid blob tag for shader asset: {}",
std::to_underlying(tag)
@ -88,16 +88,17 @@ constexpr auto total_metadata_size = //
+ sizeof(BlobMetadata::compressed_size) //
+ sizeof(BlobMetadata::uncompressed_size);
ShaderAsset::ShaderAsset(const std::filesystem::path &path): m_stream(path)
ShaderAsset::ShaderAsset(const std::filesystem::path &path)
: m_stream(path, std::ios::beg | std::ios::binary)
{
debug::ensure(m_stream.is_open(), "Failed to open shader asset at: {}", path.string());
ensure(m_stream.is_open(), "Failed to open shader asset at: {}", path.string());
const auto read = [this](auto &field) {
m_stream.read(std::bit_cast<char *>(&field), sizeof(field));
};
m_stream.seekg(0, std::ifstream::end);
const auto file_size = static_cast<std::size_t>(m_stream.tellg());
debug::ensure(
const auto file_size = static_cast<size_t>(m_stream.tellg());
ensure(
file_size > total_metadata_size,
"Failed to open shader asset at: {}, file smaller than metadata: {} < {}",
path.string(),
@ -109,13 +110,14 @@ ShaderAsset::ShaderAsset(const std::filesystem::path &path): m_stream(path)
read(m_asset_metadata.type);
read(m_asset_metadata.version);
read(m_metadata.type);
read(m_code_blob_metadata.tag);
read(m_code_blob_metadata.offset);
read(m_code_blob_metadata.compression_type);
read(m_code_blob_metadata.compressed_size);
read(m_code_blob_metadata.uncompressed_size);
debug::ensure(
ensure(
m_asset_metadata.type == asset_type_identifier,
"Failed to open shader asset at: {}, incorrect asset type: {} != {}",
path.string(),
@ -123,7 +125,7 @@ ShaderAsset::ShaderAsset(const std::filesystem::path &path): m_stream(path)
asset_type_identifier
);
debug::ensure(
ensure(
m_asset_metadata.version == current_version,
"Failed to open shader asset at: {}, version mismatch: {} != {}",
path.string(),
@ -131,21 +133,21 @@ ShaderAsset::ShaderAsset(const std::filesystem::path &path): m_stream(path)
current_version
);
debug::ensure(
ensure(
std::to_underlying(m_metadata.type) <= std::to_underlying(Type::compute),
"Failed to open shader asset at: {}, invalid shader type: {}",
path.string(),
std::to_underlying(m_metadata.type)
);
debug::ensure(
ensure(
m_code_blob_metadata.tag == std::to_underlying(BlobTag::code),
"Failed to open shader asset at: {}, invalid blob tag: {}",
path.string(),
m_code_blob_metadata.tag
);
debug::ensure(
ensure(
m_code_blob_metadata.offset + m_code_blob_metadata.compressed_size <= file_size,
"Failed to open shader asset at: {}, file smaller than blob: {} > {} + {}",
path.string(),
@ -175,7 +177,7 @@ ShaderAsset::ShaderAsset(const std::filesystem::path &path): m_stream(path)
.uncompressed_size = code_blob.size(),
};
debug::ensure(stream.is_open(), "Failed to pack shader asset to {}", destination.string());
ensure(stream.is_open(), "Failed to pack shader asset to {}", destination.string());
const auto write = [&stream](auto &field) {
stream.write(std::bit_cast<char *>(&field), sizeof(field));
};
@ -190,38 +192,30 @@ ShaderAsset::ShaderAsset(const std::filesystem::path &path): m_stream(path)
stream.write(std::bit_cast<char *>(code_blob.data()), static_cast<long long>(code_blob.size()));
}
void ShaderAsset::unpack_to(BlobTag tag, std::span<std::byte> destination) const
void ShaderAsset::unpack_to(BlobTag tag, std::span<byte> destination) const
{
debug::ensure(
tag == BlobTag::code,
"Invalid blob tag for shader asset: {}",
std::to_underlying(tag)
);
ensure(tag == BlobTag::code, "Invalid blob tag for shader asset: {}", std::to_underlying(tag));
debug::ensure(
ensure(
destination.size() >= m_code_blob_metadata.uncompressed_size,
"Failed to unpack shader blob {} to destination ({}) of size {} since it's smaller "
"than the blobl's uncompressed size: {}",
std::to_underlying(tag),
std::bit_cast<std::size_t>(destination.data()),
std::bit_cast<size_t>(destination.data()),
destination.size(),
m_code_blob_metadata.uncompressed_size
);
m_stream.seekg(static_cast<long long>(m_code_blob_metadata.offset));
m_stream.seekg(static_cast<long long>(m_code_blob_metadata.offset), std::ifstream::beg);
m_stream.read(
std::bit_cast<char *>(destination.data()),
static_cast<long long>(m_code_blob_metadata.uncompressed_size)
m_code_blob_metadata.uncompressed_size
);
}
[[nodiscard]] auto ShaderAsset::unpack(BlobTag tag) const -> Blob
{
debug::ensure(
tag == BlobTag::code,
"Invalid blob tag for shader asset: {}",
std::to_underlying(tag)
);
ensure(tag == BlobTag::code, "Invalid blob tag for shader asset: {}", std::to_underlying(tag));
auto blob = Blob(m_code_blob_metadata.uncompressed_size);
unpack_to(tag, blob);

View file

@ -1,47 +1,65 @@
import test;
import assets.metadata;
import assets.shader;
import test.test;
import test.expects;
import std;
using ::lt::assets::AssetMetadata;
using ::lt::assets::Blob;
using ::lt::assets::BlobMetadata;
using ::lt::assets::ShaderAsset;
using ::lt::test::Case;
using ::lt::test::expect_eq;
using ::lt::test::expect_throw;
using ::lt::test::expect_true;
using ::lt::test::Suite;
using ::lt::test::operator""_suite;
const auto test_data_path = std::filesystem::path { "./data/test_assets" };
const auto tmp_path = std::filesystem::path { "/tmp/lt_assets_tests/" };
[[nodiscard]] auto generate_blob(size_t size) -> Blob
{
auto blob = Blob {};
for (auto idx : std::views::iota(0u, size))
{
blob.emplace_back(static_cast<byte>(idx));
}
return blob;
}
Suite raii = "shader_raii"_suite = [] {
std::filesystem::current_path(test_data_path);
std::filesystem::create_directories(tmp_path);
Case { "happy path won't throw" } = [] {
Case { "happy paths" } = [] {
auto shader_asset = ShaderAsset { "triangle.frag.asset" };
};
Case { "many won't freeze/throw" } = [] {
};
Case { "unhappy paths" } = [] {
// non-existent file
expect_throw([] { ShaderAsset { "path" }; });
Case { "unhappy path throws" } = [] {
expect_throw([] { ShaderAsset { "random_path" }; });
};
};
// incompatible type
expect_throw([] { ShaderAsset { "dummytext" }; });
// NOLINTNEXTLINE(cppcoreguidelines-interfaces-global-init)
Suite packing = "shader_pack"_suite = [] {
Case { "" } = [] {
const auto out_path = tmp_path / "shader_packing";
auto dummy_blob = lt::assets::Blob {};
for (auto idx : std::views::iota(0, 255))
// some random stressing
expect_throw([] {
for (auto idx : std::views::iota(0u, 1'000u))
{
dummy_blob.emplace_back(static_cast<std::byte>(idx));
auto shader_asset = ShaderAsset { std::to_string(idx) };
}
});
};
Case { "many" } = [] {
for (auto idx : std::views::iota(0u, 1'000u))
{
ignore = idx;
auto shader_asset = ShaderAsset { "triangle.frag.asset" };
}
};
};
Suite packing = "shader_pack"_suite = [] {
Case { "Unpacking packed data returns the same data" } = [] {
const auto out_path = tmp_path / "shader_packing";
constexpr auto blob_size = size_t { 255u };
auto blob = generate_blob(blob_size);
const auto expected_size = //
sizeof(AssetMetadata::type) //
@ -52,7 +70,7 @@ Suite packing = "shader_pack"_suite = [] {
+ sizeof(BlobMetadata::compression_type) //
+ sizeof(BlobMetadata::compressed_size) //
+ sizeof(BlobMetadata::uncompressed_size) //
+ dummy_blob.size();
+ blob.size();
ShaderAsset::pack(
out_path,
@ -63,7 +81,7 @@ Suite packing = "shader_pack"_suite = [] {
ShaderAsset::Metadata {
.type = ShaderAsset::Type::vertex,
},
std::move(dummy_blob)
std::move(blob)
);
auto stream = std::ifstream {
@ -72,8 +90,8 @@ Suite packing = "shader_pack"_suite = [] {
};
expect_true(stream.is_open());
stream.seekg(0, std::ios::end);
const auto file_size = static_cast<std::size_t>(stream.tellg());
stream.seekg(0u, std::ios::end);
const auto file_size = static_cast<size_t>(stream.tellg());
expect_eq(file_size, expected_size);
stream.close();
@ -86,12 +104,12 @@ Suite packing = "shader_pack"_suite = [] {
const auto &metadata = shader_asset.get_metadata();
expect_eq(metadata.type, ShaderAsset::Type::vertex);
auto blob = shader_asset.unpack(ShaderAsset::BlobTag::code);
expect_eq(blob.size(), 255);
auto unpakced_blob = shader_asset.unpack(ShaderAsset::BlobTag::code);
expect_eq(unpakced_blob.size(), blob_size);
for (auto idx : std::views::iota(0, 255))
for (auto idx : std::views::iota(0u, blob_size))
{
expect_eq(blob[idx], static_cast<std::byte>(idx));
expect_eq(unpakced_blob[idx], static_cast<byte>(idx));
}
};
};

View file

@ -1,12 +1,12 @@
export module bitwise;
import std;
import preliminary;
namespace lt::bitwise {
/* bit-wise */
export constexpr auto bit(std::uint32_t x) -> std::uint32_t
export constexpr auto bit(u32 x) -> u32
{
return 1u << x;
return u32 { 1u } << x;
}
} // namespace lt::bitwise

View file

@ -1,17 +1,19 @@
export module camera.components;
import preliminary;
import math.vec4;
namespace lt::camera::components {
export namespace lt::camera::components {
export struct PerspectiveCamera
struct PerspectiveCamera
{
float vertical_fov {};
f32 vertical_fov {};
float near_plane {};
f32 near_plane {};
float far_plane {};
f32 far_plane {};
float aspect_ratio {};
f32 aspect_ratio {};
math::vec4 background_color;

View file

@ -1,47 +0,0 @@
export module debug.assertions;
import std;
namespace lt::debug {
///////////////////////////////////////
// ----------* INTERFACE *--------- //
/////////////////////////////////////
export template<typename Expression_T, typename... Args_T>
struct ensure
{
ensure(
const Expression_T &expression,
std::format_string<Args_T...> fmt,
Args_T &&...args,
const std::source_location &location = std::source_location::current()
);
};
export template<typename Expression_T, typename... Args_T>
ensure(Expression_T, std::format_string<Args_T...>, Args_T &&...)
-> ensure<Expression_T, Args_T...>;
///////////////////////////////////////
// * IMPLEMENTATION -- TEMPLATES * //
/////////////////////////////////////
template<typename Expression_T, typename... Args_T>
ensure<Expression_T, Args_T...>::ensure(
const Expression_T &expression,
std::format_string<Args_T...> fmt,
Args_T &&...args,
const std::source_location &location
)
{
if (!static_cast<bool>(expression))
{
throw std::runtime_error { std::format(
"exception: {}\nlocation: {}:{}",
std::format(fmt, std::forward<Args_T>(args)...),
location.file_name(),
location.line()
) };
}
}
} // namespace lt::debug

View file

@ -1,20 +1,20 @@
export module ecs.entity;
import debug.assertions;
import preliminary;
import memory.reference;
import ecs.registry;
import std;
namespace lt::ecs {
export namespace lt::ecs {
/** High-level entity convenience wrapper */
export class Entity
class Entity
{
public:
Entity(memory::Ref<Registry> registry, EntityId identifier)
: m_registry(std::move(registry))
, m_identifier(identifier)
{
debug::ensure(m_registry, "Failed to create Entity ({}): null registry", m_identifier);
ensure(m_registry, "Failed to create Entity ({}): null registry", m_identifier);
}
template<typename Component_T>
@ -51,5 +51,4 @@ private:
EntityId m_identifier;
};
} // namespace lt::ecs

View file

@ -1,14 +1,14 @@
export module ecs.registry;
import debug.assertions;
import preliminary;
import ecs.sparse_set;
import memory.scope;
import std;
namespace lt::ecs {
export namespace lt::ecs {
export using EntityId = std::uint32_t;
using EntityId = u32;
export constexpr auto null_entity = std::numeric_limits<EntityId>::max();
constexpr auto null_entity = std::numeric_limits<EntityId>::max();
/** A registry of components, the heart of an ECS architecture.
*
@ -23,7 +23,7 @@ export constexpr auto null_entity = std::numeric_limits<EntityId>::max();
* @ref https://github.com/skypjack/entt
* @ref https://github.com/SanderMertens/flecs
*/
export class Registry
class Registry
{
public:
using UnderlyingSparseSet_T = TypeErasedSparseSet<EntityId>;
@ -190,25 +190,25 @@ public:
}
};
[[nodiscard]] auto get_entity_count() const -> std::size_t
[[nodiscard]] auto get_entity_count() const -> size_t
{
return static_cast<std::size_t>(m_entity_count);
return static_cast<size_t>(m_entity_count);
}
private:
using TypeId = std::size_t;
using TypeId = size_t;
static consteval auto hash_cstr(const char *str) -> TypeId
{
constexpr auto fnv_offset_basis = std::size_t { 14695981039346656037ull };
constexpr auto fnv_prime = std::size_t { 1099511628211ull };
constexpr auto fnv_offset_basis = size_t { 14695981039346656037ull };
constexpr auto fnv_prime = size_t { 1099511628211ull };
auto hash = fnv_offset_basis;
for (const auto &ch : std::string_view { str })
{
hash *= fnv_prime;
hash ^= static_cast<std::uint8_t>(ch);
hash ^= static_cast<u8>(ch);
}
return hash;
@ -242,7 +242,7 @@ private:
auto *base_set = m_sparsed_sets[type_id].get();
auto *derived_set = dynamic_cast<SparseSet<T, EntityId> *>(base_set);
debug::ensure(derived_set, "Failed to downcast to derived set");
ensure(derived_set, "Failed to downcast to derived set");
return *derived_set;
}

View file

@ -1,21 +1,12 @@
import test;
import ecs.registry;
import test.test;
import test.expects;
import std;
using ::lt::ecs::EntityId;
using ::lt::ecs::Registry;
using ::lt::test::Case;
using ::lt::test::expect_eq;
using ::lt::test::expect_false;
using ::lt::test::expect_true;
using ::lt::test::expect_unreachable;
using ::lt::test::Suite;
using ::lt::test::operator""_suite;
struct Component
{
int m_int {};
i32 m_int {};
std::string m_string;
[[nodiscard]] friend auto operator==(const Component &lhs, const Component &rhs) -> bool
@ -39,7 +30,7 @@ struct std::formatter<Component>
struct Component_B
{
float m_float {};
f32 m_float {};
[[nodiscard]] friend auto operator==(const Component_B lhs, const Component_B &rhs) -> bool
{
@ -61,20 +52,21 @@ struct std::formatter<Component_B>
};
Suite raii = "raii"_suite = [] {
Case { "happy path won't throw" } = [] {
std::ignore = Registry {};
Case { "happy paths" } = [] {
ignore = Registry {};
};
Case { "many won't freeze/throw" } = [] {
Case { "unhappy paths" } = [] {
};
Case { "many" } = [] {
for (auto idx : std::views::iota(0, 100'000))
{
std::ignore = Registry {};
ignore = idx;
ignore = Registry {};
}
};
Case { "unhappy path throws" } = [] {
};
Case { "post construct has correct state" } = [] {
auto registry = Registry {};
expect_eq(registry.get_entity_count(), 0);
@ -158,6 +150,7 @@ Suite callbacks = "callbacks"_suite = [] {
Case { "on_construct/on_destruct won't get called on unrelated component" } = [] {
auto registry = Registry {};
registry.connect_on_construct<Component>([&](Registry &, EntityId) {
expect_unreachable();
});
@ -167,6 +160,7 @@ Suite callbacks = "callbacks"_suite = [] {
for (auto idx : std::views::iota(0, 100'000))
{
ignore = idx;
registry.add<Component_B>(registry.create_entity(), {});
}
};
@ -188,6 +182,8 @@ Suite callbacks = "callbacks"_suite = [] {
expect_true(on_destruct_called.empty());
for (auto idx : std::views::iota(0, 100'000))
{
ignore = idx;
auto entity = all_entities.emplace_back(registry.create_entity());
registry.add<Component>(entity, {});
}
@ -222,7 +218,7 @@ Suite each = "each"_suite = [] {
component_map_a[entity] = component;
}
auto component_map_b = std::unordered_map<lt::ecs::EntityId, Component_B> {};
auto component_map_b = std::unordered_map<EntityId, Component_B> {};
for (auto idx : std::views::iota(0, 10'000))
{
auto entity = EntityId {};
@ -237,7 +233,7 @@ Suite each = "each"_suite = [] {
}
auto &component = registry.add<Component_B>(
entity,
{ .m_float = static_cast<float>(idx) / 2.0f }
{ .m_float = static_cast<f32>(idx) / 2.0f }
);
component_map_b[entity] = component;
@ -308,7 +304,7 @@ Suite views = "views"_suite = [] {
}
auto &component = registry.add<Component_B>(
entity,
{ .m_float = static_cast<float>(idx) / 2.0f }
{ .m_float = static_cast<f32>(idx) / 2.0f }
);
component_map_b[entity] = component;

View file

@ -1,13 +1,13 @@
export module ecs.sparse_set;
import debug.assertions;
import std;
namespace lt::ecs {
import preliminary;
export namespace lt::ecs {
/**
* @ref https://programmingpraxis.com/2012/03/09/sparse-sets/
*/
export template<typename Identifier_T = std::uint32_t>
template<typename Identifier_T = u32>
class TypeErasedSparseSet
{
public:
@ -26,19 +26,19 @@ public:
virtual void remove(Identifier_T identifier) = 0;
};
export template<typename Value_T, typename Identifier_T = std::uint32_t>
template<typename Value_T, typename Identifier_T = u32>
class SparseSet: public TypeErasedSparseSet<Identifier_T>
{
public:
using Dense_T = std::pair<Identifier_T, Value_T>;
static constexpr auto max_capacity = std::size_t { 1'000'000 };
static constexpr auto max_capacity = size_t { 1'000'000 };
static constexpr auto null_identifier = std::numeric_limits<Identifier_T>().max();
explicit SparseSet(std::size_t initial_capacity = 1)
explicit SparseSet(size_t initial_capacity = 1)
{
debug::ensure(
ensure(
initial_capacity <= max_capacity,
"Failed to create SparseSet: capacity too large ({} > {})",
initial_capacity,
@ -51,24 +51,18 @@ public:
auto insert(Identifier_T identifier, Value_T value) -> Dense_T &
{
ensure(identifier < max_capacity, "SparseSet::insert: identifier < max_capacity");
if (m_sparse.size() < identifier + 1)
{
auto new_capacity = std::max(
static_cast<std::size_t>(identifier + 1),
m_sparse.size() * 2
);
auto new_capacity = std::max(static_cast<size_t>(identifier + 1), m_sparse.size() * 2);
new_capacity = std::min(new_capacity, max_capacity);
// log::debug("Increasing sparse vector size:", m_dead_count);
// log::debug("\tdead_count: {}", m_dead_count);
// log::debug("\talive_count: {}", m_alive_count);
// log::debug("\tsparse.size: {} -> {}", m_sparse.size(), new_capacity);
m_sparse.resize(new_capacity, null_identifier);
}
++m_alive_count;
m_sparse[identifier] = m_dense.size();
m_sparse[identifier] = static_cast<Identifier_T>(m_dense.size());
return m_dense.emplace_back(identifier, std::move(value));
}
@ -78,7 +72,27 @@ public:
*/
void remove(Identifier_T identifier) override
{
ensure(
identifier < m_sparse.size(),
"Failed to ensure: identifier < m_sparse.size() [{} < {}]",
identifier,
m_sparse.size()
);
auto &idx = m_sparse[identifier];
ensure(
idx != null_identifier,
"Failed to ensure: idx != null_identifier [{} != {}]",
idx,
null_identifier
);
ensure(
idx < m_dense.size(),
"Failed to ensure: idx < m_dense.size() [{} < {}]",
idx,
m_dense.size()
);
auto &[entity, component] = m_dense[idx];
auto &[last_entity, last_component] = m_dense.back();
@ -149,12 +163,12 @@ public:
return std::forward<Self_T>(self).m_dense[std::forward<Self_T>(self).m_sparse[identifier]];
}
[[nodiscard]] auto get_size() const noexcept -> std::size_t
[[nodiscard]] auto get_size() const noexcept -> size_t
{
return m_alive_count;
}
[[nodiscard]] auto get_capacity() const noexcept -> std::size_t
[[nodiscard]] auto get_capacity() const noexcept -> size_t
{
return m_sparse.capacity();
}
@ -169,9 +183,9 @@ private:
std::vector<Identifier_T> m_sparse;
std::size_t m_alive_count {};
size_t m_alive_count {};
std::size_t m_dead_count {};
size_t m_dead_count {};
};
} // namespace lt::ecs

View file

@ -1,28 +1,26 @@
import test;
import ecs.sparse_set;
import test.test;
import test.expects;
import std;
using ::lt::test::Case;
using ::lt::test::expect_eq;
using ::lt::test::expect_false;
using ::lt::test::expect_ne;
using ::lt::test::expect_throw;
using ::lt::test::expect_true;
using ::lt::test::Suite;
using ::lt::test::operator""_suite;
using Value_T = i32;
using Set = lt::ecs::SparseSet<Value_T>;
using Set = lt::ecs::SparseSet<int>;
constexpr auto capacity = 100;
Suite raii = "raii"_suite = [] {
Case { "happy path won't throw" } = [] {
std::ignore = Set {};
std::ignore = Set { Set::max_capacity };
Case { "happy paths" } = [] {
ignore = Set {};
ignore = Set { Set::max_capacity };
};
Case { "unhappy path throws" } = [] {
expect_throw([] { std::ignore = Set { Set::max_capacity + 1 }; });
Case { "unhappy paths" } = [] {
expect_throw([] { ignore = Set { Set::max_capacity + 1 }; });
};
Case { "many" } = [] {
for (auto idx : std::views::iota(0, 1'000))
{
ignore = Set { static_cast<size_t>(idx) };
}
};
Case { "post construct has correct state" } = [&] {
@ -33,7 +31,29 @@ Suite raii = "raii"_suite = [] {
};
Suite element_raii = "element_raii"_suite = [] {
Case { "many inserts/removes won't freeze/throw" } = [] {
Case { "happy paths" } = [] {
auto set = Set { capacity };
set.insert(0, {});
set.remove(0);
};
Case { "unhappy paths" } = [] {
expect_throw([] {
auto set = Set { capacity };
set.insert(Set::max_capacity + 1, {});
});
expect_throw([] {
auto set = Set { capacity };
set.insert(0, {});
set.insert(1, {});
set.insert(2, {});
set.remove(3);
});
};
Case { "many" } = [] {
auto set = Set {};
for (auto idx : std::views::iota(0, 10'000))
{
@ -77,7 +97,7 @@ Suite element_raii = "element_raii"_suite = [] {
expect_eq(set.get_size(), 10'000 - (idx + 1));
expect_false(set.contains(idx));
expect_throw([&] { std::ignore = set.at(idx); });
expect_throw([&] { ignore = set.at(idx); });
}
};
@ -97,7 +117,7 @@ Suite element_raii = "element_raii"_suite = [] {
for (auto &[identifier, value] : set)
{
expect_eq(identifier, value);
expect_eq(static_cast<Value_T>(identifier), value);
expect_ne(value, 0);
expect_ne(value, 32);
expect_ne(value, 69);
@ -129,7 +149,7 @@ Suite getters = "getters"_suite = [] {
expect_eq(set.get_capacity(), 10'000);
set.insert(set.get_size(), {});
set.insert(static_cast<Value_T>(set.get_size()), {});
expect_ne(set.get_capacity(), 10'000);
};
@ -140,12 +160,12 @@ Suite getters = "getters"_suite = [] {
{
expect_throw([&] {
set.insert(idx, {});
std::ignore = set.at(50);
ignore = set.at(50);
});
}
set.insert(50, {});
std::ignore = set.at(50); // should not throw
ignore = set.at(50); // should not throw
};
};
@ -159,5 +179,10 @@ Suite clear = "clear"_suite = [] {
set.clear();
expect_eq(set.get_size(), 0);
for (auto idx : std::views::iota(0, 10'000))
{
expect_throw([&] { ignore = set.at(idx); });
}
};
};

View file

@ -1,17 +1,18 @@
export module input.system:components;
import preliminary;
import input.codes;
import std;
namespace lt::input {
export namespace lt::input {
export struct Trigger
struct Trigger
{
Key mapped_keycode;
};
export struct InputAction
struct InputAction
{
enum class State : std::uint8_t
enum class State : u8
{
inactive,
active,
@ -26,18 +27,18 @@ export struct InputAction
Trigger trigger;
};
export class InputComponent
class InputComponent
{
public:
InputComponent() = default;
auto add_action(InputAction action) -> std::size_t
auto add_action(InputAction action) -> size_t
{
m_actions.emplace_back(std::move(action));
return m_actions.size() - 1;
}
auto get_action(std::size_t idx) -> const InputAction &
auto get_action(size_t idx) -> const InputAction &
{
return m_actions[idx];
}

View file

@ -2,7 +2,6 @@ export module input.system;
export import :components;
import logger;
import app.system;
import debug.assertions;
import ecs.registry;
import memory.reference;
import surface.system;
@ -69,7 +68,7 @@ struct overloads: Ts...
System::System(memory::Ref<ecs::Registry> registry): m_registry(std::move(registry))
{
debug::ensure(m_registry, "Failed to initialize input system: null registry");
ensure(m_registry, "Failed to initialize input system: null registry");
}
void System::tick(app::TickInfo tick)
@ -158,8 +157,10 @@ void System::on_key_press(const lt::surface::KeyPressedEvent &event)
{
if (std::to_underlying(event.get_key()) > m_keys.size())
{
log::debug("Key code larger than key container size, implement platform-dependant "
"key-code-mapping!");
log::warn(
"Key code larger than key container size, implement platform-dependant "
"key-code-mapping!"
);
return;
}
@ -171,8 +172,10 @@ void System::on_key_release(const lt::surface::KeyReleasedEvent &event)
{
if (std::to_underlying(event.get_key()) > m_keys.size())
{
log::debug("Key code larger than key container size, implement platform-dependant "
"key-code-mapping!");
log::warn(
"Key code larger than key container size, implement platform-dependant "
"key-code-mapping!"
);
return;
}

View file

@ -1,9 +1,6 @@
import std;
import test;
import input.system;
import input.codes;
import std;
import test.test;
import test.expects;
import surface.events;
import memory.scope;
import memory.reference;
@ -12,23 +9,10 @@ import ecs.entity;
import ecs.registry;
import surface.system;
using ::lt::input::InputComponent;
using ::lt::input::System;
// NOLINTBEGIN
using namespace lt;
using input::InputComponent;
using input::System;
using std::ignore;
using test::Case;
using test::expect_eq;
using test::expect_false;
using test::expect_ne;
using test::expect_not_nullptr;
using test::operator""_suite;
using test::expect_throw;
using test::Suite;
// NOLINTEND
[[nodiscard]] auto tick_info() -> app::TickInfo
[[nodiscard]] auto tick_info() -> lt::app::TickInfo
{
return {
.delta_time = std::chrono::milliseconds { 16 },
@ -40,12 +24,12 @@ using test::Suite;
class Fixture
{
public:
[[nodiscard]] auto registry() -> memory::Ref<ecs::Registry>
[[nodiscard]] auto registry() -> lt::memory::Ref<lt::ecs::Registry>
{
return m_registry;
}
auto add_input_component() -> ecs::EntityId
auto add_input_component() -> lt::ecs::EntityId
{
auto entity = m_registry->create_entity();
m_registry->add<InputComponent>(entity, {});
@ -53,7 +37,7 @@ public:
return entity;
}
auto add_surface_component() -> ecs::EntityId
auto add_surface_component() -> lt::ecs::EntityId
{
auto entity = m_registry->create_entity();
m_surface_system.create_surface_component(
@ -65,27 +49,28 @@ public:
}
private:
memory::Ref<ecs::Registry> m_registry = memory::create_ref<ecs::Registry>();
lt::memory::Ref<lt::ecs::Registry> m_registry = lt::memory::create_ref<lt::ecs::Registry>();
surface::System m_surface_system = surface::System { m_registry };
lt::surface::System m_surface_system = lt::surface::System { m_registry };
};
Suite raii = "raii"_suite = "raii"_suite = [] {
Case { "happy path won't throw" } = [&] {
Case { "happy paths" } = [&] {
System { Fixture {}.registry() };
};
Case { "many won't freeze/throw" } = [&] {
Case { "unhappy paths" } = [] {
expect_throw([] { ignore = System { {} }; });
};
Case { "many" } = [&] {
auto fixture = Fixture {};
for (auto idx : std::views::iota(0, 10'000))
{
ignore = idx;
ignore = System { fixture.registry() };
}
};
Case { "unhappy path throws" } = [] {
expect_throw([] { ignore = System { {} }; });
};
};
Suite system_events = "system_events"_suite = [] {
@ -115,14 +100,14 @@ Suite registry_events = "registry_events"_suite = [] {
auto registry = fixture.registry();
auto system = System { registry };
const auto &entity = fixture.add_input_component();
fixture.add_input_component();
expect_eq(registry->view<InputComponent>().get_size(), 1);
};
Case { "on_destrroy<InputComponent>" } = [] {
auto fixture = Fixture {};
auto registry = fixture.registry();
auto system = memory::create_scope<System>(registry);
auto system = lt::memory::create_scope<System>(registry);
auto entity_a = fixture.add_input_component();
auto entity_b = fixture.add_input_component();
@ -154,7 +139,7 @@ Suite tick = "tick"_suite = [] {
auto system = System { fixture.registry() };
auto surface_entity = fixture.add_surface_component();
auto &surface = registry->get<surface::SurfaceComponent>(surface_entity);
auto &surface = registry->get<lt::surface::SurfaceComponent>(surface_entity);
auto input_entity = fixture.add_input_component();
auto &input = registry->get<InputComponent>(input_entity);
@ -162,48 +147,29 @@ Suite tick = "tick"_suite = [] {
auto action_key = input.add_action(
{
.name { "test" },
.trigger = { .mapped_keycode = Key::A },
.trigger = { .mapped_keycode = Key::a },
}
);
expect_eq(input.get_action(action_key).state, input::InputAction::State::inactive);
using enum ::lt::input::InputAction::State;
expect_eq(input.get_action(action_key).state, inactive);
system.tick(tick_info());
expect_eq(input.get_action(action_key).state, input::InputAction::State::inactive);
expect_eq(input.get_action(action_key).state, inactive);
surface.push_event(surface::KeyPressedEvent(Key::A));
surface.push_event(lt::surface::KeyPressedEvent(Key::a));
system.tick(tick_info());
expect_eq(input.get_action(action_key).state, input::InputAction::State::triggered);
expect_eq(input.get_action(action_key).state, triggered);
system.tick(tick_info());
expect_eq(input.get_action(action_key).state, input::InputAction::State::active);
expect_eq(input.get_action(action_key).state, active);
system.tick(tick_info());
system.tick(tick_info());
system.tick(tick_info());
expect_eq(input.get_action(action_key).state, input::InputAction::State::active);
expect_eq(input.get_action(action_key).state, active);
surface.push_event(surface::KeyReleasedEvent(Key::A));
surface.push_event(lt::surface::KeyReleasedEvent(Key::a));
system.tick(tick_info());
expect_eq(input.get_action(action_key).state, input::InputAction::State::inactive);
};
Case { "Tick triggers" } = [] {
auto fixture = Fixture {};
auto registry = fixture.registry();
auto system = System { fixture.registry() };
auto surface_entity = fixture.add_surface_component();
auto &surface = registry->get<surface::SurfaceComponent>(surface_entity);
auto input_entity = fixture.add_input_component();
auto &input = registry->get<InputComponent>(input_entity);
auto action_key = input.add_action(
{
.name { "test" },
.trigger = { .mapped_keycode = Key::A },
}
);
expect_eq(input.get_action(action_key).state, inactive);
};
};

View file

@ -1,20 +1,31 @@
/**
* @note: The reason this is a separate module, rather than being in the `Input` module is that
* the input is received from the hardware through the `Surface` module, and it is further parsed
* inside the `Input` module, USING the `Surface` module's events.
*
* Hence, both `Surface` and `Input` needs to agree to the same input codes, while `Input` depends
* on `Surface`. The simplest solution is to keep the codes in a 3rd module and make both depend on
* it. (I did not want to give `Surface` the responsibility of defining input codes...)
*/
export module input.codes;
import std;
export enum class Key: std::uint16_t {
import preliminary;
export enum class Key: u16 {
none = 0,
left_mouse_button,
right_mouse_button,
middle_mouse_button,
left_button,
l_button = left_button,
left_mouse = left_mouse_button,
right_mouse = right_mouse_button,
middle_mouse = middle_mouse_button,
right_button,
r_button = right_button,
l_mouse = left_mouse_button,
r_mouse = right_mouse_button,
m_mouse = middle_mouse_button,
middle_button,
m_button = middle_button,
// the buttons on the sidse of some mouses
x_button_1,
x_button_2,
backspace,
tab,
@ -87,7 +98,6 @@ export enum class Key: std::uint16_t {
digit_8,
digit_9,
/* letters */
a,
b,
c,
@ -140,7 +150,6 @@ export enum class Key: std::uint16_t {
kp_enter,
kp_equal,
/* function */
f1,
f2,
f3,
@ -154,6 +163,7 @@ export enum class Key: std::uint16_t {
f11,
f12,
/** Input was received but was none of the above. */
unknown,
};
@ -164,12 +174,13 @@ export [[nodiscard]] constexpr auto to_string(Key key) -> std::string
{
case none: return "<none>";
/* mouse */
case left_mouse_button: return "left_mouse_button";
case right_mouse_button: return "right_mouse_button";
case middle_mouse_button: return "middle_mouse_button";
case left_button: return "left_button";
case right_button: return "right_button";
case middle_button: return "middle_button";
case x_button_1: return "x_button_1";
case x_button_2: return "x_button_2";
/* editing / control */
case backspace: return "backspace";
case tab: return "tab";
case capslock: return "capslock";
@ -177,14 +188,12 @@ export [[nodiscard]] constexpr auto to_string(Key key) -> std::string
case space: return "space";
case delete_: return "delete";
/* modifiers */
case shift: return "shift";
case control: return "control";
case right_control: return "right_control";
case alt: return "alt";
case right_alt: return "right_alt";
/* navigation */
case pageup: return "pageup";
case pagedown: return "pagedown";
case home: return "home";
@ -195,7 +204,6 @@ export [[nodiscard]] constexpr auto to_string(Key key) -> std::string
case right_arrow: return "right_arrow";
case down_arrow: return "down_arrow";
/* system */
case cancel: return "cancel";
case pause: return "pause";
case select: return "select";
@ -205,7 +213,6 @@ export [[nodiscard]] constexpr auto to_string(Key key) -> std::string
case help: return "help";
case sleep: return "sleep";
/* digits */
case digit_0: return "0";
case digit_1: return "1";
case digit_2: return "2";
@ -217,7 +224,6 @@ export [[nodiscard]] constexpr auto to_string(Key key) -> std::string
case digit_8: return "8";
case digit_9: return "9";
/* letters */
case a: return "a";
case b: return "b";
case c: return "c";
@ -245,11 +251,9 @@ export [[nodiscard]] constexpr auto to_string(Key key) -> std::string
case y: return "y";
case z: return "z";
/* super / meta */
case super: return "super";
case right_super: return "right_super";
/* keypad */
case kp_0: return "kp_0";
case kp_1: return "kp_1";
case kp_2: return "kp_2";
@ -268,7 +272,6 @@ export [[nodiscard]] constexpr auto to_string(Key key) -> std::string
case kp_enter: return "kp_enter";
case kp_equal: return "kp_equal";
/* function keys */
case f1: return "f1";
case f2: return "f2";
case f3: return "f3";

View file

@ -1,11 +1,11 @@
export module logger;
import std;
import preliminary;
namespace lt::log {
/** Severity of a log message. */
enum class Level : std::uint8_t
enum class Level : u8
{
/** Lowest and most vebose log level, for tracing execution paths and events */
trace = 0,
@ -31,9 +31,9 @@ enum class Level : std::uint8_t
namespace details {
inline auto thread_hash_id() noexcept -> std::uint64_t
inline auto thread_hash_id() noexcept -> u64
{
return static_cast<std::uint64_t>(std::hash<std::thread::id> {}(std::this_thread::get_id()));
return static_cast<u64>(std::hash<std::thread::id> {}(std::this_thread::get_id()));
}
} // namespace details
@ -48,7 +48,7 @@ struct [[maybe_unused]] print
Args &&...arguments
) noexcept
{
constexpr auto to_string = [](Level level, auto location) {
constexpr auto to_string = [](Level level) {
// clang-format off
switch (level)
{
@ -70,7 +70,7 @@ struct [[maybe_unused]] print
std::println(
"{} {} ==> {}",
to_string(level, location),
to_string(level),
std::format("{}:{}", path.filename().string(), location.line()),
std::format(format, std::forward<Args>(arguments)...)
);

View file

@ -1,11 +1,7 @@
import logger;
import test.test;
using ::lt::test::Case;
using ::lt::test::Suite;
import test;
Suite suite = [] {
Case { "no format" } = [] {
Case { "formatless" } = [] {
lt::log::trace("trace");
lt::log::debug("debug");
lt::log::info("info");

View file

@ -1,6 +1,7 @@
export module math.algebra;
import preliminary;
import math.mat4;
import std;
export namespace lt::math {
@ -35,6 +36,7 @@ export namespace lt::math {
* https://www.youtube.com/watch?v=EqNcqBdrNyI
*/
template<typename T>
requires(std::is_arithmetic_v<T>)
constexpr auto perspective(T field_of_view, T aspect_ratio, T z_near, T z_far)
{
const T half_fov_tan = std::tan(field_of_view / static_cast<T>(2));

View file

@ -1,5 +1,6 @@
export module math.components;
import preliminary;
import math.vec3;
namespace lt::math::components {

View file

@ -1,11 +1,14 @@
export module math.mat4;
import preliminary;
import math.vec2;
import math.vec3;
import math.vec4;
import std;
namespace lt::math {
export namespace lt::math {
export template<typename T = float>
template<typename T = f32>
requires(std::is_arithmetic_v<T>)
struct mat4_impl
{
using Column_T = vec4_impl<T>;
@ -54,12 +57,12 @@ struct mat4_impl
};
}
[[nodiscard]] constexpr auto operator[](std::size_t idx) -> Column_T &
[[nodiscard]] constexpr auto operator[](size_t idx) -> Column_T &
{
return values[idx];
}
[[nodiscard]] constexpr auto operator[](std::size_t idx) const -> const Column_T &
[[nodiscard]] constexpr auto operator[](size_t idx) const -> const Column_T &
{
return values[idx];
}
@ -74,37 +77,41 @@ struct mat4_impl
return vec4_impl<T> {};
}
std::array<Column_T, 4> values; // NOLINT
std::array<Column_T, 4u> values;
};
export template<typename T>
/** @todo(Light): Implement */
template<typename T>
[[nodiscard]] auto translate(const vec3_impl<T> &value) -> mat4_impl<T>
{
return mat4_impl<T> {};
}
export template<typename T>
[[nodiscard]] auto rotate(float value, const vec3_impl<T> &xyz) -> mat4_impl<T>
/** @todo(Light): Implement */
template<typename T>
[[nodiscard]] auto rotate(f32 value, const vec3_impl<T> &xyz) -> mat4_impl<T>
{
return mat4_impl<T> {};
}
export template<typename T>
/** @todo(Light): Implement */
template<typename T>
[[nodiscard]] auto scale(const vec3_impl<T> &value) -> mat4_impl<T>
{
return mat4_impl<T> {};
}
export template<typename T>
/** @todo(Light): Implement */
template<typename T>
[[nodiscard]] auto inverse(const mat4_impl<T> &value) -> mat4_impl<T>
{
return mat4_impl<T> {};
}
export using mat4 = mat4_impl<float>;
using mat4 = mat4_impl<f32>;
export using imat4 = mat4_impl<std::int32_t>;
using imat4 = mat4_impl<i32>;
export using umat4 = mat4_impl<std::uint32_t>;
using umat4 = mat4_impl<u32>;
} // namespace lt::math

View file

@ -1,26 +1,27 @@
export module math.trig;
import preliminary;
export namespace lt::math {
[[nodiscard]] constexpr auto radians(float degrees) -> float
[[nodiscard]] constexpr auto radians(f32 degrees) -> f32
{
return degrees * 0.01745329251994329576923690768489f;
}
[[nodiscard]] constexpr auto radians(double degrees) -> double
[[nodiscard]] constexpr auto radians(f64 degrees) -> f64
{
return degrees * 0.01745329251994329576923690768489;
}
[[nodiscard]] constexpr auto degrees(float radians) -> float
[[nodiscard]] constexpr auto degrees(f32 radians) -> f32
{
return radians * 57.295779513082320876798154814105f;
}
[[nodiscard]] constexpr auto degrees(double radians) -> double
[[nodiscard]] constexpr auto degrees(f64 radians) -> f64
{
return radians * 57.295779513082320876798154814105;
}
} // namespace lt::math

View file

@ -1,12 +1,15 @@
export module math.vec2;
import std;
import preliminary;
namespace lt::math {
export namespace lt::math {
export template<typename T = float>
template<typename T = f32>
requires(std::is_arithmetic_v<T>)
struct vec2_impl
{
static constexpr auto num_elements = 2u;
constexpr vec2_impl(): x(), y()
{
}
@ -29,15 +32,15 @@ struct vec2_impl
return !(*this == other);
}
[[nodiscard]] auto operator*(const vec2_impl<T> &other) const -> vec2_impl
[[nodiscard]] constexpr auto operator+(const vec2_impl<T> &other) const -> vec2_impl
{
return {
x * other.x,
y * other.y,
x + other.x,
y + other.y,
};
}
[[nodiscard]] auto operator-(const vec2_impl<T> &other) const -> vec2_impl
[[nodiscard]] constexpr auto operator-(const vec2_impl<T> &other) const -> vec2_impl
{
return {
x - other.x,
@ -45,25 +48,51 @@ struct vec2_impl
};
}
[[nodiscard]] auto operator*(float scalar) const -> vec2_impl
[[nodiscard]] constexpr auto operator*(const vec2_impl<T> &other) const -> vec2_impl
{
return {
x * scalar,
y * scalar,
x * other.x,
y * other.y,
};
}
T x; // NOLINT
[[nodiscard]] constexpr auto operator/(const vec2_impl<T> &other) const -> vec2_impl
{
return {
x / other.x,
y / other.y,
};
}
T y; // NOLINT
[[nodiscard]] constexpr auto operator[](u8 idx) -> T &
{
debug_check(idx <= num_elements, "vec2 out of bound: {}", idx);
return ((T *)this)[idx];
}
[[nodiscard]] constexpr auto operator[](u8 idx) const -> const T &
{
debug_check(idx < num_elements, "vec2 out of bound: {}", idx);
return ((T *)this)[idx];
}
friend auto operator<<(std::ostream &stream, vec2_impl<T> value) -> std::ostream &
{
stream << value.x << ", " << value.y;
return stream;
}
T x;
T y;
};
export using vec2 = vec2_impl<float>;
using vec2 = vec2_impl<f32>;
export using ivec2 = vec2_impl<std::int32_t>;
using ivec2 = vec2_impl<i32>;
export using uvec2 = vec2_impl<std::uint32_t>;
using uvec2 = vec2_impl<u32>;
} // namespace lt::math

View file

@ -0,0 +1,7 @@
import test;
import math.vec2;
Suite raii = "raii"_suite = [] {
Case { "happy path" } = [] {
};
};

View file

@ -1,13 +1,16 @@
export module math.vec3;
import preliminary;
import math.vec2;
import std;
namespace lt::math {
export namespace lt::math {
export template<typename T = float>
template<typename T = f32>
requires(std::is_arithmetic_v<T>)
struct vec3_impl
{
static constexpr auto num_elements = 3u;
constexpr vec3_impl(): x(), y(), z()
{
}
@ -20,6 +23,14 @@ struct vec3_impl
{
}
constexpr vec3_impl(vec2_impl<T> xy, T z): x(xy.x), y(xy.y), z(z)
{
}
constexpr vec3_impl(T x, vec2_impl<T> yz): x(x), y(yz.y), z(yz.z)
{
}
[[nodiscard]] auto operator==(const vec3_impl<T> &other) const -> bool
{
return x == other.x && y == other.y && z == other.z;
@ -30,6 +41,15 @@ struct vec3_impl
return !(*this == other);
}
[[nodiscard]] constexpr auto operator+(const vec3_impl<T> &other) const -> vec3_impl
{
return {
x + other.x,
y + other.y,
z + other.z,
};
}
[[nodiscard]] constexpr auto operator-(const vec3_impl<T> &other) const -> vec3_impl
{
return {
@ -48,28 +68,49 @@ struct vec3_impl
};
}
[[nodiscard]] constexpr auto operator/(const vec3_impl<T> &other) const -> vec3_impl
{
return {
x / other.x,
y / other.y,
z / other.z,
};
}
[[nodiscard]] constexpr auto operator[](u8 idx) -> T &
{
debug_check(idx <= num_elements, "vec3 out of bound: {}", idx);
return ((T *)this)[idx];
}
[[nodiscard]] constexpr auto operator[](u8 idx) const -> const T &
{
debug_check(idx < num_elements, "vec3 out of bound: {}", idx);
return ((T *)this)[idx];
}
friend auto operator<<(std::ostream &stream, vec3_impl<T> value) -> std::ostream &
{
stream << value.x << ", " << value.y << ", " << value.z;
return stream;
}
T x; // NOLINT
T x;
T y; // NOLINT
T y;
T z; // NOLINT
T z;
};
export using vec3 = vec3_impl<float>;
using vec3 = vec3_impl<f32>;
export using ivec3 = vec3_impl<std::int32_t>;
using ivec3 = vec3_impl<i32>;
export using uvec3 = vec3_impl<std::uint32_t>;
using uvec3 = vec3_impl<u32>;
} // namespace lt::math
template<typename T>
export template<typename T>
struct std::formatter<lt::math::vec3_impl<T>>
{
constexpr auto parse(std::format_parse_context &context)

View file

@ -1,13 +1,17 @@
export module math.vec4;
import preliminary;
import math.vec2;
import math.vec3;
import std;
namespace lt::math {
export namespace lt::math {
export template<typename T = float>
template<typename T = f32>
requires(std::is_arithmetic_v<T>)
struct vec4_impl
{
static constexpr auto num_elements = 4u;
constexpr vec4_impl(): x(), y(), z(), w()
{
}
@ -20,6 +24,26 @@ struct vec4_impl
{
}
constexpr vec4_impl(vec2_impl<T> xy, T z, T w): x(xy.x), y(xy.y), z(z), w(w)
{
}
constexpr vec4_impl(T x, T y, vec2_impl<T> zw): x(x), y(y), z(zw.z), w(zw.w)
{
}
constexpr vec4_impl(vec2_impl<T> xy, vec2_impl<T> zw): x(xy.x), y(xy.y), z(zw.z), w(zw.w)
{
}
constexpr vec4_impl(vec3_impl<T> xyz, T w): x(xyz.x), y(xyz.y), z(xyz.z), w(w)
{
}
constexpr vec4_impl(T x, vec3_impl<T> yzw): x(x), y(yzw.y), z(yzw.z), w(yzw.w)
{
}
[[nodiscard]] auto operator==(const vec4_impl<T> &other) const -> bool
{
return x == other.x && y == other.y && z == other.z && w == other.w;
@ -30,6 +54,16 @@ struct vec4_impl
return !(*this == other);
}
[[nodiscard]] constexpr auto operator+(const vec4_impl<T> &other) const -> vec4_impl
{
return {
x + other.x,
y + other.y,
z + other.z,
w + other.w,
};
}
[[nodiscard]] constexpr auto operator-(const vec4_impl<T> &other) const -> vec4_impl
{
return {
@ -40,14 +74,36 @@ struct vec4_impl
};
}
[[nodiscard]] constexpr auto operator[](std::size_t idx) -> T &
[[nodiscard]] constexpr auto operator*(const vec4_impl<T> &other) const -> vec4_impl
{
return values[idx];
return {
x * other.x,
y * other.y,
z * other.z,
w * other.w,
};
}
[[nodiscard]] constexpr auto operator[](std::size_t idx) const -> const T &
[[nodiscard]] constexpr auto operator/(const vec4_impl<T> &other) const -> vec4_impl
{
return values[idx];
return {
x / other.x,
y / other.y,
z / other.z,
w / other.w,
};
}
[[nodiscard]] constexpr auto operator[](u8 idx) -> T &
{
debug_check(idx <= num_elements, "vec4 out of bound: {}", idx);
return ((T *)this)[idx];
}
[[nodiscard]] constexpr auto operator[](u8 idx) const -> const T &
{
debug_check(idx < num_elements, "vec4 out of bound: {}", idx);
return ((T *)this)[idx];
}
friend auto operator<<(std::ostream &stream, vec4_impl<T> value) -> std::ostream &
@ -56,11 +112,6 @@ struct vec4_impl
return stream;
}
// NOLINTNEXTLINE
union
{
struct
{
T x;
T y;
@ -69,28 +120,12 @@ struct vec4_impl
T w;
};
struct
{
T r;
T g;
using vec4 = vec4_impl<f32>;
T b;
using ivec4 = vec4_impl<i32>;
T a;
};
struct
{
std::array<T, 4> values;
};
};
};
export using vec4 = vec4_impl<float>;
export using ivec4 = vec4_impl<std::int32_t>;
export using uvec4 = vec4_impl<std::uint32_t>;
using uvec4 = vec4_impl<u32>;
} // namespace lt::math

View file

@ -2,14 +2,14 @@ export module memory.null_on_move;
import logger;
import std;
import preliminary;
namespace lt::memory {
/** Holds an `Underlying_T`, assigns it to `null_value` when this object is moved.
*
* @note For avoiding the need to explicitly implement the move constructor for objects that hold
* Vulkan handles. But may serve other purposes, hence why I kept the implementation generic.
* non-raii-handles (eg. Vulkan, Wayland).
*/
export template<typename Underlying_T, Underlying_T null_value = nullptr>
class NullOnMove
@ -81,11 +81,6 @@ public:
return m_value;
}
operator std::uint64_t() const
{
return (std::uint64_t)m_value;
}
[[nodiscard]] auto get() -> Underlying_T &
{
return m_value;

View file

@ -1,6 +1,6 @@
export module memory.reference;
import std;
import preliminary;
namespace lt::memory {

View file

@ -1,6 +1,6 @@
export module memory.scope;
import std;
import preliminary;
namespace lt::memory {

View file

@ -7,12 +7,12 @@ import mirror.system;
import renderer.factory;
/** The ultimate entrypoint. */
auto main(int argc, char *argv[]) -> std::int32_t
auto main(i32 argc, char *argv[]) -> i32
{
try
{
std::ignore = argc;
std::ignore = argv;
ignore = argc;
ignore = argv;
auto application = lt::memory::create_scope<lt::Mirror>();
if (!application)

View file

@ -1,227 +0,0 @@
#pragma once
#include <app/layer.hpp>
#include <imgui.h>
#include <math/vec2.hpp>
#include <memory/reference.hpp>
#include <mirror/panels/asset_browser.hpp>
#include <mirror/panels/properties.hpp>
#include <mirror/panels/scene_hierarchy.hpp>
#include <renderer/texture.hpp>
namespace lt {
class Scene;
class EditorLayer: public Layer
{
public:
EditorLayer(const std::string &name);
~EditorLayer() override;
EditorLayer(EditorLayer &&) = delete;
EditorLayer(const EditorLayer &) = delete;
auto operator=(EditorLayer &&) const -> EditorLayer & = delete;
auto operator=(const EditorLayer &) const -> EditorLayer & = delete;
void on_update(float delta_time) override;
void on_render() override;
void on_user_interface_update() override;
private:
std::string m_scene_dir;
math::vec2 m_direction;
float m_speed = 1000.0f;
memory::Ref<Scene> m_scene;
memory::Ref<SceneHierarchyPanel> m_sceneHierarchyPanel;
memory::Ref<PropertiesPanel> m_properties_panel;
memory::Ref<AssetBrowserPanel> m_content_browser_panel;
memory::Ref<Framebuffer> m_framebuffer;
Entity m_camera_entity;
ImVec2 m_available_content_region_prev;
};
} // namespace lt
#include <app/application.hpp>
#include <asset_manager/asset_manager.hpp>
#include <camera/component.hpp>
#include <ecs/components.hpp>
#include <ecs/registry.hpp>
#include <ecs/serializer.hpp>
#include <input/input.hpp>
#include <input/key_codes.hpp>
#include <math/vec4.hpp>
#include <memory/reference.hpp>
#include <mirror/layers/editor_layer.hpp>
#include <renderer/framebuffer.hpp>
#include <renderer/texture.hpp>
#include <ui/ui.hpp>
namespace lt {
EditorLayer::EditorLayer(const std::string &name)
: Layer(name)
, m_scene_dir("")
, m_direction { 0.0, 0.0 }
{
m_scene = memory::create_ref<Scene>();
m_properties_panel = memory::create_ref<PropertiesPanel>();
m_sceneHierarchyPanel = memory::create_ref<SceneHierarchyPanel>(m_scene, m_properties_panel);
m_content_browser_panel = memory::create_ref<AssetBrowserPanel>(m_scene);
m_framebuffer = Framebuffer::create(
{
.width = 1,
.height = 1,
.samples = 1,
},
GraphicsContext::get_shared_context()
);
if (m_scene_dir.empty())
{
m_camera_entity = m_scene->create_entity("Camera");
m_camera_entity.add_component<CameraComponent>(SceneCamera(), true);
AssetManager::load_texture("Awesomeface", "data/assets/textures/awesomeface.asset");
auto entity = Entity { m_scene->create_entity("Awesomeface", {}) };
entity.add_component<SpriteRendererComponent>(
AssetManager::get_texture("Awesomeface"),
math::vec4 { 0.0f, 1.0f, 1.0f, 1.0f }
);
}
else
{
auto serializer = SceneSerializer { m_scene };
ensure(serializer.deserialize(m_scene_dir), "Failed to de-serialize: {}", m_scene_dir);
// m_camera_entity = m_scene->GetEntityByTag("Game Camera");
}
}
EditorLayer::~EditorLayer()
{
if (!m_scene_dir.empty())
{
auto serializer = SceneSerializer { m_scene };
serializer.serialize(m_scene_dir);
}
}
void EditorLayer::on_update(float delta_time)
{
m_scene->on_update(delta_time);
if (Input::get_keyboard_key(Key::A))
{
m_direction.x = -1.0;
}
else if (Input::get_keyboard_key(Key::D))
{
m_direction.x = 1.0f;
}
else
{
m_direction.x = 0.0;
}
if (Input::get_keyboard_key(Key::S))
{
m_direction.y = -1.0;
}
else if (Input::get_keyboard_key(Key::W))
{
m_direction.y = 1.0f;
}
else
{
m_direction.y = 0.0;
}
auto &translation = m_camera_entity.get_component<TransformComponent>().translation;
auto velocity = m_direction * m_speed * delta_time;
translation = translation * math::vec3 { velocity.x, velocity.y, 0.0f };
if (Input::get_keyboard_key(Key::Escape))
{
Application::quit();
}
}
void EditorLayer::on_render()
{
m_scene->on_render(m_framebuffer);
}
void EditorLayer::on_user_interface_update()
{
UserInterface::dockspace_begin();
ImGui::ShowDemoWindow();
if (ImGui::Begin("Game"))
{
Input::receive_game_events(ImGui::IsWindowFocused());
auto available_region = ImGui::GetContentRegionAvail();
if (m_available_content_region_prev.x != available_region.x
|| m_available_content_region_prev.y != available_region.y)
{
m_framebuffer->resize(
math::uvec2 {
static_cast<uint32_t>(available_region.x),
static_cast<uint32_t>(available_region.y),
}
);
auto &camera = m_camera_entity.get_component<CameraComponent>().camera;
camera.set_viewport_size(
static_cast<uint32_t>(available_region.x),
static_cast<uint32_t>(available_region.y)
);
m_available_content_region_prev = available_region;
}
if (GraphicsContext::get_graphics_api() == GraphicsAPI::DirectX)
{
ImGui::Image(m_framebuffer->get_color_attachment(), available_region);
}
else
{
ImGui::Image(
m_framebuffer->get_color_attachment(),
available_region,
ImVec2(0, 1),
ImVec2(1, 0)
);
}
}
ImGui::End();
// Panels
m_sceneHierarchyPanel->on_user_interface_update();
m_properties_panel->on_user_interface_update();
m_content_browser_panel->on_user_interface_update();
UserInterface::dockspace_end();
}
} // namespace lt

View file

@ -1,200 +0,0 @@
#pragma once
#include <filesystem>
#include <memory/reference.hpp>
#include <mirror/panels/panel.hpp>
#include <renderer/texture.hpp>
namespace lt {
class Scene;
class AssetBrowserPanel: public Panel
{
public:
AssetBrowserPanel(memory::Ref<Scene> active_scene);
void on_user_interface_update();
private:
enum class AssetType
{
none = 0,
scene,
directory,
text,
image,
};
std::filesystem::path m_current_directory;
const std::filesystem::path m_assets_path;
float m_file_size = 128.0f;
float m_file_padding = 8.0f;
memory::Ref<Scene> m_active_scene;
memory::Ref<Texture> m_directory_texture;
memory::Ref<Texture> m_scene_texture;
memory::Ref<Texture> m_image_texture;
memory::Ref<Texture> m_text_texture;
};
} // namespace lt
#include <asset_manager/asset_manager.hpp>
#include <ecs/registry.hpp>
#include <ecs/serializer.hpp>
#include <imgui.h>
#include <memory/reference.hpp>
#include <mirror/panels/asset_browser.hpp>
#include <renderer/texture.hpp>
namespace lt {
AssetBrowserPanel::AssetBrowserPanel(memory::Ref<Scene> active_scene)
: m_current_directory("./data/assets")
, m_assets_path("./data/assets")
, m_active_scene(std::move(active_scene))
{
AssetManager::load_texture("_Assets_Directory", "data/engine/icons/asset/dir.asset");
AssetManager::load_texture("_Assets_Scene", "data/engine/icons/asset/scene.asset");
AssetManager::load_texture("_Assets_Image", "data/engine/icons/asset/img.asset");
AssetManager::load_texture("_Assets_Text", "data/engine/icons/asset/txt.asset");
m_directory_texture = AssetManager::get_texture("_Assets_Directory");
m_scene_texture = AssetManager::get_texture("_Assets_Scene");
m_image_texture = AssetManager::get_texture("_Assets_Image");
m_text_texture = AssetManager::get_texture("_Assets_Text");
}
void AssetBrowserPanel::on_user_interface_update()
{
ImGui::Begin("Content Browser");
// Parent directory button
if (m_current_directory != std::filesystem::path("data/assets"))
{
if (ImGui::Button(" <-- "))
{
m_current_directory = m_current_directory.parent_path();
}
}
const auto available_region = ImGui::GetContentRegionAvail();
const auto cell_size = m_file_size + m_file_padding;
const auto column_count = std::clamp(
static_cast<uint32_t>(std::floor(available_region.x / cell_size)),
1u,
64u
);
if (ImGui::BeginTable("ContentBrowser", static_cast<int>(column_count)))
{
m_directory_texture->bind(0u);
for (const auto &directory_entry : std::filesystem::directory_iterator(m_current_directory))
{
const auto &path = directory_entry.path();
auto extension = directory_entry.path().extension().string();
auto asset_type = AssetType {};
if (extension.empty())
{
asset_type = AssetType::directory;
}
else if (extension == ".txt" || extension == ".glsl")
{
asset_type = AssetType::text;
}
else if (extension == ".png")
{
asset_type = AssetType::image;
}
else if (extension == ".scene")
{
asset_type = AssetType::scene;
}
else
{
asset_type = AssetType::none;
}
// Extension not supported
if (asset_type == AssetType::none)
{
continue;
}
// Button
const auto path_str = path.string();
ImGui::TableNextColumn();
ImGui::PushID(path_str.c_str());
switch (asset_type)
{
// Directory
case AssetType::directory:
if (ImGui::ImageButton(
path_str.c_str(),
m_directory_texture->get_texture(),
ImVec2(m_file_size, m_file_size)
))
{
m_current_directory /= path.filename();
}
break;
// Scene
case AssetType::scene:
if (ImGui::ImageButton(
path_str.c_str(),
m_scene_texture->get_texture(),
ImVec2(m_file_size, m_file_size)
))
{
auto serializer = SceneSerializer { m_active_scene };
log::info("Attempting to deserialize: {}", path.string());
serializer.deserialize(path.string());
}
break;
// Image
case AssetType::image:
if (ImGui::ImageButton(
path_str.c_str(),
m_image_texture->get_texture(),
ImVec2(m_file_size, m_file_size)
))
{
}
break;
// Text
case AssetType::text:
if (ImGui::ImageButton(
path_str.c_str(),
m_text_texture->get_texture(),
ImVec2(m_file_size, m_file_size)
))
{
}
break;
default: break;
}
// Label
ImGui::TextUnformatted(std::format("{}", path.filename().string()).c_str());
ImGui::PopID();
}
ImGui::EndTable();
}
ImGui::End();
}
} // namespace lt

View file

@ -1,13 +0,0 @@
#pragma once
namespace lt {
class Panel
{
public:
Panel() = default;
virtual ~Panel() = default;
};
} // namespace lt

View file

@ -1,345 +0,0 @@
#pragma once
#include <ecs/entity.hpp>
#include <math/vec3.hpp>
#include <mirror/panels/panel.hpp>
namespace lt {
class PropertiesPanel: public Panel
{
public:
PropertiesPanel() = default;
void on_user_interface_update();
void set_entity_context(const Entity &entity);
private:
void draw_vec3_control(
const std::string &label,
math::vec3 &values,
float reset_value = 0.0f,
float column_width = 100.0f
);
template<typename ComponentType, typename UIFunction>
void draw_component(const std::string &name, Entity entity, UIFunction function);
Entity m_entity_context;
};
} // namespace lt
#include <asset_manager/asset_manager.hpp>
#include <camera/component.hpp>
#include <ecs/components.hpp>
#include <imgui.h>
#include <imgui_internal.h>
#include <math/trig.hpp>
#include <mirror/panels/properties.hpp>
namespace lt {
void PropertiesPanel::on_user_interface_update()
{
ImGui::Begin("Properties");
if (m_entity_context.is_valid())
{
if (m_entity_context.has_component<TagComponent>())
{
auto &tagComponent = m_entity_context.get_component<TagComponent>();
auto buffer = std::array<char, 256> {};
memset(buffer.data(), 0, buffer.size());
strncpy(buffer.data(), tagComponent.tag.c_str(), buffer.size());
if (ImGui::InputText("##Tag", buffer.data(), buffer.size()))
{
tagComponent.tag = buffer.data();
}
}
ImGui::SameLine();
ImGui::PushItemWidth(-1);
if (ImGui::Button("Add component"))
{
ImGui::OpenPopup("Components");
}
if (ImGui::BeginPopup("Components"))
{
if (ImGui::Selectable(
"SpriteRenderer",
false,
m_entity_context.has_component<SpriteRendererComponent>() ?
ImGuiSelectableFlags_Disabled :
ImGuiSelectableFlags {}
))
{
m_entity_context.add_component<SpriteRendererComponent>(
lt::AssetManager::get_texture("awesomeface")
);
}
if (ImGui::Selectable(
"Camera",
false,
m_entity_context.has_component<CameraComponent>() ?
ImGuiSelectableFlags_Disabled :
ImGuiSelectableFlags {}
))
{
m_entity_context.add_component<CameraComponent>();
}
ImGui::EndPopup();
}
ImGui::PopItemWidth();
draw_component<TransformComponent>(
"Transform Component",
m_entity_context,
[&](auto &transformComponent) {
draw_vec3_control("Translation", transformComponent.translation);
}
);
draw_component<SpriteRendererComponent>(
"SpriteRenderer Component",
m_entity_context,
[&](auto &spriteRendererComponent) {
ImGui::ColorEdit4("Color", &spriteRendererComponent.tint[0]);
}
);
draw_component<CameraComponent>(
"Camera Component",
m_entity_context,
[&](auto &cameraComponent) {
auto &camera = cameraComponent.camera;
auto projection_type = camera.get_projection_type();
auto projection_types_str = std::array<const char *, 2> {
"Orthographic",
"Perspective",
};
if (ImGui::BeginCombo("ProjectionType", projection_types_str[(int)projection_type]))
{
for (auto idx = 0; idx < 2; idx++)
{
const auto is_selected = static_cast<int>(projection_type) == idx;
if (ImGui::Selectable(projection_types_str[idx], is_selected))
{
projection_type = static_cast<SceneCamera::ProjectionType>(idx);
camera.set_projection_type(projection_type);
}
if (is_selected)
{
ImGui::SetItemDefaultFocus();
}
}
ImGui::EndCombo();
}
if (projection_type == SceneCamera::ProjectionType::Orthographic)
{
auto ortho_size = float {};
auto near_plane = float {};
auto far_plane = float {};
ortho_size = camera.get_orthographic_size();
near_plane = camera.get_orthographic_near_plane();
far_plane = camera.get_orthographic_far_plane();
if (ImGui::DragFloat("Orthographic Size", &ortho_size))
{
camera.set_orthographic_size(ortho_size);
}
if (ImGui::DragFloat("Near Plane", &near_plane))
{
camera.set_orthographic_near_plane(near_plane);
}
if (ImGui::DragFloat("Far Plane", &far_plane))
{
camera.set_orthographic_far_plane(far_plane);
}
}
else // perspective
{
auto vertical_fov = float {};
auto near_plane = float {};
auto far_plane = float {};
vertical_fov = math::degrees(camera.get_perspective_vertical_fov());
near_plane = camera.get_perspective_near_plane();
far_plane = camera.get_perspective_far_plane();
if (ImGui::DragFloat("Vertical FOV", &vertical_fov))
{
camera.set_perspective_vertical_fov(math::radians(vertical_fov));
}
if (ImGui::DragFloat("Near Plane", &near_plane))
{
camera.set_perspective_near_plane(near_plane);
}
if (ImGui::DragFloat("Far Plane", &far_plane))
{
camera.set_perspective_far_plane(far_plane);
}
}
ImGui::Separator();
}
);
}
ImGui::End();
}
void PropertiesPanel::set_entity_context(const Entity &entity)
{
m_entity_context = entity;
}
void PropertiesPanel::draw_vec3_control(
const std::string &label,
math::vec3 &values,
float reset_value,
float column_width
)
{
auto &io = ImGui::GetIO();
auto *bold_font = io.Fonts->Fonts[0];
ImGui::Columns(2);
ImGui::SetColumnWidth(0, column_width);
ImGui::TextUnformatted(label.c_str());
ImGui::NextColumn();
ImGui::PushMultiItemsWidths(3, ImGui::CalcItemWidth());
ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2 { 0, 0 });
auto line_height = GImGui->Font->LegacySize + GImGui->Style.FramePadding.y * 2.0f;
auto button_size = ImVec2 { line_height + 3.0f, line_height };
ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0.8f, 0.1f, 0.15f, 1.0f));
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4(0.9f, 0.2f, 0.2f, 1.0f));
ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4(0.8f, 0.1f, 0.15f, 1.0f));
ImGui::PushFont(bold_font);
if (ImGui::Button("X", button_size))
{
values.x = reset_value;
}
ImGui::PopFont();
ImGui::PopStyleColor(3);
ImGui::SameLine();
ImGui::DragFloat("##X", &values.x, 0.1f);
ImGui::PopItemWidth();
ImGui::SameLine();
ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0.2f, 0.7f, 0.2f, 1.0f));
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4(0.3f, 0.8f, 0.3f, 1.0f));
ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4(0.2f, 0.7f, 0.2f, 1.0f));
ImGui::PushFont(bold_font);
if (ImGui::Button("Y", button_size))
{
values.y = reset_value;
}
ImGui::PopFont();
ImGui::PopStyleColor(3);
ImGui::SameLine();
ImGui::DragFloat("##Y", &values.y, 0.1f);
ImGui::PopItemWidth();
ImGui::SameLine();
ImGui::PushStyleColor(ImGuiCol_Button, ImVec4(0.1f, 0.25f, 0.8f, 1.0f));
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, ImVec4(0.2f, 0.35f, 0.9f, 1.0f));
ImGui::PushStyleColor(ImGuiCol_ButtonActive, ImVec4(0.1f, 0.25f, 0.8f, 1.0f));
ImGui::PushFont(bold_font);
if (ImGui::Button("Z", button_size))
{
values.z = reset_value;
}
ImGui::PopFont();
ImGui::PopStyleColor(3);
ImGui::SameLine();
ImGui::DragFloat("##Z", &values.z, 0.1f);
ImGui::PopItemWidth();
ImGui::PopStyleVar();
ImGui::Columns(1);
}
template<typename ComponentType, typename UIFunction>
void PropertiesPanel::draw_component(
const std::string &name,
Entity entity,
UIFunction user_interface_function
)
{
if (!entity.has_component<ComponentType>())
{
return;
}
auto &component = entity.get_component<ComponentType>();
auto available_region = ImGui::GetContentRegionAvail();
// NOLINTNEXTLINE
auto flags = ImGuiTreeNodeFlags_DefaultOpen | ImGuiTreeNodeFlags_SpanAvailWidth
| ImGuiTreeNodeFlags_Framed | ImGuiTreeNodeFlags_AllowItemOverlap
| ImGuiTreeNodeFlags_FramePadding;
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, { 4, 4 });
auto lineHeight = GImGui->Font->LegacySize + GImGui->Style.FramePadding.y * 2.0f;
ImGui::Separator();
// NOLINTNEXTLINE
if (ImGui::TreeNodeEx((void *)typeid(ComponentType).hash_code(), flags, name.c_str()))
{
ImGui::PopStyleVar();
ImGui::SameLine(available_region.x - lineHeight * .5f);
if (ImGui::Button("+", { lineHeight, lineHeight }))
{
ImGui::OpenPopup("ComponentSettings");
}
if (ImGui::BeginPopup("ComponentSettings"))
{
if (ImGui::Selectable("Remove component"))
{
entity.remove_component<ComponentType>();
}
ImGui::EndPopup();
}
user_interface_function(component);
ImGui::TreePop();
}
else
{
ImGui::PopStyleVar();
}
}
} // namespace lt

View file

@ -1,124 +0,0 @@
#pragma once
#include <ecs/entity.hpp>
#include <ecs/registry.hpp>
#include <memory/reference.hpp>
#include <mirror/panels/panel.hpp>
namespace lt {
class PropertiesPanel;
class SceneHierarchyPanel: public Panel
{
public:
SceneHierarchyPanel();
SceneHierarchyPanel(
memory::Ref<Scene> context,
memory::Ref<PropertiesPanel> properties_panel = nullptr
);
void on_user_interface_update();
void set_context(
memory::Ref<Scene> context,
memory::Ref<PropertiesPanel> properties_panel = nullptr
);
private:
void draw_node(Entity entity, const std::string &label);
memory::Ref<Scene> m_context;
memory::Ref<PropertiesPanel> m_properties_panel_context;
Entity m_selection_context;
};
} // namespace lt
#include <ecs/components.hpp>
#include <imgui.h>
#include <memory/reference.hpp>
#include <mirror/panels/properties.hpp>
#include <mirror/panels/scene_hierarchy.hpp>
namespace lt {
SceneHierarchyPanel::SceneHierarchyPanel(): m_context(nullptr), m_properties_panel_context(nullptr)
{
}
SceneHierarchyPanel::SceneHierarchyPanel(
memory::Ref<Scene> context,
memory::Ref<PropertiesPanel> properties_panel
)
: m_context(std::move(context))
, m_properties_panel_context(std::move(properties_panel))
{
}
void SceneHierarchyPanel::on_user_interface_update()
{
if (m_context)
{
ImGui::Begin("Hierarchy");
for (auto entityID : m_context->m_registry.view<TagComponent>())
{
auto entity = Entity {
static_cast<entt::entity>(entityID),
m_context.get(),
};
const auto &tag = entity.get_component<TagComponent>();
draw_node(entity, tag);
};
}
ImGui::End();
}
void SceneHierarchyPanel::set_context(
memory::Ref<Scene> context,
memory::Ref<PropertiesPanel> properties_panel
)
{
if (properties_panel)
{
m_properties_panel_context = std::move(properties_panel);
}
m_context = std::move(context);
}
void SceneHierarchyPanel::draw_node(Entity entity, const std::string &label)
{
auto flags = ImGuiTreeNodeFlags {
// NOLINTNEXTLINE
(m_selection_context == entity ? ImGuiTreeNodeFlags_Selected : ImGuiTreeNodeFlags {})
| ImGuiTreeNodeFlags_OpenOnArrow | ImGuiTreeNodeFlags_SpanFullWidth
};
// NOLINTNEXTLINE
const auto expanded = ImGui::TreeNodeEx(
std::bit_cast<void *>(static_cast<uint64_t>(entity)),
flags,
"%s",
label.c_str()
);
if (ImGui::IsItemClicked())
{
m_selection_context = entity;
m_properties_panel_context->set_entity_context(entity);
}
if (expanded)
{
ImGui::TextUnformatted("TEST_OPENED_TREE!");
ImGui::TreePop();
}
}
} // namespace lt

View file

@ -1,4 +1,6 @@
export module mirror.system;
import preliminary;
import math.vec3;
import camera.components;
import surface.requests;
@ -22,7 +24,6 @@ import app;
import app.system;
import ecs.entity;
import ecs.registry;
import std;
namespace lt {
@ -33,11 +34,11 @@ void renderer_callback(
std::any &user_data
)
{
std::ignore = message_severity;
std::ignore = message_type;
std::ignore = user_data;
ignore = message_severity;
ignore = message_type;
ignore = user_data;
log::debug("RENDERER CALLBACK: {}", std::string { data.message });
log::trace("< Renderer > ==> {}", std::string { data.message });
}
class MirrorSystem: public lt::app::ISystem
@ -45,8 +46,8 @@ class MirrorSystem: public lt::app::ISystem
public:
MirrorSystem(
memory::Ref<ecs::Registry> registry,
std::size_t quit_action_key,
std::array<std::size_t, 4ul> debug_action_keys
size_t quit_action_key,
std::array<size_t, 4ul> debug_action_keys
)
: m_registry(std::move(registry))
, m_quit_action_key(quit_action_key)
@ -84,7 +85,7 @@ public:
for (auto &[id, camera] :
m_registry->view<lt::camera::components::PerspectiveCamera>())
{
camera.vertical_fov += (static_cast<float>(tick.delta_time.count()) * 40.0f);
camera.vertical_fov += (static_cast<f32>(tick.delta_time.count()) * 40.0f);
}
}
@ -128,9 +129,9 @@ public:
private:
memory::Ref<ecs::Registry> m_registry;
std::size_t m_quit_action_key;
size_t m_quit_action_key;
std::array<std::size_t, 4ul> m_debug_action_keys {};
std::array<size_t, 4ul> m_debug_action_keys {};
app::TickResult m_last_tick_result {};
};
@ -183,7 +184,7 @@ public:
}
);
auto debug_action_keys = std::array<std::size_t, 4ul> {};
auto debug_action_keys = std::array<size_t, 4ul> {};
debug_action_keys[0] = input.add_action(
input::InputAction {
.name = "debug_1",

View file

@ -0,0 +1,85 @@
export module preliminary.assertions;
import preliminary.build_constants;
import std;
///////////////////////////////////////
// ----------* INTERFACE *--------- //
/////////////////////////////////////
/** To be used for ensuring a condition holds true, throws otherwise. */
export template<typename Expression_T, typename... Args_T>
struct ensure
{
ensure(
const Expression_T &expression,
std::format_string<Args_T...> fmt,
Args_T &&...args,
const std::source_location &location = std::source_location::current()
);
};
/** To be used for costly checks that should be stripped in release builds. */
export template<typename Expression_T, typename... Args_T>
struct debug_check
{
debug_check(
const Expression_T &expression,
std::format_string<Args_T...> fmt,
Args_T &&...args,
const std::source_location &location = std::source_location::current()
);
};
export template<typename Expression_T, typename... Args_T>
ensure(Expression_T, std::format_string<Args_T...>, Args_T &&...)
-> ensure<Expression_T, Args_T...>;
export template<typename Expression_T, typename... Args_T>
debug_check(Expression_T, std::format_string<Args_T...>, Args_T &&...)
-> debug_check<Expression_T, Args_T...>;
///////////////////////////////////////
// * IMPLEMENTATION -- TEMPLATES * //
/////////////////////////////////////
template<typename Expression_T, typename... Args_T>
ensure<Expression_T, Args_T...>::ensure(
const Expression_T &expression,
std::format_string<Args_T...> fmt,
Args_T &&...args,
const std::source_location &location
)
{
if (!static_cast<bool>(expression))
{
throw std::runtime_error { std::format(
"exception: {}\nlocation: {}:{}",
std::format(fmt, std::forward<Args_T>(args)...),
location.file_name(),
location.line()
) };
}
}
template<typename Expression_T, typename... Args_T>
debug_check<Expression_T, Args_T...>::debug_check(
const Expression_T &expression,
std::format_string<Args_T...> fmt,
Args_T &&...args,
const std::source_location &location
)
{
if constexpr (build_constants::build_type != build_constants::BuildType::debug)
{
return;
}
if (!static_cast<bool>(expression))
{
throw std::runtime_error { std::format(
"exception: {}\nlocation: {}:{}",
std::format(fmt, std::forward<Args_T>(args)...),
location.file_name(),
location.line()
) };
}
}

View file

@ -1,10 +1,11 @@
export module env;
export module preliminary.build_constants;
import preliminary.fundumental_types;
import std;
namespace lt {
export namespace build_constants {
enum class Platform : std::uint8_t
enum class Platform : u8
{
/** The GNU/Linux platform.
* Tested on the following distros: arch-x86_64
@ -26,7 +27,7 @@ enum class Platform : std::uint8_t
};
/** The compiler that was used for compiling the project. */
enum class Compiler : std::uint8_t
enum class Compiler : u8
{
clang,
gcc,
@ -34,37 +35,51 @@ enum class Compiler : std::uint8_t
apple_clang,
};
namespace constants {
enum class BuildType
{
debug,
release,
distribution
};
#if defined(LIGHT_PLATFORM_WINDOWS)
#define lt_win(x)
constexpr auto platform = Platform::windows;
constexpr auto platform_name = "windows";
constexpr auto platform_identifier = platform_name; // TODO(Light)
#undef LIGHT_PLATFORM_WINDOWS
#elif defined(LIGHT_PLATFORM_LINUX)
constexpr auto platform = Platform::gnu_linux;
constexpr auto platform_name = "gnu_linux";
constexpr auto platform_identifier = platform_name; // TODO(Light)
#elif defined(LIGHT_PLATFORM_MAC)
#define lt_mac(x) x
constexpr auto platform = Platform::mac;
constexpr auto platform_name = "mac";
constexpr auto platform_identifier = platform_name; // TODO(Light)
#else
#error "Unsupported platform: Unknown"
#endif
/** @TODO(Light): Handle other compilers... */
#ifdef __clang__
constexpr auto compiler = Compiler::clang;
constexpr auto compiler_name = "clang";
/** @todo(Light): insert the full identifier, including version information and such */
constexpr auto full_compiler_identifier = "clang";
/** @TODO(Light): insert the full identifier, including version information and such */
constexpr auto compiler_identifier = "clang";
#endif
} // namespace constants
// @TODO(Light): inject build info through CMake using LIGHT_... constant macros
#if defined(_DEBUG)
constexpr auto build_type = BuildType::debug;
#else
constexpr auto build_type = BuildType::release;
#endif
} // namespace lt
} // namespace build_constants

View file

@ -0,0 +1,22 @@
export module preliminary.fundumental_types;
import std;
export using byte = ::std::byte;
export using u8 = ::std::uint8_t;
export using u16 = ::std::uint16_t;
export using u32 = ::std::uint32_t;
export using u64 = ::std::uint64_t;
export using i8 = ::std::int8_t;
export using i16 = ::std::int16_t;
export using i32 = ::std::int32_t;
export using i64 = ::std::int64_t;
export using f32 = float;
export using f64 = double;
export using size_t = ::std::size_t;
export using ::std::ignore;

View file

@ -0,0 +1,7 @@
export module preliminary;
export import preliminary.fundumental_types;
export import preliminary.assertions;
export import preliminary.build_constants;
// std should always be available...
export import std;

View file

@ -3,25 +3,22 @@ import renderer.test_utils;
using enum ::lt::renderer::IDebugger::MessageSeverity;
using enum ::lt::renderer::IBuffer::Usage;
using ::std::this_thread::sleep_for;
// TODO(Light): finish these (and many other) tests...
Suite raii = "buffer_raii"_suite = [] {
Case { "happy path won't throw" } = [] {
Case { "happy paths" } = [] {
auto fixture = FixtureDeviceSwapchain {};
};
std::this_thread::sleep_for(std::chrono::milliseconds(500));
sleep_for(std::chrono::milliseconds { 500u });
Case { "unhappy path throws" } = [] {
Case { "unhappy paths" } = [] {
auto fixture = FixtureDeviceSwapchain {};
};
std::this_thread::sleep_for(std::chrono::milliseconds(500));
Case { "tapping" } = [] {
auto fixture = FixtureDeviceSwapchain {};
};
std::this_thread::sleep_for(std::chrono::milliseconds(500));
sleep_for(std::chrono::milliseconds { 500u });
Case { "mapping" } = [] {
auto fixture = FixtureDeviceSwapchain {};
};
std::this_thread::sleep_for(std::chrono::milliseconds(500));
sleep_for(std::chrono::milliseconds { 500u });
};

View file

@ -1,18 +1,9 @@
import renderer.frontend;
import renderer.test_utils;
void noop_callback(
lt::renderer::IDebugger::MessageSeverity message_severity,
lt::renderer::IDebugger::MessageType message_type,
const lt::renderer::IDebugger::MessageData &data,
std::any &user_data
)
{
}
Suite raii = "debugger_raii"_suite = [] {
Case { "happy path won't throw" } = [] {
std::ignore = lt::renderer::create_debugger(
Case { "happy paths" } = [] {
ignore = lt::renderer::create_debugger(
lt::renderer::Api::vulkan,
lt::renderer::get_instance(lt::renderer::Api::vulkan),
lt::renderer::IDebugger::CreateInfo {
@ -23,9 +14,9 @@ Suite raii = "debugger_raii"_suite = [] {
);
};
Case { "unhappy path throws" } = [] {
Case { "unhappy paths" } = [] {
expect_throw([] {
std::ignore = lt::renderer::create_debugger(
ignore = lt::renderer::create_debugger(
lt::renderer::Api::vulkan,
lt::renderer::get_instance(lt::renderer::Api::vulkan),
lt::renderer::IDebugger::CreateInfo {
@ -37,7 +28,7 @@ Suite raii = "debugger_raii"_suite = [] {
});
expect_throw([] {
std::ignore = lt::renderer::create_debugger(
ignore = lt::renderer::create_debugger(
lt::renderer::Api::vulkan,
lt::renderer::get_instance(lt::renderer::Api::vulkan),
lt::renderer::IDebugger::CreateInfo {
@ -49,7 +40,7 @@ Suite raii = "debugger_raii"_suite = [] {
});
expect_throw([] {
std::ignore = lt::renderer::create_debugger(
ignore = lt::renderer::create_debugger(
lt::renderer::Api::vulkan,
lt::renderer::get_instance(lt::renderer::Api::vulkan),
lt::renderer::IDebugger::CreateInfo {

View file

@ -2,12 +2,12 @@ import renderer.frontend;
import renderer.test_utils;
Suite raii = "device_raii"_suite = [] {
Case { "happy path won't throw" } = [] {
Case { "happy paths" } = [] {
auto fixture = Fixture_SurfaceGpu {};
std::ignore = lt::renderer::create_device(constants::api, fixture.gpu(), fixture.surface());
ignore = lt::renderer::create_device(constants::api, fixture.gpu(), fixture.surface());
};
Case { "unhappy path throws" } = [] {
Case { "unhappy paths" } = [] {
auto fixture = Fixture_SurfaceGpu {};
expect_throw([&] {

View file

@ -2,9 +2,9 @@ import renderer.frontend;
import renderer.test_utils;
Suite raii = "pass_raii"_suite = [] {
Case { "happy path won't throw" } = [] {
Case { "happy paths" } = [] {
auto fixture = FixtureDeviceSwapchain {};
std::ignore = lt::renderer::create_pass(
ignore = lt::renderer::create_pass(
constants::api,
fixture.device(),
lt::assets::ShaderAsset { "./data/test_assets/triangle.vert.asset" },
@ -17,10 +17,10 @@ Suite raii = "pass_raii"_suite = [] {
);
};
Case { "unhappy path throws" } = [] {
Case { "unhappy paths" } = [] {
auto fixture = FixtureDeviceSwapchain {};
expect_throw([&] {
std::ignore = lt::renderer::create_pass(
ignore = lt::renderer::create_pass(
constants::api,
nullptr,
lt::assets::ShaderAsset { "./data/test_assets/triangle.vert.asset" },
@ -29,7 +29,7 @@ Suite raii = "pass_raii"_suite = [] {
});
expect_throw([&] {
std::ignore = lt::renderer::create_pass(
ignore = lt::renderer::create_pass(
lt::renderer::Api::none,
fixture.device(),
lt::assets::ShaderAsset { "./data/test_assets/triangle.vert.asset" },
@ -38,7 +38,7 @@ Suite raii = "pass_raii"_suite = [] {
});
expect_throw([&] {
std::ignore = lt::renderer::create_pass(
ignore = lt::renderer::create_pass(
lt::renderer::Api::direct_x,
fixture.device(),
lt::assets::ShaderAsset { "./data/test_assets/triangle.vert.asset" },
@ -47,7 +47,7 @@ Suite raii = "pass_raii"_suite = [] {
});
expect_throw([&] {
std::ignore = lt::renderer::create_pass(
ignore = lt::renderer::create_pass(
lt::renderer::Api::metal,
fixture.device(),
lt::assets::ShaderAsset { "./data/test_assets/triangle.vert.asset" },

View file

@ -2,7 +2,7 @@ import renderer.frontend;
import renderer.test_utils;
Suite raii = "renderer_raii"_suite = [] {
Case { "happy path won't throw" } = [] {
Case { "happy paths" } = [] {
auto fixture = FixtureDeviceSwapchain {};
ignore = lt::renderer::create_renderer(
constants::api,
@ -18,7 +18,7 @@ Suite raii = "renderer_raii"_suite = [] {
);
};
Case { "unhappy path throws" } = [] {
Case { "unhappy paths" } = [] {
auto fixture = FixtureDeviceSwapchain {};
expect_throw([&] {

View file

@ -2,7 +2,7 @@ import renderer.frontend;
import renderer.test_utils;
Suite raii = "surface"_suite = [] {
Case { "happy path won't throw" } = [&] {
Case { "happy paths" } = [&] {
auto fixture = Fixture_SurfaceSystem {};
const auto surface = lt::renderer::create_surface(
@ -16,13 +16,13 @@ Suite raii = "surface"_suite = [] {
expect_eq(y, constants::resolution.y);
};
Case { "unhappy path throws" } = [&] {
Case { "unhappy paths" } = [&] {
auto registry = lt::memory::create_ref<lt::ecs::Registry>();
auto entity = lt::ecs::Entity { registry, registry->create_entity() };
auto system = lt::surface::System(registry);
expect_throw([&] {
std::ignore = lt::renderer::create_surface(
ignore = lt::renderer::create_surface(
constants::api,
lt::renderer::get_instance(constants::api),
entity
@ -38,11 +38,11 @@ Suite raii = "surface"_suite = [] {
);
expect_throw([&] {
std::ignore = lt::renderer::create_surface(constants::api, nullptr, entity);
ignore = lt::renderer::create_surface(constants::api, nullptr, entity);
});
expect_throw([&] {
std::ignore = lt::renderer::create_surface(
ignore = lt::renderer::create_surface(
lt::renderer::Api::none,
lt::renderer::get_instance(constants::api),
entity
@ -50,7 +50,7 @@ Suite raii = "surface"_suite = [] {
});
expect_throw([&] {
std::ignore = lt::renderer::create_surface(
ignore = lt::renderer::create_surface(
lt::renderer::Api::direct_x,
lt::renderer::get_instance(constants::api),
entity
@ -58,7 +58,7 @@ Suite raii = "surface"_suite = [] {
});
expect_throw([&] {
std::ignore = lt::renderer::create_surface(
ignore = lt::renderer::create_surface(
lt::renderer::Api::metal,
lt::renderer::get_instance(constants::api),
entity
@ -66,7 +66,7 @@ Suite raii = "surface"_suite = [] {
});
// Ensure base creation info is non-throwing
std::ignore = lt::renderer::create_surface(
ignore = lt::renderer::create_surface(
constants::api,
lt::renderer::get_instance(constants::api),
entity

View file

@ -1,3 +1,4 @@
import preliminary;
import time;
import renderer.frontend;
import renderer.test_utils;
@ -17,95 +18,64 @@ struct RendererContext
};
Suite raii = "system_raii"_suite = [] {
Case { "sandbox" } = [] {
Case { "happy paths" } = [] {
auto fixture = Fixture_RendererSystem {};
auto &surface_system = fixture.surface_system();
auto &renderer_system = fixture.renderer_system();
auto timer = lt::time::Timer {};
lt::log::trace("Ticking for 3 seconds...");
while (timer.elapsed_time() < std::chrono::seconds { 3 })
{
surface_system.tick({});
renderer_system.tick({});
}
lt::log::trace("Three seconds passed, quitting...");
expect_false(fixture.has_any_messages_of(lt::renderer::IDebugger::MessageSeverity::error));
expect_false(
fixture.has_any_messages_of(lt::renderer::IDebugger::MessageSeverity::warning)
);
};
// Case { "happy path won't throw" } = [] {
// ignore = Fixture_RendererSystem {};
//
//
// auto timer = lt::time::Timer {};
// lt::log::trace("Ticking for 3 seconds...");
// while (timer.elapsed_time() < std::chrono::seconds { 3 })
// {
// system.tick({});
// }
//
// lt::log::trace("Three seconds passed, quitting...");
// };
//
// Case { "happy path has no errors" } = [] {
// auto fixture = Fixture_RendererSystem {};
// expect_false(fixture.has_any_messages_of(lt::renderer::IDebugger::MessageSeverity::error));
// expect_false(
// fixture.has_any_messages_of(lt::renderer::IDebugger::MessageSeverity::warning)
// );
// };
//
// Case { "unhappy path throws" } = [] {
// auto fixture = Fixture_SurfaceSystem {};
// auto empty_entity = lt::ecs::Entity { fixture.registry(),
// fixture.registry()->create_entity() };
// auto info = fixture.renderer_system_create_info();
//
// expect_throw([=] mutable {
// info.registry = nullptr;
// ignore = lt::renderer::System { info };
// });
//
// expect_throw([=] mutable {
// info.surface_entity = lt::ecs::Entity({}, {});
// ignore = lt::renderer::System { info };
// });
//
// expect_throw([=] mutable {
// info.config.target_api = lt::renderer::Api::none;
// ignore = lt::renderer::System { info };
// });
//
// // unsupported Apis
// expect_throw([=] mutable {
// info.config.target_api = lt::renderer::Api::direct_x;
// ignore = lt::renderer::System { info };
// });
//
// expect_throw([=] mutable {
// info.config.target_api = lt::renderer::Api::metal;
// ignore = lt::renderer::System { info };
// });
//
// expect_throw([=] mutable {
// constexpr auto limit = lt::renderer::System::frames_in_flight_upper_limit;
// info.config.max_frames_in_flight = limit + 1u;
// ignore = lt::renderer::System { info };
// });
//
// expect_throw([=] mutable {
// constexpr auto limit = lt::renderer::System::frames_in_flight_lower_limit;
// info.config.max_frames_in_flight = limit - 1u;
// ignore = lt::renderer::System { info };
// });
//
// expect_throw([=] mutable {
// info.debug_callback_info = lt::renderer::IDebugger::CreateInfo {};
// ignore = lt::renderer::System { info };
// });
//
// // Make sure the base info is not at fault for unhappiness.
// ignore = lt::renderer::System { info };
// };
Case { "unhappy paths" } = [] {
auto fixture = Fixture_SurfaceSystem {};
auto empty_entity = lt::ecs::Entity { fixture.registry(),
fixture.registry()->create_entity() };
auto info = fixture.renderer_system_create_info();
expect_throw([=] mutable {
info.registry = nullptr;
ignore = lt::renderer::System { info };
});
expect_throw([=] mutable {
info.surface_entity = lt::ecs::Entity({}, {});
ignore = lt::renderer::System { info };
});
expect_throw([=] mutable {
info.config.target_api = lt::renderer::Api::none;
ignore = lt::renderer::System { info };
});
// unsupported Apis
expect_throw([=] mutable {
info.config.target_api = lt::renderer::Api::direct_x;
ignore = lt::renderer::System { info };
});
expect_throw([=] mutable {
info.config.target_api = lt::renderer::Api::metal;
ignore = lt::renderer::System { info };
});
expect_throw([=] mutable {
constexpr auto limit = lt::renderer::System::frames_in_flight_upper_limit;
info.config.max_frames_in_flight = limit + 1u;
ignore = lt::renderer::System { info };
});
expect_throw([=] mutable {
constexpr auto limit = lt::renderer::System::frames_in_flight_lower_limit;
info.config.max_frames_in_flight = limit - 1u;
ignore = lt::renderer::System { info };
});
expect_throw([=] mutable {
info.debug_callback_info = lt::renderer::IDebugger::CreateInfo {};
ignore = lt::renderer::System { info };
});
// Make sure the base info is not at fault for unhappiness.
ignore = lt::renderer::System { info };
};
};

View file

@ -1,11 +1,9 @@
export module renderer.test_utils;
export import logger;
export import test;
export import surface.system;
export import ecs.registry;
export import renderer.factory;
export import test.test;
export import test.expects;
export import memory.reference;
export import renderer.frontend;
export import renderer.system;
@ -13,32 +11,21 @@ export import math.vec2;
export import math.vec3;
export import math.vec4;
export import math.mat4;
export import std;
export using ::lt::test::Case;
export using ::lt::test::expect_eq;
export using ::lt::test::expect_false;
export using ::lt::test::expect_not_nullptr;
export using ::lt::test::expect_throw;
export using ::lt::test::operator""_suite;
export using ::lt::test::expect_true;
export using ::lt::test::Suite;
export using ::std::ignore;
export namespace constants {
constexpr auto api = lt::renderer::Api::vulkan;
constexpr auto resolution = lt::math::uvec2 { 800u, 600u };
constexpr auto frames_in_flight = std::uint32_t { 3u };
constexpr auto frames_in_flight = u32 { 3u };
} // namespace constants
void noop_messenger_callback(
lt::renderer::IDebugger::MessageSeverity severity,
lt::renderer::IDebugger::MessageType type,
const lt::renderer::IDebugger::MessageData &data,
std::any &user_data
export void noop_callback(
lt::renderer::IDebugger::MessageSeverity,
lt::renderer::IDebugger::MessageType,
const lt::renderer::IDebugger::MessageData &,
std::any &
)
{
}
@ -69,7 +56,7 @@ public:
.debug_callback_info = {
.severities = lt::renderer::IDebugger::MessageSeverity::all,
.types= lt::renderer::IDebugger::MessageType::all,
.callback = noop_messenger_callback,
.callback = noop_callback,
.user_data = {},
}
} ;
@ -157,7 +144,7 @@ public:
}
[[nodiscard]] auto has_any_messages_of(lt::renderer::IDebugger ::MessageSeverity severity) const
-> std::uint32_t
-> u32
{
return m_user_data->m_severity_counter.contains(severity);
}
@ -173,8 +160,8 @@ private:
// I know this makes the tests too verbose...
// but makes it easier to figure out what the problem is when things fail on ci
lt::log::trace("vulkan: {}", std::string { data.message });
std::ignore = data;
std::ignore = type;
ignore = data;
ignore = type;
auto *fixture = std::any_cast<UserData *>(user_data);
fixture->m_has_any_messages = true;
@ -183,8 +170,7 @@ private:
struct UserData
{
std::unordered_map<lt::renderer::IDebugger::MessageSeverity, std::uint32_t>
m_severity_counter;
std::unordered_map<lt::renderer::IDebugger::MessageSeverity, u32> m_severity_counter;
bool m_has_any_messages {};
};
@ -227,7 +213,7 @@ public:
}
[[nodiscard]] auto has_any_messages_of(lt::renderer::IDebugger ::MessageSeverity severity) const
-> std::uint32_t
-> u32
{
return m_user_data->m_severity_counter.contains(severity);
}
@ -244,8 +230,8 @@ private:
// but makes it easier to figure out what the problem is when things fail on ci
lt::log::trace("vulkan: {}", std::string { data.message });
std::ignore = data;
std::ignore = type;
ignore = data;
ignore = type;
auto *fixture = std::any_cast<UserData *>(user_data);
fixture->m_has_any_messages = true;
@ -254,8 +240,7 @@ private:
struct UserData
{
std::unordered_map<lt::renderer::IDebugger::MessageSeverity, std::uint32_t>
m_severity_counter;
std::unordered_map<lt::renderer::IDebugger::MessageSeverity, u32> m_severity_counter;
bool m_has_any_messages {};
};

View file

@ -1,19 +1,20 @@
export module renderer.components;
import preliminary;
import assets.shader;
import math.vec3;
import memory.reference;
import std;
export namespace lt::renderer::components {
enum class VertexFormat : std::uint8_t
enum class VertexFormat : u8
{
r32_g32_b32_sfloat,
r32_g32_sfloat,
};
enum class VertexInputRate : std::uint8_t
enum class VertexInputRate : u8
{
per_vertex,
@ -22,20 +23,20 @@ enum class VertexInputRate : std::uint8_t
struct VertexInputAttributeDescriptipn
{
std::uint32_t location;
u32 location;
std::uint32_t binding;
u32 binding;
std::uint32_t offset;
u32 offset;
VertexFormat format;
};
struct VertexInputBindingDescription
{
std::uint32_t binding;
u32 binding;
std::uint32_t stride;
u32 stride;
};
/** Requires a math::components::Transform component on the same entity to be functional. */

View file

@ -2,10 +2,9 @@ export module renderer.data;
import math.mat4;
export namespace lt::renderer {
namespace lt::renderer {
export struct FrameConstants
struct FrameConstants
{
math::mat4 view_projection;
};

View file

@ -11,9 +11,9 @@ export import renderer.vk.gpu;
export import renderer.vk.debugger;
export import renderer.vk.surface;
export import memory.scope;
export import debug.assertions;
export import ecs.entity;
export import std;
import preliminary;
export namespace lt::renderer {
@ -48,7 +48,7 @@ export namespace lt::renderer {
IGpu *gpu,
IDevice *device,
ISwapchain *swapchain,
std::uint32_t max_frames_in_flight
u32 max_frames_in_flight
) -> memory::Scope<IRenderer>;
[[nodiscard]] auto create_buffer(
@ -82,7 +82,7 @@ namespace lt::renderer {
const lt::ecs::Entity &surface_entity
) -> memory::Scope<ISurface>
{
debug::ensure(instance, "Failed to create renderer::ISurface: null instance");
ensure(instance, "Failed to create renderer::ISurface: null instance");
switch (target_api)
{
@ -111,8 +111,8 @@ namespace lt::renderer {
[[nodiscard]] auto create_device(Api target_api, IGpu *gpu, ISurface *surface)
-> memory::Scope<IDevice>
{
debug::ensure(gpu, "Failed to create renderer::IDevice: null gpu");
debug::ensure(surface, "Failed to create renderer::IDevice: null surface");
ensure(gpu, "Failed to create renderer::IDevice: null gpu");
ensure(surface, "Failed to create renderer::IDevice: null surface");
switch (target_api)
{
@ -146,9 +146,9 @@ namespace lt::renderer {
const IBuffer::CreateInfo &info
) -> memory::Scope<IBuffer>
{
debug::ensure(device, "Failed to create renderer::IBuffer: null device");
debug::ensure(gpu, "Failed to create renderer::IBuffer: null gpu");
debug::ensure(info.size > 0, "Failed to create renderer::IBuffer: null size");
ensure(device, "Failed to create renderer::IBuffer: null device");
ensure(gpu, "Failed to create renderer::IBuffer: null gpu");
ensure(info.size > 0, "Failed to create renderer::IBuffer: null size");
switch (target_api)
{
@ -169,7 +169,7 @@ namespace lt::renderer {
const lt::assets::ShaderAsset &fragment_shader
) -> memory::Scope<IPass>
{
debug::ensure(device, "Failed to create renderer::IPass: null device");
ensure(device, "Failed to create renderer::IPass: null device");
switch (target_api)
{
@ -188,13 +188,13 @@ namespace lt::renderer {
IGpu *gpu,
IDevice *device,
ISwapchain *swapchain,
std::uint32_t max_frames_in_flight
u32 max_frames_in_flight
) -> memory::Scope<IRenderer>
{
debug::ensure(gpu, "Failed to create renderer::IRenderer: null gpu");
debug::ensure(device, "Failed to create renderer::IRenderer: null device");
debug::ensure(swapchain, "Failed to create renderer::IRenderer: null swapchain");
debug::ensure(
ensure(gpu, "Failed to create renderer::IRenderer: null gpu");
ensure(device, "Failed to create renderer::IRenderer: null device");
ensure(swapchain, "Failed to create renderer::IRenderer: null swapchain");
ensure(
std::clamp(
max_frames_in_flight,
IRenderer::frames_in_flight_lower_limit,
@ -223,17 +223,17 @@ namespace lt::renderer {
[[nodiscard]] auto create_debugger(Api target_api, IInstance *instance, IDebugger::CreateInfo info)
-> memory::Scope<IDebugger>
{
debug::ensure(
ensure(
info.severities != IDebugger::MessageSeverity::none,
"Failed to create renderer::IDebugger: severities == none"
);
debug::ensure(
ensure(
info.types != IDebugger::MessageType::none,
"Failed to create renderer::IDebugger: types == none"
);
debug::ensure(info.callback, "Failed to create vk::Messenger: null callback");
ensure(info.callback, "Failed to create vk::Messenger: null callback");
switch (target_api)
{

View file

@ -1,4 +1,6 @@
export module renderer.frontend;
import preliminary;
import renderer.data;
import renderer.components;
import bitwise;
@ -7,11 +9,10 @@ import assets.shader;
import ecs.entity;
import math.vec2;
import memory.scope;
import std;
export namespace lt::renderer {
enum class Api : std::uint8_t
enum class Api : u8
{
none = 0u,
@ -65,7 +66,7 @@ public:
class IBuffer
{
public:
enum class Usage : std::uint8_t
enum class Usage : u8
{
vertex,
@ -80,27 +81,27 @@ public:
{
Usage usage;
std::size_t size;
size_t size;
std::string debug_name;
};
struct CopyInfo
{
std::size_t offset;
size_t offset;
std::size_t size;
size_t size;
};
IBuffer() = default;
virtual ~IBuffer() = default;
[[nodiscard]] virtual auto map() -> std::span<std::byte> = 0;
[[nodiscard]] virtual auto map() -> std::span<byte> = 0;
virtual void unmap() = 0;
[[nodiscard]] virtual auto get_size() const -> std::size_t = 0;
[[nodiscard]] virtual auto get_size() const -> size_t = 0;
private:
};
@ -120,7 +121,7 @@ public:
static constexpr auto frames_in_flight_lower_limit = 1u;
enum class Result : std::uint8_t
enum class Result : u8
{
success = 0,
invalid_swapchain,
@ -131,7 +132,7 @@ public:
virtual ~IRenderer() = default;
virtual auto frame(std::uint32_t frame_idx, std::function<void()> submit_scene) -> Result = 0;
virtual auto frame(u32 frame_idx, std::function<void()> submit_scene) -> Result = 0;
virtual void replace_swapchain(class ISwapchain *swapchain) = 0;
@ -146,7 +147,7 @@ public:
class IDebugger
{
public:
enum class MessageSeverity : std::uint8_t
enum class MessageSeverity : u8
{
none = 0u,
@ -158,7 +159,7 @@ public:
all = verbose | info | warning | error,
};
enum class MessageType : std::uint8_t
enum class MessageType : u8
{
none = 0u,
general = bitwise::bit(0u),

View file

@ -1,6 +1,7 @@
export module renderer.system;
import preliminary;
import logger;
import debug.assertions;
import math.mat4;
import renderer.factory;
import app.system;
@ -16,9 +17,8 @@ import renderer.components;
import math.components;
import math.algebra;
import math.trig;
import std;
namespace lt::renderer {
export namespace lt::renderer {
/** The main rendering engine.
*
@ -27,9 +27,11 @@ namespace lt::renderer {
* - Connecting the context to the physical devices (select gpu, create surface, logical device)
* - Rendering the scene represented in registry via lt::renderer::components.
*/
export class System: public app::ISystem
class System: public app::ISystem
{
public:
// TODO(Light): this is some horrible design... fix it :(
/** config.max_frames_in_flight should not be higher than this value. */
static constexpr auto frames_in_flight_upper_limit = 5u;
@ -40,7 +42,7 @@ public:
{
Api target_api;
std::uint32_t max_frames_in_flight;
u32 max_frames_in_flight;
};
struct CreateInfo
@ -106,9 +108,9 @@ private:
app::TickResult m_last_tick_result {};
std::uint32_t m_frame_idx {};
u32 m_frame_idx {};
std::uint32_t m_max_frames_in_flight {};
u32 m_max_frames_in_flight {};
};
} // namespace lt::renderer
@ -123,8 +125,8 @@ System::System(CreateInfo info)
, m_instance(get_instance(m_api))
, m_max_frames_in_flight(info.config.max_frames_in_flight)
{
debug::ensure(m_registry, "Failed to initialize renderer::System: null registry");
debug::ensure(
ensure(m_registry, "Failed to initialize renderer::System: null registry");
ensure(
std::clamp(
info.config.max_frames_in_flight,
frames_in_flight_lower_limit,
@ -162,7 +164,7 @@ void System::on_unregister()
void System::tick(app::TickInfo tick)
{
std::ignore = tick;
ignore = tick;
handle_surface_resized_events();
auto frame_result = m_renderer->frame(m_frame_idx, [this] { submit_scene(); });

View file

@ -40,11 +40,10 @@ struct wl_surface;
#endif
export module renderer.vk.api_wrapper;
import preliminary;
import memory.null_on_move;
import math.vec3;
import math.vec2;
import debug.assertions;
import std;
import logger;
template<class... Ts>
@ -490,57 +489,57 @@ enum class Format : std::underlying_type_t<VkFormat>
r16_sscaled = VK_FORMAT_R16_SSCALED,
r16_uint = VK_FORMAT_R16_UINT,
r16_sint = VK_FORMAT_R16_SINT,
r16_sfloat = VK_FORMAT_R16_SFLOAT,
r16_sf32 = VK_FORMAT_R16_SFLOAT,
r16g16_unorm = VK_FORMAT_R16G16_UNORM,
r16g16_snorm = VK_FORMAT_R16G16_SNORM,
r16g16_uscaled = VK_FORMAT_R16G16_USCALED,
r16g16_sscaled = VK_FORMAT_R16G16_SSCALED,
r16g16_uint = VK_FORMAT_R16G16_UINT,
r16g16_sint = VK_FORMAT_R16G16_SINT,
r16g16_sfloat = VK_FORMAT_R16G16_SFLOAT,
r16g16_sf32 = VK_FORMAT_R16G16_SFLOAT,
r16g16b16_unorm = VK_FORMAT_R16G16B16_UNORM,
r16g16b16_snorm = VK_FORMAT_R16G16B16_SNORM,
r16g16b16_uscaled = VK_FORMAT_R16G16B16_USCALED,
r16g16b16_sscaled = VK_FORMAT_R16G16B16_SSCALED,
r16g16b16_uint = VK_FORMAT_R16G16B16_UINT,
r16g16b16_sint = VK_FORMAT_R16G16B16_SINT,
r16g16b16_sfloat = VK_FORMAT_R16G16B16_SFLOAT,
r16g16b16_sf32 = VK_FORMAT_R16G16B16_SFLOAT,
r16g16b16a16_unorm = VK_FORMAT_R16G16B16A16_UNORM,
r16g16b16a16_snorm = VK_FORMAT_R16G16B16A16_SNORM,
r16g16b16a16_uscaled = VK_FORMAT_R16G16B16A16_USCALED,
r16g16b16a16_sscaled = VK_FORMAT_R16G16B16A16_SSCALED,
r16g16b16a16_uint = VK_FORMAT_R16G16B16A16_UINT,
r16g16b16a16_sint = VK_FORMAT_R16G16B16A16_SINT,
r16g16b16a16_sfloat = VK_FORMAT_R16G16B16A16_SFLOAT,
r16g16b16a16_sf32 = VK_FORMAT_R16G16B16A16_SFLOAT,
r32_uint = VK_FORMAT_R32_UINT,
r32_sint = VK_FORMAT_R32_SINT,
r32_sfloat = VK_FORMAT_R32_SFLOAT,
r32_sf32 = VK_FORMAT_R32_SFLOAT,
r32g32_uint = VK_FORMAT_R32G32_UINT,
r32g32_sint = VK_FORMAT_R32G32_SINT,
r32g32_sfloat = VK_FORMAT_R32G32_SFLOAT,
r32g32_sf32 = VK_FORMAT_R32G32_SFLOAT,
r32g32b32_uint = VK_FORMAT_R32G32B32_UINT,
r32g32b32_sint = VK_FORMAT_R32G32B32_SINT,
r32g32b32_sfloat = VK_FORMAT_R32G32B32_SFLOAT,
r32g32b32_sf32 = VK_FORMAT_R32G32B32_SFLOAT,
r32g32b32a32_uint = VK_FORMAT_R32G32B32A32_UINT,
r32g32b32a32_sint = VK_FORMAT_R32G32B32A32_SINT,
r32g32b32a32_sfloat = VK_FORMAT_R32G32B32A32_SFLOAT,
r32g32b32a32_sf32 = VK_FORMAT_R32G32B32A32_SFLOAT,
r64_uint = VK_FORMAT_R64_UINT,
r64_sint = VK_FORMAT_R64_SINT,
r64_sfloat = VK_FORMAT_R64_SFLOAT,
r64_sf32 = VK_FORMAT_R64_SFLOAT,
r64g64_uint = VK_FORMAT_R64G64_UINT,
r64g64_sint = VK_FORMAT_R64G64_SINT,
r64g64_sfloat = VK_FORMAT_R64G64_SFLOAT,
r64g64_sf32 = VK_FORMAT_R64G64_SFLOAT,
r64g64b64_uint = VK_FORMAT_R64G64B64_UINT,
r64g64b64_sint = VK_FORMAT_R64G64B64_SINT,
r64g64b64_sfloat = VK_FORMAT_R64G64B64_SFLOAT,
r64g64b64_sf32 = VK_FORMAT_R64G64B64_SFLOAT,
r64g64b64a64_uint = VK_FORMAT_R64G64B64A64_UINT,
r64g64b64a64_sint = VK_FORMAT_R64G64B64A64_SINT,
r64g64b64a64_sfloat = VK_FORMAT_R64G64B64A64_SFLOAT,
r64g64b64a64_sf32 = VK_FORMAT_R64G64B64A64_SFLOAT,
b10g11r11_ufloat_pack32 = VK_FORMAT_B10G11R11_UFLOAT_PACK32,
e5b9g9r9_ufloat_pack32 = VK_FORMAT_E5B9G9R9_UFLOAT_PACK32,
d16_unorm = VK_FORMAT_D16_UNORM,
x8_d24_unorm_pack32 = VK_FORMAT_X8_D24_UNORM_PACK32,
d32_sfloat = VK_FORMAT_D32_SFLOAT,
d32_sf32 = VK_FORMAT_D32_SFLOAT,
s8_uint = VK_FORMAT_S8_UINT,
d16_unorm_s8_uint = VK_FORMAT_D16_UNORM_S8_UINT,
d24_unorm_s8_uint = VK_FORMAT_D24_UNORM_S8_UINT,
@ -696,9 +695,9 @@ struct Viewport
math::vec2 extent;
float min_depth {};
f32 min_depth {};
float max_depth {};
f32 max_depth {};
};
struct Rect2d
@ -724,7 +723,7 @@ public:
struct Setting
{
std::string name;
std::variant<std::vector<const char *>, std::uint32_t, bool> values;
std::variant<std::vector<const char *>, u32, bool> values;
};
std::string name;
@ -806,9 +805,9 @@ public:
struct Capabilities
{
std::uint32_t min_image_count;
u32 min_image_count;
std::uint32_t max_image_count;
u32 max_image_count;
math::uvec2 current_extent;
@ -816,7 +815,7 @@ public:
math::uvec2 max_image_extent;
std::uint32_t max_image_array_layers;
u32 max_image_array_layers;
std::underlying_type_t<Transform> supported_transforms;
@ -974,112 +973,112 @@ public:
struct Limits
{
std::uint32_t max_image_dimension_1d;
std::uint32_t max_image_dimension_2d;
std::uint32_t max_image_dimension_3d;
std::uint32_t max_image_dimension_cube;
std::uint32_t max_image_array_layers;
std::uint32_t max_texel_buffer_elements;
std::uint32_t max_uniform_buffer_range;
std::uint32_t max_storage_buffer_range;
std::uint32_t max_push_constants_size;
std::uint32_t max_memory_allocation_count;
std::uint32_t max_sampler_allocation_count;
std::size_t buffer_image_granularity;
std::size_t sparse_address_space_size;
std::uint32_t max_bound_descriptor_sets;
std::uint32_t max_per_stage_descriptor_samplers;
std::uint32_t max_per_stage_descriptor_uniform_buffers;
std::uint32_t max_per_stage_descriptor_storage_buffers;
std::uint32_t max_per_stage_descriptor_sampled_images;
std::uint32_t max_per_stage_descriptor_storage_images;
std::uint32_t max_per_stage_descriptor_input_attachments;
std::uint32_t max_per_stage_resources;
std::uint32_t max_descriptor_set_samplers;
std::uint32_t max_descriptor_set_uniform_buffers;
std::uint32_t max_descriptor_set_uniform_buffers_dynamic;
std::uint32_t max_descriptor_set_storage_buffers;
std::uint32_t max_descriptor_set_storage_buffers_dynamic;
std::uint32_t max_descriptor_set_sampled_images;
std::uint32_t max_descriptor_set_storage_images;
std::uint32_t max_descriptor_set_input_attachments;
std::uint32_t max_vertex_input_attributes;
std::uint32_t max_vertex_input_bindings;
std::uint32_t max_vertex_input_attribute_offset;
std::uint32_t max_vertex_input_binding_stride;
std::uint32_t max_vertex_output_components;
std::uint32_t max_tessellation_generation_level;
std::uint32_t max_tessellation_patch_size;
std::uint32_t max_tessellation_control_per_vertex_input_components;
std::uint32_t max_tessellation_control_per_vertex_output_components;
std::uint32_t max_tessellation_control_per_patch_output_components;
std::uint32_t max_tessellation_control_total_output_components;
std::uint32_t max_tessellation_evaluation_input_components;
std::uint32_t max_tessellation_evaluation_output_components;
std::uint32_t max_geometry_shader_invocations;
std::uint32_t max_geometry_input_components;
std::uint32_t max_geometry_output_components;
std::uint32_t max_geometry_output_vertices;
std::uint32_t max_geometry_total_output_components;
std::uint32_t max_fragment_input_components;
std::uint32_t max_fragment_output_attachments;
std::uint32_t max_fragment_dual_src_attachments;
std::uint32_t max_fragment_combined_output_resources;
std::uint32_t max_compute_shared_memory_size;
std::array<std::uint32_t, 3> max_compute_work_group_count;
std::uint32_t max_compute_work_group_invocations;
std::array<std::uint32_t, 3> max_compute_work_group_size;
std::uint32_t sub_pixel_precision_bits;
std::uint32_t sub_texel_precision_bits;
std::uint32_t mipmap_precision_bits;
std::uint32_t max_draw_indexed_index_value;
std::uint32_t max_draw_indirect_count;
float max_sampler_lod_bias;
float max_sampler_anisotropy;
std::uint32_t max_viewports;
std::array<std::uint32_t, 2> max_viewport_dimensions;
std::array<float, 2> viewport_bounds_range;
std::uint32_t viewport_sub_pixel_bits;
std::size_t min_memory_map_alignment;
u32 max_image_dimension_1d;
u32 max_image_dimension_2d;
u32 max_image_dimension_3d;
u32 max_image_dimension_cube;
u32 max_image_array_layers;
u32 max_texel_buffer_elements;
u32 max_uniform_buffer_range;
u32 max_storage_buffer_range;
u32 max_push_constants_size;
u32 max_memory_allocation_count;
u32 max_sampler_allocation_count;
size_t buffer_image_granularity;
size_t sparse_address_space_size;
u32 max_bound_descriptor_sets;
u32 max_per_stage_descriptor_samplers;
u32 max_per_stage_descriptor_uniform_buffers;
u32 max_per_stage_descriptor_storage_buffers;
u32 max_per_stage_descriptor_sampled_images;
u32 max_per_stage_descriptor_storage_images;
u32 max_per_stage_descriptor_input_attachments;
u32 max_per_stage_resources;
u32 max_descriptor_set_samplers;
u32 max_descriptor_set_uniform_buffers;
u32 max_descriptor_set_uniform_buffers_dynamic;
u32 max_descriptor_set_storage_buffers;
u32 max_descriptor_set_storage_buffers_dynamic;
u32 max_descriptor_set_sampled_images;
u32 max_descriptor_set_storage_images;
u32 max_descriptor_set_input_attachments;
u32 max_vertex_input_attributes;
u32 max_vertex_input_bindings;
u32 max_vertex_input_attribute_offset;
u32 max_vertex_input_binding_stride;
u32 max_vertex_output_components;
u32 max_tessellation_generation_level;
u32 max_tessellation_patch_size;
u32 max_tessellation_control_per_vertex_input_components;
u32 max_tessellation_control_per_vertex_output_components;
u32 max_tessellation_control_per_patch_output_components;
u32 max_tessellation_control_total_output_components;
u32 max_tessellation_evaluation_input_components;
u32 max_tessellation_evaluation_output_components;
u32 max_geometry_shader_invocations;
u32 max_geometry_input_components;
u32 max_geometry_output_components;
u32 max_geometry_output_vertices;
u32 max_geometry_total_output_components;
u32 max_fragment_input_components;
u32 max_fragment_output_attachments;
u32 max_fragment_dual_src_attachments;
u32 max_fragment_combined_output_resources;
u32 max_compute_shared_memory_size;
std::array<u32, 3> max_compute_work_group_count;
u32 max_compute_work_group_invocations;
std::array<u32, 3> max_compute_work_group_size;
u32 sub_pixel_precision_bits;
u32 sub_texel_precision_bits;
u32 mipmap_precision_bits;
u32 max_draw_indexed_index_value;
u32 max_draw_indirect_count;
f32 max_sampler_lod_bias;
f32 max_sampler_anisotropy;
u32 max_viewports;
std::array<u32, 2> max_viewport_dimensions;
std::array<f32, 2> viewport_bounds_range;
u32 viewport_sub_pixel_bits;
size_t min_memory_map_alignment;
VkDeviceSize min_texel_buffer_offset_alignment;
VkDeviceSize min_uniform_buffer_offset_alignment;
VkDeviceSize min_storage_buffer_offset_alignment;
std::int32_t min_texel_offset;
std::uint32_t max_texel_offset;
std::int32_t min_texel_gather_offset;
std::uint32_t max_texel_gather_offset;
float min_interpolation_offset;
float max_interpolation_offset;
std::uint32_t sub_pixel_interpolation_offset_bits;
std::uint32_t max_framebuffer_width;
std::uint32_t max_framebuffer_height;
std::uint32_t max_framebuffer_layers;
i32 min_texel_offset;
u32 max_texel_offset;
i32 min_texel_gather_offset;
u32 max_texel_gather_offset;
f32 min_interpolation_offset;
f32 max_interpolation_offset;
u32 sub_pixel_interpolation_offset_bits;
u32 max_framebuffer_width;
u32 max_framebuffer_height;
u32 max_framebuffer_layers;
VkSampleCountFlags framebuffer_color_sample_counts;
VkSampleCountFlags framebuffer_depth_sample_counts;
VkSampleCountFlags framebuffer_stencil_sample_counts;
VkSampleCountFlags framebuffer_no_attachments_sample_counts;
std::uint32_t max_color_attachments;
u32 max_color_attachments;
VkSampleCountFlags sampled_image_color_sample_counts;
VkSampleCountFlags sampled_image_integer_sample_counts;
VkSampleCountFlags sampled_image_depth_sample_counts;
VkSampleCountFlags sampled_image_stencil_sample_counts;
VkSampleCountFlags storage_image_sample_counts;
std::uint32_t max_sample_mask_words;
u32 max_sample_mask_words;
Bool32 timestamp_compute_and_graphics;
float timestamp_period;
std::uint32_t max_clip_distances;
std::uint32_t max_cull_distances;
std::uint32_t max_combined_clip_and_cull_distances;
std::uint32_t discrete_queue_priorities;
std::array<float, 2> point_size_range;
std::array<float, 2> line_width_range;
float point_size_granularity;
float line_width_granularity;
f32 timestamp_period;
u32 max_clip_distances;
u32 max_cull_distances;
u32 max_combined_clip_and_cull_distances;
u32 discrete_queue_priorities;
std::array<f32, 2> point_size_range;
std::array<f32, 2> line_width_range;
f32 point_size_granularity;
f32 line_width_granularity;
Bool32 strict_lines;
Bool32 standard_sample_locations;
std::size_t optimal_buffer_copy_offset_alignment;
std::size_t optimal_buffer_copy_row_pitch_alignment;
std::size_t non_coherent_atom_size;
size_t optimal_buffer_copy_offset_alignment;
size_t optimal_buffer_copy_row_pitch_alignment;
size_t non_coherent_atom_size;
};
struct SparseProperties
@ -1093,13 +1092,13 @@ public:
struct Properties
{
std::uint32_t api_version;
std::uint32_t driver_version;
std::uint32_t vendor_id;
std::uint32_t device_id;
u32 api_version;
u32 driver_version;
u32 vendor_id;
u32 device_id;
Type device_type;
std::array<char, constants::max_physical_device_name> device_name;
std::array<std::uint8_t, constants::uuid_size> pipeline_cache_uuid;
std::array<u8, constants::uuid_size> pipeline_cache_uuid;
Limits limits;
SparseProperties sparse_properties;
};
@ -1107,12 +1106,12 @@ public:
struct MemoryType
{
MemoryPropertyFlags::T property_flags;
std::uint32_t heap_idx;
u32 heap_idx;
};
struct MemoryHeap
{
std::size_t size;
size_t size;
MemoryHeapFlags::T flags;
};
@ -1125,8 +1124,8 @@ public:
struct QueueFamilyProperties
{
QueueFlags::T queue_flags {};
std::uint32_t queue_count {};
std::uint32_t timestamp_valid_bits {};
u32 queue_count {};
u32 timestamp_valid_bits {};
math::uvec3 min_image_transfer_granularity;
};
@ -1163,7 +1162,7 @@ public:
[[nodiscard]] auto queue_family_supports_surface(
const Surface &surface,
std::uint32_t queue_family_idx
u32 queue_family_idx
) const -> bool;
[[nodiscard]] auto get_surface_capabilities(Surface &surface) const -> Surface::Capabilities;
@ -1201,7 +1200,7 @@ public:
struct CreateInfo
{
std::set<std::uint32_t> queue_indices;
std::set<u32> queue_indices;
std::vector<std::string> extensions;
@ -1258,7 +1257,7 @@ public:
void bind_memory(VkBuffer buffer, VkDeviceMemory memory, size_t offset = 0u) const;
[[nodiscard]] auto map_memory(VkDeviceMemory memory, size_t size, size_t offset) const
-> std::span<std::byte>;
-> std::span<byte>;
void unmap_memory(VkDeviceMemory memory);
@ -1498,14 +1497,14 @@ public:
struct MemoryRequirements
{
std::size_t size;
std::size_t alignment;
std::uint32_t memory_type_bits;
size_t size;
size_t alignment;
u32 memory_type_bits;
};
struct CreateInfo
{
std::size_t size;
size_t size;
UsageFlags usage;
@ -1640,10 +1639,10 @@ public:
struct Range
{
Image::AspectFlags aspect_flags;
std::uint32_t base_mip_level;
std::uint32_t level_count;
std::uint32_t base_array_layer;
std::uint32_t layer_count;
u32 base_mip_level;
u32 level_count;
u32 base_array_layer;
u32 layer_count;
};
static constexpr auto full_color_range = Range {
@ -1764,7 +1763,7 @@ public:
struct CreateInfo
{
std::vector<std::byte> code;
std::vector<byte> code;
std::string_view name;
};
@ -1874,9 +1873,9 @@ public:
Flags flags;
std::uint32_t idx;
u32 idx;
std::uint32_t count;
u32 count;
DescriptorSet::Type type;
@ -1945,14 +1944,14 @@ public:
{
DescriptorSet::Type type;
std::uint32_t count;
u32 count;
};
struct CreateInfo
{
std::vector<Size> sizes;
std::uint32_t max_sets;
u32 max_sets;
std::string_view name;
};
@ -2009,9 +2008,9 @@ public:
struct ViewportState
{
std::uint32_t viewport_count;
u32 viewport_count;
std::uint32_t scissor_count;
u32 scissor_count;
};
struct RasterizationState
@ -2028,13 +2027,13 @@ public:
bool depth_bias_enabled;
float depth_bias_constant_factor;
f32 depth_bias_constant_factor;
float depth_bias_clamp;
f32 depth_bias_clamp;
float depth_bias_slope_factor;
f32 depth_bias_slope_factor;
float line_width;
f32 line_width;
};
struct MultisamplingState
@ -2043,7 +2042,7 @@ public:
bool sample_shading_enabled;
float min_sample_shading;
f32 min_sample_shading;
bool alpha_to_coverage_enabled;
@ -2134,8 +2133,8 @@ public:
struct PushConstantRange
{
ShaderStageFlags::T shader_stages;
std::uint32_t offset;
std::uint32_t size;
u32 offset;
u32 size;
};
struct CreateInfo
@ -2189,11 +2188,11 @@ public:
Buffer *dst_buffer;
std::size_t src_offset;
size_t src_offset;
std::size_t dst_offset;
size_t dst_offset;
std::size_t size;
size_t size;
};
struct PushConstantsInfo
@ -2202,9 +2201,9 @@ public:
vk::ShaderStageFlags::T shader_stages;
std::uint32_t offset;
u32 offset;
std::uint32_t size;
u32 size;
void *data;
};
@ -2298,11 +2297,11 @@ public:
StoreOperation store_operation;
std::array<float, 4> color_clear_values;
std::array<f32, 4> color_clear_values;
float depth_clear_value;
f32 depth_clear_value;
std::uint32_t stencil_clear_value;
u32 stencil_clear_value;
Flags resolve_mode_flags;
};
@ -2316,13 +2315,13 @@ public:
struct DrawInfo
{
std::uint32_t vertex_count;
u32 vertex_count;
std::uint32_t instance_count;
u32 instance_count;
std::uint32_t first_vertex;
u32 first_vertex;
std::uint32_t first_instance;
u32 first_instance;
};
CommandBuffer() = default;
@ -2462,7 +2461,7 @@ public:
math::uvec2 extent;
std::uint32_t min_image_count;
u32 min_image_count;
std::vector<uint32_t> queue_family_indices;
@ -2491,8 +2490,7 @@ public:
[[nodiscard]] auto get_images() -> std::vector<Image>;
[[nodiscard]] auto acquire_image(Semaphore &semaphore, std::uint64_t timeout = 100'000'000)
-> std::uint32_t;
[[nodiscard]] auto acquire_image(Semaphore &semaphore, u64 timeout = 100'000'000) -> u32;
private:
[[nodiscard]] auto get_vk_handle() -> VkSwapchainKHR
@ -2590,9 +2588,9 @@ public:
struct AllocateInfo
{
std::size_t size;
size_t size;
std::uint32_t memory_type_idx;
u32 memory_type_idx;
std::string_view name;
};
@ -2609,7 +2607,7 @@ public:
auto operator=(const Memory &) -> Memory & = delete;
[[nodiscard]] auto map(std::size_t size, std::size_t offset) -> std::span<std::byte>;
[[nodiscard]] auto map(size_t size, size_t offset) -> std::span<byte>;
void unmap();
@ -2758,8 +2756,6 @@ constexpr auto to_string(VkResult result) noexcept -> std::string_view
default: return "<unknown>";
// clang-format on
}
std::unreachable();
}
void vkc(VkResult result)
@ -2933,24 +2929,21 @@ void load_library()
{
library = dlopen("libvulkan.so", runtime_loader_flags);
}
lt::debug::ensure(library, "Failed to dlopen the libvulkan.so");
ensure(library, "Failed to dlopen the libvulkan.so");
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
api::get_instance_proc_address = reinterpret_cast<PFN_vkGetInstanceProcAddr>(
dlsym(library, "vkGetInstanceProcAddr")
);
lt::debug::ensure(
api::get_instance_proc_address,
"Failed to load vulkan function: vkGetInstanceProcAddr"
);
ensure(api::get_instance_proc_address, "Failed to load vulkan function: vkGetInstanceProcAddr");
#elif defined(LIGHT_PLATFORM_WINDOWS)
auto library = LoadLibraryA("vulkan-1.dll");
lt::debug::ensure(library, "Failed to LoadLibraryA the vulkan-1.dll");
library = LoadLibraryA("vulkan-1.dll");
ensure(library, "Failed to LoadLibraryA the vulkan-1.dll");
api::get_instance_proc_address = std::bit_cast<PFN_vkGetInstanceProcAddr>(
GetProcAddress(library, "vkGetInstanceProcAddr")
GetProcAddress(std::bit_cast<HMODULE>(library), "vkGetInstanceProcAddr")
);
lt::debug::ensure(
ensure(
api::get_instance_proc_address,
"Failed to get vkGetInstanceProcAddr function pointer from vulkan-1.dll"
);
@ -2980,7 +2973,7 @@ void load_global_functions()
{
constexpr auto load_fn = []<typename T>(T &pfn, const char *fn_name) {
pfn = std::bit_cast<T>(api::get_instance_proc_address(nullptr, fn_name));
lt::debug::ensure(pfn, "Failed to load vulkan global function: {}", fn_name);
ensure(pfn, "Failed to load vulkan global function: {}", fn_name);
};
load_fn(api::create_instance, "vkCreateInstance");
@ -2992,7 +2985,7 @@ void Instance::load_functions()
{
const auto load_fn = [this]<typename T>(T &pfn, const char *fn_name) {
pfn = std::bit_cast<T>(api::get_instance_proc_address(m_instance, fn_name));
lt::debug::ensure(pfn, "Failed to load vulkan instance function: {}", fn_name);
ensure(pfn, "Failed to load vulkan instance function: {}", fn_name);
};
load_fn(api::destroy_instance, "vkDestroyInstance");
@ -3042,7 +3035,7 @@ void Device::load_functions()
{
const auto load_fn = [this]<typename T>(T &pfn, const char *fn_name) {
pfn = std::bit_cast<T>(api::get_device_proc_address(m_device, fn_name));
lt::debug::ensure(pfn, "Failed to load vulkan device function: {}", fn_name);
ensure(pfn, "Failed to load vulkan device function: {}", fn_name);
};
load_fn(api::get_device_queue, "vkGetDeviceQueue");
@ -3114,13 +3107,13 @@ Instance::Instance(CreateInfo info)
{
const auto layer_setting_type_visitor = overloads {
[](const std::vector<const char *> &) { return VK_LAYER_SETTING_TYPE_STRING_EXT; },
[](std::uint32_t) { return VK_LAYER_SETTING_TYPE_UINT32_EXT; },
[](u32) { return VK_LAYER_SETTING_TYPE_UINT32_EXT; },
[](bool) { return VK_LAYER_SETTING_TYPE_BOOL32_EXT; },
};
const auto layer_setting_value_visitor = overloads {
[](std::vector<const char *> values) { return std::bit_cast<void *>(values.data()); },
[](std::uint32_t value) { return std::bit_cast<void *>(&value); },
[](u32 value) { return std::bit_cast<void *>(&value); },
[](bool value) { return std::bit_cast<void *>(&value); },
};
@ -3152,7 +3145,7 @@ Instance::Instance(CreateInfo info)
values = std::bit_cast<const void *>(&std::get<2>(setting.values));
}
debug::ensure(values, "Failed to get variant from setting.values");
ensure(values, "Failed to get variant from setting.values");
layer_settings.emplace_back(
VkLayerSettingEXT {
@ -3192,7 +3185,7 @@ Instance::Instance(CreateInfo info)
};
vkc(api::create_instance(&vk_info, nullptr, &m_instance));
debug::ensure(m_instance, "Failed to create vulkan instance");
ensure(m_instance, "Failed to create vulkan instance");
}
Surface::Surface(const Instance &instance, const CreateInfo &info)
@ -3238,7 +3231,7 @@ Surface::~Surface()
{
auto count = 0u;
vkc(api::enumerate_physical_devices(instance.m_instance, &count, nullptr));
debug::ensure(count != 0u, "Failed to find any gpus with Vulkan support");
ensure(count != 0u, "Failed to find any gpus with Vulkan support");
auto vk_gpus = std::vector<VkPhysicalDevice>(count);
vkc(api::enumerate_physical_devices(instance.m_instance, &count, vk_gpus.data()));
@ -3403,7 +3396,7 @@ Surface::~Surface()
std::memcpy(
properties.pipeline_cache_uuid.data(),
static_cast<std::uint8_t *>(vk_properties.pipelineCacheUUID),
static_cast<u8 *>(vk_properties.pipelineCacheUUID),
constants::uuid_size
);
@ -3559,7 +3552,7 @@ Surface::~Surface()
[[nodiscard]] auto Gpu::get_queue_family_properties() const -> std::vector<QueueFamilyProperties>
{
auto count = std::uint32_t {};
auto count = u32 {};
api::get_physical_device_queue_family_properties(m_physical_device, &count, {});
@ -3591,7 +3584,7 @@ Surface::~Surface()
[[nodiscard]] auto Gpu::queue_family_supports_surface(
const Surface &surface,
std::uint32_t queue_family_idx
u32 queue_family_idx
) const -> bool
{
auto supported = VkBool32 { false };
@ -3883,7 +3876,7 @@ void Device::reset_fence(VkFence fence) const
void Device::reset_fences(std::span<VkFence> fences) const
{
vkc(api::reset_fences(m_device, fences.size(), fences.data()));
vkc(api::reset_fences(m_device, static_cast<uint32_t>(fences.size()), fences.data()));
}
auto Device::acquire_image(VkSwapchainKHR swapchain, VkSemaphore semaphore, uint64_t timeout)
@ -3917,7 +3910,7 @@ void Device::wait_for_fences(std::span<VkFence> fences) const
{
vkc(api::wait_for_fences(
m_device,
fences.size(),
static_cast<uint32_t>(fences.size()),
fences.data(),
true,
std::numeric_limits<uint64_t>::max()
@ -3929,7 +3922,7 @@ void Device::wait_for_fences(std::span<VkFence> fences) const
{
auto count = uint32_t { 0u };
vkc(api::get_swapchain_images_khr(m_device, swapchain, &count, nullptr));
debug::ensure(count != 0u, "Failed to get swapchain images");
ensure(count != 0u, "Failed to get swapchain images");
auto images = std::vector<VkImage>(count);
vkc(api::get_swapchain_images_khr(m_device, swapchain, &count, images.data()));
@ -3949,11 +3942,11 @@ void Device::bind_memory(VkBuffer buffer, VkDeviceMemory memory, size_t offset /
}
[[nodiscard]] auto Device::map_memory(VkDeviceMemory memory, size_t size, size_t offset) const
-> std::span<std::byte>
-> std::span<byte>
{
void *data = {};
vkc(api::map_memory(m_device, memory, offset, size, {}, &data));
return { std::bit_cast<std::byte *>(data), size };
return { std::bit_cast<byte *>(data), size };
}
void Device::unmap_memory(VkDeviceMemory memory)
@ -4274,6 +4267,9 @@ void Queue::present(PresentInfo info) const
Image::Image(Device &device, CreateInfo info): m_device(device.get_vk_handle()), m_image()
{
// WIP(Light): use image create info's info
ignore = info;
auto vk_info = VkImageCreateInfo {};
vkc(api::create_image(m_device, &vk_info, nullptr, &m_image));
}
@ -4337,6 +4333,9 @@ CommandBuffer::CommandBuffer(VkCommandBuffer buffer): m_buffer(buffer)
void CommandBuffer::begin(BeginInfo info /* = {} */)
{
// WIP(Light): Use info
ignore = info;
auto vk_info = VkCommandBufferBeginInfo {
.sType = VK_STRUCTURE_TYPE_COMMAND_BUFFER_BEGIN_INFO,
.pNext = {},
@ -4443,7 +4442,7 @@ void CommandBuffer::begin_rendering(RenderingInfo info)
.extent = {info.area_extent.x, info.area_extent.y},
},
.layerCount = 1u,
.colorAttachmentCount = static_cast<std::uint32_t>(vk_color_attachments.size()),
.colorAttachmentCount = static_cast<u32>(vk_color_attachments.size()),
.pColorAttachments= vk_color_attachments.data(),
};
@ -4581,7 +4580,7 @@ Swapchain::Swapchain(Device &device, Surface &surface, CreateInfo info)
.imageArrayLayers = 1u,
.imageUsage = VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT,
.imageSharingMode = VK_SHARING_MODE_EXCLUSIVE,
.queueFamilyIndexCount = static_cast<std::uint32_t>(info.queue_family_indices.size()),
.queueFamilyIndexCount = static_cast<u32>(info.queue_family_indices.size()),
.pQueueFamilyIndices = info.queue_family_indices.data(),
.preTransform = static_cast<VkSurfaceTransformFlagBitsKHR>(info.pre_transform),
.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR,
@ -4623,10 +4622,9 @@ Swapchain::~Swapchain()
return images;
}
[[nodiscard]] auto Swapchain::acquire_image(Semaphore &semaphore, std::uint64_t timeout)
-> std::uint32_t
[[nodiscard]] auto Swapchain::acquire_image(Semaphore &semaphore, u64 timeout) -> u32
{
auto idx = std::uint32_t {};
auto idx = u32 {};
vkc(api::acquire_next_image_khr(
m_device,
m_swapchain,
@ -4700,11 +4698,11 @@ Memory::~Memory()
api::free_memory(m_device, m_memory, nullptr);
}
[[nodiscard]] auto Memory::map(std::size_t size, std::size_t offset) -> std::span<std::byte>
[[nodiscard]] auto Memory::map(size_t size, size_t offset) -> std::span<byte>
{
void *data = {};
vkc(api::map_memory(m_device, m_memory, offset, size, {}, &data));
return { std::bit_cast<std::byte *>(data), size };
return { std::bit_cast<byte *>(data), size };
}
void Memory::unmap()
@ -4719,7 +4717,7 @@ ShaderModule::ShaderModule(Device &device, CreateInfo info): m_device(device.get
.pNext = {},
.flags = {},
.codeSize = info.code.size(),
.pCode = std::bit_cast<std::uint32_t *>(info.code.data()),
.pCode = std::bit_cast<u32 *>(info.code.data()),
};
vkc(api::create_shader_module(m_device, &vk_info, nullptr, &m_shader_module));
@ -4764,7 +4762,7 @@ DescriptorSetLayout::DescriptorSetLayout(Device &device, CreateInfo info)
auto vk_binding_flags_info = VkDescriptorSetLayoutBindingFlagsCreateInfoEXT {
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_BINDING_FLAGS_CREATE_INFO,
.pNext = {},
.bindingCount = static_cast<std::uint32_t>(vk_binding_flag_values.size()),
.bindingCount = static_cast<u32>(vk_binding_flag_values.size()),
.pBindingFlags = vk_binding_flag_values.data(),
};
@ -4772,7 +4770,7 @@ DescriptorSetLayout::DescriptorSetLayout(Device &device, CreateInfo info)
.sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_LAYOUT_CREATE_INFO,
.pNext = &vk_binding_flags_info,
.flags = info.flags,
.bindingCount = static_cast<std::uint32_t>(vk_bindings.size()),
.bindingCount = static_cast<u32>(vk_bindings.size()),
.pBindings = vk_bindings.data(),
};
vkc(api::create_descriptor_set_layout(m_device, &vk_info, nullptr, &m_descriptor_set_layout));
@ -4811,7 +4809,7 @@ DescriptorPool::DescriptorPool(Device &device, CreateInfo info): m_device(device
.pNext = {},
.flags = VK_DESCRIPTOR_POOL_CREATE_UPDATE_AFTER_BIND_BIT,
.maxSets = info.max_sets,
.poolSizeCount = static_cast<std::uint32_t>(vk_sizes.size()),
.poolSizeCount = static_cast<u32>(vk_sizes.size()),
.pPoolSizes = vk_sizes.data(),
};
@ -4937,11 +4935,12 @@ Pipeline::Pipeline(Device &device, PipelineLayout &layout, CreateInfo info)
auto color_attachment_formats = std::vector<vk::Format> {};
// WIP(Light): use color attachments
for (auto &color_attachment : info.attachment_state.color_attachments)
{
ignore = color_attachment;
}
auto rendering_info = VkPipelineRenderingCreateInfoKHR {
.sType = VK_STRUCTURE_TYPE_PIPELINE_RENDERING_CREATE_INFO,
.colorAttachmentCount = static_cast<uint32_t>(color_attachment_formats.size()),
@ -5018,9 +5017,9 @@ PipelineLayout::PipelineLayout(Device &device, CreateInfo info): m_device(device
.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO,
.pNext = {},
.flags = {},
.setLayoutCount = static_cast<std::uint32_t>(vk_set_layouts.size()),
.setLayoutCount = static_cast<u32>(vk_set_layouts.size()),
.pSetLayouts = vk_set_layouts.data(),
.pushConstantRangeCount = static_cast<std::uint32_t>(vk_push_constant_ranges.size()),
.pushConstantRangeCount = static_cast<u32>(vk_push_constant_ranges.size()),
.pPushConstantRanges = vk_push_constant_ranges.data()
};
vkc(api::create_pipeline_layout(m_device, &vk_info, nullptr, &m_pipeline_layout));

View file

@ -1,13 +1,14 @@
export module renderer.vk.buffer;
import preliminary;
import renderer.vk.device;
import renderer.vk.gpu;
import renderer.vk.api_wrapper;
import renderer.frontend;
import std;
namespace lt::renderer::vkb {
export namespace lt::renderer::vkb {
export class Buffer: public IBuffer
class Buffer: public IBuffer
{
public:
Buffer(class IDevice *device, class IGpu *gpu, const CreateInfo &info);
@ -16,7 +17,7 @@ public:
void unmap() override;
[[nodiscard]] auto get_size() const -> std::size_t override
[[nodiscard]] auto get_size() const -> size_t override
{
return m_size;
}
@ -34,12 +35,11 @@ private:
[[nodiscard]] auto to_native_memory_properties(Usage usage) const -> vk::Memory::PropertyFlags;
[[nodiscard]] auto has_correct_memory_type_bit(std::uint32_t type_bits, std::uint32_t type_idx)
const -> bool;
[[nodiscard]] auto has_correct_memory_type_bit(u32 type_bits, u32 type_idx) const -> bool;
[[nodiscard]] auto has_required_memory_properties(
std::uint32_t required_properties,
std::uint32_t property_flags
u32 required_properties,
u32 property_flags
) const -> bool;
Device *m_device {};
@ -51,14 +51,13 @@ private:
vk::Memory m_memory;
// TODO(Light): should this reflect the allocation size instead?
std::size_t m_size {};
size_t m_size {};
};
} // namespace lt::renderer::vkb
module :private;
using namespace ::lt::renderer;
using namespace ::lt::renderer::vkb;
namespace lt::renderer::vkb {
Buffer::Buffer(IDevice *device, IGpu *gpu, const CreateInfo &info)
: m_device(static_cast<Device *>(device))
@ -145,18 +144,17 @@ void Buffer::unmap() /* override */
std::unreachable();
}
[[nodiscard]] auto Buffer::has_correct_memory_type_bit(
std::uint32_t type_bits,
std::uint32_t type_idx
) const -> bool
[[nodiscard]] auto Buffer::has_correct_memory_type_bit(u32 type_bits, u32 type_idx) const -> bool
{
return type_bits & (1 << type_idx);
}
[[nodiscard]] auto Buffer::has_required_memory_properties(
std::uint32_t required_properties,
std::uint32_t property_flags
u32 required_properties,
u32 property_flags
) const -> bool
{
return (property_flags & required_properties) == required_properties;
}
} // namespace lt::renderer::vkb

View file

@ -1,15 +1,15 @@
export module renderer.vk.debugger;
import preliminary;
import renderer.vk.instance;
import renderer.frontend;
import renderer.vk.api_wrapper;
import memory.null_on_move;
import debug.assertions;
import logger;
import std;
namespace lt::renderer::vkb {
export namespace lt::renderer::vkb {
export class Debugger: public IDebugger
class Debugger: public IDebugger
{
public:
Debugger(IInstance *instance, CreateInfo info);
@ -155,7 +155,7 @@ void Debugger::native_callback(
{
try
{
debug::ensure(user_data, "Null vulkan_user_data received in messenger callback");
ensure(user_data, "Null vulkan_user_data received in messenger callback");
auto *messenger = std::bit_cast<Debugger *>(user_data);
messenger->m_user_callback(

View file

@ -1,9 +0,0 @@
#pragma once
namespace lt::renderer::vk {
class Descriptors
{
};
} // namespace lt::renderer::vk

View file

@ -1,17 +1,17 @@
export module renderer.vk.device;
import preliminary;
import memory.null_on_move;
import logger;
import debug.assertions;
import renderer.vk.instance;
import renderer.frontend;
import renderer.vk.api_wrapper;
import renderer.vk.gpu;
import renderer.vk.surface;
import std;
namespace lt::renderer::vkb {
export namespace lt::renderer::vkb {
export class Device: public IDevice
class Device: public IDevice
{
public:
Device(IGpu *gpu, ISurface *surface);
@ -21,7 +21,7 @@ public:
return m_device;
}
[[nodiscard]] auto get_family_indices() const -> std::vector<std::uint32_t>
[[nodiscard]] auto get_family_indices() const -> std::vector<u32>
{
return { m_graphics_queue_family_index, m_present_queue_family_index };
}
@ -43,7 +43,7 @@ private:
void initialize_queue_indices();
[[nodiscard]] auto find_suitable_queue_family() const -> std::uint32_t;
[[nodiscard]] auto find_suitable_queue_family() const -> u32;
vkb::Gpu *m_gpu {};
@ -55,9 +55,9 @@ private:
vk::Queue m_present_queue {};
std::uint32_t m_graphics_queue_family_index = vk::constants::queue_family_ignored;
u32 m_graphics_queue_family_index = vk::constants::queue_family_ignored;
std::uint32_t m_present_queue_family_index = vk::constants::queue_family_ignored;
u32 m_present_queue_family_index = vk::constants::queue_family_ignored;
};
} // namespace lt::renderer::vkb
@ -70,7 +70,7 @@ Device::Device(IGpu *gpu, ISurface *surface)
: m_gpu(static_cast<Gpu *>(gpu))
, m_surface(static_cast<Surface *>(surface))
{
debug::ensure(m_surface->vk(), "Failed to initialize vk::Device: null vulkan surface");
ensure(m_surface->vk(), "Failed to initialize vk::Device: null vulkan surface");
initialize_queue_indices();
initialize_logical_device();
@ -124,7 +124,7 @@ void Device::initialize_logical_device()
void Device::initialize_queue_indices()
{
auto properties = m_gpu->vk().get_queue_family_properties();
for (auto idx = std::uint32_t { 0u }; const auto &property : properties)
for (auto idx = u32 { 0u }; const auto &property : properties)
{
if (property.queue_flags & vk::QueueFlags::graphics_bit)
{
@ -145,12 +145,12 @@ void Device::initialize_queue_indices()
++idx;
}
debug::ensure(
ensure(
m_graphics_queue_family_index != vk::constants::queue_family_ignored,
"Failed to find graphics queue family"
);
debug::ensure(
ensure(
m_present_queue_family_index != vk::constants::queue_family_ignored,
"Failed to find presentation queue family"
);

View file

@ -1,52 +1,19 @@
export module renderer.vk.gpu;
import preliminary;
import renderer.vk.api_wrapper;
import logger;
import debug.assertions;
import renderer.frontend;
import renderer.vk.instance;
import memory.null_on_move;
import std;
namespace lt::renderer::vkb {
export namespace lt::renderer::vkb {
export class Gpu: public IGpu
class Gpu: public IGpu
{
public:
Gpu(IInstance *instance);
// [[nodiscard]] auto queue_family_supports_presentation(
// VkSurfaceKHR surface,
// uint32_t queue_family_idx
// ) -> bool;
//
// [[nodiscard]] auto get_surface_capabilities(VkSurfaceKHR surface) const
// -> VkSurfaceCapabilitiesKHR;
//
// [[nodiscard]] auto get_surface_formats(VkSurfaceKHR surface) const
// -> std::vector<VkSurfaceFormatKHR>;
//
// [[nodiscard]] auto get_properties() const -> VkPhysicalDeviceProperties
// {
// return m_properties;
// }
//
// [[nodiscard]] auto get_descriptor_indexing_features() const
// -> VkPhysicalDeviceDescriptorIndexingFeatures
// {
// return m_descriptor_indexing_features;
// }
//
// [[nodiscard]] auto get_memory_properties() const -> VkPhysicalDeviceMemoryProperties
// {
// return m_memory_properties;
// }
//
// [[nodiscard]] auto get_queue_family_properties() const ->
// std::vector<VkQueueFamilyProperties>
// {
// return m_queue_family_properties;
// }
[[nodiscard]] auto vk() -> vk::Gpu &;
private:
@ -54,8 +21,6 @@ private:
vk::Gpu::Properties m_properties {};
// VkPhysicalDeviceDescriptorIndexingFeatures m_descriptor_indexing_features;
vk::Gpu::MemoryProperties m_memory_properties {};
std::vector<vk::Gpu::QueueFamilyProperties> m_queue_family_properties;
@ -100,7 +65,7 @@ Gpu::Gpu(IInstance *instance)
}
// No suitable GPU is fonud...
debug::ensure(m_gpu, "Failed to find any suitable Vulkan physical device");
ensure(m_gpu, "Failed to find any suitable Vulkan physical device");
m_memory_properties = m_gpu.get_memory_properties();
m_queue_family_properties = m_gpu.get_queue_family_properties();
@ -111,42 +76,4 @@ Gpu::Gpu(IInstance *instance)
return m_gpu;
}
// [[nodiscard]] auto Gpu::queue_family_supports_presentation(
// VkSurfaceKHR surface,
// uint32_t queue_family_idx
// ) -> bool
// {
// auto supported = VkBool32 { false };
// vkc(vk_get_physical_device_surface_support(m_gpu, queue_family_idx, surface, &supported));
//
// return supported;
// }
//
// [[nodiscard]] auto Gpu::get_surface_capabilities(VkSurfaceKHR surface) const
// -> VkSurfaceCapabilitiesKHR
// {
// auto capabilities = VkSurfaceCapabilitiesKHR {};
// vkc(vk_get_physical_device_surface_capabilities(m_gpu, surface, &capabilities));
// return capabilities;
// }
//
// [[nodiscard]] auto Gpu::get_surface_formats(VkSurfaceKHR surface) const
// -> std::vector<VkSurfaceFormatKHR>
// {
// auto count = uint32_t { 0u };
// vkc(vk_get_physical_device_surface_formats(m_gpu, surface, &count, nullptr));
//
// auto formats = std::vector<VkSurfaceFormatKHR>(count);
// vkc(vk_get_physical_device_surface_formats(m_gpu, surface, &count, formats.data()));
//
// return formats;
// }
// [[nodiscard]] auto Gpu::create_device(VkDeviceCreateInfo info) const -> VkDevice
// {
// auto *device = VkDevice {};
// vkc(vk_create_device(m_gpu, &info, nullptr, &device));
// return device;
// }
} // namespace lt::renderer::vkb

View file

@ -1,10 +1,10 @@
export module renderer.vk.instance;
import debug.assertions;
import preliminary;
import renderer.frontend;
import renderer.vk.api_wrapper;
import std;
namespace lt::renderer::vkb {
export namespace lt::renderer::vkb {
/**
* Responsible for dynamically loading Vulkan library/functions.
@ -15,7 +15,7 @@ namespace lt::renderer::vkb {
* https://www.xfree86.org/4.7.0/DRI11.html
* https://github.com/KhronosGroup/Vulkan-LoaderAndValidationLayers/issues/1894
*/
export class Instance: public IInstance
class Instance: public IInstance
{
public:
static auto get() -> IInstance *
@ -28,20 +28,6 @@ public:
return m_instance;
}
// /* create functions */
// [[nodiscard]] auto create_xlib_surface(VkXlibSurfaceCreateInfoKHR info) const ->
// VkSurfaceKHR;
//
// [[nodiscard]] auto create_messenger(VkDebugUtilsMessengerCreateInfoEXT info) const
// -> VkDebugUtilsMessengerEXT;
//
// /* destroy functions */
// void destroy_surface(VkSurfaceKHR surface) const;
//
// void destroy_messenger(VkDebugUtilsMessengerEXT messenger) const;
//
// [[nodiscard]] auto enumerate_gpus() const -> std::vector<VkPhysicalDevice>;
private:
static auto instance() -> IInstance &
{
@ -81,7 +67,7 @@ Instance::Instance()
Setting { .name = "enable_message_limit", .values = true },
Setting {
.name = "duplicate_message_limit",
.values = std::numeric_limits<std::uint32_t>::max(),
.values = std::numeric_limits<u32>::max(),
},
Setting {
.name = "report_flags",
@ -89,7 +75,6 @@ Instance::Instance()
},
};
using Layer = vk::Instance::Layer;
m_instance = vk::Instance(
vk::Instance::CreateInfo {
@ -110,41 +95,4 @@ Instance::Instance()
m_instance.load_functions();
}
// auto Instance::enumerate_gpus() const -> std::vector<VkPhysicalDevice>
// {
// auto count = 0u;
// vkc(vk_enumerate_physical_devices(m_instance, &count, nullptr));
// debug::ensure(count != 0u, "Failed to find any gpus with Vulkan support");
//
// auto gpus = std::vector<VkPhysicalDevice>(count);
// vkc(vk_enumerate_physical_devices(m_instance, &count, gpus.data()));
// return gpus;
// }
//
// auto Instance::create_xlib_surface(VkXlibSurfaceCreateInfoKHR info) const -> VkSurfaceKHR
// {
// auto *value = VkSurfaceKHR {};
// vk_create_xlib_surface_khr(m_instance, &info, m_allocator, &value);
//
// return value;
// }
//
// [[nodiscard]] auto Instance::create_messenger(VkDebugUtilsMessengerCreateInfoEXT info) const
// -> VkDebugUtilsMessengerEXT
// {
// auto *messenger = VkDebugUtilsMessengerEXT {};
// vkc(vk_create_debug_messenger(m_instance, &info, m_allocator, &messenger));
// return messenger;
// }
//
// void Instance::destroy_surface(VkSurfaceKHR surface) const
// {
// vk_destroy_surface_khr(m_instance, surface, m_allocator);
// }
//
// void Instance::destroy_messenger(VkDebugUtilsMessengerEXT messenger) const
// {
// vk_destroy_debug_messenger(m_instance, messenger, m_allocator);
// }
} // namespace lt::renderer::vkb

View file

@ -1,4 +1,6 @@
export module renderer.vk.pass;
import preliminary;
import renderer.data;
import renderer.vk.api_wrapper;
import renderer.vk.device;
@ -7,11 +9,10 @@ import assets.shader;
import assets.metadata;
import memory.null_on_move;
import renderer.frontend;
import std;
namespace lt::renderer::vkb {
export namespace lt::renderer::vkb {
export class Pass: public IPass
class Pass: public IPass
{
public:
Pass(
@ -51,10 +52,8 @@ private:
} // namespace lt::renderer::vkb
module :private;
using namespace ::lt::renderer::vkb;
using namespace ::lt::renderer;
namespace lt::renderer::vkb {
using enum vk::DescriptorSetLayout::Binding::FlagBits;
@ -80,36 +79,6 @@ Pass::Pass(
.push_constant_ranges = { { vk::ShaderStageFlags::vertex_bit, 0u, sizeof(FrameConstants) } }
}))
{
// auto pool_size = VkDescriptorPoolSize {
// .type = VK_DESCRIPTOR_TYPE_STORAGE_BUFFER,
// .descriptorCount = descriptor_count,
// };
//
// m_descriptor_pool = m_device->create_desscriptor_pool(
// {
// .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO,
// .poolSizeCount = 1u,
// .pPoolSizes = &pool_size,
// }
// );
//
// auto descriptor_set_variable_descriptor_count_info
// = VkDescriptorSetVariableDescriptorCountAllocateInfo {
// .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_VARIABLE_DESCRIPTOR_COUNT_ALLOCATE_INFO,
// .descriptorSetCount = 1u,
// .pDescriptorCounts = &descriptor_count,
// };
//
// m_vertices_descriptor_set = m_device->allocate_descriptor_set(
// VkDescriptorSetAllocateInfo {
// .sType = VK_STRUCTURE_TYPE_DESCRIPTOR_SET_ALLOCATE_INFO,
// .pNext = &descriptor_set_variable_descriptor_count_info,
// .descriptorPool = m_descriptor_pool,
// .descriptorSetCount = 1u,
// .pSetLayouts = &m_vertices_descriptor_set_layout,
// }
// );
auto shaders = std::vector<std::pair<vk::ShaderModule, vk::ShaderStageFlags::T>> {};
shaders.emplace_back(
vk::ShaderModule(
@ -152,3 +121,5 @@ Pass::Pass(
}
);
}
} // namespace lt::renderer::vkb

View file

@ -1,7 +1,8 @@
export module renderer.vk.renderer;
import preliminary;
import logger;
import assets.shader;
import debug.assertions;
import renderer.vk.api_wrapper;
import memory.reference;
import memory.null_on_move;
@ -14,19 +15,17 @@ import renderer.vk.buffer;
import renderer.vk.pass;
import renderer.data;
import renderer.frontend;
import std;
namespace lt::renderer::vkb {
export namespace lt::renderer::vkb {
// NOLINTNEXTLINE
export class Renderer: public IRenderer
class Renderer: public IRenderer
{
public:
Renderer(
class IGpu *gpu,
class IDevice *device,
class ISwapchain *swapchain,
std::uint32_t max_frames_in_flight
u32 max_frames_in_flight
);
~Renderer() override
@ -35,14 +34,14 @@ public:
{
m_device->vk().wait_idle();
}
catch (std::exception &exp)
catch (const std::exception &exp)
{
log::error("Failed to wait idle on device in renderer destructor");
log::error("Failed to wait idle on device in renderer destructor:");
log::error("\twhat: {}", exp.what());
}
}
[[nodiscard]] auto frame(std::uint32_t frame_idx, std::function<void()> submit_scene)
-> Result override;
[[nodiscard]] auto frame(u32 frame_idx, std::function<void()> submit_scene) -> Result override;
void replace_swapchain(ISwapchain *swapchain) override;
@ -57,11 +56,11 @@ public:
) override;
private:
void record_cmd(vk::CommandBuffer &cmd, std::uint32_t image_idx);
void record_cmd(vk::CommandBuffer &cmd, u32 image_idx);
void map_buffers(std::uint32_t frame_idx);
void map_buffers(u32 frame_idx);
std::uint32_t m_max_frames_in_flight {};
u32 m_max_frames_in_flight {};
Device *m_device {};
@ -87,13 +86,13 @@ private:
Buffer m_staging_buffer;
std::size_t m_staging_offset;
size_t m_staging_offset;
std::span<std::byte> m_staging_map;
std::span<byte> m_staging_map;
std::span<components::Sprite::Vertex> m_sprite_vertex_map;
std::size_t m_current_sprite_idx;
size_t m_current_sprite_idx;
vk::DescriptorPool m_global_set_pool;
@ -105,12 +104,7 @@ private:
module :private;
namespace lt::renderer::vkb {
Renderer::Renderer(
IGpu *gpu,
IDevice *device,
ISwapchain *swapchain,
std::uint32_t max_frames_in_flight
)
Renderer::Renderer(IGpu *gpu, IDevice *device, ISwapchain *swapchain, u32 max_frames_in_flight)
: m_device(static_cast<Device *>(device))
, m_swapchain(static_cast<Swapchain *>(swapchain))
, m_resolution(m_swapchain->get_resolution())
@ -174,10 +168,9 @@ Renderer::Renderer(
}
};
[[nodiscard]] auto Renderer::frame(std::uint32_t frame_idx, std::function<void()> submit_scene)
-> Result
[[nodiscard]] auto Renderer::frame(u32 frame_idx, std::function<void()> submit_scene) -> Result
{
debug::ensure(
ensure(
frame_idx < m_max_frames_in_flight,
"Failed to draw: frame_idx >= max_frames_in_flight ({} >= {})",
frame_idx,
@ -193,6 +186,9 @@ Renderer::Renderer(
frame_fence.reset();
map_buffers(frame_idx);
// WIP(Light): submit the scene!
ignore = submit_scene;
// submit_scene();
record_cmd(cmd, image_idx);
@ -225,7 +221,7 @@ void Renderer::replace_swapchain(ISwapchain *swapchain)
m_resolution = m_swapchain->get_resolution();
}
void Renderer::map_buffers(std::uint32_t frame_idx)
void Renderer::map_buffers(u32 frame_idx)
{
using components::Sprite;
@ -242,7 +238,7 @@ void Renderer::map_buffers(std::uint32_t frame_idx)
);
}
void Renderer::record_cmd(vk::CommandBuffer &cmd, std::uint32_t image_idx)
void Renderer::record_cmd(vk::CommandBuffer &cmd, u32 image_idx)
{
m_staging_map = {};
m_sprite_vertex_map = {};
@ -314,7 +310,7 @@ void Renderer::record_cmd(vk::CommandBuffer &cmd, std::uint32_t image_idx)
// cmd.set_viewport(
// {
// .origin = {},
// .extent = { static_cast<float>(m_resolution.x), static_cast<float>(m_resolution.y) },
// .extent = { static_cast<f32>(m_resolution.x), static_cast<f32>(m_resolution.y) },
// .min_depth = 0.0f,
// .max_depth = 1.0f,
// }
@ -322,7 +318,7 @@ void Renderer::record_cmd(vk::CommandBuffer &cmd, std::uint32_t image_idx)
// cmd.set_scissor({ .offset = {}, .extent = m_resolution });
// cmd.draw(
// {
// .vertex_count = static_cast<std::uint32_t>(m_current_sprite_idx),
// .vertex_count = static_cast<u32>(m_current_sprite_idx),
// .instance_count = 1u,
// .first_vertex = 0u,
// .first_instance = 0u,

View file

@ -1,5 +1,6 @@
export module renderer.vk.surface;
import debug.assertions;
import preliminary;
import ecs.entity;
import ecs.registry;
import memory.null_on_move;
@ -9,9 +10,9 @@ import renderer.frontend;
import renderer.vk.instance;
import renderer.vk.api_wrapper;
namespace lt::renderer::vkb {
export namespace lt::renderer::vkb {
export class Surface: public ISurface
class Surface: public ISurface
{
public:
Surface(IInstance *instance, const ecs::Entity &surface_entity);
@ -41,11 +42,11 @@ Surface::Surface(IInstance *instance, const ecs::Entity &surface_entity)
#if defined(LIGHT_PLATFORM_LINUX)
debug::ensure(
ensure(
component.get_native_data().display,
"Failed to initialize vk::Surface: null Wayland display"
);
debug::ensure(
ensure(
component.get_native_data().surface,
"Failed to initialize vk::Surface: null Wayland surface"
);
@ -59,7 +60,7 @@ Surface::Surface(IInstance *instance, const ecs::Entity &surface_entity)
);
#elif defined(LIGHT_PLATFORM_WINDOWS)
debug::ensure(
ensure(
component.get_native_data().window,
"Failed to initialize vk::Surface: null win32 window handle"
);

View file

@ -1,4 +1,6 @@
export module renderer.vk.swapchain;
import preliminary;
import renderer.vk.api_wrapper;
import renderer.vk.surface;
import renderer.vk.device;
@ -8,11 +10,10 @@ import renderer.frontend;
import math.vec2;
import memory.null_on_move;
import logger;
import std;
namespace lt::renderer::vkb {
export namespace lt::renderer::vkb {
export class Swapchain: public ISwapchain
class Swapchain: public ISwapchain
{
public:
Swapchain(ISurface *surface, IGpu *gpu, IDevice *device);
@ -32,17 +33,17 @@ public:
return m_format;
}
[[nodiscard]] auto get_image_count() const -> std::size_t
[[nodiscard]] auto get_image_count() const -> size_t
{
return m_images.size();
}
[[nodiscard]] auto get_image_view(std::uint32_t idx) -> vk::ImageView &
[[nodiscard]] auto get_image_view(u32 idx) -> vk::ImageView &
{
return m_image_views[idx];
}
[[nodiscard]] auto get_image(std::uint32_t idx) -> vk::Image &
[[nodiscard]] auto get_image(u32 idx) -> vk::Image &
{
return m_images[idx];
}
@ -50,8 +51,8 @@ public:
private:
[[nodiscard]] auto get_optimal_image_count(
vk::Surface::Capabilities capabilities,
std::uint32_t desired_image_count
) const -> std::uint32_t;
u32 desired_image_count
) const -> u32;
Gpu *m_gpu;
@ -72,7 +73,6 @@ private:
} // namespace lt::renderer::vkb
module :private;
namespace lt::renderer::vkb {
@ -81,17 +81,15 @@ Swapchain::Swapchain(ISurface *surface, IGpu *gpu, IDevice *device)
, m_gpu(static_cast<Gpu *>(gpu))
, m_device(static_cast<Device *>(device))
{
static auto idx = 0u;
auto capabilities = m_gpu->vk().get_surface_capabilities(m_surface->vk());
const auto formats = m_gpu->vk().get_surface_formats(m_surface->vk());
// TODO(Light): parameterize
constexpr auto desired_image_count = std::uint32_t { 3u };
constexpr auto desired_image_count = u32 { 3u };
const auto surface_format = formats.front();
m_format = surface_format.format;
if (capabilities.current_extent.x == std::numeric_limits<std::uint32_t>::max())
if (capabilities.current_extent.x == std::numeric_limits<u32>::max())
{
log::info(
"Vulkan surface capabilities current extent is uint32 max... This indicates that the "
@ -104,6 +102,7 @@ Swapchain::Swapchain(ISurface *surface, IGpu *gpu, IDevice *device)
capabilities.current_extent.y = 600u;
}
static auto swapchain_idx = 0u;
m_swapchain = vk::Swapchain(
m_device->vk(),
m_surface->vk(),
@ -116,7 +115,7 @@ Swapchain::Swapchain(ISurface *surface, IGpu *gpu, IDevice *device)
.queue_family_indices = m_device->get_family_indices(),
.present_mode = vk::Swapchain::PresentMode::mailbox,
.pre_transform = capabilities.current_transform,
.name = std::format("swapchain {}", idx++),
.name = std::format("swapchain {}", swapchain_idx++),
}
);
m_resolution = capabilities.current_extent;
@ -156,8 +155,8 @@ Swapchain::Swapchain(ISurface *surface, IGpu *gpu, IDevice *device)
[[nodiscard]] auto Swapchain::get_optimal_image_count(
vk::Surface::Capabilities capabilities,
std::uint32_t desired_image_count
) const -> std::uint32_t
u32 desired_image_count
) const -> u32
{
const auto min_image_count = capabilities.min_image_count;
const auto max_image_count = capabilities.max_image_count;

View file

@ -1,3 +1,3 @@
add_executable(sandbox sandbox.cpp)
target_link_libraries(sandbox PRIVATE logger bitwise env memory time test lt_debug math assets app ecs surface renderer input mirror)
target_link_libraries(sandbox PRIVATE preliminary logger bitwise memory time test math assets app ecs surface renderer input mirror)

View file

@ -1,4 +1,4 @@
import test.test;
import preliminary;
import time;
import test.expects;
import surface.system;
@ -10,7 +10,6 @@ import memory.reference;
import logger;
import math.vec2;
import app.system;
import std;
constexpr auto title = "TestWindow";
constexpr auto width = 800u;
@ -18,7 +17,8 @@ constexpr auto height = 600u;
constexpr auto vsync = true;
constexpr auto visible = false;
int main()
auto main() -> i32
try
{
auto registry = lt::memory::create_ref<lt::ecs::Registry>();
auto system = lt::surface::System { registry };
@ -37,9 +37,18 @@ int main()
auto timer = lt::time::Timer {};
lt::log::trace("Ticking for 3 seconds...");
while (timer.elapsed_time() < std::chrono::seconds { 3 })
while (timer.elapsed_time() < std::chrono::seconds { 30 })
{
system.tick({});
}
lt::log::trace("Three seconds passed, quitting...");
}
catch (const std::exception &exp)
{
lt::log::critical("Aborting due to uncaught std::exception:");
lt::log::critical("\twhat: {}", exp.what());
}
catch (...)
{
lt::log::critical("Aborting due to uncaught non std::exception!");
}

View file

@ -7,18 +7,19 @@ struct wl_surface;
#endif
export module surface.system:components;
import std;
import preliminary;
import math.vec2;
import surface.events;
import surface.requests;
namespace lt::surface {
export namespace lt::surface {
/** Represents a platform's surface (eg. a Window).
*
* @note This is a "system component"
*/
export class SurfaceComponent
class SurfaceComponent
{
public:
friend class System;
@ -59,6 +60,8 @@ public:
static constexpr auto max_title_length = 256;
// TODO(Light): add `center_to_screen` flag
// TODO(Light): add `screen_mode` flag (windowed/full_screen/windowed_full_screen)
struct CreateInfo
{
std::string_view title;

View file

@ -1,7 +1,8 @@
export module surface.events;
import preliminary;
import input.codes;
import math.vec2;
import std;
export namespace lt::surface {
@ -92,7 +93,7 @@ private:
class MouseMovedEvent
{
public:
MouseMovedEvent(float x, float y): m_position(x, y)
MouseMovedEvent(f32 x, f32 y): m_position(x, y)
{
}
@ -101,12 +102,12 @@ public:
return m_position;
}
[[nodiscard]] auto get_x() const -> float
[[nodiscard]] auto get_x() const -> f32
{
return m_position.x;
}
[[nodiscard]] auto get_y() const -> float
[[nodiscard]] auto get_y() const -> f32
{
return m_position.y;
}
@ -123,11 +124,11 @@ private:
class WheelScrolledEvent
{
public:
WheelScrolledEvent(float offset): m_offset(offset)
WheelScrolledEvent(f32 offset): m_offset(offset)
{
}
[[nodiscard]] auto get_offset() const -> float
[[nodiscard]] auto get_offset() const -> f32
{
return m_offset;
}
@ -140,7 +141,7 @@ public:
}
private:
float m_offset;
f32 m_offset;
};
class ButtonPressedEvent
@ -197,7 +198,7 @@ public:
class MovedEvent
{
public:
MovedEvent(std::int32_t x, std::int32_t y): m_position(x, y)
MovedEvent(i32 x, i32 y): m_position(x, y)
{
}
@ -218,7 +219,7 @@ private:
class ResizedEvent
{
public:
ResizedEvent(std::uint32_t width, std::uint32_t height): m_size(width, height)
ResizedEvent(u32 width, u32 height): m_size(width, height)
{
}

View file

@ -1,6 +1,7 @@
export module surface.requests;
import preliminary;
import math.vec2;
import std;
export namespace lt::surface {

File diff suppressed because it is too large Load diff

View file

@ -1,155 +0,0 @@
#include <ecs/entity.hpp>
#include <ecs/registry.hpp>
#include <memory/reference.hpp>
#include <surface/components.hpp>
#include <surface/system.hpp>
#include <test/fuzz.hpp>
#include <test/test.hpp>
namespace lt::surface {
enum class FuzzAction : uint8_t
{
create_entity,
create_surface_component,
destroy_surface_component,
push_request,
push_event,
tick_system,
count,
};
enum class EventType : uint8_t
{
Closed,
Moved,
Resized,
LostFocus,
GainFocus,
};
void create_surface_component(test::FuzzDataProvider &provider, ecs::Registry &registry)
{
const auto length = std::min(provider.consume<uint32_t>().value_or(16), 255u);
const auto title = provider.consume_string(length).value_or("");
const auto resolution = math::uvec2 {
provider.consume<uint32_t>().value_or(32u),
provider.consume<uint32_t>().value_or(64u),
};
const auto visible = provider.consume<bool>().value_or(false);
const auto vsync = provider.consume<bool>().value_or(false);
try
{
auto entity = registry.create_entity();
registry.add<surface::SurfaceComponent>(
entity,
surface::SurfaceComponent::CreateInfo {
.title = std::move(title),
.resolution = resolution,
.vsync = vsync,
.visible = visible,
}
);
}
catch (const std::exception &exp)
{
std::ignore = exp;
}
}
void remove_surface_component(ecs::Registry &registry)
{
const auto view = registry.view<SurfaceComponent>();
if (!view.is_empty())
{
registry.remove<SurfaceComponent>(view[0].first);
}
}
void push_request(ecs::Registry &registry)
{
}
void push_event(ecs::Registry &registry)
{
}
void check_invariants()
{
}
test::FuzzHarness harness = [](const uint8_t *data, size_t size) {
auto provider = test::FuzzDataProvider { data, size };
auto registry = memory::create_ref<ecs::Registry>();
auto system = surface::System { registry };
while (auto action = provider.consume<uint8_t>())
{
if (*action > std::to_underlying(FuzzAction::count))
{
*action = *action % std::to_underlying(FuzzAction::count);
}
switch (static_cast<FuzzAction>(action.value()))
{
case FuzzAction::create_entity:
{
const auto length = std::min(provider.consume<uint32_t>().value_or(16), 255u);
registry->create_entity();
break;
}
case FuzzAction::create_surface_component:
{
create_surface_component(provider, *registry);
break;
}
case FuzzAction::destroy_surface_component:
{
remove_surface_component(*registry);
break;
}
case FuzzAction::push_event:
{
auto view = registry->view<SurfaceComponent>();
if (!view.is_empty())
{
for (auto &[entity, component] : view)
{
provider.consume<uint8_t>().value_or(0);
// @TODO(Light): push some event
}
}
break;
}
case FuzzAction::push_request:
{
break;
}
case FuzzAction::count:
case FuzzAction::tick_system:
{
system.tick();
break;
}
}
check_invariants();
}
return 0;
};
} // namespace lt::surface

View file

@ -1,27 +1,16 @@
import test.test;
import test;
import time;
import test.expects;
import surface.system;
import surface.events;
import surface.requests;
import ecs.registry;
import memory.scope;
import memory.reference;
import logger;
import math.vec2;
import app.system;
import std;
using ::lt::surface::SurfaceComponent;
using ::lt::surface::System;
using ::lt::test::Case;
using ::lt::test::expect_eq;
using ::lt::test::expect_ne;
using ::lt::test::expect_not_nullptr;
using ::lt::test::expect_throw;
using ::lt::test::Suite;
using ::std::ignore;
using ::lt::test::operator""_suite;
[[nodiscard]] auto tick_info() -> lt::app::TickInfo
{
@ -88,206 +77,187 @@ private:
};
Suite raii = "raii"_suite = [] {
Case { "happy path won't throw" } = [] {
Case { "happy paths" } = [] {
auto fixture = Fixture {};
auto system = System { fixture.registry() };
};
Case { "unhappy paths" } = [] {
expect_throw([] { ignore = System { {} }; });
};
Case { "many" } = [] {
auto fixture = Fixture {};
for (auto idx : std::views::iota(0, 250))
{
ignore = idx;
ignore = System { fixture.registry() };
}
};
Case { "post construct has correct state" } = [] {
auto fixture = Fixture {};
auto system = System { fixture.registry() };
expect_eq(fixture.registry()->view<SurfaceComponent>().get_size(), 0);
};
Case { "post destruct has correct state" } = [] {
auto fixture = Fixture {};
auto system = lt::memory::create_scope<System>(fixture.registry());
fixture.create_component();
expect_eq(fixture.registry()->view<SurfaceComponent>().get_size(), 1);
system.reset();
expect_eq(fixture.registry()->view<SurfaceComponent>().get_size(), 0);
};
};
Suite system_events = "system_events"_suite = [] {
Case { "on_register won't throw" } = [] {
auto fixture = Fixture {};
auto system = System { fixture.registry() };
auto timer = lt::time::Timer {};
lt::log::trace("Ticking for 3 seconds...");
while (timer.elapsed_time() < std::chrono::seconds { 3 })
{
system.tick({});
}
lt::log::trace("Three seconds passed, quitting...");
system.on_register();
expect_eq(fixture.registry()->view<SurfaceComponent>().get_size(), 0);
};
Case { "on_unregister won't throw" } = [] {
auto fixture = Fixture {};
auto system = System { fixture.registry() };
// Case { "many won't freeze/throw" } = [] {
// auto fixture = Fixture {};
// for (auto idx : std::views::iota(0, 250))
// {
// ignore = System { fixture.registry() };
// }
// };
//
// Case { "unhappy path throws" } = [] {
// expect_throw([] { ignore = System { {} }; });
// };
//
// Case { "post construct has correct state" } = [] {
// auto fixture = Fixture {};
// auto system = System { fixture.registry() };
// expect_eq(fixture.registry()->view<SurfaceComponent>().get_size(), 0);
// };
//
// Case { "post destruct has correct state" } = [] {
// auto fixture = Fixture {};
// auto system = lt::memory::create_scope<System>(fixture.registry());
//
// fixture.create_component();
// expect_eq(fixture.registry()->view<SurfaceComponent>().get_size(), 1);
//
// system.reset();
// expect_eq(fixture.registry()->view<SurfaceComponent>().get_size(), 0);
// };
system.on_register();
system.on_unregister();
expect_eq(fixture.registry()->view<SurfaceComponent>().get_size(), 0);
};
};
// Suite system_events = "system_events"_suite = [] {
// Case { "on_register won't throw" } = [] {
// auto fixture = Fixture {};
// auto system = System { fixture.registry() };
//
// system.on_register();
// expect_eq(fixture.registry()->view<SurfaceComponent>().get_size(), 0);
// };
//
// Case { "on_unregister won't throw" } = [] {
// auto fixture = Fixture {};
// auto system = System { fixture.registry() };
//
// system.on_register();
// system.on_unregister();
// expect_eq(fixture.registry()->view<SurfaceComponent>().get_size(), 0);
// };
// };
//
// Suite registry_events = "registry_events"_suite = [] {
// Case { "on_construct<SurfaceComponent> initializes component" } = [] {
// auto fixture = Fixture {};
//
// const auto &component = fixture.create_component();
// expect_eq(fixture.registry()->view<SurfaceComponent>().get_size(), 1);
// fixture.check_values(*component);
// };
//
// Case { "unhappy on_construct<SurfaceComponent> throws" } = [] {
// auto fixture = Fixture {};
// auto system = System { fixture.registry() };
//
// expect_throw([&] { fixture.create_component({ .resolution = { width, 0 } }); });
//
// expect_throw([&] { fixture.create_component({ .resolution = { 0, height } }); });
//
// expect_throw([&] {
// fixture.create_component(
// { .title = "", .resolution = { SurfaceComponent::max_dimension + 1, height } }
// );
// });
//
// expect_throw([&] {
// fixture.create_component(
// { .title = "", .resolution = { width, SurfaceComponent::max_dimension + 1 } }
// );
// });
//
// auto big_str = std::string {};
// big_str.resize(SurfaceComponent::max_title_length + 1);
// expect_throw([&] {
// fixture.create_component({ .title = big_str, .resolution = { width, height } });
// });
// };
//
// Case { "unhappy on_construct<SurfaceComponent> removes component" } = [] {
// auto fixture = Fixture {};
// auto system = System { fixture.registry() };
//
// expect_throw([&] { fixture.create_component({ .resolution = { width, 0 } }); });
// expect_eq(fixture.registry()->view<SurfaceComponent>().get_size(), 0);
// };
//
// Case { "on_destrroy<SurfaceComponent> cleans up component" } = [] {
// auto fixture = Fixture {};
// auto system = lt::memory::create_scope<System>(fixture.registry());
//
// const auto &component = fixture.create_component();
// expect_eq(fixture.registry()->view<SurfaceComponent>().get_size(), 1);
// fixture.check_values(*component);
//
// system.reset();
// expect_eq(fixture.registry()->view<SurfaceComponent>().get_size(), 0);
// };
// };
//
// Suite tick = "tick"_suite = [] {
// Case { "ticking on empty registry won't throw" } = [] {
// auto fixture = Fixture {};
// System { fixture.registry() }.tick(tick_info());
// };
//
// Case { "ticking on non-empty registry won't throw" } = [] {
// auto fixture = Fixture {};
// auto system = System { fixture.registry() };
//
// fixture.create_component();
// system.tick(tick_info());
// };
// };
//
// Suite tick_handles_events = "tick_handles_events"_suite = [] {
// Case { "ticking clears previous tick's events" } = [] {
// auto fixture = Fixture {};
// auto system = System { fixture.registry() };
// auto &surface = **fixture.create_component();
//
// // flush window-creation events
// system.tick(tick_info());
// expect_eq(surface.peek_events().size(), 0);
//
// surface.push_event(lt::surface::MovedEvent({}, {}));
// expect_eq(surface.peek_events().size(), 1);
//
// surface.push_event(lt::surface::ButtonPressedEvent({}));
// expect_eq(surface.peek_events().size(), 2);
//
// system.tick(tick_info());
// expect_eq(surface.peek_events().size(), 0);
// };
// };
//
// Suite tick_handles_requests = "tick_handles_requests"_suite = [] {
// Case { "ticking clears requests" } = [] {
// auto fixture = Fixture {};
// auto system = System { fixture.registry() };
// auto &surface = **fixture.create_component();
//
// constexpr auto title = "ABC";
// constexpr auto position = lt::math::ivec2 { 50, 50 };
// constexpr auto resolution = lt::math::uvec2 { 50, 50 };
//
// expect_eq(surface.peek_requests().size(), 0);
//
// surface.push_request(lt::surface::ModifyVisibilityRequest(true));
// expect_eq(surface.peek_requests().size(), 1);
// system.tick(tick_info());
// expect_eq(surface.peek_requests().size(), 0);
//
// surface.push_request(lt::surface::ModifyTitleRequest(title));
// expect_eq(surface.peek_requests().size(), 1);
//
// surface.push_request(lt::surface::ModifyResolutionRequest(resolution));
// surface.push_request(lt::surface::ModifyPositionRequest(position));
// expect_eq(surface.peek_requests().size(), 1 + 2);
//
// surface.push_request(lt::surface::ModifyVisibilityRequest(false));
// surface.push_request(lt::surface::ModifyVisibilityRequest(true));
// surface.push_request(lt::surface::ModifyVisibilityRequest(false));
// expect_eq(surface.peek_requests().size(), 1 + 2 + 3);
//
// system.tick(tick_info());
// expect_eq(surface.peek_requests().size(), 0);
//
// expect_eq(surface.get_title(), title);
// expect_eq(surface.get_position(), position);
// expect_eq(surface.get_resolution(), resolution);
//
// lt::log::debug("EVENT COUNT: {}", surface.peek_events().size());
// for (const auto &event : surface.peek_events())
// {
// const auto visitor = overloads {
// [&](auto event) { lt::log::debug("event: {}", event.to_string()); },
// };
//
// std::visit(visitor, event);
// }
// };
// };
Suite registry_events = "registry_events"_suite = [] {
Case { "on_construct<SurfaceComponent> initializes component" } = [] {
auto fixture = Fixture {};
const auto &component = fixture.create_component();
expect_eq(fixture.registry()->view<SurfaceComponent>().get_size(), 1);
fixture.check_values(*component);
};
Case { "unhappy on_construct<SurfaceComponent> throws" } = [] {
auto fixture = Fixture {};
auto system = System { fixture.registry() };
expect_throw([&] { fixture.create_component({ .resolution = { width, 0 } }); });
expect_throw([&] { fixture.create_component({ .resolution = { 0, height } }); });
expect_throw([&] {
fixture.create_component(
{ .title = "", .resolution = { SurfaceComponent::max_dimension + 1, height } }
);
});
expect_throw([&] {
fixture.create_component(
{ .title = "", .resolution = { width, SurfaceComponent::max_dimension + 1 } }
);
});
auto big_str = std::string {};
big_str.resize(SurfaceComponent::max_title_length + 1);
expect_throw([&] {
fixture.create_component({ .title = big_str, .resolution = { width, height } });
});
};
Case { "unhappy on_construct<SurfaceComponent> removes component" } = [] {
auto fixture = Fixture {};
auto system = System { fixture.registry() };
expect_throw([&] { fixture.create_component({ .resolution = { width, 0 } }); });
expect_eq(fixture.registry()->view<SurfaceComponent>().get_size(), 0);
};
Case { "on_destrroy<SurfaceComponent> cleans up component" } = [] {
auto fixture = Fixture {};
auto system = lt::memory::create_scope<System>(fixture.registry());
const auto &component = fixture.create_component();
expect_eq(fixture.registry()->view<SurfaceComponent>().get_size(), 1);
fixture.check_values(*component);
system.reset();
expect_eq(fixture.registry()->view<SurfaceComponent>().get_size(), 0);
};
};
Suite tick = "tick"_suite = [] {
Case { "ticking on empty registry won't throw" } = [] {
auto fixture = Fixture {};
System { fixture.registry() }.tick(tick_info());
};
Case { "ticking on non-empty registry won't throw" } = [] {
auto fixture = Fixture {};
auto system = System { fixture.registry() };
fixture.create_component();
system.tick(tick_info());
};
};
Suite tick_handles_events = "tick_handles_events"_suite = [] {
Case { "ticking clears previous tick's events" } = [] {
auto fixture = Fixture {};
auto system = System { fixture.registry() };
auto &surface = **fixture.create_component();
// flush window-creation events
system.tick(tick_info());
expect_eq(surface.peek_events().size(), 0);
surface.push_event(lt::surface::MovedEvent({}, {}));
expect_eq(surface.peek_events().size(), 1);
surface.push_event(lt::surface::ButtonPressedEvent({}));
expect_eq(surface.peek_events().size(), 2);
system.tick(tick_info());
expect_eq(surface.peek_events().size(), 0);
};
};
Suite tick_handles_requests = "tick_handles_requests"_suite = [] {
Case { "ticking clears requests" } = [] {
auto fixture = Fixture {};
auto system = System { fixture.registry() };
auto &surface = **fixture.create_component();
constexpr auto position = lt::math::ivec2 { 50, 50 };
constexpr auto resolution = lt::math::uvec2 { width, height };
expect_eq(surface.peek_requests().size(), 0);
surface.push_request(lt::surface::ModifyVisibilityRequest(true));
expect_eq(surface.peek_requests().size(), 1);
system.tick(tick_info());
expect_eq(surface.peek_requests().size(), 0);
surface.push_request(lt::surface::ModifyTitleRequest(title));
expect_eq(surface.peek_requests().size(), 1);
surface.push_request(lt::surface::ModifyResolutionRequest(resolution));
surface.push_request(lt::surface::ModifyPositionRequest(position));
expect_eq(surface.peek_requests().size(), 1 + 2);
surface.push_request(lt::surface::ModifyVisibilityRequest(false));
surface.push_request(lt::surface::ModifyVisibilityRequest(true));
surface.push_request(lt::surface::ModifyVisibilityRequest(false));
expect_eq(surface.peek_requests().size(), 1 + 2 + 3);
system.tick(tick_info());
expect_eq(surface.peek_requests().size(), 0);
expect_eq(surface.get_title(), title);
expect_eq(surface.get_position(), position);
expect_eq(surface.get_resolution(), resolution);
};
};

View file

@ -1,12 +1,7 @@
import logger;
import test.test;
import test;
import test.registry;
import std;
using namespace ::lt::test;
void parse_option(std::string_view argument, Registry::Options &options)
void parse_option(std::string_view argument, lt::test::Registry::Options &options)
{
constexpr auto case_str = std::string_view { "--case=" };
constexpr auto suite_str = std::string_view { "--suite=" };
@ -19,7 +14,7 @@ void parse_option(std::string_view argument, Registry::Options &options)
if (argument.starts_with("--mode=") && argument.substr(7ul) == "stats")
{
options.execution_policy = Registry::ExecutionPolicy::stats;
options.execution_policy = lt::test::Registry::ExecutionPolicy::stats;
return;
}
@ -51,12 +46,12 @@ void print_help()
std::println("--help | -h --> ~You just used it! :D");
}
auto main(std::int32_t argc, char **argv) -> std::int32_t
auto main(i32 argc, char **argv) -> i32
try
{
auto raw_arguments = std::span<char *>(argv, argc);
auto options = Registry::Options {};
auto options = lt::test::Registry::Options {};
for (auto idx = 0; auto &raw_argument : raw_arguments)
{
// First argument is the "cwd'
@ -83,7 +78,7 @@ try
}
}
return static_cast<std::int32_t>(Registry::run_all(options));
return static_cast<i32>(lt::test::Registry::run_all(options));
}
catch (const std::exception &exp)
{

View file

@ -1,6 +1,7 @@
export module test.expects;
import std;
import preliminary;
namespace lt::test {
@ -31,6 +32,7 @@ export void expect_unreachable(
};
};
/** @todo(Light7734): Check exception type. */
export constexpr void expect_throw(
std::invocable auto invocable,
std::source_location source_location = std::source_location::current()
@ -40,7 +42,7 @@ export constexpr void expect_throw(
{
invocable();
}
catch (const std::exception &exp)
catch (const std::exception &)
{
return;
}

View file

@ -1,86 +0,0 @@
#include <cstring>
#include <test/test.hpp>
namespace lt::test {
class FuzzDataProvider
{
public:
FuzzDataProvider(const uint8_t *data, size_t size): m_data(data, size)
{
}
template<typename T>
requires(
std::is_trivially_constructible_v<T> //
&& std::is_trivially_copy_constructible_v<T> //
&& std::is_trivially_copy_assignable_v<T>
)
auto consume() -> std::optional<T>
{
if (m_data.size() < sizeof(T))
{
return std::nullopt;
}
T value;
std::memcpy(&value, m_data.data(), sizeof(T));
m_data = m_data.subspan(sizeof(T));
return value;
}
auto consume_string(size_t size) -> std::optional<std::string>
{
if (m_data.size() < size)
{
return std::nullopt;
}
// NOLINTNEXTLINE
auto value = std::string { (const char *)m_data.data(), size };
m_data = m_data.subspan(size);
return value;
}
auto consume_remaining_as_string() -> std::string
{
if (m_data.empty())
{
return std::string {};
}
return { m_data.begin(), m_data.end() };
};
private:
std::span<const uint8_t> m_data;
};
} // namespace lt::test
namespace lt::test {
auto process_fuzz_input(const uint8_t *data, size_t size) -> int32_t
try
{
return details::Registry::process_fuzz_input(data, size);
}
catch (const std::exception &exp)
{
std::println("Fuzz input resulted in uncaught exception:");
std::println("\twhat: {}", exp.what());
std::println("\tinput size: {}", size);
return EXIT_FAILURE;
}
}; // namespace lt::test
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
{
return lt::test::process_fuzz_input(data, size);
}

23
modules/test/module.cppm Normal file
View file

@ -0,0 +1,23 @@
export module test;
export import preliminary;
export import test.test;
export import test.expects;
export import test.expects;
export import logger;
export using ::lt::test::Suite;
export using ::lt::test::Case;
export using ::lt::test::expect_eq;
export using ::lt::test::expect_ne;
export using ::lt::test::expect_le;
export using ::lt::test::expect_true;
export using ::lt::test::expect_false;
export using ::lt::test::expect_throw;
export using ::lt::test::expect_not_nullptr;
export using ::lt::test::expect_unreachable;
export using ::lt::test::operator""_suite;

View file

@ -1,6 +1,6 @@
export module test.registry;
import std;
import preliminary;
import test.expects;
///////////////////////////////////////
@ -11,7 +11,7 @@ namespace lt::test {
export class Registry
{
public:
enum class ExecutionPolicy : std::uint8_t
enum class ExecutionPolicy : u8
{
normal,
stats,
@ -28,7 +28,7 @@ public:
std::string case_regex;
};
using FuzzFunction = std::int32_t (*)(const std::uint8_t *, std::size_t);
using FuzzFunction = i32 (*)(const u8 *, size_t);
using SuiteFunction = void (*)();
@ -36,9 +36,9 @@ public:
static void register_fuzz_harness(FuzzFunction suite);
static auto run_all(Options options) -> std::int32_t;
static auto run_all(Options options) -> i32;
static auto process_fuzz_input(const std::uint8_t *data, std::size_t size) -> std::int32_t;
static auto process_fuzz_input(const u8 *data, size_t size) -> i32;
static void set_last_suite_name(const char *name);
@ -73,7 +73,7 @@ private:
[[nodiscard]] static auto instance() -> Registry &;
auto run_all_impl() -> std::int32_t;
auto run_all_impl() -> i32;
void print_options();
@ -83,25 +83,25 @@ private:
FuzzFunction m_fuzz_harness {};
std::int32_t m_total_case_count {};
i32 m_total_case_count {};
std::int32_t m_passed_case_count {};
i32 m_passed_case_count {};
std::int32_t m_failed_case_count {};
i32 m_failed_case_count {};
std::int32_t m_matched_case_count {};
i32 m_matched_case_count {};
std::int32_t m_skipped_case_count {};
i32 m_skipped_case_count {};
std::int32_t m_total_suite_count {};
i32 m_total_suite_count {};
std::int32_t m_passed_suite_count {};
i32 m_passed_suite_count {};
std::int32_t m_failed_suite_count {};
i32 m_failed_suite_count {};
std::int32_t m_matched_suite_count {};
i32 m_matched_suite_count {};
std::int32_t m_skipped_suite_count {};
i32 m_skipped_suite_count {};
std::regex m_case_regex;
};
@ -131,14 +131,13 @@ namespace lt::test {
instance().m_fuzz_harness = suite;
}
/* static */ auto Registry::run_all(Options options) -> std::int32_t
/* static */ auto Registry::run_all(Options options) -> i32
{
instance().m_options = std::move(options);
return instance().run_all_impl();
}
/* static */ auto Registry::process_fuzz_input(const std::uint8_t *data, std::size_t size)
-> std::int32_t
/* static */ auto Registry::process_fuzz_input(const u8 *data, size_t size) -> i32
{
if (!instance().m_fuzz_harness)
{
@ -220,7 +219,7 @@ namespace lt::test {
return instance().m_case_regex;
}
auto Registry::run_all_impl() -> std::int32_t
auto Registry::run_all_impl() -> i32
{
print_options();
m_case_regex = std::regex(m_options.case_regex);

View file

@ -1,8 +1,8 @@
export module test.test;
import std;
import test.expects;
import test.registry;
import preliminary;
///////////////////////////////////////
// ----------* INTERFACE *--------- //
@ -38,7 +38,7 @@ struct TestFuzzHarness
export using Case = const TestCase;
export using Suite = const TestSuite;
export using FuzzHarness = const TestFuzzHarness;
export auto operator""_suite(const char *name, std::size_t size) -> TestSuite;
export auto operator""_suite(const char *name, size_t size) -> TestSuite;
///////////////////////////////////////
// * IMPLEMENTATION -- TEMPLATES * //
@ -106,8 +106,11 @@ constexpr TestFuzzHarness::TestFuzzHarness(auto body)
#endif
};
auto operator""_suite(const char *name, std::size_t size) -> TestSuite
auto operator""_suite(const char *name, size_t size) -> TestSuite
{
// TODO(Light): do we need the size parameter?
ignore = size;
Registry::set_last_suite_name(name);
return {};
}

View file

@ -1,21 +1,6 @@
import test.test;
import test.expects;
import std;
using lt::test::Case;
using lt::test::Suite;
using lt::test::operator""_suite;
import test;
Suite expects = "expects"_suite = []() {
using lt::test::expect_unreachable;
using lt::test::expect_true;
using lt::test::expect_false;
using lt::test::expect_eq;
using lt::test::expect_ne;
using lt::test::expect_le;
using lt::test::expect_throw;
Case { "" } = [] {
};
@ -24,7 +9,7 @@ Suite expects = "expects"_suite = []() {
// clang-format off
try { expect_unreachable(); }
catch (const std::exception &exp) { unhappy = true; }
catch (const std::exception &) { unhappy = true; }
// clang-format on
if (!unhappy)
@ -48,16 +33,16 @@ Suite expects = "expects"_suite = []() {
// clang-format off
try { expect_true(where_oongaboonga_ptr); }
catch (const std::exception& exp) { ++unhappy_counter; }
catch (const std::exception&) { ++unhappy_counter; }
try { expect_true(!true); }
catch (const std::exception& exp) { ++unhappy_counter; }
catch (const std::exception&) { ++unhappy_counter; }
try { expect_true(false); }
catch (const std::exception& exp) { ++unhappy_counter; }
catch (const std::exception&) { ++unhappy_counter; }
try { expect_true(0); } // NOLINT
catch (const std::exception& exp) { ++unhappy_counter; }
catch (const std::exception&) { ++unhappy_counter; }
// clang-format on
};
@ -66,27 +51,31 @@ Suite expects = "expects"_suite = []() {
expect_false(oongaboonga_is_slacking);
expect_false(false);
expect_false(0); // NOLINT
};
Case { "expect_false - unhappy" } = [] {
auto oongaboonga = int {};
auto *oonga_oonga_can_rest_now = (int *)nullptr;
auto *oonga_oonga_can_rest_now = (u32 *)nullptr;
auto unhappy_counter = 0u;
oonga_oonga_can_rest_now = &unhappy_counter;
// clang-format off
try { expect_false(oonga_oonga_can_rest_now); }
catch (const std::exception& exp) { ++unhappy_counter; }
catch (const std::exception&) { ++unhappy_counter; }
try { expect_false(true); }
catch (const std::exception& exp) { ++unhappy_counter; }
catch (const std::exception&) { ++unhappy_counter; }
try { expect_false(!false); }
catch (const std::exception& exp) { ++unhappy_counter; }
catch (const std::exception&) { ++unhappy_counter; }
try { expect_false(1); } // NOLINT
catch (const std::exception& exp) { ++unhappy_counter; }
try { expect_false(!!1); }
catch (const std::exception&) { ++unhappy_counter; }
// clang-format on
if (unhappy_counter != 4)
{
throw std::runtime_error { "expect_false - unhappy" };
}
};
Case { "expect_true - unhappy" } = [] {
@ -95,23 +84,29 @@ Suite expects = "expects"_suite = []() {
// clang-format off
try { expect_true(where_oongaboonga_ptr); }
catch (const std::exception& exp) { ++unhappy_counter; }
catch (const std::exception&) { ++unhappy_counter; }
try { expect_true(!true); }
catch (const std::exception& exp) { ++unhappy_counter; }
catch (const std::exception&) { ++unhappy_counter; }
try { expect_true(false); }
catch (const std::exception& exp) { ++unhappy_counter; }
catch (const std::exception&) { ++unhappy_counter; }
try { expect_true(0); } // NOLINT
catch (const std::exception& exp) { ++unhappy_counter; }
try { expect_true(!!0); }
catch (const std::exception&) { ++unhappy_counter; }
// clang-format on
if (unhappy_counter != 4)
{
throw std::runtime_error { "expect_true - unhappy" };
}
};
Case { "expect_eq - happy" } = [] {
expect_eq(5, 5);
expect_eq(20.0, 20.0);
expect_eq(true, 1);
expect_eq(true, true);
expect_eq(false, false);
};
Case { "expect_eq - unhappy" } = [] {
@ -119,7 +114,7 @@ Suite expects = "expects"_suite = []() {
// clang-format off
try { expect_eq(true, false); }
catch (const std::exception &exp) { unhappy = true; }
catch (const std::exception &) { unhappy = true; }
// clang-format on
if (!unhappy)
@ -131,7 +126,8 @@ Suite expects = "expects"_suite = []() {
Case { "expect_ne - happy " } = [] {
expect_ne(5, 5.0000001);
expect_ne(20.0, 69.0);
expect_ne(true, 0);
expect_ne(true, false);
expect_ne(false, true);
};
Case { "expect_ne - unhappy" } = [] {
@ -139,16 +135,19 @@ Suite expects = "expects"_suite = []() {
// clang-format off
try { expect_ne(5, 5); }
catch (const std::exception &exp) { ++unhappy_counter; }
catch (const std::exception &) { ++unhappy_counter; }
try { expect_ne(20.0, 20.0); }
catch (const std::exception &exp) { ++unhappy_counter; }
catch (const std::exception &) { ++unhappy_counter; }
try { expect_ne(true, 1); }
catch (const std::exception &exp) { ++unhappy_counter; }
try { expect_ne(true, true); }
catch (const std::exception &) { ++unhappy_counter; }
try { expect_ne(false, false); }
catch (const std::exception &) { ++unhappy_counter; }
// clang-format on
if (unhappy_counter != 3)
if (unhappy_counter != 4)
{
throw std::runtime_error { "expect_ne unhappy" };
}
@ -163,7 +162,7 @@ Suite expects = "expects"_suite = []() {
// clang-format off
try { expect_throw([] {}); }
catch (const std::exception &exp) { unhappy = true; }
catch (const std::exception &) { unhappy = true; }
// clang-format on
if (!unhappy)
@ -175,7 +174,7 @@ Suite expects = "expects"_suite = []() {
Case { "expect_le - happy" } = [] {
expect_le(69, 420);
expect_le(19.694206942069420, 20.0);
expect_le(false, 1);
expect_le(false, !!1);
};
Case { "expect_le - unhappy" } = [] {
@ -183,16 +182,16 @@ Suite expects = "expects"_suite = []() {
// clang-format off
try { expect_le(20020619 + 23, 20020619 ); }
catch (const std::exception &exp) { ++unhappy_counter; }
catch (const std::exception &) { ++unhappy_counter; }
try { expect_le(420, 69); }
catch (const std::exception &exp) { ++unhappy_counter; }
catch (const std::exception &) { ++unhappy_counter; }
try { expect_le(20.0, 19.694206942069420); }
catch (const std::exception &exp) { ++unhappy_counter; }
catch (const std::exception &) { ++unhappy_counter; }
try { expect_le(1, false); }
catch (const std::exception &exp) { ++unhappy_counter; }
try { expect_le(true, false); }
catch (const std::exception &) { ++unhappy_counter; }
// clang-format on
if (unhappy_counter != 4)

View file

@ -1,15 +1,16 @@
export module time;
import std;
namespace lt::time {
import preliminary;
export namespace lt::time {
/** Simple timer class to keep track of the elapsed time. */
export class Timer
class Timer
{
public:
using Clock = std::chrono::steady_clock;
using Duration = std::chrono::duration<double>;
using Duration = std::chrono::duration<f64>;
using Timepoint = std::chrono::time_point<std::chrono::steady_clock>;

View file

@ -1,33 +1,25 @@
import test;
import time;
import test.test;
import test.expects;
import std;
using ::lt::test::Case;
using ::lt::test::expect_le;
using ::lt::test::operator""_suite;
using ::lt::test::Suite;
using ::lt::time::Timer;
// error margin is high since run-time may slow down extremely due to
// sanitization/debugging or execution through valgrind...
//
// <1us error margin is tested manually in release builds and it works fine.
/* @note: error margin is high since run-time may slow down extremely due to
* sanitization/debugging or execution through valgrind...
* <1us error margin is tested manually in release builds and it works fine.
**/
constexpr auto max_error_margin = std::chrono::milliseconds { 1 };
Suite raii = "raii"_suite = [] {
using std::chrono::microseconds;
Case { "default" } = [] {
Case { "happy paths" } = [] {
Timer {};
};
Case { "unhappy path throws" } = [] {
};
Case { "plenty" } = [] {
Case { "many" } = [] {
for (auto idx : std::views::iota(0, 100'001))
{
ignore = idx;
Timer {};
}
};
@ -37,9 +29,9 @@ Suite reset_and_elapsed_time = "reset_and_elapsed_time"_suite = [] {
using std::chrono::hours;
using std::chrono::microseconds;
Case { "won't throw" } = [] {
Case { "happy path" } = [] {
Timer {}.reset();
std::ignore = Timer {}.elapsed_time();
ignore = Timer {}.elapsed_time();
};
Case { "elapsed time is sane" } = [] {

View file

@ -1,23 +1,25 @@
// @todo(Light): Implement...
export module debug.instrumentor;
import std;
import preliminary;
import logger;
namespace lt::debug {
namespace lt::tracer {
struct ScopeProfileResult
struct ScopeTraceResult
{
std::string name;
long long start, duration;
std::uint32_t threadID;
u64 start, duration;
u32 threadID;
};
class Instrumentor
class Tracer
{
public:
static auto instance() -> Instrumentor &
static auto instance() -> Tracer &
{
static auto instance = Instrumentor {};
static auto instance = Tracer {};
return instance;
}
@ -30,7 +32,7 @@ public:
instance().end_session_impl();
}
static void submit_scope_profile(const ScopeProfileResult &profileResult)
static void submit_scope_profile(const ScopeTraceResult &profileResult)
{
instance().submit_scope_profile_impl(profileResult);
}
@ -40,46 +42,46 @@ private:
unsigned int m_current_session_count { 0u };
Instrumentor() = default;
Tracer() = default;
void begin_session_impl(const std::string &outputPath);
void end_session_impl();
void submit_scope_profile_impl(const ScopeProfileResult &profileResult);
void submit_scope_profile_impl(const ScopeTraceResult &profileResult);
};
class InstrumentorTimer
class TracerTimer
{
public:
InstrumentorTimer(const std::string &scopeName);
TracerTimer(const std::string &scopeName);
~InstrumentorTimer();
~TracerTimer();
private:
ScopeProfileResult m_result;
ScopeTraceResult m_result;
std::chrono::time_point<std::chrono::steady_clock> m_start;
};
} // namespace lt::debug
} // namespace lt::tracer
/* scope */
#define lt_profile_scope(name) lt_profile_scope_no_redifinition(name, __LINE__)
#define lt_profile_scope_no_redifinition(name, line) lt_profile_scope_no_redifinition2(name, line)
#define lt_profile_scope_no_redifinition2(name, line) InstrumentorTimer timer##line(name)
#define lt_trace_scope(name) lt_profile_scope_no_redifinition(name, __LINE__)
#define lt_trace_scope_no_redifinition(name, line) lt_profile_scope_no_redifinition2(name, line)
#define lt_trace_scope_no_redifinition2(name, line) InstrumentorTimer timer##line(name)
/* function */
#define LT_PROFILE_FUNCTION lt_profile_scope(__FUNCSIG__)
#define lt_trace_function lt_profile_scope(__FUNCSIG__)
/* session */
#define lt_profile_begin_session(outputPath) ::lt::Instrumentor::begin_session(outputPath)
#define lt_profile_end_session() ::lt::Instrumentor::end_session()
#define lt_trace_begin_session(outputPath) ::lt::Instrumentor::begin_session(outputPath)
#define lt_trace_end_session() ::lt::Instrumentor::end_session()
module :private;
namespace lt::debug {
namespace lt::tracer {
void Instrumentor::begin_session_impl(const std::string &outputPath)
void Tracer::begin_session_impl(const std::string &outputPath)
{
std::filesystem::create_directory(outputPath.substr(0, outputPath.find_last_of('/') + 1));
@ -87,7 +89,7 @@ void Instrumentor::begin_session_impl(const std::string &outputPath)
m_output_file_stream << "{\"traceEvents\":[";
}
void Instrumentor::end_session_impl()
void Tracer::end_session_impl()
{
if (m_current_session_count == 0u)
{
@ -101,7 +103,7 @@ void Instrumentor::end_session_impl()
m_output_file_stream.close();
}
void Instrumentor::submit_scope_profile_impl(const ScopeProfileResult &profileResult)
void Tracer::submit_scope_profile_impl(const ScopeTraceResult &profileResult)
{
if (m_current_session_count++ == 0u)
{
@ -122,13 +124,13 @@ void Instrumentor::submit_scope_profile_impl(const ScopeProfileResult &profileRe
m_output_file_stream << "}";
}
InstrumentorTimer::InstrumentorTimer(const std::string &scopeName)
TracerTimer::TracerTimer(const std::string &scopeName)
: m_result({ .name = scopeName, .start = 0, .duration = 0, .threadID = 0 })
, m_start(std::chrono::steady_clock::now())
{
}
InstrumentorTimer::~InstrumentorTimer()
TracerTimer::~TracerTimer()
{
auto end = std::chrono::steady_clock::now();
@ -141,6 +143,6 @@ InstrumentorTimer::~InstrumentorTimer()
.count()
- m_result.start;
Instrumentor::submit_scope_profile(m_result);
Tracer::submit_scope_profile(m_result);
}
} // namespace lt::debug
} // namespace lt::tracer

View file

@ -1,5 +1,15 @@
conan profile detect
conan build . -s compiler.cppstd=20 -s build_type=Release
# @ref https://learn.microsoft.com/en-us/cpp/build/reference/compiler-options-listed-by-category
rm -Force -Recurse ./build
cmake `
-S . `
-B build `
-G Ninja `
-D ENABLE_UNIT_TESTS=ON `
-D CMAKE_BUILD_TYPE=Release `
-D CMAKE_CXX_FLAGS="/std:c++latest /EHsc /Zi /Oy- /WX /W4"
cmake --build ./build
$tests = Get-ChildItem -Path "./build" -Recurse -File | Where-Object {
$_.Name -like "*_tests.exe"