effect-parser.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. #pragma once
  15. #include "../util/darray.h"
  16. #include "../util/cf-parser.h"
  17. #include "graphics.h"
  18. #include "shader-parser.h"
  19. #ifdef __cplusplus
  20. extern "C" {
  21. #endif
  22. struct dstr;
  23. /*
  24. * The effect parser takes an effect file and converts it into individual
  25. * shaders for each technique's pass. It automatically writes all dependent
  26. * structures/functions/parameters to the shader and builds shader text for
  27. * each shader component of each pass.
  28. */
  29. /* ------------------------------------------------------------------------- */
  30. /* effect parser var data */
  31. enum ep_var_type {
  32. EP_VAR_NONE,
  33. EP_VAR_IN = EP_VAR_NONE,
  34. EP_VAR_INOUT,
  35. EP_VAR_OUT,
  36. EP_VAR_UNIFORM
  37. };
  38. struct ep_var {
  39. char *type, *name, *mapping;
  40. enum ep_var_type var_type;
  41. };
  42. static inline void ep_var_init(struct ep_var *epv)
  43. {
  44. memset(epv, 0, sizeof(struct ep_var));
  45. }
  46. static inline void ep_var_free(struct ep_var *epv)
  47. {
  48. bfree(epv->type);
  49. bfree(epv->name);
  50. bfree(epv->mapping);
  51. }
  52. /* ------------------------------------------------------------------------- */
  53. /* effect parser param data */
  54. struct ep_param {
  55. char *type, *name;
  56. DARRAY(uint8_t) default_val;
  57. DARRAY(char *) properties;
  58. struct gs_effect_param *param;
  59. bool is_const, is_property, is_uniform, is_texture, written;
  60. int writeorder, array_count;
  61. DARRAY(struct ep_param) annotations;
  62. };
  63. extern void ep_param_writevar(struct dstr *dst, struct darray *use_params);
  64. static inline void ep_param_init(struct ep_param *epp, char *type, char *name,
  65. bool is_property, bool is_const,
  66. bool is_uniform)
  67. {
  68. epp->type = type;
  69. epp->name = name;
  70. epp->is_property = is_property;
  71. epp->is_const = is_const;
  72. epp->is_uniform = is_uniform;
  73. epp->is_texture = (astrcmp_n(epp->type, "texture", 7) == 0);
  74. epp->written = false;
  75. epp->writeorder = false;
  76. epp->array_count = 0;
  77. da_init(epp->default_val);
  78. da_init(epp->properties);
  79. da_init(epp->annotations);
  80. }
  81. static inline void ep_param_free(struct ep_param *epp)
  82. {
  83. bfree(epp->type);
  84. bfree(epp->name);
  85. da_free(epp->default_val);
  86. da_free(epp->properties);
  87. for (size_t i = 0; i < epp->annotations.num; i++)
  88. ep_param_free(epp->annotations.array + i);
  89. da_free(epp->annotations);
  90. }
  91. /* ------------------------------------------------------------------------- */
  92. /* effect parser struct data */
  93. struct ep_struct {
  94. char *name;
  95. DARRAY(struct ep_var) vars; /* struct ep_var */
  96. bool written;
  97. };
  98. static inline bool ep_struct_mapped(struct ep_struct *eps)
  99. {
  100. if (eps->vars.num > 0)
  101. return eps->vars.array[0].mapping != NULL;
  102. return false;
  103. }
  104. static inline void ep_struct_init(struct ep_struct *eps)
  105. {
  106. memset(eps, 0, sizeof(struct ep_struct));
  107. }
  108. static inline void ep_struct_free(struct ep_struct *eps)
  109. {
  110. size_t i;
  111. bfree(eps->name);
  112. for (i = 0; i < eps->vars.num; i++)
  113. ep_var_free(eps->vars.array + i);
  114. da_free(eps->vars);
  115. }
  116. /* ------------------------------------------------------------------------- */
  117. /* effect parser sampler data */
  118. struct ep_sampler {
  119. char *name;
  120. DARRAY(char *) states;
  121. DARRAY(char *) values;
  122. bool written;
  123. };
  124. static inline void ep_sampler_init(struct ep_sampler *eps)
  125. {
  126. memset(eps, 0, sizeof(struct ep_sampler));
  127. }
  128. static inline void ep_sampler_free(struct ep_sampler *eps)
  129. {
  130. size_t i;
  131. for (i = 0; i < eps->states.num; i++)
  132. bfree(eps->states.array[i]);
  133. for (i = 0; i < eps->values.num; i++)
  134. bfree(eps->values.array[i]);
  135. bfree(eps->name);
  136. da_free(eps->states);
  137. da_free(eps->values);
  138. }
  139. /* ------------------------------------------------------------------------- */
  140. /* effect parser pass data */
  141. struct ep_pass {
  142. char *name;
  143. DARRAY(struct cf_token) vertex_program;
  144. DARRAY(struct cf_token) fragment_program;
  145. struct gs_effect_pass *pass;
  146. };
  147. static inline void ep_pass_init(struct ep_pass *epp)
  148. {
  149. memset(epp, 0, sizeof(struct ep_pass));
  150. }
  151. static inline void ep_pass_free(struct ep_pass *epp)
  152. {
  153. bfree(epp->name);
  154. da_free(epp->vertex_program);
  155. da_free(epp->fragment_program);
  156. }
  157. /* ------------------------------------------------------------------------- */
  158. /* effect parser technique data */
  159. struct ep_technique {
  160. char *name;
  161. DARRAY(struct ep_pass) passes; /* struct ep_pass */
  162. };
  163. static inline void ep_technique_init(struct ep_technique *ept)
  164. {
  165. memset(ept, 0, sizeof(struct ep_technique));
  166. }
  167. static inline void ep_technique_free(struct ep_technique *ept)
  168. {
  169. size_t i;
  170. for (i = 0; i < ept->passes.num; i++)
  171. ep_pass_free(ept->passes.array + i);
  172. bfree(ept->name);
  173. da_free(ept->passes);
  174. }
  175. /* ------------------------------------------------------------------------- */
  176. /* effect parser function data */
  177. struct ep_func {
  178. char *name, *ret_type, *mapping;
  179. struct dstr contents;
  180. DARRAY(struct ep_var) param_vars;
  181. DARRAY(const char *) func_deps;
  182. DARRAY(const char *) struct_deps;
  183. DARRAY(const char *) param_deps;
  184. DARRAY(const char *) sampler_deps;
  185. bool written;
  186. };
  187. static inline void ep_func_init(struct ep_func *epf, char *ret_type, char *name)
  188. {
  189. memset(epf, 0, sizeof(struct ep_func));
  190. epf->name = name;
  191. epf->ret_type = ret_type;
  192. }
  193. static inline void ep_func_free(struct ep_func *epf)
  194. {
  195. size_t i;
  196. for (i = 0; i < epf->param_vars.num; i++)
  197. ep_var_free(epf->param_vars.array + i);
  198. bfree(epf->name);
  199. bfree(epf->ret_type);
  200. bfree(epf->mapping);
  201. dstr_free(&epf->contents);
  202. da_free(epf->param_vars);
  203. da_free(epf->func_deps);
  204. da_free(epf->struct_deps);
  205. da_free(epf->param_deps);
  206. da_free(epf->sampler_deps);
  207. }
  208. /* ------------------------------------------------------------------------- */
  209. struct effect_parser {
  210. gs_effect_t *effect;
  211. DARRAY(struct ep_param) params;
  212. DARRAY(struct ep_struct) structs;
  213. DARRAY(struct ep_func) funcs;
  214. DARRAY(struct ep_sampler) samplers;
  215. DARRAY(struct ep_technique) techniques;
  216. /* internal vars */
  217. DARRAY(struct cf_lexer) files;
  218. DARRAY(struct cf_token) tokens;
  219. struct gs_effect_pass *cur_pass;
  220. struct cf_parser cfp;
  221. };
  222. static inline void ep_init(struct effect_parser *ep)
  223. {
  224. da_init(ep->params);
  225. da_init(ep->structs);
  226. da_init(ep->funcs);
  227. da_init(ep->samplers);
  228. da_init(ep->techniques);
  229. da_init(ep->files);
  230. da_init(ep->tokens);
  231. ep->cur_pass = NULL;
  232. cf_parser_init(&ep->cfp);
  233. }
  234. extern void ep_free(struct effect_parser *ep);
  235. extern bool ep_parse(struct effect_parser *ep, gs_effect_t *effect,
  236. const char *effect_string, const char *file);
  237. #ifdef __cplusplus
  238. }
  239. #endif