effect-parser.c 44 KB

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