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