Skip to content

Rewrite codegen to use Vulkan Object & refactor API Dump extensively #2431

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
c5ea3e0
api_dump: Enable sorting in OutputGenerator
charles-lunarg Jun 15, 2025
3885613
api_dump: Use constants directly instead of their value
charles-lunarg Jun 7, 2025
334e30a
api_dump: Make sTypes more readible
charles-lunarg Jun 7, 2025
8175334
api_dump: Remove spurious spaces in codegen
charles-lunarg Jun 7, 2025
ab61450
api_dump: Remove redundant #if defined's
charles-lunarg Jun 3, 2025
49e7421
api_dump: Remove extra spaces in generated code
charles-lunarg May 28, 2025
9d01ab9
api_dump: Remove unnecessary declarations
charles-lunarg Jun 17, 2025
bbbeece
scripts: Move api_dump to generators sub folder
charles-lunarg Jun 3, 2025
2f49658
api_dump: Rewrite codegen to use VulkanObject
charles-lunarg Jun 7, 2025
4ef3029
api_dump: Print double pointers and 2 dimentioned arrays
charles-lunarg Jun 7, 2025
e38fecd
api_dump: Simplify codegen for types & basetypes
charles-lunarg Jun 9, 2025
d21aa37
api_dump: Remove type indicator comments
charles-lunarg Jun 10, 2025
f049ca8
api_dump: Fix divergent pNext chain handling
charles-lunarg Jun 10, 2025
4f87998
api_dump: Reorganize codegen to consolidate logic
charles-lunarg Jun 9, 2025
018c455
api_dump: Pass types by reference
charles-lunarg Jun 10, 2025
5c55f91
api_dump: Generate the printing logic directly
charles-lunarg Jun 12, 2025
c7bedd7
api_dump: Consolidate logic into helper functions
charles-lunarg Jun 15, 2025
d8c79d7
api_dump: Use clang-format for formatting
charles-lunarg Jun 20, 2025
93e1e89
api_dump: Dont repeate #if defined guards
charles-lunarg Jun 20, 2025
7c30cb6
api_dump: Move handwritten code to separate file
charles-lunarg Jun 20, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ vt_sources = [
"layersvt/generated/api_dump_video_html.h",
"layersvt/generated/api_dump_video_json.h",
"layersvt/api_dump.h",
"layersvt/api_dump_handwritten_dispatch.cpp",
"layersvt/vk_layer_table.cpp",
"layersvt/vk_layer_table.h",
]
Expand Down
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ if (VT_CODEGEN)
"${VULKAN_HEADERS_INSTALL_DIR}/${CMAKE_INSTALL_DATADIR}/vulkan/registry"
--incremental --generated-version ${VulkanHeaders_VERSION}
WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/layersvt/generated
MAIN_DEPENDENCY ${CMAKE_SOURCE_DIR}/scripts/api_dump_generator.py
MAIN_DEPENDENCY ${CMAKE_SOURCE_DIR}/scripts/generators/api_dump_generator.py
)
add_custom_target(vt_codegen DEPENDS ${VT_GENERATED_FILES})
endif()
Expand Down
3 changes: 2 additions & 1 deletion layersvt/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,12 @@ if(BUILD_APIDUMP)
generated/api_dump_video_html.h
generated/api_dump_video_json.h
api_dump.h
api_dump_handwritten_dispatch.cpp
vk_layer_table.cpp
vk_layer_table.h
api_dump_layer.md
json/VkLayer_api_dump.json.in
../scripts/api_dump_generator.py
../scripts/generators/api_dump_generator.py
)

