Uniform.hpp 20.4 KB
Newer Older
Michele Bosi's avatar
Michele Bosi committed
1 2 3
/**************************************************************************************/
/*                                                                                    */
/*  Visualization Library                                                             */
Michele Bosi's avatar
update  
Michele Bosi committed
4
/*  http://visualizationlibrary.org                                                   */
Michele Bosi's avatar
Michele Bosi committed
5
/*                                                                                    */
Michele Bosi's avatar
Michele Bosi committed
6
/*  Copyright (c) 2005-2016, Michele Bosi                                             */
Michele Bosi's avatar
Michele Bosi committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
/*  All rights reserved.                                                              */
/*                                                                                    */
/*  Redistribution and use in source and binary forms, with or without modification,  */
/*  are permitted provided that the following conditions are met:                     */
/*                                                                                    */
/*  - Redistributions of source code must retain the above copyright notice, this     */
/*  list of conditions and the following disclaimer.                                  */
/*                                                                                    */
/*  - Redistributions in binary form must reproduce the above copyright notice, this  */
/*  list of conditions and the following disclaimer in the documentation and/or       */
/*  other materials provided with the distribution.                                   */
/*                                                                                    */
/*  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND   */
/*  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED     */
/*  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE            */
/*  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR  */
/*  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES    */
/*  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;      */
/*  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON    */
/*  ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT           */
/*  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS     */
/*  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                      */
/*                                                                                    */
/**************************************************************************************/

#ifndef Uniform_INCLUDE_ONCE
#define Uniform_INCLUDE_ONCE

#include <vlCore/vlnamespace.hpp>
#include <vlCore/Object.hpp>
#include <vlCore/Vector4.hpp>
#include <vlCore/Matrix4.hpp>
#include <vlGraphics/OpenGL.hpp>
#include <cstring>
#include <map>
#include <vector>
#include <algorithm>

namespace vl
{
  //------------------------------------------------------------------------------
  // Uniform
  //------------------------------------------------------------------------------
  /**
  * Wraps an OpenGL Shading Language uniform to be associated to a GLSLProgram (see vl::GLSLProgram documentation).
   *
   * \sa
   * - GLSLProgram
   * - Shader
   * - Actor
   * - UniformSet
  */
  class Uniform: public Object
  {
61
    VL_INSTRUMENT_CLASS(vl::Uniform, Object)
62

Michele Bosi's avatar
Michele Bosi committed
63
    friend class GLSLProgram;
64

Michele Bosi's avatar
Michele Bosi committed
65 66 67 68
  public:

    Uniform(): mType(UT_NONE)
    {
69
      VL_DEBUG_SET_OBJECT_NAME()
Michele Bosi's avatar
Michele Bosi committed
70 71
    }

Michele Bosi's avatar
Michele Bosi committed
72
    Uniform(const char* name): mType(UT_NONE)
Michele Bosi's avatar
Michele Bosi committed
73
    {
74
      VL_DEBUG_SET_OBJECT_NAME()
Michele Bosi's avatar
Michele Bosi committed
75 76 77
      mName = name;
    }

78 79 80 81 82 83 84
    ref<Uniform> clone() const
    {
      ref<Uniform> uniform  = new Uniform;
      *uniform = *this;
      return uniform;
    }

Michele Bosi's avatar
Michele Bosi committed
85 86
    //! Returns the name of the uniform variable
    const std::string& name() const { return mName; }
Michele Bosi's avatar
Michele Bosi committed
87

Michele Bosi's avatar
Michele Bosi committed
88 89
    //! Returns the name of the uniform variable
    std::string& name() { return mName; }
Michele Bosi's avatar
Michele Bosi committed
90

Michele Bosi's avatar
Michele Bosi committed
91
    //! Sets the name of the uniform variable
92
    void setName(const char* name) { mName = name; }
Michele Bosi's avatar
Michele Bosi committed
93

94 95 96
    //! Sets the name of the uniform variable
    void setName(const std::string& name) { mName = name; }

Michele Bosi's avatar
Michele Bosi committed
97
    // generic array setters
Michele Bosi's avatar
Michele Bosi committed
98

99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
    void setUniform1i(int count, const int* value) { initData(count*1); memcpy(&mData[0], value, sizeof(mData[0]) * mData.size()); mType = UT_INT; }
    void setUniform2i(int count, const int* value) { initData(count*2); memcpy(&mData[0], value, sizeof(mData[0]) * mData.size()); mType = UT_INT_VEC2; }
    void setUniform3i(int count, const int* value) { initData(count*3); memcpy(&mData[0], value, sizeof(mData[0]) * mData.size()); mType = UT_INT_VEC3; }
    void setUniform4i(int count, const int* value) { initData(count*4); memcpy(&mData[0], value, sizeof(mData[0]) * mData.size()); mType = UT_INT_VEC4; }

