render_world.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029
  1. /*
  2. * Copyright (c) 2012-2021 Daniele Bartolini et al.
  3. * License: https://github.com/dbartolini/crown/blob/master/LICENSE
  4. */
  5. #include "core/containers/array.inl"
  6. #include "core/containers/hash_map.inl"
  7. #include "core/list.inl"
  8. #include "core/math/aabb.h"
  9. #include "core/math/color4.inl"
  10. #include "core/math/constants.h"
  11. #include "core/math/intersection.h"
  12. #include "core/math/matrix4x4.inl"
  13. #include "device/pipeline.h"
  14. #include "resource/mesh_resource.h"
  15. #include "resource/resource_manager.h"
  16. #include "resource/sprite_resource.h"
  17. #include "world/debug_line.h"
  18. #include "world/material.h"
  19. #include "world/material_manager.h"
  20. #include "world/render_world.h"
  21. #include "world/unit_manager.h"
  22. #include <bgfx/bgfx.h>
  23. namespace crown
  24. {
  25. static void unit_destroyed_callback_bridge(UnitId unit, void* user_ptr)
  26. {
  27. ((RenderWorld*)user_ptr)->unit_destroyed_callback(unit);
  28. }
  29. RenderWorld::RenderWorld(Allocator& a, ResourceManager& rm, ShaderManager& sm, MaterialManager& mm, UnitManager& um)
  30. : _marker(RENDER_WORLD_MARKER)
  31. , _allocator(&a)
  32. , _resource_manager(&rm)
  33. , _shader_manager(&sm)
  34. , _material_manager(&mm)
  35. , _unit_manager(&um)
  36. , _debug_drawing(false)
  37. , _mesh_manager(a)
  38. , _sprite_manager(a)
  39. , _light_manager(a)
  40. {
  41. _unit_destroy_callback.destroy = unit_destroyed_callback_bridge;
  42. _unit_destroy_callback.user_data = this;
  43. _unit_destroy_callback.node.next = NULL;
  44. _unit_destroy_callback.node.prev = NULL;
  45. um.register_destroy_callback(&_unit_destroy_callback);
  46. _u_light_position = bgfx::createUniform("u_light_position", bgfx::UniformType::Vec4);
  47. _u_light_direction = bgfx::createUniform("u_light_direction", bgfx::UniformType::Vec4);
  48. _u_light_color = bgfx::createUniform("u_light_color", bgfx::UniformType::Vec4);
  49. _u_light_range = bgfx::createUniform("u_light_range", bgfx::UniformType::Vec4);
  50. _u_light_intensity = bgfx::createUniform("u_light_intensity", bgfx::UniformType::Vec4);
  51. }
  52. RenderWorld::~RenderWorld()
  53. {
  54. _unit_manager->unregister_destroy_callback(&_unit_destroy_callback);
  55. bgfx::destroy(_u_light_intensity);
  56. bgfx::destroy(_u_light_range);
  57. bgfx::destroy(_u_light_color);
  58. bgfx::destroy(_u_light_direction);
  59. bgfx::destroy(_u_light_position);
  60. _mesh_manager.destroy();
  61. _sprite_manager.destroy();
  62. _light_manager.destroy();
  63. _marker = 0;
  64. }
  65. MeshInstance RenderWorld::mesh_create(UnitId unit, const MeshRendererDesc& mrd, const Matrix4x4& tr)
  66. {
  67. const MeshResource* mr = (const MeshResource*)_resource_manager->get(RESOURCE_TYPE_MESH, mrd.mesh_resource);
  68. _material_manager->create_material(mrd.material_resource);
  69. return _mesh_manager.create(unit, mr, mrd, tr);
  70. }
  71. void RenderWorld::mesh_destroy(MeshInstance mesh)
  72. {
  73. CE_ASSERT(mesh.i < _mesh_manager._data.size, "Index out of bounds");
  74. _mesh_manager.destroy(mesh);
  75. }
  76. MeshInstance RenderWorld::mesh_instance(UnitId unit)
  77. {
  78. return _mesh_manager.mesh(unit);
  79. }
  80. Material* RenderWorld::mesh_material(MeshInstance mesh)
  81. {
  82. CE_ASSERT(mesh.i < _mesh_manager._data.size, "Index out of bounds");
  83. return _material_manager->get(_mesh_manager._data.material[mesh.i]);
  84. }
  85. void RenderWorld::mesh_set_material(MeshInstance mesh, StringId64 id)
  86. {
  87. CE_ASSERT(mesh.i < _mesh_manager._data.size, "Index out of bounds");
  88. _material_manager->create_material(id);
  89. _mesh_manager._data.material[mesh.i] = id;
  90. }
  91. void RenderWorld::mesh_set_visible(MeshInstance mesh, bool visible)
  92. {
  93. CE_ASSERT(mesh.i < _mesh_manager._data.size, "Index out of bounds");
  94. _mesh_manager.set_visible(mesh, visible);
  95. }
  96. OBB RenderWorld::mesh_obb(MeshInstance mesh)
  97. {
  98. CE_ASSERT(mesh.i < _mesh_manager._data.size, "Index out of bounds");
  99. const Matrix4x4& world = _mesh_manager._data.world[mesh.i];
  100. const OBB& obb = _mesh_manager._data.obb[mesh.i];
  101. OBB o;
  102. o.tm = obb.tm * world;
  103. o.half_extents = obb.half_extents;
  104. return o;
  105. }
  106. f32 RenderWorld::mesh_cast_ray(MeshInstance mesh, const Vector3& from, const Vector3& dir)
  107. {
  108. CE_ASSERT(mesh.i < _mesh_manager._data.size, "Index out of bounds");
  109. const MeshGeometry* mg = _mesh_manager._data.geometry[mesh.i];
  110. return ray_mesh_intersection(from
  111. , dir
  112. , _mesh_manager._data.world[mesh.i]
  113. , mg->vertices.data
  114. , mg->vertices.stride
  115. , (u16*)mg->indices.data
  116. , mg->indices.num
  117. );
  118. }
  119. SpriteInstance RenderWorld::sprite_create(UnitId unit, const SpriteRendererDesc& srd, const Matrix4x4& tr)
  120. {
  121. const SpriteResource* sr = (const SpriteResource*)_resource_manager->get(RESOURCE_TYPE_SPRITE, srd.sprite_resource);
  122. _material_manager->create_material(srd.material_resource);
  123. return _sprite_manager.create(unit, sr, srd, tr);
  124. }
  125. void RenderWorld::sprite_destroy(SpriteInstance sprite)
  126. {
  127. CE_ASSERT(sprite.i < _sprite_manager._data.size, "Index out of bounds");
  128. _sprite_manager.destroy(sprite);
  129. }
  130. SpriteInstance RenderWorld::sprite_instance(UnitId unit)
  131. {
  132. return _sprite_manager.sprite(unit);
  133. }
  134. Material* RenderWorld::sprite_material(SpriteInstance sprite)
  135. {
  136. CE_ASSERT(sprite.i < _sprite_manager._data.size, "Index out of bounds");
  137. return _material_manager->get(_sprite_manager._data.material[sprite.i]);
  138. }
  139. void RenderWorld::sprite_set_material(SpriteInstance sprite, StringId64 id)
  140. {
  141. CE_ASSERT(sprite.i < _sprite_manager._data.size, "Index out of bounds");
  142. _material_manager->create_material(id);
  143. _sprite_manager._data.material[sprite.i] = id;
  144. }
  145. void RenderWorld::sprite_set_frame(SpriteInstance sprite, u32 index)
  146. {
  147. CE_ASSERT(sprite.i < _sprite_manager._data.size, "Index out of bounds");
  148. _sprite_manager._data.frame[sprite.i] = index;
  149. }
  150. void RenderWorld::sprite_set_visible(SpriteInstance sprite, bool visible)
  151. {
  152. CE_ASSERT(sprite.i < _sprite_manager._data.size, "Index out of bounds");
  153. _sprite_manager.set_visible(sprite, visible);
  154. }
  155. void RenderWorld::sprite_flip_x(SpriteInstance sprite, bool flip)
  156. {
  157. CE_ASSERT(sprite.i < _sprite_manager._data.size, "Index out of bounds");
  158. _sprite_manager._data.flip_x[sprite.i] = flip;
  159. }
  160. void RenderWorld::sprite_flip_y(SpriteInstance sprite, bool flip)
  161. {
  162. CE_ASSERT(sprite.i < _sprite_manager._data.size, "Index out of bounds");
  163. _sprite_manager._data.flip_y[sprite.i] = flip;
  164. }
  165. void RenderWorld::sprite_set_layer(SpriteInstance sprite, u32 layer)
  166. {
  167. CE_ASSERT(sprite.i < _sprite_manager._data.size, "Index out of bounds");
  168. _sprite_manager._data.layer[sprite.i] = layer;
  169. }
  170. void RenderWorld::sprite_set_depth(SpriteInstance sprite, u32 depth)
  171. {
  172. CE_ASSERT(sprite.i < _sprite_manager._data.size, "Index out of bounds");
  173. _sprite_manager._data.depth[sprite.i] = depth;
  174. }
  175. OBB RenderWorld::sprite_obb(SpriteInstance sprite)
  176. {
  177. CE_ASSERT(sprite.i < _sprite_manager._data.size, "Index out of bounds");
  178. const OBB& obb = _sprite_manager._data.resource[sprite.i]->obb;
  179. const Matrix4x4& world = _sprite_manager._data.world[sprite.i];
  180. OBB o;
  181. o.tm = obb.tm * world;
  182. o.half_extents = obb.half_extents;
  183. return o;
  184. }
  185. f32 RenderWorld::sprite_cast_ray(SpriteInstance sprite, const Vector3& from, const Vector3& dir, u32& layer, u32& depth)
  186. {
  187. CE_ASSERT(sprite.i < _sprite_manager._data.size, "Index out of bounds");
  188. const SpriteManager::SpriteInstanceData& sid = _sprite_manager._data;
  189. const f32* frame = sprite_resource::frame_data(sid.resource[sprite.i], sid.frame[sprite.i]);
  190. const f32 vertices[] =
  191. {
  192. frame[ 0], frame[ 1], frame[ 2],
  193. frame[ 5], frame[ 6], frame[ 7],
  194. frame[10], frame[11], frame[12],
  195. frame[15], frame[16], frame[17]
  196. };
  197. const u16 indices[] =
  198. {
  199. 0, 1, 2,
  200. 0, 2, 3
  201. };
  202. layer = _sprite_manager._data.layer[sprite.i];
  203. depth = _sprite_manager._data.depth[sprite.i];
  204. return ray_mesh_intersection(from
  205. , dir
  206. , _sprite_manager._data.world[sprite.i]
  207. , vertices
  208. , sizeof(Vector3)
  209. , indices
  210. , 6
  211. );
  212. }
  213. LightInstance RenderWorld::light_create(UnitId unit, const LightDesc& ld, const Matrix4x4& tr)
  214. {
  215. return _light_manager.create(unit, ld, tr);
  216. }
  217. void RenderWorld::light_destroy(LightInstance light)
  218. {
  219. CE_ASSERT(light.i < _light_manager._data.size, "Index out of bounds");
  220. _light_manager.destroy(light);
  221. }
  222. LightInstance RenderWorld::light_instance(UnitId unit)
  223. {
  224. return _light_manager.light(unit);
  225. }
  226. Color4 RenderWorld::light_color(LightInstance light)
  227. {
  228. CE_ASSERT(light.i < _light_manager._data.size, "Index out of bounds");
  229. return _light_manager._data.color[light.i];
  230. }
  231. LightType::Enum RenderWorld::light_type(LightInstance light)
  232. {
  233. CE_ASSERT(light.i < _light_manager._data.size, "Index out of bounds");
  234. return (LightType::Enum)_light_manager._data.type[light.i];
  235. }
  236. f32 RenderWorld::light_range(LightInstance light)
  237. {
  238. CE_ASSERT(light.i < _light_manager._data.size, "Index out of bounds");
  239. return _light_manager._data.range[light.i];
  240. }
  241. f32 RenderWorld::light_intensity(LightInstance light)
  242. {
  243. CE_ASSERT(light.i < _light_manager._data.size, "Index out of bounds");
  244. return _light_manager._data.intensity[light.i];
  245. }
  246. f32 RenderWorld::light_spot_angle(LightInstance light)
  247. {
  248. CE_ASSERT(light.i < _light_manager._data.size, "Index out of bounds");
  249. return _light_manager._data.spot_angle[light.i];
  250. }
  251. void RenderWorld::light_set_color(LightInstance light, const Color4& col)
  252. {
  253. CE_ASSERT(light.i < _light_manager._data.size, "Index out of bounds");
  254. _light_manager._data.color[light.i] = col;
  255. }
  256. void RenderWorld::light_set_type(LightInstance light, LightType::Enum type)
  257. {
  258. CE_ASSERT(light.i < _light_manager._data.size, "Index out of bounds");
  259. _light_manager._data.type[light.i] = type;
  260. }
  261. void RenderWorld::light_set_range(LightInstance light, f32 range)
  262. {
  263. CE_ASSERT(light.i < _light_manager._data.size, "Index out of bounds");
  264. _light_manager._data.range[light.i] = range;
  265. }
  266. void RenderWorld::light_set_intensity(LightInstance light, f32 intensity)
  267. {
  268. CE_ASSERT(light.i < _light_manager._data.size, "Index out of bounds");
  269. _light_manager._data.intensity[light.i] = intensity;
  270. }
  271. void RenderWorld::light_set_spot_angle(LightInstance light, f32 angle)
  272. {
  273. CE_ASSERT(light.i < _light_manager._data.size, "Index out of bounds");
  274. _light_manager._data.spot_angle[light.i] = angle;
  275. }
  276. void RenderWorld::light_debug_draw(LightInstance light, DebugLine& dl)
  277. {
  278. CE_ASSERT(light.i < _light_manager._data.size, "Index out of bounds");
  279. _light_manager.debug_draw(light.i, 1, dl);
  280. }
  281. void RenderWorld::update_transforms(const UnitId* begin, const UnitId* end, const Matrix4x4* world)
  282. {
  283. MeshManager::MeshInstanceData& mid = _mesh_manager._data;
  284. SpriteManager::SpriteInstanceData& sid = _sprite_manager._data;
  285. LightManager::LightInstanceData& lid = _light_manager._data;
  286. for (; begin != end; ++begin, ++world)
  287. {
  288. if (_mesh_manager.has(*begin))
  289. {
  290. MeshInstance mesh = _mesh_manager.mesh(*begin);
  291. mid.world[mesh.i] = *world;
  292. }
  293. if (_sprite_manager.has(*begin))
  294. {
  295. SpriteInstance sprite = _sprite_manager.sprite(*begin);
  296. sid.world[sprite.i] = *world;
  297. }
  298. if (_light_manager.has(*begin))
  299. {
  300. LightInstance light = _light_manager.light(*begin);
  301. lid.world[light.i] = *world;
  302. }
  303. }
  304. }
  305. void RenderWorld::render(const Matrix4x4& view)
  306. {
  307. MeshManager::MeshInstanceData& mid = _mesh_manager._data;
  308. SpriteManager::SpriteInstanceData& sid = _sprite_manager._data;
  309. LightManager::LightInstanceData& lid = _light_manager._data;
  310. for (u32 ll = 0; ll < lid.size; ++ll)
  311. {
  312. const Vector4 ldir = normalize(lid.world[ll].z) * view;
  313. const Vector3 lpos = translation(lid.world[ll]);
  314. bgfx::setUniform(_u_light_position, to_float_ptr(lpos));
  315. bgfx::setUniform(_u_light_direction, to_float_ptr(ldir));
  316. bgfx::setUniform(_u_light_color, to_float_ptr(lid.color[ll]));
  317. bgfx::setUniform(_u_light_range, &lid.range[ll]);
  318. bgfx::setUniform(_u_light_intensity, &lid.intensity[ll]);
  319. // Render meshes
  320. for (u32 i = 0; i < mid.first_hidden; ++i)
  321. {
  322. bgfx::setTransform(to_float_ptr(mid.world[i]));
  323. bgfx::setVertexBuffer(0, mid.mesh[i].vbh);
  324. bgfx::setIndexBuffer(mid.mesh[i].ibh);
  325. _material_manager->get(mid.material[i])->bind(*_resource_manager, *_shader_manager, VIEW_MESH);
  326. }
  327. }
  328. // Render sprites
  329. if (sid.first_hidden)
  330. {
  331. bgfx::VertexLayout layout;
  332. layout.begin()
  333. .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float)
  334. .add(bgfx::Attrib::TexCoord0, 2, bgfx::AttribType::Float, false)
  335. .end()
  336. ;
  337. bgfx::TransientVertexBuffer tvb;
  338. bgfx::allocTransientVertexBuffer(&tvb, 4*sid.first_hidden, layout);
  339. bgfx::TransientIndexBuffer tib;
  340. bgfx::allocTransientIndexBuffer(&tib, 6*sid.first_hidden);
  341. f32* vdata = (f32*)tvb.data;
  342. u16* idata = (u16*)tib.data;
  343. // Render sprites
  344. for (u32 i = 0; i < sid.first_hidden; ++i)
  345. {
  346. const f32* frame = sprite_resource::frame_data(sid.resource[i], sid.frame[i] % sid.resource[i]->num_frames);
  347. f32 u0 = frame[ 3]; // u
  348. f32 v0 = frame[ 4]; // v
  349. f32 u1 = frame[ 8]; // u
  350. f32 v1 = frame[ 9]; // v
  351. f32 u2 = frame[13]; // u
  352. f32 v2 = frame[14]; // v
  353. f32 u3 = frame[18]; // u
  354. f32 v3 = frame[19]; // v
  355. if (sid.flip_x[i])
  356. {
  357. f32 u;
  358. u = u0; u0 = u1; u1 = u;
  359. u = u2; u2 = u3; u3 = u;
  360. }
  361. if (sid.flip_y[i])
  362. {
  363. f32 v;
  364. v = v0; v0 = v2; v2 = v;
  365. v = v1; v1 = v3; v3 = v;
  366. }
  367. vdata[ 0] = frame[ 0]; // x
  368. vdata[ 1] = frame[ 1]; // y
  369. vdata[ 2] = frame[ 2]; // z
  370. vdata[ 3] = u0;
  371. vdata[ 4] = v0;
  372. vdata[ 5] = frame[ 5]; // x
  373. vdata[ 6] = frame[ 6]; // y
  374. vdata[ 7] = frame[ 7]; // z
  375. vdata[ 8] = u1;
  376. vdata[ 9] = v1;
  377. vdata[10] = frame[10]; // x
  378. vdata[11] = frame[11]; // y
  379. vdata[12] = frame[12]; // z
  380. vdata[13] = u2;
  381. vdata[14] = v2;
  382. vdata[15] = frame[15]; // x
  383. vdata[16] = frame[16]; // y
  384. vdata[17] = frame[17]; // z
  385. vdata[18] = u3;
  386. vdata[19] = v3;
  387. vdata += 20;
  388. *idata++ = i*4+0;
  389. *idata++ = i*4+1;
  390. *idata++ = i*4+2;
  391. *idata++ = i*4+0;
  392. *idata++ = i*4+2;
  393. *idata++ = i*4+3;
  394. bgfx::setTransform(to_float_ptr(sid.world[i]));
  395. bgfx::setVertexBuffer(0, &tvb);
  396. bgfx::setIndexBuffer(&tib, i*6, 6);
  397. _material_manager->get(sid.material[i])->bind(*_resource_manager
  398. , *_shader_manager
  399. , sid.layer[i] + VIEW_SPRITE_0
  400. , sid.depth[i]
  401. );
  402. }
  403. }
  404. }
  405. void RenderWorld::debug_draw(DebugLine& dl)
  406. {
  407. if (!_debug_drawing)
  408. return;
  409. MeshManager::MeshInstanceData& mid = _mesh_manager._data;
  410. for (u32 i = 0; i < mid.size; ++i)
  411. {
  412. const OBB& obb = mid.obb[i];
  413. const Matrix4x4& world = mid.world[i];
  414. dl.add_obb(obb.tm * world, obb.half_extents, COLOR4_RED);
  415. }
  416. _light_manager.debug_draw(0, _light_manager._data.size, dl);
  417. }
  418. void RenderWorld::enable_debug_drawing(bool enable)
  419. {
  420. _debug_drawing = enable;
  421. }
  422. void RenderWorld::unit_destroyed_callback(UnitId unit)
  423. {
  424. {
  425. MeshInstance first = mesh_instance(unit);
  426. if (is_valid(first))
  427. mesh_destroy(first);
  428. }
  429. {
  430. SpriteInstance first = sprite_instance(unit);
  431. if (is_valid(first))
  432. sprite_destroy(first);
  433. }
  434. {
  435. LightInstance first = light_instance(unit);
  436. if (is_valid(first))
  437. light_destroy(first);
  438. }
  439. }
  440. void RenderWorld::MeshManager::allocate(u32 num)
  441. {
  442. CE_ENSURE(num > _data.size);
  443. const u32 bytes = 0
  444. + num*sizeof(UnitId) + alignof(UnitId)
  445. + num*sizeof(MeshResource*) + alignof(MeshResource*)
  446. + num*sizeof(MeshGeometry*) + alignof(MeshGeometry*)
  447. + num*sizeof(MeshData) + alignof(MeshData)
  448. + num*sizeof(StringId64) + alignof(StringId64)
  449. + num*sizeof(Matrix4x4) + alignof(Matrix4x4)
  450. + num*sizeof(OBB) + alignof(OBB)
  451. ;
  452. MeshInstanceData new_data;
  453. new_data.size = _data.size;
  454. new_data.capacity = num;
  455. new_data.buffer = _allocator->allocate(bytes);
  456. new_data.first_hidden = _data.first_hidden;
  457. new_data.unit = (UnitId* )memory::align_top(new_data.buffer, alignof(UnitId ));
  458. new_data.resource = (const MeshResource**)memory::align_top(new_data.unit + num, alignof(MeshResource*));
  459. new_data.geometry = (const MeshGeometry**)memory::align_top(new_data.resource + num, alignof(MeshGeometry*));
  460. new_data.mesh = (MeshData* )memory::align_top(new_data.geometry + num, alignof(MeshData ));
  461. new_data.material = (StringId64* )memory::align_top(new_data.mesh + num, alignof(StringId64 ));
  462. new_data.world = (Matrix4x4* )memory::align_top(new_data.material + num, alignof(Matrix4x4 ));
  463. new_data.obb = (OBB* )memory::align_top(new_data.world + num, alignof(OBB ));
  464. memcpy(new_data.unit, _data.unit, _data.size * sizeof(UnitId));
  465. memcpy(new_data.resource, _data.resource, _data.size * sizeof(MeshResource*));
  466. memcpy(new_data.geometry, _data.geometry, _data.size * sizeof(MeshGeometry*));
  467. memcpy(new_data.mesh, _data.mesh, _data.size * sizeof(MeshData));
  468. memcpy(new_data.material, _data.material, _data.size * sizeof(StringId64));
  469. memcpy(new_data.world, _data.world, _data.size * sizeof(Matrix4x4));
  470. memcpy(new_data.obb, _data.obb, _data.size * sizeof(OBB));
  471. _allocator->deallocate(_data.buffer);
  472. _data = new_data;
  473. }
  474. void RenderWorld::MeshManager::grow()
  475. {
  476. allocate(_data.capacity * 2 + 1);
  477. }
  478. MeshInstance RenderWorld::MeshManager::create(UnitId unit, const MeshResource* mr, const MeshRendererDesc& mrd, const Matrix4x4& tr)
  479. {
  480. CE_ASSERT(!hash_map::has(_map, unit), "Unit already has a mesh component");
  481. if (_data.size == _data.capacity)
  482. grow();
  483. const u32 last = _data.size;
  484. const MeshGeometry* mg = mr->geometry(mrd.geometry_name);
  485. _data.unit[last] = unit;
  486. _data.resource[last] = mr;
  487. _data.geometry[last] = mg;
  488. _data.mesh[last].vbh = mg->vertex_buffer;
  489. _data.mesh[last].ibh = mg->index_buffer;
  490. _data.material[last] = mrd.material_resource;
  491. _data.world[last] = tr;
  492. _data.obb[last] = mg->obb;
  493. hash_map::set(_map, unit, last);
  494. ++_data.size;
  495. if (mrd.visible)
  496. {
  497. if (last >= _data.first_hidden)
  498. {
  499. // _data now contains a visible item in its hidden partition.
  500. swap(last, _data.first_hidden);
  501. ++_data.first_hidden;
  502. return make_instance(_data.first_hidden-1);
  503. }
  504. ++_data.first_hidden;
  505. }
  506. CE_ENSURE(last >= _data.first_hidden);
  507. return make_instance(last);
  508. }
  509. void RenderWorld::MeshManager::destroy(MeshInstance inst)
  510. {
  511. CE_ASSERT(inst.i < _data.size, "Index out of bounds");
  512. const u32 last = _data.size - 1;
  513. const UnitId u = _data.unit[inst.i];
  514. const UnitId last_u = _data.unit[last];
  515. _data.unit[inst.i] = _data.unit[last];
  516. _data.resource[inst.i] = _data.resource[last];
  517. _data.geometry[inst.i] = _data.geometry[last];
  518. _data.mesh[inst.i].vbh = _data.mesh[last].vbh;
  519. _data.mesh[inst.i].ibh = _data.mesh[last].ibh;
  520. _data.material[inst.i] = _data.material[last];
  521. _data.world[inst.i] = _data.world[last];
  522. _data.obb[inst.i] = _data.obb[last];
  523. hash_map::set(_map, last_u, inst.i);
  524. hash_map::remove(_map, u);
  525. --_data.size;
  526. // If item was hidden.
  527. if (inst.i >= _data.first_hidden)
  528. return;
  529. // If item was visible *and* last item was hidden.
  530. if (last >= _data.first_hidden)
  531. swap(inst.i, _data.first_hidden-1);
  532. --_data.first_hidden;
  533. }
  534. void RenderWorld::MeshManager::swap(u32 inst_a, u32 inst_b)
  535. {
  536. if (inst_a == inst_b)
  537. return;
  538. const UnitId unit_a = _data.unit[inst_a];
  539. const UnitId unit_b = _data.unit[inst_b];
  540. exchange(_data.unit[inst_a], _data.unit[inst_b]);
  541. exchange(_data.resource[inst_a], _data.resource[inst_b]);
  542. exchange(_data.geometry[inst_a], _data.geometry[inst_b]);
  543. exchange(_data.mesh[inst_a], _data.mesh[inst_b]);
  544. exchange(_data.material[inst_a], _data.material[inst_b]);
  545. exchange(_data.world[inst_a], _data.world[inst_b]);
  546. exchange(_data.obb[inst_a], _data.obb[inst_b]);
  547. hash_map::set(_map, unit_a, inst_b);
  548. hash_map::set(_map, unit_b, inst_a);
  549. }
  550. bool RenderWorld::MeshManager::has(UnitId unit)
  551. {
  552. return is_valid(mesh(unit));
  553. }
  554. void RenderWorld::MeshManager::set_visible(MeshInstance inst, bool visible)
  555. {
  556. if (visible)
  557. {
  558. if (inst.i < _data.first_hidden)
  559. return; // Already visible.
  560. swap(inst.i, _data.first_hidden);
  561. ++_data.first_hidden;
  562. }
  563. else
  564. {
  565. if (inst.i >= _data.first_hidden)
  566. return; // Already hidden.
  567. swap(inst.i, _data.first_hidden-1);
  568. --_data.first_hidden;
  569. }
  570. }
  571. MeshInstance RenderWorld::MeshManager::mesh(UnitId unit)
  572. {
  573. return make_instance(hash_map::get(_map, unit, UINT32_MAX));
  574. }
  575. void RenderWorld::MeshManager::destroy()
  576. {
  577. _allocator->deallocate(_data.buffer);
  578. }
  579. void RenderWorld::SpriteManager::allocate(u32 num)
  580. {
  581. CE_ENSURE(num > _data.size);
  582. const u32 bytes = 0
  583. + num*sizeof(UnitId) + alignof(UnitId)
  584. + num*sizeof(SpriteResource**) + alignof(SpriteResource*)
  585. + num*sizeof(StringId64) + alignof(StringId64)
  586. + num*sizeof(u32) + alignof(u32)
  587. + num*sizeof(Matrix4x4) + alignof(Matrix4x4)
  588. + num*sizeof(AABB) + alignof(AABB)
  589. + num*sizeof(bool) + alignof(bool)
  590. + num*sizeof(bool) + alignof(bool)
  591. + num*sizeof(u32) + alignof(u32)
  592. + num*sizeof(u32) + alignof(u32)
  593. ;
  594. SpriteInstanceData new_data;
  595. new_data.size = _data.size;
  596. new_data.capacity = num;
  597. new_data.buffer = _allocator->allocate(bytes);
  598. new_data.first_hidden = _data.first_hidden;
  599. new_data.unit = (UnitId* )memory::align_top(new_data.buffer, alignof(UnitId ));
  600. new_data.resource = (const SpriteResource**)memory::align_top(new_data.unit + num, alignof(SpriteResource*));
  601. new_data.material = (StringId64* )memory::align_top(new_data.resource + num, alignof(StringId64 ));
  602. new_data.frame = (u32* )memory::align_top(new_data.material + num, alignof(u32 ));
  603. new_data.world = (Matrix4x4* )memory::align_top(new_data.frame + num, alignof(Matrix4x4 ));
  604. new_data.aabb = (AABB* )memory::align_top(new_data.world + num, alignof(AABB ));
  605. new_data.flip_x = (bool* )memory::align_top(new_data.aabb + num, alignof(bool ));
  606. new_data.flip_y = (bool* )memory::align_top(new_data.flip_x + num, alignof(bool ));
  607. new_data.layer = (u32* )memory::align_top(new_data.flip_y + num, alignof(u32 ));
  608. new_data.depth = (u32* )memory::align_top(new_data.layer + num, alignof(u32 ));
  609. memcpy(new_data.unit, _data.unit, _data.size * sizeof(UnitId));
  610. memcpy(new_data.resource, _data.resource, _data.size * sizeof(SpriteResource**));
  611. memcpy(new_data.material, _data.material, _data.size * sizeof(StringId64));
  612. memcpy(new_data.frame, _data.frame, _data.size * sizeof(u32));
  613. memcpy(new_data.world, _data.world, _data.size * sizeof(Matrix4x4));
  614. memcpy(new_data.aabb, _data.aabb, _data.size * sizeof(AABB));
  615. memcpy(new_data.flip_x, _data.flip_x, _data.size * sizeof(bool));
  616. memcpy(new_data.flip_y, _data.flip_y, _data.size * sizeof(bool));
  617. memcpy(new_data.layer, _data.layer, _data.size * sizeof(u32));
  618. memcpy(new_data.depth, _data.depth, _data.size * sizeof(u32));
  619. _allocator->deallocate(_data.buffer);
  620. _data = new_data;
  621. }
  622. void RenderWorld::SpriteManager::grow()
  623. {
  624. allocate(_data.capacity * 2 + 1);
  625. }
  626. SpriteInstance RenderWorld::SpriteManager::create(UnitId unit, const SpriteResource* sr, const SpriteRendererDesc& srd, const Matrix4x4& tr)
  627. {
  628. CE_ASSERT(!hash_map::has(_map, unit), "Unit already has a sprite component");
  629. if (_data.size == _data.capacity)
  630. grow();
  631. const u32 last = _data.size;
  632. _data.unit[last] = unit;
  633. _data.resource[last] = sr;
  634. _data.material[last] = srd.material_resource;
  635. _data.frame[last] = 0;
  636. _data.world[last] = tr;
  637. _data.aabb[last] = AABB();
  638. _data.flip_x[last] = false;
  639. _data.flip_y[last] = false;
  640. _data.layer[last] = srd.layer;
  641. _data.depth[last] = srd.depth;
  642. hash_map::set(_map, unit, last);
  643. ++_data.size;
  644. if (srd.visible)
  645. {
  646. if (last >= _data.first_hidden)
  647. {
  648. // _data now contains a visible item in its hidden partition.
  649. swap(last, _data.first_hidden);
  650. ++_data.first_hidden;
  651. return make_instance(_data.first_hidden-1);
  652. }
  653. ++_data.first_hidden;
  654. }
  655. CE_ENSURE(last >= _data.first_hidden);
  656. return make_instance(last);
  657. }
  658. void RenderWorld::SpriteManager::destroy(SpriteInstance inst)
  659. {
  660. CE_ASSERT(inst.i < _data.size, "Index out of bounds");
  661. const u32 last = _data.size - 1;
  662. const UnitId u = _data.unit[inst.i];
  663. const UnitId last_u = _data.unit[last];
  664. _data.unit[inst.i] = _data.unit[last];
  665. _data.resource[inst.i] = _data.resource[last];
  666. _data.material[inst.i] = _data.material[last];
  667. _data.frame[inst.i] = _data.frame[last];
  668. _data.world[inst.i] = _data.world[last];
  669. _data.aabb[inst.i] = _data.aabb[last];
  670. _data.flip_x[inst.i] = _data.flip_x[last];
  671. _data.flip_y[inst.i] = _data.flip_y[last];
  672. _data.layer[inst.i] = _data.layer[last];
  673. _data.depth[inst.i] = _data.depth[last];
  674. hash_map::set(_map, last_u, inst.i);
  675. hash_map::remove(_map, u);
  676. --_data.size;
  677. // If item was hidden.
  678. if (inst.i >= _data.first_hidden)
  679. return;
  680. // If item was visible *and* last item was hidden.
  681. if (last >= _data.first_hidden)
  682. swap(inst.i, _data.first_hidden-1);
  683. --_data.first_hidden;
  684. }
  685. void RenderWorld::SpriteManager::swap(u32 inst_a, u32 inst_b)
  686. {
  687. if (inst_a == inst_b)
  688. return;
  689. const UnitId unit_a = _data.unit[inst_a];
  690. const UnitId unit_b = _data.unit[inst_b];
  691. exchange(_data.unit[inst_a], _data.unit[inst_b]);
  692. exchange(_data.resource[inst_a], _data.resource[inst_b]);
  693. exchange(_data.material[inst_a], _data.material[inst_b]);
  694. exchange(_data.frame[inst_a], _data.frame[inst_b]);
  695. exchange(_data.world[inst_a], _data.world[inst_b]);
  696. exchange(_data.aabb[inst_a], _data.aabb[inst_b]);
  697. exchange(_data.flip_x[inst_a], _data.flip_x[inst_b]);
  698. exchange(_data.flip_y[inst_a], _data.flip_y[inst_b]);
  699. exchange(_data.layer[inst_a], _data.layer[inst_b]);
  700. exchange(_data.depth[inst_a], _data.depth[inst_b]);
  701. hash_map::set(_map, unit_a, inst_b);
  702. hash_map::set(_map, unit_b, inst_a);
  703. }
  704. bool RenderWorld::SpriteManager::has(UnitId unit)
  705. {
  706. return is_valid(sprite(unit));
  707. }
  708. void RenderWorld::SpriteManager::set_visible(SpriteInstance inst, bool visible)
  709. {
  710. if (visible)
  711. {
  712. if (inst.i < _data.first_hidden)
  713. return; // Already visible.
  714. swap(inst.i, _data.first_hidden);
  715. ++_data.first_hidden;
  716. }
  717. else
  718. {
  719. if (inst.i >= _data.first_hidden)
  720. return; // Already hidden.
  721. swap(inst.i, _data.first_hidden-1);
  722. --_data.first_hidden;
  723. }
  724. }
  725. SpriteInstance RenderWorld::SpriteManager::sprite(UnitId unit)
  726. {
  727. return make_instance(hash_map::get(_map, unit, UINT32_MAX));
  728. }
  729. void RenderWorld::SpriteManager::destroy()
  730. {
  731. _allocator->deallocate(_data.buffer);
  732. }
  733. void RenderWorld::LightManager::allocate(u32 num)
  734. {
  735. CE_ENSURE(num > _data.size);
  736. const u32 bytes = 0
  737. + num*sizeof(UnitId) + alignof(UnitId)
  738. + num*sizeof(Matrix4x4) + alignof(Matrix4x4)
  739. + num*sizeof(f32) + alignof(f32)
  740. + num*sizeof(f32) + alignof(f32)
  741. + num*sizeof(f32) + alignof(f32)
  742. + num*sizeof(Color4) + alignof(Color4)
  743. + num*sizeof(u32) + alignof(u32)
  744. ;
  745. LightInstanceData new_data;
  746. new_data.size = _data.size;
  747. new_data.capacity = num;
  748. new_data.buffer = _allocator->allocate(bytes);
  749. new_data.unit = (UnitId* )memory::align_top(new_data.buffer, alignof(UnitId ));
  750. new_data.world = (Matrix4x4*)memory::align_top(new_data.unit + num, alignof(Matrix4x4));
  751. new_data.range = (f32* )memory::align_top(new_data.world + num, alignof(f32 ));
  752. new_data.intensity = (f32* )memory::align_top(new_data.range + num, alignof(f32 ));
  753. new_data.spot_angle = (f32* )memory::align_top(new_data.intensity + num, alignof(f32 ));
  754. new_data.color = (Color4* )memory::align_top(new_data.spot_angle + num, alignof(Color4 ));
  755. new_data.type = (u32* )memory::align_top(new_data.color + num, alignof(u32 ));
  756. memcpy(new_data.unit, _data.unit, _data.size * sizeof(UnitId));
  757. memcpy(new_data.world, _data.world, _data.size * sizeof(Matrix4x4));
  758. memcpy(new_data.range, _data.range, _data.size * sizeof(f32));
  759. memcpy(new_data.intensity, _data.intensity, _data.size * sizeof(f32));
  760. memcpy(new_data.spot_angle, _data.spot_angle, _data.size * sizeof(f32));
  761. memcpy(new_data.color, _data.color, _data.size * sizeof(Color4));
  762. memcpy(new_data.type, _data.type, _data.size * sizeof(u32));
  763. _allocator->deallocate(_data.buffer);
  764. _data = new_data;
  765. }
  766. void RenderWorld::LightManager::grow()
  767. {
  768. allocate(_data.capacity * 2 + 1);
  769. }
  770. LightInstance RenderWorld::LightManager::create(UnitId unit, const LightDesc& ld, const Matrix4x4& tr)
  771. {
  772. CE_ASSERT(!hash_map::has(_map, unit), "Unit already has a light component");
  773. if (_data.size == _data.capacity)
  774. grow();
  775. const u32 last = _data.size;
  776. _data.unit[last] = unit;
  777. _data.world[last] = tr;
  778. _data.range[last] = ld.range;
  779. _data.intensity[last] = ld.intensity;
  780. _data.spot_angle[last] = ld.spot_angle;
  781. _data.color[last] = vector4(ld.color.x, ld.color.y, ld.color.z, 1.0f);
  782. _data.type[last] = ld.type;
  783. ++_data.size;
  784. hash_map::set(_map, unit, last);
  785. return make_instance(last);
  786. }
  787. void RenderWorld::LightManager::destroy(LightInstance light)
  788. {
  789. CE_ASSERT(light.i < _data.size, "Index out of bounds");
  790. const u32 last = _data.size - 1;
  791. const UnitId u = _data.unit[light.i];
  792. const UnitId last_u = _data.unit[last];
  793. _data.unit[light.i] = _data.unit[last];
  794. _data.world[light.i] = _data.world[last];
  795. _data.range[light.i] = _data.range[last];
  796. _data.intensity[light.i] = _data.intensity[last];
  797. _data.spot_angle[light.i] = _data.spot_angle[last];
  798. _data.color[light.i] = _data.color[last];
  799. _data.type[light.i] = _data.type[last];
  800. --_data.size;
  801. hash_map::set(_map, last_u, light.i);
  802. hash_map::remove(_map, u);
  803. }
  804. bool RenderWorld::LightManager::has(UnitId unit)
  805. {
  806. return is_valid(light(unit));
  807. }
  808. LightInstance RenderWorld::LightManager::light(UnitId unit)
  809. {
  810. return make_instance(hash_map::get(_map, unit, UINT32_MAX));
  811. }
  812. void RenderWorld::LightManager::destroy()
  813. {
  814. _allocator->deallocate(_data.buffer);
  815. }
  816. void RenderWorld::LightManager::debug_draw(u32 start_index, u32 num, DebugLine& dl)
  817. {
  818. for (u32 i = start_index; i < start_index + num; ++i)
  819. {
  820. const Vector3 pos = translation(_data.world[i]);
  821. const Vector3 dir = -z(_data.world[i]);
  822. switch (_data.type[i])
  823. {
  824. case LightType::DIRECTIONAL:
  825. {
  826. const Vector3 end = pos + dir*3.0f;
  827. dl.add_line(pos, end, COLOR4_YELLOW);
  828. dl.add_cone(pos + dir*2.8f, end, 0.1f, COLOR4_YELLOW);
  829. }
  830. break;
  831. case LightType::OMNI:
  832. dl.add_sphere(pos, _data.range[i], COLOR4_YELLOW);
  833. break;
  834. case LightType::SPOT:
  835. {
  836. const f32 angle = _data.spot_angle[i];
  837. const f32 range = _data.range[i];
  838. const f32 radius = ftan(angle)*range;
  839. dl.add_cone(pos + range*dir, pos, radius, COLOR4_YELLOW);
  840. }
  841. break;
  842. default:
  843. CE_FATAL("Unknown light type");
  844. break;
  845. }
  846. }
  847. }
  848. } // namespace crown