effect-parser.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488
  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 "effect-parser.h"
  16. #include "effect.h"
  17. void ep_free(struct effect_parser *ep)
  18. {
  19. size_t i;
  20. for (i = 0; i < ep->params.num; i++)
  21. ep_param_free(ep->params.array+i);
  22. for (i = 0; i < ep->structs.num; i++)
  23. ep_struct_free(ep->structs.array+i);
  24. for (i = 0; i < ep->funcs.num; i++)
  25. ep_func_free(ep->funcs.array+i);
  26. for (i = 0; i < ep->samplers.num; i++)
  27. ep_sampler_free(ep->samplers.array+i);
  28. for (i = 0; i < ep->techniques.num; i++)
  29. ep_technique_free(ep->techniques.array+i);
  30. ep->cur_pass = NULL;
  31. cf_parser_free(&ep->cfp);
  32. da_free(ep->params);
  33. da_free(ep->structs);
  34. da_free(ep->funcs);
  35. da_free(ep->samplers);
  36. da_free(ep->techniques);
  37. }
  38. static inline struct ep_func *ep_getfunc(struct effect_parser *ep,
  39. const char *name)
  40. {
  41. size_t i;
  42. for (i = 0; i < ep->funcs.num; i++) {
  43. if (strcmp(name, ep->funcs.array[i].name) == 0)
  44. return ep->funcs.array+i;
  45. }
  46. return NULL;
  47. }
  48. static inline struct ep_struct *ep_getstruct(struct effect_parser *ep,
  49. const char *name)
  50. {
  51. size_t i;
  52. for (i = 0; i < ep->structs.num; i++) {
  53. if (strcmp(name, ep->structs.array[i].name) == 0)
  54. return ep->structs.array+i;
  55. }
  56. return NULL;
  57. }
  58. static inline struct ep_sampler *ep_getsampler(struct effect_parser *ep,
  59. const char *name)
  60. {
  61. size_t i;
  62. for (i = 0; i < ep->samplers.num; i++) {
  63. if (strcmp(name, ep->samplers.array[i].name) == 0)
  64. return ep->samplers.array+i;
  65. }
  66. return NULL;
  67. }
  68. static inline struct ep_param *ep_getparam(struct effect_parser *ep,
  69. const char *name)
  70. {
  71. size_t i;
  72. for (i = 0; i < ep->params.num; i++) {
  73. if (strcmp(name, ep->params.array[i].name) == 0)
  74. return ep->params.array+i;
  75. }
  76. return NULL;
  77. }
  78. static inline struct ep_func *ep_getfunc_strref(struct effect_parser *ep,
  79. const struct strref *ref)
  80. {
  81. size_t i;
  82. for (i = 0; i < ep->funcs.num; i++) {
  83. if (strref_cmp(ref, ep->funcs.array[i].name) == 0)
  84. return ep->funcs.array+i;
  85. }
  86. return NULL;
  87. }
  88. static inline struct ep_struct *ep_getstruct_strref(struct effect_parser *ep,
  89. const struct strref *ref)
  90. {
  91. size_t i;
  92. for (i = 0; i < ep->structs.num; i++) {
  93. if (strref_cmp(ref, ep->structs.array[i].name) == 0)
  94. return ep->structs.array+i;
  95. }
  96. return NULL;
  97. }
  98. static inline struct ep_sampler *ep_getsampler_strref(struct effect_parser *ep,
  99. const struct strref *ref)
  100. {
  101. size_t i;
  102. for (i = 0; i < ep->samplers.num; i++) {
  103. if (strref_cmp(ref, ep->samplers.array[i].name) == 0)
  104. return ep->samplers.array+i;
  105. }
  106. return NULL;
  107. }
  108. static inline struct ep_param *ep_getparam_strref(struct effect_parser *ep,
  109. const struct strref *ref)
  110. {
  111. size_t i;
  112. for (i = 0; i < ep->params.num; i++) {
  113. if (strref_cmp(ref, ep->params.array[i].name) == 0)
  114. return ep->params.array+i;
  115. }
  116. return NULL;
  117. }
  118. static inline int ep_parse_struct_var(struct effect_parser *ep,
  119. struct ep_var *var)
  120. {
  121. int code;
  122. /* -------------------------------------- */
  123. /* variable type */
  124. if (!cf_next_valid_token(&ep->cfp)) return PARSE_EOF;
  125. if (cf_token_is(&ep->cfp, ";")) return PARSE_CONTINUE;
  126. if (cf_token_is(&ep->cfp, "}")) return PARSE_BREAK;
  127. code = cf_token_is_type(&ep->cfp, CFTOKEN_NAME, "type name", ";");
  128. if (code != PARSE_SUCCESS)
  129. return code;
  130. cf_copy_token(&ep->cfp, &var->type);
  131. /* -------------------------------------- */
  132. /* variable name */
  133. if (!cf_next_valid_token(&ep->cfp)) return PARSE_EOF;
  134. if (cf_token_is(&ep->cfp, ";")) return PARSE_UNEXPECTED_CONTINUE;
  135. if (cf_token_is(&ep->cfp, "}")) return PARSE_UNEXPECTED_BREAK;
  136. code = cf_token_is_type(&ep->cfp, CFTOKEN_NAME, "variable name", ";");
  137. if (code != PARSE_SUCCESS)
  138. return code;
  139. cf_copy_token(&ep->cfp, &var->name);
  140. /* -------------------------------------- */
  141. /* variable mapping if any (POSITION, TEXCOORD, etc) */
  142. if (!cf_next_valid_token(&ep->cfp)) return PARSE_EOF;
  143. if (cf_token_is(&ep->cfp, ":")) {
  144. if (!cf_next_valid_token(&ep->cfp)) return PARSE_EOF;
  145. if (cf_token_is(&ep->cfp, ";"))
  146. return PARSE_UNEXPECTED_CONTINUE;
  147. if (cf_token_is(&ep->cfp, "}"))
  148. return PARSE_UNEXPECTED_BREAK;
  149. code = cf_token_is_type(&ep->cfp, CFTOKEN_NAME,
  150. "mapping name", ";");
  151. if (code != PARSE_SUCCESS)
  152. return code;
  153. cf_copy_token(&ep->cfp, &var->mapping);
  154. if (!cf_next_valid_token(&ep->cfp)) return PARSE_EOF;
  155. }
  156. /* -------------------------------------- */
  157. if (!cf_token_is(&ep->cfp, ";")) {
  158. if (!cf_go_to_valid_token(&ep->cfp, ";", "}"))
  159. return PARSE_EOF;
  160. return PARSE_CONTINUE;
  161. }
  162. return PARSE_SUCCESS;
  163. }
  164. static void ep_parse_struct(struct effect_parser *ep)
  165. {
  166. struct ep_struct eps;
  167. ep_struct_init(&eps);
  168. if (cf_next_name(&ep->cfp, &eps.name, "name", ";") != PARSE_SUCCESS)
  169. goto error;
  170. if (cf_next_token_should_be(&ep->cfp, "{", ";", NULL) != PARSE_SUCCESS)
  171. goto error;
  172. /* get structure variables */
  173. while (true) {
  174. bool do_break = false;
  175. struct ep_var var;
  176. ep_var_init(&var);
  177. switch (ep_parse_struct_var(ep, &var)) {
  178. case PARSE_UNEXPECTED_CONTINUE:
  179. cf_adderror_syntax_error(&ep->cfp);
  180. case PARSE_CONTINUE:
  181. ep_var_free(&var);
  182. continue;
  183. case PARSE_UNEXPECTED_BREAK:
  184. cf_adderror_syntax_error(&ep->cfp);
  185. case PARSE_BREAK:
  186. ep_var_free(&var);
  187. do_break = true;
  188. break;
  189. case PARSE_EOF:
  190. ep_var_free(&var);
  191. goto error;
  192. }
  193. if (do_break)
  194. break;
  195. da_push_back(eps.vars, &var);
  196. }
  197. if (cf_next_token_should_be(&ep->cfp, ";", NULL, NULL) != PARSE_SUCCESS)
  198. goto error;
  199. da_push_back(ep->structs, &eps);
  200. return;
  201. error:
  202. ep_struct_free(&eps);
  203. }
  204. static inline int ep_parse_pass_command_call(struct effect_parser *ep,
  205. struct darray *call)
  206. {
  207. struct cf_token end_token;
  208. cf_token_clear(&end_token);
  209. while (!cf_token_is(&ep->cfp, ";")) {
  210. if (cf_token_is(&ep->cfp, "}")) {
  211. cf_adderror_expecting(&ep->cfp, ";");
  212. return PARSE_CONTINUE;
  213. }
  214. darray_push_back(sizeof(struct cf_token), call,
  215. ep->cfp.cur_token);
  216. if (!cf_next_valid_token(&ep->cfp)) return PARSE_EOF;
  217. }
  218. darray_push_back(sizeof(struct cf_token), call, ep->cfp.cur_token);
  219. darray_push_back(sizeof(struct cf_token), call, &end_token);
  220. return PARSE_SUCCESS;
  221. }
  222. static int ep_parse_pass_command(struct effect_parser *ep, struct ep_pass *pass)
  223. {
  224. struct darray *call; /* struct cf_token */
  225. if (!cf_next_valid_token(&ep->cfp)) return PARSE_EOF;
  226. if (cf_token_is(&ep->cfp, "vertex_shader") ||
  227. cf_token_is(&ep->cfp, "vertex_program")) {
  228. call = &pass->vertex_program.da;
  229. } else if (cf_token_is(&ep->cfp, "pixel_shader") ||
  230. cf_token_is(&ep->cfp, "pixel_program")) {
  231. call = &pass->fragment_program.da;
  232. } else {
  233. cf_adderror_syntax_error(&ep->cfp);
  234. if (!cf_go_to_valid_token(&ep->cfp, ";", "}")) return PARSE_EOF;
  235. return PARSE_CONTINUE;
  236. }
  237. if (cf_next_token_should_be(&ep->cfp, "=", ";", "}") != PARSE_SUCCESS)
  238. return PARSE_CONTINUE;
  239. if (!cf_next_valid_token(&ep->cfp)) return PARSE_EOF;
  240. if (cf_token_is(&ep->cfp, "compile")) {
  241. cf_adderror(&ep->cfp, "compile keyword not necessary",
  242. LEX_WARNING, NULL, NULL, NULL);
  243. if (!cf_next_valid_token(&ep->cfp)) return PARSE_EOF;
  244. }
  245. return ep_parse_pass_command_call(ep, call);
  246. }
  247. static int ep_parse_pass(struct effect_parser *ep, struct ep_pass *pass)
  248. {
  249. struct cf_token peek;
  250. if (!cf_token_is(&ep->cfp, "pass"))
  251. return PARSE_UNEXPECTED_CONTINUE;
  252. if (!cf_next_valid_token(&ep->cfp)) return PARSE_EOF;
  253. if (!cf_token_is(&ep->cfp, "{")) {
  254. pass->name = bstrdup_n(ep->cfp.cur_token->str.array,
  255. ep->cfp.cur_token->str.len);
  256. if (!cf_next_valid_token(&ep->cfp)) return PARSE_EOF;
  257. }
  258. if (!cf_peek_valid_token(&ep->cfp, &peek)) return PARSE_EOF;
  259. while (strref_cmp(&peek.str, "}") != 0) {
  260. int ret = ep_parse_pass_command(ep, pass);
  261. if (ret < 0 && ret != PARSE_CONTINUE)
  262. return ret;
  263. if (!cf_peek_valid_token(&ep->cfp, &peek))
  264. return PARSE_EOF;
  265. }
  266. /* token is '}' */
  267. cf_next_token(&ep->cfp);
  268. return PARSE_SUCCESS;
  269. }
  270. static void ep_parse_technique(struct effect_parser *ep)
  271. {
  272. struct ep_technique ept;
  273. ep_technique_init(&ept);
  274. if (cf_next_name(&ep->cfp, &ept.name, "name", ";") != PARSE_SUCCESS)
  275. goto error;
  276. if (cf_next_token_should_be(&ep->cfp, "{", ";", NULL) != PARSE_SUCCESS)
  277. goto error;
  278. if (!cf_next_valid_token(&ep->cfp))
  279. goto error;
  280. while (!cf_token_is(&ep->cfp, "}")) {
  281. struct ep_pass pass;
  282. ep_pass_init(&pass);
  283. switch (ep_parse_pass(ep, &pass)) {
  284. case PARSE_UNEXPECTED_CONTINUE:
  285. ep_pass_free(&pass);
  286. if (!cf_go_to_token(&ep->cfp, "}", NULL))
  287. goto error;
  288. continue;
  289. case PARSE_EOF:
  290. ep_pass_free(&pass);
  291. goto error;
  292. }
  293. da_push_back(ept.passes, &pass);
  294. if (!cf_next_valid_token(&ep->cfp))
  295. goto error;
  296. }
  297. /* pass the current token (which is '}') if we reached here */
  298. cf_next_token(&ep->cfp);
  299. da_push_back(ep->techniques, &ept);
  300. return;
  301. error:
  302. cf_next_token(&ep->cfp);
  303. ep_technique_free(&ept);
  304. }
  305. static int ep_parse_sampler_state_item(struct effect_parser *ep,
  306. struct ep_sampler *eps)
  307. {
  308. int ret;
  309. char *state = NULL, *value = NULL;
  310. ret = cf_next_name(&ep->cfp, &state, "state name", ";");
  311. if (ret != PARSE_SUCCESS) goto fail;
  312. ret = cf_next_token_should_be(&ep->cfp, "=", ";", NULL);
  313. if (ret != PARSE_SUCCESS) goto fail;
  314. ret = cf_next_name(&ep->cfp, &value, "value name", ";");
  315. if (ret != PARSE_SUCCESS) goto fail;
  316. ret = cf_next_token_should_be(&ep->cfp, ";", ";", NULL);
  317. if (ret != PARSE_SUCCESS) goto fail;
  318. da_push_back(eps->states, &state);
  319. da_push_back(eps->values, &value);
  320. return ret;
  321. fail:
  322. bfree(state);
  323. bfree(value);
  324. return ret;
  325. }
  326. static void ep_parse_sampler_state(struct effect_parser *ep)
  327. {
  328. struct ep_sampler eps;
  329. struct cf_token peek;
  330. ep_sampler_init(&eps);
  331. if (cf_next_name(&ep->cfp, &eps.name, "name", ";") != PARSE_SUCCESS)
  332. goto error;
  333. if (cf_next_token_should_be(&ep->cfp, "{", ";", NULL) != PARSE_SUCCESS)
  334. goto error;
  335. if (!cf_peek_valid_token(&ep->cfp, &peek))
  336. goto error;
  337. while (strref_cmp(&peek.str, "}") != 0) {
  338. int ret = ep_parse_sampler_state_item(ep, &eps);
  339. if (ret == PARSE_EOF)
  340. goto error;
  341. if (!cf_peek_valid_token(&ep->cfp, &peek))
  342. goto error;
  343. }
  344. if (cf_next_token_should_be(&ep->cfp, "}", ";", NULL) != PARSE_SUCCESS)
  345. goto error;
  346. if (cf_next_token_should_be(&ep->cfp, ";", NULL, NULL) != PARSE_SUCCESS)
  347. goto error;
  348. da_push_back(ep->samplers, &eps);
  349. return;
  350. error:
  351. ep_sampler_free(&eps);
  352. }
  353. static inline int ep_check_for_keyword(struct effect_parser *ep,
  354. const char *keyword, bool *val)
  355. {
  356. bool new_val = cf_token_is(&ep->cfp, keyword);
  357. if (new_val) {
  358. if (!cf_next_valid_token(&ep->cfp))
  359. return PARSE_EOF;
  360. if (new_val && *val)
  361. cf_adderror(&ep->cfp, "'$1' keyword already specified",
  362. LEX_WARNING, keyword, NULL, NULL);
  363. *val = new_val;
  364. return PARSE_CONTINUE;
  365. }
  366. return PARSE_SUCCESS;
  367. }
  368. static inline int ep_parse_func_param(struct effect_parser *ep,
  369. struct ep_func *func, struct ep_var *var)
  370. {
  371. int code;
  372. if (!cf_next_valid_token(&ep->cfp))
  373. return PARSE_EOF;
  374. code = ep_check_for_keyword(ep, "uniform", &var->uniform);
  375. if (code == PARSE_EOF)
  376. return PARSE_EOF;
  377. code = cf_get_name(&ep->cfp, &var->type, "type", ")");
  378. if (code != PARSE_SUCCESS)
  379. return code;
  380. code = cf_next_name(&ep->cfp, &var->name, "name", ")");
  381. if (code != PARSE_SUCCESS)
  382. return code;
  383. if (!cf_next_valid_token(&ep->cfp))
  384. return PARSE_EOF;
  385. if (cf_token_is(&ep->cfp, ":")) {
  386. code = cf_next_name(&ep->cfp, &var->mapping,
  387. "mapping specifier", ")");
  388. if (code != PARSE_SUCCESS)
  389. return code;
  390. if (!cf_next_valid_token(&ep->cfp))
  391. return PARSE_EOF;
  392. }
  393. if (ep_getstruct(ep, var->type) != NULL)
  394. da_push_back(func->struct_deps, &var->type);
  395. else if (ep_getsampler(ep, var->type) != NULL)
  396. da_push_back(func->sampler_deps, &var->type);
  397. return PARSE_SUCCESS;
  398. }
  399. static bool ep_parse_func_params(struct effect_parser *ep, struct ep_func *func)
  400. {
  401. struct cf_token peek;
  402. int code;
  403. cf_token_clear(&peek);
  404. if (!cf_peek_valid_token(&ep->cfp, &peek))
  405. return false;
  406. if (*peek.str.array == ')') {
  407. cf_next_token(&ep->cfp);
  408. goto exit;
  409. }
  410. do {
  411. struct ep_var var;
  412. ep_var_init(&var);
  413. if (!cf_token_is(&ep->cfp, "(") && !cf_token_is(&ep->cfp, ","))
  414. cf_adderror_syntax_error(&ep->cfp);
  415. code = ep_parse_func_param(ep, func, &var);
  416. if (code != PARSE_SUCCESS) {
  417. ep_var_free(&var);
  418. if (code == PARSE_CONTINUE)
  419. goto exit;
  420. else if (code == PARSE_EOF)
  421. return false;
  422. }
  423. da_push_back(func->param_vars, &var);
  424. } while (!cf_token_is(&ep->cfp, ")"));
  425. exit:
  426. return true;
  427. }
  428. static inline bool ep_process_struct_dep(struct effect_parser *ep,
  429. struct ep_func *func)
  430. {
  431. struct ep_struct *val = ep_getstruct_strref(ep,
  432. &ep->cfp.cur_token->str);
  433. if (val)
  434. da_push_back(func->struct_deps, &val->name);
  435. return val != NULL;
  436. }
  437. static inline bool ep_process_func_dep(struct effect_parser *ep,
  438. struct ep_func *func)
  439. {
  440. struct ep_func *val = ep_getfunc_strref(ep,
  441. &ep->cfp.cur_token->str);
  442. if (val)
  443. da_push_back(func->func_deps, &val->name);
  444. return val != NULL;
  445. }
  446. static inline bool ep_process_sampler_dep(struct effect_parser *ep,
  447. struct ep_func *func)
  448. {
  449. struct ep_sampler *val = ep_getsampler_strref(ep,
  450. &ep->cfp.cur_token->str);
  451. if (val)
  452. da_push_back(func->sampler_deps, &val->name);
  453. return val != NULL;
  454. }
  455. static inline bool ep_process_param_dep(struct effect_parser *ep,
  456. struct ep_func *func)
  457. {
  458. struct ep_param *val = ep_getparam_strref(ep, &ep->cfp.cur_token->str);
  459. if (val)
  460. da_push_back(func->param_deps, &val->name);
  461. return val != NULL;
  462. }
  463. static inline bool ep_parse_func_contents(struct effect_parser *ep,
  464. struct ep_func *func)
  465. {
  466. int braces = 1;
  467. dstr_cat_strref(&func->contents, &ep->cfp.cur_token->str);
  468. while (braces > 0) {
  469. if ((ep->cfp.cur_token++)->type == CFTOKEN_NONE)
  470. return false;
  471. if (ep->cfp.cur_token->type == CFTOKEN_SPACETAB ||
  472. ep->cfp.cur_token->type == CFTOKEN_NEWLINE) {
  473. } else if (cf_token_is(&ep->cfp, "{")) {
  474. braces++;
  475. } else if (cf_token_is(&ep->cfp, "}")) {
  476. braces--;
  477. } else if (ep_process_struct_dep(ep, func) ||
  478. ep_process_func_dep(ep, func) ||
  479. ep_process_sampler_dep(ep, func) ||
  480. ep_process_param_dep(ep, func)) {
  481. }
  482. dstr_cat_strref(&func->contents, &ep->cfp.cur_token->str);
  483. }
  484. return true;
  485. }
  486. static void ep_parse_function(struct effect_parser *ep,
  487. char *type, char *name)
  488. {
  489. struct ep_func func;
  490. int code;
  491. ep_func_init(&func, type, name);
  492. if (ep_getstruct(ep, type))
  493. da_push_back(func.struct_deps, &func.ret_type);
  494. if (!ep_parse_func_params(ep, &func))
  495. goto error;
  496. if (!cf_next_valid_token(&ep->cfp))
  497. goto error;
  498. /* if function is mapped to something, for example COLOR */
  499. if (cf_token_is(&ep->cfp, ":")) {
  500. code = cf_next_name(&ep->cfp, &func.mapping,
  501. "mapping specifier", "{");
  502. if (code == PARSE_EOF)
  503. goto error;
  504. else if (code != PARSE_CONTINUE) {
  505. if (!cf_next_valid_token(&ep->cfp))
  506. goto error;
  507. }
  508. }
  509. if (!cf_token_is(&ep->cfp, "{")) {
  510. cf_adderror_expecting(&ep->cfp, "{");
  511. goto error;
  512. }
  513. if (!ep_parse_func_contents(ep, &func))
  514. goto error;
  515. /* it is established that the current token is '}' if we reach this */
  516. cf_next_token(&ep->cfp);
  517. da_push_back(ep->funcs, &func);
  518. return;
  519. error:
  520. ep_func_free(&func);
  521. }
  522. /* parses "array[count]" */
  523. static bool ep_parse_param_array(struct effect_parser *ep,
  524. struct ep_param *param)
  525. {
  526. if (!cf_next_valid_token(&ep->cfp))
  527. return false;
  528. if (ep->cfp.cur_token->type != CFTOKEN_NUM ||
  529. !valid_int_str(ep->cfp.cur_token->str.array,
  530. ep->cfp.cur_token->str.len))
  531. return false;
  532. param->array_count =(int)strtol(ep->cfp.cur_token->str.array, NULL, 10);
  533. if (cf_next_token_should_be(&ep->cfp, "]", ";", NULL) == PARSE_EOF)
  534. return false;
  535. if (!cf_next_valid_token(&ep->cfp))
  536. return false;
  537. return true;
  538. }
  539. static inline int ep_parse_param_assign_texture(struct effect_parser *ep,
  540. struct ep_param *param)
  541. {
  542. int code;
  543. char *str;
  544. if (!cf_next_valid_token(&ep->cfp))
  545. return PARSE_EOF;
  546. code = cf_token_is_type(&ep->cfp, CFTOKEN_STRING,
  547. "texture path string", ";");
  548. if (code != PARSE_SUCCESS)
  549. return code;
  550. str = cf_literal_to_str(ep->cfp.cur_token->str.array,
  551. ep->cfp.cur_token->str.len);
  552. if (str) {
  553. da_copy_array(param->default_val, str, strlen(str) + 1);
  554. bfree(str);
  555. }
  556. return PARSE_SUCCESS;
  557. }
  558. static inline int ep_parse_param_assign_intfloat(struct effect_parser *ep,
  559. struct ep_param *param, bool is_float)
  560. {
  561. int code;
  562. if (!cf_next_valid_token(&ep->cfp))
  563. return PARSE_EOF;
  564. code = cf_token_is_type(&ep->cfp, CFTOKEN_NUM, "numeric value", ";");
  565. if (code != PARSE_SUCCESS)
  566. return code;
  567. if (is_float) {
  568. float f = (float)strtod(ep->cfp.cur_token->str.array, NULL);
  569. da_push_back_array(param->default_val, &f, sizeof(float));
  570. } else {
  571. long l = strtol(ep->cfp.cur_token->str.array, NULL, 10);
  572. da_push_back_array(param->default_val, &l, sizeof(long));
  573. }
  574. return PARSE_SUCCESS;
  575. }
  576. /*
  577. * parses assignment for float1, float2, float3, float4, and any combination
  578. * for float3x3, float4x4, etc
  579. */
  580. static inline int ep_parse_param_assign_float_array(struct effect_parser *ep,
  581. struct ep_param *param)
  582. {
  583. const char *float_type = param->type+5;
  584. int float_count = 0, code, i;
  585. /* -------------------------------------------- */
  586. if (float_type[0] < '1' || float_type[0] > '4')
  587. cf_adderror(&ep->cfp, "Invalid row count", LEX_ERROR,
  588. NULL, NULL, NULL);
  589. float_count = float_type[0]-'0';
  590. if (float_type[1] == 'x') {
  591. if (float_type[2] < '1' || float_type[2] > '4')
  592. cf_adderror(&ep->cfp, "Invalid column count",
  593. LEX_ERROR, NULL, NULL, NULL);
  594. float_count *= float_type[2]-'0';
  595. }
  596. /* -------------------------------------------- */
  597. code = cf_next_token_should_be(&ep->cfp, "{", ";", NULL);
  598. if (code != PARSE_SUCCESS) return code;
  599. for (i = 0; i < float_count; i++) {
  600. char *next = ((i+1) < float_count) ? "," : "}";
  601. code = ep_parse_param_assign_intfloat(ep, param, true);
  602. if (code != PARSE_SUCCESS) return code;
  603. code = cf_next_token_should_be(&ep->cfp, next, ";", NULL);
  604. if (code != PARSE_SUCCESS) return code;
  605. }
  606. return PARSE_SUCCESS;
  607. }
  608. static int ep_parse_param_assignment_val(struct effect_parser *ep,
  609. struct ep_param *param)
  610. {
  611. if (param->is_texture)
  612. return ep_parse_param_assign_texture(ep, param);
  613. else if (strcmp(param->type, "int") == 0)
  614. return ep_parse_param_assign_intfloat(ep, param, false);
  615. else if (strcmp(param->type, "float") == 0)
  616. return ep_parse_param_assign_intfloat(ep, param, true);
  617. else if (astrcmp_n(param->type, "float", 5) == 0)
  618. return ep_parse_param_assign_float_array(ep, param);
  619. cf_adderror(&ep->cfp, "Invalid type '$1' used for assignment",
  620. LEX_ERROR, param->type, NULL, NULL);
  621. return PARSE_CONTINUE;
  622. }
  623. static inline bool ep_parse_param_assign(struct effect_parser *ep,
  624. struct ep_param *param)
  625. {
  626. if (ep_parse_param_assignment_val(ep, param) != PARSE_SUCCESS)
  627. return false;
  628. if (!cf_next_valid_token(&ep->cfp))
  629. return false;
  630. return true;
  631. }
  632. /* static bool ep_parse_param_property(struct effect_parser *ep,
  633. struct ep_param *param)
  634. {
  635. } */
  636. static void ep_parse_param(struct effect_parser *ep,
  637. char *type, char *name,
  638. bool is_property, bool is_const, bool is_uniform)
  639. {
  640. struct ep_param param;
  641. ep_param_init(&param, type, name, is_property, is_const, is_uniform);
  642. if (cf_token_is(&ep->cfp, ";"))
  643. goto complete;
  644. if (cf_token_is(&ep->cfp, "[") && !ep_parse_param_array(ep, &param))
  645. goto error;
  646. if (cf_token_is(&ep->cfp, "=") && !ep_parse_param_assign(ep, &param))
  647. goto error;
  648. /*
  649. if (cf_token_is(&ep->cfp, "<") && !ep_parse_param_property(ep, &param))
  650. goto error; */
  651. if (!cf_token_is(&ep->cfp, ";"))
  652. goto error;
  653. complete:
  654. da_push_back(ep->params, &param);
  655. return;
  656. error:
  657. ep_param_free(&param);
  658. }
  659. static bool ep_get_var_specifiers(struct effect_parser *ep,
  660. bool *is_property, bool *is_const, bool *is_uniform)
  661. {
  662. while(true) {
  663. int code;
  664. code = ep_check_for_keyword(ep, "property", is_property);
  665. if (code == PARSE_EOF)
  666. return false;
  667. else if (code == PARSE_CONTINUE)
  668. continue;
  669. code = ep_check_for_keyword(ep, "const", is_const);
  670. if (code == PARSE_EOF)
  671. return false;
  672. else if (code == PARSE_CONTINUE)
  673. continue;
  674. code = ep_check_for_keyword(ep, "uniform", is_uniform);
  675. if (code == PARSE_EOF)
  676. return false;
  677. else if (code == PARSE_CONTINUE)
  678. continue;
  679. break;
  680. }
  681. return true;
  682. }
  683. static inline void report_invalid_func_keyword(struct effect_parser *ep,
  684. const char *name, bool val)
  685. {
  686. if (val)
  687. cf_adderror(&ep->cfp, "'$1' keyword cannot be used with a "
  688. "function", LEX_ERROR,
  689. name, NULL, NULL);
  690. }
  691. static void ep_parse_other(struct effect_parser *ep)
  692. {
  693. bool is_property = false, is_const = false, is_uniform = false;
  694. char *type = NULL, *name = NULL;
  695. if (!ep_get_var_specifiers(ep, &is_property, &is_const, &is_uniform))
  696. goto error;
  697. if (cf_get_name(&ep->cfp, &type, "type", ";") != PARSE_SUCCESS)
  698. goto error;
  699. if (cf_next_name(&ep->cfp, &name, "name", ";") != PARSE_SUCCESS)
  700. goto error;
  701. if (!cf_next_valid_token(&ep->cfp))
  702. goto error;
  703. if (cf_token_is(&ep->cfp, "(")) {
  704. report_invalid_func_keyword(ep, "property", is_property);
  705. report_invalid_func_keyword(ep, "const", is_const);
  706. report_invalid_func_keyword(ep, "uniform", is_uniform);
  707. ep_parse_function(ep, type, name);
  708. return;
  709. } else {
  710. ep_parse_param(ep, type, name, is_property, is_const,
  711. is_uniform);
  712. return;
  713. }
  714. error:
  715. bfree(type);
  716. bfree(name);
  717. }
  718. static bool ep_compile(struct effect_parser *ep);
  719. extern const char *gs_preprocessor_name(void);
  720. bool ep_parse(struct effect_parser *ep, effect_t effect,
  721. const char *effect_string, const char *file)
  722. {
  723. bool success;
  724. const char *graphics_preprocessor = gs_preprocessor_name();
  725. if (graphics_preprocessor) {
  726. struct cf_def def;
  727. cf_def_init(&def);
  728. def.name.str.array = graphics_preprocessor;
  729. def.name.str.len = strlen(graphics_preprocessor);
  730. strref_copy(&def.name.unmerged_str, &def.name.str);
  731. cf_preprocessor_add_def(&ep->cfp.pp, &def);
  732. }
  733. ep->effect = effect;
  734. if (!cf_parser_parse(&ep->cfp, effect_string, file))
  735. return false;
  736. while (ep->cfp.cur_token && ep->cfp.cur_token->type != CFTOKEN_NONE) {
  737. if (cf_token_is(&ep->cfp, ";") ||
  738. is_whitespace(*ep->cfp.cur_token->str.array)) {
  739. /* do nothing */
  740. ep->cfp.cur_token++;
  741. } else if (cf_token_is(&ep->cfp, "struct")) {
  742. ep_parse_struct(ep);
  743. } else if (cf_token_is(&ep->cfp, "technique")) {
  744. ep_parse_technique(ep);
  745. } else if (cf_token_is(&ep->cfp, "sampler_state")) {
  746. ep_parse_sampler_state(ep);
  747. } else if (cf_token_is(&ep->cfp, "{")) {
  748. /* add error and pass braces */
  749. cf_adderror(&ep->cfp, "Unexpected code segment",
  750. LEX_ERROR, NULL, NULL, NULL);
  751. cf_pass_pair(&ep->cfp, '{', '}');
  752. } else {
  753. /* parameters and functions */
  754. ep_parse_other(ep);
  755. }
  756. }
  757. success = !error_data_has_errors(&ep->cfp.error_list);
  758. if (success)
  759. success = ep_compile(ep);
  760. return success;
  761. }
  762. /* ------------------------------------------------------------------------- */
  763. static inline void ep_write_param(struct dstr *shader, struct ep_param *param,
  764. struct darray *used_params)
  765. {
  766. if (param->written)
  767. return;
  768. if (param->is_const) {
  769. dstr_cat(shader, "const ");
  770. } else if (param->is_uniform) {
  771. struct dstr new;
  772. dstr_init_copy(&new, param->name);
  773. darray_push_back(sizeof(struct dstr), used_params, &new);
  774. dstr_cat(shader, "uniform ");
  775. }
  776. dstr_cat(shader, param->type);
  777. dstr_cat(shader, " ");
  778. dstr_cat(shader, param->name);
  779. if (param->array_count)
  780. dstr_catf(shader, "[%u]", param->array_count);
  781. dstr_cat(shader, ";\n");
  782. param->written = true;
  783. }
  784. static inline void ep_write_func_param_deps(struct effect_parser *ep,
  785. struct dstr *shader, struct ep_func *func,
  786. struct darray *used_params)
  787. {
  788. size_t i;
  789. for (i = 0; i < func->param_deps.num; i++) {
  790. const char *name = func->param_deps.array[i];
  791. struct ep_param *param = ep_getparam(ep, name);
  792. ep_write_param(shader, param, used_params);
  793. }
  794. if (func->param_deps.num)
  795. dstr_cat(shader, "\n\n");
  796. }
  797. static void ep_write_sampler(struct dstr *shader, struct ep_sampler *sampler)
  798. {
  799. size_t i;
  800. if (sampler->written)
  801. return;
  802. dstr_cat(shader, "sampler_state ");
  803. dstr_cat(shader, sampler->name);
  804. dstr_cat(shader, " {");
  805. for (i = 0; i <sampler->values.num; i++) {
  806. dstr_cat(shader, "\n\t");
  807. dstr_cat(shader, sampler->states.array[i]);
  808. dstr_cat(shader, " = ");
  809. dstr_cat(shader, sampler->values.array[i]);
  810. dstr_cat(shader, ";\n");
  811. }
  812. dstr_cat(shader, "\n};\n");
  813. sampler->written = true;
  814. }
  815. static inline void ep_write_func_sampler_deps(struct effect_parser *ep,
  816. struct dstr *shader, struct ep_func *func)
  817. {
  818. size_t i;
  819. for (i = 0; i < func->sampler_deps.num; i++) {
  820. const char *name = func->sampler_deps.array[i];
  821. struct ep_sampler *sampler = ep_getsampler(ep, name);
  822. ep_write_sampler(shader, sampler);
  823. dstr_cat(shader, "\n");
  824. }
  825. }
  826. static inline void ep_write_var(struct dstr *shader, struct ep_var *var)
  827. {
  828. if (var->uniform)
  829. dstr_cat(shader, "uniform ");
  830. dstr_cat(shader, var->type);
  831. dstr_cat(shader, " ");
  832. dstr_cat(shader, var->name);
  833. if (var->mapping) {
  834. dstr_cat(shader, " : ");
  835. dstr_cat(shader, var->mapping);
  836. }
  837. }
  838. static void ep_write_struct(struct dstr *shader, struct ep_struct *st)
  839. {
  840. size_t i;
  841. if (st->written)
  842. return;
  843. dstr_cat(shader, "struct ");
  844. dstr_cat(shader, st->name);
  845. dstr_cat(shader, " {");
  846. for (i = 0; i < st->vars.num; i++) {
  847. dstr_cat(shader, "\n\t");
  848. ep_write_var(shader, st->vars.array+i);
  849. dstr_cat(shader, ";");
  850. }
  851. dstr_cat(shader, "\n};\n");
  852. st->written = true;
  853. }
  854. static inline void ep_write_func_struct_deps(struct effect_parser *ep,
  855. struct dstr *shader, struct ep_func *func)
  856. {
  857. size_t i;
  858. for (i = 0; i < func->struct_deps.num; i++) {
  859. const char *name = func->struct_deps.array[i];
  860. struct ep_struct *st = ep_getstruct(ep, name);
  861. if (!st->written) {
  862. ep_write_struct(shader, st);
  863. dstr_cat(shader, "\n");
  864. st->written = true;
  865. }
  866. }
  867. }
  868. static void ep_write_func(struct effect_parser *ep, struct dstr *shader,
  869. struct ep_func *func, struct darray *used_params);
  870. static inline void ep_write_func_func_deps(struct effect_parser *ep,
  871. struct dstr *shader, struct ep_func *func,
  872. struct darray *used_params)
  873. {
  874. size_t i;
  875. for (i = 0; i < func->func_deps.num; i++) {
  876. const char *name = func->func_deps.array[i];
  877. struct ep_func *func_dep = ep_getfunc(ep, name);
  878. if (!func_dep->written) {
  879. ep_write_func(ep, shader, func_dep, used_params);
  880. dstr_cat(shader, "\n\n");
  881. }
  882. }
  883. }
  884. static void ep_write_func(struct effect_parser *ep, struct dstr *shader,
  885. struct ep_func *func, struct darray *used_params)
  886. {
  887. size_t i;
  888. func->written = true;
  889. ep_write_func_param_deps(ep, shader, func, used_params);
  890. ep_write_func_sampler_deps(ep, shader, func);
  891. ep_write_func_struct_deps(ep, shader, func);
  892. ep_write_func_func_deps(ep, shader, func, used_params);
  893. /* ------------------------------------ */
  894. dstr_cat(shader, func->ret_type);
  895. dstr_cat(shader, " ");
  896. dstr_cat(shader, func->name);
  897. dstr_cat(shader, "(");
  898. for (i = 0; i < func->param_vars.num; i++) {
  899. struct ep_var *var = func->param_vars.array+i;
  900. if (i)
  901. dstr_cat(shader, ", ");
  902. ep_write_var(shader, var);
  903. }
  904. dstr_cat(shader, ")\n");
  905. dstr_cat_dstr(shader, &func->contents);
  906. dstr_cat(shader, "\n");
  907. }
  908. /* writes mapped vars used by the call as parameters for main */
  909. static void ep_write_main_params(struct effect_parser *ep,
  910. struct dstr *shader, struct dstr *param_str,
  911. struct ep_func *func)
  912. {
  913. size_t i;
  914. bool empty_params = dstr_isempty(param_str);
  915. for (i = 0; i < func->param_vars.num; i++) {
  916. struct ep_var *var = func->param_vars.array+i;
  917. struct ep_struct *st = NULL;
  918. bool mapped = (var->mapping != NULL);
  919. if (!mapped) {
  920. st = ep_getstruct(ep, var->type);
  921. if (st)
  922. mapped = ep_struct_mapped(st);
  923. }
  924. if (mapped) {
  925. dstr_cat(shader, var->type);
  926. dstr_cat(shader, " ");
  927. dstr_cat(shader, var->name);
  928. if (!st) {
  929. dstr_cat(shader, " : ");
  930. dstr_cat(shader, var->mapping);
  931. }
  932. if (!dstr_isempty(param_str))
  933. dstr_cat(param_str, ", ");
  934. dstr_cat(param_str, var->name);
  935. }
  936. }
  937. if (!empty_params)
  938. dstr_cat(param_str, ", ");
  939. }
  940. static void ep_write_main(struct effect_parser *ep, struct dstr *shader,
  941. struct ep_func *func, struct dstr *call_str)
  942. {
  943. struct dstr param_str;
  944. struct dstr adjusted_call;
  945. dstr_init(&param_str);
  946. dstr_init_copy_dstr(&adjusted_call, call_str);
  947. dstr_cat(shader, "\n");
  948. dstr_cat(shader, func->ret_type);
  949. dstr_cat(shader, " main(");
  950. ep_write_main_params(ep, shader, &param_str, func);
  951. dstr_cat(shader, ")");
  952. if (func->mapping) {
  953. dstr_cat(shader, " : ");
  954. dstr_cat(shader, func->mapping);
  955. }
  956. dstr_cat(shader, "\n{\n\treturn ");
  957. dstr_cat_dstr(shader, &adjusted_call);
  958. dstr_cat(shader, "\n}\n");
  959. dstr_free(&adjusted_call);
  960. dstr_free(&param_str);
  961. }
  962. static inline void ep_reset_written(struct effect_parser *ep)
  963. {
  964. size_t i;
  965. for (i = 0; i <ep->params.num; i++)
  966. ep->params.array[i].written = false;
  967. for (i = 0; i <ep->structs.num; i++)
  968. ep->structs.array[i].written = false;
  969. for (i = 0; i <ep->funcs.num; i++)
  970. ep->funcs.array[i].written = false;
  971. for (i = 0; i <ep->samplers.num; i++)
  972. ep->samplers.array[i].written = false;
  973. }
  974. static void ep_makeshaderstring(struct effect_parser *ep,
  975. struct dstr *shader, struct darray *shader_call,
  976. struct darray *used_params)
  977. {
  978. struct cf_token *token = shader_call->array;
  979. struct cf_token *func_name;
  980. struct ep_func *func;
  981. struct dstr call_str;
  982. dstr_init(&call_str);
  983. while (token->type != CFTOKEN_NONE && is_whitespace(*token->str.array))
  984. token++;
  985. if (token->type == CFTOKEN_NONE ||
  986. strref_cmp(&token->str, "NULL") == 0)
  987. return;
  988. func_name = token;
  989. while (token->type != CFTOKEN_NONE) {
  990. struct ep_param *param = ep_getparam_strref(ep, &token->str);
  991. if (param)
  992. ep_write_param(shader, param, used_params);
  993. dstr_cat_strref(&call_str, &token->str);
  994. token++;
  995. }
  996. func = ep_getfunc_strref(ep, &func_name->str);
  997. if (!func)
  998. return;
  999. ep_write_func(ep, shader, func, used_params);
  1000. ep_write_main(ep, shader, func, &call_str);
  1001. dstr_free(&call_str);
  1002. ep_reset_written(ep);
  1003. }
  1004. static void ep_compile_param(struct effect_parser *ep, size_t idx)
  1005. {
  1006. struct effect_param *param;
  1007. struct ep_param *param_in;
  1008. param = ep->effect->params.array+idx;
  1009. param_in = ep->params.array+idx;
  1010. param_in->param = param;
  1011. param->name = bstrdup(param_in->name);
  1012. param->section = EFFECT_PARAM;
  1013. param->effect = ep->effect;
  1014. da_move(param->default_val, param_in->default_val);
  1015. if (strcmp(param_in->type, "bool") == 0)
  1016. param->type = SHADER_PARAM_BOOL;
  1017. else if (strcmp(param_in->type, "float") == 0)
  1018. param->type = SHADER_PARAM_FLOAT;
  1019. else if (strcmp(param_in->type, "int") == 0)
  1020. param->type = SHADER_PARAM_INT;
  1021. else if (strcmp(param_in->type, "float2") == 0)
  1022. param->type = SHADER_PARAM_VEC2;
  1023. else if (strcmp(param_in->type, "float3") == 0)
  1024. param->type = SHADER_PARAM_VEC3;
  1025. else if (strcmp(param_in->type, "float4") == 0)
  1026. param->type = SHADER_PARAM_VEC4;
  1027. else if (strcmp(param_in->type, "float4x4") == 0)
  1028. param->type = SHADER_PARAM_MATRIX4X4;
  1029. else if (param_in->is_texture)
  1030. param->type = SHADER_PARAM_TEXTURE;
  1031. if (strcmp(param_in->name, "ViewProj") == 0)
  1032. ep->effect->view_proj = param;
  1033. else if (strcmp(param_in->name, "World") == 0)
  1034. ep->effect->world = param;
  1035. }
  1036. static bool ep_compile_pass_shaderparams(struct effect_parser *ep,
  1037. struct darray *pass_params, struct darray *used_params,
  1038. shader_t shader)
  1039. {
  1040. size_t i;
  1041. darray_resize(sizeof(struct pass_shaderparam), pass_params,
  1042. used_params->num);
  1043. for (i = 0; i < pass_params->num; i++) {
  1044. struct dstr *param_name;
  1045. struct pass_shaderparam *param;
  1046. param_name = darray_item(sizeof(struct dstr), used_params, i);
  1047. param = darray_item(sizeof(struct pass_shaderparam),
  1048. pass_params, i);
  1049. param->eparam = effect_getparambyname(ep->effect,
  1050. param_name->array);
  1051. param->sparam = shader_getparambyname(shader,
  1052. param_name->array);
  1053. if (!param->sparam) {
  1054. blog(LOG_ERROR, "Effect shader parameter not found");
  1055. return false;
  1056. }
  1057. }
  1058. return true;
  1059. }
  1060. static inline bool ep_compile_pass_shader(struct effect_parser *ep,
  1061. struct effect_technique *tech,
  1062. struct effect_pass *pass, struct ep_pass *pass_in,
  1063. size_t pass_idx, enum shader_type type)
  1064. {
  1065. struct dstr shader_str;
  1066. struct dstr location;
  1067. struct darray used_params; /* struct dstr */
  1068. struct darray *pass_params = NULL; /* struct pass_shaderparam */
  1069. shader_t shader = NULL;
  1070. bool success = true;
  1071. dstr_init(&shader_str);
  1072. darray_init(&used_params);
  1073. dstr_init(&location);
  1074. dstr_copy(&location, ep->cfp.lex.file);
  1075. if (type == SHADER_VERTEX)
  1076. dstr_cat(&location, " (Vertex ");
  1077. else if (type == SHADER_PIXEL)
  1078. dstr_cat(&location, " (Pixel ");
  1079. /*else if (type == SHADER_GEOMETRY)
  1080. dstr_cat(&location, " (Geometry ");*/
  1081. dstr_catf(&location, "shader, technique %s, pass %u)", tech->name,
  1082. pass_idx);
  1083. if (type == SHADER_VERTEX) {
  1084. ep_makeshaderstring(ep, &shader_str,
  1085. &pass_in->vertex_program.da, &used_params);
  1086. pass->vertshader = gs_create_vertexshader(shader_str.array,
  1087. location.array, NULL);
  1088. shader = pass->vertshader;
  1089. pass_params = &pass->vertshader_params.da;
  1090. } else if (type == SHADER_PIXEL) {
  1091. ep_makeshaderstring(ep, &shader_str,
  1092. &pass_in->fragment_program.da, &used_params);
  1093. pass->pixelshader = gs_create_pixelshader(shader_str.array,
  1094. location.array, NULL);
  1095. shader = pass->pixelshader;
  1096. pass_params = &pass->pixelshader_params.da;
  1097. }
  1098. #if 0
  1099. blog(LOG_DEBUG, "+++++++++++++++++++++++++++++++++++");
  1100. blog(LOG_DEBUG, " %s", location.array);
  1101. blog(LOG_DEBUG, "-----------------------------------");
  1102. blog(LOG_DEBUG, "%s", shader_str.array);
  1103. blog(LOG_DEBUG, "+++++++++++++++++++++++++++++++++++");
  1104. #endif
  1105. if (shader)
  1106. success = ep_compile_pass_shaderparams(ep, pass_params,
  1107. &used_params, shader);
  1108. else
  1109. success = false;
  1110. dstr_free(&location);
  1111. dstr_array_free(used_params.array, used_params.num);
  1112. darray_free(&used_params);
  1113. dstr_free(&shader_str);
  1114. return success;
  1115. }
  1116. static bool ep_compile_pass(struct effect_parser *ep,
  1117. struct effect_technique *tech,
  1118. struct ep_technique *tech_in,
  1119. size_t idx)
  1120. {
  1121. struct effect_pass *pass;
  1122. struct ep_pass *pass_in;
  1123. bool success = true;
  1124. pass = tech->passes.array+idx;
  1125. pass_in = tech_in->passes.array+idx;
  1126. pass->name = bstrdup(pass_in->name);
  1127. pass->section = EFFECT_PASS;
  1128. if (!ep_compile_pass_shader(ep, tech, pass, pass_in, idx,
  1129. SHADER_VERTEX))
  1130. success = false;
  1131. if (!ep_compile_pass_shader(ep, tech, pass, pass_in, idx,
  1132. SHADER_PIXEL))
  1133. success = false;
  1134. return success;
  1135. }
  1136. static inline bool ep_compile_technique(struct effect_parser *ep, size_t idx)
  1137. {
  1138. struct effect_technique *tech;
  1139. struct ep_technique *tech_in;
  1140. bool success = true;
  1141. size_t i;
  1142. tech = ep->effect->techniques.array+idx;
  1143. tech_in = ep->techniques.array+idx;
  1144. tech->name = bstrdup(tech_in->name);
  1145. tech->section = EFFECT_TECHNIQUE;
  1146. tech->effect = ep->effect;
  1147. da_resize(tech->passes, tech_in->passes.num);
  1148. for (i = 0; i < tech->passes.num; i++) {
  1149. if (!ep_compile_pass(ep, tech, tech_in, i))
  1150. success = false;
  1151. }
  1152. return success;
  1153. }
  1154. static bool ep_compile(struct effect_parser *ep)
  1155. {
  1156. bool success = true;
  1157. size_t i;
  1158. assert(ep->effect);
  1159. da_resize(ep->effect->params, ep->params.num);
  1160. da_resize(ep->effect->techniques, ep->techniques.num);
  1161. for (i = 0; i < ep->params.num; i++)
  1162. ep_compile_param(ep, i);
  1163. for (i = 0; i < ep->techniques.num; i++) {
  1164. if (!ep_compile_technique(ep, i))
  1165. success = false;
  1166. }
  1167. return success;
  1168. }