mesh_storage.h 27 KB

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