effect-parser.c 51 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067
  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. #if defined(_DEBUG) && defined(_DEBUG_SHADERS)
  967. static void debug_get_default_value(struct gs_effect_param *param,
  968. char* buffer, unsigned long long buf_size)
  969. {
  970. if (param->default_val.num == 0) {
  971. snprintf(buffer, buf_size, "(null)");
  972. return;
  973. }
  974. switch (param->type) {
  975. case GS_SHADER_PARAM_STRING:
  976. snprintf(buffer, buf_size, "'%.*s'",
  977. param->default_val.num,
  978. param->default_val.array);
  979. break;
  980. case GS_SHADER_PARAM_INT:
  981. snprintf(buffer, buf_size, "%ld",
  982. *(int*)(param->default_val.array + 0));
  983. break;
  984. case GS_SHADER_PARAM_INT2:
  985. snprintf(buffer, buf_size, "%ld,%ld",
  986. *(int*)(param->default_val.array + 0),
  987. *(int*)(param->default_val.array + 4));
  988. break;
  989. case GS_SHADER_PARAM_INT3:
  990. snprintf(buffer, buf_size, "%ld,%ld,%ld",
  991. *(int*)(param->default_val.array + 0),
  992. *(int*)(param->default_val.array + 4),
  993. *(int*)(param->default_val.array + 8));
  994. break;
  995. case GS_SHADER_PARAM_INT4:
  996. snprintf(buffer, buf_size, "%ld,%ld,%ld,%ld",
  997. *(int*)(param->default_val.array + 0),
  998. *(int*)(param->default_val.array + 4),
  999. *(int*)(param->default_val.array + 8),
  1000. *(int*)(param->default_val.array + 12));
  1001. break;
  1002. case GS_SHADER_PARAM_FLOAT:
  1003. snprintf(buffer, buf_size, "%e",
  1004. *(float*)(param->default_val.array + 0));
  1005. break;
  1006. case GS_SHADER_PARAM_VEC2:
  1007. snprintf(buffer, buf_size, "%e,%e",
  1008. *(float*)(param->default_val.array + 0),
  1009. *(float*)(param->default_val.array + 4));
  1010. break;
  1011. case GS_SHADER_PARAM_VEC3:
  1012. snprintf(buffer, buf_size, "%e,%e,%e",
  1013. *(float*)(param->default_val.array + 0),
  1014. *(float*)(param->default_val.array + 4),
  1015. *(float*)(param->default_val.array + 8));
  1016. break;
  1017. case GS_SHADER_PARAM_VEC4:
  1018. snprintf(buffer, buf_size, "%e,%e,%e,%e",
  1019. *(float*)(param->default_val.array + 0),
  1020. *(float*)(param->default_val.array + 4),
  1021. *(float*)(param->default_val.array + 8),
  1022. *(float*)(param->default_val.array + 12));
  1023. break;
  1024. case GS_SHADER_PARAM_MATRIX4X4:
  1025. snprintf(buffer, buf_size,
  1026. "[[%e,%e,%e,%e],[%e,%e,%e,%e],"
  1027. "[%e,%e,%e,%e],[%e,%e,%e,%e]]",
  1028. *(float*)(param->default_val.array + 0),
  1029. *(float*)(param->default_val.array + 4),
  1030. *(float*)(param->default_val.array + 8),
  1031. *(float*)(param->default_val.array + 12),
  1032. *(float*)(param->default_val.array + 16),
  1033. *(float*)(param->default_val.array + 20),
  1034. *(float*)(param->default_val.array + 24),
  1035. *(float*)(param->default_val.array + 28),
  1036. *(float*)(param->default_val.array + 32),
  1037. *(float*)(param->default_val.array + 36),
  1038. *(float*)(param->default_val.array + 40),
  1039. *(float*)(param->default_val.array + 44),
  1040. *(float*)(param->default_val.array + 48),
  1041. *(float*)(param->default_val.array + 52),
  1042. *(float*)(param->default_val.array + 56),
  1043. *(float*)(param->default_val.array + 60));
  1044. break;
  1045. case GS_SHADER_PARAM_BOOL:
  1046. snprintf(buffer, buf_size, "%s",
  1047. (*param->default_val.array) != 0
  1048. ? "true\0"
  1049. : "false\0");
  1050. break;
  1051. case GS_SHADER_PARAM_UNKNOWN:
  1052. case GS_SHADER_PARAM_TEXTURE:
  1053. snprintf(buffer, buf_size, "<unknown>");
  1054. break;
  1055. }
  1056. }
  1057. static void debug_param(struct gs_effect_param *param,
  1058. struct ep_param *param_in, unsigned long long idx, const char* offset)
  1059. {
  1060. char _debug_type[4096];
  1061. switch (param->type) {
  1062. case GS_SHADER_PARAM_STRING:
  1063. snprintf(_debug_type, sizeof(_debug_type), "string");
  1064. break;
  1065. case GS_SHADER_PARAM_INT:
  1066. snprintf(_debug_type, sizeof(_debug_type), "int");
  1067. break;
  1068. case GS_SHADER_PARAM_INT2:
  1069. snprintf(_debug_type, sizeof(_debug_type), "int2");
  1070. break;
  1071. case GS_SHADER_PARAM_INT3:
  1072. snprintf(_debug_type, sizeof(_debug_type), "int3");
  1073. break;
  1074. case GS_SHADER_PARAM_INT4:
  1075. snprintf(_debug_type, sizeof(_debug_type), "int4");
  1076. break;
  1077. case GS_SHADER_PARAM_FLOAT:
  1078. snprintf(_debug_type, sizeof(_debug_type), "float");
  1079. break;
  1080. case GS_SHADER_PARAM_VEC2:
  1081. snprintf(_debug_type, sizeof(_debug_type), "float2");
  1082. break;
  1083. case GS_SHADER_PARAM_VEC3:
  1084. snprintf(_debug_type, sizeof(_debug_type), "float3");
  1085. break;
  1086. case GS_SHADER_PARAM_VEC4:
  1087. snprintf(_debug_type, sizeof(_debug_type), "float4");
  1088. break;
  1089. case GS_SHADER_PARAM_MATRIX4X4:
  1090. snprintf(_debug_type, sizeof(_debug_type), "float4x4");
  1091. break;
  1092. case GS_SHADER_PARAM_BOOL:
  1093. snprintf(_debug_type, sizeof(_debug_type), "bool");
  1094. break;
  1095. case GS_SHADER_PARAM_UNKNOWN:
  1096. snprintf(_debug_type, sizeof(_debug_type), "unknown");
  1097. break;
  1098. case GS_SHADER_PARAM_TEXTURE:
  1099. snprintf(_debug_type, sizeof(_debug_type), "texture");
  1100. break;
  1101. }
  1102. char _debug_buf[4096];
  1103. debug_get_default_value(param, _debug_buf, sizeof(_debug_buf));
  1104. if (param->annotations.num > 0) {
  1105. blog(LOG_DEBUG, "%s[%4lld] %.*s '%s' with value %.*s and %lld annotations:",
  1106. offset,
  1107. idx,
  1108. sizeof(_debug_type), _debug_type,
  1109. param->name,
  1110. sizeof(_debug_buf), _debug_buf,
  1111. param->annotations.num);
  1112. } else {
  1113. blog(LOG_DEBUG, "%s[%4lld] %.*s '%s' with value %.*s.",
  1114. offset,
  1115. idx,
  1116. sizeof(_debug_type), _debug_type,
  1117. param->name,
  1118. sizeof(_debug_buf), _debug_buf);
  1119. }
  1120. }
  1121. static void debug_param_annotation(struct gs_effect_param *param,
  1122. struct ep_param *param_in, unsigned long long idx, const char* offset)
  1123. {
  1124. char _debug_buf[4096];
  1125. debug_get_default_value(param, _debug_buf, sizeof(_debug_buf));
  1126. blog(LOG_DEBUG, "%s[%4lld] %s '%s' with value %.*s",
  1127. offset,
  1128. idx,
  1129. param_in->type,
  1130. param->name,
  1131. sizeof(_debug_buf), _debug_buf);
  1132. }
  1133. static void debug_print_string(const char* offset, const char* str)
  1134. {
  1135. // Bypass 4096 limit in def_log_handler.
  1136. char const *begin = str;
  1137. unsigned long long line = 1;
  1138. for (char const *here = begin; here[0] != '\0'; here++) {
  1139. char const * str = begin;
  1140. unsigned long long len = here - begin;
  1141. bool is_line = false;
  1142. if (here[0] == '\r') {
  1143. is_line = true;
  1144. if (here[1] == '\n') {
  1145. here += 1;
  1146. }
  1147. begin = here + 1;
  1148. } else if (here[0] == '\n') {
  1149. is_line = true;
  1150. begin = here + 1;
  1151. }
  1152. if (is_line) {
  1153. blog(LOG_DEBUG, "\t\t\t\t[%4lld] %.*s", line,
  1154. len, str);
  1155. line++;
  1156. }
  1157. }
  1158. if (begin[0] != '\0') {
  1159. // Final line was not written.
  1160. blog(LOG_DEBUG, "\t\t\t\t[%4lld] %*s", line, strlen(begin), begin);
  1161. }
  1162. }
  1163. #endif
  1164. bool ep_parse(struct effect_parser *ep, gs_effect_t *effect,
  1165. const char *effect_string, const char *file)
  1166. {
  1167. bool success;
  1168. const char *graphics_preprocessor = gs_preprocessor_name();
  1169. if (graphics_preprocessor) {
  1170. struct cf_def def;
  1171. cf_def_init(&def);
  1172. def.name.str.array = graphics_preprocessor;
  1173. def.name.str.len = strlen(graphics_preprocessor);
  1174. strref_copy(&def.name.unmerged_str, &def.name.str);
  1175. cf_preprocessor_add_def(&ep->cfp.pp, &def);
  1176. }
  1177. ep->effect = effect;
  1178. if (!cf_parser_parse(&ep->cfp, effect_string, file))
  1179. return false;
  1180. while (ep->cfp.cur_token && ep->cfp.cur_token->type != CFTOKEN_NONE) {
  1181. if (cf_token_is(&ep->cfp, ";") ||
  1182. is_whitespace(*ep->cfp.cur_token->str.array)) {
  1183. /* do nothing */
  1184. ep->cfp.cur_token++;
  1185. } else if (cf_token_is(&ep->cfp, "struct")) {
  1186. ep_parse_struct(ep);
  1187. } else if (cf_token_is(&ep->cfp, "technique")) {
  1188. ep_parse_technique(ep);
  1189. } else if (cf_token_is(&ep->cfp, "sampler_state")) {
  1190. ep_parse_sampler_state(ep);
  1191. } else if (cf_token_is(&ep->cfp, "{")) {
  1192. /* add error and pass braces */
  1193. cf_adderror(&ep->cfp, "Unexpected code segment",
  1194. LEX_ERROR, NULL, NULL, NULL);
  1195. cf_pass_pair(&ep->cfp, '{', '}');
  1196. } else {
  1197. /* parameters and functions */
  1198. ep_parse_other(ep);
  1199. }
  1200. }
  1201. #if defined(_DEBUG) && defined(_DEBUG_SHADERS)
  1202. blog(LOG_DEBUG, "================================================================================");
  1203. blog(LOG_DEBUG, "Effect Parser reformatted shader '%s' to:", file);
  1204. debug_print_string("\t", ep->cfp.lex.reformatted);
  1205. #endif
  1206. success = !error_data_has_errors(&ep->cfp.error_list);
  1207. if (success)
  1208. success = ep_compile(ep);
  1209. #if defined(_DEBUG) && defined(_DEBUG_SHADERS)
  1210. blog(LOG_DEBUG, "================================================================================");
  1211. #endif
  1212. return success;
  1213. }
  1214. /* ------------------------------------------------------------------------- */
  1215. static inline void ep_write_param(struct dstr *shader, struct ep_param *param,
  1216. struct darray *used_params)
  1217. {
  1218. if (param->written)
  1219. return;
  1220. if (param->is_const) {
  1221. dstr_cat(shader, "const ");
  1222. } else if (param->is_uniform) {
  1223. struct dstr new;
  1224. dstr_init_copy(&new, param->name);
  1225. darray_push_back(sizeof(struct dstr), used_params, &new);
  1226. dstr_cat(shader, "uniform ");
  1227. }
  1228. dstr_cat(shader, param->type);
  1229. dstr_cat(shader, " ");
  1230. dstr_cat(shader, param->name);
  1231. if (param->array_count)
  1232. dstr_catf(shader, "[%u]", param->array_count);
  1233. dstr_cat(shader, ";\n");
  1234. param->written = true;
  1235. }
  1236. static inline void ep_write_func_param_deps(struct effect_parser *ep,
  1237. struct dstr *shader, struct ep_func *func,
  1238. struct darray *used_params)
  1239. {
  1240. size_t i;
  1241. for (i = 0; i < func->param_deps.num; i++) {
  1242. const char *name = func->param_deps.array[i];
  1243. struct ep_param *param = ep_getparam(ep, name);
  1244. ep_write_param(shader, param, used_params);
  1245. }
  1246. if (func->param_deps.num)
  1247. dstr_cat(shader, "\n\n");
  1248. }
  1249. static void ep_write_sampler(struct dstr *shader, struct ep_sampler *sampler)
  1250. {
  1251. size_t i;
  1252. if (sampler->written)
  1253. return;
  1254. dstr_cat(shader, "sampler_state ");
  1255. dstr_cat(shader, sampler->name);
  1256. dstr_cat(shader, " {");
  1257. for (i = 0; i <sampler->values.num; i++) {
  1258. dstr_cat(shader, "\n\t");
  1259. dstr_cat(shader, sampler->states.array[i]);
  1260. dstr_cat(shader, " = ");
  1261. dstr_cat(shader, sampler->values.array[i]);
  1262. dstr_cat(shader, ";\n");
  1263. }
  1264. dstr_cat(shader, "\n};\n");
  1265. sampler->written = true;
  1266. }
  1267. static inline void ep_write_func_sampler_deps(struct effect_parser *ep,
  1268. struct dstr *shader, struct ep_func *func)
  1269. {
  1270. size_t i;
  1271. for (i = 0; i < func->sampler_deps.num; i++) {
  1272. const char *name = func->sampler_deps.array[i];
  1273. struct ep_sampler *sampler = ep_getsampler(ep, name);
  1274. ep_write_sampler(shader, sampler);
  1275. dstr_cat(shader, "\n");
  1276. }
  1277. }
  1278. static inline void ep_write_var(struct dstr *shader, struct ep_var *var)
  1279. {
  1280. if (var->var_type == EP_VAR_INOUT)
  1281. dstr_cat(shader, "inout ");
  1282. else if (var->var_type == EP_VAR_OUT)
  1283. dstr_cat(shader, "out ");
  1284. else if (var->var_type == EP_VAR_UNIFORM)
  1285. dstr_cat(shader, "uniform ");
  1286. // The "in" input modifier is implied by default, so leave it blank
  1287. // in that case.
  1288. dstr_cat(shader, var->type);
  1289. dstr_cat(shader, " ");
  1290. dstr_cat(shader, var->name);
  1291. if (var->mapping) {
  1292. dstr_cat(shader, " : ");
  1293. dstr_cat(shader, var->mapping);
  1294. }
  1295. }
  1296. static void ep_write_struct(struct dstr *shader, struct ep_struct *st)
  1297. {
  1298. size_t i;
  1299. if (st->written)
  1300. return;
  1301. dstr_cat(shader, "struct ");
  1302. dstr_cat(shader, st->name);
  1303. dstr_cat(shader, " {");
  1304. for (i = 0; i < st->vars.num; i++) {
  1305. dstr_cat(shader, "\n\t");
  1306. ep_write_var(shader, st->vars.array+i);
  1307. dstr_cat(shader, ";");
  1308. }
  1309. dstr_cat(shader, "\n};\n");
  1310. st->written = true;
  1311. }
  1312. static inline void ep_write_func_struct_deps(struct effect_parser *ep,
  1313. struct dstr *shader, struct ep_func *func)
  1314. {
  1315. size_t i;
  1316. for (i = 0; i < func->struct_deps.num; i++) {
  1317. const char *name = func->struct_deps.array[i];
  1318. struct ep_struct *st = ep_getstruct(ep, name);
  1319. if (!st->written) {
  1320. ep_write_struct(shader, st);
  1321. dstr_cat(shader, "\n");
  1322. st->written = true;
  1323. }
  1324. }
  1325. }
  1326. static void ep_write_func(struct effect_parser *ep, struct dstr *shader,
  1327. struct ep_func *func, struct darray *used_params);
  1328. static inline void ep_write_func_func_deps(struct effect_parser *ep,
  1329. struct dstr *shader, struct ep_func *func,
  1330. struct darray *used_params)
  1331. {
  1332. size_t i;
  1333. for (i = 0; i < func->func_deps.num; i++) {
  1334. const char *name = func->func_deps.array[i];
  1335. struct ep_func *func_dep = ep_getfunc(ep, name);
  1336. if (!func_dep->written) {
  1337. ep_write_func(ep, shader, func_dep, used_params);
  1338. dstr_cat(shader, "\n\n");
  1339. }
  1340. }
  1341. }
  1342. static void ep_write_func(struct effect_parser *ep, struct dstr *shader,
  1343. struct ep_func *func, struct darray *used_params)
  1344. {
  1345. size_t i;
  1346. func->written = true;
  1347. ep_write_func_param_deps(ep, shader, func, used_params);
  1348. ep_write_func_sampler_deps(ep, shader, func);
  1349. ep_write_func_struct_deps(ep, shader, func);
  1350. ep_write_func_func_deps(ep, shader, func, used_params);
  1351. /* ------------------------------------ */
  1352. dstr_cat(shader, func->ret_type);
  1353. dstr_cat(shader, " ");
  1354. dstr_cat(shader, func->name);
  1355. dstr_cat(shader, "(");
  1356. for (i = 0; i < func->param_vars.num; i++) {
  1357. struct ep_var *var = func->param_vars.array+i;
  1358. if (i)
  1359. dstr_cat(shader, ", ");
  1360. ep_write_var(shader, var);
  1361. }
  1362. dstr_cat(shader, ")\n");
  1363. dstr_cat_dstr(shader, &func->contents);
  1364. dstr_cat(shader, "\n");
  1365. }
  1366. /* writes mapped vars used by the call as parameters for main */
  1367. static void ep_write_main_params(struct effect_parser *ep,
  1368. struct dstr *shader, struct dstr *param_str,
  1369. struct ep_func *func)
  1370. {
  1371. size_t i;
  1372. bool empty_params = dstr_is_empty(param_str);
  1373. for (i = 0; i < func->param_vars.num; i++) {
  1374. struct ep_var *var = func->param_vars.array+i;
  1375. struct ep_struct *st = NULL;
  1376. bool mapped = (var->mapping != NULL);
  1377. if (!mapped) {
  1378. st = ep_getstruct(ep, var->type);
  1379. if (st)
  1380. mapped = ep_struct_mapped(st);
  1381. }
  1382. if (mapped) {
  1383. dstr_cat(shader, var->type);
  1384. dstr_cat(shader, " ");
  1385. dstr_cat(shader, var->name);
  1386. if (!st) {
  1387. dstr_cat(shader, " : ");
  1388. dstr_cat(shader, var->mapping);
  1389. }
  1390. if (!dstr_is_empty(param_str))
  1391. dstr_cat(param_str, ", ");
  1392. dstr_cat(param_str, var->name);
  1393. }
  1394. }
  1395. if (!empty_params)
  1396. dstr_cat(param_str, ", ");
  1397. }
  1398. static void ep_write_main(struct effect_parser *ep, struct dstr *shader,
  1399. struct ep_func *func, struct dstr *call_str)
  1400. {
  1401. struct dstr param_str;
  1402. struct dstr adjusted_call;
  1403. dstr_init(&param_str);
  1404. dstr_init_copy_dstr(&adjusted_call, call_str);
  1405. dstr_cat(shader, "\n");
  1406. dstr_cat(shader, func->ret_type);
  1407. dstr_cat(shader, " main(");
  1408. ep_write_main_params(ep, shader, &param_str, func);
  1409. dstr_cat(shader, ")");
  1410. if (func->mapping) {
  1411. dstr_cat(shader, " : ");
  1412. dstr_cat(shader, func->mapping);
  1413. }
  1414. dstr_cat(shader, "\n{\n\treturn ");
  1415. dstr_cat_dstr(shader, &adjusted_call);
  1416. dstr_cat(shader, "\n}\n");
  1417. dstr_free(&adjusted_call);
  1418. dstr_free(&param_str);
  1419. }
  1420. static inline void ep_reset_written(struct effect_parser *ep)
  1421. {
  1422. size_t i;
  1423. for (i = 0; i <ep->params.num; i++)
  1424. ep->params.array[i].written = false;
  1425. for (i = 0; i <ep->structs.num; i++)
  1426. ep->structs.array[i].written = false;
  1427. for (i = 0; i <ep->funcs.num; i++)
  1428. ep->funcs.array[i].written = false;
  1429. for (i = 0; i <ep->samplers.num; i++)
  1430. ep->samplers.array[i].written = false;
  1431. }
  1432. static void ep_makeshaderstring(struct effect_parser *ep,
  1433. struct dstr *shader, struct darray *shader_call,
  1434. struct darray *used_params)
  1435. {
  1436. struct cf_token *token = shader_call->array;
  1437. struct cf_token *func_name;
  1438. struct ep_func *func;
  1439. struct dstr call_str;
  1440. dstr_init(&call_str);
  1441. if (!token)
  1442. return;
  1443. while (token->type != CFTOKEN_NONE && is_whitespace(*token->str.array))
  1444. token++;
  1445. if (token->type == CFTOKEN_NONE ||
  1446. strref_cmp(&token->str, "NULL") == 0)
  1447. return;
  1448. func_name = token;
  1449. while (token->type != CFTOKEN_NONE) {
  1450. struct ep_param *param = ep_getparam_strref(ep, &token->str);
  1451. if (param)
  1452. ep_write_param(shader, param, used_params);
  1453. dstr_cat_strref(&call_str, &token->str);
  1454. token++;
  1455. }
  1456. func = ep_getfunc_strref(ep, &func_name->str);
  1457. if (!func)
  1458. return;
  1459. ep_write_func(ep, shader, func, used_params);
  1460. ep_write_main(ep, shader, func, &call_str);
  1461. dstr_free(&call_str);
  1462. ep_reset_written(ep);
  1463. }
  1464. static void ep_compile_annotations(struct darray *ep_annotations,
  1465. struct darray *gsp_annotations, struct effect_parser *ep)
  1466. {
  1467. darray_resize(sizeof(struct gs_effect_param),
  1468. gsp_annotations, ep_annotations->num);
  1469. size_t i;
  1470. for (i = 0; i < ep_annotations->num; i++) {
  1471. struct gs_effect_param *param = ((struct gs_effect_param *)
  1472. gsp_annotations->array)+i;
  1473. struct ep_param *param_in = ((struct ep_param *)
  1474. ep_annotations->array)+i;
  1475. param->name = bstrdup(param_in->name);
  1476. param->section = EFFECT_ANNOTATION;
  1477. param->effect = ep->effect;
  1478. da_move(param->default_val, param_in->default_val);
  1479. param->type = get_effect_param_type(param_in->type);
  1480. #if defined(_DEBUG) && defined(_DEBUG_SHADERS)
  1481. debug_param(param, param_in, i, "\t\t");
  1482. #endif
  1483. }
  1484. }
  1485. static void ep_compile_param_annotations(struct ep_param *ep_param_input,
  1486. struct gs_effect_param *gs_effect_input, struct effect_parser *ep)
  1487. {
  1488. ep_compile_annotations(&(ep_param_input->annotations.da),
  1489. &(gs_effect_input->annotations.da), ep);
  1490. }
  1491. static void ep_compile_param(struct effect_parser *ep, size_t idx)
  1492. {
  1493. struct gs_effect_param *param;
  1494. struct ep_param *param_in;
  1495. param = ep->effect->params.array+idx;
  1496. param_in = ep->params.array+idx;
  1497. param_in->param = param;
  1498. param->name = bstrdup(param_in->name);
  1499. param->section = EFFECT_PARAM;
  1500. param->effect = ep->effect;
  1501. da_move(param->default_val, param_in->default_val);
  1502. param->type = get_effect_param_type(param_in->type);
  1503. if (strcmp(param_in->name, "ViewProj") == 0)
  1504. ep->effect->view_proj = param;
  1505. else if (strcmp(param_in->name, "World") == 0)
  1506. ep->effect->world = param;
  1507. #if defined(_DEBUG) && defined(_DEBUG_SHADERS)
  1508. debug_param(param, param_in, idx, "\t");
  1509. #endif
  1510. ep_compile_param_annotations(param_in, param, ep);
  1511. }
  1512. static bool ep_compile_pass_shaderparams(struct effect_parser *ep,
  1513. struct darray *pass_params, struct darray *used_params,
  1514. gs_shader_t *shader)
  1515. {
  1516. size_t i;
  1517. darray_resize(sizeof(struct pass_shaderparam), pass_params,
  1518. used_params->num);
  1519. for (i = 0; i < pass_params->num; i++) {
  1520. struct dstr *param_name;
  1521. struct pass_shaderparam *param;
  1522. param_name = darray_item(sizeof(struct dstr), used_params, i);
  1523. param = darray_item(sizeof(struct pass_shaderparam),
  1524. pass_params, i);
  1525. param->eparam = gs_effect_get_param_by_name(ep->effect,
  1526. param_name->array);
  1527. param->sparam = gs_shader_get_param_by_name(shader,
  1528. param_name->array);
  1529. #if defined(_DEBUG) && defined(_DEBUG_SHADERS)
  1530. debug_param(param->eparam, 0, i, "\t\t\t\t");
  1531. #endif
  1532. if (!param->sparam) {
  1533. blog(LOG_ERROR, "Effect shader parameter not found");
  1534. return false;
  1535. }
  1536. }
  1537. return true;
  1538. }
  1539. static inline bool ep_compile_pass_shader(struct effect_parser *ep,
  1540. struct gs_effect_technique *tech,
  1541. struct gs_effect_pass *pass, struct ep_pass *pass_in,
  1542. size_t pass_idx, enum gs_shader_type type)
  1543. {
  1544. struct dstr shader_str;
  1545. struct dstr location;
  1546. struct darray used_params; /* struct dstr */
  1547. struct darray *pass_params = NULL; /* struct pass_shaderparam */
  1548. gs_shader_t *shader = NULL;
  1549. bool success = true;
  1550. dstr_init(&shader_str);
  1551. darray_init(&used_params);
  1552. dstr_init(&location);
  1553. dstr_copy(&location, ep->cfp.lex.file);
  1554. if (type == GS_SHADER_VERTEX)
  1555. dstr_cat(&location, " (Vertex ");
  1556. else if (type == GS_SHADER_PIXEL)
  1557. dstr_cat(&location, " (Pixel ");
  1558. /*else if (type == SHADER_GEOMETRY)
  1559. dstr_cat(&location, " (Geometry ");*/
  1560. assert(pass_idx <= UINT_MAX);
  1561. dstr_catf(&location, "shader, technique %s, pass %u)", tech->name,
  1562. (unsigned)pass_idx);
  1563. if (type == GS_SHADER_VERTEX) {
  1564. ep_makeshaderstring(ep, &shader_str,
  1565. &pass_in->vertex_program.da, &used_params);
  1566. pass->vertshader = gs_vertexshader_create(shader_str.array,
  1567. location.array, NULL);
  1568. shader = pass->vertshader;
  1569. pass_params = &pass->vertshader_params.da;
  1570. } else if (type == GS_SHADER_PIXEL) {
  1571. ep_makeshaderstring(ep, &shader_str,
  1572. &pass_in->fragment_program.da, &used_params);
  1573. pass->pixelshader = gs_pixelshader_create(shader_str.array,
  1574. location.array, NULL);
  1575. shader = pass->pixelshader;
  1576. pass_params = &pass->pixelshader_params.da;
  1577. }
  1578. #if defined(_DEBUG) && defined(_DEBUG_SHADERS)
  1579. blog(LOG_DEBUG, "\t\t\t%s Shader:",
  1580. type == GS_SHADER_VERTEX ? "Vertex" : "Fragment");
  1581. blog(LOG_DEBUG, "\t\t\tCode:");
  1582. debug_print_string("\t\t\t\t\t", shader_str.array);
  1583. blog(LOG_DEBUG, "\t\t\tParameters:");
  1584. #endif
  1585. if (shader)
  1586. success = ep_compile_pass_shaderparams(ep, pass_params,
  1587. &used_params, shader);
  1588. else
  1589. success = false;
  1590. dstr_free(&location);
  1591. dstr_array_free(used_params.array, used_params.num);
  1592. darray_free(&used_params);
  1593. dstr_free(&shader_str);
  1594. return success;
  1595. }
  1596. static bool ep_compile_pass(struct effect_parser *ep,
  1597. struct gs_effect_technique *tech,
  1598. struct ep_technique *tech_in,
  1599. size_t idx)
  1600. {
  1601. struct gs_effect_pass *pass;
  1602. struct ep_pass *pass_in;
  1603. bool success = true;
  1604. pass = tech->passes.array+idx;
  1605. pass_in = tech_in->passes.array+idx;
  1606. pass->name = bstrdup(pass_in->name);
  1607. pass->section = EFFECT_PASS;
  1608. #if defined(_DEBUG) && defined(_DEBUG_SHADERS)
  1609. blog(LOG_DEBUG, "\t\t[%4lld] Pass '%s':",
  1610. idx, pass->name);
  1611. #endif
  1612. if (!ep_compile_pass_shader(ep, tech, pass, pass_in, idx,
  1613. GS_SHADER_VERTEX)) {
  1614. success = false;
  1615. blog(LOG_ERROR, "Pass (%zu) <%s> missing vertex shader!",
  1616. idx, pass->name ? pass->name : "");
  1617. }
  1618. if (!ep_compile_pass_shader(ep, tech, pass, pass_in, idx,
  1619. GS_SHADER_PIXEL)) {
  1620. success = false;
  1621. blog(LOG_ERROR, "Pass (%zu) <%s> missing pixel shader!",
  1622. idx, pass->name ? pass->name : "");
  1623. }
  1624. return success;
  1625. }
  1626. static inline bool ep_compile_technique(struct effect_parser *ep, size_t idx)
  1627. {
  1628. struct gs_effect_technique *tech;
  1629. struct ep_technique *tech_in;
  1630. bool success = true;
  1631. size_t i;
  1632. tech = ep->effect->techniques.array+idx;
  1633. tech_in = ep->techniques.array+idx;
  1634. tech->name = bstrdup(tech_in->name);
  1635. tech->section = EFFECT_TECHNIQUE;
  1636. tech->effect = ep->effect;
  1637. da_resize(tech->passes, tech_in->passes.num);
  1638. #if defined(_DEBUG) && defined(_DEBUG_SHADERS)
  1639. blog(LOG_DEBUG, "\t[%4lld] Technique '%s' has %lld passes:",
  1640. idx, tech->name, tech->passes.num);
  1641. #endif
  1642. for (i = 0; i < tech->passes.num; i++) {
  1643. if (!ep_compile_pass(ep, tech, tech_in, i))
  1644. success = false;
  1645. }
  1646. return success;
  1647. }
  1648. static bool ep_compile(struct effect_parser *ep)
  1649. {
  1650. bool success = true;
  1651. size_t i;
  1652. assert(ep->effect);
  1653. da_resize(ep->effect->params, ep->params.num);
  1654. da_resize(ep->effect->techniques, ep->techniques.num);
  1655. #if defined(_DEBUG) && defined(_DEBUG_SHADERS)
  1656. blog(LOG_DEBUG, "Shader has %lld parameters:", ep->params.num);
  1657. #endif
  1658. for (i = 0; i < ep->params.num; i++)
  1659. ep_compile_param(ep, i);
  1660. #if defined(_DEBUG) && defined(_DEBUG_SHADERS)
  1661. blog(LOG_DEBUG, "Shader has %lld techniques:", ep->techniques.num);
  1662. #endif
  1663. for (i = 0; i < ep->techniques.num; i++) {
  1664. if (!ep_compile_technique(ep, i))
  1665. success = false;
  1666. }
  1667. return success;
  1668. }