| 1 |
/* |
| 2 |
* Copyright 2021 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/mtl/MtlGraphicsPipeline.h" |
| 9 |
|
| 10 |
#include "include/gpu/graphite/TextureInfo.h" |
| 11 |
#include "include/gpu/graphite/mtl/MtlGraphiteTypes.h" |
| 12 |
#include "src/gpu/graphite/Attribute.h" |
| 13 |
#include "src/gpu/graphite/ContextUtils.h" |
| 14 |
#include "src/gpu/graphite/GraphicsPipelineDesc.h" |
| 15 |
#include "src/gpu/graphite/Log.h" |
| 16 |
#include "src/gpu/graphite/RenderPassDesc.h" |
| 17 |
#include "src/gpu/graphite/RendererProvider.h" |
| 18 |
#include "src/gpu/graphite/ShaderInfo.h" |
| 19 |
#include "src/gpu/graphite/TextureInfoPriv.h" |
| 20 |
#include "src/gpu/graphite/mtl/MtlGraphiteUtils.h" |
| 21 |
#include "src/gpu/graphite/mtl/MtlSharedContext.h" |
| 22 |
#include "src/gpu/mtl/MtlUtilsPriv.h" |
| 23 |
#include "src/sksl/SkSLCompiler.h" |
| 24 |
#include "src/sksl/SkSLProgramSettings.h" |
| 25 |
#include "src/sksl/codegen/SkSLNativeShader.h" |
| 26 |
#include "src/sksl/ir/SkSLProgram.h" |
| 27 |
|
| 28 |
namespace skgpu::graphite { |
| 29 |
|
| 30 |
namespace { |
| 31 |
|
| 32 |
inline MTLVertexFormat attribute_type_to_mtlformat(VertexAttribType type) { |
| 33 |
switch (type) { |
| 34 |
case VertexAttribType::kFloat: |
| 35 |
return MTLVertexFormatFloat; |
| 36 |
case VertexAttribType::kFloat2: |
| 37 |
return MTLVertexFormatFloat2; |
| 38 |
case VertexAttribType::kFloat3: |
| 39 |
return MTLVertexFormatFloat3; |
| 40 |
case VertexAttribType::kFloat4: |
| 41 |
return MTLVertexFormatFloat4; |
| 42 |
case VertexAttribType::kHalf: |
| 43 |
if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)) { |
| 44 |
return MTLVertexFormatHalf; |
| 45 |
} else { |
| 46 |
return MTLVertexFormatInvalid; |
| 47 |
} |
| 48 |
case VertexAttribType::kHalf2: |
| 49 |
return MTLVertexFormatHalf2; |
| 50 |
case VertexAttribType::kHalf4: |
| 51 |
return MTLVertexFormatHalf4; |
| 52 |
case VertexAttribType::kInt2: |
| 53 |
return MTLVertexFormatInt2; |
| 54 |
case VertexAttribType::kInt3: |
| 55 |
return MTLVertexFormatInt3; |
| 56 |
case VertexAttribType::kInt4: |
| 57 |
return MTLVertexFormatInt4; |
| 58 |
case VertexAttribType::kUInt2: |
| 59 |
return MTLVertexFormatUInt2; |
| 60 |
case VertexAttribType::kByte: |
| 61 |
if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)) { |
| 62 |
return MTLVertexFormatChar; |
| 63 |
} else { |
| 64 |
return MTLVertexFormatInvalid; |
| 65 |
} |
| 66 |
case VertexAttribType::kByte2: |
| 67 |
return MTLVertexFormatChar2; |
| 68 |
case VertexAttribType::kByte4: |
| 69 |
return MTLVertexFormatChar4; |
| 70 |
case VertexAttribType::kUByte: |
| 71 |
if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)) { |
| 72 |
return MTLVertexFormatUChar; |
| 73 |
} else { |
| 74 |
return MTLVertexFormatInvalid; |
| 75 |
} |
| 76 |
case VertexAttribType::kUByte2: |
| 77 |
return MTLVertexFormatUChar2; |
| 78 |
case VertexAttribType::kUByte4: |
| 79 |
return MTLVertexFormatUChar4; |
| 80 |
case VertexAttribType::kUByte_norm: |
| 81 |
if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)) { |
| 82 |
return MTLVertexFormatUCharNormalized; |
| 83 |
} else { |
| 84 |
return MTLVertexFormatInvalid; |
| 85 |
} |
| 86 |
case VertexAttribType::kUByte4_norm: |
| 87 |
return MTLVertexFormatUChar4Normalized; |
| 88 |
case VertexAttribType::kShort2: |
| 89 |
return MTLVertexFormatShort2; |
| 90 |
case VertexAttribType::kShort4: |
| 91 |
return MTLVertexFormatShort4; |
| 92 |
case VertexAttribType::kUShort2: |
| 93 |
return MTLVertexFormatUShort2; |
| 94 |
case VertexAttribType::kUShort2_norm: |
| 95 |
return MTLVertexFormatUShort2Normalized; |
| 96 |
case VertexAttribType::kInt: |
| 97 |
return MTLVertexFormatInt; |
| 98 |
case VertexAttribType::kUInt: |
| 99 |
return MTLVertexFormatUInt; |
| 100 |
case VertexAttribType::kUShort_norm: |
| 101 |
if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)) { |
| 102 |
return MTLVertexFormatUShortNormalized; |
| 103 |
} else { |
| 104 |
return MTLVertexFormatInvalid; |
| 105 |
} |
| 106 |
case VertexAttribType::kUShort4_norm: |
| 107 |
return MTLVertexFormatUShort4Normalized; |
| 108 |
} |
| 109 |
SK_ABORT("Unknown vertex attribute type"); |
| 110 |
} |
| 111 |
|
| 112 |
MTLVertexDescriptor* create_vertex_descriptor(MTLVertexStepFunction appendStepFunc, |
| 113 |
SkSpan<const Attribute> staticAttrs, |
| 114 |
SkSpan<const Attribute> appendAttrs) { |
| 115 |
auto vertexDescriptor = [[MTLVertexDescriptor alloc] init]; |
| 116 |
int attributeIndex = 0; |
| 117 |
|
| 118 |
size_t staticAttributeOffset = 0; |
| 119 |
for (const auto& attribute : staticAttrs) { |
| 120 |
MTLVertexAttributeDescriptor* mtlAttribute = vertexDescriptor.attributes[attributeIndex]; |
| 121 |
MTLVertexFormat format = attribute_type_to_mtlformat(attribute.cpuType()); |
| 122 |
SkASSERT(MTLVertexFormatInvalid != format); |
| 123 |
mtlAttribute.format = format; |
| 124 |
mtlAttribute.offset = staticAttributeOffset; |
| 125 |
mtlAttribute.bufferIndex = MtlGraphicsPipeline::kStaticDataBufferIndex; |
| 126 |
|
| 127 |
staticAttributeOffset += attribute.sizeAlign4(); |
| 128 |
attributeIndex++; |
| 129 |
} |
| 130 |
|
| 131 |
if (staticAttributeOffset) { |
| 132 |
MTLVertexBufferLayoutDescriptor* staticDataBufferLayout = |
| 133 |
vertexDescriptor.layouts[MtlGraphicsPipeline::kStaticDataBufferIndex]; |
| 134 |
staticDataBufferLayout.stepFunction = MTLVertexStepFunctionPerVertex; |
| 135 |
staticDataBufferLayout.stepRate = 1; |
| 136 |
staticDataBufferLayout.stride = staticAttributeOffset; |
| 137 |
} |
| 138 |
|
| 139 |
size_t appendAttributeOffset = 0; |
| 140 |
for (const auto& attribute : appendAttrs) { |
| 141 |
MTLVertexAttributeDescriptor* mtlAttribute = vertexDescriptor.attributes[attributeIndex]; |
| 142 |
MTLVertexFormat format = attribute_type_to_mtlformat(attribute.cpuType()); |
| 143 |
SkASSERT(MTLVertexFormatInvalid != format); |
| 144 |
mtlAttribute.format = format; |
| 145 |
mtlAttribute.offset = appendAttributeOffset; |
| 146 |
mtlAttribute.bufferIndex = MtlGraphicsPipeline::kAppendDataBufferIndex; |
| 147 |
|
| 148 |
appendAttributeOffset += attribute.sizeAlign4(); |
| 149 |
attributeIndex++; |
| 150 |
} |
| 151 |
|
| 152 |
if (appendAttributeOffset) { |
| 153 |
MTLVertexBufferLayoutDescriptor* appendBufferDataLayout = |
| 154 |
vertexDescriptor.layouts[MtlGraphicsPipeline::kAppendDataBufferIndex]; |
| 155 |
appendBufferDataLayout.stepFunction = appendStepFunc; |
| 156 |
appendBufferDataLayout.stepRate = 1; |
| 157 |
appendBufferDataLayout.stride = appendAttributeOffset; |
| 158 |
} |
| 159 |
return vertexDescriptor; |
| 160 |
} |
| 161 |
|
| 162 |
// TODO: share this w/ Ganesh Metal backend? |
| 163 |
static MTLBlendFactor blend_coeff_to_mtl_blend(skgpu::BlendCoeff coeff) { |
| 164 |
switch (coeff) { |
| 165 |
case skgpu::BlendCoeff::kZero: |
| 166 |
return MTLBlendFactorZero; |
| 167 |
case skgpu::BlendCoeff::kOne: |
| 168 |
return MTLBlendFactorOne; |
| 169 |
case skgpu::BlendCoeff::kSC: |
| 170 |
return MTLBlendFactorSourceColor; |
| 171 |
case skgpu::BlendCoeff::kISC: |
| 172 |
return MTLBlendFactorOneMinusSourceColor; |
| 173 |
case skgpu::BlendCoeff::kDC: |
| 174 |
return MTLBlendFactorDestinationColor; |
| 175 |
case skgpu::BlendCoeff::kIDC: |
| 176 |
return MTLBlendFactorOneMinusDestinationColor; |
| 177 |
case skgpu::BlendCoeff::kSA: |
| 178 |
return MTLBlendFactorSourceAlpha; |
| 179 |
case skgpu::BlendCoeff::kISA: |
| 180 |
return MTLBlendFactorOneMinusSourceAlpha; |
| 181 |
case skgpu::BlendCoeff::kDA: |
| 182 |
return MTLBlendFactorDestinationAlpha; |
| 183 |
case skgpu::BlendCoeff::kIDA: |
| 184 |
return MTLBlendFactorOneMinusDestinationAlpha; |
| 185 |
case skgpu::BlendCoeff::kConstC: |
| 186 |
return MTLBlendFactorBlendColor; |
| 187 |
case skgpu::BlendCoeff::kIConstC: |
| 188 |
return MTLBlendFactorOneMinusBlendColor; |
| 189 |
case skgpu::BlendCoeff::kS2C: |
| 190 |
if (@available(macOS 10.12, iOS 11.0, tvOS 11.0, *)) { |
| 191 |
return MTLBlendFactorSource1Color; |
| 192 |
} else { |
| 193 |
return MTLBlendFactorZero; |
| 194 |
} |
| 195 |
case skgpu::BlendCoeff::kIS2C: |
| 196 |
if (@available(macOS 10.12, iOS 11.0, tvOS 11.0, *)) { |
| 197 |
return MTLBlendFactorOneMinusSource1Color; |
| 198 |
} else { |
| 199 |
return MTLBlendFactorZero; |
| 200 |
} |
| 201 |
case skgpu::BlendCoeff::kS2A: |
| 202 |
if (@available(macOS 10.12, iOS 11.0, tvOS 11.0, *)) { |
| 203 |
return MTLBlendFactorSource1Alpha; |
| 204 |
} else { |
| 205 |
return MTLBlendFactorZero; |
| 206 |
} |
| 207 |
case skgpu::BlendCoeff::kIS2A: |
| 208 |
if (@available(macOS 10.12, iOS 11.0, tvOS 11.0, *)) { |
| 209 |
return MTLBlendFactorOneMinusSource1Alpha; |
| 210 |
} else { |
| 211 |
return MTLBlendFactorZero; |
| 212 |
} |
| 213 |
case skgpu::BlendCoeff::kIllegal: |
| 214 |
return MTLBlendFactorZero; |
| 215 |
} |
| 216 |
|
| 217 |
SK_ABORT("Unknown blend coefficient"); |
| 218 |
} |
| 219 |
|
| 220 |
// TODO: share this w/ Ganesh Metal backend? |
| 221 |
static MTLBlendOperation blend_equation_to_mtl_blend_op(skgpu::BlendEquation equation) { |
| 222 |
static const MTLBlendOperation gTable[] = { |
| 223 |
MTLBlendOperationAdd, // skgpu::BlendEquation::kAdd |
| 224 |
MTLBlendOperationSubtract, // skgpu::BlendEquation::kSubtract |
| 225 |
MTLBlendOperationReverseSubtract, // skgpu::BlendEquation::kReverseSubtract |
| 226 |
}; |
| 227 |
static_assert(std::size(gTable) == (int)skgpu::BlendEquation::kFirstAdvanced); |
| 228 |
static_assert(0 == (int)skgpu::BlendEquation::kAdd); |
| 229 |
static_assert(1 == (int)skgpu::BlendEquation::kSubtract); |
| 230 |
static_assert(2 == (int)skgpu::BlendEquation::kReverseSubtract); |
| 231 |
|
| 232 |
SkASSERT((unsigned)equation < skgpu::kBlendEquationCnt); |
| 233 |
return gTable[(int)equation]; |
| 234 |
} |
| 235 |
|
| 236 |
static MTLRenderPipelineColorAttachmentDescriptor* create_color_attachment( |
| 237 |
MTLPixelFormat format, |
| 238 |
const BlendInfo& blendInfo) { |
| 239 |
|
| 240 |
skgpu::BlendEquation equation = blendInfo.fEquation; |
| 241 |
skgpu::BlendCoeff srcCoeff = blendInfo.fSrcBlend; |
| 242 |
skgpu::BlendCoeff dstCoeff = blendInfo.fDstBlend; |
| 243 |
bool blendOn = !skgpu::BlendShouldDisable(equation, srcCoeff, dstCoeff); |
| 244 |
|
| 245 |
// TODO: I *think* this gets cleaned up by the pipelineDescriptor? |
| 246 |
auto mtlColorAttachment = [[MTLRenderPipelineColorAttachmentDescriptor alloc] init]; |
| 247 |
|
| 248 |
mtlColorAttachment.pixelFormat = format; |
| 249 |
|
| 250 |
mtlColorAttachment.blendingEnabled = blendOn; |
| 251 |
|
| 252 |
if (blendOn) { |
| 253 |
mtlColorAttachment.sourceRGBBlendFactor = blend_coeff_to_mtl_blend(srcCoeff); |
| 254 |
mtlColorAttachment.destinationRGBBlendFactor = blend_coeff_to_mtl_blend(dstCoeff); |
| 255 |
mtlColorAttachment.rgbBlendOperation = blend_equation_to_mtl_blend_op(equation); |
| 256 |
mtlColorAttachment.sourceAlphaBlendFactor = blend_coeff_to_mtl_blend(srcCoeff); |
| 257 |
mtlColorAttachment.destinationAlphaBlendFactor = blend_coeff_to_mtl_blend(dstCoeff); |
| 258 |
mtlColorAttachment.alphaBlendOperation = blend_equation_to_mtl_blend_op(equation); |
| 259 |
} |
| 260 |
|
| 261 |
mtlColorAttachment.writeMask = blendInfo.fWritesColor ? MTLColorWriteMaskAll |
| 262 |
: MTLColorWriteMaskNone; |
| 263 |
|
| 264 |
return mtlColorAttachment; |
| 265 |
} |
| 266 |
|
| 267 |
} // anonymous namespace |
| 268 |
|
| 269 |
sk_sp<MtlGraphicsPipeline> MtlGraphicsPipeline::Make( |
| 270 |
const MtlSharedContext* sharedContext, |
| 271 |
const RuntimeEffectDictionary* runtimeDict, |
| 272 |
const UniqueKey& pipelineKey, |
| 273 |
const GraphicsPipelineDesc& pipelineDesc, |
| 274 |
const RenderPassDesc& renderPassDesc, |
| 275 |
SkEnumBitMask<PipelineCreationFlags> pipelineCreationFlags, |
| 276 |
uint32_t compilationID) { |
| 277 |
SkSL::NativeShader vsMSL, fsMSL; |
| 278 |
SkSL::Program::Interface vsInterface, fsInterface; |
| 279 |
|
| 280 |
SkSL::ProgramSettings settings; |
| 281 |
settings.fSharpenTextures = true; |
| 282 |
settings.fForceNoRTFlip = true; |
| 283 |
|
| 284 |
SkSL::Compiler skslCompiler; |
| 285 |
ShaderErrorHandler* errorHandler = sharedContext->caps()->shaderErrorHandler(); |
| 286 |
|
| 287 |
const RenderStep* step = sharedContext->rendererProvider()->lookup(pipelineDesc.renderStepID()); |
| 288 |
|
| 289 |
UniquePaintParamsID paintID = pipelineDesc.paintParamsID(); |
| 290 |
|
| 291 |
std::unique_ptr<ShaderInfo> shaderInfo = |
| 292 |
ShaderInfo::Make(sharedContext->caps(), |
| 293 |
sharedContext->shaderCodeDictionary(), |
| 294 |
runtimeDict, |
| 295 |
renderPassDesc, |
| 296 |
step, |
| 297 |
paintID); |
| 298 |
|
| 299 |
const std::string& fsSkSL = shaderInfo->fragmentSkSL(); |
| 300 |
const BlendInfo& blendInfo = shaderInfo->blendInfo(); |
| 301 |
if (!SkSLToMSL(sharedContext->caps()->shaderCaps(), |
| 302 |
fsSkSL, |
| 303 |
SkSL::ProgramKind::kGraphiteFragment, |
| 304 |
settings, |
| 305 |
&fsMSL, |
| 306 |
&fsInterface, |
| 307 |
errorHandler)) { |
| 308 |
return nullptr; |
| 309 |
} |
| 310 |
|
| 311 |
const std::string& vsSkSL = shaderInfo->vertexSkSL(); |
| 312 |
if (!SkSLToMSL(sharedContext->caps()->shaderCaps(), |
| 313 |
vsSkSL, |
| 314 |
SkSL::ProgramKind::kGraphiteVertex, |
| 315 |
settings, |
| 316 |
&vsMSL, |
| 317 |
&vsInterface, |
| 318 |
errorHandler)) { |
| 319 |
return nullptr; |
| 320 |
} |
| 321 |
|
| 322 |
auto vsLibrary = MtlCompileShaderLibrary( |
| 323 |
sharedContext, shaderInfo->vsLabel(), vsMSL.fText, errorHandler); |
| 324 |
auto fsLibrary = MtlCompileShaderLibrary( |
| 325 |
sharedContext, shaderInfo->fsLabel(), fsMSL.fText, errorHandler); |
| 326 |
|
| 327 |
sk_cfp<id<MTLDepthStencilState>> dss = |
| 328 |
sharedContext->getCompatibleDepthStencilState(step->depthStencilSettings()); |
| 329 |
|
| 330 |
PipelineInfo pipelineInfo{ *shaderInfo, pipelineCreationFlags, |
| 331 |
pipelineKey.hash(), compilationID }; |
| 332 |
#if defined(GPU_TEST_UTILS) |
| 333 |
pipelineInfo.fNativeVertexShader = std::move(vsMSL.fText); |
| 334 |
pipelineInfo.fNativeFragmentShader = std::move(fsMSL.fText); |
| 335 |
#endif |
| 336 |
|
| 337 |
return Make(sharedContext, |
| 338 |
shaderInfo->pipelineLabel(), |
| 339 |
pipelineInfo, |
| 340 |
{vsLibrary.get(), "vertexMain"}, |
| 341 |
step->appendsVertices() ? MTLVertexStepFunctionPerVertex : |
| 342 |
MTLVertexStepFunctionPerInstance, |
| 343 |
step->staticAttributes(), |
| 344 |
step->appendAttributes(), |
| 345 |
{fsLibrary.get(), "fragmentMain"}, |
| 346 |
std::move(dss), |
| 347 |
step->depthStencilSettings().fStencilReferenceValue, |
| 348 |
blendInfo, |
| 349 |
renderPassDesc); |
| 350 |
} |
| 351 |
|
| 352 |
sk_sp<MtlGraphicsPipeline> MtlGraphicsPipeline::MakeLoadMSAAPipeline( |
| 353 |
const MtlSharedContext* sharedContext, |
| 354 |
const RenderPassDesc& renderPassDesc) { |
| 355 |
static const char* kLoadMSAAShaderText = |
| 356 |
"#include <metal_stdlib>\n" |
| 357 |
"#include <simd/simd.h>\n" |
| 358 |
"using namespace metal;" |
| 359 |
|
| 360 |
"typedef struct {" |
| 361 |
"float4 position [[position]];" |
| 362 |
"} VertexOutput;" |
| 363 |
|
| 364 |
"vertex VertexOutput vertexMain(uint vertexID [[vertex_id]]) {" |
| 365 |
"VertexOutput out;" |
| 366 |
"float2 position = float2(float(vertexID >> 1), float(vertexID & 1));" |
| 367 |
"out.position = float4(2.0 * position - 1.0, 0.0, 1.0);" |
| 368 |
"return out;" |
| 369 |
"}" |
| 370 |
|
| 371 |
"fragment float4 fragmentMain(VertexOutput in [[stage_in]]," |
| 372 |
"texture2d<half> colorMap [[texture(0)]]) {" |
| 373 |
"uint2 coords = uint2(in.position.x, in.position.y);" |
| 374 |
"half4 colorSample = colorMap.read(coords);" |
| 375 |
"return float4(colorSample);" |
| 376 |
"}"; |
| 377 |
|
| 378 |
auto mtlLibrary = MtlCompileShaderLibrary(sharedContext, |
| 379 |
"LoadMSAAFromResolve", |
| 380 |
kLoadMSAAShaderText, |
| 381 |
sharedContext->caps()->shaderErrorHandler()); |
| 382 |
BlendInfo noBlend{}; // default is equivalent to kSrc blending |
| 383 |
|
| 384 |
static constexpr DepthStencilSettings kIgnoreDSS; |
| 385 |
sk_cfp<id<MTLDepthStencilState>> ignoreDS = |
| 386 |
sharedContext->getCompatibleDepthStencilState(kIgnoreDSS); |
| 387 |
|
| 388 |
std::string pipelineLabel = "LoadMSAAFromResolve + "; |
| 389 |
pipelineLabel += renderPassDesc.toString().c_str(); |
| 390 |
|
| 391 |
PipelineInfo pipelineInfo; |
| 392 |
pipelineInfo.fNumFragTexturesAndSamplers = 1; |
| 393 |
// This is an internal shader, leave off filling out the test-utils shader code |
| 394 |
return Make(sharedContext, |
| 395 |
pipelineLabel, |
| 396 |
pipelineInfo, |
| 397 |
{mtlLibrary.get(), "vertexMain"}, |
| 398 |
/*appendStepFunc=*/{}, |
| 399 |
/*staticAttrs=*/{}, |
| 400 |
/*appendAttrs=*/{}, |
| 401 |
{mtlLibrary.get(), "fragmentMain"}, |
| 402 |
std::move(ignoreDS), |
| 403 |
/*stencilRefValue=*/0, |
| 404 |
noBlend, |
| 405 |
renderPassDesc); |
| 406 |
} |
| 407 |
|
| 408 |
sk_sp<MtlGraphicsPipeline> MtlGraphicsPipeline::Make(const MtlSharedContext* sharedContext, |
| 409 |
const std::string& label, |
| 410 |
const PipelineInfo& pipelineInfo, |
| 411 |
MSLFunction vertexMain, |
| 412 |
MTLVertexStepFunction appendStepFunc, |
| 413 |
SkSpan<const Attribute> staticAttrs, |
| 414 |
SkSpan<const Attribute> appendAttrs, |
| 415 |
MSLFunction fragmentMain, |
| 416 |
sk_cfp<id<MTLDepthStencilState>> dss, |
| 417 |
uint32_t stencilRefValue, |
| 418 |
const BlendInfo& blendInfo, |
| 419 |
const RenderPassDesc& renderPassDesc) { |
| 420 |
id<MTLLibrary> vsLibrary = std::get<0>(vertexMain); |
| 421 |
id<MTLLibrary> fsLibrary = std::get<0>(fragmentMain); |
| 422 |
if (!vsLibrary || !fsLibrary) { |
| 423 |
return nullptr; |
| 424 |
} |
| 425 |
|
| 426 |
sk_cfp<MTLRenderPipelineDescriptor*> psoDescriptor([[MTLRenderPipelineDescriptor alloc] init]); |
| 427 |
|
| 428 |
NSString* labelName = [NSString stringWithUTF8String: label.c_str()]; |
| 429 |
NSString* vsFuncName = [NSString stringWithUTF8String: std::get<1>(vertexMain).c_str()]; |
| 430 |
NSString* fsFuncName = [NSString stringWithUTF8String: std::get<1>(fragmentMain).c_str()]; |
| 431 |
|
| 432 |
(*psoDescriptor).label = labelName; |
| 433 |
(*psoDescriptor).vertexFunction = [vsLibrary newFunctionWithName: vsFuncName]; |
| 434 |
(*psoDescriptor).fragmentFunction = [fsLibrary newFunctionWithName: fsFuncName]; |
| 435 |
|
| 436 |
// TODO: I *think* this gets cleaned up by the pipelineDescriptor? |
| 437 |
(*psoDescriptor).vertexDescriptor = create_vertex_descriptor(appendStepFunc, |
| 438 |
staticAttrs, |
| 439 |
appendAttrs); |
| 440 |
|
| 441 |
TextureFormat colorFormat = renderPassDesc.fColorAttachment.fFormat; |
| 442 |
TextureFormat dsFormat = renderPassDesc.fDepthStencilAttachment.fFormat; |
| 443 |
|
| 444 |
auto mtlColorAttachment = |
| 445 |
create_color_attachment(TextureFormatToMTLPixelFormat(colorFormat), blendInfo); |
| 446 |
(*psoDescriptor).colorAttachments[0] = mtlColorAttachment; |
| 447 |
|
| 448 |
(*psoDescriptor).rasterSampleCount = (uint8_t) renderPassDesc.fColorAttachment.fSampleCount; |
| 449 |
|
| 450 |
if (TextureFormatHasStencil(dsFormat)) { |
| 451 |
(*psoDescriptor).stencilAttachmentPixelFormat = TextureFormatToMTLPixelFormat(dsFormat); |
| 452 |
} else { |
| 453 |
(*psoDescriptor).stencilAttachmentPixelFormat = MTLPixelFormatInvalid; |
| 454 |
} |
| 455 |
if (TextureFormatHasDepth(dsFormat)) { |
| 456 |
(*psoDescriptor).depthAttachmentPixelFormat = TextureFormatToMTLPixelFormat(dsFormat); |
| 457 |
} else { |
| 458 |
(*psoDescriptor).depthAttachmentPixelFormat = MTLPixelFormatInvalid; |
| 459 |
} |
| 460 |
|
| 461 |
NSError* error; |
| 462 |
sk_cfp<id<MTLRenderPipelineState>> pso( |
| 463 |
[sharedContext->device() newRenderPipelineStateWithDescriptor:psoDescriptor.get() |
| 464 |
error:&error]); |
| 465 |
if (!pso) { |
| 466 |
SKGPU_LOG_E("Render pipeline creation failure:\n%s", error.debugDescription.UTF8String); |
| 467 |
return nullptr; |
| 468 |
} |
| 469 |
|
| 470 |
return sk_sp<MtlGraphicsPipeline>(new MtlGraphicsPipeline(sharedContext, |
| 471 |
pipelineInfo, |
| 472 |
label, |
| 473 |
std::move(pso), |
| 474 |
std::move(dss), |
| 475 |
stencilRefValue)); |
| 476 |
} |
| 477 |
|
| 478 |
MtlGraphicsPipeline::MtlGraphicsPipeline(const skgpu::graphite::SharedContext* sharedContext, |
| 479 |
const PipelineInfo& pipelineInfo, |
| 480 |
std::string_view pipelineLabel, |
| 481 |
sk_cfp<id<MTLRenderPipelineState>> pso, |
| 482 |
sk_cfp<id<MTLDepthStencilState>> dss, |
| 483 |
uint32_t refValue) |
| 484 |
: GraphicsPipeline(sharedContext, pipelineInfo, pipelineLabel) |
| 485 |
, fPipelineState(std::move(pso)) |
| 486 |
, fDepthStencilState(std::move(dss)) |
| 487 |
, fStencilReferenceValue(refValue) {} |
| 488 |
|
| 489 |
void MtlGraphicsPipeline::freeGpuData() { |
| 490 |
fPipelineState.reset(); |
| 491 |
} |
| 492 |
|
| 493 |
} // namespace skgpu::graphite |