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