Hash :
d7d42395
Author :
Date :
2019-05-06T13:15:35
Format all of ANGLE's python code. BUG=angleproject:3421 Change-Id: I1d7282ac513c046de5d8ed87f7789290780d30a6 Reviewed-on: https://chromium-review.googlesource.com/c/angle/angle/+/1595440 Reviewed-by: Jamie Madill <jmadill@chromium.org> Commit-Queue: Geoff Lang <geofflang@chromium.org>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
#!/usr/bin/python
# Copyright 2017 The ANGLE Project Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
#
# gen_emulated_builtin_function_tables.py:
# Generator for the builtin function maps.
# NOTE: don't run this script directly. Run scripts/run_code_generation.py.
from datetime import date
import json
import os, sys
template_emulated_builtin_functions_hlsl = """// GENERATED FILE - DO NOT EDIT.
// Generated by {script_name} using data from {data_source_name}.
//
// Copyright {copyright_year} The ANGLE Project Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// emulated_builtin_functions_hlsl:
// HLSL code for emulating GLSL builtin functions not present in HLSL.
#include "compiler/translator/BuiltInFunctionEmulator.h"
#include "compiler/translator/tree_util/BuiltIn_autogen.h"
namespace sh
{{
namespace
{{
struct FunctionPair
{{
constexpr FunctionPair(const TSymbolUniqueId &idIn, const char *bodyIn) : id(idIn.get()), body(bodyIn)
{{
}}
int id;
const char *body;
}};
constexpr FunctionPair g_hlslFunctions[] = {{
{emulated_functions}}};
}} // anonymous namespace
const char *FindHLSLFunction(int uniqueId)
{{
for (size_t index = 0; index < ArraySize(g_hlslFunctions); ++index)
{{
const auto &function = g_hlslFunctions[index];
if (function.id == uniqueId)
{{
return function.body;
}}
}}
return nullptr;
}}
}} // namespace sh
"""
def reject_duplicate_keys(pairs):
found_keys = {}
for key, value in pairs:
if key in found_keys:
raise ValueError("duplicate key: %r" % (key,))
else:
found_keys[key] = value
return found_keys
def load_json(path):
with open(path) as map_file:
file_data = map_file.read()
map_file.close()
return json.loads(file_data, object_pairs_hook=reject_duplicate_keys)
def enum_type(arg):
# handle 'argtype argname' and 'out argtype argname'
chunks = arg.split(' ')
arg_type = chunks[0]
if len(chunks) == 3:
arg_type = chunks[1]
suffix = ""
if not arg_type[-1].isdigit():
suffix = '1'
if arg_type[0:4] == 'uint':
return 'UI' + arg_type[2:] + suffix
return arg_type.capitalize() + suffix
def gen_emulated_function(data):
func = ""
if 'comment' in data:
func += "".join(["// " + line + "\n" for line in data['comment']])
sig = data['return_type'] + ' ' + data['op'] + '_emu(' + ', '.join(data['args']) + ')'
body = [sig, '{'] + [' ' + line for line in data['body']] + ['}']
func += "{\n"
func += "BuiltInId::" + data['op'] + "_" + "_".join([enum_type(arg) for arg in data['args']
]) + ",\n"
if 'helper' in data:
func += '"' + '\\n"\n"'.join(data['helper']) + '\\n"\n'
func += '"' + '\\n"\n"'.join(body) + '\\n"\n'
func += "},\n"
return [func]
def main():
input_script = "emulated_builtin_function_data_hlsl.json"
hlsl_fname = "emulated_builtin_functions_hlsl_autogen.cpp"
# auto_script parameters.
if len(sys.argv) > 1:
inputs = [input_script]
outputs = [hlsl_fname]
if sys.argv[1] == 'inputs':
print ','.join(inputs)
elif sys.argv[1] == 'outputs':
print ','.join(outputs)
else:
print('Invalid script parameters')
return 1
return 0
hlsl_json = load_json(input_script)
emulated_functions = []
for item in hlsl_json:
emulated_functions += gen_emulated_function(item)
hlsl_gen = template_emulated_builtin_functions_hlsl.format(
script_name=sys.argv[0],
data_source_name=input_script,
copyright_year=date.today().year,
emulated_functions="".join(emulated_functions))
with open(hlsl_fname, 'wt') as f:
f.write(hlsl_gen)
f.close()
return 0
if __name__ == '__main__':
sys.exit(main())