Tobin Ehlis | 33267fd | 2014-11-10 16:03:19 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
| 2 | # |
Karl Schultz | 8e42f40 | 2016-02-02 19:32:33 -0700 | [diff] [blame] | 3 | # Copyright (c) 2015-2016 The Khronos Group Inc. |
| 4 | # Copyright (c) 2015-2016 Valve Corporation |
| 5 | # Copyright (c) 2015-2016 LunarG, Inc. |
| 6 | # Copyright (c) 2015-2016 Google Inc. |
Tobin Ehlis | 33267fd | 2014-11-10 16:03:19 -0700 | [diff] [blame] | 7 | # |
Jon Ashburn | 3ebf125 | 2016-04-19 11:30:31 -0600 | [diff] [blame] | 8 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | # you may not use this file except in compliance with the License. |
| 10 | # You may obtain a copy of the License at |
Tobin Ehlis | 33267fd | 2014-11-10 16:03:19 -0700 | [diff] [blame] | 11 | # |
Jon Ashburn | 3ebf125 | 2016-04-19 11:30:31 -0600 | [diff] [blame] | 12 | # http://www.apache.org/licenses/LICENSE-2.0 |
Tobin Ehlis | 33267fd | 2014-11-10 16:03:19 -0700 | [diff] [blame] | 13 | # |
Jon Ashburn | 3ebf125 | 2016-04-19 11:30:31 -0600 | [diff] [blame] | 14 | # Unless required by applicable law or agreed to in writing, software |
| 15 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | # See the License for the specific language governing permissions and |
| 18 | # limitations under the License. |
Courtney Goeltzenleuchter | 0555952 | 2015-10-30 11:14:30 -0600 | [diff] [blame] | 19 | # |
| 20 | # Author: Courtney Goeltzenleuchter <courtney@LunarG.com> |
| 21 | # Author: Tobin Ehlis <tobin@lunarg.com> |
| 22 | |
Tobin Ehlis | 6442dca | 2014-10-22 15:13:53 -0600 | [diff] [blame] | 23 | import argparse |
| 24 | import os |
| 25 | import sys |
Michael Lentine | 33d96a3 | 2016-01-05 11:10:15 -0600 | [diff] [blame] | 26 | import re |
Tobin Ehlis | a30e7e5 | 2015-07-06 14:02:36 -0600 | [diff] [blame] | 27 | import vulkan |
Tobin Ehlis | 08fafd0 | 2015-06-12 12:49:01 -0600 | [diff] [blame] | 28 | from source_line_info import sourcelineinfo |
Tobin Ehlis | 6442dca | 2014-10-22 15:13:53 -0600 | [diff] [blame] | 29 | |
Tobin Ehlis | 08fafd0 | 2015-06-12 12:49:01 -0600 | [diff] [blame] | 30 | # vk_helper.py overview |
| 31 | # This script generates code based on vulkan input header |
| 32 | # It generate wrappers functions that can be used to display |
Tobin Ehlis | 6442dca | 2014-10-22 15:13:53 -0600 | [diff] [blame] | 33 | # structs in a human-readable txt format, as well as utility functions |
| 34 | # to print enum values as strings |
| 35 | |
Tobin Ehlis | 6442dca | 2014-10-22 15:13:53 -0600 | [diff] [blame] | 36 | def handle_args(): |
| 37 | parser = argparse.ArgumentParser(description='Perform analysis of vogl trace.') |
| 38 | parser.add_argument('input_file', help='The input header file from which code will be generated.') |
Mark Lobodzinski | 28b8f02 | 2015-08-27 15:30:29 -0600 | [diff] [blame] | 39 | parser.add_argument('--rel_out_dir', required=False, default='vktrace_gen', help='Path relative to exec path to write output files. Will be created if needed.') |
Tobin Ehlis | 6442dca | 2014-10-22 15:13:53 -0600 | [diff] [blame] | 40 | parser.add_argument('--abs_out_dir', required=False, default=None, help='Absolute path to write output files. Will be created if needed.') |
| 41 | parser.add_argument('--gen_enum_string_helper', required=False, action='store_true', default=False, help='Enable generation of helper header file to print string versions of enums.') |
| 42 | parser.add_argument('--gen_struct_wrappers', required=False, action='store_true', default=False, help='Enable generation of struct wrapper classes.') |
Jon Ashburn | ea65e49 | 2015-08-06 17:27:49 -0600 | [diff] [blame] | 43 | parser.add_argument('--gen_struct_sizes', required=False, action='store_true', default=False, help='Enable generation of struct sizes.') |
Jamie Madill | 9c04736 | 2016-12-14 13:05:05 -0500 | [diff] [blame] | 44 | parser.add_argument('--quiet', required=False, action='store_true', default=False, help='Suppress output from running the script.') |
Tobin Ehlis | 6442dca | 2014-10-22 15:13:53 -0600 | [diff] [blame] | 45 | #parser.add_argument('--test', action='store_true', default=False, help='Run simple test.') |
| 46 | return parser.parse_args() |
| 47 | |
| 48 | # TODO : Ideally these data structs would be opaque to user and could be wrapped |
| 49 | # in their own class(es) to make them friendly for data look-up |
| 50 | # Dicts for Data storage |
| 51 | # enum_val_dict[value_name] = dict keys are the string enum value names for all enums |
| 52 | # |-------->['type'] = the type of enum class into which the value falls |
| 53 | # |-------->['val'] = the value assigned to this particular value_name |
| 54 | # '-------->['unique'] = bool designating if this enum 'val' is unique within this enum 'type' |
| 55 | enum_val_dict = {} |
| 56 | # enum_type_dict['type'] = the type or class of of enum |
| 57 | # '----->['val_name1', 'val_name2'...] = each type references a list of val_names within this type |
| 58 | enum_type_dict = {} |
| 59 | # struct_dict['struct_basename'] = the base (non-typedef'd) name of the struct |
| 60 | # |->[<member_num>] = members are stored via their integer placement in the struct |
| 61 | # | |->['name'] = string name of this struct member |
| 62 | # ... |->['full_type'] = complete type qualifier for this member |
| 63 | # |->['type'] = base type for this member |
| 64 | # |->['ptr'] = bool indicating if this member is ptr |
| 65 | # |->['const'] = bool indicating if this member is const |
| 66 | # |->['struct'] = bool indicating if this member is a struct type |
| 67 | # |->['array'] = bool indicating if this member is an array |
Tobin Ehlis | bd6a916 | 2015-03-13 07:18:05 -0600 | [diff] [blame] | 68 | # |->['dyn_array'] = bool indicating if member is a dynamically sized array |
| 69 | # '->['array_size'] = For dyn_array, member name used to size array, else int size for static array |
Tobin Ehlis | 6442dca | 2014-10-22 15:13:53 -0600 | [diff] [blame] | 70 | struct_dict = {} |
Tobin Ehlis | 3764969 | 2016-02-10 15:32:46 -0700 | [diff] [blame] | 71 | struct_order_list = [] # struct names in order they're declared |
| 72 | # Store struct names that require #ifdef guards |
| 73 | # dict stores struct and enum definitions that are guarded by ifdef as the key |
| 74 | # and the txt of the ifdef is the value |
| 75 | ifdef_dict = {} |
Tobin Ehlis | 6442dca | 2014-10-22 15:13:53 -0600 | [diff] [blame] | 76 | # typedef_fwd_dict stores mapping from orig_type_name -> new_type_name |
| 77 | typedef_fwd_dict = {} |
| 78 | # typedef_rev_dict stores mapping from new_type_name -> orig_type_name |
| 79 | typedef_rev_dict = {} # store new_name -> orig_name mapping |
| 80 | # types_dict['id_name'] = identifier name will map to it's type |
| 81 | # '---->'type' = currently either 'struct' or 'enum' |
| 82 | types_dict = {} # store orig_name -> type mapping |
| 83 | |
| 84 | |
| 85 | # Class that parses header file and generates data structures that can |
| 86 | # Then be used for other tasks |
| 87 | class HeaderFileParser: |
| 88 | def __init__(self, header_file=None): |
| 89 | self.header_file = header_file |
| 90 | # store header data in various formats, see above for more info |
| 91 | self.enum_val_dict = {} |
| 92 | self.enum_type_dict = {} |
| 93 | self.struct_dict = {} |
| 94 | self.typedef_fwd_dict = {} |
| 95 | self.typedef_rev_dict = {} |
| 96 | self.types_dict = {} |
Tobin Ehlis | 5178bd7 | 2015-03-06 10:38:25 -0700 | [diff] [blame] | 97 | self.last_struct_count_name = '' |
Mark Lobodzinski | 28b8f02 | 2015-08-27 15:30:29 -0600 | [diff] [blame] | 98 | |
Tobin Ehlis | 6442dca | 2014-10-22 15:13:53 -0600 | [diff] [blame] | 99 | def setHeaderFile(self, header_file): |
| 100 | self.header_file = header_file |
| 101 | |
| 102 | def get_enum_val_dict(self): |
| 103 | return self.enum_val_dict |
| 104 | |
| 105 | def get_enum_type_dict(self): |
| 106 | return self.enum_type_dict |
| 107 | |
| 108 | def get_struct_dict(self): |
| 109 | return self.struct_dict |
| 110 | |
| 111 | def get_typedef_fwd_dict(self): |
| 112 | return self.typedef_fwd_dict |
| 113 | |
| 114 | def get_typedef_rev_dict(self): |
| 115 | return self.typedef_rev_dict |
| 116 | |
| 117 | def get_types_dict(self): |
| 118 | return self.types_dict |
| 119 | |
| 120 | # Parse header file into data structures |
| 121 | def parse(self): |
| 122 | # parse through the file, identifying different sections |
| 123 | parse_enum = False |
| 124 | parse_struct = False |
| 125 | member_num = 0 |
| 126 | # TODO : Comment parsing is very fragile but handles 2 known files |
| 127 | block_comment = False |
Tobin Ehlis | 5178bd7 | 2015-03-06 10:38:25 -0700 | [diff] [blame] | 128 | prev_count_name = '' |
Tobin Ehlis | 3764969 | 2016-02-10 15:32:46 -0700 | [diff] [blame] | 129 | ifdef_txt = '' |
| 130 | ifdef_active = 0 |
Ian Elliott | 7e40db9 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 131 | exclude_struct_list = ['VkPlatformHandleXcbKHR', 'VkPlatformHandleX11KHR'] |
Tobin Ehlis | 6442dca | 2014-10-22 15:13:53 -0600 | [diff] [blame] | 132 | with open(self.header_file) as f: |
| 133 | for line in f: |
Tobin Ehlis | 3764969 | 2016-02-10 15:32:46 -0700 | [diff] [blame] | 134 | if True in [ifd_txt in line for ifd_txt in ['#ifdef ', '#ifndef ']]: |
| 135 | ifdef_txt = line.split()[1] |
| 136 | ifdef_active = ifdef_active + 1 |
| 137 | continue |
| 138 | if ifdef_active != 0 and '#endif' in line: |
| 139 | ifdef_active = ifdef_active - 1 |
Tobin Ehlis | 6442dca | 2014-10-22 15:13:53 -0600 | [diff] [blame] | 140 | if block_comment: |
| 141 | if '*/' in line: |
| 142 | block_comment = False |
| 143 | continue |
| 144 | if '/*' in line: |
Courtney Goeltzenleuchter | 82e7239 | 2015-07-10 09:06:56 -0600 | [diff] [blame] | 145 | if '*/' in line: # single line block comment |
| 146 | continue |
Tobin Ehlis | 6442dca | 2014-10-22 15:13:53 -0600 | [diff] [blame] | 147 | block_comment = True |
| 148 | elif 0 == len(line.split()): |
| 149 | #print("Skipping empty line") |
| 150 | continue |
| 151 | elif line.split()[0].strip().startswith("//"): |
Jon Ashburn | fe6113c | 2015-01-09 09:11:44 -0700 | [diff] [blame] | 152 | #print("Skipping commented line %s" % line) |
Tobin Ehlis | 6442dca | 2014-10-22 15:13:53 -0600 | [diff] [blame] | 153 | continue |
| 154 | elif 'typedef enum' in line: |
| 155 | (ty_txt, en_txt, base_type) = line.strip().split(None, 2) |
| 156 | #print("Found ENUM type %s" % base_type) |
Courtney Goeltzenleuchter | 82e7239 | 2015-07-10 09:06:56 -0600 | [diff] [blame] | 157 | if '{' == base_type: |
| 158 | base_type = 'tmp_enum' |
Tobin Ehlis | 6442dca | 2014-10-22 15:13:53 -0600 | [diff] [blame] | 159 | parse_enum = True |
Jon Ashburn | fe6113c | 2015-01-09 09:11:44 -0700 | [diff] [blame] | 160 | default_enum_val = 0 |
Tobin Ehlis | 6442dca | 2014-10-22 15:13:53 -0600 | [diff] [blame] | 161 | self.types_dict[base_type] = 'enum' |
Courtney Goeltzenleuchter | 82e7239 | 2015-07-10 09:06:56 -0600 | [diff] [blame] | 162 | elif 'typedef struct' in line or 'typedef union' in line: |
Jon Ashburn | ea65e49 | 2015-08-06 17:27:49 -0600 | [diff] [blame] | 163 | if True in [ex_type in line for ex_type in exclude_struct_list]: |
| 164 | continue |
Chia-I Wu | 44c99b6 | 2015-10-27 19:55:29 +0800 | [diff] [blame] | 165 | |
Tobin Ehlis | 6442dca | 2014-10-22 15:13:53 -0600 | [diff] [blame] | 166 | (ty_txt, st_txt, base_type) = line.strip().split(None, 2) |
Chia-I Wu | 44c99b6 | 2015-10-27 19:55:29 +0800 | [diff] [blame] | 167 | if ' ' in base_type: |
| 168 | (ignored, base_type) = base_type.strip().split(None, 1) |
| 169 | |
Tobin Ehlis | 6442dca | 2014-10-22 15:13:53 -0600 | [diff] [blame] | 170 | #print("Found STRUCT type: %s" % base_type) |
Cody Northrop | 22b7fae | 2015-08-11 11:21:48 -0600 | [diff] [blame] | 171 | # Note: This really needs to be updated to handle one line struct definition, like |
| 172 | # typedef struct obj##_T { uint64_t handle; } obj; |
| 173 | if ('{' == base_type or not (' ' in base_type)): |
Courtney Goeltzenleuchter | 82e7239 | 2015-07-10 09:06:56 -0600 | [diff] [blame] | 174 | base_type = 'tmp_struct' |
Cody Northrop | 22b7fae | 2015-08-11 11:21:48 -0600 | [diff] [blame] | 175 | parse_struct = True |
| 176 | self.types_dict[base_type] = 'struct' |
Courtney Goeltzenleuchter | 82e7239 | 2015-07-10 09:06:56 -0600 | [diff] [blame] | 177 | # elif 'typedef union' in line: |
| 178 | # (ty_txt, st_txt, base_type) = line.strip().split(None, 2) |
| 179 | # print("Found UNION type: %s" % base_type) |
| 180 | # parse_struct = True |
| 181 | # self.types_dict[base_type] = 'struct' |
Tobin Ehlis | 6442dca | 2014-10-22 15:13:53 -0600 | [diff] [blame] | 182 | elif '}' in line and (parse_enum or parse_struct): |
| 183 | if len(line.split()) > 1: # deals with embedded union in one struct |
| 184 | parse_enum = False |
| 185 | parse_struct = False |
Tobin Ehlis | 5178bd7 | 2015-03-06 10:38:25 -0700 | [diff] [blame] | 186 | self.last_struct_count_name = '' |
Tobin Ehlis | 6442dca | 2014-10-22 15:13:53 -0600 | [diff] [blame] | 187 | member_num = 0 |
Tobin Ehlis | 6442dca | 2014-10-22 15:13:53 -0600 | [diff] [blame] | 188 | (cur_char, targ_type) = line.strip().split(None, 1) |
Courtney Goeltzenleuchter | 82e7239 | 2015-07-10 09:06:56 -0600 | [diff] [blame] | 189 | if 'tmp_struct' == base_type: |
| 190 | base_type = targ_type.strip(';') |
Ian Elliott | 7e40db9 | 2015-08-21 15:09:33 -0600 | [diff] [blame] | 191 | if True in [ex_type in base_type for ex_type in exclude_struct_list]: |
| 192 | del self.struct_dict['tmp_struct'] |
| 193 | continue |
Courtney Goeltzenleuchter | 82e7239 | 2015-07-10 09:06:56 -0600 | [diff] [blame] | 194 | #print("Found Actual Struct type %s" % base_type) |
| 195 | self.struct_dict[base_type] = self.struct_dict['tmp_struct'] |
| 196 | self.struct_dict.pop('tmp_struct', 0) |
Tobin Ehlis | 3764969 | 2016-02-10 15:32:46 -0700 | [diff] [blame] | 197 | struct_order_list.append(base_type) |
Courtney Goeltzenleuchter | 82e7239 | 2015-07-10 09:06:56 -0600 | [diff] [blame] | 198 | self.types_dict[base_type] = 'struct' |
| 199 | self.types_dict.pop('tmp_struct', 0) |
| 200 | elif 'tmp_enum' == base_type: |
| 201 | base_type = targ_type.strip(';') |
| 202 | #print("Found Actual ENUM type %s" % base_type) |
| 203 | for n in self.enum_val_dict: |
| 204 | if 'tmp_enum' == self.enum_val_dict[n]['type']: |
| 205 | self.enum_val_dict[n]['type'] = base_type |
| 206 | # self.enum_val_dict[base_type] = self.enum_val_dict['tmp_enum'] |
| 207 | # self.enum_val_dict.pop('tmp_enum', 0) |
| 208 | self.enum_type_dict[base_type] = self.enum_type_dict['tmp_enum'] |
| 209 | self.enum_type_dict.pop('tmp_enum', 0) |
| 210 | self.types_dict[base_type] = 'enum' |
| 211 | self.types_dict.pop('tmp_enum', 0) |
Tobin Ehlis | 3764969 | 2016-02-10 15:32:46 -0700 | [diff] [blame] | 212 | if ifdef_active: |
| 213 | ifdef_dict[base_type] = ifdef_txt |
Tobin Ehlis | 6442dca | 2014-10-22 15:13:53 -0600 | [diff] [blame] | 214 | self.typedef_fwd_dict[base_type] = targ_type.strip(';') |
| 215 | self.typedef_rev_dict[targ_type.strip(';')] = base_type |
Courtney Goeltzenleuchter | 95487bc | 2015-04-14 18:48:46 -0600 | [diff] [blame] | 216 | #print("fwd_dict: %s = %s" % (base_type, targ_type)) |
Tobin Ehlis | 6442dca | 2014-10-22 15:13:53 -0600 | [diff] [blame] | 217 | elif parse_enum: |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 218 | #if 'VK_MAX_ENUM' not in line and '{' not in line: |
Courtney Goeltzenleuchter | 82e7239 | 2015-07-10 09:06:56 -0600 | [diff] [blame] | 219 | if True not in [ens in line for ens in ['{', '_MAX_ENUM', '_BEGIN_RANGE', '_END_RANGE', '_NUM = ', '_ENUM_RANGE']]: |
Jon Ashburn | fe6113c | 2015-01-09 09:11:44 -0700 | [diff] [blame] | 220 | self._add_enum(line, base_type, default_enum_val) |
| 221 | default_enum_val += 1 |
Tobin Ehlis | 6442dca | 2014-10-22 15:13:53 -0600 | [diff] [blame] | 222 | elif parse_struct: |
| 223 | if ';' in line: |
| 224 | self._add_struct(line, base_type, member_num) |
| 225 | member_num = member_num + 1 |
Mark Lobodzinski | 28b8f02 | 2015-08-27 15:30:29 -0600 | [diff] [blame] | 226 | |
Tobin Ehlis | 6442dca | 2014-10-22 15:13:53 -0600 | [diff] [blame] | 227 | # populate enum dicts based on enum lines |
Jon Ashburn | fe6113c | 2015-01-09 09:11:44 -0700 | [diff] [blame] | 228 | def _add_enum(self, line_txt, enum_type, def_enum_val): |
Tobin Ehlis | 6442dca | 2014-10-22 15:13:53 -0600 | [diff] [blame] | 229 | #print("Parsing enum line %s" % line_txt) |
Jon Ashburn | fe6113c | 2015-01-09 09:11:44 -0700 | [diff] [blame] | 230 | if '=' in line_txt: |
| 231 | (enum_name, eq_char, enum_val) = line_txt.split(None, 2) |
| 232 | else: |
| 233 | enum_name = line_txt.split(',')[0] |
| 234 | enum_val = str(def_enum_val) |
Tobin Ehlis | 6442dca | 2014-10-22 15:13:53 -0600 | [diff] [blame] | 235 | self.enum_val_dict[enum_name] = {} |
| 236 | self.enum_val_dict[enum_name]['type'] = enum_type |
| 237 | # strip comma and comment, then extra split in case of no comma w/ comments |
| 238 | enum_val = enum_val.strip().split(',', 1)[0] |
| 239 | self.enum_val_dict[enum_name]['val'] = enum_val.split()[0] |
Tobin Ehlis | d335065 | 2015-04-22 13:59:43 -0600 | [diff] [blame] | 240 | # Perform conversion of VK_BIT macro |
| 241 | if 'VK_BIT' in self.enum_val_dict[enum_name]['val']: |
| 242 | vk_bit_val = self.enum_val_dict[enum_name]['val'] |
| 243 | bit_shift = int(vk_bit_val[vk_bit_val.find('(')+1:vk_bit_val.find(')')], 0) |
| 244 | self.enum_val_dict[enum_name]['val'] = str(1 << bit_shift) |
| 245 | else: |
| 246 | # account for negative values surrounded by parens |
| 247 | self.enum_val_dict[enum_name]['val'] = self.enum_val_dict[enum_name]['val'].strip(')').replace('-(', '-') |
Tobin Ehlis | 43ebcac | 2014-12-05 12:13:07 -0700 | [diff] [blame] | 248 | # Try to cast to int to determine if enum value is unique |
Tobin Ehlis | 6442dca | 2014-10-22 15:13:53 -0600 | [diff] [blame] | 249 | try: |
| 250 | #print("ENUM val:", self.enum_val_dict[enum_name]['val']) |
| 251 | int(self.enum_val_dict[enum_name]['val'], 0) |
| 252 | self.enum_val_dict[enum_name]['unique'] = True |
| 253 | #print("ENUM has num value") |
| 254 | except ValueError: |
| 255 | self.enum_val_dict[enum_name]['unique'] = False |
| 256 | #print("ENUM is not a number value") |
| 257 | # Update enum_type_dict as well |
| 258 | if not enum_type in self.enum_type_dict: |
| 259 | self.enum_type_dict[enum_type] = [] |
| 260 | self.enum_type_dict[enum_type].append(enum_name) |
Tobin Ehlis | 5178bd7 | 2015-03-06 10:38:25 -0700 | [diff] [blame] | 261 | |
Jon Ashburn | ea65e49 | 2015-08-06 17:27:49 -0600 | [diff] [blame] | 262 | # Return True if struct member is a dynamic array |
Tobin Ehlis | 5178bd7 | 2015-03-06 10:38:25 -0700 | [diff] [blame] | 263 | # RULES : This is a bit quirky based on the API |
| 264 | # NOTE : Changes in API spec may cause these rules to change |
| 265 | # 1. There must be a previous uint var w/ 'count' in the name in the struct |
| 266 | # 2. Dynam array must have 'const' and '*' qualifiers |
| 267 | # 3a. Name of dynam array must end in 's' char OR |
| 268 | # 3b. Name of count var minus 'count' must be contained in name of dynamic array |
| 269 | def _is_dynamic_array(self, full_type, name): |
Tobin Ehlis | 3764969 | 2016-02-10 15:32:46 -0700 | [diff] [blame] | 270 | exceptions = ['pEnabledFeatures', 'pWaitDstStageMask', 'pSampleMask'] |
| 271 | if name in exceptions: |
| 272 | return False |
Tobin Ehlis | 5178bd7 | 2015-03-06 10:38:25 -0700 | [diff] [blame] | 273 | if '' != self.last_struct_count_name: |
| 274 | if 'const' in full_type and '*' in full_type: |
| 275 | if name.endswith('s') or self.last_struct_count_name.lower().replace('count', '') in name.lower(): |
| 276 | return True |
Chia-I Wu | d50a7d7 | 2015-10-26 20:48:51 +0800 | [diff] [blame] | 277 | |
| 278 | # VkWriteDescriptorSet |
| 279 | if self.last_struct_count_name == "descriptorCount": |
| 280 | return True |
| 281 | |
Tobin Ehlis | 5178bd7 | 2015-03-06 10:38:25 -0700 | [diff] [blame] | 282 | return False |
| 283 | |
Tobin Ehlis | 6442dca | 2014-10-22 15:13:53 -0600 | [diff] [blame] | 284 | # populate struct dicts based on struct lines |
| 285 | # TODO : Handle ":" bitfield, "**" ptr->ptr and "const type*const*" |
| 286 | def _add_struct(self, line_txt, struct_type, num): |
| 287 | #print("Parsing struct line %s" % line_txt) |
Courtney Goeltzenleuchter | 82e7239 | 2015-07-10 09:06:56 -0600 | [diff] [blame] | 288 | if '{' == struct_type: |
| 289 | print("Parsing struct '{' w/ line %s" % line_txt) |
Tobin Ehlis | 6442dca | 2014-10-22 15:13:53 -0600 | [diff] [blame] | 290 | if not struct_type in self.struct_dict: |
| 291 | self.struct_dict[struct_type] = {} |
| 292 | members = line_txt.strip().split(';', 1)[0] # first strip semicolon & comments |
| 293 | # TODO : Handle bitfields more correctly |
| 294 | members = members.strip().split(':', 1)[0] # strip bitfield element |
| 295 | (member_type, member_name) = members.rsplit(None, 1) |
Tobin Ehlis | 5178bd7 | 2015-03-06 10:38:25 -0700 | [diff] [blame] | 296 | # Store counts to help recognize and size dynamic arrays |
Mark Lobodzinski | c3c85f5 | 2016-12-05 13:27:29 -0700 | [diff] [blame] | 297 | # Add special case for pObjectEntryCounts -- though it meets the criteria for a 'count', it should not |
| 298 | # replace the previously identified (and correct) objectCount. |
| 299 | # TODO: convert to using vk.xml and avoid parsing the header |
| 300 | if 'count' in member_name.lower() and 'samplecount' != member_name.lower() and 'uint' in member_type and member_name != "pObjectEntryCounts": |
Tobin Ehlis | 5178bd7 | 2015-03-06 10:38:25 -0700 | [diff] [blame] | 301 | self.last_struct_count_name = member_name |
Tobin Ehlis | 6442dca | 2014-10-22 15:13:53 -0600 | [diff] [blame] | 302 | self.struct_dict[struct_type][num] = {} |
| 303 | self.struct_dict[struct_type][num]['full_type'] = member_type |
Tobin Ehlis | 5178bd7 | 2015-03-06 10:38:25 -0700 | [diff] [blame] | 304 | self.struct_dict[struct_type][num]['dyn_array'] = False |
Tobin Ehlis | 6442dca | 2014-10-22 15:13:53 -0600 | [diff] [blame] | 305 | if '*' in member_type: |
| 306 | self.struct_dict[struct_type][num]['ptr'] = True |
Tobin Ehlis | 5178bd7 | 2015-03-06 10:38:25 -0700 | [diff] [blame] | 307 | # TODO : Need more general purpose way here to reduce down to basic type |
| 308 | member_type = member_type.replace(' const*', '') |
Tobin Ehlis | 6442dca | 2014-10-22 15:13:53 -0600 | [diff] [blame] | 309 | member_type = member_type.strip('*') |
| 310 | else: |
| 311 | self.struct_dict[struct_type][num]['ptr'] = False |
| 312 | if 'const' in member_type: |
| 313 | self.struct_dict[struct_type][num]['const'] = True |
Courtney Goeltzenleuchter | 95487bc | 2015-04-14 18:48:46 -0600 | [diff] [blame] | 314 | member_type = member_type.replace('const', '').strip() |
Tobin Ehlis | 6442dca | 2014-10-22 15:13:53 -0600 | [diff] [blame] | 315 | else: |
| 316 | self.struct_dict[struct_type][num]['const'] = False |
Tobin Ehlis | 5a487e5 | 2014-11-11 12:28:12 -0700 | [diff] [blame] | 317 | # TODO : There is a bug here where it seems that at the time we do this check, |
| 318 | # the data is not in the types or typedef_rev_dict, so we never pass this if check |
Tobin Ehlis | 6442dca | 2014-10-22 15:13:53 -0600 | [diff] [blame] | 319 | if is_type(member_type, 'struct'): |
| 320 | self.struct_dict[struct_type][num]['struct'] = True |
| 321 | else: |
| 322 | self.struct_dict[struct_type][num]['struct'] = False |
| 323 | self.struct_dict[struct_type][num]['type'] = member_type |
| 324 | if '[' in member_name: |
| 325 | (member_name, array_size) = member_name.split('[', 1) |
Tobin Ehlis | 3764969 | 2016-02-10 15:32:46 -0700 | [diff] [blame] | 326 | #if 'char' in member_type: |
| 327 | # self.struct_dict[struct_type][num]['array'] = False |
| 328 | # self.struct_dict[struct_type][num]['array_size'] = 0 |
| 329 | # self.struct_dict[struct_type][num]['ptr'] = True |
| 330 | #else: |
| 331 | self.struct_dict[struct_type][num]['array'] = True |
| 332 | self.struct_dict[struct_type][num]['array_size'] = array_size.strip(']') |
Tobin Ehlis | 5178bd7 | 2015-03-06 10:38:25 -0700 | [diff] [blame] | 333 | elif self._is_dynamic_array(self.struct_dict[struct_type][num]['full_type'], member_name): |
| 334 | #print("Found dynamic array %s of size %s" % (member_name, self.last_struct_count_name)) |
| 335 | self.struct_dict[struct_type][num]['array'] = True |
| 336 | self.struct_dict[struct_type][num]['dyn_array'] = True |
| 337 | self.struct_dict[struct_type][num]['array_size'] = self.last_struct_count_name |
Tobin Ehlis | b8013a2 | 2015-02-26 12:57:08 -0700 | [diff] [blame] | 338 | elif not 'array' in self.struct_dict[struct_type][num]: |
Tobin Ehlis | 6442dca | 2014-10-22 15:13:53 -0600 | [diff] [blame] | 339 | self.struct_dict[struct_type][num]['array'] = False |
| 340 | self.struct_dict[struct_type][num]['array_size'] = 0 |
| 341 | self.struct_dict[struct_type][num]['name'] = member_name |
| 342 | |
Tobin Ehlis | 5178bd7 | 2015-03-06 10:38:25 -0700 | [diff] [blame] | 343 | # check if given identifier is of specified type_to_check |
Tobin Ehlis | 6442dca | 2014-10-22 15:13:53 -0600 | [diff] [blame] | 344 | def is_type(identifier, type_to_check): |
Tobin Ehlis | 6442dca | 2014-10-22 15:13:53 -0600 | [diff] [blame] | 345 | if identifier in types_dict and type_to_check == types_dict[identifier]: |
| 346 | return True |
| 347 | if identifier in typedef_rev_dict: |
| 348 | new_id = typedef_rev_dict[identifier] |
| 349 | if new_id in types_dict and type_to_check == types_dict[new_id]: |
| 350 | return True |
| 351 | return False |
| 352 | |
| 353 | # This is a validation function to verify that we can reproduce the original structs |
| 354 | def recreate_structs(): |
| 355 | for struct_name in struct_dict: |
| 356 | sys.stdout.write("typedef struct %s\n{\n" % struct_name) |
| 357 | for mem_num in sorted(struct_dict[struct_name]): |
| 358 | sys.stdout.write(" ") |
| 359 | if struct_dict[struct_name][mem_num]['const']: |
| 360 | sys.stdout.write("const ") |
| 361 | #if struct_dict[struct_name][mem_num]['struct']: |
| 362 | # sys.stdout.write("struct ") |
| 363 | sys.stdout.write (struct_dict[struct_name][mem_num]['type']) |
| 364 | if struct_dict[struct_name][mem_num]['ptr']: |
| 365 | sys.stdout.write("*") |
| 366 | sys.stdout.write(" ") |
| 367 | sys.stdout.write(struct_dict[struct_name][mem_num]['name']) |
| 368 | if struct_dict[struct_name][mem_num]['array']: |
| 369 | sys.stdout.write("[") |
| 370 | sys.stdout.write(struct_dict[struct_name][mem_num]['array_size']) |
| 371 | sys.stdout.write("]") |
| 372 | sys.stdout.write(";\n") |
| 373 | sys.stdout.write("} ") |
| 374 | sys.stdout.write(typedef_fwd_dict[struct_name]) |
| 375 | sys.stdout.write(";\n\n") |
| 376 | |
Courtney Goeltzenleuchter | 95487bc | 2015-04-14 18:48:46 -0600 | [diff] [blame] | 377 | # |
Courtney Goeltzenleuchter | fb4efc6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 378 | # TODO: Fix construction of struct name |
Courtney Goeltzenleuchter | 95487bc | 2015-04-14 18:48:46 -0600 | [diff] [blame] | 379 | def get_struct_name_from_struct_type(struct_type): |
Courtney Goeltzenleuchter | fb4efc6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 380 | # Note: All struct types are now camel-case |
Karl Schultz | 7b74e11 | 2016-02-12 13:50:13 -0700 | [diff] [blame] | 381 | # Debug Report has an inconsistency - so need special case. |
Courtney Goeltzenleuchter | 95487bc | 2015-04-14 18:48:46 -0600 | [diff] [blame] | 382 | caps_struct_name = struct_type.replace("_STRUCTURE_TYPE", "") |
Courtney Goeltzenleuchter | fb4efc6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 383 | char_idx = 0 |
| 384 | struct_name = '' |
| 385 | for char in caps_struct_name: |
| 386 | if (0 == char_idx) or (caps_struct_name[char_idx-1] == '_'): |
| 387 | struct_name += caps_struct_name[char_idx] |
| 388 | elif (caps_struct_name[char_idx] == '_'): |
| 389 | pass |
| 390 | else: |
| 391 | struct_name += caps_struct_name[char_idx].lower() |
| 392 | char_idx += 1 |
Chia-I Wu | 3432a0c | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 393 | |
David Pinedo | 7249724 | 2016-11-17 15:03:16 -0700 | [diff] [blame] | 394 | # Vendor extension structs ending in vendor TLA need to be uppercase. |
| 395 | if (caps_struct_name[-2:] == "NV"): |
| 396 | struct_name = struct_name[:-2] + caps_struct_name[-2:] |
| 397 | if ((caps_struct_name[-3:] == "AMD") or (caps_struct_name[-3:] == "IMG") or (caps_struct_name[-3:] == "EXT")): |
| 398 | struct_name = struct_name[:-3] + caps_struct_name[-3:] |
| 399 | |
Courtney Goeltzenleuchter | 95487bc | 2015-04-14 18:48:46 -0600 | [diff] [blame] | 400 | return struct_name |
| 401 | |
Cody Northrop | 2bd41b3 | 2016-04-19 10:37:36 -0600 | [diff] [blame] | 402 | # Emit an ifdef if incoming func matches a platform identifier |
| 403 | def add_platform_wrapper_entry(list, func): |
| 404 | if (re.match(r'.*Xlib.*', func)): |
| 405 | list.append("#ifdef VK_USE_PLATFORM_XLIB_KHR") |
| 406 | if (re.match(r'.*Xcb.*', func)): |
| 407 | list.append("#ifdef VK_USE_PLATFORM_XCB_KHR") |
| 408 | if (re.match(r'.*Wayland.*', func)): |
| 409 | list.append("#ifdef VK_USE_PLATFORM_WAYLAND_KHR") |
| 410 | if (re.match(r'.*Mir.*', func)): |
| 411 | list.append("#ifdef VK_USE_PLATFORM_MIR_KHR") |
| 412 | if (re.match(r'.*Android.*', func)): |
| 413 | list.append("#ifdef VK_USE_PLATFORM_ANDROID_KHR") |
| 414 | if (re.match(r'.*Win32.*', func)): |
| 415 | list.append("#ifdef VK_USE_PLATFORM_WIN32_KHR") |
| 416 | |
| 417 | # Emit an endif if incoming func matches a platform identifier |
| 418 | def add_platform_wrapper_exit(list, func): |
| 419 | if (re.match(r'.*Xlib.*', func)): |
| 420 | list.append("#endif //VK_USE_PLATFORM_XLIB_KHR") |
| 421 | if (re.match(r'.*Xcb.*', func)): |
| 422 | list.append("#endif //VK_USE_PLATFORM_XCB_KHR") |
| 423 | if (re.match(r'.*Wayland.*', func)): |
| 424 | list.append("#endif //VK_USE_PLATFORM_WAYLAND_KHR") |
| 425 | if (re.match(r'.*Mir.*', func)): |
| 426 | list.append("#endif //VK_USE_PLATFORM_MIR_KHR") |
| 427 | if (re.match(r'.*Android.*', func)): |
| 428 | list.append("#endif //VK_USE_PLATFORM_ANDROID_KHR") |
| 429 | if (re.match(r'.*Win32.*', func)): |
| 430 | list.append("#endif //VK_USE_PLATFORM_WIN32_KHR") |
| 431 | |
Tobin Ehlis | 6442dca | 2014-10-22 15:13:53 -0600 | [diff] [blame] | 432 | # class for writing common file elements |
| 433 | # Here's how this class lays out a file: |
| 434 | # COPYRIGHT |
| 435 | # HEADER |
| 436 | # BODY |
| 437 | # FOOTER |
| 438 | # |
| 439 | # For each of these sections, there's a "set*" function |
| 440 | # The class as a whole has a generate function which will write each section in order |
| 441 | class CommonFileGen: |
| 442 | def __init__(self, filename=None, copyright_txt="", header_txt="", body_txt="", footer_txt=""): |
| 443 | self.filename = filename |
| 444 | self.contents = {'copyright': copyright_txt, 'header': header_txt, 'body': body_txt, 'footer': footer_txt} |
| 445 | # TODO : Set a default copyright & footer at least |
Mark Lobodzinski | 28b8f02 | 2015-08-27 15:30:29 -0600 | [diff] [blame] | 446 | |
Tobin Ehlis | 6442dca | 2014-10-22 15:13:53 -0600 | [diff] [blame] | 447 | def setFilename(self, filename): |
| 448 | self.filename = filename |
Mark Lobodzinski | 28b8f02 | 2015-08-27 15:30:29 -0600 | [diff] [blame] | 449 | |
Tobin Ehlis | 6442dca | 2014-10-22 15:13:53 -0600 | [diff] [blame] | 450 | def setCopyright(self, c): |
| 451 | self.contents['copyright'] = c |
Mark Lobodzinski | 28b8f02 | 2015-08-27 15:30:29 -0600 | [diff] [blame] | 452 | |
Tobin Ehlis | 6442dca | 2014-10-22 15:13:53 -0600 | [diff] [blame] | 453 | def setHeader(self, h): |
| 454 | self.contents['header'] = h |
Mark Lobodzinski | 28b8f02 | 2015-08-27 15:30:29 -0600 | [diff] [blame] | 455 | |
Tobin Ehlis | 6442dca | 2014-10-22 15:13:53 -0600 | [diff] [blame] | 456 | def setBody(self, b): |
| 457 | self.contents['body'] = b |
Mark Lobodzinski | 28b8f02 | 2015-08-27 15:30:29 -0600 | [diff] [blame] | 458 | |
Tobin Ehlis | 6442dca | 2014-10-22 15:13:53 -0600 | [diff] [blame] | 459 | def setFooter(self, f): |
| 460 | self.contents['footer'] = f |
Mark Lobodzinski | 28b8f02 | 2015-08-27 15:30:29 -0600 | [diff] [blame] | 461 | |
Tobin Ehlis | 6442dca | 2014-10-22 15:13:53 -0600 | [diff] [blame] | 462 | def generate(self): |
| 463 | #print("Generate to file %s" % self.filename) |
| 464 | with open(self.filename, "w") as f: |
| 465 | f.write(self.contents['copyright']) |
| 466 | f.write(self.contents['header']) |
| 467 | f.write(self.contents['body']) |
| 468 | f.write(self.contents['footer']) |
| 469 | |
| 470 | # class for writing a wrapper class for structures |
| 471 | # The wrapper class wraps the structs and includes utility functions for |
| 472 | # setting/getting member values and displaying the struct data in various formats |
| 473 | class StructWrapperGen: |
Jamie Madill | 9c04736 | 2016-12-14 13:05:05 -0500 | [diff] [blame] | 474 | def __init__(self, in_struct_dict, prefix, out_dir, quiet): |
Tobin Ehlis | 6442dca | 2014-10-22 15:13:53 -0600 | [diff] [blame] | 475 | self.struct_dict = in_struct_dict |
| 476 | self.include_headers = [] |
Jon Ashburn | ea65e49 | 2015-08-06 17:27:49 -0600 | [diff] [blame] | 477 | self.lineinfo = sourcelineinfo() |
Tobin Ehlis | 6442dca | 2014-10-22 15:13:53 -0600 | [diff] [blame] | 478 | self.api = prefix |
Courtney Goeltzenleuchter | dba7998 | 2015-04-14 16:30:53 -0600 | [diff] [blame] | 479 | if prefix.lower() == "vulkan": |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 480 | self.api_prefix = "vk" |
| 481 | else: |
| 482 | self.api_prefix = prefix |
Tobin Ehlis | 3764969 | 2016-02-10 15:32:46 -0700 | [diff] [blame] | 483 | self.safe_struct_header_filename = os.path.join(out_dir, self.api_prefix+"_safe_struct.h") |
| 484 | self.safe_struct_source_filename = os.path.join(out_dir, self.api_prefix+"_safe_struct.cpp") |
Tobin Ehlis | 3764969 | 2016-02-10 15:32:46 -0700 | [diff] [blame] | 485 | # Safe Struct (ss) header and source files |
| 486 | self.ssh = CommonFileGen(self.safe_struct_header_filename) |
| 487 | self.sss = CommonFileGen(self.safe_struct_source_filename) |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 488 | self.size_helper_filename = os.path.join(out_dir, self.api_prefix+"_struct_size_helper.h") |
| 489 | self.size_helper_c_filename = os.path.join(out_dir, self.api_prefix+"_struct_size_helper.c") |
Tobin Ehlis | 6035477 | 2015-03-12 14:50:40 -0600 | [diff] [blame] | 490 | self.size_helper_gen = CommonFileGen(self.size_helper_filename) |
Peter Lohrmann | 4a8d891 | 2015-04-03 11:43:06 -0700 | [diff] [blame] | 491 | self.size_helper_c_gen = CommonFileGen(self.size_helper_c_filename) |
Tobin Ehlis | 6442dca | 2014-10-22 15:13:53 -0600 | [diff] [blame] | 492 | self.header_txt = "" |
| 493 | self.definition_txt = "" |
Jamie Madill | 9c04736 | 2016-12-14 13:05:05 -0500 | [diff] [blame] | 494 | self.quiet = quiet |
Mark Lobodzinski | 28b8f02 | 2015-08-27 15:30:29 -0600 | [diff] [blame] | 495 | |
Tobin Ehlis | 6442dca | 2014-10-22 15:13:53 -0600 | [diff] [blame] | 496 | def set_include_headers(self, include_headers): |
| 497 | self.include_headers = include_headers |
| 498 | |
| 499 | # Return class name for given struct name |
| 500 | def get_class_name(self, struct_name): |
| 501 | class_name = struct_name.strip('_').lower() + "_struct_wrapper" |
| 502 | return class_name |
Mark Lobodzinski | 28b8f02 | 2015-08-27 15:30:29 -0600 | [diff] [blame] | 503 | |
Tobin Ehlis | 6442dca | 2014-10-22 15:13:53 -0600 | [diff] [blame] | 504 | def get_file_list(self): |
| 505 | return [os.path.basename(self.header_filename), os.path.basename(self.class_filename), os.path.basename(self.string_helper_filename)] |
| 506 | |
Tobin Ehlis | 3764969 | 2016-02-10 15:32:46 -0700 | [diff] [blame] | 507 | # Safe Structs are versions of vulkan structs with non-const safe ptrs |
| 508 | # that make shadowing structures and clean-up of shadowed structures very simple |
| 509 | def generateSafeStructHeader(self): |
| 510 | self.ssh.setCopyright(self._generateCopyright()) |
| 511 | self.ssh.setHeader(self._generateSafeStructHeader()) |
| 512 | self.ssh.setBody(self._generateSafeStructDecls()) |
| 513 | self.ssh.generate() |
| 514 | |
| 515 | def generateSafeStructs(self): |
| 516 | self.sss.setCopyright(self._generateCopyright()) |
| 517 | self.sss.setHeader(self._generateSafeStructSourceHeader()) |
| 518 | self.sss.setBody(self._generateSafeStructSource()) |
| 519 | self.sss.generate() |
| 520 | |
Tobin Ehlis | 6035477 | 2015-03-12 14:50:40 -0600 | [diff] [blame] | 521 | def generateSizeHelper(self): |
Jamie Madill | 9c04736 | 2016-12-14 13:05:05 -0500 | [diff] [blame] | 522 | if not self.quiet: |
| 523 | print("Generating struct size helper") |
Tobin Ehlis | 6035477 | 2015-03-12 14:50:40 -0600 | [diff] [blame] | 524 | self.size_helper_gen.setCopyright(self._generateCopyright()) |
| 525 | self.size_helper_gen.setHeader(self._generateSizeHelperHeader()) |
| 526 | self.size_helper_gen.setBody(self._generateSizeHelperFunctions()) |
Cody Northrop | fa2f36c | 2016-09-22 14:39:16 -0600 | [diff] [blame] | 527 | self.size_helper_gen.setFooter(self._generateSizeHelperFooter()) |
Tobin Ehlis | 6035477 | 2015-03-12 14:50:40 -0600 | [diff] [blame] | 528 | self.size_helper_gen.generate() |
| 529 | |
Peter Lohrmann | 4a8d891 | 2015-04-03 11:43:06 -0700 | [diff] [blame] | 530 | def generateSizeHelperC(self): |
Jamie Madill | 9c04736 | 2016-12-14 13:05:05 -0500 | [diff] [blame] | 531 | if not self.quiet: |
| 532 | print("Generating struct size helper c") |
Peter Lohrmann | 4a8d891 | 2015-04-03 11:43:06 -0700 | [diff] [blame] | 533 | self.size_helper_c_gen.setCopyright(self._generateCopyright()) |
| 534 | self.size_helper_c_gen.setHeader(self._generateSizeHelperHeaderC()) |
| 535 | self.size_helper_c_gen.setBody(self._generateSizeHelperFunctionsC()) |
| 536 | self.size_helper_c_gen.generate() |
| 537 | |
Tobin Ehlis | 6442dca | 2014-10-22 15:13:53 -0600 | [diff] [blame] | 538 | def _generateCopyright(self): |
Peter Lohrmann | 4a8d891 | 2015-04-03 11:43:06 -0700 | [diff] [blame] | 539 | copyright = [] |
| 540 | copyright.append('/* THIS FILE IS GENERATED. DO NOT EDIT. */'); |
| 541 | copyright.append(''); |
| 542 | copyright.append('/*'); |
Ian Elliott | 0977abc | 2015-04-15 11:15:13 -0600 | [diff] [blame] | 543 | copyright.append(' * Vulkan'); |
Peter Lohrmann | 4a8d891 | 2015-04-03 11:43:06 -0700 | [diff] [blame] | 544 | copyright.append(' *'); |
Karl Schultz | 8e42f40 | 2016-02-02 19:32:33 -0700 | [diff] [blame] | 545 | copyright.append(' * Copyright (c) 2015-2016 The Khronos Group Inc.'); |
| 546 | copyright.append(' * Copyright (c) 2015-2016 Valve Corporation.'); |
| 547 | copyright.append(' * Copyright (c) 2015-2016 LunarG, Inc.'); |
| 548 | copyright.append(' * Copyright (c) 2015-2016 Google Inc.'); |
Peter Lohrmann | 4a8d891 | 2015-04-03 11:43:06 -0700 | [diff] [blame] | 549 | copyright.append(' *'); |
Jon Ashburn | 3ebf125 | 2016-04-19 11:30:31 -0600 | [diff] [blame] | 550 | copyright.append(' * Licensed under the Apache License, Version 2.0 (the "License");'); |
| 551 | copyright.append(' * you may not use this file except in compliance with the License.'); |
| 552 | copyright.append(' * You may obtain a copy of the License at'); |
Peter Lohrmann | 4a8d891 | 2015-04-03 11:43:06 -0700 | [diff] [blame] | 553 | copyright.append(' *'); |
Jon Ashburn | 3ebf125 | 2016-04-19 11:30:31 -0600 | [diff] [blame] | 554 | copyright.append(' * http://www.apache.org/licenses/LICENSE-2.0'); |
Peter Lohrmann | 4a8d891 | 2015-04-03 11:43:06 -0700 | [diff] [blame] | 555 | copyright.append(' *'); |
Jon Ashburn | 3ebf125 | 2016-04-19 11:30:31 -0600 | [diff] [blame] | 556 | copyright.append(' * Unless required by applicable law or agreed to in writing, software'); |
| 557 | copyright.append(' * distributed under the License is distributed on an "AS IS" BASIS,'); |
| 558 | copyright.append(' * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.'); |
| 559 | copyright.append(' * See the License for the specific language governing permissions and'); |
| 560 | copyright.append(' * limitations under the License.'); |
Karl Schultz | 8e42f40 | 2016-02-02 19:32:33 -0700 | [diff] [blame] | 561 | copyright.append(' *'); |
| 562 | copyright.append(' * Author: Courtney Goeltzenleuchter <courtney@LunarG.com>'); |
| 563 | copyright.append(' * Author: Tobin Ehlis <tobin@lunarg.com>'); |
Peter Lohrmann | 4a8d891 | 2015-04-03 11:43:06 -0700 | [diff] [blame] | 564 | copyright.append(' */'); |
| 565 | copyright.append(''); |
| 566 | return "\n".join(copyright) |
Tobin Ehlis | 6442dca | 2014-10-22 15:13:53 -0600 | [diff] [blame] | 567 | |
| 568 | def _generateCppHeader(self): |
| 569 | header = [] |
| 570 | header.append("//#includes, #defines, globals and such...\n") |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 571 | header.append("#include <stdio.h>\n#include <%s>\n#include <%s_enum_string_helper.h>\n" % (os.path.basename(self.header_filename), self.api_prefix)) |
Tobin Ehlis | 6442dca | 2014-10-22 15:13:53 -0600 | [diff] [blame] | 572 | return "".join(header) |
Mark Lobodzinski | 28b8f02 | 2015-08-27 15:30:29 -0600 | [diff] [blame] | 573 | |
Tobin Ehlis | 14ff085 | 2014-12-17 17:44:50 -0700 | [diff] [blame] | 574 | def _get_func_name(self, struct, mid_str): |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 575 | return "%s_%s_%s" % (self.api_prefix, mid_str, struct.lower().strip("_")) |
Tobin Ehlis | 14ff085 | 2014-12-17 17:44:50 -0700 | [diff] [blame] | 576 | |
Tobin Ehlis | 6035477 | 2015-03-12 14:50:40 -0600 | [diff] [blame] | 577 | def _get_size_helper_func_name(self, struct): |
| 578 | return self._get_func_name(struct, 'size') |
| 579 | |
Tobin Ehlis | 6035477 | 2015-03-12 14:50:40 -0600 | [diff] [blame] | 580 | def _generateSizeHelperFunctions(self): |
| 581 | sh_funcs = [] |
Peter Lohrmann | 4a8d891 | 2015-04-03 11:43:06 -0700 | [diff] [blame] | 582 | # just generates prototypes for all the functions |
Peter Lohrmann | 49ea184 | 2015-03-30 16:29:56 -0700 | [diff] [blame] | 583 | for s in sorted(self.struct_dict): |
Cody Northrop | 2bd41b3 | 2016-04-19 10:37:36 -0600 | [diff] [blame] | 584 | |
| 585 | # Wrap this in platform check since it may contain undefined structs or functions |
| 586 | add_platform_wrapper_entry(sh_funcs, typedef_fwd_dict[s]) |
| 587 | sh_funcs.append('size_t %s(const %s* pStruct);' % (self._get_size_helper_func_name(s), typedef_fwd_dict[s])) |
| 588 | add_platform_wrapper_exit(sh_funcs, typedef_fwd_dict[s]) |
| 589 | |
Peter Lohrmann | 4a8d891 | 2015-04-03 11:43:06 -0700 | [diff] [blame] | 590 | return "\n".join(sh_funcs) |
| 591 | |
Peter Lohrmann | 4a8d891 | 2015-04-03 11:43:06 -0700 | [diff] [blame] | 592 | def _generateSizeHelperFunctionsC(self): |
| 593 | sh_funcs = [] |
| 594 | # generate function definitions |
Peter Lohrmann | 49ea184 | 2015-03-30 16:29:56 -0700 | [diff] [blame] | 595 | for s in sorted(self.struct_dict): |
Cody Northrop | 2bd41b3 | 2016-04-19 10:37:36 -0600 | [diff] [blame] | 596 | |
| 597 | # Wrap this in platform check since it may contain undefined structs or functions |
| 598 | add_platform_wrapper_entry(sh_funcs, typedef_fwd_dict[s]) |
| 599 | |
Tobin Ehlis | 6dd0fc3 | 2016-02-12 14:37:09 -0700 | [diff] [blame] | 600 | skip_list = [] # Used when struct elements need to be skipped because size already accounted for |
Tobin Ehlis | 6035477 | 2015-03-12 14:50:40 -0600 | [diff] [blame] | 601 | sh_funcs.append('size_t %s(const %s* pStruct)\n{' % (self._get_size_helper_func_name(s), typedef_fwd_dict[s])) |
| 602 | indent = ' ' |
| 603 | sh_funcs.append('%ssize_t structSize = 0;' % (indent)) |
| 604 | sh_funcs.append('%sif (pStruct) {' % (indent)) |
| 605 | indent = ' ' |
| 606 | sh_funcs.append('%sstructSize = sizeof(%s);' % (indent, typedef_fwd_dict[s])) |
| 607 | i_decl = False |
| 608 | for m in sorted(self.struct_dict[s]): |
| 609 | if m in skip_list: |
| 610 | continue |
| 611 | if self.struct_dict[s][m]['dyn_array']: |
| 612 | if self.struct_dict[s][m]['full_type'].count('*') > 1: |
| 613 | if not is_type(self.struct_dict[s][m]['type'], 'struct') and not 'char' in self.struct_dict[s][m]['type'].lower(): |
Chia-I Wu | 3432a0c | 2015-10-27 18:04:07 +0800 | [diff] [blame] | 614 | if 'ppMemoryBarriers' == self.struct_dict[s][m]['name']: |
Tobin Ehlis | 0411292 | 2015-03-16 10:44:40 -0600 | [diff] [blame] | 615 | # TODO : For now be conservative and consider all memBarrier ptrs as largest possible struct |
Courtney Goeltzenleuchter | fb4efc6 | 2015-04-10 08:34:15 -0600 | [diff] [blame] | 616 | sh_funcs.append('%sstructSize += pStruct->%s*(sizeof(%s*) + sizeof(VkImageMemoryBarrier));' % (indent, self.struct_dict[s][m]['array_size'], self.struct_dict[s][m]['type'])) |
Tobin Ehlis | 0411292 | 2015-03-16 10:44:40 -0600 | [diff] [blame] | 617 | else: |
| 618 | sh_funcs.append('%sstructSize += pStruct->%s*(sizeof(%s*) + sizeof(%s));' % (indent, self.struct_dict[s][m]['array_size'], self.struct_dict[s][m]['type'], self.struct_dict[s][m]['type'])) |
Tobin Ehlis | 6035477 | 2015-03-12 14:50:40 -0600 | [diff] [blame] | 619 | else: # This is an array of char* or array of struct ptrs |
| 620 | if not i_decl: |
| 621 | sh_funcs.append('%suint32_t i = 0;' % (indent)) |
| 622 | i_decl = True |
| 623 | sh_funcs.append('%sfor (i = 0; i < pStruct->%s; i++) {' % (indent, self.struct_dict[s][m]['array_size'])) |
| 624 | indent = ' ' |
| 625 | if is_type(self.struct_dict[s][m]['type'], 'struct'): |
| 626 | sh_funcs.append('%sstructSize += (sizeof(%s*) + %s(pStruct->%s[i]));' % (indent, self.struct_dict[s][m]['type'], self._get_size_helper_func_name(self.struct_dict[s][m]['type']), self.struct_dict[s][m]['name'])) |
| 627 | else: |
Tobin Ehlis | bd6a916 | 2015-03-13 07:18:05 -0600 | [diff] [blame] | 628 | sh_funcs.append('%sstructSize += (sizeof(char*) + (sizeof(char) * (1 + strlen(pStruct->%s[i]))));' % (indent, self.struct_dict[s][m]['name'])) |
Tobin Ehlis | 6035477 | 2015-03-12 14:50:40 -0600 | [diff] [blame] | 629 | indent = ' ' |
| 630 | sh_funcs.append('%s}' % (indent)) |
| 631 | else: |
| 632 | if is_type(self.struct_dict[s][m]['type'], 'struct'): |
| 633 | if not i_decl: |
| 634 | sh_funcs.append('%suint32_t i = 0;' % (indent)) |
| 635 | i_decl = True |
| 636 | sh_funcs.append('%sfor (i = 0; i < pStruct->%s; i++) {' % (indent, self.struct_dict[s][m]['array_size'])) |
| 637 | indent = ' ' |
| 638 | sh_funcs.append('%sstructSize += %s(&pStruct->%s[i]);' % (indent, self._get_size_helper_func_name(self.struct_dict[s][m]['type']), self.struct_dict[s][m]['name'])) |
| 639 | indent = ' ' |
| 640 | sh_funcs.append('%s}' % (indent)) |
| 641 | else: |
| 642 | sh_funcs.append('%sstructSize += pStruct->%s*sizeof(%s);' % (indent, self.struct_dict[s][m]['array_size'], self.struct_dict[s][m]['type'])) |
David Pinedo | 7249724 | 2016-11-17 15:03:16 -0700 | [diff] [blame] | 643 | elif self.struct_dict[s][m]['ptr'] and 'pNext' != self.struct_dict[s][m]['name'] and 'dpy' != self.struct_dict[s][m]['name']: |
Tobin Ehlis | 6035477 | 2015-03-12 14:50:40 -0600 | [diff] [blame] | 644 | if 'char' in self.struct_dict[s][m]['type'].lower(): |
Karl Schultz | c3f0963 | 2016-01-27 16:03:49 -0700 | [diff] [blame] | 645 | sh_funcs.append('%sstructSize += (pStruct->%s != NULL) ? sizeof(%s)*(1+strlen(pStruct->%s)) : 0;' % (indent, self.struct_dict[s][m]['name'], self.struct_dict[s][m]['type'], self.struct_dict[s][m]['name'])) |
Tobin Ehlis | 6035477 | 2015-03-12 14:50:40 -0600 | [diff] [blame] | 646 | elif is_type(self.struct_dict[s][m]['type'], 'struct'): |
| 647 | sh_funcs.append('%sstructSize += %s(pStruct->%s);' % (indent, self._get_size_helper_func_name(self.struct_dict[s][m]['type']), self.struct_dict[s][m]['name'])) |
Tobin Ehlis | 0411292 | 2015-03-16 10:44:40 -0600 | [diff] [blame] | 648 | elif 'void' not in self.struct_dict[s][m]['type'].lower(): |
Jon Ashburn | 81b1d9c | 2015-12-29 14:21:25 -0700 | [diff] [blame] | 649 | if (self.struct_dict[s][m]['type'] != 'xcb_connection_t'): |
| 650 | sh_funcs.append('%sstructSize += sizeof(%s);' % (indent, self.struct_dict[s][m]['type'])) |
Tobin Ehlis | 6035477 | 2015-03-12 14:50:40 -0600 | [diff] [blame] | 651 | elif 'size_t' == self.struct_dict[s][m]['type'].lower(): |
| 652 | sh_funcs.append('%sstructSize += pStruct->%s;' % (indent, self.struct_dict[s][m]['name'])) |
| 653 | skip_list.append(m+1) |
| 654 | indent = ' ' |
| 655 | sh_funcs.append('%s}' % (indent)) |
| 656 | sh_funcs.append("%sreturn structSize;\n}" % (indent)) |
Cody Northrop | 2bd41b3 | 2016-04-19 10:37:36 -0600 | [diff] [blame] | 657 | |
| 658 | # End of platform wrapped section |
| 659 | add_platform_wrapper_exit(sh_funcs, typedef_fwd_dict[s]) |
| 660 | |
Tobin Ehlis | 0411292 | 2015-03-16 10:44:40 -0600 | [diff] [blame] | 661 | # Now generate generic functions to loop over entire struct chain (or just handle single generic structs) |
Mark Lobodzinski | 66cdd5d | 2016-12-05 15:27:44 -0700 | [diff] [blame] | 662 | for follow_chain in [True, False]: |
| 663 | sh_funcs.append('%s' % self.lineinfo.get()) |
| 664 | if follow_chain: |
| 665 | sh_funcs.append('size_t get_struct_chain_size(const void* pStruct)\n{') |
| 666 | else: |
| 667 | sh_funcs.append('size_t get_dynamic_struct_size(const void* pStruct)\n{') |
| 668 | indent = ' ' |
| 669 | sh_funcs.append('%s// Just use VkApplicationInfo as struct until actual type is resolved' % (indent)) |
| 670 | sh_funcs.append('%sVkApplicationInfo* pNext = (VkApplicationInfo*)pStruct;' % (indent)) |
| 671 | sh_funcs.append('%ssize_t structSize = 0;' % (indent)) |
| 672 | if follow_chain: |
| 673 | sh_funcs.append('%swhile (pNext) {' % (indent)) |
| 674 | indent = ' ' |
| 675 | sh_funcs.append('%sswitch (pNext->sType) {' % (indent)) |
| 676 | indent += ' ' |
| 677 | for e in enum_type_dict: |
| 678 | if 'StructureType' in e: |
| 679 | for v in sorted(enum_type_dict[e]): |
| 680 | struct_name = get_struct_name_from_struct_type(v) |
| 681 | if struct_name not in self.struct_dict: |
| 682 | continue |
Chia-I Wu | 736324e | 2015-10-26 20:42:12 +0800 | [diff] [blame] | 683 | |
Mark Lobodzinski | 66cdd5d | 2016-12-05 15:27:44 -0700 | [diff] [blame] | 684 | if 'WIN32' in v: |
| 685 | sh_funcs.append("#ifdef VK_USE_PLATFORM_WIN32_KHR") |
| 686 | sh_funcs.append('%scase %s:' % (indent, v)) |
| 687 | sh_funcs.append('%s{' % (indent)) |
Tobin Ehlis | 0411292 | 2015-03-16 10:44:40 -0600 | [diff] [blame] | 688 | indent += ' ' |
Mark Lobodzinski | 66cdd5d | 2016-12-05 15:27:44 -0700 | [diff] [blame] | 689 | sh_funcs.append('%sstructSize += %s((%s*)pNext);' % (indent, self._get_size_helper_func_name(struct_name), struct_name)) |
| 690 | sh_funcs.append('%sbreak;' % (indent)) |
Tobin Ehlis | 0411292 | 2015-03-16 10:44:40 -0600 | [diff] [blame] | 691 | indent = indent[:-4] |
Mark Lobodzinski | 66cdd5d | 2016-12-05 15:27:44 -0700 | [diff] [blame] | 692 | sh_funcs.append('%s}' % (indent)) |
| 693 | if 'WIN32' in v: |
| 694 | sh_funcs.append("#endif // VK_USE_PLATFORM_WIN32_KHR") |
| 695 | sh_funcs.append('%sdefault:' % (indent)) |
| 696 | indent += ' ' |
| 697 | sh_funcs.append('%sassert(0);' % (indent)) |
| 698 | sh_funcs.append('%sstructSize += 0;' % (indent)) |
| 699 | indent = indent[:-4] |
| 700 | indent = indent[:-4] |
| 701 | sh_funcs.append('%s}' % (indent)) |
| 702 | if follow_chain: |
| 703 | sh_funcs.append('%spNext = (VkApplicationInfo*)pNext->pNext;' % (indent)) |
Tobin Ehlis | 0411292 | 2015-03-16 10:44:40 -0600 | [diff] [blame] | 704 | indent = indent[:-4] |
| 705 | sh_funcs.append('%s}' % (indent)) |
Mark Lobodzinski | 66cdd5d | 2016-12-05 15:27:44 -0700 | [diff] [blame] | 706 | sh_funcs.append('%sreturn structSize;\n}' % indent) |
Tobin Ehlis | 6035477 | 2015-03-12 14:50:40 -0600 | [diff] [blame] | 707 | return "\n".join(sh_funcs) |
| 708 | |
| 709 | def _generateSizeHelperHeader(self): |
| 710 | header = [] |
Cody Northrop | fa2f36c | 2016-09-22 14:39:16 -0600 | [diff] [blame] | 711 | header.append('\n#ifdef __cplusplus\n') |
| 712 | header.append('extern "C" {\n') |
| 713 | header.append('#endif\n') |
| 714 | header.append("\n") |
Tobin Ehlis | 6035477 | 2015-03-12 14:50:40 -0600 | [diff] [blame] | 715 | header.append("//#includes, #defines, globals and such...\n") |
| 716 | for f in self.include_headers: |
| 717 | header.append("#include <%s>\n" % f) |
| 718 | header.append('\n// Function Prototypes\n') |
| 719 | header.append("size_t get_struct_chain_size(const void* pStruct);\n") |
Tobin Ehlis | 0411292 | 2015-03-16 10:44:40 -0600 | [diff] [blame] | 720 | header.append("size_t get_dynamic_struct_size(const void* pStruct);\n") |
Tobin Ehlis | 6035477 | 2015-03-12 14:50:40 -0600 | [diff] [blame] | 721 | return "".join(header) |
| 722 | |
Peter Lohrmann | 4a8d891 | 2015-04-03 11:43:06 -0700 | [diff] [blame] | 723 | def _generateSizeHelperHeaderC(self): |
| 724 | header = [] |
Courtney Goeltzenleuchter | dba7998 | 2015-04-14 16:30:53 -0600 | [diff] [blame] | 725 | header.append('#include "vk_struct_size_helper.h"') |
Peter Lohrmann | 4a8d891 | 2015-04-03 11:43:06 -0700 | [diff] [blame] | 726 | header.append('#include <string.h>') |
| 727 | header.append('#include <assert.h>') |
| 728 | header.append('\n// Function definitions\n') |
| 729 | return "\n".join(header) |
| 730 | |
Cody Northrop | fa2f36c | 2016-09-22 14:39:16 -0600 | [diff] [blame] | 731 | def _generateSizeHelperFooter(self): |
| 732 | footer = [] |
| 733 | footer.append('\n\n#ifdef __cplusplus') |
| 734 | footer.append('}') |
| 735 | footer.append('#endif') |
| 736 | return "\n".join(footer) |
Peter Lohrmann | 4a8d891 | 2015-04-03 11:43:06 -0700 | [diff] [blame] | 737 | |
Tobin Ehlis | 6442dca | 2014-10-22 15:13:53 -0600 | [diff] [blame] | 738 | def _generateHeader(self): |
| 739 | header = [] |
| 740 | header.append("//#includes, #defines, globals and such...\n") |
| 741 | for f in self.include_headers: |
| 742 | header.append("#include <%s>\n" % f) |
| 743 | return "".join(header) |
Mark Lobodzinski | 28b8f02 | 2015-08-27 15:30:29 -0600 | [diff] [blame] | 744 | |
Tobin Ehlis | 6442dca | 2014-10-22 15:13:53 -0600 | [diff] [blame] | 745 | # Declarations |
| 746 | def _generateConstructorDeclarations(self, s): |
| 747 | constructors = [] |
| 748 | constructors.append(" %s();\n" % self.get_class_name(s)) |
| 749 | constructors.append(" %s(%s* pInStruct);\n" % (self.get_class_name(s), typedef_fwd_dict[s])) |
| 750 | constructors.append(" %s(const %s* pInStruct);\n" % (self.get_class_name(s), typedef_fwd_dict[s])) |
| 751 | return "".join(constructors) |
Mark Lobodzinski | 28b8f02 | 2015-08-27 15:30:29 -0600 | [diff] [blame] | 752 | |
Tobin Ehlis | 6442dca | 2014-10-22 15:13:53 -0600 | [diff] [blame] | 753 | def _generateDestructorDeclarations(self, s): |
| 754 | return " virtual ~%s();\n" % self.get_class_name(s) |
Mark Lobodzinski | 28b8f02 | 2015-08-27 15:30:29 -0600 | [diff] [blame] | 755 | |
Tobin Ehlis | 6442dca | 2014-10-22 15:13:53 -0600 | [diff] [blame] | 756 | def _generateDisplayDeclarations(self, s): |
| 757 | return " void display_txt();\n void display_single_txt();\n void display_full_txt();\n" |
Mark Lobodzinski | 28b8f02 | 2015-08-27 15:30:29 -0600 | [diff] [blame] | 758 | |
Tobin Ehlis | 6442dca | 2014-10-22 15:13:53 -0600 | [diff] [blame] | 759 | def _generateGetSetDeclarations(self, s): |
| 760 | get_set = [] |
| 761 | get_set.append(" void set_indent(uint32_t indent) { m_indent = indent; }\n") |
| 762 | for member in sorted(self.struct_dict[s]): |
| 763 | # TODO : Skipping array set/get funcs for now |
| 764 | if self.struct_dict[s][member]['array']: |
| 765 | continue |
| 766 | get_set.append(" %s get_%s() { return m_struct.%s; }\n" % (self.struct_dict[s][member]['full_type'], self.struct_dict[s][member]['name'], self.struct_dict[s][member]['name'])) |
| 767 | if not self.struct_dict[s][member]['const']: |
| 768 | get_set.append(" void set_%s(%s inValue) { m_struct.%s = inValue; }\n" % (self.struct_dict[s][member]['name'], self.struct_dict[s][member]['full_type'], self.struct_dict[s][member]['name'])) |
| 769 | return "".join(get_set) |
Mark Lobodzinski | 28b8f02 | 2015-08-27 15:30:29 -0600 | [diff] [blame] | 770 | |
Tobin Ehlis | 6442dca | 2014-10-22 15:13:53 -0600 | [diff] [blame] | 771 | def _generatePrivateMembers(self, s): |
| 772 | priv = [] |
| 773 | priv.append("\nprivate:\n") |
| 774 | priv.append(" %s m_struct;\n" % typedef_fwd_dict[s]) |
| 775 | priv.append(" const %s* m_origStructAddr;\n" % typedef_fwd_dict[s]) |
| 776 | priv.append(" uint32_t m_indent;\n") |
| 777 | priv.append(" const char m_dummy_prefix;\n") |
| 778 | priv.append(" void display_struct_members();\n") |
| 779 | return "".join(priv) |
Mark Lobodzinski | 28b8f02 | 2015-08-27 15:30:29 -0600 | [diff] [blame] | 780 | |
Tobin Ehlis | 6442dca | 2014-10-22 15:13:53 -0600 | [diff] [blame] | 781 | def _generateClassDeclaration(self): |
| 782 | class_decl = [] |
Peter Lohrmann | 49ea184 | 2015-03-30 16:29:56 -0700 | [diff] [blame] | 783 | for s in sorted(self.struct_dict): |
Tobin Ehlis | 6442dca | 2014-10-22 15:13:53 -0600 | [diff] [blame] | 784 | class_decl.append("\n//class declaration") |
| 785 | class_decl.append("class %s\n{\npublic:" % self.get_class_name(s)) |
| 786 | class_decl.append(self._generateConstructorDeclarations(s)) |
| 787 | class_decl.append(self._generateDestructorDeclarations(s)) |
| 788 | class_decl.append(self._generateDisplayDeclarations(s)) |
| 789 | class_decl.append(self._generateGetSetDeclarations(s)) |
| 790 | class_decl.append(self._generatePrivateMembers(s)) |
| 791 | class_decl.append("};\n") |
| 792 | return "\n".join(class_decl) |
Mark Lobodzinski | 28b8f02 | 2015-08-27 15:30:29 -0600 | [diff] [blame] | 793 | |
Tobin Ehlis | 6442dca | 2014-10-22 15:13:53 -0600 | [diff] [blame] | 794 | def _generateFooter(self): |
| 795 | return "\n//any footer info for class\n" |
| 796 | |
Tobin Ehlis | 3764969 | 2016-02-10 15:32:46 -0700 | [diff] [blame] | 797 | def _getSafeStructName(self, struct): |
| 798 | return "safe_%s" % (struct) |
| 799 | |
| 800 | # If struct has sType or ptr members, generate safe type |
| 801 | def _hasSafeStruct(self, s): |
Tobin Ehlis | ec68b97 | 2016-05-23 16:02:29 -0600 | [diff] [blame] | 802 | exceptions = ['VkPhysicalDeviceFeatures'] |
Tobin Ehlis | 3764969 | 2016-02-10 15:32:46 -0700 | [diff] [blame] | 803 | if s in exceptions: |
| 804 | return False |
| 805 | if 'sType' == self.struct_dict[s][0]['name']: |
| 806 | return True |
| 807 | for m in self.struct_dict[s]: |
| 808 | if self.struct_dict[s][m]['ptr']: |
| 809 | return True |
Jon Ashburn | 5e026df | 2016-06-15 08:19:07 -0600 | [diff] [blame] | 810 | inclusions = ['VkDisplayPlanePropertiesKHR', 'VkDisplayModePropertiesKHR', 'VkDisplayPropertiesKHR'] |
| 811 | if s in inclusions: |
| 812 | return True |
Tobin Ehlis | 3764969 | 2016-02-10 15:32:46 -0700 | [diff] [blame] | 813 | return False |
| 814 | |
| 815 | def _generateSafeStructHeader(self): |
| 816 | header = [] |
| 817 | header.append("//#includes, #defines, globals and such...\n") |
Tobin Ehlis | 2d9deec | 2016-04-21 14:19:26 -0600 | [diff] [blame] | 818 | header.append('#pragma once\n') |
Tobin Ehlis | 3764969 | 2016-02-10 15:32:46 -0700 | [diff] [blame] | 819 | header.append('#include "vulkan/vulkan.h"') |
| 820 | return "".join(header) |
| 821 | |
| 822 | # If given ty is in obj list, or is a struct that contains anything in obj list, return True |
| 823 | def _typeHasObject(self, ty, obj): |
| 824 | if ty in obj: |
| 825 | return True |
| 826 | if is_type(ty, 'struct'): |
| 827 | for m in self.struct_dict[ty]: |
| 828 | if self.struct_dict[ty][m]['type'] in obj: |
| 829 | return True |
| 830 | return False |
| 831 | |
| 832 | def _generateSafeStructDecls(self): |
| 833 | ss_decls = [] |
| 834 | for s in struct_order_list: |
| 835 | if not self._hasSafeStruct(s): |
| 836 | continue |
| 837 | if s in ifdef_dict: |
| 838 | ss_decls.append('#ifdef %s' % ifdef_dict[s]) |
| 839 | ss_name = self._getSafeStructName(s) |
| 840 | ss_decls.append("\nstruct %s {" % (ss_name)) |
| 841 | for m in sorted(self.struct_dict[s]): |
| 842 | m_type = self.struct_dict[s][m]['type'] |
| 843 | if is_type(m_type, 'struct') and self._hasSafeStruct(m_type): |
| 844 | m_type = self._getSafeStructName(m_type) |
| 845 | if self.struct_dict[s][m]['array_size'] != 0 and not self.struct_dict[s][m]['dyn_array']: |
| 846 | ss_decls.append(" %s %s[%s];" % (m_type, self.struct_dict[s][m]['name'], self.struct_dict[s][m]['array_size'])) |
| 847 | elif self.struct_dict[s][m]['ptr'] and 'safe_' not in m_type and not self._typeHasObject(m_type, vulkan.object_non_dispatch_list):#m_type in ['char', 'float', 'uint32_t', 'void', 'VkPhysicalDeviceFeatures']: # We'll never overwrite char* so it can remain const |
| 848 | ss_decls.append(" %s %s;" % (self.struct_dict[s][m]['full_type'], self.struct_dict[s][m]['name'])) |
| 849 | elif self.struct_dict[s][m]['array']: |
| 850 | ss_decls.append(" %s* %s;" % (m_type, self.struct_dict[s][m]['name'])) |
| 851 | elif self.struct_dict[s][m]['ptr']: |
| 852 | ss_decls.append(" %s* %s;" % (m_type, self.struct_dict[s][m]['name'])) |
| 853 | else: |
| 854 | ss_decls.append(" %s %s;" % (m_type, self.struct_dict[s][m]['name'])) |
| 855 | ss_decls.append(" %s(const %s* pInStruct);" % (ss_name, s)) |
Tobin Ehlis | 5666b39 | 2016-04-26 14:17:19 -0600 | [diff] [blame] | 856 | ss_decls.append(" %s(const %s& src);" % (ss_name, ss_name)) # Copy constructor |
| 857 | ss_decls.append(" %s();" % (ss_name)) # Default constructor |
Tobin Ehlis | 3764969 | 2016-02-10 15:32:46 -0700 | [diff] [blame] | 858 | ss_decls.append(" ~%s();" % (ss_name)) |
| 859 | ss_decls.append(" void initialize(const %s* pInStruct);" % (s)) |
Tobin Ehlis | 5666b39 | 2016-04-26 14:17:19 -0600 | [diff] [blame] | 860 | ss_decls.append(" void initialize(const %s* src);" % (ss_name)) |
Chris Forbes | 6f6844a | 2016-04-27 14:00:44 +1200 | [diff] [blame] | 861 | ss_decls.append(" %s *ptr() { return reinterpret_cast<%s *>(this); }" % (s, s)) |
| 862 | ss_decls.append(" %s const *ptr() const { return reinterpret_cast<%s const *>(this); }" % (s, s)) |
Tobin Ehlis | 3764969 | 2016-02-10 15:32:46 -0700 | [diff] [blame] | 863 | ss_decls.append("};") |
| 864 | if s in ifdef_dict: |
| 865 | ss_decls.append('#endif') |
| 866 | return "\n".join(ss_decls) |
| 867 | |
| 868 | def _generateSafeStructSourceHeader(self): |
| 869 | header = [] |
| 870 | header.append("//#includes, #defines, globals and such...\n") |
Tobin Ehlis | 5f4cef1 | 2016-04-01 13:51:33 -0600 | [diff] [blame] | 871 | header.append('#include "vk_safe_struct.h"\n#include <string.h>\n\n') |
Tobin Ehlis | 3764969 | 2016-02-10 15:32:46 -0700 | [diff] [blame] | 872 | return "".join(header) |
| 873 | |
| 874 | def _generateSafeStructSource(self): |
| 875 | ss_src = [] |
| 876 | for s in struct_order_list: |
| 877 | if not self._hasSafeStruct(s): |
| 878 | continue |
| 879 | if s in ifdef_dict: |
| 880 | ss_src.append('#ifdef %s' % ifdef_dict[s]) |
| 881 | ss_name = self._getSafeStructName(s) |
| 882 | init_list = '' # list of members in struct constructor initializer |
Tobin Ehlis | d94b785 | 2016-06-27 12:55:49 -0600 | [diff] [blame] | 883 | default_init_list = '' # Default constructor just inits ptrs to nullptr in initializer |
Tobin Ehlis | 3764969 | 2016-02-10 15:32:46 -0700 | [diff] [blame] | 884 | init_func_txt = '' # Txt for initialize() function that takes struct ptr and inits members |
Tobin Ehlis | 5f4cef1 | 2016-04-01 13:51:33 -0600 | [diff] [blame] | 885 | construct_txt = '' # Body of constuctor as well as body of initialize() func following init_func_txt |
Tobin Ehlis | 3764969 | 2016-02-10 15:32:46 -0700 | [diff] [blame] | 886 | destruct_txt = '' |
Tobin Ehlis | 6dd0fc3 | 2016-02-12 14:37:09 -0700 | [diff] [blame] | 887 | # VkWriteDescriptorSet is special case because pointers may be non-null but ignored |
| 888 | # TODO : This is ugly, figure out better way to do this |
| 889 | custom_construct_txt = {'VkWriteDescriptorSet' : |
| 890 | ' switch (descriptorType) {\n' |
| 891 | ' case VK_DESCRIPTOR_TYPE_SAMPLER:\n' |
| 892 | ' case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:\n' |
| 893 | ' case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:\n' |
| 894 | ' case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:\n' |
| 895 | ' case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:\n' |
| 896 | ' if (descriptorCount && pInStruct->pImageInfo) {\n' |
| 897 | ' pImageInfo = new VkDescriptorImageInfo[descriptorCount];\n' |
| 898 | ' for (uint32_t i=0; i<descriptorCount; ++i) {\n' |
| 899 | ' pImageInfo[i] = pInStruct->pImageInfo[i];\n' |
| 900 | ' }\n' |
| 901 | ' }\n' |
| 902 | ' break;\n' |
| 903 | ' case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:\n' |
| 904 | ' case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:\n' |
| 905 | ' case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:\n' |
| 906 | ' case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:\n' |
| 907 | ' if (descriptorCount && pInStruct->pBufferInfo) {\n' |
| 908 | ' pBufferInfo = new VkDescriptorBufferInfo[descriptorCount];\n' |
| 909 | ' for (uint32_t i=0; i<descriptorCount; ++i) {\n' |
| 910 | ' pBufferInfo[i] = pInStruct->pBufferInfo[i];\n' |
| 911 | ' }\n' |
| 912 | ' }\n' |
| 913 | ' break;\n' |
| 914 | ' case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:\n' |
| 915 | ' case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:\n' |
| 916 | ' if (descriptorCount && pInStruct->pTexelBufferView) {\n' |
| 917 | ' pTexelBufferView = new VkBufferView[descriptorCount];\n' |
| 918 | ' for (uint32_t i=0; i<descriptorCount; ++i) {\n' |
| 919 | ' pTexelBufferView[i] = pInStruct->pTexelBufferView[i];\n' |
| 920 | ' }\n' |
| 921 | ' }\n' |
| 922 | ' break;\n' |
Tobin Ehlis | d183b2d | 2016-02-16 12:08:54 -0700 | [diff] [blame] | 923 | ' default:\n' |
| 924 | ' break;\n' |
Tobin Ehlis | 6dd0fc3 | 2016-02-12 14:37:09 -0700 | [diff] [blame] | 925 | ' }\n'} |
Tobin Ehlis | 3764969 | 2016-02-10 15:32:46 -0700 | [diff] [blame] | 926 | for m in self.struct_dict[s]: |
| 927 | m_name = self.struct_dict[s][m]['name'] |
| 928 | m_type = self.struct_dict[s][m]['type'] |
| 929 | if is_type(m_type, 'struct') and self._hasSafeStruct(m_type): |
| 930 | m_type = self._getSafeStructName(m_type) |
| 931 | if self.struct_dict[s][m]['ptr'] and 'safe_' not in m_type and not self._typeHasObject(m_type, vulkan.object_non_dispatch_list):# in ['char', 'float', 'uint32_t', 'void', 'VkPhysicalDeviceFeatures']) or 'pp' == self.struct_dict[s][m]['name'][0:1]: |
Tobin Ehlis | 5f4cef1 | 2016-04-01 13:51:33 -0600 | [diff] [blame] | 932 | # Ptr types w/o a safe_struct, for non-null case need to allocate new ptr and copy data in |
| 933 | if 'KHR' in ss_name or m_type in ['void', 'char']: |
| 934 | # For these exceptions just copy initial value over for now |
| 935 | init_list += '\n\t%s(pInStruct->%s),' % (m_name, m_name) |
| 936 | init_func_txt += ' %s = pInStruct->%s;\n' % (m_name, m_name) |
| 937 | else: |
Tobin Ehlis | d94b785 | 2016-06-27 12:55:49 -0600 | [diff] [blame] | 938 | default_init_list += '\n\t%s(nullptr),' % (m_name) |
Tobin Ehlis | 5f4cef1 | 2016-04-01 13:51:33 -0600 | [diff] [blame] | 939 | init_list += '\n\t%s(nullptr),' % (m_name) |
| 940 | init_func_txt += ' %s = nullptr;\n' % (m_name) |
| 941 | if 'pNext' != m_name and 'void' not in m_type: |
| 942 | if not self.struct_dict[s][m]['array']: |
| 943 | construct_txt += ' if (pInStruct->%s) {\n' % (m_name) |
| 944 | construct_txt += ' %s = new %s(*pInStruct->%s);\n' % (m_name, m_type, m_name) |
| 945 | construct_txt += ' }\n' |
| 946 | destruct_txt += ' if (%s)\n' % (m_name) |
| 947 | destruct_txt += ' delete %s;\n' % (m_name) |
| 948 | else: # new array and then init each element |
| 949 | construct_txt += ' if (pInStruct->%s) {\n' % (m_name) |
| 950 | construct_txt += ' %s = new %s[pInStruct->%s];\n' % (m_name, m_type, self.struct_dict[s][m]['array_size']) |
| 951 | #construct_txt += ' std::copy (pInStruct->%s, pInStruct->%s+pInStruct->%s, %s);\n' % (m_name, m_name, self.struct_dict[s][m]['array_size'], m_name) |
| 952 | construct_txt += ' memcpy ((void *)%s, (void *)pInStruct->%s, sizeof(%s)*pInStruct->%s);\n' % (m_name, m_name, m_type, self.struct_dict[s][m]['array_size']) |
| 953 | construct_txt += ' }\n' |
| 954 | destruct_txt += ' if (%s)\n' % (m_name) |
| 955 | destruct_txt += ' delete[] %s;\n' % (m_name) |
Tobin Ehlis | 3764969 | 2016-02-10 15:32:46 -0700 | [diff] [blame] | 956 | elif self.struct_dict[s][m]['array']: |
Tobin Ehlis | ec68b97 | 2016-05-23 16:02:29 -0600 | [diff] [blame] | 957 | if not self.struct_dict[s][m]['dyn_array']: |
| 958 | # Handle static array case |
Chris Forbes | 9b22de3 | 2016-05-27 11:58:23 +1200 | [diff] [blame] | 959 | construct_txt += ' for (uint32_t i=0; i<%s; ++i) {\n' % (self.struct_dict[s][m]['array_size']) |
| 960 | construct_txt += ' %s[i] = pInStruct->%s[i];\n' % (m_name, m_name) |
| 961 | construct_txt += ' }\n' |
Tobin Ehlis | 3764969 | 2016-02-10 15:32:46 -0700 | [diff] [blame] | 962 | else: |
Tobin Ehlis | ec68b97 | 2016-05-23 16:02:29 -0600 | [diff] [blame] | 963 | # Init array ptr to NULL |
Tobin Ehlis | d94b785 | 2016-06-27 12:55:49 -0600 | [diff] [blame] | 964 | default_init_list += '\n\t%s(nullptr),' % (m_name) |
| 965 | init_list += '\n\t%s(nullptr),' % (m_name) |
| 966 | init_func_txt += ' %s = nullptr;\n' % (m_name) |
Tobin Ehlis | ec68b97 | 2016-05-23 16:02:29 -0600 | [diff] [blame] | 967 | array_element = 'pInStruct->%s[i]' % (m_name) |
| 968 | if is_type(self.struct_dict[s][m]['type'], 'struct') and self._hasSafeStruct(self.struct_dict[s][m]['type']): |
| 969 | array_element = '%s(&pInStruct->%s[i])' % (self._getSafeStructName(self.struct_dict[s][m]['type']), m_name) |
| 970 | construct_txt += ' if (%s && pInStruct->%s) {\n' % (self.struct_dict[s][m]['array_size'], m_name) |
| 971 | construct_txt += ' %s = new %s[%s];\n' % (m_name, m_type, self.struct_dict[s][m]['array_size']) |
| 972 | destruct_txt += ' if (%s)\n' % (m_name) |
| 973 | destruct_txt += ' delete[] %s;\n' % (m_name) |
| 974 | construct_txt += ' for (uint32_t i=0; i<%s; ++i) {\n' % (self.struct_dict[s][m]['array_size']) |
| 975 | if 'safe_' in m_type: |
| 976 | construct_txt += ' %s[i].initialize(&pInStruct->%s[i]);\n' % (m_name, m_name) |
| 977 | else: |
| 978 | construct_txt += ' %s[i] = %s;\n' % (m_name, array_element) |
| 979 | construct_txt += ' }\n' |
| 980 | construct_txt += ' }\n' |
Tobin Ehlis | 3764969 | 2016-02-10 15:32:46 -0700 | [diff] [blame] | 981 | elif self.struct_dict[s][m]['ptr']: |
| 982 | construct_txt += ' if (pInStruct->%s)\n' % (m_name) |
| 983 | construct_txt += ' %s = new %s(pInStruct->%s);\n' % (m_name, m_type, m_name) |
| 984 | construct_txt += ' else\n' |
| 985 | construct_txt += ' %s = NULL;\n' % (m_name) |
| 986 | destruct_txt += ' if (%s)\n' % (m_name) |
| 987 | destruct_txt += ' delete %s;\n' % (m_name) |
| 988 | elif 'safe_' in m_type: # inline struct, need to pass in reference for constructor |
| 989 | init_list += '\n\t%s(&pInStruct->%s),' % (m_name, m_name) |
| 990 | init_func_txt += ' %s.initialize(&pInStruct->%s);\n' % (m_name, m_name) |
| 991 | else: |
| 992 | init_list += '\n\t%s(pInStruct->%s),' % (m_name, m_name) |
Tobin Ehlis | 6dd0fc3 | 2016-02-12 14:37:09 -0700 | [diff] [blame] | 993 | init_func_txt += ' %s = pInStruct->%s;\n' % (m_name, m_name) |
Tobin Ehlis | 3764969 | 2016-02-10 15:32:46 -0700 | [diff] [blame] | 994 | if '' != init_list: |
| 995 | init_list = init_list[:-1] # hack off final comma |
Tobin Ehlis | 6dd0fc3 | 2016-02-12 14:37:09 -0700 | [diff] [blame] | 996 | if s in custom_construct_txt: |
| 997 | construct_txt = custom_construct_txt[s] |
Tobin Ehlis | 3764969 | 2016-02-10 15:32:46 -0700 | [diff] [blame] | 998 | ss_src.append("\n%s::%s(const %s* pInStruct) : %s\n{\n%s}" % (ss_name, ss_name, s, init_list, construct_txt)) |
Tobin Ehlis | d94b785 | 2016-06-27 12:55:49 -0600 | [diff] [blame] | 999 | if '' != default_init_list: |
| 1000 | default_init_list = " : %s" % (default_init_list[:-1]) |
| 1001 | ss_src.append("\n%s::%s()%s\n{}" % (ss_name, ss_name, default_init_list)) |
Tobin Ehlis | 5666b39 | 2016-04-26 14:17:19 -0600 | [diff] [blame] | 1002 | # Create slight variation of init and construct txt for copy constructor that takes a src object reference vs. struct ptr |
| 1003 | copy_construct_init = init_func_txt.replace('pInStruct->', 'src.') |
| 1004 | copy_construct_txt = construct_txt.replace(' (pInStruct->', ' (src.') # Exclude 'if' blocks from next line |
| 1005 | copy_construct_txt = copy_construct_txt.replace('(pInStruct->', '(*src.') # Pass object to copy constructors |
| 1006 | copy_construct_txt = copy_construct_txt.replace('pInStruct->', 'src.') # Modify remaining struct refs for src object |
| 1007 | ss_src.append("\n%s::%s(const %s& src)\n{\n%s%s}" % (ss_name, ss_name, ss_name, copy_construct_init, copy_construct_txt)) # Copy constructor |
Tobin Ehlis | 3764969 | 2016-02-10 15:32:46 -0700 | [diff] [blame] | 1008 | ss_src.append("\n%s::~%s()\n{\n%s}" % (ss_name, ss_name, destruct_txt)) |
| 1009 | ss_src.append("\nvoid %s::initialize(const %s* pInStruct)\n{\n%s%s}" % (ss_name, s, init_func_txt, construct_txt)) |
Tobin Ehlis | 5666b39 | 2016-04-26 14:17:19 -0600 | [diff] [blame] | 1010 | # Copy initializer uses same txt as copy constructor but has a ptr and not a reference |
| 1011 | init_copy = copy_construct_init.replace('src.', 'src->') |
| 1012 | init_construct = copy_construct_txt.replace('src.', 'src->') |
| 1013 | ss_src.append("\nvoid %s::initialize(const %s* src)\n{\n%s%s}" % (ss_name, ss_name, init_copy, init_construct)) |
Tobin Ehlis | 3764969 | 2016-02-10 15:32:46 -0700 | [diff] [blame] | 1014 | if s in ifdef_dict: |
| 1015 | ss_src.append('#endif') |
| 1016 | return "\n".join(ss_src) |
| 1017 | |
Tobin Ehlis | 6442dca | 2014-10-22 15:13:53 -0600 | [diff] [blame] | 1018 | class EnumCodeGen: |
Mark Lobodzinski | 02ee0ce | 2016-12-15 14:14:28 -0700 | [diff] [blame] | 1019 | def __init__(self, enum_type_dict=None, enum_val_dict=None, typedef_fwd_dict=None, in_file=None, out_sh_file=None): |
Tobin Ehlis | 6442dca | 2014-10-22 15:13:53 -0600 | [diff] [blame] | 1020 | self.et_dict = enum_type_dict |
| 1021 | self.ev_dict = enum_val_dict |
| 1022 | self.tf_dict = typedef_fwd_dict |
| 1023 | self.in_file = in_file |
Tobin Ehlis | 7c47fc4 | 2014-12-17 07:20:23 -0700 | [diff] [blame] | 1024 | self.out_sh_file = out_sh_file |
| 1025 | self.eshfg = CommonFileGen(self.out_sh_file) |
Mark Lobodzinski | 28b8f02 | 2015-08-27 15:30:29 -0600 | [diff] [blame] | 1026 | |
Tobin Ehlis | 6442dca | 2014-10-22 15:13:53 -0600 | [diff] [blame] | 1027 | def generateStringHelper(self): |
Tobin Ehlis | 7c47fc4 | 2014-12-17 07:20:23 -0700 | [diff] [blame] | 1028 | self.eshfg.setHeader(self._generateSHHeader()) |
| 1029 | self.eshfg.setBody(self._generateSHBody()) |
| 1030 | self.eshfg.generate() |
| 1031 | |
Tobin Ehlis | 6442dca | 2014-10-22 15:13:53 -0600 | [diff] [blame] | 1032 | def _generateSHBody(self): |
| 1033 | body = [] |
| 1034 | # with open(self.out_file, "a") as hf: |
| 1035 | # bet == base_enum_type, fet == final_enum_type |
Peter Lohrmann | 49ea184 | 2015-03-30 16:29:56 -0700 | [diff] [blame] | 1036 | for bet in sorted(self.et_dict): |
Tobin Ehlis | 6442dca | 2014-10-22 15:13:53 -0600 | [diff] [blame] | 1037 | fet = self.tf_dict[bet] |
Ian Elliott | feae405 | 2015-02-18 12:38:04 -0700 | [diff] [blame] | 1038 | body.append("static inline const char* string_%s(%s input_value)\n{\n switch ((%s)input_value)\n {" % (fet, fet, fet)) |
Tobin Ehlis | 6442dca | 2014-10-22 15:13:53 -0600 | [diff] [blame] | 1039 | for e in sorted(self.et_dict[bet]): |
| 1040 | if (self.ev_dict[e]['unique']): |
Tobin Ehlis | 1077703 | 2015-01-16 15:13:34 -0700 | [diff] [blame] | 1041 | body.append(' case %s:\n return "%s";' % (e, e)) |
| 1042 | body.append(' default:\n return "Unhandled %s";\n }\n}\n\n' % (fet)) |
Tobin Ehlis | 6442dca | 2014-10-22 15:13:53 -0600 | [diff] [blame] | 1043 | return "\n".join(body) |
Mark Lobodzinski | 28b8f02 | 2015-08-27 15:30:29 -0600 | [diff] [blame] | 1044 | |
Tobin Ehlis | 6442dca | 2014-10-22 15:13:53 -0600 | [diff] [blame] | 1045 | def _generateSHHeader(self): |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 1046 | header = [] |
| 1047 | header.append('#pragma once\n') |
Ian Elliott | c779c7e | 2015-04-28 16:11:24 -0600 | [diff] [blame] | 1048 | header.append('#ifdef _WIN32\n') |
| 1049 | header.append('#pragma warning( disable : 4065 )\n') |
| 1050 | header.append('#endif\n') |
David Pinedo | 9316d3b | 2015-11-06 12:54:48 -0700 | [diff] [blame] | 1051 | header.append('#include <vulkan/%s>\n\n\n' % self.in_file) |
Ian Elliott | 2d4ab1e | 2015-01-13 17:52:38 -0700 | [diff] [blame] | 1052 | return "\n".join(header) |
Mark Lobodzinski | 28b8f02 | 2015-08-27 15:30:29 -0600 | [diff] [blame] | 1053 | |
Tobin Ehlis | 6442dca | 2014-10-22 15:13:53 -0600 | [diff] [blame] | 1054 | |
Tobin Ehlis | 6442dca | 2014-10-22 15:13:53 -0600 | [diff] [blame] | 1055 | def main(argv=None): |
| 1056 | opts = handle_args() |
| 1057 | # Parse input file and fill out global dicts |
| 1058 | hfp = HeaderFileParser(opts.input_file) |
| 1059 | hfp.parse() |
| 1060 | # TODO : Don't want these to be global, see note at top about wrapper classes |
| 1061 | global enum_val_dict |
| 1062 | global enum_type_dict |
| 1063 | global struct_dict |
| 1064 | global typedef_fwd_dict |
| 1065 | global typedef_rev_dict |
| 1066 | global types_dict |
| 1067 | enum_val_dict = hfp.get_enum_val_dict() |
| 1068 | enum_type_dict = hfp.get_enum_type_dict() |
| 1069 | struct_dict = hfp.get_struct_dict() |
Tobin Ehlis | 5a487e5 | 2014-11-11 12:28:12 -0700 | [diff] [blame] | 1070 | # TODO : Would like to validate struct data here to verify that all of the bools for struct members are correct at this point |
Tobin Ehlis | 6442dca | 2014-10-22 15:13:53 -0600 | [diff] [blame] | 1071 | typedef_fwd_dict = hfp.get_typedef_fwd_dict() |
| 1072 | typedef_rev_dict = hfp.get_typedef_rev_dict() |
| 1073 | types_dict = hfp.get_types_dict() |
| 1074 | #print(enum_val_dict) |
| 1075 | #print(typedef_dict) |
| 1076 | #print(struct_dict) |
Jeremy Hayes | df3f205 | 2015-11-26 13:32:58 -0700 | [diff] [blame] | 1077 | input_header = os.path.basename(opts.input_file) |
| 1078 | if 'vulkan.h' == input_header: |
| 1079 | input_header = "vulkan/vulkan.h" |
Jon Ashburn | 54e2460 | 2016-02-01 17:02:38 -0700 | [diff] [blame] | 1080 | |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1081 | prefix = os.path.basename(opts.input_file).strip(".h") |
| 1082 | if prefix == "vulkan": |
| 1083 | prefix = "vk" |
Tobin Ehlis | 6442dca | 2014-10-22 15:13:53 -0600 | [diff] [blame] | 1084 | if (opts.abs_out_dir is not None): |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1085 | enum_sh_filename = os.path.join(opts.abs_out_dir, prefix+"_enum_string_helper.h") |
Tobin Ehlis | 6442dca | 2014-10-22 15:13:53 -0600 | [diff] [blame] | 1086 | else: |
Courtney Goeltzenleuchter | d8e229c | 2015-04-08 15:36:08 -0600 | [diff] [blame] | 1087 | enum_sh_filename = os.path.join(os.getcwd(), opts.rel_out_dir, prefix+"_enum_string_helper.h") |
Tobin Ehlis | 7c47fc4 | 2014-12-17 07:20:23 -0700 | [diff] [blame] | 1088 | enum_sh_filename = os.path.abspath(enum_sh_filename) |
| 1089 | if not os.path.exists(os.path.dirname(enum_sh_filename)): |
Jamie Madill | 9c04736 | 2016-12-14 13:05:05 -0500 | [diff] [blame] | 1090 | if not opts.quiet: |
| 1091 | print("Creating output dir %s" % os.path.dirname(enum_sh_filename)) |
Tobin Ehlis | 7c47fc4 | 2014-12-17 07:20:23 -0700 | [diff] [blame] | 1092 | os.mkdir(os.path.dirname(enum_sh_filename)) |
Tobin Ehlis | 6442dca | 2014-10-22 15:13:53 -0600 | [diff] [blame] | 1093 | if opts.gen_enum_string_helper: |
Jamie Madill | 9c04736 | 2016-12-14 13:05:05 -0500 | [diff] [blame] | 1094 | if not opts.quiet: |
| 1095 | print("Generating enum string helper to %s" % enum_sh_filename) |
Mark Lobodzinski | 02ee0ce | 2016-12-15 14:14:28 -0700 | [diff] [blame] | 1096 | eg = EnumCodeGen(enum_type_dict, enum_val_dict, typedef_fwd_dict, os.path.basename(opts.input_file), enum_sh_filename) |
Tobin Ehlis | 6442dca | 2014-10-22 15:13:53 -0600 | [diff] [blame] | 1097 | eg.generateStringHelper() |
| 1098 | #for struct in struct_dict: |
| 1099 | #print(struct) |
| 1100 | if opts.gen_struct_wrappers: |
Jamie Madill | 9c04736 | 2016-12-14 13:05:05 -0500 | [diff] [blame] | 1101 | sw = StructWrapperGen(struct_dict, os.path.basename(opts.input_file).strip(".h"), os.path.dirname(enum_sh_filename), opts.quiet) |
Tobin Ehlis | 6442dca | 2014-10-22 15:13:53 -0600 | [diff] [blame] | 1102 | #print(sw.get_class_name(struct)) |
Karl Schultz | d7f3754 | 2016-05-10 11:36:08 -0600 | [diff] [blame] | 1103 | sw.set_include_headers([input_header,os.path.basename(enum_sh_filename),"stdint.h","cinttypes", "stdio.h","stdlib.h"]) |
Jeremy Hayes | df3f205 | 2015-11-26 13:32:58 -0700 | [diff] [blame] | 1104 | sw.set_include_headers(["stdio.h", "stdlib.h", input_header]) |
Tobin Ehlis | 6035477 | 2015-03-12 14:50:40 -0600 | [diff] [blame] | 1105 | sw.generateSizeHelper() |
Peter Lohrmann | 4a8d891 | 2015-04-03 11:43:06 -0700 | [diff] [blame] | 1106 | sw.generateSizeHelperC() |
Tobin Ehlis | 3764969 | 2016-02-10 15:32:46 -0700 | [diff] [blame] | 1107 | sw.generateSafeStructHeader() |
| 1108 | sw.generateSafeStructs() |
Jon Ashburn | ea65e49 | 2015-08-06 17:27:49 -0600 | [diff] [blame] | 1109 | if opts.gen_struct_sizes: |
Jamie Madill | 9c04736 | 2016-12-14 13:05:05 -0500 | [diff] [blame] | 1110 | st = StructWrapperGen(struct_dict, os.path.basename(opts.input_file).strip(".h"), os.path.dirname(enum_sh_filename), opts.quiet) |
Jeremy Hayes | df3f205 | 2015-11-26 13:32:58 -0700 | [diff] [blame] | 1111 | st.set_include_headers(["stdio.h", "stdlib.h", input_header]) |
Jon Ashburn | ea65e49 | 2015-08-06 17:27:49 -0600 | [diff] [blame] | 1112 | st.generateSizeHelper() |
| 1113 | st.generateSizeHelperC() |
Jamie Madill | 9c04736 | 2016-12-14 13:05:05 -0500 | [diff] [blame] | 1114 | if not opts.quiet: |
| 1115 | print("DONE!") |
Tobin Ehlis | 6442dca | 2014-10-22 15:13:53 -0600 | [diff] [blame] | 1116 | #print(typedef_rev_dict) |
| 1117 | #print(types_dict) |
| 1118 | #recreate_structs() |
| 1119 | |
| 1120 | if __name__ == "__main__": |
| 1121 | sys.exit(main()) |