shader_language.cpp 116 KB

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