~sircmpwn/xrgears

7ce24477633267ab0603699838c246c64271d075 — Lubosz Sarnecki 4 years ago 7aa16a6
move more to common class.
M examples/vkcube/main.cpp => examples/vkcube/main.cpp +1 -1
@@ 448,7 448,7 @@ public:
  }

  void init() {
    renderer->init_render_pass(window->swap_chain->image_format);
    renderer->init_render_pass(window->swap_chain->surface_format.format);

    VkDescriptorSetLayout set_layout = init_descriptor_set_layout();
    init_pipeline_layout(set_layout);

M vitamin-k/vikSwapChain.hpp => vitamin-k/vikSwapChain.hpp +1 -1
@@ 18,7 18,7 @@ public:
  std::vector<SwapChainBuffer> buffers;

  uint32_t image_count = 0;
  VkFormat image_format;
  VkSurfaceFormatKHR surface_format;

  SwapChain() {}
  ~SwapChain() {}

M vitamin-k/vikSwapChainVK.hpp => vitamin-k/vikSwapChainVK.hpp +50 -3
@@ 75,13 75,13 @@ public:
    vkGetPhysicalDeviceSurfaceFormatsKHR(physical_device, surface,
                                         &num_formats, formats);

    image_format = VK_FORMAT_UNDEFINED;
    surface_format.format = VK_FORMAT_UNDEFINED;
    for (int i = 0; i < num_formats; i++) {
      switch (formats[i].format) {
        case VK_FORMAT_R8G8B8A8_SRGB:
        case VK_FORMAT_B8G8R8A8_SRGB:
          /* These formats are all fine */
          image_format = formats[i].format;
          surface_format.format = formats[i].format;
          break;
        case VK_FORMAT_R8G8B8_SRGB:
        case VK_FORMAT_B8G8R8_SRGB:


@@ 93,7 93,7 @@ public:
      }
    }

    assert(image_format != VK_FORMAT_UNDEFINED);
    assert(surface_format.format != VK_FORMAT_UNDEFINED);
  }

  /**


@@ 110,5 110,52 @@ public:
    device = d;
  }

  void update_images() {
    vkGetSwapchainImagesKHR(device, swap_chain, &image_count, NULL);
    assert(image_count > 0);
    vik_log_d("Creating swap chain with %d images.", image_count);
    VkImage images[image_count];
    vkGetSwapchainImagesKHR(device, swap_chain, &image_count, images);

    buffers.resize(image_count);

    for (uint32_t i = 0; i < image_count; i++) {
      buffers[i].image = images[i];
      create_image_view(device, buffers[i].image,
                        surface_format.format, &buffers[i].view);
    }
  }

  VkPresentModeKHR select_present_mode() {
    // Select a present mode for the swapchain

    // The VK_PRESENT_MODE_FIFO_KHR mode must always be present as per spec
    // This mode waits for the vertical blank ("v-sync")


    // If v-sync is not requested, try to find a mailbox mode
    // It's the lowest latency non-tearing present mode available

    // Get available present modes
    uint32_t count;
    vkGetPhysicalDeviceSurfacePresentModesKHR(physical_device, surface,
                                              &count, NULL);
    assert(count > 0);

    VkPresentModeKHR present_modes[count];
    vkGetPhysicalDeviceSurfacePresentModesKHR(physical_device, surface,
                                              &count, present_modes);

    VkPresentModeKHR mode = VK_PRESENT_MODE_FIFO_KHR;

    for (size_t i = 0; i < count; i++) {
      if (present_modes[i] == VK_PRESENT_MODE_MAILBOX_KHR)
        return VK_PRESENT_MODE_MAILBOX_KHR;
      else if (present_modes[i] == VK_PRESENT_MODE_IMMEDIATE_KHR)
        return VK_PRESENT_MODE_IMMEDIATE_KHR;
    }
    return mode;
  }

};
}

M vkc/vkcSwapChainVK.hpp => vkc/vkcSwapChainVK.hpp +1 -33
@@ 16,24 16,6 @@ public:
  ~SwapChainVK() {
  }

  VkPresentModeKHR select_present_mode() {
    uint32_t count;
    vkGetPhysicalDeviceSurfacePresentModesKHR(physical_device, surface,
                                              &count, NULL);
    VkPresentModeKHR present_modes[count];
    vkGetPhysicalDeviceSurfacePresentModesKHR(physical_device, surface,
                                              &count, present_modes);
    int i;
    VkPresentModeKHR present_mode = VK_PRESENT_MODE_MAILBOX_KHR;
    for (i = 0; i < count; i++) {
      if (present_modes[i] == VK_PRESENT_MODE_FIFO_KHR) {
        present_mode = VK_PRESENT_MODE_FIFO_KHR;
        break;
      }
    }
    return present_mode;
  }

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


@@ 49,7 31,7 @@ public:
    swapchainfo.sType = VK_STRUCTURE_TYPE_SWAPCHAIN_CREATE_INFO_KHR;
    swapchainfo.surface = surface;
    swapchainfo.minImageCount = 2;
    swapchainfo.imageFormat = image_format;
    swapchainfo.imageFormat = surface_format.format;
    swapchainfo.imageColorSpace = VK_COLORSPACE_SRGB_NONLINEAR_KHR;
    swapchainfo.imageExtent = { width, height };
    swapchainfo.imageArrayLayers = 1;


@@ 70,21 52,7 @@ public:
    }
  }

  void update_images() {
    vkGetSwapchainImagesKHR(device, swap_chain, &image_count, NULL);
    assert(image_count > 0);
    vik_log_d("Creating swap chain with %d images.", image_count);
    VkImage swap_chain_images[image_count];
    vkGetSwapchainImagesKHR(device, swap_chain, &image_count, swap_chain_images);

    buffers.resize(image_count);

    for (uint32_t i = 0; i < image_count; i++) {
      buffers[i].image = swap_chain_images[i];
      create_image_view(device, buffers[i].image,
                        image_format, &buffers[i].view);
    }
  }

};
}

M vkc/vkcWindowKMS.hpp => vkc/vkcWindowKMS.hpp +2 -2
@@ 172,9 172,9 @@ public:

    SwapChainDRM *sc = (SwapChainDRM*) swap_chain;

    sc->image_format = VK_FORMAT_R8G8B8A8_SRGB;
    sc->surface_format.format = VK_FORMAT_R8G8B8A8_SRGB;

    sc->init(r->device, sc->image_format, gbm_dev, fd,
    sc->init(r->device, sc->surface_format.format, gbm_dev, fd,
             r->width, r->height, r->render_pass);
    r->create_frame_buffers(swap_chain);
    sc->set_mode_and_page_flip(fd, crtc, connector);

M vks/vksSwapChain.hpp => vks/vksSwapChain.hpp +2 -50
@@ 30,8 30,6 @@ namespace vks {

class SwapChain : public vik::SwapChainVK {
 public:
  VkSurfaceFormatKHR surface_format;

  // Index of the deteced graphics and presenting device queue
  /** @brief Queue family index of the detected graphics and presenting device queue */
  uint32_t queueNodeIndex = UINT32_MAX;


