Skip to content

Commit

Permalink
don't update camera buffer data more than necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
stohrendorf committed Sep 25, 2021
1 parent c7a8460 commit 22352b9
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions src/render/scene/camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ struct CameraMatrices
{
enum class DirtyFlag
{
BufferData,
Projection,
ViewProjection,
_flag_set_size [[maybe_unused]]
Expand Down Expand Up @@ -41,6 +42,8 @@ class Camera final
m_matrices.screenSize = glm::vec4{screenSize, 0, 0};
m_matrices.nearPlane = nearPlane;
m_matrices.farPlane = farPlane;

m_matricesBuffer.setData(m_matrices, gl::api::BufferUsage::DynamicDraw);
}

Camera(const Camera&) = delete;
Expand Down Expand Up @@ -71,6 +74,7 @@ class Camera final
m_matrices.screenSize = glm::vec4{screenSize, 0, 0};
m_dirty.set(CameraMatrices::DirtyFlag::Projection);
m_dirty.set(CameraMatrices::DirtyFlag::ViewProjection);
m_dirty.set(CameraMatrices::DirtyFlag::BufferData);
}

[[nodiscard]] float getNearPlane() const
Expand All @@ -92,6 +96,7 @@ class Camera final
{
m_matrices.view = m;
m_dirty.set(CameraMatrices::DirtyFlag::ViewProjection);
m_dirty.set(CameraMatrices::DirtyFlag::BufferData);
}

[[nodiscard]] auto getInverseViewMatrix() const
Expand All @@ -106,6 +111,7 @@ class Camera final
m_matrices.projection
= glm::perspective(m_fieldOfView, m_matrices.aspectRatio, m_matrices.nearPlane, m_matrices.farPlane);
m_dirty.reset(CameraMatrices::DirtyFlag::Projection);
m_dirty.set(CameraMatrices::DirtyFlag::BufferData);
}

return m_matrices.projection;
Expand All @@ -117,6 +123,7 @@ class Camera final
{
m_matrices.viewProjection = getProjectionMatrix() * getViewMatrix();
m_dirty.reset(CameraMatrices::DirtyFlag::ViewProjection);
m_dirty.set(CameraMatrices::DirtyFlag::BufferData);
}

return m_matrices.viewProjection;
Expand Down Expand Up @@ -154,13 +161,20 @@ class Camera final
return m_fieldOfView;
}

[[nodiscard]] const auto& getMatricesBuffer() const
[[nodiscard]] const gl::UniformBuffer<CameraMatrices>& getMatricesBuffer() const
{
(void)getProjectionMatrix();
(void)getViewProjectionMatrix();
BOOST_ASSERT(m_dirty.none());
if(m_dirty.any())
{
(void)getProjectionMatrix();
(void)getViewProjectionMatrix();
}
if(m_dirty.is_set(CameraMatrices::DirtyFlag::BufferData))
{
m_matricesBuffer.setData(m_matrices, gl::api::BufferUsage::DynamicDraw);
m_dirty.reset(CameraMatrices::DirtyFlag::BufferData);
}

m_matricesBuffer.setData(m_matrices, gl::api::BufferUsage::DynamicDraw);
BOOST_ASSERT(m_dirty.none());
return m_matricesBuffer;
}

Expand Down

0 comments on commit 22352b9

Please sign in to comment.