blob: 88f1fa20279b8361f4c3ea0cef826bd07d524525 [file] [log] [blame]
Tobin Ehlis33267fd2014-11-10 16:03:19 -07001#!/usr/bin/env python3
2#
Karl Schultz8e42f402016-02-02 19:32:33 -07003# 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 Ehlis33267fd2014-11-10 16:03:19 -07007#
Karl Schultz8e42f402016-02-02 19:32:33 -07008# Permission is hereby granted, free of charge, to any person obtaining a copy
9# of this software and/or associated documentation files (the "Materials"), to
10# deal in the Materials without restriction, including without limitation the
11# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
12# sell copies of the Materials, and to permit persons to whom the Materials
13# are furnished to do so, subject to the following conditions:
Tobin Ehlis33267fd2014-11-10 16:03:19 -070014#
Karl Schultz8e42f402016-02-02 19:32:33 -070015# The above copyright notice(s) and this permission notice shall be included
16# in all copies or substantial portions of the Materials.
Tobin Ehlis33267fd2014-11-10 16:03:19 -070017#
Karl Schultz8e42f402016-02-02 19:32:33 -070018# THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Tobin Ehlis33267fd2014-11-10 16:03:19 -070019# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Karl Schultz8e42f402016-02-02 19:32:33 -070020# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21#
22# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
23# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE MATERIALS OR THE
25# USE OR OTHER DEALINGS IN THE MATERIALS
Courtney Goeltzenleuchter05559522015-10-30 11:14:30 -060026#
27# Author: Courtney Goeltzenleuchter <courtney@LunarG.com>
28# Author: Tobin Ehlis <tobin@lunarg.com>
29
Tobin Ehlis6442dca2014-10-22 15:13:53 -060030import argparse
31import os
32import sys
Michael Lentine33d96a32016-01-05 11:10:15 -060033import re
Tobin Ehlisa30e7e52015-07-06 14:02:36 -060034import vulkan
Tobin Ehlis08fafd02015-06-12 12:49:01 -060035from source_line_info import sourcelineinfo
Tobin Ehlis6442dca2014-10-22 15:13:53 -060036
Tobin Ehlis08fafd02015-06-12 12:49:01 -060037# vk_helper.py overview
38# This script generates code based on vulkan input header
39# It generate wrappers functions that can be used to display
Tobin Ehlis6442dca2014-10-22 15:13:53 -060040# structs in a human-readable txt format, as well as utility functions
41# to print enum values as strings
42
Tobin Ehlis6442dca2014-10-22 15:13:53 -060043def handle_args():
44 parser = argparse.ArgumentParser(description='Perform analysis of vogl trace.')
45 parser.add_argument('input_file', help='The input header file from which code will be generated.')
Mark Lobodzinski28b8f022015-08-27 15:30:29 -060046 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 Ehlis6442dca2014-10-22 15:13:53 -060047 parser.add_argument('--abs_out_dir', required=False, default=None, help='Absolute path to write output files. Will be created if needed.')
48 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.')
49 parser.add_argument('--gen_struct_wrappers', required=False, action='store_true', default=False, help='Enable generation of struct wrapper classes.')
Jon Ashburnea65e492015-08-06 17:27:49 -060050 parser.add_argument('--gen_struct_sizes', required=False, action='store_true', default=False, help='Enable generation of struct sizes.')
Tobin Ehlis6442dca2014-10-22 15:13:53 -060051 parser.add_argument('--gen_cmake', required=False, action='store_true', default=False, help='Enable generation of cmake file for generated code.')
52 parser.add_argument('--gen_graphviz', required=False, action='store_true', default=False, help='Enable generation of graphviz dot file.')
53 #parser.add_argument('--test', action='store_true', default=False, help='Run simple test.')
54 return parser.parse_args()
55
56# TODO : Ideally these data structs would be opaque to user and could be wrapped
57# in their own class(es) to make them friendly for data look-up
58# Dicts for Data storage
59# enum_val_dict[value_name] = dict keys are the string enum value names for all enums
60# |-------->['type'] = the type of enum class into which the value falls
61# |-------->['val'] = the value assigned to this particular value_name
62# '-------->['unique'] = bool designating if this enum 'val' is unique within this enum 'type'
63enum_val_dict = {}
64# enum_type_dict['type'] = the type or class of of enum
65# '----->['val_name1', 'val_name2'...] = each type references a list of val_names within this type
66enum_type_dict = {}
67# struct_dict['struct_basename'] = the base (non-typedef'd) name of the struct
68# |->[<member_num>] = members are stored via their integer placement in the struct
69# | |->['name'] = string name of this struct member
70# ... |->['full_type'] = complete type qualifier for this member
71# |->['type'] = base type for this member
72# |->['ptr'] = bool indicating if this member is ptr
73# |->['const'] = bool indicating if this member is const
74# |->['struct'] = bool indicating if this member is a struct type
75# |->['array'] = bool indicating if this member is an array
Tobin Ehlisbd6a9162015-03-13 07:18:05 -060076# |->['dyn_array'] = bool indicating if member is a dynamically sized array
77# '->['array_size'] = For dyn_array, member name used to size array, else int size for static array
Tobin Ehlis6442dca2014-10-22 15:13:53 -060078struct_dict = {}
Tobin Ehlis37649692016-02-10 15:32:46 -070079struct_order_list = [] # struct names in order they're declared
80# Store struct names that require #ifdef guards
81# dict stores struct and enum definitions that are guarded by ifdef as the key
82# and the txt of the ifdef is the value
83ifdef_dict = {}
Tobin Ehlis6442dca2014-10-22 15:13:53 -060084# typedef_fwd_dict stores mapping from orig_type_name -> new_type_name
85typedef_fwd_dict = {}
86# typedef_rev_dict stores mapping from new_type_name -> orig_type_name
87typedef_rev_dict = {} # store new_name -> orig_name mapping
88# types_dict['id_name'] = identifier name will map to it's type
89# '---->'type' = currently either 'struct' or 'enum'
90types_dict = {} # store orig_name -> type mapping
91
92
93# Class that parses header file and generates data structures that can
94# Then be used for other tasks
95class HeaderFileParser:
96 def __init__(self, header_file=None):
97 self.header_file = header_file
98 # store header data in various formats, see above for more info
99 self.enum_val_dict = {}
100 self.enum_type_dict = {}
101 self.struct_dict = {}
102 self.typedef_fwd_dict = {}
103 self.typedef_rev_dict = {}
104 self.types_dict = {}
Tobin Ehlis5178bd72015-03-06 10:38:25 -0700105 self.last_struct_count_name = ''
Mark Lobodzinski28b8f022015-08-27 15:30:29 -0600106
Tobin Ehlis6442dca2014-10-22 15:13:53 -0600107 def setHeaderFile(self, header_file):
108 self.header_file = header_file
109
110 def get_enum_val_dict(self):
111 return self.enum_val_dict
112
113 def get_enum_type_dict(self):
114 return self.enum_type_dict
115
116 def get_struct_dict(self):
117 return self.struct_dict
118
119 def get_typedef_fwd_dict(self):
120 return self.typedef_fwd_dict
121
122 def get_typedef_rev_dict(self):
123 return self.typedef_rev_dict
124
125 def get_types_dict(self):
126 return self.types_dict
127
128 # Parse header file into data structures
129 def parse(self):
130 # parse through the file, identifying different sections
131 parse_enum = False
132 parse_struct = False
133 member_num = 0
134 # TODO : Comment parsing is very fragile but handles 2 known files
135 block_comment = False
Tobin Ehlis5178bd72015-03-06 10:38:25 -0700136 prev_count_name = ''
Tobin Ehlis37649692016-02-10 15:32:46 -0700137 ifdef_txt = ''
138 ifdef_active = 0
Ian Elliott7e40db92015-08-21 15:09:33 -0600139 exclude_struct_list = ['VkPlatformHandleXcbKHR', 'VkPlatformHandleX11KHR']
Tobin Ehlis6442dca2014-10-22 15:13:53 -0600140 with open(self.header_file) as f:
141 for line in f:
Tobin Ehlis37649692016-02-10 15:32:46 -0700142 if True in [ifd_txt in line for ifd_txt in ['#ifdef ', '#ifndef ']]:
143 ifdef_txt = line.split()[1]
144 ifdef_active = ifdef_active + 1
145 continue
146 if ifdef_active != 0 and '#endif' in line:
147 ifdef_active = ifdef_active - 1
Tobin Ehlis6442dca2014-10-22 15:13:53 -0600148 if block_comment:
149 if '*/' in line:
150 block_comment = False
151 continue
152 if '/*' in line:
Courtney Goeltzenleuchter82e72392015-07-10 09:06:56 -0600153 if '*/' in line: # single line block comment
154 continue
Tobin Ehlis6442dca2014-10-22 15:13:53 -0600155 block_comment = True
156 elif 0 == len(line.split()):
157 #print("Skipping empty line")
158 continue
159 elif line.split()[0].strip().startswith("//"):
Jon Ashburnfe6113c2015-01-09 09:11:44 -0700160 #print("Skipping commented line %s" % line)
Tobin Ehlis6442dca2014-10-22 15:13:53 -0600161 continue
162 elif 'typedef enum' in line:
163 (ty_txt, en_txt, base_type) = line.strip().split(None, 2)
164 #print("Found ENUM type %s" % base_type)
Courtney Goeltzenleuchter82e72392015-07-10 09:06:56 -0600165 if '{' == base_type:
166 base_type = 'tmp_enum'
Tobin Ehlis6442dca2014-10-22 15:13:53 -0600167 parse_enum = True
Jon Ashburnfe6113c2015-01-09 09:11:44 -0700168 default_enum_val = 0
Tobin Ehlis6442dca2014-10-22 15:13:53 -0600169 self.types_dict[base_type] = 'enum'
Courtney Goeltzenleuchter82e72392015-07-10 09:06:56 -0600170 elif 'typedef struct' in line or 'typedef union' in line:
Jon Ashburnea65e492015-08-06 17:27:49 -0600171 if True in [ex_type in line for ex_type in exclude_struct_list]:
172 continue
Chia-I Wu44c99b62015-10-27 19:55:29 +0800173
Tobin Ehlis6442dca2014-10-22 15:13:53 -0600174 (ty_txt, st_txt, base_type) = line.strip().split(None, 2)
Chia-I Wu44c99b62015-10-27 19:55:29 +0800175 if ' ' in base_type:
176 (ignored, base_type) = base_type.strip().split(None, 1)
177
Tobin Ehlis6442dca2014-10-22 15:13:53 -0600178 #print("Found STRUCT type: %s" % base_type)
Cody Northrop22b7fae2015-08-11 11:21:48 -0600179 # Note: This really needs to be updated to handle one line struct definition, like
180 # typedef struct obj##_T { uint64_t handle; } obj;
181 if ('{' == base_type or not (' ' in base_type)):
Courtney Goeltzenleuchter82e72392015-07-10 09:06:56 -0600182 base_type = 'tmp_struct'
Cody Northrop22b7fae2015-08-11 11:21:48 -0600183 parse_struct = True
184 self.types_dict[base_type] = 'struct'
Courtney Goeltzenleuchter82e72392015-07-10 09:06:56 -0600185# elif 'typedef union' in line:
186# (ty_txt, st_txt, base_type) = line.strip().split(None, 2)
187# print("Found UNION type: %s" % base_type)
188# parse_struct = True
189# self.types_dict[base_type] = 'struct'
Tobin Ehlis6442dca2014-10-22 15:13:53 -0600190 elif '}' in line and (parse_enum or parse_struct):
191 if len(line.split()) > 1: # deals with embedded union in one struct
192 parse_enum = False
193 parse_struct = False
Tobin Ehlis5178bd72015-03-06 10:38:25 -0700194 self.last_struct_count_name = ''
Tobin Ehlis6442dca2014-10-22 15:13:53 -0600195 member_num = 0
Tobin Ehlis6442dca2014-10-22 15:13:53 -0600196 (cur_char, targ_type) = line.strip().split(None, 1)
Courtney Goeltzenleuchter82e72392015-07-10 09:06:56 -0600197 if 'tmp_struct' == base_type:
198 base_type = targ_type.strip(';')
Ian Elliott7e40db92015-08-21 15:09:33 -0600199 if True in [ex_type in base_type for ex_type in exclude_struct_list]:
200 del self.struct_dict['tmp_struct']
201 continue
Courtney Goeltzenleuchter82e72392015-07-10 09:06:56 -0600202 #print("Found Actual Struct type %s" % base_type)
203 self.struct_dict[base_type] = self.struct_dict['tmp_struct']
204 self.struct_dict.pop('tmp_struct', 0)
Tobin Ehlis37649692016-02-10 15:32:46 -0700205 struct_order_list.append(base_type)
Courtney Goeltzenleuchter82e72392015-07-10 09:06:56 -0600206 self.types_dict[base_type] = 'struct'
207 self.types_dict.pop('tmp_struct', 0)
208 elif 'tmp_enum' == base_type:
209 base_type = targ_type.strip(';')
210 #print("Found Actual ENUM type %s" % base_type)
211 for n in self.enum_val_dict:
212 if 'tmp_enum' == self.enum_val_dict[n]['type']:
213 self.enum_val_dict[n]['type'] = base_type
214# self.enum_val_dict[base_type] = self.enum_val_dict['tmp_enum']
215# self.enum_val_dict.pop('tmp_enum', 0)
216 self.enum_type_dict[base_type] = self.enum_type_dict['tmp_enum']
217 self.enum_type_dict.pop('tmp_enum', 0)
218 self.types_dict[base_type] = 'enum'
219 self.types_dict.pop('tmp_enum', 0)
Tobin Ehlis37649692016-02-10 15:32:46 -0700220 if ifdef_active:
221 ifdef_dict[base_type] = ifdef_txt
Tobin Ehlis6442dca2014-10-22 15:13:53 -0600222 self.typedef_fwd_dict[base_type] = targ_type.strip(';')
223 self.typedef_rev_dict[targ_type.strip(';')] = base_type
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -0600224 #print("fwd_dict: %s = %s" % (base_type, targ_type))
Tobin Ehlis6442dca2014-10-22 15:13:53 -0600225 elif parse_enum:
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600226 #if 'VK_MAX_ENUM' not in line and '{' not in line:
Courtney Goeltzenleuchter82e72392015-07-10 09:06:56 -0600227 if True not in [ens in line for ens in ['{', '_MAX_ENUM', '_BEGIN_RANGE', '_END_RANGE', '_NUM = ', '_ENUM_RANGE']]:
Jon Ashburnfe6113c2015-01-09 09:11:44 -0700228 self._add_enum(line, base_type, default_enum_val)
229 default_enum_val += 1
Tobin Ehlis6442dca2014-10-22 15:13:53 -0600230 elif parse_struct:
231 if ';' in line:
232 self._add_struct(line, base_type, member_num)
233 member_num = member_num + 1
Mark Lobodzinski28b8f022015-08-27 15:30:29 -0600234
Tobin Ehlis6442dca2014-10-22 15:13:53 -0600235 # populate enum dicts based on enum lines
Jon Ashburnfe6113c2015-01-09 09:11:44 -0700236 def _add_enum(self, line_txt, enum_type, def_enum_val):
Tobin Ehlis6442dca2014-10-22 15:13:53 -0600237 #print("Parsing enum line %s" % line_txt)
Jon Ashburnfe6113c2015-01-09 09:11:44 -0700238 if '=' in line_txt:
239 (enum_name, eq_char, enum_val) = line_txt.split(None, 2)
240 else:
241 enum_name = line_txt.split(',')[0]
242 enum_val = str(def_enum_val)
Tobin Ehlis6442dca2014-10-22 15:13:53 -0600243 self.enum_val_dict[enum_name] = {}
244 self.enum_val_dict[enum_name]['type'] = enum_type
245 # strip comma and comment, then extra split in case of no comma w/ comments
246 enum_val = enum_val.strip().split(',', 1)[0]
247 self.enum_val_dict[enum_name]['val'] = enum_val.split()[0]
Tobin Ehlisd3350652015-04-22 13:59:43 -0600248 # Perform conversion of VK_BIT macro
249 if 'VK_BIT' in self.enum_val_dict[enum_name]['val']:
250 vk_bit_val = self.enum_val_dict[enum_name]['val']
251 bit_shift = int(vk_bit_val[vk_bit_val.find('(')+1:vk_bit_val.find(')')], 0)
252 self.enum_val_dict[enum_name]['val'] = str(1 << bit_shift)
253 else:
254 # account for negative values surrounded by parens
255 self.enum_val_dict[enum_name]['val'] = self.enum_val_dict[enum_name]['val'].strip(')').replace('-(', '-')
Tobin Ehlis43ebcac2014-12-05 12:13:07 -0700256 # Try to cast to int to determine if enum value is unique
Tobin Ehlis6442dca2014-10-22 15:13:53 -0600257 try:
258 #print("ENUM val:", self.enum_val_dict[enum_name]['val'])
259 int(self.enum_val_dict[enum_name]['val'], 0)
260 self.enum_val_dict[enum_name]['unique'] = True
261 #print("ENUM has num value")
262 except ValueError:
263 self.enum_val_dict[enum_name]['unique'] = False
264 #print("ENUM is not a number value")
265 # Update enum_type_dict as well
266 if not enum_type in self.enum_type_dict:
267 self.enum_type_dict[enum_type] = []
268 self.enum_type_dict[enum_type].append(enum_name)
Tobin Ehlis5178bd72015-03-06 10:38:25 -0700269
Jon Ashburnea65e492015-08-06 17:27:49 -0600270 # Return True if struct member is a dynamic array
Tobin Ehlis5178bd72015-03-06 10:38:25 -0700271 # RULES : This is a bit quirky based on the API
272 # NOTE : Changes in API spec may cause these rules to change
273 # 1. There must be a previous uint var w/ 'count' in the name in the struct
274 # 2. Dynam array must have 'const' and '*' qualifiers
275 # 3a. Name of dynam array must end in 's' char OR
276 # 3b. Name of count var minus 'count' must be contained in name of dynamic array
277 def _is_dynamic_array(self, full_type, name):
Tobin Ehlis37649692016-02-10 15:32:46 -0700278 exceptions = ['pEnabledFeatures', 'pWaitDstStageMask', 'pSampleMask']
279 if name in exceptions:
280 return False
Tobin Ehlis5178bd72015-03-06 10:38:25 -0700281 if '' != self.last_struct_count_name:
282 if 'const' in full_type and '*' in full_type:
283 if name.endswith('s') or self.last_struct_count_name.lower().replace('count', '') in name.lower():
284 return True
Chia-I Wud50a7d72015-10-26 20:48:51 +0800285
286 # VkWriteDescriptorSet
287 if self.last_struct_count_name == "descriptorCount":
288 return True
289
Tobin Ehlis5178bd72015-03-06 10:38:25 -0700290 return False
291
Tobin Ehlis6442dca2014-10-22 15:13:53 -0600292 # populate struct dicts based on struct lines
293 # TODO : Handle ":" bitfield, "**" ptr->ptr and "const type*const*"
294 def _add_struct(self, line_txt, struct_type, num):
295 #print("Parsing struct line %s" % line_txt)
Courtney Goeltzenleuchter82e72392015-07-10 09:06:56 -0600296 if '{' == struct_type:
297 print("Parsing struct '{' w/ line %s" % line_txt)
Tobin Ehlis6442dca2014-10-22 15:13:53 -0600298 if not struct_type in self.struct_dict:
299 self.struct_dict[struct_type] = {}
300 members = line_txt.strip().split(';', 1)[0] # first strip semicolon & comments
301 # TODO : Handle bitfields more correctly
302 members = members.strip().split(':', 1)[0] # strip bitfield element
303 (member_type, member_name) = members.rsplit(None, 1)
Tobin Ehlis5178bd72015-03-06 10:38:25 -0700304 # Store counts to help recognize and size dynamic arrays
Tobin Ehlisb4934382015-04-15 16:01:16 -0600305 if 'count' in member_name.lower() and 'samplecount' != member_name.lower() and 'uint' in member_type:
Tobin Ehlis5178bd72015-03-06 10:38:25 -0700306 self.last_struct_count_name = member_name
Tobin Ehlis6442dca2014-10-22 15:13:53 -0600307 self.struct_dict[struct_type][num] = {}
308 self.struct_dict[struct_type][num]['full_type'] = member_type
Tobin Ehlis5178bd72015-03-06 10:38:25 -0700309 self.struct_dict[struct_type][num]['dyn_array'] = False
Tobin Ehlis6442dca2014-10-22 15:13:53 -0600310 if '*' in member_type:
311 self.struct_dict[struct_type][num]['ptr'] = True
Tobin Ehlis5178bd72015-03-06 10:38:25 -0700312 # TODO : Need more general purpose way here to reduce down to basic type
313 member_type = member_type.replace(' const*', '')
Tobin Ehlis6442dca2014-10-22 15:13:53 -0600314 member_type = member_type.strip('*')
315 else:
316 self.struct_dict[struct_type][num]['ptr'] = False
317 if 'const' in member_type:
318 self.struct_dict[struct_type][num]['const'] = True
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -0600319 member_type = member_type.replace('const', '').strip()
Tobin Ehlis6442dca2014-10-22 15:13:53 -0600320 else:
321 self.struct_dict[struct_type][num]['const'] = False
Tobin Ehlis5a487e52014-11-11 12:28:12 -0700322 # TODO : There is a bug here where it seems that at the time we do this check,
323 # the data is not in the types or typedef_rev_dict, so we never pass this if check
Tobin Ehlis6442dca2014-10-22 15:13:53 -0600324 if is_type(member_type, 'struct'):
325 self.struct_dict[struct_type][num]['struct'] = True
326 else:
327 self.struct_dict[struct_type][num]['struct'] = False
328 self.struct_dict[struct_type][num]['type'] = member_type
329 if '[' in member_name:
330 (member_name, array_size) = member_name.split('[', 1)
Tobin Ehlis37649692016-02-10 15:32:46 -0700331 #if 'char' in member_type:
332 # self.struct_dict[struct_type][num]['array'] = False
333 # self.struct_dict[struct_type][num]['array_size'] = 0
334 # self.struct_dict[struct_type][num]['ptr'] = True
335 #else:
336 self.struct_dict[struct_type][num]['array'] = True
337 self.struct_dict[struct_type][num]['array_size'] = array_size.strip(']')
Tobin Ehlis5178bd72015-03-06 10:38:25 -0700338 elif self._is_dynamic_array(self.struct_dict[struct_type][num]['full_type'], member_name):
339 #print("Found dynamic array %s of size %s" % (member_name, self.last_struct_count_name))
340 self.struct_dict[struct_type][num]['array'] = True
341 self.struct_dict[struct_type][num]['dyn_array'] = True
342 self.struct_dict[struct_type][num]['array_size'] = self.last_struct_count_name
Tobin Ehlisb8013a22015-02-26 12:57:08 -0700343 elif not 'array' in self.struct_dict[struct_type][num]:
Tobin Ehlis6442dca2014-10-22 15:13:53 -0600344 self.struct_dict[struct_type][num]['array'] = False
345 self.struct_dict[struct_type][num]['array_size'] = 0
346 self.struct_dict[struct_type][num]['name'] = member_name
347
Tobin Ehlis5178bd72015-03-06 10:38:25 -0700348# check if given identifier is of specified type_to_check
Tobin Ehlis6442dca2014-10-22 15:13:53 -0600349def is_type(identifier, type_to_check):
Tobin Ehlis6442dca2014-10-22 15:13:53 -0600350 if identifier in types_dict and type_to_check == types_dict[identifier]:
351 return True
352 if identifier in typedef_rev_dict:
353 new_id = typedef_rev_dict[identifier]
354 if new_id in types_dict and type_to_check == types_dict[new_id]:
355 return True
356 return False
357
358# This is a validation function to verify that we can reproduce the original structs
359def recreate_structs():
360 for struct_name in struct_dict:
361 sys.stdout.write("typedef struct %s\n{\n" % struct_name)
362 for mem_num in sorted(struct_dict[struct_name]):
363 sys.stdout.write(" ")
364 if struct_dict[struct_name][mem_num]['const']:
365 sys.stdout.write("const ")
366 #if struct_dict[struct_name][mem_num]['struct']:
367 # sys.stdout.write("struct ")
368 sys.stdout.write (struct_dict[struct_name][mem_num]['type'])
369 if struct_dict[struct_name][mem_num]['ptr']:
370 sys.stdout.write("*")
371 sys.stdout.write(" ")
372 sys.stdout.write(struct_dict[struct_name][mem_num]['name'])
373 if struct_dict[struct_name][mem_num]['array']:
374 sys.stdout.write("[")
375 sys.stdout.write(struct_dict[struct_name][mem_num]['array_size'])
376 sys.stdout.write("]")
377 sys.stdout.write(";\n")
378 sys.stdout.write("} ")
379 sys.stdout.write(typedef_fwd_dict[struct_name])
380 sys.stdout.write(";\n\n")
381
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -0600382#
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600383# TODO: Fix construction of struct name
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -0600384def get_struct_name_from_struct_type(struct_type):
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600385 # Note: All struct types are now camel-case
Karl Schultz7b74e112016-02-12 13:50:13 -0700386 # Debug Report has an inconsistency - so need special case.
387 if ("VK_STRUCTURE_TYPE_DEBUG_REPORT_CREATE_INFO_EXT" == struct_type):
388 return "VkDebugReportCallbackCreateInfoEXT"
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -0600389 caps_struct_name = struct_type.replace("_STRUCTURE_TYPE", "")
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600390 char_idx = 0
391 struct_name = ''
392 for char in caps_struct_name:
393 if (0 == char_idx) or (caps_struct_name[char_idx-1] == '_'):
394 struct_name += caps_struct_name[char_idx]
395 elif (caps_struct_name[char_idx] == '_'):
396 pass
397 else:
398 struct_name += caps_struct_name[char_idx].lower()
399 char_idx += 1
Chia-I Wu3432a0c2015-10-27 18:04:07 +0800400
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -0600401 return struct_name
402
Tobin Ehlis6442dca2014-10-22 15:13:53 -0600403# class for writing common file elements
404# Here's how this class lays out a file:
405# COPYRIGHT
406# HEADER
407# BODY
408# FOOTER
409#
410# For each of these sections, there's a "set*" function
411# The class as a whole has a generate function which will write each section in order
412class CommonFileGen:
413 def __init__(self, filename=None, copyright_txt="", header_txt="", body_txt="", footer_txt=""):
414 self.filename = filename
415 self.contents = {'copyright': copyright_txt, 'header': header_txt, 'body': body_txt, 'footer': footer_txt}
416 # TODO : Set a default copyright & footer at least
Mark Lobodzinski28b8f022015-08-27 15:30:29 -0600417
Tobin Ehlis6442dca2014-10-22 15:13:53 -0600418 def setFilename(self, filename):
419 self.filename = filename
Mark Lobodzinski28b8f022015-08-27 15:30:29 -0600420
Tobin Ehlis6442dca2014-10-22 15:13:53 -0600421 def setCopyright(self, c):
422 self.contents['copyright'] = c
Mark Lobodzinski28b8f022015-08-27 15:30:29 -0600423
Tobin Ehlis6442dca2014-10-22 15:13:53 -0600424 def setHeader(self, h):
425 self.contents['header'] = h
Mark Lobodzinski28b8f022015-08-27 15:30:29 -0600426
Tobin Ehlis6442dca2014-10-22 15:13:53 -0600427 def setBody(self, b):
428 self.contents['body'] = b
Mark Lobodzinski28b8f022015-08-27 15:30:29 -0600429
Tobin Ehlis6442dca2014-10-22 15:13:53 -0600430 def setFooter(self, f):
431 self.contents['footer'] = f
Mark Lobodzinski28b8f022015-08-27 15:30:29 -0600432
Tobin Ehlis6442dca2014-10-22 15:13:53 -0600433 def generate(self):
434 #print("Generate to file %s" % self.filename)
435 with open(self.filename, "w") as f:
436 f.write(self.contents['copyright'])
437 f.write(self.contents['header'])
438 f.write(self.contents['body'])
439 f.write(self.contents['footer'])
440
441# class for writing a wrapper class for structures
442# The wrapper class wraps the structs and includes utility functions for
443# setting/getting member values and displaying the struct data in various formats
444class StructWrapperGen:
445 def __init__(self, in_struct_dict, prefix, out_dir):
446 self.struct_dict = in_struct_dict
447 self.include_headers = []
Jon Ashburnea65e492015-08-06 17:27:49 -0600448 self.lineinfo = sourcelineinfo()
Tobin Ehlis6442dca2014-10-22 15:13:53 -0600449 self.api = prefix
Courtney Goeltzenleuchterdba79982015-04-14 16:30:53 -0600450 if prefix.lower() == "vulkan":
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600451 self.api_prefix = "vk"
452 else:
453 self.api_prefix = prefix
454 self.header_filename = os.path.join(out_dir, self.api_prefix+"_struct_wrappers.h")
455 self.class_filename = os.path.join(out_dir, self.api_prefix+"_struct_wrappers.cpp")
Tobin Ehlis37649692016-02-10 15:32:46 -0700456 self.safe_struct_header_filename = os.path.join(out_dir, self.api_prefix+"_safe_struct.h")
457 self.safe_struct_source_filename = os.path.join(out_dir, self.api_prefix+"_safe_struct.cpp")
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600458 self.string_helper_filename = os.path.join(out_dir, self.api_prefix+"_struct_string_helper.h")
459 self.string_helper_no_addr_filename = os.path.join(out_dir, self.api_prefix+"_struct_string_helper_no_addr.h")
460 self.string_helper_cpp_filename = os.path.join(out_dir, self.api_prefix+"_struct_string_helper_cpp.h")
461 self.string_helper_no_addr_cpp_filename = os.path.join(out_dir, self.api_prefix+"_struct_string_helper_no_addr_cpp.h")
462 self.validate_helper_filename = os.path.join(out_dir, self.api_prefix+"_struct_validate_helper.h")
Tobin Ehlisd49efcb2014-11-25 17:43:26 -0700463 self.no_addr = False
Tobin Ehlis37649692016-02-10 15:32:46 -0700464 # Safe Struct (ss) header and source files
465 self.ssh = CommonFileGen(self.safe_struct_header_filename)
466 self.sss = CommonFileGen(self.safe_struct_source_filename)
Tobin Ehlis6442dca2014-10-22 15:13:53 -0600467 self.hfg = CommonFileGen(self.header_filename)
468 self.cfg = CommonFileGen(self.class_filename)
469 self.shg = CommonFileGen(self.string_helper_filename)
Tobin Ehlis99f88672015-01-10 12:42:41 -0700470 self.shcppg = CommonFileGen(self.string_helper_cpp_filename)
Tobin Ehlis14ff0852014-12-17 17:44:50 -0700471 self.vhg = CommonFileGen(self.validate_helper_filename)
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600472 self.size_helper_filename = os.path.join(out_dir, self.api_prefix+"_struct_size_helper.h")
473 self.size_helper_c_filename = os.path.join(out_dir, self.api_prefix+"_struct_size_helper.c")
Tobin Ehlis60354772015-03-12 14:50:40 -0600474 self.size_helper_gen = CommonFileGen(self.size_helper_filename)
Peter Lohrmann4a8d8912015-04-03 11:43:06 -0700475 self.size_helper_c_gen = CommonFileGen(self.size_helper_c_filename)
Tobin Ehlis6442dca2014-10-22 15:13:53 -0600476 #print(self.header_filename)
477 self.header_txt = ""
478 self.definition_txt = ""
Mark Lobodzinski28b8f022015-08-27 15:30:29 -0600479
Tobin Ehlis6442dca2014-10-22 15:13:53 -0600480 def set_include_headers(self, include_headers):
481 self.include_headers = include_headers
482
Tobin Ehlisd49efcb2014-11-25 17:43:26 -0700483 def set_no_addr(self, no_addr):
484 self.no_addr = no_addr
485 if self.no_addr:
486 self.shg = CommonFileGen(self.string_helper_no_addr_filename)
Tobin Ehlis99f88672015-01-10 12:42:41 -0700487 self.shcppg = CommonFileGen(self.string_helper_no_addr_cpp_filename)
Tobin Ehlisd49efcb2014-11-25 17:43:26 -0700488 else:
489 self.shg = CommonFileGen(self.string_helper_filename)
Tobin Ehlis99f88672015-01-10 12:42:41 -0700490 self.shcppg = CommonFileGen(self.string_helper_cpp_filename)
Tobin Ehlisd49efcb2014-11-25 17:43:26 -0700491
Tobin Ehlis6442dca2014-10-22 15:13:53 -0600492 # Return class name for given struct name
493 def get_class_name(self, struct_name):
494 class_name = struct_name.strip('_').lower() + "_struct_wrapper"
495 return class_name
Mark Lobodzinski28b8f022015-08-27 15:30:29 -0600496
Tobin Ehlis6442dca2014-10-22 15:13:53 -0600497 def get_file_list(self):
498 return [os.path.basename(self.header_filename), os.path.basename(self.class_filename), os.path.basename(self.string_helper_filename)]
499
Mark Lobodzinski28b8f022015-08-27 15:30:29 -0600500 # Generate class header file
Tobin Ehlis6442dca2014-10-22 15:13:53 -0600501 def generateHeader(self):
502 self.hfg.setCopyright(self._generateCopyright())
503 self.hfg.setHeader(self._generateHeader())
504 self.hfg.setBody(self._generateClassDeclaration())
505 self.hfg.setFooter(self._generateFooter())
506 self.hfg.generate()
Mark Lobodzinski28b8f022015-08-27 15:30:29 -0600507
Tobin Ehlis6442dca2014-10-22 15:13:53 -0600508 # Generate class definition
509 def generateBody(self):
510 self.cfg.setCopyright(self._generateCopyright())
511 self.cfg.setHeader(self._generateCppHeader())
512 self.cfg.setBody(self._generateClassDefinition())
513 self.cfg.setFooter(self._generateFooter())
514 self.cfg.generate()
515
Tobin Ehlis37649692016-02-10 15:32:46 -0700516 # Safe Structs are versions of vulkan structs with non-const safe ptrs
517 # that make shadowing structures and clean-up of shadowed structures very simple
518 def generateSafeStructHeader(self):
519 self.ssh.setCopyright(self._generateCopyright())
520 self.ssh.setHeader(self._generateSafeStructHeader())
521 self.ssh.setBody(self._generateSafeStructDecls())
522 self.ssh.generate()
523
524 def generateSafeStructs(self):
525 self.sss.setCopyright(self._generateCopyright())
526 self.sss.setHeader(self._generateSafeStructSourceHeader())
527 self.sss.setBody(self._generateSafeStructSource())
528 self.sss.generate()
529
Tobin Ehlis6442dca2014-10-22 15:13:53 -0600530 # Generate c-style .h file that contains functions for printing structs
531 def generateStringHelper(self):
532 print("Generating struct string helper")
533 self.shg.setCopyright(self._generateCopyright())
534 self.shg.setHeader(self._generateStringHelperHeader())
535 self.shg.setBody(self._generateStringHelperFunctions())
536 self.shg.generate()
537
Tobin Ehlis99f88672015-01-10 12:42:41 -0700538 # Generate cpp-style .h file that contains functions for printing structs
539 def generateStringHelperCpp(self):
540 print("Generating struct string helper cpp")
541 self.shcppg.setCopyright(self._generateCopyright())
542 self.shcppg.setHeader(self._generateStringHelperHeaderCpp())
543 self.shcppg.setBody(self._generateStringHelperFunctionsCpp())
544 self.shcppg.generate()
545
Tobin Ehlis14ff0852014-12-17 17:44:50 -0700546 # Generate c-style .h file that contains functions for printing structs
547 def generateValidateHelper(self):
548 print("Generating struct validate helper")
549 self.vhg.setCopyright(self._generateCopyright())
550 self.vhg.setHeader(self._generateValidateHelperHeader())
551 self.vhg.setBody(self._generateValidateHelperFunctions())
552 self.vhg.generate()
553
Tobin Ehlis60354772015-03-12 14:50:40 -0600554 def generateSizeHelper(self):
555 print("Generating struct size helper")
556 self.size_helper_gen.setCopyright(self._generateCopyright())
557 self.size_helper_gen.setHeader(self._generateSizeHelperHeader())
558 self.size_helper_gen.setBody(self._generateSizeHelperFunctions())
559 self.size_helper_gen.generate()
560
Peter Lohrmann4a8d8912015-04-03 11:43:06 -0700561 def generateSizeHelperC(self):
562 print("Generating struct size helper c")
563 self.size_helper_c_gen.setCopyright(self._generateCopyright())
564 self.size_helper_c_gen.setHeader(self._generateSizeHelperHeaderC())
565 self.size_helper_c_gen.setBody(self._generateSizeHelperFunctionsC())
566 self.size_helper_c_gen.generate()
567
Tobin Ehlis6442dca2014-10-22 15:13:53 -0600568 def _generateCopyright(self):
Peter Lohrmann4a8d8912015-04-03 11:43:06 -0700569 copyright = []
570 copyright.append('/* THIS FILE IS GENERATED. DO NOT EDIT. */');
571 copyright.append('');
572 copyright.append('/*');
Ian Elliott0977abc2015-04-15 11:15:13 -0600573 copyright.append(' * Vulkan');
Peter Lohrmann4a8d8912015-04-03 11:43:06 -0700574 copyright.append(' *');
Karl Schultz8e42f402016-02-02 19:32:33 -0700575 copyright.append(' * Copyright (c) 2015-2016 The Khronos Group Inc.');
576 copyright.append(' * Copyright (c) 2015-2016 Valve Corporation.');
577 copyright.append(' * Copyright (c) 2015-2016 LunarG, Inc.');
578 copyright.append(' * Copyright (c) 2015-2016 Google Inc.');
Peter Lohrmann4a8d8912015-04-03 11:43:06 -0700579 copyright.append(' *');
580 copyright.append(' * Permission is hereby granted, free of charge, to any person obtaining a');
Karl Schultz8e42f402016-02-02 19:32:33 -0700581 copyright.append(' * copy of this software and associated documentation files (the "Materials"),');
582 copyright.append(' * to deal in the Materials without restriction, including without limitation');
Peter Lohrmann4a8d8912015-04-03 11:43:06 -0700583 copyright.append(' * the rights to use, copy, modify, merge, publish, distribute, sublicense,');
Karl Schultz8e42f402016-02-02 19:32:33 -0700584 copyright.append(' * and/or sell copies of the Materials, and to permit persons to whom the');
585 copyright.append(' * Materials is furnished to do so, subject to the following conditions:');
Peter Lohrmann4a8d8912015-04-03 11:43:06 -0700586 copyright.append(' *');
587 copyright.append(' * The above copyright notice and this permission notice shall be included');
Karl Schultz8e42f402016-02-02 19:32:33 -0700588 copyright.append(' * in all copies or substantial portions of the Materials.');
Peter Lohrmann4a8d8912015-04-03 11:43:06 -0700589 copyright.append(' *');
Karl Schultz8e42f402016-02-02 19:32:33 -0700590 copyright.append(' * THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR');
Peter Lohrmann4a8d8912015-04-03 11:43:06 -0700591 copyright.append(' * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,');
Karl Schultz8e42f402016-02-02 19:32:33 -0700592 copyright.append(' * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.');
593 copyright.append(' *');
594 copyright.append(' * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,');
595 copyright.append(' * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR');
596 copyright.append(' * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE MATERIALS OR THE');
597 copyright.append(' * USE OR OTHER DEALINGS IN THE MATERIALS');
598 copyright.append(' *');
599 copyright.append(' * Author: Courtney Goeltzenleuchter <courtney@LunarG.com>');
600 copyright.append(' * Author: Tobin Ehlis <tobin@lunarg.com>');
Peter Lohrmann4a8d8912015-04-03 11:43:06 -0700601 copyright.append(' */');
602 copyright.append('');
603 return "\n".join(copyright)
Tobin Ehlis6442dca2014-10-22 15:13:53 -0600604
605 def _generateCppHeader(self):
606 header = []
607 header.append("//#includes, #defines, globals and such...\n")
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600608 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 Ehlis6442dca2014-10-22 15:13:53 -0600609 return "".join(header)
Mark Lobodzinski28b8f022015-08-27 15:30:29 -0600610
Tobin Ehlis6442dca2014-10-22 15:13:53 -0600611 def _generateClassDefinition(self):
612 class_def = []
Mark Lobodzinski28b8f022015-08-27 15:30:29 -0600613 if 'vk' == self.api:
Tobin Ehlis6442dca2014-10-22 15:13:53 -0600614 class_def.append(self._generateDynamicPrintFunctions())
Peter Lohrmann49ea1842015-03-30 16:29:56 -0700615 for s in sorted(self.struct_dict):
Tobin Ehlis6442dca2014-10-22 15:13:53 -0600616 class_def.append("\n// %s class definition" % self.get_class_name(s))
617 class_def.append(self._generateConstructorDefinitions(s))
618 class_def.append(self._generateDestructorDefinitions(s))
619 class_def.append(self._generateDisplayDefinitions(s))
620 return "\n".join(class_def)
Mark Lobodzinski28b8f022015-08-27 15:30:29 -0600621
Tobin Ehlis6442dca2014-10-22 15:13:53 -0600622 def _generateConstructorDefinitions(self, s):
623 con_defs = []
624 con_defs.append("%s::%s() : m_struct(), m_indent(0), m_dummy_prefix('\\0'), m_origStructAddr(NULL) {}" % (self.get_class_name(s), self.get_class_name(s)))
625 # TODO : This is a shallow copy of ptrs
626 con_defs.append("%s::%s(%s* pInStruct) : m_indent(0), m_dummy_prefix('\\0')\n{\n m_struct = *pInStruct;\n m_origStructAddr = pInStruct;\n}" % (self.get_class_name(s), self.get_class_name(s), typedef_fwd_dict[s]))
627 con_defs.append("%s::%s(const %s* pInStruct) : m_indent(0), m_dummy_prefix('\\0')\n{\n m_struct = *pInStruct;\n m_origStructAddr = pInStruct;\n}" % (self.get_class_name(s), self.get_class_name(s), typedef_fwd_dict[s]))
628 return "\n".join(con_defs)
Mark Lobodzinski28b8f022015-08-27 15:30:29 -0600629
Tobin Ehlis6442dca2014-10-22 15:13:53 -0600630 def _generateDestructorDefinitions(self, s):
631 return "%s::~%s() {}" % (self.get_class_name(s), self.get_class_name(s))
Mark Lobodzinski28b8f022015-08-27 15:30:29 -0600632
Tobin Ehlis6442dca2014-10-22 15:13:53 -0600633 def _generateDynamicPrintFunctions(self):
634 dp_funcs = []
Mark Lobodzinski17caf572015-01-29 08:55:56 -0600635 dp_funcs.append("\nvoid dynamic_display_full_txt(const void* pStruct, uint32_t indent)\n{\n // Cast to APP_INFO ptr initially just to pull sType off struct")
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600636 dp_funcs.append(" VkStructureType sType = ((VkApplicationInfo*)pStruct)->sType;\n")
Tobin Ehlis6442dca2014-10-22 15:13:53 -0600637 dp_funcs.append(" switch (sType)\n {")
638 for e in enum_type_dict:
639 class_num = 0
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600640 if "StructureType" in e:
Tobin Ehlis6442dca2014-10-22 15:13:53 -0600641 for v in sorted(enum_type_dict[e]):
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -0600642 struct_name = get_struct_name_from_struct_type(v)
Chia-I Wu736324e2015-10-26 20:42:12 +0800643 if struct_name not in self.struct_dict:
644 continue
645
Tobin Ehlis6442dca2014-10-22 15:13:53 -0600646 class_name = self.get_class_name(struct_name)
Tobin Ehlis6442dca2014-10-22 15:13:53 -0600647 instance_name = "swc%i" % class_num
648 dp_funcs.append(" case %s:\n {" % (v))
649 dp_funcs.append(" %s %s((%s*)pStruct);" % (class_name, instance_name, struct_name))
650 dp_funcs.append(" %s.set_indent(indent);" % (instance_name))
651 dp_funcs.append(" %s.display_full_txt();" % (instance_name))
652 dp_funcs.append(" }")
653 dp_funcs.append(" break;")
654 class_num += 1
655 dp_funcs.append(" }")
656 dp_funcs.append("}\n")
657 return "\n".join(dp_funcs)
658
Tobin Ehlis14ff0852014-12-17 17:44:50 -0700659 def _get_func_name(self, struct, mid_str):
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -0600660 return "%s_%s_%s" % (self.api_prefix, mid_str, struct.lower().strip("_"))
Tobin Ehlis14ff0852014-12-17 17:44:50 -0700661
Tobin Ehlis6442dca2014-10-22 15:13:53 -0600662 def _get_sh_func_name(self, struct):
Tobin Ehlis14ff0852014-12-17 17:44:50 -0700663 return self._get_func_name(struct, 'print')
664
665 def _get_vh_func_name(self, struct):
666 return self._get_func_name(struct, 'validate')
Tobin Ehlis6442dca2014-10-22 15:13:53 -0600667
Tobin Ehlis60354772015-03-12 14:50:40 -0600668 def _get_size_helper_func_name(self, struct):
669 return self._get_func_name(struct, 'size')
670
Tobin Ehlis6442dca2014-10-22 15:13:53 -0600671 # Return elements to create formatted string for given struct member
672 def _get_struct_print_formatted(self, struct_member, pre_var_name="prefix", postfix = "\\n", struct_var_name="pStruct", struct_ptr=True, print_array=False):
673 struct_op = "->"
674 if not struct_ptr:
675 struct_op = "."
676 member_name = struct_member['name']
677 print_type = "p"
678 cast_type = ""
679 member_post = ""
680 array_index = ""
681 member_print_post = ""
Tobin Ehlisb0497852015-02-11 14:24:02 -0700682 print_delimiter = "%"
Tobin Ehlis5178bd72015-03-06 10:38:25 -0700683 if struct_member['array'] and 'char' in struct_member['type'].lower(): # just print char array as string
684 if member_name.startswith('pp'): # TODO : Only printing first element of dynam array of char* for now
685 member_post = "[0]"
Tobin Ehlis6442dca2014-10-22 15:13:53 -0600686 print_type = "s"
687 print_array = False
688 elif struct_member['array'] and not print_array:
689 # Just print base address of array when not full print_array
690 cast_type = "(void*)"
691 elif is_type(struct_member['type'], 'enum'):
692 cast_type = "string_%s" % struct_member['type']
Jon Ashburnfe6113c2015-01-09 09:11:44 -0700693 if struct_member['ptr']:
694 struct_var_name = "*" + struct_var_name
Tobin Ehlis6442dca2014-10-22 15:13:53 -0600695 print_type = "s"
696 elif is_type(struct_member['type'], 'struct'): # print struct address for now
697 cast_type = "(void*)"
698 if not struct_member['ptr']:
699 cast_type = "(void*)&"
Courtney Goeltzenleuchtercd2a0992015-07-09 11:44:38 -0600700 elif 'bool' in struct_member['type'].lower():
Tobin Ehlis6442dca2014-10-22 15:13:53 -0600701 print_type = "s"
702 member_post = ' ? "TRUE" : "FALSE"'
Tobin Ehlis7e65d752015-01-15 17:51:52 -0700703 elif 'float' in struct_member['type']:
Tobin Ehlis6442dca2014-10-22 15:13:53 -0600704 print_type = "f"
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600705 elif 'uint64' in struct_member['type'] or 'gpusize' in struct_member['type'].lower():
706 print_type = '" PRId64 "'
Tobin Ehlis7e65d752015-01-15 17:51:52 -0700707 elif 'uint8' in struct_member['type']:
Tobin Ehlis6442dca2014-10-22 15:13:53 -0600708 print_type = "hu"
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600709 elif 'size' in struct_member['type'].lower():
Tobin Ehlisb0497852015-02-11 14:24:02 -0700710 print_type = '" PRINTF_SIZE_T_SPECIFIER "'
711 print_delimiter = ""
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600712 elif True in [ui_str.lower() in struct_member['type'].lower() for ui_str in ['uint', 'flags', 'samplemask']]:
Tobin Ehlis6442dca2014-10-22 15:13:53 -0600713 print_type = "u"
Tobin Ehlis7e65d752015-01-15 17:51:52 -0700714 elif 'int' in struct_member['type']:
Tobin Ehlis6442dca2014-10-22 15:13:53 -0600715 print_type = "i"
716 elif struct_member['ptr']:
Tobin Ehlis6442dca2014-10-22 15:13:53 -0600717 pass
718 else:
719 #print("Unhandled struct type: %s" % struct_member['type'])
720 cast_type = "(void*)"
721 if print_array and struct_member['array']:
722 member_print_post = "[%u]"
723 array_index = " i,"
724 member_post = "[i]"
Tobin Ehlisb0497852015-02-11 14:24:02 -0700725 print_out = "%%s%s%s = %s%s%s" % (member_name, member_print_post, print_delimiter, print_type, postfix) # section of print that goes inside of quotes
Tobin Ehlis6442dca2014-10-22 15:13:53 -0600726 print_arg = ", %s,%s %s(%s%s%s)%s" % (pre_var_name, array_index, cast_type, struct_var_name, struct_op, member_name, member_post) # section of print passed to portion in quotes
Tobin Ehlisd49efcb2014-11-25 17:43:26 -0700727 if self.no_addr and "p" == print_type:
728 print_out = "%%s%s%s = addr\\n" % (member_name, member_print_post) # section of print that goes inside of quotes
729 print_arg = ", %s" % (pre_var_name)
Tobin Ehlis6442dca2014-10-22 15:13:53 -0600730 return (print_out, print_arg)
731
732 def _generateStringHelperFunctions(self):
733 sh_funcs = []
Tobin Ehlis5a487e52014-11-11 12:28:12 -0700734 # We do two passes, first pass just generates prototypes for all the functsions
Tobin Ehlis291cd702015-01-20 09:48:48 -0700735 for s in sorted(self.struct_dict):
Tobin Ehlis5178bd72015-03-06 10:38:25 -0700736 sh_funcs.append('char* %s(const %s* pStruct, const char* prefix);' % (self._get_sh_func_name(s), typedef_fwd_dict[s]))
737 sh_funcs.append('')
738 sh_funcs.append('#if defined(_WIN32)')
739 sh_funcs.append('// Microsoft did not implement C99 in Visual Studio; but started adding it with')
740 sh_funcs.append('// VS2013. However, VS2013 still did not have snprintf(). The following is a')
741 sh_funcs.append('// work-around.')
742 sh_funcs.append('#define snprintf _snprintf')
743 sh_funcs.append('#endif // _WIN32\n')
Peter Lohrmann49ea1842015-03-30 16:29:56 -0700744 for s in sorted(self.struct_dict):
Tobin Ehlis6442dca2014-10-22 15:13:53 -0600745 p_out = ""
746 p_args = ""
Tobin Ehlis5a487e52014-11-11 12:28:12 -0700747 stp_list = [] # stp == "struct to print" a list of structs for this API call that should be printed as structs
Tobin Ehlis5178bd72015-03-06 10:38:25 -0700748 # This pre-pass flags embedded structs and pNext
Tobin Ehlis291cd702015-01-20 09:48:48 -0700749 for m in sorted(self.struct_dict[s]):
Tobin Ehlis5a487e52014-11-11 12:28:12 -0700750 if 'pNext' == self.struct_dict[s][m]['name'] or is_type(self.struct_dict[s][m]['type'], 'struct'):
751 stp_list.append(self.struct_dict[s][m])
Tobin Ehlis5178bd72015-03-06 10:38:25 -0700752 sh_funcs.append('char* %s(const %s* pStruct, const char* prefix)\n{\n char* str;' % (self._get_sh_func_name(s), typedef_fwd_dict[s]))
753 sh_funcs.append(" size_t len;")
Tobin Ehlis5a487e52014-11-11 12:28:12 -0700754 num_stps = len(stp_list);
755 total_strlen_str = ''
756 if 0 != num_stps:
Tobin Ehlis5178bd72015-03-06 10:38:25 -0700757 sh_funcs.append(" char* tmpStr;")
758 sh_funcs.append(' char* extra_indent = (char*)malloc(strlen(prefix) + 3);')
759 sh_funcs.append(' strcpy(extra_indent, " ");')
760 sh_funcs.append(' strncat(extra_indent, prefix, strlen(prefix));')
761 sh_funcs.append(' char* stp_strs[%i];' % num_stps)
Tobin Ehlis5a487e52014-11-11 12:28:12 -0700762 for index in range(num_stps):
Tobin Ehlis5178bd72015-03-06 10:38:25 -0700763 # If it's an array, print all of the elements
764 # If it's a ptr, print thing it's pointing to
765 # Non-ptr struct case. Print the struct using its address
766 struct_deref = '&'
767 if 1 < stp_list[index]['full_type'].count('*'):
768 struct_deref = ''
Tobin Ehlis5a487e52014-11-11 12:28:12 -0700769 if (stp_list[index]['ptr']):
Tobin Ehlis5178bd72015-03-06 10:38:25 -0700770 sh_funcs.append(' if (pStruct->%s) {' % stp_list[index]['name'])
Tobin Ehlis5a487e52014-11-11 12:28:12 -0700771 if 'pNext' == stp_list[index]['name']:
Tobin Ehlis5178bd72015-03-06 10:38:25 -0700772 sh_funcs.append(' tmpStr = dynamic_display((void*)pStruct->pNext, prefix);')
773 sh_funcs.append(' len = 256+strlen(tmpStr);')
774 sh_funcs.append(' stp_strs[%i] = (char*)malloc(len);' % index)
Tobin Ehlisd49efcb2014-11-25 17:43:26 -0700775 if self.no_addr:
Tobin Ehlis5178bd72015-03-06 10:38:25 -0700776 sh_funcs.append(' snprintf(stp_strs[%i], len, " %%spNext (addr)\\n%%s", prefix, tmpStr);' % index)
Tobin Ehlisd49efcb2014-11-25 17:43:26 -0700777 else:
Tobin Ehlis5178bd72015-03-06 10:38:25 -0700778 sh_funcs.append(' snprintf(stp_strs[%i], len, " %%spNext (%%p)\\n%%s", prefix, (void*)pStruct->pNext, tmpStr);' % index)
779 sh_funcs.append(' free(tmpStr);')
Tobin Ehlis5a487e52014-11-11 12:28:12 -0700780 else:
Tobin Ehlisb8013a22015-02-26 12:57:08 -0700781 if stp_list[index]['name'] in ['pImageViews', 'pBufferViews']:
782 # TODO : This is a quick hack to handle these arrays of ptrs
Courtney Goeltzenleuchtereeae16b2015-04-02 22:54:15 -0600783 sh_funcs.append(' tmpStr = %s(&pStruct->%s[0], extra_indent);' % (self._get_sh_func_name(stp_list[index]['type']), stp_list[index]['name']))
Tobin Ehlisb8013a22015-02-26 12:57:08 -0700784 else:
Tobin Ehlis5178bd72015-03-06 10:38:25 -0700785 sh_funcs.append(' tmpStr = %s(pStruct->%s, extra_indent);' % (self._get_sh_func_name(stp_list[index]['type']), stp_list[index]['name']))
786 sh_funcs.append(' len = 256+strlen(tmpStr)+strlen(prefix);')
787 sh_funcs.append(' stp_strs[%i] = (char*)malloc(len);' % (index))
Tobin Ehlisd49efcb2014-11-25 17:43:26 -0700788 if self.no_addr:
Tobin Ehlis5178bd72015-03-06 10:38:25 -0700789 sh_funcs.append(' snprintf(stp_strs[%i], len, " %%s%s (addr)\\n%%s", prefix, tmpStr);' % (index, stp_list[index]['name']))
Tobin Ehlisd49efcb2014-11-25 17:43:26 -0700790 else:
Tobin Ehlis5178bd72015-03-06 10:38:25 -0700791 sh_funcs.append(' snprintf(stp_strs[%i], len, " %%s%s (%%p)\\n%%s", prefix, (void*)pStruct->%s, tmpStr);' % (index, stp_list[index]['name'], stp_list[index]['name']))
792 sh_funcs.append(' }')
793 sh_funcs.append(" else\n stp_strs[%i] = \"\";" % (index))
794 elif stp_list[index]['array']:
795 sh_funcs.append(' tmpStr = %s(&pStruct->%s[0], extra_indent);' % (self._get_sh_func_name(stp_list[index]['type']), stp_list[index]['name']))
796 sh_funcs.append(' len = 256+strlen(tmpStr);')
797 sh_funcs.append(' stp_strs[%i] = (char*)malloc(len);' % (index))
Tobin Ehlisd49efcb2014-11-25 17:43:26 -0700798 if self.no_addr:
Tobin Ehlis5178bd72015-03-06 10:38:25 -0700799 sh_funcs.append(' snprintf(stp_strs[%i], len, " %%s%s[0] (addr)\\n%%s", prefix, tmpStr);' % (index, stp_list[index]['name']))
Tobin Ehlisd49efcb2014-11-25 17:43:26 -0700800 else:
Tobin Ehlis5178bd72015-03-06 10:38:25 -0700801 sh_funcs.append(' snprintf(stp_strs[%i], len, " %%s%s[0] (%%p)\\n%%s", prefix, (void*)&pStruct->%s[0], tmpStr);' % (index, stp_list[index]['name'], stp_list[index]['name']))
Tobin Ehlis5a487e52014-11-11 12:28:12 -0700802 else:
Tobin Ehlis5178bd72015-03-06 10:38:25 -0700803 sh_funcs.append(' tmpStr = %s(&pStruct->%s, extra_indent);' % (self._get_sh_func_name(stp_list[index]['type']), stp_list[index]['name']))
804 sh_funcs.append(' len = 256+strlen(tmpStr);')
805 sh_funcs.append(' stp_strs[%i] = (char*)malloc(len);' % (index))
Tobin Ehlisd49efcb2014-11-25 17:43:26 -0700806 if self.no_addr:
Tobin Ehlis5178bd72015-03-06 10:38:25 -0700807 sh_funcs.append(' snprintf(stp_strs[%i], len, " %%s%s (addr)\\n%%s", prefix, tmpStr);' % (index, stp_list[index]['name']))
Tobin Ehlisd49efcb2014-11-25 17:43:26 -0700808 else:
Tobin Ehlis5178bd72015-03-06 10:38:25 -0700809 sh_funcs.append(' snprintf(stp_strs[%i], len, " %%s%s (%%p)\\n%%s", prefix, (void*)&pStruct->%s, tmpStr);' % (index, stp_list[index]['name'], stp_list[index]['name']))
Tobin Ehlis5a487e52014-11-11 12:28:12 -0700810 total_strlen_str += 'strlen(stp_strs[%i]) + ' % index
Tobin Ehlis5178bd72015-03-06 10:38:25 -0700811 sh_funcs.append(' len = %ssizeof(char)*1024;' % (total_strlen_str))
812 sh_funcs.append(' str = (char*)malloc(len);')
Courtney Goeltzenleuchterbbd0a362014-12-01 09:31:17 -0700813 sh_funcs.append(' snprintf(str, len, "')
Tobin Ehlis6442dca2014-10-22 15:13:53 -0600814 for m in sorted(self.struct_dict[s]):
815 (p_out1, p_args1) = self._get_struct_print_formatted(self.struct_dict[s][m])
816 p_out += p_out1
817 p_args += p_args1
Tobin Ehlis6442dca2014-10-22 15:13:53 -0600818 p_out += '"'
Tobin Ehlis5178bd72015-03-06 10:38:25 -0700819 p_args += ");"
820 sh_funcs[-1] = '%s%s%s' % (sh_funcs[-1], p_out, p_args)
Tobin Ehlis5a487e52014-11-11 12:28:12 -0700821 if 0 != num_stps:
Tobin Ehlis5178bd72015-03-06 10:38:25 -0700822 sh_funcs.append(' for (int32_t stp_index = %i; stp_index >= 0; stp_index--) {' % (num_stps-1))
823 sh_funcs.append(' if (0 < strlen(stp_strs[stp_index])) {')
824 sh_funcs.append(' strncat(str, stp_strs[stp_index], strlen(stp_strs[stp_index]));')
825 sh_funcs.append(' free(stp_strs[stp_index]);')
826 sh_funcs.append(' }')
827 sh_funcs.append(' }')
828 sh_funcs.append(' free(extra_indent);')
829 sh_funcs.append(" return str;\n}")
Tobin Ehlis6442dca2014-10-22 15:13:53 -0600830 # Add function to dynamically print out unknown struct
Tobin Ehlis5178bd72015-03-06 10:38:25 -0700831 sh_funcs.append("char* dynamic_display(const void* pStruct, const char* prefix)\n{")
832 sh_funcs.append(" // Cast to APP_INFO ptr initially just to pull sType off struct")
833 sh_funcs.append(" if (pStruct == NULL) {")
834 sh_funcs.append(" return NULL;")
835 sh_funcs.append(" }")
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600836 sh_funcs.append(" VkStructureType sType = ((VkApplicationInfo*)pStruct)->sType;")
Tobin Ehlis5178bd72015-03-06 10:38:25 -0700837 sh_funcs.append(' char indent[100];\n strcpy(indent, " ");\n strcat(indent, prefix);')
838 sh_funcs.append(" switch (sType)\n {")
Tobin Ehlis6442dca2014-10-22 15:13:53 -0600839 for e in enum_type_dict:
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600840 if "StructureType" in e:
Tobin Ehlis6442dca2014-10-22 15:13:53 -0600841 for v in sorted(enum_type_dict[e]):
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -0600842 struct_name = get_struct_name_from_struct_type(v)
Chia-I Wu736324e2015-10-26 20:42:12 +0800843 if struct_name not in self.struct_dict:
844 continue
Tobin Ehlis6442dca2014-10-22 15:13:53 -0600845 print_func_name = self._get_sh_func_name(struct_name)
Tobin Ehlis5178bd72015-03-06 10:38:25 -0700846 sh_funcs.append(' case %s:\n {' % (v))
847 sh_funcs.append(' return %s((%s*)pStruct, indent);' % (print_func_name, struct_name))
848 sh_funcs.append(' }')
849 sh_funcs.append(' break;')
850 sh_funcs.append(" default:")
851 sh_funcs.append(" return NULL;")
852 sh_funcs.append(" }")
Tobin Ehlis6442dca2014-10-22 15:13:53 -0600853 sh_funcs.append("}")
Tobin Ehlis5178bd72015-03-06 10:38:25 -0700854 return "\n".join(sh_funcs)
Tobin Ehlis99f88672015-01-10 12:42:41 -0700855
856 def _generateStringHelperFunctionsCpp(self):
857 # declare str & tmp str
858 # declare array of stringstreams for every struct ptr in current struct
859 # declare array of stringstreams for every non-string element in current struct
Tobin Ehlis5178bd72015-03-06 10:38:25 -0700860 # For every struct ptr, if non-Null, then set its string, else set to NULL str
861 # For every non-string element, set its string stream
Tobin Ehlis99f88672015-01-10 12:42:41 -0700862 # create and return final string
863 sh_funcs = []
864 # First generate prototypes for every struct
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -0600865 # XXX - REMOVE this comment
Tobin Ehlis08fafd02015-06-12 12:49:01 -0600866 lineinfo = sourcelineinfo()
867 sh_funcs.append('%s' % lineinfo.get())
Ian Elliottf6b168c2015-12-11 15:03:05 -0700868 exclude_struct_list = ['VkAndroidSurfaceCreateInfoKHR',
869 'VkMirSurfaceCreateInfoKHR',
870 'VkWaylandSurfaceCreateInfoKHR',
871 'VkXlibSurfaceCreateInfoKHR']
872 if sys.platform == 'win32':
873 exclude_struct_list.append('VkXcbSurfaceCreateInfoKHR')
874 else:
875 exclude_struct_list.append('VkWin32SurfaceCreateInfoKHR')
Tobin Ehlis291cd702015-01-20 09:48:48 -0700876 for s in sorted(self.struct_dict):
Ian Elliottf6b168c2015-12-11 15:03:05 -0700877 if (typedef_fwd_dict[s] not in exclude_struct_list):
Michael Lentine33d96a32016-01-05 11:10:15 -0600878 if (re.match(r'.*Xcb.*', typedef_fwd_dict[s])):
879 sh_funcs.append("#ifdef VK_USE_PLATFORM_XCB_KHR")
Cody Northrop0a179fe2016-02-24 12:28:41 -0700880 if (re.match(r'.*Win32.*', typedef_fwd_dict[s])):
881 sh_funcs.append("#ifdef VK_USE_PLATFORM_WIN32_KHR")
Ian Elliottf6b168c2015-12-11 15:03:05 -0700882 sh_funcs.append('string %s(const %s* pStruct, const string prefix);' % (self._get_sh_func_name(s), typedef_fwd_dict[s]))
Cody Northrop0a179fe2016-02-24 12:28:41 -0700883 if (re.match(r'.*Win32.*', typedef_fwd_dict[s])):
884 sh_funcs.append("#endif //VK_USE_PLATFORM_WIN32_KHR")
Michael Lentine33d96a32016-01-05 11:10:15 -0600885 if (re.match(r'.*Xcb.*', typedef_fwd_dict[s])):
886 sh_funcs.append("#endif //VK_USE_PLATFORM_XCB_KHR")
Tobin Ehlis99f88672015-01-10 12:42:41 -0700887 sh_funcs.append('\n')
Tobin Ehlis08fafd02015-06-12 12:49:01 -0600888 sh_funcs.append('%s' % lineinfo.get())
Tobin Ehlis291cd702015-01-20 09:48:48 -0700889 for s in sorted(self.struct_dict):
Tobin Ehlisce320942015-06-11 09:37:00 -0600890 num_non_enum_elems = [(is_type(self.struct_dict[s][elem]['type'], 'enum') and not self.struct_dict[s][elem]['ptr']) for elem in self.struct_dict[s]].count(False)
Tobin Ehlis99f88672015-01-10 12:42:41 -0700891 stp_list = [] # stp == "struct to print" a list of structs for this API call that should be printed as structs
Tobin Ehlis5178bd72015-03-06 10:38:25 -0700892 # This pre-pass flags embedded structs and pNext
Tobin Ehlis291cd702015-01-20 09:48:48 -0700893 for m in sorted(self.struct_dict[s]):
Tobin Ehlis5178bd72015-03-06 10:38:25 -0700894 if 'pNext' == self.struct_dict[s][m]['name'] or is_type(self.struct_dict[s][m]['type'], 'struct') or self.struct_dict[s][m]['array']:
Courtney Goeltzenleuchter82e72392015-07-10 09:06:56 -0600895 # TODO: This is a tmp workaround
896 if 'ppActiveLayerNames' not in self.struct_dict[s][m]['name']:
897 stp_list.append(self.struct_dict[s][m])
Ian Elliottf6b168c2015-12-11 15:03:05 -0700898 if (typedef_fwd_dict[s] in exclude_struct_list):
899 continue
Tobin Ehlis08fafd02015-06-12 12:49:01 -0600900 sh_funcs.append('%s' % lineinfo.get())
Michael Lentine33d96a32016-01-05 11:10:15 -0600901 if (re.match(r'.*Xcb.*', typedef_fwd_dict[s])):
902 sh_funcs.append("#ifdef VK_USE_PLATFORM_XCB_KHR")
Cody Northrop0a179fe2016-02-24 12:28:41 -0700903 if (re.match(r'.*Win32.*', typedef_fwd_dict[s])):
904 sh_funcs.append("#ifdef VK_USE_PLATFORM_WIN32_KHR")
Tobin Ehlis99f88672015-01-10 12:42:41 -0700905 sh_funcs.append('string %s(const %s* pStruct, const string prefix)\n{' % (self._get_sh_func_name(s), typedef_fwd_dict[s]))
Tobin Ehlis08fafd02015-06-12 12:49:01 -0600906 sh_funcs.append('%s' % lineinfo.get())
Tobin Ehlis5178bd72015-03-06 10:38:25 -0700907 indent = ' '
Tobin Ehlise78dbd82015-04-09 09:19:36 -0600908 sh_funcs.append('%susing namespace StreamControl;' % (indent))
Tobin Ehlis5178bd72015-03-06 10:38:25 -0700909 sh_funcs.append('%sstring final_str;' % (indent))
910 sh_funcs.append('%sstring tmp_str;' % (indent))
911 sh_funcs.append('%sstring extra_indent = " " + prefix;' % (indent))
Tobin Ehlis291cd702015-01-20 09:48:48 -0700912 if (0 != num_non_enum_elems):
Tobin Ehlis5178bd72015-03-06 10:38:25 -0700913 sh_funcs.append('%sstringstream ss[%u];' % (indent, num_non_enum_elems))
Tobin Ehlis99f88672015-01-10 12:42:41 -0700914 num_stps = len(stp_list)
Tobin Ehlisce320942015-06-11 09:37:00 -0600915 # First generate code for any embedded structs or arrays
Tobin Ehlis99f88672015-01-10 12:42:41 -0700916 if 0 < num_stps:
Tobin Ehlis5178bd72015-03-06 10:38:25 -0700917 sh_funcs.append('%sstring stp_strs[%u];' % (indent, num_stps))
Tobin Ehlis99f88672015-01-10 12:42:41 -0700918 idx_ss_decl = False # Make sure to only decl this once
919 for index in range(num_stps):
920 addr_char = '&'
Tobin Ehlis5178bd72015-03-06 10:38:25 -0700921 if 1 < stp_list[index]['full_type'].count('*'):
Tobin Ehlis99f88672015-01-10 12:42:41 -0700922 addr_char = ''
Tobin Ehlisf03e8cd2015-06-30 15:28:00 -0600923 if stp_list[index]['array']:
Tobin Ehlis08fafd02015-06-12 12:49:01 -0600924 sh_funcs.append('%s' % lineinfo.get())
Tobin Ehlis5178bd72015-03-06 10:38:25 -0700925 if stp_list[index]['dyn_array']:
Tobin Ehlis08fafd02015-06-12 12:49:01 -0600926 sh_funcs.append('%s' % lineinfo.get())
Tobin Ehlis5178bd72015-03-06 10:38:25 -0700927 array_count = 'pStruct->%s' % (stp_list[index]['array_size'])
928 else:
Tobin Ehlis08fafd02015-06-12 12:49:01 -0600929 sh_funcs.append('%s' % lineinfo.get())
Tobin Ehlis5178bd72015-03-06 10:38:25 -0700930 array_count = '%s' % (stp_list[index]['array_size'])
Tobin Ehlis08fafd02015-06-12 12:49:01 -0600931 sh_funcs.append('%s' % lineinfo.get())
Tobin Ehlis5178bd72015-03-06 10:38:25 -0700932 sh_funcs.append('%sstp_strs[%u] = "";' % (indent, index))
933 if not idx_ss_decl:
934 sh_funcs.append('%sstringstream index_ss;' % (indent))
935 idx_ss_decl = True
Mark Lobodzinski74dfa182015-08-11 09:35:02 -0600936 if (stp_list[index]['name'] == 'pQueueFamilyIndices'):
Ian Elliott05846062015-11-20 14:13:17 -0700937 if (typedef_fwd_dict[s] == 'VkSwapchainCreateInfoKHR'):
938 sh_funcs.append('%sif (pStruct->imageSharingMode == VK_SHARING_MODE_CONCURRENT) {' % (indent))
939 else:
940 sh_funcs.append('%sif (pStruct->sharingMode == VK_SHARING_MODE_CONCURRENT) {' % (indent))
Mark Lobodzinski74dfa182015-08-11 09:35:02 -0600941 indent += ' '
Mark Lobodzinski80971c62016-01-08 14:52:49 -0700942 if (stp_list[index]['name'] == 'pImageInfo'):
943 sh_funcs.append('%sif ((pStruct->descriptorType == VK_DESCRIPTOR_TYPE_SAMPLER) ||' % (indent))
944 sh_funcs.append('%s (pStruct->descriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER) ||' % (indent))
945 sh_funcs.append('%s (pStruct->descriptorType == VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE) ||' % (indent))
946 sh_funcs.append('%s (pStruct->descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_IMAGE)) {' % (indent))
947 indent += ' '
948 elif (stp_list[index]['name'] == 'pBufferInfo'):
949 sh_funcs.append('%sif ((pStruct->descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER) ||' % (indent))
950 sh_funcs.append('%s (pStruct->descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER) ||' % (indent))
951 sh_funcs.append('%s (pStruct->descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC) ||' % (indent))
952 sh_funcs.append('%s (pStruct->descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC)) {' % (indent))
953 indent += ' '
954 elif (stp_list[index]['name'] == 'pTexelBufferView'):
955 sh_funcs.append('%sif ((pStruct->descriptorType == VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER) ||' % (indent))
956 sh_funcs.append('%s (pStruct->descriptorType == VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER)) {' % (indent))
957 indent += ' '
Michael Lentine0a369f62016-02-03 16:51:46 -0600958 if stp_list[index]['dyn_array']:
959 sh_funcs.append('%sif (pStruct->%s) {' % (indent, stp_list[index]['name']))
960 indent += ' '
Tobin Ehlis5178bd72015-03-06 10:38:25 -0700961 sh_funcs.append('%sfor (uint32_t i = 0; i < %s; i++) {' % (indent, array_count))
Tobin Ehlis793ad302015-04-03 12:01:11 -0600962 indent += ' '
Tobin Ehlis5178bd72015-03-06 10:38:25 -0700963 sh_funcs.append('%sindex_ss.str("");' % (indent))
964 sh_funcs.append('%sindex_ss << i;' % (indent))
Tobin Ehlisce320942015-06-11 09:37:00 -0600965 if is_type(stp_list[index]['type'], 'enum'):
Tobin Ehlis08fafd02015-06-12 12:49:01 -0600966 sh_funcs.append('%s' % lineinfo.get())
Tobin Ehlis5178bd72015-03-06 10:38:25 -0700967 addr_char = ''
Tobin Ehlisce320942015-06-11 09:37:00 -0600968 #value_print = 'string_%s(%spStruct->%s)' % (self.struct_dict[s][m]['type'], deref, self.struct_dict[s][m]['name'])
969 sh_funcs.append('%sss[%u] << string_%s(pStruct->%s[i]);' % (indent, index, stp_list[index]['type'], stp_list[index]['name']))
Tobin Ehlis5178bd72015-03-06 10:38:25 -0700970 sh_funcs.append('%sstp_strs[%u] += " " + prefix + "%s[" + index_ss.str() + "] = " + ss[%u].str() + "\\n";' % (indent, index, stp_list[index]['name'], index))
Tobin Ehlisce320942015-06-11 09:37:00 -0600971 elif is_type(stp_list[index]['type'], 'struct'):
Tobin Ehlis08fafd02015-06-12 12:49:01 -0600972 sh_funcs.append('%s' % lineinfo.get())
Tobin Ehlis5178bd72015-03-06 10:38:25 -0700973 sh_funcs.append('%sss[%u] << %spStruct->%s[i];' % (indent, index, addr_char, stp_list[index]['name']))
974 sh_funcs.append('%stmp_str = %s(%spStruct->%s[i], extra_indent);' % (indent, self._get_sh_func_name(stp_list[index]['type']), addr_char, stp_list[index]['name']))
975 if self.no_addr:
Tobin Ehlis08fafd02015-06-12 12:49:01 -0600976 sh_funcs.append('%s' % lineinfo.get())
Tobin Ehlis5178bd72015-03-06 10:38:25 -0700977 sh_funcs.append('%sstp_strs[%u] += " " + prefix + "%s[" + index_ss.str() + "] (addr)\\n" + tmp_str;' % (indent, index, stp_list[index]['name']))
978 else:
Tobin Ehlis08fafd02015-06-12 12:49:01 -0600979 sh_funcs.append('%s' % lineinfo.get())
Tobin Ehlis5178bd72015-03-06 10:38:25 -0700980 sh_funcs.append('%sstp_strs[%u] += " " + prefix + "%s[" + index_ss.str() + "] (" + ss[%u].str() + ")\\n" + tmp_str;' % (indent, index, stp_list[index]['name'], index))
Tobin Ehlisce320942015-06-11 09:37:00 -0600981 else:
Tobin Ehlis08fafd02015-06-12 12:49:01 -0600982 sh_funcs.append('%s' % lineinfo.get())
Tobin Ehlisce320942015-06-11 09:37:00 -0600983 addr_char = ''
Karl Schultzcb855192016-03-04 15:20:27 -0700984 sh_funcs.append('%sss[%u] << %spStruct->%s[i];' % (indent, index, addr_char, stp_list[index]['name']))
Courtney Goeltzenleuchter44981552015-10-23 15:48:32 -0600985 if stp_list[index]['type'] in vulkan.core.objects:
Tobin Ehlisa30e7e52015-07-06 14:02:36 -0600986 sh_funcs.append('%sstp_strs[%u] += " " + prefix + "%s[" + index_ss.str() + "].handle = " + ss[%u].str() + "\\n";' % (indent, index, stp_list[index]['name'], index))
987 else:
Tobin Ehlisa30e7e52015-07-06 14:02:36 -0600988 sh_funcs.append('%sstp_strs[%u] += " " + prefix + "%s[" + index_ss.str() + "] = " + ss[%u].str() + "\\n";' % (indent, index, stp_list[index]['name'], index))
Tobin Ehlis08fafd02015-06-12 12:49:01 -0600989 sh_funcs.append('%s' % lineinfo.get())
Tobin Ehlis5178bd72015-03-06 10:38:25 -0700990 sh_funcs.append('%sss[%u].str("");' % (indent, index))
Tobin Ehlis793ad302015-04-03 12:01:11 -0600991 indent = indent[4:]
Tobin Ehlis5178bd72015-03-06 10:38:25 -0700992 sh_funcs.append('%s}' % (indent))
Michael Lentine0a369f62016-02-03 16:51:46 -0600993 if stp_list[index]['dyn_array']:
994 indent = indent[4:]
995 sh_funcs.append('%s}' % (indent))
Mark Lobodzinski74dfa182015-08-11 09:35:02 -0600996 #endif
Mark Lobodzinski80971c62016-01-08 14:52:49 -0700997 if (stp_list[index]['name'] == 'pQueueFamilyIndices') or (stp_list[index]['name'] == 'pImageInfo') or (stp_list[index]['name'] == 'pBufferInfo') or (stp_list[index]['name'] == 'pTexelBufferView'):
Mark Lobodzinski74dfa182015-08-11 09:35:02 -0600998 indent = indent[4:]
999 sh_funcs.append('%s}' % (indent))
Tobin Ehlis5178bd72015-03-06 10:38:25 -07001000 elif (stp_list[index]['ptr']):
Tobin Ehlis08fafd02015-06-12 12:49:01 -06001001 sh_funcs.append('%s' % lineinfo.get())
Tobin Ehlisf03e8cd2015-06-30 15:28:00 -06001002 sh_funcs.append('%sif (pStruct->%s) {' % (indent, stp_list[index]['name']))
1003 indent += ' '
Tobin Ehlis99f88672015-01-10 12:42:41 -07001004 if 'pNext' == stp_list[index]['name']:
Tobin Ehlis08fafd02015-06-12 12:49:01 -06001005 sh_funcs.append('%s' % lineinfo.get())
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001006 sh_funcs.append(' tmp_str = dynamic_display((void*)pStruct->pNext, prefix);')
Tobin Ehlis99f88672015-01-10 12:42:41 -07001007 else:
Tobin Ehlisb8013a22015-02-26 12:57:08 -07001008 if stp_list[index]['name'] in ['pImageViews', 'pBufferViews']:
1009 # TODO : This is a quick hack to handle these arrays of ptrs
Tobin Ehlis08fafd02015-06-12 12:49:01 -06001010 sh_funcs.append('%s' % lineinfo.get())
Courtney Goeltzenleuchtereeae16b2015-04-02 22:54:15 -06001011 sh_funcs.append(' tmp_str = %s(&pStruct->%s[0], extra_indent);' % (self._get_sh_func_name(stp_list[index]['type']), stp_list[index]['name']))
Tobin Ehlisb8013a22015-02-26 12:57:08 -07001012 else:
Tobin Ehlis08fafd02015-06-12 12:49:01 -06001013 sh_funcs.append('%s' % lineinfo.get())
Tobin Ehlisb8013a22015-02-26 12:57:08 -07001014 sh_funcs.append(' tmp_str = %s(pStruct->%s, extra_indent);' % (self._get_sh_func_name(stp_list[index]['type']), stp_list[index]['name']))
Tobin Ehlis99f88672015-01-10 12:42:41 -07001015 sh_funcs.append(' ss[%u] << %spStruct->%s;' % (index, addr_char, stp_list[index]['name']))
1016 if self.no_addr:
Tobin Ehlis08fafd02015-06-12 12:49:01 -06001017 sh_funcs.append('%s' % lineinfo.get())
Tobin Ehlis99f88672015-01-10 12:42:41 -07001018 sh_funcs.append(' stp_strs[%u] = " " + prefix + "%s (addr)\\n" + tmp_str;' % (index, stp_list[index]['name']))
1019 else:
Tobin Ehlis08fafd02015-06-12 12:49:01 -06001020 sh_funcs.append('%s' % lineinfo.get())
Tobin Ehlis99f88672015-01-10 12:42:41 -07001021 sh_funcs.append(' stp_strs[%u] = " " + prefix + "%s (" + ss[%u].str() + ")\\n" + tmp_str;' % (index, stp_list[index]['name'], index))
1022 sh_funcs.append(' ss[%u].str("");' % (index))
1023 sh_funcs.append(' }')
1024 sh_funcs.append(' else')
1025 sh_funcs.append(' stp_strs[%u] = "";' % index)
Tobin Ehlis99f88672015-01-10 12:42:41 -07001026 else:
Tobin Ehlis08fafd02015-06-12 12:49:01 -06001027 sh_funcs.append('%s' % lineinfo.get())
Tobin Ehlis99f88672015-01-10 12:42:41 -07001028 sh_funcs.append(' tmp_str = %s(&pStruct->%s, extra_indent);' % (self._get_sh_func_name(stp_list[index]['type']), stp_list[index]['name']))
1029 sh_funcs.append(' ss[%u] << %spStruct->%s;' % (index, addr_char, stp_list[index]['name']))
1030 if self.no_addr:
1031 sh_funcs.append(' stp_strs[%u] = " " + prefix + "%s (addr)\\n" + tmp_str;' % (index, stp_list[index]['name']))
Tobin Ehlis08fafd02015-06-12 12:49:01 -06001032 sh_funcs.append('%s' % lineinfo.get())
Tobin Ehlis99f88672015-01-10 12:42:41 -07001033 else:
1034 sh_funcs.append(' stp_strs[%u] = " " + prefix + "%s (" + ss[%u].str() + ")\\n" + tmp_str;' % (index, stp_list[index]['name'], index))
Tobin Ehlis08fafd02015-06-12 12:49:01 -06001035 sh_funcs.append('%s' % lineinfo.get())
Tobin Ehlis99f88672015-01-10 12:42:41 -07001036 sh_funcs.append(' ss[%u].str("");' % index)
Tobin Ehlisce320942015-06-11 09:37:00 -06001037 # Now print one-line info for all data members
Tobin Ehlis99f88672015-01-10 12:42:41 -07001038 index = 0
1039 final_str = ''
1040 for m in sorted(self.struct_dict[s]):
1041 if not is_type(self.struct_dict[s][m]['type'], 'enum'):
1042 if is_type(self.struct_dict[s][m]['type'], 'struct') and not self.struct_dict[s][m]['ptr']:
1043 if self.no_addr:
Tobin Ehlis08fafd02015-06-12 12:49:01 -06001044 sh_funcs.append('%s' % lineinfo.get())
Tobin Ehlis99f88672015-01-10 12:42:41 -07001045 sh_funcs.append(' ss[%u].str("addr");' % (index))
1046 else:
Tobin Ehlis08fafd02015-06-12 12:49:01 -06001047 sh_funcs.append('%s' % lineinfo.get())
Tobin Ehlis99f88672015-01-10 12:42:41 -07001048 sh_funcs.append(' ss[%u] << &pStruct->%s;' % (index, self.struct_dict[s][m]['name']))
Jon Ashburnc669cc62015-07-09 15:02:25 -06001049 elif self.struct_dict[s][m]['array']:
1050 sh_funcs.append('%s' % lineinfo.get())
1051 sh_funcs.append(' ss[%u] << (void*)pStruct->%s;' % (index, self.struct_dict[s][m]['name']))
Tobin Ehlis5178bd72015-03-06 10:38:25 -07001052 elif 'bool' in self.struct_dict[s][m]['type'].lower():
Tobin Ehlis08fafd02015-06-12 12:49:01 -06001053 sh_funcs.append('%s' % lineinfo.get())
Tobin Ehlis99f88672015-01-10 12:42:41 -07001054 sh_funcs.append(' ss[%u].str(pStruct->%s ? "TRUE" : "FALSE");' % (index, self.struct_dict[s][m]['name']))
Tobin Ehlis5178bd72015-03-06 10:38:25 -07001055 elif 'uint8' in self.struct_dict[s][m]['type'].lower():
Tobin Ehlis08fafd02015-06-12 12:49:01 -06001056 sh_funcs.append('%s' % lineinfo.get())
Jon Ashburnc669cc62015-07-09 15:02:25 -06001057 sh_funcs.append(' ss[%u] << pStruct->%s;' % (index, self.struct_dict[s][m]['name']))
Tobin Ehlis17f45a62015-04-28 17:27:16 -06001058 elif 'void' in self.struct_dict[s][m]['type'].lower() and self.struct_dict[s][m]['ptr']:
Tobin Ehlis08fafd02015-06-12 12:49:01 -06001059 sh_funcs.append('%s' % lineinfo.get())
Tobin Ehlis17f45a62015-04-28 17:27:16 -06001060 sh_funcs.append(' if (StreamControl::writeAddress)')
1061 sh_funcs.append(' ss[%u] << pStruct->%s;' % (index, self.struct_dict[s][m]['name']))
1062 sh_funcs.append(' else')
1063 sh_funcs.append(' ss[%u].str("address");' % (index))
Mark Youngd46f6e92016-01-27 17:46:16 -07001064 elif 'char' in self.struct_dict[s][m]['type'].lower() and self.struct_dict[s][m]['ptr']:
1065 sh_funcs.append('%s' % lineinfo.get())
Jon Ashburn34edc122016-03-10 14:21:09 -07001066 sh_funcs.append(' if (pStruct->%s != NULL) {' % self.struct_dict[s][m]['name'])
1067 sh_funcs.append(' ss[%u] << pStruct->%s;' % (index, self.struct_dict[s][m]['name']))
1068 sh_funcs.append(' } else {')
1069 sh_funcs.append(' ss[%u] << "";' % index)
1070 sh_funcs.append(' }')
Tobin Ehlis99f88672015-01-10 12:42:41 -07001071 else:
Tobin Ehlis08fafd02015-06-12 12:49:01 -06001072 sh_funcs.append('%s' % lineinfo.get())
Tobin Ehlis99f88672015-01-10 12:42:41 -07001073 (po, pa) = self._get_struct_print_formatted(self.struct_dict[s][m])
Tobin Ehlis5178bd72015-03-06 10:38:25 -07001074 if "addr" in po: # or self.struct_dict[s][m]['ptr']:
Tobin Ehlis99f88672015-01-10 12:42:41 -07001075 sh_funcs.append(' ss[%u].str("addr");' % (index))
Tobin Ehlisa30e7e52015-07-06 14:02:36 -06001076 elif not self.struct_dict[s][m]['ptr'] and self.struct_dict[s][m]['type'] in vulkan.core.objects:
Chia-I Wue2fc5522015-10-26 20:04:44 +08001077 sh_funcs.append(' ss[%u] << pStruct->%s;' % (index, self.struct_dict[s][m]['name']))
Tobin Ehlis99f88672015-01-10 12:42:41 -07001078 else:
1079 sh_funcs.append(' ss[%u] << pStruct->%s;' % (index, self.struct_dict[s][m]['name']))
1080 value_print = 'ss[%u].str()' % index
1081 index += 1
1082 else:
Tobin Ehlisce320942015-06-11 09:37:00 -06001083 # For an non-empty array of enums just print address w/ note that array will be displayed below
Tobin Ehlis7e65d752015-01-15 17:51:52 -07001084 if self.struct_dict[s][m]['ptr']:
Tobin Ehlis08fafd02015-06-12 12:49:01 -06001085 sh_funcs.append('%s' % lineinfo.get())
Tobin Ehlisce320942015-06-11 09:37:00 -06001086 sh_funcs.append(' if (pStruct->%s)' % (self.struct_dict[s][m]['name']))
1087 sh_funcs.append(' ss[%u] << pStruct->%s << " (See individual array values below)";' % (index, self.struct_dict[s][m]['name']))
1088 sh_funcs.append(' else')
1089 sh_funcs.append(' ss[%u].str("NULL");' % (index))
1090 value_print = 'ss[%u].str()' % index
1091 index += 1
1092 # For single enum just print the string representation
1093 else:
1094 value_print = 'string_%s(pStruct->%s)' % (self.struct_dict[s][m]['type'], self.struct_dict[s][m]['name'])
Tobin Ehlis99f88672015-01-10 12:42:41 -07001095 final_str += ' + prefix + "%s = " + %s + "\\n"' % (self.struct_dict[s][m]['name'], value_print)
1096 final_str = final_str[3:] # strip off the initial ' + '
Tobin Ehlis5178bd72015-03-06 10:38:25 -07001097 if 0 != num_stps: # Append data for any embedded structs
Tobin Ehlis99f88672015-01-10 12:42:41 -07001098 final_str += " + %s" % " + ".join(['stp_strs[%u]' % n for n in reversed(range(num_stps))])
Tobin Ehlis08fafd02015-06-12 12:49:01 -06001099 sh_funcs.append('%s' % lineinfo.get())
Tobin Ehlis99f88672015-01-10 12:42:41 -07001100 sh_funcs.append(' final_str = %s;' % final_str)
1101 sh_funcs.append(' return final_str;\n}')
Cody Northrop0a179fe2016-02-24 12:28:41 -07001102 if (re.match(r'.*Win32.*', typedef_fwd_dict[s])):
1103 sh_funcs.append("#endif //VK_USE_PLATFORM_WIN32_KHR")
Michael Lentine33d96a32016-01-05 11:10:15 -06001104 if (re.match(r'.*Xcb.*', typedef_fwd_dict[s])):
1105 sh_funcs.append("#endif //VK_USE_PLATFORM_XCB_KHR")
Tobin Ehlis291cd702015-01-20 09:48:48 -07001106 # Add function to return a string value for input void*
Tobin Ehlis08fafd02015-06-12 12:49:01 -06001107 sh_funcs.append('%s' % lineinfo.get())
Tobin Ehlis291cd702015-01-20 09:48:48 -07001108 sh_funcs.append("string string_convert_helper(const void* toString, const string prefix)\n{")
Tobin Ehlise78dbd82015-04-09 09:19:36 -06001109 sh_funcs.append(" using namespace StreamControl;")
Tobin Ehlis291cd702015-01-20 09:48:48 -07001110 sh_funcs.append(" stringstream ss;")
1111 sh_funcs.append(' ss << toString;')
1112 sh_funcs.append(' string final_str = prefix + ss.str();')
1113 sh_funcs.append(" return final_str;")
1114 sh_funcs.append("}")
Tobin Ehlis08fafd02015-06-12 12:49:01 -06001115 sh_funcs.append('%s' % lineinfo.get())
Tony Barbourb5d2c942015-07-14 13:34:05 -06001116 # Add function to return a string value for input uint64_t
1117 sh_funcs.append("string string_convert_helper(const uint64_t toString, const string prefix)\n{")
Tobin Ehlis08541742015-04-16 15:56:11 -06001118 sh_funcs.append(" using namespace StreamControl;")
1119 sh_funcs.append(" stringstream ss;")
1120 sh_funcs.append(' ss << toString;')
1121 sh_funcs.append(' string final_str = prefix + ss.str();')
1122 sh_funcs.append(" return final_str;")
1123 sh_funcs.append("}")
Tobin Ehlis08fafd02015-06-12 12:49:01 -06001124 sh_funcs.append('%s' % lineinfo.get())
Ian Elliott7e40db92015-08-21 15:09:33 -06001125 # Add function to return a string value for input VkSurfaceFormatKHR*
1126 sh_funcs.append("string string_convert_helper(VkSurfaceFormatKHR toString, const string prefix)\n{")
1127 sh_funcs.append(' string final_str = prefix + "format = " + string_VkFormat(toString.format) + "format = " + string_VkColorSpaceKHR(toString.colorSpace);')
Ian Elliott2b6b68a2015-08-07 14:11:14 -06001128 sh_funcs.append(" return final_str;")
1129 sh_funcs.append("}")
1130 sh_funcs.append('%s' % lineinfo.get())
Tobin Ehlis99f88672015-01-10 12:42:41 -07001131 # Add function to dynamically print out unknown struct
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001132 sh_funcs.append("string dynamic_display(const void* pStruct, const string prefix)\n{")
Tobin Ehlis99f88672015-01-10 12:42:41 -07001133 sh_funcs.append(" // Cast to APP_INFO ptr initially just to pull sType off struct")
Ian Elliott82800022015-02-25 17:40:38 -07001134 sh_funcs.append(" if (pStruct == NULL) {\n")
Michael Lentinefb3b23b2015-10-23 12:40:50 -07001135 sh_funcs.append(" return string();")
Ian Elliott82800022015-02-25 17:40:38 -07001136 sh_funcs.append(" }\n")
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001137 sh_funcs.append(" VkStructureType sType = ((VkApplicationInfo*)pStruct)->sType;")
Tobin Ehlis99f88672015-01-10 12:42:41 -07001138 sh_funcs.append(' string indent = " ";')
1139 sh_funcs.append(' indent += prefix;')
1140 sh_funcs.append(" switch (sType)\n {")
1141 for e in enum_type_dict:
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001142 if "StructureType" in e:
Tobin Ehlis99f88672015-01-10 12:42:41 -07001143 for v in sorted(enum_type_dict[e]):
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -06001144 struct_name = get_struct_name_from_struct_type(v)
Chia-I Wu736324e2015-10-26 20:42:12 +08001145 if struct_name not in self.struct_dict:
1146 continue
Tobin Ehlis99f88672015-01-10 12:42:41 -07001147 print_func_name = self._get_sh_func_name(struct_name)
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001148 #sh_funcs.append('string %s(const %s* pStruct, const string prefix);' % (self._get_sh_func_name(s), typedef_fwd_dict[s]))
Tobin Ehlis99f88672015-01-10 12:42:41 -07001149 sh_funcs.append(' case %s:\n {' % (v))
1150 sh_funcs.append(' return %s((%s*)pStruct, indent);' % (print_func_name, struct_name))
1151 sh_funcs.append(' }')
1152 sh_funcs.append(' break;')
1153 sh_funcs.append(" default:")
Michael Lentinefb3b23b2015-10-23 12:40:50 -07001154 sh_funcs.append(" return string();")
Tobin Ehlis08fafd02015-06-12 12:49:01 -06001155 sh_funcs.append('%s' % lineinfo.get())
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001156 sh_funcs.append(" }")
Tobin Ehlis99f88672015-01-10 12:42:41 -07001157 sh_funcs.append("}")
1158 return "\n".join(sh_funcs)
Mark Lobodzinski28b8f022015-08-27 15:30:29 -06001159
Tobin Ehlis6442dca2014-10-22 15:13:53 -06001160 def _genStructMemberPrint(self, member, s, array, struct_array):
1161 (p_out, p_arg) = self._get_struct_print_formatted(self.struct_dict[s][member], pre_var_name="&m_dummy_prefix", struct_var_name="m_struct", struct_ptr=False, print_array=True)
1162 extra_indent = ""
1163 if array:
1164 extra_indent = " "
1165 if is_type(self.struct_dict[s][member]['type'], 'struct'): # print struct address for now
1166 struct_array.insert(0, self.struct_dict[s][member])
1167 elif self.struct_dict[s][member]['ptr']:
Tobin Ehlis7e65d752015-01-15 17:51:52 -07001168 # Special case for void* named "pNext"
1169 if "void" in self.struct_dict[s][member]['type'] and "pNext" == self.struct_dict[s][member]['name']:
Tobin Ehlis6442dca2014-10-22 15:13:53 -06001170 struct_array.insert(0, self.struct_dict[s][member])
1171 return (' %sprintf("%%*s %s", m_indent, ""%s);' % (extra_indent, p_out, p_arg), struct_array)
1172
1173 def _generateDisplayDefinitions(self, s):
1174 disp_def = []
1175 struct_array = []
1176 # Single-line struct print function
1177 disp_def.append("// Output 'structname = struct_address' on a single line")
1178 disp_def.append("void %s::display_single_txt()\n{" % self.get_class_name(s))
1179 disp_def.append(' printf(" %%*s%s = %%p", m_indent, "", (void*)m_origStructAddr);' % typedef_fwd_dict[s])
1180 disp_def.append("}\n")
1181 # Private helper function to print struct members
1182 disp_def.append("// Private helper function that displays the members of the wrapped struct")
1183 disp_def.append("void %s::display_struct_members()\n{" % self.get_class_name(s))
1184 i_declared = False
1185 for member in sorted(self.struct_dict[s]):
1186 # TODO : Need to display each member based on its type
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001187 # TODO : Need to handle pNext which are structs, but of void* type
Tobin Ehlis6442dca2014-10-22 15:13:53 -06001188 # Can grab struct type off of header of struct pointed to
1189 # TODO : Handle Arrays
1190 if self.struct_dict[s][member]['array']:
1191 # Create for loop to print each element of array
1192 if not i_declared:
1193 disp_def.append(' uint32_t i;')
1194 i_declared = True
1195 disp_def.append(' for (i = 0; i<%s; i++) {' % self.struct_dict[s][member]['array_size'])
1196 (return_str, struct_array) = self._genStructMemberPrint(member, s, True, struct_array)
1197 disp_def.append(return_str)
1198 disp_def.append(' }')
1199 else:
1200 (return_str, struct_array) = self._genStructMemberPrint(member, s, False, struct_array)
1201 disp_def.append(return_str)
1202 disp_def.append("}\n")
1203 i_declared = False
1204 # Basic print function to display struct members
1205 disp_def.append("// Output all struct elements, each on their own line")
1206 disp_def.append("void %s::display_txt()\n{" % self.get_class_name(s))
1207 disp_def.append(' printf("%%*s%s struct contents at %%p:\\n", m_indent, "", (void*)m_origStructAddr);' % typedef_fwd_dict[s])
1208 disp_def.append(' this->display_struct_members();')
1209 disp_def.append("}\n")
1210 # Advanced print function to display current struct and contents of any pointed-to structs
1211 disp_def.append("// Output all struct elements, and for any structs pointed to, print complete contents")
1212 disp_def.append("void %s::display_full_txt()\n{" % self.get_class_name(s))
1213 disp_def.append(' printf("%%*s%s struct contents at %%p:\\n", m_indent, "", (void*)m_origStructAddr);' % typedef_fwd_dict[s])
1214 disp_def.append(' this->display_struct_members();')
1215 class_num = 0
1216 # TODO : Need to handle arrays of structs here
1217 for ms in struct_array:
1218 swc_name = "class%s" % str(class_num)
1219 if ms['array']:
1220 if not i_declared:
1221 disp_def.append(' uint32_t i;')
1222 i_declared = True
1223 disp_def.append(' for (i = 0; i<%s; i++) {' % ms['array_size'])
1224 #disp_def.append(" if (m_struct.%s[i]) {" % (ms['name']))
1225 disp_def.append(" %s %s(&(m_struct.%s[i]));" % (self.get_class_name(ms['type']), swc_name, ms['name']))
1226 disp_def.append(" %s.set_indent(m_indent + 4);" % (swc_name))
1227 disp_def.append(" %s.display_full_txt();" % (swc_name))
1228 #disp_def.append(' }')
1229 disp_def.append(' }')
1230 elif 'pNext' == ms['name']:
1231 # Need some code trickery here
1232 # I'm thinking have a generated function that takes pNext ptr value
1233 # then it checks sType and in large switch statement creates appropriate
1234 # wrapper class type and then prints contents
1235 disp_def.append(" if (m_struct.%s) {" % (ms['name']))
1236 #disp_def.append(' printf("%*s This is where we would call dynamic print function\\n", m_indent, "");')
1237 disp_def.append(' dynamic_display_full_txt(m_struct.%s, m_indent);' % (ms['name']))
1238 disp_def.append(" }")
1239 else:
1240 if ms['ptr']:
1241 disp_def.append(" if (m_struct.%s) {" % (ms['name']))
1242 disp_def.append(" %s %s(m_struct.%s);" % (self.get_class_name(ms['type']), swc_name, ms['name']))
1243 else:
1244 disp_def.append(" if (&m_struct.%s) {" % (ms['name']))
1245 disp_def.append(" %s %s(&m_struct.%s);" % (self.get_class_name(ms['type']), swc_name, ms['name']))
1246 disp_def.append(" %s.set_indent(m_indent + 4);" % (swc_name))
1247 disp_def.append(" %s.display_full_txt();\n }" % (swc_name))
1248 class_num += 1
1249 disp_def.append("}\n")
1250 return "\n".join(disp_def)
Mark Lobodzinski28b8f022015-08-27 15:30:29 -06001251
Tobin Ehlis6442dca2014-10-22 15:13:53 -06001252 def _generateStringHelperHeader(self):
1253 header = []
1254 header.append("//#includes, #defines, globals and such...\n")
1255 for f in self.include_headers:
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001256 if 'vk_enum_string_helper' not in f:
Tobin Ehlis6442dca2014-10-22 15:13:53 -06001257 header.append("#include <%s>\n" % f)
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001258 header.append('#include "vk_enum_string_helper.h"\n\n// Function Prototypes\n')
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001259 header.append("char* dynamic_display(const void* pStruct, const char* prefix);\n")
Tobin Ehlis6442dca2014-10-22 15:13:53 -06001260 return "".join(header)
Tobin Ehlis99f88672015-01-10 12:42:41 -07001261
1262 def _generateStringHelperHeaderCpp(self):
1263 header = []
1264 header.append("//#includes, #defines, globals and such...\n")
1265 for f in self.include_headers:
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001266 if 'vk_enum_string_helper' not in f:
Tobin Ehlis99f88672015-01-10 12:42:41 -07001267 header.append("#include <%s>\n" % f)
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001268 header.append('#include "vk_enum_string_helper.h"\n')
Tobin Ehlis99f88672015-01-10 12:42:41 -07001269 header.append('using namespace std;\n\n// Function Prototypes\n')
Tobin Ehlise78dbd82015-04-09 09:19:36 -06001270 header.append('\n')
1271 header.append('namespace StreamControl\n')
1272 header.append('{\n')
1273 header.append('bool writeAddress = true;\n')
1274 header.append('template <typename T>\n')
1275 header.append('std::ostream& operator<< (std::ostream &out, T const* pointer)\n')
1276 header.append('{\n')
1277 header.append(' if(writeAddress)\n')
1278 header.append(' {\n')
1279 header.append(' out.operator<<(pointer);\n')
1280 header.append(' }\n')
1281 header.append(' else\n')
1282 header.append(' {\n')
1283 header.append(' std::operator<<(out, "address");\n')
1284 header.append(' }\n')
1285 header.append(' return out;\n')
1286 header.append('}\n')
1287 header.append('std::ostream& operator<<(std::ostream &out, char const*const s)\n')
1288 header.append('{\n')
1289 header.append(' return std::operator<<(out, s);\n')
1290 header.append('}\n')
1291 header.append('}\n')
1292 header.append('\n')
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001293 header.append("string dynamic_display(const void* pStruct, const string prefix);\n")
Tobin Ehlis99f88672015-01-10 12:42:41 -07001294 return "".join(header)
1295
Tobin Ehlis14ff0852014-12-17 17:44:50 -07001296 def _generateValidateHelperFunctions(self):
1297 sh_funcs = []
1298 # We do two passes, first pass just generates prototypes for all the functsions
Ian Elliottf6b168c2015-12-11 15:03:05 -07001299 exclude_struct_list = ['VkAndroidSurfaceCreateInfoKHR',
1300 'VkMirSurfaceCreateInfoKHR',
1301 'VkWaylandSurfaceCreateInfoKHR',
1302 'VkXlibSurfaceCreateInfoKHR']
1303 if sys.platform == 'win32':
1304 exclude_struct_list.append('VkXcbSurfaceCreateInfoKHR')
1305 else:
1306 exclude_struct_list.append('VkWin32SurfaceCreateInfoKHR')
Peter Lohrmann49ea1842015-03-30 16:29:56 -07001307 for s in sorted(self.struct_dict):
Ian Elliottf6b168c2015-12-11 15:03:05 -07001308 if (typedef_fwd_dict[s] not in exclude_struct_list):
Michael Lentine33d96a32016-01-05 11:10:15 -06001309 if (re.match(r'.*Xcb.*', typedef_fwd_dict[s])):
1310 sh_funcs.append("#ifdef VK_USE_PLATFORM_XCB_KHR")
Cody Northrop0a179fe2016-02-24 12:28:41 -07001311 if (re.match(r'.*Win32.*', typedef_fwd_dict[s])):
1312 sh_funcs.append("#ifdef VK_USE_PLATFORM_WIN32_KHR")
Ian Elliottf6b168c2015-12-11 15:03:05 -07001313 sh_funcs.append('uint32_t %s(const %s* pStruct);' % (self._get_vh_func_name(s), typedef_fwd_dict[s]))
Cody Northrop0a179fe2016-02-24 12:28:41 -07001314 if (re.match(r'.*Win32.*', typedef_fwd_dict[s])):
1315 sh_funcs.append("#endif //VK_USE_PLATFORM_WIN32_KHR")
Michael Lentine33d96a32016-01-05 11:10:15 -06001316 if (re.match(r'.*Xcb.*', typedef_fwd_dict[s])):
1317 sh_funcs.append("#endif //VK_USE_PLATFORM_XCB_KHR")
Tobin Ehlis14ff0852014-12-17 17:44:50 -07001318 sh_funcs.append('\n')
Peter Lohrmann49ea1842015-03-30 16:29:56 -07001319 for s in sorted(self.struct_dict):
Ian Elliottf6b168c2015-12-11 15:03:05 -07001320 if (typedef_fwd_dict[s] in exclude_struct_list):
1321 continue
Michael Lentine33d96a32016-01-05 11:10:15 -06001322 if (re.match(r'.*Xcb.*', typedef_fwd_dict[s])):
1323 sh_funcs.append("#ifdef VK_USE_PLATFORM_XCB_KHR")
Cody Northrop0a179fe2016-02-24 12:28:41 -07001324 if (re.match(r'.*Win32.*', typedef_fwd_dict[s])):
1325 sh_funcs.append("#ifdef VK_USE_PLATFORM_WIN32_KHR")
Tobin Ehlis14ff0852014-12-17 17:44:50 -07001326 sh_funcs.append('uint32_t %s(const %s* pStruct)\n{' % (self._get_vh_func_name(s), typedef_fwd_dict[s]))
1327 for m in sorted(self.struct_dict[s]):
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001328 # TODO : Need to handle arrays of enums like in VkRenderPassCreateInfo struct
Tobin Ehlis7e65d752015-01-15 17:51:52 -07001329 if is_type(self.struct_dict[s][m]['type'], 'enum') and not self.struct_dict[s][m]['ptr']:
Tobin Ehlis14ff0852014-12-17 17:44:50 -07001330 sh_funcs.append(' if (!validate_%s(pStruct->%s))\n return 0;' % (self.struct_dict[s][m]['type'], self.struct_dict[s][m]['name']))
Jon Ashburn27e02a62015-01-12 15:46:51 -07001331 # TODO : Need a little refinement to this code to make sure type of struct matches expected input (ptr, const...)
Tobin Ehlisce12ad62014-12-18 09:29:58 -07001332 if is_type(self.struct_dict[s][m]['type'], 'struct'):
1333 if (self.struct_dict[s][m]['ptr']):
Tobin Ehlisf4a3ac52015-02-24 15:39:04 -07001334 sh_funcs.append(' if (pStruct->%s && !%s((const %s*)pStruct->%s))\n return 0;' % (self.struct_dict[s][m]['name'], self._get_vh_func_name(self.struct_dict[s][m]['type']), self.struct_dict[s][m]['type'], self.struct_dict[s][m]['name']))
Tobin Ehlisce12ad62014-12-18 09:29:58 -07001335 else:
1336 sh_funcs.append(' if (!%s((const %s*)&pStruct->%s))\n return 0;' % (self._get_vh_func_name(self.struct_dict[s][m]['type']), self.struct_dict[s][m]['type'], self.struct_dict[s][m]['name']))
Tobin Ehlis14ff0852014-12-17 17:44:50 -07001337 sh_funcs.append(" return 1;\n}")
Cody Northrop0a179fe2016-02-24 12:28:41 -07001338 if (re.match(r'.*Win32.*', typedef_fwd_dict[s])):
1339 sh_funcs.append("#endif //VK_USE_PLATFORM_WIN32_KHR")
Michael Lentine33d96a32016-01-05 11:10:15 -06001340 if (re.match(r'.*Xcb.*', typedef_fwd_dict[s])):
1341 sh_funcs.append("#endif //VK_USE_PLATFORM_XCB_KHR")
Tobin Ehlis14ff0852014-12-17 17:44:50 -07001342
1343 return "\n".join(sh_funcs)
1344
1345 def _generateValidateHelperHeader(self):
1346 header = []
1347 header.append("//#includes, #defines, globals and such...\n")
1348 for f in self.include_headers:
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001349 if 'vk_enum_validate_helper' not in f:
Tobin Ehlis14ff0852014-12-17 17:44:50 -07001350 header.append("#include <%s>\n" % f)
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001351 header.append('#include "vk_enum_validate_helper.h"\n\n// Function Prototypes\n')
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001352 #header.append("char* dynamic_display(const void* pStruct, const char* prefix);\n")
Tobin Ehlis14ff0852014-12-17 17:44:50 -07001353 return "".join(header)
1354
Tobin Ehlis60354772015-03-12 14:50:40 -06001355 def _generateSizeHelperFunctions(self):
1356 sh_funcs = []
Peter Lohrmann4a8d8912015-04-03 11:43:06 -07001357 # just generates prototypes for all the functions
Ian Elliottf6b168c2015-12-11 15:03:05 -07001358 exclude_struct_list = ['VkAndroidSurfaceCreateInfoKHR',
1359 'VkMirSurfaceCreateInfoKHR',
1360 'VkWaylandSurfaceCreateInfoKHR',
1361 'VkXlibSurfaceCreateInfoKHR']
1362 if sys.platform == 'win32':
1363 exclude_struct_list.append('VkXcbSurfaceCreateInfoKHR')
1364 else:
1365 exclude_struct_list.append('VkWin32SurfaceCreateInfoKHR')
Peter Lohrmann49ea1842015-03-30 16:29:56 -07001366 for s in sorted(self.struct_dict):
Ian Elliottf6b168c2015-12-11 15:03:05 -07001367 if (typedef_fwd_dict[s] not in exclude_struct_list):
Michael Lentine33d96a32016-01-05 11:10:15 -06001368 if (re.match(r'.*Xcb.*', typedef_fwd_dict[s])):
1369 sh_funcs.append("#ifdef VK_USE_PLATFORM_XCB_KHR")
Cody Northrop0a179fe2016-02-24 12:28:41 -07001370 if (re.match(r'.*Win32.*', typedef_fwd_dict[s])):
1371 sh_funcs.append("#ifdef VK_USE_PLATFORM_WIN32_KHR")
Ian Elliottf6b168c2015-12-11 15:03:05 -07001372 sh_funcs.append('size_t %s(const %s* pStruct);' % (self._get_size_helper_func_name(s), typedef_fwd_dict[s]))
Cody Northrop0a179fe2016-02-24 12:28:41 -07001373 if (re.match(r'.*Win32.*', typedef_fwd_dict[s])):
1374 sh_funcs.append("#endif //VK_USE_PLATFORM_WIN32_KHR")
Michael Lentine33d96a32016-01-05 11:10:15 -06001375 if (re.match(r'.*Xcb.*', typedef_fwd_dict[s])):
1376 sh_funcs.append("#endif //VK_USE_PLATFORM_XCB_KHR")
Peter Lohrmann4a8d8912015-04-03 11:43:06 -07001377 return "\n".join(sh_funcs)
1378
1379
1380 def _generateSizeHelperFunctionsC(self):
1381 sh_funcs = []
1382 # generate function definitions
Jon Ashburn81b1d9c2015-12-29 14:21:25 -07001383 exclude_struct_list = ['VkAndroidSurfaceCreateInfoKHR',
1384 'VkMirSurfaceCreateInfoKHR',
1385 'VkWaylandSurfaceCreateInfoKHR',
1386 'VkXlibSurfaceCreateInfoKHR']
1387 if sys.platform == 'win32':
1388 exclude_struct_list.append('VkXcbSurfaceCreateInfoKHR')
1389 else:
1390 exclude_struct_list.append('VkWin32SurfaceCreateInfoKHR')
Peter Lohrmann49ea1842015-03-30 16:29:56 -07001391 for s in sorted(self.struct_dict):
Jon Ashburn81b1d9c2015-12-29 14:21:25 -07001392 if (typedef_fwd_dict[s] in exclude_struct_list):
1393 continue
Tobin Ehlis6dd0fc32016-02-12 14:37:09 -07001394 skip_list = [] # Used when struct elements need to be skipped because size already accounted for
Tobin Ehlis60354772015-03-12 14:50:40 -06001395 sh_funcs.append('size_t %s(const %s* pStruct)\n{' % (self._get_size_helper_func_name(s), typedef_fwd_dict[s]))
1396 indent = ' '
1397 sh_funcs.append('%ssize_t structSize = 0;' % (indent))
1398 sh_funcs.append('%sif (pStruct) {' % (indent))
1399 indent = ' '
1400 sh_funcs.append('%sstructSize = sizeof(%s);' % (indent, typedef_fwd_dict[s]))
1401 i_decl = False
1402 for m in sorted(self.struct_dict[s]):
1403 if m in skip_list:
1404 continue
1405 if self.struct_dict[s][m]['dyn_array']:
1406 if self.struct_dict[s][m]['full_type'].count('*') > 1:
1407 if not is_type(self.struct_dict[s][m]['type'], 'struct') and not 'char' in self.struct_dict[s][m]['type'].lower():
Chia-I Wu3432a0c2015-10-27 18:04:07 +08001408 if 'ppMemoryBarriers' == self.struct_dict[s][m]['name']:
Tobin Ehlis04112922015-03-16 10:44:40 -06001409 # TODO : For now be conservative and consider all memBarrier ptrs as largest possible struct
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001410 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 Ehlis04112922015-03-16 10:44:40 -06001411 else:
1412 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 Ehlis60354772015-03-12 14:50:40 -06001413 else: # This is an array of char* or array of struct ptrs
1414 if not i_decl:
1415 sh_funcs.append('%suint32_t i = 0;' % (indent))
1416 i_decl = True
1417 sh_funcs.append('%sfor (i = 0; i < pStruct->%s; i++) {' % (indent, self.struct_dict[s][m]['array_size']))
1418 indent = ' '
1419 if is_type(self.struct_dict[s][m]['type'], 'struct'):
1420 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']))
1421 else:
Tobin Ehlisbd6a9162015-03-13 07:18:05 -06001422 sh_funcs.append('%sstructSize += (sizeof(char*) + (sizeof(char) * (1 + strlen(pStruct->%s[i]))));' % (indent, self.struct_dict[s][m]['name']))
Tobin Ehlis60354772015-03-12 14:50:40 -06001423 indent = ' '
1424 sh_funcs.append('%s}' % (indent))
1425 else:
1426 if is_type(self.struct_dict[s][m]['type'], 'struct'):
1427 if not i_decl:
1428 sh_funcs.append('%suint32_t i = 0;' % (indent))
1429 i_decl = True
1430 sh_funcs.append('%sfor (i = 0; i < pStruct->%s; i++) {' % (indent, self.struct_dict[s][m]['array_size']))
1431 indent = ' '
1432 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']))
1433 indent = ' '
1434 sh_funcs.append('%s}' % (indent))
1435 else:
1436 sh_funcs.append('%sstructSize += pStruct->%s*sizeof(%s);' % (indent, self.struct_dict[s][m]['array_size'], self.struct_dict[s][m]['type']))
1437 elif self.struct_dict[s][m]['ptr'] and 'pNext' != self.struct_dict[s][m]['name']:
1438 if 'char' in self.struct_dict[s][m]['type'].lower():
Karl Schultzc3f09632016-01-27 16:03:49 -07001439 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 Ehlis60354772015-03-12 14:50:40 -06001440 elif is_type(self.struct_dict[s][m]['type'], 'struct'):
1441 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 Ehlis04112922015-03-16 10:44:40 -06001442 elif 'void' not in self.struct_dict[s][m]['type'].lower():
Jon Ashburn81b1d9c2015-12-29 14:21:25 -07001443 if (self.struct_dict[s][m]['type'] != 'xcb_connection_t'):
1444 sh_funcs.append('%sstructSize += sizeof(%s);' % (indent, self.struct_dict[s][m]['type']))
Tobin Ehlis60354772015-03-12 14:50:40 -06001445 elif 'size_t' == self.struct_dict[s][m]['type'].lower():
1446 sh_funcs.append('%sstructSize += pStruct->%s;' % (indent, self.struct_dict[s][m]['name']))
1447 skip_list.append(m+1)
1448 indent = ' '
1449 sh_funcs.append('%s}' % (indent))
1450 sh_funcs.append("%sreturn structSize;\n}" % (indent))
Tobin Ehlis04112922015-03-16 10:44:40 -06001451 # Now generate generic functions to loop over entire struct chain (or just handle single generic structs)
Ian Elliottba042332015-11-18 14:57:08 -07001452 if '_debug_' not in self.header_filename:
Jon Ashburnea65e492015-08-06 17:27:49 -06001453 for follow_chain in [True, False]:
1454 sh_funcs.append('%s' % self.lineinfo.get())
1455 if follow_chain:
1456 sh_funcs.append('size_t get_struct_chain_size(const void* pStruct)\n{')
1457 else:
1458 sh_funcs.append('size_t get_dynamic_struct_size(const void* pStruct)\n{')
1459 indent = ' '
1460 sh_funcs.append('%s// Just use VkApplicationInfo as struct until actual type is resolved' % (indent))
1461 sh_funcs.append('%sVkApplicationInfo* pNext = (VkApplicationInfo*)pStruct;' % (indent))
1462 sh_funcs.append('%ssize_t structSize = 0;' % (indent))
1463 if follow_chain:
1464 sh_funcs.append('%swhile (pNext) {' % (indent))
1465 indent = ' '
1466 sh_funcs.append('%sswitch (pNext->sType) {' % (indent))
1467 indent += ' '
1468 for e in enum_type_dict:
1469 if 'StructureType' in e:
1470 for v in sorted(enum_type_dict[e]):
1471 struct_name = get_struct_name_from_struct_type(v)
Chia-I Wu736324e2015-10-26 20:42:12 +08001472 if struct_name not in self.struct_dict:
1473 continue
1474
Jon Ashburnea65e492015-08-06 17:27:49 -06001475 sh_funcs.append('%scase %s:' % (indent, v))
1476 sh_funcs.append('%s{' % (indent))
1477 indent += ' '
1478 sh_funcs.append('%sstructSize += %s((%s*)pNext);' % (indent, self._get_size_helper_func_name(struct_name), struct_name))
1479 sh_funcs.append('%sbreak;' % (indent))
1480 indent = indent[:-4]
1481 sh_funcs.append('%s}' % (indent))
1482 sh_funcs.append('%sdefault:' % (indent))
Tobin Ehlis04112922015-03-16 10:44:40 -06001483 indent += ' '
Jon Ashburnea65e492015-08-06 17:27:49 -06001484 sh_funcs.append('%sassert(0);' % (indent))
1485 sh_funcs.append('%sstructSize += 0;' % (indent))
Tobin Ehlis04112922015-03-16 10:44:40 -06001486 indent = indent[:-4]
Tobin Ehlis04112922015-03-16 10:44:40 -06001487 indent = indent[:-4]
1488 sh_funcs.append('%s}' % (indent))
Jon Ashburnea65e492015-08-06 17:27:49 -06001489 if follow_chain:
1490 sh_funcs.append('%spNext = (VkApplicationInfo*)pNext->pNext;' % (indent))
1491 indent = indent[:-4]
1492 sh_funcs.append('%s}' % (indent))
1493 sh_funcs.append('%sreturn structSize;\n}' % indent)
Tobin Ehlis60354772015-03-12 14:50:40 -06001494 return "\n".join(sh_funcs)
1495
1496 def _generateSizeHelperHeader(self):
1497 header = []
1498 header.append("//#includes, #defines, globals and such...\n")
1499 for f in self.include_headers:
1500 header.append("#include <%s>\n" % f)
1501 header.append('\n// Function Prototypes\n')
1502 header.append("size_t get_struct_chain_size(const void* pStruct);\n")
Tobin Ehlis04112922015-03-16 10:44:40 -06001503 header.append("size_t get_dynamic_struct_size(const void* pStruct);\n")
Tobin Ehlis60354772015-03-12 14:50:40 -06001504 return "".join(header)
1505
Peter Lohrmann4a8d8912015-04-03 11:43:06 -07001506 def _generateSizeHelperHeaderC(self):
1507 header = []
Courtney Goeltzenleuchterdba79982015-04-14 16:30:53 -06001508 header.append('#include "vk_struct_size_helper.h"')
Peter Lohrmann4a8d8912015-04-03 11:43:06 -07001509 header.append('#include <string.h>')
1510 header.append('#include <assert.h>')
1511 header.append('\n// Function definitions\n')
1512 return "\n".join(header)
1513
1514
Tobin Ehlis6442dca2014-10-22 15:13:53 -06001515 def _generateHeader(self):
1516 header = []
1517 header.append("//#includes, #defines, globals and such...\n")
1518 for f in self.include_headers:
1519 header.append("#include <%s>\n" % f)
1520 return "".join(header)
Mark Lobodzinski28b8f022015-08-27 15:30:29 -06001521
Tobin Ehlis6442dca2014-10-22 15:13:53 -06001522 # Declarations
1523 def _generateConstructorDeclarations(self, s):
1524 constructors = []
1525 constructors.append(" %s();\n" % self.get_class_name(s))
1526 constructors.append(" %s(%s* pInStruct);\n" % (self.get_class_name(s), typedef_fwd_dict[s]))
1527 constructors.append(" %s(const %s* pInStruct);\n" % (self.get_class_name(s), typedef_fwd_dict[s]))
1528 return "".join(constructors)
Mark Lobodzinski28b8f022015-08-27 15:30:29 -06001529
Tobin Ehlis6442dca2014-10-22 15:13:53 -06001530 def _generateDestructorDeclarations(self, s):
1531 return " virtual ~%s();\n" % self.get_class_name(s)
Mark Lobodzinski28b8f022015-08-27 15:30:29 -06001532
Tobin Ehlis6442dca2014-10-22 15:13:53 -06001533 def _generateDisplayDeclarations(self, s):
1534 return " void display_txt();\n void display_single_txt();\n void display_full_txt();\n"
Mark Lobodzinski28b8f022015-08-27 15:30:29 -06001535
Tobin Ehlis6442dca2014-10-22 15:13:53 -06001536 def _generateGetSetDeclarations(self, s):
1537 get_set = []
1538 get_set.append(" void set_indent(uint32_t indent) { m_indent = indent; }\n")
1539 for member in sorted(self.struct_dict[s]):
1540 # TODO : Skipping array set/get funcs for now
1541 if self.struct_dict[s][member]['array']:
1542 continue
1543 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']))
1544 if not self.struct_dict[s][member]['const']:
1545 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']))
1546 return "".join(get_set)
Mark Lobodzinski28b8f022015-08-27 15:30:29 -06001547
Tobin Ehlis6442dca2014-10-22 15:13:53 -06001548 def _generatePrivateMembers(self, s):
1549 priv = []
1550 priv.append("\nprivate:\n")
1551 priv.append(" %s m_struct;\n" % typedef_fwd_dict[s])
1552 priv.append(" const %s* m_origStructAddr;\n" % typedef_fwd_dict[s])
1553 priv.append(" uint32_t m_indent;\n")
1554 priv.append(" const char m_dummy_prefix;\n")
1555 priv.append(" void display_struct_members();\n")
1556 return "".join(priv)
Mark Lobodzinski28b8f022015-08-27 15:30:29 -06001557
Tobin Ehlis6442dca2014-10-22 15:13:53 -06001558 def _generateClassDeclaration(self):
1559 class_decl = []
Peter Lohrmann49ea1842015-03-30 16:29:56 -07001560 for s in sorted(self.struct_dict):
Tobin Ehlis6442dca2014-10-22 15:13:53 -06001561 class_decl.append("\n//class declaration")
1562 class_decl.append("class %s\n{\npublic:" % self.get_class_name(s))
1563 class_decl.append(self._generateConstructorDeclarations(s))
1564 class_decl.append(self._generateDestructorDeclarations(s))
1565 class_decl.append(self._generateDisplayDeclarations(s))
1566 class_decl.append(self._generateGetSetDeclarations(s))
1567 class_decl.append(self._generatePrivateMembers(s))
1568 class_decl.append("};\n")
1569 return "\n".join(class_decl)
Mark Lobodzinski28b8f022015-08-27 15:30:29 -06001570
Tobin Ehlis6442dca2014-10-22 15:13:53 -06001571 def _generateFooter(self):
1572 return "\n//any footer info for class\n"
1573
Tobin Ehlis37649692016-02-10 15:32:46 -07001574 def _getSafeStructName(self, struct):
1575 return "safe_%s" % (struct)
1576
1577 # If struct has sType or ptr members, generate safe type
1578 def _hasSafeStruct(self, s):
1579 exceptions = ['VkPhysicalDeviceFeatures', 'VkPipelineColorBlendStateCreateInfo']
1580 if s in exceptions:
1581 return False
1582 if 'sType' == self.struct_dict[s][0]['name']:
1583 return True
1584 for m in self.struct_dict[s]:
1585 if self.struct_dict[s][m]['ptr']:
1586 return True
1587 return False
1588
1589 def _generateSafeStructHeader(self):
1590 header = []
1591 header.append("//#includes, #defines, globals and such...\n")
1592 header.append('#include "vulkan/vulkan.h"')
1593 return "".join(header)
1594
1595 # If given ty is in obj list, or is a struct that contains anything in obj list, return True
1596 def _typeHasObject(self, ty, obj):
1597 if ty in obj:
1598 return True
1599 if is_type(ty, 'struct'):
1600 for m in self.struct_dict[ty]:
1601 if self.struct_dict[ty][m]['type'] in obj:
1602 return True
1603 return False
1604
1605 def _generateSafeStructDecls(self):
1606 ss_decls = []
1607 for s in struct_order_list:
1608 if not self._hasSafeStruct(s):
1609 continue
1610 if s in ifdef_dict:
1611 ss_decls.append('#ifdef %s' % ifdef_dict[s])
1612 ss_name = self._getSafeStructName(s)
1613 ss_decls.append("\nstruct %s {" % (ss_name))
1614 for m in sorted(self.struct_dict[s]):
1615 m_type = self.struct_dict[s][m]['type']
1616 if is_type(m_type, 'struct') and self._hasSafeStruct(m_type):
1617 m_type = self._getSafeStructName(m_type)
1618 if self.struct_dict[s][m]['array_size'] != 0 and not self.struct_dict[s][m]['dyn_array']:
1619 ss_decls.append(" %s %s[%s];" % (m_type, self.struct_dict[s][m]['name'], self.struct_dict[s][m]['array_size']))
1620 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
1621 ss_decls.append(" %s %s;" % (self.struct_dict[s][m]['full_type'], self.struct_dict[s][m]['name']))
1622 elif self.struct_dict[s][m]['array']:
1623 ss_decls.append(" %s* %s;" % (m_type, self.struct_dict[s][m]['name']))
1624 elif self.struct_dict[s][m]['ptr']:
1625 ss_decls.append(" %s* %s;" % (m_type, self.struct_dict[s][m]['name']))
1626 else:
1627 ss_decls.append(" %s %s;" % (m_type, self.struct_dict[s][m]['name']))
1628 ss_decls.append(" %s(const %s* pInStruct);" % (ss_name, s))
1629 ss_decls.append(" %s();" % (ss_name))
1630 ss_decls.append(" ~%s();" % (ss_name))
1631 ss_decls.append(" void initialize(const %s* pInStruct);" % (s))
1632 ss_decls.append("};")
1633 if s in ifdef_dict:
1634 ss_decls.append('#endif')
1635 return "\n".join(ss_decls)
1636
1637 def _generateSafeStructSourceHeader(self):
1638 header = []
1639 header.append("//#includes, #defines, globals and such...\n")
1640 header.append('#include "vk_safe_struct.h"')
1641 return "".join(header)
1642
1643 def _generateSafeStructSource(self):
1644 ss_src = []
1645 for s in struct_order_list:
1646 if not self._hasSafeStruct(s):
1647 continue
1648 if s in ifdef_dict:
1649 ss_src.append('#ifdef %s' % ifdef_dict[s])
1650 ss_name = self._getSafeStructName(s)
1651 init_list = '' # list of members in struct constructor initializer
1652 init_func_txt = '' # Txt for initialize() function that takes struct ptr and inits members
1653 construct_txt = ''
1654 destruct_txt = ''
Tobin Ehlis6dd0fc32016-02-12 14:37:09 -07001655 # VkWriteDescriptorSet is special case because pointers may be non-null but ignored
1656 # TODO : This is ugly, figure out better way to do this
1657 custom_construct_txt = {'VkWriteDescriptorSet' :
1658 ' switch (descriptorType) {\n'
1659 ' case VK_DESCRIPTOR_TYPE_SAMPLER:\n'
1660 ' case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:\n'
1661 ' case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:\n'
1662 ' case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:\n'
1663 ' case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:\n'
1664 ' if (descriptorCount && pInStruct->pImageInfo) {\n'
1665 ' pImageInfo = new VkDescriptorImageInfo[descriptorCount];\n'
1666 ' for (uint32_t i=0; i<descriptorCount; ++i) {\n'
1667 ' pImageInfo[i] = pInStruct->pImageInfo[i];\n'
1668 ' }\n'
1669 ' }\n'
1670 ' break;\n'
1671 ' case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:\n'
1672 ' case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:\n'
1673 ' case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:\n'
1674 ' case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:\n'
1675 ' if (descriptorCount && pInStruct->pBufferInfo) {\n'
1676 ' pBufferInfo = new VkDescriptorBufferInfo[descriptorCount];\n'
1677 ' for (uint32_t i=0; i<descriptorCount; ++i) {\n'
1678 ' pBufferInfo[i] = pInStruct->pBufferInfo[i];\n'
1679 ' }\n'
1680 ' }\n'
1681 ' break;\n'
1682 ' case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:\n'
1683 ' case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:\n'
1684 ' if (descriptorCount && pInStruct->pTexelBufferView) {\n'
1685 ' pTexelBufferView = new VkBufferView[descriptorCount];\n'
1686 ' for (uint32_t i=0; i<descriptorCount; ++i) {\n'
1687 ' pTexelBufferView[i] = pInStruct->pTexelBufferView[i];\n'
1688 ' }\n'
1689 ' }\n'
1690 ' break;\n'
Tobin Ehlisd183b2d2016-02-16 12:08:54 -07001691 ' default:\n'
1692 ' break;\n'
Tobin Ehlis6dd0fc32016-02-12 14:37:09 -07001693 ' }\n'}
Tobin Ehlis37649692016-02-10 15:32:46 -07001694 for m in self.struct_dict[s]:
1695 m_name = self.struct_dict[s][m]['name']
1696 m_type = self.struct_dict[s][m]['type']
1697 if is_type(m_type, 'struct') and self._hasSafeStruct(m_type):
1698 m_type = self._getSafeStructName(m_type)
1699 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]:
1700 init_list += '\n\t%s(pInStruct->%s),' % (m_name, m_name)
Tobin Ehlis6dd0fc32016-02-12 14:37:09 -07001701 init_func_txt += ' %s = pInStruct->%s;\n' % (m_name, m_name)
Tobin Ehlis37649692016-02-10 15:32:46 -07001702 elif self.struct_dict[s][m]['array']:
Tobin Ehlis6dd0fc32016-02-12 14:37:09 -07001703 # Init array ptr to NULL
1704 init_list += '\n\t%s(NULL),' % (m_name)
1705 init_func_txt += ' %s = NULL;\n' % (m_name)
Tobin Ehlis37649692016-02-10 15:32:46 -07001706 array_element = 'pInStruct->%s[i]' % (m_name)
1707 if is_type(self.struct_dict[s][m]['type'], 'struct') and self._hasSafeStruct(self.struct_dict[s][m]['type']):
1708 array_element = '%s(&pInStruct->%s[i])' % (self._getSafeStructName(self.struct_dict[s][m]['type']), m_name)
1709 construct_txt += ' if (%s && pInStruct->%s) {\n' % (self.struct_dict[s][m]['array_size'], m_name)
1710 construct_txt += ' %s = new %s[%s];\n' % (m_name, m_type, self.struct_dict[s][m]['array_size'])
1711 destruct_txt += ' if (%s)\n' % (m_name)
1712 destruct_txt += ' delete[] %s;\n' % (m_name)
1713 construct_txt += ' for (uint32_t i=0; i<%s; ++i) {\n' % (self.struct_dict[s][m]['array_size'])
1714 if 'safe_' in m_type:
1715 construct_txt += ' %s[i].initialize(&pInStruct->%s[i]);\n' % (m_name, m_name)
1716 else:
1717 construct_txt += ' %s[i] = %s;\n' % (m_name, array_element)
1718 construct_txt += ' }\n'
Tobin Ehlis37649692016-02-10 15:32:46 -07001719 construct_txt += ' }\n'
1720 elif self.struct_dict[s][m]['ptr']:
1721 construct_txt += ' if (pInStruct->%s)\n' % (m_name)
1722 construct_txt += ' %s = new %s(pInStruct->%s);\n' % (m_name, m_type, m_name)
1723 construct_txt += ' else\n'
1724 construct_txt += ' %s = NULL;\n' % (m_name)
1725 destruct_txt += ' if (%s)\n' % (m_name)
1726 destruct_txt += ' delete %s;\n' % (m_name)
1727 elif 'safe_' in m_type: # inline struct, need to pass in reference for constructor
1728 init_list += '\n\t%s(&pInStruct->%s),' % (m_name, m_name)
1729 init_func_txt += ' %s.initialize(&pInStruct->%s);\n' % (m_name, m_name)
1730 else:
1731 init_list += '\n\t%s(pInStruct->%s),' % (m_name, m_name)
Tobin Ehlis6dd0fc32016-02-12 14:37:09 -07001732 init_func_txt += ' %s = pInStruct->%s;\n' % (m_name, m_name)
Tobin Ehlis37649692016-02-10 15:32:46 -07001733 if '' != init_list:
1734 init_list = init_list[:-1] # hack off final comma
Tobin Ehlis6dd0fc32016-02-12 14:37:09 -07001735 if s in custom_construct_txt:
1736 construct_txt = custom_construct_txt[s]
Tobin Ehlis37649692016-02-10 15:32:46 -07001737 ss_src.append("\n%s::%s(const %s* pInStruct) : %s\n{\n%s}" % (ss_name, ss_name, s, init_list, construct_txt))
1738 ss_src.append("\n%s::%s() {}" % (ss_name, ss_name))
1739 ss_src.append("\n%s::~%s()\n{\n%s}" % (ss_name, ss_name, destruct_txt))
1740 ss_src.append("\nvoid %s::initialize(const %s* pInStruct)\n{\n%s%s}" % (ss_name, s, init_func_txt, construct_txt))
1741 if s in ifdef_dict:
1742 ss_src.append('#endif')
1743 return "\n".join(ss_src)
1744
Tobin Ehlis6442dca2014-10-22 15:13:53 -06001745class EnumCodeGen:
Tobin Ehlis7c47fc42014-12-17 07:20:23 -07001746 def __init__(self, enum_type_dict=None, enum_val_dict=None, typedef_fwd_dict=None, in_file=None, out_sh_file=None, out_vh_file=None):
Tobin Ehlis6442dca2014-10-22 15:13:53 -06001747 self.et_dict = enum_type_dict
1748 self.ev_dict = enum_val_dict
1749 self.tf_dict = typedef_fwd_dict
1750 self.in_file = in_file
Tobin Ehlis7c47fc42014-12-17 07:20:23 -07001751 self.out_sh_file = out_sh_file
1752 self.eshfg = CommonFileGen(self.out_sh_file)
1753 self.out_vh_file = out_vh_file
1754 self.evhfg = CommonFileGen(self.out_vh_file)
Mark Lobodzinski28b8f022015-08-27 15:30:29 -06001755
Tobin Ehlis6442dca2014-10-22 15:13:53 -06001756 def generateStringHelper(self):
Tobin Ehlis7c47fc42014-12-17 07:20:23 -07001757 self.eshfg.setHeader(self._generateSHHeader())
1758 self.eshfg.setBody(self._generateSHBody())
1759 self.eshfg.generate()
1760
1761 def generateEnumValidate(self):
1762 self.evhfg.setHeader(self._generateSHHeader())
Tobin Ehlis14ff0852014-12-17 17:44:50 -07001763 self.evhfg.setBody(self._generateVHBody())
Tobin Ehlis7c47fc42014-12-17 07:20:23 -07001764 self.evhfg.generate()
1765
1766 def _generateVHBody(self):
1767 body = []
Peter Lohrmann49ea1842015-03-30 16:29:56 -07001768 for bet in sorted(self.et_dict):
Tobin Ehlis7c47fc42014-12-17 07:20:23 -07001769 fet = self.tf_dict[bet]
Tobin Ehlis9d8ba362015-04-15 17:04:03 -06001770 body.append("static inline uint32_t validate_%s(%s input_value)\n{" % (fet, fet))
1771 # TODO : This is not ideal, but allows for flag combinations. Need more rigorous validation of realistic flag combinations
Tobin Ehlisd3350652015-04-22 13:59:43 -06001772 if 'flagbits' in bet.lower():
Tobin Ehlis9d8ba362015-04-15 17:04:03 -06001773 body.append(' if (input_value > (%s))' % (' | '.join(self.et_dict[bet])))
1774 body.append(' return 0;')
1775 body.append(' return 1;')
1776 body.append('}\n\n')
1777 else:
1778 body.append(' switch ((%s)input_value)\n {' % (fet))
1779 for e in sorted(self.et_dict[bet]):
1780 if (self.ev_dict[e]['unique']):
1781 body.append(' case %s:' % (e))
1782 body.append(' return 1;\n default:\n return 0;\n }\n}\n\n')
Tobin Ehlis7c47fc42014-12-17 07:20:23 -07001783 return "\n".join(body)
1784
Tobin Ehlis6442dca2014-10-22 15:13:53 -06001785 def _generateSHBody(self):
1786 body = []
1787# with open(self.out_file, "a") as hf:
1788 # bet == base_enum_type, fet == final_enum_type
Peter Lohrmann49ea1842015-03-30 16:29:56 -07001789 for bet in sorted(self.et_dict):
Tobin Ehlis6442dca2014-10-22 15:13:53 -06001790 fet = self.tf_dict[bet]
Ian Elliottfeae4052015-02-18 12:38:04 -07001791 body.append("static inline const char* string_%s(%s input_value)\n{\n switch ((%s)input_value)\n {" % (fet, fet, fet))
Tobin Ehlis6442dca2014-10-22 15:13:53 -06001792 for e in sorted(self.et_dict[bet]):
1793 if (self.ev_dict[e]['unique']):
Tobin Ehlis10777032015-01-16 15:13:34 -07001794 body.append(' case %s:\n return "%s";' % (e, e))
1795 body.append(' default:\n return "Unhandled %s";\n }\n}\n\n' % (fet))
Tobin Ehlis6442dca2014-10-22 15:13:53 -06001796 return "\n".join(body)
Mark Lobodzinski28b8f022015-08-27 15:30:29 -06001797
Tobin Ehlis6442dca2014-10-22 15:13:53 -06001798 def _generateSHHeader(self):
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001799 header = []
1800 header.append('#pragma once\n')
Ian Elliottc779c7e2015-04-28 16:11:24 -06001801 header.append('#ifdef _WIN32\n')
1802 header.append('#pragma warning( disable : 4065 )\n')
1803 header.append('#endif\n')
David Pinedo9316d3b2015-11-06 12:54:48 -07001804 header.append('#include <vulkan/%s>\n\n\n' % self.in_file)
Ian Elliott2d4ab1e2015-01-13 17:52:38 -07001805 return "\n".join(header)
Mark Lobodzinski28b8f022015-08-27 15:30:29 -06001806
Tobin Ehlis6442dca2014-10-22 15:13:53 -06001807
1808class CMakeGen:
1809 def __init__(self, struct_wrapper=None, out_dir=None):
1810 self.sw = struct_wrapper
Tobin Ehlis83562882014-11-27 15:43:39 -07001811 self.include_headers = []
Tobin Ehlis6442dca2014-10-22 15:13:53 -06001812 self.add_lib_file_list = self.sw.get_file_list()
1813 self.out_dir = out_dir
1814 self.out_file = os.path.join(self.out_dir, "CMakeLists.txt")
1815 self.cmg = CommonFileGen(self.out_file)
Mark Lobodzinski28b8f022015-08-27 15:30:29 -06001816
Tobin Ehlis6442dca2014-10-22 15:13:53 -06001817 def generate(self):
1818 self.cmg.setBody(self._generateBody())
1819 self.cmg.generate()
Mark Lobodzinski28b8f022015-08-27 15:30:29 -06001820
Tobin Ehlis6442dca2014-10-22 15:13:53 -06001821 def _generateBody(self):
1822 body = []
1823 body.append("project(%s)" % os.path.basename(self.out_dir))
1824 body.append("cmake_minimum_required(VERSION 2.8)\n")
1825 body.append("add_library(${PROJECT_NAME} %s)\n" % " ".join(self.add_lib_file_list))
1826 body.append('set(COMPILE_FLAGS "-fpermissive")')
1827 body.append('set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${COMPILE_FLAGS}")\n')
1828 body.append("include_directories(${SRC_DIR}/thirdparty/${GEN_API}/inc/)\n")
1829 body.append("target_include_directories (%s PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})\n" % os.path.basename(self.out_dir))
1830 return "\n".join(body)
1831
1832class GraphVizGen:
Tobin Ehlis83562882014-11-27 15:43:39 -07001833 def __init__(self, struct_dict, prefix, out_dir):
1834 self.struct_dict = struct_dict
1835 self.api = prefix
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001836 if prefix == "vulkan":
1837 self.api_prefix = "vk"
1838 else:
1839 self.api_prefix = prefix
1840 self.out_file = os.path.join(out_dir, self.api_prefix+"_struct_graphviz_helper.h")
Tobin Ehlis6442dca2014-10-22 15:13:53 -06001841 self.gvg = CommonFileGen(self.out_file)
Tobin Ehlis83562882014-11-27 15:43:39 -07001842
Tobin Ehlis6442dca2014-10-22 15:13:53 -06001843 def generate(self):
Tobin Ehlis83562882014-11-27 15:43:39 -07001844 self.gvg.setCopyright("//This is the copyright\n")
Tobin Ehlis6442dca2014-10-22 15:13:53 -06001845 self.gvg.setHeader(self._generateHeader())
1846 self.gvg.setBody(self._generateBody())
Tobin Ehlis83562882014-11-27 15:43:39 -07001847 #self.gvg.setFooter('}')
Tobin Ehlis6442dca2014-10-22 15:13:53 -06001848 self.gvg.generate()
Tobin Ehlis83562882014-11-27 15:43:39 -07001849
1850 def set_include_headers(self, include_headers):
1851 self.include_headers = include_headers
1852
Tobin Ehlis6442dca2014-10-22 15:13:53 -06001853 def _generateHeader(self):
Tobin Ehlis83562882014-11-27 15:43:39 -07001854 header = []
1855 header.append("//#includes, #defines, globals and such...\n")
1856 for f in self.include_headers:
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001857 if 'vk_enum_string_helper' not in f:
Tobin Ehlis83562882014-11-27 15:43:39 -07001858 header.append("#include <%s>\n" % f)
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001859 #header.append('#include "vk_enum_string_helper.h"\n\n// Function Prototypes\n')
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001860 header.append("\nchar* dynamic_gv_display(const void* pStruct, const char* prefix);\n")
Tobin Ehlis83562882014-11-27 15:43:39 -07001861 return "".join(header)
1862
1863 def _get_gv_func_name(self, struct):
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06001864 return "%s_gv_print_%s" % (self.api_prefix, struct.lower().strip("_"))
Tobin Ehlis83562882014-11-27 15:43:39 -07001865
1866 # Return elements to create formatted string for given struct member
1867 def _get_struct_gv_print_formatted(self, struct_member, pre_var_name="", postfix = "\\n", struct_var_name="pStruct", struct_ptr=True, print_array=False, port_label=""):
1868 struct_op = "->"
1869 pre_var_name = '"%s "' % struct_member['full_type']
1870 if not struct_ptr:
1871 struct_op = "."
1872 member_name = struct_member['name']
1873 print_type = "p"
1874 cast_type = ""
1875 member_post = ""
1876 array_index = ""
1877 member_print_post = ""
Tobin Ehlisb0497852015-02-11 14:24:02 -07001878 print_delimiter = "%"
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001879 if struct_member['array'] and 'char' in struct_member['type'].lower(): # just print char array as string
1880 print_type = "p"
Tobin Ehlis83562882014-11-27 15:43:39 -07001881 print_array = False
1882 elif struct_member['array'] and not print_array:
1883 # Just print base address of array when not full print_array
1884 cast_type = "(void*)"
1885 elif is_type(struct_member['type'], 'enum'):
Jon Ashburnfe6113c2015-01-09 09:11:44 -07001886 if struct_member['ptr']:
1887 struct_var_name = "*" + struct_var_name
Tobin Ehlis83562882014-11-27 15:43:39 -07001888 cast_type = "string_%s" % struct_member['type']
1889 print_type = "s"
1890 elif is_type(struct_member['type'], 'struct'): # print struct address for now
1891 cast_type = "(void*)"
1892 if not struct_member['ptr']:
1893 cast_type = "(void*)&"
Courtney Goeltzenleuchtercd2a0992015-07-09 11:44:38 -06001894 elif 'bool' in struct_member['type'].lower():
Tobin Ehlis83562882014-11-27 15:43:39 -07001895 print_type = "s"
1896 member_post = ' ? "TRUE" : "FALSE"'
Tobin Ehlis7e65d752015-01-15 17:51:52 -07001897 elif 'float' in struct_member['type']:
Tobin Ehlis83562882014-11-27 15:43:39 -07001898 print_type = "f"
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001899 elif 'uint64' in struct_member['type'] or 'gpusize' in struct_member['type'].lower():
1900 print_type = '" PRId64 "'
Tobin Ehlis7e65d752015-01-15 17:51:52 -07001901 elif 'uint8' in struct_member['type']:
Tobin Ehlis83562882014-11-27 15:43:39 -07001902 print_type = "hu"
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001903 elif 'size' in struct_member['type'].lower():
Tobin Ehlisb0497852015-02-11 14:24:02 -07001904 print_type = '" PRINTF_SIZE_T_SPECIFIER "'
1905 print_delimiter = ""
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001906 elif True in [ui_str.lower() in struct_member['type'].lower() for ui_str in ['uint', 'flags', 'samplemask']]:
Tobin Ehlis83562882014-11-27 15:43:39 -07001907 print_type = "u"
Tobin Ehlis7e65d752015-01-15 17:51:52 -07001908 elif 'int' in struct_member['type']:
Tobin Ehlis83562882014-11-27 15:43:39 -07001909 print_type = "i"
1910 elif struct_member['ptr']:
1911 pass
1912 else:
1913 #print("Unhandled struct type: %s" % struct_member['type'])
1914 cast_type = "(void*)"
1915 if print_array and struct_member['array']:
1916 member_print_post = "[%u]"
1917 array_index = " i,"
1918 member_post = "[i]"
Tobin Ehlisb0497852015-02-11 14:24:02 -07001919 print_out = "<TR><TD>%%s%s%s</TD><TD%s>%s%s%s</TD></TR>" % (member_name, member_print_post, port_label, print_delimiter, print_type, postfix) # section of print that goes inside of quotes
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001920 print_arg = ", %s,%s %s(%s%s%s)%s\n" % (pre_var_name, array_index, cast_type, struct_var_name, struct_op, member_name, member_post) # section of print passed to portion in quotes
Tobin Ehlis83562882014-11-27 15:43:39 -07001921 return (print_out, print_arg)
1922
Tobin Ehlis6442dca2014-10-22 15:13:53 -06001923 def _generateBody(self):
Tobin Ehlis83562882014-11-27 15:43:39 -07001924 gv_funcs = []
1925 array_func_list = [] # structs for which we'll generate an array version of their print function
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001926 array_func_list.append('vkbufferviewattachinfo')
1927 array_func_list.append('vkimageviewattachinfo')
1928 array_func_list.append('vksamplerimageviewinfo')
1929 array_func_list.append('vkdescriptortypecount')
Tobin Ehlis83562882014-11-27 15:43:39 -07001930 # For first pass, generate prototype
Peter Lohrmann49ea1842015-03-30 16:29:56 -07001931 for s in sorted(self.struct_dict):
Tobin Ehlis83562882014-11-27 15:43:39 -07001932 gv_funcs.append('char* %s(const %s* pStruct, const char* myNodeName);\n' % (self._get_gv_func_name(s), typedef_fwd_dict[s]))
1933 if s.lower().strip("_") in array_func_list:
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06001934 if s.lower().strip("_") in ['vkbufferviewattachinfo', 'vkimageviewattachinfo']:
Tobin Ehlisb8013a22015-02-26 12:57:08 -07001935 gv_funcs.append('char* %s_array(uint32_t count, const %s* const* pStruct, const char* myNodeName);\n' % (self._get_gv_func_name(s), typedef_fwd_dict[s]))
1936 else:
1937 gv_funcs.append('char* %s_array(uint32_t count, const %s* pStruct, const char* myNodeName);\n' % (self._get_gv_func_name(s), typedef_fwd_dict[s]))
Tobin Ehlis83562882014-11-27 15:43:39 -07001938 gv_funcs.append('\n')
Peter Lohrmann49ea1842015-03-30 16:29:56 -07001939 for s in sorted(self.struct_dict):
Tobin Ehlis83562882014-11-27 15:43:39 -07001940 p_out = ""
1941 p_args = ""
1942 stp_list = [] # stp == "struct to print" a list of structs for this API call that should be printed as structs
1943 # the fields below are a super-hacky way for now to get port labels into GV output, TODO : Clean this up!
1944 pl_dict = {}
1945 struct_num = 0
Tobin Ehlisb8013a22015-02-26 12:57:08 -07001946 # This isn't great but this pre-pass flags structs w/ pNext and other struct ptrs
Tobin Ehlis83562882014-11-27 15:43:39 -07001947 for m in sorted(self.struct_dict[s]):
1948 if 'pNext' == self.struct_dict[s][m]['name'] or is_type(self.struct_dict[s][m]['type'], 'struct'):
1949 stp_list.append(self.struct_dict[s][m])
1950 if 'pNext' == self.struct_dict[s][m]['name']:
1951 pl_dict[m] = ' PORT=\\"pNext\\"'
1952 else:
1953 pl_dict[m] = ' PORT=\\"struct%i\\"' % struct_num
1954 struct_num += 1
1955 gv_funcs.append('char* %s(const %s* pStruct, const char* myNodeName)\n{\n char* str;\n' % (self._get_gv_func_name(s), typedef_fwd_dict[s]))
1956 num_stps = len(stp_list);
1957 total_strlen_str = ''
1958 if 0 != num_stps:
1959 gv_funcs.append(" char* tmpStr;\n")
1960 gv_funcs.append(" char nodeName[100];\n")
Tobin Ehlis83562882014-11-27 15:43:39 -07001961 gv_funcs.append(' char* stp_strs[%i];\n' % num_stps)
1962 for index in range(num_stps):
1963 if (stp_list[index]['ptr']):
1964 if 'pDescriptorInfo' == stp_list[index]['name']:
1965 gv_funcs.append(' if (pStruct->pDescriptorInfo && (0 != pStruct->descriptorCount)) {\n')
1966 else:
1967 gv_funcs.append(' if (pStruct->%s) {\n' % stp_list[index]['name'])
1968 if 'pNext' == stp_list[index]['name']:
1969 gv_funcs.append(' sprintf(nodeName, "pNext_%p", (void*)pStruct->pNext);\n')
Mark Lobodzinski17caf572015-01-29 08:55:56 -06001970 gv_funcs.append(' tmpStr = dynamic_gv_display((void*)pStruct->pNext, nodeName);\n')
Tobin Ehlis83562882014-11-27 15:43:39 -07001971 gv_funcs.append(' stp_strs[%i] = (char*)malloc(256+strlen(tmpStr)+strlen(nodeName)+strlen(myNodeName));\n' % index)
1972 gv_funcs.append(' sprintf(stp_strs[%i], "%%s\\n\\"%%s\\":pNext -> \\"%%s\\" [];\\n", tmpStr, myNodeName, nodeName);\n' % index)
1973 gv_funcs.append(' free(tmpStr);\n')
1974 else:
1975 gv_funcs.append(' sprintf(nodeName, "%s_%%p", (void*)pStruct->%s);\n' % (stp_list[index]['name'], stp_list[index]['name']))
Tobin Ehlis793ad302015-04-03 12:01:11 -06001976 if stp_list[index]['name'] in ['pTypeCount', 'pSamplerImageViews']:
Tobin Ehlisb8013a22015-02-26 12:57:08 -07001977 gv_funcs.append(' tmpStr = %s_array(pStruct->count, pStruct->%s, nodeName);\n' % (self._get_gv_func_name(stp_list[index]['type']), stp_list[index]['name']))
Tobin Ehlis83562882014-11-27 15:43:39 -07001978 else:
1979 gv_funcs.append(' tmpStr = %s(pStruct->%s, nodeName);\n' % (self._get_gv_func_name(stp_list[index]['type']), stp_list[index]['name']))
1980 gv_funcs.append(' stp_strs[%i] = (char*)malloc(256+strlen(tmpStr)+strlen(nodeName)+strlen(myNodeName));\n' % (index))
1981 gv_funcs.append(' sprintf(stp_strs[%i], "%%s\\n\\"%%s\\":struct%i -> \\"%%s\\" [];\\n", tmpStr, myNodeName, nodeName);\n' % (index, index))
1982 gv_funcs.append(' }\n')
Chia-I Wu3bf80a62014-12-16 00:36:58 +08001983 gv_funcs.append(" else\n stp_strs[%i] = \"\";\n" % (index))
Tobin Ehlis83562882014-11-27 15:43:39 -07001984 elif stp_list[index]['array']: # TODO : For now just printing first element of array
1985 gv_funcs.append(' sprintf(nodeName, "%s_%%p", (void*)&pStruct->%s[0]);\n' % (stp_list[index]['name'], stp_list[index]['name']))
1986 gv_funcs.append(' tmpStr = %s(&pStruct->%s[0], nodeName);\n' % (self._get_gv_func_name(stp_list[index]['type']), stp_list[index]['name']))
1987 gv_funcs.append(' stp_strs[%i] = (char*)malloc(256+strlen(tmpStr)+strlen(nodeName)+strlen(myNodeName));\n' % (index))
1988 gv_funcs.append(' sprintf(stp_strs[%i], "%%s\\n\\"%%s\\":struct%i -> \\"%%s\\" [];\\n", tmpStr, myNodeName, nodeName);\n' % (index, index))
1989 else:
1990 gv_funcs.append(' sprintf(nodeName, "%s_%%p", (void*)&pStruct->%s);\n' % (stp_list[index]['name'], stp_list[index]['name']))
1991 gv_funcs.append(' tmpStr = %s(&pStruct->%s, nodeName);\n' % (self._get_gv_func_name(stp_list[index]['type']), stp_list[index]['name']))
1992 gv_funcs.append(' stp_strs[%i] = (char*)malloc(256+strlen(tmpStr)+strlen(nodeName)+strlen(myNodeName));\n' % (index))
1993 gv_funcs.append(' sprintf(stp_strs[%i], "%%s\\n\\"%%s\\":struct%i -> \\"%%s\\" [];\\n", tmpStr, myNodeName, nodeName);\n' % (index, index))
1994 total_strlen_str += 'strlen(stp_strs[%i]) + ' % index
1995 gv_funcs.append(' str = (char*)malloc(%ssizeof(char)*2048);\n' % (total_strlen_str))
1996 gv_funcs.append(' sprintf(str, "\\"%s\\" [\\nlabel = <<TABLE BORDER=\\"0\\" CELLBORDER=\\"1\\" CELLSPACING=\\"0\\"><TR><TD COLSPAN=\\"2\\">%s (%p)</TD></TR>')
1997 p_args = ", myNodeName, myNodeName, pStruct"
1998 for m in sorted(self.struct_dict[s]):
1999 plabel = ""
2000 if m in pl_dict:
2001 plabel = pl_dict[m]
2002 (p_out1, p_args1) = self._get_struct_gv_print_formatted(self.struct_dict[s][m], port_label=plabel)
2003 p_out += p_out1
2004 p_args += p_args1
2005 p_out += '</TABLE>>\\n];\\n\\n"'
2006 p_args += ");\n"
2007 gv_funcs.append(p_out)
2008 gv_funcs.append(p_args)
2009 if 0 != num_stps:
2010 gv_funcs.append(' for (int32_t stp_index = %i; stp_index >= 0; stp_index--) {\n' % (num_stps-1))
2011 gv_funcs.append(' if (0 < strlen(stp_strs[stp_index])) {\n')
2012 gv_funcs.append(' strncat(str, stp_strs[stp_index], strlen(stp_strs[stp_index]));\n')
2013 gv_funcs.append(' free(stp_strs[stp_index]);\n')
2014 gv_funcs.append(' }\n')
2015 gv_funcs.append(' }\n')
2016 gv_funcs.append(" return str;\n}\n")
2017 if s.lower().strip("_") in array_func_list:
Tobin Ehlisb8013a22015-02-26 12:57:08 -07002018 ptr_array = False
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06002019 if s.lower().strip("_") in ['vkbufferviewattachinfo', 'vkimageviewattachinfo']:
Tobin Ehlisb8013a22015-02-26 12:57:08 -07002020 ptr_array = True
2021 gv_funcs.append('char* %s_array(uint32_t count, const %s* const* pStruct, const char* myNodeName)\n{\n char* str;\n char tmpStr[1024];\n' % (self._get_gv_func_name(s), typedef_fwd_dict[s]))
2022 else:
2023 gv_funcs.append('char* %s_array(uint32_t count, const %s* pStruct, const char* myNodeName)\n{\n char* str;\n char tmpStr[1024];\n' % (self._get_gv_func_name(s), typedef_fwd_dict[s]))
Tobin Ehlis83562882014-11-27 15:43:39 -07002024 gv_funcs.append(' str = (char*)malloc(sizeof(char)*1024*count);\n')
2025 gv_funcs.append(' sprintf(str, "\\"%s\\" [\\nlabel = <<TABLE BORDER=\\"0\\" CELLBORDER=\\"1\\" CELLSPACING=\\"0\\"><TR><TD COLSPAN=\\"3\\">%s (%p)</TD></TR>", myNodeName, myNodeName, pStruct);\n')
2026 gv_funcs.append(' for (uint32_t i=0; i < count; i++) {\n')
2027 gv_funcs.append(' sprintf(tmpStr, "');
2028 p_args = ""
2029 p_out = ""
2030 for m in sorted(self.struct_dict[s]):
Tobin Ehlis83562882014-11-27 15:43:39 -07002031 plabel = ""
2032 (p_out1, p_args1) = self._get_struct_gv_print_formatted(self.struct_dict[s][m], port_label=plabel)
Tobin Ehlisb8013a22015-02-26 12:57:08 -07002033 if 0 == m: # Add array index notation at end of first row
2034 p_out1 = '%s<TD ROWSPAN=\\"%i\\" PORT=\\"slot%%u\\">%%u</TD></TR>' % (p_out1[:-5], len(self.struct_dict[s]))
Tobin Ehlis83562882014-11-27 15:43:39 -07002035 p_args1 += ', i, i'
2036 p_out += p_out1
2037 p_args += p_args1
2038 p_out += '"'
2039 p_args += ");\n"
Tobin Ehlisb8013a22015-02-26 12:57:08 -07002040 if ptr_array:
2041 p_args = p_args.replace('->', '[i]->')
2042 else:
2043 p_args = p_args.replace('->', '[i].')
Tobin Ehlis83562882014-11-27 15:43:39 -07002044 gv_funcs.append(p_out);
2045 gv_funcs.append(p_args);
2046 gv_funcs.append(' strncat(str, tmpStr, strlen(tmpStr));\n')
2047 gv_funcs.append(' }\n')
2048 gv_funcs.append(' strncat(str, "</TABLE>>\\n];\\n\\n", 20);\n')
Tobin Ehlis83562882014-11-27 15:43:39 -07002049 gv_funcs.append(' return str;\n}\n')
2050 # Add function to dynamically print out unknown struct
Mark Lobodzinski17caf572015-01-29 08:55:56 -06002051 gv_funcs.append("char* dynamic_gv_display(const void* pStruct, const char* nodeName)\n{\n")
Tobin Ehlis83562882014-11-27 15:43:39 -07002052 gv_funcs.append(" // Cast to APP_INFO ptr initially just to pull sType off struct\n")
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06002053 gv_funcs.append(" VkStructureType sType = ((VkApplicationInfo*)pStruct)->sType;\n")
Tobin Ehlis83562882014-11-27 15:43:39 -07002054 gv_funcs.append(" switch (sType)\n {\n")
2055 for e in enum_type_dict:
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06002056 if "StructureType" in e:
Tobin Ehlis83562882014-11-27 15:43:39 -07002057 for v in sorted(enum_type_dict[e]):
Courtney Goeltzenleuchter95487bc2015-04-14 18:48:46 -06002058 struct_name = get_struct_name_from_struct_type(v)
Chia-I Wu736324e2015-10-26 20:42:12 +08002059 if struct_name not in self.struct_dict:
2060 continue
2061
Tobin Ehlis83562882014-11-27 15:43:39 -07002062 print_func_name = self._get_gv_func_name(struct_name)
2063 # TODO : Hand-coded fixes for some exceptions
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06002064 #if 'VkPipelineCbStateCreateInfo' in struct_name:
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06002065 # struct_name = 'VK_PIPELINE_CB_STATE'
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06002066 if 'VkSemaphoreCreateInfo' in struct_name:
2067 struct_name = 'VkSemaphoreCreateInfo'
Tobin Ehlis83562882014-11-27 15:43:39 -07002068 print_func_name = self._get_gv_func_name(struct_name)
Courtney Goeltzenleuchterfb4efc62015-04-10 08:34:15 -06002069 elif 'VkSemaphoreOpenInfo' in struct_name:
2070 struct_name = 'VkSemaphoreOpenInfo'
Tobin Ehlis83562882014-11-27 15:43:39 -07002071 print_func_name = self._get_gv_func_name(struct_name)
2072 gv_funcs.append(' case %s:\n' % (v))
2073 gv_funcs.append(' return %s((%s*)pStruct, nodeName);\n' % (print_func_name, struct_name))
2074 #gv_funcs.append(' }\n')
2075 #gv_funcs.append(' break;\n')
Chia-I Wubfa3fea2014-12-16 00:43:20 +08002076 gv_funcs.append(" default:\n")
2077 gv_funcs.append(" return NULL;\n")
Tobin Ehlis83562882014-11-27 15:43:39 -07002078 gv_funcs.append(" }\n")
2079 gv_funcs.append("}")
2080 return "".join(gv_funcs)
2081
2082
2083
2084
2085
2086# def _generateHeader(self):
2087# hdr = []
2088# hdr.append('digraph g {\ngraph [\nrankdir = "LR"\n];')
2089# hdr.append('node [\nfontsize = "16"\nshape = "plaintext"\n];')
2090# hdr.append('edge [\n];\n')
2091# return "\n".join(hdr)
Mark Lobodzinski28b8f022015-08-27 15:30:29 -06002092#
Tobin Ehlis83562882014-11-27 15:43:39 -07002093# def _generateBody(self):
2094# body = []
Peter Lohrmann49ea1842015-03-30 16:29:56 -07002095# for s in sorted(self.struc_dict):
Tobin Ehlis83562882014-11-27 15:43:39 -07002096# field_num = 1
2097# body.append('"%s" [\nlabel = <<TABLE BORDER="0" CELLBORDER="1" CELLSPACING="0"> <TR><TD COLSPAN="2" PORT="f0">%s</TD></TR>' % (s, typedef_fwd_dict[s]))
2098# for m in sorted(self.struc_dict[s]):
2099# body.append('<TR><TD PORT="f%i">%s</TD><TD PORT="f%i">%s</TD></TR>' % (field_num, self.struc_dict[s][m]['full_type'], field_num+1, self.struc_dict[s][m]['name']))
2100# field_num += 2
2101# body.append('</TABLE>>\n];\n')
2102# return "".join(body)
Tobin Ehlis6442dca2014-10-22 15:13:53 -06002103
2104def main(argv=None):
2105 opts = handle_args()
2106 # Parse input file and fill out global dicts
2107 hfp = HeaderFileParser(opts.input_file)
2108 hfp.parse()
2109 # TODO : Don't want these to be global, see note at top about wrapper classes
2110 global enum_val_dict
2111 global enum_type_dict
2112 global struct_dict
2113 global typedef_fwd_dict
2114 global typedef_rev_dict
2115 global types_dict
2116 enum_val_dict = hfp.get_enum_val_dict()
2117 enum_type_dict = hfp.get_enum_type_dict()
2118 struct_dict = hfp.get_struct_dict()
Tobin Ehlis5a487e52014-11-11 12:28:12 -07002119 # TODO : Would like to validate struct data here to verify that all of the bools for struct members are correct at this point
Tobin Ehlis6442dca2014-10-22 15:13:53 -06002120 typedef_fwd_dict = hfp.get_typedef_fwd_dict()
2121 typedef_rev_dict = hfp.get_typedef_rev_dict()
2122 types_dict = hfp.get_types_dict()
2123 #print(enum_val_dict)
2124 #print(typedef_dict)
2125 #print(struct_dict)
Jeremy Hayesdf3f2052015-11-26 13:32:58 -07002126 input_header = os.path.basename(opts.input_file)
2127 if 'vulkan.h' == input_header:
2128 input_header = "vulkan/vulkan.h"
Jon Ashburn54e24602016-02-01 17:02:38 -07002129
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06002130 prefix = os.path.basename(opts.input_file).strip(".h")
2131 if prefix == "vulkan":
2132 prefix = "vk"
Tobin Ehlis6442dca2014-10-22 15:13:53 -06002133 if (opts.abs_out_dir is not None):
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06002134 enum_sh_filename = os.path.join(opts.abs_out_dir, prefix+"_enum_string_helper.h")
Tobin Ehlis6442dca2014-10-22 15:13:53 -06002135 else:
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06002136 enum_sh_filename = os.path.join(os.getcwd(), opts.rel_out_dir, prefix+"_enum_string_helper.h")
Tobin Ehlis7c47fc42014-12-17 07:20:23 -07002137 enum_sh_filename = os.path.abspath(enum_sh_filename)
2138 if not os.path.exists(os.path.dirname(enum_sh_filename)):
2139 print("Creating output dir %s" % os.path.dirname(enum_sh_filename))
2140 os.mkdir(os.path.dirname(enum_sh_filename))
Tobin Ehlis6442dca2014-10-22 15:13:53 -06002141 if opts.gen_enum_string_helper:
Tobin Ehlis7c47fc42014-12-17 07:20:23 -07002142 print("Generating enum string helper to %s" % enum_sh_filename)
Courtney Goeltzenleuchterd8e229c2015-04-08 15:36:08 -06002143 enum_vh_filename = os.path.join(os.path.dirname(enum_sh_filename), prefix+"_enum_validate_helper.h")
Tobin Ehlis7c47fc42014-12-17 07:20:23 -07002144 print("Generating enum validate helper to %s" % enum_vh_filename)
2145 eg = EnumCodeGen(enum_type_dict, enum_val_dict, typedef_fwd_dict, os.path.basename(opts.input_file), enum_sh_filename, enum_vh_filename)
Tobin Ehlis6442dca2014-10-22 15:13:53 -06002146 eg.generateStringHelper()
Tobin Ehlis7c47fc42014-12-17 07:20:23 -07002147 eg.generateEnumValidate()
Tobin Ehlis6442dca2014-10-22 15:13:53 -06002148 #for struct in struct_dict:
2149 #print(struct)
2150 if opts.gen_struct_wrappers:
Tobin Ehlis7c47fc42014-12-17 07:20:23 -07002151 sw = StructWrapperGen(struct_dict, os.path.basename(opts.input_file).strip(".h"), os.path.dirname(enum_sh_filename))
Tobin Ehlis6442dca2014-10-22 15:13:53 -06002152 #print(sw.get_class_name(struct))
Jeremy Hayesdf3f2052015-11-26 13:32:58 -07002153 sw.set_include_headers([input_header,os.path.basename(enum_sh_filename),"stdint.h","inttypes.h", "stdio.h","stdlib.h"])
Tobin Ehlis6442dca2014-10-22 15:13:53 -06002154 print("Generating struct wrapper header to %s" % sw.header_filename)
2155 sw.generateHeader()
2156 print("Generating struct wrapper class to %s" % sw.class_filename)
2157 sw.generateBody()
2158 sw.generateStringHelper()
Tobin Ehlis14ff0852014-12-17 17:44:50 -07002159 sw.generateValidateHelper()
Tobin Ehlisd49efcb2014-11-25 17:43:26 -07002160 # Generate a 2nd helper file that excludes addrs
2161 sw.set_no_addr(True)
2162 sw.generateStringHelper()
Tobin Ehlis99f88672015-01-10 12:42:41 -07002163 sw.set_no_addr(False)
Jeremy Hayesdf3f2052015-11-26 13:32:58 -07002164 sw.set_include_headers([input_header,os.path.basename(enum_sh_filename),"stdint.h","stdio.h","stdlib.h","iostream","sstream","string"])
Tobin Ehlis99f88672015-01-10 12:42:41 -07002165 sw.set_no_addr(True)
2166 sw.generateStringHelperCpp()
Tobin Ehlis5178bd72015-03-06 10:38:25 -07002167 sw.set_no_addr(False)
2168 sw.generateStringHelperCpp()
Jeremy Hayesdf3f2052015-11-26 13:32:58 -07002169 sw.set_include_headers(["stdio.h", "stdlib.h", input_header])
Tobin Ehlis60354772015-03-12 14:50:40 -06002170 sw.generateSizeHelper()
Peter Lohrmann4a8d8912015-04-03 11:43:06 -07002171 sw.generateSizeHelperC()
Tobin Ehlis37649692016-02-10 15:32:46 -07002172 sw.generateSafeStructHeader()
2173 sw.generateSafeStructs()
Jon Ashburnea65e492015-08-06 17:27:49 -06002174 if opts.gen_struct_sizes:
2175 st = StructWrapperGen(struct_dict, os.path.basename(opts.input_file).strip(".h"), os.path.dirname(enum_sh_filename))
Jeremy Hayesdf3f2052015-11-26 13:32:58 -07002176 st.set_include_headers(["stdio.h", "stdlib.h", input_header])
Jon Ashburnea65e492015-08-06 17:27:49 -06002177 st.generateSizeHelper()
2178 st.generateSizeHelperC()
Tobin Ehlis6442dca2014-10-22 15:13:53 -06002179 if opts.gen_cmake:
Tobin Ehlis7c47fc42014-12-17 07:20:23 -07002180 cmg = CMakeGen(sw, os.path.dirname(enum_sh_filename))
Tobin Ehlis6442dca2014-10-22 15:13:53 -06002181 cmg.generate()
2182 if opts.gen_graphviz:
Tobin Ehlis7c47fc42014-12-17 07:20:23 -07002183 gv = GraphVizGen(struct_dict, os.path.basename(opts.input_file).strip(".h"), os.path.dirname(enum_sh_filename))
Jeremy Hayesdf3f2052015-11-26 13:32:58 -07002184 gv.set_include_headers([input_header,os.path.basename(enum_sh_filename),"stdint.h","stdio.h","stdlib.h", "inttypes.h"])
Tobin Ehlis6442dca2014-10-22 15:13:53 -06002185 gv.generate()
2186 print("DONE!")
2187 #print(typedef_rev_dict)
2188 #print(types_dict)
2189 #recreate_structs()
2190
2191if __name__ == "__main__":
2192 sys.exit(main())