Upgrading pool pattern

This commit is contained in:
2026-09-20 20:52:09 -07:00
parent fe40d4a629
commit 16f7bb7da3
18 changed files with 268 additions and 332 deletions
+3 -4
View File
@@ -7,7 +7,7 @@
#include <sophia/canvas.hpp>
#include <sophia/console/console_command_registry.hpp>
#include <sophia/frame_info.hpp>
#include <sophia/resource_registry.hpp>
#include <sophia/resource_registry_ref.hpp>
#include <sophia/types.hpp>
#include <sophia/ui/debug_ui.hpp>
#include <sophia/ui/panel_registry.hpp>
@@ -45,7 +45,7 @@ class Application {
virtual void onRender() {}
virtual void onDestroy() {}
ResourceRegistry& resources() { return this->resourceRegistry; }
ResourceRegistryRef resources();
PanelRegistry& panels() { return this->panelRegistry; }
UI& ui() { return this->_ui; }
@@ -62,7 +62,7 @@ class Application {
/**
* Chooses where the scene is rendered. An invalid handle (the default) means
* the swapchain; a target created with
* ResourceRegistry::create<RenderTarget>() redirects it offscreen, and the
* ResourceRegistryRef::create<RenderTarget>() redirects it offscreen, and the
* swapchain then only receives the UI. FrameInfo::currentFramebufferExtent
* and the camera projection follow the target's size.
*/
@@ -77,7 +77,6 @@ class Application {
class Impl;
std::unique_ptr<Impl> impl;
ResourceRegistry resourceRegistry;
PanelRegistry panelRegistry;
UI _ui;
+1 -1
View File
@@ -1,6 +1,6 @@
#pragma once
#include <sophia/resource_registry.hpp>
#include <sophia/handles.hpp>
#include <string>
#define GLM_FORCE_RADIANS
+2
View File
@@ -27,9 +27,11 @@ struct Handle {
struct MeshTag {};
struct PanelTag {};
struct RenderTargetTag {};
struct SceneTag {};
using MeshHandle = Handle<MeshTag>;
using PanelHandle = Handle<PanelTag>;
using RenderTargetHandle = Handle<RenderTargetTag>;
using SceneHandle = Handle<SceneTag>;
} // namespace sophia
@@ -1,25 +1,23 @@
#pragma once
#include <memory>
#include <sophia/canvas.hpp>
#include <sophia/handles.hpp>
#include <sophia/types.hpp>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>
#include "sophia/canvas.hpp"
#include "sophia/types.hpp"
#define GLM_FORCE_RADIANS
#define GLM_FORCE_DEPTH_ZERO_TO_ONE
#include <glm/glm.hpp>
namespace sophia {
class Device;
class Mesh;
class RenderTarget;
class ImGuiRenderer;
class Scene;
class ResourceRegistry;
template <typename T>
struct ResourceTraits;
@@ -34,13 +32,13 @@ struct ResourceTraits<RenderTarget> {
using Handle = RenderTargetHandle;
};
class ResourceRegistry {
template <>
struct ResourceTraits<Scene> {
using Handle = SceneHandle;
};
class ResourceRegistryRef {
public:
~ResourceRegistry();
ResourceRegistry(const ResourceRegistry&) = delete;
ResourceRegistry& operator=(const ResourceRegistry&) = delete;
template <typename T>
typename ResourceTraits<T>::Handle load(const std::string& path);
@@ -54,21 +52,16 @@ class ResourceRegistry {
glm::vec2 getExtent(RenderTargetHandle handle) const;
bool isAlive(RenderTargetHandle handle) const;
bool isAlive(SceneHandle handle) const;
/**
* Waits for the GPU to go idle, so don't call this every frame. Don't release
* the target the scene is currently rendering into.
*/
template <typename T>
void release(typename ResourceTraits<T>::Handle handle);
private:
friend class Application;
friend class ImGuiRenderer;
// `renderTargetFormat` is a VkFormat value; it must match the color format
// the scene pipelines are built against.
ResourceRegistry(Device& device, u32 renderTargetFormat);
explicit ResourceRegistryRef(ResourceRegistry& registry)
: registry{&registry} {}
MeshHandle createResource(std::type_identity<Mesh>,
std::vector<CanvasVertex> vertices,
@@ -77,27 +70,21 @@ class ResourceRegistry {
RenderTargetHandle createResource(std::type_identity<RenderTarget>,
u32 width = 1, u32 height = 1);
RenderTarget& getRenderTarget(RenderTargetHandle handle);
const RenderTarget& getRenderTarget(RenderTargetHandle handle) const;
void applyPendingResizes();
SceneHandle createResource(std::type_identity<Scene>);
template <typename T>
const T& get(typename ResourceTraits<T>::Handle handle) const;
class Impl;
std::unique_ptr<Impl> impl;
ResourceRegistry* registry;
};
template <>
MeshHandle ResourceRegistry::load<Mesh>(const std::string& path);
MeshHandle ResourceRegistryRef::load<Mesh>(const std::string& path);
template <>
void ResourceRegistry::release<Mesh>(MeshHandle handle);
void ResourceRegistryRef::release<Mesh>(MeshHandle handle);
template <>
void ResourceRegistry::release<RenderTarget>(RenderTargetHandle handle);
void ResourceRegistryRef::release<RenderTarget>(RenderTargetHandle handle);
template <>
const Mesh& ResourceRegistry::get<Mesh>(MeshHandle handle) const;
void ResourceRegistryRef::release<Scene>(SceneHandle handle);
} // namespace sophia
+3 -3
View File
@@ -3,11 +3,11 @@
#include <imgui.h>
#include <sophia/handles.hpp>
#include <sophia/resource_registry_ref.hpp>
namespace sophia {
class ImGuiRenderer;
class ResourceRegistry;
class UI {
public:
@@ -31,9 +31,9 @@ class UI {
private:
friend class Application;
explicit UI(ResourceRegistry& resources) : resources{resources} {}
explicit UI(ResourceRegistryRef resources) : resources{resources} {}
ResourceRegistry& resources;
ResourceRegistryRef resources;
ImGuiRenderer* renderer = nullptr;
};