Flutter Engine
The Flutter Engine
Functions | Variables
GrMtlPipelineStateBuilder.mm File Reference
#include "src/gpu/ganesh/mtl/GrMtlPipelineStateBuilder.h"
#include "include/gpu/GrDirectContext.h"
#include "src/core/SkReadBuffer.h"
#include "src/core/SkTraceEvent.h"
#include "src/core/SkWriteBuffer.h"
#include "src/gpu/PipelineUtils.h"
#include "src/gpu/ganesh/GrAutoLocaleSetter.h"
#include "src/gpu/ganesh/GrDirectContextPriv.h"
#include "src/gpu/ganesh/GrPersistentCacheUtils.h"
#include "src/gpu/ganesh/GrRenderTarget.h"
#include "src/sksl/SkSLProgramKind.h"
#include "src/sksl/SkSLProgramSettings.h"
#include "src/utils/SkShaderUtils.h"
#include "src/gpu/ganesh/mtl/GrMtlGpu.h"
#include "src/gpu/ganesh/mtl/GrMtlPipelineState.h"
#include "src/gpu/ganesh/mtl/GrMtlUtil.h"
#include "src/gpu/mtl/MtlUtilsPriv.h"
#import <simd/simd.h>

Go to the source code of this file.

Functions

static MTLVertexFormat attribute_type_to_mtlformat (GrVertexAttribType type)
 
static MTLVertexDescriptor * create_vertex_descriptor (const GrGeometryProcessor &geomProc, SkBinaryWriteBuffer *writer)
 
static MTLBlendFactor blend_coeff_to_mtl_blend (skgpu::BlendCoeff coeff)
 
static MTLBlendOperation blend_equation_to_mtl_blend_op (skgpu::BlendEquation equation)
 
static MTLRenderPipelineColorAttachmentDescriptor * create_color_attachment (MTLPixelFormat format, const GrPipeline &pipeline, SkBinaryWriteBuffer *writer)
 
static uint32_t buffer_size (uint32_t offset, uint32_t maxAlignment)
 
static MTLRenderPipelineDescriptor * read_pipeline_data (SkReadBuffer *reader)
 

Variables

static constexpr SkFourByteTag kMSL_Tag = SkSetFourByteTag('M', 'S', 'L', ' ')
 
static constexpr SkFourByteTag kSKSL_Tag = SkSetFourByteTag('S', 'K', 'S', 'L')
 

Function Documentation

◆ attribute_type_to_mtlformat()

static MTLVertexFormat attribute_type_to_mtlformat ( GrVertexAttribType  type)
inlinestatic

Definition at line 98 of file GrMtlPipelineStateBuilder.mm.

