1 #ifndef DALI_INTERNAL_UNIFORM_BUFFER_H
2 #define DALI_INTERNAL_UNIFORM_BUFFER_H
5 * Copyright (c) 2022 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] usageFlags type of usage ( Graphics::BufferUsage )
53 * @param[in] propertiesFlags buffer properties (Gracphis::BufferPropertiesFlags)
55 UniformBuffer(Dali::Graphics::Controller* mController,
58 Graphics::BufferUsageFlags usageFlags,
59 Graphics::BufferPropertiesFlags propertiesFlags);
63 * Destructor of UniformBuffer
68 * @brief Writes data into the buffer
69 * @note We prefer to call ReadyToLockUniformBuffer before call Write API.
70 * And also, prefer to call UnlockUniformBuffer if current frame's all Write API action done.
72 * @param[in] data pointer to the source data
73 * @param[in] size size of source data
74 * @param[in] offset destination offset
76 void Write(const void* data, uint32_t size, uint32_t offset);
79 * @brief Flushes whole buffer range
81 * @param[in] bufferIndex Index of Graphics::Buffer
83 void Flush(uint32_t bufferIndex = 0);
86 * @brief Returns allocated ( requested ) size
87 * @return size of buffer
89 [[nodiscard]] uint32_t GetSize() const
95 * @brief Return Graphics::Buffer object at specified array index
97 * @param[in] bufferIndex index of Graphics buffer
99 * @return pointer to the buffer object
101 [[nodiscard]] Dali::Graphics::Buffer* GetBuffer(uint32_t bufferIndex) const
103 return mBuffers[bufferIndex].buffer.get();
107 * @brief Maps individual Graphics buffer memory
109 * @param[in] bufferIndex index of Graphics buffer
111 void Map(uint32_t bufferIndex = 0);
114 * Unmaps individual Graphics buffer memory
116 * @param[in] bufferIndex index of Graphics buffer
118 void Unmap(uint32_t bufferIndex = 0);
121 * @brief Resizes the buffer
123 * The resize strategy depends on 'invalidate' parameter.
125 * If 'invalidate' is true, all the content if the buffer
126 * is discarded, the individual Graphics::Buffers are deleted
127 * and a single Graphics::Buffer is allocated.
129 * If 'invalidate' is false, additional Graphics::Buffer
130 * is created and all recorded content is kept unchanged.
132 * @param[in] newSize new size of UniformBuffer
133 * @param[in] invalidate specifies whether the content should be discarded
136 void Resize(uint32_t newSize, bool invalidate);
139 * @copydoc Dali::Internal::Render::UniformBufferViewPool::ReadyToLockUniformBuffer
141 void ReadyToLockUniformBuffer();
144 * @copydoc Dali::Internal::Render::UniformBufferViewPool::UnlockUniformBuffer
146 void UnlockUniformBuffer();
150 * @brief GfxBuffer wraps single GPU buffer and encapsulates individual
151 * buffer mapping and create info details.
153 * The array of GfxBuffers makes a single UniformBuffer.
157 GfxBuffer() = default;
158 ~GfxBuffer() = default;
159 GfxBuffer(GfxBuffer&&) = default;
160 GfxBuffer(Graphics::UniquePtr<Graphics::Buffer>&& b, const Graphics::BufferCreateInfo& i)
161 : buffer(std::move(b)),
166 Graphics::UniquePtr<Graphics::Buffer> buffer{}; ///< Graphics buffer
167 Graphics::UniquePtr<Graphics::Memory> memory{}; ///< Mapped memory associated with buffer
168 Graphics::BufferCreateInfo createInfo{}; ///< create info describing the buffer
169 void* mappedPtr{}; ///< Mapped pointer (if mapped)
170 bool needsUpdate{true}; ///< Indicates whether the buffer needs flushing the queue
174 * @brief Returns GfxBuffer object by offset
176 * The memory of UniformBuffer is considered to be continuous, however,
177 * it may contain multiple graphics buffers.
180 const GfxBuffer* GetBufferByOffset(uint32_t offset, uint32_t* newOffset, uint32_t* bufferIndex) const;
182 std::vector<GfxBuffer> mBuffers; ///< List of GfxBuffer objects
184 Dali::Graphics::Controller* mController; ///< Pointer to the controller
186 uint32_t mSize; ///< Current size of buffer
187 uint32_t mAlignment{0}; ///< Buffer alignment
189 Graphics::BufferUsageFlags mUsageFlags;
190 Graphics::BufferPropertiesFlags mPropertiesFlags;
192 uint32_t mLockedBufferIndex; ///< Current locked buffer region index.
193 uint8_t* mLockedPtr; ///< Current locked buffer pointer.
194 bool mReadyToBeLocked : 1; ///< True if current uniform buffer is ready to be locked.
197 } // namespace Dali::Internal::Render
199 #endif //DALI_INTERNAL_UNIFORM_BUFFER_H