effect-parser.c 36 KB

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