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-1-0.h"  // NOLINT(build/include)
34 #include "vk-wrappers-1-1.h"  // NOLINT(build/include)
35 };
36 
37 /// Wrapper around a Vulkan Device object.
38 class Device {
39  public:
40   Device(VkInstance instance,
41          VkPhysicalDevice physical_device,
42          uint32_t queue_family_index,
43          VkDevice device,
44          VkQueue queue);
45   virtual ~Device();
46 
47   Result Initialize(PFN_vkGetInstanceProcAddr getInstanceProcAddr,
48                     Delegate* delegate,
49                     const std::vector<std::string>& required_features,
50                     const std::vector<std::string>& required_device_extensions,
51                     const VkPhysicalDeviceFeatures& available_features,
52                     const VkPhysicalDeviceFeatures2KHR& available_features2,
53                     const std::vector<std::string>& available_extensions);
54 
55   /// Returns true if |format| and the |buffer|s buffer type combination is
56   /// supported by the physical device.
57   bool IsFormatSupportedByPhysicalDevice(const Format& format, BufferType type);
58 
GetVkDevice()59   VkDevice GetVkDevice() const { return device_; }
GetVkQueue()60   VkQueue GetVkQueue() const { return queue_; }
61   VkFormat GetVkFormat(const Format& format) const;
62 
GetQueueFamilyIndex()63   uint32_t GetQueueFamilyIndex() const { return queue_family_index_; }
64   uint32_t GetMaxPushConstants() const;
65 
66   /// Returns true if the given |descriptor_set| is within the bounds of
67   /// this device.
68   bool IsDescriptorSetInBounds(uint32_t descriptor_set) const;
69 
70   /// Returns true if the memory at |memory_type_index| has |flags| set.
71   virtual bool HasMemoryFlags(uint32_t memory_type_index,
72                               const VkMemoryPropertyFlags flags) const;
73   /// Returns true if the memory at |memory_type_index| is host accessible.
74   bool IsMemoryHostAccessible(uint32_t memory_type_index) const;
75   /// Returns true if the memory at |memory_type_index| is host coherent.
76   bool IsMemoryHostCoherent(uint32_t memory_type_index) const;
77 
78   /// Returns the pointers to the Vulkan API methods.
GetPtrs()79   virtual const VulkanPtrs* GetPtrs() const { return &ptrs_; }
80 
81   /// Returns true if the required subgroup size is supported for given stage
82   bool IsRequiredSubgroupSizeSupported(
83       const ShaderType type,
84       const uint32_t required_subgroup_size) const;
85   /// Returns the minimum required subgroup size or 0 if subgroup size control
86   /// is not supported.
87   uint32_t GetMinSubgroupSize() const;
88   /// Returns the maximum required subgroup size or 0 if subgroup size control
89   /// is not supported.
90   uint32_t GetMaxSubgroupSize() const;
91 
92  private:
93   Result LoadVulkanPointers(PFN_vkGetInstanceProcAddr, Delegate* delegate);
94   bool SupportsApiVersion(uint32_t major, uint32_t minor, uint32_t patch);
95 
96   VkInstance instance_ = VK_NULL_HANDLE;
97   VkPhysicalDevice physical_device_ = VK_NULL_HANDLE;
98   VkPhysicalDeviceProperties physical_device_properties_;
99   VkPhysicalDeviceMemoryProperties physical_memory_properties_;
100   VkPhysicalDeviceSubgroupSizeControlPropertiesEXT
101       subgroup_size_control_properties_;
102   VkDevice device_ = VK_NULL_HANDLE;
103   VkQueue queue_ = VK_NULL_HANDLE;
104   uint32_t queue_family_index_ = 0;
105 
106   VulkanPtrs ptrs_;
107 };
108 
109 }  // namespace vulkan
110 }  // namespace amber
111 
112 #endif  // SRC_VULKAN_DEVICE_H_
113