libs/render/src/eu/render/postproc.h
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | #pragma once | ||
| 2 | |||
| 3 | #include "eu/render/texture.h" | ||
| 4 | |||
| 5 | namespace eu::imgui | ||
| 6 | { | ||
| 7 | struct ImguiShaderCache; | ||
| 8 | } | ||
| 9 | |||
| 10 | namespace eu::render | ||
| 11 | { | ||
| 12 | struct Texture2d; | ||
| 13 | struct Renderer; | ||
| 14 | struct Camera; | ||
| 15 | struct World; | ||
| 16 | struct RenderWorld; | ||
| 17 | |||
| 18 | /** \addtogroup postproc Post Processing | ||
| 19 | * \brief A basic framework for applying post-processing effects and rendering the \ref World. | ||
| 20 | * @{ | ||
| 21 | */ | ||
| 22 | |||
| 23 | /// Arguments for rendering a post-processing effect. | ||
| 24 | struct PostProcArg | ||
| 25 | { | ||
| 26 | const World* world; | ||
| 27 | Size window_size; | ||
| 28 | Rect final_rect; | ||
| 29 | const Camera* camera; | ||
| 30 | Renderer* renderer; | ||
| 31 | }; | ||
| 32 | |||
| 33 | /// A source that can be rendered to a framebuffer or a screen. | ||
| 34 | /// Pretty much always a "render world" call or the output of an existing \ref RenderTextureWithShader. | ||
| 35 | struct RenderSource | ||
| 36 | { | ||
| 37 | ✗ | RenderSource() = default; | |
| 38 | ✗ | virtual ~RenderSource() = default; | |
| 39 | |||
| 40 | RenderSource(const RenderSource&) = delete; | ||
| 41 | RenderSource(RenderSource&&) = delete; | ||
| 42 | void operator=(const RenderSource&) = delete; | ||
| 43 | void operator=(RenderSource&&) = delete; | ||
| 44 | |||
| 45 | virtual void render(const PostProcArg& arg) = 0; | ||
| 46 | }; | ||
| 47 | |||
| 48 | /// A functor that sends properties/uniforms to the shader. | ||
| 49 | /// First and foremost this would the used texture/fbo, but it could also be other uniforms like time, resolution, etc. | ||
| 50 | struct ShaderPropertyProvider | ||
| 51 | { | ||
| 52 | ✗ | ShaderPropertyProvider() = default; | |
| 53 | ✗ | virtual ~ShaderPropertyProvider() = default; | |
| 54 | |||
| 55 | ShaderPropertyProvider(const ShaderPropertyProvider&) = delete; | ||
| 56 | ShaderPropertyProvider(ShaderPropertyProvider&&) = delete; | ||
| 57 | void operator=(const ShaderPropertyProvider&) = delete; | ||
| 58 | void operator=(ShaderPropertyProvider&&) = delete; | ||
| 59 | |||
| 60 | virtual void use_shader(const PostProcArg& a, const FrameBuffer& t) = 0; | ||
| 61 | }; | ||
| 62 | |||
| 63 | /// Applies a shader to the output of another source. | ||
| 64 | /// This is done using a framebuffer that is rendered to a quad with the shader applied. | ||
| 65 | struct RenderTextureWithShader : RenderSource | ||
| 66 | { | ||
| 67 | std::string name; | ||
| 68 | std::shared_ptr<RenderSource> source; | ||
| 69 | std::shared_ptr<FrameBuffer> fbo; | ||
| 70 | ShaderPropertyProvider* effect; | ||
| 71 | |||
| 72 | RenderTextureWithShader(std::string n, std::shared_ptr<RenderSource> s, std::shared_ptr<FrameBuffer> f, ShaderPropertyProvider* e); | ||
| 73 | |||
| 74 | /// render internal fbo to a quad with a shader | ||
| 75 | void render(const PostProcArg& arg) override; | ||
| 76 | |||
| 77 | /// call render on linked source and render to fbo | ||
| 78 | void update(const PostProcArg& arg); | ||
| 79 | }; | ||
| 80 | |||
| 81 | /// A compiled full-screen-effect. | ||
| 82 | /// when compiled it could be: | ||
| 83 | /// * [render world to screen] | ||
| 84 | /// * [render world to fbo], [render fbo to screen with shader] | ||
| 85 | /// * [render world to fbo] [[render fbo to intermediate with shader] [render int to screen with shader]] | ||
| 86 | /// * [render world to fbo] [[render fbo to intermediate with shader] [render int to fbo with shader]] [render fbo to screen with shader] | ||
| 87 | struct CompiledStack | ||
| 88 | { | ||
| 89 | /// start with a simple world, but depending on the current effect list, could be more... | ||
| 90 | std::shared_ptr<RenderSource> last_source; | ||
| 91 | |||
| 92 | std::vector<std::shared_ptr<RenderTextureWithShader>> targets; | ||
| 93 | }; | ||
| 94 | |||
| 95 | |||
| 96 | /// Arguments for when building a effect stack. | ||
| 97 | /// @see \ref Effect::build | ||
| 98 | struct BuildArg | ||
| 99 | { | ||
| 100 | CompiledStack* builder; | ||
| 101 | Size window_size; | ||
| 102 | }; | ||
| 103 | |||
| 104 | |||
| 105 | struct EffectStack; | ||
| 106 | |||
| 107 | /// A effect that can be toggled. | ||
| 108 | /// Common effects are: blur, bloom, color grading, etc. | ||
| 109 | /// Most effects from https://lettier.github.io/3d-game-shaders-for-beginners/index.html would be implemented as effects. | ||
| 110 | struct Effect | ||
| 111 | { | ||
| 112 | ✗ | Effect() = default; | |
| 113 | ✗ | virtual ~Effect() = default; | |
| 114 | |||
| 115 | Effect(const Effect&) = delete; | ||
| 116 | Effect(Effect&&) = delete; | ||
| 117 | void operator=(const Effect&) = delete; | ||
| 118 | void operator=(Effect&&) = delete; | ||
| 119 | |||
| 120 | virtual void build(const BuildArg& arg) = 0; | ||
| 121 | virtual void update(float dt) = 0; | ||
| 122 | virtual void gui() = 0; | ||
| 123 | |||
| 124 | bool enabled() const; | ||
| 125 | |||
| 126 | protected: | ||
| 127 | |||
| 128 | void set_enabled(bool n); | ||
| 129 | |||
| 130 | private: | ||
| 131 | |||
| 132 | friend struct EffectStack; | ||
| 133 | |||
| 134 | bool is_enabled = false; | ||
| 135 | EffectStack* owner = nullptr; | ||
| 136 | }; | ||
| 137 | |||
| 138 | /// A special effect where the effect can be eased into existence. | ||
| 139 | /// Setting it to 0 will disable the effect, setting it anything above will enable it. | ||
| 140 | struct FactorEffect : Effect | ||
| 141 | { | ||
| 142 | FactorEffect(); | ||
| 143 | [[nodiscard]] float get_factor() const; | ||
| 144 | void set_factor(float f); | ||
| 145 | |||
| 146 | private: | ||
| 147 | |||
| 148 | float factor = 0.0f; | ||
| 149 | }; | ||
| 150 | |||
| 151 | /// The facade of the post-proc framework. | ||
| 152 | struct EffectStack | ||
| 153 | { | ||
| 154 | int current_msaa_setting = -1; | ||
| 155 | bool dirty = true; | ||
| 156 | std::optional<Size> window_size; | ||
| 157 | std::vector<std::shared_ptr<Effect>> effects; | ||
| 158 | CompiledStack compiled; | ||
| 159 | |||
| 160 | // render world settings | ||
| 161 | // todo(Gustav): is it useful to disable hdr rendering or should that just be removed? | ||
| 162 | // todo(Gustav): move exposure to a better place? | ||
| 163 | bool use_hdr = true; | ||
| 164 | float exposure = 1.0f; | ||
| 165 | |||
| 166 | std::shared_ptr<RenderWorld> render_world; | ||
| 167 | |||
| 168 | /// rebuilds stack if dirty, update all targets, then render the last_source | ||
| 169 | void render(const PostProcArg& arg); | ||
| 170 | void update(float dt) const; | ||
| 171 | void gui(imgui::ImguiShaderCache* cache); | ||
| 172 | }; | ||
| 173 | |||
| 174 | /** | ||
| 175 | * @} | ||
| 176 | */ | ||
| 177 | |||
| 178 | } | ||
| 179 |