    void setUniform1ui(int count, const unsigned int* value) { initData(count*1); memcpy(&mData[0], value, sizeof(mData[0]) * mData.size()); mType = UT_UNSIGNED_INT; }
    void setUniform2ui(int count, const unsigned int* value) { initData(count*2); memcpy(&mData[0], value, sizeof(mData[0]) * mData.size()); mType = UT_UNSIGNED_INT_VEC2; }
    void setUniform3ui(int count, const unsigned int* value) { initData(count*3); memcpy(&mData[0], value, sizeof(mData[0]) * mData.size()); mType = UT_UNSIGNED_INT_VEC3; }
    void setUniform4ui(int count, const unsigned int* value) { initData(count*4); memcpy(&mData[0], value, sizeof(mData[0]) * mData.size()); mType = UT_UNSIGNED_INT_VEC4; }

    void setUniform1f(int count, const float* value) { initData(count*1); memcpy(&mData[0], value, sizeof(mData[0]) * mData.size()); mType = UT_FLOAT;  }
    void setUniform2f(int count, const float* value) { initData(count*2); memcpy(&mData[0], value, sizeof(mData[0]) * mData.size()); mType = UT_FLOAT_VEC2; }
    void setUniform3f(int count, const float* value) { initData(count*3); memcpy(&mData[0], value, sizeof(mData[0]) * mData.size()); mType = UT_FLOAT_VEC3; }
    void setUniform4f(int count, const float* value) { initData(count*4); memcpy(&mData[0], value, sizeof(mData[0]) * mData.size()); mType = UT_FLOAT_VEC4; }

    void setUniform1d(int count, const double* value) { initDouble(count*1); memcpy(&mData[0], value, sizeof(mData[0]) * mData.size()); mType = UT_DOUBLE; }
    void setUniform2d(int count, const double* value) { initDouble(count*2); memcpy(&mData[0], value, sizeof(mData[0]) * mData.size()); mType = UT_DOUBLE_VEC2; }
    void setUniform3d(int count, const double* value) { initDouble(count*3); memcpy(&mData[0], value, sizeof(mData[0]) * mData.size()); mType = UT_DOUBLE_VEC3; }
    void setUniform4d(int count, const double* value) { initDouble(count*4); memcpy(&mData[0], value, sizeof(mData[0]) * mData.size()); mType = UT_DOUBLE_VEC4; }
Michele Bosi's avatar
Michele Bosi committed
118

Michele Bosi's avatar
Michele Bosi committed
119
    // generic matrix array setters
Michele Bosi's avatar
Michele Bosi committed
120

121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
    void setUniformMatrix2f(int count, const float* value) { initData(count*2*2); memcpy(&mData[0], value, sizeof(mData[0]) * mData.size()); mType = UT_FLOAT_MAT2; }
    void setUniformMatrix3f(int count, const float* value) { initData(count*3*3); memcpy(&mData[0], value, sizeof(mData[0]) * mData.size()); mType = UT_FLOAT_MAT3; }
    void setUniformMatrix4f(int count, const float* value) { initData(count*4*4); memcpy(&mData[0], value, sizeof(mData[0]) * mData.size()); mType = UT_FLOAT_MAT4; }

