Skip to content

Commit e7d8661

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 e7d8661

File tree

1 file changed

+23
-56
lines changed

1 file changed

+23
-56
lines changed

attachments/05_window_surface.cpp

Lines changed: 23 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -40,18 +40,14 @@ class HelloTriangleApplication {
4040
}
4141

4242
private:
43-
GLFWwindow* window = nullptr;
44-
45-
vk::raii::Context context;
46-
vk::raii::Instance instance = nullptr;
43+
GLFWwindow * window = nullptr;
44+
vk::raii::Context context;
45+
vk::raii::Instance instance = nullptr;
4746
vk::raii::DebugUtilsMessengerEXT debugMessenger = nullptr;
48-
vk::raii::SurfaceKHR surface = nullptr;
49-
50-
vk::raii::PhysicalDevice physicalDevice = nullptr;
51-
vk::raii::Device device = nullptr;
52-
53-
vk::raii::Queue graphicsQueue = nullptr;
54-
vk::raii::Queue presentQueue = nullptr;
47+
vk::raii::SurfaceKHR surface = nullptr;
48+
vk::raii::PhysicalDevice physicalDevice = nullptr;
49+
vk::raii::Device device = nullptr;
50+
vk::raii::Queue queue = nullptr;
5551

5652
std::vector<const char*> requiredDeviceExtension = {
5753
vk::KHRSwapchainExtensionName,
@@ -201,53 +197,25 @@ class HelloTriangleApplication {
201197
}
202198

203199
void createLogicalDevice() {
204-
// find the index of the first queue family that supports graphics
205-
std::vector<vk::QueueFamilyProperties> queueFamilyProperties = physicalDevice.getQueueFamilyProperties();
200+
std::vector<vk::QueueFamilyProperties> queueFamilyProperties = physicalDevice.getQueueFamilyProperties();
206201

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() )
202+
// get the first index into queueFamilyProperties which supports both graphics and present
203+
uint32_t queueIndex = ~0;
204+
for (uint32_t qfpIndex = 0; qfpIndex < queueFamilyProperties.size(); qfpIndex++)
219205
{
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-
}
206+
if ((queueFamilyProperties[qfpIndex].queueFlags & vk::QueueFlagBits::eGraphics) &&
207+
physicalDevice.getSurfaceSupportKHR(qfpIndex, *surface))
208+
{
209+
// found a queue family that supports both graphics and present
210+
queueIndex = qfpIndex;
211+
break;
212+
}
245213
}
246-
if ( ( graphicsIndex == queueFamilyProperties.size() ) || ( presentIndex == queueFamilyProperties.size() ) )
214+
if (queueIndex == ~0)
247215
{
248-
throw std::runtime_error( "Could not find a queue for graphics or present -> terminating" );
216+
throw std::runtime_error("Could not find a queue for graphics and present -> terminating");
249217
}
250-
218+
251219
// query for Vulkan 1.3 features
252220
vk::StructureChain<vk::PhysicalDeviceFeatures2, vk::PhysicalDeviceVulkan13Features, vk::PhysicalDeviceExtendedDynamicStateFeaturesEXT> featureChain = {
253221
{}, // vk::PhysicalDeviceFeatures2
@@ -257,16 +225,15 @@ class HelloTriangleApplication {
257225

258226
// create a Device
259227
float queuePriority = 0.0f;
260-
vk::DeviceQueueCreateInfo deviceQueueCreateInfo{ .queueFamilyIndex = graphicsIndex, .queueCount = 1, .pQueuePriorities = &queuePriority };
228+
vk::DeviceQueueCreateInfo deviceQueueCreateInfo{ .queueFamilyIndex = queueIndex, .queueCount = 1, .pQueuePriorities = &queuePriority };
261229
vk::DeviceCreateInfo deviceCreateInfo{ .pNext = &featureChain.get<vk::PhysicalDeviceFeatures2>(),
262230
.queueCreateInfoCount = 1,
263231
.pQueueCreateInfos = &deviceQueueCreateInfo,
264232
.enabledExtensionCount = static_cast<uint32_t>(requiredDeviceExtension.size()),
265233
.ppEnabledExtensionNames = requiredDeviceExtension.data() };
266234

267235
device = vk::raii::Device( physicalDevice, deviceCreateInfo );
268-
graphicsQueue = vk::raii::Queue( device, graphicsIndex, 0 );
269-
presentQueue = vk::raii::Queue( device, presentIndex, 0 );
236+
queue = vk::raii::Queue( device, queueIndex, 0 );
270237
}
271238

272239
std::vector<const char*> getRequiredExtensions() {

0 commit comments

Comments
 (0)