shader_language.cpp 137 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743
  1. /*************************************************************************/
  2. /* shader_language.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "shader_language.h"
  31. #include "core/os/os.h"
  32. #include "core/print_string.h"
  33. static bool _is_text_char(CharType c) {
  34. return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_';
  35. }
  36. static bool _is_number(CharType c) {
  37. return (c >= '0' && c <= '9');
  38. }
  39. static bool _is_hex(CharType c) {
  40. return (c >= '0' && c <= '9') || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F');
  41. }
  42. String ShaderLanguage::get_operator_text(Operator p_op) {
  43. static const char *op_names[OP_MAX] = { "==",
  44. "!=",
  45. "<",
  46. "<=",
  47. ">",
  48. ">=",
  49. "&&",
  50. "||",
  51. "!",
  52. "-",
  53. "+",
  54. "-",
  55. "*",
  56. "/",
  57. "%",
  58. "<<",
  59. ">>",
  60. "=",
  61. "+=",
  62. "-=",
  63. "*=",
  64. "/=",
  65. "%=",
  66. "<<=",
  67. ">>=",
  68. "&=",
  69. "|=",
  70. "^=",
  71. "&",
  72. "|",
  73. "^",
  74. "~",
  75. "++",
  76. "--",
  77. "?",
  78. ":",
  79. "++",
  80. "--",
  81. "()",
  82. "construct",
  83. "index" };
  84. return op_names[p_op];
  85. }
  86. const char *ShaderLanguage::token_names[TK_MAX] = {
  87. "EMPTY",
  88. "IDENTIFIER",
  89. "TRUE",
  90. "FALSE",
  91. "REAL_CONSTANT",
  92. "INT_CONSTANT",
  93. "TYPE_VOID",
  94. "TYPE_BOOL",
  95. "TYPE_BVEC2",
  96. "TYPE_BVEC3",
  97. "TYPE_BVEC4",
  98. "TYPE_INT",
  99. "TYPE_IVEC2",
  100. "TYPE_IVEC3",
  101. "TYPE_IVEC4",
  102. "TYPE_UINT",
  103. "TYPE_UVEC2",
  104. "TYPE_UVEC3",
  105. "TYPE_UVEC4",
  106. "TYPE_FLOAT",
  107. "TYPE_VEC2",
  108. "TYPE_VEC3",
  109. "TYPE_VEC4",
  110. "TYPE_MAT2",
  111. "TYPE_MAT3",
  112. "TYPE_MAT4",
  113. "TYPE_SAMPLER2D",
  114. "TYPE_ISAMPLER2D",
  115. "TYPE_USAMPLER2D",
  116. "TYPE_SAMPLER2DARRAY",
  117. "TYPE_ISAMPLER2DARRAY",
  118. "TYPE_USAMPLER2DARRAY",
  119. "TYPE_SAMPLER3D",
  120. "TYPE_ISAMPLER3D",
  121. "TYPE_USAMPLER3D",
  122. "TYPE_SAMPLERCUBE",
  123. "INTERPOLATION_FLAT",
  124. "INTERPOLATION_NO_PERSPECTIVE",
  125. "INTERPOLATION_SMOOTH",
  126. "PRECISION_LOW",
  127. "PRECISION_MID",
  128. "PRECISION_HIGH",
  129. "OP_EQUAL",
  130. "OP_NOT_EQUAL",
  131. "OP_LESS",
  132. "OP_LESS_EQUAL",
  133. "OP_GREATER",
  134. "OP_GREATER_EQUAL",
  135. "OP_AND",
  136. "OP_OR",
  137. "OP_NOT",
  138. "OP_ADD",
  139. "OP_SUB",
  140. "OP_MUL",
  141. "OP_DIV",
  142. "OP_MOD",
  143. "OP_SHIFT_LEFT",
  144. "OP_SHIFT_RIGHT",
  145. "OP_ASSIGN",
  146. "OP_ASSIGN_ADD",
  147. "OP_ASSIGN_SUB",
  148. "OP_ASSIGN_MUL",
  149. "OP_ASSIGN_DIV",
  150. "OP_ASSIGN_MOD",
  151. "OP_ASSIGN_SHIFT_LEFT",
  152. "OP_ASSIGN_SHIFT_RIGHT",
  153. "OP_ASSIGN_BIT_AND",
  154. "OP_ASSIGN_BIT_OR",
  155. "OP_ASSIGN_BIT_XOR",
  156. "OP_BIT_AND",
  157. "OP_BIT_OR",
  158. "OP_BIT_XOR",
  159. "OP_BIT_INVERT",
  160. "OP_INCREMENT",
  161. "OP_DECREMENT",
  162. "CF_IF",
  163. "CF_ELSE",
  164. "CF_FOR",
  165. "CF_WHILE",
  166. "CF_DO",
  167. "CF_SWITCH",
  168. "CF_CASE",
  169. "CF_BREAK",
  170. "CF_CONTINUE",
  171. "CF_RETURN",
  172. "CF_DISCARD",
  173. "BRACKET_OPEN",
  174. "BRACKET_CLOSE",
  175. "CURLY_BRACKET_OPEN",
  176. "CURLY_BRACKET_CLOSE",
  177. "PARENTHESIS_OPEN",
  178. "PARENTHESIS_CLOSE",
  179. "QUESTION",
  180. "COMMA",
  181. "COLON",
  182. "SEMICOLON",
  183. "PERIOD",
  184. "UNIFORM",
  185. "VARYING",
  186. "IN",
  187. "OUT",
  188. "INOUT",
  189. "RENDER_MODE",
  190. "HINT_WHITE_TEXTURE",
  191. "HINT_BLACK_TEXTURE",
  192. "HINT_NORMAL_TEXTURE",
  193. "HINT_ANISO_TEXTURE",
  194. "HINT_ALBEDO_TEXTURE",
  195. "HINT_BLACK_ALBEDO_TEXTURE",
  196. "HINT_COLOR",
  197. "HINT_RANGE",
  198. "SHADER_TYPE",
  199. "CURSOR",
  200. "ERROR",
  201. "EOF",
  202. };
  203. String ShaderLanguage::get_token_text(Token p_token) {
  204. String name = token_names[p_token.type];
  205. if (p_token.type == TK_INT_CONSTANT || p_token.type == TK_REAL_CONSTANT) {
  206. name += "(" + rtos(p_token.constant) + ")";
  207. } else if (p_token.type == TK_IDENTIFIER) {
  208. name += "(" + String(p_token.text) + ")";
  209. } else if (p_token.type == TK_ERROR) {
  210. name += "(" + String(p_token.text) + ")";
  211. }
  212. return name;
  213. }
  214. ShaderLanguage::Token ShaderLanguage::_make_token(TokenType p_type, const StringName &p_text) {
  215. Token tk;
  216. tk.type = p_type;
  217. tk.text = p_text;
  218. tk.line = tk_line;
  219. if (tk.type == TK_ERROR) {
  220. _set_error(p_text);
  221. }
  222. return tk;
  223. }
  224. const ShaderLanguage::KeyWord ShaderLanguage::keyword_list[] = {
  225. { TK_TRUE, "true" },
  226. { TK_FALSE, "false" },
  227. { TK_TYPE_VOID, "void" },
  228. { TK_TYPE_BOOL, "bool" },
  229. { TK_TYPE_BVEC2, "bvec2" },
  230. { TK_TYPE_BVEC3, "bvec3" },
  231. { TK_TYPE_BVEC4, "bvec4" },
  232. { TK_TYPE_INT, "int" },
  233. { TK_TYPE_IVEC2, "ivec2" },
  234. { TK_TYPE_IVEC3, "ivec3" },
  235. { TK_TYPE_IVEC4, "ivec4" },
  236. { TK_TYPE_UINT, "uint" },
  237. { TK_TYPE_UVEC2, "uvec2" },
  238. { TK_TYPE_UVEC3, "uvec3" },
  239. { TK_TYPE_UVEC4, "uvec4" },
  240. { TK_TYPE_FLOAT, "float" },
  241. { TK_TYPE_VEC2, "vec2" },
  242. { TK_TYPE_VEC3, "vec3" },
  243. { TK_TYPE_VEC4, "vec4" },
  244. { TK_TYPE_MAT2, "mat2" },
  245. { TK_TYPE_MAT3, "mat3" },
  246. { TK_TYPE_MAT4, "mat4" },
  247. { TK_TYPE_SAMPLER2D, "sampler2D" },
  248. { TK_TYPE_ISAMPLER2D, "isampler2D" },
  249. { TK_TYPE_USAMPLER2D, "usampler2D" },
  250. { TK_TYPE_SAMPLER2DARRAY, "sampler2DArray" },
  251. { TK_TYPE_ISAMPLER2DARRAY, "isampler2DArray" },
  252. { TK_TYPE_USAMPLER2DARRAY, "usampler2DArray" },
  253. { TK_TYPE_SAMPLER3D, "sampler3D" },
  254. { TK_TYPE_ISAMPLER3D, "isampler3D" },
  255. { TK_TYPE_USAMPLER3D, "usampler3D" },
  256. { TK_TYPE_SAMPLERCUBE, "samplerCube" },
  257. { TK_INTERPOLATION_FLAT, "flat" },
  258. { TK_INTERPOLATION_NO_PERSPECTIVE, "noperspective" },
  259. { TK_INTERPOLATION_SMOOTH, "smooth" },
  260. { TK_PRECISION_LOW, "lowp" },
  261. { TK_PRECISION_MID, "mediump" },
  262. { TK_PRECISION_HIGH, "highp" },
  263. { TK_CF_IF, "if" },
  264. { TK_CF_ELSE, "else" },
  265. { TK_CF_FOR, "for" },
  266. { TK_CF_WHILE, "while" },
  267. { TK_CF_DO, "do" },
  268. { TK_CF_SWITCH, "switch" },
  269. { TK_CF_CASE, "case" },
  270. { TK_CF_BREAK, "break" },
  271. { TK_CF_CONTINUE, "continue" },
  272. { TK_CF_RETURN, "return" },
  273. { TK_CF_DISCARD, "discard" },
  274. { TK_UNIFORM, "uniform" },
  275. { TK_VARYING, "varying" },
  276. { TK_ARG_IN, "in" },
  277. { TK_ARG_OUT, "out" },
  278. { TK_ARG_INOUT, "inout" },
  279. { TK_RENDER_MODE, "render_mode" },
  280. { TK_HINT_WHITE_TEXTURE, "hint_white" },
  281. { TK_HINT_BLACK_TEXTURE, "hint_black" },
  282. { TK_HINT_NORMAL_TEXTURE, "hint_normal" },
  283. { TK_HINT_ANISO_TEXTURE, "hint_aniso" },
  284. { TK_HINT_ALBEDO_TEXTURE, "hint_albedo" },
  285. { TK_HINT_BLACK_ALBEDO_TEXTURE, "hint_black_albedo" },
  286. { TK_HINT_COLOR, "hint_color" },
  287. { TK_HINT_RANGE, "hint_range" },
  288. { TK_SHADER_TYPE, "shader_type" },
  289. { TK_ERROR, NULL }
  290. };
  291. ShaderLanguage::Token ShaderLanguage::_get_token() {
  292. #define GETCHAR(m_idx) (((char_idx + m_idx) < code.length()) ? code[char_idx + m_idx] : CharType(0))
  293. while (true) {
  294. char_idx++;
  295. switch (GETCHAR(-1)) {
  296. case 0:
  297. return _make_token(TK_EOF);
  298. case 0xFFFF:
  299. return _make_token(TK_CURSOR); //for completion
  300. case '\t':
  301. case '\r':
  302. case ' ':
  303. continue;
  304. case '\n':
  305. tk_line++;
  306. continue;
  307. case '/': {
  308. switch (GETCHAR(0)) {
  309. case '*': { // block comment
  310. char_idx++;
  311. while (true) {
  312. if (GETCHAR(0) == 0) {
  313. return _make_token(TK_EOF);
  314. }
  315. if (GETCHAR(0) == '*' && GETCHAR(1) == '/') {
  316. char_idx += 2;
  317. break;
  318. } else if (GETCHAR(0) == '\n') {
  319. tk_line++;
  320. }
  321. char_idx++;
  322. }
  323. } break;
  324. case '/': { // line comment skip
  325. while (true) {
  326. if (GETCHAR(0) == '\n') {
  327. tk_line++;
  328. char_idx++;
  329. break;
  330. }
  331. if (GETCHAR(0) == 0) {
  332. return _make_token(TK_EOF);
  333. }
  334. char_idx++;
  335. }
  336. } break;
  337. case '=': { // diveq
  338. char_idx++;
  339. return _make_token(TK_OP_ASSIGN_DIV);
  340. } break;
  341. default:
  342. return _make_token(TK_OP_DIV);
  343. }
  344. continue; //a comment, continue to next token
  345. } break;
  346. case '=': {
  347. if (GETCHAR(0) == '=') {
  348. char_idx++;
  349. return _make_token(TK_OP_EQUAL);
  350. }
  351. return _make_token(TK_OP_ASSIGN);
  352. } break;
  353. case '<': {
  354. if (GETCHAR(0) == '=') {
  355. char_idx++;
  356. return _make_token(TK_OP_LESS_EQUAL);
  357. } else if (GETCHAR(0) == '<') {
  358. char_idx++;
  359. if (GETCHAR(0) == '=') {
  360. char_idx++;
  361. return _make_token(TK_OP_ASSIGN_SHIFT_LEFT);
  362. }
  363. return _make_token(TK_OP_SHIFT_LEFT);
  364. }
  365. return _make_token(TK_OP_LESS);
  366. } break;
  367. case '>': {
  368. if (GETCHAR(0) == '=') {
  369. char_idx++;
  370. return _make_token(TK_OP_GREATER_EQUAL);
  371. } else if (GETCHAR(0) == '>') {
  372. char_idx++;
  373. if (GETCHAR(0) == '=') {
  374. char_idx++;
  375. return _make_token(TK_OP_ASSIGN_SHIFT_RIGHT);
  376. }
  377. return _make_token(TK_OP_SHIFT_RIGHT);
  378. }
  379. return _make_token(TK_OP_GREATER);
  380. } break;
  381. case '!': {
  382. if (GETCHAR(0) == '=') {
  383. char_idx++;
  384. return _make_token(TK_OP_NOT_EQUAL);
  385. }
  386. return _make_token(TK_OP_NOT);
  387. } break;
  388. //case '"' //string - no strings in shader
  389. //case '\'' //string - no strings in shader
  390. case '{':
  391. return _make_token(TK_CURLY_BRACKET_OPEN);
  392. case '}':
  393. return _make_token(TK_CURLY_BRACKET_CLOSE);
  394. case '[':
  395. return _make_token(TK_BRACKET_OPEN);
  396. case ']':
  397. return _make_token(TK_BRACKET_CLOSE);
  398. case '(':
  399. return _make_token(TK_PARENTHESIS_OPEN);
  400. case ')':
  401. return _make_token(TK_PARENTHESIS_CLOSE);
  402. case ',':
  403. return _make_token(TK_COMMA);
  404. case ';':
  405. return _make_token(TK_SEMICOLON);
  406. case '?':
  407. return _make_token(TK_QUESTION);
  408. case ':':
  409. return _make_token(TK_COLON);
  410. case '^':
  411. return _make_token(TK_OP_BIT_XOR);
  412. case '~':
  413. return _make_token(TK_OP_BIT_INVERT);
  414. case '&': {
  415. if (GETCHAR(0) == '=') {
  416. char_idx++;
  417. return _make_token(TK_OP_ASSIGN_BIT_AND);
  418. } else if (GETCHAR(0) == '&') {
  419. char_idx++;
  420. return _make_token(TK_OP_AND);
  421. }
  422. return _make_token(TK_OP_BIT_AND);
  423. } break;
  424. case '|': {
  425. if (GETCHAR(0) == '=') {
  426. char_idx++;
  427. return _make_token(TK_OP_ASSIGN_BIT_OR);
  428. } else if (GETCHAR(0) == '|') {
  429. char_idx++;
  430. return _make_token(TK_OP_OR);
  431. }
  432. return _make_token(TK_OP_BIT_OR);
  433. } break;
  434. case '*': {
  435. if (GETCHAR(0) == '=') {
  436. char_idx++;
  437. return _make_token(TK_OP_ASSIGN_MUL);
  438. }
  439. return _make_token(TK_OP_MUL);
  440. } break;
  441. case '+': {
  442. if (GETCHAR(0) == '=') {
  443. char_idx++;
  444. return _make_token(TK_OP_ASSIGN_ADD);
  445. } else if (GETCHAR(0) == '+') {
  446. char_idx++;
  447. return _make_token(TK_OP_INCREMENT);
  448. }
  449. return _make_token(TK_OP_ADD);
  450. } break;
  451. case '-': {
  452. if (GETCHAR(0) == '=') {
  453. char_idx++;
  454. return _make_token(TK_OP_ASSIGN_SUB);
  455. } else if (GETCHAR(0) == '-') {
  456. char_idx++;
  457. return _make_token(TK_OP_DECREMENT);
  458. }
  459. return _make_token(TK_OP_SUB);
  460. } break;
  461. case '%': {
  462. if (GETCHAR(0) == '=') {
  463. char_idx++;
  464. return _make_token(TK_OP_ASSIGN_MOD);
  465. }
  466. return _make_token(TK_OP_MOD);
  467. } break;
  468. default: {
  469. char_idx--; //go back one, since we have no idea what this is
  470. if (_is_number(GETCHAR(0)) || (GETCHAR(0) == '.' && _is_number(GETCHAR(1)))) {
  471. // parse number
  472. bool period_found = false;
  473. bool exponent_found = false;
  474. bool hexa_found = false;
  475. bool sign_found = false;
  476. bool minus_exponent_found = false;
  477. bool float_suffix_found = false;
  478. String str;
  479. int i = 0;
  480. while (true) {
  481. if (GETCHAR(i) == '.') {
  482. if (period_found || exponent_found || hexa_found || float_suffix_found)
  483. return _make_token(TK_ERROR, "Invalid numeric constant");
  484. period_found = true;
  485. } else if (GETCHAR(i) == 'x') {
  486. if (hexa_found || str.length() != 1 || str[0] != '0')
  487. return _make_token(TK_ERROR, "Invalid numeric constant");
  488. hexa_found = true;
  489. } else if (GETCHAR(i) == 'e') {
  490. if (hexa_found || exponent_found || float_suffix_found)
  491. return _make_token(TK_ERROR, "Invalid numeric constant");
  492. exponent_found = true;
  493. } else if (GETCHAR(i) == 'f') {
  494. if (hexa_found || exponent_found)
  495. return _make_token(TK_ERROR, "Invalid numeric constant");
  496. float_suffix_found = true;
  497. } else if (_is_number(GETCHAR(i))) {
  498. if (float_suffix_found)
  499. return _make_token(TK_ERROR, "Invalid numeric constant");
  500. } else if (hexa_found && _is_hex(GETCHAR(i))) {
  501. } else if ((GETCHAR(i) == '-' || GETCHAR(i) == '+') && exponent_found) {
  502. if (sign_found)
  503. return _make_token(TK_ERROR, "Invalid numeric constant");
  504. sign_found = true;
  505. if (GETCHAR(i) == '-')
  506. minus_exponent_found = true;
  507. } else
  508. break;
  509. str += CharType(GETCHAR(i));
  510. i++;
  511. }
  512. CharType last_char = str[str.length() - 1];
  513. if (hexa_found) {
  514. //hex integers eg."0xFF" or "0x12AB", etc - NOT supported yet
  515. return _make_token(TK_ERROR, "Invalid (hexadecimal) numeric constant - Not supported");
  516. } else if (period_found || float_suffix_found) {
  517. //floats
  518. if (period_found) {
  519. if (float_suffix_found) {
  520. //checks for eg "1.f" or "1.99f" notations
  521. if (last_char != 'f') {
  522. return _make_token(TK_ERROR, "Invalid (float) numeric constant");
  523. }
  524. } else {
  525. //checks for eg. "1." or "1.99" notations
  526. if (last_char != '.' && !_is_number(last_char)) {
  527. return _make_token(TK_ERROR, "Invalid (float) numeric constant");
  528. }
  529. }
  530. } else if (float_suffix_found) {
  531. // if no period found the float suffix must be the last character, like in "2f" for "2.0"
  532. if (last_char != 'f') {
  533. return _make_token(TK_ERROR, "Invalid (float) numeric constant");
  534. }
  535. }
  536. if (float_suffix_found) {
  537. //strip the suffix
  538. str = str.left(str.length() - 1);
  539. //compensate reading cursor position
  540. char_idx += 1;
  541. }
  542. if (!str.is_valid_float()) {
  543. return _make_token(TK_ERROR, "Invalid (float) numeric constant");
  544. }
  545. } else {
  546. //integers
  547. if (!_is_number(last_char)) {
  548. return _make_token(TK_ERROR, "Invalid (integer) numeric constant");
  549. }
  550. if (!str.is_valid_integer()) {
  551. return _make_token(TK_ERROR, "Invalid numeric constant");
  552. }
  553. }
  554. char_idx += str.length();
  555. Token tk;
  556. if (period_found || minus_exponent_found || float_suffix_found)
  557. tk.type = TK_REAL_CONSTANT;
  558. else
  559. tk.type = TK_INT_CONSTANT;
  560. tk.constant = str.to_double(); //wont work with hex
  561. tk.line = tk_line;
  562. return tk;
  563. }
  564. if (GETCHAR(0) == '.') {
  565. //parse period
  566. char_idx++;
  567. return _make_token(TK_PERIOD);
  568. }
  569. if (_is_text_char(GETCHAR(0))) {
  570. // parse identifier
  571. String str;
  572. while (_is_text_char(GETCHAR(0))) {
  573. str += CharType(GETCHAR(0));
  574. char_idx++;
  575. }
  576. //see if keyword
  577. //should be converted to a static map
  578. int idx = 0;
  579. while (keyword_list[idx].text) {
  580. if (str == keyword_list[idx].text) {
  581. return _make_token(keyword_list[idx].token);
  582. }
  583. idx++;
  584. }
  585. return _make_token(TK_IDENTIFIER, str);
  586. }
  587. if (GETCHAR(0) > 32)
  588. return _make_token(TK_ERROR, "Tokenizer: Unknown character #" + itos(GETCHAR(0)) + ": '" + String::chr(GETCHAR(0)) + "'");
  589. else
  590. return _make_token(TK_ERROR, "Tokenizer: Unknown character #" + itos(GETCHAR(0)));
  591. } break;
  592. }
  593. }
  594. ERR_PRINT("BUG");
  595. return Token();
  596. #undef GETCHAR
  597. }
  598. String ShaderLanguage::token_debug(const String &p_code) {
  599. clear();
  600. code = p_code;
  601. String output;
  602. Token tk = _get_token();
  603. while (tk.type != TK_EOF && tk.type != TK_ERROR) {
  604. output += itos(tk_line) + ": " + get_token_text(tk) + "\n";
  605. tk = _get_token();
  606. }
  607. return output;
  608. }
  609. bool ShaderLanguage::is_token_datatype(TokenType p_type) {
  610. return (
  611. p_type == TK_TYPE_VOID ||
  612. p_type == TK_TYPE_BOOL ||
  613. p_type == TK_TYPE_BVEC2 ||
  614. p_type == TK_TYPE_BVEC3 ||
  615. p_type == TK_TYPE_BVEC4 ||
  616. p_type == TK_TYPE_INT ||
  617. p_type == TK_TYPE_IVEC2 ||
  618. p_type == TK_TYPE_IVEC3 ||
  619. p_type == TK_TYPE_IVEC4 ||
  620. p_type == TK_TYPE_UINT ||
  621. p_type == TK_TYPE_UVEC2 ||
  622. p_type == TK_TYPE_UVEC3 ||
  623. p_type == TK_TYPE_UVEC4 ||
  624. p_type == TK_TYPE_FLOAT ||
  625. p_type == TK_TYPE_VEC2 ||
  626. p_type == TK_TYPE_VEC3 ||
  627. p_type == TK_TYPE_VEC4 ||
  628. p_type == TK_TYPE_MAT2 ||
  629. p_type == TK_TYPE_MAT3 ||
  630. p_type == TK_TYPE_MAT4 ||
  631. p_type == TK_TYPE_SAMPLER2D ||
  632. p_type == TK_TYPE_ISAMPLER2D ||
  633. p_type == TK_TYPE_USAMPLER2D ||
  634. p_type == TK_TYPE_SAMPLER2DARRAY ||
  635. p_type == TK_TYPE_ISAMPLER2DARRAY ||
  636. p_type == TK_TYPE_USAMPLER2DARRAY ||
  637. p_type == TK_TYPE_SAMPLER3D ||
  638. p_type == TK_TYPE_ISAMPLER3D ||
  639. p_type == TK_TYPE_USAMPLER3D ||
  640. p_type == TK_TYPE_SAMPLERCUBE);
  641. }
  642. ShaderLanguage::DataType ShaderLanguage::get_token_datatype(TokenType p_type) {
  643. return DataType(p_type - TK_TYPE_VOID);
  644. }
  645. bool ShaderLanguage::is_token_interpolation(TokenType p_type) {
  646. return (
  647. p_type == TK_INTERPOLATION_FLAT ||
  648. p_type == TK_INTERPOLATION_NO_PERSPECTIVE ||
  649. p_type == TK_INTERPOLATION_SMOOTH);
  650. }
  651. ShaderLanguage::DataInterpolation ShaderLanguage::get_token_interpolation(TokenType p_type) {
  652. if (p_type == TK_INTERPOLATION_FLAT)
  653. return INTERPOLATION_FLAT;
  654. else if (p_type == TK_INTERPOLATION_NO_PERSPECTIVE)
  655. return INTERPOLATION_NO_PERSPECTIVE;
  656. else
  657. return INTERPOLATION_SMOOTH;
  658. }
  659. bool ShaderLanguage::is_token_precision(TokenType p_type) {
  660. return (
  661. p_type == TK_PRECISION_LOW ||
  662. p_type == TK_PRECISION_MID ||
  663. p_type == TK_PRECISION_HIGH);
  664. }
  665. ShaderLanguage::DataPrecision ShaderLanguage::get_token_precision(TokenType p_type) {
  666. if (p_type == TK_PRECISION_LOW)
  667. return PRECISION_LOWP;
  668. else if (p_type == TK_PRECISION_HIGH)
  669. return PRECISION_HIGHP;
  670. else
  671. return PRECISION_MEDIUMP;
  672. }
  673. String ShaderLanguage::get_datatype_name(DataType p_type) {
  674. switch (p_type) {
  675. case TYPE_VOID: return "void";
  676. case TYPE_BOOL: return "bool";
  677. case TYPE_BVEC2: return "bvec2";
  678. case TYPE_BVEC3: return "bvec3";
  679. case TYPE_BVEC4: return "bvec4";
  680. case TYPE_INT: return "int";
  681. case TYPE_IVEC2: return "ivec2";
  682. case TYPE_IVEC3: return "ivec3";
  683. case TYPE_IVEC4: return "ivec4";
  684. case TYPE_UINT: return "uint";
  685. case TYPE_UVEC2: return "uvec2";
  686. case TYPE_UVEC3: return "uvec3";
  687. case TYPE_UVEC4: return "uvec4";
  688. case TYPE_FLOAT: return "float";
  689. case TYPE_VEC2: return "vec2";
  690. case TYPE_VEC3: return "vec3";
  691. case TYPE_VEC4: return "vec4";
  692. case TYPE_MAT2: return "mat2";
  693. case TYPE_MAT3: return "mat3";
  694. case TYPE_MAT4: return "mat4";
  695. case TYPE_SAMPLER2D: return "sampler2D";
  696. case TYPE_ISAMPLER2D: return "isampler2D";
  697. case TYPE_USAMPLER2D: return "usampler2D";
  698. case TYPE_SAMPLER2DARRAY: return "sampler2DArray";
  699. case TYPE_ISAMPLER2DARRAY: return "isampler2DArray";
  700. case TYPE_USAMPLER2DARRAY: return "usampler2DArray";
  701. case TYPE_SAMPLER3D: return "sampler3D";
  702. case TYPE_ISAMPLER3D: return "isampler3D";
  703. case TYPE_USAMPLER3D: return "usampler3D";
  704. case TYPE_SAMPLERCUBE: return "samplerCube";
  705. }
  706. return "";
  707. }
  708. bool ShaderLanguage::is_token_nonvoid_datatype(TokenType p_type) {
  709. return is_token_datatype(p_type) && p_type != TK_TYPE_VOID;
  710. }
  711. void ShaderLanguage::clear() {
  712. current_function = StringName();
  713. completion_type = COMPLETION_NONE;
  714. completion_block = NULL;
  715. completion_function = StringName();
  716. error_line = 0;
  717. tk_line = 1;
  718. char_idx = 0;
  719. error_set = false;
  720. error_str = "";
  721. while (nodes) {
  722. Node *n = nodes;
  723. nodes = nodes->next;
  724. memdelete(n);
  725. }
  726. }
  727. bool ShaderLanguage::_find_identifier(const BlockNode *p_block, const Map<StringName, BuiltInInfo> &p_builtin_types, const StringName &p_identifier, DataType *r_data_type, IdentifierType *r_type) {
  728. if (p_builtin_types.has(p_identifier)) {
  729. if (r_data_type) {
  730. *r_data_type = p_builtin_types[p_identifier].type;
  731. }
  732. if (r_type) {
  733. *r_type = IDENTIFIER_BUILTIN_VAR;
  734. }
  735. return true;
  736. }
  737. FunctionNode *function = NULL;
  738. while (p_block) {
  739. if (p_block->variables.has(p_identifier)) {
  740. if (r_data_type) {
  741. *r_data_type = p_block->variables[p_identifier].type;
  742. }
  743. if (r_type) {
  744. *r_type = IDENTIFIER_LOCAL_VAR;
  745. }
  746. return true;
  747. }
  748. if (p_block->parent_function) {
  749. function = p_block->parent_function;
  750. break;
  751. } else {
  752. ERR_FAIL_COND_V(!p_block->parent_block, false);
  753. p_block = p_block->parent_block;
  754. }
  755. }
  756. if (function) {
  757. for (int i = 0; i < function->arguments.size(); i++) {
  758. if (function->arguments[i].name == p_identifier) {
  759. if (r_data_type) {
  760. *r_data_type = function->arguments[i].type;
  761. }
  762. if (r_type) {
  763. *r_type = IDENTIFIER_FUNCTION_ARGUMENT;
  764. }
  765. return true;
  766. }
  767. }
  768. }
  769. if (shader->varyings.has(p_identifier)) {
  770. if (r_data_type) {
  771. *r_data_type = shader->varyings[p_identifier].type;
  772. }
  773. if (r_type) {
  774. *r_type = IDENTIFIER_VARYING;
  775. }
  776. return true;
  777. }
  778. if (shader->uniforms.has(p_identifier)) {
  779. if (r_data_type) {
  780. *r_data_type = shader->uniforms[p_identifier].type;
  781. }
  782. if (r_type) {
  783. *r_type = IDENTIFIER_UNIFORM;
  784. }
  785. return true;
  786. }
  787. for (int i = 0; i < shader->functions.size(); i++) {
  788. if (!shader->functions[i].callable)
  789. continue;
  790. if (shader->functions[i].name == p_identifier) {
  791. if (r_data_type) {
  792. *r_data_type = shader->functions[i].function->return_type;
  793. }
  794. if (r_type) {
  795. *r_type = IDENTIFIER_FUNCTION;
  796. }
  797. }
  798. }
  799. return false;
  800. }
  801. bool ShaderLanguage::_validate_operator(OperatorNode *p_op, DataType *r_ret_type) {
  802. bool valid = false;
  803. DataType ret_type;
  804. switch (p_op->op) {
  805. case OP_EQUAL:
  806. case OP_NOT_EQUAL: {
  807. DataType na = p_op->arguments[0]->get_datatype();
  808. DataType nb = p_op->arguments[1]->get_datatype();
  809. valid = na == nb;
  810. ret_type = TYPE_BOOL;
  811. } break;
  812. case OP_LESS:
  813. case OP_LESS_EQUAL:
  814. case OP_GREATER:
  815. case OP_GREATER_EQUAL: {
  816. DataType na = p_op->arguments[0]->get_datatype();
  817. DataType nb = p_op->arguments[1]->get_datatype();
  818. valid = na == nb && (na == TYPE_UINT || na == TYPE_INT || na == TYPE_FLOAT);
  819. ret_type = TYPE_BOOL;
  820. } break;
  821. case OP_AND:
  822. case OP_OR: {
  823. DataType na = p_op->arguments[0]->get_datatype();
  824. DataType nb = p_op->arguments[1]->get_datatype();
  825. valid = na == nb && na == TYPE_BOOL;
  826. ret_type = TYPE_BOOL;
  827. } break;
  828. case OP_NOT: {
  829. DataType na = p_op->arguments[0]->get_datatype();
  830. valid = na == TYPE_BOOL;
  831. ret_type = TYPE_BOOL;
  832. } break;
  833. case OP_INCREMENT:
  834. case OP_DECREMENT:
  835. case OP_POST_INCREMENT:
  836. case OP_POST_DECREMENT:
  837. case OP_NEGATE: {
  838. DataType na = p_op->arguments[0]->get_datatype();
  839. valid = na > TYPE_BOOL && na < TYPE_MAT2;
  840. ret_type = na;
  841. } break;
  842. case OP_ADD:
  843. case OP_SUB:
  844. case OP_MUL:
  845. case OP_DIV: {
  846. DataType na = p_op->arguments[0]->get_datatype();
  847. DataType nb = p_op->arguments[1]->get_datatype();
  848. if (na > nb) {
  849. //make things easier;
  850. SWAP(na, nb);
  851. }
  852. if (na == nb) {
  853. valid = (na > TYPE_BOOL && na <= TYPE_MAT4);
  854. ret_type = na;
  855. } else if (na == TYPE_INT && nb == TYPE_IVEC2) {
  856. valid = true;
  857. ret_type = TYPE_IVEC2;
  858. } else if (na == TYPE_INT && nb == TYPE_IVEC3) {
  859. valid = true;
  860. ret_type = TYPE_IVEC3;
  861. } else if (na == TYPE_INT && nb == TYPE_IVEC4) {
  862. valid = true;
  863. ret_type = TYPE_IVEC4;
  864. } else if (na == TYPE_UINT && nb == TYPE_UVEC2) {
  865. valid = true;
  866. ret_type = TYPE_UVEC2;
  867. } else if (na == TYPE_UINT && nb == TYPE_UVEC3) {
  868. valid = true;
  869. ret_type = TYPE_UVEC3;
  870. } else if (na == TYPE_UINT && nb == TYPE_UVEC4) {
  871. valid = true;
  872. ret_type = TYPE_UVEC4;
  873. } else if (na == TYPE_FLOAT && nb == TYPE_VEC2) {
  874. valid = true;
  875. ret_type = TYPE_VEC2;
  876. } else if (na == TYPE_FLOAT && nb == TYPE_VEC3) {
  877. valid = true;
  878. ret_type = TYPE_VEC3;
  879. } else if (na == TYPE_FLOAT && nb == TYPE_VEC4) {
  880. valid = true;
  881. ret_type = TYPE_VEC4;
  882. } else if (p_op->op == OP_MUL && na == TYPE_FLOAT && nb == TYPE_MAT2) {
  883. valid = true;
  884. ret_type = TYPE_MAT2;
  885. } else if (p_op->op == OP_MUL && na == TYPE_FLOAT && nb == TYPE_MAT3) {
  886. valid = true;
  887. ret_type = TYPE_MAT3;
  888. } else if (p_op->op == OP_MUL && na == TYPE_FLOAT && nb == TYPE_MAT4) {
  889. valid = true;
  890. ret_type = TYPE_MAT4;
  891. } else if (p_op->op == OP_MUL && na == TYPE_VEC2 && nb == TYPE_MAT2) {
  892. valid = true;
  893. ret_type = TYPE_VEC2;
  894. } else if (p_op->op == OP_MUL && na == TYPE_VEC3 && nb == TYPE_MAT3) {
  895. valid = true;
  896. ret_type = TYPE_VEC3;
  897. } else if (p_op->op == OP_MUL && na == TYPE_VEC4 && nb == TYPE_MAT4) {
  898. valid = true;
  899. ret_type = TYPE_VEC4;
  900. }
  901. } break;
  902. case OP_ASSIGN_MOD:
  903. case OP_MOD: {
  904. /*
  905. * The operator modulus (%) operates on signed or unsigned integers or integer vectors. The operand
  906. * types must both be signed or both be unsigned. The operands cannot be vectors of differing size. If
  907. * one operand is a scalar and the other vector, then the scalar is applied component-wise to the vector,
  908. * resulting in the same type as the vector. If both are vectors of the same size, the result is computed
  909. * component-wise.
  910. */
  911. DataType na = p_op->arguments[0]->get_datatype();
  912. DataType nb = p_op->arguments[1]->get_datatype();
  913. if (na == TYPE_INT && nb == TYPE_INT) {
  914. valid = true;
  915. ret_type = TYPE_INT;
  916. } else if (na == TYPE_IVEC2 && nb == TYPE_INT) {
  917. valid = true;
  918. ret_type = TYPE_IVEC2;
  919. } else if (na == TYPE_IVEC3 && nb == TYPE_INT) {
  920. valid = true;
  921. ret_type = TYPE_IVEC3;
  922. } else if (na == TYPE_IVEC4 && nb == TYPE_INT) {
  923. valid = true;
  924. ret_type = TYPE_IVEC4;
  925. } else if (na == TYPE_IVEC2 && nb == TYPE_IVEC2) {
  926. valid = true;
  927. ret_type = TYPE_IVEC2;
  928. } else if (na == TYPE_IVEC3 && nb == TYPE_IVEC3) {
  929. valid = true;
  930. ret_type = TYPE_IVEC3;
  931. } else if (na == TYPE_IVEC4 && nb == TYPE_IVEC4) {
  932. valid = true;
  933. ret_type = TYPE_IVEC4;
  934. /////
  935. } else if (na == TYPE_UINT && nb == TYPE_UINT) {
  936. valid = true;
  937. ret_type = TYPE_UINT;
  938. } else if (na == TYPE_UVEC2 && nb == TYPE_UINT) {
  939. valid = true;
  940. ret_type = TYPE_UVEC2;
  941. } else if (na == TYPE_UVEC3 && nb == TYPE_UINT) {
  942. valid = true;
  943. ret_type = TYPE_UVEC3;
  944. } else if (na == TYPE_UVEC4 && nb == TYPE_UINT) {
  945. valid = true;
  946. ret_type = TYPE_UVEC4;
  947. } else if (na == TYPE_UVEC2 && nb == TYPE_UVEC2) {
  948. valid = true;
  949. ret_type = TYPE_UVEC2;
  950. } else if (na == TYPE_UVEC3 && nb == TYPE_UVEC3) {
  951. valid = true;
  952. ret_type = TYPE_UVEC3;
  953. } else if (na == TYPE_UVEC4 && nb == TYPE_UVEC4) {
  954. valid = true;
  955. ret_type = TYPE_UVEC4;
  956. }
  957. } break;
  958. case OP_ASSIGN_SHIFT_LEFT:
  959. case OP_ASSIGN_SHIFT_RIGHT:
  960. case OP_SHIFT_LEFT:
  961. case OP_SHIFT_RIGHT: {
  962. DataType na = p_op->arguments[0]->get_datatype();
  963. DataType nb = p_op->arguments[1]->get_datatype();
  964. if (na == TYPE_INT && nb == TYPE_INT) {
  965. valid = true;
  966. ret_type = TYPE_INT;
  967. } else if (na == TYPE_IVEC2 && nb == TYPE_INT) {
  968. valid = true;
  969. ret_type = TYPE_IVEC2;
  970. } else if (na == TYPE_IVEC3 && nb == TYPE_INT) {
  971. valid = true;
  972. ret_type = TYPE_IVEC3;
  973. } else if (na == TYPE_IVEC4 && nb == TYPE_INT) {
  974. valid = true;
  975. ret_type = TYPE_IVEC4;
  976. } else if (na == TYPE_IVEC2 && nb == TYPE_IVEC2) {
  977. valid = true;
  978. ret_type = TYPE_IVEC2;
  979. } else if (na == TYPE_IVEC3 && nb == TYPE_IVEC3) {
  980. valid = true;
  981. ret_type = TYPE_IVEC3;
  982. } else if (na == TYPE_IVEC4 && nb == TYPE_IVEC4) {
  983. valid = true;
  984. ret_type = TYPE_IVEC4;
  985. } else if (na == TYPE_UINT && nb == TYPE_UINT) {
  986. valid = true;
  987. ret_type = TYPE_UINT;
  988. } else if (na == TYPE_UVEC2 && nb == TYPE_UINT) {
  989. valid = true;
  990. ret_type = TYPE_UVEC2;
  991. } else if (na == TYPE_UVEC3 && nb == TYPE_UINT) {
  992. valid = true;
  993. ret_type = TYPE_UVEC3;
  994. } else if (na == TYPE_UVEC4 && nb == TYPE_UINT) {
  995. valid = true;
  996. ret_type = TYPE_UVEC4;
  997. } else if (na == TYPE_UVEC2 && nb == TYPE_UVEC2) {
  998. valid = true;
  999. ret_type = TYPE_UVEC2;
  1000. } else if (na == TYPE_UVEC3 && nb == TYPE_UVEC3) {
  1001. valid = true;
  1002. ret_type = TYPE_UVEC3;
  1003. } else if (na == TYPE_UVEC4 && nb == TYPE_UVEC4) {
  1004. valid = true;
  1005. ret_type = TYPE_UVEC4;
  1006. }
  1007. } break;
  1008. case OP_ASSIGN: {
  1009. DataType na = p_op->arguments[0]->get_datatype();
  1010. DataType nb = p_op->arguments[1]->get_datatype();
  1011. valid = na == nb;
  1012. ret_type = na;
  1013. } break;
  1014. case OP_ASSIGN_ADD:
  1015. case OP_ASSIGN_SUB:
  1016. case OP_ASSIGN_MUL:
  1017. case OP_ASSIGN_DIV: {
  1018. DataType na = p_op->arguments[0]->get_datatype();
  1019. DataType nb = p_op->arguments[1]->get_datatype();
  1020. if (na == nb) {
  1021. valid = (na > TYPE_BOOL && na < TYPE_MAT2) || (p_op->op == OP_ASSIGN_MUL && na >= TYPE_MAT2 && na <= TYPE_MAT4);
  1022. ret_type = na;
  1023. } else if (na == TYPE_IVEC2 && nb == TYPE_INT) {
  1024. valid = true;
  1025. ret_type = TYPE_IVEC2;
  1026. } else if (na == TYPE_IVEC3 && nb == TYPE_INT) {
  1027. valid = true;
  1028. ret_type = TYPE_IVEC3;
  1029. } else if (na == TYPE_IVEC4 && nb == TYPE_INT) {
  1030. valid = true;
  1031. ret_type = TYPE_IVEC4;
  1032. } else if (na == TYPE_UVEC2 && nb == TYPE_UINT) {
  1033. valid = true;
  1034. ret_type = TYPE_UVEC2;
  1035. } else if (na == TYPE_UVEC3 && nb == TYPE_UINT) {
  1036. valid = true;
  1037. ret_type = TYPE_UVEC3;
  1038. } else if (na == TYPE_UVEC4 && nb == TYPE_UINT) {
  1039. valid = true;
  1040. ret_type = TYPE_UVEC4;
  1041. } else if (na == TYPE_VEC2 && nb == TYPE_FLOAT) {
  1042. valid = true;
  1043. ret_type = TYPE_VEC2;
  1044. } else if (na == TYPE_VEC3 && nb == TYPE_FLOAT) {
  1045. valid = true;
  1046. ret_type = TYPE_VEC3;
  1047. } else if (na == TYPE_VEC4 && nb == TYPE_FLOAT) {
  1048. valid = true;
  1049. ret_type = TYPE_VEC4;
  1050. } else if (p_op->op == OP_ASSIGN_MUL && na == TYPE_MAT2 && nb == TYPE_VEC2) {
  1051. valid = true;
  1052. ret_type = TYPE_MAT2;
  1053. } else if (p_op->op == OP_ASSIGN_MUL && na == TYPE_MAT3 && nb == TYPE_VEC3) {
  1054. valid = true;
  1055. ret_type = TYPE_MAT3;
  1056. } else if (p_op->op == OP_ASSIGN_MUL && na == TYPE_MAT4 && nb == TYPE_VEC4) {
  1057. valid = true;
  1058. ret_type = TYPE_MAT4;
  1059. }
  1060. } break;
  1061. case OP_ASSIGN_BIT_AND:
  1062. case OP_ASSIGN_BIT_OR:
  1063. case OP_ASSIGN_BIT_XOR:
  1064. case OP_BIT_AND:
  1065. case OP_BIT_OR:
  1066. case OP_BIT_XOR: {
  1067. /*
  1068. * The bitwise operators and (&), exclusive-or (^), and inclusive-or (|). The operands must be of type
  1069. * signed or unsigned integers or integer vectors. The operands cannot be vectors of differing size. If
  1070. * one operand is a scalar and the other a vector, the scalar is applied component-wise to the vector,
  1071. * resulting in the same type as the vector. The fundamental types of the operands (signed or unsigned)
  1072. * must match.
  1073. */
  1074. DataType na = p_op->arguments[0]->get_datatype();
  1075. DataType nb = p_op->arguments[1]->get_datatype();
  1076. if (na > nb && p_op->op >= OP_BIT_AND) {
  1077. //can swap for non assign
  1078. SWAP(na, nb);
  1079. }
  1080. if (na == TYPE_INT && nb == TYPE_INT) {
  1081. valid = true;
  1082. ret_type = TYPE_INT;
  1083. } else if (na == TYPE_IVEC2 && nb == TYPE_INT) {
  1084. valid = true;
  1085. ret_type = TYPE_IVEC2;
  1086. } else if (na == TYPE_IVEC3 && nb == TYPE_INT) {
  1087. valid = true;
  1088. ret_type = TYPE_IVEC3;
  1089. } else if (na == TYPE_IVEC4 && nb == TYPE_INT) {
  1090. valid = true;
  1091. ret_type = TYPE_IVEC4;
  1092. } else if (na == TYPE_IVEC2 && nb == TYPE_IVEC2) {
  1093. valid = true;
  1094. ret_type = TYPE_IVEC2;
  1095. } else if (na == TYPE_IVEC3 && nb == TYPE_IVEC3) {
  1096. valid = true;
  1097. ret_type = TYPE_IVEC3;
  1098. } else if (na == TYPE_IVEC4 && nb == TYPE_IVEC4) {
  1099. valid = true;
  1100. ret_type = TYPE_IVEC4;
  1101. /////
  1102. } else if (na == TYPE_UINT && nb == TYPE_UINT) {
  1103. valid = true;
  1104. ret_type = TYPE_UINT;
  1105. } else if (na == TYPE_UVEC2 && nb == TYPE_UINT) {
  1106. valid = true;
  1107. ret_type = TYPE_UVEC2;
  1108. } else if (na == TYPE_UVEC3 && nb == TYPE_UINT) {
  1109. valid = true;
  1110. ret_type = TYPE_UVEC3;
  1111. } else if (na == TYPE_UVEC4 && nb == TYPE_UINT) {
  1112. valid = true;
  1113. ret_type = TYPE_UVEC4;
  1114. } else if (na == TYPE_UVEC2 && nb == TYPE_UVEC2) {
  1115. valid = true;
  1116. ret_type = TYPE_UVEC2;
  1117. } else if (na == TYPE_UVEC3 && nb == TYPE_UVEC3) {
  1118. valid = true;
  1119. ret_type = TYPE_UVEC3;
  1120. } else if (na == TYPE_UVEC4 && nb == TYPE_UVEC4) {
  1121. valid = true;
  1122. ret_type = TYPE_UVEC4;
  1123. }
  1124. } break;
  1125. case OP_BIT_INVERT: { //unaries
  1126. DataType na = p_op->arguments[0]->get_datatype();
  1127. valid = na >= TYPE_INT && na < TYPE_FLOAT;
  1128. ret_type = na;
  1129. } break;
  1130. case OP_SELECT_IF: {
  1131. DataType na = p_op->arguments[0]->get_datatype();
  1132. DataType nb = p_op->arguments[1]->get_datatype();
  1133. DataType nc = p_op->arguments[2]->get_datatype();
  1134. valid = na == TYPE_BOOL && (nb == nc);
  1135. ret_type = nb;
  1136. } break;
  1137. default: {
  1138. ERR_FAIL_V(false);
  1139. }
  1140. }
  1141. if (r_ret_type)
  1142. *r_ret_type = ret_type;
  1143. return valid;
  1144. }
  1145. const ShaderLanguage::BuiltinFuncDef ShaderLanguage::builtin_func_defs[] = {
  1146. //constructors
  1147. { "bool", TYPE_BOOL, { TYPE_BOOL, TYPE_VOID } },
  1148. { "bvec2", TYPE_BVEC2, { TYPE_BOOL, TYPE_VOID } },
  1149. { "bvec2", TYPE_BVEC2, { TYPE_BOOL, TYPE_BOOL, TYPE_VOID } },
  1150. { "bvec3", TYPE_BVEC3, { TYPE_BOOL, TYPE_VOID } },
  1151. { "bvec3", TYPE_BVEC3, { TYPE_BOOL, TYPE_BOOL, TYPE_BOOL, TYPE_VOID } },
  1152. { "bvec3", TYPE_BVEC3, { TYPE_BVEC2, TYPE_BOOL, TYPE_VOID } },
  1153. { "bvec3", TYPE_BVEC3, { TYPE_BOOL, TYPE_BVEC2, TYPE_VOID } },
  1154. { "bvec4", TYPE_BVEC4, { TYPE_BOOL, TYPE_VOID } },
  1155. { "bvec4", TYPE_BVEC4, { TYPE_BOOL, TYPE_BOOL, TYPE_BOOL, TYPE_BOOL, TYPE_VOID } },
  1156. { "bvec4", TYPE_BVEC4, { TYPE_BOOL, TYPE_BVEC2, TYPE_BOOL, TYPE_VOID } },
  1157. { "bvec4", TYPE_BVEC4, { TYPE_BVEC2, TYPE_BOOL, TYPE_BOOL, TYPE_VOID } },
  1158. { "bvec4", TYPE_BVEC4, { TYPE_BOOL, TYPE_BOOL, TYPE_BVEC2, TYPE_VOID } },
  1159. { "bvec4", TYPE_BVEC4, { TYPE_BOOL, TYPE_BVEC3, TYPE_VOID } },
  1160. { "bvec4", TYPE_BVEC4, { TYPE_BVEC3, TYPE_BOOL, TYPE_VOID } },
  1161. { "bvec4", TYPE_BVEC4, { TYPE_BVEC2, TYPE_BVEC2, TYPE_VOID } },
  1162. { "float", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID } },
  1163. { "vec2", TYPE_VEC2, { TYPE_FLOAT, TYPE_VOID } },
  1164. { "vec2", TYPE_VEC2, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID } },
  1165. { "vec3", TYPE_VEC3, { TYPE_FLOAT, TYPE_VOID } },
  1166. { "vec3", TYPE_VEC3, { TYPE_FLOAT, TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID } },
  1167. { "vec3", TYPE_VEC3, { TYPE_VEC2, TYPE_FLOAT, TYPE_VOID } },
  1168. { "vec3", TYPE_VEC3, { TYPE_FLOAT, TYPE_VEC2, TYPE_VOID } },
  1169. { "vec4", TYPE_VEC4, { TYPE_FLOAT, TYPE_VOID } },
  1170. { "vec4", TYPE_VEC4, { TYPE_FLOAT, TYPE_FLOAT, TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID } },
  1171. { "vec4", TYPE_VEC4, { TYPE_FLOAT, TYPE_VEC2, TYPE_FLOAT, TYPE_VOID } },
  1172. { "vec4", TYPE_VEC4, { TYPE_VEC2, TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID } },
  1173. { "vec4", TYPE_VEC4, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VEC2, TYPE_VOID } },
  1174. { "vec4", TYPE_VEC4, { TYPE_FLOAT, TYPE_VEC3, TYPE_VOID } },
  1175. { "vec4", TYPE_VEC4, { TYPE_VEC3, TYPE_FLOAT, TYPE_VOID } },
  1176. { "vec4", TYPE_VEC4, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID } },
  1177. { "int", TYPE_INT, { TYPE_INT, TYPE_VOID } },
  1178. { "ivec2", TYPE_IVEC2, { TYPE_INT, TYPE_VOID } },
  1179. { "ivec2", TYPE_IVEC2, { TYPE_INT, TYPE_INT, TYPE_VOID } },
  1180. { "ivec3", TYPE_IVEC3, { TYPE_INT, TYPE_VOID } },
  1181. { "ivec3", TYPE_IVEC3, { TYPE_INT, TYPE_INT, TYPE_INT, TYPE_VOID } },
  1182. { "ivec3", TYPE_IVEC3, { TYPE_IVEC2, TYPE_INT, TYPE_VOID } },
  1183. { "ivec3", TYPE_IVEC3, { TYPE_INT, TYPE_IVEC2, TYPE_VOID } },
  1184. { "ivec4", TYPE_IVEC4, { TYPE_INT, TYPE_VOID } },
  1185. { "ivec4", TYPE_IVEC4, { TYPE_INT, TYPE_INT, TYPE_INT, TYPE_INT, TYPE_VOID } },
  1186. { "ivec4", TYPE_IVEC4, { TYPE_INT, TYPE_IVEC2, TYPE_INT, TYPE_VOID } },
  1187. { "ivec4", TYPE_IVEC4, { TYPE_IVEC2, TYPE_INT, TYPE_INT, TYPE_VOID } },
  1188. { "ivec4", TYPE_IVEC4, { TYPE_INT, TYPE_INT, TYPE_IVEC2, TYPE_VOID } },
  1189. { "ivec4", TYPE_IVEC4, { TYPE_INT, TYPE_IVEC3, TYPE_VOID } },
  1190. { "ivec4", TYPE_IVEC4, { TYPE_IVEC3, TYPE_INT, TYPE_VOID } },
  1191. { "ivec4", TYPE_IVEC4, { TYPE_IVEC2, TYPE_IVEC2, TYPE_VOID } },
  1192. { "uint", TYPE_UINT, { TYPE_UINT, TYPE_VOID } },
  1193. { "uvec2", TYPE_UVEC2, { TYPE_UINT, TYPE_VOID } },
  1194. { "uvec2", TYPE_UVEC2, { TYPE_UINT, TYPE_UINT, TYPE_VOID } },
  1195. { "uvec3", TYPE_UVEC3, { TYPE_UINT, TYPE_VOID } },
  1196. { "uvec3", TYPE_UVEC3, { TYPE_UINT, TYPE_UINT, TYPE_UINT, TYPE_VOID } },
  1197. { "uvec3", TYPE_UVEC3, { TYPE_UVEC2, TYPE_UINT, TYPE_VOID } },
  1198. { "uvec3", TYPE_UVEC3, { TYPE_UINT, TYPE_UVEC2, TYPE_VOID } },
  1199. { "uvec4", TYPE_UVEC4, { TYPE_UINT, TYPE_VOID } },
  1200. { "uvec4", TYPE_UVEC4, { TYPE_UINT, TYPE_UINT, TYPE_UINT, TYPE_UINT, TYPE_VOID } },
  1201. { "uvec4", TYPE_UVEC4, { TYPE_UINT, TYPE_UVEC2, TYPE_UINT, TYPE_VOID } },
  1202. { "uvec4", TYPE_UVEC4, { TYPE_UVEC2, TYPE_UINT, TYPE_UINT, TYPE_VOID } },
  1203. { "uvec4", TYPE_UVEC4, { TYPE_UINT, TYPE_UINT, TYPE_UVEC2, TYPE_VOID } },
  1204. { "uvec4", TYPE_UVEC4, { TYPE_UINT, TYPE_UVEC3, TYPE_VOID } },
  1205. { "uvec4", TYPE_UVEC4, { TYPE_UVEC3, TYPE_UINT, TYPE_VOID } },
  1206. { "uvec4", TYPE_UVEC4, { TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID } },
  1207. { "mat2", TYPE_MAT2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID } },
  1208. { "mat3", TYPE_MAT3, { TYPE_VEC3, TYPE_VEC3, TYPE_VEC3, TYPE_VOID } },
  1209. { "mat4", TYPE_MAT4, { TYPE_VEC4, TYPE_VEC4, TYPE_VEC4, TYPE_VEC4, TYPE_VOID } },
  1210. { "mat2", TYPE_MAT2, { TYPE_FLOAT, TYPE_VOID } },
  1211. { "mat3", TYPE_MAT3, { TYPE_FLOAT, TYPE_VOID } },
  1212. { "mat4", TYPE_MAT4, { TYPE_FLOAT, TYPE_VOID } },
  1213. //conversion scalars
  1214. { "int", TYPE_INT, { TYPE_BOOL, TYPE_VOID } },
  1215. { "int", TYPE_INT, { TYPE_INT, TYPE_VOID } },
  1216. { "int", TYPE_INT, { TYPE_UINT, TYPE_VOID } },
  1217. { "int", TYPE_INT, { TYPE_FLOAT, TYPE_VOID } },
  1218. { "float", TYPE_FLOAT, { TYPE_BOOL, TYPE_VOID } },
  1219. { "float", TYPE_FLOAT, { TYPE_INT, TYPE_VOID } },
  1220. { "float", TYPE_FLOAT, { TYPE_UINT, TYPE_VOID } },
  1221. { "float", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID } },
  1222. { "uint", TYPE_UINT, { TYPE_BOOL, TYPE_VOID } },
  1223. { "uint", TYPE_UINT, { TYPE_INT, TYPE_VOID } },
  1224. { "uint", TYPE_UINT, { TYPE_UINT, TYPE_VOID } },
  1225. { "uint", TYPE_UINT, { TYPE_FLOAT, TYPE_VOID } },
  1226. { "bool", TYPE_BOOL, { TYPE_BOOL, TYPE_VOID } },
  1227. { "bool", TYPE_BOOL, { TYPE_INT, TYPE_VOID } },
  1228. { "bool", TYPE_BOOL, { TYPE_UINT, TYPE_VOID } },
  1229. { "bool", TYPE_BOOL, { TYPE_FLOAT, TYPE_VOID } },
  1230. //conversion vectors
  1231. { "ivec2", TYPE_IVEC2, { TYPE_BVEC2, TYPE_VOID } },
  1232. { "ivec2", TYPE_IVEC2, { TYPE_IVEC2, TYPE_VOID } },
  1233. { "ivec2", TYPE_IVEC2, { TYPE_UVEC2, TYPE_VOID } },
  1234. { "ivec2", TYPE_IVEC2, { TYPE_VEC2, TYPE_VOID } },
  1235. { "vec2", TYPE_VEC2, { TYPE_BVEC2, TYPE_VOID } },
  1236. { "vec2", TYPE_VEC2, { TYPE_IVEC2, TYPE_VOID } },
  1237. { "vec2", TYPE_VEC2, { TYPE_UVEC2, TYPE_VOID } },
  1238. { "vec2", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID } },
  1239. { "uvec2", TYPE_UVEC2, { TYPE_BVEC2, TYPE_VOID } },
  1240. { "uvec2", TYPE_UVEC2, { TYPE_IVEC2, TYPE_VOID } },
  1241. { "uvec2", TYPE_UVEC2, { TYPE_UVEC2, TYPE_VOID } },
  1242. { "uvec2", TYPE_UVEC2, { TYPE_VEC2, TYPE_VOID } },
  1243. { "bvec2", TYPE_BVEC2, { TYPE_BVEC2, TYPE_VOID } },
  1244. { "bvec2", TYPE_BVEC2, { TYPE_IVEC2, TYPE_VOID } },
  1245. { "bvec2", TYPE_BVEC2, { TYPE_UVEC2, TYPE_VOID } },
  1246. { "bvec2", TYPE_BVEC2, { TYPE_VEC2, TYPE_VOID } },
  1247. { "ivec3", TYPE_IVEC3, { TYPE_BVEC3, TYPE_VOID } },
  1248. { "ivec3", TYPE_IVEC3, { TYPE_IVEC3, TYPE_VOID } },
  1249. { "ivec3", TYPE_IVEC3, { TYPE_UVEC3, TYPE_VOID } },
  1250. { "ivec3", TYPE_IVEC3, { TYPE_VEC3, TYPE_VOID } },
  1251. { "vec3", TYPE_VEC3, { TYPE_BVEC3, TYPE_VOID } },
  1252. { "vec3", TYPE_VEC3, { TYPE_IVEC3, TYPE_VOID } },
  1253. { "vec3", TYPE_VEC3, { TYPE_UVEC3, TYPE_VOID } },
  1254. { "vec3", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID } },
  1255. { "uvec3", TYPE_UVEC3, { TYPE_BVEC3, TYPE_VOID } },
  1256. { "uvec3", TYPE_UVEC3, { TYPE_IVEC3, TYPE_VOID } },
  1257. { "uvec3", TYPE_UVEC3, { TYPE_UVEC3, TYPE_VOID } },
  1258. { "uvec3", TYPE_UVEC3, { TYPE_VEC3, TYPE_VOID } },
  1259. { "bvec3", TYPE_BVEC3, { TYPE_BVEC3, TYPE_VOID } },
  1260. { "bvec3", TYPE_BVEC3, { TYPE_IVEC3, TYPE_VOID } },
  1261. { "bvec3", TYPE_BVEC3, { TYPE_UVEC3, TYPE_VOID } },
  1262. { "bvec3", TYPE_BVEC3, { TYPE_VEC3, TYPE_VOID } },
  1263. { "ivec4", TYPE_IVEC4, { TYPE_BVEC4, TYPE_VOID } },
  1264. { "ivec4", TYPE_IVEC4, { TYPE_IVEC4, TYPE_VOID } },
  1265. { "ivec4", TYPE_IVEC4, { TYPE_UVEC4, TYPE_VOID } },
  1266. { "ivec4", TYPE_IVEC4, { TYPE_VEC4, TYPE_VOID } },
  1267. { "vec4", TYPE_VEC4, { TYPE_BVEC4, TYPE_VOID } },
  1268. { "vec4", TYPE_VEC4, { TYPE_IVEC4, TYPE_VOID } },
  1269. { "vec4", TYPE_VEC4, { TYPE_UVEC4, TYPE_VOID } },
  1270. { "vec4", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID } },
  1271. { "uvec4", TYPE_UVEC4, { TYPE_BVEC4, TYPE_VOID } },
  1272. { "uvec4", TYPE_UVEC4, { TYPE_IVEC4, TYPE_VOID } },
  1273. { "uvec4", TYPE_UVEC4, { TYPE_UVEC4, TYPE_VOID } },
  1274. { "uvec4", TYPE_UVEC4, { TYPE_VEC4, TYPE_VOID } },
  1275. { "bvec4", TYPE_BVEC4, { TYPE_BVEC4, TYPE_VOID } },
  1276. { "bvec4", TYPE_BVEC4, { TYPE_IVEC4, TYPE_VOID } },
  1277. { "bvec4", TYPE_BVEC4, { TYPE_UVEC4, TYPE_VOID } },
  1278. { "bvec4", TYPE_BVEC4, { TYPE_VEC4, TYPE_VOID } },
  1279. //conversion between matrixes
  1280. { "mat2", TYPE_MAT2, { TYPE_MAT3, TYPE_VOID } },
  1281. { "mat2", TYPE_MAT2, { TYPE_MAT4, TYPE_VOID } },
  1282. { "mat3", TYPE_MAT3, { TYPE_MAT2, TYPE_VOID } },
  1283. { "mat3", TYPE_MAT3, { TYPE_MAT4, TYPE_VOID } },
  1284. { "mat4", TYPE_MAT4, { TYPE_MAT2, TYPE_VOID } },
  1285. { "mat4", TYPE_MAT4, { TYPE_MAT3, TYPE_VOID } },
  1286. //builtins - trigonometry
  1287. { "radians", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID } },
  1288. { "radians", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID } },
  1289. { "radians", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID } },
  1290. { "radians", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID } },
  1291. { "degrees", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID } },
  1292. { "degrees", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID } },
  1293. { "degrees", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID } },
  1294. { "degrees", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID } },
  1295. { "sin", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID } },
  1296. { "sin", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID } },
  1297. { "sin", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID } },
  1298. { "sin", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID } },
  1299. { "cos", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID } },
  1300. { "cos", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID } },
  1301. { "cos", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID } },
  1302. { "cos", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID } },
  1303. { "tan", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID } },
  1304. { "tan", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID } },
  1305. { "tan", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID } },
  1306. { "tan", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID } },
  1307. { "asin", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID } },
  1308. { "asin", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID } },
  1309. { "asin", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID } },
  1310. { "asin", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID } },
  1311. { "acos", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID } },
  1312. { "acos", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID } },
  1313. { "acos", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID } },
  1314. { "acos", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID } },
  1315. { "atan", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID } },
  1316. { "atan", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID } },
  1317. { "atan", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID } },
  1318. { "atan", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID } },
  1319. { "atan", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID } },
  1320. { "atan", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID } },
  1321. { "atan", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID } },
  1322. { "atan", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID } },
  1323. { "sinh", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID } },
  1324. { "sinh", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID } },
  1325. { "sinh", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID } },
  1326. { "sinh", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID } },
  1327. { "cosh", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID } },
  1328. { "cosh", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID } },
  1329. { "cosh", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID } },
  1330. { "cosh", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID } },
  1331. { "tanh", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID } },
  1332. { "tanh", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID } },
  1333. { "tanh", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID } },
  1334. { "tanh", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID } },
  1335. { "asinh", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID } },
  1336. { "asinh", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID } },
  1337. { "asinh", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID } },
  1338. { "asinh", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID } },
  1339. { "acosh", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID } },
  1340. { "acosh", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID } },
  1341. { "acosh", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID } },
  1342. { "acosh", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID } },
  1343. { "atanh", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID } },
  1344. { "atanh", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID } },
  1345. { "atanh", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID } },
  1346. { "atanh", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID } },
  1347. //builtins - exponential
  1348. { "pow", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID } },
  1349. { "pow", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID } },
  1350. { "pow", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID } },
  1351. { "pow", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID } },
  1352. { "exp", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID } },
  1353. { "exp", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID } },
  1354. { "exp", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID } },
  1355. { "exp", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID } },
  1356. { "log", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID } },
  1357. { "log", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID } },
  1358. { "log", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID } },
  1359. { "log", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID } },
  1360. { "exp2", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID } },
  1361. { "exp2", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID } },
  1362. { "exp2", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID } },
  1363. { "exp2", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID } },
  1364. { "log2", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID } },
  1365. { "log2", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID } },
  1366. { "log2", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID } },
  1367. { "log2", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID } },
  1368. { "sqrt", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID } },
  1369. { "sqrt", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID } },
  1370. { "sqrt", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID } },
  1371. { "sqrt", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID } },
  1372. { "inversesqrt", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID } },
  1373. { "inversesqrt", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID } },
  1374. { "inversesqrt", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID } },
  1375. { "inversesqrt", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID } },
  1376. //builtins - common
  1377. { "abs", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID } },
  1378. { "abs", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID } },
  1379. { "abs", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID } },
  1380. { "abs", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID } },
  1381. { "abs", TYPE_INT, { TYPE_INT, TYPE_VOID } },
  1382. { "abs", TYPE_IVEC2, { TYPE_IVEC2, TYPE_VOID } },
  1383. { "abs", TYPE_IVEC3, { TYPE_IVEC3, TYPE_VOID } },
  1384. { "abs", TYPE_IVEC4, { TYPE_IVEC4, TYPE_VOID } },
  1385. { "sign", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID } },
  1386. { "sign", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID } },
  1387. { "sign", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID } },
  1388. { "sign", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID } },
  1389. { "sign", TYPE_INT, { TYPE_INT, TYPE_VOID } },
  1390. { "sign", TYPE_IVEC2, { TYPE_IVEC2, TYPE_VOID } },
  1391. { "sign", TYPE_IVEC3, { TYPE_IVEC3, TYPE_VOID } },
  1392. { "sign", TYPE_IVEC4, { TYPE_IVEC4, TYPE_VOID } },
  1393. { "floor", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID } },
  1394. { "floor", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID } },
  1395. { "floor", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID } },
  1396. { "floor", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID } },
  1397. { "trunc", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID } },
  1398. { "trunc", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID } },
  1399. { "trunc", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID } },
  1400. { "trunc", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID } },
  1401. { "round", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID } },
  1402. { "round", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID } },
  1403. { "round", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID } },
  1404. { "round", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID } },
  1405. { "roundEven", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID } },
  1406. { "roundEven", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID } },
  1407. { "roundEven", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID } },
  1408. { "roundEven", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID } },
  1409. { "ceil", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID } },
  1410. { "ceil", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID } },
  1411. { "ceil", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID } },
  1412. { "ceil", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID } },
  1413. { "fract", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID } },
  1414. { "fract", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID } },
  1415. { "fract", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID } },
  1416. { "fract", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID } },
  1417. { "mod", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID } },
  1418. { "mod", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID } },
  1419. { "mod", TYPE_VEC2, { TYPE_VEC2, TYPE_FLOAT, TYPE_VOID } },
  1420. { "mod", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID } },
  1421. { "mod", TYPE_VEC3, { TYPE_VEC3, TYPE_FLOAT, TYPE_VOID } },
  1422. { "mod", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID } },
  1423. { "mod", TYPE_VEC4, { TYPE_VEC4, TYPE_FLOAT, TYPE_VOID } },
  1424. { "modf", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID } },
  1425. { "modf", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID } },
  1426. { "modf", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID } },
  1427. { "modf", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID } },
  1428. { "min", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID } },
  1429. { "min", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID } },
  1430. { "min", TYPE_VEC2, { TYPE_VEC2, TYPE_FLOAT, TYPE_VOID } },
  1431. { "min", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID } },
  1432. { "min", TYPE_VEC3, { TYPE_VEC3, TYPE_FLOAT, TYPE_VOID } },
  1433. { "min", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID } },
  1434. { "min", TYPE_VEC4, { TYPE_VEC4, TYPE_FLOAT, TYPE_VOID } },
  1435. { "min", TYPE_INT, { TYPE_INT, TYPE_INT, TYPE_VOID } },
  1436. { "min", TYPE_IVEC2, { TYPE_IVEC2, TYPE_IVEC2, TYPE_VOID } },
  1437. { "min", TYPE_IVEC2, { TYPE_IVEC2, TYPE_INT, TYPE_VOID } },
  1438. { "min", TYPE_IVEC3, { TYPE_IVEC3, TYPE_IVEC3, TYPE_VOID } },
  1439. { "min", TYPE_IVEC3, { TYPE_IVEC3, TYPE_INT, TYPE_VOID } },
  1440. { "min", TYPE_IVEC4, { TYPE_IVEC4, TYPE_IVEC4, TYPE_VOID } },
  1441. { "min", TYPE_IVEC4, { TYPE_IVEC4, TYPE_INT, TYPE_VOID } },
  1442. { "min", TYPE_UINT, { TYPE_UINT, TYPE_UINT, TYPE_VOID } },
  1443. { "min", TYPE_UVEC2, { TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID } },
  1444. { "min", TYPE_UVEC2, { TYPE_UVEC2, TYPE_UINT, TYPE_VOID } },
  1445. { "min", TYPE_UVEC3, { TYPE_UVEC3, TYPE_UVEC3, TYPE_VOID } },
  1446. { "min", TYPE_UVEC3, { TYPE_UVEC3, TYPE_UINT, TYPE_VOID } },
  1447. { "min", TYPE_UVEC4, { TYPE_UVEC4, TYPE_UVEC4, TYPE_VOID } },
  1448. { "min", TYPE_UVEC4, { TYPE_UVEC4, TYPE_UINT, TYPE_VOID } },
  1449. { "max", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID } },
  1450. { "max", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID } },
  1451. { "max", TYPE_VEC2, { TYPE_VEC2, TYPE_FLOAT, TYPE_VOID } },
  1452. { "max", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID } },
  1453. { "max", TYPE_VEC3, { TYPE_VEC3, TYPE_FLOAT, TYPE_VOID } },
  1454. { "max", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID } },
  1455. { "max", TYPE_VEC4, { TYPE_VEC4, TYPE_FLOAT, TYPE_VOID } },
  1456. { "max", TYPE_INT, { TYPE_INT, TYPE_INT, TYPE_VOID } },
  1457. { "max", TYPE_IVEC2, { TYPE_IVEC2, TYPE_IVEC2, TYPE_VOID } },
  1458. { "max", TYPE_IVEC2, { TYPE_IVEC2, TYPE_INT, TYPE_VOID } },
  1459. { "max", TYPE_IVEC3, { TYPE_IVEC3, TYPE_IVEC3, TYPE_VOID } },
  1460. { "max", TYPE_IVEC3, { TYPE_IVEC3, TYPE_INT, TYPE_VOID } },
  1461. { "max", TYPE_IVEC4, { TYPE_IVEC4, TYPE_IVEC4, TYPE_VOID } },
  1462. { "max", TYPE_IVEC4, { TYPE_IVEC4, TYPE_INT, TYPE_VOID } },
  1463. { "max", TYPE_UINT, { TYPE_UINT, TYPE_UINT, TYPE_VOID } },
  1464. { "max", TYPE_UVEC2, { TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID } },
  1465. { "max", TYPE_UVEC2, { TYPE_UVEC2, TYPE_UINT, TYPE_VOID } },
  1466. { "max", TYPE_UVEC3, { TYPE_UVEC3, TYPE_UVEC3, TYPE_VOID } },
  1467. { "max", TYPE_UVEC3, { TYPE_UVEC3, TYPE_UINT, TYPE_VOID } },
  1468. { "max", TYPE_UVEC4, { TYPE_UVEC4, TYPE_UVEC4, TYPE_VOID } },
  1469. { "max", TYPE_UVEC4, { TYPE_UVEC4, TYPE_UINT, TYPE_VOID } },
  1470. { "clamp", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID } },
  1471. { "clamp", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VEC2, TYPE_VOID } },
  1472. { "clamp", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VEC3, TYPE_VOID } },
  1473. { "clamp", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VEC4, TYPE_VOID } },
  1474. { "clamp", TYPE_VEC2, { TYPE_VEC2, TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID } },
  1475. { "clamp", TYPE_VEC3, { TYPE_VEC3, TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID } },
  1476. { "clamp", TYPE_VEC4, { TYPE_VEC4, TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID } },
  1477. { "clamp", TYPE_INT, { TYPE_INT, TYPE_INT, TYPE_INT, TYPE_VOID } },
  1478. { "clamp", TYPE_IVEC2, { TYPE_IVEC2, TYPE_IVEC2, TYPE_IVEC2, TYPE_VOID } },
  1479. { "clamp", TYPE_IVEC3, { TYPE_IVEC3, TYPE_IVEC3, TYPE_IVEC3, TYPE_VOID } },
  1480. { "clamp", TYPE_IVEC4, { TYPE_IVEC4, TYPE_IVEC4, TYPE_IVEC4, TYPE_VOID } },
  1481. { "clamp", TYPE_IVEC2, { TYPE_IVEC2, TYPE_INT, TYPE_INT, TYPE_VOID } },
  1482. { "clamp", TYPE_IVEC3, { TYPE_IVEC3, TYPE_INT, TYPE_INT, TYPE_VOID } },
  1483. { "clamp", TYPE_IVEC4, { TYPE_IVEC4, TYPE_INT, TYPE_INT, TYPE_VOID } },
  1484. { "clamp", TYPE_UINT, { TYPE_UINT, TYPE_UINT, TYPE_UINT, TYPE_VOID } },
  1485. { "clamp", TYPE_UVEC2, { TYPE_UVEC2, TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID } },
  1486. { "clamp", TYPE_UVEC3, { TYPE_UVEC3, TYPE_UVEC3, TYPE_UVEC3, TYPE_VOID } },
  1487. { "clamp", TYPE_UVEC4, { TYPE_UVEC4, TYPE_UVEC4, TYPE_UVEC4, TYPE_VOID } },
  1488. { "clamp", TYPE_UVEC2, { TYPE_UVEC2, TYPE_UINT, TYPE_UINT, TYPE_VOID } },
  1489. { "clamp", TYPE_UVEC3, { TYPE_UVEC3, TYPE_UINT, TYPE_UINT, TYPE_VOID } },
  1490. { "clamp", TYPE_UVEC4, { TYPE_UVEC4, TYPE_UINT, TYPE_UINT, TYPE_VOID } },
  1491. { "mix", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID } },
  1492. { "mix", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_BOOL, TYPE_VOID } },
  1493. { "mix", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_FLOAT, TYPE_VOID } },
  1494. { "mix", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_BOOL, TYPE_VOID } },
  1495. { "mix", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_BVEC2, TYPE_VOID } },
  1496. { "mix", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VEC2, TYPE_VOID } },
  1497. { "mix", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID } },
  1498. { "mix", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_BOOL, TYPE_VOID } },
  1499. { "mix", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_BVEC3, TYPE_VOID } },
  1500. { "mix", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VEC3, TYPE_VOID } },
  1501. { "mix", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID } },
  1502. { "mix", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_BOOL, TYPE_VOID } },
  1503. { "mix", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_BVEC4, TYPE_VOID } },
  1504. { "mix", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VEC4, TYPE_VOID } },
  1505. { "step", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID } },
  1506. { "step", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID } },
  1507. { "step", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID } },
  1508. { "step", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID } },
  1509. { "step", TYPE_VEC2, { TYPE_FLOAT, TYPE_VEC2, TYPE_VOID } },
  1510. { "step", TYPE_VEC3, { TYPE_FLOAT, TYPE_VEC3, TYPE_VOID } },
  1511. { "step", TYPE_VEC4, { TYPE_FLOAT, TYPE_VEC4, TYPE_VOID } },
  1512. { "smoothstep", TYPE_FLOAT, { TYPE_FLOAT, TYPE_FLOAT, TYPE_FLOAT, TYPE_VOID } },
  1513. { "smoothstep", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VEC2, TYPE_VOID } },
  1514. { "smoothstep", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VEC3, TYPE_VOID } },
  1515. { "smoothstep", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VEC4, TYPE_VOID } },
  1516. { "smoothstep", TYPE_VEC2, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VEC2, TYPE_VOID } },
  1517. { "smoothstep", TYPE_VEC3, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VEC3, TYPE_VOID } },
  1518. { "smoothstep", TYPE_VEC4, { TYPE_FLOAT, TYPE_FLOAT, TYPE_VEC4, TYPE_VOID } },
  1519. { "isnan", TYPE_BOOL, { TYPE_FLOAT, TYPE_VOID } },
  1520. { "isnan", TYPE_BVEC2, { TYPE_VEC2, TYPE_VOID } },
  1521. { "isnan", TYPE_BVEC3, { TYPE_VEC3, TYPE_VOID } },
  1522. { "isnan", TYPE_BVEC4, { TYPE_VEC4, TYPE_VOID } },
  1523. { "isinf", TYPE_BOOL, { TYPE_FLOAT, TYPE_VOID } },
  1524. { "isinf", TYPE_BVEC2, { TYPE_VEC2, TYPE_VOID } },
  1525. { "isinf", TYPE_BVEC3, { TYPE_VEC3, TYPE_VOID } },
  1526. { "isinf", TYPE_BVEC4, { TYPE_VEC4, TYPE_VOID } },
  1527. { "floatBitsToInt", TYPE_INT, { TYPE_FLOAT, TYPE_VOID } },
  1528. { "floatBitsToInt", TYPE_IVEC2, { TYPE_VEC2, TYPE_VOID } },
  1529. { "floatBitsToInt", TYPE_IVEC3, { TYPE_VEC3, TYPE_VOID } },
  1530. { "floatBitsToInt", TYPE_IVEC4, { TYPE_VEC4, TYPE_VOID } },
  1531. { "floatBitsToUint", TYPE_UINT, { TYPE_FLOAT, TYPE_VOID } },
  1532. { "floatBitsToUint", TYPE_UVEC2, { TYPE_VEC2, TYPE_VOID } },
  1533. { "floatBitsToUint", TYPE_UVEC3, { TYPE_VEC3, TYPE_VOID } },
  1534. { "floatBitsToUint", TYPE_UVEC4, { TYPE_VEC4, TYPE_VOID } },
  1535. { "intBitsToFloat", TYPE_FLOAT, { TYPE_INT, TYPE_VOID } },
  1536. { "intBitsToFloat", TYPE_VEC2, { TYPE_IVEC2, TYPE_VOID } },
  1537. { "intBitsToFloat", TYPE_VEC3, { TYPE_IVEC3, TYPE_VOID } },
  1538. { "intBitsToFloat", TYPE_VEC4, { TYPE_IVEC4, TYPE_VOID } },
  1539. { "uintBitsToFloat", TYPE_FLOAT, { TYPE_UINT, TYPE_VOID } },
  1540. { "uintBitsToFloat", TYPE_VEC2, { TYPE_UVEC2, TYPE_VOID } },
  1541. { "uintBitsToFloat", TYPE_VEC3, { TYPE_UVEC3, TYPE_VOID } },
  1542. { "uintBitsToFloat", TYPE_VEC4, { TYPE_UVEC4, TYPE_VOID } },
  1543. //builtins - geometric
  1544. { "length", TYPE_FLOAT, { TYPE_VEC2, TYPE_VOID } },
  1545. { "length", TYPE_FLOAT, { TYPE_VEC3, TYPE_VOID } },
  1546. { "length", TYPE_FLOAT, { TYPE_VEC4, TYPE_VOID } },
  1547. { "distance", TYPE_FLOAT, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID } },
  1548. { "distance", TYPE_FLOAT, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID } },
  1549. { "distance", TYPE_FLOAT, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID } },
  1550. { "dot", TYPE_FLOAT, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID } },
  1551. { "dot", TYPE_FLOAT, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID } },
  1552. { "dot", TYPE_FLOAT, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID } },
  1553. { "cross", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID } },
  1554. { "normalize", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID } },
  1555. { "normalize", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID } },
  1556. { "normalize", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID } },
  1557. { "reflect", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID } },
  1558. { "refract", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID } },
  1559. { "faceforward", TYPE_VEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VEC2, TYPE_VOID } },
  1560. { "faceforward", TYPE_VEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VEC3, TYPE_VOID } },
  1561. { "faceforward", TYPE_VEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VEC4, TYPE_VOID } },
  1562. { "matrixCompMult", TYPE_MAT2, { TYPE_MAT2, TYPE_MAT2, TYPE_VOID } },
  1563. { "matrixCompMult", TYPE_MAT3, { TYPE_MAT3, TYPE_MAT3, TYPE_VOID } },
  1564. { "matrixCompMult", TYPE_MAT4, { TYPE_MAT4, TYPE_MAT4, TYPE_VOID } },
  1565. { "outerProduct", TYPE_MAT2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID } },
  1566. { "outerProduct", TYPE_MAT3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID } },
  1567. { "outerProduct", TYPE_MAT4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID } },
  1568. { "transpose", TYPE_MAT2, { TYPE_MAT2, TYPE_VOID } },
  1569. { "transpose", TYPE_MAT3, { TYPE_MAT3, TYPE_VOID } },
  1570. { "transpose", TYPE_MAT4, { TYPE_MAT4, TYPE_VOID } },
  1571. { "determinant", TYPE_FLOAT, { TYPE_MAT2, TYPE_VOID } },
  1572. { "determinant", TYPE_FLOAT, { TYPE_MAT3, TYPE_VOID } },
  1573. { "determinant", TYPE_FLOAT, { TYPE_MAT4, TYPE_VOID } },
  1574. { "inverse", TYPE_MAT2, { TYPE_MAT2, TYPE_VOID } },
  1575. { "inverse", TYPE_MAT3, { TYPE_MAT3, TYPE_VOID } },
  1576. { "inverse", TYPE_MAT4, { TYPE_MAT4, TYPE_VOID } },
  1577. { "lessThan", TYPE_BVEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID } },
  1578. { "lessThan", TYPE_BVEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID } },
  1579. { "lessThan", TYPE_BVEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID } },
  1580. { "lessThan", TYPE_BVEC2, { TYPE_IVEC2, TYPE_IVEC2, TYPE_VOID } },
  1581. { "lessThan", TYPE_BVEC3, { TYPE_IVEC3, TYPE_IVEC3, TYPE_VOID } },
  1582. { "lessThan", TYPE_BVEC4, { TYPE_IVEC4, TYPE_IVEC4, TYPE_VOID } },
  1583. { "lessThan", TYPE_BVEC2, { TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID } },
  1584. { "lessThan", TYPE_BVEC3, { TYPE_UVEC3, TYPE_UVEC3, TYPE_VOID } },
  1585. { "lessThan", TYPE_BVEC4, { TYPE_UVEC4, TYPE_UVEC4, TYPE_VOID } },
  1586. { "greaterThan", TYPE_BVEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID } },
  1587. { "greaterThan", TYPE_BVEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID } },
  1588. { "greaterThan", TYPE_BVEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID } },
  1589. { "greaterThan", TYPE_BVEC2, { TYPE_IVEC2, TYPE_IVEC2, TYPE_VOID } },
  1590. { "greaterThan", TYPE_BVEC3, { TYPE_IVEC3, TYPE_IVEC3, TYPE_VOID } },
  1591. { "greaterThan", TYPE_BVEC4, { TYPE_IVEC4, TYPE_IVEC4, TYPE_VOID } },
  1592. { "greaterThan", TYPE_BVEC2, { TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID } },
  1593. { "greaterThan", TYPE_BVEC3, { TYPE_UVEC3, TYPE_UVEC3, TYPE_VOID } },
  1594. { "greaterThan", TYPE_BVEC4, { TYPE_UVEC4, TYPE_UVEC4, TYPE_VOID } },
  1595. { "lessThanEqual", TYPE_BVEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID } },
  1596. { "lessThanEqual", TYPE_BVEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID } },
  1597. { "lessThanEqual", TYPE_BVEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID } },
  1598. { "lessThanEqual", TYPE_BVEC2, { TYPE_IVEC2, TYPE_IVEC2, TYPE_VOID } },
  1599. { "lessThanEqual", TYPE_BVEC3, { TYPE_IVEC3, TYPE_IVEC3, TYPE_VOID } },
  1600. { "lessThanEqual", TYPE_BVEC4, { TYPE_IVEC4, TYPE_IVEC4, TYPE_VOID } },
  1601. { "lessThanEqual", TYPE_BVEC2, { TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID } },
  1602. { "lessThanEqual", TYPE_BVEC3, { TYPE_UVEC3, TYPE_UVEC3, TYPE_VOID } },
  1603. { "lessThanEqual", TYPE_BVEC4, { TYPE_UVEC4, TYPE_UVEC4, TYPE_VOID } },
  1604. { "greaterThanEqual", TYPE_BVEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID } },
  1605. { "greaterThanEqual", TYPE_BVEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID } },
  1606. { "greaterThanEqual", TYPE_BVEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID } },
  1607. { "greaterThanEqual", TYPE_BVEC2, { TYPE_IVEC2, TYPE_IVEC2, TYPE_VOID } },
  1608. { "greaterThanEqual", TYPE_BVEC3, { TYPE_IVEC3, TYPE_IVEC3, TYPE_VOID } },
  1609. { "greaterThanEqual", TYPE_BVEC4, { TYPE_IVEC4, TYPE_IVEC4, TYPE_VOID } },
  1610. { "greaterThanEqual", TYPE_BVEC2, { TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID } },
  1611. { "greaterThanEqual", TYPE_BVEC3, { TYPE_UVEC3, TYPE_UVEC3, TYPE_VOID } },
  1612. { "greaterThanEqual", TYPE_BVEC4, { TYPE_UVEC4, TYPE_UVEC4, TYPE_VOID } },
  1613. { "equal", TYPE_BVEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID } },
  1614. { "equal", TYPE_BVEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID } },
  1615. { "equal", TYPE_BVEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID } },
  1616. { "equal", TYPE_BVEC2, { TYPE_IVEC2, TYPE_IVEC2, TYPE_VOID } },
  1617. { "equal", TYPE_BVEC3, { TYPE_IVEC3, TYPE_IVEC3, TYPE_VOID } },
  1618. { "equal", TYPE_BVEC4, { TYPE_IVEC4, TYPE_IVEC4, TYPE_VOID } },
  1619. { "equal", TYPE_BVEC2, { TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID } },
  1620. { "equal", TYPE_BVEC3, { TYPE_UVEC3, TYPE_UVEC3, TYPE_VOID } },
  1621. { "equal", TYPE_BVEC4, { TYPE_UVEC4, TYPE_UVEC4, TYPE_VOID } },
  1622. { "equal", TYPE_BVEC2, { TYPE_BVEC2, TYPE_BVEC2, TYPE_VOID } },
  1623. { "equal", TYPE_BVEC3, { TYPE_BVEC3, TYPE_BVEC3, TYPE_VOID } },
  1624. { "equal", TYPE_BVEC4, { TYPE_BVEC4, TYPE_BVEC4, TYPE_VOID } },
  1625. { "notEqual", TYPE_BVEC2, { TYPE_VEC2, TYPE_VEC2, TYPE_VOID } },
  1626. { "notEqual", TYPE_BVEC3, { TYPE_VEC3, TYPE_VEC3, TYPE_VOID } },
  1627. { "notEqual", TYPE_BVEC4, { TYPE_VEC4, TYPE_VEC4, TYPE_VOID } },
  1628. { "notEqual", TYPE_BVEC2, { TYPE_IVEC2, TYPE_IVEC2, TYPE_VOID } },
  1629. { "notEqual", TYPE_BVEC3, { TYPE_IVEC3, TYPE_IVEC3, TYPE_VOID } },
  1630. { "notEqual", TYPE_BVEC4, { TYPE_IVEC4, TYPE_IVEC4, TYPE_VOID } },
  1631. { "notEqual", TYPE_BVEC2, { TYPE_UVEC2, TYPE_UVEC2, TYPE_VOID } },
  1632. { "notEqual", TYPE_BVEC3, { TYPE_UVEC3, TYPE_UVEC3, TYPE_VOID } },
  1633. { "notEqual", TYPE_BVEC4, { TYPE_UVEC4, TYPE_UVEC4, TYPE_VOID } },
  1634. { "notEqual", TYPE_BVEC2, { TYPE_BVEC2, TYPE_BVEC2, TYPE_VOID } },
  1635. { "notEqual", TYPE_BVEC3, { TYPE_BVEC3, TYPE_BVEC3, TYPE_VOID } },
  1636. { "notEqual", TYPE_BVEC4, { TYPE_BVEC4, TYPE_BVEC4, TYPE_VOID } },
  1637. { "any", TYPE_BOOL, { TYPE_BVEC2, TYPE_VOID } },
  1638. { "any", TYPE_BOOL, { TYPE_BVEC3, TYPE_VOID } },
  1639. { "any", TYPE_BOOL, { TYPE_BVEC4, TYPE_VOID } },
  1640. { "all", TYPE_BOOL, { TYPE_BVEC2, TYPE_VOID } },
  1641. { "all", TYPE_BOOL, { TYPE_BVEC3, TYPE_VOID } },
  1642. { "all", TYPE_BOOL, { TYPE_BVEC4, TYPE_VOID } },
  1643. { "not", TYPE_BOOL, { TYPE_BVEC2, TYPE_VOID } },
  1644. { "not", TYPE_BOOL, { TYPE_BVEC3, TYPE_VOID } },
  1645. { "not", TYPE_BOOL, { TYPE_BVEC4, TYPE_VOID } },
  1646. //builtins - texture
  1647. { "textureSize", TYPE_IVEC2, { TYPE_SAMPLER2D, TYPE_INT, TYPE_VOID } },
  1648. { "textureSize", TYPE_IVEC2, { TYPE_ISAMPLER2D, TYPE_INT, TYPE_VOID } },
  1649. { "textureSize", TYPE_IVEC2, { TYPE_USAMPLER2D, TYPE_INT, TYPE_VOID } },
  1650. { "textureSize", TYPE_IVEC3, { TYPE_SAMPLER2DARRAY, TYPE_INT, TYPE_VOID } },
  1651. { "textureSize", TYPE_IVEC3, { TYPE_ISAMPLER2DARRAY, TYPE_INT, TYPE_VOID } },
  1652. { "textureSize", TYPE_IVEC3, { TYPE_USAMPLER2DARRAY, TYPE_INT, TYPE_VOID } },
  1653. { "textureSize", TYPE_IVEC3, { TYPE_SAMPLER3D, TYPE_INT, TYPE_VOID } },
  1654. { "textureSize", TYPE_IVEC3, { TYPE_ISAMPLER3D, TYPE_INT, TYPE_VOID } },
  1655. { "textureSize", TYPE_IVEC3, { TYPE_USAMPLER3D, TYPE_INT, TYPE_VOID } },
  1656. { "textureSize", TYPE_IVEC2, { TYPE_SAMPLERCUBE, TYPE_INT, TYPE_VOID } },
  1657. { "texture", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC2, TYPE_VOID } },
  1658. { "texture", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC2, TYPE_FLOAT, TYPE_VOID } },
  1659. { "texture", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC2, TYPE_VOID } },
  1660. { "texture", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC2, TYPE_FLOAT, TYPE_VOID } },
  1661. { "texture", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC2, TYPE_VOID } },
  1662. { "texture", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC2, TYPE_FLOAT, TYPE_VOID } },
  1663. { "texture", TYPE_VEC4, { TYPE_SAMPLER2DARRAY, TYPE_VEC3, TYPE_VOID } },
  1664. { "texture", TYPE_VEC4, { TYPE_SAMPLER2DARRAY, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID } },
  1665. { "texture", TYPE_UVEC4, { TYPE_USAMPLER2DARRAY, TYPE_VEC3, TYPE_VOID } },
  1666. { "texture", TYPE_UVEC4, { TYPE_USAMPLER2DARRAY, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID } },
  1667. { "texture", TYPE_IVEC4, { TYPE_ISAMPLER2DARRAY, TYPE_VEC3, TYPE_VOID } },
  1668. { "texture", TYPE_IVEC4, { TYPE_ISAMPLER2DARRAY, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID } },
  1669. { "texture", TYPE_VEC4, { TYPE_SAMPLER3D, TYPE_VEC3, TYPE_VOID } },
  1670. { "texture", TYPE_VEC4, { TYPE_SAMPLER3D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID } },
  1671. { "texture", TYPE_UVEC4, { TYPE_USAMPLER3D, TYPE_VEC3, TYPE_VOID } },
  1672. { "texture", TYPE_UVEC4, { TYPE_USAMPLER3D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID } },
  1673. { "texture", TYPE_IVEC4, { TYPE_ISAMPLER3D, TYPE_VEC3, TYPE_VOID } },
  1674. { "texture", TYPE_IVEC4, { TYPE_ISAMPLER3D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID } },
  1675. { "texture", TYPE_VEC4, { TYPE_SAMPLERCUBE, TYPE_VEC3, TYPE_VOID } },
  1676. { "texture", TYPE_VEC4, { TYPE_SAMPLERCUBE, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID } },
  1677. { "textureProj", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC3, TYPE_VOID } },
  1678. { "textureProj", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC4, TYPE_VOID } },
  1679. { "textureProj", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID } },
  1680. { "textureProj", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID } },
  1681. { "textureProj", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC3, TYPE_VOID } },
  1682. { "textureProj", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC4, TYPE_VOID } },
  1683. { "textureProj", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID } },
  1684. { "textureProj", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID } },
  1685. { "textureProj", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC3, TYPE_VOID } },
  1686. { "textureProj", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC4, TYPE_VOID } },
  1687. { "textureProj", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID } },
  1688. { "textureProj", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID } },
  1689. { "textureProj", TYPE_VEC4, { TYPE_SAMPLER3D, TYPE_VEC4, TYPE_VOID } },
  1690. { "textureProj", TYPE_VEC4, { TYPE_SAMPLER3D, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID } },
  1691. { "textureProj", TYPE_IVEC4, { TYPE_ISAMPLER3D, TYPE_VEC4, TYPE_VOID } },
  1692. { "textureProj", TYPE_IVEC4, { TYPE_ISAMPLER3D, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID } },
  1693. { "textureProj", TYPE_UVEC4, { TYPE_USAMPLER3D, TYPE_VEC4, TYPE_VOID } },
  1694. { "textureProj", TYPE_UVEC4, { TYPE_USAMPLER3D, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID } },
  1695. { "textureLod", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC2, TYPE_FLOAT, TYPE_VOID } },
  1696. { "textureLod", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC2, TYPE_FLOAT, TYPE_VOID } },
  1697. { "textureLod", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC2, TYPE_FLOAT, TYPE_VOID } },
  1698. { "textureLod", TYPE_VEC4, { TYPE_SAMPLER2DARRAY, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID } },
  1699. { "textureLod", TYPE_IVEC4, { TYPE_ISAMPLER2DARRAY, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID } },
  1700. { "textureLod", TYPE_UVEC4, { TYPE_USAMPLER2DARRAY, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID } },
  1701. { "textureLod", TYPE_VEC4, { TYPE_SAMPLER3D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID } },
  1702. { "textureLod", TYPE_IVEC4, { TYPE_ISAMPLER3D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID } },
  1703. { "textureLod", TYPE_UVEC4, { TYPE_USAMPLER3D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID } },
  1704. { "textureLod", TYPE_VEC4, { TYPE_SAMPLERCUBE, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID } },
  1705. { "texelFetch", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_IVEC2, TYPE_INT, TYPE_VOID } },
  1706. { "texelFetch", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_IVEC2, TYPE_INT, TYPE_VOID } },
  1707. { "texelFetch", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_IVEC2, TYPE_INT, TYPE_VOID } },
  1708. { "texelFetch", TYPE_VEC4, { TYPE_SAMPLER2DARRAY, TYPE_IVEC3, TYPE_INT, TYPE_VOID } },
  1709. { "texelFetch", TYPE_IVEC4, { TYPE_ISAMPLER2DARRAY, TYPE_IVEC3, TYPE_INT, TYPE_VOID } },
  1710. { "texelFetch", TYPE_UVEC4, { TYPE_USAMPLER2DARRAY, TYPE_IVEC3, TYPE_INT, TYPE_VOID } },
  1711. { "texelFetch", TYPE_VEC4, { TYPE_SAMPLER3D, TYPE_IVEC3, TYPE_INT, TYPE_VOID } },
  1712. { "texelFetch", TYPE_IVEC4, { TYPE_ISAMPLER3D, TYPE_IVEC3, TYPE_INT, TYPE_VOID } },
  1713. { "texelFetch", TYPE_UVEC4, { TYPE_USAMPLER3D, TYPE_IVEC3, TYPE_INT, TYPE_VOID } },
  1714. { "textureProjLod", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID } },
  1715. { "textureProjLod", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID } },
  1716. { "textureProjLod", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID } },
  1717. { "textureProjLod", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID } },
  1718. { "textureProjLod", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC3, TYPE_FLOAT, TYPE_VOID } },
  1719. { "textureProjLod", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC4, TYPE_FLOAT, TYPE_VOID } },
  1720. { "textureGrad", TYPE_VEC4, { TYPE_SAMPLER2D, TYPE_VEC2, TYPE_VEC2, TYPE_VEC2, TYPE_VOID } },
  1721. { "textureGrad", TYPE_IVEC4, { TYPE_ISAMPLER2D, TYPE_VEC2, TYPE_VEC2, TYPE_VEC2, TYPE_VOID } },
  1722. { "textureGrad", TYPE_UVEC4, { TYPE_USAMPLER2D, TYPE_VEC2, TYPE_VEC2, TYPE_VEC2, TYPE_VOID } },
  1723. { "textureGrad", TYPE_VEC4, { TYPE_SAMPLER2DARRAY, TYPE_VEC3, TYPE_VEC2, TYPE_VEC2, TYPE_VOID } },
  1724. { "textureGrad", TYPE_IVEC4, { TYPE_ISAMPLER2DARRAY, TYPE_VEC3, TYPE_VEC2, TYPE_VEC2, TYPE_VOID } },
  1725. { "textureGrad", TYPE_UVEC4, { TYPE_USAMPLER2DARRAY, TYPE_VEC3, TYPE_VEC2, TYPE_VEC2, TYPE_VOID } },
  1726. { "textureGrad", TYPE_VEC4, { TYPE_SAMPLER3D, TYPE_VEC3, TYPE_VEC3, TYPE_VEC3, TYPE_VOID } },
  1727. { "textureGrad", TYPE_IVEC4, { TYPE_ISAMPLER3D, TYPE_VEC3, TYPE_VEC3, TYPE_VEC3, TYPE_VOID } },
  1728. { "textureGrad", TYPE_UVEC4, { TYPE_USAMPLER3D, TYPE_VEC3, TYPE_VEC3, TYPE_VEC3, TYPE_VOID } },
  1729. { "textureGrad", TYPE_VEC4, { TYPE_SAMPLERCUBE, TYPE_VEC3, TYPE_VEC3, TYPE_VEC3, TYPE_VOID } },
  1730. { "dFdx", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID } },
  1731. { "dFdx", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID } },
  1732. { "dFdx", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID } },
  1733. { "dFdx", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID } },
  1734. { "dFdy", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID } },
  1735. { "dFdy", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID } },
  1736. { "dFdy", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID } },
  1737. { "dFdy", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID } },
  1738. { "fwidth", TYPE_FLOAT, { TYPE_FLOAT, TYPE_VOID } },
  1739. { "fwidth", TYPE_VEC2, { TYPE_VEC2, TYPE_VOID } },
  1740. { "fwidth", TYPE_VEC3, { TYPE_VEC3, TYPE_VOID } },
  1741. { "fwidth", TYPE_VEC4, { TYPE_VEC4, TYPE_VOID } },
  1742. { NULL, TYPE_VOID, { TYPE_VOID } }
  1743. };
  1744. bool ShaderLanguage::_validate_function_call(BlockNode *p_block, OperatorNode *p_func, DataType *r_ret_type) {
  1745. ERR_FAIL_COND_V(p_func->op != OP_CALL && p_func->op != OP_CONSTRUCT, NULL);
  1746. Vector<DataType> args;
  1747. ERR_FAIL_COND_V(p_func->arguments[0]->type != Node::TYPE_VARIABLE, NULL);
  1748. StringName name = static_cast<VariableNode *>(p_func->arguments[0])->name.operator String();
  1749. for (int i = 1; i < p_func->arguments.size(); i++) {
  1750. args.push_back(p_func->arguments[i]->get_datatype());
  1751. }
  1752. int argcount = args.size();
  1753. bool failed_builtin = false;
  1754. if (argcount <= 4) {
  1755. // test builtins
  1756. int idx = 0;
  1757. while (builtin_func_defs[idx].name) {
  1758. if (name == builtin_func_defs[idx].name) {
  1759. failed_builtin = true;
  1760. bool fail = false;
  1761. for (int i = 0; i < argcount; i++) {
  1762. if (get_scalar_type(args[i]) == args[i] && p_func->arguments[i + 1]->type == Node::TYPE_CONSTANT && convert_constant(static_cast<ConstantNode *>(p_func->arguments[i + 1]), builtin_func_defs[idx].args[i])) {
  1763. //all good
  1764. } else if (args[i] != builtin_func_defs[idx].args[i]) {
  1765. fail = true;
  1766. break;
  1767. }
  1768. }
  1769. if (!fail && argcount < 4 && builtin_func_defs[idx].args[argcount] != TYPE_VOID)
  1770. fail = true; //make sure the number of arguments matches
  1771. if (!fail) {
  1772. if (r_ret_type)
  1773. *r_ret_type = builtin_func_defs[idx].rettype;
  1774. return true;
  1775. }
  1776. }
  1777. idx++;
  1778. }
  1779. }
  1780. if (failed_builtin) {
  1781. String err = "Invalid arguments for built-in function: " + String(name) + "(";
  1782. for (int i = 0; i < argcount; i++) {
  1783. if (i > 0)
  1784. err += ",";
  1785. if (p_func->arguments[i + 1]->type == Node::TYPE_CONSTANT && p_func->arguments[i + 1]->get_datatype() == TYPE_INT && static_cast<ConstantNode *>(p_func->arguments[i + 1])->values[0].sint < 0) {
  1786. err += "-";
  1787. }
  1788. err += get_datatype_name(args[i]);
  1789. }
  1790. err += ")";
  1791. _set_error(err);
  1792. return false;
  1793. }
  1794. // try existing functions..
  1795. StringName exclude_function;
  1796. BlockNode *block = p_block;
  1797. while (block) {
  1798. if (block->parent_function) {
  1799. exclude_function = block->parent_function->name;
  1800. }
  1801. block = block->parent_block;
  1802. }
  1803. if (name == exclude_function) {
  1804. _set_error("Recursion is not allowed");
  1805. return false;
  1806. }
  1807. for (int i = 0; i < shader->functions.size(); i++) {
  1808. if (name != shader->functions[i].name)
  1809. continue;
  1810. if (!shader->functions[i].callable) {
  1811. _set_error("Function '" + String(name) + " can't be called from source code.");
  1812. return false;
  1813. }
  1814. FunctionNode *pfunc = shader->functions[i].function;
  1815. if (pfunc->arguments.size() != args.size())
  1816. continue;
  1817. bool fail = false;
  1818. for (int i = 0; i < args.size(); i++) {
  1819. if (get_scalar_type(args[i]) == args[i] && p_func->arguments[i + 1]->type == Node::TYPE_CONSTANT && convert_constant(static_cast<ConstantNode *>(p_func->arguments[i + 1]), pfunc->arguments[i].type)) {
  1820. //all good
  1821. } else if (args[i] != pfunc->arguments[i].type) {
  1822. fail = true;
  1823. break;
  1824. }
  1825. }
  1826. if (!fail) {
  1827. if (r_ret_type)
  1828. *r_ret_type = pfunc->return_type;
  1829. return true;
  1830. }
  1831. }
  1832. return false;
  1833. }
  1834. bool ShaderLanguage::_parse_function_arguments(BlockNode *p_block, const Map<StringName, BuiltInInfo> &p_builtin_types, OperatorNode *p_func, int *r_complete_arg) {
  1835. TkPos pos = _get_tkpos();
  1836. Token tk = _get_token();
  1837. if (tk.type == TK_PARENTHESIS_CLOSE) {
  1838. return true;
  1839. }
  1840. _set_tkpos(pos);
  1841. while (true) {
  1842. if (r_complete_arg) {
  1843. pos = _get_tkpos();
  1844. tk = _get_token();
  1845. if (tk.type == TK_CURSOR) {
  1846. *r_complete_arg = p_func->arguments.size() - 1;
  1847. } else {
  1848. _set_tkpos(pos);
  1849. }
  1850. }
  1851. Node *arg = _parse_and_reduce_expression(p_block, p_builtin_types);
  1852. if (!arg) {
  1853. return false;
  1854. }
  1855. p_func->arguments.push_back(arg);
  1856. tk = _get_token();
  1857. if (tk.type == TK_PARENTHESIS_CLOSE) {
  1858. return true;
  1859. } else if (tk.type != TK_COMMA) {
  1860. // something is broken
  1861. _set_error("Expected ',' or ')' after argument");
  1862. return false;
  1863. }
  1864. }
  1865. return true;
  1866. }
  1867. bool ShaderLanguage::is_token_operator(TokenType p_type) {
  1868. return (p_type == TK_OP_EQUAL ||
  1869. p_type == TK_OP_NOT_EQUAL ||
  1870. p_type == TK_OP_LESS ||
  1871. p_type == TK_OP_LESS_EQUAL ||
  1872. p_type == TK_OP_GREATER ||
  1873. p_type == TK_OP_GREATER_EQUAL ||
  1874. p_type == TK_OP_AND ||
  1875. p_type == TK_OP_OR ||
  1876. p_type == TK_OP_NOT ||
  1877. p_type == TK_OP_ADD ||
  1878. p_type == TK_OP_SUB ||
  1879. p_type == TK_OP_MUL ||
  1880. p_type == TK_OP_DIV ||
  1881. p_type == TK_OP_MOD ||
  1882. p_type == TK_OP_SHIFT_LEFT ||
  1883. p_type == TK_OP_SHIFT_RIGHT ||
  1884. p_type == TK_OP_ASSIGN ||
  1885. p_type == TK_OP_ASSIGN_ADD ||
  1886. p_type == TK_OP_ASSIGN_SUB ||
  1887. p_type == TK_OP_ASSIGN_MUL ||
  1888. p_type == TK_OP_ASSIGN_DIV ||
  1889. p_type == TK_OP_ASSIGN_MOD ||
  1890. p_type == TK_OP_ASSIGN_SHIFT_LEFT ||
  1891. p_type == TK_OP_ASSIGN_SHIFT_RIGHT ||
  1892. p_type == TK_OP_ASSIGN_BIT_AND ||
  1893. p_type == TK_OP_ASSIGN_BIT_OR ||
  1894. p_type == TK_OP_ASSIGN_BIT_XOR ||
  1895. p_type == TK_OP_BIT_AND ||
  1896. p_type == TK_OP_BIT_OR ||
  1897. p_type == TK_OP_BIT_XOR ||
  1898. p_type == TK_OP_BIT_INVERT ||
  1899. p_type == TK_OP_INCREMENT ||
  1900. p_type == TK_OP_DECREMENT ||
  1901. p_type == TK_QUESTION ||
  1902. p_type == TK_COLON);
  1903. }
  1904. bool ShaderLanguage::convert_constant(ConstantNode *p_constant, DataType p_to_type, ConstantNode::Value *p_value) {
  1905. if (p_constant->datatype == p_to_type) {
  1906. if (p_value) {
  1907. for (int i = 0; i < p_constant->values.size(); i++) {
  1908. p_value[i] = p_constant->values[i];
  1909. }
  1910. }
  1911. return true;
  1912. } else if (p_constant->datatype == TYPE_INT && p_to_type == TYPE_FLOAT) {
  1913. if (p_value) {
  1914. p_value->real = p_constant->values[0].sint;
  1915. }
  1916. return true;
  1917. } else if (p_constant->datatype == TYPE_UINT && p_to_type == TYPE_FLOAT) {
  1918. if (p_value) {
  1919. p_value->real = p_constant->values[0].uint;
  1920. }
  1921. return true;
  1922. } else if (p_constant->datatype == TYPE_INT && p_to_type == TYPE_UINT) {
  1923. if (p_constant->values[0].sint < 0) {
  1924. return false;
  1925. }
  1926. if (p_value) {
  1927. p_value->uint = p_constant->values[0].sint;
  1928. }
  1929. return true;
  1930. } else if (p_constant->datatype == TYPE_UINT && p_to_type == TYPE_INT) {
  1931. if (p_constant->values[0].uint > 0x7FFFFFFF) {
  1932. return false;
  1933. }
  1934. if (p_value) {
  1935. p_value->sint = p_constant->values[0].uint;
  1936. }
  1937. return true;
  1938. } else
  1939. return false;
  1940. }
  1941. bool ShaderLanguage::is_scalar_type(DataType p_type) {
  1942. return p_type == TYPE_BOOL || p_type == TYPE_INT || p_type == TYPE_UINT || p_type == TYPE_FLOAT;
  1943. }
  1944. bool ShaderLanguage::is_sampler_type(DataType p_type) {
  1945. return p_type == TYPE_SAMPLER2D ||
  1946. p_type == TYPE_ISAMPLER2D ||
  1947. p_type == TYPE_USAMPLER2D ||
  1948. p_type == TYPE_SAMPLER2DARRAY ||
  1949. p_type == TYPE_ISAMPLER2DARRAY ||
  1950. p_type == TYPE_USAMPLER2DARRAY ||
  1951. p_type == TYPE_SAMPLER3D ||
  1952. p_type == TYPE_ISAMPLER3D ||
  1953. p_type == TYPE_USAMPLER3D ||
  1954. p_type == TYPE_SAMPLERCUBE;
  1955. }
  1956. Variant ShaderLanguage::constant_value_to_variant(const Vector<ShaderLanguage::ConstantNode::Value> &p_value, DataType p_type) {
  1957. if (p_value.size() > 0) {
  1958. Variant value;
  1959. switch (p_type) {
  1960. case ShaderLanguage::TYPE_BOOL:
  1961. value = Variant(p_value[0].boolean);
  1962. break;
  1963. case ShaderLanguage::TYPE_BVEC2:
  1964. case ShaderLanguage::TYPE_BVEC3:
  1965. case ShaderLanguage::TYPE_BVEC4:
  1966. case ShaderLanguage::TYPE_INT:
  1967. value = Variant(p_value[0].sint);
  1968. break;
  1969. case ShaderLanguage::TYPE_IVEC2:
  1970. value = Variant(Vector2(p_value[0].sint, p_value[1].sint));
  1971. break;
  1972. case ShaderLanguage::TYPE_IVEC3:
  1973. value = Variant(Vector3(p_value[0].sint, p_value[1].sint, p_value[2].sint));
  1974. break;
  1975. case ShaderLanguage::TYPE_IVEC4:
  1976. value = Variant(Plane(p_value[0].sint, p_value[1].sint, p_value[2].sint, p_value[3].sint));
  1977. break;
  1978. case ShaderLanguage::TYPE_UINT:
  1979. value = Variant(p_value[0].uint);
  1980. break;
  1981. case ShaderLanguage::TYPE_UVEC2:
  1982. value = Variant(Vector2(p_value[0].uint, p_value[1].uint));
  1983. break;
  1984. case ShaderLanguage::TYPE_UVEC3:
  1985. value = Variant(Vector3(p_value[0].uint, p_value[1].uint, p_value[2].uint));
  1986. break;
  1987. case ShaderLanguage::TYPE_UVEC4:
  1988. value = Variant(Plane(p_value[0].uint, p_value[1].uint, p_value[2].uint, p_value[3].uint));
  1989. break;
  1990. case ShaderLanguage::TYPE_FLOAT:
  1991. value = Variant(p_value[0].real);
  1992. break;
  1993. case ShaderLanguage::TYPE_VEC2:
  1994. value = Variant(Vector2(p_value[0].real, p_value[1].real));
  1995. break;
  1996. case ShaderLanguage::TYPE_VEC3:
  1997. value = Variant(Vector3(p_value[0].real, p_value[1].real, p_value[2].real));
  1998. break;
  1999. case ShaderLanguage::TYPE_VEC4:
  2000. value = Variant(Plane(p_value[0].real, p_value[1].real, p_value[2].real, p_value[3].real));
  2001. break;
  2002. case ShaderLanguage::TYPE_MAT2:
  2003. value = Variant(Transform2D(p_value[0].real, p_value[2].real, p_value[1].real, p_value[3].real, 0.0, 0.0));
  2004. break;
  2005. case ShaderLanguage::TYPE_MAT3: {
  2006. Basis p;
  2007. p[0][0] = p_value[0].real;
  2008. p[0][1] = p_value[1].real;
  2009. p[0][2] = p_value[2].real;
  2010. p[1][0] = p_value[3].real;
  2011. p[1][1] = p_value[4].real;
  2012. p[1][2] = p_value[5].real;
  2013. p[2][0] = p_value[6].real;
  2014. p[2][1] = p_value[7].real;
  2015. p[2][2] = p_value[8].real;
  2016. value = Variant(p);
  2017. break;
  2018. }
  2019. case ShaderLanguage::TYPE_MAT4: {
  2020. Basis p;
  2021. p[0][0] = p_value[0].real;
  2022. p[0][1] = p_value[1].real;
  2023. p[0][2] = p_value[2].real;
  2024. p[1][0] = p_value[4].real;
  2025. p[1][1] = p_value[5].real;
  2026. p[1][2] = p_value[6].real;
  2027. p[2][0] = p_value[8].real;
  2028. p[2][1] = p_value[9].real;
  2029. p[2][2] = p_value[10].real;
  2030. Transform t = Transform(p, Vector3(p_value[3].real, p_value[7].real, p_value[11].real));
  2031. value = Variant(t);
  2032. break;
  2033. }
  2034. case ShaderLanguage::TYPE_ISAMPLER2DARRAY:
  2035. case ShaderLanguage::TYPE_ISAMPLER2D:
  2036. case ShaderLanguage::TYPE_ISAMPLER3D:
  2037. case ShaderLanguage::TYPE_SAMPLER2DARRAY:
  2038. case ShaderLanguage::TYPE_SAMPLER2D:
  2039. case ShaderLanguage::TYPE_SAMPLER3D:
  2040. case ShaderLanguage::TYPE_USAMPLER2DARRAY:
  2041. case ShaderLanguage::TYPE_USAMPLER2D:
  2042. case ShaderLanguage::TYPE_USAMPLER3D:
  2043. case ShaderLanguage::TYPE_SAMPLERCUBE: {
  2044. // Texture types, likely not relevant here.
  2045. break;
  2046. }
  2047. case ShaderLanguage::TYPE_VOID:
  2048. break;
  2049. }
  2050. return value;
  2051. }
  2052. return Variant();
  2053. }
  2054. void ShaderLanguage::get_keyword_list(List<String> *r_keywords) {
  2055. Set<String> kws;
  2056. int idx = 0;
  2057. while (keyword_list[idx].text) {
  2058. kws.insert(keyword_list[idx].text);
  2059. idx++;
  2060. }
  2061. idx = 0;
  2062. while (builtin_func_defs[idx].name) {
  2063. kws.insert(builtin_func_defs[idx].name);
  2064. idx++;
  2065. }
  2066. for (Set<String>::Element *E = kws.front(); E; E = E->next()) {
  2067. r_keywords->push_back(E->get());
  2068. }
  2069. }
  2070. void ShaderLanguage::get_builtin_funcs(List<String> *r_keywords) {
  2071. Set<String> kws;
  2072. int idx = 0;
  2073. while (builtin_func_defs[idx].name) {
  2074. kws.insert(builtin_func_defs[idx].name);
  2075. idx++;
  2076. }
  2077. for (Set<String>::Element *E = kws.front(); E; E = E->next()) {
  2078. r_keywords->push_back(E->get());
  2079. }
  2080. }
  2081. ShaderLanguage::DataType ShaderLanguage::get_scalar_type(DataType p_type) {
  2082. static const DataType scalar_types[] = {
  2083. TYPE_VOID,
  2084. TYPE_BOOL,
  2085. TYPE_BOOL,
  2086. TYPE_BOOL,
  2087. TYPE_BOOL,
  2088. TYPE_INT,
  2089. TYPE_INT,
  2090. TYPE_INT,
  2091. TYPE_INT,
  2092. TYPE_UINT,
  2093. TYPE_UINT,
  2094. TYPE_UINT,
  2095. TYPE_UINT,
  2096. TYPE_FLOAT,
  2097. TYPE_FLOAT,
  2098. TYPE_FLOAT,
  2099. TYPE_FLOAT,
  2100. TYPE_FLOAT,
  2101. TYPE_FLOAT,
  2102. TYPE_FLOAT,
  2103. TYPE_FLOAT,
  2104. TYPE_INT,
  2105. TYPE_UINT,
  2106. TYPE_FLOAT,
  2107. };
  2108. return scalar_types[p_type];
  2109. }
  2110. int ShaderLanguage::get_cardinality(DataType p_type) {
  2111. static const int cardinality_table[] = {
  2112. 0,
  2113. 1,
  2114. 2,
  2115. 3,
  2116. 4,
  2117. 1,
  2118. 2,
  2119. 3,
  2120. 4,
  2121. 1,
  2122. 2,
  2123. 3,
  2124. 4,
  2125. 1,
  2126. 2,
  2127. 3,
  2128. 4,
  2129. 4,
  2130. 9,
  2131. 16,
  2132. 1,
  2133. 1,
  2134. 1,
  2135. 1,
  2136. };
  2137. return cardinality_table[p_type];
  2138. }
  2139. bool ShaderLanguage::_get_completable_identifier(BlockNode *p_block, CompletionType p_type, StringName &identifier) {
  2140. identifier = StringName();
  2141. TkPos pos;
  2142. Token tk = _get_token();
  2143. if (tk.type == TK_IDENTIFIER) {
  2144. identifier = tk.text;
  2145. pos = _get_tkpos();
  2146. tk = _get_token();
  2147. }
  2148. if (tk.type == TK_CURSOR) {
  2149. completion_type = p_type;
  2150. completion_line = tk_line;
  2151. completion_block = p_block;
  2152. pos = _get_tkpos();
  2153. tk = _get_token();
  2154. if (tk.type == TK_IDENTIFIER) {
  2155. identifier = identifier.operator String() + tk.text.operator String();
  2156. } else {
  2157. _set_tkpos(pos);
  2158. }
  2159. return true;
  2160. } else if (identifier != StringName()) {
  2161. _set_tkpos(pos);
  2162. }
  2163. return false;
  2164. }
  2165. bool ShaderLanguage::_is_operator_assign(Operator p_op) const {
  2166. switch (p_op) {
  2167. case OP_ASSIGN:
  2168. case OP_ASSIGN_ADD:
  2169. case OP_ASSIGN_SUB:
  2170. case OP_ASSIGN_MUL:
  2171. case OP_ASSIGN_DIV:
  2172. case OP_ASSIGN_MOD:
  2173. case OP_ASSIGN_SHIFT_LEFT:
  2174. case OP_ASSIGN_SHIFT_RIGHT:
  2175. case OP_ASSIGN_BIT_AND:
  2176. case OP_ASSIGN_BIT_OR:
  2177. case OP_ASSIGN_BIT_XOR:
  2178. return true;
  2179. default:
  2180. return false;
  2181. }
  2182. return false;
  2183. }
  2184. bool ShaderLanguage::_validate_assign(Node *p_node, const Map<StringName, BuiltInInfo> &p_builtin_types, String *r_message) {
  2185. if (p_node->type == Node::TYPE_OPERATOR) {
  2186. OperatorNode *op = static_cast<OperatorNode *>(p_node);
  2187. if (op->op == OP_INDEX) {
  2188. return _validate_assign(op->arguments[0], p_builtin_types, r_message);
  2189. } else if (_is_operator_assign(op->op)) {
  2190. //chained assignment
  2191. return _validate_assign(op->arguments[1], p_builtin_types, r_message);
  2192. } else if (op->op == OP_CALL) {
  2193. if (r_message)
  2194. *r_message = RTR("Assignment to function.");
  2195. return false;
  2196. }
  2197. } else if (p_node->type == Node::TYPE_MEMBER) {
  2198. MemberNode *member = static_cast<MemberNode *>(p_node);
  2199. return _validate_assign(member->owner, p_builtin_types, r_message);
  2200. } else if (p_node->type == Node::TYPE_VARIABLE) {
  2201. VariableNode *var = static_cast<VariableNode *>(p_node);
  2202. if (shader->uniforms.has(var->name)) {
  2203. if (r_message)
  2204. *r_message = RTR("Assignment to uniform.");
  2205. return false;
  2206. }
  2207. if (shader->varyings.has(var->name) && current_function != String("vertex")) {
  2208. if (r_message)
  2209. *r_message = RTR("Varyings can only be assigned in vertex function.");
  2210. return false;
  2211. }
  2212. if (!(p_builtin_types.has(var->name) && p_builtin_types[var->name].constant)) {
  2213. return true;
  2214. }
  2215. }
  2216. if (r_message)
  2217. *r_message = "Assignment to constant expression.";
  2218. return false;
  2219. }
  2220. ShaderLanguage::Node *ShaderLanguage::_parse_expression(BlockNode *p_block, const Map<StringName, BuiltInInfo> &p_builtin_types) {
  2221. Vector<Expression> expression;
  2222. //Vector<TokenType> operators;
  2223. while (true) {
  2224. Node *expr = NULL;
  2225. TkPos prepos = _get_tkpos();
  2226. Token tk = _get_token();
  2227. TkPos pos = _get_tkpos();
  2228. if (tk.type == TK_PARENTHESIS_OPEN) {
  2229. //handle subexpression
  2230. expr = _parse_and_reduce_expression(p_block, p_builtin_types);
  2231. if (!expr)
  2232. return NULL;
  2233. tk = _get_token();
  2234. if (tk.type != TK_PARENTHESIS_CLOSE) {
  2235. _set_error("Expected ')' in expression");
  2236. return NULL;
  2237. }
  2238. } else if (tk.type == TK_REAL_CONSTANT) {
  2239. ConstantNode *constant = alloc_node<ConstantNode>();
  2240. ConstantNode::Value v;
  2241. v.real = tk.constant;
  2242. constant->values.push_back(v);
  2243. constant->datatype = TYPE_FLOAT;
  2244. expr = constant;
  2245. } else if (tk.type == TK_INT_CONSTANT) {
  2246. ConstantNode *constant = alloc_node<ConstantNode>();
  2247. ConstantNode::Value v;
  2248. v.sint = tk.constant;
  2249. constant->values.push_back(v);
  2250. constant->datatype = TYPE_INT;
  2251. expr = constant;
  2252. } else if (tk.type == TK_TRUE) {
  2253. //handle true constant
  2254. ConstantNode *constant = alloc_node<ConstantNode>();
  2255. ConstantNode::Value v;
  2256. v.boolean = true;
  2257. constant->values.push_back(v);
  2258. constant->datatype = TYPE_BOOL;
  2259. expr = constant;
  2260. } else if (tk.type == TK_FALSE) {
  2261. //handle false constant
  2262. ConstantNode *constant = alloc_node<ConstantNode>();
  2263. ConstantNode::Value v;
  2264. v.boolean = false;
  2265. constant->values.push_back(v);
  2266. constant->datatype = TYPE_BOOL;
  2267. expr = constant;
  2268. } else if (tk.type == TK_TYPE_VOID) {
  2269. //make sure void is not used in expression
  2270. _set_error("Void value not allowed in Expression");
  2271. return NULL;
  2272. } else if (is_token_nonvoid_datatype(tk.type)) {
  2273. //basic type constructor
  2274. OperatorNode *func = alloc_node<OperatorNode>();
  2275. func->op = OP_CONSTRUCT;
  2276. if (is_token_precision(tk.type)) {
  2277. func->return_precision_cache = get_token_precision(tk.type);
  2278. tk = _get_token();
  2279. }
  2280. VariableNode *funcname = alloc_node<VariableNode>();
  2281. funcname->name = get_datatype_name(get_token_datatype(tk.type));
  2282. func->arguments.push_back(funcname);
  2283. tk = _get_token();
  2284. if (tk.type != TK_PARENTHESIS_OPEN) {
  2285. _set_error("Expected '(' after type name");
  2286. return NULL;
  2287. }
  2288. int carg = -1;
  2289. bool ok = _parse_function_arguments(p_block, p_builtin_types, func, &carg);
  2290. if (carg >= 0) {
  2291. completion_type = COMPLETION_CALL_ARGUMENTS;
  2292. completion_line = tk_line;
  2293. completion_block = p_block;
  2294. completion_function = funcname->name;
  2295. completion_argument = carg;
  2296. }
  2297. if (!ok)
  2298. return NULL;
  2299. if (!_validate_function_call(p_block, func, &func->return_cache)) {
  2300. _set_error("No matching constructor found for: '" + String(funcname->name) + "'");
  2301. return NULL;
  2302. }
  2303. //validate_Function_call()
  2304. expr = _reduce_expression(p_block, func);
  2305. } else if (tk.type == TK_IDENTIFIER) {
  2306. _set_tkpos(prepos);
  2307. StringName identifier;
  2308. _get_completable_identifier(p_block, COMPLETION_IDENTIFIER, identifier);
  2309. tk = _get_token();
  2310. if (tk.type == TK_PARENTHESIS_OPEN) {
  2311. //a function
  2312. StringName name = identifier;
  2313. OperatorNode *func = alloc_node<OperatorNode>();
  2314. func->op = OP_CALL;
  2315. VariableNode *funcname = alloc_node<VariableNode>();
  2316. funcname->name = name;
  2317. func->arguments.push_back(funcname);
  2318. int carg = -1;
  2319. bool ok = _parse_function_arguments(p_block, p_builtin_types, func, &carg);
  2320. //test if function was parsed first
  2321. for (int i = 0; i < shader->functions.size(); i++) {
  2322. if (shader->functions[i].name == name) {
  2323. //add to current function as dependency
  2324. for (int j = 0; j < shader->functions.size(); j++) {
  2325. if (shader->functions[j].name == current_function) {
  2326. shader->functions.write[j].uses_function.insert(name);
  2327. break;
  2328. }
  2329. }
  2330. break;
  2331. }
  2332. }
  2333. if (carg >= 0) {
  2334. completion_type = COMPLETION_CALL_ARGUMENTS;
  2335. completion_line = tk_line;
  2336. completion_block = p_block;
  2337. completion_function = funcname->name;
  2338. completion_argument = carg;
  2339. }
  2340. if (!ok)
  2341. return NULL;
  2342. if (!_validate_function_call(p_block, func, &func->return_cache)) {
  2343. _set_error("No matching function found for: '" + String(funcname->name) + "'");
  2344. return NULL;
  2345. }
  2346. expr = func;
  2347. } else {
  2348. //an identifier
  2349. _set_tkpos(pos);
  2350. DataType data_type;
  2351. IdentifierType ident_type;
  2352. if (!_find_identifier(p_block, p_builtin_types, identifier, &data_type, &ident_type)) {
  2353. _set_error("Unknown identifier in expression: " + String(identifier));
  2354. return NULL;
  2355. }
  2356. if (ident_type == IDENTIFIER_FUNCTION) {
  2357. _set_error("Can't use function as identifier: " + String(identifier));
  2358. return NULL;
  2359. }
  2360. VariableNode *varname = alloc_node<VariableNode>();
  2361. varname->name = identifier;
  2362. varname->datatype_cache = data_type;
  2363. expr = varname;
  2364. }
  2365. } else if (tk.type == TK_OP_ADD) {
  2366. continue; //this one does nothing
  2367. } else if (tk.type == TK_OP_SUB || tk.type == TK_OP_NOT || tk.type == TK_OP_BIT_INVERT || tk.type == TK_OP_INCREMENT || tk.type == TK_OP_DECREMENT) {
  2368. Expression e;
  2369. e.is_op = true;
  2370. switch (tk.type) {
  2371. case TK_OP_SUB: e.op = OP_NEGATE; break;
  2372. case TK_OP_NOT: e.op = OP_NOT; break;
  2373. case TK_OP_BIT_INVERT: e.op = OP_BIT_INVERT; break;
  2374. case TK_OP_INCREMENT: e.op = OP_INCREMENT; break;
  2375. case TK_OP_DECREMENT: e.op = OP_DECREMENT; break;
  2376. default: ERR_FAIL_V(NULL);
  2377. }
  2378. expression.push_back(e);
  2379. continue;
  2380. } else {
  2381. _set_error("Expected expression, found: " + get_token_text(tk));
  2382. return NULL;
  2383. //nothing
  2384. }
  2385. ERR_FAIL_COND_V(!expr, NULL);
  2386. /* OK now see what's NEXT to the operator.. */
  2387. /* OK now see what's NEXT to the operator.. */
  2388. /* OK now see what's NEXT to the operator.. */
  2389. while (true) {
  2390. TkPos pos = _get_tkpos();
  2391. tk = _get_token();
  2392. if (tk.type == TK_CURSOR) {
  2393. //do nothing
  2394. } else if (tk.type == TK_PERIOD) {
  2395. StringName identifier;
  2396. if (_get_completable_identifier(p_block, COMPLETION_INDEX, identifier)) {
  2397. completion_base = expr->get_datatype();
  2398. }
  2399. if (identifier == StringName()) {
  2400. _set_error("Expected identifier as member");
  2401. return NULL;
  2402. }
  2403. DataType dt = expr->get_datatype();
  2404. String ident = identifier;
  2405. bool ok = true;
  2406. DataType member_type;
  2407. switch (dt) {
  2408. case TYPE_BVEC2:
  2409. case TYPE_IVEC2:
  2410. case TYPE_UVEC2:
  2411. case TYPE_VEC2: {
  2412. int l = ident.length();
  2413. if (l == 1) {
  2414. member_type = DataType(dt - 1);
  2415. } else if (l == 2) {
  2416. member_type = dt;
  2417. } else {
  2418. ok = false;
  2419. break;
  2420. }
  2421. const CharType *c = ident.ptr();
  2422. for (int i = 0; i < l; i++) {
  2423. switch (c[i]) {
  2424. case 'r':
  2425. case 'g':
  2426. case 'x':
  2427. case 'y':
  2428. break;
  2429. default:
  2430. ok = false;
  2431. break;
  2432. }
  2433. }
  2434. } break;
  2435. case TYPE_BVEC3:
  2436. case TYPE_IVEC3:
  2437. case TYPE_UVEC3:
  2438. case TYPE_VEC3: {
  2439. int l = ident.length();
  2440. if (l == 1) {
  2441. member_type = DataType(dt - 2);
  2442. } else if (l == 2) {
  2443. member_type = DataType(dt - 1);
  2444. } else if (l == 3) {
  2445. member_type = dt;
  2446. } else {
  2447. ok = false;
  2448. break;
  2449. }
  2450. const CharType *c = ident.ptr();
  2451. for (int i = 0; i < l; i++) {
  2452. switch (c[i]) {
  2453. case 'r':
  2454. case 'g':
  2455. case 'b':
  2456. case 'x':
  2457. case 'y':
  2458. case 'z':
  2459. break;
  2460. default:
  2461. ok = false;
  2462. break;
  2463. }
  2464. }
  2465. } break;
  2466. case TYPE_BVEC4:
  2467. case TYPE_IVEC4:
  2468. case TYPE_UVEC4:
  2469. case TYPE_VEC4: {
  2470. int l = ident.length();
  2471. if (l == 1) {
  2472. member_type = DataType(dt - 3);
  2473. } else if (l == 2) {
  2474. member_type = DataType(dt - 2);
  2475. } else if (l == 3) {
  2476. member_type = DataType(dt - 1);
  2477. } else if (l == 4) {
  2478. member_type = dt;
  2479. } else {
  2480. ok = false;
  2481. break;
  2482. }
  2483. const CharType *c = ident.ptr();
  2484. for (int i = 0; i < l; i++) {
  2485. switch (c[i]) {
  2486. case 'r':
  2487. case 'g':
  2488. case 'b':
  2489. case 'a':
  2490. case 'x':
  2491. case 'y':
  2492. case 'z':
  2493. case 'w':
  2494. break;
  2495. default:
  2496. ok = false;
  2497. break;
  2498. }
  2499. }
  2500. } break;
  2501. default: {
  2502. ok = false;
  2503. }
  2504. }
  2505. if (!ok) {
  2506. _set_error("Invalid member for " + get_datatype_name(dt) + " expression: ." + ident);
  2507. return NULL;
  2508. }
  2509. MemberNode *mn = alloc_node<MemberNode>();
  2510. mn->basetype = dt;
  2511. mn->datatype = member_type;
  2512. mn->name = ident;
  2513. mn->owner = expr;
  2514. expr = mn;
  2515. //todo
  2516. //member (period) has priority over any operator
  2517. //creates a subindexing expression in place
  2518. /*} else if (tk.type==TK_BRACKET_OPEN) {
  2519. //todo
  2520. //subindexing has priority over any operator
  2521. //creates a subindexing expression in place
  2522. */
  2523. } else if (tk.type == TK_BRACKET_OPEN) {
  2524. Node *index = _parse_and_reduce_expression(p_block, p_builtin_types);
  2525. if (!index)
  2526. return NULL;
  2527. if (index->get_datatype() != TYPE_INT && index->get_datatype() != TYPE_UINT) {
  2528. _set_error("Only integer datatypes are allowed for indexing");
  2529. return NULL;
  2530. }
  2531. DataType member_type = TYPE_VOID;
  2532. switch (expr->get_datatype()) {
  2533. case TYPE_BVEC2:
  2534. case TYPE_VEC2:
  2535. case TYPE_IVEC2:
  2536. case TYPE_UVEC2:
  2537. case TYPE_MAT2:
  2538. if (index->type == Node::TYPE_CONSTANT) {
  2539. uint32_t index_constant = static_cast<ConstantNode *>(index)->values[0].uint;
  2540. if (index_constant >= 2) {
  2541. _set_error("Index out of range (0-1)");
  2542. return NULL;
  2543. }
  2544. } else {
  2545. _set_error("Only integer constants are allowed as index at the moment");
  2546. return NULL;
  2547. }
  2548. switch (expr->get_datatype()) {
  2549. case TYPE_BVEC2: member_type = TYPE_BOOL; break;
  2550. case TYPE_VEC2: member_type = TYPE_FLOAT; break;
  2551. case TYPE_IVEC2: member_type = TYPE_INT; break;
  2552. case TYPE_UVEC2: member_type = TYPE_UINT; break;
  2553. case TYPE_MAT2: member_type = TYPE_VEC2; break;
  2554. default: break;
  2555. }
  2556. break;
  2557. case TYPE_BVEC3:
  2558. case TYPE_VEC3:
  2559. case TYPE_IVEC3:
  2560. case TYPE_UVEC3:
  2561. case TYPE_MAT3:
  2562. if (index->type == Node::TYPE_CONSTANT) {
  2563. uint32_t index_constant = static_cast<ConstantNode *>(index)->values[0].uint;
  2564. if (index_constant >= 3) {
  2565. _set_error("Index out of range (0-2)");
  2566. return NULL;
  2567. }
  2568. } else {
  2569. _set_error("Only integer constants are allowed as index at the moment");
  2570. return NULL;
  2571. }
  2572. switch (expr->get_datatype()) {
  2573. case TYPE_BVEC3: member_type = TYPE_BOOL; break;
  2574. case TYPE_VEC3: member_type = TYPE_FLOAT; break;
  2575. case TYPE_IVEC3: member_type = TYPE_INT; break;
  2576. case TYPE_UVEC3: member_type = TYPE_UINT; break;
  2577. case TYPE_MAT3: member_type = TYPE_VEC3; break;
  2578. default: break;
  2579. }
  2580. break;
  2581. case TYPE_BVEC4:
  2582. case TYPE_VEC4:
  2583. case TYPE_IVEC4:
  2584. case TYPE_UVEC4:
  2585. case TYPE_MAT4:
  2586. if (index->type == Node::TYPE_CONSTANT) {
  2587. uint32_t index_constant = static_cast<ConstantNode *>(index)->values[0].uint;
  2588. if (index_constant >= 4) {
  2589. _set_error("Index out of range (0-3)");
  2590. return NULL;
  2591. }
  2592. } else {
  2593. _set_error("Only integer constants are allowed as index at the moment");
  2594. return NULL;
  2595. }
  2596. switch (expr->get_datatype()) {
  2597. case TYPE_BVEC4: member_type = TYPE_BOOL; break;
  2598. case TYPE_VEC4: member_type = TYPE_FLOAT; break;
  2599. case TYPE_IVEC4: member_type = TYPE_INT; break;
  2600. case TYPE_UVEC4: member_type = TYPE_UINT; break;
  2601. case TYPE_MAT4: member_type = TYPE_VEC4; break;
  2602. default: break;
  2603. }
  2604. break;
  2605. default: {
  2606. _set_error("Object of type '" + get_datatype_name(expr->get_datatype()) + "' can't be indexed");
  2607. return NULL;
  2608. }
  2609. }
  2610. OperatorNode *op = alloc_node<OperatorNode>();
  2611. op->op = OP_INDEX;
  2612. op->return_cache = member_type;
  2613. op->arguments.push_back(expr);
  2614. op->arguments.push_back(index);
  2615. expr = op;
  2616. tk = _get_token();
  2617. if (tk.type != TK_BRACKET_CLOSE) {
  2618. _set_error("Expected ']' after indexing expression");
  2619. return NULL;
  2620. }
  2621. } else if (tk.type == TK_OP_INCREMENT || tk.type == TK_OP_DECREMENT) {
  2622. OperatorNode *op = alloc_node<OperatorNode>();
  2623. op->op = tk.type == TK_OP_DECREMENT ? OP_POST_DECREMENT : OP_POST_INCREMENT;
  2624. op->arguments.push_back(expr);
  2625. if (!_validate_operator(op, &op->return_cache)) {
  2626. _set_error("Invalid base type for increment/decrement operator");
  2627. return NULL;
  2628. }
  2629. if (!_validate_assign(expr, p_builtin_types)) {
  2630. _set_error("Invalid use of increment/decrement operator in constant expression.");
  2631. return NULL;
  2632. }
  2633. expr = op;
  2634. } else {
  2635. _set_tkpos(pos);
  2636. break;
  2637. }
  2638. }
  2639. Expression e;
  2640. e.is_op = false;
  2641. e.node = expr;
  2642. expression.push_back(e);
  2643. pos = _get_tkpos();
  2644. tk = _get_token();
  2645. if (is_token_operator(tk.type)) {
  2646. Expression o;
  2647. o.is_op = true;
  2648. switch (tk.type) {
  2649. case TK_OP_EQUAL: o.op = OP_EQUAL; break;
  2650. case TK_OP_NOT_EQUAL: o.op = OP_NOT_EQUAL; break;
  2651. case TK_OP_LESS: o.op = OP_LESS; break;
  2652. case TK_OP_LESS_EQUAL: o.op = OP_LESS_EQUAL; break;
  2653. case TK_OP_GREATER: o.op = OP_GREATER; break;
  2654. case TK_OP_GREATER_EQUAL: o.op = OP_GREATER_EQUAL; break;
  2655. case TK_OP_AND: o.op = OP_AND; break;
  2656. case TK_OP_OR: o.op = OP_OR; break;
  2657. case TK_OP_ADD: o.op = OP_ADD; break;
  2658. case TK_OP_SUB: o.op = OP_SUB; break;
  2659. case TK_OP_MUL: o.op = OP_MUL; break;
  2660. case TK_OP_DIV: o.op = OP_DIV; break;
  2661. case TK_OP_MOD: o.op = OP_MOD; break;
  2662. case TK_OP_SHIFT_LEFT: o.op = OP_SHIFT_LEFT; break;
  2663. case TK_OP_SHIFT_RIGHT: o.op = OP_SHIFT_RIGHT; break;
  2664. case TK_OP_ASSIGN: o.op = OP_ASSIGN; break;
  2665. case TK_OP_ASSIGN_ADD: o.op = OP_ASSIGN_ADD; break;
  2666. case TK_OP_ASSIGN_SUB: o.op = OP_ASSIGN_SUB; break;
  2667. case TK_OP_ASSIGN_MUL: o.op = OP_ASSIGN_MUL; break;
  2668. case TK_OP_ASSIGN_DIV: o.op = OP_ASSIGN_DIV; break;
  2669. case TK_OP_ASSIGN_MOD: o.op = OP_ASSIGN_MOD; break;
  2670. case TK_OP_ASSIGN_SHIFT_LEFT: o.op = OP_ASSIGN_SHIFT_LEFT; break;
  2671. case TK_OP_ASSIGN_SHIFT_RIGHT: o.op = OP_ASSIGN_SHIFT_RIGHT; break;
  2672. case TK_OP_ASSIGN_BIT_AND: o.op = OP_ASSIGN_BIT_AND; break;
  2673. case TK_OP_ASSIGN_BIT_OR: o.op = OP_ASSIGN_BIT_OR; break;
  2674. case TK_OP_ASSIGN_BIT_XOR: o.op = OP_ASSIGN_BIT_XOR; break;
  2675. case TK_OP_BIT_AND: o.op = OP_BIT_AND; break;
  2676. case TK_OP_BIT_OR: o.op = OP_BIT_OR; break;
  2677. case TK_OP_BIT_XOR: o.op = OP_BIT_XOR; break;
  2678. case TK_QUESTION: o.op = OP_SELECT_IF; break;
  2679. case TK_COLON: o.op = OP_SELECT_ELSE; break;
  2680. default: {
  2681. _set_error("Invalid token for operator: " + get_token_text(tk));
  2682. return NULL;
  2683. }
  2684. }
  2685. expression.push_back(o);
  2686. } else {
  2687. _set_tkpos(pos); //something else, so rollback and end
  2688. break;
  2689. }
  2690. }
  2691. /* Reduce the set set of expressions and place them in an operator tree, respecting precedence */
  2692. while (expression.size() > 1) {
  2693. int next_op = -1;
  2694. int min_priority = 0xFFFFF;
  2695. bool is_unary = false;
  2696. bool is_ternary = false;
  2697. for (int i = 0; i < expression.size(); i++) {
  2698. if (!expression[i].is_op) {
  2699. continue;
  2700. }
  2701. bool unary = false;
  2702. bool ternary = false;
  2703. int priority;
  2704. switch (expression[i].op) {
  2705. case OP_EQUAL: priority = 8; break;
  2706. case OP_NOT_EQUAL: priority = 8; break;
  2707. case OP_LESS: priority = 7; break;
  2708. case OP_LESS_EQUAL: priority = 7; break;
  2709. case OP_GREATER: priority = 7; break;
  2710. case OP_GREATER_EQUAL: priority = 7; break;
  2711. case OP_AND: priority = 12; break;
  2712. case OP_OR: priority = 14; break;
  2713. case OP_NOT:
  2714. priority = 3;
  2715. unary = true;
  2716. break;
  2717. case OP_NEGATE:
  2718. priority = 3;
  2719. unary = true;
  2720. break;
  2721. case OP_ADD: priority = 5; break;
  2722. case OP_SUB: priority = 5; break;
  2723. case OP_MUL: priority = 4; break;
  2724. case OP_DIV: priority = 4; break;
  2725. case OP_MOD: priority = 4; break;
  2726. case OP_SHIFT_LEFT: priority = 6; break;
  2727. case OP_SHIFT_RIGHT: priority = 6; break;
  2728. case OP_ASSIGN: priority = 16; break;
  2729. case OP_ASSIGN_ADD: priority = 16; break;
  2730. case OP_ASSIGN_SUB: priority = 16; break;
  2731. case OP_ASSIGN_MUL: priority = 16; break;
  2732. case OP_ASSIGN_DIV: priority = 16; break;
  2733. case OP_ASSIGN_MOD: priority = 16; break;
  2734. case OP_ASSIGN_SHIFT_LEFT: priority = 16; break;
  2735. case OP_ASSIGN_SHIFT_RIGHT: priority = 16; break;
  2736. case OP_ASSIGN_BIT_AND: priority = 16; break;
  2737. case OP_ASSIGN_BIT_OR: priority = 16; break;
  2738. case OP_ASSIGN_BIT_XOR: priority = 16; break;
  2739. case OP_BIT_AND: priority = 9; break;
  2740. case OP_BIT_OR: priority = 11; break;
  2741. case OP_BIT_XOR: priority = 10; break;
  2742. case OP_BIT_INVERT:
  2743. priority = 3;
  2744. unary = true;
  2745. break;
  2746. case OP_INCREMENT:
  2747. priority = 3;
  2748. unary = true;
  2749. break;
  2750. case OP_DECREMENT:
  2751. priority = 3;
  2752. unary = true;
  2753. break;
  2754. case OP_SELECT_IF:
  2755. priority = 15;
  2756. ternary = true;
  2757. break;
  2758. case OP_SELECT_ELSE:
  2759. priority = 15;
  2760. ternary = true;
  2761. break;
  2762. default:
  2763. ERR_FAIL_V(NULL); //unexpected operator
  2764. }
  2765. if (priority < min_priority) {
  2766. // < is used for left to right (default)
  2767. // <= is used for right to left
  2768. next_op = i;
  2769. min_priority = priority;
  2770. is_unary = unary;
  2771. is_ternary = ternary;
  2772. }
  2773. }
  2774. ERR_FAIL_COND_V(next_op == -1, NULL);
  2775. // OK! create operator..
  2776. // OK! create operator..
  2777. if (is_unary) {
  2778. int expr_pos = next_op;
  2779. while (expression[expr_pos].is_op) {
  2780. expr_pos++;
  2781. if (expr_pos == expression.size()) {
  2782. //can happen..
  2783. _set_error("Unexpected end of expression...");
  2784. return NULL;
  2785. }
  2786. }
  2787. //consecutively do unary opeators
  2788. for (int i = expr_pos - 1; i >= next_op; i--) {
  2789. OperatorNode *op = alloc_node<OperatorNode>();
  2790. op->op = expression[i].op;
  2791. if ((op->op == OP_INCREMENT || op->op == OP_DECREMENT) && !_validate_assign(expression[i + 1].node, p_builtin_types)) {
  2792. _set_error("Can't use increment/decrement operator in constant expression.");
  2793. return NULL;
  2794. }
  2795. op->arguments.push_back(expression[i + 1].node);
  2796. expression.write[i].is_op = false;
  2797. expression.write[i].node = op;
  2798. if (!_validate_operator(op, &op->return_cache)) {
  2799. String at;
  2800. for (int i = 0; i < op->arguments.size(); i++) {
  2801. if (i > 0)
  2802. at += " and ";
  2803. at += get_datatype_name(op->arguments[i]->get_datatype());
  2804. }
  2805. _set_error("Invalid arguments to unary operator '" + get_operator_text(op->op) + "' :" + at);
  2806. return NULL;
  2807. }
  2808. expression.remove(i + 1);
  2809. }
  2810. } else if (is_ternary) {
  2811. if (next_op < 1 || next_op >= (expression.size() - 1)) {
  2812. _set_error("Parser bug...");
  2813. ERR_FAIL_V(NULL);
  2814. }
  2815. if (next_op + 2 >= expression.size() || !expression[next_op + 2].is_op || expression[next_op + 2].op != OP_SELECT_ELSE) {
  2816. _set_error("Missing matching ':' for select operator");
  2817. return NULL;
  2818. }
  2819. OperatorNode *op = alloc_node<OperatorNode>();
  2820. op->op = expression[next_op].op;
  2821. op->arguments.push_back(expression[next_op - 1].node);
  2822. op->arguments.push_back(expression[next_op + 1].node);
  2823. op->arguments.push_back(expression[next_op + 3].node);
  2824. expression.write[next_op - 1].is_op = false;
  2825. expression.write[next_op - 1].node = op;
  2826. if (!_validate_operator(op, &op->return_cache)) {
  2827. String at;
  2828. for (int i = 0; i < op->arguments.size(); i++) {
  2829. if (i > 0)
  2830. at += " and ";
  2831. at += get_datatype_name(op->arguments[i]->get_datatype());
  2832. }
  2833. _set_error("Invalid argument to ternary ?: operator: " + at);
  2834. return NULL;
  2835. }
  2836. for (int i = 0; i < 4; i++) {
  2837. expression.remove(next_op);
  2838. }
  2839. } else {
  2840. if (next_op < 1 || next_op >= (expression.size() - 1)) {
  2841. _set_error("Parser bug...");
  2842. ERR_FAIL_V(NULL);
  2843. }
  2844. OperatorNode *op = alloc_node<OperatorNode>();
  2845. op->op = expression[next_op].op;
  2846. if (expression[next_op - 1].is_op) {
  2847. _set_error("Parser bug...");
  2848. ERR_FAIL_V(NULL);
  2849. }
  2850. if (_is_operator_assign(op->op)) {
  2851. String assign_message;
  2852. if (!_validate_assign(expression[next_op - 1].node, p_builtin_types, &assign_message)) {
  2853. _set_error(assign_message);
  2854. return NULL;
  2855. }
  2856. }
  2857. if (expression[next_op + 1].is_op) {
  2858. // this is not invalid and can really appear
  2859. // but it becomes invalid anyway because no binary op
  2860. // can be followed by a unary op in a valid combination,
  2861. // due to how precedence works, unaries will always disappear first
  2862. _set_error("Parser bug...");
  2863. }
  2864. op->arguments.push_back(expression[next_op - 1].node); //expression goes as left
  2865. op->arguments.push_back(expression[next_op + 1].node); //next expression goes as right
  2866. expression.write[next_op - 1].node = op;
  2867. //replace all 3 nodes by this operator and make it an expression
  2868. if (!_validate_operator(op, &op->return_cache)) {
  2869. String at;
  2870. for (int i = 0; i < op->arguments.size(); i++) {
  2871. if (i > 0)
  2872. at += " and ";
  2873. at += get_datatype_name(op->arguments[i]->get_datatype());
  2874. }
  2875. _set_error("Invalid arguments to operator '" + get_operator_text(op->op) + "' :" + at);
  2876. return NULL;
  2877. }
  2878. expression.remove(next_op);
  2879. expression.remove(next_op);
  2880. }
  2881. }
  2882. return expression[0].node;
  2883. }
  2884. ShaderLanguage::Node *ShaderLanguage::_reduce_expression(BlockNode *p_block, ShaderLanguage::Node *p_node) {
  2885. if (p_node->type != Node::TYPE_OPERATOR)
  2886. return p_node;
  2887. //for now only reduce simple constructors
  2888. OperatorNode *op = static_cast<OperatorNode *>(p_node);
  2889. if (op->op == OP_CONSTRUCT) {
  2890. ERR_FAIL_COND_V(op->arguments[0]->type != Node::TYPE_VARIABLE, p_node);
  2891. DataType type = op->get_datatype();
  2892. DataType base = get_scalar_type(type);
  2893. int cardinality = get_cardinality(type);
  2894. Vector<ConstantNode::Value> values;
  2895. for (int i = 1; i < op->arguments.size(); i++) {
  2896. op->arguments.write[i] = _reduce_expression(p_block, op->arguments[i]);
  2897. if (op->arguments[i]->type == Node::TYPE_CONSTANT) {
  2898. ConstantNode *cn = static_cast<ConstantNode *>(op->arguments[i]);
  2899. if (get_scalar_type(cn->datatype) == base) {
  2900. for (int j = 0; j < cn->values.size(); j++) {
  2901. values.push_back(cn->values[j]);
  2902. }
  2903. } else if (get_scalar_type(cn->datatype) == cn->datatype) {
  2904. ConstantNode::Value v;
  2905. if (!convert_constant(cn, base, &v)) {
  2906. return p_node;
  2907. }
  2908. values.push_back(v);
  2909. } else {
  2910. return p_node;
  2911. }
  2912. } else {
  2913. return p_node;
  2914. }
  2915. }
  2916. if (values.size() == 1) {
  2917. if (type >= TYPE_MAT2 && type <= TYPE_MAT4) {
  2918. ConstantNode::Value value = values[0];
  2919. ConstantNode::Value zero;
  2920. zero.real = 0.0f;
  2921. int size = 2 + (type - TYPE_MAT2);
  2922. values.clear();
  2923. for (int i = 0; i < size; i++) {
  2924. for (int j = 0; j < size; j++) {
  2925. values.push_back(i == j ? value : zero);
  2926. }
  2927. }
  2928. } else {
  2929. ConstantNode::Value value = values[0];
  2930. for (int i = 1; i < cardinality; i++) {
  2931. values.push_back(value);
  2932. }
  2933. }
  2934. } else if (values.size() != cardinality) {
  2935. ERR_PRINT("Failed to reduce expression, values and cardinality mismatch.");
  2936. return p_node;
  2937. }
  2938. ConstantNode *cn = alloc_node<ConstantNode>();
  2939. cn->datatype = op->get_datatype();
  2940. cn->values = values;
  2941. return cn;
  2942. } else if (op->op == OP_NEGATE) {
  2943. op->arguments.write[0] = _reduce_expression(p_block, op->arguments[0]);
  2944. if (op->arguments[0]->type == Node::TYPE_CONSTANT) {
  2945. ConstantNode *cn = static_cast<ConstantNode *>(op->arguments[0]);
  2946. DataType base = get_scalar_type(cn->datatype);
  2947. Vector<ConstantNode::Value> values;
  2948. for (int i = 0; i < cn->values.size(); i++) {
  2949. ConstantNode::Value nv;
  2950. switch (base) {
  2951. case TYPE_BOOL: {
  2952. nv.boolean = !cn->values[i].boolean;
  2953. } break;
  2954. case TYPE_INT: {
  2955. nv.sint = -cn->values[i].sint;
  2956. } break;
  2957. case TYPE_UINT: {
  2958. nv.uint = -cn->values[i].uint;
  2959. } break;
  2960. case TYPE_FLOAT: {
  2961. nv.real = -cn->values[i].real;
  2962. } break;
  2963. default: {}
  2964. }
  2965. values.push_back(nv);
  2966. }
  2967. cn->values = values;
  2968. return cn;
  2969. }
  2970. }
  2971. return p_node;
  2972. }
  2973. ShaderLanguage::Node *ShaderLanguage::_parse_and_reduce_expression(BlockNode *p_block, const Map<StringName, BuiltInInfo> &p_builtin_types) {
  2974. ShaderLanguage::Node *expr = _parse_expression(p_block, p_builtin_types);
  2975. if (!expr) //errored
  2976. return NULL;
  2977. expr = _reduce_expression(p_block, expr);
  2978. return expr;
  2979. }
  2980. Error ShaderLanguage::_parse_block(BlockNode *p_block, const Map<StringName, BuiltInInfo> &p_builtin_types, bool p_just_one, bool p_can_break, bool p_can_continue) {
  2981. while (true) {
  2982. TkPos pos = _get_tkpos();
  2983. Token tk = _get_token();
  2984. if (tk.type == TK_CURLY_BRACKET_CLOSE) { //end of block
  2985. if (p_just_one) {
  2986. _set_error("Unexpected '}'");
  2987. return ERR_PARSE_ERROR;
  2988. }
  2989. return OK;
  2990. } else if (is_token_precision(tk.type) || is_token_nonvoid_datatype(tk.type)) {
  2991. DataPrecision precision = PRECISION_DEFAULT;
  2992. if (is_token_precision(tk.type)) {
  2993. precision = get_token_precision(tk.type);
  2994. tk = _get_token();
  2995. if (!is_token_nonvoid_datatype(tk.type)) {
  2996. _set_error("Expected datatype after precision");
  2997. return ERR_PARSE_ERROR;
  2998. }
  2999. }
  3000. DataType type = get_token_datatype(tk.type);
  3001. tk = _get_token();
  3002. VariableDeclarationNode *vardecl = alloc_node<VariableDeclarationNode>();
  3003. vardecl->datatype = type;
  3004. vardecl->precision = precision;
  3005. p_block->statements.push_back(vardecl);
  3006. while (true) {
  3007. if (tk.type != TK_IDENTIFIER) {
  3008. _set_error("Expected identifier after type");
  3009. return ERR_PARSE_ERROR;
  3010. }
  3011. StringName name = tk.text;
  3012. if (_find_identifier(p_block, p_builtin_types, name)) {
  3013. _set_error("Redefinition of '" + String(name) + "'");
  3014. return ERR_PARSE_ERROR;
  3015. }
  3016. BlockNode::Variable var;
  3017. var.type = type;
  3018. var.precision = precision;
  3019. var.line = tk_line;
  3020. VariableDeclarationNode::Declaration decl;
  3021. decl.name = name;
  3022. decl.initializer = NULL;
  3023. tk = _get_token();
  3024. if (tk.type == TK_OP_ASSIGN) {
  3025. //variable created with assignment! must parse an expression
  3026. Node *n = _parse_and_reduce_expression(p_block, p_builtin_types);
  3027. if (!n)
  3028. return ERR_PARSE_ERROR;
  3029. decl.initializer = n;
  3030. if (var.type != n->get_datatype()) {
  3031. _set_error("Invalid assignment of '" + get_datatype_name(n->get_datatype()) + "' to '" + get_datatype_name(var.type) + "'");
  3032. return ERR_PARSE_ERROR;
  3033. }
  3034. tk = _get_token();
  3035. }
  3036. p_block->variables[name] = var;
  3037. vardecl->declarations.push_back(decl);
  3038. if (tk.type == TK_COMMA) {
  3039. tk = _get_token();
  3040. //another variable
  3041. } else if (tk.type == TK_SEMICOLON) {
  3042. break;
  3043. } else {
  3044. _set_error("Expected ',' or ';' after variable");
  3045. return ERR_PARSE_ERROR;
  3046. }
  3047. }
  3048. } else if (tk.type == TK_CURLY_BRACKET_OPEN) {
  3049. //a sub block, just because..
  3050. BlockNode *block = alloc_node<BlockNode>();
  3051. block->parent_block = p_block;
  3052. _parse_block(block, p_builtin_types, false, p_can_break, p_can_continue);
  3053. p_block->statements.push_back(block);
  3054. } else if (tk.type == TK_CF_IF) {
  3055. //if () {}
  3056. tk = _get_token();
  3057. if (tk.type != TK_PARENTHESIS_OPEN) {
  3058. _set_error("Expected '(' after if");
  3059. return ERR_PARSE_ERROR;
  3060. }
  3061. ControlFlowNode *cf = alloc_node<ControlFlowNode>();
  3062. cf->flow_op = FLOW_OP_IF;
  3063. Node *n = _parse_and_reduce_expression(p_block, p_builtin_types);
  3064. if (!n)
  3065. return ERR_PARSE_ERROR;
  3066. tk = _get_token();
  3067. if (tk.type != TK_PARENTHESIS_CLOSE) {
  3068. _set_error("Expected ')' after expression");
  3069. return ERR_PARSE_ERROR;
  3070. }
  3071. BlockNode *block = alloc_node<BlockNode>();
  3072. block->parent_block = p_block;
  3073. cf->expressions.push_back(n);
  3074. cf->blocks.push_back(block);
  3075. p_block->statements.push_back(cf);
  3076. Error err = _parse_block(block, p_builtin_types, true, p_can_break, p_can_continue);
  3077. if (err)
  3078. return err;
  3079. pos = _get_tkpos();
  3080. tk = _get_token();
  3081. if (tk.type == TK_CF_ELSE) {
  3082. block = alloc_node<BlockNode>();
  3083. block->parent_block = p_block;
  3084. cf->blocks.push_back(block);
  3085. err = _parse_block(block, p_builtin_types, true, p_can_break, p_can_continue);
  3086. } else {
  3087. _set_tkpos(pos); //rollback
  3088. }
  3089. } else if (tk.type == TK_CF_WHILE) {
  3090. //if () {}
  3091. tk = _get_token();
  3092. if (tk.type != TK_PARENTHESIS_OPEN) {
  3093. _set_error("Expected '(' after while");
  3094. return ERR_PARSE_ERROR;
  3095. }
  3096. ControlFlowNode *cf = alloc_node<ControlFlowNode>();
  3097. cf->flow_op = FLOW_OP_WHILE;
  3098. Node *n = _parse_and_reduce_expression(p_block, p_builtin_types);
  3099. if (!n)
  3100. return ERR_PARSE_ERROR;
  3101. tk = _get_token();
  3102. if (tk.type != TK_PARENTHESIS_CLOSE) {
  3103. _set_error("Expected ')' after expression");
  3104. return ERR_PARSE_ERROR;
  3105. }
  3106. BlockNode *block = alloc_node<BlockNode>();
  3107. block->parent_block = p_block;
  3108. cf->expressions.push_back(n);
  3109. cf->blocks.push_back(block);
  3110. p_block->statements.push_back(cf);
  3111. Error err = _parse_block(block, p_builtin_types, true, true, true);
  3112. if (err)
  3113. return err;
  3114. } else if (tk.type == TK_CF_FOR) {
  3115. //if () {}
  3116. tk = _get_token();
  3117. if (tk.type != TK_PARENTHESIS_OPEN) {
  3118. _set_error("Expected '(' after for");
  3119. return ERR_PARSE_ERROR;
  3120. }
  3121. ControlFlowNode *cf = alloc_node<ControlFlowNode>();
  3122. cf->flow_op = FLOW_OP_FOR;
  3123. BlockNode *init_block = alloc_node<BlockNode>();
  3124. init_block->parent_block = p_block;
  3125. init_block->single_statement = true;
  3126. cf->blocks.push_back(init_block);
  3127. if (_parse_block(init_block, p_builtin_types, true, false, false) != OK) {
  3128. return ERR_PARSE_ERROR;
  3129. }
  3130. Node *n = _parse_and_reduce_expression(init_block, p_builtin_types);
  3131. if (!n)
  3132. return ERR_PARSE_ERROR;
  3133. if (n->get_datatype() != TYPE_BOOL) {
  3134. _set_error("Middle expression is expected to be boolean.");
  3135. return ERR_PARSE_ERROR;
  3136. }
  3137. tk = _get_token();
  3138. if (tk.type != TK_SEMICOLON) {
  3139. _set_error("Expected ';' after middle expression");
  3140. return ERR_PARSE_ERROR;
  3141. }
  3142. cf->expressions.push_back(n);
  3143. n = _parse_and_reduce_expression(init_block, p_builtin_types);
  3144. if (!n)
  3145. return ERR_PARSE_ERROR;
  3146. cf->expressions.push_back(n);
  3147. tk = _get_token();
  3148. if (tk.type != TK_PARENTHESIS_CLOSE) {
  3149. _set_error("Expected ')' after third expression");
  3150. return ERR_PARSE_ERROR;
  3151. }
  3152. BlockNode *block = alloc_node<BlockNode>();
  3153. block->parent_block = init_block;
  3154. cf->blocks.push_back(block);
  3155. p_block->statements.push_back(cf);
  3156. Error err = _parse_block(block, p_builtin_types, true, true, true);
  3157. if (err)
  3158. return err;
  3159. } else if (tk.type == TK_CF_RETURN) {
  3160. //check return type
  3161. BlockNode *b = p_block;
  3162. while (b && !b->parent_function) {
  3163. b = b->parent_block;
  3164. }
  3165. if (!b) {
  3166. _set_error("Bug");
  3167. return ERR_BUG;
  3168. }
  3169. ControlFlowNode *flow = alloc_node<ControlFlowNode>();
  3170. flow->flow_op = FLOW_OP_RETURN;
  3171. pos = _get_tkpos();
  3172. tk = _get_token();
  3173. if (tk.type == TK_SEMICOLON) {
  3174. //all is good
  3175. if (b->parent_function->return_type != TYPE_VOID) {
  3176. _set_error("Expected return with expression of type '" + get_datatype_name(b->parent_function->return_type) + "'");
  3177. return ERR_PARSE_ERROR;
  3178. }
  3179. } else {
  3180. _set_tkpos(pos); //rollback, wants expression
  3181. Node *expr = _parse_and_reduce_expression(p_block, p_builtin_types);
  3182. if (!expr)
  3183. return ERR_PARSE_ERROR;
  3184. if (b->parent_function->return_type != expr->get_datatype()) {
  3185. _set_error("Expected return expression of type '" + get_datatype_name(b->parent_function->return_type) + "'");
  3186. return ERR_PARSE_ERROR;
  3187. }
  3188. tk = _get_token();
  3189. if (tk.type != TK_SEMICOLON) {
  3190. _set_error("Expected ';' after return expression");
  3191. return ERR_PARSE_ERROR;
  3192. }
  3193. flow->expressions.push_back(expr);
  3194. }
  3195. p_block->statements.push_back(flow);
  3196. } else if (tk.type == TK_CF_DISCARD) {
  3197. //check return type
  3198. BlockNode *b = p_block;
  3199. while (b && !b->parent_function) {
  3200. b = b->parent_block;
  3201. }
  3202. if (!b) {
  3203. _set_error("Bug");
  3204. return ERR_BUG;
  3205. }
  3206. if (!b->parent_function->can_discard) {
  3207. _set_error("Use of 'discard' is not allowed here.");
  3208. return ERR_PARSE_ERROR;
  3209. }
  3210. ControlFlowNode *flow = alloc_node<ControlFlowNode>();
  3211. flow->flow_op = FLOW_OP_DISCARD;
  3212. pos = _get_tkpos();
  3213. tk = _get_token();
  3214. if (tk.type != TK_SEMICOLON) {
  3215. //all is good
  3216. _set_error("Expected ';' after discard");
  3217. }
  3218. p_block->statements.push_back(flow);
  3219. } else if (tk.type == TK_CF_BREAK) {
  3220. if (!p_can_break) {
  3221. //all is good
  3222. _set_error("Breaking is not allowed here");
  3223. }
  3224. ControlFlowNode *flow = alloc_node<ControlFlowNode>();
  3225. flow->flow_op = FLOW_OP_BREAK;
  3226. pos = _get_tkpos();
  3227. tk = _get_token();
  3228. if (tk.type != TK_SEMICOLON) {
  3229. //all is good
  3230. _set_error("Expected ';' after break");
  3231. }
  3232. p_block->statements.push_back(flow);
  3233. } else if (tk.type == TK_CF_CONTINUE) {
  3234. if (!p_can_break) {
  3235. //all is good
  3236. _set_error("Continuing is not allowed here");
  3237. }
  3238. ControlFlowNode *flow = alloc_node<ControlFlowNode>();
  3239. flow->flow_op = FLOW_OP_CONTINUE;
  3240. pos = _get_tkpos();
  3241. tk = _get_token();
  3242. if (tk.type != TK_SEMICOLON) {
  3243. //all is good
  3244. _set_error("Expected ';' after continue");
  3245. }
  3246. p_block->statements.push_back(flow);
  3247. } else {
  3248. //nothng else, so expression
  3249. _set_tkpos(pos); //rollback
  3250. Node *expr = _parse_and_reduce_expression(p_block, p_builtin_types);
  3251. if (!expr)
  3252. return ERR_PARSE_ERROR;
  3253. p_block->statements.push_back(expr);
  3254. tk = _get_token();
  3255. if (tk.type != TK_SEMICOLON) {
  3256. _set_error("Expected ';' after statement");
  3257. return ERR_PARSE_ERROR;
  3258. }
  3259. }
  3260. if (p_just_one)
  3261. break;
  3262. }
  3263. return OK;
  3264. }
  3265. Error ShaderLanguage::_parse_shader(const Map<StringName, FunctionInfo> &p_functions, const Vector<StringName> &p_render_modes, const Set<String> &p_shader_types) {
  3266. Token tk = _get_token();
  3267. if (tk.type != TK_SHADER_TYPE) {
  3268. _set_error("Expected 'shader_type' at the beginning of shader.");
  3269. return ERR_PARSE_ERROR;
  3270. }
  3271. tk = _get_token();
  3272. if (tk.type != TK_IDENTIFIER) {
  3273. _set_error("Expected identifier after 'shader_type', indicating type of shader.");
  3274. return ERR_PARSE_ERROR;
  3275. }
  3276. String shader_type_identifier;
  3277. shader_type_identifier = tk.text;
  3278. if (!p_shader_types.has(shader_type_identifier)) {
  3279. String valid;
  3280. for (Set<String>::Element *E = p_shader_types.front(); E; E = E->next()) {
  3281. if (valid != String()) {
  3282. valid += ", ";
  3283. }
  3284. valid += "'" + E->get() + "'";
  3285. }
  3286. _set_error("Invalid shader type, valid types are: " + valid);
  3287. return ERR_PARSE_ERROR;
  3288. }
  3289. tk = _get_token();
  3290. if (tk.type != TK_SEMICOLON) {
  3291. _set_error("Expected ';' after 'shader_type <type>'.");
  3292. }
  3293. tk = _get_token();
  3294. int texture_uniforms = 0;
  3295. int uniforms = 0;
  3296. while (tk.type != TK_EOF) {
  3297. switch (tk.type) {
  3298. case TK_RENDER_MODE: {
  3299. while (true) {
  3300. StringName mode;
  3301. _get_completable_identifier(NULL, COMPLETION_RENDER_MODE, mode);
  3302. if (mode == StringName()) {
  3303. _set_error("Expected identifier for render mode");
  3304. return ERR_PARSE_ERROR;
  3305. }
  3306. if (p_render_modes.find(mode) == -1) {
  3307. _set_error("Invalid render mode: '" + String(mode) + "'");
  3308. return ERR_PARSE_ERROR;
  3309. }
  3310. if (shader->render_modes.find(mode) != -1) {
  3311. _set_error("Duplicate render mode: '" + String(mode) + "'");
  3312. return ERR_PARSE_ERROR;
  3313. }
  3314. shader->render_modes.push_back(mode);
  3315. tk = _get_token();
  3316. if (tk.type == TK_COMMA) {
  3317. //all good, do nothing
  3318. } else if (tk.type == TK_SEMICOLON) {
  3319. break; //done
  3320. } else {
  3321. _set_error("Unexpected token: " + get_token_text(tk));
  3322. return ERR_PARSE_ERROR;
  3323. }
  3324. }
  3325. } break;
  3326. case TK_UNIFORM:
  3327. case TK_VARYING: {
  3328. bool uniform = tk.type == TK_UNIFORM;
  3329. DataPrecision precision = PRECISION_DEFAULT;
  3330. DataInterpolation interpolation = INTERPOLATION_SMOOTH;
  3331. DataType type;
  3332. StringName name;
  3333. tk = _get_token();
  3334. if (is_token_interpolation(tk.type)) {
  3335. interpolation = get_token_interpolation(tk.type);
  3336. tk = _get_token();
  3337. }
  3338. if (is_token_precision(tk.type)) {
  3339. precision = get_token_precision(tk.type);
  3340. tk = _get_token();
  3341. }
  3342. if (!is_token_datatype(tk.type)) {
  3343. _set_error("Expected datatype. ");
  3344. return ERR_PARSE_ERROR;
  3345. }
  3346. type = get_token_datatype(tk.type);
  3347. if (type == TYPE_VOID) {
  3348. _set_error("void datatype not allowed here");
  3349. return ERR_PARSE_ERROR;
  3350. }
  3351. if (!uniform && (type < TYPE_FLOAT || type > TYPE_MAT4)) {
  3352. _set_error("Invalid type for varying, only float,vec2,vec3,vec4,mat2,mat3,mat4 allowed.");
  3353. return ERR_PARSE_ERROR;
  3354. }
  3355. tk = _get_token();
  3356. if (tk.type != TK_IDENTIFIER) {
  3357. _set_error("Expected identifier!");
  3358. return ERR_PARSE_ERROR;
  3359. }
  3360. name = tk.text;
  3361. if (_find_identifier(NULL, Map<StringName, BuiltInInfo>(), name)) {
  3362. _set_error("Redefinition of '" + String(name) + "'");
  3363. return ERR_PARSE_ERROR;
  3364. }
  3365. if (uniform) {
  3366. ShaderNode::Uniform uniform;
  3367. if (is_sampler_type(type)) {
  3368. uniform.texture_order = texture_uniforms++;
  3369. uniform.order = -1;
  3370. } else {
  3371. uniform.texture_order = -1;
  3372. uniform.order = uniforms++;
  3373. }
  3374. uniform.type = type;
  3375. uniform.precission = precision;
  3376. //todo parse default value
  3377. tk = _get_token();
  3378. if (tk.type == TK_COLON) {
  3379. //hint
  3380. tk = _get_token();
  3381. if (tk.type == TK_HINT_WHITE_TEXTURE) {
  3382. uniform.hint = ShaderNode::Uniform::HINT_WHITE;
  3383. } else if (tk.type == TK_HINT_BLACK_TEXTURE) {
  3384. uniform.hint = ShaderNode::Uniform::HINT_BLACK;
  3385. } else if (tk.type == TK_HINT_NORMAL_TEXTURE) {
  3386. uniform.hint = ShaderNode::Uniform::HINT_NORMAL;
  3387. } else if (tk.type == TK_HINT_ANISO_TEXTURE) {
  3388. uniform.hint = ShaderNode::Uniform::HINT_ANISO;
  3389. } else if (tk.type == TK_HINT_ALBEDO_TEXTURE) {
  3390. uniform.hint = ShaderNode::Uniform::HINT_ALBEDO;
  3391. } else if (tk.type == TK_HINT_BLACK_ALBEDO_TEXTURE) {
  3392. uniform.hint = ShaderNode::Uniform::HINT_BLACK_ALBEDO;
  3393. } else if (tk.type == TK_HINT_COLOR) {
  3394. if (type != TYPE_VEC4) {
  3395. _set_error("Color hint is for vec4 only");
  3396. return ERR_PARSE_ERROR;
  3397. }
  3398. uniform.hint = ShaderNode::Uniform::HINT_COLOR;
  3399. } else if (tk.type == TK_HINT_RANGE) {
  3400. uniform.hint = ShaderNode::Uniform::HINT_RANGE;
  3401. if (type != TYPE_FLOAT && type != TYPE_INT) {
  3402. _set_error("Range hint is for float and int only");
  3403. return ERR_PARSE_ERROR;
  3404. }
  3405. tk = _get_token();
  3406. if (tk.type != TK_PARENTHESIS_OPEN) {
  3407. _set_error("Expected '(' after hint_range");
  3408. return ERR_PARSE_ERROR;
  3409. }
  3410. tk = _get_token();
  3411. float sign = 1.0;
  3412. if (tk.type == TK_OP_SUB) {
  3413. sign = -1.0;
  3414. tk = _get_token();
  3415. }
  3416. if (tk.type != TK_REAL_CONSTANT && tk.type != TK_INT_CONSTANT) {
  3417. _set_error("Expected integer constant");
  3418. return ERR_PARSE_ERROR;
  3419. }
  3420. uniform.hint_range[0] = tk.constant;
  3421. uniform.hint_range[0] *= sign;
  3422. tk = _get_token();
  3423. if (tk.type != TK_COMMA) {
  3424. _set_error("Expected ',' after integer constant");
  3425. return ERR_PARSE_ERROR;
  3426. }
  3427. tk = _get_token();
  3428. sign = 1.0;
  3429. if (tk.type == TK_OP_SUB) {
  3430. sign = -1.0;
  3431. tk = _get_token();
  3432. }
  3433. if (tk.type != TK_REAL_CONSTANT && tk.type != TK_INT_CONSTANT) {
  3434. _set_error("Expected integer constant after ','");
  3435. return ERR_PARSE_ERROR;
  3436. }
  3437. uniform.hint_range[1] = tk.constant;
  3438. uniform.hint_range[1] *= sign;
  3439. tk = _get_token();
  3440. if (tk.type == TK_COMMA) {
  3441. tk = _get_token();
  3442. if (tk.type != TK_REAL_CONSTANT && tk.type != TK_INT_CONSTANT) {
  3443. _set_error("Expected integer constant after ','");
  3444. return ERR_PARSE_ERROR;
  3445. }
  3446. uniform.hint_range[2] = tk.constant;
  3447. tk = _get_token();
  3448. } else {
  3449. if (type == TYPE_INT) {
  3450. uniform.hint_range[2] = 1;
  3451. } else {
  3452. uniform.hint_range[2] = 0.001;
  3453. }
  3454. }
  3455. if (tk.type != TK_PARENTHESIS_CLOSE) {
  3456. _set_error("Expected ','");
  3457. return ERR_PARSE_ERROR;
  3458. }
  3459. } else {
  3460. _set_error("Expected valid type hint after ':'.");
  3461. }
  3462. if (uniform.hint != ShaderNode::Uniform::HINT_RANGE && uniform.hint != ShaderNode::Uniform::HINT_NONE && uniform.hint != ShaderNode::Uniform::HINT_COLOR && type <= TYPE_MAT4) {
  3463. _set_error("This hint is only for sampler types");
  3464. return ERR_PARSE_ERROR;
  3465. }
  3466. tk = _get_token();
  3467. }
  3468. if (tk.type == TK_OP_ASSIGN) {
  3469. Node *expr = _parse_and_reduce_expression(NULL, Map<StringName, BuiltInInfo>());
  3470. if (!expr)
  3471. return ERR_PARSE_ERROR;
  3472. if (expr->type != Node::TYPE_CONSTANT) {
  3473. _set_error("Expected constant expression after '='");
  3474. return ERR_PARSE_ERROR;
  3475. }
  3476. ConstantNode *cn = static_cast<ConstantNode *>(expr);
  3477. uniform.default_value.resize(cn->values.size());
  3478. if (!convert_constant(cn, uniform.type, uniform.default_value.ptrw())) {
  3479. _set_error("Can't convert constant to " + get_datatype_name(uniform.type));
  3480. return ERR_PARSE_ERROR;
  3481. }
  3482. tk = _get_token();
  3483. }
  3484. shader->uniforms[name] = uniform;
  3485. if (tk.type != TK_SEMICOLON) {
  3486. _set_error("Expected ';'");
  3487. return ERR_PARSE_ERROR;
  3488. }
  3489. } else {
  3490. ShaderNode::Varying varying;
  3491. varying.type = type;
  3492. varying.precission = precision;
  3493. varying.interpolation = interpolation;
  3494. shader->varyings[name] = varying;
  3495. tk = _get_token();
  3496. if (tk.type != TK_SEMICOLON) {
  3497. _set_error("Expected ';'");
  3498. return ERR_PARSE_ERROR;
  3499. }
  3500. }
  3501. } break;
  3502. default: {
  3503. //function
  3504. DataPrecision precision = PRECISION_DEFAULT;
  3505. DataType type;
  3506. StringName name;
  3507. if (is_token_precision(tk.type)) {
  3508. precision = get_token_precision(tk.type);
  3509. tk = _get_token();
  3510. }
  3511. if (!is_token_datatype(tk.type)) {
  3512. _set_error("Expected function, uniform or varying ");
  3513. return ERR_PARSE_ERROR;
  3514. }
  3515. type = get_token_datatype(tk.type);
  3516. _get_completable_identifier(NULL, COMPLETION_MAIN_FUNCTION, name);
  3517. if (name == StringName()) {
  3518. _set_error("Expected function name after datatype");
  3519. return ERR_PARSE_ERROR;
  3520. }
  3521. if (_find_identifier(NULL, Map<StringName, BuiltInInfo>(), name)) {
  3522. _set_error("Redefinition of '" + String(name) + "'");
  3523. return ERR_PARSE_ERROR;
  3524. }
  3525. tk = _get_token();
  3526. if (tk.type != TK_PARENTHESIS_OPEN) {
  3527. _set_error("Expected '(' after identifier");
  3528. return ERR_PARSE_ERROR;
  3529. }
  3530. Map<StringName, BuiltInInfo> builtin_types;
  3531. if (p_functions.has(name)) {
  3532. builtin_types = p_functions[name].built_ins;
  3533. }
  3534. ShaderNode::Function function;
  3535. function.callable = !p_functions.has(name);
  3536. function.name = name;
  3537. FunctionNode *func_node = alloc_node<FunctionNode>();
  3538. function.function = func_node;
  3539. shader->functions.push_back(function);
  3540. func_node->name = name;
  3541. func_node->return_type = type;
  3542. func_node->return_precision = precision;
  3543. if (p_functions.has(name)) {
  3544. func_node->can_discard = p_functions[name].can_discard;
  3545. }
  3546. func_node->body = alloc_node<BlockNode>();
  3547. func_node->body->parent_function = func_node;
  3548. tk = _get_token();
  3549. while (true) {
  3550. if (tk.type == TK_PARENTHESIS_CLOSE) {
  3551. break;
  3552. }
  3553. ArgumentQualifier qualifier = ARGUMENT_QUALIFIER_IN;
  3554. if (tk.type == TK_ARG_IN) {
  3555. qualifier = ARGUMENT_QUALIFIER_IN;
  3556. tk = _get_token();
  3557. } else if (tk.type == TK_ARG_OUT) {
  3558. qualifier = ARGUMENT_QUALIFIER_OUT;
  3559. tk = _get_token();
  3560. } else if (tk.type == TK_ARG_INOUT) {
  3561. qualifier = ARGUMENT_QUALIFIER_INOUT;
  3562. tk = _get_token();
  3563. }
  3564. DataType ptype;
  3565. StringName pname;
  3566. DataPrecision pprecision = PRECISION_DEFAULT;
  3567. if (is_token_precision(tk.type)) {
  3568. pprecision = get_token_precision(tk.type);
  3569. tk = _get_token();
  3570. }
  3571. if (!is_token_datatype(tk.type)) {
  3572. _set_error("Expected a valid datatype for argument");
  3573. return ERR_PARSE_ERROR;
  3574. }
  3575. ptype = get_token_datatype(tk.type);
  3576. if (ptype == TYPE_VOID) {
  3577. _set_error("void not allowed in argument");
  3578. return ERR_PARSE_ERROR;
  3579. }
  3580. tk = _get_token();
  3581. if (tk.type != TK_IDENTIFIER) {
  3582. _set_error("Expected identifier for argument name");
  3583. return ERR_PARSE_ERROR;
  3584. }
  3585. pname = tk.text;
  3586. if (_find_identifier(func_node->body, builtin_types, pname)) {
  3587. _set_error("Redefinition of '" + String(pname) + "'");
  3588. return ERR_PARSE_ERROR;
  3589. }
  3590. FunctionNode::Argument arg;
  3591. arg.type = ptype;
  3592. arg.name = pname;
  3593. arg.precision = pprecision;
  3594. arg.qualifier = qualifier;
  3595. func_node->arguments.push_back(arg);
  3596. tk = _get_token();
  3597. if (tk.type == TK_COMMA) {
  3598. tk = _get_token();
  3599. //do none and go on
  3600. } else if (tk.type != TK_PARENTHESIS_CLOSE) {
  3601. _set_error("Expected ',' or ')' after identifier");
  3602. return ERR_PARSE_ERROR;
  3603. }
  3604. }
  3605. if (p_functions.has(name)) {
  3606. //if one of the core functions, make sure they are of the correct form
  3607. if (func_node->arguments.size() > 0) {
  3608. _set_error("Function '" + String(name) + "' expects no arguments.");
  3609. return ERR_PARSE_ERROR;
  3610. }
  3611. if (func_node->return_type != TYPE_VOID) {
  3612. _set_error("Function '" + String(name) + "' must be of void return type.");
  3613. return ERR_PARSE_ERROR;
  3614. }
  3615. }
  3616. //all good let's parse inside the function!
  3617. tk = _get_token();
  3618. if (tk.type != TK_CURLY_BRACKET_OPEN) {
  3619. _set_error("Expected '{' to begin function");
  3620. return ERR_PARSE_ERROR;
  3621. }
  3622. current_function = name;
  3623. Error err = _parse_block(func_node->body, builtin_types);
  3624. if (err)
  3625. return err;
  3626. current_function = StringName();
  3627. }
  3628. }
  3629. tk = _get_token();
  3630. }
  3631. return OK;
  3632. }
  3633. // skips over whitespace and /* */ and // comments
  3634. static int _get_first_ident_pos(const String &p_code) {
  3635. int idx = 0;
  3636. #define GETCHAR(m_idx) (((idx + m_idx) < p_code.length()) ? p_code[idx + m_idx] : CharType(0))
  3637. while (true) {
  3638. if (GETCHAR(0) == '/' && GETCHAR(1) == '/') {
  3639. idx += 2;
  3640. while (true) {
  3641. if (GETCHAR(0) == 0) return 0;
  3642. if (GETCHAR(0) == '\n') {
  3643. idx++;
  3644. break; // loop
  3645. }
  3646. idx++;
  3647. }
  3648. } else if (GETCHAR(0) == '/' && GETCHAR(1) == '*') {
  3649. idx += 2;
  3650. while (true) {
  3651. if (GETCHAR(0) == 0) return 0;
  3652. if (GETCHAR(0) == '*' && GETCHAR(1) == '/') {
  3653. idx += 2;
  3654. break; // loop
  3655. }
  3656. idx++;
  3657. }
  3658. } else {
  3659. switch (GETCHAR(0)) {
  3660. case ' ':
  3661. case '\t':
  3662. case '\r':
  3663. case '\n': {
  3664. idx++;
  3665. } break; // switch
  3666. default:
  3667. return idx;
  3668. }
  3669. }
  3670. }
  3671. #undef GETCHAR
  3672. }
  3673. String ShaderLanguage::get_shader_type(const String &p_code) {
  3674. bool reading_type = false;
  3675. String cur_identifier;
  3676. for (int i = _get_first_ident_pos(p_code); i < p_code.length(); i++) {
  3677. if (p_code[i] == ';') {
  3678. break;
  3679. } else if (p_code[i] <= 32) {
  3680. if (cur_identifier != String()) {
  3681. if (!reading_type) {
  3682. if (cur_identifier != "shader_type") {
  3683. return String();
  3684. }
  3685. reading_type = true;
  3686. cur_identifier = String();
  3687. } else {
  3688. return cur_identifier;
  3689. }
  3690. }
  3691. } else {
  3692. cur_identifier += String::chr(p_code[i]);
  3693. }
  3694. }
  3695. if (reading_type)
  3696. return cur_identifier;
  3697. return String();
  3698. }
  3699. Error ShaderLanguage::compile(const String &p_code, const Map<StringName, FunctionInfo> &p_functions, const Vector<StringName> &p_render_modes, const Set<String> &p_shader_types) {
  3700. clear();
  3701. code = p_code;
  3702. nodes = NULL;
  3703. shader = alloc_node<ShaderNode>();
  3704. Error err = _parse_shader(p_functions, p_render_modes, p_shader_types);
  3705. if (err != OK) {
  3706. return err;
  3707. }
  3708. return OK;
  3709. }
  3710. Error ShaderLanguage::complete(const String &p_code, const Map<StringName, FunctionInfo> &p_functions, const Vector<StringName> &p_render_modes, const Set<String> &p_shader_types, List<String> *r_options, String &r_call_hint) {
  3711. clear();
  3712. code = p_code;
  3713. nodes = NULL;
  3714. shader = alloc_node<ShaderNode>();
  3715. Error err = _parse_shader(p_functions, p_render_modes, p_shader_types);
  3716. if (err != OK)
  3717. ERR_PRINT("Failed to parse shader");
  3718. switch (completion_type) {
  3719. case COMPLETION_NONE: {
  3720. //do nothing
  3721. return OK;
  3722. } break;
  3723. case COMPLETION_RENDER_MODE: {
  3724. for (int i = 0; i < p_render_modes.size(); i++) {
  3725. r_options->push_back(p_render_modes[i]);
  3726. }
  3727. return OK;
  3728. } break;
  3729. case COMPLETION_MAIN_FUNCTION: {
  3730. for (const Map<StringName, FunctionInfo>::Element *E = p_functions.front(); E; E = E->next()) {
  3731. r_options->push_back(E->key());
  3732. }
  3733. return OK;
  3734. } break;
  3735. case COMPLETION_IDENTIFIER:
  3736. case COMPLETION_FUNCTION_CALL: {
  3737. bool comp_ident = completion_type == COMPLETION_IDENTIFIER;
  3738. Set<String> matches;
  3739. StringName skip_function;
  3740. BlockNode *block = completion_block;
  3741. while (block) {
  3742. if (comp_ident) {
  3743. for (const Map<StringName, BlockNode::Variable>::Element *E = block->variables.front(); E; E = E->next()) {
  3744. if (E->get().line < completion_line) {
  3745. matches.insert(E->key());
  3746. }
  3747. }
  3748. }
  3749. if (block->parent_function) {
  3750. if (comp_ident) {
  3751. for (int i = 0; i < block->parent_function->arguments.size(); i++) {
  3752. matches.insert(block->parent_function->arguments[i].name);
  3753. }
  3754. }
  3755. skip_function = block->parent_function->name;
  3756. }
  3757. block = block->parent_block;
  3758. }
  3759. if (comp_ident && skip_function != StringName() && p_functions.has(skip_function)) {
  3760. for (Map<StringName, BuiltInInfo>::Element *E = p_functions[skip_function].built_ins.front(); E; E = E->next()) {
  3761. matches.insert(E->key());
  3762. }
  3763. }
  3764. if (comp_ident) {
  3765. for (const Map<StringName, ShaderNode::Varying>::Element *E = shader->varyings.front(); E; E = E->next()) {
  3766. matches.insert(E->key());
  3767. }
  3768. for (const Map<StringName, ShaderNode::Uniform>::Element *E = shader->uniforms.front(); E; E = E->next()) {
  3769. matches.insert(E->key());
  3770. }
  3771. }
  3772. for (int i = 0; i < shader->functions.size(); i++) {
  3773. if (!shader->functions[i].callable || shader->functions[i].name == skip_function)
  3774. continue;
  3775. matches.insert(String(shader->functions[i].name) + "(");
  3776. }
  3777. int idx = 0;
  3778. while (builtin_func_defs[idx].name) {
  3779. matches.insert(String(builtin_func_defs[idx].name) + "(");
  3780. idx++;
  3781. }
  3782. for (Set<String>::Element *E = matches.front(); E; E = E->next()) {
  3783. r_options->push_back(E->get());
  3784. }
  3785. return OK;
  3786. } break;
  3787. case COMPLETION_CALL_ARGUMENTS: {
  3788. for (int i = 0; i < shader->functions.size(); i++) {
  3789. if (!shader->functions[i].callable)
  3790. continue;
  3791. if (shader->functions[i].name == completion_function) {
  3792. String calltip;
  3793. calltip += get_datatype_name(shader->functions[i].function->return_type);
  3794. calltip += " ";
  3795. calltip += shader->functions[i].name;
  3796. calltip += "(";
  3797. for (int j = 0; j < shader->functions[i].function->arguments.size(); j++) {
  3798. if (j > 0)
  3799. calltip += ", ";
  3800. else
  3801. calltip += " ";
  3802. if (j == completion_argument) {
  3803. calltip += CharType(0xFFFF);
  3804. }
  3805. calltip += get_datatype_name(shader->functions[i].function->arguments[j].type);
  3806. calltip += " ";
  3807. calltip += shader->functions[i].function->arguments[j].name;
  3808. if (j == completion_argument) {
  3809. calltip += CharType(0xFFFF);
  3810. }
  3811. }
  3812. if (shader->functions[i].function->arguments.size())
  3813. calltip += " ";
  3814. calltip += ")";
  3815. r_call_hint = calltip;
  3816. return OK;
  3817. }
  3818. }
  3819. int idx = 0;
  3820. String calltip;
  3821. while (builtin_func_defs[idx].name) {
  3822. if (completion_function == builtin_func_defs[idx].name) {
  3823. if (calltip.length())
  3824. calltip += "\n";
  3825. calltip += get_datatype_name(builtin_func_defs[idx].rettype);
  3826. calltip += " ";
  3827. calltip += builtin_func_defs[idx].name;
  3828. calltip += "(";
  3829. bool found_arg = false;
  3830. for (int i = 0; i < 4; i++) {
  3831. if (builtin_func_defs[idx].args[i] == TYPE_VOID)
  3832. break;
  3833. if (i > 0)
  3834. calltip += ", ";
  3835. else
  3836. calltip += " ";
  3837. if (i == completion_argument) {
  3838. calltip += CharType(0xFFFF);
  3839. }
  3840. calltip += get_datatype_name(builtin_func_defs[idx].args[i]);
  3841. if (i == completion_argument) {
  3842. calltip += CharType(0xFFFF);
  3843. }
  3844. found_arg = true;
  3845. }
  3846. if (found_arg)
  3847. calltip += " ";
  3848. calltip += ")";
  3849. }
  3850. idx++;
  3851. }
  3852. r_call_hint = calltip;
  3853. return OK;
  3854. } break;
  3855. case COMPLETION_INDEX: {
  3856. const char colv[4] = { 'r', 'g', 'b', 'a' };
  3857. const char coordv[4] = { 'x', 'y', 'z', 'w' };
  3858. int limit = 0;
  3859. switch (completion_base) {
  3860. case TYPE_BVEC2:
  3861. case TYPE_IVEC2:
  3862. case TYPE_UVEC2:
  3863. case TYPE_VEC2: {
  3864. limit = 2;
  3865. } break;
  3866. case TYPE_BVEC3:
  3867. case TYPE_IVEC3:
  3868. case TYPE_UVEC3:
  3869. case TYPE_VEC3: {
  3870. limit = 3;
  3871. } break;
  3872. case TYPE_BVEC4:
  3873. case TYPE_IVEC4:
  3874. case TYPE_UVEC4:
  3875. case TYPE_VEC4: {
  3876. limit = 4;
  3877. } break;
  3878. case TYPE_MAT2: limit = 2; break;
  3879. case TYPE_MAT3: limit = 3; break;
  3880. case TYPE_MAT4: limit = 4; break;
  3881. default: {}
  3882. }
  3883. for (int i = 0; i < limit; i++) {
  3884. r_options->push_back(String::chr(colv[i]));
  3885. r_options->push_back(String::chr(coordv[i]));
  3886. }
  3887. } break;
  3888. }
  3889. return ERR_PARSE_ERROR;
  3890. }
  3891. String ShaderLanguage::get_error_text() {
  3892. return error_str;
  3893. }
  3894. int ShaderLanguage::get_error_line() {
  3895. return error_line;
  3896. }
  3897. ShaderLanguage::ShaderNode *ShaderLanguage::get_shader() {
  3898. return shader;
  3899. }
  3900. ShaderLanguage::ShaderLanguage() {
  3901. nodes = NULL;
  3902. }
  3903. ShaderLanguage::~ShaderLanguage() {
  3904. clear();
  3905. }