MCPcopy Create free account
hub / github.com/KhronosGroup/Vulkan-Samples / create_instance

Method create_instance

samples/tooling/profiles/profiles.cpp:140–209  ·  view source on GitHub ↗

This sample overrides the instance creation part of the framework Instead of manually setting up all properties we use the Vulkan Profiles library to simplify instance setup

Source from the content-addressed store, hash-verified

138// This sample overrides the instance creation part of the framework
139// Instead of manually setting up all properties we use the Vulkan Profiles library to simplify instance setup
140std::unique_ptr<vkb::core::InstanceC> Profiles::create_instance()
141{
142 // Initialize Volk Vulkan Loader
143 VkResult result = volkInitialize();
144 if (result)
145 {
146 throw vkb::VulkanException(result, "Failed to initialize volk.");
147 }
148
149 // Check if the profile is supported at instance level
150 VkBool32 profile_supported;
151 vpGetInstanceProfileSupport(nullptr, &profile_properties, &profile_supported);
152 if (!profile_supported)
153 {
154 throw std::runtime_error{"The selected profile is not supported (error at creating the instance)!"};
155 }
156
157 // Even when using profiles we still need to provide the platform specific get_surface() extension
158 std::vector<const char *> enabled_extensions;
159 enabled_extensions.push_back(VK_KHR_SURFACE_EXTENSION_NAME);
160
161 for (const char *extension_name : window->get_required_surface_extensions())
162 {
163 enabled_extensions.push_back(extension_name);
164 }
165
166 VkInstanceCreateInfo create_info{};
167
168#if (defined(VKB_ENABLE_PORTABILITY))
169 enabled_extensions.push_back(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME);
170
171 // Enumerate all instance extensions for the loader + driver to determine if VK_KHR_portability_enumeration is available
172 uint32_t instance_extension_count;
173 VK_CHECK(vkEnumerateInstanceExtensionProperties(nullptr, &instance_extension_count, nullptr));
174 std::vector<VkExtensionProperties> available_instance_extensions(instance_extension_count);
175 VK_CHECK(vkEnumerateInstanceExtensionProperties(nullptr, &instance_extension_count, available_instance_extensions.data()));
176
177 // If VK_KHR_portability_enumeration is available in the implementation, then we must enable the extension
178 if (std::ranges::any_of(available_instance_extensions,
179 [](VkExtensionProperties const &extension) { return strcmp(extension.extensionName, VK_KHR_PORTABILITY_ENUMERATION_EXTENSION_NAME) == 0; }))
180 {
181 enabled_extensions.push_back(VK_KHR_PORTABILITY_ENUMERATION_EXTENSION_NAME);
182 create_info.flags |= VK_INSTANCE_CREATE_ENUMERATE_PORTABILITY_BIT_KHR;
183 }
184#endif
185
186 create_info.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
187 create_info.ppEnabledExtensionNames = enabled_extensions.data();
188 create_info.enabledExtensionCount = static_cast<uint32_t>(enabled_extensions.size());
189 // Note: We don't explicitly set an application info here so the one from the profile is used
190 // This also defines the api version to be used
191
192 // Create the instance using the profiles library
193 VpInstanceCreateInfo instance_create_info{};
194 instance_create_info.pEnabledFullProfiles = &profile_properties;
195 instance_create_info.enabledFullProfileCount = 1;
196 instance_create_info.pCreateInfo = &create_info;
197 VkInstance vulkan_instance;

Callers

nothing calls this directly

Calls 3

VulkanExceptionClass · 0.85
sizeMethod · 0.80

Tested by

no test coverage detected