@@ -102,15 +102,26 @@ static std::optional<QueueIndexVK> PickQueue(const vk::PhysicalDevice& device,
102102 return std::nullopt ;
103103}
104104
105- std::shared_ptr<ContextVK> ContextVK::Create (Settings settings) {
105+ std::shared_ptr<ContextVK> ContextVK::DeviceSelection::CreateContext () {
106+ if (!device_holder) {
107+ return nullptr ;
108+ }
106109 auto context = std::shared_ptr<ContextVK>(new ContextVK (settings.flags ));
107- context->Setup (std::move (settings ));
110+ context->Setup (std::move (* this ));
108111 if (!context->IsValid ()) {
109112 return nullptr ;
110113 }
111114 return context;
112115}
113116
117+ std::shared_ptr<ContextVK> ContextVK::Create (Settings settings) {
118+ auto selection = SelectDevice (std::move (settings));
119+ if (!selection.ok ()) {
120+ return nullptr ;
121+ }
122+ return selection->CreateContext ();
123+ }
124+
114125// static
115126size_t ContextVK::ChooseThreadCountForWorkers (size_t hardware_concurrency) {
116127 // Never create more than 4 worker threads. Attempt to use up to
@@ -144,18 +155,17 @@ Context::BackendType ContextVK::GetBackendType() const {
144155/* version 2.0.0 */
145156static constexpr uint32_t kImpellerEngineVersion =
146157 VK_MAKE_API_VERSION (0 , 2 , 0 , 0 );
158+ static constexpr uint32_t kImpellerVulkanApiVersion = VK_API_VERSION_1_1 ;
147159
148- void ContextVK::Setup (Settings settings) {
149- TRACE_EVENT0 (" impeller" , " ContextVK::Setup" );
160+ absl::StatusOr<ContextVK::DeviceSelection> ContextVK::SelectDevice (
161+ Settings settings) {
162+ TRACE_EVENT0 (" impeller" , " ContextVK::SelectDevice" );
150163
151164 if (!settings.proc_address_callback ) {
152165 VALIDATION_LOG << " Missing proc address callback." ;
153- return ;
166+ return absl::InvalidArgumentError ( " Missing proc address callback. " ) ;
154167 }
155168
156- raster_message_loop_ = fml::ConcurrentMessageLoop::Create (
157- ChooseThreadCountForWorkers (std::thread::hardware_concurrency ()));
158-
159169 auto & dispatcher = VULKAN_HPP_DEFAULT_DISPATCHER ;
160170 dispatcher.init (settings.proc_address_callback );
161171
@@ -175,7 +185,7 @@ void ContextVK::Setup(Settings settings) {
175185
176186 if (!caps->IsValid ()) {
177187 VALIDATION_LOG << " Could not determine device capabilities." ;
178- return ;
188+ return absl::InternalError ( " Could not determine device capabilities. " ) ;
179189 }
180190
181191 gHasValidationLayers = caps->AreValidationsEnabled ();
@@ -185,7 +195,7 @@ void ContextVK::Setup(Settings settings) {
185195
186196 if (!enabled_layers.has_value () || !enabled_extensions.has_value ()) {
187197 VALIDATION_LOG << " Device has insufficient capabilities." ;
188- return ;
198+ return absl::InternalError ( " Device has insufficient capabilities. " ) ;
189199 }
190200
191201 vk::InstanceCreateFlags instance_flags = {};
@@ -220,7 +230,7 @@ void ContextVK::Setup(Settings settings) {
220230 //
221231 // variant, major, minor, patch
222232 application_info.setApplicationVersion (VK_API_VERSION_1_0 );
223- application_info.setApiVersion (VK_API_VERSION_1_1 );
233+ application_info.setApiVersion (kImpellerVulkanApiVersion );
224234 application_info.setEngineVersion (kImpellerEngineVersion );
225235 application_info.setPEngineName (" Impeller" );
226236 application_info.setPApplicationName (" Impeller" );
@@ -251,7 +261,7 @@ void ContextVK::Setup(Settings settings) {
251261 if (instance.result != vk::Result::eSuccess) {
252262 VALIDATION_LOG << " Could not create Vulkan instance: "
253263 << vk::to_string (instance.result );
254- return ;
264+ return absl::InternalError ( " Could not create Vulkan instance. " ) ;
255265 }
256266 device_holder->instance = std::move (instance.value );
257267 } else {
@@ -271,7 +281,7 @@ void ContextVK::Setup(Settings settings) {
271281
272282 if (!debug_report->IsValid ()) {
273283 VALIDATION_LOG << " Could not set up debug report." ;
274- return ;
284+ return absl::InternalError ( " Could not set up debug report. " ) ;
275285 }
276286
277287 // ----------------------------------------------------------------------------
@@ -282,13 +292,34 @@ void ContextVK::Setup(Settings settings) {
282292 PickPhysicalDevice (*caps, device_holder->instance .get ());
283293 if (!physical_device.has_value ()) {
284294 VALIDATION_LOG << " No valid Vulkan device found." ;
285- return ;
295+ return absl::NotFoundError ( " No valid Vulkan device found. " ) ;
286296 }
287297 device_holder->physical_device = physical_device.value ();
288298 } else {
289299 device_holder->physical_device = settings.embedder_data ->physical_device ;
290300 }
291301
302+ auto driver_info =
303+ std::make_unique<DriverInfoVK>(device_holder->physical_device );
304+
305+ return DeviceSelection{
306+ .settings = std::move (settings),
307+ .caps = std::move (caps),
308+ .device_holder = std::move (device_holder),
309+ .debug_report = std::move (debug_report),
310+ .driver_info = std::move (driver_info),
311+ };
312+ }
313+
314+ void ContextVK::Setup (DeviceSelection selection) {
315+ TRACE_EVENT0 (" impeller" , " ContextVK::Setup" );
316+
317+ Settings settings = std::move (selection.settings );
318+ auto caps = std::move (selection.caps );
319+ auto device_holder = std::move (selection.device_holder );
320+ auto debug_report = std::move (selection.debug_report );
321+ auto driver_info = std::move (selection.driver_info );
322+
292323 // ----------------------------------------------------------------------------
293324 // / Pick device queues.
294325 // /
@@ -368,7 +399,7 @@ void ContextVK::Setup(Settings settings) {
368399 // /
369400 auto allocator = std::shared_ptr<AllocatorVK>(new AllocatorVK (
370401 weak_from_this (), //
371- application_info. apiVersion , //
402+ kImpellerVulkanApiVersion , //
372403 device_holder->physical_device , //
373404 device_holder, //
374405 device_holder->instance .get (), //
@@ -381,8 +412,11 @@ void ContextVK::Setup(Settings settings) {
381412 }
382413
383414 // ----------------------------------------------------------------------------
384- // / Setup the pipeline library.
415+ // / Setup the worker threads and the pipeline library.
385416 // /
417+ raster_message_loop_ = fml::ConcurrentMessageLoop::Create (
418+ ChooseThreadCountForWorkers (std::thread::hardware_concurrency ()));
419+
386420 auto pipeline_library = std::shared_ptr<PipelineLibraryVK>(
387421 new PipelineLibraryVK (device_holder, //
388422 caps, //
@@ -457,20 +491,15 @@ void ContextVK::Setup(Settings settings) {
457491 return ;
458492 }
459493
460- VkPhysicalDeviceProperties physical_device_properties;
461- dispatcher.vkGetPhysicalDeviceProperties (device_holder->physical_device ,
462- &physical_device_properties);
463-
464494 // ----------------------------------------------------------------------------
465495 // / All done!
466496 // /
467497
468498 // Apply workarounds for broken drivers.
469- auto driver_info =
470- std::make_unique<DriverInfoVK>(device_holder->physical_device );
471499 workarounds_ = GetWorkaroundsFromDriverInfo (*driver_info);
472500 caps->ApplyWorkarounds (workarounds_);
473501
502+ device_name_ = driver_info->GetDriverName ();
474503 device_holder_ = std::move (device_holder);
475504 idle_waiter_vk_ = std::make_shared<IdleWaiterVK>(device_holder_);
476505 driver_info_ = std::move (driver_info);
@@ -487,7 +516,6 @@ void ContextVK::Setup(Settings settings) {
487516 resource_manager_ = std::move (resource_manager);
488517 command_pool_recycler_ = std::move (command_pool_recycler);
489518 descriptor_pool_recycler_ = std::move (descriptor_pool_recycler);
490- device_name_ = std::string (physical_device_properties.deviceName );
491519 command_queue_vk_ = std::make_shared<CommandQueueVK>(weak_from_this ());
492520 should_enable_surface_control_ = settings.enable_surface_control ;
493521 should_batch_cmd_buffers_ = !workarounds_.batch_submit_command_buffer_timeout ;
0 commit comments