1
0

effect-parser.h 6.7 KB

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