Skip to content

Commit f501e9d

Browse files
committed
I think, this code better shows the logic followed here, to determine the two queue indices.
If it looks good, I can again carry that over to all the other chapters.
1 parent 3d5207c commit f501e9d

File tree

1 file changed

+35
-43
lines changed

1 file changed

+35
-43
lines changed

attachments/05_window_surface.cpp

Lines changed: 35 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -201,53 +201,45 @@ class HelloTriangleApplication {
201201
}
202202

203203
void createLogicalDevice() {
204-
// find the index of the first queue family that supports graphics
205-
std::vector<vk::QueueFamilyProperties> queueFamilyProperties = physicalDevice.getQueueFamilyProperties();
206-
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() )
204+
// get the first index into queueFamilyProperties which supports both graphics and present
205+
uint32_t graphicsIndex = ~0, presentIndex = ~0;
206+
for (uint32_t queueIndex = 0; queueIndex < queueFamilyProperties.size(); queueIndex++)
219207
{
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-
}
208+
if ((queueFamilyProperties[queueIndex].queueFlags & vk::QueueFlagBits::eGraphics) &&
209+
physicalDevice.getSurfaceSupportKHR(queueIndex, *surface))
210+
{
211+
// found a queue family that supports both graphics and present
212+
graphicsIndex = queueIndex;
213+
presentIndex = queueIndex;
214+
break;
215+
}
245216
}
246-
if ( ( graphicsIndex == queueFamilyProperties.size() ) || ( presentIndex == queueFamilyProperties.size() ) )
217+
if (graphicsIndex == ~0)
247218
{
248-
throw std::runtime_error( "Could not find a queue for graphics or present -> terminating" );
249-
}
219+
// no queue family that supports both graphics and present found -> look for a queue family that supports graphics only
220+
auto graphicsQueueFamilyProperty = std::ranges::find_if(queueFamilyProperties, [](auto const& qfp)
221+
{ return (qfp.queueFlags & vk::QueueFlagBits::eGraphics) != static_cast<vk::QueueFlags>(0); });
222+
if (graphicsQueueFamilyProperty == queueFamilyProperties.end())
223+
{
224+
throw std::runtime_error("Could not find a queue for graphics -> terminating");
225+
}
226+
graphicsIndex = static_cast<uint32_t>(std::distance(queueFamilyProperties.begin(), graphicsQueueFamilyProperty));
250227

228+
// now look for a queue family that supports present
229+
for (uint32_t queueIndex = 0; queueIndex < queueFamilyProperties.size(); queueIndex++)
230+
{
231+
if (physicalDevice.getSurfaceSupportKHR(queueIndex, *surface))
232+
{
233+
presentIndex = queueIndex;
234+
break;
235+
}
236+
}
237+
if (presentIndex == ~0)
238+
{
239+
throw std::runtime_error("Could not find a queue for present -> terminating");
240+
}
241+
}
242+
251243
// query for Vulkan 1.3 features
252244
vk::StructureChain<vk::PhysicalDeviceFeatures2, vk::PhysicalDeviceVulkan13Features, vk::PhysicalDeviceExtendedDynamicStateFeaturesEXT> featureChain = {
253245
{}, // vk::PhysicalDeviceFeatures2

0 commit comments

Comments
 (0)