AssetRegistry and small changes
This commit is contained in:
@@ -0,0 +1,64 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <memory>
|
||||||
|
#include <sophia/handles.hpp>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
#include "sophia/canvas.hpp"
|
||||||
|
#include "sophia/types.hpp"
|
||||||
|
|
||||||
|
namespace sophia {
|
||||||
|
|
||||||
|
class Device;
|
||||||
|
class Mesh;
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
struct AssetTraits;
|
||||||
|
|
||||||
|
template <>
|
||||||
|
struct AssetTraits<Mesh> {
|
||||||
|
using Handle = MeshHandle;
|
||||||
|
};
|
||||||
|
|
||||||
|
class AssetRegistry {
|
||||||
|
public:
|
||||||
|
~AssetRegistry();
|
||||||
|
|
||||||
|
AssetRegistry(const AssetRegistry&) = delete;
|
||||||
|
AssetRegistry& operator=(const AssetRegistry&) = delete;
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
typename AssetTraits<T>::Handle load(const std::string& path);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* TEMPORARY: Remove once load<Mesh> can read real model files from disk.
|
||||||
|
*/
|
||||||
|
MeshHandle loadMesh(std::vector<CanvasVertex> vertices,
|
||||||
|
std::vector<u32> indices);
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
void release(typename AssetTraits<T>::Handle handle);
|
||||||
|
|
||||||
|
private:
|
||||||
|
friend class Application;
|
||||||
|
|
||||||
|
explicit AssetRegistry(Device& device);
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
const T& get(typename AssetTraits<T>::Handle handle) const;
|
||||||
|
|
||||||
|
class Impl;
|
||||||
|
std::unique_ptr<Impl> impl;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <>
|
||||||
|
MeshHandle AssetRegistry::load<Mesh>(const std::string& path);
|
||||||
|
|
||||||
|
template <>
|
||||||
|
void AssetRegistry::release<Mesh>(MeshHandle handle);
|
||||||
|
|
||||||
|
template <>
|
||||||
|
const Mesh& AssetRegistry::get<Mesh>(MeshHandle handle) const;
|
||||||
|
|
||||||
|
} // namespace sophia
|
||||||
@@ -33,7 +33,7 @@ class Canvas {
|
|||||||
private:
|
private:
|
||||||
friend class Application;
|
friend class Application;
|
||||||
|
|
||||||
Canvas(Application* application);
|
explicit Canvas(Application* application);
|
||||||
|
|
||||||
class Impl;
|
class Impl;
|
||||||
std::unique_ptr<Impl> impl;
|
std::unique_ptr<Impl> impl;
|
||||||
|
|||||||
@@ -1,11 +1,13 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <sophia/asset_registry.hpp>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
#define GLM_FORCE_RADIANS
|
#define GLM_FORCE_RADIANS
|
||||||
#define GLM_FORCE_DEPTH_ZERO_TO_ONE
|
#define GLM_FORCE_DEPTH_ZERO_TO_ONE
|
||||||
#include <glm/glm.hpp>
|
#include <glm/glm.hpp>
|
||||||
#include <glm/gtc/matrix_transform.hpp>
|
#include <glm/gtc/matrix_transform.hpp>
|
||||||
#include <glm/gtc/quaternion.hpp>
|
#include <glm/gtc/quaternion.hpp>
|
||||||
#include <string>
|
|
||||||
|
|
||||||
namespace sophia {
|
namespace sophia {
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,33 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <compare>
|
||||||
|
|
||||||
|
#include "sophia/types.hpp"
|
||||||
|
|
||||||
|
namespace sophia {
|
||||||
|
|
||||||
|
template <typename Tag>
|
||||||
|
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<MeshTag>;
|
||||||
|
using PanelHandle = Handle<PanelTag>;
|
||||||
|
|
||||||
|
} // namespace sophia
|
||||||
@@ -17,6 +17,4 @@ using s64 = int64_t;
|
|||||||
using f32 = float;
|
using f32 = float;
|
||||||
using f64 = double;
|
using f64 = double;
|
||||||
|
|
||||||
using MeshHandle = u32;
|
|
||||||
|
|
||||||
} // namespace sophia
|
} // namespace sophia
|
||||||
|
|||||||
@@ -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<Mesh>(const Mesh::Builder& builder) {
|
|
||||||
return meshPool.create(builder);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <>
|
|
||||||
const Mesh& AssetManager::get<Mesh>(MeshHandle handle) const {
|
|
||||||
return meshPool.get(handle);
|
|
||||||
}
|
|
||||||
|
|
||||||
template <>
|
|
||||||
void AssetManager::release<Mesh>(MeshHandle handle) {
|
|
||||||
meshPool.release(handle);
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace sophia
|
|
||||||
@@ -1,41 +0,0 @@
|
|||||||
#pragma once
|
|
||||||
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
#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 <typename T>
|
|
||||||
using HandleFor = typename T::HandleType;
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
HandleFor<T> load(const std::string& path);
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
HandleFor<T> create(const typename T::Builder& builder);
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
const T& get(HandleFor<T> handle) const;
|
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
void release(HandleFor<T> handle);
|
|
||||||
|
|
||||||
private:
|
|
||||||
Device& device;
|
|
||||||
|
|
||||||
MeshPool meshPool;
|
|
||||||
// TexturePool texturePool;
|
|
||||||
// MaterialPool materialPool;
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace sophia
|
|
||||||
@@ -0,0 +1,60 @@
|
|||||||
|
#include <sophia/asset_registry.hpp>
|
||||||
|
#include <sophia/util/assert.hpp>
|
||||||
|
|
||||||
|
#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<Impl>(device)} {}
|
||||||
|
|
||||||
|
AssetRegistry::~AssetRegistry() = default;
|
||||||
|
|
||||||
|
template <>
|
||||||
|
MeshHandle AssetRegistry::load<Mesh>(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 T>
|
||||||
|
typename AssetTraits<T>::Handle load(const std::string&) {
|
||||||
|
static_assert(sizeof(T) == 0,
|
||||||
|
"load<T> is not implemented for this asset type");
|
||||||
|
}
|
||||||
|
|
||||||
|
template <>
|
||||||
|
void AssetRegistry::release<Mesh>(MeshHandle handle) {
|
||||||
|
impl->meshPool.release(handle);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <>
|
||||||
|
const Mesh& AssetRegistry::get<Mesh>(MeshHandle handle) const {
|
||||||
|
return impl->meshPool.get(handle);
|
||||||
|
}
|
||||||
|
|
||||||
|
MeshHandle AssetRegistry::loadMesh(std::vector<CanvasVertex> vertices,
|
||||||
|
std::vector<u32> 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
|
||||||
+1
-1
@@ -36,7 +36,7 @@ Mesh::Mesh(Device& device, const Builder& builder) : device{device} {
|
|||||||
createIndexBuffers(builder.indices);
|
createIndexBuffers(builder.indices);
|
||||||
}
|
}
|
||||||
|
|
||||||
Mesh::~Mesh() {}
|
Mesh::~Mesh() = default;
|
||||||
|
|
||||||
void Mesh::bind(vk::CommandBuffer commandBuffer) const {
|
void Mesh::bind(vk::CommandBuffer commandBuffer) const {
|
||||||
vk::Buffer buffers[] = {this->vertexBuffer->getBuffer()};
|
vk::Buffer buffers[] = {this->vertexBuffer->getBuffer()};
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <sophia/asset_registry.hpp>
|
||||||
#include <sophia/types.hpp>
|
#include <sophia/types.hpp>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
|||||||
@@ -142,8 +142,6 @@ void RenderContext::recreateSwapchain() {
|
|||||||
// Old fences referred to images owned by the now-replaced swapchain;
|
// Old fences referred to images owned by the now-replaced swapchain;
|
||||||
// there's nothing valid left to wait on.
|
// there's nothing valid left to wait on.
|
||||||
this->imagesInFlight.assign(this->swapchain->imageCount(), vk::Fence{});
|
this->imagesInFlight.assign(this->swapchain->imageCount(), vk::Fence{});
|
||||||
|
|
||||||
log::trace("recreated swapchain");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void RenderContext::createFrames() {
|
void RenderContext::createFrames() {
|
||||||
|
|||||||
@@ -186,7 +186,7 @@ vk::PresentModeKHR Swapchain::choosePresentMode(
|
|||||||
bestMode = availablePresentMode;
|
bestMode = availablePresentMode;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
log::warning("Present mode MAILBOX not available.");
|
log::trace("Present mode MAILBOX not available.");
|
||||||
|
|
||||||
if (bestMode == vk::PresentModeKHR::eImmediate) {
|
if (bestMode == vk::PresentModeKHR::eImmediate) {
|
||||||
log::info("Selected present mode: IMMEDIATE (no vsync)");
|
log::info("Selected present mode: IMMEDIATE (no vsync)");
|
||||||
|
|||||||
Reference in New Issue
Block a user