1 // Copyright 2018 The SwiftShader Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "VkRenderPass.hpp"
16 #include "VkStringify.hpp"
17 #include <cstring>
18
19 namespace {
20
21 template<class T>
ComputeRequiredAllocationSizeT(const T * pCreateInfo)22 size_t ComputeRequiredAllocationSizeT(const T *pCreateInfo)
23 {
24 size_t attachmentSize = pCreateInfo->attachmentCount * sizeof(VkAttachmentDescription) + pCreateInfo->attachmentCount * sizeof(int) // first use
25 + pCreateInfo->attachmentCount * sizeof(uint32_t); // union of subpass view masks, per attachment
26 size_t subpassesSize = 0;
27 for(uint32_t i = 0; i < pCreateInfo->subpassCount; ++i)
28 {
29 const auto &subpass = pCreateInfo->pSubpasses[i];
30 uint32_t nbAttachments = subpass.inputAttachmentCount + subpass.colorAttachmentCount;
31 if(subpass.pResolveAttachments)
32 {
33 nbAttachments += subpass.colorAttachmentCount;
34 }
35 if(subpass.pDepthStencilAttachment)
36 {
37 nbAttachments += 1;
38 }
39 subpassesSize += sizeof(VkSubpassDescription) +
40 sizeof(VkAttachmentReference) * nbAttachments +
41 sizeof(uint32_t) * subpass.preserveAttachmentCount +
42 sizeof(uint32_t); // view mask
43 }
44 size_t dependenciesSize = pCreateInfo->dependencyCount * sizeof(VkSubpassDependency);
45
46 return attachmentSize + subpassesSize + dependenciesSize;
47 }
48
49 template<class T>
CopySubpasses(VkSubpassDescription * dst,const T * src,uint32_t count)50 void CopySubpasses(VkSubpassDescription *dst, const T *src, uint32_t count)
51 {
52 for(uint32_t i = 0; i < count; ++i)
53 {
54 dst[i].flags = src[i].flags;
55 dst[i].pipelineBindPoint = src[i].pipelineBindPoint;
56 dst[i].inputAttachmentCount = src[i].inputAttachmentCount;
57 dst[i].pInputAttachments = nullptr;
58 dst[i].colorAttachmentCount = src[i].colorAttachmentCount;
59 dst[i].pColorAttachments = nullptr;
60 dst[i].pResolveAttachments = nullptr;
61 dst[i].pDepthStencilAttachment = nullptr;
62 dst[i].preserveAttachmentCount = src[i].preserveAttachmentCount;
63 dst[i].pPreserveAttachments = nullptr;
64 }
65 }
66
67 template<class T>
CopyAttachmentDescriptions(VkAttachmentDescription * dst,const T * src,uint32_t count)68 void CopyAttachmentDescriptions(VkAttachmentDescription *dst, const T *src, uint32_t count)
69 {
70 for(uint32_t i = 0; i < count; ++i)
71 {
72 dst[i].flags = src[i].flags;
73 dst[i].format = src[i].format;
74 dst[i].samples = src[i].samples;
75 dst[i].loadOp = src[i].loadOp;
76 dst[i].storeOp = src[i].storeOp;
77 dst[i].stencilLoadOp = src[i].stencilLoadOp;
78 dst[i].stencilStoreOp = src[i].stencilStoreOp;
79 dst[i].initialLayout = src[i].initialLayout;
80 dst[i].finalLayout = src[i].finalLayout;
81 }
82 }
83
84 template<class T>
CopyAttachmentReferences(VkAttachmentReference * dst,const T * src,uint32_t count)85 void CopyAttachmentReferences(VkAttachmentReference *dst, const T *src, uint32_t count)
86 {
87 for(uint32_t i = 0; i < count; ++i)
88 {
89 dst[i].attachment = src[i].attachment;
90 dst[i].layout = src[i].layout;
91 }
92 }
93
94 template<class T>
CopySubpassDependencies(VkSubpassDependency * dst,const T * src,uint32_t count)95 void CopySubpassDependencies(VkSubpassDependency *dst, const T *src, uint32_t count)
96 {
97 for(uint32_t i = 0; i < count; ++i)
98 {
99 dst[i].srcSubpass = src[i].srcSubpass;
100 dst[i].dstSubpass = src[i].dstSubpass;
101 dst[i].srcStageMask = src[i].srcStageMask;
102 dst[i].dstStageMask = src[i].dstStageMask;
103 dst[i].srcAccessMask = src[i].srcAccessMask;
104 dst[i].dstAccessMask = src[i].dstAccessMask;
105 dst[i].dependencyFlags = src[i].dependencyFlags;
106 }
107 }
108
GetViewMasks(const VkRenderPassCreateInfo * pCreateInfo,uint32_t * masks)109 bool GetViewMasks(const VkRenderPassCreateInfo *pCreateInfo, uint32_t *masks)
110 {
111 return false;
112 }
113
GetViewMasks(const VkRenderPassCreateInfo2KHR * pCreateInfo,uint32_t * masks)114 bool GetViewMasks(const VkRenderPassCreateInfo2KHR *pCreateInfo, uint32_t *masks)
115 {
116 for(uint32_t i = 0; i < pCreateInfo->subpassCount; ++i)
117 {
118 masks[i] = pCreateInfo->pSubpasses[i].viewMask;
119 }
120 return true;
121 }
122
123 } // namespace
124
125 namespace vk {
126
RenderPass(const VkRenderPassCreateInfo * pCreateInfo,void * mem)127 RenderPass::RenderPass(const VkRenderPassCreateInfo *pCreateInfo, void *mem)
128 : attachmentCount(pCreateInfo->attachmentCount)
129 , subpassCount(pCreateInfo->subpassCount)
130 , dependencyCount(pCreateInfo->dependencyCount)
131 {
132 init(pCreateInfo, &mem);
133 }
134
RenderPass(const VkRenderPassCreateInfo2KHR * pCreateInfo,void * mem)135 RenderPass::RenderPass(const VkRenderPassCreateInfo2KHR *pCreateInfo, void *mem)
136 : attachmentCount(pCreateInfo->attachmentCount)
137 , subpassCount(pCreateInfo->subpassCount)
138 , dependencyCount(pCreateInfo->dependencyCount)
139 {
140 init(pCreateInfo, &mem);
141 // Note: the init function above ignores:
142 // - pCorrelatedViewMasks: This provides a potential performance optimization
143 // - VkAttachmentReference2::aspectMask : This specifies which aspects may be used
144 // - VkSubpassDependency2::viewOffset : This is the same as VkRenderPassMultiviewCreateInfo::pViewOffsets, which is currently ignored
145 // - Any pNext pointer in VkRenderPassCreateInfo2KHR's internal structures
146
147 char *hostMemory = reinterpret_cast<char *>(mem);
148
149 // Handle the extensions in each subpass
150 for(uint32_t i = 0; i < subpassCount; i++)
151 {
152 auto const &subpass = pCreateInfo->pSubpasses[i];
153 const VkBaseInStructure *extension = reinterpret_cast<const VkBaseInStructure *>(subpass.pNext);
154 while(extension)
155 {
156 switch(extension->sType)
157 {
158 case VK_STRUCTURE_TYPE_SUBPASS_DESCRIPTION_DEPTH_STENCIL_RESOLVE:
159 {
160 const auto *ext = reinterpret_cast<const VkSubpassDescriptionDepthStencilResolve *>(extension);
161 // If any subpass includes depthStencilResolve, allocate a DSR struct for each subpass
162 // This allows us to index into subpassDepthStencilResolves using the subpass index.
163 if(ext->pDepthStencilResolveAttachment != nullptr && ext->pDepthStencilResolveAttachment->attachment != VK_ATTACHMENT_UNUSED)
164 {
165 if(subpassDepthStencilResolves == nullptr)
166 {
167 subpassDepthStencilResolves = reinterpret_cast<VkSubpassDescriptionDepthStencilResolve *>(hostMemory);
168 hostMemory += subpassCount * sizeof(VkSubpassDescriptionDepthStencilResolve);
169 for(uint32_t subpass = 0; subpass < subpassCount; subpass++)
170 {
171 subpassDepthStencilResolves[subpass].sType = VK_STRUCTURE_TYPE_SUBPASS_DESCRIPTION_DEPTH_STENCIL_RESOLVE;
172 subpassDepthStencilResolves[subpass].pNext = nullptr;
173 subpassDepthStencilResolves[subpass].depthResolveMode = VK_RESOLVE_MODE_NONE;
174 subpassDepthStencilResolves[subpass].stencilResolveMode = VK_RESOLVE_MODE_NONE;
175 subpassDepthStencilResolves[subpass].pDepthStencilResolveAttachment = nullptr;
176 }
177 }
178
179 VkAttachmentReference2 *reference = reinterpret_cast<VkAttachmentReference2 *>(hostMemory);
180 hostMemory += sizeof(VkAttachmentReference2);
181
182 subpassDepthStencilResolves[i].depthResolveMode = ext->depthResolveMode;
183 subpassDepthStencilResolves[i].stencilResolveMode = ext->stencilResolveMode;
184 reference->pNext = nullptr;
185 reference->sType = ext->pDepthStencilResolveAttachment->sType;
186 reference->attachment = ext->pDepthStencilResolveAttachment->attachment;
187 reference->layout = ext->pDepthStencilResolveAttachment->layout;
188 reference->aspectMask = ext->pDepthStencilResolveAttachment->aspectMask;
189 subpassDepthStencilResolves[i].pDepthStencilResolveAttachment = reinterpret_cast<const VkAttachmentReference2 *>(reference);
190
191 MarkFirstUse(reference->attachment, i);
192 }
193 }
194 break;
195 default:
196 LOG_TRAP("VkRenderPassCreateInfo2KHR->subpass[%d]->pNext sType: %s",
197 i, vk::Stringify(extension->sType).c_str());
198 break;
199 }
200
201 extension = extension->pNext;
202 }
203 }
204 }
205
206 template<class T>
init(const T * pCreateInfo,void ** mem)207 void RenderPass::init(const T *pCreateInfo, void **mem)
208 {
209 char *hostMemory = reinterpret_cast<char *>(*mem);
210
211 // subpassCount must be greater than 0
212 ASSERT(pCreateInfo->subpassCount > 0);
213
214 size_t subpassesSize = pCreateInfo->subpassCount * sizeof(VkSubpassDescription);
215 subpasses = reinterpret_cast<VkSubpassDescription *>(hostMemory);
216 CopySubpasses(subpasses, pCreateInfo->pSubpasses, pCreateInfo->subpassCount);
217 hostMemory += subpassesSize;
218 uint32_t *masks = reinterpret_cast<uint32_t *>(hostMemory);
219 hostMemory += subpassCount * sizeof(uint32_t);
220
221 if(attachmentCount > 0)
222 {
223 size_t attachmentSize = pCreateInfo->attachmentCount * sizeof(VkAttachmentDescription);
224 attachments = reinterpret_cast<VkAttachmentDescription *>(hostMemory);
225 CopyAttachmentDescriptions(attachments, pCreateInfo->pAttachments, pCreateInfo->attachmentCount);
226 hostMemory += attachmentSize;
227
228 size_t firstUseSize = pCreateInfo->attachmentCount * sizeof(int);
229 attachmentFirstUse = reinterpret_cast<int *>(hostMemory);
230 hostMemory += firstUseSize;
231
232 attachmentViewMasks = reinterpret_cast<uint32_t *>(hostMemory);
233 hostMemory += pCreateInfo->attachmentCount * sizeof(uint32_t);
234 for(auto i = 0u; i < pCreateInfo->attachmentCount; i++)
235 {
236 attachmentFirstUse[i] = -1;
237 attachmentViewMasks[i] = 0;
238 }
239 }
240
241 const VkBaseInStructure *extensionCreateInfo = reinterpret_cast<const VkBaseInStructure *>(pCreateInfo->pNext);
242 while(extensionCreateInfo)
243 {
244 switch(extensionCreateInfo->sType)
245 {
246 case VK_STRUCTURE_TYPE_RENDER_PASS_MULTIVIEW_CREATE_INFO:
247 {
248 // Renderpass uses multiview if this structure is present AND some subpass specifies
249 // a nonzero view mask
250 auto const *multiviewCreateInfo = reinterpret_cast<VkRenderPassMultiviewCreateInfo const *>(extensionCreateInfo);
251 for(auto i = 0u; i < pCreateInfo->subpassCount; i++)
252 {
253 masks[i] = multiviewCreateInfo->pViewMasks[i];
254 // This is now a multiview renderpass, so make the masks available
255 if(masks[i])
256 {
257 viewMasks = masks;
258 }
259 }
260
261 break;
262 }
263 default:
264 WARN("pCreateInfo->pNext sType = %s", vk::Stringify(extensionCreateInfo->sType).c_str());
265 break;
266 }
267
268 extensionCreateInfo = extensionCreateInfo->pNext;
269 }
270
271 if(!viewMasks && (GetViewMasks(pCreateInfo, masks)))
272 {
273 for(auto i = 0u; i < pCreateInfo->subpassCount; i++)
274 {
275 if(masks[i])
276 {
277 viewMasks = masks;
278 }
279 }
280 }
281
282 // Deep copy subpasses
283 for(uint32_t i = 0; i < pCreateInfo->subpassCount; ++i)
284 {
285 const auto &subpass = pCreateInfo->pSubpasses[i];
286
287 if(subpass.inputAttachmentCount > 0)
288 {
289 size_t inputAttachmentsSize = subpass.inputAttachmentCount * sizeof(VkAttachmentReference);
290 subpasses[i].pInputAttachments = reinterpret_cast<VkAttachmentReference *>(hostMemory);
291 CopyAttachmentReferences(const_cast<VkAttachmentReference *>(subpasses[i].pInputAttachments),
292 pCreateInfo->pSubpasses[i].pInputAttachments, subpass.inputAttachmentCount);
293 hostMemory += inputAttachmentsSize;
294
295 for(auto j = 0u; j < subpasses[i].inputAttachmentCount; j++)
296 {
297 if(subpass.pInputAttachments[j].attachment != VK_ATTACHMENT_UNUSED)
298 MarkFirstUse(subpass.pInputAttachments[j].attachment, i);
299 }
300 }
301
302 if(subpass.colorAttachmentCount > 0)
303 {
304 size_t colorAttachmentsSize = subpass.colorAttachmentCount * sizeof(VkAttachmentReference);
305 subpasses[i].pColorAttachments = reinterpret_cast<VkAttachmentReference *>(hostMemory);
306 CopyAttachmentReferences(const_cast<VkAttachmentReference *>(subpasses[i].pColorAttachments),
307 subpass.pColorAttachments, subpass.colorAttachmentCount);
308 hostMemory += colorAttachmentsSize;
309
310 if(subpass.pResolveAttachments)
311 {
312 subpasses[i].pResolveAttachments = reinterpret_cast<VkAttachmentReference *>(hostMemory);
313 CopyAttachmentReferences(const_cast<VkAttachmentReference *>(subpasses[i].pResolveAttachments),
314 subpass.pResolveAttachments, subpass.colorAttachmentCount);
315 hostMemory += colorAttachmentsSize;
316 }
317
318 for(auto j = 0u; j < subpasses[i].colorAttachmentCount; j++)
319 {
320 if(subpass.pColorAttachments[j].attachment != VK_ATTACHMENT_UNUSED)
321 MarkFirstUse(subpass.pColorAttachments[j].attachment, i);
322 if(subpass.pResolveAttachments &&
323 subpass.pResolveAttachments[j].attachment != VK_ATTACHMENT_UNUSED)
324 MarkFirstUse(subpass.pResolveAttachments[j].attachment, i);
325 }
326 }
327
328 if(subpass.pDepthStencilAttachment)
329 {
330 subpasses[i].pDepthStencilAttachment = reinterpret_cast<VkAttachmentReference *>(hostMemory);
331 CopyAttachmentReferences(const_cast<VkAttachmentReference *>(subpasses[i].pDepthStencilAttachment),
332 subpass.pDepthStencilAttachment, 1);
333 hostMemory += sizeof(VkAttachmentReference);
334
335 if(subpass.pDepthStencilAttachment->attachment != VK_ATTACHMENT_UNUSED)
336 MarkFirstUse(subpass.pDepthStencilAttachment->attachment, i);
337 }
338
339 if(subpass.preserveAttachmentCount > 0)
340 {
341 size_t preserveAttachmentSize = subpass.preserveAttachmentCount * sizeof(uint32_t);
342 subpasses[i].pPreserveAttachments = reinterpret_cast<uint32_t *>(hostMemory);
343 for(uint32_t j = 0u; j < subpass.preserveAttachmentCount; j++)
344 {
345 const_cast<uint32_t *>(subpasses[i].pPreserveAttachments)[j] = pCreateInfo->pSubpasses[i].pPreserveAttachments[j];
346 }
347 hostMemory += preserveAttachmentSize;
348
349 for(auto j = 0u; j < subpasses[i].preserveAttachmentCount; j++)
350 {
351 if(subpass.pPreserveAttachments[j] != VK_ATTACHMENT_UNUSED)
352 MarkFirstUse(subpass.pPreserveAttachments[j], i);
353 }
354 }
355 }
356
357 if(pCreateInfo->dependencyCount > 0)
358 {
359 dependencies = reinterpret_cast<VkSubpassDependency *>(hostMemory);
360 CopySubpassDependencies(dependencies, pCreateInfo->pDependencies, pCreateInfo->dependencyCount);
361 hostMemory += dependencyCount * sizeof(VkSubpassDependency);
362 }
363 *mem = hostMemory;
364 }
365
destroy(const VkAllocationCallbacks * pAllocator)366 void RenderPass::destroy(const VkAllocationCallbacks *pAllocator)
367 {
368 vk::deallocate(subpasses, pAllocator); // attachments and dependencies are in the same allocation
369 }
370
ComputeRequiredAllocationSize(const VkRenderPassCreateInfo * pCreateInfo)371 size_t RenderPass::ComputeRequiredAllocationSize(const VkRenderPassCreateInfo *pCreateInfo)
372 {
373 return ComputeRequiredAllocationSizeT(pCreateInfo);
374 }
375
ComputeRequiredAllocationSize(const VkRenderPassCreateInfo2KHR * pCreateInfo)376 size_t RenderPass::ComputeRequiredAllocationSize(const VkRenderPassCreateInfo2KHR *pCreateInfo)
377 {
378 size_t requiredMemory = ComputeRequiredAllocationSizeT(pCreateInfo);
379
380 // Calculate the memory required to handle depth stencil resolves
381 bool usesDSR = false;
382 for(uint32_t i = 0; i < pCreateInfo->subpassCount; i++)
383 {
384 auto const &subpass = pCreateInfo->pSubpasses[i];
385 const VkBaseInStructure *extension = reinterpret_cast<const VkBaseInStructure *>(subpass.pNext);
386 while(extension)
387 {
388 switch(extension->sType)
389 {
390 case VK_STRUCTURE_TYPE_SUBPASS_DESCRIPTION_DEPTH_STENCIL_RESOLVE:
391 {
392 const auto *ext = reinterpret_cast<const VkSubpassDescriptionDepthStencilResolve *>(extension);
393 if(ext->pDepthStencilResolveAttachment != nullptr && ext->pDepthStencilResolveAttachment->attachment != VK_ATTACHMENT_UNUSED)
394 {
395 if(!usesDSR)
396 {
397 // If any subpass uses DSR, then allocate a VkSubpassDescriptionDepthStencilResolve
398 // for all subpasses. This allows us to index into our DSR structs using the subpass index.
399 requiredMemory += sizeof(VkSubpassDescriptionDepthStencilResolve) * pCreateInfo->subpassCount;
400 usesDSR = true;
401 }
402 // For each subpass that actually uses DSR, allocate a VkAttachmentReference2.
403 requiredMemory += sizeof(VkAttachmentReference2);
404 }
405 }
406 break;
407 default:
408 LOG_TRAP("VkRenderPassCreateInfo2KHR->subpass[%d]->pNext sType: %s",
409 i, vk::Stringify(extension->sType).c_str());
410 break;
411 }
412
413 extension = extension->pNext;
414 }
415 }
416
417 return requiredMemory;
418 }
419
getRenderAreaGranularity(VkExtent2D * pGranularity) const420 void RenderPass::getRenderAreaGranularity(VkExtent2D *pGranularity) const
421 {
422 pGranularity->width = 1;
423 pGranularity->height = 1;
424 }
425
MarkFirstUse(int attachment,int subpass)426 void RenderPass::MarkFirstUse(int attachment, int subpass)
427 {
428 // FIXME: we may not actually need to track attachmentFirstUse if we're going to eagerly
429 // clear attachments at the start of the renderpass; can use attachmentViewMasks always instead.
430
431 if(attachmentFirstUse[attachment] == -1)
432 attachmentFirstUse[attachment] = subpass;
433
434 if(isMultiView())
435 attachmentViewMasks[attachment] |= viewMasks[subpass];
436 }
437
438 } // namespace vk
439