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