98 {
99 switch (type) {
101 return MTLVertexFormatFloat;
103 return MTLVertexFormatFloat2;
105 return MTLVertexFormatFloat3;
107 return MTLVertexFormatFloat4;
109 if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)) {
110 return MTLVertexFormatHalf;
111 } else {
112 return MTLVertexFormatInvalid;
113 }
115 return MTLVertexFormatHalf2;
117 return MTLVertexFormatHalf4;
119 return MTLVertexFormatInt2;
121 return MTLVertexFormatInt3;
123 return MTLVertexFormatInt4;
125 if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)) {
126 return MTLVertexFormatChar;
127 } else {
128 return MTLVertexFormatInvalid;
129 }
131 return MTLVertexFormatChar2;
133 return MTLVertexFormatChar4;
135 if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)) {
136 return MTLVertexFormatUChar;
137 } else {
138 return MTLVertexFormatInvalid;
139 }
141 return MTLVertexFormatUChar2;
143 return MTLVertexFormatUChar4;
145 if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)) {
146 return MTLVertexFormatUCharNormalized;
147 } else {
148 return MTLVertexFormatInvalid;
149 }
151 return MTLVertexFormatUChar4Normalized;
153 return MTLVertexFormatShort2;
155 return MTLVertexFormatShort4;
157 return MTLVertexFormatUShort2;
159 return MTLVertexFormatUShort2Normalized;
161 return MTLVertexFormatInt;
163 return MTLVertexFormatUInt;
165 if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)) {
166 return MTLVertexFormatUShortNormalized;
167 } else {
168 return MTLVertexFormatInvalid;
169 }
171 return MTLVertexFormatUShort4Normalized;
172 }
173 SK_ABORT("Unknown vertex attribute type");
174}
@ kUShort_norm_GrVertexAttribType
Definition: GrTypesPriv.h:346
@ kFloat2_GrVertexAttribType
Definition: GrTypesPriv.h:314
@ kUShort2_GrVertexAttribType
Definition: GrTypesPriv.h:340
@ kUInt_GrVertexAttribType
Definition: GrTypesPriv.h:344
@ kUByte4_norm_GrVertexAttribType
Definition: GrTypesPriv.h:334
@ kUByte_GrVertexAttribType
Definition: GrTypesPriv.h:329
@ kShort2_GrVertexAttribType
Definition: GrTypesPriv.h:337
@ kUShort4_norm_GrVertexAttribType
Definition: GrTypesPriv.h:348
@ kInt_GrVertexAttribType
Definition: GrTypesPriv.h:343
@ kByte_GrVertexAttribType
Definition: GrTypesPriv.h:326
@ kByte4_GrVertexAttribType
Definition: GrTypesPriv.h:328
@ kFloat3_GrVertexAttribType
Definition: GrTypesPriv.h:315
@ kFloat_GrVertexAttribType
Definition: GrTypesPriv.h:313
@ kByte2_GrVertexAttribType
Definition: GrTypesPriv.h:327
@ kFloat4_GrVertexAttribType
Definition: GrTypesPriv.h:316
@ kShort4_GrVertexAttribType
Definition: GrTypesPriv.h:338
@ kUShort2_norm_GrVertexAttribType
Definition: GrTypesPriv.h:341
@ kInt3_GrVertexAttribType
Definition: GrTypesPriv.h:322
@ kHalf2_GrVertexAttribType
Definition: GrTypesPriv.h:318
@ kHalf4_GrVertexAttribType
Definition: GrTypesPriv.h:319
@ kUByte4_GrVertexAttribType
Definition: GrTypesPriv.h:331
@ kUByte2_GrVertexAttribType
Definition: GrTypesPriv.h:330
@ kInt4_GrVertexAttribType
Definition: GrTypesPriv.h:323
@ kUByte_norm_GrVertexAttribType
Definition: GrTypesPriv.h:333
@ kInt2_GrVertexAttribType
Definition: GrTypesPriv.h:321
@ kHalf_GrVertexAttribType
Definition: GrTypesPriv.h:317
#define SK_ABORT(message,...)
Definition: SkAssert.h:70
GLenum type

◆ blend_coeff_to_mtl_blend()

static MTLBlendFactor blend_coeff_to_mtl_blend ( skgpu::BlendCoeff  coeff)
static

Definition at line 260 of file GrMtlPipelineStateBuilder.mm.

