material_storage.cpp 80 KB

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