example_widget.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #include <iostream>
  2. //#define VKW_WINDOW_LIB 1
  3. #if VKW_WINDOW_LIB == 1
  4. #include <vkw/SDLWidget.h>
  5. #elif VKW_WINDOW_LIB == 2
  6. #include <vkw/GLFWWidget.h>
  7. #endif
  8. // callback function for validation layers
  9. static VKAPI_ATTR VkBool32 VKAPI_CALL VulkanReportFunc(
  10. VkDebugReportFlagsEXT flags,
  11. VkDebugReportObjectTypeEXT objType,
  12. uint64_t obj,
  13. size_t location,
  14. int32_t code,
  15. const char* layerPrefix,
  16. const char* msg,
  17. void* userData)
  18. {
  19. std::cout << "VULKAN VALIDATION: " << layerPrefix << " :: " << msg << std::endl;;
  20. return VK_FALSE;
  21. }
  22. // This header file contains the actual
  23. // rendering code for vulkan. It can be used:
  24. // GLFWVulkanWidget,
  25. // SDLVulkanWidget,
  26. // QTVulkanWidget
  27. //
  28. #include "example_myApplication.h"
  29. int MAIN(int argc, char *argv[])
  30. {
  31. (void)argc;
  32. (void)argv;
  33. // create a vulkan window widget
  34. #if VKW_WINDOW_LIB == 1
  35. using WidgetType = vkw::SDLVulkanWidget;
  36. #elif VKW_WINDOW_LIB == 2
  37. using WidgetType = vkw::GLFWVulkanWidget;
  38. #endif
  39. WidgetType vulkanWindow;
  40. // set the initial properties of the
  41. // window. Also specify that we want
  42. // a depth stencil attachment
  43. WidgetType::CreateInfo c;
  44. c.width = 1024;
  45. c.height = 768;
  46. c.windowTitle = "My Vulkan Application Window";
  47. // configure the vulkan instance
  48. c.instanceInfo.vulkanVersion = VK_MAKE_VERSION(1,2,0);
  49. c.instanceInfo.debugCallback = &VulkanReportFunc;
  50. // Set up the surface information
  51. c.surfaceInfo.presentMode = VK_PRESENT_MODE_FIFO_KHR;
  52. c.surfaceInfo.depthFormat = VK_FORMAT_D32_SFLOAT_S8_UINT;
  53. // Configure the device extensions you would like
  54. c.deviceInfo.deviceExtensions.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME);
  55. c.deviceInfo.deviceExtensions.push_back(VK_EXT_TRANSFORM_FEEDBACK_EXTENSION_NAME);
  56. // enable a new extended feature
  57. VkPhysicalDeviceVertexInputDynamicStateFeaturesEXT dynamicVertexState = {};
  58. dynamicVertexState.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VERTEX_INPUT_DYNAMIC_STATE_FEATURES_EXT;
  59. dynamicVertexState.vertexInputDynamicState = true;
  60. c.deviceInfo.enabledFeatures12.pNext = &dynamicVertexState;
  61. // Here is the actual vulkan application that does
  62. // all the rendering.
  63. MyApplication app;
  64. {
  65. #if VKW_WINDOW_LIB == 1
  66. // This needs to be called first to initialize SDL
  67. SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS);
  68. vulkanWindow.create(c);
  69. // put the window in the main loop
  70. // and provide a callback function for the SDL events
  71. vulkanWindow.exec(&app,
  72. [&app](SDL_Event const & evt)
  73. {
  74. if( evt.type == SDL_QUIT)
  75. app.quit();
  76. });
  77. vulkanWindow.destroy();
  78. SDL_Quit();
  79. #elif VKW_WINDOW_LIB == 2
  80. glfwInit();
  81. vulkanWindow.create(c);
  82. // put the window in the main loop
  83. // GLFW requires you to register callbacks
  84. // for input events. you will have to do these yourself
  85. vulkanWindow.exec(&app);
  86. vulkanWindow.destroy();
  87. glfwTerminate();
  88. #endif
  89. }
  90. return 0;
  91. }
  92. #if VKW_WINDOW_LIB == 1
  93. #if defined(__WIN32__)
  94. int SDL_main(int argc, char *argv[])
  95. #else
  96. int main(int argc, char *argv[])
  97. #endif
  98. {
  99. return MAIN(argc, argv);
  100. }
  101. #elif VKW_WINDOW_LIB == 2
  102. int main(int argc, char *argv[])
  103. {
  104. return MAIN(argc, argv);
  105. }
  106. #endif
  107. #include <vkw/VKWVulkanWindow.inl>