Vulkan Cookbook 第一章 10 枚举可用的物理设备

枚举可用的物理设备

译者注:示例代码点击此处

Vulkan中的几乎所有工作都是在逻辑设备上执行的:我们在它们上创建资源,管理内存,记录从它们创建的命令缓冲区,并提交命令以便处理它们的队列。 在我们的应用程序中,逻辑设备代表启用了一组功能和扩展的物理设备。 要创建逻辑设备,需要选择给定硬件平台上一个可用的物理设备。可是我们如何知道给定计算机上有多少物理设备可用呢?着需要枚举它们。

怎么做...

1.获取之前创建的Vulkan Instance的句柄,是名为instance的VkInstance类型变量。
2.准备一个名为devices_countuint32_t类型变量。
3.调用vkEnumeratePhysicalDevices(instance, &devices_count,nullptr)。第一个参数传入Vulkan Instance的句柄指针,第二个参数指向devices_count变量,还有第三个参数设置为nullptr
4.如果函数调用成功,则devices_count变量将包含可用物理设备的总数。
5.准备储存物理设备的容器,最好的解决方案是使用类型为VkPhysicalDevicestd::vector类型的变量,称之为available_devices
6.调整容器的大小以至少能够储存devices_count数量元素。
7.调用vkEnumeratePhysicalDevices(instance ,&devices_count,& available_devices[0])。第一个参数再次传入Vulkan Instance的句柄指针,第二个参数扔应指向extensions_count的变量,第三个参数质量VkPhysicalDevice类型数量为extensions_count的元素数组。这里提供的是available_devices的第一个元素的地址。
8.如果调用返回成功,则available_devices向量将包含安装在支持Vulkan API的给定硬件平台上的所有物理设备的列表。

这个怎么运作...

枚举可用物理设备操作分为两个阶段:首先,我们检查在任何给定硬件上有多少物理设备可用。这是通过调用vkEnumeratePhysicalDevices()函数来完成的,最后一个参数设置为nullptr,如下所示:

uint32_t devices_count = 0;
 VkResult result = VK_SUCCESS;

result = vkEnumeratePhysicalDevices( instance, &devices_count, nullptr ); 
if( (result != VK_SUCCESS) || (devices_count == 0) ) {
  std::cout << "Could not get the number of available physical devices." << std::endl; 
  return false;
}

这样,我们知道有多少物理设备支持Vulkan,以及需要存储多少个物理设备的句柄。当准备好足够的空间时,我们可以进入第二阶段,得到物理设备的实际手柄。这是通过调用相同的vkEnumeratePhysicalDevices()函数完成的,但是这一次,最后一个参数必须指向VkPhysicalDevice数组的元素:
 

available_devices.resize( devices_count );
result = vkEnumeratePhysicalDevices( instance, &devices_count, &available_devices[0] ); 
if( (result != VK_SUCCESS) || (devices_count == 0) ) {
  std::cout << "Could not enumerate physical devices." << std::endl; 
  return false;
}

return true;

当调用成功,available_devices里会填充物理设备句柄。

现在有了物理设备列表,我们就可以查看每个设备的属性、检查我们可以对其执行的操作,以及查看它所支持的扩展。

Computer graphics have a very long and interesting history. Many APIs or custom approaches to the generation of 2D or 3D images have come and gone. A landmark in this history was the invention of OpenGL, one of the first graphics libraries, which allowed us to create real-time, high-performance 3D graphics, and which was available for everyone on multiple operating systems. It is still developed and widely used even today. And this year we can celebrate its 25th birthday! But many things have changed since OpenGL was created. The graphics hardware industry is evolving very quickly. And recently, to accommodate these changes, a new approach to 3D graphics rendering was presented. It took the form of a low-level access to the graphics hardware. OpenGL was designed as a high-level API, which allows users to easily render images on screen. But this high-level approach, convenient for users, is difficult for graphics drivers to handle. This is one of the main reasons for restricting the hardware to show its full potential. The new approach tries to overcome these struggles–it gives users much more control over the hardware, but also many more responsibilities. This way application developers can release the full potential of the graphics hardware, because the drivers no longer block them. Low-level access allows drivers to be much smaller, much thinner. But these benefits come at the expense of much more work that needs to done by the developers. The first evangelist of the new approach to graphics rendering was a Mantle API designed by AMD. When it proved that low-level access can give considerable performance benefits, other companies started working on their own graphics libraries. One of the most notable representatives of the new trend were Metal API, designed by Apple, and DirectX 12, developed by Microsoft. But all of the above libraries were developed with specific operating systems and/or hardware in mind. There was no open and multiplatform standard such as OpenGL. Until last year. Year 2016 saw the release of the Vulkan API, developed by Khronos consortium, which maintains the OpenGL library. Vulkan also represents the new approach, a low-level access to the graphics hardware, but unlike the other libraries it is available for everyone on multiple operating systems and hardware platforms–from high-performance desktop computers with Windows or Linux operating systems, to mobile devices with Android OS. And as it is still being very new, there are few resources teaching developers how to use it. This book tries to fill this gap.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值