@@ -201,51 +201,45 @@ class HelloTriangleApplication {
201201 }
202202
203203 void createLogicalDevice () {
204- // find the index of the first queue family that supports graphics
205204 std::vector<vk::QueueFamilyProperties> queueFamilyProperties = physicalDevice.getQueueFamilyProperties ();
206205
207- // get the first index into queueFamilyProperties which supports graphics
208- auto graphicsQueueFamilyProperty = std::ranges::find_if ( queueFamilyProperties, []( auto const & qfp )
209- { return (qfp.queueFlags & vk::QueueFlagBits::eGraphics) != static_cast <vk::QueueFlags>(0 ); } );
210-
211- auto graphicsIndex = static_cast <uint32_t >( std::distance ( queueFamilyProperties.begin (), graphicsQueueFamilyProperty ) );
212-
213- // determine a queueFamilyIndex that supports present
214- // first check if the graphicsIndex is good enough
215- auto presentIndex = physicalDevice.getSurfaceSupportKHR ( graphicsIndex, *surface )
216- ? graphicsIndex
217- : ~0 ;
218- if ( presentIndex == queueFamilyProperties.size () )
206+ // get the first index into queueFamilyProperties which supports both graphics and present
207+ uint32_t graphicsIndex = ~0 , presentIndex = ~0 ;
208+ for (uint32_t queueIndex = 0 ; queueIndex < queueFamilyProperties.size (); queueIndex++)
219209 {
220- // the graphicsIndex doesn't support present -> look for another family index that supports both
221- // graphics and present
222- for ( size_t i = 0 ; i < queueFamilyProperties.size (); i++ )
223- {
224- if ( ( queueFamilyProperties[i].queueFlags & vk::QueueFlagBits::eGraphics ) &&
225- physicalDevice.getSurfaceSupportKHR ( static_cast <uint32_t >( i ), *surface ) )
226- {
227- graphicsIndex = static_cast <uint32_t >( i );
228- presentIndex = graphicsIndex;
229- break ;
230- }
231- }
232- if ( presentIndex == queueFamilyProperties.size () )
233- {
234- // there's nothing like a single family index that supports both graphics and present -> look for another
235- // family index that supports present
236- for ( size_t i = 0 ; i < queueFamilyProperties.size (); i++ )
237- {
238- if ( physicalDevice.getSurfaceSupportKHR ( static_cast <uint32_t >( i ), *surface ) )
239- {
240- presentIndex = static_cast <uint32_t >( i );
241- break ;
242- }
243- }
244- }
210+ if ((queueFamilyProperties[queueIndex].queueFlags & vk::QueueFlagBits::eGraphics) &&
211+ physicalDevice.getSurfaceSupportKHR (queueIndex, *surface))
212+ {
213+ // found a queue family that supports both graphics and present
214+ graphicsIndex = queueIndex;
215+ presentIndex = queueIndex;
216+ break ;
217+ }
245218 }
246- if ( ( graphicsIndex == queueFamilyProperties. size () ) || ( presentIndex == queueFamilyProperties. size () ) )
219+ if (graphicsIndex == ~ 0 )
247220 {
248- throw std::runtime_error ( " Could not find a queue for graphics or present -> terminating" );
221+ // no queue family that supports both graphics and present found -> look for a queue family that supports graphics only
222+ auto graphicsQueueFamilyProperty = std::ranges::find_if (queueFamilyProperties, [](auto const & qfp)
223+ { return (qfp.queueFlags & vk::QueueFlagBits::eGraphics) != static_cast <vk::QueueFlags>(0 ); });
224+ if (graphicsQueueFamilyProperty == queueFamilyProperties.end ())
225+ {
226+ throw std::runtime_error (" Could not find a queue for graphics -> terminating" );
227+ }
228+ graphicsIndex = static_cast <uint32_t >(std::distance (queueFamilyProperties.begin (), graphicsQueueFamilyProperty));
229+
230+ // now look for a queue family that supports present
231+ for (uint32_t queueIndex = 0 ; queueIndex < queueFamilyProperties.size (); queueIndex++)
232+ {
233+ if (physicalDevice.getSurfaceSupportKHR (queueIndex, *surface))
234+ {
235+ presentIndex = queueIndex;
236+ break ;
237+ }
238+ }
239+ if (presentIndex == ~0 )
240+ {
241+ throw std::runtime_error (" Could not find a queue for present -> terminating" );
242+ }
249243 }
250244
251245 // query for Vulkan 1.3 features
0 commit comments