Blender V2.61 - r43446

BL_Shader.h

Go to the documentation of this file.
00001 
00006 #ifndef __BL_SHADER_H__
00007 #define __BL_SHADER_H__
00008 
00009 #include "PyObjectPlus.h"
00010 #include "BL_Material.h"
00011 #include "BL_Texture.h"
00012 // --
00013 #include "MT_Matrix4x4.h"
00014 #include "MT_Matrix3x3.h"
00015 #include "MT_Tuple2.h"
00016 #include "MT_Tuple3.h"
00017 #include "MT_Tuple4.h"
00018 
00019 #define SHADER_ATTRIBMAX 1
00020 
00025 class BL_Sampler
00026 {
00027 public:
00028     BL_Sampler():
00029         mLoc(-1)
00030     {
00031     }
00032     int             mLoc;       // Sampler location
00033     
00034 #ifdef WITH_CXX_GUARDEDALLOC
00035 public:
00036     void *operator new(size_t num_bytes) { return MEM_mallocN(num_bytes, "GE:BL_Sampler"); }
00037     void operator delete( void *mem ) { MEM_freeN(mem); }
00038 #endif
00039 };
00040 
00045 class BL_Uniform 
00046 {
00047 private:
00048     int         mLoc;       // Uniform location
00049     void*       mData;      // Memory allocated for variable
00050     bool        mDirty;     // Caching variable  
00051     int         mType;      // Enum UniformTypes
00052     bool        mTranspose; // Transpose matrices
00053     const int   mDataLen;   // Length of our data
00054 public:
00055     BL_Uniform(int data_size);
00056     ~BL_Uniform();
00057     
00058 
00059     void Apply(class BL_Shader *shader);
00060     void SetData(int location, int type, bool transpose=false);
00061 
00062     enum UniformTypes {
00063         UNI_NONE    =0,
00064         UNI_INT,
00065         UNI_FLOAT,
00066         UNI_INT2,
00067         UNI_FLOAT2,
00068         UNI_INT3,
00069         UNI_FLOAT3,
00070         UNI_INT4,
00071         UNI_FLOAT4,
00072         UNI_MAT3,
00073         UNI_MAT4,
00074         UNI_MAX
00075     };
00076 
00077     int GetLocation()   { return mLoc; }
00078     void* getData()     { return mData; }
00079     
00080     
00081 #ifdef WITH_CXX_GUARDEDALLOC
00082 public:
00083     void *operator new(size_t num_bytes) { return MEM_mallocN(num_bytes, "GE:BL_Uniform"); }
00084     void operator delete( void *mem ) { MEM_freeN(mem); }
00085 #endif
00086 };
00087 
00092 class BL_DefUniform
00093 {
00094 public:
00095     BL_DefUniform() :
00096         mType(0),
00097         mLoc(0),
00098         mFlag(0)
00099     {
00100     }
00101     int             mType;
00102     int             mLoc;
00103     unsigned int    mFlag;
00104     
00105     
00106 #ifdef WITH_CXX_GUARDEDALLOC
00107 public:
00108     void *operator new(size_t num_bytes) { return MEM_mallocN(num_bytes, "GE:BL_DefUniform"); }
00109     void operator delete( void *mem ) { MEM_freeN(mem); }
00110 #endif
00111 };
00112 
00117 class BL_Shader : public PyObjectPlus
00118 {
00119     Py_Header
00120 private:
00121     typedef std::vector<BL_Uniform*>    BL_UniformVec;
00122     typedef std::vector<BL_DefUniform*> BL_UniformVecDef;
00123 
00124     unsigned int    mShader;            // Shader object 
00125     int             mPass;              // 1.. unused
00126     bool            mOk;                // Valid and ok
00127     bool            mUse;               // ...
00128 //BL_Sampler        mSampler[MAXTEX];   // Number of samplers
00129     int             mAttr;              // Tangent attribute
00130     const char*     vertProg;           // Vertex program string
00131     const char*     fragProg;           // Fragment program string
00132     bool            mError;             // ...
00133     bool            mDirty;             // 
00134 
00135     // Compiles and links the shader
00136     bool LinkProgram();
00137 
00138     // Stored uniform variables
00139     BL_UniformVec       mUniforms;
00140     BL_UniformVecDef    mPreDef;
00141 
00142     // search by location
00143     BL_Uniform*     FindUniform(const int location);
00144     // clears uniform data
00145     void            ClearUniforms();
00146 
00147 public:
00148     BL_Shader();
00149     virtual ~BL_Shader();
00150 
00151     // Unused for now tangent is set as 
00152     // tex coords
00153     enum AttribTypes {
00154         SHD_TANGENT =1
00155     };
00156 
00157     enum GenType {
00158         MODELVIEWMATRIX,
00159         MODELVIEWMATRIX_TRANSPOSE,
00160         MODELVIEWMATRIX_INVERSE,
00161         MODELVIEWMATRIX_INVERSETRANSPOSE,
00162     
00163         // Model matrix
00164         MODELMATRIX,
00165         MODELMATRIX_TRANSPOSE,
00166         MODELMATRIX_INVERSE,
00167         MODELMATRIX_INVERSETRANSPOSE,
00168     
00169         // View Matrix
00170         VIEWMATRIX,
00171         VIEWMATRIX_TRANSPOSE,
00172         VIEWMATRIX_INVERSE,
00173         VIEWMATRIX_INVERSETRANSPOSE,
00174 
00175         // Current camera position 
00176         CAM_POS,
00177 
00178         // RAS timer
00179         CONSTANT_TIMER
00180     };
00181 
00182     const char* GetVertPtr();
00183     const char* GetFragPtr();
00184     void SetVertPtr( char *vert );
00185     void SetFragPtr( char *frag );
00186     
00187     // ---
00188     int getNumPass()    {return mPass;}
00189     bool GetError()     {return mError;}
00190     // ---
00191     //const BL_Sampler* GetSampler(int i);
00192     void                SetSampler(int loc, int unit);
00193 
00194     bool                Ok()const;
00195     unsigned int        GetProg();
00196     void                SetProg(bool enable);
00197     int                 GetAttribute(){return mAttr;};
00198 
00199     // -- 
00200     // Apply methods : sets colected uniforms
00201     void ApplyShader();
00202     void UnloadShader();
00203 
00204     // Update predefined uniforms each render call
00205     void Update(const class RAS_MeshSlot & ms, class RAS_IRasterizer* rasty);
00206 
00208     //void InitializeSampler(int unit, BL_Texture* texture );
00209 
00210 
00211     void SetUniformfv(int location,int type, float *param, int size,bool transpose=false);
00212     void SetUniformiv(int location,int type, int *param, int size,bool transpose=false);
00213 
00214     int GetAttribLocation(const STR_String& name);
00215     void BindAttribute(const STR_String& attr, int loc);
00216     int GetUniformLocation(const STR_String& name);
00217 
00218     void SetUniform(int uniform, const MT_Tuple2& vec);
00219     void SetUniform(int uniform, const MT_Tuple3& vec);
00220     void SetUniform(int uniform, const MT_Tuple4& vec);
00221     void SetUniform(int uniform, const MT_Matrix4x4& vec, bool transpose=false);
00222     void SetUniform(int uniform, const MT_Matrix3x3& vec, bool transpose=false);
00223     void SetUniform(int uniform, const float& val);
00224     void SetUniform(int uniform, const float* val, int len);
00225     void SetUniform(int uniform, const int* val, int len);
00226     void SetUniform(int uniform, const unsigned int& val);
00227     void SetUniform(int uniform, const int val);
00228 
00229     // Python interface
00230 #ifdef WITH_PYTHON
00231     virtual PyObject* py_repr(void) { return PyUnicode_FromFormat("BL_Shader\n\tvertex shader:%s\n\n\tfragment shader%s\n\n", vertProg, fragProg); }
00232 
00233     // -----------------------------------
00234     KX_PYMETHOD_DOC( BL_Shader, setSource );
00235     KX_PYMETHOD_DOC( BL_Shader, delSource );
00236     KX_PYMETHOD_DOC( BL_Shader, getVertexProg );
00237     KX_PYMETHOD_DOC( BL_Shader, getFragmentProg );
00238     KX_PYMETHOD_DOC( BL_Shader, setNumberOfPasses );
00239     KX_PYMETHOD_DOC( BL_Shader, isValid);
00240     KX_PYMETHOD_DOC( BL_Shader, validate);
00241 
00242     // -----------------------------------
00243     KX_PYMETHOD_DOC( BL_Shader, setUniform4f );
00244     KX_PYMETHOD_DOC( BL_Shader, setUniform3f );
00245     KX_PYMETHOD_DOC( BL_Shader, setUniform2f );
00246     KX_PYMETHOD_DOC( BL_Shader, setUniform1f );
00247     KX_PYMETHOD_DOC( BL_Shader, setUniform4i );
00248     KX_PYMETHOD_DOC( BL_Shader, setUniform3i );
00249     KX_PYMETHOD_DOC( BL_Shader, setUniform2i );
00250     KX_PYMETHOD_DOC( BL_Shader, setUniform1i );
00251     KX_PYMETHOD_DOC( BL_Shader, setUniformfv );
00252     KX_PYMETHOD_DOC( BL_Shader, setUniformiv );
00253     KX_PYMETHOD_DOC( BL_Shader, setUniformMatrix4 );
00254     KX_PYMETHOD_DOC( BL_Shader, setUniformMatrix3 );
00255     KX_PYMETHOD_DOC( BL_Shader, setUniformDef );
00256     KX_PYMETHOD_DOC( BL_Shader, setAttrib );
00257     KX_PYMETHOD_DOC( BL_Shader, setSampler);
00258 #endif
00259 };
00260 
00261 #endif//__BL_SHADER_H__