Skip to content

Commit

Permalink
Cleanup common code
Browse files Browse the repository at this point in the history
  • Loading branch information
solidpixel committed Dec 9, 2024
1 parent 835af46 commit f1ec702
Show file tree
Hide file tree
Showing 11 changed files with 89 additions and 75 deletions.
5 changes: 4 additions & 1 deletion generator/vk_codegen/device_defs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
#include <mutex>
#include <thread>

// Include from per-layer code
#include "utils.hpp"
#include "device.hpp"
#include "device_functions.hpp"

// Include from common code
#include "framework/device_functions.hpp"

extern std::mutex g_vulkanLock;

Expand Down
2 changes: 1 addition & 1 deletion generator/vk_codegen/function_vkCreateDevice.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
// Release the lock to call into the driver
lock.unlock();

auto* chainInfo = get_chain_info(pCreateInfo, VK_LAYER_LINK_INFO);
auto* chainInfo = getChainInfo(pCreateInfo, VK_LAYER_LINK_INFO);
auto fpGetInstanceProcAddr = chainInfo->u.pLayerInfo->pfnNextGetInstanceProcAddr;
auto fpGetDeviceProcAddr = chainInfo->u.pLayerInfo->pfnNextGetDeviceProcAddr;
auto fpCreateDevice = reinterpret_cast<PFN_vkCreateDevice>(fpGetInstanceProcAddr(layer->instance, "vkCreateDevice"));
Expand Down
2 changes: 1 addition & 1 deletion generator/vk_codegen/function_vkCreateInstance.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
auto* chainInfo = get_chain_info(pCreateInfo, VK_LAYER_LINK_INFO);
auto* chainInfo = getChainInfo(pCreateInfo, VK_LAYER_LINK_INFO);

auto fpGetInstanceProcAddr = chainInfo->u.pLayerInfo->pfnNextGetInstanceProcAddr;
auto fpCreateInstance = reinterpret_cast<PFN_vkCreateInstance>(fpGetInstanceProcAddr(nullptr, "vkCreateInstance"));
Expand Down
9 changes: 6 additions & 3 deletions generator/vk_codegen/instance_defs.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
#include <mutex>
#include <thread>

// Include from per-layer code
#include "device.hpp"
#include "entry_utils.hpp"
#include "instance.hpp"
#include "instance_functions.hpp"
#include "utils.hpp"

// Include from common code
#include "framework/instance_functions_manual.hpp"
#include "framework/instance_functions.hpp"
#include "framework/utils.hpp"

extern std::mutex g_vulkanLock;

Expand Down
2 changes: 1 addition & 1 deletion generator/vk_layer/source/entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
#include <thread>

#include "framework/utils.hpp"
#include "framework/entry_utils.hpp"
#include "framework/instance_functions_manual.hpp"

std::mutex g_vulkanLock;

Expand Down
2 changes: 1 addition & 1 deletion layer_example/source/entry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
#include <thread>

#include "framework/utils.hpp"
#include "framework/entry_utils.hpp"
#include "framework/instance_functions_manual.hpp"

std::mutex g_vulkanLock;

Expand Down
4 changes: 2 additions & 2 deletions source_common/framework/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ set(LIB_BINARY lib_layer_framework)
add_library(
${LIB_BINARY} STATIC
device_functions.cpp
entry_utils.cpp
instance_functions.cpp)
instance_functions.cpp
instance_functions_manual.cpp)

