effect-parser.c 37 KB

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