effect-parser.c 51 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084
  1. /******************************************************************************
  2. Copyright (C) 2023 by Lain 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. typedef DARRAY(struct dstr) dstr_array_t;
  20. static inline bool ep_parse_param_assign(struct effect_parser *ep,
  21. struct ep_param *param);
  22. static enum gs_shader_param_type get_effect_param_type(const char *type)
  23. {
  24. if (strcmp(type, "float") == 0)
  25. return GS_SHADER_PARAM_FLOAT;
  26. else if (strcmp(type, "float2") == 0)
  27. return GS_SHADER_PARAM_VEC2;
  28. else if (strcmp(type, "float3") == 0)
  29. return GS_SHADER_PARAM_VEC3;
  30. else if (strcmp(type, "float4") == 0)
  31. return GS_SHADER_PARAM_VEC4;
  32. else if (strcmp(type, "int2") == 0)
  33. return GS_SHADER_PARAM_INT2;
  34. else if (strcmp(type, "int3") == 0)
  35. return GS_SHADER_PARAM_INT3;
  36. else if (strcmp(type, "int4") == 0)
  37. return GS_SHADER_PARAM_INT4;
  38. else if (astrcmp_n(type, "texture", 7) == 0)
  39. return GS_SHADER_PARAM_TEXTURE;
  40. else if (strcmp(type, "float4x4") == 0)
  41. return GS_SHADER_PARAM_MATRIX4X4;
  42. else if (strcmp(type, "bool") == 0)
  43. return GS_SHADER_PARAM_BOOL;
  44. else if (strcmp(type, "int") == 0)
  45. return GS_SHADER_PARAM_INT;
  46. else if (strcmp(type, "string") == 0)
  47. return GS_SHADER_PARAM_STRING;
  48. return GS_SHADER_PARAM_UNKNOWN;
  49. }
  50. void ep_free(struct effect_parser *ep)
  51. {
  52. size_t i;
  53. for (i = 0; i < ep->params.num; i++)
  54. ep_param_free(ep->params.array + i);
  55. for (i = 0; i < ep->structs.num; i++)
  56. ep_struct_free(ep->structs.array + i);
  57. for (i = 0; i < ep->funcs.num; i++)
  58. ep_func_free(ep->funcs.array + i);
  59. for (i = 0; i < ep->samplers.num; i++)
  60. ep_sampler_free(ep->samplers.array + i);
  61. for (i = 0; i < ep->techniques.num; i++)
  62. ep_technique_free(ep->techniques.array + i);
  63. ep->cur_pass = NULL;
  64. cf_parser_free(&ep->cfp);
  65. da_free(ep->params);
  66. da_free(ep->structs);
  67. da_free(ep->funcs);
  68. da_free(ep->samplers);
  69. da_free(ep->techniques);
  70. }
  71. static inline struct ep_func *ep_getfunc(struct effect_parser *ep,
  72. const char *name)
  73. {
  74. size_t i;
  75. for (i = 0; i < ep->funcs.num; i++) {
  76. if (strcmp(name, ep->funcs.array[i].name) == 0)
  77. return ep->funcs.array + i;
  78. }
  79. return NULL;
  80. }
  81. static inline struct ep_struct *ep_getstruct(struct effect_parser *ep,
  82. const char *name)
  83. {
  84. size_t i;
  85. for (i = 0; i < ep->structs.num; i++) {
  86. if (strcmp(name, ep->structs.array[i].name) == 0)
  87. return ep->structs.array + i;
  88. }
  89. return NULL;
  90. }
  91. static inline struct ep_sampler *ep_getsampler(struct effect_parser *ep,
  92. const char *name)
  93. {
  94. size_t i;
  95. for (i = 0; i < ep->samplers.num; i++) {
  96. if (strcmp(name, ep->samplers.array[i].name) == 0)
  97. return ep->samplers.array + i;
  98. }
  99. return NULL;
  100. }
  101. static inline struct ep_param *ep_getparam(struct effect_parser *ep,
  102. const char *name)
  103. {
  104. size_t i;
  105. for (i = 0; i < ep->params.num; i++) {
  106. if (strcmp(name, ep->params.array[i].name) == 0)
  107. return ep->params.array + i;
  108. }
  109. return NULL;
  110. }
  111. static inline struct ep_param *ep_getannotation(struct ep_param *param,
  112. const char *name)
  113. {
  114. size_t i;
  115. for (i = 0; i < param->annotations.num; i++) {
  116. if (strcmp(name, param->annotations.array[i].name) == 0)
  117. return param->annotations.array + i;
  118. }
  119. return NULL;
  120. }
  121. static inline struct ep_func *ep_getfunc_strref(struct effect_parser *ep,
  122. const struct strref *ref)
  123. {
  124. size_t i;
  125. for (i = 0; i < ep->funcs.num; i++) {
  126. if (strref_cmp(ref, ep->funcs.array[i].name) == 0)
  127. return ep->funcs.array + i;
  128. }
  129. return NULL;
  130. }
  131. static inline struct ep_struct *ep_getstruct_strref(struct effect_parser *ep,
  132. const struct strref *ref)
  133. {
  134. size_t i;
  135. for (i = 0; i < ep->structs.num; i++) {
  136. if (strref_cmp(ref, ep->structs.array[i].name) == 0)
  137. return ep->structs.array + i;
  138. }
  139. return NULL;
  140. }
  141. static inline struct ep_sampler *ep_getsampler_strref(struct effect_parser *ep,
  142. const struct strref *ref)
  143. {
  144. size_t i;
  145. for (i = 0; i < ep->samplers.num; i++) {
  146. if (strref_cmp(ref, ep->samplers.array[i].name) == 0)
  147. return ep->samplers.array + i;
  148. }
  149. return NULL;
  150. }
  151. static inline struct ep_param *ep_getparam_strref(struct effect_parser *ep,
  152. const struct strref *ref)
  153. {
  154. size_t i;
  155. for (i = 0; i < ep->params.num; i++) {
  156. if (strref_cmp(ref, ep->params.array[i].name) == 0)
  157. return ep->params.array + i;
  158. }
  159. return NULL;
  160. }
  161. static inline int ep_parse_struct_var(struct effect_parser *ep,
  162. struct ep_var *var)
  163. {
  164. int code;
  165. /* -------------------------------------- */
  166. /* variable type */
  167. if (!cf_next_valid_token(&ep->cfp))
  168. return PARSE_EOF;
  169. if (cf_token_is(&ep->cfp, ";"))
  170. return PARSE_CONTINUE;
  171. if (cf_token_is(&ep->cfp, "}"))
  172. return PARSE_BREAK;
  173. code = cf_token_is_type(&ep->cfp, CFTOKEN_NAME, "type name", ";");
  174. if (code != PARSE_SUCCESS)
  175. return code;
  176. cf_copy_token(&ep->cfp, &var->type);
  177. /* -------------------------------------- */
  178. /* variable name */
  179. if (!cf_next_valid_token(&ep->cfp))
  180. return PARSE_EOF;
  181. if (cf_token_is(&ep->cfp, ";"))
  182. return PARSE_UNEXPECTED_CONTINUE;
  183. if (cf_token_is(&ep->cfp, "}"))
  184. return PARSE_UNEXPECTED_BREAK;
  185. code = cf_token_is_type(&ep->cfp, CFTOKEN_NAME, "variable name", ";");
  186. if (code != PARSE_SUCCESS)
  187. return code;
  188. cf_copy_token(&ep->cfp, &var->name);
  189. /* -------------------------------------- */
  190. /* variable mapping if any (POSITION, TEXCOORD, etc) */
  191. if (!cf_next_valid_token(&ep->cfp))
  192. return PARSE_EOF;
  193. if (cf_token_is(&ep->cfp, ":")) {
  194. if (!cf_next_valid_token(&ep->cfp))
  195. return PARSE_EOF;
  196. if (cf_token_is(&ep->cfp, ";"))
  197. return PARSE_UNEXPECTED_CONTINUE;
  198. if (cf_token_is(&ep->cfp, "}"))
  199. return PARSE_UNEXPECTED_BREAK;
  200. code = cf_token_is_type(&ep->cfp, CFTOKEN_NAME, "mapping name",
  201. ";");
  202. if (code != PARSE_SUCCESS)
  203. return code;
  204. cf_copy_token(&ep->cfp, &var->mapping);
  205. if (!cf_next_valid_token(&ep->cfp))
  206. return PARSE_EOF;
  207. }
  208. /* -------------------------------------- */
  209. if (!cf_token_is(&ep->cfp, ";")) {
  210. if (!cf_go_to_valid_token(&ep->cfp, ";", "}"))
  211. return PARSE_EOF;
  212. return PARSE_CONTINUE;
  213. }
  214. return PARSE_SUCCESS;
  215. }
  216. static void ep_parse_struct(struct effect_parser *ep)
  217. {
  218. struct ep_struct eps;
  219. ep_struct_init(&eps);
  220. if (cf_next_name(&ep->cfp, &eps.name, "name", ";") != PARSE_SUCCESS)
  221. goto error;
  222. if (cf_next_token_should_be(&ep->cfp, "{", ";", NULL) != PARSE_SUCCESS)
  223. goto error;
  224. /* get structure variables */
  225. while (true) {
  226. bool do_break = false;
  227. struct ep_var var;
  228. ep_var_init(&var);
  229. switch (ep_parse_struct_var(ep, &var)) {
  230. case PARSE_UNEXPECTED_CONTINUE:
  231. cf_adderror_syntax_error(&ep->cfp);
  232. /* Falls through. */
  233. case PARSE_CONTINUE:
  234. ep_var_free(&var);
  235. continue;
  236. case PARSE_UNEXPECTED_BREAK:
  237. cf_adderror_syntax_error(&ep->cfp);
  238. /* Falls through. */
  239. case PARSE_BREAK:
  240. ep_var_free(&var);
  241. do_break = true;
  242. break;
  243. case PARSE_EOF:
  244. ep_var_free(&var);
  245. goto error;
  246. }
  247. if (do_break)
  248. break;
  249. da_push_back(eps.vars, &var);
  250. }
  251. if (cf_next_token_should_be(&ep->cfp, ";", NULL, NULL) != PARSE_SUCCESS)
  252. goto error;
  253. da_push_back(ep->structs, &eps);
  254. return;
  255. error:
  256. ep_struct_free(&eps);
  257. }
  258. static inline int ep_parse_param_annotation_var(struct effect_parser *ep,
  259. struct ep_param *var)
  260. {
  261. int code;
  262. /* -------------------------------------- */
  263. /* variable type */
  264. if (!cf_next_valid_token(&ep->cfp))
  265. return PARSE_EOF;
  266. if (cf_token_is(&ep->cfp, ";"))
  267. return PARSE_CONTINUE;
  268. if (cf_token_is(&ep->cfp, ">"))
  269. return PARSE_BREAK;
  270. code = cf_token_is_type(&ep->cfp, CFTOKEN_NAME, "type name", ";");
  271. if (code != PARSE_SUCCESS)
  272. return code;
  273. bfree(var->type);
  274. cf_copy_token(&ep->cfp, &var->type);
  275. /* -------------------------------------- */
  276. /* variable name */
  277. if (!cf_next_valid_token(&ep->cfp))
  278. return PARSE_EOF;
  279. if (cf_token_is(&ep->cfp, ";")) {
  280. cf_adderror_expecting(&ep->cfp, "variable name");
  281. return PARSE_UNEXPECTED_CONTINUE;
  282. }
  283. if (cf_token_is(&ep->cfp, ">")) {
  284. cf_adderror_expecting(&ep->cfp, "variable name");
  285. return PARSE_UNEXPECTED_BREAK;
  286. }
  287. code = cf_token_is_type(&ep->cfp, CFTOKEN_NAME, "variable name", ";");
  288. if (code != PARSE_SUCCESS)
  289. return code;
  290. bfree(var->name);
  291. cf_copy_token(&ep->cfp, &var->name);
  292. /* -------------------------------------- */
  293. /* variable mapping if any (POSITION, TEXCOORD, etc) */
  294. if (!cf_next_valid_token(&ep->cfp))
  295. return PARSE_EOF;
  296. if (cf_token_is(&ep->cfp, ":")) {
  297. cf_adderror_expecting(&ep->cfp, "= or ;");
  298. return PARSE_UNEXPECTED_BREAK;
  299. } else if (cf_token_is(&ep->cfp, ">")) {
  300. cf_adderror_expecting(&ep->cfp, "= or ;");
  301. return PARSE_UNEXPECTED_BREAK;
  302. } else if (cf_token_is(&ep->cfp, "=")) {
  303. if (!ep_parse_param_assign(ep, var)) {
  304. cf_adderror_expecting(&ep->cfp, "assignment value");
  305. return PARSE_UNEXPECTED_BREAK;
  306. }
  307. }
  308. /* -------------------------------------- */
  309. if (!cf_token_is(&ep->cfp, ";")) {
  310. if (!cf_go_to_valid_token(&ep->cfp, ";", ">")) {
  311. cf_adderror_expecting(&ep->cfp, "; or >");
  312. return PARSE_EOF;
  313. }
  314. return PARSE_CONTINUE;
  315. }
  316. return PARSE_SUCCESS;
  317. }
  318. static int ep_parse_annotations(struct effect_parser *ep,
  319. ep_param_array_t *annotations)
  320. {
  321. if (!cf_token_is(&ep->cfp, "<")) {
  322. cf_adderror_expecting(&ep->cfp, "<");
  323. goto error;
  324. }
  325. /* get annotation variables */
  326. while (true) {
  327. bool do_break = false;
  328. struct ep_param var;
  329. ep_param_init(&var, bstrdup(""), bstrdup(""), false, false,
  330. false);
  331. switch (ep_parse_param_annotation_var(ep, &var)) {
  332. case PARSE_UNEXPECTED_CONTINUE:
  333. cf_adderror_syntax_error(&ep->cfp);
  334. /* Falls through. */
  335. case PARSE_CONTINUE:
  336. ep_param_free(&var);
  337. continue;
  338. case PARSE_UNEXPECTED_BREAK:
  339. cf_adderror_syntax_error(&ep->cfp);
  340. /* Falls through. */
  341. case PARSE_BREAK:
  342. ep_param_free(&var);
  343. do_break = true;
  344. break;
  345. case PARSE_EOF:
  346. ep_param_free(&var);
  347. goto error;
  348. }
  349. if (do_break)
  350. break;
  351. da_push_back(*annotations, &var);
  352. }
  353. if (!cf_token_is(&ep->cfp, ">")) {
  354. cf_adderror_expecting(&ep->cfp, ">");
  355. goto error;
  356. }
  357. if (!cf_next_valid_token(&ep->cfp))
  358. goto error;
  359. return true;
  360. error:
  361. return false;
  362. }
  363. static int ep_parse_param_annotations(struct effect_parser *ep,
  364. struct ep_param *param)
  365. {
  366. return ep_parse_annotations(ep, &param->annotations);
  367. }
  368. static inline int ep_parse_pass_command_call(struct effect_parser *ep,
  369. cf_token_array_t *call)
  370. {
  371. struct cf_token end_token;
  372. cf_token_clear(&end_token);
  373. while (!cf_token_is(&ep->cfp, ";")) {
  374. if (cf_token_is(&ep->cfp, "}")) {
  375. cf_adderror_expecting(&ep->cfp, ";");
  376. return PARSE_CONTINUE;
  377. }
  378. da_push_back(*call, ep->cfp.cur_token);
  379. if (!cf_next_valid_token(&ep->cfp))
  380. return PARSE_EOF;
  381. }
  382. da_push_back(*call, ep->cfp.cur_token);
  383. da_push_back(*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. cf_token_array_t *call;
  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;
  394. } else if (cf_token_is(&ep->cfp, "pixel_shader") ||
  395. cf_token_is(&ep->cfp, "pixel_program")) {
  396. call = &pass->fragment_program;
  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. dstr_array_t *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. da_push_back(*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. dstr_array_t *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, dstr_array_t *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. dstr_array_t *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, dstr_array_t *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. cf_token_array_t *shader_call,
  1458. dstr_array_t *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(ep_param_array_t *ep_annotations,
  1488. gs_effect_param_array_t *gsp_annotations,
  1489. struct effect_parser *ep)
  1490. {
  1491. da_resize(*gsp_annotations, ep_annotations->num);
  1492. size_t i;
  1493. for (i = 0; i < ep_annotations->num; i++) {
  1494. struct gs_effect_param *param = gsp_annotations->array + i;
  1495. struct ep_param *param_in = ep_annotations->array + i;
  1496. param->name = bstrdup(param_in->name);
  1497. param->section = EFFECT_ANNOTATION;
  1498. param->effect = ep->effect;
  1499. da_move(param->default_val, param_in->default_val);
  1500. param->type = get_effect_param_type(param_in->type);
  1501. #if defined(_DEBUG) && defined(_DEBUG_SHADERS)
  1502. debug_param(param, param_in, i, "\t\t");
  1503. #endif
  1504. }
  1505. }
  1506. static void
  1507. ep_compile_param_annotations(struct ep_param *ep_param_input,
  1508. struct gs_effect_param *gs_effect_input,
  1509. struct effect_parser *ep)
  1510. {
  1511. ep_compile_annotations(&(ep_param_input->annotations),
  1512. &(gs_effect_input->annotations), ep);
  1513. }
  1514. static void ep_compile_param(struct effect_parser *ep, size_t idx)
  1515. {
  1516. struct gs_effect_param *param;
  1517. struct ep_param *param_in;
  1518. param = ep->effect->params.array + idx;
  1519. param_in = ep->params.array + idx;
  1520. param_in->param = param;
  1521. param->name = bstrdup(param_in->name);
  1522. param->section = EFFECT_PARAM;
  1523. param->effect = ep->effect;
  1524. da_move(param->default_val, param_in->default_val);
  1525. param->type = get_effect_param_type(param_in->type);
  1526. if (strcmp(param_in->name, "ViewProj") == 0)
  1527. ep->effect->view_proj = param;
  1528. else if (strcmp(param_in->name, "World") == 0)
  1529. ep->effect->world = param;
  1530. #if defined(_DEBUG) && defined(_DEBUG_SHADERS)
  1531. debug_param(param, param_in, idx, "\t");
  1532. #endif
  1533. ep_compile_param_annotations(param_in, param, ep);
  1534. }
  1535. static bool ep_compile_pass_shaderparams(struct effect_parser *ep,
  1536. pass_shaderparam_array_t *pass_params,
  1537. dstr_array_t *used_params,
  1538. gs_shader_t *shader)
  1539. {
  1540. size_t i;
  1541. da_resize(*pass_params, used_params->num);
  1542. for (i = 0; i < pass_params->num; i++) {
  1543. struct dstr *param_name = used_params->array + i;
  1544. struct pass_shaderparam *param = pass_params->array + i;
  1545. param->eparam = gs_effect_get_param_by_name(ep->effect,
  1546. param_name->array);
  1547. param->sparam =
  1548. gs_shader_get_param_by_name(shader, param_name->array);
  1549. #if defined(_DEBUG) && defined(_DEBUG_SHADERS)
  1550. debug_param(param->eparam, 0, i, "\t\t\t\t");
  1551. #endif
  1552. if (!param->sparam) {
  1553. blog(LOG_ERROR, "Effect shader parameter not found");
  1554. return false;
  1555. }
  1556. }
  1557. return true;
  1558. }
  1559. static inline bool ep_compile_pass_shader(struct effect_parser *ep,
  1560. struct gs_effect_technique *tech,
  1561. struct gs_effect_pass *pass,
  1562. struct ep_pass *pass_in,
  1563. size_t pass_idx,
  1564. enum gs_shader_type type)
  1565. {
  1566. struct dstr shader_str;
  1567. struct dstr location;
  1568. dstr_array_t used_params;
  1569. pass_shaderparam_array_t *pass_params = NULL;
  1570. gs_shader_t *shader = NULL;
  1571. bool success = true;
  1572. dstr_init(&shader_str);
  1573. da_init(used_params);
  1574. dstr_init(&location);
  1575. dstr_copy(&location, ep->cfp.lex.file);
  1576. if (type == GS_SHADER_VERTEX)
  1577. dstr_cat(&location, " (Vertex ");
  1578. else if (type == GS_SHADER_PIXEL)
  1579. dstr_cat(&location, " (Pixel ");
  1580. /*else if (type == SHADER_GEOMETRY)
  1581. dstr_cat(&location, " (Geometry ");*/
  1582. assert(pass_idx <= UINT_MAX);
  1583. dstr_catf(&location, "shader, technique %s, pass %u)", tech->name,
  1584. (unsigned)pass_idx);
  1585. if (type == GS_SHADER_VERTEX) {
  1586. ep_makeshaderstring(ep, &shader_str, &pass_in->vertex_program,
  1587. &used_params);
  1588. pass->vertshader = gs_vertexshader_create(shader_str.array,
  1589. location.array, NULL);
  1590. shader = pass->vertshader;
  1591. pass_params = &pass->vertshader_params;
  1592. } else if (type == GS_SHADER_PIXEL) {
  1593. ep_makeshaderstring(ep, &shader_str, &pass_in->fragment_program,
  1594. &used_params);
  1595. pass->pixelshader = gs_pixelshader_create(shader_str.array,
  1596. location.array, NULL);
  1597. shader = pass->pixelshader;
  1598. pass_params = &pass->pixelshader_params;
  1599. }
  1600. #if defined(_DEBUG) && defined(_DEBUG_SHADERS)
  1601. blog(LOG_DEBUG, "\t\t\t%s Shader:",
  1602. type == GS_SHADER_VERTEX ? "Vertex" : "Fragment");
  1603. blog(LOG_DEBUG, "\t\t\tCode:");
  1604. debug_print_string("\t\t\t\t\t", shader_str.array);
  1605. blog(LOG_DEBUG, "\t\t\tParameters:");
  1606. #endif
  1607. if (shader)
  1608. success = ep_compile_pass_shaderparams(ep, pass_params,
  1609. &used_params, shader);
  1610. else
  1611. success = false;
  1612. dstr_free(&location);
  1613. dstr_array_free(used_params.array, used_params.num);
  1614. da_free(used_params);
  1615. dstr_free(&shader_str);
  1616. return success;
  1617. }
  1618. static bool ep_compile_pass(struct effect_parser *ep,
  1619. struct gs_effect_technique *tech,
  1620. struct ep_technique *tech_in, size_t idx)
  1621. {
  1622. struct gs_effect_pass *pass;
  1623. struct ep_pass *pass_in;
  1624. bool success = true;
  1625. pass = tech->passes.array + idx;
  1626. pass_in = tech_in->passes.array + idx;
  1627. pass->name = bstrdup(pass_in->name);
  1628. pass->section = EFFECT_PASS;
  1629. #if defined(_DEBUG) && defined(_DEBUG_SHADERS)
  1630. blog(LOG_DEBUG, "\t\t[%4lld] Pass '%s':", idx, pass->name);
  1631. #endif
  1632. if (!ep_compile_pass_shader(ep, tech, pass, pass_in, idx,
  1633. GS_SHADER_VERTEX)) {
  1634. success = false;
  1635. blog(LOG_ERROR, "Pass (%zu) <%s> missing vertex shader!", idx,
  1636. pass->name ? pass->name : "");
  1637. }
  1638. if (!ep_compile_pass_shader(ep, tech, pass, pass_in, idx,
  1639. GS_SHADER_PIXEL)) {
  1640. success = false;
  1641. blog(LOG_ERROR, "Pass (%zu) <%s> missing pixel shader!", idx,
  1642. pass->name ? pass->name : "");
  1643. }
  1644. return success;
  1645. }
  1646. static inline bool ep_compile_technique(struct effect_parser *ep, size_t idx)
  1647. {
  1648. struct gs_effect_technique *tech;
  1649. struct ep_technique *tech_in;
  1650. bool success = true;
  1651. size_t i;
  1652. tech = ep->effect->techniques.array + idx;
  1653. tech_in = ep->techniques.array + idx;
  1654. tech->name = bstrdup(tech_in->name);
  1655. tech->section = EFFECT_TECHNIQUE;
  1656. tech->effect = ep->effect;
  1657. da_resize(tech->passes, tech_in->passes.num);
  1658. #if defined(_DEBUG) && defined(_DEBUG_SHADERS)
  1659. blog(LOG_DEBUG, "\t[%4lld] Technique '%s' has %lld passes:", idx,
  1660. tech->name, tech->passes.num);
  1661. #endif
  1662. for (i = 0; i < tech->passes.num; i++) {
  1663. if (!ep_compile_pass(ep, tech, tech_in, i))
  1664. success = false;
  1665. }
  1666. return success;
  1667. }
  1668. static bool ep_compile(struct effect_parser *ep)
  1669. {
  1670. bool success = true;
  1671. size_t i;
  1672. assert(ep->effect);
  1673. da_resize(ep->effect->params, ep->params.num);
  1674. da_resize(ep->effect->techniques, ep->techniques.num);
  1675. #if defined(_DEBUG) && defined(_DEBUG_SHADERS)
  1676. blog(LOG_DEBUG, "Shader has %lld parameters:", ep->params.num);
  1677. #endif
  1678. for (i = 0; i < ep->params.num; i++)
  1679. ep_compile_param(ep, i);
  1680. #if defined(_DEBUG) && defined(_DEBUG_SHADERS)
  1681. blog(LOG_DEBUG, "Shader has %lld techniques:", ep->techniques.num);
  1682. #endif
  1683. for (i = 0; i < ep->techniques.num; i++) {
  1684. if (!ep_compile_technique(ep, i))
  1685. success = false;
  1686. }
  1687. return success;
  1688. }