GCC Code Coverage Report


./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
0 of 9, 0 excluded
0.0%
Functions:
0 of 6, 0 excluded
0.0%
Branches:
0 of 0, 0 excluded
-%

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