effect-parser.c 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486
  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. blog(LOG_DEBUG, "+++++++++++++++++++++++++++++++++++");
  1099. blog(LOG_DEBUG, " %s", location.array);
  1100. blog(LOG_DEBUG, "-----------------------------------");
  1101. blog(LOG_DEBUG, "%s", shader_str.array);
  1102. blog(LOG_DEBUG, "+++++++++++++++++++++++++++++++++++");
  1103. if (shader)
  1104. success = ep_compile_pass_shaderparams(ep, pass_params,
  1105. &used_params, shader);
  1106. else
  1107. success = false;
  1108. dstr_free(&location);
  1109. dstr_array_free(used_params.array, used_params.num);
  1110. darray_free(&used_params);
  1111. dstr_free(&shader_str);
  1112. return success;
  1113. }
  1114. static bool ep_compile_pass(struct effect_parser *ep,
  1115. struct effect_technique *tech,
  1116. struct ep_technique *tech_in,
  1117. size_t idx)
  1118. {
  1119. struct effect_pass *pass;
  1120. struct ep_pass *pass_in;
  1121. bool success = true;
  1122. pass = tech->passes.array+idx;
  1123. pass_in = tech_in->passes.array+idx;
  1124. pass->name = bstrdup(pass_in->name);
  1125. pass->section = EFFECT_PASS;
  1126. if (!ep_compile_pass_shader(ep, tech, pass, pass_in, idx,
  1127. SHADER_VERTEX))
  1128. success = false;
  1129. if (!ep_compile_pass_shader(ep, tech, pass, pass_in, idx,
  1130. SHADER_PIXEL))
  1131. success = false;
  1132. return success;
  1133. }
  1134. static inline bool ep_compile_technique(struct effect_parser *ep, size_t idx)
  1135. {
  1136. struct effect_technique *tech;
  1137. struct ep_technique *tech_in;
  1138. bool success = true;
  1139. size_t i;
  1140. tech = ep->effect->techniques.array+idx;
  1141. tech_in = ep->techniques.array+idx;
  1142. tech->name = bstrdup(tech_in->name);
  1143. tech->section = EFFECT_TECHNIQUE;
  1144. tech->effect = ep->effect;
  1145. da_resize(tech->passes, tech_in->passes.num);
  1146. for (i = 0; i < tech->passes.num; i++) {
  1147. if (!ep_compile_pass(ep, tech, tech_in, i))
  1148. success = false;
  1149. }
  1150. return success;
  1151. }
  1152. static bool ep_compile(struct effect_parser *ep)
  1153. {
  1154. bool success = true;
  1155. size_t i;
  1156. assert(ep->effect);
  1157. da_resize(ep->effect->params, ep->params.num);
  1158. da_resize(ep->effect->techniques, ep->techniques.num);
  1159. for (i = 0; i < ep->params.num; i++)
  1160. ep_compile_param(ep, i);
  1161. for (i = 0; i < ep->techniques.num; i++) {
  1162. if (!ep_compile_technique(ep, i))
  1163. success = false;
  1164. }
  1165. return success;
  1166. }