effect-parser.c 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092
  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))
  167. return PARSE_EOF;
  168. if (cf_token_is(&ep->cfp, ";"))
  169. return PARSE_CONTINUE;
  170. if (cf_token_is(&ep->cfp, "}"))
  171. return PARSE_BREAK;
  172. code = cf_token_is_type(&ep->cfp, CFTOKEN_NAME, "type name", ";");
  173. if (code != PARSE_SUCCESS)
  174. return code;
  175. cf_copy_token(&ep->cfp, &var->type);
  176. /* -------------------------------------- */
  177. /* variable name */
  178. if (!cf_next_valid_token(&ep->cfp))
  179. return PARSE_EOF;
  180. if (cf_token_is(&ep->cfp, ";"))
  181. return PARSE_UNEXPECTED_CONTINUE;
  182. if (cf_token_is(&ep->cfp, "}"))
  183. return PARSE_UNEXPECTED_BREAK;
  184. code = cf_token_is_type(&ep->cfp, CFTOKEN_NAME, "variable name", ";");
  185. if (code != PARSE_SUCCESS)
  186. return code;
  187. cf_copy_token(&ep->cfp, &var->name);
  188. /* -------------------------------------- */
  189. /* variable mapping if any (POSITION, TEXCOORD, etc) */
  190. if (!cf_next_valid_token(&ep->cfp))
  191. return PARSE_EOF;
  192. if (cf_token_is(&ep->cfp, ":")) {
  193. if (!cf_next_valid_token(&ep->cfp))
  194. return PARSE_EOF;
  195. if (cf_token_is(&ep->cfp, ";"))
  196. return PARSE_UNEXPECTED_CONTINUE;
  197. if (cf_token_is(&ep->cfp, "}"))
  198. return PARSE_UNEXPECTED_BREAK;
  199. code = cf_token_is_type(&ep->cfp, CFTOKEN_NAME, "mapping name",
  200. ";");
  201. if (code != PARSE_SUCCESS)
  202. return code;
  203. cf_copy_token(&ep->cfp, &var->mapping);
  204. if (!cf_next_valid_token(&ep->cfp))
  205. return PARSE_EOF;
  206. }
  207. /* -------------------------------------- */
  208. if (!cf_token_is(&ep->cfp, ";")) {
  209. if (!cf_go_to_valid_token(&ep->cfp, ";", "}"))
  210. return PARSE_EOF;
  211. return PARSE_CONTINUE;
  212. }
  213. return PARSE_SUCCESS;
  214. }
  215. static void ep_parse_struct(struct effect_parser *ep)
  216. {
  217. struct ep_struct eps;
  218. ep_struct_init(&eps);
  219. if (cf_next_name(&ep->cfp, &eps.name, "name", ";") != PARSE_SUCCESS)
  220. goto error;
  221. if (cf_next_token_should_be(&ep->cfp, "{", ";", NULL) != PARSE_SUCCESS)
  222. goto error;
  223. /* get structure variables */
  224. while (true) {
  225. bool do_break = false;
  226. struct ep_var var;
  227. ep_var_init(&var);
  228. switch (ep_parse_struct_var(ep, &var)) {
  229. case PARSE_UNEXPECTED_CONTINUE:
  230. cf_adderror_syntax_error(&ep->cfp);
  231. /* Falls through. */
  232. case PARSE_CONTINUE:
  233. ep_var_free(&var);
  234. continue;
  235. case PARSE_UNEXPECTED_BREAK:
  236. cf_adderror_syntax_error(&ep->cfp);
  237. /* Falls through. */
  238. case PARSE_BREAK:
  239. ep_var_free(&var);
  240. do_break = true;
  241. break;
  242. case PARSE_EOF:
  243. ep_var_free(&var);
  244. goto error;
  245. }
  246. if (do_break)
  247. break;
  248. da_push_back(eps.vars, &var);
  249. }
  250. if (cf_next_token_should_be(&ep->cfp, ";", NULL, NULL) != PARSE_SUCCESS)
  251. goto error;
  252. da_push_back(ep->structs, &eps);
  253. return;
  254. error:
  255. ep_struct_free(&eps);
  256. }
  257. static inline int ep_parse_param_annotation_var(struct effect_parser *ep,
  258. struct ep_param *var)
  259. {
  260. int code;
  261. /* -------------------------------------- */
  262. /* variable type */
  263. if (!cf_next_valid_token(&ep->cfp))
  264. return PARSE_EOF;
  265. if (cf_token_is(&ep->cfp, ";"))
  266. return PARSE_CONTINUE;
  267. if (cf_token_is(&ep->cfp, ">"))
  268. return PARSE_BREAK;
  269. code = cf_token_is_type(&ep->cfp, CFTOKEN_NAME, "type name", ";");
  270. if (code != PARSE_SUCCESS)
  271. return code;
  272. bfree(var->type);
  273. cf_copy_token(&ep->cfp, &var->type);
  274. /* -------------------------------------- */
  275. /* variable name */
  276. if (!cf_next_valid_token(&ep->cfp))
  277. return PARSE_EOF;
  278. if (cf_token_is(&ep->cfp, ";")) {
  279. cf_adderror_expecting(&ep->cfp, "variable name");
  280. return PARSE_UNEXPECTED_CONTINUE;
  281. }
  282. if (cf_token_is(&ep->cfp, ">")) {
  283. cf_adderror_expecting(&ep->cfp, "variable name");
  284. return PARSE_UNEXPECTED_BREAK;
  285. }
  286. code = cf_token_is_type(&ep->cfp, CFTOKEN_NAME, "variable name", ";");
  287. if (code != PARSE_SUCCESS)
  288. return code;
  289. bfree(var->name);
  290. cf_copy_token(&ep->cfp, &var->name);
  291. /* -------------------------------------- */
  292. /* variable mapping if any (POSITION, TEXCOORD, etc) */
  293. if (!cf_next_valid_token(&ep->cfp))
  294. return PARSE_EOF;
  295. if (cf_token_is(&ep->cfp, ":")) {
  296. cf_adderror_expecting(&ep->cfp, "= or ;");
  297. return PARSE_UNEXPECTED_BREAK;
  298. } else if (cf_token_is(&ep->cfp, ">")) {
  299. cf_adderror_expecting(&ep->cfp, "= or ;");
  300. return PARSE_UNEXPECTED_BREAK;
  301. } else if (cf_token_is(&ep->cfp, "=")) {
  302. if (!ep_parse_param_assign(ep, var)) {
  303. cf_adderror_expecting(&ep->cfp, "assignment value");
  304. return PARSE_UNEXPECTED_BREAK;
  305. }
  306. }
  307. /* -------------------------------------- */
  308. if (!cf_token_is(&ep->cfp, ";")) {
  309. if (!cf_go_to_valid_token(&ep->cfp, ";", ">")) {
  310. cf_adderror_expecting(&ep->cfp, "; or >");
  311. return PARSE_EOF;
  312. }
  313. return PARSE_CONTINUE;
  314. }
  315. return PARSE_SUCCESS;
  316. }
  317. static int ep_parse_annotations(struct effect_parser *ep,
  318. struct darray *annotations)
  319. {
  320. if (!cf_token_is(&ep->cfp, "<")) {
  321. cf_adderror_expecting(&ep->cfp, "<");
  322. goto error;
  323. }
  324. /* get annotation variables */
  325. while (true) {
  326. bool do_break = false;
  327. struct ep_param var;
  328. ep_param_init(&var, bstrdup(""), bstrdup(""), false, false,
  329. false);
  330. switch (ep_parse_param_annotation_var(ep, &var)) {
  331. case PARSE_UNEXPECTED_CONTINUE:
  332. cf_adderror_syntax_error(&ep->cfp);
  333. /* Falls through. */
  334. case PARSE_CONTINUE:
  335. ep_param_free(&var);
  336. continue;
  337. case PARSE_UNEXPECTED_BREAK:
  338. cf_adderror_syntax_error(&ep->cfp);
  339. /* Falls through. */
  340. case PARSE_BREAK:
  341. ep_param_free(&var);
  342. do_break = true;
  343. break;
  344. case PARSE_EOF:
  345. ep_param_free(&var);
  346. goto error;
  347. }
  348. if (do_break)
  349. break;
  350. darray_push_back(sizeof(struct ep_param), annotations, &var);
  351. }
  352. if (!cf_token_is(&ep->cfp, ">")) {
  353. cf_adderror_expecting(&ep->cfp, ">");
  354. goto error;
  355. }
  356. if (!cf_next_valid_token(&ep->cfp))
  357. goto error;
  358. return true;
  359. error:
  360. return false;
  361. }
  362. static int ep_parse_param_annotations(struct effect_parser *ep,
  363. struct ep_param *param)
  364. {
  365. return ep_parse_annotations(ep, &param->annotations.da);
  366. }
  367. static inline int ep_parse_pass_command_call(struct effect_parser *ep,
  368. struct darray *call)
  369. {
  370. struct cf_token end_token;
  371. cf_token_clear(&end_token);
  372. while (!cf_token_is(&ep->cfp, ";")) {
  373. if (cf_token_is(&ep->cfp, "}")) {
  374. cf_adderror_expecting(&ep->cfp, ";");
  375. return PARSE_CONTINUE;
  376. }
  377. darray_push_back(sizeof(struct cf_token), call,
  378. ep->cfp.cur_token);
  379. if (!cf_next_valid_token(&ep->cfp))
  380. return PARSE_EOF;
  381. }
  382. darray_push_back(sizeof(struct cf_token), call, ep->cfp.cur_token);
  383. darray_push_back(sizeof(struct cf_token), call, &end_token);
  384. return PARSE_SUCCESS;
  385. }
  386. static int ep_parse_pass_command(struct effect_parser *ep, struct ep_pass *pass)
  387. {
  388. struct darray *call; /* struct cf_token */
  389. if (!cf_next_valid_token(&ep->cfp))
  390. return PARSE_EOF;
  391. if (cf_token_is(&ep->cfp, "vertex_shader") ||
  392. cf_token_is(&ep->cfp, "vertex_program")) {
  393. call = &pass->vertex_program.da;
  394. } else if (cf_token_is(&ep->cfp, "pixel_shader") ||
  395. cf_token_is(&ep->cfp, "pixel_program")) {
  396. call = &pass->fragment_program.da;
  397. } else {
  398. cf_adderror_syntax_error(&ep->cfp);
  399. if (!cf_go_to_valid_token(&ep->cfp, ";", "}"))
  400. return PARSE_EOF;
  401. return PARSE_CONTINUE;
  402. }
  403. if (cf_next_token_should_be(&ep->cfp, "=", ";", "}") != PARSE_SUCCESS)
  404. return PARSE_CONTINUE;
  405. if (!cf_next_valid_token(&ep->cfp))
  406. return PARSE_EOF;
  407. if (cf_token_is(&ep->cfp, "compile")) {
  408. cf_adderror(&ep->cfp, "compile keyword not necessary",
  409. LEX_WARNING, NULL, NULL, NULL);
  410. if (!cf_next_valid_token(&ep->cfp))
  411. return PARSE_EOF;
  412. }
  413. return ep_parse_pass_command_call(ep, call);
  414. }
  415. static int ep_parse_pass(struct effect_parser *ep, struct ep_pass *pass)
  416. {
  417. struct cf_token peek;
  418. if (!cf_token_is(&ep->cfp, "pass"))
  419. return PARSE_UNEXPECTED_CONTINUE;
  420. if (!cf_next_valid_token(&ep->cfp))
  421. return PARSE_EOF;
  422. if (!cf_token_is(&ep->cfp, "{")) {
  423. pass->name = bstrdup_n(ep->cfp.cur_token->str.array,
  424. ep->cfp.cur_token->str.len);
  425. if (!cf_next_valid_token(&ep->cfp))
  426. return PARSE_EOF;
  427. }
  428. if (!cf_peek_valid_token(&ep->cfp, &peek))
  429. return PARSE_EOF;
  430. while (strref_cmp(&peek.str, "}") != 0) {
  431. int ret = ep_parse_pass_command(ep, pass);
  432. if (ret < 0 && ret != PARSE_CONTINUE)
  433. return ret;
  434. if (!cf_peek_valid_token(&ep->cfp, &peek))
  435. return PARSE_EOF;
  436. }
  437. /* token is '}' */
  438. cf_next_token(&ep->cfp);
  439. return PARSE_SUCCESS;
  440. }
  441. static void ep_parse_technique(struct effect_parser *ep)
  442. {
  443. struct ep_technique ept;
  444. ep_technique_init(&ept);
  445. if (cf_next_name(&ep->cfp, &ept.name, "name", ";") != PARSE_SUCCESS)
  446. goto error;
  447. if (!cf_next_valid_token(&ep->cfp))
  448. return;
  449. if (!cf_token_is(&ep->cfp, "{")) {
  450. if (!cf_go_to_token(&ep->cfp, ";", NULL)) {
  451. cf_adderror_expecting(&ep->cfp, ";");
  452. return;
  453. }
  454. cf_adderror_expecting(&ep->cfp, "{");
  455. goto error;
  456. }
  457. if (!cf_next_valid_token(&ep->cfp))
  458. goto error;
  459. while (!cf_token_is(&ep->cfp, "}")) {
  460. struct ep_pass pass;
  461. ep_pass_init(&pass);
  462. switch (ep_parse_pass(ep, &pass)) {
  463. case PARSE_UNEXPECTED_CONTINUE:
  464. ep_pass_free(&pass);
  465. if (!cf_go_to_token(&ep->cfp, "}", NULL))
  466. goto error;
  467. continue;
  468. case PARSE_EOF:
  469. ep_pass_free(&pass);
  470. goto error;
  471. }
  472. da_push_back(ept.passes, &pass);
  473. if (!cf_next_valid_token(&ep->cfp))
  474. goto error;
  475. }
  476. /* pass the current token (which is '}') if we reached here */
  477. cf_next_token(&ep->cfp);
  478. da_push_back(ep->techniques, &ept);
  479. return;
  480. error:
  481. cf_next_token(&ep->cfp);
  482. ep_technique_free(&ept);
  483. }
  484. static int ep_parse_sampler_state_item(struct effect_parser *ep,
  485. struct ep_sampler *eps)
  486. {
  487. int ret;
  488. char *state = NULL;
  489. struct dstr value = {0};
  490. ret = cf_next_name(&ep->cfp, &state, "state name", ";");
  491. if (ret != PARSE_SUCCESS)
  492. goto fail;
  493. ret = cf_next_token_should_be(&ep->cfp, "=", ";", NULL);
  494. if (ret != PARSE_SUCCESS)
  495. goto fail;
  496. for (;;) {
  497. const char *cur_str;
  498. if (!cf_next_valid_token(&ep->cfp))
  499. return PARSE_EOF;
  500. cur_str = ep->cfp.cur_token->str.array;
  501. if (*cur_str == ';')
  502. break;
  503. dstr_ncat(&value, cur_str, ep->cfp.cur_token->str.len);
  504. }
  505. if (value.len) {
  506. da_push_back(eps->states, &state);
  507. da_push_back(eps->values, &value.array);
  508. }
  509. return ret;
  510. fail:
  511. bfree(state);
  512. dstr_free(&value);
  513. return ret;
  514. }
  515. static void ep_parse_sampler_state(struct effect_parser *ep)
  516. {
  517. struct ep_sampler eps;
  518. struct cf_token peek;
  519. ep_sampler_init(&eps);
  520. if (cf_next_name(&ep->cfp, &eps.name, "name", ";") != PARSE_SUCCESS)
  521. goto error;
  522. if (cf_next_token_should_be(&ep->cfp, "{", ";", NULL) != PARSE_SUCCESS)
  523. goto error;
  524. if (!cf_peek_valid_token(&ep->cfp, &peek))
  525. goto error;
  526. while (strref_cmp(&peek.str, "}") != 0) {
  527. int ret = ep_parse_sampler_state_item(ep, &eps);
  528. if (ret == PARSE_EOF)
  529. goto error;
  530. if (!cf_peek_valid_token(&ep->cfp, &peek))
  531. goto error;
  532. }
  533. if (cf_next_token_should_be(&ep->cfp, "}", ";", NULL) != PARSE_SUCCESS)
  534. goto error;
  535. if (cf_next_token_should_be(&ep->cfp, ";", NULL, NULL) != PARSE_SUCCESS)
  536. goto error;
  537. da_push_back(ep->samplers, &eps);
  538. return;
  539. error:
  540. ep_sampler_free(&eps);
  541. }
  542. static inline int ep_check_for_keyword(struct effect_parser *ep,
  543. const char *keyword, bool *val)
  544. {
  545. bool new_val = cf_token_is(&ep->cfp, keyword);
  546. if (new_val) {
  547. if (!cf_next_valid_token(&ep->cfp))
  548. return PARSE_EOF;
  549. if (new_val && *val)
  550. cf_adderror(&ep->cfp, "'$1' keyword already specified",
  551. LEX_WARNING, keyword, NULL, NULL);
  552. *val = new_val;
  553. return PARSE_CONTINUE;
  554. }
  555. return PARSE_SUCCESS;
  556. }
  557. static inline int ep_parse_func_param(struct effect_parser *ep,
  558. struct ep_func *func, struct ep_var *var)
  559. {
  560. int code;
  561. bool var_type_keyword = false;
  562. if (!cf_next_valid_token(&ep->cfp))
  563. return PARSE_EOF;
  564. code = ep_check_for_keyword(ep, "in", &var_type_keyword);
  565. if (code == PARSE_EOF)
  566. return PARSE_EOF;
  567. else if (var_type_keyword)
  568. var->var_type = EP_VAR_IN;
  569. if (!var_type_keyword) {
  570. code = ep_check_for_keyword(ep, "inout", &var_type_keyword);
  571. if (code == PARSE_EOF)
  572. return PARSE_EOF;
  573. else if (var_type_keyword)
  574. var->var_type = EP_VAR_INOUT;
  575. }
  576. if (!var_type_keyword) {
  577. code = ep_check_for_keyword(ep, "out", &var_type_keyword);
  578. if (code == PARSE_EOF)
  579. return PARSE_EOF;
  580. else if (var_type_keyword)
  581. var->var_type = EP_VAR_OUT;
  582. }
  583. if (!var_type_keyword) {
  584. code = ep_check_for_keyword(ep, "uniform", &var_type_keyword);
  585. if (code == PARSE_EOF)
  586. return PARSE_EOF;
  587. else if (var_type_keyword)
  588. var->var_type = EP_VAR_UNIFORM;
  589. }
  590. code = cf_get_name(&ep->cfp, &var->type, "type", ")");
  591. if (code != PARSE_SUCCESS)
  592. return code;
  593. code = cf_next_name(&ep->cfp, &var->name, "name", ")");
  594. if (code != PARSE_SUCCESS)
  595. return code;
  596. if (!cf_next_valid_token(&ep->cfp))
  597. return PARSE_EOF;
  598. if (cf_token_is(&ep->cfp, ":")) {
  599. code = cf_next_name(&ep->cfp, &var->mapping,
  600. "mapping specifier", ")");
  601. if (code != PARSE_SUCCESS)
  602. return code;
  603. if (!cf_next_valid_token(&ep->cfp))
  604. return PARSE_EOF;
  605. }
  606. if (ep_getstruct(ep, var->type) != NULL)
  607. da_push_back(func->struct_deps, &var->type);
  608. else if (ep_getsampler(ep, var->type) != NULL)
  609. da_push_back(func->sampler_deps, &var->type);
  610. return PARSE_SUCCESS;
  611. }
  612. static bool ep_parse_func_params(struct effect_parser *ep, struct ep_func *func)
  613. {
  614. struct cf_token peek;
  615. int code;
  616. cf_token_clear(&peek);
  617. if (!cf_peek_valid_token(&ep->cfp, &peek))
  618. return false;
  619. if (*peek.str.array == ')') {
  620. cf_next_token(&ep->cfp);
  621. goto exit;
  622. }
  623. do {
  624. struct ep_var var;
  625. ep_var_init(&var);
  626. if (!cf_token_is(&ep->cfp, "(") && !cf_token_is(&ep->cfp, ","))
  627. cf_adderror_syntax_error(&ep->cfp);
  628. code = ep_parse_func_param(ep, func, &var);
  629. if (code != PARSE_SUCCESS) {
  630. ep_var_free(&var);
  631. if (code == PARSE_CONTINUE)
  632. goto exit;
  633. else if (code == PARSE_EOF)
  634. return false;
  635. }
  636. da_push_back(func->param_vars, &var);
  637. } while (!cf_token_is(&ep->cfp, ")"));
  638. exit:
  639. return true;
  640. }
  641. static inline bool ep_process_struct_dep(struct effect_parser *ep,
  642. struct ep_func *func)
  643. {
  644. struct ep_struct *val =
  645. ep_getstruct_strref(ep, &ep->cfp.cur_token->str);
  646. if (val)
  647. da_push_back(func->struct_deps, &val->name);
  648. return val != NULL;
  649. }
  650. static inline bool ep_process_func_dep(struct effect_parser *ep,
  651. struct ep_func *func)
  652. {
  653. struct ep_func *val = ep_getfunc_strref(ep, &ep->cfp.cur_token->str);
  654. if (val)
  655. da_push_back(func->func_deps, &val->name);
  656. return val != NULL;
  657. }
  658. static inline bool ep_process_sampler_dep(struct effect_parser *ep,
  659. struct ep_func *func)
  660. {
  661. struct ep_sampler *val =
  662. ep_getsampler_strref(ep, &ep->cfp.cur_token->str);
  663. if (val)
  664. da_push_back(func->sampler_deps, &val->name);
  665. return val != NULL;
  666. }
  667. static inline bool ep_process_param_dep(struct effect_parser *ep,
  668. struct ep_func *func)
  669. {
  670. struct ep_param *val = ep_getparam_strref(ep, &ep->cfp.cur_token->str);
  671. if (val)
  672. da_push_back(func->param_deps, &val->name);
  673. return val != NULL;
  674. }
  675. static inline bool ep_parse_func_contents(struct effect_parser *ep,
  676. struct ep_func *func)
  677. {
  678. int braces = 1;
  679. dstr_cat_strref(&func->contents, &ep->cfp.cur_token->str);
  680. while (braces > 0) {
  681. if ((ep->cfp.cur_token++)->type == CFTOKEN_NONE)
  682. return false;
  683. if (ep->cfp.cur_token->type == CFTOKEN_SPACETAB ||
  684. ep->cfp.cur_token->type == CFTOKEN_NEWLINE) {
  685. } else if (cf_token_is(&ep->cfp, "{")) {
  686. braces++;
  687. } else if (cf_token_is(&ep->cfp, "}")) {
  688. braces--;
  689. } else if (ep_process_struct_dep(ep, func) ||
  690. ep_process_func_dep(ep, func) ||
  691. ep_process_sampler_dep(ep, func) ||
  692. ep_process_param_dep(ep, func)) {
  693. }
  694. dstr_cat_strref(&func->contents, &ep->cfp.cur_token->str);
  695. }
  696. return true;
  697. }
  698. static void ep_parse_function(struct effect_parser *ep, char *type, char *name)
  699. {
  700. struct ep_func func;
  701. int code;
  702. ep_func_init(&func, type, name);
  703. if (ep_getstruct(ep, type))
  704. da_push_back(func.struct_deps, &func.ret_type);
  705. if (!ep_parse_func_params(ep, &func))
  706. goto error;
  707. if (!cf_next_valid_token(&ep->cfp))
  708. goto error;
  709. /* if function is mapped to something, for example COLOR */
  710. if (cf_token_is(&ep->cfp, ":")) {
  711. code = cf_next_name(&ep->cfp, &func.mapping,
  712. "mapping specifier", "{");
  713. if (code == PARSE_EOF)
  714. goto error;
  715. else if (code != PARSE_CONTINUE) {
  716. if (!cf_next_valid_token(&ep->cfp))
  717. goto error;
  718. }
  719. }
  720. if (!cf_token_is(&ep->cfp, "{")) {
  721. cf_adderror_expecting(&ep->cfp, "{");
  722. goto error;
  723. }
  724. if (!ep_parse_func_contents(ep, &func))
  725. goto error;
  726. /* it is established that the current token is '}' if we reach this */
  727. cf_next_token(&ep->cfp);
  728. da_push_back(ep->funcs, &func);
  729. return;
  730. error:
  731. ep_func_free(&func);
  732. }
  733. /* parses "array[count]" */
  734. static bool ep_parse_param_array(struct effect_parser *ep,
  735. struct ep_param *param)
  736. {
  737. if (!cf_next_valid_token(&ep->cfp))
  738. return false;
  739. if (ep->cfp.cur_token->type != CFTOKEN_NUM ||
  740. !valid_int_str(ep->cfp.cur_token->str.array,
  741. ep->cfp.cur_token->str.len))
  742. return false;
  743. param->array_count =
  744. (int)strtol(ep->cfp.cur_token->str.array, NULL, 10);
  745. if (cf_next_token_should_be(&ep->cfp, "]", ";", NULL) == PARSE_EOF)
  746. return false;
  747. if (!cf_next_valid_token(&ep->cfp))
  748. return false;
  749. return true;
  750. }
  751. static inline int ep_parse_param_assign_texture(struct effect_parser *ep,
  752. struct ep_param *param)
  753. {
  754. int code;
  755. char *str;
  756. if (!cf_next_valid_token(&ep->cfp))
  757. return PARSE_EOF;
  758. code = cf_token_is_type(&ep->cfp, CFTOKEN_STRING, "texture path string",
  759. ";");
  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_string(struct effect_parser *ep,
  771. struct ep_param *param)
  772. {
  773. int code;
  774. char *str = NULL;
  775. if (!cf_next_valid_token(&ep->cfp))
  776. return PARSE_EOF;
  777. code = cf_token_is_type(&ep->cfp, CFTOKEN_STRING, "string", ";");
  778. if (code != PARSE_SUCCESS)
  779. return code;
  780. str = cf_literal_to_str(ep->cfp.cur_token->str.array,
  781. ep->cfp.cur_token->str.len);
  782. if (str) {
  783. da_copy_array(param->default_val, str, strlen(str) + 1);
  784. bfree(str);
  785. }
  786. return PARSE_SUCCESS;
  787. }
  788. static inline int ep_parse_param_assign_intfloat(struct effect_parser *ep,
  789. struct ep_param *param,
  790. bool is_float)
  791. {
  792. int code;
  793. bool is_negative = false;
  794. if (!cf_next_valid_token(&ep->cfp))
  795. return PARSE_EOF;
  796. if (cf_token_is(&ep->cfp, "-")) {
  797. is_negative = true;
  798. if (!cf_next_token(&ep->cfp))
  799. return PARSE_EOF;
  800. }
  801. code = cf_token_is_type(&ep->cfp, CFTOKEN_NUM, "numeric value", ";");
  802. if (code != PARSE_SUCCESS)
  803. return code;
  804. if (is_float) {
  805. float f = (float)os_strtod(ep->cfp.cur_token->str.array);
  806. if (is_negative)
  807. f = -f;
  808. da_push_back_array(param->default_val, (uint8_t *)&f,
  809. sizeof(float));
  810. } else {
  811. long l = strtol(ep->cfp.cur_token->str.array, NULL, 10);
  812. if (is_negative)
  813. l = -l;
  814. da_push_back_array(param->default_val, (uint8_t *)&l,
  815. sizeof(long));
  816. }
  817. return PARSE_SUCCESS;
  818. }
  819. static inline int ep_parse_param_assign_bool(struct effect_parser *ep,
  820. struct ep_param *param)
  821. {
  822. if (!cf_next_valid_token(&ep->cfp))
  823. return PARSE_EOF;
  824. if (cf_token_is(&ep->cfp, "true")) {
  825. long l = 1;
  826. da_push_back_array(param->default_val, (uint8_t *)&l,
  827. sizeof(long));
  828. return PARSE_SUCCESS;
  829. } else if (cf_token_is(&ep->cfp, "false")) {
  830. long l = 0;
  831. da_push_back_array(param->default_val, (uint8_t *)&l,
  832. sizeof(long));
  833. return PARSE_SUCCESS;
  834. }
  835. cf_adderror_expecting(&ep->cfp, "true or false");
  836. return PARSE_EOF;
  837. }
  838. /*
  839. * parses assignment for float1, float2, float3, float4, int1, int2, int3, int4,
  840. * and any combination for float3x3, float4x4, int3x3, int4x4, etc
  841. */
  842. static inline int ep_parse_param_assign_intfloat_array(struct effect_parser *ep,
  843. struct ep_param *param,
  844. bool is_float)
  845. {
  846. const char *intfloat_type = param->type + (is_float ? 5 : 3);
  847. int intfloat_count = 0, code, i;
  848. /* -------------------------------------------- */
  849. if (intfloat_type[0] < '1' || intfloat_type[0] > '4')
  850. cf_adderror(&ep->cfp, "Invalid row count", LEX_ERROR, NULL,
  851. NULL, NULL);
  852. intfloat_count = intfloat_type[0] - '0';
  853. if (intfloat_type[1] == 'x') {
  854. if (intfloat_type[2] < '1' || intfloat_type[2] > '4')
  855. cf_adderror(&ep->cfp, "Invalid column count", LEX_ERROR,
  856. NULL, NULL, NULL);
  857. intfloat_count *= intfloat_type[2] - '0';
  858. }
  859. /* -------------------------------------------- */
  860. code = cf_next_token_should_be(&ep->cfp, "{", ";", NULL);
  861. if (code != PARSE_SUCCESS)
  862. return code;
  863. for (i = 0; i < intfloat_count; i++) {
  864. char *next = ((i + 1) < intfloat_count) ? "," : "}";
  865. code = ep_parse_param_assign_intfloat(ep, param, is_float);
  866. if (code != PARSE_SUCCESS)
  867. return code;
  868. code = cf_next_token_should_be(&ep->cfp, next, ";", NULL);
  869. if (code != PARSE_SUCCESS)
  870. return code;
  871. }
  872. return PARSE_SUCCESS;
  873. }
  874. static int ep_parse_param_assignment_val(struct effect_parser *ep,
  875. struct ep_param *param)
  876. {
  877. if (param->is_texture)
  878. return ep_parse_param_assign_texture(ep, param);
  879. else if (strcmp(param->type, "int") == 0)
  880. return ep_parse_param_assign_intfloat(ep, param, false);
  881. else if (strcmp(param->type, "float") == 0)
  882. return ep_parse_param_assign_intfloat(ep, param, true);
  883. else if (astrcmp_n(param->type, "int", 3) == 0)
  884. return ep_parse_param_assign_intfloat_array(ep, param, false);
  885. else if (astrcmp_n(param->type, "float", 5) == 0)
  886. return ep_parse_param_assign_intfloat_array(ep, param, true);
  887. else if (astrcmp_n(param->type, "string", 6) == 0)
  888. return ep_parse_param_assign_string(ep, param);
  889. else if (strcmp(param->type, "bool") == 0)
  890. return ep_parse_param_assign_bool(ep, param);
  891. cf_adderror(&ep->cfp, "Invalid type '$1' used for assignment",
  892. LEX_ERROR, param->type, NULL, NULL);
  893. return PARSE_CONTINUE;
  894. }
  895. static inline bool ep_parse_param_assign(struct effect_parser *ep,
  896. struct ep_param *param)
  897. {
  898. if (ep_parse_param_assignment_val(ep, param) != PARSE_SUCCESS)
  899. return false;
  900. if (!cf_next_valid_token(&ep->cfp))
  901. return false;
  902. return true;
  903. }
  904. /* static bool ep_parse_param_property(struct effect_parser *ep,
  905. struct ep_param *param)
  906. {
  907. } */
  908. static void ep_parse_param(struct effect_parser *ep, char *type, char *name,
  909. bool is_property, bool is_const, bool is_uniform)
  910. {
  911. struct ep_param param;
  912. ep_param_init(&param, type, name, is_property, is_const, is_uniform);
  913. if (cf_token_is(&ep->cfp, ";"))
  914. goto complete;
  915. if (cf_token_is(&ep->cfp, "[") && !ep_parse_param_array(ep, &param))
  916. goto error;
  917. if (cf_token_is(&ep->cfp, "<") &&
  918. !ep_parse_param_annotations(ep, &param))
  919. goto error;
  920. if (cf_token_is(&ep->cfp, "=") && !ep_parse_param_assign(ep, &param))
  921. goto error;
  922. /*
  923. if (cf_token_is(&ep->cfp, "<") && !ep_parse_param_property(ep, &param))
  924. goto error; */
  925. if (!cf_token_is(&ep->cfp, ";"))
  926. goto error;
  927. complete:
  928. da_push_back(ep->params, &param);
  929. return;
  930. error:
  931. ep_param_free(&param);
  932. }
  933. static bool ep_get_var_specifiers(struct effect_parser *ep, bool *is_property,
  934. bool *is_const, bool *is_uniform)
  935. {
  936. while (true) {
  937. int code;
  938. code = ep_check_for_keyword(ep, "property", is_property);
  939. if (code == PARSE_EOF)
  940. return false;
  941. else if (code == PARSE_CONTINUE)
  942. continue;
  943. code = ep_check_for_keyword(ep, "const", is_const);
  944. if (code == PARSE_EOF)
  945. return false;
  946. else if (code == PARSE_CONTINUE)
  947. continue;
  948. code = ep_check_for_keyword(ep, "uniform", is_uniform);
  949. if (code == PARSE_EOF)
  950. return false;
  951. else if (code == PARSE_CONTINUE)
  952. continue;
  953. break;
  954. }
  955. return true;
  956. }
  957. static inline void report_invalid_func_keyword(struct effect_parser *ep,
  958. const char *name, bool val)
  959. {
  960. if (val)
  961. cf_adderror(&ep->cfp,
  962. "'$1' keyword cannot be used with a "
  963. "function",
  964. LEX_ERROR, name, NULL, NULL);
  965. }
  966. static void ep_parse_other(struct effect_parser *ep)
  967. {
  968. bool is_property = false, is_const = false, is_uniform = false;
  969. char *type = NULL, *name = NULL;
  970. if (!ep_get_var_specifiers(ep, &is_property, &is_const, &is_uniform))
  971. goto error;
  972. if (cf_get_name(&ep->cfp, &type, "type", ";") != PARSE_SUCCESS)
  973. goto error;
  974. if (cf_next_name(&ep->cfp, &name, "name", ";") != PARSE_SUCCESS)
  975. goto error;
  976. if (!cf_next_valid_token(&ep->cfp))
  977. goto error;
  978. if (cf_token_is(&ep->cfp, "(")) {
  979. report_invalid_func_keyword(ep, "property", is_property);
  980. report_invalid_func_keyword(ep, "const", is_const);
  981. report_invalid_func_keyword(ep, "uniform", is_uniform);
  982. ep_parse_function(ep, type, name);
  983. return;
  984. } else {
  985. ep_parse_param(ep, type, name, is_property, is_const,
  986. is_uniform);
  987. return;
  988. }
  989. error:
  990. bfree(type);
  991. bfree(name);
  992. }
  993. static bool ep_compile(struct effect_parser *ep);
  994. extern const char *gs_preprocessor_name(void);
  995. #if defined(_DEBUG) && defined(_DEBUG_SHADERS)
  996. static void debug_get_default_value(struct gs_effect_param *param, char *buffer,
  997. unsigned long long buf_size)
  998. {
  999. if (param->default_val.num == 0) {
  1000. snprintf(buffer, buf_size, "(null)");
  1001. return;
  1002. }
  1003. switch (param->type) {
  1004. case GS_SHADER_PARAM_STRING:
  1005. snprintf(buffer, buf_size, "'%.*s'", param->default_val.num,
  1006. param->default_val.array);
  1007. break;
  1008. case GS_SHADER_PARAM_INT:
  1009. snprintf(buffer, buf_size, "%ld",
  1010. *(int *)(param->default_val.array + 0));
  1011. break;
  1012. case GS_SHADER_PARAM_INT2:
  1013. snprintf(buffer, buf_size, "%ld,%ld",
  1014. *(int *)(param->default_val.array + 0),
  1015. *(int *)(param->default_val.array + 4));
  1016. break;
  1017. case GS_SHADER_PARAM_INT3:
  1018. snprintf(buffer, buf_size, "%ld,%ld,%ld",
  1019. *(int *)(param->default_val.array + 0),
  1020. *(int *)(param->default_val.array + 4),
  1021. *(int *)(param->default_val.array + 8));
  1022. break;
  1023. case GS_SHADER_PARAM_INT4:
  1024. snprintf(buffer, buf_size, "%ld,%ld,%ld,%ld",
  1025. *(int *)(param->default_val.array + 0),
  1026. *(int *)(param->default_val.array + 4),
  1027. *(int *)(param->default_val.array + 8),
  1028. *(int *)(param->default_val.array + 12));
  1029. break;
  1030. case GS_SHADER_PARAM_FLOAT:
  1031. snprintf(buffer, buf_size, "%e",
  1032. *(float *)(param->default_val.array + 0));
  1033. break;
  1034. case GS_SHADER_PARAM_VEC2:
  1035. snprintf(buffer, buf_size, "%e,%e",
  1036. *(float *)(param->default_val.array + 0),
  1037. *(float *)(param->default_val.array + 4));
  1038. break;
  1039. case GS_SHADER_PARAM_VEC3:
  1040. snprintf(buffer, buf_size, "%e,%e,%e",
  1041. *(float *)(param->default_val.array + 0),
  1042. *(float *)(param->default_val.array + 4),
  1043. *(float *)(param->default_val.array + 8));
  1044. break;
  1045. case GS_SHADER_PARAM_VEC4:
  1046. snprintf(buffer, buf_size, "%e,%e,%e,%e",
  1047. *(float *)(param->default_val.array + 0),
  1048. *(float *)(param->default_val.array + 4),
  1049. *(float *)(param->default_val.array + 8),
  1050. *(float *)(param->default_val.array + 12));
  1051. break;
  1052. case GS_SHADER_PARAM_MATRIX4X4:
  1053. snprintf(buffer, buf_size,
  1054. "[[%e,%e,%e,%e],[%e,%e,%e,%e],"
  1055. "[%e,%e,%e,%e],[%e,%e,%e,%e]]",
  1056. *(float *)(param->default_val.array + 0),
  1057. *(float *)(param->default_val.array + 4),
  1058. *(float *)(param->default_val.array + 8),
  1059. *(float *)(param->default_val.array + 12),
  1060. *(float *)(param->default_val.array + 16),
  1061. *(float *)(param->default_val.array + 20),
  1062. *(float *)(param->default_val.array + 24),
  1063. *(float *)(param->default_val.array + 28),
  1064. *(float *)(param->default_val.array + 32),
  1065. *(float *)(param->default_val.array + 36),
  1066. *(float *)(param->default_val.array + 40),
  1067. *(float *)(param->default_val.array + 44),
  1068. *(float *)(param->default_val.array + 48),
  1069. *(float *)(param->default_val.array + 52),
  1070. *(float *)(param->default_val.array + 56),
  1071. *(float *)(param->default_val.array + 60));
  1072. break;
  1073. case GS_SHADER_PARAM_BOOL:
  1074. snprintf(buffer, buf_size, "%s",
  1075. (*param->default_val.array) != 0 ? "true\0"
  1076. : "false\0");
  1077. break;
  1078. case GS_SHADER_PARAM_UNKNOWN:
  1079. case GS_SHADER_PARAM_TEXTURE:
  1080. snprintf(buffer, buf_size, "<unknown>");
  1081. break;
  1082. }
  1083. }
  1084. static void debug_param(struct gs_effect_param *param,
  1085. struct ep_param *param_in, unsigned long long idx,
  1086. const char *offset)
  1087. {
  1088. char _debug_type[4096];
  1089. switch (param->type) {
  1090. case GS_SHADER_PARAM_STRING:
  1091. snprintf(_debug_type, sizeof(_debug_type), "string");
  1092. break;
  1093. case GS_SHADER_PARAM_INT:
  1094. snprintf(_debug_type, sizeof(_debug_type), "int");
  1095. break;
  1096. case GS_SHADER_PARAM_INT2:
  1097. snprintf(_debug_type, sizeof(_debug_type), "int2");
  1098. break;
  1099. case GS_SHADER_PARAM_INT3:
  1100. snprintf(_debug_type, sizeof(_debug_type), "int3");
  1101. break;
  1102. case GS_SHADER_PARAM_INT4:
  1103. snprintf(_debug_type, sizeof(_debug_type), "int4");
  1104. break;
  1105. case GS_SHADER_PARAM_FLOAT:
  1106. snprintf(_debug_type, sizeof(_debug_type), "float");
  1107. break;
  1108. case GS_SHADER_PARAM_VEC2:
  1109. snprintf(_debug_type, sizeof(_debug_type), "float2");
  1110. break;
  1111. case GS_SHADER_PARAM_VEC3:
  1112. snprintf(_debug_type, sizeof(_debug_type), "float3");
  1113. break;
  1114. case GS_SHADER_PARAM_VEC4:
  1115. snprintf(_debug_type, sizeof(_debug_type), "float4");
  1116. break;
  1117. case GS_SHADER_PARAM_MATRIX4X4:
  1118. snprintf(_debug_type, sizeof(_debug_type), "float4x4");
  1119. break;
  1120. case GS_SHADER_PARAM_BOOL:
  1121. snprintf(_debug_type, sizeof(_debug_type), "bool");
  1122. break;
  1123. case GS_SHADER_PARAM_UNKNOWN:
  1124. snprintf(_debug_type, sizeof(_debug_type), "unknown");
  1125. break;
  1126. case GS_SHADER_PARAM_TEXTURE:
  1127. snprintf(_debug_type, sizeof(_debug_type), "texture");
  1128. break;
  1129. }
  1130. char _debug_buf[4096];
  1131. debug_get_default_value(param, _debug_buf, sizeof(_debug_buf));
  1132. if (param->annotations.num > 0) {
  1133. blog(LOG_DEBUG,
  1134. "%s[%4lld] %.*s '%s' with value %.*s and %lld annotations:",
  1135. offset, idx, sizeof(_debug_type), _debug_type, param->name,
  1136. sizeof(_debug_buf), _debug_buf, param->annotations.num);
  1137. } else {
  1138. blog(LOG_DEBUG, "%s[%4lld] %.*s '%s' with value %.*s.", offset,
  1139. idx, sizeof(_debug_type), _debug_type, param->name,
  1140. sizeof(_debug_buf), _debug_buf);
  1141. }
  1142. }
  1143. static void debug_param_annotation(struct gs_effect_param *param,
  1144. struct ep_param *param_in,
  1145. unsigned long long idx, const char *offset)
  1146. {
  1147. char _debug_buf[4096];
  1148. debug_get_default_value(param, _debug_buf, sizeof(_debug_buf));
  1149. blog(LOG_DEBUG, "%s[%4lld] %s '%s' with value %.*s", offset, idx,
  1150. param_in->type, param->name, sizeof(_debug_buf), _debug_buf);
  1151. }
  1152. static void debug_print_string(const char *offset, const char *str)
  1153. {
  1154. // Bypass 4096 limit in def_log_handler.
  1155. char const *begin = str;
  1156. unsigned long long line = 1;
  1157. for (char const *here = begin; here[0] != '\0'; here++) {
  1158. char const *str = begin;
  1159. unsigned long long len = here - begin;
  1160. bool is_line = false;
  1161. if (here[0] == '\r') {
  1162. is_line = true;
  1163. if (here[1] == '\n') {
  1164. here += 1;
  1165. }
  1166. begin = here + 1;
  1167. } else if (here[0] == '\n') {
  1168. is_line = true;
  1169. begin = here + 1;
  1170. }
  1171. if (is_line) {
  1172. blog(LOG_DEBUG, "\t\t\t\t[%4lld] %.*s", line, len, str);
  1173. line++;
  1174. }
  1175. }
  1176. if (begin[0] != '\0') {
  1177. // Final line was not written.
  1178. blog(LOG_DEBUG, "\t\t\t\t[%4lld] %*s", line, strlen(begin),
  1179. begin);
  1180. }
  1181. }
  1182. #endif
  1183. bool ep_parse(struct effect_parser *ep, gs_effect_t *effect,
  1184. const char *effect_string, const char *file)
  1185. {
  1186. bool success;
  1187. const char *graphics_preprocessor = gs_preprocessor_name();
  1188. if (graphics_preprocessor) {
  1189. struct cf_def def;
  1190. cf_def_init(&def);
  1191. def.name.str.array = graphics_preprocessor;
  1192. def.name.str.len = strlen(graphics_preprocessor);
  1193. strref_copy(&def.name.unmerged_str, &def.name.str);
  1194. cf_preprocessor_add_def(&ep->cfp.pp, &def);
  1195. }
  1196. ep->effect = effect;
  1197. if (!cf_parser_parse(&ep->cfp, effect_string, file))
  1198. return false;
  1199. while (ep->cfp.cur_token && ep->cfp.cur_token->type != CFTOKEN_NONE) {
  1200. if (cf_token_is(&ep->cfp, ";") ||
  1201. is_whitespace(*ep->cfp.cur_token->str.array)) {
  1202. /* do nothing */
  1203. ep->cfp.cur_token++;
  1204. } else if (cf_token_is(&ep->cfp, "struct")) {
  1205. ep_parse_struct(ep);
  1206. } else if (cf_token_is(&ep->cfp, "technique")) {
  1207. ep_parse_technique(ep);
  1208. } else if (cf_token_is(&ep->cfp, "sampler_state")) {
  1209. ep_parse_sampler_state(ep);
  1210. } else if (cf_token_is(&ep->cfp, "{")) {
  1211. /* add error and pass braces */
  1212. cf_adderror(&ep->cfp, "Unexpected code segment",
  1213. LEX_ERROR, NULL, NULL, NULL);
  1214. cf_pass_pair(&ep->cfp, '{', '}');
  1215. } else {
  1216. /* parameters and functions */
  1217. ep_parse_other(ep);
  1218. }
  1219. }
  1220. #if defined(_DEBUG) && defined(_DEBUG_SHADERS)
  1221. blog(LOG_DEBUG,
  1222. "================================================================================");
  1223. blog(LOG_DEBUG, "Effect Parser reformatted shader '%s' to:", file);
  1224. debug_print_string("\t", ep->cfp.lex.reformatted);
  1225. #endif
  1226. success = !error_data_has_errors(&ep->cfp.error_list);
  1227. if (success)
  1228. success = ep_compile(ep);
  1229. #if defined(_DEBUG) && defined(_DEBUG_SHADERS)
  1230. blog(LOG_DEBUG,
  1231. "================================================================================");
  1232. #endif
  1233. return success;
  1234. }
  1235. /* ------------------------------------------------------------------------- */
  1236. static inline void ep_write_param(struct dstr *shader, struct ep_param *param,
  1237. struct darray *used_params)
  1238. {
  1239. if (param->written)
  1240. return;
  1241. if (param->is_const) {
  1242. dstr_cat(shader, "const ");
  1243. } else if (param->is_uniform) {
  1244. struct dstr new;
  1245. dstr_init_copy(&new, param->name);
  1246. darray_push_back(sizeof(struct dstr), used_params, &new);
  1247. dstr_cat(shader, "uniform ");
  1248. }
  1249. dstr_cat(shader, param->type);
  1250. dstr_cat(shader, " ");
  1251. dstr_cat(shader, param->name);
  1252. if (param->array_count)
  1253. dstr_catf(shader, "[%u]", param->array_count);
  1254. dstr_cat(shader, ";\n");
  1255. param->written = true;
  1256. }
  1257. static inline void ep_write_func_param_deps(struct effect_parser *ep,
  1258. struct dstr *shader,
  1259. struct ep_func *func,
  1260. struct darray *used_params)
  1261. {
  1262. size_t i;
  1263. for (i = 0; i < func->param_deps.num; i++) {
  1264. const char *name = func->param_deps.array[i];
  1265. struct ep_param *param = ep_getparam(ep, name);
  1266. ep_write_param(shader, param, used_params);
  1267. }
  1268. if (func->param_deps.num)
  1269. dstr_cat(shader, "\n\n");
  1270. }
  1271. static void ep_write_sampler(struct dstr *shader, struct ep_sampler *sampler)
  1272. {
  1273. size_t i;
  1274. if (sampler->written)
  1275. return;
  1276. dstr_cat(shader, "sampler_state ");
  1277. dstr_cat(shader, sampler->name);
  1278. dstr_cat(shader, " {");
  1279. for (i = 0; i < sampler->values.num; i++) {
  1280. dstr_cat(shader, "\n\t");
  1281. dstr_cat(shader, sampler->states.array[i]);
  1282. dstr_cat(shader, " = ");
  1283. dstr_cat(shader, sampler->values.array[i]);
  1284. dstr_cat(shader, ";\n");
  1285. }
  1286. dstr_cat(shader, "\n};\n");
  1287. sampler->written = true;
  1288. }
  1289. static inline void ep_write_func_sampler_deps(struct effect_parser *ep,
  1290. struct dstr *shader,
  1291. struct ep_func *func)
  1292. {
  1293. size_t i;
  1294. for (i = 0; i < func->sampler_deps.num; i++) {
  1295. const char *name = func->sampler_deps.array[i];
  1296. struct ep_sampler *sampler = ep_getsampler(ep, name);
  1297. ep_write_sampler(shader, sampler);
  1298. dstr_cat(shader, "\n");
  1299. }
  1300. }
  1301. static inline void ep_write_var(struct dstr *shader, struct ep_var *var)
  1302. {
  1303. if (var->var_type == EP_VAR_INOUT)
  1304. dstr_cat(shader, "inout ");
  1305. else if (var->var_type == EP_VAR_OUT)
  1306. dstr_cat(shader, "out ");
  1307. else if (var->var_type == EP_VAR_UNIFORM)
  1308. dstr_cat(shader, "uniform ");
  1309. // The "in" input modifier is implied by default, so leave it blank
  1310. // in that case.
  1311. dstr_cat(shader, var->type);
  1312. dstr_cat(shader, " ");
  1313. dstr_cat(shader, var->name);
  1314. if (var->mapping) {
  1315. dstr_cat(shader, " : ");
  1316. dstr_cat(shader, var->mapping);
  1317. }
  1318. }
  1319. static void ep_write_struct(struct dstr *shader, struct ep_struct *st)
  1320. {
  1321. size_t i;
  1322. if (st->written)
  1323. return;
  1324. dstr_cat(shader, "struct ");
  1325. dstr_cat(shader, st->name);
  1326. dstr_cat(shader, " {");
  1327. for (i = 0; i < st->vars.num; i++) {
  1328. dstr_cat(shader, "\n\t");
  1329. ep_write_var(shader, st->vars.array + i);
  1330. dstr_cat(shader, ";");
  1331. }
  1332. dstr_cat(shader, "\n};\n");
  1333. st->written = true;
  1334. }
  1335. static inline void ep_write_func_struct_deps(struct effect_parser *ep,
  1336. struct dstr *shader,
  1337. struct ep_func *func)
  1338. {
  1339. size_t i;
  1340. for (i = 0; i < func->struct_deps.num; i++) {
  1341. const char *name = func->struct_deps.array[i];
  1342. struct ep_struct *st = ep_getstruct(ep, name);
  1343. if (!st->written) {
  1344. ep_write_struct(shader, st);
  1345. dstr_cat(shader, "\n");
  1346. st->written = true;
  1347. }
  1348. }
  1349. }
  1350. static void ep_write_func(struct effect_parser *ep, struct dstr *shader,
  1351. struct ep_func *func, struct darray *used_params);
  1352. static inline void ep_write_func_func_deps(struct effect_parser *ep,
  1353. struct dstr *shader,
  1354. struct ep_func *func,
  1355. struct darray *used_params)
  1356. {
  1357. size_t i;
  1358. for (i = 0; i < func->func_deps.num; i++) {
  1359. const char *name = func->func_deps.array[i];
  1360. struct ep_func *func_dep = ep_getfunc(ep, name);
  1361. if (!func_dep->written) {
  1362. ep_write_func(ep, shader, func_dep, used_params);
  1363. dstr_cat(shader, "\n\n");
  1364. }
  1365. }
  1366. }
  1367. static void ep_write_func(struct effect_parser *ep, struct dstr *shader,
  1368. struct ep_func *func, struct darray *used_params)
  1369. {
  1370. size_t i;
  1371. func->written = true;
  1372. ep_write_func_param_deps(ep, shader, func, used_params);
  1373. ep_write_func_sampler_deps(ep, shader, func);
  1374. ep_write_func_struct_deps(ep, shader, func);
  1375. ep_write_func_func_deps(ep, shader, func, used_params);
  1376. /* ------------------------------------ */
  1377. dstr_cat(shader, func->ret_type);
  1378. dstr_cat(shader, " ");
  1379. dstr_cat(shader, func->name);
  1380. dstr_cat(shader, "(");
  1381. for (i = 0; i < func->param_vars.num; i++) {
  1382. struct ep_var *var = func->param_vars.array + i;
  1383. if (i)
  1384. dstr_cat(shader, ", ");
  1385. ep_write_var(shader, var);
  1386. }
  1387. dstr_cat(shader, ")\n");
  1388. dstr_cat_dstr(shader, &func->contents);
  1389. dstr_cat(shader, "\n");
  1390. }
  1391. /* writes mapped vars used by the call as parameters for main */
  1392. static void ep_write_main_params(struct effect_parser *ep, struct dstr *shader,
  1393. struct dstr *param_str, struct ep_func *func)
  1394. {
  1395. size_t i;
  1396. bool empty_params = dstr_is_empty(param_str);
  1397. for (i = 0; i < func->param_vars.num; i++) {
  1398. struct ep_var *var = func->param_vars.array + i;
  1399. struct ep_struct *st = NULL;
  1400. bool mapped = (var->mapping != NULL);
  1401. if (!mapped) {
  1402. st = ep_getstruct(ep, var->type);
  1403. if (st)
  1404. mapped = ep_struct_mapped(st);
  1405. }
  1406. if (mapped) {
  1407. dstr_cat(shader, var->type);
  1408. dstr_cat(shader, " ");
  1409. dstr_cat(shader, var->name);
  1410. if (!st) {
  1411. dstr_cat(shader, " : ");
  1412. dstr_cat(shader, var->mapping);
  1413. }
  1414. if (!dstr_is_empty(param_str))
  1415. dstr_cat(param_str, ", ");
  1416. dstr_cat(param_str, var->name);
  1417. }
  1418. }
  1419. if (!empty_params)
  1420. dstr_cat(param_str, ", ");
  1421. }
  1422. static void ep_write_main(struct effect_parser *ep, struct dstr *shader,
  1423. struct ep_func *func, struct dstr *call_str)
  1424. {
  1425. struct dstr param_str;
  1426. struct dstr adjusted_call;
  1427. dstr_init(&param_str);
  1428. dstr_init_copy_dstr(&adjusted_call, call_str);
  1429. dstr_cat(shader, "\n");
  1430. dstr_cat(shader, func->ret_type);
  1431. dstr_cat(shader, " main(");
  1432. ep_write_main_params(ep, shader, &param_str, func);
  1433. dstr_cat(shader, ")");
  1434. if (func->mapping) {
  1435. dstr_cat(shader, " : ");
  1436. dstr_cat(shader, func->mapping);
  1437. }
  1438. dstr_cat(shader, "\n{\n\treturn ");
  1439. dstr_cat_dstr(shader, &adjusted_call);
  1440. dstr_cat(shader, "\n}\n");
  1441. dstr_free(&adjusted_call);
  1442. dstr_free(&param_str);
  1443. }
  1444. static inline void ep_reset_written(struct effect_parser *ep)
  1445. {
  1446. size_t i;
  1447. for (i = 0; i < ep->params.num; i++)
  1448. ep->params.array[i].written = false;
  1449. for (i = 0; i < ep->structs.num; i++)
  1450. ep->structs.array[i].written = false;
  1451. for (i = 0; i < ep->funcs.num; i++)
  1452. ep->funcs.array[i].written = false;
  1453. for (i = 0; i < ep->samplers.num; i++)
  1454. ep->samplers.array[i].written = false;
  1455. }
  1456. static void ep_makeshaderstring(struct effect_parser *ep, struct dstr *shader,
  1457. struct darray *shader_call,
  1458. struct darray *used_params)
  1459. {
  1460. struct cf_token *token = shader_call->array;
  1461. struct cf_token *func_name;
  1462. struct ep_func *func;
  1463. struct dstr call_str;
  1464. dstr_init(&call_str);
  1465. if (!token)
  1466. return;
  1467. while (token->type != CFTOKEN_NONE && is_whitespace(*token->str.array))
  1468. token++;
  1469. if (token->type == CFTOKEN_NONE || strref_cmp(&token->str, "NULL") == 0)
  1470. return;
  1471. func_name = token;
  1472. while (token->type != CFTOKEN_NONE) {
  1473. struct ep_param *param = ep_getparam_strref(ep, &token->str);
  1474. if (param)
  1475. ep_write_param(shader, param, used_params);
  1476. dstr_cat_strref(&call_str, &token->str);
  1477. token++;
  1478. }
  1479. func = ep_getfunc_strref(ep, &func_name->str);
  1480. if (!func)
  1481. return;
  1482. ep_write_func(ep, shader, func, used_params);
  1483. ep_write_main(ep, shader, func, &call_str);
  1484. dstr_free(&call_str);
  1485. ep_reset_written(ep);
  1486. }
  1487. static void ep_compile_annotations(struct darray *ep_annotations,
  1488. struct darray *gsp_annotations,
  1489. struct effect_parser *ep)
  1490. {
  1491. darray_resize(sizeof(struct gs_effect_param), gsp_annotations,
  1492. ep_annotations->num);
  1493. size_t i;
  1494. for (i = 0; i < ep_annotations->num; i++) {
  1495. struct gs_effect_param *param =
  1496. ((struct gs_effect_param *)gsp_annotations->array) + i;
  1497. struct ep_param *param_in =
  1498. ((struct ep_param *)ep_annotations->array) + i;
  1499. param->name = bstrdup(param_in->name);
  1500. param->section = EFFECT_ANNOTATION;
  1501. param->effect = ep->effect;
  1502. da_move(param->default_val, param_in->default_val);
  1503. param->type = get_effect_param_type(param_in->type);
  1504. #if defined(_DEBUG) && defined(_DEBUG_SHADERS)
  1505. debug_param(param, param_in, i, "\t\t");
  1506. #endif
  1507. }
  1508. }
  1509. static void
  1510. ep_compile_param_annotations(struct ep_param *ep_param_input,
  1511. struct gs_effect_param *gs_effect_input,
  1512. struct effect_parser *ep)
  1513. {
  1514. ep_compile_annotations(&(ep_param_input->annotations.da),
  1515. &(gs_effect_input->annotations.da), ep);
  1516. }
  1517. static void ep_compile_param(struct effect_parser *ep, size_t idx)
  1518. {
  1519. struct gs_effect_param *param;
  1520. struct ep_param *param_in;
  1521. param = ep->effect->params.array + idx;
  1522. param_in = ep->params.array + idx;
  1523. param_in->param = param;
  1524. param->name = bstrdup(param_in->name);
  1525. param->section = EFFECT_PARAM;
  1526. param->effect = ep->effect;
  1527. da_move(param->default_val, param_in->default_val);
  1528. param->type = get_effect_param_type(param_in->type);
  1529. if (strcmp(param_in->name, "ViewProj") == 0)
  1530. ep->effect->view_proj = param;
  1531. else if (strcmp(param_in->name, "World") == 0)
  1532. ep->effect->world = param;
  1533. #if defined(_DEBUG) && defined(_DEBUG_SHADERS)
  1534. debug_param(param, param_in, idx, "\t");
  1535. #endif
  1536. ep_compile_param_annotations(param_in, param, ep);
  1537. }
  1538. static bool ep_compile_pass_shaderparams(struct effect_parser *ep,
  1539. struct darray *pass_params,
  1540. struct darray *used_params,
  1541. gs_shader_t *shader)
  1542. {
  1543. size_t i;
  1544. darray_resize(sizeof(struct pass_shaderparam), pass_params,
  1545. used_params->num);
  1546. for (i = 0; i < pass_params->num; i++) {
  1547. struct dstr *param_name;
  1548. struct pass_shaderparam *param;
  1549. param_name = darray_item(sizeof(struct dstr), used_params, i);
  1550. param = darray_item(sizeof(struct pass_shaderparam),
  1551. pass_params, i);
  1552. param->eparam = gs_effect_get_param_by_name(ep->effect,
  1553. param_name->array);
  1554. param->sparam =
  1555. gs_shader_get_param_by_name(shader, param_name->array);
  1556. #if defined(_DEBUG) && defined(_DEBUG_SHADERS)
  1557. debug_param(param->eparam, 0, i, "\t\t\t\t");
  1558. #endif
  1559. if (!param->sparam) {
  1560. blog(LOG_ERROR, "Effect shader parameter not found");
  1561. return false;
  1562. }
  1563. }
  1564. return true;
  1565. }
  1566. static inline bool ep_compile_pass_shader(struct effect_parser *ep,
  1567. struct gs_effect_technique *tech,
  1568. struct gs_effect_pass *pass,
  1569. struct ep_pass *pass_in,
  1570. size_t pass_idx,
  1571. enum gs_shader_type type)
  1572. {
  1573. struct dstr shader_str;
  1574. struct dstr location;
  1575. struct darray used_params; /* struct dstr */
  1576. struct darray *pass_params = NULL; /* struct pass_shaderparam */
  1577. gs_shader_t *shader = NULL;
  1578. bool success = true;
  1579. dstr_init(&shader_str);
  1580. darray_init(&used_params);
  1581. dstr_init(&location);
  1582. dstr_copy(&location, ep->cfp.lex.file);
  1583. if (type == GS_SHADER_VERTEX)
  1584. dstr_cat(&location, " (Vertex ");
  1585. else if (type == GS_SHADER_PIXEL)
  1586. dstr_cat(&location, " (Pixel ");
  1587. /*else if (type == SHADER_GEOMETRY)
  1588. dstr_cat(&location, " (Geometry ");*/
  1589. assert(pass_idx <= UINT_MAX);
  1590. dstr_catf(&location, "shader, technique %s, pass %u)", tech->name,
  1591. (unsigned)pass_idx);
  1592. if (type == GS_SHADER_VERTEX) {
  1593. ep_makeshaderstring(ep, &shader_str,
  1594. &pass_in->vertex_program.da, &used_params);
  1595. pass->vertshader = gs_vertexshader_create(shader_str.array,
  1596. location.array, NULL);
  1597. shader = pass->vertshader;
  1598. pass_params = &pass->vertshader_params.da;
  1599. } else if (type == GS_SHADER_PIXEL) {
  1600. ep_makeshaderstring(ep, &shader_str,
  1601. &pass_in->fragment_program.da,
  1602. &used_params);
  1603. pass->pixelshader = gs_pixelshader_create(shader_str.array,
  1604. location.array, NULL);
  1605. shader = pass->pixelshader;
  1606. pass_params = &pass->pixelshader_params.da;
  1607. }
  1608. #if defined(_DEBUG) && defined(_DEBUG_SHADERS)
  1609. blog(LOG_DEBUG, "\t\t\t%s Shader:",
  1610. type == GS_SHADER_VERTEX ? "Vertex" : "Fragment");
  1611. blog(LOG_DEBUG, "\t\t\tCode:");
  1612. debug_print_string("\t\t\t\t\t", shader_str.array);
  1613. blog(LOG_DEBUG, "\t\t\tParameters:");
  1614. #endif
  1615. if (shader)
  1616. success = ep_compile_pass_shaderparams(ep, pass_params,
  1617. &used_params, shader);
  1618. else
  1619. success = false;
  1620. dstr_free(&location);
  1621. dstr_array_free(used_params.array, used_params.num);
  1622. darray_free(&used_params);
  1623. dstr_free(&shader_str);
  1624. return success;
  1625. }
  1626. static bool ep_compile_pass(struct effect_parser *ep,
  1627. struct gs_effect_technique *tech,
  1628. struct ep_technique *tech_in, size_t idx)
  1629. {
  1630. struct gs_effect_pass *pass;
  1631. struct ep_pass *pass_in;
  1632. bool success = true;
  1633. pass = tech->passes.array + idx;
  1634. pass_in = tech_in->passes.array + idx;
  1635. pass->name = bstrdup(pass_in->name);
  1636. pass->section = EFFECT_PASS;
  1637. #if defined(_DEBUG) && defined(_DEBUG_SHADERS)
  1638. blog(LOG_DEBUG, "\t\t[%4lld] Pass '%s':", idx, pass->name);
  1639. #endif
  1640. if (!ep_compile_pass_shader(ep, tech, pass, pass_in, idx,
  1641. GS_SHADER_VERTEX)) {
  1642. success = false;
  1643. blog(LOG_ERROR, "Pass (%zu) <%s> missing vertex shader!", idx,
  1644. pass->name ? pass->name : "");
  1645. }
  1646. if (!ep_compile_pass_shader(ep, tech, pass, pass_in, idx,
  1647. GS_SHADER_PIXEL)) {
  1648. success = false;
  1649. blog(LOG_ERROR, "Pass (%zu) <%s> missing pixel shader!", idx,
  1650. pass->name ? pass->name : "");
  1651. }
  1652. return success;
  1653. }
  1654. static inline bool ep_compile_technique(struct effect_parser *ep, size_t idx)
  1655. {
  1656. struct gs_effect_technique *tech;
  1657. struct ep_technique *tech_in;
  1658. bool success = true;
  1659. size_t i;
  1660. tech = ep->effect->techniques.array + idx;
  1661. tech_in = ep->techniques.array + idx;
  1662. tech->name = bstrdup(tech_in->name);
  1663. tech->section = EFFECT_TECHNIQUE;
  1664. tech->effect = ep->effect;
  1665. da_resize(tech->passes, tech_in->passes.num);
  1666. #if defined(_DEBUG) && defined(_DEBUG_SHADERS)
  1667. blog(LOG_DEBUG, "\t[%4lld] Technique '%s' has %lld passes:", idx,
  1668. tech->name, tech->passes.num);
  1669. #endif
  1670. for (i = 0; i < tech->passes.num; i++) {
  1671. if (!ep_compile_pass(ep, tech, tech_in, i))
  1672. success = false;
  1673. }
  1674. return success;
  1675. }
  1676. static bool ep_compile(struct effect_parser *ep)
  1677. {
  1678. bool success = true;
  1679. size_t i;
  1680. assert(ep->effect);
  1681. da_resize(ep->effect->params, ep->params.num);
  1682. da_resize(ep->effect->techniques, ep->techniques.num);
  1683. #if defined(_DEBUG) && defined(_DEBUG_SHADERS)
  1684. blog(LOG_DEBUG, "Shader has %lld parameters:", ep->params.num);
  1685. #endif
  1686. for (i = 0; i < ep->params.num; i++)
  1687. ep_compile_param(ep, i);
  1688. #if defined(_DEBUG) && defined(_DEBUG_SHADERS)
  1689. blog(LOG_DEBUG, "Shader has %lld techniques:", ep->techniques.num);
  1690. #endif
  1691. for (i = 0; i < ep->techniques.num; i++) {
  1692. if (!ep_compile_technique(ep, i))
  1693. success = false;
  1694. }
  1695. return success;
  1696. }