1 import sys 2 import random 3 import operator 4 import itertools 5 6 from genutil import * 7 8 random.seed(1234567) 9 indices = 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 55 def 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 65 def 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 71 def combineUVec(comps): 72 return [x.toUint() for x in combineIVec(comps)] 73 74 def 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 80 def 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 87 def combineMat2(comps): return combineMat(2, 2, comps) 88 def combineMat2x3(comps): return combineMat(2, 3, comps) 89 def combineMat2x4(comps): return combineMat(2, 4, comps) 90 def combineMat3x2(comps): return combineMat(3, 2, comps) 91 def combineMat3(comps): return combineMat(3, 3, comps) 92 def combineMat3x4(comps): return combineMat(3, 4, comps) 93 def combineMat4x2(comps): return combineMat(4, 2, comps) 94 def combineMat4x3(comps): return combineMat(4, 3, comps) 95 def 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 103 s_simpleCaseTemplate = """ 104 case ${{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 "" 125 end 126 """[1:] 127 128 s_simpleIllegalCaseTemplate = """ 129 case ${{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 "" 148 end 149 """[1:] 150 151 class 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 166 class 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 184 class 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 200 class 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 221 def toPos (value): 222 if isinstance(value, list): 223 return [toPos(x) for x in value] 224 else: 225 return GenMath.abs(value) 226 227 inFloat = [Scalar(x) for x in [0.0, 1.0, 2.0, 3.5, -0.5, -8.25, -20.125, 36.8125]] 228 inInt = [Scalar(x) for x in [0, 1, 2, 5, 8, 11, -12, -66, -192, 255]] 229 inUint = [Uint(x) for x in [0, 2, 3, 8, 9, 12, 10, 45, 193, 255]] 230 inBool = [Scalar(x) for x in [True, False]] 231 232 inVec4 = [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)] 235 inVec3 = toVec3(inVec4) 236 inVec2 = toVec2(inVec4) 237 inIVec4 = toIVec4(inVec4) 238 inIVec3 = toIVec3(inVec4) 239 inIVec2 = toIVec2(inVec4) 240 inBVec4 = [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)] 241 inBVec3 = toBVec3(inBVec4) 242 inBVec2 = toBVec2(inBVec4) 243 inUVec4 = toUVec4(toPos(inVec4)) 244 inUVec3 = toUVec3(toPos(inVec3)) 245 inUVec2 = toUVec2(toPos(inVec2)) 246 247 # \todo [petri] Enable large values when epsilon adapts to the values. 248 inMat4 = [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 ] 252 inMat2 = [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 262 inMat4x3 = toMat4x3(inMat4) 263 inMat4x2 = toMat4x2(inMat4) 264 inMat3x4 = toMat3x4(inMat4) 265 inMat3 = toMat3(inMat4) 266 inMat3x2 = toMat3x2(inMat4) 267 inMat2x4 = toMat2x4(inMat4) 268 inMat2x3 = toMat2x3(inMat4) 269 270 def genConversionCases(inValueList, convFuncList): 271 combinations = list(itertools.product(inValueList, convFuncList)) 272 return [ConversionCase(inValues, convFunc) for (inValues, convFunc) in combinations] 273 274 def 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 280 def 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 286 def 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 296 allConversionCases = [] 297 298 # Scalar-to-scalar conversions. 299 allConversionCases.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. 304 allConversionCases.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. 309 allConversionCases.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). 314 allConversionCases.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). 319 allConversionCases.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. 328 allConversionCases.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. 337 allConversionCases.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. 341 in2Comp = [[inFloat, inFloat], [inInt, inInt], [inUint, inUint], [inBool, inBool], [inFloat, inInt], [inFloat, inBool], [inInt, inBool], [inInt, inUint], [inUint, inFloat]] 342 in3Comp = [[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]] 343 in4Comp = [[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]] 344 in6Comp = [[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]] 345 in8Comp = [[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]] 346 in9Comp = [[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]] 347 in12Comp = [[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]] 348 in16Comp = [[inVec4, inVec4, inVec4, inVec4], [inIVec4, inIVec4, inIVec4, inIVec4], [inBVec4, inBVec4, inBVec4, inBVec4], [inFloat, inIVec3, inBVec3, inVec4, inIVec2, inFloat, inVec2]] 349 350 allConversionCases.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 358 allConversionCases.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 372 if __name__ == "__main__": 373 print "Generating shader case files." 374 writeAllCases("conversions.test", allConversionCases) 375