Cleanup: add doxy sections to view3d_draw,header
[blender.git] / source / blender / editors / space_view3d / view3d_draw.c
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2008 Blender Foundation.
17  * All rights reserved.
18  */
19
20 /** \file
21  * \ingroup spview3d
22  */
23
24 #include <math.h>
25
26 #include "BLI_listbase.h"
27 #include "BLI_math.h"
28 #include "BLI_rect.h"
29 #include "BLI_string.h"
30 #include "BLI_threads.h"
31 #include "BLI_jitter_2d.h"
32
33
34 #include "BKE_camera.h"
35 #include "BKE_collection.h"
36 #include "BKE_context.h"
37 #include "BKE_global.h"
38 #include "BKE_key.h"
39 #include "BKE_main.h"
40 #include "BKE_scene.h"
41 #include "BKE_object.h"
42 #include "BKE_paint.h"
43 #include "BKE_unit.h"
44
45 #include "BLF_api.h"
46
47 #include "BLT_translation.h"
48
49 #include "DNA_armature_types.h"
50 #include "DNA_brush_types.h"
51 #include "DNA_camera_types.h"
52 #include "DNA_key_types.h"
53 #include "DNA_mesh_types.h"
54 #include "DNA_object_types.h"
55 #include "DNA_view3d_types.h"
56 #include "DNA_windowmanager_types.h"
57
58 #include "DRW_engine.h"
59
60 #include "ED_armature.h"
61 #include "ED_keyframing.h"
62 #include "ED_gpencil.h"
63 #include "ED_screen.h"
64 #include "ED_transform.h"
65
66 #include "DEG_depsgraph_query.h"
67
68 #include "GPU_batch.h"
69 #include "GPU_batch_presets.h"
70 #include "GPU_draw.h"
71 #include "GPU_matrix.h"
72 #include "GPU_immediate.h"
73 #include "GPU_immediate_util.h"
74 #include "GPU_material.h"
75 #include "GPU_viewport.h"
76 #include "GPU_state.h"
77 #include "GPU_framebuffer.h"
78
79 #include "MEM_guardedalloc.h"
80
81 #include "UI_interface.h"
82 #include "UI_resources.h"
83
84 #include "RE_engine.h"
85
86 #include "WM_api.h"
87 #include "WM_types.h"
88
89 #include "RNA_access.h"
90
91 #include "IMB_imbuf.h"
92 #include "IMB_imbuf_types.h"
93
94 #include "view3d_intern.h"  /* own include */
95
96 /* -------------------------------------------------------------------- */
97 /** \name General Functions
98  * \{ */
99
100 /**
101  * \note keep this synced with #ED_view3d_mats_rv3d_backup/#ED_view3d_mats_rv3d_restore
102  */
103 void ED_view3d_update_viewmat(
104         Depsgraph *depsgraph, Scene *scene, View3D *v3d, ARegion *ar,
105         float viewmat[4][4], float winmat[4][4], const rcti *rect)
106 {
107         RegionView3D *rv3d = ar->regiondata;
108
109         /* setup window matrices */
110         if (winmat) {
111                 copy_m4_m4(rv3d->winmat, winmat);
112         }
113         else {
114                 view3d_winmatrix_set(depsgraph, ar, v3d, rect);
115         }
116
117         /* setup view matrix */
118         if (viewmat) {
119                 copy_m4_m4(rv3d->viewmat, viewmat);
120         }
121         else {
122                 float rect_scale[2];
123                 if (rect) {
124                         rect_scale[0] = (float)BLI_rcti_size_x(rect) / (float)ar->winx;
125                         rect_scale[1] = (float)BLI_rcti_size_y(rect) / (float)ar->winy;
126                 }
127                 /* note: calls BKE_object_where_is_calc for camera... */
128                 view3d_viewmatrix_set(depsgraph, scene, v3d, rv3d, rect ? rect_scale : NULL);
129         }
130         /* update utility matrices */
131         mul_m4_m4m4(rv3d->persmat, rv3d->winmat, rv3d->viewmat);
132         invert_m4_m4(rv3d->persinv, rv3d->persmat);
133         invert_m4_m4(rv3d->viewinv, rv3d->viewmat);
134
135         /* calculate GLSL view dependent values */
136
137         /* store window coordinates scaling/offset */
138         if (rv3d->persp == RV3D_CAMOB && v3d->camera) {
139                 rctf cameraborder;
140                 ED_view3d_calc_camera_border(scene, depsgraph, ar, v3d, rv3d, &cameraborder, false);
141                 rv3d->viewcamtexcofac[0] = (float)ar->winx / BLI_rctf_size_x(&cameraborder);
142                 rv3d->viewcamtexcofac[1] = (float)ar->winy / BLI_rctf_size_y(&cameraborder);
143
144                 rv3d->viewcamtexcofac[2] = -rv3d->viewcamtexcofac[0] * cameraborder.xmin / (float)ar->winx;
145                 rv3d->viewcamtexcofac[3] = -rv3d->viewcamtexcofac[1] * cameraborder.ymin / (float)ar->winy;
146         }
147         else {
148                 rv3d->viewcamtexcofac[0] = rv3d->viewcamtexcofac[1] = 1.0f;
149                 rv3d->viewcamtexcofac[2] = rv3d->viewcamtexcofac[3] = 0.0f;
150         }
151
152         /* calculate pixelsize factor once, is used for lights and obcenters */
153         {
154                 /* note:  '1.0f / len_v3(v1)'  replaced  'len_v3(rv3d->viewmat[0])'
155                  * because of float point precision problems at large values [#23908] */
156                 float v1[3], v2[3];
157                 float len_px, len_sc;
158
159                 v1[0] = rv3d->persmat[0][0];
160                 v1[1] = rv3d->persmat[1][0];
161                 v1[2] = rv3d->persmat[2][0];
162
163                 v2[0] = rv3d->persmat[0][1];
164                 v2[1] = rv3d->persmat[1][1];
165                 v2[2] = rv3d->persmat[2][1];
166
167                 len_px = 2.0f / sqrtf(min_ff(len_squared_v3(v1), len_squared_v3(v2)));
168                 len_sc = (float)MAX2(ar->winx, ar->winy);
169
170                 rv3d->pixsize = len_px / len_sc;
171         }
172 }
173
174 static void view3d_main_region_setup_view(
175         Depsgraph *depsgraph, Scene *scene,
176         View3D *v3d, ARegion *ar, float viewmat[4][4], float winmat[4][4], const rcti *rect)
177 {
178         RegionView3D *rv3d = ar->regiondata;
179
180         ED_view3d_update_viewmat(depsgraph, scene, v3d, ar, viewmat, winmat, rect);
181
182         /* set for opengl */
183         GPU_matrix_projection_set(rv3d->winmat);
184         GPU_matrix_set(rv3d->viewmat);
185 }
186
187 static bool view3d_stereo3d_active(wmWindow *win, Scene *scene, View3D *v3d, RegionView3D *rv3d)
188 {
189         if ((scene->r.scemode & R_MULTIVIEW) == 0) {
190                 return false;
191         }
192
193         if ((v3d->camera == NULL) || (v3d->camera->type != OB_CAMERA) || rv3d->persp != RV3D_CAMOB) {
194                 return false;
195         }
196
197         switch (v3d->stereo3d_camera) {
198                 case STEREO_MONO_ID:
199                         return false;
200                         break;
201                 case STEREO_3D_ID:
202                         /* win will be NULL when calling this from the selection or draw loop. */
203                         if ((win == NULL) || (WM_stereo3d_enabled(win, true) == false)) {
204                                 return false;
205                         }
206                         if (((scene->r.views_format & SCE_VIEWS_FORMAT_MULTIVIEW) != 0) &&
207                             !BKE_scene_multiview_is_stereo3d(&scene->r))
208                         {
209                                 return false;
210                         }
211                         break;
212                 /* We always need the stereo calculation for left and right cameras. */
213                 case STEREO_LEFT_ID:
214                 case STEREO_RIGHT_ID:
215                 default:
216                         break;
217         }
218         return true;
219 }
220
221
222 /* setup the view and win matrices for the multiview cameras
223  *
224  * unlike view3d_stereo3d_setup_offscreen, when view3d_stereo3d_setup is called
225  * we have no winmatrix (i.e., projection matrix) defined at that time.
226  * Since the camera and the camera shift are needed for the winmat calculation
227  * we do a small hack to replace it temporarily so we don't need to change the
228  * view3d)main_region_setup_view() code to account for that.
229  */
230 static void view3d_stereo3d_setup(
231         Depsgraph *depsgraph, Scene *scene, View3D *v3d, ARegion *ar, const rcti *rect)
232 {
233         bool is_left;
234         const char *names[2] = { STEREO_LEFT_NAME, STEREO_RIGHT_NAME };
235         const char *viewname;
236
237         /* show only left or right camera */
238         if (v3d->stereo3d_camera != STEREO_3D_ID) {
239                 v3d->multiview_eye = v3d->stereo3d_camera;
240         }
241
242         is_left = v3d->multiview_eye == STEREO_LEFT_ID;
243         viewname = names[is_left ? STEREO_LEFT_ID : STEREO_RIGHT_ID];
244
245         /* update the viewport matrices with the new camera */
246         if (scene->r.views_format == SCE_VIEWS_FORMAT_STEREO_3D) {
247                 Camera *data, *data_eval;
248                 float viewmat[4][4];
249                 float shiftx;
250
251                 data = (Camera *)v3d->camera->data;
252                 data_eval = (Camera *)DEG_get_evaluated_id(depsgraph, &data->id);
253
254                 shiftx = data_eval->shiftx;
255
256                 BLI_thread_lock(LOCK_VIEW3D);
257                 data_eval->shiftx = BKE_camera_multiview_shift_x(&scene->r, v3d->camera, viewname);
258
259                 BKE_camera_multiview_view_matrix(&scene->r, v3d->camera, is_left, viewmat);
260                 view3d_main_region_setup_view(depsgraph, scene, v3d, ar, viewmat, NULL, rect);
261
262                 data_eval->shiftx = shiftx;
263                 BLI_thread_unlock(LOCK_VIEW3D);
264         }
265         else { /* SCE_VIEWS_FORMAT_MULTIVIEW */
266                 float viewmat[4][4];
267                 Object *view_ob = v3d->camera;
268                 Object *camera = BKE_camera_multiview_render(scene, v3d->camera, viewname);
269
270                 BLI_thread_lock(LOCK_VIEW3D);
271                 v3d->camera = camera;
272
273                 BKE_camera_multiview_view_matrix(&scene->r, camera, false, viewmat);
274                 view3d_main_region_setup_view(depsgraph, scene, v3d, ar, viewmat, NULL, rect);
275
276                 v3d->camera = view_ob;
277                 BLI_thread_unlock(LOCK_VIEW3D);
278         }
279 }
280
281 /**
282  * Set the correct matrices
283  */
284 void ED_view3d_draw_setup_view(
285         wmWindow *win, Depsgraph *depsgraph, Scene *scene, ARegion *ar, View3D *v3d,
286         float viewmat[4][4], float winmat[4][4], const rcti *rect)
287 {
288         RegionView3D *rv3d = ar->regiondata;
289
290         /* Setup the view matrix. */
291         if (view3d_stereo3d_active(win, scene, v3d, rv3d)) {
292                 view3d_stereo3d_setup(depsgraph, scene, v3d, ar, rect);
293         }
294         else {
295                 view3d_main_region_setup_view(depsgraph, scene, v3d, ar, viewmat, winmat, rect);
296         }
297 }
298
299 /** \} */
300
301 /* -------------------------------------------------------------------- */
302 /** \name Draw View Border
303  * \{ */
304
305 static void view3d_camera_border(
306         const Scene *scene, struct Depsgraph *depsgraph,
307         const ARegion *ar, const View3D *v3d, const RegionView3D *rv3d,
308         rctf *r_viewborder, const bool no_shift, const bool no_zoom)
309 {
310         CameraParams params;
311         rctf rect_view, rect_camera;
312         Object *camera_eval = DEG_get_evaluated_object(depsgraph, v3d->camera);
313
314         /* get viewport viewplane */
315         BKE_camera_params_init(&params);
316         BKE_camera_params_from_view3d(&params, depsgraph, v3d, rv3d);
317         if (no_zoom) {
318                 params.zoom = 1.0f;
319         }
320         BKE_camera_params_compute_viewplane(&params, ar->winx, ar->winy, 1.0f, 1.0f);
321         rect_view = params.viewplane;
322
323         /* get camera viewplane */
324         BKE_camera_params_init(&params);
325         /* fallback for non camera objects */
326         params.clip_start = v3d->clip_start;
327         params.clip_end = v3d->clip_end;
328         BKE_camera_params_from_object(&params, camera_eval);
329         if (no_shift) {
330                 params.shiftx = 0.0f;
331                 params.shifty = 0.0f;
332         }
333         BKE_camera_params_compute_viewplane(&params, scene->r.xsch, scene->r.ysch, scene->r.xasp, scene->r.yasp);
334         rect_camera = params.viewplane;
335
336         /* get camera border within viewport */
337         r_viewborder->xmin = ((rect_camera.xmin - rect_view.xmin) / BLI_rctf_size_x(&rect_view)) * ar->winx;
338         r_viewborder->xmax = ((rect_camera.xmax - rect_view.xmin) / BLI_rctf_size_x(&rect_view)) * ar->winx;
339         r_viewborder->ymin = ((rect_camera.ymin - rect_view.ymin) / BLI_rctf_size_y(&rect_view)) * ar->winy;
340         r_viewborder->ymax = ((rect_camera.ymax - rect_view.ymin) / BLI_rctf_size_y(&rect_view)) * ar->winy;
341 }
342
343 void ED_view3d_calc_camera_border_size(
344         const Scene *scene, Depsgraph *depsgraph,
345         const ARegion *ar, const View3D *v3d, const RegionView3D *rv3d,
346         float r_size[2])
347 {
348         rctf viewborder;
349
350         view3d_camera_border(scene, depsgraph, ar, v3d, rv3d, &viewborder, true, true);
351         r_size[0] = BLI_rctf_size_x(&viewborder);
352         r_size[1] = BLI_rctf_size_y(&viewborder);
353 }
354
355 void ED_view3d_calc_camera_border(
356         const Scene *scene, Depsgraph *depsgraph,
357         const ARegion *ar, const View3D *v3d, const RegionView3D *rv3d,
358         rctf *r_viewborder, const bool no_shift)
359 {
360         view3d_camera_border(scene, depsgraph, ar, v3d, rv3d, r_viewborder, no_shift, false);
361 }
362
363 static void drawviewborder_grid3(uint shdr_pos, float x1, float x2, float y1, float y2, float fac)
364 {
365         float x3, y3, x4, y4;
366
367         x3 = x1 + fac * (x2 - x1);
368         y3 = y1 + fac * (y2 - y1);
369         x4 = x1 + (1.0f - fac) * (x2 - x1);
370         y4 = y1 + (1.0f - fac) * (y2 - y1);
371
372         immBegin(GPU_PRIM_LINES, 8);
373
374         immVertex2f(shdr_pos, x1, y3);
375         immVertex2f(shdr_pos, x2, y3);
376
377         immVertex2f(shdr_pos, x1, y4);
378         immVertex2f(shdr_pos, x2, y4);
379
380         immVertex2f(shdr_pos, x3, y1);
381         immVertex2f(shdr_pos, x3, y2);
382
383         immVertex2f(shdr_pos, x4, y1);
384         immVertex2f(shdr_pos, x4, y2);
385
386         immEnd();
387 }
388
389 /* harmonious triangle */
390 static void drawviewborder_triangle(
391         uint shdr_pos, float x1, float x2, float y1, float y2, const char golden, const char dir)
392 {
393         float ofs;
394         float w = x2 - x1;
395         float h = y2 - y1;
396
397         immBegin(GPU_PRIM_LINES, 6);
398
399         if (w > h) {
400                 if (golden) {
401                         ofs = w * (1.0f - (1.0f / 1.61803399f));
402                 }
403                 else {
404                         ofs = h * (h / w);
405                 }
406                 if (dir == 'B') {
407                         SWAP(float, y1, y2);
408                 }
409
410                 immVertex2f(shdr_pos, x1, y1);
411                 immVertex2f(shdr_pos, x2, y2);
412
413                 immVertex2f(shdr_pos, x2, y1);
414                 immVertex2f(shdr_pos, x1 + (w - ofs), y2);
415
416                 immVertex2f(shdr_pos, x1, y2);
417                 immVertex2f(shdr_pos, x1 + ofs, y1);
418         }
419         else {
420                 if (golden) {
421                         ofs = h * (1.0f - (1.0f / 1.61803399f));
422                 }
423                 else {
424                         ofs = w * (w / h);
425                 }
426                 if (dir == 'B') {
427                         SWAP(float, x1, x2);
428                 }
429
430                 immVertex2f(shdr_pos, x1, y1);
431                 immVertex2f(shdr_pos, x2, y2);
432
433                 immVertex2f(shdr_pos, x2, y1);
434                 immVertex2f(shdr_pos, x1, y1 + ofs);
435
436                 immVertex2f(shdr_pos, x1, y2);
437                 immVertex2f(shdr_pos, x2, y1 + (h - ofs));
438         }
439
440         immEnd();
441 }
442
443 static void drawviewborder(Scene *scene, Depsgraph *depsgraph, ARegion *ar, View3D *v3d)
444 {
445         float x1, x2, y1, y2;
446         float x1i, x2i, y1i, y2i;
447
448         rctf viewborder;
449         Camera *ca = NULL;
450         RegionView3D *rv3d = ar->regiondata;
451
452         if (v3d->camera == NULL) {
453                 return;
454         }
455         if (v3d->camera->type == OB_CAMERA) {
456                 ca = v3d->camera->data;
457         }
458
459         ED_view3d_calc_camera_border(scene, depsgraph, ar, v3d, rv3d, &viewborder, false);
460         /* the offsets */
461         x1 = viewborder.xmin;
462         y1 = viewborder.ymin;
463         x2 = viewborder.xmax;
464         y2 = viewborder.ymax;
465
466         GPU_line_width(1.0f);
467
468         /* apply offsets so the real 3D camera shows through */
469
470         /* note: quite un-scientific but without this bit extra
471          * 0.0001 on the lower left the 2D border sometimes
472          * obscures the 3D camera border */
473         /* note: with VIEW3D_CAMERA_BORDER_HACK defined this error isn't noticeable
474          * but keep it here in case we need to remove the workaround */
475         x1i = (int)(x1 - 1.0001f);
476         y1i = (int)(y1 - 1.0001f);
477         x2i = (int)(x2 + (1.0f - 0.0001f));
478         y2i = (int)(y2 + (1.0f - 0.0001f));
479
480         uint shdr_pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
481
482         /* First, solid lines. */
483         {
484                 immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
485
486                 /* passepartout, specified in camera edit buttons */
487                 if (ca && (ca->flag & CAM_SHOWPASSEPARTOUT) && ca->passepartalpha > 0.000001f) {
488                         const float winx = (ar->winx + 1);
489                         const float winy = (ar->winy + 1);
490
491                         float alpha = 1.0f;
492
493                         if (ca->passepartalpha != 1.0f) {
494                                 GPU_blend_set_func_separate(GPU_SRC_ALPHA, GPU_ONE_MINUS_SRC_ALPHA, GPU_ONE, GPU_ONE_MINUS_SRC_ALPHA);
495                                 GPU_blend(true);
496                                 alpha = ca->passepartalpha;
497                         }
498
499                         immUniformColor4f(0.0f, 0.0f, 0.0f, alpha);
500
501                         if (x1i > 0.0f) {
502                                 immRectf(shdr_pos, 0.0f, winy, x1i, 0.0f);
503                         }
504                         if (x2i < winx) {
505                                 immRectf(shdr_pos, x2i, winy, winx, 0.0f);
506                         }
507                         if (y2i < winy) {
508                                 immRectf(shdr_pos, x1i, winy, x2i, y2i);
509                         }
510                         if (y2i > 0.0f) {
511                                 immRectf(shdr_pos, x1i, y1i, x2i, 0.0f);
512                         }
513
514                         GPU_blend(false);
515                 }
516
517                 immUniformThemeColor(TH_BACK);
518                 imm_draw_box_wire_2d(shdr_pos, x1i, y1i, x2i, y2i);
519
520 #ifdef VIEW3D_CAMERA_BORDER_HACK
521                 if (view3d_camera_border_hack_test == true) {
522                         immUniformColor3ubv(view3d_camera_border_hack_col);
523                         imm_draw_box_wire_2d(shdr_pos, x1i + 1, y1i + 1, x2i - 1, y2i - 1);
524                         view3d_camera_border_hack_test = false;
525                 }
526 #endif
527
528                 immUnbindProgram();
529         }
530
531         /* When overlays are disabled, only show camera outline & passepartout. */
532         if (v3d->flag2 & V3D_HIDE_OVERLAYS) {
533                 return;
534         }
535
536         /* And now, the dashed lines! */
537         immBindBuiltinProgram(GPU_SHADER_2D_LINE_DASHED_UNIFORM_COLOR);
538
539         {
540                 float viewport_size[4];
541                 GPU_viewport_size_get_f(viewport_size);
542                 immUniform2f("viewport_size", viewport_size[2], viewport_size[3]);
543
544                 immUniform1i("colors_len", 0);  /* "simple" mode */
545                 immUniform1f("dash_width", 6.0f);
546                 immUniform1f("dash_factor", 0.5f);
547
548                 /* outer line not to confuse with object selection */
549                 if (v3d->flag2 & V3D_LOCK_CAMERA) {
550                         immUniformThemeColor(TH_REDALERT);
551                         imm_draw_box_wire_2d(shdr_pos, x1i - 1, y1i - 1, x2i + 1, y2i + 1);
552                 }
553
554                 immUniformThemeColor(TH_VIEW_OVERLAY);
555                 imm_draw_box_wire_2d(shdr_pos, x1i, y1i, x2i, y2i);
556         }
557
558         /* Render Border. */
559         if (scene->r.mode & R_BORDER) {
560                 float x3, y3, x4, y4;
561
562                 x3 = floorf(x1 + (scene->r.border.xmin * (x2 - x1))) - 1;
563                 y3 = floorf(y1 + (scene->r.border.ymin * (y2 - y1))) - 1;
564                 x4 = floorf(x1 + (scene->r.border.xmax * (x2 - x1))) + (U.pixelsize - 1);
565                 y4 = floorf(y1 + (scene->r.border.ymax * (y2 - y1))) + (U.pixelsize - 1);
566
567                 immUniformColor3f(1.0f, 0.25f, 0.25f);
568                 imm_draw_box_wire_2d(shdr_pos, x3, y3, x4, y4);
569         }
570
571         /* safety border */
572         if (ca) {
573                 immUniformThemeColorBlend(TH_VIEW_OVERLAY, TH_BACK, 0.25f);
574
575                 if (ca->dtx & CAM_DTX_CENTER) {
576                         float x3, y3;
577
578                         x3 = x1 + 0.5f * (x2 - x1);
579                         y3 = y1 + 0.5f * (y2 - y1);
580
581                         immBegin(GPU_PRIM_LINES, 4);
582
583                         immVertex2f(shdr_pos, x1, y3);
584                         immVertex2f(shdr_pos, x2, y3);
585
586                         immVertex2f(shdr_pos, x3, y1);
587                         immVertex2f(shdr_pos, x3, y2);
588
589                         immEnd();
590                 }
591
592                 if (ca->dtx & CAM_DTX_CENTER_DIAG) {
593                         immBegin(GPU_PRIM_LINES, 4);
594
595                         immVertex2f(shdr_pos, x1, y1);
596                         immVertex2f(shdr_pos, x2, y2);
597
598                         immVertex2f(shdr_pos, x1, y2);
599                         immVertex2f(shdr_pos, x2, y1);
600
601                         immEnd();
602                 }
603
604                 if (ca->dtx & CAM_DTX_THIRDS) {
605                         drawviewborder_grid3(shdr_pos, x1, x2, y1, y2, 1.0f / 3.0f);
606                 }
607
608                 if (ca->dtx & CAM_DTX_GOLDEN) {
609                         drawviewborder_grid3(shdr_pos, x1, x2, y1, y2, 1.0f - (1.0f / 1.61803399f));
610                 }
611
612                 if (ca->dtx & CAM_DTX_GOLDEN_TRI_A) {
613                         drawviewborder_triangle(shdr_pos, x1, x2, y1, y2, 0, 'A');
614                 }
615
616                 if (ca->dtx & CAM_DTX_GOLDEN_TRI_B) {
617                         drawviewborder_triangle(shdr_pos, x1, x2, y1, y2, 0, 'B');
618                 }
619
620                 if (ca->dtx & CAM_DTX_HARMONY_TRI_A) {
621                         drawviewborder_triangle(shdr_pos, x1, x2, y1, y2, 1, 'A');
622                 }
623
624                 if (ca->dtx & CAM_DTX_HARMONY_TRI_B) {
625                         drawviewborder_triangle(shdr_pos, x1, x2, y1, y2, 1, 'B');
626                 }
627
628                 if (ca->flag & CAM_SHOW_SAFE_MARGINS) {
629                         UI_draw_safe_areas(
630                                 shdr_pos, x1, x2, y1, y2,
631                                 scene->safe_areas.title, scene->safe_areas.action);
632
633                         if (ca->flag & CAM_SHOW_SAFE_CENTER) {
634                                 UI_draw_safe_areas(
635                                         shdr_pos, x1, x2, y1, y2,
636                                         scene->safe_areas.title_center, scene->safe_areas.action_center);
637                         }
638                 }
639
640                 if (ca->flag & CAM_SHOWSENSOR) {
641                         /* determine sensor fit, and get sensor x/y, for auto fit we
642                          * assume and square sensor and only use sensor_x */
643                         float sizex = scene->r.xsch * scene->r.xasp;
644                         float sizey = scene->r.ysch * scene->r.yasp;
645                         int sensor_fit = BKE_camera_sensor_fit(ca->sensor_fit, sizex, sizey);
646                         float sensor_x = ca->sensor_x;
647                         float sensor_y = (ca->sensor_fit == CAMERA_SENSOR_FIT_AUTO) ? ca->sensor_x : ca->sensor_y;
648
649                         /* determine sensor plane */
650                         rctf rect;
651
652                         if (sensor_fit == CAMERA_SENSOR_FIT_HOR) {
653                                 float sensor_scale = (x2i - x1i) / sensor_x;
654                                 float sensor_height = sensor_scale * sensor_y;
655
656                                 rect.xmin = x1i;
657                                 rect.xmax = x2i;
658                                 rect.ymin = (y1i + y2i) * 0.5f - sensor_height * 0.5f;
659                                 rect.ymax = rect.ymin + sensor_height;
660                         }
661                         else {
662                                 float sensor_scale = (y2i - y1i) / sensor_y;
663                                 float sensor_width = sensor_scale * sensor_x;
664
665                                 rect.xmin = (x1i + x2i) * 0.5f - sensor_width * 0.5f;
666                                 rect.xmax = rect.xmin + sensor_width;
667                                 rect.ymin = y1i;
668                                 rect.ymax = y2i;
669                         }
670
671                         /* draw */
672                         immUniformThemeColorShade(TH_VIEW_OVERLAY, 100);
673
674                         /* TODO Was using UI_draw_roundbox_4fv(false, rect.xmin, rect.ymin, rect.xmax, rect.ymax, 2.0f, color).
675                          * We'll probably need a new imm_draw_line_roundbox_dashed dor that - though in practice the
676                          * 2.0f round corner effect was nearly not visible anyway... */
677                         imm_draw_box_wire_2d(shdr_pos, rect.xmin, rect.ymin, rect.xmax, rect.ymax);
678                 }
679         }
680
681         immUnbindProgram();
682         /* end dashed lines */
683
684         /* camera name - draw in highlighted text color */
685         if (ca && ((v3d->overlay.flag & V3D_OVERLAY_HIDE_TEXT) == 0) && (ca->flag & CAM_SHOWNAME)) {
686                 UI_FontThemeColor(BLF_default(), TH_TEXT_HI);
687                 BLF_draw_default(
688                         x1i, y1i - (0.7f * U.widget_unit), 0.0f,
689                         v3d->camera->id.name + 2, sizeof(v3d->camera->id.name) - 2);
690         }
691 }
692
693 static void drawrenderborder(ARegion *ar, View3D *v3d)
694 {
695         /* use the same program for everything */
696         uint shdr_pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
697
698         GPU_line_width(1.0f);
699
700         immBindBuiltinProgram(GPU_SHADER_2D_LINE_DASHED_UNIFORM_COLOR);
701
702         float viewport_size[4];
703         GPU_viewport_size_get_f(viewport_size);
704         immUniform2f("viewport_size", viewport_size[2], viewport_size[3]);
705
706         immUniform1i("colors_len", 0);  /* "simple" mode */
707         immUniform4f("color", 1.0f, 0.25f, 0.25f, 1.0f);
708         immUniform1f("dash_width", 6.0f);
709         immUniform1f("dash_factor", 0.5f);
710
711         imm_draw_box_wire_2d(shdr_pos,
712                           v3d->render_border.xmin * ar->winx, v3d->render_border.ymin * ar->winy,
713                           v3d->render_border.xmax * ar->winx, v3d->render_border.ymax * ar->winy);
714
715         immUnbindProgram();
716 }
717
718 void ED_view3d_draw_depth(
719         Depsgraph *depsgraph,
720         ARegion *ar, View3D *v3d, bool alphaoverride)
721 {
722         struct bThemeState theme_state;
723         Scene *scene = DEG_get_evaluated_scene(depsgraph);
724         RegionView3D *rv3d = ar->regiondata;
725
726         short flag = v3d->flag;
727         float glalphaclip = U.glalphaclip;
728         int obcenter_dia = U.obcenter_dia;
729         /* temp set drawtype to solid */
730         /* Setting these temporarily is not nice */
731         v3d->flag &= ~V3D_SELECT_OUTLINE;
732
733         /* not that nice but means we wont zoom into billboards */
734         U.glalphaclip = alphaoverride ? 0.5f : glalphaclip;
735
736         U.obcenter_dia = 0;
737
738         /* Tools may request depth outside of regular drawing code. */
739         UI_Theme_Store(&theme_state);
740         UI_SetTheme(SPACE_VIEW3D, RGN_TYPE_WINDOW);
741
742         ED_view3d_draw_setup_view(NULL, depsgraph, scene, ar, v3d, NULL, NULL, NULL);
743
744         GPU_clear(GPU_DEPTH_BIT);
745
746         if (rv3d->rflag & RV3D_CLIPPING) {
747                 ED_view3d_clipping_set(rv3d);
748         }
749         /* get surface depth without bias */
750         rv3d->rflag |= RV3D_ZOFFSET_DISABLED;
751
752         GPU_depth_test(true);
753
754         GPUViewport *viewport = WM_draw_region_get_viewport(ar, 0);
755         DRW_draw_depth_loop(depsgraph, ar, v3d, viewport);
756
757         if (rv3d->rflag & RV3D_CLIPPING) {
758                 ED_view3d_clipping_disable();
759         }
760         rv3d->rflag &= ~RV3D_ZOFFSET_DISABLED;
761
762         /* Reset default for UI */
763         GPU_depth_test(false);
764
765         U.glalphaclip = glalphaclip;
766         v3d->flag = flag;
767         U.obcenter_dia = obcenter_dia;
768
769         UI_Theme_Restore(&theme_state);
770 }
771
772 /* ******************** other elements ***************** */
773
774 /** could move this elsewhere, but tied into #ED_view3d_grid_scale */
775 float ED_scene_grid_scale(Scene *scene, const char **grid_unit)
776 {
777         /* apply units */
778         if (scene->unit.system) {
779                 const void *usys;
780                 int len;
781
782                 bUnit_GetSystem(scene->unit.system, B_UNIT_LENGTH, &usys, &len);
783
784                 if (usys) {
785                         int i = bUnit_GetBaseUnit(usys);
786                         if (grid_unit) {
787                                 *grid_unit = bUnit_GetNameDisplay(usys, i);
788                         }
789                         return (float)bUnit_GetScaler(usys, i) / scene->unit.scale_length;
790                 }
791         }
792
793         return 1.0f;
794 }
795
796 float ED_view3d_grid_scale(Scene *scene, View3D *v3d, const char **grid_unit)
797 {
798         return v3d->grid * ED_scene_grid_scale(scene, grid_unit);
799 }
800
801 /* Simulates the grid scale that is actually viewed.
802  * The actual code is seen in `object_grid_frag.glsl` (see `grid_res`).
803  * Currently the simulation is only done when RV3D_VIEW_IS_AXIS. */
804 float ED_view3d_grid_view_scale(
805         Scene *scene, View3D *v3d, RegionView3D *rv3d, const char **grid_unit)
806 {
807         float grid_scale = ED_view3d_grid_scale(scene, v3d, grid_unit);
808         if (!rv3d->is_persp && RV3D_VIEW_IS_AXIS(rv3d->view)) {
809                 /* Decrease the distance between grid snap points depending on zoom. */
810                 float grid_subdiv = v3d->gridsubdiv;
811                 if (grid_subdiv > 1) {
812                         /* Allow 3 more subdivisions (see OBJECT_engine_init). */
813                         grid_scale /= powf(grid_subdiv, 3);
814
815                         /* `3.0` was a value obtained by trial and error in order to get
816                          * a nice snap distance.*/
817                         float grid_res = 3.0 * (rv3d->dist / v3d->lens);
818                         float lvl = (logf(grid_res / grid_scale) / logf(grid_subdiv));
819
820                         CLAMP_MIN(lvl, 0.0f);
821
822                         grid_scale *= pow(grid_subdiv, (int)lvl);
823                 }
824         }
825
826         return grid_scale;
827 }
828
829 static void draw_view_axis(RegionView3D *rv3d, const rcti *rect)
830 {
831         const float k = U.rvisize * U.pixelsize;  /* axis size */
832         /* axis alpha offset (rvibright has range 0-10) */
833         const int bright = - 20 * (10 - U.rvibright);
834
835         /* Axis center in screen coordinates.
836          *
837          * - Unit size offset so small text doesn't draw outside the screen
838          * - Extra X offset because of the panel expander.
839          */
840         const float startx = rect->xmax - (k + UI_UNIT_X * 1.5);
841         const float starty = rect->ymax - (k + UI_UNIT_Y);
842
843         float axis_pos[3][2];
844         uchar axis_col[3][4];
845
846         int axis_order[3] = {0, 1, 2};
847         axis_sort_v3(rv3d->viewinv[2], axis_order);
848
849         for (int axis_i = 0; axis_i < 3; axis_i++) {
850                 int i = axis_order[axis_i];
851
852                 /* get position of each axis tip on screen */
853                 float vec[3] = { 0.0f };
854                 vec[i] = 1.0f;
855                 mul_qt_v3(rv3d->viewquat, vec);
856                 axis_pos[i][0] = startx + vec[0] * k;
857                 axis_pos[i][1] = starty + vec[1] * k;
858
859                 /* get color of each axis */
860                 UI_GetThemeColorShade3ubv(TH_AXIS_X + i, bright, axis_col[i]); /* rgb */
861                 axis_col[i][3] = 255 * hypotf(vec[0], vec[1]); /* alpha */
862         }
863
864         /* draw axis lines */
865         GPU_line_width(2.0f);
866         GPU_line_smooth(true);
867         GPU_blend(true);
868         GPU_blend_set_func_separate(GPU_SRC_ALPHA, GPU_ONE_MINUS_SRC_ALPHA, GPU_ONE, GPU_ONE_MINUS_SRC_ALPHA);
869
870         GPUVertFormat *format = immVertexFormat();
871         uint pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
872         uint col = GPU_vertformat_attr_add(format, "color", GPU_COMP_U8, 4, GPU_FETCH_INT_TO_FLOAT_UNIT);
873
874         immBindBuiltinProgram(GPU_SHADER_2D_FLAT_COLOR);
875         immBegin(GPU_PRIM_LINES, 6);
876
877         for (int axis_i = 0; axis_i < 3; axis_i++) {
878                 int i = axis_order[axis_i];
879
880                 immAttr4ubv(col, axis_col[i]);
881                 immVertex2f(pos, startx, starty);
882                 immAttr4ubv(col, axis_col[i]);
883                 immVertex2fv(pos, axis_pos[i]);
884         }
885
886         immEnd();
887         immUnbindProgram();
888         GPU_line_smooth(false);
889
890         /* draw axis names */
891         for (int axis_i = 0; axis_i < 3; axis_i++) {
892                 int i = axis_order[axis_i];
893
894                 const char axis_text[2] = {'x' + i, '\0'};
895                 BLF_color4ubv(BLF_default(), axis_col[i]);
896                 BLF_draw_default_ascii(axis_pos[i][0] + 2, axis_pos[i][1] + 2, 0.0f, axis_text, 1);
897         }
898 }
899
900 #ifdef WITH_INPUT_NDOF
901 /* draw center and axis of rotation for ongoing 3D mouse navigation */
902 static void draw_rotation_guide(const RegionView3D *rv3d)
903 {
904         float o[3];    /* center of rotation */
905         float end[3];  /* endpoints for drawing */
906
907         GLubyte color[4] = {0, 108, 255, 255};  /* bright blue so it matches device LEDs */
908
909         negate_v3_v3(o, rv3d->ofs);
910
911         GPU_blend(true);
912         GPU_blend_set_func_separate(GPU_SRC_ALPHA, GPU_ONE_MINUS_SRC_ALPHA, GPU_ONE, GPU_ONE_MINUS_SRC_ALPHA);
913         glDepthMask(GL_FALSE);  /* don't overwrite zbuf */
914
915         GPUVertFormat *format = immVertexFormat();
916         uint pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
917         uint col = GPU_vertformat_attr_add(format, "color", GPU_COMP_U8, 4, GPU_FETCH_INT_TO_FLOAT_UNIT);
918
919         immBindBuiltinProgram(GPU_SHADER_3D_SMOOTH_COLOR);
920
921         if (rv3d->rot_angle != 0.0f) {
922                 /* -- draw rotation axis -- */
923                 float scaled_axis[3];
924                 const float scale = rv3d->dist;
925                 mul_v3_v3fl(scaled_axis, rv3d->rot_axis, scale);
926
927
928                 immBegin(GPU_PRIM_LINE_STRIP, 3);
929                 color[3] = 0; /* more transparent toward the ends */
930                 immAttr4ubv(col, color);
931                 add_v3_v3v3(end, o, scaled_axis);
932                 immVertex3fv(pos, end);
933
934 #if 0
935                 color[3] = 0.2f + fabsf(rv3d->rot_angle);  /* modulate opacity with angle */
936                 /* ^^ neat idea, but angle is frame-rate dependent, so it's usually close to 0.2 */
937 #endif
938
939                 color[3] = 127; /* more opaque toward the center */
940                 immAttr4ubv(col, color);
941                 immVertex3fv(pos, o);
942
943                 color[3] = 0;
944                 immAttr4ubv(col, color);
945                 sub_v3_v3v3(end, o, scaled_axis);
946                 immVertex3fv(pos, end);
947                 immEnd();
948
949                 /* -- draw ring around rotation center -- */
950                 {
951 #define     ROT_AXIS_DETAIL 13
952
953                         const float s = 0.05f * scale;
954                         const float step = 2.0f * (float)(M_PI / ROT_AXIS_DETAIL);
955
956                         float q[4];  /* rotate ring so it's perpendicular to axis */
957                         const int upright = fabsf(rv3d->rot_axis[2]) >= 0.95f;
958                         if (!upright) {
959                                 const float up[3] = {0.0f, 0.0f, 1.0f};
960                                 float vis_angle, vis_axis[3];
961
962                                 cross_v3_v3v3(vis_axis, up, rv3d->rot_axis);
963                                 vis_angle = acosf(dot_v3v3(up, rv3d->rot_axis));
964                                 axis_angle_to_quat(q, vis_axis, vis_angle);
965                         }
966
967                         immBegin(GPU_PRIM_LINE_LOOP, ROT_AXIS_DETAIL);
968                         color[3] = 63; /* somewhat faint */
969                         immAttr4ubv(col, color);
970                         float angle = 0.0f;
971                         for (int i = 0; i < ROT_AXIS_DETAIL; ++i, angle += step) {
972                                 float p[3] = {s * cosf(angle), s * sinf(angle), 0.0f};
973
974                                 if (!upright) {
975                                         mul_qt_v3(q, p);
976                                 }
977
978                                 add_v3_v3(p, o);
979                                 immVertex3fv(pos, p);
980                         }
981                         immEnd();
982
983 #undef      ROT_AXIS_DETAIL
984                 }
985
986                 color[3] = 255;  /* solid dot */
987         }
988         else {
989                 color[3] = 127;  /* see-through dot */
990         }
991
992         immUnbindProgram();
993
994         /* -- draw rotation center -- */
995         immBindBuiltinProgram(GPU_SHADER_3D_POINT_FIXED_SIZE_VARYING_COLOR);
996         GPU_point_size(5.0f);
997         immBegin(GPU_PRIM_POINTS, 1);
998         immAttr4ubv(col, color);
999         immVertex3fv(pos, o);
1000         immEnd();
1001         immUnbindProgram();
1002
1003 #if 0
1004         /* find screen coordinates for rotation center, then draw pretty icon */
1005         mul_m4_v3(rv3d->persinv, rot_center);
1006         UI_icon_draw(rot_center[0], rot_center[1], ICON_NDOF_TURN);
1007         /* ^^ just playing around, does not work */
1008 #endif
1009
1010         GPU_blend(false);
1011         glDepthMask(GL_TRUE);
1012 }
1013 #endif /* WITH_INPUT_NDOF */
1014
1015 /**
1016  * Render and camera border
1017  */
1018 static void view3d_draw_border(const bContext *C, ARegion *ar)
1019 {
1020         Scene *scene = CTX_data_scene(C);
1021         Depsgraph *depsgraph = CTX_data_depsgraph(C);
1022         RegionView3D *rv3d = ar->regiondata;
1023         View3D *v3d = CTX_wm_view3d(C);
1024
1025         if (rv3d->persp == RV3D_CAMOB) {
1026                 drawviewborder(scene, depsgraph, ar, v3d);
1027         }
1028         else if (v3d->flag2 & V3D_RENDER_BORDER) {
1029                 drawrenderborder(ar, v3d);
1030         }
1031 }
1032
1033 /** \} */
1034
1035 /* -------------------------------------------------------------------- */
1036 /** \name Draw Text & Info
1037  * \{ */
1038
1039 /**
1040  * Draw Info
1041  */
1042 static void view3d_draw_grease_pencil(const bContext *UNUSED(C))
1043 {
1044         /* TODO viewport */
1045 }
1046
1047 /**
1048  * Viewport Name
1049  */
1050 static const char *view3d_get_name(View3D *v3d, RegionView3D *rv3d)
1051 {
1052         const char *name = NULL;
1053
1054         switch (rv3d->view) {
1055                 case RV3D_VIEW_FRONT:
1056                         if (rv3d->persp == RV3D_ORTHO) {
1057                                 name = IFACE_("Front Orthographic");
1058                         }
1059                         else {
1060                                 name = IFACE_("Front Perspective");
1061                         }
1062                         break;
1063                 case RV3D_VIEW_BACK:
1064                         if (rv3d->persp == RV3D_ORTHO) {
1065                                 name = IFACE_("Back Orthographic");
1066                         }
1067                         else {
1068                                 name = IFACE_("Back Perspective");
1069                         }
1070                         break;
1071                 case RV3D_VIEW_TOP:
1072                         if (rv3d->persp == RV3D_ORTHO) {
1073                                 name = IFACE_("Top Orthographic");
1074                         }
1075                         else {
1076                                 name = IFACE_("Top Perspective");
1077                         }
1078                         break;
1079                 case RV3D_VIEW_BOTTOM:
1080                         if (rv3d->persp == RV3D_ORTHO) {
1081                                 name = IFACE_("Bottom Orthographic");
1082                         }
1083                         else {
1084                                 name = IFACE_("Bottom Perspective");
1085                         }
1086                         break;
1087                 case RV3D_VIEW_RIGHT:
1088                         if (rv3d->persp == RV3D_ORTHO) {
1089                                 name = IFACE_("Right Orthographic");
1090                         }
1091                         else {
1092                                 name = IFACE_("Right Perspective");
1093                         }
1094                         break;
1095                 case RV3D_VIEW_LEFT:
1096                         if (rv3d->persp == RV3D_ORTHO) {
1097                                 name = IFACE_("Left Orthographic");
1098                         }
1099                         else {
1100                                 name = IFACE_("Left Perspective");
1101                         }
1102                         break;
1103
1104                 default:
1105                         if (rv3d->persp == RV3D_CAMOB) {
1106                                 if ((v3d->camera) && (v3d->camera->type == OB_CAMERA)) {
1107                                         Camera *cam;
1108                                         cam = v3d->camera->data;
1109                                         if (cam->type == CAM_PERSP) {
1110                                                 name = IFACE_("Camera Perspective");
1111                                         }
1112                                         else if (cam->type == CAM_ORTHO) {
1113                                                 name = IFACE_("Camera Orthographic");
1114                                         }
1115                                         else {
1116                                                 BLI_assert(cam->type == CAM_PANO);
1117                                                 name = IFACE_("Camera Panoramic");
1118                                         }
1119                                 }
1120                                 else {
1121                                         name = IFACE_("Object as Camera");
1122                                 }
1123                         }
1124                         else {
1125                                 name = (rv3d->persp == RV3D_ORTHO) ? IFACE_("User Orthographic") : IFACE_("User Perspective");
1126                         }
1127         }
1128
1129         return name;
1130 }
1131
1132 static void draw_viewport_name(ARegion *ar, View3D *v3d, int xoffset, int *yoffset)
1133 {
1134         RegionView3D *rv3d = ar->regiondata;
1135         const char *name = view3d_get_name(v3d, rv3d);
1136         const int font_id = BLF_default();
1137
1138         /* increase size for unicode languages (Chinese in utf-8...) */
1139 #ifdef WITH_INTERNATIONAL
1140         char tmpstr[96];
1141 #else
1142         char tmpstr[32];
1143 #endif
1144
1145         BLF_enable(font_id, BLF_SHADOW);
1146         BLF_shadow(font_id, 5, (const float[4]){0.0f, 0.0f, 0.0f, 1.0f});
1147         BLF_shadow_offset(font_id, 1, -1);
1148
1149         if (v3d->localvd) {
1150                 BLI_snprintf(tmpstr, sizeof(tmpstr), IFACE_("%s (Local)"), name);
1151                 name = tmpstr;
1152         }
1153
1154         UI_FontThemeColor(BLF_default(), TH_TEXT_HI);
1155
1156         *yoffset -= U.widget_unit;
1157
1158 #ifdef WITH_INTERNATIONAL
1159         BLF_draw_default(xoffset, *yoffset, 0.0f, name, sizeof(tmpstr));
1160 #else
1161         BLF_draw_default_ascii(xoffset, *yoffset, 0.0f, name, sizeof(tmpstr));
1162 #endif
1163
1164         BLF_disable(font_id, BLF_SHADOW);
1165 }
1166
1167 /**
1168  * draw info beside axes in bottom left-corner:
1169  * framenum, collection, object name, bone name (if available), marker name (if available)
1170  */
1171
1172 static void draw_selected_name(Scene *scene, ViewLayer *view_layer, Object *ob, int xoffset, int *yoffset)
1173 {
1174         const int cfra = CFRA;
1175         const char *msg_pin = " (Pinned)";
1176         const char *msg_sep = " : ";
1177
1178         const int font_id = BLF_default();
1179
1180         char info[300];
1181         char *s = info;
1182
1183         s += sprintf(s, "(%d)", cfra);
1184
1185         if ((ob == NULL) || (ob->mode == OB_MODE_OBJECT)) {
1186                 LayerCollection *layer_collection = view_layer->active_collection;
1187                 s += sprintf(s, " %s%s", BKE_collection_ui_name_get(layer_collection->collection), (ob == NULL) ? "" : " |");
1188         }
1189
1190         /*
1191          * info can contain:
1192          * - a frame (7 + 2)
1193          * - a collection name (MAX_NAME + 3)
1194          * - 3 object names (MAX_NAME)
1195          * - 2 BREAD_CRUMB_SEPARATORs (6)
1196          * - a SHAPE_KEY_PINNED marker and a trailing '\0' (9+1) - translated, so give some room!
1197          * - a marker name (MAX_NAME + 3)
1198          */
1199
1200         /* get name of marker on current frame (if available) */
1201         const char *markern = BKE_scene_find_marker_name(scene, cfra);
1202
1203         /* check if there is an object */
1204         if (ob) {
1205                 *s++ = ' ';
1206                 s += BLI_strcpy_rlen(s, ob->id.name + 2);
1207
1208                 /* name(s) to display depends on type of object */
1209                 if (ob->type == OB_ARMATURE) {
1210                         bArmature *arm = ob->data;
1211
1212                         /* show name of active bone too (if possible) */
1213                         if (arm->edbo) {
1214                                 if (arm->act_edbone) {
1215                                         s += BLI_strcpy_rlen(s, msg_sep);
1216                                         s += BLI_strcpy_rlen(s, arm->act_edbone->name);
1217                                 }
1218                         }
1219                         else if (ob->mode & OB_MODE_POSE) {
1220                                 if (arm->act_bone) {
1221
1222                                         if (arm->act_bone->layer & arm->layer) {
1223                                                 s += BLI_strcpy_rlen(s, msg_sep);
1224                                                 s += BLI_strcpy_rlen(s, arm->act_bone->name);
1225                                         }
1226                                 }
1227                         }
1228                 }
1229                 else if (ELEM(ob->type, OB_MESH, OB_LATTICE, OB_CURVE)) {
1230                         /* try to display active bone and active shapekey too (if they exist) */
1231
1232                         if (ob->type == OB_MESH && ob->mode & OB_MODE_WEIGHT_PAINT) {
1233                                 Object *armobj = BKE_object_pose_armature_get(ob);
1234                                 if (armobj  && armobj->mode & OB_MODE_POSE) {
1235                                         bArmature *arm = armobj->data;
1236                                         if (arm->act_bone) {
1237                                                 if (arm->act_bone->layer & arm->layer) {
1238                                                         s += BLI_strcpy_rlen(s, msg_sep);
1239                                                         s += BLI_strcpy_rlen(s, arm->act_bone->name);
1240                                                 }
1241                                         }
1242                                 }
1243                         }
1244
1245                         Key *key = BKE_key_from_object(ob);
1246                         if (key) {
1247                                 KeyBlock *kb = BLI_findlink(&key->block, ob->shapenr - 1);
1248                                 if (kb) {
1249                                         s += BLI_strcpy_rlen(s, msg_sep);
1250                                         s += BLI_strcpy_rlen(s, kb->name);
1251                                         if (ob->shapeflag & OB_SHAPE_LOCK) {
1252                                                 s += BLI_strcpy_rlen(s, IFACE_(msg_pin));
1253                                         }
1254                                 }
1255                         }
1256                 }
1257
1258                 /* color depends on whether there is a keyframe */
1259                 if (id_frame_has_keyframe((ID *)ob, /* BKE_scene_frame_get(scene) */ (float)cfra, ANIMFILTER_KEYS_LOCAL)) {
1260                         UI_FontThemeColor(font_id, TH_TIME_KEYFRAME);
1261                 }
1262                 else if (ED_gpencil_has_keyframe_v3d(scene, ob, cfra)) {
1263                         UI_FontThemeColor(font_id, TH_TIME_GP_KEYFRAME);
1264                 }
1265                 else {
1266                         UI_FontThemeColor(font_id, TH_TEXT_HI);
1267                 }
1268         }
1269         else {
1270                 /* no object */
1271                 if (ED_gpencil_has_keyframe_v3d(scene, NULL, cfra)) {
1272                         UI_FontThemeColor(font_id, TH_TIME_GP_KEYFRAME);
1273                 }
1274                 else {
1275                         UI_FontThemeColor(font_id, TH_TEXT_HI);
1276                 }
1277         }
1278
1279         if (markern) {
1280                 s += sprintf(s, " <%s>", markern);
1281         }
1282
1283         BLF_enable(font_id, BLF_SHADOW);
1284         BLF_shadow(font_id, 5, (const float[4]){0.0f, 0.0f, 0.0f, 1.0f});
1285         BLF_shadow_offset(font_id, 1, -1);
1286
1287         *yoffset -= U.widget_unit;
1288         BLF_draw_default(xoffset, *yoffset, 0.0f, info, sizeof(info));
1289
1290         BLF_disable(font_id, BLF_SHADOW);
1291 }
1292
1293 /**
1294  * Information drawn on top of the solid plates and composed data
1295  */
1296 void view3d_draw_region_info(const bContext *C, ARegion *ar)
1297 {
1298         RegionView3D *rv3d = ar->regiondata;
1299         View3D *v3d = CTX_wm_view3d(C);
1300         Scene *scene = CTX_data_scene(C);
1301         wmWindowManager *wm = CTX_wm_manager(C);
1302
1303 #ifdef WITH_INPUT_NDOF
1304         if ((U.ndof_flag & NDOF_SHOW_GUIDE) &&
1305             ((rv3d->viewlock & RV3D_LOCKED) == 0) &&
1306             (rv3d->persp != RV3D_CAMOB))
1307         {
1308                 /* TODO: draw something else (but not this) during fly mode */
1309                 draw_rotation_guide(rv3d);
1310         }
1311 #endif
1312
1313         /* correct projection matrix */
1314         ED_region_pixelspace(ar);
1315
1316         /* local coordinate visible rect inside region, to accommodate overlapping ui */
1317         rcti rect;
1318         ED_region_visible_rect(ar, &rect);
1319
1320
1321         view3d_draw_border(C, ar);
1322         view3d_draw_grease_pencil(C);
1323
1324         BLF_batch_draw_begin();
1325
1326         if ((U.uiflag & USER_SHOW_GIZMO_AXIS) ||
1327             (v3d->flag2 & V3D_HIDE_OVERLAYS) ||
1328             /* No need to display gizmo and this info. */
1329             (v3d->gizmo_flag & (V3D_GIZMO_HIDE | V3D_GIZMO_HIDE_NAVIGATE)))
1330         {
1331                 /* pass */
1332         }
1333         else {
1334                 draw_view_axis(rv3d, &rect);
1335         }
1336
1337         int xoffset = rect.xmin + U.widget_unit;
1338         int yoffset = rect.ymax;
1339
1340         if ((v3d->flag2 & V3D_HIDE_OVERLAYS) == 0 &&
1341             (v3d->overlay.flag & V3D_OVERLAY_HIDE_TEXT) == 0)
1342         {
1343                 if ((U.uiflag & USER_SHOW_FPS) && ED_screen_animation_no_scrub(wm)) {
1344                         ED_scene_draw_fps(scene, xoffset, &yoffset);
1345                 }
1346                 else if (U.uiflag & USER_SHOW_VIEWPORTNAME) {
1347                         draw_viewport_name(ar, v3d, xoffset, &yoffset);
1348                 }
1349
1350                 if (U.uiflag & USER_DRAWVIEWINFO) {
1351                         ViewLayer *view_layer = CTX_data_view_layer(C);
1352                         Object *ob = OBACT(view_layer);
1353                         draw_selected_name(scene, view_layer, ob, xoffset, &yoffset);
1354                 }
1355
1356 #if 0 /* TODO */
1357                 if (grid_unit) { /* draw below the viewport name */
1358                         char numstr[32] = "";
1359
1360                         UI_FontThemeColor(BLF_default(), TH_TEXT_HI);
1361                         if (v3d->grid != 1.0f) {
1362                                 BLI_snprintf(numstr, sizeof(numstr), "%s x %.4g", grid_unit, v3d->grid);
1363                         }
1364
1365                         *yoffset -= U.widget_unit;
1366                         BLF_draw_default_ascii(xoffset, *yoffset, numstr[0] ? numstr : grid_unit, sizeof(numstr));
1367                 }
1368 #endif
1369         }
1370
1371         if ((v3d->overlay.flag & V3D_OVERLAY_HIDE_TEXT) == 0) {
1372                 DRW_draw_region_engine_info(xoffset, yoffset);
1373         }
1374
1375         BLF_batch_draw_end();
1376 }
1377
1378 /** \} */
1379
1380 /* -------------------------------------------------------------------- */
1381 /** \name Draw Viewport Contents
1382  * \{ */
1383
1384 static void view3d_draw_view(const bContext *C, ARegion *ar)
1385 {
1386         ED_view3d_draw_setup_view(CTX_wm_window(C), CTX_data_depsgraph(C), CTX_data_scene(C), ar, CTX_wm_view3d(C), NULL, NULL, NULL);
1387
1388         /* Only 100% compliant on new spec goes below */
1389         DRW_draw_view(C);
1390 }
1391
1392 RenderEngineType *ED_view3d_engine_type(Scene *scene, int drawtype)
1393 {
1394         /*
1395          * Temporary viewport draw modes until we have a proper system.
1396          * all modes are done in the draw manager, except
1397          * cycles material as it is an external render engine.
1398          */
1399         if (strcmp(scene->r.engine, RE_engine_id_CYCLES) == 0 && drawtype == OB_MATERIAL) {
1400                 return RE_engines_find(RE_engine_id_BLENDER_EEVEE);
1401         }
1402         return RE_engines_find(scene->r.engine);
1403 }
1404
1405 void view3d_main_region_draw(const bContext *C, ARegion *ar)
1406 {
1407         Main *bmain = CTX_data_main(C);
1408         View3D *v3d = CTX_wm_view3d(C);
1409
1410         view3d_draw_view(C, ar);
1411
1412         GPU_free_images_old(bmain);
1413         GPU_pass_cache_garbage_collect();
1414
1415         /* XXX This is in order to draw UI batches with the DRW
1416          * olg context since we now use it for drawing the entire area */
1417         gpu_batch_presets_reset();
1418
1419         /* No depth test for drawing action zones afterwards. */
1420         GPU_depth_test(false);
1421
1422         v3d->flag |= V3D_INVALID_BACKBUF;
1423 }
1424
1425 /** \} */
1426
1427 /* -------------------------------------------------------------------- */
1428 /** \name Offscreen Drawing
1429  * \{ */
1430
1431 static void view3d_stereo3d_setup_offscreen(
1432         Depsgraph *depsgraph, Scene *scene, View3D *v3d, ARegion *ar,
1433         float winmat[4][4], const char *viewname)
1434 {
1435         /* update the viewport matrices with the new camera */
1436         if (scene->r.views_format == SCE_VIEWS_FORMAT_STEREO_3D) {
1437                 float viewmat[4][4];
1438                 const bool is_left = STREQ(viewname, STEREO_LEFT_NAME);
1439
1440                 BKE_camera_multiview_view_matrix(&scene->r, v3d->camera, is_left, viewmat);
1441                 view3d_main_region_setup_view(depsgraph, scene, v3d, ar, viewmat, winmat, NULL);
1442         }
1443         else { /* SCE_VIEWS_FORMAT_MULTIVIEW */
1444                 float viewmat[4][4];
1445                 Object *camera = BKE_camera_multiview_render(scene, v3d->camera, viewname);
1446
1447                 BKE_camera_multiview_view_matrix(&scene->r, camera, false, viewmat);
1448                 view3d_main_region_setup_view(depsgraph, scene, v3d, ar, viewmat, winmat, NULL);
1449         }
1450 }
1451
1452 void ED_view3d_draw_offscreen(
1453         Depsgraph *depsgraph, Scene *scene,
1454         int drawtype,
1455         View3D *v3d, ARegion *ar, int winx, int winy,
1456         float viewmat[4][4], float winmat[4][4],
1457         bool do_sky, bool UNUSED(is_persp), const char *viewname,
1458         GPUFXSettings *UNUSED(fx_settings),
1459         GPUOffScreen *ofs, GPUViewport *viewport)
1460 {
1461         RegionView3D *rv3d = ar->regiondata;
1462         RenderEngineType *engine_type = ED_view3d_engine_type(scene, drawtype);
1463
1464         /* set temporary new size */
1465         int bwinx = ar->winx;
1466         int bwiny = ar->winy;
1467         rcti brect = ar->winrct;
1468
1469         ar->winx = winx;
1470         ar->winy = winy;
1471         ar->winrct.xmin = 0;
1472         ar->winrct.ymin = 0;
1473         ar->winrct.xmax = winx;
1474         ar->winrct.ymax = winy;
1475
1476         struct bThemeState theme_state;
1477         UI_Theme_Store(&theme_state);
1478         UI_SetTheme(SPACE_VIEW3D, RGN_TYPE_WINDOW);
1479
1480         /* set flags */
1481         G.f |= G_FLAG_RENDER_VIEWPORT;
1482
1483         {
1484                 /* free images which can have changed on frame-change
1485                  * warning! can be slow so only free animated images - campbell */
1486                 GPU_free_images_anim(G.main);  /* XXX :((( */
1487         }
1488
1489         GPU_matrix_push_projection();
1490         GPU_matrix_identity_set();
1491         GPU_matrix_push();
1492         GPU_matrix_identity_set();
1493
1494         if ((viewname != NULL && viewname[0] != '\0') && (viewmat == NULL) && rv3d->persp == RV3D_CAMOB && v3d->camera) {
1495                 view3d_stereo3d_setup_offscreen(depsgraph, scene, v3d, ar, winmat, viewname);
1496         }
1497         else {
1498                 view3d_main_region_setup_view(depsgraph, scene, v3d, ar, viewmat, winmat, NULL);
1499         }
1500
1501         /* main drawing call */
1502         DRW_draw_render_loop_offscreen(
1503                 depsgraph, engine_type, ar, v3d,
1504                 do_sky, ofs, viewport);
1505
1506         /* restore size */
1507         ar->winx = bwinx;
1508         ar->winy = bwiny;
1509         ar->winrct = brect;
1510
1511         GPU_matrix_pop_projection();
1512         GPU_matrix_pop();
1513
1514         UI_Theme_Restore(&theme_state);
1515
1516         G.f &= ~G_FLAG_RENDER_VIEWPORT;
1517 }
1518
1519 /**
1520  * Utility func for ED_view3d_draw_offscreen
1521  *
1522  * \param ofs: Optional off-screen buffer, can be NULL.
1523  * (avoids re-creating when doing multiple GL renders).
1524  */
1525 ImBuf *ED_view3d_draw_offscreen_imbuf(
1526         Depsgraph *depsgraph, Scene *scene,
1527         int drawtype,
1528         View3D *v3d, ARegion *ar, int sizex, int sizey,
1529         uint flag, uint draw_flags,
1530         int alpha_mode, int samples, const char *viewname,
1531         /* output vars */
1532         GPUOffScreen *ofs, char err_out[256])
1533 {
1534         RegionView3D *rv3d = ar->regiondata;
1535         const bool draw_sky = (alpha_mode == R_ADDSKY);
1536         const bool use_full_sample = (draw_flags & V3D_OFSDRAW_USE_FULL_SAMPLE);
1537
1538         /* view state */
1539         GPUFXSettings fx_settings = v3d->fx_settings;
1540         bool is_ortho = false;
1541         float winmat[4][4];
1542
1543         if (ofs && ((GPU_offscreen_width(ofs) != sizex) || (GPU_offscreen_height(ofs) != sizey))) {
1544                 /* sizes differ, can't reuse */
1545                 ofs = NULL;
1546         }
1547
1548         GPUFrameBuffer *old_fb = GPU_framebuffer_active_get();
1549
1550         if (old_fb)  {
1551                 GPU_framebuffer_restore();
1552         }
1553
1554         const bool own_ofs = (ofs == NULL);
1555         DRW_opengl_context_enable();
1556
1557         if (own_ofs) {
1558                 /* bind */
1559                 ofs = GPU_offscreen_create(sizex, sizey, use_full_sample ? 0 : samples, true, false, err_out);
1560                 if (ofs == NULL) {
1561                         DRW_opengl_context_disable();
1562                         return NULL;
1563                 }
1564         }
1565
1566         GPU_offscreen_bind(ofs, true);
1567
1568         /* read in pixels & stamp */
1569         ImBuf *ibuf = IMB_allocImBuf(sizex, sizey, 32, flag);
1570
1571         /* render 3d view */
1572         if (rv3d->persp == RV3D_CAMOB && v3d->camera) {
1573                 CameraParams params;
1574                 Object *camera = BKE_camera_multiview_render(scene, v3d->camera, viewname);
1575                 const Object *camera_eval = DEG_get_evaluated_object(
1576                                                 depsgraph,
1577                                                 camera);
1578
1579                 BKE_camera_params_init(&params);
1580                 /* fallback for non camera objects */
1581                 params.clip_start = v3d->clip_start;
1582                 params.clip_end = v3d->clip_end;
1583                 BKE_camera_params_from_object(&params, camera_eval);
1584                 BKE_camera_multiview_params(&scene->r, &params, camera_eval, viewname);
1585                 BKE_camera_params_compute_viewplane(&params, sizex, sizey, scene->r.xasp, scene->r.yasp);
1586                 BKE_camera_params_compute_matrix(&params);
1587
1588                 BKE_camera_to_gpu_dof(camera, &fx_settings);
1589
1590                 is_ortho = params.is_ortho;
1591                 copy_m4_m4(winmat, params.winmat);
1592         }
1593         else {
1594                 rctf viewplane;
1595                 float clip_start, clipend;
1596
1597                 is_ortho = ED_view3d_viewplane_get(depsgraph, v3d, rv3d, sizex, sizey, &viewplane, &clip_start, &clipend, NULL);
1598                 if (is_ortho) {
1599                         orthographic_m4(winmat, viewplane.xmin, viewplane.xmax, viewplane.ymin, viewplane.ymax, -clipend, clipend);
1600                 }
1601                 else {
1602                         perspective_m4(winmat, viewplane.xmin, viewplane.xmax, viewplane.ymin, viewplane.ymax, clip_start, clipend);
1603                 }
1604         }
1605
1606         if ((samples && use_full_sample) == 0) {
1607                 /* Single-pass render, common case */
1608                 ED_view3d_draw_offscreen(
1609                         depsgraph, scene, drawtype,
1610                         v3d, ar, sizex, sizey, NULL, winmat,
1611                         draw_sky, !is_ortho, viewname,
1612                         &fx_settings, ofs, NULL);
1613
1614                 if (ibuf->rect_float) {
1615                         GPU_offscreen_read_pixels(ofs, GL_FLOAT, ibuf->rect_float);
1616                 }
1617                 else if (ibuf->rect) {
1618                         GPU_offscreen_read_pixels(ofs, GL_UNSIGNED_BYTE, ibuf->rect);
1619                 }
1620         }
1621         else {
1622                 /* Multi-pass render, use accumulation buffer & jitter for 'full' oversampling.
1623                  * Use because OpenGL may use a lower quality MSAA, and only over-sample edges. */
1624                 static float jit_ofs[32][2];
1625                 float winmat_jitter[4][4];
1626                 float *rect_temp = (ibuf->rect_float) ? ibuf->rect_float : MEM_mallocN(sizex * sizey * sizeof(float[4]), "rect_temp");
1627                 float *accum_buffer = MEM_mallocN(sizex * sizey * sizeof(float[4]), "accum_buffer");
1628                 GPUViewport *viewport = GPU_viewport_create_from_offscreen(ofs);
1629
1630                 BLI_jitter_init(jit_ofs, samples);
1631
1632                 /* first sample buffer, also initializes 'rv3d->persmat' */
1633                 ED_view3d_draw_offscreen(
1634                         depsgraph, scene, drawtype,
1635                         v3d, ar, sizex, sizey, NULL, winmat,
1636                         draw_sky, !is_ortho, viewname,
1637                         &fx_settings, ofs, viewport);
1638                 GPU_offscreen_read_pixels(ofs, GL_FLOAT, accum_buffer);
1639
1640                 /* skip the first sample */
1641                 for (int j = 1; j < samples; j++) {
1642                         copy_m4_m4(winmat_jitter, winmat);
1643                         window_translate_m4(
1644                                 winmat_jitter, rv3d->persmat,
1645                                 (jit_ofs[j][0] * 2.0f) / sizex,
1646                                 (jit_ofs[j][1] * 2.0f) / sizey);
1647
1648                         ED_view3d_draw_offscreen(
1649                                 depsgraph, scene, drawtype,
1650                                 v3d, ar, sizex, sizey, NULL, winmat_jitter,
1651                                 draw_sky, !is_ortho, viewname,
1652                                 &fx_settings, ofs, viewport);
1653                         GPU_offscreen_read_pixels(ofs, GL_FLOAT, rect_temp);
1654
1655                         uint i = sizex * sizey * 4;
1656                         while (i--) {
1657                                 accum_buffer[i] += rect_temp[i];
1658                         }
1659                 }
1660
1661                 {
1662                         /* don't free data owned by 'ofs' */
1663                         GPU_viewport_clear_from_offscreen(viewport);
1664                         GPU_viewport_free(viewport);
1665                 }
1666
1667                 if (ibuf->rect_float == NULL) {
1668                         MEM_freeN(rect_temp);
1669                 }
1670
1671                 if (ibuf->rect_float) {
1672                         float *rect_float = ibuf->rect_float;
1673                         uint i = sizex * sizey * 4;
1674                         while (i--) {
1675                                 rect_float[i] = accum_buffer[i] / samples;
1676                         }
1677                 }
1678                 else {
1679                         uchar *rect_ub = (uchar *)ibuf->rect;
1680                         uint i = sizex * sizey * 4;
1681                         while (i--) {
1682                                 rect_ub[i] = (uchar)(255.0f * accum_buffer[i] / samples);
1683                         }
1684                 }
1685
1686                 MEM_freeN(accum_buffer);
1687         }
1688
1689         /* unbind */
1690         GPU_offscreen_unbind(ofs, true);
1691
1692         if (own_ofs) {
1693                 GPU_offscreen_free(ofs);
1694         }
1695
1696         DRW_opengl_context_disable();
1697
1698         if (old_fb)  {
1699                 GPU_framebuffer_bind(old_fb);
1700         }
1701
1702         if (ibuf->rect_float && ibuf->rect) {
1703                 IMB_rect_from_float(ibuf);
1704         }
1705
1706         return ibuf;
1707 }
1708
1709 /**
1710  * Creates own fake 3d views (wrapping #ED_view3d_draw_offscreen_imbuf)
1711  *
1712  * \param ofs: Optional off-screen buffer can be NULL.
1713  * (avoids re-creating when doing multiple GL renders).
1714  *
1715  * \note used by the sequencer
1716  */
1717 ImBuf *ED_view3d_draw_offscreen_imbuf_simple(
1718         Depsgraph *depsgraph, Scene *scene,
1719         int drawtype,
1720         Object *camera, int width, int height,
1721         uint flag, uint draw_flags,
1722         int alpha_mode, int samples, const char *viewname,
1723         GPUOffScreen *ofs, char err_out[256])
1724 {
1725         View3D v3d = {NULL};
1726         ARegion ar = {NULL};
1727         RegionView3D rv3d = {{{0}}};
1728
1729         /* connect data */
1730         v3d.regionbase.first = v3d.regionbase.last = &ar;
1731         ar.regiondata = &rv3d;
1732         ar.regiontype = RGN_TYPE_WINDOW;
1733
1734         v3d.camera = camera;
1735         v3d.shading.type = drawtype;
1736         v3d.flag2 = V3D_HIDE_OVERLAYS;
1737
1738         if (draw_flags & V3D_OFSDRAW_USE_GPENCIL) {
1739                 v3d.flag2 |= V3D_SHOW_ANNOTATION;
1740         }
1741
1742         v3d.shading.background_type = V3D_SHADING_BACKGROUND_WORLD;
1743
1744         if (draw_flags & V3D_OFSDRAW_USE_CAMERA_DOF) {
1745                 if (camera->type == OB_CAMERA) {
1746                         v3d.fx_settings.dof = &((Camera *)camera->data)->gpu_dof;
1747                         v3d.fx_settings.fx_flag |= GPU_FX_FLAG_DOF;
1748                 }
1749         }
1750
1751         rv3d.persp = RV3D_CAMOB;
1752
1753         copy_m4_m4(rv3d.viewinv, v3d.camera->obmat);
1754         normalize_m4(rv3d.viewinv);
1755         invert_m4_m4(rv3d.viewmat, rv3d.viewinv);
1756
1757         {
1758                 CameraParams params;
1759                 const Object *view_camera_eval = DEG_get_evaluated_object(
1760                                                      depsgraph,
1761                                                      BKE_camera_multiview_render(scene, v3d.camera, viewname));
1762
1763                 BKE_camera_params_init(&params);
1764                 BKE_camera_params_from_object(&params, view_camera_eval);
1765                 BKE_camera_multiview_params(&scene->r, &params, view_camera_eval, viewname);
1766                 BKE_camera_params_compute_viewplane(&params, width, height, scene->r.xasp, scene->r.yasp);
1767                 BKE_camera_params_compute_matrix(&params);
1768
1769                 copy_m4_m4(rv3d.winmat, params.winmat);
1770                 v3d.clip_start = params.clip_start;
1771                 v3d.clip_end = params.clip_end;
1772                 v3d.lens = params.lens;
1773         }
1774
1775         mul_m4_m4m4(rv3d.persmat, rv3d.winmat, rv3d.viewmat);
1776         invert_m4_m4(rv3d.persinv, rv3d.viewinv);
1777
1778         return ED_view3d_draw_offscreen_imbuf(
1779                 depsgraph, scene, drawtype,
1780                 &v3d, &ar, width, height, flag,
1781                 draw_flags, alpha_mode, samples, viewname, ofs, err_out);
1782 }
1783
1784 /** \} */
1785
1786 /* -------------------------------------------------------------------- */
1787 /** \name Viewport Clipping
1788  * \{ */
1789
1790 static bool view3d_clipping_test(const float co[3], const float clip[6][4])
1791 {
1792         if (plane_point_side_v3(clip[0], co) > 0.0f) {
1793                 if (plane_point_side_v3(clip[1], co) > 0.0f) {
1794                         if (plane_point_side_v3(clip[2], co) > 0.0f) {
1795                                 if (plane_point_side_v3(clip[3], co) > 0.0f) {
1796                                         return false;
1797                                 }
1798                         }
1799                 }
1800         }
1801
1802         return true;
1803 }
1804
1805 /* for 'local' ED_view3d_clipping_local must run first
1806  * then all comparisons can be done in localspace */
1807 bool ED_view3d_clipping_test(const RegionView3D *rv3d, const float co[3], const bool is_local)
1808 {
1809         return view3d_clipping_test(co, is_local ? rv3d->clip_local : rv3d->clip);
1810 }
1811
1812 /** \} */