Add offscreen render targets and rename AssetRegistry to ResourceRegistry #8
+1
-1
@@ -20,7 +20,7 @@ class Editor : public sophia::Application {
|
|||||||
panels().emplace<AssetBrowserPanel>();
|
panels().emplace<AssetBrowserPanel>();
|
||||||
sophia::RenderTargetHandle sceneTarget =
|
sophia::RenderTargetHandle sceneTarget =
|
||||||
resources().create<sophia::RenderTarget>();
|
resources().create<sophia::RenderTarget>();
|
||||||
setSceneTarget(sceneTarget);
|
views().create({.scene = scenes().create(), .target = sceneTarget});
|
||||||
panels().emplace<ViewportPanel>(ui(), sceneTarget);
|
panels().emplace<ViewportPanel>(ui(), sceneTarget);
|
||||||
|
|
||||||
panels().setDefaultLayout(
|
panels().setDefaultLayout(
|
||||||
|
|||||||
@@ -10,6 +10,7 @@
|
|||||||
#include <sophia/math.hpp>
|
#include <sophia/math.hpp>
|
||||||
#include <sophia/resource_registry_ref.hpp>
|
#include <sophia/resource_registry_ref.hpp>
|
||||||
#include <sophia/scene_registry.hpp>
|
#include <sophia/scene_registry.hpp>
|
||||||
|
#include <sophia/scene_view_registry.hpp>
|
||||||
#include <sophia/types.hpp>
|
#include <sophia/types.hpp>
|
||||||
#include <sophia/ui/debug_ui.hpp>
|
#include <sophia/ui/debug_ui.hpp>
|
||||||
#include <sophia/ui/panel_registry.hpp>
|
#include <sophia/ui/panel_registry.hpp>
|
||||||
@@ -46,6 +47,7 @@ class Application {
|
|||||||
ResourceRegistryRef resources();
|
ResourceRegistryRef resources();
|
||||||
PanelRegistry& panels() { return this->panelRegistry; }
|
PanelRegistry& panels() { return this->panelRegistry; }
|
||||||
SceneRegistry& scenes() { return this->sceneRegistry; }
|
SceneRegistry& scenes() { return this->sceneRegistry; }
|
||||||
|
SceneViewRegistry& views() { return this->sceneViewRegistry; }
|
||||||
UI& ui() { return this->_ui; }
|
UI& ui() { return this->_ui; }
|
||||||
|
|
||||||
DebugUI& debugUI() { return this->_debugUI; }
|
DebugUI& debugUI() { return this->_debugUI; }
|
||||||
@@ -58,16 +60,6 @@ class Application {
|
|||||||
|
|
||||||
void drawMesh(MeshHandle handle, const glm::mat4& transform);
|
void drawMesh(MeshHandle handle, const glm::mat4& transform);
|
||||||
|
|
||||||
/**
|
|
||||||
* !TEMP !TODO remove this func eventually
|
|
||||||
* Chooses where the scene is rendered. An invalid handle (the default) means
|
|
||||||
* the swapchain; a target created with
|
|
||||||
* 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.
|
|
||||||
*/
|
|
||||||
void setSceneTarget(RenderTargetHandle handle);
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
friend class Canvas;
|
friend class Canvas;
|
||||||
|
|
||||||
@@ -79,6 +71,7 @@ class Application {
|
|||||||
|
|
||||||
PanelRegistry panelRegistry;
|
PanelRegistry panelRegistry;
|
||||||
SceneRegistry sceneRegistry;
|
SceneRegistry sceneRegistry;
|
||||||
|
SceneViewRegistry sceneViewRegistry;
|
||||||
UI _ui;
|
UI _ui;
|
||||||
|
|
||||||
DebugUI _debugUI;
|
DebugUI _debugUI;
|
||||||
|
|||||||
@@ -37,10 +37,12 @@ class Mesh;
|
|||||||
class Panel;
|
class Panel;
|
||||||
class RenderTarget;
|
class RenderTarget;
|
||||||
class Scene;
|
class Scene;
|
||||||
|
struct SceneView;
|
||||||
|
|
||||||
using MeshHandle = Handle<Mesh>;
|
using MeshHandle = Handle<Mesh>;
|
||||||
using PanelHandle = Handle<Panel>;
|
using PanelHandle = Handle<Panel>;
|
||||||
using RenderTargetHandle = Handle<RenderTarget>;
|
using RenderTargetHandle = Handle<RenderTarget>;
|
||||||
using SceneHandle = Handle<Scene>;
|
using SceneHandle = Handle<Scene>;
|
||||||
|
using SceneViewHandle = Handle<SceneView>;
|
||||||
|
|
||||||
} // namespace sophia
|
} // namespace sophia
|
||||||
|
|||||||
@@ -0,0 +1,34 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <sophia/entity.hpp>
|
||||||
|
#include <sophia/handles.hpp>
|
||||||
|
#include <sophia/math.hpp>
|
||||||
|
#include <sophia/scene.hpp>
|
||||||
|
|
||||||
|
namespace sophia {
|
||||||
|
|
||||||
|
enum class ClearFlags : u8 {
|
||||||
|
None = 0,
|
||||||
|
Color = 1 << 0,
|
||||||
|
Depth = 1 << 1,
|
||||||
|
Stencil = 1 << 2,
|
||||||
|
All = Color | Depth | Stencil
|
||||||
|
};
|
||||||
|
|
||||||
|
struct SceneView {
|
||||||
|
SceneHandle scene = INVALID_HANDLE;
|
||||||
|
RenderTargetHandle target = INVALID_HANDLE;
|
||||||
|
Entity camera;
|
||||||
|
|
||||||
|
vec4 viewport{0.0f, 0.0f, 1.0f, 1.0f};
|
||||||
|
|
||||||
|
vec4 clearColor{0.f, 0.f, 0.f, 1.f};
|
||||||
|
ClearFlags clearFlags = ClearFlags::All;
|
||||||
|
|
||||||
|
u32 priority = 0; // Lower priority renders first
|
||||||
|
u32 layerMask = 0xFFFFFFFF;
|
||||||
|
|
||||||
|
bool enabled = true; // Disabled views are kept but not rendered
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace sophia
|
||||||
@@ -0,0 +1,86 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
|
#include <sophia/handles.hpp>
|
||||||
|
#include <sophia/scene_view.hpp>
|
||||||
|
#include <sophia/util/pool.hpp>
|
||||||
|
#include <utility>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
namespace sophia {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Every live SceneView. The application renders all enabled views each frame,
|
||||||
|
* in ascending priority order, before the UI.
|
||||||
|
*/
|
||||||
|
class SceneViewRegistry {
|
||||||
|
public:
|
||||||
|
SceneViewRegistry(const SceneViewRegistry&) = delete;
|
||||||
|
SceneViewRegistry& operator=(const SceneViewRegistry&) = delete;
|
||||||
|
|
||||||
|
SceneViewRegistry() = default;
|
||||||
|
~SceneViewRegistry() = default;
|
||||||
|
|
||||||
|
SceneViewHandle create(const SceneView& view = {}) {
|
||||||
|
isRenderQueueDirty = true;
|
||||||
|
return pool.create(view);
|
||||||
|
}
|
||||||
|
|
||||||
|
const SceneView& get(SceneViewHandle handle) const {
|
||||||
|
return pool.get(handle);
|
||||||
|
}
|
||||||
|
|
||||||
|
SceneView& get(SceneViewHandle handle) {
|
||||||
|
isRenderQueueDirty = true;
|
||||||
|
return pool.get(handle);
|
||||||
|
}
|
||||||
|
|
||||||
|
void release(SceneViewHandle handle) {
|
||||||
|
isRenderQueueDirty = true;
|
||||||
|
pool.release(handle);
|
||||||
|
}
|
||||||
|
|
||||||
|
bool isAlive(SceneViewHandle handle) const { return pool.isAlive(handle); }
|
||||||
|
|
||||||
|
u32 size() const { return pool.size(); }
|
||||||
|
|
||||||
|
template <typename Fn>
|
||||||
|
void forEach(Fn&& fn) {
|
||||||
|
isRenderQueueDirty = true;
|
||||||
|
pool.forEach(std::forward<Fn>(fn));
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename Fn>
|
||||||
|
void forEach(Fn&& fn) const {
|
||||||
|
pool.forEach(std::forward<Fn>(fn));
|
||||||
|
}
|
||||||
|
|
||||||
|
const std::vector<SceneViewHandle>& GetRenderQueue() const {
|
||||||
|
if (isRenderQueueDirty) {
|
||||||
|
rebuildRenderQueue();
|
||||||
|
}
|
||||||
|
return renderQueue;
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
void rebuildRenderQueue() const {
|
||||||
|
renderQueue.clear();
|
||||||
|
pool.forEachWithHandle([&](SceneViewHandle handle, const SceneView& view) {
|
||||||
|
if (view.enabled) {
|
||||||
|
renderQueue.push_back(handle);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
std::ranges::stable_sort(renderQueue, {}, [this](SceneViewHandle handle) {
|
||||||
|
return pool.get(handle).priority;
|
||||||
|
});
|
||||||
|
isRenderQueueDirty = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
Pool<SceneView> pool{"SceneView"};
|
||||||
|
|
||||||
|
mutable std::vector<SceneViewHandle> renderQueue;
|
||||||
|
mutable bool isRenderQueueDirty = true;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace sophia
|
||||||
+47
-62
@@ -1,9 +1,11 @@
|
|||||||
#include <imgui.h>
|
#include <imgui.h>
|
||||||
|
|
||||||
|
#include <algorithm>
|
||||||
#include <array>
|
#include <array>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <glm/glm.hpp>
|
#include <glm/glm.hpp>
|
||||||
|
#include <map>
|
||||||
#include <sophia/application.hpp>
|
#include <sophia/application.hpp>
|
||||||
#include <sophia/util/logger.hpp>
|
#include <sophia/util/logger.hpp>
|
||||||
#include <string>
|
#include <string>
|
||||||
@@ -15,6 +17,7 @@
|
|||||||
#include "rendering/model_render_system.hpp"
|
#include "rendering/model_render_system.hpp"
|
||||||
#include "rendering/render_context.hpp"
|
#include "rendering/render_context.hpp"
|
||||||
#include "rendering/render_graph.hpp"
|
#include "rendering/render_graph.hpp"
|
||||||
|
#include "rendering/scene_renderer.hpp"
|
||||||
#include "resources/mesh.hpp"
|
#include "resources/mesh.hpp"
|
||||||
#include "resources/render_target.hpp"
|
#include "resources/render_target.hpp"
|
||||||
#include "resources/resource_registry.hpp"
|
#include "resources/resource_registry.hpp"
|
||||||
@@ -43,35 +46,8 @@ std::vector<std::string> commandLineArgs;
|
|||||||
|
|
||||||
constexpr ClearColor CLEAR_COLOR = {0.01f, 0.01f, 0.02f, 1.0f};
|
constexpr ClearColor CLEAR_COLOR = {0.01f, 0.01f, 0.02f, 1.0f};
|
||||||
|
|
||||||
void beginColorPass(vk::CommandBuffer commandBuffer, vk::ImageView view,
|
bool hasFlag(ClearFlags flags, ClearFlags flag) {
|
||||||
vk::Extent2D extent) {
|
return (static_cast<u8>(flags) & static_cast<u8>(flag)) != 0;
|
||||||
vk::ClearValue clearValue{};
|
|
||||||
clearValue.color = vk::ClearColorValue{CLEAR_COLOR};
|
|
||||||
|
|
||||||
vk::RenderingAttachmentInfo colorAttachment{};
|
|
||||||
colorAttachment.imageView = view;
|
|
||||||
colorAttachment.imageLayout = vk::ImageLayout::eColorAttachmentOptimal;
|
|
||||||
colorAttachment.loadOp = vk::AttachmentLoadOp::eClear;
|
|
||||||
colorAttachment.storeOp = vk::AttachmentStoreOp::eStore;
|
|
||||||
colorAttachment.clearValue = clearValue;
|
|
||||||
|
|
||||||
vk::RenderingInfo renderingInfo{};
|
|
||||||
renderingInfo.renderArea = vk::Rect2D{{0, 0}, extent};
|
|
||||||
renderingInfo.layerCount = 1;
|
|
||||||
renderingInfo.colorAttachmentCount = 1;
|
|
||||||
renderingInfo.pColorAttachments = &colorAttachment;
|
|
||||||
|
|
||||||
commandBuffer.beginRendering(renderingInfo);
|
|
||||||
|
|
||||||
vk::Viewport viewport{0.0f,
|
|
||||||
0.0f,
|
|
||||||
static_cast<float>(extent.width),
|
|
||||||
static_cast<float>(extent.height),
|
|
||||||
0.0f,
|
|
||||||
1.0f};
|
|
||||||
vk::Rect2D scissor{{0, 0}, extent};
|
|
||||||
commandBuffer.setViewport(0, viewport);
|
|
||||||
commandBuffer.setScissor(0, scissor);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
@@ -88,6 +64,8 @@ class Application::Impl {
|
|||||||
|
|
||||||
void run() {
|
void run() {
|
||||||
ModelRenderSystem modelRenderSystem{device, renderContext};
|
ModelRenderSystem modelRenderSystem{device, renderContext};
|
||||||
|
SceneRenderer sceneRenderer{this->application.sceneRegistry,
|
||||||
|
this->resourceRegistry};
|
||||||
ImGuiRenderer imguiRenderer{window, device, renderContext.getImageFormat(),
|
ImGuiRenderer imguiRenderer{window, device, renderContext.getImageFormat(),
|
||||||
renderContext.getImageCount(),
|
renderContext.getImageCount(),
|
||||||
this->resourceRegistry};
|
this->resourceRegistry};
|
||||||
@@ -106,6 +84,7 @@ class Application::Impl {
|
|||||||
auto& renderTargetPool = this->resourceRegistry.renderTargetPool;
|
auto& renderTargetPool = this->resourceRegistry.renderTargetPool;
|
||||||
|
|
||||||
PanelRegistry& panelRegistry = this->application.panelRegistry;
|
PanelRegistry& panelRegistry = this->application.panelRegistry;
|
||||||
|
const SceneViewRegistry& sceneViews = this->application.sceneViewRegistry;
|
||||||
|
|
||||||
imguiRenderer.setDefaultLayout(panelRegistry.getDefaultLayout());
|
imguiRenderer.setDefaultLayout(panelRegistry.getDefaultLayout());
|
||||||
|
|
||||||
@@ -128,17 +107,10 @@ class Application::Impl {
|
|||||||
// frame's work (or ImGui's references to the targets) is recorded.
|
// frame's work (or ImGui's references to the targets) is recorded.
|
||||||
renderTargetPool.applyPendingResizes();
|
renderTargetPool.applyPendingResizes();
|
||||||
|
|
||||||
RenderTarget* sceneTarget =
|
|
||||||
this->sceneTargetHandle.isValid()
|
|
||||||
? &renderTargetPool.get(this->sceneTargetHandle)
|
|
||||||
: nullptr;
|
|
||||||
|
|
||||||
vk::Extent2D extent = renderContext.getExtent();
|
vk::Extent2D extent = renderContext.getExtent();
|
||||||
vk::Extent2D sceneExtent =
|
|
||||||
sceneTarget ? sceneTarget->getExtent() : extent;
|
|
||||||
FrameInfo frameInfo{frameIndex, elapsedTime, deltaTime,
|
FrameInfo frameInfo{frameIndex, elapsedTime, deltaTime,
|
||||||
glm::vec2(static_cast<float>(sceneExtent.width),
|
glm::vec2(static_cast<float>(extent.width),
|
||||||
static_cast<float>(sceneExtent.height)),
|
static_cast<float>(extent.height)),
|
||||||
this->application.camera};
|
this->application.camera};
|
||||||
this->application.onUpdate(frameInfo);
|
this->application.onUpdate(frameInfo);
|
||||||
this->application.camera.setProjection(
|
this->application.camera.setProjection(
|
||||||
@@ -166,31 +138,54 @@ class Application::Impl {
|
|||||||
this->drawCalls.end());
|
this->drawCalls.end());
|
||||||
this->drawCalls.clear();
|
this->drawCalls.clear();
|
||||||
|
|
||||||
if (sceneTarget) {
|
|
||||||
Image& targetImage = sceneTarget->getImage();
|
|
||||||
targetImage.transitionLayout(commandBuffer,
|
|
||||||
vk::ImageLayout::eColorAttachmentOptimal);
|
|
||||||
beginColorPass(commandBuffer, targetImage.getView(), sceneExtent);
|
|
||||||
modelRenderSystem.render(commandBuffer, instances, frameInfo);
|
|
||||||
commandBuffer.endRendering();
|
|
||||||
targetImage.transitionLayout(commandBuffer,
|
|
||||||
vk::ImageLayout::eShaderReadOnlyOptimal);
|
|
||||||
}
|
|
||||||
|
|
||||||
RenderGraph graph;
|
RenderGraph graph;
|
||||||
ImageRef backbuffer =
|
ImageRef backbuffer =
|
||||||
graph.importImage(renderContext.getCurrentImage(), Access::Present);
|
graph.importImage(renderContext.getCurrentImage(), Access::Present);
|
||||||
|
|
||||||
|
std::map<RenderTargetHandle, ImageRef> viewTargets;
|
||||||
|
for (SceneViewHandle handle : sceneViews.GetRenderQueue()) {
|
||||||
|
const SceneView& view = sceneViews.get(handle);
|
||||||
|
if (!this->application.sceneRegistry.isAlive(view.scene) ||
|
||||||
|
!renderTargetPool.isAlive(view.target)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto [it, inserted] = viewTargets.try_emplace(view.target);
|
||||||
|
if (inserted) {
|
||||||
|
it->second =
|
||||||
|
graph.importImage(renderTargetPool.get(view.target).getImage(),
|
||||||
|
Access::SampledRead);
|
||||||
|
}
|
||||||
|
|
||||||
|
const vec4& color = view.clearColor;
|
||||||
|
graph.addPass(
|
||||||
|
"scene view",
|
||||||
|
[&](RenderGraph::PassBuilder& pass) {
|
||||||
|
pass.writeColor(it->second,
|
||||||
|
hasFlag(view.clearFlags, ClearFlags::Color)
|
||||||
|
? vk::AttachmentLoadOp::eClear
|
||||||
|
: vk::AttachmentLoadOp::eLoad,
|
||||||
|
{color.r, color.g, color.b, color.a});
|
||||||
|
},
|
||||||
|
[&sceneRenderer, &sceneViews, handle](vk::CommandBuffer) {
|
||||||
|
const SceneView& view = sceneViews.get(handle);
|
||||||
|
if (view.camera) {
|
||||||
|
sceneRenderer.render(view);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
graph.addPass(
|
graph.addPass(
|
||||||
"main",
|
"main",
|
||||||
[&](RenderGraph::PassBuilder& pass) {
|
[&](RenderGraph::PassBuilder& pass) {
|
||||||
pass.writeColor(backbuffer, vk::AttachmentLoadOp::eClear,
|
pass.writeColor(backbuffer, vk::AttachmentLoadOp::eClear,
|
||||||
CLEAR_COLOR);
|
CLEAR_COLOR);
|
||||||
|
for (const auto& [handle, target] : viewTargets) {
|
||||||
|
pass.sample(target);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
[&](vk::CommandBuffer commandBuffer) {
|
[&](vk::CommandBuffer commandBuffer) {
|
||||||
if (!sceneTarget) {
|
modelRenderSystem.render(commandBuffer, instances, frameInfo);
|
||||||
modelRenderSystem.render(commandBuffer, instances, frameInfo);
|
|
||||||
}
|
|
||||||
|
|
||||||
imguiRenderer.endFrame(commandBuffer);
|
imguiRenderer.endFrame(commandBuffer);
|
||||||
});
|
});
|
||||||
@@ -237,10 +232,6 @@ class Application::Impl {
|
|||||||
this->drawCalls.push_back({&mesh, transform});
|
this->drawCalls.push_back({&mesh, transform});
|
||||||
}
|
}
|
||||||
|
|
||||||
void setSceneTarget(RenderTargetHandle handle) {
|
|
||||||
this->sceneTargetHandle = handle;
|
|
||||||
}
|
|
||||||
|
|
||||||
ResourceRegistry& getResourceRegistry() { return resourceRegistry; }
|
ResourceRegistry& getResourceRegistry() { return resourceRegistry; }
|
||||||
|
|
||||||
private:
|
private:
|
||||||
@@ -253,8 +244,6 @@ class Application::Impl {
|
|||||||
|
|
||||||
std::vector<std::unique_ptr<Mesh>> meshes;
|
std::vector<std::unique_ptr<Mesh>> meshes;
|
||||||
std::vector<ModelRenderSystem::MeshInstance> drawCalls;
|
std::vector<ModelRenderSystem::MeshInstance> drawCalls;
|
||||||
|
|
||||||
RenderTargetHandle sceneTargetHandle = RenderTargetHandle::invalid();
|
|
||||||
};
|
};
|
||||||
|
|
||||||
void Application::setCommandLineArgs(int argc, const char* const* argv) {
|
void Application::setCommandLineArgs(int argc, const char* const* argv) {
|
||||||
@@ -281,10 +270,6 @@ void Application::pushMesh(std::vector<CanvasVertex> vertices,
|
|||||||
impl->pushMesh(std::move(vertices), std::move(indices));
|
impl->pushMesh(std::move(vertices), std::move(indices));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Application::setSceneTarget(RenderTargetHandle handle) {
|
|
||||||
impl->setSceneTarget(handle);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Application::clearMeshes() { impl->clearMeshes(); }
|
void Application::clearMeshes() { impl->clearMeshes(); }
|
||||||
|
|
||||||
void Application::drawMesh(MeshHandle handle, const glm::mat4& transform) {
|
void Application::drawMesh(MeshHandle handle, const glm::mat4& transform) {
|
||||||
|
|||||||
Reference in New Issue
Block a user