1 # -*- coding: utf-8 -*-
3 #-------------------------------------------------------------------------
4 # drawElements Quality Program utilities
5 # --------------------------------------
7 # Copyright 2016 The Android Open Source Project
9 # Licensed under the Apache License, Version 2.0 (the "License");
10 # you may not use this file except in compliance with the License.
11 # You may obtain a copy of the License at
13 # http://www.apache.org/licenses/LICENSE-2.0
15 # Unless required by applicable law or agreed to in writing, software
16 # distributed under the License is distributed on an "AS IS" BASIS,
17 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 # See the License for the specific language governing permissions and
19 # limitations under the License.
21 #-------------------------------------------------------------------------
28 # WARNING: This file is auto-generated. Do NOT modify it manually, but rather
29 # modify the generating script file. Otherwise changes will be lost!
33 def __init__(self, name, description, children):
35 self.description = description
36 self.children = children
44 def indentTextBlock(text, indent):
45 indentStr = indent * "\t"
46 lines = text.split("\n")
47 lines = [indentStr + line for line in lines]
48 lines = [ ["", line][line.strip() != ""] for line in lines]
49 return "\n".join(lines)
51 def writeCase(f, case, indent, prefix):
52 print "\t%s" % (prefix + case.name)
53 if isinstance(case, CaseGroup):
54 f.write(indentTextBlock('group %s "%s"\n\n' % (case.name, case.description), indent))
55 for child in case.children:
56 writeCase(f, child, indent + 1, prefix + case.name + ".")
57 f.write(indentTextBlock("\nend # %s\n" % case.name, indent))
59 # \todo [petri] Fix hack.
60 fullPath = prefix + case.name
61 assert (fullPath not in g_processedCases)
62 g_processedCases[fullPath] = None
63 f.write(indentTextBlock(str(case) + "\n", indent))
65 def writeAllCases(fileName, caseList):
66 # Write all cases to file.
67 print " %s.." % fileName
68 f = file(fileName, "wb")
69 f.write(PREAMBLE + "\n")
71 writeCase(f, case, 0, "")
74 print "done! (%d cases written)" % len(g_processedCases)
76 # Template operations.
78 def genValues(inputs, outputs):
80 for (name, values) in inputs:
81 res.append("input %s = [ %s ];" % (name, " | ".join([str(v) for v in values]).lower()))
82 for (name, values) in outputs:
83 res.append("output %s = [ %s ];" % (name, " | ".join([str(v) for v in values]).lower()))
84 return ("\n".join(res))
86 def fillTemplate(template, params):
89 for (key, value) in params.items():
90 m = re.search(r"^(\s*)\$\{\{%s\}\}$" % key, s, re.M)
96 repl = "\n".join(["%s%s" % (ws, line) for line in value.split("\n")])
97 s = s[:start] + repl + s[end:]
99 s = s[:start] + s[end+1:] # drop the whole line
101 s = s.replace("${{%s}}" % key, value)
104 # Return shuffled version of list
110 def repeatToLength(lst, toLength):
111 return (toLength / len(lst)) * lst + lst[: toLength % len(lst)]
113 # Helpers to convert a list of Scalar/Vec values into another type.
115 def toFloat(lst): return [Scalar(float(v.x)) for v in lst]
116 def toInt(lst): return [Scalar(int(v.x)) for v in lst]
117 def toBool(lst): return [Scalar(bool(v.x)) for v in lst]
118 def toVec4(lst): return [v.toFloat().toVec4() for v in lst]
119 def toVec3(lst): return [v.toFloat().toVec3() for v in lst]
120 def toVec2(lst): return [v.toFloat().toVec2() for v in lst]
121 def toIVec4(lst): return [v.toInt().toVec4() for v in lst]
122 def toIVec3(lst): return [v.toInt().toVec3() for v in lst]
123 def toIVec2(lst): return [v.toInt().toVec2() for v in lst]
124 def toBVec4(lst): return [v.toBool().toVec4() for v in lst]
125 def toBVec3(lst): return [v.toBool().toVec3() for v in lst]
126 def toBVec2(lst): return [v.toBool().toVec2() for v in lst]
127 def toMat2(lst): return [v.toMat2() for v in lst]
128 def toMat3(lst): return [v.toMat3() for v in lst]
129 def toMat4(lst): return [v.toMat4() for v in lst]
131 # Random value generation.
137 def uniformVec4(self, count, mn, mx):
138 ret = [Vec4(random.uniform(mn, mx), random.uniform(mn, mx), random.uniform(mn, mx), random.uniform(mn, mx)) for x in xrange(count)]
141 ret[2].x = (mn + mx) * 0.5
144 def uniformBVec4(self, count):
145 ret = [Vec4(random.random() >= 0.5, random.random() >= 0.5, random.random() >= 0.5, random.random() >= 0.5) for x in xrange(count)]
152 # Math operating on Scalar/Vector types.
154 def glslSign(a): return 0.0 if (a == 0) else +1.0 if (a > 0.0) else -1.0
155 def glslMod(x, y): return x - y*math.floor(x/y)
156 def glslClamp(x, mn, mx): return mn if (x < mn) else mx if (x > mx) else x
160 def unary(func): return lambda val: val.applyUnary(func)
163 def binary(func): return lambda a, b: (b.expandVec(a)).applyBinary(func, a.expandVec(b))
166 def frac(val): return val.applyUnary(lambda x: x - math.floor(x))
169 def exp2(val): return val.applyUnary(lambda x: math.pow(2.0, x))
172 def log2(val): return val.applyUnary(lambda x: math.log(x, 2.0))
175 def rsq(val): return val.applyUnary(lambda x: 1.0 / math.sqrt(x))
178 def sign(val): return val.applyUnary(glslSign)
181 def isEqual(a, b): return Scalar(a.isEqual(b))
184 def isNotEqual(a, b): return Scalar(not a.isEqual(b))
187 def step(a, b): return (b.expandVec(a)).applyBinary(lambda edge, x: [1.0, 0.0][x < edge], a.expandVec(b))
190 def length(a): return a.length()
193 def distance(a, b): return a.distance(b)
196 def dot(a, b): return a.dot(b)
199 def cross(a, b): return a.cross(b)
202 def normalize(a): return a.normalize()
205 def boolAny(a): return a.boolAny()
208 def boolAll(a): return a.boolAll()
211 def boolNot(a): return a.boolNot()
216 def __init__(self, x):
219 def applyUnary(self, func): return Scalar(func(self.x))
220 def applyBinary(self, func, other): return Scalar(func(self.x, other.x))
222 def isEqual(self, other): assert isinstance(other, Scalar); return (self.x == other.x)
224 def expandVec(self, val): return val
225 def toScalar(self): return Scalar(self.x)
226 def toVec2(self): return Vec2(self.x, self.x)
227 def toVec3(self): return Vec3(self.x, self.x, self.x)
228 def toVec4(self): return Vec4(self.x, self.x, self.x, self.x)
229 def toMat2(self): return self.toVec2().toMat2()
230 def toMat3(self): return self.toVec3().toMat3()
231 def toMat4(self): return self.toVec4().toMat4()
233 def toFloat(self): return Scalar(float(self.x))
234 def toInt(self): return Scalar(int(self.x))
235 def toBool(self): return Scalar(bool(self.x))
237 def getNumScalars(self): return 1
238 def getScalars(self): return [self.x]
240 def typeString(self):
241 if isinstance(self.x, bool):
243 elif isinstance(self.x, int):
245 elif isinstance(self.x, float):
250 def vec4Swizzle(self):
257 return Scalar(abs(self.x))
259 def distance(self, v):
260 assert isinstance(v, Scalar)
261 return Scalar(abs(self.x - v.x))
264 assert isinstance(v, Scalar)
265 return Scalar(self.x * v.x)
268 return Scalar(glslSign(self.x))
271 return Scalar(-self.x)
273 def __add__(self, val):
274 assert isinstance(val, Scalar)
275 return Scalar(self.x + val.x)
277 def __sub__(self, val):
280 def __mul__(self, val):
281 if isinstance(val, Scalar):
282 return Scalar(self.x * val.x)
283 elif isinstance(val, Vec2):
284 return Vec2(self.x * val.x, self.x * val.y)
285 elif isinstance(val, Vec3):
286 return Vec3(self.x * val.x, self.x * val.y, self.x * val.z)
287 elif isinstance(val, Vec4):
288 return Vec4(self.x * val.x, self.x * val.y, self.x * val.z, self.x * val.w)
292 def __div__(self, val):
293 if isinstance(val, Scalar):
294 return Scalar(self.x / val.x)
295 elif isinstance(val, Vec2):
296 return Vec2(self.x / val.x, self.x / val.y)
297 elif isinstance(val, Vec3):
298 return Vec3(self.x / val.x, self.x / val.y, self.x / val.z)
299 elif isinstance(val, Vec4):
300 return Vec4(self.x / val.x, self.x / val.y, self.x / val.z, self.x / val.w)
306 def fromScalarList(lst):
307 assert (len(lst) >= 1 and len(lst) <= 4)
308 if (len(lst) == 1): return Scalar(lst[0])
309 elif (len(lst) == 2): return Vec2(lst[0], lst[1])
310 elif (len(lst) == 3): return Vec3(lst[0], lst[1], lst[2])
311 else: return Vec4(lst[0], lst[1], lst[2], lst[3])
313 def isEqual(self, other):
314 assert isinstance(other, Vec);
315 return (self.getScalars() == other.getScalars())
318 return Scalar(math.sqrt(self.dot(self).x))
321 return self * Scalar(1.0 / self.length().x)
323 def swizzle(self, indexList):
324 inScalars = self.getScalars()
325 outScalars = map(lambda ndx: inScalars[ndx], indexList)
326 return Vec.fromScalarList(outScalars)
332 def __init__(self, x, y):
333 assert(x.__class__ == y.__class__)
337 def applyUnary(self, func): return Vec2(func(self.x), func(self.y))
338 def applyBinary(self, func, other): return Vec2(func(self.x, other.x), func(self.y, other.y))
340 def expandVec(self, val): return val.toVec2()
341 def toScalar(self): return Scalar(self.x)
342 def toVec2(self): return Vec2(self.x, self.y)
343 def toVec3(self): return Vec3(self.x, self.y, 0.0)
344 def toVec4(self): return Vec4(self.x, self.y, 0.0, 0.0)
345 def toMat2(self): return Mat2(float(self.x), 0.0, 0.0, float(self.y));
347 def toFloat(self): return Vec2(float(self.x), float(self.y))
348 def toInt(self): return Vec2(int(self.x), int(self.y))
349 def toBool(self): return Vec2(bool(self.x), bool(self.y))
351 def getNumScalars(self): return 2
352 def getScalars(self): return [self.x, self.y]
354 def typeString(self):
355 if isinstance(self.x, bool):
357 elif isinstance(self.x, int):
359 elif isinstance(self.x, float):
364 def vec4Swizzle(self):
368 if isinstance(self.x, bool):
369 return "bvec2(%s, %s)" % (str(self.x).lower(), str(self.y).lower())
370 elif isinstance(self.x, int):
371 return "ivec2(%i, %i)" % (self.x, self.y)
372 elif isinstance(self.x, float):
373 return "vec2(%s, %s)" % (self.x, self.y)
377 def distance(self, v):
378 assert isinstance(v, Vec2)
379 return (self - v).length()
382 assert isinstance(v, Vec2)
383 return Scalar(self.x*v.x + self.y*v.y)
386 return Vec2(-self.x, -self.y)
388 def __add__(self, val):
389 if isinstance(val, Scalar):
390 return Vec2(self.x + val, self.y + val)
391 elif isinstance(val, Vec2):
392 return Vec2(self.x + val.x, self.y + val.y)
396 def __sub__(self, val):
399 def __mul__(self, val):
400 if isinstance(val, Scalar):
402 assert isinstance(val, Vec2)
403 return Vec2(self.x * val.x, self.y * val.y)
405 def __div__(self, val):
406 if isinstance(val, Scalar):
407 return Vec2(self.x / val.x, self.y / val.x)
409 assert isinstance(val, Vec2)
410 return Vec2(self.x / val.x, self.y / val.y)
412 def boolAny(self): return Scalar(self.x or self.y)
413 def boolAll(self): return Scalar(self.x and self.y)
414 def boolNot(self): return Vec2(not self.x, not self.y)
417 def __init__(self, x, y, z):
418 assert((x.__class__ == y.__class__) and (x.__class__ == z.__class__))
423 def applyUnary(self, func): return Vec3(func(self.x), func(self.y), func(self.z))
424 def applyBinary(self, func, other): return Vec3(func(self.x, other.x), func(self.y, other.y), func(self.z, other.z))
426 def expandVec(self, val): return val.toVec3()
427 def toScalar(self): return Scalar(self.x)
428 def toVec2(self): return Vec2(self.x, self.y)
429 def toVec3(self): return Vec3(self.x, self.y, self.z)
430 def toVec4(self): return Vec4(self.x, self.y, self.z, 0.0)
431 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));
433 def toFloat(self): return Vec3(float(self.x), float(self.y), float(self.z))
434 def toInt(self): return Vec3(int(self.x), int(self.y), int(self.z))
435 def toBool(self): return Vec3(bool(self.x), bool(self.y), bool(self.z))
437 def getNumScalars(self): return 3
438 def getScalars(self): return [self.x, self.y, self.z]
440 def typeString(self):
441 if isinstance(self.x, bool):
443 elif isinstance(self.x, int):
445 elif isinstance(self.x, float):
450 def vec4Swizzle(self):
454 if isinstance(self.x, bool):
455 return "bvec3(%s, %s, %s)" % (str(self.x).lower(), str(self.y).lower(), str(self.z).lower())
456 elif isinstance(self.x, int):
457 return "ivec3(%i, %i, %i)" % (self.x, self.y, self.z)
458 elif isinstance(self.x, float):
459 return "vec3(%s, %s, %s)" % (self.x, self.y, self.z)
463 def distance(self, v):
464 assert isinstance(v, Vec3)
465 return (self - v).length()
468 assert isinstance(v, Vec3)
469 return Scalar(self.x*v.x + self.y*v.y + self.z*v.z)
472 assert isinstance(v, Vec3)
473 return Vec3(self.y*v.z - v.y*self.z,
474 self.z*v.x - v.z*self.x,
475 self.x*v.y - v.x*self.y)
478 return Vec3(-self.x, -self.y, -self.z)
480 def __add__(self, val):
481 if isinstance(val, Scalar):
482 return Vec3(self.x + val, self.y + val)
483 elif isinstance(val, Vec3):
484 return Vec3(self.x + val.x, self.y + val.y, self.z + val.z)
488 def __sub__(self, val):
491 def __mul__(self, val):
492 if isinstance(val, Scalar):
494 assert isinstance(val, Vec3)
495 return Vec3(self.x * val.x, self.y * val.y, self.z * val.z)
497 def __div__(self, val):
498 if isinstance(val, Scalar):
499 return Vec3(self.x / val.x, self.y / val.x, self.z / val.x)
503 def boolAny(self): return Scalar(self.x or self.y or self.z)
504 def boolAll(self): return Scalar(self.x and self.y and self.z)
505 def boolNot(self): return Vec3(not self.x, not self.y, not self.z)
508 def __init__(self, x, y, z, w):
509 assert((x.__class__ == y.__class__) and (x.__class__ == z.__class__) and (x.__class__ == w.__class__))
515 def applyUnary(self, func): return Vec4(func(self.x), func(self.y), func(self.z), func(self.w))
516 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))
518 def expandVec(self, val): return val.toVec4()
519 def toScalar(self): return Scalar(self.x)
520 def toVec2(self): return Vec2(self.x, self.y)
521 def toVec3(self): return Vec3(self.x, self.y, self.z)
522 def toVec4(self): return Vec4(self.x, self.y, self.z, self.w)
523 def toMat2(self): return Mat2(float(self.x), float(self.y), float(self.z), float(self.w))
524 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));
526 def toFloat(self): return Vec4(float(self.x), float(self.y), float(self.z), float(self.w))
527 def toInt(self): return Vec4(int(self.x), int(self.y), int(self.z), int(self.w))
528 def toBool(self): return Vec4(bool(self.x), bool(self.y), bool(self.z), bool(self.w))
530 def getNumScalars(self): return 4
531 def getScalars(self): return [self.x, self.y, self.z, self.w]
533 def typeString(self):
534 if isinstance(self.x, bool):
536 elif isinstance(self.x, int):
538 elif isinstance(self.x, float):
543 def vec4Swizzle(self):
547 if isinstance(self.x, bool):
548 return "bvec4(%s, %s, %s, %s)" % (str(self.x).lower(), str(self.y).lower(), str(self.z).lower(), str(self.w).lower())
549 elif isinstance(self.x, int):
550 return "ivec4(%i, %i, %i, %i)" % (self.x, self.y, self.z, self.w)
551 elif isinstance(self.x, float):
552 return "vec4(%s, %s, %s, %s)" % (self.x, self.y, self.z, self.w)
556 def distance(self, v):
557 assert isinstance(v, Vec4)
558 return (self - v).length()
561 assert isinstance(v, Vec4)
562 return Scalar(self.x*v.x + self.y*v.y + self.z*v.z + self.w*v.w)
565 return Vec4(-self.x, -self.y, -self.z, -self.w)
567 def __add__(self, val):
568 if isinstance(val, Scalar):
569 return Vec3(self.x + val, self.y + val)
570 elif isinstance(val, Vec4):
571 return Vec4(self.x + val.x, self.y + val.y, self.z + val.z, self.w + val.w)
575 def __sub__(self, val):
578 def __mul__(self, val):
579 if isinstance(val, Scalar):
581 assert isinstance(val, Vec4)
582 return Vec4(self.x * val.x, self.y * val.y, self.z * val.z, self.w * val.w)
584 def __div__(self, val):
585 if isinstance(val, Scalar):
586 return Vec4(self.x / val.x, self.y / val.x, self.z / val.x, self.w / val.x)
590 def boolAny(self): return Scalar(self.x or self.y or self.z or self.w)
591 def boolAll(self): return Scalar(self.x and self.y and self.z and self.w)
592 def boolNot(self): return Vec4(not self.x, not self.y, not self.z, not self.w)
594 # \note Column-major storage.
596 def __init__ (self, numCols, numRows, scalars):
597 assert len(scalars) == numRows*numCols
598 self.numCols = numCols
599 self.numRows = numRows
600 self.scalars = scalars
603 def identity (numCols, numRows):
605 for col in range(0, numCols):
606 for row in range(0, numRows):
607 scalars.append(1.0 if col == row else 0.0)
608 return Mat(numCols, numRows, scalars)
610 def get (self, colNdx, rowNdx):
611 assert 0 <= colNdx and colNdx < self.numCols
612 assert 0 <= rowNdx and rowNdx < self.numRows
613 return self.scalars[colNdx*self.numRows + rowNdx]
615 def set (self, colNdx, rowNdx, scalar):
616 assert 0 <= colNdx and colNdx < self.numCols
617 assert 0 <= rowNdx and rowNdx < self.numRows
618 self.scalars[colNdx*self.numRows + rowNdx] = scalar
620 def toMatrix (self, numCols, numRows):
621 res = Mat.identity(numCols, numRows)
622 for col in range(0, min(self.numCols, numCols)):
623 for row in range(0, min(self.numRows, numRows)):
624 res.set(col, row, self.get(col, row))
627 def toMat2 (self): return self.toMatrix(2, 2)
628 def toMat2x3 (self): return self.toMatrix(2, 3)
629 def toMat2x4 (self): return self.toMatrix(2, 4)
630 def toMat3x2 (self): return self.toMatrix(3, 2)
631 def toMat3 (self): return self.toMatrix(3, 3)
632 def toMat3x4 (self): return self.toMatrix(3, 4)
633 def toMat4x2 (self): return self.toMatrix(4, 2)
634 def toMat4x3 (self): return self.toMatrix(4, 3)
635 def toMat4 (self): return self.toMatrix(4, 4)
637 def typeString(self):
638 if self.numRows == self.numCols:
639 return "mat%d" % self.numRows
641 return "mat%dx%d" % (self.numCols, self.numRows)
644 return "%s(%s)" % (self.typeString(), ", ".join([str(s) for s in self.scalars]))
646 def isTypeEqual (self, other):
647 return isinstance(other, Mat) and self.numRows == other.numRows and self.numCols == other.numCols
649 def isEqual(self, other):
650 assert self.isTypeEqual(other)
651 return (self.scalars == other.scalars)
653 def compMul(self, val):
654 assert self.isTypeEqual(val)
655 return Mat(self.numRows, self.numCols, [self.scalars(i) * val.scalars(i) for i in range(self.numRows*self.numCols)])
658 def __init__(self, m00, m01, m10, m11):
659 Mat.__init__(self, 2, 2, [m00, m10, m01, m11])
662 def __init__(self, m00, m01, m02, m10, m11, m12, m20, m21, m22):
663 Mat.__init__(self, 3, 3, [m00, m10, m20,
668 def __init__(self, m00, m01, m02, m03, m10, m11, m12, m13, m20, m21, m22, m23, m30, m31, m32, m33):
669 Mat.__init__(self, 4, 4, [m00, m10, m20, m30,