1 // Copyright 2018 The Amber Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #ifndef SRC_VULKAN_DEVICE_H_ 16 #define SRC_VULKAN_DEVICE_H_ 17 18 #include <functional> 19 #include <memory> 20 #include <string> 21 #include <vector> 22 23 #include "amber/amber.h" 24 #include "amber/result.h" 25 #include "amber/vulkan_header.h" 26 #include "src/buffer.h" 27 #include "src/format.h" 28 29 namespace amber { 30 namespace vulkan { 31 32 struct VulkanPtrs { 33 #include "vk-wrappers.h" // NOLINT(build/include) 34 }; 35 36 /// Wrapper around a Vulkan Device object. 37 class Device { 38 public: 39 Device(VkInstance instance, 40 VkPhysicalDevice physical_device, 41 uint32_t queue_family_index, 42 VkDevice device, 43 VkQueue queue); 44 ~Device(); 45 46 Result Initialize(PFN_vkGetInstanceProcAddr getInstanceProcAddr, 47 Delegate* delegate, 48 const std::vector<std::string>& required_features, 49 const std::vector<std::string>& required_extensions, 50 const VkPhysicalDeviceFeatures& available_features, 51 const VkPhysicalDeviceFeatures2KHR& available_features2, 52 const std::vector<std::string>& available_extensions); 53 54 /// Returns true if |format| and the |buffer|s buffer type combination is 55 /// supported by the physical device. 56 bool IsFormatSupportedByPhysicalDevice(const Format& format, Buffer* buffer); 57 GetVkDevice()58 VkDevice GetVkDevice() const { return device_; } GetVkQueue()59 VkQueue GetVkQueue() const { return queue_; } 60 VkFormat GetVkFormat(const Format& format) const; 61 GetQueueFamilyIndex()62 uint32_t GetQueueFamilyIndex() const { return queue_family_index_; } 63 uint32_t GetMaxPushConstants() const; 64 65 /// Returns true if the given |descriptor_set| is within the bounds of 66 /// this device. 67 bool IsDescriptorSetInBounds(uint32_t descriptor_set) const; 68 69 /// Returns true if the memory at |memory_type_index| has |flags| set. 70 bool HasMemoryFlags(uint32_t memory_type_index, 71 const VkMemoryPropertyFlags flags) const; 72 /// Returns true if the memory at |memory_type_index| is host accessible. 73 bool IsMemoryHostAccessible(uint32_t memory_type_index) const; 74 /// Returns true if the memory at |memory_type_index| is host corherent. 75 bool IsMemoryHostCoherent(uint32_t memory_type_index) const; 76 77 /// Returns the pointers to the Vulkan API methods. GetPtrs()78 const VulkanPtrs* GetPtrs() const { return &ptrs_; } 79 80 private: 81 Result LoadVulkanPointers(PFN_vkGetInstanceProcAddr, Delegate* delegate); 82 83 VkInstance instance_ = VK_NULL_HANDLE; 84 VkPhysicalDevice physical_device_ = VK_NULL_HANDLE; 85 VkPhysicalDeviceProperties physical_device_properties_; 86 VkPhysicalDeviceMemoryProperties physical_memory_properties_; 87 VkDevice device_ = VK_NULL_HANDLE; 88 VkQueue queue_ = VK_NULL_HANDLE; 89 uint32_t queue_family_index_ = 0; 90 91 VulkanPtrs ptrs_; 92 }; 93 94 } // namespace vulkan 95 } // namespace amber 96 97 #endif // SRC_VULKAN_DEVICE_H_ 98