Skip to content

Commit

Permalink
make local shader time invariant across effect pipeline reconstructions
Browse files Browse the repository at this point in the history
  • Loading branch information
stohrendorf committed Oct 26, 2021
1 parent ddc1959 commit af0165f
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 8 deletions.
10 changes: 6 additions & 4 deletions src/render/pass/effectpass.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "effectpass.h"

#include "config.h"
#include "render/renderpipeline.h"
#include "render/scene/mesh.h"
#include "render/scene/rendercontext.h"
#include "render/scene/rendermode.h"
Expand Down Expand Up @@ -28,10 +29,12 @@ class Node;

namespace render::pass
{
EffectPass::EffectPass(std::string name,
EffectPass::EffectPass(gsl::not_null<const RenderPipeline*> renderPipeline,
std::string name,
gsl::not_null<std::shared_ptr<scene::Material>> material,
const gsl::not_null<std::shared_ptr<gl::TextureHandle<gl::Texture2D<gl::SRGB8>>>>& input)
: m_name{std::move(name)}
: m_renderPipeline{std::move(renderPipeline)}
, m_name{std::move(name)}
, m_material{std::move(material)}
, m_mesh{scene::createScreenQuad(m_material, m_name)}
, m_colorBuffer{std::make_shared<gl::Texture2D<gl::SRGB8>>(input->getTexture()->size(), m_name + "-color")}
Expand Down Expand Up @@ -69,8 +72,7 @@ void EffectPass::render(bool inWater)
m_mesh->bind("u_time",
[this](const scene::Node&, const scene::Mesh& /*mesh*/, gl::Uniform& uniform)
{
const auto now = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::high_resolution_clock::now() - m_creationTime);
const auto now = m_renderPipeline->getLocalTime();
uniform.set(gsl::narrow_cast<float>(now.count()));
});
m_mesh->render(context);
Expand Down
11 changes: 8 additions & 3 deletions src/render/pass/effectpass.h
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
#pragma once

#include <chrono>
#include <gl/pixel.h>
#include <gl/soglb_fwd.h>
#include <gsl/gsl-lite.hpp>
#include <memory>
#include <string>
#include <utility>

namespace render
{
class RenderPipeline;
}

namespace render::scene
{
class Material;
Expand All @@ -19,7 +23,8 @@ namespace render::pass
class EffectPass final
{
public:
explicit EffectPass(std::string name,
explicit EffectPass(gsl::not_null<const RenderPipeline*> renderPipeline,
std::string name,
gsl::not_null<std::shared_ptr<scene::Material>> material,
const gsl::not_null<std::shared_ptr<gl::TextureHandle<gl::Texture2D<gl::SRGB8>>>>& input);

Expand All @@ -42,12 +47,12 @@ class EffectPass final
}

private:
const gsl::not_null<const RenderPipeline*> m_renderPipeline;
const std::string m_name;
const gsl::not_null<std::shared_ptr<scene::Material>> m_material;
gsl::not_null<std::shared_ptr<scene::Mesh>> m_mesh;
gsl::not_null<std::shared_ptr<gl::Texture2D<gl::SRGB8>>> m_colorBuffer;
gsl::not_null<std::shared_ptr<gl::TextureHandle<gl::Texture2D<gl::SRGB8>>>> m_colorBufferHandle;
gsl::not_null<std::shared_ptr<gl::Framebuffer>> m_fb;
const std::chrono::high_resolution_clock::time_point m_creationTime = std::chrono::high_resolution_clock::now();
};
} // namespace render::pass
2 changes: 1 addition & 1 deletion src/render/renderpipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ void RenderPipeline::resize(scene::MaterialManager& materialManager,
auto addEffect =
[this, &fxSource](const std::string& name, const gsl::not_null<std::shared_ptr<render::scene::Material>>& material)
{
auto fx = std::make_shared<pass::EffectPass>("fx:" + name, material, fxSource);
auto fx = std::make_shared<pass::EffectPass>(gsl::not_null{this}, "fx:" + name, material, fxSource);
m_effects.emplace_back(fx);
fxSource = fx->getOutput();
return fx;
Expand Down
9 changes: 9 additions & 0 deletions src/render/renderpipeline.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "rendersettings.h"

#include <chrono>
#include <gl/soglb_fwd.h>
#include <glm/vec2.hpp>
#include <memory>
Expand Down Expand Up @@ -33,6 +34,8 @@ class EffectPass;
class RenderPipeline
{
private:
const std::chrono::high_resolution_clock::time_point m_creationTime = std::chrono::high_resolution_clock::now();

RenderSettings m_renderSettings{};
glm::ivec2 m_renderSize{-1};
glm::ivec2 m_displaySize{-1};
Expand Down Expand Up @@ -63,5 +66,11 @@ class RenderPipeline
bool force = false);

void apply(const RenderSettings& renderSettings, scene::MaterialManager& materialManager);

[[nodiscard]] auto getLocalTime() const
{
return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now()
- m_creationTime);
}
};
} // namespace render

0 comments on commit af0165f

Please sign in to comment.