Welcome to mirror list, hosted at ThFree Co, Russian Federation.

draw_debug.c « intern « draw « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: bd3dcddee9105f13c5917ef07f6e593e2a28202c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * Copyright 2018, Blender Foundation.
 */

/** \file
 * \ingroup draw
 *
 * \brief Simple API to draw debug shapes in the viewport.
 */

#include "MEM_guardedalloc.h"

#include "DNA_object_types.h"

#include "BKE_object.h"

#include "BLI_link_utils.h"

#include "GPU_capabilities.h"
#include "GPU_compute.h"
#include "GPU_immediate.h"
#include "GPU_uniform_buffer.h"

#include "draw_debug.h"
#include "draw_manager.h"
#include "draw_shader.h"

/* --------- Register --------- */

/* Matrix applied to all points before drawing. Could be a stack if needed. */
static float g_modelmat[4][4];

void DRW_debug_modelmat_reset(void)
{
  unit_m4(g_modelmat);
}

void DRW_debug_modelmat(const float modelmat[4][4])
{
  copy_m4_m4(g_modelmat, modelmat);
}

void DRW_debug_line_v3v3(const float v1[3], const float v2[3], const float color[4])
{
  DRWDebugLine *line = MEM_mallocN(sizeof(DRWDebugLine), "DRWDebugLine");
  mul_v3_m4v3(line->pos[0], g_modelmat, v1);
  mul_v3_m4v3(line->pos[1], g_modelmat, v2);
  copy_v4_v4(line->color, color);
  BLI_LINKS_PREPEND(DST.debug.lines, line);
}

void DRW_debug_polygon_v3(const float (*v)[3], const int vert_len, const float color[4])
{
  BLI_assert(vert_len > 1);

  for (int i = 0; i < vert_len; i++) {
    DRW_debug_line_v3v3(v[i], v[(i + 1) % vert_len], color);
  }
}

void DRW_debug_m4(const float m[4][4])
{
  float v0[3] = {0.0f, 0.0f, 0.0f};
  float v1[3] = {1.0f, 0.0f, 0.0f};
  float v2[3] = {0.0f, 1.0f, 0.0f};
  float v3[3] = {0.0f, 0.0f, 1.0f};

  mul_m4_v3(m, v0);
  mul_m4_v3(m, v1);
  mul_m4_v3(m, v2);
  mul_m4_v3(m, v3);

  DRW_debug_line_v3v3(v0, v1, (float[4]){1.0f, 0.0f, 0.0f, 1.0f});
  DRW_debug_line_v3v3(v0, v2, (float[4]){0.0f, 1.0f, 0.0f, 1.0f});
  DRW_debug_line_v3v3(v0, v3, (float[4]){0.0f, 0.0f, 1.0f, 1.0f});
}

void DRW_debug_bbox(const BoundBox *bbox, const float color[4])
{
  DRW_debug_line_v3v3(bbox->vec[0], bbox->vec[1], color);
  DRW_debug_line_v3v3(bbox->vec[1], bbox->vec[2], color);
  DRW_debug_line_v3v3(bbox->vec[2], bbox->vec[3], color);
  DRW_debug_line_v3v3(bbox->vec[3], bbox->vec[0], color);

  DRW_debug_line_v3v3(bbox->vec[4], bbox->vec[5], color);
  DRW_debug_line_v3v3(bbox->vec[5], bbox->vec[6], color);
  DRW_debug_line_v3v3(bbox->vec[6], bbox->vec[7], color);
  DRW_debug_line_v3v3(bbox->vec[7], bbox->vec[4], color);

  DRW_debug_line_v3v3(bbox->vec[0], bbox->vec[4], color);
  DRW_debug_line_v3v3(bbox->vec[1], bbox->vec[5], color);
  DRW_debug_line_v3v3(bbox->vec[2], bbox->vec[6], color);
  DRW_debug_line_v3v3(bbox->vec[3], bbox->vec[7], color);
}

void DRW_debug_m4_as_bbox(const float m[4][4], const float color[4], const bool invert)
{
  BoundBox bb;
  const float min[3] = {-1.0f, -1.0f, -1.0f}, max[3] = {1.0f, 1.0f, 1.0f};
  float project_matrix[4][4];
  if (invert) {
    invert_m4_m4(project_matrix, m);
  }
  else {
    copy_m4_m4(project_matrix, m);
  }

  BKE_boundbox_init_from_minmax(&bb, min, max);
  for (int i = 0; i < 8; i++) {
    mul_project_m4_v3(project_matrix, bb.vec[i]);
  }
  DRW_debug_bbox(&bb, color);
}

