55
66#include < vulkan/vulkan.hpp>
77
8+ // mf ycm, FIXME: remove before merge
9+ #include < vulkan/vulkan_handles.hpp>
10+ #include < vulkan/vulkan_core.h>
11+ #include < vulkan/vulkan_structs.hpp>
12+ #include < vulkan/vulkan_enums.hpp>
13+ #include < vulkan/vulkan_funcs.hpp>
14+
815#include < SDL_vulkan.h>
916
1017#include < mm/logger.hpp>
@@ -188,6 +195,14 @@ bool VulkanRenderer::enable(Engine& engine, std::vector<UpdateStrategies::TaskIn
188195
189196 _debug_messenger = instance.createDebugUtilsMessengerEXT (debug_utils_messenger_create_info);
190197
198+ { // add task
199+ task_array.push_back (
200+ UpdateStrategies::TaskInfo{" VulkanRenderer::render" }
201+ .phase (UpdateStrategies::update_phase_t ::POST) // *smirk*
202+ .fn ([this ](Engine& e){ this ->render (e); })
203+ );
204+ }
205+
191206 return true ;
192207}
193208
@@ -196,13 +211,18 @@ void VulkanRenderer::disable(Engine&) {
196211 if (_device) {
197212 vk::Device device{_device};
198213
214+ device.waitIdle ();
215+
199216 for (const auto & fb : _swapchain_framebuffers) {
200217 device.destroy (fb);
201218 }
202219 for (const auto & img_view : _swapchain_image_views) {
203220 device.destroy (img_view);
204221 }
205222 device.destroy (_swapchain);
223+ device.destroy (_swapchain_sem_image_available);
224+ device.destroy (_swapchain_sem_render_finished);
225+ device.destroy (_swapchain_fence_in_flight);
206226 device.destroy ();
207227 }
208228
@@ -212,6 +232,55 @@ void VulkanRenderer::disable(Engine&) {
212232 instance.destroy ();
213233}
214234
235+ void VulkanRenderer::render (Engine&) {
236+ vk::Device device{_device};
237+ vk::SwapchainKHR swapchain{_swapchain};
238+
239+ // wait for next fb/img/img_view to be free again
240+ // in most cases there are 2 but might be 1 or more
241+ vk::Fence in_flight{_swapchain_fence_in_flight};
242+ auto wait_in_flight_res = device.waitForFences (in_flight, true , UINT64_MAX);
243+ device.resetFences (in_flight);
244+
245+ uint32_t next_img_index = device.acquireNextImageKHR (
246+ _swapchain,
247+ UINT64_MAX,
248+ _swapchain_sem_image_available
249+ ).value ;
250+
251+ {
252+ auto g_queue = vk::Queue{_graphics_queue};
253+ // do the commands n stuff
254+
255+ // queue submit
256+ vk::Semaphore tmp_sem_wait{_swapchain_sem_image_available};
257+ vk::PipelineStageFlags tmp_sem_wait_stages{vk::PipelineStageFlagBits::eColorAttachmentOutput};
258+ vk::Semaphore tmp_sem_sig{_swapchain_sem_render_finished};
259+ g_queue.submit ({vk::SubmitInfo{
260+ tmp_sem_wait,
261+ tmp_sem_wait_stages,
262+ {},
263+ tmp_sem_sig
264+ }}, _swapchain_fence_in_flight);
265+ }
266+
267+ { // queue present
268+ auto p_queue = vk::Queue{_graphics_queue}; // TODO: present queue
269+
270+ vk::Semaphore tmp_sem_wait{_swapchain_sem_render_finished};
271+ auto present_res = p_queue.presentKHR (vk::PresentInfoKHR{
272+ tmp_sem_wait,
273+ swapchain,
274+ // _swapchain_curr_idx
275+ next_img_index
276+ });
277+
278+ // TODO: do i need this??
279+ // next image
280+ _swapchain_curr_idx = (_swapchain_curr_idx + 1 ) % _swapchain_images.size ();
281+ }
282+ }
283+
215284bool VulkanRenderer::createDevice (Engine& engine) {
216285 // the surface for the window (not device dependent)
217286 if (SDL_Vulkan_CreateSurface (engine.getService <SDLService>().win , _instance, &_surface) != SDL_TRUE) {
@@ -304,6 +373,10 @@ bool VulkanRenderer::createDevice(Engine& engine) {
304373 // we assume it also does present
305374 _graphics_queue = device.getQueue (0 , 0 );
306375
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});
379+
307380 return true ;
308381}
309382
@@ -383,6 +456,7 @@ bool VulkanRenderer::createSwapchain(Engine& engine) {
383456 }));
384457 }
385458
459+
386460 // TODO: move
387461
388462 _swapchain_framebuffers.clear ();
0 commit comments