(Translated by https://www.hiragana.jp/)
GitHub - sunxfancy/VKBuilder: Vulkan helper utilities - builders for everything
Skip to content

Vulkan helper utilities - builders for everything

Notifications You must be signed in to change notification settings

sunxfancy/VKBuilder

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

VkBuilder 便びんとしてきvulkan对象构造

わが们在使用しようvulkan时,往往おうおうしげる琐的概念がいねん大量たいりょうてきまいりすう设置こま扰,这个库的主要しゅよう目的もくてき创建一套能快速构建Vulkan对象てき构造使用しよう这些builder以快そく创建常用じょうようてき,带有良好りょうこう设计默认参すうてき,带有常用じょうよう配置はいち方案ほうあんてき对象。从而以快そく创建可用かようてき渲染流水りゅうすい线。

わが们对Instance,Deviceとう对象进行りょう包装ほうそうはた其和其他常用じょうよう对象つつめ使つかいとく很多API调用时可以减しょうさんすうてき使用しようゆう其是减少さんすうてき传入错误

设计おもえ

  1. さき提供ていきょう常用じょうようてきさんすうまこと许默认构けん性能せいのう一般但可用的对象,提供ていきょうせっこう对构づくり时进ぎょうとく指定してい
  2. 大量たいりょう使用しようりゅうしきAPI
  3. つう过打つつみ其他对象,つきりょう减少せっこう调用时的さんすう
  4. あかり确哪些对ぞう其他对象てき约束关系,ざいせっ口中くちじゅうたい现这种约たば关系,つきりょう使符合ふごう约束关系てき调用无法编译
  5. 使用しようばん减少だい码量

使用しよう方法ほうほう

使用しようVKBuilder以快そく创建实例,设备,渲染界面かいめん,交换链等とうVulkan对象。

#define VKB_IMPL
#include "vkbuilder.hpp"

class Render {
public:
  void init(void *window) {

    // くびさき创建构造,选择API版本はんぽん需要じゅようてきlayer,配置はいちdebug messenger
    vkb::InstanceBuilder builder;
    inst = builder.require_api_version(1, 0)
                  .request_validation_layers()   // validate correctness
                  .use_default_debug_messenger() // use DebugUtilsMessage
                  .build();

    // 创建surfaceよう于渲しみ
    auto surface = (VkSurfaceKHR)create_surface_glfw(inst.instance, window);

    // 创建いち个PhysicalDeviceSelectorようらい选择渲染设备
    vkb::PhysicalDeviceSelector selector{inst};

    auto phys = selector.set_surface(surface)
            .set_minimum_version(1, 0) // require a vulkan 1.0 capable device
            .select();

    vkb::DeviceBuilder device_builder{phys};

    // 构造设备
    // automatically propagate needed data from instance & physical device
    device = device_builder.build();

    create_swapchain();
    create_pipeline();
  }

  void create_swapchain() {
    // 创建だま认交换链
    vkb::SwapchainBuilder swapchain_builder{device};
    auto swap_ret = swapchain_builder.set_old_swapchain(swapchain).build();
    swapchain.destroy();
    swapchain = swap_ret;
  }

  void create_pipeline() {
    // 创建渲染Pass
    vkb::RenderPassBuilder renderpass_builder{device};
    renderpass = renderpass_builder
            .addPresentAttachment(swapchain.image_format, vk::AttachmentLoadOp::eClear)
            .addSubpass(vkb::SubpassBuilder()
              .addAttachmentRef(0, vk::ImageLayout::eColorAttachmentOptimal))
            .addDependency(VK_SUBPASS_EXTERNAL, 0)
            .build();

    auto vert_code = readFile("vert.spv");
    auto frag_code = readFile("frag.spv");

    // 创建渲染流水りゅうすい线
    vkb::PipelineBuilder pipeline_builder{device, swapchain};
    pipeline = pipeline_builder
        .useClassicPipeline(vert_code, frag_code)
        .setVertexInputState(
          vkb::VertexInputStateBuilder()  // VertexInputStateBuilder 以根すえVertexじょうてき函数かんすう进行创建
            .addInputBinding<Vertex>()
            .addAttributeDescription<Vertex>()
        )
        .build(renderpass);

    vkb::PresentBuilder present_builder{device, swapchain};
    present = present_builder.build(renderpass);

    initVertex();
  }

  vkb::Instance inst;
  vkb::Device device;
  vkb::Swapchain swapchain;

  vk::RenderPass renderpass;
  vk::Pipeline pipeline;

  vkb::Present present;

  std::vector<Vertex> v;
  vkb::HostVertexBuffer buffer;
};

Render render;

void onRender() {
  render.render();
}

void initVulkan(void *window) {
  render.init(window);
}

int main() {
  GLFW_Window window;
  window.createWindow();
  initVulkan(window.getWindow());
  window.mainLoop();
  return 0;
}

对于顶点すうすえ结构,ただ需要じゅよう创建getBindingDescription函数かんすうあずかgetAttributeDescription函数かんすう,就可以将该结构体ちゅうさついたわが们的けい统中らい

  struct Vertex {
    glm::vec2 pos;
    glm::vec3 color;
    
    static vk::VertexInputBindingDescription 
    getBindingDescription(uint32_t binding) {
      vk::VertexInputBindingDescription bindingDescription{};
      bindingDescription.binding = binding;
      bindingDescription.stride = sizeof(Vertex);
      bindingDescription.inputRate = vk::VertexInputRate::eVertex;
      return bindingDescription;
    }
    static std::vector<vk::VertexInputAttributeDescription>
    getAttributeDescription(uint32_t binding) {
      std::vector<vk::VertexInputAttributeDescription> attrs = {
        vk::VertexInputAttributeDescription(0, binding, vk::Format::eR32G32Sfloat, offsetof(Vertex, pos)),
        vk::VertexInputAttributeDescription(1, binding, vk::Format::eR32G32B32Sfloat, offsetof(Vertex, color))
      };
      return attrs;
    }
  };

  void initVertex() {
    v.resize(3);
    v[0].pos = {0, 0.5}; v[0].color = {1,0,0};
    v[1].pos = {-0.5, -0.5}; v[1].color = {0,1,0};
    v[2].pos = {0.5, -0.5}; v[2].color = {0,0,1};

    // create Vertex buffer
    buffer.allocate<Vertex>(device, v);
  }

渲染ひかえせい非常ひじょう轻松:

  void render() {
    present.begin();
    present.beginRenderPass(renderpass);

    vk::DeviceSize offset(0);
    auto& cb = present.getCurrentCommandBuffer();
    cb.bindPipeline(vk::PipelineBindPoint::eGraphics, pipeline);
    cb.bindVertexBuffers(0, 1, buffer, &offset);
    cb.draw(3, 1, 0, 0);

    present.endRenderPass();
    present.end();
    present.drawFrame();
  }

Releases

No releases published

Packages

No packages published

Languages