void DRW_debug_view(const DRWView *view, const float color[4])
{
  float persinv[4][4], viewinv[4][4];
  DRW_view_persmat_get(view, persinv, true);
  DRW_view_viewmat_get(view, viewinv, true);

  DRW_debug_modelmat_reset();
  DRW_debug_m4_as_bbox(persinv, color, false);
  DRW_debug_m4(viewinv);
}

void DRW_debug_sphere(const float center[3], const float radius, const float color[4])
{
  float size_mat[4][4];
  DRWDebugSphere *sphere = MEM_mallocN(sizeof(DRWDebugSphere), "DRWDebugSphere");
  /* Bake all transform into a Matrix4 */
  scale_m4_fl(size_mat, radius);
  copy_m4_m4(sphere->mat, g_modelmat);
  translate_m4(sphere->mat, center[0], center[1], center[2]);
  mul_m4_m4m4(sphere->mat, sphere->mat, size_mat);

  copy_v4_v4(sphere->color, color);
  BLI_LINKS_PREPEND(DST.debug.spheres, sphere);
}

/* --------- Indirect Rendering --------- */

/* Keep in sync with shaders. */
#define DEBUG_VERT_MAX 16 * 4096
#define DRW_DEBUG_PRINT_MAX 4096

static GPUVertFormat *debug_buf_format(void)
{
  static GPUVertFormat format = {0};
  if (format.attr_len == 0) {
    GPU_vertformat_attr_add(&format, "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
    GPU_vertformat_attr_add(&format, "color", GPU_COMP_U8, 4, GPU_FETCH_INT_TO_FLOAT_UNIT);
  }
  return &format;
}

GPUVertBuf *drw_debug_line_buffer_get()
{
  DRWDebugBuffer *buf = MEM_mallocN(sizeof(DRWDebugBuffer), "DRWDebugBuffer");

  buf->verts = GPU_vertbuf_create_with_format(debug_buf_format());
  GPU_vertbuf_data_alloc(buf->verts, DEBUG_VERT_MAX);
  uint(*data)[4] = GPU_vertbuf_get_data(buf->verts);
  /* Set vertex count to 1 to skip the first 2 degenerate verts.
   * This is because the first one is already being aliased in the shader definition. */
  data[0][3] = 1;
  /* Fill positions to NaN to avoid rendering unused verts. */
  /* TODO(fclem): This could be done on GPU if that becomes a bottleneck. */
  float(*fdata)[4] = (float(*)[4])data;
  for (int v = 0; v < DEBUG_VERT_MAX; v++) {
    for (int i = 0; i < 3; i++) {
      fdata[v][i] = NAN_FLT;
    }
  }
  BLI_LINKS_PREPEND(DST.debug.line_buffers, buf);

  return buf->verts;
}

GPUVertBuf *drw_debug_print_buffer_get()
{
  if (!DST.debug.print_buffer) {
    static GPUVertFormat format = {0};
    if (format.attr_len == 0) {
      GPU_vertformat_attr_add(&format, "char_data", GPU_COMP_U32, 1, GPU_FETCH_INT);
    }

    GPUVertBuf *char_buf = GPU_vertbuf_create_with_format(&format);
    GPU_vertbuf_data_alloc(char_buf, DRW_DEBUG_PRINT_MAX);

    uint *data = GPU_vertbuf_get_data(char_buf);
    memset(data, 0, sizeof(*data) * DRW_DEBUG_PRINT_MAX);

    DST.debug.print_buffer = char_buf;
  }
  return DST.debug.print_buffer;
}

/* --------- Render --------- */

static void drw_debug_draw_lines(void)
{
  int count = BLI_linklist_count((LinkNode *)DST.debug.lines);

  if (count == 0) {
    return;
  }

  GPUVertFormat *vert_format = immVertexFormat();
  uint pos = GPU_vertformat_attr_add(vert_format, "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
  uint col = GPU_vertformat_attr_add(vert_format, "color", GPU_COMP_F32, 4, GPU_FETCH_FLOAT);

  immBindBuiltinProgram(GPU_SHADER_3D_FLAT_COLOR);

  immBegin(GPU_PRIM_LINES, count * 2);

  while (DST.debug.lines) {
    void *next = DST.debug.lines->next;

    immAttr4fv(col, DST.debug.lines->color);
    immVertex3fv(pos, DST.debug.lines->pos[0]);

    immAttr4fv(col, DST.debug.lines->color);
    immVertex3fv(pos, DST.debug.lines->pos[1]);

    MEM_freeN(DST.debug.lines);
    DST.debug.lines = next;
  }
  immEnd();

  immUnbindProgram();
}

static void drw_debug_draw_spheres(void)
{
  int count = BLI_linklist_count((LinkNode *)DST.debug.spheres);

  if (count == 0) {
    return;
  }

  float one = 1.0f;
  GPUVertFormat vert_format = {0};
  uint mat = GPU_vertformat_attr_add(
      &vert_format, "InstanceModelMatrix", GPU_COMP_F32, 16, GPU_FETCH_FLOAT);
  uint col = GPU_vertformat_attr_add(&vert_format, "color", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
  uint siz = GPU_vertformat_attr_add(&vert_format, "size", GPU_COMP_F32, 1, GPU_FETCH_FLOAT);

  GPUVertBuf *inst_vbo = GPU_vertbuf_create_with_format(&vert_format);

  GPU_vertbuf_data_alloc(inst_vbo, count);

  int v = 0;
  while (DST.debug.spheres) {
    void *next = DST.debug.spheres->next;

    GPU_vertbuf_attr_set(inst_vbo, mat, v, DST.debug.spheres->mat[0]);
    GPU_vertbuf_attr_set(inst_vbo, col, v, DST.debug.spheres->color);
    GPU_vertbuf_attr_set(inst_vbo, siz, v, &one);
    v++;

    MEM_freeN(DST.debug.spheres);
    DST.debug.spheres = next;
  }

  GPUBatch *empty_sphere = DRW_cache_empty_sphere_get();

  GPUBatch *draw_batch = GPU_batch_create(GPU_PRIM_LINES, empty_sphere->verts[0], NULL);
  GPU_batch_instbuf_set(draw_batch, inst_vbo, true);
  GPU_batch_program_set_builtin(draw_batch, GPU_SHADER_INSTANCE_VARIYING_COLOR_VARIYING_SIZE);

  float persmat[4][4];
  DRW_view_persmat_get(NULL, persmat, false);
  GPU_batch_uniform_mat4(draw_batch, "ViewProjectionMatrix", persmat);

  GPU_batch_draw(draw_batch);
  GPU_batch_discard(draw_batch);
}

static void drw_debug_draw_buffers(void)
{
  int count = BLI_linklist_count((LinkNode *)DST.debug.line_buffers);

  if (count == 0) {
    return;
  }

  while (DST.debug.line_buffers) {
    void *next = DST.debug.line_buffers->next;

    GPUBatch *batch = GPU_batch_create_ex(
        GPU_PRIM_LINES, DST.debug.line_buffers->verts, NULL, GPU_BATCH_OWNS_VBO);
    GPU_batch_program_set_builtin(batch, GPU_SHADER_3D_FLAT_COLOR);

    float persmat[4][4];
    DRW_view_persmat_get(NULL, persmat, false);
    GPU_batch_uniform_mat4(batch, "ViewProjectionMatrix", persmat);

    GPU_batch_draw(batch);
    GPU_batch_discard(batch);

    MEM_freeN(DST.debug.line_buffers);
    DST.debug.line_buffers = next;
  }
}

static void drw_debug_draw_print(void)
{
  if (!GPU_compute_shader_support() || DST.debug.print_buffer == NULL) {
    return;
  }

  {
    /* Display the characters. */
    GPUUniformBuf *view_ubo = G_draw.view_ubo;

    GPUShader *sh = DRW_shader_debug_print_display_get();
    GPUBatch *batch = GPU_batch_create_ex(
        GPU_PRIM_POINTS, DST.debug.print_buffer, NULL, GPU_BATCH_OWNS_VBO);
    GPU_batch_set_shader(batch, sh);

    eGPUBlend blend = GPU_blend_get();

    GPU_blend(GPU_BLEND_ALPHA_PREMULT);
    GPU_program_point_size(true);
    GPU_uniformbuf_bind(view_ubo, GPU_shader_get_builtin_block(sh, GPU_UNIFORM_BLOCK_DRW_VIEW));

    /* Wait for all writting jobs */
    GPU_memory_barrier(GPU_BARRIER_VERTEX_ATTRIB_ARRAY);

    GPU_batch_draw(batch);
    GPU_batch_discard(batch);
    /* Freed, with the batch. */
    DST.debug.print_buffer = NULL;

    GPU_uniformbuf_unbind(G_draw.view_ubo);
    GPU_program_point_size(false);
    GPU_blend(blend);
  }
}

void drw_debug_draw(void)
{
  drw_debug_draw_lines();
  drw_debug_draw_spheres();
  drw_debug_draw_buffers();
  drw_debug_draw_print();
}

void drw_debug_init(void)
{
  DRW_debug_modelmat_reset();
}