particle_system.cpp 19 KB

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