1
0

effect-parser.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091
  1. /******************************************************************************
  2. Copyright (C) 2023 by Lain 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. typedef DARRAY(struct dstr) dstr_array_t;
  20. static inline bool ep_parse_param_assign(struct effect_parser *ep,
  21. struct ep_param *param);
  22. static enum gs_shader_param_type get_effect_param_type(const char *type)
  23. {
  24. if (strcmp(type, "float") == 0)
  25. return GS_SHADER_PARAM_FLOAT;
  26. else if (strcmp(type, "float2") == 0)
  27. return GS_SHADER_PARAM_VEC2;
  28. else if (strcmp(type, "float3") == 0)
  29. return GS_SHADER_PARAM_VEC3;
  30. else if (strcmp(type, "float4") == 0)
  31. return GS_SHADER_PARAM_VEC4;
  32. else if (strcmp(type, "int2") == 0)
  33. return GS_SHADER_PARAM_INT2;
  34. else if (strcmp(type, "int3") == 0)
  35. return GS_SHADER_PARAM_INT3;
  36. else if (strcmp(type, "int4") == 0)
  37. return GS_SHADER_PARAM_INT4;
  38. else if (astrcmp_n(type, "texture", 7) == 0)
  39. return GS_SHADER_PARAM_TEXTURE;
  40. else if (strcmp(type, "float4x4") == 0)
  41. return GS_SHADER_PARAM_MATRIX4X4;
  42. else if (strcmp(type, "bool") == 0)
  43. return GS_SHADER_PARAM_BOOL;
  44. else if (strcmp(type, "int") == 0)
  45. return GS_SHADER_PARAM_INT;
  46. else if (strcmp(type, "string") == 0)
  47. return GS_SHADER_PARAM_STRING;
  48. return GS_SHADER_PARAM_UNKNOWN;
  49. }
  50. void ep_free(struct effect_parser *ep)
  51. {
  52. size_t i;
  53. for (i = 0; i < ep->params.num; i++)
  54. ep_param_free(ep->params.array + i);
  55. for (i = 0; i < ep->structs.num; i++)
  56. ep_struct_free(ep->structs.array + i);
  57. for (i = 0; i < ep->funcs.num; i++)
  58. ep_func_free(ep->funcs.array + i);
  59. for (i = 0; i < ep->samplers.num; i++)
  60. ep_sampler_free(ep->samplers.array + i);
  61. for (i = 0; i < ep->techniques.num; i++)
  62. ep_technique_free(ep->techniques.array + i);
  63. ep->cur_pass = NULL;
  64. cf_parser_free(&ep->cfp);
  65. da_free(ep->params);
  66. da_free(ep->structs);
  67. da_free(ep->funcs);
  68. da_free(ep->samplers);
  69. da_free(ep->techniques);
  70. }
  71. static inline struct ep_func *ep_getfunc(struct effect_parser *ep,
  72. const char *name)
  73. {
  74. size_t i;
  75. for (i = 0; i < ep->funcs.num; i++) {
  76. if (strcmp(name, ep->funcs.array[i].name) == 0)
  77. return ep->funcs.array + i;
  78. }
  79. return NULL;
  80. }
  81. static inline struct ep_struct *ep_getstruct(struct effect_parser *ep,
  82. const char *name)
  83. {
  84. size_t i;
  85. for (i = 0; i < ep->structs.num; i++) {
  86. if (strcmp(name, ep->structs.array[i].name) == 0)
  87. return ep->structs.array + i;
  88. }
  89. return NULL;
  90. }
  91. static inline struct ep_sampler *ep_getsampler(struct effect_parser *ep,
  92. const char *name)
  93. {
  94. size_t i;
  95. for (i = 0; i < ep->samplers.num; i++) {
  96. if (strcmp(name, ep->samplers.array[i].name) == 0)
  97. return ep->samplers.array + i;
  98. }
  99. return NULL;
  100. }
  101. static inline struct ep_param *ep_getparam(struct effect_parser *ep,
  102. const char *name)
  103. {
  104. size_t i;
  105. for (i = 0; i < ep->params.num; i++) {
  106. if (strcmp(name, ep->params.array[i].name) == 0)
  107. return ep->params.array + i;
  108. }
  109. return NULL;
  110. }
  111. static inline struct ep_param *ep_getannotation(struct ep_param *param,
  112. const char *name)
  113. {
  114. size_t i;
  115. for (i = 0; i < param->annotations.num; i++) {
  116. if (strcmp(name, param->annotations.array[i].name) == 0)
  117. return param->annotations.array + i;
  118. }
  119. return NULL;
  120. }
  121. static inline struct ep_func *ep_getfunc_strref(struct effect_parser *ep,
  122. const struct strref *ref)
  123. {
  124. size_t i;
  125. for (i = 0; i < ep->funcs.num; i++) {
  126. if (strref_cmp(ref, ep->funcs.array[i].name) == 0)
  127. return ep->funcs.array + i;
  128. }
  129. return NULL;
  130. }
  131. static inline struct ep_struct *ep_getstruct_strref(struct effect_parser *ep,
  132. const struct strref *ref)
  133. {
  134. size_t i;
  135. for (i = 0; i < ep->structs.num; i++) {
  136. if (strref_cmp(ref, ep->structs.array[i].name) == 0)
  137. return ep->structs.array + i;
  138. }
  139. return NULL;
  140. }
  141. static inline struct ep_sampler *ep_getsampler_strref(struct effect_parser *ep,
  142. const struct strref *ref)
  143. {
  144. size_t i;
  145. for (i = 0; i < ep->samplers.num; i++) {
  146. if (strref_cmp(ref, ep->samplers.array[i].name) == 0)
  147. return ep->samplers.array + i;
  148. }
  149. return NULL;
  150. }
  151. static inline struct ep_param *ep_getparam_strref(struct effect_parser *ep,
  152. const struct strref *ref)
  153. {
  154. size_t i;
  155. for (i = 0; i < ep->params.num; i++) {
  156. if (strref_cmp(ref, ep->params.array[i].name) == 0)
  157. return ep->params.array + i;
  158. }
  159. return NULL;
  160. }
  161. static inline int ep_parse_struct_var(struct effect_parser *ep,
  162. struct ep_var *var)
  163. {
  164. int code;
  165. /* -------------------------------------- */
  166. /* variable type */
  167. if (!cf_next_valid_token(&ep->cfp))
  168. return PARSE_EOF;
  169. if (cf_token_is(&ep->cfp, ";"))
  170. return PARSE_CONTINUE;
  171. if (cf_token_is(&ep->cfp, "}"))
  172. return PARSE_BREAK;
  173. code = cf_token_is_type(&ep->cfp, CFTOKEN_NAME, "type name", ";");
  174. if (code != PARSE_SUCCESS)
  175. return code;
  176. cf_copy_token(&ep->cfp, &var->type);
  177. /* -------------------------------------- */
  178. /* variable name */
  179. if (!cf_next_valid_token(&ep->cfp))
  180. return PARSE_EOF;
  181. if (cf_token_is(&ep->cfp, ";"))
  182. return PARSE_UNEXPECTED_CONTINUE;
  183. if (cf_token_is(&ep->cfp, "}"))
  184. return PARSE_UNEXPECTED_BREAK;
  185. code = cf_token_is_type(&ep->cfp, CFTOKEN_NAME, "variable name", ";");
  186. if (code != PARSE_SUCCESS)
  187. return code;
  188. cf_copy_token(&ep->cfp, &var->name);
  189. /* -------------------------------------- */
  190. /* variable mapping if any (POSITION, TEXCOORD, etc) */
  191. if (!cf_next_valid_token(&ep->cfp))
  192. return PARSE_EOF;
  193. if (cf_token_is(&ep->cfp, ":")) {
  194. if (!cf_next_valid_token(&ep->cfp))
  195. return PARSE_EOF;
  196. if (cf_token_is(&ep->cfp, ";"))
  197. return PARSE_UNEXPECTED_CONTINUE;
  198. if (cf_token_is(&ep->cfp, "}"))
  199. return PARSE_UNEXPECTED_BREAK;
  200. code = cf_token_is_type(&ep->cfp, CFTOKEN_NAME, "mapping name",
  201. ";");
  202. if (code != PARSE_SUCCESS)
  203. return code;
  204. cf_copy_token(&ep->cfp, &var->mapping);
  205. if (!cf_next_valid_token(&ep->cfp))
  206. return PARSE_EOF;
  207. }
  208. /* -------------------------------------- */
  209. if (!cf_token_is(&ep->cfp, ";")) {
  210. if (!cf_go_to_valid_token(&ep->cfp, ";", "}"))
  211. return PARSE_EOF;
  212. return PARSE_CONTINUE;
  213. }
  214. return PARSE_SUCCESS;
  215. }
  216. static void ep_parse_struct(struct effect_parser *ep)
  217. {
  218. struct ep_struct eps;
  219. ep_struct_init(&eps);
  220. if (cf_next_name(&ep->cfp, &eps.name, "name", ";") != PARSE_SUCCESS)
  221. goto error;
  222. if (cf_next_token_should_be(&ep->cfp, "{", ";", NULL) != PARSE_SUCCESS)
  223. goto error;
  224. /* get structure variables */
  225. while (true) {
  226. bool do_break = false;
  227. struct ep_var var;
  228. ep_var_init(&var);
  229. switch (ep_parse_struct_var(ep, &var)) {
  230. case PARSE_UNEXPECTED_CONTINUE:
  231. cf_adderror_syntax_error(&ep->cfp);
  232. /* Falls through. */
  233. case PARSE_CONTINUE:
  234. ep_var_free(&var);
  235. continue;
  236. case PARSE_UNEXPECTED_BREAK:
  237. cf_adderror_syntax_error(&ep->cfp);
  238. /* Falls through. */
  239. case PARSE_BREAK:
  240. ep_var_free(&var);
  241. do_break = true;
  242. break;
  243. case PARSE_EOF:
  244. ep_var_free(&var);
  245. goto error;
  246. }
  247. if (do_break)
  248. break;
  249. da_push_back(eps.vars, &var);
  250. }
  251. if (cf_next_token_should_be(&ep->cfp, ";", NULL, NULL) != PARSE_SUCCESS)
  252. goto error;
  253. da_push_back(ep->structs, &eps);
  254. return;
  255. error:
  256. ep_struct_free(&eps);
  257. }
  258. static inline int ep_parse_param_annotation_var(struct effect_parser *ep,
  259. struct ep_param *var)
  260. {
  261. int code;
  262. /* -------------------------------------- */
  263. /* variable type */
  264. if (!cf_next_valid_token(&ep->cfp))
  265. return PARSE_EOF;
  266. if (cf_token_is(&ep->cfp, ";"))
  267. return PARSE_CONTINUE;
  268. if (cf_token_is(&ep->cfp, ">"))
  269. return PARSE_BREAK;
  270. code = cf_token_is_type(&ep->cfp, CFTOKEN_NAME, "type name", ";");
  271. if (code != PARSE_SUCCESS)
  272. return code;
  273. bfree(var->type);
  274. cf_copy_token(&ep->cfp, &var->type);
  275. /* -------------------------------------- */
  276. /* variable name */
  277. if (!cf_next_valid_token(&ep->cfp))
  278. return PARSE_EOF;
  279. if (cf_token_is(&ep->cfp, ";")) {
  280. cf_adderror_expecting(&ep->cfp, "variable name");
  281. return PARSE_UNEXPECTED_CONTINUE;
  282. }
  283. if (cf_token_is(&ep->cfp, ">")) {
  284. cf_adderror_expecting(&ep->cfp, "variable name");
  285. return PARSE_UNEXPECTED_BREAK;
  286. }
  287. code = cf_token_is_type(&ep->cfp, CFTOKEN_NAME, "variable name", ";");
  288. if (code != PARSE_SUCCESS)
  289. return code;
  290. bfree(var->name);
  291. cf_copy_token(&ep->cfp, &var->name);
  292. /* -------------------------------------- */
  293. /* variable mapping if any (POSITION, TEXCOORD, etc) */
  294. if (!cf_next_valid_token(&ep->cfp))
  295. return PARSE_EOF;
  296. if (cf_token_is(&ep->cfp, ":")) {
  297. cf_adderror_expecting(&ep->cfp, "= or ;");
  298. return PARSE_UNEXPECTED_BREAK;
  299. } else if (cf_token_is(&ep->cfp, ">")) {
  300. cf_adderror_expecting(&ep->cfp, "= or ;");
  301. return PARSE_UNEXPECTED_BREAK;
  302. } else if (cf_token_is(&ep->cfp, "=")) {
  303. if (!ep_parse_param_assign(ep, var)) {
  304. cf_adderror_expecting(&ep->cfp, "assignment value");
  305. return PARSE_UNEXPECTED_BREAK;
  306. }
  307. }
  308. /* -------------------------------------- */
  309. if (!cf_token_is(&ep->cfp, ";")) {
  310. if (!cf_go_to_valid_token(&ep->cfp, ";", ">")) {
  311. cf_adderror_expecting(&ep->cfp, "; or >");
  312. return PARSE_EOF;
  313. }
  314. return PARSE_CONTINUE;
  315. }
  316. return PARSE_SUCCESS;
  317. }
  318. static int ep_parse_annotations(struct effect_parser *ep,
  319. ep_param_array_t *annotations)
  320. {
  321. if (!cf_token_is(&ep->cfp, "<")) {
  322. cf_adderror_expecting(&ep->cfp, "<");
  323. goto error;
  324. }
  325. /* get annotation variables */
  326. while (true) {
  327. bool do_break = false;
  328. struct ep_param var;
  329. ep_param_init(&var, bstrdup(""), bstrdup(""), false, false,
  330. false);
  331. switch (ep_parse_param_annotation_var(ep, &var)) {
  332. case PARSE_UNEXPECTED_CONTINUE:
  333. cf_adderror_syntax_error(&ep->cfp);
  334. /* Falls through. */
  335. case PARSE_CONTINUE:
  336. ep_param_free(&var);
  337. continue;
  338. case PARSE_UNEXPECTED_BREAK:
  339. cf_adderror_syntax_error(&ep->cfp);
  340. /* Falls through. */
  341. case PARSE_BREAK:
  342. ep_param_free(&var);
  343. do_break = true;
  344. break;
  345. case PARSE_EOF:
  346. ep_param_free(&var);
  347. goto error;
  348. }
  349. if (do_break)
  350. break;
  351. da_push_back(*annotations, &var);
  352. }
  353. if (!cf_token_is(&ep->cfp, ">")) {
  354. cf_adderror_expecting(&ep->cfp, ">");
  355. goto error;
  356. }
  357. if (!cf_next_valid_token(&ep->cfp))
  358. goto error;
  359. return true;
  360. error:
  361. return false;
  362. }
  363. static int ep_parse_param_annotations(struct effect_parser *ep,
  364. struct ep_param *param)
  365. {
  366. return ep_parse_annotations(ep, &param->annotations);
  367. }
  368. static inline int ep_parse_pass_command_call(struct effect_parser *ep,
  369. cf_token_array_t *call)
  370. {
  371. struct cf_token end_token;
  372. cf_token_clear(&end_token);
  373. while (!cf_token_is(&ep->cfp, ";")) {
  374. if (cf_token_is(&ep->cfp, "}")) {
  375. cf_adderror_expecting(&ep->cfp, ";");
  376. return PARSE_CONTINUE;
  377. }
  378. da_push_back(*call, ep->cfp.cur_token);
  379. if (!cf_next_valid_token(&ep->cfp))
  380. return PARSE_EOF;
  381. }
  382. da_push_back(*call, ep->cfp.cur_token);
  383. da_push_back(*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. cf_token_array_t *call;
  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;
  394. } else if (cf_token_is(&ep->cfp, "pixel_shader") ||
  395. cf_token_is(&ep->cfp, "pixel_program")) {
  396. call = &pass->fragment_program;
  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, (uint8_t *)&f,
  809. sizeof(float));
  810. } else {
  811. long l = strtol(ep->cfp.cur_token->str.array, NULL, 10);
  812. if (is_negative)
  813. l = -l;
  814. da_push_back_array(param->default_val, (uint8_t *)&l,
  815. sizeof(long));
  816. }
  817. return PARSE_SUCCESS;
  818. }
  819. static inline int ep_parse_param_assign_bool(struct effect_parser *ep,
  820. struct ep_param *param)
  821. {
  822. if (!cf_next_valid_token(&ep->cfp))
  823. return PARSE_EOF;
  824. if (cf_token_is(&ep->cfp, "true")) {
  825. long l = 1;
  826. da_push_back_array(param->default_val, (uint8_t *)&l,
  827. sizeof(long));
  828. return PARSE_SUCCESS;
  829. } else if (cf_token_is(&ep->cfp, "false")) {
  830. long l = 0;
  831. da_push_back_array(param->default_val, (uint8_t *)&l,
  832. sizeof(long));
  833. return PARSE_SUCCESS;
  834. }
  835. cf_adderror_expecting(&ep->cfp, "true or false");
  836. return PARSE_EOF;
  837. }
  838. /*
  839. * parses assignment for float1, float2, float3, float4, int1, int2, int3, int4,
  840. * and any combination for float3x3, float4x4, int3x3, int4x4, etc
  841. */
  842. static inline int ep_parse_param_assign_intfloat_array(struct effect_parser *ep,
  843. struct ep_param *param,
  844. bool is_float)
  845. {
  846. const char *intfloat_type = param->type + (is_float ? 5 : 3);
  847. int intfloat_count = 0, code, i;
  848. /* -------------------------------------------- */
  849. if (intfloat_type[0] < '1' || intfloat_type[0] > '4')
  850. cf_adderror(&ep->cfp, "Invalid row count", LEX_ERROR, NULL,
  851. NULL, NULL);
  852. intfloat_count = intfloat_type[0] - '0';
  853. if (intfloat_type[1] == 'x') {
  854. if (intfloat_type[2] < '1' || intfloat_type[2] > '4')
  855. cf_adderror(&ep->cfp, "Invalid column count", LEX_ERROR,
  856. NULL, NULL, NULL);
  857. intfloat_count *= intfloat_type[2] - '0';
  858. }
  859. /* -------------------------------------------- */
  860. code = cf_next_token_should_be(&ep->cfp, "{", ";", NULL);
  861. if (code != PARSE_SUCCESS)
  862. return code;
  863. for (i = 0; i < intfloat_count; i++) {
  864. char *next = ((i + 1) < intfloat_count) ? "," : "}";
  865. code = ep_parse_param_assign_intfloat(ep, param, is_float);
  866. if (code != PARSE_SUCCESS)
  867. return code;
  868. code = cf_next_token_should_be(&ep->cfp, next, ";", NULL);
  869. if (code != PARSE_SUCCESS)
  870. return code;
  871. }
  872. return PARSE_SUCCESS;
  873. }
  874. static int ep_parse_param_assignment_val(struct effect_parser *ep,
  875. struct ep_param *param)
  876. {
  877. if (param->is_texture)
  878. return ep_parse_param_assign_texture(ep, param);
  879. else if (strcmp(param->type, "int") == 0)
  880. return ep_parse_param_assign_intfloat(ep, param, false);
  881. else if (strcmp(param->type, "float") == 0)
  882. return ep_parse_param_assign_intfloat(ep, param, true);
  883. else if (astrcmp_n(param->type, "int", 3) == 0)
  884. return ep_parse_param_assign_intfloat_array(ep, param, false);
  885. else if (astrcmp_n(param->type, "float", 5) == 0)
  886. return ep_parse_param_assign_intfloat_array(ep, param, true);
  887. else if (astrcmp_n(param->type, "string", 6) == 0)
  888. return ep_parse_param_assign_string(ep, param);
  889. else if (strcmp(param->type, "bool") == 0)
  890. return ep_parse_param_assign_bool(ep, param);
  891. cf_adderror(&ep->cfp, "Invalid type '$1' used for assignment",
  892. LEX_ERROR, param->type, NULL, NULL);
  893. return PARSE_CONTINUE;
  894. }
  895. static inline bool ep_parse_param_assign(struct effect_parser *ep,
  896. struct ep_param *param)
  897. {
  898. if (ep_parse_param_assignment_val(ep, param) != PARSE_SUCCESS)
  899. return false;
  900. if (!cf_next_valid_token(&ep->cfp))
  901. return false;
  902. return true;
  903. }
  904. /* static bool ep_parse_param_property(struct effect_parser *ep,
  905. struct ep_param *param)
  906. {
  907. } */
  908. static void ep_parse_param(struct effect_parser *ep, char *type, char *name,
  909. bool is_property, bool is_const, bool is_uniform)
  910. {
  911. struct ep_param param;
  912. ep_param_init(&param, type, name, is_property, is_const, is_uniform);
  913. if (cf_token_is(&ep->cfp, ";"))
  914. goto complete;
  915. if (cf_token_is(&ep->cfp, "[") && !ep_parse_param_array(ep, &param))
  916. goto error;
  917. if (cf_token_is(&ep->cfp, "<") &&
  918. !ep_parse_param_annotations(ep, &param))
  919. goto error;
  920. if (cf_token_is(&ep->cfp, "=") && !ep_parse_param_assign(ep, &param))
  921. goto error;
  922. /*
  923. if (cf_token_is(&ep->cfp, "<") && !ep_parse_param_property(ep, &param))
  924. goto error; */
  925. if (!cf_token_is(&ep->cfp, ";"))
  926. goto error;
  927. complete:
  928. da_push_back(ep->params, &param);
  929. return;
  930. error:
  931. ep_param_free(&param);
  932. }
  933. static bool ep_get_var_specifiers(struct effect_parser *ep, bool *is_property,
  934. bool *is_const, bool *is_uniform)
  935. {
  936. while (true) {
  937. int code;
  938. code = ep_check_for_keyword(ep, "property", is_property);
  939. if (code == PARSE_EOF)
  940. return false;
  941. else if (code == PARSE_CONTINUE)
  942. continue;
  943. code = ep_check_for_keyword(ep, "const", is_const);
  944. if (code == PARSE_EOF)
  945. return false;
  946. else if (code == PARSE_CONTINUE)
  947. continue;
  948. code = ep_check_for_keyword(ep, "uniform", is_uniform);
  949. if (code == PARSE_EOF)
  950. return false;
  951. else if (code == PARSE_CONTINUE)
  952. continue;
  953. break;
  954. }
  955. return true;
  956. }
  957. static inline void report_invalid_func_keyword(struct effect_parser *ep,
  958. const char *name, bool val)
  959. {
  960. if (val)
  961. cf_adderror(&ep->cfp,
  962. "'$1' keyword cannot be used with a "
  963. "function",
  964. LEX_ERROR, name, NULL, NULL);
  965. }
  966. static void ep_parse_other(struct effect_parser *ep)
  967. {
  968. bool is_property = false, is_const = false, is_uniform = false;
  969. char *type = NULL, *name = NULL;
  970. if (!ep_get_var_specifiers(ep, &is_property, &is_const, &is_uniform))
  971. goto error;
  972. if (cf_get_name(&ep->cfp, &type, "type", ";") != PARSE_SUCCESS)
  973. goto error;
  974. if (cf_next_name(&ep->cfp, &name, "name", ";") != PARSE_SUCCESS)
  975. goto error;
  976. if (!cf_next_valid_token(&ep->cfp))
  977. goto error;
  978. if (cf_token_is(&ep->cfp, "(")) {
  979. report_invalid_func_keyword(ep, "property", is_property);
  980. report_invalid_func_keyword(ep, "const", is_const);
  981. report_invalid_func_keyword(ep, "uniform", is_uniform);
  982. ep_parse_function(ep, type, name);
  983. return;
  984. } else {
  985. ep_parse_param(ep, type, name, is_property, is_const,
  986. is_uniform);
  987. return;
  988. }
  989. error:
  990. bfree(type);
  991. bfree(name);
  992. }
  993. static bool ep_compile(struct effect_parser *ep);
  994. extern const char *gs_preprocessor_name(void);
  995. #if defined(_DEBUG) && defined(_DEBUG_SHADERS)
  996. static void debug_get_default_value(struct gs_effect_param *param, char *buffer,
  997. unsigned long long buf_size)
  998. {
  999. if (param->default_val.num == 0) {
  1000. snprintf(buffer, buf_size, "(null)");
  1001. return;
  1002. }
  1003. switch (param->type) {
  1004. case GS_SHADER_PARAM_STRING:
  1005. snprintf(buffer, buf_size, "'%.*s'", param->default_val.num,
  1006. param->default_val.array);
  1007. break;
  1008. case GS_SHADER_PARAM_INT:
  1009. snprintf(buffer, buf_size, "%ld",
  1010. *(int *)(param->default_val.array + 0));
  1011. break;
  1012. case GS_SHADER_PARAM_INT2:
  1013. snprintf(buffer, buf_size, "%ld,%ld",
  1014. *(int *)(param->default_val.array + 0),
  1015. *(int *)(param->default_val.array + 4));
  1016. break;
  1017. case GS_SHADER_PARAM_INT3:
  1018. snprintf(buffer, buf_size, "%ld,%ld,%ld",
  1019. *(int *)(param->default_val.array + 0),
  1020. *(int *)(param->default_val.array + 4),
  1021. *(int *)(param->default_val.array + 8));
  1022. break;
  1023. case GS_SHADER_PARAM_INT4:
  1024. snprintf(buffer, buf_size, "%ld,%ld,%ld,%ld",
  1025. *(int *)(param->default_val.array + 0),
  1026. *(int *)(param->default_val.array + 4),
  1027. *(int *)(param->default_val.array + 8),
  1028. *(int *)(param->default_val.array + 12));
  1029. break;
  1030. case GS_SHADER_PARAM_FLOAT:
  1031. snprintf(buffer, buf_size, "%e",
  1032. *(float *)(param->default_val.array + 0));
  1033. break;
  1034. case GS_SHADER_PARAM_VEC2:
  1035. snprintf(buffer, buf_size, "%e,%e",
  1036. *(float *)(param->default_val.array + 0),
  1037. *(float *)(param->default_val.array + 4));
  1038. break;
  1039. case GS_SHADER_PARAM_VEC3:
  1040. snprintf(buffer, buf_size, "%e,%e,%e",
  1041. *(float *)(param->default_val.array + 0),
  1042. *(float *)(param->default_val.array + 4),
  1043. *(float *)(param->default_val.array + 8));
  1044. break;
  1045. case GS_SHADER_PARAM_VEC4:
  1046. snprintf(buffer, buf_size, "%e,%e,%e,%e",
  1047. *(float *)(param->default_val.array + 0),
  1048. *(float *)(param->default_val.array + 4),
  1049. *(float *)(param->default_val.array + 8),
  1050. *(float *)(param->default_val.array + 12));
  1051. break;
  1052. case GS_SHADER_PARAM_MATRIX4X4:
  1053. snprintf(buffer, buf_size,
  1054. "[[%e,%e,%e,%e],[%e,%e,%e,%e],"
  1055. "[%e,%e,%e,%e],[%e,%e,%e,%e]]",
  1056. *(float *)(param->default_val.array + 0),
  1057. *(float *)(param->default_val.array + 4),
  1058. *(float *)(param->default_val.array + 8),
  1059. *(float *)(param->default_val.array + 12),
  1060. *(float *)(param->default_val.array + 16),
  1061. *(float *)(param->default_val.array + 20),
  1062. *(float *)(param->default_val.array + 24),
  1063. *(float *)(param->default_val.array + 28),
  1064. *(float *)(param->default_val.array + 32),
  1065. *(float *)(param->default_val.array + 36),
  1066. *(float *)(param->default_val.array + 40),
  1067. *(float *)(param->default_val.array + 44),
  1068. *(float *)(param->default_val.array + 48),
  1069. *(float *)(param->default_val.array + 52),
  1070. *(float *)(param->default_val.array + 56),
  1071. *(float *)(param->default_val.array + 60));
  1072. break;
  1073. case GS_SHADER_PARAM_BOOL:
  1074. snprintf(buffer, buf_size, "%s",
  1075. (*param->default_val.array) != 0 ? "true\0"
  1076. : "false\0");
  1077. break;
  1078. case GS_SHADER_PARAM_UNKNOWN:
  1079. case GS_SHADER_PARAM_TEXTURE:
  1080. snprintf(buffer, buf_size, "<unknown>");
  1081. break;
  1082. }
  1083. }
  1084. static void debug_param(struct gs_effect_param *param,
  1085. struct ep_param *param_in, unsigned long long idx,
  1086. const char *offset)
  1087. {
  1088. char _debug_type[4096];
  1089. switch (param->type) {
  1090. case GS_SHADER_PARAM_STRING:
  1091. snprintf(_debug_type, sizeof(_debug_type), "string");
  1092. break;
  1093. case GS_SHADER_PARAM_INT:
  1094. snprintf(_debug_type, sizeof(_debug_type), "int");
  1095. break;
  1096. case GS_SHADER_PARAM_INT2:
  1097. snprintf(_debug_type, sizeof(_debug_type), "int2");
  1098. break;
  1099. case GS_SHADER_PARAM_INT3:
  1100. snprintf(_debug_type, sizeof(_debug_type), "int3");
  1101. break;
  1102. case GS_SHADER_PARAM_INT4:
  1103. snprintf(_debug_type, sizeof(_debug_type), "int4");
  1104. break;
  1105. case GS_SHADER_PARAM_FLOAT:
  1106. snprintf(_debug_type, sizeof(_debug_type), "float");
  1107. break;
  1108. case GS_SHADER_PARAM_VEC2:
  1109. snprintf(_debug_type, sizeof(_debug_type), "float2");
  1110. break;
  1111. case GS_SHADER_PARAM_VEC3:
  1112. snprintf(_debug_type, sizeof(_debug_type), "float3");
  1113. break;
  1114. case GS_SHADER_PARAM_VEC4:
  1115. snprintf(_debug_type, sizeof(_debug_type), "float4");
  1116. break;
  1117. case GS_SHADER_PARAM_MATRIX4X4:
  1118. snprintf(_debug_type, sizeof(_debug_type), "float4x4");
  1119. break;
  1120. case GS_SHADER_PARAM_BOOL:
  1121. snprintf(_debug_type, sizeof(_debug_type), "bool");
  1122. break;
  1123. case GS_SHADER_PARAM_UNKNOWN:
  1124. snprintf(_debug_type, sizeof(_debug_type), "unknown");
  1125. break;
  1126. case GS_SHADER_PARAM_TEXTURE:
  1127. snprintf(_debug_type, sizeof(_debug_type), "texture");
  1128. break;
  1129. }
  1130. char _debug_buf[4096];
  1131. debug_get_default_value(param, _debug_buf, sizeof(_debug_buf));
  1132. if (param->annotations.num > 0) {
  1133. blog(LOG_DEBUG,
  1134. "%s[%4lld] %.*s '%s' with value %.*s and %lld annotations:",
  1135. offset, idx, sizeof(_debug_type), _debug_type, param->name,
  1136. sizeof(_debug_buf), _debug_buf, param->annotations.num);
  1137. } else {
  1138. blog(LOG_DEBUG, "%s[%4lld] %.*s '%s' with value %.*s.", offset,
  1139. idx, sizeof(_debug_type), _debug_type, param->name,
  1140. sizeof(_debug_buf), _debug_buf);
  1141. }
  1142. }
  1143. static void debug_param_annotation(struct gs_effect_param *param,
  1144. struct ep_param *param_in,
  1145. unsigned long long idx, const char *offset)
  1146. {
  1147. char _debug_buf[4096];
  1148. debug_get_default_value(param, _debug_buf, sizeof(_debug_buf));
  1149. blog(LOG_DEBUG, "%s[%4lld] %s '%s' with value %.*s", offset, idx,
  1150. param_in->type, param->name, sizeof(_debug_buf), _debug_buf);
  1151. }
  1152. static void debug_print_string(const char *offset, const char *str)
  1153. {
  1154. // Bypass 4096 limit in def_log_handler.
  1155. char const *begin = str;
  1156. unsigned long long line = 1;
  1157. for (char const *here = begin; here[0] != '\0'; here++) {
  1158. char const *str = begin;
  1159. unsigned long long len = here - begin;
  1160. bool is_line = false;
  1161. if (here[0] == '\r') {
  1162. is_line = true;
  1163. if (here[1] == '\n') {
  1164. here += 1;
  1165. }
  1166. begin = here + 1;
  1167. } else if (here[0] == '\n') {
  1168. is_line = true;
  1169. begin = here + 1;
  1170. }
  1171. if (is_line) {
  1172. blog(LOG_DEBUG, "\t\t\t\t[%4lld] %.*s", line, len, str);
  1173. line++;
  1174. }
  1175. }
  1176. if (begin[0] != '\0') {
  1177. // Final line was not written.
  1178. blog(LOG_DEBUG, "\t\t\t\t[%4lld] %*s", line, strlen(begin),
  1179. begin);
  1180. }
  1181. }
  1182. #endif
  1183. bool ep_parse(struct effect_parser *ep, gs_effect_t *effect,
  1184. const char *effect_string, const char *file)
  1185. {
  1186. bool success;
  1187. const char *graphics_preprocessor = gs_preprocessor_name();
  1188. if (graphics_preprocessor) {
  1189. struct cf_def def;
  1190. cf_def_init(&def);
  1191. def.name.str.array = graphics_preprocessor;
  1192. def.name.str.len = strlen(graphics_preprocessor);
  1193. strref_copy(&def.name.unmerged_str, &def.name.str);
  1194. cf_preprocessor_add_def(&ep->cfp.pp, &def);
  1195. }
  1196. ep->effect = effect;
  1197. if (!cf_parser_parse(&ep->cfp, effect_string, file))
  1198. return false;
  1199. while (ep->cfp.cur_token && ep->cfp.cur_token->type != CFTOKEN_NONE) {
  1200. if (cf_token_is(&ep->cfp, ";") ||
  1201. is_whitespace(*ep->cfp.cur_token->str.array)) {
  1202. /* do nothing */
  1203. ep->cfp.cur_token++;
  1204. } else if (cf_token_is(&ep->cfp, "struct")) {
  1205. ep_parse_struct(ep);
  1206. } else if (cf_token_is(&ep->cfp, "technique")) {
  1207. ep_parse_technique(ep);
  1208. } else if (cf_token_is(&ep->cfp, "sampler_state")) {
  1209. ep_parse_sampler_state(ep);
  1210. } else if (cf_token_is(&ep->cfp, "{")) {
  1211. /* add error and pass braces */
  1212. cf_adderror(&ep->cfp, "Unexpected code segment",
  1213. LEX_ERROR, NULL, NULL, NULL);
  1214. cf_pass_pair(&ep->cfp, '{', '}');
  1215. } else {
  1216. /* parameters and functions */
  1217. ep_parse_other(ep);
  1218. }
  1219. }
  1220. #if defined(_DEBUG) && defined(_DEBUG_SHADERS)
  1221. blog(LOG_DEBUG,
  1222. "================================================================================");
  1223. blog(LOG_DEBUG, "Effect Parser reformatted shader '%s' to:", file);
  1224. debug_print_string("\t", ep->cfp.lex.reformatted);
  1225. #endif
  1226. success = !error_data_has_errors(&ep->cfp.error_list);
  1227. if (success)
  1228. success = ep_compile(ep);
  1229. #if defined(_DEBUG) && defined(_DEBUG_SHADERS)
  1230. blog(LOG_DEBUG,
  1231. "================================================================================");
  1232. #endif
  1233. return success;
  1234. }
  1235. /* ------------------------------------------------------------------------- */
  1236. static inline void ep_write_param(struct dstr *shader, struct ep_param *param,
  1237. dstr_array_t *used_params)
  1238. {
  1239. if (param->written)
  1240. return;
  1241. if (param->is_const) {
  1242. dstr_cat(shader, "const ");
  1243. } else if (param->is_uniform) {
  1244. struct dstr new;
  1245. dstr_init_copy(&new, param->name);
  1246. da_push_back(*used_params, &new);
  1247. dstr_cat(shader, "uniform ");
  1248. }
  1249. dstr_cat(shader, param->type);
  1250. dstr_cat(shader, " ");
  1251. dstr_cat(shader, param->name);
  1252. if (param->array_count)
  1253. dstr_catf(shader, "[%u]", param->array_count);
  1254. dstr_cat(shader, ";\n");
  1255. param->written = true;
  1256. }
  1257. static inline void ep_write_func_param_deps(struct effect_parser *ep,
  1258. struct dstr *shader,
  1259. struct ep_func *func,
  1260. dstr_array_t *used_params)
  1261. {
  1262. size_t i;
  1263. for (i = 0; i < func->param_deps.num; i++) {
  1264. const char *name = func->param_deps.array[i];
  1265. struct ep_param *param = ep_getparam(ep, name);
  1266. ep_write_param(shader, param, used_params);
  1267. }
  1268. if (func->param_deps.num)
  1269. dstr_cat(shader, "\n\n");
  1270. }
  1271. static void ep_write_sampler(struct dstr *shader, struct ep_sampler *sampler)
  1272. {
  1273. size_t i;
  1274. if (sampler->written)
  1275. return;
  1276. dstr_cat(shader, "sampler_state ");
  1277. dstr_cat(shader, sampler->name);
  1278. dstr_cat(shader, " {");
  1279. for (i = 0; i < sampler->values.num; i++) {
  1280. dstr_cat(shader, "\n\t");
  1281. dstr_cat(shader, sampler->states.array[i]);
  1282. dstr_cat(shader, " = ");
  1283. dstr_cat(shader, sampler->values.array[i]);
  1284. dstr_cat(shader, ";\n");
  1285. }
  1286. dstr_cat(shader, "\n};\n");
  1287. sampler->written = true;
  1288. }
  1289. static inline void ep_write_func_sampler_deps(struct effect_parser *ep,
  1290. struct dstr *shader,
  1291. struct ep_func *func)
  1292. {
  1293. size_t i;
  1294. for (i = 0; i < func->sampler_deps.num; i++) {
  1295. const char *name = func->sampler_deps.array[i];
  1296. struct ep_sampler *sampler = ep_getsampler(ep, name);
  1297. ep_write_sampler(shader, sampler);
  1298. dstr_cat(shader, "\n");
  1299. }
  1300. }
  1301. static inline void ep_write_var(struct dstr *shader, struct ep_var *var)
  1302. {
  1303. if (var->var_type == EP_VAR_INOUT)
  1304. dstr_cat(shader, "inout ");
  1305. else if (var->var_type == EP_VAR_OUT)
  1306. dstr_cat(shader, "out ");
  1307. else if (var->var_type == EP_VAR_UNIFORM)
  1308. dstr_cat(shader, "uniform ");
  1309. // The "in" input modifier is implied by default, so leave it blank
  1310. // in that case.
  1311. dstr_cat(shader, var->type);
  1312. dstr_cat(shader, " ");
  1313. dstr_cat(shader, var->name);
  1314. if (var->mapping) {
  1315. dstr_cat(shader, " : ");
  1316. dstr_cat(shader, var->mapping);
  1317. }
  1318. }
  1319. static void ep_write_struct(struct dstr *shader, struct ep_struct *st)
  1320. {
  1321. size_t i;
  1322. if (st->written)
  1323. return;
  1324. dstr_cat(shader, "struct ");
  1325. dstr_cat(shader, st->name);
  1326. dstr_cat(shader, " {");
  1327. for (i = 0; i < st->vars.num; i++) {
  1328. dstr_cat(shader, "\n\t");
  1329. ep_write_var(shader, st->vars.array + i);
  1330. dstr_cat(shader, ";");
  1331. }
  1332. dstr_cat(shader, "\n};\n");
  1333. st->written = true;
  1334. }
  1335. static inline void ep_write_func_struct_deps(struct effect_parser *ep,
  1336. struct dstr *shader,
  1337. struct ep_func *func)
  1338. {
  1339. size_t i;
  1340. for (i = 0; i < func->struct_deps.num; i++) {
  1341. const char *name = func->struct_deps.array[i];
  1342. struct ep_struct *st = ep_getstruct(ep, name);
  1343. if (!st->written) {
  1344. ep_write_struct(shader, st);
  1345. dstr_cat(shader, "\n");
  1346. st->written = true;
  1347. }
  1348. }
  1349. }
  1350. static void ep_write_func(struct effect_parser *ep, struct dstr *shader,
  1351. struct ep_func *func, dstr_array_t *used_params);
  1352. static inline void ep_write_func_func_deps(struct effect_parser *ep,
  1353. struct dstr *shader,
  1354. struct ep_func *func,
  1355. dstr_array_t *used_params)
  1356. {
  1357. size_t i;
  1358. for (i = 0; i < func->func_deps.num; i++) {
  1359. const char *name = func->func_deps.array[i];
  1360. struct ep_func *func_dep = ep_getfunc(ep, name);
  1361. if (!func_dep->written) {
  1362. ep_write_func(ep, shader, func_dep, used_params);
  1363. dstr_cat(shader, "\n\n");
  1364. }
  1365. }
  1366. }
  1367. static void ep_write_func(struct effect_parser *ep, struct dstr *shader,
  1368. struct ep_func *func, dstr_array_t *used_params)
  1369. {
  1370. size_t i;
  1371. func->written = true;
  1372. ep_write_func_param_deps(ep, shader, func, used_params);
  1373. ep_write_func_sampler_deps(ep, shader, func);
  1374. ep_write_func_struct_deps(ep, shader, func);
  1375. ep_write_func_func_deps(ep, shader, func, used_params);
  1376. /* ------------------------------------ */
  1377. dstr_cat(shader, func->ret_type);
  1378. dstr_cat(shader, " ");
  1379. dstr_cat(shader, func->name);
  1380. dstr_cat(shader, "(");
  1381. for (i = 0; i < func->param_vars.num; i++) {
  1382. struct ep_var *var = func->param_vars.array + i;
  1383. if (i)
  1384. dstr_cat(shader, ", ");
  1385. ep_write_var(shader, var);
  1386. }
  1387. dstr_cat(shader, ")\n");
  1388. dstr_cat_dstr(shader, &func->contents);
  1389. dstr_cat(shader, "\n");
  1390. }
  1391. /* writes mapped vars used by the call as parameters for main */
  1392. static void ep_write_main_params(struct effect_parser *ep, struct dstr *shader,
  1393. struct dstr *param_str, struct ep_func *func)
  1394. {
  1395. size_t i;
  1396. bool empty_params = dstr_is_empty(param_str);
  1397. for (i = 0; i < func->param_vars.num; i++) {
  1398. struct ep_var *var = func->param_vars.array + i;
  1399. struct ep_struct *st = NULL;
  1400. bool mapped = (var->mapping != NULL);
  1401. if (!mapped) {
  1402. st = ep_getstruct(ep, var->type);
  1403. if (st)
  1404. mapped = ep_struct_mapped(st);
  1405. }
  1406. if (mapped) {
  1407. dstr_cat(shader, var->type);
  1408. dstr_cat(shader, " ");
  1409. dstr_cat(shader, var->name);
  1410. if (!st) {
  1411. dstr_cat(shader, " : ");
  1412. dstr_cat(shader, var->mapping);
  1413. }
  1414. if (!dstr_is_empty(param_str))
  1415. dstr_cat(param_str, ", ");
  1416. dstr_cat(param_str, var->name);
  1417. }
  1418. }
  1419. if (!empty_params)
  1420. dstr_cat(param_str, ", ");
  1421. }
  1422. static void ep_write_main(struct effect_parser *ep, struct dstr *shader,
  1423. struct ep_func *func, struct dstr *call_str)
  1424. {
  1425. struct dstr param_str;
  1426. struct dstr adjusted_call;
  1427. dstr_init(&param_str);
  1428. dstr_init_copy_dstr(&adjusted_call, call_str);
  1429. dstr_cat(shader, "\n");
  1430. dstr_cat(shader, func->ret_type);
  1431. dstr_cat(shader, " main(");
  1432. ep_write_main_params(ep, shader, &param_str, func);
  1433. dstr_cat(shader, ")");
  1434. if (func->mapping) {
  1435. dstr_cat(shader, " : ");
  1436. dstr_cat(shader, func->mapping);
  1437. }
  1438. dstr_cat(shader, "\n{\n\treturn ");
  1439. dstr_cat_dstr(shader, &adjusted_call);
  1440. dstr_cat(shader, "\n}\n");
  1441. dstr_free(&adjusted_call);
  1442. dstr_free(&param_str);
  1443. }
  1444. static inline void ep_reset_written(struct effect_parser *ep)
  1445. {
  1446. size_t i;
  1447. for (i = 0; i < ep->params.num; i++)
  1448. ep->params.array[i].written = false;
  1449. for (i = 0; i < ep->structs.num; i++)
  1450. ep->structs.array[i].written = false;
  1451. for (i = 0; i < ep->funcs.num; i++)
  1452. ep->funcs.array[i].written = false;
  1453. for (i = 0; i < ep->samplers.num; i++)
  1454. ep->samplers.array[i].written = false;
  1455. }
  1456. static void ep_makeshaderstring(struct effect_parser *ep, struct dstr *shader,
  1457. cf_token_array_t *shader_call,
  1458. dstr_array_t *used_params)
  1459. {
  1460. struct cf_token *token = shader_call->array;
  1461. struct cf_token *func_name;
  1462. struct ep_func *func;
  1463. struct dstr call_str;
  1464. dstr_init(&call_str);
  1465. if (!token)
  1466. return;
  1467. while (token->type != CFTOKEN_NONE && is_whitespace(*token->str.array))
  1468. token++;
  1469. if (token->type == CFTOKEN_NONE || strref_cmp(&token->str, "NULL") == 0)
  1470. return;
  1471. func_name = token;
  1472. while (token->type != CFTOKEN_NONE) {
  1473. struct ep_param *param = ep_getparam_strref(ep, &token->str);
  1474. if (param)
  1475. ep_write_param(shader, param, used_params);
  1476. dstr_cat_strref(&call_str, &token->str);
  1477. token++;
  1478. }
  1479. func = ep_getfunc_strref(ep, &func_name->str);
  1480. if (!func)
  1481. return;
  1482. ep_write_func(ep, shader, func, used_params);
  1483. ep_write_main(ep, shader, func, &call_str);
  1484. dstr_free(&call_str);
  1485. ep_reset_written(ep);
  1486. }
  1487. static void ep_compile_annotations(ep_param_array_t *ep_annotations,
  1488. gs_effect_param_array_t *gsp_annotations,
  1489. struct effect_parser *ep)
  1490. {
  1491. da_resize(*gsp_annotations, ep_annotations->num);
  1492. size_t i;
  1493. for (i = 0; i < ep_annotations->num; i++) {
  1494. struct gs_effect_param *param = gsp_annotations->array + i;
  1495. struct ep_param *param_in = ep_annotations->array + i;
  1496. param->name = bstrdup(param_in->name);
  1497. param->section = EFFECT_ANNOTATION;
  1498. param->effect = ep->effect;
  1499. da_move(param->default_val, param_in->default_val);
  1500. param->type = get_effect_param_type(param_in->type);
  1501. #if defined(_DEBUG) && defined(_DEBUG_SHADERS)
  1502. debug_param(param, param_in, i, "\t\t");
  1503. #endif
  1504. }
  1505. }
  1506. static void
  1507. ep_compile_param_annotations(struct ep_param *ep_param_input,
  1508. struct gs_effect_param *gs_effect_input,
  1509. struct effect_parser *ep)
  1510. {
  1511. ep_compile_annotations(&(ep_param_input->annotations),
  1512. &(gs_effect_input->annotations), ep);
  1513. }
  1514. static void ep_compile_param(struct effect_parser *ep, size_t idx)
  1515. {
  1516. struct gs_effect_param *param;
  1517. struct ep_param *param_in;
  1518. param = ep->effect->params.array + idx;
  1519. param_in = ep->params.array + idx;
  1520. param_in->param = param;
  1521. param->name = bstrdup(param_in->name);
  1522. param->section = EFFECT_PARAM;
  1523. param->effect = ep->effect;
  1524. da_move(param->default_val, param_in->default_val);
  1525. param->type = get_effect_param_type(param_in->type);
  1526. if (strcmp(param_in->name, "ViewProj") == 0)
  1527. ep->effect->view_proj = param;
  1528. else if (strcmp(param_in->name, "World") == 0)
  1529. ep->effect->world = param;
  1530. #if defined(_DEBUG) && defined(_DEBUG_SHADERS)
  1531. debug_param(param, param_in, idx, "\t");
  1532. #endif
  1533. ep_compile_param_annotations(param_in, param, ep);
  1534. }
  1535. static bool ep_compile_pass_shaderparams(struct effect_parser *ep,
  1536. pass_shaderparam_array_t *pass_params,
  1537. dstr_array_t *used_params,
  1538. gs_shader_t *shader)
  1539. {
  1540. size_t i;
  1541. da_resize(*pass_params, used_params->num);
  1542. for (i = 0; i < pass_params->num; i++) {
  1543. struct dstr *param_name = used_params->array + i;
  1544. struct pass_shaderparam *param = pass_params->array + i;
  1545. param->eparam = gs_effect_get_param_by_name(ep->effect,
  1546. param_name->array);
  1547. param->sparam =
  1548. gs_shader_get_param_by_name(shader, param_name->array);
  1549. #if defined(_DEBUG) && defined(_DEBUG_SHADERS)
  1550. debug_param(param->eparam, 0, i, "\t\t\t\t");
  1551. #endif
  1552. if (!param->sparam) {
  1553. blog(LOG_ERROR, "Effect shader parameter not found");
  1554. return false;
  1555. }
  1556. }
  1557. return true;
  1558. }
  1559. static inline bool ep_compile_pass_shader(struct effect_parser *ep,
  1560. struct gs_effect_technique *tech,
  1561. struct gs_effect_pass *pass,
  1562. struct ep_pass *pass_in,
  1563. size_t pass_idx,
  1564. enum gs_shader_type type)
  1565. {
  1566. struct dstr shader_str;
  1567. struct dstr location;
  1568. dstr_array_t used_params;
  1569. pass_shaderparam_array_t *pass_params = NULL;
  1570. gs_shader_t *shader = NULL;
  1571. bool success = true;
  1572. char *errors = NULL;
  1573. dstr_init(&shader_str);
  1574. da_init(used_params);
  1575. dstr_init(&location);
  1576. dstr_copy(&location, ep->cfp.lex.file);
  1577. if (type == GS_SHADER_VERTEX)
  1578. dstr_cat(&location, " (Vertex ");
  1579. else if (type == GS_SHADER_PIXEL)
  1580. dstr_cat(&location, " (Pixel ");
  1581. /*else if (type == SHADER_GEOMETRY)
  1582. dstr_cat(&location, " (Geometry ");*/
  1583. assert(pass_idx <= UINT_MAX);
  1584. dstr_catf(&location, "shader, technique %s, pass %u)", tech->name,
  1585. (unsigned)pass_idx);
  1586. if (type == GS_SHADER_VERTEX) {
  1587. ep_makeshaderstring(ep, &shader_str, &pass_in->vertex_program,
  1588. &used_params);
  1589. pass->vertshader = gs_vertexshader_create(
  1590. shader_str.array, location.array, &errors);
  1591. shader = pass->vertshader;
  1592. pass_params = &pass->vertshader_params;
  1593. } else if (type == GS_SHADER_PIXEL) {
  1594. ep_makeshaderstring(ep, &shader_str, &pass_in->fragment_program,
  1595. &used_params);
  1596. pass->pixelshader = gs_pixelshader_create(
  1597. shader_str.array, location.array, &errors);
  1598. shader = pass->pixelshader;
  1599. pass_params = &pass->pixelshader_params;
  1600. }
  1601. if (errors && strlen(errors)) {
  1602. cf_adderror(&ep->cfp, "Error creating shader: $1", LEX_ERROR,
  1603. errors, NULL, NULL);
  1604. }
  1605. bfree(errors);
  1606. #if defined(_DEBUG) && defined(_DEBUG_SHADERS)
  1607. blog(LOG_DEBUG, "\t\t\t%s Shader:",
  1608. type == GS_SHADER_VERTEX ? "Vertex" : "Fragment");
  1609. blog(LOG_DEBUG, "\t\t\tCode:");
  1610. debug_print_string("\t\t\t\t\t", shader_str.array);
  1611. blog(LOG_DEBUG, "\t\t\tParameters:");
  1612. #endif
  1613. if (shader)
  1614. success = ep_compile_pass_shaderparams(ep, pass_params,
  1615. &used_params, shader);
  1616. else
  1617. success = false;
  1618. dstr_free(&location);
  1619. dstr_array_free(used_params.array, used_params.num);
  1620. da_free(used_params);
  1621. dstr_free(&shader_str);
  1622. return success;
  1623. }
  1624. static bool ep_compile_pass(struct effect_parser *ep,
  1625. struct gs_effect_technique *tech,
  1626. struct ep_technique *tech_in, size_t idx)
  1627. {
  1628. struct gs_effect_pass *pass;
  1629. struct ep_pass *pass_in;
  1630. bool success = true;
  1631. pass = tech->passes.array + idx;
  1632. pass_in = tech_in->passes.array + idx;
  1633. pass->name = bstrdup(pass_in->name);
  1634. pass->section = EFFECT_PASS;
  1635. #if defined(_DEBUG) && defined(_DEBUG_SHADERS)
  1636. blog(LOG_DEBUG, "\t\t[%4lld] Pass '%s':", idx, pass->name);
  1637. #endif
  1638. if (!ep_compile_pass_shader(ep, tech, pass, pass_in, idx,
  1639. GS_SHADER_VERTEX)) {
  1640. success = false;
  1641. blog(LOG_ERROR, "Pass (%zu) <%s> missing vertex shader!", idx,
  1642. pass->name ? pass->name : "");
  1643. }
  1644. if (!ep_compile_pass_shader(ep, tech, pass, pass_in, idx,
  1645. GS_SHADER_PIXEL)) {
  1646. success = false;
  1647. blog(LOG_ERROR, "Pass (%zu) <%s> missing pixel shader!", idx,
  1648. pass->name ? pass->name : "");
  1649. }
  1650. return success;
  1651. }
  1652. static inline bool ep_compile_technique(struct effect_parser *ep, size_t idx)
  1653. {
  1654. struct gs_effect_technique *tech;
  1655. struct ep_technique *tech_in;
  1656. bool success = true;
  1657. size_t i;
  1658. tech = ep->effect->techniques.array + idx;
  1659. tech_in = ep->techniques.array + idx;
  1660. tech->name = bstrdup(tech_in->name);
  1661. tech->section = EFFECT_TECHNIQUE;
  1662. tech->effect = ep->effect;
  1663. da_resize(tech->passes, tech_in->passes.num);
  1664. #if defined(_DEBUG) && defined(_DEBUG_SHADERS)
  1665. blog(LOG_DEBUG, "\t[%4lld] Technique '%s' has %lld passes:", idx,
  1666. tech->name, tech->passes.num);
  1667. #endif
  1668. for (i = 0; i < tech->passes.num; i++) {
  1669. if (!ep_compile_pass(ep, tech, tech_in, i))
  1670. success = false;
  1671. }
  1672. return success;
  1673. }
  1674. static bool ep_compile(struct effect_parser *ep)
  1675. {
  1676. bool success = true;
  1677. size_t i;
  1678. assert(ep->effect);
  1679. da_resize(ep->effect->params, ep->params.num);
  1680. da_resize(ep->effect->techniques, ep->techniques.num);
  1681. #if defined(_DEBUG) && defined(_DEBUG_SHADERS)
  1682. blog(LOG_DEBUG, "Shader has %lld parameters:", ep->params.num);
  1683. #endif
  1684. for (i = 0; i < ep->params.num; i++)
  1685. ep_compile_param(ep, i);
  1686. #if defined(_DEBUG) && defined(_DEBUG_SHADERS)
  1687. blog(LOG_DEBUG, "Shader has %lld techniques:", ep->techniques.num);
  1688. #endif
  1689. for (i = 0; i < ep->techniques.num; i++) {
  1690. if (!ep_compile_technique(ep, i))
  1691. success = false;
  1692. }
  1693. return success;
  1694. }