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