effect-parser.c 36 KB

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