操作系統:Windows8.1
顯卡:Nivida GTX965M
開發工具:Visual Studio 2017
Selecting a physical device
通過VkInstance初始化Vulkan后,我們需要在系統中查找並選擇一個支持我們所需功能的顯卡。實際上,我們可以選擇任意數量的顯卡並同時使用他們,但在本小節中,我們簡單的設定選擇規則,即將查找到的第一個圖形卡作為我們適合的物理設備。

我們添加函數pickPhysicalDevice並在initVulkan函數中調用。
void initVulkan() { createInstance(); setupDebugCallback(); pickPhysicalDevice(); } void pickPhysicalDevice() { }
最終我們選擇的圖形顯卡存儲在類成員VkPhysicalDevice句柄中。當VkInstance銷毀時,這個對象將會被隱式銷毀,所以我們並不需要在cleanup函數中做任何操作。
VkPhysicalDevice physicalDevice = VK_NULL_HANDLE;
關於獲取圖形卡列表的方式與獲得擴展列表的方式類似。
uint32_t deviceCount = 0; vkEnumeratePhysicalDevices(instance, &deviceCount, nullptr);
如果Vulkan支持的設備數為0,那么沒有任何意義進行下一步,我們選擇拋出異常。
if (deviceCount == 0) { throw std::runtime_error("failed to find GPUs with Vulkan support!"); }
否則我們分配數組存儲所有VkPhysicalDevice的句柄。
std::vector<VkPhysicalDevice> devices(deviceCount);
vkEnumeratePhysicalDevices(instance, &deviceCount, devices.data());
現在我們需要對它們進行評估,檢查它們是否適合我們要執行的操作,因為並不是所有的顯卡功能一致。為此我們添加一個新的函數:
bool isDeviceSuitable(VkPhysicalDevice device) { return true; }
我們將檢查是否有任何物理設備符合我們的功能需求。
for (const auto& device : devices) { if (isDeviceSuitable(device)) { physicalDevice = device; break; } } if (physicalDevice == VK_NULL_HANDLE) { throw std::runtime_error("failed to find a suitable GPU!"); }
下一節我們介紹isDeviceSuitable函數,並檢查第一個需要滿足的功能。在后續的小節中,我們將開始使用更多的Vulkan功能,我們會擴展此功能函數以滿足更多的檢查條件。
Base device suitability checks
評估合適的設備我們可以通過遍歷一些細節來完成。基本的設備屬性像name, type以及Vulkan版本都可以通過vkGetPhysicalDeviceProperties來遍歷得到。
VkPhysicalDeviceProperties deviceProperties;
vkGetPhysicalDeviceProperties(device, &deviceProperties);
可以使用vkGetPhysicalDeviceFeatures查詢對紋理壓縮,64位浮點數和多視圖渲染(VR非常有用)等可選功能的支持:
VkPhysicalDeviceFeatures deviceFeatures;
vkGetPhysicalDeviceFeatures(device, &deviceFeatures);
更多遍歷物理設備細節的信息,諸如設備內存、隊列簇我們將會在后續小節討論。
例如,我們假設我們的應用程序僅適用於支持geometry shaders的專用顯卡。那么isDeviceSuitable函數將如下所示:
bool isDeviceSuitable(VkPhysicalDevice device) { VkPhysicalDeviceProperties deviceProperties; VkPhysicalDeviceFeatures deviceFeatures; vkGetPhysicalDeviceProperties(device, &deviceProperties); vkGetPhysicalDeviceFeatures(device, &deviceFeatures); return deviceProperties.deviceType == VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU && deviceFeatures.geometryShader; }
為了避免純粹的單一的判斷一個設備是否合適,尤其是當你發現多個設備都合適的條件下,你也可以給每一個設備做權值,選擇最高的一個。這樣,可以通過給予更高權值獲取定制化的圖形設備,但如果沒有一個可用的設備,可以回滾到集成圖形設備。你可以按照如下方式實現:
#include <map> ... void pickPhysicalDevice() { ... // Use an ordered map to automatically sort candidates by increasing score std::multimap<int, VkPhysicalDevice> candidates; for (const auto& device : devices) { int score = rateDeviceSuitability(device); candidates.insert(std::make_pair(score, device)); } // Check if the best candidate is suitable at all if (candidates.rbegin()->first > 0) { physicalDevice = candidates.rbegin()->second; } else { throw std::runtime_error("failed to find a suitable GPU!"); } } int rateDeviceSuitability(VkPhysicalDevice device) { ... int score = 0; // Discrete GPUs have a significant performance advantage if (deviceProperties.deviceType == VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU) { score += 1000; } // Maximum possible size of textures affects graphics quality score += deviceProperties.limits.maxImageDimension2D; // Application can't function without geometry shaders if (!deviceFeatures.geometryShader) { return 0; } return score; }
我們不需要在小節內實現所有內容,但我們可以了解如何選擇圖形設備的過程。當然,我們也可以顯示圖形設備的名稱列表,讓用戶選擇。
因為我們剛剛開始,Vulkan的支持是我們唯一需要的,在這里假設任何GPU都可以:
bool isDeviceSuitable(VkPhysicalDevice device) { return true; }
在下一小節中,我們將會討論第一個真正需要檢查的設備功能。
Queue families
之前已經簡要的介紹過,幾乎所有的Vulkan操作,從繪圖到上傳紋理,都需要將命令提交到隊列中。有不同類型的隊列來源於不同的隊列簇,每個隊列簇只允許部分commands。例如,可以有一個隊列簇,只允許處理計算commands或者只允許內存傳輸commands:
我們需要檢測設備中支持的隊列簇,其中哪一個隊列簇支持我們想要的commands。為此我們添加一個新的函數findQueueFamilies來查找我們需要的隊列簇。現在我們只會尋找一個支持圖形commands隊列簇,但是我們可以在稍后的小節中擴展更多的內容。

此函數返回滿足某個屬性的隊列簇索引。定義結構體,其中索引-1表示"未找到":
struct QueueFamilyIndices { int graphicsFamily = -1; bool isComplete() { return graphicsFamily >= 0; } };
現在我們實現findQueueFamilies函數:
QueueFamilyIndices findQueueFamilies(VkPhysicalDevice device) { QueueFamilyIndices indices; ... return indices; }
獲取隊列簇的列表函數為vkGetPhysicalDeviceQueueFamilyProperties:
uint32_t queueFamilyCount = 0; vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, nullptr); std::vector<VkQueueFamilyProperties> queueFamilies(queueFamilyCount); vkGetPhysicalDeviceQueueFamilyProperties(device, &queueFamilyCount, queueFamilies.data());
有關隊列簇,結構體VkQueueFamilyProperties包含了具體信息,包括支持的操作類型和基於當前隊列簇可以創建的有效隊列數。我們至少需要找到一個支持VK_QUEUE_GRAPHICS_BIT的隊列簇。
int i = 0; for (const auto& queueFamily : queueFamilies) { if (queueFamily.queueCount > 0 && queueFamily.queueFlags & VK_QUEUE_GRAPHICS_BIT) { indices.graphicsFamily = i; } if (indices.isComplete()) { break; } i++; }
現在我們有了比較理想的隊列簇查詢功能,我們可以在isDeviceSuitable函數中使用,確保物理設備可以處理我們需要的命令:
bool isDeviceSuitable(VkPhysicalDevice device) { QueueFamilyIndices indices = findQueueFamilies(device); return indices.isComplete(); }
很好,我們已經找到了我們需要的物理設備,在下一個小節我們會討論邏輯設備。
獲取工程代碼 GitHub checkout
