~sircmpwn/xrgears

6dac09b18c6d4912be490375b473acfb6e1ceed3 — Lubosz Sarnecki 4 years ago f8ebc96
textoverlay: move semaphore to textoverlay renderer class. auto ident.
M vitamin-k/render/vikRendererTextOverlay.hpp => vitamin-k/render/vikRendererTextOverlay.hpp +91 -70
@@ 9,93 9,114 @@ public:

  TextOverlay *textOverlay;

  VkSemaphore text_overlay_complete;

  RendererTextOverlay(Settings *s, Window *w) : RendererVks(s, w) {}
  ~RendererTextOverlay() {
    vkDestroySemaphore(device, text_overlay_complete, nullptr);
    if (settings->enable_text_overlay)
      delete textOverlay;
  }

    void init(const std::string &name, const std::string &title) {
      RendererVks::init(name, title);
      if (settings->enable_text_overlay) {
        init_text_overlay();
        update_text_overlay(title);
      }
  void init(const std::string &name, const std::string &title) {
    RendererVks::init(name, title);
    if (settings->enable_text_overlay) {
      init_text_overlay();
      update_text_overlay(title);
    }
  }

    void init_text_overlay() {
      // Load the text rendering shaders
      std::vector<VkPipelineShaderStageCreateInfo> shaderStages;
      shaderStages.push_back(Shader::load(device, "base/textoverlay.vert.spv", VK_SHADER_STAGE_VERTEX_BIT));
      shaderStages.push_back(Shader::load(device, "base/textoverlay.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT));

      textOverlay = new TextOverlay(
            vksDevice,
            queue,
            &frame_buffers,
            window->get_swap_chain()->surface_format.format,
            depthFormat,
            &width,
            &height,
            shaderStages);
    }
  void init_text_overlay() {
    // Load the text rendering shaders
    std::vector<VkPipelineShaderStageCreateInfo> shaderStages;
    shaderStages.push_back(Shader::load(device, "base/textoverlay.vert.spv", VK_SHADER_STAGE_VERTEX_BIT));
    shaderStages.push_back(Shader::load(device, "base/textoverlay.frag.spv", VK_SHADER_STAGE_FRAGMENT_BIT));

    textOverlay = new TextOverlay(
          vksDevice,
          queue,
          &frame_buffers,
          window->get_swap_chain()->surface_format.format,
          depthFormat,
          &width,
          &height,
          shaderStages);
  }

    VkSubmitInfo init_text_submit_info() {
      // Wait for color attachment output to finish before rendering the text overlay
      VkPipelineStageFlags stageFlags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;

      VkSubmitInfo submit_info = initializers::submitInfo();
      submit_info.pWaitDstStageMask = &stageFlags;
      submit_info.waitSemaphoreCount = 1;
      submit_info.pWaitSemaphores = &semaphores.render_complete;
      submit_info.signalSemaphoreCount = 1;
      submit_info.pSignalSemaphores = &semaphores.text_overlay_complete;
      submit_info.commandBufferCount = 1;
      return submit_info;
    }
  VkSubmitInfo init_text_submit_info() {
    // Wait for color attachment output to finish before rendering the text overlay
    VkPipelineStageFlags stageFlags = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;

    VkSubmitInfo submit_info = initializers::submitInfo();
    submit_info.pWaitDstStageMask = &stageFlags;
    submit_info.waitSemaphoreCount = 1;
    submit_info.pWaitSemaphores = &semaphores.render_complete;
    submit_info.signalSemaphoreCount = 1;
    submit_info.pSignalSemaphores = &text_overlay_complete;
    submit_info.commandBufferCount = 1;
    return submit_info;
  }

    void update_text_overlay(const std::string& title) {
      if (!settings->enable_text_overlay)
        return;
  void update_text_overlay(const std::string& title) {
    if (!settings->enable_text_overlay)
      return;

      std::stringstream ss;
      ss << std::fixed
         << std::setprecision(3)
         << (timer.frame_time_seconds * 1000.0f)
         << "ms (" << timer.frames_per_second
         << " fps)";
      std::string deviceName(deviceProperties.deviceName);
    std::stringstream ss;
    ss << std::fixed
       << std::setprecision(3)
       << (timer.frame_time_seconds * 1000.0f)
       << "ms (" << timer.frames_per_second
       << " fps)";
    std::string deviceName(deviceProperties.deviceName);

      textOverlay->update(title, ss.str(), deviceName);
    }
    textOverlay->update(title, ss.str(), deviceName);
  }

    VkSubmitInfo submit_text_overlay() {
      VkSubmitInfo submit_info = init_text_submit_info();
      submit_info.pCommandBuffers = &textOverlay->cmdBuffers[currentBuffer];
      vik_log_check(vkQueueSubmit(queue, 1, &submit_info, VK_NULL_HANDLE));
  VkSubmitInfo submit_text_overlay() {
    VkSubmitInfo submit_info = init_text_submit_info();
    submit_info.pCommandBuffers = &textOverlay->cmdBuffers[currentBuffer];
    vik_log_check(vkQueueSubmit(queue, 1, &submit_info, VK_NULL_HANDLE));
  }

  void check_tick_finnished(const std::string& title) {
    if (timer.tick_finnished()) {
      timer.update_fps();
      if (settings->enable_text_overlay)
        update_text_overlay(title);
      timer.reset();
    }
  }

    void check_tick_finnished(const std::string& title) {
      if (timer.tick_finnished()) {
        timer.update_fps();
        if (settings->enable_text_overlay)
          update_text_overlay(title);
        timer.reset();
      }
  void resize() {
    RendererVks::resize();
    if (settings->enable_text_overlay) {
      textOverlay->reallocateCommandBuffers();
      std::string title = "Foo.";
      update_text_overlay(title);
    }
  }

    void submit_frame() {
      VkSemaphore waitSemaphore;
      if (settings->enable_text_overlay && textOverlay->visible) {
        submit_text_overlay();
        waitSemaphore = semaphores.text_overlay_complete;
      } else {
        waitSemaphore = semaphores.render_complete;
      }

      SwapChainVK *sc = (SwapChainVK*) window->get_swap_chain();
      vik_log_check(sc->present(queue, currentBuffer, waitSemaphore));
      vik_log_check(vkQueueWaitIdle(queue));
  void submit_frame() {
    VkSemaphore waitSemaphore;
    if (settings->enable_text_overlay && textOverlay->visible) {
      submit_text_overlay();
      waitSemaphore = text_overlay_complete;
    } else {
      waitSemaphore = semaphores.render_complete;
    }

    SwapChainVK *sc = (SwapChainVK*) window->get_swap_chain();
    vik_log_check(sc->present(queue, currentBuffer, waitSemaphore));
    vik_log_check(vkQueueWaitIdle(queue));
  }

  void init_semaphores() {
    RendererVks::init_semaphores();
     VkSemaphoreCreateInfo semaphore_info = initializers::semaphoreCreateInfo();
    // Create a semaphore used to synchronize command submission
    // Ensures that the image is not presented until all commands for the text overlay have been sumbitted and executed
    // Will be inserted after the render complete semaphore if the text overlay is enabled
    vik_log_check(vkCreateSemaphore(device, &semaphore_info, nullptr, &text_overlay_complete));
  }
};
}

M vitamin-k/render/vikRendererVks.hpp => vitamin-k/render/vikRendererVks.hpp +3 -9
@@ 43,7 43,6 @@ public:
  struct {
    VkSemaphore present_complete;
    VkSemaphore render_complete;
    VkSemaphore text_overlay_complete;
  } semaphores;

  std::vector<VkCommandBuffer> drawCmdBuffers;


@@ 100,7 99,6 @@ public:

    vkDestroySemaphore(device, semaphores.present_complete, nullptr);
    vkDestroySemaphore(device, semaphores.render_complete, nullptr);
    vkDestroySemaphore(device, semaphores.text_overlay_complete, nullptr);

    delete vksDevice;



@@ 111,7 109,7 @@ public:
  }

  void init(const std::string &name, const std::string &title) {
    init_vulkan(name, window->required_extensions());
    init_vulkan(title, window->required_extensions());
    window->init(width, height);

    window->update_window_title(make_title_string(title));


@@ 361,7 359,7 @@ public:

  }

  void init_semaphores() {
  virtual void init_semaphores() {
    // Create synchronization objects
    VkSemaphoreCreateInfo semaphoreCreateInfo = initializers::semaphoreCreateInfo();
    // Create a semaphore used to synchronize image presentation


@@ 370,10 368,6 @@ public:
    // Create a semaphore used to synchronize command submission
    // Ensures that the image is not presented until all commands have been sumbitted and executed
    vik_log_check(vkCreateSemaphore(device, &semaphoreCreateInfo, nullptr, &semaphores.render_complete));
    // Create a semaphore used to synchronize command submission
    // Ensures that the image is not presented until all commands for the text overlay have been sumbitted and executed
    // Will be inserted after the render complete semaphore if the text overlay is enabled
    vik_log_check(vkCreateSemaphore(device, &semaphoreCreateInfo, nullptr, &semaphores.text_overlay_complete));
  }

  void create_command_pool() {


@@ 396,7 390,7 @@ public:
    return (float)width / (float)height;
  }

  void resize() {
  virtual void resize() {
    // Ensure all operations on the device have been finished before destroying resources
    vkDeviceWaitIdle(device);


M vitamin-k/system/vikApplicationVks.hpp => vitamin-k/system/vikApplicationVks.hpp +7 -12
@@ 39,7 39,7 @@ class Window;
class ApplicationVks : public Application {

public:
  RendererVks *renderer;
  RendererTextOverlay *renderer;
  CameraBase camera;

  bool viewUpdated = false;


@@ 63,7 63,7 @@ public:

  ApplicationVks(int argc, char *argv[]) : Application(argc, argv) {
    init_window_from_settings();
    renderer = new RendererVks(&settings, window);
    renderer = new RendererTextOverlay(&settings, window);

    std::function<void()> set_window_resize_cb = [this]() { resize(); };
    renderer->set_window_resize_cb(set_window_resize_cb);


@@ 145,8 145,8 @@ public:
            renderer->timer.toggle_animation_pause();
          break;
        case Input::Key::F1:
          //if (state && settings.enable_text_overlay)
          //  renderer->textOverlay->visible = !renderer->textOverlay->visible;
          if (state && settings.enable_text_overlay)
            renderer->textOverlay->visible = !renderer->textOverlay->visible;
          break;
        case Input::Key::ESCAPE:
          quit = true;


@@ 206,8 206,7 @@ public:

      window->iterate(nullptr, nullptr);

      //if (prepared)
        render();
      render();
      renderer->timer.increment();
      float frame_time = renderer->timer.update_frame_time();
      update_camera(frame_time);


@@ 227,16 226,12 @@ public:
  void resize() {
    renderer->resize();


    build_command_buffers();

    vkDeviceWaitIdle(renderer->device);

    /*
    if (settings.enable_text_overlay) {
      renderer->textOverlay->reallocateCommandBuffers();
      renderer->update_text_overlay(title);
    }
    */


    camera.update_aspect_ratio(renderer->get_aspect_ratio());