-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Begin separate render thread implementation
- Loading branch information
Showing
8 changed files
with
128 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
|
||
#pragma once | ||
|
||
#include <zephyr/math/matrix4.hpp> | ||
#include <zephyr/scene/node.hpp> | ||
#include <zephyr/scene/mesh.hpp> | ||
#include <atomic> | ||
#include <semaphore> | ||
#include <thread> | ||
#include <vector> | ||
|
||
namespace zephyr { | ||
|
||
struct RenderObject { | ||
Matrix4 local_to_world_transform; | ||
}; | ||
|
||
class RenderEngine { | ||
public: | ||
RenderEngine(); | ||
~RenderEngine(); | ||
|
||
void RenderScene(SceneNode* scene_root); | ||
|
||
private: | ||
void CreateRenderThread(); | ||
void JoinRenderThread(); | ||
void RenderThreadMain(); | ||
|
||
std::thread m_render_thread; | ||
std::atomic_bool m_render_thread_running; | ||
std::atomic_bool m_render_thread_is_waiting; | ||
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 | ||
|
||
std::vector<RenderObject> m_render_objects; | ||
}; | ||
|
||
} // namespace zephyr |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
|
||
#include <zephyr/renderer/render_engine.hpp> | ||
#include <fmt/format.h> | ||
|
||
namespace zephyr { | ||
|
||
RenderEngine::RenderEngine() { | ||
CreateRenderThread(); | ||
} | ||
|
||
RenderEngine::~RenderEngine() { | ||
JoinRenderThread(); | ||
} | ||
|
||
void RenderEngine::RenderScene(SceneNode* scene_root) { | ||
// Wait for the render thread to complete reading the internal render structures. | ||
m_render_thread_semaphore.acquire(); | ||
|
||
m_render_objects.clear(); | ||
|
||
scene_root->Traverse([&](SceneNode* node) -> bool { | ||
if(!node->IsVisible()) return false; | ||
|
||
if(node->HasComponent<MeshComponent>()) { | ||
m_render_objects.push_back({ | ||
.local_to_world_transform = node->GetTransform().GetWorld() | ||
}); | ||
} | ||
|
||
return true; | ||
}); | ||
|
||
// Signal to the render thread that the next frame is ready | ||
m_caller_thread_semaphore.release(); | ||
} | ||
|
||
void RenderEngine::CreateRenderThread() { | ||
m_render_thread_running = true; | ||
m_render_thread_is_waiting = false; | ||
m_render_thread = std::thread{[this] { RenderThreadMain(); }}; | ||
} | ||
|
||
void RenderEngine::JoinRenderThread() { | ||
m_render_thread_running = false; | ||
if(m_render_thread_is_waiting) { | ||
m_caller_thread_semaphore.release(); | ||
} | ||
m_render_thread.join(); | ||
} | ||
|
||
void RenderEngine::RenderThreadMain() { | ||
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; | ||
|
||
fmt::print("got {} render objects\n", m_render_objects.size()); | ||
|
||
// Signal to the caller thread that we are done reading the internal render structures. | ||
m_render_thread_semaphore.release(); | ||
} | ||
} | ||
|
||
} // namespace zephyr |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters