material_storage.cpp 116 KB

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