00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00027 #ifndef UNIFORM_BINDINGS_HPP
00028 #define UNIFORM_BINDINGS_HPP
00029
00030 #include "renderstack/rs.h"
00031 #include "renderstack/vec2.hpp"
00032 #include "renderstack/vec3.hpp"
00033 #include "renderstack/vec4.hpp"
00034 #include "renderstack/matrix.hpp"
00035 #include "renderstack/scoped_array.hpp"
00036 #include <memory>
00037 #include <vector>
00038 #include <string>
00039 #include <map>
00040
00041 namespace renderstack {
00042
00043 enum uniform_type
00044 {
00045 uniform_type_matrix_4,
00046 uniform_type_float,
00047 uniform_type_vec2,
00048 uniform_type_vec3,
00049 uniform_type_vec4,
00050 uniform_type_int,
00051 uniform_type_uint,
00052 uniform_type_texture_2d,
00053 uniform_type_texture_cube
00054 };
00055
00056 enum logical_uniform
00057 {
00058 logical_uniform_local_to_world,
00059 logical_uniform_world_to_local,
00060 logical_uniform_local_to_clip,
00061 logical_uniform_clip_to_local,
00062 logical_uniform_world_to_clip,
00063 logical_uniform_clip_to_world,
00064 logical_uniform_view_position_in_world,
00065 logical_uniform_generic_uniform
00066 };
00067
00068
00069 struct uniform_binding;
00070 struct uniform_mapping;
00071 class uniform_collection;
00072 class uniform_mappings;
00073 class uniform_source;
00074 struct float_parameter;
00075 struct vec2_parameter;
00076 struct vec3_parameter;
00077 struct vec4_parameter;
00078 struct matrix_parameter;
00079 class texture;
00080 class uniform_parameter_source;
00081
00082 struct texture_binding
00083 {
00084 int index;
00085 class texture *texture;
00086 };
00087
00089 struct uniform_binding
00090 {
00091 uniform_binding(
00092 enum uniform_type type_,
00093 int gl_slot_,
00094 int count_,
00095 void * value_
00096 )
00097 : type(type_)
00098 , gl_slot(gl_slot_)
00099 , count(count_)
00100 , value(value_)
00101 {
00102 }
00103 enum uniform_type type;
00104 int gl_slot;
00105 int count;
00106 void * value;
00107 };
00108
00110 class uniform_bindings
00111 {
00112 public:
00113 void clear()
00114 {
00115 m_bindings.clear();
00116 }
00117
00118 bool add(
00119 enum uniform_type type,
00120 int gl_slot,
00121 int count,
00122 void *value
00123 );
00124
00125 void apply();
00126
00127 private:
00128 std::vector<uniform_binding> m_bindings;
00129 };
00130
00132 struct uniform_mapping
00133 {
00134 uniform_mapping(
00135 std::string shader_name_,
00136 std::string parameter_name_,
00137 enum uniform_type type_,
00138 int count_
00139 )
00140 : shader_name (shader_name_)
00141 , parameter_name (parameter_name_)
00142 , type (type_)
00143 , count (count_)
00144 {
00145 }
00146
00147 std::string shader_name;
00148 std::string parameter_name;
00149 enum uniform_type type;
00150 int count;
00151 };
00152
00154 class uniform_mappings
00155 {
00156 public:
00157 void insert(std::string const &name_in_shader, enum logical_uniform u)
00158 {
00159 m_string_to_uniform.insert(std::make_pair(name_in_shader, u));
00160 m_uniform_to_string.insert(std::make_pair(u, name_in_shader));
00161 }
00162 void insert(
00163 std::string shader_name,
00164 std::string parameter_name,
00165 enum uniform_type type,
00166 int count
00167 )
00168 {
00169 std::tr1::shared_ptr<uniform_mapping> m(
00170 new uniform_mapping(shader_name, parameter_name, type, count)
00171 );
00172 m_parameter_mappings.push_back(m);
00173 }
00174 std::string const &name (enum logical_uniform u) const;
00175 enum logical_uniform logical_uniform (std::string const &name_in_shader) const;
00176
00177 void bind_uniforms(
00178 class uniform_bindings &bindings,
00179 class program &program,
00180 class uniform_source &source
00181 );
00182 void bind_uniforms(
00183 class uniform_bindings &bindings,
00184 class program &program,
00185 class uniform_parameter_source &source
00186 );
00187
00188 private:
00189 std::map<std::string, enum logical_uniform> m_string_to_uniform;
00190 std::map<enum logical_uniform, std::string> m_uniform_to_string;
00191 std::vector<std::tr1::shared_ptr<uniform_mapping> > m_parameter_mappings;
00192 };
00193
00195 class uniform_source
00196 {
00197 public:
00198 virtual bool connect(uniform_bindings &bindings, enum logical_uniform type, int slot) = 0;
00199 };
00200
00201 struct uint_parameter
00202 {
00203 uint_parameter(std::string const &name, int size = 1)
00204 : value(new unsigned int[size])
00205 {
00206 this->name = name;
00207 }
00208 std::string name;
00209 scoped_array<unsigned int> value;
00210 };
00211 struct float_parameter
00212 {
00213 float_parameter(std::string const &name, int size = 1)
00214 : value(new float[size])
00215 {
00216 this->name = name;
00217 }
00218 std::string name;
00219 scoped_array<float> value;
00220 };
00221 struct vec2_parameter
00222 {
00223 vec2_parameter(std::string const &name, int size = 1)
00224 : value(new vec2[size])
00225 {
00226 this->name = name;
00227 }
00228 std::string name;
00229 scoped_array<vec2> value;
00230 };
00231
00232 struct vec3_parameter
00233 {
00234 vec3_parameter(std::string const &name, int size = 1)
00235 : value(new vec3[size])
00236 {
00237 this->name = name;
00238 }
00239 std::string name;
00240 scoped_array<vec3> value;
00241 };
00242
00243 struct vec4_parameter
00244 {
00245 vec4_parameter(std::string const &name, int size = 1)
00246 : value(new vec4[size])
00247 {
00248 this->name = name;
00249 }
00250 std::string name;
00251 scoped_array<vec4> value;
00252 };
00253
00254
00255 struct matrix_parameter
00256 {
00257 matrix_parameter(std::string const &name, int size = 1)
00258 : value(new matrix[size])
00259 {
00260 this->name = name;
00261 }
00262 std::string name;
00263 scoped_array<matrix> value;
00264 };
00265
00266 struct texture_parameter
00267 {
00268 texture_parameter(std::string const &name, int size = 1)
00269 : value(new texture_binding[size])
00270 {
00271 this->name = name;
00272 }
00273 std::string name;
00274 scoped_array<texture_binding> value;
00275 };
00276
00278 class uniform_parameter_source
00279 {
00280 public:
00281 void *parameter(std::string const &name);
00282 uint_parameter *add_uint(std::string const &name, int size = 1)
00283 {
00284 std::tr1::shared_ptr<uint_parameter> p(new uint_parameter(name, size));
00285 m_uint_parameters.push_back(p);
00286 m_parameters.insert(std::make_pair(name, &p->value[0]));
00287 return p.get();
00288 }
00289 float_parameter *add_float(std::string const &name, int size = 1)
00290 {
00291 std::tr1::shared_ptr<float_parameter> p(new float_parameter(name, size));
00292 m_float_parameters.push_back(p);
00293 m_parameters.insert(std::make_pair(name, &p->value[0]));
00294 return p.get();
00295 }
00296 vec2_parameter *add_vec2(std::string const &name, int size = 1)
00297 {
00298 std::tr1::shared_ptr<vec2_parameter> p(new vec2_parameter(name, size));
00299 m_vec2_parameters.push_back(p);
00300 m_parameters.insert(std::make_pair(name, &p->value[0].x));
00301 return p.get();
00302 }
00303 vec3_parameter *add_vec3(std::string const &name, int size = 1)
00304 {
00305 std::tr1::shared_ptr<vec3_parameter> p(new vec3_parameter(name, size));
00306 m_vec3_parameters.push_back(p);
00307 m_parameters.insert(std::make_pair(name, &p->value[0].x));
00308 return p.get();
00309 }
00310 vec4_parameter *add_vec4(std::string const &name, int size = 1)
00311 {
00312 std::tr1::shared_ptr<vec4_parameter> p(new vec4_parameter(name, size));
00313 m_vec4_parameters.push_back(p);
00314 m_parameters.insert(std::make_pair(name, &p->value[0].x));
00315 return p.get();
00316 }
00317 matrix_parameter *add_matrix(std::string const &name, int size = 1)
00318 {
00319 std::tr1::shared_ptr<matrix_parameter> p(new matrix_parameter(name, size));
00320 m_matrix_parameters.push_back(p);
00321 m_parameters.insert(std::make_pair(name, &p->value[0].m[0][0]));
00322 return p.get();
00323 }
00324
00325 texture_parameter *add_texture(std::string const &name, int size = 1)
00326 {
00327 std::tr1::shared_ptr<texture_parameter> p(new texture_parameter(name, size));
00328 m_texture_parameters.push_back(p);
00329 texture_binding *binding = &p->value[0];
00330 void *v = reinterpret_cast<void *>(binding);
00331 m_parameters.insert(std::make_pair(name, v));
00332
00333 return p.get();
00334 }
00335
00336 private:
00337 std::map<std::string, void *> m_parameters;
00338 std::vector<std::tr1::shared_ptr<uint_parameter> > m_uint_parameters;
00339 std::vector<std::tr1::shared_ptr<float_parameter> > m_float_parameters;
00340 std::vector<std::tr1::shared_ptr<vec2_parameter> > m_vec2_parameters;
00341 std::vector<std::tr1::shared_ptr<vec3_parameter> > m_vec3_parameters;
00342 std::vector<std::tr1::shared_ptr<vec4_parameter> > m_vec4_parameters;
00343 std::vector<std::tr1::shared_ptr<matrix_parameter> > m_matrix_parameters;
00344 std::vector<std::tr1::shared_ptr<texture_parameter> > m_texture_parameters;
00345 };
00346
00347 }
00348
00349 #endif
00350