Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
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 101 of file GrMtlPipelineStateBuilder.mm.

101 {
102 switch (type) {
104 return MTLVertexFormatFloat;
106 return MTLVertexFormatFloat2;
108 return MTLVertexFormatFloat3;
110 return MTLVertexFormatFloat4;
112 if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)) {
113 return MTLVertexFormatHalf;
114 } else {
115 return MTLVertexFormatInvalid;
116 }
118 return MTLVertexFormatHalf2;
120 return MTLVertexFormatHalf4;
122 return MTLVertexFormatInt2;
124 return MTLVertexFormatInt3;
126 return MTLVertexFormatInt4;
128 if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)) {
129 return MTLVertexFormatChar;
130 } else {
131 return MTLVertexFormatInvalid;
132 }
134 return MTLVertexFormatChar2;
136 return MTLVertexFormatChar4;
138 if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)) {
139 return MTLVertexFormatUChar;
140 } else {
141 return MTLVertexFormatInvalid;
142 }
144 return MTLVertexFormatUChar2;
146 return MTLVertexFormatUChar4;
148 if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)) {
149 return MTLVertexFormatUCharNormalized;
150 } else {
151 return MTLVertexFormatInvalid;
152 }
154 return MTLVertexFormatUChar4Normalized;
156 return MTLVertexFormatShort2;
158 return MTLVertexFormatShort4;
160 return MTLVertexFormatUShort2;
162 return MTLVertexFormatUShort2Normalized;
164 return MTLVertexFormatInt;
166 return MTLVertexFormatUInt;
168 if (@available(macOS 10.13, iOS 11.0, tvOS 11.0, *)) {
169 return MTLVertexFormatUShortNormalized;
170 } else {
171 return MTLVertexFormatInvalid;
172 }
174 return MTLVertexFormatUShort4Normalized;
175 }
176 SK_ABORT("Unknown vertex attribute type");
177}
@ 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:54
enum flutter::testing::@2643::KeyboardChange::Type type

◆ blend_coeff_to_mtl_blend()

static MTLBlendFactor blend_coeff_to_mtl_blend ( skgpu::BlendCoeff  coeff)
static

Definition at line 263 of file GrMtlPipelineStateBuilder.mm.

263 {
264 switch (coeff) {
266 return MTLBlendFactorZero;
268 return MTLBlendFactorOne;
270 return MTLBlendFactorSourceColor;
272 return MTLBlendFactorOneMinusSourceColor;
274 return MTLBlendFactorDestinationColor;
276 return MTLBlendFactorOneMinusDestinationColor;
278 return MTLBlendFactorSourceAlpha;
280 return MTLBlendFactorOneMinusSourceAlpha;
282 return MTLBlendFactorDestinationAlpha;
284 return MTLBlendFactorOneMinusDestinationAlpha;
286 return MTLBlendFactorBlendColor;
288 return MTLBlendFactorOneMinusBlendColor;
290 if (@available(macOS 10.12, iOS 11.0, tvOS 11.0, *)) {
291 return MTLBlendFactorSource1Color;
292 } else {
293 return MTLBlendFactorZero;
294 }
296 if (@available(macOS 10.12, iOS 11.0, tvOS 11.0, *)) {
297 return MTLBlendFactorOneMinusSource1Color;
298 } else {
299 return MTLBlendFactorZero;
300 }
302 if (@available(macOS 10.12, iOS 11.0, tvOS 11.0, *)) {
303 return MTLBlendFactorSource1Alpha;
304 } else {
305 return MTLBlendFactorZero;
306 }
308 if (@available(macOS 10.12, iOS 11.0, tvOS 11.0, *)) {
309 return MTLBlendFactorOneMinusSource1Alpha;
310 } else {
311 return MTLBlendFactorZero;
312 }
314 return MTLBlendFactorZero;
315 }
316
317 SK_ABORT("Unknown blend coefficient");
318}

◆ blend_equation_to_mtl_blend_op()

static MTLBlendOperation blend_equation_to_mtl_blend_op ( skgpu::BlendEquation  equation)
static

Definition at line 320 of file GrMtlPipelineStateBuilder.mm.

