mesh_storage.h 27 KB

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