effect-parser.c 35 KB

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