mirror of
https://github.com/opus-tango/IntroductionToVulkan.git
synced 2026-03-20 12:05:20 +00:00
Changed all printf() functions to std::cout. Added information about available swap chain image usages. Cleaned included header files.
This commit is contained in:
@@ -8,7 +8,6 @@
|
||||
// Intel does not assume any responsibility for any errors which may appear in this software
|
||||
// nor any responsibility to update it.
|
||||
|
||||
#include <cstring>
|
||||
#include "Tutorial02.h"
|
||||
#include "VulkanFunctions.h"
|
||||
|
||||
@@ -64,7 +63,7 @@ namespace Tutorial {
|
||||
#endif
|
||||
|
||||
if( VulkanLibrary == nullptr ) {
|
||||
printf( "Could not load Vulkan library!\n" );
|
||||
std::cout << "Could not load Vulkan library!" << std::endl;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
@@ -77,10 +76,10 @@ namespace Tutorial {
|
||||
#define LoadProcAddress dlsym
|
||||
#endif
|
||||
|
||||
#define VK_EXPORTED_FUNCTION( fun ) \
|
||||
if( !(fun = (PFN_##fun)LoadProcAddress( VulkanLibrary, #fun )) ) { \
|
||||
printf( "Could not load exported function: " #fun "!\n" ); \
|
||||
return false; \
|
||||
#define VK_EXPORTED_FUNCTION( fun ) \
|
||||
if( !(fun = (PFN_##fun)LoadProcAddress( VulkanLibrary, #fun )) ) { \
|
||||
std::cout << "Could not load exported function: " << #fun << "!" << std::endl; \
|
||||
return false; \
|
||||
}
|
||||
|
||||
#include "ListOfFunctions.inl"
|
||||
@@ -89,10 +88,10 @@ namespace Tutorial {
|
||||
}
|
||||
|
||||
bool Tutorial02::LoadGlobalLevelEntryPoints() {
|
||||
#define VK_GLOBAL_LEVEL_FUNCTION( fun ) \
|
||||
if( !(fun = (PFN_##fun)vkGetInstanceProcAddr( nullptr, #fun )) ) { \
|
||||
printf( "Could not load global level function: " #fun "!\n" ); \
|
||||
return false; \
|
||||
#define VK_GLOBAL_LEVEL_FUNCTION( fun ) \
|
||||
if( !(fun = (PFN_##fun)vkGetInstanceProcAddr( nullptr, #fun )) ) { \
|
||||
std::cout << "Could not load global level function: " << #fun << "!" << std::endl; \
|
||||
return false; \
|
||||
}
|
||||
|
||||
#include "ListOfFunctions.inl"
|
||||
@@ -104,13 +103,13 @@ namespace Tutorial {
|
||||
uint32_t extensions_count = 0;
|
||||
if( (vkEnumerateInstanceExtensionProperties( nullptr, &extensions_count, nullptr ) != VK_SUCCESS) ||
|
||||
(extensions_count == 0) ) {
|
||||
printf( "Error occurred during instance extensions enumeration!\n" );
|
||||
std::cout << "Error occurred during instance extensions enumeration!" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
std::vector<VkExtensionProperties> available_extensions( extensions_count );
|
||||
if( vkEnumerateInstanceExtensionProperties( nullptr, &extensions_count, &available_extensions[0] ) != VK_SUCCESS ) {
|
||||
printf( "Error occurred during instance extensions enumeration!\n" );
|
||||
std::cout << "Error occurred during instance extensions enumeration!" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -127,7 +126,7 @@ namespace Tutorial {
|
||||
|
||||
for( size_t i = 0; i < extensions.size(); ++i ) {
|
||||
if( !CheckExtensionAvailability( extensions[i], available_extensions ) ) {
|
||||
printf( "Could not find instance extension named \"%s\"!\n", extensions[i] );
|
||||
std::cout << "Could not find instance extension named \"" << extensions[i] << "\"!" << std::endl;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -154,7 +153,7 @@ namespace Tutorial {
|
||||
};
|
||||
|
||||
if( vkCreateInstance( &instance_create_info, nullptr, &Vulkan.Instance ) != VK_SUCCESS ) {
|
||||
printf( "Could not create Vulkan instance!\n" );
|
||||
std::cout << "Could not create Vulkan instance!" << std::endl;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
@@ -170,10 +169,10 @@ namespace Tutorial {
|
||||
}
|
||||
|
||||
bool Tutorial02::LoadInstanceLevelEntryPoints() {
|
||||
#define VK_INSTANCE_LEVEL_FUNCTION( fun ) \
|
||||
if( !(fun = (PFN_##fun)vkGetInstanceProcAddr( Vulkan.Instance, #fun )) ) { \
|
||||
printf( "Could not load instance level function: " #fun "\n" ); \
|
||||
return false; \
|
||||
#define VK_INSTANCE_LEVEL_FUNCTION( fun ) \
|
||||
if( !(fun = (PFN_##fun)vkGetInstanceProcAddr( Vulkan.Instance, #fun )) ) { \
|
||||
std::cout << "Could not load instance level function: " << #fun << "!" << std::endl; \
|
||||
return false; \
|
||||
}
|
||||
|
||||
#include "ListOfFunctions.inl"
|
||||
@@ -222,7 +221,7 @@ namespace Tutorial {
|
||||
|
||||
#endif
|
||||
|
||||
printf( "Could not create presentation surface!\n" );
|
||||
std::cout << "Could not create presentation surface!" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -230,13 +229,13 @@ namespace Tutorial {
|
||||
uint32_t num_devices = 0;
|
||||
if( (vkEnumeratePhysicalDevices( Vulkan.Instance, &num_devices, nullptr ) != VK_SUCCESS) ||
|
||||
(num_devices == 0) ) {
|
||||
printf( "Error occurred during physical devices enumeration!\n" );
|
||||
std::cout << "Error occurred during physical devices enumeration!" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
std::vector<VkPhysicalDevice> physical_devices( num_devices );
|
||||
if( vkEnumeratePhysicalDevices( Vulkan.Instance, &num_devices, &physical_devices[0] ) != VK_SUCCESS ) {
|
||||
printf( "Error occurred during physical devices enumeration!\n" );
|
||||
std::cout << "Error occurred during physical devices enumeration!" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -249,7 +248,7 @@ namespace Tutorial {
|
||||
}
|
||||
}
|
||||
if( Vulkan.PhysicalDevice == VK_NULL_HANDLE ) {
|
||||
printf( "Could not select physical device based on the chosen properties!\n" );
|
||||
std::cout << "Could not select physical device based on the chosen properties!" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -257,12 +256,12 @@ namespace Tutorial {
|
||||
std::vector<float> queue_priorities = { 1.0f };
|
||||
|
||||
queue_create_infos.push_back( {
|
||||
VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, // VkStructureType sType
|
||||
nullptr, // const void *pNext
|
||||
0, // VkDeviceQueueCreateFlags flags
|
||||
selected_graphics_queue_family_index, // uint32_t queueFamilyIndex
|
||||
static_cast<uint32_t>(queue_priorities.size()), // uint32_t queueCount
|
||||
&queue_priorities[0] // const float *pQueuePriorities
|
||||
VK_STRUCTURE_TYPE_DEVICE_QUEUE_CREATE_INFO, // VkStructureType sType
|
||||
nullptr, // const void *pNext
|
||||
0, // VkDeviceQueueCreateFlags flags
|
||||
selected_graphics_queue_family_index, // uint32_t queueFamilyIndex
|
||||
static_cast<uint32_t>(queue_priorities.size()), // uint32_t queueCount
|
||||
&queue_priorities[0] // const float *pQueuePriorities
|
||||
} );
|
||||
|
||||
if( selected_graphics_queue_family_index != selected_present_queue_family_index ) {
|
||||
@@ -294,7 +293,7 @@ namespace Tutorial {
|
||||
};
|
||||
|
||||
if( vkCreateDevice( Vulkan.PhysicalDevice, &device_create_info, nullptr, &Vulkan.Device ) != VK_SUCCESS ) {
|
||||
printf( "Could not create Vulkan device!\n" );
|
||||
std::cout << "Could not create Vulkan device!" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -307,13 +306,13 @@ namespace Tutorial {
|
||||
uint32_t extensions_count = 0;
|
||||
if( (vkEnumerateDeviceExtensionProperties( physical_device, nullptr, &extensions_count, nullptr ) != VK_SUCCESS) ||
|
||||
(extensions_count == 0) ) {
|
||||
printf( "Error occurred during physical device %p extensions enumeration!\n", physical_device );
|
||||
std::cout << "Error occurred during physical device " << physical_device << " extensions enumeration!" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
std::vector<VkExtensionProperties> available_extensions( extensions_count );
|
||||
if( vkEnumerateDeviceExtensionProperties( physical_device, nullptr, &extensions_count, &available_extensions[0] ) != VK_SUCCESS ) {
|
||||
printf( "Error occurred during physical device %p extensions enumeration!\n", physical_device );
|
||||
std::cout << "Error occurred during physical device " << physical_device << " extensions enumeration!" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -323,7 +322,7 @@ namespace Tutorial {
|
||||
|
||||
for( size_t i = 0; i < device_extensions.size(); ++i ) {
|
||||
if( !CheckExtensionAvailability( device_extensions[i], available_extensions ) ) {
|
||||
printf( "Physical device %p doesn't support extension named \"%s\"!\n", physical_device, device_extensions[i] );
|
||||
std::cout << "Physical device " << physical_device << " doesn't support extension named \"" << device_extensions[i] << "\"!" << std::endl;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -338,14 +337,14 @@ namespace Tutorial {
|
||||
|
||||
if( (major_version < 1) &&
|
||||
(device_properties.limits.maxImageDimension2D < 4096) ) {
|
||||
printf( "Physical device %p doesn't support required parameters!\n", physical_device );
|
||||
std::cout << "Physical device " << physical_device << " doesn't support required parameters!" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
uint32_t queue_families_count = 0;
|
||||
vkGetPhysicalDeviceQueueFamilyProperties( physical_device, &queue_families_count, nullptr );
|
||||
if( queue_families_count == 0 ) {
|
||||
printf( "Physical device %p doesn't have any queue families!\n", physical_device );
|
||||
std::cout << "Physical device " << physical_device << " doesn't have any queue families!" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -387,7 +386,7 @@ namespace Tutorial {
|
||||
// If this device doesn't support queues with graphics and present capabilities don't use it
|
||||
if( (graphics_queue_family_index == UINT32_MAX) ||
|
||||
(present_queue_family_index == UINT32_MAX) ) {
|
||||
printf( "Could not find queue families with required properties on physical device %p!\n", physical_device );
|
||||
std::cout << "Could not find queue family with required properties on physical device " << physical_device << "!" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -397,10 +396,10 @@ namespace Tutorial {
|
||||
}
|
||||
|
||||
bool Tutorial02::LoadDeviceLevelEntryPoints() {
|
||||
#define VK_DEVICE_LEVEL_FUNCTION( fun ) \
|
||||
if( !(fun = (PFN_##fun)vkGetDeviceProcAddr( Vulkan.Device, #fun )) ) { \
|
||||
printf( "Could not load device level function: " #fun "!\n" ); \
|
||||
return false; \
|
||||
#define VK_DEVICE_LEVEL_FUNCTION( fun ) \
|
||||
if( !(fun = (PFN_##fun)vkGetDeviceProcAddr( Vulkan.Device, #fun )) ) { \
|
||||
std::cout << "Could not load device level function: " << #fun << "!" << std::endl; \
|
||||
return false; \
|
||||
}
|
||||
|
||||
#include "ListOfFunctions.inl"
|
||||
@@ -423,7 +422,7 @@ namespace Tutorial {
|
||||
|
||||
if( (vkCreateSemaphore( Vulkan.Device, &semaphore_create_info, nullptr, &Vulkan.ImageAvailableSemaphore ) != VK_SUCCESS) ||
|
||||
(vkCreateSemaphore( Vulkan.Device, &semaphore_create_info, nullptr, &Vulkan.RenderingFinishedSemaphore ) != VK_SUCCESS) ) {
|
||||
printf( "Could not create semaphores!\n" );
|
||||
std::cout << "Could not create semaphores!" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -437,33 +436,33 @@ namespace Tutorial {
|
||||
|
||||
VkSurfaceCapabilitiesKHR surface_capabilities;
|
||||
if( vkGetPhysicalDeviceSurfaceCapabilitiesKHR( Vulkan.PhysicalDevice, Vulkan.PresentationSurface, &surface_capabilities ) != VK_SUCCESS ) {
|
||||
printf( "Could not check presentation surface capabilities!\n" );
|
||||
std::cout << "Could not check presentation surface capabilities!" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
uint32_t formats_count;
|
||||
if( (vkGetPhysicalDeviceSurfaceFormatsKHR( Vulkan.PhysicalDevice, Vulkan.PresentationSurface, &formats_count, nullptr ) != VK_SUCCESS) ||
|
||||
(formats_count == 0) ) {
|
||||
printf( "Error occurred during presentation surface formats enumeration!\n" );
|
||||
std::cout << "Error occurred during presentation surface formats enumeration!" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
std::vector<VkSurfaceFormatKHR> surface_formats( formats_count );
|
||||
if( vkGetPhysicalDeviceSurfaceFormatsKHR( Vulkan.PhysicalDevice, Vulkan.PresentationSurface, &formats_count, &surface_formats[0] ) != VK_SUCCESS ) {
|
||||
printf( "Error occurred during presentation surface formats enumeration!\n" );
|
||||
std::cout << "Error occurred during presentation surface formats enumeration!" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
uint32_t present_modes_count;
|
||||
if( (vkGetPhysicalDeviceSurfacePresentModesKHR( Vulkan.PhysicalDevice, Vulkan.PresentationSurface, &present_modes_count, nullptr ) != VK_SUCCESS) ||
|
||||
(present_modes_count == 0) ) {
|
||||
printf( "Error occurred during presentation surface present modes enumeration!\n" );
|
||||
std::cout << "Error occurred during presentation surface present modes enumeration!" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
std::vector<VkPresentModeKHR> present_modes( present_modes_count );
|
||||
if( vkGetPhysicalDeviceSurfacePresentModesKHR( Vulkan.PhysicalDevice, Vulkan.PresentationSurface, &present_modes_count, &present_modes[0] ) != VK_SUCCESS ) {
|
||||
printf( "Error occurred during presentation surface present modes enumeration!\n" );
|
||||
std::cout << "Error occurred during presentation surface present modes enumeration!" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -475,8 +474,10 @@ namespace Tutorial {
|
||||
VkPresentModeKHR desired_present_mode = GetSwapChainPresentMode( present_modes );
|
||||
VkSwapchainKHR old_swap_chain = Vulkan.SwapChain;
|
||||
|
||||
if( static_cast<int>(desired_usage) == 0 ) {
|
||||
printf( "TRANSFER_DST image usage is not supported by the swap chain!" );
|
||||
if( static_cast<int>(desired_usage) == -1 ) {
|
||||
return false;
|
||||
}
|
||||
if( static_cast<int>(desired_present_mode) == -1 ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -502,7 +503,7 @@ namespace Tutorial {
|
||||
};
|
||||
|
||||
if( vkCreateSwapchainKHR( Vulkan.Device, &swap_chain_create_info, nullptr, &Vulkan.SwapChain ) != VK_SUCCESS ) {
|
||||
printf( "Could not create swap chain!\n" );
|
||||
std::cout << "Could not create swap chain!" << std::endl;
|
||||
return false;
|
||||
}
|
||||
if( old_swap_chain != VK_NULL_HANDLE ) {
|
||||
@@ -574,7 +575,18 @@ namespace Tutorial {
|
||||
if( surface_capabilities.supportedUsageFlags & VK_IMAGE_USAGE_TRANSFER_DST_BIT ) {
|
||||
return VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT | VK_IMAGE_USAGE_TRANSFER_DST_BIT;
|
||||
}
|
||||
return 0;
|
||||
std::cout << "VK_IMAGE_USAGE_TRANSFER_DST image usage is not supported by the swap chain!" << std::endl
|
||||
<< "Supported swap chain's image usages include:" << std::endl
|
||||
<< (surface_capabilities.supportedUsageFlags & VK_IMAGE_USAGE_TRANSFER_SRC_BIT ? " VK_IMAGE_USAGE_TRANSFER_SRC\n" : "")
|
||||
<< (surface_capabilities.supportedUsageFlags & VK_IMAGE_USAGE_TRANSFER_DST_BIT ? " VK_IMAGE_USAGE_TRANSFER_DST\n" : "")
|
||||
<< (surface_capabilities.supportedUsageFlags & VK_IMAGE_USAGE_SAMPLED_BIT ? " VK_IMAGE_USAGE_SAMPLED\n" : "")
|
||||
<< (surface_capabilities.supportedUsageFlags & VK_IMAGE_USAGE_STORAGE_BIT ? " VK_IMAGE_USAGE_STORAGE\n" : "")
|
||||
<< (surface_capabilities.supportedUsageFlags & VK_IMAGE_USAGE_COLOR_ATTACHMENT_BIT ? " VK_IMAGE_USAGE_COLOR_ATTACHMENT\n" : "")
|
||||
<< (surface_capabilities.supportedUsageFlags & VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT_BIT ? " VK_IMAGE_USAGE_DEPTH_STENCIL_ATTACHMENT\n" : "")
|
||||
<< (surface_capabilities.supportedUsageFlags & VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT_BIT ? " VK_IMAGE_USAGE_TRANSIENT_ATTACHMENT\n" : "")
|
||||
<< (surface_capabilities.supportedUsageFlags & VK_IMAGE_USAGE_INPUT_ATTACHMENT_BIT ? " VK_IMAGE_USAGE_INPUT_ATTACHMENT" : "")
|
||||
<< std::endl;
|
||||
return static_cast<VkImageUsageFlags>(-1);
|
||||
}
|
||||
|
||||
VkSurfaceTransformFlagBitsKHR Tutorial02::GetSwapChainTransform( VkSurfaceCapabilitiesKHR &surface_capabilities ) {
|
||||
@@ -599,7 +611,13 @@ namespace Tutorial {
|
||||
return present_mode;
|
||||
}
|
||||
}
|
||||
return VK_PRESENT_MODE_FIFO_KHR;
|
||||
for( VkPresentModeKHR &present_mode : present_modes ) {
|
||||
if( present_mode == VK_PRESENT_MODE_FIFO_KHR ) {
|
||||
return present_mode;
|
||||
}
|
||||
}
|
||||
std::cout << "FIFO present mode is not supported by the swap chain!" << std::endl;
|
||||
return static_cast<VkPresentModeKHR>(-1);
|
||||
}
|
||||
|
||||
bool Tutorial02::OnWindowSizeChanged() {
|
||||
@@ -623,14 +641,14 @@ namespace Tutorial {
|
||||
};
|
||||
|
||||
if( vkCreateCommandPool( Vulkan.Device, &cmd_pool_create_info, nullptr, &Vulkan.PresentQueueCmdPool ) != VK_SUCCESS ) {
|
||||
printf( "Could not create a command pool!\n" );
|
||||
std::cout << "Could not create a command pool!" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
uint32_t image_count = 0;
|
||||
if( (vkGetSwapchainImagesKHR( Vulkan.Device, Vulkan.SwapChain, &image_count, nullptr ) != VK_SUCCESS) ||
|
||||
(image_count == 0) ) {
|
||||
printf( "Could not get the number of swap chain images!\n" );
|
||||
std::cout << "Could not get the number of swap chain images!" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -644,12 +662,12 @@ namespace Tutorial {
|
||||
image_count // uint32_t bufferCount
|
||||
};
|
||||
if( vkAllocateCommandBuffers( Vulkan.Device, &cmd_buffer_allocate_info, &Vulkan.PresentQueueCmdBuffers[0] ) != VK_SUCCESS ) {
|
||||
printf( "Could not allocate command buffers!\n" );
|
||||
std::cout << "Could not allocate command buffers!" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
if( !RecordCommandBuffers() ) {
|
||||
printf( "Could not record command buffers!\n" );
|
||||
std::cout << "Could not record command buffers!" << std::endl;
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
@@ -660,7 +678,7 @@ namespace Tutorial {
|
||||
|
||||
std::vector<VkImage> swap_chain_images( image_count );
|
||||
if( vkGetSwapchainImagesKHR( Vulkan.Device, Vulkan.SwapChain, &image_count, &swap_chain_images[0] ) != VK_SUCCESS ) {
|
||||
printf( "Could not get swap chain images!\n" );
|
||||
std::cout << "Could not get swap chain images!" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -717,7 +735,7 @@ namespace Tutorial {
|
||||
|
||||
vkCmdPipelineBarrier( Vulkan.PresentQueueCmdBuffers[i], VK_PIPELINE_STAGE_TRANSFER_BIT, VK_PIPELINE_STAGE_BOTTOM_OF_PIPE_BIT, 0, 0, nullptr, 0, nullptr, 1, &barrier_from_clear_to_present );
|
||||
if( vkEndCommandBuffer( Vulkan.PresentQueueCmdBuffers[i] ) != VK_SUCCESS ) {
|
||||
printf( "Could not record command buffers!\n" );
|
||||
std::cout << "Could not record command buffers!" << std::endl;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -751,7 +769,7 @@ namespace Tutorial {
|
||||
case VK_ERROR_OUT_OF_DATE_KHR:
|
||||
return OnWindowSizeChanged();
|
||||
default:
|
||||
printf( "Problem occurred during swap chain image acquisition!\n" );
|
||||
std::cout << "Problem occurred during swap chain image acquisition!" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -791,7 +809,7 @@ namespace Tutorial {
|
||||
case VK_SUBOPTIMAL_KHR:
|
||||
return OnWindowSizeChanged();
|
||||
default:
|
||||
printf( "Problem occurred during image presentation!\n" );
|
||||
std::cout << "Problem occurred during image presentation!" << std::endl;
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user