~sircmpwn/xrgears

e30503a17df5559db8ec9b96ce2590439487ac66 — Lubosz Sarnecki 4 years ago 7eb743c
move context passing out of init swap chain, make common function for creating frame buffers.
M vitamin-k/render/vikRenderer.hpp => vitamin-k/render/vikRenderer.hpp +18 -0
@@ 70,5 70,23 @@ public:

    return vkCreateInstance(&instance_info, nullptr, &instance);
  }

  void create_frame_buffer(VkFramebuffer *frame_buffer,
                           const std::vector<VkImageView> &attachments) {
    VkFramebufferCreateInfo frame_buffer_info = {};
    frame_buffer_info.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
    frame_buffer_info.renderPass = render_pass;
    frame_buffer_info.attachmentCount = attachments.size();
    frame_buffer_info.pAttachments = attachments.data();
    frame_buffer_info.width = width;
    frame_buffer_info.height = height;
    frame_buffer_info.layers = 1;

    vik_log_check(vkCreateFramebuffer(device,
                                      &frame_buffer_info,
                                      nullptr,
                                      frame_buffer));
  }

};
}

M vitamin-k/render/vikRendererVkc.hpp => vitamin-k/render/vikRendererVkc.hpp +18 -23
@@ 35,8 35,8 @@ public:
    height = s->height;
    gettimeofday(&start_tv, NULL);

    auto recreate_swap_chain_vk_cb = [this](SwapChain *sc) {
      create_frame_buffers(sc);
    auto recreate_swap_chain_vk_cb = [this]() {
      create_frame_buffers();
    };
    window->set_recreate_swap_chain_vk_cb(recreate_swap_chain_vk_cb);



@@ 60,11 60,13 @@ public:
    if (!window->check_support(physical_device))
      vik_log_f("Vulkan not supported on given surface");

    window->init_swap_chain(instance, physical_device, device, width, height);
    window->get_swap_chain()->set_context(instance, physical_device, device);

    window->init_swap_chain(width, height);

    auto render_cb = [this](uint32_t index) { render(index); };
    window->get_swap_chain()->set_render_cb(render_cb);
    create_frame_buffers(window->get_swap_chain());
    create_frame_buffers();
  }

  void init_vulkan(const std::string& name,


@@ 318,31 320,24 @@ public:
                 "vkEndCommandBuffer: %s", Log::result_string(r).c_str());
  }

  void iterate() {
    window->iterate(queue, semaphore);
  }

  void render(uint32_t index) {
    build_command_buffer(frame_buffers[index]);
    submit_queue();
    wait_and_reset_fences();
  }

  void create_frame_buffer(VkImageView *view, VkFramebuffer *frame_buffer) {
    VkFramebufferCreateInfo framebufferinfo = {};
    framebufferinfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
    framebufferinfo.renderPass = render_pass;
    framebufferinfo.attachmentCount = 1;
    framebufferinfo.pAttachments = view;
    framebufferinfo.width = width;
    framebufferinfo.height = height;
    framebufferinfo.layers = 1;
    vkCreateFramebuffer(device,
                        &framebufferinfo,
                        NULL,
                        frame_buffer);
  }

  void create_frame_buffers(SwapChain *sc) {
    frame_buffers.resize(sc->image_count);
    for (uint32_t i = 0; i < sc->image_count; i++) {
      create_frame_buffer(&sc->buffers[i].view, &frame_buffers[i]);
  void create_frame_buffers() {
    uint32_t count = window->get_swap_chain()->image_count;
    frame_buffers.resize(count);
    for (uint32_t i = 0; i < count; i++) {
      std::vector<VkImageView> attachments = {
        window->get_swap_chain()->buffers[i].view
      };
      create_frame_buffer(&frame_buffers[i], attachments);
    }
  }
};

M vitamin-k/render/vikRendererVks.hpp => vitamin-k/render/vikRendererVks.hpp +9 -20
@@ 120,9 120,9 @@ public:
    init_vulkan(name, window->required_extensions());
    window->init(width, height, settings->fullscreen);

    std::string windowTitle = make_title_string(title);
    window->update_window_title(windowTitle);
    window->init_swap_chain(instance, physical_device, device, width, height);
    window->update_window_title(make_title_string(title));
    window->get_swap_chain()->set_context(instance, physical_device, device);
    window->init_swap_chain(width, height);

    if (vksDevice->enableDebugMarkers)
      debugmarker::setup(device);


@@ 514,29 514,18 @@ public:
  }

  void create_frame_buffers() {
    vik_log_d("setupFrameBuffer");
    uint32_t count = window->get_swap_chain()->image_count;

    // Create frame buffers for every swap chain image
    frame_buffers.resize(count);

    VkImageView attachments[2];

    std::vector<VkImageView> attachments = std::vector<VkImageView>(2);
    // Depth/Stencil attachment is the same for all frame buffers
    attachments[1] = depthStencil.view;

    VkFramebufferCreateInfo frameBufferCreateInfo = {};
    frameBufferCreateInfo.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
    frameBufferCreateInfo.pNext = NULL;
    frameBufferCreateInfo.renderPass = render_pass;
    frameBufferCreateInfo.attachmentCount = 2;
    frameBufferCreateInfo.pAttachments = attachments;
    frameBufferCreateInfo.width = width;
    frameBufferCreateInfo.height = height;
    frameBufferCreateInfo.layers = 1;

    // Create frame buffers for every swap chain image
    frame_buffers.resize(window->get_swap_chain()->image_count);
    for (uint32_t i = 0; i < frame_buffers.size(); i++) {
    for (uint32_t i = 0; i < count; i++) {
      attachments[0] = window->get_swap_chain()->buffers[i].view;
      vik_log_check(vkCreateFramebuffer(device, &frameBufferCreateInfo, nullptr, &frame_buffers[i]));
      create_frame_buffer(&frame_buffers[i], attachments);
    }
  }


M vitamin-k/render/vikSwapChain.hpp => vitamin-k/render/vikSwapChain.hpp +17 -0
@@ 16,6 16,9 @@ struct SwapChainBuffer {

class SwapChain {
public:
  VkPhysicalDevice physical_device;
  VkDevice device;
  VkInstance instance;
  std::vector<SwapChainBuffer> buffers;

  uint32_t image_count = 0;


@@ 30,6 33,20 @@ public:
    render_cb = cb;
  }

  /**
  * Set instance, physical and logical device to use for the swapchain and get all required function pointers
  *
  * @param instance Vulkan instance to use
  * @param physicalDevice Physical device used to query properties and formats relevant to the swapchain
  * @param device Logical representation of the device to create the swapchain for
  *
  */
  void set_context(VkInstance i, VkPhysicalDevice p, VkDevice d) {
    instance = i;
    physical_device = p;
    device = d;
  }

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

M vitamin-k/render/vikSwapChainDRM.hpp => vitamin-k/render/vikSwapChainDRM.hpp +5 -5
@@ 28,7 28,7 @@

namespace vik {

struct kms_buffer {
struct KMSBuffer {
  gbm_bo *gbm_buffer;
  VkDeviceMemory mem;
  uint32_t fb;


@@ 39,7 39,7 @@ class SwapChainDRM : public SwapChain {

public:

  std::vector<kms_buffer> kms_buffers;
  std::vector<KMSBuffer> kms_buffers;
  int current;

  SwapChainDRM() {


@@ 57,8 57,8 @@ public:
        (PFN_vkCreateDmaBufImageINTEL)vkGetDeviceProcAddr(device, "vkCreateDmaBufImageINTEL");

    for (uint32_t i = 0; i < image_count; i++) {
      vik::SwapChainBuffer *b = &buffers[i];
      kms_buffer *kms_b = &kms_buffers[i];
      SwapChainBuffer *b = &buffers[i];
      KMSBuffer *kms_b = &kms_buffers[i];
      int buffer_fd, stride, ret;

      kms_b->gbm_buffer = gbm_bo_create(gbm_dev, width, height,


@@ 116,7 116,7 @@ public:
    int index = current & 1;

    render_cb(index);
    vik::kms_buffer *kms_b = &kms_buffers[index];
    KMSBuffer *kms_b = &kms_buffers[index];
    int ret = drmModePageFlip(fd, crtc_id, kms_b->fb,
                              DRM_MODE_PAGE_FLIP_EVENT, NULL);
    vik_log_f_if(ret < 0, "pageflip failed: %m");

M vitamin-k/render/vikSwapChainVK.hpp => vitamin-k/render/vikSwapChainVK.hpp +0 -20
@@ 6,12 6,6 @@

namespace vik {
class SwapChainVK : public SwapChain {

protected:
  VkInstance instance;
  VkDevice device;
  VkPhysicalDevice physical_device;

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


@@ 143,20 137,6 @@ public:
    assert(surface_format.format != VK_FORMAT_UNDEFINED);
  }

  /**
  * Set instance, physical and logical device to use for the swapchain and get all required function pointers
  *
  * @param instance Vulkan instance to use
  * @param physicalDevice Physical device used to query properties and formats relevant to the swapchain
  * @param device Logical representation of the device to create the swapchain for
  *
  */
  void set_context(VkInstance i, VkPhysicalDevice p, VkDevice d) {
    instance = i;
    physical_device = p;
    device = d;
  }

  void update_images() {
    vkGetSwapchainImagesKHR(device, swap_chain, &image_count, NULL);
    assert(image_count > 0);

M vitamin-k/system/vikApplicationVkc.hpp => vitamin-k/system/vikApplicationVkc.hpp +3 -5
@@ 21,10 21,8 @@ public:
    init_window();
    renderer = new RendererVkc(&settings, window);

    auto update_cb = [this]() { update_scene(); };
    window->set_update_cb(update_cb);
    auto quit_cb = [this]() { quit = true; };
    window->set_quit_cb(quit_cb);
    window->set_update_cb([this]() { update_scene(); });
    window->set_quit_cb([this]() { quit = true; });

    std::function<void(Input::Key key, bool state)> keyboard_key_cb =
        [this](Input::Key key, bool state) {


@@ 70,7 68,7 @@ public:

  void loop() {
    while (!quit)
      window->iterate(renderer->queue, renderer->semaphore);
      renderer->iterate();
  }

  virtual void init_cb() = 0;

M vitamin-k/window/vikWindow.hpp => vitamin-k/window/vikWindow.hpp +3 -9
@@ 14,8 14,7 @@ public:
  std::function<void()> update_cb;
  std::function<void()> quit_cb;

  std::function<void(SwapChain *sc)> recreate_frame_buffers_cb;
  std::function<void()> recreate_swap_chain_drm_cb;
  std::function<void()> recreate_frame_buffers_cb;

  std::function<void(double x, double y)> pointer_motion_cb;
  std::function<void(Input::MouseButton button, bool state)> pointer_button_cb;


@@ 40,14 39,10 @@ public:
    quit_cb = cb;
  }

  void set_recreate_swap_chain_vk_cb(std::function<void(SwapChain *sc)> cb) {
  void set_recreate_swap_chain_vk_cb(std::function<void()> cb) {
    recreate_frame_buffers_cb = cb;
  }

  void set_recreate_swap_chain_drm_cb(std::function<void()> cb) {
    recreate_swap_chain_drm_cb = cb;
  }

  void set_pointer_motion_cb(std::function<void(double x, double y)> cb) {
    pointer_motion_cb = cb;
  }


@@ 81,8 76,7 @@ public:
  virtual int init(uint32_t width, uint32_t height, bool fullscreen) = 0;
  virtual void iterate(VkQueue queue, VkSemaphore semaphore) = 0;
  virtual const std::vector<const char*> required_extensions() = 0;
  virtual void init_swap_chain(VkInstance instance, VkPhysicalDevice physical_device,
                               VkDevice device, uint32_t width, uint32_t height) = 0;
  virtual void init_swap_chain(uint32_t width, uint32_t height) = 0;
  virtual void update_window_title(const std::string& title) = 0;
  virtual VkBool32 check_support(VkPhysicalDevice physical_device) = 0;
};

M vitamin-k/window/vikWindowKMS.hpp => vitamin-k/window/vikWindowKMS.hpp +2 -3
@@ 171,11 171,10 @@ public:
    return {};
  }

  void init_swap_chain(VkInstance instance, VkPhysicalDevice physical_device,
                       VkDevice device, uint32_t width, uint32_t height) {
  void init_swap_chain(uint32_t width, uint32_t height) {
    swap_chain.surface_format.format = VK_FORMAT_R8G8B8A8_SRGB;

    swap_chain.init(device, swap_chain.surface_format.format, gbm_dev, fd,
    swap_chain.init(swap_chain.device, swap_chain.surface_format.format, gbm_dev, fd,
             width, height);
    swap_chain.set_mode_and_page_flip(fd, crtc, connector);
  }

M vitamin-k/window/vikWindowKhrDisplay.hpp => vitamin-k/window/vikWindowKhrDisplay.hpp +4 -5
@@ 29,13 29,12 @@ class WindowKhrDisplay  : public Window {
    return { VK_KHR_DISPLAY_EXTENSION_NAME };
  }

  void init_swap_chain(VkInstance instance, VkPhysicalDevice physical_device,
                       VkDevice device, uint32_t width, uint32_t height) {

    swap_chain.set_context(instance, physical_device, device);
  void init_swap_chain(uint32_t width, uint32_t height) {

    uint32_t displayPropertyCount;

    VkPhysicalDevice physical_device = swap_chain.physical_device;

    // Get display property
    vkGetPhysicalDeviceDisplayPropertiesKHR(physical_device, &displayPropertyCount, NULL);
    VkDisplayPropertiesKHR* pDisplayProperties = new VkDisplayPropertiesKHR[displayPropertyCount];


@@ 124,7 123,7 @@ class WindowKhrDisplay  : public Window {
    surfaceInfo.imageExtent.width = width;
    surfaceInfo.imageExtent.height = height;

    VkResult result = vkCreateDisplayPlaneSurfaceKHR(instance, &surfaceInfo, NULL, &swap_chain.surface);
    VkResult result = vkCreateDisplayPlaneSurfaceKHR(swap_chain.instance, &surfaceInfo, NULL, &swap_chain.surface);
    vik_log_f_if(result !=VK_SUCCESS, "Failed to create surface!");

    delete[] pDisplays;

M vitamin-k/window/vikWindowWaylandShell.hpp => vitamin-k/window/vikWindowWaylandShell.hpp +2 -5
@@ 86,11 86,8 @@ public:
    wl_display_dispatch_pending(display);
  }

  void init_swap_chain(VkInstance instance, VkPhysicalDevice physical_device,
                       VkDevice device, uint32_t width, uint32_t height) {
    swap_chain.set_context(instance, physical_device, device);

    VkResult err = create_surface(instance, &swap_chain.surface);
  void init_swap_chain(uint32_t width, uint32_t height) {
    VkResult err = create_surface(swap_chain.instance, &swap_chain.surface);
    vik_log_f_if(err != VK_SUCCESS, "Could not create surface!");

    swap_chain.select_queue_and_format();

M vitamin-k/window/vikWindowWaylandXDG.hpp => vitamin-k/window/vikWindowWaylandXDG.hpp +3 -5
@@ 98,17 98,15 @@ public:
    swap_chain.render(queue, semaphore);
  }

  void init_swap_chain(VkInstance instance, VkPhysicalDevice physical_device,
                       VkDevice device, uint32_t width, uint32_t height) {
    swap_chain.set_context(instance, physical_device, device);
  void init_swap_chain(uint32_t width, uint32_t height) {

    create_surface(instance, &swap_chain.surface);
    create_surface(swap_chain.instance, &swap_chain.surface);

    swap_chain.choose_surface_format();

    swap_chain.recreate_simple(width, height);

    recreate_frame_buffers_cb(&swap_chain);
    recreate_frame_buffers_cb();
  }

  SwapChain* get_swap_chain() {

M vitamin-k/window/vikWindowXCBInput.hpp => vitamin-k/window/vikWindowXCBInput.hpp +2 -6
@@ 114,13 114,9 @@ class WindowXCBInput : public WindowXCB {
    }
  }

  void init_swap_chain(VkInstance instance, VkPhysicalDevice physical_device,
                       VkDevice device, uint32_t width, uint32_t height) {
    swap_chain.set_context(instance, physical_device, device);

    VkResult err = create_surface(instance, &swap_chain.surface);
  void init_swap_chain(uint32_t width, uint32_t height) {
    VkResult err = create_surface(swap_chain.instance, &swap_chain.surface);
    vik_log_f_if(err != VK_SUCCESS, "Could not create surface!");

    swap_chain.select_queue_and_format();
  }


M vitamin-k/window/vikWindowXCBSimple.hpp => vitamin-k/window/vikWindowXCBSimple.hpp +3 -5
@@ 113,10 113,8 @@ public:
    xcb_flush(connection);
  }

  void init_swap_chain(VkInstance instance, VkPhysicalDevice physical_device,
                       VkDevice device, uint32_t width, uint32_t height) {
    swap_chain.set_context(instance, physical_device, device);
    create_surface(instance, &swap_chain.surface);
  void init_swap_chain(uint32_t width, uint32_t height) {
    create_surface(swap_chain.instance, &swap_chain.surface);
    swap_chain.choose_surface_format();
  }



@@ 163,7 161,7 @@ public:
  void handle_expose(const xcb_expose_event_t *event) {
      vik_log_d("XCB_EXPOSE %dx%d", event->width, event->height);
      swap_chain.recreate_simple(event->width, event->height);
      recreate_frame_buffers_cb(&swap_chain);
      recreate_frame_buffers_cb();
      schedule_repaint();
  }