3 Copyright (C) 2022 x2048, Dmitry Kostenko <codeforsmile@gmail.com>
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU Lesser General Public License as published by
7 the Free Software Foundation; either version 2.1 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 #include "irrlichttypes_extrabloated.h"
34 struct PipelineContext
36 PipelineContext(IrrlichtDevice *_device, Client *_client, Hud *_hud, ShadowRenderer *_shadow_renderer, video::SColor _color, v2u32 _target_size)
37 : device(_device), client(_client), hud(_hud), shadow_renderer(_shadow_renderer), clear_color(_color), target_size(_target_size)
41 IrrlichtDevice *device;
44 ShadowRenderer *shadow_renderer;
45 video::SColor clear_color;
49 bool show_minimap {true};
50 bool draw_wield_tool {true};
51 bool draw_crosshair {true};
55 * Base object that can be owned by RenderPipeline
58 class RenderPipelineObject
61 virtual ~RenderPipelineObject() = default;
62 virtual void reset(PipelineContext &context) {}
66 * Represents a source of rendering information such as textures
68 class RenderSource : virtual public RenderPipelineObject
72 * Return the number of textures in the source.
74 virtual u8 getTextureCount() = 0;
77 * Get a texture by index.
78 * Returns nullptr is the texture does not exist.
80 virtual video::ITexture *getTexture(u8 index) = 0;
84 * Represents a render target (screen or framebuffer)
86 class RenderTarget : virtual public RenderPipelineObject
90 * Activate the render target and configure OpenGL state for the output.
91 * This is usually done by @see RenderStep implementations.
93 virtual void activate(PipelineContext &context)
99 * Resets the state of the object for the next pipeline iteration
101 virtual void reset(PipelineContext &context) override
111 * Texture buffer represents a framebuffer with a multiple attached textures.
113 * @note Use of TextureBuffer requires use of gl_FragData[] in the shader
115 class TextureBuffer : public RenderSource
118 virtual ~TextureBuffer() override;
121 * Configure fixed-size texture for the specific index
123 * @param index index of the texture
124 * @param size width and height of the texture in pixels
125 * @param height height of the texture in pixels
126 * @param name unique name of the texture
127 * @param format color format
129 void setTexture(u8 index, core::dimension2du size, const std::string& name, video::ECOLOR_FORMAT format);
132 * Configure relative-size texture for the specific index
134 * @param index index of the texture
135 * @param scale_factor relation of the texture dimensions to the screen dimensions
136 * @param name unique name of the texture
137 * @param format color format
139 void setTexture(u8 index, v2f scale_factor, const std::string& name, video::ECOLOR_FORMAT format);
141 virtual u8 getTextureCount() override { return m_textures.size(); }
142 virtual video::ITexture *getTexture(u8 index) override;
143 virtual void reset(PipelineContext &context) override;
145 static const u8 NO_DEPTH_TEXTURE = 255;
147 struct TextureDefinition
149 bool valid { false };
150 bool fixed_size { false };
151 bool dirty { false };
153 core::dimension2du size;
155 video::ECOLOR_FORMAT format;
159 * Make sure the texture in the given slot matches the texture definition given the current context.
160 * @param textureSlot address of the texture pointer to verify and populate.
161 * @param definition logical definition of the texture
162 * @param context current context of the rendering pipeline
163 * @return true if a new texture was created and put into the slot
164 * @return false if the slot was not modified
166 bool ensureTexture(video::ITexture **textureSlot, const TextureDefinition& definition, PipelineContext &context);
168 video::IVideoDriver *m_driver { nullptr };
169 std::vector<TextureDefinition> m_definitions;
170 core::array<video::ITexture *> m_textures;
174 * Targets output to designated texture in texture buffer
176 class TextureBufferOutput : public RenderTarget
179 TextureBufferOutput(TextureBuffer *buffer, u8 texture_index);
180 TextureBufferOutput(TextureBuffer *buffer, const std::vector<u8> &texture_map);
181 TextureBufferOutput(TextureBuffer *buffer, const std::vector<u8> &texture_map, u8 depth_stencil);
182 virtual ~TextureBufferOutput() override;
183 void activate(PipelineContext &context) override;
185 static const u8 NO_DEPTH_TEXTURE = 255;
187 TextureBuffer *buffer;
188 std::vector<u8> texture_map;
189 u8 depth_stencil { NO_DEPTH_TEXTURE };
190 video::IRenderTarget* render_target { nullptr };
191 video::IVideoDriver* driver { nullptr };
195 * Allows remapping texture indicies in another RenderSource.
197 * @note all unmapped indexes are passed through to the underlying render source.
199 class RemappingSource : RenderSource
202 RemappingSource(RenderSource *source)
207 * Maps texture index to a different index in the dependent source.
209 * @param index texture index as requested by the @see RenderStep.
210 * @param target_index matching texture index in the underlying @see RenderSource.
212 void setMapping(u8 index, u8 target_index)
214 if (index >= m_mappings.size()) {
215 u8 start = m_mappings.size();
216 m_mappings.resize(index);
217 for (u8 i = start; i < m_mappings.size(); ++i)
221 m_mappings[index] = target_index;
224 virtual u8 getTextureCount() override
226 return m_mappings.size();
229 virtual video::ITexture *getTexture(u8 index) override
231 if (index < m_mappings.size())
232 index = m_mappings[index];
234 return m_source->getTexture(index);
237 RenderSource *m_source;
238 std::vector<u8> m_mappings;
241 class DynamicSource : public RenderSource
244 bool isConfigured() { return upstream != nullptr; }
245 void setRenderSource(RenderSource *value) { upstream = value; }
248 * Return the number of textures in the source.
250 virtual u8 getTextureCount() override;
253 * Get a texture by index.
254 * Returns nullptr is the texture does not exist.
256 virtual video::ITexture *getTexture(u8 index) override;
258 RenderSource *upstream { nullptr };
262 * Implements direct output to screen framebuffer.
264 class ScreenTarget : public RenderTarget
267 virtual void activate(PipelineContext &context) override;
268 virtual void reset(PipelineContext &context) override;
270 core::dimension2du size;
273 class DynamicTarget : public RenderTarget
276 bool isConfigured() { return upstream != nullptr; }
277 void setRenderTarget(RenderTarget *value) { upstream = value; }
278 virtual void activate(PipelineContext &context) override;
280 RenderTarget *upstream { nullptr };
284 * Base class for rendering steps in the pipeline
286 class RenderStep : virtual public RenderPipelineObject
290 * Assigns render source to this step.
292 * @param source source of rendering information
294 virtual void setRenderSource(RenderSource *source) = 0;
297 * Assigned render target to this step.
299 * @param target render target to send output to.
301 virtual void setRenderTarget(RenderTarget *target) = 0;
304 * Runs the step. This method is invoked by the pipeline.
306 virtual void run(PipelineContext &context) = 0;
310 * Provides default empty implementation of supporting methods in a rendering step.
312 class TrivialRenderStep : public RenderStep
315 virtual void setRenderSource(RenderSource *source) override {}
316 virtual void setRenderTarget(RenderTarget *target) override {}
317 virtual void reset(PipelineContext &) override {}
321 * Dynamically changes render target of another step.
323 * This allows re-running parts of the pipeline with different outputs
325 class SetRenderTargetStep : public TrivialRenderStep
328 SetRenderTargetStep(RenderStep *step, RenderTarget *target);
329 virtual void run(PipelineContext &context) override;
332 RenderTarget *target;
336 * Render Pipeline provides a flexible way to execute rendering steps in the engine.
338 * RenderPipeline also implements @see RenderStep, allowing for nesting of the pipelines.
340 class RenderPipeline : public RenderStep
344 * Add a step to the end of the pipeline
346 * @param step reference to a @see RenderStep implementation.
348 RenderStep *addStep(RenderStep *step)
350 m_pipeline.push_back(step);
355 * Capture ownership of a dynamically created @see RenderStep instance.
357 * RenderPipeline will delete the instance when the pipeline is destroyed.
359 * @param step reference to the instance.
360 * @return RenderStep* value of the 'step' parameter.
363 T *own(std::unique_ptr<T> &&object)
365 T* result = object.release();
366 m_objects.push_back(std::unique_ptr<RenderPipelineObject>(result));
371 * Create a new object that will be managed by the pipeline
373 * @tparam T type of the object to be created
374 * @tparam Args types of constructor arguments
375 * @param args constructor arguments
376 * @return T* pointer to the newly created object
378 template<typename T, typename... Args>
379 T *createOwned(Args&&... args) {
380 return own(std::make_unique<T>(std::forward<Args>(args)...));
384 * Create and add a step managed by the pipeline and return a pointer
385 * to the step for further configuration.
387 * @tparam T Type of the step to be added.
388 * @tparam Args Types of the constructor parameters
389 * @param args Constructor parameters
390 * @return RenderStep* Pointer to the created step for further configuration.
392 template<typename T, typename... Args>
393 T *addStep(Args&&... args) {
394 T* result = own(std::make_unique<T>(std::forward<Args>(args)...));
399 RenderSource *getInput();
400 RenderTarget *getOutput();
402 v2f getScale() { return scale; }
403 void setScale(v2f value) { scale = value; }
405 virtual void reset(PipelineContext &context) override {}
406 virtual void run(PipelineContext &context) override;
408 virtual void setRenderSource(RenderSource *source) override;
409 virtual void setRenderTarget(RenderTarget *target) override;
411 std::vector<RenderStep *> m_pipeline;
412 std::vector< std::unique_ptr<RenderPipelineObject> > m_objects;
413 DynamicSource m_input;
414 DynamicTarget m_output;
415 v2f scale { 1.0f, 1.0f };