260 {
261 switch (coeff) {
263 return MTLBlendFactorZero;
265 return MTLBlendFactorOne;
267 return MTLBlendFactorSourceColor;
269 return MTLBlendFactorOneMinusSourceColor;
271 return MTLBlendFactorDestinationColor;
273 return MTLBlendFactorOneMinusDestinationColor;
275 return MTLBlendFactorSourceAlpha;
277 return MTLBlendFactorOneMinusSourceAlpha;
279 return MTLBlendFactorDestinationAlpha;
281 return MTLBlendFactorOneMinusDestinationAlpha;
283 return MTLBlendFactorBlendColor;
285 return MTLBlendFactorOneMinusBlendColor;
287 if (@available(macOS 10.12, iOS 11.0, tvOS 11.0, *)) {
288 return MTLBlendFactorSource1Color;
289 } else {
290 return MTLBlendFactorZero;
291 }
293 if (@available(macOS 10.12, iOS 11.0, tvOS 11.0, *)) {
294 return MTLBlendFactorOneMinusSource1Color;
295 } else {
296 return MTLBlendFactorZero;
297 }
299 if (@available(macOS 10.12, iOS 11.0, tvOS 11.0, *)) {
300 return MTLBlendFactorSource1Alpha;
301 } else {
302 return MTLBlendFactorZero;
303 }
305 if (@available(macOS 10.12, iOS 11.0, tvOS 11.0, *)) {
306 return MTLBlendFactorOneMinusSource1Alpha;
307 } else {
308 return MTLBlendFactorZero;
309 }
311 return MTLBlendFactorZero;
312 }
313
314 SK_ABORT("Unknown blend coefficient");
315}

◆ blend_equation_to_mtl_blend_op()

static MTLBlendOperation blend_equation_to_mtl_blend_op ( skgpu::BlendEquation  equation)
static

Definition at line 317 of file GrMtlPipelineStateBuilder.mm.

317 {
318 static const MTLBlendOperation gTable[] = {
319 MTLBlendOperationAdd, // skgpu::BlendEquation::kAdd
320 MTLBlendOperationSubtract, // skgpu::BlendEquation::kSubtract
321 MTLBlendOperationReverseSubtract, // skgpu::BlendEquation::kReverseSubtract
322 };
323 static_assert(std::size(gTable) == (int)skgpu::BlendEquation::kFirstAdvanced);
324 static_assert(0 == (int)skgpu::BlendEquation::kAdd);
325 static_assert(1 == (int)skgpu::BlendEquation::kSubtract);
326 static_assert(2 == (int)skgpu::BlendEquation::kReverseSubtract);
327
328 SkASSERT((unsigned)equation < skgpu::kBlendEquationCnt);
329 return gTable[(int)equation];
330}
#define SkASSERT(cond)
Definition: SkAssert.h:116
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition: switches.h:259
static const int kBlendEquationCnt
Definition: Blend.h:55

◆ buffer_size()

static uint32_t buffer_size ( uint32_t  offset,
uint32_t  maxAlignment 
)
static

Definition at line 382 of file GrMtlPipelineStateBuilder.mm.

382 {
383 // Metal expects the buffer to be padded at the end according to the alignment
384 // of the largest element in the buffer.
385 uint32_t offsetDiff = offset & maxAlignment;
386 if (offsetDiff != 0) {
387 offsetDiff = maxAlignment - offsetDiff + 1;
388 }
389 return offset + offsetDiff;
390}
SeparatedVector2 offset

◆ create_color_attachment()

static MTLRenderPipelineColorAttachmentDescriptor * create_color_attachment ( MTLPixelFormat  format,
const GrPipeline pipeline,
SkBinaryWriteBuffer writer 
)
static

Definition at line 332 of file GrMtlPipelineStateBuilder.mm.

