Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 23 additions & 56 deletions attachments/05_window_surface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,14 @@ class HelloTriangleApplication {
}

private:
GLFWwindow* window = nullptr;

vk::raii::Context context;
vk::raii::Instance instance = nullptr;
GLFWwindow * window = nullptr;
vk::raii::Context context;
vk::raii::Instance instance = nullptr;
vk::raii::DebugUtilsMessengerEXT debugMessenger = nullptr;
vk::raii::SurfaceKHR surface = nullptr;

vk::raii::PhysicalDevice physicalDevice = nullptr;
vk::raii::Device device = nullptr;

vk::raii::Queue graphicsQueue = nullptr;
vk::raii::Queue presentQueue = nullptr;
vk::raii::SurfaceKHR surface = nullptr;
vk::raii::PhysicalDevice physicalDevice = nullptr;
vk::raii::Device device = nullptr;
vk::raii::Queue queue = nullptr;

std::vector<const char*> requiredDeviceExtension = {
vk::KHRSwapchainExtensionName,
Expand Down Expand Up @@ -201,53 +197,25 @@ class HelloTriangleApplication {
}

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

// get the first index into queueFamilyProperties which supports graphics
auto graphicsQueueFamilyProperty = std::ranges::find_if( queueFamilyProperties, []( auto const & qfp )
{ return (qfp.queueFlags & vk::QueueFlagBits::eGraphics) != static_cast<vk::QueueFlags>(0); } );

auto graphicsIndex = static_cast<uint32_t>( std::distance( queueFamilyProperties.begin(), graphicsQueueFamilyProperty ) );

// determine a queueFamilyIndex that supports present
// first check if the graphicsIndex is good enough
auto presentIndex = physicalDevice.getSurfaceSupportKHR( graphicsIndex, *surface )
? graphicsIndex
: ~0;
if ( presentIndex == queueFamilyProperties.size() )
// get the first index into queueFamilyProperties which supports both graphics and present
uint32_t queueIndex = ~0;
for (uint32_t qfpIndex = 0; qfpIndex < queueFamilyProperties.size(); qfpIndex++)
{
// the graphicsIndex doesn't support present -> look for another family index that supports both
// graphics and present
for ( size_t i = 0; i < queueFamilyProperties.size(); i++ )
{
if ( ( queueFamilyProperties[i].queueFlags & vk::QueueFlagBits::eGraphics ) &&
physicalDevice.getSurfaceSupportKHR( static_cast<uint32_t>( i ), *surface ) )
{
graphicsIndex = static_cast<uint32_t>( i );
presentIndex = graphicsIndex;
break;
}
}
if ( presentIndex == queueFamilyProperties.size() )
{
// there's nothing like a single family index that supports both graphics and present -> look for another
// family index that supports present
for ( size_t i = 0; i < queueFamilyProperties.size(); i++ )
{
if ( physicalDevice.getSurfaceSupportKHR( static_cast<uint32_t>( i ), *surface ) )
{
presentIndex = static_cast<uint32_t>( i );
break;
}
}
}
if ((queueFamilyProperties[qfpIndex].queueFlags & vk::QueueFlagBits::eGraphics) &&
physicalDevice.getSurfaceSupportKHR(qfpIndex, *surface))
{
// found a queue family that supports both graphics and present
queueIndex = qfpIndex;
break;
}
}
if ( ( graphicsIndex == queueFamilyProperties.size() ) || ( presentIndex == queueFamilyProperties.size() ) )
if (queueIndex == ~0)
{
throw std::runtime_error( "Could not find a queue for graphics or present -> terminating" );
throw std::runtime_error("Could not find a queue for graphics and present -> terminating");
}

// query for Vulkan 1.3 features
vk::StructureChain<vk::PhysicalDeviceFeatures2, vk::PhysicalDeviceVulkan13Features, vk::PhysicalDeviceExtendedDynamicStateFeaturesEXT> featureChain = {
{}, // vk::PhysicalDeviceFeatures2
Expand All @@ -257,16 +225,15 @@ class HelloTriangleApplication {

// create a Device
float queuePriority = 0.0f;
vk::DeviceQueueCreateInfo deviceQueueCreateInfo{ .queueFamilyIndex = graphicsIndex, .queueCount = 1, .pQueuePriorities = &queuePriority };
vk::DeviceQueueCreateInfo deviceQueueCreateInfo{ .queueFamilyIndex = queueIndex, .queueCount = 1, .pQueuePriorities = &queuePriority };
vk::DeviceCreateInfo deviceCreateInfo{ .pNext = &featureChain.get<vk::PhysicalDeviceFeatures2>(),
.queueCreateInfoCount = 1,
.pQueueCreateInfos = &deviceQueueCreateInfo,
.enabledExtensionCount = static_cast<uint32_t>(requiredDeviceExtension.size()),
.ppEnabledExtensionNames = requiredDeviceExtension.data() };

device = vk::raii::Device( physicalDevice, deviceCreateInfo );
graphicsQueue = vk::raii::Queue( device, graphicsIndex, 0 );
presentQueue = vk::raii::Queue( device, presentIndex, 0 );
queue = vk::raii::Queue( device, queueIndex, 0 );
}

std::vector<const char*> getRequiredExtensions() {
Expand Down
71 changes: 19 additions & 52 deletions attachments/06_swap_chain_creation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,14 @@ class HelloTriangleApplication {
}

private:
GLFWwindow* window = nullptr;

vk::raii::Context context;
vk::raii::Instance instance = nullptr;
GLFWwindow * window = nullptr;
vk::raii::Context context;
vk::raii::Instance instance = nullptr;
vk::raii::DebugUtilsMessengerEXT debugMessenger = nullptr;
vk::raii::SurfaceKHR surface = nullptr;

vk::raii::PhysicalDevice physicalDevice = nullptr;
vk::raii::Device device = nullptr;

vk::raii::Queue graphicsQueue = nullptr;
vk::raii::Queue presentQueue = nullptr;
vk::raii::SurfaceKHR surface = nullptr;
vk::raii::PhysicalDevice physicalDevice = nullptr;
vk::raii::Device device = nullptr;
vk::raii::Queue queue = nullptr;

vk::raii::SwapchainKHR swapChain = nullptr;
std::vector<vk::Image> swapChainImages;
Expand Down Expand Up @@ -209,51 +205,23 @@ class HelloTriangleApplication {
}

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

// get the first index into queueFamilyProperties which supports graphics
auto graphicsQueueFamilyProperty = std::ranges::find_if( queueFamilyProperties, []( auto const & qfp )
{ return (qfp.queueFlags & vk::QueueFlagBits::eGraphics) != static_cast<vk::QueueFlags>(0); } );

auto graphicsIndex = static_cast<uint32_t>( std::distance( queueFamilyProperties.begin(), graphicsQueueFamilyProperty ) );

// determine a queueFamilyIndex that supports present
// first check if the graphicsIndex is good enough
auto presentIndex = physicalDevice.getSurfaceSupportKHR( graphicsIndex, *surface )
? graphicsIndex
: ~0;
if ( presentIndex == queueFamilyProperties.size() )
// get the first index into queueFamilyProperties which supports both graphics and present
uint32_t queueIndex = ~0;
for (uint32_t qfpIndex = 0; qfpIndex < queueFamilyProperties.size(); qfpIndex++)
{
// the graphicsIndex doesn't support present -> look for another family index that supports both
// graphics and present
for ( size_t i = 0; i < queueFamilyProperties.size(); i++ )
{
if ( ( queueFamilyProperties[i].queueFlags & vk::QueueFlagBits::eGraphics ) &&
physicalDevice.getSurfaceSupportKHR( static_cast<uint32_t>( i ), *surface ) )
{
graphicsIndex = static_cast<uint32_t>( i );
presentIndex = graphicsIndex;
break;
}
}
if ( presentIndex == queueFamilyProperties.size() )
if ((queueFamilyProperties[qfpIndex].queueFlags & vk::QueueFlagBits::eGraphics) &&
physicalDevice.getSurfaceSupportKHR(qfpIndex, *surface))
{
// there's nothing like a single family index that supports both graphics and present -> look for another
// family index that supports present
for ( size_t i = 0; i < queueFamilyProperties.size(); i++ )
{
if ( physicalDevice.getSurfaceSupportKHR( static_cast<uint32_t>( i ), *surface ) )
{
presentIndex = static_cast<uint32_t>( i );
break;
}
}
// found a queue family that supports both graphics and present
queueIndex = qfpIndex;
break;
}
}
if ( ( graphicsIndex == queueFamilyProperties.size() ) || ( presentIndex == queueFamilyProperties.size() ) )
if (queueIndex == ~0)
{
throw std::runtime_error( "Could not find a queue for graphics or present -> terminating" );
throw std::runtime_error("Could not find a queue for graphics and present -> terminating");
}

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

// create a Device
float queuePriority = 0.0f;
vk::DeviceQueueCreateInfo deviceQueueCreateInfo{ .queueFamilyIndex = graphicsIndex, .queueCount = 1, .pQueuePriorities = &queuePriority };
vk::DeviceQueueCreateInfo deviceQueueCreateInfo{ .queueFamilyIndex = queueIndex, .queueCount = 1, .pQueuePriorities = &queuePriority };
vk::DeviceCreateInfo deviceCreateInfo{ .pNext = &featureChain.get<vk::PhysicalDeviceFeatures2>(),
.queueCreateInfoCount = 1,
.pQueueCreateInfos = &deviceQueueCreateInfo,
.enabledExtensionCount = static_cast<uint32_t>(requiredDeviceExtension.size()),
.ppEnabledExtensionNames = requiredDeviceExtension.data() };

device = vk::raii::Device( physicalDevice, deviceCreateInfo );
graphicsQueue = vk::raii::Queue( device, graphicsIndex, 0 );
presentQueue = vk::raii::Queue( device, presentIndex, 0 );
queue = vk::raii::Queue( device, queueIndex, 0 );
}

void createSwapChain() {
Expand Down
71 changes: 19 additions & 52 deletions attachments/07_image_views.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,14 @@ class HelloTriangleApplication {
}

private:
GLFWwindow* window = nullptr;

vk::raii::Context context;
vk::raii::Instance instance = nullptr;
GLFWwindow * window = nullptr;
vk::raii::Context context;
vk::raii::Instance instance = nullptr;
vk::raii::DebugUtilsMessengerEXT debugMessenger = nullptr;
vk::raii::SurfaceKHR surface = nullptr;

vk::raii::PhysicalDevice physicalDevice = nullptr;
vk::raii::Device device = nullptr;

vk::raii::Queue graphicsQueue = nullptr;
vk::raii::Queue presentQueue = nullptr;
vk::raii::SurfaceKHR surface = nullptr;
vk::raii::PhysicalDevice physicalDevice = nullptr;
vk::raii::Device device = nullptr;
vk::raii::Queue queue = nullptr;

vk::raii::SwapchainKHR swapChain = nullptr;
std::vector<vk::Image> swapChainImages;
Expand Down Expand Up @@ -210,51 +206,23 @@ class HelloTriangleApplication {
}

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

// get the first index into queueFamilyProperties which supports graphics
auto graphicsQueueFamilyProperty = std::ranges::find_if( queueFamilyProperties, []( auto const & qfp )
{ return (qfp.queueFlags & vk::QueueFlagBits::eGraphics) != static_cast<vk::QueueFlags>(0); } );

auto graphicsIndex = static_cast<uint32_t>( std::distance( queueFamilyProperties.begin(), graphicsQueueFamilyProperty ) );

// determine a queueFamilyIndex that supports present
// first check if the graphicsIndex is good enough
auto presentIndex = physicalDevice.getSurfaceSupportKHR( graphicsIndex, *surface )
? graphicsIndex
: ~0;
if ( presentIndex == queueFamilyProperties.size() )
// get the first index into queueFamilyProperties which supports both graphics and present
uint32_t queueIndex = ~0;
for (uint32_t qfpIndex = 0; qfpIndex < queueFamilyProperties.size(); qfpIndex++)
{
// the graphicsIndex doesn't support present -> look for another family index that supports both
// graphics and present
for ( size_t i = 0; i < queueFamilyProperties.size(); i++ )
{
if ( ( queueFamilyProperties[i].queueFlags & vk::QueueFlagBits::eGraphics ) &&
physicalDevice.getSurfaceSupportKHR( static_cast<uint32_t>( i ), *surface ) )
{
graphicsIndex = static_cast<uint32_t>( i );
presentIndex = graphicsIndex;
break;
}
}
if ( presentIndex == queueFamilyProperties.size() )
if ((queueFamilyProperties[qfpIndex].queueFlags & vk::QueueFlagBits::eGraphics) &&
physicalDevice.getSurfaceSupportKHR(qfpIndex, *surface))
{
// there's nothing like a single family index that supports both graphics and present -> look for another
// family index that supports present
for ( size_t i = 0; i < queueFamilyProperties.size(); i++ )
{
if ( physicalDevice.getSurfaceSupportKHR( static_cast<uint32_t>( i ), *surface ) )
{
presentIndex = static_cast<uint32_t>( i );
break;
}
}
// found a queue family that supports both graphics and present
queueIndex = qfpIndex;
break;
}
}
if ( ( graphicsIndex == queueFamilyProperties.size() ) || ( presentIndex == queueFamilyProperties.size() ) )
if (queueIndex == ~0)
{
throw std::runtime_error( "Could not find a queue for graphics or present -> terminating" );
throw std::runtime_error("Could not find a queue for graphics and present -> terminating");
}

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

// create a Device
float queuePriority = 0.0f;
vk::DeviceQueueCreateInfo deviceQueueCreateInfo{ .queueFamilyIndex = graphicsIndex, .queueCount = 1, .pQueuePriorities = &queuePriority };
vk::DeviceQueueCreateInfo deviceQueueCreateInfo{ .queueFamilyIndex = queueIndex, .queueCount = 1, .pQueuePriorities = &queuePriority };
vk::DeviceCreateInfo deviceCreateInfo{ .pNext = &featureChain.get<vk::PhysicalDeviceFeatures2>(),
.queueCreateInfoCount = 1,
.pQueueCreateInfos = &deviceQueueCreateInfo,
.enabledExtensionCount = static_cast<uint32_t>(requiredDeviceExtension.size()),
.ppEnabledExtensionNames = requiredDeviceExtension.data() };

device = vk::raii::Device( physicalDevice, deviceCreateInfo );
graphicsQueue = vk::raii::Queue( device, graphicsIndex, 0 );
presentQueue = vk::raii::Queue( device, presentIndex, 0 );
queue = vk::raii::Queue( device, queueIndex, 0 );
}

void createSwapChain() {
Expand Down
Loading
Loading