effect-parser.h 7.0 KB

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