Skip to content

Commit 683c74d

Browse files
authored
Improve static common code (#21)
Change to improve the static common code. * Non-generated common code is no longer copied by the generator. * GitHub Actions tests now validate that the generated common code matches the committed copy of that code to prevent committed template vs committed source desyncs. * Non-generated common code uses consistent function name style. * All common code uses consistent module-based include paths.
1 parent e181c48 commit 683c74d

24 files changed

+439
-643
lines changed

.github/workflows/build_test.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ jobs:
8787
cmake -G "Unix Makefiles" -DCMAKE_BUILD_TYPE=Release ..
8888
make -j4
8989
90+
- name: Check unexpected diffs
91+
run: |
92+
git diff --exit-code
93+
9094
build-ubuntu-x64-clang-new-project:
9195
name: Ubuntu x64 generate new layer
9296
runs-on: ubuntu-22.04

generator/generate_vulkan_common.py

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -685,15 +685,6 @@ def main():
685685
base_dir = os.path.dirname(__file__)
686686
outdir = os.path.join(base_dir, '..', 'source_common', 'framework')
687687

688-
# Clean the output directory if needed
689-
if os.path.exists(outdir):
690-
if not os.path.isdir(outdir):
691-
print(f'ERROR: Output location "{outdir}" is not a directory')
692-
return 1
693-
694-
shutil.rmtree(outdir, ignore_errors=True)
695-
os.makedirs(outdir)
696-
697688
# Parse the XML headers
698689
tree = ET.parse('./khronos/vulkan/registry/vk.xml')
699690
root = tree.getroot()
@@ -732,11 +723,6 @@ def main():
732723
# Load hand written function bodies
733724
manual_commands = load_handwritten_commands()
734725

735-
# Generate static resources
736-
base_dir = os.path.dirname(__file__)
737-
source_dir = os.path.join(base_dir, 'vk_common')
738-
copy_resource(source_dir, outdir)
739-
740726
# Generate dynamic resources
741727
outfile = os.path.join(outdir, 'instance_dispatch_table.hpp')
742728
with open(outfile, 'w', encoding='utf-8', newline='\n') as handle:

generator/vk_codegen/device_defs.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22
#include <mutex>
33
#include <thread>
44

5+
// Include from per-layer code
56
#include "utils.hpp"
67
#include "device.hpp"
7-
#include "device_functions.hpp"
8+
9+
// Include from common code
10+
#include "framework/device_functions.hpp"
811

912
extern std::mutex g_vulkanLock;
1013

generator/vk_codegen/device_dispatch_table.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <vulkan/vulkan.h>
44

55
#include "framework/device_functions.hpp"
6+
#include "framework/utils.hpp"
67
#include "utils/misc.hpp"
78

89
#if __has_include ("layer_device_functions.hpp")

generator/vk_codegen/function_vkCreateDevice.txt

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// Release the lock to call into the driver
66
lock.unlock();
77

8-
auto* chainInfo = get_chain_info(pCreateInfo, VK_LAYER_LINK_INFO);
8+
auto* chainInfo = getChainInfo(pCreateInfo);
99
auto fpGetInstanceProcAddr = chainInfo->u.pLayerInfo->pfnNextGetInstanceProcAddr;
1010
auto fpGetDeviceProcAddr = chainInfo->u.pLayerInfo->pfnNextGetDeviceProcAddr;
1111
auto fpCreateDevice = reinterpret_cast<PFN_vkCreateDevice>(fpGetInstanceProcAddr(layer->instance, "vkCreateDevice"));
@@ -16,17 +16,15 @@
1616

1717
// Advance the link info for the next element on the chain
1818
chainInfo->u.pLayerInfo = chainInfo->u.pLayerInfo->pNext;
19-
2019
auto res = fpCreateDevice(physicalDevice, pCreateInfo, pAllocator, pDevice);
2120
if (res != VK_SUCCESS)
2221
{
2322
return res;
2423
}
2524

26-
auto device = std::make_unique<Device>(layer, physicalDevice, *pDevice, fpGetDeviceProcAddr);
27-
28-
// Hold the lock to access layer-wide global store
25+
// Retake the lock to access layer-wide global store
2926
lock.lock();
27+
auto device = std::make_unique<Device>(layer, physicalDevice, *pDevice, fpGetDeviceProcAddr);
3028
Device::store(*pDevice, std::move(device));
3129

3230
return VK_SUCCESS;

generator/vk_codegen/function_vkCreateInstance.txt

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
auto* chainInfo = get_chain_info(pCreateInfo, VK_LAYER_LINK_INFO);
1+
auto* chainInfo = getChainInfo(pCreateInfo);
2+
auto supportedExtensions = getInstanceExtensionList(pCreateInfo);
23

34
auto fpGetInstanceProcAddr = chainInfo->u.pLayerInfo->pfnNextGetInstanceProcAddr;
45
auto fpCreateInstance = reinterpret_cast<PFN_vkCreateInstance>(fpGetInstanceProcAddr(nullptr, "vkCreateInstance"));
@@ -7,14 +8,45 @@
78
return VK_ERROR_INITIALIZATION_FAILED;
89
}
910

