Welcome to mirror list, hosted at ThFree Co, Russian Federation.

BL_Shader.h « Ketsji « gameengine « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 5108acea0ffe742fac56b2e2f0961999bf4bb1be (plain)
1
2
3
4
5
6
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#ifndef __BL_SHADER_H__
#define __BL_SHADER_H__

#include "PyObjectPlus.h"
#include "BL_Material.h"
#include "BL_Texture.h"
// --
#include "MT_Matrix4x4.h"
#include "MT_Matrix3x3.h"
#include "MT_Tuple2.h"
#include "MT_Tuple3.h"
#include "MT_Tuple4.h"

#define SHADER_ATTRIBMAX 1

/**
 * BL_Sampler
 *  Sampler access 
 */
class BL_Sampler
{
public:
	BL_Sampler():
		mLoc(-1)
	{
	}
	int				mLoc;		// Sampler location
	
#ifdef WITH_CXX_GUARDEDALLOC
public:
	void *operator new(size_t num_bytes) { return MEM_mallocN(num_bytes, "GE:BL_Sampler"); }
	void operator delete( void *mem ) { MEM_freeN(mem); }
#endif
};

/**
 * BL_Uniform
 *  uniform storage 
 */
class BL_Uniform 
{
private:
	int			mLoc;		// Uniform location
	void*		mData;		// Memory allocated for variable
	bool		mDirty;		// Caching variable  
	int			mType;		// Enum UniformTypes
	bool		mTranspose; // Transpose matrices
	const int	mDataLen;	// Length of our data
public:
	BL_Uniform(int data_size);
	~BL_Uniform();
	

	void Apply(class BL_Shader *shader);
	void SetData(int location, int type, bool transpose=false);

	enum UniformTypes {
		UNI_NONE	=0,
		UNI_INT,
		UNI_FLOAT,
		UNI_INT2,
		UNI_FLOAT2,
		UNI_INT3,
		UNI_FLOAT3,
		UNI_INT4,
		UNI_FLOAT4,
		UNI_MAT3,
		UNI_MAT4,
		UNI_MAX
	};

	int GetLocation()	{ return mLoc; }
	void* getData()		{ return mData; }
	
	
#ifdef WITH_CXX_GUARDEDALLOC
public:
	void *operator new(size_t num_bytes) { return MEM_mallocN(num_bytes, "GE:BL_Uniform"); }
	void operator delete( void *mem ) { MEM_freeN(mem); }
#endif
};

/**
 * BL_DefUniform
 * pre defined uniform storage 
 */
class BL_DefUniform
{
public:
	BL_DefUniform() :
		mType(0),
		mLoc(0),
		mFlag(0)
	{
	}
	int				mType;
	int				mLoc;
	unsigned int	mFlag;
	
	
#ifdef WITH_CXX_GUARDEDALLOC
public:
	void *operator new(size_t num_bytes) { return MEM_mallocN(num_bytes, "GE:BL_DefUniform"); }
	void operator delete( void *mem ) { MEM_freeN(mem); }
#endif
};

/**
 * BL_Shader
 *  shader access
 */
class BL_Shader : public PyObjectPlus
{
	Py_Header;
private:
	typedef std::vector<BL_Uniform*>	BL_UniformVec;
	typedef std::vector<BL_DefUniform*>	BL_UniformVecDef;

	unsigned int	mShader;			// Shader object 
	int				mPass;				// 1.. unused
	bool			mOk;				// Valid and ok
	bool			mUse;				// ...
//BL_Sampler		mSampler[MAXTEX];	// Number of samplers
	int				mAttr;				// Tangent attribute
	const char*		vertProg;			// Vertex program string
	const char*		fragProg;			// Fragment program string
	bool			mError;				// ...
	bool			mDirty;				// 

	// Compiles and links the shader
	bool LinkProgram();

	// Stored uniform variables
	BL_UniformVec		mUniforms;
	BL_UniformVecDef	mPreDef;

	// search by location
	BL_Uniform*		FindUniform(const int location);
	// clears uniform data
	void			ClearUniforms();

public:
	BL_Shader();
	virtual ~BL_Shader();

	// Unused for now tangent is set as 
	// tex coords
	enum AttribTypes {
		SHD_TANGENT =1
	};

	enum GenType {
		MODELVIEWMATRIX,
		MODELVIEWMATRIX_TRANSPOSE,
		MODELVIEWMATRIX_INVERSE,
		MODELVIEWMATRIX_INVERSETRANSPOSE,
	
