{
try
{
- if(tied_program)
- tied_program->get_uniform_info(name);
- else if(name[name.size()-1]==']')
- throw invalid_argument("ProgramData::uniform");
+ if(!validate_name(name))
+ {
+ delete uni;
+ return;
+ }
}
- catch(const exception &e)
+ catch(...)
{
delete uni;
-#ifdef DEBUG
- IO::print(IO::cerr, "Error while setting uniform %s: %s: %s\n", name, Debug::demangle(typeid(e).name()), e.what());
- return;
-#else
throw;
-#endif
}
int i = find_uniform_index(name);
- if(i>=0)
- {
- uniforms[i].replace_value(uni);
- dirty |= 1<<i;
+ if(i<0)
+ return add_uniform(name, uni);
+
+ uniforms[i].replace_value(uni);
+ dirty |= 1<<i;
+}
+
+template<typename T, typename V>
+void ProgramData::uniform(const string &name, V value)
+{
+ if(!validate_name(name))
return;
+
+ int i = find_uniform_index(name);
+ if(i<0)
+ return add_uniform(name, new T(value));
+
+ if(T *uni = dynamic_cast<T *>(uniforms[i].value))
+ uni->set(value);
+ else
+ uniforms[i].replace_value(new T(value));
+
+ dirty |= 1<<i;
+}
+
+template<typename T, typename V>
+void ProgramData::uniform_array(const string &name, unsigned n, V value)
+{
+ if(!validate_name(name))
+ return;
+
+ int i = find_uniform_index(name);
+ if(i<0)
+ return add_uniform(name, new UniformArray<T>(n, value));
+
+ UniformArray<T> *uni = dynamic_cast<UniformArray<T> *>(uniforms[i].value);
+ if(uni && n==uni->size())
+ uni->set(value);
+ else
+ uniforms[i].replace_value(new UniformArray<T>(n, value));
+
+ dirty |= 1<<i;
+}
+
+bool ProgramData::validate_name(const string &name) const
+{
+#ifdef DEBUG
+ try
+#endif
+ {
+ if(tied_program)
+ tied_program->get_uniform_info(name);
+ else if(name[name.size()-1]==']')
+ throw invalid_argument("ProgramData::uniform");
+ return true;
}
+#ifdef DEBUG
+ catch(const exception &e)
+ {
+ IO::print(IO::cerr, "Error while setting uniform %s: %s: %s\n", name, Debug::demangle(typeid(e).name()), e.what());
+ return false;
+ }
+#endif
+}
+void ProgramData::add_uniform(const string &name, Uniform *uni)
+{
if(uniforms.size()>=MASK_BITS)
{
delete uni;
void ProgramData::uniform(const string &name, int v)
{
- uniform(name, new Uniform1i(v));
+ uniform<Uniform1i>(name, v);
}
void ProgramData::uniform(const string &name, float v)
{
- uniform(name, new Uniform1f(v));
+ uniform<Uniform1f>(name, v);
}
void ProgramData::uniform(const string &name, int v0, int v1)
void ProgramData::uniform2(const string &name, const int *v)
{
- uniform(name, new Uniform2i(v));
+ uniform<Uniform2i>(name, v);
}
void ProgramData::uniform2(const string &name, const float *v)
{
- uniform(name, new Uniform2f(v));
+ uniform<Uniform2f>(name, v);
}
void ProgramData::uniform(const string &name, int v0, int v1, int v2)
void ProgramData::uniform3(const string &name, const int *v)
{
- uniform(name, new Uniform3i(v));
+ uniform<Uniform3i>(name, v);
}
void ProgramData::uniform3(const string &name, const float *v)
{
- uniform(name, new Uniform3f(v));
+ uniform<Uniform3f>(name, v);
}
void ProgramData::uniform(const string &name, int v0, int v1, int v2, int v3)
void ProgramData::uniform4(const string &name, const int *v)
{
- uniform(name, new Uniform4i(v));
+ uniform<Uniform4i>(name, v);
}
void ProgramData::uniform4(const string &name, const float *v)
{
- uniform(name, new Uniform4f(v));
+ uniform<Uniform4f>(name, v);
}
void ProgramData::uniform(const string &name, const LinAl::Matrix<float, 2, 2> &m)
void ProgramData::uniform_matrix2(const string &name, const float *v)
{
- uniform(name, new UniformMatrix2x2f(v));
+ uniform<UniformMatrix2x2f>(name, v);
}
void ProgramData::uniform(const string &name, const LinAl::Matrix<float, 2, 3> &m)
void ProgramData::uniform_matrix3x2(const string &name, const float *v)
{
- uniform(name, new UniformMatrix3x2f(v));
+ uniform<UniformMatrix3x2f>(name, v);
}
void ProgramData::uniform(const string &name, const LinAl::Matrix<float, 2, 4> &m)
void ProgramData::uniform_matrix4x2(const string &name, const float *v)
{
- uniform(name, new UniformMatrix4x2f(v));
+ uniform<UniformMatrix4x2f>(name, v);
}
void ProgramData::uniform(const string &name, const LinAl::Matrix<float, 3, 2> &m)
void ProgramData::uniform_matrix2x3(const string &name, const float *v)
{
- uniform(name, new UniformMatrix2x3f(v));
+ uniform<UniformMatrix2x3f>(name, v);
}
void ProgramData::uniform(const string &name, const LinAl::Matrix<float, 3, 3> &m)
void ProgramData::uniform_matrix3(const string &name, const float *v)
{
- uniform(name, new UniformMatrix3x3f(v));
+ uniform<UniformMatrix3x3f>(name, v);
}
void ProgramData::uniform(const string &name, const LinAl::Matrix<float, 3, 4> &m)
void ProgramData::uniform_matrix4x3(const string &name, const float *v)
{
- uniform(name, new UniformMatrix4x3f(v));
+ uniform<UniformMatrix4x3f>(name, v);
}
void ProgramData::uniform(const string &name, const LinAl::Matrix<float, 4, 2> &m)
void ProgramData::uniform_matrix2x4(const string &name, const float *v)
{
- uniform(name, new UniformMatrix2x4f(v));
+ uniform<UniformMatrix2x4f>(name, v);
}
void ProgramData::uniform(const string &name, const LinAl::Matrix<float, 4, 3> &m)
void ProgramData::uniform_matrix3x4(const string &name, const float *v)
{
- uniform(name, new UniformMatrix3x4f(v));
+ uniform<UniformMatrix3x4f>(name, v);
}
void ProgramData::uniform(const string &name, const Matrix &m)
void ProgramData::uniform_matrix4(const string &name, const float *v)
{
- uniform(name, new UniformMatrix4x4f(v));
+ uniform<UniformMatrix4x4f>(name, v);
}
void ProgramData::uniform1_array(const string &name, unsigned n, const int *v)
{
- uniform(name, new UniformArray<Uniform1i>(n, v));
+ uniform_array<Uniform1i>(name, n, v);
}
void ProgramData::uniform1_array(const string &name, unsigned n, const float *v)
{
- uniform(name, new UniformArray<Uniform1f>(n, v));
+ uniform_array<Uniform1f>(name, n, v);
}
void ProgramData::uniform2_array(const string &name, unsigned n, const int *v)
{
- uniform(name, new UniformArray<Uniform2i>(n, v));
+ uniform_array<Uniform2i>(name, n, v);
}
void ProgramData::uniform2_array(const string &name, unsigned n, const float *v)
{
- uniform(name, new UniformArray<Uniform2f>(n, v));
+ uniform_array<Uniform2f>(name, n, v);
}
void ProgramData::uniform3_array(const string &name, unsigned n, const int *v)
{
- uniform(name, new UniformArray<Uniform3i>(n, v));
+ uniform_array<Uniform3i>(name, n, v);
}
void ProgramData::uniform3_array(const string &name, unsigned n, const float *v)
{
- uniform(name, new UniformArray<Uniform3f>(n, v));
+ uniform_array<Uniform3f>(name, n, v);
}
void ProgramData::uniform4_array(const string &name, unsigned n, const int *v)
{
- uniform(name, new UniformArray<Uniform4i>(n, v));
+ uniform_array<Uniform4i>(name, n, v);
}
void ProgramData::uniform4_array(const string &name, unsigned n, const float *v)
{
- uniform(name, new UniformArray<Uniform4f>(n, v));
+ uniform_array<Uniform4f>(name, n, v);
}
void ProgramData::uniform_matrix2_array(const string &name, unsigned n, const float *v)
{
- uniform(name, new UniformArray<UniformMatrix2x2f>(n, v));
+ uniform_array<UniformMatrix2x2f>(name, n, v);
}
void ProgramData::uniform_matrix3x2_array(const string &name, unsigned n, const float *v)
{
- uniform(name, new UniformArray<UniformMatrix3x2f>(n, v));
+ uniform_array<UniformMatrix3x2f>(name, n, v);
}
void ProgramData::uniform_matrix4x2_array(const string &name, unsigned n, const float *v)
{
- uniform(name, new UniformArray<UniformMatrix4x2f>(n, v));
+ uniform_array<UniformMatrix4x2f>(name, n, v);
}
void ProgramData::uniform_matrix2x3_array(const string &name, unsigned n, const float *v)
{
- uniform(name, new UniformArray<UniformMatrix2x3f>(n, v));
+ uniform_array<UniformMatrix2x3f>(name, n, v);
}
void ProgramData::uniform_matrix3_array(const string &name, unsigned n, const float *v)
{
- uniform(name, new UniformArray<UniformMatrix3x3f>(n, v));
+ uniform_array<UniformMatrix3x3f>(name, n, v);
}
void ProgramData::uniform_matrix4x3_array(const string &name, unsigned n, const float *v)
{
- uniform(name, new UniformArray<UniformMatrix4x3f>(n, v));
+ uniform_array<UniformMatrix4x3f>(name, n, v);
}
void ProgramData::uniform_matrix2x4_array(const string &name, unsigned n, const float *v)
{
- uniform(name, new UniformArray<UniformMatrix2x4f>(n, v));
+ uniform_array<UniformMatrix2x4f>(name, n, v);
}
void ProgramData::uniform_matrix3x4_array(const string &name, unsigned n, const float *v)
{
- uniform(name, new UniformArray<UniformMatrix3x4f>(n, v));
+ uniform_array<UniformMatrix3x4f>(name, n, v);
}
void ProgramData::uniform_matrix4_array(const string &name, unsigned n, const float *v)
{
- uniform(name, new UniformArray<UniformMatrix4x4f>(n, v));
+ uniform_array<UniformMatrix4x4f>(name, n, v);
}
void ProgramData::remove_uniform(const string &name)