Move vk_safe_struct to VUL

This code was being generated in both Vulkan-ValidationLayers
and Vulkan-ExtensionLayer. Further uses are on the horizon so
lets stop the copypasta.

Also, add functions to manipulate extension lists and pNext chains,
since many client layers have been doing that themselves.
This commit is contained in:
Jeremy Gebben 2024-03-19 09:51:36 -06:00
parent d90f5c7eb1
commit cdd0e00cff
26 changed files with 91119 additions and 68 deletions

View file

@ -6,6 +6,7 @@ import("//build_overrides/vulkan_utility_libraries.gni")
config("vulkan_utility_libraries_config") {
include_dirs = [ "include" ]
defines = [ "VK_ENABLE_BETA_EXTENSIONS" ]
}
static_library("vulkan_layer_settings") {
@ -14,8 +15,11 @@ static_library("vulkan_layer_settings") {
sources = [
"include/vulkan/layer/vk_layer_settings.h",
"include/vulkan/layer/vk_layer_settings.hpp",
"include/vulkan/utility/vk_concurrent_unordered_map.hpp",
"include/vulkan/utility/vk_dispatch_table.h",
"include/vulkan/utility/vk_format_utils.h",
"include/vulkan/utility/vk_safe_struct.hpp",
"include/vulkan/utility/vk_safe_struct_utils.hpp",
"include/vulkan/utility/vk_struct_helper.hpp",
"include/vulkan/vk_enum_string_helper.h",
"scripts/gn/stub.cpp",
@ -25,5 +29,11 @@ static_library("vulkan_layer_settings") {
"src/layer/layer_settings_util.hpp",
"src/layer/vk_layer_settings.cpp",
"src/layer/vk_layer_settings_helper.cpp",
"src/vulkan/vk_safe_struct_core.cpp",
"src/vulkan/vk_safe_struct_ext.cpp",
"src/vulkan/vk_safe_struct_khr.cpp",
"src/vulkan/vk_safe_struct_utils.cpp",
"src/vulkan/vk_safe_struct_vendor.cpp",
"src/vulkan/vk_safe_struct_manual.cpp",
]
}

View file

@ -48,9 +48,10 @@ if (VUL_IS_TOP_LEVEL)
# Create VulkanUtilityLibraries-targets.cmake
set_target_properties(VulkanLayerSettings PROPERTIES EXPORT_NAME "LayerSettings")
set_target_properties(VulkanUtilityHeaders PROPERTIES EXPORT_NAME "UtilityHeaders")
set_target_properties(VulkanSafeStruct PROPERTIES EXPORT_NAME "SafeStruct")
set_target_properties(VulkanCompilerConfiguration PROPERTIES EXPORT_NAME "CompilerConfiguration")
install(
TARGETS VulkanLayerSettings VulkanUtilityHeaders VulkanCompilerConfiguration
TARGETS VulkanLayerSettings VulkanUtilityHeaders VulkanSafeStruct VulkanCompilerConfiguration
EXPORT VulkanUtilityLibraries-targets
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
)

View file

@ -6,8 +6,15 @@
target_include_directories(VulkanLayerSettings PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>)
target_sources(VulkanLayerSettings PRIVATE
vulkan/layer/vk_layer_settings.h
vulkan/layer/vk_layer_settings.hpp
vulkan/layer/vk_layer_settings.h
vulkan/layer/vk_layer_settings.hpp
)
target_include_directories(VulkanSafeStruct PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>)
target_sources(VulkanSafeStruct PRIVATE
vulkan/utility/vk_safe_struct.hpp
vulkan/utility/vk_safe_struct_utils.hpp
)
set(CMAKE_FOLDER "${CMAKE_FOLDER}/VulkanUtilityHeaders")
@ -17,15 +24,16 @@ add_library(Vulkan::UtilityHeaders ALIAS VulkanUtilityHeaders)
# https://cmake.org/cmake/help/latest/release/3.19.html#other
if (CMAKE_VERSION VERSION_GREATER_EQUAL "3.19")
target_sources(VulkanUtilityHeaders PRIVATE
vulkan/utility/vk_dispatch_table.h
vulkan/vk_enum_string_helper.h
vulkan/utility/vk_format_utils.h
vulkan/utility/vk_struct_helper.hpp
)
target_sources(VulkanUtilityHeaders PRIVATE
vulkan/vk_enum_string_helper.h
vulkan/utility/vk_concurrent_unordered_map.hpp
vulkan/utility/vk_dispatch_table.h
vulkan/utility/vk_format_utils.h
vulkan/utility/vk_struct_helper.hpp
)
endif()
target_link_Libraries(VulkanUtilityHeaders INTERFACE Vulkan::Headers)
target_link_libraries(VulkanUtilityHeaders INTERFACE Vulkan::Headers)
target_include_directories(VulkanUtilityHeaders INTERFACE $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>)

View file

@ -0,0 +1,201 @@
/* Copyright (c) 2015-2017, 2019-2024 The Khronos Group Inc.
* Copyright (c) 2015-2017, 2019-2024 Valve Corporation
* Copyright (c) 2015-2017, 2019-2024 LunarG, Inc.
* Modifications Copyright (C) 2022 RasterGrid Kft.
*
* SPDX-License-Identifier: Apache-2.0
*
*/
#pragma once
#include <array>
#include <functional>
#include <mutex>
#include <shared_mutex>
#include <unordered_map>
#include <vector>
namespace vku {
namespace concurrent {
// https://en.cppreference.com/w/cpp/thread/hardware_destructive_interference_size
// https://en.wikipedia.org/wiki/False_sharing
// TODO use C++20 to check for std::hardware_destructive_interference_size feature support.
constexpr std::size_t get_hardware_destructive_interference_size() { return 64; }
// Limited concurrent unordered_map that supports internally-synchronized
// insert/erase/access. Splits locking across N buckets and uses shared_mutex
// for read/write locking. Iterators are not supported. The following
// operations are supported:
//
// insert_or_assign: Insert a new element or update an existing element.
// insert: Insert a new element and return whether it was inserted.
// erase: Remove an element.
// contains: Returns true if the key is in the map.
// find: Returns != end() if found, value is in ret->second.
// pop: Erases and returns the erased value if found.
//
// find/end: find returns a vaguely iterator-like type that can be compared to
// end and can use iter->second to retrieve the reference. This is to ease porting
// for existing code that combines the existence check and lookup in a single
// operation (and thus a single lock). i.e.:
//
// auto iter = map.find(key);
// if (iter != map.end()) {
// T t = iter->second;
// ...
//
// snapshot: Return an array of elements (key, value pairs) that satisfy an optional
// predicate. This can be used as a substitute for iterators in exceptional cases.
template <typename Key, typename T, int BUCKETSLOG2 = 2, typename Map = std::unordered_map<Key, T>>
class unordered_map {
// Aliases to avoid excessive typing. We can't easily auto these away because
// there are virtual methods in ValidationObject which return lock guards
// and those cannot use return type deduction.
using ReadLockGuard = std::shared_lock<std::shared_mutex>;
using WriteLockGuard = std::unique_lock<std::shared_mutex>;
public:
template <typename... Args>
void insert_or_assign(const Key &key, Args &&...args) {
uint32_t h = ConcurrentMapHashObject(key);
WriteLockGuard lock(locks[h].lock);
maps[h][key] = {std::forward<Args>(args)...};
}
template <typename... Args>
bool insert(const Key &key, Args &&...args) {
uint32_t h = ConcurrentMapHashObject(key);
WriteLockGuard lock(locks[h].lock);
auto ret = maps[h].emplace(key, std::forward<Args>(args)...);
return ret.second;
}
// returns size_type
size_t erase(const Key &key) {
uint32_t h = ConcurrentMapHashObject(key);
WriteLockGuard lock(locks[h].lock);
return maps[h].erase(key);
}
bool contains(const Key &key) const {
uint32_t h = ConcurrentMapHashObject(key);
ReadLockGuard lock(locks[h].lock);
return maps[h].count(key) != 0;
}
// type returned by find() and end().
class FindResult {
public:
FindResult(bool a, T b) : result(a, std::move(b)) {}
// == and != only support comparing against end()
bool operator==(const FindResult &other) const {
if (result.first == false && other.result.first == false) {
return true;
}
return false;
}
bool operator!=(const FindResult &other) const { return !(*this == other); }
// Make -> act kind of like an iterator.
std::pair<bool, T> *operator->() { return &result; }
const std::pair<bool, T> *operator->() const { return &result; }
private:
// (found, reference to element)
std::pair<bool, T> result;
};
// find()/end() return a FindResult containing a copy of the value. For end(),
// return a default value.
FindResult end() const { return FindResult(false, T()); }
FindResult cend() const { return end(); }
FindResult find(const Key &key) const {
uint32_t h = ConcurrentMapHashObject(key);
ReadLockGuard lock(locks[h].lock);
auto itr = maps[h].find(key);
const bool found = itr != maps[h].end();
if (found) {
return FindResult(true, itr->second);
} else {
return end();
}
}
FindResult pop(const Key &key) {
uint32_t h = ConcurrentMapHashObject(key);
WriteLockGuard lock(locks[h].lock);
auto itr = maps[h].find(key);
const bool found = itr != maps[h].end();
if (found) {
auto ret = FindResult(true, itr->second);
maps[h].erase(itr);
return ret;
} else {
return end();
}
}
std::vector<std::pair<const Key, T>> snapshot(std::function<bool(T)> f = nullptr) const {
std::vector<std::pair<const Key, T>> ret;
for (int h = 0; h < BUCKETS; ++h) {
ReadLockGuard lock(locks[h].lock);
for (const auto &j : maps[h]) {
if (!f || f(j.second)) {
ret.emplace_back(j.first, j.second);
}
}
}
return ret;
}
void clear() {
for (int h = 0; h < BUCKETS; ++h) {
WriteLockGuard lock(locks[h].lock);
maps[h].clear();
}
}
size_t size() const {
size_t result = 0;
for (int h = 0; h < BUCKETS; ++h) {
ReadLockGuard lock(locks[h].lock);
result += maps[h].size();
}
return result;
}
bool empty() const {
bool result = 0;
for (int h = 0; h < BUCKETS; ++h) {
ReadLockGuard lock(locks[h].lock);
result |= maps[h].empty();
}
return result;
}
private:
static const int BUCKETS = (1 << BUCKETSLOG2);
Map maps[BUCKETS];
struct alignas(get_hardware_destructive_interference_size()) AlignedSharedMutex {
std::shared_mutex lock;
};
mutable std::array<AlignedSharedMutex, BUCKETS> locks;
uint32_t ConcurrentMapHashObject(const Key &object) const {
uint64_t u64 = (uint64_t)(uintptr_t)object;
uint32_t hash = (uint32_t)(u64 >> 32) + (uint32_t)u64;
hash ^= (hash >> BUCKETSLOG2) ^ (hash >> (2 * BUCKETSLOG2));
hash &= (BUCKETS - 1);
return hash;
}
};
} // namespace concurrent
} // namespace vku

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,128 @@
/***************************************************************************
*
* Copyright (c) 2015-2024 The Khronos Group Inc.
* Copyright (c) 2015-2024 Valve Corporation
* Copyright (c) 2015-2024 LunarG, Inc.
* Copyright (c) 2015-2024 Google Inc.
*
* SPDX-License-Identifier: Apache-2.0
*
****************************************************************************/
#pragma once
#include <vulkan/vulkan.h>
#include <cassert>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <vector>
namespace vku {
// Mapping of unknown stype codes to structure lengths. This should be set up by the application
// before vkCreateInstance() and not modified afterwards.
extern std::vector<std::pair<uint32_t, uint32_t>> custom_stype_info;
// State that elements in a pNext chain may need to be aware of
struct PNextCopyState {
// Custom initialization function. Returns true if the structure passed to init was initialized, false otherwise
std::function<bool(VkBaseOutStructure* /* safe_sruct */, const VkBaseOutStructure* /* in_struct */)> init;
};
void* SafePnextCopy(const void* pNext, PNextCopyState* copy_state = {});
void FreePnextChain(const void* pNext);
char* SafeStringCopy(const char* in_string);
template <typename Base, typename T>
bool AddToPnext(Base& base, const T& data) {
assert(base.ptr()); // All safe struct have a ptr() method. Prevent use with non-safe structs.
auto** prev = reinterpret_cast<VkBaseOutStructure**>(const_cast<void**>(&base.pNext));
auto* current = *prev;
while (current) {
if (data.sType == current->sType) {
return false;
}
prev = reinterpret_cast<VkBaseOutStructure**>(&current->pNext);
current = *prev;
}
*prev = reinterpret_cast<VkBaseOutStructure*>(SafePnextCopy(&data));
return true;
}
template <typename Base>
bool RemoveFromPnext(Base& base, VkStructureType t) {
assert(base.ptr()); // All safe struct have a ptr() method. Prevent use with non-safe structs.
auto** prev = reinterpret_cast<VkBaseOutStructure**>(const_cast<void**>(&base.pNext));
auto* current = *prev;
while (current) {
if (t == current->sType) {
*prev = current->pNext;
current->pNext = nullptr;
FreePnextChain(current);
return true;
}
prev = reinterpret_cast<VkBaseOutStructure**>(&current->pNext);
current = *prev;
}
return false;
}
template <typename CreateInfo>
uint32_t FindExtension(CreateInfo& ci, const char* extension_name) {
assert(ci.ptr()); // All safe struct have a ptr() method. Prevent use with non-safe structs.
for (uint32_t i = 0; i < ci.enabledExtensionCount; i++) {
if (strcmp(ci.ppEnabledExtensionNames[i], extension_name) == 0) {
return i;
}
}
return ci.enabledExtensionCount;
}
template <typename CreateInfo>
bool AddExtension(CreateInfo& ci, const char* extension_name) {
assert(ci.ptr()); // All safe struct have a ptr() method. Prevent use with non-safe structs.
uint32_t pos = FindExtension(ci, extension_name);
if (pos < ci.enabledExtensionCount) {
// already present
return false;
}
char** exts = new char*[ci.enabledExtensionCount + 1];
memcpy(exts, ci.ppEnabledExtensionNames, sizeof(char*) * ci.enabledExtensionCount);
exts[ci.enabledExtensionCount] = SafeStringCopy(extension_name);
delete[] ci.ppEnabledExtensionNames;
ci.ppEnabledExtensionNames = exts;
ci.enabledExtensionCount++;
return true;
}
template <typename CreateInfo>
bool RemoveExtension(CreateInfo& ci, const char* extension_name) {
assert(ci.ptr()); // All safe struct have a ptr() method. Prevent use with non-safe structs.
uint32_t pos = FindExtension(ci, extension_name);
if (pos >= ci.enabledExtensionCount) {
// not present
return false;
}
if (ci.enabledExtensionCount == 1) {
delete[] ci.ppEnabledExtensionNames[0];
delete[] ci.ppEnabledExtensionNames;
ci.ppEnabledExtensionNames = nullptr;
ci.enabledExtensionCount = 0;
return true;
}
uint32_t out_pos = 0;
char** exts = new char*[ci.enabledExtensionCount - 1];
for (uint32_t i = 0; i < ci.enabledExtensionCount; i++) {
if (i == pos) {
delete[] ci.ppEnabledExtensionNames[i];
} else {
exts[out_pos++] = const_cast<char*>(ci.ppEnabledExtensionNames[i]);
}
}
delete[] ci.ppEnabledExtensionNames;
ci.ppEnabledExtensionNames = exts;
ci.enabledExtensionCount--;
return true;
}
} // namespace vku

54
scripts/common_ci.py Normal file
View file

@ -0,0 +1,54 @@
#!/usr/bin/python3 -i
#
# Copyright (c) 2015-2024 The Khronos Group Inc.
# Copyright (c) 2015-2024 Valve Corporation
# Copyright (c) 2015-2024 LunarG, Inc.
#
# SPDX-License-Identifier: Apache-2.0
import os
import sys
import subprocess
import platform
# Use Ninja for all platforms for performance/simplicity
os.environ['CMAKE_GENERATOR'] = "Ninja"
# helper to define paths relative to the repo root
def RepoRelative(path):
return os.path.abspath(os.path.join(os.path.dirname(__file__), '..', path))
# Points to the directory containing the top level CMakeLists.txt
PROJECT_SRC_DIR = os.path.abspath(os.path.join(os.path.split(os.path.abspath(__file__))[0], '..'))
if not os.path.isfile(f'{PROJECT_SRC_DIR}/CMakeLists.txt'):
print(f'PROJECT_SRC_DIR invalid! {PROJECT_SRC_DIR}')
sys.exit(1)
# Returns true if we are running in GitHub actions
# https://docs.github.com/en/actions/learn-github-actions/variables#default-environment-variables
def IsGHA():
if 'GITHUB_ACTION' in os.environ:
return True
return False
# Runs a command in a directory and returns its return code.
# Directory is project root by default, or a relative path from project root
def RunShellCmd(command, start_dir = PROJECT_SRC_DIR, env=None, verbose=False):
# Flush stdout here. Helps when debugging on CI.
sys.stdout.flush()
if start_dir != PROJECT_SRC_DIR:
start_dir = RepoRelative(start_dir)
cmd_list = command.split(" ")
# Helps a lot when debugging CI issues
if IsGHA():
verbose = True
if verbose:
print(f'CICMD({cmd_list}, env={env})')
subprocess.check_call(cmd_list, cwd=start_dir, env=env)
#
# Check if the system is Windows
def IsWindows(): return 'windows' == platform.system().lower()

View file

@ -8,10 +8,16 @@
import argparse
import os
import sys
import shutil
import common_ci
from xml.etree import ElementTree
def RunGenerators(api: str, registry: str, targetFilter: str) -> None:
has_clang_format = shutil.which('clang-format') is not None
if not has_clang_format:
print("WARNING: Unable to find clang-format!")
# These live in the Vulkan-Docs repo, but are pulled in via the
# Vulkan-Headers/registry folder
# At runtime we inject python path to find these helper scripts
@ -26,6 +32,7 @@ def RunGenerators(api: str, registry: str, targetFilter: str) -> None:
from generators.enum_string_helper_generator import EnumStringHelperOutputGenerator
from generators.format_utils_generator import FormatUtilsOutputGenerator
from generators.struct_helper_generator import StructHelperOutputGenerator
from generators.safe_struct_generator import SafeStructOutputGenerator
# These set fields that are needed by both OutputGenerator and BaseGenerator,
# but are uniform and don't need to be set at a per-generated file level
@ -54,6 +61,37 @@ def RunGenerators(api: str, registry: str, targetFilter: str) -> None:
'genCombined': True,
'directory' : f'include/{api}/utility',
},
'vk_safe_struct.hpp' : {
'generator' : SafeStructOutputGenerator,
'genCombined': True,
'directory' : f'include/{api}/utility',
},
'vk_safe_struct_utils.cpp' : {
'generator' : SafeStructOutputGenerator,
'genCombined': True,
'directory' : f'src/{api}',
},
'vk_safe_struct_core.cpp' : {
'generator' : SafeStructOutputGenerator,
'genCombined': True,
'regenerate' : True,
'directory' : f'src/{api}',
},
'vk_safe_struct_khr.cpp' : {
'generator' : SafeStructOutputGenerator,
'genCombined': True,
'directory' : f'src/{api}',
},
'vk_safe_struct_ext.cpp' : {
'generator' : SafeStructOutputGenerator,
'genCombined': True,
'directory' : f'src/{api}',
},
'vk_safe_struct_vendor.cpp' : {
'generator' : SafeStructOutputGenerator,
'genCombined': True,
'directory' : f'src/{api}',
},
}
if (targetFilter and targetFilter not in generators.keys()):
@ -102,6 +140,9 @@ def RunGenerators(api: str, registry: str, targetFilter: str) -> None:
# Finally, use the output generator to create the requested target
reg.apiGen()
# Run clang-format on the file
if has_clang_format:
common_ci.RunShellCmd(f'clang-format -i {os.path.join(outDirectory, target)}')
def main(argv):
parser = argparse.ArgumentParser(description='Generate source code for this repository')