    void setUniformMatrix2x3f(int count, const float* value) { initData(count*2*3); memcpy(&mData[0], value, sizeof(mData[0]) * mData.size()); mType = UT_FLOAT_MAT2x3; }
    void setUniformMatrix3x2f(int count, const float* value) { initData(count*3*2); memcpy(&mData[0], value, sizeof(mData[0]) * mData.size()); mType = UT_FLOAT_MAT3x2; }
    void setUniformMatrix2x4f(int count, const float* value) { initData(count*2*4); memcpy(&mData[0], value, sizeof(mData[0]) * mData.size()); mType = UT_FLOAT_MAT2x4; }
    void setUniformMatrix4x2f(int count, const float* value) { initData(count*4*2); memcpy(&mData[0], value, sizeof(mData[0]) * mData.size()); mType = UT_FLOAT_MAT4x2; }
    void setUniformMatrix3x4f(int count, const float* value) { initData(count*3*4); memcpy(&mData[0], value, sizeof(mData[0]) * mData.size()); mType = UT_FLOAT_MAT3x4; }
    void setUniformMatrix4x3f(int count, const float* value) { initData(count*4*3); memcpy(&mData[0], value, sizeof(mData[0]) * mData.size()); mType = UT_FLOAT_MAT4x3; }

    void setUniformMatrix2d(int count, const double* value) { initDouble(count*2*2); memcpy(&mData[0], value, sizeof(mData[0]) * mData.size()); mType = UT_DOUBLE_MAT2; }
    void setUniformMatrix3d(int count, const double* value) { initDouble(count*3*3); memcpy(&mData[0], value, sizeof(mData[0]) * mData.size()); mType = UT_DOUBLE_MAT3; }
    void setUniformMatrix4d(int count, const double* value) { initDouble(count*4*4); memcpy(&mData[0], value, sizeof(mData[0]) * mData.size()); mType = UT_DOUBLE_MAT4; }

    void setUniformMatrix2x3d(int count, const double* value) { initDouble(count*2*3); memcpy(&mData[0], value, sizeof(mData[0]) * mData.size()); mType = UT_DOUBLE_MAT2x3; }
    void setUniformMatrix3x2d(int count, const double* value) { initDouble(count*3*2); memcpy(&mData[0], value, sizeof(mData[0]) * mData.size()); mType = UT_DOUBLE_MAT3x2; }
    void setUniformMatrix2x4d(int count, const double* value) { initDouble(count*2*4); memcpy(&mData[0], value, sizeof(mData[0]) * mData.size()); mType = UT_DOUBLE_MAT2x4; }
    void setUniformMatrix4x2d(int count, const double* value) { initDouble(count*4*2); memcpy(&mData[0], value, sizeof(mData[0]) * mData.size()); mType = UT_DOUBLE_MAT4x2; }
    void setUniformMatrix3x4d(int count, const double* value) { initDouble(count*3*4); memcpy(&mData[0], value, sizeof(mData[0]) * mData.size()); mType = UT_DOUBLE_MAT3x4; }
    void setUniformMatrix4x3d(int count, const double* value) { initDouble(count*4*3); memcpy(&mData[0], value, sizeof(mData[0]) * mData.size()); mType = UT_DOUBLE_MAT4x3; }
Michele Bosi's avatar
Michele Bosi committed
142 143 144