target_include_directories(VkLayer_api_dump PRIVATE
Expand Down
825 changes: 367 additions & 458 deletions layersvt/api_dump.h

Large diffs are not rendered by default.

190 changes: 190 additions & 0 deletions layersvt/api_dump_handwritten_dispatch.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
/* Copyright (c) 2015-2023 The Khronos Group Inc.
* Copyright (c) 2015-2023 Valve Corporation
* Copyright (c) 2015-2023 LunarG, Inc.
* Copyright (C) 2015-2016 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* Author: Lenny Komow <[email protected]>
* Author: Shannon McPherson <[email protected]>
* Author: David Pinedo <[email protected]>
* Author: Charles Giessen <[email protected]>
*/

// Implementation file for specifically implemented functions

#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))

#include "api_dump.h"

#include "generated/api_dump_text.h"
#include "generated/api_dump_html.h"
#include "generated/api_dump_json.h"

VKAPI_ATTR VkResult VKAPI_CALL vkCreateInstance(const VkInstanceCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkInstance* pInstance)
{
std::lock_guard<std::mutex> lg(ApiDumpInstance::current().outputMutex());
ApiDumpInstance::current().initLayerSettings(pCreateInfo, pAllocator);
if (ApiDumpInstance::current().settings().shouldPreDump() && ApiDumpInstance::current().settings().format() == ApiDumpFormat::Text) {
dump_function_head(ApiDumpInstance::current(), "vkCreateInstance", "pCreateInfo, pAllocator, pInstance");
if (ApiDumpInstance::current().shouldDumpOutput()) {
dump_text_params_vkCreateInstance(ApiDumpInstance::current(), pCreateInfo, pAllocator, pInstance);
}
dump_return_preamble(ApiDumpInstance::current(), "VkResult");
} else {
dump_function_head(ApiDumpInstance::current(), "vkCreateInstance", "pCreateInfo, pAllocator, pInstance", "VkResult");
}

// Get the function pointer
VkLayerInstanceCreateInfo* chain_info = get_chain_info(pCreateInfo, VK_LAYER_LINK_INFO);
assert(chain_info->u.pLayerInfo != 0);
PFN_vkGetInstanceProcAddr fpGetInstanceProcAddr = chain_info->u.pLayerInfo->pfnNextGetInstanceProcAddr;
assert(fpGetInstanceProcAddr != 0);
PFN_vkCreateInstance fpCreateInstance = (PFN_vkCreateInstance) fpGetInstanceProcAddr(NULL, "vkCreateInstance");
if(fpCreateInstance == NULL) {
return VK_ERROR_INITIALIZATION_FAILED;
}

// Call the function and create the dispatch table
chain_info->u.pLayerInfo = chain_info->u.pLayerInfo->pNext;
VkResult result = fpCreateInstance(pCreateInfo, pAllocator, pInstance);
if(result == VK_SUCCESS) {
initInstanceTable(*pInstance, fpGetInstanceProcAddr);
}

// Output the API dump
if (ApiDumpInstance::current().shouldDumpOutput()) {
switch(ApiDumpInstance::current().settings().format())
{
case ApiDumpFormat::Text:
dump_text_vkCreateInstance(ApiDumpInstance::current(), result, pCreateInfo, pAllocator, pInstance);
break;
case ApiDumpFormat::Html:
dump_html_vkCreateInstance(ApiDumpInstance::current(), result, pCreateInfo, pAllocator, pInstance);
break;
case ApiDumpFormat::Json:
dump_json_vkCreateInstance(ApiDumpInstance::current(), result, pCreateInfo, pAllocator, pInstance);
break;
}
}
return result;
}

VKAPI_ATTR VkResult VKAPI_CALL vkCreateDevice(VkPhysicalDevice physicalDevice, const VkDeviceCreateInfo* pCreateInfo, const VkAllocationCallbacks* pAllocator, VkDevice* pDevice)
{
std::lock_guard<std::mutex> lg(ApiDumpInstance::current().outputMutex());
if (ApiDumpInstance::current().settings().shouldPreDump() && ApiDumpInstance::current().settings().format() == ApiDumpFormat::Text) {
dump_function_head(ApiDumpInstance::current(), "vkCreateDevice", "physicalDevice, pCreateInfo, pAllocator, pDevice");
if (ApiDumpInstance::current().shouldDumpOutput()) {
dump_text_params_vkCreateDevice(ApiDumpInstance::current(), physicalDevice, pCreateInfo, pAllocator, pDevice);
}
dump_return_preamble(ApiDumpInstance::current(), "VkResult");
} else {
dump_function_head(ApiDumpInstance::current(), "vkCreateDevice", "physicalDevice, pCreateInfo, pAllocator, pDevice", "VkResult");
}

// Get the function pointer
VkLayerDeviceCreateInfo* chain_info = get_chain_info(pCreateInfo, VK_LAYER_LINK_INFO);
assert(chain_info->u.pLayerInfo != 0);
PFN_vkGetInstanceProcAddr fpGetInstanceProcAddr = chain_info->u.pLayerInfo->pfnNextGetInstanceProcAddr;
PFN_vkGetDeviceProcAddr fpGetDeviceProcAddr = chain_info->u.pLayerInfo->pfnNextGetDeviceProcAddr;
VkInstance vk_instance = ApiDumpInstance::current().get_vk_instance(physicalDevice);
PFN_vkCreateDevice fpCreateDevice = (PFN_vkCreateDevice) fpGetInstanceProcAddr(vk_instance, "vkCreateDevice");
if(fpCreateDevice == NULL) {
return VK_ERROR_INITIALIZATION_FAILED;
}

// Call the function and create the dispatch table
chain_info->u.pLayerInfo = chain_info->u.pLayerInfo->pNext;
VkResult result = fpCreateDevice(physicalDevice, pCreateInfo, pAllocator, pDevice);
if(result == VK_SUCCESS) {
initDeviceTable(*pDevice, fpGetDeviceProcAddr);
}

// Output the API dump
if (ApiDumpInstance::current().shouldDumpOutput()) {
switch(ApiDumpInstance::current().settings().format())
{
case ApiDumpFormat::Text:
dump_text_vkCreateDevice(ApiDumpInstance::current(), result, physicalDevice, pCreateInfo, pAllocator, pDevice);
break;
case ApiDumpFormat::Html:
dump_html_vkCreateDevice(ApiDumpInstance::current(), result, physicalDevice, pCreateInfo, pAllocator, pDevice);
break;
case ApiDumpFormat::Json:
dump_json_vkCreateDevice(ApiDumpInstance::current(), result, physicalDevice, pCreateInfo, pAllocator, pDevice);
break;
}
}
return result;
}

EXPORT_FUNCTION VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateInstanceExtensionProperties(const char* pLayerName, uint32_t* pPropertyCount, VkExtensionProperties* pProperties)
{
return util_GetExtensionProperties(0, NULL, pPropertyCount, pProperties);
}

EXPORT_FUNCTION VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateInstanceLayerProperties(uint32_t* pPropertyCount, VkLayerProperties* pProperties)
{
static const VkLayerProperties layerProperties[] = {
{
"VK_LAYER_LUNARG_api_dump",
VK_MAKE_VERSION(1, 4, VK_HEADER_VERSION), // specVersion
VK_MAKE_VERSION(0, 2, 0), // implementationVersion
"layer: api_dump",
}
};

return util_GetLayerProperties(ARRAY_SIZE(layerProperties), layerProperties, pPropertyCount, pProperties);
}

EXPORT_FUNCTION VKAPI_ATTR VkResult VKAPI_CALL vkEnumerateDeviceLayerProperties(VkPhysicalDevice physicalDevice, uint32_t* pPropertyCount, VkLayerProperties* pProperties)
{
static const VkLayerProperties layerProperties[] = {
{
"VK_LAYER_LUNARG_api_dump",
VK_MAKE_VERSION(1, 4, VK_HEADER_VERSION),
VK_MAKE_VERSION(0, 2, 0),
"layer: api_dump",
}
};

return util_GetLayerProperties(ARRAY_SIZE(layerProperties), layerProperties, pPropertyCount, pProperties);
}


EXPORT_FUNCTION VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vkGetInstanceProcAddr(VkInstance instance, const char* pName)
{
auto instance_func = api_dump_known_instance_functions(instance, pName);
if (instance_func) return instance_func;

// Make sure that device functions queried through GIPA works
auto device_func = api_dump_known_device_functions(NULL, pName);
if (device_func) return device_func;

// Haven't created an instance yet, exit now since there is no instance_dispatch_table
if(instance_dispatch_table(instance)->GetInstanceProcAddr == NULL)
return nullptr;
return instance_dispatch_table(instance)->GetInstanceProcAddr(instance, pName);
}

EXPORT_FUNCTION VKAPI_ATTR PFN_vkVoidFunction VKAPI_CALL vkGetDeviceProcAddr(VkDevice device, const char* pName)
{
auto device_func = api_dump_known_device_functions(device, pName);
if (device_func) return device_func;

// Haven't created a device yet, exit now since there is no device_dispatch_table
if(device_dispatch_table(device)->GetDeviceProcAddr == NULL)
return nullptr;
return device_dispatch_table(device)->GetDeviceProcAddr(device, pName);
}
7 changes: 7 additions & 0 deletions layersvt/generated/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
# Use defaults from the Google style with the following exceptions:
BasedOnStyle: Google
IndentWidth: 4
ColumnLimit: 0
SortIncludes: false
...
Loading