1import re
2import math
3import random
4
5PREAMBLE = """
6# WARNING: This file is auto-generated. Do NOT modify it manually, but rather
7# modify the generating script file. Otherwise changes will be lost!
8"""[1:]
9
10class CaseGroup(object):
11	def __init__(self, name, description, children):
12		self.name			= name
13		self.description	= description
14		self.children		= children
15
16class ShaderCase(object):
17	def __init__(self):
18		pass
19
20g_processedCases = {}
21
22def indentTextBlock(text, indent):
23	indentStr = indent * "\t"
24	lines = text.split("\n")
25	lines = [indentStr + line for line in lines]
26	lines = [ ["", line][line.strip() != ""] for line in lines]
27	return "\n".join(lines)
28
29def writeCase(f, case, indent, prefix):
30	print "    %s" % (prefix + case.name)
31	if isinstance(case, CaseGroup):
32		f.write(indentTextBlock('group %s "%s"\n\n' % (case.name, case.description), indent))
33		for child in case.children:
34			writeCase(f, child, indent + 1, prefix + case.name + ".")
35		f.write(indentTextBlock("\nend # %s\n" % case.name, indent))
36	else:
37		# \todo [petri] Fix hack.
38		fullPath = prefix + case.name
39		assert (fullPath not in g_processedCases)
40		g_processedCases[fullPath] = None
41		f.write(indentTextBlock(str(case) + "\n", indent))
42
43def writeAllCases(fileName, caseList):
44	# Write all cases to file.
45	print "  %s.." % fileName
46	f = file(fileName, "wb")
47	f.write(PREAMBLE + "\n")
48	for case in caseList:
49		writeCase(f, case, 0, "")
50	f.close()
51
52	print "done! (%d cases written)" % len(g_processedCases)
53
54# Template operations.
55
56def genValues(inputs, outputs):
57	res = []
58	for (name, values) in inputs:
59		res.append("input %s = [ %s ];" % (name, " | ".join([str(v) for v in values]).lower()))
60	for (name, values) in outputs:
61		res.append("output %s = [ %s ];" % (name, " | ".join([str(v) for v in values]).lower()))
62	return ("\n".join(res))
63
64def fillTemplate(template, params):
65	s = template
66
67	for (key, value) in params.items():
68		m = re.search(r"^(\s*)\$\{\{%s\}\}$" % key, s, re.M)
69		if m is not None:
70			start = m.start(0)
71			end = m.end(0)
72			ws = m.group(1)
73			if value is not None:
74				repl = "\n".join(["%s%s" % (ws, line) for line in value.split("\n")])
75				s = s[:start] + repl + s[end:]
76			else:
77				s = s[:start] + s[end+1:] # drop the whole line
78		else:
79			s = s.replace("${{%s}}" % key, value)
80	return s
81
82# Return shuffled version of list
83def shuffled(lst):
84	tmp = lst[:]
85	random.shuffle(tmp)
86	return tmp
87
88def repeatToLength(lst, toLength):
89	return (toLength / len(lst)) * lst + lst[: toLength % len(lst)]
90
91# Helpers to convert a list of Scalar/Vec values into another type.
92
93def toFloat(lst):	return [Scalar(float(v.x)) for v in lst]
94def toInt(lst):		return [Scalar(int(v.x)) for v in lst]
95def toUint(lst):	return [Uint(int(v.x)) for v in lst]
96def toBool(lst):	return [Scalar(bool(v.x)) for v in lst]
97def toVec4(lst):	return [v.toFloat().toVec4() for v in lst]
98def toVec3(lst):	return [v.toFloat().toVec3() for v in lst]
99def toVec2(lst):	return [v.toFloat().toVec2() for v in lst]
100def toIVec4(lst):	return [v.toInt().toVec4() for v in lst]
101def toIVec3(lst):	return [v.toInt().toVec3() for v in lst]
102def toIVec2(lst):	return [v.toInt().toVec2() for v in lst]
103def toBVec4(lst):	return [v.toBool().toVec4() for v in lst]
104def toBVec3(lst):	return [v.toBool().toVec3() for v in lst]
105def toBVec2(lst):	return [v.toBool().toVec2() for v in lst]
106def toUVec4(lst):	return [v.toUint().toUVec4() for v in lst]
107def toUVec3(lst):	return [v.toUint().toUVec3() for v in lst]
108def toUVec2(lst):	return [v.toUint().toUVec2() for v in lst]
109def toMat2(lst):	return [v.toMat2() for v in lst]
110def toMat2x3(lst):	return [v.toMat2x3() for v in lst]
111def toMat2x4(lst):	return [v.toMat2x4() for v in lst]
112def toMat3x2(lst):	return [v.toMat3x2() for v in lst]
113def toMat3(lst):	return [v.toMat3() for v in lst]
114def toMat3x4(lst):	return [v.toMat3x4() for v in lst]
115def toMat4x2(lst):	return [v.toMat4x2() for v in lst]
116def toMat4x3(lst):	return [v.toMat4x3() for v in lst]
117def toMat4(lst):	return [v.toMat4() for v in lst]
118
119# Random value generation.
120
121class GenRandom(object):
122	def __init__(self):
123		pass
124
125	def uniformVec4(self, count, mn, mx):
126		ret = [Vec4(random.uniform(mn, mx), random.uniform(mn, mx), random.uniform(mn, mx), random.uniform(mn, mx)) for x in xrange(count)]
127		ret[0].x = mn
128		ret[1].x = mx
129		ret[2].x = (mn + mx) * 0.5
130		return ret
131
132	def uniformBVec4(self, count):
133		ret = [Vec4(random.random() >= 0.5, random.random() >= 0.5, random.random() >= 0.5, random.random() >= 0.5) for x in xrange(count)]
134		ret[0].x = True
135		ret[1].x = False
136		return ret
137
138#	def uniform(self,
139
140# Math operating on Scalar/Vector types.
141
142def glslSign(a):			return 0.0 if (a == 0) else +1.0 if (a > 0.0) else -1.0
143def glslMod(x, y):			return x - y*math.floor(x/y)
144def glslClamp(x, mn, mx):	return mn if (x < mn) else mx if (x > mx) else x
145
146class GenMath(object):
147	@staticmethod
148	def unary(func):	return lambda val: val.applyUnary(func)
149
150	@staticmethod
151	def binary(func):	return lambda a, b: (b.expandVec(a)).applyBinary(func, a.expandVec(b))
152
153	@staticmethod
154	def frac(val):		return val.applyUnary(lambda x: x - math.floor(x))
155
156	@staticmethod
157	def exp2(val):		return val.applyUnary(lambda x: math.pow(2.0, x))
158
159	@staticmethod
160	def log2(val):		return val.applyUnary(lambda x: math.log(x, 2.0))
161
162	@staticmethod
163	def rsq(val):		return val.applyUnary(lambda x: 1.0 / math.sqrt(x))
164
165	@staticmethod
166	def sign(val):		return val.applyUnary(glslSign)
167
168	@staticmethod
169	def isEqual(a, b):	return Scalar(a.isEqual(b))
170
171	@staticmethod
172	def isNotEqual(a, b):	return Scalar(not a.isEqual(b))
173
174	@staticmethod
175	def step(a, b):		return (b.expandVec(a)).applyBinary(lambda edge, x: [1.0, 0.0][x < edge], a.expandVec(b))
176
177	@staticmethod
178	def length(a):		return a.length()
179
180	@staticmethod
181	def distance(a, b):	return a.distance(b)
182
183	@staticmethod
184	def dot(a, b):		return a.dot(b)
185
186	@staticmethod
187	def cross(a, b):	return a.cross(b)
188
189	@staticmethod
190	def normalize(a):	return a.normalize()
191
192	@staticmethod
193	def boolAny(a):		return a.boolAny()
194
195	@staticmethod
196	def boolAll(a):		return a.boolAll()
197
198	@staticmethod
199	def boolNot(a):		return a.boolNot()
200
201	@staticmethod
202	def abs(a):			return a.abs()
203
204# ..
205
206class Scalar(object):
207	def __init__(self, x):
208		self.x = x
209
210	def applyUnary(self, func):			return Scalar(func(self.x))
211	def applyBinary(self, func, other):	return Scalar(func(self.x, other.x))
212
213	def isEqual(self, other):	assert isinstance(other, Scalar); return (self.x == other.x)
214
215	def expandVec(self, val):	return val
216	def toScalar(self):			return Scalar(self.x)
217	def toVec2(self):			return Vec2(self.x, self.x)
218	def toVec3(self):			return Vec3(self.x, self.x, self.x)
219	def toVec4(self):			return Vec4(self.x, self.x, self.x, self.x)
220	def toUVec2(self):			return UVec2(self.x, self.x)
221	def toUVec3(self):			return UVec3(self.x, self.x, self.x)
222	def toUVec4(self):			return UVec4(self.x, self.x, self.x, self.x)
223	def toMat2(self):			return Mat.fromScalar(2, 2, float(self.x))
224	def toMat2x3(self):			return Mat.fromScalar(2, 3, float(self.x))
225	def toMat2x4(self):			return Mat.fromScalar(2, 4, float(self.x))
226	def toMat3x2(self):			return Mat.fromScalar(3, 2, float(self.x))
227	def toMat3(self):			return Mat.fromScalar(3, 3, float(self.x))
228	def toMat3x4(self):			return Mat.fromScalar(3, 4, float(self.x))
229	def toMat4x2(self):			return Mat.fromScalar(4, 2, float(self.x))
230	def toMat4x3(self):			return Mat.fromScalar(4, 3, float(self.x))
231	def toMat4(self):			return Mat.fromScalar(4, 4, float(self.x))
232
233	def toFloat(self):			return Scalar(float(self.x))
234	def toInt(self):			return Scalar(int(self.x))
235	def toUint(self):			return Uint(int(self.x))
236	def toBool(self):			return Scalar(bool(self.x))
237
238	def getNumScalars(self):	return 1
239	def getScalars(self):		return [self.x]
240
241	def typeString(self):
242		if isinstance(self.x, bool):
243			return "bool"
244		elif isinstance(self.x, int):
245			return "int"
246		elif isinstance(self.x, float):
247			return "float"
248		else:
249			assert False
250
251	def vec4Swizzle(self):
252		return ""
253
254	def __str__(self):
255		return str(self.x).lower()
256
257	def __float__(self):
258		return float(self.x)
259
260	def length(self):
261		return Scalar(abs(self.x))
262
263	def distance(self, v):
264		assert isinstance(v, Scalar)
265		return Scalar(abs(self.x - v.x))
266
267	def dot(self, v):
268		assert isinstance(v, Scalar)
269		return Scalar(self.x * v.x)
270
271	def normalize(self):
272		return Scalar(glslSign(self.x))
273
274	def abs(self):
275		if isinstance(self.x, bool):
276			return Scalar(self.x)
277		else:
278			return Scalar(abs(self.x))
279
280	def __neg__(self):
281		return Scalar(-self.x)
282
283	def __add__(self, val):
284		assert isinstance(val, Scalar)
285		return Scalar(self.x + val.x)
286
287	def __sub__(self, val):
288		return self + (-val)
289
290	def __mul__(self, val):
291		if isinstance(val, Scalar):
292			return Scalar(self.x * val.x)
293		elif isinstance(val, Vec2):
294			return Vec2(self.x * val.x, self.x * val.y)
295		elif isinstance(val, Vec3):
296			return Vec3(self.x * val.x, self.x * val.y, self.x * val.z)
297		elif isinstance(val, Vec4):
298			return Vec4(self.x * val.x, self.x * val.y, self.x * val.z, self.x * val.w)
299		else:
300			assert False
301
302	def __div__(self, val):
303		if isinstance(val, Scalar):
304			return Scalar(self.x / val.x)
305		elif isinstance(val, Vec2):
306			return Vec2(self.x / val.x, self.x / val.y)
307		elif isinstance(val, Vec3):
308			return Vec3(self.x / val.x, self.x / val.y, self.x / val.z)
309		elif isinstance(val, Vec4):
310			return Vec4(self.x / val.x, self.x / val.y, self.x / val.z, self.x / val.w)
311		else:
312			assert False
313
314class Uint(Scalar):
315	def __init__(self, x):
316		assert x >= 0
317		self.x = x
318
319	def typeString(self):
320		return "uint"
321
322	def abs(self):
323		return Scalar.abs(self).toUint()
324
325	def __neg__(self):
326		return Scalar.__neg__(self).toUint()
327
328	def __add__(self, val):
329		return Scalar.__add__(self, val).toUint()
330
331	def __sub__(self, val):
332		return self + (-val)
333
334	def __mul__(self, val):
335		return Scalar.__mul__(self, val).toUint()
336
337	def __div__(self, val):
338		return Scalar.__div__(self, val).toUint()
339
340class Vec(object):
341	@staticmethod
342	def fromScalarList(lst):
343		assert (len(lst) >= 1 and len(lst) <= 4)
344		if (len(lst) == 1):		return Scalar(lst[0])
345		elif (len(lst) == 2):	return Vec2(lst[0], lst[1])
346		elif (len(lst) == 3):	return Vec3(lst[0], lst[1], lst[2])
347		else:					return Vec4(lst[0], lst[1], lst[2], lst[3])
348
349	def isEqual(self, other):
350		assert isinstance(other, Vec);
351		return (self.getScalars() == other.getScalars())
352
353	def length(self):
354		return Scalar(math.sqrt(self.dot(self).x))
355
356	def normalize(self):
357		return self * Scalar(1.0 / self.length().x)
358
359	def swizzle(self, indexList):
360		inScalars = self.getScalars()
361		outScalars = map(lambda ndx: inScalars[ndx], indexList)
362		return Vec.fromScalarList(outScalars)
363
364	def __init__(self):
365		pass
366
367class Vec2(Vec):
368	def __init__(self, x, y):
369		assert(x.__class__ == y.__class__)
370		self.x = x
371		self.y = y
372
373	def applyUnary(self, func):			return Vec2(func(self.x), func(self.y))
374	def applyBinary(self, func, other):	return Vec2(func(self.x, other.x), func(self.y, other.y))
375
376	def expandVec(self, val):	return val.toVec2()
377	def toScalar(self):			return Scalar(self.x)
378	def toVec2(self):			return Vec2(self.x, self.y)
379	def toVec3(self):			return Vec3(self.x, self.y, 0.0)
380	def toVec4(self):			return Vec4(self.x, self.y, 0.0, 0.0)
381	def toUVec2(self):			return UVec2(self.x, self.y)
382	def toUVec3(self):			return UVec3(self.x, self.y, 0.0)
383	def toUVec4(self):			return UVec4(self.x, self.y, 0.0, 0.0)
384	def toMat2(self):			return Mat2(float(self.x), 0.0, 0.0, float(self.y));
385
386	def toFloat(self):			return Vec2(float(self.x), float(self.y))
387	def toInt(self):			return Vec2(int(self.x), int(self.y))
388	def toUint(self):			return UVec2(int(self.x), int(self.y))
389	def toBool(self):			return Vec2(bool(self.x), bool(self.y))
390
391	def getNumScalars(self):	return 2
392	def getScalars(self):		return [self.x, self.y]
393
394	def typeString(self):
395		if isinstance(self.x, bool):
396			return "bvec2"
397		elif isinstance(self.x, int):
398			return "ivec2"
399		elif isinstance(self.x, float):
400			return "vec2"
401		else:
402			assert False
403
404	def vec4Swizzle(self):
405		return ".xyxy"
406
407	def __str__(self):
408		if isinstance(self.x, bool):
409			return "bvec2(%s, %s)" % (str(self.x).lower(), str(self.y).lower())
410		elif isinstance(self.x, int):
411			return "ivec2(%i, %i)" % (self.x, self.y)
412		elif isinstance(self.x, float):
413			return "vec2(%s, %s)" % (self.x, self.y)
414		else:
415			assert False
416
417	def distance(self, v):
418		assert isinstance(v, Vec2)
419		return (self - v).length()
420
421	def dot(self, v):
422		assert isinstance(v, Vec2)
423		return Scalar(self.x*v.x + self.y*v.y)
424
425	def abs(self):
426		if isinstance(self.x, bool):
427			return Vec2(self.x, self.y)
428		else:
429			return Vec2(abs(self.x), abs(self.y))
430
431	def __neg__(self):
432		return Vec2(-self.x, -self.y)
433
434	def __add__(self, val):
435		if isinstance(val, Scalar):
436			return Vec2(self.x + val, self.y + val)
437		elif isinstance(val, Vec2):
438			return Vec2(self.x + val.x, self.y + val.y)
439		else:
440			assert False
441
442	def __sub__(self, val):
443		return self + (-val)
444
445	def __mul__(self, val):
446		if isinstance(val, Scalar):
447			val = val.toVec2()
448		assert isinstance(val, Vec2)
449		return Vec2(self.x * val.x, self.y * val.y)
450
451	def __div__(self, val):
452		if isinstance(val, Scalar):
453			return Vec2(self.x / val.x, self.y / val.x)
454		else:
455			assert isinstance(val, Vec2)
456			return Vec2(self.x / val.x, self.y / val.y)
457
458	def boolAny(self):	return Scalar(self.x or self.y)
459	def boolAll(self):	return Scalar(self.x and self.y)
460	def boolNot(self):	return Vec2(not self.x, not self.y)
461
462class UVec2(Vec2):
463	def __init__(self, x, y):
464		assert isinstance(x, int) and isinstance(y, int)
465		assert x >= 0 and y >= 0
466		Vec2.__init__(self, x, y)
467
468	def typeString(self):
469		return "uvec2"
470
471	def __str__(self):
472		return "uvec2(%i, %i)" % (self.x, self.y)
473
474	def abs(self):
475		return Vec2.abs(self).toUint()
476
477class Vec3(Vec):
478	def __init__(self, x, y, z):
479		assert((x.__class__ == y.__class__) and (x.__class__ == z.__class__))
480		self.x = x
481		self.y = y
482		self.z = z
483
484	def applyUnary(self, func):			return Vec3(func(self.x), func(self.y), func(self.z))
485	def applyBinary(self, func, other):	return Vec3(func(self.x, other.x), func(self.y, other.y), func(self.z, other.z))
486
487	def expandVec(self, val):	return val.toVec3()
488	def toScalar(self):			return Scalar(self.x)
489	def toVec2(self):			return Vec2(self.x, self.y)
490	def toVec3(self):			return Vec3(self.x, self.y, self.z)
491	def toVec4(self):			return Vec4(self.x, self.y, self.z, 0.0)
492	def toUVec2(self):			return UVec2(self.x, self.y)
493	def toUVec3(self):			return UVec3(self.x, self.y, self.z)
494	def toUVec4(self):			return UVec4(self.x, self.y, self.z, 0.0)
495	def toMat3(self):			return Mat3(float(self.x), 0.0, 0.0,  0.0, float(self.y), 0.0,  0.0, 0.0, float(self.z));
496
497	def toFloat(self):			return Vec3(float(self.x), float(self.y), float(self.z))
498	def toInt(self):			return Vec3(int(self.x), int(self.y), int(self.z))
499	def toUint(self):			return UVec3(int(self.x), int(self.y), int(self.z))
500	def toBool(self):			return Vec3(bool(self.x), bool(self.y), bool(self.z))
501
502	def getNumScalars(self):	return 3
503	def getScalars(self):		return [self.x, self.y, self.z]
504
505	def typeString(self):
506		if isinstance(self.x, bool):
507			return "bvec3"
508		elif isinstance(self.x, int):
509			return "ivec3"
510		elif isinstance(self.x, float):
511			return "vec3"
512		else:
513			assert False
514
515	def vec4Swizzle(self):
516		return ".xyzx"
517
518	def __str__(self):
519		if isinstance(self.x, bool):
520			return "bvec3(%s, %s, %s)" % (str(self.x).lower(), str(self.y).lower(), str(self.z).lower())
521		elif isinstance(self.x, int):
522			return "ivec3(%i, %i, %i)" % (self.x, self.y, self.z)
523		elif isinstance(self.x, float):
524			return "vec3(%s, %s, %s)" % (self.x, self.y, self.z)
525		else:
526			assert False
527
528	def distance(self, v):
529		assert isinstance(v, Vec3)
530		return (self - v).length()
531
532	def dot(self, v):
533		assert isinstance(v, Vec3)
534		return Scalar(self.x*v.x + self.y*v.y + self.z*v.z)
535
536	def cross(self, v):
537		assert isinstance(v, Vec3)
538		return Vec3(self.y*v.z - v.y*self.z,
539					self.z*v.x - v.z*self.x,
540					self.x*v.y - v.x*self.y)
541
542	def abs(self):
543		if isinstance(self.x, bool):
544			return Vec3(self.x, self.y, self.z)
545		else:
546			return Vec3(abs(self.x), abs(self.y), abs(self.z))
547
548	def __neg__(self):
549		return Vec3(-self.x, -self.y, -self.z)
550
551	def __add__(self, val):
552		if isinstance(val, Scalar):
553			return Vec3(self.x + val, self.y + val)
554		elif isinstance(val, Vec3):
555			return Vec3(self.x + val.x, self.y + val.y, self.z + val.z)
556		else:
557			assert False
558
559	def __sub__(self, val):
560		return self + (-val)
561
562	def __mul__(self, val):
563		if isinstance(val, Scalar):
564			val = val.toVec3()
565		assert isinstance(val, Vec3)
566		return Vec3(self.x * val.x, self.y * val.y, self.z * val.z)
567
568	def __div__(self, val):
569		if isinstance(val, Scalar):
570			return Vec3(self.x / val.x, self.y / val.x, self.z / val.x)
571		elif isinstance(val, Vec3):
572			return Vec3(self.x / val.x, self.y / val.y, self.z / val.z)
573		else:
574			assert False
575
576	def boolAny(self):	return Scalar(self.x or self.y or self.z)
577	def boolAll(self):	return Scalar(self.x and self.y and self.z)
578	def boolNot(self):	return Vec3(not self.x, not self.y, not self.z)
579
580class UVec3(Vec3):
581	def __init__(self, x, y, z):
582		assert isinstance(x, int) and isinstance(y, int) and isinstance(z, int)
583		assert x >= 0 and y >= 0 and z >= 0
584		Vec3.__init__(self, x, y, z)
585
586	def typeString(self):
587		return "uvec3"
588
589	def __str__(self):
590		return "uvec3(%i, %i, %i)" % (self.x, self.y, self.z)
591
592	def abs(self):
593		return Vec3.abs(self).toUint()
594
595class Vec4(Vec):
596	def __init__(self, x, y, z, w):
597		assert((x.__class__ == y.__class__) and (x.__class__ == z.__class__) and (x.__class__ == w.__class__))
598		self.x = x
599		self.y = y
600		self.z = z
601		self.w = w
602
603	def applyUnary(self, func):			return Vec4(func(self.x), func(self.y), func(self.z), func(self.w))
604	def applyBinary(self, func, other):	return Vec4(func(self.x, other.x), func(self.y, other.y), func(self.z, other.z), func(self.w, other.w))
605
606	def expandVec(self, val):	return val.toVec4()
607	def toScalar(self):			return Scalar(self.x)
608	def toVec2(self):			return Vec2(self.x, self.y)
609	def toVec3(self):			return Vec3(self.x, self.y, self.z)
610	def toVec4(self):			return Vec4(self.x, self.y, self.z, self.w)
611	def toUVec2(self):			return UVec2(self.x, self.y)
612	def toUVec3(self):			return UVec3(self.x, self.y, self.z)
613	def toUVec4(self):			return UVec4(self.x, self.y, self.z, self.w)
614	def toMat2(self):			return Mat2(float(self.x), float(self.y), float(self.z), float(self.w))
615	def toMat4(self):			return Mat4(float(self.x), 0.0, 0.0, 0.0,  0.0, float(self.y), 0.0, 0.0,  0.0, 0.0, float(self.z), 0.0,  0.0, 0.0, 0.0, float(self.w));
616
617	def toFloat(self):			return Vec4(float(self.x), float(self.y), float(self.z), float(self.w))
618	def toInt(self):			return Vec4(int(self.x), int(self.y), int(self.z), int(self.w))
619	def toUint(self):			return UVec4(int(self.x), int(self.y), int(self.z), int(self.w))
620	def toBool(self):			return Vec4(bool(self.x), bool(self.y), bool(self.z), bool(self.w))
621
622	def getNumScalars(self):	return 4
623	def getScalars(self):		return [self.x, self.y, self.z, self.w]
624
625	def typeString(self):
626		if isinstance(self.x, bool):
627			return "bvec4"
628		elif isinstance(self.x, int):
629			return "ivec4"
630		elif isinstance(self.x, float):
631			return "vec4"
632		else:
633			assert False
634
635	def vec4Swizzle(self):
636		return ""
637
638	def __str__(self):
639		if isinstance(self.x, bool):
640			return "bvec4(%s, %s, %s, %s)" % (str(self.x).lower(), str(self.y).lower(), str(self.z).lower(), str(self.w).lower())
641		elif isinstance(self.x, int):
642			return "ivec4(%i, %i, %i, %i)" % (self.x, self.y, self.z, self.w)
643		elif isinstance(self.x, float):
644			return "vec4(%s, %s, %s, %s)" % (self.x, self.y, self.z, self.w)
645		else:
646			assert False
647
648	def distance(self, v):
649		assert isinstance(v, Vec4)
650		return (self - v).length()
651
652	def dot(self, v):
653		assert isinstance(v, Vec4)
654		return Scalar(self.x*v.x + self.y*v.y + self.z*v.z + self.w*v.w)
655
656	def abs(self):
657		if isinstance(self.x, bool):
658			return Vec4(self.x, self.y, self.z, self.w)
659		else:
660			return Vec4(abs(self.x), abs(self.y), abs(self.z), abs(self.w))
661
662	def __neg__(self):
663		return Vec4(-self.x, -self.y, -self.z, -self.w)
664
665	def __add__(self, val):
666		if isinstance(val, Scalar):
667			return Vec3(self.x + val, self.y + val)
668		elif isinstance(val, Vec4):
669			return Vec4(self.x + val.x, self.y + val.y, self.z + val.z, self.w + val.w)
670		else:
671			assert False
672
673	def __sub__(self, val):
674		return self + (-val)
675
676	def __mul__(self, val):
677		if isinstance(val, Scalar):
678			val = val.toVec4()
679		assert isinstance(val, Vec4)
680		return Vec4(self.x * val.x, self.y * val.y, self.z * val.z, self.w * val.w)
681
682	def __div__(self, val):
683		if isinstance(val, Scalar):
684			return Vec4(self.x / val.x, self.y / val.x, self.z / val.x, self.w / val.x)
685		elif isinstance(val, Vec4):
686			return Vec4(self.x / val.x, self.y / val.y, self.z / val.z, self.w / val.w)
687		else:
688			assert False
689
690	def boolAny(self):	return Scalar(self.x or self.y or self.z or self.w)
691	def boolAll(self):	return Scalar(self.x and self.y and self.z and self.w)
692	def boolNot(self):	return Vec4(not self.x, not self.y, not self.z, not self.w)
693
694class UVec4(Vec4):
695	def __init__(self, x, y, z, w):
696		assert isinstance(x, int) and isinstance(y, int) and isinstance(z, int) and isinstance(w, int)
697		assert x >= 0 and y >= 0 and z >= 0 and w >= 0
698		Vec4.__init__(self, x, y, z, w)
699
700	def typeString(self):
701		return "uvec4"
702
703	def __str__(self):
704		return "uvec4(%i, %i, %i, %i)" % (self.x, self.y, self.z, self.w)
705
706	def abs(self):
707		return Vec4.abs(self).toUint()
708
709# \note Column-major storage.
710class Mat(object):
711	def __init__ (self, numCols, numRows, scalars):
712		assert len(scalars) == numRows*numCols
713		self.numCols	= numCols
714		self.numRows	= numRows
715		self.scalars	= scalars
716
717	@staticmethod
718	def fromScalar (numCols, numRows, scalar):
719		scalars = []
720		for col in range(0, numCols):
721			for row in range(0, numRows):
722				scalars.append(scalar if col == row else 0.0)
723		return Mat(numCols, numRows, scalars)
724
725	@staticmethod
726	def identity (numCols, numRows):
727		return Mat.fromScalar(numCols, numRows, 1.0)
728
729	def get (self, colNdx, rowNdx):
730		assert 0 <= colNdx and colNdx < self.numCols
731		assert 0 <= rowNdx and rowNdx < self.numRows
732		return self.scalars[colNdx*self.numRows + rowNdx]
733
734	def set (self, colNdx, rowNdx, scalar):
735		assert 0 <= colNdx and colNdx < self.numCols
736		assert 0 <= rowNdx and rowNdx < self.numRows
737		self.scalars[colNdx*self.numRows + rowNdx] = scalar
738
739	def toMatrix (self, numCols, numRows):
740		res = Mat.identity(numCols, numRows)
741		for col in range(0, min(self.numCols, numCols)):
742			for row in range(0, min(self.numRows, numRows)):
743				res.set(col, row, self.get(col, row))
744		return res
745
746	def toMat2 (self):		return self.toMatrix(2, 2)
747	def toMat2x3 (self):	return self.toMatrix(2, 3)
748	def toMat2x4 (self):	return self.toMatrix(2, 4)
749	def toMat3x2 (self):	return self.toMatrix(3, 2)
750	def toMat3 (self):		return self.toMatrix(3, 3)
751	def toMat3x4 (self):	return self.toMatrix(3, 4)
752	def toMat4x2 (self):	return self.toMatrix(4, 2)
753	def toMat4x3 (self):	return self.toMatrix(4, 3)
754	def toMat4 (self):		return self.toMatrix(4, 4)
755
756	def typeString(self):
757		if self.numRows == self.numCols:
758			return "mat%d" % self.numRows
759		else:
760			return "mat%dx%d" % (self.numCols, self.numRows)
761
762	def __str__(self):
763		return "%s(%s)" % (self.typeString(), ", ".join(["%s" % s for s in self.scalars]))
764
765	def isTypeEqual (self, other):
766		return isinstance(other, Mat) and self.numRows == other.numRows and self.numCols == other.numCols
767
768	def isEqual(self, other):
769		assert self.isTypeEqual(other)
770		return (self.scalars == other.scalars)
771
772	def compMul(self, val):
773		assert self.isTypeEqual(val)
774		return Mat(self.numRows, self.numCols, [self.scalars(i) * val.scalars(i) for i in range(self.numRows*self.numCols)])
775
776class Mat2(Mat):
777	def __init__(self, m00, m01, m10, m11):
778		Mat.__init__(self, 2, 2, [m00, m10, m01, m11])
779
780class Mat3(Mat):
781	def __init__(self, m00, m01, m02, m10, m11, m12, m20, m21, m22):
782		Mat.__init__(self, 3, 3, [m00, m10, m20,
783								  m01, m11, m21,
784								  m02, m12, m22])
785
786class Mat4(Mat):
787	def __init__(self, m00, m01, m02, m03, m10, m11, m12, m13, m20, m21, m22, m23, m30, m31, m32, m33):
788		Mat.__init__(self, 4, 4, [m00, m10, m20, m30,
789								  m01, m11, m21, m31,
790								  m02, m12, m22, m32,
791								  m03, m13, m23, m33])
792