particle_system.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757
  1. /*
  2. * Copyright 2011-2018 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_dt = 0.0f;
  156. m_uniforms.reset();
  157. m_num = 0;
  158. bx::memSet(&m_aabb, 0, sizeof(Aabb) );
  159. m_rng.reset();
  160. }
  161. void update(float _dt)
  162. {
  163. uint32_t num = m_num;
  164. for (uint32_t ii = 0; ii < num; ++ii)
  165. {
  166. Particle& particle = m_particles[ii];
  167. particle.life += _dt * 1.0f/particle.lifeSpan;
  168. if (particle.life > 1.0f)
  169. {
  170. if (ii != num-1)
  171. {
  172. bx::memCopy(&particle, &m_particles[num-1], sizeof(Particle) );
  173. --ii;
  174. }
  175. --num;
  176. }
  177. }
  178. m_num = num;
  179. if (0 < m_uniforms.m_particlesPerSecond)
  180. {
  181. spawn(_dt);
  182. }
  183. }
  184. void spawn(float _dt)
  185. {
  186. float mtx[16];
  187. bx::mtxSRT(mtx
  188. , 1.0f, 1.0f, 1.0f
  189. , m_uniforms.m_angle[0], m_uniforms.m_angle[1], m_uniforms.m_angle[2]
  190. , m_uniforms.m_position[0], m_uniforms.m_position[1], m_uniforms.m_position[2]
  191. );
  192. const float timePerParticle = 1.0f/m_uniforms.m_particlesPerSecond;
  193. m_dt += _dt;
  194. const uint32_t numParticles = uint32_t(m_dt / timePerParticle);
  195. m_dt -= numParticles * timePerParticle;
  196. float time = 0.0f;
  197. for (uint32_t ii = 0
  198. ; ii < numParticles && m_num < m_max
  199. ; ++ii
  200. )
  201. {
  202. Particle& particle = m_particles[m_num];
  203. m_num++;
  204. const float up[3] = { 0.0f, 1.0f, 0.0f };
  205. float pos[3];
  206. switch (m_shape)
  207. {
  208. default:
  209. case EmitterShape::Sphere:
  210. bx::randUnitSphere(pos, &m_rng);
  211. break;
  212. case EmitterShape::Hemisphere:
  213. bx::randUnitHemisphere(pos, &m_rng, up);
  214. break;
  215. case EmitterShape::Circle:
  216. bx::randUnitCircle(pos, &m_rng);
  217. break;
  218. case EmitterShape::Disc:
  219. {
  220. float tmp[3];
  221. bx::randUnitCircle(tmp, &m_rng);
  222. bx::vec3Mul(pos, tmp, bx::frnd(&m_rng) );
  223. }
  224. break;
  225. case EmitterShape::Rect:
  226. pos[0] = bx::frndh(&m_rng);
  227. pos[1] = 0.0f;
  228. pos[2] = bx::frndh(&m_rng);
  229. break;
  230. }
  231. float dir[3];
  232. switch (m_direction)
  233. {
  234. default:
  235. case EmitterDirection::Up:
  236. bx::vec3Move(dir, up);
  237. break;
  238. case EmitterDirection::Outward:
  239. bx::vec3Norm(dir, pos);
  240. break;
  241. }
  242. float start[3];
  243. float end[3];
  244. const float startOffset = bx::lerp(m_uniforms.m_offsetStart[0], m_uniforms.m_offsetStart[1], bx::frnd(&m_rng) );
  245. bx::vec3Mul(start, pos, startOffset);
  246. const float endOffset = bx::lerp(m_uniforms.m_offsetEnd[0], m_uniforms.m_offsetEnd[1], bx::frnd(&m_rng) );
  247. float tmp1[3];
  248. bx::vec3Mul(tmp1, dir, endOffset);
  249. bx::vec3Add(end, tmp1, start);
  250. particle.life = time;
  251. particle.lifeSpan = bx::lerp(m_uniforms.m_lifeSpan[0], m_uniforms.m_lifeSpan[1], bx::frnd(&m_rng) );
  252. float gravity[3] = { 0.0f, -9.81f * m_uniforms.m_gravityScale * bx::square(particle.lifeSpan), 0.0f };
  253. bx::vec3MulMtx(particle.start, start, mtx);
  254. bx::vec3MulMtx(particle.end[0], end, mtx);
  255. bx::vec3Add(particle.end[1], particle.end[0], gravity);
  256. bx::memCopy(particle.rgba, m_uniforms.m_rgba, BX_COUNTOF(m_uniforms.m_rgba)*sizeof(uint32_t) );
  257. particle.blendStart = bx::lerp(m_uniforms.m_blendStart[0], m_uniforms.m_blendStart[1], bx::frnd(&m_rng) );
  258. particle.blendEnd = bx::lerp(m_uniforms.m_blendEnd[0], m_uniforms.m_blendEnd[1], bx::frnd(&m_rng) );
  259. particle.scaleStart = bx::lerp(m_uniforms.m_scaleStart[0], m_uniforms.m_scaleStart[1], bx::frnd(&m_rng) );
  260. particle.scaleEnd = bx::lerp(m_uniforms.m_scaleEnd[0], m_uniforms.m_scaleEnd[1], bx::frnd(&m_rng) );
  261. time += timePerParticle;
  262. }
  263. }
  264. uint32_t render(const float _uv[4], const float* _mtxView, const float* _eye, uint32_t _first, uint32_t _max, ParticleSort* _outSort, PosColorTexCoord0Vertex* _outVertices)
  265. {
  266. bx::EaseFn easeRgba = bx::getEaseFunc(m_uniforms.m_easeRgba);
  267. bx::EaseFn easePos = bx::getEaseFunc(m_uniforms.m_easePos);
  268. bx::EaseFn easeBlend = bx::getEaseFunc(m_uniforms.m_easeBlend);
  269. bx::EaseFn easeScale = bx::getEaseFunc(m_uniforms.m_easeScale);
  270. Aabb aabb =
  271. {
  272. { bx::kInfinity, bx::kInfinity, bx::kInfinity },
  273. { -bx::kInfinity, -bx::kInfinity, -bx::kInfinity },
  274. };
  275. for (uint32_t jj = 0, num = m_num, current = _first
  276. ; jj < num && current < _max
  277. ; ++jj, ++current
  278. )
  279. {
  280. const Particle& particle = m_particles[jj];
  281. const float ttPos = easePos(particle.life);
  282. const float ttScale = easeScale(particle.life);
  283. const float ttBlend = bx::clamp(easeBlend(particle.life), 0.0f, 1.0f);
  284. const float ttRgba = bx::clamp(easeRgba(particle.life), 0.0f, 1.0f);
  285. float p0[3];
  286. bx::vec3Lerp(p0, particle.start, particle.end[0], ttPos);
  287. float p1[3];
  288. bx::vec3Lerp(p1, particle.end[0], particle.end[1], ttPos);
  289. float pos[3];
  290. bx::vec3Lerp(pos, p0, p1, ttPos);
  291. ParticleSort& sort = _outSort[current];
  292. float tmp[3];
  293. bx::vec3Sub(tmp, _eye, pos);
  294. sort.dist = bx::sqrt(bx::vec3Dot(tmp, tmp) );
  295. sort.idx = current;
  296. uint32_t idx = uint32_t(ttRgba*4);
  297. float ttmod = bx::mod(ttRgba, 0.25f)/0.25f;
  298. uint32_t rgbaStart = particle.rgba[idx];
  299. uint32_t rgbaEnd = particle.rgba[idx+1];
  300. float rr = bx::lerp( ( (uint8_t*)&rgbaStart)[0], ( (uint8_t*)&rgbaEnd)[0], ttmod)/255.0f;
  301. float gg = bx::lerp( ( (uint8_t*)&rgbaStart)[1], ( (uint8_t*)&rgbaEnd)[1], ttmod)/255.0f;
  302. float bb = bx::lerp( ( (uint8_t*)&rgbaStart)[2], ( (uint8_t*)&rgbaEnd)[2], ttmod)/255.0f;
  303. float aa = bx::lerp( ( (uint8_t*)&rgbaStart)[3], ( (uint8_t*)&rgbaEnd)[3], ttmod)/255.0f;
  304. float blend = bx::lerp(particle.blendStart, particle.blendEnd, ttBlend);
  305. float scale = bx::lerp(particle.scaleStart, particle.scaleEnd, ttScale);
  306. uint32_t abgr = toAbgr(rr, gg, bb, aa);
  307. float udir[3] = { _mtxView[0]*scale, _mtxView[4]*scale, _mtxView[8]*scale };
  308. float vdir[3] = { _mtxView[1]*scale, _mtxView[5]*scale, _mtxView[9]*scale };
  309. PosColorTexCoord0Vertex* vertex = &_outVertices[current*4];
  310. bx::vec3Sub(tmp, pos, udir);
  311. bx::vec3Sub(&vertex->m_x, tmp, vdir);
  312. aabbExpand(aabb, &vertex->m_x);
  313. vertex->m_abgr = abgr;
  314. vertex->m_u = _uv[0];
  315. vertex->m_v = _uv[1];
  316. vertex->m_blend = blend;
  317. ++vertex;
  318. bx::vec3Add(tmp, pos, udir);
  319. bx::vec3Sub(&vertex->m_x, tmp, vdir);
  320. aabbExpand(aabb, &vertex->m_x);
  321. vertex->m_abgr = abgr;
  322. vertex->m_u = _uv[2];
  323. vertex->m_v = _uv[1];
  324. vertex->m_blend = blend;
  325. ++vertex;
  326. bx::vec3Add(tmp, pos, udir);
  327. bx::vec3Add(&vertex->m_x, tmp, vdir);
  328. aabbExpand(aabb, &vertex->m_x);
  329. vertex->m_abgr = abgr;
  330. vertex->m_u = _uv[2];
  331. vertex->m_v = _uv[3];
  332. vertex->m_blend = blend;
  333. ++vertex;
  334. bx::vec3Sub(tmp, pos, udir);
  335. bx::vec3Add(&vertex->m_x, tmp, vdir);
  336. aabbExpand(aabb, &vertex->m_x);
  337. vertex->m_abgr = abgr;
  338. vertex->m_u = _uv[0];
  339. vertex->m_v = _uv[3];
  340. vertex->m_blend = blend;
  341. ++vertex;
  342. }
  343. m_aabb = aabb;
  344. return m_num;
  345. }
  346. EmitterShape::Enum m_shape;
  347. EmitterDirection::Enum m_direction;
  348. float m_dt;
  349. bx::RngMwc m_rng;
  350. EmitterUniforms m_uniforms;
  351. Aabb m_aabb;
  352. Particle* m_particles;
  353. uint32_t m_num;
  354. uint32_t m_max;
  355. };
  356. static int32_t particleSortFn(const void* _lhs, const void* _rhs)
  357. {
  358. const ParticleSort& lhs = *(const ParticleSort*)_lhs;
  359. const ParticleSort& rhs = *(const ParticleSort*)_rhs;
  360. return lhs.dist > rhs.dist ? -1 : 1;
  361. }
  362. struct ParticleSystem
  363. {
  364. void init(uint16_t _maxEmitters, bx::AllocatorI* _allocator)
  365. {
  366. m_allocator = _allocator;
  367. if (NULL == _allocator)
  368. {
  369. static bx::DefaultAllocator allocator;
  370. m_allocator = &allocator;
  371. }
  372. m_emitterAlloc = bx::createHandleAlloc(m_allocator, _maxEmitters);
  373. m_emitter = (Emitter*)BX_ALLOC(m_allocator, sizeof(Emitter)*_maxEmitters);
  374. PosColorTexCoord0Vertex::init();
  375. m_num = 0;
  376. s_texColor = bgfx::createUniform("s_texColor", bgfx::UniformType::Int1);
  377. m_texture = bgfx::createTexture2D(
  378. SPRITE_TEXTURE_SIZE
  379. , SPRITE_TEXTURE_SIZE
  380. , false
  381. , 1
  382. , bgfx::TextureFormat::BGRA8
  383. );
  384. bgfx::RendererType::Enum type = bgfx::getRendererType();
  385. m_particleProgram = bgfx::createProgram(
  386. bgfx::createEmbeddedShader(s_embeddedShaders, type, "vs_particle")
  387. , bgfx::createEmbeddedShader(s_embeddedShaders, type, "fs_particle")
  388. , true
  389. );
  390. }
  391. void shutdown()
  392. {
  393. bgfx::destroy(m_particleProgram);
  394. bgfx::destroy(m_texture);
  395. bgfx::destroy(s_texColor);
  396. bx::destroyHandleAlloc(m_allocator, m_emitterAlloc);
  397. BX_FREE(m_allocator, m_emitter);
  398. m_allocator = NULL;
  399. }
  400. EmitterSpriteHandle createSprite(uint16_t _width, uint16_t _height, const void* _data)
  401. {
  402. EmitterSpriteHandle handle = m_sprite.create(_width, _height);
  403. if (isValid(handle) )
  404. {
  405. const Pack2D& pack = m_sprite.get(handle);
  406. bgfx::updateTexture2D(
  407. m_texture
  408. , 0
  409. , 0
  410. , pack.m_x
  411. , pack.m_y
  412. , pack.m_width
  413. , pack.m_height
  414. , bgfx::copy(_data, pack.m_width*pack.m_height*4)
  415. );
  416. }
  417. return handle;
  418. }
  419. void destroy(EmitterSpriteHandle _handle)
  420. {
  421. m_sprite.destroy(_handle);
  422. }
  423. void update(float _dt)
  424. {
  425. uint32_t numParticles = 0;
  426. for (uint16_t ii = 0, num = m_emitterAlloc->getNumHandles(); ii < num; ++ii)
  427. {
  428. const uint16_t idx = m_emitterAlloc->getHandleAt(ii);
  429. Emitter& emitter = m_emitter[idx];
  430. emitter.update(_dt);
  431. numParticles += emitter.m_num;
  432. }
  433. m_num = numParticles;
  434. }
  435. void render(uint8_t _view, const float* _mtxView, const float* _eye)
  436. {
  437. if (0 != m_num)
  438. {
  439. bgfx::TransientVertexBuffer tvb;
  440. bgfx::TransientIndexBuffer tib;
  441. const uint32_t numVertices = bgfx::getAvailTransientVertexBuffer(m_num*4, PosColorTexCoord0Vertex::ms_decl);
  442. const uint32_t numIndices = bgfx::getAvailTransientIndexBuffer(m_num*6);
  443. const uint32_t max = bx::uint32_min(numVertices/4, numIndices/6);
  444. BX_WARN(m_num == max
  445. , "Truncating transient buffer for particles to maximum available (requested %d, available %d)."
  446. , m_num
  447. , max
  448. );
  449. if (0 < max)
  450. {
  451. bgfx::allocTransientBuffers(&tvb
  452. , PosColorTexCoord0Vertex::ms_decl
  453. , max*4
  454. , &tib
  455. , max*6
  456. );
  457. PosColorTexCoord0Vertex* vertices = (PosColorTexCoord0Vertex*)tvb.data;
  458. ParticleSort* particleSort = (ParticleSort*)BX_ALLOC(m_allocator, max*sizeof(ParticleSort) );
  459. uint32_t pos = 0;
  460. for (uint16_t ii = 0, numEmitters = m_emitterAlloc->getNumHandles(); ii < numEmitters; ++ii)
  461. {
  462. const uint16_t idx = m_emitterAlloc->getHandleAt(ii);
  463. Emitter& emitter = m_emitter[idx];
  464. const Pack2D& pack = m_sprite.get(emitter.m_uniforms.m_handle);
  465. const float invTextureSize = 1.0f/SPRITE_TEXTURE_SIZE;
  466. const float uv[4] =
  467. {
  468. pack.m_x * invTextureSize,
  469. pack.m_y * invTextureSize,
  470. (pack.m_x + pack.m_width ) * invTextureSize,
  471. (pack.m_y + pack.m_height) * invTextureSize,
  472. };
  473. pos += emitter.render(uv, _mtxView, _eye, pos, max, particleSort, vertices);
  474. }
  475. qsort(particleSort
  476. , max
  477. , sizeof(ParticleSort)
  478. , particleSortFn
  479. );
  480. uint16_t* indices = (uint16_t*)tib.data;
  481. for (uint32_t ii = 0; ii < max; ++ii)
  482. {
  483. const ParticleSort& sort = particleSort[ii];
  484. uint16_t* index = &indices[ii*6];
  485. uint16_t idx = (uint16_t)sort.idx;
  486. index[0] = idx*4+0;
  487. index[1] = idx*4+1;
  488. index[2] = idx*4+2;
  489. index[3] = idx*4+2;
  490. index[4] = idx*4+3;
  491. index[5] = idx*4+0;
  492. }
  493. BX_FREE(m_allocator, particleSort);
  494. bgfx::setState(0
  495. | BGFX_STATE_WRITE_RGB
  496. | BGFX_STATE_WRITE_A
  497. | BGFX_STATE_DEPTH_TEST_LESS
  498. | BGFX_STATE_CULL_CW
  499. | BGFX_STATE_BLEND_NORMAL
  500. );
  501. bgfx::setVertexBuffer(0, &tvb);
  502. bgfx::setIndexBuffer(&tib);
  503. bgfx::setTexture(0, s_texColor, m_texture);
  504. bgfx::submit(_view, m_particleProgram);
  505. }
  506. }
  507. }
  508. EmitterHandle createEmitter(EmitterShape::Enum _shape, EmitterDirection::Enum _direction, uint32_t _maxParticles)
  509. {
  510. EmitterHandle handle = { m_emitterAlloc->alloc() };
  511. if (UINT16_MAX != handle.idx)
  512. {
  513. m_emitter[handle.idx].create(_shape, _direction, _maxParticles);
  514. }
  515. return handle;
  516. }
  517. void updateEmitter(EmitterHandle _handle, const EmitterUniforms* _uniforms)
  518. {
  519. BX_CHECK(m_emitterAlloc.isValid(_handle.idx)
  520. , "destroyEmitter handle %d is not valid."
  521. , _handle.idx
  522. );
  523. Emitter& emitter = m_emitter[_handle.idx];
  524. if (NULL == _uniforms)
  525. {
  526. emitter.reset();
  527. }
  528. else
  529. {
  530. bx::memCopy(&emitter.m_uniforms, _uniforms, sizeof(EmitterUniforms) );
  531. }
  532. }
  533. void getAabb(EmitterHandle _handle, Aabb& _outAabb)
  534. {
  535. BX_CHECK(m_emitterAlloc.isValid(_handle.idx)
  536. , "getAabb handle %d is not valid."
  537. , _handle.idx
  538. );
  539. _outAabb = m_emitter[_handle.idx].m_aabb;
  540. }
  541. void destroyEmitter(EmitterHandle _handle)
  542. {
  543. BX_CHECK(m_emitterAlloc.isValid(_handle.idx)
  544. , "destroyEmitter handle %d is not valid."
  545. , _handle.idx
  546. );
  547. m_emitter[_handle.idx].destroy();
  548. m_emitterAlloc->free(_handle.idx);
  549. }
  550. bx::AllocatorI* m_allocator;
  551. bx::HandleAlloc* m_emitterAlloc;
  552. Emitter* m_emitter;
  553. typedef SpriteT<256, SPRITE_TEXTURE_SIZE> Sprite;
  554. Sprite m_sprite;
  555. bgfx::UniformHandle s_texColor;
  556. bgfx::TextureHandle m_texture;
  557. bgfx::ProgramHandle m_particleProgram;
  558. uint32_t m_num;
  559. };
  560. static ParticleSystem s_ctx;
  561. void Emitter::create(EmitterShape::Enum _shape, EmitterDirection::Enum _direction, uint32_t _maxParticles)
  562. {
  563. reset();
  564. m_shape = _shape;
  565. m_direction = _direction;
  566. m_max = _maxParticles;
  567. m_particles = (Particle*)BX_ALLOC(s_ctx.m_allocator, m_max*sizeof(Particle) );
  568. }
  569. void Emitter::destroy()
  570. {
  571. BX_FREE(s_ctx.m_allocator, m_particles);
  572. m_particles = NULL;
  573. }
  574. } // namespace ps
  575. using namespace ps;
  576. void psInit(uint16_t _maxEmitters, bx::AllocatorI* _allocator)
  577. {
  578. s_ctx.init(_maxEmitters, _allocator);
  579. }
  580. void psShutdown()
  581. {
  582. s_ctx.shutdown();
  583. }
  584. EmitterSpriteHandle psCreateSprite(uint16_t _width, uint16_t _height, const void* _data)
  585. {
  586. return s_ctx.createSprite(_width, _height, _data);
  587. }
  588. void psDestroy(EmitterSpriteHandle _handle)
  589. {
  590. s_ctx.destroy(_handle);
  591. }
  592. EmitterHandle psCreateEmitter(EmitterShape::Enum _shape, EmitterDirection::Enum _direction, uint32_t _maxParticles)
  593. {
  594. return s_ctx.createEmitter(_shape, _direction, _maxParticles);
  595. }
  596. void psUpdateEmitter(EmitterHandle _handle, const EmitterUniforms* _uniforms)
  597. {
  598. s_ctx.updateEmitter(_handle, _uniforms);
  599. }
  600. void psGetAabb(EmitterHandle _handle, Aabb& _outAabb)
  601. {
  602. s_ctx.getAabb(_handle, _outAabb);
  603. }
  604. void psDestroyEmitter(EmitterHandle _handle)
  605. {
  606. s_ctx.destroyEmitter(_handle);
  607. }
  608. void psUpdate(float _dt)
  609. {
  610. s_ctx.update(_dt);
  611. }
  612. void psRender(uint8_t _view, const float* _mtxView, const float* _eye)
  613. {
  614. s_ctx.render(_view, _mtxView, _eye);
  615. }