FCT
载入中...
搜索中...
未找到
VK_Pass.cpp
浏览该文件的文档.
1//
2// Created by Administrator on 2025/3/28.
3//
4
5#include "./VK_Pass.h"
6#include "./VK_PassGroup.h"
9namespace FCT
10{
11 namespace RHI
12 {
14 {
15 m_ctx = ctx;
16 m_group = nullptr;
17 }
18
19 void VK_Pass::create(PassGroup* srcGroup)
20 {
21 auto group = static_cast<VK_PassGroup*>(srcGroup);
22 m_desc.pipelineBindPoint = vk::PipelineBindPoint::eGraphics;
23
24 if (!m_renderTargets.empty()) {
25 uint32_t maxSlot = m_renderTargets.rbegin()->first;
26
27 m_renderTargetRefs.resize(maxSlot + 1);
28
29 for (auto& ref : m_renderTargetRefs) {
30 ref.attachment = VK_ATTACHMENT_UNUSED;
31 ref.layout = vk::ImageLayout::eUndefined;
32 }
33
34 for (const auto& [slot, target] : m_renderTargets) {
35 vk::AttachmentReference& ref = m_renderTargetRefs[slot];
36 ref.attachment = m_targetAttachmentIndices[slot];
37 ref.layout = vk::ImageLayout::eColorAttachmentOptimal;
38 }
39
40 m_desc.colorAttachmentCount = maxSlot + 1;
41 m_desc.pColorAttachments = m_renderTargetRefs.data();
42 } else {
43 m_desc.colorAttachmentCount = 0;
44 m_desc.pColorAttachments = nullptr;
45 }
46
47 if (m_depthStencil) {
49 m_depthStencilRef.layout = vk::ImageLayout::eDepthStencilAttachmentOptimal;
50
51 m_desc.pDepthStencilAttachment = &m_depthStencilRef;
52
53 fout << "Pass using depth stencil attachment with index: "
54 << m_depthStencilAttachmentIndex << std::endl;
55 } else {
56 m_desc.pDepthStencilAttachment = nullptr;
57 }
58
59 m_group = group;
60 }
61
62
64 {
65 if (!m_clearValue.types) {
66 return;
67 }
68
69 auto vkCmdBuf = static_cast<VK_CommandBuffer*>(cmdBuf);
70 std::vector<vk::ClearAttachment> clearAttachments;
71
72 if (m_clearValue.types & ClearType::color) {
73 for (const auto& [slot, attachmentIndex] : m_targetAttachmentIndices) {
74 if (slot < m_renderTargetRefs.size() && m_renderTargetRefs[slot].attachment != VK_ATTACHMENT_UNUSED) {
75 vk::ClearAttachment clearAttachment;
76 clearAttachment.aspectMask = vk::ImageAspectFlagBits::eColor;
77 clearAttachment.colorAttachment = slot;
78 clearAttachment.clearValue.color = vk::ClearColorValue(std::array<float, 4>{
79 m_clearValue.color.x,
80 m_clearValue.color.y,
81 m_clearValue.color.z,
82 m_clearValue.color.w
83 });
84 clearAttachments.push_back(clearAttachment);
85 }
86 }
87 }
88
89 if (m_depthStencil && m_depthStencilAttachmentIndex != UINT32_MAX &&
91
92 vk::ClearAttachment clearAttachment;
93 clearAttachment.aspectMask = vk::ImageAspectFlagBits::eNone;
94
95 if (m_clearValue.types & ClearType::depth) {
96 clearAttachment.aspectMask |= vk::ImageAspectFlagBits::eDepth;
97 }
98
99 if (m_clearValue.types & ClearType::stencil) {
100 clearAttachment.aspectMask |= vk::ImageAspectFlagBits::eStencil;
101 }
102
103 clearAttachment.clearValue.depthStencil = vk::ClearDepthStencilValue(
104 m_clearValue.depth,
105 m_clearValue.stencil
106 );
107
108 clearAttachments.push_back(clearAttachment);
109 }
110
111 if (!clearAttachments.empty()) {
112 vk::ClearRect clearRect;
113 clearRect.rect.offset = vk::Offset2D(0, 0);
114
115 uint32_t width = 0, height = 0;
116 if (!m_renderTargets.empty()) {
117 auto& firstTarget = m_renderTargets.begin()->second;
118 width = firstTarget->width();
119 height = firstTarget->height();
120 } else if (m_depthStencil) {
121 width = m_depthStencil->width();
122 height = m_depthStencil->height();
123 }
124
125 clearRect.rect.extent = vk::Extent2D(width, height);
126 clearRect.baseArrayLayer = 0;
127 clearRect.layerCount = 1;
128
129 vkCmdBuf->commandBuffer().clearAttachments(clearAttachments, clearRect);
130 }
131 }
132
134 {
135
136 }
137
139 {
140 if (!m_clearValue.types) {
141 return;
142 }
143
144 auto vkCmdBuf = static_cast<VK_CommandBuffer*>(cmdBuf);
145 std::vector<vk::ClearAttachment> clearAttachments;
146
147 if (m_clearValue.types & ClearType::color) {
148 for (const auto& [slot, attachmentIndex] : m_targetAttachmentIndices) {
149 if (slot < m_renderTargetRefs.size() && m_renderTargetRefs[slot].attachment != VK_ATTACHMENT_UNUSED) {
150 vk::ClearAttachment clearAttachment;
151 clearAttachment.aspectMask = vk::ImageAspectFlagBits::eColor;
152 clearAttachment.colorAttachment = slot;
153 clearAttachment.clearValue.color = vk::ClearColorValue(std::array<float, 4>{
154 m_clearValue.color.x,
155 m_clearValue.color.y,
156 m_clearValue.color.z,
157 m_clearValue.color.w
158 });
159 clearAttachments.push_back(clearAttachment);
160 }
161 }
162 }
163
164 if (m_depthStencil && m_depthStencilAttachmentIndex != UINT32_MAX &&
166
167 vk::ClearAttachment clearAttachment;
168 clearAttachment.aspectMask = vk::ImageAspectFlagBits::eNone;
169
170 if (m_clearValue.types & ClearType::depth) {
171 clearAttachment.aspectMask |= vk::ImageAspectFlagBits::eDepth;
172 }
173
174 if (m_clearValue.types & ClearType::stencil) {
175 clearAttachment.aspectMask |= vk::ImageAspectFlagBits::eStencil;
176 }
177
178 clearAttachment.clearValue.depthStencil = vk::ClearDepthStencilValue(
179 m_clearValue.depth,
180 m_clearValue.stencil
181 );
182
183 clearAttachments.push_back(clearAttachment);
184 }
185
186 if (!clearAttachments.empty()) {
187 vk::ClearRect clearRect;
188 clearRect.rect.offset = vk::Offset2D(0, 0);
189
190 uint32_t width = 0, height = 0;
191 if (!m_renderTargets.empty()) {
192 auto& firstTarget = m_renderTargets.begin()->second;
193 width = firstTarget->width();
194 height = firstTarget->height();
195 } else if (m_depthStencil) {
196 width = m_depthStencil->width();
197 height = m_depthStencil->height();
198 }
199
200 clearRect.rect.extent = vk::Extent2D(width, height);
201 clearRect.baseArrayLayer = 0;
202 clearRect.layerCount = 1;
203
204 vkCmdBuf->commandBuffer().clearAttachments(clearAttachments, clearRect);
205 }
206 }
207 }
208}
PassClearValue m_clearValue
定义 Pass.h:90
std::map< uint32_t, FCT::Image * > m_renderTargets
定义 Pass.h:88
FCT::Image * m_depthStencil
定义 Pass.h:84
PassGroup * m_group
定义 Pass.h:85
PassGroup * group() const
定义 Pass.h:79
void executeClear(CommandBuffer *cmdBuf) override
uint32_t m_depthStencilAttachmentIndex
VK_Pass(VK_Context *ctx)
std::map< uint32_t, uint32_t > m_targetAttachmentIndices
vk::AttachmentReference m_depthStencilRef
vk::SubpassDescription m_desc
std::vector< vk::AttachmentReference > m_renderTargetRefs
VK_Context * m_ctx
void create(PassGroup *group) override
void beginSubmit(CommandBuffer *cmdBuf) override
void endSubmit() override
std::ostream & fout