333 {
334 auto mtlColorAttachment = [[MTLRenderPipelineColorAttachmentDescriptor alloc] init];
335
336 // pixel format
337 mtlColorAttachment.pixelFormat = format;
338 if (writer) {
339 writer->writeInt(format);
340 }
341
342 // blending
343 const skgpu::BlendInfo& blendInfo = pipeline.getXferProcessor().getBlendInfo();
344
345 skgpu::BlendEquation equation = blendInfo.fEquation;
346 skgpu::BlendCoeff srcCoeff = blendInfo.fSrcBlend;
347 skgpu::BlendCoeff dstCoeff = blendInfo.fDstBlend;
348 bool blendOn = !skgpu::BlendShouldDisable(equation, srcCoeff, dstCoeff);
349
350 mtlColorAttachment.blendingEnabled = blendOn;
351 if (writer) {
352 writer->writeBool(blendOn);
353 }
354 if (blendOn) {
355 mtlColorAttachment.sourceRGBBlendFactor = blend_coeff_to_mtl_blend(srcCoeff);
356 mtlColorAttachment.destinationRGBBlendFactor = blend_coeff_to_mtl_blend(dstCoeff);
357 mtlColorAttachment.rgbBlendOperation = blend_equation_to_mtl_blend_op(equation);
358 mtlColorAttachment.sourceAlphaBlendFactor = blend_coeff_to_mtl_blend(srcCoeff);
359 mtlColorAttachment.destinationAlphaBlendFactor = blend_coeff_to_mtl_blend(dstCoeff);
360 mtlColorAttachment.alphaBlendOperation = blend_equation_to_mtl_blend_op(equation);
361 if (writer) {
362 writer->writeInt(mtlColorAttachment.sourceRGBBlendFactor);
363 writer->writeInt(mtlColorAttachment.destinationRGBBlendFactor);
364 writer->writeInt(mtlColorAttachment.rgbBlendOperation);
365 writer->writeInt(mtlColorAttachment.sourceAlphaBlendFactor);
366 writer->writeInt(mtlColorAttachment.destinationAlphaBlendFactor);
367 writer->writeInt(mtlColorAttachment.alphaBlendOperation);
368 }
369 }
370
371 if (blendInfo.fWritesColor) {
372 mtlColorAttachment.writeMask = MTLColorWriteMaskAll;
373 } else {
374 mtlColorAttachment.writeMask = MTLColorWriteMaskNone;
375 }
376 if (writer) {
377 writer->writeBool(blendInfo.fWritesColor);
378 }
379 return mtlColorAttachment;
380}
static MTLBlendOperation blend_equation_to_mtl_blend_op(skgpu::BlendEquation equation)
static MTLBlendFactor blend_coeff_to_mtl_blend(skgpu::BlendCoeff coeff)
const GrXferProcessor & getXferProcessor() const
Definition: GrPipeline.h:116
skgpu::BlendInfo getBlendInfo() const
void writeBool(bool value) override
void writeInt(int32_t value) override
uint32_t uint32_t * format
static bool init()
static constexpr bool BlendShouldDisable(BlendEquation equation, BlendCoeff srcCoeff, BlendCoeff dstCoeff)
Definition: Blend.h:145
BlendEquation
Definition: Blend.h:26
BlendCoeff
Definition: Blend.h:60
skgpu::BlendCoeff fDstBlend
Definition: Blend.h:96
bool fWritesColor
Definition: Blend.h:98
skgpu::BlendCoeff fSrcBlend
Definition: Blend.h:95

◆ create_vertex_descriptor()

static MTLVertexDescriptor * create_vertex_descriptor ( const GrGeometryProcessor geomProc,
SkBinaryWriteBuffer writer 
)
static

Definition at line 176 of file GrMtlPipelineStateBuilder.mm.

