material_storage.cpp 88 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863
  1. /**************************************************************************/
  2. /* material_storage.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #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. uint32_t 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. uint32_t 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. uint32_t 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.columns[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[0] = 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. memset(data, 0, 12 * p_array_size);
  816. } break;
  817. case ShaderLanguage::TYPE_BVEC4:
  818. case ShaderLanguage::TYPE_IVEC4:
  819. case ShaderLanguage::TYPE_UVEC4:
  820. case ShaderLanguage::TYPE_VEC4: {
  821. memset(data, 0, 16 * p_array_size);
  822. } break;
  823. case ShaderLanguage::TYPE_MAT2: {
  824. memset(data, 0, 32 * p_array_size);
  825. } break;
  826. case ShaderLanguage::TYPE_MAT3: {
  827. memset(data, 0, 48 * p_array_size);
  828. } break;
  829. case ShaderLanguage::TYPE_MAT4: {
  830. memset(data, 0, 64 * p_array_size);
  831. } break;
  832. default: {
  833. }
  834. }
  835. }
  836. ///////////////////////////////////////////////////////////////////////////
  837. // MaterialStorage::ShaderData
  838. void MaterialStorage::ShaderData::set_path_hint(const String &p_hint) {
  839. path = p_hint;
  840. }
  841. void MaterialStorage::ShaderData::set_default_texture_parameter(const StringName &p_name, RID p_texture, int p_index) {
  842. if (!p_texture.is_valid()) {
  843. if (default_texture_params.has(p_name) && default_texture_params[p_name].has(p_index)) {
  844. default_texture_params[p_name].erase(p_index);
  845. if (default_texture_params[p_name].is_empty()) {
  846. default_texture_params.erase(p_name);
  847. }
  848. }
  849. } else {
  850. if (!default_texture_params.has(p_name)) {
  851. default_texture_params[p_name] = HashMap<int, RID>();
  852. }
  853. default_texture_params[p_name][p_index] = p_texture;
  854. }
  855. }
  856. Variant MaterialStorage::ShaderData::get_default_parameter(const StringName &p_parameter) const {
  857. if (uniforms.has(p_parameter)) {
  858. ShaderLanguage::ShaderNode::Uniform uniform = uniforms[p_parameter];
  859. Vector<ShaderLanguage::ConstantNode::Value> default_value = uniform.default_value;
  860. return ShaderLanguage::constant_value_to_variant(default_value, uniform.type, uniform.array_size, uniform.hint);
  861. }
  862. return Variant();
  863. }
  864. void MaterialStorage::ShaderData::get_shader_uniform_list(List<PropertyInfo> *p_param_list) const {
  865. SortArray<Pair<StringName, int>, ShaderLanguage::UniformOrderComparator> sorter;
  866. LocalVector<Pair<StringName, int>> filtered_uniforms;
  867. for (const KeyValue<StringName, ShaderLanguage::ShaderNode::Uniform> &E : uniforms) {
  868. if (E.value.scope != ShaderLanguage::ShaderNode::Uniform::SCOPE_LOCAL) {
  869. continue;
  870. }
  871. if (E.value.texture_order >= 0) {
  872. filtered_uniforms.push_back(Pair<StringName, int>(E.key, E.value.texture_order + 100000));
  873. } else {
  874. filtered_uniforms.push_back(Pair<StringName, int>(E.key, E.value.order));
  875. }
  876. }
  877. int uniform_count = filtered_uniforms.size();
  878. sorter.sort(filtered_uniforms.ptr(), uniform_count);
  879. String last_group;
  880. for (int i = 0; i < uniform_count; i++) {
  881. const StringName &uniform_name = filtered_uniforms[i].first;
  882. const ShaderLanguage::ShaderNode::Uniform &uniform = uniforms[uniform_name];
  883. String group = uniform.group;
  884. if (!uniform.subgroup.is_empty()) {
  885. group += "::" + uniform.subgroup;
  886. }
  887. if (group != last_group) {
  888. PropertyInfo pi;
  889. pi.usage = PROPERTY_USAGE_GROUP;
  890. pi.name = group;
  891. p_param_list->push_back(pi);
  892. last_group = group;
  893. }
  894. PropertyInfo pi = ShaderLanguage::uniform_to_property_info(uniform);
  895. pi.name = uniform_name;
  896. p_param_list->push_back(pi);
  897. }
  898. }
  899. void MaterialStorage::ShaderData::get_instance_param_list(List<RendererMaterialStorage::InstanceShaderParam> *p_param_list) const {
  900. for (const KeyValue<StringName, ShaderLanguage::ShaderNode::Uniform> &E : uniforms) {
  901. if (E.value.scope != ShaderLanguage::ShaderNode::Uniform::SCOPE_INSTANCE) {
  902. continue;
  903. }
  904. RendererMaterialStorage::InstanceShaderParam p;
  905. p.info = ShaderLanguage::uniform_to_property_info(E.value);
  906. p.info.name = E.key; //supply name
  907. p.index = E.value.instance_index;
  908. p.default_value = ShaderLanguage::constant_value_to_variant(E.value.default_value, E.value.type, E.value.array_size, E.value.hint);
  909. p_param_list->push_back(p);
  910. }
  911. }
  912. bool MaterialStorage::ShaderData::is_parameter_texture(const StringName &p_param) const {
  913. if (!uniforms.has(p_param)) {
  914. return false;
  915. }
  916. return uniforms[p_param].texture_order >= 0;
  917. }
  918. ///////////////////////////////////////////////////////////////////////////
  919. // MaterialStorage::MaterialData
  920. void MaterialStorage::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) {
  921. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  922. bool uses_global_buffer = false;
  923. for (const KeyValue<StringName, ShaderLanguage::ShaderNode::Uniform> &E : p_uniforms) {
  924. if (E.value.order < 0) {
  925. continue; // texture, does not go here
  926. }
  927. if (E.value.scope == ShaderLanguage::ShaderNode::Uniform::SCOPE_INSTANCE) {
  928. continue; //instance uniforms don't appear in the buffer
  929. }
  930. if (E.value.hint == ShaderLanguage::ShaderNode::Uniform::HINT_SCREEN_TEXTURE ||
  931. E.value.hint == ShaderLanguage::ShaderNode::Uniform::HINT_NORMAL_ROUGHNESS_TEXTURE ||
  932. E.value.hint == ShaderLanguage::ShaderNode::Uniform::HINT_DEPTH_TEXTURE) {
  933. continue;
  934. }
  935. if (E.value.scope == ShaderLanguage::ShaderNode::Uniform::SCOPE_GLOBAL) {
  936. //this is a global variable, get the index to it
  937. GlobalShaderUniforms::Variable *gv = material_storage->global_shader_uniforms.variables.getptr(E.key);
  938. uint32_t index = 0;
  939. if (gv) {
  940. index = gv->buffer_index;
  941. } else {
  942. WARN_PRINT("Shader uses global parameter '" + E.key + "', but it was removed at some point. Material will not display correctly.");
  943. }
  944. uint32_t offset = p_uniform_offsets[E.value.order];
  945. uint32_t *intptr = (uint32_t *)&p_buffer[offset];
  946. *intptr = index;
  947. uses_global_buffer = true;
  948. continue;
  949. }
  950. //regular uniform
  951. uint32_t offset = p_uniform_offsets[E.value.order];
  952. #ifdef DEBUG_ENABLED
  953. uint32_t size = 0U;
  954. // The following code enforces a 16-byte alignment of uniform arrays.
  955. if (E.value.array_size > 0) {
  956. size = ShaderLanguage::get_datatype_size(E.value.type) * E.value.array_size;
  957. int m = (16 * E.value.array_size);
  958. if ((size % m) != 0U) {
  959. size += m - (size % m);
  960. }
  961. } else {
  962. size = ShaderLanguage::get_datatype_size(E.value.type);
  963. }
  964. ERR_CONTINUE(offset + size > p_buffer_size);
  965. #endif
  966. uint8_t *data = &p_buffer[offset];
  967. HashMap<StringName, Variant>::ConstIterator V = p_parameters.find(E.key);
  968. if (V) {
  969. //user provided
  970. _fill_std140_variant_ubo_value(E.value.type, E.value.array_size, V->value, data, p_use_linear_color);
  971. } else if (E.value.default_value.size()) {
  972. //default value
  973. _fill_std140_ubo_value(E.value.type, E.value.default_value, data);
  974. //value=E.value.default_value;
  975. } else {
  976. //zero because it was not provided
  977. if ((E.value.type == ShaderLanguage::TYPE_VEC3 || E.value.type == ShaderLanguage::TYPE_VEC4) && E.value.hint == ShaderLanguage::ShaderNode::Uniform::HINT_SOURCE_COLOR) {
  978. //colors must be set as black, with alpha as 1.0
  979. _fill_std140_variant_ubo_value(E.value.type, E.value.array_size, Color(0, 0, 0, 1), data, p_use_linear_color);
  980. } else {
  981. //else just zero it out
  982. _fill_std140_ubo_empty(E.value.type, E.value.array_size, data);
  983. }
  984. }
  985. }
  986. if (uses_global_buffer != (global_buffer_E != nullptr)) {
  987. if (uses_global_buffer) {
  988. global_buffer_E = material_storage->global_shader_uniforms.materials_using_buffer.push_back(self);
  989. } else {
  990. material_storage->global_shader_uniforms.materials_using_buffer.erase(global_buffer_E);
  991. global_buffer_E = nullptr;
  992. }
  993. }
  994. }
  995. MaterialStorage::MaterialData::~MaterialData() {
  996. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  997. if (global_buffer_E) {
  998. //unregister global buffers
  999. material_storage->global_shader_uniforms.materials_using_buffer.erase(global_buffer_E);
  1000. }
  1001. if (global_texture_E) {
  1002. //unregister global textures
  1003. for (const KeyValue<StringName, uint64_t> &E : used_global_textures) {
  1004. GlobalShaderUniforms::Variable *v = material_storage->global_shader_uniforms.variables.getptr(E.key);
  1005. if (v) {
  1006. v->texture_materials.erase(self);
  1007. }
  1008. }
  1009. //unregister material from those using global textures
  1010. material_storage->global_shader_uniforms.materials_using_texture.erase(global_texture_E);
  1011. }
  1012. if (uniform_buffer.is_valid()) {
  1013. RD::get_singleton()->free(uniform_buffer);
  1014. }
  1015. }
  1016. void MaterialStorage::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) {
  1017. TextureStorage *texture_storage = TextureStorage::get_singleton();
  1018. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  1019. #ifdef TOOLS_ENABLED
  1020. TextureStorage::Texture *roughness_detect_texture = nullptr;
  1021. RS::TextureDetectRoughnessChannel roughness_channel = RS::TEXTURE_DETECT_ROUGHNESS_R;
  1022. TextureStorage::Texture *normal_detect_texture = nullptr;
  1023. #endif
  1024. bool uses_global_textures = false;
  1025. global_textures_pass++;
  1026. for (int i = 0, k = 0; i < p_texture_uniforms.size(); i++) {
  1027. const StringName &uniform_name = p_texture_uniforms[i].name;
  1028. int uniform_array_size = p_texture_uniforms[i].array_size;
  1029. Vector<RID> textures;
  1030. if (p_texture_uniforms[i].hint == ShaderLanguage::ShaderNode::Uniform::HINT_SCREEN_TEXTURE ||
  1031. p_texture_uniforms[i].hint == ShaderLanguage::ShaderNode::Uniform::HINT_NORMAL_ROUGHNESS_TEXTURE ||
  1032. p_texture_uniforms[i].hint == ShaderLanguage::ShaderNode::Uniform::HINT_DEPTH_TEXTURE) {
  1033. continue;
  1034. }
  1035. if (p_texture_uniforms[i].global) {
  1036. uses_global_textures = true;
  1037. GlobalShaderUniforms::Variable *v = material_storage->global_shader_uniforms.variables.getptr(uniform_name);
  1038. if (v) {
  1039. if (v->buffer_index >= 0) {
  1040. WARN_PRINT("Shader uses global parameter texture '" + String(uniform_name) + "', but it changed type and is no longer a texture!.");
  1041. } else {
  1042. HashMap<StringName, uint64_t>::Iterator E = used_global_textures.find(uniform_name);
  1043. if (!E) {
  1044. E = used_global_textures.insert(uniform_name, global_textures_pass);
  1045. v->texture_materials.insert(self);
  1046. } else {
  1047. E->value = global_textures_pass;
  1048. }
  1049. textures.push_back(v->override.get_type() != Variant::NIL ? v->override : v->value);
  1050. }
  1051. } else {
  1052. WARN_PRINT("Shader uses global parameter texture '" + String(uniform_name) + "', but it was removed at some point. Material will not display correctly.");
  1053. }
  1054. } else {
  1055. HashMap<StringName, Variant>::ConstIterator V = p_parameters.find(uniform_name);
  1056. if (V) {
  1057. if (V->value.is_array()) {
  1058. Array array = (Array)V->value;
  1059. if (uniform_array_size > 0) {
  1060. for (int j = 0; j < array.size(); j++) {
  1061. textures.push_back(array[j]);
  1062. }
  1063. } else {
  1064. if (array.size() > 0) {
  1065. textures.push_back(array[0]);
  1066. }
  1067. }
  1068. } else {
  1069. textures.push_back(V->value);
  1070. }
  1071. }
  1072. if (uniform_array_size > 0) {
  1073. if (textures.size() < uniform_array_size) {
  1074. HashMap<StringName, HashMap<int, RID>>::ConstIterator W = p_default_textures.find(uniform_name);
  1075. for (int j = textures.size(); j < uniform_array_size; j++) {
  1076. if (W && W->value.has(j)) {
  1077. textures.push_back(W->value[j]);
  1078. } else {
  1079. textures.push_back(RID());
  1080. }
  1081. }
  1082. }
  1083. } else if (textures.is_empty()) {
  1084. HashMap<StringName, HashMap<int, RID>>::ConstIterator W = p_default_textures.find(uniform_name);
  1085. if (W && W->value.has(0)) {
  1086. textures.push_back(W->value[0]);
  1087. }
  1088. }
  1089. }
  1090. RID rd_texture;
  1091. if (textures.is_empty()) {
  1092. //check default usage
  1093. switch (p_texture_uniforms[i].type) {
  1094. case ShaderLanguage::TYPE_ISAMPLER2D:
  1095. case ShaderLanguage::TYPE_USAMPLER2D:
  1096. case ShaderLanguage::TYPE_SAMPLER2D: {
  1097. switch (p_texture_uniforms[i].hint) {
  1098. case ShaderLanguage::ShaderNode::Uniform::HINT_DEFAULT_BLACK: {
  1099. rd_texture = texture_storage->texture_rd_get_default(TextureStorage::DEFAULT_RD_TEXTURE_BLACK);
  1100. } break;
  1101. case ShaderLanguage::ShaderNode::Uniform::HINT_DEFAULT_TRANSPARENT: {
  1102. rd_texture = texture_storage->texture_rd_get_default(TextureStorage::DEFAULT_RD_TEXTURE_TRANSPARENT);
  1103. } break;
  1104. case ShaderLanguage::ShaderNode::Uniform::HINT_ANISOTROPY: {
  1105. rd_texture = texture_storage->texture_rd_get_default(TextureStorage::DEFAULT_RD_TEXTURE_ANISO);
  1106. } break;
  1107. case ShaderLanguage::ShaderNode::Uniform::HINT_NORMAL: {
  1108. rd_texture = texture_storage->texture_rd_get_default(TextureStorage::DEFAULT_RD_TEXTURE_NORMAL);
  1109. } break;
  1110. case ShaderLanguage::ShaderNode::Uniform::HINT_ROUGHNESS_NORMAL: {
  1111. rd_texture = texture_storage->texture_rd_get_default(TextureStorage::DEFAULT_RD_TEXTURE_NORMAL);
  1112. } break;
  1113. default: {
  1114. rd_texture = texture_storage->texture_rd_get_default(TextureStorage::DEFAULT_RD_TEXTURE_WHITE);
  1115. } break;
  1116. }
  1117. } break;
  1118. case ShaderLanguage::TYPE_SAMPLERCUBE: {
  1119. switch (p_texture_uniforms[i].hint) {
  1120. case ShaderLanguage::ShaderNode::Uniform::HINT_DEFAULT_BLACK: {
  1121. rd_texture = texture_storage->texture_rd_get_default(TextureStorage::DEFAULT_RD_TEXTURE_CUBEMAP_BLACK);
  1122. } break;
  1123. default: {
  1124. rd_texture = texture_storage->texture_rd_get_default(TextureStorage::DEFAULT_RD_TEXTURE_CUBEMAP_WHITE);
  1125. } break;
  1126. }
  1127. } break;
  1128. case ShaderLanguage::TYPE_SAMPLERCUBEARRAY: {
  1129. rd_texture = texture_storage->texture_rd_get_default(TextureStorage::DEFAULT_RD_TEXTURE_CUBEMAP_ARRAY_BLACK);
  1130. } break;
  1131. case ShaderLanguage::TYPE_ISAMPLER3D:
  1132. case ShaderLanguage::TYPE_USAMPLER3D:
  1133. case ShaderLanguage::TYPE_SAMPLER3D: {
  1134. rd_texture = texture_storage->texture_rd_get_default(TextureStorage::DEFAULT_RD_TEXTURE_3D_WHITE);
  1135. } break;
  1136. case ShaderLanguage::TYPE_ISAMPLER2DARRAY:
  1137. case ShaderLanguage::TYPE_USAMPLER2DARRAY:
  1138. case ShaderLanguage::TYPE_SAMPLER2DARRAY: {
  1139. rd_texture = texture_storage->texture_rd_get_default(TextureStorage::DEFAULT_RD_TEXTURE_2D_ARRAY_WHITE);
  1140. } break;
  1141. default: {
  1142. }
  1143. }
  1144. #ifdef TOOLS_ENABLED
  1145. if (roughness_detect_texture && normal_detect_texture && !normal_detect_texture->path.is_empty()) {
  1146. roughness_detect_texture->detect_roughness_callback(roughness_detect_texture->detect_roughness_callback_ud, normal_detect_texture->path, roughness_channel);
  1147. }
  1148. #endif
  1149. if (uniform_array_size > 0) {
  1150. for (int j = 0; j < uniform_array_size; j++) {
  1151. p_textures[k++] = rd_texture;
  1152. }
  1153. } else {
  1154. p_textures[k++] = rd_texture;
  1155. }
  1156. } else {
  1157. bool srgb = p_use_linear_color && p_texture_uniforms[i].use_color;
  1158. for (int j = 0; j < textures.size(); j++) {
  1159. TextureStorage::Texture *tex = TextureStorage::get_singleton()->get_texture(textures[j]);
  1160. if (tex) {
  1161. rd_texture = (srgb && tex->rd_texture_srgb.is_valid()) ? tex->rd_texture_srgb : tex->rd_texture;
  1162. #ifdef TOOLS_ENABLED
  1163. if (tex->detect_3d_callback && p_use_linear_color) {
  1164. tex->detect_3d_callback(tex->detect_3d_callback_ud);
  1165. }
  1166. 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)) {
  1167. if (p_texture_uniforms[i].hint == ShaderLanguage::ShaderNode::Uniform::HINT_ROUGHNESS_NORMAL) {
  1168. normal_detect_texture = tex;
  1169. }
  1170. tex->detect_normal_callback(tex->detect_normal_callback_ud);
  1171. }
  1172. 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)) {
  1173. //find the normal texture
  1174. roughness_detect_texture = tex;
  1175. roughness_channel = RS::TextureDetectRoughnessChannel(p_texture_uniforms[i].hint - ShaderLanguage::ShaderNode::Uniform::HINT_ROUGHNESS_R);
  1176. }
  1177. if (tex->render_target) {
  1178. tex->render_target->was_used = true;
  1179. render_target_cache.push_back(tex->render_target);
  1180. }
  1181. #endif
  1182. }
  1183. if (rd_texture.is_null()) {
  1184. rd_texture = texture_storage->texture_rd_get_default(TextureStorage::DEFAULT_RD_TEXTURE_WHITE);
  1185. }
  1186. #ifdef TOOLS_ENABLED
  1187. if (roughness_detect_texture && normal_detect_texture && !normal_detect_texture->path.is_empty()) {
  1188. roughness_detect_texture->detect_roughness_callback(roughness_detect_texture->detect_roughness_callback_ud, normal_detect_texture->path, roughness_channel);
  1189. }
  1190. #endif
  1191. p_textures[k++] = rd_texture;
  1192. }
  1193. }
  1194. }
  1195. {
  1196. //for textures no longer used, unregister them
  1197. List<StringName> to_delete;
  1198. for (KeyValue<StringName, uint64_t> &E : used_global_textures) {
  1199. if (E.value != global_textures_pass) {
  1200. to_delete.push_back(E.key);
  1201. GlobalShaderUniforms::Variable *v = material_storage->global_shader_uniforms.variables.getptr(E.key);
  1202. if (v) {
  1203. v->texture_materials.erase(self);
  1204. }
  1205. }
  1206. }
  1207. while (to_delete.front()) {
  1208. used_global_textures.erase(to_delete.front()->get());
  1209. to_delete.pop_front();
  1210. }
  1211. //handle registering/unregistering global textures
  1212. if (uses_global_textures != (global_texture_E != nullptr)) {
  1213. if (uses_global_textures) {
  1214. global_texture_E = material_storage->global_shader_uniforms.materials_using_texture.push_back(self);
  1215. } else {
  1216. material_storage->global_shader_uniforms.materials_using_texture.erase(global_texture_E);
  1217. global_texture_E = nullptr;
  1218. }
  1219. }
  1220. }
  1221. }
  1222. void MaterialStorage::MaterialData::free_parameters_uniform_set(RID p_uniform_set) {
  1223. if (p_uniform_set.is_valid() && RD::get_singleton()->uniform_set_is_valid(p_uniform_set)) {
  1224. RD::get_singleton()->uniform_set_set_invalidation_callback(p_uniform_set, nullptr, nullptr);
  1225. RD::get_singleton()->free(p_uniform_set);
  1226. }
  1227. }
  1228. bool MaterialStorage::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, bool p_use_linear_color, uint32_t p_barrier) {
  1229. if ((uint32_t)ubo_data.size() != p_ubo_size) {
  1230. p_uniform_dirty = true;
  1231. if (uniform_buffer.is_valid()) {
  1232. RD::get_singleton()->free(uniform_buffer);
  1233. uniform_buffer = RID();
  1234. }
  1235. ubo_data.resize(p_ubo_size);
  1236. if (ubo_data.size()) {
  1237. uniform_buffer = RD::get_singleton()->uniform_buffer_create(ubo_data.size());
  1238. memset(ubo_data.ptrw(), 0, ubo_data.size()); //clear
  1239. }
  1240. //clear previous uniform set
  1241. if (uniform_set.is_valid() && RD::get_singleton()->uniform_set_is_valid(uniform_set)) {
  1242. RD::get_singleton()->uniform_set_set_invalidation_callback(uniform_set, nullptr, nullptr);
  1243. RD::get_singleton()->free(uniform_set);
  1244. uniform_set = RID();
  1245. }
  1246. }
  1247. //check whether buffer changed
  1248. if (p_uniform_dirty && ubo_data.size()) {
  1249. update_uniform_buffer(p_uniforms, p_uniform_offsets, p_parameters, ubo_data.ptrw(), ubo_data.size(), p_use_linear_color);
  1250. RD::get_singleton()->buffer_update(uniform_buffer, 0, ubo_data.size(), ubo_data.ptrw(), p_barrier);
  1251. }
  1252. uint32_t tex_uniform_count = 0U;
  1253. for (int i = 0; i < p_texture_uniforms.size(); i++) {
  1254. tex_uniform_count += uint32_t(p_texture_uniforms[i].array_size > 0 ? p_texture_uniforms[i].array_size : 1);
  1255. }
  1256. if ((uint32_t)texture_cache.size() != tex_uniform_count || p_textures_dirty) {
  1257. texture_cache.resize(tex_uniform_count);
  1258. render_target_cache.clear();
  1259. p_textures_dirty = true;
  1260. //clear previous uniform set
  1261. if (uniform_set.is_valid() && RD::get_singleton()->uniform_set_is_valid(uniform_set)) {
  1262. RD::get_singleton()->uniform_set_set_invalidation_callback(uniform_set, nullptr, nullptr);
  1263. RD::get_singleton()->free(uniform_set);
  1264. uniform_set = RID();
  1265. }
  1266. }
  1267. if (p_textures_dirty && tex_uniform_count) {
  1268. update_textures(p_parameters, p_default_texture_params, p_texture_uniforms, texture_cache.ptrw(), true);
  1269. }
  1270. if (p_ubo_size == 0 && (p_texture_uniforms.size() == 0)) {
  1271. // This material does not require an uniform set, so don't create it.
  1272. return false;
  1273. }
  1274. if (!p_textures_dirty && uniform_set.is_valid() && RD::get_singleton()->uniform_set_is_valid(uniform_set)) {
  1275. //no reason to update uniform set, only UBO (or nothing) was needed to update
  1276. return false;
  1277. }
  1278. Vector<RD::Uniform> uniforms;
  1279. {
  1280. if (p_ubo_size) {
  1281. RD::Uniform u;
  1282. u.uniform_type = RD::UNIFORM_TYPE_UNIFORM_BUFFER;
  1283. u.binding = 0;
  1284. u.append_id(uniform_buffer);
  1285. uniforms.push_back(u);
  1286. }
  1287. const RID *textures = texture_cache.ptrw();
  1288. for (int i = 0, k = 0; i < p_texture_uniforms.size(); i++) {
  1289. const int array_size = p_texture_uniforms[i].array_size;
  1290. RD::Uniform u;
  1291. u.uniform_type = RD::UNIFORM_TYPE_TEXTURE;
  1292. u.binding = 1 + k;
  1293. if (array_size > 0) {
  1294. for (int j = 0; j < array_size; j++) {
  1295. u.append_id(textures[k++]);
  1296. }
  1297. } else {
  1298. u.append_id(textures[k++]);
  1299. }
  1300. uniforms.push_back(u);
  1301. }
  1302. }
  1303. uniform_set = RD::get_singleton()->uniform_set_create(uniforms, p_shader, p_shader_uniform_set);
  1304. RD::get_singleton()->uniform_set_set_invalidation_callback(uniform_set, MaterialStorage::_material_uniform_set_erased, &self);
  1305. return true;
  1306. }
  1307. void MaterialStorage::MaterialData::set_as_used() {
  1308. for (int i = 0; i < render_target_cache.size(); i++) {
  1309. render_target_cache[i]->was_used = true;
  1310. }
  1311. }
  1312. ///////////////////////////////////////////////////////////////////////////
  1313. // MaterialStorage
  1314. MaterialStorage *MaterialStorage::singleton = nullptr;
  1315. MaterialStorage *MaterialStorage::get_singleton() {
  1316. return singleton;
  1317. }
  1318. MaterialStorage::MaterialStorage() {
  1319. singleton = this;
  1320. //default samplers
  1321. for (int i = 1; i < RS::CANVAS_ITEM_TEXTURE_FILTER_MAX; i++) {
  1322. for (int j = 1; j < RS::CANVAS_ITEM_TEXTURE_REPEAT_MAX; j++) {
  1323. RD::SamplerState sampler_state;
  1324. switch (i) {
  1325. case RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST: {
  1326. sampler_state.mag_filter = RD::SAMPLER_FILTER_NEAREST;
  1327. sampler_state.min_filter = RD::SAMPLER_FILTER_NEAREST;
  1328. sampler_state.max_lod = 0;
  1329. } break;
  1330. case RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR: {
  1331. sampler_state.mag_filter = RD::SAMPLER_FILTER_LINEAR;
  1332. sampler_state.min_filter = RD::SAMPLER_FILTER_LINEAR;
  1333. sampler_state.max_lod = 0;
  1334. } break;
  1335. case RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS: {
  1336. sampler_state.mag_filter = RD::SAMPLER_FILTER_NEAREST;
  1337. sampler_state.min_filter = RD::SAMPLER_FILTER_NEAREST;
  1338. if (GLOBAL_GET("rendering/textures/default_filters/use_nearest_mipmap_filter")) {
  1339. sampler_state.mip_filter = RD::SAMPLER_FILTER_NEAREST;
  1340. } else {
  1341. sampler_state.mip_filter = RD::SAMPLER_FILTER_LINEAR;
  1342. }
  1343. } break;
  1344. case RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS: {
  1345. sampler_state.mag_filter = RD::SAMPLER_FILTER_LINEAR;
  1346. sampler_state.min_filter = RD::SAMPLER_FILTER_LINEAR;
  1347. if (GLOBAL_GET("rendering/textures/default_filters/use_nearest_mipmap_filter")) {
  1348. sampler_state.mip_filter = RD::SAMPLER_FILTER_NEAREST;
  1349. } else {
  1350. sampler_state.mip_filter = RD::SAMPLER_FILTER_LINEAR;
  1351. }
  1352. } break;
  1353. case RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS_ANISOTROPIC: {
  1354. sampler_state.mag_filter = RD::SAMPLER_FILTER_NEAREST;
  1355. sampler_state.min_filter = RD::SAMPLER_FILTER_NEAREST;
  1356. if (GLOBAL_GET("rendering/textures/default_filters/use_nearest_mipmap_filter")) {
  1357. sampler_state.mip_filter = RD::SAMPLER_FILTER_NEAREST;
  1358. } else {
  1359. sampler_state.mip_filter = RD::SAMPLER_FILTER_LINEAR;
  1360. }
  1361. sampler_state.use_anisotropy = true;
  1362. sampler_state.anisotropy_max = 1 << int(GLOBAL_GET("rendering/textures/default_filters/anisotropic_filtering_level"));
  1363. } break;
  1364. case RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS_ANISOTROPIC: {
  1365. sampler_state.mag_filter = RD::SAMPLER_FILTER_LINEAR;
  1366. sampler_state.min_filter = RD::SAMPLER_FILTER_LINEAR;
  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.use_anisotropy = true;
  1373. sampler_state.anisotropy_max = 1 << int(GLOBAL_GET("rendering/textures/default_filters/anisotropic_filtering_level"));
  1374. } break;
  1375. default: {
  1376. }
  1377. }
  1378. switch (j) {
  1379. case RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED: {
  1380. sampler_state.repeat_u = RD::SAMPLER_REPEAT_MODE_CLAMP_TO_EDGE;
  1381. sampler_state.repeat_v = RD::SAMPLER_REPEAT_MODE_CLAMP_TO_EDGE;
  1382. sampler_state.repeat_w = RD::SAMPLER_REPEAT_MODE_CLAMP_TO_EDGE;
  1383. } break;
  1384. case RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED: {
  1385. sampler_state.repeat_u = RD::SAMPLER_REPEAT_MODE_REPEAT;
  1386. sampler_state.repeat_v = RD::SAMPLER_REPEAT_MODE_REPEAT;
  1387. sampler_state.repeat_w = RD::SAMPLER_REPEAT_MODE_REPEAT;
  1388. } break;
  1389. case RS::CANVAS_ITEM_TEXTURE_REPEAT_MIRROR: {
  1390. sampler_state.repeat_u = RD::SAMPLER_REPEAT_MODE_MIRRORED_REPEAT;
  1391. sampler_state.repeat_v = RD::SAMPLER_REPEAT_MODE_MIRRORED_REPEAT;
  1392. sampler_state.repeat_w = RD::SAMPLER_REPEAT_MODE_MIRRORED_REPEAT;
  1393. } break;
  1394. default: {
  1395. }
  1396. }
  1397. default_rd_samplers[i][j] = RD::get_singleton()->sampler_create(sampler_state);
  1398. }
  1399. }
  1400. //custom sampler
  1401. sampler_rd_configure_custom(0.0f);
  1402. // buffers
  1403. { //create index array for copy shaders
  1404. Vector<uint8_t> pv;
  1405. pv.resize(6 * 4);
  1406. {
  1407. uint8_t *w = pv.ptrw();
  1408. int *p32 = (int *)w;
  1409. p32[0] = 0;
  1410. p32[1] = 1;
  1411. p32[2] = 2;
  1412. p32[3] = 0;
  1413. p32[4] = 2;
  1414. p32[5] = 3;
  1415. }
  1416. quad_index_buffer = RD::get_singleton()->index_buffer_create(6, RenderingDevice::INDEX_BUFFER_FORMAT_UINT32, pv);
  1417. quad_index_array = RD::get_singleton()->index_array_create(quad_index_buffer, 0, 6);
  1418. }
  1419. // Shaders
  1420. for (int i = 0; i < SHADER_TYPE_MAX; i++) {
  1421. shader_data_request_func[i] = nullptr;
  1422. }
  1423. static_assert(sizeof(GlobalShaderUniforms::Value) == 16);
  1424. global_shader_uniforms.buffer_size = MAX(4096, (int)GLOBAL_GET("rendering/limits/global_shader_variables/buffer_size"));
  1425. global_shader_uniforms.buffer_values = memnew_arr(GlobalShaderUniforms::Value, global_shader_uniforms.buffer_size);
  1426. memset(global_shader_uniforms.buffer_values, 0, sizeof(GlobalShaderUniforms::Value) * global_shader_uniforms.buffer_size);
  1427. global_shader_uniforms.buffer_usage = memnew_arr(GlobalShaderUniforms::ValueUsage, global_shader_uniforms.buffer_size);
  1428. global_shader_uniforms.buffer_dirty_regions = memnew_arr(bool, global_shader_uniforms.buffer_size / GlobalShaderUniforms::BUFFER_DIRTY_REGION_SIZE);
  1429. memset(global_shader_uniforms.buffer_dirty_regions, 0, sizeof(bool) * global_shader_uniforms.buffer_size / GlobalShaderUniforms::BUFFER_DIRTY_REGION_SIZE);
  1430. global_shader_uniforms.buffer = RD::get_singleton()->storage_buffer_create(sizeof(GlobalShaderUniforms::Value) * global_shader_uniforms.buffer_size);
  1431. }
  1432. MaterialStorage::~MaterialStorage() {
  1433. memdelete_arr(global_shader_uniforms.buffer_values);
  1434. memdelete_arr(global_shader_uniforms.buffer_usage);
  1435. memdelete_arr(global_shader_uniforms.buffer_dirty_regions);
  1436. RD::get_singleton()->free(global_shader_uniforms.buffer);
  1437. // buffers
  1438. RD::get_singleton()->free(quad_index_buffer); //array gets freed as dependency
  1439. //def samplers
  1440. for (int i = 1; i < RS::CANVAS_ITEM_TEXTURE_FILTER_MAX; i++) {
  1441. for (int j = 1; j < RS::CANVAS_ITEM_TEXTURE_REPEAT_MAX; j++) {
  1442. RD::get_singleton()->free(default_rd_samplers[i][j]);
  1443. }
  1444. }
  1445. //custom samplers
  1446. for (int i = 1; i < RS::CANVAS_ITEM_TEXTURE_FILTER_MAX; i++) {
  1447. for (int j = 0; j < RS::CANVAS_ITEM_TEXTURE_REPEAT_MAX; j++) {
  1448. if (custom_rd_samplers[i][j].is_valid()) {
  1449. RD::get_singleton()->free(custom_rd_samplers[i][j]);
  1450. }
  1451. }
  1452. }
  1453. singleton = nullptr;
  1454. }
  1455. bool MaterialStorage::free(RID p_rid) {
  1456. if (owns_shader(p_rid)) {
  1457. shader_free(p_rid);
  1458. return true;
  1459. } else if (owns_material(p_rid)) {
  1460. material_free(p_rid);
  1461. return true;
  1462. }
  1463. return false;
  1464. }
  1465. /* Samplers */
  1466. void MaterialStorage::sampler_rd_configure_custom(float p_mipmap_bias) {
  1467. for (int i = 1; i < RS::CANVAS_ITEM_TEXTURE_FILTER_MAX; i++) {
  1468. for (int j = 1; j < RS::CANVAS_ITEM_TEXTURE_REPEAT_MAX; j++) {
  1469. RD::SamplerState sampler_state;
  1470. switch (i) {
  1471. case RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST: {
  1472. sampler_state.mag_filter = RD::SAMPLER_FILTER_NEAREST;
  1473. sampler_state.min_filter = RD::SAMPLER_FILTER_NEAREST;
  1474. sampler_state.max_lod = 0;
  1475. } break;
  1476. case RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR: {
  1477. sampler_state.mag_filter = RD::SAMPLER_FILTER_LINEAR;
  1478. sampler_state.min_filter = RD::SAMPLER_FILTER_LINEAR;
  1479. sampler_state.max_lod = 0;
  1480. } break;
  1481. case RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS: {
  1482. sampler_state.mag_filter = RD::SAMPLER_FILTER_NEAREST;
  1483. sampler_state.min_filter = RD::SAMPLER_FILTER_NEAREST;
  1484. if (GLOBAL_GET("rendering/textures/default_filters/use_nearest_mipmap_filter")) {
  1485. sampler_state.mip_filter = RD::SAMPLER_FILTER_NEAREST;
  1486. } else {
  1487. sampler_state.mip_filter = RD::SAMPLER_FILTER_LINEAR;
  1488. }
  1489. sampler_state.lod_bias = p_mipmap_bias;
  1490. } break;
  1491. case RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS: {
  1492. sampler_state.mag_filter = RD::SAMPLER_FILTER_LINEAR;
  1493. sampler_state.min_filter = RD::SAMPLER_FILTER_LINEAR;
  1494. if (GLOBAL_GET("rendering/textures/default_filters/use_nearest_mipmap_filter")) {
  1495. sampler_state.mip_filter = RD::SAMPLER_FILTER_NEAREST;
  1496. } else {
  1497. sampler_state.mip_filter = RD::SAMPLER_FILTER_LINEAR;
  1498. }
  1499. sampler_state.lod_bias = p_mipmap_bias;
  1500. } break;
  1501. case RS::CANVAS_ITEM_TEXTURE_FILTER_NEAREST_WITH_MIPMAPS_ANISOTROPIC: {
  1502. sampler_state.mag_filter = RD::SAMPLER_FILTER_NEAREST;
  1503. sampler_state.min_filter = RD::SAMPLER_FILTER_NEAREST;
  1504. if (GLOBAL_GET("rendering/textures/default_filters/use_nearest_mipmap_filter")) {
  1505. sampler_state.mip_filter = RD::SAMPLER_FILTER_NEAREST;
  1506. } else {
  1507. sampler_state.mip_filter = RD::SAMPLER_FILTER_LINEAR;
  1508. }
  1509. sampler_state.lod_bias = p_mipmap_bias;
  1510. sampler_state.use_anisotropy = true;
  1511. sampler_state.anisotropy_max = 1 << int(GLOBAL_GET("rendering/textures/default_filters/anisotropic_filtering_level"));
  1512. } break;
  1513. case RS::CANVAS_ITEM_TEXTURE_FILTER_LINEAR_WITH_MIPMAPS_ANISOTROPIC: {
  1514. sampler_state.mag_filter = RD::SAMPLER_FILTER_LINEAR;
  1515. sampler_state.min_filter = RD::SAMPLER_FILTER_LINEAR;
  1516. if (GLOBAL_GET("rendering/textures/default_filters/use_nearest_mipmap_filter")) {
  1517. sampler_state.mip_filter = RD::SAMPLER_FILTER_NEAREST;
  1518. } else {
  1519. sampler_state.mip_filter = RD::SAMPLER_FILTER_LINEAR;
  1520. }
  1521. sampler_state.lod_bias = p_mipmap_bias;
  1522. sampler_state.use_anisotropy = true;
  1523. sampler_state.anisotropy_max = 1 << int(GLOBAL_GET("rendering/textures/default_filters/anisotropic_filtering_level"));
  1524. } break;
  1525. default: {
  1526. }
  1527. }
  1528. switch (j) {
  1529. case RS::CANVAS_ITEM_TEXTURE_REPEAT_DISABLED: {
  1530. sampler_state.repeat_u = RD::SAMPLER_REPEAT_MODE_CLAMP_TO_EDGE;
  1531. sampler_state.repeat_v = RD::SAMPLER_REPEAT_MODE_CLAMP_TO_EDGE;
  1532. sampler_state.repeat_w = RD::SAMPLER_REPEAT_MODE_CLAMP_TO_EDGE;
  1533. } break;
  1534. case RS::CANVAS_ITEM_TEXTURE_REPEAT_ENABLED: {
  1535. sampler_state.repeat_u = RD::SAMPLER_REPEAT_MODE_REPEAT;
  1536. sampler_state.repeat_v = RD::SAMPLER_REPEAT_MODE_REPEAT;
  1537. sampler_state.repeat_w = RD::SAMPLER_REPEAT_MODE_REPEAT;
  1538. } break;
  1539. case RS::CANVAS_ITEM_TEXTURE_REPEAT_MIRROR: {
  1540. sampler_state.repeat_u = RD::SAMPLER_REPEAT_MODE_MIRRORED_REPEAT;
  1541. sampler_state.repeat_v = RD::SAMPLER_REPEAT_MODE_MIRRORED_REPEAT;
  1542. sampler_state.repeat_w = RD::SAMPLER_REPEAT_MODE_MIRRORED_REPEAT;
  1543. } break;
  1544. default: {
  1545. }
  1546. }
  1547. if (custom_rd_samplers[i][j].is_valid()) {
  1548. RD::get_singleton()->free(custom_rd_samplers[i][j]);
  1549. }
  1550. custom_rd_samplers[i][j] = RD::get_singleton()->sampler_create(sampler_state);
  1551. }
  1552. }
  1553. }
  1554. /* GLOBAL SHADER UNIFORM API */
  1555. int32_t MaterialStorage::_global_shader_uniform_allocate(uint32_t p_elements) {
  1556. int32_t idx = 0;
  1557. while (idx + p_elements <= global_shader_uniforms.buffer_size) {
  1558. if (global_shader_uniforms.buffer_usage[idx].elements == 0) {
  1559. bool valid = true;
  1560. for (uint32_t i = 1; i < p_elements; i++) {
  1561. if (global_shader_uniforms.buffer_usage[idx + i].elements > 0) {
  1562. valid = false;
  1563. idx += i + global_shader_uniforms.buffer_usage[idx + i].elements;
  1564. break;
  1565. }
  1566. }
  1567. if (!valid) {
  1568. continue; //if not valid, idx is in new position
  1569. }
  1570. return idx;
  1571. } else {
  1572. idx += global_shader_uniforms.buffer_usage[idx].elements;
  1573. }
  1574. }
  1575. return -1;
  1576. }
  1577. void MaterialStorage::_global_shader_uniform_store_in_buffer(int32_t p_index, RS::GlobalShaderParameterType p_type, const Variant &p_value) {
  1578. switch (p_type) {
  1579. case RS::GLOBAL_VAR_TYPE_BOOL: {
  1580. GlobalShaderUniforms::Value &bv = global_shader_uniforms.buffer_values[p_index];
  1581. bool b = p_value;
  1582. bv.x = b ? 1.0 : 0.0;
  1583. bv.y = 0.0;
  1584. bv.z = 0.0;
  1585. bv.w = 0.0;
  1586. } break;
  1587. case RS::GLOBAL_VAR_TYPE_BVEC2: {
  1588. GlobalShaderUniforms::Value &bv = global_shader_uniforms.buffer_values[p_index];
  1589. uint32_t bvec = p_value;
  1590. bv.x = (bvec & 1) ? 1.0 : 0.0;
  1591. bv.y = (bvec & 2) ? 1.0 : 0.0;
  1592. bv.z = 0.0;
  1593. bv.w = 0.0;
  1594. } break;
  1595. case RS::GLOBAL_VAR_TYPE_BVEC3: {
  1596. GlobalShaderUniforms::Value &bv = global_shader_uniforms.buffer_values[p_index];
  1597. uint32_t bvec = p_value;
  1598. bv.x = (bvec & 1) ? 1.0 : 0.0;
  1599. bv.y = (bvec & 2) ? 1.0 : 0.0;
  1600. bv.z = (bvec & 4) ? 1.0 : 0.0;
  1601. bv.w = 0.0;
  1602. } break;
  1603. case RS::GLOBAL_VAR_TYPE_BVEC4: {
  1604. GlobalShaderUniforms::Value &bv = global_shader_uniforms.buffer_values[p_index];
  1605. uint32_t bvec = p_value;
  1606. bv.x = (bvec & 1) ? 1.0 : 0.0;
  1607. bv.y = (bvec & 2) ? 1.0 : 0.0;
  1608. bv.z = (bvec & 4) ? 1.0 : 0.0;
  1609. bv.w = (bvec & 8) ? 1.0 : 0.0;
  1610. } break;
  1611. case RS::GLOBAL_VAR_TYPE_INT: {
  1612. GlobalShaderUniforms::ValueInt &bv = *(GlobalShaderUniforms::ValueInt *)&global_shader_uniforms.buffer_values[p_index];
  1613. int32_t v = p_value;
  1614. bv.x = v;
  1615. bv.y = 0;
  1616. bv.z = 0;
  1617. bv.w = 0;
  1618. } break;
  1619. case RS::GLOBAL_VAR_TYPE_IVEC2: {
  1620. GlobalShaderUniforms::ValueInt &bv = *(GlobalShaderUniforms::ValueInt *)&global_shader_uniforms.buffer_values[p_index];
  1621. Vector2i v = p_value;
  1622. bv.x = v.x;
  1623. bv.y = v.y;
  1624. bv.z = 0;
  1625. bv.w = 0;
  1626. } break;
  1627. case RS::GLOBAL_VAR_TYPE_IVEC3: {
  1628. GlobalShaderUniforms::ValueInt &bv = *(GlobalShaderUniforms::ValueInt *)&global_shader_uniforms.buffer_values[p_index];
  1629. Vector3i v = p_value;
  1630. bv.x = v.x;
  1631. bv.y = v.y;
  1632. bv.z = v.z;
  1633. bv.w = 0;
  1634. } break;
  1635. case RS::GLOBAL_VAR_TYPE_IVEC4: {
  1636. GlobalShaderUniforms::ValueInt &bv = *(GlobalShaderUniforms::ValueInt *)&global_shader_uniforms.buffer_values[p_index];
  1637. Vector<int32_t> v = p_value;
  1638. bv.x = v.size() >= 1 ? v[0] : 0;
  1639. bv.y = v.size() >= 2 ? v[1] : 0;
  1640. bv.z = v.size() >= 3 ? v[2] : 0;
  1641. bv.w = v.size() >= 4 ? v[3] : 0;
  1642. } break;
  1643. case RS::GLOBAL_VAR_TYPE_RECT2I: {
  1644. GlobalShaderUniforms::ValueInt &bv = *(GlobalShaderUniforms::ValueInt *)&global_shader_uniforms.buffer_values[p_index];
  1645. Rect2i v = p_value;
  1646. bv.x = v.position.x;
  1647. bv.y = v.position.y;
  1648. bv.z = v.size.x;
  1649. bv.w = v.size.y;
  1650. } break;
  1651. case RS::GLOBAL_VAR_TYPE_UINT: {
  1652. GlobalShaderUniforms::ValueUInt &bv = *(GlobalShaderUniforms::ValueUInt *)&global_shader_uniforms.buffer_values[p_index];
  1653. uint32_t v = p_value;
  1654. bv.x = v;
  1655. bv.y = 0;
  1656. bv.z = 0;
  1657. bv.w = 0;
  1658. } break;
  1659. case RS::GLOBAL_VAR_TYPE_UVEC2: {
  1660. GlobalShaderUniforms::ValueUInt &bv = *(GlobalShaderUniforms::ValueUInt *)&global_shader_uniforms.buffer_values[p_index];
  1661. Vector2i v = p_value;
  1662. bv.x = v.x;
  1663. bv.y = v.y;
  1664. bv.z = 0;
  1665. bv.w = 0;
  1666. } break;
  1667. case RS::GLOBAL_VAR_TYPE_UVEC3: {
  1668. GlobalShaderUniforms::ValueUInt &bv = *(GlobalShaderUniforms::ValueUInt *)&global_shader_uniforms.buffer_values[p_index];
  1669. Vector3i v = p_value;
  1670. bv.x = v.x;
  1671. bv.y = v.y;
  1672. bv.z = v.z;
  1673. bv.w = 0;
  1674. } break;
  1675. case RS::GLOBAL_VAR_TYPE_UVEC4: {
  1676. GlobalShaderUniforms::ValueUInt &bv = *(GlobalShaderUniforms::ValueUInt *)&global_shader_uniforms.buffer_values[p_index];
  1677. Vector<int32_t> v = p_value;
  1678. bv.x = v.size() >= 1 ? v[0] : 0;
  1679. bv.y = v.size() >= 2 ? v[1] : 0;
  1680. bv.z = v.size() >= 3 ? v[2] : 0;
  1681. bv.w = v.size() >= 4 ? v[3] : 0;
  1682. } break;
  1683. case RS::GLOBAL_VAR_TYPE_FLOAT: {
  1684. GlobalShaderUniforms::Value &bv = global_shader_uniforms.buffer_values[p_index];
  1685. float v = p_value;
  1686. bv.x = v;
  1687. bv.y = 0;
  1688. bv.z = 0;
  1689. bv.w = 0;
  1690. } break;
  1691. case RS::GLOBAL_VAR_TYPE_VEC2: {
  1692. GlobalShaderUniforms::Value &bv = global_shader_uniforms.buffer_values[p_index];
  1693. Vector2 v = p_value;
  1694. bv.x = v.x;
  1695. bv.y = v.y;
  1696. bv.z = 0;
  1697. bv.w = 0;
  1698. } break;
  1699. case RS::GLOBAL_VAR_TYPE_VEC3: {
  1700. GlobalShaderUniforms::Value &bv = global_shader_uniforms.buffer_values[p_index];
  1701. Vector3 v = p_value;
  1702. bv.x = v.x;
  1703. bv.y = v.y;
  1704. bv.z = v.z;
  1705. bv.w = 0;
  1706. } break;
  1707. case RS::GLOBAL_VAR_TYPE_VEC4: {
  1708. GlobalShaderUniforms::Value &bv = global_shader_uniforms.buffer_values[p_index];
  1709. Plane v = p_value;
  1710. bv.x = v.normal.x;
  1711. bv.y = v.normal.y;
  1712. bv.z = v.normal.z;
  1713. bv.w = v.d;
  1714. } break;
  1715. case RS::GLOBAL_VAR_TYPE_COLOR: {
  1716. GlobalShaderUniforms::Value &bv = global_shader_uniforms.buffer_values[p_index];
  1717. Color v = p_value;
  1718. bv.x = v.r;
  1719. bv.y = v.g;
  1720. bv.z = v.b;
  1721. bv.w = v.a;
  1722. GlobalShaderUniforms::Value &bv_linear = global_shader_uniforms.buffer_values[p_index + 1];
  1723. v = v.srgb_to_linear();
  1724. bv_linear.x = v.r;
  1725. bv_linear.y = v.g;
  1726. bv_linear.z = v.b;
  1727. bv_linear.w = v.a;
  1728. } break;
  1729. case RS::GLOBAL_VAR_TYPE_RECT2: {
  1730. GlobalShaderUniforms::Value &bv = global_shader_uniforms.buffer_values[p_index];
  1731. Rect2 v = p_value;
  1732. bv.x = v.position.x;
  1733. bv.y = v.position.y;
  1734. bv.z = v.size.x;
  1735. bv.w = v.size.y;
  1736. } break;
  1737. case RS::GLOBAL_VAR_TYPE_MAT2: {
  1738. GlobalShaderUniforms::Value *bv = &global_shader_uniforms.buffer_values[p_index];
  1739. Vector<float> m2 = p_value;
  1740. if (m2.size() < 4) {
  1741. m2.resize(4);
  1742. }
  1743. bv[0].x = m2[0];
  1744. bv[0].y = m2[1];
  1745. bv[0].z = 0;
  1746. bv[0].w = 0;
  1747. bv[1].x = m2[2];
  1748. bv[1].y = m2[3];
  1749. bv[1].z = 0;
  1750. bv[1].w = 0;
  1751. } break;
  1752. case RS::GLOBAL_VAR_TYPE_MAT3: {
  1753. GlobalShaderUniforms::Value *bv = &global_shader_uniforms.buffer_values[p_index];
  1754. Basis v = p_value;
  1755. bv[0].x = v.rows[0][0];
  1756. bv[0].y = v.rows[1][0];
  1757. bv[0].z = v.rows[2][0];
  1758. bv[0].w = 0;
  1759. bv[1].x = v.rows[0][1];
  1760. bv[1].y = v.rows[1][1];
  1761. bv[1].z = v.rows[2][1];
  1762. bv[1].w = 0;
  1763. bv[2].x = v.rows[0][2];
  1764. bv[2].y = v.rows[1][2];
  1765. bv[2].z = v.rows[2][2];
  1766. bv[2].w = 0;
  1767. } break;
  1768. case RS::GLOBAL_VAR_TYPE_MAT4: {
  1769. GlobalShaderUniforms::Value *bv = &global_shader_uniforms.buffer_values[p_index];
  1770. Vector<float> m2 = p_value;
  1771. if (m2.size() < 16) {
  1772. m2.resize(16);
  1773. }
  1774. bv[0].x = m2[0];
  1775. bv[0].y = m2[1];
  1776. bv[0].z = m2[2];
  1777. bv[0].w = m2[3];
  1778. bv[1].x = m2[4];
  1779. bv[1].y = m2[5];
  1780. bv[1].z = m2[6];
  1781. bv[1].w = m2[7];
  1782. bv[2].x = m2[8];
  1783. bv[2].y = m2[9];
  1784. bv[2].z = m2[10];
  1785. bv[2].w = m2[11];
  1786. bv[3].x = m2[12];
  1787. bv[3].y = m2[13];
  1788. bv[3].z = m2[14];
  1789. bv[3].w = m2[15];
  1790. } break;
  1791. case RS::GLOBAL_VAR_TYPE_TRANSFORM_2D: {
  1792. GlobalShaderUniforms::Value *bv = &global_shader_uniforms.buffer_values[p_index];
  1793. Transform2D v = p_value;
  1794. bv[0].x = v.columns[0][0];
  1795. bv[0].y = v.columns[0][1];
  1796. bv[0].z = 0;
  1797. bv[0].w = 0;
  1798. bv[1].x = v.columns[1][0];
  1799. bv[1].y = v.columns[1][1];
  1800. bv[1].z = 0;
  1801. bv[1].w = 0;
  1802. bv[2].x = v.columns[2][0];
  1803. bv[2].y = v.columns[2][1];
  1804. bv[2].z = 1;
  1805. bv[2].w = 0;
  1806. } break;
  1807. case RS::GLOBAL_VAR_TYPE_TRANSFORM: {
  1808. GlobalShaderUniforms::Value *bv = &global_shader_uniforms.buffer_values[p_index];
  1809. Transform3D v = p_value;
  1810. bv[0].x = v.basis.rows[0][0];
  1811. bv[0].y = v.basis.rows[1][0];
  1812. bv[0].z = v.basis.rows[2][0];
  1813. bv[0].w = 0;
  1814. bv[1].x = v.basis.rows[0][1];
  1815. bv[1].y = v.basis.rows[1][1];
  1816. bv[1].z = v.basis.rows[2][1];
  1817. bv[1].w = 0;
  1818. bv[2].x = v.basis.rows[0][2];
  1819. bv[2].y = v.basis.rows[1][2];
  1820. bv[2].z = v.basis.rows[2][2];
  1821. bv[2].w = 0;
  1822. bv[3].x = v.origin.x;
  1823. bv[3].y = v.origin.y;
  1824. bv[3].z = v.origin.z;
  1825. bv[3].w = 1;
  1826. } break;
  1827. default: {
  1828. ERR_FAIL();
  1829. }
  1830. }
  1831. }
  1832. void MaterialStorage::_global_shader_uniform_mark_buffer_dirty(int32_t p_index, int32_t p_elements) {
  1833. int32_t prev_chunk = -1;
  1834. for (int32_t i = 0; i < p_elements; i++) {
  1835. int32_t chunk = (p_index + i) / GlobalShaderUniforms::BUFFER_DIRTY_REGION_SIZE;
  1836. if (chunk != prev_chunk) {
  1837. if (!global_shader_uniforms.buffer_dirty_regions[chunk]) {
  1838. global_shader_uniforms.buffer_dirty_regions[chunk] = true;
  1839. global_shader_uniforms.buffer_dirty_region_count++;
  1840. }
  1841. }
  1842. prev_chunk = chunk;
  1843. }
  1844. }
  1845. void MaterialStorage::global_shader_parameter_add(const StringName &p_name, RS::GlobalShaderParameterType p_type, const Variant &p_value) {
  1846. ERR_FAIL_COND(global_shader_uniforms.variables.has(p_name));
  1847. GlobalShaderUniforms::Variable gv;
  1848. gv.type = p_type;
  1849. gv.value = p_value;
  1850. gv.buffer_index = -1;
  1851. if (p_type >= RS::GLOBAL_VAR_TYPE_SAMPLER2D) {
  1852. //is texture
  1853. global_shader_uniforms.must_update_texture_materials = true; //normally there are none
  1854. } else {
  1855. gv.buffer_elements = 1;
  1856. if (p_type == RS::GLOBAL_VAR_TYPE_COLOR || p_type == RS::GLOBAL_VAR_TYPE_MAT2) {
  1857. //color needs to elements to store srgb and linear
  1858. gv.buffer_elements = 2;
  1859. }
  1860. if (p_type == RS::GLOBAL_VAR_TYPE_MAT3 || p_type == RS::GLOBAL_VAR_TYPE_TRANSFORM_2D) {
  1861. //color needs to elements to store srgb and linear
  1862. gv.buffer_elements = 3;
  1863. }
  1864. if (p_type == RS::GLOBAL_VAR_TYPE_MAT4 || p_type == RS::GLOBAL_VAR_TYPE_TRANSFORM) {
  1865. //color needs to elements to store srgb and linear
  1866. gv.buffer_elements = 4;
  1867. }
  1868. //is vector, allocate in buffer and update index
  1869. gv.buffer_index = _global_shader_uniform_allocate(gv.buffer_elements);
  1870. 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)));
  1871. global_shader_uniforms.buffer_usage[gv.buffer_index].elements = gv.buffer_elements;
  1872. _global_shader_uniform_store_in_buffer(gv.buffer_index, gv.type, gv.value);
  1873. _global_shader_uniform_mark_buffer_dirty(gv.buffer_index, gv.buffer_elements);
  1874. global_shader_uniforms.must_update_buffer_materials = true; //normally there are none
  1875. }
  1876. global_shader_uniforms.variables[p_name] = gv;
  1877. }
  1878. void MaterialStorage::global_shader_parameter_remove(const StringName &p_name) {
  1879. if (!global_shader_uniforms.variables.has(p_name)) {
  1880. return;
  1881. }
  1882. const GlobalShaderUniforms::Variable &gv = global_shader_uniforms.variables[p_name];
  1883. if (gv.buffer_index >= 0) {
  1884. global_shader_uniforms.buffer_usage[gv.buffer_index].elements = 0;
  1885. global_shader_uniforms.must_update_buffer_materials = true;
  1886. } else {
  1887. global_shader_uniforms.must_update_texture_materials = true;
  1888. }
  1889. global_shader_uniforms.variables.erase(p_name);
  1890. }
  1891. Vector<StringName> MaterialStorage::global_shader_parameter_get_list() const {
  1892. if (!Engine::get_singleton()->is_editor_hint()) {
  1893. ERR_FAIL_V_MSG(Vector<StringName>(), "This function should never be used outside the editor, it can severely damage performance.");
  1894. }
  1895. Vector<StringName> names;
  1896. for (const KeyValue<StringName, GlobalShaderUniforms::Variable> &E : global_shader_uniforms.variables) {
  1897. names.push_back(E.key);
  1898. }
  1899. names.sort_custom<StringName::AlphCompare>();
  1900. return names;
  1901. }
  1902. void MaterialStorage::global_shader_parameter_set(const StringName &p_name, const Variant &p_value) {
  1903. ERR_FAIL_COND(!global_shader_uniforms.variables.has(p_name));
  1904. GlobalShaderUniforms::Variable &gv = global_shader_uniforms.variables[p_name];
  1905. gv.value = p_value;
  1906. if (gv.override.get_type() == Variant::NIL) {
  1907. if (gv.buffer_index >= 0) {
  1908. //buffer
  1909. _global_shader_uniform_store_in_buffer(gv.buffer_index, gv.type, gv.value);
  1910. _global_shader_uniform_mark_buffer_dirty(gv.buffer_index, gv.buffer_elements);
  1911. } else {
  1912. //texture
  1913. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  1914. for (const RID &E : gv.texture_materials) {
  1915. Material *material = material_storage->get_material(E);
  1916. ERR_CONTINUE(!material);
  1917. material_storage->_material_queue_update(material, false, true);
  1918. }
  1919. }
  1920. }
  1921. }
  1922. void MaterialStorage::global_shader_parameter_set_override(const StringName &p_name, const Variant &p_value) {
  1923. if (!global_shader_uniforms.variables.has(p_name)) {
  1924. return; //variable may not exist
  1925. }
  1926. ERR_FAIL_COND(p_value.get_type() == Variant::OBJECT);
  1927. GlobalShaderUniforms::Variable &gv = global_shader_uniforms.variables[p_name];
  1928. gv.override = p_value;
  1929. if (gv.buffer_index >= 0) {
  1930. //buffer
  1931. if (gv.override.get_type() == Variant::NIL) {
  1932. _global_shader_uniform_store_in_buffer(gv.buffer_index, gv.type, gv.value);
  1933. } else {
  1934. _global_shader_uniform_store_in_buffer(gv.buffer_index, gv.type, gv.override);
  1935. }
  1936. _global_shader_uniform_mark_buffer_dirty(gv.buffer_index, gv.buffer_elements);
  1937. } else {
  1938. //texture
  1939. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  1940. for (const RID &E : gv.texture_materials) {
  1941. Material *material = material_storage->get_material(E);
  1942. ERR_CONTINUE(!material);
  1943. material_storage->_material_queue_update(material, false, true);
  1944. }
  1945. }
  1946. }
  1947. Variant MaterialStorage::global_shader_parameter_get(const StringName &p_name) const {
  1948. if (!Engine::get_singleton()->is_editor_hint()) {
  1949. ERR_FAIL_V_MSG(Variant(), "This function should never be used outside the editor, it can severely damage performance.");
  1950. }
  1951. if (!global_shader_uniforms.variables.has(p_name)) {
  1952. return Variant();
  1953. }
  1954. return global_shader_uniforms.variables[p_name].value;
  1955. }
  1956. RS::GlobalShaderParameterType MaterialStorage::global_shader_parameter_get_type_internal(const StringName &p_name) const {
  1957. if (!global_shader_uniforms.variables.has(p_name)) {
  1958. return RS::GLOBAL_VAR_TYPE_MAX;
  1959. }
  1960. return global_shader_uniforms.variables[p_name].type;
  1961. }
  1962. RS::GlobalShaderParameterType MaterialStorage::global_shader_parameter_get_type(const StringName &p_name) const {
  1963. if (!Engine::get_singleton()->is_editor_hint()) {
  1964. ERR_FAIL_V_MSG(RS::GLOBAL_VAR_TYPE_MAX, "This function should never be used outside the editor, it can severely damage performance.");
  1965. }
  1966. return global_shader_parameter_get_type_internal(p_name);
  1967. }
  1968. void MaterialStorage::global_shader_parameters_load_settings(bool p_load_textures) {
  1969. List<PropertyInfo> settings;
  1970. ProjectSettings::get_singleton()->get_property_list(&settings);
  1971. for (const PropertyInfo &E : settings) {
  1972. if (E.name.begins_with("shader_globals/")) {
  1973. StringName name = E.name.get_slice("/", 1);
  1974. Dictionary d = GLOBAL_GET(E.name);
  1975. ERR_CONTINUE(!d.has("type"));
  1976. ERR_CONTINUE(!d.has("value"));
  1977. String type = d["type"];
  1978. static const char *global_var_type_names[RS::GLOBAL_VAR_TYPE_MAX] = {
  1979. "bool",
  1980. "bvec2",
  1981. "bvec3",
  1982. "bvec4",
  1983. "int",
  1984. "ivec2",
  1985. "ivec3",
  1986. "ivec4",
  1987. "rect2i",
  1988. "uint",
  1989. "uvec2",
  1990. "uvec3",
  1991. "uvec4",
  1992. "float",
  1993. "vec2",
  1994. "vec3",
  1995. "vec4",
  1996. "color",
  1997. "rect2",
  1998. "mat2",
  1999. "mat3",
  2000. "mat4",
  2001. "transform_2d",
  2002. "transform",
  2003. "sampler2D",
  2004. "sampler2DArray",
  2005. "sampler3D",
  2006. "samplerCube",
  2007. };
  2008. RS::GlobalShaderParameterType gvtype = RS::GLOBAL_VAR_TYPE_MAX;
  2009. for (int i = 0; i < RS::GLOBAL_VAR_TYPE_MAX; i++) {
  2010. if (global_var_type_names[i] == type) {
  2011. gvtype = RS::GlobalShaderParameterType(i);
  2012. break;
  2013. }
  2014. }
  2015. ERR_CONTINUE(gvtype == RS::GLOBAL_VAR_TYPE_MAX); //type invalid
  2016. Variant value = d["value"];
  2017. if (gvtype >= RS::GLOBAL_VAR_TYPE_SAMPLER2D) {
  2018. //textire
  2019. if (!p_load_textures) {
  2020. value = RID();
  2021. continue;
  2022. }
  2023. String path = value;
  2024. Ref<Resource> resource = ResourceLoader::load(path);
  2025. ERR_CONTINUE(resource.is_null());
  2026. value = resource;
  2027. }
  2028. if (global_shader_uniforms.variables.has(name)) {
  2029. //has it, update it
  2030. global_shader_parameter_set(name, value);
  2031. } else {
  2032. global_shader_parameter_add(name, gvtype, value);
  2033. }
  2034. }
  2035. }
  2036. }
  2037. void MaterialStorage::global_shader_parameters_clear() {
  2038. global_shader_uniforms.variables.clear(); //not right but for now enough
  2039. }
  2040. RID MaterialStorage::global_shader_uniforms_get_storage_buffer() const {
  2041. return global_shader_uniforms.buffer;
  2042. }
  2043. int32_t MaterialStorage::global_shader_parameters_instance_allocate(RID p_instance) {
  2044. ERR_FAIL_COND_V(global_shader_uniforms.instance_buffer_pos.has(p_instance), -1);
  2045. int32_t pos = _global_shader_uniform_allocate(ShaderLanguage::MAX_INSTANCE_UNIFORM_INDICES);
  2046. global_shader_uniforms.instance_buffer_pos[p_instance] = pos; //save anyway
  2047. ERR_FAIL_COND_V_MSG(pos < 0, -1, "Too many instances using shader instance variables. Increase buffer size in Project Settings.");
  2048. global_shader_uniforms.buffer_usage[pos].elements = ShaderLanguage::MAX_INSTANCE_UNIFORM_INDICES;
  2049. return pos;
  2050. }
  2051. void MaterialStorage::global_shader_parameters_instance_free(RID p_instance) {
  2052. ERR_FAIL_COND(!global_shader_uniforms.instance_buffer_pos.has(p_instance));
  2053. int32_t pos = global_shader_uniforms.instance_buffer_pos[p_instance];
  2054. if (pos >= 0) {
  2055. global_shader_uniforms.buffer_usage[pos].elements = 0;
  2056. }
  2057. global_shader_uniforms.instance_buffer_pos.erase(p_instance);
  2058. }
  2059. void MaterialStorage::global_shader_parameters_instance_update(RID p_instance, int p_index, const Variant &p_value, int p_flags_count) {
  2060. if (!global_shader_uniforms.instance_buffer_pos.has(p_instance)) {
  2061. return; //just not allocated, ignore
  2062. }
  2063. int32_t pos = global_shader_uniforms.instance_buffer_pos[p_instance];
  2064. if (pos < 0) {
  2065. return; //again, not allocated, ignore
  2066. }
  2067. ERR_FAIL_INDEX(p_index, ShaderLanguage::MAX_INSTANCE_UNIFORM_INDICES);
  2068. Variant::Type value_type = p_value.get_type();
  2069. ERR_FAIL_COND_MSG(p_value.get_type() > Variant::COLOR, "Unsupported variant type for instance parameter: " + Variant::get_type_name(value_type)); //anything greater not supported
  2070. const ShaderLanguage::DataType datatype_from_value[Variant::COLOR + 1] = {
  2071. ShaderLanguage::TYPE_MAX, //nil
  2072. ShaderLanguage::TYPE_BOOL, //bool
  2073. ShaderLanguage::TYPE_INT, //int
  2074. ShaderLanguage::TYPE_FLOAT, //float
  2075. ShaderLanguage::TYPE_MAX, //string
  2076. ShaderLanguage::TYPE_VEC2, //vec2
  2077. ShaderLanguage::TYPE_IVEC2, //vec2i
  2078. ShaderLanguage::TYPE_VEC4, //rect2
  2079. ShaderLanguage::TYPE_IVEC4, //rect2i
  2080. ShaderLanguage::TYPE_VEC3, // vec3
  2081. ShaderLanguage::TYPE_IVEC3, //vec3i
  2082. ShaderLanguage::TYPE_MAX, //xform2d not supported here
  2083. ShaderLanguage::TYPE_VEC4, //vec4
  2084. ShaderLanguage::TYPE_IVEC4, //vec4i
  2085. ShaderLanguage::TYPE_VEC4, //plane
  2086. ShaderLanguage::TYPE_VEC4, //quat
  2087. ShaderLanguage::TYPE_MAX, //aabb not supported here
  2088. ShaderLanguage::TYPE_MAX, //basis not supported here
  2089. ShaderLanguage::TYPE_MAX, //xform not supported here
  2090. ShaderLanguage::TYPE_MAX, //projection not supported here
  2091. ShaderLanguage::TYPE_VEC4 //color
  2092. };
  2093. ShaderLanguage::DataType datatype = ShaderLanguage::TYPE_MAX;
  2094. if (value_type == Variant::INT && p_flags_count > 0) {
  2095. switch (p_flags_count) {
  2096. case 1:
  2097. datatype = ShaderLanguage::TYPE_BVEC2;
  2098. break;
  2099. case 2:
  2100. datatype = ShaderLanguage::TYPE_BVEC3;
  2101. break;
  2102. case 3:
  2103. datatype = ShaderLanguage::TYPE_BVEC4;
  2104. break;
  2105. }
  2106. } else {
  2107. datatype = datatype_from_value[value_type];
  2108. }
  2109. ERR_FAIL_COND_MSG(datatype == ShaderLanguage::TYPE_MAX, "Unsupported variant type for instance parameter: " + Variant::get_type_name(value_type)); //anything greater not supported
  2110. pos += p_index;
  2111. _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
  2112. _global_shader_uniform_mark_buffer_dirty(pos, 1);
  2113. }
  2114. void MaterialStorage::_update_global_shader_uniforms() {
  2115. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  2116. if (global_shader_uniforms.buffer_dirty_region_count > 0) {
  2117. uint32_t total_regions = global_shader_uniforms.buffer_size / GlobalShaderUniforms::BUFFER_DIRTY_REGION_SIZE;
  2118. if (total_regions / global_shader_uniforms.buffer_dirty_region_count <= 4) {
  2119. // 25% of regions dirty, just update all buffer
  2120. RD::get_singleton()->buffer_update(global_shader_uniforms.buffer, 0, sizeof(GlobalShaderUniforms::Value) * global_shader_uniforms.buffer_size, global_shader_uniforms.buffer_values);
  2121. memset(global_shader_uniforms.buffer_dirty_regions, 0, sizeof(bool) * total_regions);
  2122. } else {
  2123. uint32_t region_byte_size = sizeof(GlobalShaderUniforms::Value) * GlobalShaderUniforms::BUFFER_DIRTY_REGION_SIZE;
  2124. for (uint32_t i = 0; i < total_regions; i++) {
  2125. if (global_shader_uniforms.buffer_dirty_regions[i]) {
  2126. 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]);
  2127. global_shader_uniforms.buffer_dirty_regions[i] = false;
  2128. }
  2129. }
  2130. }
  2131. global_shader_uniforms.buffer_dirty_region_count = 0;
  2132. }
  2133. if (global_shader_uniforms.must_update_buffer_materials) {
  2134. // only happens in the case of a buffer variable added or removed,
  2135. // so not often.
  2136. for (const RID &E : global_shader_uniforms.materials_using_buffer) {
  2137. Material *material = material_storage->get_material(E);
  2138. ERR_CONTINUE(!material); //wtf
  2139. material_storage->_material_queue_update(material, true, false);
  2140. }
  2141. global_shader_uniforms.must_update_buffer_materials = false;
  2142. }
  2143. if (global_shader_uniforms.must_update_texture_materials) {
  2144. // only happens in the case of a buffer variable added or removed,
  2145. // so not often.
  2146. for (const RID &E : global_shader_uniforms.materials_using_texture) {
  2147. Material *material = material_storage->get_material(E);
  2148. ERR_CONTINUE(!material); //wtf
  2149. material_storage->_material_queue_update(material, false, true);
  2150. }
  2151. global_shader_uniforms.must_update_texture_materials = false;
  2152. }
  2153. }
  2154. /* SHADER API */
  2155. RID MaterialStorage::shader_allocate() {
  2156. return shader_owner.allocate_rid();
  2157. }
  2158. void MaterialStorage::shader_initialize(RID p_rid) {
  2159. Shader shader;
  2160. shader.data = nullptr;
  2161. shader.type = SHADER_TYPE_MAX;
  2162. shader_owner.initialize_rid(p_rid, shader);
  2163. }
  2164. void MaterialStorage::shader_free(RID p_rid) {
  2165. Shader *shader = shader_owner.get_or_null(p_rid);
  2166. ERR_FAIL_COND(!shader);
  2167. //make material unreference this
  2168. while (shader->owners.size()) {
  2169. material_set_shader((*shader->owners.begin())->self, RID());
  2170. }
  2171. //clear data if exists
  2172. if (shader->data) {
  2173. memdelete(shader->data);
  2174. }
  2175. shader_owner.free(p_rid);
  2176. }
  2177. void MaterialStorage::shader_set_code(RID p_shader, const String &p_code) {
  2178. Shader *shader = shader_owner.get_or_null(p_shader);
  2179. ERR_FAIL_COND(!shader);
  2180. shader->code = p_code;
  2181. String mode_string = ShaderLanguage::get_shader_type(p_code);
  2182. ShaderType new_type;
  2183. if (mode_string == "canvas_item") {
  2184. new_type = SHADER_TYPE_2D;
  2185. } else if (mode_string == "particles") {
  2186. new_type = SHADER_TYPE_PARTICLES;
  2187. } else if (mode_string == "spatial") {
  2188. new_type = SHADER_TYPE_3D;
  2189. } else if (mode_string == "sky") {
  2190. new_type = SHADER_TYPE_SKY;
  2191. } else if (mode_string == "fog") {
  2192. new_type = SHADER_TYPE_FOG;
  2193. } else {
  2194. new_type = SHADER_TYPE_MAX;
  2195. }
  2196. if (new_type != shader->type) {
  2197. if (shader->data) {
  2198. memdelete(shader->data);
  2199. shader->data = nullptr;
  2200. }
  2201. for (Material *E : shader->owners) {
  2202. Material *material = E;
  2203. material->shader_type = new_type;
  2204. if (material->data) {
  2205. memdelete(material->data);
  2206. material->data = nullptr;
  2207. }
  2208. }
  2209. shader->type = new_type;
  2210. if (new_type < SHADER_TYPE_MAX && shader_data_request_func[new_type]) {
  2211. shader->data = shader_data_request_func[new_type]();
  2212. } else {
  2213. shader->type = SHADER_TYPE_MAX; //invalid
  2214. }
  2215. for (Material *E : shader->owners) {
  2216. Material *material = E;
  2217. if (shader->data) {
  2218. material->data = material_get_data_request_function(new_type)(shader->data);
  2219. material->data->self = material->self;
  2220. material->data->set_next_pass(material->next_pass);
  2221. material->data->set_render_priority(material->priority);
  2222. }
  2223. material->shader_type = new_type;
  2224. }
  2225. if (shader->data) {
  2226. for (const KeyValue<StringName, HashMap<int, RID>> &E : shader->default_texture_parameter) {
  2227. for (const KeyValue<int, RID> &E2 : E.value) {
  2228. shader->data->set_default_texture_parameter(E.key, E2.value, E2.key);
  2229. }
  2230. }
  2231. }
  2232. }
  2233. if (shader->data) {
  2234. shader->data->set_path_hint(shader->path_hint);
  2235. shader->data->set_code(p_code);
  2236. }
  2237. for (Material *E : shader->owners) {
  2238. Material *material = E;
  2239. material->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_MATERIAL);
  2240. _material_queue_update(material, true, true);
  2241. }
  2242. }
  2243. void MaterialStorage::shader_set_path_hint(RID p_shader, const String &p_path) {
  2244. Shader *shader = shader_owner.get_or_null(p_shader);
  2245. ERR_FAIL_COND(!shader);
  2246. shader->path_hint = p_path;
  2247. if (shader->data) {
  2248. shader->data->set_path_hint(p_path);
  2249. }
  2250. }
  2251. String MaterialStorage::shader_get_code(RID p_shader) const {
  2252. Shader *shader = shader_owner.get_or_null(p_shader);
  2253. ERR_FAIL_COND_V(!shader, String());
  2254. return shader->code;
  2255. }
  2256. void MaterialStorage::get_shader_parameter_list(RID p_shader, List<PropertyInfo> *p_param_list) const {
  2257. Shader *shader = shader_owner.get_or_null(p_shader);
  2258. ERR_FAIL_COND(!shader);
  2259. if (shader->data) {
  2260. return shader->data->get_shader_uniform_list(p_param_list);
  2261. }
  2262. }
  2263. void MaterialStorage::shader_set_default_texture_parameter(RID p_shader, const StringName &p_name, RID p_texture, int p_index) {
  2264. Shader *shader = shader_owner.get_or_null(p_shader);
  2265. ERR_FAIL_COND(!shader);
  2266. if (p_texture.is_valid() && TextureStorage::get_singleton()->owns_texture(p_texture)) {
  2267. if (!shader->default_texture_parameter.has(p_name)) {
  2268. shader->default_texture_parameter[p_name] = HashMap<int, RID>();
  2269. }
  2270. shader->default_texture_parameter[p_name][p_index] = p_texture;
  2271. } else {
  2272. if (shader->default_texture_parameter.has(p_name) && shader->default_texture_parameter[p_name].has(p_index)) {
  2273. shader->default_texture_parameter[p_name].erase(p_index);
  2274. if (shader->default_texture_parameter[p_name].is_empty()) {
  2275. shader->default_texture_parameter.erase(p_name);
  2276. }
  2277. }
  2278. }
  2279. if (shader->data) {
  2280. shader->data->set_default_texture_parameter(p_name, p_texture, p_index);
  2281. }
  2282. for (Material *E : shader->owners) {
  2283. Material *material = E;
  2284. _material_queue_update(material, false, true);
  2285. }
  2286. }
  2287. RID MaterialStorage::shader_get_default_texture_parameter(RID p_shader, const StringName &p_name, int p_index) const {
  2288. Shader *shader = shader_owner.get_or_null(p_shader);
  2289. ERR_FAIL_COND_V(!shader, RID());
  2290. if (shader->default_texture_parameter.has(p_name) && shader->default_texture_parameter[p_name].has(p_index)) {
  2291. return shader->default_texture_parameter[p_name][p_index];
  2292. }
  2293. return RID();
  2294. }
  2295. Variant MaterialStorage::shader_get_parameter_default(RID p_shader, const StringName &p_param) const {
  2296. Shader *shader = shader_owner.get_or_null(p_shader);
  2297. ERR_FAIL_COND_V(!shader, Variant());
  2298. if (shader->data) {
  2299. return shader->data->get_default_parameter(p_param);
  2300. }
  2301. return Variant();
  2302. }
  2303. void MaterialStorage::shader_set_data_request_function(ShaderType p_shader_type, ShaderDataRequestFunction p_function) {
  2304. ERR_FAIL_INDEX(p_shader_type, SHADER_TYPE_MAX);
  2305. shader_data_request_func[p_shader_type] = p_function;
  2306. }
  2307. RS::ShaderNativeSourceCode MaterialStorage::shader_get_native_source_code(RID p_shader) const {
  2308. Shader *shader = shader_owner.get_or_null(p_shader);
  2309. ERR_FAIL_COND_V(!shader, RS::ShaderNativeSourceCode());
  2310. if (shader->data) {
  2311. return shader->data->get_native_source_code();
  2312. }
  2313. return RS::ShaderNativeSourceCode();
  2314. }
  2315. /* MATERIAL API */
  2316. void MaterialStorage::_material_uniform_set_erased(void *p_material) {
  2317. RID rid = *(RID *)p_material;
  2318. Material *material = MaterialStorage::get_singleton()->get_material(rid);
  2319. if (material) {
  2320. if (material->data) {
  2321. // Uniform set may be gone because a dependency was erased. This happens
  2322. // if a texture is deleted, so re-create it.
  2323. MaterialStorage::get_singleton()->_material_queue_update(material, false, true);
  2324. }
  2325. material->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_MATERIAL);
  2326. }
  2327. }
  2328. void MaterialStorage::_material_queue_update(Material *material, bool p_uniform, bool p_texture) {
  2329. material->uniform_dirty = material->uniform_dirty || p_uniform;
  2330. material->texture_dirty = material->texture_dirty || p_texture;
  2331. if (material->update_element.in_list()) {
  2332. return;
  2333. }
  2334. material_update_list.add(&material->update_element);
  2335. }
  2336. void MaterialStorage::_update_queued_materials() {
  2337. while (material_update_list.first()) {
  2338. Material *material = material_update_list.first()->self();
  2339. bool uniforms_changed = false;
  2340. if (material->data) {
  2341. uniforms_changed = material->data->update_parameters(material->params, material->uniform_dirty, material->texture_dirty);
  2342. }
  2343. material->texture_dirty = false;
  2344. material->uniform_dirty = false;
  2345. material_update_list.remove(&material->update_element);
  2346. if (uniforms_changed) {
  2347. //some implementations such as 3D renderer cache the material uniform set, so update is required
  2348. material->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_MATERIAL);
  2349. }
  2350. }
  2351. }
  2352. RID MaterialStorage::material_allocate() {
  2353. return material_owner.allocate_rid();
  2354. }
  2355. void MaterialStorage::material_initialize(RID p_rid) {
  2356. material_owner.initialize_rid(p_rid);
  2357. Material *material = material_owner.get_or_null(p_rid);
  2358. material->self = p_rid;
  2359. }
  2360. void MaterialStorage::material_free(RID p_rid) {
  2361. Material *material = material_owner.get_or_null(p_rid);
  2362. ERR_FAIL_COND(!material);
  2363. // Need to clear texture arrays to prevent spin locking of their RID's.
  2364. // This happens when the app is being closed.
  2365. for (KeyValue<StringName, Variant> &E : material->params) {
  2366. if (E.value.get_type() == Variant::ARRAY) {
  2367. Array(E.value).clear();
  2368. }
  2369. }
  2370. material_set_shader(p_rid, RID()); //clean up shader
  2371. material->dependency.deleted_notify(p_rid);
  2372. material_owner.free(p_rid);
  2373. }
  2374. void MaterialStorage::material_set_shader(RID p_material, RID p_shader) {
  2375. Material *material = material_owner.get_or_null(p_material);
  2376. ERR_FAIL_COND(!material);
  2377. if (material->data) {
  2378. memdelete(material->data);
  2379. material->data = nullptr;
  2380. }
  2381. if (material->shader) {
  2382. material->shader->owners.erase(material);
  2383. material->shader = nullptr;
  2384. material->shader_type = SHADER_TYPE_MAX;
  2385. }
  2386. if (p_shader.is_null()) {
  2387. material->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_MATERIAL);
  2388. material->shader_id = 0;
  2389. return;
  2390. }
  2391. Shader *shader = get_shader(p_shader);
  2392. ERR_FAIL_COND(!shader);
  2393. material->shader = shader;
  2394. material->shader_type = shader->type;
  2395. material->shader_id = p_shader.get_local_index();
  2396. shader->owners.insert(material);
  2397. if (shader->type == SHADER_TYPE_MAX) {
  2398. return;
  2399. }
  2400. ERR_FAIL_COND(shader->data == nullptr);
  2401. material->data = material_data_request_func[shader->type](shader->data);
  2402. material->data->self = p_material;
  2403. material->data->set_next_pass(material->next_pass);
  2404. material->data->set_render_priority(material->priority);
  2405. //updating happens later
  2406. material->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_MATERIAL);
  2407. _material_queue_update(material, true, true);
  2408. }
  2409. MaterialStorage::ShaderData *MaterialStorage::material_get_shader_data(RID p_material) {
  2410. const MaterialStorage::Material *material = MaterialStorage::get_singleton()->get_material(p_material);
  2411. if (material && material->shader && material->shader->data) {
  2412. return material->shader->data;
  2413. }
  2414. return nullptr;
  2415. }
  2416. void MaterialStorage::material_set_param(RID p_material, const StringName &p_param, const Variant &p_value) {
  2417. Material *material = material_owner.get_or_null(p_material);
  2418. ERR_FAIL_COND(!material);
  2419. if (p_value.get_type() == Variant::NIL) {
  2420. material->params.erase(p_param);
  2421. } else {
  2422. ERR_FAIL_COND(p_value.get_type() == Variant::OBJECT); //object not allowed
  2423. material->params[p_param] = p_value;
  2424. }
  2425. if (material->shader && material->shader->data) { //shader is valid
  2426. bool is_texture = material->shader->data->is_parameter_texture(p_param);
  2427. _material_queue_update(material, !is_texture, is_texture);
  2428. } else {
  2429. _material_queue_update(material, true, true);
  2430. }
  2431. }
  2432. Variant MaterialStorage::material_get_param(RID p_material, const StringName &p_param) const {
  2433. Material *material = material_owner.get_or_null(p_material);
  2434. ERR_FAIL_COND_V(!material, Variant());
  2435. if (material->params.has(p_param)) {
  2436. return material->params[p_param];
  2437. } else {
  2438. return Variant();
  2439. }
  2440. }
  2441. void MaterialStorage::material_set_next_pass(RID p_material, RID p_next_material) {
  2442. Material *material = material_owner.get_or_null(p_material);
  2443. ERR_FAIL_COND(!material);
  2444. if (material->next_pass == p_next_material) {
  2445. return;
  2446. }
  2447. material->next_pass = p_next_material;
  2448. if (material->data) {
  2449. material->data->set_next_pass(p_next_material);
  2450. }
  2451. material->dependency.changed_notify(Dependency::DEPENDENCY_CHANGED_MATERIAL);
  2452. }
  2453. void MaterialStorage::material_set_render_priority(RID p_material, int priority) {
  2454. Material *material = material_owner.get_or_null(p_material);
  2455. ERR_FAIL_COND(!material);
  2456. material->priority = priority;
  2457. if (material->data) {
  2458. material->data->set_render_priority(priority);
  2459. }
  2460. }
  2461. bool MaterialStorage::material_is_animated(RID p_material) {
  2462. Material *material = material_owner.get_or_null(p_material);
  2463. ERR_FAIL_COND_V(!material, false);
  2464. if (material->shader && material->shader->data) {
  2465. if (material->shader->data->is_animated()) {
  2466. return true;
  2467. } else if (material->next_pass.is_valid()) {
  2468. return material_is_animated(material->next_pass);
  2469. }
  2470. }
  2471. return false; //by default nothing is animated
  2472. }
  2473. bool MaterialStorage::material_casts_shadows(RID p_material) {
  2474. Material *material = material_owner.get_or_null(p_material);
  2475. ERR_FAIL_COND_V(!material, true);
  2476. if (material->shader && material->shader->data) {
  2477. if (material->shader->data->casts_shadows()) {
  2478. return true;
  2479. } else if (material->next_pass.is_valid()) {
  2480. return material_casts_shadows(material->next_pass);
  2481. }
  2482. }
  2483. return true; //by default everything casts shadows
  2484. }
  2485. void MaterialStorage::material_get_instance_shader_parameters(RID p_material, List<InstanceShaderParam> *r_parameters) {
  2486. Material *material = material_owner.get_or_null(p_material);
  2487. ERR_FAIL_COND(!material);
  2488. if (material->shader && material->shader->data) {
  2489. material->shader->data->get_instance_param_list(r_parameters);
  2490. if (material->next_pass.is_valid()) {
  2491. material_get_instance_shader_parameters(material->next_pass, r_parameters);
  2492. }
  2493. }
  2494. }
  2495. void MaterialStorage::material_update_dependency(RID p_material, DependencyTracker *p_instance) {
  2496. Material *material = material_owner.get_or_null(p_material);
  2497. ERR_FAIL_COND(!material);
  2498. p_instance->update_dependency(&material->dependency);
  2499. if (material->next_pass.is_valid()) {
  2500. material_update_dependency(material->next_pass, p_instance);
  2501. }
  2502. }
  2503. void MaterialStorage::material_set_data_request_function(ShaderType p_shader_type, MaterialStorage::MaterialDataRequestFunction p_function) {
  2504. ERR_FAIL_INDEX(p_shader_type, SHADER_TYPE_MAX);
  2505. material_data_request_func[p_shader_type] = p_function;
  2506. }
  2507. MaterialStorage::MaterialDataRequestFunction MaterialStorage::material_get_data_request_function(ShaderType p_shader_type) {
  2508. ERR_FAIL_INDEX_V(p_shader_type, SHADER_TYPE_MAX, nullptr);
  2509. return material_data_request_func[p_shader_type];
  2510. }