Blender V5.3
mtl_debug.mm
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2022-2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#include "BLI_compiler_attrs.hh"
12#include "BLI_string.hh"
13#include "BLI_system.hh"
14#include "BLI_utildefines.hh"
15
16#include "BKE_global.hh"
17
18#include "GPU_debug.hh"
19#include "GPU_platform.hh"
20
21#include "mtl_context.hh"
22#include "mtl_debug.hh"
23
24#include "CLG_log.h"
25
26#include "gpu_profile_report.hh"
27
28#include <utility>
29
30namespace blender::gpu::debug {
31
32CLG_LogRef LOG = {"gpu.metal"};
33
35{
37}
38
39} // namespace blender::gpu::debug
40
41namespace blender::gpu {
42
43static CLG_LogRef LOG = {"gpu.metal"};
44
45/* -------------------------------------------------------------------- */
51
52void MTLContext::debug_group_begin(const char *name, int /*index*/)
53{
54 /* Note: Debug groups are pushed JIT to the command encoders. This avoids splitting and nesting
55 * the command encoders which would make the debugging experience through Xcode more confusing.
56 * See #unfold_pending_debug_groups(). */
57
58 if (!G.profile_gpu) {
59 return;
60 }
61
62 ScopeTimings timings = {};
63 timings.name = name;
64 timings.finished = false;
65 timings.cpu_start = ScopeTimings::Clock::now();
66
67 scope_timings.append(timings);
68}
69
71{
72 /* Note: Debug groups are pushed JIT to the command encoders. This avoids splitting and nesting
73 * the command encoders which would make the debugging experience through Xcode more confusing.
74 * See #unfold_pending_debug_groups(). */
75
76 if (!G.profile_gpu) {
77 return;
78 }
79
80 for (int i = scope_timings.size() - 1; i >= 0; i--) {
81 ScopeTimings &query = scope_timings[i];
82 if (!query.finished) {
83 query.finished = true;
84 query.cpu_end = ScopeTimings::Clock::now();
85 break;
86 }
87 if (i == 0) {
88 CLOG_ERROR(&debug::LOG, "Profile GPU error: Extra GPU_debug_group_end() call.");
89 }
90 }
91}
92
93MTLContext::ScopeTimings::TimePoint MTLContext::ScopeTimings::epoch =
94 MTLContext::ScopeTimings::Clock::now();
95
96void MTLContext::process_frame_timings()
97{
98 if (!G.profile_gpu) {
99 return;
100 }
101
102 Vector<ScopeTimings> &queries = scope_timings;
103
104 bool frame_is_valid = !queries.is_empty();
105
106 for (int i = queries.size() - 1; i >= 0; i--) {
107 if (!queries[i].finished) {
108 frame_is_valid = false;
109 CLOG_ERROR(&debug::LOG, "Profile GPU error: Missing GPU_debug_group_end() call");
110 }
111 break;
112 }
113
114 if (!frame_is_valid) {
115 return;
116 }
117
118 for (ScopeTimings &query : queries) {
119 ScopeTimings::Nanoseconds begin = query.cpu_start - ScopeTimings::epoch;
120 ScopeTimings::Nanoseconds end = query.cpu_end - ScopeTimings::epoch;
121 ProfileReport::get().add_group_cpu(query.name, begin.count(), end.count());
122 }
123
124 queries.clear();
125}
126
127bool MTLContext::debug_capture_begin(const char * /*title*/)
128{
129 MTLCaptureManager *capture_manager = [MTLCaptureManager sharedCaptureManager];
130 if (!capture_manager) {
131 /* Early exit if frame capture is disabled. */
132 return false;
133 }
134 /* MTLCaptureManager always exists but capture only works when running under Xcode with GPU
135 * Frame Capture enabled, or with MTL_CAPTURE_ENABLED=1. Check before attempting to avoid
136 * a noisy error log in normal command-line runs. */
137 if (![capture_manager supportsDestination:MTLCaptureDestinationDeveloperTools]) {
138 return false;
139 }
140 MTLCaptureDescriptor *capture_descriptor = [[MTLCaptureDescriptor alloc] init];
141 capture_descriptor.captureObject = this->device;
142 NSError *error;
143 if (![capture_manager startCaptureWithDescriptor:capture_descriptor error:&error]) {
145 "Failed to start Metal frame capture, error %s",
146 error ? [[error localizedDescription] UTF8String] : "unknown");
147 return false;
148 }
149 return true;
150}
151
153{
154 MTLCaptureManager *capture_manager = [MTLCaptureManager sharedCaptureManager];
155 if (!capture_manager) {
156 /* Early exit if frame capture is disabled. */
157 return;
158 }
159 [capture_manager stopCapture];
160}
161
163{
164 /* Create a capture scope visible to xCode Metal Frame capture utility. */
165 MTLCaptureManager *capture_manager = [MTLCaptureManager sharedCaptureManager];
166 if (!capture_manager) {
167 /* Early exit if frame capture is disabled. */
168 return nullptr;
169 }
170 id<MTLCaptureScope> capture_scope = [capture_manager newCaptureScopeWithDevice:this->device];
171 capture_scope.label = [NSString stringWithUTF8String:name];
172 [capture_scope retain];
173
174 return reinterpret_cast<void *>(capture_scope);
175}
176
178{
179 if ([((id<MTLCaptureScope>)scope).label isEqual:@(G.gpu_debug_scope_name)]) {
180 debug_capture_begin("Auto Capture");
181 }
182
183 /* Declare opening boundary of scope.
184 * When scope is selected for capture, GPU commands between begin/end scope will be captured. */
185 [(id<MTLCaptureScope>)scope beginScope];
186
187 MTLCaptureManager *capture_manager = [MTLCaptureManager sharedCaptureManager];
188 return [capture_manager isCapturing];
189}
190
192{
193 if ([((id<MTLCaptureScope>)scope).label isEqual:@(G.gpu_debug_scope_name)]) {
195 }
196
197 [(id<MTLCaptureScope>)scope endScope];
198}
199
201
202} // namespace blender::gpu
#define CLOG_ERROR(clg_ref,...)
Definition CLG_log.h:188
#define CLOG_ENSURE(clg_ref)
Definition CLG_log.h:144
int64_t size() const
bool is_empty() const
bool debug_capture_begin(const char *title) override
Definition mtl_debug.mm:127
void * debug_capture_scope_create(const char *name) override
Definition mtl_debug.mm:162
void debug_capture_scope_end(void *scope) override
Definition mtl_debug.mm:191
void debug_group_begin(const char *name, int index) override
Definition mtl_debug.mm:52
bool debug_capture_scope_begin(void *scope) override
Definition mtl_debug.mm:177
void debug_group_end() override
Definition mtl_debug.mm:70
void debug_capture_end() override
Definition mtl_debug.mm:152
static ProfileReport & get()
void add_group_cpu(StringRefNull name, uint64_t cpu_start, uint64_t cpu_end)
static CLG_LogRef LOG
Global G
Definition blender.cc:57
iter begin(iter)