example_SDLWindow.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #include <iostream>
  2. #include <vkw/VKWVulkanWindow.h>
  3. #include <vkw/Adapters/SDLVulkanWindowAdapter.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. #if defined(__WIN32__)
  19. int SDL_main(int argc, char *argv[])
  20. #else
  21. int main(int argc, char *argv[])
  22. #endif
  23. {
  24. // This needs to be called first to initialize SDL
  25. SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS);
  26. // create a default window and initialize all vulkan
  27. // objects.
  28. auto window = new vkw::VKWVulkanWindow();
  29. auto sdl_window = new vkw::SDLVulkanWindowAdapter();
  30. // 1. create the window and set the adapater
  31. sdl_window->createWindow("Title", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1024,768);
  32. window->setWindowAdapater(sdl_window);
  33. // 2. Create the Instance
  34. vkw::VKWVulkanWindow::InstanceInitilizationInfo2 instanceInfo;
  35. instanceInfo.debugCallback = &VulkanReportFunc;
  36. instanceInfo.vulkanVersion = VK_MAKE_VERSION(1, 2, 0);
  37. instanceInfo.enabledExtensions.push_back(VK_KHR_GET_PHYSICAL_DEVICE_PROPERTIES_2_EXTENSION_NAME);
  38. window->createVulkanInstance(instanceInfo);
  39. // 3. Create the surface
  40. vkw::VKWVulkanWindow::SurfaceInitilizationInfo2 surfaceInfo;
  41. surfaceInfo.depthFormat = VK_FORMAT_D32_SFLOAT_S8_UINT;
  42. surfaceInfo.presentMode = VK_PRESENT_MODE_FIFO_KHR;
  43. surfaceInfo.additionalImageCount = 1;// how many additional swapchain images should we create ( total = min_images + additionalImageCount
  44. window->createVulkanSurface(surfaceInfo);
  45. // 4. Create the device
  46. // and add additional extensions that we want to enable
  47. vkw::VKWVulkanWindow::DeviceInitilizationInfo2 deviceInfo;
  48. #if 0
  49. // set to zero if you want to VKW to choose
  50. // a device for you
  51. deviceInfo.deviceID = 0;
  52. #else
  53. for(auto &d : window->getAvailablePhysicalDevices())
  54. {
  55. if(d.deviceType == VK_PHYSICAL_DEVICE_TYPE_DISCRETE_GPU)
  56. {
  57. deviceInfo.deviceID = d.deviceID;
  58. break;
  59. }
  60. }
  61. #endif
  62. deviceInfo.deviceExtensions.push_back(VK_KHR_SWAPCHAIN_EXTENSION_NAME);
  63. deviceInfo.deviceExtensions.push_back(VK_EXT_TRANSFORM_FEEDBACK_EXTENSION_NAME);
  64. // enable a new extended feature
  65. VkPhysicalDeviceVertexInputDynamicStateFeaturesEXT dynamicVertexState = {};
  66. dynamicVertexState.sType = VK_STRUCTURE_TYPE_PHYSICAL_DEVICE_VERTEX_INPUT_DYNAMIC_STATE_FEATURES_EXT;
  67. dynamicVertexState.vertexInputDynamicState = true;
  68. deviceInfo.enabledFeatures12.pNext = &dynamicVertexState;
  69. window->createVulkanDevice(deviceInfo);
  70. bool running=true;
  71. while(running)
  72. {
  73. SDL_Event event;
  74. bool resize=false;
  75. while (SDL_PollEvent(&event))
  76. {
  77. if (event.type == SDL_QUIT)
  78. {
  79. running = false;
  80. }
  81. else if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED
  82. /*&& event.window.windowID == SDL_GetWindowID( window->getSDLWindow()) */ )
  83. {
  84. resize=true;
  85. }
  86. }
  87. if( resize )
  88. {
  89. // If the window has changed size. we need to rebuild the swapchain
  90. // and any other textures (depth texture)
  91. window->rebuildSwapchain();
  92. }
  93. // Get the next available frame.
  94. // the Frame struct is simply a POD containing
  95. // all the information that you need to record a command buffer
  96. auto frame = window->acquireNextFrame();
  97. frame.beginCommandBuffer();
  98. frame.clearColor = {{1.f,0.f,0.f,0.f}};
  99. frame.beginRenderPass( frame.commandBuffer );
  100. // record to frame.commandbuffer
  101. frame.endRenderPass(frame.commandBuffer);
  102. frame.endCommandBuffer();
  103. window->submitFrame(frame);
  104. // Present the frame after you have recorded
  105. // the command buffer;
  106. window->presentFrame(frame);
  107. window->waitForPresent();
  108. }
  109. // delete the window to destroy all objects
  110. // that were created.
  111. window->destroy();
  112. delete window;
  113. SDL_Quit();
  114. return 0;
  115. }
  116. #include <vkw/VKWVulkanWindow.inl>