effect-parser.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483
  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 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 =(int)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, is_uniform);
  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. extern const char *gs_preprocessor_name(void);
  717. bool ep_parse(struct effect_parser *ep, effect_t effect,
  718. const char *effect_string, const char *file)
  719. {
  720. bool success;
  721. const char *graphics_preprocessor = gs_preprocessor_name();
  722. if (graphics_preprocessor) {
  723. struct cf_def def;
  724. cf_def_init(&def);
  725. def.name.str.array = graphics_preprocessor;
  726. def.name.str.len = strlen(graphics_preprocessor);
  727. strref_copy(&def.name.unmerged_str, &def.name.str);
  728. cf_preprocessor_add_def(&ep->cfp.pp, &def);
  729. }
  730. ep->effect = effect;
  731. if (!cf_parser_parse(&ep->cfp, effect_string, file))
  732. return false;
  733. while (ep->cfp.cur_token && ep->cfp.cur_token->type != CFTOKEN_NONE) {
  734. if (token_is(&ep->cfp, ";") ||
  735. is_whitespace(*ep->cfp.cur_token->str.array)) {
  736. /* do nothing */
  737. ep->cfp.cur_token++;
  738. } else if (token_is(&ep->cfp, "struct")) {
  739. ep_parse_struct(ep);
  740. } else if (token_is(&ep->cfp, "technique")) {
  741. ep_parse_technique(ep);
  742. } else if (token_is(&ep->cfp, "sampler_state")) {
  743. ep_parse_sampler_state(ep);
  744. } else if (token_is(&ep->cfp, "{")) {
  745. /* add error and pass braces */
  746. cf_adderror(&ep->cfp, "Unexpected code segment",
  747. LEVEL_ERROR, NULL, NULL, NULL);
  748. pass_pair(&ep->cfp, '{', '}');
  749. } else {
  750. /* parameters and functions */
  751. ep_parse_other(ep);
  752. }
  753. }
  754. success = !error_data_has_errors(&ep->cfp.error_list);
  755. if (success)
  756. success = ep_compile(ep);
  757. return success;
  758. }
  759. /* ------------------------------------------------------------------------- */
  760. static inline void ep_write_param(struct dstr *shader, struct ep_param *param,
  761. struct darray *used_params)
  762. {
  763. if (param->written)
  764. return;
  765. if (param->is_const) {
  766. dstr_cat(shader, "const ");
  767. } else if (param->is_uniform) {
  768. struct dstr new;
  769. dstr_init_copy(&new, param->name);
  770. darray_push_back(sizeof(struct dstr), used_params, &new);
  771. dstr_cat(shader, "uniform ");
  772. }
  773. dstr_cat(shader, param->type);
  774. dstr_cat(shader, " ");
  775. dstr_cat(shader, param->name);
  776. if (param->array_count)
  777. dstr_catf(shader, "[%u]", param->array_count);
  778. dstr_cat(shader, ";\n");
  779. param->written = true;
  780. }
  781. static inline void ep_write_func_param_deps(struct effect_parser *ep,
  782. struct dstr *shader, struct ep_func *func,
  783. struct darray *used_params)
  784. {
  785. size_t i;
  786. for (i = 0; i < func->param_deps.num; i++) {
  787. const char *name = func->param_deps.array[i];
  788. struct ep_param *param = ep_getparam(ep, name);
  789. ep_write_param(shader, param, used_params);
  790. }
  791. if (func->param_deps.num)
  792. dstr_cat(shader, "\n\n");
  793. }
  794. static void ep_write_sampler(struct dstr *shader, struct ep_sampler *sampler)
  795. {
  796. size_t i;
  797. if (sampler->written)
  798. return;
  799. dstr_cat(shader, "sampler_state ");
  800. dstr_cat(shader, sampler->name);
  801. dstr_cat(shader, " {");
  802. for (i = 0; i <sampler->values.num; i++) {
  803. dstr_cat(shader, "\n\t");
  804. dstr_cat(shader, sampler->states.array[i]);
  805. dstr_cat(shader, " = ");
  806. dstr_cat(shader, sampler->values.array[i]);
  807. dstr_cat(shader, ";\n");
  808. }
  809. dstr_cat(shader, "\n};\n");
  810. sampler->written = true;
  811. }
  812. static inline void ep_write_func_sampler_deps(struct effect_parser *ep,
  813. struct dstr *shader, struct ep_func *func)
  814. {
  815. size_t i;
  816. for (i = 0; i < func->sampler_deps.num; i++) {
  817. const char *name = func->sampler_deps.array[i];
  818. struct ep_sampler *sampler = ep_getsampler(ep, name);
  819. ep_write_sampler(shader, sampler);
  820. dstr_cat(shader, "\n");
  821. }
  822. }
  823. static inline void ep_write_var(struct dstr *shader, struct ep_var *var)
  824. {
  825. if (var->uniform)
  826. dstr_cat(shader, "uniform ");
  827. dstr_cat(shader, var->type);
  828. dstr_cat(shader, " ");
  829. dstr_cat(shader, var->name);
  830. if (var->mapping) {
  831. dstr_cat(shader, " : ");
  832. dstr_cat(shader, var->mapping);
  833. }
  834. }
  835. static void ep_write_struct(struct dstr *shader, struct ep_struct *st)
  836. {
  837. size_t i;
  838. if (st->written)
  839. return;
  840. dstr_cat(shader, "struct ");
  841. dstr_cat(shader, st->name);
  842. dstr_cat(shader, " {");
  843. for (i = 0; i < st->vars.num; i++) {
  844. dstr_cat(shader, "\n\t");
  845. ep_write_var(shader, st->vars.array+i);
  846. dstr_cat(shader, ";");
  847. }
  848. dstr_cat(shader, "\n};\n");
  849. st->written = true;
  850. }
  851. static inline void ep_write_func_struct_deps(struct effect_parser *ep,
  852. struct dstr *shader, struct ep_func *func)
  853. {
  854. size_t i;
  855. for (i = 0; i < func->struct_deps.num; i++) {
  856. const char *name = func->struct_deps.array[i];
  857. struct ep_struct *st = ep_getstruct(ep, name);
  858. if (!st->written) {
  859. ep_write_struct(shader, st);
  860. dstr_cat(shader, "\n");
  861. st->written = true;
  862. }
  863. }
  864. }
  865. static void ep_write_func(struct effect_parser *ep, struct dstr *shader,
  866. struct ep_func *func, struct darray *used_params);
  867. static inline void ep_write_func_func_deps(struct effect_parser *ep,
  868. struct dstr *shader, struct ep_func *func,
  869. struct darray *used_params)
  870. {
  871. size_t i;
  872. for (i = 0; i < func->func_deps.num; i++) {
  873. const char *name = func->func_deps.array[i];
  874. struct ep_func *func_dep = ep_getfunc(ep, name);
  875. if (!func_dep->written) {
  876. ep_write_func(ep, shader, func_dep, used_params);
  877. dstr_cat(shader, "\n\n");
  878. }
  879. }
  880. }
  881. static void ep_write_func(struct effect_parser *ep, struct dstr *shader,
  882. struct ep_func *func, struct darray *used_params)
  883. {
  884. size_t i;
  885. func->written = true;
  886. ep_write_func_param_deps(ep, shader, func, used_params);
  887. ep_write_func_sampler_deps(ep, shader, func);
  888. ep_write_func_struct_deps(ep, shader, func);
  889. ep_write_func_func_deps(ep, shader, func, used_params);
  890. /* ------------------------------------ */
  891. dstr_cat(shader, func->ret_type);
  892. dstr_cat(shader, " ");
  893. dstr_cat(shader, func->name);
  894. dstr_cat(shader, "(");
  895. for (i = 0; i < func->param_vars.num; i++) {
  896. struct ep_var *var = func->param_vars.array+i;
  897. if (i)
  898. dstr_cat(shader, ", ");
  899. ep_write_var(shader, var);
  900. }
  901. dstr_cat(shader, ")\n");
  902. dstr_cat_dstr(shader, &func->contents);
  903. dstr_cat(shader, "\n");
  904. }
  905. /* writes mapped vars used by the call as parameters for main */
  906. static void ep_write_main_params(struct effect_parser *ep,
  907. struct dstr *shader, struct dstr *param_str,
  908. struct ep_func *func)
  909. {
  910. size_t i;
  911. bool empty_params = dstr_isempty(param_str);
  912. for (i = 0; i < func->param_vars.num; i++) {
  913. struct ep_var *var = func->param_vars.array+i;
  914. struct ep_struct *st = NULL;
  915. bool mapped = (var->mapping != NULL);
  916. if (!mapped) {
  917. st = ep_getstruct(ep, var->type);
  918. if (st)
  919. mapped = ep_struct_mapped(st);
  920. }
  921. if (mapped) {
  922. dstr_cat(shader, var->type);
  923. dstr_cat(shader, " ");
  924. dstr_cat(shader, var->name);
  925. if (!st) {
  926. dstr_cat(shader, " : ");
  927. dstr_cat(shader, var->mapping);
  928. }
  929. if (!dstr_isempty(param_str))
  930. dstr_cat(param_str, ", ");
  931. dstr_cat(param_str, var->name);
  932. }
  933. }
  934. if (!empty_params)
  935. dstr_cat(param_str, ", ");
  936. }
  937. static void ep_write_main(struct effect_parser *ep, struct dstr *shader,
  938. struct ep_func *func, struct dstr *call_str)
  939. {
  940. struct dstr param_str;
  941. struct dstr adjusted_call;
  942. dstr_init(&param_str);
  943. dstr_init_copy_dstr(&adjusted_call, call_str);
  944. dstr_cat(shader, "\n");
  945. dstr_cat(shader, func->ret_type);
  946. dstr_cat(shader, " main(");
  947. ep_write_main_params(ep, shader, &param_str, func);
  948. dstr_cat(shader, ")");
  949. if (func->mapping) {
  950. dstr_cat(shader, " : ");
  951. dstr_cat(shader, func->mapping);
  952. }
  953. dstr_cat(shader, "\n{\n\treturn ");
  954. dstr_cat_dstr(shader, &adjusted_call);
  955. dstr_cat(shader, "\n}\n");
  956. dstr_free(&adjusted_call);
  957. dstr_free(&param_str);
  958. }
  959. static inline void ep_reset_written(struct effect_parser *ep)
  960. {
  961. size_t i;
  962. for (i = 0; i <ep->params.num; i++)
  963. ep->params.array[i].written = false;
  964. for (i = 0; i <ep->structs.num; i++)
  965. ep->structs.array[i].written = false;
  966. for (i = 0; i <ep->funcs.num; i++)
  967. ep->funcs.array[i].written = false;
  968. for (i = 0; i <ep->samplers.num; i++)
  969. ep->samplers.array[i].written = false;
  970. }
  971. static void ep_makeshaderstring(struct effect_parser *ep,
  972. struct dstr *shader, struct darray *shader_call,
  973. struct darray *used_params)
  974. {
  975. struct cf_token *token = shader_call->array;
  976. struct cf_token *func_name;
  977. struct ep_func *func;
  978. struct dstr call_str;
  979. dstr_init(&call_str);
  980. while (token->type != CFTOKEN_NONE && is_whitespace(*token->str.array))
  981. token++;
  982. if (token->type == CFTOKEN_NONE ||
  983. strref_cmp(&token->str, "NULL") == 0)
  984. return;
  985. func_name = token;
  986. while (token->type != CFTOKEN_NONE) {
  987. struct ep_param *param = ep_getparam_strref(ep, &token->str);
  988. if (param)
  989. ep_write_param(shader, param, used_params);
  990. dstr_cat_strref(&call_str, &token->str);
  991. token++;
  992. }
  993. func = ep_getfunc_strref(ep, &func_name->str);
  994. if (!func)
  995. return;
  996. ep_write_func(ep, shader, func, used_params);
  997. ep_write_main(ep, shader, func, &call_str);
  998. dstr_free(&call_str);
  999. ep_reset_written(ep);
  1000. }
  1001. static void ep_compile_param(struct effect_parser *ep, size_t idx)
  1002. {
  1003. struct effect_param *param;
  1004. struct ep_param *param_in;
  1005. param = ep->effect->params.array+idx;
  1006. param_in = ep->params.array+idx;
  1007. param_in->param = param;
  1008. param->name = bstrdup(param_in->name);
  1009. param->section = EFFECT_PARAM;
  1010. param->effect = ep->effect;
  1011. da_move(param->default_val, param_in->default_val);
  1012. if (strcmp(param_in->type, "bool") == 0)
  1013. param->type = SHADER_PARAM_BOOL;
  1014. else if (strcmp(param_in->type, "float") == 0)
  1015. param->type = SHADER_PARAM_FLOAT;
  1016. else if (strcmp(param_in->type, "int") == 0)
  1017. param->type = SHADER_PARAM_INT;
  1018. else if (strcmp(param_in->type, "float2") == 0)
  1019. param->type = SHADER_PARAM_VEC2;
  1020. else if (strcmp(param_in->type, "float3") == 0)
  1021. param->type = SHADER_PARAM_VEC3;
  1022. else if (strcmp(param_in->type, "float4") == 0)
  1023. param->type = SHADER_PARAM_VEC4;
  1024. else if (strcmp(param_in->type, "float4x4") == 0)
  1025. param->type = SHADER_PARAM_MATRIX4X4;
  1026. else if (param_in->is_texture)
  1027. param->type = SHADER_PARAM_TEXTURE;
  1028. if (strcmp(param_in->name, "ViewProj") == 0)
  1029. ep->effect->view_proj = param;
  1030. else if (strcmp(param_in->name, "World") == 0)
  1031. ep->effect->world = param;
  1032. }
  1033. static bool ep_compile_pass_shaderparams(struct effect_parser *ep,
  1034. struct darray *pass_params, struct darray *used_params,
  1035. shader_t shader)
  1036. {
  1037. size_t i;
  1038. darray_resize(sizeof(struct pass_shaderparam), pass_params,
  1039. used_params->num);
  1040. for (i = 0; i < pass_params->num; i++) {
  1041. struct dstr *param_name;
  1042. struct pass_shaderparam *param;
  1043. param_name = darray_item(sizeof(struct dstr), used_params, i);
  1044. param = darray_item(sizeof(struct pass_shaderparam),
  1045. pass_params, i);
  1046. param->eparam = effect_getparambyname(ep->effect,
  1047. param_name->array);
  1048. param->sparam = shader_getparambyname(shader,
  1049. param_name->array);
  1050. if (!param->sparam) {
  1051. blog(LOG_ERROR, "Effect shader parameter not found");
  1052. return false;
  1053. }
  1054. }
  1055. return true;
  1056. }
  1057. static inline bool ep_compile_pass_shader(struct effect_parser *ep,
  1058. struct effect_technique *tech,
  1059. struct effect_pass *pass, struct ep_pass *pass_in,
  1060. size_t pass_idx, enum shader_type type)
  1061. {
  1062. struct dstr shader_str;
  1063. struct dstr location;
  1064. struct darray used_params; /* struct dstr */
  1065. struct darray *pass_params = NULL; /* struct pass_shaderparam */
  1066. shader_t shader = NULL;
  1067. bool success = true;
  1068. dstr_init(&shader_str);
  1069. darray_init(&used_params);
  1070. dstr_init(&location);
  1071. dstr_copy(&location, ep->cfp.lex.file);
  1072. if (type == SHADER_VERTEX)
  1073. dstr_cat(&location, " (Vertex ");
  1074. else if (type == SHADER_PIXEL)
  1075. dstr_cat(&location, " (Pixel ");
  1076. /*else if (type == SHADER_GEOMETRY)
  1077. dstr_cat(&location, " (Geometry ");*/
  1078. dstr_catf(&location, "shader, technique %s, pass %u)", tech->name,
  1079. pass_idx);
  1080. if (type == SHADER_VERTEX) {
  1081. ep_makeshaderstring(ep, &shader_str,
  1082. &pass_in->vertex_program.da, &used_params);
  1083. pass->vertshader = gs_create_vertexshader(shader_str.array,
  1084. location.array, NULL);
  1085. shader = pass->vertshader;
  1086. pass_params = &pass->vertshader_params.da;
  1087. } else if (type == SHADER_PIXEL) {
  1088. ep_makeshaderstring(ep, &shader_str,
  1089. &pass_in->fragment_program.da, &used_params);
  1090. pass->pixelshader = gs_create_pixelshader(shader_str.array,
  1091. location.array, NULL);
  1092. shader = pass->pixelshader;
  1093. pass_params = &pass->pixelshader_params.da;
  1094. }
  1095. blog(LOG_DEBUG, "+++++++++++++++++++++++++++++++++++");
  1096. blog(LOG_DEBUG, " %s", location.array);
  1097. blog(LOG_DEBUG, "-----------------------------------");
  1098. blog(LOG_DEBUG, "%s", shader_str.array);
  1099. blog(LOG_DEBUG, "+++++++++++++++++++++++++++++++++++");
  1100. if (shader)
  1101. success = ep_compile_pass_shaderparams(ep, pass_params,
  1102. &used_params, shader);
  1103. else
  1104. success = false;
  1105. dstr_free(&location);
  1106. dstr_array_free(used_params.array, used_params.num);
  1107. darray_free(&used_params);
  1108. dstr_free(&shader_str);
  1109. return success;
  1110. }
  1111. static bool ep_compile_pass(struct effect_parser *ep,
  1112. struct effect_technique *tech,
  1113. struct ep_technique *tech_in,
  1114. size_t idx)
  1115. {
  1116. struct effect_pass *pass;
  1117. struct ep_pass *pass_in;
  1118. bool success = true;
  1119. pass = tech->passes.array+idx;
  1120. pass_in = tech_in->passes.array+idx;
  1121. pass->name = bstrdup(pass_in->name);
  1122. pass->section = EFFECT_PASS;
  1123. if (!ep_compile_pass_shader(ep, tech, pass, pass_in, idx,
  1124. SHADER_VERTEX))
  1125. success = false;
  1126. if (!ep_compile_pass_shader(ep, tech, pass, pass_in, idx,
  1127. SHADER_PIXEL))
  1128. success = false;
  1129. return success;
  1130. }
  1131. static inline bool ep_compile_technique(struct effect_parser *ep, size_t idx)
  1132. {
  1133. struct effect_technique *tech;
  1134. struct ep_technique *tech_in;
  1135. bool success = true;
  1136. size_t i;
  1137. tech = ep->effect->techniques.array+idx;
  1138. tech_in = ep->techniques.array+idx;
  1139. tech->name = bstrdup(tech_in->name);
  1140. tech->section = EFFECT_TECHNIQUE;
  1141. tech->effect = ep->effect;
  1142. da_resize(tech->passes, tech_in->passes.num);
  1143. for (i = 0; i < tech->passes.num; i++) {
  1144. if (!ep_compile_pass(ep, tech, tech_in, i))
  1145. success = false;
  1146. }
  1147. return success;
  1148. }
  1149. static bool ep_compile(struct effect_parser *ep)
  1150. {
  1151. bool success = true;
  1152. size_t i;
  1153. assert(ep->effect);
  1154. da_resize(ep->effect->params, ep->params.num);
  1155. da_resize(ep->effect->techniques, ep->techniques.num);
  1156. for (i = 0; i < ep->params.num; i++)
  1157. ep_compile_param(ep, i);
  1158. for (i = 0; i < ep->techniques.num; i++) {
  1159. if (!ep_compile_technique(ep, i))
  1160. success = false;
  1161. }
  1162. return success;
  1163. }