example_GLFWWindow.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. #include <iostream>
  2. #include <vkw/VKWVulkanWindow.h>
  3. #include <vkw/Adapters/GLFWVulkanWindowAdapter.h>
  4. // callback function for validation layers
  5. static VKAPI_ATTR VkBool32 VKAPI_CALL VulkanReportFunc(
  6. VkDebugReportFlagsEXT flags,
  7. VkDebugReportObjectTypeEXT objType,
  8. uint64_t obj,
  9. size_t location,
  10. int32_t code,
  11. const char* layerPrefix,
  12. const char* msg,
  13. void* userData)
  14. {
  15. printf("VULKAN VALIDATION: [%s] %s\n", layerPrefix, msg);
  16. return VK_FALSE;
  17. }
  18. int main(int argc, char *argv[])
  19. {
  20. // This needs to be called first to initialize SDL
  21. glfwInit();
  22. // create a default window and initialize all vulkan
  23. // objects.
  24. auto window = new vkw::VKWVulkanWindow();
  25. auto glfw_window = new vkw::GLFWVulkanWindowAdapter();
  26. // 1. create the window and set the adapater
  27. glfw_window->createWindow("Title", 1024,768);
  28. window->setWindowAdapater(glfw_window);
  29. // 2. Create the Instance
  30. vkw::VKWVulkanWindow::InstanceInitilizationInfo2 instanceInfo;
  31. instanceInfo.debugCallback = &VulkanReportFunc;
  32. instanceInfo.vulkanVersion = VK_MAKE_VERSION(1, 2, 0);
  33. instanceInfo.enabledExtensions.push_back(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME);
  34. window->createVulkanInstance(instanceInfo);
  35. // 3. Create the surface
  36. vkw::VKWVulkanWindow::SurfaceInitilizationInfo2 surfaceInfo;
  37. surfaceInfo.depthFormat = VkFormat::VK_FORMAT_D32_SFLOAT_S8_UINT;
  38. surfaceInfo.presentMode = VK_PRESENT_MODE_FIFO_KHR;
  39. surfaceInfo.additionalImageCount = 1;// how many additional swapchain images should we create ( total = min_images + additionalImageCount
  40. window->createVulkanSurface(surfaceInfo);
  41. // 4. Create the device
  42. // and add additional extensions that we want to enable
  43. vkw::VKWVulkanWindow::DeviceInitilizationInfo2 deviceInfo;
  44. #if 0
  45. // set to zero if you want to VKW to choose
  46. // a device for you
  47. deviceInfo.deviceID = 0;
  48. #else
  49. for(auto &d : window->getAvailablePhysicalDevices())
  50. {
  51. if(d.deviceType == VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU)
  52. {
  53. deviceInfo.deviceID = d.deviceID;
  54. break;
  55. }
  56. }
  57. #endif
  58. deviceInfo.deviceExtensions.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME);
  59. deviceInfo.deviceExtensions.push_back(VK_EXT_TRANSFORM_FEEDBACK_EXTENSION_NAME);
  60. // enable a new extended feature
  61. VkPhysicalDeviceVertexInputDynamicStateFeaturesEXT dynamicVertexState = {};
  62. dynamicVertexState.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VERTEX_INPUT_DYNAMIC_STATE_FEATURES_EXT;
  63. dynamicVertexState.vertexInputDynamicState = true;
  64. deviceInfo.enabledFeatures12.pNext = &dynamicVertexState;
  65. window->createVulkanDevice(deviceInfo);
  66. bool running = true;
  67. while (!glfwWindowShouldClose(glfw_window->m_window) )
  68. {
  69. glfwPollEvents();
  70. bool resize = glfw_window->requiresResize();
  71. glfw_window->clearRequireResize();
  72. if( resize )
  73. {
  74. // If the window has changed size. we need to rebuild the swapchain
  75. // and any other textures (depth texture)
  76. window->rebuildSwapchain();
  77. }
  78. // Get the next available frame.
  79. // the Frame struct is simply a POD containing
  80. // all the information that you need to record a command buffer
  81. auto frame = window->acquireNextFrame();
  82. frame.beginCommandBuffer();
  83. frame.clearColor = {{1.f,0.f,0.f,0.f}};
  84. frame.beginRenderPass( frame.commandBuffer );
  85. // record to frame.commandbuffer
  86. frame.endRenderPass(frame.commandBuffer);
  87. frame.endCommandBuffer();
  88. window->submitFrame(frame);
  89. // Present the frame after you have recorded
  90. // the command buffer;
  91. window->presentFrame(frame);
  92. window->waitForPresent();
  93. }
  94. // delete the window to destroy all objects
  95. // that were created.
  96. window->destroy();
  97. delete window;
  98. glfwTerminate();
  99. return 0;
  100. }
  101. #include <vkw/VKWVulkanWindow.inl>