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