1
0

effect-parser.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088
  1. /******************************************************************************
  2. Copyright (C) 2013 by Hugh Bailey <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include <assert.h>
  15. #include <limits.h>
  16. #include "../util/platform.h"
  17. #include "effect-parser.h"
  18. #include "effect.h"
  19. static inline bool ep_parse_param_assign(struct effect_parser *ep,
  20. struct ep_param *param);
  21. static enum gs_shader_param_type get_effect_param_type(const char *type)
  22. {
  23. if (strcmp(type, "float") == 0)
  24. return GS_SHADER_PARAM_FLOAT;
  25. else if (strcmp(type, "float2") == 0)
  26. return GS_SHADER_PARAM_VEC2;
  27. else if (strcmp(type, "float3") == 0)
  28. return GS_SHADER_PARAM_VEC3;
  29. else if (strcmp(type, "float4") == 0)
  30. return GS_SHADER_PARAM_VEC4;
  31. else if (strcmp(type, "int2") == 0)
  32. return GS_SHADER_PARAM_INT2;
  33. else if (strcmp(type, "int3") == 0)
  34. return GS_SHADER_PARAM_INT3;
  35. else if (strcmp(type, "int4") == 0)
  36. return GS_SHADER_PARAM_INT4;
  37. else if (astrcmp_n(type, "texture", 7) == 0)
  38. return GS_SHADER_PARAM_TEXTURE;
  39. else if (strcmp(type, "float4x4") == 0)
  40. return GS_SHADER_PARAM_MATRIX4X4;
  41. else if (strcmp(type, "bool") == 0)
  42. return GS_SHADER_PARAM_BOOL;
  43. else if (strcmp(type, "int") == 0)
  44. return GS_SHADER_PARAM_INT;
  45. else if (strcmp(type, "string") == 0)
  46. return GS_SHADER_PARAM_STRING;
  47. return GS_SHADER_PARAM_UNKNOWN;
  48. }
  49. void ep_free(struct effect_parser *ep)
  50. {
  51. size_t i;
  52. for (i = 0; i < ep->params.num; i++)
  53. ep_param_free(ep->params.array + i);
  54. for (i = 0; i < ep->structs.num; i++)
  55. ep_struct_free(ep->structs.array + i);
  56. for (i = 0; i < ep->funcs.num; i++)
  57. ep_func_free(ep->funcs.array + i);
  58. for (i = 0; i < ep->samplers.num; i++)
  59. ep_sampler_free(ep->samplers.array + i);
  60. for (i = 0; i < ep->techniques.num; i++)
  61. ep_technique_free(ep->techniques.array + i);
  62. ep->cur_pass = NULL;
  63. cf_parser_free(&ep->cfp);
  64. da_free(ep->params);
  65. da_free(ep->structs);
  66. da_free(ep->funcs);
  67. da_free(ep->samplers);
  68. da_free(ep->techniques);
  69. }
  70. static inline struct ep_func *ep_getfunc(struct effect_parser *ep,
  71. const char *name)
  72. {
  73. size_t i;
  74. for (i = 0; i < ep->funcs.num; i++) {
  75. if (strcmp(name, ep->funcs.array[i].name) == 0)
  76. return ep->funcs.array + i;
  77. }
  78. return NULL;
  79. }
  80. static inline struct ep_struct *ep_getstruct(struct effect_parser *ep,
  81. const char *name)
  82. {
  83. size_t i;
  84. for (i = 0; i < ep->structs.num; i++) {
  85. if (strcmp(name, ep->structs.array[i].name) == 0)
  86. return ep->structs.array + i;
  87. }
  88. return NULL;
  89. }
  90. static inline struct ep_sampler *ep_getsampler(struct effect_parser *ep,
  91. const char *name)
  92. {
  93. size_t i;
  94. for (i = 0; i < ep->samplers.num; i++) {
  95. if (strcmp(name, ep->samplers.array[i].name) == 0)
  96. return ep->samplers.array + i;
  97. }
  98. return NULL;
  99. }
  100. static inline struct ep_param *ep_getparam(struct effect_parser *ep,
  101. const char *name)
  102. {
  103. size_t i;
  104. for (i = 0; i < ep->params.num; i++) {
  105. if (strcmp(name, ep->params.array[i].name) == 0)
  106. return ep->params.array + i;
  107. }
  108. return NULL;
  109. }
  110. static inline struct ep_param *ep_getannotation(struct ep_param *param,
  111. const char *name)
  112. {
  113. size_t i;
  114. for (i = 0; i < param->annotations.num; i++) {
  115. if (strcmp(name, param->annotations.array[i].name) == 0)
  116. return param->annotations.array + i;
  117. }
  118. return NULL;
  119. }
  120. static inline struct ep_func *ep_getfunc_strref(struct effect_parser *ep,
  121. const struct strref *ref)
  122. {
  123. size_t i;
  124. for (i = 0; i < ep->funcs.num; i++) {
  125. if (strref_cmp(ref, ep->funcs.array[i].name) == 0)
  126. return ep->funcs.array + i;
  127. }
  128. return NULL;
  129. }
  130. static inline struct ep_struct *ep_getstruct_strref(struct effect_parser *ep,
  131. const struct strref *ref)
  132. {
  133. size_t i;
  134. for (i = 0; i < ep->structs.num; i++) {
  135. if (strref_cmp(ref, ep->structs.array[i].name) == 0)
  136. return ep->structs.array + i;
  137. }
  138. return NULL;
  139. }
  140. static inline struct ep_sampler *ep_getsampler_strref(struct effect_parser *ep,
  141. const struct strref *ref)
  142. {
  143. size_t i;
  144. for (i = 0; i < ep->samplers.num; i++) {
  145. if (strref_cmp(ref, ep->samplers.array[i].name) == 0)
  146. return ep->samplers.array + i;
  147. }
  148. return NULL;
  149. }
  150. static inline struct ep_param *ep_getparam_strref(struct effect_parser *ep,
  151. const struct strref *ref)
  152. {
  153. size_t i;
  154. for (i = 0; i < ep->params.num; i++) {
  155. if (strref_cmp(ref, ep->params.array[i].name) == 0)
  156. return ep->params.array + i;
  157. }
  158. return NULL;
  159. }
  160. static inline int ep_parse_struct_var(struct effect_parser *ep,
  161. struct ep_var *var)
  162. {
  163. int code;
  164. /* -------------------------------------- */
  165. /* variable type */
  166. if (!cf_next_valid_token(&ep->cfp))
  167. return PARSE_EOF;
  168. if (cf_token_is(&ep->cfp, ";"))
  169. return PARSE_CONTINUE;
  170. if (cf_token_is(&ep->cfp, "}"))
  171. return PARSE_BREAK;
  172. code = cf_token_is_type(&ep->cfp, CFTOKEN_NAME, "type name", ";");
  173. if (code != PARSE_SUCCESS)
  174. return code;
  175. cf_copy_token(&ep->cfp, &var->type);
  176. /* -------------------------------------- */
  177. /* variable name */
  178. if (!cf_next_valid_token(&ep->cfp))
  179. return PARSE_EOF;
  180. if (cf_token_is(&ep->cfp, ";"))
  181. return PARSE_UNEXPECTED_CONTINUE;
  182. if (cf_token_is(&ep->cfp, "}"))
  183. return PARSE_UNEXPECTED_BREAK;
  184. code = cf_token_is_type(&ep->cfp, CFTOKEN_NAME, "variable name", ";");
  185. if (code != PARSE_SUCCESS)
  186. return code;
  187. cf_copy_token(&ep->cfp, &var->name);
  188. /* -------------------------------------- */
  189. /* variable mapping if any (POSITION, TEXCOORD, etc) */
  190. if (!cf_next_valid_token(&ep->cfp))
  191. return PARSE_EOF;
  192. if (cf_token_is(&ep->cfp, ":")) {
  193. if (!cf_next_valid_token(&ep->cfp))
  194. return PARSE_EOF;
  195. if (cf_token_is(&ep->cfp, ";"))
  196. return PARSE_UNEXPECTED_CONTINUE;
  197. if (cf_token_is(&ep->cfp, "}"))
  198. return PARSE_UNEXPECTED_BREAK;
  199. code = cf_token_is_type(&ep->cfp, CFTOKEN_NAME, "mapping name",
  200. ";");
  201. if (code != PARSE_SUCCESS)
  202. return code;
  203. cf_copy_token(&ep->cfp, &var->mapping);
  204. if (!cf_next_valid_token(&ep->cfp))
  205. return PARSE_EOF;
  206. }
  207. /* -------------------------------------- */
  208. if (!cf_token_is(&ep->cfp, ";")) {
  209. if (!cf_go_to_valid_token(&ep->cfp, ";", "}"))
  210. return PARSE_EOF;
  211. return PARSE_CONTINUE;
  212. }
  213. return PARSE_SUCCESS;
  214. }
  215. static void ep_parse_struct(struct effect_parser *ep)
  216. {
  217. struct ep_struct eps;
  218. ep_struct_init(&eps);
  219. if (cf_next_name(&ep->cfp, &eps.name, "name", ";") != PARSE_SUCCESS)
  220. goto error;
  221. if (cf_next_token_should_be(&ep->cfp, "{", ";", NULL) != PARSE_SUCCESS)
  222. goto error;
  223. /* get structure variables */
  224. while (true) {
  225. bool do_break = false;
  226. struct ep_var var;
  227. ep_var_init(&var);
  228. switch (ep_parse_struct_var(ep, &var)) {
  229. case PARSE_UNEXPECTED_CONTINUE:
  230. cf_adderror_syntax_error(&ep->cfp);
  231. /* Falls through. */
  232. case PARSE_CONTINUE:
  233. ep_var_free(&var);
  234. continue;
  235. case PARSE_UNEXPECTED_BREAK:
  236. cf_adderror_syntax_error(&ep->cfp);
  237. /* Falls through. */
  238. case PARSE_BREAK:
  239. ep_var_free(&var);
  240. do_break = true;
  241. break;
  242. case PARSE_EOF:
  243. ep_var_free(&var);
  244. goto error;
  245. }
  246. if (do_break)
  247. break;
  248. da_push_back(eps.vars, &var);
  249. }
  250. if (cf_next_token_should_be(&ep->cfp, ";", NULL, NULL) != PARSE_SUCCESS)
  251. goto error;
  252. da_push_back(ep->structs, &eps);
  253. return;
  254. error:
  255. ep_struct_free(&eps);
  256. }
  257. static inline int ep_parse_param_annotation_var(struct effect_parser *ep,
  258. struct ep_param *var)
  259. {
  260. int code;
  261. /* -------------------------------------- */
  262. /* variable type */
  263. if (!cf_next_valid_token(&ep->cfp))
  264. return PARSE_EOF;
  265. if (cf_token_is(&ep->cfp, ";"))
  266. return PARSE_CONTINUE;
  267. if (cf_token_is(&ep->cfp, ">"))
  268. return PARSE_BREAK;
  269. code = cf_token_is_type(&ep->cfp, CFTOKEN_NAME, "type name", ";");
  270. if (code != PARSE_SUCCESS)
  271. return code;
  272. bfree(var->type);
  273. cf_copy_token(&ep->cfp, &var->type);
  274. /* -------------------------------------- */
  275. /* variable name */
  276. if (!cf_next_valid_token(&ep->cfp))
  277. return PARSE_EOF;
  278. if (cf_token_is(&ep->cfp, ";")) {
  279. cf_adderror_expecting(&ep->cfp, "variable name");
  280. return PARSE_UNEXPECTED_CONTINUE;
  281. }
  282. if (cf_token_is(&ep->cfp, ">")) {
  283. cf_adderror_expecting(&ep->cfp, "variable name");
  284. return PARSE_UNEXPECTED_BREAK;
  285. }
  286. code = cf_token_is_type(&ep->cfp, CFTOKEN_NAME, "variable name", ";");
  287. if (code != PARSE_SUCCESS)
  288. return code;
  289. bfree(var->name);
  290. cf_copy_token(&ep->cfp, &var->name);
  291. /* -------------------------------------- */
  292. /* variable mapping if any (POSITION, TEXCOORD, etc) */
  293. if (!cf_next_valid_token(&ep->cfp))
  294. return PARSE_EOF;
  295. if (cf_token_is(&ep->cfp, ":")) {
  296. cf_adderror_expecting(&ep->cfp, "= or ;");
  297. return PARSE_UNEXPECTED_BREAK;
  298. } else if (cf_token_is(&ep->cfp, ">")) {
  299. cf_adderror_expecting(&ep->cfp, "= or ;");
  300. return PARSE_UNEXPECTED_BREAK;
  301. } else if (cf_token_is(&ep->cfp, "=")) {
  302. if (!ep_parse_param_assign(ep, var)) {
  303. cf_adderror_expecting(&ep->cfp, "assignment value");
  304. return PARSE_UNEXPECTED_BREAK;
  305. }
  306. }
  307. /* -------------------------------------- */
  308. if (!cf_token_is(&ep->cfp, ";")) {
  309. if (!cf_go_to_valid_token(&ep->cfp, ";", ">")) {
  310. cf_adderror_expecting(&ep->cfp, "; or >");
  311. return PARSE_EOF;
  312. }
  313. return PARSE_CONTINUE;
  314. }
  315. return PARSE_SUCCESS;
  316. }
  317. static int ep_parse_annotations(struct effect_parser *ep,
  318. struct darray *annotations)
  319. {
  320. if (!cf_token_is(&ep->cfp, "<")) {
  321. cf_adderror_expecting(&ep->cfp, "<");
  322. goto error;
  323. }
  324. /* get annotation variables */
  325. while (true) {
  326. bool do_break = false;
  327. struct ep_param var;
  328. ep_param_init(&var, bstrdup(""), bstrdup(""), false, false,
  329. false);
  330. switch (ep_parse_param_annotation_var(ep, &var)) {
  331. case PARSE_UNEXPECTED_CONTINUE:
  332. cf_adderror_syntax_error(&ep->cfp);
  333. /* Falls through. */
  334. case PARSE_CONTINUE:
  335. ep_param_free(&var);
  336. continue;
  337. case PARSE_UNEXPECTED_BREAK:
  338. cf_adderror_syntax_error(&ep->cfp);
  339. /* Falls through. */
  340. case PARSE_BREAK:
  341. ep_param_free(&var);
  342. do_break = true;
  343. break;
  344. case PARSE_EOF:
  345. ep_param_free(&var);
  346. goto error;
  347. }
  348. if (do_break)
  349. break;
  350. darray_push_back(sizeof(struct ep_param), annotations, &var);
  351. }
  352. if (!cf_token_is(&ep->cfp, ">")) {
  353. cf_adderror_expecting(&ep->cfp, ">");
  354. goto error;
  355. }
  356. if (!cf_next_valid_token(&ep->cfp))
  357. goto error;
  358. return true;
  359. error:
  360. return false;
  361. }
  362. static int ep_parse_param_annotations(struct effect_parser *ep,
  363. struct ep_param *param)
  364. {
  365. return ep_parse_annotations(ep, &param->annotations.da);
  366. }
  367. static inline int ep_parse_pass_command_call(struct effect_parser *ep,
  368. struct darray *call)
  369. {
  370. struct cf_token end_token;
  371. cf_token_clear(&end_token);
  372. while (!cf_token_is(&ep->cfp, ";")) {
  373. if (cf_token_is(&ep->cfp, "}")) {
  374. cf_adderror_expecting(&ep->cfp, ";");
  375. return PARSE_CONTINUE;
  376. }
  377. darray_push_back(sizeof(struct cf_token), call,
  378. ep->cfp.cur_token);
  379. if (!cf_next_valid_token(&ep->cfp))
  380. return PARSE_EOF;
  381. }
  382. darray_push_back(sizeof(struct cf_token), call, ep->cfp.cur_token);
  383. darray_push_back(sizeof(struct cf_token), call, &end_token);
  384. return PARSE_SUCCESS;
  385. }
  386. static int ep_parse_pass_command(struct effect_parser *ep, struct ep_pass *pass)
  387. {
  388. struct darray *call; /* struct cf_token */
  389. if (!cf_next_valid_token(&ep->cfp))
  390. return PARSE_EOF;
  391. if (cf_token_is(&ep->cfp, "vertex_shader") ||
  392. cf_token_is(&ep->cfp, "vertex_program")) {
  393. call = &pass->vertex_program.da;
  394. } else if (cf_token_is(&ep->cfp, "pixel_shader") ||
  395. cf_token_is(&ep->cfp, "pixel_program")) {
  396. call = &pass->fragment_program.da;
  397. } else {
  398. cf_adderror_syntax_error(&ep->cfp);
  399. if (!cf_go_to_valid_token(&ep->cfp, ";", "}"))
  400. return PARSE_EOF;
  401. return PARSE_CONTINUE;
  402. }
  403. if (cf_next_token_should_be(&ep->cfp, "=", ";", "}") != PARSE_SUCCESS)
  404. return PARSE_CONTINUE;
  405. if (!cf_next_valid_token(&ep->cfp))
  406. return PARSE_EOF;
  407. if (cf_token_is(&ep->cfp, "compile")) {
  408. cf_adderror(&ep->cfp, "compile keyword not necessary",
  409. LEX_WARNING, NULL, NULL, NULL);
  410. if (!cf_next_valid_token(&ep->cfp))
  411. return PARSE_EOF;
  412. }
  413. return ep_parse_pass_command_call(ep, call);
  414. }
  415. static int ep_parse_pass(struct effect_parser *ep, struct ep_pass *pass)
  416. {
  417. struct cf_token peek;
  418. if (!cf_token_is(&ep->cfp, "pass"))
  419. return PARSE_UNEXPECTED_CONTINUE;
  420. if (!cf_next_valid_token(&ep->cfp))
  421. return PARSE_EOF;
  422. if (!cf_token_is(&ep->cfp, "{")) {
  423. pass->name = bstrdup_n(ep->cfp.cur_token->str.array,
  424. ep->cfp.cur_token->str.len);
  425. if (!cf_next_valid_token(&ep->cfp))
  426. return PARSE_EOF;
  427. }
  428. if (!cf_peek_valid_token(&ep->cfp, &peek))
  429. return PARSE_EOF;
  430. while (strref_cmp(&peek.str, "}") != 0) {
  431. int ret = ep_parse_pass_command(ep, pass);
  432. if (ret < 0 && ret != PARSE_CONTINUE)
  433. return ret;
  434. if (!cf_peek_valid_token(&ep->cfp, &peek))
  435. return PARSE_EOF;
  436. }
  437. /* token is '}' */
  438. cf_next_token(&ep->cfp);
  439. return PARSE_SUCCESS;
  440. }
  441. static void ep_parse_technique(struct effect_parser *ep)
  442. {
  443. struct ep_technique ept;
  444. ep_technique_init(&ept);
  445. if (cf_next_name(&ep->cfp, &ept.name, "name", ";") != PARSE_SUCCESS)
  446. goto error;
  447. if (!cf_next_valid_token(&ep->cfp))
  448. return;
  449. if (!cf_token_is(&ep->cfp, "{")) {
  450. if (!cf_go_to_token(&ep->cfp, ";", NULL)) {
  451. cf_adderror_expecting(&ep->cfp, ";");
  452. return;
  453. }
  454. cf_adderror_expecting(&ep->cfp, "{");
  455. goto error;
  456. }
  457. if (!cf_next_valid_token(&ep->cfp))
  458. goto error;
  459. while (!cf_token_is(&ep->cfp, "}")) {
  460. struct ep_pass pass;
  461. ep_pass_init(&pass);
  462. switch (ep_parse_pass(ep, &pass)) {
  463. case PARSE_UNEXPECTED_CONTINUE:
  464. ep_pass_free(&pass);
  465. if (!cf_go_to_token(&ep->cfp, "}", NULL))
  466. goto error;
  467. continue;
  468. case PARSE_EOF:
  469. ep_pass_free(&pass);
  470. goto error;
  471. }
  472. da_push_back(ept.passes, &pass);
  473. if (!cf_next_valid_token(&ep->cfp))
  474. goto error;
  475. }
  476. /* pass the current token (which is '}') if we reached here */
  477. cf_next_token(&ep->cfp);
  478. da_push_back(ep->techniques, &ept);
  479. return;
  480. error:
  481. cf_next_token(&ep->cfp);
  482. ep_technique_free(&ept);
  483. }
  484. static int ep_parse_sampler_state_item(struct effect_parser *ep,
  485. struct ep_sampler *eps)
  486. {
  487. int ret;
  488. char *state = NULL;
  489. struct dstr value = {0};
  490. ret = cf_next_name(&ep->cfp, &state, "state name", ";");
  491. if (ret != PARSE_SUCCESS)
  492. goto fail;
  493. ret = cf_next_token_should_be(&ep->cfp, "=", ";", NULL);
  494. if (ret != PARSE_SUCCESS)
  495. goto fail;
  496. for (;;) {
  497. const char *cur_str;
  498. if (!cf_next_valid_token(&ep->cfp))
  499. return PARSE_EOF;
  500. cur_str = ep->cfp.cur_token->str.array;
  501. if (*cur_str == ';')
  502. break;
  503. dstr_ncat(&value, cur_str, ep->cfp.cur_token->str.len);
  504. }
  505. if (value.len) {
  506. da_push_back(eps->states, &state);
  507. da_push_back(eps->values, &value.array);
  508. }
  509. return ret;
  510. fail:
  511. bfree(state);
  512. dstr_free(&value);
  513. return ret;
  514. }
  515. static void ep_parse_sampler_state(struct effect_parser *ep)
  516. {
  517. struct ep_sampler eps;
  518. struct cf_token peek;
  519. ep_sampler_init(&eps);
  520. if (cf_next_name(&ep->cfp, &eps.name, "name", ";") != PARSE_SUCCESS)
  521. goto error;
  522. if (cf_next_token_should_be(&ep->cfp, "{", ";", NULL) != PARSE_SUCCESS)
  523. goto error;
  524. if (!cf_peek_valid_token(&ep->cfp, &peek))
  525. goto error;
  526. while (strref_cmp(&peek.str, "}") != 0) {
  527. int ret = ep_parse_sampler_state_item(ep, &eps);
  528. if (ret == PARSE_EOF)
  529. goto error;
  530. if (!cf_peek_valid_token(&ep->cfp, &peek))
  531. goto error;
  532. }
  533. if (cf_next_token_should_be(&ep->cfp, "}", ";", NULL) != PARSE_SUCCESS)
  534. goto error;
  535. if (cf_next_token_should_be(&ep->cfp, ";", NULL, NULL) != PARSE_SUCCESS)
  536. goto error;
  537. da_push_back(ep->samplers, &eps);
  538. return;
  539. error:
  540. ep_sampler_free(&eps);
  541. }
  542. static inline int ep_check_for_keyword(struct effect_parser *ep,
  543. const char *keyword, bool *val)
  544. {
  545. bool new_val = cf_token_is(&ep->cfp, keyword);
  546. if (new_val) {
  547. if (!cf_next_valid_token(&ep->cfp))
  548. return PARSE_EOF;
  549. if (new_val && *val)
  550. cf_adderror(&ep->cfp, "'$1' keyword already specified",
  551. LEX_WARNING, keyword, NULL, NULL);
  552. *val = new_val;
  553. return PARSE_CONTINUE;
  554. }
  555. return PARSE_SUCCESS;
  556. }
  557. static inline int ep_parse_func_param(struct effect_parser *ep,
  558. struct ep_func *func, struct ep_var *var)
  559. {
  560. int code;
  561. bool var_type_keyword = false;
  562. if (!cf_next_valid_token(&ep->cfp))
  563. return PARSE_EOF;
  564. code = ep_check_for_keyword(ep, "in", &var_type_keyword);
  565. if (code == PARSE_EOF)
  566. return PARSE_EOF;
  567. else if (var_type_keyword)
  568. var->var_type = EP_VAR_IN;
  569. if (!var_type_keyword) {
  570. code = ep_check_for_keyword(ep, "inout", &var_type_keyword);
  571. if (code == PARSE_EOF)
  572. return PARSE_EOF;
  573. else if (var_type_keyword)
  574. var->var_type = EP_VAR_INOUT;
  575. }
  576. if (!var_type_keyword) {
  577. code = ep_check_for_keyword(ep, "out", &var_type_keyword);
  578. if (code == PARSE_EOF)
  579. return PARSE_EOF;
  580. else if (var_type_keyword)
  581. var->var_type = EP_VAR_OUT;
  582. }
  583. if (!var_type_keyword) {
  584. code = ep_check_for_keyword(ep, "uniform", &var_type_keyword);
  585. if (code == PARSE_EOF)
  586. return PARSE_EOF;
  587. else if (var_type_keyword)
  588. var->var_type = EP_VAR_UNIFORM;
  589. }
  590. code = cf_get_name(&ep->cfp, &var->type, "type", ")");
  591. if (code != PARSE_SUCCESS)
  592. return code;
  593. code = cf_next_name(&ep->cfp, &var->name, "name", ")");
  594. if (code != PARSE_SUCCESS)
  595. return code;
  596. if (!cf_next_valid_token(&ep->cfp))
  597. return PARSE_EOF;
  598. if (cf_token_is(&ep->cfp, ":")) {
  599. code = cf_next_name(&ep->cfp, &var->mapping,
  600. "mapping specifier", ")");
  601. if (code != PARSE_SUCCESS)
  602. return code;
  603. if (!cf_next_valid_token(&ep->cfp))
  604. return PARSE_EOF;
  605. }
  606. if (ep_getstruct(ep, var->type) != NULL)
  607. da_push_back(func->struct_deps, &var->type);
  608. else if (ep_getsampler(ep, var->type) != NULL)
  609. da_push_back(func->sampler_deps, &var->type);
  610. return PARSE_SUCCESS;
  611. }
  612. static bool ep_parse_func_params(struct effect_parser *ep, struct ep_func *func)
  613. {
  614. struct cf_token peek;
  615. int code;
  616. cf_token_clear(&peek);
  617. if (!cf_peek_valid_token(&ep->cfp, &peek))
  618. return false;
  619. if (*peek.str.array == ')') {
  620. cf_next_token(&ep->cfp);
  621. goto exit;
  622. }
  623. do {
  624. struct ep_var var;
  625. ep_var_init(&var);
  626. if (!cf_token_is(&ep->cfp, "(") && !cf_token_is(&ep->cfp, ","))
  627. cf_adderror_syntax_error(&ep->cfp);
  628. code = ep_parse_func_param(ep, func, &var);
  629. if (code != PARSE_SUCCESS) {
  630. ep_var_free(&var);
  631. if (code == PARSE_CONTINUE)
  632. goto exit;
  633. else if (code == PARSE_EOF)
  634. return false;
  635. }
  636. da_push_back(func->param_vars, &var);
  637. } while (!cf_token_is(&ep->cfp, ")"));
  638. exit:
  639. return true;
  640. }
  641. static inline bool ep_process_struct_dep(struct effect_parser *ep,
  642. struct ep_func *func)
  643. {
  644. struct ep_struct *val =
  645. ep_getstruct_strref(ep, &ep->cfp.cur_token->str);
  646. if (val)
  647. da_push_back(func->struct_deps, &val->name);
  648. return val != NULL;
  649. }
  650. static inline bool ep_process_func_dep(struct effect_parser *ep,
  651. struct ep_func *func)
  652. {
  653. struct ep_func *val = ep_getfunc_strref(ep, &ep->cfp.cur_token->str);
  654. if (val)
  655. da_push_back(func->func_deps, &val->name);
  656. return val != NULL;
  657. }
  658. static inline bool ep_process_sampler_dep(struct effect_parser *ep,
  659. struct ep_func *func)
  660. {
  661. struct ep_sampler *val =
  662. ep_getsampler_strref(ep, &ep->cfp.cur_token->str);
  663. if (val)
  664. da_push_back(func->sampler_deps, &val->name);
  665. return val != NULL;
  666. }
  667. static inline bool ep_process_param_dep(struct effect_parser *ep,
  668. struct ep_func *func)
  669. {
  670. struct ep_param *val = ep_getparam_strref(ep, &ep->cfp.cur_token->str);
  671. if (val)
  672. da_push_back(func->param_deps, &val->name);
  673. return val != NULL;
  674. }
  675. static inline bool ep_parse_func_contents(struct effect_parser *ep,
  676. struct ep_func *func)
  677. {
  678. int braces = 1;
  679. dstr_cat_strref(&func->contents, &ep->cfp.cur_token->str);
  680. while (braces > 0) {
  681. if ((ep->cfp.cur_token++)->type == CFTOKEN_NONE)
  682. return false;
  683. if (ep->cfp.cur_token->type == CFTOKEN_SPACETAB ||
  684. ep->cfp.cur_token->type == CFTOKEN_NEWLINE) {
  685. } else if (cf_token_is(&ep->cfp, "{")) {
  686. braces++;
  687. } else if (cf_token_is(&ep->cfp, "}")) {
  688. braces--;
  689. } else if (ep_process_struct_dep(ep, func) ||
  690. ep_process_func_dep(ep, func) ||
  691. ep_process_sampler_dep(ep, func) ||
  692. ep_process_param_dep(ep, func)) {
  693. }
  694. dstr_cat_strref(&func->contents, &ep->cfp.cur_token->str);
  695. }
  696. return true;
  697. }
  698. static void ep_parse_function(struct effect_parser *ep, char *type, char *name)
  699. {
  700. struct ep_func func;
  701. int code;
  702. ep_func_init(&func, type, name);
  703. if (ep_getstruct(ep, type))
  704. da_push_back(func.struct_deps, &func.ret_type);
  705. if (!ep_parse_func_params(ep, &func))
  706. goto error;
  707. if (!cf_next_valid_token(&ep->cfp))
  708. goto error;
  709. /* if function is mapped to something, for example COLOR */
  710. if (cf_token_is(&ep->cfp, ":")) {
  711. code = cf_next_name(&ep->cfp, &func.mapping,
  712. "mapping specifier", "{");
  713. if (code == PARSE_EOF)
  714. goto error;
  715. else if (code != PARSE_CONTINUE) {
  716. if (!cf_next_valid_token(&ep->cfp))
  717. goto error;
  718. }
  719. }
  720. if (!cf_token_is(&ep->cfp, "{")) {
  721. cf_adderror_expecting(&ep->cfp, "{");
  722. goto error;
  723. }
  724. if (!ep_parse_func_contents(ep, &func))
  725. goto error;
  726. /* it is established that the current token is '}' if we reach this */
  727. cf_next_token(&ep->cfp);
  728. da_push_back(ep->funcs, &func);
  729. return;
  730. error:
  731. ep_func_free(&func);
  732. }
  733. /* parses "array[count]" */
  734. static bool ep_parse_param_array(struct effect_parser *ep,
  735. struct ep_param *param)
  736. {
  737. if (!cf_next_valid_token(&ep->cfp))
  738. return false;
  739. if (ep->cfp.cur_token->type != CFTOKEN_NUM ||
  740. !valid_int_str(ep->cfp.cur_token->str.array,
  741. ep->cfp.cur_token->str.len))
  742. return false;
  743. param->array_count =
  744. (int)strtol(ep->cfp.cur_token->str.array, NULL, 10);
  745. if (cf_next_token_should_be(&ep->cfp, "]", ";", NULL) == PARSE_EOF)
  746. return false;
  747. if (!cf_next_valid_token(&ep->cfp))
  748. return false;
  749. return true;
  750. }
  751. static inline int ep_parse_param_assign_texture(struct effect_parser *ep,
  752. struct ep_param *param)
  753. {
  754. int code;
  755. char *str;
  756. if (!cf_next_valid_token(&ep->cfp))
  757. return PARSE_EOF;
  758. code = cf_token_is_type(&ep->cfp, CFTOKEN_STRING, "texture path string",
  759. ";");
  760. if (code != PARSE_SUCCESS)
  761. return code;
  762. str = cf_literal_to_str(ep->cfp.cur_token->str.array,
  763. ep->cfp.cur_token->str.len);
  764. if (str) {
  765. da_copy_array(param->default_val, str, strlen(str) + 1);
  766. bfree(str);
  767. }
  768. return PARSE_SUCCESS;
  769. }
  770. static inline int ep_parse_param_assign_string(struct effect_parser *ep,
  771. struct ep_param *param)
  772. {
  773. int code;
  774. char *str = NULL;
  775. if (!cf_next_valid_token(&ep->cfp))
  776. return PARSE_EOF;
  777. code = cf_token_is_type(&ep->cfp, CFTOKEN_STRING, "string", ";");
  778. if (code != PARSE_SUCCESS)
  779. return code;
  780. str = cf_literal_to_str(ep->cfp.cur_token->str.array,
  781. ep->cfp.cur_token->str.len);
  782. if (str) {
  783. da_copy_array(param->default_val, str, strlen(str) + 1);
  784. bfree(str);
  785. }
  786. return PARSE_SUCCESS;
  787. }
  788. static inline int ep_parse_param_assign_intfloat(struct effect_parser *ep,
  789. struct ep_param *param,
  790. bool is_float)
  791. {
  792. int code;
  793. bool is_negative = false;
  794. if (!cf_next_valid_token(&ep->cfp))
  795. return PARSE_EOF;
  796. if (cf_token_is(&ep->cfp, "-")) {
  797. is_negative = true;
  798. if (!cf_next_token(&ep->cfp))
  799. return PARSE_EOF;
  800. }
  801. code = cf_token_is_type(&ep->cfp, CFTOKEN_NUM, "numeric value", ";");
  802. if (code != PARSE_SUCCESS)
  803. return code;
  804. if (is_float) {
  805. float f = (float)os_strtod(ep->cfp.cur_token->str.array);
  806. if (is_negative)
  807. f = -f;
  808. da_push_back_array(param->default_val, &f, sizeof(float));
  809. } else {
  810. long l = strtol(ep->cfp.cur_token->str.array, NULL, 10);
  811. if (is_negative)
  812. l = -l;
  813. da_push_back_array(param->default_val, &l, sizeof(long));
  814. }
  815. return PARSE_SUCCESS;
  816. }
  817. static inline int ep_parse_param_assign_bool(struct effect_parser *ep,
  818. struct ep_param *param)
  819. {
  820. if (!cf_next_valid_token(&ep->cfp))
  821. return PARSE_EOF;
  822. if (cf_token_is(&ep->cfp, "true")) {
  823. long l = 1;
  824. da_push_back_array(param->default_val, &l, sizeof(long));
  825. return PARSE_SUCCESS;
  826. } else if (cf_token_is(&ep->cfp, "false")) {
  827. long l = 0;
  828. da_push_back_array(param->default_val, &l, sizeof(long));
  829. return PARSE_SUCCESS;
  830. }
  831. cf_adderror_expecting(&ep->cfp, "true or false");
  832. return PARSE_EOF;
  833. }
  834. /*
  835. * parses assignment for float1, float2, float3, float4, int1, int2, int3, int4,
  836. * and any combination for float3x3, float4x4, int3x3, int4x4, etc
  837. */
  838. static inline int ep_parse_param_assign_intfloat_array(struct effect_parser *ep,
  839. struct ep_param *param,
  840. bool is_float)
  841. {
  842. const char *intfloat_type = param->type + (is_float ? 5 : 3);
  843. int intfloat_count = 0, code, i;
  844. /* -------------------------------------------- */
  845. if (intfloat_type[0] < '1' || intfloat_type[0] > '4')
  846. cf_adderror(&ep->cfp, "Invalid row count", LEX_ERROR, NULL,
  847. NULL, NULL);
  848. intfloat_count = intfloat_type[0] - '0';
  849. if (intfloat_type[1] == 'x') {
  850. if (intfloat_type[2] < '1' || intfloat_type[2] > '4')
  851. cf_adderror(&ep->cfp, "Invalid column count", LEX_ERROR,
  852. NULL, NULL, NULL);
  853. intfloat_count *= intfloat_type[2] - '0';
  854. }
  855. /* -------------------------------------------- */
  856. code = cf_next_token_should_be(&ep->cfp, "{", ";", NULL);
  857. if (code != PARSE_SUCCESS)
  858. return code;
  859. for (i = 0; i < intfloat_count; i++) {
  860. char *next = ((i + 1) < intfloat_count) ? "," : "}";
  861. code = ep_parse_param_assign_intfloat(ep, param, is_float);
  862. if (code != PARSE_SUCCESS)
  863. return code;
  864. code = cf_next_token_should_be(&ep->cfp, next, ";", NULL);
  865. if (code != PARSE_SUCCESS)
  866. return code;
  867. }
  868. return PARSE_SUCCESS;
  869. }
  870. static int ep_parse_param_assignment_val(struct effect_parser *ep,
  871. struct ep_param *param)
  872. {
  873. if (param->is_texture)
  874. return ep_parse_param_assign_texture(ep, param);
  875. else if (strcmp(param->type, "int") == 0)
  876. return ep_parse_param_assign_intfloat(ep, param, false);
  877. else if (strcmp(param->type, "float") == 0)
  878. return ep_parse_param_assign_intfloat(ep, param, true);
  879. else if (astrcmp_n(param->type, "int", 3) == 0)
  880. return ep_parse_param_assign_intfloat_array(ep, param, false);
  881. else if (astrcmp_n(param->type, "float", 5) == 0)
  882. return ep_parse_param_assign_intfloat_array(ep, param, true);
  883. else if (astrcmp_n(param->type, "string", 6) == 0)
  884. return ep_parse_param_assign_string(ep, param);
  885. else if (strcmp(param->type, "bool") == 0)
  886. return ep_parse_param_assign_bool(ep, param);
  887. cf_adderror(&ep->cfp, "Invalid type '$1' used for assignment",
  888. LEX_ERROR, param->type, NULL, NULL);
  889. return PARSE_CONTINUE;
  890. }
  891. static inline bool ep_parse_param_assign(struct effect_parser *ep,
  892. struct ep_param *param)
  893. {
  894. if (ep_parse_param_assignment_val(ep, param) != PARSE_SUCCESS)
  895. return false;
  896. if (!cf_next_valid_token(&ep->cfp))
  897. return false;
  898. return true;
  899. }
  900. /* static bool ep_parse_param_property(struct effect_parser *ep,
  901. struct ep_param *param)
  902. {
  903. } */
  904. static void ep_parse_param(struct effect_parser *ep, char *type, char *name,
  905. bool is_property, bool is_const, bool is_uniform)
  906. {
  907. struct ep_param param;
  908. ep_param_init(&param, type, name, is_property, is_const, is_uniform);
  909. if (cf_token_is(&ep->cfp, ";"))
  910. goto complete;
  911. if (cf_token_is(&ep->cfp, "[") && !ep_parse_param_array(ep, &param))
  912. goto error;
  913. if (cf_token_is(&ep->cfp, "<") &&
  914. !ep_parse_param_annotations(ep, &param))
  915. goto error;
  916. if (cf_token_is(&ep->cfp, "=") && !ep_parse_param_assign(ep, &param))
  917. goto error;
  918. /*
  919. if (cf_token_is(&ep->cfp, "<") && !ep_parse_param_property(ep, &param))
  920. goto error; */
  921. if (!cf_token_is(&ep->cfp, ";"))
  922. goto error;
  923. complete:
  924. da_push_back(ep->params, &param);
  925. return;
  926. error:
  927. ep_param_free(&param);
  928. }
  929. static bool ep_get_var_specifiers(struct effect_parser *ep, bool *is_property,
  930. bool *is_const, bool *is_uniform)
  931. {
  932. while (true) {
  933. int code;
  934. code = ep_check_for_keyword(ep, "property", is_property);
  935. if (code == PARSE_EOF)
  936. return false;
  937. else if (code == PARSE_CONTINUE)
  938. continue;
  939. code = ep_check_for_keyword(ep, "const", is_const);
  940. if (code == PARSE_EOF)
  941. return false;
  942. else if (code == PARSE_CONTINUE)
  943. continue;
  944. code = ep_check_for_keyword(ep, "uniform", is_uniform);
  945. if (code == PARSE_EOF)
  946. return false;
  947. else if (code == PARSE_CONTINUE)
  948. continue;
  949. break;
  950. }
  951. return true;
  952. }
  953. static inline void report_invalid_func_keyword(struct effect_parser *ep,
  954. const char *name, bool val)
  955. {
  956. if (val)
  957. cf_adderror(&ep->cfp,
  958. "'$1' keyword cannot be used with a "
  959. "function",
  960. LEX_ERROR, name, NULL, NULL);
  961. }
  962. static void ep_parse_other(struct effect_parser *ep)
  963. {
  964. bool is_property = false, is_const = false, is_uniform = false;
  965. char *type = NULL, *name = NULL;
  966. if (!ep_get_var_specifiers(ep, &is_property, &is_const, &is_uniform))
  967. goto error;
  968. if (cf_get_name(&ep->cfp, &type, "type", ";") != PARSE_SUCCESS)
  969. goto error;
  970. if (cf_next_name(&ep->cfp, &name, "name", ";") != PARSE_SUCCESS)
  971. goto error;
  972. if (!cf_next_valid_token(&ep->cfp))
  973. goto error;
  974. if (cf_token_is(&ep->cfp, "(")) {
  975. report_invalid_func_keyword(ep, "property", is_property);
  976. report_invalid_func_keyword(ep, "const", is_const);
  977. report_invalid_func_keyword(ep, "uniform", is_uniform);
  978. ep_parse_function(ep, type, name);
  979. return;
  980. } else {
  981. ep_parse_param(ep, type, name, is_property, is_const,
  982. is_uniform);
  983. return;
  984. }
  985. error:
  986. bfree(type);
  987. bfree(name);
  988. }
  989. static bool ep_compile(struct effect_parser *ep);
  990. extern const char *gs_preprocessor_name(void);
  991. #if defined(_DEBUG) && defined(_DEBUG_SHADERS)
  992. static void debug_get_default_value(struct gs_effect_param *param, char *buffer,
  993. unsigned long long buf_size)
  994. {
  995. if (param->default_val.num == 0) {
  996. snprintf(buffer, buf_size, "(null)");
  997. return;
  998. }
  999. switch (param->type) {
  1000. case GS_SHADER_PARAM_STRING:
  1001. snprintf(buffer, buf_size, "'%.*s'", param->default_val.num,
  1002. param->default_val.array);
  1003. break;
  1004. case GS_SHADER_PARAM_INT:
  1005. snprintf(buffer, buf_size, "%ld",
  1006. *(int *)(param->default_val.array + 0));
  1007. break;
  1008. case GS_SHADER_PARAM_INT2:
  1009. snprintf(buffer, buf_size, "%ld,%ld",
  1010. *(int *)(param->default_val.array + 0),
  1011. *(int *)(param->default_val.array + 4));
  1012. break;
  1013. case GS_SHADER_PARAM_INT3:
  1014. snprintf(buffer, buf_size, "%ld,%ld,%ld",
  1015. *(int *)(param->default_val.array + 0),
  1016. *(int *)(param->default_val.array + 4),
  1017. *(int *)(param->default_val.array + 8));
  1018. break;
  1019. case GS_SHADER_PARAM_INT4:
  1020. snprintf(buffer, buf_size, "%ld,%ld,%ld,%ld",
  1021. *(int *)(param->default_val.array + 0),
  1022. *(int *)(param->default_val.array + 4),
  1023. *(int *)(param->default_val.array + 8),
  1024. *(int *)(param->default_val.array + 12));
  1025. break;
  1026. case GS_SHADER_PARAM_FLOAT:
  1027. snprintf(buffer, buf_size, "%e",
  1028. *(float *)(param->default_val.array + 0));
  1029. break;
  1030. case GS_SHADER_PARAM_VEC2:
  1031. snprintf(buffer, buf_size, "%e,%e",
  1032. *(float *)(param->default_val.array + 0),
  1033. *(float *)(param->default_val.array + 4));
  1034. break;
  1035. case GS_SHADER_PARAM_VEC3:
  1036. snprintf(buffer, buf_size, "%e,%e,%e",
  1037. *(float *)(param->default_val.array + 0),
  1038. *(float *)(param->default_val.array + 4),
  1039. *(float *)(param->default_val.array + 8));
  1040. break;
  1041. case GS_SHADER_PARAM_VEC4:
  1042. snprintf(buffer, buf_size, "%e,%e,%e,%e",
  1043. *(float *)(param->default_val.array + 0),
  1044. *(float *)(param->default_val.array + 4),
  1045. *(float *)(param->default_val.array + 8),
  1046. *(float *)(param->default_val.array + 12));
  1047. break;
  1048. case GS_SHADER_PARAM_MATRIX4X4:
  1049. snprintf(buffer, buf_size,
  1050. "[[%e,%e,%e,%e],[%e,%e,%e,%e],"
  1051. "[%e,%e,%e,%e],[%e,%e,%e,%e]]",
  1052. *(float *)(param->default_val.array + 0),
  1053. *(float *)(param->default_val.array + 4),
  1054. *(float *)(param->default_val.array + 8),
  1055. *(float *)(param->default_val.array + 12),
  1056. *(float *)(param->default_val.array + 16),
  1057. *(float *)(param->default_val.array + 20),
  1058. *(float *)(param->default_val.array + 24),
  1059. *(float *)(param->default_val.array + 28),
  1060. *(float *)(param->default_val.array + 32),
  1061. *(float *)(param->default_val.array + 36),
  1062. *(float *)(param->default_val.array + 40),
  1063. *(float *)(param->default_val.array + 44),
  1064. *(float *)(param->default_val.array + 48),
  1065. *(float *)(param->default_val.array + 52),
  1066. *(float *)(param->default_val.array + 56),
  1067. *(float *)(param->default_val.array + 60));
  1068. break;
  1069. case GS_SHADER_PARAM_BOOL:
  1070. snprintf(buffer, buf_size, "%s",
  1071. (*param->default_val.array) != 0 ? "true\0"
  1072. : "false\0");
  1073. break;
  1074. case GS_SHADER_PARAM_UNKNOWN:
  1075. case GS_SHADER_PARAM_TEXTURE:
  1076. snprintf(buffer, buf_size, "<unknown>");
  1077. break;
  1078. }
  1079. }
  1080. static void debug_param(struct gs_effect_param *param,
  1081. struct ep_param *param_in, unsigned long long idx,
  1082. const char *offset)
  1083. {
  1084. char _debug_type[4096];
  1085. switch (param->type) {
  1086. case GS_SHADER_PARAM_STRING:
  1087. snprintf(_debug_type, sizeof(_debug_type), "string");
  1088. break;
  1089. case GS_SHADER_PARAM_INT:
  1090. snprintf(_debug_type, sizeof(_debug_type), "int");
  1091. break;
  1092. case GS_SHADER_PARAM_INT2:
  1093. snprintf(_debug_type, sizeof(_debug_type), "int2");
  1094. break;
  1095. case GS_SHADER_PARAM_INT3:
  1096. snprintf(_debug_type, sizeof(_debug_type), "int3");
  1097. break;
  1098. case GS_SHADER_PARAM_INT4:
  1099. snprintf(_debug_type, sizeof(_debug_type), "int4");
  1100. break;
  1101. case GS_SHADER_PARAM_FLOAT:
  1102. snprintf(_debug_type, sizeof(_debug_type), "float");
  1103. break;
  1104. case GS_SHADER_PARAM_VEC2:
  1105. snprintf(_debug_type, sizeof(_debug_type), "float2");
  1106. break;
  1107. case GS_SHADER_PARAM_VEC3:
  1108. snprintf(_debug_type, sizeof(_debug_type), "float3");
  1109. break;
  1110. case GS_SHADER_PARAM_VEC4:
  1111. snprintf(_debug_type, sizeof(_debug_type), "float4");
  1112. break;
  1113. case GS_SHADER_PARAM_MATRIX4X4:
  1114. snprintf(_debug_type, sizeof(_debug_type), "float4x4");
  1115. break;
  1116. case GS_SHADER_PARAM_BOOL:
  1117. snprintf(_debug_type, sizeof(_debug_type), "bool");
  1118. break;
  1119. case GS_SHADER_PARAM_UNKNOWN:
  1120. snprintf(_debug_type, sizeof(_debug_type), "unknown");
  1121. break;
  1122. case GS_SHADER_PARAM_TEXTURE:
  1123. snprintf(_debug_type, sizeof(_debug_type), "texture");
  1124. break;
  1125. }
  1126. char _debug_buf[4096];
  1127. debug_get_default_value(param, _debug_buf, sizeof(_debug_buf));
  1128. if (param->annotations.num > 0) {
  1129. blog(LOG_DEBUG,
  1130. "%s[%4lld] %.*s '%s' with value %.*s and %lld annotations:",
  1131. offset, idx, sizeof(_debug_type), _debug_type, param->name,
  1132. sizeof(_debug_buf), _debug_buf, param->annotations.num);
  1133. } else {
  1134. blog(LOG_DEBUG, "%s[%4lld] %.*s '%s' with value %.*s.", offset,
  1135. idx, sizeof(_debug_type), _debug_type, param->name,
  1136. sizeof(_debug_buf), _debug_buf);
  1137. }
  1138. }
  1139. static void debug_param_annotation(struct gs_effect_param *param,
  1140. struct ep_param *param_in,
  1141. unsigned long long idx, const char *offset)
  1142. {
  1143. char _debug_buf[4096];
  1144. debug_get_default_value(param, _debug_buf, sizeof(_debug_buf));
  1145. blog(LOG_DEBUG, "%s[%4lld] %s '%s' with value %.*s", offset, idx,
  1146. param_in->type, param->name, sizeof(_debug_buf), _debug_buf);
  1147. }
  1148. static void debug_print_string(const char *offset, const char *str)
  1149. {
  1150. // Bypass 4096 limit in def_log_handler.
  1151. char const *begin = str;
  1152. unsigned long long line = 1;
  1153. for (char const *here = begin; here[0] != '\0'; here++) {
  1154. char const *str = begin;
  1155. unsigned long long len = here - begin;
  1156. bool is_line = false;
  1157. if (here[0] == '\r') {
  1158. is_line = true;
  1159. if (here[1] == '\n') {
  1160. here += 1;
  1161. }
  1162. begin = here + 1;
  1163. } else if (here[0] == '\n') {
  1164. is_line = true;
  1165. begin = here + 1;
  1166. }
  1167. if (is_line) {
  1168. blog(LOG_DEBUG, "\t\t\t\t[%4lld] %.*s", line, len, str);
  1169. line++;
  1170. }
  1171. }
  1172. if (begin[0] != '\0') {
  1173. // Final line was not written.
  1174. blog(LOG_DEBUG, "\t\t\t\t[%4lld] %*s", line, strlen(begin),
  1175. begin);
  1176. }
  1177. }
  1178. #endif
  1179. bool ep_parse(struct effect_parser *ep, gs_effect_t *effect,
  1180. const char *effect_string, const char *file)
  1181. {
  1182. bool success;
  1183. const char *graphics_preprocessor = gs_preprocessor_name();
  1184. if (graphics_preprocessor) {
  1185. struct cf_def def;
  1186. cf_def_init(&def);
  1187. def.name.str.array = graphics_preprocessor;
  1188. def.name.str.len = strlen(graphics_preprocessor);
  1189. strref_copy(&def.name.unmerged_str, &def.name.str);
  1190. cf_preprocessor_add_def(&ep->cfp.pp, &def);
  1191. }
  1192. ep->effect = effect;
  1193. if (!cf_parser_parse(&ep->cfp, effect_string, file))
  1194. return false;
  1195. while (ep->cfp.cur_token && ep->cfp.cur_token->type != CFTOKEN_NONE) {
  1196. if (cf_token_is(&ep->cfp, ";") ||
  1197. is_whitespace(*ep->cfp.cur_token->str.array)) {
  1198. /* do nothing */
  1199. ep->cfp.cur_token++;
  1200. } else if (cf_token_is(&ep->cfp, "struct")) {
  1201. ep_parse_struct(ep);
  1202. } else if (cf_token_is(&ep->cfp, "technique")) {
  1203. ep_parse_technique(ep);
  1204. } else if (cf_token_is(&ep->cfp, "sampler_state")) {
  1205. ep_parse_sampler_state(ep);
  1206. } else if (cf_token_is(&ep->cfp, "{")) {
  1207. /* add error and pass braces */
  1208. cf_adderror(&ep->cfp, "Unexpected code segment",
  1209. LEX_ERROR, NULL, NULL, NULL);
  1210. cf_pass_pair(&ep->cfp, '{', '}');
  1211. } else {
  1212. /* parameters and functions */
  1213. ep_parse_other(ep);
  1214. }
  1215. }
  1216. #if defined(_DEBUG) && defined(_DEBUG_SHADERS)
  1217. blog(LOG_DEBUG,
  1218. "================================================================================");
  1219. blog(LOG_DEBUG, "Effect Parser reformatted shader '%s' to:", file);
  1220. debug_print_string("\t", ep->cfp.lex.reformatted);
  1221. #endif
  1222. success = !error_data_has_errors(&ep->cfp.error_list);
  1223. if (success)
  1224. success = ep_compile(ep);
  1225. #if defined(_DEBUG) && defined(_DEBUG_SHADERS)
  1226. blog(LOG_DEBUG,
  1227. "================================================================================");
  1228. #endif
  1229. return success;
  1230. }
  1231. /* ------------------------------------------------------------------------- */
  1232. static inline void ep_write_param(struct dstr *shader, struct ep_param *param,
  1233. struct darray *used_params)
  1234. {
  1235. if (param->written)
  1236. return;
  1237. if (param->is_const) {
  1238. dstr_cat(shader, "const ");
  1239. } else if (param->is_uniform) {
  1240. struct dstr new;
  1241. dstr_init_copy(&new, param->name);
  1242. darray_push_back(sizeof(struct dstr), used_params, &new);
  1243. dstr_cat(shader, "uniform ");
  1244. }
  1245. dstr_cat(shader, param->type);
  1246. dstr_cat(shader, " ");
  1247. dstr_cat(shader, param->name);
  1248. if (param->array_count)
  1249. dstr_catf(shader, "[%u]", param->array_count);
  1250. dstr_cat(shader, ";\n");
  1251. param->written = true;
  1252. }
  1253. static inline void ep_write_func_param_deps(struct effect_parser *ep,
  1254. struct dstr *shader,
  1255. struct ep_func *func,
  1256. struct darray *used_params)
  1257. {
  1258. size_t i;
  1259. for (i = 0; i < func->param_deps.num; i++) {
  1260. const char *name = func->param_deps.array[i];
  1261. struct ep_param *param = ep_getparam(ep, name);
  1262. ep_write_param(shader, param, used_params);
  1263. }
  1264. if (func->param_deps.num)
  1265. dstr_cat(shader, "\n\n");
  1266. }
  1267. static void ep_write_sampler(struct dstr *shader, struct ep_sampler *sampler)
  1268. {
  1269. size_t i;
  1270. if (sampler->written)
  1271. return;
  1272. dstr_cat(shader, "sampler_state ");
  1273. dstr_cat(shader, sampler->name);
  1274. dstr_cat(shader, " {");
  1275. for (i = 0; i < sampler->values.num; i++) {
  1276. dstr_cat(shader, "\n\t");
  1277. dstr_cat(shader, sampler->states.array[i]);
  1278. dstr_cat(shader, " = ");
  1279. dstr_cat(shader, sampler->values.array[i]);
  1280. dstr_cat(shader, ";\n");
  1281. }
  1282. dstr_cat(shader, "\n};\n");
  1283. sampler->written = true;
  1284. }
  1285. static inline void ep_write_func_sampler_deps(struct effect_parser *ep,
  1286. struct dstr *shader,
  1287. struct ep_func *func)
  1288. {
  1289. size_t i;
  1290. for (i = 0; i < func->sampler_deps.num; i++) {
  1291. const char *name = func->sampler_deps.array[i];
  1292. struct ep_sampler *sampler = ep_getsampler(ep, name);
  1293. ep_write_sampler(shader, sampler);
  1294. dstr_cat(shader, "\n");
  1295. }
  1296. }
  1297. static inline void ep_write_var(struct dstr *shader, struct ep_var *var)
  1298. {
  1299. if (var->var_type == EP_VAR_INOUT)
  1300. dstr_cat(shader, "inout ");
  1301. else if (var->var_type == EP_VAR_OUT)
  1302. dstr_cat(shader, "out ");
  1303. else if (var->var_type == EP_VAR_UNIFORM)
  1304. dstr_cat(shader, "uniform ");
  1305. // The "in" input modifier is implied by default, so leave it blank
  1306. // in that case.
  1307. dstr_cat(shader, var->type);
  1308. dstr_cat(shader, " ");
  1309. dstr_cat(shader, var->name);
  1310. if (var->mapping) {
  1311. dstr_cat(shader, " : ");
  1312. dstr_cat(shader, var->mapping);
  1313. }
  1314. }
  1315. static void ep_write_struct(struct dstr *shader, struct ep_struct *st)
  1316. {
  1317. size_t i;
  1318. if (st->written)
  1319. return;
  1320. dstr_cat(shader, "struct ");
  1321. dstr_cat(shader, st->name);
  1322. dstr_cat(shader, " {");
  1323. for (i = 0; i < st->vars.num; i++) {
  1324. dstr_cat(shader, "\n\t");
  1325. ep_write_var(shader, st->vars.array + i);
  1326. dstr_cat(shader, ";");
  1327. }
  1328. dstr_cat(shader, "\n};\n");
  1329. st->written = true;
  1330. }
  1331. static inline void ep_write_func_struct_deps(struct effect_parser *ep,
  1332. struct dstr *shader,
  1333. struct ep_func *func)
  1334. {
  1335. size_t i;
  1336. for (i = 0; i < func->struct_deps.num; i++) {
  1337. const char *name = func->struct_deps.array[i];
  1338. struct ep_struct *st = ep_getstruct(ep, name);
  1339. if (!st->written) {
  1340. ep_write_struct(shader, st);
  1341. dstr_cat(shader, "\n");
  1342. st->written = true;
  1343. }
  1344. }
  1345. }
  1346. static void ep_write_func(struct effect_parser *ep, struct dstr *shader,
  1347. struct ep_func *func, struct darray *used_params);
  1348. static inline void ep_write_func_func_deps(struct effect_parser *ep,
  1349. struct dstr *shader,
  1350. struct ep_func *func,
  1351. struct darray *used_params)
  1352. {
  1353. size_t i;
  1354. for (i = 0; i < func->func_deps.num; i++) {
  1355. const char *name = func->func_deps.array[i];
  1356. struct ep_func *func_dep = ep_getfunc(ep, name);
  1357. if (!func_dep->written) {
  1358. ep_write_func(ep, shader, func_dep, used_params);
  1359. dstr_cat(shader, "\n\n");
  1360. }
  1361. }
  1362. }
  1363. static void ep_write_func(struct effect_parser *ep, struct dstr *shader,
  1364. struct ep_func *func, struct darray *used_params)
  1365. {
  1366. size_t i;
  1367. func->written = true;
  1368. ep_write_func_param_deps(ep, shader, func, used_params);
  1369. ep_write_func_sampler_deps(ep, shader, func);
  1370. ep_write_func_struct_deps(ep, shader, func);
  1371. ep_write_func_func_deps(ep, shader, func, used_params);
  1372. /* ------------------------------------ */
  1373. dstr_cat(shader, func->ret_type);
  1374. dstr_cat(shader, " ");
  1375. dstr_cat(shader, func->name);
  1376. dstr_cat(shader, "(");
  1377. for (i = 0; i < func->param_vars.num; i++) {
  1378. struct ep_var *var = func->param_vars.array + i;
  1379. if (i)
  1380. dstr_cat(shader, ", ");
  1381. ep_write_var(shader, var);
  1382. }
  1383. dstr_cat(shader, ")\n");
  1384. dstr_cat_dstr(shader, &func->contents);
  1385. dstr_cat(shader, "\n");
  1386. }
  1387. /* writes mapped vars used by the call as parameters for main */
  1388. static void ep_write_main_params(struct effect_parser *ep, struct dstr *shader,
  1389. struct dstr *param_str, struct ep_func *func)
  1390. {
  1391. size_t i;
  1392. bool empty_params = dstr_is_empty(param_str);
  1393. for (i = 0; i < func->param_vars.num; i++) {
  1394. struct ep_var *var = func->param_vars.array + i;
  1395. struct ep_struct *st = NULL;
  1396. bool mapped = (var->mapping != NULL);
  1397. if (!mapped) {
  1398. st = ep_getstruct(ep, var->type);
  1399. if (st)
  1400. mapped = ep_struct_mapped(st);
  1401. }
  1402. if (mapped) {
  1403. dstr_cat(shader, var->type);
  1404. dstr_cat(shader, " ");
  1405. dstr_cat(shader, var->name);
  1406. if (!st) {
  1407. dstr_cat(shader, " : ");
  1408. dstr_cat(shader, var->mapping);
  1409. }
  1410. if (!dstr_is_empty(param_str))
  1411. dstr_cat(param_str, ", ");
  1412. dstr_cat(param_str, var->name);
  1413. }
  1414. }
  1415. if (!empty_params)
  1416. dstr_cat(param_str, ", ");
  1417. }
  1418. static void ep_write_main(struct effect_parser *ep, struct dstr *shader,
  1419. struct ep_func *func, struct dstr *call_str)
  1420. {
  1421. struct dstr param_str;
  1422. struct dstr adjusted_call;
  1423. dstr_init(&param_str);
  1424. dstr_init_copy_dstr(&adjusted_call, call_str);
  1425. dstr_cat(shader, "\n");
  1426. dstr_cat(shader, func->ret_type);
  1427. dstr_cat(shader, " main(");
  1428. ep_write_main_params(ep, shader, &param_str, func);
  1429. dstr_cat(shader, ")");
  1430. if (func->mapping) {
  1431. dstr_cat(shader, " : ");
  1432. dstr_cat(shader, func->mapping);
  1433. }
  1434. dstr_cat(shader, "\n{\n\treturn ");
  1435. dstr_cat_dstr(shader, &adjusted_call);
  1436. dstr_cat(shader, "\n}\n");
  1437. dstr_free(&adjusted_call);
  1438. dstr_free(&param_str);
  1439. }
  1440. static inline void ep_reset_written(struct effect_parser *ep)
  1441. {
  1442. size_t i;
  1443. for (i = 0; i < ep->params.num; i++)
  1444. ep->params.array[i].written = false;
  1445. for (i = 0; i < ep->structs.num; i++)
  1446. ep->structs.array[i].written = false;
  1447. for (i = 0; i < ep->funcs.num; i++)
  1448. ep->funcs.array[i].written = false;
  1449. for (i = 0; i < ep->samplers.num; i++)
  1450. ep->samplers.array[i].written = false;
  1451. }
  1452. static void ep_makeshaderstring(struct effect_parser *ep, struct dstr *shader,
  1453. struct darray *shader_call,
  1454. struct darray *used_params)
  1455. {
  1456. struct cf_token *token = shader_call->array;
  1457. struct cf_token *func_name;
  1458. struct ep_func *func;
  1459. struct dstr call_str;
  1460. dstr_init(&call_str);
  1461. if (!token)
  1462. return;
  1463. while (token->type != CFTOKEN_NONE && is_whitespace(*token->str.array))
  1464. token++;
  1465. if (token->type == CFTOKEN_NONE || strref_cmp(&token->str, "NULL") == 0)
  1466. return;
  1467. func_name = token;
  1468. while (token->type != CFTOKEN_NONE) {
  1469. struct ep_param *param = ep_getparam_strref(ep, &token->str);
  1470. if (param)
  1471. ep_write_param(shader, param, used_params);
  1472. dstr_cat_strref(&call_str, &token->str);
  1473. token++;
  1474. }
  1475. func = ep_getfunc_strref(ep, &func_name->str);
  1476. if (!func)
  1477. return;
  1478. ep_write_func(ep, shader, func, used_params);
  1479. ep_write_main(ep, shader, func, &call_str);
  1480. dstr_free(&call_str);
  1481. ep_reset_written(ep);
  1482. }
  1483. static void ep_compile_annotations(struct darray *ep_annotations,
  1484. struct darray *gsp_annotations,
  1485. struct effect_parser *ep)
  1486. {
  1487. darray_resize(sizeof(struct gs_effect_param), gsp_annotations,
  1488. ep_annotations->num);
  1489. size_t i;
  1490. for (i = 0; i < ep_annotations->num; i++) {
  1491. struct gs_effect_param *param =
  1492. ((struct gs_effect_param *)gsp_annotations->array) + i;
  1493. struct ep_param *param_in =
  1494. ((struct ep_param *)ep_annotations->array) + i;
  1495. param->name = bstrdup(param_in->name);
  1496. param->section = EFFECT_ANNOTATION;
  1497. param->effect = ep->effect;
  1498. da_move(param->default_val, param_in->default_val);
  1499. param->type = get_effect_param_type(param_in->type);
  1500. #if defined(_DEBUG) && defined(_DEBUG_SHADERS)
  1501. debug_param(param, param_in, i, "\t\t");
  1502. #endif
  1503. }
  1504. }
  1505. static void
  1506. ep_compile_param_annotations(struct ep_param *ep_param_input,
  1507. struct gs_effect_param *gs_effect_input,
  1508. struct effect_parser *ep)
  1509. {
  1510. ep_compile_annotations(&(ep_param_input->annotations.da),
  1511. &(gs_effect_input->annotations.da), ep);
  1512. }
  1513. static void ep_compile_param(struct effect_parser *ep, size_t idx)
  1514. {
  1515. struct gs_effect_param *param;
  1516. struct ep_param *param_in;
  1517. param = ep->effect->params.array + idx;
  1518. param_in = ep->params.array + idx;
  1519. param_in->param = param;
  1520. param->name = bstrdup(param_in->name);
  1521. param->section = EFFECT_PARAM;
  1522. param->effect = ep->effect;
  1523. da_move(param->default_val, param_in->default_val);
  1524. param->type = get_effect_param_type(param_in->type);
  1525. if (strcmp(param_in->name, "ViewProj") == 0)
  1526. ep->effect->view_proj = param;
  1527. else if (strcmp(param_in->name, "World") == 0)
  1528. ep->effect->world = param;
  1529. #if defined(_DEBUG) && defined(_DEBUG_SHADERS)
  1530. debug_param(param, param_in, idx, "\t");
  1531. #endif
  1532. ep_compile_param_annotations(param_in, param, ep);
  1533. }
  1534. static bool ep_compile_pass_shaderparams(struct effect_parser *ep,
  1535. struct darray *pass_params,
  1536. struct darray *used_params,
  1537. gs_shader_t *shader)
  1538. {
  1539. size_t i;
  1540. darray_resize(sizeof(struct pass_shaderparam), pass_params,
  1541. used_params->num);
  1542. for (i = 0; i < pass_params->num; i++) {
  1543. struct dstr *param_name;
  1544. struct pass_shaderparam *param;
  1545. param_name = darray_item(sizeof(struct dstr), used_params, i);
  1546. param = darray_item(sizeof(struct pass_shaderparam),
  1547. pass_params, i);
  1548. param->eparam = gs_effect_get_param_by_name(ep->effect,
  1549. param_name->array);
  1550. param->sparam =
  1551. gs_shader_get_param_by_name(shader, param_name->array);
  1552. #if defined(_DEBUG) && defined(_DEBUG_SHADERS)
  1553. debug_param(param->eparam, 0, i, "\t\t\t\t");
  1554. #endif
  1555. if (!param->sparam) {
  1556. blog(LOG_ERROR, "Effect shader parameter not found");
  1557. return false;
  1558. }
  1559. }
  1560. return true;
  1561. }
  1562. static inline bool ep_compile_pass_shader(struct effect_parser *ep,
  1563. struct gs_effect_technique *tech,
  1564. struct gs_effect_pass *pass,
  1565. struct ep_pass *pass_in,
  1566. size_t pass_idx,
  1567. enum gs_shader_type type)
  1568. {
  1569. struct dstr shader_str;
  1570. struct dstr location;
  1571. struct darray used_params; /* struct dstr */
  1572. struct darray *pass_params = NULL; /* struct pass_shaderparam */
  1573. gs_shader_t *shader = NULL;
  1574. bool success = true;
  1575. dstr_init(&shader_str);
  1576. darray_init(&used_params);
  1577. dstr_init(&location);
  1578. dstr_copy(&location, ep->cfp.lex.file);
  1579. if (type == GS_SHADER_VERTEX)
  1580. dstr_cat(&location, " (Vertex ");
  1581. else if (type == GS_SHADER_PIXEL)
  1582. dstr_cat(&location, " (Pixel ");
  1583. /*else if (type == SHADER_GEOMETRY)
  1584. dstr_cat(&location, " (Geometry ");*/
  1585. assert(pass_idx <= UINT_MAX);
  1586. dstr_catf(&location, "shader, technique %s, pass %u)", tech->name,
  1587. (unsigned)pass_idx);
  1588. if (type == GS_SHADER_VERTEX) {
  1589. ep_makeshaderstring(ep, &shader_str,
  1590. &pass_in->vertex_program.da, &used_params);
  1591. pass->vertshader = gs_vertexshader_create(shader_str.array,
  1592. location.array, NULL);
  1593. shader = pass->vertshader;
  1594. pass_params = &pass->vertshader_params.da;
  1595. } else if (type == GS_SHADER_PIXEL) {
  1596. ep_makeshaderstring(ep, &shader_str,
  1597. &pass_in->fragment_program.da,
  1598. &used_params);
  1599. pass->pixelshader = gs_pixelshader_create(shader_str.array,
  1600. location.array, NULL);
  1601. shader = pass->pixelshader;
  1602. pass_params = &pass->pixelshader_params.da;
  1603. }
  1604. #if defined(_DEBUG) && defined(_DEBUG_SHADERS)
  1605. blog(LOG_DEBUG, "\t\t\t%s Shader:",
  1606. type == GS_SHADER_VERTEX ? "Vertex" : "Fragment");
  1607. blog(LOG_DEBUG, "\t\t\tCode:");
  1608. debug_print_string("\t\t\t\t\t", shader_str.array);
  1609. blog(LOG_DEBUG, "\t\t\tParameters:");
  1610. #endif
  1611. if (shader)
  1612. success = ep_compile_pass_shaderparams(ep, pass_params,
  1613. &used_params, shader);
  1614. else
  1615. success = false;
  1616. dstr_free(&location);
  1617. dstr_array_free(used_params.array, used_params.num);
  1618. darray_free(&used_params);
  1619. dstr_free(&shader_str);
  1620. return success;
  1621. }
  1622. static bool ep_compile_pass(struct effect_parser *ep,
  1623. struct gs_effect_technique *tech,
  1624. struct ep_technique *tech_in, size_t idx)
  1625. {
  1626. struct gs_effect_pass *pass;
  1627. struct ep_pass *pass_in;
  1628. bool success = true;
  1629. pass = tech->passes.array + idx;
  1630. pass_in = tech_in->passes.array + idx;
  1631. pass->name = bstrdup(pass_in->name);
  1632. pass->section = EFFECT_PASS;
  1633. #if defined(_DEBUG) && defined(_DEBUG_SHADERS)
  1634. blog(LOG_DEBUG, "\t\t[%4lld] Pass '%s':", idx, pass->name);
  1635. #endif
  1636. if (!ep_compile_pass_shader(ep, tech, pass, pass_in, idx,
  1637. GS_SHADER_VERTEX)) {
  1638. success = false;
  1639. blog(LOG_ERROR, "Pass (%zu) <%s> missing vertex shader!", idx,
  1640. pass->name ? pass->name : "");
  1641. }
  1642. if (!ep_compile_pass_shader(ep, tech, pass, pass_in, idx,
  1643. GS_SHADER_PIXEL)) {
  1644. success = false;
  1645. blog(LOG_ERROR, "Pass (%zu) <%s> missing pixel shader!", idx,
  1646. pass->name ? pass->name : "");
  1647. }
  1648. return success;
  1649. }
  1650. static inline bool ep_compile_technique(struct effect_parser *ep, size_t idx)
  1651. {
  1652. struct gs_effect_technique *tech;
  1653. struct ep_technique *tech_in;
  1654. bool success = true;
  1655. size_t i;
  1656. tech = ep->effect->techniques.array + idx;
  1657. tech_in = ep->techniques.array + idx;
  1658. tech->name = bstrdup(tech_in->name);
  1659. tech->section = EFFECT_TECHNIQUE;
  1660. tech->effect = ep->effect;
  1661. da_resize(tech->passes, tech_in->passes.num);
  1662. #if defined(_DEBUG) && defined(_DEBUG_SHADERS)
  1663. blog(LOG_DEBUG, "\t[%4lld] Technique '%s' has %lld passes:", idx,
  1664. tech->name, tech->passes.num);
  1665. #endif
  1666. for (i = 0; i < tech->passes.num; i++) {
  1667. if (!ep_compile_pass(ep, tech, tech_in, i))
  1668. success = false;
  1669. }
  1670. return success;
  1671. }
  1672. static bool ep_compile(struct effect_parser *ep)
  1673. {
  1674. bool success = true;
  1675. size_t i;
  1676. assert(ep->effect);
  1677. da_resize(ep->effect->params, ep->params.num);
  1678. da_resize(ep->effect->techniques, ep->techniques.num);
  1679. #if defined(_DEBUG) && defined(_DEBUG_SHADERS)
  1680. blog(LOG_DEBUG, "Shader has %lld parameters:", ep->params.num);
  1681. #endif
  1682. for (i = 0; i < ep->params.num; i++)
  1683. ep_compile_param(ep, i);
  1684. #if defined(_DEBUG) && defined(_DEBUG_SHADERS)
  1685. blog(LOG_DEBUG, "Shader has %lld techniques:", ep->techniques.num);
  1686. #endif
  1687. for (i = 0; i < ep->techniques.num; i++) {
  1688. if (!ep_compile_technique(ep, i))
  1689. success = false;
  1690. }
  1691. return success;
  1692. }