2
0

particle_system.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  1. /*
  2. * Copyright 2011-2017 Branimir Karadzic. All rights reserved.
  3. * License: https://github.com/bkaradzic/bgfx#license-bsd-2-clause
  4. */
  5. #include <bgfx/bgfx.h>
  6. #include <bgfx/embedded_shader.h>
  7. #include "particle_system.h"
  8. #include "../bgfx_utils.h"
  9. #include "../packrect.h"
  10. #include <bx/easing.h>
  11. #include <bx/handlealloc.h>
  12. #include "vs_particle.bin.h"
  13. #include "fs_particle.bin.h"
  14. static const bgfx::EmbeddedShader s_embeddedShaders[] =
  15. {
  16. BGFX_EMBEDDED_SHADER(vs_particle),
  17. BGFX_EMBEDDED_SHADER(fs_particle),
  18. BGFX_EMBEDDED_SHADER_END()
  19. };
  20. struct PosColorTexCoord0Vertex
  21. {
  22. float m_x;
  23. float m_y;
  24. float m_z;
  25. uint32_t m_abgr;
  26. float m_u;
  27. float m_v;
  28. float m_blend;
  29. float m_angle;
  30. static void init()
  31. {
  32. ms_decl
  33. .begin()
  34. .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
  35. .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true)
  36. .add(bgfx::Attrib::TexCoord0, 4, bgfx::AttribType::Float)
  37. .end();
  38. }
  39. static bgfx::VertexDecl ms_decl;
  40. };
  41. bgfx::VertexDecl PosColorTexCoord0Vertex::ms_decl;
  42. void EmitterUniforms::reset()
  43. {
  44. m_position[0] = 0.0f;
  45. m_position[1] = 0.0f;
  46. m_position[2] = 0.0f;
  47. m_angle[0] = 0.0f;
  48. m_angle[1] = 0.0f;
  49. m_angle[2] = 0.0f;
  50. m_particlesPerSecond = 0;
  51. m_offsetStart[0] = 0.0f;
  52. m_offsetStart[1] = 1.0f;
  53. m_offsetEnd[0] = 2.0f;
  54. m_offsetEnd[1] = 3.0f;
  55. m_rgba[0] = 0x00ffffff;
  56. m_rgba[1] = UINT32_MAX;
  57. m_rgba[2] = UINT32_MAX;
  58. m_rgba[3] = UINT32_MAX;
  59. m_rgba[4] = 0x00ffffff;
  60. m_blendStart[0] = 0.8f;
  61. m_blendStart[1] = 1.0f;
  62. m_blendEnd[0] = 0.0f;
  63. m_blendEnd[1] = 0.2f;
  64. m_scaleStart[0] = 0.1f;
  65. m_scaleStart[1] = 0.2f;
  66. m_scaleEnd[0] = 0.3f;
  67. m_scaleEnd[1] = 0.4f;
  68. m_lifeSpan[0] = 1.0f;
  69. m_lifeSpan[1] = 2.0f;
  70. m_gravityScale = 0.0f;
  71. m_easePos = bx::Easing::Linear;
  72. m_easeRgba = bx::Easing::Linear;
  73. m_easeBlend = bx::Easing::Linear;
  74. m_easeScale = bx::Easing::Linear;
  75. }
  76. namespace ps
  77. {
  78. struct Particle
  79. {
  80. float start[3];
  81. float end[2][3];
  82. float blendStart;
  83. float blendEnd;
  84. float scaleStart;
  85. float scaleEnd;
  86. uint32_t rgba[5];
  87. float life;
  88. float lifeSpan;
  89. };
  90. struct ParticleSort
  91. {
  92. float dist;
  93. uint32_t idx;
  94. };
  95. inline uint32_t toAbgr(const float* _rgba)
  96. {
  97. return 0
  98. | (uint8_t(_rgba[0]*255.0f)<< 0)
  99. | (uint8_t(_rgba[1]*255.0f)<< 8)
  100. | (uint8_t(_rgba[2]*255.0f)<<16)
  101. | (uint8_t(_rgba[3]*255.0f)<<24)
  102. ;
  103. }
  104. inline uint32_t toAbgr(float _rr, float _gg, float _bb, float _aa)
  105. {
  106. return 0
  107. | (uint8_t(_rr*255.0f)<< 0)
  108. | (uint8_t(_gg*255.0f)<< 8)
  109. | (uint8_t(_bb*255.0f)<<16)
  110. | (uint8_t(_aa*255.0f)<<24)
  111. ;
  112. }
  113. #define SPRITE_TEXTURE_SIZE 1024
  114. template<uint16_t MaxHandlesT = 256, uint16_t TextureSizeT = 1024>
  115. struct SpriteT
  116. {
  117. SpriteT()
  118. : m_ra(TextureSizeT, TextureSizeT)
  119. {
  120. }
  121. EmitterSpriteHandle create(uint16_t _width, uint16_t _height)
  122. {
  123. EmitterSpriteHandle handle = { bx::kInvalidHandle };
  124. if (m_handleAlloc.getNumHandles() < m_handleAlloc.getMaxHandles() )
  125. {
  126. Pack2D pack;
  127. if (m_ra.find(_width, _height, pack) )
  128. {
  129. handle.idx = m_handleAlloc.alloc();
  130. m_pack[handle.idx] = pack;
  131. }
  132. }
  133. return handle;
  134. }
  135. void destroy(EmitterSpriteHandle _sprite)
  136. {
  137. const Pack2D& pack = m_pack[_sprite.idx];
  138. m_ra.clear(pack);
  139. m_handleAlloc.free(_sprite.idx);
  140. }
  141. const Pack2D& get(EmitterSpriteHandle _sprite) const
  142. {
  143. return m_pack[_sprite.idx];
  144. }
  145. bx::HandleAllocT<MaxHandlesT> m_handleAlloc;
  146. Pack2D m_pack[MaxHandlesT];
  147. RectPack2DT<256> m_ra;
  148. };
  149. struct Emitter
  150. {
  151. void create(EmitterShape::Enum _shape, EmitterDirection::Enum _direction, uint32_t _maxParticles);
  152. void destroy();
  153. void reset()
  154. {
  155. m_num = 0;
  156. bx::memSet(&m_aabb, 0, sizeof(Aabb) );
  157. }
  158. void update(float _dt)
  159. {
  160. uint32_t num = m_num;
  161. for (uint32_t ii = 0; ii < num; ++ii)
  162. {
  163. Particle& particle = m_particles[ii];
  164. particle.life += _dt * 1.0f/particle.lifeSpan;
  165. if (particle.life > 1.0f)
  166. {
  167. if (ii != num-1)
  168. {
  169. bx::memCopy(&particle, &m_particles[num-1], sizeof(Particle) );
  170. --ii;
  171. }
  172. --num;
  173. }
  174. }
  175. m_num = num;
  176. if (0 < m_uniforms.m_particlesPerSecond)
  177. {
  178. spawn(_dt);
  179. }
  180. }
  181. void spawn(float _dt)
  182. {
  183. float mtx[16];
  184. bx::mtxSRT(mtx
  185. , 1.0f, 1.0f, 1.0f
  186. , m_uniforms.m_angle[0], m_uniforms.m_angle[1], m_uniforms.m_angle[2]
  187. , m_uniforms.m_position[0], m_uniforms.m_position[1], m_uniforms.m_position[2]
  188. );
  189. const float timePerParticle = 1.0f/m_uniforms.m_particlesPerSecond;
  190. m_dt += _dt;
  191. const uint32_t numParticles = uint32_t(m_dt / timePerParticle);
  192. m_dt -= numParticles * timePerParticle;
  193. float time = 0.0f;
  194. for (uint32_t ii = 0
  195. ; ii < numParticles && m_num < m_max
  196. ; ++ii
  197. )
  198. {
  199. Particle& particle = m_particles[m_num];
  200. m_num++;
  201. const float up[3] = { 0.0f, 1.0f, 0.0f };
  202. float pos[3];
  203. switch (m_shape)
  204. {
  205. default:
  206. case EmitterShape::Sphere:
  207. bx::randUnitSphere(pos, &m_rng);
  208. break;
  209. case EmitterShape::Hemisphere:
  210. bx::randUnitHemisphere(pos, &m_rng, up);
  211. break;
  212. case EmitterShape::Circle:
  213. bx::randUnitCircle(pos, &m_rng);
  214. break;
  215. case EmitterShape::Disc:
  216. {
  217. float tmp[3];
  218. bx::randUnitCircle(tmp, &m_rng);
  219. bx::vec3Mul(pos, tmp, bx::frnd(&m_rng) );
  220. }
  221. break;
  222. case EmitterShape::Rect:
  223. pos[0] = bx::frndh(&m_rng);
  224. pos[1] = 0.0f;
  225. pos[2] = bx::frndh(&m_rng);
  226. break;
  227. }
  228. float dir[3];
  229. switch (m_direction)
  230. {
  231. default:
  232. case EmitterDirection::Up:
  233. bx::vec3Move(dir, up);
  234. break;
  235. case EmitterDirection::Outward:
  236. bx::vec3Norm(dir, pos);
  237. break;
  238. }
  239. float start[3];
  240. float end[3];
  241. const float startOffset = bx::flerp(m_uniforms.m_offsetStart[0], m_uniforms.m_offsetStart[1], bx::frnd(&m_rng) );
  242. bx::vec3Mul(start, pos, startOffset);
  243. const float endOffset = bx::flerp(m_uniforms.m_offsetEnd[0], m_uniforms.m_offsetEnd[1], bx::frnd(&m_rng) );
  244. float tmp1[3];
  245. bx::vec3Mul(tmp1, dir, endOffset);
  246. bx::vec3Add(end, tmp1, start);
  247. particle.life = time;
  248. particle.lifeSpan = bx::flerp(m_uniforms.m_lifeSpan[0], m_uniforms.m_lifeSpan[1], bx::frnd(&m_rng) );
  249. float gravity[3] = { 0.0f, -9.81f * m_uniforms.m_gravityScale * bx::fsq(particle.lifeSpan), 0.0f };
  250. bx::vec3MulMtx(particle.start, start, mtx);
  251. bx::vec3MulMtx(particle.end[0], end, mtx);
  252. bx::vec3Add(particle.end[1], particle.end[0], gravity);
  253. bx::memCopy(particle.rgba, m_uniforms.m_rgba, BX_COUNTOF(m_uniforms.m_rgba)*sizeof(uint32_t) );
  254. particle.blendStart = bx::flerp(m_uniforms.m_blendStart[0], m_uniforms.m_blendStart[1], bx::frnd(&m_rng) );
  255. particle.blendEnd = bx::flerp(m_uniforms.m_blendEnd[0], m_uniforms.m_blendEnd[1], bx::frnd(&m_rng) );
  256. particle.scaleStart = bx::flerp(m_uniforms.m_scaleStart[0], m_uniforms.m_scaleStart[1], bx::frnd(&m_rng) );
  257. particle.scaleEnd = bx::flerp(m_uniforms.m_scaleEnd[0], m_uniforms.m_scaleEnd[1], bx::frnd(&m_rng) );
  258. time += timePerParticle;
  259. }
  260. }
  261. uint32_t render(const float _uv[4], const float* _mtxView, const float* _eye, uint32_t _first, uint32_t _max, ParticleSort* _outSort, PosColorTexCoord0Vertex* _outVertices)
  262. {
  263. bx::EaseFn easeRgba = bx::getEaseFunc(m_uniforms.m_easeRgba);
  264. bx::EaseFn easePos = bx::getEaseFunc(m_uniforms.m_easePos);
  265. bx::EaseFn easeBlend = bx::getEaseFunc(m_uniforms.m_easeBlend);
  266. bx::EaseFn easeScale = bx::getEaseFunc(m_uniforms.m_easeScale);
  267. Aabb aabb =
  268. {
  269. { bx::kHuge, bx::kHuge, bx::kHuge },
  270. { -bx::kHuge, -bx::kHuge, -bx::kHuge },
  271. };
  272. for (uint32_t jj = 0, num = m_num, current = _first
  273. ; jj < num && current < _max
  274. ; ++jj, ++current
  275. )
  276. {
  277. const Particle& particle = m_particles[jj];
  278. const float ttPos = easePos(particle.life);
  279. const float ttScale = easeScale(particle.life);
  280. const float ttBlend = bx::fsaturate(easeBlend(particle.life) );
  281. const float ttRgba = bx::fsaturate(easeRgba(particle.life) );
  282. float p0[3];
  283. bx::vec3Lerp(p0, particle.start, particle.end[0], ttPos);
  284. float p1[3];
  285. bx::vec3Lerp(p1, particle.end[0], particle.end[1], ttPos);
  286. float pos[3];
  287. bx::vec3Lerp(pos, p0, p1, ttPos);
  288. ParticleSort& sort = _outSort[current];
  289. float tmp[3];
  290. bx::vec3Sub(tmp, _eye, pos);
  291. sort.dist = bx::fsqrt(bx::vec3Dot(tmp, tmp) );
  292. sort.idx = current;
  293. uint32_t idx = uint32_t(ttRgba*4);
  294. float ttmod = bx::fmod(ttRgba, 0.25f)/0.25f;
  295. uint32_t rgbaStart = particle.rgba[idx];
  296. uint32_t rgbaEnd = particle.rgba[idx+1];
  297. float rr = bx::flerp( ( (uint8_t*)&rgbaStart)[0], ( (uint8_t*)&rgbaEnd)[0], ttmod)/255.0f;
  298. float gg = bx::flerp( ( (uint8_t*)&rgbaStart)[1], ( (uint8_t*)&rgbaEnd)[1], ttmod)/255.0f;
  299. float bb = bx::flerp( ( (uint8_t*)&rgbaStart)[2], ( (uint8_t*)&rgbaEnd)[2], ttmod)/255.0f;
  300. float aa = bx::flerp( ( (uint8_t*)&rgbaStart)[3], ( (uint8_t*)&rgbaEnd)[3], ttmod)/255.0f;
  301. float blend = bx::flerp(particle.blendStart, particle.blendEnd, ttBlend);
  302. float scale = bx::flerp(particle.scaleStart, particle.scaleEnd, ttScale);
  303. uint32_t abgr = toAbgr(rr, gg, bb, aa);
  304. float udir[3] = { _mtxView[0]*scale, _mtxView[4]*scale, _mtxView[8]*scale };
  305. float vdir[3] = { _mtxView[1]*scale, _mtxView[5]*scale, _mtxView[9]*scale };
  306. PosColorTexCoord0Vertex* vertex = &_outVertices[current*4];
  307. bx::vec3Sub(tmp, pos, udir);
  308. bx::vec3Sub(&vertex->m_x, tmp, vdir);
  309. aabbExpand(aabb, &vertex->m_x);
  310. vertex->m_abgr = abgr;
  311. vertex->m_u = _uv[0];
  312. vertex->m_v = _uv[1];
  313. vertex->m_blend = blend;
  314. ++vertex;
  315. bx::vec3Add(tmp, pos, udir);
  316. bx::vec3Sub(&vertex->m_x, tmp, vdir);
  317. aabbExpand(aabb, &vertex->m_x);
  318. vertex->m_abgr = abgr;
  319. vertex->m_u = _uv[2];
  320. vertex->m_v = _uv[1];
  321. vertex->m_blend = blend;
  322. ++vertex;
  323. bx::vec3Add(tmp, pos, udir);
  324. bx::vec3Add(&vertex->m_x, tmp, vdir);
  325. aabbExpand(aabb, &vertex->m_x);
  326. vertex->m_abgr = abgr;
  327. vertex->m_u = _uv[2];
  328. vertex->m_v = _uv[3];
  329. vertex->m_blend = blend;
  330. ++vertex;
  331. bx::vec3Sub(tmp, pos, udir);
  332. bx::vec3Add(&vertex->m_x, tmp, vdir);
  333. aabbExpand(aabb, &vertex->m_x);
  334. vertex->m_abgr = abgr;
  335. vertex->m_u = _uv[0];
  336. vertex->m_v = _uv[3];
  337. vertex->m_blend = blend;
  338. ++vertex;
  339. }
  340. m_aabb = aabb;
  341. return m_num;
  342. }
  343. EmitterShape::Enum m_shape;
  344. EmitterDirection::Enum m_direction;
  345. float m_dt;
  346. bx::RngMwc m_rng;
  347. EmitterUniforms m_uniforms;
  348. Aabb m_aabb;
  349. Particle* m_particles;
  350. uint32_t m_num;
  351. uint32_t m_max;
  352. };
  353. static int32_t particleSortFn(const void* _lhs, const void* _rhs)
  354. {
  355. const ParticleSort& lhs = *(const ParticleSort*)_lhs;
  356. const ParticleSort& rhs = *(const ParticleSort*)_rhs;
  357. return lhs.dist > rhs.dist ? -1 : 1;
  358. }
  359. struct ParticleSystem
  360. {
  361. void init(uint16_t _maxEmitters, bx::AllocatorI* _allocator)
  362. {
  363. m_allocator = _allocator;
  364. if (NULL == _allocator)
  365. {
  366. static bx::DefaultAllocator allocator;
  367. m_allocator = &allocator;
  368. }
  369. m_emitterAlloc = bx::createHandleAlloc(m_allocator, _maxEmitters);
  370. m_emitter = (Emitter*)BX_ALLOC(m_allocator, sizeof(Emitter)*_maxEmitters);
  371. PosColorTexCoord0Vertex::init();
  372. m_num = 0;
  373. s_texColor = bgfx::createUniform("s_texColor", bgfx::UniformType::Int1);
  374. m_texture = bgfx::createTexture2D(
  375. SPRITE_TEXTURE_SIZE
  376. , SPRITE_TEXTURE_SIZE
  377. , false
  378. , 1
  379. , bgfx::TextureFormat::BGRA8
  380. );
  381. bgfx::RendererType::Enum type = bgfx::getRendererType();
  382. m_particleProgram = bgfx::createProgram(
  383. bgfx::createEmbeddedShader(s_embeddedShaders, type, "vs_particle")
  384. , bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_particle")
  385. , true
  386. );
  387. }
  388. void shutdown()
  389. {
  390. bgfx::destroy(m_particleProgram);
  391. bgfx::destroy(m_texture);
  392. bgfx::destroy(s_texColor);
  393. bx::destroyHandleAlloc(m_allocator, m_emitterAlloc);
  394. BX_FREE(m_allocator, m_emitter);
  395. m_allocator = NULL;
  396. }
  397. EmitterSpriteHandle createSprite(uint16_t _width, uint16_t _height, const void* _data)
  398. {
  399. EmitterSpriteHandle handle = m_sprite.create(_width, _height);
  400. if (isValid(handle) )
  401. {
  402. const Pack2D& pack = m_sprite.get(handle);
  403. bgfx::updateTexture2D(
  404. m_texture
  405. , 0
  406. , 0
  407. , pack.m_x
  408. , pack.m_y
  409. , pack.m_width
  410. , pack.m_height
  411. , bgfx::copy(_data, pack.m_width*pack.m_height*4)
  412. );
  413. }
  414. return handle;
  415. }
  416. void destroy(EmitterSpriteHandle _handle)
  417. {
  418. m_sprite.destroy(_handle);
  419. }
  420. void update(float _dt)
  421. {
  422. uint32_t numParticles = 0;
  423. for (uint16_t ii = 0, num = m_emitterAlloc->getNumHandles(); ii < num; ++ii)
  424. {
  425. const uint16_t idx = m_emitterAlloc->getHandleAt(ii);
  426. Emitter& emitter = m_emitter[idx];
  427. emitter.update(_dt);
  428. numParticles += emitter.m_num;
  429. }
  430. m_num = numParticles;
  431. }
  432. void render(uint8_t _view, const float* _mtxView, const float* _eye)
  433. {
  434. if (0 != m_num)
  435. {
  436. bgfx::TransientVertexBuffer tvb;
  437. bgfx::TransientIndexBuffer tib;
  438. const uint32_t numVertices = bgfx::getAvailTransientVertexBuffer(m_num*4, PosColorTexCoord0Vertex::ms_decl);
  439. const uint32_t numIndices = bgfx::getAvailTransientIndexBuffer(m_num*6);
  440. const uint32_t max = bx::uint32_min(numVertices/4, numIndices/6);
  441. BX_WARN(m_num == max
  442. , "Truncating transient buffer for particles to maximum available (requested %d, available %d)."
  443. , m_num
  444. , max
  445. );
  446. if (0 < max)
  447. {
  448. bgfx::allocTransientBuffers(&tvb
  449. , PosColorTexCoord0Vertex::ms_decl
  450. , max*4
  451. , &tib
  452. , max*6
  453. );
  454. PosColorTexCoord0Vertex* vertices = (PosColorTexCoord0Vertex*)tvb.data;
  455. ParticleSort* particleSort = (ParticleSort*)BX_ALLOC(m_allocator, max*sizeof(ParticleSort) );
  456. uint32_t pos = 0;
  457. for (uint16_t ii = 0, numEmitters = m_emitterAlloc->getNumHandles(); ii < numEmitters; ++ii)
  458. {
  459. const uint16_t idx = m_emitterAlloc->getHandleAt(ii);
  460. Emitter& emitter = m_emitter[idx];
  461. const Pack2D& pack = m_sprite.get(emitter.m_uniforms.m_handle);
  462. const float invTextureSize = 1.0f/SPRITE_TEXTURE_SIZE;
  463. const float uv[4] =
  464. {
  465. pack.m_x * invTextureSize,
  466. pack.m_y * invTextureSize,
  467. (pack.m_x + pack.m_width ) * invTextureSize,
  468. (pack.m_y + pack.m_height) * invTextureSize,
  469. };
  470. pos += emitter.render(uv, _mtxView, _eye, pos, max, particleSort, vertices);
  471. }
  472. qsort(particleSort
  473. , max
  474. , sizeof(ParticleSort)
  475. , particleSortFn
  476. );
  477. uint16_t* indices = (uint16_t*)tib.data;
  478. for (uint32_t ii = 0; ii < max; ++ii)
  479. {
  480. const ParticleSort& sort = particleSort[ii];
  481. uint16_t* index = &indices[ii*6];
  482. uint16_t idx = (uint16_t)sort.idx;
  483. index[0] = idx*4+0;
  484. index[1] = idx*4+1;
  485. index[2] = idx*4+2;
  486. index[3] = idx*4+2;
  487. index[4] = idx*4+3;
  488. index[5] = idx*4+0;
  489. }
  490. BX_FREE(m_allocator, particleSort);
  491. bgfx::setState(0
  492. | BGFX_STATE_RGB_WRITE
  493. | BGFX_STATE_ALPHA_WRITE
  494. | BGFX_STATE_DEPTH_TEST_LESS
  495. | BGFX_STATE_CULL_CW
  496. | BGFX_STATE_BLEND_NORMAL
  497. );
  498. bgfx::setVertexBuffer(0, &tvb);
  499. bgfx::setIndexBuffer(&tib);
  500. bgfx::setTexture(0, s_texColor, m_texture);
  501. bgfx::submit(_view, m_particleProgram);
  502. }
  503. }
  504. }
  505. EmitterHandle createEmitter(EmitterShape::Enum _shape, EmitterDirection::Enum _direction, uint32_t _maxParticles)
  506. {
  507. EmitterHandle handle = { m_emitterAlloc->alloc() };
  508. if (UINT16_MAX != handle.idx)
  509. {
  510. m_emitter[handle.idx].create(_shape, _direction, _maxParticles);
  511. }
  512. return handle;
  513. }
  514. void updateEmitter(EmitterHandle _handle, const EmitterUniforms* _uniforms)
  515. {
  516. BX_CHECK(m_emitterAlloc.isValid(_handle.idx)
  517. , "destroyEmitter handle %d is not valid."
  518. , _handle.idx
  519. );
  520. Emitter& emitter = m_emitter[_handle.idx];
  521. if (NULL == _uniforms)
  522. {
  523. emitter.reset();
  524. }
  525. else
  526. {
  527. bx::memCopy(&emitter.m_uniforms, _uniforms, sizeof(EmitterUniforms) );
  528. }
  529. }
  530. void getAabb(EmitterHandle _handle, Aabb& _outAabb)
  531. {
  532. BX_CHECK(m_emitterAlloc.isValid(_handle.idx)
  533. , "getAabb handle %d is not valid."
  534. , _handle.idx
  535. );
  536. _outAabb = m_emitter[_handle.idx].m_aabb;
  537. }
  538. void destroyEmitter(EmitterHandle _handle)
  539. {
  540. BX_CHECK(m_emitterAlloc.isValid(_handle.idx)
  541. , "destroyEmitter handle %d is not valid."
  542. , _handle.idx
  543. );
  544. m_emitter[_handle.idx].destroy();
  545. m_emitterAlloc->free(_handle.idx);
  546. }
  547. bx::AllocatorI* m_allocator;
  548. bx::HandleAlloc* m_emitterAlloc;
  549. Emitter* m_emitter;
  550. typedef SpriteT<256, SPRITE_TEXTURE_SIZE> Sprite;
  551. Sprite m_sprite;
  552. bgfx::UniformHandle s_texColor;
  553. bgfx::TextureHandle m_texture;
  554. bgfx::ProgramHandle m_particleProgram;
  555. uint32_t m_num;
  556. };
  557. static ParticleSystem s_ctx;
  558. void Emitter::create(EmitterShape::Enum _shape, EmitterDirection::Enum _direction, uint32_t _maxParticles)
  559. {
  560. m_dt = 0.0f;
  561. m_uniforms.reset();
  562. m_shape = _shape;
  563. m_direction = _direction;
  564. m_num = 0;
  565. m_max = _maxParticles;
  566. m_particles = (Particle*)BX_ALLOC(s_ctx.m_allocator, m_max*sizeof(Particle) );
  567. }
  568. void Emitter::destroy()
  569. {
  570. BX_FREE(s_ctx.m_allocator, m_particles);
  571. m_particles = NULL;
  572. }
  573. } // namespace ps
  574. using namespace ps;
  575. void psInit(uint16_t _maxEmitters, bx::AllocatorI* _allocator)
  576. {
  577. s_ctx.init(_maxEmitters, _allocator);
  578. }
  579. void psShutdown()
  580. {
  581. s_ctx.shutdown();
  582. }
  583. EmitterSpriteHandle psCreateSprite(uint16_t _width, uint16_t _height, const void* _data)
  584. {
  585. return s_ctx.createSprite(_width, _height, _data);
  586. }
  587. void psDestroy(EmitterSpriteHandle _handle)
  588. {
  589. s_ctx.destroy(_handle);
  590. }
  591. EmitterHandle psCreateEmitter(EmitterShape::Enum _shape, EmitterDirection::Enum _direction, uint32_t _maxParticles)
  592. {
  593. return s_ctx.createEmitter(_shape, _direction, _maxParticles);
  594. }
  595. void psUpdateEmitter(EmitterHandle _handle, const EmitterUniforms* _uniforms)
  596. {
  597. s_ctx.updateEmitter(_handle, _uniforms);
  598. }
  599. void psGetAabb(EmitterHandle _handle, Aabb& _outAabb)
  600. {
  601. s_ctx.getAabb(_handle, _outAabb);
  602. }
  603. void psDestroyEmitter(EmitterHandle _handle)
  604. {
  605. s_ctx.destroyEmitter(_handle);
  606. }
  607. void psUpdate(float _dt)
  608. {
  609. s_ctx.update(_dt);
  610. }
  611. void psRender(uint8_t _view, const float* _mtxView, const float* _eye)
  612. {
  613. s_ctx.render(_view, _mtxView, _eye);
  614. }