    // vector/matrix array setters

Michele Bosi's avatar
Michele Bosi committed
145
    void setUniform(int count, const int* value)   { setUniform1i(count, value); }
Michele Bosi's avatar
Michele Bosi committed
146 147 148 149
    void setUniform(int count, const ivec2* value) { setUniform2i(count, value->ptr()); }
    void setUniform(int count, const ivec3* value) { setUniform3i(count, value->ptr()); }
    void setUniform(int count, const ivec4* value) { setUniform4i(count, value->ptr()); }

Michele Bosi's avatar
Michele Bosi committed
150
    void setUniform(int count, const unsigned int* value) { setUniform1ui(count, value); }
Michele Bosi's avatar
Michele Bosi committed
151 152 153 154
    void setUniform(int count, const uvec2* value) { setUniform2ui(count, value->ptr()); }
    void setUniform(int count, const uvec3* value) { setUniform3ui(count, value->ptr()); }
    void setUniform(int count, const uvec4* value) { setUniform4ui(count, value->ptr()); }

Michele Bosi's avatar
Michele Bosi committed
155
    void setUniform(int count, const float* value)  { setUniform1f(count, value); }
Michele Bosi's avatar
Michele Bosi committed
156 157 158 159 160 161 162 163
    void setUniform(int count, const fvec2* value)  { setUniform2f(count, value->ptr()); }
    void setUniform(int count, const fvec3* value)  { setUniform3f(count, value->ptr()); }
    void setUniform(int count, const fvec4* value)  { setUniform4f(count, value->ptr()); }

    void setUniform(int count, const fmat2* value)  { setUniformMatrix2f(count, value->ptr()); }
    void setUniform(int count, const fmat3* value)  { setUniformMatrix3f(count, value->ptr()); }
    void setUniform(int count, const fmat4* value)  { setUniformMatrix4f(count, value->ptr()); }

Michele Bosi's avatar
Michele Bosi committed
164
    void setUniform(int count, const double* value) { setUniform1d(count, value); }
Michele Bosi's avatar
Michele Bosi committed
165 166 167 168 169 170 171 172 173 174
    void setUniform(int count, const dvec2* value)  { setUniform2d(count, value->ptr()); }
    void setUniform(int count, const dvec3* value)  { setUniform3d(count, value->ptr()); }
    void setUniform(int count, const dvec4* value)  { setUniform4d(count, value->ptr()); }

    void setUniform(int count, const dmat2* value)  { setUniformMatrix2d(count, value->ptr()); }
    void setUniform(int count, const dmat3* value)  { setUniformMatrix3d(count, value->ptr()); }
    void setUniform(int count, const dmat4* value)  { setUniformMatrix4d(count, value->ptr()); }

    // single value setters

175
    void setUniform(int value)  { setUniform1i(1, &value); }
Michele Bosi's avatar
Michele Bosi committed
176
    void setUniformI(const int& value)  { setUniform1i(1, &value); }
Michele Bosi's avatar
Michele Bosi committed
177 178 179 180
    void setUniform(const ivec2& value) { setUniform2i(1, value.ptr()); }
    void setUniform(const ivec3& value) { setUniform3i(1, value.ptr()); }
    void setUniform(const ivec4& value) { setUniform4i(1, value.ptr()); }

181
    void setUniform(unsigned int value) { setUniform1ui(1, &value); }
Michele Bosi's avatar
Michele Bosi committed
182
    void setUniformU(const unsigned int& value) { setUniform1ui(1, &value); }
Michele Bosi's avatar
Michele Bosi committed
183 184 185 186
    void setUniform(const uvec2& value) { setUniform2ui(1, value.ptr()); }
    void setUniform(const uvec3& value) { setUniform3ui(1, value.ptr()); }
    void setUniform(const uvec4& value) { setUniform4ui(1, value.ptr()); }

187
    void setUniform(float value) { setUniform1f(1, &value); }
Michele Bosi's avatar
Michele Bosi committed
188
    void setUniformF(const float& value) { setUniform1f(1, &value); }
Michele Bosi's avatar
Michele Bosi committed
189 190 191 192
    void setUniform(const fvec2& value)  { setUniform2f(1, value.ptr()); }
    void setUniform(const fvec3& value)  { setUniform3f(1, value.ptr()); }
    void setUniform(const fvec4& value)  { setUniform4f(1, value.ptr()); }

Michele Bosi's avatar
Michele Bosi committed
193 194 195
    void setUniform(const fmat2& value) { setUniformMatrix2f(1, value.ptr()); }
    void setUniform(const fmat3& value) { setUniformMatrix3f(1, value.ptr()); }
    void setUniform(const fmat4& value) { setUniformMatrix4f(1, value.ptr()); }
Michele Bosi's avatar
Michele Bosi committed
196

197
    void setUniform(double value) { setUniform1d(1, &value); }
198
    void setUniformD(const double& value) { setUniform1d(1, &value); }
Michele Bosi's avatar
Michele Bosi committed
199 200 201
    void setUniform(const dvec2& value)   { setUniform2d(1, value.ptr()); }
    void setUniform(const dvec3& value)   { setUniform3d(1, value.ptr()); }
    void setUniform(const dvec4& value)   { setUniform4d(1, value.ptr()); }
Michele Bosi's avatar
Michele Bosi committed
202

Michele Bosi's avatar
Michele Bosi committed
203 204 205
    void setUniform(const dmat2& value) { setUniformMatrix2d(1, value.ptr()); }
    void setUniform(const dmat3& value) { setUniformMatrix3d(1, value.ptr()); }
    void setUniform(const dmat4& value) { setUniformMatrix4d(1, value.ptr()); }
Michele Bosi's avatar
Michele Bosi committed
206 207 208

