shader_language.cpp 123 KB

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