~sircmpwn/xrgears

a0e00271bd1e82858a8a2736437d1ddc16184817 — Lubosz Sarnecki 4 years ago 42d3ad7
use same interface for swapchains in renderer.
M vitamin-k/render/vikRendererVks.hpp => vitamin-k/render/vikRendererVks.hpp +3 -6
@@ 85,8 85,7 @@ public:
    if (settings->enable_text_overlay)
      delete textOverlay;

    SwapChainVkComplex *sc = (SwapChainVkComplex*) window->get_swap_chain();
    sc->cleanup();
    window->get_swap_chain()->cleanup();
    if (descriptorPool != VK_NULL_HANDLE)
      vkDestroyDescriptorPool(device, descriptorPool, nullptr);



@@ 420,8 419,7 @@ public:
  void create_command_pool() {
    VkCommandPoolCreateInfo cmdPoolInfo = {};
    cmdPoolInfo.sType = VK_STRUCTURE_TYPE_COMMAND_POOL_CREATE_INFO;
    SwapChainVkComplex *sc = (SwapChainVkComplex*) window->get_swap_chain();
    cmdPoolInfo.queueFamilyIndex = sc->queueNodeIndex;
    cmdPoolInfo.queueFamilyIndex = window->get_swap_chain()->get_queue_index();
    cmdPoolInfo.flags = VK_COMMAND_POOL_CREATE_RESET_COMMAND_BUFFER_BIT;
    vik_log_check(vkCreateCommandPool(device, &cmdPoolInfo, nullptr, &cmdPool));
  }


@@ 461,8 459,7 @@ public:
    width = destWidth;
    height = destHeight;

    SwapChainVkComplex *sc = (SwapChainVkComplex*) window->get_swap_chain();
    sc->create(&width, &height, settings->vsync);
    window->get_swap_chain()->create(width, height);
    // Recreate the frame buffers

    vkDestroyImageView(device, depthStencil.view, nullptr);

M vitamin-k/render/vikSwapChain.hpp => vitamin-k/render/vikSwapChain.hpp +15 -0
@@ 6,6 6,7 @@
#include <functional>

#include "system/vikLog.hpp"
#include "system/vikSettings.hpp"

namespace vik {



@@ 24,11 25,25 @@ public:
  uint32_t image_count = 0;
  VkSurfaceFormatKHR surface_format;

  Settings *settings;

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

  SwapChain() {}
  ~SwapChain() {}

  virtual void cleanup() = 0;

  virtual uint32_t get_queue_index() {
    return 0;
  }

  virtual void create(uint32_t width, uint32_t height) = 0;

  void set_settings(Settings *s) {
    settings = s;
  }

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

M vitamin-k/render/vikSwapChainDRM.hpp => vitamin-k/render/vikSwapChainDRM.hpp +7 -0
@@ 51,6 51,13 @@ public:
  ~SwapChainDRM() {
  }

  void cleanup() {

  }

  void create(uint32_t width, uint32_t height) {
  }

  void init(VkDevice device, VkFormat image_format, gbm_device *gbm_dev, int fd,
            uint32_t width, uint32_t height) {
    PFN_vkCreateDmaBufImageINTEL create_dma_buf_image =

M vitamin-k/render/vikSwapChainVK.hpp => vitamin-k/render/vikSwapChainVK.hpp +20 -3
@@ 15,7 15,7 @@ public:
  SwapChainVK() {}
  ~SwapChainVK() {}

  void create_simple(uint32_t width, uint32_t height) {
  void create(uint32_t width, uint32_t height) {
    VkSurfaceCapabilitiesKHR surface_caps;
    vkGetPhysicalDeviceSurfaceCapabilitiesKHR(physical_device, surface,
                                              &surface_caps);


@@ 44,9 44,9 @@ public:
    vkCreateSwapchainKHR(device, &swapchainfo, NULL, &swap_chain);
  }

  void recreate_simple(uint32_t width, uint32_t height) {
  void recreate(uint32_t width, uint32_t height) {
    destroy();
    create_simple(width, height);
    create(width, height);
    update_images();
  }



@@ 202,5 202,22 @@ public:
    }
  }

  /**
  * Destroy and free Vulkan resources used for the swapchain
  */
  void cleanup() {
    if (swap_chain != VK_NULL_HANDLE) {
      for (uint32_t i = 0; i < image_count; i++)
        vkDestroyImageView(device, buffers[i].view, nullptr);
    }

    if (surface != VK_NULL_HANDLE) {
      vkDestroySwapchainKHR(device, swap_chain, nullptr);
      vkDestroySurfaceKHR(instance, surface, nullptr);
    }
    surface = VK_NULL_HANDLE;
    swap_chain = VK_NULL_HANDLE;
  }

};
}

