Blender V5.3
gpu_debug.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2020 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#include "BKE_global.hh"
12
13#include "BLI_string.hh"
14
16
17#include "GPU_debug.hh"
18
19namespace blender {
20
21using namespace blender::gpu;
22
23void GPU_debug_group_begin(const char *name)
24{
25 if (!(G.debug & G_DEBUG_GPU) && !G.profile_gpu) {
26 return;
27 }
28 Context *ctx = Context::get();
29 DebugStack &stack = ctx->debug_stack;
30 stack.append(StringRef(name));
31 ctx->debug_group_begin(name, stack.size());
32}
33
35{
36 if (!(G.debug & G_DEBUG_GPU) && !G.profile_gpu) {
37 return;
38 }
39 Context *ctx = Context::get();
40 ctx->debug_stack.pop_last();
41 ctx->debug_group_end();
42}
43
44void GPU_debug_get_groups_names(int name_buf_maxncpy, char *r_name_buf)
45{
46 Context *ctx = Context::get();
47 if (ctx == nullptr) {
48 return;
49 }
50 DebugStack &stack = ctx->debug_stack;
51 if (stack.is_empty()) {
52 r_name_buf[0] = '\0';
53 return;
54 }
55 size_t len = 0;
56 for (StringRef &name : stack) {
57 len += BLI_snprintf_rlen(r_name_buf + len, name_buf_maxncpy - len, "%s > ", name.data());
58 }
59 r_name_buf[len - 3] = '\0';
60}
61
63{
64 Context *ctx = Context::get();
65 if (ctx == nullptr) {
66 return "";
67 }
68 DebugStack &stack = ctx->debug_stack;
69 if (stack.is_empty()) {
70 return "";
71 }
72 std::string result;
73
74 int i = 0;
75 for (StringRef &name : stack) {
76 if (levels.contains(i++)) {
77 result += name;
78 result += " > ";
79 }
80 }
81 return result.substr(0, result.size() - 3);
82}
83
84bool GPU_debug_group_match(const char *ref)
85{
86 /* Otherwise there will be no names. */
87 BLI_assert(G.debug & G_DEBUG_GPU);
88 Context *ctx = Context::get();
89 if (ctx == nullptr) {
90 return false;
91 }
92 const DebugStack &stack = ctx->debug_stack;
93 for (const StringRef &name : stack) {
94 if (name == ref) {
95 return true;
96 }
97 }
98 return false;
99}
100
101void GPU_debug_capture_begin(const char *title)
102{
103 /* GPU Frame capture is only enabled when --debug-gpu is specified. */
104 if (!(G.debug & G_DEBUG_GPU)) {
105 return;
106 }
107
108 Context *ctx = Context::get();
109 if (ctx && !ctx->debug_is_capturing) {
110 ctx->debug_is_capturing = ctx->debug_capture_begin(title);
111 /* Call GPU_finish to ensure all desired GPU commands occur within the capture boundary. */
112 GPU_finish();
113 }
114}
115
117{
118 /* GPU Frame capture is only enabled when --debug-gpu is specified. */
119 if (!(G.debug & G_DEBUG_GPU)) {
120 return;
121 }
122
123 Context *ctx = Context::get();
124 if (ctx && ctx->debug_is_capturing) {
125 /* Call GPU_finish to ensure all desired GPU commands occur within the capture boundary. */
126 GPU_finish();
127 ctx->debug_capture_end();
128 ctx->debug_is_capturing = false;
129 }
130}
131
132void *GPU_debug_capture_scope_create(const char *name)
133{
134 /* GPU Frame capture is only enabled when --debug-gpu is specified. */
135 if (!(G.debug & G_DEBUG_GPU)) {
136 return nullptr;
137 }
138
139 Context *ctx = Context::get();
140 if (!ctx) {
141 return nullptr;
142 }
143 return ctx->debug_capture_scope_create(name);
144}
145
147{
148 /* Early exit if scope does not exist or not in debug mode. */
149 if (!(G.debug & G_DEBUG_GPU) || !scope) {
150 return false;
151 }
152
153 Context *ctx = Context::get();
154 if (!ctx) {
155 return false;
156 }
157
158 /* Declare beginning of capture scope region. */
159 bool scope_capturing = ctx->debug_capture_scope_begin(scope);
160 if (scope_capturing && !ctx->debug_is_capturing) {
161 /* Call GPU_finish to ensure all desired GPU commands occur within the capture boundary. */
162 GPU_finish();
163 ctx->debug_is_capturing = true;
164 }
165 return ctx->debug_is_capturing;
166}
167
169{
170 /* Early exit if scope does not exist or not in debug mode. */
171 if (!(G.debug & G_DEBUG_GPU) || !scope) {
172 return;
173 }
174
175 Context *ctx = Context::get();
176 if (!ctx) {
177 return;
178 }
179
180 /* If capturing, call GPU_finish to ensure all desired GPU commands occur within the capture
181 * boundary. */
182 if (ctx->debug_is_capturing) {
183 GPU_finish();
184 ctx->debug_is_capturing = false;
185 }
186
187 /* Declare end of capture scope region. */
188 ctx->debug_capture_scope_end(scope);
189}
190
191namespace gpu {
192
194{
195 if (!(G.debug & G_DEBUG_GPU)) {
196 return;
197 }
198
199 const auto &texture_formats_state = Context::get()->state_manager->image_formats;
200 const auto &texture_formats_shader = Context::get()->shader->interface->image_formats_;
201 for (int image_unit = 0; image_unit < GPU_MAX_IMAGE; image_unit++) {
202 TextureWriteFormat format_state = texture_formats_state[image_unit];
203 TextureWriteFormat format_shader = texture_formats_shader[image_unit];
204 if (format_state != TextureWriteFormat::Invalid &&
205 format_shader == TextureWriteFormat::Invalid)
206 {
207 /* It is allowed for an image to be bound in the state manager but to be unused in the
208 * shader. */
209 continue;
210 }
211 if (texture_formats_shader[image_unit] != texture_formats_state[image_unit]) [[unlikely]] {
212 fprintf(
213 stderr,
214 "Error in GPU_debug_validate_binding_image_format: Image format mismatch detected for "
215 "shader '%s' at binding %d (shader format '%s' vs. bound texture format '%s').\n",
216 Context::get()->shader->name_get().c_str(),
217 image_unit,
218 GPU_texture_format_name(to_texture_format(texture_formats_shader[image_unit])),
219 GPU_texture_format_name(to_texture_format(texture_formats_state[image_unit])));
221 }
222 }
223}
224
225} // namespace gpu
226} // namespace blender
#define BLI_assert_unreachable()
constexpr bool contains(int64_t value) const
constexpr StringRef()
virtual void debug_group_begin(const char *, int)
static Context * get()
virtual void debug_capture_scope_end(void *scope)=0
virtual bool debug_capture_scope_begin(void *scope)=0
virtual void * debug_capture_scope_create(const char *name)=0
virtual void debug_capture_end()=0
virtual bool debug_capture_begin(const char *title)=0
std::array< TextureWriteFormat, GPU_MAX_IMAGE > image_formats_
ShaderInterface * interface
std::array< TextureWriteFormat, GPU_MAX_IMAGE > image_formats
#define GPU_MAX_IMAGE
ccl_device_inline dual1 len(const ccl_private dual< T > &a)
Definition math_dual.h:263
Vector< StringRef > DebugStack
void debug_validate_binding_image_format()
Definition gpu_debug.cc:193
constexpr TextureFormat to_texture_format(TextureTargetFormat format)
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void * GPU_debug_capture_scope_create(const char *name)
Definition gpu_debug.cc:132
void GPU_debug_capture_begin(const char *title)
Definition gpu_debug.cc:101
void GPU_debug_capture_end()
Definition gpu_debug.cc:116
size_t BLI_snprintf_rlen(char *__restrict dst, size_t dst_maxncpy, const char *__restrict format,...) ATTR_NONNULL(1
Global G
Definition blender.cc:57
void GPU_debug_group_end()
Definition gpu_debug.cc:34
void GPU_debug_capture_scope_end(void *scope)
Definition gpu_debug.cc:168
bool GPU_debug_group_match(const char *ref)
Definition gpu_debug.cc:84
const char * GPU_texture_format_name(gpu::TextureFormat format)
void GPU_debug_get_groups_names(int name_buf_maxncpy, char *r_name_buf)
Definition gpu_debug.cc:44
void GPU_finish()
Definition gpu_state.cc:289
void GPU_debug_group_begin(const char *name)
Definition gpu_debug.cc:23
bool GPU_debug_capture_scope_begin(void *scope)
Definition gpu_debug.cc:146
int size() const
Definition vector.hh:130