~sircmpwn/xrgears

fb9d492b99b8bb61f3c60bd3c976bccb800df57d — Lubosz Sarnecki 4 years ago 990165d
move surface and format to swapchain.
M vitamin-k/vikSwapChain.hpp => vitamin-k/vikSwapChain.hpp +1 -0
@@ 45,5 45,6 @@ public:

    vik_log_check(vkCreateImageView(device, &view_create_info, nullptr, view));
  }

};
}

M vitamin-k/vikSwapChainVK.hpp => vitamin-k/vikSwapChainVK.hpp +36 -0
@@ 11,6 11,9 @@ public:
  /** @brief Handle to the current swap chain, required for recreation */
  VkSwapchainKHR swap_chain = VK_NULL_HANDLE;

  VkSurfaceKHR surface;
  VkFormat image_format;

  SwapChainVK() {}
  ~SwapChainVK() {}



@@ 55,5 58,38 @@ public:
    return vkQueuePresentKHR(queue, &presentInfo);
  }

  void choose_surface_format(VkPhysicalDevice physical_device) {
    uint32_t num_formats = 0;

    vkGetPhysicalDeviceSurfaceFormatsKHR(physical_device, surface,
                                         &num_formats, NULL);
    assert(num_formats > 0);

    VkSurfaceFormatKHR formats[num_formats];

    vkGetPhysicalDeviceSurfaceFormatsKHR(physical_device, surface,
                                         &num_formats, formats);

    image_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;
          break;
        case VK_FORMAT_R8G8B8_SRGB:
        case VK_FORMAT_B8G8R8_SRGB:
        case VK_FORMAT_R5G6B5_UNORM_PACK16:
        case VK_FORMAT_B5G6R5_UNORM_PACK16:
          /* We would like to support these but they don't seem to work. */
        default:
          continue;
      }
    }

    assert(image_format != VK_FORMAT_UNDEFINED);
  }

};
}

M vkc/vkcRenderer.cpp => vkc/vkcRenderer.cpp +2 -44
@@ 82,53 82,11 @@ void Renderer::init_vk(const char *extension) {
  vkGetDeviceQueue(device, 0, 0, &queue);
}

void Renderer::create_vulkan_swapchain() {
  vik_log_d("Creating vk swapchain");
  swap_chain = new SwapChainVK();
  SwapChainVK* sc = (SwapChainVK*) swap_chain;
  sc->create(device, physical_device, surface, image_format, width, height);
  sc->update_images(device, image_format);
  create_frame_buffers();
}

VkFormat Renderer::choose_surface_format() {
  uint32_t num_formats = 0;
  vkGetPhysicalDeviceSurfaceFormatsKHR(physical_device, surface,
                                       &num_formats, NULL);
  assert(num_formats > 0);

  VkSurfaceFormatKHR formats[num_formats];

  vkGetPhysicalDeviceSurfaceFormatsKHR(physical_device, surface,
                                       &num_formats, formats);

  VkFormat 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 */
        format = formats[i].format;
        break;
      case VK_FORMAT_R8G8B8_SRGB:
      case VK_FORMAT_B8G8R8_SRGB:
      case VK_FORMAT_R5G6B5_UNORM_PACK16:
      case VK_FORMAT_B5G6R5_UNORM_PACK16:
        /* We would like to support these but they don't seem to work. */
      default:
        continue;
    }
  }

  assert(format != VK_FORMAT_UNDEFINED);

  return format;
}

