aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/irrlicht-1.8/source/Irrlicht/CCgMaterialRenderer.cpp
blob: f307d57bcd93641a37d75c739e8c03a8ad6efe60 (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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
// Copyright (C) 2012 Patryk Nadrowski
// This file is part of the "Irrlicht Engine".
// For conditions of distribution and use, see copyright notice in irrlicht.h

#include "IrrCompileConfig.h"
#ifdef _IRR_COMPILE_WITH_CG_

#include "CCgMaterialRenderer.h"

namespace irr
{
namespace video
{

CCgUniform::CCgUniform(const CGparameter& parameter, bool global) : Parameter(parameter), Type(CG_UNKNOWN_TYPE)
{
	Name = cgGetParameterName(Parameter);

	if(global)
		Space = CG_GLOBAL;
	else
		Space = CG_PROGRAM;
}

const core::stringc& CCgUniform::getName() const
{
	return Name;
}

const CGparameter& CCgUniform::getParameter() const
{
	return Parameter;
}

CGenum CCgUniform::getSpace() const
{
	return Space;
}

CGtype CCgUniform::getType() const
{
	return Type;
}

CCgUniform1f::CCgUniform1f(const CGparameter& parameter, bool global) : CCgUniform(parameter, global)
{
	Type = CG_FLOAT;
}

void CCgUniform1f::update(const void* data, const SMaterial& material) const
{
	f32* Data = (f32*)data;
	cgSetParameter1f(Parameter, *Data);
}

CCgUniform2f::CCgUniform2f(const CGparameter& parameter, bool global) : CCgUniform(parameter, global)
{
	Type = CG_FLOAT2;
}

void CCgUniform2f::update(const void* data, const SMaterial& material) const
{
	f32* Data = (f32*)data;
	cgSetParameter2f(Parameter, *Data, *(Data+1));
}

CCgUniform3f::CCgUniform3f(const CGparameter& parameter, bool global) : CCgUniform(parameter, global)
{
	Type = CG_FLOAT3;
}

void CCgUniform3f::update(const void* data, const SMaterial& material) const
{
	f32* Data = (f32*)data;
	cgSetParameter3f(Parameter, *Data, *(Data+1), *(Data+2));
}

CCgUniform4f::CCgUniform4f(const CGparameter& parameter, bool global) : CCgUniform(parameter, global)
{
	Type = CG_FLOAT4;
}

void CCgUniform4f::update(const void* data, const SMaterial& material) const
{
	f32* Data = (f32*)data;
	cgSetParameter4f(Parameter, *Data, *(Data+1), *(Data+2), *(Data+3));
}

CCgUniform1i::CCgUniform1i(const CGparameter& parameter, bool global) : CCgUniform(parameter, global)
{
	Type = CG_INT;
}

void CCgUniform1i::update(const void* data, const SMaterial& material) const
{
	s32* Data = (s32*)data;
	cgSetParameter1i(Parameter, *Data);
}

CCgUniform2i::CCgUniform2i(const CGparameter& parameter, bool global) : CCgUniform(parameter, global)
{
	Type = CG_INT2;
}

void CCgUniform2i::update(const void* data, const SMaterial& material) const
{
	s32* Data = (s32*)data;
	cgSetParameter2i(Parameter, *Data, *(Data+1));
}

CCgUniform3i::CCgUniform3i(const CGparameter& parameter, bool global) : CCgUniform(parameter, global)
{
	Type = CG_INT3;
}

void CCgUniform3i::update(const void* data, const SMaterial& material) const
{
	s32* Data = (s32*)data;
	cgSetParameter3i(Parameter, *Data, *(Data+1), *(Data+2));
}

CCgUniform4i::CCgUniform4i(const CGparameter& parameter, bool global) : CCgUniform(parameter, global)
{
	Type = CG_INT4;
}

void CCgUniform4i::update(const void* data, const SMaterial& material) const
{
	s32* Data = (s32*)data;
	cgSetParameter4i(Parameter, *Data, *(Data+1), *(Data+2), *(Data+3));
}

CCgUniform4x4f::CCgUniform4x4f(const CGparameter& parameter, bool global) : CCgUniform(parameter, global)
{
	Type = CG_FLOAT4x4;
}

void CCgUniform4x4f::update(const void* data, const SMaterial& material) const
{
	f32* Data = (f32*)data;
	cgSetMatrixParameterfr(Parameter, Data);
}

CCgUniformSampler2D::CCgUniformSampler2D(const CGparameter& parameter, bool global) : CCgUniform(parameter, global)
{
	Type = CG_SAMPLER2D;
}

void CCgUniformSampler2D::update(const void* data, const SMaterial& material) const
{
}

CCgMaterialRenderer::CCgMaterialRenderer(IShaderConstantSetCallBack* callback, IMaterialRenderer* baseMaterial, s32 userData) :
	CallBack(callback), BaseMaterial(baseMaterial), UserData(userData),
	VertexProgram(0), FragmentProgram(0), GeometryProgram(0), VertexProfile(CG_PROFILE_UNKNOWN), FragmentProfile(CG_PROFILE_UNKNOWN), GeometryProfile(CG_PROFILE_UNKNOWN),
	Material(IdentityMaterial), Error(CG_NO_ERROR)
{
	#ifdef _DEBUG
	setDebugName("CCgMaterialRenderer");
	#endif

	if(BaseMaterial)
		BaseMaterial->grab();

	if(CallBack)
		CallBack->grab();
}

CCgMaterialRenderer::~CCgMaterialRenderer()
{
	if(CallBack)
		CallBack->drop();

	if(BaseMaterial)
		BaseMaterial->drop();

	for(unsigned int i = 0; i < UniformInfo.size(); ++i)
		delete UniformInfo[i];

	UniformInfo.clear();
}

bool CCgMaterialRenderer::isTransparent() const
{
	return BaseMaterial ? BaseMaterial->isTransparent() : false;
}

void CCgMaterialRenderer::setVertexShaderConstant(const f32* data, s32 startRegister, s32 constantAmount)
{
	os::Printer::log("Cannot set constant, please use high level shader call instead.", ELL_WARNING);
}

bool CCgMaterialRenderer::setVertexShaderConstant(const c8* name, const f32* floats, int count)
{
	return setPixelShaderConstant(name, floats, count);
}

bool CCgMaterialRenderer::setVertexShaderConstant(const c8* name, const bool* bools, int count)
{
	return setPixelShaderConstant(name, bools, count);
}

bool CCgMaterialRenderer::setVertexShaderConstant(const c8* name, const s32* ints, int count)
{
	return setPixelShaderConstant(name, ints, count);
}

void CCgMaterialRenderer::setPixelShaderConstant(const f32* data, s32 startRegister, s32 constantAmount)
{
	os::Printer::log("Cannot set constant, please use high level shader call instead.", ELL_WARNING);
}

bool CCgMaterialRenderer::setPixelShaderConstant(const c8* name, const f32* floats, int count)
{
	bool Status = false;

	for(unsigned int i = 0; i < UniformInfo.size(); ++i)
	{
		if(UniformInfo[i]->getName() == name)
		{
			UniformInfo[i]->update(floats, Material);

			Status = true;
		}
	}

	return Status;
}

bool CCgMaterialRenderer::setPixelShaderConstant(const c8* name, const s32* ints, int count)
{
	bool Status = false;

	for(unsigned int i = 0; i < UniformInfo.size(); ++i)
	{
		if(UniformInfo[i]->getName() == name)
		{
			UniformInfo[i]->update(ints, Material);

			Status = true;
		}
	}

	return Status;
}

bool CCgMaterialRenderer::setPixelShaderConstant(const c8* name, const bool* bools, int count)
{
	bool Status = false;

	for(unsigned int i = 0; i < UniformInfo.size(); ++i)
	{
		if(UniformInfo[i]->getName() == name)
		{
			UniformInfo[i]->update(bools, Material);

			Status = true;
		}
	}

	return Status;
}

void CCgMaterialRenderer::getUniformList()
{
	for(unsigned int i = 0; i < UniformInfo.size(); ++i)
		delete UniformInfo[i];

	UniformInfo.clear();

	for(unsigned int i = 0; i < 2; ++i)
	{
		CGenum Space = CG_GLOBAL;
		bool IsGlobal = 1;

		if(i == 1)
		{
			Space = CG_PROGRAM;
			IsGlobal = 0;
		}

		for(unsigned int j = 0; j < 3; ++j)
		{
			CGprogram* Program = 0;

			switch(j)
			{
			case 0:
				Program = &VertexProgram;
				break;
			case 1:
				Program = &FragmentProgram;
				break;
			case 2:
				Program = &GeometryProgram;
				break;
			}

			if(*Program)
			{
				CGparameter Parameter = cgGetFirstParameter(*Program, Space);

				while(Parameter)
				{
					if(cgGetParameterVariability(Parameter) == CG_UNIFORM && cgGetParameterDirection(Parameter) == CG_IN)
					{
						CCgUniform* Uniform = 0;

						CGtype Type = cgGetParameterType(Parameter);

						switch(Type)
						{
						case CG_FLOAT:
						case CG_FLOAT1:
							Uniform = new CCgUniform1f(Parameter, IsGlobal);
							break;
						case CG_FLOAT2:
							Uniform = new CCgUniform2f(Parameter, IsGlobal);
							break;
						case CG_FLOAT3:
							Uniform = new CCgUniform3f(Parameter, IsGlobal);
							break;
						case CG_FLOAT4:
							Uniform = new CCgUniform4f(Parameter, IsGlobal);
							break;
						case CG_INT:
						case CG_INT1:
							Uniform = new CCgUniform1i(Parameter, IsGlobal);
							break;
						case CG_INT2:
							Uniform = new CCgUniform2i(Parameter, IsGlobal);
							break;
						case CG_INT3:
							Uniform = new CCgUniform3i(Parameter, IsGlobal);
							break;
						case CG_INT4:
							Uniform = new CCgUniform4i(Parameter, IsGlobal);
							break;
						case CG_FLOAT4x4:
							Uniform = new CCgUniform4x4f(Parameter, IsGlobal);
							break;
						case CG_SAMPLER2D:
							Uniform = new CCgUniformSampler2D(Parameter, IsGlobal);
							break;
						}

						if(Uniform)
							UniformInfo.push_back(Uniform);
					}

					Parameter = cgGetNextParameter(Parameter);
				}
			}
		}
	}
}

}
}

#endif