6 #include <Metal/Metal.h>
11 #include "flutter/fml/closure.h"
12 #include "flutter/fml/logging.h"
13 #include "flutter/fml/trace_event.h"
28 BlitPassMTL::BlitPassMTL(id<MTLCommandBuffer> buffer) : buffer_(buffer) {
32 encoder_ = [buffer_ blitCommandEncoder];
34 is_metal_trace_active_ =
35 [[MTLCaptureManager sharedCaptureManager] isCapturing];
36 #endif // IMPELLER_DEBUG
41 if (!did_finish_encoding_) {
42 [encoder_ endEncoding];
46 bool BlitPassMTL::IsValid()
const {
50 void BlitPassMTL::OnSetLabel(std::string label) {
54 [encoder_ setLabel:@(label.c_str())];
57 bool BlitPassMTL::EncodeCommands(
58 const std::shared_ptr<Allocator>& transients_allocator)
const {
59 [encoder_ endEncoding];
60 did_finish_encoding_ =
true;
65 bool BlitPassMTL::OnCopyTextureToTextureCommand(
66 std::shared_ptr<Texture> source,
67 std::shared_ptr<Texture> destination,
77 if (!destination_mtl) {
81 auto source_origin_mtl =
82 MTLOriginMake(source_region.GetX(), source_region.GetY(), 0);
83 auto source_size_mtl =
84 MTLSizeMake(source_region.GetWidth(), source_region.GetHeight(), 1);
85 auto destination_origin_mtl =
86 MTLOriginMake(destination_origin.x, destination_origin.y, 0);
89 if (is_metal_trace_active_) {
90 [encoder_ pushDebugGroup:@(label.c_str())];
92 #endif // IMPELLER_DEBUG
93 [encoder_ copyFromTexture:source_mtl
96 sourceOrigin:source_origin_mtl
97 sourceSize:source_size_mtl
98 toTexture:destination_mtl
101 destinationOrigin:destination_origin_mtl];
103 #ifdef IMPELLER_DEBUG
104 if (is_metal_trace_active_) {
105 [encoder_ popDebugGroup];
107 #endif // IMPELLER_DEBUG
113 bool BlitPassMTL::OnCopyTextureToBufferCommand(
114 std::shared_ptr<Texture> source,
115 std::shared_ptr<DeviceBuffer> destination,
117 size_t destination_offset,
125 if (!destination_mtl) {
129 auto source_origin_mtl =
130 MTLOriginMake(source_region.GetX(), source_region.GetY(), 0);
131 auto source_size_mtl =
132 MTLSizeMake(source_region.GetWidth(), source_region.GetHeight(), 1);
134 auto destination_bytes_per_pixel =
136 auto destination_bytes_per_row =
137 source_size_mtl.width * destination_bytes_per_pixel;
138 auto destination_bytes_per_image =
139 source_size_mtl.height * destination_bytes_per_row;
141 #ifdef IMPELLER_DEBUG
142 if (is_metal_trace_active_) {
143 [encoder_ pushDebugGroup:@(label.c_str())];
145 #endif // IMPELLER_DEBUG
146 [encoder_ copyFromTexture:source_mtl
149 sourceOrigin:source_origin_mtl
150 sourceSize:source_size_mtl
151 toBuffer:destination_mtl
152 destinationOffset:destination_offset
153 destinationBytesPerRow:destination_bytes_per_row
154 destinationBytesPerImage:destination_bytes_per_image];
156 #ifdef IMPELLER_DEBUG
157 if (is_metal_trace_active_) {
158 [encoder_ popDebugGroup];
160 #endif // IMPELLER_DEBUG
164 bool BlitPassMTL::OnCopyBufferToTextureCommand(
166 std::shared_ptr<Texture> destination,
167 IRect destination_region,
170 bool convert_to_read) {
177 if (!destination_mtl) {
181 auto destination_origin_mtl =
182 MTLOriginMake(destination_region.GetX(), destination_region.GetY(), 0);
183 auto source_size_mtl = MTLSizeMake(destination_region.GetWidth(),
184 destination_region.GetHeight(), 1);
186 auto destination_bytes_per_pixel =
188 auto source_bytes_per_row =
189 destination_region.GetWidth() * destination_bytes_per_pixel;
191 #ifdef IMPELLER_DEBUG
192 if (is_metal_trace_active_) {
193 [encoder_ pushDebugGroup:@(label.c_str())];
195 #endif // IMPELLER_DEBUG
197 copyFromBuffer:source_mtl
198 sourceOffset:source.range.offset
199 sourceBytesPerRow:source_bytes_per_row
203 sourceSize:source_size_mtl
204 toTexture:destination_mtl
205 destinationSlice:slice
207 destinationOrigin:destination_origin_mtl];
209 #ifdef IMPELLER_DEBUG
210 if (is_metal_trace_active_) {
211 [encoder_ popDebugGroup];
213 #endif // IMPELLER_DEBUG
218 bool BlitPassMTL::OnGenerateMipmapCommand(std::shared_ptr<Texture> texture,
220 #ifdef IMPELLER_DEBUG
221 if (is_metal_trace_active_) {
222 [encoder_ pushDebugGroup:@(label.c_str())];
224 #endif // IMPELLER_DEBUG
226 #ifdef IMPELLER_DEBUG
227 if (is_metal_trace_active_) {
228 [encoder_ popDebugGroup];
230 #endif // IMPELLER_DEBUG