@@ 152,37 150,6 @@ class SwapChain : public vik::SwapChainVK {
    return swapchainExtent;
  }

  VkPresentModeKHR select_present_mode(bool vsync) {
    // Select a present mode for the swapchain

    // The VK_PRESENT_MODE_FIFO_KHR mode must always be present as per spec
    // This mode waits for the vertical blank ("v-sync")
    VkPresentModeKHR swapchainPresentMode = VK_PRESENT_MODE_FIFO_KHR;

    // If v-sync is not requested, try to find a mailbox mode
    // It's the lowest latency non-tearing present mode available
    if (vsync)
      return swapchainPresentMode;

    // Get available present modes
    uint32_t presentModeCount;
    vik_log_check(vkGetPhysicalDeviceSurfacePresentModesKHR(physical_device, surface, &presentModeCount, NULL));
    assert(presentModeCount > 0);

    std::vector<VkPresentModeKHR> presentModes(presentModeCount);
    vik_log_check(vkGetPhysicalDeviceSurfacePresentModesKHR(physical_device, surface, &presentModeCount, presentModes.data()));

    for (size_t i = 0; i < presentModes.size(); i++) {
      if (presentModes[i] == VK_PRESENT_MODE_MAILBOX_KHR) {
        swapchainPresentMode = VK_PRESENT_MODE_MAILBOX_KHR;
        break;
      }
      if ((swapchainPresentMode != VK_PRESENT_MODE_MAILBOX_KHR) && (presentModes[i] == VK_PRESENT_MODE_IMMEDIATE_KHR))
        swapchainPresentMode = VK_PRESENT_MODE_IMMEDIATE_KHR;
    }
    return swapchainPresentMode;
  }

  // Determine the number of swapchain images
  uint32_t select_image_count(const VkSurfaceCapabilitiesKHR &surfCaps) {
    uint32_t count = surfCaps.minImageCount + 1;


@@ 229,21 196,6 @@ class SwapChain : public vik::SwapChainVK {
    vkDestroySwapchainKHR(device, sc, nullptr);
  }

  void update_swap_chain_images() {
    vik_log_check(vkGetSwapchainImagesKHR(device, swap_chain, &image_count, NULL));

    // Get the swap chain images
    VkImage images[image_count];
    vik_log_check(vkGetSwapchainImagesKHR(device, swap_chain, &image_count, images));

    // Get the swap chain buffers containing the image and imageview
    buffers.resize(image_count);
    for (uint32_t i = 0; i < image_count; i++) {
      buffers[i].image = images[i];
      create_image_view(device, images[i], surface_format.format, &buffers[i].view);
    }
  }

  /**
  * Create the swapchain and get it's images with given width and height
  *


@@ 278,7 230,7 @@ class SwapChain : public vik::SwapChainVK {

    swap_chain_info.minImageCount = select_image_count(surfCaps);
    swap_chain_info.preTransform = select_transform_flags(surfCaps);
    swap_chain_info.presentMode = select_present_mode(vsync);
    swap_chain_info.presentMode = 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


@@ 292,7 244,7 @@ class SwapChain : public vik::SwapChainVK {
    if (oldSwapchain != VK_NULL_HANDLE)
      destroy_swap_chain(oldSwapchain);

    update_swap_chain_images();
    update_images();
  }

  /**