177 {
178 uint32_t vertexBinding = 0, instanceBinding = 0;
179
180 int nextBinding = GrMtlUniformHandler::kLastUniformBinding + 1;
181 if (geomProc.hasVertexAttributes()) {
182 vertexBinding = nextBinding++;
183 }
184
185 if (geomProc.hasInstanceAttributes()) {
186 instanceBinding = nextBinding;
187 }
188 if (writer) {
189 writer->writeUInt(vertexBinding);
190 writer->writeUInt(instanceBinding);
191 }
192
193 auto vertexDescriptor = [[MTLVertexDescriptor alloc] init];
194 int attributeIndex = 0;
195
196 int vertexAttributeCount = geomProc.numVertexAttributes();
197 if (writer) {
198 writer->writeInt(vertexAttributeCount);
199 }
200 for (auto attribute : geomProc.vertexAttributes()) {
201 MTLVertexAttributeDescriptor* mtlAttribute = vertexDescriptor.attributes[attributeIndex];
202 MTLVertexFormat format = attribute_type_to_mtlformat(attribute.cpuType());
203 SkASSERT(MTLVertexFormatInvalid != format);
204 mtlAttribute.format = format;
205 mtlAttribute.offset = *attribute.offset();
206 mtlAttribute.bufferIndex = vertexBinding;
207 if (writer) {
208 writer->writeInt(format);
209 writer->writeUInt(*attribute.offset());
210 writer->writeUInt(vertexBinding);
211 }
212
213 attributeIndex++;
214 }
215
216 if (vertexAttributeCount) {
217 MTLVertexBufferLayoutDescriptor* vertexBufferLayout =
218 vertexDescriptor.layouts[vertexBinding];
219 vertexBufferLayout.stepFunction = MTLVertexStepFunctionPerVertex;
220 vertexBufferLayout.stepRate = 1;
221 vertexBufferLayout.stride = geomProc.vertexStride();
222 if (writer) {
223 writer->writeUInt(geomProc.vertexStride());
224 }
225 }
226
227 int instanceAttributeCount = geomProc.numInstanceAttributes();
228 if (writer) {
229 writer->writeInt(instanceAttributeCount);
230 }
231 for (auto attribute : geomProc.instanceAttributes()) {
232 MTLVertexAttributeDescriptor* mtlAttribute = vertexDescriptor.attributes[attributeIndex];
233 MTLVertexFormat format = attribute_type_to_mtlformat(attribute.cpuType());
234 SkASSERT(MTLVertexFormatInvalid != format);
235 mtlAttribute.format = format;
236 mtlAttribute.offset = *attribute.offset();
237 mtlAttribute.bufferIndex = instanceBinding;
238 if (writer) {
239 writer->writeInt(format);
240 writer->writeUInt(*attribute.offset());
241 writer->writeUInt(instanceBinding);
242 }
243
244 attributeIndex++;
245 }
246
247 if (instanceAttributeCount) {
248 MTLVertexBufferLayoutDescriptor* instanceBufferLayout =
249 vertexDescriptor.layouts[instanceBinding];
250 instanceBufferLayout.stepFunction = MTLVertexStepFunctionPerInstance;
251 instanceBufferLayout.stepRate = 1;
252 instanceBufferLayout.stride = geomProc.instanceStride();
253 if (writer) {
254 writer->writeUInt(geomProc.instanceStride());
255 }
256 }
257 return vertexDescriptor;
258}
static MTLVertexFormat attribute_type_to_mtlformat(GrVertexAttribType type)
int numInstanceAttributes() const
size_t vertexStride() const
bool hasInstanceAttributes() const
const AttributeSet & vertexAttributes() const
const AttributeSet & instanceAttributes() const
size_t instanceStride() const
bool hasVertexAttributes() const
void writeUInt(uint32_t value) override

◆ read_pipeline_data()

static MTLRenderPipelineDescriptor * read_pipeline_data ( SkReadBuffer reader)
static

Definition at line 392 of file GrMtlPipelineStateBuilder.mm.

