FastUIDraw
unpack_source_generator.hpp
Go to the documentation of this file.
1 /*!
2  * \file unpack_source_generator.hpp
3  * \brief file unpack_source_generator.hpp
4  *
5  * Copyright 2019 by Intel.
6  *
7  * Contact: kevin.rogovin@gmail.com
8  *
9  * This Source Code Form is subject to the
10  * terms of the Mozilla Public License, v. 2.0.
11  * If a copy of the MPL was not distributed with
12  * this file, You can obtain one at
13  * http://mozilla.org/MPL/2.0/.
14  *
15  * \author Kevin Rogovin <kevin.rogovin@gmail.com>
16  *
17  */
18 
19 #ifndef FASTUIDRAW_UNPACK_SOURCE_GENERATOR_HPP
20 #define FASTUIDRAW_UNPACK_SOURCE_GENERATOR_HPP
21 
23 
24 namespace fastuidraw
25 {
26  namespace glsl
27  {
28 /*!\addtogroup GLSL
29  * @{
30  */
31  /*!
32  * \brief
33  * An unpack source generator is used to generate shader
34  * source code to unpack data from the data store buffer
35  * into a GLSL struct.
36  */
38  {
39  public:
40  /*!
41  * Enumeration specifing GLSL type for field
42  * (or subfield) of a value to unpack.
43  */
44  enum type_t
45  {
46  float_type, /*!< GLSL type is float */
47  uint_type, /*!< GLSL type is uint */
48  int_type, /*!< GLSL type is int */
49 
50  /*!
51  * indicates that the offset corresponds to
52  * padding and not any field or data.
53  */
55  };
56 
57  /*!
58  * Enumeration specifying to bit-cast with
59  * GLSL's built-in uintBitsToFloat() or not
60  */
61  enum cast_t
62  {
63  /*!
64  * Reinterpret the bits as float bits, i.e.
65  * use uintBitsToFloat() of GLSL
66  */
68 
69  /*!
70  * only type-cast the bits.
71  */
73  };
74 
75  /*!
76  * Ctor.
77  * \param type_name name of GLSL type to which to unpack
78  * data from the data store buffer
79  */
80  explicit
82 
83  /*!
84  * Ctor.
85  * \param type_names names of GLSL type to which to unpack
86  * data from the data store buffer
87  */
88  explicit
90 
91  /*!
92  * Copy ctor.
93  * \param obj value from which to copy values.
94  */
96 
98 
99  /*!
100  * Assignment operator
101  */
103  operator=(const UnpackSourceGenerator &rhs);
104 
105  /*!
106  * Swap operation
107  * \param obj object with which to swap
108  */
109  void
111 
112  /*!
113  * Set the field name that corresponds to an offset
114  * \param offset offset from the start of the packed struct
115  * in units of uint32_t
116  * \param field_name GLSL name of the field to which to unpack
117  * the single scalar value. The value must
118  * include the dot if it is a field member of
119  * a struct.
120  * \param type the GLSL type of the field
121  * \param cast how to interpret the bits of the value
122  * \param struct_idx if the ctor was given an array of c_string
123  * values, refers to the index into that
124  * array of the values.
125  */
127  set(unsigned int offset, c_string field_name,
128  type_t type, enum cast_t cast,
129  unsigned int struct_idx = 0);
130 
131  /*!
132  * Set the field name that corresponds to an offset and range of
133  * bits within the value at the named offset
134  * \param offset offset from the start of the packed struct
135  * in units of uint32_t
136  * \param bit0 first bit of field value storted at offset
137  * \param num_bits number of bits used to store value.
138  * \param field_name GLSL name of the field to which to unpack
139  * the single scalar value. The value must
140  * include the dot if it is a field member of
141  * a struct.
142  * \param type the GLSL type of the field, must be \ref uint_type
143  * or \ref int_type
144  * \param cast how to interpret the bits of the value
145  * \param struct_idx if the ctor was given an array of c_string
146  * values, refers to the index into that
147  * array of the values.
148  */
150  set(unsigned int offset, unsigned int bit0, unsigned int num_bits,
151  c_string field_name, type_t type, enum cast_t cast,
152  unsigned int struct_idx = 0);
153 
154  /*!
155  * Provided as a conveniance, equivalent to
156  * \code
157  * set(offset, field_name, float_type, reinterpret_to_float_bits, struct_idx);
158  * \endcode
159  * \param offset offset from the start of the packed struct
160  * in units of uint32_t
161  * \param field_name GLSL name of the field to which to unpack
162  * the single scalar value. The value must
163  * include the dot if it is a field member of
164  * a struct.
165  * \param struct_idx if the ctor was given an array of c_string
166  * values, refers to the index into that
167  * array of the values.
168  */
170  set_float(unsigned int offset, c_string field_name,
171  unsigned int struct_idx = 0)
172  {
173  return set(offset, field_name, float_type, reinterpret_to_float_bits, struct_idx);
174  }
175 
176  /*!
177  * Provided as a conveniance, equivalent to
178  * \code
179  * set(offset, field_name, uint_type, type_cast, struct_idx);
180  * \endcode
181  * \param offset offset from the start of the packed struct
182  * in units of uint32_t
183  * \param field_name GLSL name of the field to which to unpack
184  * the single scalar value. The value must
185  * include the dot if it is a field member of
186  * a struct.
187  * \param struct_idx if the ctor was given an array of c_string
188  * values, refers to the index into that
189  * array of the values.
190  */
192  set_uint(unsigned int offset, c_string field_name,
193  unsigned int struct_idx = 0)
194  {
195  return set(offset, field_name, uint_type, type_cast, struct_idx);
196  }
197 
198  /*!
199  * Provided as a conveniance, equivalent to
200  * \code
201  * set(offset, field_name, int_type, type_cast, struct_idx);
202  * \endcode
203  * \param offset offset from the start of the packed struct
204  * in units of uint32_t
205  * \param field_name GLSL name of the field to which to unpack
206  * the single scalar value. The value must
207  * include the dot if it is a field member of
208  * a struct.
209  * \param struct_idx if the ctor was given an array of c_string
210  * values, refers to the index into that
211  * array of the values.
212  */
214  set_int(unsigned int offset, c_string field_name,
215  unsigned int struct_idx = 0)
216  {
217  return set(offset, field_name, int_type, type_cast, struct_idx);
218  }
219 
220  /*!
221  * Stream the unpack function into a \ref ShaderSource object.
222  * For values constructed passed a single c_string, the function
223  * generated is
224  * \code
225  * void
226  * function_name(in uint location, out struct_name v)
227  * \endcode
228  * and for those values constructed passed an array of c_string
229  * values, the function generated is
230  * \code
231  * void
232  * function_name(in uint location, out struct_name0 v0, out struct_name1 v1, ...)
233  * \endcode
234  * where for both, location is the location from which to unpack
235  * data and the remaining arguments are the struct types to which
236  * to unpacked as passed in the ctor.
237  * \param str \ref ShaderSource to which to stream the unpack function
238  * \param function_name name to give the function
239  */
240  const UnpackSourceGenerator&
242  c_string function_name) const;
243 
244  /*!
245  * Stream a function with the given name that returns the
246  * number of data blocks (i.e. the number of uvec4
247  * elements) used to store the struct described by this
248  * \ref UnpackSourceGenerator.
249  * \param str \ref ShaderSource to which to stream the unpack function
250  * \param function_name name to give the function
251  */
252  const UnpackSourceGenerator&
254  c_string function_name) const;
255 
256  /*!
257  * Provided as a conveniance, equivalent to
258  * \code
259  * ShaderSource return_value;
260  * stream_unpack_function(return_value, function_name);
261  * return return_value;
262  * \endcode
263  * \param function_name to give the function
264  */
266  stream_unpack_function(c_string function_name) const
267  {
268  ShaderSource return_value;
269  stream_unpack_function(return_value, function_name);
270  return return_value;
271  }
272 
273  /*!
274  * Provided as a conveniance, equivalent to
275  * \code
276  * ShaderSource return_value;
277  * stream_unpack_size_function(return_value, function_name);
278  * return return_value;
279  * \endcode
280  * \param function_name to give the function
281  */
284  {
285  ShaderSource return_value;
286  stream_unpack_size_function(return_value, function_name);
287  return return_value;
288  }
289 
290  private:
291  void *m_d;
292  };
293 /*! @} */
294 
295  }
296 }
297 
298 #endif
ShaderSource stream_unpack_size_function(c_string function_name) const
A ShaderSource represents the source code to a GLSL shader, specifying blocks of source code and macr...
all classes and functions of FastUIDraw are in the namespace fastuidraw.
Definition: colorstop.hpp:28
UnpackSourceGenerator & set_int(unsigned int offset, c_string field_name, unsigned int struct_idx=0)
UnpackSourceGenerator & set_float(unsigned int offset, c_string field_name, unsigned int struct_idx=0)
const UnpackSourceGenerator & stream_unpack_function(ShaderSource &str, c_string function_name) const
file shader_source.hpp
An unpack source generator is used to generate shader source code to unpack data from the data store ...
UnpackSourceGenerator & operator=(const UnpackSourceGenerator &rhs)
void swap(UnpackSourceGenerator &obj)
A c_array is a wrapper over a C pointer with a size parameter to facilitate bounds checking and provi...
Definition: c_array.hpp:43
const char * c_string
Conveniant typedef for C-style strings.
Definition: util.hpp:135
const UnpackSourceGenerator & stream_unpack_size_function(ShaderSource &str, c_string function_name) const
ShaderSource stream_unpack_function(c_string function_name) const
UnpackSourceGenerator & set_uint(unsigned int offset, c_string field_name, unsigned int struct_idx=0)