target_include_directories(
${LIB_BINARY} PRIVATE
Expand Down
5 changes: 4 additions & 1 deletion source_common/framework/device_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,12 @@
#include <mutex>
#include <thread>

// Include from per-layer code
#include "utils.hpp"
#include "device.hpp"
#include "device_functions.hpp"

// Include from common code
#include "framework/device_functions.hpp"

extern std::mutex g_vulkanLock;

Expand Down
13 changes: 8 additions & 5 deletions source_common/framework/instance_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,14 @@
#include <mutex>
#include <thread>

// Include from per-layer code
#include "device.hpp"
#include "entry_utils.hpp"
#include "instance.hpp"
#include "instance_functions.hpp"
#include "utils.hpp"

// Include from common code
#include "framework/instance_functions_manual.hpp"
#include "framework/instance_functions.hpp"
#include "framework/utils.hpp"

extern std::mutex g_vulkanLock;

Expand Down Expand Up @@ -109,7 +112,7 @@ VKAPI_ATTR VkResult VKAPI_CALL layer_vkCreateDevice_default(
// Release the lock to call into the driver
lock.unlock();

auto* chainInfo = get_chain_info(pCreateInfo, VK_LAYER_LINK_INFO);
auto* chainInfo = getChainInfo(pCreateInfo, VK_LAYER_LINK_INFO);
auto fpGetInstanceProcAddr = chainInfo->u.pLayerInfo->pfnNextGetInstanceProcAddr;
auto fpGetDeviceProcAddr = chainInfo->u.pLayerInfo->pfnNextGetDeviceProcAddr;
auto fpCreateDevice = reinterpret_cast<PFN_vkCreateDevice>(fpGetInstanceProcAddr(layer->instance, "vkCreateDevice"));
Expand Down Expand Up @@ -179,7 +182,7 @@ VKAPI_ATTR VkResult VKAPI_CALL layer_vkCreateInstance_default(
) {
LAYER_TRACE(__func__);

auto* chainInfo = get_chain_info(pCreateInfo, VK_LAYER_LINK_INFO);
auto* chainInfo = getChainInfo(pCreateInfo, VK_LAYER_LINK_INFO);

auto fpGetInstanceProcAddr = chainInfo->u.pLayerInfo->pfnNextGetInstanceProcAddr;
auto fpCreateInstance = reinterpret_cast<PFN_vkCreateInstance>(fpGetInstanceProcAddr(nullptr, "vkCreateInstance"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,51 @@
* implementations on a per-layer basis if needed.
*/

#include "framework/entry_utils.hpp"
#include "framework/instance_functions_manual.hpp"

VkLayerInstanceCreateInfo* get_chain_info(
/**
* @brief Shared globals.
*/
extern std::mutex g_vulkanLock;

/**
* @brief The layer configuration.
*/
#define LGL_VERSION VK_MAKE_VERSION(LGL_VER_MAJOR, LGL_VER_MINOR, LGL_VER_PATCH)

static const std::array<VkLayerProperties, 1> layerProps = {
{{ LGL_LAYER_NAME, LGL_VERSION, 1, LGL_LAYER_DESC }},
};

/**
* @brief Dispatch table lookup entry.
*/
struct DispatchTableEntry
{
/**
* @brief The function entrypoint name.
*/
const char* name;

/**
* @brief The function pointer.
*/
PFN_vkVoidFunction function;
};

/**
* @brief Utility macro to define a lookup for a core function.
*/
#define VK_TABLE_ENTRY(func) \
{ STR(func), reinterpret_cast<PFN_vkVoidFunction>(func) }

/**
* @brief Utility macro to define a lookup for a layer-dispatch-only function.
*/
#define VK_TABLE_ENTRYL(func) \
{ STR(func), reinterpret_cast<PFN_vkVoidFunction>(layer_##func) }

VkLayerInstanceCreateInfo* getChainInfo(
const VkInstanceCreateInfo* pCreateInfo,
VkLayerFunction func
) {
Expand All @@ -45,7 +87,7 @@ VkLayerInstanceCreateInfo* get_chain_info(
return const_cast<VkLayerInstanceCreateInfo*>(info);
}

VkLayerDeviceCreateInfo* get_chain_info(
VkLayerDeviceCreateInfo* getChainInfo(
const VkDeviceCreateInfo* pCreateInfo,
VkLayerFunction func
) {
Expand All @@ -66,7 +108,7 @@ VkLayerDeviceCreateInfo* get_chain_info(
* \return The layer function pointer, or \c nullptr if the layer doesn't
* intercept the function.
*/
PFN_vkVoidFunction get_fixed_instance_layer_function(
PFN_vkVoidFunction getFixedInstanceLayerFunction(
const char* name
) {
static const DispatchTableEntry layerFunctions[] = {
Expand Down Expand Up @@ -96,7 +138,7 @@ PFN_vkVoidFunction get_fixed_instance_layer_function(
* \return The layer function pointer, or \c nullptr if the layer doesn't
* intercept the function.
*/
PFN_vkVoidFunction get_instance_layer_function(
PFN_vkVoidFunction getInstanceLayerFunction(
const char* name
) {
for (auto &function : instanceIntercepts)
Expand All @@ -117,7 +159,7 @@ PFN_vkVoidFunction get_instance_layer_function(
*
* \return The layer function pointer, or \c nullptr if the layer doesn't intercept the function.
*/
PFN_vkVoidFunction get_device_layer_function(
PFN_vkVoidFunction getDeviceLayerFunction(
const char* name
) {
static const DispatchTableEntry layerFunctions[] = {
Expand Down Expand Up @@ -158,7 +200,7 @@ PFN_vkVoidFunction vkGetDeviceProcAddr_default(
// Only expose functions that the driver exposes to avoid changing
// queryable interface behavior seen by the application
auto driver_function = layer->driver.vkGetDeviceProcAddr(device, pName);
auto layer_function = get_device_layer_function(pName);
auto layer_function = getDeviceLayerFunction(pName);

// If driver exposes it and the layer has one, use the layer function
if (driver_function && layer_function)
Expand All @@ -176,15 +218,15 @@ PFN_vkVoidFunction vkGetInstanceProcAddr_default(
const char* pName
) {
// Always expose these functions ...
PFN_vkVoidFunction layerFunction = get_fixed_instance_layer_function(pName);
PFN_vkVoidFunction layerFunction = getFixedInstanceLayerFunction(pName);
if (layerFunction)
{
return layerFunction;
}

// Otherwise, only expose functions that the driver exposes to avoid
// changing queryable interface behavior seen by the application
layerFunction = get_instance_layer_function(pName);
layerFunction = getInstanceLayerFunction(pName);
if (instance) {
std::unique_lock<std::mutex> lock { g_vulkanLock };
auto* layer = Instance::retrieve(instance);
Expand Down Expand Up @@ -213,7 +255,7 @@ std::vector<std::string> getInstanceExtensionList(
std::vector<std::string> foundExtensions;

// Get layer chain info, and save next pointer so we can restore later
auto* chainInfo = get_chain_info(pCreateInfo, VK_LAYER_LINK_INFO);
auto* chainInfo = getChainInfo(pCreateInfo, VK_LAYER_LINK_INFO);

// Fetch the functions needed to query extensions availability
auto fpGetProcAddr = chainInfo->u.pLayerInfo->pfnNextGetInstanceProcAddr;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@
#include "device_dispatch_table.hpp"
#include "device_functions.hpp"

extern std::mutex g_vulkanLock;

#define VK_LAYER_EXPORT __attribute__((visibility("default")))

#if defined(VK_USE_PLATFORM_ANDROID_KHR)
Expand All @@ -55,49 +53,11 @@ extern std::mutex g_vulkanLock;
#define VK_LAYER_EXPORT_ANDROID
#endif

/**
* @brief The layer configuration.
*/
#define LGL_VERSION VK_MAKE_VERSION(LGL_VER_MAJOR, LGL_VER_MINOR, LGL_VER_PATCH)

static const std::array<VkLayerProperties, 1> layerProps = {
{{ LGL_LAYER_NAME, LGL_VERSION, 1, LGL_LAYER_DESC }},
};

/**
* @brief Dispatch table lookup entry.
*/
struct DispatchTableEntry
{
/**
* @brief The function entrypoint name.
*/
const char* name;

/**
* @brief The function pointer.
*/
PFN_vkVoidFunction function;
};

/**
* @brief Utility macro to define a lookup for a core function.
*/
#define VK_TABLE_ENTRY(func) \
{ STR(func), reinterpret_cast<PFN_vkVoidFunction>(func) }

/**
* @brief Utility macro to define a lookup for a layer-dispatch-only function.
*/
#define VK_TABLE_ENTRYL(func) \
{ STR(func), reinterpret_cast<PFN_vkVoidFunction>(layer_##func) }


VkLayerInstanceCreateInfo* get_chain_info(
VkLayerInstanceCreateInfo* getChainInfo(
const VkInstanceCreateInfo* pCreateInfo,
VkLayerFunction func);

VkLayerDeviceCreateInfo* get_chain_info(
VkLayerDeviceCreateInfo* getChainInfo(
const VkDeviceCreateInfo* pCreateInfo,
VkLayerFunction func);

Expand All @@ -109,7 +69,7 @@ VkLayerDeviceCreateInfo* get_chain_info(
* \return The layer function pointer, or \c nullptr if the layer doesn't
* intercept the function.
*/
PFN_vkVoidFunction get_fixed_instance_layer_function(
PFN_vkVoidFunction getFixedInstanceLayerFunction(
const char* name);

/**
Expand All @@ -120,7 +80,7 @@ PFN_vkVoidFunction get_fixed_instance_layer_function(
* \return The layer function pointer, or \c nullptr if the layer doesn't
* intercept the function.
*/
PFN_vkVoidFunction get_instance_layer_function(
PFN_vkVoidFunction getInstanceLayerFunction(
const char* name);

/**
Expand All @@ -130,9 +90,13 @@ PFN_vkVoidFunction get_instance_layer_function(
*
* \return The layer function pointer, or \c nullptr if the layer doesn't intercept the function.
*/
PFN_vkVoidFunction get_device_layer_function(
PFN_vkVoidFunction getDeviceLayerFunction(
const char* name);

/* TODO. */
std::vector<std::string> getInstanceExtensionList(
const VkInstanceCreateInfo* pCreateInfo);

/** See Vulkan API for documentation. */
PFN_vkVoidFunction vkGetDeviceProcAddr_default(
VkDevice device,
Expand All @@ -143,10 +107,6 @@ PFN_vkVoidFunction vkGetInstanceProcAddr_default(
VkInstance instance,
const char* pName);

/* TODO. */
std::vector<std::string> getInstanceExtensionList(
const VkInstanceCreateInfo* pCreateInfo);

/** See Vulkan API for documentation. */
VkResult vkEnumerateInstanceExtensionProperties_default(
const char* pLayerName,
Expand Down

0 comments on commit f1ec702

Please sign in to comment.