rasterizer.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743
  1. /**************************************************************************/
  2. /* rasterizer.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "rasterizer.h"
  31. #include "core/os/os.h"
  32. #include "core/print_string.h"
  33. #if defined(DEBUG_ENABLED) && defined(TOOLS_ENABLED)
  34. #include "core/project_settings.h"
  35. #endif
  36. Rasterizer *(*Rasterizer::_create_func)() = nullptr;
  37. Rasterizer *Rasterizer::create() {
  38. return _create_func();
  39. }
  40. RasterizerStorage *RasterizerStorage::base_singleton = nullptr;
  41. RasterizerStorage::RasterizerStorage() {
  42. base_singleton = this;
  43. }
  44. bool RasterizerStorage::material_uses_tangents(RID p_material) {
  45. return false;
  46. }
  47. bool RasterizerStorage::material_uses_ensure_correct_normals(RID p_material) {
  48. return false;
  49. }
  50. void RasterizerStorage::InterpolationData::notify_free_multimesh(RID p_rid) {
  51. // print_line("free multimesh " + itos(p_rid.get_id()));
  52. // if the instance was on any of the lists, remove
  53. multimesh_interpolate_update_list.erase_multiple_unordered(p_rid);
  54. multimesh_transform_update_lists[0].erase_multiple_unordered(p_rid);
  55. multimesh_transform_update_lists[1].erase_multiple_unordered(p_rid);
  56. }
  57. void RasterizerStorage::update_interpolation_tick(bool p_process) {
  58. // detect any that were on the previous transform list that are no longer active,
  59. // we should remove them from the interpolate list
  60. for (unsigned int n = 0; n < _interpolation_data.multimesh_transform_update_list_prev->size(); n++) {
  61. const RID &rid = (*_interpolation_data.multimesh_transform_update_list_prev)[n];
  62. bool active = true;
  63. // no longer active? (either the instance deleted or no longer being transformed)
  64. MMInterpolator *mmi = _multimesh_get_interpolator(rid);
  65. if (mmi && !mmi->on_transform_update_list) {
  66. active = false;
  67. mmi->on_interpolate_update_list = false;
  68. // make sure the most recent transform is set
  69. // copy data rather than use Pool = function?
  70. mmi->_data_interpolated = mmi->_data_curr;
  71. // and that both prev and current are the same, just in case of any interpolations
  72. mmi->_data_prev = mmi->_data_curr;
  73. // make sure are updated one more time to ensure the AABBs are correct
  74. //_instance_queue_update(instance, true);
  75. // Update the actual stable buffer to the backend.
  76. _multimesh_set_as_bulk_array(rid, mmi->_data_interpolated);
  77. }
  78. if (!mmi) {
  79. active = false;
  80. }
  81. if (!active) {
  82. _interpolation_data.multimesh_interpolate_update_list.erase(rid);
  83. }
  84. }
  85. if (p_process) {
  86. for (unsigned int i = 0; i < _interpolation_data.multimesh_transform_update_list_curr->size(); i++) {
  87. const RID &rid = (*_interpolation_data.multimesh_transform_update_list_curr)[i];
  88. MMInterpolator *mmi = _multimesh_get_interpolator(rid);
  89. if (mmi) {
  90. // reset for next tick
  91. mmi->on_transform_update_list = false;
  92. mmi->_data_prev = mmi->_data_curr;
  93. }
  94. } // for n
  95. }
  96. // if any have left the transform list, remove from the interpolate list
  97. // we maintain a mirror list for the transform updates, so we can detect when an instance
  98. // is no longer being transformed, and remove it from the interpolate list
  99. SWAP(_interpolation_data.multimesh_transform_update_list_curr, _interpolation_data.multimesh_transform_update_list_prev);
  100. // prepare for the next iteration
  101. _interpolation_data.multimesh_transform_update_list_curr->clear();
  102. }
  103. void RasterizerStorage::update_interpolation_frame(bool p_process) {
  104. if (p_process) {
  105. // Only need 32 bit for interpolation, don't use real_t
  106. float f = Engine::get_singleton()->get_physics_interpolation_fraction();
  107. for (unsigned int c = 0; c < _interpolation_data.multimesh_interpolate_update_list.size(); c++) {
  108. const RID &rid = _interpolation_data.multimesh_interpolate_update_list[c];
  109. // We could use the TransformInterpolator here to slerp transforms, but that might be too expensive,
  110. // so just using a Basis lerp for now.
  111. MMInterpolator *mmi = _multimesh_get_interpolator(rid);
  112. if (mmi) {
  113. // make sure arrays are correct size
  114. DEV_ASSERT(mmi->_data_prev.size() == mmi->_data_curr.size());
  115. if (mmi->_data_interpolated.size() < mmi->_data_curr.size()) {
  116. mmi->_data_interpolated.resize(mmi->_data_curr.size());
  117. }
  118. DEV_ASSERT(mmi->_data_interpolated.size() >= mmi->_data_curr.size());
  119. DEV_ASSERT((mmi->_data_curr.size() % mmi->_stride) == 0);
  120. int num = mmi->_data_curr.size() / mmi->_stride;
  121. PoolVector<float>::Read r_prev = mmi->_data_prev.read();
  122. PoolVector<float>::Read r_curr = mmi->_data_curr.read();
  123. PoolVector<float>::Write w = mmi->_data_interpolated.write();
  124. const float *pf_prev = r_prev.ptr();
  125. const float *pf_curr = r_curr.ptr();
  126. float *pf_int = w.ptr();
  127. bool use_lerp = mmi->quality == 0;
  128. // temporary transform (needed for swizzling)
  129. // (transform prev, curr and result)
  130. Transform tp, tc, tr;
  131. // Test for cache friendliness versus doing branchless
  132. for (int n = 0; n < num; n++) {
  133. // Transform
  134. if (use_lerp) {
  135. for (int i = 0; i < mmi->_vf_size_xform; i++) {
  136. float a = pf_prev[i];
  137. float b = pf_curr[i];
  138. pf_int[i] = (a + ((b - a) * f));
  139. }
  140. } else {
  141. // Silly swizzling, this will slow things down. no idea why it is using this format
  142. // .. maybe due to the shader.
  143. tp.basis.elements[0][0] = pf_prev[0];
  144. tp.basis.elements[0][1] = pf_prev[1];
  145. tp.basis.elements[0][2] = pf_prev[2];
  146. tp.basis.elements[1][0] = pf_prev[4];
  147. tp.basis.elements[1][1] = pf_prev[5];
  148. tp.basis.elements[1][2] = pf_prev[6];
  149. tp.basis.elements[2][0] = pf_prev[8];
  150. tp.basis.elements[2][1] = pf_prev[9];
  151. tp.basis.elements[2][2] = pf_prev[10];
  152. tp.origin.x = pf_prev[3];
  153. tp.origin.y = pf_prev[7];
  154. tp.origin.z = pf_prev[11];
  155. tc.basis.elements[0][0] = pf_curr[0];
  156. tc.basis.elements[0][1] = pf_curr[1];
  157. tc.basis.elements[0][2] = pf_curr[2];
  158. tc.basis.elements[1][0] = pf_curr[4];
  159. tc.basis.elements[1][1] = pf_curr[5];
  160. tc.basis.elements[1][2] = pf_curr[6];
  161. tc.basis.elements[2][0] = pf_curr[8];
  162. tc.basis.elements[2][1] = pf_curr[9];
  163. tc.basis.elements[2][2] = pf_curr[10];
  164. tc.origin.x = pf_curr[3];
  165. tc.origin.y = pf_curr[7];
  166. tc.origin.z = pf_curr[11];
  167. TransformInterpolator::interpolate_transform(tp, tc, tr, f);
  168. pf_int[0] = tr.basis.elements[0][0];
  169. pf_int[1] = tr.basis.elements[0][1];
  170. pf_int[2] = tr.basis.elements[0][2];
  171. pf_int[4] = tr.basis.elements[1][0];
  172. pf_int[5] = tr.basis.elements[1][1];
  173. pf_int[6] = tr.basis.elements[1][2];
  174. pf_int[8] = tr.basis.elements[2][0];
  175. pf_int[9] = tr.basis.elements[2][1];
  176. pf_int[10] = tr.basis.elements[2][2];
  177. pf_int[3] = tr.origin.x;
  178. pf_int[7] = tr.origin.y;
  179. pf_int[11] = tr.origin.z;
  180. }
  181. pf_prev += mmi->_vf_size_xform;
  182. pf_curr += mmi->_vf_size_xform;
  183. pf_int += mmi->_vf_size_xform;
  184. // Color
  185. if (mmi->_vf_size_color == 1) {
  186. const uint8_t *p8_prev = (const uint8_t *)pf_prev;
  187. const uint8_t *p8_curr = (const uint8_t *)pf_curr;
  188. uint8_t *p8_int = (uint8_t *)pf_int;
  189. _interpolate_RGBA8(p8_prev, p8_curr, p8_int, f);
  190. pf_prev += 1;
  191. pf_curr += 1;
  192. pf_int += 1;
  193. } else if (mmi->_vf_size_color == 4) {
  194. for (int i = 0; i < 4; i++) {
  195. pf_int[i] = pf_prev[i] + ((pf_curr[i] - pf_prev[i]) * f);
  196. }
  197. pf_prev += 4;
  198. pf_curr += 4;
  199. pf_int += 4;
  200. }
  201. // Custom Data
  202. if (mmi->_vf_size_data == 1) {
  203. const uint8_t *p8_prev = (const uint8_t *)pf_prev;
  204. const uint8_t *p8_curr = (const uint8_t *)pf_curr;
  205. uint8_t *p8_int = (uint8_t *)pf_int;
  206. _interpolate_RGBA8(p8_prev, p8_curr, p8_int, f);
  207. pf_prev += 1;
  208. pf_curr += 1;
  209. pf_int += 1;
  210. } else if (mmi->_vf_size_data == 4) {
  211. for (int i = 0; i < 4; i++) {
  212. pf_int[i] = pf_prev[i] + ((pf_curr[i] - pf_prev[i]) * f);
  213. }
  214. pf_prev += 4;
  215. pf_curr += 4;
  216. pf_int += 4;
  217. }
  218. }
  219. _multimesh_set_as_bulk_array(rid, mmi->_data_interpolated);
  220. // make sure AABBs are constantly up to date through the interpolation?
  221. // NYI
  222. }
  223. } // for n
  224. }
  225. }
  226. RID RasterizerStorage::multimesh_create() {
  227. return _multimesh_create();
  228. }
  229. void RasterizerStorage::multimesh_allocate(RID p_multimesh, int p_instances, VS::MultimeshTransformFormat p_transform_format, VS::MultimeshColorFormat p_color_format, VS::MultimeshCustomDataFormat p_data) {
  230. MMInterpolator *mmi = _multimesh_get_interpolator(p_multimesh);
  231. if (mmi) {
  232. mmi->_transform_format = p_transform_format;
  233. mmi->_color_format = p_color_format;
  234. mmi->_data_format = p_data;
  235. mmi->_num_instances = p_instances;
  236. mmi->_vf_size_xform = p_transform_format == VS::MULTIMESH_TRANSFORM_3D ? 12 : 8;
  237. switch (p_color_format) {
  238. default: {
  239. mmi->_vf_size_color = 0;
  240. } break;
  241. case VS::MULTIMESH_COLOR_8BIT: {
  242. mmi->_vf_size_color = 1;
  243. } break;
  244. case VS::MULTIMESH_COLOR_FLOAT: {
  245. mmi->_vf_size_color = 4;
  246. } break;
  247. }
  248. switch (p_data) {
  249. default: {
  250. mmi->_vf_size_data = 0;
  251. } break;
  252. case VS::MULTIMESH_CUSTOM_DATA_8BIT: {
  253. mmi->_vf_size_data = 1;
  254. } break;
  255. case VS::MULTIMESH_CUSTOM_DATA_FLOAT: {
  256. mmi->_vf_size_data = 4;
  257. } break;
  258. }
  259. mmi->_stride = mmi->_vf_size_xform + mmi->_vf_size_color + mmi->_vf_size_data;
  260. int size_in_floats = p_instances * mmi->_stride;
  261. mmi->_data_curr.resize(size_in_floats);
  262. mmi->_data_prev.resize(size_in_floats);
  263. mmi->_data_interpolated.resize(size_in_floats);
  264. mmi->_data_curr.fill(0);
  265. mmi->_data_prev.fill(0);
  266. mmi->_data_interpolated.fill(0);
  267. }
  268. return _multimesh_allocate(p_multimesh, p_instances, p_transform_format, p_color_format, p_data);
  269. }
  270. int RasterizerStorage::multimesh_get_instance_count(RID p_multimesh) const {
  271. return _multimesh_get_instance_count(p_multimesh);
  272. }
  273. void RasterizerStorage::multimesh_set_mesh(RID p_multimesh, RID p_mesh) {
  274. _multimesh_set_mesh(p_multimesh, p_mesh);
  275. }
  276. void RasterizerStorage::multimesh_instance_set_transform(RID p_multimesh, int p_index, const Transform &p_transform) {
  277. MMInterpolator *mmi = _multimesh_get_interpolator(p_multimesh);
  278. if (mmi) {
  279. if (mmi->interpolated) {
  280. ERR_FAIL_COND(p_index >= mmi->_num_instances);
  281. ERR_FAIL_COND(mmi->_vf_size_xform != 12);
  282. PoolVector<float>::Write w = mmi->_data_curr.write();
  283. int start = p_index * mmi->_stride;
  284. float *ptr = w.ptr();
  285. ptr += start;
  286. const Transform &t = p_transform;
  287. ptr[0] = t.basis.elements[0][0];
  288. ptr[1] = t.basis.elements[0][1];
  289. ptr[2] = t.basis.elements[0][2];
  290. ptr[3] = t.origin.x;
  291. ptr[4] = t.basis.elements[1][0];
  292. ptr[5] = t.basis.elements[1][1];
  293. ptr[6] = t.basis.elements[1][2];
  294. ptr[7] = t.origin.y;
  295. ptr[8] = t.basis.elements[2][0];
  296. ptr[9] = t.basis.elements[2][1];
  297. ptr[10] = t.basis.elements[2][2];
  298. ptr[11] = t.origin.z;
  299. _multimesh_add_to_interpolation_lists(p_multimesh, *mmi);
  300. #if defined(DEBUG_ENABLED) && defined(TOOLS_ENABLED)
  301. if (!Engine::get_singleton()->is_in_physics_frame()) {
  302. PHYSICS_INTERPOLATION_WARNING("Interpolated MultiMesh triggered from outside physics process");
  303. }
  304. #endif
  305. return;
  306. }
  307. }
  308. _multimesh_instance_set_transform(p_multimesh, p_index, p_transform);
  309. }
  310. void RasterizerStorage::multimesh_instance_set_transform_2d(RID p_multimesh, int p_index, const Transform2D &p_transform) {
  311. _multimesh_instance_set_transform_2d(p_multimesh, p_index, p_transform);
  312. }
  313. void RasterizerStorage::multimesh_instance_set_color(RID p_multimesh, int p_index, const Color &p_color) {
  314. MMInterpolator *mmi = _multimesh_get_interpolator(p_multimesh);
  315. if (mmi) {
  316. if (mmi->interpolated) {
  317. ERR_FAIL_COND(p_index >= mmi->_num_instances);
  318. ERR_FAIL_COND(mmi->_vf_size_color == 0);
  319. PoolVector<float>::Write w = mmi->_data_curr.write();
  320. int start = (p_index * mmi->_stride) + mmi->_vf_size_xform;
  321. float *ptr = w.ptr();
  322. ptr += start;
  323. if (mmi->_vf_size_color == 4) {
  324. for (int n = 0; n < 4; n++) {
  325. ptr[n] = p_color.components[n];
  326. }
  327. } else {
  328. #ifdef DEV_ENABLED
  329. // The options are currently 4, 1, or zero, but just in case this changes in future...
  330. ERR_FAIL_COND(mmi->_vf_size_color != 1);
  331. #endif
  332. uint32_t *pui = (uint32_t *)ptr;
  333. *pui = p_color.to_rgba32();
  334. }
  335. _multimesh_add_to_interpolation_lists(p_multimesh, *mmi);
  336. return;
  337. }
  338. }
  339. _multimesh_instance_set_color(p_multimesh, p_index, p_color);
  340. }
  341. void RasterizerStorage::multimesh_instance_set_custom_data(RID p_multimesh, int p_index, const Color &p_color) {
  342. MMInterpolator *mmi = _multimesh_get_interpolator(p_multimesh);
  343. if (mmi) {
  344. if (mmi->interpolated) {
  345. ERR_FAIL_COND(p_index >= mmi->_num_instances);
  346. ERR_FAIL_COND(mmi->_vf_size_data == 0);
  347. PoolVector<float>::Write w = mmi->_data_curr.write();
  348. int start = (p_index * mmi->_stride) + mmi->_vf_size_xform + mmi->_vf_size_color;
  349. float *ptr = w.ptr();
  350. ptr += start;
  351. if (mmi->_vf_size_data == 4) {
  352. for (int n = 0; n < 4; n++) {
  353. ptr[n] = p_color.components[n];
  354. }
  355. } else {
  356. #ifdef DEV_ENABLED
  357. // The options are currently 4, 1, or zero, but just in case this changes in future...
  358. ERR_FAIL_COND(mmi->_vf_size_data != 1);
  359. #endif
  360. uint32_t *pui = (uint32_t *)ptr;
  361. *pui = p_color.to_rgba32();
  362. }
  363. _multimesh_add_to_interpolation_lists(p_multimesh, *mmi);
  364. return;
  365. }
  366. }
  367. _multimesh_instance_set_custom_data(p_multimesh, p_index, p_color);
  368. }
  369. RID RasterizerStorage::multimesh_get_mesh(RID p_multimesh) const {
  370. return _multimesh_get_mesh(p_multimesh);
  371. }
  372. Transform RasterizerStorage::multimesh_instance_get_transform(RID p_multimesh, int p_index) const {
  373. return _multimesh_instance_get_transform(p_multimesh, p_index);
  374. }
  375. Transform2D RasterizerStorage::multimesh_instance_get_transform_2d(RID p_multimesh, int p_index) const {
  376. return _multimesh_instance_get_transform_2d(p_multimesh, p_index);
  377. }
  378. Color RasterizerStorage::multimesh_instance_get_color(RID p_multimesh, int p_index) const {
  379. return _multimesh_instance_get_color(p_multimesh, p_index);
  380. }
  381. Color RasterizerStorage::multimesh_instance_get_custom_data(RID p_multimesh, int p_index) const {
  382. return _multimesh_instance_get_custom_data(p_multimesh, p_index);
  383. }
  384. void RasterizerStorage::multimesh_set_physics_interpolated(RID p_multimesh, bool p_interpolated) {
  385. MMInterpolator *mmi = _multimesh_get_interpolator(p_multimesh);
  386. if (mmi) {
  387. mmi->interpolated = p_interpolated;
  388. }
  389. }
  390. void RasterizerStorage::multimesh_set_physics_interpolation_quality(RID p_multimesh, VS::MultimeshPhysicsInterpolationQuality p_quality) {
  391. ERR_FAIL_COND((p_quality < 0) || (p_quality > 1));
  392. MMInterpolator *mmi = _multimesh_get_interpolator(p_multimesh);
  393. if (mmi) {
  394. mmi->quality = (int)p_quality;
  395. }
  396. }
  397. void RasterizerStorage::multimesh_instance_reset_physics_interpolation(RID p_multimesh, int p_index) {
  398. MMInterpolator *mmi = _multimesh_get_interpolator(p_multimesh);
  399. if (mmi) {
  400. ERR_FAIL_INDEX(p_index, mmi->_num_instances);
  401. PoolVector<float>::Write w = mmi->_data_prev.write();
  402. PoolVector<float>::Read r = mmi->_data_curr.read();
  403. int start = p_index * mmi->_stride;
  404. for (int n = 0; n < mmi->_stride; n++) {
  405. w[start + n] = r[start + n];
  406. }
  407. }
  408. }
  409. void RasterizerStorage::multimesh_instances_reset_physics_interpolation(RID p_multimesh) {
  410. MMInterpolator *mmi = _multimesh_get_interpolator(p_multimesh);
  411. if (mmi && mmi->_data_curr.size()) {
  412. // We don't want to invoke COW here, so copy the data directly.
  413. ERR_FAIL_COND(mmi->_data_prev.size() != mmi->_data_curr.size());
  414. PoolVector<float>::Read read = mmi->_data_curr.read();
  415. PoolVector<float>::Write write = mmi->_data_prev.write();
  416. const float *r = read.ptr();
  417. float *w = write.ptr();
  418. memcpy(w, r, sizeof(float) * mmi->_data_curr.size());
  419. }
  420. }
  421. void RasterizerStorage::_multimesh_add_to_interpolation_lists(RID p_multimesh, MMInterpolator &r_mmi) {
  422. if (!r_mmi.on_interpolate_update_list) {
  423. r_mmi.on_interpolate_update_list = true;
  424. _interpolation_data.multimesh_interpolate_update_list.push_back(p_multimesh);
  425. }
  426. if (!r_mmi.on_transform_update_list) {
  427. r_mmi.on_transform_update_list = true;
  428. _interpolation_data.multimesh_transform_update_list_curr->push_back(p_multimesh);
  429. }
  430. }
  431. void RasterizerStorage::multimesh_set_as_bulk_array_interpolated(RID p_multimesh, const PoolVector<float> &p_array, const PoolVector<float> &p_array_prev) {
  432. MMInterpolator *mmi = _multimesh_get_interpolator(p_multimesh);
  433. if (mmi) {
  434. ERR_FAIL_COND_MSG(p_array.size() != mmi->_data_curr.size(), vformat("Array for current frame should have %d elements, got %d instead.", mmi->_data_curr.size(), p_array.size()));
  435. ERR_FAIL_COND_MSG(p_array_prev.size() != mmi->_data_prev.size(), vformat("Array for previous frame should have %d elements, got %d instead.", mmi->_data_prev.size(), p_array_prev.size()));
  436. // We are assuming that mmi->interpolated is the case,
  437. // (can possibly assert this?)
  438. // even if this flag hasn't been set - just calling this function suggests
  439. // interpolation is desired.
  440. mmi->_data_prev = p_array_prev;
  441. mmi->_data_curr = p_array;
  442. _multimesh_add_to_interpolation_lists(p_multimesh, *mmi);
  443. #if defined(DEBUG_ENABLED) && defined(TOOLS_ENABLED)
  444. if (!Engine::get_singleton()->is_in_physics_frame()) {
  445. PHYSICS_INTERPOLATION_WARNING("Interpolated MultiMesh triggered from outside physics process");
  446. }
  447. #endif
  448. }
  449. }
  450. void RasterizerStorage::multimesh_set_as_bulk_array(RID p_multimesh, const PoolVector<float> &p_array) {
  451. MMInterpolator *mmi = _multimesh_get_interpolator(p_multimesh);
  452. if (mmi) {
  453. if (mmi->interpolated) {
  454. ERR_FAIL_COND_MSG(p_array.size() != mmi->_data_curr.size(), vformat("Array should have %d elements, got %d instead.", mmi->_data_curr.size(), p_array.size()));
  455. mmi->_data_curr = p_array;
  456. _multimesh_add_to_interpolation_lists(p_multimesh, *mmi);
  457. #if defined(DEBUG_ENABLED) && defined(TOOLS_ENABLED)
  458. if (!Engine::get_singleton()->is_in_physics_frame()) {
  459. PHYSICS_INTERPOLATION_WARNING("Interpolated MultiMesh triggered from outside physics process");
  460. }
  461. #endif
  462. return;
  463. }
  464. }
  465. _multimesh_set_as_bulk_array(p_multimesh, p_array);
  466. }
  467. void RasterizerStorage::multimesh_set_visible_instances(RID p_multimesh, int p_visible) {
  468. _multimesh_set_visible_instances(p_multimesh, p_visible);
  469. }
  470. int RasterizerStorage::multimesh_get_visible_instances(RID p_multimesh) const {
  471. return _multimesh_get_visible_instances(p_multimesh);
  472. }
  473. AABB RasterizerStorage::multimesh_get_aabb(RID p_multimesh) const {
  474. return _multimesh_get_aabb(p_multimesh);
  475. }
  476. // The bone bounds are determined by rigging,
  477. // as such they can be calculated as a one off operation,
  478. // rather than each call to get_rect().
  479. void RasterizerCanvas::Item::precalculate_polygon_bone_bounds(const Item::CommandPolygon &p_polygon) const {
  480. p_polygon.skinning_data->dirty = false;
  481. p_polygon.skinning_data->untransformed_bound = Rect2(Vector2(), Vector2(-1, -1)); // negative means unused.
  482. int num_points = p_polygon.points.size();
  483. const Point2 *pp = &p_polygon.points[0];
  484. // Calculate bone AABBs.
  485. int bone_count = RasterizerStorage::base_singleton->skeleton_get_bone_count(skeleton);
  486. // Get some local aliases
  487. LocalVector<Rect2> &active_bounds = p_polygon.skinning_data->active_bounds;
  488. LocalVector<uint16_t> &active_bone_ids = p_polygon.skinning_data->active_bone_ids;
  489. active_bounds.clear();
  490. active_bone_ids.clear();
  491. // Uses dynamic allocation, but shouldn't happen very often.
  492. // If happens more often, use alloca.
  493. LocalVector<int32_t> bone_to_active_bone_mapping;
  494. bone_to_active_bone_mapping.resize(bone_count);
  495. for (int n = 0; n < bone_count; n++) {
  496. bone_to_active_bone_mapping[n] = -1;
  497. }
  498. const Transform2D &item_transform = skinning_data->skeleton_relative_xform;
  499. bool some_were_untransformed = false;
  500. for (int n = 0; n < num_points; n++) {
  501. Point2 p = pp[n];
  502. bool bone_space = false;
  503. float total_weight = 0;
  504. for (int k = 0; k < 4; k++) {
  505. int bone_id = p_polygon.bones[n * 4 + k];
  506. float w = p_polygon.weights[n * 4 + k];
  507. if (w == 0) {
  508. continue;
  509. }
  510. total_weight += w;
  511. // Ensure the point is in "bone space" / rigged space.
  512. if (!bone_space) {
  513. bone_space = true;
  514. p = item_transform.xform(p);
  515. }
  516. // get the active bone, or create a new active bone
  517. DEV_ASSERT(bone_id < bone_count);
  518. int32_t &active_bone = bone_to_active_bone_mapping[bone_id];
  519. if (active_bone != -1) {
  520. active_bounds[active_bone].expand_to(p);
  521. } else {
  522. // Increment the number of active bones stored.
  523. active_bone = active_bounds.size();
  524. active_bounds.resize(active_bone + 1);
  525. active_bone_ids.resize(active_bone + 1);
  526. // First point for the bone
  527. DEV_ASSERT(bone_id <= UINT16_MAX);
  528. active_bone_ids[active_bone] = bone_id;
  529. active_bounds[active_bone] = Rect2(p, Vector2(0.00001, 0.00001));
  530. }
  531. }
  532. // If some points were not rigged,
  533. // we want to add them directly to an "untransformed bound",
  534. // and merge this with the skinned bound later.
  535. // Also do this if a point is not FULLY weighted,
  536. // because the untransformed position is still having an influence.
  537. if (!bone_space || (total_weight < 0.99f)) {
  538. if (some_were_untransformed) {
  539. p_polygon.skinning_data->untransformed_bound.expand_to(pp[n]);
  540. } else {
  541. // First point
  542. some_were_untransformed = true;
  543. p_polygon.skinning_data->untransformed_bound = Rect2(pp[n], Vector2());
  544. }
  545. }
  546. }
  547. }
  548. Rect2 RasterizerCanvas::Item::calculate_polygon_bounds(const Item::CommandPolygon &p_polygon) const {
  549. int num_points = p_polygon.points.size();
  550. // If there is no skeleton, or the bones data is invalid...
  551. // Note : Can we check the second more efficiently? by checking if polygon.skinning_data is set perhaps?
  552. if (skeleton == RID() || !(num_points && p_polygon.bones.size() == num_points * 4 && p_polygon.weights.size() == p_polygon.bones.size())) {
  553. // With no skeleton, all points are untransformed.
  554. Rect2 r;
  555. const Point2 *pp = &p_polygon.points[0];
  556. r.position = pp[0];
  557. for (int n = 1; n < num_points; n++) {
  558. r.expand_to(pp[n]);
  559. }
  560. return r;
  561. }
  562. // Skinned skeleton is present.
  563. ERR_FAIL_COND_V_MSG(!skinning_data, Rect2(), "Skinned Polygon2D must have skeleton_relative_xform set for correct culling.");
  564. // Ensure the polygon skinning data is created...
  565. // (This isn't stored on every polygon to save memory).
  566. if (!p_polygon.skinning_data) {
  567. p_polygon.skinning_data = memnew(Item::CommandPolygon::SkinningData);
  568. }
  569. Item::CommandPolygon::SkinningData &pdata = *p_polygon.skinning_data;
  570. // This should only occur when rigging has changed.
  571. // Usually a one off in games.
  572. if (pdata.dirty) {
  573. precalculate_polygon_bone_bounds(p_polygon);
  574. }
  575. // We only deal with the precalculated ACTIVE bone AABBs using the skeleton.
  576. // (No need to bother with bones that are unused for this poly.)
  577. int num_active_bones = pdata.active_bounds.size();
  578. if (!num_active_bones) {
  579. return pdata.untransformed_bound;
  580. }
  581. // No need to make a dynamic allocation here in 99% of cases.
  582. Rect2 *bptr = nullptr;
  583. LocalVector<Rect2> bone_aabbs;
  584. if (num_active_bones <= 1024) {
  585. bptr = (Rect2 *)alloca(sizeof(Rect2) * num_active_bones);
  586. } else {
  587. bone_aabbs.resize(num_active_bones);
  588. bptr = bone_aabbs.ptr();
  589. }
  590. // Copy across the precalculated bone bounds.
  591. memcpy(bptr, pdata.active_bounds.ptr(), sizeof(Rect2) * num_active_bones);
  592. const Transform2D &item_transform_inv = skinning_data->skeleton_relative_xform_inv;
  593. Rect2 aabb;
  594. bool first_bone = true;
  595. for (int n = 0; n < num_active_bones; n++) {
  596. int bone_id = pdata.active_bone_ids[n];
  597. const Transform2D &mtx = RasterizerStorage::base_singleton->skeleton_bone_get_transform_2d(skeleton, bone_id);
  598. Rect2 baabb = mtx.xform(bptr[n]);
  599. if (first_bone) {
  600. aabb = baabb;
  601. first_bone = false;
  602. } else {
  603. aabb = aabb.merge(baabb);
  604. }
  605. }
  606. // Transform the polygon AABB back into local space from bone space.
  607. aabb = item_transform_inv.xform(aabb);
  608. // If some were untransformed...
  609. if (pdata.untransformed_bound.size.x >= 0) {
  610. return pdata.untransformed_bound.merge(aabb);
  611. }
  612. return aabb;
  613. }