    // getters

209 210 211 212 213
    float getUniformF()        const { VL_CHECK( type() != UT_NONE ); VL_CHECK( mData.size() == 1 ); float val; getUniform(&val); return val; }
    double getUniformD()       const { VL_CHECK( type() != UT_NONE ); VL_CHECK( mData.size() == 1 ); double val; getUniform(&val); return val; }
    int getUniformI()          const { VL_CHECK( type() != UT_NONE ); VL_CHECK( mData.size() == 1 ); int val; getUniform(&val); return val; }
    unsigned int getUniformU() const { VL_CHECK( type() != UT_NONE ); VL_CHECK( mData.size() == 1 ); unsigned int val; getUniform(&val); return val; }

Michele Bosi's avatar
Michele Bosi committed
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
    ivec2 getUniform2I() const { ivec2 v; getUniform( v.ptr() ); return v; }
    ivec3 getUniform3I() const { ivec3 v; getUniform( v.ptr() ); return v; }
    ivec4 getUniform4I() const { ivec4 v; getUniform( v.ptr() ); return v; }

    uvec2 getUniform2U() const { uvec2 v; getUniform( v.ptr() ); return v; }
    uvec3 getUniform3U() const { uvec3 v; getUniform( v.ptr() ); return v; }
    uvec4 getUniform4U() const { uvec4 v; getUniform( v.ptr() ); return v; }

    fvec2 getUniform2F() const { fvec2 v; getUniform( v.ptr() ); return v; }
    fvec3 getUniform3F() const { fvec3 v; getUniform( v.ptr() ); return v; }
    fvec4 getUniform4F() const { fvec4 v; getUniform( v.ptr() ); return v; }