View file

@ -1,12 +1,14 @@
#!/usr/bin/python3 -i
#
# Copyright 2023-2024 The Khronos Group Inc.
# Copyright 2023-2024 Valve Corporation
# Copyright 2023-2024 LunarG, Inc.
# Copyright 2023-2024 RasterGrid Kft.
# Copyright (c) 2023-2024 Valve Corporation
# Copyright (c) 2023-2024 LunarG, Inc.
# Copyright (c) 2023-2024 RasterGrid Kft.
#
# SPDX-License-Identifier: Apache-2.0
import pickle
import os
import tempfile
from generators.vulkan_object import (VulkanObject,
Extension, Version, Handle, Param, Queues, CommandScope, Command,
EnumField, Enum, Flag, Bitmask, Member, Struct,
@ -70,6 +72,11 @@ def SetMergedApiNames(names: str) -> None:
global mergedApiNames
mergedApiNames = names
cachingEnabled = False
def EnableCaching() -> None:
global cachingEnabled
cachingEnabled = True
# This class is a container for any source code, data, or other behavior that is necessary to
# customize the generator script for a specific target API variant (e.g. Vulkan SC). As such,
# all of these API-specific interfaces and their use in the generator script are part of the
@ -111,7 +118,7 @@ class BaseGeneratorOptions(GeneratorOptions):
self.apicall = 'VKAPI_ATTR '
self.apientry = 'VKAPI_CALL '
self.apientryp = 'VKAPI_PTR *'
self.alignFuncParam = 48
self.alignFuncParam = 48
#
# This object handles all the parsing from reg.py generator scripts in the Vulkan-Headers
@ -219,7 +226,6 @@ class BaseGenerator(OutputGenerator):
enum.fieldExtensions.extend([extension] if extension not in enum.fieldExtensions else [])
enumField.extensions.extend([extension] if extension not in enumField.extensions else [])
extension.enumFields[group].extend([enumField] if enumField not in extension.enumFields[group] else [])
if group in self.vk.bitmasks:
if group not in extension.flags:
extension.flags[group] = [] # Dict needs init
@ -229,11 +235,10 @@ class BaseGenerator(OutputGenerator):
for flags in [x for x in bitmask.flags if x.name in flagList]:
# Make sure list is unique
bitmask.extensions.extend([extension] if extension not in bitmask.extensions else [])
bitmask.flagExtensions.extend([extension] if extension not in bitmask.flagExtensions else [])
flags.extensions.extend([extension] if extension not in flags.extensions else [])
extension.flags[group].extend([flags] if flags not in extension.flags[group] else [])
# Need to do 'enum'/'bitmask' after 'enumconstant' has applied everything so we can add implicit extensions
#
# Sometimes two extensions enable an Enum, but the newer extension version has extra flags allowed
@ -267,17 +272,18 @@ class BaseGenerator(OutputGenerator):
for required in dict:
for group in dict[required]:
for bitmaskName in dict[required][group]:
isAlias = bitmaskName in self.enumAliasMap
bitmaskName = bitmaskName.replace('Flags', 'FlagBits') # Works since Flags isn't repeated in name
isAlias = bitmaskName in self.bitmaskAliasMap
bitmaskName = self.bitmaskAliasMap[bitmaskName] if isAlias else bitmaskName
if bitmaskName in self.vk.bitmasks:
bitmask = self.vk.bitmasks[bitmaskName]
bitmask.extensions.extend([extension] if extension not in bitmask.extensions else [])
extension.bitmask.extend([bitmask] if bitmask not in extension.bitmasks else [])
extension.bitmasks.extend([bitmask] if bitmask not in extension.bitmasks else [])
# Update flags with implicit base extension
if isAlias:
continue
bitmask.flagExtensions.extend([extension] if extension not in enum.flagExtensions else [])
for flag in [x for x in enum.flags if (not x.extensions or (x.extensions and all(e in enum.extensions for e in x.extensions)))]:
bitmask.flagExtensions.extend([extension] if extension not in bitmask.flagExtensions else [])
for flag in [x for x in bitmask.flags if (not x.extensions or (x.extensions and all(e in bitmask.extensions for e in x.extensions)))]:
flag.extensions.extend([extension] if extension not in flag.extensions else [])
if bitmaskName not in extension.flags:
extension.flags[bitmaskName] = [] # Dict needs init
@ -304,9 +310,17 @@ class BaseGenerator(OutputGenerator):
for struct in [x for x in self.vk.structs.values() if not x.returnedOnly]:
for enum in [self.vk.enums[x.type] for x in struct.members if x.type in self.vk.enums]:
enum.returnedOnly = False
for bitmask in [self.vk.bitmasks[x.type] for x in struct.members if x.type in self.vk.bitmasks]:
bitmask.returnedOnly = False
for bitmask in [self.vk.bitmasks[x.type.replace('Flags', 'FlagBits')] for x in struct.members if x.type.replace('Flags', 'FlagBits') in self.vk.bitmasks]:
bitmask.returnedOnly = False
for command in self.vk.commands.values():
for enum in [self.vk.enums[x.type] for x in command.params if x.type in self.vk.enums]:
enum.returnedOnly = False
for bitmask in [self.vk.bitmasks[x.type] for x in command.params if x.type in self.vk.bitmasks]:
bitmask.returnedOnly = False
for bitmask in [self.vk.bitmasks[x.type.replace('Flags', 'FlagBits')] for x in command.params if x.type.replace('Flags', 'FlagBits') in self.vk.bitmasks]:
bitmask.returnedOnly = False
# Turn handle parents into pointers to classess
for handle in [x for x in self.vk.handles.values() if x.parent is not None]:
@ -327,9 +341,26 @@ class BaseGenerator(OutputGenerator):
# All inherited generators should run from here
self.generate()
if cachingEnabled:
cachePath = os.path.join(tempfile.gettempdir(), f'vkobject_{os.getpid()}')
if not os.path.isfile(cachePath):
cacheFile = open(cachePath, 'wb')
pickle.dump(self.vk, cacheFile)
cacheFile.close()
# This should not have to do anything but call into OutputGenerator
OutputGenerator.endFile(self)
#
# Bypass the entire processing and load in the VkObject data
# Still need to handle the beingFile/endFile for reg.py
def generateFromCache(self, cacheVkObjectData, genOpts):
OutputGenerator.beginFile(self, genOpts)
self.filename = genOpts.filename
self.vk = cacheVkObjectData
self.generate()
OutputGenerator.endFile(self)
#
# Processing point at beginning of each extension definition
def beginFeature(self, interface, emit):
@ -337,7 +368,6 @@ class BaseGenerator(OutputGenerator):
platform = interface.get('platform')
self.featureExtraProtec = self.vk.platforms[platform] if platform in self.vk.platforms else None
protect = self.vk.platforms[platform] if platform in self.vk.platforms else None
name = interface.get('name')
if interface.tag == 'extension':
@ -463,7 +493,7 @@ class BaseGenerator(OutputGenerator):
# fields also have their own protect
groupProtect = self.currentExtension.protect if hasattr(self.currentExtension, 'protect') and self.currentExtension.protect is not None else None
enumElem = groupinfo.elem
bitwidth = 32 if enumElem.get('bitwidth') is None else enumElem.get('bitwidth')
bitwidth = 32 if enumElem.get('bitwidth') is None else int(enumElem.get('bitwidth'))
fields = []
if enumElem.get('type') == "enum":
if alias is not None:
@ -514,7 +544,7 @@ class BaseGenerator(OutputGenerator):
fields.append(Flag(flagName, protect, flagValue, flagMultiBit, flagZero, []))
flagName = groupName.replace('FlagBits', 'Flags')
self.vk.bitmasks[groupName] = Bitmask(groupName, flagName, groupProtect, bitwidth, fields, [], [])
self.vk.bitmasks[groupName] = Bitmask(groupName, flagName, groupProtect, bitwidth, True, fields, [], [])
def genType(self, typeInfo, typeName, alias):
OutputGenerator.genType(self, typeInfo, typeName, alias)

View file

@ -44,9 +44,9 @@ typedef struct VkuInstanceDispatchTable_ {
''')
guard_helper = PlatformGuardHelper()
for command in [x for x in self.vk.commands.values() if x.instance]:
out.extend(guard_helper.addGuard(command.protect))
out.extend(guard_helper.add_guard(command.protect))
out.append(f' PFN_{command.name} {command.name[2:]};\n')
out.extend(guard_helper.addGuard(None))
out.extend(guard_helper.add_guard(None))
out.append('} VkuInstanceDispatchTable;\n')
out.append('''
@ -54,9 +54,9 @@ typedef struct VkuInstanceDispatchTable_ {
typedef struct VkuDeviceDispatchTable_ {
''')
for command in [x for x in self.vk.commands.values() if x.device]:
out.extend(guard_helper.addGuard(command.protect))
out.extend(guard_helper.add_guard(command.protect))
out.append(f' PFN_{command.name} {command.name[2:]};\n')
out.extend(guard_helper.addGuard(None))
out.extend(guard_helper.add_guard(None))
out.append('} VkuDeviceDispatchTable;\n')
out.append('''
@ -67,9 +67,9 @@ static inline void vkuInitDeviceDispatchTable(VkDevice device, VkuDeviceDispatch
''')
for command in [x for x in self.vk.commands.values() if x.device and x.name != 'vkGetDeviceProcAddr']:
out.extend(guard_helper.addGuard(command.protect))
out.extend(guard_helper.add_guard(command.protect))
out.append(f' table->{command.name[2:]} = (PFN_{command.name})gdpa(device, "{command.name}");\n')
out.extend(guard_helper.addGuard(None))
out.extend(guard_helper.add_guard(None))
out.append('}\n')
out.append('''
@ -89,9 +89,9 @@ static inline void vkuInitInstanceDispatchTable(VkInstance instance, VkuInstance
'vkEnumerateInstanceVersion',
'vkGetInstanceProcAddr',
]]:
out.extend(guard_helper.addGuard(command.protect))
out.extend(guard_helper.add_guard(command.protect))
out.append(f' table->{command.name[2:]} = (PFN_{command.name})gipa(instance, "{command.name}");\n')
out.extend(guard_helper.addGuard(None))
out.extend(guard_helper.add_guard(None))
out.append('}\n')
out.append('// clang-format on')

View file

@ -39,20 +39,20 @@ class EnumStringHelperOutputGenerator(BaseGenerator):
# If there are no fields (empty enum) ignore
for enum in [x for x in self.vk.enums.values() if len(x.fields) > 0]:
groupType = enum.name if enum.bitWidth == 32 else 'uint64_t'
out.extend(guard_helper.addGuard(enum.protect))
out.extend(guard_helper.add_guard(enum.protect))
out.append(f'static inline const char* string_{enum.name}({groupType} input_value) {{\n')
out.append(' switch (input_value) {\n')
enum_field_guard_helper = PlatformGuardHelper()
for field in enum.fields:
out.extend(enum_field_guard_helper.addGuard(field.protect))
out.extend(enum_field_guard_helper.add_guard(field.protect))
out.append(f' case {field.name}:\n')
out.append(f' return "{field.name}";\n')
out.extend(enum_field_guard_helper.addGuard(None))
out.extend(enum_field_guard_helper.add_guard(None))
out.append(' default:\n')
out.append(f' return "Unhandled {enum.name}";\n')
out.append(' }\n')
out.append('}\n')
out.extend(guard_helper.addGuard(None))
out.extend(guard_helper.add_guard(None))
out.append('\n')
# For bitmask, first create a string for FlagBits, then a Flags version that calls into it
@ -65,26 +65,26 @@ class EnumStringHelperOutputGenerator(BaseGenerator):
if groupType == 'uint64_t':
use_switch_statement = False
out.extend(guard_helper.addGuard(bitmask.protect))
out.extend(guard_helper.add_guard(bitmask.protect))
out.append(f'static inline const char* string_{bitmask.name}({groupType} input_value) {{\n')
bitmask_field_guard_helper = PlatformGuardHelper()
if use_switch_statement:
out.append(' switch (input_value) {\n')
for flag in [x for x in bitmask.flags if not x.multiBit]:
out.extend(bitmask_field_guard_helper.addGuard(flag.protect))
out.extend(bitmask_field_guard_helper.add_guard(flag.protect))
out.append(f' case {flag.name}:\n')
out.append(f' return "{flag.name}";\n')
out.extend(bitmask_field_guard_helper.addGuard(None))
out.extend(bitmask_field_guard_helper.add_guard(None))
out.append(' default:\n')
out.append(f' return "Unhandled {bitmask.name}";\n')
out.append(' }\n')
else:
# We need to use if statements
for flag in [x for x in bitmask.flags if not x.multiBit]:
out.extend(bitmask_field_guard_helper.addGuard(flag.protect))
out.extend(bitmask_field_guard_helper.add_guard(flag.protect))
out.append(f' if (input_value == {flag.name}) return "{flag.name}";\n')
out.extend(bitmask_field_guard_helper.addGuard(None))
out.extend(bitmask_field_guard_helper.add_guard(None))
out.append(f' return "Unhandled {bitmask.name}";\n')
out.append('}\n')
@ -110,6 +110,6 @@ static inline std::string string_{bitmask.flagName}({bitmask.flagName} input_val
return ret;
}}\n''')
out.append('#endif // __cplusplus\n')
out.extend(guard_helper.addGuard(None))
out.extend(guard_helper.add_guard(None))
out.append('// clang-format on')
self.write("".join(out))

View file

@ -1,25 +1,148 @@
#!/usr/bin/python3 -i
#
# Copyright 2023 The Khronos Group Inc.
# Copyright 2023 Valve Corporation
# Copyright 2023 LunarG, Inc.
# Copyright 2023 RasterGrid Kft.
# Copyright (c) 2023-2024 Valve Corporation
# Copyright (c) 2023-2024 LunarG, Inc.
#
# SPDX-License-Identifier: Apache-2.0
import os
import sys
import json
# Build a set of all vuid text strings found in validusage.json
def buildListVUID(valid_usage_file: str) -> set:
# Walk the JSON-derived dict and find all "vuid" key values
def ExtractVUIDs(vuid_dict):
if hasattr(vuid_dict, 'items'):
for key, value in vuid_dict.items():
if key == "vuid":
yield value
elif isinstance(value, dict):
for vuid in ExtractVUIDs(value):
yield vuid
elif isinstance (value, list):
for listValue in value:
for vuid in ExtractVUIDs(listValue):
yield vuid
valid_vuids = set()
if not os.path.isfile(valid_usage_file):
print(f'Error: Could not find, or error loading {valid_usage_file}')
sys.exit(1)
json_file = open(valid_usage_file, 'r', encoding='utf-8')
vuid_dict = json.load(json_file)
json_file.close()
if len(vuid_dict) == 0:
print(f'Error: Failed to load {valid_usage_file}')
sys.exit(1)
for json_vuid_string in ExtractVUIDs(vuid_dict):
valid_vuids.add(json_vuid_string)
# List of VUs that should exists, but have a spec bug
for vuid in [
# https://gitlab.khronos.org/vulkan/vulkan/-/issues/3582
"VUID-VkCopyImageToImageInfoEXT-commonparent",
"VUID-vkUpdateDescriptorSetWithTemplate-descriptorSet-parent",
"VUID-vkUpdateVideoSessionParametersKHR-videoSessionParameters-parent",
"VUID-vkDestroyVideoSessionParametersKHR-videoSessionParameters-parent",
"VUID-vkGetDescriptorSetHostMappingVALVE-descriptorSet-parent",
]:
valid_vuids.add(vuid)
return valid_vuids
# Will do a sanity check the VUID exists
def getVUID(valid_vuids: set, vuid: str, quotes: bool = True) -> str:
if vuid not in valid_vuids:
print(f'Warning: Could not find {vuid} in validusage.json')
vuid = vuid.replace('VUID-', 'UNASSIGNED-')
return vuid if not quotes else f'"{vuid}"'
class PlatformGuardHelper():
"""Used to elide platform guards together, so redundant #endif then #ifdefs are removed
Note - be sure to call addGuard(None) when done to add a trailing #endif if needed
Note - be sure to call add_guard(None) when done to add a trailing #endif if needed
"""
def __init__(self):
self.currentGuard = None
self.current_guard = None
def addGuard(self, guard):
def add_guard(self, guard, extra_newline = False):
out = []
if self.currentGuard != guard:
if self.currentGuard != None:
out.append(f'#endif // {self.currentGuard}\n')
if guard != None:
out.append(f'#ifdef {guard}\n')
self.currentGuard = guard
if self.current_guard != guard and self.current_guard is not None:
out.append(f'#endif // {self.current_guard}\n')
if extra_newline:
out.append('\n')
if self.current_guard != guard and guard is not None:
out.append(f'#ifdef {guard}\n')
self.current_guard = guard
return out
# The SPIR-V grammar json doesn't have an easy way to detect these, so have listed by hand
# If we are missing one, its not critical, the goal of this list is to reduce the generated output size
def IsNonVulkanSprivCapability(capability):
return capability in [
'Kernel',
'Vector16',
'Float16Buffer',
'ImageBasic',
'ImageReadWrite',
'ImageMipmap',
'DeviceEnqueue',
'SubgroupDispatch',
'Pipes',
'LiteralSampler',
'NamedBarrier',
'PipeStorage',
'SubgroupShuffleINTEL',
'SubgroupShuffleINTEL',
'SubgroupBufferBlockIOINTEL',
'SubgroupImageBlockIOINTEL',
'SubgroupImageMediaBlockIOINTEL',
'RoundToInfinityINTEL',
'FloatingPointModeINTEL',
'IndirectReferencesINTEL',
'AsmINTEL',
'VectorComputeINTEL',
'VectorAnyINTEL',
'SubgroupAvcMotionEstimationINTEL',
'SubgroupAvcMotionEstimationIntraINTEL',
'SubgroupAvcMotionEstimationChromaINTEL',
'VariableLengthArrayINTEL',
'FunctionFloatControlINTEL',
'FPGAMemoryAttributesINTEL',
'FPFastMathModeINTEL',
'ArbitraryPrecisionIntegersINTEL',
'ArbitraryPrecisionFloatingPointINTEL',
'UnstructuredLoopControlsINTEL',
'FPGALoopControlsINTEL',
'KernelAttributesINTEL',
'FPGAKernelAttributesINTEL',
'FPGAMemoryAccessesINTEL',
'FPGAClusterAttributesINTEL',
'LoopFuseINTEL',
'FPGADSPControlINTEL',
'MemoryAccessAliasingINTEL',
'FPGAInvocationPipeliningAttributesINTEL',
'FPGABufferLocationINTEL',
'ArbitraryPrecisionFixedPointINTEL',
'USMStorageClassesINTEL',
'RuntimeAlignedAttributeINTEL',
'IOPipesINTEL',
'BlockingPipesINTEL',
'FPGARegINTEL',
'LongCompositesINTEL',
'OptNoneINTEL',
'DebugInfoModuleINTEL',
'BFloat16ConversionINTEL',
'SplitBarrierINTEL',
'FPGAClusterAttributesV2INTEL',
'FPGAKernelAttributesv2INTEL',
'FPMaxErrorINTEL',
'FPGALatencyControlINTEL',
'FPGAArgumentInterfacesINTEL',
'GlobalVariableHostAccessINTEL',
'GlobalVariableFPGADecorationsINTEL',
'MaskedGatherScatterINTEL',
'CacheControlsINTEL',
'RegisterLimitsINTEL'
]

View file

@ -0,0 +1,797 @@
#!/usr/bin/python3 -i
#
# Copyright (c) 2015-2024 The Khronos Group Inc.
# Copyright (c) 2015-2024 Valve Corporation
# Copyright (c) 2015-2024 LunarG, Inc.
# Copyright (c) 2015-2024 Google Inc.
# Copyright (c) 2023-2024 RasterGrid Kft.
#
# SPDX-License-Identifier: Apache-2.0
import os
import re
from generators.vulkan_object import Struct, Member
from generators.base_generator import BaseGenerator
from generators.generator_utils import PlatformGuardHelper
class SafeStructOutputGenerator(BaseGenerator):
def __init__(self):
BaseGenerator.__init__(self)
# Will skip completly, mainly used for things that have no one consuming the safe struct
self.no_autogen = [
# These WSI struct have nothing deep to copy, except the WSI releated field which we can't make a copy
'VkXlibSurfaceCreateInfoKHR',
'VkXcbSurfaceCreateInfoKHR',
'VkWaylandSurfaceCreateInfoKHR',
'VkAndroidSurfaceCreateInfoKHR',
'VkWin32SurfaceCreateInfoKHR',
'VkIOSSurfaceCreateInfoMVK',
'VkMacOSSurfaceCreateInfoMVK',
'VkMetalSurfaceCreateInfoEXT'
]
# Will skip generating the source logic (to be implemented in vk_safe_struct_manual.cpp)
# The header will still be generated
self.manual_source = [
# This needs to know if we're doing a host or device build, logic becomes complex and very specialized
'VkAccelerationStructureBuildGeometryInfoKHR',
'VkAccelerationStructureGeometryKHR',
# Have a pUsageCounts and ppUsageCounts that is not currently handled in the generated code
'VkMicromapBuildInfoEXT',
'VkAccelerationStructureTrianglesOpacityMicromapEXT',
'VkAccelerationStructureTrianglesDisplacementMicromapNV',
# The VkDescriptorType field needs to handle every type which is something best done manually
'VkDescriptorDataEXT',
# Special case because its pointers may be non-null but ignored
'VkGraphicsPipelineCreateInfo',
# Special case because it has custom construct parameters
'VkPipelineViewportStateCreateInfo',
]
# For abstract types just want to save the pointer away
# since we cannot make a copy.
self.abstract_types = [
'AHardwareBuffer',
]
# These 'data' union are decided by the 'type' in the same parent struct
self.union_of_pointers = [
'VkDescriptorDataEXT',
]
self.union_of_pointer_callers = [
'VkDescriptorGetInfoEXT',
]
# Will update the the function interface
self.custom_construct_params = {
# vku::safe::GraphicsPipelineCreateInfo needs to know if subpass has color and\or depth\stencil attachments to use its pointers
'VkGraphicsPipelineCreateInfo' :
', const bool uses_color_attachment, const bool uses_depthstencil_attachment',
# vku::safe::PipelineViewportStateCreateInfo needs to know if viewport and scissor is dynamic to use its pointers
'VkPipelineViewportStateCreateInfo' :
', const bool is_dynamic_viewports, const bool is_dynamic_scissors',
# vku::safe::AccelerationStructureBuildGeometryInfoKHR needs to know if we're doing a host or device build
'VkAccelerationStructureBuildGeometryInfoKHR' :
', const bool is_host, const VkAccelerationStructureBuildRangeInfoKHR *build_range_infos',
# vku::safe::AccelerationStructureGeometryKHR needs to know if we're doing a host or device build
'VkAccelerationStructureGeometryKHR' :
', const bool is_host, const VkAccelerationStructureBuildRangeInfoKHR *build_range_info',
# vku::safe::DescriptorDataEXT needs to know what field of union is intialized
'VkDescriptorDataEXT' :
', const VkDescriptorType type',
}
# Determine if a structure needs a safe_struct helper function
# That is, it has an sType or one of its members is a pointer
def needSafeStruct(self, struct: Struct) -> bool:
if struct.name in self.no_autogen:
return False
if 'VkBase' in struct.name:
return False # Ingore structs like VkBaseOutStructure
if struct.sType is not None:
return True
for member in struct.members:
if member.pointer:
return True
return False
def containsObjectHandle(self, member: Member) -> bool:
if member.type in self.vk.handles:
return True
if member.type in self.vk.structs:
for subMember in self.vk.structs[member.type].members:
if self.containsObjectHandle(subMember):
return True
return False
def typeContainsObjectHandle(self, handle_type: str, dispatchable: bool) -> bool:
if handle_type in self.vk.handles:
if dispatchable == self.vk.handles[handle_type].dispatchable:
return True
# if handle_type is a struct, search its members
if handle_type in self.vk.structs:
struct = self.vk.structs[handle_type]
for member in [x for x in struct.members if x.type in self.vk.handles]:
if dispatchable == self.vk.handles[member.type].dispatchable:
return True
return False
def generate(self):
self.write(f'''// *** THIS FILE IS GENERATED - DO NOT EDIT ***
// See {os.path.basename(__file__)} for modifications
/***************************************************************************
*
* Copyright (c) 2015-2024 The Khronos Group Inc.
* Copyright (c) 2015-2024 Valve Corporation
* Copyright (c) 2015-2024 LunarG, Inc.
* Copyright (c) 2015-2024 Google Inc.
*
* SPDX-License-Identifier: Apache-2.0
*
****************************************************************************/\n''')
self.write('// NOLINTBEGIN') # Wrap for clang-tidy to ignore
if self.filename == 'vk_safe_struct.hpp':
self.generateHeader()
elif self.filename == 'vk_safe_struct_utils.cpp':
self.generateUtil()
elif self.filename.startswith('vk_safe_struct_'):
self.generateSource()
else:
self.write(f'\nFile name {self.filename} has no code to generate\n')
self.write('// NOLINTEND') # Wrap for clang-tidy to ignore
def convertName(self, vk_name):
return "safe_" + vk_name
def generateHeader(self):
out = []
out.append('''
#pragma once
#include <vulkan/vulkan.h>
#include <algorithm>
#include <vulkan/utility/vk_safe_struct_utils.hpp>
namespace vku {
\n''')
guard_helper = PlatformGuardHelper()
for struct in [x for x in self.vk.structs.values() if self.needSafeStruct(x)]:
safe_name = self.convertName(struct.name)
out.extend(guard_helper.add_guard(struct.protect))
out.append(f'{"union" if struct.union else "struct"} {safe_name} {{\n')
# Can only initialize first member of an Union
canInitialize = True
copy_pnext = ', bool copy_pnext = true' if struct.sType is not None else ''
for member in struct.members:
if member.type in self.vk.structs:
if self.needSafeStruct(self.vk.structs[member.type]):
safe_member_type = self.convertName(member.type)
if member.pointer:
pointer = '*' * member.cDeclaration.count('*')
brackets = '' if struct.union else '{}'
out.append(f'{safe_member_type}{pointer} {member.name}{brackets};\n')
else:
out.append(f'{safe_member_type} {member.name};\n')
continue
explicitInitialize = member.pointer and 'PFN_' not in member.type and canInitialize
initialize = '{}' if explicitInitialize else ''
# Prevents union from initializing agian
canInitialize = not struct.union if explicitInitialize else canInitialize
if member.length and self.containsObjectHandle(member) and not member.fixedSizeArray:
out.append(f' {member.type}* {member.name}{initialize};\n')
else:
out.append(f'{member.cDeclaration}{initialize};\n')
if (struct.name == 'VkDescriptorDataEXT'):
out.append('char type_at_end[sizeof(VkDescriptorDataEXT)+sizeof(VkDescriptorGetInfoEXT::type)];')
constructParam = self.custom_construct_params.get(struct.name, '')
out.append(f'''
{safe_name}(const {struct.name}* in_struct{constructParam}, PNextCopyState* copy_state = {{}}{copy_pnext});
{safe_name}(const {safe_name}& copy_src);
{safe_name}& operator=(const {safe_name}& copy_src);
{safe_name}();
~{safe_name}();
void initialize(const {struct.name}* in_struct{constructParam}, PNextCopyState* copy_state = {{}});
void initialize(const {safe_name}* copy_src, PNextCopyState* copy_state = {{}});
{struct.name} *ptr() {{ return reinterpret_cast<{struct.name} *>(this); }}
{struct.name} const *ptr() const {{ return reinterpret_cast<{struct.name} const *>(this); }}
''')
if struct.name == 'VkShaderModuleCreateInfo':
out.append('''
// Primarily intended for use by GPUAV when replacing shader module code with instrumented code
template<typename Container>
void SetCode(const Container &code) {
delete[] pCode;
codeSize = static_cast<uint32_t>(code.size() * sizeof(uint32_t));
pCode = new uint32_t[code.size()];
std::copy(&code.front(), &code.back() + 1, const_cast<uint32_t*>(pCode));
}
''')
out.append('};\n')
out.extend(guard_helper.add_guard(None))
out.append('''
// Safe struct that spans NV and KHR VkRayTracingPipelineCreateInfo structures.
// It is a VkRayTracingPipelineCreateInfoKHR and supports construction from
// a VkRayTracingPipelineCreateInfoNV.
class safe_VkRayTracingPipelineCreateInfoCommon : public safe_VkRayTracingPipelineCreateInfoKHR {
public:
safe_VkRayTracingPipelineCreateInfoCommon() : safe_VkRayTracingPipelineCreateInfoKHR() {}
safe_VkRayTracingPipelineCreateInfoCommon(const VkRayTracingPipelineCreateInfoNV *pCreateInfo)
: safe_VkRayTracingPipelineCreateInfoKHR() {
initialize(pCreateInfo);
}
safe_VkRayTracingPipelineCreateInfoCommon(const VkRayTracingPipelineCreateInfoKHR *pCreateInfo)
: safe_VkRayTracingPipelineCreateInfoKHR(pCreateInfo) {}
void initialize(const VkRayTracingPipelineCreateInfoNV *pCreateInfo);
void initialize(const VkRayTracingPipelineCreateInfoKHR *pCreateInfo);
uint32_t maxRecursionDepth = 0; // NV specific
};
''')
out.append('''
} // namespace vku
''')
self.write("".join(out))
def generateUtil(self):
out = []
out.append('''
#include <vulkan/vk_layer.h>
#include <vulkan/utility/vk_safe_struct.hpp>
#include <vector>
#include <cstring>
extern std::vector<std::pair<uint32_t, uint32_t>> custom_stype_info;
namespace vku {
char *SafeStringCopy(const char *in_string) {
if (nullptr == in_string) return nullptr;
size_t len = std::strlen(in_string);
char* dest = new char[len + 1];
dest[len] = '\\0';
std::memcpy(dest, in_string, len);
return dest;
}
''')
out.append('''
// clang-format off
void *SafePnextCopy(const void *pNext, PNextCopyState* copy_state) {
void *first_pNext{};
VkBaseOutStructure *prev_pNext{};
void *safe_pNext{};
while (pNext) {
const VkBaseOutStructure *header = reinterpret_cast<const VkBaseOutStructure *>(pNext);
switch (header->sType) {
// Add special-case code to copy beloved secret loader structs
// Special-case Loader Instance Struct passed to/from layer in pNext chain
case VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO: {
VkLayerInstanceCreateInfo *struct_copy = new VkLayerInstanceCreateInfo;
// TODO: Uses original VkLayerInstanceLink* chain, which should be okay for our uses
memcpy(struct_copy, pNext, sizeof(VkLayerInstanceCreateInfo));
safe_pNext = struct_copy;
break;
}
// Special-case Loader Device Struct passed to/from layer in pNext chain
case VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO: {
VkLayerDeviceCreateInfo *struct_copy = new VkLayerDeviceCreateInfo;
// TODO: Uses original VkLayerDeviceLink*, which should be okay for our uses
memcpy(struct_copy, pNext, sizeof(VkLayerDeviceCreateInfo));
safe_pNext = struct_copy;
break;
}
''')
guard_helper = PlatformGuardHelper()
for struct in [x for x in self.vk.structs.values() if x.extends]:
safe_name = self.convertName(struct.name)
out.extend(guard_helper.add_guard(struct.protect))
out.append(f' case {struct.sType}:\n')
out.append(f' safe_pNext = new {safe_name}(reinterpret_cast<const {struct.name} *>(pNext), copy_state, false);\n')
out.append(' break;\n')
out.extend(guard_helper.add_guard(None))
out.append('''
default: // Encountered an unknown sType -- skip (do not copy) this entry in the chain
// If sType is in custom list, construct blind copy
for (auto item : custom_stype_info) {
if (item.first == static_cast<uint32_t>(header->sType)) {
safe_pNext = malloc(item.second);
memcpy(safe_pNext, header, item.second);
}
}
break;
}
if (!first_pNext) {
first_pNext = safe_pNext;
}
pNext = header->pNext;
if (prev_pNext && safe_pNext) {
prev_pNext->pNext = (VkBaseOutStructure*)safe_pNext;
}
if (safe_pNext) {
prev_pNext = (VkBaseOutStructure*)safe_pNext;
}
safe_pNext = nullptr;
}
return first_pNext;
}
void FreePnextChain(const void *pNext) {
if (!pNext) return;
auto header = reinterpret_cast<const VkBaseOutStructure *>(pNext);
switch (header->sType) {
// Special-case Loader Instance Struct passed to/from layer in pNext chain
case VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO:
FreePnextChain(header->pNext);
delete reinterpret_cast<const VkLayerInstanceCreateInfo *>(pNext);
break;
// Special-case Loader Device Struct passed to/from layer in pNext chain
case VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO:
FreePnextChain(header->pNext);
delete reinterpret_cast<const VkLayerDeviceCreateInfo *>(pNext);
break;
''')
for struct in [x for x in self.vk.structs.values() if x.extends]:
safe_name = self.convertName(struct.name)
out.extend(guard_helper.add_guard(struct.protect))
out.append(f' case {struct.sType}:\n')
out.append(f' delete reinterpret_cast<const {safe_name} *>(header);\n')
out.append(' break;\n')
out.extend(guard_helper.add_guard(None))
out.append('''
default: // Encountered an unknown sType
// If sType is in custom list, free custom struct memory and clean up
for (auto item : custom_stype_info) {
if (item.first == static_cast<uint32_t>(header->sType)) {
if (header->pNext) {
FreePnextChain(header->pNext);
}
free(const_cast<void *>(pNext));
pNext = nullptr;
break;
}
}
if (pNext) {
FreePnextChain(header->pNext);
}
break;
}
}''')
out.append('// clang-format on\n')
out.append('''
} // namespace vku
''')
self.write("".join(out))
def generateSource(self):
out = []
out.append('''
#include <vulkan/utility/vk_safe_struct.hpp>
#include <vulkan/utility/vk_struct_helper.hpp>
#include <cstring>
namespace vku {
''')
custom_defeault_construct_txt = {}
custom_construct_txt = {
# VkWriteDescriptorSet is special case because pointers may be non-null but ignored
'VkWriteDescriptorSet' : '''
switch (descriptorType) {
case VK_DESCRIPTOR_TYPE_SAMPLER:
case VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER:
case VK_DESCRIPTOR_TYPE_SAMPLED_IMAGE:
case VK_DESCRIPTOR_TYPE_STORAGE_IMAGE:
case VK_DESCRIPTOR_TYPE_INPUT_ATTACHMENT:
case VK_DESCRIPTOR_TYPE_BLOCK_MATCH_IMAGE_QCOM:
case VK_DESCRIPTOR_TYPE_SAMPLE_WEIGHT_IMAGE_QCOM:
if (descriptorCount && in_struct->pImageInfo) {
pImageInfo = new VkDescriptorImageInfo[descriptorCount];
for (uint32_t i = 0; i < descriptorCount; ++i) {
pImageInfo[i] = in_struct->pImageInfo[i];
}
}
break;
case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER:
case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER:
case VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC:
case VK_DESCRIPTOR_TYPE_STORAGE_BUFFER_DYNAMIC:
if (descriptorCount && in_struct->pBufferInfo) {
pBufferInfo = new VkDescriptorBufferInfo[descriptorCount];
for (uint32_t i = 0; i < descriptorCount; ++i) {
pBufferInfo[i] = in_struct->pBufferInfo[i];
}
}
break;
case VK_DESCRIPTOR_TYPE_UNIFORM_TEXEL_BUFFER:
case VK_DESCRIPTOR_TYPE_STORAGE_TEXEL_BUFFER:
if (descriptorCount && in_struct->pTexelBufferView) {
pTexelBufferView = new VkBufferView[descriptorCount];
for (uint32_t i = 0; i < descriptorCount; ++i) {
pTexelBufferView[i] = in_struct->pTexelBufferView[i];
}
}
break;
default:
break;
}
''',
'VkShaderModuleCreateInfo' : '''
if (in_struct->pCode) {
pCode = reinterpret_cast<uint32_t *>(new uint8_t[codeSize]);
memcpy((void *)pCode, (void *)in_struct->pCode, codeSize);
}
''',
# VkFrameBufferCreateInfo is special case because its pAttachments pointer may be non-null but ignored
'VkFramebufferCreateInfo' : '''
if (attachmentCount && in_struct->pAttachments && !(flags & VK_FRAMEBUFFER_CREATE_IMAGELESS_BIT)) {
pAttachments = new VkImageView[attachmentCount];
for (uint32_t i = 0; i < attachmentCount; ++i) {
pAttachments[i] = in_struct->pAttachments[i];
}
}
''',
# VkDescriptorSetLayoutBinding is special case because its pImmutableSamplers pointer may be non-null but ignored
'VkDescriptorSetLayoutBinding' : '''
const bool sampler_type = in_struct->descriptorType == VK_DESCRIPTOR_TYPE_SAMPLER || in_struct->descriptorType == VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER;
if (descriptorCount && in_struct->pImmutableSamplers && sampler_type) {
pImmutableSamplers = new VkSampler[descriptorCount];
for (uint32_t i = 0; i < descriptorCount; ++i) {
pImmutableSamplers[i] = in_struct->pImmutableSamplers[i];
}
}
''',
'VkPipelineRenderingCreateInfo': '''
bool custom_init = copy_state && copy_state->init;
if (custom_init) {
custom_init = copy_state->init(reinterpret_cast<VkBaseOutStructure*>(this), reinterpret_cast<const VkBaseOutStructure*>(in_struct));
}
if (!custom_init) {
// The custom iniitalization was not used, so do the regular initialization
if (in_struct->pColorAttachmentFormats) {
pColorAttachmentFormats = new VkFormat[in_struct->colorAttachmentCount];
memcpy ((void *)pColorAttachmentFormats, (void *)in_struct->pColorAttachmentFormats, sizeof(VkFormat)*in_struct->colorAttachmentCount);
}
}
''',
# TODO: VkPushDescriptorSetWithTemplateInfoKHR needs a custom constructor to handle pData
# https://github.com/KhronosGroup/Vulkan-ValidationLayers/issues/7169
'VkPushDescriptorSetWithTemplateInfoKHR': '''
''',
}
custom_copy_txt = {
'VkFramebufferCreateInfo' : '''
pNext = SafePnextCopy(copy_src.pNext);
if (attachmentCount && copy_src.pAttachments && !(flags & VK_FRAMEBUFFER_CREATE_IMAGELESS_BIT)) {
pAttachments = new VkImageView[attachmentCount];
for (uint32_t i = 0; i < attachmentCount; ++i) {
pAttachments[i] = copy_src.pAttachments[i];
}
}
''',
'VkPipelineRenderingCreateInfo': '''
if (copy_src.pColorAttachmentFormats) {
pColorAttachmentFormats = new VkFormat[copy_src.colorAttachmentCount];
memcpy ((void *)pColorAttachmentFormats, (void *)copy_src.pColorAttachmentFormats, sizeof(VkFormat)*copy_src.colorAttachmentCount);
}
'''
}
custom_destruct_txt = {
'VkShaderModuleCreateInfo' : '''
if (pCode)
delete[] reinterpret_cast<const uint8_t *>(pCode);
''',
}
member_init_transforms = {
'queueFamilyIndexCount': lambda m: f'{m.name}(0)'
}
def qfi_construct(item, member):
true_index_setter = lambda i: f'{i}queueFamilyIndexCount = in_struct->queueFamilyIndexCount;\n'
false_index_setter = lambda i: f'{i}queueFamilyIndexCount = 0;\n'
if item.name == 'VkSwapchainCreateInfoKHR':
return (f'(in_struct->imageSharingMode == VK_SHARING_MODE_CONCURRENT) && in_struct->{member.name}', true_index_setter, false_index_setter)
else:
return (f'(in_struct->sharingMode == VK_SHARING_MODE_CONCURRENT) && in_struct->{member.name}', true_index_setter, false_index_setter)
# map of:
# <member name>: function(item, member) -> (condition, true statement, false statement)
member_construct_conditions = {
'pQueueFamilyIndices': qfi_construct
}
# Find what types of safe structs need to be generated based on output file name
splitRegex = r'.*';
if self.filename.endswith('_khr.cpp'):
splitRegex = r'.*KHR$'
elif self.filename.endswith('_ext.cpp'):
splitRegex = r'.*EXT$'
elif self.filename.endswith('_vendor.cpp'):
splitRegex = r'^(?!.*(KHR|EXT)$).*[A-Z]$' # Matches all words finishing with an upper case letter, but not ending with KHRor EXT
else: # elif self.filename.endswith('_core.cpp'):
splitRegex = r'.*[a-z0-9]$'
guard_helper = PlatformGuardHelper()
for struct in [x for x in self.vk.structs.values() if self.needSafeStruct(x) and x.name not in self.manual_source and re.match(splitRegex, x.name)]:
out.extend(guard_helper.add_guard(struct.protect))
init_list = '' # list of members in struct constructor initializer
default_init_list = '' # Default constructor just inits ptrs to nullptr in initializer
init_func_txt = '' # Txt for initialize() function that takes struct ptr and inits members
construct_txt = '' # Body of constuctor as well as body of initialize() func following init_func_txt
destruct_txt = ''
has_pnext = struct.sType is not None
copy_pnext = ''
copy_pnext_if = ''
copy_strings = ''
for member in struct.members:
m_type = member.type
m_type_safe = False
if member.name == 'pNext':
copy_pnext = 'pNext = SafePnextCopy(in_struct->pNext, copy_state);\n'
copy_pnext_if = '''
if (copy_pnext) {
pNext = SafePnextCopy(in_struct->pNext, copy_state);
}'''
if member.type in self.vk.structs and self.needSafeStruct(self.vk.structs[member.type]):
m_type = self.convertName(member.type)
m_type_safe = True;
if member.pointer and not m_type_safe and 'PFN_' not in member.type and not self.typeContainsObjectHandle(member.type, False):
# Ptr types w/o a safe_struct, for non-null case need to allocate new ptr and copy data in
if m_type in ['void', 'char']:
if member.name != 'pNext':
if m_type == 'char':
# Create deep copies of strings
if member.length:
copy_strings += f'''
char **tmp_{member.name} = new char *[in_struct->{member.length}];
for (uint32_t i = 0; i < {member.length}; ++i) {{
tmp_{member.name}[i] = SafeStringCopy(in_struct->{member.name}[i]);
}}
{member.name} = tmp_{member.name};'''
destruct_txt += f'''
if ({member.name}) {{
for (uint32_t i = 0; i < {member.length}; ++i) {{
delete [] {member.name}[i];
}}
delete [] {member.name};
}}'''
else:
copy_strings += f'{member.name} = SafeStringCopy(in_struct->{member.name});\n'
destruct_txt += f'if ({member.name}) delete [] {member.name};\n'
else:
# We need a deep copy of pData / dataSize combos
if member.name == 'pData':
init_list += f'\n {member.name}(nullptr),'
construct_txt += '''
if (in_struct->pData != nullptr) {
auto temp = new std::byte[in_struct->dataSize];
std::memcpy(temp, in_struct->pData, in_struct->dataSize);
pData = temp;
}
'''
destruct_txt += '''
if (pData != nullptr) {
auto temp = reinterpret_cast<const std::byte*>(pData);
delete [] temp;
}
'''
else:
init_list += f'\n{member.name}(in_struct->{member.name}),'
init_func_txt += f'{member.name} = in_struct->{member.name};\n'
default_init_list += f'\n{member.name}(nullptr),'
else:
default_init_list += f'\n{member.name}(nullptr),'
init_list += f'\n{member.name}(nullptr),'
if m_type in self.abstract_types:
construct_txt += f'{member.name} = in_struct->{member.name};\n'
else:
init_func_txt += f'{member.name} = nullptr;\n'
if not member.fixedSizeArray and (member.length is None or '/' in member.length):
construct_txt += f'''
if (in_struct->{member.name}) {{
{member.name} = new {m_type}(*in_struct->{member.name});
}}
'''
destruct_txt += f'if ({member.name})\n'
destruct_txt += f' delete {member.name};\n'
else:
# Prepend struct members with struct name
decorated_length = member.length
for other_member in struct.members:
decorated_length = re.sub(r'\b({})\b'.format(other_member.name), r'in_struct->\1', decorated_length)
try:
concurrent_clause = member_construct_conditions[member.name](struct, member)
except:
concurrent_clause = (f'in_struct->{member.name}', lambda x: '')
construct_txt += f'''
if ({concurrent_clause[0]}) {{
{member.name} = new {m_type}[{decorated_length}];
memcpy ((void *){member.name}, (void *)in_struct->{member.name}, sizeof({m_type})*{decorated_length});
{concurrent_clause[1](' ')}'''
if len(concurrent_clause) > 2:
construct_txt += '} else {\n'
construct_txt += concurrent_clause[2](' ')
construct_txt += '}\n'
destruct_txt += f'if ({member.name})\n'
destruct_txt += f' delete[] {member.name};\n'
elif member.fixedSizeArray or member.length is not None:
if member.fixedSizeArray:
construct_txt += f'''
for (uint32_t i = 0; i < {member.fixedSizeArray[0]}; ++i) {{
{member.name}[i] = in_struct->{member.name}[i];
}}
'''
else:
# Init array ptr to NULL
default_init_list += f'\n{member.name}(nullptr),'
init_list += f'\n{member.name}(nullptr),'
init_func_txt += f'{member.name} = nullptr;\n'
array_element = f'in_struct->{member.name}[i]'
if member.type in self.vk.structs and self.needSafeStruct(self.vk.structs[member.type]):
array_element = f'{member.type}(&in_struct->safe_{member.name}[i])'
construct_txt += f'if ({member.length} && in_struct->{member.name}) {{\n'
construct_txt += f' {member.name} = new {m_type}[{member.length}];\n'
destruct_txt += f'if ({member.name})\n'
destruct_txt += f' delete[] {member.name};\n'
construct_txt += f'for (uint32_t i = 0; i < {member.length}; ++i) {{\n'
if m_type_safe:
construct_txt += f'{member.name}[i].initialize(&in_struct->{member.name}[i]);\n'
else:
construct_txt += f'{member.name}[i] = {array_element};\n'
construct_txt += '}\n'
construct_txt += '}\n'
elif member.pointer and 'PFN_' not in member.type:
default_init_list += f'\n{member.name}(nullptr),'
init_list += f'\n{member.name}(nullptr),'
init_func_txt += f'{member.name} = nullptr;\n'
construct_txt += f'if (in_struct->{member.name})\n'
construct_txt += f' {member.name} = new {m_type}(in_struct->{member.name});\n'
destruct_txt += f'if ({member.name})\n'
destruct_txt += f' delete {member.name};\n'
elif m_type_safe and member.type in self.union_of_pointers:
init_list += f'\n{member.name}(&in_struct->{member.name}, in_struct->type),'
init_func_txt += f'{member.name}.initialize(&in_struct->{member.name}, in_struct->type);\n'
elif m_type_safe:
init_list += f'\n{member.name}(&in_struct->{member.name}),'
init_func_txt += f'{member.name}.initialize(&in_struct->{member.name});\n'
else:
try:
init_list += f'\n{member_init_transforms[member.name](member)},'
except:
init_list += f'\n{member.name}(in_struct->{member.name}),'
init_func_txt += f'{member.name} = in_struct->{member.name};\n'
if not struct.union:
if member.name == 'sType' and struct.sType:
default_init_list += f'\n{member.name}({struct.sType}),'
else:
default_init_list += f'\n{member.name}(),'
if '' != init_list:
init_list = init_list[:-1] # hack off final comma
if struct.name in custom_construct_txt:
construct_txt = custom_construct_txt[struct.name]
construct_txt = copy_strings + construct_txt
if struct.name in custom_destruct_txt:
destruct_txt = custom_destruct_txt[struct.name]
copy_pnext_param = ''
if has_pnext:
copy_pnext_param = ', bool copy_pnext'
destruct_txt += ' FreePnextChain(pNext);\n'
safe_name = self.convertName(struct.name)
if struct.union:
if struct.name in self.union_of_pointers:
default_init_list = ' type_at_end {0},'
out.append(f'''
{safe_name}::{safe_name}(const {struct.name}* in_struct{self.custom_construct_params.get(struct.name, '')}, [[maybe_unused]] PNextCopyState* copy_state{copy_pnext_param})
{{
{copy_pnext + construct_txt}}}
''')
else:
# Unions don't allow multiple members in the initialization list, so just call initialize
out.append(f'''
{safe_name}::{safe_name}(const {struct.name}* in_struct{self.custom_construct_params.get(struct.name, '')}, PNextCopyState*)
{{
initialize(in_struct);
}}
''')
else:
out.append(f'''
{safe_name}::{safe_name}(const {struct.name}* in_struct{self.custom_construct_params.get(struct.name, '')}, [[maybe_unused]] PNextCopyState* copy_state{copy_pnext_param}) :{init_list}
{{
{copy_pnext_if + construct_txt}}}
''')
if '' != default_init_list:
default_init_list = f' :{default_init_list[:-1]}'
default_init_body = '\n' + custom_defeault_construct_txt[struct.name] if struct.name in custom_defeault_construct_txt else ''
out.append(f'''
{safe_name}::{safe_name}(){default_init_list}
{{{default_init_body}}}
''')
# Create slight variation of init and construct txt for copy constructor that takes a copy_src object reference vs. struct ptr
construct_txt = copy_pnext + construct_txt
copy_construct_init = init_func_txt.replace('in_struct->', 'copy_src.')
copy_construct_init = copy_construct_init.replace(', copy_state', '')
if struct.name in self.union_of_pointer_callers:
copy_construct_init = copy_construct_init.replace(', copy_src.type', '')
# Pass object to copy constructors
copy_construct_txt = re.sub('(new \\w+)\\(in_struct->', '\\1(*copy_src.', construct_txt)
# Modify remaining struct refs for copy_src object
copy_construct_txt = copy_construct_txt.replace('in_struct->', 'copy_src.')
# Modify remaining struct refs for copy_src object
copy_construct_txt = copy_construct_txt .replace(', copy_state', '')
if struct.name in custom_copy_txt:
copy_construct_txt = custom_copy_txt[struct.name]
copy_assign_txt = ' if (&copy_src == this) return *this;\n\n' + destruct_txt + '\n' + copy_construct_init + copy_construct_txt + '\n return *this;'
# Copy constructor
out.append(f'''
{safe_name}::{safe_name}(const {safe_name}& copy_src)
{{
{copy_construct_init}{copy_construct_txt}}}
''')
# Copy assignment operator
out.append(f'''
{safe_name}& {safe_name}::operator=(const {safe_name}& copy_src)\n{{
{copy_assign_txt}
}}
''')
out.append(f'''
{safe_name}::~{safe_name}()
{{
{destruct_txt}}}
''')
out.append(f'''
void {safe_name}::initialize(const {struct.name}* in_struct{self.custom_construct_params.get(struct.name, '')}, [[maybe_unused]] PNextCopyState* copy_state)
{{
{destruct_txt}{init_func_txt}{construct_txt}}}
''')
# Copy initializer uses same txt as copy constructor but has a ptr and not a reference
init_copy = copy_construct_init.replace('copy_src.', 'copy_src->')
# Replace '&copy_src' with 'copy_src' unless it's followed by a dereference
init_copy = re.sub(r'&copy_src(?!->)', 'copy_src', init_copy)
init_construct = copy_construct_txt.replace('copy_src.', 'copy_src->')
# Replace '&copy_src' with 'copy_src' unless it's followed by a dereference
init_construct = re.sub(r'&copy_src(?!->)', 'copy_src', init_construct)
out.append(f'''
void {safe_name}::initialize(const {safe_name}* copy_src, [[maybe_unused]] PNextCopyState* copy_state)
{{
{init_copy}{init_construct}}}
''')
out.extend(guard_helper.add_guard(None))
out.append('''
} // namespace vku
''')
self.write("".join(out))

View file

@ -53,9 +53,9 @@ VkStructureType GetSType() {
guard_helper = PlatformGuardHelper()
for struct in [x for x in self.vk.structs.values() if x.sType]:
out.extend(guard_helper.addGuard(struct.protect))
out.extend(guard_helper.add_guard(struct.protect))
out.append(f'template <> inline VkStructureType GetSType<{struct.name}>() {{ return {struct.sType}; }}\n')
out.extend(guard_helper.addGuard(None))
out.extend(guard_helper.add_guard(None))
out.append('''
// Find an entry of the given type in the const pNext chain
// returns nullptr if the entry is not found
@ -132,9 +132,9 @@ template<typename T> VkObjectType GetObjectType() {
#if VK_USE_64_BIT_PTR_DEFINES == 1
''')
for handle in self.vk.handles.values():
out.extend(guard_helper.addGuard(handle.protect))
out.extend(guard_helper.add_guard(handle.protect))
out.append(f'template<> inline VkObjectType GetObjectType<{handle.name}>() {{ return {handle.type}; }}\n')
out.extend(guard_helper.addGuard(None))
out.extend(guard_helper.add_guard(None))
out.append('''
#endif // VK_USE_64_BIT_PTR_DEFINES == 1
} // namespace vku

View file

@ -1,8 +1,7 @@
#!/usr/bin/python3 -i
#
# Copyright 2023 The Khronos Group Inc.
# Copyright 2023 Valve Corporation
# Copyright 2023 LunarG, Inc.
# Copyright (c) 2023-2024 Valve Corporation
# Copyright (c) 2023-2024 LunarG, Inc.
#
# SPDX-License-Identifier: Apache-2.0
@ -33,7 +32,7 @@ class Extension:
# Quotes allow us to forward declare the dataclass
commands: list['Command'] = field(default_factory=list, init=False)
enums: list['Enum'] = field(default_factory=list, init=False)
bitmask: list['Bitmask'] = field(default_factory=list, init=False)
bitmasks: list['Bitmask'] = field(default_factory=list, init=False)
# Use the Enum name to see what fields are extended
enumFields: dict[str, list['EnumField']] = field(default_factory=dict, init=False)
# Use the Bitmaks name to see what flags are extended
@ -65,6 +64,9 @@ class Handle:
dispatchable: bool
def __lt__(self, other):
return self.name < other.name
@dataclass
class Param:
"""<command/param>"""
@ -92,6 +94,9 @@ class Param:
# - VkStructureType sType
cDeclaration: str
def __lt__(self, other):
return self.name < other.name
class Queues(IntFlag):
TRANSFER = auto() # VK_QUEUE_TRANSFER_BIT
GRAPHICS = auto() # VK_QUEUE_GRAPHICS_BIT
@ -153,6 +158,9 @@ class Command:
# (const VkInstanceCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkInstance* pInstance);
cFunctionPointer: str
def __lt__(self, other):
return self.name < other.name
@dataclass
class Member:
"""<member>"""
@ -179,6 +187,9 @@ class Member:
# - VkStructureType sType
cDeclaration: str
def __lt__(self, other):
return self.name < other.name
@dataclass
class Struct:
"""<type category="struct"> or <type category="union">"""
@ -200,6 +211,9 @@ class Struct:
extends: list[str] # Struct names that this struct extends
extendedBy: list[str] # Struct names that can be extended by this struct
def __lt__(self, other):
return self.name < other.name
@dataclass
class EnumField:
"""<enum> of type enum"""
@ -210,6 +224,9 @@ class EnumField:
# some fields are enabled from 2 extensions (ex) VK_DESCRIPTOR_UPDATE_TEMPLATE_TYPE_PUSH_DESCRIPTORS_KHR)
extensions: list[Extension] # None if part of 1.0 core
def __lt__(self, other):
return self.name < other.name
@dataclass
class Enum:
"""<enums> of type enum"""
@ -225,6 +242,9 @@ class Enum:
# Unique list of all extension that are involved in 'fields' (superset of 'extensions')
fieldExtensions: list[Extension]
def __lt__(self, other):
return self.name < other.name
@dataclass
class Flag:
"""<enum> of type bitmask"""
@ -236,7 +256,10 @@ class Flag:
zero: bool # if true, the value is zero (ex) VK_PIPELINE_STAGE_NONE)
# some fields are enabled from 2 extensions (ex) VK_TOOL_PURPOSE_DEBUG_REPORTING_BIT_EXT)
extensions: list[str] # None if part of 1.0 core
extensions: list[Extension] # None if part of 1.0 core
def __lt__(self, other):
return self.name < other.name
@dataclass
class Bitmask:
@ -246,12 +269,17 @@ class Bitmask:
protect: (str | None) # ex) VK_ENABLE_BETA_EXTENSIONS
bitWidth: int # 32 or 64
returnedOnly: bool
flags: list[Flag]
extensions: list[Extension] # None if part of 1.0 core
# Unique list of all extension that are involved in 'flag' (superset of 'extensions')
flagExtensions: list[Extension]
def __lt__(self, other):
return self.name < other.name
@dataclass
class FormatComponent:
"""<format/component>"""

View file

@ -1,6 +1,6 @@
// Copyright 2023 The Khronos Group Inc.
// Copyright 2023 Valve Corporation
// Copyright 2023 LunarG, Inc.
// Copyright 2023-2024 The Khronos Group Inc.
// Copyright 2023-2024 Valve Corporation
// Copyright 2023-2024 LunarG, Inc.
//
// SPDX-License-Identifier: Apache-2.0
@ -9,6 +9,9 @@
#include <vulkan/layer/vk_layer_settings.h>
#include <vulkan/layer/vk_layer_settings.hpp>
#include <vulkan/utility/vk_dispatch_table.h>
#include <vulkan/utility/vk_concurrent_unordered_map.hpp>
#include <vulkan/utility/vk_format_utils.h>
#include <vulkan/utility/vk_struct_helper.hpp>
#include <vulkan/utility/vk_safe_struct.hpp>
#include <vulkan/utility/vk_safe_struct_utils.hpp>
#include <vulkan/vk_enum_string_helper.h>

View file

@ -28,6 +28,7 @@ elseif(MSVC)
target_compile_options(VulkanCompilerConfiguration INTERFACE
/W4
/we5038 # Enable warning about MIL ordering in constructors
/wd4324 # Disable warning about alignment padding
)
# Enforce stricter ISO C++
@ -90,4 +91,7 @@ if (VUL_WERROR)
endif()
endif()
option(VUL_MOCK_ANDROID "Enable building for Android on desktop for testing with MockICD setup")
add_subdirectory(layer)
add_subdirectory(vulkan)

30
src/vulkan/CMakeLists.txt Normal file
View file

@ -0,0 +1,30 @@
# Copyright 2023 The Khronos Group Inc.
# Copyright 2023 Valve Corporation
# Copyright 2023 LunarG, Inc.
#
# SPDX-License-Identifier: Apache-2.0
set(CMAKE_FOLDER "${CMAKE_FOLDER}/VulkanSafeStruct")
add_library(VulkanSafeStruct STATIC)
add_library(Vulkan::SafeStruct ALIAS VulkanSafeStruct)
target_sources(VulkanSafeStruct PRIVATE
vk_safe_struct_core.cpp
vk_safe_struct_ext.cpp
vk_safe_struct_khr.cpp
vk_safe_struct_utils.cpp
vk_safe_struct_vendor.cpp
vk_safe_struct_manual.cpp
)
target_link_Libraries(VulkanSafeStruct
PUBLIC
Vulkan::Headers
Vulkan::UtilityHeaders
PRIVATE
Vulkan::CompilerConfiguration
)
if(VUL_MOCK_ANDROID)
target_compile_definitions(VulkanSafeStruct PUBLIC VK_USE_PLATFORM_ANDROID_KHR VUL_MOCK_ANDROID)
endif()

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

File diff suppressed because it is too large Load diff

View file

@ -16,6 +16,7 @@ target_include_directories(vul_tests PRIVATE
)
target_sources(vul_tests PRIVATE
safe_struct.cpp
struct_helper.cpp
test_formats.cpp
test_interface.cpp
@ -35,6 +36,7 @@ target_link_libraries(vul_tests PRIVATE
Vulkan::UtilityHeaders
Vulkan::LayerSettings
Vulkan::CompilerConfiguration
Vulkan::SafeStruct
)
# Test add_subdirectory suppport

239
tests/safe_struct.cpp Normal file
View file

@ -0,0 +1,239 @@
// Copyright 2023 The Khronos Group Inc.
// Copyright 2023 Valve Corporation
// Copyright 2023 LunarG, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//
#include <vulkan/utility/vk_safe_struct.hpp>
#include <vulkan/utility/vk_struct_helper.hpp>
#include <gtest/gtest.h>
#include <array>
TEST(safe_struct, basic) {
vku::safe_VkInstanceCreateInfo safe_info;
{
VkApplicationInfo app = vku::InitStructHelper();
app.pApplicationName = "test";
app.applicationVersion = 42;
VkDebugUtilsMessengerCreateInfoEXT debug_ci = vku::InitStructHelper();
debug_ci.messageSeverity = VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT;
VkInstanceCreateInfo info = vku::InitStructHelper();
info.pApplicationInfo = &app;
info.pNext = &debug_ci;
safe_info.initialize(&info);
memset(&info, 0x11, sizeof(info));
memset(&app, 0x22, sizeof(app));
memset(&debug_ci, 0x33, sizeof(debug_ci));
}
ASSERT_EQ(VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO, safe_info.sType);
ASSERT_EQ(0, strcmp("test", safe_info.pApplicationInfo->pApplicationName));
ASSERT_EQ(42, safe_info.pApplicationInfo->applicationVersion);
auto debug_ci = vku::FindStructInPNextChain<VkDebugUtilsMessengerCreateInfoEXT>(safe_info.pNext);
ASSERT_NE(nullptr, debug_ci);
ASSERT_EQ(VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT, debug_ci->messageSeverity);
}
TEST(safe_struct, safe_void_pointer_copies) {
// vku::safe_VkSpecializationInfo, constructor
{
std::vector<std::byte> data(20, std::byte{0b11110000});
VkSpecializationInfo info = {};
info.dataSize = uint32_t(data.size());
info.pData = data.data();
vku::safe_VkSpecializationInfo safe(&info);
ASSERT_TRUE(safe.pData != info.pData);
ASSERT_TRUE(safe.dataSize == info.dataSize);
data.clear(); // Invalidate any references, pointers, or iterators referring to contained elements.
auto copied_bytes = reinterpret_cast<const std::byte *>(safe.pData);
ASSERT_TRUE(copied_bytes[19] == std::byte{0b11110000});
}
// vku::safe_VkPipelineExecutableInternalRepresentationKHR, initialize
{
std::vector<std::byte> data(11, std::byte{0b01001001});
VkPipelineExecutableInternalRepresentationKHR info = {};
info.dataSize = uint32_t(data.size());
info.pData = data.data();
vku::safe_VkPipelineExecutableInternalRepresentationKHR safe;
safe.initialize(&info);
ASSERT_TRUE(safe.dataSize == info.dataSize);
ASSERT_TRUE(safe.pData != info.pData);
data.clear(); // Invalidate any references, pointers, or iterators referring to contained elements.
auto copied_bytes = reinterpret_cast<const std::byte *>(safe.pData);
ASSERT_TRUE(copied_bytes[10] == std::byte{0b01001001});
}
}
TEST(safe_struct, custom_safe_pnext_copy) {
// This tests an additional "copy_state" parameter in the SafePNextCopy function that allows "customizing" safe_* struct
// construction.. This is required for structs such as VkPipelineRenderingCreateInfo (which extend VkGraphicsPipelineCreateInfo)
// whose members must be partially ignored depending on the graphics sub-state present.
VkFormat format = VK_FORMAT_B8G8R8A8_UNORM;
VkPipelineRenderingCreateInfo pri = vku::InitStructHelper();
pri.colorAttachmentCount = 1;
pri.pColorAttachmentFormats = &format;
bool ignore_default_construction = true;
vku::PNextCopyState copy_state = {
[&ignore_default_construction](VkBaseOutStructure *safe_struct,
[[maybe_unused]] const VkBaseOutStructure *in_struct) -> bool {
if (ignore_default_construction) {
auto tmp = reinterpret_cast<vku::safe_VkPipelineRenderingCreateInfo *>(safe_struct);
tmp->colorAttachmentCount = 0;
tmp->pColorAttachmentFormats = nullptr;
return true;
}
return false;
},
};
{
VkGraphicsPipelineCreateInfo gpci = vku::InitStructHelper(&pri);
vku::safe_VkGraphicsPipelineCreateInfo safe_gpci(&gpci, false, false, &copy_state);
auto safe_pri = reinterpret_cast<const vku::safe_VkPipelineRenderingCreateInfo *>(safe_gpci.pNext);
// Ensure original input struct was not modified
ASSERT_EQ(pri.colorAttachmentCount, 1);
ASSERT_EQ(pri.pColorAttachmentFormats, &format);
// Ensure safe struct was modified
ASSERT_EQ(safe_pri->colorAttachmentCount, 0);
ASSERT_EQ(safe_pri->pColorAttachmentFormats, nullptr);
}
// Ensure PNextCopyState::init is also applied when there is more than one element in the pNext chain
{
VkGraphicsPipelineLibraryCreateInfoEXT gpl_info = vku::InitStructHelper(&pri);
VkGraphicsPipelineCreateInfo gpci = vku::InitStructHelper(&gpl_info);
vku::safe_VkGraphicsPipelineCreateInfo safe_gpci(&gpci, false, false, &copy_state);
auto safe_gpl_info = reinterpret_cast<const vku::safe_VkGraphicsPipelineLibraryCreateInfoEXT *>(safe_gpci.pNext);
auto safe_pri = reinterpret_cast<const vku::safe_VkPipelineRenderingCreateInfo *>(safe_gpl_info->pNext);
// Ensure original input struct was not modified
ASSERT_EQ(pri.colorAttachmentCount, 1);
ASSERT_EQ(pri.pColorAttachmentFormats, &format);
// Ensure safe struct was modified
ASSERT_EQ(safe_pri->colorAttachmentCount, 0);
ASSERT_EQ(safe_pri->pColorAttachmentFormats, nullptr);
}
// Check that signaling to use the default constructor works
{
pri.colorAttachmentCount = 1;
pri.pColorAttachmentFormats = &format;
ignore_default_construction = false;
VkGraphicsPipelineCreateInfo gpci = vku::InitStructHelper(&pri);
vku::safe_VkGraphicsPipelineCreateInfo safe_gpci(&gpci, false, false, &copy_state);
auto safe_pri = reinterpret_cast<const vku::safe_VkPipelineRenderingCreateInfo *>(safe_gpci.pNext);
// Ensure original input struct was not modified
ASSERT_EQ(pri.colorAttachmentCount, 1);
ASSERT_EQ(pri.pColorAttachmentFormats, &format);
// Ensure safe struct was modified
ASSERT_EQ(safe_pri->colorAttachmentCount, 1);
ASSERT_EQ(*safe_pri->pColorAttachmentFormats, format);
}
}
TEST(safe_struct, extension_add_remove) {
std::array<const char *, 6> extensions{
"VK_KHR_maintenance1", "VK_KHR_maintenance2", "VK_KHR_maintenance3",
"VK_KHR_maintenance4", "VK_KHR_maintenance5", "VK_KHR_maintenance6",
};
VkDeviceCreateInfo ci = vku::InitStructHelper();
ci.ppEnabledExtensionNames = extensions.data();
ci.enabledExtensionCount = static_cast<uint32_t>(extensions.size());
vku::safe_VkDeviceCreateInfo safe_ci(&ci);
ASSERT_EQ(3, vku::FindExtension(safe_ci, "VK_KHR_maintenance4"));
ASSERT_EQ(safe_ci.enabledExtensionCount, vku::FindExtension(safe_ci, "VK_KHR_maintenance0"));
ASSERT_EQ(false, vku::RemoveExtension(safe_ci, "VK_KHR_maintenance0"));
ASSERT_EQ(true, vku::AddExtension(safe_ci, "VK_KHR_maintenance0"));
ASSERT_EQ(true, vku::RemoveExtension(safe_ci, "VK_KHR_maintenance0"));
for (const auto &ext : extensions) {
ASSERT_EQ(true, vku::RemoveExtension(safe_ci, ext));
}
ASSERT_EQ(false, vku::RemoveExtension(safe_ci, "VK_KHR_maintenance0"));
ASSERT_EQ(0, safe_ci.enabledExtensionCount);
ASSERT_EQ(nullptr, safe_ci.ppEnabledExtensionNames);
for (const auto &ext : extensions) {
ASSERT_EQ(true, vku::AddExtension(safe_ci, ext));
}
ASSERT_EQ(extensions.size(), safe_ci.enabledExtensionCount);
}
TEST(safe_struct, pnext_add_remove) {
VkPhysicalDeviceRayTracingPipelineFeaturesKHR rtp = vku::InitStructHelper();
VkPhysicalDeviceRayQueryFeaturesKHR rtq = vku::InitStructHelper(&rtp);
VkPhysicalDeviceMeshShaderFeaturesEXT mesh = vku::InitStructHelper(&rtq);
VkPhysicalDeviceFeatures2 features = vku::InitStructHelper(&mesh);
vku::safe_VkPhysicalDeviceFeatures2 sf(&features);
// unlink the structs so they can be added one at a time.
rtp.pNext = nullptr;
rtq.pNext = nullptr;
mesh.pNext = nullptr;
ASSERT_EQ(true, vku::RemoveFromPnext(sf, rtq.sType));
ASSERT_EQ(nullptr, vku::FindStructInPNextChain<VkPhysicalDeviceRayQueryFeaturesKHR>(sf.pNext));
ASSERT_EQ(false, vku::RemoveFromPnext(sf, rtq.sType));
ASSERT_EQ(true, vku::RemoveFromPnext(sf, rtp.sType));
ASSERT_EQ(nullptr, vku::FindStructInPNextChain<VkPhysicalDeviceRayTracingPipelineFeaturesKHR>(sf.pNext));
ASSERT_EQ(true, vku::RemoveFromPnext(sf, mesh.sType));
ASSERT_EQ(nullptr, vku::FindStructInPNextChain<VkPhysicalDeviceMeshShaderFeaturesEXT>(sf.pNext));
ASSERT_EQ(nullptr, sf.pNext);
ASSERT_EQ(true, vku::AddToPnext(sf, mesh));
ASSERT_EQ(false, vku::AddToPnext(sf, mesh));
ASSERT_NE(nullptr, vku::FindStructInPNextChain<VkPhysicalDeviceMeshShaderFeaturesEXT>(sf.pNext));
ASSERT_EQ(true, vku::AddToPnext(sf, rtq));
ASSERT_EQ(false, vku::AddToPnext(sf, rtq));
ASSERT_NE(nullptr, vku::FindStructInPNextChain<VkPhysicalDeviceRayQueryFeaturesKHR>(sf.pNext));
ASSERT_EQ(true, vku::AddToPnext(sf, rtp));
ASSERT_EQ(false, vku::AddToPnext(sf, rtp));
ASSERT_NE(nullptr, vku::FindStructInPNextChain<VkPhysicalDeviceRayTracingPipelineFeaturesKHR>(sf.pNext));
ASSERT_EQ(true, vku::RemoveFromPnext(sf, mesh.sType));
ASSERT_EQ(true, vku::RemoveFromPnext(sf, rtp.sType));
ASSERT_EQ(true, vku::RemoveFromPnext(sf, rtq.sType));
// relink the structs so they can be added all at once
rtq.pNext = &rtp;
mesh.pNext = &rtq;
ASSERT_EQ(true, vku::AddToPnext(sf, mesh));
ASSERT_NE(nullptr, vku::FindStructInPNextChain<VkPhysicalDeviceRayTracingPipelineFeaturesKHR>(sf.pNext));
ASSERT_NE(nullptr, vku::FindStructInPNextChain<VkPhysicalDeviceMeshShaderFeaturesEXT>(sf.pNext));
ASSERT_NE(nullptr, vku::FindStructInPNextChain<VkPhysicalDeviceRayQueryFeaturesKHR>(sf.pNext));
}