particle_system.cpp 19 KB

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