1 #ifndef MSP_GL_UNIFORM_H_
2 #define MSP_GL_UNIFORM_H_
15 Uniform(const Uniform &);
16 Uniform &operator=(const Uniform &);
18 virtual ~Uniform() { }
20 virtual void apply(int) const = 0;
21 virtual void store(const Program::UniformInfo &, void *) const = 0;
22 virtual Uniform *clone() const = 0;
27 class UniformScalar: public Uniform
37 UniformScalar(Type v): value(v) { }
39 virtual void apply(int index) const
40 { apply(index, 1, &value); }
42 static void apply(int, unsigned, const T *);
44 virtual void store(const Program::UniformInfo &info, void *buffer) const
45 { store(info, buffer, &value); }
47 static void store(const Program::UniformInfo &, void *buffer, const T *value)
48 { *reinterpret_cast<T *>(buffer) = *value; }
50 virtual UniformScalar *clone() const
51 { return new UniformScalar(value); }
54 typedef UniformScalar<int> Uniform1i;
55 typedef UniformScalar<float> Uniform1f;
58 template<typename T, unsigned vecsize>
59 class UniformVector: public Uniform
63 typedef T Type[vecsize];
69 UniformVector(const T *vp)
70 { std::copy(vp, vp+vecsize, value); }
72 virtual void apply(int index) const
73 { apply(index, 1, value); }
75 static void apply(int index, unsigned size, const T *value);
77 virtual void store(const Program::UniformInfo &info, void *buffer) const
78 { store(info, buffer, value); }
80 static void store(const Program::UniformInfo &, void *buffer, const T *value)
81 { std::copy(value, value+vecsize, reinterpret_cast<T *>(buffer)); }
83 virtual UniformVector *clone() const
84 { return new UniformVector(value); }
87 typedef UniformVector<int, 2> Uniform2i;
88 typedef UniformVector<float, 2> Uniform2f;
89 typedef UniformVector<int, 3> Uniform3i;
90 typedef UniformVector<float, 3> Uniform3f;
91 typedef UniformVector<int, 4> Uniform4i;
92 typedef UniformVector<float, 4> Uniform4f;
95 template<typename T, unsigned rows, unsigned cols>
96 class UniformMatrix: public Uniform
100 typedef T Type[rows*cols];
106 UniformMatrix(const T *vp)
107 { std::copy(vp, vp+rows*cols, value); }
109 virtual void apply(int index) const
110 { apply(index, 1, value); }
112 static void apply(int index, unsigned size, const T *value);
114 virtual void store(const Program::UniformInfo &info, void *buffer) const
115 { store(info, buffer, value); }
117 static void store(const Program::UniformInfo &info, void *buffer, const T *value)
119 for(unsigned i=0; i<cols; ++i)
120 UniformVector<T, rows>::store(info, reinterpret_cast<char *>(buffer)+i*info.matrix_stride, value+i*rows);
123 virtual UniformMatrix *clone() const
124 { return new UniformMatrix(value); }
127 typedef UniformMatrix<float, 2, 2> UniformMatrix2x2f;
128 typedef UniformMatrix<float, 3, 3> UniformMatrix3x3f;
129 typedef UniformMatrix<float, 4, 4> UniformMatrix4x4f;
133 class UniformArray: public Uniform
136 typedef typename T::BaseType BaseType;
137 enum { elemsize = sizeof(typename T::Type)/sizeof(typename T::BaseType) };
143 UniformArray(unsigned n, const BaseType *vp):
146 values = new BaseType[elemsize*size];
147 std::copy(vp, vp+elemsize*size, values);
155 virtual void apply(int index) const
156 { T::apply(index, size, values); }
158 virtual void store(const Program::UniformInfo &info, void *buffer) const
160 for(unsigned i=0; i<size; ++i)
161 T::store(info, reinterpret_cast<char *>(buffer)+i*info.array_stride, values+i*elemsize);
164 virtual UniformArray *clone() const
165 { return new UniformArray(size, values); }