1#!/usr/bin/python3 -i 2# 3# Copyright 2013-2022 The Khronos Group Inc. 4# 5# SPDX-License-Identifier: Apache-2.0 6 7# Working-group-specific style conventions, 8# used in generation. 9 10import re 11import os 12 13from conventions import ConventionsBase 14 15 16# Modified from default implementation - see category_requires_validation() below 17CATEGORIES_REQUIRING_VALIDATION = set(('handle', 'enum', 'bitmask')) 18 19# Tokenize into "words" for structure types, approximately per spec "Implicit Valid Usage" section 2.7.2 20# This first set is for things we recognize explicitly as words, 21# as exceptions to the general regex. 22# Ideally these would be listed in the spec as exceptions, as OpenXR does. 23SPECIAL_WORDS = set(( 24 '16Bit', # VkPhysicalDevice16BitStorageFeatures 25 '2D', # VkPhysicalDeviceImage2DViewOf3DFeaturesEXT 26 '3D', # VkPhysicalDeviceImage2DViewOf3DFeaturesEXT 27 '8Bit', # VkPhysicalDevice8BitStorageFeaturesKHR 28 'AABB', # VkGeometryAABBNV 29 'ASTC', # VkPhysicalDeviceTextureCompressionASTCHDRFeaturesEXT 30 'D3D12', # VkD3D12FenceSubmitInfoKHR 31 'Float16', # VkPhysicalDeviceShaderFloat16Int8FeaturesKHR 32 'ImagePipe', # VkImagePipeSurfaceCreateInfoFUCHSIA 33 'Int64', # VkPhysicalDeviceShaderAtomicInt64FeaturesKHR 34 'Int8', # VkPhysicalDeviceShaderFloat16Int8FeaturesKHR 35 'MacOS', # VkMacOSSurfaceCreateInfoMVK 36 'RGBA10X6', # VkPhysicalDeviceRGBA10X6FormatsFeaturesEXT 37 'Uint8', # VkPhysicalDeviceIndexTypeUint8FeaturesEXT 38 'Win32', # VkWin32SurfaceCreateInfoKHR 39)) 40# A regex to match any of the SPECIAL_WORDS 41EXCEPTION_PATTERN = r'(?P<exception>{})'.format( 42 '|'.join('(%s)' % re.escape(w) for w in SPECIAL_WORDS)) 43MAIN_RE = re.compile( 44 # the negative lookahead is to prevent the all-caps pattern from being too greedy. 45 r'({}|([0-9]+)|([A-Z][a-z]+)|([A-Z][A-Z]*(?![a-z])))'.format(EXCEPTION_PATTERN)) 46 47 48class VulkanConventions(ConventionsBase): 49 @property 50 def null(self): 51 """Preferred spelling of NULL.""" 52 return '`NULL`' 53 54 @property 55 def struct_macro(self): 56 """Get the appropriate format macro for a structure. 57 58 Primarily affects generated valid usage statements. 59 """ 60 61 return 'slink:' 62 63 @property 64 def constFlagBits(self): 65 """Returns True if static const flag bits should be generated, False if an enumerated type should be generated.""" 66 return False 67 68 @property 69 def structtype_member_name(self): 70 """Return name of the structure type member""" 71 return 'sType' 72 73 @property 74 def nextpointer_member_name(self): 75 """Return name of the structure pointer chain member""" 76 return 'pNext' 77 78 @property 79 def valid_pointer_prefix(self): 80 """Return prefix to pointers which must themselves be valid""" 81 return 'valid' 82 83 def is_structure_type_member(self, paramtype, paramname): 84 """Determine if member type and name match the structure type member.""" 85 return paramtype == 'VkStructureType' and paramname == self.structtype_member_name 86 87 def is_nextpointer_member(self, paramtype, paramname): 88 """Determine if member type and name match the next pointer chain member.""" 89 return paramtype == 'void' and paramname == self.nextpointer_member_name 90 91 def generate_structure_type_from_name(self, structname): 92 """Generate a structure type name, like VK_STRUCTURE_TYPE_CREATE_INSTANCE_INFO""" 93 94 structure_type_parts = [] 95 # Tokenize into "words" 96 for elem in MAIN_RE.findall(structname): 97 word = elem[0] 98 if word == 'Vk': 99 structure_type_parts.append('VK_STRUCTURE_TYPE') 100 else: 101 structure_type_parts.append(word.upper()) 102 name = '_'.join(structure_type_parts) 103 104 # The simple-minded rules need modification for some structure names 105 subpats = [ 106 [ r'_H_(26[45])_', r'_H\1_' ], 107 [ r'_VULKAN_([0-9])([0-9])_', r'_VULKAN_\1_\2_' ], 108 [ r'_DIRECT_FB_', r'_DIRECTFB_' ], 109 ] 110 111 for subpat in subpats: 112 name = re.sub(subpat[0], subpat[1], name) 113 return name 114 115 @property 116 def warning_comment(self): 117 """Return warning comment to be placed in header of generated Asciidoctor files""" 118 return '// WARNING: DO NOT MODIFY! This file is automatically generated from the vk.xml registry' 119 120 @property 121 def file_suffix(self): 122 """Return suffix of generated Asciidoctor files""" 123 return '.txt' 124 125 def api_name(self, spectype='api'): 126 """Return API or specification name for citations in ref pages.ref 127 pages should link to for 128 129 spectype is the spec this refpage is for: 'api' is the Vulkan API 130 Specification. Defaults to 'api'. If an unrecognized spectype is 131 given, returns None. 132 """ 133 if spectype == 'api' or spectype is None: 134 return 'Vulkan' 135 else: 136 return None 137 138 @property 139 def api_prefix(self): 140 """Return API token prefix""" 141 return 'VK_' 142 143 @property 144 def write_contacts(self): 145 """Return whether contact list should be written to extension appendices""" 146 return True 147 148 @property 149 def write_refpage_include(self): 150 """Return whether refpage include should be written to extension appendices""" 151 return True 152 153 @property 154 def member_used_for_unique_vuid(self): 155 """Return the member name used in the VUID-...-...-unique ID.""" 156 return self.structtype_member_name 157 158 def is_externsync_command(self, protoname): 159 """Returns True if the protoname element is an API command requiring 160 external synchronization 161 """ 162 return protoname is not None and 'vkCmd' in protoname 163 164 def is_api_name(self, name): 165 """Returns True if name is in the reserved API namespace. 166 For Vulkan, these are names with a case-insensitive 'vk' prefix, or 167 a 'PFN_vk' function pointer type prefix. 168 """ 169 return name[0:2].lower() == 'vk' or name[0:6] == 'PFN_vk' 170 171 def specURL(self, spectype='api'): 172 """Return public registry URL which ref pages should link to for the 173 current all-extensions HTML specification, so xrefs in the 174 asciidoc source that are not to ref pages can link into it 175 instead. N.b. this may need to change on a per-refpage basis if 176 there are multiple documents involved. 177 """ 178 return 'https://www.khronos.org/registry/vulkan/specs/1.3-extensions/html/vkspec.html' 179 180 @property 181 def xml_api_name(self): 182 """Return the name used in the default API XML registry for the default API""" 183 return 'vulkan' 184 185 @property 186 def registry_path(self): 187 """Return relpath to the default API XML registry in this project.""" 188 return 'xml/vk.xml' 189 190 @property 191 def specification_path(self): 192 """Return relpath to the Asciidoctor specification sources in this project.""" 193 return '{generated}/meta' 194 195 @property 196 def special_use_section_anchor(self): 197 """Return asciidoctor anchor name in the API Specification of the 198 section describing extension special uses in detail.""" 199 return 'extendingvulkan-compatibility-specialuse' 200 201 @property 202 def extra_refpage_headers(self): 203 """Return any extra text to add to refpage headers.""" 204 return 'include::{config}/attribs.txt[]' 205 206 @property 207 def extension_index_prefixes(self): 208 """Return a list of extension prefixes used to group extension refpages.""" 209 return ['VK_KHR', 'VK_EXT', 'VK'] 210 211 @property 212 def unified_flag_refpages(self): 213 """Return True if Flags/FlagBits refpages are unified, False if 214 they are separate. 215 """ 216 return False 217 218 @property 219 def spec_reflow_path(self): 220 """Return the path to the spec source folder to reflow""" 221 return os.getcwd() 222 223 @property 224 def spec_no_reflow_dirs(self): 225 """Return a set of directories not to automatically descend into 226 when reflowing spec text 227 """ 228 return ('scripts', 'style') 229 230 @property 231 def zero(self): 232 return '`0`' 233 234 def category_requires_validation(self, category): 235 """Return True if the given type 'category' always requires validation. 236 237 Overridden because Vulkan does not require "valid" text for basetype 238 in the spec right now.""" 239 return category in CATEGORIES_REQUIRING_VALIDATION 240 241 @property 242 def should_skip_checking_codes(self): 243 """Return True if more than the basic validation of return codes should 244 be skipped for a command. 245 246 Vulkan mostly relies on the validation layers rather than API 247 builtin error checking, so these checks are not appropriate. 248 249 For example, passing in a VkFormat parameter will not potentially 250 generate a VK_ERROR_FORMAT_NOT_SUPPORTED code.""" 251 252 return True 253 254 def extension_include_string(self, ext): 255 """Return format string for include:: line for an extension appendix 256 file. ext is an object with the following members: 257 - name - extension string string 258 - vendor - vendor portion of name 259 - barename - remainder of name""" 260 261 return 'include::{{appendices}}/{name}{suffix}[]'.format( 262 name=ext.name, suffix=self.file_suffix) 263 264 @property 265 def refpage_generated_include_path(self): 266 """Return path relative to the generated reference pages, to the 267 generated API include files.""" 268 return "{generated}" 269 270 def valid_flag_bit(self, bitpos): 271 """Return True if bitpos is an allowed numeric bit position for 272 an API flag bit. 273 274 Vulkan uses 32 bit Vk*Flags types, and assumes C compilers may 275 cause Vk*FlagBits values with bit 31 set to result in a 64 bit 276 enumerated type, so disallows such flags.""" 277 return bitpos >= 0 and bitpos < 31 278