Vulkan-Hpp
PhysicalDeviceMemoryProperties.cpp
Go to the documentation of this file.
1 // Copyright(c) 2019, NVIDIA CORPORATION. All rights reserved.
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 // VulkanHpp Samples : PhysicalDeviceMemoryProperties
16 // Get memory properties per physical device.
17 
18 #include "../utils/utils.hpp"
19 
20 #include <sstream>
21 #include <vector>
23 
24 static char const * AppName = "PhysicalDeviceMemoryProperties";
25 static char const * EngineName = "Vulkan.hpp";
26 
27 std::string formatSize( vk::DeviceSize size )
28 {
29  std::ostringstream oss;
30  if ( size < 1024 )
31  {
32  oss << size << " B";
33  }
34  else if ( size < 1024 * 1024 )
35  {
36  oss << size / 1024.f << " KB";
37  }
38  else if ( size < 1024 * 1024 * 1024 )
39  {
40  oss << size / ( 1024.0f * 1024.0f ) << " MB";
41  }
42  else
43  {
44  oss << size / ( 1024.0f * 1024.0f * 1024.0f ) << " GB";
45  }
46  return oss.str();
47 }
48 
49 int main( int /*argc*/, char ** /*argv*/ )
50 {
51  try
52  {
53  vk::Instance instance = vk::su::createInstance( AppName, EngineName, {}, {}, VK_API_VERSION_1_1 );
54 #if !defined( NDEBUG )
56 #endif
57 
58  // enumerate the physicalDevices
59  std::vector<vk::PhysicalDevice> physicalDevices = instance.enumeratePhysicalDevices();
60 
61  /* VULKAN_KEY_START */
62 
63  for ( size_t i = 0; i < physicalDevices.size(); i++ )
64  {
65  // some properties are only valid, if a corresponding extension is available!
66  std::vector<vk::ExtensionProperties> extensionProperties = physicalDevices[i].enumerateDeviceExtensionProperties();
67  bool containsMemoryBudget = vk::su::contains( extensionProperties, "VK_EXT_memory_budget" );
68 
69  std::cout << "PhysicalDevice " << i << "\n";
70  auto memoryProperties2 = physicalDevices[i].getMemoryProperties2<vk::PhysicalDeviceMemoryProperties2, vk::PhysicalDeviceMemoryBudgetPropertiesEXT>();
71  vk::PhysicalDeviceMemoryProperties const & memoryProperties = memoryProperties2.get<vk::PhysicalDeviceMemoryProperties2>().memoryProperties;
72  vk::PhysicalDeviceMemoryBudgetPropertiesEXT const & memoryBudgetProperties = memoryProperties2.get<vk::PhysicalDeviceMemoryBudgetPropertiesEXT>();
73  std::cout << "memoryHeapCount: " << memoryProperties.memoryHeapCount << "\n";
74  for ( uint32_t j = 0; j < memoryProperties.memoryHeapCount; j++ )
75  {
76  std::cout << " " << j << ": size = " << formatSize( memoryProperties.memoryHeaps[j].size )
77  << ", flags = " << vk::to_string( memoryProperties.memoryHeaps[j].flags ) << "\n";
78  if ( containsMemoryBudget )
79  {
80  std::cout << " heapBudget = " << formatSize( memoryBudgetProperties.heapBudget[j] )
81  << ", heapUsage = " << formatSize( memoryBudgetProperties.heapUsage[j] ) << "\n";
82  }
83  }
84  std::cout << "memoryTypeCount: " << memoryProperties.memoryTypeCount << "\n";
85  for ( uint32_t j = 0; j < memoryProperties.memoryTypeCount; j++ )
86  {
87  std::cout << " " << j << ": heapIndex = " << memoryProperties.memoryTypes[j].heapIndex
88  << ", flags = " << vk::to_string( memoryProperties.memoryTypes[j].propertyFlags ) << "\n";
89  }
90  }
91 
92  /* VULKAN_KEY_END */
93 
94 #if !defined( NDEBUG )
95  instance.destroyDebugUtilsMessengerEXT( debugUtilsMessenger );
96 #endif
97  instance.destroy();
98  }
99  catch ( vk::SystemError & err )
100  {
101  std::cout << "vk::SystemError: " << err.what() << std::endl;
102  exit( -1 );
103  }
104  catch ( std::exception & err )
105  {
106  std::cout << "std::exception: " << err.what() << std::endl;
107  exit( -1 );
108  }
109  catch ( ... )
110  {
111  std::cout << "unknown error\n";
112  exit( -1 );
113  }
114  return 0;
115 }
int main(int, char **)
std::string formatSize(vk::DeviceSize size)
void cout(vk::SurfaceCapabilitiesKHR const &surfaceCapabilities)
VULKAN_HPP_NODISCARD Result createDebugUtilsMessengerEXT(const vk::DebugUtilsMessengerCreateInfoEXT *pCreateInfo, const vk::AllocationCallbacks *pAllocator, vk::DebugUtilsMessengerEXT *pMessenger, Dispatch const &d VULKAN_HPP_DEFAULT_DISPATCHER_ASSIGNMENT) const VULKAN_HPP_NOEXCEPT
void destroy(const vk::AllocationCallbacks *pAllocator, Dispatch const &d VULKAN_HPP_DEFAULT_DISPATCHER_ASSIGNMENT) const VULKAN_HPP_NOEXCEPT
VULKAN_HPP_NODISCARD Result enumeratePhysicalDevices(uint32_t *pPhysicalDeviceCount, vk::PhysicalDevice *pPhysicalDevices, Dispatch const &d VULKAN_HPP_DEFAULT_DISPATCHER_ASSIGNMENT) const VULKAN_HPP_NOEXCEPT
void destroyDebugUtilsMessengerEXT(vk::DebugUtilsMessengerEXT messenger, const vk::AllocationCallbacks *pAllocator, Dispatch const &d VULKAN_HPP_DEFAULT_DISPATCHER_ASSIGNMENT) const VULKAN_HPP_NOEXCEPT
virtual const char * what() const VULKAN_HPP_NOEXCEPT
Definition: vulkan.hpp:6206
vk::DebugUtilsMessengerCreateInfoEXT makeDebugUtilsMessengerCreateInfoEXT()
Definition: utils.cpp:1025
vk::Instance createInstance(std::string const &appName, std::string const &engineName, std::vector< std::string > const &layers, std::vector< std::string > const &extensions, uint32_t apiVersion)
Definition: utils.cpp:279
bool contains(std::vector< vk::ExtensionProperties > const &extensionProperties, std::string const &extensionName)
Definition: utils.cpp:54
VULKAN_HPP_INLINE std::string to_string(FormatFeatureFlags value)
uint64_t DeviceSize
Definition: vulkan.hpp:6122
#define VK_API_VERSION_1_1
Definition: vulkan_core.h:4854