Flutter Engine
The Flutter Engine
GrMtlRenderCommandEncoder.h
Go to the documentation of this file.
1/*
2 * Copyright 2021 Google Inc.
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#ifndef GrMtlRenderCommandEncoder_DEFINED
9#define GrMtlRenderCommandEncoder_DEFINED
10
11#include <memory>
12
18
19class GrMtlSampler;
20
21#import <Metal/Metal.h>
22
23GR_NORETAIN_BEGIN
24
25/**
26 * Wraps a MTLRenderCommandEncoder object and associated tracked state
27 */
29public:
30 static std::unique_ptr<GrMtlRenderCommandEncoder> Make(id<MTLRenderCommandEncoder> encoder) {
31 return std::unique_ptr<GrMtlRenderCommandEncoder>(new GrMtlRenderCommandEncoder(encoder));
32 }
33
34 void setLabel(NSString* label) {
35 [fCommandEncoder setLabel:label];
36 }
37
38 void pushDebugGroup(NSString* string) {
39 [fCommandEncoder pushDebugGroup:string];
40 }
42 [fCommandEncoder popDebugGroup];
43 }
44 void insertDebugSignpost(NSString* string) {
45 [fCommandEncoder insertDebugSignpost:string];
46 }
47
48 void setRenderPipelineState(id<MTLRenderPipelineState> pso) {
49 if (fCurrentRenderPipelineState != pso) {
50 [fCommandEncoder setRenderPipelineState:pso];
51 fCurrentRenderPipelineState = pso;
52 }
53 }
54
55 void setTriangleFillMode(MTLTriangleFillMode fillMode) {
56 if (fCurrentTriangleFillMode != fillMode) {
57 [fCommandEncoder setTriangleFillMode:fillMode];
58 fCurrentTriangleFillMode = fillMode;
59 }
60 }
61
62 void setFrontFacingWinding(MTLWinding winding) {
63 [fCommandEncoder setFrontFacingWinding:winding];
64 }
65
66 void setViewport(const MTLViewport& viewport) {
67 [fCommandEncoder setViewport:viewport];
68 }
69
70 void setVertexBuffer(id<MTLBuffer> buffer, NSUInteger offset, NSUInteger index) {
71 if (@available(macOS 10.11, iOS 8.3, tvOS 9.0, *)) {
72 if (fCurrentVertexBuffer[index] == buffer) {
73 this->setVertexBufferOffset(offset, index);
74 return;
75 }
76 }
77 if (fCurrentVertexBuffer[index] != buffer || fCurrentVertexOffset[index] != offset) {
78 [fCommandEncoder setVertexBuffer:buffer
80 atIndex:index];
81 fCurrentVertexBuffer[index] = buffer;
82 fCurrentVertexOffset[index] = offset;
83 }
84 }
85 void setVertexBufferOffset(NSUInteger offset, NSUInteger index)
86 SK_API_AVAILABLE(macos(10.11), ios(8.3), tvos(9.0)) {
87 if (fCurrentVertexOffset[index] != offset) {
88 [fCommandEncoder setVertexBufferOffset:offset
89 atIndex:index];
90 fCurrentVertexOffset[index] = offset;
91 }
92 }
93
94 void setFragmentBuffer(id<MTLBuffer> buffer, NSUInteger offset, NSUInteger index) {
95 if (@available(macOS 10.11, iOS 8.3, tvOS 9.0, *)) {
96 if (fCurrentFragmentBuffer[index] == buffer) {
97 this->setFragmentBufferOffset(offset, index);
98 return;
99 }
100 }
101 if (fCurrentFragmentBuffer[index] != buffer || fCurrentFragmentOffset[index] != offset) {
102 [fCommandEncoder setFragmentBuffer:buffer
104 atIndex:index];
105 fCurrentFragmentBuffer[index] = buffer;
106 fCurrentFragmentOffset[index] = offset;
107 }
108 }
109 void setFragmentBufferOffset(NSUInteger offset, NSUInteger index)
110 SK_API_AVAILABLE(macos(10.11), ios(8.3), tvos(9.0)) {
111 if (fCurrentFragmentOffset[index] != offset) {
112 [fCommandEncoder setFragmentBufferOffset:offset
113 atIndex:index];
114 fCurrentFragmentOffset[index] = offset;
115 }
116 }
117
118 void setVertexBytes(const void* bytes, NSUInteger length, NSUInteger index)
119 SK_API_AVAILABLE(macos(10.11), ios(8.3), tvos(9.0)) {
120 [fCommandEncoder setVertexBytes:bytes
122 atIndex:index];
123 }
124 void setFragmentBytes(const void* bytes, NSUInteger length, NSUInteger index)
125 SK_API_AVAILABLE(macos(10.11), ios(8.3), tvos(9.0)) {
126 [fCommandEncoder setFragmentBytes:bytes
128 atIndex:index];
129 }
130
131 void setFragmentTexture(id<MTLTexture> texture, NSUInteger index) {
132 SkASSERT(index < kMaxTextures);
133 if (fCurrentTexture[index] != texture) {
134 [fCommandEncoder setFragmentTexture:texture
135 atIndex:index];
136 fCurrentTexture[index] = texture;
137 }
138 }
139 void setFragmentSamplerState(GrMtlSampler* sampler, NSUInteger index) {
140 SkASSERT(index < kMaxSamplers);
141 if (fCurrentSampler[index] != sampler) {
142 [fCommandEncoder setFragmentSamplerState: sampler->mtlSampler()
143 atIndex: index];
144 fCurrentSampler[index] = sampler;
145 }
146 }
147
148 void setBlendColor(SkPMColor4f blendConst) {
149 [fCommandEncoder setBlendColorRed: blendConst.fR
150 green: blendConst.fG
151 blue: blendConst.fB
152 alpha: blendConst.fA];
153 }
154
155 void setStencilFrontBackReferenceValues(uint32_t frontReferenceValue,
156 uint32_t backReferenceValue)
157 SK_API_AVAILABLE(macos(10.11), ios(9.0), tvos(9.0)) {
158 [fCommandEncoder
159 setStencilFrontReferenceValue:frontReferenceValue
160 backReferenceValue:backReferenceValue];
161 }
162 void setStencilReferenceValue(uint32_t referenceValue) {
163 [fCommandEncoder setStencilReferenceValue:referenceValue];
164 }
165 void setDepthStencilState(id<MTLDepthStencilState> depthStencilState) {
166 if (depthStencilState != fCurrentDepthStencilState) {
167 [fCommandEncoder setDepthStencilState:depthStencilState];
168 fCurrentDepthStencilState = depthStencilState;
169 }
170 }
171
172 void setScissorRect(const MTLScissorRect& scissorRect) {
173 if (fCurrentScissorRect.x != scissorRect.x ||
174 fCurrentScissorRect.y != scissorRect.y ||
175 fCurrentScissorRect.width != scissorRect.width ||
176 fCurrentScissorRect.height != scissorRect.height) {
177 [fCommandEncoder setScissorRect:scissorRect];
178 fCurrentScissorRect = scissorRect;
179 }
180 }
181
182 void drawPrimitives(MTLPrimitiveType primitiveType, NSUInteger vertexStart,
183 NSUInteger vertexCount) {
184 [fCommandEncoder drawPrimitives:primitiveType
185 vertexStart:vertexStart
186 vertexCount:vertexCount];
187 }
188 void drawPrimitives(MTLPrimitiveType primitiveType, NSUInteger vertexStart,
189 NSUInteger vertexCount, NSUInteger instanceCount,
190 NSUInteger baseInstance)
191 SK_API_AVAILABLE(macos(10.11), ios(9.0), tvos(9.0)) {
192 [fCommandEncoder drawPrimitives:primitiveType
193 vertexStart:vertexStart
194 vertexCount:vertexCount
195 instanceCount:instanceCount
196 baseInstance:baseInstance];
197 }
198 void drawPrimitives(MTLPrimitiveType primitiveType, id<MTLBuffer> indirectBuffer,
199 NSUInteger indirectBufferOffset)
200 SK_API_AVAILABLE(macos(10.11), ios(9.0), tvos(9.0)) {
201 [fCommandEncoder drawPrimitives:primitiveType
202 indirectBuffer:indirectBuffer
203 indirectBufferOffset:indirectBufferOffset];
204 }
205
206 void drawIndexedPrimitives(MTLPrimitiveType primitiveType, NSUInteger indexCount,
207 MTLIndexType indexType, id<MTLBuffer> indexBuffer,
208 NSUInteger indexBufferOffset) {
209 [fCommandEncoder drawIndexedPrimitives:primitiveType
210 indexCount:indexCount
211 indexType:indexType
212 indexBuffer:indexBuffer
213 indexBufferOffset:indexBufferOffset];
214 }
215 void drawIndexedPrimitives(MTLPrimitiveType primitiveType, NSUInteger indexCount,
216 MTLIndexType indexType, id<MTLBuffer> indexBuffer,
217 NSUInteger indexBufferOffset,
218 NSUInteger instanceCount,
219 NSInteger baseVertex,
220 NSUInteger baseInstance)
221 SK_API_AVAILABLE(macos(10.11), ios(9.0), tvos(9.0)) {
222 [fCommandEncoder drawIndexedPrimitives:primitiveType
223 indexCount:indexCount
224 indexType:indexType
225 indexBuffer:indexBuffer
226 indexBufferOffset:indexBufferOffset
227 instanceCount:instanceCount
228 baseVertex:baseVertex
229 baseInstance:baseInstance];
230 }
231 void drawIndexedPrimitives(MTLPrimitiveType primitiveType,
232 MTLIndexType indexType, id<MTLBuffer> indexBuffer,
233 NSUInteger indexBufferOffset, id<MTLBuffer> indirectBuffer,
234 NSUInteger indirectBufferOffset)
235 SK_API_AVAILABLE(macos(10.11), ios(9.0), tvos(9.0)) {
236 [fCommandEncoder drawIndexedPrimitives:primitiveType
237 indexType:indexType
238 indexBuffer:indexBuffer
239 indexBufferOffset:indexBufferOffset
240 indirectBuffer:indirectBuffer
241 indirectBufferOffset:indirectBufferOffset];
242 }
243
244 void endEncoding() {
245 [fCommandEncoder endEncoding];
246 }
247
248private:
249 GrMtlRenderCommandEncoder(id<MTLRenderCommandEncoder> encoder)
250 : fCommandEncoder(encoder) {}
251
252 id<MTLRenderCommandEncoder> fCommandEncoder = nil;
253
254 // As of 2022-03-09 All GPU families have the same value at:
255 // https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf
256 static const int kMaxSamplers = 16;
257 // This is a self-imposed limit and is less than any GPU family's value at
258 // https://developer.apple.com/metal/Metal-Feature-Set-Tables.pdf
259 // Our implementation uses a 1:1 correspondence between textures and samplers.
260 static const int kMaxTextures = kMaxSamplers;
261
262 __weak id<MTLRenderPipelineState> fCurrentRenderPipelineState = nil;
263 __weak id<MTLDepthStencilState> fCurrentDepthStencilState = nil;
264 __weak id<MTLBuffer> fCurrentVertexBuffer[2 + GrMtlUniformHandler::kUniformBindingCount];
265 NSUInteger fCurrentVertexOffset[2 + GrMtlUniformHandler::kUniformBindingCount];
266 __weak id<MTLBuffer> fCurrentFragmentBuffer[GrMtlUniformHandler::kUniformBindingCount];
267 NSUInteger fCurrentFragmentOffset[2 + GrMtlUniformHandler::kUniformBindingCount];
268 __weak id<MTLTexture> fCurrentTexture[kMaxTextures];
269 GrMtlSampler* fCurrentSampler[kMaxSamplers] = {};
270 MTLScissorRect fCurrentScissorRect = { 0, 0, 0, 0 };
271 MTLTriangleFillMode fCurrentTriangleFillMode = (MTLTriangleFillMode)-1;
272};
273
274GR_NORETAIN_END
275
276#endif
#define SK_API_AVAILABLE(...)
Definition: SkAPI.h:49
#define SkASSERT(cond)
Definition: SkAssert.h:116
void drawPrimitives(MTLPrimitiveType primitiveType, NSUInteger vertexStart, NSUInteger vertexCount)
void setFragmentBufferOffset(NSUInteger offset, NSUInteger index) SK_API_AVAILABLE(macos(10.11)
void drawPrimitives(MTLPrimitiveType primitiveType, id< MTLBuffer > indirectBuffer, NSUInteger indirectBufferOffset) SK_API_AVAILABLE(macos(10.11)
static std::unique_ptr< GrMtlRenderCommandEncoder > Make(id< MTLRenderCommandEncoder > encoder)
void setDepthStencilState(id< MTLDepthStencilState > depthStencilState)
void setVertexBufferOffset(NSUInteger offset, NSUInteger index) SK_API_AVAILABLE(macos(10.11)
void setFragmentBuffer(id< MTLBuffer > buffer, NSUInteger offset, NSUInteger index)
void setBlendColor(SkPMColor4f blendConst)
void pushDebugGroup(NSString *string)
void drawIndexedPrimitives(MTLPrimitiveType primitiveType, NSUInteger indexCount, MTLIndexType indexType, id< MTLBuffer > indexBuffer, NSUInteger indexBufferOffset, NSUInteger instanceCount, NSInteger baseVertex, NSUInteger baseInstance) SK_API_AVAILABLE(macos(10.11)
void setFragmentBytes(const void *bytes, NSUInteger length, NSUInteger index) SK_API_AVAILABLE(macos(10.11)
void insertDebugSignpost(NSString *string)
void setFragmentTexture(id< MTLTexture > texture, NSUInteger index)
void drawIndexedPrimitives(MTLPrimitiveType primitiveType, NSUInteger indexCount, MTLIndexType indexType, id< MTLBuffer > indexBuffer, NSUInteger indexBufferOffset)
void drawIndexedPrimitives(MTLPrimitiveType primitiveType, MTLIndexType indexType, id< MTLBuffer > indexBuffer, NSUInteger indexBufferOffset, id< MTLBuffer > indirectBuffer, NSUInteger indirectBufferOffset) SK_API_AVAILABLE(macos(10.11)
void setVertexBytes(const void *bytes, NSUInteger length, NSUInteger index) SK_API_AVAILABLE(macos(10.11)
void setScissorRect(const MTLScissorRect &scissorRect)
void setFragmentSamplerState(GrMtlSampler *sampler, NSUInteger index)
void setViewport(const MTLViewport &viewport)
void setTriangleFillMode(MTLTriangleFillMode fillMode)
void setRenderPipelineState(id< MTLRenderPipelineState > pso)
void setVertexBuffer(id< MTLBuffer > buffer, NSUInteger offset, NSUInteger index)
void setStencilReferenceValue(uint32_t referenceValue)
void setFrontFacingWinding(MTLWinding winding)
void drawPrimitives(MTLPrimitiveType primitiveType, NSUInteger vertexStart, NSUInteger vertexCount, NSUInteger instanceCount, NSUInteger baseInstance) SK_API_AVAILABLE(macos(10.11)
void setStencilFrontBackReferenceValues(uint32_t frontReferenceValue, uint32_t backReferenceValue) SK_API_AVAILABLE(macos(10.11)
id< MTLSamplerState > mtlSampler() const
Definition: GrMtlSampler.h:26
static constexpr int kUniformBindingCount
size_t length
FlTexture * texture
DEF_SWITCHES_START aot vmservice shared library Name of the *so containing AOT compiled Dart assets for launching the service isolate vm snapshot The VM snapshot data that will be memory mapped as read only SnapshotAssetPath must be present isolate snapshot The isolate snapshot data that will be memory mapped as read only SnapshotAssetPath must be present cache dir Path to the cache directory This is different from the persistent_cache_path in embedder which is used for Skia shader cache icu native lib Path to the library file that exports the ICU data vm service The hostname IP address on which the Dart VM Service should be served If not defaults to or::depending on whether ipv6 is specified vm service A custom Dart VM Service port The default is to pick a randomly available open port disable vm Disable the Dart VM Service The Dart VM Service is never available in release mode disable vm service Disable mDNS Dart VM Service publication Bind to the IPv6 localhost address for the Dart VM Service Ignored if vm service host is set endless trace buffer
Definition: switches.h:126
SeparatedVector2 offset
float fB
blue component
Definition: SkColor.h:265
float fR
red component
Definition: SkColor.h:263
float fG
green component
Definition: SkColor.h:264
float fA
alpha component
Definition: SkColor.h:266