X-Git-Url: http://git.tdb.fi/?p=libs%2Fgl.git;a=blobdiff_plain;f=source%2Funiform.h;h=bc78bab8c1c891ad55086397f2bbcc9250d949a4;hp=32779ced0a06cc0206a20982ee57ac4d52024335;hb=bec07999d95b76f4b47cffcc564d0cd0afc0435e;hpb=f9c15dc04462b2f1eea1d6bdd71e3ba967b1bd8c diff --git a/source/uniform.h b/source/uniform.h index 32779ced..bc78bab8 100644 --- a/source/uniform.h +++ b/source/uniform.h @@ -2,6 +2,7 @@ #define MSP_GL_UNIFORM_H_ #include +#include "program.h" namespace Msp { namespace GL { @@ -17,6 +18,7 @@ public: virtual ~Uniform() { } virtual void apply(int) const = 0; + virtual void store(const Program::UniformInfo &, void *) const = 0; virtual Uniform *clone() const = 0; }; @@ -34,7 +36,20 @@ private: public: UniformScalar(Type v): value(v) { } - virtual void apply(int index) const; + void set(Type v) { value = v; } + + Type get() const { return value; } + + virtual void apply(int index) const + { apply(index, 1, &value); } + + static void apply(int, unsigned, const T *); + + virtual void store(const Program::UniformInfo &info, void *buffer) const + { store(info, buffer, &value); } + + static void store(const Program::UniformInfo &, void *buffer, const T *value) + { *reinterpret_cast(buffer) = *value; } virtual UniformScalar *clone() const { return new UniformScalar(value); } @@ -55,17 +70,33 @@ private: Type value; public: - UniformVector(const T *vp) + UniformVector(const T *vp) { set(vp); } + + void set(const T *vp) { std::copy(vp, vp+vecsize, value); } - virtual void apply(int index) const; + BaseType get(unsigned i) const { return value[i]; } + + virtual void apply(int index) const + { apply(index, 1, value); } + + static void apply(int index, unsigned size, const T *value); + + virtual void store(const Program::UniformInfo &info, void *buffer) const + { store(info, buffer, value); } + + static void store(const Program::UniformInfo &, void *buffer, const T *value) + { std::copy(value, value+vecsize, reinterpret_cast(buffer)); } virtual UniformVector *clone() const { return new UniformVector(value); } }; +typedef UniformVector Uniform2i; typedef UniformVector Uniform2f; +typedef UniformVector Uniform3i; typedef UniformVector Uniform3f; +typedef UniformVector Uniform4i; typedef UniformVector Uniform4f; @@ -80,17 +111,82 @@ private: Type value; public: - UniformMatrix(const T *vp) + UniformMatrix(const T *vp) { set(vp); } + + void set(const T *vp) { std::copy(vp, vp+rows*cols, value); } - virtual void apply(int index) const; + virtual void apply(int index) const + { apply(index, 1, value); } + + static void apply(int index, unsigned size, const T *value); + + virtual void store(const Program::UniformInfo &info, void *buffer) const + { store(info, buffer, value); } + + static void store(const Program::UniformInfo &info, void *buffer, const T *value) + { + for(unsigned i=0; i::store(info, reinterpret_cast(buffer)+i*info.matrix_stride, value+i*rows); + } virtual UniformMatrix *clone() const { return new UniformMatrix(value); } }; +// The naming of these types follows the OpenGL convention of columns x rows +typedef UniformMatrix UniformMatrix2x2f; +typedef UniformMatrix UniformMatrix3x2f; +typedef UniformMatrix UniformMatrix4x2f; +typedef UniformMatrix UniformMatrix2x3f; +typedef UniformMatrix UniformMatrix3x3f; +typedef UniformMatrix UniformMatrix4x3f; +typedef UniformMatrix UniformMatrix2x4f; +typedef UniformMatrix UniformMatrix3x4f; typedef UniformMatrix UniformMatrix4x4f; + +template +class UniformArray: public Uniform +{ +private: + typedef typename T::BaseType BaseType; + enum { elemsize = sizeof(typename T::Type)/sizeof(typename T::BaseType) }; + + unsigned size_; + BaseType *values; + +public: + UniformArray(unsigned n, const BaseType *vp): + size_(n), + values(new BaseType[elemsize*size_]) + { + set(vp); + } + + ~UniformArray() + { + delete[] values; + } + + unsigned size() const { return size_; } + + void set(const BaseType *vp) + { std::copy(vp, vp+elemsize*size_, values); } + + virtual void apply(int index) const + { T::apply(index, size_, values); } + + virtual void store(const Program::UniformInfo &info, void *buffer) const + { + for(unsigned i=0; i(buffer)+i*info.array_stride, values+i*elemsize); + } + + virtual UniformArray *clone() const + { return new UniformArray(size_, values); } +}; + } // namespace GL } // namespace Msp