1 #ifndef DALI_INTERNAL_UNIFORM_BUFFER_H
2 #define DALI_INTERNAL_UNIFORM_BUFFER_H
5 * Copyright (c) 2021 Samsung Electronics Co., Ltd.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <dali/graphics-api/graphics-controller.h>
27 namespace Dali::Internal::Render
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.
36 * From the client side, the UBO memory is continuous and individual
37 * Graphics::Buffer objects are not visible.
41 friend class UniformBufferManager;
42 friend class UniformBufferView;
43 friend class UniformBufferViewPool;
47 * Constructor of UniformBuffer
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 )
55 UniformBuffer(Dali::Graphics::Controller* mController,
58 bool persistentMappingEnabled,
59 Graphics::BufferUsageFlags usageFlags);
63 * Destructor of UniformBuffer
68 * Writes data into the buffer
70 * @param[in] data pointer to the source data
71 * @param[in] size size of source data
72 * @param[in] offset destination offset
74 void Write(const void* data, uint32_t size, uint32_t offset);
77 * Flushes whole buffer range
79 * @param[in] bufferIndex Index of Graphics::Buffer
81 void Flush( uint32_t bufferIndex = 0);
84 * Returns allocated ( requested ) size
85 * @return size of buffer
87 [[nodiscard]] uint32_t GetSize() const
93 * Return Graphics::Buffer object at specified array index
95 * @param[in] bufferIndex index of Graphics buffer
97 * @return pointer to the buffer object
99 [[nodiscard]] Dali::Graphics::Buffer* GetBuffer( uint32_t bufferIndex ) const
101 return mBuffers[bufferIndex].buffer.get();
105 * Maps individual Graphics buffer memory
107 * @param[in] bufferIndex index of Graphics buffer
109 void Map( uint32_t bufferIndex = 0);
112 * Unmaps individual Graphics buffer memory
114 * @param[in] bufferIndex index of Graphics buffer
116 void Unmap( uint32_t bufferIndex = 0);
121 * The resize strategy depends on 'invalidate' parameter.
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.
127 * If 'invalidate' is false, additional Graphics::Buffer
128 * is created and all recorded content is kept unchanged.
130 * @param[in] newSize new size of UniformBuffer
131 * @param[in] invalidate specifies whether the content should be discarded
134 void Resize( uint32_t newSize, bool invalidate );
139 * GfxBuffer wraps single GPU buffer and encapsulates individual
140 * buffer mapping and create info details.
142 * The array of GfxBuffers makes a single UniformBuffer.
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)),
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
163 * Returns GfxBuffer object by offset
165 * The memory of UniformBuffer is considered to be continuous, however,
166 * it may contain multiple graphics buffers.
169 const GfxBuffer* GetBufferByOffset( uint32_t offset, uint32_t* newOffset, uint32_t* bufferIndex ) const;
171 std::vector<GfxBuffer> mBuffers; ///< List of GfxBuffer objects
173 Dali::Graphics::Controller* mController; ///< Pointer to the controller
175 uint32_t mSize; ///< Current size of buffer
176 uint32_t mAlignment{0}; ///< Buffer alignment
178 Graphics::BufferUsageFlags mUsageFlags;
185 #endif //DALI_INTERNAL_UNIFORM_BUFFER_H