-
Notifications
You must be signed in to change notification settings - Fork 1
/
vkr_render.h
72 lines (64 loc) · 2.18 KB
/
vkr_render.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#pragma once
#include "vkr_util.h"
class SubPass {
public:
std::vector<VkAttachmentReference> attachments_;
VkSubpassDescription subpass_;
SubPass(const std::vector<VkAttachmentReference>& attachments) : attachments_(attachments) {
subpass_.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
subpass_.colorAttachmentCount = attachments_.size();
subpass_.pColorAttachments = attachments_.data();
}
};
class FrameBuffer {
VkFramebuffer buf_;
public:
FrameBuffer(VkFramebuffer buf) : buf_(buf) {}
};
class RenderPass {
VkDevice device_;
VkRenderPass render_pass_;
public:
RenderPass(VkDevice device, VkRenderPass render_pass) : device_(device), render_pass_(render_pass) {
}
~RenderPass() {
vkDestroyRenderPass(device_, render_pass_, nullptr);
}
static std::unique_ptr<RenderPass> Create(VkDevice device, VkFormat format, const std::vector<SubPass>& subpasses) {
VkAttachmentDescription ca{};
ca.format = format;
ca.samples = VK_SAMPLE_COUNT_1_BIT;
ca.loadOp = VK_ATTACHMENT_LOAD_OP_CLEAR;
ca.storeOp = VK_ATTACHMENT_STORE_OP_STORE;
ca.stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
ca.stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
ca.initialLayout = VK_IMAGE_LAYOUT_UNDEFINED;
ca.finalLayout = VK_IMAGE_LAYOUT_PRESENT_SRC_KHR;
VkRenderPassCreateInfo rpi{};
rpi.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
rpi.attachmentCount = 1;
rpi.pAttachments = &ca;
rpi.subpassCount = subpasses.size();
std::vector<VkSubpassDescription> descs;
for (auto& subpass : subpasses) {
descs.push_back(subpass.subpass_);
}
rpi.pSubpasses = descs.data();
VkRenderPass ret;
vkCreateRenderPass(device, &rpi, nullptr, &ret);
return std::make_unique<RenderPass>(device, ret);
}
std::unique_ptr<FrameBuffer> CreateFrameBuffer(VkImageView swap_chain_view, VkExtent2D extent) {
VkFramebufferCreateInfo info{};
info.sType = VK_STRUCTURE_TYPE_FRAMEBUFFER_CREATE_INFO;
info.renderPass = render_pass_;
info.width = extent.width;
info.height = extent.height;
info.layers = 1;
info.attachmentCount = 1;
info.pAttachments = &swap_chain_view;
VkFramebuffer fb{};
auto res = vkCreateFramebuffer(device_, &info, nullptr, &fb);
return std::make_unique<FrameBuffer>(fb);
}
};