Vulkan-Hpp
12_InitFrameBuffers.cpp
Go to the documentation of this file.
1 // Copyright(c) 2019, NVIDIA CORPORATION. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 // VulkanHpp Samples : 12_InitFrameBuffers
16 // Initialize framebuffers
17 
18 #include "../utils/utils.hpp"
19 
20 #include <iostream>
21 
22 static char const * AppName = "12_InitFrameBuffers";
23 static char const * EngineName = "Vulkan.hpp";
24 
25 int main( int /*argc*/, char ** /*argv*/ )
26 {
27  try
28  {
29  vk::Instance instance = vk::su::createInstance( AppName, EngineName, {}, vk::su::getInstanceExtensions() );
30 #if !defined( NDEBUG )
32 #endif
33 
34  vk::PhysicalDevice physicalDevice = instance.enumeratePhysicalDevices().front();
35 
36  vk::su::SurfaceData surfaceData( instance, AppName, vk::Extent2D( 64, 64 ) );
37 
38  std::pair<uint32_t, uint32_t> graphicsAndPresentQueueFamilyIndex = vk::su::findGraphicsAndPresentQueueFamilyIndex( physicalDevice, surfaceData.surface );
39  vk::Device device = vk::su::createDevice( physicalDevice, graphicsAndPresentQueueFamilyIndex.first, vk::su::getDeviceExtensions() );
40 
41  vk::su::SwapChainData swapChainData( physicalDevice,
42  device,
43  surfaceData.surface,
44  surfaceData.extent,
46  nullptr,
47  graphicsAndPresentQueueFamilyIndex.first,
48  graphicsAndPresentQueueFamilyIndex.second );
49 
50  vk::su::DepthBufferData depthBufferData( physicalDevice, device, vk::Format::eD16Unorm, surfaceData.extent );
51 
52  vk::RenderPass renderPass = vk::su::createRenderPass( device, swapChainData.colorFormat, depthBufferData.format );
53 
54  /* VULKAN_KEY_START */
55 
56  std::array<vk::ImageView, 2> attachments;
57  attachments[1] = depthBufferData.imageView;
58 
59  vk::FramebufferCreateInfo framebufferCreateInfo(
60  vk::FramebufferCreateFlags(), renderPass, attachments, surfaceData.extent.width, surfaceData.extent.height, 1 );
61  std::vector<vk::Framebuffer> framebuffers;
62  framebuffers.reserve( swapChainData.imageViews.size() );
63  for ( auto const & imageView : swapChainData.imageViews )
64  {
65  attachments[0] = imageView;
66  framebuffers.push_back( device.createFramebuffer( framebufferCreateInfo ) );
67  }
68 
69  for ( auto const & framebuffer : framebuffers )
70  {
71  device.destroyFramebuffer( framebuffer );
72  }
73 
74  /* VULKAN_KEY_END */
75 
76  device.destroyRenderPass( renderPass );
77  depthBufferData.clear( device );
78  swapChainData.clear( device );
79  device.destroy();
80  instance.destroySurfaceKHR( surfaceData.surface );
81 #if !defined( NDEBUG )
82  instance.destroyDebugUtilsMessengerEXT( debugUtilsMessenger );
83 #endif
84  instance.destroy();
85  }
86  catch ( vk::SystemError & err )
87  {
88  std::cout << "vk::SystemError: " << err.what() << std::endl;
89  exit( -1 );
90  }
91  catch ( std::exception & err )
92  {
93  std::cout << "std::exception: " << err.what() << std::endl;
94  exit( -1 );
95  }
96  catch ( ... )
97  {
98  std::cout << "unknown error\n";
99  exit( -1 );
100  }
101  return 0;
102 }
int main(int, char **)
void cout(vk::SurfaceCapabilitiesKHR const &surfaceCapabilities)
void destroy(const vk::AllocationCallbacks *pAllocator, Dispatch const &d VULKAN_HPP_DEFAULT_DISPATCHER_ASSIGNMENT) const VULKAN_HPP_NOEXCEPT
void destroyRenderPass(vk::RenderPass renderPass, const vk::AllocationCallbacks *pAllocator, Dispatch const &d VULKAN_HPP_DEFAULT_DISPATCHER_ASSIGNMENT) const VULKAN_HPP_NOEXCEPT
VULKAN_HPP_NODISCARD Result createFramebuffer(const vk::FramebufferCreateInfo *pCreateInfo, const vk::AllocationCallbacks *pAllocator, vk::Framebuffer *pFramebuffer, Dispatch const &d VULKAN_HPP_DEFAULT_DISPATCHER_ASSIGNMENT) const VULKAN_HPP_NOEXCEPT
void destroyFramebuffer(vk::Framebuffer framebuffer, const vk::AllocationCallbacks *pAllocator, Dispatch const &d VULKAN_HPP_DEFAULT_DISPATCHER_ASSIGNMENT) const VULKAN_HPP_NOEXCEPT
VULKAN_HPP_NODISCARD Result createDebugUtilsMessengerEXT(const vk::DebugUtilsMessengerCreateInfoEXT *pCreateInfo, const vk::AllocationCallbacks *pAllocator, vk::DebugUtilsMessengerEXT *pMessenger, Dispatch const &d VULKAN_HPP_DEFAULT_DISPATCHER_ASSIGNMENT) const VULKAN_HPP_NOEXCEPT
void destroy(const vk::AllocationCallbacks *pAllocator, Dispatch const &d VULKAN_HPP_DEFAULT_DISPATCHER_ASSIGNMENT) const VULKAN_HPP_NOEXCEPT
VULKAN_HPP_NODISCARD Result enumeratePhysicalDevices(uint32_t *pPhysicalDeviceCount, vk::PhysicalDevice *pPhysicalDevices, Dispatch const &d VULKAN_HPP_DEFAULT_DISPATCHER_ASSIGNMENT) const VULKAN_HPP_NOEXCEPT
void destroySurfaceKHR(vk::SurfaceKHR surface, const vk::AllocationCallbacks *pAllocator, Dispatch const &d VULKAN_HPP_DEFAULT_DISPATCHER_ASSIGNMENT) const VULKAN_HPP_NOEXCEPT
void destroyDebugUtilsMessengerEXT(vk::DebugUtilsMessengerEXT messenger, const vk::AllocationCallbacks *pAllocator, Dispatch const &d VULKAN_HPP_DEFAULT_DISPATCHER_ASSIGNMENT) const VULKAN_HPP_NOEXCEPT
virtual const char * what() const VULKAN_HPP_NOEXCEPT
Definition: vulkan.hpp:6206
vk::DebugUtilsMessengerCreateInfoEXT makeDebugUtilsMessengerCreateInfoEXT()
Definition: utils.cpp:1025
vk::RenderPass createRenderPass(vk::Device const &device, vk::Format colorFormat, vk::Format depthFormat, vk::AttachmentLoadOp loadOp, vk::ImageLayout colorFinalLayout)
Definition: utils.cpp:314
vk::Instance createInstance(std::string const &appName, std::string const &engineName, std::vector< std::string > const &layers, std::vector< std::string > const &extensions, uint32_t apiVersion)
Definition: utils.cpp:279
vk::Device createDevice(vk::PhysicalDevice const &physicalDevice, uint32_t queueFamilyIndex, std::vector< std::string > const &extensions, vk::PhysicalDeviceFeatures const *physicalDeviceFeatures, void const *pNext)
Definition: utils.cpp:86
std::pair< uint32_t, uint32_t > findGraphicsAndPresentQueueFamilyIndex(vk::PhysicalDevice physicalDevice, vk::SurfaceKHR const &surface)
Definition: utils.cpp:420
std::vector< std::string > getInstanceExtensions()
Definition: utils.cpp:477
std::vector< std::string > getDeviceExtensions()
Definition: utils.cpp:472
vk::ImageView imageView
Definition: utils.hpp:191
void clear(vk::Device const &device)
Definition: utils.hpp:181
vk::Format format
Definition: utils.hpp:188
vk::Extent2D extent
Definition: utils.hpp:203
vk::SurfaceKHR surface
Definition: utils.hpp:205
std::vector< vk::ImageView > imageViews
Definition: utils.hpp:233
vk::Format colorFormat
Definition: utils.hpp:230
void clear(vk::Device const &device)
Definition: utils.hpp:219