Skip to content

Commit

Permalink
Zephyr: Renderer: move geometry cache fully into RenderScene
Browse files Browse the repository at this point in the history
  • Loading branch information
fleroviux committed Jun 8, 2024
1 parent b9668fc commit ea5e1ae
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 40 deletions.
12 changes: 1 addition & 11 deletions zephyr/renderer/include/zephyr/renderer/render_engine.hpp
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@

#pragma once

#include <zephyr/math/matrix4.hpp>
#include <zephyr/renderer/backend/render_backend.hpp>
#include <zephyr/renderer/engine/geometry_cache.hpp>
#include <zephyr/renderer/resource/geometry.hpp>
#include <zephyr/renderer/render_scene.hpp>
#include <zephyr/scene/scene_graph.hpp>
#include <zephyr/scene/scene_node.hpp>
#include <EASTL/hash_map.h>
#include <atomic>
#include <optional>
#include <semaphore>
#include <thread>
#include <typeindex>
#include <vector>

namespace zephyr {

Expand All @@ -40,9 +32,7 @@ namespace zephyr {
std::binary_semaphore m_caller_thread_semaphore{0}; //< Semaphore signalled by the calling thread
std::binary_semaphore m_render_thread_semaphore{1}; //< Semaphore signalled by the rendering thread

GeometryCache m_geometry_cache;

RenderScene m_render_scene{}; //< Representation of the scene graph that is internal to the render engine.
RenderScene m_render_scene; //< Representation of the scene graph that is internal to the render engine.
RenderCamera m_render_camera{};
};

Expand Down
18 changes: 10 additions & 8 deletions zephyr/renderer/include/zephyr/renderer/render_scene.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ namespace zephyr {

class RenderScene {
public:
void SetSceneGraph(std::shared_ptr<SceneGraph> scene_graph);
void Update();
void GetRenderCamera(RenderCamera& out_render_camera);
void UpdateGeometries(GeometryCache& geometry_cache);
explicit RenderScene(std::shared_ptr<RenderBackend> render_backend);

void UpdateRenderBundles(const GeometryCache& geometry_cache);
// Game Thread API:
void SetSceneGraph(std::shared_ptr<SceneGraph> scene_graph);
void UpdateStage1();

[[nodiscard]] const eastl::hash_map<RenderBackend::RenderBundleKey, std::vector<RenderBackend::RenderBundleItem>>& GetRenderBundles() {
return m_render_bundles;
}
// Render Thread API:
void UpdateStage2();
void GetRenderCamera(RenderCamera& out_render_camera);
[[nodiscard]] const eastl::hash_map<RenderBackend::RenderBundleKey, std::vector<RenderBackend::RenderBundleItem>>& GetRenderBundles();

private:
using Entity = u32;
Expand Down Expand Up @@ -80,6 +80,8 @@ namespace zephyr {
void DestroyEntity(EntityID entity_id);
void ResizeComponentStorage(size_t capacity);

GeometryCache m_geometry_cache;

std::shared_ptr<SceneGraph> m_current_scene_graph{};
eastl::hash_map<const SceneNode*, EntityID> m_node_entity_map{};
eastl::hash_set<const Geometry*> m_active_geometry_set{};
Expand Down
20 changes: 6 additions & 14 deletions zephyr/renderer/src/render_engine.cpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@

#include <zephyr/renderer/component/camera.hpp>
#include <zephyr/renderer/component/mesh.hpp>
#include <zephyr/renderer/render_engine.hpp>
#include <fmt/format.h>

namespace zephyr {

RenderEngine::RenderEngine(std::unique_ptr<RenderBackend> render_backend)
: m_render_backend{std::move(render_backend)}
, m_geometry_cache{m_render_backend} {
, m_render_scene{m_render_backend} {
CreateRenderThread();
}

Expand All @@ -24,14 +21,8 @@ namespace zephyr {
// Wait for the render thread to complete reading the internal render structures.
m_render_thread_semaphore.acquire();

// Instruct the geometry cache to evict geometries which had been deleted in the submitted frame.
m_geometry_cache.CommitPendingDeleteTaskList();

// Update the internal scene graph representation of the render engine based on changes in the scene graph.
m_render_scene.Update();

// Update all geometries which might be rendered in this frame.
m_render_scene.UpdateGeometries(m_geometry_cache);
// Update the GPU scene based on changes in the scene graph (stage 1)
m_render_scene.UpdateStage1();

// Signal to the render thread that the next frame is ready
m_caller_thread_semaphore.release();
Expand Down Expand Up @@ -71,8 +62,9 @@ namespace zephyr {
m_caller_thread_semaphore.acquire();
m_render_thread_is_waiting = false;

m_geometry_cache.ProcessPendingUpdates();
m_render_scene.UpdateRenderBundles(m_geometry_cache);
// Update the GPU scene based on changes in the scene graph (stage 2)
m_render_scene.UpdateStage2();

m_render_scene.GetRenderCamera(m_render_camera);

// Signal to the caller thread that we are done reading the internal render structures.
Expand Down
26 changes: 19 additions & 7 deletions zephyr/renderer/src/render_scene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,32 @@

namespace zephyr {

RenderScene::RenderScene(std::shared_ptr<RenderBackend> render_backend)
: m_geometry_cache{std::move(render_backend)} {
}

void RenderScene::SetSceneGraph(std::shared_ptr<SceneGraph> scene_graph) {
if(m_current_scene_graph != scene_graph) {
m_current_scene_graph = std::move(scene_graph);
m_require_full_rebuild = true;
}
}

void RenderScene::Update() {
void RenderScene::UpdateStage1() {
if(m_require_full_rebuild) {
RebuildScene();
m_require_full_rebuild = false;
} else {
PatchScene();
}

// Instruct the geometry cache to evict geometries which had been deleted in the submitted frame.
m_geometry_cache.CommitPendingDeleteTaskList();

// Update all geometries which might be rendered in this frame.
for(const Geometry* geometry : m_active_geometry_set) {
m_geometry_cache.UpdateGeometry(geometry);
}
}

void RenderScene::GetRenderCamera(RenderCamera& out_render_camera) {
Expand All @@ -37,21 +49,21 @@ namespace zephyr {
out_render_camera.view = entity_transform.local_to_world.Inverse();
}

void RenderScene::UpdateGeometries(GeometryCache& geometry_cache) {
for(const Geometry* geometry : m_active_geometry_set) {
geometry_cache.UpdateGeometry(geometry);
}
[[nodiscard]] const eastl::hash_map<RenderBackend::RenderBundleKey, std::vector<RenderBackend::RenderBundleItem>>& RenderScene::GetRenderBundles() {
return m_render_bundles;
}

void RenderScene::UpdateRenderBundles(const GeometryCache& geometry_cache) {
void RenderScene::UpdateStage2() {
m_geometry_cache.ProcessPendingUpdates();

for(const RenderScenePatch& render_scene_patch : m_render_scene_patches) {
switch(render_scene_patch.type) {
case RenderScenePatch::Type::MeshMounted: {
const Transform& entity_transform = m_components_transform[render_scene_patch.entity_id];
const Mesh& entity_mesh = m_components_mesh[render_scene_patch.entity_id];

// TODO(fleroviux): get rid of unsafe size_t to u32 conversion.
const RenderGeometry* const render_geometry = geometry_cache.GetCachedRenderGeometry(entity_mesh.geometry);
const RenderGeometry* const render_geometry = m_geometry_cache.GetCachedRenderGeometry(entity_mesh.geometry);
RenderBackend::RenderBundleKey render_bundle_key{};
render_bundle_key.uses_ibo = render_geometry->GetNumberOfIndices();
render_bundle_key.geometry_layout = render_geometry->GetLayout().key;
Expand Down

0 comments on commit ea5e1ae

Please sign in to comment.