Big changes

This commit is contained in:
2026-09-21 00:26:50 -07:00
parent 16f7bb7da3
commit f3c0f61eb1
16 changed files with 170 additions and 187 deletions
+3 -2
View File
@@ -5,6 +5,8 @@
#include <sophia/util/logger.hpp>
#include <stdexcept>
#include "rendering/access.hpp"
namespace sophia {
RenderContext::RenderContext(Window& window, Device& device)
@@ -80,8 +82,7 @@ void RenderContext::endFrame() {
vk::Semaphore waitSemaphore = frame.imageAvailable.get();
vk::Semaphore signalSemaphore =
this->renderFinishedSemaphores.at(this->currentImageIndex).get();
vk::PipelineStageFlags waitStage =
vk::PipelineStageFlagBits::eColorAttachmentOutput;
vk::PipelineStageFlags waitStage = SWAPCHAIN_ACQUIRE_STAGE;
vk::CommandBuffer commandBuffer = frame.commandBuffer;
vk::SubmitInfo submitInfo{};
+2 -8
View File
@@ -31,14 +31,8 @@ class RenderContext {
vk::CommandBuffer beginFrame();
void endFrame();
void transitionCurrentImage(vk::CommandBuffer commandBuffer,
vk::ImageLayout newLayout) const {
this->swapchain->getImage(static_cast<int>(this->currentImageIndex))
.transitionLayout(commandBuffer, newLayout);
}
vk::ImageView getCurrentImageView() const {
return this->swapchain->getImageView(
static_cast<int>(this->currentImageIndex));
Image& getCurrentImage() const {
return this->swapchain->getImage(static_cast<int>(this->currentImageIndex));
}
vk::Extent2D getExtent() const { return this->swapchain->getExtent(); }
-90
View File
@@ -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 -3
View File
@@ -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
+1 -2
View File
@@ -1,6 +1,5 @@
#include "scene.hpp"
#include <sophia/components.hpp>
#include <sophia/scene.hpp>
#include <sophia/util/logger.hpp>
namespace sophia {
-16
View File
@@ -1,16 +0,0 @@
#pragma once
#include <entt/entt.hpp>
namespace sophia {
class Scene {
public:
Scene();
~Scene();
private:
entt::registry registry;
};
} // namespace sophia
-3
View File
@@ -23,9 +23,6 @@ class Swapchain {
Image& getImage(int index) const { return *this->images.at(index); }
Image& getDepthImage(int index) const { return *this->depthImages.at(index); }
vk::ImageView getImageView(int index) const {
return this->images.at(index)->getView();
}
vk::ImageView getDepthImageView(int index) const {
return this->depthImages.at(index)->getView();
}
+7 -72
View File
@@ -1,88 +1,23 @@
#include <sophia/ui/panel_registry.hpp>
#include <sophia/util/assert.hpp>
namespace sophia {
namespace {
constexpr u32 GENERATION_MASK = (1u << 12) - 1;
} // namespace
PanelHandle PanelRegistry::add(std::unique_ptr<Panel> panel) {
if (!freeList.empty()) {
u32 index = freeList.back();
freeList.pop_back();
slots[index].panel = std::move(panel);
return PanelHandle{index, slots[index].generation};
}
slots.push_back(Slot{std::move(panel), 0});
return PanelHandle{static_cast<u32>(slots.size() - 1), 0};
}
const Panel& PanelRegistry::get(PanelHandle handle) const {
SOPHIA_ASSERT(handle.isValid(), "invalid PanelHandle");
SOPHIA_ASSERT(handle.index < slots.size(),
"PanelHandle index out of range:", handle.index);
const Slot& slot = slots[handle.index];
SOPHIA_ASSERT(slot.generation == handle.generation,
"stale PanelHandle (generation mismatch):", handle.index);
SOPHIA_ASSERT(slot.panel != nullptr,
"PanelHandle refers to a removed panel:", handle.index);
return *slot.panel;
}
void PanelRegistry::remove(PanelHandle handle) {
SOPHIA_ASSERT(handle.isValid(), "invalid PanelHandle");
SOPHIA_ASSERT(handle.index < slots.size(),
"PanelHandle index out of range:", handle.index);
Slot& slot = slots[handle.index];
SOPHIA_ASSERT(slot.generation == handle.generation,
"stale PanelHandle (generation mismatch):", handle.index);
SOPHIA_ASSERT(slot.panel != nullptr,
"PanelHandle already removed:", handle.index);
slot.panel.reset();
slot.generation = (slot.generation + 1) & GENERATION_MASK;
freeList.push_back(handle.index);
}
bool PanelRegistry::isValid(PanelHandle handle) const {
if (!handle.isValid() || handle.index >= slots.size()) {
return false;
}
const Slot& slot = slots[handle.index];
return slot.generation == handle.generation && slot.panel != nullptr;
}
u32 PanelRegistry::size() const {
u32 count = 0;
for (const Slot& slot : slots) {
if (slot.panel) {
count++;
}
}
return count;
}
u32 PanelRegistry::visibleCount() const {
u32 count = 0;
for (const Slot& slot : slots) {
if (slot.panel && slot.panel->isVisible()) {
pool.forEach([&count](const Panel& panel) {
if (panel.isVisible()) {
count++;
}
}
});
return count;
}
void PanelRegistry::update(const FrameInfo& frameInfo) {
for (auto& slot : slots) {
if (slot.panel && slot.panel->isVisible()) {
slot.panel->draw(frameInfo);
pool.forEach([&frameInfo](Panel& panel) {
if (panel.isVisible()) {
panel.draw(frameInfo);
}
}
});
}
} // namespace sophia