| 1 |
#!/usr/bin/python3 -i |
| 2 |
# |
| 3 |
# Copyright (c) 2015-2017 The Khronos Group Inc. |
| 4 |
# Copyright (c) 2015-2017 Valve Corporation |
| 5 |
# Copyright (c) 2015-2017 LunarG, Inc. |
| 6 |
# Copyright (c) 2015-2017 Google Inc. |
| 7 |
# |
| 8 |
# Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 |
# you may not use this file except in compliance with the License. |
| 10 |
# You may obtain a copy of the License at |
| 11 |
# |
| 12 |
# http://www.apache.org/licenses/LICENSE-2.0 |
| 13 |
# |
| 14 |
# Unless required by applicable law or agreed to in writing, software |
| 15 |
# distributed under the License is distributed on an "AS IS" BASIS, |
| 16 |
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 |
# See the License for the specific language governing permissions and |
| 18 |
# limitations under the License. |
| 19 |
# |
| 20 |
# Author: Mark Lobodzinski <[email protected]> |
| 21 |
# Author: Tobin Ehlis <[email protected]> |
| 22 |
# Author: John Zulauf <[email protected]> |
| 23 |
# Author: Charles Giessen <[email protected]> |
| 24 |
|
| 25 |
import re |
| 26 |
import os |
| 27 |
from base_generator import BaseGenerator |
| 28 |
|
| 29 |
class HelperFileGenerator(BaseGenerator): |
| 30 |
def __init__(self): |
| 31 |
BaseGenerator.__init__(self) |
| 32 |
|
| 33 |
# Helper for VkDebugReportObjectTypeEXT |
| 34 |
# Maps [ 'VkBuffer' : 'VK_DEBUG_REPORT_OBJECT_TYPE_BUFFER_EXT' ] |
| 35 |
# Will be 'VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT' if no type |
| 36 |
self.debugReportObject = dict() |
| 37 |
# Maps [ 'VkBuffer' : 'VK_OBJECT_TYPE_BUFFER' ] |
| 38 |
self.objectType = dict() |
| 39 |
|
| 40 |
# Takes a VK_OBJECT_TYPE_THE_TYP_EXT and turns it into TheTypeEXT |
| 41 |
def get_kVulkanObjectName(self, str_to_process): |
| 42 |
removed_prefix = str_to_process.split('_')[3:] |
| 43 |
if ''.join(removed_prefix[-1:]) in self.vk.vendorTags: |
| 44 |
return ''.join(str_to_process.title().split('_')[3:-1] + removed_prefix[-1:]) |
| 45 |
else: |
| 46 |
return ''.join(str_to_process.title().split('_')[3:]) |
| 47 |
|
| 48 |
def split_handle_by_capital_case(self, str_to_split): |
| 49 |
name = str_to_split[2:] |
| 50 |
name = name.replace('NVX', 'Nvx') |
| 51 |
# Force vendor tags to be title case so we can split by capital letter |
| 52 |
for tag in self.vk.vendorTags: |
| 53 |
name = name.replace(tag, tag.title()) |
| 54 |
# Split by capital letters so we can concoct a VK_DEBUG_REPORT_<>_EXT string out of it |
| 55 |
return re.split('(?<=.)(?=[A-Z])', f'{name}') |
| 56 |
|
| 57 |
|
| 58 |
def convert_to_VK_OBJECT_TYPE(self, str_to_convert): |
| 59 |
return '_'.join(['VK_OBJECT_TYPE'] + self.split_handle_by_capital_case(str_to_convert)).upper() |
| 60 |
|
| 61 |
def convert_to_VK_DEBUG_REPORT_OBJECT_TYPE(self, str_to_convert): |
| 62 |
return '_'.join(['VK_DEBUG_REPORT_OBJECT_TYPE'] + self.split_handle_by_capital_case(str_to_convert) + ['EXT']).upper() |
| 63 |
|
| 64 |
def generate(self): |
| 65 |
# Search all fields of the Enum to see if has a DEBUG_REPORT_OBJECT |
| 66 |
for handle in self.vk.handles.values(): |
| 67 |
debugObjects = ([enum.name for enum in self.vk.enums['VkDebugReportObjectTypeEXT'].fields if f'{handle.type[3:]}_EXT' in enum.name]) |
| 68 |
object = 'VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT' if len(debugObjects) == 0 else debugObjects[0] |
| 69 |
self.debugReportObject[handle.name] = object |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
out = [] |
| 74 |
out.append(f'''// clang-format off |
| 75 |
// *** THIS FILE IS GENERATED - DO NOT EDIT *** |
| 76 |
// See {os.path.basename(__file__)} for modifications |
| 77 |
|
| 78 |
|
| 79 |
/*************************************************************************** |
| 80 |
* |
| 81 |
* Copyright (c) 2015-2025 The Khronos Group Inc. |
| 82 |
* Copyright (c) 2015-2025 Valve Corporation |
| 83 |
* Copyright (c) 2015-2025 LunarG, Inc. |
| 84 |
* Copyright (c) 2015-2017 Google Inc. |
| 85 |
* |
| 86 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 87 |
* you may not use this file except in compliance with the License. |
| 88 |
* You may obtain a copy of the License at |
| 89 |
* |
| 90 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 91 |
* |
| 92 |
* Unless required by applicable law or agreed to in writing, software |
| 93 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 94 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 95 |
* See the License for the specific language governing permissions and |
| 96 |
* limitations under the License. |
| 97 |
* |
| 98 |
* Author: Mark Lobodzinski <[email protected]> |
| 99 |
* Author: Courtney Goeltzenleuchter <[email protected]> |
| 100 |
* Author: Tobin Ehlis <[email protected]> |
| 101 |
* Author: Chris Forbes <[email protected]> |
| 102 |
* Author: John Zulauf<[email protected]> |
| 103 |
* Author: Charles Giessen<[email protected]> |
| 104 |
* |
| 105 |
****************************************************************************/ |
| 106 |
|
| 107 |
|
| 108 |
#pragma once |
| 109 |
|
| 110 |
#include <vulkan/vulkan.h> |
| 111 |
|
| 112 |
''') |
| 113 |
|
| 114 |
# Get the list of field names from VkDebugReportObjectTypeEXT |
| 115 |
VkDebugReportObjectTypeEXT_field_names = [] |
| 116 |
for field in self.vk.enums['VkDebugReportObjectTypeEXT'].fields: |
| 117 |
VkDebugReportObjectTypeEXT_field_names.append(field.name) |
| 118 |
|
| 119 |
# Create a list with the needed information so that when we print, we print in the exact same order, as we are printing arrays where external code may index into it |
| 120 |
object_types = [] |
| 121 |
object_type_aliases = {} |
| 122 |
enum_num = 1 # start at one since the zero place is manually printed with the UNKNOWN value |
| 123 |
for handle in self.vk.handles.values(): |
| 124 |
debug_report_object_name = self.convert_to_VK_DEBUG_REPORT_OBJECT_TYPE(handle.name) |
| 125 |
if debug_report_object_name in VkDebugReportObjectTypeEXT_field_names: |
| 126 |
object_types.append((f'{handle.name[2:]}', enum_num, debug_report_object_name)) |
| 127 |
else: |
| 128 |
object_types.append((f'{handle.name[2:]}', enum_num, 'VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT')) |
| 129 |
enum_num += 1 |
| 130 |
if len(handle.aliases) > 0: |
| 131 |
object_type_aliases[handle.name] = handle.aliases |
| 132 |
|
| 133 |
out.append('// Object Type enum for validation layer internal object handling\n') |
| 134 |
out.append('typedef enum VulkanObjectType {\n') |
| 135 |
out.append(' kVulkanObjectTypeUnknown = 0,\n') |
| 136 |
for name, number, debug_report in object_types: |
| 137 |
out.append(f' kVulkanObjectType{name} = {number},\n') |
| 138 |
out.append(f' kVulkanObjectTypeMax = {enum_num},\n') |
| 139 |
|
| 140 |
out.append(' // Aliases for backwards compatibility of "promoted" types\n') |
| 141 |
for name, aliases in object_type_aliases.items(): |
| 142 |
for alias in aliases: |
| 143 |
out.append(f' kVulkanObjectType{alias[2:]} = kVulkanObjectType{name[2:]},\n') |
| 144 |
|
| 145 |
out.append('} VulkanObjectType;\n') |
| 146 |
out.append('\n') |
| 147 |
|
| 148 |
out.append('// Array of object name strings for OBJECT_TYPE enum conversion\n') |
| 149 |
out.append('static const char * const object_string[kVulkanObjectTypeMax] = {\n') |
| 150 |
out.append(' \"Unknown\",\n') |
| 151 |
for name, number, debug_report in object_types: |
| 152 |
out.append(f' \"{name}\",\n') |
| 153 |
out.append('};\n') |
| 154 |
out.append('\n') |
| 155 |
|
| 156 |
out.append('// Helper array to get Vulkan VK_EXT_debug_report object type enum from the internal layers version\n') |
| 157 |
out.append('const VkDebugReportObjectTypeEXT get_debug_report_enum[] = {\n') |
| 158 |
out.append(' VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT, // kVulkanObjectTypeUnknown\n') |
| 159 |
for name, number, debug_report in object_types: |
| 160 |
out.append(f' {debug_report}, // kVulkanObjectType{name}\n') |
| 161 |
out.append('};\n') |
| 162 |
out.append('\n') |
| 163 |
|
| 164 |
out.append('// Helper array to get Official Vulkan VkObjectType enum from the internal layers version\n') |
| 165 |
out.append('const VkObjectType get_object_type_enum[] = {\n') |
| 166 |
out.append(' VK_OBJECT_TYPE_UNKNOWN, // kVulkanObjectTypeUnknown\n') |
| 167 |
for handle in self.vk.handles.values(): |
| 168 |
object_name = self.convert_to_VK_OBJECT_TYPE(handle.name) |
| 169 |
out.append(f' {object_name}, // kVulkanObjectType{handle.name[2:]}\n') |
| 170 |
out.append('};\n') |
| 171 |
out.append('\n') |
| 172 |
|
| 173 |
out.append('// Helper function to convert from VkDebugReportObjectTypeEXT to VkObjectType\n') |
| 174 |
out.append('static inline VkObjectType convertDebugReportObjectToCoreObject(VkDebugReportObjectTypeEXT debug_report_obj){\n') |
| 175 |
out.append(' if (debug_report_obj == VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT) {\n') |
| 176 |
out.append(' return VK_OBJECT_TYPE_UNKNOWN;\n') |
| 177 |
for field in self.vk.enums['VkObjectType'].fields: |
| 178 |
enum_field_name = f'VK_DEBUG_REPORT_{field.name[3:]}_EXT' |
| 179 |
for debug_report_field in self.vk.enums['VkDebugReportObjectTypeEXT'].fields: |
| 180 |
if enum_field_name == debug_report_field.name: |
| 181 |
out.append(f' }} else if (debug_report_obj == VK_DEBUG_REPORT_{field.name[3:]}_EXT) {{\n') |
| 182 |
out.append(f' return {field.name};\n') |
| 183 |
break |
| 184 |
out.append(' }\n') |
| 185 |
out.append(' return VK_OBJECT_TYPE_UNKNOWN;\n') |
| 186 |
out.append('}\n') |
| 187 |
out.append('\n') |
| 188 |
|
| 189 |
out.append('// Helper function to convert from VkDebugReportObjectTypeEXT to VkObjectType\n') |
| 190 |
out.append('static inline VkDebugReportObjectTypeEXT convertCoreObjectToDebugReportObject(VkObjectType core_report_obj){\n') |
| 191 |
out.append(' if (core_report_obj == VK_OBJECT_TYPE_UNKNOWN) {\n') |
| 192 |
out.append(' return VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT;\n') |
| 193 |
for field in self.vk.enums['VkObjectType'].fields: |
| 194 |
enum_field_name = f'VK_DEBUG_REPORT_{field.name[3:]}_EXT' |
| 195 |
for debug_report_field in self.vk.enums['VkDebugReportObjectTypeEXT'].fields: |
| 196 |
if enum_field_name == debug_report_field.name: |
| 197 |
out.append(f' }} else if (core_report_obj == {field.name}) {{\n') |
| 198 |
out.append(f' return VK_DEBUG_REPORT_{field.name[3:]}_EXT;\n') |
| 199 |
break |
| 200 |
out.append(' }\n') |
| 201 |
out.append(' return VK_DEBUG_REPORT_OBJECT_TYPE_UNKNOWN_EXT;\n') |
| 202 |
out.append('}\n') |
| 203 |
|
| 204 |
out.append('// clang-format on') |
| 205 |
self.write("".join(out)) |