    dvec2 getUniform2D() const { dvec2 v; getUniform( v.ptr() ); return v; }
    dvec3 getUniform3D() const { dvec3 v; getUniform( v.ptr() ); return v; }
    dvec4 getUniform4D() const { dvec4 v; getUniform( v.ptr() ); return v; }

Michele Bosi's avatar
Michele Bosi committed
230 231 232 233
    void getUniform(double* value) const { VL_CHECK(type() != UT_NONE); VL_CHECK(mData.size()); memcpy( value, &mData[0], sizeof(mData[0]) * mData.size()); }
    void getUniform(float* value) const { VL_CHECK(type() != UT_NONE); VL_CHECK(mData.size()); memcpy( value, &mData[0], sizeof(mData[0]) * mData.size()); }
    void getUniform(int* value) const { VL_CHECK(type() != UT_NONE); VL_CHECK(mData.size()); memcpy( value, &mData[0], sizeof(mData[0]) * mData.size()); }
    void getUniform(unsigned int* value) const { VL_CHECK(type() != UT_NONE); VL_CHECK(mData.size()); memcpy( value, &mData[0], sizeof(mData[0]) * mData.size()); }
Michele Bosi's avatar
Michele Bosi committed
234

Michele Bosi's avatar
Michele Bosi committed
235 236 237
    void getUniform(ivec2* value) const { getUniform(value->ptr()); }
    void getUniform(ivec3* value) const { getUniform(value->ptr()); }
    void getUniform(ivec4* value) const { getUniform(value->ptr()); }
Michele Bosi's avatar
Michele Bosi committed
238

Michele Bosi's avatar
Michele Bosi committed
239 240 241
    void getUniform(uvec2* value) const { getUniform(value->ptr()); }
    void getUniform(uvec3* value) const { getUniform(value->ptr()); }
    void getUniform(uvec4* value) const { getUniform(value->ptr()); }
Michele Bosi's avatar
Michele Bosi committed
242

Michele Bosi's avatar
Michele Bosi committed
243 244 245
    void getUniform(fvec2* value) const { getUniform(value->ptr()); }
    void getUniform(fvec3* value) const { getUniform(value->ptr()); }
    void getUniform(fvec4* value) const { getUniform(value->ptr()); }
Michele Bosi's avatar
Michele Bosi committed
246

Michele Bosi's avatar
Michele Bosi committed
247 248 249
    void getUniform(fmat2* value) const { getUniform(value->ptr()); }
    void getUniform(fmat3* value) const { getUniform(value->ptr()); }
    void getUniform(fmat4* value) const { getUniform(value->ptr()); }
Michele Bosi's avatar
Michele Bosi committed
250

Michele Bosi's avatar
Michele Bosi committed
251 252 253
    void getUniform(dvec2* value) const { getUniform(value->ptr()); }
    void getUniform(dvec3* value) const { getUniform(value->ptr()); }
    void getUniform(dvec4* value) const { getUniform(value->ptr()); }
Michele Bosi's avatar
Michele Bosi committed
254

Michele Bosi's avatar
Michele Bosi committed
255 256 257
    void getUniform(dmat2* value) const { getUniform(value->ptr()); }
    void getUniform(dmat3* value) const { getUniform(value->ptr()); }
    void getUniform(dmat4* value) const { getUniform(value->ptr()); }
Michele Bosi's avatar
Michele Bosi committed
258 259 260 261 262

    EUniformType type() const { return mType; }

