~sircmpwn/xrgears

f954ec72f7379eea469288968d0eb8e5992fef0e — Lubosz Sarnecki 4 years ago 0644636
vks renderer: move text overlay to separate class.
M vitamin-k/render/vikRendererTextOverlay.hpp => vitamin-k/render/vikRendererTextOverlay.hpp +90 -1
@@ 6,7 6,96 @@ namespace vik {

class RendererTextOverlay : public RendererVks {
public:

  TextOverlay *textOverlay;

  RendererTextOverlay(Settings *s, Window *w) : RendererVks(s, w) {}
  ~RendererTextOverlay() {}
  ~RendererTextOverlay() {
    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_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;
    }

    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);

      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));
    }

    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 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));
    }
};
}

M vitamin-k/render/vikRendererVks.hpp => vitamin-k/render/vikRendererVks.hpp +2 -75
@@ 21,7 21,6 @@ class RendererVks : public Renderer {
public:
  Timer timer;
  Device *vksDevice;
  TextOverlay *textOverlay;

  VkPhysicalDeviceProperties deviceProperties;
  VkPhysicalDeviceFeatures deviceFeatures;


@@ 83,9 82,6 @@ public:
  }

  ~RendererVks() {
    if (settings->enable_text_overlay)
      delete textOverlay;

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


@@ 133,10 129,6 @@ public:
    create_render_pass();
    create_pipeline_cache();
    create_frame_buffers();
    if (settings->enable_text_overlay) {
      init_text_overlay();
      update_text_overlay(title);
    }

    destWidth = width;
    destHeight = height;


@@ 147,23 139,6 @@ public:
    vkDeviceWaitIdle(device);
  }

  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);
  }

  bool check_command_buffers() {
    for (auto& cmdBuffer : drawCmdBuffers)
      if (cmdBuffer == VK_NULL_HANDLE)


@@ 237,11 212,9 @@ public:
    vik_log_check(vkCreatePipelineCache(device, &pipelineCacheCreateInfo, nullptr, &pipelineCache));
  }

  void check_tick_finnished(const std::string& title) {
  virtual 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();
    }
  }


@@ 258,15 231,7 @@ public:
      vik_log_check(err);
  }

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

  virtual void submit_frame() {
    SwapChainVK *sc = (SwapChainVK*) window->get_swap_chain();
    vik_log_check(sc->present(queue, currentBuffer, semaphores.render_complete));
    vik_log_check(vkQueueWaitIdle(queue));


@@ 424,21 389,6 @@ public:
    return windowTitle;
  }

  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);

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

  float get_aspect_ratio() {
    return (float)width / (float)height;
  }


@@ 469,26 419,6 @@ public:
    create_command_buffers();
  }

  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 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.renderComplete;
    submit_info.signalSemaphoreCount = 1;
    submit_info.pSignalSemaphores = &semaphores.textOverlayComplete;
    submit_info.commandBufferCount = 1;
    return submit_info;
  }

  VkSubmitInfo init_render_submit_info() {
    VkSubmitInfo submit_info = initializers::submitInfo();
    submit_info.pWaitDstStageMask = &submitPipelineStages;


@@ 619,7 549,6 @@ public:
  void init_depth_stencil() {
    VkImageCreateInfo image = {};
    image.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO;
    image.pNext = NULL;
    image.imageType = VK_IMAGE_TYPE_2D;
    image.format = depthFormat;
    image.extent = { width, height, 1 };


@@ 632,13 561,11 @@ public:

    VkMemoryAllocateInfo mem_alloc = {};
    mem_alloc.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO;
    mem_alloc.pNext = NULL;
    mem_alloc.allocationSize = 0;
    mem_alloc.memoryTypeIndex = 0;

    VkImageViewCreateInfo depthStencilView = {};
    depthStencilView.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO;
    depthStencilView.pNext = NULL;
    depthStencilView.viewType = VK_IMAGE_VIEW_TYPE_2D;
    depthStencilView.format = depthFormat;
    depthStencilView.flags = 0;

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

public:
  RendererTextOverlay *renderer;
  RendererVks *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 RendererTextOverlay(&settings, window);
    renderer = new RendererVks(&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;


@@ 231,10 231,12 @@ public:

    vkDeviceWaitIdle(renderer->device);

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

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



@@ 243,6 245,5 @@ public:
    view_changed_cb();
  }


};
}