392 {
393 auto pipelineDescriptor = [[MTLRenderPipelineDescriptor alloc] init];
394
395#ifdef SK_ENABLE_MTL_DEBUG_INFO
396 // set label
397 {
398 SkString description;
399 reader->readString(&description);
400 pipelineDescriptor.label = @(description.c_str());
401 }
402#endif
403
404 // set up vertex descriptor
405 {
406 auto vertexDescriptor = [[MTLVertexDescriptor alloc] init];
407 uint32_t vertexBinding = reader->readUInt();
408 uint32_t instanceBinding = reader->readUInt();
409
410 int attributeIndex = 0;
411
412 // vertex attributes
413 int vertexAttributeCount = reader->readInt();
414 for (int i = 0; i < vertexAttributeCount; ++i) {
415 MTLVertexAttributeDescriptor* mtlAttribute = vertexDescriptor.attributes[attributeIndex];
416 mtlAttribute.format = (MTLVertexFormat) reader->readInt();
417 mtlAttribute.offset = reader->readUInt();
418 mtlAttribute.bufferIndex = reader->readUInt();
419 ++attributeIndex;
420 }
421 if (vertexAttributeCount) {
422 MTLVertexBufferLayoutDescriptor* vertexBufferLayout =
423 vertexDescriptor.layouts[vertexBinding];
424 vertexBufferLayout.stepFunction = MTLVertexStepFunctionPerVertex;
425 vertexBufferLayout.stepRate = 1;
426 vertexBufferLayout.stride = reader->readUInt();
427 }
428
429 // instance attributes
430 int instanceAttributeCount = reader->readInt();
431 for (int i = 0; i < instanceAttributeCount; ++i) {
432 MTLVertexAttributeDescriptor* mtlAttribute = vertexDescriptor.attributes[attributeIndex];
433 mtlAttribute.format = (MTLVertexFormat) reader->readInt();
434 mtlAttribute.offset = reader->readUInt();
435 mtlAttribute.bufferIndex = reader->readUInt();
436 ++attributeIndex;
437 }
438 if (instanceAttributeCount) {
439 MTLVertexBufferLayoutDescriptor* instanceBufferLayout =
440 vertexDescriptor.layouts[instanceBinding];
441 instanceBufferLayout.stepFunction = MTLVertexStepFunctionPerInstance;
442 instanceBufferLayout.stepRate = 1;
443 instanceBufferLayout.stride = reader->readUInt();
444 }
445 pipelineDescriptor.vertexDescriptor = vertexDescriptor;
446 }
447
448 // set up color attachments
449 {
450 auto mtlColorAttachment = [[MTLRenderPipelineColorAttachmentDescriptor alloc] init];
451
452 mtlColorAttachment.pixelFormat = (MTLPixelFormat) reader->readInt();
453 mtlColorAttachment.blendingEnabled = reader->readBool();
454 if (mtlColorAttachment.blendingEnabled) {
455 mtlColorAttachment.sourceRGBBlendFactor = (MTLBlendFactor) reader->readInt();
456 mtlColorAttachment.destinationRGBBlendFactor = (MTLBlendFactor) reader->readInt();
457 mtlColorAttachment.rgbBlendOperation = (MTLBlendOperation) reader->readInt();
458 mtlColorAttachment.sourceAlphaBlendFactor = (MTLBlendFactor) reader->readInt();
459 mtlColorAttachment.destinationAlphaBlendFactor = (MTLBlendFactor) reader->readInt();
460 mtlColorAttachment.alphaBlendOperation = (MTLBlendOperation) reader->readInt();
461 }
462 if (reader->readBool()) {
463 mtlColorAttachment.writeMask = MTLColorWriteMaskAll;
464 } else {
465 mtlColorAttachment.writeMask = MTLColorWriteMaskNone;
466 }
467
468 pipelineDescriptor.colorAttachments[0] = mtlColorAttachment;
469 }
470
471 pipelineDescriptor.stencilAttachmentPixelFormat = (MTLPixelFormat) reader->readInt();
472
473 return pipelineDescriptor;
474}
uint32_t readUInt()
int32_t readInt()
void readString(SkString *string)
const char * c_str() const
Definition: SkString.h:133

Variable Documentation

◆ kMSL_Tag

constexpr SkFourByteTag kMSL_Tag = SkSetFourByteTag('M', 'S', 'L', ' ')
staticconstexpr

Definition at line 65 of file GrMtlPipelineStateBuilder.mm.

◆ kSKSL_Tag

constexpr SkFourByteTag kSKSL_Tag = SkSetFourByteTag('S', 'K', 'S', 'L')
staticconstexpr

Definition at line 66 of file GrMtlPipelineStateBuilder.mm.