|
5 | 5 | #include "impeller/renderer/backend/metal/compute_pass_mtl.h" |
6 | 6 |
|
7 | 7 | #include <Metal/Metal.h> |
8 | | -#include <algorithm> |
9 | 8 | #include <memory> |
10 | 9 |
|
11 | 10 | #include "flutter/fml/backtrace.h" |
|
58 | 57 | } |
59 | 58 |
|
60 | 59 | void ComputePassMTL::SetCommandLabel(std::string_view label) { |
| 60 | +#ifdef IMPELLER_DEBUG |
61 | 61 | has_label_ = true; |
62 | | - [encoder_ pushDebugGroup:@(label.data())]; |
| 62 | + [encoder_ pushDebugGroup:@(std::string(label).c_str())]; |
| 63 | +#endif // IMPELLER_DEBUG |
63 | 64 | } |
64 | 65 |
|
65 | 66 | // |ComputePass| |
|
124 | 125 | return true; |
125 | 126 | } |
126 | 127 |
|
| 128 | +void ComputePassMTL::PopCommandLabel() { |
| 129 | +#ifdef IMPELLER_DEBUG |
| 130 | + if (has_label_) { |
| 131 | + [encoder_ popDebugGroup]; |
| 132 | + } |
| 133 | + has_label_ = false; |
| 134 | +#endif // IMPELLER_DEBUG |
| 135 | +} |
| 136 | + |
127 | 137 | fml::Status ComputePassMTL::Compute(std::array<uint32_t, 3> workgroup_count) { |
| 138 | + // A dispatch with no workgroups does nothing, like a draw with no vertices. |
128 | 139 | if (workgroup_count[0] == 0u || workgroup_count[1] == 0u || |
129 | 140 | workgroup_count[2] == 0u) { |
130 | | - return fml::Status(fml::StatusCode::kCancelled, |
131 | | - "Invalid workgroup count for compute command."); |
| 141 | + PopCommandLabel(); |
| 142 | + return fml::Status(); |
132 | 143 | } |
133 | 144 |
|
134 | | - const NSUInteger max_total = |
135 | | - pass_bindings_cache_.GetPipeline().maxTotalThreadsPerThreadgroup; |
136 | | - |
137 | | - // Unlike Vulkan and GLES, Metal does not bake the threadgroup size into the |
138 | | - // shader; it is supplied here at dispatch. Honor the shader's declared local |
139 | | - // size. A dimension of 0 means the shader sized it with a specialization |
140 | | - // constant. In that case fill the remaining threadgroup capacity into x while |
141 | | - // honoring any literal y and z, keeping the total within the device maximum. |
142 | | - MTLSize threads_per_threadgroup; |
143 | | - if (workgroup_size_[0] == 0u) { |
144 | | - const NSUInteger size_y = |
145 | | - workgroup_size_[1] == 0u ? 1u : workgroup_size_[1]; |
146 | | - const NSUInteger size_z = |
147 | | - workgroup_size_[2] == 0u ? 1u : workgroup_size_[2]; |
148 | | - const NSUInteger size_x = |
149 | | - std::max<NSUInteger>(1u, max_total / (size_y * size_z)); |
150 | | - threads_per_threadgroup = MTLSizeMake(size_x, size_y, size_z); |
151 | | - } else { |
152 | | - threads_per_threadgroup = MTLSizeMake( |
153 | | - workgroup_size_[0], workgroup_size_[1] == 0u ? 1 : workgroup_size_[1], |
154 | | - workgroup_size_[2] == 0u ? 1 : workgroup_size_[2]); |
155 | | - } |
156 | | - |
157 | | - // Metal aborts at dispatch if the threadgroup size exceeds the device |
158 | | - // maximum. Vulkan rejects an oversized local size at pipeline creation, so do |
159 | | - // the equivalent here and fail gracefully instead. |
160 | | - if (threads_per_threadgroup.width * threads_per_threadgroup.height * |
161 | | - threads_per_threadgroup.depth > |
162 | | - max_total) { |
| 145 | + if (!pass_bindings_cache_.GetPipeline()) { |
| 146 | + PopCommandLabel(); |
163 | 147 | return fml::Status(fml::StatusCode::kCancelled, |
164 | | - "Compute shader workgroup size exceeds the device's " |
165 | | - "maximum threads per threadgroup."); |
| 148 | + "No compute pipeline is bound."); |
166 | 149 | } |
167 | 150 |
|
| 151 | + // Unlike Vulkan and GLES, Metal does not bake the threadgroup size into the |
| 152 | + // shader; it is supplied here at dispatch. Pipeline creation checked the |
| 153 | + // shader's declared size against the device and pipeline limits. |
| 154 | + const MTLSize threads_per_threadgroup = |
| 155 | + MTLSizeMake(workgroup_size_[0], workgroup_size_[1], workgroup_size_[2]); |
| 156 | + |
168 | 157 | [encoder_ |
169 | 158 | dispatchThreadgroups:MTLSizeMake(workgroup_count[0], workgroup_count[1], |
170 | 159 | workgroup_count[2]) |
171 | 160 | threadsPerThreadgroup:threads_per_threadgroup]; |
172 | 161 |
|
173 | | -#ifdef IMPELLER_DEBUG |
174 | | - if (has_label_) { |
175 | | - [encoder_ popDebugGroup]; |
176 | | - } |
177 | | - has_label_ = false; |
178 | | -#endif // IMPELLER_DEBUG |
| 162 | + PopCommandLabel(); |
179 | 163 | return fml::Status(); |
180 | 164 | } |
181 | 165 |
|
|
0 commit comments