effect-parser.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512
  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;
  312. struct dstr value = {0};
  313. ret = cf_next_name(&ep->cfp, &state, "state name", ";");
  314. if (ret != PARSE_SUCCESS) goto fail;
  315. ret = cf_next_token_should_be(&ep->cfp, "=", ";", NULL);
  316. if (ret != PARSE_SUCCESS) goto fail;
  317. for (;;) {
  318. const char *cur_str;
  319. if (!cf_next_valid_token(&ep->cfp))
  320. return PARSE_EOF;
  321. cur_str = ep->cfp.cur_token->str.array;
  322. if (*cur_str == ';')
  323. break;
  324. dstr_ncat(&value, cur_str, ep->cfp.cur_token->str.len);
  325. }
  326. if (value.len) {
  327. da_push_back(eps->states, &state);
  328. da_push_back(eps->values, &value.array);
  329. }
  330. return ret;
  331. fail:
  332. bfree(state);
  333. dstr_free(&value);
  334. return ret;
  335. }
  336. static void ep_parse_sampler_state(struct effect_parser *ep)
  337. {
  338. struct ep_sampler eps;
  339. struct cf_token peek;
  340. ep_sampler_init(&eps);
  341. if (cf_next_name(&ep->cfp, &eps.name, "name", ";") != PARSE_SUCCESS)
  342. goto error;
  343. if (cf_next_token_should_be(&ep->cfp, "{", ";", NULL) != PARSE_SUCCESS)
  344. goto error;
  345. if (!cf_peek_valid_token(&ep->cfp, &peek))
  346. goto error;
  347. while (strref_cmp(&peek.str, "}") != 0) {
  348. int ret = ep_parse_sampler_state_item(ep, &eps);
  349. if (ret == PARSE_EOF)
  350. goto error;
  351. if (!cf_peek_valid_token(&ep->cfp, &peek))
  352. goto error;
  353. }
  354. if (cf_next_token_should_be(&ep->cfp, "}", ";", NULL) != PARSE_SUCCESS)
  355. goto error;
  356. if (cf_next_token_should_be(&ep->cfp, ";", NULL, NULL) != PARSE_SUCCESS)
  357. goto error;
  358. da_push_back(ep->samplers, &eps);
  359. return;
  360. error:
  361. ep_sampler_free(&eps);
  362. }
  363. static inline int ep_check_for_keyword(struct effect_parser *ep,
  364. const char *keyword, bool *val)
  365. {
  366. bool new_val = cf_token_is(&ep->cfp, keyword);
  367. if (new_val) {
  368. if (!cf_next_valid_token(&ep->cfp))
  369. return PARSE_EOF;
  370. if (new_val && *val)
  371. cf_adderror(&ep->cfp, "'$1' keyword already specified",
  372. LEX_WARNING, keyword, NULL, NULL);
  373. *val = new_val;
  374. return PARSE_CONTINUE;
  375. }
  376. return PARSE_SUCCESS;
  377. }
  378. static inline int ep_parse_func_param(struct effect_parser *ep,
  379. struct ep_func *func, struct ep_var *var)
  380. {
  381. int code;
  382. if (!cf_next_valid_token(&ep->cfp))
  383. return PARSE_EOF;
  384. code = ep_check_for_keyword(ep, "uniform", &var->uniform);
  385. if (code == PARSE_EOF)
  386. return PARSE_EOF;
  387. code = cf_get_name(&ep->cfp, &var->type, "type", ")");
  388. if (code != PARSE_SUCCESS)
  389. return code;
  390. code = cf_next_name(&ep->cfp, &var->name, "name", ")");
  391. if (code != PARSE_SUCCESS)
  392. return code;
  393. if (!cf_next_valid_token(&ep->cfp))
  394. return PARSE_EOF;
  395. if (cf_token_is(&ep->cfp, ":")) {
  396. code = cf_next_name(&ep->cfp, &var->mapping,
  397. "mapping specifier", ")");
  398. if (code != PARSE_SUCCESS)
  399. return code;
  400. if (!cf_next_valid_token(&ep->cfp))
  401. return PARSE_EOF;
  402. }
  403. if (ep_getstruct(ep, var->type) != NULL)
  404. da_push_back(func->struct_deps, &var->type);
  405. else if (ep_getsampler(ep, var->type) != NULL)
  406. da_push_back(func->sampler_deps, &var->type);
  407. return PARSE_SUCCESS;
  408. }
  409. static bool ep_parse_func_params(struct effect_parser *ep, struct ep_func *func)
  410. {
  411. struct cf_token peek;
  412. int code;
  413. cf_token_clear(&peek);
  414. if (!cf_peek_valid_token(&ep->cfp, &peek))
  415. return false;
  416. if (*peek.str.array == ')') {
  417. cf_next_token(&ep->cfp);
  418. goto exit;
  419. }
  420. do {
  421. struct ep_var var;
  422. ep_var_init(&var);
  423. if (!cf_token_is(&ep->cfp, "(") && !cf_token_is(&ep->cfp, ","))
  424. cf_adderror_syntax_error(&ep->cfp);
  425. code = ep_parse_func_param(ep, func, &var);
  426. if (code != PARSE_SUCCESS) {
  427. ep_var_free(&var);
  428. if (code == PARSE_CONTINUE)
  429. goto exit;
  430. else if (code == PARSE_EOF)
  431. return false;
  432. }
  433. da_push_back(func->param_vars, &var);
  434. } while (!cf_token_is(&ep->cfp, ")"));
  435. exit:
  436. return true;
  437. }
  438. static inline bool ep_process_struct_dep(struct effect_parser *ep,
  439. struct ep_func *func)
  440. {
  441. struct ep_struct *val = ep_getstruct_strref(ep,
  442. &ep->cfp.cur_token->str);
  443. if (val)
  444. da_push_back(func->struct_deps, &val->name);
  445. return val != NULL;
  446. }
  447. static inline bool ep_process_func_dep(struct effect_parser *ep,
  448. struct ep_func *func)
  449. {
  450. struct ep_func *val = ep_getfunc_strref(ep,
  451. &ep->cfp.cur_token->str);
  452. if (val)
  453. da_push_back(func->func_deps, &val->name);
  454. return val != NULL;
  455. }
  456. static inline bool ep_process_sampler_dep(struct effect_parser *ep,
  457. struct ep_func *func)
  458. {
  459. struct ep_sampler *val = ep_getsampler_strref(ep,
  460. &ep->cfp.cur_token->str);
  461. if (val)
  462. da_push_back(func->sampler_deps, &val->name);
  463. return val != NULL;
  464. }
  465. static inline bool ep_process_param_dep(struct effect_parser *ep,
  466. struct ep_func *func)
  467. {
  468. struct ep_param *val = ep_getparam_strref(ep, &ep->cfp.cur_token->str);
  469. if (val)
  470. da_push_back(func->param_deps, &val->name);
  471. return val != NULL;
  472. }
  473. static inline bool ep_parse_func_contents(struct effect_parser *ep,
  474. struct ep_func *func)
  475. {
  476. int braces = 1;
  477. dstr_cat_strref(&func->contents, &ep->cfp.cur_token->str);
  478. while (braces > 0) {
  479. if ((ep->cfp.cur_token++)->type == CFTOKEN_NONE)
  480. return false;
  481. if (ep->cfp.cur_token->type == CFTOKEN_SPACETAB ||
  482. ep->cfp.cur_token->type == CFTOKEN_NEWLINE) {
  483. } else if (cf_token_is(&ep->cfp, "{")) {
  484. braces++;
  485. } else if (cf_token_is(&ep->cfp, "}")) {
  486. braces--;
  487. } else if (ep_process_struct_dep(ep, func) ||
  488. ep_process_func_dep(ep, func) ||
  489. ep_process_sampler_dep(ep, func) ||
  490. ep_process_param_dep(ep, func)) {
  491. }
  492. dstr_cat_strref(&func->contents, &ep->cfp.cur_token->str);
  493. }
  494. return true;
  495. }
  496. static void ep_parse_function(struct effect_parser *ep,
  497. char *type, char *name)
  498. {
  499. struct ep_func func;
  500. int code;
  501. ep_func_init(&func, type, name);
  502. if (ep_getstruct(ep, type))
  503. da_push_back(func.struct_deps, &func.ret_type);
  504. if (!ep_parse_func_params(ep, &func))
  505. goto error;
  506. if (!cf_next_valid_token(&ep->cfp))
  507. goto error;
  508. /* if function is mapped to something, for example COLOR */
  509. if (cf_token_is(&ep->cfp, ":")) {
  510. code = cf_next_name(&ep->cfp, &func.mapping,
  511. "mapping specifier", "{");
  512. if (code == PARSE_EOF)
  513. goto error;
  514. else if (code != PARSE_CONTINUE) {
  515. if (!cf_next_valid_token(&ep->cfp))
  516. goto error;
  517. }
  518. }
  519. if (!cf_token_is(&ep->cfp, "{")) {
  520. cf_adderror_expecting(&ep->cfp, "{");
  521. goto error;
  522. }
  523. if (!ep_parse_func_contents(ep, &func))
  524. goto error;
  525. /* it is established that the current token is '}' if we reach this */
  526. cf_next_token(&ep->cfp);
  527. da_push_back(ep->funcs, &func);
  528. return;
  529. error:
  530. ep_func_free(&func);
  531. }
  532. /* parses "array[count]" */
  533. static bool ep_parse_param_array(struct effect_parser *ep,
  534. struct ep_param *param)
  535. {
  536. if (!cf_next_valid_token(&ep->cfp))
  537. return false;
  538. if (ep->cfp.cur_token->type != CFTOKEN_NUM ||
  539. !valid_int_str(ep->cfp.cur_token->str.array,
  540. ep->cfp.cur_token->str.len))
  541. return false;
  542. param->array_count =(int)strtol(ep->cfp.cur_token->str.array, NULL, 10);
  543. if (cf_next_token_should_be(&ep->cfp, "]", ";", NULL) == PARSE_EOF)
  544. return false;
  545. if (!cf_next_valid_token(&ep->cfp))
  546. return false;
  547. return true;
  548. }
  549. static inline int ep_parse_param_assign_texture(struct effect_parser *ep,
  550. struct ep_param *param)
  551. {
  552. int code;
  553. char *str;
  554. if (!cf_next_valid_token(&ep->cfp))
  555. return PARSE_EOF;
  556. code = cf_token_is_type(&ep->cfp, CFTOKEN_STRING,
  557. "texture path string", ";");
  558. if (code != PARSE_SUCCESS)
  559. return code;
  560. str = cf_literal_to_str(ep->cfp.cur_token->str.array,
  561. ep->cfp.cur_token->str.len);
  562. if (str) {
  563. da_copy_array(param->default_val, str, strlen(str) + 1);
  564. bfree(str);
  565. }
  566. return PARSE_SUCCESS;
  567. }
  568. static inline int ep_parse_param_assign_intfloat(struct effect_parser *ep,
  569. struct ep_param *param, bool is_float)
  570. {
  571. int code;
  572. bool is_negative = false;
  573. if (!cf_next_valid_token(&ep->cfp))
  574. return PARSE_EOF;
  575. if (cf_token_is(&ep->cfp, "-")) {
  576. is_negative = true;
  577. if (!cf_next_token(&ep->cfp))
  578. return PARSE_EOF;
  579. }
  580. code = cf_token_is_type(&ep->cfp, CFTOKEN_NUM, "numeric value", ";");
  581. if (code != PARSE_SUCCESS)
  582. return code;
  583. if (is_float) {
  584. float f = (float)os_strtod(ep->cfp.cur_token->str.array);
  585. if (is_negative) f = -f;
  586. da_push_back_array(param->default_val, &f, sizeof(float));
  587. } else {
  588. long l = strtol(ep->cfp.cur_token->str.array, NULL, 10);
  589. if (is_negative) l = -l;
  590. da_push_back_array(param->default_val, &l, sizeof(long));
  591. }
  592. return PARSE_SUCCESS;
  593. }
  594. /*
  595. * parses assignment for float1, float2, float3, float4, and any combination
  596. * for float3x3, float4x4, etc
  597. */
  598. static inline int ep_parse_param_assign_float_array(struct effect_parser *ep,
  599. struct ep_param *param)
  600. {
  601. const char *float_type = param->type+5;
  602. int float_count = 0, code, i;
  603. /* -------------------------------------------- */
  604. if (float_type[0] < '1' || float_type[0] > '4')
  605. cf_adderror(&ep->cfp, "Invalid row count", LEX_ERROR,
  606. NULL, NULL, NULL);
  607. float_count = float_type[0]-'0';
  608. if (float_type[1] == 'x') {
  609. if (float_type[2] < '1' || float_type[2] > '4')
  610. cf_adderror(&ep->cfp, "Invalid column count",
  611. LEX_ERROR, NULL, NULL, NULL);
  612. float_count *= float_type[2]-'0';
  613. }
  614. /* -------------------------------------------- */
  615. code = cf_next_token_should_be(&ep->cfp, "{", ";", NULL);
  616. if (code != PARSE_SUCCESS) return code;
  617. for (i = 0; i < float_count; i++) {
  618. char *next = ((i+1) < float_count) ? "," : "}";
  619. code = ep_parse_param_assign_intfloat(ep, param, true);
  620. if (code != PARSE_SUCCESS) return code;
  621. code = cf_next_token_should_be(&ep->cfp, next, ";", NULL);
  622. if (code != PARSE_SUCCESS) return code;
  623. }
  624. return PARSE_SUCCESS;
  625. }
  626. static int ep_parse_param_assignment_val(struct effect_parser *ep,
  627. struct ep_param *param)
  628. {
  629. if (param->is_texture)
  630. return ep_parse_param_assign_texture(ep, param);
  631. else if (strcmp(param->type, "int") == 0)
  632. return ep_parse_param_assign_intfloat(ep, param, false);
  633. else if (strcmp(param->type, "float") == 0)
  634. return ep_parse_param_assign_intfloat(ep, param, true);
  635. else if (astrcmp_n(param->type, "float", 5) == 0)
  636. return ep_parse_param_assign_float_array(ep, param);
  637. cf_adderror(&ep->cfp, "Invalid type '$1' used for assignment",
  638. LEX_ERROR, param->type, NULL, NULL);
  639. return PARSE_CONTINUE;
  640. }
  641. static inline bool ep_parse_param_assign(struct effect_parser *ep,
  642. struct ep_param *param)
  643. {
  644. if (ep_parse_param_assignment_val(ep, param) != PARSE_SUCCESS)
  645. return false;
  646. if (!cf_next_valid_token(&ep->cfp))
  647. return false;
  648. return true;
  649. }
  650. /* static bool ep_parse_param_property(struct effect_parser *ep,
  651. struct ep_param *param)
  652. {
  653. } */
  654. static void ep_parse_param(struct effect_parser *ep,
  655. char *type, char *name,
  656. bool is_property, bool is_const, bool is_uniform)
  657. {
  658. struct ep_param param;
  659. ep_param_init(&param, type, name, is_property, is_const, is_uniform);
  660. if (cf_token_is(&ep->cfp, ";"))
  661. goto complete;
  662. if (cf_token_is(&ep->cfp, "[") && !ep_parse_param_array(ep, &param))
  663. goto error;
  664. if (cf_token_is(&ep->cfp, "=") && !ep_parse_param_assign(ep, &param))
  665. goto error;
  666. /*
  667. if (cf_token_is(&ep->cfp, "<") && !ep_parse_param_property(ep, &param))
  668. goto error; */
  669. if (!cf_token_is(&ep->cfp, ";"))
  670. goto error;
  671. complete:
  672. da_push_back(ep->params, &param);
  673. return;
  674. error:
  675. ep_param_free(&param);
  676. }
  677. static bool ep_get_var_specifiers(struct effect_parser *ep,
  678. bool *is_property, bool *is_const, bool *is_uniform)
  679. {
  680. while(true) {
  681. int code;
  682. code = ep_check_for_keyword(ep, "property", is_property);
  683. if (code == PARSE_EOF)
  684. return false;
  685. else if (code == PARSE_CONTINUE)
  686. continue;
  687. code = ep_check_for_keyword(ep, "const", is_const);
  688. if (code == PARSE_EOF)
  689. return false;
  690. else if (code == PARSE_CONTINUE)
  691. continue;
  692. code = ep_check_for_keyword(ep, "uniform", is_uniform);
  693. if (code == PARSE_EOF)
  694. return false;
  695. else if (code == PARSE_CONTINUE)
  696. continue;
  697. break;
  698. }
  699. return true;
  700. }
  701. static inline void report_invalid_func_keyword(struct effect_parser *ep,
  702. const char *name, bool val)
  703. {
  704. if (val)
  705. cf_adderror(&ep->cfp, "'$1' keyword cannot be used with a "
  706. "function", LEX_ERROR,
  707. name, NULL, NULL);
  708. }
  709. static void ep_parse_other(struct effect_parser *ep)
  710. {
  711. bool is_property = false, is_const = false, is_uniform = false;
  712. char *type = NULL, *name = NULL;
  713. if (!ep_get_var_specifiers(ep, &is_property, &is_const, &is_uniform))
  714. goto error;
  715. if (cf_get_name(&ep->cfp, &type, "type", ";") != PARSE_SUCCESS)
  716. goto error;
  717. if (cf_next_name(&ep->cfp, &name, "name", ";") != PARSE_SUCCESS)
  718. goto error;
  719. if (!cf_next_valid_token(&ep->cfp))
  720. goto error;
  721. if (cf_token_is(&ep->cfp, "(")) {
  722. report_invalid_func_keyword(ep, "property", is_property);
  723. report_invalid_func_keyword(ep, "const", is_const);
  724. report_invalid_func_keyword(ep, "uniform", is_uniform);
  725. ep_parse_function(ep, type, name);
  726. return;
  727. } else {
  728. ep_parse_param(ep, type, name, is_property, is_const,
  729. is_uniform);
  730. return;
  731. }
  732. error:
  733. bfree(type);
  734. bfree(name);
  735. }
  736. static bool ep_compile(struct effect_parser *ep);
  737. extern const char *gs_preprocessor_name(void);
  738. bool ep_parse(struct effect_parser *ep, gs_effect_t *effect,
  739. const char *effect_string, const char *file)
  740. {
  741. bool success;
  742. const char *graphics_preprocessor = gs_preprocessor_name();
  743. if (graphics_preprocessor) {
  744. struct cf_def def;
  745. cf_def_init(&def);
  746. def.name.str.array = graphics_preprocessor;
  747. def.name.str.len = strlen(graphics_preprocessor);
  748. strref_copy(&def.name.unmerged_str, &def.name.str);
  749. cf_preprocessor_add_def(&ep->cfp.pp, &def);
  750. }
  751. ep->effect = effect;
  752. if (!cf_parser_parse(&ep->cfp, effect_string, file))
  753. return false;
  754. while (ep->cfp.cur_token && ep->cfp.cur_token->type != CFTOKEN_NONE) {
  755. if (cf_token_is(&ep->cfp, ";") ||
  756. is_whitespace(*ep->cfp.cur_token->str.array)) {
  757. /* do nothing */
  758. ep->cfp.cur_token++;
  759. } else if (cf_token_is(&ep->cfp, "struct")) {
  760. ep_parse_struct(ep);
  761. } else if (cf_token_is(&ep->cfp, "technique")) {
  762. ep_parse_technique(ep);
  763. } else if (cf_token_is(&ep->cfp, "sampler_state")) {
  764. ep_parse_sampler_state(ep);
  765. } else if (cf_token_is(&ep->cfp, "{")) {
  766. /* add error and pass braces */
  767. cf_adderror(&ep->cfp, "Unexpected code segment",
  768. LEX_ERROR, NULL, NULL, NULL);
  769. cf_pass_pair(&ep->cfp, '{', '}');
  770. } else {
  771. /* parameters and functions */
  772. ep_parse_other(ep);
  773. }
  774. }
  775. success = !error_data_has_errors(&ep->cfp.error_list);
  776. if (success)
  777. success = ep_compile(ep);
  778. return success;
  779. }
  780. /* ------------------------------------------------------------------------- */
  781. static inline void ep_write_param(struct dstr *shader, struct ep_param *param,
  782. struct darray *used_params)
  783. {
  784. if (param->written)
  785. return;
  786. if (param->is_const) {
  787. dstr_cat(shader, "const ");
  788. } else if (param->is_uniform) {
  789. struct dstr new;
  790. dstr_init_copy(&new, param->name);
  791. darray_push_back(sizeof(struct dstr), used_params, &new);
  792. dstr_cat(shader, "uniform ");
  793. }
  794. dstr_cat(shader, param->type);
  795. dstr_cat(shader, " ");
  796. dstr_cat(shader, param->name);
  797. if (param->array_count)
  798. dstr_catf(shader, "[%u]", param->array_count);
  799. dstr_cat(shader, ";\n");
  800. param->written = true;
  801. }
  802. static inline void ep_write_func_param_deps(struct effect_parser *ep,
  803. struct dstr *shader, struct ep_func *func,
  804. struct darray *used_params)
  805. {
  806. size_t i;
  807. for (i = 0; i < func->param_deps.num; i++) {
  808. const char *name = func->param_deps.array[i];
  809. struct ep_param *param = ep_getparam(ep, name);
  810. ep_write_param(shader, param, used_params);
  811. }
  812. if (func->param_deps.num)
  813. dstr_cat(shader, "\n\n");
  814. }
  815. static void ep_write_sampler(struct dstr *shader, struct ep_sampler *sampler)
  816. {
  817. size_t i;
  818. if (sampler->written)
  819. return;
  820. dstr_cat(shader, "sampler_state ");
  821. dstr_cat(shader, sampler->name);
  822. dstr_cat(shader, " {");
  823. for (i = 0; i <sampler->values.num; i++) {
  824. dstr_cat(shader, "\n\t");
  825. dstr_cat(shader, sampler->states.array[i]);
  826. dstr_cat(shader, " = ");
  827. dstr_cat(shader, sampler->values.array[i]);
  828. dstr_cat(shader, ";\n");
  829. }
  830. dstr_cat(shader, "\n};\n");
  831. sampler->written = true;
  832. }
  833. static inline void ep_write_func_sampler_deps(struct effect_parser *ep,
  834. struct dstr *shader, struct ep_func *func)
  835. {
  836. size_t i;
  837. for (i = 0; i < func->sampler_deps.num; i++) {
  838. const char *name = func->sampler_deps.array[i];
  839. struct ep_sampler *sampler = ep_getsampler(ep, name);
  840. ep_write_sampler(shader, sampler);
  841. dstr_cat(shader, "\n");
  842. }
  843. }
  844. static inline void ep_write_var(struct dstr *shader, struct ep_var *var)
  845. {
  846. if (var->uniform)
  847. dstr_cat(shader, "uniform ");
  848. dstr_cat(shader, var->type);
  849. dstr_cat(shader, " ");
  850. dstr_cat(shader, var->name);
  851. if (var->mapping) {
  852. dstr_cat(shader, " : ");
  853. dstr_cat(shader, var->mapping);
  854. }
  855. }
  856. static void ep_write_struct(struct dstr *shader, struct ep_struct *st)
  857. {
  858. size_t i;
  859. if (st->written)
  860. return;
  861. dstr_cat(shader, "struct ");
  862. dstr_cat(shader, st->name);
  863. dstr_cat(shader, " {");
  864. for (i = 0; i < st->vars.num; i++) {
  865. dstr_cat(shader, "\n\t");
  866. ep_write_var(shader, st->vars.array+i);
  867. dstr_cat(shader, ";");
  868. }
  869. dstr_cat(shader, "\n};\n");
  870. st->written = true;
  871. }
  872. static inline void ep_write_func_struct_deps(struct effect_parser *ep,
  873. struct dstr *shader, struct ep_func *func)
  874. {
  875. size_t i;
  876. for (i = 0; i < func->struct_deps.num; i++) {
  877. const char *name = func->struct_deps.array[i];
  878. struct ep_struct *st = ep_getstruct(ep, name);
  879. if (!st->written) {
  880. ep_write_struct(shader, st);
  881. dstr_cat(shader, "\n");
  882. st->written = true;
  883. }
  884. }
  885. }
  886. static void ep_write_func(struct effect_parser *ep, struct dstr *shader,
  887. struct ep_func *func, struct darray *used_params);
  888. static inline void ep_write_func_func_deps(struct effect_parser *ep,
  889. struct dstr *shader, struct ep_func *func,
  890. struct darray *used_params)
  891. {
  892. size_t i;
  893. for (i = 0; i < func->func_deps.num; i++) {
  894. const char *name = func->func_deps.array[i];
  895. struct ep_func *func_dep = ep_getfunc(ep, name);
  896. if (!func_dep->written) {
  897. ep_write_func(ep, shader, func_dep, used_params);
  898. dstr_cat(shader, "\n\n");
  899. }
  900. }
  901. }
  902. static void ep_write_func(struct effect_parser *ep, struct dstr *shader,
  903. struct ep_func *func, struct darray *used_params)
  904. {
  905. size_t i;
  906. func->written = true;
  907. ep_write_func_param_deps(ep, shader, func, used_params);
  908. ep_write_func_sampler_deps(ep, shader, func);
  909. ep_write_func_struct_deps(ep, shader, func);
  910. ep_write_func_func_deps(ep, shader, func, used_params);
  911. /* ------------------------------------ */
  912. dstr_cat(shader, func->ret_type);
  913. dstr_cat(shader, " ");
  914. dstr_cat(shader, func->name);
  915. dstr_cat(shader, "(");
  916. for (i = 0; i < func->param_vars.num; i++) {
  917. struct ep_var *var = func->param_vars.array+i;
  918. if (i)
  919. dstr_cat(shader, ", ");
  920. ep_write_var(shader, var);
  921. }
  922. dstr_cat(shader, ")\n");
  923. dstr_cat_dstr(shader, &func->contents);
  924. dstr_cat(shader, "\n");
  925. }
  926. /* writes mapped vars used by the call as parameters for main */
  927. static void ep_write_main_params(struct effect_parser *ep,
  928. struct dstr *shader, struct dstr *param_str,
  929. struct ep_func *func)
  930. {
  931. size_t i;
  932. bool empty_params = dstr_is_empty(param_str);
  933. for (i = 0; i < func->param_vars.num; i++) {
  934. struct ep_var *var = func->param_vars.array+i;
  935. struct ep_struct *st = NULL;
  936. bool mapped = (var->mapping != NULL);
  937. if (!mapped) {
  938. st = ep_getstruct(ep, var->type);
  939. if (st)
  940. mapped = ep_struct_mapped(st);
  941. }
  942. if (mapped) {
  943. dstr_cat(shader, var->type);
  944. dstr_cat(shader, " ");
  945. dstr_cat(shader, var->name);
  946. if (!st) {
  947. dstr_cat(shader, " : ");
  948. dstr_cat(shader, var->mapping);
  949. }
  950. if (!dstr_is_empty(param_str))
  951. dstr_cat(param_str, ", ");
  952. dstr_cat(param_str, var->name);
  953. }
  954. }
  955. if (!empty_params)
  956. dstr_cat(param_str, ", ");
  957. }
  958. static void ep_write_main(struct effect_parser *ep, struct dstr *shader,
  959. struct ep_func *func, struct dstr *call_str)
  960. {
  961. struct dstr param_str;
  962. struct dstr adjusted_call;
  963. dstr_init(&param_str);
  964. dstr_init_copy_dstr(&adjusted_call, call_str);
  965. dstr_cat(shader, "\n");
  966. dstr_cat(shader, func->ret_type);
  967. dstr_cat(shader, " main(");
  968. ep_write_main_params(ep, shader, &param_str, func);
  969. dstr_cat(shader, ")");
  970. if (func->mapping) {
  971. dstr_cat(shader, " : ");
  972. dstr_cat(shader, func->mapping);
  973. }
  974. dstr_cat(shader, "\n{\n\treturn ");
  975. dstr_cat_dstr(shader, &adjusted_call);
  976. dstr_cat(shader, "\n}\n");
  977. dstr_free(&adjusted_call);
  978. dstr_free(&param_str);
  979. }
  980. static inline void ep_reset_written(struct effect_parser *ep)
  981. {
  982. size_t i;
  983. for (i = 0; i <ep->params.num; i++)
  984. ep->params.array[i].written = false;
  985. for (i = 0; i <ep->structs.num; i++)
  986. ep->structs.array[i].written = false;
  987. for (i = 0; i <ep->funcs.num; i++)
  988. ep->funcs.array[i].written = false;
  989. for (i = 0; i <ep->samplers.num; i++)
  990. ep->samplers.array[i].written = false;
  991. }
  992. static void ep_makeshaderstring(struct effect_parser *ep,
  993. struct dstr *shader, struct darray *shader_call,
  994. struct darray *used_params)
  995. {
  996. struct cf_token *token = shader_call->array;
  997. struct cf_token *func_name;
  998. struct ep_func *func;
  999. struct dstr call_str;
  1000. dstr_init(&call_str);
  1001. while (token->type != CFTOKEN_NONE && is_whitespace(*token->str.array))
  1002. token++;
  1003. if (token->type == CFTOKEN_NONE ||
  1004. strref_cmp(&token->str, "NULL") == 0)
  1005. return;
  1006. func_name = token;
  1007. while (token->type != CFTOKEN_NONE) {
  1008. struct ep_param *param = ep_getparam_strref(ep, &token->str);
  1009. if (param)
  1010. ep_write_param(shader, param, used_params);
  1011. dstr_cat_strref(&call_str, &token->str);
  1012. token++;
  1013. }
  1014. func = ep_getfunc_strref(ep, &func_name->str);
  1015. if (!func)
  1016. return;
  1017. ep_write_func(ep, shader, func, used_params);
  1018. ep_write_main(ep, shader, func, &call_str);
  1019. dstr_free(&call_str);
  1020. ep_reset_written(ep);
  1021. }
  1022. static void ep_compile_param(struct effect_parser *ep, size_t idx)
  1023. {
  1024. struct gs_effect_param *param;
  1025. struct ep_param *param_in;
  1026. param = ep->effect->params.array+idx;
  1027. param_in = ep->params.array+idx;
  1028. param_in->param = param;
  1029. param->name = bstrdup(param_in->name);
  1030. param->section = EFFECT_PARAM;
  1031. param->effect = ep->effect;
  1032. da_move(param->default_val, param_in->default_val);
  1033. if (strcmp(param_in->type, "bool") == 0)
  1034. param->type = GS_SHADER_PARAM_BOOL;
  1035. else if (strcmp(param_in->type, "float") == 0)
  1036. param->type = GS_SHADER_PARAM_FLOAT;
  1037. else if (strcmp(param_in->type, "int") == 0)
  1038. param->type = GS_SHADER_PARAM_INT;
  1039. else if (strcmp(param_in->type, "float2") == 0)
  1040. param->type = GS_SHADER_PARAM_VEC2;
  1041. else if (strcmp(param_in->type, "float3") == 0)
  1042. param->type = GS_SHADER_PARAM_VEC3;
  1043. else if (strcmp(param_in->type, "float4") == 0)
  1044. param->type = GS_SHADER_PARAM_VEC4;
  1045. else if (strcmp(param_in->type, "float4x4") == 0)
  1046. param->type = GS_SHADER_PARAM_MATRIX4X4;
  1047. else if (param_in->is_texture)
  1048. param->type = GS_SHADER_PARAM_TEXTURE;
  1049. if (strcmp(param_in->name, "ViewProj") == 0)
  1050. ep->effect->view_proj = param;
  1051. else if (strcmp(param_in->name, "World") == 0)
  1052. ep->effect->world = param;
  1053. }
  1054. static bool ep_compile_pass_shaderparams(struct effect_parser *ep,
  1055. struct darray *pass_params, struct darray *used_params,
  1056. gs_shader_t *shader)
  1057. {
  1058. size_t i;
  1059. darray_resize(sizeof(struct pass_shaderparam), pass_params,
  1060. used_params->num);
  1061. for (i = 0; i < pass_params->num; i++) {
  1062. struct dstr *param_name;
  1063. struct pass_shaderparam *param;
  1064. param_name = darray_item(sizeof(struct dstr), used_params, i);
  1065. param = darray_item(sizeof(struct pass_shaderparam),
  1066. pass_params, i);
  1067. param->eparam = gs_effect_get_param_by_name(ep->effect,
  1068. param_name->array);
  1069. param->sparam = gs_shader_get_param_by_name(shader,
  1070. param_name->array);
  1071. if (!param->sparam) {
  1072. blog(LOG_ERROR, "Effect shader parameter not found");
  1073. return false;
  1074. }
  1075. }
  1076. return true;
  1077. }
  1078. static inline bool ep_compile_pass_shader(struct effect_parser *ep,
  1079. struct gs_effect_technique *tech,
  1080. struct gs_effect_pass *pass, struct ep_pass *pass_in,
  1081. size_t pass_idx, enum gs_shader_type type)
  1082. {
  1083. struct dstr shader_str;
  1084. struct dstr location;
  1085. struct darray used_params; /* struct dstr */
  1086. struct darray *pass_params = NULL; /* struct pass_shaderparam */
  1087. gs_shader_t *shader = NULL;
  1088. bool success = true;
  1089. dstr_init(&shader_str);
  1090. darray_init(&used_params);
  1091. dstr_init(&location);
  1092. dstr_copy(&location, ep->cfp.lex.file);
  1093. if (type == GS_SHADER_VERTEX)
  1094. dstr_cat(&location, " (Vertex ");
  1095. else if (type == GS_SHADER_PIXEL)
  1096. dstr_cat(&location, " (Pixel ");
  1097. /*else if (type == SHADER_GEOMETRY)
  1098. dstr_cat(&location, " (Geometry ");*/
  1099. assert(pass_idx <= UINT_MAX);
  1100. dstr_catf(&location, "shader, technique %s, pass %u)", tech->name,
  1101. (unsigned)pass_idx);
  1102. if (type == GS_SHADER_VERTEX) {
  1103. ep_makeshaderstring(ep, &shader_str,
  1104. &pass_in->vertex_program.da, &used_params);
  1105. pass->vertshader = gs_vertexshader_create(shader_str.array,
  1106. location.array, NULL);
  1107. shader = pass->vertshader;
  1108. pass_params = &pass->vertshader_params.da;
  1109. } else if (type == GS_SHADER_PIXEL) {
  1110. ep_makeshaderstring(ep, &shader_str,
  1111. &pass_in->fragment_program.da, &used_params);
  1112. pass->pixelshader = gs_pixelshader_create(shader_str.array,
  1113. location.array, NULL);
  1114. shader = pass->pixelshader;
  1115. pass_params = &pass->pixelshader_params.da;
  1116. }
  1117. #if 0
  1118. blog(LOG_DEBUG, "+++++++++++++++++++++++++++++++++++");
  1119. blog(LOG_DEBUG, " %s", location.array);
  1120. blog(LOG_DEBUG, "-----------------------------------");
  1121. blog(LOG_DEBUG, "%s", shader_str.array);
  1122. blog(LOG_DEBUG, "+++++++++++++++++++++++++++++++++++");
  1123. #endif
  1124. if (shader)
  1125. success = ep_compile_pass_shaderparams(ep, pass_params,
  1126. &used_params, shader);
  1127. else
  1128. success = false;
  1129. dstr_free(&location);
  1130. dstr_array_free(used_params.array, used_params.num);
  1131. darray_free(&used_params);
  1132. dstr_free(&shader_str);
  1133. return success;
  1134. }
  1135. static bool ep_compile_pass(struct effect_parser *ep,
  1136. struct gs_effect_technique *tech,
  1137. struct ep_technique *tech_in,
  1138. size_t idx)
  1139. {
  1140. struct gs_effect_pass *pass;
  1141. struct ep_pass *pass_in;
  1142. bool success = true;
  1143. pass = tech->passes.array+idx;
  1144. pass_in = tech_in->passes.array+idx;
  1145. pass->name = bstrdup(pass_in->name);
  1146. pass->section = EFFECT_PASS;
  1147. if (!ep_compile_pass_shader(ep, tech, pass, pass_in, idx,
  1148. GS_SHADER_VERTEX))
  1149. success = false;
  1150. if (!ep_compile_pass_shader(ep, tech, pass, pass_in, idx,
  1151. GS_SHADER_PIXEL))
  1152. success = false;
  1153. return success;
  1154. }
  1155. static inline bool ep_compile_technique(struct effect_parser *ep, size_t idx)
  1156. {
  1157. struct gs_effect_technique *tech;
  1158. struct ep_technique *tech_in;
  1159. bool success = true;
  1160. size_t i;
  1161. tech = ep->effect->techniques.array+idx;
  1162. tech_in = ep->techniques.array+idx;
  1163. tech->name = bstrdup(tech_in->name);
  1164. tech->section = EFFECT_TECHNIQUE;
  1165. tech->effect = ep->effect;
  1166. da_resize(tech->passes, tech_in->passes.num);
  1167. for (i = 0; i < tech->passes.num; i++) {
  1168. if (!ep_compile_pass(ep, tech, tech_in, i))
  1169. success = false;
  1170. }
  1171. return success;
  1172. }
  1173. static bool ep_compile(struct effect_parser *ep)
  1174. {
  1175. bool success = true;
  1176. size_t i;
  1177. assert(ep->effect);
  1178. da_resize(ep->effect->params, ep->params.num);
  1179. da_resize(ep->effect->techniques, ep->techniques.num);
  1180. for (i = 0; i < ep->params.num; i++)
  1181. ep_compile_param(ep, i);
  1182. for (i = 0; i < ep->techniques.num; i++) {
  1183. if (!ep_compile_technique(ep, i))
  1184. success = false;
  1185. }
  1186. return success;
  1187. }