mesh_storage.cpp 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125
  1. /**************************************************************************/
  2. /* mesh_storage.cpp */
  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. #ifdef GLES3_ENABLED
  31. #include "mesh_storage.h"
  32. #include "material_storage.h"
  33. #include "utilities.h"
  34. using namespace GLES3;
  35. MeshStorage *MeshStorage::singleton = nullptr;
  36. MeshStorage *MeshStorage::get_singleton() {
  37. return singleton;
  38. }
  39. MeshStorage::MeshStorage() {
  40. singleton = this;
  41. {
  42. skeleton_shader.shader.initialize();
  43. skeleton_shader.shader_version = skeleton_shader.shader.version_create();
  44. }
  45. }
  46. MeshStorage::~MeshStorage() {
  47. singleton = nullptr;
  48. skeleton_shader.shader.version_free(skeleton_shader.shader_version);
  49. }
  50. /* MESH API */
  51. RID MeshStorage::mesh_allocate() {
  52. return mesh_owner.allocate_rid();
  53. }
  54. void MeshStorage::mesh_initialize(RID p_rid) {
  55. mesh_owner.initialize_rid(p_rid, Mesh());
  56. }
  57. void MeshStorage::mesh_free(RID p_rid) {
  58. mesh_clear(p_rid);
  59. mesh_set_shadow_mesh(p_rid, RID());
  60. Mesh *mesh = mesh_owner.get_or_null(p_rid);
  61. ERR_FAIL_COND(!mesh);
  62. mesh->dependency.deleted_notify(p_rid);
  63. if (mesh->instances.size()) {
  64. ERR_PRINT("deleting mesh with active instances");
  65. }
  66. if (mesh->shadow_owners.size()) {
  67. for (Mesh *E : mesh->shadow_owners) {
  68. Mesh *shadow_owner = E;
  69. shadow_owner->shadow_mesh = RID();
  70. shadow_owner->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_MESH);
  71. }
  72. }
  73. mesh_owner.free(p_rid);
  74. }
  75. void MeshStorage::mesh_set_blend_shape_count(RID p_mesh, int p_blend_shape_count) {
  76. ERR_FAIL_COND(p_blend_shape_count < 0);
  77. Mesh *mesh = mesh_owner.get_or_null(p_mesh);
  78. ERR_FAIL_COND(!mesh);
  79. ERR_FAIL_COND(mesh->surface_count > 0); //surfaces already exist
  80. mesh->blend_shape_count = p_blend_shape_count;
  81. }
  82. bool MeshStorage::mesh_needs_instance(RID p_mesh, bool p_has_skeleton) {
  83. Mesh *mesh = mesh_owner.get_or_null(p_mesh);
  84. ERR_FAIL_COND_V(!mesh, false);
  85. return mesh->blend_shape_count > 0 || (mesh->has_bone_weights && p_has_skeleton);
  86. }
  87. void MeshStorage::mesh_add_surface(RID p_mesh, const RS::SurfaceData &p_surface) {
  88. Mesh *mesh = mesh_owner.get_or_null(p_mesh);
  89. ERR_FAIL_COND(!mesh);
  90. ERR_FAIL_COND(mesh->surface_count == RS::MAX_MESH_SURFACES);
  91. #ifdef DEBUG_ENABLED
  92. //do a validation, to catch errors first
  93. {
  94. uint32_t stride = 0;
  95. uint32_t attrib_stride = 0;
  96. uint32_t skin_stride = 0;
  97. for (int i = 0; i < RS::ARRAY_WEIGHTS; i++) {
  98. if ((p_surface.format & (1 << i))) {
  99. switch (i) {
  100. case RS::ARRAY_VERTEX: {
  101. if (p_surface.format & RS::ARRAY_FLAG_USE_2D_VERTICES) {
  102. stride += sizeof(float) * 2;
  103. } else {
  104. stride += sizeof(float) * 3;
  105. }
  106. } break;
  107. case RS::ARRAY_NORMAL: {
  108. stride += sizeof(uint16_t) * 2;
  109. } break;
  110. case RS::ARRAY_TANGENT: {
  111. stride += sizeof(uint16_t) * 2;
  112. } break;
  113. case RS::ARRAY_COLOR: {
  114. attrib_stride += sizeof(uint32_t);
  115. } break;
  116. case RS::ARRAY_TEX_UV: {
  117. attrib_stride += sizeof(float) * 2;
  118. } break;
  119. case RS::ARRAY_TEX_UV2: {
  120. attrib_stride += sizeof(float) * 2;
  121. } break;
  122. case RS::ARRAY_CUSTOM0:
  123. case RS::ARRAY_CUSTOM1:
  124. case RS::ARRAY_CUSTOM2:
  125. case RS::ARRAY_CUSTOM3: {
  126. int idx = i - RS::ARRAY_CUSTOM0;
  127. uint32_t fmt_shift[RS::ARRAY_CUSTOM_COUNT] = { RS::ARRAY_FORMAT_CUSTOM0_SHIFT, RS::ARRAY_FORMAT_CUSTOM1_SHIFT, RS::ARRAY_FORMAT_CUSTOM2_SHIFT, RS::ARRAY_FORMAT_CUSTOM3_SHIFT };
  128. uint32_t fmt = (p_surface.format >> fmt_shift[idx]) & RS::ARRAY_FORMAT_CUSTOM_MASK;
  129. uint32_t fmtsize[RS::ARRAY_CUSTOM_MAX] = { 4, 4, 4, 8, 4, 8, 12, 16 };
  130. attrib_stride += fmtsize[fmt];
  131. } break;
  132. case RS::ARRAY_WEIGHTS:
  133. case RS::ARRAY_BONES: {
  134. //uses a separate array
  135. bool use_8 = p_surface.format & RS::ARRAY_FLAG_USE_8_BONE_WEIGHTS;
  136. skin_stride += sizeof(int16_t) * (use_8 ? 16 : 8);
  137. } break;
  138. }
  139. }
  140. }
  141. int expected_size = stride * p_surface.vertex_count;
  142. ERR_FAIL_COND_MSG(expected_size != p_surface.vertex_data.size(), "Size of vertex data provided (" + itos(p_surface.vertex_data.size()) + ") does not match expected (" + itos(expected_size) + ")");
  143. int bs_expected_size = expected_size * mesh->blend_shape_count;
  144. ERR_FAIL_COND_MSG(bs_expected_size != p_surface.blend_shape_data.size(), "Size of blend shape data provided (" + itos(p_surface.blend_shape_data.size()) + ") does not match expected (" + itos(bs_expected_size) + ")");
  145. int expected_attrib_size = attrib_stride * p_surface.vertex_count;
  146. ERR_FAIL_COND_MSG(expected_attrib_size != p_surface.attribute_data.size(), "Size of attribute data provided (" + itos(p_surface.attribute_data.size()) + ") does not match expected (" + itos(expected_attrib_size) + ")");
  147. if ((p_surface.format & RS::ARRAY_FORMAT_WEIGHTS) && (p_surface.format & RS::ARRAY_FORMAT_BONES)) {
  148. expected_size = skin_stride * p_surface.vertex_count;
  149. ERR_FAIL_COND_MSG(expected_size != p_surface.skin_data.size(), "Size of skin data provided (" + itos(p_surface.skin_data.size()) + ") does not match expected (" + itos(expected_size) + ")");
  150. }
  151. }
  152. #endif
  153. Mesh::Surface *s = memnew(Mesh::Surface);
  154. s->format = p_surface.format;
  155. s->primitive = p_surface.primitive;
  156. if (p_surface.vertex_data.size()) {
  157. glGenBuffers(1, &s->vertex_buffer);
  158. glBindBuffer(GL_ARRAY_BUFFER, s->vertex_buffer);
  159. glBufferData(GL_ARRAY_BUFFER, p_surface.vertex_data.size(), p_surface.vertex_data.ptr(), (s->format & RS::ARRAY_FLAG_USE_DYNAMIC_UPDATE) ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW);
  160. glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind
  161. s->vertex_buffer_size = p_surface.vertex_data.size();
  162. }
  163. if (p_surface.attribute_data.size()) {
  164. glGenBuffers(1, &s->attribute_buffer);
  165. glBindBuffer(GL_ARRAY_BUFFER, s->attribute_buffer);
  166. glBufferData(GL_ARRAY_BUFFER, p_surface.attribute_data.size(), p_surface.attribute_data.ptr(), (s->format & RS::ARRAY_FLAG_USE_DYNAMIC_UPDATE) ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW);
  167. glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind
  168. s->attribute_buffer_size = p_surface.attribute_data.size();
  169. }
  170. if (p_surface.skin_data.size()) {
  171. glGenBuffers(1, &s->skin_buffer);
  172. glBindBuffer(GL_ARRAY_BUFFER, s->skin_buffer);
  173. glBufferData(GL_ARRAY_BUFFER, p_surface.skin_data.size(), p_surface.skin_data.ptr(), (s->format & RS::ARRAY_FLAG_USE_DYNAMIC_UPDATE) ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW);
  174. glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind
  175. s->skin_buffer_size = p_surface.skin_data.size();
  176. }
  177. s->vertex_count = p_surface.vertex_count;
  178. if (p_surface.format & RS::ARRAY_FORMAT_BONES) {
  179. mesh->has_bone_weights = true;
  180. }
  181. if (p_surface.index_count) {
  182. bool is_index_16 = p_surface.vertex_count <= 65536 && p_surface.vertex_count > 0;
  183. glGenBuffers(1, &s->index_buffer);
  184. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, s->index_buffer);
  185. glBufferData(GL_ELEMENT_ARRAY_BUFFER, p_surface.index_data.size(), p_surface.index_data.ptr(), GL_STATIC_DRAW);
  186. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); //unbind
  187. s->index_count = p_surface.index_count;
  188. s->index_buffer_size = p_surface.index_data.size();
  189. if (p_surface.lods.size()) {
  190. s->lods = memnew_arr(Mesh::Surface::LOD, p_surface.lods.size());
  191. s->lod_count = p_surface.lods.size();
  192. for (int i = 0; i < p_surface.lods.size(); i++) {
  193. glGenBuffers(1, &s->lods[i].index_buffer);
  194. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, s->lods[i].index_buffer);
  195. glBufferData(GL_ELEMENT_ARRAY_BUFFER, p_surface.lods[i].index_data.size(), p_surface.lods[i].index_data.ptr(), GL_STATIC_DRAW);
  196. glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); //unbind
  197. s->lods[i].edge_length = p_surface.lods[i].edge_length;
  198. s->lods[i].index_count = p_surface.lods[i].index_data.size() / (is_index_16 ? 2 : 4);
  199. s->lods[i].index_buffer_size = p_surface.lods[i].index_data.size();
  200. }
  201. }
  202. }
  203. ERR_FAIL_COND_MSG(!p_surface.index_count && !p_surface.vertex_count, "Meshes must contain a vertex array, an index array, or both");
  204. s->aabb = p_surface.aabb;
  205. s->bone_aabbs = p_surface.bone_aabbs; //only really useful for returning them.
  206. if (p_surface.skin_data.size() || mesh->blend_shape_count > 0) {
  207. // Size must match the size of the vertex array.
  208. int size = p_surface.vertex_data.size();
  209. int vertex_size = 0;
  210. int stride = 0;
  211. int normal_offset = 0;
  212. int tangent_offset = 0;
  213. if ((p_surface.format & (1 << RS::ARRAY_VERTEX))) {
  214. if (p_surface.format & RS::ARRAY_FLAG_USE_2D_VERTICES) {
  215. vertex_size = 2;
  216. } else {
  217. vertex_size = 3;
  218. }
  219. stride = sizeof(float) * vertex_size;
  220. }
  221. if ((p_surface.format & (1 << RS::ARRAY_NORMAL))) {
  222. normal_offset = stride;
  223. stride += sizeof(uint16_t) * 2;
  224. }
  225. if ((p_surface.format & (1 << RS::ARRAY_TANGENT))) {
  226. tangent_offset = stride;
  227. stride += sizeof(uint16_t) * 2;
  228. }
  229. if (mesh->blend_shape_count > 0) {
  230. // Blend shapes are passed as one large array, for OpenGL, we need to split each of them into their own buffer
  231. s->blend_shapes = memnew_arr(Mesh::Surface::BlendShape, mesh->blend_shape_count);
  232. for (uint32_t i = 0; i < mesh->blend_shape_count; i++) {
  233. glGenVertexArrays(1, &s->blend_shapes[i].vertex_array);
  234. glBindVertexArray(s->blend_shapes[i].vertex_array);
  235. glGenBuffers(1, &s->blend_shapes[i].vertex_buffer);
  236. glBindBuffer(GL_ARRAY_BUFFER, s->blend_shapes[i].vertex_buffer);
  237. glBufferData(GL_ARRAY_BUFFER, size, p_surface.blend_shape_data.ptr() + i * size, (s->format & RS::ARRAY_FLAG_USE_DYNAMIC_UPDATE) ? GL_DYNAMIC_DRAW : GL_STATIC_DRAW);
  238. if ((p_surface.format & (1 << RS::ARRAY_VERTEX))) {
  239. glEnableVertexAttribArray(RS::ARRAY_VERTEX + 3);
  240. glVertexAttribPointer(RS::ARRAY_VERTEX + 3, vertex_size, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(0));
  241. }
  242. if ((p_surface.format & (1 << RS::ARRAY_NORMAL))) {
  243. glEnableVertexAttribArray(RS::ARRAY_NORMAL + 3);
  244. glVertexAttribPointer(RS::ARRAY_NORMAL + 3, 2, GL_UNSIGNED_SHORT, GL_TRUE, stride, CAST_INT_TO_UCHAR_PTR(normal_offset));
  245. }
  246. if ((p_surface.format & (1 << RS::ARRAY_TANGENT))) {
  247. glEnableVertexAttribArray(RS::ARRAY_TANGENT + 3);
  248. glVertexAttribPointer(RS::ARRAY_TANGENT + 3, 2, GL_UNSIGNED_SHORT, GL_TRUE, stride, CAST_INT_TO_UCHAR_PTR(tangent_offset));
  249. }
  250. }
  251. glBindVertexArray(0);
  252. glBindBuffer(GL_ARRAY_BUFFER, 0);
  253. }
  254. // Create a vertex array to use for skeleton/blend shapes.
  255. glGenVertexArrays(1, &s->skeleton_vertex_array);
  256. glBindVertexArray(s->skeleton_vertex_array);
  257. glBindBuffer(GL_ARRAY_BUFFER, s->vertex_buffer);
  258. if ((p_surface.format & (1 << RS::ARRAY_VERTEX))) {
  259. glEnableVertexAttribArray(RS::ARRAY_VERTEX);
  260. glVertexAttribPointer(RS::ARRAY_VERTEX, vertex_size, GL_FLOAT, GL_FALSE, stride, CAST_INT_TO_UCHAR_PTR(0));
  261. }
  262. if ((p_surface.format & (1 << RS::ARRAY_NORMAL))) {
  263. glEnableVertexAttribArray(RS::ARRAY_NORMAL);
  264. glVertexAttribPointer(RS::ARRAY_NORMAL, 2, GL_UNSIGNED_SHORT, GL_TRUE, stride, CAST_INT_TO_UCHAR_PTR(normal_offset));
  265. }
  266. if ((p_surface.format & (1 << RS::ARRAY_TANGENT))) {
  267. glEnableVertexAttribArray(RS::ARRAY_TANGENT);
  268. glVertexAttribPointer(RS::ARRAY_TANGENT, 2, GL_UNSIGNED_SHORT, GL_TRUE, stride, CAST_INT_TO_UCHAR_PTR(tangent_offset));
  269. }
  270. glBindVertexArray(0);
  271. glBindBuffer(GL_ARRAY_BUFFER, 0);
  272. }
  273. if (mesh->surface_count == 0) {
  274. mesh->aabb = p_surface.aabb;
  275. } else {
  276. mesh->aabb.merge_with(p_surface.aabb);
  277. }
  278. mesh->skeleton_aabb_version = 0;
  279. s->material = p_surface.material;
  280. mesh->surfaces = (Mesh::Surface **)memrealloc(mesh->surfaces, sizeof(Mesh::Surface *) * (mesh->surface_count + 1));
  281. mesh->surfaces[mesh->surface_count] = s;
  282. mesh->surface_count++;
  283. for (MeshInstance *mi : mesh->instances) {
  284. _mesh_instance_add_surface(mi, mesh, mesh->surface_count - 1);
  285. }
  286. mesh->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_MESH);
  287. for (Mesh *E : mesh->shadow_owners) {
  288. Mesh *shadow_owner = E;
  289. shadow_owner->shadow_mesh = RID();
  290. shadow_owner->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_MESH);
  291. }
  292. mesh->material_cache.clear();
  293. }
  294. int MeshStorage::mesh_get_blend_shape_count(RID p_mesh) const {
  295. const Mesh *mesh = mesh_owner.get_or_null(p_mesh);
  296. ERR_FAIL_COND_V(!mesh, -1);
  297. return mesh->blend_shape_count;
  298. }
  299. void MeshStorage::mesh_set_blend_shape_mode(RID p_mesh, RS::BlendShapeMode p_mode) {
  300. Mesh *mesh = mesh_owner.get_or_null(p_mesh);
  301. ERR_FAIL_COND(!mesh);
  302. ERR_FAIL_INDEX((int)p_mode, 2);
  303. mesh->blend_shape_mode = p_mode;
  304. }
  305. RS::BlendShapeMode MeshStorage::mesh_get_blend_shape_mode(RID p_mesh) const {
  306. Mesh *mesh = mesh_owner.get_or_null(p_mesh);
  307. ERR_FAIL_COND_V(!mesh, RS::BLEND_SHAPE_MODE_NORMALIZED);
  308. return mesh->blend_shape_mode;
  309. }
  310. void MeshStorage::mesh_surface_update_vertex_region(RID p_mesh, int p_surface, int p_offset, const Vector<uint8_t> &p_data) {
  311. Mesh *mesh = mesh_owner.get_or_null(p_mesh);
  312. ERR_FAIL_COND(!mesh);
  313. ERR_FAIL_UNSIGNED_INDEX((uint32_t)p_surface, mesh->surface_count);
  314. ERR_FAIL_COND(p_data.size() == 0);
  315. uint64_t data_size = p_data.size();
  316. ERR_FAIL_COND(p_offset + data_size > mesh->surfaces[p_surface]->vertex_buffer_size);
  317. const uint8_t *r = p_data.ptr();
  318. glBindBuffer(GL_ARRAY_BUFFER, mesh->surfaces[p_surface]->vertex_buffer);
  319. glBufferSubData(GL_ARRAY_BUFFER, p_offset, data_size, r);
  320. glBindBuffer(GL_ARRAY_BUFFER, 0);
  321. }
  322. void MeshStorage::mesh_surface_update_attribute_region(RID p_mesh, int p_surface, int p_offset, const Vector<uint8_t> &p_data) {
  323. Mesh *mesh = mesh_owner.get_or_null(p_mesh);
  324. ERR_FAIL_COND(!mesh);
  325. ERR_FAIL_UNSIGNED_INDEX((uint32_t)p_surface, mesh->surface_count);
  326. ERR_FAIL_COND(p_data.size() == 0);
  327. uint64_t data_size = p_data.size();
  328. ERR_FAIL_COND(p_offset + data_size > mesh->surfaces[p_surface]->attribute_buffer_size);
  329. const uint8_t *r = p_data.ptr();
  330. glBindBuffer(GL_ARRAY_BUFFER, mesh->surfaces[p_surface]->attribute_buffer);
  331. glBufferSubData(GL_ARRAY_BUFFER, p_offset, data_size, r);
  332. glBindBuffer(GL_ARRAY_BUFFER, 0);
  333. }
  334. void MeshStorage::mesh_surface_update_skin_region(RID p_mesh, int p_surface, int p_offset, const Vector<uint8_t> &p_data) {
  335. Mesh *mesh = mesh_owner.get_or_null(p_mesh);
  336. ERR_FAIL_COND(!mesh);
  337. ERR_FAIL_UNSIGNED_INDEX((uint32_t)p_surface, mesh->surface_count);
  338. ERR_FAIL_COND(p_data.size() == 0);
  339. uint64_t data_size = p_data.size();
  340. ERR_FAIL_COND(p_offset + data_size > mesh->surfaces[p_surface]->skin_buffer_size);
  341. const uint8_t *r = p_data.ptr();
  342. glBindBuffer(GL_ARRAY_BUFFER, mesh->surfaces[p_surface]->skin_buffer);
  343. glBufferSubData(GL_ARRAY_BUFFER, p_offset, data_size, r);
  344. glBindBuffer(GL_ARRAY_BUFFER, 0);
  345. }
  346. void MeshStorage::mesh_surface_set_material(RID p_mesh, int p_surface, RID p_material) {
  347. Mesh *mesh = mesh_owner.get_or_null(p_mesh);
  348. ERR_FAIL_COND(!mesh);
  349. ERR_FAIL_UNSIGNED_INDEX((uint32_t)p_surface, mesh->surface_count);
  350. mesh->surfaces[p_surface]->material = p_material;
  351. mesh->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_MATERIAL);
  352. mesh->material_cache.clear();
  353. }
  354. RID MeshStorage::mesh_surface_get_material(RID p_mesh, int p_surface) const {
  355. Mesh *mesh = mesh_owner.get_or_null(p_mesh);
  356. ERR_FAIL_COND_V(!mesh, RID());
  357. ERR_FAIL_UNSIGNED_INDEX_V((uint32_t)p_surface, mesh->surface_count, RID());
  358. return mesh->surfaces[p_surface]->material;
  359. }
  360. RS::SurfaceData MeshStorage::mesh_get_surface(RID p_mesh, int p_surface) const {
  361. Mesh *mesh = mesh_owner.get_or_null(p_mesh);
  362. ERR_FAIL_COND_V(!mesh, RS::SurfaceData());
  363. ERR_FAIL_UNSIGNED_INDEX_V((uint32_t)p_surface, mesh->surface_count, RS::SurfaceData());
  364. Mesh::Surface &s = *mesh->surfaces[p_surface];
  365. RS::SurfaceData sd;
  366. sd.format = s.format;
  367. if (s.vertex_buffer != 0) {
  368. sd.vertex_data = Utilities::buffer_get_data(GL_ARRAY_BUFFER, s.vertex_buffer, s.vertex_buffer_size);
  369. }
  370. if (s.attribute_buffer != 0) {
  371. sd.attribute_data = Utilities::buffer_get_data(GL_ARRAY_BUFFER, s.attribute_buffer, s.attribute_buffer_size);
  372. }
  373. if (s.skin_buffer != 0) {
  374. sd.skin_data = Utilities::buffer_get_data(GL_ARRAY_BUFFER, s.skin_buffer, s.skin_buffer_size);
  375. }
  376. sd.vertex_count = s.vertex_count;
  377. sd.index_count = s.index_count;
  378. sd.primitive = s.primitive;
  379. if (sd.index_count) {
  380. sd.index_data = Utilities::buffer_get_data(GL_ELEMENT_ARRAY_BUFFER, s.index_buffer, s.index_buffer_size);
  381. }
  382. sd.aabb = s.aabb;
  383. for (uint32_t i = 0; i < s.lod_count; i++) {
  384. RS::SurfaceData::LOD lod;
  385. lod.edge_length = s.lods[i].edge_length;
  386. lod.index_data = Utilities::buffer_get_data(GL_ELEMENT_ARRAY_BUFFER, s.lods[i].index_buffer, s.lods[i].index_buffer_size);
  387. sd.lods.push_back(lod);
  388. }
  389. sd.bone_aabbs = s.bone_aabbs;
  390. if (mesh->blend_shape_count) {
  391. sd.blend_shape_data = Vector<uint8_t>();
  392. for (uint32_t i = 0; i < mesh->blend_shape_count; i++) {
  393. sd.blend_shape_data.append_array(Utilities::buffer_get_data(GL_ARRAY_BUFFER, s.blend_shapes[i].vertex_buffer, s.vertex_buffer_size));
  394. }
  395. }
  396. return sd;
  397. }
  398. int MeshStorage::mesh_get_surface_count(RID p_mesh) const {
  399. Mesh *mesh = mesh_owner.get_or_null(p_mesh);
  400. ERR_FAIL_COND_V(!mesh, 0);
  401. return mesh->surface_count;
  402. }
  403. void MeshStorage::mesh_set_custom_aabb(RID p_mesh, const AABB &p_aabb) {
  404. Mesh *mesh = mesh_owner.get_or_null(p_mesh);
  405. ERR_FAIL_COND(!mesh);
  406. mesh->custom_aabb = p_aabb;
  407. mesh->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_AABB);
  408. }
  409. AABB MeshStorage::mesh_get_custom_aabb(RID p_mesh) const {
  410. Mesh *mesh = mesh_owner.get_or_null(p_mesh);
  411. ERR_FAIL_COND_V(!mesh, AABB());
  412. return mesh->custom_aabb;
  413. }
  414. AABB MeshStorage::mesh_get_aabb(RID p_mesh, RID p_skeleton) {
  415. Mesh *mesh = mesh_owner.get_or_null(p_mesh);
  416. ERR_FAIL_COND_V(!mesh, AABB());
  417. if (mesh->custom_aabb != AABB()) {
  418. return mesh->custom_aabb;
  419. }
  420. Skeleton *skeleton = skeleton_owner.get_or_null(p_skeleton);
  421. if (!skeleton || skeleton->size == 0 || mesh->skeleton_aabb_version == skeleton->version) {
  422. return mesh->aabb;
  423. }
  424. // Calculate AABB based on Skeleton
  425. AABB aabb;
  426. for (uint32_t i = 0; i < mesh->surface_count; i++) {
  427. AABB laabb;
  428. if ((mesh->surfaces[i]->format & RS::ARRAY_FORMAT_BONES) && mesh->surfaces[i]->bone_aabbs.size()) {
  429. int bs = mesh->surfaces[i]->bone_aabbs.size();
  430. const AABB *skbones = mesh->surfaces[i]->bone_aabbs.ptr();
  431. int sbs = skeleton->size;
  432. ERR_CONTINUE(bs > sbs);
  433. const float *baseptr = skeleton->data.ptr();
  434. bool first = true;
  435. if (skeleton->use_2d) {
  436. for (int j = 0; j < bs; j++) {
  437. if (skbones[j].size == Vector3(-1, -1, -1)) {
  438. continue; //bone is unused
  439. }
  440. const float *dataptr = baseptr + j * 8;
  441. Transform3D mtx;
  442. mtx.basis.rows[0][0] = dataptr[0];
  443. mtx.basis.rows[0][1] = dataptr[1];
  444. mtx.origin.x = dataptr[3];
  445. mtx.basis.rows[1][0] = dataptr[4];
  446. mtx.basis.rows[1][1] = dataptr[5];
  447. mtx.origin.y = dataptr[7];
  448. AABB baabb = mtx.xform(skbones[j]);
  449. if (first) {
  450. laabb = baabb;
  451. first = false;
  452. } else {
  453. laabb.merge_with(baabb);
  454. }
  455. }
  456. } else {
  457. for (int j = 0; j < bs; j++) {
  458. if (skbones[j].size == Vector3(-1, -1, -1)) {
  459. continue; //bone is unused
  460. }
  461. const float *dataptr = baseptr + j * 12;
  462. Transform3D mtx;
  463. mtx.basis.rows[0][0] = dataptr[0];
  464. mtx.basis.rows[0][1] = dataptr[1];
  465. mtx.basis.rows[0][2] = dataptr[2];
  466. mtx.origin.x = dataptr[3];
  467. mtx.basis.rows[1][0] = dataptr[4];
  468. mtx.basis.rows[1][1] = dataptr[5];
  469. mtx.basis.rows[1][2] = dataptr[6];
  470. mtx.origin.y = dataptr[7];
  471. mtx.basis.rows[2][0] = dataptr[8];
  472. mtx.basis.rows[2][1] = dataptr[9];
  473. mtx.basis.rows[2][2] = dataptr[10];
  474. mtx.origin.z = dataptr[11];
  475. AABB baabb = mtx.xform(skbones[j]);
  476. if (first) {
  477. laabb = baabb;
  478. first = false;
  479. } else {
  480. laabb.merge_with(baabb);
  481. }
  482. }
  483. }
  484. if (laabb.size == Vector3()) {
  485. laabb = mesh->surfaces[i]->aabb;
  486. }
  487. } else {
  488. laabb = mesh->surfaces[i]->aabb;
  489. }
  490. if (i == 0) {
  491. aabb = laabb;
  492. } else {
  493. aabb.merge_with(laabb);
  494. }
  495. }
  496. mesh->aabb = aabb;
  497. mesh->skeleton_aabb_version = skeleton->version;
  498. return aabb;
  499. }
  500. void MeshStorage::mesh_set_shadow_mesh(RID p_mesh, RID p_shadow_mesh) {
  501. Mesh *mesh = mesh_owner.get_or_null(p_mesh);
  502. ERR_FAIL_COND(!mesh);
  503. Mesh *shadow_mesh = mesh_owner.get_or_null(mesh->shadow_mesh);
  504. if (shadow_mesh) {
  505. shadow_mesh->shadow_owners.erase(mesh);
  506. }
  507. mesh->shadow_mesh = p_shadow_mesh;
  508. shadow_mesh = mesh_owner.get_or_null(mesh->shadow_mesh);
  509. if (shadow_mesh) {
  510. shadow_mesh->shadow_owners.insert(mesh);
  511. }
  512. mesh->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_MESH);
  513. }
  514. void MeshStorage::mesh_clear(RID p_mesh) {
  515. Mesh *mesh = mesh_owner.get_or_null(p_mesh);
  516. ERR_FAIL_COND(!mesh);
  517. for (uint32_t i = 0; i < mesh->surface_count; i++) {
  518. Mesh::Surface &s = *mesh->surfaces[i];
  519. if (s.vertex_buffer != 0) {
  520. glDeleteBuffers(1, &s.vertex_buffer);
  521. s.vertex_buffer = 0;
  522. }
  523. if (s.version_count != 0) {
  524. for (uint32_t j = 0; j < s.version_count; j++) {
  525. glDeleteVertexArrays(1, &s.versions[j].vertex_array);
  526. s.versions[j].vertex_array = 0;
  527. }
  528. }
  529. if (s.attribute_buffer != 0) {
  530. glDeleteBuffers(1, &s.attribute_buffer);
  531. s.attribute_buffer = 0;
  532. }
  533. if (s.skin_buffer != 0) {
  534. glDeleteBuffers(1, &s.skin_buffer);
  535. s.skin_buffer = 0;
  536. }
  537. if (s.index_buffer != 0) {
  538. glDeleteBuffers(1, &s.index_buffer);
  539. s.index_buffer = 0;
  540. }
  541. if (s.versions) {
  542. memfree(s.versions); //reallocs, so free with memfree.
  543. }
  544. if (s.lod_count) {
  545. for (uint32_t j = 0; j < s.lod_count; j++) {
  546. if (s.lods[j].index_buffer != 0) {
  547. glDeleteBuffers(1, &s.lods[j].index_buffer);
  548. s.lods[j].index_buffer = 0;
  549. }
  550. }
  551. memdelete_arr(s.lods);
  552. }
  553. if (mesh->blend_shape_count) {
  554. for (uint32_t j = 0; j < mesh->blend_shape_count; j++) {
  555. if (s.blend_shapes[j].vertex_buffer != 0) {
  556. glDeleteBuffers(1, &s.blend_shapes[j].vertex_buffer);
  557. s.blend_shapes[j].vertex_buffer = 0;
  558. }
  559. if (s.blend_shapes[j].vertex_array != 0) {
  560. glDeleteVertexArrays(1, &s.blend_shapes[j].vertex_array);
  561. s.blend_shapes[j].vertex_array = 0;
  562. }
  563. }
  564. memdelete_arr(s.blend_shapes);
  565. }
  566. if (s.skeleton_vertex_array != 0) {
  567. glDeleteVertexArrays(1, &s.skeleton_vertex_array);
  568. s.skeleton_vertex_array = 0;
  569. }
  570. memdelete(mesh->surfaces[i]);
  571. }
  572. if (mesh->surfaces) {
  573. memfree(mesh->surfaces);
  574. }
  575. mesh->surfaces = nullptr;
  576. mesh->surface_count = 0;
  577. mesh->material_cache.clear();
  578. //clear instance data
  579. for (MeshInstance *mi : mesh->instances) {
  580. _mesh_instance_clear(mi);
  581. }
  582. mesh->has_bone_weights = false;
  583. mesh->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_MESH);
  584. for (Mesh *E : mesh->shadow_owners) {
  585. Mesh *shadow_owner = E;
  586. shadow_owner->shadow_mesh = RID();
  587. shadow_owner->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_MESH);
  588. }
  589. }
  590. void MeshStorage::_mesh_surface_generate_version_for_input_mask(Mesh::Surface::Version &v, Mesh::Surface *s, uint32_t p_input_mask, MeshInstance::Surface *mis) {
  591. Mesh::Surface::Attrib attribs[RS::ARRAY_MAX];
  592. int attributes_stride = 0;
  593. int vertex_stride = 0;
  594. int skin_stride = 0;
  595. for (int i = 0; i < RS::ARRAY_INDEX; i++) {
  596. if (!(s->format & (1 << i))) {
  597. attribs[i].enabled = false;
  598. attribs[i].integer = false;
  599. continue;
  600. }
  601. attribs[i].enabled = true;
  602. attribs[i].integer = false;
  603. switch (i) {
  604. case RS::ARRAY_VERTEX: {
  605. attribs[i].offset = vertex_stride;
  606. if (s->format & RS::ARRAY_FLAG_USE_2D_VERTICES) {
  607. attribs[i].size = 2;
  608. } else {
  609. attribs[i].size = 3;
  610. }
  611. attribs[i].type = GL_FLOAT;
  612. vertex_stride += attribs[i].size * sizeof(float);
  613. attribs[i].normalized = GL_FALSE;
  614. } break;
  615. case RS::ARRAY_NORMAL: {
  616. attribs[i].offset = vertex_stride;
  617. attribs[i].size = 2;
  618. attribs[i].type = (mis ? GL_FLOAT : GL_UNSIGNED_SHORT);
  619. vertex_stride += sizeof(uint16_t) * 2 * (mis ? 2 : 1);
  620. attribs[i].normalized = GL_TRUE;
  621. } break;
  622. case RS::ARRAY_TANGENT: {
  623. attribs[i].offset = vertex_stride;
  624. attribs[i].size = 2;
  625. attribs[i].type = (mis ? GL_FLOAT : GL_UNSIGNED_SHORT);
  626. vertex_stride += sizeof(uint16_t) * 2 * (mis ? 2 : 1);
  627. attribs[i].normalized = GL_TRUE;
  628. } break;
  629. case RS::ARRAY_COLOR: {
  630. attribs[i].offset = attributes_stride;
  631. attribs[i].size = 4;
  632. attribs[i].type = GL_UNSIGNED_BYTE;
  633. attributes_stride += 4;
  634. attribs[i].normalized = GL_TRUE;
  635. } break;
  636. case RS::ARRAY_TEX_UV: {
  637. attribs[i].offset = attributes_stride;
  638. attribs[i].size = 2;
  639. attribs[i].type = GL_FLOAT;
  640. attributes_stride += 2 * sizeof(float);
  641. attribs[i].normalized = GL_FALSE;
  642. } break;
  643. case RS::ARRAY_TEX_UV2: {
  644. attribs[i].offset = attributes_stride;
  645. attribs[i].size = 2;
  646. attribs[i].type = GL_FLOAT;
  647. attributes_stride += 2 * sizeof(float);
  648. attribs[i].normalized = GL_FALSE;
  649. } break;
  650. case RS::ARRAY_CUSTOM0:
  651. case RS::ARRAY_CUSTOM1:
  652. case RS::ARRAY_CUSTOM2:
  653. case RS::ARRAY_CUSTOM3: {
  654. attribs[i].offset = attributes_stride;
  655. int idx = i - RS::ARRAY_CUSTOM0;
  656. uint32_t fmt_shift[RS::ARRAY_CUSTOM_COUNT] = { RS::ARRAY_FORMAT_CUSTOM0_SHIFT, RS::ARRAY_FORMAT_CUSTOM1_SHIFT, RS::ARRAY_FORMAT_CUSTOM2_SHIFT, RS::ARRAY_FORMAT_CUSTOM3_SHIFT };
  657. uint32_t fmt = (s->format >> fmt_shift[idx]) & RS::ARRAY_FORMAT_CUSTOM_MASK;
  658. uint32_t fmtsize[RS::ARRAY_CUSTOM_MAX] = { 4, 4, 4, 8, 4, 8, 12, 16 };
  659. GLenum gl_type[RS::ARRAY_CUSTOM_MAX] = { GL_UNSIGNED_BYTE, GL_BYTE, GL_HALF_FLOAT, GL_HALF_FLOAT, GL_FLOAT, GL_FLOAT, GL_FLOAT, GL_FLOAT };
  660. GLboolean norm[RS::ARRAY_CUSTOM_MAX] = { GL_TRUE, GL_TRUE, GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE };
  661. attribs[i].type = gl_type[fmt];
  662. attributes_stride += fmtsize[fmt];
  663. attribs[i].size = fmtsize[fmt] / sizeof(float);
  664. attribs[i].normalized = norm[fmt];
  665. } break;
  666. case RS::ARRAY_BONES: {
  667. attribs[i].offset = skin_stride;
  668. attribs[i].size = 4;
  669. attribs[i].type = GL_UNSIGNED_SHORT;
  670. skin_stride += 4 * sizeof(uint16_t);
  671. attribs[i].normalized = GL_FALSE;
  672. attribs[i].integer = true;
  673. } break;
  674. case RS::ARRAY_WEIGHTS: {
  675. attribs[i].offset = skin_stride;
  676. attribs[i].size = 4;
  677. attribs[i].type = GL_UNSIGNED_SHORT;
  678. skin_stride += 4 * sizeof(uint16_t);
  679. attribs[i].normalized = GL_TRUE;
  680. } break;
  681. }
  682. }
  683. glGenVertexArrays(1, &v.vertex_array);
  684. glBindVertexArray(v.vertex_array);
  685. for (int i = 0; i < RS::ARRAY_INDEX; i++) {
  686. if (!attribs[i].enabled) {
  687. glDisableVertexAttribArray(i);
  688. continue;
  689. }
  690. if (i <= RS::ARRAY_TANGENT) {
  691. attribs[i].stride = vertex_stride;
  692. if (mis) {
  693. glBindBuffer(GL_ARRAY_BUFFER, mis->vertex_buffer);
  694. } else {
  695. glBindBuffer(GL_ARRAY_BUFFER, s->vertex_buffer);
  696. }
  697. } else if (i <= RS::ARRAY_CUSTOM3) {
  698. attribs[i].stride = attributes_stride;
  699. glBindBuffer(GL_ARRAY_BUFFER, s->attribute_buffer);
  700. } else {
  701. attribs[i].stride = skin_stride;
  702. glBindBuffer(GL_ARRAY_BUFFER, s->skin_buffer);
  703. }
  704. if (attribs[i].integer) {
  705. glVertexAttribIPointer(i, attribs[i].size, attribs[i].type, attribs[i].stride, CAST_INT_TO_UCHAR_PTR(attribs[i].offset));
  706. } else {
  707. glVertexAttribPointer(i, attribs[i].size, attribs[i].type, attribs[i].normalized, attribs[i].stride, CAST_INT_TO_UCHAR_PTR(attribs[i].offset));
  708. }
  709. glEnableVertexAttribArray(i);
  710. }
  711. // Do not bind index here as we want to switch between index buffers for LOD
  712. glBindVertexArray(0);
  713. glBindBuffer(GL_ARRAY_BUFFER, 0);
  714. v.input_mask = p_input_mask;
  715. }
  716. /* MESH INSTANCE API */
  717. RID MeshStorage::mesh_instance_create(RID p_base) {
  718. Mesh *mesh = mesh_owner.get_or_null(p_base);
  719. ERR_FAIL_COND_V(!mesh, RID());
  720. RID rid = mesh_instance_owner.make_rid();
  721. MeshInstance *mi = mesh_instance_owner.get_or_null(rid);
  722. mi->mesh = mesh;
  723. for (uint32_t i = 0; i < mesh->surface_count; i++) {
  724. _mesh_instance_add_surface(mi, mesh, i);
  725. }
  726. mi->I = mesh->instances.push_back(mi);
  727. mi->dirty = true;
  728. return rid;
  729. }
  730. void MeshStorage::mesh_instance_free(RID p_rid) {
  731. MeshInstance *mi = mesh_instance_owner.get_or_null(p_rid);
  732. _mesh_instance_clear(mi);
  733. mi->mesh->instances.erase(mi->I);
  734. mi->I = nullptr;
  735. mesh_instance_owner.free(p_rid);
  736. }
  737. void MeshStorage::mesh_instance_set_skeleton(RID p_mesh_instance, RID p_skeleton) {
  738. MeshInstance *mi = mesh_instance_owner.get_or_null(p_mesh_instance);
  739. if (mi->skeleton == p_skeleton) {
  740. return;
  741. }
  742. mi->skeleton = p_skeleton;
  743. mi->skeleton_version = 0;
  744. mi->dirty = true;
  745. }
  746. void MeshStorage::mesh_instance_set_blend_shape_weight(RID p_mesh_instance, int p_shape, float p_weight) {
  747. MeshInstance *mi = mesh_instance_owner.get_or_null(p_mesh_instance);
  748. ERR_FAIL_COND(!mi);
  749. ERR_FAIL_INDEX(p_shape, (int)mi->blend_weights.size());
  750. mi->blend_weights[p_shape] = p_weight;
  751. mi->dirty = true;
  752. }
  753. void MeshStorage::_mesh_instance_clear(MeshInstance *mi) {
  754. for (uint32_t i = 0; i < mi->surfaces.size(); i++) {
  755. if (mi->surfaces[i].version_count != 0) {
  756. for (uint32_t j = 0; j < mi->surfaces[i].version_count; j++) {
  757. glDeleteVertexArrays(1, &mi->surfaces[i].versions[j].vertex_array);
  758. mi->surfaces[i].versions[j].vertex_array = 0;
  759. }
  760. memfree(mi->surfaces[i].versions);
  761. }
  762. if (mi->surfaces[i].vertex_buffers[0] != 0) {
  763. glDeleteBuffers(2, mi->surfaces[i].vertex_buffers);
  764. mi->surfaces[i].vertex_buffers[0] = 0;
  765. mi->surfaces[i].vertex_buffers[1] = 0;
  766. }
  767. if (mi->surfaces[i].vertex_buffer != 0) {
  768. glDeleteBuffers(1, &mi->surfaces[i].vertex_buffer);
  769. mi->surfaces[i].vertex_buffer = 0;
  770. }
  771. }
  772. mi->surfaces.clear();
  773. mi->blend_weights.clear();
  774. mi->skeleton_version = 0;
  775. }
  776. void MeshStorage::_mesh_instance_add_surface(MeshInstance *mi, Mesh *mesh, uint32_t p_surface) {
  777. if (mesh->blend_shape_count > 0) {
  778. mi->blend_weights.resize(mesh->blend_shape_count);
  779. for (uint32_t i = 0; i < mi->blend_weights.size(); i++) {
  780. mi->blend_weights[i] = 0.0;
  781. }
  782. }
  783. MeshInstance::Surface s;
  784. if ((mesh->blend_shape_count > 0 || (mesh->surfaces[p_surface]->format & RS::ARRAY_FORMAT_BONES)) && mesh->surfaces[p_surface]->vertex_buffer_size > 0) {
  785. // Cache surface properties
  786. s.format_cache = mesh->surfaces[p_surface]->format;
  787. if ((s.format_cache & (1 << RS::ARRAY_VERTEX))) {
  788. if (s.format_cache & RS::ARRAY_FLAG_USE_2D_VERTICES) {
  789. s.vertex_size_cache = 2;
  790. } else {
  791. s.vertex_size_cache = 3;
  792. }
  793. s.vertex_stride_cache = sizeof(float) * s.vertex_size_cache;
  794. }
  795. if ((s.format_cache & (1 << RS::ARRAY_NORMAL))) {
  796. s.vertex_normal_offset_cache = s.vertex_stride_cache;
  797. s.vertex_stride_cache += sizeof(uint32_t) * 2;
  798. }
  799. if ((s.format_cache & (1 << RS::ARRAY_TANGENT))) {
  800. s.vertex_tangent_offset_cache = s.vertex_stride_cache;
  801. s.vertex_stride_cache += sizeof(uint32_t) * 2;
  802. }
  803. // Buffer to be used for rendering. Final output of skeleton and blend shapes.
  804. glGenBuffers(1, &s.vertex_buffer);
  805. glBindBuffer(GL_ARRAY_BUFFER, s.vertex_buffer);
  806. glBufferData(GL_ARRAY_BUFFER, s.vertex_stride_cache * mesh->surfaces[p_surface]->vertex_count, nullptr, GL_DYNAMIC_DRAW);
  807. if (mesh->blend_shape_count > 0) {
  808. // Ping-Pong buffers for processing blendshapes.
  809. glGenBuffers(2, s.vertex_buffers);
  810. for (uint32_t i = 0; i < 2; i++) {
  811. glBindBuffer(GL_ARRAY_BUFFER, s.vertex_buffers[i]);
  812. glBufferData(GL_ARRAY_BUFFER, s.vertex_stride_cache * mesh->surfaces[p_surface]->vertex_count, nullptr, GL_DYNAMIC_DRAW);
  813. }
  814. }
  815. glBindBuffer(GL_ARRAY_BUFFER, 0); //unbind
  816. }
  817. mi->surfaces.push_back(s);
  818. mi->dirty = true;
  819. }
  820. void MeshStorage::mesh_instance_check_for_update(RID p_mesh_instance) {
  821. MeshInstance *mi = mesh_instance_owner.get_or_null(p_mesh_instance);
  822. bool needs_update = mi->dirty;
  823. if (mi->array_update_list.in_list()) {
  824. return;
  825. }
  826. if (!needs_update && mi->skeleton.is_valid()) {
  827. Skeleton *sk = skeleton_owner.get_or_null(mi->skeleton);
  828. if (sk && sk->version != mi->skeleton_version) {
  829. needs_update = true;
  830. }
  831. }
  832. if (needs_update) {
  833. dirty_mesh_instance_arrays.add(&mi->array_update_list);
  834. }
  835. }
  836. void MeshStorage::mesh_instance_set_canvas_item_transform(RID p_mesh_instance, const Transform2D &p_transform) {
  837. MeshInstance *mi = mesh_instance_owner.get_or_null(p_mesh_instance);
  838. mi->canvas_item_transform_2d = p_transform;
  839. }
  840. void MeshStorage::_blend_shape_bind_mesh_instance_buffer(MeshInstance *p_mi, uint32_t p_surface) {
  841. glBindBuffer(GL_ARRAY_BUFFER, p_mi->surfaces[p_surface].vertex_buffers[0]);
  842. if ((p_mi->surfaces[p_surface].format_cache & (1 << RS::ARRAY_VERTEX))) {
  843. glEnableVertexAttribArray(RS::ARRAY_VERTEX);
  844. glVertexAttribPointer(RS::ARRAY_VERTEX, p_mi->surfaces[p_surface].vertex_size_cache, GL_FLOAT, GL_FALSE, p_mi->surfaces[p_surface].vertex_stride_cache, CAST_INT_TO_UCHAR_PTR(0));
  845. } else {
  846. glDisableVertexAttribArray(RS::ARRAY_VERTEX);
  847. }
  848. if ((p_mi->surfaces[p_surface].format_cache & (1 << RS::ARRAY_NORMAL))) {
  849. glEnableVertexAttribArray(RS::ARRAY_NORMAL);
  850. glVertexAttribIPointer(RS::ARRAY_NORMAL, 2, GL_UNSIGNED_INT, p_mi->surfaces[p_surface].vertex_stride_cache, CAST_INT_TO_UCHAR_PTR(p_mi->surfaces[p_surface].vertex_normal_offset_cache));
  851. } else {
  852. glDisableVertexAttribArray(RS::ARRAY_NORMAL);
  853. }
  854. if ((p_mi->surfaces[p_surface].format_cache & (1 << RS::ARRAY_TANGENT))) {
  855. glEnableVertexAttribArray(RS::ARRAY_TANGENT);
  856. glVertexAttribIPointer(RS::ARRAY_TANGENT, 2, GL_UNSIGNED_INT, p_mi->surfaces[p_surface].vertex_stride_cache, CAST_INT_TO_UCHAR_PTR(p_mi->surfaces[p_surface].vertex_tangent_offset_cache));
  857. } else {
  858. glDisableVertexAttribArray(RS::ARRAY_TANGENT);
  859. }
  860. }
  861. void MeshStorage::_compute_skeleton(MeshInstance *p_mi, Skeleton *p_sk, uint32_t p_surface) {
  862. glBindBuffer(GL_ARRAY_BUFFER, 0);
  863. // Add in the bones and weights.
  864. glBindBuffer(GL_ARRAY_BUFFER, p_mi->mesh->surfaces[p_surface]->skin_buffer);
  865. bool use_8_weights = p_mi->surfaces[p_surface].format_cache & RS::ARRAY_FLAG_USE_8_BONE_WEIGHTS;
  866. int skin_stride = sizeof(int16_t) * (use_8_weights ? 16 : 8);
  867. glEnableVertexAttribArray(RS::ARRAY_BONES);
  868. glVertexAttribIPointer(RS::ARRAY_BONES, 4, GL_UNSIGNED_SHORT, skin_stride, CAST_INT_TO_UCHAR_PTR(0));
  869. if (use_8_weights) {
  870. glEnableVertexAttribArray(11);
  871. glVertexAttribIPointer(11, 4, GL_UNSIGNED_SHORT, skin_stride, CAST_INT_TO_UCHAR_PTR(4 * sizeof(uint16_t)));
  872. glEnableVertexAttribArray(12);
  873. glVertexAttribPointer(12, 4, GL_UNSIGNED_SHORT, GL_TRUE, skin_stride, CAST_INT_TO_UCHAR_PTR(8 * sizeof(uint16_t)));
  874. glEnableVertexAttribArray(13);
  875. glVertexAttribPointer(13, 4, GL_UNSIGNED_SHORT, GL_TRUE, skin_stride, CAST_INT_TO_UCHAR_PTR(12 * sizeof(uint16_t)));
  876. } else {
  877. glEnableVertexAttribArray(RS::ARRAY_WEIGHTS);
  878. glVertexAttribPointer(RS::ARRAY_WEIGHTS, 4, GL_UNSIGNED_SHORT, GL_TRUE, skin_stride, CAST_INT_TO_UCHAR_PTR(4 * sizeof(uint16_t)));
  879. }
  880. glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, p_mi->surfaces[p_surface].vertex_buffer);
  881. glActiveTexture(GL_TEXTURE0);
  882. glBindTexture(GL_TEXTURE_2D, p_sk->transforms_texture);
  883. glBeginTransformFeedback(GL_POINTS);
  884. glDrawArrays(GL_POINTS, 0, p_mi->mesh->surfaces[p_surface]->vertex_count);
  885. glEndTransformFeedback();
  886. glDisableVertexAttribArray(RS::ARRAY_BONES);
  887. glDisableVertexAttribArray(RS::ARRAY_WEIGHTS);
  888. glDisableVertexAttribArray(RS::ARRAY_BONES + 2);
  889. glDisableVertexAttribArray(RS::ARRAY_WEIGHTS + 2);
  890. glBindVertexArray(0);
  891. glBindBuffer(GL_TRANSFORM_FEEDBACK_BUFFER, 0);
  892. }
  893. void MeshStorage::update_mesh_instances() {
  894. if (dirty_mesh_instance_arrays.first() == nullptr) {
  895. return; //nothing to do
  896. }
  897. glEnable(GL_RASTERIZER_DISCARD);
  898. // Process skeletons and blend shapes using transform feedback
  899. while (dirty_mesh_instance_arrays.first()) {
  900. MeshInstance *mi = dirty_mesh_instance_arrays.first()->self();
  901. Skeleton *sk = skeleton_owner.get_or_null(mi->skeleton);
  902. // Precompute base weight if using blend shapes.
  903. float base_weight = 1.0;
  904. if (mi->mesh->blend_shape_count && mi->mesh->blend_shape_mode == RS::BLEND_SHAPE_MODE_NORMALIZED) {
  905. for (uint32_t i = 0; i < mi->mesh->blend_shape_count; i++) {
  906. base_weight -= mi->blend_weights[i];
  907. }
  908. }
  909. for (uint32_t i = 0; i < mi->surfaces.size(); i++) {
  910. if (mi->surfaces[i].vertex_buffer == 0 || mi->mesh->surfaces[i]->skeleton_vertex_array == 0) {
  911. continue;
  912. }
  913. bool array_is_2d = mi->surfaces[i].format_cache & RS::ARRAY_FLAG_USE_2D_VERTICES;
  914. bool can_use_skeleton = sk != nullptr && sk->use_2d == array_is_2d && (mi->surfaces[i].format_cache & RS::ARRAY_FORMAT_BONES);
  915. bool use_8_weights = mi->surfaces[i].format_cache & RS::ARRAY_FLAG_USE_8_BONE_WEIGHTS;
  916. // Always process blend shapes first.
  917. if (mi->mesh->blend_shape_count) {
  918. SkeletonShaderGLES3::ShaderVariant variant = SkeletonShaderGLES3::MODE_BASE_PASS;
  919. uint64_t specialization = 0;
  920. specialization |= array_is_2d ? SkeletonShaderGLES3::MODE_2D : 0;
  921. specialization |= SkeletonShaderGLES3::USE_BLEND_SHAPES;
  922. if (!array_is_2d) {
  923. if ((mi->surfaces[i].format_cache & (1 << RS::ARRAY_NORMAL))) {
  924. specialization |= SkeletonShaderGLES3::USE_NORMAL;
  925. }
  926. if ((mi->surfaces[i].format_cache & (1 << RS::ARRAY_TANGENT))) {
  927. specialization |= SkeletonShaderGLES3::USE_TANGENT;
  928. }
  929. }
  930. bool success = skeleton_shader.shader.version_bind_shader(skeleton_shader.shader_version, variant, specialization);
  931. if (!success) {
  932. continue;
  933. }
  934. skeleton_shader.shader.version_set_uniform(SkeletonShaderGLES3::BLEND_WEIGHT, base_weight, skeleton_shader.shader_version, variant, specialization);
  935. skeleton_shader.shader.version_set_uniform(SkeletonShaderGLES3::BLEND_SHAPE_COUNT, float(mi->mesh->blend_shape_count), skeleton_shader.shader_version, variant, specialization);
  936. glBindBuffer(GL_ARRAY_BUFFER, 0);
  937. glBindVertexArray(mi->mesh->surfaces[i]->skeleton_vertex_array);
  938. glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, mi->surfaces[i].vertex_buffers[0]);
  939. glBeginTransformFeedback(GL_POINTS);
  940. glDrawArrays(GL_POINTS, 0, mi->mesh->surfaces[i]->vertex_count);
  941. glEndTransformFeedback();
  942. variant = SkeletonShaderGLES3::MODE_BLEND_PASS;
  943. success = skeleton_shader.shader.version_bind_shader(skeleton_shader.shader_version, variant, specialization);
  944. if (!success) {
  945. continue;
  946. }
  947. //Do the last blend shape separately, as it can be combined with the skeleton pass.
  948. for (uint32_t bs = 0; bs < mi->mesh->blend_shape_count - 1; bs++) {
  949. float weight = mi->blend_weights[bs];
  950. if (Math::is_zero_approx(weight)) {
  951. //not bother with this one
  952. continue;
  953. }
  954. skeleton_shader.shader.version_set_uniform(SkeletonShaderGLES3::BLEND_WEIGHT, weight, skeleton_shader.shader_version, variant, specialization);
  955. skeleton_shader.shader.version_set_uniform(SkeletonShaderGLES3::BLEND_SHAPE_COUNT, float(mi->mesh->blend_shape_count), skeleton_shader.shader_version, variant, specialization);
  956. glBindVertexArray(mi->mesh->surfaces[i]->blend_shapes[bs].vertex_array);
  957. _blend_shape_bind_mesh_instance_buffer(mi, i);
  958. glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, mi->surfaces[i].vertex_buffers[1]);
  959. glBeginTransformFeedback(GL_POINTS);
  960. glDrawArrays(GL_POINTS, 0, mi->mesh->surfaces[i]->vertex_count);
  961. glEndTransformFeedback();
  962. SWAP(mi->surfaces[i].vertex_buffers[0], mi->surfaces[i].vertex_buffers[1]);
  963. }
  964. uint32_t bs = mi->mesh->blend_shape_count - 1;
  965. float weight = mi->blend_weights[bs];
  966. glBindVertexArray(mi->mesh->surfaces[i]->blend_shapes[bs].vertex_array);
  967. _blend_shape_bind_mesh_instance_buffer(mi, i);
  968. specialization |= can_use_skeleton ? SkeletonShaderGLES3::USE_SKELETON : 0;
  969. specialization |= (can_use_skeleton && use_8_weights) ? SkeletonShaderGLES3::USE_EIGHT_WEIGHTS : 0;
  970. specialization |= SkeletonShaderGLES3::FINAL_PASS;
  971. success = skeleton_shader.shader.version_bind_shader(skeleton_shader.shader_version, variant, specialization);
  972. if (!success) {
  973. continue;
  974. }
  975. skeleton_shader.shader.version_set_uniform(SkeletonShaderGLES3::BLEND_WEIGHT, weight, skeleton_shader.shader_version, variant, specialization);
  976. skeleton_shader.shader.version_set_uniform(SkeletonShaderGLES3::BLEND_SHAPE_COUNT, float(mi->mesh->blend_shape_count), skeleton_shader.shader_version, variant, specialization);
  977. if (can_use_skeleton) {
  978. Transform2D transform = mi->canvas_item_transform_2d.affine_inverse() * sk->base_transform_2d;
  979. skeleton_shader.shader.version_set_uniform(SkeletonShaderGLES3::SKELETON_TRANSFORM_X, transform[0], skeleton_shader.shader_version, variant, specialization);
  980. skeleton_shader.shader.version_set_uniform(SkeletonShaderGLES3::SKELETON_TRANSFORM_Y, transform[1], skeleton_shader.shader_version, variant, specialization);
  981. skeleton_shader.shader.version_set_uniform(SkeletonShaderGLES3::SKELETON_TRANSFORM_OFFSET, transform[2], skeleton_shader.shader_version, variant, specialization);
  982. Transform2D inverse_transform = transform.affine_inverse();
  983. skeleton_shader.shader.version_set_uniform(SkeletonShaderGLES3::INVERSE_TRANSFORM_X, inverse_transform[0], skeleton_shader.shader_version, variant, specialization);
  984. skeleton_shader.shader.version_set_uniform(SkeletonShaderGLES3::INVERSE_TRANSFORM_Y, inverse_transform[1], skeleton_shader.shader_version, variant, specialization);
  985. skeleton_shader.shader.version_set_uniform(SkeletonShaderGLES3::INVERSE_TRANSFORM_OFFSET, inverse_transform[2], skeleton_shader.shader_version, variant, specialization);
  986. // Do last blendshape in the same pass as the Skeleton.
  987. _compute_skeleton(mi, sk, i);
  988. can_use_skeleton = false;
  989. } else {
  990. // Do last blendshape by itself and prepare vertex data for use by the renderer.
  991. glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, mi->surfaces[i].vertex_buffer);
  992. glBeginTransformFeedback(GL_POINTS);
  993. glDrawArrays(GL_POINTS, 0, mi->mesh->surfaces[i]->vertex_count);
  994. glEndTransformFeedback();
  995. }
  996. glBindVertexArray(0);
  997. glBindBuffer(GL_TRANSFORM_FEEDBACK_BUFFER, 0);
  998. }
  999. // This branch should only execute when Skeleton is run by itself.
  1000. if (can_use_skeleton) {
  1001. SkeletonShaderGLES3::ShaderVariant variant = SkeletonShaderGLES3::MODE_BASE_PASS;
  1002. uint64_t specialization = 0;
  1003. specialization |= array_is_2d ? SkeletonShaderGLES3::MODE_2D : 0;
  1004. specialization |= SkeletonShaderGLES3::USE_SKELETON;
  1005. specialization |= SkeletonShaderGLES3::FINAL_PASS;
  1006. specialization |= use_8_weights ? SkeletonShaderGLES3::USE_EIGHT_WEIGHTS : 0;
  1007. if (!array_is_2d) {
  1008. if ((mi->surfaces[i].format_cache & (1 << RS::ARRAY_NORMAL))) {
  1009. specialization |= SkeletonShaderGLES3::USE_NORMAL;
  1010. }
  1011. if ((mi->surfaces[i].format_cache & (1 << RS::ARRAY_TANGENT))) {
  1012. specialization |= SkeletonShaderGLES3::USE_TANGENT;
  1013. }
  1014. }
  1015. bool success = skeleton_shader.shader.version_bind_shader(skeleton_shader.shader_version, variant, specialization);
  1016. if (!success) {
  1017. continue;
  1018. }
  1019. Transform2D transform = mi->canvas_item_transform_2d.affine_inverse() * sk->base_transform_2d;
  1020. skeleton_shader.shader.version_set_uniform(SkeletonShaderGLES3::SKELETON_TRANSFORM_X, transform[0], skeleton_shader.shader_version, variant, specialization);
  1021. skeleton_shader.shader.version_set_uniform(SkeletonShaderGLES3::SKELETON_TRANSFORM_Y, transform[1], skeleton_shader.shader_version, variant, specialization);
  1022. skeleton_shader.shader.version_set_uniform(SkeletonShaderGLES3::SKELETON_TRANSFORM_OFFSET, transform[2], skeleton_shader.shader_version, variant, specialization);
  1023. Transform2D inverse_transform = transform.affine_inverse();
  1024. skeleton_shader.shader.version_set_uniform(SkeletonShaderGLES3::INVERSE_TRANSFORM_X, inverse_transform[0], skeleton_shader.shader_version, variant, specialization);
  1025. skeleton_shader.shader.version_set_uniform(SkeletonShaderGLES3::INVERSE_TRANSFORM_Y, inverse_transform[1], skeleton_shader.shader_version, variant, specialization);
  1026. skeleton_shader.shader.version_set_uniform(SkeletonShaderGLES3::INVERSE_TRANSFORM_OFFSET, inverse_transform[2], skeleton_shader.shader_version, variant, specialization);
  1027. glBindVertexArray(mi->mesh->surfaces[i]->skeleton_vertex_array);
  1028. _compute_skeleton(mi, sk, i);
  1029. }
  1030. }
  1031. mi->dirty = false;
  1032. if (sk) {
  1033. mi->skeleton_version = sk->version;
  1034. }
  1035. dirty_mesh_instance_arrays.remove(&mi->array_update_list);
  1036. }
  1037. glDisable(GL_RASTERIZER_DISCARD);
  1038. glBindBuffer(GL_ARRAY_BUFFER, 0);
  1039. glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, 0);
  1040. }
  1041. /* MULTIMESH API */
  1042. RID MeshStorage::multimesh_allocate() {
  1043. return multimesh_owner.allocate_rid();
  1044. }
  1045. void MeshStorage::multimesh_initialize(RID p_rid) {
  1046. multimesh_owner.initialize_rid(p_rid, MultiMesh());
  1047. }
  1048. void MeshStorage::multimesh_free(RID p_rid) {
  1049. _update_dirty_multimeshes();
  1050. multimesh_allocate_data(p_rid, 0, RS::MULTIMESH_TRANSFORM_2D);
  1051. MultiMesh *multimesh = multimesh_owner.get_or_null(p_rid);
  1052. multimesh->dependency.deleted_notify(p_rid);
  1053. multimesh_owner.free(p_rid);
  1054. }
  1055. void MeshStorage::multimesh_allocate_data(RID p_multimesh, int p_instances, RS::MultimeshTransformFormat p_transform_format, bool p_use_colors, bool p_use_custom_data) {
  1056. MultiMesh *multimesh = multimesh_owner.get_or_null(p_multimesh);
  1057. ERR_FAIL_COND(!multimesh);
  1058. if (multimesh->instances == p_instances && multimesh->xform_format == p_transform_format && multimesh->uses_colors == p_use_colors && multimesh->uses_custom_data == p_use_custom_data) {
  1059. return;
  1060. }
  1061. if (multimesh->buffer) {
  1062. glDeleteBuffers(1, &multimesh->buffer);
  1063. multimesh->buffer = 0;
  1064. }
  1065. if (multimesh->data_cache_dirty_regions) {
  1066. memdelete_arr(multimesh->data_cache_dirty_regions);
  1067. multimesh->data_cache_dirty_regions = nullptr;
  1068. multimesh->data_cache_used_dirty_regions = 0;
  1069. }
  1070. multimesh->instances = p_instances;
  1071. multimesh->xform_format = p_transform_format;
  1072. multimesh->uses_colors = p_use_colors;
  1073. multimesh->color_offset_cache = p_transform_format == RS::MULTIMESH_TRANSFORM_2D ? 8 : 12;
  1074. multimesh->uses_custom_data = p_use_custom_data;
  1075. multimesh->custom_data_offset_cache = multimesh->color_offset_cache + (p_use_colors ? 2 : 0);
  1076. multimesh->stride_cache = multimesh->custom_data_offset_cache + (p_use_custom_data ? 2 : 0);
  1077. multimesh->buffer_set = false;
  1078. multimesh->data_cache = Vector<float>();
  1079. multimesh->aabb = AABB();
  1080. multimesh->aabb_dirty = false;
  1081. multimesh->visible_instances = MIN(multimesh->visible_instances, multimesh->instances);
  1082. if (multimesh->instances) {
  1083. glGenBuffers(1, &multimesh->buffer);
  1084. glBindBuffer(GL_ARRAY_BUFFER, multimesh->buffer);
  1085. glBufferData(GL_ARRAY_BUFFER, multimesh->instances * multimesh->stride_cache * sizeof(float), nullptr, GL_STATIC_DRAW);
  1086. glBindBuffer(GL_ARRAY_BUFFER, 0);
  1087. }
  1088. multimesh->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_MULTIMESH);
  1089. }
  1090. int MeshStorage::multimesh_get_instance_count(RID p_multimesh) const {
  1091. MultiMesh *multimesh = multimesh_owner.get_or_null(p_multimesh);
  1092. ERR_FAIL_COND_V(!multimesh, 0);
  1093. return multimesh->instances;
  1094. }
  1095. void MeshStorage::multimesh_set_mesh(RID p_multimesh, RID p_mesh) {
  1096. MultiMesh *multimesh = multimesh_owner.get_or_null(p_multimesh);
  1097. ERR_FAIL_COND(!multimesh);
  1098. if (multimesh->mesh == p_mesh || p_mesh.is_null()) {
  1099. return;
  1100. }
  1101. multimesh->mesh = p_mesh;
  1102. if (multimesh->instances == 0) {
  1103. return;
  1104. }
  1105. if (multimesh->data_cache.size()) {
  1106. //we have a data cache, just mark it dirty
  1107. _multimesh_mark_all_dirty(multimesh, false, true);
  1108. } else if (multimesh->instances) {
  1109. // Need to re-create AABB. Unfortunately, calling this has a penalty.
  1110. if (multimesh->buffer_set) {
  1111. Vector<uint8_t> buffer = Utilities::buffer_get_data(GL_ARRAY_BUFFER, multimesh->buffer, multimesh->instances * multimesh->stride_cache * sizeof(float));
  1112. const uint8_t *r = buffer.ptr();
  1113. const float *data = (const float *)r;
  1114. _multimesh_re_create_aabb(multimesh, data, multimesh->instances);
  1115. }
  1116. }
  1117. multimesh->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_MESH);
  1118. }
  1119. #define MULTIMESH_DIRTY_REGION_SIZE 512
  1120. void MeshStorage::_multimesh_make_local(MultiMesh *multimesh) const {
  1121. if (multimesh->data_cache.size() > 0 || multimesh->instances == 0) {
  1122. return; //already local
  1123. }
  1124. ERR_FAIL_COND(multimesh->data_cache.size() > 0);
  1125. // this means that the user wants to load/save individual elements,
  1126. // for this, the data must reside on CPU, so just copy it there.
  1127. multimesh->data_cache.resize(multimesh->instances * multimesh->stride_cache);
  1128. {
  1129. float *w = multimesh->data_cache.ptrw();
  1130. if (multimesh->buffer_set) {
  1131. Vector<uint8_t> buffer = Utilities::buffer_get_data(GL_ARRAY_BUFFER, multimesh->buffer, multimesh->instances * multimesh->stride_cache * sizeof(float));
  1132. {
  1133. const uint8_t *r = buffer.ptr();
  1134. memcpy(w, r, buffer.size());
  1135. }
  1136. } else {
  1137. memset(w, 0, (size_t)multimesh->instances * multimesh->stride_cache * sizeof(float));
  1138. }
  1139. }
  1140. uint32_t data_cache_dirty_region_count = (multimesh->instances - 1) / MULTIMESH_DIRTY_REGION_SIZE + 1;
  1141. multimesh->data_cache_dirty_regions = memnew_arr(bool, data_cache_dirty_region_count);
  1142. for (uint32_t i = 0; i < data_cache_dirty_region_count; i++) {
  1143. multimesh->data_cache_dirty_regions[i] = false;
  1144. }
  1145. multimesh->data_cache_used_dirty_regions = 0;
  1146. }
  1147. void MeshStorage::_multimesh_mark_dirty(MultiMesh *multimesh, int p_index, bool p_aabb) {
  1148. uint32_t region_index = p_index / MULTIMESH_DIRTY_REGION_SIZE;
  1149. #ifdef DEBUG_ENABLED
  1150. uint32_t data_cache_dirty_region_count = (multimesh->instances - 1) / MULTIMESH_DIRTY_REGION_SIZE + 1;
  1151. ERR_FAIL_UNSIGNED_INDEX(region_index, data_cache_dirty_region_count); //bug
  1152. #endif
  1153. if (!multimesh->data_cache_dirty_regions[region_index]) {
  1154. multimesh->data_cache_dirty_regions[region_index] = true;
  1155. multimesh->data_cache_used_dirty_regions++;
  1156. }
  1157. if (p_aabb) {
  1158. multimesh->aabb_dirty = true;
  1159. }
  1160. if (!multimesh->dirty) {
  1161. multimesh->dirty_list = multimesh_dirty_list;
  1162. multimesh_dirty_list = multimesh;
  1163. multimesh->dirty = true;
  1164. }
  1165. }
  1166. void MeshStorage::_multimesh_mark_all_dirty(MultiMesh *multimesh, bool p_data, bool p_aabb) {
  1167. if (p_data) {
  1168. uint32_t data_cache_dirty_region_count = (multimesh->instances - 1) / MULTIMESH_DIRTY_REGION_SIZE + 1;
  1169. for (uint32_t i = 0; i < data_cache_dirty_region_count; i++) {
  1170. if (!multimesh->data_cache_dirty_regions[i]) {
  1171. multimesh->data_cache_dirty_regions[i] = true;
  1172. multimesh->data_cache_used_dirty_regions++;
  1173. }
  1174. }
  1175. }
  1176. if (p_aabb) {
  1177. multimesh->aabb_dirty = true;
  1178. }
  1179. if (!multimesh->dirty) {
  1180. multimesh->dirty_list = multimesh_dirty_list;
  1181. multimesh_dirty_list = multimesh;
  1182. multimesh->dirty = true;
  1183. }
  1184. }
  1185. void MeshStorage::_multimesh_re_create_aabb(MultiMesh *multimesh, const float *p_data, int p_instances) {
  1186. ERR_FAIL_COND(multimesh->mesh.is_null());
  1187. AABB aabb;
  1188. AABB mesh_aabb = mesh_get_aabb(multimesh->mesh);
  1189. for (int i = 0; i < p_instances; i++) {
  1190. const float *data = p_data + multimesh->stride_cache * i;
  1191. Transform3D t;
  1192. if (multimesh->xform_format == RS::MULTIMESH_TRANSFORM_3D) {
  1193. t.basis.rows[0][0] = data[0];
  1194. t.basis.rows[0][1] = data[1];
  1195. t.basis.rows[0][2] = data[2];
  1196. t.origin.x = data[3];
  1197. t.basis.rows[1][0] = data[4];
  1198. t.basis.rows[1][1] = data[5];
  1199. t.basis.rows[1][2] = data[6];
  1200. t.origin.y = data[7];
  1201. t.basis.rows[2][0] = data[8];
  1202. t.basis.rows[2][1] = data[9];
  1203. t.basis.rows[2][2] = data[10];
  1204. t.origin.z = data[11];
  1205. } else {
  1206. t.basis.rows[0][0] = data[0];
  1207. t.basis.rows[0][1] = data[1];
  1208. t.origin.x = data[3];
  1209. t.basis.rows[1][0] = data[4];
  1210. t.basis.rows[1][1] = data[5];
  1211. t.origin.y = data[7];
  1212. }
  1213. if (i == 0) {
  1214. aabb = t.xform(mesh_aabb);
  1215. } else {
  1216. aabb.merge_with(t.xform(mesh_aabb));
  1217. }
  1218. }
  1219. multimesh->aabb = aabb;
  1220. }
  1221. void MeshStorage::multimesh_instance_set_transform(RID p_multimesh, int p_index, const Transform3D &p_transform) {
  1222. MultiMesh *multimesh = multimesh_owner.get_or_null(p_multimesh);
  1223. ERR_FAIL_COND(!multimesh);
  1224. ERR_FAIL_INDEX(p_index, multimesh->instances);
  1225. ERR_FAIL_COND(multimesh->xform_format != RS::MULTIMESH_TRANSFORM_3D);
  1226. _multimesh_make_local(multimesh);
  1227. {
  1228. float *w = multimesh->data_cache.ptrw();
  1229. float *dataptr = w + p_index * multimesh->stride_cache;
  1230. dataptr[0] = p_transform.basis.rows[0][0];
  1231. dataptr[1] = p_transform.basis.rows[0][1];
  1232. dataptr[2] = p_transform.basis.rows[0][2];
  1233. dataptr[3] = p_transform.origin.x;
  1234. dataptr[4] = p_transform.basis.rows[1][0];
  1235. dataptr[5] = p_transform.basis.rows[1][1];
  1236. dataptr[6] = p_transform.basis.rows[1][2];
  1237. dataptr[7] = p_transform.origin.y;
  1238. dataptr[8] = p_transform.basis.rows[2][0];
  1239. dataptr[9] = p_transform.basis.rows[2][1];
  1240. dataptr[10] = p_transform.basis.rows[2][2];
  1241. dataptr[11] = p_transform.origin.z;
  1242. }
  1243. _multimesh_mark_dirty(multimesh, p_index, true);
  1244. }
  1245. void MeshStorage::multimesh_instance_set_transform_2d(RID p_multimesh, int p_index, const Transform2D &p_transform) {
  1246. MultiMesh *multimesh = multimesh_owner.get_or_null(p_multimesh);
  1247. ERR_FAIL_COND(!multimesh);
  1248. ERR_FAIL_INDEX(p_index, multimesh->instances);
  1249. ERR_FAIL_COND(multimesh->xform_format != RS::MULTIMESH_TRANSFORM_2D);
  1250. _multimesh_make_local(multimesh);
  1251. {
  1252. float *w = multimesh->data_cache.ptrw();
  1253. float *dataptr = w + p_index * multimesh->stride_cache;
  1254. dataptr[0] = p_transform.columns[0][0];
  1255. dataptr[1] = p_transform.columns[1][0];
  1256. dataptr[2] = 0;
  1257. dataptr[3] = p_transform.columns[2][0];
  1258. dataptr[4] = p_transform.columns[0][1];
  1259. dataptr[5] = p_transform.columns[1][1];
  1260. dataptr[6] = 0;
  1261. dataptr[7] = p_transform.columns[2][1];
  1262. }
  1263. _multimesh_mark_dirty(multimesh, p_index, true);
  1264. }
  1265. void MeshStorage::multimesh_instance_set_color(RID p_multimesh, int p_index, const Color &p_color) {
  1266. MultiMesh *multimesh = multimesh_owner.get_or_null(p_multimesh);
  1267. ERR_FAIL_COND(!multimesh);
  1268. ERR_FAIL_INDEX(p_index, multimesh->instances);
  1269. ERR_FAIL_COND(!multimesh->uses_colors);
  1270. _multimesh_make_local(multimesh);
  1271. {
  1272. // Colors are packed into 2 floats.
  1273. float *w = multimesh->data_cache.ptrw();
  1274. float *dataptr = w + p_index * multimesh->stride_cache + multimesh->color_offset_cache;
  1275. uint16_t val[4] = { Math::make_half_float(p_color.r), Math::make_half_float(p_color.g), Math::make_half_float(p_color.b), Math::make_half_float(p_color.a) };
  1276. memcpy(dataptr, val, 2 * 4);
  1277. }
  1278. _multimesh_mark_dirty(multimesh, p_index, false);
  1279. }
  1280. void MeshStorage::multimesh_instance_set_custom_data(RID p_multimesh, int p_index, const Color &p_color) {
  1281. MultiMesh *multimesh = multimesh_owner.get_or_null(p_multimesh);
  1282. ERR_FAIL_COND(!multimesh);
  1283. ERR_FAIL_INDEX(p_index, multimesh->instances);
  1284. ERR_FAIL_COND(!multimesh->uses_custom_data);
  1285. _multimesh_make_local(multimesh);
  1286. {
  1287. float *w = multimesh->data_cache.ptrw();
  1288. float *dataptr = w + p_index * multimesh->stride_cache + multimesh->custom_data_offset_cache;
  1289. uint16_t val[4] = { Math::make_half_float(p_color.r), Math::make_half_float(p_color.g), Math::make_half_float(p_color.b), Math::make_half_float(p_color.a) };
  1290. memcpy(dataptr, val, 2 * 4);
  1291. }
  1292. _multimesh_mark_dirty(multimesh, p_index, false);
  1293. }
  1294. RID MeshStorage::multimesh_get_mesh(RID p_multimesh) const {
  1295. MultiMesh *multimesh = multimesh_owner.get_or_null(p_multimesh);
  1296. ERR_FAIL_COND_V(!multimesh, RID());
  1297. return multimesh->mesh;
  1298. }
  1299. AABB MeshStorage::multimesh_get_aabb(RID p_multimesh) const {
  1300. MultiMesh *multimesh = multimesh_owner.get_or_null(p_multimesh);
  1301. ERR_FAIL_COND_V(!multimesh, AABB());
  1302. if (multimesh->aabb_dirty) {
  1303. const_cast<MeshStorage *>(this)->_update_dirty_multimeshes();
  1304. }
  1305. return multimesh->aabb;
  1306. }
  1307. Transform3D MeshStorage::multimesh_instance_get_transform(RID p_multimesh, int p_index) const {
  1308. MultiMesh *multimesh = multimesh_owner.get_or_null(p_multimesh);
  1309. ERR_FAIL_COND_V(!multimesh, Transform3D());
  1310. ERR_FAIL_INDEX_V(p_index, multimesh->instances, Transform3D());
  1311. ERR_FAIL_COND_V(multimesh->xform_format != RS::MULTIMESH_TRANSFORM_3D, Transform3D());
  1312. _multimesh_make_local(multimesh);
  1313. Transform3D t;
  1314. {
  1315. const float *r = multimesh->data_cache.ptr();
  1316. const float *dataptr = r + p_index * multimesh->stride_cache;
  1317. t.basis.rows[0][0] = dataptr[0];
  1318. t.basis.rows[0][1] = dataptr[1];
  1319. t.basis.rows[0][2] = dataptr[2];
  1320. t.origin.x = dataptr[3];
  1321. t.basis.rows[1][0] = dataptr[4];
  1322. t.basis.rows[1][1] = dataptr[5];
  1323. t.basis.rows[1][2] = dataptr[6];
  1324. t.origin.y = dataptr[7];
  1325. t.basis.rows[2][0] = dataptr[8];
  1326. t.basis.rows[2][1] = dataptr[9];
  1327. t.basis.rows[2][2] = dataptr[10];
  1328. t.origin.z = dataptr[11];
  1329. }
  1330. return t;
  1331. }
  1332. Transform2D MeshStorage::multimesh_instance_get_transform_2d(RID p_multimesh, int p_index) const {
  1333. MultiMesh *multimesh = multimesh_owner.get_or_null(p_multimesh);
  1334. ERR_FAIL_COND_V(!multimesh, Transform2D());
  1335. ERR_FAIL_INDEX_V(p_index, multimesh->instances, Transform2D());
  1336. ERR_FAIL_COND_V(multimesh->xform_format != RS::MULTIMESH_TRANSFORM_2D, Transform2D());
  1337. _multimesh_make_local(multimesh);
  1338. Transform2D t;
  1339. {
  1340. const float *r = multimesh->data_cache.ptr();
  1341. const float *dataptr = r + p_index * multimesh->stride_cache;
  1342. t.columns[0][0] = dataptr[0];
  1343. t.columns[1][0] = dataptr[1];
  1344. t.columns[2][0] = dataptr[3];
  1345. t.columns[0][1] = dataptr[4];
  1346. t.columns[1][1] = dataptr[5];
  1347. t.columns[2][1] = dataptr[7];
  1348. }
  1349. return t;
  1350. }
  1351. Color MeshStorage::multimesh_instance_get_color(RID p_multimesh, int p_index) const {
  1352. MultiMesh *multimesh = multimesh_owner.get_or_null(p_multimesh);
  1353. ERR_FAIL_COND_V(!multimesh, Color());
  1354. ERR_FAIL_INDEX_V(p_index, multimesh->instances, Color());
  1355. ERR_FAIL_COND_V(!multimesh->uses_colors, Color());
  1356. _multimesh_make_local(multimesh);
  1357. Color c;
  1358. {
  1359. const float *r = multimesh->data_cache.ptr();
  1360. const float *dataptr = r + p_index * multimesh->stride_cache + multimesh->color_offset_cache;
  1361. uint16_t raw_data[4];
  1362. memcpy(raw_data, dataptr, 2 * 4);
  1363. c.r = Math::half_to_float(raw_data[0]);
  1364. c.g = Math::half_to_float(raw_data[1]);
  1365. c.b = Math::half_to_float(raw_data[2]);
  1366. c.a = Math::half_to_float(raw_data[3]);
  1367. }
  1368. return c;
  1369. }
  1370. Color MeshStorage::multimesh_instance_get_custom_data(RID p_multimesh, int p_index) const {
  1371. MultiMesh *multimesh = multimesh_owner.get_or_null(p_multimesh);
  1372. ERR_FAIL_COND_V(!multimesh, Color());
  1373. ERR_FAIL_INDEX_V(p_index, multimesh->instances, Color());
  1374. ERR_FAIL_COND_V(!multimesh->uses_custom_data, Color());
  1375. _multimesh_make_local(multimesh);
  1376. Color c;
  1377. {
  1378. const float *r = multimesh->data_cache.ptr();
  1379. const float *dataptr = r + p_index * multimesh->stride_cache + multimesh->custom_data_offset_cache;
  1380. uint16_t raw_data[4];
  1381. memcpy(raw_data, dataptr, 2 * 4);
  1382. c.r = Math::half_to_float(raw_data[0]);
  1383. c.g = Math::half_to_float(raw_data[1]);
  1384. c.b = Math::half_to_float(raw_data[2]);
  1385. c.a = Math::half_to_float(raw_data[3]);
  1386. }
  1387. return c;
  1388. }
  1389. void MeshStorage::multimesh_set_buffer(RID p_multimesh, const Vector<float> &p_buffer) {
  1390. MultiMesh *multimesh = multimesh_owner.get_or_null(p_multimesh);
  1391. ERR_FAIL_COND(!multimesh);
  1392. if (multimesh->uses_colors || multimesh->uses_custom_data) {
  1393. // Color and custom need to be packed so copy buffer to data_cache and pack.
  1394. _multimesh_make_local(multimesh);
  1395. multimesh->data_cache = p_buffer;
  1396. float *w = multimesh->data_cache.ptrw();
  1397. uint32_t old_stride = multimesh->xform_format == RS::MULTIMESH_TRANSFORM_2D ? 8 : 12;
  1398. old_stride += multimesh->uses_colors ? 4 : 0;
  1399. old_stride += multimesh->uses_custom_data ? 4 : 0;
  1400. ERR_FAIL_COND(p_buffer.size() != (multimesh->instances * (int)old_stride));
  1401. for (int i = 0; i < multimesh->instances; i++) {
  1402. {
  1403. float *dataptr = w + i * old_stride;
  1404. float *newptr = w + i * multimesh->stride_cache;
  1405. float vals[8] = { dataptr[0], dataptr[1], dataptr[2], dataptr[3], dataptr[4], dataptr[5], dataptr[6], dataptr[7] };
  1406. memcpy(newptr, vals, 8 * 4);
  1407. }
  1408. if (multimesh->xform_format == RS::MULTIMESH_TRANSFORM_3D) {
  1409. float *dataptr = w + i * old_stride + 8;
  1410. float *newptr = w + i * multimesh->stride_cache + 8;
  1411. float vals[8] = { dataptr[0], dataptr[1], dataptr[2], dataptr[3] };
  1412. memcpy(newptr, vals, 4 * 4);
  1413. }
  1414. if (multimesh->uses_colors) {
  1415. float *dataptr = w + i * old_stride + (multimesh->xform_format == RS::MULTIMESH_TRANSFORM_2D ? 8 : 12);
  1416. float *newptr = w + i * multimesh->stride_cache + multimesh->color_offset_cache;
  1417. uint16_t val[4] = { Math::make_half_float(dataptr[0]), Math::make_half_float(dataptr[1]), Math::make_half_float(dataptr[2]), Math::make_half_float(dataptr[3]) };
  1418. memcpy(newptr, val, 2 * 4);
  1419. }
  1420. if (multimesh->uses_custom_data) {
  1421. float *dataptr = w + i * old_stride + (multimesh->xform_format == RS::MULTIMESH_TRANSFORM_2D ? 8 : 12) + (multimesh->uses_colors ? 4 : 0);
  1422. float *newptr = w + i * multimesh->stride_cache + multimesh->custom_data_offset_cache;
  1423. uint16_t val[4] = { Math::make_half_float(dataptr[0]), Math::make_half_float(dataptr[1]), Math::make_half_float(dataptr[2]), Math::make_half_float(dataptr[3]) };
  1424. memcpy(newptr, val, 2 * 4);
  1425. }
  1426. }
  1427. multimesh->data_cache.resize(multimesh->instances * (int)multimesh->stride_cache);
  1428. const float *r = multimesh->data_cache.ptr();
  1429. glBindBuffer(GL_ARRAY_BUFFER, multimesh->buffer);
  1430. glBufferData(GL_ARRAY_BUFFER, multimesh->data_cache.size() * sizeof(float), r, GL_STATIC_DRAW);
  1431. glBindBuffer(GL_ARRAY_BUFFER, 0);
  1432. } else {
  1433. // Only Transform is being used, so we can upload directly.
  1434. ERR_FAIL_COND(p_buffer.size() != (multimesh->instances * (int)multimesh->stride_cache));
  1435. const float *r = p_buffer.ptr();
  1436. glBindBuffer(GL_ARRAY_BUFFER, multimesh->buffer);
  1437. glBufferData(GL_ARRAY_BUFFER, p_buffer.size() * sizeof(float), r, GL_STATIC_DRAW);
  1438. glBindBuffer(GL_ARRAY_BUFFER, 0);
  1439. }
  1440. multimesh->buffer_set = true;
  1441. if (multimesh->data_cache.size() || multimesh->uses_colors || multimesh->uses_custom_data) {
  1442. //if we have a data cache, just update it
  1443. multimesh->data_cache = multimesh->data_cache;
  1444. {
  1445. //clear dirty since nothing will be dirty anymore
  1446. uint32_t data_cache_dirty_region_count = (multimesh->instances - 1) / MULTIMESH_DIRTY_REGION_SIZE + 1;
  1447. for (uint32_t i = 0; i < data_cache_dirty_region_count; i++) {
  1448. multimesh->data_cache_dirty_regions[i] = false;
  1449. }
  1450. multimesh->data_cache_used_dirty_regions = 0;
  1451. }
  1452. _multimesh_mark_all_dirty(multimesh, false, true); //update AABB
  1453. } else if (multimesh->mesh.is_valid()) {
  1454. //if we have a mesh set, we need to re-generate the AABB from the new data
  1455. const float *data = p_buffer.ptr();
  1456. _multimesh_re_create_aabb(multimesh, data, multimesh->instances);
  1457. multimesh->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_AABB);
  1458. }
  1459. }
  1460. Vector<float> MeshStorage::multimesh_get_buffer(RID p_multimesh) const {
  1461. MultiMesh *multimesh = multimesh_owner.get_or_null(p_multimesh);
  1462. ERR_FAIL_COND_V(!multimesh, Vector<float>());
  1463. Vector<float> ret;
  1464. if (multimesh->buffer == 0 || multimesh->instances == 0) {
  1465. return Vector<float>();
  1466. } else if (multimesh->data_cache.size()) {
  1467. ret = multimesh->data_cache;
  1468. } else {
  1469. // Buffer not cached, so fetch from GPU memory. This can be a stalling operation, avoid whenever possible.
  1470. Vector<uint8_t> buffer = Utilities::buffer_get_data(GL_ARRAY_BUFFER, multimesh->buffer, multimesh->instances * multimesh->stride_cache * sizeof(float));
  1471. ret.resize(multimesh->instances * multimesh->stride_cache);
  1472. {
  1473. float *w = ret.ptrw();
  1474. const uint8_t *r = buffer.ptr();
  1475. memcpy(w, r, buffer.size());
  1476. }
  1477. }
  1478. if (multimesh->uses_colors || multimesh->uses_custom_data) {
  1479. // Need to decompress buffer.
  1480. uint32_t new_stride = multimesh->xform_format == RS::MULTIMESH_TRANSFORM_2D ? 8 : 12;
  1481. new_stride += multimesh->uses_colors ? 4 : 0;
  1482. new_stride += multimesh->uses_custom_data ? 4 : 0;
  1483. Vector<float> decompressed;
  1484. decompressed.resize(multimesh->instances * (int)new_stride);
  1485. float *w = decompressed.ptrw();
  1486. const float *r = ret.ptr();
  1487. for (int i = 0; i < multimesh->instances; i++) {
  1488. {
  1489. float *newptr = w + i * new_stride;
  1490. const float *oldptr = r + i * multimesh->stride_cache;
  1491. float vals[8] = { oldptr[0], oldptr[1], oldptr[2], oldptr[3], oldptr[4], oldptr[5], oldptr[6], oldptr[7] };
  1492. memcpy(newptr, vals, 8 * 4);
  1493. }
  1494. if (multimesh->xform_format == RS::MULTIMESH_TRANSFORM_3D) {
  1495. float *newptr = w + i * new_stride + 8;
  1496. const float *oldptr = r + i * multimesh->stride_cache + 8;
  1497. float vals[8] = { oldptr[0], oldptr[1], oldptr[2], oldptr[3] };
  1498. memcpy(newptr, vals, 4 * 4);
  1499. }
  1500. if (multimesh->uses_colors) {
  1501. float *newptr = w + i * new_stride + (multimesh->xform_format == RS::MULTIMESH_TRANSFORM_2D ? 8 : 12);
  1502. const float *oldptr = r + i * multimesh->stride_cache + multimesh->color_offset_cache;
  1503. uint16_t raw_data[4];
  1504. memcpy(raw_data, oldptr, 2 * 4);
  1505. newptr[0] = Math::half_to_float(raw_data[0]);
  1506. newptr[1] = Math::half_to_float(raw_data[1]);
  1507. newptr[2] = Math::half_to_float(raw_data[2]);
  1508. newptr[3] = Math::half_to_float(raw_data[3]);
  1509. }
  1510. if (multimesh->uses_custom_data) {
  1511. float *newptr = w + i * new_stride + (multimesh->xform_format == RS::MULTIMESH_TRANSFORM_2D ? 8 : 12) + (multimesh->uses_colors ? 4 : 0);
  1512. const float *oldptr = r + i * multimesh->stride_cache + multimesh->custom_data_offset_cache;
  1513. uint16_t raw_data[4];
  1514. memcpy(raw_data, oldptr, 2 * 4);
  1515. newptr[0] = Math::half_to_float(raw_data[0]);
  1516. newptr[1] = Math::half_to_float(raw_data[1]);
  1517. newptr[2] = Math::half_to_float(raw_data[2]);
  1518. newptr[3] = Math::half_to_float(raw_data[3]);
  1519. }
  1520. }
  1521. return decompressed;
  1522. } else {
  1523. return ret;
  1524. }
  1525. }
  1526. void MeshStorage::multimesh_set_visible_instances(RID p_multimesh, int p_visible) {
  1527. MultiMesh *multimesh = multimesh_owner.get_or_null(p_multimesh);
  1528. ERR_FAIL_COND(!multimesh);
  1529. ERR_FAIL_COND(p_visible < -1 || p_visible > multimesh->instances);
  1530. if (multimesh->visible_instances == p_visible) {
  1531. return;
  1532. }
  1533. if (multimesh->data_cache.size()) {
  1534. // There is a data cache, but we may need to update some sections.
  1535. _multimesh_mark_all_dirty(multimesh, false, true);
  1536. int start = multimesh->visible_instances >= 0 ? multimesh->visible_instances : multimesh->instances;
  1537. for (int i = start; i < p_visible; i++) {
  1538. _multimesh_mark_dirty(multimesh, i, true);
  1539. }
  1540. }
  1541. multimesh->visible_instances = p_visible;
  1542. multimesh->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_MULTIMESH_VISIBLE_INSTANCES);
  1543. }
  1544. int MeshStorage::multimesh_get_visible_instances(RID p_multimesh) const {
  1545. MultiMesh *multimesh = multimesh_owner.get_or_null(p_multimesh);
  1546. ERR_FAIL_COND_V(!multimesh, 0);
  1547. return multimesh->visible_instances;
  1548. }
  1549. void MeshStorage::_update_dirty_multimeshes() {
  1550. while (multimesh_dirty_list) {
  1551. MultiMesh *multimesh = multimesh_dirty_list;
  1552. if (multimesh->data_cache.size()) { //may have been cleared, so only process if it exists
  1553. const float *data = multimesh->data_cache.ptr();
  1554. uint32_t visible_instances = multimesh->visible_instances >= 0 ? multimesh->visible_instances : multimesh->instances;
  1555. if (multimesh->data_cache_used_dirty_regions) {
  1556. uint32_t data_cache_dirty_region_count = (multimesh->instances - 1) / MULTIMESH_DIRTY_REGION_SIZE + 1;
  1557. uint32_t visible_region_count = visible_instances == 0 ? 0 : (visible_instances - 1) / MULTIMESH_DIRTY_REGION_SIZE + 1;
  1558. GLint region_size = multimesh->stride_cache * MULTIMESH_DIRTY_REGION_SIZE * sizeof(float);
  1559. if (multimesh->data_cache_used_dirty_regions > 32 || multimesh->data_cache_used_dirty_regions > visible_region_count / 2) {
  1560. // If there too many dirty regions, or represent the majority of regions, just copy all, else transfer cost piles up too much
  1561. glBindBuffer(GL_ARRAY_BUFFER, multimesh->buffer);
  1562. glBufferSubData(GL_ARRAY_BUFFER, 0, MIN(visible_region_count * region_size, multimesh->instances * multimesh->stride_cache * sizeof(float)), data);
  1563. glBindBuffer(GL_ARRAY_BUFFER, 0);
  1564. } else {
  1565. // Not that many regions? update them all
  1566. // TODO: profile the performance cost on low end
  1567. glBindBuffer(GL_ARRAY_BUFFER, multimesh->buffer);
  1568. for (uint32_t i = 0; i < visible_region_count; i++) {
  1569. if (multimesh->data_cache_dirty_regions[i]) {
  1570. GLint offset = i * region_size;
  1571. GLint size = multimesh->stride_cache * (uint32_t)multimesh->instances * (uint32_t)sizeof(float);
  1572. uint32_t region_start_index = multimesh->stride_cache * MULTIMESH_DIRTY_REGION_SIZE * i;
  1573. glBufferSubData(GL_ARRAY_BUFFER, offset, MIN(region_size, size - offset), &data[region_start_index]);
  1574. }
  1575. }
  1576. glBindBuffer(GL_ARRAY_BUFFER, 0);
  1577. }
  1578. for (uint32_t i = 0; i < data_cache_dirty_region_count; i++) {
  1579. multimesh->data_cache_dirty_regions[i] = false;
  1580. }
  1581. multimesh->data_cache_used_dirty_regions = 0;
  1582. }
  1583. if (multimesh->aabb_dirty && multimesh->mesh.is_valid()) {
  1584. _multimesh_re_create_aabb(multimesh, data, visible_instances);
  1585. multimesh->aabb_dirty = false;
  1586. multimesh->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_AABB);
  1587. }
  1588. }
  1589. multimesh_dirty_list = multimesh->dirty_list;
  1590. multimesh->dirty_list = nullptr;
  1591. multimesh->dirty = false;
  1592. }
  1593. multimesh_dirty_list = nullptr;
  1594. }
  1595. /* SKELETON API */
  1596. RID MeshStorage::skeleton_allocate() {
  1597. return skeleton_owner.allocate_rid();
  1598. }
  1599. void MeshStorage::skeleton_initialize(RID p_rid) {
  1600. skeleton_owner.initialize_rid(p_rid, Skeleton());
  1601. }
  1602. void MeshStorage::skeleton_free(RID p_rid) {
  1603. _update_dirty_skeletons();
  1604. skeleton_allocate_data(p_rid, 0);
  1605. Skeleton *skeleton = skeleton_owner.get_or_null(p_rid);
  1606. skeleton->dependency.deleted_notify(p_rid);
  1607. skeleton_owner.free(p_rid);
  1608. }
  1609. void MeshStorage::_skeleton_make_dirty(Skeleton *skeleton) {
  1610. if (!skeleton->dirty) {
  1611. skeleton->dirty = true;
  1612. skeleton->dirty_list = skeleton_dirty_list;
  1613. skeleton_dirty_list = skeleton;
  1614. }
  1615. }
  1616. void MeshStorage::skeleton_allocate_data(RID p_skeleton, int p_bones, bool p_2d_skeleton) {
  1617. Skeleton *skeleton = skeleton_owner.get_or_null(p_skeleton);
  1618. ERR_FAIL_COND(!skeleton);
  1619. ERR_FAIL_COND(p_bones < 0);
  1620. if (skeleton->size == p_bones && skeleton->use_2d == p_2d_skeleton) {
  1621. return;
  1622. }
  1623. skeleton->size = p_bones;
  1624. skeleton->use_2d = p_2d_skeleton;
  1625. skeleton->height = (p_bones * (p_2d_skeleton ? 2 : 3)) / 256;
  1626. if ((p_bones * (p_2d_skeleton ? 2 : 3)) % 256) {
  1627. skeleton->height++;
  1628. }
  1629. if (skeleton->transforms_texture != 0) {
  1630. glDeleteTextures(1, &skeleton->transforms_texture);
  1631. skeleton->transforms_texture = 0;
  1632. skeleton->data.clear();
  1633. }
  1634. if (skeleton->size) {
  1635. skeleton->data.resize(256 * skeleton->height * 4);
  1636. glGenTextures(1, &skeleton->transforms_texture);
  1637. glBindTexture(GL_TEXTURE_2D, skeleton->transforms_texture);
  1638. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, 256, skeleton->height, 0, GL_RGBA, GL_FLOAT, nullptr);
  1639. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
  1640. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  1641. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
  1642. glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
  1643. glBindTexture(GL_TEXTURE_2D, 0);
  1644. memset(skeleton->data.ptrw(), 0, skeleton->data.size() * sizeof(float));
  1645. _skeleton_make_dirty(skeleton);
  1646. }
  1647. skeleton->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_SKELETON_DATA);
  1648. }
  1649. void MeshStorage::skeleton_set_base_transform_2d(RID p_skeleton, const Transform2D &p_base_transform) {
  1650. Skeleton *skeleton = skeleton_owner.get_or_null(p_skeleton);
  1651. ERR_FAIL_NULL(skeleton);
  1652. ERR_FAIL_COND(!skeleton->use_2d);
  1653. skeleton->base_transform_2d = p_base_transform;
  1654. }
  1655. int MeshStorage::skeleton_get_bone_count(RID p_skeleton) const {
  1656. Skeleton *skeleton = skeleton_owner.get_or_null(p_skeleton);
  1657. ERR_FAIL_COND_V(!skeleton, 0);
  1658. return skeleton->size;
  1659. }
  1660. void MeshStorage::skeleton_bone_set_transform(RID p_skeleton, int p_bone, const Transform3D &p_transform) {
  1661. Skeleton *skeleton = skeleton_owner.get_or_null(p_skeleton);
  1662. ERR_FAIL_COND(!skeleton);
  1663. ERR_FAIL_INDEX(p_bone, skeleton->size);
  1664. ERR_FAIL_COND(skeleton->use_2d);
  1665. float *dataptr = skeleton->data.ptrw() + p_bone * 12;
  1666. dataptr[0] = p_transform.basis.rows[0][0];
  1667. dataptr[1] = p_transform.basis.rows[0][1];
  1668. dataptr[2] = p_transform.basis.rows[0][2];
  1669. dataptr[3] = p_transform.origin.x;
  1670. dataptr[4] = p_transform.basis.rows[1][0];
  1671. dataptr[5] = p_transform.basis.rows[1][1];
  1672. dataptr[6] = p_transform.basis.rows[1][2];
  1673. dataptr[7] = p_transform.origin.y;
  1674. dataptr[8] = p_transform.basis.rows[2][0];
  1675. dataptr[9] = p_transform.basis.rows[2][1];
  1676. dataptr[10] = p_transform.basis.rows[2][2];
  1677. dataptr[11] = p_transform.origin.z;
  1678. _skeleton_make_dirty(skeleton);
  1679. }
  1680. Transform3D MeshStorage::skeleton_bone_get_transform(RID p_skeleton, int p_bone) const {
  1681. Skeleton *skeleton = skeleton_owner.get_or_null(p_skeleton);
  1682. ERR_FAIL_COND_V(!skeleton, Transform3D());
  1683. ERR_FAIL_INDEX_V(p_bone, skeleton->size, Transform3D());
  1684. ERR_FAIL_COND_V(skeleton->use_2d, Transform3D());
  1685. const float *dataptr = skeleton->data.ptr() + p_bone * 12;
  1686. Transform3D t;
  1687. t.basis.rows[0][0] = dataptr[0];
  1688. t.basis.rows[0][1] = dataptr[1];
  1689. t.basis.rows[0][2] = dataptr[2];
  1690. t.origin.x = dataptr[3];
  1691. t.basis.rows[1][0] = dataptr[4];
  1692. t.basis.rows[1][1] = dataptr[5];
  1693. t.basis.rows[1][2] = dataptr[6];
  1694. t.origin.y = dataptr[7];
  1695. t.basis.rows[2][0] = dataptr[8];
  1696. t.basis.rows[2][1] = dataptr[9];
  1697. t.basis.rows[2][2] = dataptr[10];
  1698. t.origin.z = dataptr[11];
  1699. return t;
  1700. }
  1701. void MeshStorage::skeleton_bone_set_transform_2d(RID p_skeleton, int p_bone, const Transform2D &p_transform) {
  1702. Skeleton *skeleton = skeleton_owner.get_or_null(p_skeleton);
  1703. ERR_FAIL_COND(!skeleton);
  1704. ERR_FAIL_INDEX(p_bone, skeleton->size);
  1705. ERR_FAIL_COND(!skeleton->use_2d);
  1706. float *dataptr = skeleton->data.ptrw() + p_bone * 8;
  1707. dataptr[0] = p_transform.columns[0][0];
  1708. dataptr[1] = p_transform.columns[1][0];
  1709. dataptr[2] = 0;
  1710. dataptr[3] = p_transform.columns[2][0];
  1711. dataptr[4] = p_transform.columns[0][1];
  1712. dataptr[5] = p_transform.columns[1][1];
  1713. dataptr[6] = 0;
  1714. dataptr[7] = p_transform.columns[2][1];
  1715. _skeleton_make_dirty(skeleton);
  1716. }
  1717. Transform2D MeshStorage::skeleton_bone_get_transform_2d(RID p_skeleton, int p_bone) const {
  1718. Skeleton *skeleton = skeleton_owner.get_or_null(p_skeleton);
  1719. ERR_FAIL_COND_V(!skeleton, Transform2D());
  1720. ERR_FAIL_INDEX_V(p_bone, skeleton->size, Transform2D());
  1721. ERR_FAIL_COND_V(!skeleton->use_2d, Transform2D());
  1722. const float *dataptr = skeleton->data.ptr() + p_bone * 8;
  1723. Transform2D t;
  1724. t.columns[0][0] = dataptr[0];
  1725. t.columns[1][0] = dataptr[1];
  1726. t.columns[2][0] = dataptr[3];
  1727. t.columns[0][1] = dataptr[4];
  1728. t.columns[1][1] = dataptr[5];
  1729. t.columns[2][1] = dataptr[7];
  1730. return t;
  1731. }
  1732. void MeshStorage::_update_dirty_skeletons() {
  1733. while (skeleton_dirty_list) {
  1734. Skeleton *skeleton = skeleton_dirty_list;
  1735. if (skeleton->size) {
  1736. glBindTexture(GL_TEXTURE_2D, skeleton->transforms_texture);
  1737. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, 256, skeleton->height, 0, GL_RGBA, GL_FLOAT, skeleton->data.ptr());
  1738. glBindTexture(GL_TEXTURE_2D, 0);
  1739. }
  1740. skeleton_dirty_list = skeleton->dirty_list;
  1741. skeleton->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_SKELETON_BONES);
  1742. skeleton->version++;
  1743. skeleton->dirty = false;
  1744. skeleton->dirty_list = nullptr;
  1745. }
  1746. skeleton_dirty_list = nullptr;
  1747. }
  1748. void MeshStorage::skeleton_update_dependency(RID p_skeleton, DependencyTracker *p_instance) {
  1749. Skeleton *skeleton = skeleton_owner.get_or_null(p_skeleton);
  1750. ERR_FAIL_COND(!skeleton);
  1751. p_instance->update_dependency(&skeleton->dependency);
  1752. }
  1753. #endif // GLES3_ENABLED