void Renderer::init_render_pass() {

  std::array<VkAttachmentDescription, 1> attachments = {};
  attachments[0].format = image_format;
  SwapChainVK *sc = (SwapChainVK*) swap_chain;
  attachments[0].format = sc->image_format;
  attachments[0].samples = (VkSampleCountFlagBits) 1;
  attachments[0].loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
  attachments[0].storeOp = VK_ATTACHMENT_STORE_OP_STORE;

M vkc/vkcRenderer.hpp => vkc/vkcRenderer.hpp +0 -5
@@ 20,8 20,6 @@ public:
  VkCommandPool cmd_pool;

  timeval start_tv;
  VkSurfaceKHR surface;
  VkFormat image_format;

  VkCommandBuffer cmd_buffer;



@@ 31,13 29,10 @@ public:
  ~Renderer() ;

  void init_vk(const char *extension);
  VkFormat choose_surface_format();
  void init_render_pass();
  void init_vk_objects();
  void submit_queue();

  void create_vulkan_swapchain();

  void wait_and_reset_fences();

  uint64_t get_animation_time();

M vkc/vkcSwapChainVK.hpp => vkc/vkcSwapChainVK.hpp +3 -6
@@ 16,8 16,7 @@ public:
  ~SwapChainVK() {
  }

  VkPresentModeKHR select_present_mode(VkPhysicalDevice physical_device,
                                       VkSurfaceKHR surface) {
  VkPresentModeKHR select_present_mode(VkPhysicalDevice physical_device) {
    uint32_t count;
    vkGetPhysicalDeviceSurfacePresentModesKHR(physical_device, surface,
                                              &count, NULL);


@@ 36,7 35,6 @@ public:
  }

  void create(VkDevice device, VkPhysicalDevice physical_device,
              VkSurfaceKHR surface, VkFormat image_format,
              uint32_t width, uint32_t height) {
    VkSurfaceCapabilitiesKHR surface_caps;
    vkGetPhysicalDeviceSurfaceCapabilitiesKHR(physical_device, surface,


@@ 61,8 59,7 @@ public:
    swapchainfo.queueFamilyIndexCount = 1;
    swapchainfo.preTransform = VK_SURFACE_TRANSFORM_IDENTITY_BIT_KHR;
    swapchainfo.compositeAlpha = VK_COMPOSITE_ALPHA_OPAQUE_BIT_KHR;
    swapchainfo.presentMode = select_present_mode(physical_device,
                                                  surface);
    swapchainfo.presentMode = select_present_mode(physical_device);

    vkCreateSwapchainKHR(device, &swapchainfo, NULL, &swap_chain);
  }


@@ 74,7 71,7 @@ public:
    }
  }

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

M vkc/vkcWindowKMS.hpp => vkc/vkcWindowKMS.hpp +1 -2
@@ 165,14 165,13 @@ public:
    gbm_dev = gbm_create_device(fd);

    r->init_vk(NULL);
    r->image_format = VK_FORMAT_R8G8B8A8_SRGB;

    r->swap_chain = new SwapChainDRM();

    init_cb();

    SwapChainDRM *sc = (SwapChainDRM*) r->swap_chain;
    sc->init(r->device, r->image_format, gbm_dev, fd,
    sc->init(r->device, VK_FORMAT_R8G8B8A8_SRGB, gbm_dev, fd,
             r->width, r->height, r->render_pass);
    r->create_frame_buffers();
    sc->set_mode_and_page_flip(fd, crtc, connector);

M vkc/vkcWindowWayland.hpp => vkc/vkcWindowWayland.hpp +11 -3
@@ 283,11 283,19 @@ public:
    waylandSurfaceInfo.display = display;
    waylandSurfaceInfo.surface = surface;

    vkCreateWaylandSurfaceKHR(r->instance, &waylandSurfaceInfo, NULL, &r->surface);
    r->swap_chain = new SwapChainVK();
    SwapChainVK *sc = (SwapChainVK*) r->swap_chain;
    vik_log_f_if(sc == NULL, "no swapchain!");
    vkCreateWaylandSurfaceKHR(r->instance, &waylandSurfaceInfo, NULL, &sc->surface);

    r->image_format = r->choose_surface_format();
    sc->choose_surface_format(r->physical_device);
    init_cb();
    r->create_vulkan_swapchain();


    sc->create(r->device, r->physical_device, r->width, r->height);
    sc->update_images(r->device);
    r->create_frame_buffers();


    return 0;
  }

M vkc/vkcWindowXCB.hpp => vkc/vkcWindowXCB.hpp +15 -4
@@ 119,10 119,14 @@ public:
    surfaceInfo.sType = VK_STRUCTURE_TYPE_XCB_SURFACE_CREATE_INFO_KHR;
    surfaceInfo.connection = conn;
    surfaceInfo.window = window;
    if (r->swap_chain == nullptr)
      r->swap_chain = new SwapChainVK();

    vkCreateXcbSurfaceKHR(r->instance, &surfaceInfo, NULL, &r->surface);
    SwapChainVK *sc = (SwapChainVK*) r->swap_chain;

    r->image_format = r->choose_surface_format();
    vkCreateXcbSurfaceKHR(r->instance, &surfaceInfo, NULL, &sc->surface);

    sc->choose_surface_format(r->physical_device);
  }

  void schedule_repaint() {


@@ 163,7 167,7 @@ public:
          if (r->width != configure->width ||
              r->height != configure->height) {

                vik_log_d("XCB_CONFIGURE_NOTIFY %dx%d", r->width, r->height);
            vik_log_d("XCB_CONFIGURE_NOTIFY %dx%d", r->width, r->height);

            SwapChainVK *sc = (SwapChainVK*) r->swap_chain;



@@ 176,8 180,15 @@ public:
          break;
        case XCB_EXPOSE:
          vik_log_d("XCB_EXPOSE");
          //if (r->swap_chain == nullptr) {
        {
          if (r->swap_chain == nullptr)
            r->create_vulkan_swapchain(); // if needed
            r->swap_chain = new SwapChainVK();
          SwapChainVK *sc = (SwapChainVK*) r->swap_chain;
          sc->create(r->device, r->physical_device, r->width, r->height);
          sc->update_images(r->device);
          r->create_frame_buffers();
        }
          schedule_repaint();
          break;
        case XCB_KEY_PRESS: