#pragma once #include #include #include #include #include #include namespace mln { namespace gfx { class Context; class Texture2D; class OffscreenTexture; class UploadPass; using Texture2DPtr = std::shared_ptr; } // namespace gfx class LayerGroupBase; class PaintParameters; class RenderOrchestrator; class RenderTree; using LayerGroupBasePtr = std::shared_ptr; /// Render target class class RenderTarget { public: RenderTarget(gfx::Context& context, const Size size, const gfx::TextureChannelDataType type); ~RenderTarget(); /// Get the render target texture const gfx::Texture2DPtr& getTexture(); /// Create and validate the backing offscreen framebuffer. bool isRenderable(); /// @brief Add a layer group to the render target /// @param replace Flag to replace if exists /// @return whether added bool addLayerGroup(LayerGroupBasePtr, bool replace); /// @brief Remove a layer group /// @param layerIndex index of the layer to remove /// @return whether removed bool removeLayerGroup(const int32_t layerIndex); /// Get the layer group count size_t numLayerGroups() const noexcept; /// @brief Get a specific layer group by index /// @param layerIndex index /// @return the layer group if existent, otherwise a shared null pointer const LayerGroupBasePtr& getLayerGroup(const int32_t layerIndex) const; /// Execute the given function for each contained layer group template void visitLayerGroups(Func f) { for (auto& pair : layerGroupsByLayerIndex) { if (pair.second) { f(*pair.second); } } } /// Execute the given function for each contained layer group in reversed order template void visitLayerGroupsReversed(Func f) { for (auto rit = layerGroupsByLayerIndex.rbegin(); rit != layerGroupsByLayerIndex.rend(); ++rit) { if (rit->second) { f(*rit->second); } } } /// Upload the layer groups void upload(gfx::UploadPass& uploadPass); /// Render the layer groups void render(RenderOrchestrator&, const RenderTree&, PaintParameters&); protected: gfx::Context& context; std::unique_ptr offscreenTexture; using LayerGroupMap = std::map; LayerGroupMap layerGroupsByLayerIndex; }; } // namespace mln