    int count() const
    {
Michele Bosi's avatar
Michele Bosi committed
263 264 265
      if (mData.empty())
        return 0;

Michele Bosi's avatar
Michele Bosi committed
266 267
      switch(mType)
      {
Michele Bosi's avatar
Michele Bosi committed
268 269 270
        case UT_INT:      return singleCount();
        case UT_INT_VEC2: return singleCount() / 2;
        case UT_INT_VEC3: return singleCount() / 3;
Michele Bosi's avatar
Michele Bosi committed
271
        case UT_INT_VEC4: return singleCount() / 4;
272

Michele Bosi's avatar
Michele Bosi committed
273 274 275
        case UT_UNSIGNED_INT:      return singleCount();
        case UT_UNSIGNED_INT_VEC2: return singleCount() / 2;
        case UT_UNSIGNED_INT_VEC3: return singleCount() / 3;
Michele Bosi's avatar
Michele Bosi committed
276
        case UT_UNSIGNED_INT_VEC4: return singleCount() / 4;
277

Michele Bosi's avatar
Michele Bosi committed
278 279 280
        case UT_FLOAT:      return singleCount();
        case UT_FLOAT_VEC2: return singleCount() / 2;
        case UT_FLOAT_VEC3: return singleCount() / 3;
Michele Bosi's avatar
Michele Bosi committed
281
        case UT_FLOAT_VEC4: return singleCount() / 4;
282

Michele Bosi's avatar
Michele Bosi committed
283
        case UT_FLOAT_MAT2: return singleCount() / (2*2);
Michele Bosi's avatar
Michele Bosi committed
284 285
        case UT_FLOAT_MAT3: return singleCount() / (3*3);
        case UT_FLOAT_MAT4: return singleCount() / (4*4);
286 287 288 289 290 291 292 293

        case UT_FLOAT_MAT2x3: return singleCount() / (2*3);
        case UT_FLOAT_MAT3x2: return singleCount() / (3*2);
        case UT_FLOAT_MAT2x4: return singleCount() / (2*4);
        case UT_FLOAT_MAT4x2: return singleCount() / (4*2);
        case UT_FLOAT_MAT3x4: return singleCount() / (3*4);
        case UT_FLOAT_MAT4x3: return singleCount() / (4*3);

Michele Bosi's avatar
Michele Bosi committed
294
        case UT_DOUBLE:      return doubleCount();
295 296 297 298
        case UT_DOUBLE_VEC2: return doubleCount() / 2;
        case UT_DOUBLE_VEC3: return doubleCount() / 3;
        case UT_DOUBLE_VEC4: return doubleCount() / 4;

Michele Bosi's avatar
Michele Bosi committed
299 300 301
        case UT_DOUBLE_MAT2: return doubleCount() / (2*2);
        case UT_DOUBLE_MAT3: return doubleCount() / (3*3);
        case UT_DOUBLE_MAT4: return doubleCount() / (4*4);
302 303 304 305 306 307 308

        case UT_DOUBLE_MAT2x3: return doubleCount() / (2*3);
        case UT_DOUBLE_MAT3x2: return doubleCount() / (3*2);
        case UT_DOUBLE_MAT2x4: return doubleCount() / (2*4);
        case UT_DOUBLE_MAT4x2: return doubleCount() / (4*2);
        case UT_DOUBLE_MAT3x4: return doubleCount() / (3*4);
        case UT_DOUBLE_MAT4x3: return doubleCount() / (4*3);
Michele Bosi's avatar
Michele Bosi committed
309 310 311 312 313 314 315

        default:
        VL_TRAP()
        return -1;
      }
    }

Michele Bosi's avatar
Michele Bosi committed
316 317 318 319
    void* rawData() { if (mData.empty()) return NULL; else return &mData[0]; }

    const void* rawData() const { if (mData.empty()) return NULL; else return &mData[0]; }

Michele Bosi's avatar
Michele Bosi committed
320 321 322 323 324 325
  protected:
    VL_COMPILE_TIME_CHECK( sizeof(int) == sizeof(float) )
    void initData(int count) { mData.resize(count); }
    void initDouble(int count) { mData.resize(count*2); }
    int singleCount() const { return (int)mData.size(); }
    int doubleCount() const { VL_CHECK((mData.size() & 0x1) == 0 ); return (int)(mData.size() >> 1); }
Michele Bosi's avatar
Michele Bosi committed
326 327 328 329
    const double* doubleData() const { VL_CHECK(!mData.empty()); VL_CHECK((mData.size() & 0x1) == 0 ); return (double*)&mData[0]; }
    const float* floatData() const { VL_CHECK(!mData.empty()); return (float*)&mData[0]; }
    const int* intData() const { VL_CHECK(!mData.empty()); return (int*)&mData[0]; }
    const unsigned int* uintData() const { VL_CHECK(!mData.empty()); return (unsigned int*)&mData[0]; }
Michele Bosi's avatar
Michele Bosi committed
330 331 332 333 334 335 336 337

    EUniformType mType;
    std::vector<int> mData;
    std::string mName;
  };
}

#endif