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/common/list-wrapper.h>
25 #include <dali/public-api/object/base-handle.h>
28 namespace Dali::Toolkit::ParticleSystem::Internal
33 namespace Dali::Toolkit::ParticleSystem
36 * The function is a wrapper needed to retrieve data type enum through template
39 struct StreamDataTypeWrapper
41 static ParticleStream::StreamDataType GetType()
50 * ParticleList defines a storage or a partial view on an existing storage of particle-related data.
52 * ParticleList contains streams of data (properties) laid out independently (non-interleaved).
54 * The layout is more optimal for:
55 * - parallel processing
56 * - data-oriented processing (CPU-cache friendly)
57 * - adding custom streams of data (for example, physics properties)
59 * Some streams are being added automatically by the emitter when certain modifiers are being added.
60 * If the modifier requires particular data it the emitter will update the list with proper stream.
62 * There are several built-in streams defined:
69 * The New() function allows adding streams upon creation.
71 * Custom streams may be used by custom renderers, modifiers and sources but also may play
72 * a role of temporary storage space when it comes to computing particle parameters.
74 * Represents all or subset of group of particles
76 class DALI_TOOLKIT_API ParticleList : public Dali::BaseHandle
85 * @brief Bit flags grouping built-in particle streams by type
87 struct ParticleStreamTypeFlags
89 ParticleStreamTypeFlags(const ParticleStreamTypeFlags& flags)
94 ParticleStreamTypeFlags(const ParticleStreamTypeFlagBit& bit)
99 ParticleStreamTypeFlags& operator|=(ParticleStreamTypeFlagBit flagBit)
105 ParticleStreamTypeFlags operator&(ParticleStreamTypeFlagBit flagBit)
107 return (value & flagBit);
110 explicit operator uint32_t() const
115 explicit operator bool() const
124 * @brief Creates new ParticleList
126 * ParticleList is a storage object that contains data for particle system
127 * layouted as an array of streams.
129 * Stream may contain data like position, velocity, etc.
131 * When new ParticleList is created the default streams can be pre-allocated.
133 * @param[in] capacity Maximum capacity (number of particles in the system)
134 * @param[in] defaultStreams Default data streams to pre-allocate
136 static ParticleList New(uint32_t capacity, const ParticleStreamTypeFlags& defaultStreams);
139 * @brief Downcasts a handle to ParticleList handle.
141 * If handle points to an ParticleList object, the downcast produces valid handle.
142 * If not, the returned handle is left uninitialized.
144 * @param[in] handle to An object
145 * @return handle to a ParticleList object or an uninitialized handle
147 static ParticleList DownCast(BaseHandle handle);
150 * @brief Registers new data-stream with given unique identifier
152 * @tparam T Type of data stored in the stream
153 * @param[in] defaults Default value of the stream uninitialized data
155 * Streams added using AddStream() function are automatically passed into
156 * the shader program as input attributes.
158 * If there is no need for the stream to be used as a shader attribute
159 * then use AddLocalStream() instead.
161 * @return Returns index of an allocated data stream
164 inline uint32_t AddStream(T defaults)
166 return AddStream(&defaults, StreamDataTypeWrapper<T>::GetType(), sizeof(T), false);
170 * @brief Adds local data stream
172 * @tparam T Type of data stored in the stream
173 * @param[in] defaults Default value of the stream
175 * @return Returns index of newly allocated data stream
178 inline uint32_t AddLocalStream(T defaults)
180 return AddStream(&defaults, sizeof(T), StreamDataTypeWrapper<T>::GetType(), true);
184 T* GetStream(uint32_t streamIndex)
186 return reinterpret_cast<T*>(GetRawStream(streamIndex));
190 T* GetDefaultStream(ParticleStreamTypeFlagBit streamFlagBit)
192 return reinterpret_cast<T*>(GetDefaultStream(streamFlagBit));
196 * @brief Returns size of list including only active particles
200 uint32_t GetActiveParticleCount();
203 * @brief Returns capacity of particle list
205 [[nodiscard]] uint32_t GetCapacity() const;
208 * Creates new particle in the list with specified lifetime
210 * @param[in] lifetime Expected lifetime of new particle (0.0f - lives forever)
211 * @return index into data streams
213 Particle NewParticle(float lifetime);
216 * @brief Returns internal data size of streams
218 * @param[in] includeLocalStreams If true, the size will include local streams
219 * @return Size of data structure
221 uint32_t GetParticleDataSize(bool includeLocalStreams);
224 * @brief Returns index associated with specified default stream
226 * @param[in] defaultStreamBit Default stream bit
227 * @return Returns a valid index or -1 on error.
229 int GetDefaultStreamIndex(ParticleStreamTypeFlagBit defaultStreamBit);
231 std::list<Particle>& GetActiveParticles();
236 * @brief Adds new data stream to the list
238 * @param[in] defaults Default values to fill the stream with
239 * @param[in] dataTypeSize size of stored data type
240 * @param[in] dataType Stream data type
241 * @param[in] localStream Flag indicating whether stream is local (not used in shaders) or not
242 * @return Index of new stream
245 AddStream(void* defaults, size_t dataTypeSize, ParticleStream::StreamDataType dataType, bool localStream);
250 * @brief Returns raw data pointer to the stream at given index
252 * @param[in] streamIndex Stream index
254 * @return Valid data pointer or nullptr if index invalid
256 void* GetRawStream(uint32_t streamIndex);
261 * @brief Returns raw data stream of a default (built-in) stream
262 * @param[in] streamBit Bit (ParticleStreamTypeFlagBit) representing the stream
264 * @return Returns valid pointer to the data or nullptr if stream hasn't been used
266 void* GetDefaultStream(ParticleStreamTypeFlagBit streamBit);
271 * @brief This constructor is used by ParticleList::New() methods.
273 * @param [in] impl A pointer to a newly allocated implementation
275 ParticleList(Dali::Toolkit::ParticleSystem::Internal::ParticleList* impl);
279 } // namespace Dali::Toolkit::ParticleSystem