Blender V5.3
draw_debug.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2018 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
10
11#include "BKE_object.hh"
12#include "BLI_math_bits.hh"
13#include "BLI_math_matrix.hh"
14#include "BLI_math_matrix_c.hh"
15#include "GPU_batch.hh"
16#include "GPU_capabilities.hh"
17#include "GPU_debug.hh"
18
20#include "draw_debug.hh"
21#include "draw_debug_shared.hh"
22#include "draw_shader.hh"
23#include "draw_shader_shared.hh"
24
25namespace blender::draw {
26
27/* -------------------------------------------------------------------- */
30
32{
33 for (int i = 0; i < 2; i++) {
34 vertex_len_.store(0);
35
36 if (cpu_draw_buf_.current() == nullptr) {
37 cpu_draw_buf_.current() = MEM_new<DebugDrawBuf>("DebugDrawBuf-CPU", "DebugDrawBuf-CPU");
38 gpu_draw_buf_.current() = MEM_new<DebugDrawBuf>("DebugDrawBuf-GPU", "DebugDrawBuf-GPU");
39 }
40
41 cpu_draw_buf_.current()->command.array.vertex_len = 0;
42 cpu_draw_buf_.current()->command.array.vertex_first = 0;
43 cpu_draw_buf_.current()->command.array.instance_len = 1;
44 cpu_draw_buf_.current()->command.array.instance_first = 0;
45
46 gpu_draw_buf_.current()->command.array.vertex_len = 0;
47 gpu_draw_buf_.current()->command.array.vertex_first = 0;
48 gpu_draw_buf_.current()->command.array.instance_len = 1;
49 gpu_draw_buf_.current()->command.array.instance_first = 0;
50 gpu_draw_buf_.current()->push_update();
51
52 cpu_draw_buf_.swap();
53 gpu_draw_buf_.swap();
54 }
55
56 gpu_draw_buf_used = false;
57}
58
60{
61#ifdef WITH_DRAW_DEBUG
62 gpu_draw_buf_used = true;
63 return *gpu_draw_buf_.current();
64#else
65 return nullptr;
66#endif
67}
68
69void DebugDraw::clear_gpu_data()
70{
71 for (int i = 0; i < 2; i++) {
72 MEM_SAFE_DELETE(cpu_draw_buf_.current());
73 MEM_SAFE_DELETE(gpu_draw_buf_.current());
74
75 cpu_draw_buf_.swap();
76 gpu_draw_buf_.swap();
77 }
78}
79
81{
83}
84
86
87/* -------------------------------------------------------------------- */
90
91void drw_debug_line(const float3 v1, const float3 v2, const float4 color, const uint lifetime)
92{
94 dd.draw_line(v1, v2, debug_color_pack(color), lifetime);
95}
96
97void drw_debug_polygon(Span<float3> face_verts, const float4 color, const uint lifetime)
98{
99 BLI_assert(!face_verts.is_empty());
102 float3 v0 = face_verts.last();
103 for (auto vert : face_verts) {
104 float3 v1 = vert;
105 dd.draw_line(v0, v1, col, lifetime);
106 v0 = v1;
107 }
108}
109
110void drw_debug_bbox(const BoundBox &bbox, const float4 color, const uint lifetime)
111{
114 dd.draw_line(bbox.vec[0], bbox.vec[1], col, lifetime);
115 dd.draw_line(bbox.vec[1], bbox.vec[2], col, lifetime);
116 dd.draw_line(bbox.vec[2], bbox.vec[3], col, lifetime);
117 dd.draw_line(bbox.vec[3], bbox.vec[0], col, lifetime);
118
119 dd.draw_line(bbox.vec[4], bbox.vec[5], col, lifetime);
120 dd.draw_line(bbox.vec[5], bbox.vec[6], col, lifetime);
121 dd.draw_line(bbox.vec[6], bbox.vec[7], col, lifetime);
122 dd.draw_line(bbox.vec[7], bbox.vec[4], col, lifetime);
123
124 dd.draw_line(bbox.vec[0], bbox.vec[4], col, lifetime);
125 dd.draw_line(bbox.vec[1], bbox.vec[5], col, lifetime);
126 dd.draw_line(bbox.vec[2], bbox.vec[6], col, lifetime);
127 dd.draw_line(bbox.vec[3], bbox.vec[7], col, lifetime);
128}
129
130static Vector<float3> precompute_sphere_points(int circle_resolution)
131{
133 for (auto axis : IndexRange(3)) {
134 for (auto edge : IndexRange(circle_resolution)) {
135 for (auto vert : IndexRange(2)) {
136 const float angle = (2 * M_PI) * (edge + vert) / float(circle_resolution);
137 const float point[3] = {cosf(angle), sinf(angle), 0.0f};
138 result.append(float3(point[(0 + axis) % 3], point[(1 + axis) % 3], point[(2 + axis) % 3]));
139 }
140 }
141 }
142 return result;
143}
144
145void drw_debug_sphere(const float3 center, float radius, const float4 color, const uint lifetime)
146{
148 static Vector<float3> sphere_verts = precompute_sphere_points(16);
149
152 for (auto i : IndexRange(sphere_verts.size() / 2)) {
153 float3 v0 = sphere_verts[i * 2] * radius + center;
154 float3 v1 = sphere_verts[i * 2 + 1] * radius + center;
155 dd.draw_line(v0, v1, col, lifetime);
156 }
157}
158
159void drw_debug_point(const float3 pos, float rad, const float4 col, const uint lifetime)
160{
161 static Vector<float3> point_verts = precompute_sphere_points(4);
162
165 for (auto i : IndexRange(point_verts.size() / 2)) {
166 float3 v0 = point_verts[i * 2] * rad + pos;
167 float3 v1 = point_verts[i * 2 + 1] * rad + pos;
168 dd.draw_line(v0, v1, color, lifetime);
169 }
170}
171
172void drw_debug_matrix(const float4x4 &m4, const uint lifetime)
173{
174 float3 v0 = math::transform_point(m4, float3(0.0f, 0.0f, 0.0f));
175 float3 v1 = math::transform_point(m4, float3(1.0f, 0.0f, 0.0f));
176 float3 v2 = math::transform_point(m4, float3(0.0f, 1.0f, 0.0f));
177 float3 v3 = math::transform_point(m4, float3(0.0f, 0.0f, 1.0f));
178
180 dd.draw_line(v0, v1, debug_color_pack(float4(1.0f, 0.0f, 0.0f, 1.0f)), lifetime);
181 dd.draw_line(v0, v2, debug_color_pack(float4(0.0f, 1.0f, 0.0f, 1.0f)), lifetime);
182 dd.draw_line(v0, v3, debug_color_pack(float4(0.0f, 0.0f, 1.0f, 1.0f)), lifetime);
183}
184
185void drw_debug_matrix_as_bbox(const float4x4 &mat, const float4 color, const uint lifetime)
186{
187 BoundBox bb;
188 std::array<float3, 8> corners = bounds::corners(Bounds<float3>(float3(-1), float3(1)));
189 for (auto i : IndexRange(8)) {
190 mul_project_m4_v3(mat.ptr(), corners[i]);
191 }
192 drw_debug_bbox(bb, color, lifetime);
193}
194
196
197/* -------------------------------------------------------------------- */
201
203{
205 return;
206 }
207 DebugDrawBuf &buf = *cpu_draw_buf_.current();
208 uint index = vertex_len_.fetch_add(2);
209 if (index + 2 < DRW_DEBUG_DRAW_VERT_MAX) {
210 buf.verts[index / 2] = debug_line_make(float_as_uint(v1.x),
211 float_as_uint(v1.y),
212 float_as_uint(v1.z),
213 float_as_uint(v2.x),
214 float_as_uint(v2.y),
215 float_as_uint(v2.z),
216 color,
217 lifetime);
218 buf.command.array.vertex_len += 2;
219 }
220}
221
223
224/* -------------------------------------------------------------------- */
227
228void DebugDraw::display_lines(View &view)
229{
231 return;
232 }
233 const bool cpu_draw_buf_used = vertex_len_.load() != 0;
234
235 if (!cpu_draw_buf_used && !gpu_draw_buf_used) {
236 return;
237 }
238
240
241 float viewport_size[4];
242 GPU_viewport_size_get_f(viewport_size);
243
244 gpu::Batch *batch = GPU_batch_procedural_lines_get();
245 gpu::Shader *shader = DRW_shader_debug_draw_display_get();
246 GPU_batch_set_shader(batch, shader);
247 GPU_shader_uniform_mat4(shader, "persmat", view.persmat().ptr());
248 GPU_shader_uniform_2f(shader, "size_viewport", viewport_size[2], viewport_size[3]);
249
250 if (gpu_draw_buf_used) {
252 /* Reset buffer. */
253 gpu_draw_buf_.next()->command.array.vertex_len = 0;
254 gpu_draw_buf_.next()->push_update();
255
256 GPU_storagebuf_bind(*gpu_draw_buf_.current(), DRW_DEBUG_DRAW_SLOT);
258 GPU_batch_draw_indirect(batch, *gpu_draw_buf_.current(), 0);
259 GPU_storagebuf_unbind(*gpu_draw_buf_.current());
260 GPU_storagebuf_unbind(*gpu_draw_buf_.next());
262 }
263
264 {
266 /* We might have race condition here (a writer thread might still be outputting vertices).
267 * But that is ok. At worse, we will be missing some vertex data and show 1 corrupted line. */
268 cpu_draw_buf_.current()->command.array.vertex_len = vertex_len_.load();
269 cpu_draw_buf_.current()->push_update();
270 /* Reset buffer. */
271 cpu_draw_buf_.next()->command.array.vertex_len = 0;
272 cpu_draw_buf_.next()->push_update();
273
274 GPU_storagebuf_bind(*cpu_draw_buf_.current(), DRW_DEBUG_DRAW_SLOT);
276 GPU_batch_draw_indirect(batch, *cpu_draw_buf_.current(), 0);
277 GPU_storagebuf_unbind(*cpu_draw_buf_.current());
278 GPU_storagebuf_unbind(*cpu_draw_buf_.next());
279
280 /* Read result of lifetime management. */
281 cpu_draw_buf_.next()->read();
282 vertex_len_.store(
283 min_ii(DRW_DEBUG_DRAW_VERT_MAX, cpu_draw_buf_.next()->command.array.vertex_len));
285 }
286
287 gpu_draw_buf_.swap();
288 cpu_draw_buf_.swap();
289}
290
292{
293 /* Display only on the main thread. Avoid concurrent usage of the resource. */
295
297 return;
298 }
299
300 GPU_debug_group_begin("DebugDraw");
301
302 display_lines(view);
303
305}
306
308
309} // namespace blender::draw
General operations, lookup, etc. for blender objects.
#define M_PI
SIMD_FORCE_INLINE btScalar angle(const btVector3 &v) const
Return the angle between this and another vector.
Definition btVector3.h:356
constexpr IndexRange()=default
constexpr const T & last(const int64_t n=0) const
Definition BLI_span.hh:329
constexpr bool is_empty() const
Definition BLI_span.hh:264
int64_t size() const
gpu::StorageBuf * gpu_draw_buf_get()
Definition draw_debug.cc:59
static DebugDraw & get()
void display_to_view(View &view)
void draw_line(float3 v1, float3 v2, uint color, uint lifetime=1)
const float4x4 persmat(int view_id=0) const
Definition draw_view.hh:161
Simple API to draw debug shapes and log in the viewport.
#define DRW_DEBUG_DRAW_VERT_MAX
#define DRW_DEBUG_DRAW_SLOT
#define DRW_DEBUG_DRAW_FEEDBACK_SLOT
std::array< VecBase< T, 3 >, 8 > corners(const Bounds< VecBase< T, 3 > > &bounds)
void drw_debug_bbox(const BoundBox &bbox, const float4 color, const uint lifetime)
void drw_debug_matrix_as_bbox(const float4x4 &mat, const float4 color, const uint lifetime)
void drw_debug_sphere(const float3 center, float radius, const float4 color, const uint lifetime)
void drw_debug_matrix(const float4x4 &m4, const uint lifetime)
void drw_debug_line(const float3 v1, const float3 v2, const float4 color, const uint lifetime)
Definition draw_debug.cc:91
static Vector< float3 > precompute_sphere_points(int circle_resolution)
void drw_debug_polygon(Span< float3 > face_verts, const float4 color, const uint lifetime)
Definition draw_debug.cc:97
void drw_debug_clear()
Definition draw_debug.cc:80
void drw_debug_point(const float3 pos, float rad, const float4 col, const uint lifetime)
VecBase< T, 3 > transform_point(const CartesianBasis &basis, const VecBase< T, 3 > &v)
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
void GPU_batch_draw_indirect(gpu::Batch *batch, gpu::StorageBuf *indirect_buf, intptr_t offset)
void GPU_shader_uniform_2f(gpu::Shader *sh, const char *name, float x, float y)
unsigned int uint
Definition BKE_text.h:55
void GPU_shader_uniform_mat4(gpu::Shader *sh, const char *name, const float data[4][4])
gpu::Shader * DRW_shader_debug_draw_display_get()
void mul_project_m4_v3(const float mat[4][4], float vec[3])
bool GPU_vertex_pipeline_stores_and_atomics_support()
void GPU_batch_set_shader(gpu::Batch *batch, gpu::Shader *shader, const gpu::shader::SpecializationConstants *constants_state=nullptr)
MINLINE unsigned int float_as_uint(float f)
MatBase< float, 4, 4 > float4x4
VecBase< float, 4 > float4
void GPU_viewport_size_get_f(float coords[4])
Definition gpu_state.cc:258
int BLI_thread_is_main()
Definition threads.cc:184
@ DRW_STATE_WRITE_COLOR
Definition draw_state.hh:32
@ DRW_STATE_WRITE_DEPTH
Definition draw_state.hh:31
@ DRW_STATE_DEPTH_LESS
Definition draw_state.hh:39
void GPU_storagebuf_bind(gpu::StorageBuf *ssbo, int slot)
void GPU_storagebuf_unbind(gpu::StorageBuf *ssbo)
void GPU_debug_group_end()
Definition gpu_debug.cc:34
ATTR_WARN_UNUSED_RESULT const BMVert * v2
uint debug_color_pack(float4 v_color)
VecBase< float, 3 > float3
MINLINE int min_ii(int a, int b)
DRWDebugVertPair debug_line_make(uint in_pos1_x, uint in_pos1_y, uint in_pos1_z, uint in_pos2_x, uint in_pos2_y, uint in_pos2_z, uint in_vert_color, uint in_lifetime)
gpu::Batch * GPU_batch_procedural_lines_get()
Definition gpu_batch.cc:518
void GPU_debug_group_begin(const char *name)
Definition gpu_debug.cc:23
#define sinf
#define cosf
KERNEL_FEATURE_LIGHT_PASSES KERNEL_FEATURE_PATH_GUIDING float
struct DRWDebugVertPair verts[DRW_DEBUG_DRAW_VERT_MAX]
const c_style_mat & ptr() const
static void set(DRWState state=DRW_STATE_DEFAULT)