M vitamin-k/render/vikSwapChainVKComplex.hpp => vitamin-k/render/vikSwapChainVKComplex.hpp +15 -23
@@ 34,6 34,15 @@ class SwapChainVkComplex : public vik::SwapChainVK {
  /** @brief Queue family index of the detected graphics and presenting device queue */
  uint32_t queueNodeIndex = UINT32_MAX;

  std::function<void(uint32_t width, uint32_t height)> dimension_cb;
  void set_dimension_cb(std::function<void(uint32_t width, uint32_t height)> cb) {
    dimension_cb = cb;
  }

  uint32_t get_queue_index() {
    return queueNodeIndex;
  }

  uint32_t select_queue() {
    // Get available queue family properties
    uint32_t queueCount;


@@ 133,19 142,18 @@ class SwapChainVkComplex : public vik::SwapChainVK {
    select_format();
  }

  VkExtent2D select_extent(const VkSurfaceCapabilitiesKHR &surfCaps, uint32_t *width, uint32_t *height) {
  VkExtent2D select_extent(const VkSurfaceCapabilitiesKHR &surfCaps, uint32_t width, uint32_t height) {
    VkExtent2D swapchainExtent = {};
    // If width (and height) equals the special value 0xFFFFFFFF, the size of the surface will be set by the swapchain
    if (surfCaps.currentExtent.width == (uint32_t)-1) {
      // If the surface size is undefined, the size is set to
      // the size of the images requested.
      swapchainExtent.width = *width;
      swapchainExtent.height = *height;
      swapchainExtent.width = width;
      swapchainExtent.height = height;
    } else {
      // If the surface size is defined, the swap chain size must match
      swapchainExtent = surfCaps.currentExtent;
      *width = surfCaps.currentExtent.width;
      *height = surfCaps.currentExtent.height;
      dimension_cb(surfCaps.currentExtent.width, surfCaps.currentExtent.height);
    }
    return swapchainExtent;
  }


@@ 199,7 207,7 @@ class SwapChainVkComplex : public vik::SwapChainVK {
  * @param height Pointer to the height of the swapchain (may be adjusted to fit the requirements of the swapchain)
  * @param vsync (Optional) Can be used to force vsync'd rendering (by using VK_PRESENT_MODE_FIFO_KHR as presentation mode)
  */
  void create(uint32_t *width, uint32_t *height, bool vsync = false) {
  void create(uint32_t width, uint32_t height) {
    // Get physical device surface properties and formats
    VkSurfaceCapabilitiesKHR surfCaps;
    vik_log_check(vkGetPhysicalDeviceSurfaceCapabilitiesKHR(physical_device, surface, &surfCaps));


@@ 226,7 234,7 @@ class SwapChainVkComplex : public vik::SwapChainVK {

    swap_chain_info.minImageCount = select_image_count(surfCaps);
    swap_chain_info.preTransform = select_transform_flags(surfCaps);
    swap_chain_info.presentMode = vsync ? VK_PRESENT_MODE_FIFO_KHR : select_present_mode();
    swap_chain_info.presentMode = settings->vsync ? VK_PRESENT_MODE_FIFO_KHR : select_present_mode();
    swap_chain_info.compositeAlpha = select_composite_alpha(surfCaps);

    // Set additional usage flag for blitting from the swapchain images if supported


@@ 243,21 251,5 @@ class SwapChainVkComplex : public vik::SwapChainVK {
    update_images();
  }

  /**
  * Destroy and free Vulkan resources used for the swapchain
  */
  void cleanup() {
    if (swap_chain != VK_NULL_HANDLE) {
      for (uint32_t i = 0; i < image_count; i++)
        vkDestroyImageView(device, buffers[i].view, nullptr);
    }

    if (surface != VK_NULL_HANDLE) {
      vkDestroySwapchainKHR(device, swap_chain, nullptr);
      vkDestroySurfaceKHR(instance, surface, nullptr);
    }
    surface = VK_NULL_HANDLE;
    swap_chain = VK_NULL_HANDLE;
  }
};
}

M vitamin-k/window/vikWindowWaylandShell.hpp => vitamin-k/window/vikWindowWaylandShell.hpp +3 -1
@@ 89,8 89,10 @@ public:
  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.set_dimension_cb(dimension_cb);
    swap_chain.select_queue_and_format();
    swap_chain.create(&width, &height, settings->vsync);
    swap_chain.set_settings(settings);
    swap_chain.create(width, height);
  }

  SwapChain* get_swap_chain() {

M vitamin-k/window/vikWindowWaylandXDG.hpp => vitamin-k/window/vikWindowWaylandXDG.hpp +1 -1
@@ 104,7 104,7 @@ public:

    swap_chain.choose_surface_format();

    swap_chain.recreate_simple(width, height);
    swap_chain.recreate(width, height);

    recreate_frame_buffers_cb();
  }

M vitamin-k/window/vikWindowXCBInput.hpp => vitamin-k/window/vikWindowXCBInput.hpp +3 -1
@@ 117,8 117,10 @@ class WindowXCBInput : public WindowXCB {
  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.set_dimension_cb(dimension_cb);
    swap_chain.select_queue_and_format();
    swap_chain.create(&width, &height, settings->vsync);
    swap_chain.set_settings(settings);
    swap_chain.create(width, height);
  }

  SwapChain* get_swap_chain() {

M vitamin-k/window/vikWindowXCBSimple.hpp => vitamin-k/window/vikWindowXCBSimple.hpp +1 -1
@@ 160,7 160,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);
      swap_chain.recreate(event->width, event->height);
      recreate_frame_buffers_cb();
      schedule_repaint();
  }