| 1 | /**************************************************************************/ |
| 2 | /* mesh_storage.h */ |
| 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 | #ifndef MESH_STORAGE_GLES3_H |
| 32 | #define MESH_STORAGE_GLES3_H |
| 33 | |
| 34 | #ifdef GLES3_ENABLED |
| 35 | |
| 36 | #include "core/templates/local_vector.h" |
| 37 | #include "core/templates/rid_owner.h" |
| 38 | #include "core/templates/self_list.h" |
| 39 | #include "drivers/gles3/shaders/skeleton.glsl.gen.h" |
| 40 | #include "servers/rendering/storage/mesh_storage.h" |
| 41 | #include "servers/rendering/storage/utilities.h" |
| 42 | |
| 43 | #include "platform_config.h" |
| 44 | #ifndef OPENGL_INCLUDE_H |
| 45 | #include <GLES3/gl3.h> |
| 46 | #else |
| 47 | #include OPENGL_INCLUDE_H |
| 48 | #endif |
| 49 | |
| 50 | namespace GLES3 { |
| 51 | |
| 52 | struct MeshInstance; |
| 53 | |
| 54 | struct Mesh { |
| 55 | struct Surface { |
| 56 | struct Attrib { |
| 57 | bool enabled; |
| 58 | bool integer; |
| 59 | GLint size; |
| 60 | GLenum type; |
| 61 | GLboolean normalized; |
| 62 | GLsizei stride; |
| 63 | uint32_t offset; |
| 64 | }; |
| 65 | RS::PrimitiveType primitive = RS::PRIMITIVE_POINTS; |
| 66 | uint32_t format = 0; |
| 67 | |
| 68 | GLuint vertex_buffer = 0; |
| 69 | GLuint attribute_buffer = 0; |
| 70 | GLuint skin_buffer = 0; |
| 71 | uint32_t vertex_count = 0; |
| 72 | uint32_t vertex_buffer_size = 0; |
| 73 | uint32_t attribute_buffer_size = 0; |
| 74 | uint32_t skin_buffer_size = 0; |
| 75 | |
| 76 | // Cache vertex arrays so they can be created |
| 77 | struct Version { |
| 78 | uint32_t input_mask = 0; |
| 79 | GLuint vertex_array = 0; |
| 80 | |
| 81 | Attrib attribs[RS::ARRAY_MAX]; |
| 82 | }; |
| 83 | |
| 84 | SpinLock version_lock; //needed to access versions |
| 85 | Version *versions = nullptr; //allocated on demand |
| 86 | uint32_t version_count = 0; |
| 87 | |
| 88 | GLuint index_buffer = 0; |
| 89 | uint32_t index_count = 0; |
| 90 | uint32_t index_buffer_size = 0; |
| 91 | |
| 92 | struct LOD { |
| 93 | float edge_length = 0.0; |
| 94 | uint32_t index_count = 0; |
| 95 | uint32_t index_buffer_size = 0; |
| 96 | GLuint index_buffer = 0; |
| 97 | }; |
| 98 | |
| 99 | LOD *lods = nullptr; |
| 100 | uint32_t lod_count = 0; |
| 101 | |
| 102 | AABB aabb; |
| 103 | |
| 104 | Vector<AABB> bone_aabbs; |
| 105 | |
| 106 | struct BlendShape { |
| 107 | GLuint vertex_buffer = 0; |
| 108 | GLuint vertex_array = 0; |
| 109 | }; |
| 110 | |
| 111 | BlendShape *blend_shapes = nullptr; |
| 112 | GLuint skeleton_vertex_array = 0; |
| 113 | |
| 114 | RID material; |
| 115 | }; |
| 116 | |
| 117 | uint32_t blend_shape_count = 0; |
| 118 | RS::BlendShapeMode blend_shape_mode = RS::BLEND_SHAPE_MODE_NORMALIZED; |
| 119 | |
| 120 | Surface **surfaces = nullptr; |
| 121 | uint32_t surface_count = 0; |
| 122 | |
| 123 | bool has_bone_weights = false; |
| 124 | |
| 125 | AABB aabb; |
| 126 | AABB custom_aabb; |
| 127 | uint64_t skeleton_aabb_version = 0; |
| 128 | |
| 129 | Vector<RID> material_cache; |
| 130 | |
| 131 | List<MeshInstance *> instances; |
| 132 | |
| 133 | RID shadow_mesh; |
| 134 | HashSet<Mesh *> shadow_owners; |
| 135 | |
| 136 | Dependency dependency; |
| 137 | }; |
| 138 | |
| 139 | /* Mesh Instance */ |
| 140 | |
| 141 | struct MeshInstance { |
| 142 | Mesh *mesh = nullptr; |
| 143 | RID skeleton; |
| 144 | struct Surface { |
| 145 | GLuint vertex_buffers[2] = { 0, 0 }; |
| 146 | GLuint vertex_arrays[2] = { 0, 0 }; |
| 147 | GLuint vertex_buffer = 0; |
| 148 | int vertex_stride_cache = 0; |
| 149 | int vertex_size_cache = 0; |
| 150 | int vertex_normal_offset_cache = 0; |
| 151 | int vertex_tangent_offset_cache = 0; |
| 152 | uint32_t format_cache = 0; |
| 153 | |
| 154 | Mesh::Surface::Version *versions = nullptr; //allocated on demand |
| 155 | uint32_t version_count = 0; |
| 156 | }; |
| 157 | LocalVector<Surface> surfaces; |
| 158 | LocalVector<float> blend_weights; |
| 159 | |
| 160 | List<MeshInstance *>::Element *I = nullptr; //used to erase itself |
| 161 | uint64_t skeleton_version = 0; |
| 162 | bool dirty = false; |
| 163 | bool weights_dirty = false; |
| 164 | SelfList<MeshInstance> weight_update_list; |
| 165 | SelfList<MeshInstance> array_update_list; |
| 166 | Transform2D canvas_item_transform_2d; |
| 167 | MeshInstance() : |
| 168 | weight_update_list(this), array_update_list(this) {} |
| 169 | }; |
| 170 | |
| 171 | /* MultiMesh */ |
| 172 | |
| 173 | struct MultiMesh { |
| 174 | RID mesh; |
| 175 | int instances = 0; |
| 176 | RS::MultimeshTransformFormat xform_format = RS::MULTIMESH_TRANSFORM_3D; |
| 177 | bool uses_colors = false; |
| 178 | bool uses_custom_data = false; |
| 179 | int visible_instances = -1; |
| 180 | AABB aabb; |
| 181 | bool aabb_dirty = false; |
| 182 | bool buffer_set = false; |
| 183 | uint32_t stride_cache = 0; |
| 184 | uint32_t color_offset_cache = 0; |
| 185 | uint32_t custom_data_offset_cache = 0; |
| 186 | |
| 187 | Vector<float> data_cache; //used if individual setting is used |
| 188 | bool *data_cache_dirty_regions = nullptr; |
| 189 | uint32_t data_cache_used_dirty_regions = 0; |
| 190 | |
| 191 | GLuint buffer = 0; |
| 192 | |
| 193 | bool dirty = false; |
| 194 | MultiMesh *dirty_list = nullptr; |
| 195 | |
| 196 | Dependency dependency; |
| 197 | }; |
| 198 | |
| 199 | struct Skeleton { |
| 200 | bool use_2d = false; |
| 201 | int size = 0; |
| 202 | int height = 0; |
| 203 | Vector<float> data; |
| 204 | |
| 205 | bool dirty = false; |
| 206 | Skeleton *dirty_list = nullptr; |
| 207 | Transform2D base_transform_2d; |
| 208 | |
| 209 | GLuint transforms_texture = 0; |
| 210 | |
| 211 | uint64_t version = 1; |
| 212 | |
| 213 | Dependency dependency; |
| 214 | }; |
| 215 | |
| 216 | class MeshStorage : public RendererMeshStorage { |
| 217 | private: |
| 218 | static MeshStorage *singleton; |
| 219 | |
| 220 | struct { |
| 221 | SkeletonShaderGLES3 shader; |
| 222 | RID shader_version; |
| 223 | } skeleton_shader; |
| 224 | |
| 225 | /* Mesh */ |
| 226 | |
| 227 | mutable RID_Owner<Mesh, true> mesh_owner; |
| 228 | |
| 229 | void _mesh_surface_generate_version_for_input_mask(Mesh::Surface::Version &v, Mesh::Surface *s, uint32_t p_input_mask, MeshInstance::Surface *mis = nullptr); |
| 230 | |
| 231 | /* Mesh Instance API */ |
| 232 | |
| 233 | mutable RID_Owner<MeshInstance> mesh_instance_owner; |
| 234 | |
| 235 | void _mesh_instance_clear(MeshInstance *mi); |
| 236 | void _mesh_instance_add_surface(MeshInstance *mi, Mesh *mesh, uint32_t p_surface); |
| 237 | void _blend_shape_bind_mesh_instance_buffer(MeshInstance *p_mi, uint32_t p_surface); |
| 238 | SelfList<MeshInstance>::List dirty_mesh_instance_weights; |
| 239 | SelfList<MeshInstance>::List dirty_mesh_instance_arrays; |
| 240 | |
| 241 | /* MultiMesh */ |
| 242 | |
| 243 | mutable RID_Owner<MultiMesh, true> multimesh_owner; |
| 244 | |
| 245 | MultiMesh *multimesh_dirty_list = nullptr; |
| 246 | |
| 247 | _FORCE_INLINE_ void _multimesh_make_local(MultiMesh *multimesh) const; |
| 248 | _FORCE_INLINE_ void _multimesh_mark_dirty(MultiMesh *multimesh, int p_index, bool p_aabb); |
| 249 | _FORCE_INLINE_ void _multimesh_mark_all_dirty(MultiMesh *multimesh, bool p_data, bool p_aabb); |
| 250 | _FORCE_INLINE_ void _multimesh_re_create_aabb(MultiMesh *multimesh, const float *p_data, int p_instances); |
| 251 | |
| 252 | /* Skeleton */ |
| 253 | |
| 254 | mutable RID_Owner<Skeleton, true> skeleton_owner; |
| 255 | |
| 256 | _FORCE_INLINE_ void _skeleton_make_dirty(Skeleton *skeleton); |
| 257 | void _compute_skeleton(MeshInstance *p_mi, Skeleton *p_sk, uint32_t p_surface); |
| 258 | |
| 259 | Skeleton *skeleton_dirty_list = nullptr; |
| 260 | |
| 261 | public: |
| 262 | static MeshStorage *get_singleton(); |
| 263 | |
| 264 | MeshStorage(); |
| 265 | virtual ~MeshStorage(); |
| 266 | |
| 267 | /* MESH API */ |
| 268 | |
| 269 | Mesh *get_mesh(RID p_rid) { return mesh_owner.get_or_null(p_rid); }; |
| 270 | bool owns_mesh(RID p_rid) { return mesh_owner.owns(p_rid); }; |
| 271 | |
| 272 | virtual RID mesh_allocate() override; |
| 273 | virtual void mesh_initialize(RID p_rid) override; |
| 274 | virtual void mesh_free(RID p_rid) override; |
| 275 | |
| 276 | virtual void mesh_set_blend_shape_count(RID p_mesh, int p_blend_shape_count) override; |
| 277 | virtual bool mesh_needs_instance(RID p_mesh, bool p_has_skeleton) override; |
| 278 | |
| 279 | virtual void mesh_add_surface(RID p_mesh, const RS::SurfaceData &p_surface) override; |
| 280 | |
| 281 | virtual int mesh_get_blend_shape_count(RID p_mesh) const override; |
| 282 | |
| 283 | virtual void mesh_set_blend_shape_mode(RID p_mesh, RS::BlendShapeMode p_mode) override; |
| 284 | virtual RS::BlendShapeMode mesh_get_blend_shape_mode(RID p_mesh) const override; |
| 285 | |
| 286 | virtual void mesh_surface_update_vertex_region(RID p_mesh, int p_surface, int p_offset, const Vector<uint8_t> &p_data) override; |
| 287 | virtual void mesh_surface_update_attribute_region(RID p_mesh, int p_surface, int p_offset, const Vector<uint8_t> &p_data) override; |
| 288 | virtual void mesh_surface_update_skin_region(RID p_mesh, int p_surface, int p_offset, const Vector<uint8_t> &p_data) override; |
| 289 | |
| 290 | virtual void mesh_surface_set_material(RID p_mesh, int p_surface, RID p_material) override; |
| 291 | virtual RID mesh_surface_get_material(RID p_mesh, int p_surface) const override; |
| 292 | |
| 293 | virtual RS::SurfaceData mesh_get_surface(RID p_mesh, int p_surface) const override; |
| 294 | virtual int mesh_get_surface_count(RID p_mesh) const override; |
| 295 | |
| 296 | virtual void mesh_set_custom_aabb(RID p_mesh, const AABB &p_aabb) override; |
| 297 | virtual AABB mesh_get_custom_aabb(RID p_mesh) const override; |
| 298 | |
| 299 | virtual AABB mesh_get_aabb(RID p_mesh, RID p_skeleton = RID()) override; |
| 300 | virtual void mesh_set_shadow_mesh(RID p_mesh, RID p_shadow_mesh) override; |
| 301 | virtual void mesh_clear(RID p_mesh) override; |
| 302 | |
| 303 | _FORCE_INLINE_ const RID *mesh_get_surface_count_and_materials(RID p_mesh, uint32_t &r_surface_count) { |
| 304 | Mesh *mesh = mesh_owner.get_or_null(p_mesh); |
| 305 | ERR_FAIL_NULL_V(mesh, nullptr); |
| 306 | r_surface_count = mesh->surface_count; |
| 307 | if (r_surface_count == 0) { |
| 308 | return nullptr; |
| 309 | } |
| 310 | if (mesh->material_cache.is_empty()) { |
| 311 | mesh->material_cache.resize(mesh->surface_count); |
| 312 | for (uint32_t i = 0; i < r_surface_count; i++) { |
| 313 | mesh->material_cache.write[i] = mesh->surfaces[i]->material; |
| 314 | } |
| 315 | } |
| 316 | |
| 317 | return mesh->material_cache.ptr(); |
| 318 | } |
| 319 | |
| 320 | _FORCE_INLINE_ void *mesh_get_surface(RID p_mesh, uint32_t p_surface_index) { |
| 321 | Mesh *mesh = mesh_owner.get_or_null(p_mesh); |
| 322 | ERR_FAIL_NULL_V(mesh, nullptr); |
| 323 | ERR_FAIL_UNSIGNED_INDEX_V(p_surface_index, mesh->surface_count, nullptr); |
| 324 | |
| 325 | return mesh->surfaces[p_surface_index]; |
| 326 | } |
| 327 | |
| 328 | _FORCE_INLINE_ RID mesh_get_shadow_mesh(RID p_mesh) { |
| 329 | Mesh *mesh = mesh_owner.get_or_null(p_mesh); |
| 330 | ERR_FAIL_NULL_V(mesh, RID()); |
| 331 | |
| 332 | return mesh->shadow_mesh; |
| 333 | } |
| 334 | |
| 335 | _FORCE_INLINE_ RS::PrimitiveType mesh_surface_get_primitive(void *p_surface) { |
| 336 | Mesh::Surface *surface = reinterpret_cast<Mesh::Surface *>(p_surface); |
| 337 | return surface->primitive; |
| 338 | } |
| 339 | |
| 340 | _FORCE_INLINE_ bool mesh_surface_has_lod(void *p_surface) const { |
| 341 | Mesh::Surface *s = reinterpret_cast<Mesh::Surface *>(p_surface); |
| 342 | return s->lod_count > 0; |
| 343 | } |
| 344 | |
| 345 | _FORCE_INLINE_ uint32_t mesh_surface_get_vertices_drawn_count(void *p_surface) const { |
| 346 | Mesh::Surface *s = reinterpret_cast<Mesh::Surface *>(p_surface); |
| 347 | return s->index_count ? s->index_count : s->vertex_count; |
| 348 | } |
| 349 | |
| 350 | _FORCE_INLINE_ uint32_t mesh_surface_get_lod(void *p_surface, float p_model_scale, float p_distance_threshold, float p_mesh_lod_threshold, uint32_t &r_index_count) const { |
| 351 | Mesh::Surface *s = reinterpret_cast<Mesh::Surface *>(p_surface); |
| 352 | ERR_FAIL_NULL_V(s, 0); |
| 353 | |
| 354 | int32_t current_lod = -1; |
| 355 | r_index_count = s->index_count; |
| 356 | |
| 357 | for (uint32_t i = 0; i < s->lod_count; i++) { |
| 358 | float screen_size = s->lods[i].edge_length * p_model_scale / p_distance_threshold; |
| 359 | if (screen_size > p_mesh_lod_threshold) { |
| 360 | break; |
| 361 | } |
| 362 | current_lod = i; |
| 363 | } |
| 364 | if (current_lod == -1) { |
| 365 | return 0; |
| 366 | } else { |
| 367 | r_index_count = s->lods[current_lod].index_count; |
| 368 | return current_lod + 1; |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | _FORCE_INLINE_ GLuint mesh_surface_get_index_buffer(void *p_surface, uint32_t p_lod) const { |
| 373 | Mesh::Surface *s = reinterpret_cast<Mesh::Surface *>(p_surface); |
| 374 | |
| 375 | if (p_lod == 0) { |
| 376 | return s->index_buffer; |
| 377 | } else { |
| 378 | return s->lods[p_lod - 1].index_buffer; |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | _FORCE_INLINE_ GLenum mesh_surface_get_index_type(void *p_surface) const { |
| 383 | Mesh::Surface *s = reinterpret_cast<Mesh::Surface *>(p_surface); |
| 384 | |
| 385 | return (s->vertex_count <= 65536 && s->vertex_count > 0) ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT; |
| 386 | } |
| 387 | |
| 388 | // Use this to cache Vertex Array Objects so they are only generated once |
| 389 | _FORCE_INLINE_ void mesh_surface_get_vertex_arrays_and_format(void *p_surface, uint32_t p_input_mask, GLuint &r_vertex_array_gl) { |
| 390 | Mesh::Surface *s = reinterpret_cast<Mesh::Surface *>(p_surface); |
| 391 | |
| 392 | s->version_lock.lock(); |
| 393 | |
| 394 | //there will never be more than, at much, 3 or 4 versions, so iterating is the fastest way |
| 395 | |
| 396 | for (uint32_t i = 0; i < s->version_count; i++) { |
| 397 | if (s->versions[i].input_mask != p_input_mask) { |
| 398 | continue; |
| 399 | } |
| 400 | //we have this version, hooray |
| 401 | r_vertex_array_gl = s->versions[i].vertex_array; |
| 402 | s->version_lock.unlock(); |
| 403 | return; |
| 404 | } |
| 405 | |
| 406 | uint32_t version = s->version_count; |
| 407 | s->version_count++; |
| 408 | s->versions = (Mesh::Surface::Version *)memrealloc(s->versions, sizeof(Mesh::Surface::Version) * s->version_count); |
| 409 | |
| 410 | _mesh_surface_generate_version_for_input_mask(s->versions[version], s, p_input_mask); |
| 411 | |
| 412 | r_vertex_array_gl = s->versions[version].vertex_array; |
| 413 | |
| 414 | s->version_lock.unlock(); |
| 415 | } |
| 416 | |
| 417 | /* MESH INSTANCE API */ |
| 418 | |
| 419 | MeshInstance *get_mesh_instance(RID p_rid) { return mesh_instance_owner.get_or_null(p_rid); }; |
| 420 | bool owns_mesh_instance(RID p_rid) { return mesh_instance_owner.owns(p_rid); }; |
| 421 | |
| 422 | virtual RID mesh_instance_create(RID p_base) override; |
| 423 | virtual void mesh_instance_free(RID p_rid) override; |
| 424 | virtual void mesh_instance_set_skeleton(RID p_mesh_instance, RID p_skeleton) override; |
| 425 | virtual void mesh_instance_set_blend_shape_weight(RID p_mesh_instance, int p_shape, float p_weight) override; |
| 426 | virtual void mesh_instance_check_for_update(RID p_mesh_instance) override; |
| 427 | virtual void mesh_instance_set_canvas_item_transform(RID p_mesh_instance, const Transform2D &p_transform) override; |
| 428 | virtual void update_mesh_instances() override; |
| 429 | |
| 430 | // TODO: considering hashing versions with multimesh buffer RID. |
| 431 | // Doing so would allow us to avoid specifying multimesh buffer pointers every frame and may improve performance. |
| 432 | _FORCE_INLINE_ void mesh_instance_surface_get_vertex_arrays_and_format(RID p_mesh_instance, uint32_t p_surface_index, uint32_t p_input_mask, GLuint &r_vertex_array_gl) { |
| 433 | MeshInstance *mi = mesh_instance_owner.get_or_null(p_mesh_instance); |
| 434 | ERR_FAIL_NULL(mi); |
| 435 | Mesh *mesh = mi->mesh; |
| 436 | ERR_FAIL_UNSIGNED_INDEX(p_surface_index, mesh->surface_count); |
| 437 | |
| 438 | MeshInstance::Surface *mis = &mi->surfaces[p_surface_index]; |
| 439 | Mesh::Surface *s = mesh->surfaces[p_surface_index]; |
| 440 | |
| 441 | s->version_lock.lock(); |
| 442 | |
| 443 | //there will never be more than, at much, 3 or 4 versions, so iterating is the fastest way |
| 444 | |
| 445 | for (uint32_t i = 0; i < mis->version_count; i++) { |
| 446 | if (mis->versions[i].input_mask != p_input_mask) { |
| 447 | continue; |
| 448 | } |
| 449 | //we have this version, hooray |
| 450 | r_vertex_array_gl = mis->versions[i].vertex_array; |
| 451 | s->version_lock.unlock(); |
| 452 | return; |
| 453 | } |
| 454 | |
| 455 | uint32_t version = mis->version_count; |
| 456 | mis->version_count++; |
| 457 | mis->versions = (Mesh::Surface::Version *)memrealloc(mis->versions, sizeof(Mesh::Surface::Version) * mis->version_count); |
| 458 | |
| 459 | _mesh_surface_generate_version_for_input_mask(mis->versions[version], s, p_input_mask, mis); |
| 460 | |
| 461 | r_vertex_array_gl = mis->versions[version].vertex_array; |
| 462 | |
| 463 | s->version_lock.unlock(); |
| 464 | } |
| 465 | |
| 466 | /* MULTIMESH API */ |
| 467 | |
| 468 | MultiMesh *get_multimesh(RID p_rid) { return multimesh_owner.get_or_null(p_rid); }; |
| 469 | bool owns_multimesh(RID p_rid) { return multimesh_owner.owns(p_rid); }; |
| 470 | |
| 471 | virtual RID multimesh_allocate() override; |
| 472 | virtual void multimesh_initialize(RID p_rid) override; |
| 473 | virtual void multimesh_free(RID p_rid) override; |
| 474 | virtual void multimesh_allocate_data(RID p_multimesh, int p_instances, RS::MultimeshTransformFormat p_transform_format, bool p_use_colors = false, bool p_use_custom_data = false) override; |
| 475 | virtual int multimesh_get_instance_count(RID p_multimesh) const override; |
| 476 | |
| 477 | virtual void multimesh_set_mesh(RID p_multimesh, RID p_mesh) override; |
| 478 | virtual void multimesh_instance_set_transform(RID p_multimesh, int p_index, const Transform3D &p_transform) override; |
| 479 | virtual void multimesh_instance_set_transform_2d(RID p_multimesh, int p_index, const Transform2D &p_transform) override; |
| 480 | virtual void multimesh_instance_set_color(RID p_multimesh, int p_index, const Color &p_color) override; |
| 481 | virtual void multimesh_instance_set_custom_data(RID p_multimesh, int p_index, const Color &p_color) override; |
| 482 | |
| 483 | virtual RID multimesh_get_mesh(RID p_multimesh) const override; |
| 484 | virtual AABB multimesh_get_aabb(RID p_multimesh) const override; |
| 485 | |
| 486 | virtual Transform3D multimesh_instance_get_transform(RID p_multimesh, int p_index) const override; |
| 487 | virtual Transform2D multimesh_instance_get_transform_2d(RID p_multimesh, int p_index) const override; |
| 488 | virtual Color multimesh_instance_get_color(RID p_multimesh, int p_index) const override; |
| 489 | virtual Color multimesh_instance_get_custom_data(RID p_multimesh, int p_index) const override; |
| 490 | virtual void multimesh_set_buffer(RID p_multimesh, const Vector<float> &p_buffer) override; |
| 491 | virtual Vector<float> multimesh_get_buffer(RID p_multimesh) const override; |
| 492 | |
| 493 | virtual void multimesh_set_visible_instances(RID p_multimesh, int p_visible) override; |
| 494 | virtual int multimesh_get_visible_instances(RID p_multimesh) const override; |
| 495 | |
| 496 | void _update_dirty_multimeshes(); |
| 497 | |
| 498 | _FORCE_INLINE_ RS::MultimeshTransformFormat multimesh_get_transform_format(RID p_multimesh) const { |
| 499 | MultiMesh *multimesh = multimesh_owner.get_or_null(p_multimesh); |
| 500 | return multimesh->xform_format; |
| 501 | } |
| 502 | |
| 503 | _FORCE_INLINE_ bool multimesh_uses_colors(RID p_multimesh) const { |
| 504 | MultiMesh *multimesh = multimesh_owner.get_or_null(p_multimesh); |
| 505 | return multimesh->uses_colors; |
| 506 | } |
| 507 | |
| 508 | _FORCE_INLINE_ bool multimesh_uses_custom_data(RID p_multimesh) const { |
| 509 | MultiMesh *multimesh = multimesh_owner.get_or_null(p_multimesh); |
| 510 | return multimesh->uses_custom_data; |
| 511 | } |
| 512 | |
| 513 | _FORCE_INLINE_ uint32_t multimesh_get_instances_to_draw(RID p_multimesh) const { |
| 514 | MultiMesh *multimesh = multimesh_owner.get_or_null(p_multimesh); |
| 515 | if (multimesh->visible_instances >= 0) { |
| 516 | return multimesh->visible_instances; |
| 517 | } |
| 518 | return multimesh->instances; |
| 519 | } |
| 520 | |
| 521 | _FORCE_INLINE_ GLuint multimesh_get_gl_buffer(RID p_multimesh) const { |
| 522 | MultiMesh *multimesh = multimesh_owner.get_or_null(p_multimesh); |
| 523 | return multimesh->buffer; |
| 524 | } |
| 525 | |
| 526 | _FORCE_INLINE_ uint32_t multimesh_get_stride(RID p_multimesh) const { |
| 527 | MultiMesh *multimesh = multimesh_owner.get_or_null(p_multimesh); |
| 528 | return multimesh->stride_cache; |
| 529 | } |
| 530 | |
| 531 | _FORCE_INLINE_ uint32_t multimesh_get_color_offset(RID p_multimesh) const { |
| 532 | MultiMesh *multimesh = multimesh_owner.get_or_null(p_multimesh); |
| 533 | return multimesh->color_offset_cache; |
| 534 | } |
| 535 | |
| 536 | _FORCE_INLINE_ uint32_t multimesh_get_custom_data_offset(RID p_multimesh) const { |
| 537 | MultiMesh *multimesh = multimesh_owner.get_or_null(p_multimesh); |
| 538 | return multimesh->custom_data_offset_cache; |
| 539 | } |
| 540 | |
| 541 | /* SKELETON API */ |
| 542 | |
| 543 | Skeleton *get_skeleton(RID p_rid) { return skeleton_owner.get_or_null(p_rid); }; |
| 544 | bool owns_skeleton(RID p_rid) { return skeleton_owner.owns(p_rid); }; |
| 545 | |
| 546 | virtual RID skeleton_allocate() override; |
| 547 | virtual void skeleton_initialize(RID p_rid) override; |
| 548 | virtual void skeleton_free(RID p_rid) override; |
| 549 | |
| 550 | virtual void skeleton_allocate_data(RID p_skeleton, int p_bones, bool p_2d_skeleton = false) override; |
| 551 | virtual void skeleton_set_base_transform_2d(RID p_skeleton, const Transform2D &p_base_transform) override; |
| 552 | virtual int skeleton_get_bone_count(RID p_skeleton) const override; |
| 553 | virtual void skeleton_bone_set_transform(RID p_skeleton, int p_bone, const Transform3D &p_transform) override; |
| 554 | virtual Transform3D skeleton_bone_get_transform(RID p_skeleton, int p_bone) const override; |
| 555 | virtual void skeleton_bone_set_transform_2d(RID p_skeleton, int p_bone, const Transform2D &p_transform) override; |
| 556 | virtual Transform2D skeleton_bone_get_transform_2d(RID p_skeleton, int p_bone) const override; |
| 557 | |
| 558 | virtual void skeleton_update_dependency(RID p_base, DependencyTracker *p_instance) override; |
| 559 | |
| 560 | void _update_dirty_skeletons(); |
| 561 | |
| 562 | _FORCE_INLINE_ bool skeleton_is_valid(RID p_skeleton) { |
| 563 | return skeleton_owner.get_or_null(p_skeleton) != nullptr; |
| 564 | } |
| 565 | }; |
| 566 | |
| 567 | } // namespace GLES3 |
| 568 | |
| 569 | #endif // GLES3_ENABLED |
| 570 | |
| 571 | #endif // MESH_STORAGE_GLES3_H |
| 572 | |