| 1 | /**************************************************************************/ |
| 2 | /* mesh_storage.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 | |
| 31 | #include "mesh_storage.h" |
| 32 | |
| 33 | #include "core/math/transform_interpolator.h" |
| 34 | |
| 35 | #if defined(DEBUG_ENABLED) && defined(TOOLS_ENABLED) |
| 36 | #include "core/config/project_settings.h" |
| 37 | #endif |
| 38 | |
| 39 | RID RendererMeshStorage::multimesh_allocate() { |
| 40 | return _multimesh_allocate(); |
| 41 | } |
| 42 | |
| 43 | void RendererMeshStorage::multimesh_initialize(RID p_rid) { |
| 44 | _multimesh_initialize(p_rid); |
| 45 | } |
| 46 | |
| 47 | void RendererMeshStorage::multimesh_free(RID p_rid) { |
| 48 | _multimesh_free(p_rid); |
| 49 | } |
| 50 | |
| 51 | void RendererMeshStorage::multimesh_allocate_data(RID p_multimesh, int p_instances, RS::MultimeshTransformFormat p_transform_format, bool p_use_colors, bool p_use_custom_data, bool p_use_indirect) { |
| 52 | MultiMeshInterpolator *mmi = _multimesh_get_interpolator(p_multimesh); |
| 53 | if (mmi) { |
| 54 | mmi->_transform_format = p_transform_format; |
| 55 | mmi->_use_colors = p_use_colors; |
| 56 | mmi->_use_custom_data = p_use_custom_data; |
| 57 | mmi->_num_instances = p_instances; |
| 58 | |
| 59 | mmi->_vf_size_xform = p_transform_format == RS::MULTIMESH_TRANSFORM_2D ? 8 : 12; |
| 60 | mmi->_vf_size_color = p_use_colors ? 4 : 0; |
| 61 | mmi->_vf_size_data = p_use_custom_data ? 4 : 0; |
| 62 | |
| 63 | mmi->_stride = mmi->_vf_size_xform + mmi->_vf_size_color + mmi->_vf_size_data; |
| 64 | |
| 65 | int size_in_floats = p_instances * mmi->_stride; |
| 66 | mmi->_data_curr.resize_initialized(size_in_floats); |
| 67 | mmi->_data_prev.resize_initialized(size_in_floats); |
| 68 | mmi->_data_interpolated.resize_initialized(size_in_floats); |
| 69 | } |
| 70 | |
| 71 | _multimesh_allocate_data(p_multimesh, p_instances, p_transform_format, p_use_colors, p_use_custom_data, p_use_indirect); |
| 72 | } |
| 73 | |
| 74 | int RendererMeshStorage::multimesh_get_instance_count(RID p_multimesh) const { |
| 75 | return _multimesh_get_instance_count(p_multimesh); |
| 76 | } |
| 77 | |
| 78 | void RendererMeshStorage::multimesh_set_mesh(RID p_multimesh, RID p_mesh) { |
| 79 | _multimesh_set_mesh(p_multimesh, p_mesh); |
| 80 | } |
| 81 | |
| 82 | void RendererMeshStorage::multimesh_instance_set_transform(RID p_multimesh, int p_index, const Transform3D &p_transform) { |
| 83 | MultiMeshInterpolator *mmi = _multimesh_get_interpolator(p_multimesh); |
| 84 | if (mmi && mmi->interpolated) { |
| 85 | ERR_FAIL_COND(p_index >= mmi->_num_instances); |
| 86 | ERR_FAIL_COND(mmi->_vf_size_xform != 12); |
| 87 | |
| 88 | int start = p_index * mmi->_stride; |
| 89 | float *ptr = mmi->_data_curr.ptrw(); |
| 90 | ptr += start; |
| 91 | |
| 92 | const Transform3D &t = p_transform; |
| 93 | ptr[0] = t.basis.rows[0][0]; |
| 94 | ptr[1] = t.basis.rows[0][1]; |
| 95 | ptr[2] = t.basis.rows[0][2]; |
| 96 | ptr[3] = t.origin.x; |
| 97 | ptr[4] = t.basis.rows[1][0]; |
| 98 | ptr[5] = t.basis.rows[1][1]; |
| 99 | ptr[6] = t.basis.rows[1][2]; |
| 100 | ptr[7] = t.origin.y; |
| 101 | ptr[8] = t.basis.rows[2][0]; |
| 102 | ptr[9] = t.basis.rows[2][1]; |
| 103 | ptr[10] = t.basis.rows[2][2]; |
| 104 | ptr[11] = t.origin.z; |
| 105 | |
| 106 | _multimesh_add_to_interpolation_lists(p_multimesh, *mmi); |
| 107 | |
| 108 | #if defined(DEBUG_ENABLED) && defined(TOOLS_ENABLED) |
| 109 | if (!Engine::get_singleton()->is_in_physics_frame()) { |
| 110 | PHYSICS_INTERPOLATION_WARNING("MultiMesh interpolation is being triggered from outside physics process, this might lead to issues"); |
| 111 | } |
| 112 | #endif |
| 113 | |
| 114 | return; |
| 115 | } |
| 116 | |
| 117 | _multimesh_instance_set_transform(p_multimesh, p_index, p_transform); |
| 118 | } |
| 119 | |
| 120 | void RendererMeshStorage::multimesh_instance_set_transform_2d(RID p_multimesh, int p_index, const Transform2D &p_transform) { |
| 121 | MultiMeshInterpolator *mmi = _multimesh_get_interpolator(p_multimesh); |
| 122 | if (mmi && mmi->interpolated) { |
| 123 | ERR_FAIL_COND(p_index >= mmi->_num_instances); |
| 124 | ERR_FAIL_COND(mmi->_vf_size_xform != 8); |
| 125 | |
| 126 | int start = p_index * mmi->_stride; |
| 127 | float *ptr = mmi->_data_curr.ptrw(); |
| 128 | ptr += start; |
| 129 | |
| 130 | const Transform2D &t = p_transform; |
| 131 | |
| 132 | ptr[0] = t.columns[0][0]; |
| 133 | ptr[1] = t.columns[1][0]; |
| 134 | ptr[2] = 0; |
| 135 | ptr[3] = t.columns[2][0]; |
| 136 | ptr[4] = t.columns[0][1]; |
| 137 | ptr[5] = t.columns[1][1]; |
| 138 | ptr[6] = 0; |
| 139 | ptr[7] = t.columns[2][1]; |
| 140 | |
| 141 | _multimesh_add_to_interpolation_lists(p_multimesh, *mmi); |
| 142 | |
| 143 | #if defined(DEBUG_ENABLED) && defined(TOOLS_ENABLED) |
| 144 | if (!Engine::get_singleton()->is_in_physics_frame()) { |
| 145 | PHYSICS_INTERPOLATION_WARNING("MultiMesh interpolation is being triggered from outside physics process, this might lead to issues"); |
| 146 | } |
| 147 | #endif |
| 148 | |
| 149 | return; |
| 150 | } |
| 151 | |
| 152 | _multimesh_instance_set_transform_2d(p_multimesh, p_index, p_transform); |
| 153 | } |
| 154 | |
| 155 | void RendererMeshStorage::multimesh_instance_set_color(RID p_multimesh, int p_index, const Color &p_color) { |
| 156 | MultiMeshInterpolator *mmi = _multimesh_get_interpolator(p_multimesh); |
| 157 | if (mmi && mmi->interpolated) { |
| 158 | ERR_FAIL_COND(p_index >= mmi->_num_instances); |
| 159 | ERR_FAIL_COND(mmi->_vf_size_color == 0); |
| 160 | |
| 161 | int start = (p_index * mmi->_stride) + mmi->_vf_size_xform; |
| 162 | float *ptr = mmi->_data_curr.ptrw(); |
| 163 | ptr += start; |
| 164 | |
| 165 | if (mmi->_vf_size_color == 4) { |
| 166 | for (int n = 0; n < 4; n++) { |
| 167 | ptr[n] = p_color.components[n]; |
| 168 | } |
| 169 | } else { |
| 170 | #ifdef DEV_ENABLED |
| 171 | // The options are currently 4 or zero, but just in case this changes in future... |
| 172 | ERR_FAIL_COND(mmi->_vf_size_color != 0); |
| 173 | #endif |
| 174 | } |
| 175 | _multimesh_add_to_interpolation_lists(p_multimesh, *mmi); |
| 176 | return; |
| 177 | } |
| 178 | |
| 179 | _multimesh_instance_set_color(p_multimesh, p_index, p_color); |
| 180 | } |
| 181 | |
| 182 | void RendererMeshStorage::multimesh_instance_set_custom_data(RID p_multimesh, int p_index, const Color &p_color) { |
| 183 | MultiMeshInterpolator *mmi = _multimesh_get_interpolator(p_multimesh); |
| 184 | if (mmi && mmi->interpolated) { |
| 185 | ERR_FAIL_COND(p_index >= mmi->_num_instances); |
| 186 | ERR_FAIL_COND(mmi->_vf_size_data == 0); |
| 187 | |
| 188 | int start = (p_index * mmi->_stride) + mmi->_vf_size_xform + mmi->_vf_size_color; |
| 189 | float *ptr = mmi->_data_curr.ptrw(); |
| 190 | ptr += start; |
| 191 | |
| 192 | if (mmi->_vf_size_data == 4) { |
| 193 | for (int n = 0; n < 4; n++) { |
| 194 | ptr[n] = p_color.components[n]; |
| 195 | } |
| 196 | } else { |
| 197 | #ifdef DEV_ENABLED |
| 198 | // The options are currently 4 or zero, but just in case this changes in future... |
| 199 | ERR_FAIL_COND(mmi->_vf_size_data != 0); |
| 200 | #endif |
| 201 | } |
| 202 | _multimesh_add_to_interpolation_lists(p_multimesh, *mmi); |
| 203 | return; |
| 204 | } |
| 205 | |
| 206 | _multimesh_instance_set_custom_data(p_multimesh, p_index, p_color); |
| 207 | } |
| 208 | |
| 209 | void RendererMeshStorage::multimesh_set_custom_aabb(RID p_multimesh, const AABB &p_aabb) { |
| 210 | _multimesh_set_custom_aabb(p_multimesh, p_aabb); |
| 211 | } |
| 212 | |
| 213 | AABB RendererMeshStorage::multimesh_get_custom_aabb(RID p_multimesh) const { |
| 214 | return _multimesh_get_custom_aabb(p_multimesh); |
| 215 | } |
| 216 | |
| 217 | RID RendererMeshStorage::multimesh_get_mesh(RID p_multimesh) const { |
| 218 | return _multimesh_get_mesh(p_multimesh); |
| 219 | } |
| 220 | |
| 221 | Transform3D RendererMeshStorage::multimesh_instance_get_transform(RID p_multimesh, int p_index) const { |
| 222 | return _multimesh_instance_get_transform(p_multimesh, p_index); |
| 223 | } |
| 224 | |
| 225 | Transform2D RendererMeshStorage::multimesh_instance_get_transform_2d(RID p_multimesh, int p_index) const { |
| 226 | return _multimesh_instance_get_transform_2d(p_multimesh, p_index); |
| 227 | } |
| 228 | |
| 229 | Color RendererMeshStorage::multimesh_instance_get_color(RID p_multimesh, int p_index) const { |
| 230 | return _multimesh_instance_get_color(p_multimesh, p_index); |
| 231 | } |
| 232 | |
| 233 | Color RendererMeshStorage::multimesh_instance_get_custom_data(RID p_multimesh, int p_index) const { |
| 234 | return _multimesh_instance_get_custom_data(p_multimesh, p_index); |
| 235 | } |
| 236 | |
| 237 | void RendererMeshStorage::multimesh_set_buffer(RID p_multimesh, const Vector<float> &p_buffer) { |
| 238 | MultiMeshInterpolator *mmi = _multimesh_get_interpolator(p_multimesh); |
| 239 | if (mmi && mmi->interpolated) { |
| 240 | ERR_FAIL_COND_MSG(p_buffer.size() != mmi->_data_curr.size(), vformat("Buffer should have %d elements, got %d instead.", mmi->_data_curr.size(), p_buffer.size())); |
| 241 | |
| 242 | mmi->_data_curr = p_buffer; |
| 243 | _multimesh_add_to_interpolation_lists(p_multimesh, *mmi); |
| 244 | |
| 245 | #if defined(DEBUG_ENABLED) && defined(TOOLS_ENABLED) |
| 246 | if (!Engine::get_singleton()->is_in_physics_frame()) { |
| 247 | PHYSICS_INTERPOLATION_WARNING("MultiMesh interpolation is being triggered from outside physics process, this might lead to issues"); |
| 248 | } |
| 249 | #endif |
| 250 | |
| 251 | return; |
| 252 | } |
| 253 | |
| 254 | _multimesh_set_buffer(p_multimesh, p_buffer); |
| 255 | } |
| 256 | |
| 257 | RID RendererMeshStorage::multimesh_get_command_buffer_rd_rid(RID p_multimesh) const { |
| 258 | return _multimesh_get_command_buffer_rd_rid(p_multimesh); |
| 259 | } |
| 260 | |
| 261 | RID RendererMeshStorage::multimesh_get_buffer_rd_rid(RID p_multimesh) const { |
| 262 | return _multimesh_get_buffer_rd_rid(p_multimesh); |
| 263 | } |
| 264 | |
| 265 | Vector<float> RendererMeshStorage::multimesh_get_buffer(RID p_multimesh) const { |
| 266 | return _multimesh_get_buffer(p_multimesh); |
| 267 | } |
| 268 | |
| 269 | void RendererMeshStorage::multimesh_set_buffer_interpolated(RID p_multimesh, const Vector<float> &p_buffer, const Vector<float> &p_buffer_prev) { |
| 270 | MultiMeshInterpolator *mmi = _multimesh_get_interpolator(p_multimesh); |
| 271 | if (mmi) { |
| 272 | ERR_FAIL_COND_MSG(p_buffer.size() != mmi->_data_curr.size(), vformat("Buffer for current frame should have %d elements, got %d instead.", mmi->_data_curr.size(), p_buffer.size())); |
| 273 | ERR_FAIL_COND_MSG(p_buffer_prev.size() != mmi->_data_prev.size(), vformat("Buffer for previous frame should have %d elements, got %d instead.", mmi->_data_prev.size(), p_buffer_prev.size())); |
| 274 | |
| 275 | // We are assuming that mmi->interpolated is the case. (Can possibly assert this?) |
| 276 | // Even if this flag hasn't been set - just calling this function suggests interpolation is desired. |
| 277 | mmi->_data_prev = p_buffer_prev; |
| 278 | mmi->_data_curr = p_buffer; |
| 279 | _multimesh_add_to_interpolation_lists(p_multimesh, *mmi); |
| 280 | |
| 281 | #if defined(DEBUG_ENABLED) && defined(TOOLS_ENABLED) |
| 282 | if (!Engine::get_singleton()->is_in_physics_frame()) { |
| 283 | PHYSICS_INTERPOLATION_WARNING("MultiMesh interpolation is being triggered from outside physics process, this might lead to issues"); |
| 284 | } |
| 285 | #endif |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | void RendererMeshStorage::multimesh_set_physics_interpolated(RID p_multimesh, bool p_interpolated) { |
| 290 | MultiMeshInterpolator *mmi = _multimesh_get_interpolator(p_multimesh); |
| 291 | if (mmi) { |
| 292 | if (p_interpolated == mmi->interpolated) { |
| 293 | return; |
| 294 | } |
| 295 | |
| 296 | mmi->interpolated = p_interpolated; |
| 297 | |
| 298 | // If we are turning on physics interpolation, as a convenience, |
| 299 | // we want to get the current buffer data from the backend, |
| 300 | // and reset all the instances. |
| 301 | if (p_interpolated) { |
| 302 | mmi->_data_curr = _multimesh_get_buffer(p_multimesh); |
| 303 | mmi->_data_prev = mmi->_data_curr; |
| 304 | mmi->_data_interpolated = mmi->_data_curr; |
| 305 | } |
| 306 | } |
| 307 | } |
| 308 | |
| 309 | void RendererMeshStorage::multimesh_set_physics_interpolation_quality(RID p_multimesh, RS::MultimeshPhysicsInterpolationQuality p_quality) { |
| 310 | ERR_FAIL_COND((p_quality < 0) || (p_quality > 1)); |
| 311 | MultiMeshInterpolator *mmi = _multimesh_get_interpolator(p_multimesh); |
| 312 | if (mmi) { |
| 313 | mmi->quality = (int)p_quality; |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | void RendererMeshStorage::multimesh_instance_reset_physics_interpolation(RID p_multimesh, int p_index) { |
| 318 | MultiMeshInterpolator *mmi = _multimesh_get_interpolator(p_multimesh); |
| 319 | if (mmi) { |
| 320 | ERR_FAIL_INDEX(p_index, mmi->_num_instances); |
| 321 | |
| 322 | float *w = mmi->_data_prev.ptrw(); |
| 323 | const float *r = mmi->_data_curr.ptr(); |
| 324 | int start = p_index * mmi->_stride; |
| 325 | |
| 326 | for (int n = 0; n < mmi->_stride; n++) { |
| 327 | w[start + n] = r[start + n]; |
| 328 | } |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | void RendererMeshStorage::multimesh_instances_reset_physics_interpolation(RID p_multimesh) { |
| 333 | MultiMeshInterpolator *mmi = _multimesh_get_interpolator(p_multimesh); |
| 334 | if (mmi && mmi->_data_curr.size()) { |
| 335 | // We don't want to invoke COW here, so copy the data directly. |
| 336 | ERR_FAIL_COND(mmi->_data_prev.size() != mmi->_data_curr.size()); |
| 337 | float *w = mmi->_data_prev.ptrw(); |
| 338 | const float *r = mmi->_data_curr.ptr(); |
| 339 | memcpy(w, r, sizeof(float) * mmi->_data_curr.size()); |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | void RendererMeshStorage::multimesh_set_visible_instances(RID p_multimesh, int p_visible) { |
| 344 | return _multimesh_set_visible_instances(p_multimesh, p_visible); |
| 345 | } |
| 346 | |
| 347 | int RendererMeshStorage::multimesh_get_visible_instances(RID p_multimesh) const { |
| 348 | return _multimesh_get_visible_instances(p_multimesh); |
| 349 | } |
| 350 | |
| 351 | AABB RendererMeshStorage::multimesh_get_aabb(RID p_multimesh) { |
| 352 | return _multimesh_get_aabb(p_multimesh); |
| 353 | } |
| 354 | |
| 355 | void RendererMeshStorage::_multimesh_add_to_interpolation_lists(RID p_multimesh, MultiMeshInterpolator &r_mmi) { |
| 356 | if (!r_mmi.on_interpolate_update_list) { |
| 357 | r_mmi.on_interpolate_update_list = true; |
| 358 | _interpolation_data.multimesh_interpolate_update_list.push_back(p_multimesh); |
| 359 | } |
| 360 | |
| 361 | if (!r_mmi.on_transform_update_list) { |
| 362 | r_mmi.on_transform_update_list = true; |
| 363 | _interpolation_data.multimesh_transform_update_list_curr->push_back(p_multimesh); |
| 364 | } |
| 365 | } |
| 366 | |
| 367 | void RendererMeshStorage::InterpolationData::notify_free_multimesh(RID p_rid) { |
| 368 | // If the instance was on any of the lists, remove. |
| 369 | multimesh_interpolate_update_list.erase_multiple_unordered(p_rid); |
| 370 | multimesh_transform_update_lists[0].erase_multiple_unordered(p_rid); |
| 371 | multimesh_transform_update_lists[1].erase_multiple_unordered(p_rid); |
| 372 | } |
| 373 | |
| 374 | void RendererMeshStorage::update_interpolation_tick(bool p_process) { |
| 375 | // Detect any that were on the previous transform list that are no longer active, |
| 376 | // we should remove them from the interpolate list. |
| 377 | |
| 378 | for (unsigned int n = 0; n < _interpolation_data.multimesh_transform_update_list_prev->size(); n++) { |
| 379 | const RID &rid = (*_interpolation_data.multimesh_transform_update_list_prev)[n]; |
| 380 | |
| 381 | bool active = true; |
| 382 | |
| 383 | // No longer active? (Either the instance deleted or no longer being transformed.) |
| 384 | |
| 385 | MultiMeshInterpolator *mmi = _multimesh_get_interpolator(rid); |
| 386 | if (mmi && !mmi->on_transform_update_list) { |
| 387 | active = false; |
| 388 | mmi->on_interpolate_update_list = false; |
| 389 | |
| 390 | // Make sure the most recent transform is set... |
| 391 | mmi->_data_interpolated = mmi->_data_curr; // TODO: Copy data rather than use Packed = function? |
| 392 | |
| 393 | // ... and that both prev and current are the same, just in case of any interpolations. |
| 394 | mmi->_data_prev = mmi->_data_curr; |
| 395 | |
| 396 | // Update the actual stable buffer to the backend. |
| 397 | _multimesh_set_buffer(rid, mmi->_data_interpolated); |
| 398 | } |
| 399 | |
| 400 | if (!mmi) { |
| 401 | active = false; |
| 402 | } |
| 403 | |
| 404 | if (!active) { |
| 405 | _interpolation_data.multimesh_interpolate_update_list.erase(rid); |
| 406 | } |
| 407 | } |
| 408 | |
| 409 | if (p_process) { |
| 410 | for (unsigned int i = 0; i < _interpolation_data.multimesh_transform_update_list_curr->size(); i++) { |
| 411 | const RID &rid = (*_interpolation_data.multimesh_transform_update_list_curr)[i]; |
| 412 | |
| 413 | MultiMeshInterpolator *mmi = _multimesh_get_interpolator(rid); |
| 414 | if (mmi) { |
| 415 | // Reset for next tick. |
| 416 | mmi->on_transform_update_list = false; |
| 417 | mmi->_data_prev = mmi->_data_curr; |
| 418 | } |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | // If any have left the transform list, remove from the interpolate list. |
| 423 | |
| 424 | // We maintain a mirror list for the transform updates, so we can detect when an instance |
| 425 | // is no longer being transformed, and remove it from the interpolate list. |
| 426 | SWAP(_interpolation_data.multimesh_transform_update_list_curr, _interpolation_data.multimesh_transform_update_list_prev); |
| 427 | |
| 428 | // Prepare for the next iteration. |
| 429 | _interpolation_data.multimesh_transform_update_list_curr->clear(); |
| 430 | } |
| 431 | |
| 432 | void RendererMeshStorage::update_interpolation_frame(bool p_process) { |
| 433 | if (p_process) { |
| 434 | // Only need 32 bits for interpolation, don't use real_t. |
| 435 | float f = Engine::get_singleton()->get_physics_interpolation_fraction(); |
| 436 | |
| 437 | for (unsigned int c = 0; c < _interpolation_data.multimesh_interpolate_update_list.size(); c++) { |
| 438 | const RID &rid = _interpolation_data.multimesh_interpolate_update_list[c]; |
| 439 | |
| 440 | // We could use the TransformInterpolator here to slerp transforms, but that might be too expensive, |
| 441 | // so just using a Basis lerp for now. |
| 442 | MultiMeshInterpolator *mmi = _multimesh_get_interpolator(rid); |
| 443 | if (mmi) { |
| 444 | // Make sure arrays are the correct size. |
| 445 | DEV_ASSERT(mmi->_data_prev.size() == mmi->_data_curr.size()); |
| 446 | |
| 447 | if (mmi->_data_interpolated.size() < mmi->_data_curr.size()) { |
| 448 | mmi->_data_interpolated.resize(mmi->_data_curr.size()); |
| 449 | } |
| 450 | DEV_ASSERT(mmi->_data_interpolated.size() >= mmi->_data_curr.size()); |
| 451 | |
| 452 | DEV_ASSERT((mmi->_data_curr.size() % mmi->_stride) == 0); |
| 453 | int num = mmi->_data_curr.size() / mmi->_stride; |
| 454 | |
| 455 | const float *pf_prev = mmi->_data_prev.ptr(); |
| 456 | const float *pf_curr = mmi->_data_curr.ptr(); |
| 457 | float *pf_int = mmi->_data_interpolated.ptrw(); |
| 458 | |
| 459 | bool use_lerp = mmi->quality == 0; |
| 460 | |
| 461 | // Temporary transform (needed for swizzling). |
| 462 | Transform3D tp, tc, tr; // (transform prev, curr and result) |
| 463 | |
| 464 | // Test for cache friendliness versus doing branchless. |
| 465 | for (int n = 0; n < num; n++) { |
| 466 | // Transform. |
| 467 | if (use_lerp) { |
| 468 | for (int i = 0; i < mmi->_vf_size_xform; i++) { |
| 469 | pf_int[i] = Math::lerp(pf_prev[i], pf_curr[i], f); |
| 470 | } |
| 471 | } else { |
| 472 | // Silly swizzling, this will slow things down. |
| 473 | // No idea why it is using this format... |
| 474 | // ... maybe due to the shader. |
| 475 | tp.basis.rows[0][0] = pf_prev[0]; |
| 476 | tp.basis.rows[0][1] = pf_prev[1]; |
| 477 | tp.basis.rows[0][2] = pf_prev[2]; |
| 478 | tp.basis.rows[1][0] = pf_prev[4]; |
| 479 | tp.basis.rows[1][1] = pf_prev[5]; |
| 480 | tp.basis.rows[1][2] = pf_prev[6]; |
| 481 | tp.basis.rows[2][0] = pf_prev[8]; |
| 482 | tp.basis.rows[2][1] = pf_prev[9]; |
| 483 | tp.basis.rows[2][2] = pf_prev[10]; |
| 484 | tp.origin.x = pf_prev[3]; |
| 485 | tp.origin.y = pf_prev[7]; |
| 486 | tp.origin.z = pf_prev[11]; |
| 487 | |
| 488 | tc.basis.rows[0][0] = pf_curr[0]; |
| 489 | tc.basis.rows[0][1] = pf_curr[1]; |
| 490 | tc.basis.rows[0][2] = pf_curr[2]; |
| 491 | tc.basis.rows[1][0] = pf_curr[4]; |
| 492 | tc.basis.rows[1][1] = pf_curr[5]; |
| 493 | tc.basis.rows[1][2] = pf_curr[6]; |
| 494 | tc.basis.rows[2][0] = pf_curr[8]; |
| 495 | tc.basis.rows[2][1] = pf_curr[9]; |
| 496 | tc.basis.rows[2][2] = pf_curr[10]; |
| 497 | tc.origin.x = pf_curr[3]; |
| 498 | tc.origin.y = pf_curr[7]; |
| 499 | tc.origin.z = pf_curr[11]; |
| 500 | |
| 501 | TransformInterpolator::interpolate_transform_3d(tp, tc, tr, f); |
| 502 | |
| 503 | pf_int[0] = tr.basis.rows[0][0]; |
| 504 | pf_int[1] = tr.basis.rows[0][1]; |
| 505 | pf_int[2] = tr.basis.rows[0][2]; |
| 506 | pf_int[4] = tr.basis.rows[1][0]; |
| 507 | pf_int[5] = tr.basis.rows[1][1]; |
| 508 | pf_int[6] = tr.basis.rows[1][2]; |
| 509 | pf_int[8] = tr.basis.rows[2][0]; |
| 510 | pf_int[9] = tr.basis.rows[2][1]; |
| 511 | pf_int[10] = tr.basis.rows[2][2]; |
| 512 | pf_int[3] = tr.origin.x; |
| 513 | pf_int[7] = tr.origin.y; |
| 514 | pf_int[11] = tr.origin.z; |
| 515 | } |
| 516 | |
| 517 | pf_prev += mmi->_vf_size_xform; |
| 518 | pf_curr += mmi->_vf_size_xform; |
| 519 | pf_int += mmi->_vf_size_xform; |
| 520 | |
| 521 | // Color. |
| 522 | if (mmi->_vf_size_color == 4) { |
| 523 | for (int i = 0; i < 4; i++) { |
| 524 | pf_int[i] = Math::lerp(pf_prev[i], pf_curr[i], f); |
| 525 | } |
| 526 | |
| 527 | pf_prev += 4; |
| 528 | pf_curr += 4; |
| 529 | pf_int += 4; |
| 530 | } |
| 531 | |
| 532 | // Custom data. |
| 533 | if (mmi->_vf_size_data == 4) { |
| 534 | for (int i = 0; i < 4; i++) { |
| 535 | pf_int[i] = Math::lerp(pf_prev[i], pf_curr[i], f); |
| 536 | } |
| 537 | |
| 538 | pf_prev += 4; |
| 539 | pf_curr += 4; |
| 540 | pf_int += 4; |
| 541 | } |
| 542 | } |
| 543 | |
| 544 | _multimesh_set_buffer(rid, mmi->_data_interpolated); |
| 545 | |
| 546 | // TODO: Make sure AABBs are constantly up to date through the interpolation? |
| 547 | // NYI. |
| 548 | } |
| 549 | } |
| 550 | } |
| 551 | } |