material_storage.cpp 96 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951
  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. #ifdef GLES3_ENABLED
  31. #include "core/config/project_settings.h"
  32. #include "config.h"
  33. #include "material_storage.h"
  34. #include "texture_storage.h"
  35. #include "drivers/gles3/rasterizer_canvas_gles3.h"
  36. using namespace GLES3;
  37. ///////////////////////////////////////////////////////////////////////////
  38. // UBI helper functions
  39. _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) {
  40. switch (type) {
  41. case ShaderLanguage::TYPE_BOOL: {
  42. uint32_t *gui = (uint32_t *)data;
  43. if (p_array_size > 0) {
  44. const PackedInt32Array &ba = value;
  45. int s = ba.size();
  46. const int *r = ba.ptr();
  47. for (int i = 0, j = 0; i < p_array_size; i++, j += 4) {
  48. if (i < s) {
  49. gui[j] = (r[i] != 0) ? 1 : 0;
  50. } else {
  51. gui[j] = 0;
  52. }
  53. gui[j + 1] = 0; // ignored
  54. gui[j + 2] = 0; // ignored
  55. gui[j + 3] = 0; // ignored
  56. }
  57. } else {
  58. bool v = value;
  59. gui[0] = v ? 1 : 0;
  60. }
  61. } break;
  62. case ShaderLanguage::TYPE_BVEC2: {
  63. uint32_t *gui = (uint32_t *)data;
  64. if (p_array_size > 0) {
  65. const PackedInt32Array &ba = value;
  66. int s = ba.size();
  67. const int *r = ba.ptr();
  68. int count = 2 * p_array_size;
  69. for (int i = 0, j = 0; i < count; i += 2, j += 4) {
  70. if (i < s) {
  71. gui[j] = r[i] ? 1 : 0;
  72. gui[j + 1] = r[i + 1] ? 1 : 0;
  73. } else {
  74. gui[j] = 0;
  75. gui[j + 1] = 0;
  76. }
  77. gui[j + 2] = 0; // ignored
  78. gui[j + 3] = 0; // ignored
  79. }
  80. } else {
  81. int v = value;
  82. gui[0] = v & 1 ? 1 : 0;
  83. gui[1] = v & 2 ? 1 : 0;
  84. }
  85. } break;
  86. case ShaderLanguage::TYPE_BVEC3: {
  87. uint32_t *gui = (uint32_t *)data;
  88. if (p_array_size > 0) {
  89. const PackedInt32Array &ba = value;
  90. int s = ba.size();
  91. const int *r = ba.ptr();
  92. int count = 3 * p_array_size;
  93. for (int i = 0, j = 0; i < count; i += 3, j += 4) {
  94. if (i < s) {
  95. gui[j] = r[i] ? 1 : 0;
  96. gui[j + 1] = r[i + 1] ? 1 : 0;
  97. gui[j + 2] = r[i + 2] ? 1 : 0;
  98. } else {
  99. gui[j] = 0;
  100. gui[j + 1] = 0;
  101. gui[j + 2] = 0;
  102. }
  103. gui[j + 3] = 0; // ignored
  104. }
  105. } else {
  106. int v = value;
  107. gui[0] = (v & 1) ? 1 : 0;
  108. gui[1] = (v & 2) ? 1 : 0;
  109. gui[2] = (v & 4) ? 1 : 0;
  110. }
  111. } break;
  112. case ShaderLanguage::TYPE_BVEC4: {
  113. uint32_t *gui = (uint32_t *)data;
  114. if (p_array_size > 0) {
  115. const PackedInt32Array &ba = value;
  116. int s = ba.size();
  117. const int *r = ba.ptr();
  118. int count = 4 * p_array_size;
  119. for (int i = 0; i < count; i += 4) {
  120. if (i < s) {
  121. gui[i] = r[i] ? 1 : 0;
  122. gui[i + 1] = r[i + 1] ? 1 : 0;
  123. gui[i + 2] = r[i + 2] ? 1 : 0;
  124. gui[i + 3] = r[i + 3] ? 1 : 0;
  125. } else {
  126. gui[i] = 0;
  127. gui[i + 1] = 0;
  128. gui[i + 2] = 0;
  129. gui[i + 3] = 0;
  130. }
  131. }
  132. } else {
  133. int v = value;
  134. gui[0] = (v & 1) ? 1 : 0;
  135. gui[1] = (v & 2) ? 1 : 0;
  136. gui[2] = (v & 4) ? 1 : 0;
  137. gui[3] = (v & 8) ? 1 : 0;
  138. }
  139. } break;
  140. case ShaderLanguage::TYPE_INT: {
  141. int32_t *gui = (int32_t *)data;
  142. if (p_array_size > 0) {
  143. Vector<int> iv = value;
  144. int s = iv.size();
  145. const int *r = iv.ptr();
  146. for (int i = 0, j = 0; i < p_array_size; i++, j += 4) {
  147. if (i < s) {
  148. gui[j] = r[i];
  149. } else {
  150. gui[j] = 0;
  151. }
  152. gui[j + 1] = 0; // ignored
  153. gui[j + 2] = 0; // ignored
  154. gui[j + 3] = 0; // ignored
  155. }
  156. } else {
  157. int v = value;
  158. gui[0] = v;
  159. }
  160. } break;
  161. case ShaderLanguage::TYPE_IVEC2: {
  162. Vector<int> iv = value;
  163. int s = iv.size();
  164. int32_t *gui = (int32_t *)data;
  165. if (p_array_size <= 0) {
  166. p_array_size = 1;
  167. }
  168. int count = 2 * p_array_size;
  169. const int *r = iv.ptr();
  170. for (int i = 0, j = 0; i < count; i += 2, j += 4) {
  171. if (i < s) {
  172. gui[j] = r[i];
  173. gui[j + 1] = r[i + 1];
  174. } else {
  175. gui[j] = 0;
  176. gui[j + 1] = 0;
  177. }
  178. gui[j + 2] = 0; // ignored
  179. gui[j + 3] = 0; // ignored
  180. }
  181. } break;
  182. case ShaderLanguage::TYPE_IVEC3: {
  183. Vector<int> iv = value;
  184. int s = iv.size();
  185. int32_t *gui = (int32_t *)data;
  186. if (p_array_size <= 0) {
  187. p_array_size = 1;
  188. }
  189. int count = 3 * p_array_size;
  190. const int *r = iv.ptr();
  191. for (int i = 0, j = 0; i < count; i += 3, j += 4) {
  192. if (i < s) {
  193. gui[j] = r[i];
  194. gui[j + 1] = r[i + 1];
  195. gui[j + 2] = r[i + 2];
  196. } else {
  197. gui[j] = 0;
  198. gui[j + 1] = 0;
  199. gui[j + 2] = 0;
  200. }
  201. gui[j + 3] = 0; // ignored
  202. }
  203. } break;
  204. case ShaderLanguage::TYPE_IVEC4: {
  205. Vector<int> iv = value;
  206. int s = iv.size();
  207. int32_t *gui = (int32_t *)data;
  208. if (p_array_size <= 0) {
  209. p_array_size = 1;
  210. }
  211. int count = 4 * p_array_size;
  212. const int *r = iv.ptr();
  213. for (int i = 0; i < count; i += 4) {
  214. if (i < s) {
  215. gui[i] = r[i];
  216. gui[i + 1] = r[i + 1];
  217. gui[i + 2] = r[i + 2];
  218. gui[i + 3] = r[i + 3];
  219. } else {
  220. gui[i] = 0;
  221. gui[i + 1] = 0;
  222. gui[i + 2] = 0;
  223. gui[i + 3] = 0;
  224. }
  225. }
  226. } break;
  227. case ShaderLanguage::TYPE_UINT: {
  228. uint32_t *gui = (uint32_t *)data;
  229. if (p_array_size > 0) {
  230. Vector<int> iv = value;
  231. int s = iv.size();
  232. const int *r = iv.ptr();
  233. for (int i = 0, j = 0; i < p_array_size; i++, j += 4) {
  234. if (i < s) {
  235. gui[j] = r[i];
  236. } else {
  237. gui[j] = 0;
  238. }
  239. gui[j + 1] = 0; // ignored
  240. gui[j + 2] = 0; // ignored
  241. gui[j + 3] = 0; // ignored
  242. }
  243. } else {
  244. int v = value;
  245. gui[0] = v;
  246. }
  247. } break;
  248. case ShaderLanguage::TYPE_UVEC2: {
  249. Vector<int> iv = value;
  250. int s = iv.size();
  251. uint32_t *gui = (uint32_t *)data;
  252. if (p_array_size <= 0) {
  253. p_array_size = 1;
  254. }
  255. int count = 2 * p_array_size;
  256. const int *r = iv.ptr();
  257. for (int i = 0, j = 0; i < count; i += 2, j += 4) {
  258. if (i < s) {
  259. gui[j] = r[i];
  260. gui[j + 1] = r[i + 1];
  261. } else {
  262. gui[j] = 0;
  263. gui[j + 1] = 0;
  264. }
  265. gui[j + 2] = 0; // ignored
  266. gui[j + 3] = 0; // ignored
  267. }
  268. } break;
  269. case ShaderLanguage::TYPE_UVEC3: {
  270. Vector<int> iv = value;
  271. int s = iv.size();
  272. uint32_t *gui = (uint32_t *)data;
  273. if (p_array_size <= 0) {
  274. p_array_size = 1;
  275. }
  276. int count = 3 * p_array_size;
  277. const int *r = iv.ptr();
  278. for (int i = 0, j = 0; i < count; i += 3, j += 4) {
  279. if (i < s) {
  280. gui[j] = r[i];
  281. gui[j + 1] = r[i + 1];
  282. gui[j + 2] = r[i + 2];
  283. } else {
  284. gui[j] = 0;
  285. gui[j + 1] = 0;
  286. gui[j + 2] = 0;
  287. }
  288. gui[j + 3] = 0; // ignored
  289. }
  290. } break;
  291. case ShaderLanguage::TYPE_UVEC4: {
  292. Vector<int> iv = value;
  293. int s = iv.size();
  294. uint32_t *gui = (uint32_t *)data;
  295. if (p_array_size <= 0) {
  296. p_array_size = 1;
  297. }
  298. int count = 4 * p_array_size;
  299. const int *r = iv.ptr();
  300. for (int i = 0; i < count; i++) {
  301. if (i < s) {
  302. gui[i] = r[i];
  303. gui[i + 1] = r[i + 1];
  304. gui[i + 2] = r[i + 2];
  305. gui[i + 3] = r[i + 3];
  306. } else {
  307. gui[i] = 0;
  308. gui[i + 1] = 0;
  309. gui[i + 2] = 0;
  310. gui[i + 3] = 0;
  311. }
  312. }
  313. } break;
  314. case ShaderLanguage::TYPE_FLOAT: {
  315. float *gui = (float *)data;
  316. if (p_array_size > 0) {
  317. const PackedFloat32Array &a = value;
  318. int s = a.size();
  319. for (int i = 0, j = 0; i < p_array_size; i++, j += 4) {
  320. if (i < s) {
  321. gui[j] = a[i];
  322. } else {
  323. gui[j] = 0;
  324. }
  325. gui[j + 1] = 0; // ignored
  326. gui[j + 2] = 0; // ignored
  327. gui[j + 3] = 0; // ignored
  328. }
  329. } else {
  330. float v = value;
  331. gui[0] = v;
  332. }
  333. } break;
  334. case ShaderLanguage::TYPE_VEC2: {
  335. float *gui = (float *)data;
  336. if (p_array_size > 0) {
  337. const PackedVector2Array &a = value;
  338. int s = a.size();
  339. for (int i = 0, j = 0; i < p_array_size; i++, j += 4) {
  340. if (i < s) {
  341. gui[j] = a[i].x;
  342. gui[j + 1] = a[i].y;
  343. } else {
  344. gui[j] = 0;
  345. gui[j + 1] = 0;
  346. }
  347. gui[j + 2] = 0; // ignored
  348. gui[j + 3] = 0; // ignored
  349. }
  350. } else {
  351. Vector2 v = value;
  352. gui[0] = v.x;
  353. gui[1] = v.y;
  354. }
  355. } break;
  356. case ShaderLanguage::TYPE_VEC3: {
  357. float *gui = (float *)data;
  358. if (p_array_size > 0) {
  359. const PackedVector3Array &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. gui[j] = a[i].x;
  364. gui[j + 1] = a[i].y;
  365. gui[j + 2] = a[i].z;
  366. } else {
  367. gui[j] = 0;
  368. gui[j + 1] = 0;
  369. gui[j + 2] = 0;
  370. }
  371. gui[j + 3] = 0; // ignored
  372. }
  373. } else {
  374. Vector3 v = value;
  375. gui[0] = v.x;
  376. gui[1] = v.y;
  377. gui[2] = v.z;
  378. }
  379. } break;
  380. case ShaderLanguage::TYPE_VEC4: {
  381. float *gui = (float *)data;
  382. if (p_array_size > 0) {
  383. if (value.get_type() == Variant::PACKED_COLOR_ARRAY) {
  384. const PackedColorArray &a = value;
  385. int s = a.size();
  386. for (int i = 0, j = 0; i < p_array_size; i++, j += 4) {
  387. if (i < s) {
  388. Color color = a[i];
  389. if (p_linear_color) {
  390. color = color.srgb_to_linear();
  391. }
  392. gui[j] = color.r;
  393. gui[j + 1] = color.g;
  394. gui[j + 2] = color.b;
  395. gui[j + 3] = color.a;
  396. } else {
  397. gui[j] = 0;
  398. gui[j + 1] = 0;
  399. gui[j + 2] = 0;
  400. gui[j + 3] = 0;
  401. }
  402. }
  403. } else {
  404. const PackedFloat32Array &a = value;
  405. int s = a.size();
  406. int count = 4 * p_array_size;
  407. for (int i = 0; i < count; i += 4) {
  408. if (i + 3 < s) {
  409. gui[i] = a[i];
  410. gui[i + 1] = a[i + 1];
  411. gui[i + 2] = a[i + 2];
  412. gui[i + 3] = a[i + 3];
  413. } else {
  414. gui[i] = 0;
  415. gui[i + 1] = 0;
  416. gui[i + 2] = 0;
  417. gui[i + 3] = 0;
  418. }
  419. }
  420. }
  421. } else {
  422. if (value.get_type() == Variant::COLOR) {
  423. Color v = value;
  424. if (p_linear_color) {
  425. v = v.srgb_to_linear();
  426. }
  427. gui[0] = v.r;
  428. gui[1] = v.g;
  429. gui[2] = v.b;
  430. gui[3] = v.a;
  431. } else if (value.get_type() == Variant::RECT2) {
  432. Rect2 v = value;
  433. gui[0] = v.position.x;
  434. gui[1] = v.position.y;
  435. gui[2] = v.size.x;
  436. gui[3] = v.size.y;
  437. } else if (value.get_type() == Variant::QUATERNION) {
  438. Quaternion v = value;
  439. gui[0] = v.x;
  440. gui[1] = v.y;
  441. gui[2] = v.z;
  442. gui[3] = v.w;
  443. } else {
  444. Plane v = value;
  445. gui[0] = v.normal.x;
  446. gui[1] = v.normal.y;
  447. gui[2] = v.normal.z;
  448. gui[3] = v.d;
  449. }
  450. }
  451. } break;
  452. case ShaderLanguage::TYPE_MAT2: {
  453. float *gui = (float *)data;
  454. if (p_array_size > 0) {
  455. const PackedFloat32Array &a = value;
  456. int s = a.size();
  457. for (int i = 0, j = 0; i < p_array_size * 4; i += 4, j += 8) {
  458. if (i + 3 < s) {
  459. gui[j] = a[i];
  460. gui[j + 1] = a[i + 1];
  461. gui[j + 4] = a[i + 2];
  462. gui[j + 5] = a[i + 3];
  463. } else {
  464. gui[j] = 1;
  465. gui[j + 1] = 0;
  466. gui[j + 4] = 0;
  467. gui[j + 5] = 1;
  468. }
  469. gui[j + 2] = 0; // ignored
  470. gui[j + 3] = 0; // ignored
  471. gui[j + 6] = 0; // ignored
  472. gui[j + 7] = 0; // ignored
  473. }
  474. } else {
  475. Transform2D v = value;
  476. //in std140 members of mat2 are treated as vec4s
  477. gui[0] = v.columns[0][0];
  478. gui[1] = v.columns[0][1];
  479. gui[2] = 0; // ignored
  480. gui[3] = 0; // ignored
  481. gui[4] = v.columns[1][0];
  482. gui[5] = v.columns[1][1];
  483. gui[6] = 0; // ignored
  484. gui[7] = 0; // ignored
  485. }
  486. } break;
  487. case ShaderLanguage::TYPE_MAT3: {
  488. float *gui = (float *)data;
  489. if (p_array_size > 0) {
  490. const PackedFloat32Array &a = value;
  491. int s = a.size();
  492. for (int i = 0, j = 0; i < p_array_size * 9; i += 9, j += 12) {
  493. if (i + 8 < s) {
  494. gui[j] = a[i];
  495. gui[j + 1] = a[i + 1];
  496. gui[j + 2] = a[i + 2];
  497. gui[j + 4] = a[i + 3];
  498. gui[j + 5] = a[i + 4];
  499. gui[j + 6] = a[i + 5];
  500. gui[j + 8] = a[i + 6];
  501. gui[j + 9] = a[i + 7];
  502. gui[j + 10] = a[i + 8];
  503. } else {
  504. gui[j] = 1;
  505. gui[j + 1] = 0;
  506. gui[j + 2] = 0;
  507. gui[j + 4] = 0;
  508. gui[j + 5] = 1;
  509. gui[j + 6] = 0;
  510. gui[j + 8] = 0;
  511. gui[j + 9] = 0;
  512. gui[j + 10] = 1;
  513. }
  514. gui[j + 3] = 0; // ignored
  515. gui[j + 7] = 0; // ignored
  516. gui[j + 11] = 0; // ignored
  517. }
  518. } else {
  519. Basis v = value;
  520. gui[0] = v.elements[0][0];
  521. gui[1] = v.elements[1][0];
  522. gui[2] = v.elements[2][0];
  523. gui[3] = 0; // ignored
  524. gui[4] = v.elements[0][1];
  525. gui[5] = v.elements[1][1];
  526. gui[6] = v.elements[2][1];
  527. gui[7] = 0; // ignored
  528. gui[8] = v.elements[0][2];
  529. gui[9] = v.elements[1][2];
  530. gui[10] = v.elements[2][2];
  531. gui[11] = 0; // ignored
  532. }
  533. } break;
  534. case ShaderLanguage::TYPE_MAT4: {
  535. float *gui = (float *)data;
  536. if (p_array_size > 0) {
  537. const PackedFloat32Array &a = value;
  538. int s = a.size();
  539. for (int i = 0; i < p_array_size * 16; i += 16) {
  540. if (i + 15 < s) {
  541. gui[i] = a[i];
  542. gui[i + 1] = a[i + 1];
  543. gui[i + 2] = a[i + 2];
  544. gui[i + 3] = a[i + 3];
  545. gui[i + 4] = a[i + 4];
  546. gui[i + 5] = a[i + 5];
  547. gui[i + 6] = a[i + 6];
  548. gui[i + 7] = a[i + 7];
  549. gui[i + 8] = a[i + 8];
  550. gui[i + 9] = a[i + 9];
  551. gui[i + 10] = a[i + 10];
  552. gui[i + 11] = a[i + 11];
  553. gui[i + 12] = a[i + 12];
  554. gui[i + 13] = a[i + 13];
  555. gui[i + 14] = a[i + 14];
  556. gui[i + 15] = a[i + 15];
  557. } else {
  558. gui[i] = 1;
  559. gui[i + 1] = 0;
  560. gui[i + 2] = 0;
  561. gui[i + 3] = 0;
  562. gui[i + 4] = 0;
  563. gui[i + 5] = 1;
  564. gui[i + 6] = 0;
  565. gui[i + 7] = 0;
  566. gui[i + 8] = 0;
  567. gui[i + 9] = 0;
  568. gui[i + 10] = 1;
  569. gui[i + 11] = 0;
  570. gui[i + 12] = 0;
  571. gui[i + 13] = 0;
  572. gui[i + 14] = 0;
  573. gui[i + 15] = 1;
  574. }
  575. }
  576. } else {
  577. Transform3D v = value;
  578. gui[0] = v.basis.elements[0][0];
  579. gui[1] = v.basis.elements[1][0];
  580. gui[2] = v.basis.elements[2][0];
  581. gui[3] = 0;
  582. gui[4] = v.basis.elements[0][1];
  583. gui[5] = v.basis.elements[1][1];
  584. gui[6] = v.basis.elements[2][1];
  585. gui[7] = 0;
  586. gui[8] = v.basis.elements[0][2];
  587. gui[9] = v.basis.elements[1][2];
  588. gui[10] = v.basis.elements[2][2];
  589. gui[11] = 0;
  590. gui[12] = v.origin.x;
  591. gui[13] = v.origin.y;
  592. gui[14] = v.origin.z;
  593. gui[15] = 1;
  594. }
  595. } break;
  596. default: {
  597. }
  598. }
  599. }
  600. _FORCE_INLINE_ static void _fill_std140_ubo_value(ShaderLanguage::DataType type, const Vector<ShaderLanguage::ConstantNode::Value> &value, uint8_t *data) {
  601. switch (type) {
  602. case ShaderLanguage::TYPE_BOOL: {
  603. uint32_t *gui = (uint32_t *)data;
  604. *gui = value[0].boolean ? 1 : 0;
  605. } break;
  606. case ShaderLanguage::TYPE_BVEC2: {
  607. uint32_t *gui = (uint32_t *)data;
  608. gui[0] = value[0].boolean ? 1 : 0;
  609. gui[1] = value[1].boolean ? 1 : 0;
  610. } break;
  611. case ShaderLanguage::TYPE_BVEC3: {
  612. uint32_t *gui = (uint32_t *)data;
  613. gui[0] = value[0].boolean ? 1 : 0;
  614. gui[1] = value[1].boolean ? 1 : 0;
  615. gui[2] = value[2].boolean ? 1 : 0;
  616. } break;
  617. case ShaderLanguage::TYPE_BVEC4: {
  618. uint32_t *gui = (uint32_t *)data;
  619. gui[0] = value[0].boolean ? 1 : 0;
  620. gui[1] = value[1].boolean ? 1 : 0;
  621. gui[2] = value[2].boolean ? 1 : 0;
  622. gui[3] = value[3].boolean ? 1 : 0;
  623. } break;
  624. case ShaderLanguage::TYPE_INT: {
  625. int32_t *gui = (int32_t *)data;
  626. gui[0] = value[0].sint;
  627. } break;
  628. case ShaderLanguage::TYPE_IVEC2: {
  629. int32_t *gui = (int32_t *)data;
  630. for (int i = 0; i < 2; i++) {
  631. gui[i] = value[i].sint;
  632. }
  633. } break;
  634. case ShaderLanguage::TYPE_IVEC3: {
  635. int32_t *gui = (int32_t *)data;
  636. for (int i = 0; i < 3; i++) {
  637. gui[i] = value[i].sint;
  638. }
  639. } break;
  640. case ShaderLanguage::TYPE_IVEC4: {
  641. int32_t *gui = (int32_t *)data;
  642. for (int i = 0; i < 4; i++) {
  643. gui[i] = value[i].sint;
  644. }
  645. } break;
  646. case ShaderLanguage::TYPE_UINT: {
  647. uint32_t *gui = (uint32_t *)data;
  648. gui[0] = value[0].uint;
  649. } break;
  650. case ShaderLanguage::TYPE_UVEC2: {
  651. int32_t *gui = (int32_t *)data;
  652. for (int i = 0; i < 2; i++) {
  653. gui[i] = value[i].uint;
  654. }
  655. } break;
  656. case ShaderLanguage::TYPE_UVEC3: {
  657. int32_t *gui = (int32_t *)data;
  658. for (int i = 0; i < 3; i++) {
  659. gui[i] = value[i].uint;
  660. }
  661. } break;
  662. case ShaderLanguage::TYPE_UVEC4: {
  663. int32_t *gui = (int32_t *)data;
  664. for (int i = 0; i < 4; i++) {
  665. gui[i] = value[i].uint;
  666. }
  667. } break;
  668. case ShaderLanguage::TYPE_FLOAT: {
  669. float *gui = (float *)data;
  670. gui[0] = value[0].real;
  671. } break;
  672. case ShaderLanguage::TYPE_VEC2: {
  673. float *gui = (float *)data;
  674. for (int i = 0; i < 2; i++) {
  675. gui[i] = value[i].real;
  676. }
  677. } break;
  678. case ShaderLanguage::TYPE_VEC3: {
  679. float *gui = (float *)data;
  680. for (int i = 0; i < 3; i++) {
  681. gui[i] = value[i].real;
  682. }
  683. } break;
  684. case ShaderLanguage::TYPE_VEC4: {
  685. float *gui = (float *)data;
  686. for (int i = 0; i < 4; i++) {
  687. gui[i] = value[i].real;
  688. }
  689. } break;
  690. case ShaderLanguage::TYPE_MAT2: {
  691. float *gui = (float *)data;
  692. //in std140 members of mat2 are treated as vec4s
  693. gui[0] = value[0].real;
  694. gui[1] = value[1].real;
  695. gui[2] = 0;
  696. gui[3] = 0;
  697. gui[4] = value[2].real;
  698. gui[5] = value[3].real;
  699. gui[6] = 0;
  700. gui[7] = 0;
  701. } break;
  702. case ShaderLanguage::TYPE_MAT3: {
  703. float *gui = (float *)data;
  704. gui[0] = value[0].real;
  705. gui[1] = value[1].real;
  706. gui[2] = value[2].real;
  707. gui[3] = 0;
  708. gui[4] = value[3].real;
  709. gui[5] = value[4].real;
  710. gui[6] = value[5].real;
  711. gui[7] = 0;
  712. gui[8] = value[6].real;
  713. gui[9] = value[7].real;
  714. gui[10] = value[8].real;
  715. gui[11] = 0;
  716. } break;
  717. case ShaderLanguage::TYPE_MAT4: {
  718. float *gui = (float *)data;
  719. for (int i = 0; i < 16; i++) {
  720. gui[i] = value[i].real;
  721. }
  722. } break;
  723. default: {
  724. }
  725. }
  726. }
  727. _FORCE_INLINE_ static void _fill_std140_ubo_empty(ShaderLanguage::DataType type, int p_array_size, uint8_t *data) {
  728. if (p_array_size <= 0) {
  729. p_array_size = 1;
  730. }
  731. switch (type) {
  732. case ShaderLanguage::TYPE_BOOL:
  733. case ShaderLanguage::TYPE_INT:
  734. case ShaderLanguage::TYPE_UINT:
  735. case ShaderLanguage::TYPE_FLOAT: {
  736. memset(data, 0, 4 * p_array_size);
  737. } break;
  738. case ShaderLanguage::TYPE_BVEC2:
  739. case ShaderLanguage::TYPE_IVEC2:
  740. case ShaderLanguage::TYPE_UVEC2:
  741. case ShaderLanguage::TYPE_VEC2: {
  742. memset(data, 0, 8 * p_array_size);
  743. } break;
  744. case ShaderLanguage::TYPE_BVEC3:
  745. case ShaderLanguage::TYPE_IVEC3:
  746. case ShaderLanguage::TYPE_UVEC3:
  747. case ShaderLanguage::TYPE_VEC3:
  748. case ShaderLanguage::TYPE_BVEC4:
  749. case ShaderLanguage::TYPE_IVEC4:
  750. case ShaderLanguage::TYPE_UVEC4:
  751. case ShaderLanguage::TYPE_VEC4: {
  752. memset(data, 0, 16 * p_array_size);
  753. } break;
  754. case ShaderLanguage::TYPE_MAT2: {
  755. memset(data, 0, 32 * p_array_size);
  756. } break;
  757. case ShaderLanguage::TYPE_MAT3: {
  758. memset(data, 0, 48 * p_array_size);
  759. } break;
  760. case ShaderLanguage::TYPE_MAT4: {
  761. memset(data, 0, 64 * p_array_size);
  762. } break;
  763. default: {
  764. }
  765. }
  766. }
  767. ///////////////////////////////////////////////////////////////////////////
  768. // MaterialData
  769. void MaterialData::update_uniform_buffer(const Map<StringName, ShaderLanguage::ShaderNode::Uniform> &p_uniforms, const uint32_t *p_uniform_offsets, const Map<StringName, Variant> &p_parameters, uint8_t *p_buffer, uint32_t p_buffer_size, bool p_use_linear_color) {
  770. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  771. bool uses_global_buffer = false;
  772. for (const KeyValue<StringName, ShaderLanguage::ShaderNode::Uniform> &E : p_uniforms) {
  773. if (E.value.order < 0) {
  774. continue; // texture, does not go here
  775. }
  776. if (E.value.scope == ShaderLanguage::ShaderNode::Uniform::SCOPE_INSTANCE) {
  777. continue; //instance uniforms don't appear in the buffer
  778. }
  779. if (E.value.scope == ShaderLanguage::ShaderNode::Uniform::SCOPE_GLOBAL) {
  780. //this is a global variable, get the index to it
  781. GlobalVariables::Variable *gv = material_storage->global_variables.variables.getptr(E.key);
  782. uint32_t index = 0;
  783. if (gv) {
  784. index = gv->buffer_index;
  785. } else {
  786. WARN_PRINT("Shader uses global uniform '" + E.key + "', but it was removed at some point. Material will not display correctly.");
  787. }
  788. uint32_t offset = p_uniform_offsets[E.value.order];
  789. uint32_t *intptr = (uint32_t *)&p_buffer[offset];
  790. *intptr = index;
  791. uses_global_buffer = true;
  792. continue;
  793. }
  794. //regular uniform
  795. uint32_t offset = p_uniform_offsets[E.value.order];
  796. #ifdef DEBUG_ENABLED
  797. uint32_t size = 0U;
  798. // The following code enforces a 16-byte alignment of uniform arrays.
  799. if (E.value.array_size > 0) {
  800. size = ShaderLanguage::get_datatype_size(E.value.type) * E.value.array_size;
  801. int m = (16 * E.value.array_size);
  802. if ((size % m) != 0U) {
  803. size += m - (size % m);
  804. }
  805. } else {
  806. size = ShaderLanguage::get_datatype_size(E.value.type);
  807. }
  808. ERR_CONTINUE(offset + size > p_buffer_size);
  809. #endif
  810. uint8_t *data = &p_buffer[offset];
  811. const Map<StringName, Variant>::Element *V = p_parameters.find(E.key);
  812. if (V) {
  813. //user provided
  814. _fill_std140_variant_ubo_value(E.value.type, E.value.array_size, V->get(), data, p_use_linear_color);
  815. } else if (E.value.default_value.size()) {
  816. //default value
  817. _fill_std140_ubo_value(E.value.type, E.value.default_value, data);
  818. //value=E.value.default_value;
  819. } else {
  820. //zero because it was not provided
  821. if (E.value.type == ShaderLanguage::TYPE_VEC4 && E.value.hint == ShaderLanguage::ShaderNode::Uniform::HINT_COLOR) {
  822. //colors must be set as black, with alpha as 1.0
  823. _fill_std140_variant_ubo_value(E.value.type, E.value.array_size, Color(0, 0, 0, 1), data, p_use_linear_color);
  824. } else {
  825. //else just zero it out
  826. _fill_std140_ubo_empty(E.value.type, E.value.array_size, data);
  827. }
  828. }
  829. }
  830. if (uses_global_buffer != (global_buffer_E != nullptr)) {
  831. if (uses_global_buffer) {
  832. global_buffer_E = material_storage->global_variables.materials_using_buffer.push_back(self);
  833. } else {
  834. material_storage->global_variables.materials_using_buffer.erase(global_buffer_E);
  835. global_buffer_E = nullptr;
  836. }
  837. }
  838. }
  839. MaterialData::~MaterialData() {
  840. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  841. if (global_buffer_E) {
  842. //unregister global buffers
  843. material_storage->global_variables.materials_using_buffer.erase(global_buffer_E);
  844. }
  845. if (global_texture_E) {
  846. //unregister global textures
  847. for (const KeyValue<StringName, uint64_t> &E : used_global_textures) {
  848. GlobalVariables::Variable *v = material_storage->global_variables.variables.getptr(E.key);
  849. if (v) {
  850. v->texture_materials.erase(self);
  851. }
  852. }
  853. //unregister material from those using global textures
  854. material_storage->global_variables.materials_using_texture.erase(global_texture_E);
  855. }
  856. if (uniform_buffer) {
  857. glDeleteBuffers(1, &uniform_buffer);
  858. }
  859. }
  860. void MaterialData::update_textures(const Map<StringName, Variant> &p_parameters, const Map<StringName, Map<int, RID>> &p_default_textures, const Vector<ShaderCompiler::GeneratedCode::Texture> &p_texture_uniforms, RID *p_textures, bool p_use_linear_color) {
  861. TextureStorage *texture_storage = TextureStorage::get_singleton();
  862. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  863. #ifdef TOOLS_ENABLED
  864. Texture *roughness_detect_texture = nullptr;
  865. RS::TextureDetectRoughnessChannel roughness_channel = RS::TEXTURE_DETECT_ROUGHNESS_R;
  866. Texture *normal_detect_texture = nullptr;
  867. #endif
  868. bool uses_global_textures = false;
  869. global_textures_pass++;
  870. for (int i = 0, k = 0; i < p_texture_uniforms.size(); i++) {
  871. const StringName &uniform_name = p_texture_uniforms[i].name;
  872. int uniform_array_size = p_texture_uniforms[i].array_size;
  873. Vector<RID> textures;
  874. if (p_texture_uniforms[i].global) {
  875. uses_global_textures = true;
  876. GlobalVariables::Variable *v = material_storage->global_variables.variables.getptr(uniform_name);
  877. if (v) {
  878. if (v->buffer_index >= 0) {
  879. WARN_PRINT("Shader uses global uniform texture '" + String(uniform_name) + "', but it changed type and is no longer a texture!.");
  880. } else {
  881. Map<StringName, uint64_t>::Element *E = used_global_textures.find(uniform_name);
  882. if (!E) {
  883. E = used_global_textures.insert(uniform_name, global_textures_pass);
  884. v->texture_materials.insert(self);
  885. } else {
  886. E->get() = global_textures_pass;
  887. }
  888. textures.push_back(v->override.get_type() != Variant::NIL ? v->override : v->value);
  889. }
  890. } else {
  891. WARN_PRINT("Shader uses global uniform texture '" + String(uniform_name) + "', but it was removed at some point. Material will not display correctly.");
  892. }
  893. } else {
  894. const Map<StringName, Variant>::Element *V = p_parameters.find(uniform_name);
  895. if (V) {
  896. if (V->get().is_array()) {
  897. Array array = (Array)V->get();
  898. if (uniform_array_size > 0) {
  899. for (int j = 0; j < array.size(); j++) {
  900. textures.push_back(array[j]);
  901. }
  902. } else {
  903. if (array.size() > 0) {
  904. textures.push_back(array[0]);
  905. }
  906. }
  907. } else {
  908. textures.push_back(V->get());
  909. }
  910. }
  911. if (uniform_array_size > 0) {
  912. if (textures.size() < uniform_array_size) {
  913. const Map<StringName, Map<int, RID>>::Element *W = p_default_textures.find(uniform_name);
  914. for (int j = textures.size(); j < uniform_array_size; j++) {
  915. if (W && W->get().has(j)) {
  916. textures.push_back(W->get()[j]);
  917. } else {
  918. textures.push_back(RID());
  919. }
  920. }
  921. }
  922. } else if (textures.is_empty()) {
  923. const Map<StringName, Map<int, RID>>::Element *W = p_default_textures.find(uniform_name);
  924. if (W && W->get().has(0)) {
  925. textures.push_back(W->get()[0]);
  926. }
  927. }
  928. }
  929. RID gl_texture = texture_storage->texture_gl_get_default(DEFAULT_GL_TEXTURE_WHITE);
  930. if (textures.is_empty()) {
  931. //check default usage
  932. switch (p_texture_uniforms[i].type) {
  933. case ShaderLanguage::TYPE_ISAMPLER2D:
  934. case ShaderLanguage::TYPE_USAMPLER2D:
  935. case ShaderLanguage::TYPE_SAMPLER2D: {
  936. switch (p_texture_uniforms[i].hint) {
  937. case ShaderLanguage::ShaderNode::Uniform::HINT_BLACK:
  938. case ShaderLanguage::ShaderNode::Uniform::HINT_BLACK_ALBEDO: {
  939. gl_texture = texture_storage->texture_gl_get_default(DEFAULT_GL_TEXTURE_BLACK);
  940. } break;
  941. case ShaderLanguage::ShaderNode::Uniform::HINT_ANISOTROPY: {
  942. gl_texture = texture_storage->texture_gl_get_default(DEFAULT_GL_TEXTURE_ANISO);
  943. } break;
  944. case ShaderLanguage::ShaderNode::Uniform::HINT_NORMAL: {
  945. gl_texture = texture_storage->texture_gl_get_default(DEFAULT_GL_TEXTURE_NORMAL);
  946. } break;
  947. case ShaderLanguage::ShaderNode::Uniform::HINT_ROUGHNESS_NORMAL: {
  948. gl_texture = texture_storage->texture_gl_get_default(DEFAULT_GL_TEXTURE_NORMAL);
  949. } break;
  950. default: {
  951. gl_texture = texture_storage->texture_gl_get_default(DEFAULT_GL_TEXTURE_WHITE);
  952. } break;
  953. }
  954. } break;
  955. case ShaderLanguage::TYPE_SAMPLERCUBE: {
  956. switch (p_texture_uniforms[i].hint) {
  957. case ShaderLanguage::ShaderNode::Uniform::HINT_BLACK:
  958. case ShaderLanguage::ShaderNode::Uniform::HINT_BLACK_ALBEDO: {
  959. gl_texture = texture_storage->texture_gl_get_default(DEFAULT_GL_TEXTURE_CUBEMAP_BLACK);
  960. } break;
  961. default: {
  962. gl_texture = texture_storage->texture_gl_get_default(DEFAULT_GL_TEXTURE_CUBEMAP_WHITE);
  963. } break;
  964. }
  965. } break;
  966. case ShaderLanguage::TYPE_SAMPLERCUBEARRAY: {
  967. ERR_PRINT_ONCE("Type: SamplerCubeArray not supported in OpenGL renderer, please use another type.");
  968. } break;
  969. case ShaderLanguage::TYPE_ISAMPLER3D:
  970. case ShaderLanguage::TYPE_USAMPLER3D:
  971. case ShaderLanguage::TYPE_SAMPLER3D: {
  972. switch (p_texture_uniforms[i].hint) {
  973. case ShaderLanguage::ShaderNode::Uniform::HINT_BLACK:
  974. case ShaderLanguage::ShaderNode::Uniform::HINT_BLACK_ALBEDO: {
  975. gl_texture = texture_storage->texture_gl_get_default(DEFAULT_GL_TEXTURE_3D_BLACK);
  976. } break;
  977. default: {
  978. gl_texture = texture_storage->texture_gl_get_default(DEFAULT_GL_TEXTURE_3D_WHITE);
  979. } break;
  980. }
  981. } break;
  982. case ShaderLanguage::TYPE_ISAMPLER2DARRAY:
  983. case ShaderLanguage::TYPE_USAMPLER2DARRAY:
  984. case ShaderLanguage::TYPE_SAMPLER2DARRAY: {
  985. gl_texture = texture_storage->texture_gl_get_default(DEFAULT_GL_TEXTURE_2D_ARRAY_WHITE);
  986. } break;
  987. default: {
  988. }
  989. }
  990. #ifdef TOOLS_ENABLED
  991. if (roughness_detect_texture && normal_detect_texture && !normal_detect_texture->path.is_empty()) {
  992. roughness_detect_texture->detect_roughness_callback(roughness_detect_texture->detect_roughness_callback_ud, normal_detect_texture->path, roughness_channel);
  993. }
  994. #endif
  995. if (uniform_array_size > 0) {
  996. for (int j = 0; j < uniform_array_size; j++) {
  997. p_textures[k++] = gl_texture;
  998. }
  999. } else {
  1000. p_textures[k++] = gl_texture;
  1001. }
  1002. } else {
  1003. //bool srgb = p_use_linear_color && (p_texture_uniforms[i].hint == ShaderLanguage::ShaderNode::Uniform::HINT_ALBEDO || p_texture_uniforms[i].hint == ShaderLanguage::ShaderNode::Uniform::HINT_BLACK_ALBEDO);
  1004. for (int j = 0; j < textures.size(); j++) {
  1005. Texture *tex = TextureStorage::get_singleton()->get_texture(textures[j]);
  1006. if (tex) {
  1007. gl_texture = textures[j];
  1008. #ifdef TOOLS_ENABLED
  1009. if (tex->detect_3d_callback && p_use_linear_color) {
  1010. tex->detect_3d_callback(tex->detect_3d_callback_ud);
  1011. }
  1012. 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)) {
  1013. if (p_texture_uniforms[i].hint == ShaderLanguage::ShaderNode::Uniform::HINT_ROUGHNESS_NORMAL) {
  1014. normal_detect_texture = tex;
  1015. }
  1016. tex->detect_normal_callback(tex->detect_normal_callback_ud);
  1017. }
  1018. 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)) {
  1019. //find the normal texture
  1020. roughness_detect_texture = tex;
  1021. roughness_channel = RS::TextureDetectRoughnessChannel(p_texture_uniforms[i].hint - ShaderLanguage::ShaderNode::Uniform::HINT_ROUGHNESS_R);
  1022. }
  1023. #endif
  1024. }
  1025. #ifdef TOOLS_ENABLED
  1026. if (roughness_detect_texture && normal_detect_texture && !normal_detect_texture->path.is_empty()) {
  1027. roughness_detect_texture->detect_roughness_callback(roughness_detect_texture->detect_roughness_callback_ud, normal_detect_texture->path, roughness_channel);
  1028. }
  1029. #endif
  1030. p_textures[k++] = gl_texture;
  1031. }
  1032. }
  1033. }
  1034. {
  1035. //for textures no longer used, unregister them
  1036. List<Map<StringName, uint64_t>::Element *> to_delete;
  1037. for (Map<StringName, uint64_t>::Element *E = used_global_textures.front(); E; E = E->next()) {
  1038. if (E->get() != global_textures_pass) {
  1039. to_delete.push_back(E);
  1040. GlobalVariables::Variable *v = material_storage->global_variables.variables.getptr(E->key());
  1041. if (v) {
  1042. v->texture_materials.erase(self);
  1043. }
  1044. }
  1045. }
  1046. while (to_delete.front()) {
  1047. used_global_textures.erase(to_delete.front()->get());
  1048. to_delete.pop_front();
  1049. }
  1050. //handle registering/unregistering global textures
  1051. if (uses_global_textures != (global_texture_E != nullptr)) {
  1052. if (uses_global_textures) {
  1053. global_texture_E = material_storage->global_variables.materials_using_texture.push_back(self);
  1054. } else {
  1055. material_storage->global_variables.materials_using_texture.erase(global_texture_E);
  1056. global_texture_E = nullptr;
  1057. }
  1058. }
  1059. }
  1060. }
  1061. void MaterialData::update_parameters_internal(const Map<StringName, Variant> &p_parameters, bool p_uniform_dirty, bool p_textures_dirty, const Map<StringName, ShaderLanguage::ShaderNode::Uniform> &p_uniforms, const uint32_t *p_uniform_offsets, const Vector<ShaderCompiler::GeneratedCode::Texture> &p_texture_uniforms, const Map<StringName, Map<int, RID>> &p_default_texture_params, uint32_t p_ubo_size) {
  1062. if ((uint32_t)ubo_data.size() != p_ubo_size) {
  1063. p_uniform_dirty = true;
  1064. if (!uniform_buffer) {
  1065. glGenBuffers(1, &uniform_buffer);
  1066. }
  1067. ubo_data.resize(p_ubo_size);
  1068. if (ubo_data.size()) {
  1069. memset(ubo_data.ptrw(), 0, ubo_data.size()); //clear
  1070. }
  1071. }
  1072. //check whether buffer changed
  1073. if (p_uniform_dirty && ubo_data.size()) {
  1074. update_uniform_buffer(p_uniforms, p_uniform_offsets, p_parameters, ubo_data.ptrw(), ubo_data.size(), true);
  1075. glBindBuffer(GL_UNIFORM_BUFFER, uniform_buffer);
  1076. glBufferData(GL_UNIFORM_BUFFER, ubo_data.size(), ubo_data.ptrw(), GL_DYNAMIC_DRAW);
  1077. glBindBuffer(GL_UNIFORM_BUFFER, 0);
  1078. }
  1079. uint32_t tex_uniform_count = 0U;
  1080. for (int i = 0; i < p_texture_uniforms.size(); i++) {
  1081. tex_uniform_count += uint32_t(p_texture_uniforms[i].array_size > 0 ? p_texture_uniforms[i].array_size : 1);
  1082. }
  1083. if ((uint32_t)texture_cache.size() != tex_uniform_count || p_textures_dirty) {
  1084. texture_cache.resize(tex_uniform_count);
  1085. p_textures_dirty = true;
  1086. }
  1087. if (p_textures_dirty && tex_uniform_count) {
  1088. update_textures(p_parameters, p_default_texture_params, p_texture_uniforms, texture_cache.ptrw(), true);
  1089. }
  1090. }
  1091. ///////////////////////////////////////////////////////////////////////////
  1092. // Material Storage
  1093. MaterialStorage *MaterialStorage::singleton = nullptr;
  1094. MaterialStorage *MaterialStorage::get_singleton() {
  1095. return singleton;
  1096. }
  1097. MaterialStorage::MaterialStorage() {
  1098. singleton = this;
  1099. shader_data_request_func[RS::SHADER_SPATIAL] = nullptr;
  1100. shader_data_request_func[RS::SHADER_CANVAS_ITEM] = _create_canvas_shader_func;
  1101. shader_data_request_func[RS::SHADER_PARTICLES] = nullptr;
  1102. shader_data_request_func[RS::SHADER_SKY] = nullptr;
  1103. shader_data_request_func[RS::SHADER_FOG] = nullptr;
  1104. material_data_request_func[RS::SHADER_SPATIAL] = nullptr;
  1105. material_data_request_func[RS::SHADER_CANVAS_ITEM] = _create_canvas_material_func;
  1106. material_data_request_func[RS::SHADER_PARTICLES] = nullptr;
  1107. material_data_request_func[RS::SHADER_SKY] = nullptr;
  1108. material_data_request_func[RS::SHADER_FOG] = nullptr;
  1109. static_assert(sizeof(GlobalVariables::Value) == 16);
  1110. global_variables.buffer_size = MAX(4096, (int)GLOBAL_GET("rendering/limits/global_shader_variables/buffer_size"));
  1111. if (global_variables.buffer_size > uint32_t(Config::get_singleton()->max_uniform_buffer_size)) {
  1112. global_variables.buffer_size = uint32_t(Config::get_singleton()->max_uniform_buffer_size);
  1113. WARN_PRINT("Project setting: rendering/limits/global_shader_variables/buffer_size exceeds maximum uniform buffer size of: " + itos(Config::get_singleton()->max_uniform_buffer_size));
  1114. }
  1115. global_variables.buffer_values = memnew_arr(GlobalVariables::Value, global_variables.buffer_size);
  1116. memset(global_variables.buffer_values, 0, sizeof(GlobalVariables::Value) * global_variables.buffer_size);
  1117. global_variables.buffer_usage = memnew_arr(GlobalVariables::ValueUsage, global_variables.buffer_size);
  1118. global_variables.buffer_dirty_regions = memnew_arr(bool, global_variables.buffer_size / GlobalVariables::BUFFER_DIRTY_REGION_SIZE);
  1119. memset(global_variables.buffer_dirty_regions, 0, sizeof(bool) * global_variables.buffer_size / GlobalVariables::BUFFER_DIRTY_REGION_SIZE);
  1120. glGenBuffers(1, &global_variables.buffer);
  1121. glBindBuffer(GL_UNIFORM_BUFFER, global_variables.buffer);
  1122. glBufferData(GL_UNIFORM_BUFFER, sizeof(GlobalVariables::Value) * global_variables.buffer_size, nullptr, GL_DYNAMIC_DRAW);
  1123. glBindBuffer(GL_UNIFORM_BUFFER, 0);
  1124. {
  1125. // Setup CanvasItem compiler
  1126. ShaderCompiler::DefaultIdentifierActions actions;
  1127. actions.renames["VERTEX"] = "vertex";
  1128. actions.renames["LIGHT_VERTEX"] = "light_vertex";
  1129. actions.renames["SHADOW_VERTEX"] = "shadow_vertex";
  1130. actions.renames["UV"] = "uv";
  1131. actions.renames["POINT_SIZE"] = "gl_PointSize";
  1132. actions.renames["MODEL_MATRIX"] = "model_matrix";
  1133. actions.renames["CANVAS_MATRIX"] = "canvas_data.canvas_transform";
  1134. actions.renames["SCREEN_MATRIX"] = "canvas_data.screen_transform";
  1135. actions.renames["TIME"] = "canvas_data.time";
  1136. actions.renames["PI"] = _MKSTR(Math_PI);
  1137. actions.renames["TAU"] = _MKSTR(Math_TAU);
  1138. actions.renames["E"] = _MKSTR(Math_E);
  1139. actions.renames["AT_LIGHT_PASS"] = "false";
  1140. actions.renames["INSTANCE_CUSTOM"] = "instance_custom";
  1141. actions.renames["COLOR"] = "color";
  1142. actions.renames["NORMAL"] = "normal";
  1143. actions.renames["NORMAL_MAP"] = "normal_map";
  1144. actions.renames["NORMAL_MAP_DEPTH"] = "normal_map_depth";
  1145. actions.renames["TEXTURE"] = "color_texture";
  1146. actions.renames["TEXTURE_PIXEL_SIZE"] = "draw_data.color_texture_pixel_size";
  1147. actions.renames["NORMAL_TEXTURE"] = "normal_texture";
  1148. actions.renames["SPECULAR_SHININESS_TEXTURE"] = "specular_texture";
  1149. actions.renames["SPECULAR_SHININESS"] = "specular_shininess";
  1150. actions.renames["SCREEN_UV"] = "screen_uv";
  1151. actions.renames["SCREEN_TEXTURE"] = "screen_texture";
  1152. actions.renames["SCREEN_PIXEL_SIZE"] = "canvas_data.screen_pixel_size";
  1153. actions.renames["FRAGCOORD"] = "gl_FragCoord";
  1154. actions.renames["POINT_COORD"] = "gl_PointCoord";
  1155. actions.renames["INSTANCE_ID"] = "gl_InstanceIndex";
  1156. actions.renames["VERTEX_ID"] = "gl_VertexIndex";
  1157. actions.renames["LIGHT_POSITION"] = "light_position";
  1158. actions.renames["LIGHT_COLOR"] = "light_color";
  1159. actions.renames["LIGHT_ENERGY"] = "light_energy";
  1160. actions.renames["LIGHT"] = "light";
  1161. actions.renames["SHADOW_MODULATE"] = "shadow_modulate";
  1162. actions.renames["texture_sdf"] = "texture_sdf";
  1163. actions.renames["texture_sdf_normal"] = "texture_sdf_normal";
  1164. actions.renames["sdf_to_screen_uv"] = "sdf_to_screen_uv";
  1165. actions.renames["screen_uv_to_sdf"] = "screen_uv_to_sdf";
  1166. actions.usage_defines["COLOR"] = "#define COLOR_USED\n";
  1167. actions.usage_defines["SCREEN_TEXTURE"] = "#define SCREEN_TEXTURE_USED\n";
  1168. actions.usage_defines["SCREEN_UV"] = "#define SCREEN_UV_USED\n";
  1169. actions.usage_defines["SCREEN_PIXEL_SIZE"] = "@SCREEN_UV";
  1170. actions.usage_defines["NORMAL"] = "#define NORMAL_USED\n";
  1171. actions.usage_defines["NORMAL_MAP"] = "#define NORMAL_MAP_USED\n";
  1172. actions.usage_defines["LIGHT"] = "#define LIGHT_SHADER_CODE_USED\n";
  1173. actions.render_mode_defines["skip_vertex_transform"] = "#define SKIP_TRANSFORM_USED\n";
  1174. actions.render_mode_defines["unshaded"] = "#define MODE_UNSHADED\n";
  1175. actions.render_mode_defines["light_only"] = "#define MODE_LIGHT_ONLY\n";
  1176. actions.base_texture_binding_index = 1;
  1177. actions.base_uniform_string = "";
  1178. actions.global_buffer_array_variable = "";
  1179. shaders.compiler_canvas.initialize(actions);
  1180. }
  1181. {
  1182. // Setup Scene compiler
  1183. /*
  1184. //shader compiler
  1185. ShaderCompiler::DefaultIdentifierActions actions;
  1186. actions.renames["MODEL_MATRIX"] = "model_matrix";
  1187. actions.renames["MODEL_NORMAL_MATRIX"] = "model_normal_matrix";
  1188. actions.renames["VIEW_MATRIX"] = "scene_data.view_matrix";
  1189. actions.renames["INV_VIEW_MATRIX"] = "scene_data.inv_view_matrix";
  1190. actions.renames["PROJECTION_MATRIX"] = "projection_matrix";
  1191. actions.renames["INV_PROJECTION_MATRIX"] = "inv_projection_matrix";
  1192. actions.renames["MODELVIEW_MATRIX"] = "modelview";
  1193. actions.renames["MODELVIEW_NORMAL_MATRIX"] = "modelview_normal";
  1194. actions.renames["VERTEX"] = "vertex";
  1195. actions.renames["NORMAL"] = "normal";
  1196. actions.renames["TANGENT"] = "tangent";
  1197. actions.renames["BINORMAL"] = "binormal";
  1198. actions.renames["POSITION"] = "position";
  1199. actions.renames["UV"] = "uv_interp";
  1200. actions.renames["UV2"] = "uv2_interp";
  1201. actions.renames["COLOR"] = "color_interp";
  1202. actions.renames["POINT_SIZE"] = "gl_PointSize";
  1203. actions.renames["INSTANCE_ID"] = "gl_InstanceIndex";
  1204. actions.renames["VERTEX_ID"] = "gl_VertexIndex";
  1205. actions.renames["ALPHA_SCISSOR_THRESHOLD"] = "alpha_scissor_threshold";
  1206. actions.renames["ALPHA_HASH_SCALE"] = "alpha_hash_scale";
  1207. actions.renames["ALPHA_ANTIALIASING_EDGE"] = "alpha_antialiasing_edge";
  1208. actions.renames["ALPHA_TEXTURE_COORDINATE"] = "alpha_texture_coordinate";
  1209. //builtins
  1210. actions.renames["TIME"] = "scene_data.time";
  1211. actions.renames["PI"] = _MKSTR(Math_PI);
  1212. actions.renames["TAU"] = _MKSTR(Math_TAU);
  1213. actions.renames["E"] = _MKSTR(Math_E);
  1214. actions.renames["VIEWPORT_SIZE"] = "scene_data.viewport_size";
  1215. actions.renames["FRAGCOORD"] = "gl_FragCoord";
  1216. actions.renames["FRONT_FACING"] = "gl_FrontFacing";
  1217. actions.renames["NORMAL_MAP"] = "normal_map";
  1218. actions.renames["NORMAL_MAP_DEPTH"] = "normal_map_depth";
  1219. actions.renames["ALBEDO"] = "albedo";
  1220. actions.renames["ALPHA"] = "alpha";
  1221. actions.renames["METALLIC"] = "metallic";
  1222. actions.renames["SPECULAR"] = "specular";
  1223. actions.renames["ROUGHNESS"] = "roughness";
  1224. actions.renames["RIM"] = "rim";
  1225. actions.renames["RIM_TINT"] = "rim_tint";
  1226. actions.renames["CLEARCOAT"] = "clearcoat";
  1227. actions.renames["CLEARCOAT_ROUGHNESS"] = "clearcoat_roughness";
  1228. actions.renames["ANISOTROPY"] = "anisotropy";
  1229. actions.renames["ANISOTROPY_FLOW"] = "anisotropy_flow";
  1230. actions.renames["SSS_STRENGTH"] = "sss_strength";
  1231. actions.renames["SSS_TRANSMITTANCE_COLOR"] = "transmittance_color";
  1232. actions.renames["SSS_TRANSMITTANCE_DEPTH"] = "transmittance_depth";
  1233. actions.renames["SSS_TRANSMITTANCE_BOOST"] = "transmittance_boost";
  1234. actions.renames["BACKLIGHT"] = "backlight";
  1235. actions.renames["AO"] = "ao";
  1236. actions.renames["AO_LIGHT_AFFECT"] = "ao_light_affect";
  1237. actions.renames["EMISSION"] = "emission";
  1238. actions.renames["POINT_COORD"] = "gl_PointCoord";
  1239. actions.renames["INSTANCE_CUSTOM"] = "instance_custom";
  1240. actions.renames["SCREEN_UV"] = "screen_uv";
  1241. actions.renames["SCREEN_TEXTURE"] = "color_buffer";
  1242. actions.renames["DEPTH_TEXTURE"] = "depth_buffer";
  1243. actions.renames["NORMAL_ROUGHNESS_TEXTURE"] = "normal_roughness_buffer";
  1244. actions.renames["DEPTH"] = "gl_FragDepth";
  1245. actions.renames["OUTPUT_IS_SRGB"] = "true";
  1246. actions.renames["FOG"] = "custom_fog";
  1247. actions.renames["RADIANCE"] = "custom_radiance";
  1248. actions.renames["IRRADIANCE"] = "custom_irradiance";
  1249. actions.renames["BONE_INDICES"] = "bone_attrib";
  1250. actions.renames["BONE_WEIGHTS"] = "weight_attrib";
  1251. actions.renames["CUSTOM0"] = "custom0_attrib";
  1252. actions.renames["CUSTOM1"] = "custom1_attrib";
  1253. actions.renames["CUSTOM2"] = "custom2_attrib";
  1254. actions.renames["CUSTOM3"] = "custom3_attrib";
  1255. actions.renames["OUTPUT_IS_SRGB"] = "SHADER_IS_SRGB";
  1256. actions.renames["VIEW_INDEX"] = "ViewIndex";
  1257. actions.renames["VIEW_MONO_LEFT"] = "0";
  1258. actions.renames["VIEW_RIGHT"] = "1";
  1259. //for light
  1260. actions.renames["VIEW"] = "view";
  1261. actions.renames["LIGHT_COLOR"] = "light_color";
  1262. actions.renames["LIGHT"] = "light";
  1263. actions.renames["ATTENUATION"] = "attenuation";
  1264. actions.renames["DIFFUSE_LIGHT"] = "diffuse_light";
  1265. actions.renames["SPECULAR_LIGHT"] = "specular_light";
  1266. actions.usage_defines["NORMAL"] = "#define NORMAL_USED\n";
  1267. actions.usage_defines["TANGENT"] = "#define TANGENT_USED\n";
  1268. actions.usage_defines["BINORMAL"] = "@TANGENT";
  1269. actions.usage_defines["RIM"] = "#define LIGHT_RIM_USED\n";
  1270. actions.usage_defines["RIM_TINT"] = "@RIM";
  1271. actions.usage_defines["CLEARCOAT"] = "#define LIGHT_CLEARCOAT_USED\n";
  1272. actions.usage_defines["CLEARCOAT_ROUGHNESS"] = "@CLEARCOAT";
  1273. actions.usage_defines["ANISOTROPY"] = "#define LIGHT_ANISOTROPY_USED\n";
  1274. actions.usage_defines["ANISOTROPY_FLOW"] = "@ANISOTROPY";
  1275. actions.usage_defines["AO"] = "#define AO_USED\n";
  1276. actions.usage_defines["AO_LIGHT_AFFECT"] = "#define AO_USED\n";
  1277. actions.usage_defines["UV"] = "#define UV_USED\n";
  1278. actions.usage_defines["UV2"] = "#define UV2_USED\n";
  1279. actions.usage_defines["BONE_INDICES"] = "#define BONES_USED\n";
  1280. actions.usage_defines["BONE_WEIGHTS"] = "#define WEIGHTS_USED\n";
  1281. actions.usage_defines["CUSTOM0"] = "#define CUSTOM0_USED\n";
  1282. actions.usage_defines["CUSTOM1"] = "#define CUSTOM1_USED\n";
  1283. actions.usage_defines["CUSTOM2"] = "#define CUSTOM2_USED\n";
  1284. actions.usage_defines["CUSTOM3"] = "#define CUSTOM3_USED\n";
  1285. actions.usage_defines["NORMAL_MAP"] = "#define NORMAL_MAP_USED\n";
  1286. actions.usage_defines["NORMAL_MAP_DEPTH"] = "@NORMAL_MAP";
  1287. actions.usage_defines["COLOR"] = "#define COLOR_USED\n";
  1288. actions.usage_defines["INSTANCE_CUSTOM"] = "#define ENABLE_INSTANCE_CUSTOM\n";
  1289. actions.usage_defines["POSITION"] = "#define OVERRIDE_POSITION\n";
  1290. actions.usage_defines["ALPHA_SCISSOR_THRESHOLD"] = "#define ALPHA_SCISSOR_USED\n";
  1291. actions.usage_defines["ALPHA_HASH_SCALE"] = "#define ALPHA_HASH_USED\n";
  1292. actions.usage_defines["ALPHA_ANTIALIASING_EDGE"] = "#define ALPHA_ANTIALIASING_EDGE_USED\n";
  1293. actions.usage_defines["ALPHA_TEXTURE_COORDINATE"] = "@ALPHA_ANTIALIASING_EDGE";
  1294. actions.usage_defines["SSS_STRENGTH"] = "#define ENABLE_SSS\n";
  1295. actions.usage_defines["SSS_TRANSMITTANCE_DEPTH"] = "#define ENABLE_TRANSMITTANCE\n";
  1296. actions.usage_defines["BACKLIGHT"] = "#define LIGHT_BACKLIGHT_USED\n";
  1297. actions.usage_defines["SCREEN_TEXTURE"] = "#define SCREEN_TEXTURE_USED\n";
  1298. actions.usage_defines["SCREEN_UV"] = "#define SCREEN_UV_USED\n";
  1299. actions.usage_defines["DIFFUSE_LIGHT"] = "#define USE_LIGHT_SHADER_CODE\n";
  1300. actions.usage_defines["SPECULAR_LIGHT"] = "#define USE_LIGHT_SHADER_CODE\n";
  1301. actions.usage_defines["FOG"] = "#define CUSTOM_FOG_USED\n";
  1302. actions.usage_defines["RADIANCE"] = "#define CUSTOM_RADIANCE_USED\n";
  1303. actions.usage_defines["IRRADIANCE"] = "#define CUSTOM_IRRADIANCE_USED\n";
  1304. actions.render_mode_defines["skip_vertex_transform"] = "#define SKIP_TRANSFORM_USED\n";
  1305. actions.render_mode_defines["world_vertex_coords"] = "#define VERTEX_WORLD_COORDS_USED\n";
  1306. actions.render_mode_defines["ensure_correct_normals"] = "#define ENSURE_CORRECT_NORMALS\n";
  1307. actions.render_mode_defines["cull_front"] = "#define DO_SIDE_CHECK\n";
  1308. actions.render_mode_defines["cull_disabled"] = "#define DO_SIDE_CHECK\n";
  1309. actions.render_mode_defines["particle_trails"] = "#define USE_PARTICLE_TRAILS\n";
  1310. actions.render_mode_defines["depth_draw_opaque"] = "#define USE_OPAQUE_PREPASS\n";
  1311. bool force_lambert = GLOBAL_GET("rendering/shading/overrides/force_lambert_over_burley");
  1312. if (!force_lambert) {
  1313. actions.render_mode_defines["diffuse_burley"] = "#define DIFFUSE_BURLEY\n";
  1314. }
  1315. actions.render_mode_defines["diffuse_lambert_wrap"] = "#define DIFFUSE_LAMBERT_WRAP\n";
  1316. actions.render_mode_defines["diffuse_toon"] = "#define DIFFUSE_TOON\n";
  1317. actions.render_mode_defines["sss_mode_skin"] = "#define SSS_MODE_SKIN\n";
  1318. actions.render_mode_defines["specular_schlick_ggx"] = "#define SPECULAR_SCHLICK_GGX\n";
  1319. actions.custom_samplers["SCREEN_TEXTURE"] = "material_samplers[3]"; // linear filter with mipmaps
  1320. actions.custom_samplers["DEPTH_TEXTURE"] = "material_samplers[3]";
  1321. actions.custom_samplers["NORMAL_ROUGHNESS_TEXTURE"] = "material_samplers[1]"; // linear filter
  1322. actions.render_mode_defines["specular_toon"] = "#define SPECULAR_TOON\n";
  1323. actions.render_mode_defines["specular_disabled"] = "#define SPECULAR_DISABLED\n";
  1324. actions.render_mode_defines["shadows_disabled"] = "#define SHADOWS_DISABLED\n";
  1325. actions.render_mode_defines["ambient_light_disabled"] = "#define AMBIENT_LIGHT_DISABLED\n";
  1326. actions.render_mode_defines["shadow_to_opacity"] = "#define USE_SHADOW_TO_OPACITY\n";
  1327. actions.render_mode_defines["unshaded"] = "#define MODE_UNSHADED\n";
  1328. actions.sampler_array_name = "material_samplers";
  1329. actions.base_texture_binding_index = 1;
  1330. actions.texture_layout_set = RenderForwardClustered::MATERIAL_UNIFORM_SET;
  1331. actions.base_uniform_string = "material.";
  1332. actions.base_varying_index = 10;
  1333. actions.default_filter = ShaderLanguage::FILTER_LINEAR_MIPMAP;
  1334. actions.default_repeat = ShaderLanguage::REPEAT_ENABLE;
  1335. actions.global_buffer_array_variable = "global_variables.data";
  1336. actions.instance_uniform_index_variable = "instances.data[instance_index].instance_uniforms_ofs";
  1337. compiler.initialize(actions);
  1338. */
  1339. }
  1340. {
  1341. // Setup Particles compiler
  1342. /*
  1343. ShaderCompiler::DefaultIdentifierActions actions;
  1344. actions.renames["COLOR"] = "PARTICLE.color";
  1345. actions.renames["VELOCITY"] = "PARTICLE.velocity";
  1346. //actions.renames["MASS"] = "mass"; ?
  1347. actions.renames["ACTIVE"] = "particle_active";
  1348. actions.renames["RESTART"] = "restart";
  1349. actions.renames["CUSTOM"] = "PARTICLE.custom";
  1350. for (int i = 0; i < ParticlesShader::MAX_USERDATAS; i++) {
  1351. String udname = "USERDATA" + itos(i + 1);
  1352. actions.renames[udname] = "PARTICLE.userdata" + itos(i + 1);
  1353. actions.usage_defines[udname] = "#define USERDATA" + itos(i + 1) + "_USED\n";
  1354. }
  1355. actions.renames["TRANSFORM"] = "PARTICLE.xform";
  1356. actions.renames["TIME"] = "frame_history.data[0].time";
  1357. actions.renames["PI"] = _MKSTR(Math_PI);
  1358. actions.renames["TAU"] = _MKSTR(Math_TAU);
  1359. actions.renames["E"] = _MKSTR(Math_E);
  1360. actions.renames["LIFETIME"] = "params.lifetime";
  1361. actions.renames["DELTA"] = "local_delta";
  1362. actions.renames["NUMBER"] = "particle_number";
  1363. actions.renames["INDEX"] = "index";
  1364. //actions.renames["GRAVITY"] = "current_gravity";
  1365. actions.renames["EMISSION_TRANSFORM"] = "FRAME.emission_transform";
  1366. actions.renames["RANDOM_SEED"] = "FRAME.random_seed";
  1367. actions.renames["FLAG_EMIT_POSITION"] = "EMISSION_FLAG_HAS_POSITION";
  1368. actions.renames["FLAG_EMIT_ROT_SCALE"] = "EMISSION_FLAG_HAS_ROTATION_SCALE";
  1369. actions.renames["FLAG_EMIT_VELOCITY"] = "EMISSION_FLAG_HAS_VELOCITY";
  1370. actions.renames["FLAG_EMIT_COLOR"] = "EMISSION_FLAG_HAS_COLOR";
  1371. actions.renames["FLAG_EMIT_CUSTOM"] = "EMISSION_FLAG_HAS_CUSTOM";
  1372. actions.renames["RESTART_POSITION"] = "restart_position";
  1373. actions.renames["RESTART_ROT_SCALE"] = "restart_rotation_scale";
  1374. actions.renames["RESTART_VELOCITY"] = "restart_velocity";
  1375. actions.renames["RESTART_COLOR"] = "restart_color";
  1376. actions.renames["RESTART_CUSTOM"] = "restart_custom";
  1377. actions.renames["emit_subparticle"] = "emit_subparticle";
  1378. actions.renames["COLLIDED"] = "collided";
  1379. actions.renames["COLLISION_NORMAL"] = "collision_normal";
  1380. actions.renames["COLLISION_DEPTH"] = "collision_depth";
  1381. actions.renames["ATTRACTOR_FORCE"] = "attractor_force";
  1382. actions.render_mode_defines["disable_force"] = "#define DISABLE_FORCE\n";
  1383. actions.render_mode_defines["disable_velocity"] = "#define DISABLE_VELOCITY\n";
  1384. actions.render_mode_defines["keep_data"] = "#define ENABLE_KEEP_DATA\n";
  1385. actions.render_mode_defines["collision_use_scale"] = "#define USE_COLLISON_SCALE\n";
  1386. actions.sampler_array_name = "material_samplers";
  1387. actions.base_texture_binding_index = 1;
  1388. actions.texture_layout_set = 3;
  1389. actions.base_uniform_string = "material.";
  1390. actions.base_varying_index = 10;
  1391. actions.default_filter = ShaderLanguage::FILTER_LINEAR_MIPMAP;
  1392. actions.default_repeat = ShaderLanguage::REPEAT_ENABLE;
  1393. actions.global_buffer_array_variable = "global_variables.data";
  1394. particles_shader.compiler.initialize(actions);
  1395. */
  1396. }
  1397. {
  1398. // Setup Sky compiler
  1399. ShaderCompiler::DefaultIdentifierActions actions;
  1400. actions.renames["COLOR"] = "color";
  1401. actions.renames["ALPHA"] = "alpha";
  1402. actions.renames["EYEDIR"] = "cube_normal";
  1403. actions.renames["POSITION"] = "params.position_multiplier.xyz";
  1404. actions.renames["SKY_COORDS"] = "panorama_coords";
  1405. actions.renames["SCREEN_UV"] = "uv";
  1406. actions.renames["TIME"] = "params.time";
  1407. actions.renames["PI"] = _MKSTR(Math_PI);
  1408. actions.renames["TAU"] = _MKSTR(Math_TAU);
  1409. actions.renames["E"] = _MKSTR(Math_E);
  1410. actions.renames["HALF_RES_COLOR"] = "half_res_color";
  1411. actions.renames["QUARTER_RES_COLOR"] = "quarter_res_color";
  1412. actions.renames["RADIANCE"] = "radiance";
  1413. actions.renames["FOG"] = "custom_fog";
  1414. actions.renames["LIGHT0_ENABLED"] = "directional_lights.data[0].enabled";
  1415. actions.renames["LIGHT0_DIRECTION"] = "directional_lights.data[0].direction_energy.xyz";
  1416. actions.renames["LIGHT0_ENERGY"] = "directional_lights.data[0].direction_energy.w";
  1417. actions.renames["LIGHT0_COLOR"] = "directional_lights.data[0].color_size.xyz";
  1418. actions.renames["LIGHT0_SIZE"] = "directional_lights.data[0].color_size.w";
  1419. actions.renames["LIGHT1_ENABLED"] = "directional_lights.data[1].enabled";
  1420. actions.renames["LIGHT1_DIRECTION"] = "directional_lights.data[1].direction_energy.xyz";
  1421. actions.renames["LIGHT1_ENERGY"] = "directional_lights.data[1].direction_energy.w";
  1422. actions.renames["LIGHT1_COLOR"] = "directional_lights.data[1].color_size.xyz";
  1423. actions.renames["LIGHT1_SIZE"] = "directional_lights.data[1].color_size.w";
  1424. actions.renames["LIGHT2_ENABLED"] = "directional_lights.data[2].enabled";
  1425. actions.renames["LIGHT2_DIRECTION"] = "directional_lights.data[2].direction_energy.xyz";
  1426. actions.renames["LIGHT2_ENERGY"] = "directional_lights.data[2].direction_energy.w";
  1427. actions.renames["LIGHT2_COLOR"] = "directional_lights.data[2].color_size.xyz";
  1428. actions.renames["LIGHT2_SIZE"] = "directional_lights.data[2].color_size.w";
  1429. actions.renames["LIGHT3_ENABLED"] = "directional_lights.data[3].enabled";
  1430. actions.renames["LIGHT3_DIRECTION"] = "directional_lights.data[3].direction_energy.xyz";
  1431. actions.renames["LIGHT3_ENERGY"] = "directional_lights.data[3].direction_energy.w";
  1432. actions.renames["LIGHT3_COLOR"] = "directional_lights.data[3].color_size.xyz";
  1433. actions.renames["LIGHT3_SIZE"] = "directional_lights.data[3].color_size.w";
  1434. actions.renames["AT_CUBEMAP_PASS"] = "AT_CUBEMAP_PASS";
  1435. actions.renames["AT_HALF_RES_PASS"] = "AT_HALF_RES_PASS";
  1436. actions.renames["AT_QUARTER_RES_PASS"] = "AT_QUARTER_RES_PASS";
  1437. actions.custom_samplers["RADIANCE"] = "material_samplers[3]";
  1438. actions.usage_defines["HALF_RES_COLOR"] = "\n#define USES_HALF_RES_COLOR\n";
  1439. actions.usage_defines["QUARTER_RES_COLOR"] = "\n#define USES_QUARTER_RES_COLOR\n";
  1440. actions.render_mode_defines["disable_fog"] = "#define DISABLE_FOG\n";
  1441. actions.sampler_array_name = "material_samplers";
  1442. actions.base_texture_binding_index = 1;
  1443. actions.texture_layout_set = 1;
  1444. actions.base_uniform_string = "material.";
  1445. actions.base_varying_index = 10;
  1446. actions.default_filter = ShaderLanguage::FILTER_LINEAR_MIPMAP;
  1447. actions.default_repeat = ShaderLanguage::REPEAT_ENABLE;
  1448. actions.global_buffer_array_variable = "global_variables";
  1449. shaders.compiler_sky.initialize(actions);
  1450. }
  1451. //shaders.copy.initialize();
  1452. //shaders.copy_version = shaders.copy.version_create(); //TODO
  1453. //shaders.copy.version_bind_shader(shaders.copy_version, CopyShaderGLES3::MODE_COPY_SECTION);
  1454. //shaders.cubemap_filter.init();
  1455. //bool ggx_hq = GLOBAL_GET("rendering/quality/reflections/high_quality_ggx");
  1456. //shaders.cubemap_filter.set_conditional(CubemapFilterShaderGLES3::LOW_QUALITY, !ggx_hq);
  1457. }
  1458. MaterialStorage::~MaterialStorage() {
  1459. //shaders.copy.version_free(shaders.copy_version);
  1460. memdelete_arr(global_variables.buffer_values);
  1461. memdelete_arr(global_variables.buffer_usage);
  1462. memdelete_arr(global_variables.buffer_dirty_regions);
  1463. glDeleteBuffers(1, &global_variables.buffer);
  1464. singleton = nullptr;
  1465. }
  1466. /* GLOBAL VARIABLE API */
  1467. int32_t MaterialStorage::_global_variable_allocate(uint32_t p_elements) {
  1468. int32_t idx = 0;
  1469. while (idx + p_elements <= global_variables.buffer_size) {
  1470. if (global_variables.buffer_usage[idx].elements == 0) {
  1471. bool valid = true;
  1472. for (uint32_t i = 1; i < p_elements; i++) {
  1473. if (global_variables.buffer_usage[idx + i].elements > 0) {
  1474. valid = false;
  1475. idx += i + global_variables.buffer_usage[idx + i].elements;
  1476. break;
  1477. }
  1478. }
  1479. if (!valid) {
  1480. continue; //if not valid, idx is in new position
  1481. }
  1482. return idx;
  1483. } else {
  1484. idx += global_variables.buffer_usage[idx].elements;
  1485. }
  1486. }
  1487. return -1;
  1488. }
  1489. void MaterialStorage::_global_variable_store_in_buffer(int32_t p_index, RS::GlobalVariableType p_type, const Variant &p_value) {
  1490. switch (p_type) {
  1491. case RS::GLOBAL_VAR_TYPE_BOOL: {
  1492. GlobalVariables::Value &bv = global_variables.buffer_values[p_index];
  1493. bool b = p_value;
  1494. bv.x = b ? 1.0 : 0.0;
  1495. bv.y = 0.0;
  1496. bv.z = 0.0;
  1497. bv.w = 0.0;
  1498. } break;
  1499. case RS::GLOBAL_VAR_TYPE_BVEC2: {
  1500. GlobalVariables::Value &bv = global_variables.buffer_values[p_index];
  1501. uint32_t bvec = p_value;
  1502. bv.x = (bvec & 1) ? 1.0 : 0.0;
  1503. bv.y = (bvec & 2) ? 1.0 : 0.0;
  1504. bv.z = 0.0;
  1505. bv.w = 0.0;
  1506. } break;
  1507. case RS::GLOBAL_VAR_TYPE_BVEC3: {
  1508. GlobalVariables::Value &bv = global_variables.buffer_values[p_index];
  1509. uint32_t bvec = p_value;
  1510. bv.x = (bvec & 1) ? 1.0 : 0.0;
  1511. bv.y = (bvec & 2) ? 1.0 : 0.0;
  1512. bv.z = (bvec & 4) ? 1.0 : 0.0;
  1513. bv.w = 0.0;
  1514. } break;
  1515. case RS::GLOBAL_VAR_TYPE_BVEC4: {
  1516. GlobalVariables::Value &bv = global_variables.buffer_values[p_index];
  1517. uint32_t bvec = p_value;
  1518. bv.x = (bvec & 1) ? 1.0 : 0.0;
  1519. bv.y = (bvec & 2) ? 1.0 : 0.0;
  1520. bv.z = (bvec & 4) ? 1.0 : 0.0;
  1521. bv.w = (bvec & 8) ? 1.0 : 0.0;
  1522. } break;
  1523. case RS::GLOBAL_VAR_TYPE_INT: {
  1524. GlobalVariables::ValueInt &bv = *(GlobalVariables::ValueInt *)&global_variables.buffer_values[p_index];
  1525. int32_t v = p_value;
  1526. bv.x = v;
  1527. bv.y = 0;
  1528. bv.z = 0;
  1529. bv.w = 0;
  1530. } break;
  1531. case RS::GLOBAL_VAR_TYPE_IVEC2: {
  1532. GlobalVariables::ValueInt &bv = *(GlobalVariables::ValueInt *)&global_variables.buffer_values[p_index];
  1533. Vector2i v = p_value;
  1534. bv.x = v.x;
  1535. bv.y = v.y;
  1536. bv.z = 0;
  1537. bv.w = 0;
  1538. } break;
  1539. case RS::GLOBAL_VAR_TYPE_IVEC3: {
  1540. GlobalVariables::ValueInt &bv = *(GlobalVariables::ValueInt *)&global_variables.buffer_values[p_index];
  1541. Vector3i v = p_value;
  1542. bv.x = v.x;
  1543. bv.y = v.y;
  1544. bv.z = v.z;
  1545. bv.w = 0;
  1546. } break;
  1547. case RS::GLOBAL_VAR_TYPE_IVEC4: {
  1548. GlobalVariables::ValueInt &bv = *(GlobalVariables::ValueInt *)&global_variables.buffer_values[p_index];
  1549. Vector<int32_t> v = p_value;
  1550. bv.x = v.size() >= 1 ? v[0] : 0;
  1551. bv.y = v.size() >= 2 ? v[1] : 0;
  1552. bv.z = v.size() >= 3 ? v[2] : 0;
  1553. bv.w = v.size() >= 4 ? v[3] : 0;
  1554. } break;
  1555. case RS::GLOBAL_VAR_TYPE_RECT2I: {
  1556. GlobalVariables::ValueInt &bv = *(GlobalVariables::ValueInt *)&global_variables.buffer_values[p_index];
  1557. Rect2i v = p_value;
  1558. bv.x = v.position.x;
  1559. bv.y = v.position.y;
  1560. bv.z = v.size.x;
  1561. bv.w = v.size.y;
  1562. } break;
  1563. case RS::GLOBAL_VAR_TYPE_UINT: {
  1564. GlobalVariables::ValueUInt &bv = *(GlobalVariables::ValueUInt *)&global_variables.buffer_values[p_index];
  1565. uint32_t v = p_value;
  1566. bv.x = v;
  1567. bv.y = 0;
  1568. bv.z = 0;
  1569. bv.w = 0;
  1570. } break;
  1571. case RS::GLOBAL_VAR_TYPE_UVEC2: {
  1572. GlobalVariables::ValueUInt &bv = *(GlobalVariables::ValueUInt *)&global_variables.buffer_values[p_index];
  1573. Vector2i v = p_value;
  1574. bv.x = v.x;
  1575. bv.y = v.y;
  1576. bv.z = 0;
  1577. bv.w = 0;
  1578. } break;
  1579. case RS::GLOBAL_VAR_TYPE_UVEC3: {
  1580. GlobalVariables::ValueUInt &bv = *(GlobalVariables::ValueUInt *)&global_variables.buffer_values[p_index];
  1581. Vector3i v = p_value;
  1582. bv.x = v.x;
  1583. bv.y = v.y;
  1584. bv.z = v.z;
  1585. bv.w = 0;
  1586. } break;
  1587. case RS::GLOBAL_VAR_TYPE_UVEC4: {
  1588. GlobalVariables::ValueUInt &bv = *(GlobalVariables::ValueUInt *)&global_variables.buffer_values[p_index];
  1589. Vector<int32_t> v = p_value;
  1590. bv.x = v.size() >= 1 ? v[0] : 0;
  1591. bv.y = v.size() >= 2 ? v[1] : 0;
  1592. bv.z = v.size() >= 3 ? v[2] : 0;
  1593. bv.w = v.size() >= 4 ? v[3] : 0;
  1594. } break;
  1595. case RS::GLOBAL_VAR_TYPE_FLOAT: {
  1596. GlobalVariables::Value &bv = global_variables.buffer_values[p_index];
  1597. float v = p_value;
  1598. bv.x = v;
  1599. bv.y = 0;
  1600. bv.z = 0;
  1601. bv.w = 0;
  1602. } break;
  1603. case RS::GLOBAL_VAR_TYPE_VEC2: {
  1604. GlobalVariables::Value &bv = global_variables.buffer_values[p_index];
  1605. Vector2 v = p_value;
  1606. bv.x = v.x;
  1607. bv.y = v.y;
  1608. bv.z = 0;
  1609. bv.w = 0;
  1610. } break;
  1611. case RS::GLOBAL_VAR_TYPE_VEC3: {
  1612. GlobalVariables::Value &bv = global_variables.buffer_values[p_index];
  1613. Vector3 v = p_value;
  1614. bv.x = v.x;
  1615. bv.y = v.y;
  1616. bv.z = v.z;
  1617. bv.w = 0;
  1618. } break;
  1619. case RS::GLOBAL_VAR_TYPE_VEC4: {
  1620. GlobalVariables::Value &bv = global_variables.buffer_values[p_index];
  1621. Plane v = p_value;
  1622. bv.x = v.normal.x;
  1623. bv.y = v.normal.y;
  1624. bv.z = v.normal.z;
  1625. bv.w = v.d;
  1626. } break;
  1627. case RS::GLOBAL_VAR_TYPE_COLOR: {
  1628. GlobalVariables::Value &bv = global_variables.buffer_values[p_index];
  1629. Color v = p_value;
  1630. bv.x = v.r;
  1631. bv.y = v.g;
  1632. bv.z = v.b;
  1633. bv.w = v.a;
  1634. GlobalVariables::Value &bv_linear = global_variables.buffer_values[p_index + 1];
  1635. v = v.srgb_to_linear();
  1636. bv_linear.x = v.r;
  1637. bv_linear.y = v.g;
  1638. bv_linear.z = v.b;
  1639. bv_linear.w = v.a;
  1640. } break;
  1641. case RS::GLOBAL_VAR_TYPE_RECT2: {
  1642. GlobalVariables::Value &bv = global_variables.buffer_values[p_index];
  1643. Rect2 v = p_value;
  1644. bv.x = v.position.x;
  1645. bv.y = v.position.y;
  1646. bv.z = v.size.x;
  1647. bv.w = v.size.y;
  1648. } break;
  1649. case RS::GLOBAL_VAR_TYPE_MAT2: {
  1650. GlobalVariables::Value *bv = &global_variables.buffer_values[p_index];
  1651. Vector<float> m2 = p_value;
  1652. if (m2.size() < 4) {
  1653. m2.resize(4);
  1654. }
  1655. bv[0].x = m2[0];
  1656. bv[0].y = m2[1];
  1657. bv[0].z = 0;
  1658. bv[0].w = 0;
  1659. bv[1].x = m2[2];
  1660. bv[1].y = m2[3];
  1661. bv[1].z = 0;
  1662. bv[1].w = 0;
  1663. } break;
  1664. case RS::GLOBAL_VAR_TYPE_MAT3: {
  1665. GlobalVariables::Value *bv = &global_variables.buffer_values[p_index];
  1666. Basis v = p_value;
  1667. bv[0].x = v.elements[0][0];
  1668. bv[0].y = v.elements[1][0];
  1669. bv[0].z = v.elements[2][0];
  1670. bv[0].w = 0;
  1671. bv[1].x = v.elements[0][1];
  1672. bv[1].y = v.elements[1][1];
  1673. bv[1].z = v.elements[2][1];
  1674. bv[1].w = 0;
  1675. bv[2].x = v.elements[0][2];
  1676. bv[2].y = v.elements[1][2];
  1677. bv[2].z = v.elements[2][2];
  1678. bv[2].w = 0;
  1679. } break;
  1680. case RS::GLOBAL_VAR_TYPE_MAT4: {
  1681. GlobalVariables::Value *bv = &global_variables.buffer_values[p_index];
  1682. Vector<float> m2 = p_value;
  1683. if (m2.size() < 16) {
  1684. m2.resize(16);
  1685. }
  1686. bv[0].x = m2[0];
  1687. bv[0].y = m2[1];
  1688. bv[0].z = m2[2];
  1689. bv[0].w = m2[3];
  1690. bv[1].x = m2[4];
  1691. bv[1].y = m2[5];
  1692. bv[1].z = m2[6];
  1693. bv[1].w = m2[7];
  1694. bv[2].x = m2[8];
  1695. bv[2].y = m2[9];
  1696. bv[2].z = m2[10];
  1697. bv[2].w = m2[11];
  1698. bv[3].x = m2[12];
  1699. bv[3].y = m2[13];
  1700. bv[3].z = m2[14];
  1701. bv[3].w = m2[15];
  1702. } break;
  1703. case RS::GLOBAL_VAR_TYPE_TRANSFORM_2D: {
  1704. GlobalVariables::Value *bv = &global_variables.buffer_values[p_index];
  1705. Transform2D v = p_value;
  1706. bv[0].x = v.columns[0][0];
  1707. bv[0].y = v.columns[0][1];
  1708. bv[0].z = 0;
  1709. bv[0].w = 0;
  1710. bv[1].x = v.columns[1][0];
  1711. bv[1].y = v.columns[1][1];
  1712. bv[1].z = 0;
  1713. bv[1].w = 0;
  1714. bv[2].x = v.columns[2][0];
  1715. bv[2].y = v.columns[2][1];
  1716. bv[2].z = 1;
  1717. bv[2].w = 0;
  1718. } break;
  1719. case RS::GLOBAL_VAR_TYPE_TRANSFORM: {
  1720. GlobalVariables::Value *bv = &global_variables.buffer_values[p_index];
  1721. Transform3D v = p_value;
  1722. bv[0].x = v.basis.elements[0][0];
  1723. bv[0].y = v.basis.elements[1][0];
  1724. bv[0].z = v.basis.elements[2][0];
  1725. bv[0].w = 0;
  1726. bv[1].x = v.basis.elements[0][1];
  1727. bv[1].y = v.basis.elements[1][1];
  1728. bv[1].z = v.basis.elements[2][1];
  1729. bv[1].w = 0;
  1730. bv[2].x = v.basis.elements[0][2];
  1731. bv[2].y = v.basis.elements[1][2];
  1732. bv[2].z = v.basis.elements[2][2];
  1733. bv[2].w = 0;
  1734. bv[3].x = v.origin.x;
  1735. bv[3].y = v.origin.y;
  1736. bv[3].z = v.origin.z;
  1737. bv[3].w = 1;
  1738. } break;
  1739. default: {
  1740. ERR_FAIL();
  1741. }
  1742. }
  1743. }
  1744. void MaterialStorage::_global_variable_mark_buffer_dirty(int32_t p_index, int32_t p_elements) {
  1745. int32_t prev_chunk = -1;
  1746. for (int32_t i = 0; i < p_elements; i++) {
  1747. int32_t chunk = (p_index + i) / GlobalVariables::BUFFER_DIRTY_REGION_SIZE;
  1748. if (chunk != prev_chunk) {
  1749. if (!global_variables.buffer_dirty_regions[chunk]) {
  1750. global_variables.buffer_dirty_regions[chunk] = true;
  1751. global_variables.buffer_dirty_region_count++;
  1752. }
  1753. }
  1754. prev_chunk = chunk;
  1755. }
  1756. }
  1757. void MaterialStorage::global_variable_add(const StringName &p_name, RS::GlobalVariableType p_type, const Variant &p_value) {
  1758. ERR_FAIL_COND(global_variables.variables.has(p_name));
  1759. GlobalVariables::Variable gv;
  1760. gv.type = p_type;
  1761. gv.value = p_value;
  1762. gv.buffer_index = -1;
  1763. if (p_type >= RS::GLOBAL_VAR_TYPE_SAMPLER2D) {
  1764. //is texture
  1765. global_variables.must_update_texture_materials = true; //normally there are none
  1766. } else {
  1767. gv.buffer_elements = 1;
  1768. if (p_type == RS::GLOBAL_VAR_TYPE_COLOR || p_type == RS::GLOBAL_VAR_TYPE_MAT2) {
  1769. //color needs to elements to store srgb and linear
  1770. gv.buffer_elements = 2;
  1771. }
  1772. if (p_type == RS::GLOBAL_VAR_TYPE_MAT3 || p_type == RS::GLOBAL_VAR_TYPE_TRANSFORM_2D) {
  1773. //color needs to elements to store srgb and linear
  1774. gv.buffer_elements = 3;
  1775. }
  1776. if (p_type == RS::GLOBAL_VAR_TYPE_MAT4 || p_type == RS::GLOBAL_VAR_TYPE_TRANSFORM) {
  1777. //color needs to elements to store srgb and linear
  1778. gv.buffer_elements = 4;
  1779. }
  1780. //is vector, allocate in buffer and update index
  1781. gv.buffer_index = _global_variable_allocate(gv.buffer_elements);
  1782. 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)));
  1783. global_variables.buffer_usage[gv.buffer_index].elements = gv.buffer_elements;
  1784. _global_variable_store_in_buffer(gv.buffer_index, gv.type, gv.value);
  1785. _global_variable_mark_buffer_dirty(gv.buffer_index, gv.buffer_elements);
  1786. global_variables.must_update_buffer_materials = true; //normally there are none
  1787. }
  1788. global_variables.variables[p_name] = gv;
  1789. }
  1790. void MaterialStorage::global_variable_remove(const StringName &p_name) {
  1791. if (!global_variables.variables.has(p_name)) {
  1792. return;
  1793. }
  1794. GlobalVariables::Variable &gv = global_variables.variables[p_name];
  1795. if (gv.buffer_index >= 0) {
  1796. global_variables.buffer_usage[gv.buffer_index].elements = 0;
  1797. global_variables.must_update_buffer_materials = true;
  1798. } else {
  1799. global_variables.must_update_texture_materials = true;
  1800. }
  1801. global_variables.variables.erase(p_name);
  1802. }
  1803. Vector<StringName> MaterialStorage::global_variable_get_list() const {
  1804. if (!Engine::get_singleton()->is_editor_hint()) {
  1805. ERR_FAIL_V_MSG(Vector<StringName>(), "This function should never be used outside the editor, it can severely damage performance.");
  1806. }
  1807. const StringName *K = nullptr;
  1808. Vector<StringName> names;
  1809. while ((K = global_variables.variables.next(K))) {
  1810. names.push_back(*K);
  1811. }
  1812. names.sort_custom<StringName::AlphCompare>();
  1813. return names;
  1814. }
  1815. void MaterialStorage::global_variable_set(const StringName &p_name, const Variant &p_value) {
  1816. ERR_FAIL_COND(!global_variables.variables.has(p_name));
  1817. GlobalVariables::Variable &gv = global_variables.variables[p_name];
  1818. gv.value = p_value;
  1819. if (gv.override.get_type() == Variant::NIL) {
  1820. if (gv.buffer_index >= 0) {
  1821. //buffer
  1822. _global_variable_store_in_buffer(gv.buffer_index, gv.type, gv.value);
  1823. _global_variable_mark_buffer_dirty(gv.buffer_index, gv.buffer_elements);
  1824. } else {
  1825. //texture
  1826. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  1827. for (Set<RID>::Element *E = gv.texture_materials.front(); E; E = E->next()) {
  1828. Material *material = material_storage->get_material(E->get());
  1829. ERR_CONTINUE(!material);
  1830. material_storage->_material_queue_update(material, false, true);
  1831. }
  1832. }
  1833. }
  1834. }
  1835. void MaterialStorage::global_variable_set_override(const StringName &p_name, const Variant &p_value) {
  1836. if (!global_variables.variables.has(p_name)) {
  1837. return; //variable may not exist
  1838. }
  1839. ERR_FAIL_COND(p_value.get_type() == Variant::OBJECT);
  1840. GlobalVariables::Variable &gv = global_variables.variables[p_name];
  1841. gv.override = p_value;
  1842. if (gv.buffer_index >= 0) {
  1843. //buffer
  1844. if (gv.override.get_type() == Variant::NIL) {
  1845. _global_variable_store_in_buffer(gv.buffer_index, gv.type, gv.value);
  1846. } else {
  1847. _global_variable_store_in_buffer(gv.buffer_index, gv.type, gv.override);
  1848. }
  1849. _global_variable_mark_buffer_dirty(gv.buffer_index, gv.buffer_elements);
  1850. } else {
  1851. //texture
  1852. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  1853. for (Set<RID>::Element *E = gv.texture_materials.front(); E; E = E->next()) {
  1854. Material *material = material_storage->get_material(E->get());
  1855. ERR_CONTINUE(!material);
  1856. material_storage->_material_queue_update(material, false, true);
  1857. }
  1858. }
  1859. }
  1860. Variant MaterialStorage::global_variable_get(const StringName &p_name) const {
  1861. if (!Engine::get_singleton()->is_editor_hint()) {
  1862. ERR_FAIL_V_MSG(Variant(), "This function should never be used outside the editor, it can severely damage performance.");
  1863. }
  1864. if (!global_variables.variables.has(p_name)) {
  1865. return Variant();
  1866. }
  1867. return global_variables.variables[p_name].value;
  1868. }
  1869. RS::GlobalVariableType MaterialStorage::global_variable_get_type_internal(const StringName &p_name) const {
  1870. if (!global_variables.variables.has(p_name)) {
  1871. return RS::GLOBAL_VAR_TYPE_MAX;
  1872. }
  1873. return global_variables.variables[p_name].type;
  1874. }
  1875. RS::GlobalVariableType MaterialStorage::global_variable_get_type(const StringName &p_name) const {
  1876. if (!Engine::get_singleton()->is_editor_hint()) {
  1877. ERR_FAIL_V_MSG(RS::GLOBAL_VAR_TYPE_MAX, "This function should never be used outside the editor, it can severely damage performance.");
  1878. }
  1879. return global_variable_get_type_internal(p_name);
  1880. }
  1881. void MaterialStorage::global_variables_load_settings(bool p_load_textures) {
  1882. List<PropertyInfo> settings;
  1883. ProjectSettings::get_singleton()->get_property_list(&settings);
  1884. for (const PropertyInfo &E : settings) {
  1885. if (E.name.begins_with("shader_globals/")) {
  1886. StringName name = E.name.get_slice("/", 1);
  1887. Dictionary d = ProjectSettings::get_singleton()->get(E.name);
  1888. ERR_CONTINUE(!d.has("type"));
  1889. ERR_CONTINUE(!d.has("value"));
  1890. String type = d["type"];
  1891. static const char *global_var_type_names[RS::GLOBAL_VAR_TYPE_MAX] = {
  1892. "bool",
  1893. "bvec2",
  1894. "bvec3",
  1895. "bvec4",
  1896. "int",
  1897. "ivec2",
  1898. "ivec3",
  1899. "ivec4",
  1900. "rect2i",
  1901. "uint",
  1902. "uvec2",
  1903. "uvec3",
  1904. "uvec4",
  1905. "float",
  1906. "vec2",
  1907. "vec3",
  1908. "vec4",
  1909. "color",
  1910. "rect2",
  1911. "mat2",
  1912. "mat3",
  1913. "mat4",
  1914. "transform_2d",
  1915. "transform",
  1916. "sampler2D",
  1917. "sampler2DArray",
  1918. "sampler3D",
  1919. "samplerCube",
  1920. };
  1921. RS::GlobalVariableType gvtype = RS::GLOBAL_VAR_TYPE_MAX;
  1922. for (int i = 0; i < RS::GLOBAL_VAR_TYPE_MAX; i++) {
  1923. if (global_var_type_names[i] == type) {
  1924. gvtype = RS::GlobalVariableType(i);
  1925. break;
  1926. }
  1927. }
  1928. ERR_CONTINUE(gvtype == RS::GLOBAL_VAR_TYPE_MAX); //type invalid
  1929. Variant value = d["value"];
  1930. if (gvtype >= RS::GLOBAL_VAR_TYPE_SAMPLER2D) {
  1931. //textire
  1932. if (!p_load_textures) {
  1933. value = RID();
  1934. continue;
  1935. }
  1936. String path = value;
  1937. RES resource = ResourceLoader::load(path);
  1938. ERR_CONTINUE(resource.is_null());
  1939. value = resource;
  1940. }
  1941. if (global_variables.variables.has(name)) {
  1942. //has it, update it
  1943. global_variable_set(name, value);
  1944. } else {
  1945. global_variable_add(name, gvtype, value);
  1946. }
  1947. }
  1948. }
  1949. }
  1950. void MaterialStorage::global_variables_clear() {
  1951. global_variables.variables.clear();
  1952. }
  1953. GLuint MaterialStorage::global_variables_get_uniform_buffer() const {
  1954. return global_variables.buffer;
  1955. }
  1956. int32_t MaterialStorage::global_variables_instance_allocate(RID p_instance) {
  1957. ERR_FAIL_COND_V(global_variables.instance_buffer_pos.has(p_instance), -1);
  1958. int32_t pos = _global_variable_allocate(ShaderLanguage::MAX_INSTANCE_UNIFORM_INDICES);
  1959. global_variables.instance_buffer_pos[p_instance] = pos; //save anyway
  1960. ERR_FAIL_COND_V_MSG(pos < 0, -1, "Too many instances using shader instance variables. Increase buffer size in Project Settings.");
  1961. global_variables.buffer_usage[pos].elements = ShaderLanguage::MAX_INSTANCE_UNIFORM_INDICES;
  1962. return pos;
  1963. }
  1964. void MaterialStorage::global_variables_instance_free(RID p_instance) {
  1965. ERR_FAIL_COND(!global_variables.instance_buffer_pos.has(p_instance));
  1966. int32_t pos = global_variables.instance_buffer_pos[p_instance];
  1967. if (pos >= 0) {
  1968. global_variables.buffer_usage[pos].elements = 0;
  1969. }
  1970. global_variables.instance_buffer_pos.erase(p_instance);
  1971. }
  1972. void MaterialStorage::global_variables_instance_update(RID p_instance, int p_index, const Variant &p_value) {
  1973. if (!global_variables.instance_buffer_pos.has(p_instance)) {
  1974. return; //just not allocated, ignore
  1975. }
  1976. int32_t pos = global_variables.instance_buffer_pos[p_instance];
  1977. if (pos < 0) {
  1978. return; //again, not allocated, ignore
  1979. }
  1980. ERR_FAIL_INDEX(p_index, ShaderLanguage::MAX_INSTANCE_UNIFORM_INDICES);
  1981. 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
  1982. ShaderLanguage::DataType datatype_from_value[Variant::COLOR + 1] = {
  1983. ShaderLanguage::TYPE_MAX, //nil
  1984. ShaderLanguage::TYPE_BOOL, //bool
  1985. ShaderLanguage::TYPE_INT, //int
  1986. ShaderLanguage::TYPE_FLOAT, //float
  1987. ShaderLanguage::TYPE_MAX, //string
  1988. ShaderLanguage::TYPE_VEC2, //vec2
  1989. ShaderLanguage::TYPE_IVEC2, //vec2i
  1990. ShaderLanguage::TYPE_VEC4, //rect2
  1991. ShaderLanguage::TYPE_IVEC4, //rect2i
  1992. ShaderLanguage::TYPE_VEC3, // vec3
  1993. ShaderLanguage::TYPE_IVEC3, //vec3i
  1994. ShaderLanguage::TYPE_MAX, //xform2d not supported here
  1995. ShaderLanguage::TYPE_VEC4, //plane
  1996. ShaderLanguage::TYPE_VEC4, //quat
  1997. ShaderLanguage::TYPE_MAX, //aabb not supported here
  1998. ShaderLanguage::TYPE_MAX, //basis not supported here
  1999. ShaderLanguage::TYPE_MAX, //xform not supported here
  2000. ShaderLanguage::TYPE_VEC4 //color
  2001. };
  2002. ShaderLanguage::DataType datatype = datatype_from_value[p_value.get_type()];
  2003. 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
  2004. pos += p_index;
  2005. _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
  2006. _global_variable_mark_buffer_dirty(pos, 1);
  2007. }
  2008. void MaterialStorage::_update_global_variables() {
  2009. MaterialStorage *material_storage = MaterialStorage::get_singleton();
  2010. if (global_variables.buffer_dirty_region_count > 0) {
  2011. uint32_t total_regions = global_variables.buffer_size / GlobalVariables::BUFFER_DIRTY_REGION_SIZE;
  2012. if (total_regions / global_variables.buffer_dirty_region_count <= 4) {
  2013. // 25% of regions dirty, just update all buffer
  2014. glBindBuffer(GL_UNIFORM_BUFFER, global_variables.buffer);
  2015. glBufferData(GL_UNIFORM_BUFFER, sizeof(GlobalVariables::Value) * global_variables.buffer_size, global_variables.buffer_values, GL_DYNAMIC_DRAW);
  2016. glBindBuffer(GL_UNIFORM_BUFFER, 0);
  2017. memset(global_variables.buffer_dirty_regions, 0, sizeof(bool) * total_regions);
  2018. } else {
  2019. uint32_t region_byte_size = sizeof(GlobalVariables::Value) * GlobalVariables::BUFFER_DIRTY_REGION_SIZE;
  2020. glBindBuffer(GL_UNIFORM_BUFFER, global_variables.buffer);
  2021. for (uint32_t i = 0; i < total_regions; i++) {
  2022. if (global_variables.buffer_dirty_regions[i]) {
  2023. glBufferSubData(GL_UNIFORM_BUFFER, i * region_byte_size, region_byte_size, &global_variables.buffer_values[i * GlobalVariables::BUFFER_DIRTY_REGION_SIZE]);
  2024. global_variables.buffer_dirty_regions[i] = false;
  2025. }
  2026. }
  2027. glBindBuffer(GL_UNIFORM_BUFFER, 0);
  2028. }
  2029. global_variables.buffer_dirty_region_count = 0;
  2030. }
  2031. if (global_variables.must_update_buffer_materials) {
  2032. // only happens in the case of a buffer variable added or removed,
  2033. // so not often.
  2034. for (const RID &E : global_variables.materials_using_buffer) {
  2035. Material *material = material_storage->get_material(E);
  2036. ERR_CONTINUE(!material); //wtf
  2037. material_storage->_material_queue_update(material, true, false);
  2038. }
  2039. global_variables.must_update_buffer_materials = false;
  2040. }
  2041. if (global_variables.must_update_texture_materials) {
  2042. // only happens in the case of a buffer variable added or removed,
  2043. // so not often.
  2044. for (const RID &E : global_variables.materials_using_texture) {
  2045. Material *material = material_storage->get_material(E);
  2046. ERR_CONTINUE(!material); //wtf
  2047. material_storage->_material_queue_update(material, false, true);
  2048. }
  2049. global_variables.must_update_texture_materials = false;
  2050. }
  2051. }
  2052. /* SHADER API */
  2053. RID MaterialStorage::shader_allocate() {
  2054. return shader_owner.allocate_rid();
  2055. }
  2056. void MaterialStorage::shader_initialize(RID p_rid) {
  2057. Shader shader;
  2058. shader.data = nullptr;
  2059. shader.mode = RS::SHADER_MAX;
  2060. shader_owner.initialize_rid(p_rid, shader);
  2061. }
  2062. void MaterialStorage::shader_free(RID p_rid) {
  2063. GLES3::Shader *shader = shader_owner.get_or_null(p_rid);
  2064. ERR_FAIL_COND(!shader);
  2065. //make material unreference this
  2066. while (shader->owners.size()) {
  2067. material_set_shader(shader->owners.front()->get()->self, RID());
  2068. }
  2069. //clear data if exists
  2070. if (shader->data) {
  2071. memdelete(shader->data);
  2072. }
  2073. shader_owner.free(p_rid);
  2074. }
  2075. void MaterialStorage::shader_set_code(RID p_shader, const String &p_code) {
  2076. GLES3::Shader *shader = shader_owner.get_or_null(p_shader);
  2077. ERR_FAIL_COND(!shader);
  2078. shader->code = p_code;
  2079. String mode_string = ShaderLanguage::get_shader_type(p_code);
  2080. RS::ShaderMode new_mode;
  2081. if (mode_string == "canvas_item") {
  2082. new_mode = RS::SHADER_CANVAS_ITEM;
  2083. //} else if (mode_string == "particles") {
  2084. // new_mode = RS::SHADER_PARTICLES;
  2085. } else if (mode_string == "spatial") {
  2086. new_mode = RS::SHADER_SPATIAL;
  2087. } else if (mode_string == "sky") {
  2088. new_mode = RS::SHADER_SKY;
  2089. //} else if (mode_string == "fog") {
  2090. // new_mode = RS::SHADER_FOG;
  2091. } else {
  2092. new_mode = RS::SHADER_MAX;
  2093. ERR_PRINT("shader type " + mode_string + " not supported in OpenGL renderer");
  2094. }
  2095. if (new_mode != shader->mode) {
  2096. if (shader->data) {
  2097. memdelete(shader->data);
  2098. shader->data = nullptr;
  2099. }
  2100. for (Set<Material *>::Element *E = shader->owners.front(); E; E = E->next()) {
  2101. Material *material = E->get();
  2102. material->shader_mode = new_mode;
  2103. if (material->data) {
  2104. memdelete(material->data);
  2105. material->data = nullptr;
  2106. }
  2107. }
  2108. shader->mode = new_mode;
  2109. if (new_mode < RS::SHADER_MAX && shader_data_request_func[new_mode]) {
  2110. shader->data = shader_data_request_func[new_mode]();
  2111. } else {
  2112. shader->mode = RS::SHADER_MAX; //invalid
  2113. }
  2114. for (Set<Material *>::Element *E = shader->owners.front(); E; E = E->next()) {
  2115. Material *material = E->get();
  2116. if (shader->data) {
  2117. material->data = material_data_request_func[new_mode](shader->data);
  2118. material->data->self = material->self;
  2119. material->data->set_next_pass(material->next_pass);
  2120. material->data->set_render_priority(material->priority);
  2121. }
  2122. material->shader_mode = new_mode;
  2123. }
  2124. if (shader->data) {
  2125. for (const KeyValue<StringName, Map<int, RID>> &E : shader->default_texture_parameter) {
  2126. for (const KeyValue<int, RID> &E2 : E.value) {
  2127. shader->data->set_default_texture_param(E.key, E2.value, E2.key);
  2128. }
  2129. }
  2130. }
  2131. }
  2132. if (shader->data) {
  2133. shader->data->set_code(p_code);
  2134. }
  2135. for (Set<Material *>::Element *E = shader->owners.front(); E; E = E->next()) {
  2136. Material *material = E->get();
  2137. material->dependency.changed_notify(RendererStorage::DEPENDENCY_CHANGED_MATERIAL);
  2138. _material_queue_update(material, true, true);
  2139. }
  2140. }
  2141. String MaterialStorage::shader_get_code(RID p_shader) const {
  2142. const GLES3::Shader *shader = shader_owner.get_or_null(p_shader);
  2143. ERR_FAIL_COND_V(!shader, String());
  2144. return shader->code;
  2145. }
  2146. void MaterialStorage::shader_get_param_list(RID p_shader, List<PropertyInfo> *p_param_list) const {
  2147. GLES3::Shader *shader = shader_owner.get_or_null(p_shader);
  2148. ERR_FAIL_COND(!shader);
  2149. if (shader->data) {
  2150. return shader->data->get_param_list(p_param_list);
  2151. }
  2152. }
  2153. void MaterialStorage::shader_set_default_texture_param(RID p_shader, const StringName &p_name, RID p_texture, int p_index) {
  2154. GLES3::Shader *shader = shader_owner.get_or_null(p_shader);
  2155. ERR_FAIL_COND(!shader);
  2156. if (p_texture.is_valid() && TextureStorage::get_singleton()->owns_texture(p_texture)) {
  2157. if (!shader->default_texture_parameter.has(p_name)) {
  2158. shader->default_texture_parameter[p_name] = Map<int, RID>();
  2159. }
  2160. shader->default_texture_parameter[p_name][p_index] = p_texture;
  2161. } else {
  2162. if (shader->default_texture_parameter.has(p_name) && shader->default_texture_parameter[p_name].has(p_index)) {
  2163. shader->default_texture_parameter[p_name].erase(p_index);
  2164. if (shader->default_texture_parameter[p_name].is_empty()) {
  2165. shader->default_texture_parameter.erase(p_name);
  2166. }
  2167. }
  2168. }
  2169. if (shader->data) {
  2170. shader->data->set_default_texture_param(p_name, p_texture, p_index);
  2171. }
  2172. for (Set<Material *>::Element *E = shader->owners.front(); E; E = E->next()) {
  2173. Material *material = E->get();
  2174. _material_queue_update(material, false, true);
  2175. }
  2176. }
  2177. RID MaterialStorage::shader_get_default_texture_param(RID p_shader, const StringName &p_name, int p_index) const {
  2178. const GLES3::Shader *shader = shader_owner.get_or_null(p_shader);
  2179. ERR_FAIL_COND_V(!shader, RID());
  2180. if (shader->default_texture_parameter.has(p_name) && shader->default_texture_parameter[p_name].has(p_index)) {
  2181. return shader->default_texture_parameter[p_name][p_index];
  2182. }
  2183. return RID();
  2184. }
  2185. Variant MaterialStorage::shader_get_param_default(RID p_shader, const StringName &p_param) const {
  2186. Shader *shader = shader_owner.get_or_null(p_shader);
  2187. ERR_FAIL_COND_V(!shader, Variant());
  2188. if (shader->data) {
  2189. return shader->data->get_default_parameter(p_param);
  2190. }
  2191. return Variant();
  2192. }
  2193. RS::ShaderNativeSourceCode MaterialStorage::shader_get_native_source_code(RID p_shader) const {
  2194. Shader *shader = shader_owner.get_or_null(p_shader);
  2195. ERR_FAIL_COND_V(!shader, RS::ShaderNativeSourceCode());
  2196. if (shader->data) {
  2197. return shader->data->get_native_source_code();
  2198. }
  2199. return RS::ShaderNativeSourceCode();
  2200. }
  2201. /* MATERIAL API */
  2202. void MaterialStorage::_material_queue_update(Material *material, bool p_uniform, bool p_texture) {
  2203. material->uniform_dirty = material->uniform_dirty || p_uniform;
  2204. material->texture_dirty = material->texture_dirty || p_texture;
  2205. if (material->update_element.in_list()) {
  2206. return;
  2207. }
  2208. material_update_list.add(&material->update_element);
  2209. }
  2210. void MaterialStorage::_update_queued_materials() {
  2211. while (material_update_list.first()) {
  2212. Material *material = material_update_list.first()->self();
  2213. if (material->data) {
  2214. material->data->update_parameters(material->params, material->uniform_dirty, material->texture_dirty);
  2215. }
  2216. material->texture_dirty = false;
  2217. material->uniform_dirty = false;
  2218. material_update_list.remove(&material->update_element);
  2219. }
  2220. }
  2221. RID MaterialStorage::material_allocate() {
  2222. return material_owner.allocate_rid();
  2223. }
  2224. void MaterialStorage::material_initialize(RID p_rid) {
  2225. material_owner.initialize_rid(p_rid);
  2226. Material *material = material_owner.get_or_null(p_rid);
  2227. material->self = p_rid;
  2228. }
  2229. void MaterialStorage::material_free(RID p_rid) {
  2230. Material *material = material_owner.get_or_null(p_rid);
  2231. ERR_FAIL_COND(!material);
  2232. material_set_shader(p_rid, RID()); //clean up shader
  2233. material->dependency.deleted_notify(p_rid);
  2234. material_owner.free(p_rid);
  2235. }
  2236. void MaterialStorage::material_set_shader(RID p_material, RID p_shader) {
  2237. GLES3::Material *material = material_owner.get_or_null(p_material);
  2238. ERR_FAIL_COND(!material);
  2239. if (material->data) {
  2240. memdelete(material->data);
  2241. material->data = nullptr;
  2242. }
  2243. if (material->shader) {
  2244. material->shader->owners.erase(material);
  2245. material->shader = nullptr;
  2246. material->shader_mode = RS::SHADER_MAX;
  2247. }
  2248. if (p_shader.is_null()) {
  2249. material->dependency.changed_notify(RendererStorage::DEPENDENCY_CHANGED_MATERIAL);
  2250. material->shader_id = 0;
  2251. return;
  2252. }
  2253. Shader *shader = get_shader(p_shader);
  2254. ERR_FAIL_COND(!shader);
  2255. material->shader = shader;
  2256. material->shader_mode = shader->mode;
  2257. material->shader_id = p_shader.get_local_index();
  2258. shader->owners.insert(material);
  2259. if (shader->mode == RS::SHADER_MAX) {
  2260. return;
  2261. }
  2262. ERR_FAIL_COND(shader->data == nullptr);
  2263. material->data = material_data_request_func[shader->mode](shader->data);
  2264. material->data->self = p_material;
  2265. material->data->set_next_pass(material->next_pass);
  2266. material->data->set_render_priority(material->priority);
  2267. //updating happens later
  2268. material->dependency.changed_notify(RendererStorage::DEPENDENCY_CHANGED_MATERIAL);
  2269. _material_queue_update(material, true, true);
  2270. }
  2271. void MaterialStorage::material_set_param(RID p_material, const StringName &p_param, const Variant &p_value) {
  2272. GLES3::Material *material = material_owner.get_or_null(p_material);
  2273. ERR_FAIL_COND(!material);
  2274. if (p_value.get_type() == Variant::NIL) {
  2275. material->params.erase(p_param);
  2276. } else {
  2277. ERR_FAIL_COND(p_value.get_type() == Variant::OBJECT); //object not allowed
  2278. material->params[p_param] = p_value;
  2279. }
  2280. if (material->shader && material->shader->data) { //shader is valid
  2281. bool is_texture = material->shader->data->is_param_texture(p_param);
  2282. _material_queue_update(material, !is_texture, is_texture);
  2283. } else {
  2284. _material_queue_update(material, true, true);
  2285. }
  2286. }
  2287. Variant MaterialStorage::material_get_param(RID p_material, const StringName &p_param) const {
  2288. const GLES3::Material *material = material_owner.get_or_null(p_material);
  2289. ERR_FAIL_COND_V(!material, Variant());
  2290. if (material->params.has(p_param)) {
  2291. return material->params[p_param];
  2292. } else {
  2293. return Variant();
  2294. }
  2295. }
  2296. void MaterialStorage::material_set_next_pass(RID p_material, RID p_next_material) {
  2297. GLES3::Material *material = material_owner.get_or_null(p_material);
  2298. ERR_FAIL_COND(!material);
  2299. if (material->next_pass == p_next_material) {
  2300. return;
  2301. }
  2302. material->next_pass = p_next_material;
  2303. if (material->data) {
  2304. material->data->set_next_pass(p_next_material);
  2305. }
  2306. material->dependency.changed_notify(RendererStorage::DEPENDENCY_CHANGED_MATERIAL);
  2307. }
  2308. void MaterialStorage::material_set_render_priority(RID p_material, int priority) {
  2309. ERR_FAIL_COND(priority < RS::MATERIAL_RENDER_PRIORITY_MIN);
  2310. ERR_FAIL_COND(priority > RS::MATERIAL_RENDER_PRIORITY_MAX);
  2311. GLES3::Material *material = material_owner.get_or_null(p_material);
  2312. ERR_FAIL_COND(!material);
  2313. material->priority = priority;
  2314. if (material->data) {
  2315. material->data->set_render_priority(priority);
  2316. }
  2317. }
  2318. bool MaterialStorage::material_is_animated(RID p_material) {
  2319. GLES3::Material *material = material_owner.get_or_null(p_material);
  2320. ERR_FAIL_COND_V(!material, false);
  2321. if (material->shader && material->shader->data) {
  2322. if (material->shader->data->is_animated()) {
  2323. return true;
  2324. } else if (material->next_pass.is_valid()) {
  2325. return material_is_animated(material->next_pass);
  2326. }
  2327. }
  2328. return false; //by default nothing is animated
  2329. }
  2330. bool MaterialStorage::material_casts_shadows(RID p_material) {
  2331. GLES3::Material *material = material_owner.get_or_null(p_material);
  2332. ERR_FAIL_COND_V(!material, true);
  2333. if (material->shader && material->shader->data) {
  2334. if (material->shader->data->casts_shadows()) {
  2335. return true;
  2336. } else if (material->next_pass.is_valid()) {
  2337. return material_casts_shadows(material->next_pass);
  2338. }
  2339. }
  2340. return true; //by default everything casts shadows
  2341. }
  2342. void MaterialStorage::material_get_instance_shader_parameters(RID p_material, List<InstanceShaderParam> *r_parameters) {
  2343. GLES3::Material *material = material_owner.get_or_null(p_material);
  2344. ERR_FAIL_COND(!material);
  2345. if (material->shader && material->shader->data) {
  2346. material->shader->data->get_instance_param_list(r_parameters);
  2347. if (material->next_pass.is_valid()) {
  2348. material_get_instance_shader_parameters(material->next_pass, r_parameters);
  2349. }
  2350. }
  2351. }
  2352. void MaterialStorage::material_update_dependency(RID p_material, RendererStorage::DependencyTracker *p_instance) {
  2353. Material *material = material_owner.get_or_null(p_material);
  2354. ERR_FAIL_COND(!material);
  2355. p_instance->update_dependency(&material->dependency);
  2356. if (material->next_pass.is_valid()) {
  2357. material_update_dependency(material->next_pass, p_instance);
  2358. }
  2359. }
  2360. // Canvas Shader Data
  2361. void CanvasShaderData::set_code(const String &p_code) {
  2362. // compile the shader
  2363. code = p_code;
  2364. valid = false;
  2365. ubo_size = 0;
  2366. uniforms.clear();
  2367. uses_screen_texture = false;
  2368. uses_sdf = false;
  2369. uses_time = false;
  2370. if (code.is_empty()) {
  2371. return; //just invalid, but no error
  2372. }
  2373. ShaderCompiler::GeneratedCode gen_code;
  2374. int blend_mode = BLEND_MODE_MIX;
  2375. uses_screen_texture = false;
  2376. ShaderCompiler::IdentifierActions actions;
  2377. actions.entry_point_stages["vertex"] = ShaderCompiler::STAGE_VERTEX;
  2378. actions.entry_point_stages["fragment"] = ShaderCompiler::STAGE_FRAGMENT;
  2379. actions.entry_point_stages["light"] = ShaderCompiler::STAGE_FRAGMENT;
  2380. actions.render_mode_values["blend_add"] = Pair<int *, int>(&blend_mode, BLEND_MODE_ADD);
  2381. actions.render_mode_values["blend_mix"] = Pair<int *, int>(&blend_mode, BLEND_MODE_MIX);
  2382. actions.render_mode_values["blend_sub"] = Pair<int *, int>(&blend_mode, BLEND_MODE_SUB);
  2383. actions.render_mode_values["blend_mul"] = Pair<int *, int>(&blend_mode, BLEND_MODE_MUL);
  2384. actions.render_mode_values["blend_premul_alpha"] = Pair<int *, int>(&blend_mode, BLEND_MODE_PMALPHA);
  2385. actions.render_mode_values["blend_disabled"] = Pair<int *, int>(&blend_mode, BLEND_MODE_DISABLED);
  2386. actions.usage_flag_pointers["SCREEN_TEXTURE"] = &uses_screen_texture;
  2387. actions.usage_flag_pointers["texture_sdf"] = &uses_sdf;
  2388. actions.usage_flag_pointers["TIME"] = &uses_time;
  2389. actions.uniforms = &uniforms;
  2390. Error err = MaterialStorage::get_singleton()->shaders.compiler_canvas.compile(RS::SHADER_CANVAS_ITEM, code, &actions, path, gen_code);
  2391. ERR_FAIL_COND_MSG(err != OK, "Shader compilation failed.");
  2392. if (version.is_null()) {
  2393. version = MaterialStorage::get_singleton()->shaders.canvas_shader.version_create();
  2394. }
  2395. #if 0
  2396. print_line("**compiling shader:");
  2397. print_line("**defines:\n");
  2398. for (int i = 0; i < gen_code.defines.size(); i++) {
  2399. print_line(gen_code.defines[i]);
  2400. }
  2401. print_line("\n**uniforms:\n" + gen_code.uniforms);
  2402. print_line("\n**vertex_globals:\n" + gen_code.vertex_global);
  2403. print_line("\n**vertex_code:\n" + gen_code.vertex);
  2404. print_line("\n**fragment_globals:\n" + gen_code.fragment_global);
  2405. print_line("\n**fragment_code:\n" + gen_code.fragment);
  2406. print_line("\n**light_code:\n" + gen_code.light);
  2407. #endif
  2408. Vector<StringName> texture_uniform_names;
  2409. for (int i = 0; i < gen_code.texture_uniforms.size(); i++) {
  2410. texture_uniform_names.push_back(gen_code.texture_uniforms[i].name);
  2411. }
  2412. MaterialStorage::get_singleton()->shaders.canvas_shader.version_set_code(version, gen_code.code, gen_code.uniforms, gen_code.stage_globals[ShaderCompiler::STAGE_VERTEX], gen_code.stage_globals[ShaderCompiler::STAGE_FRAGMENT], gen_code.defines, texture_uniform_names);
  2413. ERR_FAIL_COND(!MaterialStorage::get_singleton()->shaders.canvas_shader.version_is_valid(version));
  2414. ubo_size = gen_code.uniform_total_size;
  2415. ubo_offsets = gen_code.uniform_offsets;
  2416. texture_uniforms = gen_code.texture_uniforms;
  2417. valid = true;
  2418. }
  2419. void CanvasShaderData::set_default_texture_param(const StringName &p_name, RID p_texture, int p_index) {
  2420. if (!p_texture.is_valid()) {
  2421. if (default_texture_params.has(p_name) && default_texture_params[p_name].has(p_index)) {
  2422. default_texture_params[p_name].erase(p_index);
  2423. if (default_texture_params[p_name].is_empty()) {
  2424. default_texture_params.erase(p_name);
  2425. }
  2426. }
  2427. } else {
  2428. if (!default_texture_params.has(p_name)) {
  2429. default_texture_params[p_name] = Map<int, RID>();
  2430. }
  2431. default_texture_params[p_name][p_index] = p_texture;
  2432. }
  2433. }
  2434. void CanvasShaderData::get_param_list(List<PropertyInfo> *p_param_list) const {
  2435. Map<int, StringName> order;
  2436. for (const KeyValue<StringName, ShaderLanguage::ShaderNode::Uniform> &E : uniforms) {
  2437. if (E.value.scope != ShaderLanguage::ShaderNode::Uniform::SCOPE_LOCAL) {
  2438. continue;
  2439. }
  2440. if (E.value.texture_order >= 0) {
  2441. order[E.value.texture_order + 100000] = E.key;
  2442. } else {
  2443. order[E.value.order] = E.key;
  2444. }
  2445. }
  2446. for (const KeyValue<int, StringName> &E : order) {
  2447. PropertyInfo pi = ShaderLanguage::uniform_to_property_info(uniforms[E.value]);
  2448. pi.name = E.value;
  2449. p_param_list->push_back(pi);
  2450. }
  2451. }
  2452. void CanvasShaderData::get_instance_param_list(List<RendererMaterialStorage::InstanceShaderParam> *p_param_list) const {
  2453. for (const KeyValue<StringName, ShaderLanguage::ShaderNode::Uniform> &E : uniforms) {
  2454. if (E.value.scope != ShaderLanguage::ShaderNode::Uniform::SCOPE_INSTANCE) {
  2455. continue;
  2456. }
  2457. RendererMaterialStorage::InstanceShaderParam p;
  2458. p.info = ShaderLanguage::uniform_to_property_info(E.value);
  2459. p.info.name = E.key; //supply name
  2460. p.index = E.value.instance_index;
  2461. p.default_value = ShaderLanguage::constant_value_to_variant(E.value.default_value, E.value.type, E.value.array_size, E.value.hint);
  2462. p_param_list->push_back(p);
  2463. }
  2464. }
  2465. bool CanvasShaderData::is_param_texture(const StringName &p_param) const {
  2466. if (!uniforms.has(p_param)) {
  2467. return false;
  2468. }
  2469. return uniforms[p_param].texture_order >= 0;
  2470. }
  2471. bool CanvasShaderData::is_animated() const {
  2472. return false;
  2473. }
  2474. bool CanvasShaderData::casts_shadows() const {
  2475. return false;
  2476. }
  2477. Variant CanvasShaderData::get_default_parameter(const StringName &p_parameter) const {
  2478. if (uniforms.has(p_parameter)) {
  2479. ShaderLanguage::ShaderNode::Uniform uniform = uniforms[p_parameter];
  2480. Vector<ShaderLanguage::ConstantNode::Value> default_value = uniform.default_value;
  2481. return ShaderLanguage::constant_value_to_variant(default_value, uniform.type, uniform.array_size, uniform.hint);
  2482. }
  2483. return Variant();
  2484. }
  2485. RS::ShaderNativeSourceCode CanvasShaderData::get_native_source_code() const {
  2486. return MaterialStorage::get_singleton()->shaders.canvas_shader.version_get_native_source_code(version);
  2487. }
  2488. CanvasShaderData::CanvasShaderData() {
  2489. valid = false;
  2490. uses_screen_texture = false;
  2491. uses_sdf = false;
  2492. }
  2493. CanvasShaderData::~CanvasShaderData() {
  2494. if (version.is_valid()) {
  2495. MaterialStorage::get_singleton()->shaders.canvas_shader.version_free(version);
  2496. }
  2497. }
  2498. GLES3::ShaderData *GLES3::_create_canvas_shader_func() {
  2499. CanvasShaderData *shader_data = memnew(CanvasShaderData);
  2500. return shader_data;
  2501. }
  2502. void CanvasMaterialData::update_parameters(const Map<StringName, Variant> &p_parameters, bool p_uniform_dirty, bool p_textures_dirty) {
  2503. return update_parameters_internal(p_parameters, p_uniform_dirty, p_textures_dirty, shader_data->uniforms, shader_data->ubo_offsets.ptr(), shader_data->texture_uniforms, shader_data->default_texture_params, shader_data->ubo_size);
  2504. }
  2505. // Look up table to translate ShaderLanguage::DataType to GL_TEXTURE_*
  2506. static const GLenum target_from_type[ShaderLanguage::TYPE_MAX] = {
  2507. GL_TEXTURE_2D, // TYPE_VOID,
  2508. GL_TEXTURE_2D, // TYPE_BOOL,
  2509. GL_TEXTURE_2D, // TYPE_BVEC2,
  2510. GL_TEXTURE_2D, // TYPE_BVEC3,
  2511. GL_TEXTURE_2D, // TYPE_BVEC4,
  2512. GL_TEXTURE_2D, // TYPE_INT,
  2513. GL_TEXTURE_2D, // TYPE_IVEC2,
  2514. GL_TEXTURE_2D, // TYPE_IVEC3,
  2515. GL_TEXTURE_2D, // TYPE_IVEC4,
  2516. GL_TEXTURE_2D, // TYPE_UINT,
  2517. GL_TEXTURE_2D, // TYPE_UVEC2,
  2518. GL_TEXTURE_2D, // TYPE_UVEC3,
  2519. GL_TEXTURE_2D, // TYPE_UVEC4,
  2520. GL_TEXTURE_2D, // TYPE_FLOAT,
  2521. GL_TEXTURE_2D, // TYPE_VEC2,
  2522. GL_TEXTURE_2D, // TYPE_VEC3,
  2523. GL_TEXTURE_2D, // TYPE_VEC4,
  2524. GL_TEXTURE_2D, // TYPE_MAT2,
  2525. GL_TEXTURE_2D, // TYPE_MAT3,
  2526. GL_TEXTURE_2D, // TYPE_MAT4,
  2527. GL_TEXTURE_2D, // TYPE_SAMPLER2D,
  2528. GL_TEXTURE_2D, // TYPE_ISAMPLER2D,
  2529. GL_TEXTURE_2D, // TYPE_USAMPLER2D,
  2530. GL_TEXTURE_2D_ARRAY, // TYPE_SAMPLER2DARRAY,
  2531. GL_TEXTURE_2D_ARRAY, // TYPE_ISAMPLER2DARRAY,
  2532. GL_TEXTURE_2D_ARRAY, // TYPE_USAMPLER2DARRAY,
  2533. GL_TEXTURE_3D, // TYPE_SAMPLER3D,
  2534. GL_TEXTURE_3D, // TYPE_ISAMPLER3D,
  2535. GL_TEXTURE_3D, // TYPE_USAMPLER3D,
  2536. GL_TEXTURE_CUBE_MAP, // TYPE_SAMPLERCUBE,
  2537. GL_TEXTURE_CUBE_MAP, // TYPE_SAMPLERCUBEARRAY,
  2538. GL_TEXTURE_2D, // TYPE_STRUCT
  2539. };
  2540. void CanvasMaterialData::bind_uniforms() {
  2541. // Bind Material Uniforms
  2542. glBindBufferBase(GL_UNIFORM_BUFFER, RasterizerCanvasGLES3::MATERIAL_UNIFORM_BUFFER_OBJECT, uniform_buffer);
  2543. RID *textures = texture_cache.ptrw();
  2544. ShaderCompiler::GeneratedCode::Texture *texture_uniforms = shader_data->texture_uniforms.ptrw();
  2545. for (int ti = 0; ti < texture_cache.size(); ti++) {
  2546. Texture *texture = TextureStorage::get_singleton()->get_texture(textures[ti]);
  2547. glActiveTexture(GL_TEXTURE1 + ti);
  2548. glBindTexture(target_from_type[texture_uniforms[ti].type], texture->tex_id);
  2549. // Set sampler state here as the same texture can be used in multiple places with different flags
  2550. // Need to convert sampler state from ShaderLanguage::Texture* to RS::CanvasItemTexture*
  2551. RS::CanvasItemTextureFilter filter = RS::CanvasItemTextureFilter((int(texture_uniforms[ti].filter) + 1) % RS::CANVAS_ITEM_TEXTURE_FILTER_MAX);
  2552. RS::CanvasItemTextureRepeat repeat = RS::CanvasItemTextureRepeat((int(texture_uniforms[ti].repeat) + 1) % RS::CANVAS_ITEM_TEXTURE_REPEAT_MIRROR);
  2553. texture->gl_set_filter(filter);
  2554. texture->gl_set_repeat(repeat);
  2555. }
  2556. }
  2557. CanvasMaterialData::~CanvasMaterialData() {
  2558. }
  2559. GLES3::MaterialData *GLES3::_create_canvas_material_func(ShaderData *p_shader) {
  2560. CanvasMaterialData *material_data = memnew(CanvasMaterialData);
  2561. material_data->shader_data = static_cast<CanvasShaderData *>(p_shader);
  2562. //update will happen later anyway so do nothing.
  2563. return material_data;
  2564. }
  2565. #endif // !GLES3_ENABLED