Open Broadcaster Software
Free, open source software for live streaming and recording
effect-parser.h
Go to the documentation of this file.
1 /******************************************************************************
2  Copyright (C) 2013 by Hugh Bailey <obs.jim@gmail.com>
3 
4  This program is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 2 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program. If not, see <http://www.gnu.org/licenses/>.
16 ******************************************************************************/
17 
18 #pragma once
19 
20 #include "../util/darray.h"
21 #include "../util/cf-parser.h"
22 #include "graphics.h"
23 #include "shader-parser.h"
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
29 struct dstr;
30 
31 /*
32  * The effect parser takes an effect file and converts it into individual
33  * shaders for each technique's pass. It automatically writes all dependent
34  * structures/functions/parameters to the shader and builds shader text for
35  * each shader component of each pass.
36  */
37 
38 /* ------------------------------------------------------------------------- */
39 /* effect parser var data */
40 
47 };
48 
49 struct ep_var {
50  char *type, *name, *mapping;
52 };
53 
54 static inline void ep_var_init(struct ep_var *epv)
55 {
56  memset(epv, 0, sizeof(struct ep_var));
57 }
58 
59 static inline void ep_var_free(struct ep_var *epv)
60 {
61  bfree(epv->type);
62  bfree(epv->name);
63  bfree(epv->mapping);
64 }
65 
66 /* ------------------------------------------------------------------------- */
67 /* effect parser param data */
68 
69 struct ep_param {
70  char *type, *name;
71  DARRAY(uint8_t) default_val;
72  DARRAY(char*) properties;
76  DARRAY(struct ep_param) annotations;
77 };
78 
79 extern void ep_param_writevar(struct dstr *dst, struct darray *use_params);
80 
81 static inline void ep_param_init(struct ep_param *epp,
82  char *type, char *name,
83  bool is_property, bool is_const, bool is_uniform)
84 {
85  epp->type = type;
86  epp->name = name;
87  epp->is_property = is_property;
88  epp->is_const = is_const;
89  epp->is_uniform = is_uniform;
90  epp->is_texture = (astrcmp_n(epp->type, "texture", 7) == 0);
91  epp->written = false;
92  epp->writeorder = false;
93  epp->array_count = 0;
94  da_init(epp->default_val);
95  da_init(epp->properties);
96  da_init(epp->annotations);
97 }
98 
99 static inline void ep_param_free(struct ep_param *epp)
100 {
101  bfree(epp->type);
102  bfree(epp->name);
103  da_free(epp->default_val);
104  da_free(epp->properties);
105 
106  for (size_t i = 0; i < epp->annotations.num; i++)
107  ep_param_free(epp->annotations.array + i);
108  da_free(epp->annotations);
109 }
110 
111 /* ------------------------------------------------------------------------- */
112 /* effect parser struct data */
113 
114 struct ep_struct {
115  char *name;
116  DARRAY(struct ep_var) vars; /* struct ep_var */
117  bool written;
118 };
119 
120 static inline bool ep_struct_mapped(struct ep_struct *eps)
121 {
122  if (eps->vars.num > 0)
123  return eps->vars.array[0].mapping != NULL;
124 
125  return false;
126 }
127 
128 static inline void ep_struct_init(struct ep_struct *eps)
129 {
130  memset(eps, 0, sizeof(struct ep_struct));
131 }
132 
133 static inline void ep_struct_free(struct ep_struct *eps)
134 {
135  size_t i;
136 
137  bfree(eps->name);
138  for (i = 0; i < eps->vars.num; i++)
139  ep_var_free(eps->vars.array+i);
140  da_free(eps->vars);
141 }
142 
143 /* ------------------------------------------------------------------------- */
144 /* effect parser sampler data */
145 
146 struct ep_sampler {
147  char *name;
148  DARRAY(char*) states;
149  DARRAY(char*) values;
150 
151  bool written;
152 };
153 
154 static inline void ep_sampler_init(struct ep_sampler *eps)
155 {
156  memset(eps, 0, sizeof(struct ep_sampler));
157 }
158 
159 static inline void ep_sampler_free(struct ep_sampler *eps)
160 {
161  size_t i;
162 
163  for (i = 0; i < eps->states.num; i++)
164  bfree(eps->states.array[i]);
165  for (i = 0; i < eps->values.num; i++)
166  bfree(eps->values.array[i]);
167 
168  bfree(eps->name);
169  da_free(eps->states);
170  da_free(eps->values);
171 }
172 
173 /* ------------------------------------------------------------------------- */
174 /* effect parser pass data */
175 
176 struct ep_pass {
177  char *name;
178  DARRAY(struct cf_token) vertex_program;
179  DARRAY(struct cf_token) fragment_program;
181 };
182 
183 static inline void ep_pass_init(struct ep_pass *epp)
184 {
185  memset(epp, 0, sizeof(struct ep_pass));
186 }
187 
188 static inline void ep_pass_free(struct ep_pass *epp)
189 {
190  bfree(epp->name);
191  da_free(epp->vertex_program);
192  da_free(epp->fragment_program);
193 }
194 
195 /* ------------------------------------------------------------------------- */
196 /* effect parser technique data */
197 
198 struct ep_technique {
199  char *name;
200  DARRAY(struct ep_pass) passes; /* struct ep_pass */
201 };
202 
203 static inline void ep_technique_init(struct ep_technique *ept)
204 {
205  memset(ept, 0, sizeof(struct ep_technique));
206 }
207 
208 static inline void ep_technique_free(struct ep_technique *ept)
209 {
210  size_t i;
211 
212  for (i = 0; i < ept->passes.num; i++)
213  ep_pass_free(ept->passes.array+i);
214 
215  bfree(ept->name);
216  da_free(ept->passes);
217 }
218 
219 /* ------------------------------------------------------------------------- */
220 /* effect parser function data */
221 
222 struct ep_func {
223  char *name, *ret_type, *mapping;
224  struct dstr contents;
225  DARRAY(struct ep_var) param_vars;
226  DARRAY(const char*) func_deps;
227  DARRAY(const char*) struct_deps;
228  DARRAY(const char*) param_deps;
229  DARRAY(const char*) sampler_deps;
230  bool written;
231 };
232 
233 static inline void ep_func_init(struct ep_func *epf, char *ret_type,
234  char *name)
235 {
236  memset(epf, 0, sizeof(struct ep_func));
237  epf->name = name;
238  epf->ret_type = ret_type;
239 }
240 
241 static inline void ep_func_free(struct ep_func *epf)
242 {
243  size_t i;
244  for (i = 0; i < epf->param_vars.num; i++)
245  ep_var_free(epf->param_vars.array+i);
246 
247  bfree(epf->name);
248  bfree(epf->ret_type);
249  bfree(epf->mapping);
250  dstr_free(&epf->contents);
251  da_free(epf->param_vars);
252  da_free(epf->func_deps);
253  da_free(epf->struct_deps);
254  da_free(epf->param_deps);
255  da_free(epf->sampler_deps);
256 }
257 
258 /* ------------------------------------------------------------------------- */
259 
262 
263  DARRAY(struct ep_param) params;
264  DARRAY(struct ep_struct) structs;
265  DARRAY(struct ep_func) funcs;
266  DARRAY(struct ep_sampler) samplers;
267  DARRAY(struct ep_technique) techniques;
268 
269  /* internal vars */
270  DARRAY(struct cf_lexer) files;
271  DARRAY(struct cf_token) tokens;
273 
274  struct cf_parser cfp;
275 };
276 
277 static inline void ep_init(struct effect_parser *ep)
278 {
279  da_init(ep->params);
280  da_init(ep->structs);
281  da_init(ep->funcs);
282  da_init(ep->samplers);
283  da_init(ep->techniques);
284  da_init(ep->files);
285  da_init(ep->tokens);
286 
287  ep->cur_pass = NULL;
288  cf_parser_init(&ep->cfp);
289 }
290 
291 extern void ep_free(struct effect_parser *ep);
292 
293 extern bool ep_parse(struct effect_parser *ep, gs_effect_t *effect,
294  const char *effect_string, const char *file);
295 
296 #ifdef __cplusplus
297 }
298 #endif
struct cf_parser cfp
Definition: effect-parser.h:274
Definition: effect-parser.h:43
char * name
Definition: effect-parser.h:177
char * mapping
Definition: effect-parser.h:223
bool written
Definition: effect-parser.h:230
bool is_const
Definition: effect-parser.h:74
bool is_uniform
Definition: effect-parser.h:74
EXPORT int astrcmp_n(const char *str1, const char *str2, size_t n)
struct dstr contents
Definition: effect-parser.h:224
Definition: darray.h:41
Definition: effect-parser.h:69
char * name
Definition: effect-parser.h:70
Definition: effect-parser.h:222
Definition: effect-parser.h:42
Definition: cf-lexer.h:47
DARRAY(char *) states
Definition: effect-parser.h:260
char * name
Definition: effect-parser.h:147
unsigned char uint8_t
Definition: vc_stdint.h:27
DARRAY(struct ep_param) params
bool ep_parse(struct effect_parser *ep, gs_effect_t *effect, const char *effect_string, const char *file)
DARRAY(struct ep_var) param_vars
bool written
Definition: effect-parser.h:151
char * name
Definition: effect-parser.h:50
Definition: effect-parser.h:49
char * name
Definition: effect-parser.h:115
char * type
Definition: effect-parser.h:50
Definition: effect.h:150
bool is_property
Definition: effect-parser.h:74
Definition: effect.h:98
char * type
Definition: effect-parser.h:70
Definition: effect-parser.h:146
Definition: effect-parser.h:198
DARRAY(struct ep_pass) passes
int array_count
Definition: effect-parser.h:75
#define da_free(v)
Definition: darray.h:458
struct gs_effect_pass * cur_pass
Definition: effect-parser.h:272
struct gs_effect_pass * pass
Definition: effect-parser.h:180
enum gs_shader_param_type type
Definition: effect.h:54
void ep_free(struct effect_parser *ep)
Definition: effect-parser.h:46
void ep_param_writevar(struct dstr *dst, struct darray *use_params)
Definition: cf-lexer.h:85
Definition: dstr.h:36
bool written
Definition: effect-parser.h:74
char * ret_type
Definition: effect-parser.h:223
Definition: effect-parser.h:114
enum ep_var_type var_type
Definition: effect-parser.h:51
ep_var_type
Definition: effect-parser.h:41
Definition: effect-parser.h:44
struct gs_effect_param * param
Definition: effect-parser.h:73
DARRAY(uint8_t) default_val
Definition: effect-parser.h:45
Definition: cf-parser.h:40
Definition: effect.h:50
char * mapping
Definition: effect-parser.h:50
DARRAY(struct cf_token) vertex_program
#define da_init(v)
Definition: darray.h:456
char * name
Definition: effect.h:51
int writeorder
Definition: effect-parser.h:75
DARRAY(struct ep_var) vars
char * name
Definition: effect-parser.h:223
EXPORT void bfree(void *ptr)
Definition: effect-parser.h:176
bool written
Definition: effect-parser.h:117
bool is_texture
Definition: effect-parser.h:74
char * name
Definition: effect-parser.h:199
gs_effect_t * effect
Definition: effect-parser.h:261