1
0

effect-parser.c 36 KB

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