Skip to content

Commit

Permalink
Zephyr: Renderer: introduce intermediate game thread render objects
Browse files Browse the repository at this point in the history
  • Loading branch information
fleroviux committed May 1, 2024
1 parent 54f990b commit c808210
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 10 deletions.
8 changes: 8 additions & 0 deletions zephyr/renderer/include/zephyr/renderer/render_engine.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <zephyr/math/matrix4.hpp>
#include <zephyr/renderer/backend/render_backend.hpp>
#include <zephyr/renderer/resource/geometry.hpp>
#include <zephyr/scene/node.hpp>
#include <atomic>
#include <semaphore>
Expand All @@ -22,6 +23,7 @@ namespace zephyr {
void CreateRenderThread();
void JoinRenderThread();
void RenderThreadMain();
void ReadyRenderThreadData();

std::shared_ptr<RenderBackend> m_render_backend;

Expand All @@ -31,6 +33,12 @@ 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

struct GameThreadRenderObject {
Matrix4 local_to_world;
Geometry* geometry;
};
std::vector<GameThreadRenderObject> m_game_thread_render_objects;

std::vector<RenderObject> m_render_objects;
};

Expand Down
33 changes: 23 additions & 10 deletions zephyr/renderer/src/render_engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ namespace zephyr {
// Wait for the render thread to complete reading the internal render structures.
m_render_thread_semaphore.acquire();

m_render_objects.clear();
m_game_thread_render_objects.clear();

scene_root->Traverse([&](SceneNode* node) -> bool {
if(!node->IsVisible()) return false;
Expand All @@ -28,8 +28,9 @@ namespace zephyr {
const auto& geometry = mesh_component.geometry;

if(geometry) {
m_render_objects.push_back({
.local_to_world = node->GetTransform().GetWorld()
m_game_thread_render_objects.push_back({
.local_to_world = node->GetTransform().GetWorld(),
.geometry = geometry.get()
});
}
}
Expand Down Expand Up @@ -59,21 +60,33 @@ namespace zephyr {
m_render_backend->InitializeContext();

while(m_render_thread_running) {
// Wait for the caller thread to prepare the internal render structures for the next frame.
m_render_thread_is_waiting = true;
m_caller_thread_semaphore.acquire();
m_render_thread_is_waiting = false;
ReadyRenderThreadData();

// TODO(fleroviux): do not hardcode the aspect ratio.
const Matrix4 projection = Matrix4::PerspectiveVK(45.0f, 16.0f/9.0, 0.01f, 100.0f);
m_render_backend->Render(projection, m_render_objects);
m_render_backend->SwapBuffers();

// Signal to the caller thread that we are done reading the internal render structures.
m_render_thread_semaphore.release();
}

m_render_backend->DestroyContext();
}

void RenderEngine::ReadyRenderThreadData() {
// Wait for the caller thread to prepare the internal render structures for the next frame.
m_render_thread_is_waiting = true;
m_caller_thread_semaphore.acquire();
m_render_thread_is_waiting = false;

m_render_objects.clear();

for(const auto& game_thread_render_object : m_game_thread_render_objects) {
m_render_objects.push_back({
.local_to_world = game_thread_render_object.local_to_world
});
}

// Signal to the caller thread that we are done reading the internal render structures.
m_render_thread_semaphore.release();
}

} // namespace zephyr

0 comments on commit c808210

Please sign in to comment.