material_storage.cpp 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646
  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].hint == ShaderLanguage::ShaderNode::Uniform::HINT_SOURCE_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_code(p_code);
  2062. }
  2063. for (Material *E : shader->owners) {
  2064. Material *material = E;
  2065. material->dependency.changed_notify(RendererStorage::DEPENDENCY_CHANGED_MATERIAL);
  2066. _material_queue_update(material, true, true);
  2067. }
  2068. }
  2069. String MaterialStorage::shader_get_code(RID p_shader) const {
  2070. Shader *shader = shader_owner.get_or_null(p_shader);
  2071. ERR_FAIL_COND_V(!shader, String());
  2072. return shader->code;
  2073. }
  2074. void MaterialStorage::shader_get_param_list(RID p_shader, List<PropertyInfo> *p_param_list) const {
  2075. Shader *shader = shader_owner.get_or_null(p_shader);
  2076. ERR_FAIL_COND(!shader);
  2077. if (shader->data) {
  2078. return shader->data->get_param_list(p_param_list);
  2079. }
  2080. }
  2081. void MaterialStorage::shader_set_default_texture_param(RID p_shader, const StringName &p_name, RID p_texture, int p_index) {
  2082. Shader *shader = shader_owner.get_or_null(p_shader);
  2083. ERR_FAIL_COND(!shader);
  2084. if (p_texture.is_valid() && TextureStorage::get_singleton()->owns_texture(p_texture)) {
  2085. if (!shader->default_texture_parameter.has(p_name)) {
  2086. shader->default_texture_parameter[p_name] = HashMap<int, RID>();
  2087. }
  2088. shader->default_texture_parameter[p_name][p_index] = p_texture;
  2089. } else {
  2090. if (shader->default_texture_parameter.has(p_name) && shader->default_texture_parameter[p_name].has(p_index)) {
  2091. shader->default_texture_parameter[p_name].erase(p_index);
  2092. if (shader->default_texture_parameter[p_name].is_empty()) {
  2093. shader->default_texture_parameter.erase(p_name);
  2094. }
  2095. }
  2096. }
  2097. if (shader->data) {
  2098. shader->data->set_default_texture_param(p_name, p_texture, p_index);
  2099. }
  2100. for (Material *E : shader->owners) {
  2101. Material *material = E;
  2102. _material_queue_update(material, false, true);
  2103. }
  2104. }
  2105. RID MaterialStorage::shader_get_default_texture_param(RID p_shader, const StringName &p_name, int p_index) const {
  2106. Shader *shader = shader_owner.get_or_null(p_shader);
  2107. ERR_FAIL_COND_V(!shader, RID());
  2108. if (shader->default_texture_parameter.has(p_name) && shader->default_texture_parameter[p_name].has(p_index)) {
  2109. return shader->default_texture_parameter[p_name][p_index];
  2110. }
  2111. return RID();
  2112. }
  2113. Variant MaterialStorage::shader_get_param_default(RID p_shader, const StringName &p_param) const {
  2114. Shader *shader = shader_owner.get_or_null(p_shader);
  2115. ERR_FAIL_COND_V(!shader, Variant());
  2116. if (shader->data) {
  2117. return shader->data->get_default_parameter(p_param);
  2118. }
  2119. return Variant();
  2120. }
  2121. void MaterialStorage::shader_set_data_request_function(ShaderType p_shader_type, ShaderDataRequestFunction p_function) {
  2122. ERR_FAIL_INDEX(p_shader_type, SHADER_TYPE_MAX);
  2123. shader_data_request_func[p_shader_type] = p_function;
  2124. }
  2125. RS::ShaderNativeSourceCode MaterialStorage::shader_get_native_source_code(RID p_shader) const {
  2126. Shader *shader = shader_owner.get_or_null(p_shader);
  2127. ERR_FAIL_COND_V(!shader, RS::ShaderNativeSourceCode());
  2128. if (shader->data) {
  2129. return shader->data->get_native_source_code();
  2130. }
  2131. return RS::ShaderNativeSourceCode();
  2132. }
  2133. /* MATERIAL API */
  2134. void MaterialStorage::_material_uniform_set_erased(void *p_material) {
  2135. RID rid = *(RID *)p_material;
  2136. Material *material = MaterialStorage::get_singleton()->get_material(rid);
  2137. if (material) {
  2138. if (material->data) {
  2139. // Uniform set may be gone because a dependency was erased. This happens
  2140. // if a texture is deleted, so re-create it.
  2141. MaterialStorage::get_singleton()->_material_queue_update(material, false, true);
  2142. }
  2143. material->dependency.changed_notify(RendererStorage::DEPENDENCY_CHANGED_MATERIAL);
  2144. }
  2145. }
  2146. void MaterialStorage::_material_queue_update(Material *material, bool p_uniform, bool p_texture) {
  2147. material->uniform_dirty = material->uniform_dirty || p_uniform;
  2148. material->texture_dirty = material->texture_dirty || p_texture;
  2149. if (material->update_element.in_list()) {
  2150. return;
  2151. }
  2152. material_update_list.add(&material->update_element);
  2153. }
  2154. void MaterialStorage::_update_queued_materials() {
  2155. while (material_update_list.first()) {
  2156. Material *material = material_update_list.first()->self();
  2157. bool uniforms_changed = false;
  2158. if (material->data) {
  2159. uniforms_changed = material->data->update_parameters(material->params, material->uniform_dirty, material->texture_dirty);
  2160. }
  2161. material->texture_dirty = false;
  2162. material->uniform_dirty = false;
  2163. material_update_list.remove(&material->update_element);
  2164. if (uniforms_changed) {
  2165. //some implementations such as 3D renderer cache the matreial uniform set, so update is required
  2166. material->dependency.changed_notify(RendererStorage::DEPENDENCY_CHANGED_MATERIAL);
  2167. }
  2168. }
  2169. }
  2170. RID MaterialStorage::material_allocate() {
  2171. return material_owner.allocate_rid();
  2172. }
  2173. void MaterialStorage::material_initialize(RID p_rid) {
  2174. material_owner.initialize_rid(p_rid);
  2175. Material *material = material_owner.get_or_null(p_rid);
  2176. material->self = p_rid;
  2177. }
  2178. void MaterialStorage::material_free(RID p_rid) {
  2179. Material *material = material_owner.get_or_null(p_rid);
  2180. ERR_FAIL_COND(!material);
  2181. material_set_shader(p_rid, RID()); //clean up shader
  2182. material->dependency.deleted_notify(p_rid);
  2183. material_owner.free(p_rid);
  2184. }
  2185. void MaterialStorage::material_set_shader(RID p_material, RID p_shader) {
  2186. Material *material = material_owner.get_or_null(p_material);
  2187. ERR_FAIL_COND(!material);
  2188. if (material->data) {
  2189. memdelete(material->data);
  2190. material->data = nullptr;
  2191. }
  2192. if (material->shader) {
  2193. material->shader->owners.erase(material);
  2194. material->shader = nullptr;
  2195. material->shader_type = SHADER_TYPE_MAX;
  2196. }
  2197. if (p_shader.is_null()) {
  2198. material->dependency.changed_notify(RendererStorage::DEPENDENCY_CHANGED_MATERIAL);
  2199. material->shader_id = 0;
  2200. return;
  2201. }
  2202. Shader *shader = get_shader(p_shader);
  2203. ERR_FAIL_COND(!shader);
  2204. material->shader = shader;
  2205. material->shader_type = shader->type;
  2206. material->shader_id = p_shader.get_local_index();
  2207. shader->owners.insert(material);
  2208. if (shader->type == SHADER_TYPE_MAX) {
  2209. return;
  2210. }
  2211. ERR_FAIL_COND(shader->data == nullptr);
  2212. material->data = material_data_request_func[shader->type](shader->data);
  2213. material->data->self = p_material;
  2214. material->data->set_next_pass(material->next_pass);
  2215. material->data->set_render_priority(material->priority);
  2216. //updating happens later
  2217. material->dependency.changed_notify(RendererStorage::DEPENDENCY_CHANGED_MATERIAL);
  2218. _material_queue_update(material, true, true);
  2219. }
  2220. void MaterialStorage::material_set_param(RID p_material, const StringName &p_param, const Variant &p_value) {
  2221. Material *material = material_owner.get_or_null(p_material);
  2222. ERR_FAIL_COND(!material);
  2223. if (p_value.get_type() == Variant::NIL) {
  2224. material->params.erase(p_param);
  2225. } else {
  2226. ERR_FAIL_COND(p_value.get_type() == Variant::OBJECT); //object not allowed
  2227. material->params[p_param] = p_value;
  2228. }
  2229. if (material->shader && material->shader->data) { //shader is valid
  2230. bool is_texture = material->shader->data->is_param_texture(p_param);
  2231. _material_queue_update(material, !is_texture, is_texture);
  2232. } else {
  2233. _material_queue_update(material, true, true);
  2234. }
  2235. }
  2236. Variant MaterialStorage::material_get_param(RID p_material, const StringName &p_param) const {
  2237. Material *material = material_owner.get_or_null(p_material);
  2238. ERR_FAIL_COND_V(!material, Variant());
  2239. if (material->params.has(p_param)) {
  2240. return material->params[p_param];
  2241. } else {
  2242. return Variant();
  2243. }
  2244. }
  2245. void MaterialStorage::material_set_next_pass(RID p_material, RID p_next_material) {
  2246. Material *material = material_owner.get_or_null(p_material);
  2247. ERR_FAIL_COND(!material);
  2248. if (material->next_pass == p_next_material) {
  2249. return;
  2250. }
  2251. material->next_pass = p_next_material;
  2252. if (material->data) {
  2253. material->data->set_next_pass(p_next_material);
  2254. }
  2255. material->dependency.changed_notify(RendererStorage::DEPENDENCY_CHANGED_MATERIAL);
  2256. }
  2257. void MaterialStorage::material_set_render_priority(RID p_material, int priority) {
  2258. Material *material = material_owner.get_or_null(p_material);
  2259. ERR_FAIL_COND(!material);
  2260. material->priority = priority;
  2261. if (material->data) {
  2262. material->data->set_render_priority(priority);
  2263. }
  2264. }
  2265. bool MaterialStorage::material_is_animated(RID p_material) {
  2266. Material *material = material_owner.get_or_null(p_material);
  2267. ERR_FAIL_COND_V(!material, false);
  2268. if (material->shader && material->shader->data) {
  2269. if (material->shader->data->is_animated()) {
  2270. return true;
  2271. } else if (material->next_pass.is_valid()) {
  2272. return material_is_animated(material->next_pass);
  2273. }
  2274. }
  2275. return false; //by default nothing is animated
  2276. }
  2277. bool MaterialStorage::material_casts_shadows(RID p_material) {
  2278. Material *material = material_owner.get_or_null(p_material);
  2279. ERR_FAIL_COND_V(!material, true);
  2280. if (material->shader && material->shader->data) {
  2281. if (material->shader->data->casts_shadows()) {
  2282. return true;
  2283. } else if (material->next_pass.is_valid()) {
  2284. return material_casts_shadows(material->next_pass);
  2285. }
  2286. }
  2287. return true; //by default everything casts shadows
  2288. }
  2289. void MaterialStorage::material_get_instance_shader_parameters(RID p_material, List<InstanceShaderParam> *r_parameters) {
  2290. Material *material = material_owner.get_or_null(p_material);
  2291. ERR_FAIL_COND(!material);
  2292. if (material->shader && material->shader->data) {
  2293. material->shader->data->get_instance_param_list(r_parameters);
  2294. if (material->next_pass.is_valid()) {
  2295. material_get_instance_shader_parameters(material->next_pass, r_parameters);
  2296. }
  2297. }
  2298. }
  2299. void MaterialStorage::material_update_dependency(RID p_material, RendererStorage::DependencyTracker *p_instance) {
  2300. Material *material = material_owner.get_or_null(p_material);
  2301. ERR_FAIL_COND(!material);
  2302. p_instance->update_dependency(&material->dependency);
  2303. if (material->next_pass.is_valid()) {
  2304. material_update_dependency(material->next_pass, p_instance);
  2305. }
  2306. }
  2307. void MaterialStorage::material_set_data_request_function(ShaderType p_shader_type, MaterialDataRequestFunction p_function) {
  2308. ERR_FAIL_INDEX(p_shader_type, SHADER_TYPE_MAX);
  2309. material_data_request_func[p_shader_type] = p_function;
  2310. }
  2311. MaterialDataRequestFunction MaterialStorage::material_get_data_request_function(ShaderType p_shader_type) {
  2312. ERR_FAIL_INDEX_V(p_shader_type, SHADER_TYPE_MAX, nullptr);
  2313. return material_data_request_func[p_shader_type];
  2314. }