25 return MTLVertexFormatFloat;
27 return MTLVertexFormatFloat2;
29 return MTLVertexFormatFloat3;
31 return MTLVertexFormatFloat4;
33 if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)) {
34 return MTLVertexFormatHalf;
36 return MTLVertexFormatInvalid;
39 return MTLVertexFormatHalf2;
41 return MTLVertexFormatHalf4;
43 return MTLVertexFormatInt2;
45 return MTLVertexFormatInt3;
47 return MTLVertexFormatInt4;
49 if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)) {
50 return MTLVertexFormatChar;
52 return MTLVertexFormatInvalid;
55 return MTLVertexFormatChar2;
57 return MTLVertexFormatChar4;
59 if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)) {
60 return MTLVertexFormatUChar;
62 return MTLVertexFormatInvalid;
65 return MTLVertexFormatUChar2;
67 return MTLVertexFormatUChar4;
69 if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)) {
70 return MTLVertexFormatUCharNormalized;
72 return MTLVertexFormatInvalid;
75 return MTLVertexFormatUChar4Normalized;
77 return MTLVertexFormatShort2;
79 return MTLVertexFormatShort4;
81 return MTLVertexFormatUShort2;
83 return MTLVertexFormatUShort2Normalized;
85 return MTLVertexFormatInt;
87 return MTLVertexFormatUInt;
89 if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)) {
90 return MTLVertexFormatUShortNormalized;
92 return MTLVertexFormatInvalid;
95 return MTLVertexFormatUShort4Normalized;
97 SK_ABORT(
"Unknown vertex attribute type");
102 auto vertexDescriptor = [[MTLVertexDescriptor alloc]
init];
103 int attributeIndex = 0;
105 size_t vertexAttributeOffset = 0;
106 for (
const auto& attribute : vertexAttrs) {
107 MTLVertexAttributeDescriptor* mtlAttribute = vertexDescriptor.attributes[attributeIndex];
110 mtlAttribute.format =
format;
111 mtlAttribute.offset = vertexAttributeOffset;
114 vertexAttributeOffset += attribute.sizeAlign4();
118 if (vertexAttributeOffset) {
119 MTLVertexBufferLayoutDescriptor* vertexBufferLayout =
121 vertexBufferLayout.stepFunction = MTLVertexStepFunctionPerVertex;
122 vertexBufferLayout.stepRate = 1;
123 vertexBufferLayout.stride = vertexAttributeOffset;
126 size_t instanceAttributeOffset = 0;
127 for (
const auto& attribute : instanceAttrs) {
128 MTLVertexAttributeDescriptor* mtlAttribute = vertexDescriptor.attributes[attributeIndex];
131 mtlAttribute.format =
format;
132 mtlAttribute.offset = instanceAttributeOffset;
135 instanceAttributeOffset += attribute.sizeAlign4();
139 if (instanceAttributeOffset) {
140 MTLVertexBufferLayoutDescriptor* instanceBufferLayout =
142 instanceBufferLayout.stepFunction = MTLVertexStepFunctionPerInstance;
143 instanceBufferLayout.stepRate = 1;
144 instanceBufferLayout.stride = instanceAttributeOffset;
146 return vertexDescriptor;
153 return MTLBlendFactorZero;
155 return MTLBlendFactorOne;
157 return MTLBlendFactorSourceColor;
159 return MTLBlendFactorOneMinusSourceColor;
161 return MTLBlendFactorDestinationColor;
163 return MTLBlendFactorOneMinusDestinationColor;
165 return MTLBlendFactorSourceAlpha;
167 return MTLBlendFactorOneMinusSourceAlpha;
169 return MTLBlendFactorDestinationAlpha;
171 return MTLBlendFactorOneMinusDestinationAlpha;
173 return MTLBlendFactorBlendColor;
175 return MTLBlendFactorOneMinusBlendColor;
177 if (@available(macOS 10.12, iOS 11.0, tvOS 11.0, *)) {
178 return MTLBlendFactorSource1Color;
180 return MTLBlendFactorZero;
183 if (@available(macOS 10.12, iOS 11.0, tvOS 11.0, *)) {
184 return MTLBlendFactorOneMinusSource1Color;
186 return MTLBlendFactorZero;
189 if (@available(macOS 10.12, iOS 11.0, tvOS 11.0, *)) {
190 return MTLBlendFactorSource1Alpha;
192 return MTLBlendFactorZero;
195 if (@available(macOS 10.12, iOS 11.0, tvOS 11.0, *)) {
196 return MTLBlendFactorOneMinusSource1Alpha;
198 return MTLBlendFactorZero;
201 return MTLBlendFactorZero;
204 SK_ABORT(
"Unknown blend coefficient");
209 static const MTLBlendOperation gTable[] = {
210 MTLBlendOperationAdd,
211 MTLBlendOperationSubtract,
212 MTLBlendOperationReverseSubtract,
220 return gTable[(
int)equation];
225 const BlendInfo& blendInfo) {
233 auto mtlColorAttachment = [[MTLRenderPipelineColorAttachmentDescriptor alloc]
init];
235 mtlColorAttachment.pixelFormat =
format;
237 mtlColorAttachment.blendingEnabled = blendOn;
248 mtlColorAttachment.writeMask = blendInfo.fWritesColor ? MTLColorWriteMaskAll
249 : MTLColorWriteMaskNone;
251 return mtlColorAttachment;
257 const std::string& label,
262 sk_cfp<id<MTLDepthStencilState>> dss,
263 uint32_t stencilRefValue,
266 PipelineInfo* pipelineInfo) {
267 id<MTLLibrary> vsLibrary = std::get<0>(vertexMain);
268 id<MTLLibrary> fsLibrary = std::get<0>(fragmentMain);
269 if (!vsLibrary || !fsLibrary) {
273 sk_cfp<MTLRenderPipelineDescriptor*> psoDescriptor([[MTLRenderPipelineDescriptor alloc] init]);
275 NSString* labelName = [NSString stringWithUTF8String: label.c_str()];
276 NSString* vsFuncName = [NSString stringWithUTF8String: std::get<1>(vertexMain).c_str()];
277 NSString* fsFuncName = [NSString stringWithUTF8String: std::get<1>(fragmentMain).c_str()];
279 (*psoDescriptor).label = labelName;
280 (*psoDescriptor).vertexFunction = [vsLibrary newFunctionWithName: vsFuncName];
281 (*psoDescriptor).fragmentFunction = [fsLibrary newFunctionWithName: fsFuncName];
290 (*psoDescriptor).colorAttachments[0] = mtlColorAttachment;
292 (*psoDescriptor).rasterSampleCount =
297 MTLPixelFormat depthStencilFormat = (MTLPixelFormat)mtlDSSpec.
fFormat;
299 (*psoDescriptor).stencilAttachmentPixelFormat = depthStencilFormat;
301 (*psoDescriptor).stencilAttachmentPixelFormat = MTLPixelFormatInvalid;
304 (*psoDescriptor).depthAttachmentPixelFormat = depthStencilFormat;
306 (*psoDescriptor).depthAttachmentPixelFormat = MTLPixelFormatInvalid;
310 sk_cfp<id<MTLRenderPipelineState>> pso(
311 [
sharedContext->device() newRenderPipelineStateWithDescriptor:psoDescriptor.get()
314 SKGPU_LOG_E(
"Render pipeline creation failure:\n%s",
error.debugDescription.UTF8String);
326 PipelineInfo* pipelineInfo,
327 sk_cfp<id<MTLRenderPipelineState>> pso,
328 sk_cfp<id<MTLDepthStencilState>> dss,
331 , fPipelineState(
std::move(pso))
332 , fDepthStencilState(
std::move(dss))
333 , fStencilReferenceValue(refValue) {}
336 fPipelineState.reset();
static MTLBlendOperation blend_equation_to_mtl_blend_op(skgpu::BlendEquation equation)
static MTLRenderPipelineColorAttachmentDescriptor * create_color_attachment(MTLPixelFormat format, const GrPipeline &pipeline, SkBinaryWriteBuffer *writer)
static MTLBlendFactor blend_coeff_to_mtl_blend(skgpu::BlendCoeff coeff)
static MTLVertexFormat attribute_type_to_mtlformat(GrVertexAttribType type)
static MTLVertexDescriptor * create_vertex_descriptor(const GrGeometryProcessor &geomProc, SkBinaryWriteBuffer *writer)
#define SKGPU_LOG_E(fmt,...)
#define SK_ABORT(message,...)
Type::kYUV Type::kRGBA() int(0.7 *637)
std::pair< id< MTLLibrary >, std::string > MSLFunction
static constexpr unsigned int kVertexBufferIndex
static constexpr unsigned int kInstanceBufferIndex
static sk_sp< MtlGraphicsPipeline > Make(const MtlSharedContext *, const std::string &label, MSLFunction vertexMain, SkSpan< const Attribute > vertexAttrs, SkSpan< const Attribute > instanceAttrs, MSLFunction fragmentMain, sk_cfp< id< MTLDepthStencilState > >, uint32_t stencilRefValue, const BlendInfo &blendInfo, const RenderPassDesc &, PipelineInfo *pipelineInfo)
void freeGpuData() override
const SharedContext * sharedContext() const
uint32_t numSamples() const
const uint8_t uint32_t uint32_t GError ** error
uint32_t uint32_t * format
static constexpr bool BlendShouldDisable(BlendEquation equation, BlendCoeff srcCoeff, BlendCoeff dstCoeff)
static const int kBlendEquationCnt
bool MtlFormatIsStencil(MTLPixelFormat format)
bool MtlFormatIsDepth(MTLPixelFormat format)
AttachmentDesc fDepthStencilAttachment
AttachmentDesc fColorAttachment