Pyrogenesis  trunk
DeviceSelection.h
Go to the documentation of this file.
1 /* Copyright (C) 2023 Wildfire Games.
2  * This file is part of 0 A.D.
3  *
4  * 0 A.D. is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * 0 A.D. is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef INCLUDED_RENDERER_BACKEND_VULKAN_DEVICESELECTION
19 #define INCLUDED_RENDERER_BACKEND_VULKAN_DEVICESELECTION
20 
22 
23 #include <glad/vulkan.h>
24 #include <limits>
25 #include <vector>
26 
27 namespace Renderer
28 {
29 
30 namespace Backend
31 {
32 
33 namespace Vulkan
34 {
35 
36 /**
37  * Structure to store all information that might be useful on device selection.
38  */
40 {
41  uint32_t index = std::numeric_limits<uint32_t>::max();
42  VkPhysicalDevice device = VK_NULL_HANDLE;
43  VkPhysicalDeviceProperties properties{};
44  VkPhysicalDeviceDescriptorIndexingPropertiesEXT descriptorIndexingProperties{};
45  VkPhysicalDeviceMemoryProperties memoryProperties{};
46  VkPhysicalDeviceFeatures features{};
47  VkPhysicalDeviceDescriptorIndexingFeaturesEXT descriptorIndexingFeatures{};
48  std::vector<VkQueueFamilyProperties> queueFamilies;
49  bool hasRequiredExtensions = false;
53  VkDeviceSize deviceTotalMemory = 0;
54  VkDeviceSize hostTotalMemory = 0;
55  std::vector<std::string> extensions;
56  VkSurfaceCapabilitiesKHR surfaceCapabilities;
57  std::vector<VkSurfaceFormatKHR> surfaceFormats;
58  std::vector<VkPresentModeKHR> presentModes;
59 };
60 
61 /**
62  * @return all available physical devices for the Vulkan instance with
63  * additional flags of surface and required extensions support.
64  * We could have a single function that returns a selected device. But we use
65  * multiple functions to be able to save some information about available
66  * devices before filtering and give a choice to a user.
67  */
68 std::vector<SAvailablePhysicalDevice> GetAvailablePhysicalDevices(
69  VkInstance instance, VkSurfaceKHR surface,
70  const std::vector<const char*>& requiredDeviceExtensions);
71 
72 /**
73  * @return true if we can't use the device for our needs. For example, it
74  * doesn't graphics or present queues. Because we can't render the game without
75  * them.
76  */
78 
79 /**
80  * @return true if the first device is better for our needs than the second
81  * one. Useful in functions like std::sort. The first and the second devices
82  * should be supported (in other words IsPhysicalDeviceSupported should
83  * return true for both of them).
84  */
86  const SAvailablePhysicalDevice& device1,
87  const SAvailablePhysicalDevice& device2);
88 
90  const VkSurfaceFormatKHR& surfaceFormat);
91 
92 /**
93  * Report all desired information about the available physical device.
94  */
96  const ScriptRequest& rq, JS::HandleValue settings);
97 
98 } // namespace Vulkan
99 
100 } // namespace Backend
101 
102 } // namespace Renderer
103 
104 #endif // INCLUDED_RENDERER_BACKEND_VULKAN_DEVICESELECTION
VkDeviceSize deviceTotalMemory
Definition: DeviceSelection.h:53
std::vector< VkSurfaceFormatKHR > surfaceFormats
Definition: DeviceSelection.h:57
bool IsSurfaceFormatSupported(const VkSurfaceFormatKHR &surfaceFormat)
Definition: DeviceSelection.cpp:322
bool hasOutputToSurfaceSupport
Definition: DeviceSelection.h:50
uint32_t index
Definition: DeviceSelection.h:41
std::vector< VkPresentModeKHR > presentModes
Definition: DeviceSelection.h:58
VkPhysicalDeviceDescriptorIndexingFeaturesEXT descriptorIndexingFeatures
Definition: DeviceSelection.h:47
void ReportAvailablePhysicalDevice(const SAvailablePhysicalDevice &device, const ScriptRequest &rq, JS::HandleValue settings)
Report all desired information about the available physical device.
Definition: DeviceSelection.cpp:331
std::vector< std::string > extensions
Definition: DeviceSelection.h:55
bool ComparePhysicalDevices(const SAvailablePhysicalDevice &device1, const SAvailablePhysicalDevice &device2)
Definition: DeviceSelection.cpp:309
bool IsPhysicalDeviceUnsupported(const SAvailablePhysicalDevice &device)
Definition: DeviceSelection.cpp:291
VkPhysicalDeviceFeatures features
Definition: DeviceSelection.h:46
VkPhysicalDeviceProperties properties
Definition: DeviceSelection.h:43
VkSurfaceCapabilitiesKHR surfaceCapabilities
Definition: DeviceSelection.h:56
VkPhysicalDeviceDescriptorIndexingPropertiesEXT descriptorIndexingProperties
Definition: DeviceSelection.h:44
std::vector< SAvailablePhysicalDevice > GetAvailablePhysicalDevices(VkInstance instance, VkSurfaceKHR surface, const std::vector< const char *> &requiredDeviceExtensions)
Definition: DeviceSelection.cpp:268
Backend
Definition: Backend.h:27
size_t graphicsQueueFamilyIndex
Definition: DeviceSelection.h:51
VkDeviceSize hostTotalMemory
Definition: DeviceSelection.h:54
unsigned int uint32_t
Definition: wposix_types.h:53
Definition: VideoMode.h:28
VkPhysicalDeviceMemoryProperties memoryProperties
Definition: DeviceSelection.h:45
bool hasRequiredExtensions
Definition: DeviceSelection.h:49
VkPhysicalDevice device
Definition: DeviceSelection.h:42
std::vector< VkQueueFamilyProperties > queueFamilies
Definition: DeviceSelection.h:48
Spidermonkey maintains some &#39;local&#39; state via the JSContext* object.
Definition: ScriptRequest.h:59
Structure to store all information that might be useful on device selection.
Definition: DeviceSelection.h:39
size_t presentQueueFamilyIndex
Definition: DeviceSelection.h:52