mesh_storage.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  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. #pragma once
  31. #ifdef GLES3_ENABLED
  32. #include "core/templates/local_vector.h"
  33. #include "core/templates/rid_owner.h"
  34. #include "core/templates/self_list.h"
  35. #include "drivers/gles3/shaders/skeleton.glsl.gen.h"
  36. #include "servers/rendering/storage/mesh_storage.h"
  37. #include "servers/rendering/storage/utilities.h"
  38. #include "platform_gl.h"
  39. namespace GLES3 {
  40. struct MeshInstance;
  41. struct Mesh {
  42. struct Surface {
  43. struct Attrib {
  44. bool enabled;
  45. bool integer;
  46. GLint size;
  47. GLenum type;
  48. GLboolean normalized;
  49. GLsizei stride;
  50. uint32_t offset;
  51. };
  52. RS::PrimitiveType primitive = RS::PRIMITIVE_POINTS;
  53. uint64_t format = 0;
  54. GLuint vertex_buffer = 0;
  55. GLuint attribute_buffer = 0;
  56. GLuint skin_buffer = 0;
  57. uint32_t vertex_count = 0;
  58. uint32_t vertex_buffer_size = 0;
  59. uint32_t attribute_buffer_size = 0;
  60. uint32_t skin_buffer_size = 0;
  61. // Cache vertex arrays so they can be created
  62. struct Version {
  63. uint32_t input_mask = 0;
  64. GLuint vertex_array = 0;
  65. Attrib attribs[RS::ARRAY_MAX];
  66. };
  67. SpinLock version_lock; //needed to access versions
  68. Version *versions = nullptr; //allocated on demand
  69. uint32_t version_count = 0;
  70. GLuint index_buffer = 0;
  71. uint32_t index_count = 0;
  72. uint32_t index_buffer_size = 0;
  73. struct Wireframe {
  74. GLuint index_buffer = 0;
  75. uint32_t index_count = 0;
  76. uint32_t index_buffer_size = 0;
  77. };
  78. Wireframe *wireframe = nullptr;
  79. struct LOD {
  80. float edge_length = 0.0;
  81. uint32_t index_count = 0;
  82. uint32_t index_buffer_size = 0;
  83. GLuint index_buffer = 0;
  84. };
  85. LOD *lods = nullptr;
  86. uint32_t lod_count = 0;
  87. AABB aabb;
  88. Vector<AABB> bone_aabbs;
  89. // Transform used in runtime bone AABBs compute.
  90. // As bone AABBs are saved in Mesh space, but bones animation is in Skeleton space.
  91. Transform3D mesh_to_skeleton_xform;
  92. Vector4 uv_scale;
  93. struct BlendShape {
  94. GLuint vertex_buffer = 0;
  95. GLuint vertex_array = 0;
  96. };
  97. BlendShape *blend_shapes = nullptr;
  98. GLuint skeleton_vertex_array = 0;
  99. RID material;
  100. };
  101. uint32_t blend_shape_count = 0;
  102. RS::BlendShapeMode blend_shape_mode = RS::BLEND_SHAPE_MODE_NORMALIZED;
  103. Surface **surfaces = nullptr;
  104. uint32_t surface_count = 0;
  105. bool has_bone_weights = false;
  106. AABB aabb;
  107. AABB custom_aabb;
  108. uint64_t skeleton_aabb_version = 0;
  109. Vector<RID> material_cache;
  110. List<MeshInstance *> instances;
  111. RID shadow_mesh;
  112. HashSet<Mesh *> shadow_owners;
  113. String path;
  114. Dependency dependency;
  115. };
  116. /* Mesh Instance */
  117. struct MeshInstance {
  118. Mesh *mesh = nullptr;
  119. RID skeleton;
  120. struct Surface {
  121. GLuint vertex_buffers[2] = { 0, 0 };
  122. GLuint vertex_arrays[2] = { 0, 0 };
  123. GLuint vertex_buffer = 0;
  124. int vertex_stride_cache = 0;
  125. int vertex_size_cache = 0;
  126. int vertex_normal_offset_cache = 0;
  127. int vertex_tangent_offset_cache = 0;
  128. uint64_t format_cache = 0;
  129. Mesh::Surface::Version *versions = nullptr; //allocated on demand
  130. uint32_t version_count = 0;
  131. };
  132. LocalVector<Surface> surfaces;
  133. LocalVector<float> blend_weights;
  134. List<MeshInstance *>::Element *I = nullptr; //used to erase itself
  135. uint64_t skeleton_version = 0;
  136. bool dirty = false;
  137. bool weights_dirty = false;
  138. SelfList<MeshInstance> weight_update_list;
  139. SelfList<MeshInstance> array_update_list;
  140. Transform2D canvas_item_transform_2d;
  141. MeshInstance() :
  142. weight_update_list(this), array_update_list(this) {}
  143. };
  144. /* MultiMesh */
  145. struct MultiMesh {
  146. RID mesh;
  147. int instances = 0;
  148. RS::MultimeshTransformFormat xform_format = RS::MULTIMESH_TRANSFORM_3D;
  149. bool uses_colors = false;
  150. bool uses_custom_data = false;
  151. int visible_instances = -1;
  152. AABB aabb;
  153. AABB custom_aabb;
  154. bool aabb_dirty = false;
  155. bool buffer_set = false;
  156. uint32_t stride_cache = 0;
  157. uint32_t color_offset_cache = 0;
  158. uint32_t custom_data_offset_cache = 0;
  159. Vector<float> data_cache; //used if individual setting is used
  160. bool *data_cache_dirty_regions = nullptr;
  161. uint32_t data_cache_used_dirty_regions = 0;
  162. GLuint buffer = 0;
  163. bool dirty = false;
  164. MultiMesh *dirty_list = nullptr;
  165. RendererMeshStorage::MultiMeshInterpolator interpolator;
  166. Dependency dependency;
  167. };
  168. struct Skeleton {
  169. bool use_2d = false;
  170. int size = 0;
  171. int height = 0;
  172. LocalVector<float> data;
  173. bool dirty = false;
  174. Skeleton *dirty_list = nullptr;
  175. Transform2D base_transform_2d;
  176. GLuint transforms_texture = 0;
  177. uint64_t version = 1;
  178. Dependency dependency;
  179. };
  180. class MeshStorage : public RendererMeshStorage {
  181. private:
  182. static MeshStorage *singleton;
  183. struct {
  184. SkeletonShaderGLES3 shader;
  185. RID shader_version;
  186. } skeleton_shader;
  187. /* Mesh */
  188. mutable RID_Owner<Mesh, true> mesh_owner;
  189. void _mesh_surface_generate_version_for_input_mask(Mesh::Surface::Version &v, Mesh::Surface *s, uint64_t p_input_mask, MeshInstance::Surface *mis = nullptr);
  190. void _mesh_surface_clear(Mesh *mesh, int p_surface);
  191. /* Mesh Instance API */
  192. mutable RID_Owner<MeshInstance> mesh_instance_owner;
  193. void _mesh_instance_clear(MeshInstance *mi);
  194. void _mesh_instance_add_surface(MeshInstance *mi, Mesh *mesh, uint32_t p_surface);
  195. void _mesh_instance_remove_surface(MeshInstance *mi, int p_surface);
  196. void _blend_shape_bind_mesh_instance_buffer(MeshInstance *p_mi, uint32_t p_surface);
  197. SelfList<MeshInstance>::List dirty_mesh_instance_weights;
  198. SelfList<MeshInstance>::List dirty_mesh_instance_arrays;
  199. /* MultiMesh */
  200. mutable RID_Owner<MultiMesh, true> multimesh_owner;
  201. MultiMesh *multimesh_dirty_list = nullptr;
  202. _FORCE_INLINE_ void _multimesh_make_local(MultiMesh *multimesh) const;
  203. _FORCE_INLINE_ void _multimesh_mark_dirty(MultiMesh *multimesh, int p_index, bool p_aabb);
  204. _FORCE_INLINE_ void _multimesh_mark_all_dirty(MultiMesh *multimesh, bool p_data, bool p_aabb);
  205. _FORCE_INLINE_ void _multimesh_re_create_aabb(MultiMesh *multimesh, const float *p_data, int p_instances);
  206. /* Skeleton */
  207. mutable RID_Owner<Skeleton, true> skeleton_owner;
  208. _FORCE_INLINE_ void _skeleton_make_dirty(Skeleton *skeleton);
  209. void _compute_skeleton(MeshInstance *p_mi, Skeleton *p_sk, uint32_t p_surface);
  210. Skeleton *skeleton_dirty_list = nullptr;
  211. public:
  212. static MeshStorage *get_singleton();
  213. MeshStorage();
  214. virtual ~MeshStorage();
  215. /* MESH API */
  216. Mesh *get_mesh(RID p_rid) { return mesh_owner.get_or_null(p_rid); }
  217. bool owns_mesh(RID p_rid) { return mesh_owner.owns(p_rid); }
  218. virtual RID mesh_allocate() override;
  219. virtual void mesh_initialize(RID p_rid) override;
  220. virtual void mesh_free(RID p_rid) override;
  221. virtual void mesh_set_blend_shape_count(RID p_mesh, int p_blend_shape_count) override;
  222. virtual bool mesh_needs_instance(RID p_mesh, bool p_has_skeleton) override;
  223. virtual void mesh_add_surface(RID p_mesh, const RS::SurfaceData &p_surface) override;
  224. virtual int mesh_get_blend_shape_count(RID p_mesh) const override;
  225. virtual void mesh_set_blend_shape_mode(RID p_mesh, RS::BlendShapeMode p_mode) override;
  226. virtual RS::BlendShapeMode mesh_get_blend_shape_mode(RID p_mesh) const override;
  227. virtual void mesh_surface_update_vertex_region(RID p_mesh, int p_surface, int p_offset, const Vector<uint8_t> &p_data) override;
  228. virtual void mesh_surface_update_attribute_region(RID p_mesh, int p_surface, int p_offset, const Vector<uint8_t> &p_data) override;
  229. virtual void mesh_surface_update_skin_region(RID p_mesh, int p_surface, int p_offset, const Vector<uint8_t> &p_data) override;
  230. virtual void mesh_surface_update_index_region(RID p_mesh, int p_surface, int p_offset, const Vector<uint8_t> &p_data) override;
  231. virtual void mesh_surface_set_material(RID p_mesh, int p_surface, RID p_material) override;
  232. virtual RID mesh_surface_get_material(RID p_mesh, int p_surface) const override;
  233. virtual RS::SurfaceData mesh_get_surface(RID p_mesh, int p_surface) const override;
  234. virtual int mesh_get_surface_count(RID p_mesh) const override;
  235. virtual void mesh_set_custom_aabb(RID p_mesh, const AABB &p_aabb) override;
  236. virtual AABB mesh_get_custom_aabb(RID p_mesh) const override;
  237. virtual AABB mesh_get_aabb(RID p_mesh, RID p_skeleton = RID()) override;
  238. virtual void mesh_set_path(RID p_mesh, const String &p_path) override;
  239. virtual String mesh_get_path(RID p_mesh) const override;
  240. virtual void mesh_set_shadow_mesh(RID p_mesh, RID p_shadow_mesh) override;
  241. virtual void mesh_clear(RID p_mesh) override;
  242. virtual void mesh_surface_remove(RID p_mesh, int p_surface) override;
  243. virtual void mesh_debug_usage(List<RS::MeshInfo> *r_info) override {}
  244. _FORCE_INLINE_ const RID *mesh_get_surface_count_and_materials(RID p_mesh, uint32_t &r_surface_count) {
  245. Mesh *mesh = mesh_owner.get_or_null(p_mesh);
  246. ERR_FAIL_NULL_V(mesh, nullptr);
  247. r_surface_count = mesh->surface_count;
  248. if (r_surface_count == 0) {
  249. return nullptr;
  250. }
  251. if (mesh->material_cache.is_empty()) {
  252. mesh->material_cache.resize(mesh->surface_count);
  253. for (uint32_t i = 0; i < r_surface_count; i++) {
  254. mesh->material_cache.write[i] = mesh->surfaces[i]->material;
  255. }
  256. }
  257. return mesh->material_cache.ptr();
  258. }
  259. _FORCE_INLINE_ void *mesh_get_surface(RID p_mesh, uint32_t p_surface_index) {
  260. Mesh *mesh = mesh_owner.get_or_null(p_mesh);
  261. ERR_FAIL_NULL_V(mesh, nullptr);
  262. ERR_FAIL_UNSIGNED_INDEX_V(p_surface_index, mesh->surface_count, nullptr);
  263. return mesh->surfaces[p_surface_index];
  264. }
  265. _FORCE_INLINE_ RID mesh_get_shadow_mesh(RID p_mesh) {
  266. Mesh *mesh = mesh_owner.get_or_null(p_mesh);
  267. ERR_FAIL_NULL_V(mesh, RID());
  268. return mesh->shadow_mesh;
  269. }
  270. _FORCE_INLINE_ RS::PrimitiveType mesh_surface_get_primitive(void *p_surface) {
  271. Mesh::Surface *surface = reinterpret_cast<Mesh::Surface *>(p_surface);
  272. return surface->primitive;
  273. }
  274. _FORCE_INLINE_ bool mesh_surface_has_lod(void *p_surface) const {
  275. Mesh::Surface *s = reinterpret_cast<Mesh::Surface *>(p_surface);
  276. return s->lod_count > 0;
  277. }
  278. _FORCE_INLINE_ uint32_t mesh_surface_get_vertices_drawn_count(void *p_surface) const {
  279. Mesh::Surface *s = reinterpret_cast<Mesh::Surface *>(p_surface);
  280. return s->index_count ? s->index_count : s->vertex_count;
  281. }
  282. _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 {
  283. Mesh::Surface *s = reinterpret_cast<Mesh::Surface *>(p_surface);
  284. ERR_FAIL_NULL_V(s, 0);
  285. int32_t current_lod = -1;
  286. r_index_count = s->index_count;
  287. for (uint32_t i = 0; i < s->lod_count; i++) {
  288. float screen_size = s->lods[i].edge_length * p_model_scale / p_distance_threshold;
  289. if (screen_size > p_mesh_lod_threshold) {
  290. break;
  291. }
  292. current_lod = i;
  293. }
  294. if (current_lod == -1) {
  295. return 0;
  296. } else {
  297. r_index_count = s->lods[current_lod].index_count;
  298. return current_lod + 1;
  299. }
  300. }
  301. _FORCE_INLINE_ GLuint mesh_surface_get_index_buffer(void *p_surface, uint32_t p_lod) const {
  302. Mesh::Surface *s = reinterpret_cast<Mesh::Surface *>(p_surface);
  303. if (p_lod == 0) {
  304. return s->index_buffer;
  305. } else {
  306. return s->lods[p_lod - 1].index_buffer;
  307. }
  308. }
  309. _FORCE_INLINE_ GLuint mesh_surface_get_index_buffer_wireframe(void *p_surface) const {
  310. Mesh::Surface *s = reinterpret_cast<Mesh::Surface *>(p_surface);
  311. if (s->wireframe) {
  312. return s->wireframe->index_buffer;
  313. }
  314. return 0;
  315. }
  316. _FORCE_INLINE_ GLenum mesh_surface_get_index_type(void *p_surface) const {
  317. Mesh::Surface *s = reinterpret_cast<Mesh::Surface *>(p_surface);
  318. return (s->vertex_count <= 65536 && s->vertex_count > 0) ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT;
  319. }
  320. // Use this to cache Vertex Array Objects so they are only generated once
  321. _FORCE_INLINE_ void mesh_surface_get_vertex_arrays_and_format(void *p_surface, uint64_t p_input_mask, GLuint &r_vertex_array_gl) {
  322. Mesh::Surface *s = reinterpret_cast<Mesh::Surface *>(p_surface);
  323. s->version_lock.lock();
  324. // There will never be more than 3 or 4 versions, so iterating is the fastest way.
  325. for (uint32_t i = 0; i < s->version_count; i++) {
  326. if (s->versions[i].input_mask != p_input_mask) {
  327. continue;
  328. }
  329. // We have this version, hooray.
  330. r_vertex_array_gl = s->versions[i].vertex_array;
  331. s->version_lock.unlock();
  332. return;
  333. }
  334. uint32_t version = s->version_count;
  335. s->version_count++;
  336. s->versions = (Mesh::Surface::Version *)memrealloc(s->versions, sizeof(Mesh::Surface::Version) * s->version_count);
  337. _mesh_surface_generate_version_for_input_mask(s->versions[version], s, p_input_mask);
  338. r_vertex_array_gl = s->versions[version].vertex_array;
  339. s->version_lock.unlock();
  340. }
  341. /* MESH INSTANCE API */
  342. MeshInstance *get_mesh_instance(RID p_rid) { return mesh_instance_owner.get_or_null(p_rid); }
  343. bool owns_mesh_instance(RID p_rid) { return mesh_instance_owner.owns(p_rid); }
  344. virtual RID mesh_instance_create(RID p_base) override;
  345. virtual void mesh_instance_free(RID p_rid) override;
  346. virtual void mesh_instance_set_skeleton(RID p_mesh_instance, RID p_skeleton) override;
  347. virtual void mesh_instance_set_blend_shape_weight(RID p_mesh_instance, int p_shape, float p_weight) override;
  348. virtual void mesh_instance_check_for_update(RID p_mesh_instance) override;
  349. virtual void mesh_instance_set_canvas_item_transform(RID p_mesh_instance, const Transform2D &p_transform) override;
  350. virtual void update_mesh_instances() override;
  351. // TODO: considering hashing versions with multimesh buffer RID.
  352. // Doing so would allow us to avoid specifying multimesh buffer pointers every frame and may improve performance.
  353. _FORCE_INLINE_ void mesh_instance_surface_get_vertex_arrays_and_format(RID p_mesh_instance, uint32_t p_surface_index, uint64_t p_input_mask, GLuint &r_vertex_array_gl) {
  354. MeshInstance *mi = mesh_instance_owner.get_or_null(p_mesh_instance);
  355. ERR_FAIL_NULL(mi);
  356. Mesh *mesh = mi->mesh;
  357. ERR_FAIL_UNSIGNED_INDEX(p_surface_index, mesh->surface_count);
  358. MeshInstance::Surface *mis = &mi->surfaces[p_surface_index];
  359. Mesh::Surface *s = mesh->surfaces[p_surface_index];
  360. s->version_lock.lock();
  361. //there will never be more than, at much, 3 or 4 versions, so iterating is the fastest way
  362. for (uint32_t i = 0; i < mis->version_count; i++) {
  363. if (mis->versions[i].input_mask != p_input_mask) {
  364. continue;
  365. }
  366. //we have this version, hooray
  367. r_vertex_array_gl = mis->versions[i].vertex_array;
  368. s->version_lock.unlock();
  369. return;
  370. }
  371. uint32_t version = mis->version_count;
  372. mis->version_count++;
  373. mis->versions = (Mesh::Surface::Version *)memrealloc(mis->versions, sizeof(Mesh::Surface::Version) * mis->version_count);
  374. _mesh_surface_generate_version_for_input_mask(mis->versions[version], s, p_input_mask, mis);
  375. r_vertex_array_gl = mis->versions[version].vertex_array;
  376. s->version_lock.unlock();
  377. }
  378. /* MULTIMESH API */
  379. MultiMesh *get_multimesh(RID p_rid) { return multimesh_owner.get_or_null(p_rid); }
  380. bool owns_multimesh(RID p_rid) { return multimesh_owner.owns(p_rid); }
  381. virtual RID _multimesh_allocate() override;
  382. virtual void _multimesh_initialize(RID p_rid) override;
  383. virtual void _multimesh_free(RID p_rid) override;
  384. 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;
  385. virtual int _multimesh_get_instance_count(RID p_multimesh) const override;
  386. virtual void _multimesh_set_mesh(RID p_multimesh, RID p_mesh) override;
  387. virtual void _multimesh_instance_set_transform(RID p_multimesh, int p_index, const Transform3D &p_transform) override;
  388. virtual void _multimesh_instance_set_transform_2d(RID p_multimesh, int p_index, const Transform2D &p_transform) override;
  389. virtual void _multimesh_instance_set_color(RID p_multimesh, int p_index, const Color &p_color) override;
  390. virtual void _multimesh_instance_set_custom_data(RID p_multimesh, int p_index, const Color &p_color) override;
  391. virtual RID _multimesh_get_mesh(RID p_multimesh) const override;
  392. virtual void _multimesh_set_custom_aabb(RID p_multimesh, const AABB &p_aabb) override;
  393. virtual AABB _multimesh_get_custom_aabb(RID p_multimesh) const override;
  394. virtual AABB _multimesh_get_aabb(RID p_multimesh) override;
  395. virtual Transform3D _multimesh_instance_get_transform(RID p_multimesh, int p_index) const override;
  396. virtual Transform2D _multimesh_instance_get_transform_2d(RID p_multimesh, int p_index) const override;
  397. virtual Color _multimesh_instance_get_color(RID p_multimesh, int p_index) const override;
  398. virtual Color _multimesh_instance_get_custom_data(RID p_multimesh, int p_index) const override;
  399. virtual void _multimesh_set_buffer(RID p_multimesh, const Vector<float> &p_buffer) override;
  400. virtual RID _multimesh_get_command_buffer_rd_rid(RID p_multimesh) const override;
  401. virtual RID _multimesh_get_buffer_rd_rid(RID p_multimesh) const override;
  402. virtual Vector<float> _multimesh_get_buffer(RID p_multimesh) const override;
  403. virtual void _multimesh_set_visible_instances(RID p_multimesh, int p_visible) override;
  404. virtual int _multimesh_get_visible_instances(RID p_multimesh) const override;
  405. virtual MultiMeshInterpolator *_multimesh_get_interpolator(RID p_multimesh) const override;
  406. void _update_dirty_multimeshes();
  407. _FORCE_INLINE_ RS::MultimeshTransformFormat multimesh_get_transform_format(RID p_multimesh) const {
  408. MultiMesh *multimesh = multimesh_owner.get_or_null(p_multimesh);
  409. return multimesh->xform_format;
  410. }
  411. _FORCE_INLINE_ bool multimesh_uses_colors(RID p_multimesh) const {
  412. MultiMesh *multimesh = multimesh_owner.get_or_null(p_multimesh);
  413. return multimesh->uses_colors;
  414. }
  415. _FORCE_INLINE_ bool multimesh_uses_custom_data(RID p_multimesh) const {
  416. MultiMesh *multimesh = multimesh_owner.get_or_null(p_multimesh);
  417. return multimesh->uses_custom_data;
  418. }
  419. _FORCE_INLINE_ uint32_t multimesh_get_instances_to_draw(RID p_multimesh) const {
  420. MultiMesh *multimesh = multimesh_owner.get_or_null(p_multimesh);
  421. if (multimesh->visible_instances >= 0) {
  422. return multimesh->visible_instances;
  423. }
  424. return multimesh->instances;
  425. }
  426. _FORCE_INLINE_ GLuint multimesh_get_gl_buffer(RID p_multimesh) const {
  427. MultiMesh *multimesh = multimesh_owner.get_or_null(p_multimesh);
  428. return multimesh->buffer;
  429. }
  430. _FORCE_INLINE_ uint32_t multimesh_get_stride(RID p_multimesh) const {
  431. MultiMesh *multimesh = multimesh_owner.get_or_null(p_multimesh);
  432. return multimesh->stride_cache;
  433. }
  434. _FORCE_INLINE_ uint32_t multimesh_get_color_offset(RID p_multimesh) const {
  435. MultiMesh *multimesh = multimesh_owner.get_or_null(p_multimesh);
  436. return multimesh->color_offset_cache;
  437. }
  438. _FORCE_INLINE_ uint32_t multimesh_get_custom_data_offset(RID p_multimesh) const {
  439. MultiMesh *multimesh = multimesh_owner.get_or_null(p_multimesh);
  440. return multimesh->custom_data_offset_cache;
  441. }
  442. /* SKELETON API */
  443. Skeleton *get_skeleton(RID p_rid) { return skeleton_owner.get_or_null(p_rid); }
  444. bool owns_skeleton(RID p_rid) { return skeleton_owner.owns(p_rid); }
  445. virtual RID skeleton_allocate() override;
  446. virtual void skeleton_initialize(RID p_rid) override;
  447. virtual void skeleton_free(RID p_rid) override;
  448. virtual void skeleton_allocate_data(RID p_skeleton, int p_bones, bool p_2d_skeleton = false) override;
  449. virtual void skeleton_set_base_transform_2d(RID p_skeleton, const Transform2D &p_base_transform) override;
  450. virtual int skeleton_get_bone_count(RID p_skeleton) const override;
  451. virtual void skeleton_bone_set_transform(RID p_skeleton, int p_bone, const Transform3D &p_transform) override;
  452. virtual Transform3D skeleton_bone_get_transform(RID p_skeleton, int p_bone) const override;
  453. virtual void skeleton_bone_set_transform_2d(RID p_skeleton, int p_bone, const Transform2D &p_transform) override;
  454. virtual Transform2D skeleton_bone_get_transform_2d(RID p_skeleton, int p_bone) const override;
  455. virtual void skeleton_update_dependency(RID p_base, DependencyTracker *p_instance) override;
  456. void _update_dirty_skeletons();
  457. _FORCE_INLINE_ bool skeleton_is_valid(RID p_skeleton) {
  458. return skeleton_owner.get_or_null(p_skeleton) != nullptr;
  459. }
  460. };
  461. } // namespace GLES3
  462. #endif // GLES3_ENABLED