synched with trunk at revision 32129
[blender.git] / source / gameengine / Ketsji / BL_Shader.h
1 #ifndef __BL_SHADER_H__
2 #define __BL_SHADER_H__
3
4 #include "PyObjectPlus.h"
5 #include "BL_Material.h"
6 #include "BL_Texture.h"
7 // --
8 #include "MT_Matrix4x4.h"
9 #include "MT_Matrix3x3.h"
10 #include "MT_Tuple2.h"
11 #include "MT_Tuple3.h"
12 #include "MT_Tuple4.h"
13
14 #define SHADER_ATTRIBMAX 1
15
16 /**
17  * BL_Sampler
18  *  Sampler access 
19  */
20 class BL_Sampler
21 {
22 public:
23         BL_Sampler():
24                 mLoc(-1)
25         {
26         }
27         int                             mLoc;           // Sampler location
28         
29 #ifdef WITH_CXX_GUARDEDALLOC
30 public:
31         void *operator new(size_t num_bytes) { return MEM_mallocN(num_bytes, "GE:BL_Sampler"); }
32         void operator delete( void *mem ) { MEM_freeN(mem); }
33 #endif
34 };
35
36 /**
37  * BL_Uniform
38  *  uniform storage 
39  */
40 class BL_Uniform 
41 {
42 private:
43         int                     mLoc;           // Uniform location
44         void*           mData;          // Memory allocated for variable
45         bool            mDirty;         // Caching variable  
46         int                     mType;          // Enum UniformTypes
47         bool            mTranspose; // Transpose matrices
48         const int       mDataLen;       // Length of our data
49 public:
50         BL_Uniform(int data_size);
51         ~BL_Uniform();
52         
53
54         void Apply(class BL_Shader *shader);
55         void SetData(int location, int type, bool transpose=false);
56
57         enum UniformTypes {
58                 UNI_NONE        =0,
59                 UNI_INT,
60                 UNI_FLOAT,
61                 UNI_INT2,
62                 UNI_FLOAT2,
63                 UNI_INT3,
64                 UNI_FLOAT3,
65                 UNI_INT4,
66                 UNI_FLOAT4,
67                 UNI_MAT3,
68                 UNI_MAT4,
69                 UNI_MAX
70         };
71
72         int GetLocation()       { return mLoc; }
73         void* getData()         { return mData; }
74         
75         
76 #ifdef WITH_CXX_GUARDEDALLOC
77 public:
78         void *operator new(size_t num_bytes) { return MEM_mallocN(num_bytes, "GE:BL_Uniform"); }
79         void operator delete( void *mem ) { MEM_freeN(mem); }
80 #endif
81 };
82
83 /**
84  * BL_DefUniform
85  * pre defined uniform storage 
86  */
87 class BL_DefUniform
88 {
89 public:
90         BL_DefUniform() :
91                 mType(0),
92                 mLoc(0),
93                 mFlag(0)
94         {
95         }
96         int                             mType;
97         int                             mLoc;
98         unsigned int    mFlag;
99         
100         
101 #ifdef WITH_CXX_GUARDEDALLOC
102 public:
103         void *operator new(size_t num_bytes) { return MEM_mallocN(num_bytes, "GE:BL_DefUniform"); }
104         void operator delete( void *mem ) { MEM_freeN(mem); }
105 #endif
106 };
107
108 /**
109  * BL_Shader
110  *  shader access
111  */
112 class BL_Shader : public PyObjectPlus
113 {
114         Py_Header;
115 private:
116         typedef std::vector<BL_Uniform*>        BL_UniformVec;
117         typedef std::vector<BL_DefUniform*>     BL_UniformVecDef;
118
119         unsigned int    mShader;                        // Shader object 
120         int                             mPass;                          // 1.. unused
121         bool                    mOk;                            // Valid and ok
122         bool                    mUse;                           // ...
123 //BL_Sampler            mSampler[MAXTEX];       // Number of samplers
124         int                             mAttr;                          // Tangent attribute
125         const char*             vertProg;                       // Vertex program string
126         const char*             fragProg;                       // Fragment program string
127         bool                    mError;                         // ...
128         bool                    mDirty;                         // 
129
130         // Compiles and links the shader
131         bool LinkProgram();
132
133         // Stored uniform variables
134         BL_UniformVec           mUniforms;
135         BL_UniformVecDef        mPreDef;
136
137         // search by location
138         BL_Uniform*             FindUniform(const int location);
139         // clears uniform data
140         void                    ClearUniforms();
141
142 public:
143         BL_Shader();
144         virtual ~BL_Shader();
145
146         // Unused for now tangent is set as 
147         // tex coords
148         enum AttribTypes {
149                 SHD_TANGENT =1
150         };
151
152         enum GenType {
153                 MODELVIEWMATRIX,
154                 MODELVIEWMATRIX_TRANSPOSE,
155                 MODELVIEWMATRIX_INVERSE,
156                 MODELVIEWMATRIX_INVERSETRANSPOSE,
157         
158                 // Model matrix
159                 MODELMATRIX,
160                 MODELMATRIX_TRANSPOSE,
161                 MODELMATRIX_INVERSE,
162                 MODELMATRIX_INVERSETRANSPOSE,
163         
164                 // View Matrix
165                 VIEWMATRIX,
166                 VIEWMATRIX_TRANSPOSE,
167                 VIEWMATRIX_INVERSE,
168                 VIEWMATRIX_INVERSETRANSPOSE,
169
170                 // Current camera position 
171                 CAM_POS,
172
173                 // RAS timer
174                 CONSTANT_TIMER
175         };
176
177         const char* GetVertPtr();
178         const char* GetFragPtr();
179         void SetVertPtr( char *vert );
180         void SetFragPtr( char *frag );
181         
182         // ---
183         int getNumPass()        {return mPass;}
184         bool GetError()         {return mError;}
185         // ---
186         //const BL_Sampler*     GetSampler(int i);
187         void                            SetSampler(int loc, int unit);
188
189         bool                            Ok()const;
190         unsigned int            GetProg();
191         void                            SetProg(bool enable);
192         int                                     GetAttribute(){return mAttr;};
193
194         // -- 
195         // Apply methods : sets colected uniforms
196         void ApplyShader();
197         void UnloadShader();
198
199         // Update predefined uniforms each render call
200         void Update(const class RAS_MeshSlot & ms, class RAS_IRasterizer* rasty);
201
202         //// Set sampler units (copied)
203         //void InitializeSampler(int unit, BL_Texture* texture );
204
205
206         void SetUniformfv(int location,int type, float *param, int size,bool transpose=false);
207         void SetUniformiv(int location,int type, int *param, int size,bool transpose=false);
208
209         int GetAttribLocation(const STR_String& name);
210         void BindAttribute(const STR_String& attr, int loc);
211         int GetUniformLocation(const STR_String& name);
212
213         void SetUniform(int uniform, const MT_Tuple2& vec);
214         void SetUniform(int uniform, const MT_Tuple3& vec);
215         void SetUniform(int uniform, const MT_Tuple4& vec);
216         void SetUniform(int uniform, const MT_Matrix4x4& vec, bool transpose=false);
217         void SetUniform(int uniform, const MT_Matrix3x3& vec, bool transpose=false);
218         void SetUniform(int uniform, const float& val);
219         void SetUniform(int uniform, const float* val, int len);
220         void SetUniform(int uniform, const int* val, int len);
221         void SetUniform(int uniform, const unsigned int& val);
222         void SetUniform(int uniform, const int val);
223
224         // Python interface
225 #ifndef DISABLE_PYTHON
226         virtual PyObject* py_repr(void) { return PyUnicode_FromFormat("BL_Shader\n\tvertex shader:%s\n\n\tfragment shader%s\n\n", vertProg, fragProg); }
227
228         // -----------------------------------
229         KX_PYMETHOD_DOC( BL_Shader, setSource );
230         KX_PYMETHOD_DOC( BL_Shader, delSource );
231         KX_PYMETHOD_DOC( BL_Shader, getVertexProg );
232         KX_PYMETHOD_DOC( BL_Shader, getFragmentProg );
233         KX_PYMETHOD_DOC( BL_Shader, setNumberOfPasses );
234         KX_PYMETHOD_DOC( BL_Shader, isValid);
235         KX_PYMETHOD_DOC( BL_Shader, validate);
236
237         // -----------------------------------
238         KX_PYMETHOD_DOC( BL_Shader, setUniform4f );
239         KX_PYMETHOD_DOC( BL_Shader, setUniform3f );
240         KX_PYMETHOD_DOC( BL_Shader, setUniform2f );
241         KX_PYMETHOD_DOC( BL_Shader, setUniform1f );
242         KX_PYMETHOD_DOC( BL_Shader, setUniform4i );
243         KX_PYMETHOD_DOC( BL_Shader, setUniform3i );
244         KX_PYMETHOD_DOC( BL_Shader, setUniform2i );
245         KX_PYMETHOD_DOC( BL_Shader, setUniform1i );
246         KX_PYMETHOD_DOC( BL_Shader, setUniformfv );
247         KX_PYMETHOD_DOC( BL_Shader, setUniformiv );
248         KX_PYMETHOD_DOC( BL_Shader, setUniformMatrix4 );
249         KX_PYMETHOD_DOC( BL_Shader, setUniformMatrix3 );
250         KX_PYMETHOD_DOC( BL_Shader, setUniformDef );
251         KX_PYMETHOD_DOC( BL_Shader, setAttrib );
252         KX_PYMETHOD_DOC( BL_Shader, setSampler);
253 #endif
254 };
255
256 #endif//__BL_SHADER_H__