Home | History | Annotate | Download | only in scripts
      1 # -*- coding: utf-8 -*-
      2 
      3 #-------------------------------------------------------------------------
      4 # drawElements Quality Program utilities
      5 # --------------------------------------
      6 #
      7 # Copyright 2017 The Android Open Source Project
      8 #
      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
     12 #
     13 #      http://www.apache.org/licenses/LICENSE-2.0
     14 #
     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.
     20 #
     21 #-------------------------------------------------------------------------
     22 
     23 import random
     24 from genutil import *
     25 
     26 random.seed(0x1234)
     27 
     28 DATA_TYPES	= ["float", "vec4"]
     29 ARRAY_SIZES	= [16, 32, 64, 128]
     30 
     31 s_largeArrayCaseTemplate = """
     32 case ${{NAME}}
     33 	version 300 es
     34 	values
     35 	{
     36 		${{VALUES}}
     37 	}
     38 
     39 	both ""
     40 		#version 300 es
     41 		precision mediump float;
     42 
     43 		${DECLARATIONS}
     44 
     45 		void main()
     46 		{
     47 			${{ARRAY_DECL}}
     48 
     49 			${SETUP}
     50 			${{OP}}
     51 			${OUTPUT}
     52 		}
     53 	""
     54 end
     55 """[1:]
     56 
     57 
     58 class LargeConstantArrayCase(ShaderCase):
     59 	def __init__(self, name, array, inputs, outputs):
     60 		self.name	= name
     61 		self.array	= array
     62 		self.inputs	= inputs
     63 		self.outputs	= outputs
     64 		self.op		= "out0 = array[in0];"
     65 
     66 	def __str__(self):
     67 		params = {
     68 			"NAME":		self.name,
     69 			"VALUES":	genValues(self.inputs, self.outputs),
     70 			"ARRAY_DECL":	self.array,
     71 			"OP":		self.op
     72 		}
     73 		return fillTemplate(s_largeArrayCaseTemplate, params)
     74 
     75 
     76 def genArray(dataType, size):
     77 	elements = []
     78 	for i in xrange(size):
     79 		if dataType == "float":
     80 		    elements.append(Scalar(round(random.uniform(-1.0, 1.0), 6)))
     81 		if dataType == "vec4":
     82 		    elements.append(Vec4(*[round(random.uniform(-1.0, 1.0), 6) for x in range(4)]))
     83 
     84 	return elements
     85 
     86 
     87 def arrayToString(elements):
     88 	array = ('const {TYPE} array[{LENGTH}] = {TYPE}[](\n'
     89 		.format(TYPE=elements[0].typeString(), LENGTH=len(elements)))
     90 
     91 	array += "\n".join(str(e) + ',' for e in elements[:-1])
     92 	array += "\n" + str(elements[-1])
     93 	array += ");"
     94 
     95 	return array
     96 
     97 allCases = []
     98 largeConstantArrayCases = []
     99 
    100 for dataType in DATA_TYPES:
    101 	for arraySize in ARRAY_SIZES:
    102 		indexes = random.sample(range(arraySize-1), 10)
    103 		array = genArray(dataType, arraySize)
    104 		outputs = [array[index] for index in indexes]
    105 		outType = outputs[0].typeString()
    106 		caseName = "%s_%s" % (dataType, arraySize)
    107 
    108 		case = LargeConstantArrayCase(caseName,
    109 					      arrayToString(array),
    110 					      [("int in0", indexes)],
    111 					      [("%s out0" % outType, outputs)])
    112 
    113 		largeConstantArrayCases.append(case)
    114 
    115 allCases.append(CaseGroup("indexing", "Large constant array indexing", largeConstantArrayCases))
    116 
    117 if __name__ == "__main__":
    118 	print "Generating shader case files."
    119 	writeAllCases("large_constant_arrays.test", allCases)
    120