rasterizer.cpp 25 KB

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