| 1 |
/* |
| 2 |
* Copyright 2022 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 "include/core/SkColorSpace.h" |
| 9 |
#include "include/core/SkColorType.h" |
| 10 |
#include "include/gpu/graphite/PrecompileContext.h" |
| 11 |
#include "include/gpu/graphite/precompile/Precompile.h" |
| 12 |
#include "include/gpu/graphite/precompile/PrecompileColorFilter.h" |
| 13 |
#include "src/gpu/graphite/Caps.h" |
| 14 |
#include "src/gpu/graphite/ContextPriv.h" |
| 15 |
#include "src/gpu/graphite/ContextUtils.h" |
| 16 |
#include "src/gpu/graphite/GraphicsPipeline.h" |
| 17 |
#include "src/gpu/graphite/GraphicsPipelineDesc.h" |
| 18 |
#include "src/gpu/graphite/GraphicsPipelineHandle.h" |
| 19 |
#include "src/gpu/graphite/KeyContext.h" |
| 20 |
#include "src/gpu/graphite/Log.h" |
| 21 |
#include "src/gpu/graphite/PipelineCreationTask.h" |
| 22 |
#include "src/gpu/graphite/PipelineData.h" |
| 23 |
#include "src/gpu/graphite/PrecompileContextPriv.h" |
| 24 |
#include "src/gpu/graphite/PrecompileInternal.h" |
| 25 |
#include "src/gpu/graphite/RenderPassDesc.h" |
| 26 |
#include "src/gpu/graphite/Renderer.h" |
| 27 |
#include "src/gpu/graphite/RendererProvider.h" |
| 28 |
#include "src/gpu/graphite/ResourceProvider.h" |
| 29 |
#include "src/gpu/graphite/RuntimeEffectDictionary.h" |
| 30 |
#include "src/gpu/graphite/UniquePaintParamsID.h" |
| 31 |
#include "src/gpu/graphite/precompile/PaintOptionsPriv.h" |
| 32 |
#include "src/gpu/graphite/precompile/PrecompileColorFiltersPriv.h" |
| 33 |
|
| 34 |
namespace { |
| 35 |
|
| 36 |
using namespace skgpu::graphite; |
| 37 |
|
| 38 |
void compile(const RendererProvider* rendererProvider, |
| 39 |
ResourceProvider* resourceProvider, |
| 40 |
const KeyContext& keyContext, |
| 41 |
UniquePaintParamsID uniqueID, |
| 42 |
DrawTypeFlags drawTypes, |
| 43 |
const RenderPassDesc& renderPassDesc, |
| 44 |
bool withPrimitiveBlender, |
| 45 |
Coverage coverage) { |
| 46 |
|
| 47 |
for (const Renderer* r : rendererProvider->renderers()) { |
| 48 |
if (!(r->drawTypes() & drawTypes)) { |
| 49 |
continue; |
| 50 |
} |
| 51 |
|
| 52 |
if (r->emitsPrimitiveColor() != withPrimitiveBlender) { |
| 53 |
// UniqueIDs are explicitly built either w/ or w/o primitiveBlending so must |
| 54 |
// match what the Renderer requires |
| 55 |
continue; |
| 56 |
} |
| 57 |
|
| 58 |
if (r->coverage() != coverage) { |
| 59 |
// For now, UniqueIDs are explicitly built with a specific type of coverage so must |
| 60 |
// match what the Renderer requires |
| 61 |
continue; |
| 62 |
} |
| 63 |
|
| 64 |
for (auto&& s : r->steps()) { |
| 65 |
SkASSERT(!s->performsShading() || s->emitsPrimitiveColor() == withPrimitiveBlender); |
| 66 |
|
| 67 |
UniquePaintParamsID paintID = s->performsShading() ? uniqueID |
| 68 |
: UniquePaintParamsID::Invalid(); |
| 69 |
|
| 70 |
GraphicsPipelineHandle handle = resourceProvider->createGraphicsPipelineHandle( |
| 71 |
{ s->renderStepID(), paintID }, |
| 72 |
renderPassDesc, |
| 73 |
PipelineCreationFlags::kForPrecompilation); |
| 74 |
resourceProvider->startPipelineCreationTask(keyContext.rtEffectDict(), handle); |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
|
| 79 |
} // anonymous namespace |
| 80 |
|
| 81 |
namespace skgpu::graphite { |
| 82 |
|
| 83 |
void Precompile(PrecompileContext* precompileContext, |
| 84 |
const PaintOptions& options, |
| 85 |
DrawTypeFlags drawTypes, |
| 86 |
SkSpan<const RenderPassProperties> renderPassProperties) { |
| 87 |
|
| 88 |
ShaderCodeDictionary* dict = precompileContext->priv().shaderCodeDictionary(); |
| 89 |
const RendererProvider* rendererProvider = precompileContext->priv().rendererProvider(); |
| 90 |
ResourceProvider* resourceProvider = precompileContext->priv().resourceProvider(); |
| 91 |
const Caps* caps = precompileContext->priv().caps(); |
| 92 |
|
| 93 |
sk_sp<RuntimeEffectDictionary> rtEffectDict = sk_make_sp<RuntimeEffectDictionary>(); |
| 94 |
|
| 95 |
for (const RenderPassProperties& rpp : renderPassProperties) { |
| 96 |
// TODO: Allow the client to pass in mipmapping and protection too? |
| 97 |
TextureInfo info = caps->getDefaultSampledTextureInfo(rpp.fDstCT, |
| 98 |
Mipmapped::kNo, |
| 99 |
Protected::kNo, |
| 100 |
Renderable::kYes); |
| 101 |
|
| 102 |
Swizzle writeSwizzle = caps->getWriteSwizzle(rpp.fDstCT, info); |
| 103 |
|
| 104 |
// TODO(robertphillips): address mismatches between the MSAA requirements of the Renderers |
| 105 |
// associated w/ the requested drawTypes and the specified MSAA setting |
| 106 |
|
| 107 |
// On Native Metal, the LoadOp, StoreOp and clearColor fields don't influence |
| 108 |
// the actual RenderPassDescKey. |
| 109 |
// For Dawn, the LoadOp will sometimes matter. We add an extra LoadOp::kLoad combination |
| 110 |
// when necessary. |
| 111 |
const LoadOp kLoadOps[2] = { LoadOp::kClear, LoadOp::kLoad }; |
| 112 |
|
| 113 |
int numLoadOps = 1; |
| 114 |
if (rpp.fRequiresMSAA && |
| 115 |
!caps->msaaRenderToSingleSampledSupport() && |
| 116 |
caps->loadOpAffectsMSAAPipelines()) { |
| 117 |
numLoadOps = 2; |
| 118 |
} |
| 119 |
|
| 120 |
for (int loadOpIndex = 0; loadOpIndex < numLoadOps; ++loadOpIndex) { |
| 121 |
const RenderPassDesc renderPassDesc = |
| 122 |
RenderPassDesc::Make(caps, |
| 123 |
info, |
| 124 |
kLoadOps[loadOpIndex], |
| 125 |
StoreOp::kStore, |
| 126 |
rpp.fDSFlags, |
| 127 |
/* clearColor= */ { .0f, .0f, .0f, .0f }, |
| 128 |
rpp.fRequiresMSAA, |
| 129 |
writeSwizzle, |
| 130 |
caps->getDstReadStrategy()); |
| 131 |
|
| 132 |
SkColorInfo ci(rpp.fDstCT, kPremul_SkAlphaType, rpp.fDstCS); |
| 133 |
|
| 134 |
// The PipelineDataGatherer and FloatStorageManager are only used to accumulate uniform |
| 135 |
// data. In the pre-compile case we don't need to record the uniform data but the |
| 136 |
// process of generating it is required to create the correct key. |
| 137 |
FloatStorageManager floatStorageManager; |
| 138 |
PipelineDataGatherer gatherer(Layout::kMetal); |
| 139 |
PaintParamsKeyBuilder builder(dict); |
| 140 |
KeyContext keyContext(caps, &floatStorageManager, &builder, &gatherer, dict, |
| 141 |
rtEffectDict, ci); |
| 142 |
|
| 143 |
for (Coverage coverage : { Coverage::kNone, Coverage::kSingleChannel }) { |
| 144 |
PrecompileCombinations( |
| 145 |
rendererProvider, |
| 146 |
resourceProvider, |
| 147 |
options, keyContext, |
| 148 |
static_cast<DrawTypeFlags>(drawTypes & ~(DrawTypeFlags::kBitmapText_Color | |
| 149 |
DrawTypeFlags::kBitmapText_LCD | |
| 150 |
DrawTypeFlags::kSDFText_LCD | |
| 151 |
DrawTypeFlags::kDrawVertices | |
| 152 |
DrawTypeFlags::kDropShadows)), |
| 153 |
/* withPrimitiveBlender= */ false, |
| 154 |
coverage, |
| 155 |
renderPassDesc); |
| 156 |
} |
| 157 |
|
| 158 |
if (drawTypes & DrawTypeFlags::kNonSimpleShape) { |
| 159 |
// Special case handling to pick up the: |
| 160 |
// "CoverBoundsRenderStep[InverseCover] + (empty)" |
| 161 |
// pipelines. |
| 162 |
const RenderStep* renderStep = |
| 163 |
rendererProvider->lookup(RenderStep::RenderStepID::kCoverBounds_InverseCover); |
| 164 |
|
| 165 |
GraphicsPipelineHandle handle = resourceProvider->createGraphicsPipelineHandle( |
| 166 |
{ renderStep->renderStepID(), UniquePaintParamsID::Invalid() }, |
| 167 |
renderPassDesc, |
| 168 |
PipelineCreationFlags::kForPrecompilation); |
| 169 |
resourceProvider->startPipelineCreationTask(keyContext.rtEffectDict(), handle); |
| 170 |
} |
| 171 |
|
| 172 |
if (drawTypes & DrawTypeFlags::kBitmapText_Color) { |
| 173 |
DrawTypeFlags reducedTypes = |
| 174 |
static_cast<DrawTypeFlags>(drawTypes & (DrawTypeFlags::kBitmapText_Color | |
| 175 |
DrawTypeFlags::kAnalyticClip)); |
| 176 |
// For color emoji text, shaders don't affect the final color |
| 177 |
PaintOptions tmp = options; |
| 178 |
tmp.setShaders({}); |
| 179 |
|
| 180 |
// ARGB text doesn't emit coverage and always has a primitive blender |
| 181 |
PrecompileCombinations(rendererProvider, |
| 182 |
resourceProvider, |
| 183 |
tmp, |
| 184 |
keyContext, |
| 185 |
reducedTypes, |
| 186 |
/* withPrimitiveBlender= */ true, |
| 187 |
Coverage::kNone, |
| 188 |
renderPassDesc); |
| 189 |
} |
| 190 |
|
| 191 |
if (drawTypes & (DrawTypeFlags::kBitmapText_LCD | DrawTypeFlags::kSDFText_LCD)) { |
| 192 |
DrawTypeFlags reducedTypes = |
| 193 |
static_cast<DrawTypeFlags>(drawTypes & (DrawTypeFlags::kBitmapText_LCD | |
| 194 |
DrawTypeFlags::kSDFText_LCD | |
| 195 |
DrawTypeFlags::kAnalyticClip)); |
| 196 |
// LCD-based text always emits LCD coverage but never has primitiveBlenders |
| 197 |
PrecompileCombinations( |
| 198 |
rendererProvider, |
| 199 |
resourceProvider, |
| 200 |
options, keyContext, |
| 201 |
reducedTypes, |
| 202 |
/* withPrimitiveBlender= */ false, |
| 203 |
Coverage::kLCD, |
| 204 |
renderPassDesc); |
| 205 |
} |
| 206 |
|
| 207 |
if (drawTypes & DrawTypeFlags::kDrawVertices) { |
| 208 |
DrawTypeFlags reducedTypes = |
| 209 |
static_cast<DrawTypeFlags>(drawTypes & (DrawTypeFlags::kDrawVertices | |
| 210 |
DrawTypeFlags::kAnalyticClip)); |
| 211 |
// drawVertices w/ colors use a primitiveBlender while those w/o don't. It never |
| 212 |
// emits coverage. |
| 213 |
for (bool withPrimitiveBlender : { true, false }) { |
| 214 |
PrecompileCombinations(rendererProvider, |
| 215 |
resourceProvider, |
| 216 |
options, keyContext, |
| 217 |
reducedTypes, |
| 218 |
withPrimitiveBlender, |
| 219 |
Coverage::kNone, |
| 220 |
renderPassDesc); |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
if (drawTypes & DrawTypeFlags::kDropShadows) { |
| 225 |
DrawTypeFlags reducedTypes = |
| 226 |
static_cast<DrawTypeFlags>(drawTypes & (DrawTypeFlags::kDropShadows | |
| 227 |
DrawTypeFlags::kAnalyticClip)); |
| 228 |
|
| 229 |
PaintOptions newOptions; |
| 230 |
newOptions.setBlendModes(SKSPAN_INIT_ONE(SkBlendMode::kSrcOver)); |
| 231 |
|
| 232 |
// Analytic |
| 233 |
{ |
| 234 |
PrecompileCombinations(rendererProvider, |
| 235 |
resourceProvider, |
| 236 |
newOptions, keyContext, |
| 237 |
reducedTypes, |
| 238 |
/* withPrimitiveBlender= */ false, |
| 239 |
Coverage::kSingleChannel, |
| 240 |
renderPassDesc); |
| 241 |
} |
| 242 |
|
| 243 |
// Geometric |
| 244 |
{ |
| 245 |
sk_sp<PrecompileColorFilter> cf = PrecompileColorFilters::Compose( |
| 246 |
{{ PrecompileColorFilters::Blend(SKSPAN_INIT_ONE(SkBlendMode::kModulate)) }}, |
| 247 |
{{ PrecompileColorFiltersPriv::Gaussian() }}); |
| 248 |
|
| 249 |
newOptions.setColorFilters({{ std::move(cf) }}); |
| 250 |
newOptions.priv().setPrimitiveBlendMode(SkBlendMode::kDst); |
| 251 |
newOptions.priv().setSkipColorXform(true); |
| 252 |
|
| 253 |
PrecompileCombinations(rendererProvider, |
| 254 |
resourceProvider, |
| 255 |
newOptions, keyContext, |
| 256 |
reducedTypes, |
| 257 |
/* withPrimitiveBlender= */ true, |
| 258 |
Coverage::kNone, |
| 259 |
renderPassDesc); |
| 260 |
} |
| 261 |
} |
| 262 |
} |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
void PrecompileCombinations(const RendererProvider* rendererProvider, |
| 267 |
ResourceProvider* resourceProvider, |
| 268 |
const PaintOptions& options, |
| 269 |
const KeyContext& keyContext, |
| 270 |
DrawTypeFlags drawTypes, |
| 271 |
bool withPrimitiveBlender, |
| 272 |
Coverage coverage, |
| 273 |
const RenderPassDesc& renderPassDescIn) { |
| 274 |
if (drawTypes == DrawTypeFlags::kNone) { |
| 275 |
return; |
| 276 |
} |
| 277 |
|
| 278 |
options.priv().buildCombinations( |
| 279 |
keyContext, |
| 280 |
drawTypes, |
| 281 |
withPrimitiveBlender, |
| 282 |
coverage, |
| 283 |
renderPassDescIn, |
| 284 |
[rendererProvider, resourceProvider, &keyContext](UniquePaintParamsID uniqueID, |
| 285 |
DrawTypeFlags drawTypes, |
| 286 |
bool withPrimitiveBlender, |
| 287 |
Coverage coverage, |
| 288 |
const RenderPassDesc& renderPassDesc) { |
| 289 |
compile(rendererProvider, |
| 290 |
resourceProvider, |
| 291 |
keyContext, |
| 292 |
uniqueID, |
| 293 |
drawTypes, |
| 294 |
renderPassDesc, |
| 295 |
withPrimitiveBlender, |
| 296 |
coverage); |
| 297 |
}); |
| 298 |
} |
| 299 |
|
| 300 |
} // namespace skgpu::graphite |