material_storage.cpp 82 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692
  1. /*************************************************************************/
  2. /* material_storage.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
  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. #include "material_storage.h"
  31. #include "core/config/engine.h"
  32. #include "core/config/project_settings.h"
  33. #include "core/io/resource_loader.h"
  34. #include "texture_storage.h"
  35. using namespace RendererRD;
  36. ///////////////////////////////////////////////////////////////////////////
  37. // UBI helper functions
  38. _FORCE_INLINE_ static void _fill_std140_variant_ubo_value(ShaderLanguage::DataType type, int p_array_size, const Variant &value, uint8_t *data, bool p_linear_color) {
  39. switch (type) {
  40. case ShaderLanguage::TYPE_BOOL: {
  41. uint32_t *gui = (uint32_t *)data;
  42. if (p_array_size > 0) {
  43. const PackedInt32Array &ba = value;
  44. int s = ba.size();
  45. const int *r = ba.ptr();
  46. for (int i = 0, j = 0; i < p_array_size; i++, j += 4) {
  47. if (i < s) {
  48. gui[j] = (r[i] != 0) ? 1 : 0;
  49. } else {
  50. gui[j] = 0;
  51. }
  52. gui[j + 1] = 0; // ignored
  53. gui[j + 2] = 0; // ignored
  54. gui[j + 3] = 0; // ignored
  55. }
  56. } else {
  57. bool v = value;
  58. gui[0] = v ? 1 : 0;
  59. }
  60. } break;
  61. case ShaderLanguage::TYPE_BVEC2: {
  62. uint32_t *gui = (uint32_t *)data;
  63. if (p_array_size > 0) {
  64. const PackedInt32Array &ba = value;
  65. int s = ba.size();
  66. const int *r = ba.ptr();
  67. int count = 2 * p_array_size;
  68. for (int i = 0, j = 0; i < count; i += 2, j += 4) {
  69. if (i < s) {
  70. gui[j] = r[i] ? 1 : 0;
  71. gui[j + 1] = r[i + 1] ? 1 : 0;
  72. } else {
  73. gui[j] = 0;
  74. gui[j + 1] = 0;
  75. }
  76. gui[j + 2] = 0; // ignored
  77. gui[j + 3] = 0; // ignored
  78. }
  79. } else {
  80. int v = value;
  81. gui[0] = v & 1 ? 1 : 0;
  82. gui[1] = v & 2 ? 1 : 0;
  83. }
  84. } break;
  85. case ShaderLanguage::TYPE_BVEC3: {
  86. uint32_t *gui = (uint32_t *)data;
  87. if (p_array_size > 0) {
  88. const PackedInt32Array &ba = value;
  89. int s = ba.size();
  90. const int *r = ba.ptr();
  91. int count = 3 * p_array_size;
  92. for (int i = 0, j = 0; i < count; i += 3, j += 4) {
  93. if (i < s) {
  94. gui[j] = r[i] ? 1 : 0;
  95. gui[j + 1] = r[i + 1] ? 1 : 0;
  96. gui[j + 2] = r[i + 2] ? 1 : 0;
  97. } else {
  98. gui[j] = 0;
  99. gui[j + 1] = 0;
  100. gui[j + 2] = 0;
  101. }
  102. gui[j + 3] = 0; // ignored
  103. }
  104. } else {
  105. int v = value;
  106. gui[0] = (v & 1) ? 1 : 0;
  107. gui[1] = (v & 2) ? 1 : 0;
  108. gui[2] = (v & 4) ? 1 : 0;
  109. }
  110. } break;
  111. case ShaderLanguage::TYPE_BVEC4: {
  112. uint32_t *gui = (uint32_t *)data;
  113. if (p_array_size > 0) {
  114. const PackedInt32Array &ba = value;
  115. int s = ba.size();
  116. const int *r = ba.ptr();
  117. int count = 4 * p_array_size;
  118. for (int i = 0; i < count; i += 4) {
  119. if (i < s) {
  120. gui[i] = r[i] ? 1 : 0;
  121. gui[i + 1] = r[i + 1] ? 1 : 0;
  122. gui[i + 2] = r[i + 2] ? 1 : 0;
  123. gui[i + 3] = r[i + 3] ? 1 : 0;
  124. } else {
  125. gui[i] = 0;
  126. gui[i + 1] = 0;
  127. gui[i + 2] = 0;
  128. gui[i + 3] = 0;
  129. }
  130. }
  131. } else {
  132. int v = value;
  133. gui[0] = (v & 1) ? 1 : 0;
  134. gui[1] = (v & 2) ? 1 : 0;
  135. gui[2] = (v & 4) ? 1 : 0;
  136. gui[3] = (v & 8) ? 1 : 0;
  137. }
  138. } break;
  139. case ShaderLanguage::TYPE_INT: {
  140. int32_t *gui = (int32_t *)data;
  141. if (p_array_size > 0) {
  142. Vector<int> iv = value;
  143. int s = iv.size();
  144. const int *r = iv.ptr();
  145. for (int i = 0, j = 0; i < p_array_size; i++, j += 4) {
  146. if (i < s) {
  147. gui[j] = r[i];
  148. } else {
  149. gui[j] = 0;
  150. }
  151. gui[j + 1] = 0; // ignored
  152. gui[j + 2] = 0; // ignored
  153. gui[j + 3] = 0; // ignored
  154. }
  155. } else {
  156. int v = value;
  157. gui[0] = v;
  158. }
  159. } break;
  160. case ShaderLanguage::TYPE_IVEC2: {
  161. int32_t *gui = (int32_t *)data;
  162. if (p_array_size > 0) {
  163. Vector<int> iv = value;
  164. int s = iv.size();
  165. int count = 2 * p_array_size;
  166. const int *r = iv.ptr();
  167. for (int i = 0, j = 0; i < count; i += 2, j += 4) {
  168. if (i < s) {
  169. gui[j] = r[i];
  170. gui[j + 1] = r[i + 1];
  171. } else {
  172. gui[j] = 0;
  173. gui[j + 1] = 0;
  174. }
  175. gui[j + 2] = 0; // ignored
  176. gui[j + 3] = 0; // ignored
  177. }
  178. } else {
  179. Vector2i v = value;
  180. gui[0] = v.x;
  181. gui[1] = v.y;
  182. }
  183. } break;
  184. case ShaderLanguage::TYPE_IVEC3: {
  185. int32_t *gui = (int32_t *)data;
  186. if (p_array_size > 0) {
  187. Vector<int> iv = value;
  188. int s = iv.size();
  189. int count = 3 * p_array_size;
  190. const int *r = iv.ptr();
  191. for (int i = 0, j = 0; i < count; i += 3, j += 4) {
  192. if (i < s) {
  193. gui[j] = r[i];
  194. gui[j + 1] = r[i + 1];
  195. gui[j + 2] = r[i + 2];
  196. } else {
  197. gui[j] = 0;
  198. gui[j + 1] = 0;
  199. gui[j + 2] = 0;
  200. }
  201. gui[j + 3] = 0; // ignored
  202. }
  203. } else {
  204. Vector3i v = value;
  205. gui[0] = v.x;
  206. gui[1] = v.y;
  207. gui[2] = v.z;
  208. }
  209. } break;
  210. case ShaderLanguage::TYPE_IVEC4: {
  211. int32_t *gui = (int32_t *)data;
  212. if (p_array_size > 0) {
  213. Vector<int> iv = value;
  214. int s = iv.size();
  215. int count = 4 * p_array_size;
  216. const int *r = iv.ptr();
  217. for (int i = 0; i < count; i += 4) {
  218. if (i < s) {
  219. gui[i] = r[i];
  220. gui[i + 1] = r[i + 1];
  221. gui[i + 2] = r[i + 2];
  222. gui[i + 3] = r[i + 3];
  223. } else {
  224. gui[i] = 0;
  225. gui[i + 1] = 0;
  226. gui[i + 2] = 0;
  227. gui[i + 3] = 0;
  228. }
  229. }
  230. } else {
  231. Vector4i v = value;
  232. gui[0] = v.x;
  233. gui[1] = v.y;
  234. gui[2] = v.z;
  235. gui[3] = v.w;
  236. }
  237. } break;
  238. case ShaderLanguage::TYPE_UINT: {
  239. uint32_t *gui = (uint32_t *)data;
  240. if (p_array_size > 0) {
  241. Vector<int> iv = value;
  242. int s = iv.size();
  243. const int *r = iv.ptr();
  244. for (int i = 0, j = 0; i < p_array_size; i++, j += 4) {
  245. if (i < s) {
  246. gui[j] = r[i];
  247. } else {
  248. gui[j] = 0;
  249. }
  250. gui[j + 1] = 0; // ignored
  251. gui[j + 2] = 0; // ignored
  252. gui[j + 3] = 0; // ignored
  253. }
  254. } else {
  255. int v = value;
  256. gui[0] = v;
  257. }
  258. } break;
  259. case ShaderLanguage::TYPE_UVEC2: {
  260. uint32_t *gui = (uint32_t *)data;
  261. if (p_array_size > 0) {
  262. Vector<int> iv = value;
  263. int s = iv.size();
  264. int count = 2 * p_array_size;
  265. const int *r = iv.ptr();
  266. for (int i = 0, j = 0; i < count; i += 2, j += 4) {
  267. if (i < s) {
  268. gui[j] = r[i];
  269. gui[j + 1] = r[i + 1];
  270. } else {
  271. gui[j] = 0;
  272. gui[j + 1] = 0;
  273. }
  274. gui[j + 2] = 0; // ignored
  275. gui[j + 3] = 0; // ignored
  276. }
  277. } else {
  278. Vector2i v = value;
  279. gui[0] = v.x;
  280. gui[1] = v.y;
  281. }
  282. } break;
  283. case ShaderLanguage::TYPE_UVEC3: {
  284. uint32_t *gui = (uint32_t *)data;
  285. if (p_array_size > 0) {
  286. Vector<int> iv = value;
  287. int s = iv.size();
  288. int count = 3 * p_array_size;
  289. const int *r = iv.ptr();
  290. for (int i = 0, j = 0; i < count; i += 3, j += 4) {
  291. if (i < s) {
  292. gui[j] = r[i];
  293. gui[j + 1] = r[i + 1];
  294. gui[j + 2] = r[i + 2];
  295. } else {
  296. gui[j] = 0;
  297. gui[j + 1] = 0;
  298. gui[j + 2] = 0;
  299. }
  300. gui[j + 3] = 0; // ignored
  301. }
  302. } else {
  303. Vector3i v = value;
  304. gui[0] = v.x;
  305. gui[1] = v.y;
  306. gui[2] = v.z;
  307. }
  308. } break;
  309. case ShaderLanguage::TYPE_UVEC4: {
  310. uint32_t *gui = (uint32_t *)data;
  311. if (p_array_size > 0) {
  312. Vector<int> iv = value;
  313. int s = iv.size();
  314. int count = 4 * p_array_size;
  315. const int *r = iv.ptr();
  316. for (int i = 0; i < count; i++) {
  317. if (i < s) {
  318. gui[i] = r[i];
  319. gui[i + 1] = r[i + 1];
  320. gui[i + 2] = r[i + 2];
  321. gui[i + 3] = r[i + 3];
  322. } else {
  323. gui[i] = 0;
  324. gui[i + 1] = 0;
  325. gui[i + 2] = 0;
  326. gui[i + 3] = 0;
  327. }
  328. }
  329. } else {
  330. Vector4i v = value;
  331. gui[0] = v.x;
  332. gui[1] = v.y;
  333. gui[2] = v.z;
  334. gui[3] = v.w;
  335. }
  336. } break;
  337. case ShaderLanguage::TYPE_FLOAT: {
  338. float *gui = reinterpret_cast<float *>(data);
  339. if (p_array_size > 0) {
  340. const PackedFloat32Array &a = value;
  341. int s = a.size();
  342. for (int i = 0, j = 0; i < p_array_size; i++, j += 4) {
  343. if (i < s) {
  344. gui[j] = a[i];
  345. } else {
  346. gui[j] = 0;
  347. }
  348. gui[j + 1] = 0; // ignored
  349. gui[j + 2] = 0; // ignored
  350. gui[j + 3] = 0; // ignored
  351. }
  352. } else {
  353. float v = value;
  354. gui[0] = v;
  355. }
  356. } break;
  357. case ShaderLanguage::TYPE_VEC2: {
  358. float *gui = reinterpret_cast<float *>(data);
  359. if (p_array_size > 0) {
  360. const PackedVector2Array &a = value;
  361. int s = a.size();
  362. for (int i = 0, j = 0; i < p_array_size; i++, j += 4) {
  363. if (i < s) {
  364. gui[j] = a[i].x;
  365. gui[j + 1] = a[i].y;
  366. } else {
  367. gui[j] = 0;
  368. gui[j + 1] = 0;
  369. }
  370. gui[j + 2] = 0; // ignored
  371. gui[j + 3] = 0; // ignored
  372. }
  373. } else {
  374. Vector2 v = value;
  375. gui[0] = v.x;
  376. gui[1] = v.y;
  377. }
  378. } break;
  379. case ShaderLanguage::TYPE_VEC3: {
  380. float *gui = reinterpret_cast<float *>(data);
  381. if (p_array_size > 0) {
  382. if (value.get_type() == Variant::PACKED_COLOR_ARRAY) {
  383. const PackedColorArray &a = value;
  384. int s = a.size();
  385. for (int i = 0, j = 0; i < p_array_size; i++, j += 4) {
  386. if (i < s) {
  387. Color color = a[i];
  388. if (p_linear_color) {
  389. color = color.srgb_to_linear();
  390. }
  391. gui[j] = color.r;
  392. gui[j + 1] = color.g;
  393. gui[j + 2] = color.b;
  394. } else {
  395. gui[j] = 0;
  396. gui[j + 1] = 0;
  397. gui[j + 2] = 0;
  398. }
  399. gui[j + 3] = 0; // ignored
  400. }
  401. } else {
  402. const PackedVector3Array &a = value;
  403. int s = a.size();
  404. for (int i = 0, j = 0; i < p_array_size; i++, j += 4) {
  405. if (i < s) {
  406. gui[j] = a[i].x;
  407. gui[j + 1] = a[i].y;
  408. gui[j + 2] = a[i].z;
  409. } else {
  410. gui[j] = 0;
  411. gui[j + 1] = 0;
  412. gui[j + 2] = 0;
  413. }
  414. gui[j + 3] = 0; // ignored
  415. }
  416. }
  417. } else {
  418. if (value.get_type() == Variant::COLOR) {
  419. Color v = value;
  420. if (p_linear_color) {
  421. v = v.srgb_to_linear();
  422. }
  423. gui[0] = v.r;
  424. gui[1] = v.g;
  425. gui[2] = v.b;
  426. } else {
  427. Vector3 v = value;
  428. gui[0] = v.x;
  429. gui[1] = v.y;
  430. gui[2] = v.z;
  431. }
  432. }
  433. } break;
  434. case ShaderLanguage::TYPE_VEC4: {
  435. float *gui = reinterpret_cast<float *>(data);
  436. if (p_array_size > 0) {
  437. if (value.get_type() == Variant::PACKED_COLOR_ARRAY) {
  438. const PackedColorArray &a = value;
  439. int s = a.size();
  440. for (int i = 0, j = 0; i < p_array_size; i++, j += 4) {
  441. if (i < s) {
  442. Color color = a[i];
  443. if (p_linear_color) {
  444. color = color.srgb_to_linear();
  445. }
  446. gui[j] = color.r;
  447. gui[j + 1] = color.g;
  448. gui[j + 2] = color.b;
  449. gui[j + 3] = color.a;
  450. } else {
  451. gui[j] = 0;
  452. gui[j + 1] = 0;
  453. gui[j + 2] = 0;
  454. gui[j + 3] = 0;
  455. }
  456. }
  457. } else {
  458. const PackedFloat32Array &a = value;
  459. int s = a.size();
  460. int count = 4 * p_array_size;
  461. for (int i = 0; i < count; i += 4) {
  462. if (i + 3 < s) {
  463. gui[i] = a[i];
  464. gui[i + 1] = a[i + 1];
  465. gui[i + 2] = a[i + 2];
  466. gui[i + 3] = a[i + 3];
  467. } else {
  468. gui[i] = 0;
  469. gui[i + 1] = 0;
  470. gui[i + 2] = 0;
  471. gui[i + 3] = 0;
  472. }
  473. }
  474. }
  475. } else {
  476. if (value.get_type() == Variant::COLOR) {
  477. Color v = value;
  478. if (p_linear_color) {
  479. v = v.srgb_to_linear();
  480. }
  481. gui[0] = v.r;
  482. gui[1] = v.g;
  483. gui[2] = v.b;
  484. gui[3] = v.a;
  485. } else if (value.get_type() == Variant::RECT2) {
  486. Rect2 v = value;
  487. gui[0] = v.position.x;
  488. gui[1] = v.position.y;
  489. gui[2] = v.size.x;
  490. gui[3] = v.size.y;
  491. } else if (value.get_type() == Variant::QUATERNION) {
  492. Quaternion v = value;
  493. gui[0] = v.x;
  494. gui[1] = v.y;
  495. gui[2] = v.z;
  496. gui[3] = v.w;
  497. } else if (value.get_type() == Variant::PLANE) {
  498. Plane v = value;
  499. gui[0] = v.normal.x;
  500. gui[1] = v.normal.y;
  501. gui[2] = v.normal.z;
  502. gui[3] = v.d;
  503. } else {
  504. Vector4 v = value;
  505. gui[0] = v.x;
  506. gui[1] = v.y;
  507. gui[2] = v.z;
  508. gui[3] = v.w;
  509. }
  510. }
  511. } break;
  512. case ShaderLanguage::TYPE_MAT2: {
  513. float *gui = reinterpret_cast<float *>(data);
  514. if (p_array_size > 0) {
  515. const PackedFloat32Array &a = value;
  516. int s = a.size();
  517. for (int i = 0, j = 0; i < p_array_size * 4; i += 4, j += 8) {
  518. if (i + 3 < s) {
  519. gui[j] = a[i];
  520. gui[j + 1] = a[i + 1];
  521. gui[j + 4] = a[i + 2];
  522. gui[j + 5] = a[i + 3];
  523. } else {
  524. gui[j] = 1;
  525. gui[j + 1] = 0;
  526. gui[j + 4] = 0;
  527. gui[j + 5] = 1;
  528. }
  529. gui[j + 2] = 0; // ignored
  530. gui[j + 3] = 0; // ignored
  531. gui[j + 6] = 0; // ignored
  532. gui[j + 7] = 0; // ignored
  533. }
  534. } else {
  535. Transform2D v = value;
  536. //in std140 members of mat2 are treated as vec4s
  537. gui[0] = v.columns[0][0];
  538. gui[1] = v.columns[0][1];
  539. gui[2] = 0; // ignored
  540. gui[3] = 0; // ignored
  541. gui[4] = v.columns[1][0];
  542. gui[5] = v.columns[1][1];
  543. gui[6] = 0; // ignored
  544. gui[7] = 0; // ignored
  545. }
  546. } break;
  547. case ShaderLanguage::TYPE_MAT3: {
  548. float *gui = reinterpret_cast<float *>(data);
  549. if (p_array_size > 0) {
  550. const PackedFloat32Array &a = value;
  551. int s = a.size();
  552. for (int i = 0, j = 0; i < p_array_size * 9; i += 9, j += 12) {
  553. if (i + 8 < s) {
  554. gui[j] = a[i];
  555. gui[j + 1] = a[i + 1];
  556. gui[j + 2] = a[i + 2];
  557. gui[j + 4] = a[i + 3];
  558. gui[j + 5] = a[i + 4];
  559. gui[j + 6] = a[i + 5];
  560. gui[j + 8] = a[i + 6];
  561. gui[j + 9] = a[i + 7];
  562. gui[j + 10] = a[i + 8];
  563. } else {
  564. gui[j] = 1;
  565. gui[j + 1] = 0;
  566. gui[j + 2] = 0;
  567. gui[j + 4] = 0;
  568. gui[j + 5] = 1;
  569. gui[j + 6] = 0;
  570. gui[j + 8] = 0;
  571. gui[j + 9] = 0;
  572. gui[j + 10] = 1;
  573. }
  574. gui[j + 3] = 0; // ignored
  575. gui[j + 7] = 0; // ignored
  576. gui[j + 11] = 0; // ignored
  577. }
  578. } else {
  579. Basis v = value;
  580. gui[0] = v.rows[0][0];
  581. gui[1] = v.rows[1][0];
  582. gui[2] = v.rows[2][0];
  583. gui[3] = 0; // ignored
  584. gui[4] = v.rows[0][1];
  585. gui[5] = v.rows[1][1];
  586. gui[6] = v.rows[2][1];
  587. gui[7] = 0; // ignored
  588. gui[8] = v.rows[0][2];
  589. gui[9] = v.rows[1][2];
  590. gui[10] = v.rows[2][2];
  591. gui[11] = 0; // ignored
  592. }
  593. } break;
  594. case ShaderLanguage::TYPE_MAT4: {
  595. float *gui = reinterpret_cast<float *>(data);
  596. if (p_array_size > 0) {
  597. const PackedFloat32Array &a = value;
  598. int s = a.size();
  599. for (int i = 0; i < p_array_size * 16; i += 16) {
  600. if (i + 15 < s) {
  601. gui[i] = a[i];
  602. gui[i + 1] = a[i + 1];
  603. gui[i + 2] = a[i + 2];
  604. gui[i + 3] = a[i + 3];
  605. gui[i + 4] = a[i + 4];
  606. gui[i + 5] = a[i + 5];
  607. gui[i + 6] = a[i + 6];
  608. gui[i + 7] = a[i + 7];
  609. gui[i + 8] = a[i + 8];
  610. gui[i + 9] = a[i + 9];
  611. gui[i + 10] = a[i + 10];
  612. gui[i + 11] = a[i + 11];
  613. gui[i + 12] = a[i + 12];
  614. gui[i + 13] = a[i + 13];
  615. gui[i + 14] = a[i + 14];
  616. gui[i + 15] = a[i + 15];
  617. } else {
  618. gui[i] = 1;
  619. gui[i + 1] = 0;
  620. gui[i + 2] = 0;
  621. gui[i + 3] = 0;
  622. gui[i + 4] = 0;
  623. gui[i + 5] = 1;
  624. gui[i + 6] = 0;
  625. gui[i + 7] = 0;
  626. gui[i + 8] = 0;
  627. gui[i + 9] = 0;
  628. gui[i + 10] = 1;
  629. gui[i + 11] = 0;
  630. gui[i + 12] = 0;
  631. gui[i + 13] = 0;
  632. gui[i + 14] = 0;
  633. gui[i + 15] = 1;
  634. }
  635. }
  636. } else if (value.get_type() == Variant::TRANSFORM3D) {
  637. Transform3D v = value;
  638. gui[0] = v.basis.rows[0][0];
  639. gui[1] = v.basis.rows[1][0];
  640. gui[2] = v.basis.rows[2][0];
  641. gui[3] = 0;
  642. gui[4] = v.basis.rows[0][1];
  643. gui[5] = v.basis.rows[1][1];
  644. gui[6] = v.basis.rows[2][1];
  645. gui[7] = 0;
  646. gui[8] = v.basis.rows[0][2];
  647. gui[9] = v.basis.rows[1][2];
  648. gui[10] = v.basis.rows[2][2];
  649. gui[11] = 0;
  650. gui[12] = v.origin.x;
  651. gui[13] = v.origin.y;
  652. gui[14] = v.origin.z;
  653. gui[15] = 1;
  654. } else {
  655. Projection v = value;
  656. for (int i = 0; i < 4; i++) {
  657. for (int j = 0; j < 4; j++) {
  658. gui[i * 4 + j] = v.matrix[i][j];
  659. }
  660. }
  661. }
  662. } break;
  663. default: {
  664. }
  665. }
  666. }
  667. _FORCE_INLINE_ static void _fill_std140_ubo_value(ShaderLanguage::DataType type, const Vector<ShaderLanguage::ConstantNode::Value> &value, uint8_t *data) {
  668. switch (type) {
  669. case ShaderLanguage::TYPE_BOOL: {
  670. uint32_t *gui = (uint32_t *)data;
  671. *gui = value[0].boolean ? 1 : 0;
  672. } break;
  673. case ShaderLanguage::TYPE_BVEC2: {
  674. uint32_t *gui = (uint32_t *)data;
  675. gui[0] = value[0].boolean ? 1 : 0;
  676. gui[1] = value[1].boolean ? 1 : 0;
  677. } break;
  678. case ShaderLanguage::TYPE_BVEC3: {
  679. uint32_t *gui = (uint32_t *)data;
  680. gui[0] = value[0].boolean ? 1 : 0;
  681. gui[1] = value[1].boolean ? 1 : 0;
  682. gui[2] = value[2].boolean ? 1 : 0;
  683. } break;
  684. case ShaderLanguage::TYPE_BVEC4: {
  685. uint32_t *gui = (uint32_t *)data;
  686. gui[0] = value[0].boolean ? 1 : 0;
  687. gui[1] = value[1].boolean ? 1 : 0;
  688. gui[2] = value[2].boolean ? 1 : 0;
  689. gui[3] = value[3].boolean ? 1 : 0;
  690. } break;
  691. case ShaderLanguage::TYPE_INT: {
  692. int32_t *gui = (int32_t *)data;
  693. gui[0] = value[0].sint;
  694. } break;
  695. case ShaderLanguage::TYPE_IVEC2: {
  696. int32_t *gui = (int32_t *)data;
  697. for (int i = 0; i < 2; i++) {
  698. gui[i] = value[i].sint;
  699. }
  700. } break;
  701. case ShaderLanguage::TYPE_IVEC3: {
  702. int32_t *gui = (int32_t *)data;
  703. for (int i = 0; i < 3; i++) {
  704. gui[i] = value[i].sint;
  705. }
  706. } break;
  707. case ShaderLanguage::TYPE_IVEC4: {
  708. int32_t *gui = (int32_t *)data;
  709. for (int i = 0; i < 4; i++) {
  710. gui[i] = value[i].sint;
  711. }
  712. } break;
  713. case ShaderLanguage::TYPE_UINT: {
  714. uint32_t *gui = (uint32_t *)data;
  715. gui[0] = value[0].uint;
  716. } break;
  717. case ShaderLanguage::TYPE_UVEC2: {
  718. int32_t *gui = (int32_t *)data;
  719. for (int i = 0; i < 2; i++) {
  720. gui[i] = value[i].uint;
  721. }
  722. } break;
  723. case ShaderLanguage::TYPE_UVEC3: {
  724. int32_t *gui = (int32_t *)data;
  725. for (int i = 0; i < 3; i++) {
  726. gui[i] = value[i].uint;
  727. }
  728. } break;
  729. case ShaderLanguage::TYPE_UVEC4: {
  730. int32_t *gui = (int32_t *)data;
  731. for (int i = 0; i < 4; i++) {
  732. gui[i] = value[i].uint;
  733. }
  734. } break;
  735. case ShaderLanguage::TYPE_FLOAT: {
  736. float *gui = reinterpret_cast<float *>(data);
  737. gui[0] = value[0].real;
  738. } break;
  739. case ShaderLanguage::TYPE_VEC2: {
  740. float *gui = reinterpret_cast<float *>(data);
  741. for (int i = 0; i < 2; i++) {
  742. gui[i] = value[i].real;
  743. }
  744. } break;
  745. case ShaderLanguage::TYPE_VEC3: {
  746. float *gui = reinterpret_cast<float *>(data);
  747. for (int i = 0; i < 3; i++) {
  748. gui[i] = value[i].real;
  749. }
  750. } break;
  751. case ShaderLanguage::TYPE_VEC4: {
  752. float *gui = reinterpret_cast<float *>(data);
  753. for (int i = 0; i < 4; i++) {
  754. gui[i] = value[i].real;
  755. }
  756. } break;
  757. case ShaderLanguage::TYPE_MAT2: {
  758. float *gui = reinterpret_cast<float *>(data);
  759. //in std140 members of mat2 are treated as vec4s
  760. gui[0] = value[0].real;
  761. gui[1] = value[1].real;
  762. gui[2] = 0;
  763. gui[3] = 0;
  764. gui[4] = value[2].real;
  765. gui[5] = value[3].real;
  766. gui[6] = 0;
  767. gui[7] = 0;
  768. } break;
  769. case ShaderLanguage::TYPE_MAT3: {
  770. float *gui = reinterpret_cast<float *>(data);
  771. gui[0] = value[0].real;
  772. gui[1] = value[1].real;
  773. gui[2] = value[2].real;
  774. gui[3] = 0;
  775. gui[4] = value[3].real;
  776. gui[5] = value[4].real;
  777. gui[6] = value[5].real;
  778. gui[7] = 0;
  779. gui[8] = value[6].real;
  780. gui[9] = value[7].real;
  781. gui[10] = value[8].real;
  782. gui[11] = 0;
  783. } break;
  784. case ShaderLanguage::TYPE_MAT4: {
  785. float *gui = reinterpret_cast<float *>(data);
  786. for (int i = 0; i < 16; i++) {
  787. gui[i] = value[i].real;
  788. }
  789. } break;
  790. default: {
  791. }
  792. }
  793. }
  794. _FORCE_INLINE_ static void _fill_std140_ubo_empty(ShaderLanguage::DataType type, int p_array_size, uint8_t *data) {
  795. if (p_array_size <= 0) {
  796. p_array_size = 1;
  797. }
  798. switch (type) {
  799. case ShaderLanguage::TYPE_BOOL:
  800. case ShaderLanguage::TYPE_INT:
  801. case ShaderLanguage::TYPE_UINT:
  802. case ShaderLanguage::TYPE_FLOAT: {
  803. memset(data, 0, 4 * p_array_size);
  804. } break;
  805. case ShaderLanguage::TYPE_BVEC2:
  806. case ShaderLanguage::TYPE_IVEC2:
  807. case ShaderLanguage::TYPE_UVEC2:
  808. case ShaderLanguage::TYPE_VEC2: {
  809. memset(data, 0, 8 * p_array_size);
  810. } break;
  811. case ShaderLanguage::TYPE_BVEC3:
  812. case ShaderLanguage::TYPE_IVEC3:
  813. case ShaderLanguage::TYPE_UVEC3:
  814. case ShaderLanguage::TYPE_VEC3:
  815. case ShaderLanguage::TYPE_BVEC4:
  816. case ShaderLanguage::TYPE_IVEC4:
  817. case ShaderLanguage::TYPE_UVEC4:
  818. case ShaderLanguage::TYPE_VEC4: {
  819. memset(data, 0, 16 * p_array_size);
  820. } break;
  821. case ShaderLanguage::TYPE_MAT2: {
  822. memset(data, 0, 32 * p_array_size);
  823. } break;
  824. case ShaderLanguage::TYPE_MAT3: {
  825. memset(data, 0, 48 * p_array_size);
  826. } break;
  827. case ShaderLanguage::TYPE_MAT4: {
  828. memset(data, 0, 64 * p_array_size);
  829. } break;
  830. default: {
  831. }
  832. }
  833. }
  834. ///////////////////////////////////////////////////////////////////////////
  835. // MaterialData
  836. void MaterialData::update_uniform_buffer(const HashMap<StringName, ShaderLanguage::ShaderNode::Uniform> &p_uniforms, const uint32_t *p_uniform_offsets, const HashMap<StringName, Variant> &p_parameters, uint8_t *p_buffer, uint32_t p_buffer_size, bool p_use_linear_color) {
  837. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  838. bool uses_global_buffer = false;
  839. for (const KeyValue<StringName, ShaderLanguage::ShaderNode::Uniform> &E : p_uniforms) {
  840. if (E.value.order < 0) {
  841. continue; // texture, does not go here
  842. }
  843. if (E.value.scope == ShaderLanguage::ShaderNode::Uniform::SCOPE_INSTANCE) {
  844. continue; //instance uniforms don't appear in the buffer
  845. }
  846. if (E.value.scope == ShaderLanguage::ShaderNode::Uniform::SCOPE_GLOBAL) {
  847. //this is a global variable, get the index to it
  848. GlobalShaderUniforms::Variable *gv = material_storage->global_shader_uniforms.variables.getptr(E.key);
  849. uint32_t index = 0;
  850. if (gv) {
  851. index = gv->buffer_index;
  852. } else {
  853. WARN_PRINT("Shader uses global uniform '" + E.key + "', but it was removed at some point. Material will not display correctly.");
  854. }
  855. uint32_t offset = p_uniform_offsets[E.value.order];
  856. uint32_t *intptr = (uint32_t *)&p_buffer[offset];
  857. *intptr = index;
  858. uses_global_buffer = true;
  859. continue;
  860. }
  861. //regular uniform
  862. uint32_t offset = p_uniform_offsets[E.value.order];
  863. #ifdef DEBUG_ENABLED
  864. uint32_t size = 0U;
  865. // The following code enforces a 16-byte alignment of uniform arrays.
  866. if (E.value.array_size > 0) {
  867. size = ShaderLanguage::get_datatype_size(E.value.type) * E.value.array_size;
  868. int m = (16 * E.value.array_size);
  869. if ((size % m) != 0U) {
  870. size += m - (size % m);
  871. }
  872. } else {
  873. size = ShaderLanguage::get_datatype_size(E.value.type);
  874. }
  875. ERR_CONTINUE(offset + size > p_buffer_size);
  876. #endif
  877. uint8_t *data = &p_buffer[offset];
  878. HashMap<StringName, Variant>::ConstIterator V = p_parameters.find(E.key);
  879. if (V) {
  880. //user provided
  881. _fill_std140_variant_ubo_value(E.value.type, E.value.array_size, V->value, data, p_use_linear_color);
  882. } else if (E.value.default_value.size()) {
  883. //default value
  884. _fill_std140_ubo_value(E.value.type, E.value.default_value, data);
  885. //value=E.value.default_value;
  886. } else {
  887. //zero because it was not provided
  888. if ((E.value.type == ShaderLanguage::TYPE_VEC3 || E.value.type == ShaderLanguage::TYPE_VEC4) && E.value.hint == ShaderLanguage::ShaderNode::Uniform::HINT_SOURCE_COLOR) {
  889. //colors must be set as black, with alpha as 1.0
  890. _fill_std140_variant_ubo_value(E.value.type, E.value.array_size, Color(0, 0, 0, 1), data, p_use_linear_color);
  891. } else {
  892. //else just zero it out
  893. _fill_std140_ubo_empty(E.value.type, E.value.array_size, data);
  894. }
  895. }
  896. }
  897. if (uses_global_buffer != (global_buffer_E != nullptr)) {
  898. if (uses_global_buffer) {
  899. global_buffer_E = material_storage->global_shader_uniforms.materials_using_buffer.push_back(self);
  900. } else {
  901. material_storage->global_shader_uniforms.materials_using_buffer.erase(global_buffer_E);
  902. global_buffer_E = nullptr;
  903. }
  904. }
  905. }
  906. MaterialData::~MaterialData() {
  907. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  908. if (global_buffer_E) {
  909. //unregister global buffers
  910. material_storage->global_shader_uniforms.materials_using_buffer.erase(global_buffer_E);
  911. }
  912. if (global_texture_E) {
  913. //unregister global textures
  914. for (const KeyValue<StringName, uint64_t> &E : used_global_textures) {
  915. GlobalShaderUniforms::Variable *v = material_storage->global_shader_uniforms.variables.getptr(E.key);
  916. if (v) {
  917. v->texture_materials.erase(self);
  918. }
  919. }
  920. //unregister material from those using global textures
  921. material_storage->global_shader_uniforms.materials_using_texture.erase(global_texture_E);
  922. }
  923. if (uniform_buffer.is_valid()) {
  924. RD::get_singleton()->free(uniform_buffer);
  925. }
  926. }
  927. void MaterialData::update_textures(const HashMap<StringName, Variant> &p_parameters, const HashMap<StringName, HashMap<int, RID>> &p_default_textures, const Vector<ShaderCompiler::GeneratedCode::Texture> &p_texture_uniforms, RID *p_textures, bool p_use_linear_color) {
  928. TextureStorage *texture_storage = TextureStorage::get_singleton();
  929. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  930. #ifdef TOOLS_ENABLED
  931. Texture *roughness_detect_texture = nullptr;
  932. RS::TextureDetectRoughnessChannel roughness_channel = RS::TEXTURE_DETECT_ROUGHNESS_R;
  933. Texture *normal_detect_texture = nullptr;
  934. #endif
  935. bool uses_global_textures = false;
  936. global_textures_pass++;
  937. for (int i = 0, k = 0; i < p_texture_uniforms.size(); i++) {
  938. const StringName &uniform_name = p_texture_uniforms[i].name;
  939. int uniform_array_size = p_texture_uniforms[i].array_size;
  940. Vector<RID> textures;
  941. if (p_texture_uniforms[i].global) {
  942. uses_global_textures = true;
  943. GlobalShaderUniforms::Variable *v = material_storage->global_shader_uniforms.variables.getptr(uniform_name);
  944. if (v) {
  945. if (v->buffer_index >= 0) {
  946. WARN_PRINT("Shader uses global uniform texture '" + String(uniform_name) + "', but it changed type and is no longer a texture!.");
  947. } else {
  948. HashMap<StringName, uint64_t>::Iterator E = used_global_textures.find(uniform_name);
  949. if (!E) {
  950. E = used_global_textures.insert(uniform_name, global_textures_pass);
  951. v->texture_materials.insert(self);
  952. } else {
  953. E->value = global_textures_pass;
  954. }
  955. textures.push_back(v->override.get_type() != Variant::NIL ? v->override : v->value);
  956. }
  957. } else {
  958. WARN_PRINT("Shader uses global uniform texture '" + String(uniform_name) + "', but it was removed at some point. Material will not display correctly.");
  959. }
  960. } else {
  961. HashMap<StringName, Variant>::ConstIterator V = p_parameters.find(uniform_name);
  962. if (V) {
  963. if (V->value.is_array()) {
  964. Array array = (Array)V->value;
  965. if (uniform_array_size > 0) {
  966. for (int j = 0; j < array.size(); j++) {
  967. textures.push_back(array[j]);
  968. }
  969. } else {
  970. if (array.size() > 0) {
  971. textures.push_back(array[0]);
  972. }
  973. }
  974. } else {
  975. textures.push_back(V->value);
  976. }
  977. }
  978. if (uniform_array_size > 0) {
  979. if (textures.size() < uniform_array_size) {
  980. HashMap<StringName, HashMap<int, RID>>::ConstIterator W = p_default_textures.find(uniform_name);
  981. for (int j = textures.size(); j < uniform_array_size; j++) {
  982. if (W && W->value.has(j)) {
  983. textures.push_back(W->value[j]);
  984. } else {
  985. textures.push_back(RID());
  986. }
  987. }
  988. }
  989. } else if (textures.is_empty()) {
  990. HashMap<StringName, HashMap<int, RID>>::ConstIterator W = p_default_textures.find(uniform_name);
  991. if (W && W->value.has(0)) {
  992. textures.push_back(W->value[0]);
  993. }
  994. }
  995. }
  996. RID rd_texture;
  997. if (textures.is_empty()) {
  998. //check default usage
  999. switch (p_texture_uniforms[i].type) {
  1000. case ShaderLanguage::TYPE_ISAMPLER2D:
  1001. case ShaderLanguage::TYPE_USAMPLER2D:
  1002. case ShaderLanguage::TYPE_SAMPLER2D: {
  1003. switch (p_texture_uniforms[i].hint) {
  1004. case ShaderLanguage::ShaderNode::Uniform::HINT_DEFAULT_BLACK: {
  1005. rd_texture = texture_storage->texture_rd_get_default(DEFAULT_RD_TEXTURE_BLACK);
  1006. } break;
  1007. case ShaderLanguage::ShaderNode::Uniform::HINT_ANISOTROPY: {
  1008. rd_texture = texture_storage->texture_rd_get_default(DEFAULT_RD_TEXTURE_ANISO);
  1009. } break;
  1010. case ShaderLanguage::ShaderNode::Uniform::HINT_NORMAL: {
  1011. rd_texture = texture_storage->texture_rd_get_default(DEFAULT_RD_TEXTURE_NORMAL);
  1012. } break;
  1013. case ShaderLanguage::ShaderNode::Uniform::HINT_ROUGHNESS_NORMAL: {
  1014. rd_texture = texture_storage->texture_rd_get_default(DEFAULT_RD_TEXTURE_NORMAL);
  1015. } break;
  1016. default: {
  1017. rd_texture = texture_storage->texture_rd_get_default(DEFAULT_RD_TEXTURE_WHITE);
  1018. } break;
  1019. }
  1020. } break;
  1021. case ShaderLanguage::TYPE_SAMPLERCUBE: {
  1022. switch (p_texture_uniforms[i].hint) {
  1023. case ShaderLanguage::ShaderNode::Uniform::HINT_DEFAULT_BLACK: {
  1024. rd_texture = texture_storage->texture_rd_get_default(DEFAULT_RD_TEXTURE_CUBEMAP_BLACK);
  1025. } break;
  1026. default: {
  1027. rd_texture = texture_storage->texture_rd_get_default(DEFAULT_RD_TEXTURE_CUBEMAP_WHITE);
  1028. } break;
  1029. }
  1030. } break;
  1031. case ShaderLanguage::TYPE_SAMPLERCUBEARRAY: {
  1032. rd_texture = texture_storage->texture_rd_get_default(DEFAULT_RD_TEXTURE_CUBEMAP_ARRAY_BLACK);
  1033. } break;
  1034. case ShaderLanguage::TYPE_ISAMPLER3D:
  1035. case ShaderLanguage::TYPE_USAMPLER3D:
  1036. case ShaderLanguage::TYPE_SAMPLER3D: {
  1037. rd_texture = texture_storage->texture_rd_get_default(DEFAULT_RD_TEXTURE_3D_WHITE);
  1038. } break;
  1039. case ShaderLanguage::TYPE_ISAMPLER2DARRAY:
  1040. case ShaderLanguage::TYPE_USAMPLER2DARRAY:
  1041. case ShaderLanguage::TYPE_SAMPLER2DARRAY: {
  1042. rd_texture = texture_storage->texture_rd_get_default(DEFAULT_RD_TEXTURE_2D_ARRAY_WHITE);
  1043. } break;
  1044. default: {
  1045. }
  1046. }
  1047. #ifdef TOOLS_ENABLED
  1048. if (roughness_detect_texture && normal_detect_texture && !normal_detect_texture->path.is_empty()) {
  1049. roughness_detect_texture->detect_roughness_callback(roughness_detect_texture->detect_roughness_callback_ud, normal_detect_texture->path, roughness_channel);
  1050. }
  1051. #endif
  1052. if (uniform_array_size > 0) {
  1053. for (int j = 0; j < uniform_array_size; j++) {
  1054. p_textures[k++] = rd_texture;
  1055. }
  1056. } else {
  1057. p_textures[k++] = rd_texture;
  1058. }
  1059. } else {
  1060. bool srgb = p_use_linear_color && p_texture_uniforms[i].use_color;
  1061. for (int j = 0; j < textures.size(); j++) {
  1062. Texture *tex = TextureStorage::get_singleton()->get_texture(textures[j]);
  1063. if (tex) {
  1064. rd_texture = (srgb && tex->rd_texture_srgb.is_valid()) ? tex->rd_texture_srgb : tex->rd_texture;
  1065. #ifdef TOOLS_ENABLED
  1066. if (tex->detect_3d_callback && p_use_linear_color) {
  1067. tex->detect_3d_callback(tex->detect_3d_callback_ud);
  1068. }
  1069. if (tex->detect_normal_callback && (p_texture_uniforms[i].hint == ShaderLanguage::ShaderNode::Uniform::HINT_NORMAL || p_texture_uniforms[i].hint == ShaderLanguage::ShaderNode::Uniform::HINT_ROUGHNESS_NORMAL)) {
  1070. if (p_texture_uniforms[i].hint == ShaderLanguage::ShaderNode::Uniform::HINT_ROUGHNESS_NORMAL) {
  1071. normal_detect_texture = tex;
  1072. }
  1073. tex->detect_normal_callback(tex->detect_normal_callback_ud);
  1074. }
  1075. if (tex->detect_roughness_callback && (p_texture_uniforms[i].hint >= ShaderLanguage::ShaderNode::Uniform::HINT_ROUGHNESS_R || p_texture_uniforms[i].hint <= ShaderLanguage::ShaderNode::Uniform::HINT_ROUGHNESS_GRAY)) {
  1076. //find the normal texture
  1077. roughness_detect_texture = tex;
  1078. roughness_channel = RS::TextureDetectRoughnessChannel(p_texture_uniforms[i].hint - ShaderLanguage::ShaderNode::Uniform::HINT_ROUGHNESS_R);
  1079. }
  1080. #endif
  1081. }
  1082. if (rd_texture.is_null()) {
  1083. rd_texture = texture_storage->texture_rd_get_default(DEFAULT_RD_TEXTURE_WHITE);
  1084. }
  1085. #ifdef TOOLS_ENABLED
  1086. if (roughness_detect_texture && normal_detect_texture && !normal_detect_texture->path.is_empty()) {
  1087. roughness_detect_texture->detect_roughness_callback(roughness_detect_texture->detect_roughness_callback_ud, normal_detect_texture->path, roughness_channel);
  1088. }
  1089. #endif
  1090. p_textures[k++] = rd_texture;
  1091. }
  1092. }
  1093. }
  1094. {
  1095. //for textures no longer used, unregister them
  1096. List<StringName> to_delete;
  1097. for (KeyValue<StringName, uint64_t> &E : used_global_textures) {
  1098. if (E.value != global_textures_pass) {
  1099. to_delete.push_back(E.key);
  1100. GlobalShaderUniforms::Variable *v = material_storage->global_shader_uniforms.variables.getptr(E.key);
  1101. if (v) {
  1102. v->texture_materials.erase(self);
  1103. }
  1104. }
  1105. }
  1106. while (to_delete.front()) {
  1107. used_global_textures.erase(to_delete.front()->get());
  1108. to_delete.pop_front();
  1109. }
  1110. //handle registering/unregistering global textures
  1111. if (uses_global_textures != (global_texture_E != nullptr)) {
  1112. if (uses_global_textures) {
  1113. global_texture_E = material_storage->global_shader_uniforms.materials_using_texture.push_back(self);
  1114. } else {
  1115. material_storage->global_shader_uniforms.materials_using_texture.erase(global_texture_E);
  1116. global_texture_E = nullptr;
  1117. }
  1118. }
  1119. }
  1120. }
  1121. void MaterialData::free_parameters_uniform_set(RID p_uniform_set) {
  1122. if (p_uniform_set.is_valid() && RD::get_singleton()->uniform_set_is_valid(p_uniform_set)) {
  1123. RD::get_singleton()->uniform_set_set_invalidation_callback(p_uniform_set, nullptr, nullptr);
  1124. RD::get_singleton()->free(p_uniform_set);
  1125. }
  1126. }
  1127. bool MaterialData::update_parameters_uniform_set(const HashMap<StringName, Variant> &p_parameters, bool p_uniform_dirty, bool p_textures_dirty, const HashMap<StringName, ShaderLanguage::ShaderNode::Uniform> &p_uniforms, const uint32_t *p_uniform_offsets, const Vector<ShaderCompiler::GeneratedCode::Texture> &p_texture_uniforms, const HashMap<StringName, HashMap<int, RID>> &p_default_texture_params, uint32_t p_ubo_size, RID &uniform_set, RID p_shader, uint32_t p_shader_uniform_set, uint32_t p_barrier) {
  1128. if ((uint32_t)ubo_data.size() != p_ubo_size) {
  1129. p_uniform_dirty = true;
  1130. if (uniform_buffer.is_valid()) {
  1131. RD::get_singleton()->free(uniform_buffer);
  1132. uniform_buffer = RID();
  1133. }
  1134. ubo_data.resize(p_ubo_size);
  1135. if (ubo_data.size()) {
  1136. uniform_buffer = RD::get_singleton()->uniform_buffer_create(ubo_data.size());
  1137. memset(ubo_data.ptrw(), 0, ubo_data.size()); //clear
  1138. }
  1139. //clear previous uniform set
  1140. if (uniform_set.is_valid() && RD::get_singleton()->uniform_set_is_valid(uniform_set)) {
  1141. RD::get_singleton()->uniform_set_set_invalidation_callback(uniform_set, nullptr, nullptr);
  1142. RD::get_singleton()->free(uniform_set);
  1143. uniform_set = RID();
  1144. }
  1145. }
  1146. //check whether buffer changed
  1147. if (p_uniform_dirty && ubo_data.size()) {
  1148. update_uniform_buffer(p_uniforms, p_uniform_offsets, p_parameters, ubo_data.ptrw(), ubo_data.size(), true);
  1149. RD::get_singleton()->buffer_update(uniform_buffer, 0, ubo_data.size(), ubo_data.ptrw(), p_barrier);
  1150. }
  1151. uint32_t tex_uniform_count = 0U;
  1152. for (int i = 0; i < p_texture_uniforms.size(); i++) {
  1153. tex_uniform_count += uint32_t(p_texture_uniforms[i].array_size > 0 ? p_texture_uniforms[i].array_size : 1);
  1154. }
  1155. if ((uint32_t)texture_cache.size() != tex_uniform_count || p_textures_dirty) {
  1156. texture_cache.resize(tex_uniform_count);
  1157. p_textures_dirty = true;
  1158. //clear previous uniform set
  1159. if (uniform_set.is_valid() && RD::get_singleton()->uniform_set_is_valid(uniform_set)) {
  1160. RD::get_singleton()->uniform_set_set_invalidation_callback(uniform_set, nullptr, nullptr);
  1161. RD::get_singleton()->free(uniform_set);
  1162. uniform_set = RID();
  1163. }
  1164. }
  1165. if (p_textures_dirty && tex_uniform_count) {
  1166. update_textures(p_parameters, p_default_texture_params, p_texture_uniforms, texture_cache.ptrw(), true);
  1167. }
  1168. if (p_ubo_size == 0 && p_texture_uniforms.size() == 0) {
  1169. // This material does not require an uniform set, so don't create it.
  1170. return false;
  1171. }
  1172. if (!p_textures_dirty && uniform_set.is_valid() && RD::get_singleton()->uniform_set_is_valid(uniform_set)) {
  1173. //no reason to update uniform set, only UBO (or nothing) was needed to update
  1174. return false;
  1175. }
  1176. Vector<RD::Uniform> uniforms;
  1177. {
  1178. if (p_ubo_size) {
  1179. RD::Uniform u;
  1180. u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
  1181. u.binding = 0;
  1182. u.append_id(uniform_buffer);
  1183. uniforms.push_back(u);
  1184. }
  1185. const RID *textures = texture_cache.ptrw();
  1186. for (int i = 0, k = 0; i < p_texture_uniforms.size(); i++) {
  1187. const int array_size = p_texture_uniforms[i].array_size;
  1188. RD::Uniform u;
  1189. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  1190. u.binding = 1 + k;
  1191. if (array_size > 0) {
  1192. for (int j = 0; j < array_size; j++) {
  1193. u.append_id(textures[k++]);
  1194. }
  1195. } else {
  1196. u.append_id(textures[k++]);
  1197. }
  1198. uniforms.push_back(u);
  1199. }
  1200. }
  1201. uniform_set = RD::get_singleton()->uniform_set_create(uniforms, p_shader, p_shader_uniform_set);
  1202. RD::get_singleton()->uniform_set_set_invalidation_callback(uniform_set, MaterialStorage::_material_uniform_set_erased, &self);
  1203. return true;
  1204. }
  1205. ///////////////////////////////////////////////////////////////////////////
  1206. // MaterialStorage
  1207. MaterialStorage *MaterialStorage::singleton = nullptr;
  1208. MaterialStorage *MaterialStorage::get_singleton() {
  1209. return singleton;
  1210. }
  1211. MaterialStorage::MaterialStorage() {
  1212. singleton = this;
  1213. //default samplers
  1214. for (int i = 1; i < RS::CANVAS_ITEM_TEXTURE_FILTER_MAX; i++) {
  1215. for (int j = 1; j < RS::CANVAS_ITEM_TEXTURE_REPEAT_MAX; j++) {
  1216. RD::SamplerState sampler_state;
  1217. switch (i) {
  1218. case RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST: {
  1219. sampler_state.mag_filter = RD::SAMPLER_FILTER_NEAREST;
  1220. sampler_state.min_filter = RD::SAMPLER_FILTER_NEAREST;
  1221. sampler_state.max_lod = 0;
  1222. } break;
  1223. case RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR: {
  1224. sampler_state.mag_filter = RD::SAMPLER_FILTER_LINEAR;
  1225. sampler_state.min_filter = RD::SAMPLER_FILTER_LINEAR;
  1226. sampler_state.max_lod = 0;
  1227. } break;
  1228. case RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS: {
  1229. sampler_state.mag_filter = RD::SAMPLER_FILTER_NEAREST;
  1230. sampler_state.min_filter = RD::SAMPLER_FILTER_NEAREST;
  1231. if (GLOBAL_GET("rendering/textures/default_filters/use_nearest_mipmap_filter")) {
  1232. sampler_state.mip_filter = RD::SAMPLER_FILTER_NEAREST;
  1233. } else {
  1234. sampler_state.mip_filter = RD::SAMPLER_FILTER_LINEAR;
  1235. }
  1236. } break;
  1237. case RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS: {
  1238. sampler_state.mag_filter = RD::SAMPLER_FILTER_LINEAR;
  1239. sampler_state.min_filter = RD::SAMPLER_FILTER_LINEAR;
  1240. if (GLOBAL_GET("rendering/textures/default_filters/use_nearest_mipmap_filter")) {
  1241. sampler_state.mip_filter = RD::SAMPLER_FILTER_NEAREST;
  1242. } else {
  1243. sampler_state.mip_filter = RD::SAMPLER_FILTER_LINEAR;
  1244. }
  1245. } break;
  1246. case RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS_ANISOTROPIC: {
  1247. sampler_state.mag_filter = RD::SAMPLER_FILTER_NEAREST;
  1248. sampler_state.min_filter = RD::SAMPLER_FILTER_NEAREST;
  1249. if (GLOBAL_GET("rendering/textures/default_filters/use_nearest_mipmap_filter")) {
  1250. sampler_state.mip_filter = RD::SAMPLER_FILTER_NEAREST;
  1251. } else {
  1252. sampler_state.mip_filter = RD::SAMPLER_FILTER_LINEAR;
  1253. }
  1254. sampler_state.use_anisotropy = true;
  1255. sampler_state.anisotropy_max = 1 << int(GLOBAL_GET("rendering/textures/default_filters/anisotropic_filtering_level"));
  1256. } break;
  1257. case RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS_ANISOTROPIC: {
  1258. sampler_state.mag_filter = RD::SAMPLER_FILTER_LINEAR;
  1259. sampler_state.min_filter = RD::SAMPLER_FILTER_LINEAR;
  1260. if (GLOBAL_GET("rendering/textures/default_filters/use_nearest_mipmap_filter")) {
  1261. sampler_state.mip_filter = RD::SAMPLER_FILTER_NEAREST;
  1262. } else {
  1263. sampler_state.mip_filter = RD::SAMPLER_FILTER_LINEAR;
  1264. }
  1265. sampler_state.use_anisotropy = true;
  1266. sampler_state.anisotropy_max = 1 << int(GLOBAL_GET("rendering/textures/default_filters/anisotropic_filtering_level"));
  1267. } break;
  1268. default: {
  1269. }
  1270. }
  1271. switch (j) {
  1272. case RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED: {
  1273. sampler_state.repeat_u = RD::SAMPLER_REPEAT_MODE_CLAMP_TO_EDGE;
  1274. sampler_state.repeat_v = RD::SAMPLER_REPEAT_MODE_CLAMP_TO_EDGE;
  1275. sampler_state.repeat_w = RD::SAMPLER_REPEAT_MODE_CLAMP_TO_EDGE;
  1276. } break;
  1277. case RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED: {
  1278. sampler_state.repeat_u = RD::SAMPLER_REPEAT_MODE_REPEAT;
  1279. sampler_state.repeat_v = RD::SAMPLER_REPEAT_MODE_REPEAT;
  1280. sampler_state.repeat_w = RD::SAMPLER_REPEAT_MODE_REPEAT;
  1281. } break;
  1282. case RS::CANVAS_ITEM_TEXTURE_REPEAT_MIRROR: {
  1283. sampler_state.repeat_u = RD::SAMPLER_REPEAT_MODE_MIRRORED_REPEAT;
  1284. sampler_state.repeat_v = RD::SAMPLER_REPEAT_MODE_MIRRORED_REPEAT;
  1285. sampler_state.repeat_w = RD::SAMPLER_REPEAT_MODE_MIRRORED_REPEAT;
  1286. } break;
  1287. default: {
  1288. }
  1289. }
  1290. default_rd_samplers[i][j] = RD::get_singleton()->sampler_create(sampler_state);
  1291. }
  1292. }
  1293. //custom sampler
  1294. sampler_rd_configure_custom(0.0f);
  1295. // buffers
  1296. { //create index array for copy shaders
  1297. Vector<uint8_t> pv;
  1298. pv.resize(6 * 4);
  1299. {
  1300. uint8_t *w = pv.ptrw();
  1301. int *p32 = (int *)w;
  1302. p32[0] = 0;
  1303. p32[1] = 1;
  1304. p32[2] = 2;
  1305. p32[3] = 0;
  1306. p32[4] = 2;
  1307. p32[5] = 3;
  1308. }
  1309. quad_index_buffer = RD::get_singleton()->index_buffer_create(6, RenderingDevice::INDEX_BUFFER_FORMAT_UINT32, pv);
  1310. quad_index_array = RD::get_singleton()->index_array_create(quad_index_buffer, 0, 6);
  1311. }
  1312. // Shaders
  1313. for (int i = 0; i < SHADER_TYPE_MAX; i++) {
  1314. shader_data_request_func[i] = nullptr;
  1315. }
  1316. static_assert(sizeof(GlobalShaderUniforms::Value) == 16);
  1317. global_shader_uniforms.buffer_size = MAX(4096, (int)GLOBAL_GET("rendering/limits/global_shader_variables/buffer_size"));
  1318. global_shader_uniforms.buffer_values = memnew_arr(GlobalShaderUniforms::Value, global_shader_uniforms.buffer_size);
  1319. memset(global_shader_uniforms.buffer_values, 0, sizeof(GlobalShaderUniforms::Value) * global_shader_uniforms.buffer_size);
  1320. global_shader_uniforms.buffer_usage = memnew_arr(GlobalShaderUniforms::ValueUsage, global_shader_uniforms.buffer_size);
  1321. global_shader_uniforms.buffer_dirty_regions = memnew_arr(bool, global_shader_uniforms.buffer_size / GlobalShaderUniforms::BUFFER_DIRTY_REGION_SIZE);
  1322. memset(global_shader_uniforms.buffer_dirty_regions, 0, sizeof(bool) * global_shader_uniforms.buffer_size / GlobalShaderUniforms::BUFFER_DIRTY_REGION_SIZE);
  1323. global_shader_uniforms.buffer = RD::get_singleton()->storage_buffer_create(sizeof(GlobalShaderUniforms::Value) * global_shader_uniforms.buffer_size);
  1324. }
  1325. MaterialStorage::~MaterialStorage() {
  1326. memdelete_arr(global_shader_uniforms.buffer_values);
  1327. memdelete_arr(global_shader_uniforms.buffer_usage);
  1328. memdelete_arr(global_shader_uniforms.buffer_dirty_regions);
  1329. RD::get_singleton()->free(global_shader_uniforms.buffer);
  1330. // buffers
  1331. RD::get_singleton()->free(quad_index_buffer); //array gets freed as dependency
  1332. //def samplers
  1333. for (int i = 1; i < RS::CANVAS_ITEM_TEXTURE_FILTER_MAX; i++) {
  1334. for (int j = 1; j < RS::CANVAS_ITEM_TEXTURE_REPEAT_MAX; j++) {
  1335. RD::get_singleton()->free(default_rd_samplers[i][j]);
  1336. }
  1337. }
  1338. //custom samplers
  1339. for (int i = 1; i < RS::CANVAS_ITEM_TEXTURE_FILTER_MAX; i++) {
  1340. for (int j = 0; j < RS::CANVAS_ITEM_TEXTURE_REPEAT_MAX; j++) {
  1341. if (custom_rd_samplers[i][j].is_valid()) {
  1342. RD::get_singleton()->free(custom_rd_samplers[i][j]);
  1343. }
  1344. }
  1345. }
  1346. singleton = nullptr;
  1347. }
  1348. /* Samplers */
  1349. void MaterialStorage::sampler_rd_configure_custom(float p_mipmap_bias) {
  1350. for (int i = 1; i < RS::CANVAS_ITEM_TEXTURE_FILTER_MAX; i++) {
  1351. for (int j = 1; j < RS::CANVAS_ITEM_TEXTURE_REPEAT_MAX; j++) {
  1352. RD::SamplerState sampler_state;
  1353. switch (i) {
  1354. case RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST: {
  1355. sampler_state.mag_filter = RD::SAMPLER_FILTER_NEAREST;
  1356. sampler_state.min_filter = RD::SAMPLER_FILTER_NEAREST;
  1357. sampler_state.max_lod = 0;
  1358. } break;
  1359. case RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR: {
  1360. sampler_state.mag_filter = RD::SAMPLER_FILTER_LINEAR;
  1361. sampler_state.min_filter = RD::SAMPLER_FILTER_LINEAR;
  1362. sampler_state.max_lod = 0;
  1363. } break;
  1364. case RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS: {
  1365. sampler_state.mag_filter = RD::SAMPLER_FILTER_NEAREST;
  1366. sampler_state.min_filter = RD::SAMPLER_FILTER_NEAREST;
  1367. if (GLOBAL_GET("rendering/textures/default_filters/use_nearest_mipmap_filter")) {
  1368. sampler_state.mip_filter = RD::SAMPLER_FILTER_NEAREST;
  1369. } else {
  1370. sampler_state.mip_filter = RD::SAMPLER_FILTER_LINEAR;
  1371. }
  1372. sampler_state.lod_bias = p_mipmap_bias;
  1373. } break;
  1374. case RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS: {
  1375. sampler_state.mag_filter = RD::SAMPLER_FILTER_LINEAR;
  1376. sampler_state.min_filter = RD::SAMPLER_FILTER_LINEAR;
  1377. if (GLOBAL_GET("rendering/textures/default_filters/use_nearest_mipmap_filter")) {
  1378. sampler_state.mip_filter = RD::SAMPLER_FILTER_NEAREST;
  1379. } else {
  1380. sampler_state.mip_filter = RD::SAMPLER_FILTER_LINEAR;
  1381. }
  1382. sampler_state.lod_bias = p_mipmap_bias;
  1383. } break;
  1384. case RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS_ANISOTROPIC: {
  1385. sampler_state.mag_filter = RD::SAMPLER_FILTER_NEAREST;
  1386. sampler_state.min_filter = RD::SAMPLER_FILTER_NEAREST;
  1387. if (GLOBAL_GET("rendering/textures/default_filters/use_nearest_mipmap_filter")) {
  1388. sampler_state.mip_filter = RD::SAMPLER_FILTER_NEAREST;
  1389. } else {
  1390. sampler_state.mip_filter = RD::SAMPLER_FILTER_LINEAR;
  1391. }
  1392. sampler_state.lod_bias = p_mipmap_bias;
  1393. sampler_state.use_anisotropy = true;
  1394. sampler_state.anisotropy_max = 1 << int(GLOBAL_GET("rendering/textures/default_filters/anisotropic_filtering_level"));
  1395. } break;
  1396. case RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS_ANISOTROPIC: {
  1397. sampler_state.mag_filter = RD::SAMPLER_FILTER_LINEAR;
  1398. sampler_state.min_filter = RD::SAMPLER_FILTER_LINEAR;
  1399. if (GLOBAL_GET("rendering/textures/default_filters/use_nearest_mipmap_filter")) {
  1400. sampler_state.mip_filter = RD::SAMPLER_FILTER_NEAREST;
  1401. } else {
  1402. sampler_state.mip_filter = RD::SAMPLER_FILTER_LINEAR;
  1403. }
  1404. sampler_state.lod_bias = p_mipmap_bias;
  1405. sampler_state.use_anisotropy = true;
  1406. sampler_state.anisotropy_max = 1 << int(GLOBAL_GET("rendering/textures/default_filters/anisotropic_filtering_level"));
  1407. } break;
  1408. default: {
  1409. }
  1410. }
  1411. switch (j) {
  1412. case RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED: {
  1413. sampler_state.repeat_u = RD::SAMPLER_REPEAT_MODE_CLAMP_TO_EDGE;
  1414. sampler_state.repeat_v = RD::SAMPLER_REPEAT_MODE_CLAMP_TO_EDGE;
  1415. sampler_state.repeat_w = RD::SAMPLER_REPEAT_MODE_CLAMP_TO_EDGE;
  1416. } break;
  1417. case RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED: {
  1418. sampler_state.repeat_u = RD::SAMPLER_REPEAT_MODE_REPEAT;
  1419. sampler_state.repeat_v = RD::SAMPLER_REPEAT_MODE_REPEAT;
  1420. sampler_state.repeat_w = RD::SAMPLER_REPEAT_MODE_REPEAT;
  1421. } break;
  1422. case RS::CANVAS_ITEM_TEXTURE_REPEAT_MIRROR: {
  1423. sampler_state.repeat_u = RD::SAMPLER_REPEAT_MODE_MIRRORED_REPEAT;
  1424. sampler_state.repeat_v = RD::SAMPLER_REPEAT_MODE_MIRRORED_REPEAT;
  1425. sampler_state.repeat_w = RD::SAMPLER_REPEAT_MODE_MIRRORED_REPEAT;
  1426. } break;
  1427. default: {
  1428. }
  1429. }
  1430. if (custom_rd_samplers[i][j].is_valid()) {
  1431. RD::get_singleton()->free(custom_rd_samplers[i][j]);
  1432. }
  1433. custom_rd_samplers[i][j] = RD::get_singleton()->sampler_create(sampler_state);
  1434. }
  1435. }
  1436. }
  1437. /* GLOBAL SHADER UNIFORM API */
  1438. int32_t MaterialStorage::_global_shader_uniform_allocate(uint32_t p_elements) {
  1439. int32_t idx = 0;
  1440. while (idx + p_elements <= global_shader_uniforms.buffer_size) {
  1441. if (global_shader_uniforms.buffer_usage[idx].elements == 0) {
  1442. bool valid = true;
  1443. for (uint32_t i = 1; i < p_elements; i++) {
  1444. if (global_shader_uniforms.buffer_usage[idx + i].elements > 0) {
  1445. valid = false;
  1446. idx += i + global_shader_uniforms.buffer_usage[idx + i].elements;
  1447. break;
  1448. }
  1449. }
  1450. if (!valid) {
  1451. continue; //if not valid, idx is in new position
  1452. }
  1453. return idx;
  1454. } else {
  1455. idx += global_shader_uniforms.buffer_usage[idx].elements;
  1456. }
  1457. }
  1458. return -1;
  1459. }
  1460. void MaterialStorage::_global_shader_uniform_store_in_buffer(int32_t p_index, RS::GlobalShaderUniformType p_type, const Variant &p_value) {
  1461. switch (p_type) {
  1462. case RS::GLOBAL_VAR_TYPE_BOOL: {
  1463. GlobalShaderUniforms::Value &bv = global_shader_uniforms.buffer_values[p_index];
  1464. bool b = p_value;
  1465. bv.x = b ? 1.0 : 0.0;
  1466. bv.y = 0.0;
  1467. bv.z = 0.0;
  1468. bv.w = 0.0;
  1469. } break;
  1470. case RS::GLOBAL_VAR_TYPE_BVEC2: {
  1471. GlobalShaderUniforms::Value &bv = global_shader_uniforms.buffer_values[p_index];
  1472. uint32_t bvec = p_value;
  1473. bv.x = (bvec & 1) ? 1.0 : 0.0;
  1474. bv.y = (bvec & 2) ? 1.0 : 0.0;
  1475. bv.z = 0.0;
  1476. bv.w = 0.0;
  1477. } break;
  1478. case RS::GLOBAL_VAR_TYPE_BVEC3: {
  1479. GlobalShaderUniforms::Value &bv = global_shader_uniforms.buffer_values[p_index];
  1480. uint32_t bvec = p_value;
  1481. bv.x = (bvec & 1) ? 1.0 : 0.0;
  1482. bv.y = (bvec & 2) ? 1.0 : 0.0;
  1483. bv.z = (bvec & 4) ? 1.0 : 0.0;
  1484. bv.w = 0.0;
  1485. } break;
  1486. case RS::GLOBAL_VAR_TYPE_BVEC4: {
  1487. GlobalShaderUniforms::Value &bv = global_shader_uniforms.buffer_values[p_index];
  1488. uint32_t bvec = p_value;
  1489. bv.x = (bvec & 1) ? 1.0 : 0.0;
  1490. bv.y = (bvec & 2) ? 1.0 : 0.0;
  1491. bv.z = (bvec & 4) ? 1.0 : 0.0;
  1492. bv.w = (bvec & 8) ? 1.0 : 0.0;
  1493. } break;
  1494. case RS::GLOBAL_VAR_TYPE_INT: {
  1495. GlobalShaderUniforms::ValueInt &bv = *(GlobalShaderUniforms::ValueInt *)&global_shader_uniforms.buffer_values[p_index];
  1496. int32_t v = p_value;
  1497. bv.x = v;
  1498. bv.y = 0;
  1499. bv.z = 0;
  1500. bv.w = 0;
  1501. } break;
  1502. case RS::GLOBAL_VAR_TYPE_IVEC2: {
  1503. GlobalShaderUniforms::ValueInt &bv = *(GlobalShaderUniforms::ValueInt *)&global_shader_uniforms.buffer_values[p_index];
  1504. Vector2i v = p_value;
  1505. bv.x = v.x;
  1506. bv.y = v.y;
  1507. bv.z = 0;
  1508. bv.w = 0;
  1509. } break;
  1510. case RS::GLOBAL_VAR_TYPE_IVEC3: {
  1511. GlobalShaderUniforms::ValueInt &bv = *(GlobalShaderUniforms::ValueInt *)&global_shader_uniforms.buffer_values[p_index];
  1512. Vector3i v = p_value;
  1513. bv.x = v.x;
  1514. bv.y = v.y;
  1515. bv.z = v.z;
  1516. bv.w = 0;
  1517. } break;
  1518. case RS::GLOBAL_VAR_TYPE_IVEC4: {
  1519. GlobalShaderUniforms::ValueInt &bv = *(GlobalShaderUniforms::ValueInt *)&global_shader_uniforms.buffer_values[p_index];
  1520. Vector<int32_t> v = p_value;
  1521. bv.x = v.size() >= 1 ? v[0] : 0;
  1522. bv.y = v.size() >= 2 ? v[1] : 0;
  1523. bv.z = v.size() >= 3 ? v[2] : 0;
  1524. bv.w = v.size() >= 4 ? v[3] : 0;
  1525. } break;
  1526. case RS::GLOBAL_VAR_TYPE_RECT2I: {
  1527. GlobalShaderUniforms::ValueInt &bv = *(GlobalShaderUniforms::ValueInt *)&global_shader_uniforms.buffer_values[p_index];
  1528. Rect2i v = p_value;
  1529. bv.x = v.position.x;
  1530. bv.y = v.position.y;
  1531. bv.z = v.size.x;
  1532. bv.w = v.size.y;
  1533. } break;
  1534. case RS::GLOBAL_VAR_TYPE_UINT: {
  1535. GlobalShaderUniforms::ValueUInt &bv = *(GlobalShaderUniforms::ValueUInt *)&global_shader_uniforms.buffer_values[p_index];
  1536. uint32_t v = p_value;
  1537. bv.x = v;
  1538. bv.y = 0;
  1539. bv.z = 0;
  1540. bv.w = 0;
  1541. } break;
  1542. case RS::GLOBAL_VAR_TYPE_UVEC2: {
  1543. GlobalShaderUniforms::ValueUInt &bv = *(GlobalShaderUniforms::ValueUInt *)&global_shader_uniforms.buffer_values[p_index];
  1544. Vector2i v = p_value;
  1545. bv.x = v.x;
  1546. bv.y = v.y;
  1547. bv.z = 0;
  1548. bv.w = 0;
  1549. } break;
  1550. case RS::GLOBAL_VAR_TYPE_UVEC3: {
  1551. GlobalShaderUniforms::ValueUInt &bv = *(GlobalShaderUniforms::ValueUInt *)&global_shader_uniforms.buffer_values[p_index];
  1552. Vector3i v = p_value;
  1553. bv.x = v.x;
  1554. bv.y = v.y;
  1555. bv.z = v.z;
  1556. bv.w = 0;
  1557. } break;
  1558. case RS::GLOBAL_VAR_TYPE_UVEC4: {
  1559. GlobalShaderUniforms::ValueUInt &bv = *(GlobalShaderUniforms::ValueUInt *)&global_shader_uniforms.buffer_values[p_index];
  1560. Vector<int32_t> v = p_value;
  1561. bv.x = v.size() >= 1 ? v[0] : 0;
  1562. bv.y = v.size() >= 2 ? v[1] : 0;
  1563. bv.z = v.size() >= 3 ? v[2] : 0;
  1564. bv.w = v.size() >= 4 ? v[3] : 0;
  1565. } break;
  1566. case RS::GLOBAL_VAR_TYPE_FLOAT: {
  1567. GlobalShaderUniforms::Value &bv = global_shader_uniforms.buffer_values[p_index];
  1568. float v = p_value;
  1569. bv.x = v;
  1570. bv.y = 0;
  1571. bv.z = 0;
  1572. bv.w = 0;
  1573. } break;
  1574. case RS::GLOBAL_VAR_TYPE_VEC2: {
  1575. GlobalShaderUniforms::Value &bv = global_shader_uniforms.buffer_values[p_index];
  1576. Vector2 v = p_value;
  1577. bv.x = v.x;
  1578. bv.y = v.y;
  1579. bv.z = 0;
  1580. bv.w = 0;
  1581. } break;
  1582. case RS::GLOBAL_VAR_TYPE_VEC3: {
  1583. GlobalShaderUniforms::Value &bv = global_shader_uniforms.buffer_values[p_index];
  1584. Vector3 v = p_value;
  1585. bv.x = v.x;
  1586. bv.y = v.y;
  1587. bv.z = v.z;
  1588. bv.w = 0;
  1589. } break;
  1590. case RS::GLOBAL_VAR_TYPE_VEC4: {
  1591. GlobalShaderUniforms::Value &bv = global_shader_uniforms.buffer_values[p_index];
  1592. Plane v = p_value;
  1593. bv.x = v.normal.x;
  1594. bv.y = v.normal.y;
  1595. bv.z = v.normal.z;
  1596. bv.w = v.d;
  1597. } break;
  1598. case RS::GLOBAL_VAR_TYPE_COLOR: {
  1599. GlobalShaderUniforms::Value &bv = global_shader_uniforms.buffer_values[p_index];
  1600. Color v = p_value;
  1601. bv.x = v.r;
  1602. bv.y = v.g;
  1603. bv.z = v.b;
  1604. bv.w = v.a;
  1605. GlobalShaderUniforms::Value &bv_linear = global_shader_uniforms.buffer_values[p_index + 1];
  1606. v = v.srgb_to_linear();
  1607. bv_linear.x = v.r;
  1608. bv_linear.y = v.g;
  1609. bv_linear.z = v.b;
  1610. bv_linear.w = v.a;
  1611. } break;
  1612. case RS::GLOBAL_VAR_TYPE_RECT2: {
  1613. GlobalShaderUniforms::Value &bv = global_shader_uniforms.buffer_values[p_index];
  1614. Rect2 v = p_value;
  1615. bv.x = v.position.x;
  1616. bv.y = v.position.y;
  1617. bv.z = v.size.x;
  1618. bv.w = v.size.y;
  1619. } break;
  1620. case RS::GLOBAL_VAR_TYPE_MAT2: {
  1621. GlobalShaderUniforms::Value *bv = &global_shader_uniforms.buffer_values[p_index];
  1622. Vector<float> m2 = p_value;
  1623. if (m2.size() < 4) {
  1624. m2.resize(4);
  1625. }
  1626. bv[0].x = m2[0];
  1627. bv[0].y = m2[1];
  1628. bv[0].z = 0;
  1629. bv[0].w = 0;
  1630. bv[1].x = m2[2];
  1631. bv[1].y = m2[3];
  1632. bv[1].z = 0;
  1633. bv[1].w = 0;
  1634. } break;
  1635. case RS::GLOBAL_VAR_TYPE_MAT3: {
  1636. GlobalShaderUniforms::Value *bv = &global_shader_uniforms.buffer_values[p_index];
  1637. Basis v = p_value;
  1638. bv[0].x = v.rows[0][0];
  1639. bv[0].y = v.rows[1][0];
  1640. bv[0].z = v.rows[2][0];
  1641. bv[0].w = 0;
  1642. bv[1].x = v.rows[0][1];
  1643. bv[1].y = v.rows[1][1];
  1644. bv[1].z = v.rows[2][1];
  1645. bv[1].w = 0;
  1646. bv[2].x = v.rows[0][2];
  1647. bv[2].y = v.rows[1][2];
  1648. bv[2].z = v.rows[2][2];
  1649. bv[2].w = 0;
  1650. } break;
  1651. case RS::GLOBAL_VAR_TYPE_MAT4: {
  1652. GlobalShaderUniforms::Value *bv = &global_shader_uniforms.buffer_values[p_index];
  1653. Vector<float> m2 = p_value;
  1654. if (m2.size() < 16) {
  1655. m2.resize(16);
  1656. }
  1657. bv[0].x = m2[0];
  1658. bv[0].y = m2[1];
  1659. bv[0].z = m2[2];
  1660. bv[0].w = m2[3];
  1661. bv[1].x = m2[4];
  1662. bv[1].y = m2[5];
  1663. bv[1].z = m2[6];
  1664. bv[1].w = m2[7];
  1665. bv[2].x = m2[8];
  1666. bv[2].y = m2[9];
  1667. bv[2].z = m2[10];
  1668. bv[2].w = m2[11];
  1669. bv[3].x = m2[12];
  1670. bv[3].y = m2[13];
  1671. bv[3].z = m2[14];
  1672. bv[3].w = m2[15];
  1673. } break;
  1674. case RS::GLOBAL_VAR_TYPE_TRANSFORM_2D: {
  1675. GlobalShaderUniforms::Value *bv = &global_shader_uniforms.buffer_values[p_index];
  1676. Transform2D v = p_value;
  1677. bv[0].x = v.columns[0][0];
  1678. bv[0].y = v.columns[0][1];
  1679. bv[0].z = 0;
  1680. bv[0].w = 0;
  1681. bv[1].x = v.columns[1][0];
  1682. bv[1].y = v.columns[1][1];
  1683. bv[1].z = 0;
  1684. bv[1].w = 0;
  1685. bv[2].x = v.columns[2][0];
  1686. bv[2].y = v.columns[2][1];
  1687. bv[2].z = 1;
  1688. bv[2].w = 0;
  1689. } break;
  1690. case RS::GLOBAL_VAR_TYPE_TRANSFORM: {
  1691. GlobalShaderUniforms::Value *bv = &global_shader_uniforms.buffer_values[p_index];
  1692. Transform3D v = p_value;
  1693. bv[0].x = v.basis.rows[0][0];
  1694. bv[0].y = v.basis.rows[1][0];
  1695. bv[0].z = v.basis.rows[2][0];
  1696. bv[0].w = 0;
  1697. bv[1].x = v.basis.rows[0][1];
  1698. bv[1].y = v.basis.rows[1][1];
  1699. bv[1].z = v.basis.rows[2][1];
  1700. bv[1].w = 0;
  1701. bv[2].x = v.basis.rows[0][2];
  1702. bv[2].y = v.basis.rows[1][2];
  1703. bv[2].z = v.basis.rows[2][2];
  1704. bv[2].w = 0;
  1705. bv[3].x = v.origin.x;
  1706. bv[3].y = v.origin.y;
  1707. bv[3].z = v.origin.z;
  1708. bv[3].w = 1;
  1709. } break;
  1710. default: {
  1711. ERR_FAIL();
  1712. }
  1713. }
  1714. }
  1715. void MaterialStorage::_global_shader_uniform_mark_buffer_dirty(int32_t p_index, int32_t p_elements) {
  1716. int32_t prev_chunk = -1;
  1717. for (int32_t i = 0; i < p_elements; i++) {
  1718. int32_t chunk = (p_index + i) / GlobalShaderUniforms::BUFFER_DIRTY_REGION_SIZE;
  1719. if (chunk != prev_chunk) {
  1720. if (!global_shader_uniforms.buffer_dirty_regions[chunk]) {
  1721. global_shader_uniforms.buffer_dirty_regions[chunk] = true;
  1722. global_shader_uniforms.buffer_dirty_region_count++;
  1723. }
  1724. }
  1725. prev_chunk = chunk;
  1726. }
  1727. }
  1728. void MaterialStorage::global_shader_uniform_add(const StringName &p_name, RS::GlobalShaderUniformType p_type, const Variant &p_value) {
  1729. ERR_FAIL_COND(global_shader_uniforms.variables.has(p_name));
  1730. GlobalShaderUniforms::Variable gv;
  1731. gv.type = p_type;
  1732. gv.value = p_value;
  1733. gv.buffer_index = -1;
  1734. if (p_type >= RS::GLOBAL_VAR_TYPE_SAMPLER2D) {
  1735. //is texture
  1736. global_shader_uniforms.must_update_texture_materials = true; //normally there are none
  1737. } else {
  1738. gv.buffer_elements = 1;
  1739. if (p_type == RS::GLOBAL_VAR_TYPE_COLOR || p_type == RS::GLOBAL_VAR_TYPE_MAT2) {
  1740. //color needs to elements to store srgb and linear
  1741. gv.buffer_elements = 2;
  1742. }
  1743. if (p_type == RS::GLOBAL_VAR_TYPE_MAT3 || p_type == RS::GLOBAL_VAR_TYPE_TRANSFORM_2D) {
  1744. //color needs to elements to store srgb and linear
  1745. gv.buffer_elements = 3;
  1746. }
  1747. if (p_type == RS::GLOBAL_VAR_TYPE_MAT4 || p_type == RS::GLOBAL_VAR_TYPE_TRANSFORM) {
  1748. //color needs to elements to store srgb and linear
  1749. gv.buffer_elements = 4;
  1750. }
  1751. //is vector, allocate in buffer and update index
  1752. gv.buffer_index = _global_shader_uniform_allocate(gv.buffer_elements);
  1753. ERR_FAIL_COND_MSG(gv.buffer_index < 0, vformat("Failed allocating global variable '%s' out of buffer memory. Consider increasing it in the Project Settings.", String(p_name)));
  1754. global_shader_uniforms.buffer_usage[gv.buffer_index].elements = gv.buffer_elements;
  1755. _global_shader_uniform_store_in_buffer(gv.buffer_index, gv.type, gv.value);
  1756. _global_shader_uniform_mark_buffer_dirty(gv.buffer_index, gv.buffer_elements);
  1757. global_shader_uniforms.must_update_buffer_materials = true; //normally there are none
  1758. }
  1759. global_shader_uniforms.variables[p_name] = gv;
  1760. }
  1761. void MaterialStorage::global_shader_uniform_remove(const StringName &p_name) {
  1762. if (!global_shader_uniforms.variables.has(p_name)) {
  1763. return;
  1764. }
  1765. const GlobalShaderUniforms::Variable &gv = global_shader_uniforms.variables[p_name];
  1766. if (gv.buffer_index >= 0) {
  1767. global_shader_uniforms.buffer_usage[gv.buffer_index].elements = 0;
  1768. global_shader_uniforms.must_update_buffer_materials = true;
  1769. } else {
  1770. global_shader_uniforms.must_update_texture_materials = true;
  1771. }
  1772. global_shader_uniforms.variables.erase(p_name);
  1773. }
  1774. Vector<StringName> MaterialStorage::global_shader_uniform_get_list() const {
  1775. if (!Engine::get_singleton()->is_editor_hint()) {
  1776. ERR_FAIL_V_MSG(Vector<StringName>(), "This function should never be used outside the editor, it can severely damage performance.");
  1777. }
  1778. Vector<StringName> names;
  1779. for (const KeyValue<StringName, GlobalShaderUniforms::Variable> &E : global_shader_uniforms.variables) {
  1780. names.push_back(E.key);
  1781. }
  1782. names.sort_custom<StringName::AlphCompare>();
  1783. return names;
  1784. }
  1785. void MaterialStorage::global_shader_uniform_set(const StringName &p_name, const Variant &p_value) {
  1786. ERR_FAIL_COND(!global_shader_uniforms.variables.has(p_name));
  1787. GlobalShaderUniforms::Variable &gv = global_shader_uniforms.variables[p_name];
  1788. gv.value = p_value;
  1789. if (gv.override.get_type() == Variant::NIL) {
  1790. if (gv.buffer_index >= 0) {
  1791. //buffer
  1792. _global_shader_uniform_store_in_buffer(gv.buffer_index, gv.type, gv.value);
  1793. _global_shader_uniform_mark_buffer_dirty(gv.buffer_index, gv.buffer_elements);
  1794. } else {
  1795. //texture
  1796. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  1797. for (const RID &E : gv.texture_materials) {
  1798. Material *material = material_storage->get_material(E);
  1799. ERR_CONTINUE(!material);
  1800. material_storage->_material_queue_update(material, false, true);
  1801. }
  1802. }
  1803. }
  1804. }
  1805. void MaterialStorage::global_shader_uniform_set_override(const StringName &p_name, const Variant &p_value) {
  1806. if (!global_shader_uniforms.variables.has(p_name)) {
  1807. return; //variable may not exist
  1808. }
  1809. ERR_FAIL_COND(p_value.get_type() == Variant::OBJECT);
  1810. GlobalShaderUniforms::Variable &gv = global_shader_uniforms.variables[p_name];
  1811. gv.override = p_value;
  1812. if (gv.buffer_index >= 0) {
  1813. //buffer
  1814. if (gv.override.get_type() == Variant::NIL) {
  1815. _global_shader_uniform_store_in_buffer(gv.buffer_index, gv.type, gv.value);
  1816. } else {
  1817. _global_shader_uniform_store_in_buffer(gv.buffer_index, gv.type, gv.override);
  1818. }
  1819. _global_shader_uniform_mark_buffer_dirty(gv.buffer_index, gv.buffer_elements);
  1820. } else {
  1821. //texture
  1822. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  1823. for (const RID &E : gv.texture_materials) {
  1824. Material *material = material_storage->get_material(E);
  1825. ERR_CONTINUE(!material);
  1826. material_storage->_material_queue_update(material, false, true);
  1827. }
  1828. }
  1829. }
  1830. Variant MaterialStorage::global_shader_uniform_get(const StringName &p_name) const {
  1831. if (!Engine::get_singleton()->is_editor_hint()) {
  1832. ERR_FAIL_V_MSG(Variant(), "This function should never be used outside the editor, it can severely damage performance.");
  1833. }
  1834. if (!global_shader_uniforms.variables.has(p_name)) {
  1835. return Variant();
  1836. }
  1837. return global_shader_uniforms.variables[p_name].value;
  1838. }
  1839. RS::GlobalShaderUniformType MaterialStorage::global_shader_uniform_get_type_internal(const StringName &p_name) const {
  1840. if (!global_shader_uniforms.variables.has(p_name)) {
  1841. return RS::GLOBAL_VAR_TYPE_MAX;
  1842. }
  1843. return global_shader_uniforms.variables[p_name].type;
  1844. }
  1845. RS::GlobalShaderUniformType MaterialStorage::global_shader_uniform_get_type(const StringName &p_name) const {
  1846. if (!Engine::get_singleton()->is_editor_hint()) {
  1847. ERR_FAIL_V_MSG(RS::GLOBAL_VAR_TYPE_MAX, "This function should never be used outside the editor, it can severely damage performance.");
  1848. }
  1849. return global_shader_uniform_get_type_internal(p_name);
  1850. }
  1851. void MaterialStorage::global_shader_uniforms_load_settings(bool p_load_textures) {
  1852. List<PropertyInfo> settings;
  1853. ProjectSettings::get_singleton()->get_property_list(&settings);
  1854. for (const PropertyInfo &E : settings) {
  1855. if (E.name.begins_with("shader_globals/")) {
  1856. StringName name = E.name.get_slice("/", 1);
  1857. Dictionary d = ProjectSettings::get_singleton()->get(E.name);
  1858. ERR_CONTINUE(!d.has("type"));
  1859. ERR_CONTINUE(!d.has("value"));
  1860. String type = d["type"];
  1861. static const char *global_var_type_names[RS::GLOBAL_VAR_TYPE_MAX] = {
  1862. "bool",
  1863. "bvec2",
  1864. "bvec3",
  1865. "bvec4",
  1866. "int",
  1867. "ivec2",
  1868. "ivec3",
  1869. "ivec4",
  1870. "rect2i",
  1871. "uint",
  1872. "uvec2",
  1873. "uvec3",
  1874. "uvec4",
  1875. "float",
  1876. "vec2",
  1877. "vec3",
  1878. "vec4",
  1879. "color",
  1880. "rect2",
  1881. "mat2",
  1882. "mat3",
  1883. "mat4",
  1884. "transform_2d",
  1885. "transform",
  1886. "sampler2D",
  1887. "sampler2DArray",
  1888. "sampler3D",
  1889. "samplerCube",
  1890. };
  1891. RS::GlobalShaderUniformType gvtype = RS::GLOBAL_VAR_TYPE_MAX;
  1892. for (int i = 0; i < RS::GLOBAL_VAR_TYPE_MAX; i++) {
  1893. if (global_var_type_names[i] == type) {
  1894. gvtype = RS::GlobalShaderUniformType(i);
  1895. break;
  1896. }
  1897. }
  1898. ERR_CONTINUE(gvtype == RS::GLOBAL_VAR_TYPE_MAX); //type invalid
  1899. Variant value = d["value"];
  1900. if (gvtype >= RS::GLOBAL_VAR_TYPE_SAMPLER2D) {
  1901. //textire
  1902. if (!p_load_textures) {
  1903. value = RID();
  1904. continue;
  1905. }
  1906. String path = value;
  1907. Ref<Resource> resource = ResourceLoader::load(path);
  1908. ERR_CONTINUE(resource.is_null());
  1909. value = resource;
  1910. }
  1911. if (global_shader_uniforms.variables.has(name)) {
  1912. //has it, update it
  1913. global_shader_uniform_set(name, value);
  1914. } else {
  1915. global_shader_uniform_add(name, gvtype, value);
  1916. }
  1917. }
  1918. }
  1919. }
  1920. void MaterialStorage::global_shader_uniforms_clear() {
  1921. global_shader_uniforms.variables.clear(); //not right but for now enough
  1922. }
  1923. RID MaterialStorage::global_shader_uniforms_get_storage_buffer() const {
  1924. return global_shader_uniforms.buffer;
  1925. }
  1926. int32_t MaterialStorage::global_shader_uniforms_instance_allocate(RID p_instance) {
  1927. ERR_FAIL_COND_V(global_shader_uniforms.instance_buffer_pos.has(p_instance), -1);
  1928. int32_t pos = _global_shader_uniform_allocate(ShaderLanguage::MAX_INSTANCE_UNIFORM_INDICES);
  1929. global_shader_uniforms.instance_buffer_pos[p_instance] = pos; //save anyway
  1930. ERR_FAIL_COND_V_MSG(pos < 0, -1, "Too many instances using shader instance variables. Increase buffer size in Project Settings.");
  1931. global_shader_uniforms.buffer_usage[pos].elements = ShaderLanguage::MAX_INSTANCE_UNIFORM_INDICES;
  1932. return pos;
  1933. }
  1934. void MaterialStorage::global_shader_uniforms_instance_free(RID p_instance) {
  1935. ERR_FAIL_COND(!global_shader_uniforms.instance_buffer_pos.has(p_instance));
  1936. int32_t pos = global_shader_uniforms.instance_buffer_pos[p_instance];
  1937. if (pos >= 0) {
  1938. global_shader_uniforms.buffer_usage[pos].elements = 0;
  1939. }
  1940. global_shader_uniforms.instance_buffer_pos.erase(p_instance);
  1941. }
  1942. void MaterialStorage::global_shader_uniforms_instance_update(RID p_instance, int p_index, const Variant &p_value) {
  1943. if (!global_shader_uniforms.instance_buffer_pos.has(p_instance)) {
  1944. return; //just not allocated, ignore
  1945. }
  1946. int32_t pos = global_shader_uniforms.instance_buffer_pos[p_instance];
  1947. if (pos < 0) {
  1948. return; //again, not allocated, ignore
  1949. }
  1950. ERR_FAIL_INDEX(p_index, ShaderLanguage::MAX_INSTANCE_UNIFORM_INDICES);
  1951. ERR_FAIL_COND_MSG(p_value.get_type() > Variant::COLOR, "Unsupported variant type for instance parameter: " + Variant::get_type_name(p_value.get_type())); //anything greater not supported
  1952. const ShaderLanguage::DataType datatype_from_value[Variant::COLOR + 1] = {
  1953. ShaderLanguage::TYPE_MAX, //nil
  1954. ShaderLanguage::TYPE_BOOL, //bool
  1955. ShaderLanguage::TYPE_INT, //int
  1956. ShaderLanguage::TYPE_FLOAT, //float
  1957. ShaderLanguage::TYPE_MAX, //string
  1958. ShaderLanguage::TYPE_VEC2, //vec2
  1959. ShaderLanguage::TYPE_IVEC2, //vec2i
  1960. ShaderLanguage::TYPE_VEC4, //rect2
  1961. ShaderLanguage::TYPE_IVEC4, //rect2i
  1962. ShaderLanguage::TYPE_VEC3, // vec3
  1963. ShaderLanguage::TYPE_IVEC3, //vec3i
  1964. ShaderLanguage::TYPE_MAX, //xform2d not supported here
  1965. ShaderLanguage::TYPE_VEC4, //vec4
  1966. ShaderLanguage::TYPE_IVEC4, //vec4i
  1967. ShaderLanguage::TYPE_VEC4, //plane
  1968. ShaderLanguage::TYPE_VEC4, //quat
  1969. ShaderLanguage::TYPE_MAX, //aabb not supported here
  1970. ShaderLanguage::TYPE_MAX, //basis not supported here
  1971. ShaderLanguage::TYPE_MAX, //xform not supported here
  1972. ShaderLanguage::TYPE_MAX, //projection not supported here
  1973. ShaderLanguage::TYPE_VEC4 //color
  1974. };
  1975. ShaderLanguage::DataType datatype = datatype_from_value[p_value.get_type()];
  1976. ERR_FAIL_COND_MSG(datatype == ShaderLanguage::TYPE_MAX, "Unsupported variant type for instance parameter: " + Variant::get_type_name(p_value.get_type())); //anything greater not supported
  1977. pos += p_index;
  1978. _fill_std140_variant_ubo_value(datatype, 0, p_value, (uint8_t *)&global_shader_uniforms.buffer_values[pos], true); //instances always use linear color in this renderer
  1979. _global_shader_uniform_mark_buffer_dirty(pos, 1);
  1980. }
  1981. void MaterialStorage::_update_global_shader_uniforms() {
  1982. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  1983. if (global_shader_uniforms.buffer_dirty_region_count > 0) {
  1984. uint32_t total_regions = global_shader_uniforms.buffer_size / GlobalShaderUniforms::BUFFER_DIRTY_REGION_SIZE;
  1985. if (total_regions / global_shader_uniforms.buffer_dirty_region_count <= 4) {
  1986. // 25% of regions dirty, just update all buffer
  1987. RD::get_singleton()->buffer_update(global_shader_uniforms.buffer, 0, sizeof(GlobalShaderUniforms::Value) * global_shader_uniforms.buffer_size, global_shader_uniforms.buffer_values);
  1988. memset(global_shader_uniforms.buffer_dirty_regions, 0, sizeof(bool) * total_regions);
  1989. } else {
  1990. uint32_t region_byte_size = sizeof(GlobalShaderUniforms::Value) * GlobalShaderUniforms::BUFFER_DIRTY_REGION_SIZE;
  1991. for (uint32_t i = 0; i < total_regions; i++) {
  1992. if (global_shader_uniforms.buffer_dirty_regions[i]) {
  1993. RD::get_singleton()->buffer_update(global_shader_uniforms.buffer, i * region_byte_size, region_byte_size, &global_shader_uniforms.buffer_values[i * GlobalShaderUniforms::BUFFER_DIRTY_REGION_SIZE]);
  1994. global_shader_uniforms.buffer_dirty_regions[i] = false;
  1995. }
  1996. }
  1997. }
  1998. global_shader_uniforms.buffer_dirty_region_count = 0;
  1999. }
  2000. if (global_shader_uniforms.must_update_buffer_materials) {
  2001. // only happens in the case of a buffer variable added or removed,
  2002. // so not often.
  2003. for (const RID &E : global_shader_uniforms.materials_using_buffer) {
  2004. Material *material = material_storage->get_material(E);
  2005. ERR_CONTINUE(!material); //wtf
  2006. material_storage->_material_queue_update(material, true, false);
  2007. }
  2008. global_shader_uniforms.must_update_buffer_materials = false;
  2009. }
  2010. if (global_shader_uniforms.must_update_texture_materials) {
  2011. // only happens in the case of a buffer variable added or removed,
  2012. // so not often.
  2013. for (const RID &E : global_shader_uniforms.materials_using_texture) {
  2014. Material *material = material_storage->get_material(E);
  2015. ERR_CONTINUE(!material); //wtf
  2016. material_storage->_material_queue_update(material, false, true);
  2017. }
  2018. global_shader_uniforms.must_update_texture_materials = false;
  2019. }
  2020. }
  2021. /* SHADER API */
  2022. RID MaterialStorage::shader_allocate() {
  2023. return shader_owner.allocate_rid();
  2024. }
  2025. void MaterialStorage::shader_initialize(RID p_rid) {
  2026. Shader shader;
  2027. shader.data = nullptr;
  2028. shader.type = SHADER_TYPE_MAX;
  2029. shader_owner.initialize_rid(p_rid, shader);
  2030. }
  2031. void MaterialStorage::shader_free(RID p_rid) {
  2032. Shader *shader = shader_owner.get_or_null(p_rid);
  2033. ERR_FAIL_COND(!shader);
  2034. //make material unreference this
  2035. while (shader->owners.size()) {
  2036. material_set_shader((*shader->owners.begin())->self, RID());
  2037. }
  2038. //clear data if exists
  2039. if (shader->data) {
  2040. memdelete(shader->data);
  2041. }
  2042. shader_owner.free(p_rid);
  2043. }
  2044. void MaterialStorage::shader_set_code(RID p_shader, const String &p_code) {
  2045. Shader *shader = shader_owner.get_or_null(p_shader);
  2046. ERR_FAIL_COND(!shader);
  2047. shader->code = p_code;
  2048. String mode_string = ShaderLanguage::get_shader_type(p_code);
  2049. ShaderType new_type;
  2050. if (mode_string == "canvas_item") {
  2051. new_type = SHADER_TYPE_2D;
  2052. } else if (mode_string == "particles") {
  2053. new_type = SHADER_TYPE_PARTICLES;
  2054. } else if (mode_string == "spatial") {
  2055. new_type = SHADER_TYPE_3D;
  2056. } else if (mode_string == "sky") {
  2057. new_type = SHADER_TYPE_SKY;
  2058. } else if (mode_string == "fog") {
  2059. new_type = SHADER_TYPE_FOG;
  2060. } else {
  2061. new_type = SHADER_TYPE_MAX;
  2062. }
  2063. if (new_type != shader->type) {
  2064. if (shader->data) {
  2065. memdelete(shader->data);
  2066. shader->data = nullptr;
  2067. }
  2068. for (Material *E : shader->owners) {
  2069. Material *material = E;
  2070. material->shader_type = new_type;
  2071. if (material->data) {
  2072. memdelete(material->data);
  2073. material->data = nullptr;
  2074. }
  2075. }
  2076. shader->type = new_type;
  2077. if (new_type < SHADER_TYPE_MAX && shader_data_request_func[new_type]) {
  2078. shader->data = shader_data_request_func[new_type]();
  2079. } else {
  2080. shader->type = SHADER_TYPE_MAX; //invalid
  2081. }
  2082. for (Material *E : shader->owners) {
  2083. Material *material = E;
  2084. if (shader->data) {
  2085. material->data = material_get_data_request_function(new_type)(shader->data);
  2086. material->data->self = material->self;
  2087. material->data->set_next_pass(material->next_pass);
  2088. material->data->set_render_priority(material->priority);
  2089. }
  2090. material->shader_type = new_type;
  2091. }
  2092. if (shader->data) {
  2093. for (const KeyValue<StringName, HashMap<int, RID>> &E : shader->default_texture_parameter) {
  2094. for (const KeyValue<int, RID> &E2 : E.value) {
  2095. shader->data->set_default_texture_param(E.key, E2.value, E2.key);
  2096. }
  2097. }
  2098. }
  2099. }
  2100. if (shader->data) {
  2101. shader->data->set_path_hint(shader->path_hint);
  2102. shader->data->set_code(p_code);
  2103. }
  2104. for (Material *E : shader->owners) {
  2105. Material *material = E;
  2106. material->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_MATERIAL);
  2107. _material_queue_update(material, true, true);
  2108. }
  2109. }
  2110. void MaterialStorage::shader_set_path_hint(RID p_shader, const String &p_path) {
  2111. Shader *shader = shader_owner.get_or_null(p_shader);
  2112. ERR_FAIL_COND(!shader);
  2113. shader->path_hint = p_path;
  2114. if (shader->data) {
  2115. shader->data->set_path_hint(p_path);
  2116. }
  2117. }
  2118. String MaterialStorage::shader_get_code(RID p_shader) const {
  2119. Shader *shader = shader_owner.get_or_null(p_shader);
  2120. ERR_FAIL_COND_V(!shader, String());
  2121. return shader->code;
  2122. }
  2123. void MaterialStorage::shader_get_param_list(RID p_shader, List<PropertyInfo> *p_param_list) const {
  2124. Shader *shader = shader_owner.get_or_null(p_shader);
  2125. ERR_FAIL_COND(!shader);
  2126. if (shader->data) {
  2127. return shader->data->get_param_list(p_param_list);
  2128. }
  2129. }
  2130. void MaterialStorage::shader_set_default_texture_param(RID p_shader, const StringName &p_name, RID p_texture, int p_index) {
  2131. Shader *shader = shader_owner.get_or_null(p_shader);
  2132. ERR_FAIL_COND(!shader);
  2133. if (p_texture.is_valid() && TextureStorage::get_singleton()->owns_texture(p_texture)) {
  2134. if (!shader->default_texture_parameter.has(p_name)) {
  2135. shader->default_texture_parameter[p_name] = HashMap<int, RID>();
  2136. }
  2137. shader->default_texture_parameter[p_name][p_index] = p_texture;
  2138. } else {
  2139. if (shader->default_texture_parameter.has(p_name) && shader->default_texture_parameter[p_name].has(p_index)) {
  2140. shader->default_texture_parameter[p_name].erase(p_index);
  2141. if (shader->default_texture_parameter[p_name].is_empty()) {
  2142. shader->default_texture_parameter.erase(p_name);
  2143. }
  2144. }
  2145. }
  2146. if (shader->data) {
  2147. shader->data->set_default_texture_param(p_name, p_texture, p_index);
  2148. }
  2149. for (Material *E : shader->owners) {
  2150. Material *material = E;
  2151. _material_queue_update(material, false, true);
  2152. }
  2153. }
  2154. RID MaterialStorage::shader_get_default_texture_param(RID p_shader, const StringName &p_name, int p_index) const {
  2155. Shader *shader = shader_owner.get_or_null(p_shader);
  2156. ERR_FAIL_COND_V(!shader, RID());
  2157. if (shader->default_texture_parameter.has(p_name) && shader->default_texture_parameter[p_name].has(p_index)) {
  2158. return shader->default_texture_parameter[p_name][p_index];
  2159. }
  2160. return RID();
  2161. }
  2162. Variant MaterialStorage::shader_get_param_default(RID p_shader, const StringName &p_param) const {
  2163. Shader *shader = shader_owner.get_or_null(p_shader);
  2164. ERR_FAIL_COND_V(!shader, Variant());
  2165. if (shader->data) {
  2166. return shader->data->get_default_parameter(p_param);
  2167. }
  2168. return Variant();
  2169. }
  2170. void MaterialStorage::shader_set_data_request_function(ShaderType p_shader_type, ShaderDataRequestFunction p_function) {
  2171. ERR_FAIL_INDEX(p_shader_type, SHADER_TYPE_MAX);
  2172. shader_data_request_func[p_shader_type] = p_function;
  2173. }
  2174. RS::ShaderNativeSourceCode MaterialStorage::shader_get_native_source_code(RID p_shader) const {
  2175. Shader *shader = shader_owner.get_or_null(p_shader);
  2176. ERR_FAIL_COND_V(!shader, RS::ShaderNativeSourceCode());
  2177. if (shader->data) {
  2178. return shader->data->get_native_source_code();
  2179. }
  2180. return RS::ShaderNativeSourceCode();
  2181. }
  2182. /* MATERIAL API */
  2183. void MaterialStorage::_material_uniform_set_erased(void *p_material) {
  2184. RID rid = *(RID *)p_material;
  2185. Material *material = MaterialStorage::get_singleton()->get_material(rid);
  2186. if (material) {
  2187. if (material->data) {
  2188. // Uniform set may be gone because a dependency was erased. This happens
  2189. // if a texture is deleted, so re-create it.
  2190. MaterialStorage::get_singleton()->_material_queue_update(material, false, true);
  2191. }
  2192. material->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_MATERIAL);
  2193. }
  2194. }
  2195. void MaterialStorage::_material_queue_update(Material *material, bool p_uniform, bool p_texture) {
  2196. material->uniform_dirty = material->uniform_dirty || p_uniform;
  2197. material->texture_dirty = material->texture_dirty || p_texture;
  2198. if (material->update_element.in_list()) {
  2199. return;
  2200. }
  2201. material_update_list.add(&material->update_element);
  2202. }
  2203. void MaterialStorage::_update_queued_materials() {
  2204. while (material_update_list.first()) {
  2205. Material *material = material_update_list.first()->self();
  2206. bool uniforms_changed = false;
  2207. if (material->data) {
  2208. uniforms_changed = material->data->update_parameters(material->params, material->uniform_dirty, material->texture_dirty);
  2209. }
  2210. material->texture_dirty = false;
  2211. material->uniform_dirty = false;
  2212. material_update_list.remove(&material->update_element);
  2213. if (uniforms_changed) {
  2214. //some implementations such as 3D renderer cache the matreial uniform set, so update is required
  2215. material->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_MATERIAL);
  2216. }
  2217. }
  2218. }
  2219. RID MaterialStorage::material_allocate() {
  2220. return material_owner.allocate_rid();
  2221. }
  2222. void MaterialStorage::material_initialize(RID p_rid) {
  2223. material_owner.initialize_rid(p_rid);
  2224. Material *material = material_owner.get_or_null(p_rid);
  2225. material->self = p_rid;
  2226. }
  2227. void MaterialStorage::material_free(RID p_rid) {
  2228. Material *material = material_owner.get_or_null(p_rid);
  2229. ERR_FAIL_COND(!material);
  2230. material_set_shader(p_rid, RID()); //clean up shader
  2231. material->dependency.deleted_notify(p_rid);
  2232. material_owner.free(p_rid);
  2233. }
  2234. void MaterialStorage::material_set_shader(RID p_material, RID p_shader) {
  2235. Material *material = material_owner.get_or_null(p_material);
  2236. ERR_FAIL_COND(!material);
  2237. if (material->data) {
  2238. memdelete(material->data);
  2239. material->data = nullptr;
  2240. }
  2241. if (material->shader) {
  2242. material->shader->owners.erase(material);
  2243. material->shader = nullptr;
  2244. material->shader_type = SHADER_TYPE_MAX;
  2245. }
  2246. if (p_shader.is_null()) {
  2247. material->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_MATERIAL);
  2248. material->shader_id = 0;
  2249. return;
  2250. }
  2251. Shader *shader = get_shader(p_shader);
  2252. ERR_FAIL_COND(!shader);
  2253. material->shader = shader;
  2254. material->shader_type = shader->type;
  2255. material->shader_id = p_shader.get_local_index();
  2256. shader->owners.insert(material);
  2257. if (shader->type == SHADER_TYPE_MAX) {
  2258. return;
  2259. }
  2260. ERR_FAIL_COND(shader->data == nullptr);
  2261. material->data = material_data_request_func[shader->type](shader->data);
  2262. material->data->self = p_material;
  2263. material->data->set_next_pass(material->next_pass);
  2264. material->data->set_render_priority(material->priority);
  2265. //updating happens later
  2266. material->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_MATERIAL);
  2267. _material_queue_update(material, true, true);
  2268. }
  2269. void MaterialStorage::material_set_param(RID p_material, const StringName &p_param, const Variant &p_value) {
  2270. Material *material = material_owner.get_or_null(p_material);
  2271. ERR_FAIL_COND(!material);
  2272. if (p_value.get_type() == Variant::NIL) {
  2273. material->params.erase(p_param);
  2274. } else {
  2275. ERR_FAIL_COND(p_value.get_type() == Variant::OBJECT); //object not allowed
  2276. material->params[p_param] = p_value;
  2277. }
  2278. if (material->shader && material->shader->data) { //shader is valid
  2279. bool is_texture = material->shader->data->is_param_texture(p_param);
  2280. _material_queue_update(material, !is_texture, is_texture);
  2281. } else {
  2282. _material_queue_update(material, true, true);
  2283. }
  2284. }
  2285. Variant MaterialStorage::material_get_param(RID p_material, const StringName &p_param) const {
  2286. Material *material = material_owner.get_or_null(p_material);
  2287. ERR_FAIL_COND_V(!material, Variant());
  2288. if (material->params.has(p_param)) {
  2289. return material->params[p_param];
  2290. } else {
  2291. return Variant();
  2292. }
  2293. }
  2294. void MaterialStorage::material_set_next_pass(RID p_material, RID p_next_material) {
  2295. Material *material = material_owner.get_or_null(p_material);
  2296. ERR_FAIL_COND(!material);
  2297. if (material->next_pass == p_next_material) {
  2298. return;
  2299. }
  2300. material->next_pass = p_next_material;
  2301. if (material->data) {
  2302. material->data->set_next_pass(p_next_material);
  2303. }
  2304. material->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_MATERIAL);
  2305. }
  2306. void MaterialStorage::material_set_render_priority(RID p_material, int priority) {
  2307. Material *material = material_owner.get_or_null(p_material);
  2308. ERR_FAIL_COND(!material);
  2309. material->priority = priority;
  2310. if (material->data) {
  2311. material->data->set_render_priority(priority);
  2312. }
  2313. }
  2314. bool MaterialStorage::material_is_animated(RID p_material) {
  2315. Material *material = material_owner.get_or_null(p_material);
  2316. ERR_FAIL_COND_V(!material, false);
  2317. if (material->shader && material->shader->data) {
  2318. if (material->shader->data->is_animated()) {
  2319. return true;
  2320. } else if (material->next_pass.is_valid()) {
  2321. return material_is_animated(material->next_pass);
  2322. }
  2323. }
  2324. return false; //by default nothing is animated
  2325. }
  2326. bool MaterialStorage::material_casts_shadows(RID p_material) {
  2327. Material *material = material_owner.get_or_null(p_material);
  2328. ERR_FAIL_COND_V(!material, true);
  2329. if (material->shader && material->shader->data) {
  2330. if (material->shader->data->casts_shadows()) {
  2331. return true;
  2332. } else if (material->next_pass.is_valid()) {
  2333. return material_casts_shadows(material->next_pass);
  2334. }
  2335. }
  2336. return true; //by default everything casts shadows
  2337. }
  2338. void MaterialStorage::material_get_instance_shader_parameters(RID p_material, List<InstanceShaderParam> *r_parameters) {
  2339. Material *material = material_owner.get_or_null(p_material);
  2340. ERR_FAIL_COND(!material);
  2341. if (material->shader && material->shader->data) {
  2342. material->shader->data->get_instance_param_list(r_parameters);
  2343. if (material->next_pass.is_valid()) {
  2344. material_get_instance_shader_parameters(material->next_pass, r_parameters);
  2345. }
  2346. }
  2347. }
  2348. void MaterialStorage::material_update_dependency(RID p_material, DependencyTracker *p_instance) {
  2349. Material *material = material_owner.get_or_null(p_material);
  2350. ERR_FAIL_COND(!material);
  2351. p_instance->update_dependency(&material->dependency);
  2352. if (material->next_pass.is_valid()) {
  2353. material_update_dependency(material->next_pass, p_instance);
  2354. }
  2355. }
  2356. void MaterialStorage::material_set_data_request_function(ShaderType p_shader_type, MaterialDataRequestFunction p_function) {
  2357. ERR_FAIL_INDEX(p_shader_type, SHADER_TYPE_MAX);
  2358. material_data_request_func[p_shader_type] = p_function;
  2359. }
  2360. MaterialDataRequestFunction MaterialStorage::material_get_data_request_function(ShaderType p_shader_type) {
  2361. ERR_FAIL_INDEX_V(p_shader_type, SHADER_TYPE_MAX, nullptr);
  2362. return material_data_request_func[p_shader_type];
  2363. }