		// Model matrix
		MODELMATRIX,
		MODELMATRIX_TRANSPOSE,
		MODELMATRIX_INVERSE,
		MODELMATRIX_INVERSETRANSPOSE,
	
		// View Matrix
		VIEWMATRIX,
		VIEWMATRIX_TRANSPOSE,
		VIEWMATRIX_INVERSE,
		VIEWMATRIX_INVERSETRANSPOSE,

		// Current camera position 
		CAM_POS,

		// RAS timer
		CONSTANT_TIMER
	};

	const char* GetVertPtr();
	const char* GetFragPtr();
	void SetVertPtr( char *vert );
	void SetFragPtr( char *frag );
	
	// ---
	int getNumPass()	{return mPass;}
	bool GetError()		{return mError;}
	// ---
	//const BL_Sampler*	GetSampler(int i);
	void				SetSampler(int loc, int unit);

	bool				Ok()const;
	unsigned int		GetProg();
	void				SetProg(bool enable);
	int					GetAttribute(){return mAttr;};

	// -- 
	// Apply methods : sets colected uniforms
	void ApplyShader();
	void UnloadShader();

	// Update predefined uniforms each render call
	void Update(const class RAS_MeshSlot & ms, class RAS_IRasterizer* rasty);

	//// Set sampler units (copied)
	//void InitializeSampler(int unit, BL_Texture* texture );


	void SetUniformfv(int location,int type, float *param, int size,bool transpose=false);
	void SetUniformiv(int location,int type, int *param, int size,bool transpose=false);

	int GetAttribLocation(const STR_String& name);
	void BindAttribute(const STR_String& attr, int loc);
	int GetUniformLocation(const STR_String& name);

	void SetUniform(int uniform, const MT_Tuple2& vec);
	void SetUniform(int uniform, const MT_Tuple3& vec);
	void SetUniform(int uniform, const MT_Tuple4& vec);
	void SetUniform(int uniform, const MT_Matrix4x4& vec, bool transpose=false);
	void SetUniform(int uniform, const MT_Matrix3x3& vec, bool transpose=false);
	void SetUniform(int uniform, const float& val);
	void SetUniform(int uniform, const float* val, int len);
	void SetUniform(int uniform, const int* val, int len);
	void SetUniform(int uniform, const unsigned int& val);
	void SetUniform(int uniform, const int val);

	// Python interface
#ifndef DISABLE_PYTHON
	virtual PyObject* py_repr(void) { return PyUnicode_FromFormat("BL_Shader\n\tvertex shader:%s\n\n\tfragment shader%s\n\n", vertProg, fragProg); }

	// -----------------------------------
	KX_PYMETHOD_DOC( BL_Shader, setSource );
	KX_PYMETHOD_DOC( BL_Shader, delSource );
	KX_PYMETHOD_DOC( BL_Shader, getVertexProg );
	KX_PYMETHOD_DOC( BL_Shader, getFragmentProg );
	KX_PYMETHOD_DOC( BL_Shader, setNumberOfPasses );
	KX_PYMETHOD_DOC( BL_Shader, isValid);
	KX_PYMETHOD_DOC( BL_Shader, validate);

	// -----------------------------------
	KX_PYMETHOD_DOC( BL_Shader, setUniform4f );
	KX_PYMETHOD_DOC( BL_Shader, setUniform3f );
	KX_PYMETHOD_DOC( BL_Shader, setUniform2f );
	KX_PYMETHOD_DOC( BL_Shader, setUniform1f );
	KX_PYMETHOD_DOC( BL_Shader, setUniform4i );
	KX_PYMETHOD_DOC( BL_Shader, setUniform3i );
	KX_PYMETHOD_DOC( BL_Shader, setUniform2i );
	KX_PYMETHOD_DOC( BL_Shader, setUniform1i );
	KX_PYMETHOD_DOC( BL_Shader, setUniformfv );
	KX_PYMETHOD_DOC( BL_Shader, setUniformiv );
	KX_PYMETHOD_DOC( BL_Shader, setUniformMatrix4 );
	KX_PYMETHOD_DOC( BL_Shader, setUniformMatrix3 );
	KX_PYMETHOD_DOC( BL_Shader, setUniformDef );
	KX_PYMETHOD_DOC( BL_Shader, setAttrib );
	KX_PYMETHOD_DOC( BL_Shader, setSampler);
#endif
};

#endif//__BL_SHADER_H__