| 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 | using namespace RendererRD; |
| 34 | |
| 35 | MeshStorage *MeshStorage::singleton = nullptr; |
| 36 | |
| 37 | MeshStorage *MeshStorage::get_singleton() { |
| 38 | return singleton; |
| 39 | } |
| 40 | |
| 41 | MeshStorage::MeshStorage() { |
| 42 | singleton = this; |
| 43 | |
| 44 | default_rd_storage_buffer = RD::get_singleton()->storage_buffer_create(sizeof(uint32_t) * 4); |
| 45 | |
| 46 | //default rd buffers |
| 47 | { |
| 48 | Vector<uint8_t> buffer; |
| 49 | { |
| 50 | buffer.resize(sizeof(float) * 3); |
| 51 | { |
| 52 | uint8_t *w = buffer.ptrw(); |
| 53 | float *fptr = reinterpret_cast<float *>(w); |
| 54 | fptr[0] = 0.0; |
| 55 | fptr[1] = 0.0; |
| 56 | fptr[2] = 0.0; |
| 57 | } |
| 58 | mesh_default_rd_buffers[DEFAULT_RD_BUFFER_VERTEX] = RD::get_singleton()->vertex_buffer_create(buffer.size(), buffer); |
| 59 | } |
| 60 | |
| 61 | { //normal |
| 62 | buffer.resize(sizeof(float) * 3); |
| 63 | { |
| 64 | uint8_t *w = buffer.ptrw(); |
| 65 | float *fptr = reinterpret_cast<float *>(w); |
| 66 | fptr[0] = 1.0; |
| 67 | fptr[1] = 0.0; |
| 68 | fptr[2] = 0.0; |
| 69 | } |
| 70 | mesh_default_rd_buffers[DEFAULT_RD_BUFFER_NORMAL] = RD::get_singleton()->vertex_buffer_create(buffer.size(), buffer); |
| 71 | } |
| 72 | |
| 73 | { //tangent |
| 74 | buffer.resize(sizeof(float) * 4); |
| 75 | { |
| 76 | uint8_t *w = buffer.ptrw(); |
| 77 | float *fptr = reinterpret_cast<float *>(w); |
| 78 | fptr[0] = 1.0; |
| 79 | fptr[1] = 0.0; |
| 80 | fptr[2] = 0.0; |
| 81 | fptr[3] = 0.0; |
| 82 | } |
| 83 | mesh_default_rd_buffers[DEFAULT_RD_BUFFER_TANGENT] = RD::get_singleton()->vertex_buffer_create(buffer.size(), buffer); |
| 84 | } |
| 85 | |
| 86 | { //color |
| 87 | buffer.resize(sizeof(float) * 4); |
| 88 | { |
| 89 | uint8_t *w = buffer.ptrw(); |
| 90 | float *fptr = reinterpret_cast<float *>(w); |
| 91 | fptr[0] = 1.0; |
| 92 | fptr[1] = 1.0; |
| 93 | fptr[2] = 1.0; |
| 94 | fptr[3] = 1.0; |
| 95 | } |
| 96 | mesh_default_rd_buffers[DEFAULT_RD_BUFFER_COLOR] = RD::get_singleton()->vertex_buffer_create(buffer.size(), buffer); |
| 97 | } |
| 98 | |
| 99 | { //tex uv 1 |
| 100 | buffer.resize(sizeof(float) * 2); |
| 101 | { |
| 102 | uint8_t *w = buffer.ptrw(); |
| 103 | float *fptr = reinterpret_cast<float *>(w); |
| 104 | fptr[0] = 0.0; |
| 105 | fptr[1] = 0.0; |
| 106 | } |
| 107 | mesh_default_rd_buffers[DEFAULT_RD_BUFFER_TEX_UV] = RD::get_singleton()->vertex_buffer_create(buffer.size(), buffer); |
| 108 | } |
| 109 | { //tex uv 2 |
| 110 | buffer.resize(sizeof(float) * 2); |
| 111 | { |
| 112 | uint8_t *w = buffer.ptrw(); |
| 113 | float *fptr = reinterpret_cast<float *>(w); |
| 114 | fptr[0] = 0.0; |
| 115 | fptr[1] = 0.0; |
| 116 | } |
| 117 | mesh_default_rd_buffers[DEFAULT_RD_BUFFER_TEX_UV2] = RD::get_singleton()->vertex_buffer_create(buffer.size(), buffer); |
| 118 | } |
| 119 | |
| 120 | for (int i = 0; i < RS::ARRAY_CUSTOM_COUNT; i++) { |
| 121 | buffer.resize(sizeof(float) * 4); |
| 122 | { |
| 123 | uint8_t *w = buffer.ptrw(); |
| 124 | float *fptr = reinterpret_cast<float *>(w); |
| 125 | fptr[0] = 0.0; |
| 126 | fptr[1] = 0.0; |
| 127 | fptr[2] = 0.0; |
| 128 | fptr[3] = 0.0; |
| 129 | } |
| 130 | mesh_default_rd_buffers[DEFAULT_RD_BUFFER_CUSTOM0 + i] = RD::get_singleton()->vertex_buffer_create(buffer.size(), buffer); |
| 131 | } |
| 132 | |
| 133 | { //bones |
| 134 | buffer.resize(sizeof(uint32_t) * 4); |
| 135 | { |
| 136 | uint8_t *w = buffer.ptrw(); |
| 137 | uint32_t *fptr = reinterpret_cast<uint32_t *>(w); |
| 138 | fptr[0] = 0; |
| 139 | fptr[1] = 0; |
| 140 | fptr[2] = 0; |
| 141 | fptr[3] = 0; |
| 142 | } |
| 143 | mesh_default_rd_buffers[DEFAULT_RD_BUFFER_BONES] = RD::get_singleton()->vertex_buffer_create(buffer.size(), buffer); |
| 144 | } |
| 145 | |
| 146 | { //weights |
| 147 | buffer.resize(sizeof(float) * 4); |
| 148 | { |
| 149 | uint8_t *w = buffer.ptrw(); |
| 150 | float *fptr = reinterpret_cast<float *>(w); |
| 151 | fptr[0] = 0.0; |
| 152 | fptr[1] = 0.0; |
| 153 | fptr[2] = 0.0; |
| 154 | fptr[3] = 0.0; |
| 155 | } |
| 156 | mesh_default_rd_buffers[DEFAULT_RD_BUFFER_WEIGHTS] = RD::get_singleton()->vertex_buffer_create(buffer.size(), buffer); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | { |
| 161 | Vector<String> skeleton_modes; |
| 162 | skeleton_modes.push_back("\n#define MODE_2D\n"); |
| 163 | skeleton_modes.push_back(""); |
| 164 | |
| 165 | skeleton_shader.shader.initialize(skeleton_modes); |
| 166 | skeleton_shader.version = skeleton_shader.shader.version_create(); |
| 167 | for (int i = 0; i < SkeletonShader::SHADER_MODE_MAX; i++) { |
| 168 | skeleton_shader.version_shader[i] = skeleton_shader.shader.version_get_shader(skeleton_shader.version, i); |
| 169 | skeleton_shader.pipeline[i] = RD::get_singleton()->compute_pipeline_create(skeleton_shader.version_shader[i]); |
| 170 | } |
| 171 | |
| 172 | { |
| 173 | Vector<RD::Uniform> uniforms; |
| 174 | { |
| 175 | RD::Uniform u; |
| 176 | u.binding = 0; |
| 177 | u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER; |
| 178 | u.append_id(default_rd_storage_buffer); |
| 179 | uniforms.push_back(u); |
| 180 | } |
| 181 | skeleton_shader.default_skeleton_uniform_set = RD::get_singleton()->uniform_set_create(uniforms, skeleton_shader.version_shader[0], SkeletonShader::UNIFORM_SET_SKELETON); |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | MeshStorage::~MeshStorage() { |
| 187 | //def buffers |
| 188 | for (int i = 0; i < DEFAULT_RD_BUFFER_MAX; i++) { |
| 189 | RD::get_singleton()->free_rid(mesh_default_rd_buffers[i]); |
| 190 | } |
| 191 | |
| 192 | skeleton_shader.shader.version_free(skeleton_shader.version); |
| 193 | |
| 194 | RD::get_singleton()->free_rid(default_rd_storage_buffer); |
| 195 | |
| 196 | singleton = nullptr; |
| 197 | } |
| 198 | |
| 199 | bool MeshStorage::free(RID p_rid) { |
| 200 | if (owns_mesh(p_rid)) { |
| 201 | mesh_free(p_rid); |
| 202 | return true; |
| 203 | } else if (owns_mesh_instance(p_rid)) { |
| 204 | mesh_instance_free(p_rid); |
| 205 | return true; |
| 206 | } else if (owns_multimesh(p_rid)) { |
| 207 | multimesh_free(p_rid); |
| 208 | return true; |
| 209 | } else if (owns_skeleton(p_rid)) { |
| 210 | skeleton_free(p_rid); |
| 211 | return true; |
| 212 | } |
| 213 | |
| 214 | return false; |
| 215 | } |
| 216 | |
| 217 | /* MESH API */ |
| 218 | |
| 219 | RID MeshStorage::mesh_allocate() { |
| 220 | return mesh_owner.allocate_rid(); |
| 221 | } |
| 222 | |
| 223 | void MeshStorage::mesh_initialize(RID p_rid) { |
| 224 | mesh_owner.initialize_rid(p_rid, Mesh()); |
| 225 | } |
| 226 | |
| 227 | void MeshStorage::mesh_free(RID p_rid) { |
| 228 | mesh_clear(p_rid); |
| 229 | mesh_set_shadow_mesh(p_rid, RID()); |
| 230 | Mesh *mesh = mesh_owner.get_or_null(p_rid); |
| 231 | ERR_FAIL_NULL(mesh); |
| 232 | |
| 233 | mesh->dependency.deleted_notify(p_rid); |
| 234 | if (mesh->instances.size()) { |
| 235 | ERR_PRINT("deleting mesh with active instances"); |
| 236 | } |
| 237 | if (mesh->shadow_owners.size()) { |
| 238 | for (Mesh *E : mesh->shadow_owners) { |
| 239 | Mesh *shadow_owner = E; |
| 240 | shadow_owner->shadow_mesh = RID(); |
| 241 | shadow_owner->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_MESH); |
| 242 | } |
| 243 | } |
| 244 | mesh_owner.free(p_rid); |
| 245 | } |
| 246 | |
| 247 | void MeshStorage::mesh_set_blend_shape_count(RID p_mesh, int p_blend_shape_count) { |
| 248 | ERR_FAIL_COND(p_blend_shape_count < 0); |
| 249 | |
| 250 | Mesh *mesh = mesh_owner.get_or_null(p_mesh); |
| 251 | ERR_FAIL_NULL(mesh); |
| 252 | |
| 253 | ERR_FAIL_COND(mesh->surface_count > 0); //surfaces already exist |
| 254 | |
| 255 | mesh->blend_shape_count = p_blend_shape_count; |
| 256 | } |
| 257 | |
| 258 | /// Returns stride |
| 259 | void MeshStorage::mesh_add_surface(RID p_mesh, const RS::SurfaceData &p_surface) { |
| 260 | Mesh *mesh = mesh_owner.get_or_null(p_mesh); |
| 261 | ERR_FAIL_NULL(mesh); |
| 262 | |
| 263 | ERR_FAIL_COND(mesh->surface_count == RS::MAX_MESH_SURFACES); |
| 264 | |
| 265 | #ifdef DEBUG_ENABLED |
| 266 | //do a validation, to catch errors first |
| 267 | { |
| 268 | uint32_t stride = 0; |
| 269 | uint32_t attrib_stride = 0; |
| 270 | uint32_t skin_stride = 0; |
| 271 | |
| 272 | for (int i = 0; i < RS::ARRAY_WEIGHTS; i++) { |
| 273 | if ((p_surface.format & (1ULL << i))) { |
| 274 | switch (i) { |
| 275 | case RS::ARRAY_VERTEX: { |
| 276 | if ((p_surface.format & RS::ARRAY_FLAG_USE_2D_VERTICES) || (p_surface.format & RS::ARRAY_FLAG_COMPRESS_ATTRIBUTES)) { |
| 277 | stride += sizeof(float) * 2; |
| 278 | } else { |
| 279 | stride += sizeof(float) * 3; |
| 280 | } |
| 281 | |
| 282 | } break; |
| 283 | case RS::ARRAY_NORMAL: { |
| 284 | stride += sizeof(uint16_t) * 2; |
| 285 | |
| 286 | } break; |
| 287 | case RS::ARRAY_TANGENT: { |
| 288 | if (!(p_surface.format & RS::ARRAY_FLAG_COMPRESS_ATTRIBUTES)) { |
| 289 | stride += sizeof(uint16_t) * 2; |
| 290 | } |
| 291 | } break; |
| 292 | case RS::ARRAY_COLOR: { |
| 293 | attrib_stride += sizeof(uint32_t); |
| 294 | } break; |
| 295 | case RS::ARRAY_TEX_UV: { |
| 296 | if (p_surface.format & RS::ARRAY_FLAG_COMPRESS_ATTRIBUTES) { |
| 297 | attrib_stride += sizeof(uint16_t) * 2; |
| 298 | } else { |
| 299 | attrib_stride += sizeof(float) * 2; |
| 300 | } |
| 301 | |
| 302 | } break; |
| 303 | case RS::ARRAY_TEX_UV2: { |
| 304 | if (p_surface.format & RS::ARRAY_FLAG_COMPRESS_ATTRIBUTES) { |
| 305 | attrib_stride += sizeof(uint16_t) * 2; |
| 306 | } else { |
| 307 | attrib_stride += sizeof(float) * 2; |
| 308 | } |
| 309 | |
| 310 | } break; |
| 311 | case RS::ARRAY_CUSTOM0: |
| 312 | case RS::ARRAY_CUSTOM1: |
| 313 | case RS::ARRAY_CUSTOM2: |
| 314 | case RS::ARRAY_CUSTOM3: { |
| 315 | int idx = i - RS::ARRAY_CUSTOM0; |
| 316 | const uint32_t fmt_shift[RS::ARRAY_CUSTOM_COUNT] = { RS::ARRAY_FORMAT_CUSTOM0_SHIFT, RS::ARRAY_FORMAT_CUSTOM1_SHIFT, RS::ARRAY_FORMAT_CUSTOM2_SHIFT, RS::ARRAY_FORMAT_CUSTOM3_SHIFT }; |
| 317 | uint32_t fmt = (p_surface.format >> fmt_shift[idx]) & RS::ARRAY_FORMAT_CUSTOM_MASK; |
| 318 | const uint32_t fmtsize[RS::ARRAY_CUSTOM_MAX] = { 4, 4, 4, 8, 4, 8, 12, 16 }; |
| 319 | attrib_stride += fmtsize[fmt]; |
| 320 | |
| 321 | } break; |
| 322 | case RS::ARRAY_WEIGHTS: |
| 323 | case RS::ARRAY_BONES: { |
| 324 | //uses a separate array |
| 325 | bool use_8 = p_surface.format & RS::ARRAY_FLAG_USE_8_BONE_WEIGHTS; |
| 326 | skin_stride += sizeof(int16_t) * (use_8 ? 16 : 8); |
| 327 | } break; |
| 328 | } |
| 329 | } |
| 330 | } |
| 331 | |
| 332 | int expected_size = stride * p_surface.vertex_count; |
| 333 | ERR_FAIL_COND_MSG(expected_size != p_surface.vertex_data.size(), "Size of vertex data provided (" + itos(p_surface.vertex_data.size()) + ") does not match expected (" + itos(expected_size) + ")"); |
| 334 | |
| 335 | int bs_expected_size = expected_size * mesh->blend_shape_count; |
| 336 | |
| 337 | ERR_FAIL_COND_MSG(bs_expected_size != p_surface.blend_shape_data.size(), "Size of blend shape data provided (" + itos(p_surface.blend_shape_data.size()) + ") does not match expected (" + itos(bs_expected_size) + ")"); |
| 338 | |
| 339 | int expected_attrib_size = attrib_stride * p_surface.vertex_count; |
| 340 | ERR_FAIL_COND_MSG(expected_attrib_size != p_surface.attribute_data.size(), "Size of attribute data provided (" + itos(p_surface.attribute_data.size()) + ") does not match expected (" + itos(expected_attrib_size) + ")"); |
| 341 | |
| 342 | if ((p_surface.format & RS::ARRAY_FORMAT_WEIGHTS) && (p_surface.format & RS::ARRAY_FORMAT_BONES)) { |
| 343 | expected_size = skin_stride * p_surface.vertex_count; |
| 344 | ERR_FAIL_COND_MSG(expected_size != p_surface.skin_data.size(), "Size of skin data provided (" + itos(p_surface.skin_data.size()) + ") does not match expected (" + itos(expected_size) + ")"); |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | #endif |
| 349 | |
| 350 | uint64_t surface_version = p_surface.format & (uint64_t(RS::ARRAY_FLAG_FORMAT_VERSION_MASK) << RS::ARRAY_FLAG_FORMAT_VERSION_SHIFT); |
| 351 | RS::SurfaceData new_surface = p_surface; |
| 352 | #ifdef DISABLE_DEPRECATED |
| 353 | |
| 354 | ERR_FAIL_COND_MSG(surface_version != RS::ARRAY_FLAG_FORMAT_CURRENT_VERSION, "Surface version provided (" + itos(int(surface_version >> RS::ARRAY_FLAG_FORMAT_VERSION_SHIFT)) + ") does not match current version (" + itos(RS::ARRAY_FLAG_FORMAT_CURRENT_VERSION >> RS::ARRAY_FLAG_FORMAT_VERSION_SHIFT) + ")"); |
| 355 | |
| 356 | #else |
| 357 | |
| 358 | if (surface_version != uint64_t(RS::ARRAY_FLAG_FORMAT_CURRENT_VERSION)) { |
| 359 | RS::get_singleton()->fix_surface_compatibility(new_surface); |
| 360 | surface_version = new_surface.format & (RS::ARRAY_FLAG_FORMAT_VERSION_MASK << RS::ARRAY_FLAG_FORMAT_VERSION_SHIFT); |
| 361 | ERR_FAIL_COND_MSG(surface_version != RS::ARRAY_FLAG_FORMAT_CURRENT_VERSION, |
| 362 | vformat("Surface version provided (%d) does not match current version (%d).", |
| 363 | (surface_version >> RS::ARRAY_FLAG_FORMAT_VERSION_SHIFT) & RS::ARRAY_FLAG_FORMAT_VERSION_MASK, |
| 364 | (RS::ARRAY_FLAG_FORMAT_CURRENT_VERSION >> RS::ARRAY_FLAG_FORMAT_VERSION_SHIFT) & RS::ARRAY_FLAG_FORMAT_VERSION_MASK)); |
| 365 | } |
| 366 | #endif |
| 367 | |
| 368 | Mesh::Surface *s = memnew(Mesh::Surface); |
| 369 | |
| 370 | s->format = new_surface.format; |
| 371 | s->primitive = new_surface.primitive; |
| 372 | |
| 373 | const bool use_as_storage = (new_surface.skin_data.size() || mesh->blend_shape_count > 0); |
| 374 | const BitField<RD::BufferCreationBits> as_storage_flag = use_as_storage ? RD::BUFFER_CREATION_AS_STORAGE_BIT : 0; |
| 375 | |
| 376 | if (new_surface.vertex_data.size()) { |
| 377 | // If we have an uncompressed surface that contains normals, but not tangents, we need to differentiate the array |
| 378 | // from a compressed array in the shader. To do so, we allow the normal to read 4 components out of the buffer |
| 379 | // But only give it 2 components per normal. So essentially, each vertex reads the next normal in normal.zw. |
| 380 | // This allows us to avoid adding a shader permutation, and avoid passing dummy tangents. Since the stride is kept small |
| 381 | // this should still be a net win for bandwidth. |
| 382 | // If we do this, then the last normal will read past the end of the array. So we need to pad the array with dummy data. |
| 383 | if (!(new_surface.format & RS::ARRAY_FLAG_COMPRESS_ATTRIBUTES) && (new_surface.format & RS::ARRAY_FORMAT_NORMAL) && !(new_surface.format & RS::ARRAY_FORMAT_TANGENT)) { |
| 384 | // Unfortunately, we need to copy the buffer, which is fine as doing a resize triggers a CoW anyway. |
| 385 | Vector<uint8_t> new_vertex_data; |
| 386 | new_vertex_data.resize_initialized(new_surface.vertex_data.size() + sizeof(uint16_t) * 2); |
| 387 | memcpy(new_vertex_data.ptrw(), new_surface.vertex_data.ptr(), new_surface.vertex_data.size()); |
| 388 | s->vertex_buffer = RD::get_singleton()->vertex_buffer_create(new_vertex_data.size(), new_vertex_data, as_storage_flag); |
| 389 | s->vertex_buffer_size = new_vertex_data.size(); |
| 390 | } else { |
| 391 | s->vertex_buffer = RD::get_singleton()->vertex_buffer_create(new_surface.vertex_data.size(), new_surface.vertex_data, as_storage_flag); |
| 392 | s->vertex_buffer_size = new_surface.vertex_data.size(); |
| 393 | } |
| 394 | } |
| 395 | |
| 396 | if (new_surface.attribute_data.size()) { |
| 397 | s->attribute_buffer = RD::get_singleton()->vertex_buffer_create(new_surface.attribute_data.size(), new_surface.attribute_data); |
| 398 | s->attribute_buffer_size = new_surface.attribute_data.size(); |
| 399 | } |
| 400 | if (new_surface.skin_data.size()) { |
| 401 | s->skin_buffer = RD::get_singleton()->vertex_buffer_create(new_surface.skin_data.size(), new_surface.skin_data, as_storage_flag); |
| 402 | s->skin_buffer_size = new_surface.skin_data.size(); |
| 403 | } |
| 404 | |
| 405 | s->vertex_count = new_surface.vertex_count; |
| 406 | |
| 407 | if (new_surface.format & RS::ARRAY_FORMAT_BONES) { |
| 408 | mesh->has_bone_weights = true; |
| 409 | } |
| 410 | |
| 411 | if (new_surface.index_count) { |
| 412 | bool is_index_16 = new_surface.vertex_count <= 65536 && new_surface.vertex_count > 0; |
| 413 | |
| 414 | s->index_buffer = RD::get_singleton()->index_buffer_create(new_surface.index_count, is_index_16 ? RD::INDEX_BUFFER_FORMAT_UINT16 : RD::INDEX_BUFFER_FORMAT_UINT32, new_surface.index_data, false); |
| 415 | s->index_buffer_size = new_surface.index_data.size(); |
| 416 | s->index_count = new_surface.index_count; |
| 417 | s->index_array = RD::get_singleton()->index_array_create(s->index_buffer, 0, s->index_count); |
| 418 | if (new_surface.lods.size()) { |
| 419 | s->lods = memnew_arr(Mesh::Surface::LOD, new_surface.lods.size()); |
| 420 | s->lod_count = new_surface.lods.size(); |
| 421 | |
| 422 | for (int i = 0; i < new_surface.lods.size(); i++) { |
| 423 | uint32_t indices = new_surface.lods[i].index_data.size() / (is_index_16 ? 2 : 4); |
| 424 | s->lods[i].index_buffer = RD::get_singleton()->index_buffer_create(indices, is_index_16 ? RD::INDEX_BUFFER_FORMAT_UINT16 : RD::INDEX_BUFFER_FORMAT_UINT32, new_surface.lods[i].index_data); |
| 425 | s->lods[i].index_buffer_size = new_surface.lods[i].index_data.size(); |
| 426 | s->lods[i].index_array = RD::get_singleton()->index_array_create(s->lods[i].index_buffer, 0, indices); |
| 427 | s->lods[i].edge_length = new_surface.lods[i].edge_length; |
| 428 | s->lods[i].index_count = indices; |
| 429 | } |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | ERR_FAIL_COND_MSG(!new_surface.index_count && !new_surface.vertex_count, "Meshes must contain a vertex array, an index array, or both"); |
| 434 | |
| 435 | s->aabb = new_surface.aabb; |
| 436 | s->bone_aabbs = new_surface.bone_aabbs; //only really useful for returning them. |
| 437 | s->mesh_to_skeleton_xform = p_surface.mesh_to_skeleton_xform; |
| 438 | |
| 439 | s->uv_scale = new_surface.uv_scale; |
| 440 | |
| 441 | if (mesh->blend_shape_count > 0) { |
| 442 | s->blend_shape_buffer = RD::get_singleton()->storage_buffer_create(new_surface.blend_shape_data.size(), new_surface.blend_shape_data); |
| 443 | s->blend_shape_buffer_size = new_surface.blend_shape_data.size(); |
| 444 | } |
| 445 | |
| 446 | if (use_as_storage) { |
| 447 | Vector<RD::Uniform> uniforms; |
| 448 | { |
| 449 | RD::Uniform u; |
| 450 | u.binding = 0; |
| 451 | u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER; |
| 452 | if (s->vertex_buffer.is_valid()) { |
| 453 | u.append_id(s->vertex_buffer); |
| 454 | } else { |
| 455 | u.append_id(default_rd_storage_buffer); |
| 456 | } |
| 457 | uniforms.push_back(u); |
| 458 | } |
| 459 | { |
| 460 | RD::Uniform u; |
| 461 | u.binding = 1; |
| 462 | u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER; |
| 463 | if (s->skin_buffer.is_valid()) { |
| 464 | u.append_id(s->skin_buffer); |
| 465 | } else { |
| 466 | u.append_id(default_rd_storage_buffer); |
| 467 | } |
| 468 | uniforms.push_back(u); |
| 469 | } |
| 470 | { |
| 471 | RD::Uniform u; |
| 472 | u.binding = 2; |
| 473 | u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER; |
| 474 | if (s->blend_shape_buffer.is_valid()) { |
| 475 | u.append_id(s->blend_shape_buffer); |
| 476 | } else { |
| 477 | u.append_id(default_rd_storage_buffer); |
| 478 | } |
| 479 | uniforms.push_back(u); |
| 480 | } |
| 481 | |
| 482 | s->uniform_set = RD::get_singleton()->uniform_set_create(uniforms, skeleton_shader.version_shader[0], SkeletonShader::UNIFORM_SET_SURFACE); |
| 483 | } |
| 484 | |
| 485 | if (mesh->surface_count == 0) { |
| 486 | mesh->aabb = new_surface.aabb; |
| 487 | } else { |
| 488 | mesh->aabb.merge_with(new_surface.aabb); |
| 489 | } |
| 490 | mesh->skeleton_aabb_version = 0; |
| 491 | |
| 492 | s->material = new_surface.material; |
| 493 | |
| 494 | mesh->surfaces = (Mesh::Surface **)memrealloc(mesh->surfaces, sizeof(Mesh::Surface *) * (mesh->surface_count + 1)); |
| 495 | mesh->surfaces[mesh->surface_count] = s; |
| 496 | mesh->surface_count++; |
| 497 | |
| 498 | for (MeshInstance *mi : mesh->instances) { |
| 499 | _mesh_instance_add_surface(mi, mesh, mesh->surface_count - 1); |
| 500 | } |
| 501 | |
| 502 | mesh->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_MESH); |
| 503 | |
| 504 | for (Mesh *E : mesh->shadow_owners) { |
| 505 | Mesh *shadow_owner = E; |
| 506 | shadow_owner->shadow_mesh = RID(); |
| 507 | shadow_owner->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_MESH); |
| 508 | } |
| 509 | |
| 510 | mesh->material_cache.clear(); |
| 511 | } |
| 512 | |
| 513 | void MeshStorage::_mesh_surface_clear(Mesh *p_mesh, int p_surface) { |
| 514 | Mesh::Surface &s = *p_mesh->surfaces[p_surface]; |
| 515 | |
| 516 | if (s.vertex_buffer.is_valid()) { |
| 517 | RD::get_singleton()->free_rid(s.vertex_buffer); // Clears arrays as dependency automatically, including all versions. |
| 518 | } |
| 519 | if (s.attribute_buffer.is_valid()) { |
| 520 | RD::get_singleton()->free_rid(s.attribute_buffer); |
| 521 | } |
| 522 | if (s.skin_buffer.is_valid()) { |
| 523 | RD::get_singleton()->free_rid(s.skin_buffer); |
| 524 | } |
| 525 | if (s.versions) { |
| 526 | memfree(s.versions); // reallocs, so free with memfree. |
| 527 | } |
| 528 | |
| 529 | if (s.index_buffer.is_valid()) { |
| 530 | RD::get_singleton()->free_rid(s.index_buffer); |
| 531 | } |
| 532 | |
| 533 | if (s.lod_count) { |
| 534 | for (uint32_t j = 0; j < s.lod_count; j++) { |
| 535 | RD::get_singleton()->free_rid(s.lods[j].index_buffer); |
| 536 | } |
| 537 | memdelete_arr(s.lods); |
| 538 | } |
| 539 | |
| 540 | if (s.blend_shape_buffer.is_valid()) { |
| 541 | RD::get_singleton()->free_rid(s.blend_shape_buffer); |
| 542 | } |
| 543 | |
| 544 | memdelete(p_mesh->surfaces[p_surface]); |
| 545 | } |
| 546 | |
| 547 | int MeshStorage::mesh_get_blend_shape_count(RID p_mesh) const { |
| 548 | const Mesh *mesh = mesh_owner.get_or_null(p_mesh); |
| 549 | ERR_FAIL_NULL_V(mesh, -1); |
| 550 | return mesh->blend_shape_count; |
| 551 | } |
| 552 | |
| 553 | void MeshStorage::mesh_set_blend_shape_mode(RID p_mesh, RS::BlendShapeMode p_mode) { |
| 554 | Mesh *mesh = mesh_owner.get_or_null(p_mesh); |
| 555 | ERR_FAIL_NULL(mesh); |
| 556 | ERR_FAIL_INDEX((int)p_mode, 2); |
| 557 | |
| 558 | mesh->blend_shape_mode = p_mode; |
| 559 | } |
| 560 | |
| 561 | RS::BlendShapeMode MeshStorage::mesh_get_blend_shape_mode(RID p_mesh) const { |
| 562 | Mesh *mesh = mesh_owner.get_or_null(p_mesh); |
| 563 | ERR_FAIL_NULL_V(mesh, RS::BLEND_SHAPE_MODE_NORMALIZED); |
| 564 | return mesh->blend_shape_mode; |
| 565 | } |
| 566 | |
| 567 | void MeshStorage::mesh_surface_update_vertex_region(RID p_mesh, int p_surface, int p_offset, const Vector<uint8_t> &p_data) { |
| 568 | ERR_FAIL_COND(p_data.is_empty()); |
| 569 | Mesh *mesh = mesh_owner.get_or_null(p_mesh); |
| 570 | ERR_FAIL_NULL(mesh); |
| 571 | ERR_FAIL_UNSIGNED_INDEX((uint32_t)p_surface, mesh->surface_count); |
| 572 | ERR_FAIL_COND(mesh->surfaces[p_surface]->vertex_buffer.is_null()); |
| 573 | |
| 574 | uint64_t data_size = p_data.size(); |
| 575 | const uint8_t *r = p_data.ptr(); |
| 576 | |
| 577 | RD::get_singleton()->buffer_update(mesh->surfaces[p_surface]->vertex_buffer, p_offset, data_size, r); |
| 578 | } |
| 579 | |
| 580 | void MeshStorage::mesh_surface_update_attribute_region(RID p_mesh, int p_surface, int p_offset, const Vector<uint8_t> &p_data) { |
| 581 | ERR_FAIL_COND(p_data.is_empty()); |
| 582 | Mesh *mesh = mesh_owner.get_or_null(p_mesh); |
| 583 | ERR_FAIL_NULL(mesh); |
| 584 | ERR_FAIL_UNSIGNED_INDEX((uint32_t)p_surface, mesh->surface_count); |
| 585 | ERR_FAIL_COND(mesh->surfaces[p_surface]->attribute_buffer.is_null()); |
| 586 | |
| 587 | uint64_t data_size = p_data.size(); |
| 588 | const uint8_t *r = p_data.ptr(); |
| 589 | |
| 590 | RD::get_singleton()->buffer_update(mesh->surfaces[p_surface]->attribute_buffer, p_offset, data_size, r); |
| 591 | } |
| 592 | |
| 593 | void MeshStorage::mesh_surface_update_skin_region(RID p_mesh, int p_surface, int p_offset, const Vector<uint8_t> &p_data) { |
| 594 | ERR_FAIL_COND(p_data.is_empty()); |
| 595 | Mesh *mesh = mesh_owner.get_or_null(p_mesh); |
| 596 | ERR_FAIL_NULL(mesh); |
| 597 | ERR_FAIL_UNSIGNED_INDEX((uint32_t)p_surface, mesh->surface_count); |
| 598 | ERR_FAIL_COND(mesh->surfaces[p_surface]->skin_buffer.is_null()); |
| 599 | |
| 600 | uint64_t data_size = p_data.size(); |
| 601 | const uint8_t *r = p_data.ptr(); |
| 602 | |
| 603 | RD::get_singleton()->buffer_update(mesh->surfaces[p_surface]->skin_buffer, p_offset, data_size, r); |
| 604 | } |
| 605 | |
| 606 | void RendererRD::MeshStorage::mesh_surface_update_index_region(RID p_mesh, int p_surface, int p_offset, const Vector<uint8_t> &p_data) { |
| 607 | ERR_FAIL_COND(p_data.is_empty()); |
| 608 | Mesh *mesh = mesh_owner.get_or_null(p_mesh); |
| 609 | ERR_FAIL_NULL(mesh); |
| 610 | ERR_FAIL_UNSIGNED_INDEX((uint32_t)p_surface, mesh->surface_count); |
| 611 | ERR_FAIL_COND(mesh->surfaces[p_surface]->index_buffer.is_null()); |
| 612 | |
| 613 | uint64_t data_size = p_data.size(); |
| 614 | const uint8_t *r = p_data.ptr(); |
| 615 | |
| 616 | RD::get_singleton()->buffer_update(mesh->surfaces[p_surface]->index_buffer, p_offset, data_size, r); |
| 617 | } |
| 618 | |
| 619 | void MeshStorage::mesh_surface_set_material(RID p_mesh, int p_surface, RID p_material) { |
| 620 | Mesh *mesh = mesh_owner.get_or_null(p_mesh); |
| 621 | ERR_FAIL_NULL(mesh); |
| 622 | ERR_FAIL_UNSIGNED_INDEX((uint32_t)p_surface, mesh->surface_count); |
| 623 | mesh->surfaces[p_surface]->material = p_material; |
| 624 | |
| 625 | mesh->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_MATERIAL); |
| 626 | mesh->material_cache.clear(); |
| 627 | } |
| 628 | |
| 629 | RID MeshStorage::mesh_surface_get_material(RID p_mesh, int p_surface) const { |
| 630 | Mesh *mesh = mesh_owner.get_or_null(p_mesh); |
| 631 | ERR_FAIL_NULL_V(mesh, RID()); |
| 632 | ERR_FAIL_UNSIGNED_INDEX_V((uint32_t)p_surface, mesh->surface_count, RID()); |
| 633 | |
| 634 | return mesh->surfaces[p_surface]->material; |
| 635 | } |
| 636 | |
| 637 | RS::SurfaceData MeshStorage::mesh_get_surface(RID p_mesh, int p_surface) const { |
| 638 | Mesh *mesh = mesh_owner.get_or_null(p_mesh); |
| 639 | ERR_FAIL_NULL_V(mesh, RS::SurfaceData()); |
| 640 | ERR_FAIL_UNSIGNED_INDEX_V((uint32_t)p_surface, mesh->surface_count, RS::SurfaceData()); |
| 641 | |
| 642 | Mesh::Surface &s = *mesh->surfaces[p_surface]; |
| 643 | |
| 644 | RS::SurfaceData sd; |
| 645 | sd.format = s.format; |
| 646 | if (s.vertex_buffer.is_valid()) { |
| 647 | sd.vertex_data = RD::get_singleton()->buffer_get_data(s.vertex_buffer); |
| 648 | // When using an uncompressed buffer with normals, but without tangents, we have to trim the padding. |
| 649 | if (!(s.format & RS::ARRAY_FLAG_COMPRESS_ATTRIBUTES) && (s.format & RS::ARRAY_FORMAT_NORMAL) && !(s.format & RS::ARRAY_FORMAT_TANGENT)) { |
| 650 | sd.vertex_data.resize(sd.vertex_data.size() - sizeof(uint16_t) * 2); |
| 651 | } |
| 652 | } |
| 653 | if (s.attribute_buffer.is_valid()) { |
| 654 | sd.attribute_data = RD::get_singleton()->buffer_get_data(s.attribute_buffer); |
| 655 | } |
| 656 | if (s.skin_buffer.is_valid()) { |
| 657 | sd.skin_data = RD::get_singleton()->buffer_get_data(s.skin_buffer); |
| 658 | } |
| 659 | sd.vertex_count = s.vertex_count; |
| 660 | sd.index_count = s.index_count; |
| 661 | sd.primitive = s.primitive; |
| 662 | |
| 663 | if (sd.index_count) { |
| 664 | sd.index_data = RD::get_singleton()->buffer_get_data(s.index_buffer); |
| 665 | } |
| 666 | sd.aabb = s.aabb; |
| 667 | sd.uv_scale = s.uv_scale; |
| 668 | for (uint32_t i = 0; i < s.lod_count; i++) { |
| 669 | RS::SurfaceData::LOD lod; |
| 670 | lod.edge_length = s.lods[i].edge_length; |
| 671 | lod.index_data = RD::get_singleton()->buffer_get_data(s.lods[i].index_buffer); |
| 672 | sd.lods.push_back(lod); |
| 673 | } |
| 674 | |
| 675 | sd.bone_aabbs = s.bone_aabbs; |
| 676 | sd.mesh_to_skeleton_xform = s.mesh_to_skeleton_xform; |
| 677 | |
| 678 | if (s.blend_shape_buffer.is_valid()) { |
| 679 | sd.blend_shape_data = RD::get_singleton()->buffer_get_data(s.blend_shape_buffer); |
| 680 | } |
| 681 | |
| 682 | return sd; |
| 683 | } |
| 684 | |
| 685 | int MeshStorage::mesh_get_surface_count(RID p_mesh) const { |
| 686 | Mesh *mesh = mesh_owner.get_or_null(p_mesh); |
| 687 | ERR_FAIL_NULL_V(mesh, 0); |
| 688 | return mesh->surface_count; |
| 689 | } |
| 690 | |
| 691 | void MeshStorage::mesh_set_custom_aabb(RID p_mesh, const AABB &p_aabb) { |
| 692 | Mesh *mesh = mesh_owner.get_or_null(p_mesh); |
| 693 | ERR_FAIL_NULL(mesh); |
| 694 | mesh->custom_aabb = p_aabb; |
| 695 | |
| 696 | mesh->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_AABB); |
| 697 | } |
| 698 | |
| 699 | AABB MeshStorage::mesh_get_custom_aabb(RID p_mesh) const { |
| 700 | Mesh *mesh = mesh_owner.get_or_null(p_mesh); |
| 701 | ERR_FAIL_NULL_V(mesh, AABB()); |
| 702 | return mesh->custom_aabb; |
| 703 | } |
| 704 | |
| 705 | AABB MeshStorage::mesh_get_aabb(RID p_mesh, RID p_skeleton) { |
| 706 | Mesh *mesh = mesh_owner.get_or_null(p_mesh); |
| 707 | ERR_FAIL_NULL_V(mesh, AABB()); |
| 708 | |
| 709 | if (mesh->custom_aabb != AABB()) { |
| 710 | return mesh->custom_aabb; |
| 711 | } |
| 712 | |
| 713 | Skeleton *skeleton = skeleton_owner.get_or_null(p_skeleton); |
| 714 | |
| 715 | // A mesh can be shared by multiple skeletons and we need to avoid using the AABB from a different skeleton. |
| 716 | if (!skeleton || skeleton->size == 0 || (mesh->skeleton_aabb_version == skeleton->version && mesh->skeleton_aabb_rid == p_skeleton)) { |
| 717 | return mesh->aabb; |
| 718 | } |
| 719 | |
| 720 | AABB aabb; |
| 721 | |
| 722 | for (uint32_t i = 0; i < mesh->surface_count; i++) { |
| 723 | AABB laabb; |
| 724 | const Mesh::Surface &surface = *mesh->surfaces[i]; |
| 725 | if ((surface.format & RS::ARRAY_FORMAT_BONES) && surface.bone_aabbs.size()) { |
| 726 | int bs = surface.bone_aabbs.size(); |
| 727 | const AABB *skbones = surface.bone_aabbs.ptr(); |
| 728 | |
| 729 | int sbs = skeleton->size; |
| 730 | ERR_CONTINUE(bs > sbs); |
| 731 | const float *baseptr = skeleton->data.ptr(); |
| 732 | |
| 733 | bool found_bone_aabb = false; |
| 734 | |
| 735 | if (skeleton->use_2d) { |
| 736 | for (int j = 0; j < bs; j++) { |
| 737 | if (skbones[j].size == Vector3(-1, -1, -1)) { |
| 738 | continue; //bone is unused |
| 739 | } |
| 740 | |
| 741 | const float *dataptr = baseptr + j * 8; |
| 742 | |
| 743 | Transform3D mtx; |
| 744 | |
| 745 | mtx.basis.rows[0][0] = dataptr[0]; |
| 746 | mtx.basis.rows[0][1] = dataptr[1]; |
| 747 | mtx.origin.x = dataptr[3]; |
| 748 | |
| 749 | mtx.basis.rows[1][0] = dataptr[4]; |
| 750 | mtx.basis.rows[1][1] = dataptr[5]; |
| 751 | mtx.origin.y = dataptr[7]; |
| 752 | |
| 753 | // Transform bounds to skeleton's space before applying animation data. |
| 754 | AABB baabb = surface.mesh_to_skeleton_xform.xform(skbones[j]); |
| 755 | baabb = mtx.xform(baabb); |
| 756 | |
| 757 | if (!found_bone_aabb) { |
| 758 | laabb = baabb; |
| 759 | found_bone_aabb = true; |
| 760 | } else { |
| 761 | laabb.merge_with(baabb); |
| 762 | } |
| 763 | } |
| 764 | } else { |
| 765 | for (int j = 0; j < bs; j++) { |
| 766 | if (skbones[j].size == Vector3(-1, -1, -1)) { |
| 767 | continue; //bone is unused |
| 768 | } |
| 769 | |
| 770 | const float *dataptr = baseptr + j * 12; |
| 771 | |
| 772 | Transform3D mtx; |
| 773 | |
| 774 | mtx.basis.rows[0][0] = dataptr[0]; |
| 775 | mtx.basis.rows[0][1] = dataptr[1]; |
| 776 | mtx.basis.rows[0][2] = dataptr[2]; |
| 777 | mtx.origin.x = dataptr[3]; |
| 778 | mtx.basis.rows[1][0] = dataptr[4]; |
| 779 | mtx.basis.rows[1][1] = dataptr[5]; |
| 780 | mtx.basis.rows[1][2] = dataptr[6]; |
| 781 | mtx.origin.y = dataptr[7]; |
| 782 | mtx.basis.rows[2][0] = dataptr[8]; |
| 783 | mtx.basis.rows[2][1] = dataptr[9]; |
| 784 | mtx.basis.rows[2][2] = dataptr[10]; |
| 785 | mtx.origin.z = dataptr[11]; |
| 786 | |
| 787 | // Transform bounds to skeleton's space before applying animation data. |
| 788 | AABB baabb = surface.mesh_to_skeleton_xform.xform(skbones[j]); |
| 789 | baabb = mtx.xform(baabb); |
| 790 | |
| 791 | if (!found_bone_aabb) { |
| 792 | laabb = baabb; |
| 793 | found_bone_aabb = true; |
| 794 | } else { |
| 795 | laabb.merge_with(baabb); |
| 796 | } |
| 797 | } |
| 798 | } |
| 799 | |
| 800 | if (found_bone_aabb) { |
| 801 | // Transform skeleton bounds back to mesh's space if any animated AABB applied. |
| 802 | laabb = surface.mesh_to_skeleton_xform.affine_inverse().xform(laabb); |
| 803 | } |
| 804 | |
| 805 | if (laabb.size == Vector3()) { |
| 806 | laabb = surface.aabb; |
| 807 | } |
| 808 | } else { |
| 809 | laabb = surface.aabb; |
| 810 | } |
| 811 | |
| 812 | if (i == 0) { |
| 813 | aabb = laabb; |
| 814 | } else { |
| 815 | aabb.merge_with(laabb); |
| 816 | } |
| 817 | } |
| 818 | |
| 819 | mesh->aabb = aabb; |
| 820 | |
| 821 | mesh->skeleton_aabb_version = skeleton->version; |
| 822 | mesh->skeleton_aabb_rid = p_skeleton; |
| 823 | return aabb; |
| 824 | } |
| 825 | |
| 826 | void MeshStorage::mesh_set_path(RID p_mesh, const String &p_path) { |
| 827 | Mesh *mesh = mesh_owner.get_or_null(p_mesh); |
| 828 | ERR_FAIL_NULL(mesh); |
| 829 | |
| 830 | mesh->path = p_path; |
| 831 | } |
| 832 | |
| 833 | String MeshStorage::mesh_get_path(RID p_mesh) const { |
| 834 | Mesh *mesh = mesh_owner.get_or_null(p_mesh); |
| 835 | ERR_FAIL_NULL_V(mesh, String()); |
| 836 | |
| 837 | return mesh->path; |
| 838 | } |
| 839 | |
| 840 | void MeshStorage::mesh_set_shadow_mesh(RID p_mesh, RID p_shadow_mesh) { |
| 841 | ERR_FAIL_COND_MSG(p_mesh == p_shadow_mesh, "Cannot set a mesh as its own shadow mesh."); |
| 842 | Mesh *mesh = mesh_owner.get_or_null(p_mesh); |
| 843 | ERR_FAIL_NULL(mesh); |
| 844 | |
| 845 | Mesh *shadow_mesh = mesh_owner.get_or_null(mesh->shadow_mesh); |
| 846 | if (shadow_mesh) { |
| 847 | shadow_mesh->shadow_owners.erase(mesh); |
| 848 | } |
| 849 | mesh->shadow_mesh = p_shadow_mesh; |
| 850 | |
| 851 | shadow_mesh = mesh_owner.get_or_null(mesh->shadow_mesh); |
| 852 | |
| 853 | if (shadow_mesh) { |
| 854 | shadow_mesh->shadow_owners.insert(mesh); |
| 855 | } |
| 856 | |
| 857 | mesh->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_MESH); |
| 858 | } |
| 859 | |
| 860 | void MeshStorage::mesh_clear(RID p_mesh) { |
| 861 | Mesh *mesh = mesh_owner.get_or_null(p_mesh); |
| 862 | ERR_FAIL_NULL(mesh); |
| 863 | |
| 864 | // Clear instance data before mesh data. |
| 865 | for (MeshInstance *mi : mesh->instances) { |
| 866 | _mesh_instance_clear(mi); |
| 867 | } |
| 868 | |
| 869 | for (uint32_t i = 0; i < mesh->surface_count; i++) { |
| 870 | _mesh_surface_clear(mesh, i); |
| 871 | } |
| 872 | if (mesh->surfaces) { |
| 873 | memfree(mesh->surfaces); |
| 874 | } |
| 875 | |
| 876 | mesh->surfaces = nullptr; |
| 877 | mesh->surface_count = 0; |
| 878 | mesh->material_cache.clear(); |
| 879 | mesh->has_bone_weights = false; |
| 880 | mesh->aabb = AABB(); |
| 881 | mesh->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_MESH); |
| 882 | |
| 883 | for (Mesh *E : mesh->shadow_owners) { |
| 884 | Mesh *shadow_owner = E; |
| 885 | shadow_owner->shadow_mesh = RID(); |
| 886 | shadow_owner->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_MESH); |
| 887 | } |
| 888 | } |
| 889 | |
| 890 | void MeshStorage::mesh_surface_remove(RID p_mesh, int p_surface) { |
| 891 | Mesh *mesh = mesh_owner.get_or_null(p_mesh); |
| 892 | ERR_FAIL_NULL(mesh); |
| 893 | ERR_FAIL_UNSIGNED_INDEX((uint32_t)p_surface, mesh->surface_count); |
| 894 | |
| 895 | // Clear instance data before mesh data. |
| 896 | for (MeshInstance *mi : mesh->instances) { |
| 897 | _mesh_instance_remove_surface(mi, p_surface); |
| 898 | } |
| 899 | |
| 900 | _mesh_surface_clear(mesh, p_surface); |
| 901 | |
| 902 | if ((uint32_t)p_surface < mesh->surface_count - 1) { |
| 903 | memmove(mesh->surfaces + p_surface, mesh->surfaces + p_surface + 1, sizeof(Mesh::Surface *) * (mesh->surface_count - (p_surface + 1))); |
| 904 | } |
| 905 | mesh->surfaces = (Mesh::Surface **)memrealloc(mesh->surfaces, sizeof(Mesh::Surface *) * (mesh->surface_count - 1)); |
| 906 | --mesh->surface_count; |
| 907 | |
| 908 | mesh->material_cache.clear(); |
| 909 | |
| 910 | mesh->skeleton_aabb_version = 0; |
| 911 | |
| 912 | if (mesh->has_bone_weights) { |
| 913 | mesh->has_bone_weights = false; |
| 914 | for (uint32_t i = 0; i < mesh->surface_count; i++) { |
| 915 | if (mesh->surfaces[i]->format & RS::ARRAY_FORMAT_BONES) { |
| 916 | mesh->has_bone_weights = true; |
| 917 | break; |
| 918 | } |
| 919 | } |
| 920 | } |
| 921 | |
| 922 | if (mesh->surface_count == 0) { |
| 923 | mesh->aabb = AABB(); |
| 924 | } else { |
| 925 | mesh->aabb = mesh->surfaces[0]->aabb; |
| 926 | for (uint32_t i = 1; i < mesh->surface_count; i++) { |
| 927 | mesh->aabb.merge_with(mesh->surfaces[i]->aabb); |
| 928 | } |
| 929 | } |
| 930 | |
| 931 | mesh->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_MESH); |
| 932 | |
| 933 | for (Mesh *E : mesh->shadow_owners) { |
| 934 | Mesh *shadow_owner = E; |
| 935 | shadow_owner->shadow_mesh = RID(); |
| 936 | shadow_owner->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_MESH); |
| 937 | } |
| 938 | } |
| 939 | |
| 940 | void MeshStorage::mesh_debug_usage(List<RS::MeshInfo> *r_info) { |
| 941 | for (const RID &mesh_rid : mesh_owner.get_owned_list()) { |
| 942 | Mesh *mesh = mesh_owner.get_or_null(mesh_rid); |
| 943 | if (!mesh) { |
| 944 | continue; |
| 945 | } |
| 946 | RS::MeshInfo mesh_info; |
| 947 | mesh_info.mesh = mesh_rid; |
| 948 | mesh_info.path = mesh->path; |
| 949 | |
| 950 | for (uint32_t surface_index = 0; surface_index < mesh->surface_count; surface_index++) { |
| 951 | MeshStorage::Mesh::Surface *surface = mesh->surfaces[surface_index]; |
| 952 | |
| 953 | mesh_info.vertex_buffer_size += surface->vertex_buffer_size; |
| 954 | mesh_info.attribute_buffer_size += surface->attribute_buffer_size; |
| 955 | mesh_info.skin_buffer_size += surface->skin_buffer_size; |
| 956 | mesh_info.index_buffer_size += surface->index_buffer_size; |
| 957 | mesh_info.blend_shape_buffer_size += surface->blend_shape_buffer_size; |
| 958 | mesh_info.vertex_count += surface->vertex_count; |
| 959 | |
| 960 | for (uint32_t lod_index = 0; lod_index < surface->lod_count; lod_index++) { |
| 961 | mesh_info.lod_index_buffers_size += surface->lods[lod_index].index_buffer_size; |
| 962 | } |
| 963 | } |
| 964 | |
| 965 | r_info->push_back(mesh_info); |
| 966 | } |
| 967 | } |
| 968 | |
| 969 | bool MeshStorage::mesh_needs_instance(RID p_mesh, bool p_has_skeleton) { |
| 970 | Mesh *mesh = mesh_owner.get_or_null(p_mesh); |
| 971 | ERR_FAIL_NULL_V(mesh, false); |
| 972 | |
| 973 | return mesh->blend_shape_count > 0 || (mesh->has_bone_weights && p_has_skeleton); |
| 974 | } |
| 975 | |
| 976 | Dependency *MeshStorage::mesh_get_dependency(RID p_mesh) const { |
| 977 | Mesh *mesh = mesh_owner.get_or_null(p_mesh); |
| 978 | ERR_FAIL_NULL_V(mesh, nullptr); |
| 979 | |
| 980 | return &mesh->dependency; |
| 981 | } |
| 982 | |
| 983 | /* MESH INSTANCE */ |
| 984 | |
| 985 | RID MeshStorage::mesh_instance_create(RID p_base) { |
| 986 | Mesh *mesh = mesh_owner.get_or_null(p_base); |
| 987 | ERR_FAIL_NULL_V(mesh, RID()); |
| 988 | |
| 989 | RID rid = mesh_instance_owner.make_rid(); |
| 990 | MeshInstance *mi = mesh_instance_owner.get_or_null(rid); |
| 991 | |
| 992 | mi->mesh = mesh; |
| 993 | |
| 994 | for (uint32_t i = 0; i < mesh->surface_count; i++) { |
| 995 | _mesh_instance_add_surface(mi, mesh, i); |
| 996 | } |
| 997 | |
| 998 | mi->I = mesh->instances.push_back(mi); |
| 999 | |
| 1000 | mi->dirty = true; |
| 1001 | |
| 1002 | return rid; |
| 1003 | } |
| 1004 | |
| 1005 | void MeshStorage::mesh_instance_free(RID p_rid) { |
| 1006 | MeshInstance *mi = mesh_instance_owner.get_or_null(p_rid); |
| 1007 | _mesh_instance_clear(mi); |
| 1008 | mi->mesh->instances.erase(mi->I); |
| 1009 | mi->I = nullptr; |
| 1010 | |
| 1011 | mesh_instance_owner.free(p_rid); |
| 1012 | } |
| 1013 | |
| 1014 | void MeshStorage::mesh_instance_set_skeleton(RID p_mesh_instance, RID p_skeleton) { |
| 1015 | MeshInstance *mi = mesh_instance_owner.get_or_null(p_mesh_instance); |
| 1016 | if (mi->skeleton == p_skeleton) { |
| 1017 | return; |
| 1018 | } |
| 1019 | mi->skeleton = p_skeleton; |
| 1020 | mi->skeleton_version = 0; |
| 1021 | mi->dirty = true; |
| 1022 | } |
| 1023 | |
| 1024 | void MeshStorage::mesh_instance_set_blend_shape_weight(RID p_mesh_instance, int p_shape, float p_weight) { |
| 1025 | MeshInstance *mi = mesh_instance_owner.get_or_null(p_mesh_instance); |
| 1026 | ERR_FAIL_NULL(mi); |
| 1027 | ERR_FAIL_INDEX(p_shape, (int)mi->blend_weights.size()); |
| 1028 | mi->blend_weights[p_shape] = p_weight; |
| 1029 | mi->weights_dirty = true; |
| 1030 | //will be eventually updated |
| 1031 | } |
| 1032 | |
| 1033 | void MeshStorage::_mesh_instance_clear(MeshInstance *mi) { |
| 1034 | while (mi->surfaces.size()) { |
| 1035 | _mesh_instance_remove_surface(mi, mi->surfaces.size() - 1); |
| 1036 | } |
| 1037 | mi->dirty = false; |
| 1038 | } |
| 1039 | |
| 1040 | void MeshStorage::_mesh_instance_add_surface(MeshInstance *mi, Mesh *mesh, uint32_t p_surface) { |
| 1041 | if (mesh->blend_shape_count > 0 && mi->blend_weights_buffer.is_null()) { |
| 1042 | mi->blend_weights.resize(mesh->blend_shape_count); |
| 1043 | for (float &weight : mi->blend_weights) { |
| 1044 | weight = 0; |
| 1045 | } |
| 1046 | mi->blend_weights_buffer = RD::get_singleton()->storage_buffer_create(sizeof(float) * mi->blend_weights.size(), mi->blend_weights.span().reinterpret<uint8_t>()); |
| 1047 | mi->weights_dirty = true; |
| 1048 | } |
| 1049 | |
| 1050 | MeshInstance::Surface s; |
| 1051 | if ((mesh->blend_shape_count > 0 || (mesh->surfaces[p_surface]->format & RS::ARRAY_FORMAT_BONES)) && mesh->surfaces[p_surface]->vertex_buffer_size > 0) { |
| 1052 | _mesh_instance_add_surface_buffer(mi, mesh, &s, p_surface, 0); |
| 1053 | } |
| 1054 | |
| 1055 | mi->surfaces.push_back(s); |
| 1056 | mi->dirty = true; |
| 1057 | } |
| 1058 | |
| 1059 | void MeshStorage::_mesh_instance_add_surface_buffer(MeshInstance *mi, Mesh *mesh, MeshInstance::Surface *s, uint32_t p_surface, uint32_t p_buffer_index) { |
| 1060 | s->vertex_buffer[p_buffer_index] = RD::get_singleton()->vertex_buffer_create(mesh->surfaces[p_surface]->vertex_buffer_size, Vector<uint8_t>(), RD::BUFFER_CREATION_AS_STORAGE_BIT); |
| 1061 | |
| 1062 | Vector<RD::Uniform> uniforms; |
| 1063 | { |
| 1064 | RD::Uniform u; |
| 1065 | u.binding = 1; |
| 1066 | u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER; |
| 1067 | u.append_id(s->vertex_buffer[p_buffer_index]); |
| 1068 | uniforms.push_back(u); |
| 1069 | } |
| 1070 | { |
| 1071 | RD::Uniform u; |
| 1072 | u.binding = 2; |
| 1073 | u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER; |
| 1074 | if (mi->blend_weights_buffer.is_valid()) { |
| 1075 | u.append_id(mi->blend_weights_buffer); |
| 1076 | } else { |
| 1077 | u.append_id(default_rd_storage_buffer); |
| 1078 | } |
| 1079 | uniforms.push_back(u); |
| 1080 | } |
| 1081 | s->uniform_set[p_buffer_index] = RD::get_singleton()->uniform_set_create(uniforms, skeleton_shader.version_shader[0], SkeletonShader::UNIFORM_SET_INSTANCE); |
| 1082 | } |
| 1083 | |
| 1084 | void MeshStorage::_mesh_instance_remove_surface(MeshInstance *mi, int p_surface) { |
| 1085 | MeshInstance::Surface &surface = mi->surfaces[p_surface]; |
| 1086 | |
| 1087 | if (surface.versions) { |
| 1088 | for (uint32_t j = 0; j < surface.version_count; j++) { |
| 1089 | RD::get_singleton()->free_rid(surface.versions[j].vertex_array); |
| 1090 | } |
| 1091 | memfree(surface.versions); |
| 1092 | } |
| 1093 | for (uint32_t i = 0; i < 2; i++) { |
| 1094 | if (surface.vertex_buffer[i].is_valid()) { |
| 1095 | RD::get_singleton()->free_rid(surface.vertex_buffer[i]); |
| 1096 | } |
| 1097 | } |
| 1098 | |
| 1099 | mi->surfaces.remove_at(p_surface); |
| 1100 | |
| 1101 | if (mi->surfaces.is_empty()) { |
| 1102 | if (mi->blend_weights_buffer.is_valid()) { |
| 1103 | RD::get_singleton()->free_rid(mi->blend_weights_buffer); |
| 1104 | mi->blend_weights_buffer = RID(); |
| 1105 | } |
| 1106 | |
| 1107 | mi->blend_weights.clear(); |
| 1108 | mi->weights_dirty = false; |
| 1109 | mi->skeleton_version = 0; |
| 1110 | } |
| 1111 | mi->dirty = true; |
| 1112 | } |
| 1113 | |
| 1114 | void MeshStorage::mesh_instance_check_for_update(RID p_mesh_instance) { |
| 1115 | MeshInstance *mi = mesh_instance_owner.get_or_null(p_mesh_instance); |
| 1116 | |
| 1117 | bool needs_update = mi->dirty; |
| 1118 | |
| 1119 | if (mi->weights_dirty && !mi->weight_update_list.in_list()) { |
| 1120 | dirty_mesh_instance_weights.add(&mi->weight_update_list); |
| 1121 | needs_update = true; |
| 1122 | } |
| 1123 | |
| 1124 | if (mi->array_update_list.in_list()) { |
| 1125 | return; |
| 1126 | } |
| 1127 | |
| 1128 | if (!needs_update && mi->skeleton.is_valid()) { |
| 1129 | Skeleton *sk = skeleton_owner.get_or_null(mi->skeleton); |
| 1130 | if (sk && sk->version != mi->skeleton_version) { |
| 1131 | needs_update = true; |
| 1132 | } |
| 1133 | } |
| 1134 | |
| 1135 | if (needs_update) { |
| 1136 | dirty_mesh_instance_arrays.add(&mi->array_update_list); |
| 1137 | } |
| 1138 | } |
| 1139 | |
| 1140 | void MeshStorage::mesh_instance_set_canvas_item_transform(RID p_mesh_instance, const Transform2D &p_transform) { |
| 1141 | MeshInstance *mi = mesh_instance_owner.get_or_null(p_mesh_instance); |
| 1142 | mi->canvas_item_transform_2d = p_transform; |
| 1143 | } |
| 1144 | |
| 1145 | void MeshStorage::update_mesh_instances() { |
| 1146 | while (dirty_mesh_instance_weights.first()) { |
| 1147 | MeshInstance *mi = dirty_mesh_instance_weights.first()->self(); |
| 1148 | |
| 1149 | if (mi->blend_weights_buffer.is_valid()) { |
| 1150 | RD::get_singleton()->buffer_update(mi->blend_weights_buffer, 0, mi->blend_weights.size() * sizeof(float), mi->blend_weights.ptr()); |
| 1151 | } |
| 1152 | dirty_mesh_instance_weights.remove(&mi->weight_update_list); |
| 1153 | mi->weights_dirty = false; |
| 1154 | } |
| 1155 | if (dirty_mesh_instance_arrays.first() == nullptr) { |
| 1156 | return; //nothing to do |
| 1157 | } |
| 1158 | |
| 1159 | //process skeletons and blend shapes |
| 1160 | uint64_t frame = RSG::rasterizer->get_frame_number(); |
| 1161 | bool uses_motion_vectors = (RSG::viewport->get_num_viewports_with_motion_vectors() > 0) || (RendererCompositorStorage::get_singleton()->get_num_compositor_effects_with_motion_vectors() > 0); |
| 1162 | RD::ComputeListID compute_list = RD::get_singleton()->compute_list_begin(); |
| 1163 | |
| 1164 | while (dirty_mesh_instance_arrays.first()) { |
| 1165 | MeshInstance *mi = dirty_mesh_instance_arrays.first()->self(); |
| 1166 | |
| 1167 | Skeleton *sk = skeleton_owner.get_or_null(mi->skeleton); |
| 1168 | |
| 1169 | for (uint32_t i = 0; i < mi->surfaces.size(); i++) { |
| 1170 | if (mi->surfaces[i].uniform_set[0].is_null() || mi->mesh->surfaces[i]->uniform_set.is_null()) { |
| 1171 | // Skip over mesh instances that don't require their own uniform buffers. |
| 1172 | continue; |
| 1173 | } |
| 1174 | |
| 1175 | mi->surfaces[i].previous_buffer = mi->surfaces[i].current_buffer; |
| 1176 | |
| 1177 | if (uses_motion_vectors && mi->surfaces[i].last_change && (frame - mi->surfaces[i].last_change) <= 2) { |
| 1178 | // Use a 2-frame tolerance so that stepped skeletal animations have correct motion vectors |
| 1179 | // (stepped animation is common for distant NPCs). |
| 1180 | uint32_t new_buffer_index = mi->surfaces[i].current_buffer ^ 1; |
| 1181 | |
| 1182 | if (mi->surfaces[i].uniform_set[new_buffer_index].is_null()) { |
| 1183 | // Create the new vertex buffer on demand where the result for the current frame will be stored. |
| 1184 | _mesh_instance_add_surface_buffer(mi, mi->mesh, &mi->surfaces[i], i, new_buffer_index); |
| 1185 | } |
| 1186 | |
| 1187 | mi->surfaces[i].current_buffer = new_buffer_index; |
| 1188 | } |
| 1189 | |
| 1190 | mi->surfaces[i].last_change = frame; |
| 1191 | |
| 1192 | RID mi_surface_uniform_set = mi->surfaces[i].uniform_set[mi->surfaces[i].current_buffer]; |
| 1193 | if (mi_surface_uniform_set.is_null()) { |
| 1194 | continue; |
| 1195 | } |
| 1196 | |
| 1197 | bool array_is_2d = mi->mesh->surfaces[i]->format & RS::ARRAY_FLAG_USE_2D_VERTICES; |
| 1198 | |
| 1199 | RD::get_singleton()->compute_list_bind_compute_pipeline(compute_list, skeleton_shader.pipeline[array_is_2d ? SkeletonShader::SHADER_MODE_2D : SkeletonShader::SHADER_MODE_3D]); |
| 1200 | |
| 1201 | RD::get_singleton()->compute_list_bind_uniform_set(compute_list, mi_surface_uniform_set, SkeletonShader::UNIFORM_SET_INSTANCE); |
| 1202 | RD::get_singleton()->compute_list_bind_uniform_set(compute_list, mi->mesh->surfaces[i]->uniform_set, SkeletonShader::UNIFORM_SET_SURFACE); |
| 1203 | if (sk && sk->uniform_set_mi.is_valid()) { |
| 1204 | RD::get_singleton()->compute_list_bind_uniform_set(compute_list, sk->uniform_set_mi, SkeletonShader::UNIFORM_SET_SKELETON); |
| 1205 | } else { |
| 1206 | RD::get_singleton()->compute_list_bind_uniform_set(compute_list, skeleton_shader.default_skeleton_uniform_set, SkeletonShader::UNIFORM_SET_SKELETON); |
| 1207 | } |
| 1208 | |
| 1209 | SkeletonShader::PushConstant push_constant; |
| 1210 | |
| 1211 | push_constant.has_normal = mi->mesh->surfaces[i]->format & RS::ARRAY_FORMAT_NORMAL; |
| 1212 | push_constant.has_tangent = mi->mesh->surfaces[i]->format & RS::ARRAY_FORMAT_TANGENT; |
| 1213 | push_constant.has_skeleton = sk != nullptr && sk->use_2d == array_is_2d && (mi->mesh->surfaces[i]->format & RS::ARRAY_FORMAT_BONES); |
| 1214 | push_constant.has_blend_shape = mi->mesh->blend_shape_count > 0; |
| 1215 | |
| 1216 | push_constant.normal_tangent_stride = (push_constant.has_normal ? 1 : 0) + (push_constant.has_tangent ? 1 : 0); |
| 1217 | |
| 1218 | push_constant.vertex_count = mi->mesh->surfaces[i]->vertex_count; |
| 1219 | push_constant.vertex_stride = ((mi->mesh->surfaces[i]->vertex_buffer_size / mi->mesh->surfaces[i]->vertex_count) / 4) - push_constant.normal_tangent_stride; |
| 1220 | push_constant.skin_stride = (mi->mesh->surfaces[i]->skin_buffer_size / mi->mesh->surfaces[i]->vertex_count) / 4; |
| 1221 | push_constant.skin_weight_offset = (mi->mesh->surfaces[i]->format & RS::ARRAY_FLAG_USE_8_BONE_WEIGHTS) ? 4 : 2; |
| 1222 | |
| 1223 | Transform2D transform = Transform2D(); |
| 1224 | if (sk && sk->use_2d) { |
| 1225 | transform = mi->canvas_item_transform_2d.affine_inverse() * sk->base_transform_2d; |
| 1226 | } |
| 1227 | push_constant.skeleton_transform_x[0] = transform.columns[0][0]; |
| 1228 | push_constant.skeleton_transform_x[1] = transform.columns[0][1]; |
| 1229 | push_constant.skeleton_transform_y[0] = transform.columns[1][0]; |
| 1230 | push_constant.skeleton_transform_y[1] = transform.columns[1][1]; |
| 1231 | push_constant.skeleton_transform_offset[0] = transform.columns[2][0]; |
| 1232 | push_constant.skeleton_transform_offset[1] = transform.columns[2][1]; |
| 1233 | |
| 1234 | Transform2D inverse_transform = transform.affine_inverse(); |
| 1235 | push_constant.inverse_transform_x[0] = inverse_transform.columns[0][0]; |
| 1236 | push_constant.inverse_transform_x[1] = inverse_transform.columns[0][1]; |
| 1237 | push_constant.inverse_transform_y[0] = inverse_transform.columns[1][0]; |
| 1238 | push_constant.inverse_transform_y[1] = inverse_transform.columns[1][1]; |
| 1239 | push_constant.inverse_transform_offset[0] = inverse_transform.columns[2][0]; |
| 1240 | push_constant.inverse_transform_offset[1] = inverse_transform.columns[2][1]; |
| 1241 | |
| 1242 | push_constant.blend_shape_count = mi->mesh->blend_shape_count; |
| 1243 | push_constant.normalized_blend_shapes = mi->mesh->blend_shape_mode == RS::BLEND_SHAPE_MODE_NORMALIZED; |
| 1244 | push_constant.pad1 = 0; |
| 1245 | |
| 1246 | RD::get_singleton()->compute_list_set_push_constant(compute_list, &push_constant, sizeof(SkeletonShader::PushConstant)); |
| 1247 | |
| 1248 | //dispatch without barrier, so all is done at the same time |
| 1249 | RD::get_singleton()->compute_list_dispatch_threads(compute_list, push_constant.vertex_count, 1, 1); |
| 1250 | } |
| 1251 | |
| 1252 | mi->dirty = false; |
| 1253 | if (sk) { |
| 1254 | mi->skeleton_version = sk->version; |
| 1255 | } |
| 1256 | dirty_mesh_instance_arrays.remove(&mi->array_update_list); |
| 1257 | } |
| 1258 | |
| 1259 | RD::get_singleton()->compute_list_end(); |
| 1260 | } |
| 1261 | |
| 1262 | RD::VertexFormatID MeshStorage::_mesh_surface_generate_vertex_format(uint64_t p_surface_format, uint64_t p_input_mask, bool p_instanced_surface, bool p_input_motion_vectors, bool p_point_size_emulated, uint32_t &r_position_stride) { |
| 1263 | Vector<RD::VertexAttribute> attributes; |
| 1264 | uint32_t normal_tangent_stride = 0; |
| 1265 | uint32_t attribute_stride = 0; |
| 1266 | uint32_t skin_stride = 0; |
| 1267 | |
| 1268 | r_position_stride = 0; |
| 1269 | |
| 1270 | for (int i = 0; i < RS::ARRAY_INDEX; i++) { |
| 1271 | RD::VertexAttribute vd; |
| 1272 | vd.location = i; |
| 1273 | |
| 1274 | if (!(p_surface_format & (1ULL << i))) { |
| 1275 | vd.stride = 0; |
| 1276 | switch (i) { |
| 1277 | case RS::ARRAY_VERTEX: |
| 1278 | case RS::ARRAY_NORMAL: |
| 1279 | vd.format = RD::DATA_FORMAT_R32G32B32_SFLOAT; |
| 1280 | break; |
| 1281 | case RS::ARRAY_TEX_UV: |
| 1282 | case RS::ARRAY_TEX_UV2: |
| 1283 | vd.format = RD::DATA_FORMAT_R32G32_SFLOAT; |
| 1284 | break; |
| 1285 | case RS::ARRAY_BONES: |
| 1286 | vd.format = RD::DATA_FORMAT_R32G32B32A32_UINT; |
| 1287 | break; |
| 1288 | case RS::ARRAY_TANGENT: |
| 1289 | case RS::ARRAY_COLOR: |
| 1290 | case RS::ARRAY_CUSTOM0: |
| 1291 | case RS::ARRAY_CUSTOM1: |
| 1292 | case RS::ARRAY_CUSTOM2: |
| 1293 | case RS::ARRAY_CUSTOM3: |
| 1294 | case RS::ARRAY_WEIGHTS: |
| 1295 | vd.format = RD::DATA_FORMAT_R32G32B32A32_SFLOAT; |
| 1296 | break; |
| 1297 | default: |
| 1298 | DEV_ASSERT(false && "Unknown vertex format element."); |
| 1299 | break; |
| 1300 | } |
| 1301 | } else { |
| 1302 | // Mark that it needs a stride set (default uses 0). |
| 1303 | vd.stride = 1; |
| 1304 | |
| 1305 | switch (i) { |
| 1306 | case RS::ARRAY_VERTEX: { |
| 1307 | vd.offset = r_position_stride; |
| 1308 | |
| 1309 | if (p_surface_format & RS::ARRAY_FLAG_USE_2D_VERTICES) { |
| 1310 | vd.format = RD::DATA_FORMAT_R32G32_SFLOAT; |
| 1311 | r_position_stride = sizeof(float) * 2; |
| 1312 | } else { |
| 1313 | if (!p_instanced_surface && (p_surface_format & RS::ARRAY_FLAG_COMPRESS_ATTRIBUTES)) { |
| 1314 | vd.format = RD::DATA_FORMAT_R16G16B16A16_UNORM; |
| 1315 | r_position_stride = sizeof(uint16_t) * 4; |
| 1316 | } else { |
| 1317 | vd.format = RD::DATA_FORMAT_R32G32B32_SFLOAT; |
| 1318 | r_position_stride = sizeof(float) * 3; |
| 1319 | } |
| 1320 | } |
| 1321 | |
| 1322 | } break; |
| 1323 | case RS::ARRAY_NORMAL: { |
| 1324 | vd.offset = 0; |
| 1325 | |
| 1326 | if (!p_instanced_surface && (p_surface_format & RS::ARRAY_FLAG_COMPRESS_ATTRIBUTES)) { |
| 1327 | vd.format = RD::DATA_FORMAT_R16G16_UNORM; |
| 1328 | normal_tangent_stride += sizeof(uint16_t) * 2; |
| 1329 | } else { |
| 1330 | vd.format = RD::DATA_FORMAT_R16G16B16A16_UNORM; |
| 1331 | // A small trick here: if we are uncompressed and we have normals, but no tangents. We need |
| 1332 | // the shader to think there are 4 components to "axis_tangent_attrib". So we give a size of 4, |
| 1333 | // but a stride based on only having 2 elements. |
| 1334 | if (!(p_surface_format & RS::ARRAY_FORMAT_TANGENT)) { |
| 1335 | normal_tangent_stride += sizeof(uint16_t) * 2; |
| 1336 | } else { |
| 1337 | normal_tangent_stride += sizeof(uint16_t) * 4; |
| 1338 | } |
| 1339 | } |
| 1340 | } break; |
| 1341 | case RS::ARRAY_TANGENT: { |
| 1342 | vd.stride = 0; |
| 1343 | vd.format = RD::DATA_FORMAT_R32G32B32A32_SFLOAT; |
| 1344 | } break; |
| 1345 | case RS::ARRAY_COLOR: { |
| 1346 | vd.offset = attribute_stride; |
| 1347 | |
| 1348 | vd.format = RD::DATA_FORMAT_R8G8B8A8_UNORM; |
| 1349 | attribute_stride += sizeof(int8_t) * 4; |
| 1350 | } break; |
| 1351 | case RS::ARRAY_TEX_UV: { |
| 1352 | vd.offset = attribute_stride; |
| 1353 | if (p_surface_format & RS::ARRAY_FLAG_COMPRESS_ATTRIBUTES) { |
| 1354 | vd.format = RD::DATA_FORMAT_R16G16_UNORM; |
| 1355 | attribute_stride += sizeof(uint16_t) * 2; |
| 1356 | } else { |
| 1357 | vd.format = RD::DATA_FORMAT_R32G32_SFLOAT; |
| 1358 | attribute_stride += sizeof(float) * 2; |
| 1359 | } |
| 1360 | |
| 1361 | } break; |
| 1362 | case RS::ARRAY_TEX_UV2: { |
| 1363 | vd.offset = attribute_stride; |
| 1364 | if (p_surface_format & RS::ARRAY_FLAG_COMPRESS_ATTRIBUTES) { |
| 1365 | vd.format = RD::DATA_FORMAT_R16G16_UNORM; |
| 1366 | attribute_stride += sizeof(uint16_t) * 2; |
| 1367 | } else { |
| 1368 | vd.format = RD::DATA_FORMAT_R32G32_SFLOAT; |
| 1369 | attribute_stride += sizeof(float) * 2; |
| 1370 | } |
| 1371 | } break; |
| 1372 | case RS::ARRAY_CUSTOM0: |
| 1373 | case RS::ARRAY_CUSTOM1: |
| 1374 | case RS::ARRAY_CUSTOM2: |
| 1375 | case RS::ARRAY_CUSTOM3: { |
| 1376 | vd.offset = attribute_stride; |
| 1377 | |
| 1378 | int idx = i - RS::ARRAY_CUSTOM0; |
| 1379 | const uint32_t fmt_shift[RS::ARRAY_CUSTOM_COUNT] = { RS::ARRAY_FORMAT_CUSTOM0_SHIFT, RS::ARRAY_FORMAT_CUSTOM1_SHIFT, RS::ARRAY_FORMAT_CUSTOM2_SHIFT, RS::ARRAY_FORMAT_CUSTOM3_SHIFT }; |
| 1380 | uint32_t fmt = (p_surface_format >> fmt_shift[idx]) & RS::ARRAY_FORMAT_CUSTOM_MASK; |
| 1381 | const uint32_t fmtsize[RS::ARRAY_CUSTOM_MAX] = { 4, 4, 4, 8, 4, 8, 12, 16 }; |
| 1382 | const RD::DataFormat fmtrd[RS::ARRAY_CUSTOM_MAX] = { RD::DATA_FORMAT_R8G8B8A8_UNORM, RD::DATA_FORMAT_R8G8B8A8_SNORM, RD::DATA_FORMAT_R16G16_SFLOAT, RD::DATA_FORMAT_R16G16B16A16_SFLOAT, RD::DATA_FORMAT_R32_SFLOAT, RD::DATA_FORMAT_R32G32_SFLOAT, RD::DATA_FORMAT_R32G32B32_SFLOAT, RD::DATA_FORMAT_R32G32B32A32_SFLOAT }; |
| 1383 | vd.format = fmtrd[fmt]; |
| 1384 | attribute_stride += fmtsize[fmt]; |
| 1385 | } break; |
| 1386 | case RS::ARRAY_BONES: { |
| 1387 | vd.offset = skin_stride; |
| 1388 | |
| 1389 | vd.format = RD::DATA_FORMAT_R16G16B16A16_UINT; |
| 1390 | skin_stride += sizeof(int16_t) * 4; |
| 1391 | } break; |
| 1392 | case RS::ARRAY_WEIGHTS: { |
| 1393 | vd.offset = skin_stride; |
| 1394 | |
| 1395 | vd.format = RD::DATA_FORMAT_R16G16B16A16_UNORM; |
| 1396 | skin_stride += sizeof(int16_t) * 4; |
| 1397 | } break; |
| 1398 | } |
| 1399 | |
| 1400 | if (p_point_size_emulated) { |
| 1401 | vd.frequency = RD::VERTEX_FREQUENCY_INSTANCE; |
| 1402 | } |
| 1403 | } |
| 1404 | |
| 1405 | if (!(p_input_mask & (1ULL << i))) { |
| 1406 | continue; // Shader does not need this, skip it (but computing stride was important anyway) |
| 1407 | } |
| 1408 | |
| 1409 | attributes.push_back(vd); |
| 1410 | |
| 1411 | if (p_input_motion_vectors) { |
| 1412 | // Since the previous vertex, normal and tangent can't be part of the vertex format but they are required when |
| 1413 | // motion vectors are enabled, we opt to push a copy of the vertex attribute with a different location. |
| 1414 | switch (i) { |
| 1415 | case RS::ARRAY_VERTEX: { |
| 1416 | vd.location = ATTRIBUTE_LOCATION_PREV_VERTEX; |
| 1417 | } break; |
| 1418 | case RS::ARRAY_NORMAL: { |
| 1419 | vd.location = ATTRIBUTE_LOCATION_PREV_NORMAL; |
| 1420 | } break; |
| 1421 | case RS::ARRAY_TANGENT: { |
| 1422 | vd.location = ATTRIBUTE_LOCATION_PREV_TANGENT; |
| 1423 | } break; |
| 1424 | } |
| 1425 | |
| 1426 | if (int(vd.location) != i) { |
| 1427 | attributes.push_back(vd); |
| 1428 | } |
| 1429 | } |
| 1430 | } |
| 1431 | |
| 1432 | // Update final stride. |
| 1433 | for (int i = 0; i < attributes.size(); i++) { |
| 1434 | if (attributes[i].stride == 0) { |
| 1435 | // Default location. |
| 1436 | continue; |
| 1437 | } |
| 1438 | |
| 1439 | int loc = attributes[i].location; |
| 1440 | if (loc == RS::ARRAY_VERTEX || loc == ATTRIBUTE_LOCATION_PREV_VERTEX) { |
| 1441 | attributes.write[i].stride = r_position_stride; |
| 1442 | } else if ((loc < RS::ARRAY_COLOR) || ((loc >= ATTRIBUTE_LOCATION_PREV_NORMAL) && (loc <= ATTRIBUTE_LOCATION_PREV_TANGENT))) { |
| 1443 | attributes.write[i].stride = normal_tangent_stride; |
| 1444 | } else if (loc < RS::ARRAY_BONES) { |
| 1445 | attributes.write[i].stride = attribute_stride; |
| 1446 | } else { |
| 1447 | attributes.write[i].stride = skin_stride; |
| 1448 | } |
| 1449 | } |
| 1450 | |
| 1451 | return RD::get_singleton()->vertex_format_create(attributes); |
| 1452 | } |
| 1453 | |
| 1454 | void MeshStorage::_mesh_surface_generate_version_for_input_mask(Mesh::Surface::Version &v, Mesh::Surface *s, uint64_t p_input_mask, bool p_input_motion_vectors, bool p_point_size_emulated, MeshInstance::Surface *mis, uint32_t p_current_buffer, uint32_t p_previous_buffer) { |
| 1455 | uint32_t position_stride = 0; |
| 1456 | v.vertex_format = _mesh_surface_generate_vertex_format(s->format, p_input_mask, mis != nullptr, p_input_motion_vectors, p_point_size_emulated, position_stride); |
| 1457 | |
| 1458 | Vector<RID> buffers; |
| 1459 | Vector<uint64_t> offsets; |
| 1460 | RID buffer; |
| 1461 | uint64_t offset = 0; |
| 1462 | for (int i = 0; i < RS::ARRAY_INDEX; i++) { |
| 1463 | offset = 0; |
| 1464 | |
| 1465 | if (!(s->format & (1ULL << i))) { |
| 1466 | // Not supplied by surface, use default buffers. |
| 1467 | buffer = mesh_default_rd_buffers[i]; |
| 1468 | } else { |
| 1469 | // Supplied by surface, use buffer. |
| 1470 | switch (i) { |
| 1471 | case RS::ARRAY_VERTEX: |
| 1472 | case RS::ARRAY_NORMAL: |
| 1473 | offset = i == RS::ARRAY_NORMAL ? position_stride * s->vertex_count : 0; |
| 1474 | buffer = mis != nullptr ? mis->vertex_buffer[p_current_buffer] : s->vertex_buffer; |
| 1475 | break; |
| 1476 | case RS::ARRAY_TANGENT: |
| 1477 | buffer = mesh_default_rd_buffers[i]; |
| 1478 | break; |
| 1479 | case RS::ARRAY_COLOR: |
| 1480 | case RS::ARRAY_TEX_UV: |
| 1481 | case RS::ARRAY_TEX_UV2: |
| 1482 | case RS::ARRAY_CUSTOM0: |
| 1483 | case RS::ARRAY_CUSTOM1: |
| 1484 | case RS::ARRAY_CUSTOM2: |
| 1485 | case RS::ARRAY_CUSTOM3: |
| 1486 | buffer = s->attribute_buffer; |
| 1487 | break; |
| 1488 | case RS::ARRAY_BONES: |
| 1489 | case RS::ARRAY_WEIGHTS: |
| 1490 | buffer = s->skin_buffer; |
| 1491 | break; |
| 1492 | } |
| 1493 | } |
| 1494 | |
| 1495 | if (!(p_input_mask & (1ULL << i))) { |
| 1496 | continue; // Shader does not need this, skip it (but computing stride was important anyway) |
| 1497 | } |
| 1498 | |
| 1499 | buffers.push_back(buffer); |
| 1500 | offsets.push_back(offset); |
| 1501 | |
| 1502 | if (p_input_motion_vectors) { |
| 1503 | // Push the buffer for motion vector inputs. |
| 1504 | if (i == RS::ARRAY_VERTEX || i == RS::ARRAY_NORMAL || i == RS::ARRAY_TANGENT) { |
| 1505 | if (mis && buffer != mesh_default_rd_buffers[i]) { |
| 1506 | buffers.push_back(mis->vertex_buffer[p_previous_buffer]); |
| 1507 | } else { |
| 1508 | buffers.push_back(buffer); |
| 1509 | } |
| 1510 | |
| 1511 | offsets.push_back(offset); |
| 1512 | } |
| 1513 | } |
| 1514 | } |
| 1515 | |
| 1516 | v.input_mask = p_input_mask; |
| 1517 | v.current_buffer = p_current_buffer; |
| 1518 | v.previous_buffer = p_previous_buffer; |
| 1519 | v.input_motion_vectors = p_input_motion_vectors; |
| 1520 | v.point_size_emulated = p_point_size_emulated; |
| 1521 | v.vertex_array = RD::get_singleton()->vertex_array_create(p_point_size_emulated ? 0 : s->vertex_count, v.vertex_format, buffers, offsets); |
| 1522 | } |
| 1523 | |
| 1524 | ////////////////// MULTIMESH |
| 1525 | |
| 1526 | RID MeshStorage::_multimesh_allocate() { |
| 1527 | return multimesh_owner.allocate_rid(); |
| 1528 | } |
| 1529 | void MeshStorage::_multimesh_initialize(RID p_rid) { |
| 1530 | multimesh_owner.initialize_rid(p_rid, MultiMesh()); |
| 1531 | } |
| 1532 | |
| 1533 | void MeshStorage::_multimesh_free(RID p_rid) { |
| 1534 | // Remove from interpolator. |
| 1535 | _interpolation_data.notify_free_multimesh(p_rid); |
| 1536 | _update_dirty_multimeshes(); |
| 1537 | multimesh_allocate_data(p_rid, 0, RS::MULTIMESH_TRANSFORM_2D); |
| 1538 | MultiMesh *multimesh = multimesh_owner.get_or_null(p_rid); |
| 1539 | multimesh->dependency.deleted_notify(p_rid); |
| 1540 | multimesh_owner.free(p_rid); |
| 1541 | } |
| 1542 | |
| 1543 | void MeshStorage::_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) { |
| 1544 | MultiMesh *multimesh = multimesh_owner.get_or_null(p_multimesh); |
| 1545 | ERR_FAIL_NULL(multimesh); |
| 1546 | |
| 1547 | if (multimesh->instances == p_instances && multimesh->xform_format == p_transform_format && multimesh->uses_colors == p_use_colors && multimesh->uses_custom_data == p_use_custom_data) { |
| 1548 | return; |
| 1549 | } |
| 1550 | |
| 1551 | if (multimesh->buffer.is_valid()) { |
| 1552 | RD::get_singleton()->free_rid(multimesh->buffer); |
| 1553 | multimesh->buffer = RID(); |
| 1554 | multimesh->uniform_set_2d = RID(); //cleared by dependency |
| 1555 | multimesh->uniform_set_3d = RID(); //cleared by dependency |
| 1556 | } |
| 1557 | |
| 1558 | if (multimesh->data_cache_dirty_regions) { |
| 1559 | memdelete_arr(multimesh->data_cache_dirty_regions); |
| 1560 | multimesh->data_cache_dirty_regions = nullptr; |
| 1561 | multimesh->data_cache_dirty_region_count = 0; |
| 1562 | } |
| 1563 | |
| 1564 | if (multimesh->previous_data_cache_dirty_regions) { |
| 1565 | memdelete_arr(multimesh->previous_data_cache_dirty_regions); |
| 1566 | multimesh->previous_data_cache_dirty_regions = nullptr; |
| 1567 | multimesh->previous_data_cache_dirty_region_count = 0; |
| 1568 | } |
| 1569 | |
| 1570 | multimesh->instances = p_instances; |
| 1571 | multimesh->xform_format = p_transform_format; |
| 1572 | multimesh->uses_colors = p_use_colors; |
| 1573 | multimesh->color_offset_cache = p_transform_format == RS::MULTIMESH_TRANSFORM_2D ? 8 : 12; |
| 1574 | multimesh->uses_custom_data = p_use_custom_data; |
| 1575 | multimesh->custom_data_offset_cache = multimesh->color_offset_cache + (p_use_colors ? 4 : 0); |
| 1576 | multimesh->stride_cache = multimesh->custom_data_offset_cache + (p_use_custom_data ? 4 : 0); |
| 1577 | multimesh->buffer_set = false; |
| 1578 | |
| 1579 | multimesh->indirect = p_use_indirect; |
| 1580 | multimesh->command_buffer = RID(); |
| 1581 | |
| 1582 | //print_line("allocate, elements: " + itos(p_instances) + " 2D: " + itos(p_transform_format == RS::MULTIMESH_TRANSFORM_2D) + " colors " + itos(multimesh->uses_colors) + " data " + itos(multimesh->uses_custom_data) + " stride " + itos(multimesh->stride_cache) + " total size " + itos(multimesh->stride_cache * multimesh->instances)); |
| 1583 | multimesh->data_cache = Vector<float>(); |
| 1584 | multimesh->aabb = AABB(); |
| 1585 | multimesh->aabb_dirty = false; |
| 1586 | multimesh->visible_instances = MIN(multimesh->visible_instances, multimesh->instances); |
| 1587 | multimesh->motion_vectors_current_offset = 0; |
| 1588 | multimesh->motion_vectors_previous_offset = 0; |
| 1589 | multimesh->motion_vectors_last_change = -1; |
| 1590 | multimesh->motion_vectors_enabled = false; |
| 1591 | |
| 1592 | if (multimesh->instances) { |
| 1593 | uint32_t buffer_size = multimesh->instances * multimesh->stride_cache * sizeof(float); |
| 1594 | multimesh->buffer = RD::get_singleton()->storage_buffer_create(buffer_size); |
| 1595 | } |
| 1596 | |
| 1597 | multimesh->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_MULTIMESH); |
| 1598 | } |
| 1599 | |
| 1600 | void MeshStorage::_multimesh_enable_motion_vectors(MultiMesh *multimesh) { |
| 1601 | if (multimesh->motion_vectors_enabled) { |
| 1602 | return; |
| 1603 | } |
| 1604 | |
| 1605 | multimesh->motion_vectors_enabled = true; |
| 1606 | |
| 1607 | multimesh->motion_vectors_current_offset = 0; |
| 1608 | multimesh->motion_vectors_previous_offset = 0; |
| 1609 | multimesh->motion_vectors_last_change = -1; |
| 1610 | |
| 1611 | if (!multimesh->data_cache.is_empty()) { |
| 1612 | multimesh->data_cache.append_array(multimesh->data_cache); |
| 1613 | } |
| 1614 | |
| 1615 | uint32_t buffer_size = multimesh->instances * multimesh->stride_cache * sizeof(float); |
| 1616 | uint32_t new_buffer_size = buffer_size * 2; |
| 1617 | RID new_buffer = RD::get_singleton()->storage_buffer_create(new_buffer_size); |
| 1618 | |
| 1619 | if (multimesh->buffer_set && multimesh->data_cache.is_empty()) { |
| 1620 | // If the buffer was set but there's no data cached in the CPU, we copy the buffer directly on the GPU. |
| 1621 | RD::get_singleton()->buffer_copy(multimesh->buffer, new_buffer, 0, 0, buffer_size); |
| 1622 | RD::get_singleton()->buffer_copy(multimesh->buffer, new_buffer, 0, buffer_size, buffer_size); |
| 1623 | } else if (!multimesh->data_cache.is_empty()) { |
| 1624 | // Simply upload the data cached in the CPU, which should already be doubled in size. |
| 1625 | ERR_FAIL_COND(multimesh->data_cache.size() * sizeof(float) != size_t(new_buffer_size)); |
| 1626 | RD::get_singleton()->buffer_update(new_buffer, 0, new_buffer_size, multimesh->data_cache.ptr()); |
| 1627 | } |
| 1628 | |
| 1629 | if (multimesh->buffer.is_valid()) { |
| 1630 | RD::get_singleton()->free_rid(multimesh->buffer); |
| 1631 | } |
| 1632 | |
| 1633 | multimesh->buffer = new_buffer; |
| 1634 | multimesh->uniform_set_3d = RID(); // Cleared by dependency. |
| 1635 | |
| 1636 | // Invalidate any references to the buffer that was released and the uniform set that was pointing to it. |
| 1637 | multimesh->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_MULTIMESH); |
| 1638 | } |
| 1639 | |
| 1640 | void MeshStorage::_multimesh_get_motion_vectors_offsets(RID p_multimesh, uint32_t &r_current_offset, uint32_t &r_prev_offset) { |
| 1641 | MultiMesh *multimesh = multimesh_owner.get_or_null(p_multimesh); |
| 1642 | ERR_FAIL_NULL(multimesh); |
| 1643 | r_current_offset = multimesh->motion_vectors_current_offset; |
| 1644 | if (!_multimesh_uses_motion_vectors(multimesh)) { |
| 1645 | multimesh->motion_vectors_previous_offset = multimesh->motion_vectors_current_offset; |
| 1646 | } |
| 1647 | r_prev_offset = multimesh->motion_vectors_previous_offset; |
| 1648 | } |
| 1649 | |
| 1650 | bool MeshStorage::_multimesh_uses_motion_vectors_offsets(RID p_multimesh) { |
| 1651 | MultiMesh *multimesh = multimesh_owner.get_or_null(p_multimesh); |
| 1652 | ERR_FAIL_NULL_V(multimesh, false); |
| 1653 | return _multimesh_uses_motion_vectors(multimesh); |
| 1654 | } |
| 1655 | |
| 1656 | int MeshStorage::_multimesh_get_instance_count(RID p_multimesh) const { |
| 1657 | MultiMesh *multimesh = multimesh_owner.get_or_null(p_multimesh); |
| 1658 | ERR_FAIL_NULL_V(multimesh, 0); |
| 1659 | return multimesh->instances; |
| 1660 | } |
| 1661 | |
| 1662 | void MeshStorage::_multimesh_set_mesh(RID p_multimesh, RID p_mesh) { |
| 1663 | MultiMesh *multimesh = multimesh_owner.get_or_null(p_multimesh); |
| 1664 | ERR_FAIL_NULL(multimesh); |
| 1665 | if (multimesh->mesh == p_mesh) { |
| 1666 | return; |
| 1667 | } |
| 1668 | multimesh->mesh = p_mesh; |
| 1669 | |
| 1670 | if (multimesh->indirect) { |
| 1671 | Mesh *mesh = mesh_owner.get_or_null(p_mesh); |
| 1672 | ERR_FAIL_NULL(mesh); |
| 1673 | if (mesh->surface_count > 0) { |
| 1674 | if (multimesh->command_buffer.is_valid()) { |
| 1675 | RD::get_singleton()->free_rid(multimesh->command_buffer); |
| 1676 | } |
| 1677 | |
| 1678 | Vector<uint8_t> newVector; |
| 1679 | newVector.resize_initialized(sizeof(uint32_t) * INDIRECT_MULTIMESH_COMMAND_STRIDE * mesh->surface_count); |
| 1680 | |
| 1681 | for (uint32_t i = 0; i < mesh->surface_count; i++) { |
| 1682 | uint32_t count = mesh_surface_get_vertices_drawn_count(mesh->surfaces[i]); |
| 1683 | newVector.set(i * sizeof(uint32_t) * INDIRECT_MULTIMESH_COMMAND_STRIDE, static_cast<uint8_t>(count)); |
| 1684 | newVector.set(i * sizeof(uint32_t) * INDIRECT_MULTIMESH_COMMAND_STRIDE + 1, static_cast<uint8_t>(count >> 8)); |
| 1685 | newVector.set(i * sizeof(uint32_t) * INDIRECT_MULTIMESH_COMMAND_STRIDE + 2, static_cast<uint8_t>(count >> 16)); |
| 1686 | newVector.set(i * sizeof(uint32_t) * INDIRECT_MULTIMESH_COMMAND_STRIDE + 3, static_cast<uint8_t>(count >> 24)); |
| 1687 | } |
| 1688 | |
| 1689 | RID newBuffer = RD::get_singleton()->storage_buffer_create(sizeof(uint32_t) * INDIRECT_MULTIMESH_COMMAND_STRIDE * mesh->surface_count, newVector, RD::STORAGE_BUFFER_USAGE_DISPATCH_INDIRECT); |
| 1690 | multimesh->command_buffer = newBuffer; |
| 1691 | } |
| 1692 | } |
| 1693 | |
| 1694 | if (multimesh->instances == 0) { |
| 1695 | return; |
| 1696 | } |
| 1697 | |
| 1698 | if (multimesh->data_cache.size()) { |
| 1699 | //we have a data cache, just mark it dirt |
| 1700 | _multimesh_mark_all_dirty(multimesh, false, true); |
| 1701 | } else if (multimesh->instances) { |
| 1702 | //need to re-create AABB unfortunately, calling this has a penalty |
| 1703 | if (multimesh->buffer_set) { |
| 1704 | Vector<uint8_t> buffer = RD::get_singleton()->buffer_get_data(multimesh->buffer); |
| 1705 | const uint8_t *r = buffer.ptr() + multimesh->motion_vectors_current_offset * multimesh->stride_cache * sizeof(float); |
| 1706 | const float *data = reinterpret_cast<const float *>(r); |
| 1707 | _multimesh_re_create_aabb(multimesh, data, multimesh->instances); |
| 1708 | } |
| 1709 | } |
| 1710 | |
| 1711 | multimesh->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_MESH); |
| 1712 | } |
| 1713 | |
| 1714 | #define MULTIMESH_DIRTY_REGION_SIZE 512 |
| 1715 | |
| 1716 | void MeshStorage::_multimesh_make_local(MultiMesh *multimesh) const { |
| 1717 | if (multimesh->data_cache.size() > 0) { |
| 1718 | return; //already local |
| 1719 | } |
| 1720 | |
| 1721 | // this means that the user wants to load/save individual elements, |
| 1722 | // for this, the data must reside on CPU, so just copy it there. |
| 1723 | uint32_t buffer_size = multimesh->instances * multimesh->stride_cache; |
| 1724 | if (multimesh->motion_vectors_enabled) { |
| 1725 | buffer_size *= 2; |
| 1726 | } |
| 1727 | multimesh->data_cache.resize(buffer_size); |
| 1728 | { |
| 1729 | float *w = multimesh->data_cache.ptrw(); |
| 1730 | |
| 1731 | if (multimesh->buffer_set) { |
| 1732 | Vector<uint8_t> buffer = RD::get_singleton()->buffer_get_data(multimesh->buffer); |
| 1733 | { |
| 1734 | const uint8_t *r = buffer.ptr(); |
| 1735 | memcpy(w, r, buffer.size()); |
| 1736 | } |
| 1737 | } else { |
| 1738 | memset(w, 0, buffer_size * sizeof(float)); |
| 1739 | } |
| 1740 | } |
| 1741 | uint32_t data_cache_dirty_region_count = Math::division_round_up(multimesh->instances, MULTIMESH_DIRTY_REGION_SIZE); |
| 1742 | multimesh->data_cache_dirty_regions = memnew_arr(bool, data_cache_dirty_region_count); |
| 1743 | memset(multimesh->data_cache_dirty_regions, 0, data_cache_dirty_region_count * sizeof(bool)); |
| 1744 | multimesh->data_cache_dirty_region_count = 0; |
| 1745 | |
| 1746 | multimesh->previous_data_cache_dirty_regions = memnew_arr(bool, data_cache_dirty_region_count); |
| 1747 | memset(multimesh->previous_data_cache_dirty_regions, 0, data_cache_dirty_region_count * sizeof(bool)); |
| 1748 | multimesh->previous_data_cache_dirty_region_count = 0; |
| 1749 | } |
| 1750 | |
| 1751 | void MeshStorage::_multimesh_update_motion_vectors_data_cache(MultiMesh *multimesh) { |
| 1752 | ERR_FAIL_COND(multimesh->data_cache.is_empty()); |
| 1753 | |
| 1754 | if (!multimesh->motion_vectors_enabled) { |
| 1755 | return; |
| 1756 | } |
| 1757 | |
| 1758 | uint32_t frame = RSG::rasterizer->get_frame_number(); |
| 1759 | if (multimesh->motion_vectors_last_change != frame) { |
| 1760 | multimesh->motion_vectors_previous_offset = multimesh->motion_vectors_current_offset; |
| 1761 | multimesh->motion_vectors_current_offset = multimesh->instances - multimesh->motion_vectors_current_offset; |
| 1762 | multimesh->motion_vectors_last_change = frame; |
| 1763 | |
| 1764 | if (multimesh->previous_data_cache_dirty_region_count > 0) { |
| 1765 | uint8_t *data = (uint8_t *)multimesh->data_cache.ptrw(); |
| 1766 | uint32_t current_ofs = multimesh->motion_vectors_current_offset * multimesh->stride_cache * sizeof(float); |
| 1767 | uint32_t previous_ofs = multimesh->motion_vectors_previous_offset * multimesh->stride_cache * sizeof(float); |
| 1768 | uint32_t visible_instances = multimesh->visible_instances >= 0 ? multimesh->visible_instances : multimesh->instances; |
| 1769 | uint32_t visible_region_count = visible_instances == 0 ? 0 : Math::division_round_up(visible_instances, (uint32_t)MULTIMESH_DIRTY_REGION_SIZE); |
| 1770 | uint32_t region_size = multimesh->stride_cache * MULTIMESH_DIRTY_REGION_SIZE * sizeof(float); |
| 1771 | uint32_t size = multimesh->stride_cache * (uint32_t)multimesh->instances * (uint32_t)sizeof(float); |
| 1772 | for (uint32_t i = 0; i < visible_region_count; i++) { |
| 1773 | if (multimesh->previous_data_cache_dirty_regions[i]) { |
| 1774 | uint32_t offset = i * region_size; |
| 1775 | memcpy(data + current_ofs + offset, data + previous_ofs + offset, MIN(region_size, size - offset)); |
| 1776 | } |
| 1777 | } |
| 1778 | } |
| 1779 | } |
| 1780 | } |
| 1781 | |
| 1782 | bool MeshStorage::_multimesh_uses_motion_vectors(MultiMesh *multimesh) { |
| 1783 | return (RSG::rasterizer->get_frame_number() - multimesh->motion_vectors_last_change) < 2; |
| 1784 | } |
| 1785 | |
| 1786 | void MeshStorage::_multimesh_mark_dirty(MultiMesh *multimesh, int p_index, bool p_aabb) { |
| 1787 | uint32_t region_index = p_index / MULTIMESH_DIRTY_REGION_SIZE; |
| 1788 | #ifdef DEBUG_ENABLED |
| 1789 | uint32_t data_cache_dirty_region_count = Math::division_round_up(multimesh->instances, MULTIMESH_DIRTY_REGION_SIZE); |
| 1790 | ERR_FAIL_UNSIGNED_INDEX(region_index, data_cache_dirty_region_count); //bug |
| 1791 | #endif |
| 1792 | if (!multimesh->data_cache_dirty_regions[region_index]) { |
| 1793 | multimesh->data_cache_dirty_regions[region_index] = true; |
| 1794 | multimesh->data_cache_dirty_region_count++; |
| 1795 | } |
| 1796 | |
| 1797 | if (p_aabb) { |
| 1798 | multimesh->aabb_dirty = true; |
| 1799 | } |
| 1800 | |
| 1801 | if (!multimesh->dirty) { |
| 1802 | multimesh->dirty_list = multimesh_dirty_list; |
| 1803 | multimesh_dirty_list = multimesh; |
| 1804 | multimesh->dirty = true; |
| 1805 | } |
| 1806 | } |
| 1807 | |
| 1808 | void MeshStorage::_multimesh_mark_all_dirty(MultiMesh *multimesh, bool p_data, bool p_aabb) { |
| 1809 | if (p_data) { |
| 1810 | uint32_t data_cache_dirty_region_count = Math::division_round_up(multimesh->instances, MULTIMESH_DIRTY_REGION_SIZE); |
| 1811 | |
| 1812 | for (uint32_t i = 0; i < data_cache_dirty_region_count; i++) { |
| 1813 | if (!multimesh->data_cache_dirty_regions[i]) { |
| 1814 | multimesh->data_cache_dirty_regions[i] = true; |
| 1815 | multimesh->data_cache_dirty_region_count++; |
| 1816 | } |
| 1817 | } |
| 1818 | } |
| 1819 | |
| 1820 | if (p_aabb) { |
| 1821 | multimesh->aabb_dirty = true; |
| 1822 | } |
| 1823 | |
| 1824 | if (!multimesh->dirty) { |
| 1825 | multimesh->dirty_list = multimesh_dirty_list; |
| 1826 | multimesh_dirty_list = multimesh; |
| 1827 | multimesh->dirty = true; |
| 1828 | } |
| 1829 | } |
| 1830 | |
| 1831 | void MeshStorage::_multimesh_re_create_aabb(MultiMesh *multimesh, const float *p_data, int p_instances) { |
| 1832 | ERR_FAIL_COND(multimesh->mesh.is_null()); |
| 1833 | if (multimesh->custom_aabb != AABB()) { |
| 1834 | return; |
| 1835 | } |
| 1836 | AABB aabb; |
| 1837 | AABB mesh_aabb = mesh_get_aabb(multimesh->mesh); |
| 1838 | for (int i = 0; i < p_instances; i++) { |
| 1839 | const float *data = p_data + multimesh->stride_cache * i; |
| 1840 | Transform3D t; |
| 1841 | |
| 1842 | if (multimesh->xform_format == RS::MULTIMESH_TRANSFORM_3D) { |
| 1843 | t.basis.rows[0][0] = data[0]; |
| 1844 | t.basis.rows[0][1] = data[1]; |
| 1845 | t.basis.rows[0][2] = data[2]; |
| 1846 | t.origin.x = data[3]; |
| 1847 | t.basis.rows[1][0] = data[4]; |
| 1848 | t.basis.rows[1][1] = data[5]; |
| 1849 | t.basis.rows[1][2] = data[6]; |
| 1850 | t.origin.y = data[7]; |
| 1851 | t.basis.rows[2][0] = data[8]; |
| 1852 | t.basis.rows[2][1] = data[9]; |
| 1853 | t.basis.rows[2][2] = data[10]; |
| 1854 | t.origin.z = data[11]; |
| 1855 | |
| 1856 | } else { |
| 1857 | t.basis.rows[0][0] = data[0]; |
| 1858 | t.basis.rows[0][1] = data[1]; |
| 1859 | t.origin.x = data[3]; |
| 1860 | |
| 1861 | t.basis.rows[1][0] = data[4]; |
| 1862 | t.basis.rows[1][1] = data[5]; |
| 1863 | t.origin.y = data[7]; |
| 1864 | } |
| 1865 | |
| 1866 | if (i == 0) { |
| 1867 | aabb = t.xform(mesh_aabb); |
| 1868 | } else { |
| 1869 | aabb.merge_with(t.xform(mesh_aabb)); |
| 1870 | } |
| 1871 | } |
| 1872 | |
| 1873 | multimesh->aabb = aabb; |
| 1874 | } |
| 1875 | |
| 1876 | void MeshStorage::_multimesh_instance_set_transform(RID p_multimesh, int p_index, const Transform3D &p_transform) { |
| 1877 | MultiMesh *multimesh = multimesh_owner.get_or_null(p_multimesh); |
| 1878 | ERR_FAIL_NULL(multimesh); |
| 1879 | ERR_FAIL_INDEX(p_index, multimesh->instances); |
| 1880 | ERR_FAIL_COND(multimesh->xform_format != RS::MULTIMESH_TRANSFORM_3D); |
| 1881 | |
| 1882 | _multimesh_make_local(multimesh); |
| 1883 | |
| 1884 | bool uses_motion_vectors = (RSG::viewport->get_num_viewports_with_motion_vectors() > 0) || (RendererCompositorStorage::get_singleton()->get_num_compositor_effects_with_motion_vectors() > 0); |
| 1885 | if (uses_motion_vectors) { |
| 1886 | _multimesh_enable_motion_vectors(multimesh); |
| 1887 | } |
| 1888 | |
| 1889 | _multimesh_update_motion_vectors_data_cache(multimesh); |
| 1890 | |
| 1891 | { |
| 1892 | float *w = multimesh->data_cache.ptrw(); |
| 1893 | |
| 1894 | float *dataptr = w + (multimesh->motion_vectors_current_offset + p_index) * multimesh->stride_cache; |
| 1895 | |
| 1896 | dataptr[0] = p_transform.basis.rows[0][0]; |
| 1897 | dataptr[1] = p_transform.basis.rows[0][1]; |
| 1898 | dataptr[2] = p_transform.basis.rows[0][2]; |
| 1899 | dataptr[3] = p_transform.origin.x; |
| 1900 | dataptr[4] = p_transform.basis.rows[1][0]; |
| 1901 | dataptr[5] = p_transform.basis.rows[1][1]; |
| 1902 | dataptr[6] = p_transform.basis.rows[1][2]; |
| 1903 | dataptr[7] = p_transform.origin.y; |
| 1904 | dataptr[8] = p_transform.basis.rows[2][0]; |
| 1905 | dataptr[9] = p_transform.basis.rows[2][1]; |
| 1906 | dataptr[10] = p_transform.basis.rows[2][2]; |
| 1907 | dataptr[11] = p_transform.origin.z; |
| 1908 | } |
| 1909 | |
| 1910 | _multimesh_mark_dirty(multimesh, p_index, true); |
| 1911 | } |
| 1912 | |
| 1913 | void MeshStorage::_multimesh_instance_set_transform_2d(RID p_multimesh, int p_index, const Transform2D &p_transform) { |
| 1914 | MultiMesh *multimesh = multimesh_owner.get_or_null(p_multimesh); |
| 1915 | ERR_FAIL_NULL(multimesh); |
| 1916 | ERR_FAIL_INDEX(p_index, multimesh->instances); |
| 1917 | ERR_FAIL_COND(multimesh->xform_format != RS::MULTIMESH_TRANSFORM_2D); |
| 1918 | |
| 1919 | _multimesh_make_local(multimesh); |
| 1920 | _multimesh_update_motion_vectors_data_cache(multimesh); |
| 1921 | |
| 1922 | { |
| 1923 | float *w = multimesh->data_cache.ptrw(); |
| 1924 | |
| 1925 | float *dataptr = w + (multimesh->motion_vectors_current_offset + p_index) * multimesh->stride_cache; |
| 1926 | |
| 1927 | dataptr[0] = p_transform.columns[0][0]; |
| 1928 | dataptr[1] = p_transform.columns[1][0]; |
| 1929 | dataptr[2] = 0; |
| 1930 | dataptr[3] = p_transform.columns[2][0]; |
| 1931 | dataptr[4] = p_transform.columns[0][1]; |
| 1932 | dataptr[5] = p_transform.columns[1][1]; |
| 1933 | dataptr[6] = 0; |
| 1934 | dataptr[7] = p_transform.columns[2][1]; |
| 1935 | } |
| 1936 | |
| 1937 | _multimesh_mark_dirty(multimesh, p_index, true); |
| 1938 | } |
| 1939 | |
| 1940 | void MeshStorage::_multimesh_instance_set_color(RID p_multimesh, int p_index, const Color &p_color) { |
| 1941 | MultiMesh *multimesh = multimesh_owner.get_or_null(p_multimesh); |
| 1942 | ERR_FAIL_NULL(multimesh); |
| 1943 | ERR_FAIL_INDEX(p_index, multimesh->instances); |
| 1944 | ERR_FAIL_COND(!multimesh->uses_colors); |
| 1945 | |
| 1946 | _multimesh_make_local(multimesh); |
| 1947 | _multimesh_update_motion_vectors_data_cache(multimesh); |
| 1948 | |
| 1949 | { |
| 1950 | float *w = multimesh->data_cache.ptrw(); |
| 1951 | |
| 1952 | float *dataptr = w + (multimesh->motion_vectors_current_offset + p_index) * multimesh->stride_cache + multimesh->color_offset_cache; |
| 1953 | |
| 1954 | dataptr[0] = p_color.r; |
| 1955 | dataptr[1] = p_color.g; |
| 1956 | dataptr[2] = p_color.b; |
| 1957 | dataptr[3] = p_color.a; |
| 1958 | } |
| 1959 | |
| 1960 | _multimesh_mark_dirty(multimesh, p_index, false); |
| 1961 | } |
| 1962 | |
| 1963 | void MeshStorage::_multimesh_instance_set_custom_data(RID p_multimesh, int p_index, const Color &p_color) { |
| 1964 | MultiMesh *multimesh = multimesh_owner.get_or_null(p_multimesh); |
| 1965 | ERR_FAIL_NULL(multimesh); |
| 1966 | ERR_FAIL_INDEX(p_index, multimesh->instances); |
| 1967 | ERR_FAIL_COND(!multimesh->uses_custom_data); |
| 1968 | |
| 1969 | _multimesh_make_local(multimesh); |
| 1970 | _multimesh_update_motion_vectors_data_cache(multimesh); |
| 1971 | |
| 1972 | { |
| 1973 | float *w = multimesh->data_cache.ptrw(); |
| 1974 | |
| 1975 | float *dataptr = w + (multimesh->motion_vectors_current_offset + p_index) * multimesh->stride_cache + multimesh->custom_data_offset_cache; |
| 1976 | |
| 1977 | dataptr[0] = p_color.r; |
| 1978 | dataptr[1] = p_color.g; |
| 1979 | dataptr[2] = p_color.b; |
| 1980 | dataptr[3] = p_color.a; |
| 1981 | } |
| 1982 | |
| 1983 | _multimesh_mark_dirty(multimesh, p_index, false); |
| 1984 | } |
| 1985 | |
| 1986 | RID MeshStorage::_multimesh_get_mesh(RID p_multimesh) const { |
| 1987 | MultiMesh *multimesh = multimesh_owner.get_or_null(p_multimesh); |
| 1988 | ERR_FAIL_NULL_V(multimesh, RID()); |
| 1989 | |
| 1990 | return multimesh->mesh; |
| 1991 | } |
| 1992 | |
| 1993 | Dependency *MeshStorage::multimesh_get_dependency(RID p_multimesh) const { |
| 1994 | MultiMesh *multimesh = multimesh_owner.get_or_null(p_multimesh); |
| 1995 | ERR_FAIL_NULL_V(multimesh, nullptr); |
| 1996 | |
| 1997 | return &multimesh->dependency; |
| 1998 | } |
| 1999 | |
| 2000 | Transform3D MeshStorage::_multimesh_instance_get_transform(RID p_multimesh, int p_index) const { |
| 2001 | MultiMesh *multimesh = multimesh_owner.get_or_null(p_multimesh); |
| 2002 | ERR_FAIL_NULL_V(multimesh, Transform3D()); |
| 2003 | ERR_FAIL_INDEX_V(p_index, multimesh->instances, Transform3D()); |
| 2004 | ERR_FAIL_COND_V(multimesh->xform_format != RS::MULTIMESH_TRANSFORM_3D, Transform3D()); |
| 2005 | |
| 2006 | _multimesh_make_local(multimesh); |
| 2007 | |
| 2008 | Transform3D t; |
| 2009 | { |
| 2010 | const float *r = multimesh->data_cache.ptr(); |
| 2011 | |
| 2012 | const float *dataptr = r + (multimesh->motion_vectors_current_offset + p_index) * multimesh->stride_cache; |
| 2013 | |
| 2014 | t.basis.rows[0][0] = dataptr[0]; |
| 2015 | t.basis.rows[0][1] = dataptr[1]; |
| 2016 | t.basis.rows[0][2] = dataptr[2]; |
| 2017 | t.origin.x = dataptr[3]; |
| 2018 | t.basis.rows[1][0] = dataptr[4]; |
| 2019 | t.basis.rows[1][1] = dataptr[5]; |
| 2020 | t.basis.rows[1][2] = dataptr[6]; |
| 2021 | t.origin.y = dataptr[7]; |
| 2022 | t.basis.rows[2][0] = dataptr[8]; |
| 2023 | t.basis.rows[2][1] = dataptr[9]; |
| 2024 | t.basis.rows[2][2] = dataptr[10]; |
| 2025 | t.origin.z = dataptr[11]; |
| 2026 | } |
| 2027 | |
| 2028 | return t; |
| 2029 | } |
| 2030 | |
| 2031 | Transform2D MeshStorage::_multimesh_instance_get_transform_2d(RID p_multimesh, int p_index) const { |
| 2032 | MultiMesh *multimesh = multimesh_owner.get_or_null(p_multimesh); |
| 2033 | ERR_FAIL_NULL_V(multimesh, Transform2D()); |
| 2034 | ERR_FAIL_INDEX_V(p_index, multimesh->instances, Transform2D()); |
| 2035 | ERR_FAIL_COND_V(multimesh->xform_format != RS::MULTIMESH_TRANSFORM_2D, Transform2D()); |
| 2036 | |
| 2037 | _multimesh_make_local(multimesh); |
| 2038 | |
| 2039 | Transform2D t; |
| 2040 | { |
| 2041 | const float *r = multimesh->data_cache.ptr(); |
| 2042 | |
| 2043 | const float *dataptr = r + (multimesh->motion_vectors_current_offset + p_index) * multimesh->stride_cache; |
| 2044 | |
| 2045 | t.columns[0][0] = dataptr[0]; |
| 2046 | t.columns[1][0] = dataptr[1]; |
| 2047 | t.columns[2][0] = dataptr[3]; |
| 2048 | t.columns[0][1] = dataptr[4]; |
| 2049 | t.columns[1][1] = dataptr[5]; |
| 2050 | t.columns[2][1] = dataptr[7]; |
| 2051 | } |
| 2052 | |
| 2053 | return t; |
| 2054 | } |
| 2055 | |
| 2056 | Color MeshStorage::_multimesh_instance_get_color(RID p_multimesh, int p_index) const { |
| 2057 | MultiMesh *multimesh = multimesh_owner.get_or_null(p_multimesh); |
| 2058 | ERR_FAIL_NULL_V(multimesh, Color()); |
| 2059 | ERR_FAIL_INDEX_V(p_index, multimesh->instances, Color()); |
| 2060 | ERR_FAIL_COND_V(!multimesh->uses_colors, Color()); |
| 2061 | |
| 2062 | _multimesh_make_local(multimesh); |
| 2063 | |
| 2064 | Color c; |
| 2065 | { |
| 2066 | const float *r = multimesh->data_cache.ptr(); |
| 2067 | |
| 2068 | const float *dataptr = r + (multimesh->motion_vectors_current_offset + p_index) * multimesh->stride_cache + multimesh->color_offset_cache; |
| 2069 | |
| 2070 | c.r = dataptr[0]; |
| 2071 | c.g = dataptr[1]; |
| 2072 | c.b = dataptr[2]; |
| 2073 | c.a = dataptr[3]; |
| 2074 | } |
| 2075 | |
| 2076 | return c; |
| 2077 | } |
| 2078 | |
| 2079 | Color MeshStorage::_multimesh_instance_get_custom_data(RID p_multimesh, int p_index) const { |
| 2080 | MultiMesh *multimesh = multimesh_owner.get_or_null(p_multimesh); |
| 2081 | ERR_FAIL_NULL_V(multimesh, Color()); |
| 2082 | ERR_FAIL_INDEX_V(p_index, multimesh->instances, Color()); |
| 2083 | ERR_FAIL_COND_V(!multimesh->uses_custom_data, Color()); |
| 2084 | |
| 2085 | _multimesh_make_local(multimesh); |
| 2086 | |
| 2087 | Color c; |
| 2088 | { |
| 2089 | const float *r = multimesh->data_cache.ptr(); |
| 2090 | |
| 2091 | const float *dataptr = r + (multimesh->motion_vectors_current_offset + p_index) * multimesh->stride_cache + multimesh->custom_data_offset_cache; |
| 2092 | |
| 2093 | c.r = dataptr[0]; |
| 2094 | c.g = dataptr[1]; |
| 2095 | c.b = dataptr[2]; |
| 2096 | c.a = dataptr[3]; |
| 2097 | } |
| 2098 | |
| 2099 | return c; |
| 2100 | } |
| 2101 | |
| 2102 | void MeshStorage::_multimesh_set_buffer(RID p_multimesh, const Vector<float> &p_buffer) { |
| 2103 | MultiMesh *multimesh = multimesh_owner.get_or_null(p_multimesh); |
| 2104 | ERR_FAIL_NULL(multimesh); |
| 2105 | ERR_FAIL_COND(p_buffer.size() != (multimesh->instances * (int)multimesh->stride_cache)); |
| 2106 | |
| 2107 | bool used_motion_vectors = multimesh->motion_vectors_enabled; |
| 2108 | bool uses_motion_vectors = (RSG::viewport->get_num_viewports_with_motion_vectors() > 0) || (RendererCompositorStorage::get_singleton()->get_num_compositor_effects_with_motion_vectors() > 0); |
| 2109 | if (uses_motion_vectors) { |
| 2110 | _multimesh_enable_motion_vectors(multimesh); |
| 2111 | } |
| 2112 | |
| 2113 | if (multimesh->motion_vectors_enabled) { |
| 2114 | uint32_t frame = RSG::rasterizer->get_frame_number(); |
| 2115 | |
| 2116 | if (multimesh->motion_vectors_last_change != frame) { |
| 2117 | multimesh->motion_vectors_previous_offset = multimesh->motion_vectors_current_offset; |
| 2118 | multimesh->motion_vectors_current_offset = multimesh->instances - multimesh->motion_vectors_current_offset; |
| 2119 | multimesh->motion_vectors_last_change = frame; |
| 2120 | } |
| 2121 | } |
| 2122 | |
| 2123 | { |
| 2124 | const float *r = p_buffer.ptr(); |
| 2125 | RD::get_singleton()->buffer_update(multimesh->buffer, multimesh->motion_vectors_current_offset * multimesh->stride_cache * sizeof(float), p_buffer.size() * sizeof(float), r); |
| 2126 | if (multimesh->motion_vectors_enabled && !used_motion_vectors) { |
| 2127 | // Motion vectors were just enabled, and the other half of the buffer will be empty. |
| 2128 | // Need to ensure that both halves are filled for correct operation. |
| 2129 | RD::get_singleton()->buffer_update(multimesh->buffer, multimesh->motion_vectors_previous_offset * multimesh->stride_cache * sizeof(float), p_buffer.size() * sizeof(float), r); |
| 2130 | } |
| 2131 | multimesh->buffer_set = true; |
| 2132 | } |
| 2133 | |
| 2134 | if (multimesh->data_cache.size()) { |
| 2135 | float *cache_data = multimesh->data_cache.ptrw(); |
| 2136 | memcpy(cache_data + (multimesh->motion_vectors_current_offset * multimesh->stride_cache), p_buffer.ptr(), p_buffer.size() * sizeof(float)); |
| 2137 | _multimesh_mark_all_dirty(multimesh, true, true); //update AABB |
| 2138 | } else if (multimesh->mesh.is_valid()) { |
| 2139 | //if we have a mesh set, we need to re-generate the AABB from the new data |
| 2140 | const float *data = p_buffer.ptr(); |
| 2141 | |
| 2142 | if (multimesh->custom_aabb == AABB()) { |
| 2143 | _multimesh_re_create_aabb(multimesh, data, multimesh->instances); |
| 2144 | multimesh->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_AABB); |
| 2145 | } |
| 2146 | } |
| 2147 | } |
| 2148 | |
| 2149 | RID MeshStorage::_multimesh_get_command_buffer_rd_rid(RID p_multimesh) const { |
| 2150 | MultiMesh *multimesh = multimesh_owner.get_or_null(p_multimesh); |
| 2151 | ERR_FAIL_NULL_V(multimesh, RID()); |
| 2152 | return multimesh->command_buffer; |
| 2153 | } |
| 2154 | |
| 2155 | RID MeshStorage::_multimesh_get_buffer_rd_rid(RID p_multimesh) const { |
| 2156 | MultiMesh *multimesh = multimesh_owner.get_or_null(p_multimesh); |
| 2157 | ERR_FAIL_NULL_V(multimesh, RID()); |
| 2158 | return multimesh->buffer; |
| 2159 | } |
| 2160 | |
| 2161 | Vector<float> MeshStorage::_multimesh_get_buffer(RID p_multimesh) const { |
| 2162 | MultiMesh *multimesh = multimesh_owner.get_or_null(p_multimesh); |
| 2163 | ERR_FAIL_NULL_V(multimesh, Vector<float>()); |
| 2164 | if (multimesh->buffer.is_null()) { |
| 2165 | return Vector<float>(); |
| 2166 | } else { |
| 2167 | Vector<float> ret; |
| 2168 | ret.resize(multimesh->instances * multimesh->stride_cache); |
| 2169 | float *w = ret.ptrw(); |
| 2170 | |
| 2171 | if (multimesh->data_cache.size()) { |
| 2172 | const uint8_t *r = (uint8_t *)multimesh->data_cache.ptr() + multimesh->motion_vectors_current_offset * multimesh->stride_cache * sizeof(float); |
| 2173 | memcpy(w, r, ret.size() * sizeof(float)); |
| 2174 | } else { |
| 2175 | Vector<uint8_t> buffer = RD::get_singleton()->buffer_get_data(multimesh->buffer); |
| 2176 | const uint8_t *r = buffer.ptr() + multimesh->motion_vectors_current_offset * multimesh->stride_cache * sizeof(float); |
| 2177 | memcpy(w, r, ret.size() * sizeof(float)); |
| 2178 | } |
| 2179 | return ret; |
| 2180 | } |
| 2181 | } |
| 2182 | |
| 2183 | void MeshStorage::_multimesh_set_visible_instances(RID p_multimesh, int p_visible) { |
| 2184 | MultiMesh *multimesh = multimesh_owner.get_or_null(p_multimesh); |
| 2185 | ERR_FAIL_NULL(multimesh); |
| 2186 | ERR_FAIL_COND(p_visible < -1 || p_visible > multimesh->instances); |
| 2187 | if (multimesh->visible_instances == p_visible) { |
| 2188 | return; |
| 2189 | } |
| 2190 | |
| 2191 | if (multimesh->data_cache.size()) { |
| 2192 | // There is a data cache, but we may need to update some sections. |
| 2193 | _multimesh_mark_all_dirty(multimesh, false, true); |
| 2194 | int start = multimesh->visible_instances >= 0 ? multimesh->visible_instances : multimesh->instances; |
| 2195 | for (int i = start; i < p_visible; i++) { |
| 2196 | _multimesh_mark_dirty(multimesh, i, true); |
| 2197 | } |
| 2198 | } |
| 2199 | |
| 2200 | multimesh->visible_instances = p_visible; |
| 2201 | |
| 2202 | if (multimesh->indirect) { //we have to update the command buffer for the instance counts, in each stride this will be the second integer. |
| 2203 | Mesh *mesh = mesh_owner.get_or_null(multimesh->mesh); |
| 2204 | if (mesh != nullptr) { |
| 2205 | for (uint32_t i = 0; i < mesh->surface_count; i++) { |
| 2206 | RD::get_singleton()->buffer_update(multimesh->command_buffer, (i * sizeof(uint32_t) * INDIRECT_MULTIMESH_COMMAND_STRIDE) + sizeof(uint32_t), sizeof(uint32_t), &p_visible); |
| 2207 | } |
| 2208 | } |
| 2209 | } |
| 2210 | |
| 2211 | multimesh->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_MULTIMESH_VISIBLE_INSTANCES); |
| 2212 | } |
| 2213 | |
| 2214 | int MeshStorage::_multimesh_get_visible_instances(RID p_multimesh) const { |
| 2215 | MultiMesh *multimesh = multimesh_owner.get_or_null(p_multimesh); |
| 2216 | ERR_FAIL_NULL_V(multimesh, 0); |
| 2217 | return multimesh->visible_instances; |
| 2218 | } |
| 2219 | |
| 2220 | void MeshStorage::_multimesh_set_custom_aabb(RID p_multimesh, const AABB &p_aabb) { |
| 2221 | MultiMesh *multimesh = multimesh_owner.get_or_null(p_multimesh); |
| 2222 | ERR_FAIL_NULL(multimesh); |
| 2223 | multimesh->custom_aabb = p_aabb; |
| 2224 | multimesh->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_AABB); |
| 2225 | } |
| 2226 | |
| 2227 | AABB MeshStorage::_multimesh_get_custom_aabb(RID p_multimesh) const { |
| 2228 | MultiMesh *multimesh = multimesh_owner.get_or_null(p_multimesh); |
| 2229 | ERR_FAIL_NULL_V(multimesh, AABB()); |
| 2230 | return multimesh->custom_aabb; |
| 2231 | } |
| 2232 | |
| 2233 | AABB MeshStorage::_multimesh_get_aabb(RID p_multimesh) { |
| 2234 | MultiMesh *multimesh = multimesh_owner.get_or_null(p_multimesh); |
| 2235 | ERR_FAIL_NULL_V(multimesh, AABB()); |
| 2236 | if (multimesh->custom_aabb != AABB()) { |
| 2237 | return multimesh->custom_aabb; |
| 2238 | } |
| 2239 | |
| 2240 | if (multimesh->aabb_dirty) { |
| 2241 | _update_dirty_multimeshes(); |
| 2242 | } |
| 2243 | return multimesh->aabb; |
| 2244 | } |
| 2245 | |
| 2246 | MeshStorage::MultiMeshInterpolator *MeshStorage::_multimesh_get_interpolator(RID p_multimesh) const { |
| 2247 | MultiMesh *multimesh = multimesh_owner.get_or_null(p_multimesh); |
| 2248 | ERR_FAIL_NULL_V_MSG(multimesh, nullptr, "Multimesh not found: " + itos(p_multimesh.get_id())); |
| 2249 | |
| 2250 | return &multimesh->interpolator; |
| 2251 | } |
| 2252 | |
| 2253 | void MeshStorage::_update_dirty_multimeshes() { |
| 2254 | while (multimesh_dirty_list) { |
| 2255 | MultiMesh *multimesh = multimesh_dirty_list; |
| 2256 | |
| 2257 | if (multimesh->data_cache.size()) { //may have been cleared, so only process if it exists |
| 2258 | |
| 2259 | uint32_t visible_instances = multimesh->visible_instances >= 0 ? multimesh->visible_instances : multimesh->instances; |
| 2260 | uint32_t buffer_offset = multimesh->motion_vectors_current_offset * multimesh->stride_cache; |
| 2261 | const float *data = multimesh->data_cache.ptr() + buffer_offset; |
| 2262 | |
| 2263 | uint32_t total_dirty_regions = multimesh->data_cache_dirty_region_count + multimesh->previous_data_cache_dirty_region_count; |
| 2264 | if (total_dirty_regions != 0) { |
| 2265 | uint32_t data_cache_dirty_region_count = Math::division_round_up(multimesh->instances, (int)MULTIMESH_DIRTY_REGION_SIZE); |
| 2266 | uint32_t visible_region_count = visible_instances == 0 ? 0 : Math::division_round_up(visible_instances, (uint32_t)MULTIMESH_DIRTY_REGION_SIZE); |
| 2267 | |
| 2268 | uint32_t region_size = multimesh->stride_cache * MULTIMESH_DIRTY_REGION_SIZE * sizeof(float); |
| 2269 | if (total_dirty_regions > 32 || total_dirty_regions > visible_region_count / 2) { |
| 2270 | //if there too many dirty regions, or represent the majority of regions, just copy all, else transfer cost piles up too much |
| 2271 | RD::get_singleton()->buffer_update(multimesh->buffer, buffer_offset * sizeof(float), MIN(visible_region_count * region_size, multimesh->instances * (uint32_t)multimesh->stride_cache * (uint32_t)sizeof(float)), data); |
| 2272 | } else { |
| 2273 | //not that many regions? update them all |
| 2274 | for (uint32_t i = 0; i < visible_region_count; i++) { |
| 2275 | if (multimesh->data_cache_dirty_regions[i] || multimesh->previous_data_cache_dirty_regions[i]) { |
| 2276 | uint32_t offset = i * region_size; |
| 2277 | uint32_t size = multimesh->stride_cache * (uint32_t)multimesh->instances * (uint32_t)sizeof(float); |
| 2278 | uint32_t region_start_index = multimesh->stride_cache * MULTIMESH_DIRTY_REGION_SIZE * i; |
| 2279 | RD::get_singleton()->buffer_update(multimesh->buffer, buffer_offset * sizeof(float) + offset, MIN(region_size, size - offset), &data[region_start_index]); |
| 2280 | } |
| 2281 | } |
| 2282 | } |
| 2283 | |
| 2284 | memcpy(multimesh->previous_data_cache_dirty_regions, multimesh->data_cache_dirty_regions, data_cache_dirty_region_count * sizeof(bool)); |
| 2285 | memset(multimesh->data_cache_dirty_regions, 0, data_cache_dirty_region_count * sizeof(bool)); |
| 2286 | |
| 2287 | multimesh->previous_data_cache_dirty_region_count = multimesh->data_cache_dirty_region_count; |
| 2288 | multimesh->data_cache_dirty_region_count = 0; |
| 2289 | } |
| 2290 | |
| 2291 | if (multimesh->aabb_dirty) { |
| 2292 | //aabb is dirty.. |
| 2293 | multimesh->aabb_dirty = false; |
| 2294 | if (multimesh->custom_aabb == AABB()) { |
| 2295 | _multimesh_re_create_aabb(multimesh, data, visible_instances); |
| 2296 | multimesh->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_AABB); |
| 2297 | } |
| 2298 | } |
| 2299 | } |
| 2300 | |
| 2301 | multimesh_dirty_list = multimesh->dirty_list; |
| 2302 | |
| 2303 | multimesh->dirty_list = nullptr; |
| 2304 | multimesh->dirty = false; |
| 2305 | } |
| 2306 | |
| 2307 | multimesh_dirty_list = nullptr; |
| 2308 | } |
| 2309 | |
| 2310 | /* SKELETON API */ |
| 2311 | |
| 2312 | RID MeshStorage::skeleton_allocate() { |
| 2313 | return skeleton_owner.allocate_rid(); |
| 2314 | } |
| 2315 | void MeshStorage::skeleton_initialize(RID p_rid) { |
| 2316 | skeleton_owner.initialize_rid(p_rid, Skeleton()); |
| 2317 | } |
| 2318 | |
| 2319 | void MeshStorage::skeleton_free(RID p_rid) { |
| 2320 | _update_dirty_skeletons(); |
| 2321 | skeleton_allocate_data(p_rid, 0); |
| 2322 | Skeleton *skeleton = skeleton_owner.get_or_null(p_rid); |
| 2323 | skeleton->dependency.deleted_notify(p_rid); |
| 2324 | skeleton_owner.free(p_rid); |
| 2325 | } |
| 2326 | |
| 2327 | void MeshStorage::_skeleton_make_dirty(Skeleton *skeleton) { |
| 2328 | if (!skeleton->dirty) { |
| 2329 | skeleton->dirty = true; |
| 2330 | skeleton->dirty_list = skeleton_dirty_list; |
| 2331 | skeleton_dirty_list = skeleton; |
| 2332 | } |
| 2333 | } |
| 2334 | |
| 2335 | void MeshStorage::skeleton_allocate_data(RID p_skeleton, int p_bones, bool p_2d_skeleton) { |
| 2336 | Skeleton *skeleton = skeleton_owner.get_or_null(p_skeleton); |
| 2337 | ERR_FAIL_NULL(skeleton); |
| 2338 | ERR_FAIL_COND(p_bones < 0); |
| 2339 | |
| 2340 | if (skeleton->size == p_bones && skeleton->use_2d == p_2d_skeleton) { |
| 2341 | return; |
| 2342 | } |
| 2343 | |
| 2344 | skeleton->size = p_bones; |
| 2345 | skeleton->use_2d = p_2d_skeleton; |
| 2346 | skeleton->uniform_set_3d = RID(); |
| 2347 | |
| 2348 | if (skeleton->buffer.is_valid()) { |
| 2349 | RD::get_singleton()->free_rid(skeleton->buffer); |
| 2350 | skeleton->buffer = RID(); |
| 2351 | skeleton->data.clear(); |
| 2352 | skeleton->uniform_set_mi = RID(); |
| 2353 | } |
| 2354 | |
| 2355 | if (skeleton->size) { |
| 2356 | skeleton->data.resize(skeleton->size * (skeleton->use_2d ? 8 : 12)); |
| 2357 | skeleton->buffer = RD::get_singleton()->storage_buffer_create(skeleton->data.size() * sizeof(float)); |
| 2358 | memset(skeleton->data.ptr(), 0, skeleton->data.size() * sizeof(float)); |
| 2359 | |
| 2360 | _skeleton_make_dirty(skeleton); |
| 2361 | |
| 2362 | { |
| 2363 | Vector<RD::Uniform> uniforms; |
| 2364 | { |
| 2365 | RD::Uniform u; |
| 2366 | u.binding = 0; |
| 2367 | u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER; |
| 2368 | u.append_id(skeleton->buffer); |
| 2369 | uniforms.push_back(u); |
| 2370 | } |
| 2371 | skeleton->uniform_set_mi = RD::get_singleton()->uniform_set_create(uniforms, skeleton_shader.version_shader[0], SkeletonShader::UNIFORM_SET_SKELETON); |
| 2372 | } |
| 2373 | } |
| 2374 | |
| 2375 | skeleton->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_SKELETON_DATA); |
| 2376 | } |
| 2377 | |
| 2378 | int MeshStorage::skeleton_get_bone_count(RID p_skeleton) const { |
| 2379 | Skeleton *skeleton = skeleton_owner.get_or_null(p_skeleton); |
| 2380 | ERR_FAIL_NULL_V(skeleton, 0); |
| 2381 | |
| 2382 | return skeleton->size; |
| 2383 | } |
| 2384 | |
| 2385 | void MeshStorage::skeleton_bone_set_transform(RID p_skeleton, int p_bone, const Transform3D &p_transform) { |
| 2386 | Skeleton *skeleton = skeleton_owner.get_or_null(p_skeleton); |
| 2387 | |
| 2388 | ERR_FAIL_NULL(skeleton); |
| 2389 | ERR_FAIL_INDEX(p_bone, skeleton->size); |
| 2390 | ERR_FAIL_COND(skeleton->use_2d); |
| 2391 | |
| 2392 | float *dataptr = skeleton->data.ptr() + p_bone * 12; |
| 2393 | |
| 2394 | dataptr[0] = p_transform.basis.rows[0][0]; |
| 2395 | dataptr[1] = p_transform.basis.rows[0][1]; |
| 2396 | dataptr[2] = p_transform.basis.rows[0][2]; |
| 2397 | dataptr[3] = p_transform.origin.x; |
| 2398 | dataptr[4] = p_transform.basis.rows[1][0]; |
| 2399 | dataptr[5] = p_transform.basis.rows[1][1]; |
| 2400 | dataptr[6] = p_transform.basis.rows[1][2]; |
| 2401 | dataptr[7] = p_transform.origin.y; |
| 2402 | dataptr[8] = p_transform.basis.rows[2][0]; |
| 2403 | dataptr[9] = p_transform.basis.rows[2][1]; |
| 2404 | dataptr[10] = p_transform.basis.rows[2][2]; |
| 2405 | dataptr[11] = p_transform.origin.z; |
| 2406 | |
| 2407 | _skeleton_make_dirty(skeleton); |
| 2408 | } |
| 2409 | |
| 2410 | Transform3D MeshStorage::skeleton_bone_get_transform(RID p_skeleton, int p_bone) const { |
| 2411 | Skeleton *skeleton = skeleton_owner.get_or_null(p_skeleton); |
| 2412 | |
| 2413 | ERR_FAIL_NULL_V(skeleton, Transform3D()); |
| 2414 | ERR_FAIL_INDEX_V(p_bone, skeleton->size, Transform3D()); |
| 2415 | ERR_FAIL_COND_V(skeleton->use_2d, Transform3D()); |
| 2416 | |
| 2417 | const float *dataptr = skeleton->data.ptr() + p_bone * 12; |
| 2418 | |
| 2419 | Transform3D t; |
| 2420 | |
| 2421 | t.basis.rows[0][0] = dataptr[0]; |
| 2422 | t.basis.rows[0][1] = dataptr[1]; |
| 2423 | t.basis.rows[0][2] = dataptr[2]; |
| 2424 | t.origin.x = dataptr[3]; |
| 2425 | t.basis.rows[1][0] = dataptr[4]; |
| 2426 | t.basis.rows[1][1] = dataptr[5]; |
| 2427 | t.basis.rows[1][2] = dataptr[6]; |
| 2428 | t.origin.y = dataptr[7]; |
| 2429 | t.basis.rows[2][0] = dataptr[8]; |
| 2430 | t.basis.rows[2][1] = dataptr[9]; |
| 2431 | t.basis.rows[2][2] = dataptr[10]; |
| 2432 | t.origin.z = dataptr[11]; |
| 2433 | |
| 2434 | return t; |
| 2435 | } |
| 2436 | |
| 2437 | void MeshStorage::skeleton_bone_set_transform_2d(RID p_skeleton, int p_bone, const Transform2D &p_transform) { |
| 2438 | Skeleton *skeleton = skeleton_owner.get_or_null(p_skeleton); |
| 2439 | |
| 2440 | ERR_FAIL_NULL(skeleton); |
| 2441 | ERR_FAIL_INDEX(p_bone, skeleton->size); |
| 2442 | ERR_FAIL_COND(!skeleton->use_2d); |
| 2443 | float *dataptr = skeleton->data.ptr() + p_bone * 8; |
| 2444 | |
| 2445 | dataptr[0] = p_transform.columns[0][0]; |
| 2446 | dataptr[1] = p_transform.columns[1][0]; |
| 2447 | dataptr[2] = 0; |
| 2448 | dataptr[3] = p_transform.columns[2][0]; |
| 2449 | dataptr[4] = p_transform.columns[0][1]; |
| 2450 | dataptr[5] = p_transform.columns[1][1]; |
| 2451 | dataptr[6] = 0; |
| 2452 | dataptr[7] = p_transform.columns[2][1]; |
| 2453 | |
| 2454 | _skeleton_make_dirty(skeleton); |
| 2455 | } |
| 2456 | |
| 2457 | Transform2D MeshStorage::skeleton_bone_get_transform_2d(RID p_skeleton, int p_bone) const { |
| 2458 | Skeleton *skeleton = skeleton_owner.get_or_null(p_skeleton); |
| 2459 | |
| 2460 | ERR_FAIL_NULL_V(skeleton, Transform2D()); |
| 2461 | ERR_FAIL_INDEX_V(p_bone, skeleton->size, Transform2D()); |
| 2462 | ERR_FAIL_COND_V(!skeleton->use_2d, Transform2D()); |
| 2463 | |
| 2464 | const float *dataptr = skeleton->data.ptr() + p_bone * 8; |
| 2465 | |
| 2466 | Transform2D t; |
| 2467 | t.columns[0][0] = dataptr[0]; |
| 2468 | t.columns[1][0] = dataptr[1]; |
| 2469 | t.columns[2][0] = dataptr[3]; |
| 2470 | t.columns[0][1] = dataptr[4]; |
| 2471 | t.columns[1][1] = dataptr[5]; |
| 2472 | t.columns[2][1] = dataptr[7]; |
| 2473 | |
| 2474 | return t; |
| 2475 | } |
| 2476 | |
| 2477 | void MeshStorage::skeleton_set_base_transform_2d(RID p_skeleton, const Transform2D &p_base_transform) { |
| 2478 | Skeleton *skeleton = skeleton_owner.get_or_null(p_skeleton); |
| 2479 | |
| 2480 | ERR_FAIL_NULL(skeleton); |
| 2481 | ERR_FAIL_COND(!skeleton->use_2d); |
| 2482 | |
| 2483 | skeleton->base_transform_2d = p_base_transform; |
| 2484 | } |
| 2485 | |
| 2486 | void MeshStorage::_update_dirty_skeletons() { |
| 2487 | while (skeleton_dirty_list) { |
| 2488 | Skeleton *skeleton = skeleton_dirty_list; |
| 2489 | |
| 2490 | if (skeleton->size) { |
| 2491 | RD::get_singleton()->buffer_update(skeleton->buffer, 0, skeleton->data.size() * sizeof(float), skeleton->data.ptr()); |
| 2492 | } |
| 2493 | |
| 2494 | skeleton_dirty_list = skeleton->dirty_list; |
| 2495 | |
| 2496 | skeleton->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_SKELETON_BONES); |
| 2497 | |
| 2498 | skeleton->version++; |
| 2499 | |
| 2500 | skeleton->dirty = false; |
| 2501 | skeleton->dirty_list = nullptr; |
| 2502 | } |
| 2503 | |
| 2504 | skeleton_dirty_list = nullptr; |
| 2505 | } |
| 2506 | |
| 2507 | void MeshStorage::skeleton_update_dependency(RID p_skeleton, DependencyTracker *p_instance) { |
| 2508 | Skeleton *skeleton = skeleton_owner.get_or_null(p_skeleton); |
| 2509 | ERR_FAIL_NULL(skeleton); |
| 2510 | |
| 2511 | p_instance->update_dependency(&skeleton->dependency); |
| 2512 | } |