Add offscreen render targets and rename AssetRegistry to ResourceRegistry #8
@@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
namespace sophia {
|
namespace sophia {
|
||||||
|
|
||||||
template <typename Tag>
|
template <typename T>
|
||||||
struct Handle {
|
struct Handle {
|
||||||
static constexpr u32 INVALID_INDEX = (1u << 20) - 1;
|
static constexpr u32 INVALID_INDEX = (1u << 20) - 1;
|
||||||
|
|
||||||
@@ -24,14 +24,14 @@ struct Handle {
|
|||||||
[[nodiscard]] auto operator<=>(const Handle&) const = default;
|
[[nodiscard]] auto operator<=>(const Handle&) const = default;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct MeshTag {};
|
class Mesh;
|
||||||
struct PanelTag {};
|
class Panel;
|
||||||
struct RenderTargetTag {};
|
class RenderTarget;
|
||||||
struct SceneTag {};
|
class Scene;
|
||||||
|
|
||||||
using MeshHandle = Handle<MeshTag>;
|
using MeshHandle = Handle<Mesh>;
|
||||||
using PanelHandle = Handle<PanelTag>;
|
using PanelHandle = Handle<Panel>;
|
||||||
using RenderTargetHandle = Handle<RenderTargetTag>;
|
using RenderTargetHandle = Handle<RenderTarget>;
|
||||||
using SceneHandle = Handle<SceneTag>;
|
using SceneHandle = Handle<Scene>;
|
||||||
|
|
||||||
} // namespace sophia
|
} // namespace sophia
|
||||||
|
|||||||
@@ -14,30 +14,15 @@
|
|||||||
|
|
||||||
namespace sophia {
|
namespace sophia {
|
||||||
|
|
||||||
class Mesh;
|
|
||||||
class RenderTarget;
|
|
||||||
class ResourceRegistry;
|
class ResourceRegistry;
|
||||||
|
|
||||||
template <typename T>
|
|
||||||
struct ResourceTraits;
|
|
||||||
|
|
||||||
template <>
|
|
||||||
struct ResourceTraits<Mesh> {
|
|
||||||
using Handle = MeshHandle;
|
|
||||||
};
|
|
||||||
|
|
||||||
template <>
|
|
||||||
struct ResourceTraits<RenderTarget> {
|
|
||||||
using Handle = RenderTargetHandle;
|
|
||||||
};
|
|
||||||
|
|
||||||
class ResourceRegistryRef {
|
class ResourceRegistryRef {
|
||||||
public:
|
public:
|
||||||
template <typename T>
|
template <typename T>
|
||||||
typename ResourceTraits<T>::Handle load(const std::string& path);
|
Handle<T> load(const std::string& path);
|
||||||
|
|
||||||
template <typename T, typename... Args>
|
template <typename T, typename... Args>
|
||||||
typename ResourceTraits<T>::Handle create(Args&&... args) {
|
Handle<T> create(Args&&... args) {
|
||||||
return createResource(std::type_identity<T>{}, std::forward<Args>(args)...);
|
return createResource(std::type_identity<T>{}, std::forward<Args>(args)...);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -48,7 +33,7 @@ class ResourceRegistryRef {
|
|||||||
bool isAlive(RenderTargetHandle handle) const;
|
bool isAlive(RenderTargetHandle handle) const;
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
void release(typename ResourceTraits<T>::Handle handle);
|
void release(Handle<T> handle);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
friend class Application;
|
friend class Application;
|
||||||
|
|||||||
@@ -25,7 +25,7 @@ class SceneRegistry {
|
|||||||
u32 size() const { return pool.size(); }
|
u32 size() const { return pool.size(); }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Pool<Scene, SceneHandle> pool{"Scene"};
|
Pool<Scene> pool{"Scene"};
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace sophia
|
} // namespace sophia
|
||||||
|
|||||||
@@ -93,7 +93,7 @@ class PanelRegistry {
|
|||||||
const DockLayout& getDefaultLayout() const { return defaultLayout; }
|
const DockLayout& getDefaultLayout() const { return defaultLayout; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Pool<Panel, PanelHandle> pool{"Panel"};
|
Pool<Panel> pool{"Panel"};
|
||||||
DockLayout defaultLayout;
|
DockLayout defaultLayout;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <sophia/handles.hpp>
|
||||||
#include <sophia/types.hpp>
|
#include <sophia/types.hpp>
|
||||||
#include <sophia/util/assert.hpp>
|
#include <sophia/util/assert.hpp>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
@@ -8,7 +9,7 @@
|
|||||||
|
|
||||||
namespace sophia {
|
namespace sophia {
|
||||||
|
|
||||||
template <typename T, typename HandleType>
|
template <typename T>
|
||||||
class Pool {
|
class Pool {
|
||||||
public:
|
public:
|
||||||
explicit Pool(const char* name) : name{name} {}
|
explicit Pool(const char* name) : name{name} {}
|
||||||
@@ -17,29 +18,29 @@ class Pool {
|
|||||||
Pool& operator=(const Pool&) = delete;
|
Pool& operator=(const Pool&) = delete;
|
||||||
|
|
||||||
template <typename... Args>
|
template <typename... Args>
|
||||||
HandleType create(Args&&... args) {
|
Handle<T> create(Args&&... args) {
|
||||||
return add(std::make_unique<T>(std::forward<Args>(args)...));
|
return add(std::make_unique<T>(std::forward<Args>(args)...));
|
||||||
}
|
}
|
||||||
|
|
||||||
HandleType add(std::unique_ptr<T> item) {
|
Handle<T> add(std::unique_ptr<T> item) {
|
||||||
if (!freeList.empty()) {
|
if (!freeList.empty()) {
|
||||||
u32 index = freeList.back();
|
u32 index = freeList.back();
|
||||||
freeList.pop_back();
|
freeList.pop_back();
|
||||||
slots[index].item = std::move(item);
|
slots[index].item = std::move(item);
|
||||||
return HandleType{index, slots[index].generation};
|
return Handle<T>{index, slots[index].generation};
|
||||||
}
|
}
|
||||||
|
|
||||||
slots.push_back(Slot{std::move(item), 0});
|
slots.push_back(Slot{std::move(item), 0});
|
||||||
return HandleType{static_cast<u32>(slots.size() - 1), 0};
|
return Handle<T>{static_cast<u32>(slots.size() - 1), 0};
|
||||||
}
|
}
|
||||||
|
|
||||||
T& get(HandleType handle) {
|
T& get(Handle<T> handle) {
|
||||||
return const_cast<T&>(std::as_const(*this).get(handle));
|
return const_cast<T&>(std::as_const(*this).get(handle));
|
||||||
}
|
}
|
||||||
|
|
||||||
const T& get(HandleType handle) const { return *validate(handle).item; }
|
const T& get(Handle<T> handle) const { return *validate(handle).item; }
|
||||||
|
|
||||||
void release(HandleType handle) {
|
void release(Handle<T> handle) {
|
||||||
validate(handle);
|
validate(handle);
|
||||||
|
|
||||||
Slot& slot = slots[handle.index];
|
Slot& slot = slots[handle.index];
|
||||||
@@ -48,7 +49,7 @@ class Pool {
|
|||||||
freeList.push_back(handle.index);
|
freeList.push_back(handle.index);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isAlive(HandleType handle) const {
|
bool isAlive(Handle<T> handle) const {
|
||||||
return handle.isValid() && handle.index < slots.size() &&
|
return handle.isValid() && handle.index < slots.size() &&
|
||||||
slots[handle.index].generation == handle.generation &&
|
slots[handle.index].generation == handle.generation &&
|
||||||
slots[handle.index].item != nullptr;
|
slots[handle.index].item != nullptr;
|
||||||
@@ -98,7 +99,7 @@ class Pool {
|
|||||||
u32 generation = 0;
|
u32 generation = 0;
|
||||||
};
|
};
|
||||||
|
|
||||||
const Slot& validate(HandleType handle) const {
|
const Slot& validate(Handle<T> handle) const {
|
||||||
SOPHIA_ASSERT(handle.isValid(), "invalid handle:", name);
|
SOPHIA_ASSERT(handle.isValid(), "invalid handle:", name);
|
||||||
SOPHIA_ASSERT(handle.index < slots.size(), name,
|
SOPHIA_ASSERT(handle.index < slots.size(), name,
|
||||||
"handle index out of range:", handle.index);
|
"handle index out of range:", handle.index);
|
||||||
|
|||||||
@@ -16,8 +16,6 @@ namespace sophia {
|
|||||||
|
|
||||||
class Mesh {
|
class Mesh {
|
||||||
public:
|
public:
|
||||||
using HandleType = MeshHandle;
|
|
||||||
|
|
||||||
struct Vertex {
|
struct Vertex {
|
||||||
glm::vec2 position{};
|
glm::vec2 position{};
|
||||||
glm::vec3 color{};
|
glm::vec3 color{};
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ class RenderTargetPool {
|
|||||||
Device& device;
|
Device& device;
|
||||||
vk::Format format;
|
vk::Format format;
|
||||||
|
|
||||||
Pool<RenderTarget, RenderTargetHandle> pool{"RenderTarget"};
|
Pool<RenderTarget> pool{"RenderTarget"};
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace sophia
|
} // namespace sophia
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ class ResourceRegistry {
|
|||||||
: device{device}, renderTargetPool{device, renderTargetFormat} {}
|
: device{device}, renderTargetPool{device, renderTargetFormat} {}
|
||||||
|
|
||||||
Device& device;
|
Device& device;
|
||||||
Pool<Mesh, MeshHandle> meshPool{"Mesh"};
|
Pool<Mesh> meshPool{"Mesh"};
|
||||||
RenderTargetPool renderTargetPool;
|
RenderTargetPool renderTargetPool;
|
||||||
// TexturePool textures;
|
// TexturePool textures;
|
||||||
// MaterialPool materials;
|
// MaterialPool materials;
|
||||||
|
|||||||
Reference in New Issue
Block a user