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 "VkFramebuffer.hpp"
16
17 #include "VkImageView.hpp"
18 #include "VkRenderPass.hpp"
19 #include "VkStringify.hpp"
20
21 #include <memory.h>
22 #include <algorithm>
23
24 namespace vk {
25
Framebuffer(const VkFramebufferCreateInfo * pCreateInfo,void * mem)26 Framebuffer::Framebuffer(const VkFramebufferCreateInfo *pCreateInfo, void *mem)
27 : attachments(reinterpret_cast<ImageView **>(mem))
28 , extent{ pCreateInfo->width, pCreateInfo->height }
29 {
30 const VkBaseInStructure *curInfo = reinterpret_cast<const VkBaseInStructure *>(pCreateInfo->pNext);
31 const VkFramebufferAttachmentsCreateInfo *attachmentsCreateInfo = nullptr;
32 while(curInfo)
33 {
34 switch(curInfo->sType)
35 {
36 case VK_STRUCTURE_TYPE_FRAMEBUFFER_ATTACHMENTS_CREATE_INFO:
37 attachmentsCreateInfo = reinterpret_cast<const VkFramebufferAttachmentsCreateInfo *>(curInfo);
38 break;
39 case VK_STRUCTURE_TYPE_MAX_ENUM:
40 // dEQP tests that this value is ignored.
41 break;
42 default:
43 UNSUPPORTED("pFramebufferCreateInfo->pNext->sType = %s", vk::Stringify(curInfo->sType).c_str());
44 break;
45 }
46 curInfo = curInfo->pNext;
47 }
48
49 if(pCreateInfo->flags & VK_FRAMEBUFFER_CREATE_IMAGELESS_BIT)
50 {
51 // If flags includes VK_FRAMEBUFFER_CREATE_IMAGELESS_BIT, the pNext chain **must**
52 // include a VkFramebufferAttachmentsCreateInfo.
53 ASSERT(attachmentsCreateInfo != nullptr);
54 attachmentCount = attachmentsCreateInfo->attachmentImageInfoCount;
55 for(uint32_t i = 0; i < attachmentCount; i++)
56 {
57 attachments[i] = nullptr;
58 }
59 }
60 else
61 {
62 attachmentCount = pCreateInfo->attachmentCount;
63 for(uint32_t i = 0; i < attachmentCount; i++)
64 {
65 attachments[i] = vk::Cast(pCreateInfo->pAttachments[i]);
66 }
67 }
68 }
69
destroy(const VkAllocationCallbacks * pAllocator)70 void Framebuffer::destroy(const VkAllocationCallbacks *pAllocator)
71 {
72 vk::freeHostMemory(attachments, pAllocator);
73 }
74
executeLoadOp(const RenderPass * renderPass,uint32_t clearValueCount,const VkClearValue * pClearValues,const VkRect2D & renderArea)75 void Framebuffer::executeLoadOp(const RenderPass *renderPass, uint32_t clearValueCount, const VkClearValue *pClearValues, const VkRect2D &renderArea)
76 {
77 // This gets called at the start of a renderpass. Logically the `loadOp` gets executed at the
78 // subpass where an attachment is first used, but since we don't discard contents between subpasses,
79 // we can execute it sooner. Only clear operations have an effect.
80
81 ASSERT(attachmentCount == renderPass->getAttachmentCount());
82
83 const uint32_t count = std::min(clearValueCount, attachmentCount);
84 for(uint32_t i = 0; i < count; i++)
85 {
86 const VkAttachmentDescription attachment = renderPass->getAttachment(i);
87 VkImageAspectFlags clearMask = 0;
88
89 switch(attachment.loadOp)
90 {
91 case VK_ATTACHMENT_LOAD_OP_CLEAR:
92 clearMask |= VK_IMAGE_ASPECT_COLOR_BIT | VK_IMAGE_ASPECT_DEPTH_BIT;
93 break;
94 case VK_ATTACHMENT_LOAD_OP_LOAD:
95 case VK_ATTACHMENT_LOAD_OP_DONT_CARE:
96 case VK_ATTACHMENT_LOAD_OP_NONE_EXT:
97 // Don't clear the attachment's color or depth aspect.
98 break;
99 default:
100 UNSUPPORTED("attachment.loadOp %d", attachment.loadOp);
101 }
102
103 switch(attachment.stencilLoadOp)
104 {
105 case VK_ATTACHMENT_LOAD_OP_CLEAR:
106 clearMask |= VK_IMAGE_ASPECT_STENCIL_BIT;
107 break;
108 case VK_ATTACHMENT_LOAD_OP_LOAD:
109 case VK_ATTACHMENT_LOAD_OP_DONT_CARE:
110 case VK_ATTACHMENT_LOAD_OP_NONE_EXT:
111 // Don't clear the attachment's stencil aspect.
112 break;
113 default:
114 UNSUPPORTED("attachment.stencilLoadOp %d", attachment.stencilLoadOp);
115 }
116
117 // Image::clear() demands that we only specify existing aspects.
118 clearMask &= Format(attachment.format).getAspects();
119
120 if(!clearMask || !renderPass->isAttachmentUsed(i))
121 {
122 continue;
123 }
124
125 uint32_t viewMask = renderPass->isMultiView() ? renderPass->getAttachmentViewMask(i) : 0;
126 attachments[i]->clear(pClearValues[i], clearMask, renderArea, viewMask);
127 }
128 }
129
clearAttachment(const RenderPass * renderPass,uint32_t subpassIndex,const VkClearAttachment & attachment,const VkClearRect & rect)130 void Framebuffer::clearAttachment(const RenderPass *renderPass, uint32_t subpassIndex, const VkClearAttachment &attachment, const VkClearRect &rect)
131 {
132 VkSubpassDescription subpass = renderPass->getSubpass(subpassIndex);
133 uint32_t viewMask = renderPass->getViewMask(subpassIndex);
134
135 if(attachment.aspectMask == VK_IMAGE_ASPECT_COLOR_BIT)
136 {
137 ASSERT(attachment.colorAttachment < subpass.colorAttachmentCount);
138 uint32_t attachmentIndex = subpass.pColorAttachments[attachment.colorAttachment].attachment;
139
140 if(attachmentIndex != VK_ATTACHMENT_UNUSED)
141 {
142 ASSERT(attachmentIndex < attachmentCount);
143 ImageView *imageView = attachments[attachmentIndex];
144
145 imageView->clear(attachment.clearValue, attachment.aspectMask, rect, viewMask);
146 }
147 }
148 else if(attachment.aspectMask & (VK_IMAGE_ASPECT_DEPTH_BIT | VK_IMAGE_ASPECT_STENCIL_BIT))
149 {
150 uint32_t attachmentIndex = subpass.pDepthStencilAttachment->attachment;
151
152 if(attachmentIndex != VK_ATTACHMENT_UNUSED)
153 {
154 ASSERT(attachmentIndex < attachmentCount);
155 ImageView *imageView = attachments[attachmentIndex];
156
157 imageView->clear(attachment.clearValue, attachment.aspectMask, rect, viewMask);
158 }
159 }
160 else
161 UNSUPPORTED("attachment.aspectMask %X", attachment.aspectMask);
162 }
163
setAttachment(ImageView * imageView,uint32_t index)164 void Framebuffer::setAttachment(ImageView *imageView, uint32_t index)
165 {
166 ASSERT(index < attachmentCount);
167 attachments[index] = imageView;
168 }
169
getAttachment(uint32_t index) const170 ImageView *Framebuffer::getAttachment(uint32_t index) const
171 {
172 return attachments[index];
173 }
174
resolve(const RenderPass * renderPass,uint32_t subpassIndex)175 void Framebuffer::resolve(const RenderPass *renderPass, uint32_t subpassIndex)
176 {
177 const auto &subpass = renderPass->getSubpass(subpassIndex);
178 uint32_t viewMask = renderPass->getViewMask(subpassIndex);
179
180 if(subpass.pResolveAttachments)
181 {
182 for(uint32_t i = 0; i < subpass.colorAttachmentCount; i++)
183 {
184 uint32_t resolveAttachment = subpass.pResolveAttachments[i].attachment;
185 if(resolveAttachment != VK_ATTACHMENT_UNUSED)
186 {
187 ImageView *imageView = attachments[subpass.pColorAttachments[i].attachment];
188 imageView->resolve(attachments[resolveAttachment], viewMask);
189 }
190 }
191 }
192
193 if(renderPass->hasDepthStencilResolve() && subpass.pDepthStencilAttachment != nullptr)
194 {
195 VkSubpassDescriptionDepthStencilResolve dsResolve = renderPass->getSubpassDepthStencilResolve(subpassIndex);
196 uint32_t depthStencilAttachment = subpass.pDepthStencilAttachment->attachment;
197 if((depthStencilAttachment != VK_ATTACHMENT_UNUSED) && (dsResolve.pDepthStencilResolveAttachment != nullptr))
198 {
199 ImageView *imageView = attachments[depthStencilAttachment];
200 imageView->resolveDepthStencil(attachments[dsResolve.pDepthStencilResolveAttachment->attachment],
201 dsResolve.depthResolveMode, dsResolve.stencilResolveMode);
202 }
203 }
204 }
205
ComputeRequiredAllocationSize(const VkFramebufferCreateInfo * pCreateInfo)206 size_t Framebuffer::ComputeRequiredAllocationSize(const VkFramebufferCreateInfo *pCreateInfo)
207 {
208 const VkBaseInStructure *curInfo = reinterpret_cast<const VkBaseInStructure *>(pCreateInfo->pNext);
209 const VkFramebufferAttachmentsCreateInfo *attachmentsInfo = nullptr;
210 while(curInfo)
211 {
212 switch(curInfo->sType)
213 {
214 case VK_STRUCTURE_TYPE_FRAMEBUFFER_ATTACHMENTS_CREATE_INFO:
215 attachmentsInfo = reinterpret_cast<const VkFramebufferAttachmentsCreateInfo *>(curInfo);
216 break;
217 case VK_STRUCTURE_TYPE_MAX_ENUM:
218 // dEQP tests that this value is ignored.
219 break;
220 default:
221 UNSUPPORTED("pFramebufferCreateInfo->pNext->sType = %s", vk::Stringify(curInfo->sType).c_str());
222 break;
223 }
224
225 curInfo = curInfo->pNext;
226 }
227
228 if(pCreateInfo->flags & VK_FRAMEBUFFER_CREATE_IMAGELESS_BIT)
229 {
230 ASSERT(attachmentsInfo != nullptr);
231 return attachmentsInfo->attachmentImageInfoCount * sizeof(void *);
232 }
233 else
234 {
235 return pCreateInfo->attachmentCount * sizeof(void *);
236 }
237 }
238
239 } // namespace vk
240