diff --git a/include/mbgl/gfx/drawable.hpp b/include/mbgl/gfx/drawable.hpp index 985a11937..94607c859 100644 --- a/include/mbgl/gfx/drawable.hpp +++ b/include/mbgl/gfx/drawable.hpp @@ -45,11 +45,13 @@ protected: Drawable(std::string name); public: + /// Drawable constructor virtual ~Drawable(); struct DrawSegment; using UniqueDrawSegment = std::unique_ptr; + /// Get drawable's ID const util::SimpleIdentity& getID() const { return uniqueID; } /// Draw the drawable @@ -57,14 +59,20 @@ public: /// Drawable name is used for debugging and troubleshooting const std::string& getName() const { return name; } + + /// Set drawable name void setName(std::string value) { name = std::move(value); } /// Which shader to use when rendering this drawable const gfx::ShaderProgramBasePtr& getShader() const { return shader; } + + /// Set the shader to be used void setShader(gfx::ShaderProgramBasePtr value) { shader = std::move(value); } /// The pass on which we'll be rendered mbgl::RenderPass getRenderPass() const { return renderPass; } + + /// Sets the render passes void setRenderPass(mbgl::RenderPass value) { renderPass = value; } /// Test whether to draw this drawable in a given render pass. @@ -81,6 +89,8 @@ public: /// Width for lines int32_t getLineWidth() const { return lineWidth; } + + /// Set line width void setLineWidth(int32_t value) { lineWidth = value; } /// @brief Remove an attached texture from this drawable at the given sampler location @@ -106,72 +116,111 @@ public: /// Whether the drawble should be drawn bool getEnabled() const { return enabled; } + + /// Sets whether the drawble should be drawn void setEnabled(bool value) { enabled = value; } /// Whether to render to the color target bool getEnableColor() const { return enableColor; } + + /// Set whether to render to the color target void setEnableColor(bool value) { enableColor = value; } /// Whether to do stenciling (based on the Tile ID or 3D) bool getEnableStencil() const { return enableStencil; } + + /// Set stencil usage void setEnableStencil(bool value) { enableStencil = value; } /// not used for anything yet DrawPriority getDrawPriority() const { return drawPriority; } void setDrawPriority(DrawPriority value) { drawPriority = value; } - /// Determines depth range within the layer for 2D drawables + /// Get sub-layer index. Determines depth range within the layer for 2D drawables int32_t getSubLayerIndex() const { return subLayerIndex; } + + /// Set sub-layer index void setSubLayerIndex(int32_t value) { subLayerIndex = value; } /// Depth writability for 2D drawables DepthMaskType getDepthType() const { return depthType; } + + /// Set depth type void setDepthType(DepthMaskType value) { depthType = value; } /// Uses 3D depth mode bool getIs3D() const { return is3D; } + + /// Set 3D mode void setIs3D(bool value) { is3D = value; } - /// Is custom + /// True if this is a custom drawable bool getIsCustom() const { return isCustom; } + + /// Sets custom status for this drawable void setIsCustom(bool value) { isCustom = value; } - /// The ID of the tile that this drawable represents, if any + /// Get the ID of the tile that this drawable represents, if any const std::optional& getTileID() const { return tileID; } + + /// Set the ID of the tile that this drawable represents void setTileID(const OverscaledTileID& value) { tileID = value; } + /// Get cull face mode const gfx::CullFaceMode& getCullFaceMode() const; + + /// Set cull face mode void setCullFaceMode(const gfx::CullFaceMode&); + /// Get color mode const gfx::ColorMode& getColorMode() const; + + /// Set color mode void setColorMode(const gfx::ColorMode&); /// Get the vertex attributes that override default values in the shader program virtual const gfx::VertexAttributeArray& getVertexAttributes() const = 0; + + /// Get the mutable vertex attribute array virtual gfx::VertexAttributeArray& mutableVertexAttributes() = 0; + /// Set vertex attribute array virtual void setVertexAttributes(const gfx::VertexAttributeArray&) = 0; + + /// Set vertex attribute array virtual void setVertexAttributes(gfx::VertexAttributeArray&&) = 0; - /// Provide raw data for vertices - /// Incompatible with adding primitives + /// Provide raw data for vertices. Incompatible with adding primitives virtual void setVertices(std::vector&&, std::size_t, AttributeDataType) = 0; /// Provide raw indexes and segments void setIndexData(std::vector indexes, std::vector); + + /// Set shared indexes and segments virtual void setIndexData(gfx::IndexVectorBasePtr, std::vector) = 0; /// Get the tweakers attached to this drawable const std::vector& getTweakers() const { return tweakers; } + + /// Add a tweaker to this drawable void addTweaker(DrawableTweakerPtr value) { tweakers.emplace_back(std::move(value)); } + + /// Set the entire tweaker collection void setTweakers(std::vector value) { tweakers = std::move(value); } + + /// Clear the tweaker collection void clearTweakers() { tweakers.clear(); } /// Get the uniform buffers attached to this drawable virtual const gfx::UniformBufferArray& getUniformBuffers() const = 0; + + /// Get the mutable uniform buffer array virtual gfx::UniformBufferArray& mutableUniformBuffers() = 0; + /// Get drawable data const UniqueDrawableData& getData() const { return drawableData; } + + /// Set drawable data void setData(UniqueDrawableData&& value) { drawableData = std::move(value); } protected: diff --git a/include/mbgl/gfx/drawable_builder.hpp b/include/mbgl/gfx/drawable_builder.hpp index 4a824a810..80614c576 100644 --- a/include/mbgl/gfx/drawable_builder.hpp +++ b/include/mbgl/gfx/drawable_builder.hpp @@ -31,6 +31,7 @@ protected: DrawableBuilder(std::string name); public: + /// Destructor virtual ~DrawableBuilder(); /// Get the drawable we're currently working on, if any @@ -54,14 +55,20 @@ public: /// Whether the drawble should be drawn bool getEnabled() const { return enabled; } + + /// Set whether the drawable should be enabled void setEnabled(bool value) { enabled = value; } /// The pass on which we'll be rendered mbgl::RenderPass getRenderPass() const { return renderPass; } + + /// Set the pass on which the drawable will render void setRenderPass(mbgl::RenderPass value) { renderPass = value; } /// The draw priority assigned to generated drawables DrawPriority getDrawPriority() const; + + /// Set drawable's draw priority void setDrawPriority(DrawPriority); /// Determines depth range within the layer for 2D drawables @@ -70,10 +77,14 @@ public: /// Depth writability for 2D drawables DepthMaskType getDepthType() const { return depthType; } + + /// Set depth type void setDepthType(DepthMaskType value) { depthType = value; } /// Uses 3D depth mode bool getIs3D() const { return is3D; } + + /// Set 3D mode void setIs3D(bool value) { is3D = value; } /// Set the draw priority on all drawables including those already generated @@ -81,24 +92,38 @@ public: /// Width for lines float getLineWidth() const { return lineWidth; } + + /// Set line width void setLineWidth(float value) { lineWidth = value; } /// Whether to render to the color target bool getEnableColor() const { return enableColor; } + + /// Set whether to render to the color target void setEnableColor(bool value) { enableColor = value; } /// Whether to do stenciling (based on the Tile ID or 3D) bool getEnableStencil() const { return enableStencil; } + + /// Set stencil usage void setEnableStencil(bool value) { enableStencil = value; } + /// Get color mode const gfx::ColorMode& getColorMode() const; + + /// Set color mode void setColorMode(const gfx::ColorMode& value); + /// Get cull face mode const gfx::CullFaceMode& getCullFaceMode() const; + + /// Set cull face mode void setCullFaceMode(const gfx::CullFaceMode& value); /// Which shader to use when rendering emitted drawables const gfx::ShaderProgramBasePtr& getShader() const { return shader; } + + /// Set the shader to be used void setShader(gfx::ShaderProgramBasePtr value) { shader = std::move(value); } /// Get the vertex attributes that override default values in the shader program @@ -115,11 +140,15 @@ public: /// @param location A sampler location in the shader being used. void setTexture(const gfx::Texture2DPtr&, int32_t location); + /// Add a tweaker to emitted drawable void addTweaker(DrawableTweakerPtr value) { tweakers.emplace_back(std::move(value)); } + + /// Clear the tweaker collection void clearTweakers() { tweakers.clear(); } /// Add a triangle void addTriangle(int16_t x0, int16_t y0, int16_t x1, int16_t y1, int16_t x2, int16_t y2); + /// Add another triangle based on the previous two points void appendTriangle(int16_t x0, int16_t y0); @@ -135,11 +164,13 @@ public: std::size_t vertexOffset, std::size_t vertexLength); - /// Provide raw data for vertices - /// Incompatible with adding primitives + /// Provide raw data for vertices. Incompatible with adding primitives void setRawVertices(std::vector&&, std::size_t, AttributeDataType); + /// Set indexes and segments void setSegments(gfx::DrawMode, std::vector indexes, const SegmentBase*, std::size_t segmentCount); + + /// Set shared indices and segments void setSegments(gfx::DrawMode, gfx::IndexVectorBasePtr, const SegmentBase*, std::size_t segmentCount); /// Add lines based on existing vertices diff --git a/include/mbgl/gfx/gfx_types.hpp b/include/mbgl/gfx/gfx_types.hpp index a73d45241..933f94460 100644 --- a/include/mbgl/gfx/gfx_types.hpp +++ b/include/mbgl/gfx/gfx_types.hpp @@ -5,58 +5,61 @@ namespace mbgl { namespace gfx { +/// Graphic primitive type enumeration enum class PrimitiveType : uint8_t { - Point, - Line, - Triangle, + Point, ///< point primitive + Line, ///< line primive + Triangle, ///< triangle primitive }; +/// Drawing mode type enum class DrawModeType : uint8_t { - Points, - Lines, - LineLoop, - LineStrip, - Triangles, - TriangleStrip, - TriangleFan, + Points, ///< points + Lines, ///< line segments + LineLoop, ///< line loop, a.k.a. closed polyline + LineStrip, ///< line strip, a.k.a. open polyline + Triangles, ///< independent triangles + TriangleStrip, ///< triangles sharing vertices + TriangleFan, ///< fan of triangles, with a common central point }; +/// Attribute data types enum class AttributeDataType : uint8_t { - Byte, - Byte2, - Byte3, - Byte4, + Byte, ///< one signed byte (8 bit) + Byte2, ///< pack of 2 signed bytes + Byte3, ///< pack of 3 signed bytes + Byte4, ///< pack 4 signed bytes - UByte, - UByte2, - UByte3, - UByte4, + UByte, ///< one unsigned byte (8 bit) + UByte2, ///< pack of 2 unsigned bytes + UByte3, ///< pack of 3 unsigned bytes + UByte4, ///< pack of 4 unsigned bytes - Short, - Short2, - Short3, - Short4, + Short, ///< signed 16 bit integer value + Short2, ///< pack of 2 signed 16 bit integers + Short3, ///< pack of 3 signed 16 bit integers + Short4, ///< pack of 4 signed 16 bit integers - UShort, - UShort2, - UShort3, - UShort4, - UShort8, + UShort, ///< unsigned 16 bit integer value + UShort2, ///< pack of 2 unsigned 16 bit integers + UShort3, ///< pack of 3 unsigned 16 bit integers + UShort4, ///< pack of 4 unsigned 16 bit integers + UShort8, ///< pack of 6 unsigned 16 bit integers - Int, - Int2, - Int3, - Int4, + Int, ///< signed 32 bit integer value + Int2, ///< pack of 2 signed 32 bit integers + Int3, ///< pack of 3 signed 32 bit integers + Int4, ///< pack of 4 signed 32 bit integers - UInt, - UInt2, - UInt3, - UInt4, + UInt, ///< unsigned 32 bit integer value + UInt2, ///< pack of 2 unsigned 32 bit integers + UInt3, ///< pack of 3 unsigned 32 bit integers + UInt4, ///< pack of 4 unsigned 32 bit integers - Float, - Float2, - Float3, - Float4, + Float, ///< 32 bit floating point value + Float2, ///< pack of 2 floating point values + Float3, ///< pack of 3 floating point values + Float4, ///< pack of 4 floating point values Invalid = 255, }; diff --git a/include/mbgl/gfx/texture2d.hpp b/include/mbgl/gfx/texture2d.hpp index 9f5c14a9b..4787e1410 100644 --- a/include/mbgl/gfx/texture2d.hpp +++ b/include/mbgl/gfx/texture2d.hpp @@ -85,10 +85,21 @@ public: upload(img.data ? img.data.get() : nullptr, img.size); } + /// @brief Upload a subregion of the texture resource + /// @param pixelData image pixel data pointer + /// @param size image dimensions + /// @param xOffset destination x coordinate + /// @param yOffset destination y coordinate virtual void uploadSubRegion(const void* pixelData, const Size& size, uint16_t xOffset, uint16_t yOffset) noexcept = 0; + + /// @brief Upload a subregion of the texture resource + /// @tparam Image Image object type + /// @param img Image to upload + /// @param xOffset Destination x coordinate + /// @param yOffset Destination y coordinate template void uploadSubRegion(const Image& img, uint16_t xOffset, uint16_t yOffset) noexcept { assert(Image::channels == numChannels()); @@ -108,6 +119,7 @@ public: virtual gfx::TextureResource& getResource() const = 0; /// @brief Check whether the texture needs upload + /// @return bool virtual bool needsUpload() const noexcept = 0; }; diff --git a/include/mbgl/gfx/types.hpp b/include/mbgl/gfx/types.hpp index d848ae270..7d77fe014 100644 --- a/include/mbgl/gfx/types.hpp +++ b/include/mbgl/gfx/types.hpp @@ -7,117 +7,137 @@ namespace mbgl { namespace gfx { +/// Color blending equation type enum class ColorBlendEquationType : uint8_t { - Add, - Subtract, - ReverseSubtract, + Add, ///< The source and destination colors are added to each other. O = sS + dD. The The s and d are blending + ///< parameters that are multiplied into each of S and D before the addition. + Subtract, ///< Subtracts the destination from the source. O = sS - dD. The source and dest are multiplied by + ///< blending parameters. + ReverseSubtract, ///< Subtracts the source from the destination. O = dD - sS. The source and dest are multiplied by + ///< blending parameters. }; +/// Color blending factor type enum class ColorBlendFactorType : uint8_t { - Zero, - One, - SrcColor, - OneMinusSrcColor, - SrcAlpha, - OneMinusSrcAlpha, - DstAlpha, - OneMinusDstAlpha, - DstColor, - OneMinusDstColor, - SrcAlphaSaturate, - ConstantColor, - OneMinusConstantColor, - ConstantAlpha, - OneMinusConstantAlpha, + Zero, ///< Factor is (0, 0, 0, 0) + One, ///< Factor is (1, 1, 1, 1) + SrcColor, ///< Factor is (Rs, Gs, Bs, As) where S is the source color + OneMinusSrcColor, ///< Factor is (1, 1, 1, 1) - (Rs, Gs, Bs, As) where S is the source color + SrcAlpha, ///< Factor is (As, As, As, As) where S is the source color + OneMinusSrcAlpha, ///< Factor is (1, 1, 1, 1) - (As, As, As, As) where S is the source color + DstAlpha, ///< Factor is (Ad, Ad, Ad, Ad) where D is the destination color + OneMinusDstAlpha, ///< Factor is (1, 1, 1, 1) - (Ad, Ad, Ad, Ad) where D is the destination color + DstColor, ///< Factor is (Rd, Gd, Bd, Ad) where D is the destination color + OneMinusDstColor, ///< Factor is (1, 1, 1, 1) - (Rd, Gd, Bd, Ad) where D is the destination color + SrcAlphaSaturate, ///< Factor is (f, f, f, 1) where f = min(As, 1 - Ad) and S is the source color + ConstantColor, ///< Factor is (Rc, Gc, Bc, Ac) where C is the constant color + OneMinusConstantColor, ///< Factor is (1, 1, 1, 1) - (Rc, Gc, Bc, Ac) where C is the constant color + ConstantAlpha, ///< Factor is (Ac, Ac, Ac, Ac) where C is the constant color + OneMinusConstantAlpha, ///< Factor is (1, 1, 1, 1) - (Ac, Ac, Ac, Ac) where C is the constant color }; +/// Depth function type enum class DepthFunctionType : uint8_t { - Never, - Less, - Equal, - LessEqual, - Greater, - NotEqual, - GreaterEqual, - Always, + Never, ///< The depth test never passes. + Less, ///< The depth test passes if the incoming value is less than the stored value. + Equal, ///< The depth test passes if the incoming value is equal to the stored value. + LessEqual, ///< The depth test passes if the incoming value is less than or equal to the stored value. + Greater, ///< The depth test passes if the incoming value is greater than the stored value. + NotEqual, ///< The depth test passes if the incoming value is not equal to the stored value. + GreaterEqual, ///< The depth test passes if the incoming value is greater than or equal to the stored value. + Always, ///< The depth test always passes. }; +/// Depth buffer masking type enum class DepthMaskType : bool { - ReadOnly = false, - ReadWrite = true, + ReadOnly = false, ///< The depth buffer is not written to + ReadWrite = true, ///< The depth buffer is is updated by the new fragments' depth }; +/// Stencil function type enum class StencilFunctionType : uint8_t { - Never, - Less, - Equal, - LessEqual, - Greater, - NotEqual, - GreaterEqual, - Always, + Never, ///< Always fails. + Less, ///< Passes if ( ref & mask ) < ( stencil & mask ). + Equal, ///< Passes if ( ref & mask ) == ( stencil & mask ). + LessEqual, ///< Passes if ( ref & mask ) <= ( stencil & mask ). + Greater, ///< Passes if ( ref & mask ) > ( stencil & mask ). + NotEqual, ///< Passes if ( ref & mask ) != ( stencil & mask ). + GreaterEqual, ///< Passes if ( ref & mask ) >= ( stencil & mask ). + Always, ///< Always passes. }; +/// Stencil operation type enum class StencilOpType : uint8_t { - Zero, - Keep, - Replace, - Increment, - Decrement, - Invert, - IncrementWrap, - DecrementWrap, + Zero, ///< Sets the stencil buffer value to 0. + Keep, ///< Keeps the current value. + Replace, ///< Replace the stencil buffer value + Increment, ///< Increments the current stencil buffer value. Clamps to the maximum representable unsigned value. + Decrement, ///< Decrements the current stencil buffer value. Clamps to 0. + Invert, ///< Bitwise inverts the current stencil buffer value. + IncrementWrap, ///< Increments the current stencil buffer value. Wraps stencil buffer value to zero when + ///< incrementing the maximum representable unsigned value. + DecrementWrap, ///< Decrements the current stencil buffer value. Wraps stencil buffer value to the maximum + ///< representable unsigned value when decrementing a stencil buffer value of zero. }; +/// Cull face side enum CullFaceSideType : uint8_t { - Front, - Back, - FrontAndBack, + Front, ///< Front facing facets are culled. + Back, ///< Back facing facets are culled. + FrontAndBack, ///< Front and back facing facets are culled. }; +/// Define the orientation of front facing polygons enum CullFaceWindingType : uint8_t { - Clockwise, - CounterClockwise, + Clockwise, ///< Front face is clockwise. + CounterClockwise, ///< Front face is counter clockwise. }; +/// Buffer usage type enum class BufferUsageType : uint8_t { - StreamDraw, - StaticDraw, - DynamicDraw, + StreamDraw, ///< The data store contents will be modified once and used at most a few times. + StaticDraw, ///< The data store contents will be modified once and used many times. + DynamicDraw, ///< The data store contents will be modified repeatedly and used many times. }; +/// Texture pixel type enum class TexturePixelType : uint8_t { - RGBA, - Alpha, - Stencil, - Depth, - Luminance, + RGBA, ///< Red, Green, Blue and Alpha + Alpha, ///< Alpha only + Stencil, ///< Stencil + Depth, ///< Depth component + Luminance, ///< Luminance }; +/// Texture channel data type enum class TextureChannelDataType : uint8_t { - UnsignedByte, - HalfFloat, + UnsignedByte, ///< 8 bit unsigned byte + HalfFloat, ///< 16 bit "half-float" }; +/// Texture mip map type enum class TextureMipMapType : bool { - No, - Yes + No, ///< does not use mipmapping + Yes ///< uses mipmapping }; +/// Texture filter type enum class TextureFilterType : bool { - Nearest, - Linear, + Nearest, ///< Nearest pixel + Linear, ///< Bilinear interpolation }; +/// Texture wrap mode enum class TextureWrapType : bool { - Clamp, - Repeat, + Clamp, ///< Clamp to edge + Repeat, ///< Repeat }; +/// Render buffer pixel type enum class RenderbufferPixelType : uint8_t { - RGBA, - Depth, - DepthStencil, + RGBA, ///< Red, Green, Blue and Alpha + Depth, ///< Depth component + DepthStencil, ///< Depth and stencil components }; } // namespace gfx diff --git a/include/mbgl/gfx/uniform_block.hpp b/include/mbgl/gfx/uniform_block.hpp index 16e8ede26..256b8e311 100644 --- a/include/mbgl/gfx/uniform_block.hpp +++ b/include/mbgl/gfx/uniform_block.hpp @@ -9,9 +9,12 @@ namespace gfx { class UniformBuffer; +/// @brief This class represents an uniform block class UniformBlock { - // Can only be created by UniformBlockArray implementations protected: + /// @brief Constructor. Can only be created by UniformBlockArray implementations + /// @param index_ + /// @param size_ UniformBlock(int index_, std::size_t size_) : index(index_), size(size_) {} @@ -21,13 +24,22 @@ protected: size(other.size) {} public: + /// @brief Destructor virtual ~UniformBlock() = default; + /// @brief Retrieves the index of this uniform block + /// @return int int getIndex() const { return index; } + /// @brief Get the size of the uniform block + /// @return std::size_t std::size_t getSize() const { return size; } + /// @brief Binds the buffer + /// @param uniformBuffer virtual void bindBuffer(const UniformBuffer& uniformBuffer) = 0; + + /// @brief Unbinds the uniform buffer virtual void unbindBuffer() = 0; protected: @@ -48,27 +60,40 @@ class UniformBlockArray { public: using UniformBlockMap = std::unordered_map>; + /// @brief Constructor + /// @param initCapacity initial collection capacity UniformBlockArray(int initCapacity = 10); + + /// @brief Move constructor UniformBlockArray(UniformBlockArray&&); - // Would need to use the virtual assignment operator + + /// @brief Copy constructor. Would need to use the virtual assignment operator UniformBlockArray(const UniformBlockArray&) = delete; + + /// @brief Destructor virtual ~UniformBlockArray() = default; - /// Get map of elements. + /// @brief Get map of elements. const UniformBlockMap& getMap() const { return uniformBlockMap; } - /// Number of elements + /// @brief Number of elements std::size_t size() const { return uniformBlockMap.size(); } - /// Get an uniform block element. - /// Returns a pointer to the element on success, or null if the uniform block doesn't exists. + /// @brief Get an uniform block element. + /// @return Pointer to the element on success, or null if the uniform block doesn't exists. const std::unique_ptr& get(const std::string& name) const; - /// Add a new uniform block element. - /// Returns a pointer to the new element on success, or null if the uniform block already exists. + /// @brief Add a new uniform block element. + /// @param name + /// @param index + /// @param size + /// @return Pointer to the new element on success, or null if the uniform block already exists. const std::unique_ptr& add(std::string name, int index, std::size_t size); + /// @brief Move assignment operator UniformBlockArray& operator=(UniformBlockArray&&); + + /// @brief Copy assignment operator UniformBlockArray& operator=(const UniformBlockArray&); protected: diff --git a/include/mbgl/gfx/vertex_attribute.hpp b/include/mbgl/gfx/vertex_attribute.hpp index 9510e9161..ecc1cab35 100644 --- a/include/mbgl/gfx/vertex_attribute.hpp +++ b/include/mbgl/gfx/vertex_attribute.hpp @@ -15,8 +15,13 @@ namespace mbgl { +/// A 2 by 2 matrix using mat2 = std::array; + +/// A 3 by 3 matrix using mat3 = std::array; + +/// A 4 by 4 matrix using mat4 = std::array; namespace gfx { @@ -26,20 +31,44 @@ class VertexVectorBase; class VertexAttribute { public: + /// type for vector of 2 floats using float2 = std::array; + + /// type for vector of 3 floats using float3 = std::array; + + /// type for vector of 3 floats using float4 = std::array; + + /// type for 2 by 2 matrix of floats using matf2 = std::array; + + /// type for 3 by 3 matrix of floats using matf3 = std::array; + + /// type for 4 by 4 matrix of floats using matf4 = std::array; + + /// type for vector of 2 integers using int2 = std::array; + + /// type for vector of 3 integers using int3 = std::array; + + /// type for vector of 4 integers using int4 = std::array; + + /// type for vector of 8 unsigned short integers using ushort8 = std::array; using ElementType = std::variant; + /// @brief Constructor for vertex attribute + /// @param index_ the index of the vertex attribute + /// @param dataType_ the vertex attribute data type + /// @param count_ element count + /// @param stride_ the vertex attribute stride VertexAttribute(int index_, AttributeDataType dataType_, std::size_t count_, std::size_t stride_) : index(index_), stride((int)stride_), @@ -67,18 +96,35 @@ public: public: virtual ~VertexAttribute() = default; + /// @brief Get the index of the vertex attribute int getIndex() const { return index; } + + /// @brief Set the index of the vertex attribute void setIndex(int value) { index = value; } + /// @brief Get the stride of the vertex attribute std::size_t getStride() const { return stride; } + /// @brief Get the count of vertex attribute items std::size_t getCount() const; + + /// @brief Get the data type of the vertex attribute AttributeDataType getDataType() const { return dataType; } + /// @brief Get a vertex attribute item + /// @param index index of the item + /// @return ElementType reference to the item const ElementType& get(std::size_t i) const { return items[i]; } + /// @brief Reserves space for a number of items + /// @param count Number of items to reserve memory for void reserve(std::size_t count) { items.reserve(count); } + /// @brief Set the value of an item + /// @tparam T item data type + /// @param i index of the item + /// @param value value to set + /// @return ElementType reference to item template const ElementType& set(std::size_t i, T value) { if (items.size() < i + 1) { @@ -98,6 +144,11 @@ public: } return items[i] = value; } + + /// @brief Set the value of an item + /// @param i index of the item + /// @param value value to set + /// @return ElementType reference to item const ElementType& setVariant(std::size_t i, const ElementType& value) { if (items.size() < i + 1) { items.resize(std::max(items.size(), i + 1)); @@ -106,6 +157,7 @@ public: return items[i] = value; } + /// @brief Clear the items void clear() { if (!items.empty()) { setDirty(); @@ -113,12 +165,16 @@ public: items.clear(); } + /// @brief Get dirty state bool isDirty() const { return dirty; } + + /// @brief Set dirty state void setDirty(bool value = true) { dirty = value; } template inline typename std::enable_if::type set(std::size_t, std::tuple, std::size_t) {} + /// Set item value template inline typename std::enable_if < I::type set(std::size_t i, std::tuple tuple, std::size_t tupleIndex) { @@ -129,14 +185,31 @@ public: } } + /// Get the vertex attribute's raw data std::vector& getRawData() { return rawData; } const std::vector& getRawData() const { return rawData; } + /// Get the vertex attribute's shared raw data const std::shared_ptr& getSharedRawData() const { return sharedRawData; } + + /// Get the vertex attribute's shared data type AttributeDataType getSharedType() const { return sharedType; } + + /// Get the vertex attribute's shared data offset uint32_t getSharedOffset() const { return sharedOffset; } + + /// Get the vertex attribute's shared vertex offset uint32_t getSharedVertexOffset() const { return sharedVertexOffset; } + + /// Get the vertex attribute's shared data stride uint32_t getSharedStride() const { return sharedStride; } + + /// @brief Set the vertex attribute's shared data + /// @param data shared pointer to vertex vector + /// @param offset Offset into the source vertex layout structure + /// @param vertexOffset Offset into the source vertex array + /// @param stride Stride of the source vertex structure + /// @param type Type of the attribute void setSharedRawData(std::shared_ptr data, uint32_t offset, uint32_t vertexOffset, @@ -148,6 +221,8 @@ public: sharedVertexOffset = vertexOffset; sharedStride = stride_; } + + /// @brief Clears the shared data void resetSharedRawData() { sharedRawData.reset(); } /// Convert from the odd partially-normalized color component array produced by `Color::toArray` into normalized @@ -247,17 +322,20 @@ public: attrs.begin(), attrs.end(), [](const auto& kv) { return kv.second && kv.second->isDirty(); }); } + /// Clear the collection void clear(); /// Do something with each attribute - void observeAttributes(const std::function& f) { + void visitAttributes(const std::function& f) { std::for_each(attrs.begin(), attrs.end(), [&](const auto& kv) { if (kv.second) { f(kv.first, *kv.second); } }); } - void observeAttributes(const std::function& f) const { + + /// Do something with each attribute + void visitAttributes(const std::function& f) const { std::for_each(attrs.begin(), attrs.end(), [&](const auto& kv) { if (kv.second) { f(kv.first, *kv.second); @@ -273,12 +351,14 @@ public: VertexAttributeArray& operator=(VertexAttributeArray&&); VertexAttributeArray& operator=(const VertexAttributeArray&); + /// Clone the collection virtual std::unique_ptr clone() const { auto newAttrs = std::make_unique(); newAttrs->copy(*this); return newAttrs; } + /// Specialized DataDrivenPaintProperty reader template std::vector readDataDrivenPaintProperties(const Binders& binders, const Evaluated& evaluated) { std::vector propertiesAsUniforms; @@ -323,6 +403,7 @@ public: return propertiesAsUniforms; } + /// Copy another collection into this one virtual void copy(const VertexAttributeArray& other) { for (const auto& kv : other.attrs) { if (kv.second) { @@ -356,4 +437,4 @@ protected: }; } // namespace gfx -} // namespace mbgl +} // namespace mbgl \ No newline at end of file diff --git a/include/mbgl/renderer/layer_group.hpp b/include/mbgl/renderer/layer_group.hpp index aa9e80811..dd708f8ea 100644 --- a/include/mbgl/renderer/layer_group.hpp +++ b/include/mbgl/renderer/layer_group.hpp @@ -45,16 +45,24 @@ public: bool getEnabled() const { return enabled; } void setEnabled(bool value) { enabled = value; } + /// Get the name of the layer group const std::string& getName() const { return name; } + /// Set the name of the layer group void setName(std::string value) { name = std::move(value); } + /// Get the layer index int32_t getLayerIndex() const { return layerIndex; } + /// Update the layer index to a new value void updateLayerIndex(int32_t value) { layerIndex = value; } + /// Get the number of drawables contained virtual std::size_t getDrawableCount() const = 0; + + /// Whether the number of drawables contained is zero bool empty() const { return getDrawableCount() == 0; } + /// Clear the drawable collection virtual std::size_t clearDrawables() = 0; /// Add a drawable @@ -70,13 +78,13 @@ public: virtual void postRender(RenderOrchestrator&, PaintParameters&) {} /// Call the provided function for each drawable in priority order - virtual std::size_t observeDrawables(const std::function&&) = 0; - virtual std::size_t observeDrawables(const std::function&&) const = 0; + virtual std::size_t visitDrawables(const std::function&&) = 0; + virtual std::size_t visitDrawables(const std::function&&) const = 0; /// Call the provided function for each drawable in undefined order, allowing for removal. - /// @param f A function called with each drawable, returning true to keep and false to discard it. + /// @param f A function called with each drawable, returning true to discard it and false to keep it /// @return The number of items removed - virtual std::size_t observeDrawablesRemove(const std::function&& f) = 0; + virtual std::size_t removeDrawablesIf(const std::function&& f) = 0; /// Attach a tweaker to be run on this layer group for each frame void setLayerTweaker(LayerTweakerPtr tweaker) { layerTweaker = std::move(tweaker); } @@ -105,17 +113,15 @@ public: std::vector removeDrawables(mbgl::RenderPass, const OverscaledTileID&); void addDrawable(mbgl::RenderPass, const OverscaledTileID&, gfx::UniqueDrawable&&); - std::size_t observeDrawables(const std::function&&) override; - std::size_t observeDrawables(const std::function&&) const override; - std::size_t observeDrawablesRemove(const std::function&&) override; + std::size_t visitDrawables(const std::function&&) override; + std::size_t visitDrawables(const std::function&&) const override; + std::size_t removeDrawablesIf(const std::function&&) override; /// Call the provided function for each drawable for the given tile - std::size_t observeDrawables(mbgl::RenderPass, - const OverscaledTileID&, - const std::function&&); - std::size_t observeDrawables(mbgl::RenderPass, - const OverscaledTileID&, - const std::function&&) const; + std::size_t visitDrawables(mbgl::RenderPass, const OverscaledTileID&, const std::function&&); + std::size_t visitDrawables(mbgl::RenderPass, + const OverscaledTileID&, + const std::function&&) const; std::size_t clearDrawables() override; @@ -138,9 +144,9 @@ public: std::vector removeDrawables(mbgl::RenderPass); void addDrawable(gfx::UniqueDrawable&&); - std::size_t observeDrawables(const std::function&&) override; - std::size_t observeDrawables(const std::function&&) const override; - std::size_t observeDrawablesRemove(const std::function&&) override; + std::size_t visitDrawables(const std::function&&) override; + std::size_t visitDrawables(const std::function&&) const override; + std::size_t removeDrawablesIf(const std::function&&) override; std::size_t clearDrawables() override; diff --git a/include/mbgl/renderer/render_target.hpp b/include/mbgl/renderer/render_target.hpp index 2219c820d..886758fcf 100644 --- a/include/mbgl/renderer/render_target.hpp +++ b/include/mbgl/renderer/render_target.hpp @@ -20,21 +20,43 @@ class RenderTree; using LayerGroupBasePtr = std::shared_ptr; +/// Render target class class RenderTarget { public: virtual ~RenderTarget() = default; + /// Set the render target texture void setTexture(const gfx::Texture2DPtr& texture_) { texture = std::move(texture_); }; + + /// Get the render target texture const gfx::Texture2DPtr& getTexture() const { return texture; }; + /// @brief Add a layer group to the render target + /// @param replace Flag to replace if exists + /// @return whether added bool addLayerGroup(LayerGroupBasePtr, bool replace); - bool removeLayerGroup(const int32_t layerIndex); - size_t numLayerGroups() const noexcept; - const LayerGroupBasePtr& getLayerGroup(const int32_t layerIndex) const; - void observeLayerGroups(std::function); - void observeLayerGroups(std::function) const; + /// @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 existant, othewise a shared null pointer + const LayerGroupBasePtr& getLayerGroup(const int32_t layerIndex) const; + + /// Execute the given function for each contained layer group + void visitLayerGroups(std::function); + void visitLayerGroups(std::function) const; + + /// Upload the layer groups virtual void upload(gfx::UploadPass& uploadPass) = 0; + + /// Render the layer groups virtual void render(RenderOrchestrator&, const RenderTree&, PaintParameters&) = 0; protected: diff --git a/src/mbgl/gl/layer_group_gl.cpp b/src/mbgl/gl/layer_group_gl.cpp index 90ccb66b7..d8df4c621 100644 --- a/src/mbgl/gl/layer_group_gl.cpp +++ b/src/mbgl/gl/layer_group_gl.cpp @@ -21,7 +21,7 @@ void TileLayerGroupGL::upload(gfx::UploadPass& uploadPass) { return; } - observeDrawables([&](gfx::Drawable& drawable) { + visitDrawables([&](gfx::Drawable& drawable) { if (!drawable.getEnabled()) { return; } @@ -64,7 +64,7 @@ void TileLayerGroupGL::render(RenderOrchestrator&, PaintParameters& parameters) // Collect the tile IDs relevant to stenciling and update the stencil buffer, if necessary. std::set tileIDs; - observeDrawables([&](const gfx::Drawable& drawable) { + visitDrawables([&](const gfx::Drawable& drawable) { if (!drawable.getEnabled() || !drawable.hasRenderPass(parameters.pass)) { return; } @@ -95,7 +95,7 @@ void TileLayerGroupGL::render(RenderOrchestrator&, PaintParameters& parameters) const auto debugGroupRender = parameters.encoder->createDebugGroup(label_render.c_str()); #endif - observeDrawables([&](gfx::Drawable& drawable) { + visitDrawables([&](gfx::Drawable& drawable) { if (!drawable.getEnabled() || !drawable.hasRenderPass(parameters.pass)) { return; } @@ -132,7 +132,7 @@ void LayerGroupGL::upload(gfx::UploadPass& uploadPass) { return; } - observeDrawables([&](gfx::Drawable& drawable) { + visitDrawables([&](gfx::Drawable& drawable) { if (!drawable.getEnabled()) { return; } @@ -152,7 +152,7 @@ void LayerGroupGL::render(RenderOrchestrator&, PaintParameters& parameters) { return; } - observeDrawables([&](gfx::Drawable& drawable) { + visitDrawables([&](gfx::Drawable& drawable) { if (!drawable.getEnabled() || !drawable.hasRenderPass(parameters.pass)) { return; } diff --git a/src/mbgl/gl/render_target_gl.cpp b/src/mbgl/gl/render_target_gl.cpp index e319f5f00..03621b5bd 100644 --- a/src/mbgl/gl/render_target_gl.cpp +++ b/src/mbgl/gl/render_target_gl.cpp @@ -34,7 +34,7 @@ RenderTargetGL::~RenderTargetGL() { } void RenderTargetGL::upload(gfx::UploadPass& uploadPass) { - observeLayerGroups(([&](LayerGroupBase& layerGroup) { layerGroup.upload(uploadPass); })); + visitLayerGroups(([&](LayerGroupBase& layerGroup) { layerGroup.upload(uploadPass); })); } void RenderTargetGL::render(RenderOrchestrator& orchestrator, @@ -52,7 +52,7 @@ void RenderTargetGL::render(RenderOrchestrator& orchestrator, glContext.clear(Color{0.0f, 0.0f, 0.0f, 1.0f}, {}, {}); // Run layer tweakers to update any dynamic elements - observeLayerGroups([&](LayerGroupBase& layerGroup) { + visitLayerGroups([&](LayerGroupBase& layerGroup) { if (layerGroup.getLayerTweaker()) { layerGroup.getLayerTweaker()->execute(layerGroup, renderTree, parameters); } @@ -63,7 +63,7 @@ void RenderTargetGL::render(RenderOrchestrator& orchestrator, parameters.currentLayer = 0; parameters.depthRangeSize = 1 - (numLayerGroups() + 2) * parameters.numSublayers * PaintParameters::depthEpsilon; - observeLayerGroups([&](LayerGroupBase& layerGroup) { + visitLayerGroups([&](LayerGroupBase& layerGroup) { layerGroup.render(orchestrator, parameters); parameters.currentLayer++; }); @@ -73,7 +73,7 @@ void RenderTargetGL::render(RenderOrchestrator& orchestrator, parameters.currentLayer = static_cast(numLayerGroups()) - 1; parameters.depthRangeSize = 1 - (numLayerGroups() + 2) * parameters.numSublayers * PaintParameters::depthEpsilon; - observeLayerGroups([&](LayerGroupBase& layerGroup) { + visitLayerGroups([&](LayerGroupBase& layerGroup) { layerGroup.render(orchestrator, parameters); if (parameters.currentLayer != 0) { parameters.currentLayer--; diff --git a/src/mbgl/renderer/layer_group.cpp b/src/mbgl/renderer/layer_group.cpp index e621bb870..cc9ffdb97 100644 --- a/src/mbgl/renderer/layer_group.cpp +++ b/src/mbgl/renderer/layer_group.cpp @@ -36,7 +36,7 @@ void LayerGroup::addDrawable(gfx::UniqueDrawable&& drawable) { impl->drawables.emplace(std::move(drawable)); } -std::size_t LayerGroup::observeDrawables(const std::function&& f) { +std::size_t LayerGroup::visitDrawables(const std::function&& f) { for (const auto& item : impl->drawables) { if (item) { f(*item); @@ -45,7 +45,7 @@ std::size_t LayerGroup::observeDrawables(const std::functiondrawables.size(); } -std::size_t LayerGroup::observeDrawables(const std::function&& f) const { +std::size_t LayerGroup::visitDrawables(const std::function&& f) const { for (const auto& item : impl->drawables) { if (item) { f(*item); @@ -54,13 +54,13 @@ std::size_t LayerGroup::observeDrawables(const std::functiondrawables.size(); } -std::size_t LayerGroup::observeDrawablesRemove(const std::function&& f) { +std::size_t LayerGroup::removeDrawablesIf(const std::function&& f) { decltype(impl->drawables) newSet; const auto oldSize = impl->drawables.size(); while (!impl->drawables.empty()) { // set members are immutable, since changes could affect its position, so extract each item gfx::UniqueDrawable drawable = std::move(impl->drawables.extract(impl->drawables.begin()).value()); - if (f(*drawable)) { + if (!f(*drawable)) { // Not removed, keep it, but in a new set so that if the key value // has increased, we don't see it again during this iteration. newSet.emplace_hint(newSet.end(), std::move(drawable)); diff --git a/src/mbgl/renderer/render_target.cpp b/src/mbgl/renderer/render_target.cpp index e0ee3d659..2dfa6b048 100644 --- a/src/mbgl/renderer/render_target.cpp +++ b/src/mbgl/renderer/render_target.cpp @@ -44,7 +44,7 @@ const LayerGroupBasePtr& RenderTarget::getLayerGroup(const int32_t layerIndex) c return (hit == layerGroupsByLayerIndex.end()) ? no_group : hit->second; } -void RenderTarget::observeLayerGroups(std::function f) { +void RenderTarget::visitLayerGroups(std::function f) { for (auto& pair : layerGroupsByLayerIndex) { if (pair.second) { f(*pair.second); @@ -52,7 +52,7 @@ void RenderTarget::observeLayerGroups(std::function f) { } } -void RenderTarget::observeLayerGroups(std::function f) const { +void RenderTarget::visitLayerGroups(std::function f) const { for (const auto& pair : layerGroupsByLayerIndex) { if (pair.second) { f(*pair.second); diff --git a/src/mbgl/renderer/tile_layer_group.cpp b/src/mbgl/renderer/tile_layer_group.cpp index 4d670988e..a81b374c3 100644 --- a/src/mbgl/renderer/tile_layer_group.cpp +++ b/src/mbgl/renderer/tile_layer_group.cpp @@ -84,7 +84,7 @@ void TileLayerGroup::addDrawable(mbgl::RenderPass pass, const OverscaledTileID& } } -std::size_t TileLayerGroup::observeDrawables(const std::function&& f) { +std::size_t TileLayerGroup::visitDrawables(const std::function&& f) { assert(impl->drawablesByTile.size() == impl->sortedDrawables.size()); for (auto* drawable : impl->sortedDrawables) { f(*drawable); @@ -92,7 +92,7 @@ std::size_t TileLayerGroup::observeDrawables(const std::functionsortedDrawables.size(); } -std::size_t TileLayerGroup::observeDrawables(const std::function&& f) const { +std::size_t TileLayerGroup::visitDrawables(const std::function&& f) const { assert(impl->drawablesByTile.size() == impl->sortedDrawables.size()); for (const auto* drawable : impl->sortedDrawables) { f(*drawable); @@ -100,11 +100,11 @@ std::size_t TileLayerGroup::observeDrawables(const std::functionsortedDrawables.size(); } -std::size_t TileLayerGroup::observeDrawablesRemove(const std::function&& f) { +std::size_t TileLayerGroup::removeDrawablesIf(const std::function&& f) { const auto oldSize = impl->drawablesByTile.size(); for (auto i = impl->drawablesByTile.begin(); i != impl->drawablesByTile.end();) { auto& drawable = i->second; - if (f(*drawable)) { + if (!f(*drawable)) { // Not removed, keep going ++i; } else { @@ -117,18 +117,18 @@ std::size_t TileLayerGroup::observeDrawablesRemove(const std::functiondrawablesByTile.size()); } -std::size_t TileLayerGroup::observeDrawables(mbgl::RenderPass pass, - const OverscaledTileID& tileID, - const std::function&& f) { +std::size_t TileLayerGroup::visitDrawables(mbgl::RenderPass pass, + const OverscaledTileID& tileID, + const std::function&& f) { assert(impl->drawablesByTile.size() == impl->sortedDrawables.size()); const auto range = impl->drawablesByTile.equal_range({pass, tileID}); std::for_each(range.first, range.second, [&f](const auto& pair) { f(*pair.second); }); return std::distance(range.first, range.second); } -std::size_t TileLayerGroup::observeDrawables(mbgl::RenderPass pass, - const OverscaledTileID& tileID, - const std::function&& f) const { +std::size_t TileLayerGroup::visitDrawables(mbgl::RenderPass pass, + const OverscaledTileID& tileID, + const std::function&& f) const { assert(impl->drawablesByTile.size() == impl->sortedDrawables.size()); const auto range = impl->drawablesByTile.equal_range({pass, tileID}); std::for_each(range.first, range.second, [&f](const auto& pair) { f(*pair.second); });