Big changes
This commit is contained in:
@@ -1,90 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <sophia/types.hpp>
|
||||
#include <sophia/util/assert.hpp>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
namespace sophia {
|
||||
|
||||
template <typename T, typename HandleT>
|
||||
class Pool {
|
||||
public:
|
||||
explicit Pool(const char* name) : name{name} {}
|
||||
|
||||
Pool(const Pool&) = delete;
|
||||
Pool& operator=(const Pool&) = delete;
|
||||
|
||||
template <typename... Args>
|
||||
HandleT create(Args&&... args) {
|
||||
auto item = std::make_unique<T>(std::forward<Args>(args)...);
|
||||
|
||||
if (!freeList.empty()) {
|
||||
u32 index = freeList.back();
|
||||
freeList.pop_back();
|
||||
slots[index].item = std::move(item);
|
||||
return HandleT{index, slots[index].generation};
|
||||
}
|
||||
|
||||
slots.push_back(Slot{std::move(item), 0});
|
||||
return HandleT{static_cast<u32>(slots.size() - 1), 0};
|
||||
}
|
||||
|
||||
T& get(HandleT handle) {
|
||||
return const_cast<T&>(std::as_const(*this).get(handle));
|
||||
}
|
||||
|
||||
const T& get(HandleT handle) const { return *validate(handle).item; }
|
||||
|
||||
void release(HandleT handle) {
|
||||
validate(handle);
|
||||
|
||||
Slot& slot = slots[handle.index];
|
||||
slot.item.reset();
|
||||
slot.generation = (slot.generation + 1) & GENERATION_MASK;
|
||||
freeList.push_back(handle.index);
|
||||
}
|
||||
|
||||
bool isAlive(HandleT handle) const {
|
||||
return handle.isValid() && handle.index < slots.size() &&
|
||||
slots[handle.index].generation == handle.generation &&
|
||||
slots[handle.index].item != nullptr;
|
||||
}
|
||||
|
||||
template <typename Fn>
|
||||
void forEach(Fn&& fn) {
|
||||
for (Slot& slot : slots) {
|
||||
if (slot.item) {
|
||||
fn(*slot.item);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
static constexpr u32 GENERATION_MASK = (1u << 12) - 1;
|
||||
|
||||
struct Slot {
|
||||
std::unique_ptr<T> item;
|
||||
u32 generation = 0;
|
||||
};
|
||||
|
||||
const Slot& validate(HandleT handle) const {
|
||||
SOPHIA_ASSERT(handle.isValid(), "invalid handle:", name);
|
||||
SOPHIA_ASSERT(handle.index < slots.size(), name,
|
||||
"handle index out of range:", handle.index);
|
||||
|
||||
const Slot& slot = slots[handle.index];
|
||||
SOPHIA_ASSERT(slot.generation == handle.generation, "stale", name,
|
||||
"handle (generation mismatch):", handle.index);
|
||||
SOPHIA_ASSERT(slot.item != nullptr, name,
|
||||
"handle refers to a released item:", handle.index);
|
||||
return slot;
|
||||
}
|
||||
|
||||
const char* name;
|
||||
std::vector<Slot> slots;
|
||||
std::vector<u32> freeList;
|
||||
};
|
||||
|
||||
} // namespace sophia
|
||||
@@ -1,10 +1,10 @@
|
||||
#pragma once
|
||||
|
||||
#include <sophia/handles.hpp>
|
||||
#include <sophia/util/pool.hpp>
|
||||
|
||||
#include "device.hpp"
|
||||
#include "render_target.hpp"
|
||||
#include "resources/pools/pool.hpp"
|
||||
|
||||
namespace sophia {
|
||||
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#include <sophia/handles.hpp>
|
||||
#include <sophia/util/pool.hpp>
|
||||
|
||||
#include "device.hpp"
|
||||
#include "mesh.hpp"
|
||||
#include "resources/pools/pool.hpp"
|
||||
#include "resources/pools/render_target_pool.hpp"
|
||||
#include "scene.hpp"
|
||||
|
||||
namespace sophia {
|
||||
|
||||
@@ -21,7 +20,6 @@ class ResourceRegistry {
|
||||
Device& device;
|
||||
Pool<Mesh, MeshHandle> meshPool{"Mesh"};
|
||||
RenderTargetPool renderTargetPool;
|
||||
Pool<Scene, SceneHandle> scenePool{"Scene"};
|
||||
// TexturePool textures;
|
||||
// MaterialPool materials;
|
||||
};
|
||||
|
||||
@@ -24,11 +24,6 @@ void ResourceRegistryRef::release<RenderTarget>(RenderTargetHandle handle) {
|
||||
registry->renderTargetPool.release(handle);
|
||||
}
|
||||
|
||||
template <>
|
||||
void ResourceRegistryRef::release<Scene>(SceneHandle handle) {
|
||||
registry->scenePool.release(handle);
|
||||
}
|
||||
|
||||
MeshHandle ResourceRegistryRef::createResource(
|
||||
std::type_identity<Mesh>, std::vector<CanvasVertex> vertices,
|
||||
std::vector<u32> indices) {
|
||||
@@ -47,10 +42,6 @@ RenderTargetHandle ResourceRegistryRef::createResource(
|
||||
return registry->renderTargetPool.create(width, height);
|
||||
}
|
||||
|
||||
SceneHandle ResourceRegistryRef::createResource(std::type_identity<Scene>) {
|
||||
return registry->scenePool.create();
|
||||
}
|
||||
|
||||
void ResourceRegistryRef::requestResize(RenderTargetHandle handle, u32 width,
|
||||
u32 height) {
|
||||
registry->renderTargetPool.get(handle).requestResize(width, height);
|
||||
@@ -65,8 +56,4 @@ bool ResourceRegistryRef::isAlive(RenderTargetHandle handle) const {
|
||||
return registry->renderTargetPool.isAlive(handle);
|
||||
}
|
||||
|
||||
bool ResourceRegistryRef::isAlive(SceneHandle handle) const {
|
||||
return registry->scenePool.isAlive(handle);
|
||||
}
|
||||
|
||||
} // namespace sophia
|
||||
|
||||
Reference in New Issue
Block a user