Debian Codegraph @

src/gpu/graphite/vk/VulkanRenderPass.cpp

raw
1 /*
2 * Copyright 2023 Google LLC
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "src/gpu/graphite/vk/VulkanRenderPass.h"
9
10 #include "include/gpu/graphite/vk/VulkanGraphiteTypes.h"
11 #include "src/gpu/graphite/RenderPassDesc.h"
12 #include "src/gpu/graphite/Texture.h"
13 #include "src/gpu/graphite/vk/VulkanCommandBuffer.h"
14 #include "src/gpu/graphite/vk/VulkanGraphiteUtils.h"
15 #include "src/gpu/graphite/vk/VulkanSharedContext.h"
16 #include "src/gpu/graphite/vk/VulkanTexture.h"
17
18 #include <limits>
19
20 namespace skgpu::graphite {
21
22 static constexpr uint32_t kColorFormatOffset = 0;
23 static constexpr uint32_t kDepthStencilFormatOffset = kColorFormatOffset + 8;
24 static constexpr uint32_t kSampleCountOffset = kDepthStencilFormatOffset + 8;
25 static constexpr uint32_t kColorLoadOpOffset = kSampleCountOffset + kNumSampleKeyBits;
26 static constexpr uint32_t kColorStoreOpOffset = kColorLoadOpOffset + 2;
27 static constexpr uint32_t kResolveLoadOpOffset = kColorStoreOpOffset + 1;
28 static constexpr uint32_t kResolveStoreOpOffset = kResolveLoadOpOffset + 2;
29 static constexpr uint32_t kMSRTTOffset = kResolveStoreOpOffset + 1;
30
31 static constexpr uint32_t kFormatMask = 0xFF;
32 static constexpr uint32_t kSampleCountMask = 0x7;
33 static constexpr uint32_t kLoadOpMask = 0x3;
34 static constexpr uint32_t kStoreOpMask = 0x1;
35 static constexpr uint32_t kMSRTTMask = 0x1;
36
37 uint32_t VulkanRenderPass::GetRenderPassKey(const RenderPassDesc& originalRenderPassDesc,
38 bool compatibleForPipelineKey) {
39 const RenderPassDesc renderPassDesc =
40 compatibleForPipelineKey ? MakePipelineCompatibleRenderPass(originalRenderPassDesc)
41 : originalRenderPassDesc;
42 const AttachmentDesc color = renderPassDesc.fColorAttachment;
43 const AttachmentDesc resolve = renderPassDesc.fColorResolveAttachment;
44 const AttachmentDesc depthStencil = renderPassDesc.fDepthStencilAttachment;
45
46 const bool hasResolveAttachment = resolve.fFormat != TextureFormat::kUnsupported;
47 const bool isMultisampledRenderToSingleSampled =
48 RenderPassDescWillImplicitlyLoadMSAA(renderPassDesc);
49 // Silence warning about unused variable in release builds.
50 (void)hasResolveAttachment;
51
52 // Current assumptions in the render pass desc, allowing us to create a smaller key:
53 SkASSERT(color.fFormat != TextureFormat::kUnsupported);
54 SkASSERT(!hasResolveAttachment || resolve.fFormat == color.fFormat);
55 SkASSERT(depthStencil.fFormat == TextureFormat::kUnsupported ||
56 depthStencil.fSampleCount == renderPassDesc.fSampleCount);
57 SkASSERT(depthStencil.fFormat == TextureFormat::kUnsupported ||
58 depthStencil.fLoadOp == LoadOp::kClear);
59 SkASSERT(depthStencil.fFormat == TextureFormat::kUnsupported ||
60 depthStencil.fStoreOp == StoreOp::kDiscard);
61 SkASSERT(!hasResolveAttachment || resolve.fSampleCount == SampleCount::k1);
62 SkASSERT(color.fSampleCount == renderPassDesc.fSampleCount ||
63 isMultisampledRenderToSingleSampled);
64 SkASSERT(!hasResolveAttachment || color.fLoadOp == LoadOp::kDiscard ||
65 color.fLoadOp == LoadOp::kClear);
66 SkASSERT(!hasResolveAttachment || resolve.fLoadOp == LoadOp::kDiscard ||
67 resolve.fLoadOp == LoadOp::kLoad);
68 SkASSERT(!hasResolveAttachment || color.fStoreOp == StoreOp::kDiscard);
69
70 // The following information uniquely defines the render pass:
71 //
72 // Color format (CF): TextureFormat, fits in 6 bits
73 // Depth/stencil format (DSF): TextureFormat, fits in 6 bits
74 // Sample count (M): SampleToKey(count), fits in 3 bits
75 // Color load op (L): LoadOp, fits in 2 bits
76 // Color store op (S): StoreOp, fits in 1 bit
77 // Whether multisampled data s loaded from resolve attachment: fits in 1 bit
78 // Whether rendering multisampled->single-sampled (MSRTSS): fits in 1 bit
79 //
80 // Note that technically, renderable color formats can fit in 5 bits and depth/stencil formats
81 // in 3 bits if more packing is needed.
82 //
83 // Depth/stencil load op is always kClear, and store op is always kDiscard.
84 // Color load op is either found in fColorAttachment if no resolve attachment, or can be derived
85 // from a combination of the load ops specified in the color and resolve attachments:
86 // * If color attachment is kClear, load op is kClear
87 // * Otherwise, if resolve attachment is kLoad, load op is kLoad
88 // * Otherwise, it's kDiscard
89 // Color store op is either found in fColorAttachment if no resolve attachment, or it's found in
90 // fColorResolveAttachment.
91 //
92 // There are currently lots of free bits, so with regards to load/store ops, we don't try too
93 // hard to pack things. Including the color and resolve's load and store ops obviates the need
94 // to store a "load MSAA from resolve" bit. The format of the key is thus:
95 //
96 // LSB MSB
97 // +----+-----+---+---+---+------------+------------+--------+---+
98 // | CF | DSF | M | L | S | L(resolve) | S(resolve) | MSRTSS | 0 |
99 // +----+-----+---+---+---+------------+------------+--------+---+
100 // bits 8 8 3 2 1 2 1 1 6
101 //
102 static_assert(static_cast<uint32_t>(TextureFormat::kLast) < (1 << 8));
103 static_assert(static_cast<uint32_t>(LoadOp::kLast) < (1 << 2));
104 static_assert(static_cast<uint32_t>(StoreOp::kLast) < (1 << 1));
105
106 const uint32_t key =
107 (static_cast<uint32_t>(color.fFormat) << kColorFormatOffset) |
108 (static_cast<uint32_t>(depthStencil.fFormat) << kDepthStencilFormatOffset) |
109 (SamplesToKey(renderPassDesc.fSampleCount) << kSampleCountOffset) |
110 (static_cast<uint32_t>(color.fLoadOp) << kColorLoadOpOffset) |
111 (static_cast<uint32_t>(color.fStoreOp) << kColorStoreOpOffset) |
112 (static_cast<uint32_t>(resolve.fLoadOp) << kResolveLoadOpOffset) |
113 (static_cast<uint32_t>(resolve.fStoreOp) << kResolveStoreOpOffset) |
114 (static_cast<uint32_t>(isMultisampledRenderToSingleSampled) << kMSRTTOffset);
115
116 return key;
117 }
118
119 void VulkanRenderPass::ExtractRenderPassDesc(uint32_t key,
120 Swizzle writeSwizzle,
121 DstReadStrategy dstReadStrategy,
122 RenderPassDesc* renderPassDesc) {
123 // See comment in GetRenderPassKey() describing the format of the key.
124 const TextureFormat colorFormat =
125 SkTo<TextureFormat>((key >> kColorFormatOffset) & kFormatMask);
126 const TextureFormat depthStencilFormat =
127 SkTo<TextureFormat>((key >> kDepthStencilFormatOffset) & kFormatMask);
128 const SampleCount sampleCount = KeyToSamples((key >> kSampleCountOffset) & kSampleCountMask);
129 const LoadOp colorLoadOp = SkTo<LoadOp>((key >> kColorLoadOpOffset) & kLoadOpMask);
130 const StoreOp colorStoreOp = SkTo<StoreOp>((key >> kColorStoreOpOffset) & kStoreOpMask);
131 const LoadOp resolveLoadOp = SkTo<LoadOp>((key >> kResolveLoadOpOffset) & kLoadOpMask);
132 const StoreOp resolveStoreOp = SkTo<StoreOp>((key >> kResolveStoreOpOffset) & kStoreOpMask);
133 const bool isMultisampledRenderToSingleSampled = SkTo<bool>((key >> kMSRTTOffset) & kMSRTTMask);
134
135 // Relationship between render pass sample count, attachment's sample counts and resolve
136 // attachments:
137 //
138 // | RP Samples == 1 | RP Samples > 1 && MSRTSS | RP Samples > 1 && !MSRTSS |
139 // +-----------------+--------------------------+---------------------------+
140 // Has Resolve? | No | No | Yes |
141 // +-----------------+--------------------------+---------------------------+
142 // Resolve Format | Unsupported | Unsupported | Color Format |
143 // +-----------------+--------------------------+---------------------------+
144 // Color Samples | RP Samples (1) | 1 | RP Samples |
145 // +-----------------+--------------------------+---------------------------+
146 // D/S Samples | RP Samples (1) | 1 | RP Samples |
147 // +-----------------+--------------------------+---------------------------+
148 //
149 // The color and resolve attachment's load/store op are already stored in the key. For
150 // depth/stencil, load op is always Clear and store op is always Discard.
151 const SampleCount attachmentSamples = isMultisampledRenderToSingleSampled ? SampleCount::k1
152 : sampleCount;
153
154 *renderPassDesc = {};
155 renderPassDesc->fColorAttachment = {colorFormat, colorLoadOp, colorStoreOp, attachmentSamples};
156 renderPassDesc->fDepthStencilAttachment = {
157 depthStencilFormat, LoadOp::kClear, StoreOp::kDiscard, sampleCount};
158 if (attachmentSamples > SampleCount::k1 && !isMultisampledRenderToSingleSampled) {
159 renderPassDesc->fColorResolveAttachment = {colorFormat,
160 resolveLoadOp,
161 resolveStoreOp,
162 SampleCount::k1};
163 }
164 renderPassDesc->fSampleCount = sampleCount;
165 renderPassDesc->fWriteSwizzle = writeSwizzle;
166 renderPassDesc->fDstReadStrategy = dstReadStrategy;
167 }
168
169 namespace {
170
171 template <typename AttachmentDescription>
172 void setup_vk_attachment_description(AttachmentDescription* outAttachment,
173 const AttachmentDesc& desc,
174 const AttachmentDescription& defaultAttachmentDescription,
175 bool isColor) {
176 static_assert((int) LoadOp::kLoad == (int) VK_ATTACHMENT_LOAD_OP_LOAD);
177 static_assert((int) LoadOp::kClear == (int) VK_ATTACHMENT_LOAD_OP_CLEAR);
178 static_assert((int) LoadOp::kDiscard == (int) VK_ATTACHMENT_LOAD_OP_DONT_CARE);
179 static_assert((int) StoreOp::kStore == (int) VK_ATTACHMENT_STORE_OP_STORE);
180 static_assert((int) StoreOp::kDiscard == (int) VK_ATTACHMENT_STORE_OP_DONT_CARE);
181
182 VkAttachmentLoadOp vkLoadOp = static_cast<VkAttachmentLoadOp>(desc.fLoadOp);
183 VkAttachmentStoreOp vkStoreOp = static_cast<VkAttachmentStoreOp>(desc.fStoreOp);
184
185 *outAttachment = defaultAttachmentDescription;
186 outAttachment->format = TextureFormatToVkFormat(desc.fFormat);
187 outAttachment->samples = SampleCountToVkSampleCount(desc.fSampleCount);
188
189 outAttachment->loadOp = vkLoadOp;
190 outAttachment->storeOp = vkStoreOp;
191 if (isColor) {
192 outAttachment->initialLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
193 outAttachment->finalLayout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
194 outAttachment->stencilLoadOp = VK_ATTACHMENT_LOAD_OP_DONT_CARE;
195 outAttachment->stencilStoreOp = VK_ATTACHMENT_STORE_OP_DONT_CARE;
196 } else {
197 outAttachment->initialLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
198 outAttachment->finalLayout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
199 outAttachment->stencilLoadOp = vkLoadOp;
200 outAttachment->stencilStoreOp = vkStoreOp;
201 }
202 }
203
204 template <typename AttachmentDescription, typename AttachmentReference>
205 void populate_attachment_refs(const RenderPassDesc& renderPassDesc,
206 bool needLoadMSAAFromResolveSubpass,
207 skia_private::TArray<AttachmentDescription>& descs,
208 AttachmentReference& colorRef,
209 AttachmentReference& resolveRef,
210 AttachmentReference& resolveLoadInputRef,
211 AttachmentReference& depthStencilRef,
212 const AttachmentDescription& defaultAttachmentDescription) {
213 // Note: See assumptions regarding RenderPassDesc structure in AddToKey().
214 const AttachmentDesc color = renderPassDesc.fColorAttachment;
215 const AttachmentDesc resolve = renderPassDesc.fColorResolveAttachment;
216 const AttachmentDesc depthStencil = renderPassDesc.fDepthStencilAttachment;
217 SkASSERT(color.fFormat != TextureFormat::kUnsupported);
218 // If MSAA data needs to be loaded from the resolve attachment, there must be a resolve
219 // attachment!
220 SkASSERT(!needLoadMSAAFromResolveSubpass || resolve.fFormat != TextureFormat::kUnsupported);
221
222 // If reading from the dst as an input attachment, we must use VK_IMAGE_LAYOUT_GENERAL
223 // for the color attachment description. Use a general image layout for all renderpasses to
224 // support this.
225 //
226 // As long as the initial/final layouts are VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL, the
227 // choice of the VK_IMAGE_LAYOUT_GENERAL for the subpass layout is efficient; the few GPUs
228 // that treat VK_IMAGE_LAYOUT_GENERAL differently recognize this pattern and keep the
229 // internal layout optimal.
230 colorRef.layout = VK_IMAGE_LAYOUT_GENERAL;
231 colorRef.attachment = descs.size();
232 AttachmentDescription& vkColorAttachDesc = descs.push_back();
233 setup_vk_attachment_description(&vkColorAttachDesc,
234 color,
235 defaultAttachmentDescription,
236 /*isColor=*/true);
237
238 if (resolve.fFormat != TextureFormat::kUnsupported) {
239 resolveRef.attachment = descs.size();
240 resolveRef.layout = VK_IMAGE_LAYOUT_COLOR_ATTACHMENT_OPTIMAL;
241 if (needLoadMSAAFromResolveSubpass) {
242 resolveLoadInputRef.attachment = resolveRef.attachment;
243 // The resolve attachment is used as input attachment in the first subpass. In that
244 // subpass, it is only read from so the layout could have been read-only. To avoid
245 // special-casing this input attachment though, the VK_IMAGE_LAYOUT_GENERAL layout
246 // is used like with the color attachment. This is no less efficient than the
247 // read-only layout when specified on the attachment ref.
248 resolveLoadInputRef.layout = VK_IMAGE_LAYOUT_GENERAL;
249 } else {
250 resolveLoadInputRef.attachment = VK_ATTACHMENT_UNUSED;
251 }
252
253 AttachmentDescription& vkResolveAttachDesc = descs.push_back();
254 setup_vk_attachment_description(&vkResolveAttachDesc,
255 resolve,
256 defaultAttachmentDescription,
257 /*isColor=*/true);
258 } else {
259 resolveRef.attachment = resolveLoadInputRef.attachment = VK_ATTACHMENT_UNUSED;
260 }
261
262 if (depthStencil.fFormat != TextureFormat::kUnsupported) {
263 depthStencilRef.attachment = descs.size();
264 depthStencilRef.layout = VK_IMAGE_LAYOUT_DEPTH_STENCIL_ATTACHMENT_OPTIMAL;
265
266 AttachmentDescription& vkDepthStencilAttachDesc = descs.push_back();
267 setup_vk_attachment_description(&vkDepthStencilAttachDesc,
268 depthStencil,
269 defaultAttachmentDescription,
270 /*isColor=*/false);
271 } else {
272 depthStencilRef.attachment = VK_ATTACHMENT_UNUSED;
273 }
274 }
275
276 template <typename SubpassDependency, typename SubpassDependencyArray>
277 void populate_subpass_dependencies(const VulkanSharedContext* context,
278 SubpassDependencyArray& deps,
279 bool needLoadMSAAFromResolveSubpass,
280 const SubpassDependency& defaultSubpassDependency) {
281 const int mainSubpassIdx = needLoadMSAAFromResolveSubpass ? 1 : 0;
282
283 // Adding a single subpass self-dependency for color attachments is basically free, so apply
284 // one to every RenderPass which has an input attachment on the main subpass. This is useful
285 // because it means that as we perform draw calls, if we encounter a draw that uses a blend
286 // operation requiring a dst read, we can avoid having to switch RenderPasses.
287
288 const bool hasRasterizationOrderColorAttachmentAccess =
289 context->vulkanCaps().supportsRasterizationOrderColorAttachmentAccess();
290 const bool hasNonCoherentAdvancedBlend = context->vulkanCaps().blendEquationSupport() ==
291 Caps::BlendEquationSupport::kAdvancedNoncoherent;
292
293 if (!hasRasterizationOrderColorAttachmentAccess || hasNonCoherentAdvancedBlend) {
294 SubpassDependency& selfDependency = deps.push_back();
295 selfDependency = defaultSubpassDependency;
296 selfDependency.srcSubpass = mainSubpassIdx;
297 selfDependency.dstSubpass = mainSubpassIdx;
298 selfDependency.dependencyFlags = VK_DEPENDENCY_BY_REGION_BIT;
299 selfDependency.srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
300 selfDependency.dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
301 selfDependency.srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
302 selfDependency.dstAccessMask = 0;
303
304 if (!hasRasterizationOrderColorAttachmentAccess) {
305 selfDependency.dstStageMask |= VK_PIPELINE_STAGE_FRAGMENT_SHADER_BIT;
306 selfDependency.dstAccessMask |= VK_ACCESS_INPUT_ATTACHMENT_READ_BIT;
307 }
308 if (hasNonCoherentAdvancedBlend) {
309 selfDependency.dstAccessMask |= VK_ACCESS_COLOR_ATTACHMENT_READ_NONCOHERENT_BIT_EXT;
310 }
311 }
312
313 // If loading MSAA from resolve, enforce that subpass goes first with a subpass dependency.
314 if (needLoadMSAAFromResolveSubpass) {
315 SubpassDependency& dependency = deps.push_back();
316 dependency = defaultSubpassDependency;
317 dependency.srcSubpass = 0;
318 dependency.dstSubpass = mainSubpassIdx;
319 dependency.dependencyFlags = VK_DEPENDENCY_BY_REGION_BIT;
320 dependency.srcStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
321 dependency.srcAccessMask = VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
322 dependency.dstStageMask = VK_PIPELINE_STAGE_COLOR_ATTACHMENT_OUTPUT_BIT;
323 dependency.dstAccessMask =
324 VK_ACCESS_COLOR_ATTACHMENT_READ_BIT | VK_ACCESS_COLOR_ATTACHMENT_WRITE_BIT;
325 }
326 }
327
328 template <typename SubpassDescription, typename AttachmentReference>
329 void populate_main_subpass_desc(const VulkanCaps& caps,
330 SubpassDescription& mainSubpassDesc,
331 const AttachmentReference& colorRef,
332 const AttachmentReference& resolveRef,
333 const AttachmentReference& depthStencilRef) {
334 mainSubpassDesc.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
335 // In the main subpass, the input attachment is the color attachment. We could have excluded
336 // this attachment if unused, but always assigning it to be the color attachment even when
337 // unused allows for compatible-only renderpasses to be shared for pipelines that do read from
338 // the dst and those that do not.
339 mainSubpassDesc.inputAttachmentCount = 1;
340 mainSubpassDesc.pInputAttachments = &colorRef;
341 mainSubpassDesc.colorAttachmentCount = 1;
342 mainSubpassDesc.pColorAttachments = &colorRef;
343 mainSubpassDesc.pResolveAttachments = &resolveRef;
344 mainSubpassDesc.pDepthStencilAttachment = &depthStencilRef;
345 }
346
347 void populate_subpass_descs(const VulkanCaps& caps,
348 skia_private::TArray<VkSubpassDescription>& descs,
349 const VkAttachmentReference& colorRef,
350 const VkAttachmentReference& resolveRef,
351 const VkAttachmentReference& resolveLoadInputRef,
352 const VkAttachmentReference& depthStencilRef) {
353 // If loading MSAA from resolve, add the additional subpass to do so.
354 if (resolveLoadInputRef.attachment != VK_ATTACHMENT_UNUSED) {
355 VkSubpassDescription& loadSubpassDesc = descs.push_back();
356 loadSubpassDesc = {};
357 loadSubpassDesc.pipelineBindPoint = VK_PIPELINE_BIND_POINT_GRAPHICS;
358 loadSubpassDesc.inputAttachmentCount = 1;
359 loadSubpassDesc.pInputAttachments = &resolveLoadInputRef;
360 loadSubpassDesc.colorAttachmentCount = 1;
361 loadSubpassDesc.pColorAttachments = &colorRef;
362 }
363
364 VkSubpassDescription& mainSubpassDesc = descs.push_back();
365 mainSubpassDesc = {};
366 populate_main_subpass_desc(caps, mainSubpassDesc, colorRef, resolveRef, depthStencilRef);
367
368 if (caps.supportsRasterizationOrderColorAttachmentAccess()) {
369 for (VkSubpassDescription& desc : descs) {
370 desc.flags = VK_SUBPASS_DESCRIPTION_RASTERIZATION_ORDER_ATTACHMENT_COLOR_ACCESS_BIT_EXT;
371 }
372 }
373 }
374
375 } // anonymous namespace
376
377 sk_sp<VulkanRenderPass> VulkanRenderPass::Make(const VulkanSharedContext* context,
378 const RenderPassDesc& renderPassDesc) {
379 const bool needLoadMSAAFromResolveSubpass =
380 RenderPassDescWillLoadMSAAFromResolve(renderPassDesc);
381 const bool isMultisampledRenderToSingleSampled =
382 RenderPassDescWillImplicitlyLoadMSAA(renderPassDesc);
383
384 VkResult result;
385 VkRenderPass renderPass = VK_NULL_HANDLE;
386
387 // VK_EXT_multisampled_render_to_single_sampled usage requires vkCreateRenderPass2.
388 if (isMultisampledRenderToSingleSampled) {
389 // Set up attachment descriptions + references. Declare them before having a helper populate
390 // their values so we can reference them later during RP creation.
391 SkASSERT(renderPassDesc.fColorResolveAttachment.fFormat == TextureFormat::kUnsupported);
392 SkASSERT(!needLoadMSAAFromResolveSubpass);
393
394 VkAttachmentDescription2 defaultAttachmentDescription = {};
395 defaultAttachmentDescription.sType = VK_STRUCTURE_TYPE_ATTACHMENT_DESCRIPTION_2;
396
397 VkAttachmentReference2 defaultAttachmentReference = {};
398 defaultAttachmentReference.sType = VK_STRUCTURE_TYPE_ATTACHMENT_REFERENCE_2;
399 defaultAttachmentReference.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT;
400
401 skia_private::TArray<VkAttachmentDescription2> attachmentDescs;
402 VkAttachmentReference2 colorRef = defaultAttachmentReference;
403 VkAttachmentReference2 resolveRef = defaultAttachmentReference;
404 VkAttachmentReference2 resolveLoadInputRef = defaultAttachmentReference;
405 VkAttachmentReference2 depthStencilRef = defaultAttachmentReference;
406 depthStencilRef.aspectMask =
407 GetVkImageAspectFlags(renderPassDesc.fDepthStencilAttachment.fFormat);
408
409 populate_attachment_refs(renderPassDesc,
410 /*needLoadMSAAFromResolveSubpass=*/false,
411 attachmentDescs,
412 colorRef,
413 resolveRef,
414 resolveLoadInputRef,
415 depthStencilRef,
416 defaultAttachmentDescription);
417 SkASSERT(resolveLoadInputRef.attachment == VK_ATTACHMENT_UNUSED);
418
419 // Assemble subpass information before creating the renderpass. Each renderpass has at least
420 // one subpass dependency (self-dependency for reading the dst texture).
421 VkSubpassDescription2 mainSubpassDesc = {};
422 mainSubpassDesc.sType = VK_STRUCTURE_TYPE_SUBPASS_DESCRIPTION_2;
423 populate_main_subpass_desc(
424 context->vulkanCaps(), mainSubpassDesc, colorRef, resolveRef, depthStencilRef);
425
426 // Enable multisampled render to single-sampled with the render pass's sample count.
427 VkMultisampledRenderToSingleSampledInfoEXT msrtss = {};
428 msrtss.sType = VK_STRUCTURE_TYPE_MULTISAMPLED_RENDER_TO_SINGLE_SAMPLED_INFO_EXT;
429 msrtss.multisampledRenderToSingleSampledEnable = VK_TRUE;
430 msrtss.rasterizationSamples = SampleCountToVkSampleCount(renderPassDesc.fSampleCount);
431 AddToPNextChain(&mainSubpassDesc, &msrtss);
432
433 // Depth/stencil resolve needs additional configuration (even though Graphite always
434 // discards depth/stencil).
435 SkASSERT(renderPassDesc.fDepthStencilAttachment.fStoreOp == StoreOp::kDiscard);
436 VkSubpassDescriptionDepthStencilResolve dsResolve = {};
437 dsResolve.sType = VK_STRUCTURE_TYPE_SUBPASS_DESCRIPTION_DEPTH_STENCIL_RESOLVE;
438 dsResolve.depthResolveMode = VK_RESOLVE_MODE_SAMPLE_ZERO_BIT;
439 dsResolve.stencilResolveMode = VK_RESOLVE_MODE_SAMPLE_ZERO_BIT;
440 AddToPNextChain(&mainSubpassDesc, &dsResolve);
441
442 VkSubpassDependency2 defaultSubpassDependency = {};
443 defaultSubpassDependency.sType = VK_STRUCTURE_TYPE_SUBPASS_DEPENDENCY_2;
444
445 skia_private::STArray<1, VkSubpassDependency2> dependencies;
446 populate_subpass_dependencies(context,
447 dependencies,
448 /*needLoadMSAAFromResolveSubpass=*/false,
449 defaultSubpassDependency);
450
451 // Create VkRenderPass
452 VkRenderPassCreateInfo2 renderPassInfo = {};
453 renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO_2;
454 renderPassInfo.subpassCount = 1;
455 renderPassInfo.pSubpasses = &mainSubpassDesc;
456 renderPassInfo.dependencyCount = dependencies.size();
457 renderPassInfo.pDependencies = dependencies.data();
458 renderPassInfo.attachmentCount = attachmentDescs.size();
459 renderPassInfo.pAttachments = attachmentDescs.data();
460
461 VULKAN_CALL_RESULT(
462 context,
463 result,
464 CreateRenderPass2(context->device(), &renderPassInfo, nullptr, &renderPass));
465 } else {
466 // Same thing as above, but using Vulkan 1.0 render pass API. Practically every usable
467 // driver supports VK_KHR_create_renderpass2, but that is not yet a minimum requirement of
468 // Graphite.
469 skia_private::TArray<VkAttachmentDescription> attachmentDescs;
470 VkAttachmentReference colorRef = {};
471 VkAttachmentReference resolveRef = {};
472 VkAttachmentReference resolveLoadInputRef = {};
473 VkAttachmentReference depthStencilRef = {};
474 populate_attachment_refs(renderPassDesc,
475 needLoadMSAAFromResolveSubpass,
476 attachmentDescs,
477 colorRef,
478 resolveRef,
479 resolveLoadInputRef,
480 depthStencilRef,
481 /*defaultAttachmentDescription=*/{});
482
483 // If loading MSAA from resolve, an extra subpass and an additional dependency is needed.
484 skia_private::STArray<2, VkSubpassDescription> subpassDescs;
485 populate_subpass_descs(context->vulkanCaps(),
486 subpassDescs,
487 colorRef,
488 resolveRef,
489 resolveLoadInputRef,
490 depthStencilRef);
491 skia_private::STArray<2, VkSubpassDependency> dependencies;
492 populate_subpass_dependencies(context,
493 dependencies,
494 needLoadMSAAFromResolveSubpass,
495 /*defaultSubpassDependency=*/VkSubpassDependency{});
496
497 // Create VkRenderPass
498 VkRenderPassCreateInfo renderPassInfo = {};
499 renderPassInfo.sType = VK_STRUCTURE_TYPE_RENDER_PASS_CREATE_INFO;
500 renderPassInfo.subpassCount = subpassDescs.size();
501 renderPassInfo.pSubpasses = subpassDescs.begin();
502 renderPassInfo.dependencyCount = dependencies.size();
503 renderPassInfo.pDependencies = dependencies.data();
504 renderPassInfo.attachmentCount = attachmentDescs.size();
505 renderPassInfo.pAttachments = attachmentDescs.data();
506
507 VULKAN_CALL_RESULT(
508 context,
509 result,
510 CreateRenderPass(context->device(), &renderPassInfo, nullptr, &renderPass));
511 }
512
513 if (result != VK_SUCCESS) {
514 return nullptr;
515 }
516 VkExtent2D granularity;
517 VULKAN_CALL(context->interface(), GetRenderAreaGranularity(context->device(),
518 renderPass,
519 &granularity));
520 return sk_sp<VulkanRenderPass>(new VulkanRenderPass(context, renderPass, granularity));
521 }
522
523 VulkanRenderPass::VulkanRenderPass(const VulkanSharedContext* context,
524 VkRenderPass renderPass,
525 VkExtent2D granularity)
526 : Resource(context,
527 Ownership::kOwned,
528 /*gpuMemorySize=*/0,
529 /*reusableRequiresPurgeable=*/false)
530 , fSharedContext(context)
531 , fRenderPass(renderPass)
532 , fGranularity(granularity) {}
533
534 void VulkanRenderPass::freeGpuData() {
535 VULKAN_CALL(fSharedContext->interface(),
536 DestroyRenderPass(fSharedContext->device(), fRenderPass, nullptr));
537 }
538
539 } // namespace skgpu::graphite