1#!/usr/bin/python3 2# Copyright 2016 The ANGLE Project Authors. All rights reserved. 3# Use of this source code is governed by a BSD-style license that can be 4# found in the LICENSE file. 5# 6# gen_format_map.py: 7# Code generation for GL format map. The format map matches between 8# {format,type} and internal format. 9# NOTE: don't run this script directly. Run scripts/run_code_generation.py. 10 11import sys 12 13sys.path.append('renderer') 14import angle_format 15 16template_cpp = """// GENERATED FILE - DO NOT EDIT. 17// Generated by {script_name} using data from {data_source_name}. 18// ES3 format info from {es3_data_source_name}. 19// 20// Copyright 2016 The ANGLE Project Authors. All rights reserved. 21// Use of this source code is governed by a BSD-style license that can be 22// found in the LICENSE file. 23// 24// format_map: 25// Determining the sized internal format from a (format,type) pair. 26// Also check es3 format combinations for validity. 27 28#include "angle_gl.h" 29#include "common/debug.h" 30 31namespace gl 32{{ 33 34GLenum GetSizedFormatInternal(GLenum format, GLenum type) 35{{ 36 switch (format) 37 {{ 38{format_cases} case GL_NONE: 39 return GL_NONE; 40 41 default: 42 break; 43 }} 44 45 return GL_NONE; 46}} 47 48bool ValidES3Format(GLenum format) 49{{ 50 switch (format) 51 {{ 52{es3_format_cases} return true; 53 54 default: 55 return false; 56 }} 57}} 58 59bool ValidES3Type(GLenum type) 60{{ 61 switch (type) 62 {{ 63{es3_type_cases} return true; 64 65 default: 66 return false; 67 }} 68}} 69 70bool ValidES3FormatCombination(GLenum format, GLenum type, GLenum internalFormat) 71{{ 72 ASSERT(ValidES3Format(format) && ValidES3Type(type)); 73 74 switch (format) 75 {{ 76{es3_combo_cases} default: 77 UNREACHABLE(); 78 break; 79 }} 80 81 return false; 82}} 83 84}} // namespace gl 85""" 86 87template_format_case = """ case {format}: 88 switch (type) 89 {{ 90{type_cases} default: 91 break; 92 }} 93 break; 94 95""" 96 97template_simple_case = """ case {key}: 98 return {result}; 99""" 100 101template_es3_combo_type_case = """ case {type}: 102 {{ 103 switch (internalFormat) 104 {{ 105{internal_format_cases} return true; 106 default: 107 break; 108 }} 109 break; 110 }} 111""" 112 113 114def parse_type_case(type, result): 115 return template_simple_case.format(key=type, result=result) 116 117 118def parse_format_case(format, type_map): 119 type_cases = "" 120 for type, internal_format in sorted(type_map.items()): 121 type_cases += parse_type_case(type, internal_format) 122 return template_format_case.format(format=format, type_cases=type_cases) 123 124 125def main(): 126 127 # auto_script parameters. 128 if len(sys.argv) > 1: 129 inputs = [ 130 'renderer/angle_format.py', 'es3_format_type_combinations.json', 'format_map_data.json' 131 ] 132 outputs = ['format_map_autogen.cpp'] 133 134 if sys.argv[1] == 'inputs': 135 print(','.join(inputs)) 136 elif sys.argv[1] == 'outputs': 137 print(','.join(outputs)) 138 else: 139 print('Invalid script parameters') 140 return 1 141 return 0 142 143 input_script = 'format_map_data.json' 144 145 format_map = angle_format.load_json(input_script) 146 147 format_cases = "" 148 149 for format, type_map in sorted(format_map.items()): 150 format_cases += parse_format_case(format, type_map) 151 152 combo_data_file = 'es3_format_type_combinations.json' 153 es3_combo_data = angle_format.load_json(combo_data_file) 154 combo_data = [combo for sublist in es3_combo_data.values() for combo in sublist] 155 156 types = set() 157 formats = set() 158 combos = {} 159 160 for internal_format, format, type in combo_data: 161 types.update([type]) 162 formats.update([format]) 163 if format not in combos: 164 combos[format] = {} 165 if type not in combos[format]: 166 combos[format][type] = [internal_format] 167 else: 168 combos[format][type] += [internal_format] 169 170 es3_format_cases = "" 171 172 for format in sorted(formats): 173 es3_format_cases += " case " + format + ":\n" 174 175 es3_type_cases = "" 176 177 for type in sorted(types): 178 es3_type_cases += " case " + type + ":\n" 179 180 es3_combo_cases = "" 181 182 for format, type_combos in sorted(combos.items()): 183 this_type_cases = "" 184 for type, combos in sorted(type_combos.items()): 185 internal_format_cases = "" 186 for internal_format in combos: 187 internal_format_cases += " case " + internal_format + ":\n" 188 189 this_type_cases += template_es3_combo_type_case.format( 190 type=type, internal_format_cases=internal_format_cases) 191 192 es3_combo_cases += template_format_case.format(format=format, type_cases=this_type_cases) 193 194 with open('format_map_autogen.cpp', 'wt') as out_file: 195 output_cpp = template_cpp.format( 196 script_name=sys.argv[0], 197 data_source_name=input_script, 198 es3_data_source_name=combo_data_file, 199 format_cases=format_cases, 200 es3_format_cases=es3_format_cases, 201 es3_type_cases=es3_type_cases, 202 es3_combo_cases=es3_combo_cases) 203 out_file.write(output_cpp) 204 return 0 205 206 207if __name__ == '__main__': 208 sys.exit(main()) 209