effect-parser.c 35 KB

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