3. 初始化
在使用Vulkan之前,应用程序必须通过载入Vulkan命令和创建VkInstance对象来初始化它。
3.1. 命令函数的指针
Vulkan命令在各平台上并不是静态的暴露出来的。可以通过以下命令来获取Vulkan命令的函数指针:
PFN_vkVoidFunction vkGetInstanceProcAddr(
VkInstance instance,
const char* pName);
-
instanceis the instance that the function pointer will be compatible with, orNULLfor commands not dependent on any instance. -
pName是需要获取的命令的名字。
vkGetInstanceProcAddr 自己是通过平台和loader各异的方式获取的。通常,loader库将以函数符号的方式导出这个命令, 所以,应用程序可以链接到loader库,或者动态的载入并使用平台自己的API来寻找符号。 loader应导出其他所有的核心Vulkan命令;如果完成了,应用程序只使用核心的Vulkan命令,就没有必要使用vkGetInstanceProcAddr了。
下面的表格定义了vkGetInstanceProcAddr 各种使用场景和期待的返回值(fp 是函数指针):
返回的函数指针是 PFN_vkVoidFunction 类型的,必须强制转换为查询所用的类型:
instance | pName | return value |
|---|---|---|
| * | | undefined |
| invalid instance | * | undefined |
| | fp | |
| | fp | |
| | fp | |
| | * (any | |
| instance | core Vulkan command | fp1 |
| instance | enabled instance extension commands for | fp1 |
| instance | available device extension2 commands for | fp1 |
| instance | * (any | |
-
1
-
The returned function pointer must only be called with a dispatchable object (the first parameter) that is
instanceor a child ofinstance. e.g.VkInstance,VkPhysicalDevice,VkDevice,VkQueue, orVkCommandBuffer.
2
-
An “available extension” is an extension function supported by any of the loader, driver or layer.
为了支持有多个Vulkan实现的异构系统,vkGetInstanceProcAddr 返回的函数指针可能指向 不可可分发的代码,亦即 对不同的VkDevice对象调用不同的真实实现。 这个内部分发的开销可以通过获取设备各异的函数指针而避免,这些命令使用设备或者设备子对象作为不可分发对象。 这些函数指针可通过以下命令获取:
PFN_vkVoidFunction vkGetDeviceProcAddr(
VkDevice device,
const char* pName);
下面的表格定义了使用vkGetDeviceProcAddr的各种场景和各自期待的返回值。
返回的函数指针是 PFN_vkVoidFunction 类型的,必须强制转换为查询所用的类型:
device | pName | return value |
|---|---|---|
| | * | undefined |
| invalid device | * | undefined |
| device | | undefined |
| device | core Vulkan command | fp1 |
| device | enabled extension commands | fp1 |
| device | * (any | |
-
1
-
The returned function pointer must only be called with a dispatchable object (the first parameter) that is
deviceor a child ofdevice. e.g.VkDevice,VkQueue, orVkCommandBuffer.
The definition of PFN_vkVoidFunction is:
typedef void (VKAPI_PTR *PFN_vkVoidFunction)(void);
3.2. 实例
在Vulkan中没有全局的状态,所有应用程序自己的状态都存储在一个VkInstance对象中。 创建一个VkInstance 对象会初始化Vulkan库并允许应用程序传递信息给Vulkan实现。
Instances are represented by VkInstance handles:
VK_DEFINE_HANDLE(VkInstance)
To create an instance object, call:
VkResult vkCreateInstance(
const VkInstanceCreateInfo* pCreateInfo,
const VkAllocationCallbacks* pAllocator,
VkInstance* pInstance);
-
pCreateInfopoints to an instance ofVkInstanceCreateInfocontrolling creation of the instance. -
pAllocatorcontrols host memory allocation as described in the Memory Allocation chapter. -
pInstancepoints aVkInstancehandle in which the resulting instance is returned.
vkCreateInstance创建一个实例,然后启用并初始化应用程序需要的全局层和拓展。 如果一个拓展通过一个层提供,那么层和拓展都必须在vkCreateInstance指定。 如果指定的层没有被找到,那么将不会创建VkInstance对象,函数将返回VK_ERROR_LAYER_NOT_PRESENT。 同样,如果一个指定的拓展没有被找到,函数调用将返回VK_ERROR_EXTENSION_NOT_PRESENT。
The VkInstanceCreateInfo structure is defined as:
typedef struct VkInstanceCreateInfo {
VkStructureType sType;
const void* pNext;
VkInstanceCreateFlags flags;
const VkApplicationInfo* pApplicationInfo;
uint32_t enabledLayerCount;
const char* const* ppEnabledLayerNames;
uint32_t enabledExtensionCount;
const char* const* ppEnabledExtensionNames;
} VkInstanceCreateInfo;
-
sTypeis the type of this structure. -
pNextisNULLor a pointer to an extension-specific structure. -
flagsis reserved for future use. -
pApplicationInfoisNULLor a pointer to an instance ofVkApplicationInfo. If notNULL, this information helps implementations recognize behavior inherent to classes of applications.VkApplicationInfois defined in detail below. -
enabledLayerCountis the number of global layers to enable. -
ppEnabledLayerNamesis a pointer to an array ofenabledLayerCountnull-terminated UTF-8 strings containing the names of layers to enable for the created instance. See the Layers section for further details. -
enabledExtensionCountis the number of global extensions to enable. -
ppEnabledExtensionNamesis a pointer to an array ofenabledExtensionCountnull-terminated UTF-8 strings containing the names of extensions to enable.
The VkApplicationInfo structure is defined as:
typedef struct VkApplicationInfo {
VkStructureType sType;
const void* pNext;
const char* pApplicationName;
uint32_t applicationVersion;
const char* pEngineName;
uint32_t engineVersion;
uint32_t apiVersion;
} VkApplicationInfo;
-
sTypeis the type of this structure. -
pNextisNULLor a pointer to an extension-specific structure. -
pApplicationNameis a pointer to a null-terminated UTF-8 string containing the name of the application. -
applicationVersionis an unsigned integer variable containing the developer-supplied version number of the application. -
pEngineNameis a pointer to a null-terminated UTF-8 string containing the name of the engine (if any) used to create the application. -
engineVersionis an unsigned integer variable containing the developer-supplied version number of the engine used to create the application. -
apiVersionis the version of the Vulkan API against which the application expects to run, encoded as described in theAPI Version Numbers and Semantics section. IfapiVersionis 0 the implementation must ignore it, otherwise if the implementation does not support the requestedapiVersionit must returnVK_ERROR_INCOMPATIBLE_DRIVER. The patch version number specified inapiVersionis ignored when creating an instance object. Only the major and minor versions of the instance must match those requested inapiVersion.
To destroy an instance, call:
void vkDestroyInstance(
VkInstance instance,
const VkAllocationCallbacks* pAllocator);
-
instanceis the handle of the instance to destroy. -
pAllocatorcontrols host memory allocation as described in the Memory Allocation chapter.
438

被折叠的 条评论
为什么被折叠?



