diff options
author | Hugues Evrard <hevrard@users.noreply.github.com> | 2019-03-12 13:47:59 +0100 |
---|---|---|
committer | dan sinclair <dj2@everburning.com> | 2019-03-12 08:47:59 -0400 |
commit | 6bebb11e52056e8ec7bb1b367b86f47e20cd0e9c (patch) | |
tree | a1aa8aa305dd34fb726ffb2d0d83e6270b915e60 | |
parent | 6b1e152820e3ef3bc3c47c12aee387cf53db97d7 (diff) | |
download | amber-6bebb11e52056e8ec7bb1b367b86f47e20cd0e9c.tar.gz |
Use template for Vulkan wrappers generation (#354)
-rwxr-xr-x | tools/update_vk_wrappers.py | 96 |
1 files changed, 45 insertions, 51 deletions
diff --git a/tools/update_vk_wrappers.py b/tools/update_vk_wrappers.py index eba68e9..bb7dd45 100755 --- a/tools/update_vk_wrappers.py +++ b/tools/update_vk_wrappers.py @@ -22,7 +22,7 @@ import os.path import re import sys import xml.etree.ElementTree as ET - +from string import Template def read_inc(file): methods = [] @@ -80,51 +80,42 @@ def gen_wrappers(methods, xml): param_vals.append(param['def']) param_names.append(param['name']) - content += "{\n" - content += " PFN_{} ptr = reinterpret_cast<PFN_{}>(".format(method, method) - content += "getInstanceProcAddr(instance_, \"{}\"));\n".format(method); - content += " if (!ptr) {\n" - content += " return Result(\"Vulkan: Unable to " - content += "load {} pointer\");\n".format(method) - content += " }\n" - - # if delegate is not null ... - content += " if (delegate && delegate->LogGraphicsCalls()) {\n" - - # ... lambda with delegate calls ... - content += " ptrs_.{} = [ptr, delegate](".format(method) - content += ', '.join(str(x) for x in param_vals) - content += ") -> " + data['return_type'] + " {\n" - content += ' delegate->Log("{}");\n'.format(method) - if data['return_type'] != 'void': - content += " {} ret = ".format(data['return_type']) - content += "ptr(" + ", ".join(str(x) for x in param_names) + ");\n" - content += " return"; - if data['return_type'] != 'void': - content += " ret" - content += ";\n" - content += " };\n" - - # ... else ... - content += " } else {\n" - - # ... simple wrapper lambda ... - content += " ptrs_.{} = [ptr](".format(method) - content += ', '.join(str(x) for x in param_vals) - content += ") -> " + data['return_type'] + " {\n" - if data['return_type'] != 'void': - content += " {} ret = ".format(data['return_type']) - content += "ptr(" + ", ".join(str(x) for x in param_names) + ");\n" - content += " return"; - if data['return_type'] != 'void': - content += " ret" - content += ";\n" - - content += " };\n" - - # ... end if - content += " }\n" - content += "}\n" + signature = ', '.join(str(x) for x in param_vals) + arguments = ', '.join(str(x) for x in param_names) + return_type = data['return_type'] + return_variable = '' + call_prefix = '' + if return_type != 'void': + return_variable = 'ret' + call_prefix = return_type + ' ' + return_variable + ' = ' + + + template = Template(R'''{ + PFN_${method} ptr = reinterpret_cast<PFN_${method}>(getInstanceProcAddr(instance_, "${method}")); + if (!ptr) { + return Result("Vulkan: Unable to load ${method} pointer"); + } + if (delegate && delegate->LogGraphicsCalls()) { + ptrs_.${method} = [ptr, delegate](${signature}) -> ${return_type} { + delegate->Log("${method}"); + ${call_prefix}ptr(${arguments}); + return ${return_variable}; + }; + } else { + ptrs_.${method} = [ptr](${signature}) -> ${return_type} { + ${call_prefix}ptr(${arguments}); + return ${return_variable}; + }; + } +} +''') + + content += template.substitute(method=method, + signature=signature, + arguments=arguments, + return_type=return_type, + return_variable=return_variable, + call_prefix=call_prefix) return content @@ -150,12 +141,15 @@ def gen_headers(methods, xml): def gen_direct(methods): content = ""; + + template = Template(R''' +if (!(ptrs_.${method} = reinterpret_cast<PFN_${method}>(getInstanceProcAddr(instance_, "${method}")))) { + return Result("Vulkan: Unable to load ${method} pointer"); +} +''') + for method in methods: - content += "if (!(ptrs_.{} = reinterpret_cast<PFN_{}>(".format(method, method) - content += "getInstanceProcAddr(instance_, \"{}\"))))".format(method) - content += " {\n" - content += " return Result(\"Vulkan: Unable to load {} pointer\");\n".format(method) - content += "}\n" + content += template.substitute(method=method) return content |