1import sys
2import random
3import operator
4import itertools
5
6from genutil import *
7
8random.seed(1234567)
9indices = xrange(sys.maxint)
10
11# Constructors:
12#
13# - scalars types
14#   * int <-> float <-> bool (also float(float) etc.)
15#   * to bool: zero means false, others true
16#   * from bool: false==0, true==1
17#   * \todo [petri] float<->int rounding rules?
18# - scalar type from vector
19#   * choose the first component
20# - vectors & matrices
21#   * vector from scalar: broadcast to all components
22#   * matrix from scalar: broadcast scalar to diagonal, other components zero
23#   * vector from vector: copy existing components
24#     + illegal: vector from smaller vector
25#   * mat from mat: copy existing components, other components from identity matrix
26#   * from components: consumed by-component in column-major order, must have same
27#     number of components,
28#     + note: vec4(mat2) valid
29#     \todo [petri] Implement!
30# - notes:
31#   * type conversions are always allowed: mat3(ivec3, bvec3, bool, int, float) is valid!
32#
33# Accessors:
34#
35# - vector components
36#   * .xyzw, .rgba, .stpq
37#   * illegal to mix
38#   * now allowed for scalar types
39#   * legal to chain: vec4.rgba.xyzw.stpq
40#   * illegal to select more than 4 components
41#   * array indexing with [] operator
42#   * can also write!
43# - matrix columns
44#   * [] accessor
45#   * note: mat4[0].x = 1.0; vs mat4[0][0] = 1.0; ??
46#   * out-of-bounds accesses
47#
48# \todo [petri] Accessors!
49#
50# Spec issues:
51#
52# - constructing larger vector from smaller: vec3(vec2) ?
53# - base type and size conversion at same time: vec4(bool), int(vec3) allowed?
54
55def combineVec(comps):
56	res = []
57	for ndx in range(len(comps[0])):
58#		for x in comps:
59#			print x[ndx].toFloat().getScalars() ,
60		scalars = reduce(operator.add, [x[ndx].toFloat().getScalars() for x in comps])
61#		print "->", scalars
62		res.append(Vec.fromScalarList(scalars))
63	return res
64
65def combineIVec(comps):
66	res = []
67	for ndx in range(len(comps[0])):
68		res.append(Vec.fromScalarList(reduce(operator.add, [x[ndx].toInt().getScalars() for x in comps])))
69	return res
70
71def combineUVec(comps):
72	return [x.toUint() for x in combineIVec(comps)]
73
74def combineBVec(comps):
75	res = []
76	for ndx in range(len(comps[0])):
77		res.append(Vec.fromScalarList(reduce(operator.add, [x[ndx].toBool().getScalars() for x in comps])))
78	return res
79
80def combineMat(numCols, numRows, comps):
81	res = []
82	for ndx in range(len(comps[0])):
83		scalars = reduce(operator.add, [x[ndx].toFloat().getScalars() for x in comps])
84		res.append(Mat(numCols, numRows, scalars))
85	return res
86
87def combineMat2(comps):		return combineMat(2, 2, comps)
88def combineMat2x3(comps):	return combineMat(2, 3, comps)
89def combineMat2x4(comps):	return combineMat(2, 4, comps)
90def combineMat3x2(comps):	return combineMat(3, 2, comps)
91def combineMat3(comps):		return combineMat(3, 3, comps)
92def combineMat3x4(comps):	return combineMat(3, 4, comps)
93def combineMat4x2(comps):	return combineMat(4, 2, comps)
94def combineMat4x3(comps):	return combineMat(4, 3, comps)
95def combineMat4(comps):		return combineMat(4, 4, comps)
96
97# 0 \+ [f*f for f in lst]
98# r = 0 \+ [f in lst -> f*f]
99# r = 0 \+ lst
100
101# Templates.
102
103s_simpleCaseTemplate = """
104case ${{NAME}}
105	version 300 es
106	values
107	{
108		${{VALUES}}
109	}
110
111	both ""
112		#version 300 es
113		precision mediump float;
114		precision mediump int;
115
116		${DECLARATIONS}
117
118		void main()
119		{
120			${SETUP}
121			${{OP}}
122			${OUTPUT}
123		}
124	""
125end
126"""[1:]
127
128s_simpleIllegalCaseTemplate = """
129case ${{NAME}}
130	version 300 es
131	expect compile_fail
132	values {}
133
134	both ""
135		#version 300 es
136		precision mediump float;
137		precision mediump int;
138
139		${DECLARATIONS}
140
141		void main()
142		{
143			${SETUP}
144			${{OP}}
145			${OUTPUT}
146		}
147	""
148end
149"""[1:]
150
151class SimpleCase(ShaderCase):
152	def __init__(self, name, inputs, outputs, op):
153		self.name		= name
154		self.inputs		= inputs
155		self.outputs	= outputs
156		self.op			= op
157
158	def __str__(self):
159		params = {
160			"NAME":		self.name,
161			"VALUES":	genValues(self.inputs, self.outputs),
162			"OP":		self.op
163		}
164		return fillTemplate(s_simpleCaseTemplate, params)
165
166class ConversionCase(ShaderCase):
167	def __init__(self, inValues, convFunc):
168		outValues = convFunc(inValues)
169		inType	= inValues[0].typeString()
170		outType	= outValues[0].typeString()
171		self.name		= "%s_to_%s" % (inType, outType)
172		self.op			= "out0 = %s(in0);" % outType
173		self.inputs		= [("%s in0" % inType, inValues)]
174		self.outputs	= [("%s out0" % outType, outValues)]
175
176	def __str__(self):
177		params = {
178			"NAME":		self.name,
179			"VALUES":	genValues(self.inputs, self.outputs),
180			"OP":		self.op
181		}
182		return fillTemplate(s_simpleCaseTemplate, params)
183
184class IllegalConversionCase(ShaderCase):
185	def __init__(self, inValue, outValue):
186		inType	= inValue.typeString()
187		outType	= outValue.typeString()
188		self.name		= "%s_to_%s" % (inType, outType)
189		self.op			= "%s in0 = %s;\n%s out0 = %s(in0);" % (inType, str(inValue), outType, outType)
190		self.inType		= inType
191		self.outType	= outType
192
193	def __str__(self):
194		params = {
195			"NAME":		self.name,
196			"OP":		self.op
197		}
198		return fillTemplate(s_simpleIllegalCaseTemplate, params)
199
200class CombineCase(ShaderCase):
201	def __init__(self, inComps, combFunc):
202		self.inComps	= inComps
203		self.outValues	= combFunc(inComps)
204		self.outType	= self.outValues[0].typeString()
205		inTypes = [values[0].typeString() for values in inComps]
206		self.name		= "%s_to_%s" % ("_".join(inTypes), self.outType)
207		self.inputs		= [("%s in%s" % (comp[0].typeString(), ndx), comp) for (comp, ndx) in zip(inComps, indices)]
208		self.outputs	= [("%s out0" % self.outType, self.outValues)]
209		self.op			= "out0 = %s(%s);" % (self.outType, ", ".join(["in%d" % x for x in range(len(inComps))]))
210
211	def __str__(self):
212		params = {
213			"NAME":		self.name,
214			"VALUES":	genValues(self.inputs, self.outputs),
215			"OP":		self.op
216		}
217		return fillTemplate(s_simpleCaseTemplate, params)
218
219# CASE DECLARATIONS
220
221def toPos (value):
222	if isinstance(value, list):
223		return [toPos(x) for x in value]
224	else:
225		return GenMath.abs(value)
226
227inFloat	= [Scalar(x) for x in [0.0, 1.0, 2.0, 3.5, -0.5, -8.25, -20.125, 36.8125]]
228inInt	= [Scalar(x) for x in [0, 1, 2, 5, 8, 11, -12, -66, -192, 255]]
229inUint	= [Uint(x) for x in [0, 2, 3, 8, 9, 12, 10, 45, 193, 255]]
230inBool	= [Scalar(x) for x in [True, False]]
231
232inVec4	= [Vec4(0.0, 0.5, 0.75, 0.825), Vec4(1.0, 1.25, 1.125, 1.75),
233		   Vec4(-0.5, -2.25, -4.875, 9.0), Vec4(-32.0, 64.0, -51.0, 24.0),
234		   Vec4(-0.75, -1.0/31.0, 1.0/19.0, 1.0/4.0)]
235inVec3	= toVec3(inVec4)
236inVec2	= toVec2(inVec4)
237inIVec4	= toIVec4(inVec4)
238inIVec3	= toIVec3(inVec4)
239inIVec2	= toIVec2(inVec4)
240inBVec4	= [Vec4(True, False, False, True), Vec4(False, False, False, True), Vec4(False, True, False, False), Vec4(True, True, True, True), Vec4(False, False, False, False)]
241inBVec3	= toBVec3(inBVec4)
242inBVec2	= toBVec2(inBVec4)
243inUVec4	= toUVec4(toPos(inVec4))
244inUVec3 = toUVec3(toPos(inVec3))
245inUVec2 = toUVec2(toPos(inVec2))
246
247# \todo [petri] Enable large values when epsilon adapts to the values.
248inMat4	= [Mat4(1.0, 0.0, 0.0, 0.0,  0.0, 1.0, 0.0, 0.0,  0.0, 0.0, 1.0, 0.0,  0.0, 0.0, 0.0, 1.0),
249		   Mat4(6.5, 12.5, -0.75, 9.975,  32.0, 1.0/48.0, -8.425, -6.542,  1.0/8.0, 1.0/16.0, 1.0/32.0, 1.0/64.0,  -6.725, -0.5, -0.0125, 9.975),
250		   #Mat4(128.0, 256.0, -512.0, -1024.0,  2048.0, -4096.0, 8192.0, -8192.0,  192.0, -384.0, 768.0, -1536.0,  8192.0, -8192.0, 6144.0, -6144.0)
251		   ]
252inMat2	= [Mat2(1.0, 0.0,  0.0, 1.0),
253		   Mat2(6.5, 12.5,  -0.75, 9.975),
254		   Mat2(6.5, 12.5,  -0.75, 9.975),
255		   Mat2(8.0, 16.0,  -24.0, -16.0),
256		   Mat2(1.0/8.0, 1.0/16.0,  1.0/32.0, 1.0/64.0),
257		   Mat2(-18.725, -0.5,  -0.0125, 19.975),
258		   #Mat2(128.0, -4096.0,  192.0, -1536.0),
259		   #Mat2(-1536.0, 8192.0,  6144.0, -6144.0)
260		   ]
261
262inMat4x3	= toMat4x3(inMat4)
263inMat4x2	= toMat4x2(inMat4)
264inMat3x4	= toMat3x4(inMat4)
265inMat3		= toMat3(inMat4)
266inMat3x2	= toMat3x2(inMat4)
267inMat2x4	= toMat2x4(inMat4)
268inMat2x3	= toMat2x3(inMat4)
269
270def genConversionCases(inValueList, convFuncList):
271	combinations = list(itertools.product(inValueList, convFuncList))
272	return [ConversionCase(inValues, convFunc) for (inValues, convFunc) in combinations]
273
274def genIllegalConversionCases(inValueList, outValueList):
275	inValues	= [x[0] for x in inValueList]
276	outValues	= [x[0] for x in outValueList]
277	combinations = list(itertools.product(inValues, outValues))
278	return [IllegalConversionCase(inVal, outVal) for (inVal, outVal) in combinations]
279
280def shuffleSubLists(outer):
281	return [shuffled(inner) for inner in outer]
282
283# Generate all combinations of CombineCases.
284# inTupleList	a list of tuples of value-lists
285# combFuncList	a list of comb* functions to combine
286def genComponentCases(inCompLists, combFuncList):
287	res = []
288	for comps in inCompLists:
289		maxLen = reduce(max, [len(values) for values in comps])
290		comps = [repeatToLength(values, maxLen) for values in comps]
291		comps = [shuffled(values) for values in comps]
292		for combFunc in combFuncList:
293			res += [CombineCase(comps, combFunc)]
294	return res
295
296allConversionCases = []
297
298# Scalar-to-scalar conversions.
299allConversionCases.append(CaseGroup("scalar_to_scalar", "Scalar to Scalar Conversions",
300	genConversionCases([inFloat, inInt, inUint, inBool], [toFloat, toInt, toBool]) +\
301	genConversionCases([toPos(inFloat), toPos(inInt), inUint, inBool], [toUint])))
302
303# Scalar-to-vector conversions.
304allConversionCases.append(CaseGroup("scalar_to_vector", "Scalar to Vector Conversions",
305	genConversionCases([inFloat, inInt, inUint, inBool], [toVec2, toVec3, toVec4, toIVec2, toIVec3, toIVec4, toBVec2, toBVec3, toBVec4]) +\
306	genConversionCases([toPos(inFloat), toPos(inInt), inUint, inBool], [toUVec2, toUVec3, toUVec4])))
307
308# Vector-to-scalar conversions.
309allConversionCases.append(CaseGroup("vector_to_scalar", "Vector to Scalar Conversions",
310	genConversionCases([inVec2, inVec3, inVec4, inIVec2, inIVec3, inIVec4, inUVec2, inUVec3, inUVec4, inBVec2, inBVec3, inBVec4], [toFloat, toInt, toBool]) +\
311	genConversionCases([toPos(inVec2), toPos(inVec3), toPos(inVec4), toPos(inIVec2), toPos(inIVec3), toPos(inIVec4), inUVec2, inUVec3, inUVec4, inBVec2, inBVec3, inBVec4], [toUint])))
312
313# Illegal vector-to-vector conversions (to longer vec).
314allConversionCases.append(CaseGroup("vector_illegal", "Illegal Vector Conversions",
315	genIllegalConversionCases([inVec2, inIVec2, inUVec2, inBVec2], [inVec3, inIVec3, inUVec3, inBVec3, inVec4, inIVec4, inUVec4, inBVec4]) +\
316	genIllegalConversionCases([inVec3, inIVec3, inUVec3, inBVec3], [inVec4, inIVec4, inUVec4, inBVec4])))
317
318# Vector-to-vector conversions (type conversions, downcasts).
319allConversionCases.append(CaseGroup("vector_to_vector", "Vector to Vector Conversions",
320	genConversionCases([inVec4, inIVec4, inUVec4, inBVec4], [toVec4, toVec3, toVec2, toIVec4, toIVec3, toIVec2, toBVec4, toBVec3, toBVec2]) +\
321	genConversionCases([toPos(inVec4), toPos(inIVec4), inUVec4, inBVec4], [toUVec4, toUVec3, toUVec2]) +\
322	genConversionCases([inVec3, inIVec3, inUVec3, inBVec3], [toVec3, toVec2, toIVec3, toIVec2, toBVec3, toBVec2]) +\
323	genConversionCases([toPos(inVec3), toPos(inIVec3), inUVec3, inBVec3], [toUVec3, toUVec2]) +\
324	genConversionCases([inVec2, inIVec2, inUVec2, inBVec2], [toVec2, toIVec2, toBVec2]) +\
325	genConversionCases([toPos(inVec2), toPos(inIVec2), inUVec2, inBVec2], [toUVec2])))
326
327# Scalar-to-matrix.
328allConversionCases.append(CaseGroup("scalar_to_matrix", "Scalar to Matrix Conversions",
329	genConversionCases([inFloat, inInt, inUint, inBool], [toMat4, toMat4x3, toMat4x2, toMat3x4, toMat3, toMat3x2, toMat2x4, toMat2x3, toMat2])))
330
331# Vector-to-matrix.
332#allConversionCases += genConversionCases([inVec4, inIVec4, inBVec4], [toMat4])
333#allConversionCases += genConversionCases([inVec3, inIVec3, inBVec3], [toMat3])
334#allConversionCases += genConversionCases([inVec2, inIVec2, inBVec2], [toMat2])
335
336# Matrix-to-matrix.
337allConversionCases.append(CaseGroup("matrix_to_matrix", "Matrix to Matrix Conversions",
338	genConversionCases([inMat4, inMat4x3, inMat4x2, inMat3x4, inMat3, inMat3x2, inMat2x4, inMat2x3, inMat2], [toMat4, toMat4x3, toMat4x2, toMat3x4, toMat3, toMat3x2, toMat2x4, toMat2x3, toMat2])))
339
340# Vector-from-components, matrix-from-components.
341in2Comp 	= [[inFloat, inFloat], [inInt, inInt], [inUint, inUint], [inBool, inBool], [inFloat, inInt], [inFloat, inBool], [inInt, inBool], [inInt, inUint], [inUint, inFloat]]
342in3Comp 	= [[inFloat, inFloat, inFloat], [inInt, inInt, inInt], [inUint, inUint, inUint], [inBool, inBool, inBool], [inBool, inFloat, inInt], [inVec2, inBool], [inBVec2, inFloat], [inBVec2, inInt], [inBool, inIVec2], [inFloat, inUVec2]]
343in4Comp 	= [[inVec2, inVec2], [inBVec2, inBVec2], [inFloat, inFloat, inFloat, inFloat], [inInt, inInt, inInt, inInt], [inUint, inUint, inUint, inUint], [inBool, inBool, inBool, inBool], [inBool, inFloat, inInt, inBool], [inVec2, inIVec2], [inVec2, inBVec2], [inBVec3, inFloat], [inVec3, inFloat], [inInt, inIVec2, inInt], [inBool, inFloat, inIVec2], [inFloat, inUVec3], [inInt, inUVec2, inBool]]
344in6Comp 	= [[inVec3, inVec3], [inBVec3, inBVec3], [inFloat, inFloat, inFloat, inFloat, inFloat, inFloat], [inInt, inInt, inInt, inInt, inInt, inInt], [inBool, inBool, inBool, inBool, inBool, inBool], [inBool, inFloat, inInt, inBool, inFloat, inInt], [inVec3, inIVec3], [inVec2, inBVec4], [inBVec3, inFloat, inIVec2], [inVec3, inFloat, inBVec2]]
345in8Comp 	= [[inVec3, inVec3, inVec2], [inIVec3, inIVec3, inIVec2], [inVec2, inIVec2, inFloat, inFloat, inInt, inBool], [inBool, inFloat, inInt, inVec2, inBool, inBVec2], [inBool, inBVec2, inInt, inVec4], [inFloat, inBVec4, inIVec2, inBool]]
346in9Comp 	= [[inVec3, inVec3, inVec3], [inIVec3, inIVec3, inIVec3], [inVec2, inIVec2, inFloat, inFloat, inInt, inBool, inBool], [inBool, inFloat, inInt, inVec2, inBool, inBVec2, inFloat], [inBool, inBVec2, inInt, inVec4, inBool], [inFloat, inBVec4, inIVec2, inBool, inBool]]
347in12Comp 	= [[inVec4, inVec4, inVec4], [inIVec4, inIVec4, inIVec4], [inVec2, inIVec2, inFloat, inFloat, inFloat, inInt, inInt, inBool, inBool, inBool], [inBool, inFloat, inInt, inVec3, inBool, inBVec3, inFloat, inBool], [inBool, inBVec4, inInt, inVec4, inBool, inFloat], [inFloat, inBVec4, inIVec4, inBool, inBool, inInt]]
348in16Comp	= [[inVec4, inVec4, inVec4, inVec4], [inIVec4, inIVec4, inIVec4, inIVec4], [inBVec4, inBVec4, inBVec4, inBVec4], [inFloat, inIVec3, inBVec3, inVec4, inIVec2, inFloat, inVec2]]
349
350allConversionCases.append(CaseGroup("vector_combine", "Vector Combine Constructors",
351	genComponentCases(in4Comp, [combineVec, combineIVec, combineBVec]) +\
352	genComponentCases(toPos(in4Comp), [combineUVec]) +\
353	genComponentCases(in3Comp, [combineVec, combineIVec, combineBVec]) +\
354	genComponentCases(toPos(in3Comp), [combineUVec]) +\
355	genComponentCases(in2Comp, [combineVec, combineIVec, combineBVec]) +\
356	genComponentCases(toPos(in2Comp), [combineUVec])))
357
358allConversionCases.append(CaseGroup("matrix_combine", "Matrix Combine Constructors",
359	genComponentCases(in4Comp,	[combineMat2])		+\
360	genComponentCases(in6Comp,	[combineMat2x3])	+\
361	genComponentCases(in8Comp,	[combineMat2x4])	+\
362	genComponentCases(in6Comp,	[combineMat3x2])	+\
363	genComponentCases(in9Comp,	[combineMat3])		+\
364	genComponentCases(in12Comp,	[combineMat3x4])	+\
365	genComponentCases(in8Comp,	[combineMat4x2])	+\
366	genComponentCases(in12Comp,	[combineMat4x3])	+\
367	genComponentCases(in16Comp, [combineMat4])
368	))
369
370# Main program.
371
372if __name__ == "__main__":
373	print "Generating shader case files."
374	writeAllCases("conversions.test", allConversionCases)
375