Skip to content

Commit 01ab96a

Browse files
committed
2 parents 47523e8 + 090ecd6 commit 01ab96a

31 files changed

+754
-1810
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() {

attachments/06_swap_chain_creation.cpp

Lines changed: 19 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,14 @@ class HelloTriangleApplication {
4141
}
4242

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

5753
vk::raii::SwapchainKHR swapChain = nullptr;
5854
std::vector<vk::Image> swapChainImages;
@@ -209,51 +205,23 @@ class HelloTriangleApplication {
209205
}
210206

211207
void createLogicalDevice() {
212-
// find the index of the first queue family that supports graphics
213208
std::vector<vk::QueueFamilyProperties> queueFamilyProperties = physicalDevice.getQueueFamilyProperties();
214209

215-
// get the first index into queueFamilyProperties which supports graphics
216-
auto graphicsQueueFamilyProperty = std::ranges::find_if( queueFamilyProperties, []( auto const & qfp )
217-
{ return (qfp.queueFlags & vk::QueueFlagBits::eGraphics) != static_cast<vk::QueueFlags>(0); } );
218-
219-
auto graphicsIndex = static_cast<uint32_t>( std::distance( queueFamilyProperties.begin(), graphicsQueueFamilyProperty ) );
220-
221-
// determine a queueFamilyIndex that supports present
222-
// first check if the graphicsIndex is good enough
223-
auto presentIndex = physicalDevice.getSurfaceSupportKHR( graphicsIndex, *surface )
224-
? graphicsIndex
225-
: ~0;
226-
if ( presentIndex == queueFamilyProperties.size() )
210+
// get the first index into queueFamilyProperties which supports both graphics and present
211+
uint32_t queueIndex = ~0;
212+
for (uint32_t qfpIndex = 0; qfpIndex < queueFamilyProperties.size(); qfpIndex++)
227213
{
228-
// the graphicsIndex doesn't support present -> look for another family index that supports both
229-
// graphics and present
230-
for ( size_t i = 0; i < queueFamilyProperties.size(); i++ )
231-
{
232-
if ( ( queueFamilyProperties[i].queueFlags & vk::QueueFlagBits::eGraphics ) &&
233-
physicalDevice.getSurfaceSupportKHR( static_cast<uint32_t>( i ), *surface ) )
234-
{
235-
graphicsIndex = static_cast<uint32_t>( i );
236-
presentIndex = graphicsIndex;
237-
break;
238-
}
239-
}
240-
if ( presentIndex == queueFamilyProperties.size() )
214+
if ((queueFamilyProperties[qfpIndex].queueFlags & vk::QueueFlagBits::eGraphics) &&
215+
physicalDevice.getSurfaceSupportKHR(qfpIndex, *surface))
241216
{
242-
// there's nothing like a single family index that supports both graphics and present -> look for another
243-
// family index that supports present
244-
for ( size_t i = 0; i < queueFamilyProperties.size(); i++ )
245-
{
246-
if ( physicalDevice.getSurfaceSupportKHR( static_cast<uint32_t>( i ), *surface ) )
247-
{
248-
presentIndex = static_cast<uint32_t>( i );
249-
break;
250-
}
251-
}
217+
// found a queue family that supports both graphics and present
218+
queueIndex = qfpIndex;
219+
break;
252220
}
253221
}
254-
if ( ( graphicsIndex == queueFamilyProperties.size() ) || ( presentIndex == queueFamilyProperties.size() ) )
222+
if (queueIndex == ~0)
255223
{
256-
throw std::runtime_error( "Could not find a queue for graphics or present -> terminating" );
224+
throw std::runtime_error("Could not find a queue for graphics and present -> terminating");
257225
}
258226

259227
// query for Vulkan 1.3 features
@@ -265,16 +233,15 @@ class HelloTriangleApplication {
265233

266234
// create a Device
267235
float queuePriority = 0.0f;
268-
vk::DeviceQueueCreateInfo deviceQueueCreateInfo{ .queueFamilyIndex = graphicsIndex, .queueCount = 1, .pQueuePriorities = &queuePriority };
236+
vk::DeviceQueueCreateInfo deviceQueueCreateInfo{ .queueFamilyIndex = queueIndex, .queueCount = 1, .pQueuePriorities = &queuePriority };
269237
vk::DeviceCreateInfo deviceCreateInfo{ .pNext = &featureChain.get<vk::PhysicalDeviceFeatures2>(),
270238
.queueCreateInfoCount = 1,
271239
.pQueueCreateInfos = &deviceQueueCreateInfo,
272240
.enabledExtensionCount = static_cast<uint32_t>(requiredDeviceExtension.size()),
273241
.ppEnabledExtensionNames = requiredDeviceExtension.data() };
274242

275243
device = vk::raii::Device( physicalDevice, deviceCreateInfo );
276-
graphicsQueue = vk::raii::Queue( device, graphicsIndex, 0 );
277-
presentQueue = vk::raii::Queue( device, presentIndex, 0 );
244+
queue = vk::raii::Queue( device, queueIndex, 0 );
278245
}
279246

280247
void createSwapChain() {

attachments/07_image_views.cpp

Lines changed: 19 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,14 @@ class HelloTriangleApplication {
4141
}
4242

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

5753
vk::raii::SwapchainKHR swapChain = nullptr;
5854
std::vector<vk::Image> swapChainImages;
@@ -210,51 +206,23 @@ class HelloTriangleApplication {
210206
}
211207

212208
void createLogicalDevice() {
213-
// find the index of the first queue family that supports graphics
214209
std::vector<vk::QueueFamilyProperties> queueFamilyProperties = physicalDevice.getQueueFamilyProperties();
215210

216-
// get the first index into queueFamilyProperties which supports graphics
217-
auto graphicsQueueFamilyProperty = std::ranges::find_if( queueFamilyProperties, []( auto const & qfp )
218-
{ return (qfp.queueFlags & vk::QueueFlagBits::eGraphics) != static_cast<vk::QueueFlags>(0); } );
219-
220-
auto graphicsIndex = static_cast<uint32_t>( std::distance( queueFamilyProperties.begin(), graphicsQueueFamilyProperty ) );
221-
222-
// determine a queueFamilyIndex that supports present
223-
// first check if the graphicsIndex is good enough
224-
auto presentIndex = physicalDevice.getSurfaceSupportKHR( graphicsIndex, *surface )
225-
? graphicsIndex
226-
: ~0;
227-
if ( presentIndex == queueFamilyProperties.size() )
211+
// get the first index into queueFamilyProperties which supports both graphics and present
212+
uint32_t queueIndex = ~0;
213+
for (uint32_t qfpIndex = 0; qfpIndex < queueFamilyProperties.size(); qfpIndex++)
228214
{
229-
// the graphicsIndex doesn't support present -> look for another family index that supports both
230-
// graphics and present
231-
for ( size_t i = 0; i < queueFamilyProperties.size(); i++ )
232-
{
233-
if ( ( queueFamilyProperties[i].queueFlags & vk::QueueFlagBits::eGraphics ) &&
234-
physicalDevice.getSurfaceSupportKHR( static_cast<uint32_t>( i ), *surface ) )
235-
{
236-
graphicsIndex = static_cast<uint32_t>( i );
237-
presentIndex = graphicsIndex;
238-
break;
239-
}
240-
}
241-
if ( presentIndex == queueFamilyProperties.size() )
215+
if ((queueFamilyProperties[qfpIndex].queueFlags & vk::QueueFlagBits::eGraphics) &&
216+
physicalDevice.getSurfaceSupportKHR(qfpIndex, *surface))
242217
{
243-
// there's nothing like a single family index that supports both graphics and present -> look for another
244-
// family index that supports present
245-
for ( size_t i = 0; i < queueFamilyProperties.size(); i++ )
246-
{
247-
if ( physicalDevice.getSurfaceSupportKHR( static_cast<uint32_t>( i ), *surface ) )
248-
{
249-
presentIndex = static_cast<uint32_t>( i );
250-
break;
251-
}
252-
}
218+
// found a queue family that supports both graphics and present
219+
queueIndex = qfpIndex;
220+
break;
253221
}
254222
}
255-
if ( ( graphicsIndex == queueFamilyProperties.size() ) || ( presentIndex == queueFamilyProperties.size() ) )
223+
if (queueIndex == ~0)
256224
{
257-
throw std::runtime_error( "Could not find a queue for graphics or present -> terminating" );
225+
throw std::runtime_error("Could not find a queue for graphics and present -> terminating");
258226
}
259227

260228
// query for Vulkan 1.3 features
@@ -266,16 +234,15 @@ class HelloTriangleApplication {
266234

267235
// create a Device
268236
float queuePriority = 0.0f;
269-
vk::DeviceQueueCreateInfo deviceQueueCreateInfo{ .queueFamilyIndex = graphicsIndex, .queueCount = 1, .pQueuePriorities = &queuePriority };
237+
vk::DeviceQueueCreateInfo deviceQueueCreateInfo{ .queueFamilyIndex = queueIndex, .queueCount = 1, .pQueuePriorities = &queuePriority };
270238
vk::DeviceCreateInfo deviceCreateInfo{ .pNext = &featureChain.get<vk::PhysicalDeviceFeatures2>(),
271239
.queueCreateInfoCount = 1,
272240
.pQueueCreateInfos = &deviceQueueCreateInfo,
273241
.enabledExtensionCount = static_cast<uint32_t>(requiredDeviceExtension.size()),
274242
.ppEnabledExtensionNames = requiredDeviceExtension.data() };
275243

276244
device = vk::raii::Device( physicalDevice, deviceCreateInfo );
277-
graphicsQueue = vk::raii::Queue( device, graphicsIndex, 0 );
278-
presentQueue = vk::raii::Queue( device, presentIndex, 0 );
245+
queue = vk::raii::Queue( device, queueIndex, 0 );
279246
}
280247

281248
void createSwapChain() {

0 commit comments

Comments
 (0)