~sircmpwn/xrgears

3ed8325f5675b9eed0d3567255613b1a8af6b33f — Lubosz Sarnecki 4 years ago 3e8200e
vkc: make swapchain render callback.
M vitamin-k/vikSwapChain.hpp => vitamin-k/vikSwapChain.hpp +7 -0
@@ 3,6 3,7 @@
#include <vulkan/vulkan.h>

#include <vector>
#include <functional>

#include "../vks/vksLog.hpp"



@@ 20,9 21,15 @@ public:
  uint32_t image_count = 0;
  VkSurfaceFormatKHR surface_format;

  std::function<void(uint32_t index)> render_cb;

  SwapChain() {}
  ~SwapChain() {}

  void set_render_cb(std::function<void(uint32_t index)> cb) {
    render_cb = cb;
  }

  void create_image_view(const VkDevice &device, const VkImage& image,
                         const VkFormat &format, VkImageView *view) {
    VkImageViewCreateInfo view_create_info = {};

M vitamin-k/vikSwapChainVK.hpp => vitamin-k/vikSwapChainVK.hpp +21 -4
@@ 8,12 8,11 @@ namespace vik {
class SwapChainVK : public SwapChain {

protected:
 VkInstance instance;
 VkDevice device;
 VkPhysicalDevice physical_device;
  VkInstance instance;
  VkDevice device;
  VkPhysicalDevice physical_device;

public:

  /** @brief Handle to the current swap chain, required for recreation */
  VkSwapchainKHR swap_chain = VK_NULL_HANDLE;



@@ 199,5 198,23 @@ public:
    return mode;
  }

  void render(VkQueue queue, VkSemaphore semaphore) {
    uint32_t present_index = 0;
    VkResult result = acquire_next_image(semaphore, &present_index);
    switch (result) {
      case VK_SUCCESS:
        render_cb(present_index);
        vik_log_check(present(queue, present_index));
        break;
      case VK_TIMEOUT:
        // TODO: XCB times out
        break;
      default:
        vik_log_e("vkAcquireNextImageKHR failed: %s",
                   vks::Log::result_string(result).c_str());
        break;
    }
  }

};
}

M vkc/vkcRenderer.hpp => vkc/vkcRenderer.hpp +0 -18
@@ 324,24 324,6 @@ public:
    wait_and_reset_fences();
  }

  void render_swapchain_vk(vik::SwapChainVK *swap_chain) {
    uint32_t present_index = 0;
    VkResult result = swap_chain->acquire_next_image(semaphore, &present_index);
    switch (result) {
      case VK_SUCCESS:
        render(present_index);
        vik_log_check(swap_chain->present(queue, present_index));
        break;
      case VK_TIMEOUT:
        // TODO: XCB times out
        break;
      default:
        vik_log_e("vkAcquireNextImageKHR failed: %s",
                   vks::Log::result_string(result).c_str());
        break;
    }
  }

  void create_frame_buffer(VkImageView *view, VkFramebuffer *frame_buffer) {
    VkFramebufferCreateInfo framebufferinfo = {};
    framebufferinfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;

M vkc/vkcWindowWayland.hpp => vkc/vkcWindowWayland.hpp +7 -1
@@ 299,6 299,11 @@ public:
    vik::SwapChainVK *sc = (vik::SwapChainVK*) r->swap_chain;
    sc->set_context(r->instance, r->physical_device, r->device);

    vkc::Renderer *vkc_renderer = (vkc::Renderer*) r;
    std::function<void(uint32_t index)> render_cb =
        [vkc_renderer](uint32_t index) { vkc_renderer->render(index); };
    sc->set_render_cb(render_cb);

    create_surface(r->instance, &sc->surface);

    sc->choose_surface_format();


@@ 330,7 335,8 @@ public:
  void iterate(Renderer *r) {
    flush();
    update_cb();
    r->render_swapchain_vk((vik::SwapChainVK*) r->swap_chain);
    vik::SwapChainVK* sc = (vik::SwapChainVK*) r->swap_chain;
    sc->render(r->queue, r->semaphore);
  }



M vkc/vkcWindowXCB.hpp => vkc/vkcWindowXCB.hpp +8 -1
@@ 127,6 127,11 @@ public:
    vik::SwapChainVK *sc = (vik::SwapChainVK*) r->swap_chain;
    sc->set_context(r->instance, r->physical_device, r->device);

    vkc::Renderer *vkc_renderer = (vkc::Renderer*) r;
    std::function<void(uint32_t index)> render_cb =
        [vkc_renderer](uint32_t index) { vkc_renderer->render(index); };
    sc->set_render_cb(render_cb);

    create_surface(r->instance, &sc->surface);

    sc->choose_surface_format();


@@ 213,7 218,9 @@ public:
    if (repaint) {

      update_cb();
      r->render_swapchain_vk((vik::SwapChainVK*) r->swap_chain);

      vik::SwapChainVK* sc = (vik::SwapChainVK*) r->swap_chain;
      sc->render(r->queue, r->semaphore);
      schedule_repaint();
    }
    xcb_flush(conn);