shader-parser.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  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 "../util/platform.h"
  15. #include "shader-parser.h"
  16. enum gs_shader_param_type get_shader_param_type(const char *type)
  17. {
  18. if (strcmp(type, "float") == 0)
  19. return GS_SHADER_PARAM_FLOAT;
  20. else if (strcmp(type, "float2") == 0)
  21. return GS_SHADER_PARAM_VEC2;
  22. else if (strcmp(type, "float3") == 0)
  23. return GS_SHADER_PARAM_VEC3;
  24. else if (strcmp(type, "float4") == 0)
  25. return GS_SHADER_PARAM_VEC4;
  26. else if (strcmp(type, "int2") == 0)
  27. return GS_SHADER_PARAM_INT2;
  28. else if (strcmp(type, "int3") == 0)
  29. return GS_SHADER_PARAM_INT3;
  30. else if (strcmp(type, "int4") == 0)
  31. return GS_SHADER_PARAM_INT4;
  32. else if (astrcmp_n(type, "texture", 7) == 0)
  33. return GS_SHADER_PARAM_TEXTURE;
  34. else if (strcmp(type, "float4x4") == 0)
  35. return GS_SHADER_PARAM_MATRIX4X4;
  36. else if (strcmp(type, "bool") == 0)
  37. return GS_SHADER_PARAM_BOOL;
  38. else if (strcmp(type, "int") == 0)
  39. return GS_SHADER_PARAM_INT;
  40. else if (strcmp(type, "string") == 0)
  41. return GS_SHADER_PARAM_STRING;
  42. return GS_SHADER_PARAM_UNKNOWN;
  43. }
  44. enum gs_sample_filter get_sample_filter(const char *filter)
  45. {
  46. if (astrcmpi(filter, "Anisotropy") == 0)
  47. return GS_FILTER_ANISOTROPIC;
  48. else if (astrcmpi(filter, "Point") == 0 ||
  49. strcmp(filter, "MIN_MAG_MIP_POINT") == 0)
  50. return GS_FILTER_POINT;
  51. else if (astrcmpi(filter, "Linear") == 0 ||
  52. strcmp(filter, "MIN_MAG_MIP_LINEAR") == 0)
  53. return GS_FILTER_LINEAR;
  54. else if (strcmp(filter, "MIN_MAG_POINT_MIP_LINEAR") == 0)
  55. return GS_FILTER_MIN_MAG_POINT_MIP_LINEAR;
  56. else if (strcmp(filter, "MIN_POINT_MAG_LINEAR_MIP_POINT") == 0)
  57. return GS_FILTER_MIN_POINT_MAG_LINEAR_MIP_POINT;
  58. else if (strcmp(filter, "MIN_POINT_MAG_MIP_LINEAR") == 0)
  59. return GS_FILTER_MIN_POINT_MAG_MIP_LINEAR;
  60. else if (strcmp(filter, "MIN_LINEAR_MAG_MIP_POINT") == 0)
  61. return GS_FILTER_MIN_LINEAR_MAG_MIP_POINT;
  62. else if (strcmp(filter, "MIN_LINEAR_MAG_POINT_MIP_LINEAR") == 0)
  63. return GS_FILTER_MIN_LINEAR_MAG_POINT_MIP_LINEAR;
  64. else if (strcmp(filter, "MIN_MAG_LINEAR_MIP_POINT") == 0)
  65. return GS_FILTER_MIN_MAG_LINEAR_MIP_POINT;
  66. return GS_FILTER_LINEAR;
  67. }
  68. extern enum gs_address_mode get_address_mode(const char *mode)
  69. {
  70. if (astrcmpi(mode, "Wrap") == 0 || astrcmpi(mode, "Repeat") == 0)
  71. return GS_ADDRESS_WRAP;
  72. else if (astrcmpi(mode, "Clamp") == 0 || astrcmpi(mode, "None") == 0)
  73. return GS_ADDRESS_CLAMP;
  74. else if (astrcmpi(mode, "Mirror") == 0)
  75. return GS_ADDRESS_MIRROR;
  76. else if (astrcmpi(mode, "Border") == 0)
  77. return GS_ADDRESS_BORDER;
  78. else if (astrcmpi(mode, "MirrorOnce") == 0)
  79. return GS_ADDRESS_MIRRORONCE;
  80. return GS_ADDRESS_CLAMP;
  81. }
  82. void shader_sampler_convert(struct shader_sampler *ss,
  83. struct gs_sampler_info *info)
  84. {
  85. size_t i;
  86. memset(info, 0, sizeof(struct gs_sampler_info));
  87. info->max_anisotropy = 1;
  88. for (i = 0; i < ss->states.num; i++) {
  89. const char *state = ss->states.array[i];
  90. const char *value = ss->values.array[i];
  91. if (astrcmpi(state, "Filter") == 0)
  92. info->filter = get_sample_filter(value);
  93. else if (astrcmpi(state, "AddressU") == 0)
  94. info->address_u = get_address_mode(value);
  95. else if (astrcmpi(state, "AddressV") == 0)
  96. info->address_v = get_address_mode(value);
  97. else if (astrcmpi(state, "AddressW") == 0)
  98. info->address_w = get_address_mode(value);
  99. else if (astrcmpi(state, "MaxAnisotropy") == 0)
  100. info->max_anisotropy = (int)strtol(value, NULL, 10);
  101. else if (astrcmpi(state, "BorderColor") == 0)
  102. info->border_color = strtol(value + 1, NULL, 16);
  103. }
  104. }
  105. /* ------------------------------------------------------------------------- */
  106. static int sp_parse_sampler_state_item(struct shader_parser *sp,
  107. struct shader_sampler *ss)
  108. {
  109. int ret;
  110. char *state = NULL, *value = NULL;
  111. ret = cf_next_name(&sp->cfp, &state, "state name", ";");
  112. if (ret != PARSE_SUCCESS)
  113. goto fail;
  114. ret = cf_next_token_should_be(&sp->cfp, "=", ";", NULL);
  115. if (ret != PARSE_SUCCESS)
  116. goto fail;
  117. ret = cf_next_token_copy(&sp->cfp, &value);
  118. if (ret != PARSE_SUCCESS)
  119. goto fail;
  120. ret = cf_next_token_should_be(&sp->cfp, ";", ";", NULL);
  121. if (ret != PARSE_SUCCESS)
  122. goto fail;
  123. da_push_back(ss->states, &state);
  124. da_push_back(ss->values, &value);
  125. return ret;
  126. fail:
  127. bfree(state);
  128. bfree(value);
  129. return ret;
  130. }
  131. static void sp_parse_sampler_state(struct shader_parser *sp)
  132. {
  133. struct shader_sampler ss;
  134. struct cf_token peek;
  135. shader_sampler_init(&ss);
  136. if (cf_next_name(&sp->cfp, &ss.name, "name", ";") != PARSE_SUCCESS)
  137. goto error;
  138. if (cf_next_token_should_be(&sp->cfp, "{", ";", NULL) != PARSE_SUCCESS)
  139. goto error;
  140. if (!cf_peek_valid_token(&sp->cfp, &peek))
  141. goto error;
  142. while (strref_cmp(&peek.str, "}") != 0) {
  143. int ret = sp_parse_sampler_state_item(sp, &ss);
  144. if (ret == PARSE_EOF)
  145. goto error;
  146. if (!cf_peek_valid_token(&sp->cfp, &peek))
  147. goto error;
  148. }
  149. if (cf_next_token_should_be(&sp->cfp, "}", ";", NULL) != PARSE_SUCCESS)
  150. goto error;
  151. if (cf_next_token_should_be(&sp->cfp, ";", NULL, NULL) != PARSE_SUCCESS)
  152. goto error;
  153. da_push_back(sp->samplers, &ss);
  154. return;
  155. error:
  156. shader_sampler_free(&ss);
  157. }
  158. static inline int sp_parse_struct_var(struct shader_parser *sp,
  159. struct shader_var *var)
  160. {
  161. int code;
  162. /* -------------------------------------- */
  163. /* variable type */
  164. if (!cf_next_valid_token(&sp->cfp))
  165. return PARSE_EOF;
  166. if (cf_token_is(&sp->cfp, ";"))
  167. return PARSE_CONTINUE;
  168. if (cf_token_is(&sp->cfp, "}"))
  169. return PARSE_BREAK;
  170. code = cf_token_is_type(&sp->cfp, CFTOKEN_NAME, "type name", ";");
  171. if (code != PARSE_SUCCESS)
  172. return code;
  173. cf_copy_token(&sp->cfp, &var->type);
  174. /* -------------------------------------- */
  175. /* variable name */
  176. if (!cf_next_valid_token(&sp->cfp))
  177. return PARSE_EOF;
  178. if (cf_token_is(&sp->cfp, ";"))
  179. return PARSE_UNEXPECTED_CONTINUE;
  180. if (cf_token_is(&sp->cfp, "}"))
  181. return PARSE_UNEXPECTED_BREAK;
  182. code = cf_token_is_type(&sp->cfp, CFTOKEN_NAME, "variable name", ";");
  183. if (code != PARSE_SUCCESS)
  184. return code;
  185. cf_copy_token(&sp->cfp, &var->name);
  186. /* -------------------------------------- */
  187. /* variable mapping if any (POSITION, TEXCOORD, etc) */
  188. if (!cf_next_valid_token(&sp->cfp))
  189. return PARSE_EOF;
  190. if (cf_token_is(&sp->cfp, ":")) {
  191. if (!cf_next_valid_token(&sp->cfp))
  192. return PARSE_EOF;
  193. if (cf_token_is(&sp->cfp, ";"))
  194. return PARSE_UNEXPECTED_CONTINUE;
  195. if (cf_token_is(&sp->cfp, "}"))
  196. return PARSE_UNEXPECTED_BREAK;
  197. code = cf_token_is_type(&sp->cfp, CFTOKEN_NAME, "mapping name",
  198. ";");
  199. if (code != PARSE_SUCCESS)
  200. return code;
  201. cf_copy_token(&sp->cfp, &var->mapping);
  202. if (!cf_next_valid_token(&sp->cfp))
  203. return PARSE_EOF;
  204. }
  205. /* -------------------------------------- */
  206. if (!cf_token_is(&sp->cfp, ";")) {
  207. if (!cf_go_to_valid_token(&sp->cfp, ";", "}"))
  208. return PARSE_EOF;
  209. return PARSE_CONTINUE;
  210. }
  211. return PARSE_SUCCESS;
  212. }
  213. static void sp_parse_struct(struct shader_parser *sp)
  214. {
  215. struct shader_struct ss;
  216. shader_struct_init(&ss);
  217. if (cf_next_name(&sp->cfp, &ss.name, "name", ";") != PARSE_SUCCESS)
  218. goto error;
  219. if (cf_next_token_should_be(&sp->cfp, "{", ";", NULL) != PARSE_SUCCESS)
  220. goto error;
  221. /* get structure variables */
  222. while (true) {
  223. bool do_break = false;
  224. struct shader_var var;
  225. shader_var_init(&var);
  226. switch (sp_parse_struct_var(sp, &var)) {
  227. case PARSE_UNEXPECTED_CONTINUE:
  228. cf_adderror_syntax_error(&sp->cfp);
  229. /* Falls through. */
  230. case PARSE_CONTINUE:
  231. shader_var_free(&var);
  232. continue;
  233. case PARSE_UNEXPECTED_BREAK:
  234. cf_adderror_syntax_error(&sp->cfp);
  235. /* Falls through. */
  236. case PARSE_BREAK:
  237. shader_var_free(&var);
  238. do_break = true;
  239. break;
  240. case PARSE_EOF:
  241. shader_var_free(&var);
  242. goto error;
  243. }
  244. if (do_break)
  245. break;
  246. da_push_back(ss.vars, &var);
  247. }
  248. if (cf_next_token_should_be(&sp->cfp, ";", NULL, NULL) != PARSE_SUCCESS)
  249. goto error;
  250. da_push_back(sp->structs, &ss);
  251. return;
  252. error:
  253. shader_struct_free(&ss);
  254. }
  255. static inline int sp_check_for_keyword(struct shader_parser *sp,
  256. const char *keyword, bool *val)
  257. {
  258. bool new_val = cf_token_is(&sp->cfp, keyword);
  259. if (new_val) {
  260. if (!cf_next_valid_token(&sp->cfp))
  261. return PARSE_EOF;
  262. if (new_val && *val)
  263. cf_adderror(&sp->cfp, "'$1' keyword already specified",
  264. LEX_WARNING, keyword, NULL, NULL);
  265. *val = new_val;
  266. return PARSE_CONTINUE;
  267. }
  268. return PARSE_SUCCESS;
  269. }
  270. static inline int sp_parse_func_param(struct shader_parser *sp,
  271. struct shader_var *var)
  272. {
  273. int code;
  274. bool var_type_keyword = false;
  275. if (!cf_next_valid_token(&sp->cfp))
  276. return PARSE_EOF;
  277. code = sp_check_for_keyword(sp, "in", &var_type_keyword);
  278. if (code == PARSE_EOF)
  279. return PARSE_EOF;
  280. else if (var_type_keyword)
  281. var->var_type = SHADER_VAR_IN;
  282. if (!var_type_keyword) {
  283. code = sp_check_for_keyword(sp, "inout", &var_type_keyword);
  284. if (code == PARSE_EOF)
  285. return PARSE_EOF;
  286. else if (var_type_keyword)
  287. var->var_type = SHADER_VAR_INOUT;
  288. }
  289. if (!var_type_keyword) {
  290. code = sp_check_for_keyword(sp, "out", &var_type_keyword);
  291. if (code == PARSE_EOF)
  292. return PARSE_EOF;
  293. else if (var_type_keyword)
  294. var->var_type = SHADER_VAR_OUT;
  295. }
  296. if (!var_type_keyword) {
  297. code = sp_check_for_keyword(sp, "uniform", &var_type_keyword);
  298. if (code == PARSE_EOF)
  299. return PARSE_EOF;
  300. else if (var_type_keyword)
  301. var->var_type = SHADER_VAR_UNIFORM;
  302. }
  303. code = cf_get_name(&sp->cfp, &var->type, "type", ")");
  304. if (code != PARSE_SUCCESS)
  305. return code;
  306. code = cf_next_name(&sp->cfp, &var->name, "name", ")");
  307. if (code != PARSE_SUCCESS)
  308. return code;
  309. if (!cf_next_valid_token(&sp->cfp))
  310. return PARSE_EOF;
  311. if (cf_token_is(&sp->cfp, ":")) {
  312. code = cf_next_name(&sp->cfp, &var->mapping,
  313. "mapping specifier", ")");
  314. if (code != PARSE_SUCCESS)
  315. return code;
  316. if (!cf_next_valid_token(&sp->cfp))
  317. return PARSE_EOF;
  318. }
  319. return PARSE_SUCCESS;
  320. }
  321. static bool sp_parse_func_params(struct shader_parser *sp,
  322. struct shader_func *func)
  323. {
  324. struct cf_token peek;
  325. int code;
  326. cf_token_clear(&peek);
  327. if (!cf_peek_valid_token(&sp->cfp, &peek))
  328. return false;
  329. if (*peek.str.array == ')') {
  330. cf_next_token(&sp->cfp);
  331. goto exit;
  332. }
  333. do {
  334. struct shader_var var;
  335. shader_var_init(&var);
  336. if (!cf_token_is(&sp->cfp, "(") && !cf_token_is(&sp->cfp, ","))
  337. cf_adderror_syntax_error(&sp->cfp);
  338. code = sp_parse_func_param(sp, &var);
  339. if (code != PARSE_SUCCESS) {
  340. shader_var_free(&var);
  341. if (code == PARSE_CONTINUE)
  342. goto exit;
  343. else if (code == PARSE_EOF)
  344. return false;
  345. }
  346. da_push_back(func->params, &var);
  347. } while (!cf_token_is(&sp->cfp, ")"));
  348. exit:
  349. return true;
  350. }
  351. static void sp_parse_function(struct shader_parser *sp, char *type, char *name)
  352. {
  353. struct shader_func func;
  354. shader_func_init(&func, type, name);
  355. if (!sp_parse_func_params(sp, &func))
  356. goto error;
  357. if (!cf_next_valid_token(&sp->cfp))
  358. goto error;
  359. /* if function is mapped to something, for example COLOR */
  360. if (cf_token_is(&sp->cfp, ":")) {
  361. char *mapping = NULL;
  362. int errorcode =
  363. cf_next_name(&sp->cfp, &mapping, "mapping", "{");
  364. if (errorcode != PARSE_SUCCESS)
  365. goto error;
  366. func.mapping = mapping;
  367. if (!cf_next_valid_token(&sp->cfp))
  368. goto error;
  369. }
  370. if (!cf_token_is(&sp->cfp, "{")) {
  371. cf_adderror_expecting(&sp->cfp, "{");
  372. goto error;
  373. }
  374. func.start = sp->cfp.cur_token;
  375. if (!cf_pass_pair(&sp->cfp, '{', '}'))
  376. goto error;
  377. /* it is established that the current token is '}' if we reach this */
  378. cf_next_token(&sp->cfp);
  379. func.end = sp->cfp.cur_token;
  380. da_push_back(sp->funcs, &func);
  381. return;
  382. error:
  383. shader_func_free(&func);
  384. }
  385. /* parses "array[count]" */
  386. static bool sp_parse_param_array(struct shader_parser *sp,
  387. struct shader_var *param)
  388. {
  389. if (!cf_next_valid_token(&sp->cfp))
  390. return false;
  391. if (sp->cfp.cur_token->type != CFTOKEN_NUM ||
  392. !valid_int_str(sp->cfp.cur_token->str.array,
  393. sp->cfp.cur_token->str.len))
  394. return false;
  395. param->array_count =
  396. (int)strtol(sp->cfp.cur_token->str.array, NULL, 10);
  397. if (cf_next_token_should_be(&sp->cfp, "]", ";", NULL) == PARSE_EOF)
  398. return false;
  399. if (!cf_next_valid_token(&sp->cfp))
  400. return false;
  401. return true;
  402. }
  403. static inline int sp_parse_param_assign_intfloat(struct shader_parser *sp,
  404. struct shader_var *param,
  405. bool is_float)
  406. {
  407. int code;
  408. bool is_negative = false;
  409. if (!cf_next_valid_token(&sp->cfp))
  410. return PARSE_EOF;
  411. if (cf_token_is(&sp->cfp, "-")) {
  412. is_negative = true;
  413. if (!cf_next_token(&sp->cfp))
  414. return PARSE_EOF;
  415. }
  416. code = cf_token_is_type(&sp->cfp, CFTOKEN_NUM, "numeric value", ";");
  417. if (code != PARSE_SUCCESS)
  418. return code;
  419. if (is_float) {
  420. float f = (float)os_strtod(sp->cfp.cur_token->str.array);
  421. if (is_negative)
  422. f = -f;
  423. da_push_back_array(param->default_val, (uint8_t *)&f,
  424. sizeof(float));
  425. } else {
  426. long l = strtol(sp->cfp.cur_token->str.array, NULL, 10);
  427. if (is_negative)
  428. l = -l;
  429. da_push_back_array(param->default_val, (uint8_t *)&l,
  430. sizeof(long));
  431. }
  432. return PARSE_SUCCESS;
  433. }
  434. /*
  435. * parses assignment for float1, float2, float3, float4, and any combination
  436. * for float3x3, float4x4, etc
  437. */
  438. static inline int sp_parse_param_assign_float_array(struct shader_parser *sp,
  439. struct shader_var *param)
  440. {
  441. const char *float_type = param->type + 5;
  442. int float_count = 0, code, i;
  443. /* -------------------------------------------- */
  444. if (float_type[0] < '1' || float_type[0] > '4')
  445. cf_adderror(&sp->cfp, "Invalid row count", LEX_ERROR, NULL,
  446. NULL, NULL);
  447. float_count = float_type[0] - '0';
  448. if (float_type[1] == 'x') {
  449. if (float_type[2] < '1' || float_type[2] > '4')
  450. cf_adderror(&sp->cfp, "Invalid column count", LEX_ERROR,
  451. NULL, NULL, NULL);
  452. float_count *= float_type[2] - '0';
  453. }
  454. /* -------------------------------------------- */
  455. code = cf_next_token_should_be(&sp->cfp, "{", ";", NULL);
  456. if (code != PARSE_SUCCESS)
  457. return code;
  458. for (i = 0; i < float_count; i++) {
  459. char *next = ((i + 1) < float_count) ? "," : "}";
  460. code = sp_parse_param_assign_intfloat(sp, param, true);
  461. if (code != PARSE_SUCCESS)
  462. return code;
  463. code = cf_next_token_should_be(&sp->cfp, next, ";", NULL);
  464. if (code != PARSE_SUCCESS)
  465. return code;
  466. }
  467. return PARSE_SUCCESS;
  468. }
  469. static int sp_parse_param_assignment_val(struct shader_parser *sp,
  470. struct shader_var *param)
  471. {
  472. if (strcmp(param->type, "int") == 0)
  473. return sp_parse_param_assign_intfloat(sp, param, false);
  474. else if (strcmp(param->type, "float") == 0)
  475. return sp_parse_param_assign_intfloat(sp, param, true);
  476. else if (astrcmp_n(param->type, "float", 5) == 0)
  477. return sp_parse_param_assign_float_array(sp, param);
  478. cf_adderror(&sp->cfp, "Invalid type '$1' used for assignment",
  479. LEX_ERROR, param->type, NULL, NULL);
  480. return PARSE_CONTINUE;
  481. }
  482. static inline bool sp_parse_param_assign(struct shader_parser *sp,
  483. struct shader_var *param)
  484. {
  485. if (sp_parse_param_assignment_val(sp, param) != PARSE_SUCCESS)
  486. return false;
  487. if (!cf_next_valid_token(&sp->cfp))
  488. return false;
  489. return true;
  490. }
  491. static void sp_parse_param(struct shader_parser *sp, char *type, char *name,
  492. bool is_const, bool is_uniform)
  493. {
  494. struct shader_var param;
  495. shader_var_init_param(&param, type, name, is_uniform, is_const);
  496. if (cf_token_is(&sp->cfp, ";"))
  497. goto complete;
  498. if (cf_token_is(&sp->cfp, "[") && !sp_parse_param_array(sp, &param))
  499. goto error;
  500. if (cf_token_is(&sp->cfp, "=") && !sp_parse_param_assign(sp, &param))
  501. goto error;
  502. if (!cf_token_is(&sp->cfp, ";"))
  503. goto error;
  504. complete:
  505. da_push_back(sp->params, &param);
  506. return;
  507. error:
  508. shader_var_free(&param);
  509. }
  510. static bool sp_get_var_specifiers(struct shader_parser *sp, bool *is_const,
  511. bool *is_uniform)
  512. {
  513. while (true) {
  514. int code = sp_check_for_keyword(sp, "const", is_const);
  515. if (code == PARSE_EOF)
  516. return false;
  517. else if (code == PARSE_CONTINUE)
  518. continue;
  519. code = sp_check_for_keyword(sp, "uniform", is_uniform);
  520. if (code == PARSE_EOF)
  521. return false;
  522. else if (code == PARSE_CONTINUE)
  523. continue;
  524. break;
  525. }
  526. return true;
  527. }
  528. static inline void report_invalid_func_keyword(struct shader_parser *sp,
  529. const char *name, bool val)
  530. {
  531. if (val)
  532. cf_adderror(&sp->cfp,
  533. "'$1' keyword cannot be used with a "
  534. "function",
  535. LEX_ERROR, name, NULL, NULL);
  536. }
  537. static void sp_parse_other(struct shader_parser *sp)
  538. {
  539. bool is_const = false, is_uniform = false;
  540. char *type = NULL, *name = NULL;
  541. if (!sp_get_var_specifiers(sp, &is_const, &is_uniform))
  542. goto error;
  543. if (cf_get_name(&sp->cfp, &type, "type", ";") != PARSE_SUCCESS)
  544. goto error;
  545. if (cf_next_name(&sp->cfp, &name, "name", ";") != PARSE_SUCCESS)
  546. goto error;
  547. if (!cf_next_valid_token(&sp->cfp))
  548. goto error;
  549. if (cf_token_is(&sp->cfp, "(")) {
  550. report_invalid_func_keyword(sp, "const", is_const);
  551. report_invalid_func_keyword(sp, "uniform", is_uniform);
  552. sp_parse_function(sp, type, name);
  553. return;
  554. } else {
  555. sp_parse_param(sp, type, name, is_const, is_uniform);
  556. return;
  557. }
  558. error:
  559. bfree(type);
  560. bfree(name);
  561. }
  562. bool shader_parse(struct shader_parser *sp, const char *shader,
  563. const char *file)
  564. {
  565. if (!cf_parser_parse(&sp->cfp, shader, file))
  566. return false;
  567. while (sp->cfp.cur_token && sp->cfp.cur_token->type != CFTOKEN_NONE) {
  568. if (cf_token_is(&sp->cfp, ";") ||
  569. is_whitespace(*sp->cfp.cur_token->str.array)) {
  570. sp->cfp.cur_token++;
  571. } else if (cf_token_is(&sp->cfp, "struct")) {
  572. sp_parse_struct(sp);
  573. } else if (cf_token_is(&sp->cfp, "sampler_state")) {
  574. sp_parse_sampler_state(sp);
  575. } else if (cf_token_is(&sp->cfp, "{")) {
  576. cf_adderror(&sp->cfp, "Unexpected code segment",
  577. LEX_ERROR, NULL, NULL, NULL);
  578. cf_pass_pair(&sp->cfp, '{', '}');
  579. } else {
  580. /* parameters and functions */
  581. sp_parse_other(sp);
  582. }
  583. }
  584. return !error_data_has_errors(&sp->cfp.error_list);
  585. }