1 # -*- coding: utf-8 -*-
3 #-------------------------------------------------------------------------
4 # drawElements Quality Program utilities
5 # --------------------------------------
7 # Copyright 2017 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 #-------------------------------------------------------------------------
25 from collections import OrderedDict
27 VECTOR_TYPES = ["vec2", "vec3", "vec4", "ivec2", "ivec3", "ivec4"]
28 PRECISION_TYPES = ["mediump"]
29 SWIZZLE_NAMES = ["xyzw"]
31 s_swizzleCaseTemplate = """
41 precision mediump float;
55 def getDataTypeScalarSize (dt):
65 def getSwizzlesForWidth(width):
69 (1,0,1), (0,1,0,0), (1,0,1,0)]
73 (0,1,2), (2,1,0), (0,0,0), (2,2,2), (2,2,1), (1,0,1), (0,2,0),
78 (3,3,3), (1,1,3), (3,2,1),
79 (0,1,2,3), (3,2,1,0), (0,1,0,1), (1,2,2,1), (3,0,3,3), (0,1,0,0), (2,2,2,2)]
83 def operatorToSymbol(operator):
84 if operator == "add": return "+"
85 if operator == "subtract": return "-"
86 if operator == "multiply": return "*"
87 if operator == "divide": return "/"
92 class SwizzleCase(ShaderCase):
93 def __init__(self, name, swizzle1, swizzle2, inputs1, inputs2, operator, outputs):
95 self.swizzle1 = swizzle1
96 self.swizzle2 = swizzle2
97 self.inputs = inputs1 + inputs2
98 self.outputs = outputs
99 self.op = "out0 = in0.%s %s in1.%s;" % (swizzle1, operator, swizzle2)
104 "VALUES": genValues(self.inputs, self.outputs),
107 return fillTemplate(s_swizzleCaseTemplate, params)
111 inFloat = [Scalar(x) for x in [0.0, 1.0, 2.0, 3.5, -0.5, -20.125, 36.8125]]
112 inInt = [Scalar(x) for x in [0, 1, 2, 5, 8, 11, -12, -66, -192, 255]]
115 Vec4(0.1, 0.5, 0.75, 0.825),
116 Vec4(1.0, 1.25, 1.125, 1.75),
117 Vec4(-0.5, -2.25, -4.875, 9.0),
118 Vec4(-32.0, 64.0, -51.0, 24.0),
119 Vec4(-0.75, -1.0/31.0, 1.0/19.0, 1.0/4.0),
122 inVec3 = toVec3(inVec4)
123 inVec2 = toVec2(inVec4)
129 Vec4(-1, -2, -4, -9),
133 inIVec3 = toIVec3(inIVec4)
134 inIVec2 = toIVec2(inIVec4)
136 INPUTS = OrderedDict([
147 OPERATORS = OrderedDict([
149 ("subtract", op.sub),
150 ("multiply", op.mul),
154 vectorSwizzleGroupCases = {
163 for operator in OPERATORS:
164 for dataType in VECTOR_TYPES:
165 scalarSize = getDataTypeScalarSize(dataType)
166 for precision in PRECISION_TYPES:
167 for swizzleComponents in SWIZZLE_NAMES:
168 for swizzleIndices in getSwizzlesForWidth(scalarSize):
169 swizzle1 = "".join(map(lambda x: swizzleComponents[x], swizzleIndices))
171 swizzle2 = rotate(swizzle1, 1)
172 rotatedSwizzleIndices = rotate(swizzleIndices, 1)
174 operands1 = INPUTS[dataType]
175 operands2 = INPUTS[dataType] # these input values will be swizzled
177 outputs = map(lambda x, y: OPERATORS[operator](x.swizzle(swizzleIndices), y.swizzle(rotatedSwizzleIndices)), operands1, operands2)
178 outType = outputs[0].typeString()
179 caseName = "%s_%s_%s_%s" % (precision, dataType, swizzle1, swizzle2)
181 case = SwizzleCase( caseName,
184 [("%s in0" % dataType, operands1)],
185 [("%s in1" % dataType, operands2)],
186 operatorToSymbol(operator),
187 [("%s out0" % outType, outputs)])
189 vectorSwizzleGroupCases[operator].append(case)
191 allCases.append(CaseGroup("vector_" + operator, "Vector swizzle math operations", vectorSwizzleGroupCases[operator]))
193 if __name__ == "__main__":
194 print "Generating shader case files."
195 writeAllCases("swizzle_math_operations.test", allCases)