11+
// Create a copy we can write
12+
VkInstanceCreateInfo newCreateInfo = *pCreateInfo;
13+
14+
// Query extension state
15+
std::string targetExt("VK_EXT_debug_utils");
16+
bool targetSupported = isIn(targetExt, supportedExtensions);
17+
bool targetEnabled = isInExtensionList(
18+
targetExt,
19+
pCreateInfo->enabledExtensionCount,
20+
pCreateInfo->ppEnabledExtensionNames);
21+
22+
if (!targetSupported)
23+
{
24+
LAYER_LOG("WARNING: Cannot enable additional extension: %s", targetExt.c_str());
25+
}
26+
27+
// Enable the extension if we need to
28+
std::vector<const char*> newExtList;
29+
if (targetSupported && !targetEnabled)
30+
{
31+
LAYER_LOG("Enabling additional extension: %s", targetExt.c_str());
32+
newExtList = cloneExtensionList(
33+
pCreateInfo->enabledExtensionCount,
34+
pCreateInfo->ppEnabledExtensionNames);
35+
36+
newExtList.push_back(targetExt.c_str());
37+
38+
newCreateInfo.enabledExtensionCount = newExtList.size();
39+
newCreateInfo.ppEnabledExtensionNames = newExtList.data();
40+
}
41+
1042
chainInfo->u.pLayerInfo = chainInfo->u.pLayerInfo->pNext;
11-
auto res = fpCreateInstance(pCreateInfo, pAllocator, pInstance);
43+
auto res = fpCreateInstance(&newCreateInfo, pAllocator, pInstance);
1244
if (res != VK_SUCCESS)
1345
{
1446
return res;
1547
}
1648

17-
// Hold the lock to access layer-wide global store
49+
// Retake the lock to access layer-wide global store
1850
auto instance = std::make_unique<Instance>(*pInstance, fpGetInstanceProcAddr);
1951
{
2052
std::lock_guard<std::mutex> lock { g_vulkanLock };

generator/vk_codegen/instance_defs.txt

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,15 @@
22
#include <mutex>
33
#include <thread>
44

5-
#include "utils.hpp"
6-
#include "instance.hpp"
5+
// Include from per-layer code
76
#include "device.hpp"
8-
#include "instance_functions.hpp"
9-
10-
extern std::mutex g_vulkanLock;
11-
12-
static VkLayerInstanceCreateInfo* get_chain_info(
13-
const VkInstanceCreateInfo* pCreateInfo,
14-
VkLayerFunction func
15-
) {
16-
auto* info = static_cast<const VkLayerInstanceCreateInfo*>(pCreateInfo->pNext);
17-
while (info && !(info->sType == VK_STRUCTURE_TYPE_LOADER_INSTANCE_CREATE_INFO && info->function == func))
18-
{
19-
info = static_cast<const VkLayerInstanceCreateInfo*>(info->pNext);
20-
}
21-
22-
return const_cast<VkLayerInstanceCreateInfo*>(info);
23-
}
7+
#include "instance.hpp"
248

25-
static VkLayerDeviceCreateInfo* get_chain_info(
26-
const VkDeviceCreateInfo* pCreateInfo,
27-
VkLayerFunction func
28-
) {
29-
auto* info = static_cast<const VkLayerDeviceCreateInfo*>(pCreateInfo->pNext);
30-
while (info && !(info->sType == VK_STRUCTURE_TYPE_LOADER_DEVICE_CREATE_INFO && info->function == func))
31-
{
32-
info = static_cast<const VkLayerDeviceCreateInfo*>(info->pNext);
33-
}
9+
// Include from common code
10+
#include "framework/instance_functions_manual.hpp"
11+
#include "framework/instance_functions.hpp"
12+
#include "framework/utils.hpp"
3413

35-
return const_cast<VkLayerDeviceCreateInfo*>(info);
36-
}
14+
extern std::mutex g_vulkanLock;
3715

3816
{FUNCTION_DEFS}

generator/vk_codegen/instance_dispatch_table.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <vulkan/vulkan.h>
44

55
#include "framework/instance_functions.hpp"
6+
#include "framework/utils.hpp"
67
#include "utils/misc.hpp"
78

89
#if __has_include ("layer_instance_functions.hpp")

generator/vk_codegen/root_CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ set(CMAKE_CXX_STANDARD 20)
2828
project({PROJECT_NAME} VERSION 1.0.0)
2929

3030
# Common configuration
31-
set(LGL_LOG_TAG, "{LAYER_NAME}")
31+
set(LGL_LOG_TAG "{LAYER_NAME}")
3232
include(../source_common/compiler_helper.cmake)
3333

3434
# Build steps

generator/vk_common/CMakeLists.txt

Lines changed: 0 additions & 42 deletions
This file was deleted.

0 commit comments

Comments
 (0)