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