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<float, 2> Uniform2f;
88 typedef UniformVector<float, 3> Uniform3f;
89 typedef UniformVector<float, 4> Uniform4f;
92 template<typename T, unsigned rows, unsigned cols>
93 class UniformMatrix: public Uniform
97 typedef T Type[rows*cols];
103 UniformMatrix(const T *vp)
104 { std::copy(vp, vp+rows*cols, value); }
106 virtual void apply(int index) const
107 { apply(index, 1, value); }
109 static void apply(int index, unsigned size, const T *value);
111 virtual void store(const Program::UniformInfo &info, void *buffer) const
112 { store(info, buffer, value); }
114 static void store(const Program::UniformInfo &info, void *buffer, const T *value)
116 for(unsigned i=0; i<cols; ++i)
117 UniformVector<T, rows>::store(info, reinterpret_cast<char *>(buffer)+i*info.matrix_stride, value+i*rows);
120 virtual UniformMatrix *clone() const
121 { return new UniformMatrix(value); }
124 typedef UniformMatrix<float, 2, 2> UniformMatrix2x2f;
125 typedef UniformMatrix<float, 3, 3> UniformMatrix3x3f;
126 typedef UniformMatrix<float, 4, 4> UniformMatrix4x4f;
130 class UniformArray: public Uniform
133 typedef typename T::BaseType BaseType;
134 enum { elemsize = sizeof(typename T::Type)/sizeof(typename T::BaseType) };
140 UniformArray(unsigned n, const BaseType *vp):
143 values = new BaseType[elemsize*size];
144 std::copy(vp, vp+elemsize*size, values);
152 virtual void apply(int index) const
153 { T::apply(index, size, values); }
155 virtual void store(const Program::UniformInfo &info, void *buffer) const
157 for(unsigned i=0; i<size; ++i)
158 T::store(info, reinterpret_cast<char *>(buffer)+i*info.array_stride, values+i*elemsize);
161 virtual UniformArray *clone() const
162 { return new UniformArray(size, values); }