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_jitter_2d.h"
27 #include "BLI_listbase.h"
28 #include "BLI_math.h"
29 #include "BLI_rect.h"
30 #include "BLI_string.h"
31 #include "BLI_string_utils.h"
32 #include "BLI_threads.h"
33 
34 #include "BKE_armature.h"
35 #include "BKE_camera.h"
36 #include "BKE_collection.h"
37 #include "BKE_context.h"
38 #include "BKE_customdata.h"
39 #include "BKE_global.h"
40 #include "BKE_image.h"
41 #include "BKE_key.h"
42 #include "BKE_layer.h"
43 #include "BKE_main.h"
44 #include "BKE_object.h"
45 #include "BKE_paint.h"
46 #include "BKE_scene.h"
47 #include "BKE_studiolight.h"
48 #include "BKE_unit.h"
49 
50 #include "BLF_api.h"
51 
52 #include "BLT_translation.h"
53 
54 #include "DNA_armature_types.h"
55 #include "DNA_brush_types.h"
56 #include "DNA_camera_types.h"
57 #include "DNA_key_types.h"
58 #include "DNA_mesh_types.h"
59 #include "DNA_object_types.h"
60 #include "DNA_view3d_types.h"
61 #include "DNA_windowmanager_types.h"
62 
63 #include "DRW_engine.h"
64 #include "DRW_select_buffer.h"
65 
66 #include "ED_gpencil.h"
67 #include "ED_info.h"
68 #include "ED_keyframing.h"
69 #include "ED_screen.h"
70 #include "ED_screen_types.h"
71 #include "ED_transform.h"
72 #include "ED_view3d_offscreen.h"
73 
74 #include "DEG_depsgraph_query.h"
75 
76 #include "GPU_batch.h"
77 #include "GPU_batch_presets.h"
78 #include "GPU_framebuffer.h"
79 #include "GPU_immediate.h"
80 #include "GPU_immediate_util.h"
81 #include "GPU_material.h"
82 #include "GPU_matrix.h"
83 #include "GPU_state.h"
84 #include "GPU_viewport.h"
85 
86 #include "MEM_guardedalloc.h"
87 
88 #include "UI_interface.h"
89 #include "UI_resources.h"
90 
91 #include "RE_engine.h"
92 
93 #include "WM_api.h"
94 #include "WM_types.h"
95 
96 #include "RNA_access.h"
97 
98 #include "IMB_imbuf.h"
99 #include "IMB_imbuf_types.h"
100 
101 #include "view3d_intern.h" /* own include */
102 
103 #define M_GOLDEN_RATIO_CONJUGATE 0.618033988749895f
104 
105 #define VIEW3D_OVERLAY_LINEHEIGHT (0.9f * U.widget_unit)
106 
107 /* -------------------------------------------------------------------- */
108 /** \name General Functions
109  * \{ */
110 
111 /**
112  * \note keep this synced with #ED_view3d_mats_rv3d_backup/#ED_view3d_mats_rv3d_restore
113  */
ED_view3d_update_viewmat(Depsgraph * depsgraph,const Scene * scene,View3D * v3d,ARegion * region,const float viewmat[4][4],const float winmat[4][4],const rcti * rect,bool offscreen)114 void ED_view3d_update_viewmat(Depsgraph *depsgraph,
115                               const Scene *scene,
116                               View3D *v3d,
117                               ARegion *region,
118                               const float viewmat[4][4],
119                               const float winmat[4][4],
120                               const rcti *rect,
121                               bool offscreen)
122 {
123   RegionView3D *rv3d = region->regiondata;
124 
125   /* setup window matrices */
126   if (winmat) {
127     copy_m4_m4(rv3d->winmat, winmat);
128   }
129   else {
130     view3d_winmatrix_set(depsgraph, region, v3d, rect);
131   }
132 
133   /* setup view matrix */
134   if (viewmat) {
135     copy_m4_m4(rv3d->viewmat, viewmat);
136   }
137   else {
138     float rect_scale[2];
139     if (rect) {
140       rect_scale[0] = (float)BLI_rcti_size_x(rect) / (float)region->winx;
141       rect_scale[1] = (float)BLI_rcti_size_y(rect) / (float)region->winy;
142     }
143     /* note: calls BKE_object_where_is_calc for camera... */
144     view3d_viewmatrix_set(depsgraph, scene, v3d, rv3d, rect ? rect_scale : NULL);
145   }
146   /* update utility matrices */
147   mul_m4_m4m4(rv3d->persmat, rv3d->winmat, rv3d->viewmat);
148   invert_m4_m4(rv3d->persinv, rv3d->persmat);
149   invert_m4_m4(rv3d->viewinv, rv3d->viewmat);
150 
151   /* calculate GLSL view dependent values */
152 
153   /* store window coordinates scaling/offset */
154   if (!offscreen && rv3d->persp == RV3D_CAMOB && v3d->camera) {
155     rctf cameraborder;
156     ED_view3d_calc_camera_border(scene, depsgraph, region, v3d, rv3d, &cameraborder, false);
157     rv3d->viewcamtexcofac[0] = (float)region->winx / BLI_rctf_size_x(&cameraborder);
158     rv3d->viewcamtexcofac[1] = (float)region->winy / BLI_rctf_size_y(&cameraborder);
159 
160     rv3d->viewcamtexcofac[2] = -rv3d->viewcamtexcofac[0] * cameraborder.xmin / (float)region->winx;
161     rv3d->viewcamtexcofac[3] = -rv3d->viewcamtexcofac[1] * cameraborder.ymin / (float)region->winy;
162   }
163   else {
164     rv3d->viewcamtexcofac[0] = rv3d->viewcamtexcofac[1] = 1.0f;
165     rv3d->viewcamtexcofac[2] = rv3d->viewcamtexcofac[3] = 0.0f;
166   }
167 
168   /* calculate pixelsize factor once, is used for lights and obcenters */
169   {
170     /* note:  '1.0f / len_v3(v1)'  replaced  'len_v3(rv3d->viewmat[0])'
171      * because of float point precision problems at large values T23908. */
172     float v1[3], v2[3];
173     float len_px, len_sc;
174 
175     v1[0] = rv3d->persmat[0][0];
176     v1[1] = rv3d->persmat[1][0];
177     v1[2] = rv3d->persmat[2][0];
178 
179     v2[0] = rv3d->persmat[0][1];
180     v2[1] = rv3d->persmat[1][1];
181     v2[2] = rv3d->persmat[2][1];
182 
183     len_px = 2.0f / sqrtf(min_ff(len_squared_v3(v1), len_squared_v3(v2)));
184 
185     if (rect) {
186       len_sc = (float)max_ii(BLI_rcti_size_x(rect), BLI_rcti_size_y(rect));
187     }
188     else {
189       len_sc = (float)MAX2(region->winx, region->winy);
190     }
191 
192     rv3d->pixsize = len_px / len_sc;
193   }
194 }
195 
view3d_main_region_setup_view(Depsgraph * depsgraph,Scene * scene,View3D * v3d,ARegion * region,const float viewmat[4][4],const float winmat[4][4],const rcti * rect)196 static void view3d_main_region_setup_view(Depsgraph *depsgraph,
197                                           Scene *scene,
198                                           View3D *v3d,
199                                           ARegion *region,
200                                           const float viewmat[4][4],
201                                           const float winmat[4][4],
202                                           const rcti *rect)
203 {
204   RegionView3D *rv3d = region->regiondata;
205 
206   ED_view3d_update_viewmat(depsgraph, scene, v3d, region, viewmat, winmat, rect, false);
207 
208   /* set for opengl */
209   GPU_matrix_projection_set(rv3d->winmat);
210   GPU_matrix_set(rv3d->viewmat);
211 }
212 
view3d_main_region_setup_offscreen(Depsgraph * depsgraph,const Scene * scene,View3D * v3d,ARegion * region,const float viewmat[4][4],const float winmat[4][4])213 static void view3d_main_region_setup_offscreen(Depsgraph *depsgraph,
214                                                const Scene *scene,
215                                                View3D *v3d,
216                                                ARegion *region,
217                                                const float viewmat[4][4],
218                                                const float winmat[4][4])
219 {
220   RegionView3D *rv3d = region->regiondata;
221   ED_view3d_update_viewmat(depsgraph, scene, v3d, region, viewmat, winmat, NULL, true);
222 
223   /* set for opengl */
224   GPU_matrix_projection_set(rv3d->winmat);
225   GPU_matrix_set(rv3d->viewmat);
226 }
227 
view3d_stereo3d_active(wmWindow * win,const Scene * scene,View3D * v3d,RegionView3D * rv3d)228 static bool view3d_stereo3d_active(wmWindow *win,
229                                    const Scene *scene,
230                                    View3D *v3d,
231                                    RegionView3D *rv3d)
232 {
233   if ((scene->r.scemode & R_MULTIVIEW) == 0) {
234     return false;
235   }
236 
237   if ((v3d->camera == NULL) || (v3d->camera->type != OB_CAMERA) || rv3d->persp != RV3D_CAMOB) {
238     return false;
239   }
240 
241   switch (v3d->stereo3d_camera) {
242     case STEREO_MONO_ID:
243       return false;
244       break;
245     case STEREO_3D_ID:
246       /* win will be NULL when calling this from the selection or draw loop. */
247       if ((win == NULL) || (WM_stereo3d_enabled(win, true) == false)) {
248         return false;
249       }
250       if (((scene->r.views_format & SCE_VIEWS_FORMAT_MULTIVIEW) != 0) &&
251           !BKE_scene_multiview_is_stereo3d(&scene->r)) {
252         return false;
253       }
254       break;
255     /* We always need the stereo calculation for left and right cameras. */
256     case STEREO_LEFT_ID:
257     case STEREO_RIGHT_ID:
258     default:
259       break;
260   }
261   return true;
262 }
263 
264 /* setup the view and win matrices for the multiview cameras
265  *
266  * unlike view3d_stereo3d_setup_offscreen, when view3d_stereo3d_setup is called
267  * we have no winmatrix (i.e., projection matrix) defined at that time.
268  * Since the camera and the camera shift are needed for the winmat calculation
269  * we do a small hack to replace it temporarily so we don't need to change the
270  * view3d)main_region_setup_view() code to account for that.
271  */
view3d_stereo3d_setup(Depsgraph * depsgraph,Scene * scene,View3D * v3d,ARegion * region,const rcti * rect)272 static void view3d_stereo3d_setup(
273     Depsgraph *depsgraph, Scene *scene, View3D *v3d, ARegion *region, const rcti *rect)
274 {
275   bool is_left;
276   const char *names[2] = {STEREO_LEFT_NAME, STEREO_RIGHT_NAME};
277   const char *viewname;
278 
279   /* show only left or right camera */
280   if (v3d->stereo3d_camera != STEREO_3D_ID) {
281     v3d->multiview_eye = v3d->stereo3d_camera;
282   }
283 
284   is_left = v3d->multiview_eye == STEREO_LEFT_ID;
285   viewname = names[is_left ? STEREO_LEFT_ID : STEREO_RIGHT_ID];
286 
287   /* update the viewport matrices with the new camera */
288   if (scene->r.views_format == SCE_VIEWS_FORMAT_STEREO_3D) {
289     Camera *data, *data_eval;
290     float viewmat[4][4];
291     float shiftx;
292 
293     data = (Camera *)v3d->camera->data;
294     data_eval = (Camera *)DEG_get_evaluated_id(depsgraph, &data->id);
295 
296     shiftx = data_eval->shiftx;
297 
298     BLI_thread_lock(LOCK_VIEW3D);
299     data_eval->shiftx = BKE_camera_multiview_shift_x(&scene->r, v3d->camera, viewname);
300 
301     BKE_camera_multiview_view_matrix(&scene->r, v3d->camera, is_left, viewmat);
302     view3d_main_region_setup_view(depsgraph, scene, v3d, region, viewmat, NULL, rect);
303 
304     data_eval->shiftx = shiftx;
305     BLI_thread_unlock(LOCK_VIEW3D);
306   }
307   else { /* SCE_VIEWS_FORMAT_MULTIVIEW */
308     float viewmat[4][4];
309     Object *view_ob = v3d->camera;
310     Object *camera = BKE_camera_multiview_render(scene, v3d->camera, viewname);
311 
312     BLI_thread_lock(LOCK_VIEW3D);
313     v3d->camera = camera;
314 
315     BKE_camera_multiview_view_matrix(&scene->r, camera, false, viewmat);
316     view3d_main_region_setup_view(depsgraph, scene, v3d, region, viewmat, NULL, rect);
317 
318     v3d->camera = view_ob;
319     BLI_thread_unlock(LOCK_VIEW3D);
320   }
321 }
322 
323 #ifdef WITH_XR_OPENXR
view3d_xr_mirror_setup(const wmWindowManager * wm,Depsgraph * depsgraph,Scene * scene,View3D * v3d,ARegion * region,const rcti * rect)324 static void view3d_xr_mirror_setup(const wmWindowManager *wm,
325                                    Depsgraph *depsgraph,
326                                    Scene *scene,
327                                    View3D *v3d,
328                                    ARegion *region,
329                                    const rcti *rect)
330 {
331   RegionView3D *rv3d = region->regiondata;
332   float viewmat[4][4];
333   const float lens_old = v3d->lens;
334 
335   if (!WM_xr_session_state_viewer_pose_matrix_info_get(&wm->xr, viewmat, &v3d->lens)) {
336     /* Can't get info from XR session, use fallback values. */
337     copy_m4_m4(viewmat, rv3d->viewmat);
338     v3d->lens = lens_old;
339   }
340   view3d_main_region_setup_view(depsgraph, scene, v3d, region, viewmat, NULL, rect);
341 
342   /* Reset overridden View3D data */
343   v3d->lens = lens_old;
344 }
345 #endif /* WITH_XR_OPENXR */
346 
347 /**
348  * Set the correct matrices
349  */
ED_view3d_draw_setup_view(const wmWindowManager * wm,wmWindow * win,Depsgraph * depsgraph,Scene * scene,ARegion * region,View3D * v3d,const float viewmat[4][4],const float winmat[4][4],const rcti * rect)350 void ED_view3d_draw_setup_view(const wmWindowManager *wm,
351                                wmWindow *win,
352                                Depsgraph *depsgraph,
353                                Scene *scene,
354                                ARegion *region,
355                                View3D *v3d,
356                                const float viewmat[4][4],
357                                const float winmat[4][4],
358                                const rcti *rect)
359 {
360   RegionView3D *rv3d = region->regiondata;
361 
362 #ifdef WITH_XR_OPENXR
363   /* Setup the view matrix. */
364   if (ED_view3d_is_region_xr_mirror_active(wm, v3d, region)) {
365     view3d_xr_mirror_setup(wm, depsgraph, scene, v3d, region, rect);
366   }
367   else
368 #endif
369       if (view3d_stereo3d_active(win, scene, v3d, rv3d)) {
370     view3d_stereo3d_setup(depsgraph, scene, v3d, region, rect);
371   }
372   else {
373     view3d_main_region_setup_view(depsgraph, scene, v3d, region, viewmat, winmat, rect);
374   }
375 
376 #ifndef WITH_XR_OPENXR
377   UNUSED_VARS(wm);
378 #endif
379 }
380 
381 /** \} */
382 
383 /* -------------------------------------------------------------------- */
384 /** \name Draw View Border
385  * \{ */
386 
view3d_camera_border(const Scene * scene,struct Depsgraph * depsgraph,const ARegion * region,const View3D * v3d,const RegionView3D * rv3d,rctf * r_viewborder,const bool no_shift,const bool no_zoom)387 static void view3d_camera_border(const Scene *scene,
388                                  struct Depsgraph *depsgraph,
389                                  const ARegion *region,
390                                  const View3D *v3d,
391                                  const RegionView3D *rv3d,
392                                  rctf *r_viewborder,
393                                  const bool no_shift,
394                                  const bool no_zoom)
395 {
396   CameraParams params;
397   rctf rect_view, rect_camera;
398   Object *camera_eval = DEG_get_evaluated_object(depsgraph, v3d->camera);
399 
400   /* get viewport viewplane */
401   BKE_camera_params_init(&params);
402   BKE_camera_params_from_view3d(&params, depsgraph, v3d, rv3d);
403   if (no_zoom) {
404     params.zoom = 1.0f;
405   }
406   BKE_camera_params_compute_viewplane(&params, region->winx, region->winy, 1.0f, 1.0f);
407   rect_view = params.viewplane;
408 
409   /* get camera viewplane */
410   BKE_camera_params_init(&params);
411   /* fallback for non camera objects */
412   params.clip_start = v3d->clip_start;
413   params.clip_end = v3d->clip_end;
414   BKE_camera_params_from_object(&params, camera_eval);
415   if (no_shift) {
416     params.shiftx = 0.0f;
417     params.shifty = 0.0f;
418   }
419   BKE_camera_params_compute_viewplane(
420       &params, scene->r.xsch, scene->r.ysch, scene->r.xasp, scene->r.yasp);
421   rect_camera = params.viewplane;
422 
423   /* get camera border within viewport */
424   r_viewborder->xmin = ((rect_camera.xmin - rect_view.xmin) / BLI_rctf_size_x(&rect_view)) *
425                        region->winx;
426   r_viewborder->xmax = ((rect_camera.xmax - rect_view.xmin) / BLI_rctf_size_x(&rect_view)) *
427                        region->winx;
428   r_viewborder->ymin = ((rect_camera.ymin - rect_view.ymin) / BLI_rctf_size_y(&rect_view)) *
429                        region->winy;
430   r_viewborder->ymax = ((rect_camera.ymax - rect_view.ymin) / BLI_rctf_size_y(&rect_view)) *
431                        region->winy;
432 }
433 
ED_view3d_calc_camera_border_size(const Scene * scene,Depsgraph * depsgraph,const ARegion * region,const View3D * v3d,const RegionView3D * rv3d,float r_size[2])434 void ED_view3d_calc_camera_border_size(const Scene *scene,
435                                        Depsgraph *depsgraph,
436                                        const ARegion *region,
437                                        const View3D *v3d,
438                                        const RegionView3D *rv3d,
439                                        float r_size[2])
440 {
441   rctf viewborder;
442 
443   view3d_camera_border(scene, depsgraph, region, v3d, rv3d, &viewborder, true, true);
444   r_size[0] = BLI_rctf_size_x(&viewborder);
445   r_size[1] = BLI_rctf_size_y(&viewborder);
446 }
447 
ED_view3d_calc_camera_border(const Scene * scene,Depsgraph * depsgraph,const ARegion * region,const View3D * v3d,const RegionView3D * rv3d,rctf * r_viewborder,const bool no_shift)448 void ED_view3d_calc_camera_border(const Scene *scene,
449                                   Depsgraph *depsgraph,
450                                   const ARegion *region,
451                                   const View3D *v3d,
452                                   const RegionView3D *rv3d,
453                                   rctf *r_viewborder,
454                                   const bool no_shift)
455 {
456   view3d_camera_border(scene, depsgraph, region, v3d, rv3d, r_viewborder, no_shift, false);
457 }
458 
drawviewborder_grid3(uint shdr_pos,float x1,float x2,float y1,float y2,float fac)459 static void drawviewborder_grid3(uint shdr_pos, float x1, float x2, float y1, float y2, float fac)
460 {
461   float x3, y3, x4, y4;
462 
463   x3 = x1 + fac * (x2 - x1);
464   y3 = y1 + fac * (y2 - y1);
465   x4 = x1 + (1.0f - fac) * (x2 - x1);
466   y4 = y1 + (1.0f - fac) * (y2 - y1);
467 
468   immBegin(GPU_PRIM_LINES, 8);
469 
470   immVertex2f(shdr_pos, x1, y3);
471   immVertex2f(shdr_pos, x2, y3);
472 
473   immVertex2f(shdr_pos, x1, y4);
474   immVertex2f(shdr_pos, x2, y4);
475 
476   immVertex2f(shdr_pos, x3, y1);
477   immVertex2f(shdr_pos, x3, y2);
478 
479   immVertex2f(shdr_pos, x4, y1);
480   immVertex2f(shdr_pos, x4, y2);
481 
482   immEnd();
483 }
484 
485 /* harmonious triangle */
drawviewborder_triangle(uint shdr_pos,float x1,float x2,float y1,float y2,const char golden,const char dir)486 static void drawviewborder_triangle(
487     uint shdr_pos, float x1, float x2, float y1, float y2, const char golden, const char dir)
488 {
489   float ofs;
490   float w = x2 - x1;
491   float h = y2 - y1;
492 
493   immBegin(GPU_PRIM_LINES, 6);
494 
495   if (w > h) {
496     if (golden) {
497       ofs = w * (1.0f - M_GOLDEN_RATIO_CONJUGATE);
498     }
499     else {
500       ofs = h * (h / w);
501     }
502     if (dir == 'B') {
503       SWAP(float, y1, y2);
504     }
505 
506     immVertex2f(shdr_pos, x1, y1);
507     immVertex2f(shdr_pos, x2, y2);
508 
509     immVertex2f(shdr_pos, x2, y1);
510     immVertex2f(shdr_pos, x1 + (w - ofs), y2);
511 
512     immVertex2f(shdr_pos, x1, y2);
513     immVertex2f(shdr_pos, x1 + ofs, y1);
514   }
515   else {
516     if (golden) {
517       ofs = h * (1.0f - M_GOLDEN_RATIO_CONJUGATE);
518     }
519     else {
520       ofs = w * (w / h);
521     }
522     if (dir == 'B') {
523       SWAP(float, x1, x2);
524     }
525 
526     immVertex2f(shdr_pos, x1, y1);
527     immVertex2f(shdr_pos, x2, y2);
528 
529     immVertex2f(shdr_pos, x2, y1);
530     immVertex2f(shdr_pos, x1, y1 + ofs);
531 
532     immVertex2f(shdr_pos, x1, y2);
533     immVertex2f(shdr_pos, x2, y1 + (h - ofs));
534   }
535 
536   immEnd();
537 }
538 
drawviewborder(Scene * scene,Depsgraph * depsgraph,ARegion * region,View3D * v3d)539 static void drawviewborder(Scene *scene, Depsgraph *depsgraph, ARegion *region, View3D *v3d)
540 {
541   float x1, x2, y1, y2;
542   float x1i, x2i, y1i, y2i;
543 
544   rctf viewborder;
545   Camera *ca = NULL;
546   RegionView3D *rv3d = region->regiondata;
547 
548   if (v3d->camera == NULL) {
549     return;
550   }
551   if (v3d->camera->type == OB_CAMERA) {
552     ca = v3d->camera->data;
553   }
554 
555   ED_view3d_calc_camera_border(scene, depsgraph, region, v3d, rv3d, &viewborder, false);
556   /* the offsets */
557   x1 = viewborder.xmin;
558   y1 = viewborder.ymin;
559   x2 = viewborder.xmax;
560   y2 = viewborder.ymax;
561 
562   GPU_line_width(1.0f);
563 
564   /* apply offsets so the real 3D camera shows through */
565 
566   /* note: quite un-scientific but without this bit extra
567    * 0.0001 on the lower left the 2D border sometimes
568    * obscures the 3D camera border */
569   /* note: with VIEW3D_CAMERA_BORDER_HACK defined this error isn't noticeable
570    * but keep it here in case we need to remove the workaround */
571   x1i = (int)(x1 - 1.0001f);
572   y1i = (int)(y1 - 1.0001f);
573   x2i = (int)(x2 + (1.0f - 0.0001f));
574   y2i = (int)(y2 + (1.0f - 0.0001f));
575 
576   uint shdr_pos = GPU_vertformat_attr_add(
577       immVertexFormat(), "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
578 
579   /* First, solid lines. */
580   {
581     immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
582 
583     /* passepartout, specified in camera edit buttons */
584     if (ca && (ca->flag & CAM_SHOWPASSEPARTOUT) && ca->passepartalpha > 0.000001f) {
585       const float winx = (region->winx + 1);
586       const float winy = (region->winy + 1);
587 
588       float alpha = 1.0f;
589 
590       if (ca->passepartalpha != 1.0f) {
591         GPU_blend(GPU_BLEND_ALPHA);
592         alpha = ca->passepartalpha;
593       }
594 
595       immUniformColor4f(0.0f, 0.0f, 0.0f, alpha);
596 
597       if (x1i > 0.0f) {
598         immRectf(shdr_pos, 0.0f, winy, x1i, 0.0f);
599       }
600       if (x2i < winx) {
601         immRectf(shdr_pos, x2i, winy, winx, 0.0f);
602       }
603       if (y2i < winy) {
604         immRectf(shdr_pos, x1i, winy, x2i, y2i);
605       }
606       if (y2i > 0.0f) {
607         immRectf(shdr_pos, x1i, y1i, x2i, 0.0f);
608       }
609 
610       GPU_blend(GPU_BLEND_NONE);
611     }
612 
613     immUniformThemeColor3(TH_BACK);
614     imm_draw_box_wire_2d(shdr_pos, x1i, y1i, x2i, y2i);
615 
616 #ifdef VIEW3D_CAMERA_BORDER_HACK
617     if (view3d_camera_border_hack_test == true) {
618       immUniformColor3ubv(view3d_camera_border_hack_col);
619       imm_draw_box_wire_2d(shdr_pos, x1i + 1, y1i + 1, x2i - 1, y2i - 1);
620       view3d_camera_border_hack_test = false;
621     }
622 #endif
623 
624     immUnbindProgram();
625   }
626 
627   /* When overlays are disabled, only show camera outline & passepartout. */
628   if (v3d->flag2 & V3D_HIDE_OVERLAYS) {
629     return;
630   }
631 
632   /* And now, the dashed lines! */
633   immBindBuiltinProgram(GPU_SHADER_2D_LINE_DASHED_UNIFORM_COLOR);
634 
635   {
636     float viewport_size[4];
637     GPU_viewport_size_get_f(viewport_size);
638     immUniform2f("viewport_size", viewport_size[2], viewport_size[3]);
639 
640     immUniform1i("colors_len", 0); /* "simple" mode */
641     immUniform1f("dash_width", 6.0f);
642     immUniform1f("dash_factor", 0.5f);
643 
644     /* outer line not to confuse with object selection */
645     if (v3d->flag2 & V3D_LOCK_CAMERA) {
646       immUniformThemeColor(TH_REDALERT);
647       imm_draw_box_wire_2d(shdr_pos, x1i - 1, y1i - 1, x2i + 1, y2i + 1);
648     }
649 
650     immUniformThemeColor3(TH_VIEW_OVERLAY);
651     imm_draw_box_wire_2d(shdr_pos, x1i, y1i, x2i, y2i);
652   }
653 
654   /* Render Border. */
655   if (scene->r.mode & R_BORDER) {
656     float x3, y3, x4, y4;
657 
658     x3 = floorf(x1 + (scene->r.border.xmin * (x2 - x1))) - 1;
659     y3 = floorf(y1 + (scene->r.border.ymin * (y2 - y1))) - 1;
660     x4 = floorf(x1 + (scene->r.border.xmax * (x2 - x1))) + (U.pixelsize - 1);
661     y4 = floorf(y1 + (scene->r.border.ymax * (y2 - y1))) + (U.pixelsize - 1);
662 
663     immUniformColor3f(1.0f, 0.25f, 0.25f);
664     imm_draw_box_wire_2d(shdr_pos, x3, y3, x4, y4);
665   }
666 
667   /* safety border */
668   if (ca) {
669     GPU_blend(GPU_BLEND_ALPHA);
670     immUniformThemeColorAlpha(TH_VIEW_OVERLAY, 0.75f);
671 
672     if (ca->dtx & CAM_DTX_CENTER) {
673       float x3, y3;
674 
675       x3 = x1 + 0.5f * (x2 - x1);
676       y3 = y1 + 0.5f * (y2 - y1);
677 
678       immBegin(GPU_PRIM_LINES, 4);
679 
680       immVertex2f(shdr_pos, x1, y3);
681       immVertex2f(shdr_pos, x2, y3);
682 
683       immVertex2f(shdr_pos, x3, y1);
684       immVertex2f(shdr_pos, x3, y2);
685 
686       immEnd();
687     }
688 
689     if (ca->dtx & CAM_DTX_CENTER_DIAG) {
690       immBegin(GPU_PRIM_LINES, 4);
691 
692       immVertex2f(shdr_pos, x1, y1);
693       immVertex2f(shdr_pos, x2, y2);
694 
695       immVertex2f(shdr_pos, x1, y2);
696       immVertex2f(shdr_pos, x2, y1);
697 
698       immEnd();
699     }
700 
701     if (ca->dtx & CAM_DTX_THIRDS) {
702       drawviewborder_grid3(shdr_pos, x1, x2, y1, y2, 1.0f / 3.0f);
703     }
704 
705     if (ca->dtx & CAM_DTX_GOLDEN) {
706       drawviewborder_grid3(shdr_pos, x1, x2, y1, y2, 1.0f - M_GOLDEN_RATIO_CONJUGATE);
707     }
708 
709     if (ca->dtx & CAM_DTX_GOLDEN_TRI_A) {
710       drawviewborder_triangle(shdr_pos, x1, x2, y1, y2, 0, 'A');
711     }
712 
713     if (ca->dtx & CAM_DTX_GOLDEN_TRI_B) {
714       drawviewborder_triangle(shdr_pos, x1, x2, y1, y2, 0, 'B');
715     }
716 
717     if (ca->dtx & CAM_DTX_HARMONY_TRI_A) {
718       drawviewborder_triangle(shdr_pos, x1, x2, y1, y2, 1, 'A');
719     }
720 
721     if (ca->dtx & CAM_DTX_HARMONY_TRI_B) {
722       drawviewborder_triangle(shdr_pos, x1, x2, y1, y2, 1, 'B');
723     }
724 
725     if (ca->flag & CAM_SHOW_SAFE_MARGINS) {
726       UI_draw_safe_areas(
727           shdr_pos, x1, x2, y1, y2, scene->safe_areas.title, scene->safe_areas.action);
728 
729       if (ca->flag & CAM_SHOW_SAFE_CENTER) {
730         UI_draw_safe_areas(shdr_pos,
731                            x1,
732                            x2,
733                            y1,
734                            y2,
735                            scene->safe_areas.title_center,
736                            scene->safe_areas.action_center);
737       }
738     }
739 
740     if (ca->flag & CAM_SHOWSENSOR) {
741       /* determine sensor fit, and get sensor x/y, for auto fit we
742        * assume and square sensor and only use sensor_x */
743       float sizex = scene->r.xsch * scene->r.xasp;
744       float sizey = scene->r.ysch * scene->r.yasp;
745       int sensor_fit = BKE_camera_sensor_fit(ca->sensor_fit, sizex, sizey);
746       float sensor_x = ca->sensor_x;
747       float sensor_y = (ca->sensor_fit == CAMERA_SENSOR_FIT_AUTO) ? ca->sensor_x : ca->sensor_y;
748 
749       /* determine sensor plane */
750       rctf rect;
751 
752       if (sensor_fit == CAMERA_SENSOR_FIT_HOR) {
753         float sensor_scale = (x2i - x1i) / sensor_x;
754         float sensor_height = sensor_scale * sensor_y;
755 
756         rect.xmin = x1i;
757         rect.xmax = x2i;
758         rect.ymin = (y1i + y2i) * 0.5f - sensor_height * 0.5f;
759         rect.ymax = rect.ymin + sensor_height;
760       }
761       else {
762         float sensor_scale = (y2i - y1i) / sensor_y;
763         float sensor_width = sensor_scale * sensor_x;
764 
765         rect.xmin = (x1i + x2i) * 0.5f - sensor_width * 0.5f;
766         rect.xmax = rect.xmin + sensor_width;
767         rect.ymin = y1i;
768         rect.ymax = y2i;
769       }
770 
771       /* draw */
772       immUniformThemeColorShade(TH_VIEW_OVERLAY, 100);
773 
774       /* TODO Was using:
775        * UI_draw_roundbox_4fv(false, rect.xmin, rect.ymin, rect.xmax, rect.ymax, 2.0f, color);
776        * We'll probably need a new imm_draw_line_roundbox_dashed dor that - though in practice the
777        * 2.0f round corner effect was nearly not visible anyway... */
778       imm_draw_box_wire_2d(shdr_pos, rect.xmin, rect.ymin, rect.xmax, rect.ymax);
779     }
780 
781     GPU_blend(GPU_BLEND_NONE);
782   }
783 
784   immUnbindProgram();
785   /* end dashed lines */
786 
787   /* camera name - draw in highlighted text color */
788   if (ca && ((v3d->overlay.flag & V3D_OVERLAY_HIDE_TEXT) == 0) && (ca->flag & CAM_SHOWNAME)) {
789     UI_FontThemeColor(BLF_default(), TH_TEXT_HI);
790     BLF_draw_default(x1i,
791                      y1i - (0.7f * U.widget_unit),
792                      0.0f,
793                      v3d->camera->id.name + 2,
794                      sizeof(v3d->camera->id.name) - 2);
795   }
796 }
797 
drawrenderborder(ARegion * region,View3D * v3d)798 static void drawrenderborder(ARegion *region, View3D *v3d)
799 {
800   /* use the same program for everything */
801   uint shdr_pos = GPU_vertformat_attr_add(
802       immVertexFormat(), "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
803 
804   GPU_line_width(1.0f);
805 
806   immBindBuiltinProgram(GPU_SHADER_2D_LINE_DASHED_UNIFORM_COLOR);
807 
808   float viewport_size[4];
809   GPU_viewport_size_get_f(viewport_size);
810   immUniform2f("viewport_size", viewport_size[2], viewport_size[3]);
811 
812   immUniform1i("colors_len", 0); /* "simple" mode */
813   immUniform4f("color", 1.0f, 0.25f, 0.25f, 1.0f);
814   immUniform1f("dash_width", 6.0f);
815   immUniform1f("dash_factor", 0.5f);
816 
817   imm_draw_box_wire_2d(shdr_pos,
818                        v3d->render_border.xmin * region->winx,
819                        v3d->render_border.ymin * region->winy,
820                        v3d->render_border.xmax * region->winx,
821                        v3d->render_border.ymax * region->winy);
822 
823   immUnbindProgram();
824 }
825 
ED_view3d_draw_depth(Depsgraph * depsgraph,ARegion * region,View3D * v3d,bool alphaoverride)826 void ED_view3d_draw_depth(Depsgraph *depsgraph, ARegion *region, View3D *v3d, bool alphaoverride)
827 {
828   struct bThemeState theme_state;
829   Scene *scene = DEG_get_evaluated_scene(depsgraph);
830   RegionView3D *rv3d = region->regiondata;
831 
832   short flag = v3d->flag;
833   float glalphaclip = U.glalphaclip;
834   /* temp set drawtype to solid */
835   /* Setting these temporarily is not nice */
836   v3d->flag &= ~V3D_SELECT_OUTLINE;
837 
838   /* not that nice but means we wont zoom into billboards */
839   U.glalphaclip = alphaoverride ? 0.5f : glalphaclip;
840 
841   /* Tools may request depth outside of regular drawing code. */
842   UI_Theme_Store(&theme_state);
843   UI_SetTheme(SPACE_VIEW3D, RGN_TYPE_WINDOW);
844 
845   ED_view3d_draw_setup_view(
846       G_MAIN->wm.first, NULL, depsgraph, scene, region, v3d, NULL, NULL, NULL);
847 
848   /* get surface depth without bias */
849   rv3d->rflag |= RV3D_ZOFFSET_DISABLED;
850 
851   /* Needed in cases the view-port isn't already setup. */
852   WM_draw_region_viewport_ensure(region, SPACE_VIEW3D);
853   WM_draw_region_viewport_bind(region);
854 
855   GPUViewport *viewport = WM_draw_region_get_viewport(region);
856   /* When Blender is starting, a click event can trigger a depth test while the viewport is not
857    * yet available. */
858   if (viewport != NULL) {
859     DRW_draw_depth_loop(depsgraph, region, v3d, viewport, false);
860   }
861 
862   WM_draw_region_viewport_unbind(region);
863 
864   rv3d->rflag &= ~RV3D_ZOFFSET_DISABLED;
865 
866   U.glalphaclip = glalphaclip;
867   v3d->flag = flag;
868 
869   UI_Theme_Restore(&theme_state);
870 }
871 
872 /* ******************** other elements ***************** */
873 
874 /** could move this elsewhere, but tied into #ED_view3d_grid_scale */
ED_scene_grid_scale(const Scene * scene,const char ** r_grid_unit)875 float ED_scene_grid_scale(const Scene *scene, const char **r_grid_unit)
876 {
877   /* apply units */
878   if (scene->unit.system) {
879     const void *usys;
880     int len;
881 
882     BKE_unit_system_get(scene->unit.system, B_UNIT_LENGTH, &usys, &len);
883 
884     if (usys) {
885       int i = BKE_unit_base_get(usys);
886       if (r_grid_unit) {
887         *r_grid_unit = BKE_unit_display_name_get(usys, i);
888       }
889       return (float)BKE_unit_scalar_get(usys, i) / scene->unit.scale_length;
890     }
891   }
892 
893   return 1.0f;
894 }
895 
ED_view3d_grid_scale(const Scene * scene,View3D * v3d,const char ** r_grid_unit)896 float ED_view3d_grid_scale(const Scene *scene, View3D *v3d, const char **r_grid_unit)
897 {
898   return v3d->grid * ED_scene_grid_scale(scene, r_grid_unit);
899 }
900 
901 #define STEPS_LEN 8
ED_view3d_grid_steps(const Scene * scene,View3D * v3d,RegionView3D * rv3d,float r_grid_steps[STEPS_LEN])902 void ED_view3d_grid_steps(const Scene *scene,
903                           View3D *v3d,
904                           RegionView3D *rv3d,
905                           float r_grid_steps[STEPS_LEN])
906 {
907   const void *usys;
908   int len;
909   BKE_unit_system_get(scene->unit.system, B_UNIT_LENGTH, &usys, &len);
910   float grid_scale = v3d->grid;
911   BLI_assert(STEPS_LEN >= len);
912 
913   if (usys) {
914     if (rv3d->view == RV3D_VIEW_USER) {
915       /* Skip steps */
916       len = BKE_unit_base_get(usys) + 1;
917     }
918 
919     grid_scale /= scene->unit.scale_length;
920 
921     int i;
922     for (i = 0; i < len; i++) {
923       r_grid_steps[i] = (float)BKE_unit_scalar_get(usys, len - 1 - i) * grid_scale;
924     }
925     for (; i < STEPS_LEN; i++) {
926       /* Fill last slots */
927       r_grid_steps[i] = 10.0f * r_grid_steps[i - 1];
928     }
929   }
930   else {
931     if (rv3d->view != RV3D_VIEW_USER) {
932       /* Allow 3 more subdivisions. */
933       grid_scale /= powf(v3d->gridsubdiv, 3);
934     }
935     int subdiv = 1;
936     for (int i = 0;; i++) {
937       r_grid_steps[i] = grid_scale * subdiv;
938 
939       if (i == STEPS_LEN - 1) {
940         break;
941       }
942       subdiv *= v3d->gridsubdiv;
943     }
944   }
945 }
946 
947 /* Simulates the grid scale that is actually viewed.
948  * The actual code is seen in `object_grid_frag.glsl` (see `grid_res`).
949  * Currently the simulation is only done when RV3D_VIEW_IS_AXIS. */
ED_view3d_grid_view_scale(Scene * scene,View3D * v3d,RegionView3D * rv3d,const char ** r_grid_unit)950 float ED_view3d_grid_view_scale(Scene *scene,
951                                 View3D *v3d,
952                                 RegionView3D *rv3d,
953                                 const char **r_grid_unit)
954 {
955   float grid_scale;
956   if (!rv3d->is_persp && RV3D_VIEW_IS_AXIS(rv3d->view)) {
957     /* Decrease the distance between grid snap points depending on zoom. */
958     /* `0.38` was a value visually obtained in order to get a snap distance
959      * that matches previous versions Blender.*/
960     float min_dist = 0.38f * (rv3d->dist / v3d->lens);
961     float grid_steps[STEPS_LEN];
962     ED_view3d_grid_steps(scene, v3d, rv3d, grid_steps);
963     /* Skip last item, in case the 'mid_dist' is greater than the largest unit. */
964     int i;
965     for (i = 0; i < ARRAY_SIZE(grid_steps) - 1; i++) {
966       grid_scale = grid_steps[i];
967       if (grid_scale > min_dist) {
968         break;
969       }
970     }
971 
972     if (r_grid_unit) {
973       const void *usys;
974       int len;
975       BKE_unit_system_get(scene->unit.system, B_UNIT_LENGTH, &usys, &len);
976 
977       if (usys) {
978         *r_grid_unit = BKE_unit_display_name_get(usys, len - i - 1);
979       }
980     }
981   }
982   else {
983     grid_scale = ED_view3d_grid_scale(scene, v3d, r_grid_unit);
984   }
985 
986   return grid_scale;
987 }
988 
989 #undef STEPS_LEN
990 
draw_view_axis(RegionView3D * rv3d,const rcti * rect)991 static void draw_view_axis(RegionView3D *rv3d, const rcti *rect)
992 {
993   const float k = U.rvisize * U.pixelsize; /* axis size */
994   /* axis alpha offset (rvibright has range 0-10) */
995   const int bright = -20 * (10 - U.rvibright);
996 
997   /* Axis center in screen coordinates.
998    *
999    * - Unit size offset so small text doesn't draw outside the screen
1000    * - Extra X offset because of the panel expander.
1001    */
1002   const float startx = rect->xmax - (k + UI_UNIT_X * 1.5);
1003   const float starty = rect->ymax - (k + UI_UNIT_Y);
1004 
1005   float axis_pos[3][2];
1006   uchar axis_col[3][4];
1007 
1008   int axis_order[3] = {0, 1, 2};
1009   axis_sort_v3(rv3d->viewinv[2], axis_order);
1010 
1011   for (int axis_i = 0; axis_i < 3; axis_i++) {
1012     int i = axis_order[axis_i];
1013 
1014     /* get position of each axis tip on screen */
1015     float vec[3] = {0.0f};
1016     vec[i] = 1.0f;
1017     mul_qt_v3(rv3d->viewquat, vec);
1018     axis_pos[i][0] = startx + vec[0] * k;
1019     axis_pos[i][1] = starty + vec[1] * k;
1020 
1021     /* get color of each axis */
1022     UI_GetThemeColorShade3ubv(TH_AXIS_X + i, bright, axis_col[i]); /* rgb */
1023     axis_col[i][3] = 255 * hypotf(vec[0], vec[1]);                 /* alpha */
1024   }
1025 
1026   /* draw axis lines */
1027   GPU_line_width(2.0f);
1028   GPU_line_smooth(true);
1029   GPU_blend(GPU_BLEND_ALPHA);
1030 
1031   GPUVertFormat *format = immVertexFormat();
1032   uint pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
1033   uint col = GPU_vertformat_attr_add(format, "color", GPU_COMP_U8, 4, GPU_FETCH_INT_TO_FLOAT_UNIT);
1034 
1035   immBindBuiltinProgram(GPU_SHADER_2D_FLAT_COLOR);
1036   immBegin(GPU_PRIM_LINES, 6);
1037 
1038   for (int axis_i = 0; axis_i < 3; axis_i++) {
1039     int i = axis_order[axis_i];
1040 
1041     immAttr4ubv(col, axis_col[i]);
1042     immVertex2f(pos, startx, starty);
1043     immAttr4ubv(col, axis_col[i]);
1044     immVertex2fv(pos, axis_pos[i]);
1045   }
1046 
1047   immEnd();
1048   immUnbindProgram();
1049   GPU_line_smooth(false);
1050 
1051   /* draw axis names */
1052   for (int axis_i = 0; axis_i < 3; axis_i++) {
1053     int i = axis_order[axis_i];
1054 
1055     const char axis_text[2] = {'x' + i, '\0'};
1056     BLF_color4ubv(BLF_default(), axis_col[i]);
1057     BLF_draw_default_ascii(axis_pos[i][0] + 2, axis_pos[i][1] + 2, 0.0f, axis_text, 1);
1058   }
1059 }
1060 
1061 #ifdef WITH_INPUT_NDOF
1062 /* draw center and axis of rotation for ongoing 3D mouse navigation */
draw_rotation_guide(const RegionView3D * rv3d)1063 static void draw_rotation_guide(const RegionView3D *rv3d)
1064 {
1065   float o[3];   /* center of rotation */
1066   float end[3]; /* endpoints for drawing */
1067 
1068   uchar color[4] = {0, 108, 255, 255}; /* bright blue so it matches device LEDs */
1069 
1070   negate_v3_v3(o, rv3d->ofs);
1071 
1072   GPU_blend(GPU_BLEND_ALPHA);
1073   GPU_depth_mask(false); /* don't overwrite zbuf */
1074 
1075   GPUVertFormat *format = immVertexFormat();
1076   uint pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
1077   uint col = GPU_vertformat_attr_add(format, "color", GPU_COMP_U8, 4, GPU_FETCH_INT_TO_FLOAT_UNIT);
1078 
1079   immBindBuiltinProgram(GPU_SHADER_3D_SMOOTH_COLOR);
1080 
1081   if (rv3d->rot_angle != 0.0f) {
1082     /* -- draw rotation axis -- */
1083     float scaled_axis[3];
1084     const float scale = rv3d->dist;
1085     mul_v3_v3fl(scaled_axis, rv3d->rot_axis, scale);
1086 
1087     immBegin(GPU_PRIM_LINE_STRIP, 3);
1088     color[3] = 0; /* more transparent toward the ends */
1089     immAttr4ubv(col, color);
1090     add_v3_v3v3(end, o, scaled_axis);
1091     immVertex3fv(pos, end);
1092 
1093 #  if 0
1094     color[3] = 0.2f + fabsf(rv3d->rot_angle); /* modulate opacity with angle */
1095     /* ^^ neat idea, but angle is frame-rate dependent, so it's usually close to 0.2 */
1096 #  endif
1097 
1098     color[3] = 127; /* more opaque toward the center */
1099     immAttr4ubv(col, color);
1100     immVertex3fv(pos, o);
1101 
1102     color[3] = 0;
1103     immAttr4ubv(col, color);
1104     sub_v3_v3v3(end, o, scaled_axis);
1105     immVertex3fv(pos, end);
1106     immEnd();
1107 
1108     /* -- draw ring around rotation center -- */
1109     {
1110 #  define ROT_AXIS_DETAIL 13
1111 
1112       const float s = 0.05f * scale;
1113       const float step = 2.0f * (float)(M_PI / ROT_AXIS_DETAIL);
1114 
1115       float q[4]; /* rotate ring so it's perpendicular to axis */
1116       const int upright = fabsf(rv3d->rot_axis[2]) >= 0.95f;
1117       if (!upright) {
1118         const float up[3] = {0.0f, 0.0f, 1.0f};
1119         float vis_angle, vis_axis[3];
1120 
1121         cross_v3_v3v3(vis_axis, up, rv3d->rot_axis);
1122         vis_angle = acosf(dot_v3v3(up, rv3d->rot_axis));
1123         axis_angle_to_quat(q, vis_axis, vis_angle);
1124       }
1125 
1126       immBegin(GPU_PRIM_LINE_LOOP, ROT_AXIS_DETAIL);
1127       color[3] = 63; /* somewhat faint */
1128       immAttr4ubv(col, color);
1129       float angle = 0.0f;
1130       for (int i = 0; i < ROT_AXIS_DETAIL; i++, angle += step) {
1131         float p[3] = {s * cosf(angle), s * sinf(angle), 0.0f};
1132 
1133         if (!upright) {
1134           mul_qt_v3(q, p);
1135         }
1136 
1137         add_v3_v3(p, o);
1138         immVertex3fv(pos, p);
1139       }
1140       immEnd();
1141 
1142 #  undef ROT_AXIS_DETAIL
1143     }
1144 
1145     color[3] = 255; /* solid dot */
1146   }
1147   else {
1148     color[3] = 127; /* see-through dot */
1149   }
1150 
1151   immUnbindProgram();
1152 
1153   /* -- draw rotation center -- */
1154   immBindBuiltinProgram(GPU_SHADER_3D_POINT_FIXED_SIZE_VARYING_COLOR);
1155   GPU_point_size(5.0f);
1156   immBegin(GPU_PRIM_POINTS, 1);
1157   immAttr4ubv(col, color);
1158   immVertex3fv(pos, o);
1159   immEnd();
1160   immUnbindProgram();
1161 
1162   GPU_blend(GPU_BLEND_NONE);
1163   GPU_depth_mask(true);
1164 }
1165 #endif /* WITH_INPUT_NDOF */
1166 
1167 /**
1168  * Render and camera border
1169  */
view3d_draw_border(const bContext * C,ARegion * region)1170 static void view3d_draw_border(const bContext *C, ARegion *region)
1171 {
1172   Scene *scene = CTX_data_scene(C);
1173   Depsgraph *depsgraph = CTX_data_expect_evaluated_depsgraph(C);
1174   RegionView3D *rv3d = region->regiondata;
1175   View3D *v3d = CTX_wm_view3d(C);
1176 
1177   if (rv3d->persp == RV3D_CAMOB) {
1178     drawviewborder(scene, depsgraph, region, v3d);
1179   }
1180   else if (v3d->flag2 & V3D_RENDER_BORDER) {
1181     drawrenderborder(region, v3d);
1182   }
1183 }
1184 
1185 /** \} */
1186 
1187 /* -------------------------------------------------------------------- */
1188 /** \name Draw Text & Info
1189  * \{ */
1190 
1191 /**
1192  * Draw Info
1193  */
view3d_draw_grease_pencil(const bContext * UNUSED (C))1194 static void view3d_draw_grease_pencil(const bContext *UNUSED(C))
1195 {
1196   /* TODO viewport */
1197 }
1198 
1199 /**
1200  * Viewport Name
1201  */
view3d_get_name(View3D * v3d,RegionView3D * rv3d)1202 static const char *view3d_get_name(View3D *v3d, RegionView3D *rv3d)
1203 {
1204   const char *name = NULL;
1205 
1206   switch (rv3d->view) {
1207     case RV3D_VIEW_FRONT:
1208       if (rv3d->persp == RV3D_ORTHO) {
1209         name = IFACE_("Front Orthographic");
1210       }
1211       else {
1212         name = IFACE_("Front Perspective");
1213       }
1214       break;
1215     case RV3D_VIEW_BACK:
1216       if (rv3d->persp == RV3D_ORTHO) {
1217         name = IFACE_("Back Orthographic");
1218       }
1219       else {
1220         name = IFACE_("Back Perspective");
1221       }
1222       break;
1223     case RV3D_VIEW_TOP:
1224       if (rv3d->persp == RV3D_ORTHO) {
1225         name = IFACE_("Top Orthographic");
1226       }
1227       else {
1228         name = IFACE_("Top Perspective");
1229       }
1230       break;
1231     case RV3D_VIEW_BOTTOM:
1232       if (rv3d->persp == RV3D_ORTHO) {
1233         name = IFACE_("Bottom Orthographic");
1234       }
1235       else {
1236         name = IFACE_("Bottom Perspective");
1237       }
1238       break;
1239     case RV3D_VIEW_RIGHT:
1240       if (rv3d->persp == RV3D_ORTHO) {
1241         name = IFACE_("Right Orthographic");
1242       }
1243       else {
1244         name = IFACE_("Right Perspective");
1245       }
1246       break;
1247     case RV3D_VIEW_LEFT:
1248       if (rv3d->persp == RV3D_ORTHO) {
1249         name = IFACE_("Left Orthographic");
1250       }
1251       else {
1252         name = IFACE_("Left Perspective");
1253       }
1254       break;
1255 
1256     default:
1257       if (rv3d->persp == RV3D_CAMOB) {
1258         if ((v3d->camera) && (v3d->camera->type == OB_CAMERA)) {
1259           Camera *cam;
1260           cam = v3d->camera->data;
1261           if (cam->type == CAM_PERSP) {
1262             name = IFACE_("Camera Perspective");
1263           }
1264           else if (cam->type == CAM_ORTHO) {
1265             name = IFACE_("Camera Orthographic");
1266           }
1267           else {
1268             BLI_assert(cam->type == CAM_PANO);
1269             name = IFACE_("Camera Panoramic");
1270           }
1271         }
1272         else {
1273           name = IFACE_("Object as Camera");
1274         }
1275       }
1276       else {
1277         name = (rv3d->persp == RV3D_ORTHO) ? IFACE_("User Orthographic") :
1278                                              IFACE_("User Perspective");
1279       }
1280   }
1281 
1282   return name;
1283 }
1284 
draw_viewport_name(ARegion * region,View3D * v3d,int xoffset,int * yoffset)1285 static void draw_viewport_name(ARegion *region, View3D *v3d, int xoffset, int *yoffset)
1286 {
1287   RegionView3D *rv3d = region->regiondata;
1288   const char *name = view3d_get_name(v3d, rv3d);
1289   const char *name_array[3] = {name, NULL, NULL};
1290   int name_array_len = 1;
1291   const int font_id = BLF_default();
1292 
1293   /* 6 is the maximum size of the axis roll text. */
1294   /* increase size for unicode languages (Chinese in utf-8...) */
1295 #ifdef WITH_INTERNATIONAL
1296   char tmpstr[96 + 6];
1297 #else
1298   char tmpstr[32 + 6];
1299 #endif
1300 
1301   BLF_enable(font_id, BLF_SHADOW);
1302   BLF_shadow(font_id, 5, (const float[4]){0.0f, 0.0f, 0.0f, 1.0f});
1303   BLF_shadow_offset(font_id, 1, -1);
1304 
1305   if (RV3D_VIEW_IS_AXIS(rv3d->view) && (rv3d->view_axis_roll != RV3D_VIEW_AXIS_ROLL_0)) {
1306     const char *axis_roll;
1307     switch (rv3d->view_axis_roll) {
1308       case RV3D_VIEW_AXIS_ROLL_90:
1309         axis_roll = " 90\xC2\xB0";
1310         break;
1311       case RV3D_VIEW_AXIS_ROLL_180:
1312         axis_roll = " 180\xC2\xB0";
1313         break;
1314       default:
1315         axis_roll = " -90\xC2\xB0";
1316         break;
1317     }
1318     name_array[name_array_len++] = axis_roll;
1319   }
1320 
1321   if (v3d->localvd) {
1322     name_array[name_array_len++] = IFACE_(" (Local)");
1323   }
1324 
1325   if (name_array_len > 1) {
1326     BLI_string_join_array(tmpstr, sizeof(tmpstr), name_array, name_array_len);
1327     name = tmpstr;
1328   }
1329 
1330   UI_FontThemeColor(BLF_default(), TH_TEXT_HI);
1331 
1332   *yoffset -= VIEW3D_OVERLAY_LINEHEIGHT;
1333 
1334   BLF_draw_default(xoffset, *yoffset, 0.0f, name, sizeof(tmpstr));
1335 
1336   BLF_disable(font_id, BLF_SHADOW);
1337 }
1338 
1339 /**
1340  * Draw info beside axes in bottom left-corner:
1341  * frame-number, collection, object name, bone name (if available), marker name (if available).
1342  */
1343 
draw_selected_name(Scene * scene,ViewLayer * view_layer,Object * ob,int xoffset,int * yoffset)1344 static void draw_selected_name(
1345     Scene *scene, ViewLayer *view_layer, Object *ob, int xoffset, int *yoffset)
1346 {
1347   const int cfra = CFRA;
1348   const char *msg_pin = " (Pinned)";
1349   const char *msg_sep = " : ";
1350 
1351   const int font_id = BLF_default();
1352 
1353   char info[300];
1354   char *s = info;
1355 
1356   s += sprintf(s, "(%d)", cfra);
1357 
1358   if ((ob == NULL) || (ob->mode == OB_MODE_OBJECT)) {
1359     LayerCollection *layer_collection = view_layer->active_collection;
1360     s += sprintf(s,
1361                  " %s%s",
1362                  BKE_collection_ui_name_get(layer_collection->collection),
1363                  (ob == NULL) ? "" : " |");
1364   }
1365 
1366   /*
1367    * info can contain:
1368    * - a frame (7 + 2)
1369    * - a collection name (MAX_NAME + 3)
1370    * - 3 object names (MAX_NAME)
1371    * - 2 BREAD_CRUMB_SEPARATORs (6)
1372    * - a SHAPE_KEY_PINNED marker and a trailing '\0' (9+1) - translated, so give some room!
1373    * - a marker name (MAX_NAME + 3)
1374    */
1375 
1376   /* get name of marker on current frame (if available) */
1377   const char *markern = BKE_scene_find_marker_name(scene, cfra);
1378 
1379   /* check if there is an object */
1380   if (ob) {
1381     *s++ = ' ';
1382     s += BLI_strcpy_rlen(s, ob->id.name + 2);
1383 
1384     /* name(s) to display depends on type of object */
1385     if (ob->type == OB_ARMATURE) {
1386       bArmature *arm = ob->data;
1387 
1388       /* show name of active bone too (if possible) */
1389       if (arm->edbo) {
1390         if (arm->act_edbone) {
1391           s += BLI_strcpy_rlen(s, msg_sep);
1392           s += BLI_strcpy_rlen(s, arm->act_edbone->name);
1393         }
1394       }
1395       else if (ob->mode & OB_MODE_POSE) {
1396         if (arm->act_bone) {
1397 
1398           if (arm->act_bone->layer & arm->layer) {
1399             s += BLI_strcpy_rlen(s, msg_sep);
1400             s += BLI_strcpy_rlen(s, arm->act_bone->name);
1401           }
1402         }
1403       }
1404     }
1405     else if (ELEM(ob->type, OB_MESH, OB_LATTICE, OB_CURVE)) {
1406       /* try to display active bone and active shapekey too (if they exist) */
1407 
1408       if (ob->type == OB_MESH && ob->mode & OB_MODE_WEIGHT_PAINT) {
1409         Object *armobj = BKE_object_pose_armature_get(ob);
1410         if (armobj && armobj->mode & OB_MODE_POSE) {
1411           bArmature *arm = armobj->data;
1412           if (arm->act_bone) {
1413             if (arm->act_bone->layer & arm->layer) {
1414               s += BLI_strcpy_rlen(s, msg_sep);
1415               s += BLI_strcpy_rlen(s, arm->act_bone->name);
1416             }
1417           }
1418         }
1419       }
1420 
1421       Key *key = BKE_key_from_object(ob);
1422       if (key) {
1423         KeyBlock *kb = BLI_findlink(&key->block, ob->shapenr - 1);
1424         if (kb) {
1425           s += BLI_strcpy_rlen(s, msg_sep);
1426           s += BLI_strcpy_rlen(s, kb->name);
1427           if (ob->shapeflag & OB_SHAPE_LOCK) {
1428             s += BLI_strcpy_rlen(s, IFACE_(msg_pin));
1429           }
1430         }
1431       }
1432     }
1433 
1434     /* color depends on whether there is a keyframe */
1435     if (id_frame_has_keyframe(
1436             (ID *)ob, /* BKE_scene_frame_get(scene) */ (float)cfra, ANIMFILTER_KEYS_LOCAL)) {
1437       UI_FontThemeColor(font_id, TH_TIME_KEYFRAME);
1438     }
1439     else if (ED_gpencil_has_keyframe_v3d(scene, ob, cfra)) {
1440       UI_FontThemeColor(font_id, TH_TIME_GP_KEYFRAME);
1441     }
1442     else {
1443       UI_FontThemeColor(font_id, TH_TEXT_HI);
1444     }
1445   }
1446   else {
1447     /* no object */
1448     if (ED_gpencil_has_keyframe_v3d(scene, NULL, cfra)) {
1449       UI_FontThemeColor(font_id, TH_TIME_GP_KEYFRAME);
1450     }
1451     else {
1452       UI_FontThemeColor(font_id, TH_TEXT_HI);
1453     }
1454   }
1455 
1456   if (markern) {
1457     s += sprintf(s, " <%s>", markern);
1458   }
1459 
1460   BLF_enable(font_id, BLF_SHADOW);
1461   BLF_shadow(font_id, 5, (const float[4]){0.0f, 0.0f, 0.0f, 1.0f});
1462   BLF_shadow_offset(font_id, 1, -1);
1463 
1464   *yoffset -= VIEW3D_OVERLAY_LINEHEIGHT;
1465   BLF_draw_default(xoffset, *yoffset, 0.0f, info, sizeof(info));
1466 
1467   BLF_disable(font_id, BLF_SHADOW);
1468 }
1469 
draw_grid_unit_name(Scene * scene,RegionView3D * rv3d,View3D * v3d,int xoffset,int * yoffset)1470 static void draw_grid_unit_name(
1471     Scene *scene, RegionView3D *rv3d, View3D *v3d, int xoffset, int *yoffset)
1472 {
1473   if (!rv3d->is_persp && RV3D_VIEW_IS_AXIS(rv3d->view)) {
1474     const char *grid_unit = NULL;
1475     int font_id = BLF_default();
1476     ED_view3d_grid_view_scale(scene, v3d, rv3d, &grid_unit);
1477 
1478     if (grid_unit) {
1479       char numstr[32] = "";
1480       UI_FontThemeColor(font_id, TH_TEXT_HI);
1481       if (v3d->grid != 1.0f) {
1482         BLI_snprintf(numstr, sizeof(numstr), "%s x %.4g", grid_unit, v3d->grid);
1483       }
1484 
1485       *yoffset -= VIEW3D_OVERLAY_LINEHEIGHT;
1486       BLF_enable(font_id, BLF_SHADOW);
1487       BLF_shadow(font_id, 5, (const float[4]){0.0f, 0.0f, 0.0f, 1.0f});
1488       BLF_shadow_offset(font_id, 1, -1);
1489       BLF_draw_default_ascii(
1490           xoffset, *yoffset, 0.0f, numstr[0] ? numstr : grid_unit, sizeof(numstr));
1491 
1492       BLF_disable(font_id, BLF_SHADOW);
1493     }
1494   }
1495 }
1496 
1497 /**
1498  * Information drawn on top of the solid plates and composed data
1499  */
view3d_draw_region_info(const bContext * C,ARegion * region)1500 void view3d_draw_region_info(const bContext *C, ARegion *region)
1501 {
1502   RegionView3D *rv3d = region->regiondata;
1503   View3D *v3d = CTX_wm_view3d(C);
1504   Scene *scene = CTX_data_scene(C);
1505   wmWindowManager *wm = CTX_wm_manager(C);
1506   Main *bmain = CTX_data_main(C);
1507   ViewLayer *view_layer = CTX_data_view_layer(C);
1508 
1509 #ifdef WITH_INPUT_NDOF
1510   if ((U.ndof_flag & NDOF_SHOW_GUIDE) && ((RV3D_LOCK_FLAGS(rv3d) & RV3D_LOCK_ROTATION) == 0) &&
1511       (rv3d->persp != RV3D_CAMOB)) {
1512     /* TODO: draw something else (but not this) during fly mode */
1513     draw_rotation_guide(rv3d);
1514   }
1515 #endif
1516 
1517   /* correct projection matrix */
1518   ED_region_pixelspace(region);
1519 
1520   /* local coordinate visible rect inside region, to accommodate overlapping ui */
1521   const rcti *rect = ED_region_visible_rect(region);
1522 
1523   view3d_draw_border(C, region);
1524   view3d_draw_grease_pencil(C);
1525 
1526   BLF_batch_draw_begin();
1527 
1528   if (v3d->gizmo_flag & (V3D_GIZMO_HIDE | V3D_GIZMO_HIDE_NAVIGATE)) {
1529     /* pass */
1530   }
1531   else {
1532     switch ((eUserpref_MiniAxisType)U.mini_axis_type) {
1533       case USER_MINI_AXIS_TYPE_GIZMO:
1534         /* The gizmo handles its own drawing. */
1535         break;
1536       case USER_MINI_AXIS_TYPE_MINIMAL:
1537         draw_view_axis(rv3d, rect);
1538       case USER_MINI_AXIS_TYPE_NONE:
1539         break;
1540     }
1541   }
1542 
1543   int xoffset = rect->xmin + (0.5f * U.widget_unit);
1544   int yoffset = rect->ymax - (0.1f * U.widget_unit);
1545 
1546   if ((v3d->flag2 & V3D_HIDE_OVERLAYS) == 0 && (v3d->overlay.flag & V3D_OVERLAY_HIDE_TEXT) == 0) {
1547     if ((U.uiflag & USER_SHOW_FPS) && ED_screen_animation_no_scrub(wm)) {
1548       ED_scene_draw_fps(scene, xoffset, &yoffset);
1549     }
1550     else if (U.uiflag & USER_SHOW_VIEWPORTNAME) {
1551       draw_viewport_name(region, v3d, xoffset, &yoffset);
1552     }
1553 
1554     if (U.uiflag & USER_DRAWVIEWINFO) {
1555       Object *ob = OBACT(view_layer);
1556       draw_selected_name(scene, view_layer, ob, xoffset, &yoffset);
1557     }
1558 
1559     if (v3d->gridflag & (V3D_SHOW_FLOOR | V3D_SHOW_X | V3D_SHOW_Y | V3D_SHOW_Z)) {
1560       /* draw below the viewport name */
1561       draw_grid_unit_name(scene, rv3d, v3d, xoffset, &yoffset);
1562     }
1563 
1564     DRW_draw_region_engine_info(xoffset, &yoffset, VIEW3D_OVERLAY_LINEHEIGHT);
1565   }
1566 
1567   if ((v3d->flag2 & V3D_HIDE_OVERLAYS) == 0 && (v3d->overlay.flag & V3D_OVERLAY_STATS)) {
1568     ED_info_draw_stats(bmain, scene, view_layer, xoffset, &yoffset, VIEW3D_OVERLAY_LINEHEIGHT);
1569   }
1570 
1571   BLF_batch_draw_end();
1572 }
1573 
1574 /** \} */
1575 
1576 /* -------------------------------------------------------------------- */
1577 /** \name Draw Viewport Contents
1578  * \{ */
1579 
view3d_draw_view(const bContext * C,ARegion * region)1580 static void view3d_draw_view(const bContext *C, ARegion *region)
1581 {
1582   ED_view3d_draw_setup_view(CTX_wm_manager(C),
1583                             CTX_wm_window(C),
1584                             CTX_data_expect_evaluated_depsgraph(C),
1585                             CTX_data_scene(C),
1586                             region,
1587                             CTX_wm_view3d(C),
1588                             NULL,
1589                             NULL,
1590                             NULL);
1591 
1592   /* Only 100% compliant on new spec goes below */
1593   DRW_draw_view(C);
1594 }
1595 
ED_view3d_engine_type(const Scene * scene,int drawtype)1596 RenderEngineType *ED_view3d_engine_type(const Scene *scene, int drawtype)
1597 {
1598   /*
1599    * Temporary viewport draw modes until we have a proper system.
1600    * all modes are done in the draw manager, except external render
1601    * engines like Cycles.
1602    */
1603   RenderEngineType *type = RE_engines_find(scene->r.engine);
1604   if (drawtype == OB_MATERIAL && (type->flag & RE_USE_EEVEE_VIEWPORT)) {
1605     return RE_engines_find(RE_engine_id_BLENDER_EEVEE);
1606   }
1607   return type;
1608 }
1609 
view3d_main_region_draw(const bContext * C,ARegion * region)1610 void view3d_main_region_draw(const bContext *C, ARegion *region)
1611 {
1612   Main *bmain = CTX_data_main(C);
1613   View3D *v3d = CTX_wm_view3d(C);
1614 
1615   view3d_draw_view(C, region);
1616 
1617   DRW_cache_free_old_batches(bmain);
1618   BKE_image_free_old_gputextures(bmain);
1619   GPU_pass_cache_garbage_collect();
1620 
1621   /* No depth test for drawing action zones afterwards. */
1622   GPU_depth_test(GPU_DEPTH_NONE);
1623 
1624   v3d->flag |= V3D_INVALID_BACKBUF;
1625 }
1626 
1627 /** \} */
1628 
1629 /* -------------------------------------------------------------------- */
1630 /** \name Offscreen Drawing
1631  * \{ */
1632 
view3d_stereo3d_setup_offscreen(Depsgraph * depsgraph,const Scene * scene,View3D * v3d,ARegion * region,const float winmat[4][4],const char * viewname)1633 static void view3d_stereo3d_setup_offscreen(Depsgraph *depsgraph,
1634                                             const Scene *scene,
1635                                             View3D *v3d,
1636                                             ARegion *region,
1637                                             const float winmat[4][4],
1638                                             const char *viewname)
1639 {
1640   /* update the viewport matrices with the new camera */
1641   if (scene->r.views_format == SCE_VIEWS_FORMAT_STEREO_3D) {
1642     float viewmat[4][4];
1643     const bool is_left = STREQ(viewname, STEREO_LEFT_NAME);
1644 
1645     BKE_camera_multiview_view_matrix(&scene->r, v3d->camera, is_left, viewmat);
1646     view3d_main_region_setup_offscreen(depsgraph, scene, v3d, region, viewmat, winmat);
1647   }
1648   else { /* SCE_VIEWS_FORMAT_MULTIVIEW */
1649     float viewmat[4][4];
1650     Object *camera = BKE_camera_multiview_render(scene, v3d->camera, viewname);
1651 
1652     BKE_camera_multiview_view_matrix(&scene->r, camera, false, viewmat);
1653     view3d_main_region_setup_offscreen(depsgraph, scene, v3d, region, viewmat, winmat);
1654   }
1655 }
1656 
ED_view3d_draw_offscreen(Depsgraph * depsgraph,const Scene * scene,eDrawType drawtype,View3D * v3d,ARegion * region,int winx,int winy,const float viewmat[4][4],const float winmat[4][4],bool is_image_render,bool do_sky,bool UNUSED (is_persp),const char * viewname,const bool do_color_management,GPUOffScreen * ofs,GPUViewport * viewport)1657 void ED_view3d_draw_offscreen(Depsgraph *depsgraph,
1658                               const Scene *scene,
1659                               eDrawType drawtype,
1660                               View3D *v3d,
1661                               ARegion *region,
1662                               int winx,
1663                               int winy,
1664                               const float viewmat[4][4],
1665                               const float winmat[4][4],
1666                               bool is_image_render,
1667                               bool do_sky,
1668                               bool UNUSED(is_persp),
1669                               const char *viewname,
1670                               const bool do_color_management,
1671                               GPUOffScreen *ofs,
1672                               GPUViewport *viewport)
1673 {
1674   RegionView3D *rv3d = region->regiondata;
1675   RenderEngineType *engine_type = ED_view3d_engine_type(scene, drawtype);
1676 
1677   /* set temporary new size */
1678   int bwinx = region->winx;
1679   int bwiny = region->winy;
1680   rcti brect = region->winrct;
1681 
1682   region->winx = winx;
1683   region->winy = winy;
1684   region->winrct.xmin = 0;
1685   region->winrct.ymin = 0;
1686   region->winrct.xmax = winx;
1687   region->winrct.ymax = winy;
1688 
1689   struct bThemeState theme_state;
1690   UI_Theme_Store(&theme_state);
1691   UI_SetTheme(SPACE_VIEW3D, RGN_TYPE_WINDOW);
1692 
1693   /* set flags */
1694   G.f |= G_FLAG_RENDER_VIEWPORT;
1695 
1696   /* There are too many functions inside the draw manager that check the shading type,
1697    * so use a temporary override instead. */
1698   const eDrawType drawtype_orig = v3d->shading.type;
1699   v3d->shading.type = drawtype;
1700 
1701   {
1702     /* free images which can have changed on frame-change
1703      * warning! can be slow so only free animated images - campbell */
1704     BKE_image_free_anim_gputextures(G.main); /* XXX :((( */
1705   }
1706 
1707   GPU_matrix_push_projection();
1708   GPU_matrix_identity_set();
1709   GPU_matrix_push();
1710   GPU_matrix_identity_set();
1711 
1712   if ((viewname != NULL && viewname[0] != '\0') && (viewmat == NULL) &&
1713       rv3d->persp == RV3D_CAMOB && v3d->camera) {
1714     view3d_stereo3d_setup_offscreen(depsgraph, scene, v3d, region, winmat, viewname);
1715   }
1716   else {
1717     view3d_main_region_setup_offscreen(depsgraph, scene, v3d, region, viewmat, winmat);
1718   }
1719 
1720   /* main drawing call */
1721   DRW_draw_render_loop_offscreen(depsgraph,
1722                                  engine_type,
1723                                  region,
1724                                  v3d,
1725                                  is_image_render,
1726                                  do_sky,
1727                                  do_color_management,
1728                                  ofs,
1729                                  viewport);
1730 
1731   /* restore size */
1732   region->winx = bwinx;
1733   region->winy = bwiny;
1734   region->winrct = brect;
1735 
1736   GPU_matrix_pop_projection();
1737   GPU_matrix_pop();
1738 
1739   UI_Theme_Restore(&theme_state);
1740 
1741   v3d->shading.type = drawtype_orig;
1742   G.f &= ~G_FLAG_RENDER_VIEWPORT;
1743 }
1744 
1745 /**
1746  * Creates own fake 3d views (wrapping #ED_view3d_draw_offscreen). Similar too
1747  * #ED_view_draw_offscreen_imbuf_simple, but takes view/projection matrices as arguments.
1748  */
ED_view3d_draw_offscreen_simple(Depsgraph * depsgraph,Scene * scene,View3DShading * shading_override,int drawtype,int winx,int winy,uint draw_flags,const float viewmat[4][4],const float winmat[4][4],float clip_start,float clip_end,bool is_image_render,bool do_sky,bool is_persp,const char * viewname,const bool do_color_management,GPUOffScreen * ofs,GPUViewport * viewport)1749 void ED_view3d_draw_offscreen_simple(Depsgraph *depsgraph,
1750                                      Scene *scene,
1751                                      View3DShading *shading_override,
1752                                      int drawtype,
1753                                      int winx,
1754                                      int winy,
1755                                      uint draw_flags,
1756                                      const float viewmat[4][4],
1757                                      const float winmat[4][4],
1758                                      float clip_start,
1759                                      float clip_end,
1760                                      bool is_image_render,
1761                                      bool do_sky,
1762                                      bool is_persp,
1763                                      const char *viewname,
1764                                      const bool do_color_management,
1765                                      GPUOffScreen *ofs,
1766                                      GPUViewport *viewport)
1767 {
1768   View3D v3d = {NULL};
1769   ARegion ar = {NULL};
1770   RegionView3D rv3d = {{{0}}};
1771 
1772   v3d.regionbase.first = v3d.regionbase.last = &ar;
1773   ar.regiondata = &rv3d;
1774   ar.regiontype = RGN_TYPE_WINDOW;
1775 
1776   View3DShading *source_shading_settings = &scene->display.shading;
1777   if (draw_flags & V3D_OFSDRAW_OVERRIDE_SCENE_SETTINGS && shading_override != NULL) {
1778     source_shading_settings = shading_override;
1779   }
1780   memcpy(&v3d.shading, source_shading_settings, sizeof(View3DShading));
1781   v3d.shading.type = drawtype;
1782 
1783   if (shading_override) {
1784     /* Pass. */
1785   }
1786   else if (drawtype == OB_MATERIAL) {
1787     v3d.shading.flag = V3D_SHADING_SCENE_WORLD | V3D_SHADING_SCENE_LIGHTS;
1788   }
1789 
1790   if (draw_flags & V3D_OFSDRAW_SHOW_ANNOTATION) {
1791     v3d.flag2 |= V3D_SHOW_ANNOTATION;
1792   }
1793   if (draw_flags & V3D_OFSDRAW_SHOW_GRIDFLOOR) {
1794     v3d.gridflag |= V3D_SHOW_FLOOR | V3D_SHOW_X | V3D_SHOW_Y;
1795     v3d.grid = 1.0f;
1796     v3d.gridlines = 16;
1797     v3d.gridsubdiv = 10;
1798 
1799     /* Show grid, disable other overlays (set all available _HIDE_ flags). */
1800     v3d.overlay.flag |= V3D_OVERLAY_HIDE_CURSOR | V3D_OVERLAY_HIDE_TEXT |
1801                         V3D_OVERLAY_HIDE_MOTION_PATHS | V3D_OVERLAY_HIDE_BONES |
1802                         V3D_OVERLAY_HIDE_OBJECT_XTRAS | V3D_OVERLAY_HIDE_OBJECT_ORIGINS;
1803     v3d.flag |= V3D_HIDE_HELPLINES;
1804   }
1805   else {
1806     v3d.flag2 = V3D_HIDE_OVERLAYS;
1807   }
1808 
1809   rv3d.persp = RV3D_PERSP;
1810   v3d.clip_start = clip_start;
1811   v3d.clip_end = clip_end;
1812   /* Actually not used since we pass in the projection matrix. */
1813   v3d.lens = 0;
1814 
1815   ED_view3d_draw_offscreen(depsgraph,
1816                            scene,
1817                            drawtype,
1818                            &v3d,
1819                            &ar,
1820                            winx,
1821                            winy,
1822                            viewmat,
1823                            winmat,
1824                            is_image_render,
1825                            do_sky,
1826                            is_persp,
1827                            viewname,
1828                            do_color_management,
1829                            ofs,
1830                            viewport);
1831 }
1832 
1833 /**
1834  * Utility func for ED_view3d_draw_offscreen
1835  *
1836  * \param ofs: Optional off-screen buffer, can be NULL.
1837  * (avoids re-creating when doing multiple GL renders).
1838  */
ED_view3d_draw_offscreen_imbuf(Depsgraph * depsgraph,Scene * scene,eDrawType drawtype,View3D * v3d,ARegion * region,int sizex,int sizey,eImBufFlags imbuf_flag,int alpha_mode,const char * viewname,GPUOffScreen * ofs,char err_out[256])1839 ImBuf *ED_view3d_draw_offscreen_imbuf(Depsgraph *depsgraph,
1840                                       Scene *scene,
1841                                       eDrawType drawtype,
1842                                       View3D *v3d,
1843                                       ARegion *region,
1844                                       int sizex,
1845                                       int sizey,
1846                                       eImBufFlags imbuf_flag,
1847                                       int alpha_mode,
1848                                       const char *viewname,
1849                                       /* output vars */
1850                                       GPUOffScreen *ofs,
1851                                       char err_out[256])
1852 {
1853   RegionView3D *rv3d = region->regiondata;
1854   const bool draw_sky = (alpha_mode == R_ADDSKY);
1855 
1856   /* view state */
1857   bool is_ortho = false;
1858   float winmat[4][4];
1859 
1860   if (ofs && ((GPU_offscreen_width(ofs) != sizex) || (GPU_offscreen_height(ofs) != sizey))) {
1861     /* sizes differ, can't reuse */
1862     ofs = NULL;
1863   }
1864 
1865   GPUFrameBuffer *old_fb = GPU_framebuffer_active_get();
1866 
1867   if (old_fb) {
1868     GPU_framebuffer_restore();
1869   }
1870 
1871   const bool own_ofs = (ofs == NULL);
1872   DRW_opengl_context_enable();
1873 
1874   if (own_ofs) {
1875     /* bind */
1876     ofs = GPU_offscreen_create(sizex, sizey, true, false, err_out);
1877     if (ofs == NULL) {
1878       DRW_opengl_context_disable();
1879       return NULL;
1880     }
1881   }
1882 
1883   GPU_offscreen_bind(ofs, true);
1884 
1885   /* read in pixels & stamp */
1886   ImBuf *ibuf = IMB_allocImBuf(sizex, sizey, 32, imbuf_flag);
1887 
1888   /* render 3d view */
1889   if (rv3d->persp == RV3D_CAMOB && v3d->camera) {
1890     CameraParams params;
1891     Object *camera = BKE_camera_multiview_render(scene, v3d->camera, viewname);
1892     const Object *camera_eval = DEG_get_evaluated_object(depsgraph, camera);
1893 
1894     BKE_camera_params_init(&params);
1895     /* fallback for non camera objects */
1896     params.clip_start = v3d->clip_start;
1897     params.clip_end = v3d->clip_end;
1898     BKE_camera_params_from_object(&params, camera_eval);
1899     BKE_camera_multiview_params(&scene->r, &params, camera_eval, viewname);
1900     BKE_camera_params_compute_viewplane(&params, sizex, sizey, scene->r.xasp, scene->r.yasp);
1901     BKE_camera_params_compute_matrix(&params);
1902 
1903     is_ortho = params.is_ortho;
1904     copy_m4_m4(winmat, params.winmat);
1905   }
1906   else {
1907     rctf viewplane;
1908     float clip_start, clipend;
1909 
1910     is_ortho = ED_view3d_viewplane_get(
1911         depsgraph, v3d, rv3d, sizex, sizey, &viewplane, &clip_start, &clipend, NULL);
1912     if (is_ortho) {
1913       orthographic_m4(winmat,
1914                       viewplane.xmin,
1915                       viewplane.xmax,
1916                       viewplane.ymin,
1917                       viewplane.ymax,
1918                       -clipend,
1919                       clipend);
1920     }
1921     else {
1922       perspective_m4(winmat,
1923                      viewplane.xmin,
1924                      viewplane.xmax,
1925                      viewplane.ymin,
1926                      viewplane.ymax,
1927                      clip_start,
1928                      clipend);
1929     }
1930   }
1931 
1932   const bool do_color_management = (ibuf->rect_float == NULL);
1933   ED_view3d_draw_offscreen(depsgraph,
1934                            scene,
1935                            drawtype,
1936                            v3d,
1937                            region,
1938                            sizex,
1939                            sizey,
1940                            NULL,
1941                            winmat,
1942                            true,
1943                            draw_sky,
1944                            !is_ortho,
1945                            viewname,
1946                            do_color_management,
1947                            ofs,
1948                            NULL);
1949 
1950   if (ibuf->rect_float) {
1951     GPU_offscreen_read_pixels(ofs, GPU_DATA_FLOAT, ibuf->rect_float);
1952   }
1953   else if (ibuf->rect) {
1954     GPU_offscreen_read_pixels(ofs, GPU_DATA_UNSIGNED_BYTE, ibuf->rect);
1955   }
1956 
1957   /* unbind */
1958   GPU_offscreen_unbind(ofs, true);
1959 
1960   if (own_ofs) {
1961     GPU_offscreen_free(ofs);
1962   }
1963 
1964   DRW_opengl_context_disable();
1965 
1966   if (old_fb) {
1967     GPU_framebuffer_bind(old_fb);
1968   }
1969 
1970   if (ibuf->rect_float && ibuf->rect) {
1971     IMB_rect_from_float(ibuf);
1972   }
1973 
1974   return ibuf;
1975 }
1976 
1977 /**
1978  * Creates own fake 3d views (wrapping #ED_view3d_draw_offscreen_imbuf)
1979  *
1980  * \param ofs: Optional off-screen buffer can be NULL.
1981  * (avoids re-creating when doing multiple GL renders).
1982  *
1983  * \note used by the sequencer
1984  */
ED_view3d_draw_offscreen_imbuf_simple(Depsgraph * depsgraph,Scene * scene,View3DShading * shading_override,eDrawType drawtype,Object * camera,int width,int height,eImBufFlags imbuf_flag,eV3DOffscreenDrawFlag draw_flags,int alpha_mode,const char * viewname,GPUOffScreen * ofs,char err_out[256])1985 ImBuf *ED_view3d_draw_offscreen_imbuf_simple(Depsgraph *depsgraph,
1986                                              Scene *scene,
1987                                              View3DShading *shading_override,
1988                                              eDrawType drawtype,
1989                                              Object *camera,
1990                                              int width,
1991                                              int height,
1992                                              eImBufFlags imbuf_flag,
1993                                              eV3DOffscreenDrawFlag draw_flags,
1994                                              int alpha_mode,
1995                                              const char *viewname,
1996                                              GPUOffScreen *ofs,
1997                                              char err_out[256])
1998 {
1999   View3D v3d = {NULL};
2000   ARegion region = {NULL};
2001   RegionView3D rv3d = {{{0}}};
2002 
2003   /* connect data */
2004   v3d.regionbase.first = v3d.regionbase.last = &region;
2005   region.regiondata = &rv3d;
2006   region.regiontype = RGN_TYPE_WINDOW;
2007 
2008   v3d.camera = camera;
2009   View3DShading *source_shading_settings = &scene->display.shading;
2010   if (draw_flags & V3D_OFSDRAW_OVERRIDE_SCENE_SETTINGS && shading_override != NULL) {
2011     source_shading_settings = shading_override;
2012   }
2013   memcpy(&v3d.shading, source_shading_settings, sizeof(View3DShading));
2014   v3d.shading.type = drawtype;
2015 
2016   if (drawtype == OB_MATERIAL) {
2017     v3d.shading.flag = V3D_SHADING_SCENE_WORLD | V3D_SHADING_SCENE_LIGHTS;
2018     v3d.shading.render_pass = SCE_PASS_COMBINED;
2019   }
2020   else if (drawtype == OB_RENDER) {
2021     v3d.shading.flag = V3D_SHADING_SCENE_WORLD_RENDER | V3D_SHADING_SCENE_LIGHTS_RENDER;
2022     v3d.shading.render_pass = SCE_PASS_COMBINED;
2023   }
2024 
2025   v3d.flag2 = V3D_HIDE_OVERLAYS;
2026 
2027   if (draw_flags & V3D_OFSDRAW_SHOW_ANNOTATION) {
2028     v3d.flag2 |= V3D_SHOW_ANNOTATION;
2029   }
2030   if (draw_flags & V3D_OFSDRAW_SHOW_GRIDFLOOR) {
2031     v3d.gridflag |= V3D_SHOW_FLOOR | V3D_SHOW_X | V3D_SHOW_Y;
2032   }
2033 
2034   v3d.shading.background_type = V3D_SHADING_BACKGROUND_WORLD;
2035 
2036   rv3d.persp = RV3D_CAMOB;
2037 
2038   copy_m4_m4(rv3d.viewinv, v3d.camera->obmat);
2039   normalize_m4(rv3d.viewinv);
2040   invert_m4_m4(rv3d.viewmat, rv3d.viewinv);
2041 
2042   {
2043     CameraParams params;
2044     const Object *view_camera_eval = DEG_get_evaluated_object(
2045         depsgraph, BKE_camera_multiview_render(scene, v3d.camera, viewname));
2046 
2047     BKE_camera_params_init(&params);
2048     BKE_camera_params_from_object(&params, view_camera_eval);
2049     BKE_camera_multiview_params(&scene->r, &params, view_camera_eval, viewname);
2050     BKE_camera_params_compute_viewplane(&params, width, height, scene->r.xasp, scene->r.yasp);
2051     BKE_camera_params_compute_matrix(&params);
2052 
2053     copy_m4_m4(rv3d.winmat, params.winmat);
2054     v3d.clip_start = params.clip_start;
2055     v3d.clip_end = params.clip_end;
2056     v3d.lens = params.lens;
2057   }
2058 
2059   mul_m4_m4m4(rv3d.persmat, rv3d.winmat, rv3d.viewmat);
2060   invert_m4_m4(rv3d.persinv, rv3d.viewinv);
2061 
2062   return ED_view3d_draw_offscreen_imbuf(depsgraph,
2063                                         scene,
2064                                         drawtype,
2065                                         &v3d,
2066                                         &region,
2067                                         width,
2068                                         height,
2069                                         imbuf_flag,
2070                                         alpha_mode,
2071                                         viewname,
2072                                         ofs,
2073                                         err_out);
2074 }
2075 
2076 /** \} */
2077 
2078 /* -------------------------------------------------------------------- */
2079 /** \name Viewport Clipping
2080  * \{ */
2081 
view3d_clipping_test(const float co[3],const float clip[6][4])2082 static bool view3d_clipping_test(const float co[3], const float clip[6][4])
2083 {
2084   if (plane_point_side_v3(clip[0], co) > 0.0f) {
2085     if (plane_point_side_v3(clip[1], co) > 0.0f) {
2086       if (plane_point_side_v3(clip[2], co) > 0.0f) {
2087         if (plane_point_side_v3(clip[3], co) > 0.0f) {
2088           return false;
2089         }
2090       }
2091     }
2092   }
2093 
2094   return true;
2095 }
2096 
2097 /* For 'local' ED_view3d_clipping_local must run first
2098  * then all comparisons can be done in localspace. */
ED_view3d_clipping_test(const RegionView3D * rv3d,const float co[3],const bool is_local)2099 bool ED_view3d_clipping_test(const RegionView3D *rv3d, const float co[3], const bool is_local)
2100 {
2101   return view3d_clipping_test(co, is_local ? rv3d->clip_local : rv3d->clip);
2102 }
2103 
2104 /* *********************** backdraw for selection *************** */
2105 
2106 /**
2107  * \note Only use in object mode.
2108  */
validate_object_select_id(struct Depsgraph * depsgraph,ViewLayer * view_layer,ARegion * region,View3D * v3d,Object * obact)2109 static void validate_object_select_id(struct Depsgraph *depsgraph,
2110                                       ViewLayer *view_layer,
2111                                       ARegion *region,
2112                                       View3D *v3d,
2113                                       Object *obact)
2114 {
2115   Object *obact_eval = DEG_get_evaluated_object(depsgraph, obact);
2116 
2117   BLI_assert(region->regiontype == RGN_TYPE_WINDOW);
2118   UNUSED_VARS_NDEBUG(region);
2119 
2120   if (obact_eval && (obact_eval->mode & (OB_MODE_VERTEX_PAINT | OB_MODE_WEIGHT_PAINT) ||
2121                      BKE_paint_select_face_test(obact_eval))) {
2122     /* do nothing */
2123   }
2124   /* texture paint mode sampling */
2125   else if (obact_eval && (obact_eval->mode & OB_MODE_TEXTURE_PAINT) &&
2126            (v3d->shading.type > OB_WIRE)) {
2127     /* do nothing */
2128   }
2129   else if ((obact_eval && (obact_eval->mode & OB_MODE_PARTICLE_EDIT)) && !XRAY_ENABLED(v3d)) {
2130     /* do nothing */
2131   }
2132   else {
2133     v3d->flag &= ~V3D_INVALID_BACKBUF;
2134     return;
2135   }
2136 
2137   if (!(v3d->flag & V3D_INVALID_BACKBUF)) {
2138     return;
2139   }
2140 
2141   if (obact_eval && ((obact_eval->base_flag & BASE_VISIBLE_DEPSGRAPH) != 0)) {
2142     Base *base = BKE_view_layer_base_find(view_layer, obact);
2143     DRW_select_buffer_context_create(&base, 1, -1);
2144   }
2145 
2146   /* TODO: Create a flag in `DRW_manager` because the drawing is no longer
2147    *       made on the backbuffer in this case. */
2148   v3d->flag &= ~V3D_INVALID_BACKBUF;
2149 }
2150 
2151 /* TODO: Creating, attaching texture, and destroying a framebuffer is quite slow.
2152  *       Calling this function should be avoided during interactive drawing. */
view3d_opengl_read_Z_pixels(GPUViewport * viewport,rcti * rect,void * data)2153 static void view3d_opengl_read_Z_pixels(GPUViewport *viewport, rcti *rect, void *data)
2154 {
2155   DefaultTextureList *dtxl = (DefaultTextureList *)GPU_viewport_texture_list_get(viewport);
2156 
2157   GPUFrameBuffer *tmp_fb = GPU_framebuffer_create(__func__);
2158   GPU_framebuffer_texture_attach(tmp_fb, dtxl->depth, 0, 0);
2159   GPU_framebuffer_bind(tmp_fb);
2160 
2161   GPU_framebuffer_read_depth(tmp_fb,
2162                              rect->xmin,
2163                              rect->ymin,
2164                              BLI_rcti_size_x(rect),
2165                              BLI_rcti_size_y(rect),
2166                              GPU_DATA_FLOAT,
2167                              data);
2168 
2169   GPU_framebuffer_restore();
2170   GPU_framebuffer_free(tmp_fb);
2171 }
2172 
ED_view3d_select_id_validate(ViewContext * vc)2173 void ED_view3d_select_id_validate(ViewContext *vc)
2174 {
2175   /* TODO: Create a flag in `DRW_manager` because the drawing is no longer
2176    *       made on the backbuffer in this case. */
2177   if (vc->v3d->flag & V3D_INVALID_BACKBUF) {
2178     validate_object_select_id(vc->depsgraph, vc->view_layer, vc->region, vc->v3d, vc->obact);
2179   }
2180 }
2181 
ED_view3d_backbuf_depth_validate(ViewContext * vc)2182 void ED_view3d_backbuf_depth_validate(ViewContext *vc)
2183 {
2184   if (vc->v3d->flag & V3D_INVALID_BACKBUF) {
2185     ARegion *region = vc->region;
2186     Object *obact_eval = DEG_get_evaluated_object(vc->depsgraph, vc->obact);
2187 
2188     if (obact_eval && ((obact_eval->base_flag & BASE_VISIBLE_DEPSGRAPH) != 0)) {
2189       GPUViewport *viewport = WM_draw_region_get_viewport(region);
2190       DRW_draw_depth_object(vc->scene, vc->region, vc->v3d, viewport, obact_eval);
2191     }
2192 
2193     vc->v3d->flag &= ~V3D_INVALID_BACKBUF;
2194   }
2195 }
2196 
2197 /**
2198  * allow for small values [0.5 - 2.5],
2199  * and large values, FLT_MAX by clamping by the area size
2200  */
ED_view3d_backbuf_sample_size_clamp(ARegion * region,const float dist)2201 int ED_view3d_backbuf_sample_size_clamp(ARegion *region, const float dist)
2202 {
2203   return (int)min_ff(ceilf(dist), (float)max_ii(region->winx, region->winx));
2204 }
2205 
2206 /* *********************** */
2207 
view3d_update_depths_rect(ARegion * region,ViewDepths * d,rcti * rect)2208 void view3d_update_depths_rect(ARegion *region, ViewDepths *d, rcti *rect)
2209 {
2210   /* clamp rect by region */
2211   rcti r = {
2212       .xmin = 0,
2213       .xmax = region->winx - 1,
2214       .ymin = 0,
2215       .ymax = region->winy - 1,
2216   };
2217 
2218   /* Constrain rect to depth bounds */
2219   BLI_rcti_isect(&r, rect, rect);
2220 
2221   /* assign values to compare with the ViewDepths */
2222   int x = rect->xmin;
2223   int y = rect->ymin;
2224 
2225   int w = BLI_rcti_size_x(rect);
2226   int h = BLI_rcti_size_y(rect);
2227 
2228   if (w <= 0 || h <= 0) {
2229     if (d->depths) {
2230       MEM_freeN(d->depths);
2231     }
2232     d->depths = NULL;
2233 
2234     d->damaged = false;
2235   }
2236   else if (d->w != w || d->h != h || d->x != x || d->y != y || d->depths == NULL) {
2237     d->x = x;
2238     d->y = y;
2239     d->w = w;
2240     d->h = h;
2241 
2242     if (d->depths) {
2243       MEM_freeN(d->depths);
2244     }
2245 
2246     d->depths = MEM_mallocN(sizeof(float) * d->w * d->h, "View depths Subset");
2247 
2248     d->damaged = true;
2249   }
2250 
2251   if (d->damaged) {
2252     GPUViewport *viewport = WM_draw_region_get_viewport(region);
2253     view3d_opengl_read_Z_pixels(viewport, rect, d->depths);
2254     /* Range is assumed to be this as they are never changed. */
2255     d->depth_range[0] = 0.0;
2256     d->depth_range[1] = 1.0;
2257     d->damaged = false;
2258   }
2259 }
2260 
2261 /* Note, with nouveau drivers the glReadPixels() is very slow. T24339. */
ED_view3d_depth_update(ARegion * region)2262 void ED_view3d_depth_update(ARegion *region)
2263 {
2264   RegionView3D *rv3d = region->regiondata;
2265 
2266   /* Create storage for, and, if necessary, copy depth buffer. */
2267   if (!rv3d->depths) {
2268     rv3d->depths = MEM_callocN(sizeof(ViewDepths), "ViewDepths");
2269   }
2270   if (rv3d->depths) {
2271     ViewDepths *d = rv3d->depths;
2272     if (d->w != region->winx || d->h != region->winy || !d->depths) {
2273       d->w = region->winx;
2274       d->h = region->winy;
2275       if (d->depths) {
2276         MEM_freeN(d->depths);
2277       }
2278       d->depths = MEM_mallocN(sizeof(float) * d->w * d->h, "View depths");
2279       d->damaged = true;
2280     }
2281 
2282     if (d->damaged) {
2283       GPUViewport *viewport = WM_draw_region_get_viewport(region);
2284       rcti r = {
2285           .xmin = 0,
2286           .xmax = d->w,
2287           .ymin = 0,
2288           .ymax = d->h,
2289       };
2290       view3d_opengl_read_Z_pixels(viewport, &r, d->depths);
2291       /* Assumed to be this as they are never changed. */
2292       d->depth_range[0] = 0.0;
2293       d->depth_range[1] = 1.0;
2294       d->damaged = false;
2295     }
2296   }
2297 }
2298 
2299 /* Utility function to find the closest Z value, use for autodepth. */
view3d_depth_near(ViewDepths * d)2300 float view3d_depth_near(ViewDepths *d)
2301 {
2302   /* Convert to float for comparisons. */
2303   const float near = (float)d->depth_range[0];
2304   const float far_real = (float)d->depth_range[1];
2305   float far = far_real;
2306 
2307   const float *depths = d->depths;
2308   float depth = FLT_MAX;
2309   int i = (int)d->w * (int)d->h; /* Cast to avoid short overflow. */
2310 
2311   /* Far is both the starting 'far' value
2312    * and the closest value found. */
2313   while (i--) {
2314     depth = *depths++;
2315     if ((depth < far) && (depth > near)) {
2316       far = depth;
2317     }
2318   }
2319 
2320   return far == far_real ? FLT_MAX : far;
2321 }
2322 
ED_view3d_draw_depth_gpencil(Depsgraph * depsgraph,Scene * scene,ARegion * region,View3D * v3d)2323 void ED_view3d_draw_depth_gpencil(Depsgraph *depsgraph, Scene *scene, ARegion *region, View3D *v3d)
2324 {
2325   /* Setup view matrix. */
2326   ED_view3d_draw_setup_view(NULL, NULL, depsgraph, scene, region, v3d, NULL, NULL, NULL);
2327 
2328   GPU_clear_depth(1.0f);
2329 
2330   GPU_depth_test(GPU_DEPTH_LESS_EQUAL);
2331 
2332   GPUViewport *viewport = WM_draw_region_get_viewport(region);
2333   DRW_draw_depth_loop_gpencil(depsgraph, region, v3d, viewport);
2334 
2335   GPU_depth_test(GPU_DEPTH_NONE);
2336 }
2337 
2338 /* *********************** customdata **************** */
2339 
ED_view3d_datamask(const bContext * C,const Scene * UNUSED (scene),const View3D * v3d,CustomData_MeshMasks * r_cddata_masks)2340 void ED_view3d_datamask(const bContext *C,
2341                         const Scene *UNUSED(scene),
2342                         const View3D *v3d,
2343                         CustomData_MeshMasks *r_cddata_masks)
2344 {
2345   if (ELEM(v3d->shading.type, OB_TEXTURE, OB_MATERIAL, OB_RENDER)) {
2346     r_cddata_masks->lmask |= CD_MASK_MLOOPUV | CD_MASK_MLOOPCOL;
2347     r_cddata_masks->vmask |= CD_MASK_ORCO | CD_MASK_PROP_COLOR;
2348   }
2349   else if (v3d->shading.type == OB_SOLID) {
2350     if (v3d->shading.color_type == V3D_SHADING_TEXTURE_COLOR) {
2351       r_cddata_masks->lmask |= CD_MASK_MLOOPUV;
2352     }
2353     if (v3d->shading.color_type == V3D_SHADING_VERTEX_COLOR) {
2354       r_cddata_masks->lmask |= CD_MASK_MLOOPCOL;
2355       r_cddata_masks->vmask |= CD_MASK_ORCO | CD_MASK_PROP_COLOR;
2356     }
2357   }
2358 
2359   if ((CTX_data_mode_enum(C) == CTX_MODE_EDIT_MESH) &&
2360       (v3d->overlay.edit_flag & V3D_OVERLAY_EDIT_WEIGHT)) {
2361     r_cddata_masks->vmask |= CD_MASK_MDEFORMVERT;
2362   }
2363   if ((CTX_data_mode_enum(C) == CTX_MODE_SCULPT)) {
2364     r_cddata_masks->vmask |= CD_MASK_PAINT_MASK;
2365     r_cddata_masks->pmask |= CD_MASK_SCULPT_FACE_SETS;
2366   }
2367 }
2368 
2369 /* Goes over all modes and view3d settings. */
ED_view3d_screen_datamask(const bContext * C,const Scene * scene,const bScreen * screen,CustomData_MeshMasks * r_cddata_masks)2370 void ED_view3d_screen_datamask(const bContext *C,
2371                                const Scene *scene,
2372                                const bScreen *screen,
2373                                CustomData_MeshMasks *r_cddata_masks)
2374 {
2375   CustomData_MeshMasks_update(r_cddata_masks, &CD_MASK_BAREMESH);
2376 
2377   /* Check if we need tfaces & mcols due to view mode. */
2378   LISTBASE_FOREACH (const ScrArea *, area, &screen->areabase) {
2379     if (area->spacetype == SPACE_VIEW3D) {
2380       ED_view3d_datamask(C, scene, area->spacedata.first, r_cddata_masks);
2381     }
2382   }
2383 }
2384 
2385 /**
2386  * Store values from #RegionView3D, set when drawing.
2387  * This is needed when we draw with to a viewport using a different matrix
2388  * (offscreen drawing for example).
2389  *
2390  * Values set by #ED_view3d_update_viewmat should be handled here.
2391  */
2392 struct RV3DMatrixStore {
2393   float winmat[4][4];
2394   float viewmat[4][4];
2395   float viewinv[4][4];
2396   float persmat[4][4];
2397   float persinv[4][4];
2398   float viewcamtexcofac[4];
2399   float pixsize;
2400 };
2401 
ED_view3d_mats_rv3d_backup(struct RegionView3D * rv3d)2402 struct RV3DMatrixStore *ED_view3d_mats_rv3d_backup(struct RegionView3D *rv3d)
2403 {
2404   struct RV3DMatrixStore *rv3dmat = MEM_mallocN(sizeof(*rv3dmat), __func__);
2405   copy_m4_m4(rv3dmat->winmat, rv3d->winmat);
2406   copy_m4_m4(rv3dmat->viewmat, rv3d->viewmat);
2407   copy_m4_m4(rv3dmat->persmat, rv3d->persmat);
2408   copy_m4_m4(rv3dmat->persinv, rv3d->persinv);
2409   copy_m4_m4(rv3dmat->viewinv, rv3d->viewinv);
2410   copy_v4_v4(rv3dmat->viewcamtexcofac, rv3d->viewcamtexcofac);
2411   rv3dmat->pixsize = rv3d->pixsize;
2412   return rv3dmat;
2413 }
2414 
ED_view3d_mats_rv3d_restore(struct RegionView3D * rv3d,struct RV3DMatrixStore * rv3dmat_pt)2415 void ED_view3d_mats_rv3d_restore(struct RegionView3D *rv3d, struct RV3DMatrixStore *rv3dmat_pt)
2416 {
2417   struct RV3DMatrixStore *rv3dmat = rv3dmat_pt;
2418   copy_m4_m4(rv3d->winmat, rv3dmat->winmat);
2419   copy_m4_m4(rv3d->viewmat, rv3dmat->viewmat);
2420   copy_m4_m4(rv3d->persmat, rv3dmat->persmat);
2421   copy_m4_m4(rv3d->persinv, rv3dmat->persinv);
2422   copy_m4_m4(rv3d->viewinv, rv3dmat->viewinv);
2423   copy_v4_v4(rv3d->viewcamtexcofac, rv3dmat->viewcamtexcofac);
2424   rv3d->pixsize = rv3dmat->pixsize;
2425 }
2426 
2427 /**
2428  * \note The info that this uses is updated in #ED_refresh_viewport_fps,
2429  * which currently gets called during #SCREEN_OT_animation_step.
2430  */
ED_scene_draw_fps(const Scene * scene,int xoffset,int * yoffset)2431 void ED_scene_draw_fps(const Scene *scene, int xoffset, int *yoffset)
2432 {
2433   ScreenFrameRateInfo *fpsi = scene->fps_info;
2434   char printable[16];
2435 
2436   if (!fpsi || !fpsi->lredrawtime || !fpsi->redrawtime) {
2437     return;
2438   }
2439 
2440   printable[0] = '\0';
2441 
2442   /* Doing an average for a more robust calculation. */
2443   fpsi->redrawtimes_fps[fpsi->redrawtime_index] = (float)(1.0 /
2444                                                           (fpsi->lredrawtime - fpsi->redrawtime));
2445 
2446   float fps = 0.0f;
2447   int tot = 0;
2448   for (int i = 0; i < REDRAW_FRAME_AVERAGE; i++) {
2449     if (fpsi->redrawtimes_fps[i]) {
2450       fps += fpsi->redrawtimes_fps[i];
2451       tot++;
2452     }
2453   }
2454   if (tot) {
2455     fpsi->redrawtime_index = (fpsi->redrawtime_index + 1) % REDRAW_FRAME_AVERAGE;
2456     fps = fps / tot;
2457   }
2458 
2459   const int font_id = BLF_default();
2460 
2461   /* Is this more than half a frame behind? */
2462   if (fps + 0.5f < (float)(FPS)) {
2463     UI_FontThemeColor(font_id, TH_REDALERT);
2464     BLI_snprintf(printable, sizeof(printable), IFACE_("fps: %.2f"), fps);
2465   }
2466   else {
2467     UI_FontThemeColor(font_id, TH_TEXT_HI);
2468     BLI_snprintf(printable, sizeof(printable), IFACE_("fps: %i"), (int)(fps + 0.5f));
2469   }
2470 
2471   BLF_enable(font_id, BLF_SHADOW);
2472   BLF_shadow(font_id, 5, (const float[4]){0.0f, 0.0f, 0.0f, 1.0f});
2473   BLF_shadow_offset(font_id, 1, -1);
2474 
2475   *yoffset -= VIEW3D_OVERLAY_LINEHEIGHT;
2476 
2477 #ifdef WITH_INTERNATIONAL
2478   BLF_draw_default(xoffset, *yoffset, 0.0f, printable, sizeof(printable));
2479 #else
2480   BLF_draw_default_ascii(xoffset, *yoffset, 0.0f, printable, sizeof(printable));
2481 #endif
2482 
2483   BLF_disable(font_id, BLF_SHADOW);
2484 }
2485 
view3d_main_region_do_render_draw(const Scene * scene)2486 static bool view3d_main_region_do_render_draw(const Scene *scene)
2487 {
2488   RenderEngineType *type = RE_engines_find(scene->r.engine);
2489   return (type && type->view_update && type->view_draw);
2490 }
2491 
ED_view3d_calc_render_border(const Scene * scene,Depsgraph * depsgraph,View3D * v3d,ARegion * region,rcti * rect)2492 bool ED_view3d_calc_render_border(
2493     const Scene *scene, Depsgraph *depsgraph, View3D *v3d, ARegion *region, rcti *rect)
2494 {
2495   RegionView3D *rv3d = region->regiondata;
2496   bool use_border;
2497 
2498   /* Test if there is a 3d view rendering. */
2499   if (v3d->shading.type != OB_RENDER || !view3d_main_region_do_render_draw(scene)) {
2500     return false;
2501   }
2502 
2503   /* Test if there is a border render. */
2504   if (rv3d->persp == RV3D_CAMOB) {
2505     use_border = (scene->r.mode & R_BORDER) != 0;
2506   }
2507   else {
2508     use_border = (v3d->flag2 & V3D_RENDER_BORDER) != 0;
2509   }
2510 
2511   if (!use_border) {
2512     return false;
2513   }
2514 
2515   /* Compute border. */
2516   if (rv3d->persp == RV3D_CAMOB) {
2517     rctf viewborder;
2518     ED_view3d_calc_camera_border(scene, depsgraph, region, v3d, rv3d, &viewborder, false);
2519 
2520     rect->xmin = viewborder.xmin + scene->r.border.xmin * BLI_rctf_size_x(&viewborder);
2521     rect->ymin = viewborder.ymin + scene->r.border.ymin * BLI_rctf_size_y(&viewborder);
2522     rect->xmax = viewborder.xmin + scene->r.border.xmax * BLI_rctf_size_x(&viewborder);
2523     rect->ymax = viewborder.ymin + scene->r.border.ymax * BLI_rctf_size_y(&viewborder);
2524   }
2525   else {
2526     rect->xmin = v3d->render_border.xmin * region->winx;
2527     rect->xmax = v3d->render_border.xmax * region->winx;
2528     rect->ymin = v3d->render_border.ymin * region->winy;
2529     rect->ymax = v3d->render_border.ymax * region->winy;
2530   }
2531 
2532   BLI_rcti_translate(rect, region->winrct.xmin, region->winrct.ymin);
2533   BLI_rcti_isect(&region->winrct, rect, rect);
2534 
2535   return true;
2536 }
2537 
2538 /** \} */
2539