| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
#pragma once |
| 32 |
|
| 33 |
#include "../../rendering_server_globals.h" |
| 34 |
#include "core/templates/local_vector.h" |
| 35 |
#include "core/templates/rid_owner.h" |
| 36 |
#include "core/templates/self_list.h" |
| 37 |
#include "servers/rendering/renderer_rd/shaders/skeleton.glsl.gen.h" |
| 38 |
#include "servers/rendering/storage/mesh_storage.h" |
| 39 |
#include "servers/rendering/storage/utilities.h" |
| 40 |
|
| 41 |
namespace RendererRD { |
| 42 |
|
| 43 |
class MeshStorage : public RendererMeshStorage { |
| 44 |
public: |
| 45 |
enum DefaultRDBuffer { |
| 46 |
DEFAULT_RD_BUFFER_VERTEX, |
| 47 |
DEFAULT_RD_BUFFER_NORMAL, |
| 48 |
DEFAULT_RD_BUFFER_TANGENT, |
| 49 |
DEFAULT_RD_BUFFER_COLOR, |
| 50 |
DEFAULT_RD_BUFFER_TEX_UV, |
| 51 |
DEFAULT_RD_BUFFER_TEX_UV2, |
| 52 |
DEFAULT_RD_BUFFER_CUSTOM0, |
| 53 |
DEFAULT_RD_BUFFER_CUSTOM1, |
| 54 |
DEFAULT_RD_BUFFER_CUSTOM2, |
| 55 |
DEFAULT_RD_BUFFER_CUSTOM3, |
| 56 |
DEFAULT_RD_BUFFER_BONES, |
| 57 |
DEFAULT_RD_BUFFER_WEIGHTS, |
| 58 |
DEFAULT_RD_BUFFER_MAX, |
| 59 |
}; |
| 60 |
|
| 61 |
enum IndirectMultiMesh : uint32_t { |
| 62 |
INDIRECT_MULTIMESH_COMMAND_STRIDE = 5 |
| 63 |
}; |
| 64 |
|
| 65 |
private: |
| 66 |
static MeshStorage *singleton; |
| 67 |
|
| 68 |
RID default_rd_storage_buffer; |
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
RID mesh_default_rd_buffers[DEFAULT_RD_BUFFER_MAX]; |
| 73 |
|
| 74 |
struct MeshInstance; |
| 75 |
|
| 76 |
struct Mesh { |
| 77 |
struct Surface { |
| 78 |
RS::PrimitiveType primitive = RS::PRIMITIVE_POINTS; |
| 79 |
uint64_t format = 0; |
| 80 |
|
| 81 |
uint32_t vertex_count = 0; |
| 82 |
RID vertex_buffer; |
| 83 |
uint32_t vertex_buffer_size = 0; |
| 84 |
|
| 85 |
RID attribute_buffer; |
| 86 |
uint32_t attribute_buffer_size = 0; |
| 87 |
|
| 88 |
RID skin_buffer; |
| 89 |
uint32_t skin_buffer_size = 0; |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
struct Version { |
| 99 |
uint64_t input_mask = 0; |
| 100 |
uint32_t current_buffer = 0; |
| 101 |
uint32_t previous_buffer = 0; |
| 102 |
bool input_motion_vectors = false; |
| 103 |
bool point_size_emulated = false; |
| 104 |
RD::VertexFormatID vertex_format = 0; |
| 105 |
RID vertex_array; |
| 106 |
}; |
| 107 |
|
| 108 |
SpinLock version_lock; |
| 109 |
Version *versions = nullptr; |
| 110 |
uint32_t version_count = 0; |
| 111 |
|
| 112 |
RID index_buffer; |
| 113 |
uint32_t index_buffer_size = 0; |
| 114 |
RID index_array; |
| 115 |
uint32_t index_count = 0; |
| 116 |
|
| 117 |
struct LOD { |
| 118 |
float edge_length = 0.0; |
| 119 |
uint32_t index_count = 0; |
| 120 |
RID index_buffer; |
| 121 |
uint32_t index_buffer_size = 0; |
| 122 |
RID index_array; |
| 123 |
}; |
| 124 |
|
| 125 |
LOD *lods = nullptr; |
| 126 |
uint32_t lod_count = 0; |
| 127 |
|
| 128 |
AABB aabb; |
| 129 |
|
| 130 |
Vector<AABB> bone_aabbs; |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
Transform3D mesh_to_skeleton_xform; |
| 135 |
|
| 136 |
Vector4 uv_scale; |
| 137 |
|
| 138 |
RID blend_shape_buffer; |
| 139 |
uint32_t blend_shape_buffer_size = 0; |
| 140 |
|
| 141 |
RID material; |
| 142 |
|
| 143 |
uint32_t render_index = 0; |
| 144 |
uint64_t render_pass = 0; |
| 145 |
|
| 146 |
uint32_t multimesh_render_index = 0; |
| 147 |
uint64_t multimesh_render_pass = 0; |
| 148 |
|
| 149 |
uint32_t particles_render_index = 0; |
| 150 |
uint64_t particles_render_pass = 0; |
| 151 |
|
| 152 |
RID uniform_set; |
| 153 |
}; |
| 154 |
|
| 155 |
uint32_t blend_shape_count = 0; |
| 156 |
RS::BlendShapeMode blend_shape_mode = RS::BLEND_SHAPE_MODE_NORMALIZED; |
| 157 |
|
| 158 |
Surface **surfaces = nullptr; |
| 159 |
uint32_t surface_count = 0; |
| 160 |
|
| 161 |
bool has_bone_weights = false; |
| 162 |
|
| 163 |
AABB aabb; |
| 164 |
AABB custom_aabb; |
| 165 |
uint64_t skeleton_aabb_version = 0; |
| 166 |
RID skeleton_aabb_rid; |
| 167 |
|
| 168 |
Vector<RID> material_cache; |
| 169 |
|
| 170 |
List<MeshInstance *> instances; |
| 171 |
|
| 172 |
RID shadow_mesh; |
| 173 |
HashSet<Mesh *> shadow_owners; |
| 174 |
|
| 175 |
String path; |
| 176 |
|
| 177 |
Dependency dependency; |
| 178 |
}; |
| 179 |
|
| 180 |
mutable RID_Owner<Mesh, true> mesh_owner; |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
struct MeshInstance { |
| 185 |
Mesh *mesh = nullptr; |
| 186 |
RID skeleton; |
| 187 |
struct Surface { |
| 188 |
RID vertex_buffer[2]; |
| 189 |
RID uniform_set[2]; |
| 190 |
uint32_t current_buffer = 0; |
| 191 |
uint32_t previous_buffer = 0; |
| 192 |
uint64_t last_change = 0; |
| 193 |
|
| 194 |
Mesh::Surface::Version *versions = nullptr; |
| 195 |
uint32_t version_count = 0; |
| 196 |
}; |
| 197 |
LocalVector<Surface> surfaces; |
| 198 |
LocalVector<float> blend_weights; |
| 199 |
|
| 200 |
RID blend_weights_buffer; |
| 201 |
List<MeshInstance *>::Element *I = nullptr; |
| 202 |
uint64_t skeleton_version = 0; |
| 203 |
bool dirty = false; |
| 204 |
bool weights_dirty = false; |
| 205 |
SelfList<MeshInstance> weight_update_list; |
| 206 |
SelfList<MeshInstance> array_update_list; |
| 207 |
Transform2D canvas_item_transform_2d; |
| 208 |
MeshInstance() : |
| 209 |
weight_update_list(this), array_update_list(this) {} |
| 210 |
}; |
| 211 |
|
| 212 |
RD::VertexFormatID _mesh_surface_generate_vertex_format(uint64_t p_surface_format, uint64_t p_input_mask, bool p_instanced_surface, bool p_input_motion_vectors, bool p_point_size_emulated, uint32_t &r_position_stride); |
| 213 |
void _mesh_surface_generate_version_for_input_mask(Mesh::Surface::Version &v, Mesh::Surface *s, uint64_t p_input_mask, bool p_input_motion_vectors, bool p_point_size_emulated, MeshInstance::Surface *mis = nullptr, uint32_t p_current_buffer = 0, uint32_t p_previous_buffer = 0); |
| 214 |
void _mesh_surface_clear(Mesh *p_mesh, int p_surface); |
| 215 |
|
| 216 |
void _mesh_instance_clear(MeshInstance *mi); |
| 217 |
void _mesh_instance_add_surface(MeshInstance *mi, Mesh *mesh, uint32_t p_surface); |
| 218 |
void _mesh_instance_add_surface_buffer(MeshInstance *mi, Mesh *mesh, MeshInstance::Surface *s, uint32_t p_surface, uint32_t p_buffer_index); |
| 219 |
void _mesh_instance_remove_surface(MeshInstance *mi, int p_surface); |
| 220 |
|
| 221 |
mutable RID_Owner<MeshInstance> mesh_instance_owner; |
| 222 |
|
| 223 |
SelfList<MeshInstance>::List dirty_mesh_instance_weights; |
| 224 |
SelfList<MeshInstance>::List dirty_mesh_instance_arrays; |
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
struct MultiMesh { |
| 229 |
RID mesh; |
| 230 |
int instances = 0; |
| 231 |
RS::MultimeshTransformFormat xform_format = RS::MULTIMESH_TRANSFORM_3D; |
| 232 |
bool uses_colors = false; |
| 233 |
bool uses_custom_data = false; |
| 234 |
int visible_instances = -1; |
| 235 |
AABB aabb; |
| 236 |
AABB custom_aabb; |
| 237 |
bool aabb_dirty = false; |
| 238 |
bool buffer_set = false; |
| 239 |
bool indirect = false; |
| 240 |
bool motion_vectors_enabled = false; |
| 241 |
uint32_t motion_vectors_current_offset = 0; |
| 242 |
uint32_t motion_vectors_previous_offset = 0; |
| 243 |
uint64_t motion_vectors_last_change = -1; |
| 244 |
uint32_t stride_cache = 0; |
| 245 |
uint32_t color_offset_cache = 0; |
| 246 |
uint32_t custom_data_offset_cache = 0; |
| 247 |
|
| 248 |
Vector<float> data_cache; |
| 249 |
bool *data_cache_dirty_regions = nullptr; |
| 250 |
uint32_t data_cache_dirty_region_count = 0; |
| 251 |
bool *previous_data_cache_dirty_regions = nullptr; |
| 252 |
uint32_t previous_data_cache_dirty_region_count = 0; |
| 253 |
|
| 254 |
RID buffer; |
| 255 |
RID uniform_set_3d; |
| 256 |
RID uniform_set_2d; |
| 257 |
RID command_buffer; |
| 258 |
|
| 259 |
bool dirty = false; |
| 260 |
MultiMesh *dirty_list = nullptr; |
| 261 |
|
| 262 |
RendererMeshStorage::MultiMeshInterpolator interpolator; |
| 263 |
|
| 264 |
Dependency dependency; |
| 265 |
}; |
| 266 |
|
| 267 |
mutable RID_Owner<MultiMesh, true> multimesh_owner; |
| 268 |
|
| 269 |
MultiMesh *multimesh_dirty_list = nullptr; |
| 270 |
|
| 271 |
_FORCE_INLINE_ void _multimesh_make_local(MultiMesh *multimesh) const; |
| 272 |
_FORCE_INLINE_ void _multimesh_enable_motion_vectors(MultiMesh *multimesh); |
| 273 |
_FORCE_INLINE_ void _multimesh_update_motion_vectors_data_cache(MultiMesh *multimesh); |
| 274 |
_FORCE_INLINE_ bool _multimesh_uses_motion_vectors(MultiMesh *multimesh); |
| 275 |
_FORCE_INLINE_ void _multimesh_mark_dirty(MultiMesh *multimesh, int p_index, bool p_aabb); |
| 276 |
_FORCE_INLINE_ void _multimesh_mark_all_dirty(MultiMesh *multimesh, bool p_data, bool p_aabb); |
| 277 |
_FORCE_INLINE_ void _multimesh_re_create_aabb(MultiMesh *multimesh, const float *p_data, int p_instances); |
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
struct SkeletonShader { |
| 282 |
struct PushConstant { |
| 283 |
uint32_t has_normal; |
| 284 |
uint32_t has_tangent; |
| 285 |
uint32_t has_skeleton; |
| 286 |
uint32_t has_blend_shape; |
| 287 |
|
| 288 |
uint32_t vertex_count; |
| 289 |
uint32_t vertex_stride; |
| 290 |
uint32_t skin_stride; |
| 291 |
uint32_t skin_weight_offset; |
| 292 |
|
| 293 |
uint32_t blend_shape_count; |
| 294 |
uint32_t normalized_blend_shapes; |
| 295 |
uint32_t normal_tangent_stride; |
| 296 |
uint32_t pad1; |
| 297 |
float skeleton_transform_x[2]; |
| 298 |
float skeleton_transform_y[2]; |
| 299 |
|
| 300 |
float skeleton_transform_offset[2]; |
| 301 |
float inverse_transform_x[2]; |
| 302 |
|
| 303 |
float inverse_transform_y[2]; |
| 304 |
float inverse_transform_offset[2]; |
| 305 |
}; |
| 306 |
|
| 307 |
enum { |
| 308 |
UNIFORM_SET_INSTANCE = 0, |
| 309 |
UNIFORM_SET_SURFACE = 1, |
| 310 |
UNIFORM_SET_SKELETON = 2, |
| 311 |
}; |
| 312 |
enum { |
| 313 |
SHADER_MODE_2D, |
| 314 |
SHADER_MODE_3D, |
| 315 |
SHADER_MODE_MAX |
| 316 |
}; |
| 317 |
|
| 318 |
SkeletonShaderRD shader; |
| 319 |
RID version; |
| 320 |
RID version_shader[SHADER_MODE_MAX]; |
| 321 |
RID pipeline[SHADER_MODE_MAX]; |
| 322 |
|
| 323 |
RID default_skeleton_uniform_set; |
| 324 |
} skeleton_shader; |
| 325 |
|
| 326 |
struct Skeleton { |
| 327 |
bool use_2d = false; |
| 328 |
int size = 0; |
| 329 |
LocalVector<float> data; |
| 330 |
RID buffer; |
| 331 |
|
| 332 |
bool dirty = false; |
| 333 |
Skeleton *dirty_list = nullptr; |
| 334 |
Transform2D base_transform_2d; |
| 335 |
|
| 336 |
RID uniform_set_3d; |
| 337 |
RID uniform_set_mi; |
| 338 |
|
| 339 |
uint64_t version = 1; |
| 340 |
|
| 341 |
Dependency dependency; |
| 342 |
}; |
| 343 |
|
| 344 |
mutable RID_Owner<Skeleton, true> skeleton_owner; |
| 345 |
|
| 346 |
_FORCE_INLINE_ void _skeleton_make_dirty(Skeleton *skeleton); |
| 347 |
|
| 348 |
Skeleton *skeleton_dirty_list = nullptr; |
| 349 |
|
| 350 |
enum AttributeLocation { |
| 351 |
ATTRIBUTE_LOCATION_PREV_VERTEX = 12, |
| 352 |
ATTRIBUTE_LOCATION_PREV_NORMAL = 13, |
| 353 |
ATTRIBUTE_LOCATION_PREV_TANGENT = 14 |
| 354 |
}; |
| 355 |
|
| 356 |
public: |
| 357 |
static MeshStorage *get_singleton(); |
| 358 |
|
| 359 |
MeshStorage(); |
| 360 |
virtual ~MeshStorage(); |
| 361 |
|
| 362 |
bool free(RID p_rid); |
| 363 |
|
| 364 |
RID get_default_rd_storage_buffer() const { return default_rd_storage_buffer; } |
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
bool owns_mesh(RID p_rid) { return mesh_owner.owns(p_rid); } |
| 369 |
|
| 370 |
virtual RID mesh_allocate() override; |
| 371 |
virtual void mesh_initialize(RID p_mesh) override; |
| 372 |
virtual void mesh_free(RID p_rid) override; |
| 373 |
|
| 374 |
virtual void mesh_set_blend_shape_count(RID p_mesh, int p_blend_shape_count) override; |
| 375 |
|
| 376 |
|
| 377 |
virtual void mesh_add_surface(RID p_mesh, const RS::SurfaceData &p_surface) override; |
| 378 |
|
| 379 |
virtual int mesh_get_blend_shape_count(RID p_mesh) const override; |
| 380 |
|
| 381 |
virtual void mesh_set_blend_shape_mode(RID p_mesh, RS::BlendShapeMode p_mode) override; |
| 382 |
virtual RS::BlendShapeMode mesh_get_blend_shape_mode(RID p_mesh) const override; |
| 383 |
|
| 384 |
virtual void mesh_surface_update_vertex_region(RID p_mesh, int p_surface, int p_offset, const Vector<uint8_t> &p_data) override; |
| 385 |
virtual void mesh_surface_update_attribute_region(RID p_mesh, int p_surface, int p_offset, const Vector<uint8_t> &p_data) override; |
| 386 |
virtual void mesh_surface_update_skin_region(RID p_mesh, int p_surface, int p_offset, const Vector<uint8_t> &p_data) override; |
| 387 |
virtual void mesh_surface_update_index_region(RID p_mesh, int p_surface, int p_offset, const Vector<uint8_t> &p_data) override; |
| 388 |
|
| 389 |
virtual void mesh_surface_set_material(RID p_mesh, int p_surface, RID p_material) override; |
| 390 |
virtual RID mesh_surface_get_material(RID p_mesh, int p_surface) const override; |
| 391 |
|
| 392 |
virtual RS::SurfaceData mesh_get_surface(RID p_mesh, int p_surface) const override; |
| 393 |
|
| 394 |
virtual int mesh_get_surface_count(RID p_mesh) const override; |
| 395 |
|
| 396 |
virtual void mesh_set_custom_aabb(RID p_mesh, const AABB &p_aabb) override; |
| 397 |
virtual AABB mesh_get_custom_aabb(RID p_mesh) const override; |
| 398 |
|
| 399 |
virtual AABB mesh_get_aabb(RID p_mesh, RID p_skeleton = RID()) override; |
| 400 |
virtual void mesh_set_shadow_mesh(RID p_mesh, RID p_shadow_mesh) override; |
| 401 |
|
| 402 |
virtual void mesh_set_path(RID p_mesh, const String &p_path) override; |
| 403 |
virtual String mesh_get_path(RID p_mesh) const override; |
| 404 |
|
| 405 |
virtual void mesh_clear(RID p_mesh) override; |
| 406 |
virtual void mesh_surface_remove(RID p_mesh, int p_surface) override; |
| 407 |
|
| 408 |
virtual void mesh_debug_usage(List<RS::MeshInfo> *r_info) override; |
| 409 |
|
| 410 |
virtual bool mesh_needs_instance(RID p_mesh, bool p_has_skeleton) override; |
| 411 |
|
| 412 |
_FORCE_INLINE_ const RID *mesh_get_surface_count_and_materials(RID p_mesh, uint32_t &r_surface_count) { |
| 413 |
Mesh *mesh = mesh_owner.get_or_null(p_mesh); |
| 414 |
ERR_FAIL_NULL_V(mesh, nullptr); |
| 415 |
r_surface_count = mesh->surface_count; |
| 416 |
if (r_surface_count == 0) { |
| 417 |
return nullptr; |
| 418 |
} |
| 419 |
if (mesh->material_cache.is_empty()) { |
| 420 |
mesh->material_cache.resize(mesh->surface_count); |
| 421 |
for (uint32_t i = 0; i < r_surface_count; i++) { |
| 422 |
mesh->material_cache.write[i] = mesh->surfaces[i]->material; |
| 423 |
} |
| 424 |
} |
| 425 |
|
| 426 |
return mesh->material_cache.ptr(); |
| 427 |
} |
| 428 |
|
| 429 |
_FORCE_INLINE_ void *mesh_get_surface(RID p_mesh, uint32_t p_surface_index) { |
| 430 |
Mesh *mesh = mesh_owner.get_or_null(p_mesh); |
| 431 |
ERR_FAIL_NULL_V(mesh, nullptr); |
| 432 |
ERR_FAIL_UNSIGNED_INDEX_V(p_surface_index, mesh->surface_count, nullptr); |
| 433 |
|
| 434 |
return mesh->surfaces[p_surface_index]; |
| 435 |
} |
| 436 |
|
| 437 |
_FORCE_INLINE_ RID mesh_get_shadow_mesh(RID p_mesh) { |
| 438 |
Mesh *mesh = mesh_owner.get_or_null(p_mesh); |
| 439 |
ERR_FAIL_NULL_V(mesh, RID()); |
| 440 |
|
| 441 |
return mesh->shadow_mesh; |
| 442 |
} |
| 443 |
|
| 444 |
_FORCE_INLINE_ RS::PrimitiveType mesh_surface_get_primitive(void *p_surface) { |
| 445 |
Mesh::Surface *surface = reinterpret_cast<Mesh::Surface *>(p_surface); |
| 446 |
return surface->primitive; |
| 447 |
} |
| 448 |
|
| 449 |
_FORCE_INLINE_ uint32_t mesh_surface_get_vertex_count(void *p_surface) { |
| 450 |
Mesh::Surface *surface = reinterpret_cast<Mesh::Surface *>(p_surface); |
| 451 |
return surface->vertex_count; |
| 452 |
} |
| 453 |
|
| 454 |
_FORCE_INLINE_ bool mesh_surface_has_lod(void *p_surface) const { |
| 455 |
Mesh::Surface *s = reinterpret_cast<Mesh::Surface *>(p_surface); |
| 456 |
return s->lod_count > 0; |
| 457 |
} |
| 458 |
|
| 459 |
_FORCE_INLINE_ uint32_t mesh_surface_get_vertices_drawn_count(void *p_surface) const { |
| 460 |
Mesh::Surface *s = reinterpret_cast<Mesh::Surface *>(p_surface); |
| 461 |
return s->index_count ? s->index_count : s->vertex_count; |
| 462 |
} |
| 463 |
|
| 464 |
_FORCE_INLINE_ AABB mesh_surface_get_aabb(void *p_surface) { |
| 465 |
Mesh::Surface *s = reinterpret_cast<Mesh::Surface *>(p_surface); |
| 466 |
return s->aabb; |
| 467 |
} |
| 468 |
|
| 469 |
_FORCE_INLINE_ uint64_t mesh_surface_get_format(void *p_surface) { |
| 470 |
Mesh::Surface *s = reinterpret_cast<Mesh::Surface *>(p_surface); |
| 471 |
return s->format; |
| 472 |
} |
| 473 |
|
| 474 |
_FORCE_INLINE_ Vector4 mesh_surface_get_uv_scale(void *p_surface) { |
| 475 |
Mesh::Surface *s = reinterpret_cast<Mesh::Surface *>(p_surface); |
| 476 |
return s->uv_scale; |
| 477 |
} |
| 478 |
|
| 479 |
_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 { |
| 480 |
Mesh::Surface *s = reinterpret_cast<Mesh::Surface *>(p_surface); |
| 481 |
|
| 482 |
int32_t current_lod = -1; |
| 483 |
r_index_count = s->index_count; |
| 484 |
for (uint32_t i = 0; i < s->lod_count; i++) { |
| 485 |
float screen_size = s->lods[i].edge_length * p_model_scale / p_distance_threshold; |
| 486 |
if (screen_size > p_mesh_lod_threshold) { |
| 487 |
break; |
| 488 |
} |
| 489 |
current_lod = i; |
| 490 |
} |
| 491 |
if (current_lod == -1) { |
| 492 |
return 0; |
| 493 |
} else { |
| 494 |
r_index_count = s->lods[current_lod].index_count; |
| 495 |
return current_lod + 1; |
| 496 |
} |
| 497 |
} |
| 498 |
|
| 499 |
_FORCE_INLINE_ RID mesh_surface_get_index_array(void *p_surface, uint32_t p_lod) const { |
| 500 |
Mesh::Surface *s = reinterpret_cast<Mesh::Surface *>(p_surface); |
| 501 |
|
| 502 |
if (p_lod == 0) { |
| 503 |
return s->index_array; |
| 504 |
} else { |
| 505 |
return s->lods[p_lod - 1].index_array; |
| 506 |
} |
| 507 |
} |
| 508 |
|
| 509 |
_FORCE_INLINE_ void mesh_surface_get_vertex_arrays_and_format(void *p_surface, uint64_t p_input_mask, bool p_input_motion_vectors, bool p_point_size_emulated, RID &r_vertex_array_rd, RD::VertexFormatID &r_vertex_format) { |
| 510 |
Mesh::Surface *s = reinterpret_cast<Mesh::Surface *>(p_surface); |
| 511 |
|
| 512 |
s->version_lock.lock(); |
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
for (uint32_t i = 0; i < s->version_count; i++) { |
| 517 |
if (s->versions[i].input_mask != p_input_mask || s->versions[i].input_motion_vectors != p_input_motion_vectors || s->versions[i].point_size_emulated != p_point_size_emulated) { |
| 518 |
|
| 519 |
continue; |
| 520 |
} |
| 521 |
|
| 522 |
|
| 523 |
r_vertex_format = s->versions[i].vertex_format; |
| 524 |
r_vertex_array_rd = s->versions[i].vertex_array; |
| 525 |
s->version_lock.unlock(); |
| 526 |
return; |
| 527 |
} |
| 528 |
|
| 529 |
uint32_t version = s->version_count; |
| 530 |
s->version_count++; |
| 531 |
s->versions = (Mesh::Surface::Version *)memrealloc(s->versions, sizeof(Mesh::Surface::Version) * s->version_count); |
| 532 |
|
| 533 |
_mesh_surface_generate_version_for_input_mask(s->versions[version], s, p_input_mask, p_input_motion_vectors, p_point_size_emulated); |
| 534 |
|
| 535 |
r_vertex_format = s->versions[version].vertex_format; |
| 536 |
r_vertex_array_rd = s->versions[version].vertex_array; |
| 537 |
|
| 538 |
s->version_lock.unlock(); |
| 539 |
} |
| 540 |
|
| 541 |
_FORCE_INLINE_ void mesh_instance_surface_get_vertex_arrays_and_format(RID p_mesh_instance, uint64_t p_surface_index, uint64_t p_input_mask, bool p_input_motion_vectors, bool p_point_size_emulated, RID &r_vertex_array_rd, RD::VertexFormatID &r_vertex_format) { |
| 542 |
MeshInstance *mi = mesh_instance_owner.get_or_null(p_mesh_instance); |
| 543 |
ERR_FAIL_NULL(mi); |
| 544 |
Mesh *mesh = mi->mesh; |
| 545 |
ERR_FAIL_UNSIGNED_INDEX(p_surface_index, mesh->surface_count); |
| 546 |
|
| 547 |
MeshInstance::Surface *mis = &mi->surfaces[p_surface_index]; |
| 548 |
Mesh::Surface *s = mesh->surfaces[p_surface_index]; |
| 549 |
uint32_t current_buffer = mis->current_buffer; |
| 550 |
|
| 551 |
|
| 552 |
uint32_t previous_buffer = p_input_motion_vectors && (RSG::rasterizer->get_frame_number() == mis->last_change) ? mis->previous_buffer : current_buffer; |
| 553 |
|
| 554 |
s->version_lock.lock(); |
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
for (uint32_t i = 0; i < mis->version_count; i++) { |
| 559 |
if (mis->versions[i].input_mask != p_input_mask || mis->versions[i].input_motion_vectors != p_input_motion_vectors) { |
| 560 |
|
| 561 |
continue; |
| 562 |
} |
| 563 |
|
| 564 |
if (mis->versions[i].current_buffer != current_buffer || mis->versions[i].previous_buffer != previous_buffer) { |
| 565 |
|
| 566 |
continue; |
| 567 |
} |
| 568 |
|
| 569 |
|
| 570 |
r_vertex_format = mis->versions[i].vertex_format; |
| 571 |
r_vertex_array_rd = mis->versions[i].vertex_array; |
| 572 |
s->version_lock.unlock(); |
| 573 |
return; |
| 574 |
} |
| 575 |
|
| 576 |
uint32_t version = mis->version_count; |
| 577 |
mis->version_count++; |
| 578 |
mis->versions = (Mesh::Surface::Version *)memrealloc(mis->versions, sizeof(Mesh::Surface::Version) * mis->version_count); |
| 579 |
|
| 580 |
_mesh_surface_generate_version_for_input_mask(mis->versions[version], s, p_input_mask, p_input_motion_vectors, p_point_size_emulated, mis, current_buffer, previous_buffer); |
| 581 |
|
| 582 |
r_vertex_format = mis->versions[version].vertex_format; |
| 583 |
r_vertex_array_rd = mis->versions[version].vertex_array; |
| 584 |
|
| 585 |
s->version_lock.unlock(); |
| 586 |
} |
| 587 |
|
| 588 |
_FORCE_INLINE_ RID mesh_get_default_rd_buffer(DefaultRDBuffer p_buffer) { |
| 589 |
ERR_FAIL_INDEX_V(p_buffer, DEFAULT_RD_BUFFER_MAX, RID()); |
| 590 |
return mesh_default_rd_buffers[p_buffer]; |
| 591 |
} |
| 592 |
|
| 593 |
_FORCE_INLINE_ uint32_t mesh_surface_get_render_pass_index(RID p_mesh, uint32_t p_surface_index, uint64_t p_render_pass, uint32_t *r_index) { |
| 594 |
Mesh *mesh = mesh_owner.get_or_null(p_mesh); |
| 595 |
Mesh::Surface *s = mesh->surfaces[p_surface_index]; |
| 596 |
|
| 597 |
if (s->render_pass != p_render_pass) { |
| 598 |
(*r_index)++; |
| 599 |
s->render_pass = p_render_pass; |
| 600 |
s->render_index = *r_index; |
| 601 |
} |
| 602 |
|
| 603 |
return s->render_index; |
| 604 |
} |
| 605 |
|
| 606 |
_FORCE_INLINE_ uint32_t mesh_surface_get_multimesh_render_pass_index(RID p_mesh, uint32_t p_surface_index, uint64_t p_render_pass, uint32_t *r_index) { |
| 607 |
Mesh *mesh = mesh_owner.get_or_null(p_mesh); |
| 608 |
Mesh::Surface *s = mesh->surfaces[p_surface_index]; |
| 609 |
|
| 610 |
if (s->multimesh_render_pass != p_render_pass) { |
| 611 |
(*r_index)++; |
| 612 |
s->multimesh_render_pass = p_render_pass; |
| 613 |
s->multimesh_render_index = *r_index; |
| 614 |
} |
| 615 |
|
| 616 |
return s->multimesh_render_index; |
| 617 |
} |
| 618 |
|
| 619 |
_FORCE_INLINE_ uint32_t mesh_surface_get_particles_render_pass_index(RID p_mesh, uint32_t p_surface_index, uint64_t p_render_pass, uint32_t *r_index) { |
| 620 |
Mesh *mesh = mesh_owner.get_or_null(p_mesh); |
| 621 |
Mesh::Surface *s = mesh->surfaces[p_surface_index]; |
| 622 |
|
| 623 |
if (s->particles_render_pass != p_render_pass) { |
| 624 |
(*r_index)++; |
| 625 |
s->particles_render_pass = p_render_pass; |
| 626 |
s->particles_render_index = *r_index; |
| 627 |
} |
| 628 |
|
| 629 |
return s->particles_render_index; |
| 630 |
} |
| 631 |
|
| 632 |
_FORCE_INLINE_ RD::VertexFormatID mesh_surface_get_vertex_format(void *p_surface, uint64_t p_input_mask, bool p_instanced_surface, bool p_input_motion_vectors, bool p_point_size_emulated) { |
| 633 |
Mesh::Surface *s = reinterpret_cast<Mesh::Surface *>(p_surface); |
| 634 |
uint32_t position_stride = 0; |
| 635 |
return _mesh_surface_generate_vertex_format(s->format, p_input_mask, p_instanced_surface, p_input_motion_vectors, p_point_size_emulated, position_stride); |
| 636 |
} |
| 637 |
|
| 638 |
Dependency *mesh_get_dependency(RID p_mesh) const; |
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
bool owns_mesh_instance(RID p_rid) const { return mesh_instance_owner.owns(p_rid); } |
| 643 |
|
| 644 |
virtual RID mesh_instance_create(RID p_base) override; |
| 645 |
virtual void mesh_instance_free(RID p_rid) override; |
| 646 |
virtual void mesh_instance_set_skeleton(RID p_mesh_instance, RID p_skeleton) override; |
| 647 |
virtual void mesh_instance_set_blend_shape_weight(RID p_mesh_instance, int p_shape, float p_weight) override; |
| 648 |
virtual void mesh_instance_check_for_update(RID p_mesh_instance) override; |
| 649 |
virtual void mesh_instance_set_canvas_item_transform(RID p_mesh_instance, const Transform2D &p_transform) override; |
| 650 |
virtual void update_mesh_instances() override; |
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
bool owns_multimesh(RID p_rid) { return multimesh_owner.owns(p_rid); } |
| 655 |
|
| 656 |
virtual RID _multimesh_allocate() override; |
| 657 |
virtual void _multimesh_initialize(RID p_multimesh) override; |
| 658 |
virtual void _multimesh_free(RID p_rid) override; |
| 659 |
|
| 660 |
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, bool p_use_indirect = false) override; |
| 661 |
virtual int _multimesh_get_instance_count(RID p_multimesh) const override; |
| 662 |
|
| 663 |
virtual void _multimesh_set_mesh(RID p_multimesh, RID p_mesh) override; |
| 664 |
virtual void _multimesh_instance_set_transform(RID p_multimesh, int p_index, const Transform3D &p_transform) override; |
| 665 |
virtual void _multimesh_instance_set_transform_2d(RID p_multimesh, int p_index, const Transform2D &p_transform) override; |
| 666 |
virtual void _multimesh_instance_set_color(RID p_multimesh, int p_index, const Color &p_color) override; |
| 667 |
virtual void _multimesh_instance_set_custom_data(RID p_multimesh, int p_index, const Color &p_color) override; |
| 668 |
|
| 669 |
virtual RID _multimesh_get_mesh(RID p_multimesh) const override; |
| 670 |
|
| 671 |
virtual Transform3D _multimesh_instance_get_transform(RID p_multimesh, int p_index) const override; |
| 672 |
virtual Transform2D _multimesh_instance_get_transform_2d(RID p_multimesh, int p_index) const override; |
| 673 |
virtual Color _multimesh_instance_get_color(RID p_multimesh, int p_index) const override; |
| 674 |
virtual Color _multimesh_instance_get_custom_data(RID p_multimesh, int p_index) const override; |
| 675 |
|
| 676 |
virtual void _multimesh_set_buffer(RID p_multimesh, const Vector<float> &p_buffer) override; |
| 677 |
virtual RID _multimesh_get_command_buffer_rd_rid(RID p_multimesh) const override; |
| 678 |
virtual RID _multimesh_get_buffer_rd_rid(RID p_multimesh) const override; |
| 679 |
virtual Vector<float> _multimesh_get_buffer(RID p_multimesh) const override; |
| 680 |
|
| 681 |
virtual void _multimesh_set_visible_instances(RID p_multimesh, int p_visible) override; |
| 682 |
virtual int _multimesh_get_visible_instances(RID p_multimesh) const override; |
| 683 |
|
| 684 |
virtual void _multimesh_set_custom_aabb(RID p_multimesh, const AABB &p_aabb) override; |
| 685 |
virtual AABB _multimesh_get_custom_aabb(RID p_multimesh) const override; |
| 686 |
|
| 687 |
virtual AABB _multimesh_get_aabb(RID p_multimesh) override; |
| 688 |
|
| 689 |
virtual MultiMeshInterpolator *_multimesh_get_interpolator(RID p_multimesh) const override; |
| 690 |
|
| 691 |
void _update_dirty_multimeshes(); |
| 692 |
void _multimesh_get_motion_vectors_offsets(RID p_multimesh, uint32_t &r_current_offset, uint32_t &r_prev_offset); |
| 693 |
bool _multimesh_uses_motion_vectors_offsets(RID p_multimesh); |
| 694 |
bool _multimesh_uses_motion_vectors(RID p_multimesh); |
| 695 |
|
| 696 |
_FORCE_INLINE_ bool multimesh_uses_indirect(RID p_multimesh) const { |
| 697 |
MultiMesh *multimesh = multimesh_owner.get_or_null(p_multimesh); |
| 698 |
ERR_FAIL_NULL_V(multimesh, false); |
| 699 |
return multimesh->indirect; |
| 700 |
} |
| 701 |
|
| 702 |
_FORCE_INLINE_ RS::MultimeshTransformFormat multimesh_get_transform_format(RID p_multimesh) const { |
| 703 |
MultiMesh *multimesh = multimesh_owner.get_or_null(p_multimesh); |
| 704 |
ERR_FAIL_NULL_V(multimesh, RS::MULTIMESH_TRANSFORM_3D); |
| 705 |
return multimesh->xform_format; |
| 706 |
} |
| 707 |
|
| 708 |
_FORCE_INLINE_ bool multimesh_uses_colors(RID p_multimesh) const { |
| 709 |
MultiMesh *multimesh = multimesh_owner.get_or_null(p_multimesh); |
| 710 |
ERR_FAIL_NULL_V(multimesh, false); |
| 711 |
return multimesh->uses_colors; |
| 712 |
} |
| 713 |
|
| 714 |
_FORCE_INLINE_ bool multimesh_uses_custom_data(RID p_multimesh) const { |
| 715 |
MultiMesh *multimesh = multimesh_owner.get_or_null(p_multimesh); |
| 716 |
ERR_FAIL_NULL_V(multimesh, false); |
| 717 |
return multimesh->uses_custom_data; |
| 718 |
} |
| 719 |
|
| 720 |
_FORCE_INLINE_ uint32_t multimesh_get_instances_to_draw(RID p_multimesh) const { |
| 721 |
MultiMesh *multimesh = multimesh_owner.get_or_null(p_multimesh); |
| 722 |
ERR_FAIL_NULL_V(multimesh, 0); |
| 723 |
if (multimesh->visible_instances >= 0) { |
| 724 |
return multimesh->visible_instances; |
| 725 |
} |
| 726 |
return multimesh->instances; |
| 727 |
} |
| 728 |
|
| 729 |
_FORCE_INLINE_ RID multimesh_get_3d_uniform_set(RID p_multimesh, RID p_shader, uint32_t p_set) const { |
| 730 |
MultiMesh *multimesh = multimesh_owner.get_or_null(p_multimesh); |
| 731 |
if (multimesh == nullptr) { |
| 732 |
return RID(); |
| 733 |
} |
| 734 |
if (!multimesh->uniform_set_3d.is_valid()) { |
| 735 |
if (!multimesh->buffer.is_valid()) { |
| 736 |
return RID(); |
| 737 |
} |
| 738 |
Vector<RD::Uniform> uniforms; |
| 739 |
RD::Uniform u; |
| 740 |
u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER; |
| 741 |
u.binding = 0; |
| 742 |
u.append_id(multimesh->buffer); |
| 743 |
uniforms.push_back(u); |
| 744 |
multimesh->uniform_set_3d = RD::get_singleton()->uniform_set_create(uniforms, p_shader, p_set); |
| 745 |
} |
| 746 |
|
| 747 |
return multimesh->uniform_set_3d; |
| 748 |
} |
| 749 |
|
| 750 |
_FORCE_INLINE_ RID multimesh_get_2d_uniform_set(RID p_multimesh, RID p_shader, uint32_t p_set) const { |
| 751 |
MultiMesh *multimesh = multimesh_owner.get_or_null(p_multimesh); |
| 752 |
if (multimesh == nullptr) { |
| 753 |
return RID(); |
| 754 |
} |
| 755 |
if (!multimesh->uniform_set_2d.is_valid()) { |
| 756 |
if (!multimesh->buffer.is_valid()) { |
| 757 |
return RID(); |
| 758 |
} |
| 759 |
Vector<RD::Uniform> uniforms; |
| 760 |
RD::Uniform u; |
| 761 |
u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER; |
| 762 |
u.binding = 0; |
| 763 |
u.append_id(multimesh->buffer); |
| 764 |
uniforms.push_back(u); |
| 765 |
multimesh->uniform_set_2d = RD::get_singleton()->uniform_set_create(uniforms, p_shader, p_set); |
| 766 |
} |
| 767 |
|
| 768 |
return multimesh->uniform_set_2d; |
| 769 |
} |
| 770 |
|
| 771 |
Dependency *multimesh_get_dependency(RID p_multimesh) const; |
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
bool owns_skeleton(RID p_rid) const { return skeleton_owner.owns(p_rid); } |
| 776 |
|
| 777 |
virtual RID skeleton_allocate() override; |
| 778 |
virtual void skeleton_initialize(RID p_skeleton) override; |
| 779 |
virtual void skeleton_free(RID p_rid) override; |
| 780 |
|
| 781 |
virtual void skeleton_allocate_data(RID p_skeleton, int p_bones, bool p_2d_skeleton = false) override; |
| 782 |
virtual void skeleton_set_base_transform_2d(RID p_skeleton, const Transform2D &p_base_transform) override; |
| 783 |
virtual int skeleton_get_bone_count(RID p_skeleton) const override; |
| 784 |
virtual void skeleton_bone_set_transform(RID p_skeleton, int p_bone, const Transform3D &p_transform) override; |
| 785 |
virtual Transform3D skeleton_bone_get_transform(RID p_skeleton, int p_bone) const override; |
| 786 |
virtual void skeleton_bone_set_transform_2d(RID p_skeleton, int p_bone, const Transform2D &p_transform) override; |
| 787 |
virtual Transform2D skeleton_bone_get_transform_2d(RID p_skeleton, int p_bone) const override; |
| 788 |
|
| 789 |
virtual void skeleton_update_dependency(RID p_skeleton, DependencyTracker *p_instance) override; |
| 790 |
|
| 791 |
void _update_dirty_skeletons(); |
| 792 |
|
| 793 |
_FORCE_INLINE_ bool skeleton_is_valid(RID p_skeleton) { |
| 794 |
return skeleton_owner.get_or_null(p_skeleton) != nullptr; |
| 795 |
} |
| 796 |
|
| 797 |
_FORCE_INLINE_ RID skeleton_get_3d_uniform_set(RID p_skeleton, RID p_shader, uint32_t p_set) const { |
| 798 |
Skeleton *skeleton = skeleton_owner.get_or_null(p_skeleton); |
| 799 |
ERR_FAIL_NULL_V(skeleton, RID()); |
| 800 |
if (skeleton->size == 0) { |
| 801 |
return RID(); |
| 802 |
} |
| 803 |
if (skeleton->use_2d) { |
| 804 |
return RID(); |
| 805 |
} |
| 806 |
if (!skeleton->uniform_set_3d.is_valid()) { |
| 807 |
Vector<RD::Uniform> uniforms; |
| 808 |
RD::Uniform u; |
| 809 |
u.uniform_type = RD::UNIFORM_TYPE_STORAGE_BUFFER; |
| 810 |
u.binding = 0; |
| 811 |
u.append_id(skeleton->buffer); |
| 812 |
uniforms.push_back(u); |
| 813 |
skeleton->uniform_set_3d = RD::get_singleton()->uniform_set_create(uniforms, p_shader, p_set); |
| 814 |
} |
| 815 |
|
| 816 |
return skeleton->uniform_set_3d; |
| 817 |
} |
| 818 |
}; |
| 819 |
|
| 820 |
} |