@@ -21,8 +21,10 @@ VULKAN_HPP_DEFAULT_DISPATCH_LOADER_DYNAMIC_STORAGE
2121
2222// create a dispatcher, based on additional vkDevice/vkGetDeviceProcAddr
2323static void setup_dispacher (void ) {
24- #if 1
25- // investigate why this stopped working
24+ // both should work
25+ // but vk::DynamicLoader reloads the dll, so it will be open more then once
26+ // and also might be a different one from sdl
27+ #if 0
2628 static vk::DynamicLoader dl;
2729 PFN_vkGetInstanceProcAddr vkGetInstanceProcAddr = dl.getProcAddress<PFN_vkGetInstanceProcAddr>("vkGetInstanceProcAddr");
2830#else
@@ -44,10 +46,6 @@ static bool can_use_layer(std::string_view layer_want) {
4446 return false ;
4547}
4648
47- static bool can_use_validation (void ) {
48- return can_use_layer (" VK_LAYER_KHRONOS_validation" );
49- }
50-
5149VKAPI_ATTR VkBool32 VKAPI_CALL debug_callback (
5250 VkDebugUtilsMessageSeverityFlagBitsEXT messageSeverity,
5351 VkDebugUtilsMessageTypeFlagsEXT messageType,
@@ -162,12 +160,12 @@ VulkanRenderer::VulkanRenderer(void) {
162160VulkanRenderer::~VulkanRenderer (void ) {
163161}
164162
165- bool VulkanRenderer::enable (Engine& engine , std::vector<UpdateStrategies::TaskInfo>& task_array) {
163+ bool VulkanRenderer::enable (Engine&, std::vector<UpdateStrategies::TaskInfo>& task_array) {
166164 assert (!VULKAN_HPP_DEFAULT_DISPATCHER.vkEnumerateInstanceLayerProperties );
167165 setup_dispacher ();
168166 assert (VULKAN_HPP_DEFAULT_DISPATCHER.vkEnumerateInstanceLayerProperties );
169167
170- // create vulkan instance
168+ // TODO: user configurable
171169 const vk::ApplicationInfo app_info {
172170 " app_name" ,
173171 VK_MAKE_VERSION (1 , 0 , 0 ), // app version
@@ -179,7 +177,8 @@ bool VulkanRenderer::enable(Engine& engine, std::vector<UpdateStrategies::TaskIn
179177
180178 // TODO: make validation layer conditional
181179 std::vector<const char *> layers{};
182- if (can_use_validation ()) {
180+
181+ if (can_use_layer (" VK_LAYER_KHRONOS_validation" )) {
183182 layers.push_back (" VK_LAYER_KHRONOS_validation" );
184183 SPDLOG_INFO (" ENABLED validation layer" );
185184 } else {
@@ -195,6 +194,8 @@ bool VulkanRenderer::enable(Engine& engine, std::vector<UpdateStrategies::TaskIn
195194
196195 _debug_messenger = instance.createDebugUtilsMessengerEXT (debug_utils_messenger_create_info);
197196
197+ _swapchain_curr_idx = 0 ; // important
198+
198199 { // add task
199200 task_array.push_back (
200201 UpdateStrategies::TaskInfo{" VulkanRenderer::render" }
@@ -211,18 +212,20 @@ void VulkanRenderer::disable(Engine&) {
211212 if (_device) {
212213 vk::Device device{_device};
213214
215+ auto device_destroy_each = [&device](auto & container) {
216+ for (const auto & it : container) {
217+ device.destroy (it);
218+ }
219+ };
220+
214221 device.waitIdle ();
215222
216- for (const auto & fb : _swapchain_framebuffers) {
217- device.destroy (fb);
218- }
219- for (const auto & img_view : _swapchain_image_views) {
220- device.destroy (img_view);
221- }
223+ device_destroy_each (_swapchain_framebuffers);
224+ device_destroy_each (_swapchain_image_views);
222225 device.destroy (_swapchain);
223- device. destroy (_swapchain_sem_image_available);
224- device. destroy (_swapchain_sem_render_finished);
225- device. destroy (_swapchain_fence_in_flight);
226+ device_destroy_each (_swapchain_sem_image_available);
227+ device_destroy_each (_swapchain_sem_render_finished);
228+ device_destroy_each (_swapchain_fence_in_flight);
226229 device.destroy ();
227230 }
228231
@@ -238,47 +241,46 @@ void VulkanRenderer::render(Engine&) {
238241
239242 // wait for next fb/img/img_view to be free again
240243 // in most cases there are 2 but might be 1 or more
241- vk::Fence in_flight{_swapchain_fence_in_flight};
244+ vk::Fence in_flight{_swapchain_fence_in_flight. at (_swapchain_curr_idx) };
242245 auto wait_in_flight_res = device.waitForFences (in_flight, true , UINT64_MAX);
243246 device.resetFences (in_flight);
244247
245248 uint32_t next_img_index = device.acquireNextImageKHR (
246249 _swapchain,
247250 UINT64_MAX,
248- _swapchain_sem_image_available
251+ _swapchain_sem_image_available. at (_swapchain_curr_idx)
249252 ).value ;
250253
251254 {
252255 auto g_queue = vk::Queue{_graphics_queue};
253256 // do the commands n stuff
254257
255258 // queue submit
256- vk::Semaphore tmp_sem_wait{_swapchain_sem_image_available};
259+ vk::Semaphore tmp_sem_wait{_swapchain_sem_image_available. at (_swapchain_curr_idx) };
257260 vk::PipelineStageFlags tmp_sem_wait_stages{vk::PipelineStageFlagBits::eColorAttachmentOutput};
258- vk::Semaphore tmp_sem_sig{_swapchain_sem_render_finished};
261+ vk::Semaphore tmp_sem_sig{_swapchain_sem_render_finished. at (_swapchain_curr_idx) };
259262 g_queue.submit ({vk::SubmitInfo{
260263 tmp_sem_wait,
261264 tmp_sem_wait_stages,
262265 {},
263266 tmp_sem_sig
264- }}, _swapchain_fence_in_flight);
267+ }}, _swapchain_fence_in_flight. at (_swapchain_curr_idx) );
265268 }
266269
267270 { // queue present
268271 auto p_queue = vk::Queue{_graphics_queue}; // TODO: present queue
269272
270- vk::Semaphore tmp_sem_wait{_swapchain_sem_render_finished};
273+ vk::Semaphore tmp_sem_wait{_swapchain_sem_render_finished. at (_swapchain_curr_idx) };
271274 auto present_res = p_queue.presentKHR (vk::PresentInfoKHR{
272275 tmp_sem_wait,
273276 swapchain,
274277 // _swapchain_curr_idx
275278 next_img_index
276279 });
277280
278- // TODO: do i need this??
279- // next image
280- _swapchain_curr_idx = (_swapchain_curr_idx + 1 ) % _swapchain_images.size ();
281281 }
282+ // next image (everything)
283+ _swapchain_curr_idx = (_swapchain_curr_idx + 1 ) % _swapchain_images.size ();
282284}
283285
284286bool VulkanRenderer::createDevice (Engine& engine) {
@@ -373,9 +375,6 @@ bool VulkanRenderer::createDevice(Engine& engine) {
373375 // we assume it also does present
374376 _graphics_queue = device.getQueue (0 , 0 );
375377
376- _swapchain_sem_image_available = device.createSemaphore (vk::SemaphoreCreateInfo{});
377- _swapchain_sem_render_finished = device.createSemaphore (vk::SemaphoreCreateInfo{});
378- _swapchain_fence_in_flight = device.createFence ({vk::FenceCreateFlagBits::eSignaled});
379378
380379 return true ;
381380}
@@ -473,6 +472,16 @@ bool VulkanRenderer::createSwapchain(Engine& engine) {
473472 }));
474473 }
475474
475+ // TODO: max simultanious frames
476+ _swapchain_sem_image_available.clear ();
477+ _swapchain_sem_render_finished.clear ();
478+ _swapchain_fence_in_flight.clear ();
479+ for (size_t i = 0 ; i < _swapchain_images.size (); i++) {
480+ _swapchain_sem_image_available.push_back (device.createSemaphore (vk::SemaphoreCreateInfo{}));
481+ _swapchain_sem_render_finished.push_back (device.createSemaphore (vk::SemaphoreCreateInfo{}));
482+ _swapchain_fence_in_flight.push_back (device.createFence ({vk::FenceCreateFlagBits::eSignaled}));
483+ }
484+
476485 return true ;
477486}
478487
0 commit comments