mesh_storage.h 28 KB

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