/ Core / Clockworks / CwGraphics.cpp
CwGraphics.cpp
  1  #include <vulkan/vulkan_core.h>
  2  
  3  #include <Clockworks/CwGraphics.hpp>
  4  
  5  // CwGraphics class contiuation
  6  
  7  void CwGraphics::CwInit(const char Name[]) {
  8          // vulkan Vallayers
  9          if (EnableValLayers && !CheckValSupport())
 10                  throw std::runtime_error("CW-ERROR: Validation layers not available, please check your Vulkan-Sdk Install! :(");
 11  
 12          // Vulkan Instance Init
 13  
 14          VkInstanceCreateInfo CInfo{};
 15  
 16          VkApplicationInfo AppInfo{};
 17          AppInfo.sType = VK_STRUCTURE_TYPE_APPLICATION_INFO;
 18          AppInfo.pApplicationName = Name;
 19          AppInfo.applicationVersion = VK_MAKE_VERSION(1, 0, 0);
 20          AppInfo.pEngineName = "Cw Engine";
 21          AppInfo.engineVersion = VK_MAKE_VERSION(1, 0, 0);
 22          AppInfo.apiVersion = VK_API_VERSION_1_0;
 23  
 24          CInfo.sType = VK_STRUCTURE_TYPE_INSTANCE_CREATE_INFO;
 25          CInfo.pApplicationInfo = &AppInfo;
 26  
 27          auto Ext = getExtensions();
 28  
 29          CInfo.enabledExtensionCount = static_cast<uint32_t>(Ext.size());
 30          CInfo.ppEnabledExtensionNames = Ext.data();
 31  
 32          CInfo.enabledLayerCount = 0;
 33  
 34          if (EnableValLayers) {
 35                  CInfo.enabledLayerCount = static_cast<uint32_t>(validationLayers.size());
 36                  CInfo.ppEnabledLayerNames = validationLayers.data();
 37                  std::cout << "Cw-Info: Validation Layers enabled for this session :D\n";
 38          } else {
 39                  CInfo.enabledLayerCount = 0;
 40          }
 41  
 42          if (vkCreateInstance(&CInfo, nullptr, &Instance) != VK_SUCCESS) throw std::runtime_error("CW-ERROR: failed to create instance! :(");
 43  
 44          // Vulkan Extensions
 45          uint32_t ExtCount;
 46          vkEnumerateInstanceExtensionProperties(nullptr, &ExtCount, nullptr);
 47          std::vector<VkExtensionProperties> Ex(ExtCount);
 48          vkEnumerateInstanceExtensionProperties(nullptr, &ExtCount, Ex.data());
 49  }
 50  
 51  std::vector<const char *> CwGraphics::getExtensions() {
 52          uint32_t GlfwExtCount = 0;
 53          const char **GlfwExt = glfwGetRequiredInstanceExtensions(&GlfwExtCount);
 54          std::vector<const char *> Ex(GlfwExt, GlfwExt + GlfwExtCount);
 55  
 56          if (EnableValLayers) Ex.push_back("VK_EXT_debug_utils");
 57  
 58          return Ex;
 59  }
 60  
 61  void CwGraphics::Loop() {
 62          //        for (size_t i = 0; i < 10; i++) {
 63          while (!glfwWindowShouldClose(Window.Window)) {
 64                  glfwPollEvents();
 65                  DrawFrame();
 66          }
 67          vkDeviceWaitIdle(Dev);
 68  }
 69  
 70  void CwGraphics::Clean() {
 71          CleanUpChain();
 72  
 73          vkDestroyBuffer(Dev, VertexBuffer, nullptr);
 74          vkFreeMemory(Dev, VertexBufferMemory, nullptr);
 75  
 76          vkDestroyCommandPool(Dev, CommandPool, nullptr);
 77  
 78          vkDestroyPipeline(Dev, GraphicsPipeline, nullptr);
 79  
 80          vkDestroyPipelineLayout(Dev, PipelineLayout, nullptr);
 81          vkDestroyRenderPass(Dev, RenderPass, nullptr);
 82  
 83          vkDestroyDevice(Dev, nullptr);
 84  
 85          if (EnableValLayers) DestroyDebugMessenger(Instance, DebugMessenger, nullptr);
 86  
 87          vkDestroySurfaceKHR(Instance, Surface, nullptr);
 88          vkDestroyInstance(Instance, nullptr);
 89  
 90          glfwTerminate();
 91  }
 92  
 93  bool CwGraphics::CheckValSupport() {
 94          uint32_t LayerCount;
 95  
 96          vkEnumerateInstanceLayerProperties(&LayerCount, nullptr);
 97  
 98          std::vector<VkLayerProperties> AvailableLay(LayerCount);
 99          vkEnumerateInstanceLayerProperties(&LayerCount, AvailableLay.data());
100  
101          for (const char *LayerName : validationLayers)
102                  for (const auto &LayerProperties : AvailableLay)
103                          if (strcmp(LayerName, LayerProperties.layerName) == 0) return true;  //(layer found)
104  
105          return false;
106  }
107  void CwGraphics::CleanUpChain() {
108          for (auto Semaphore : Semaphores.RenderFinished) vkDestroySemaphore(Dev, Semaphore, nullptr);
109  
110          for (int i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) vkDestroySemaphore(Dev, ImageAvailable[i], nullptr);
111          for (int i = 0; i < MAX_FRAMES_IN_FLIGHT; i++) vkDestroyFence(Dev, FlightFence[i], nullptr);
112  
113          for (auto FrameBuffer : ChainFrameBuffers) vkDestroyFramebuffer(Dev, FrameBuffer, nullptr);
114          for (auto ImageView : ImageViews) vkDestroyImageView(Dev, ImageView, nullptr);
115          vkDestroySwapchainKHR(Dev, SwapChain, nullptr);
116  }
117  void CwGraphics::RecreateChain() {
118          int Width{}, Height{};
119          glfwGetFramebufferSize(Window.Window, &Width, &Height);
120          while (Width == 0 || Height == 0) {
121                  glfwGetFramebufferSize(Window.Window, &Width, &Height);
122                  glfwWaitEvents();
123          }
124          vkDeviceWaitIdle(Dev);
125  
126          CleanUpChain();
127  
128          vkDeviceWaitIdle(Dev);
129          CreateChain();
130          CreateImageViews();
131          CreateFrameBuffers();
132          CreateSynchronizationObjects();
133  }