1 #ifndef DALI_TOOLKIT_PARTICLE_SYSTEM_PARTICLE_LIST_H
2 #define DALI_TOOLKIT_PARTICLE_SYSTEM_PARTICLE_LIST_H
4 * Copyright (c) 2023 Samsung Electronics Co., Ltd.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
21 #include <dali-toolkit/public-api/particle-system/particle.h>
24 #include <dali/public-api/object/base-handle.h>
25 #include <dali/public-api/common/list-wrapper.h>
29 namespace Dali::Toolkit::ParticleSystem::Internal
34 namespace Dali::Toolkit::ParticleSystem
37 * The function is a wrapper needed to retrieve data type enum through template
40 struct StreamDataTypeWrapper
42 static ParticleStream::StreamDataType GetType()
51 * ParticleList defines a storage or a partial view on an existing storage of particle-related data.
53 * ParticleList contains streams of data (properties) laid out independently (non-interleaved).
55 * The layout is more optimal for:
56 * - parallel processing
57 * - data-oriented processing (CPU-cache friendly)
58 * - adding custom streams of data (for example, physics properties)
60 * Some streams are being added automatically by the emitter when certain modifiers are being added.
61 * If the modifier requires particular data it the emitter will update the list with proper stream.
63 * There are several built-in streams defined:
70 * The New() function allows adding streams upon creation.
72 * Custom streams may be used by custom renderers, modifiers and sources but also may play
73 * a role of temporary storage space when it comes to computing particle parameters.
75 * Represents all or subset of group of particles
77 class DALI_TOOLKIT_API ParticleList : public Dali::BaseHandle
86 * @brief Bit flags grouping built-in particle streams by type
88 struct ParticleStreamTypeFlags
90 ParticleStreamTypeFlags(const ParticleStreamTypeFlags& flags)
95 ParticleStreamTypeFlags(const ParticleStreamTypeFlagBit& bit)
100 ParticleStreamTypeFlags& operator|=(ParticleStreamTypeFlagBit flagBit)
106 ParticleStreamTypeFlags operator&(ParticleStreamTypeFlagBit flagBit)
108 return (value & flagBit);
111 explicit operator uint32_t() const
116 explicit operator bool() const
125 * @brief Creates new ParticleList
127 * ParticleList is a storage object that contains data for particle system
128 * layouted as an array of streams.
130 * Stream may contain data like position, velocity, etc.
132 * When new ParticleList is created the default streams can be pre-allocated.
134 * @param[in] capacity Maximum capacity (number of particles in the system)
135 * @param[in] defaultStreams Default data streams to pre-allocate
137 static ParticleList New(uint32_t capacity, const ParticleStreamTypeFlags& defaultStreams);
140 * @brief Downcasts a handle to ParticleList handle.
142 * If handle points to an ParticleList object, the downcast produces valid handle.
143 * If not, the returned handle is left uninitialized.
145 * @param[in] handle to An object
146 * @return handle to a ParticleList object or an uninitialized handle
148 static ParticleList DownCast(BaseHandle handle);
151 * @brief Registers new data-stream with given unique identifier
153 * @tparam T Type of data stored in the stream
154 * @param[in] defaults Default value of the stream uninitialized data
156 * Streams added using AddStream() function are automatically passed into
157 * the shader program as input attributes.
159 * If there is no need for the stream to be used as a shader attribute
160 * then use AddLocalStream() instead.
162 * @return Returns index of an allocated data stream
165 inline uint32_t AddStream(T defaults)
167 return AddStream(&defaults, StreamDataTypeWrapper<T>::GetType(), sizeof(T), false);
171 * @brief Adds local data stream
173 * @tparam T Type of data stored in the stream
174 * @param[in] defaults Default value of the stream
176 * @return Returns index of newly allocated data stream
179 inline uint32_t AddLocalStream(T defaults)
181 return AddStream(&defaults, sizeof(T), StreamDataTypeWrapper<T>::GetType(), true);
185 T* GetStream(uint32_t streamIndex)
187 return reinterpret_cast<T*>(GetRawStream(streamIndex));
191 T* GetDefaultStream(ParticleStreamTypeFlagBit streamFlagBit)
193 return reinterpret_cast<T*>(GetDefaultStream(streamFlagBit));
197 * @brief Returns size of list including only active particles
201 uint32_t GetActiveParticleCount();
204 * @brief Returns capacity of particle list
206 [[nodiscard]] uint32_t GetCapacity() const;
209 * Creates new particle in the list with specified lifetime
211 * @param[in] lifetime Expected lifetime of new particle (0.0f - lives forever)
212 * @return index into data streams
214 Particle NewParticle(float lifetime);
217 * @brief Returns internal data size of streams
219 * @param[in] includeLocalStreams If true, the size will include local streams
220 * @return Size of data structure
222 uint32_t GetParticleDataSize(bool includeLocalStreams);
224 std::list<Particle>& GetActiveParticles();
229 * @brief Adds new data stream to the list
231 * @param[in] defaults Default values to fill the stream with
232 * @param[in] dataTypeSize size of stored data type
233 * @param[in] dataType Stream data type
234 * @param[in] localStream Flag indicating whether stream is local (not used in shaders) or not
235 * @return Index of new stream
238 AddStream(void* defaults, size_t dataTypeSize, ParticleStream::StreamDataType dataType, bool localStream);
243 * @brief Returns raw data pointer to the stream at given index
245 * @param[in] streamIndex Stream index
247 * @return Valid data pointer or nullptr if index invalid
249 void* GetRawStream(uint32_t streamIndex);
254 * @brief Returns raw data stream of a default (built-in) stream
255 * @param[in] streamBit Bit (ParticleStreamTypeFlagBit) representing the stream
257 * @return Returns valid pointer to the data or nullptr if stream hasn't been used
259 void* GetDefaultStream(ParticleStreamTypeFlagBit streamBit);
264 * @brief This constructor is used by ParticleList::New() methods.
266 * @param [in] impl A pointer to a newly allocated implementation
268 ParticleList(Dali::Toolkit::ParticleSystem::Internal::ParticleList* impl);
272 } // namespace Dali::Toolkit::ParticleSystem