| 1 |
/* |
| 2 |
* Copyright 2024 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/RenderPassDesc.h" |
| 9 |
|
| 10 |
#include "include/gpu/graphite/TextureInfo.h" |
| 11 |
#include "src/gpu/graphite/Caps.h" |
| 12 |
#include "src/gpu/graphite/TextureInfoPriv.h" |
| 13 |
|
| 14 |
namespace skgpu::graphite { |
| 15 |
|
| 16 |
namespace { |
| 17 |
|
| 18 |
const char* to_str(LoadOp op) { |
| 19 |
switch (op) { |
| 20 |
case LoadOp::kLoad: return "load"; |
| 21 |
case LoadOp::kClear: return "clear"; |
| 22 |
case LoadOp::kDiscard: return "discard"; |
| 23 |
} |
| 24 |
|
| 25 |
SkUNREACHABLE; |
| 26 |
} |
| 27 |
|
| 28 |
const char* to_str(StoreOp op) { |
| 29 |
switch (op) { |
| 30 |
case StoreOp::kStore: return "store"; |
| 31 |
case StoreOp::kDiscard: return "discard"; |
| 32 |
} |
| 33 |
|
| 34 |
SkUNREACHABLE; |
| 35 |
} |
| 36 |
|
| 37 |
} // anonymous namespace |
| 38 |
|
| 39 |
RenderPassDesc RenderPassDesc::Make(const Caps* caps, |
| 40 |
const TextureInfo& targetInfo, |
| 41 |
LoadOp loadOp, |
| 42 |
StoreOp storeOp, |
| 43 |
SkEnumBitMask<DepthStencilFlags> depthStencilFlags, |
| 44 |
const std::array<float, 4>& clearColor, |
| 45 |
bool requiresMSAA, |
| 46 |
Swizzle writeSwizzle, |
| 47 |
const DstReadStrategy dstReadStrategy) { |
| 48 |
// It doesn't make sense to have a storeOp for our main target not be store. Why are we doing |
| 49 |
// this DrawPass then |
| 50 |
SkASSERT(storeOp == StoreOp::kStore); |
| 51 |
|
| 52 |
RenderPassDesc desc; |
| 53 |
desc.fClearColor = clearColor; |
| 54 |
// Depth and stencil is currently always cleared to 1.f or 0 if it's used. Depth is 1.0 and |
| 55 |
// counts down as painter's order increases due to HW preference for historic OpenGL defaults |
| 56 |
// of a fast hi-z clear value of 1.0 with depth test of lesser. |
| 57 |
desc.fClearDepth = 1.f; |
| 58 |
desc.fClearStencil = 0; |
| 59 |
desc.fWriteSwizzle = writeSwizzle; |
| 60 |
desc.fDstReadStrategy = dstReadStrategy; |
| 61 |
|
| 62 |
TextureFormat colorFormat = TextureInfoPriv::ViewFormat(targetInfo); |
| 63 |
// The render pass's overall sample count will either be the target's sample count |
| 64 |
// (when single-sampling or already multisampled), or the default sample count (which will then |
| 65 |
// be either the implicit sample count for msaa-render-to-single-sample or the explicit sample |
| 66 |
// count of a separate color attachment). |
| 67 |
// |
| 68 |
// Higher-level logic should ensure the default MSAA sample count is supported if using either |
| 69 |
// msaa-render-to-single-sample or with separate attachments, and select non-MSAA techniques if |
| 70 |
// they weren't supported. getCompatibleMSAASampleCount() downgrades to single-sampled if we got |
| 71 |
// here and MSAA isn't supported. |
| 72 |
const bool msaaRenderToSingleSampledSupport = |
| 73 |
caps->msaaTextureRenderToSingleSampledSupport(targetInfo); |
| 74 |
desc.fSampleCount = requiresMSAA ? caps->getCompatibleMSAASampleCount(targetInfo) |
| 75 |
: targetInfo.sampleCount(); |
| 76 |
|
| 77 |
// We need to handle MSAA with an extra color attachment if: |
| 78 |
const bool needsMSAAColorAttachment = |
| 79 |
desc.fSampleCount > SampleCount::k1 && // using MSAA for the render pass, |
| 80 |
targetInfo.sampleCount() == SampleCount::k1 && // the target isn't already MSAA'ed, |
| 81 |
!msaaRenderToSingleSampledSupport; // can't use an MSAA->single extension. |
| 82 |
if (needsMSAAColorAttachment) { |
| 83 |
// We set the color and resolve attachments up the same regardless of if the backend ends up |
| 84 |
// using msaaRenderToSingleSampledSupport() to skip explicitly creating the MSAA attachment. |
| 85 |
// The color attachment (and any depth/stencil attachment) will use `sampleCount` and the |
| 86 |
// resolve attachment will be single-sampled. |
| 87 |
desc.fColorAttachment = {colorFormat, |
| 88 |
loadOp != LoadOp::kClear ? LoadOp::kDiscard : LoadOp::kClear, |
| 89 |
StoreOp::kDiscard, |
| 90 |
desc.fSampleCount}; |
| 91 |
desc.fColorResolveAttachment = {colorFormat, |
| 92 |
loadOp != LoadOp::kLoad ? LoadOp::kDiscard : LoadOp::kLoad, |
| 93 |
storeOp, |
| 94 |
SampleCount::k1}; |
| 95 |
} else { |
| 96 |
// The target will be the color attachment and skip configuring the resolve attachment. |
| 97 |
SkASSERT(desc.fColorResolveAttachment.fFormat == TextureFormat::kUnsupported); |
| 98 |
desc.fColorAttachment = {colorFormat, |
| 99 |
loadOp, |
| 100 |
storeOp, |
| 101 |
targetInfo.sampleCount()}; |
| 102 |
} |
| 103 |
|
| 104 |
if (depthStencilFlags != DepthStencilFlags::kNone) { |
| 105 |
// To reduce pipeline compiles and attachment creations, if we need multisampling and need |
| 106 |
// a depth or stencil attachment, we always choose a depth-AND-stencil format. |
| 107 |
if (desc.fColorAttachment.fSampleCount > SampleCount::k1) { |
| 108 |
depthStencilFlags = DepthStencilFlags::kDepthStencil; |
| 109 |
} |
| 110 |
TextureFormat dsFormat = caps->getDepthStencilFormat(depthStencilFlags); |
| 111 |
SkASSERT(dsFormat != TextureFormat::kUnsupported); |
| 112 |
// Depth and stencil values are currently always cleared and don't need to persist. |
| 113 |
// The sample count should always match render pass. |
| 114 |
desc.fDepthStencilAttachment = {dsFormat, |
| 115 |
LoadOp::kClear, |
| 116 |
StoreOp::kDiscard, |
| 117 |
desc.fSampleCount}; |
| 118 |
} else { |
| 119 |
SkASSERT(desc.fDepthStencilAttachment.fFormat == TextureFormat::kUnsupported); |
| 120 |
} |
| 121 |
|
| 122 |
return desc; |
| 123 |
} |
| 124 |
|
| 125 |
SkString RenderPassDesc::toString() const { |
| 126 |
return SkStringPrintf("RP(color: %s, resolve: %s, ds: %s, samples: %u, swizzle: %s, " |
| 127 |
"clear: c(%f,%f,%f,%f), d(%f), s(0x%02x), dst read: %u)", |
| 128 |
fColorAttachment.toString().c_str(), |
| 129 |
fColorResolveAttachment.toString().c_str(), |
| 130 |
fDepthStencilAttachment.toString().c_str(), |
| 131 |
(unsigned)fSampleCount, |
| 132 |
fWriteSwizzle.asString().c_str(), |
| 133 |
fClearColor[0], fClearColor[1], fClearColor[2], fClearColor[3], |
| 134 |
fClearDepth, |
| 135 |
fClearStencil, |
| 136 |
(unsigned)fDstReadStrategy); |
| 137 |
} |
| 138 |
|
| 139 |
SkString RenderPassDesc::toPipelineLabel() const { |
| 140 |
// Given current policies, these assumptions should hold and mean the conciseness in the label |
| 141 |
// is still unambiguous. |
| 142 |
SkASSERT(fColorAttachment.fFormat != TextureFormat::kUnsupported); |
| 143 |
SkASSERT(fColorResolveAttachment.fFormat == TextureFormat::kUnsupported || |
| 144 |
fColorResolveAttachment.fFormat == fColorAttachment.fFormat); |
| 145 |
SkASSERT(fColorResolveAttachment.fFormat == TextureFormat::kUnsupported || |
| 146 |
fColorResolveAttachment.fSampleCount == SampleCount::k1); |
| 147 |
SkASSERT(fColorAttachment.fSampleCount == fSampleCount || |
| 148 |
(fColorAttachment.fSampleCount == SampleCount::k1 && fSampleCount > SampleCount::k1)); |
| 149 |
|
| 150 |
const char* colorFormatStr = TextureFormatName(fColorAttachment.fFormat); |
| 151 |
const char* dsFormatStr = "{}"; |
| 152 |
if (fDepthStencilAttachment.fFormat != TextureFormat::kUnsupported) { |
| 153 |
dsFormatStr = TextureFormatName(fDepthStencilAttachment.fFormat); |
| 154 |
} |
| 155 |
|
| 156 |
// This intentionally only includes the fixed state that impacts pipeline compilation. |
| 157 |
// We include the load op of the color attachment when there is a resolve attachment because |
| 158 |
// the load may trigger a different renderpass description. |
| 159 |
const char* colorLoadStr = ""; |
| 160 |
const bool loadMsaaFromResolve = |
| 161 |
fColorResolveAttachment.fFormat != TextureFormat::kUnsupported && |
| 162 |
fColorResolveAttachment.fLoadOp == LoadOp::kLoad; |
| 163 |
|
| 164 |
// This should, technically, check Caps::loadOpAffectsMSAAPipelines before adding the extra |
| 165 |
// string. Only the Metal backend doesn't set that flag, however, so we just assume it is set |
| 166 |
// to reduce plumbing. Since the Metal backend doesn't differentiate its UniqueKeys wrt |
| 167 |
// resolve-loads, this can lead to instances where two Metal Pipeline labels will map to the |
| 168 |
// same UniqueKey (i.e., one with "w/ msaa load" and one without it). |
| 169 |
if (loadMsaaFromResolve /* && Caps::loadOpAffectsMSAAPipelines() */) { |
| 170 |
colorLoadStr = " w/ msaa load"; |
| 171 |
} |
| 172 |
|
| 173 |
// There are three supported ways of achieving MSAA rendering that we distinguish compactly. |
| 174 |
// 1. Direct sampling w/ N samples (includes single sample) |
| 175 |
// 2. MSAA render to single-sampled extensions |
| 176 |
// 3. Explicit MSAA color attachment w/ resolve |
| 177 |
// Since we don't expect to be mixing case 2 and 3 on the same device, treating them the same |
| 178 |
// in the pipeline labels makes it more convenient when writing test expectations. |
| 179 |
SkString sampleCountStr; |
| 180 |
if (fColorResolveAttachment.fFormat == TextureFormat::kUnsupported && |
| 181 |
fSampleCount == fColorAttachment.fSampleCount) { |
| 182 |
// Case 1: "xN" |
| 183 |
sampleCountStr = SkStringPrintf("x%u", (unsigned)fSampleCount); |
| 184 |
} else { |
| 185 |
// Case 2 and 3: "xN->1" |
| 186 |
sampleCountStr = SkStringPrintf("x%u->1", (unsigned)fSampleCount); |
| 187 |
} |
| 188 |
// NOTE: This label does not differentiate between explicitly resolved MSAA color attachments |
| 189 |
// and MSAA-render-to-single-sample renderpasses. For a given set of Caps, we currently only |
| 190 |
// expect to generate one or the other variety. |
| 191 |
return SkStringPrintf("RP((%s+%s %s).%s%s)", |
| 192 |
colorFormatStr, |
| 193 |
dsFormatStr, |
| 194 |
sampleCountStr.c_str(), |
| 195 |
fWriteSwizzle.asString().c_str(), |
| 196 |
colorLoadStr); |
| 197 |
} |
| 198 |
|
| 199 |
SkString AttachmentDesc::toString() const { |
| 200 |
if (fFormat == TextureFormat::kUnsupported) { |
| 201 |
return SkString("{}"); |
| 202 |
} else { |
| 203 |
return SkStringPrintf("{f: %s x%u, ops: %s->%s}", |
| 204 |
TextureFormatName(fFormat), |
| 205 |
(unsigned)fSampleCount, |
| 206 |
to_str(fLoadOp), |
| 207 |
to_str(fStoreOp)); |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
bool AttachmentDesc::isCompatible(const TextureInfo& texInfo) const { |
| 212 |
return fFormat == TextureInfoPriv::ViewFormat(texInfo) && fSampleCount == texInfo.sampleCount(); |
| 213 |
} |
| 214 |
|
| 215 |
} // namespace skgpu::graphite |