d66549215c3f45ddbc5694a588292b1f8a14a948
[platform/core/uifw/dali-core.git] / dali / internal / render / renderers / uniform-buffer.h
1 #ifndef DALI_INTERNAL_UNIFORM_BUFFER_H
2 #define DALI_INTERNAL_UNIFORM_BUFFER_H
3
4 /*
5  * Copyright (c) 2021 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 // INTERNAL INCLUDES
22 #include <dali/graphics-api/graphics-controller.h>
23
24 // EXTERNAL INCLUDES
25 #include <memory>
26
27 namespace Dali::Internal::Render
28 {
29 /**
30  * Class UniformBuffer
31  *
32  * The class wraps one or more Graphics::Buffer objects and creates
33  * a continuous memory to store uniforms. The UniformBuffer may
34  * reallocate and merge individual Graphics::Buffer objects into one.
35  *
36  * From the client side, the UBO memory is continuous and individual
37  * Graphics::Buffer objects are not visible.
38  */
39 class UniformBuffer
40 {
41   friend class UniformBufferManager;
42   friend class UniformBufferView;
43   friend class UniformBufferViewPool;
44
45 private:
46   /**
47    * Constructor of UniformBuffer
48    *
49    * @param[in] mController Pointer of the graphics controller
50    * @param[in] sizeInBytes initial size of allocated buffer
51    * @param[in] alignment memory alignment in bytes
52    * @param[in] persistentMappingEnabled if true, buffer is mapped persistently
53    * @param[in] usageFlags type of usage ( Graphics::BufferUsage )
54    */
55   UniformBuffer(Dali::Graphics::Controller* mController,
56                 uint32_t                    sizeInBytes,
57                 uint32_t                    alignment,
58                 bool                        persistentMappingEnabled,
59                 Graphics::BufferUsageFlags  usageFlags);
60
61 public:
62   /**
63    * Destructor of UniformBuffer
64    */
65   ~UniformBuffer();
66
67   /**
68    * Writes data into the buffer
69    *
70    * @param[in] data pointer to the source data
71    * @param[in] size size of source data
72    * @param[in] offset destination offset
73    */
74   void Write(const void* data, uint32_t size, uint32_t offset);
75
76   /**
77    * Flushes whole buffer range
78    *
79    * @param[in] bufferIndex Index of Graphics::Buffer
80    */
81   void Flush( uint32_t bufferIndex = 0);
82
83   /**
84    * Returns allocated ( requested ) size
85    * @return size of buffer
86    */
87   [[nodiscard]] uint32_t GetSize() const
88   {
89     return mSize;
90   }
91
92   /**
93    * Return Graphics::Buffer object at specified array index
94    *
95    * @param[in] bufferIndex index of Graphics buffer
96    *
97    * @return pointer to the buffer object
98    */
99   [[nodiscard]] Dali::Graphics::Buffer* GetBuffer( uint32_t bufferIndex ) const
100   {
101     return mBuffers[bufferIndex].buffer.get();
102   }
103
104   /**
105    * Maps individual Graphics buffer memory
106    *
107    * @param[in] bufferIndex index of Graphics buffer
108    */
109   void Map( uint32_t bufferIndex = 0);
110
111   /**
112    * Unmaps individual Graphics buffer memory
113    *
114    * @param[in] bufferIndex index of Graphics buffer
115    */
116   void Unmap( uint32_t bufferIndex = 0);
117
118   /**
119    * Resizes the buffer
120    *
121    * The resize strategy depends on 'invalidate' parameter.
122    *
123    * If 'invalidate' is true, all the content if the buffer
124    * is discarded, the individual Graphics::Buffers are deleted
125    * and a single Graphics::Buffer is allocated.
126    *
127    * If 'invalidate' is false, additional Graphics::Buffer
128    * is created and all recorded content is kept unchanged.
129    *
130    * @param[in] newSize new size of UniformBuffer
131    * @param[in] invalidate specifies whether the content should be discarded
132    *
133    */
134   void Resize( uint32_t newSize, bool invalidate );
135
136 private:
137
138   /**
139    * GfxBuffer wraps single GPU buffer and encapsulates individual
140    * buffer mapping and create info details.
141    *
142    * The array of GfxBuffers makes a single UniformBuffer.
143    */
144   struct GfxBuffer
145   {
146     GfxBuffer() = default;
147     ~GfxBuffer() = default;
148     GfxBuffer( GfxBuffer&& ) = default;
149     GfxBuffer(Graphics::UniquePtr<Graphics::Buffer>&& b, const Graphics::BufferCreateInfo& i) :
150       buffer(std::move(b)),
151       createInfo(i)
152     {
153     }
154
155     Graphics::UniquePtr<Graphics::Buffer> buffer{}; ///< Graphics buffer
156     Graphics::UniquePtr<Graphics::Memory> memory{}; ///< Mapped memory associated with buffer
157     Graphics::BufferCreateInfo createInfo{}; ///< create info describing the buffer
158     void* mappedPtr{}; ///< Mapped pointer (if mapped)
159     bool needsUpdate{true}; ///< Indicates whether the buffer needs flushing the queue
160   };
161
162   /**
163    * Returns GfxBuffer object by offset
164    *
165    * The memory of UniformBuffer is considered to be continuous, however,
166    * it may contain multiple graphics buffers.
167    *
168    */
169   const GfxBuffer* GetBufferByOffset( uint32_t offset, uint32_t* newOffset, uint32_t* bufferIndex ) const;
170
171   std::vector<GfxBuffer> mBuffers; ///< List of GfxBuffer objects
172
173   Dali::Graphics::Controller* mController; ///< Pointer to the controller
174
175   uint32_t mSize;        ///< Current size of buffer
176   uint32_t mAlignment{0}; ///< Buffer alignment
177
178   Graphics::BufferUsageFlags mUsageFlags;
179
180   bool mValid{false};
181 };
182
183 }
184
185 #endif //DALI_INTERNAL_UNIFORM_BUFFER_H