7300a38f02b2924d7f0497a4fcfeb7ed75f2d0c0
[platform/core/uifw/dali-core.git] / dali / public-api / rendering / vertex-buffer.h
1 #ifndef DALI_VERTEX_BUFFER_H
2 #define DALI_VERTEX_BUFFER_H
3
4 /*
5  * Copyright (c) 2023 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 // EXTERNAL INCLUDES
22 #include <cstddef> // std::size_t
23 #include <string>  // std::string
24
25 // INTERNAL INCLUDES
26 #include <dali/public-api/object/handle.h>       // Dali::Handle
27 #include <dali/public-api/object/property-map.h> // Dali::Property::Map
28
29 namespace Dali
30 {
31 /**
32  * @addtogroup dali_core_rendering_effects
33  * @{
34  */
35
36 namespace Internal DALI_INTERNAL
37 {
38 class VertexBuffer;
39 }
40
41 /**
42  * @brief VertexBuffer is a handle to an object that contains a buffer of structured data.
43  *
44  * VertexBuffers can be used to provide data to Geometry objects.
45  *
46  * Example:
47  *
48  *  const float halfQuadSize = .5f;
49  *  struct TexturedQuadVertex { Vector2 position; Vector2 textureCoordinates; };
50  *  TexturedQuadVertex texturedQuadVertexData[4] = {
51  *    { Vector2(-halfQuadSize, -halfQuadSize), Vector2(0.f, 0.f) },
52  *    { Vector2( halfQuadSize, -halfQuadSize), Vector2(1.f, 0.f) },
53  *    { Vector2(-halfQuadSize,  halfQuadSize), Vector2(0.f, 1.f) },
54  *    { Vector2( halfQuadSize,  halfQuadSize), Vector2(1.f, 1.f) } };
55  *
56  *  Property::Map texturedQuadVertexFormat;
57  *  texturedQuadVertexFormat["aPosition"] = Property::VECTOR2;
58  *  texturedQuadVertexFormat["aTexCoord"] = Property::VECTOR2;
59  *  VertexBuffer texturedQuadVertices = VertexBuffer::New( texturedQuadVertexFormat );
60  *  texturedQuadVertices.SetData( texturedQuadVertexData, 4 );
61  *
62  *  // Create indices
63  *  uint32_t indexData[6] = { 0, 3, 1, 0, 2, 3 };
64  *
65  *  // Create the geometry object
66  *  Geometry texturedQuadGeometry = Geometry::New();
67  *  texturedQuadGeometry.AddVertexBuffer( texturedQuadVertices );
68  *  texturedQuadGeometry.SetIndexBuffer( indexData, sizeof(indexData)/sizeof(indexData[0] );
69  *
70  * @SINCE_1_1.43
71  */
72 class DALI_CORE_API VertexBuffer : public BaseHandle
73 {
74 public:
75   /**
76    * @brief Creates a VertexBuffer.
77    *
78    * @SINCE_1_9.27
79    * @param[in] bufferFormat Map of names and types that describes the components of the buffer
80    * @return Handle to a newly allocated VertexBuffer
81    */
82   static VertexBuffer New(Dali::Property::Map& bufferFormat);
83
84   /**
85    * @brief Default constructor, creates an empty handle.
86    *
87    * @SINCE_1_9.27
88    */
89   VertexBuffer();
90
91   /**
92    * @brief Destructor.
93    *
94    * @SINCE_1_9.27
95    */
96   ~VertexBuffer();
97
98   /**
99    * @brief Copy constructor, creates a new handle to the same object.
100    *
101    * @SINCE_1_9.27
102    * @param[in] handle Handle to an object
103    */
104   VertexBuffer(const VertexBuffer& handle);
105
106   /**
107    * @brief Downcasts to a property buffer handle.
108    * If not, a property buffer the returned property buffer handle is left uninitialized.
109    *
110    * @SINCE_1_9.27
111    * @param[in] handle Handle to an object
112    * @return Property buffer handle or an uninitialized handle
113    */
114   static VertexBuffer DownCast(BaseHandle handle);
115
116   /**
117    * @brief Assignment operator, changes this handle to point at the same object.
118    *
119    * @SINCE_1_9.27
120    * @param[in] handle Handle to an object
121    * @return Reference to the assigned object
122    */
123   VertexBuffer& operator=(const VertexBuffer& handle);
124
125   /**
126    * @brief Move constructor.
127    *
128    * @SINCE_1_9.27
129    * @param[in] rhs A reference to the moved handle
130    */
131   VertexBuffer(VertexBuffer&& rhs) noexcept;
132
133   /**
134    * @brief Move assignment operator.
135    *
136    * @SINCE_1_9.27
137    * @param[in] rhs A reference to the moved handle
138    * @return A reference to this handle
139    */
140   VertexBuffer& operator=(VertexBuffer&& rhs) noexcept;
141
142   /**
143    * @brief Updates the whole buffer information.
144    *
145    * This function expects a pointer to an array of structures with the same
146    * format that was given in the construction, and the number of elements to
147    * be the same as the size of the buffer.
148    *
149    * If the initial structure was: { { "position", VECTOR3}, { "uv", VECTOR2 } }
150    * and a size of 10 elements, this function should be called with a pointer equivalent to:
151    * <pre>
152    * struct Vertex {
153    *   Dali::Vector3 position;
154    *   Dali::Vector2 uv;
155    * };
156    * Vertex vertices[ 10 ] = { ... };
157    * vertexBuffer.SetData( vertices );
158    * </pre>
159    *
160    * @SINCE_1_9.27
161    * @param[in] data A pointer to the data that will be copied to the buffer
162    * @param[in] size Number of elements to expand or contract the buffer
163    */
164   void SetData(const void* data, std::size_t size);
165
166   /**
167    * @brief Gets the number of elements in the buffer.
168    *
169    * @SINCE_1_9.27
170    * @return Number of elements to expand or contract the buffer
171    */
172   std::size_t GetSize() const;
173
174   /**
175    * @brief Sets vertex divisor for all attributes
176    *
177    * If instancing isn't supported, the function has no effect.
178    * It's responsibility of developer to make sure the feature is supported.
179    * A divisor of 0 will turn off instanced drawing.
180    * Currently, a divisor > 1 will turn on instanced draw, but will have an
181    * actual rate of 1.
182    *
183    * @param[in] divisor Sets vertex buffer divisor for an instanced draw
184    */
185   void SetDivisor(uint32_t divisor);
186
187   /**
188    * @brief Get the divisor for the given attribute. A return value of 0 means that
189    * instancing is turned off.
190    *
191    * @return either 0 (not instanced), or > 0 (instanced)
192    */
193   uint32_t GetDivisor() const;
194
195 public:
196   /**
197    * @brief The constructor.
198    * @note  Not intended for application developers.
199    * @SINCE_1_9.27
200    * @param[in] pointer A pointer to a newly allocated VertexBuffer
201    */
202   explicit DALI_INTERNAL VertexBuffer(Internal::VertexBuffer* pointer);
203 };
204
205 /**
206  * @}
207  */
208 } // namespace Dali
209
210 #endif // DALI_VERTEX_BUFFER_H