320 {
321 static const MTLBlendOperation gTable[] = {
322 MTLBlendOperationAdd, // skgpu::BlendEquation::kAdd
323 MTLBlendOperationSubtract, // skgpu::BlendEquation::kSubtract
324 MTLBlendOperationReverseSubtract, // skgpu::BlendEquation::kReverseSubtract
325 };
326 static_assert(std::size(gTable) == (int)skgpu::BlendEquation::kFirstAdvanced);
327 static_assert(0 == (int)skgpu::BlendEquation::kAdd);
328 static_assert(1 == (int)skgpu::BlendEquation::kSubtract);
329 static_assert(2 == (int)skgpu::BlendEquation::kReverseSubtract);
330
331 SkASSERT((unsigned)equation < skgpu::kBlendEquationCnt);
332 return gTable[(int)equation];
333}
#define SkASSERT(cond)
Definition: SkAssert.h:100
Type::kYUV Type::kRGBA() int(0.7 *637)
static const int kBlendEquationCnt
Definition: Blend.h:52

◆ buffer_size()

static uint32_t buffer_size ( uint32_t  offset,
uint32_t  maxAlignment 
)
static

Definition at line 385 of file GrMtlPipelineStateBuilder.mm.

385 {
386 // Metal expects the buffer to be padded at the end according to the alignment
387 // of the largest element in the buffer.
388 uint32_t offsetDiff = offset & maxAlignment;
389 if (offsetDiff != 0) {
390 offsetDiff = maxAlignment - offsetDiff + 1;
391 }
392 return offset + offsetDiff;
393}
static SkRect offset(SkRect r, SkIPoint p)
Definition: editor.cpp:18

◆ create_color_attachment()

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

Definition at line 335 of file GrMtlPipelineStateBuilder.mm.

336 {
337 auto mtlColorAttachment = [[MTLRenderPipelineColorAttachmentDescriptor alloc] init];
338
339 // pixel format
340 mtlColorAttachment.pixelFormat = format;
341 if (writer) {
342 writer->writeInt(format);
343 }
344
345 // blending
346 const skgpu::BlendInfo& blendInfo = pipeline.getXferProcessor().getBlendInfo();
347
348 skgpu::BlendEquation equation = blendInfo.fEquation;
349 skgpu::BlendCoeff srcCoeff = blendInfo.fSrcBlend;
350 skgpu::BlendCoeff dstCoeff = blendInfo.fDstBlend;
351 bool blendOn = !skgpu::BlendShouldDisable(equation, srcCoeff, dstCoeff);
352
353 mtlColorAttachment.blendingEnabled = blendOn;
354 if (writer) {
355 writer->writeBool(blendOn);
356 }
357 if (blendOn) {
358 mtlColorAttachment.sourceRGBBlendFactor = blend_coeff_to_mtl_blend(srcCoeff);
359 mtlColorAttachment.destinationRGBBlendFactor = blend_coeff_to_mtl_blend(dstCoeff);
360 mtlColorAttachment.rgbBlendOperation = blend_equation_to_mtl_blend_op(equation);
361 mtlColorAttachment.sourceAlphaBlendFactor = blend_coeff_to_mtl_blend(srcCoeff);
362 mtlColorAttachment.destinationAlphaBlendFactor = blend_coeff_to_mtl_blend(dstCoeff);
363 mtlColorAttachment.alphaBlendOperation = blend_equation_to_mtl_blend_op(equation);
364 if (writer) {
365 writer->writeInt(mtlColorAttachment.sourceRGBBlendFactor);
366 writer->writeInt(mtlColorAttachment.destinationRGBBlendFactor);
367 writer->writeInt(mtlColorAttachment.rgbBlendOperation);
368 writer->writeInt(mtlColorAttachment.sourceAlphaBlendFactor);
369 writer->writeInt(mtlColorAttachment.destinationAlphaBlendFactor);
370 writer->writeInt(mtlColorAttachment.alphaBlendOperation);
371 }
372 }
373
374 if (blendInfo.fWritesColor) {
375 mtlColorAttachment.writeMask = MTLColorWriteMaskAll;
376 } else {
377 mtlColorAttachment.writeMask = MTLColorWriteMaskNone;
378 }
379 if (writer) {
380 writer->writeBool(blendInfo.fWritesColor);
381 }
382 return mtlColorAttachment;
383}
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 constexpr bool BlendShouldDisable(BlendEquation equation, BlendCoeff srcCoeff, BlendCoeff dstCoeff)
Definition: Blend.h:142
BlendEquation
Definition: Blend.h:23
BlendCoeff
Definition: Blend.h:57
def init(device_serial, adb_binary)
Definition: _adb_path.py:12
skgpu::BlendCoeff fDstBlend
Definition: Blend.h:93
bool fWritesColor
Definition: Blend.h:95
skgpu::BlendCoeff fSrcBlend
Definition: Blend.h:92

◆ create_vertex_descriptor()

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

Definition at line 179 of file GrMtlPipelineStateBuilder.mm.

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

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

◆ kSKSL_Tag

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

Definition at line 70 of file GrMtlPipelineStateBuilder.mm.