1#!/usr/bin/python3 -i 2# 3# Copyright 2013-2023 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 spec_tools.conventions import ConventionsBase 14 15# Modified from default implementation - see category_requires_validation() below 16CATEGORIES_REQUIRING_VALIDATION = set(('handle', 'enum', 'bitmask')) 17 18# Tokenize into "words" for structure types, approximately per spec "Implicit Valid Usage" section 2.7.2 19# This first set is for things we recognize explicitly as words, 20# as exceptions to the general regex. 21# Ideally these would be listed in the spec as exceptions, as OpenXR does. 22SPECIAL_WORDS = set(( 23 '16Bit', # VkPhysicalDevice16BitStorageFeatures 24 '2D', # VkPhysicalDeviceImage2DViewOf3DFeaturesEXT 25 '3D', # VkPhysicalDeviceImage2DViewOf3DFeaturesEXT 26 '8Bit', # VkPhysicalDevice8BitStorageFeaturesKHR 27 'AABB', # VkGeometryAABBNV 28 'ASTC', # VkPhysicalDeviceTextureCompressionASTCHDRFeaturesEXT 29 'D3D12', # VkD3D12FenceSubmitInfoKHR 30 'Float16', # VkPhysicalDeviceShaderFloat16Int8FeaturesKHR 31 'ImagePipe', # VkImagePipeSurfaceCreateInfoFUCHSIA 32 'Int64', # VkPhysicalDeviceShaderAtomicInt64FeaturesKHR 33 'Int8', # VkPhysicalDeviceShaderFloat16Int8FeaturesKHR 34 'MacOS', # VkMacOSSurfaceCreateInfoMVK 35 'RGBA10X6', # VkPhysicalDeviceRGBA10X6FormatsFeaturesEXT 36 'Uint8', # VkPhysicalDeviceIndexTypeUint8FeaturesEXT 37 'Win32', # VkWin32SurfaceCreateInfoKHR 38)) 39# A regex to match any of the SPECIAL_WORDS 40EXCEPTION_PATTERN = r'(?P<exception>{})'.format( 41 '|'.join('(%s)' % re.escape(w) for w in SPECIAL_WORDS)) 42MAIN_RE = re.compile( 43 # the negative lookahead is to prevent the all-caps pattern from being too greedy. 44 r'({}|([0-9]+)|([A-Z][a-z]+)|([A-Z][A-Z]*(?![a-z])))'.format(EXCEPTION_PATTERN)) 45 46 47class VulkanConventions(ConventionsBase): 48 @property 49 def null(self): 50 """Preferred spelling of NULL.""" 51 return '`NULL`' 52 53 def formatExtension(self, name): 54 """Mark up an extension name as a link the spec.""" 55 return '`apiext:{}`'.format(name) 56 57 @property 58 def struct_macro(self): 59 """Get the appropriate format macro for a structure. 60 61 Primarily affects generated valid usage statements. 62 """ 63 64 return 'slink:' 65 66 @property 67 def constFlagBits(self): 68 """Returns True if static const flag bits should be generated, False if an enumerated type should be generated.""" 69 return False 70 71 @property 72 def structtype_member_name(self): 73 """Return name of the structure type member""" 74 return 'sType' 75 76 @property 77 def nextpointer_member_name(self): 78 """Return name of the structure pointer chain member""" 79 return 'pNext' 80 81 @property 82 def valid_pointer_prefix(self): 83 """Return prefix to pointers which must themselves be valid""" 84 return 'valid' 85 86 def is_structure_type_member(self, paramtype, paramname): 87 """Determine if member type and name match the structure type member.""" 88 return paramtype == 'VkStructureType' and paramname == self.structtype_member_name 89 90 def is_nextpointer_member(self, paramtype, paramname): 91 """Determine if member type and name match the next pointer chain member.""" 92 return paramtype == 'void' and paramname == self.nextpointer_member_name 93 94 def generate_structure_type_from_name(self, structname): 95 """Generate a structure type name, like VK_STRUCTURE_TYPE_CREATE_INSTANCE_INFO""" 96 97 structure_type_parts = [] 98 # Tokenize into "words" 99 for elem in MAIN_RE.findall(structname): 100 word = elem[0] 101 if word == 'Vk': 102 structure_type_parts.append('VK_STRUCTURE_TYPE') 103 else: 104 structure_type_parts.append(word.upper()) 105 name = '_'.join(structure_type_parts) 106 107 # The simple-minded rules need modification for some structure names 108 subpats = [ 109 [ r'_H_(26[45])_', r'_H\1_' ], 110 [ r'_VULKAN_([0-9])([0-9])_', r'_VULKAN_\1_\2_' ], 111 [ r'_DIRECT_FB_', r'_DIRECTFB_' ], 112 [ r'_VULKAN_SC_10', r'_VULKAN_SC_1_0' ], 113 114 ] 115 116 for subpat in subpats: 117 name = re.sub(subpat[0], subpat[1], name) 118 return name 119 120 @property 121 def warning_comment(self): 122 """Return warning comment to be placed in header of generated Asciidoctor files""" 123 return '// WARNING: DO NOT MODIFY! This file is automatically generated from the vk.xml registry' 124 125 @property 126 def file_suffix(self): 127 """Return suffix of generated Asciidoctor files""" 128 return '.adoc' 129 130 def api_name(self, spectype='api'): 131 """Return API or specification name for citations in ref pages.ref 132 pages should link to for 133 134 spectype is the spec this refpage is for: 'api' is the Vulkan API 135 Specification. Defaults to 'api'. If an unrecognized spectype is 136 given, returns None. 137 """ 138 if spectype == 'api' or spectype is None: 139 return 'Vulkan' 140 else: 141 return None 142 143 @property 144 def api_prefix(self): 145 """Return API token prefix""" 146 return 'VK_' 147 148 @property 149 def write_contacts(self): 150 """Return whether contact list should be written to extension appendices""" 151 return True 152 153 @property 154 def write_refpage_include(self): 155 """Return whether refpage include should be written to extension appendices""" 156 return True 157 158 @property 159 def member_used_for_unique_vuid(self): 160 """Return the member name used in the VUID-...-...-unique ID.""" 161 return self.structtype_member_name 162 163 def is_externsync_command(self, protoname): 164 """Returns True if the protoname element is an API command requiring 165 external synchronization 166 """ 167 return protoname is not None and 'vkCmd' in protoname 168 169 def is_api_name(self, name): 170 """Returns True if name is in the reserved API namespace. 171 For Vulkan, these are names with a case-insensitive 'vk' prefix, or 172 a 'PFN_vk' function pointer type prefix. 173 """ 174 return name[0:2].lower() == 'vk' or name[0:6] == 'PFN_vk' 175 176 def specURL(self, spectype='api'): 177 """Return public registry URL which ref pages should link to for the 178 current all-extensions HTML specification, so xrefs in the 179 asciidoc source that are not to ref pages can link into it 180 instead. N.b. this may need to change on a per-refpage basis if 181 there are multiple documents involved. 182 """ 183 return 'https://registry.khronos.org/vulkan/specs/1.3-extensions/html/vkspec.html' 184 185 @property 186 def xml_api_name(self): 187 """Return the name used in the default API XML registry for the default API""" 188 return 'vulkan' 189 190 @property 191 def registry_path(self): 192 """Return relpath to the default API XML registry in this project.""" 193 return 'xml/vk.xml' 194 195 @property 196 def specification_path(self): 197 """Return relpath to the Asciidoctor specification sources in this project.""" 198 return '{generated}/meta' 199 200 @property 201 def special_use_section_anchor(self): 202 """Return asciidoctor anchor name in the API Specification of the 203 section describing extension special uses in detail.""" 204 return 'extendingvulkan-compatibility-specialuse' 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_file_path(self, name): 255 """Return file path to an extension appendix relative to a directory 256 containing all such appendices. 257 - name - extension name""" 258 259 return f'{name}{self.file_suffix}' 260 261 def valid_flag_bit(self, bitpos): 262 """Return True if bitpos is an allowed numeric bit position for 263 an API flag bit. 264 265 Vulkan uses 32 bit Vk*Flags types, and assumes C compilers may 266 cause Vk*FlagBits values with bit 31 set to result in a 64 bit 267 enumerated type, so disallows such flags.""" 268 return bitpos >= 0 and bitpos < 31 269 270 @property 271 def extra_refpage_headers(self): 272 """Return any extra text to add to refpage headers.""" 273 return 'include::{config}/attribs.adoc[]' 274 275 @property 276 def extra_refpage_body(self): 277 """Return any extra text (following the title) for generated 278 reference pages.""" 279 return 'include::{generated}/specattribs.adoc[]' 280