]> git.tdb.fi Git - libs/gl.git/blob - source/uniform.h
f8a59f2a441e5fd2b3ec2ba55cdd604b50ab0477
[libs/gl.git] / source / uniform.h
1 #ifndef MSP_GL_UNIFORM_H_
2 #define MSP_GL_UNIFORM_H_
3
4 #include <algorithm>
5 #include "program.h"
6
7 namespace Msp {
8 namespace GL {
9
10 class Uniform
11 {
12 protected:
13         Uniform() { }
14 private:
15         Uniform(const Uniform &);
16         Uniform &operator=(const Uniform &);
17 public:
18         virtual ~Uniform() { }
19
20         virtual void apply(int) const = 0;
21         virtual void store(const Program::UniformInfo &, void *) const = 0;
22         virtual Uniform *clone() const = 0;
23 };
24
25
26 template<typename T>
27 class UniformScalar: public Uniform
28 {
29 public:
30         typedef T BaseType;
31         typedef T Type;
32
33 private:
34         Type value;
35
36 public:
37         UniformScalar(Type v): value(v) { }
38
39         virtual void apply(int index) const
40         { apply(index, 1, &value); }
41
42         static void apply(int, unsigned, const T *);
43
44         virtual void store(const Program::UniformInfo &info, void *buffer) const
45         { store(info, buffer, &value); }
46
47         static void store(const Program::UniformInfo &, void *buffer, const T *value)
48         { *reinterpret_cast<T *>(buffer) = *value; }
49
50         virtual UniformScalar *clone() const
51         { return new UniformScalar(value); }
52 };
53
54 typedef UniformScalar<int> Uniform1i;
55 typedef UniformScalar<float> Uniform1f;
56
57
58 template<typename T, unsigned vecsize>
59 class UniformVector: public Uniform
60 {
61 public:
62         typedef T BaseType;
63         typedef T Type[vecsize];
64
65 private:
66         Type value;
67
68 public:
69         UniformVector(const T *vp)
70         { std::copy(vp, vp+vecsize, value); }
71
72         virtual void apply(int index) const
73         { apply(index, 1, value); }
74
75         static void apply(int index, unsigned size, const T *value);
76
77         virtual void store(const Program::UniformInfo &info, void *buffer) const
78         { store(info, buffer, value); }
79
80         static void store(const Program::UniformInfo &, void *buffer, const T *value)
81         { std::copy(value, value+vecsize, reinterpret_cast<T *>(buffer)); }
82
83         virtual UniformVector *clone() const
84         { return new UniformVector(value); }
85 };
86
87 typedef UniformVector<float, 2> Uniform2f;
88 typedef UniformVector<float, 3> Uniform3f;
89 typedef UniformVector<float, 4> Uniform4f;
90
91
92 template<typename T, unsigned rows, unsigned cols>
93 class UniformMatrix: public Uniform
94 {
95 public:
96         typedef T BaseType;
97         typedef T Type[rows*cols];
98
99 private:
100         Type value;
101
102 public:
103         UniformMatrix(const T *vp)
104         { std::copy(vp, vp+rows*cols, value); }
105
106         virtual void apply(int index) const
107         { apply(index, 1, value); }
108
109         static void apply(int index, unsigned size, const T *value);
110
111         virtual void store(const Program::UniformInfo &info, void *buffer) const
112         { store(info, buffer, value); }
113
114         static void store(const Program::UniformInfo &info, void *buffer, const T *value)
115         {
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);
118         }
119
120         virtual UniformMatrix *clone() const
121         { return new UniformMatrix(value); }
122 };
123
124 typedef UniformMatrix<float, 4, 4> UniformMatrix4x4f;
125
126
127 template<typename T>
128 class UniformArray: public Uniform
129 {
130 private:
131         typedef typename T::BaseType BaseType;
132         enum { elemsize = sizeof(typename T::Type)/sizeof(typename T::BaseType) };
133
134         BaseType *values;
135         unsigned size;
136
137 public:
138         UniformArray(unsigned n, const BaseType *vp):
139                 size(n)
140         {
141                 values = new BaseType[elemsize*size];
142                 std::copy(vp, vp+elemsize*size, values);
143         }
144
145         virtual void apply(int index) const
146         { T::apply(index, size, values); }
147
148         virtual void store(const Program::UniformInfo &info, void *buffer) const
149         {
150                 for(unsigned i=0; i<size; ++i)
151                         T::store(info, reinterpret_cast<char *>(buffer)+i*info.array_stride, values+i*elemsize);
152         }
153
154         virtual UniformArray *clone() const
155         { return new UniformArray(size, values); }
156 };
157
158 } // namespace GL
159 } // namespace Msp
160
161 #endif