00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00027 #include "renderstack/rs_gl.h"
00028 #include "renderstack/rs_log.h"
00029 #include "renderstack/uniform_bindings.hpp"
00030 #include "renderstack/program.hpp"
00031 #include "renderstack/texture.hpp"
00032
00033 namespace renderstack {
00034
00035 bool uniform_bindings::add(
00036 enum uniform_type type,
00037 int gl_slot,
00038 int count,
00039 void *value
00040 )
00041 {
00042 uniform_binding u(type, gl_slot, count, value);
00043
00044 m_bindings.push_back(u);
00045
00046 return true;
00047 }
00048
00049 void uniform_bindings::apply()
00050 {
00051 for(
00052 std::vector<uniform_binding>::const_iterator i = m_bindings.begin();
00053 i != m_bindings.end();
00054 ++i
00055 )
00056 {
00057 uniform_binding const& binding = *i;
00058
00059 switch(binding.type)
00060 {
00061 case uniform_type_matrix_4:
00062 {
00063 ::rs_gl_uniform_matrix_4fv(
00064 binding.gl_slot,
00065 binding.count,
00066 GL_FALSE,
00067 reinterpret_cast<::GLfloat const *>(binding.value)
00068 );
00069 break;
00070 }
00071 case uniform_type_float:
00072 {
00073 ::rs_gl_uniform_1fv(
00074 binding.gl_slot,
00075 binding.count,
00076 reinterpret_cast<::GLfloat const *>(binding.value)
00077 );
00078 break;
00079 }
00080 case uniform_type_vec2:
00081 {
00082 ::rs_gl_uniform_2fv(
00083 binding.gl_slot,
00084 binding.count,
00085 reinterpret_cast<::GLfloat const *>(binding.value)
00086 );
00087 break;
00088 }
00089 case uniform_type_vec3:
00090 {
00091 ::rs_gl_uniform_3fv(
00092 binding.gl_slot,
00093 binding.count,
00094 reinterpret_cast<::GLfloat const *>(binding.value)
00095 );
00096 break;
00097 }
00098 case uniform_type_vec4:
00099 {
00100 ::rs_gl_uniform_4fv(
00101 binding.gl_slot,
00102 binding.count,
00103 reinterpret_cast<::GLfloat const *>(binding.value)
00104 );
00105 break;
00106 }
00107 case uniform_type_int:
00108 {
00109 ::rs_gl_uniform_1iv(
00110 binding.gl_slot,
00111 binding.count,
00112 reinterpret_cast<::GLint const *>(binding.value)
00113 );
00114 break;
00115 }
00116 case uniform_type_uint:
00117 {
00118 ::rs_gl_uniform_1uiv(
00119 binding.gl_slot,
00120 binding.count,
00121 reinterpret_cast<::GLuint const *>(binding.value)
00122 );
00123 break;
00124 }
00125 case uniform_type_texture_2d:
00126 {
00127 texture_binding *t = reinterpret_cast<texture_binding*>(binding.value);
00128 int texture_index = t->index;
00129 class texture *texture = t->texture;
00130
00131 ::rs_gl_uniform_1i (binding.gl_slot, texture_index);
00132 ::rs_gl_active_texture (GL_TEXTURE0 + texture_index);
00133 ::rs_gl_bind_texture (texture->gl_bind_target(), texture->gl_texture_object());
00134 ::rs_gl_tex_parameter_i(texture->gl_bind_target(), GL_TEXTURE_WRAP_S, texture->gl_wrap());
00135 ::rs_gl_tex_parameter_i(texture->gl_bind_target(), GL_TEXTURE_WRAP_T, texture->gl_wrap());
00136 ::rs_gl_tex_parameter_i(texture->gl_bind_target(), GL_TEXTURE_MAG_FILTER, texture->gl_magnification_filter());
00137 ::rs_gl_tex_parameter_i(texture->gl_bind_target(), GL_TEXTURE_MIN_FILTER, texture->gl_minification_filter());
00138 break;
00139 }
00140 case uniform_type_texture_cube:
00141 {
00142 break;
00143 }
00144
00145 default:
00146 {
00147 throw false;
00148 }
00149
00150 }
00151 }
00152 }
00153
00154
00155
00156 std::string const &uniform_mappings::name(
00157 enum logical_uniform u
00158 ) const
00159 {
00160 std::map<enum logical_uniform, std::string>::const_iterator i = m_uniform_to_string.find(u);
00161 if(i != m_uniform_to_string.end())
00162 {
00163 return (*i).second;
00164 }
00165 else
00166 {
00167 throw std::exception();
00168 }
00169 }
00170
00171 enum logical_uniform uniform_mappings::logical_uniform(std::string const &name) const
00172 {
00173 std::map<std::string, enum logical_uniform>::const_iterator i = m_string_to_uniform.find(name);
00174 if(i != m_string_to_uniform.end())
00175 {
00176 return (*i).second;
00177 }
00178 else
00179 {
00180 return logical_uniform_generic_uniform;
00181 }
00182 }
00183
00184 void uniform_mappings::bind_uniforms(
00185 class uniform_bindings &bindings,
00186 class program &program,
00187 class uniform_source &source
00188 )
00189 {
00190 for(
00191 std::map<
00192 std::string,
00193 enum logical_uniform
00194 >::const_iterator i = m_string_to_uniform.begin();
00195 i != m_string_to_uniform.end();
00196 ++i
00197 )
00198 {
00199 std::string const &name = (*i).first;
00200 enum logical_uniform lu = (*i).second;
00201
00202 if(program.has_uniform(name) == true)
00203 {
00204 int gl_slot = program.uniform(name).gl_slot();
00205 if(gl_slot < 0)
00206 {
00207 continue;
00208 }
00209 if(source.connect(bindings, lu, gl_slot) == true)
00210 {
00211 #if 0
00212 ::rs_log("bound %s slot %d\n", name.c_str(), gl_slot);
00213 #endif
00214 }
00215 }
00216
00217 }
00218 }
00219
00220
00221 void uniform_mappings::bind_uniforms(
00222 class uniform_bindings &bindings,
00223 class program &program,
00224 class uniform_parameter_source &source
00225 )
00226 {
00227 for(
00228 std::vector<std::tr1::shared_ptr<uniform_mapping> >::const_iterator i = m_parameter_mappings.begin();
00229 i != m_parameter_mappings.end();
00230 ++i
00231 )
00232 {
00233 std::tr1::shared_ptr<uniform_mapping> mapping = *i;
00234 void *value_source = source.parameter(mapping->parameter_name);
00235
00236 if(
00237 (program.has_uniform(mapping->shader_name) == true) &&
00238 (value_source != NULL)
00239 )
00240 {
00241 int gl_slot = program.uniform(mapping->shader_name).gl_slot();
00242 if(gl_slot < 0)
00243 {
00244 continue;
00245 }
00246 bindings.add(
00247 mapping->type,
00248 gl_slot,
00249 mapping->count,
00250 value_source
00251 );
00252 #if 0
00253 ::rs_log("source bound %s slot %d count %d\n", mapping->shader_name.c_str(), gl_slot, mapping->count);
00254 #endif
00255 }
00256
00257 }
00258 }
00259
00260
00261 void *uniform_parameter_source::parameter(std::string const &name)
00262 {
00263 std::map<std::string, void*>::const_iterator i = m_parameters.find(name);
00264 if(i != m_parameters.end())
00265 {
00266 return (*i).second;
00267 }
00268 return NULL;
00269 }
00270
00271 }
00272