diff --git a/engine/include/sophia/asset_registry.hpp b/engine/include/sophia/asset_registry.hpp new file mode 100644 index 0000000..4ccf294 --- /dev/null +++ b/engine/include/sophia/asset_registry.hpp @@ -0,0 +1,64 @@ +#pragma once + +#include +#include +#include +#include + +#include "sophia/canvas.hpp" +#include "sophia/types.hpp" + +namespace sophia { + +class Device; +class Mesh; + +template +struct AssetTraits; + +template <> +struct AssetTraits { + using Handle = MeshHandle; +}; + +class AssetRegistry { + public: + ~AssetRegistry(); + + AssetRegistry(const AssetRegistry&) = delete; + AssetRegistry& operator=(const AssetRegistry&) = delete; + + template + typename AssetTraits::Handle load(const std::string& path); + + /** + * TEMPORARY: Remove once load can read real model files from disk. + */ + MeshHandle loadMesh(std::vector vertices, + std::vector indices); + + template + void release(typename AssetTraits::Handle handle); + + private: + friend class Application; + + explicit AssetRegistry(Device& device); + + template + const T& get(typename AssetTraits::Handle handle) const; + + class Impl; + std::unique_ptr impl; +}; + +template <> +MeshHandle AssetRegistry::load(const std::string& path); + +template <> +void AssetRegistry::release(MeshHandle handle); + +template <> +const Mesh& AssetRegistry::get(MeshHandle handle) const; + +} // namespace sophia diff --git a/engine/include/sophia/canvas.hpp b/engine/include/sophia/canvas.hpp index 47d884e..54b6d84 100644 --- a/engine/include/sophia/canvas.hpp +++ b/engine/include/sophia/canvas.hpp @@ -33,7 +33,7 @@ class Canvas { private: friend class Application; - Canvas(Application* application); + explicit Canvas(Application* application); class Impl; std::unique_ptr impl; diff --git a/engine/include/sophia/components.hpp b/engine/include/sophia/components.hpp index 61b9ac3..bcebe53 100644 --- a/engine/include/sophia/components.hpp +++ b/engine/include/sophia/components.hpp @@ -1,11 +1,13 @@ #pragma once +#include +#include + #define GLM_FORCE_RADIANS #define GLM_FORCE_DEPTH_ZERO_TO_ONE #include #include #include -#include namespace sophia { diff --git a/engine/include/sophia/handles.hpp b/engine/include/sophia/handles.hpp new file mode 100644 index 0000000..a668790 --- /dev/null +++ b/engine/include/sophia/handles.hpp @@ -0,0 +1,33 @@ +#pragma once + +#include + +#include "sophia/types.hpp" + +namespace sophia { + +template +struct Handle { + static constexpr u32 INVALID_INDEX = (1u << 20) - 1; + + u32 index : 20 = INVALID_INDEX; + u32 generation : 12 = 1; + + [[nodiscard]] static constexpr Handle invalid() { + return Handle{INVALID_INDEX, 0}; + } + + [[nodiscard]] constexpr bool isValid() const { + return index != INVALID_INDEX; + } + + [[nodiscard]] auto operator<=>(const Handle&) const = default; +}; + +struct MeshTag {}; +struct PanelTag {}; + +using MeshHandle = Handle; +using PanelHandle = Handle; + +} // namespace sophia diff --git a/engine/include/sophia/types.hpp b/engine/include/sophia/types.hpp index f121532..83fc851 100644 --- a/engine/include/sophia/types.hpp +++ b/engine/include/sophia/types.hpp @@ -17,6 +17,4 @@ using s64 = int64_t; using f32 = float; using f64 = double; -using MeshHandle = u32; - } // namespace sophia diff --git a/engine/src/assets/asset_manager.cpp b/engine/src/assets/asset_manager.cpp deleted file mode 100644 index 7ec4d74..0000000 --- a/engine/src/assets/asset_manager.cpp +++ /dev/null @@ -1,24 +0,0 @@ -#include "assets/asset_manager.hpp" - -#include "mesh.hpp" - -namespace sophia { - -AssetManager::AssetManager(Device& device) : device{device}, meshPool{device} {} - -template <> -MeshHandle AssetManager::create(const Mesh::Builder& builder) { - return meshPool.create(builder); -} - -template <> -const Mesh& AssetManager::get(MeshHandle handle) const { - return meshPool.get(handle); -} - -template <> -void AssetManager::release(MeshHandle handle) { - meshPool.release(handle); -} - -} // namespace sophia diff --git a/engine/src/assets/asset_manager.hpp b/engine/src/assets/asset_manager.hpp deleted file mode 100644 index 91abc30..0000000 --- a/engine/src/assets/asset_manager.hpp +++ /dev/null @@ -1,41 +0,0 @@ -#pragma once - -#include - -#include "device.hpp" -#include "pools/mesh_pool.hpp" - -namespace sophia { - -class AssetManager { - public: - explicit AssetManager(Device& device); - ~AssetManager() = default; - - AssetManager(const AssetManager&) = delete; - AssetManager& operator=(const AssetManager&) = delete; - - template - using HandleFor = typename T::HandleType; - - template - HandleFor load(const std::string& path); - - template - HandleFor create(const typename T::Builder& builder); - - template - const T& get(HandleFor handle) const; - - template - void release(HandleFor handle); - - private: - Device& device; - - MeshPool meshPool; - // TexturePool texturePool; - // MaterialPool materialPool; -}; - -} // namespace sophia diff --git a/engine/src/assets/asset_registry.cpp b/engine/src/assets/asset_registry.cpp new file mode 100644 index 0000000..27fcdd9 --- /dev/null +++ b/engine/src/assets/asset_registry.cpp @@ -0,0 +1,60 @@ +#include +#include + +#include "mesh.hpp" +#include "pools/mesh_pool.hpp" + +namespace sophia { + +class AssetRegistry::Impl { + public: + explicit Impl(Device& device) : meshPool{device} {} + + MeshPool meshPool; + // TexturePool texturePool; + // MaterialPool materialPool; +}; + +AssetRegistry::AssetRegistry(Device& device) + : impl{std::make_unique(device)} {} + +AssetRegistry::~AssetRegistry() = default; + +template <> +MeshHandle AssetRegistry::load(const std::string& path) { + (void)path; + SOPHIA_ASSERT(false, + "Mesh::load(path) is not implemented yet - no model file " + "format is supported; build one via Mesh::Builder for now"); + return MeshHandle{}; +} + +template +typename AssetTraits::Handle load(const std::string&) { + static_assert(sizeof(T) == 0, + "load is not implemented for this asset type"); +} + +template <> +void AssetRegistry::release(MeshHandle handle) { + impl->meshPool.release(handle); +} + +template <> +const Mesh& AssetRegistry::get(MeshHandle handle) const { + return impl->meshPool.get(handle); +} + +MeshHandle AssetRegistry::loadMesh(std::vector vertices, + std::vector indices) { + Mesh::Builder builder{}; + builder.vertices.reserve(vertices.size()); + for (const CanvasVertex& vertex : vertices) { + builder.vertices.push_back({vertex.position, vertex.color}); + } + builder.indices = std::move(indices); + + return impl->meshPool.create(builder); +} + +} // namespace sophia diff --git a/engine/src/mesh.cpp b/engine/src/mesh.cpp index 533fefd..b865bad 100644 --- a/engine/src/mesh.cpp +++ b/engine/src/mesh.cpp @@ -36,7 +36,7 @@ Mesh::Mesh(Device& device, const Builder& builder) : device{device} { createIndexBuffers(builder.indices); } -Mesh::~Mesh() {} +Mesh::~Mesh() = default; void Mesh::bind(vk::CommandBuffer commandBuffer) const { vk::Buffer buffers[] = {this->vertexBuffer->getBuffer()}; diff --git a/engine/src/mesh.hpp b/engine/src/mesh.hpp index 880fb09..7ee69a8 100644 --- a/engine/src/mesh.hpp +++ b/engine/src/mesh.hpp @@ -1,6 +1,7 @@ #pragma once #include +#include #include #include diff --git a/engine/src/render_context.cpp b/engine/src/render_context.cpp index 9cfe4fd..76296c6 100644 --- a/engine/src/render_context.cpp +++ b/engine/src/render_context.cpp @@ -142,8 +142,6 @@ void RenderContext::recreateSwapchain() { // Old fences referred to images owned by the now-replaced swapchain; // there's nothing valid left to wait on. this->imagesInFlight.assign(this->swapchain->imageCount(), vk::Fence{}); - - log::trace("recreated swapchain"); } void RenderContext::createFrames() { diff --git a/engine/src/swapchain.cpp b/engine/src/swapchain.cpp index a7f49ee..ed07c2e 100644 --- a/engine/src/swapchain.cpp +++ b/engine/src/swapchain.cpp @@ -186,7 +186,7 @@ vk::PresentModeKHR Swapchain::choosePresentMode( bestMode = availablePresentMode; } } - log::warning("Present mode MAILBOX not available."); + log::trace("Present mode MAILBOX not available."); if (bestMode == vk::PresentModeKHR::eImmediate) { log::info("Selected present mode: IMMEDIATE (no vsync)");