Geometry Batching
[platform/core/uifw/dali-core.git] / dali / internal / render / renderers / render-property-buffer.h
1 #ifndef DALI_INTERNAL_RENDER_PROPERTY_BUFFER_H
2 #define DALI_INTERNAL_RENDER_PROPERTY_BUFFER_H
3
4 /*
5  * Copyright (c) 2016 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 #include <dali/public-api/actors/sampling.h>
20 #include <dali/public-api/common/vector-wrapper.h>
21 #include <dali/public-api/rendering/sampler.h>
22 #include <dali/internal/common/owner-pointer.h>
23 #include <dali/internal/render/renderers/render-sampler.h>
24 #include <dali/internal/render/gl-resources/gpu-buffer.h>
25
26 namespace Dali
27 {
28 namespace Internal
29 {
30 namespace Render
31 {
32
33 class PropertyBuffer
34 {
35 public:
36
37   struct Component
38   {
39     std::string     name;
40     unsigned int    offset;
41     unsigned int    size;
42     Property::Type  type;
43   };
44
45   /**
46    * Structure that holds the meta-data of the format of PropertyBuffer.
47    */
48   struct Format
49   {
50     std::vector<Component> components;
51     unsigned int           size;
52   };
53
54   /**
55    * @brief Default constructor
56    */
57   PropertyBuffer();
58
59   /**
60    * @brief Destructor
61    */
62   ~PropertyBuffer();
63
64   /**
65    * @brief Set the format of the buffer
66    *
67    * This function takes ownership of the pointer
68    *
69    * @param[in] format The format for the PropertyBuffer
70    */
71   void SetFormat( PropertyBuffer::Format* format );
72
73   /**
74    * @brief Set the data of the PropertyBuffer
75    *
76    * This function takes ownership of the pointer
77    * @param[in] data The new data of the PropertyBuffer
78    * @param[in] size The new size of the buffer
79    */
80   void SetData( Dali::Vector<char>* data, size_t size );
81
82   /**
83    * @brief Sets flag to update data in the buffer when next PropertyBuffer::Update()
84    * is called.
85    */
86   void UpdateData();
87
88   /**
89    * @brief Set the number of elements
90    * @param[in] size The number of elements
91    */
92   void SetSize( unsigned int size );
93
94   /**
95    * @brief Bind the property buffer
96    * @param[in] target The binding point
97    */
98   void BindBuffer(GpuBuffer::Target target);
99
100   /**
101    * Perform the upload of the buffer only when requiered
102    * @param[in] context The GL context
103    */
104   bool Update( Context& context );
105
106   /**
107    * Enable the vertex attributes for each vertex buffer from the corresponding
108    * shader program.
109    * @param[in] context The GL context
110    * @param[in] vAttributeLocation Vector containing attributes location for current program
111    * @param[in] locationBase Index in vAttributeLocation corresponding to the first attribute defined by this buffer
112    */
113   unsigned int EnableVertexAttributes( Context& context, Vector<GLint>& vAttributeLocation, unsigned int locationBase );
114
115   /**
116    * Get the number of attributes present in the buffer
117    * @return The number of attributes stored in this buffer
118    */
119   inline unsigned int GetAttributeCount() const
120   {
121     DALI_ASSERT_DEBUG( mFormat && "Format should be set ");
122     return mFormat->components.size();
123   }
124
125   /**
126    * Retrieve the i-essim attribute name
127    * @param[in] index The index of the attribute
128    * @return The name of the attribute
129    */
130   inline const std::string& GetAttributeName(unsigned int index ) const
131   {
132     DALI_ASSERT_DEBUG( mFormat && "Format should be set ");
133     return mFormat->components[index].name;
134   }
135
136   /**
137    * Retrieve the size of the buffer in bytes
138    * @return The total size of the buffer
139    */
140   inline std::size_t GetDataSize() const
141   {
142     DALI_ASSERT_DEBUG( mFormat && "Format should be set ");
143     return mFormat->size * mSize;
144   }
145
146   /**
147    * Retrieve the size of one element of the buffer
148    * @return The size of one element
149    */
150   inline std::size_t GetElementSize() const
151   {
152     DALI_ASSERT_DEBUG( mFormat && "Format should be set ");
153     return mFormat->size;
154   }
155
156   /**
157    * Retrieve the number of elements in the buffer
158    * @return The total number of elements
159    */
160   inline unsigned int GetElementCount() const
161   {
162     return mSize;
163   }
164
165   /**
166    * Retrieve reference to the data storage vector
167    * @return Reference to the data storage
168    */
169   inline const Dali::Vector< char >& GetData() const
170   {
171     return *mData.Get();
172   }
173
174   /**
175    * Retrieve data writeable pointer ( direct access to the buffer data )
176    * @return Pointer to data converted to requested type
177    */
178   template <typename T>
179   inline T* GetDataTypedPtr()
180   {
181     Dali::Vector< char >* data = mData.Release();
182     mData = data;
183     return reinterpret_cast<T*>( &data->operator[]( 0 ) );
184   }
185
186   inline const PropertyBuffer::Format* GetFormat() const
187   {
188     return mFormat.Get();
189   }
190
191 private:
192   OwnerPointer< PropertyBuffer::Format >  mFormat;    ///< Format of the buffer
193   OwnerPointer< Dali::Vector< char > >    mData;      ///< Data
194   OwnerPointer< GpuBuffer >               mGpuBuffer; ///< Pointer to the GpuBuffer associated with this RenderPropertyBuffer
195
196   size_t mSize;       ///< Number of Elements in the buffer
197   bool mDataChanged;  ///< Flag to know if data has changed in a frame
198 };
199
200 } // namespace Render
201
202 } // namespace Internal
203
204 } // namespace Dali
205
206
207 #endif //  DALI_INTERNAL_RENDER_PROPERTY_BUFFER_H