Merge branch 'master' into blender2.8
authorCampbell Barton <ideasman42@gmail.com>
Sun, 18 Feb 2018 11:33:05 +0000 (22:33 +1100)
committerCampbell Barton <ideasman42@gmail.com>
Sun, 18 Feb 2018 11:33:05 +0000 (22:33 +1100)
26 files changed:
1  2 
CMakeLists.txt
intern/cycles/device/device.cpp
intern/cycles/device/device.h
intern/cycles/device/device_cuda.cpp
source/blender/blenkernel/intern/editderivedmesh.c
source/blender/blenkernel/intern/mesh_evaluate.c
source/blender/blenkernel/intern/particle_distribute.c
source/blender/blenkernel/intern/particle_system.c
source/blender/blenlib/CMakeLists.txt
source/blender/editors/gpencil/drawgpencil.c
source/blender/editors/manipulator_library/manipulator_types/cage2d_manipulator.c
source/blender/editors/manipulator_library/manipulator_types/cage3d_manipulator.c
source/blender/editors/physics/particle_edit.c
source/blender/editors/sculpt_paint/paint_mask.c
source/blender/editors/sculpt_paint/sculpt.c
source/blender/editors/space_view3d/view3d_draw.c
source/blender/editors/space_view3d/view3d_draw_legacy.c
source/blender/editors/space_view3d/view3d_select.c
source/blender/editors/uvedit/uvedit_ops.c
source/blender/gpu/intern/gpu_batch.c
source/blender/python/mathutils/mathutils_bvhtree.c
source/blender/render/intern/source/initrender.c
source/blender/render/intern/source/zbuf.c
source/blender/windowmanager/intern/wm_gesture.c
source/blender/windowmanager/intern/wm_operators.c
tests/gtests/blenlib/CMakeLists.txt

diff --cc CMakeLists.txt
Simple merge
Simple merge
Simple merge
Simple merge
index 9e7b4ed8b6f54128415eafb5adc91c9eb95a6d50,d137de58e178dfee2d553ea0987fcc99e4270ced..ef36b5ac6bd0efa26e4556f55e5a0a051b0627fc
@@@ -157,11 -157,10 +158,11 @@@ set(SR
        BLI_hash_md5.h
        BLI_hash_mm2a.h
        BLI_heap.h
-       BLI_jitter.h
 +      BLI_iterator.h
+       BLI_jitter_2d.h
        BLI_kdopbvh.h
        BLI_kdtree.h
-       BLI_lasso.h
+       BLI_lasso_2d.h
        BLI_link_utils.h
        BLI_linklist.h
        BLI_linklist_stack.h
index 0299a33d0fed297f6c96f80378b0b2bf6ae16d84,0000000000000000000000000000000000000000..23c6030f091d841c16a94c0905b611ba0d52e00c
mode 100644,000000..100644
--- /dev/null
@@@ -1,1109 -1,0 +1,1109 @@@
- #include "BLI_dial.h"
 +/*
 + * ***** BEGIN GPL LICENSE BLOCK *****
 + *
 + * This program is free software; you can redistribute it and/or
 + * modify it under the terms of the GNU General Public License
 + * as published by the Free Software Foundation; either version 2
 + * of the License, or (at your option) any later version.
 + *
 + * This program is distributed in the hope that it will be useful,
 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 + * GNU General Public License for more details.
 + *
 + * You should have received a copy of the GNU General Public License
 + * along with this program; if not, write to the Free Software Foundation,
 + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 + *
 + * The Original Code is Copyright (C) 2014 Blender Foundation.
 + * All rights reserved.
 + *
 + * Contributor(s): Blender Foundation
 + *
 + * ***** END GPL LICENSE BLOCK *****
 + */
 +
 +/** \file cage2d_manipulator.c
 + *  \ingroup wm
 + *
 + * \name Cage Manipulator
 + *
 + * 2D Manipulator
 + *
 + * \brief Rectangular manipulator acting as a 'cage' around its content.
 + * Interacting scales or translates the manipulator.
 + */
 +
 +#include "MEM_guardedalloc.h"
 +
 +#include "BLI_math.h"
++#include "BLI_dial_2d.h"
 +#include "BLI_rect.h"
 +
 +#include "BKE_context.h"
 +
 +#include "BIF_gl.h"
 +
 +#include "GPU_matrix.h"
 +#include "GPU_shader.h"
 +#include "GPU_immediate.h"
 +#include "GPU_immediate_util.h"
 +#include "GPU_select.h"
 +
 +#include "RNA_access.h"
 +#include "RNA_define.h"
 +
 +#include "WM_api.h"
 +#include "WM_types.h"
 +
 +#include "ED_screen.h"
 +#include "ED_view3d.h"
 +#include "ED_manipulator_library.h"
 +
 +/* own includes */
 +#include "../manipulator_library_intern.h"
 +
 +#define MANIPULATOR_RESIZER_SIZE 10.0f
 +#define MANIPULATOR_MARGIN_OFFSET_SCALE 1.5f
 +
 +static void manipulator_calc_matrix_final_no_offset(
 +        const wmManipulator *mpr, float orig_matrix_final_no_offset[4][4])
 +{
 +      float mat_identity[4][4];
 +      struct WM_ManipulatorMatrixParams params = {NULL};
 +      unit_m4(mat_identity);
 +      params.matrix_offset = mat_identity;
 +      WM_manipulator_calc_matrix_final_params(mpr, &params, orig_matrix_final_no_offset);
 +}
 +
 +static void manipulator_calc_rect_view_scale(
 +        const wmManipulator *mpr, const float dims[2], float scale[2])
 +{
 +      float matrix_final_no_offset[4][4];
 +      float asp[2] = {1.0f, 1.0f};
 +      if (dims[0] > dims[1]) {
 +              asp[0] = dims[1] / dims[0];
 +      }
 +      else {
 +              asp[1] = dims[0] / dims[1];
 +      }
 +      float x_axis[3], y_axis[3];
 +      manipulator_calc_matrix_final_no_offset(mpr, matrix_final_no_offset);
 +      mul_v3_mat3_m4v3(x_axis, matrix_final_no_offset, mpr->matrix_offset[0]);
 +      mul_v3_mat3_m4v3(y_axis, matrix_final_no_offset, mpr->matrix_offset[1]);
 +
 +      mul_v2_v2(x_axis, asp);
 +      mul_v2_v2(y_axis, asp);
 +
 +      scale[0] = 1.0f / len_v3(x_axis);
 +      scale[1] = 1.0f / len_v3(y_axis);
 +}
 +
 +static void manipulator_calc_rect_view_margin(
 +        const wmManipulator *mpr, const float dims[2], float margin[2])
 +{
 +      float handle_size;
 +      if (mpr->parent_mgroup->type->flag & WM_MANIPULATORGROUPTYPE_3D) {
 +              handle_size = 0.15f;
 +      }
 +      else {
 +              handle_size = MANIPULATOR_RESIZER_SIZE;
 +      }
 +      handle_size *= mpr->scale_final;
 +      float scale_xy[2];
 +      manipulator_calc_rect_view_scale(mpr, dims, scale_xy);
 +      margin[0] = ((handle_size * scale_xy[0]));
 +      margin[1] = ((handle_size * scale_xy[1]));
 +}
 +
 +/* -------------------------------------------------------------------- */
 +
 +static void manipulator_rect_pivot_from_scale_part(int part, float r_pt[2], bool r_constrain_axis[2])
 +{
 +      bool x = true, y = true;
 +      switch (part) {
 +              case ED_MANIPULATOR_CAGE2D_PART_SCALE_MIN_X: { ARRAY_SET_ITEMS(r_pt,  0.5,  0.0); x = false; break; }
 +              case ED_MANIPULATOR_CAGE2D_PART_SCALE_MAX_X: { ARRAY_SET_ITEMS(r_pt, -0.5,  0.0); x = false; break; }
 +              case ED_MANIPULATOR_CAGE2D_PART_SCALE_MIN_Y: { ARRAY_SET_ITEMS(r_pt,  0.0,  0.5); y = false; break; }
 +              case ED_MANIPULATOR_CAGE2D_PART_SCALE_MAX_Y: { ARRAY_SET_ITEMS(r_pt,  0.0, -0.5); y = false; break; }
 +              case ED_MANIPULATOR_CAGE2D_PART_SCALE_MIN_X_MIN_Y: { ARRAY_SET_ITEMS(r_pt,  0.5,  0.5); x = y = false; break; }
 +              case ED_MANIPULATOR_CAGE2D_PART_SCALE_MIN_X_MAX_Y: { ARRAY_SET_ITEMS(r_pt,  0.5, -0.5); x = y = false; break; }
 +              case ED_MANIPULATOR_CAGE2D_PART_SCALE_MAX_X_MIN_Y: { ARRAY_SET_ITEMS(r_pt, -0.5,  0.5); x = y = false; break; }
 +              case ED_MANIPULATOR_CAGE2D_PART_SCALE_MAX_X_MAX_Y: { ARRAY_SET_ITEMS(r_pt, -0.5, -0.5); x = y = false; break; }
 +              default: BLI_assert(0);
 +      }
 +      r_constrain_axis[0] = x;
 +      r_constrain_axis[1] = y;
 +}
 +
 +/* -------------------------------------------------------------------- */
 +/** \name Box Draw Style
 + *
 + * Useful for 3D views, see: #ED_MANIPULATOR_CAGE2D_STYLE_BOX
 + * \{ */
 +
 +static void cage2d_draw_box_corners(
 +        const rctf *r, const float margin[2], const float color[3])
 +{
 +      uint pos = GWN_vertformat_attr_add(immVertexFormat(), "pos", GWN_COMP_F32, 2, GWN_FETCH_FLOAT);
 +
 +      immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
 +      immUniformColor3fv(color);
 +
 +      immBegin(GWN_PRIM_LINES, 16);
 +
 +      immVertex2f(pos, r->xmin, r->ymin + margin[1]);
 +      immVertex2f(pos, r->xmin, r->ymin);
 +      immVertex2f(pos, r->xmin, r->ymin);
 +      immVertex2f(pos, r->xmin + margin[0], r->ymin);
 +
 +      immVertex2f(pos, r->xmax, r->ymin + margin[1]);
 +      immVertex2f(pos, r->xmax, r->ymin);
 +      immVertex2f(pos, r->xmax, r->ymin);
 +      immVertex2f(pos, r->xmax - margin[0], r->ymin);
 +
 +      immVertex2f(pos, r->xmax, r->ymax - margin[1]);
 +      immVertex2f(pos, r->xmax, r->ymax);
 +      immVertex2f(pos, r->xmax, r->ymax);
 +      immVertex2f(pos, r->xmax - margin[0], r->ymax);
 +
 +      immVertex2f(pos, r->xmin, r->ymax - margin[1]);
 +      immVertex2f(pos, r->xmin, r->ymax);
 +      immVertex2f(pos, r->xmin, r->ymax);
 +      immVertex2f(pos, r->xmin + margin[0], r->ymax);
 +
 +      immEnd();
 +
 +      immUnbindProgram();
 +}
 +
 +static void cage2d_draw_box_interaction(
 +        const float color[4], const int highlighted,
 +        const float size[2], const float margin[2],
 +        const float line_width, const bool is_solid, const int draw_options)
 +{
 +      /* 4 verts for translate, otherwise only 3 are used. */
 +      float verts[4][2];
 +      uint verts_len = 0;
 +      Gwn_PrimType prim_type = GWN_PRIM_NONE;
 +
 +      switch (highlighted) {
 +              case ED_MANIPULATOR_CAGE2D_PART_SCALE_MIN_X:
 +              {
 +                      rctf r = {
 +                              .xmin = -size[0], .xmax = -size[0] + margin[0],
 +                              .ymin = -size[1] + margin[1], .ymax = size[1] - margin[1],
 +                      };
 +                      ARRAY_SET_ITEMS(verts[0], r.xmin, r.ymin);
 +                      ARRAY_SET_ITEMS(verts[1], r.xmin, r.ymax);
 +                      verts_len = 2;
 +                      if (is_solid) {
 +                              ARRAY_SET_ITEMS(verts[2], r.xmax, r.ymax);
 +                              ARRAY_SET_ITEMS(verts[3], r.xmax, r.ymin);
 +                              verts_len += 2;
 +                              prim_type = GWN_PRIM_TRI_FAN;
 +                      }
 +                      else {
 +                              prim_type = GWN_PRIM_LINE_STRIP;
 +                      }
 +                      break;
 +              }
 +              case ED_MANIPULATOR_CAGE2D_PART_SCALE_MAX_X:
 +              {
 +                      rctf r = {
 +                              .xmin = size[0] - margin[0], .xmax = size[0],
 +                              .ymin = -size[1] + margin[1], .ymax = size[1] - margin[1],
 +                      };
 +                      ARRAY_SET_ITEMS(verts[0], r.xmax, r.ymin);
 +                      ARRAY_SET_ITEMS(verts[1], r.xmax, r.ymax);
 +                      verts_len = 2;
 +                      if (is_solid) {
 +                              ARRAY_SET_ITEMS(verts[2], r.xmin, r.ymax);
 +                              ARRAY_SET_ITEMS(verts[3], r.xmin, r.ymin);
 +                              verts_len += 2;
 +                              prim_type = GWN_PRIM_TRI_FAN;
 +                      }
 +                      else {
 +                              prim_type = GWN_PRIM_LINE_STRIP;
 +                      }
 +                      break;
 +              }
 +              case ED_MANIPULATOR_CAGE2D_PART_SCALE_MIN_Y:
 +              {
 +                      rctf r = {
 +                              .xmin = -size[0] + margin[0], .xmax = size[0] - margin[0],
 +                              .ymin = -size[1], .ymax = -size[1] + margin[1],
 +                      };
 +                      ARRAY_SET_ITEMS(verts[0], r.xmin, r.ymin);
 +                      ARRAY_SET_ITEMS(verts[1], r.xmax, r.ymin);
 +                      verts_len = 2;
 +                      if (is_solid) {
 +                              ARRAY_SET_ITEMS(verts[2], r.xmax, r.ymax);
 +                              ARRAY_SET_ITEMS(verts[3], r.xmin, r.ymax);
 +                              verts_len += 2;
 +                              prim_type = GWN_PRIM_TRI_FAN;
 +                      }
 +                      else {
 +                              prim_type = GWN_PRIM_LINE_STRIP;
 +                      }
 +                      break;
 +              }
 +              case ED_MANIPULATOR_CAGE2D_PART_SCALE_MAX_Y:
 +              {
 +                      rctf r = {
 +                              .xmin = -size[0] + margin[0], .xmax = size[0] - margin[0],
 +                              .ymin = size[1] - margin[1], .ymax = size[1],
 +                      };
 +                      ARRAY_SET_ITEMS(verts[0], r.xmin, r.ymax);
 +                      ARRAY_SET_ITEMS(verts[1], r.xmax, r.ymax);
 +                      verts_len = 2;
 +                      if (is_solid) {
 +                              ARRAY_SET_ITEMS(verts[2], r.xmax, r.ymin);
 +                              ARRAY_SET_ITEMS(verts[3], r.xmin, r.ymin);
 +                              verts_len += 2;
 +                              prim_type = GWN_PRIM_TRI_FAN;
 +                      }
 +                      else {
 +                              prim_type = GWN_PRIM_LINE_STRIP;
 +                      }
 +                      break;
 +              }
 +              case ED_MANIPULATOR_CAGE2D_PART_SCALE_MIN_X_MIN_Y:
 +              {
 +                      rctf r = {
 +                              .xmin = -size[0], .xmax = -size[0] + margin[0],
 +                              .ymin = -size[1], .ymax = -size[1] + margin[1],
 +                      };
 +                      ARRAY_SET_ITEMS(verts[0], r.xmax, r.ymin);
 +                      ARRAY_SET_ITEMS(verts[1], r.xmax, r.ymax);
 +                      ARRAY_SET_ITEMS(verts[2], r.xmin, r.ymax);
 +                      verts_len = 3;
 +                      if (is_solid) {
 +                              ARRAY_SET_ITEMS(verts[3], r.xmin, r.ymin);
 +                              verts_len += 1;
 +                              prim_type = GWN_PRIM_TRI_FAN;
 +                      }
 +                      else {
 +                              prim_type = GWN_PRIM_LINE_STRIP;
 +                      }
 +                      break;
 +              }
 +              case ED_MANIPULATOR_CAGE2D_PART_SCALE_MIN_X_MAX_Y:
 +              {
 +                      rctf r = {
 +                              .xmin = -size[0], .xmax = -size[0] + margin[0],
 +                              .ymin = size[1] - margin[1], .ymax = size[1],
 +                      };
 +                      ARRAY_SET_ITEMS(verts[0], r.xmax, r.ymax);
 +                      ARRAY_SET_ITEMS(verts[1], r.xmax, r.ymin);
 +                      ARRAY_SET_ITEMS(verts[2], r.xmin, r.ymin);
 +                      verts_len = 3;
 +                      if (is_solid) {
 +                              ARRAY_SET_ITEMS(verts[3], r.xmin, r.ymax);
 +                              verts_len += 1;
 +                              prim_type = GWN_PRIM_TRI_FAN;
 +                      }
 +                      else {
 +                              prim_type = GWN_PRIM_LINE_STRIP;
 +                      }
 +                      break;
 +              }
 +              case ED_MANIPULATOR_CAGE2D_PART_SCALE_MAX_X_MIN_Y:
 +              {
 +                      rctf r = {
 +                              .xmin = size[0] - margin[0], .xmax = size[0],
 +                              .ymin = -size[1], .ymax = -size[1] + margin[1],
 +                      };
 +                      ARRAY_SET_ITEMS(verts[0], r.xmin, r.ymin);
 +                      ARRAY_SET_ITEMS(verts[1], r.xmin, r.ymax);
 +                      ARRAY_SET_ITEMS(verts[2], r.xmax, r.ymax);
 +                      verts_len = 3;
 +                      if (is_solid) {
 +                              ARRAY_SET_ITEMS(verts[3], r.xmax, r.ymin);
 +                              verts_len += 1;
 +                              prim_type = GWN_PRIM_TRI_FAN;
 +                      }
 +                      else {
 +                              prim_type = GWN_PRIM_LINE_STRIP;
 +                      }
 +                      break;
 +              }
 +              case ED_MANIPULATOR_CAGE2D_PART_SCALE_MAX_X_MAX_Y:
 +              {
 +                      rctf r = {
 +                              .xmin = size[0] - margin[0], .xmax = size[0],
 +                              .ymin = size[1] - margin[1], .ymax = size[1],
 +                      };
 +                      ARRAY_SET_ITEMS(verts[0], r.xmin, r.ymax);
 +                      ARRAY_SET_ITEMS(verts[1], r.xmin, r.ymin);
 +                      ARRAY_SET_ITEMS(verts[2], r.xmax, r.ymin);
 +                      verts_len = 3;
 +                      if (is_solid) {
 +                              ARRAY_SET_ITEMS(verts[3], r.xmax, r.ymax);
 +                              verts_len += 1;
 +                              prim_type = GWN_PRIM_TRI_FAN;
 +                      }
 +                      else {
 +                              prim_type = GWN_PRIM_LINE_STRIP;
 +                      }
 +                      break;
 +              }
 +              case ED_MANIPULATOR_CAGE2D_PART_ROTATE:
 +              {
 +                      const float rotate_pt[2] = {0.0f, size[1] + margin[1]};
 +                      const rctf r_rotate = {
 +                              .xmin = rotate_pt[0] - margin[0] / 2.0f,
 +                              .xmax = rotate_pt[0] + margin[0] / 2.0f,
 +                              .ymin = rotate_pt[1] - margin[1] / 2.0f,
 +                              .ymax = rotate_pt[1] + margin[1] / 2.0f,
 +                      };
 +
 +                      ARRAY_SET_ITEMS(verts[0], r_rotate.xmin, r_rotate.ymin);
 +                      ARRAY_SET_ITEMS(verts[1], r_rotate.xmin, r_rotate.ymax);
 +                      ARRAY_SET_ITEMS(verts[2], r_rotate.xmax, r_rotate.ymax);
 +                      ARRAY_SET_ITEMS(verts[3], r_rotate.xmax, r_rotate.ymin);
 +                      verts_len = 4;
 +                      if (is_solid) {
 +                              prim_type = GWN_PRIM_TRI_FAN;
 +                      }
 +                      else {
 +                              prim_type = GWN_PRIM_LINE_STRIP;
 +                      }
 +                      break;
 +              }
 +
 +              case ED_MANIPULATOR_CAGE2D_PART_TRANSLATE:
 +                      if (draw_options & ED_MANIPULATOR_CAGE2D_DRAW_FLAG_XFORM_CENTER_HANDLE) {
 +                              ARRAY_SET_ITEMS(verts[0], -margin[0] / 2, -margin[1] / 2);
 +                              ARRAY_SET_ITEMS(verts[1],  margin[0] / 2,  margin[1] / 2);
 +                              ARRAY_SET_ITEMS(verts[2], -margin[0] / 2,  margin[1] / 2);
 +                              ARRAY_SET_ITEMS(verts[3],  margin[0] / 2, -margin[1] / 2);
 +                              verts_len = 4;
 +                              if (is_solid) {
 +                                      prim_type = GWN_PRIM_TRI_FAN;
 +                              }
 +                              else {
 +                                      prim_type = GWN_PRIM_LINES;
 +                              }
 +                      }
 +                      else {
 +                              /* Only used for 3D view selection, never displayed to the user. */
 +                              ARRAY_SET_ITEMS(verts[0], -size[0], -size[1]);
 +                              ARRAY_SET_ITEMS(verts[1], -size[0],  size[1]);
 +                              ARRAY_SET_ITEMS(verts[2], size[0],   size[1]);
 +                              ARRAY_SET_ITEMS(verts[3], size[0],  -size[1]);
 +                              verts_len = 4;
 +                              if (is_solid) {
 +                                      prim_type = GWN_PRIM_TRI_FAN;
 +                              }
 +                              else {
 +                                      /* unreachable */
 +                                      BLI_assert(0);
 +                                      prim_type = GWN_PRIM_LINE_STRIP;
 +                              }
 +                      }
 +                      break;
 +              default:
 +                      return;
 +      }
 +
 +      BLI_assert(prim_type != GWN_PRIM_NONE);
 +
 +      Gwn_VertFormat *format = immVertexFormat();
 +      struct {
 +              uint pos, col;
 +      } attr_id = {
 +              .pos = GWN_vertformat_attr_add(format, "pos", GWN_COMP_F32, 2, GWN_FETCH_FLOAT),
 +              .col = GWN_vertformat_attr_add(format, "color", GWN_COMP_F32, 3, GWN_FETCH_FLOAT),
 +      };
 +      immBindBuiltinProgram(GPU_SHADER_2D_FLAT_COLOR);
 +
 +      {
 +              if (is_solid) {
 +                      BLI_assert(ELEM(prim_type, GWN_PRIM_TRI_FAN));
 +                      immBegin(prim_type, verts_len);
 +                      immAttrib3f(attr_id.col, 0.0f, 0.0f, 0.0f);
 +                      for (uint i = 0; i < verts_len; i++) {
 +                              immVertex2fv(attr_id.pos, verts[i]);
 +                      }
 +                      immEnd();
 +              }
 +              else {
 +                      BLI_assert(ELEM(prim_type, GWN_PRIM_LINE_STRIP, GWN_PRIM_LINES));
 +                      glLineWidth(line_width + 3.0f);
 +
 +                      immBegin(prim_type, verts_len);
 +                      immAttrib3f(attr_id.col, 0.0f, 0.0f, 0.0f);
 +                      for (uint i = 0; i < verts_len; i++) {
 +                              immVertex2fv(attr_id.pos, verts[i]);
 +                      }
 +                      immEnd();
 +
 +                      glLineWidth(line_width);
 +
 +                      immBegin(prim_type, verts_len);
 +                      immAttrib3fv(attr_id.col, color);
 +                      for (uint i = 0; i < verts_len; i++) {
 +                              immVertex2fv(attr_id.pos, verts[i]);
 +                      }
 +                      immEnd();
 +              }
 +      }
 +
 +      immUnbindProgram();
 +}
 +
 +/** \} */
 +
 +/* -------------------------------------------------------------------- */
 +/** \name Circle Draw Style
 + *
 + * Useful for 2D views, see: #ED_MANIPULATOR_CAGE2D_STYLE_CIRCLE
 + * \{ */
 +
 +static void imm_draw_point_aspect_2d(
 +        uint pos, float x, float y, float rad_x, float rad_y, bool solid)
 +{
 +      immBegin(solid ? GWN_PRIM_TRI_FAN : GWN_PRIM_LINE_LOOP, 4);
 +      immVertex2f(pos, x - rad_x, y - rad_y);
 +      immVertex2f(pos, x - rad_x, y + rad_y);
 +      immVertex2f(pos, x + rad_x, y + rad_y);
 +      immVertex2f(pos, x + rad_x, y - rad_y);
 +      immEnd();
 +}
 +
 +static void cage2d_draw_circle_wire(
 +        const rctf *r, const float margin[2], const float color[3],
 +        const int transform_flag, const int draw_options)
 +{
 +      uint pos = GWN_vertformat_attr_add(immVertexFormat(), "pos", GWN_COMP_F32, 2, GWN_FETCH_FLOAT);
 +
 +      immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
 +      immUniformColor3fv(color);
 +
 +      immBegin(GWN_PRIM_LINE_LOOP, 4);
 +      immVertex2f(pos, r->xmin, r->ymin);
 +      immVertex2f(pos, r->xmax, r->ymin);
 +      immVertex2f(pos, r->xmax, r->ymax);
 +      immVertex2f(pos, r->xmin, r->ymax);
 +      immEnd();
 +
 +      if (transform_flag & ED_MANIPULATOR_CAGE2D_XFORM_FLAG_ROTATE) {
 +              immBegin(GWN_PRIM_LINE_LOOP, 2);
 +              immVertex2f(pos, BLI_rctf_cent_x(r), r->ymax);
 +              immVertex2f(pos, BLI_rctf_cent_x(r), r->ymax + margin[1]);
 +              immEnd();
 +      }
 +
 +      if (transform_flag & ED_MANIPULATOR_CAGE2D_XFORM_FLAG_TRANSLATE) {
 +              if (draw_options & ED_MANIPULATOR_CAGE2D_DRAW_FLAG_XFORM_CENTER_HANDLE) {
 +                      const float rad[2] = {margin[0] / 2, margin[1] / 2};
 +                      const float center[2] = {BLI_rctf_cent_x(r), BLI_rctf_cent_y(r)};
 +
 +                      immBegin(GWN_PRIM_LINES, 4);
 +                      immVertex2f(pos, center[0] - rad[0], center[1] - rad[1]);
 +                      immVertex2f(pos, center[0] + rad[0], center[1] + rad[1]);
 +                      immVertex2f(pos, center[0] + rad[0], center[1] - rad[1]);
 +                      immVertex2f(pos, center[0] - rad[0], center[1] + rad[1]);
 +                      immEnd();
 +              }
 +      }
 +
 +      immUnbindProgram();
 +}
 +
 +static void cage2d_draw_circle_handles(
 +        const rctf *r, const float margin[2], const float color[3],
 +        const int transform_flag,
 +        bool solid)
 +{
 +      uint pos = GWN_vertformat_attr_add(immVertexFormat(), "pos", GWN_COMP_F32, 2, GWN_FETCH_FLOAT);
 +      void (*circle_fn)(uint, float, float, float, float, int) =
 +              (solid) ? imm_draw_circle_fill_aspect_2d : imm_draw_circle_wire_aspect_2d;
 +      const int resolu = 12;
 +      const float rad[2] = {margin[0] / 3, margin[1] / 3};
 +
 +      immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
 +      immUniformColor3fv(color);
 +
 +      /* should  really divide by two, but looks too bulky. */
 +      {
 +              imm_draw_point_aspect_2d(pos, r->xmin, r->ymin, rad[0], rad[1], solid);
 +              imm_draw_point_aspect_2d(pos, r->xmax, r->ymin, rad[0], rad[1], solid);
 +              imm_draw_point_aspect_2d(pos, r->xmax, r->ymax, rad[0], rad[1], solid);
 +              imm_draw_point_aspect_2d(pos, r->xmin, r->ymax, rad[0], rad[1], solid);
 +      }
 +
 +      if (transform_flag & ED_MANIPULATOR_CAGE2D_XFORM_FLAG_ROTATE) {
 +              const float handle[2] = {BLI_rctf_cent_x(r), r->ymax + (margin[1] * MANIPULATOR_MARGIN_OFFSET_SCALE)};
 +              circle_fn(pos, handle[0], handle[1], rad[0], rad[1], resolu);
 +      }
 +
 +      immUnbindProgram();
 +}
 +
 +/** \} */
 +
 +static void manipulator_cage2d_draw_intern(
 +        wmManipulator *mpr, const bool select, const bool highlight, const int select_id)
 +{
 +      // const bool use_clamp = (mpr->parent_mgroup->type->flag & WM_MANIPULATORGROUPTYPE_3D) == 0;
 +      float dims[2];
 +      RNA_float_get_array(mpr->ptr, "dimensions", dims);
 +      float matrix_final[4][4];
 +
 +      const int transform_flag = RNA_enum_get(mpr->ptr, "transform");
 +      const int draw_style = RNA_enum_get(mpr->ptr, "draw_style");
 +      const int draw_options = RNA_enum_get(mpr->ptr, "draw_options");
 +
 +      const float size_real[2] = {dims[0] / 2.0f, dims[1] / 2.0f};
 +
 +      WM_manipulator_calc_matrix_final(mpr, matrix_final);
 +
 +      gpuPushMatrix();
 +      gpuMultMatrix(matrix_final);
 +
 +      float margin[2];
 +      manipulator_calc_rect_view_margin(mpr, dims, margin);
 +
 +      /* Handy for quick testing draw (if it's outside bounds). */
 +      if (false) {
 +              glEnable(GL_BLEND);
 +              uint pos = GWN_vertformat_attr_add(immVertexFormat(), "pos", GWN_COMP_F32, 2, GWN_FETCH_FLOAT);
 +              immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
 +              immUniformColor4fv((const float[4]){1, 1, 1, 0.5f});
 +              float s = 0.5f;
 +              immRectf(pos, -s, -s, s, s);
 +              immUnbindProgram();
 +              glDisable(GL_BLEND);
 +      }
 +
 +      if (select) {
 +              /* expand for hotspot */
 +              const float size[2] = {size_real[0] + margin[0] / 2, size_real[1] + margin[1] / 2};
 +
 +              if (transform_flag & ED_MANIPULATOR_CAGE2D_XFORM_FLAG_SCALE) {
 +                      int scale_parts[] = {
 +                              ED_MANIPULATOR_CAGE2D_PART_SCALE_MIN_X,
 +                              ED_MANIPULATOR_CAGE2D_PART_SCALE_MAX_X,
 +                              ED_MANIPULATOR_CAGE2D_PART_SCALE_MIN_Y,
 +                              ED_MANIPULATOR_CAGE2D_PART_SCALE_MAX_Y,
 +
 +                              ED_MANIPULATOR_CAGE2D_PART_SCALE_MIN_X_MIN_Y,
 +                              ED_MANIPULATOR_CAGE2D_PART_SCALE_MIN_X_MAX_Y,
 +                              ED_MANIPULATOR_CAGE2D_PART_SCALE_MAX_X_MIN_Y,
 +                              ED_MANIPULATOR_CAGE2D_PART_SCALE_MAX_X_MAX_Y,
 +                      };
 +                      for (int i = 0; i < ARRAY_SIZE(scale_parts); i++) {
 +                              GPU_select_load_id(select_id | scale_parts[i]);
 +                              cage2d_draw_box_interaction(
 +                                      mpr->color, scale_parts[i], size, margin, mpr->line_width, true, draw_options);
 +                      }
 +              }
 +              if (transform_flag & ED_MANIPULATOR_CAGE2D_XFORM_FLAG_TRANSLATE) {
 +                      const int transform_part = ED_MANIPULATOR_CAGE2D_PART_TRANSLATE;
 +                      GPU_select_load_id(select_id | transform_part);
 +                      cage2d_draw_box_interaction(
 +                              mpr->color, transform_part, size, margin, mpr->line_width, true, draw_options);
 +              }
 +              if (transform_flag & ED_MANIPULATOR_CAGE2D_XFORM_FLAG_ROTATE) {
 +                      cage2d_draw_box_interaction(
 +                              mpr->color, ED_MANIPULATOR_CAGE2D_PART_ROTATE, size_real, margin, mpr->line_width, true, draw_options);
 +              }
 +      }
 +      else {
 +              const rctf r = {
 +                      .xmin = -size_real[0],
 +                      .ymin = -size_real[1],
 +                      .xmax = size_real[0],
 +                      .ymax = size_real[1],
 +              };
 +              if (draw_style == ED_MANIPULATOR_CAGE2D_STYLE_BOX) {
 +                      /* corner manipulators */
 +                      glLineWidth(mpr->line_width + 3.0f);
 +                      cage2d_draw_box_corners(&r, margin, (const float[3]){0, 0, 0});
 +
 +                      /* corner manipulators */
 +                      float color[4];
 +                      manipulator_color_get(mpr, highlight, color);
 +                      glLineWidth(mpr->line_width);
 +                      cage2d_draw_box_corners(&r, margin, color);
 +
 +                      bool show = false;
 +                      if (mpr->highlight_part == ED_MANIPULATOR_CAGE2D_PART_TRANSLATE) {
 +                              /* Only show if we're drawing the center handle
 +                               * otherwise the entire rectangle is the hotspot. */
 +                              if (draw_options & ED_MANIPULATOR_CAGE2D_DRAW_FLAG_XFORM_CENTER_HANDLE) {
 +                                      show = true;
 +                              }
 +                      }
 +                      else {
 +                              show = true;
 +                      }
 +
 +                      if (show) {
 +                              cage2d_draw_box_interaction(
 +                                      mpr->color, mpr->highlight_part, size_real, margin, mpr->line_width, false, draw_options);
 +                      }
 +
 +                      if (transform_flag & ED_MANIPULATOR_CAGE2D_XFORM_FLAG_ROTATE) {
 +                              cage2d_draw_box_interaction(
 +                                      mpr->color, ED_MANIPULATOR_CAGE2D_PART_ROTATE, size_real, margin, mpr->line_width, false, draw_options);
 +                      }
 +              }
 +              else if (draw_style == ED_MANIPULATOR_CAGE2D_STYLE_CIRCLE) {
 +                      float color[4];
 +                      manipulator_color_get(mpr, highlight, color);
 +
 +                      glEnable(GL_LINE_SMOOTH);
 +                      glEnable(GL_BLEND);
 +
 +                      glLineWidth(mpr->line_width + 3.0f);
 +                      cage2d_draw_circle_wire(&r, margin, (const float[3]){0, 0, 0}, transform_flag, draw_options);
 +                      glLineWidth(mpr->line_width);
 +                      cage2d_draw_circle_wire(&r, margin, color, transform_flag, draw_options);
 +
 +
 +                      /* corner manipulators */
 +                      cage2d_draw_circle_handles(&r, margin, color, transform_flag, true);
 +                      cage2d_draw_circle_handles(&r, margin, (const float[3]){0, 0, 0}, transform_flag, false);
 +
 +                      glDisable(GL_BLEND);
 +                      glDisable(GL_LINE_SMOOTH);
 +              }
 +              else {
 +                      BLI_assert(0);
 +              }
 +      }
 +
 +      glLineWidth(1.0);
 +      gpuPopMatrix();
 +}
 +
 +/**
 + * For when we want to draw 2d cage in 3d views.
 + */
 +static void manipulator_cage2d_draw_select(const bContext *UNUSED(C), wmManipulator *mpr, int select_id)
 +{
 +      manipulator_cage2d_draw_intern(mpr, true, false, select_id);
 +}
 +
 +static void manipulator_cage2d_draw(const bContext *UNUSED(C), wmManipulator *mpr)
 +{
 +      const bool is_highlight = (mpr->state & WM_MANIPULATOR_STATE_HIGHLIGHT) != 0;
 +      manipulator_cage2d_draw_intern(mpr, false, is_highlight, -1);
 +}
 +
 +static int manipulator_cage2d_get_cursor(wmManipulator *mpr)
 +{
 +      int highlight_part = mpr->highlight_part;
 +
 +      if (mpr->parent_mgroup->type->flag & WM_MANIPULATORGROUPTYPE_3D) {
 +              return BC_NSEW_SCROLLCURSOR;
 +      }
 +
 +      switch (highlight_part) {
 +              case ED_MANIPULATOR_CAGE2D_PART_TRANSLATE:
 +                      return BC_HANDCURSOR;
 +              case ED_MANIPULATOR_CAGE2D_PART_SCALE_MIN_X:
 +              case ED_MANIPULATOR_CAGE2D_PART_SCALE_MAX_X:
 +                      return CURSOR_X_MOVE;
 +              case ED_MANIPULATOR_CAGE2D_PART_SCALE_MIN_Y:
 +              case ED_MANIPULATOR_CAGE2D_PART_SCALE_MAX_Y:
 +                      return CURSOR_Y_MOVE;
 +
 +                      /* TODO diagonal cursor */
 +              case ED_MANIPULATOR_CAGE2D_PART_SCALE_MIN_X_MIN_Y:
 +              case ED_MANIPULATOR_CAGE2D_PART_SCALE_MAX_X_MIN_Y:
 +                      return BC_NSEW_SCROLLCURSOR;
 +              case ED_MANIPULATOR_CAGE2D_PART_SCALE_MIN_X_MAX_Y:
 +              case ED_MANIPULATOR_CAGE2D_PART_SCALE_MAX_X_MAX_Y:
 +                      return BC_NSEW_SCROLLCURSOR;
 +              case ED_MANIPULATOR_CAGE2D_PART_ROTATE:
 +                      return BC_CROSSCURSOR;
 +              default:
 +                      return CURSOR_STD;
 +      }
 +}
 +
 +static int manipulator_cage2d_test_select(
 +        bContext *C, wmManipulator *mpr, const wmEvent *event)
 +{
 +      float point_local[2];
 +      float dims[2];
 +      RNA_float_get_array(mpr->ptr, "dimensions", dims);
 +      const float size_real[2] = {dims[0] / 2.0f, dims[1] / 2.0f};
 +
 +      if (manipulator_window_project_2d(
 +              C, mpr, (const float[2]){UNPACK2(event->mval)}, 2, true, point_local) == false)
 +      {
 +              return -1;
 +      }
 +
 +      float margin[2];
 +      manipulator_calc_rect_view_margin(mpr, dims, margin);
 +      /* expand for hotspot */
 +      const float size[2] = {size_real[0] + margin[0] / 2, size_real[1] + margin[1] / 2};
 +
 +      const int transform_flag = RNA_enum_get(mpr->ptr, "transform");
 +      const int draw_options = RNA_enum_get(mpr->ptr, "draw_options");
 +
 +      if (transform_flag & ED_MANIPULATOR_CAGE2D_XFORM_FLAG_TRANSLATE) {
 +              rctf r;
 +              if (draw_options & ED_MANIPULATOR_CAGE2D_DRAW_FLAG_XFORM_CENTER_HANDLE) {
 +                      r.xmin = -margin[0] / 2;
 +                      r.ymin = -margin[1] / 2;
 +                      r.xmax =  margin[0] / 2;
 +                      r.ymax =  margin[1] / 2;
 +              }
 +              else {
 +                      r.xmin = -size[0] + margin[0];
 +                      r.ymin = -size[1] + margin[1];
 +                      r.xmax =  size[0] - margin[0];
 +                      r.ymax =  size[1] - margin[1];
 +              };
 +              bool isect = BLI_rctf_isect_pt_v(&r, point_local);
 +              if (isect) {
 +                      return ED_MANIPULATOR_CAGE2D_PART_TRANSLATE;
 +              }
 +      }
 +
 +      /* if manipulator does not have a scale intersection, don't do it */
 +      if (transform_flag & (ED_MANIPULATOR_CAGE2D_XFORM_FLAG_SCALE | ED_MANIPULATOR_CAGE2D_XFORM_FLAG_SCALE_UNIFORM)) {
 +              const rctf r_xmin = {.xmin = -size[0], .ymin = -size[1], .xmax = -size[0] + margin[0], .ymax = size[1]};
 +              const rctf r_xmax = {.xmin = size[0] - margin[0], .ymin = -size[1], .xmax = size[0], .ymax = size[1]};
 +              const rctf r_ymin = {.xmin = -size[0], .ymin = -size[1], .xmax = size[0], .ymax = -size[1] + margin[1]};
 +              const rctf r_ymax = {.xmin = -size[0], .ymin = size[1] - margin[1], .xmax = size[0], .ymax = size[1]};
 +
 +              if (BLI_rctf_isect_pt_v(&r_xmin, point_local)) {
 +                      if (BLI_rctf_isect_pt_v(&r_ymin, point_local)) {
 +                              return ED_MANIPULATOR_CAGE2D_PART_SCALE_MIN_X_MIN_Y;
 +                      }
 +                      if (BLI_rctf_isect_pt_v(&r_ymax, point_local)) {
 +                              return ED_MANIPULATOR_CAGE2D_PART_SCALE_MIN_X_MAX_Y;
 +                      }
 +                      return ED_MANIPULATOR_CAGE2D_PART_SCALE_MIN_X;
 +              }
 +              if (BLI_rctf_isect_pt_v(&r_xmax, point_local)) {
 +                      if (BLI_rctf_isect_pt_v(&r_ymin, point_local)) {
 +                              return ED_MANIPULATOR_CAGE2D_PART_SCALE_MAX_X_MIN_Y;
 +                      }
 +                      if (BLI_rctf_isect_pt_v(&r_ymax, point_local)) {
 +                              return ED_MANIPULATOR_CAGE2D_PART_SCALE_MAX_X_MAX_Y;
 +                      }
 +                      return ED_MANIPULATOR_CAGE2D_PART_SCALE_MAX_X;
 +              }
 +              if (BLI_rctf_isect_pt_v(&r_ymin, point_local)) {
 +                      return ED_MANIPULATOR_CAGE2D_PART_SCALE_MIN_Y;
 +              }
 +              if (BLI_rctf_isect_pt_v(&r_ymax, point_local)) {
 +                      return ED_MANIPULATOR_CAGE2D_PART_SCALE_MAX_Y;
 +              }
 +      }
 +
 +      if (transform_flag & ED_MANIPULATOR_CAGE2D_XFORM_FLAG_ROTATE) {
 +              /* Rotate:
 +               *  (*) <-- hot spot is here!
 +               * +---+
 +               * |   |
 +               * +---+ */
 +              const float r_rotate_pt[2] = {0.0f, size_real[1] + (margin[1] * MANIPULATOR_MARGIN_OFFSET_SCALE)};
 +              const rctf r_rotate = {
 +                      .xmin = r_rotate_pt[0] - margin[0] / 2.0f,
 +                      .xmax = r_rotate_pt[0] + margin[0] / 2.0f,
 +                      .ymin = r_rotate_pt[1] - margin[1] / 2.0f,
 +                      .ymax = r_rotate_pt[1] + margin[1] / 2.0f,
 +              };
 +
 +              if (BLI_rctf_isect_pt_v(&r_rotate, point_local)) {
 +                      return ED_MANIPULATOR_CAGE2D_PART_ROTATE;
 +              }
 +      }
 +
 +      return -1;
 +}
 +
 +typedef struct RectTransformInteraction {
 +      float orig_mouse[2];
 +      float orig_matrix_offset[4][4];
 +      float orig_matrix_final_no_offset[4][4];
 +      Dial *dial;
 +} RectTransformInteraction;
 +
 +static void manipulator_cage2d_setup(wmManipulator *mpr)
 +{
 +      mpr->flag |= WM_MANIPULATOR_DRAW_MODAL | WM_MANIPULATOR_DRAW_NO_SCALE;
 +}
 +
 +static int manipulator_cage2d_invoke(
 +        bContext *C, wmManipulator *mpr, const wmEvent *event)
 +{
 +      RectTransformInteraction *data = MEM_callocN(sizeof(RectTransformInteraction), "cage_interaction");
 +
 +      copy_m4_m4(data->orig_matrix_offset, mpr->matrix_offset);
 +      manipulator_calc_matrix_final_no_offset(mpr, data->orig_matrix_final_no_offset);
 +
 +      if (manipulator_window_project_2d(
 +              C, mpr, (const float[2]){UNPACK2(event->mval)}, 2, false, data->orig_mouse) == 0)
 +      {
 +              zero_v2(data->orig_mouse);
 +      }
 +
 +      mpr->interaction_data = data;
 +
 +      return OPERATOR_RUNNING_MODAL;
 +}
 +
 +static int manipulator_cage2d_modal(
 +        bContext *C, wmManipulator *mpr, const wmEvent *event,
 +        eWM_ManipulatorTweak UNUSED(tweak_flag))
 +{
 +      /* For transform logic to be managable we operate in -0.5..0.5 2D space,
 +       * no matter the size of the rectangle, mouse coorts are scaled to unit space.
 +       * The mouse coords have been projected into the matrix so we don't need to worry about axis alignment.
 +       *
 +       * - The cursor offset are multiplied by 'dims'.
 +       * - Matrix translation is also multiplied by 'dims'.
 +       */
 +      RectTransformInteraction *data = mpr->interaction_data;
 +      float point_local[2];
 +
 +      float dims[2];
 +      RNA_float_get_array(mpr->ptr, "dimensions", dims);
 +
 +      {
 +              float matrix_back[4][4];
 +              copy_m4_m4(matrix_back, mpr->matrix_offset);
 +              copy_m4_m4(mpr->matrix_offset, data->orig_matrix_offset);
 +
 +              bool ok = manipulator_window_project_2d(
 +                      C, mpr, (const float[2]){UNPACK2(event->mval)}, 2, false, point_local);
 +              copy_m4_m4(mpr->matrix_offset, matrix_back);
 +              if (!ok) {
 +                      return OPERATOR_RUNNING_MODAL;
 +              }
 +      }
 +
 +      const int transform_flag = RNA_enum_get(mpr->ptr, "transform");
 +      wmManipulatorProperty *mpr_prop;
 +
 +      mpr_prop = WM_manipulator_target_property_find(mpr, "matrix");
 +      if (mpr_prop->type != NULL) {
 +              WM_manipulator_target_property_value_get_array(mpr, mpr_prop, &mpr->matrix_offset[0][0]);
 +      }
 +
 +      if (mpr->highlight_part == ED_MANIPULATOR_CAGE2D_PART_TRANSLATE) {
 +              /* do this to prevent clamping from changing size */
 +              copy_m4_m4(mpr->matrix_offset, data->orig_matrix_offset);
 +              mpr->matrix_offset[3][0] = data->orig_matrix_offset[3][0] + (point_local[0] - data->orig_mouse[0]);
 +              mpr->matrix_offset[3][1] = data->orig_matrix_offset[3][1] + (point_local[1] - data->orig_mouse[1]);
 +      }
 +      else if (mpr->highlight_part == ED_MANIPULATOR_CAGE2D_PART_ROTATE) {
 +
 +#define MUL_V2_V3_M4_FINAL(test_co, mouse_co) \
 +              mul_v3_m4v3(test_co, data->orig_matrix_final_no_offset, ((const float[3]){UNPACK2(mouse_co), 0.0}))
 +
 +              float test_co[3];
 +
 +              if (data->dial == NULL) {
 +                      MUL_V2_V3_M4_FINAL(test_co, data->orig_matrix_offset[3]);
 +
 +                      data->dial = BLI_dial_initialize(test_co, FLT_EPSILON);
 +
 +                      MUL_V2_V3_M4_FINAL(test_co, data->orig_mouse);
 +                      BLI_dial_angle(data->dial, test_co);
 +              }
 +
 +              /* rotate */
 +              MUL_V2_V3_M4_FINAL(test_co, point_local);
 +              const float angle =  BLI_dial_angle(data->dial, test_co);
 +
 +              float matrix_space_inv[4][4];
 +              float matrix_rotate[4][4];
 +              float pivot[3];
 +
 +              copy_v3_v3(pivot, data->orig_matrix_offset[3]);
 +
 +              invert_m4_m4(matrix_space_inv, mpr->matrix_space);
 +
 +              unit_m4(matrix_rotate);
 +              mul_m4_m4m4(matrix_rotate, matrix_rotate, matrix_space_inv);
 +              rotate_m4(matrix_rotate, 'Z', -angle);
 +              mul_m4_m4m4(matrix_rotate, matrix_rotate, mpr->matrix_space);
 +
 +              zero_v3(matrix_rotate[3]);
 +              transform_pivot_set_m4(matrix_rotate, pivot);
 +
 +              mul_m4_m4m4(mpr->matrix_offset, matrix_rotate, data->orig_matrix_offset);
 +
 +#undef MUL_V2_V3_M4_FINAL
 +      }
 +      else {
 +              /* scale */
 +              copy_m4_m4(mpr->matrix_offset, data->orig_matrix_offset);
 +              float pivot[2];
 +              bool constrain_axis[2] = {false};
 +
 +              if (transform_flag & ED_MANIPULATOR_CAGE2D_XFORM_FLAG_TRANSLATE) {
 +                      manipulator_rect_pivot_from_scale_part(mpr->highlight_part, pivot, constrain_axis);
 +              }
 +              else {
 +                      zero_v2(pivot);
 +              }
 +
 +              /* Cursor deltas scaled to (-0.5..0.5). */
 +              float delta_orig[2], delta_curr[2];
 +              for (int i = 0; i < 2; i++) {
 +                      delta_orig[i] = ((data->orig_mouse[i] - data->orig_matrix_offset[3][i]) / dims[i]) - pivot[i];
 +                      delta_curr[i] = ((point_local[i]      - data->orig_matrix_offset[3][i]) / dims[i]) - pivot[i];
 +              }
 +
 +              float scale[2] = {1.0f, 1.0f};
 +              for (int i = 0; i < 2; i++) {
 +                      if (constrain_axis[i] == false) {
 +                              if (delta_orig[i] < 0.0f) {
 +                                      delta_orig[i] *= -1.0f;
 +                                      delta_curr[i] *= -1.0f;
 +                              }
 +                              const int sign = signum_i(scale[i]);
 +
 +                              scale[i] = 1.0f + ((delta_curr[i] - delta_orig[i]) / len_v3(data->orig_matrix_offset[i]));
 +
 +                              if ((transform_flag & ED_MANIPULATOR_CAGE2D_XFORM_FLAG_SCALE_SIGNED) == 0) {
 +                                      if (sign != signum_i(scale[i])) {
 +                                              scale[i] = 0.0f;
 +                                      }
 +                              }
 +                      }
 +              }
 +
 +              if (transform_flag & ED_MANIPULATOR_CAGE2D_XFORM_FLAG_SCALE_UNIFORM) {
 +                      if (constrain_axis[0] == false && constrain_axis[1] == false) {
 +                              scale[1] = scale[0] = (scale[1] + scale[0]) / 2.0f;
 +                      }
 +                      else if (constrain_axis[0] == false) {
 +                              scale[1] = scale[0];
 +                      }
 +                      else if (constrain_axis[1] == false) {
 +                              scale[0] = scale[1];
 +                      }
 +                      else {
 +                              BLI_assert(0);
 +                      }
 +              }
 +
 +              /* scale around pivot */
 +              float matrix_scale[4][4];
 +              unit_m4(matrix_scale);
 +
 +              mul_v3_fl(matrix_scale[0], scale[0]);
 +              mul_v3_fl(matrix_scale[1], scale[1]);
 +
 +              transform_pivot_set_m4(matrix_scale, (const float [3]){pivot[0] * dims[0], pivot[1] * dims[1], 0.0f});
 +              mul_m4_m4m4(mpr->matrix_offset, data->orig_matrix_offset, matrix_scale);
 +      }
 +
 +      if (mpr_prop->type != NULL) {
 +              WM_manipulator_target_property_value_set_array(C, mpr, mpr_prop, &mpr->matrix_offset[0][0]);
 +      }
 +
 +      /* tag the region for redraw */
 +      ED_region_tag_redraw(CTX_wm_region(C));
 +      WM_event_add_mousemove(C);
 +
 +      return OPERATOR_RUNNING_MODAL;
 +}
 +
 +static void manipulator_cage2d_property_update(wmManipulator *mpr, wmManipulatorProperty *mpr_prop)
 +{
 +      if (STREQ(mpr_prop->type->idname, "matrix")) {
 +              if (WM_manipulator_target_property_array_length(mpr, mpr_prop) == 16) {
 +                      WM_manipulator_target_property_value_get_array(mpr, mpr_prop, &mpr->matrix_offset[0][0]);
 +              }
 +              else {
 +                      BLI_assert(0);
 +              }
 +      }
 +      else {
 +              BLI_assert(0);
 +      }
 +}
 +
 +static void manipulator_cage2d_exit(bContext *C, wmManipulator *mpr, const bool cancel)
 +{
 +      RectTransformInteraction *data = mpr->interaction_data;
 +
 +      MEM_SAFE_FREE(data->dial);
 +
 +      if (!cancel)
 +              return;
 +
 +      wmManipulatorProperty *mpr_prop;
 +
 +      /* reset properties */
 +      mpr_prop = WM_manipulator_target_property_find(mpr, "matrix");
 +      if (mpr_prop->type != NULL) {
 +              WM_manipulator_target_property_value_set_array(C, mpr, mpr_prop, &data->orig_matrix_offset[0][0]);
 +      }
 +
 +      copy_m4_m4(mpr->matrix_offset, data->orig_matrix_offset);
 +}
 +
 +
 +/* -------------------------------------------------------------------- */
 +/** \name Cage Manipulator API
 + *
 + * \{ */
 +
 +static void MANIPULATOR_WT_cage_2d(wmManipulatorType *wt)
 +{
 +      /* identifiers */
 +      wt->idname = "MANIPULATOR_WT_cage_2d";
 +
 +      /* api callbacks */
 +      wt->draw = manipulator_cage2d_draw;
 +      wt->draw_select = manipulator_cage2d_draw_select;
 +      wt->test_select = manipulator_cage2d_test_select;
 +      wt->setup = manipulator_cage2d_setup;
 +      wt->invoke = manipulator_cage2d_invoke;
 +      wt->property_update = manipulator_cage2d_property_update;
 +      wt->modal = manipulator_cage2d_modal;
 +      wt->exit = manipulator_cage2d_exit;
 +      wt->cursor_get = manipulator_cage2d_get_cursor;
 +
 +      wt->struct_size = sizeof(wmManipulator);
 +
 +      /* rna */
 +      static EnumPropertyItem rna_enum_draw_style[] = {
 +              {ED_MANIPULATOR_CAGE2D_STYLE_BOX, "BOX", 0, "Box", ""},
 +              {ED_MANIPULATOR_CAGE2D_STYLE_CIRCLE, "CIRCLE", 0, "Circle", ""},
 +              {0, NULL, 0, NULL, NULL}
 +      };
 +      static EnumPropertyItem rna_enum_transform[] = {
 +              {ED_MANIPULATOR_CAGE2D_XFORM_FLAG_TRANSLATE, "TRANSLATE", 0, "Translate", ""},
 +              {ED_MANIPULATOR_CAGE2D_XFORM_FLAG_ROTATE, "ROTATE", 0, "Rotate", ""},
 +              {ED_MANIPULATOR_CAGE2D_XFORM_FLAG_SCALE, "SCALE", 0, "Scale", ""},
 +              {ED_MANIPULATOR_CAGE2D_XFORM_FLAG_SCALE_UNIFORM, "SCALE_UNIFORM", 0, "Scale Uniform", ""},
 +              {0, NULL, 0, NULL, NULL}
 +      };
 +      static EnumPropertyItem rna_enum_draw_options[] = {
 +              {ED_MANIPULATOR_CAGE2D_DRAW_FLAG_XFORM_CENTER_HANDLE, "XFORM_CENTER_HANDLE", 0, "Center Handle", ""},
 +              {0, NULL, 0, NULL, NULL}
 +      };
 +      static float unit_v2[2] = {1.0f, 1.0f};
 +      RNA_def_float_vector(wt->srna, "dimensions", 2, unit_v2, 0, FLT_MAX, "Dimensions", "", 0.0f, FLT_MAX);
 +      RNA_def_enum_flag(wt->srna, "transform", rna_enum_transform, 0, "Transform Options", "");
 +      RNA_def_enum(wt->srna, "draw_style", rna_enum_draw_style, ED_MANIPULATOR_CAGE2D_STYLE_CIRCLE, "Draw Style", "");
 +      RNA_def_enum_flag(
 +              wt->srna, "draw_options", rna_enum_draw_options,
 +              ED_MANIPULATOR_CAGE2D_DRAW_FLAG_XFORM_CENTER_HANDLE, "Draw Options", "");
 +
 +      WM_manipulatortype_target_property_def(wt, "matrix", PROP_FLOAT, 16);
 +}
 +
 +void ED_manipulatortypes_cage_2d(void)
 +{
 +      WM_manipulatortype_append(MANIPULATOR_WT_cage_2d);
 +}
 +
 +/** \} */
index d58cdb4b1871ca140f58bc3d2d35fc1c9dd8911f,0000000000000000000000000000000000000000..67fb2419931be324e6f1036abc533884a9859b7e
mode 100644,000000..100644
--- /dev/null
@@@ -1,735 -1,0 +1,735 @@@
- #include "BLI_dial.h"
 +/*
 + * ***** BEGIN GPL LICENSE BLOCK *****
 + *
 + * This program is free software; you can redistribute it and/or
 + * modify it under the terms of the GNU General Public License
 + * as published by the Free Software Foundation; either version 2
 + * of the License, or (at your option) any later version.
 + *
 + * This program is distributed in the hope that it will be useful,
 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 + * GNU General Public License for more details.
 + *
 + * You should have received a copy of the GNU General Public License
 + * along with this program; if not, write to the Free Software Foundation,
 + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 + *
 + * The Original Code is Copyright (C) 2014 Blender Foundation.
 + * All rights reserved.
 + *
 + * Contributor(s): Blender Foundation
 + *
 + * ***** END GPL LICENSE BLOCK *****
 + */
 +
 +/** \file cage3d_manipulator.c
 + *  \ingroup wm
 + *
 + * \name Cage Manipulator
 + *
 + * 2D Manipulator
 + *
 + * \brief Rectangular manipulator acting as a 'cage' around its content.
 + * Interacting scales or translates the manipulator.
 + */
 +
 +#include "MEM_guardedalloc.h"
 +
 +#include "BLI_math.h"
++#include "BLI_dial_2d.h"
 +#include "BLI_rect.h"
 +
 +#include "BKE_context.h"
 +
 +#include "BIF_gl.h"
 +
 +#include "GPU_matrix.h"
 +#include "GPU_shader.h"
 +#include "GPU_immediate.h"
 +#include "GPU_immediate_util.h"
 +#include "GPU_select.h"
 +
 +#include "RNA_access.h"
 +#include "RNA_define.h"
 +
 +#include "WM_api.h"
 +#include "WM_types.h"
 +
 +#include "ED_screen.h"
 +#include "ED_view3d.h"
 +#include "ED_manipulator_library.h"
 +
 +/* own includes */
 +#include "../manipulator_library_intern.h"
 +
 +#define MANIPULATOR_RESIZER_SIZE 10.0f
 +#define MANIPULATOR_MARGIN_OFFSET_SCALE 1.5f
 +
 +static void manipulator_calc_matrix_final_no_offset(
 +        const wmManipulator *mpr, float orig_matrix_final_no_offset[4][4], bool use_space)
 +{
 +      float mat_identity[4][4];
 +      struct WM_ManipulatorMatrixParams params = {NULL};
 +      unit_m4(mat_identity);
 +      if (use_space == false) {
 +              params.matrix_basis = mat_identity;
 +      }
 +      params.matrix_offset = mat_identity;
 +      WM_manipulator_calc_matrix_final_params(mpr, &params, orig_matrix_final_no_offset);
 +}
 +
 +static void manipulator_calc_rect_view_scale(
 +        const wmManipulator *mpr, const float dims[3], float scale[3])
 +{
 +      UNUSED_VARS(dims);
 +
 +      /* Unlike cage2d, no need to correct for aspect. */
 +      float matrix_final_no_offset[4][4];
 +
 +      float x_axis[3], y_axis[3], z_axis[3];
 +      manipulator_calc_matrix_final_no_offset(mpr, matrix_final_no_offset, false);
 +      mul_v3_mat3_m4v3(x_axis, matrix_final_no_offset, mpr->matrix_offset[0]);
 +      mul_v3_mat3_m4v3(y_axis, matrix_final_no_offset, mpr->matrix_offset[1]);
 +      mul_v3_mat3_m4v3(z_axis, matrix_final_no_offset, mpr->matrix_offset[2]);
 +
 +      scale[0] = 1.0f / len_v3(x_axis);
 +      scale[1] = 1.0f / len_v3(y_axis);
 +      scale[2] = 1.0f / len_v3(z_axis);
 +}
 +
 +static void manipulator_calc_rect_view_margin(
 +        const wmManipulator *mpr, const float dims[3], float margin[3])
 +{
 +      float handle_size;
 +      if (mpr->parent_mgroup->type->flag & WM_MANIPULATORGROUPTYPE_3D) {
 +              handle_size = 0.15f;
 +      }
 +      else {
 +              handle_size = MANIPULATOR_RESIZER_SIZE;
 +      }
 +      // XXX, the scale isn't taking offset into account, we need to calculate scale per handle!
 +      // handle_size *= mpr->scale_final;
 +
 +      float scale_xyz[3];
 +      manipulator_calc_rect_view_scale(mpr, dims, scale_xyz);
 +      margin[0] = ((handle_size * scale_xyz[0]));
 +      margin[1] = ((handle_size * scale_xyz[1]));
 +      margin[2] = ((handle_size * scale_xyz[2]));
 +}
 +
 +/* -------------------------------------------------------------------- */
 +
 +static void manipulator_rect_pivot_from_scale_part(int part, float r_pt[3], bool r_constrain_axis[3])
 +{
 +      if (part >= ED_MANIPULATOR_CAGE3D_PART_SCALE_MIN_X_MIN_Y_MIN_Z &&
 +          part <= ED_MANIPULATOR_CAGE3D_PART_SCALE_MAX_X_MAX_Y_MAX_Z)
 +      {
 +              int index = (part - ED_MANIPULATOR_CAGE3D_PART_SCALE_MIN_X_MIN_Y_MIN_Z);
 +              int range[3];
 +              range[2] = index % 3;
 +              index    = index / 3;
 +              range[1] = index % 3;
 +              index    = index / 3;
 +              range[0] = index % 3;
 +
 +              const float sign[3] = {-0.5f, 0.0f, 0.5f};
 +              for (int i = 0; i < 3; i++) {
 +                      r_pt[i] = 0.5 * sign[range[i]];
 +                      r_constrain_axis[i] = (range[i] == 1);
 +              }
 +      }
 +}
 +
 +/* -------------------------------------------------------------------- */
 +/** \name Box Draw Style
 + *
 + * Useful for 3D views, see: #ED_MANIPULATOR_CAGE2D_STYLE_BOX
 + * \{ */
 +
 +static void cage3d_draw_box_corners(
 +        const float r[3], const float margin[3], const float color[3])
 +{
 +      uint pos = GWN_vertformat_attr_add(immVertexFormat(), "pos", GWN_COMP_F32, 3, GWN_FETCH_FLOAT);
 +      UNUSED_VARS(margin);
 +
 +      immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR);
 +      immUniformColor3fv(color);
 +
 +      imm_draw_cube_wire_3d(pos, (float[3]){0}, r);
 +
 +      immUnbindProgram();
 +}
 +
 +static void cage3d_draw_box_interaction(
 +        const float color[4], const int highlighted,
 +        const float size[3], const float margin[3])
 +{
 +      if (highlighted >= ED_MANIPULATOR_CAGE3D_PART_SCALE_MIN_X_MIN_Y_MIN_Z &&
 +          highlighted <= ED_MANIPULATOR_CAGE3D_PART_SCALE_MAX_X_MAX_Y_MAX_Z)
 +      {
 +              int index = (highlighted - ED_MANIPULATOR_CAGE3D_PART_SCALE_MIN_X_MIN_Y_MIN_Z);
 +              int range[3];
 +              range[2] = index % 3;
 +              index    = index / 3;
 +              range[1] = index % 3;
 +              index    = index / 3;
 +              range[0] = index % 3;
 +
 +              const float sign[3] = {-1.0f, 0.0f, 1.0f};
 +              float co[3];
 +
 +              for (int i = 0; i < 3; i++) {
 +                      co[i] = size[i] * sign[range[i]];
 +              }
 +              const float rad[3] = {margin[0] / 3, margin[1] / 3, margin[2] / 3};
 +
 +              {
 +                      uint pos = GWN_vertformat_attr_add(immVertexFormat(), "pos", GWN_COMP_F32, 3, GWN_FETCH_FLOAT);
 +                      immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR);
 +                      immUniformColor3fv(color);
 +                      imm_draw_cube_fill_3d(pos, co, rad);
 +                      immUnbindProgram();
 +              }
 +      }
 +}
 +
 +/** \} */
 +
 +/* -------------------------------------------------------------------- */
 +/** \name Circle Draw Style
 + *
 + * Useful for 2D views, see: #ED_MANIPULATOR_CAGE2D_STYLE_CIRCLE
 + * \{ */
 +
 +static void imm_draw_point_aspect_3d(
 +        uint pos, const float co[3], const float rad[3], bool solid)
 +{
 +      if (solid) {
 +              imm_draw_cube_fill_3d(pos, co, rad);
 +      }
 +      else {
 +              imm_draw_cube_wire_3d(pos, co, rad);
 +      }
 +}
 +
 +static void cage3d_draw_circle_wire(
 +        const float r[3], const float margin[3], const float color[3],
 +        const int transform_flag, const int draw_options)
 +{
 +      uint pos = GWN_vertformat_attr_add(immVertexFormat(), "pos", GWN_COMP_F32, 3, GWN_FETCH_FLOAT);
 +
 +      immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR);
 +      immUniformColor3fv(color);
 +
 +      imm_draw_cube_wire_3d(pos, (float[3]){0}, r);
 +
 +#if 0
 +      if (transform_flag & ED_MANIPULATOR_CAGE2D_XFORM_FLAG_TRANSLATE) {
 +              if (draw_options & ED_MANIPULATOR_CAGE2D_DRAW_FLAG_XFORM_CENTER_HANDLE) {
 +                      const float rad[2] = {margin[0] / 2, margin[1] / 2};
 +                      const float center[2] = {0.0f, 0.0f};
 +
 +                      immBegin(GWN_PRIM_LINES, 4);
 +                      immVertex2f(pos, center[0] - rad[0], center[1] - rad[1]);
 +                      immVertex2f(pos, center[0] + rad[0], center[1] + rad[1]);
 +                      immVertex2f(pos, center[0] + rad[0], center[1] - rad[1]);
 +                      immVertex2f(pos, center[0] - rad[0], center[1] + rad[1]);
 +                      immEnd();
 +              }
 +      }
 +#else
 +      UNUSED_VARS(margin, transform_flag, draw_options);
 +#endif
 +
 +
 +      immUnbindProgram();
 +}
 +
 +static void cage3d_draw_circle_handles(
 +        const RegionView3D *rv3d, const float matrix_final[4][4],
 +        const float r[3], const float margin[3], const float color[3],
 +        bool solid, float scale)
 +{
 +      uint pos = GWN_vertformat_attr_add(immVertexFormat(), "pos", GWN_COMP_F32, 3, GWN_FETCH_FLOAT);
 +      const float rad[3] = {margin[0] / 3, margin[1] / 3, margin[2] / 3};
 +
 +      immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR);
 +      immUniformColor3fv(color);
 +
 +      float sign[3] = {-1.0f, 0.0f, 1.0f};
 +      for (int x = 0; x < 3; x++) {
 +              for (int y = 0; y < 3; y++) {
 +                      for (int z = 0; z < 3; z++) {
 +                              if (x == 1 && y == 1 && z == 1) {
 +                                      continue;
 +                              }
 +                              const float co[3] = {r[0] * sign[x], r[1] * sign[y], r[2] * sign[z]};
 +                              float co_test[3];
 +                              mul_v3_m4v3(co_test, matrix_final, co);
 +                              float rad_scale[3];
 +                              mul_v3_v3fl(rad_scale, rad, ED_view3d_pixel_size(rv3d, co_test) * scale);
 +                              imm_draw_point_aspect_3d(pos, co, rad_scale, solid);
 +                      }
 +              }
 +      }
 +
 +      immUnbindProgram();
 +}
 +
 +/** \} */
 +
 +static void manipulator_cage3d_draw_intern(
 +        RegionView3D *rv3d,
 +        wmManipulator *mpr, const bool select, const bool highlight, const int select_id)
 +{
 +      // const bool use_clamp = (mpr->parent_mgroup->type->flag & WM_MANIPULATORGROUPTYPE_3D) == 0;
 +      float dims[3];
 +      RNA_float_get_array(mpr->ptr, "dimensions", dims);
 +      float matrix_final[4][4];
 +
 +      const int transform_flag = RNA_enum_get(mpr->ptr, "transform");
 +      const int draw_style = RNA_enum_get(mpr->ptr, "draw_style");
 +      const int draw_options = RNA_enum_get(mpr->ptr, "draw_options");
 +
 +      const float size_real[3] = {dims[0] / 2.0f, dims[1] / 2.0f, dims[2] / 2.0f};
 +
 +      WM_manipulator_calc_matrix_final(mpr, matrix_final);
 +
 +      gpuPushMatrix();
 +      gpuMultMatrix(matrix_final);
 +
 +      float margin[3];
 +      manipulator_calc_rect_view_margin(mpr, dims, margin);
 +
 +      /* Handy for quick testing draw (if it's outside bounds). */
 +      if (false) {
 +              glEnable(GL_BLEND);
 +              uint pos = GWN_vertformat_attr_add(immVertexFormat(), "pos", GWN_COMP_F32, 3, GWN_FETCH_FLOAT);
 +              immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR);
 +              immUniformColor4fv((const float[4]){1, 1, 1, 0.5f});
 +              float s = 0.5f;
 +              immRectf(pos, -s, -s, s, s);
 +              immUnbindProgram();
 +              glDisable(GL_BLEND);
 +      }
 +
 +      if (select) {
 +              /* expand for hotspot */
 +#if 0
 +              const float size[3] = {
 +                  size_real[0] + margin[0] / 2,
 +                  size_real[1] + margin[1] / 2,
 +                  size_real[2] + margin[2] / 2,
 +              };
 +#else
 +              /* just use same value for now. */
 +              const float size[3] = {UNPACK3(size_real)};
 +#endif
 +
 +
 +              if (transform_flag & ED_MANIPULATOR_CAGE2D_XFORM_FLAG_SCALE) {
 +                      for (int i = ED_MANIPULATOR_CAGE3D_PART_SCALE_MIN_X_MIN_Y_MIN_Z;
 +                           i <= ED_MANIPULATOR_CAGE3D_PART_SCALE_MAX_X_MAX_Y_MAX_Z;
 +                           i++)
 +                      {
 +                              if (i == ED_MANIPULATOR_CAGE3D_PART_SCALE_MID_X_MID_Y_MID_Z) {
 +                                      continue;
 +                              }
 +                              GPU_select_load_id(select_id | i);
 +                              cage3d_draw_box_interaction(
 +                                      mpr->color, i, size, margin);
 +                      }
 +              }
 +              if (transform_flag & ED_MANIPULATOR_CAGE2D_XFORM_FLAG_TRANSLATE) {
 +                      const int transform_part = ED_MANIPULATOR_CAGE3D_PART_TRANSLATE;
 +                      GPU_select_load_id(select_id | transform_part);
 +                      cage3d_draw_box_interaction(
 +                              mpr->color, transform_part, size, margin);
 +              }
 +      }
 +      else {
 +#if 0
 +              const rctf _r = {
 +                      .xmin = -size_real[0],
 +                      .ymin = -size_real[1],
 +                      .xmax = size_real[0],
 +                      .ymax = size_real[1],
 +              };
 +#endif
 +              if (draw_style == ED_MANIPULATOR_CAGE2D_STYLE_BOX) {
 +                      /* corner manipulators */
 +                      glLineWidth(mpr->line_width + 3.0f);
 +                      cage3d_draw_box_corners(size_real, margin, (const float[3]){0, 0, 0});
 +
 +                      /* corner manipulators */
 +                      float color[4];
 +                      manipulator_color_get(mpr, highlight, color);
 +                      glLineWidth(mpr->line_width);
 +                      cage3d_draw_box_corners(size_real, margin, color);
 +
 +                      bool show = false;
 +                      if (mpr->highlight_part == ED_MANIPULATOR_CAGE3D_PART_TRANSLATE) {
 +                              /* Only show if we're drawing the center handle
 +                               * otherwise the entire rectangle is the hotspot. */
 +                              if (draw_options & ED_MANIPULATOR_CAGE2D_DRAW_FLAG_XFORM_CENTER_HANDLE) {
 +                                      show = true;
 +                              }
 +                      }
 +                      else {
 +                              show = true;
 +                      }
 +
 +                      if (show) {
 +                              cage3d_draw_box_interaction(
 +                                      mpr->color, mpr->highlight_part, size_real, margin);
 +                      }
 +              }
 +              else if (draw_style == ED_MANIPULATOR_CAGE2D_STYLE_CIRCLE) {
 +                      float color[4];
 +                      manipulator_color_get(mpr, highlight, color);
 +
 +                      glEnable(GL_LINE_SMOOTH);
 +                      glEnable(GL_POLYGON_SMOOTH);
 +                      glEnable(GL_BLEND);
 +
 +                      glLineWidth(mpr->line_width + 3.0f);
 +                      cage3d_draw_circle_wire(size_real, margin, (const float[3]){0, 0, 0}, transform_flag, draw_options);
 +                      glLineWidth(mpr->line_width);
 +                      cage3d_draw_circle_wire(size_real, margin, color, transform_flag, draw_options);
 +
 +                      /* corner manipulators */
 +                      cage3d_draw_circle_handles(rv3d, matrix_final, size_real, margin, (const float[3]){0, 0, 0}, true, 60);
 +                      cage3d_draw_circle_handles(rv3d, matrix_final, size_real, margin, color, true, 40);
 +
 +                      glDisable(GL_BLEND);
 +                      glDisable(GL_POLYGON_SMOOTH);
 +                      glDisable(GL_LINE_SMOOTH);
 +              }
 +              else {
 +                      BLI_assert(0);
 +              }
 +      }
 +
 +      glLineWidth(1.0);
 +      gpuPopMatrix();
 +}
 +
 +/**
 + * For when we want to draw 3d cage in 3d views.
 + */
 +static void manipulator_cage3d_draw_select(const bContext *C, wmManipulator *mpr, int select_id)
 +{
 +      ARegion *ar = CTX_wm_region(C);
 +      RegionView3D *rv3d = ar->regiondata;
 +      manipulator_cage3d_draw_intern(rv3d, mpr, true, false, select_id);
 +}
 +
 +static void manipulator_cage3d_draw(const bContext *C, wmManipulator *mpr)
 +{
 +      ARegion *ar = CTX_wm_region(C);
 +      RegionView3D *rv3d = ar->regiondata;
 +      const bool is_highlight = (mpr->state & WM_MANIPULATOR_STATE_HIGHLIGHT) != 0;
 +      manipulator_cage3d_draw_intern(rv3d, mpr, false, is_highlight, -1);
 +}
 +
 +static int manipulator_cage3d_get_cursor(wmManipulator *mpr)
 +{
 +      if (mpr->parent_mgroup->type->flag & WM_MANIPULATORGROUPTYPE_3D) {
 +              return BC_NSEW_SCROLLCURSOR;
 +      }
 +
 +      return CURSOR_STD;
 +}
 +
 +typedef struct RectTransformInteraction {
 +      float orig_mouse[2];
 +      float orig_matrix_offset[4][4];
 +      float orig_matrix_final_no_offset[4][4];
 +      Dial *dial;
 +} RectTransformInteraction;
 +
 +static void manipulator_cage3d_setup(wmManipulator *mpr)
 +{
 +      mpr->flag |= /* WM_MANIPULATOR_DRAW_MODAL | */ /* TODO */
 +                   WM_MANIPULATOR_DRAW_NO_SCALE;
 +}
 +
 +static int manipulator_cage3d_invoke(
 +        bContext *C, wmManipulator *mpr, const wmEvent *event)
 +{
 +      RectTransformInteraction *data = MEM_callocN(sizeof(RectTransformInteraction), "cage_interaction");
 +
 +      copy_m4_m4(data->orig_matrix_offset, mpr->matrix_offset);
 +      manipulator_calc_matrix_final_no_offset(mpr, data->orig_matrix_final_no_offset, true);
 +
 +      if (manipulator_window_project_2d(
 +              C, mpr, (const float[2]){UNPACK2(event->mval)}, 2, false, data->orig_mouse) == 0)
 +      {
 +              zero_v2(data->orig_mouse);
 +      }
 +
 +      mpr->interaction_data = data;
 +
 +      return OPERATOR_RUNNING_MODAL;
 +}
 +
 +/* XXX. this isn't working properly, for now rely on the modal operators. */
 +static int manipulator_cage3d_modal(
 +        bContext *C, wmManipulator *mpr, const wmEvent *event,
 +        eWM_ManipulatorTweak UNUSED(tweak_flag))
 +{
 +      /* For transform logic to be managable we operate in -0.5..0.5 2D space,
 +       * no matter the size of the rectangle, mouse coorts are scaled to unit space.
 +       * The mouse coords have been projected into the matrix so we don't need to worry about axis alignment.
 +       *
 +       * - The cursor offset are multiplied by 'dims'.
 +       * - Matrix translation is also multiplied by 'dims'.
 +       */
 +      RectTransformInteraction *data = mpr->interaction_data;
 +      float point_local[2];
 +
 +      float dims[3];
 +      RNA_float_get_array(mpr->ptr, "dimensions", dims);
 +
 +      {
 +              float matrix_back[4][4];
 +              copy_m4_m4(matrix_back, mpr->matrix_offset);
 +              copy_m4_m4(mpr->matrix_offset, data->orig_matrix_offset);
 +
 +              bool ok = manipulator_window_project_2d(
 +                      C, mpr, (const float[2]){UNPACK2(event->mval)}, 2, false, point_local);
 +              copy_m4_m4(mpr->matrix_offset, matrix_back);
 +              if (!ok) {
 +                      return OPERATOR_RUNNING_MODAL;
 +              }
 +      }
 +
 +      const int transform_flag = RNA_enum_get(mpr->ptr, "transform");
 +      wmManipulatorProperty *mpr_prop;
 +
 +      mpr_prop = WM_manipulator_target_property_find(mpr, "matrix");
 +      if (mpr_prop->type != NULL) {
 +              WM_manipulator_target_property_value_get_array(mpr, mpr_prop, &mpr->matrix_offset[0][0]);
 +      }
 +
 +      if (mpr->highlight_part == ED_MANIPULATOR_CAGE3D_PART_TRANSLATE) {
 +              /* do this to prevent clamping from changing size */
 +              copy_m4_m4(mpr->matrix_offset, data->orig_matrix_offset);
 +              mpr->matrix_offset[3][0] = data->orig_matrix_offset[3][0] + (point_local[0] - data->orig_mouse[0]);
 +              mpr->matrix_offset[3][1] = data->orig_matrix_offset[3][1] + (point_local[1] - data->orig_mouse[1]);
 +      }
 +      else if (mpr->highlight_part == ED_MANIPULATOR_CAGE3D_PART_ROTATE) {
 +
 +#define MUL_V2_V3_M4_FINAL(test_co, mouse_co) \
 +              mul_v3_m4v3(test_co, data->orig_matrix_final_no_offset, ((const float[3]){UNPACK2(mouse_co), 0.0}))
 +
 +              float test_co[3];
 +
 +              if (data->dial == NULL) {
 +                      MUL_V2_V3_M4_FINAL(test_co, data->orig_matrix_offset[3]);
 +
 +                      data->dial = BLI_dial_initialize(test_co, FLT_EPSILON);
 +
 +                      MUL_V2_V3_M4_FINAL(test_co, data->orig_mouse);
 +                      BLI_dial_angle(data->dial, test_co);
 +              }
 +
 +              /* rotate */
 +              MUL_V2_V3_M4_FINAL(test_co, point_local);
 +              const float angle =  BLI_dial_angle(data->dial, test_co);
 +
 +              float matrix_space_inv[4][4];
 +              float matrix_rotate[4][4];
 +              float pivot[3];
 +
 +              copy_v3_v3(pivot, data->orig_matrix_offset[3]);
 +
 +              invert_m4_m4(matrix_space_inv, mpr->matrix_space);
 +
 +              unit_m4(matrix_rotate);
 +              mul_m4_m4m4(matrix_rotate, matrix_rotate, matrix_space_inv);
 +              rotate_m4(matrix_rotate, 'Z', -angle);
 +              mul_m4_m4m4(matrix_rotate, matrix_rotate, mpr->matrix_space);
 +
 +              zero_v3(matrix_rotate[3]);
 +              transform_pivot_set_m4(matrix_rotate, pivot);
 +
 +              mul_m4_m4m4(mpr->matrix_offset, matrix_rotate, data->orig_matrix_offset);
 +
 +#undef MUL_V2_V3_M4_FINAL
 +      }
 +      else {
 +              /* scale */
 +              copy_m4_m4(mpr->matrix_offset, data->orig_matrix_offset);
 +              float pivot[3];
 +              bool constrain_axis[3] = {false};
 +
 +              if (transform_flag & ED_MANIPULATOR_CAGE2D_XFORM_FLAG_TRANSLATE) {
 +                      manipulator_rect_pivot_from_scale_part(mpr->highlight_part, pivot, constrain_axis);
 +              }
 +              else {
 +                      zero_v3(pivot);
 +              }
 +
 +              /* Cursor deltas scaled to (-0.5..0.5). */
 +              float delta_orig[3], delta_curr[3];
 +
 +              delta_orig[2] = 0.0;
 +              delta_curr[2] = 0.0;
 +
 +              for (int i = 0; i < 2; i++) {
 +                      delta_orig[i] = ((data->orig_mouse[i] - data->orig_matrix_offset[3][i]) / dims[i]) - pivot[i];
 +                      delta_curr[i] = ((point_local[i]      - data->orig_matrix_offset[3][i]) / dims[i]) - pivot[i];
 +              }
 +
 +              float scale[3] = {1.0f, 1.0f, 1.0f};
 +              for (int i = 0; i < 3; i++) {
 +                      if (constrain_axis[i] == false) {
 +                              if (delta_orig[i] < 0.0f) {
 +                                      delta_orig[i] *= -1.0f;
 +                                      delta_curr[i] *= -1.0f;
 +                              }
 +                              const int sign = signum_i(scale[i]);
 +
 +                              scale[i] = 1.0f + ((delta_curr[i] - delta_orig[i]) / len_v3(data->orig_matrix_offset[i]));
 +
 +                              if ((transform_flag & ED_MANIPULATOR_CAGE2D_XFORM_FLAG_SCALE_SIGNED) == 0) {
 +                                      if (sign != signum_i(scale[i])) {
 +                                              scale[i] = 0.0f;
 +                                      }
 +                              }
 +                      }
 +              }
 +
 +              if (transform_flag & ED_MANIPULATOR_CAGE2D_XFORM_FLAG_SCALE_UNIFORM) {
 +                      if (constrain_axis[0] == false && constrain_axis[1] == false) {
 +                              scale[1] = scale[0] = (scale[1] + scale[0]) / 2.0f;
 +                      }
 +                      else if (constrain_axis[0] == false) {
 +                              scale[1] = scale[0];
 +                      }
 +                      else if (constrain_axis[1] == false) {
 +                              scale[0] = scale[1];
 +                      }
 +                      else {
 +                              BLI_assert(0);
 +                      }
 +              }
 +
 +              /* scale around pivot */
 +              float matrix_scale[4][4];
 +              unit_m4(matrix_scale);
 +
 +              mul_v3_fl(matrix_scale[0], scale[0]);
 +              mul_v3_fl(matrix_scale[1], scale[1]);
 +              mul_v3_fl(matrix_scale[2], scale[2]);
 +
 +              transform_pivot_set_m4(
 +                      matrix_scale,
 +                      (const float [3]){pivot[0] * dims[0], pivot[1] * dims[1], pivot[2] * dims[2]});
 +              mul_m4_m4m4(mpr->matrix_offset, data->orig_matrix_offset, matrix_scale);
 +      }
 +
 +      if (mpr_prop->type != NULL) {
 +              WM_manipulator_target_property_value_set_array(C, mpr, mpr_prop, &mpr->matrix_offset[0][0]);
 +      }
 +
 +      /* tag the region for redraw */
 +      ED_region_tag_redraw(CTX_wm_region(C));
 +      WM_event_add_mousemove(C);
 +
 +      return OPERATOR_RUNNING_MODAL;
 +}
 +
 +static void manipulator_cage3d_property_update(wmManipulator *mpr, wmManipulatorProperty *mpr_prop)
 +{
 +      if (STREQ(mpr_prop->type->idname, "matrix")) {
 +              if (WM_manipulator_target_property_array_length(mpr, mpr_prop) == 16) {
 +                      WM_manipulator_target_property_value_get_array(mpr, mpr_prop, &mpr->matrix_offset[0][0]);
 +              }
 +              else {
 +                      BLI_assert(0);
 +              }
 +      }
 +      else {
 +              BLI_assert(0);
 +      }
 +}
 +
 +static void manipulator_cage3d_exit(bContext *C, wmManipulator *mpr, const bool cancel)
 +{
 +      RectTransformInteraction *data = mpr->interaction_data;
 +
 +      MEM_SAFE_FREE(data->dial);
 +
 +      if (!cancel)
 +              return;
 +
 +      wmManipulatorProperty *mpr_prop;
 +
 +      /* reset properties */
 +      mpr_prop = WM_manipulator_target_property_find(mpr, "matrix");
 +      if (mpr_prop->type != NULL) {
 +              WM_manipulator_target_property_value_set_array(C, mpr, mpr_prop, &data->orig_matrix_offset[0][0]);
 +      }
 +
 +      copy_m4_m4(mpr->matrix_offset, data->orig_matrix_offset);
 +}
 +
 +
 +/* -------------------------------------------------------------------- */
 +/** \name Cage Manipulator API
 + *
 + * \{ */
 +
 +static void MANIPULATOR_WT_cage_3d(wmManipulatorType *wt)
 +{
 +      /* identifiers */
 +      wt->idname = "MANIPULATOR_WT_cage_3d";
 +
 +      /* api callbacks */
 +      wt->draw = manipulator_cage3d_draw;
 +      wt->draw_select = manipulator_cage3d_draw_select;
 +      wt->setup = manipulator_cage3d_setup;
 +      wt->invoke = manipulator_cage3d_invoke;
 +      wt->property_update = manipulator_cage3d_property_update;
 +      wt->modal = manipulator_cage3d_modal;
 +      wt->exit = manipulator_cage3d_exit;
 +      wt->cursor_get = manipulator_cage3d_get_cursor;
 +
 +      wt->struct_size = sizeof(wmManipulator);
 +
 +      /* rna */
 +      static EnumPropertyItem rna_enum_draw_style[] = {
 +              {ED_MANIPULATOR_CAGE2D_STYLE_BOX, "BOX", 0, "Box", ""},
 +              {ED_MANIPULATOR_CAGE2D_STYLE_CIRCLE, "CIRCLE", 0, "Circle", ""},
 +              {0, NULL, 0, NULL, NULL}
 +      };
 +      static EnumPropertyItem rna_enum_transform[] = {
 +              {ED_MANIPULATOR_CAGE2D_XFORM_FLAG_TRANSLATE, "TRANSLATE", 0, "Translate", ""},
 +              {ED_MANIPULATOR_CAGE2D_XFORM_FLAG_SCALE, "SCALE", 0, "Scale", ""},
 +              {ED_MANIPULATOR_CAGE2D_XFORM_FLAG_SCALE_UNIFORM, "SCALE_UNIFORM", 0, "Scale Uniform", ""},
 +              {0, NULL, 0, NULL, NULL}
 +      };
 +      static EnumPropertyItem rna_enum_draw_options[] = {
 +              {ED_MANIPULATOR_CAGE2D_DRAW_FLAG_XFORM_CENTER_HANDLE, "XFORM_CENTER_HANDLE", 0, "Center Handle", ""},
 +              {0, NULL, 0, NULL, NULL}
 +      };
 +      static float unit_v3[3] = {1.0f, 1.0f, 1.0f};
 +      RNA_def_float_vector(wt->srna, "dimensions", 3, unit_v3, 0, FLT_MAX, "Dimensions", "", 0.0f, FLT_MAX);
 +      RNA_def_enum_flag(wt->srna, "transform", rna_enum_transform, 0, "Transform Options", "");
 +      RNA_def_enum(wt->srna, "draw_style", rna_enum_draw_style, ED_MANIPULATOR_CAGE2D_STYLE_CIRCLE, "Draw Style", "");
 +      RNA_def_enum_flag(
 +              wt->srna, "draw_options", rna_enum_draw_options,
 +              ED_MANIPULATOR_CAGE2D_DRAW_FLAG_XFORM_CENTER_HANDLE, "Draw Options", "");
 +
 +      WM_manipulatortype_target_property_def(wt, "matrix", PROP_FLOAT, 16);
 +}
 +
 +void ED_manipulatortypes_cage_3d(void)
 +{
 +      WM_manipulatortype_append(MANIPULATOR_WT_cage_3d);
 +}
 +
 +/** \} */
index 4a807798c20ac6b01ee8b7737f100eafc9925f36,1cbc6aa39faf83c8ace7ab214709a5927f627564..376e4659b92b2b18fcc31c20e683a4a574f4a8fe
  #include "DNA_view3d_types.h"
  #include "DNA_screen_types.h"
  #include "DNA_space_types.h"
 +#include "DNA_workspace_types.h"
  
  #include "BLI_math.h"
- #include "BLI_lasso.h"
+ #include "BLI_lasso_2d.h"
  #include "BLI_listbase.h"
  #include "BLI_string.h"
  #include "BLI_kdtree.h"
index a41b133a189716b1ae429aebe1e726fdbd37d81f,f999fab88ac978ca5764d0c0edfa1f39cd6bc59f..6e3a0883489d0b9dd868ed25992cdf80a4c947f4
   *  \ingroup spview3d
   */
  
 -#include <string.h>
 -#include <stdio.h>
  #include <math.h>
  
 -#include "DNA_armature_types.h"
 -#include "DNA_camera_types.h"
 -#include "DNA_customdata_types.h"
 -#include "DNA_object_types.h"
 -#include "DNA_group_types.h"
 -#include "DNA_mesh_types.h"
 -#include "DNA_key_types.h"
 -#include "DNA_lamp_types.h"
 -#include "DNA_scene_types.h"
 -#include "DNA_world_types.h"
 -#include "DNA_brush_types.h"
 -
 -#include "MEM_guardedalloc.h"
 -
 -#include "BLI_blenlib.h"
 +#include "BLI_listbase.h"
  #include "BLI_math.h"
 -#include "BLI_jitter_2d.h"
 -#include "BLI_utildefines.h"
 -#include "BLI_endian_switch.h"
 +#include "BLI_rect.h"
 +#include "BLI_string.h"
  #include "BLI_threads.h"
- #include "BLI_jitter.h"
++#include "BLI_jitter_2d.h"
 +
 +#include "BIF_gl.h"
 +#include "BIF_glutil.h"
  
 -#include "BKE_anim.h"
  #include "BKE_camera.h"
  #include "BKE_context.h"
 -#include "BKE_customdata.h"
 -#include "BKE_DerivedMesh.h"
 -#include "BKE_image.h"
 +#include "BKE_global.h"
  #include "BKE_key.h"
 -#include "BKE_main.h"
 +#include "BKE_scene.h"
  #include "BKE_object.h"
 -#include "BKE_global.h"
  #include "BKE_paint.h"
 -#include "BKE_scene.h"
 -#include "BKE_screen.h"
  #include "BKE_unit.h"
 -#include "BKE_movieclip.h"
 -
 -#include "RE_engine.h"
  
 -#include "IMB_imbuf_types.h"
 -#include "IMB_imbuf.h"
 -#include "IMB_colormanagement.h"
 +#include "BLF_api.h"
  
 -#include "BIF_gl.h"
 -#include "BIF_glutil.h"
 +#include "BLT_translation.h"
  
 -#include "WM_api.h"
 +#include "DNA_armature_types.h"
 +#include "DNA_brush_types.h"
 +#include "DNA_camera_types.h"
 +#include "DNA_key_types.h"
 +#include "DNA_mesh_types.h"
 +#include "DNA_object_types.h"
 +#include "DNA_view3d_types.h"
 +#include "DNA_windowmanager_types.h"
  
 -#include "BLF_api.h"
 -#include "BLT_translation.h"
 +#include "DRW_engine.h"
  
  #include "ED_armature.h"
  #include "ED_keyframing.h"
index ff9da162777cfbe42b18f1ece04b3bdc11fa867d,0000000000000000000000000000000000000000..6a746ffe27c7f135e46c4b7184e46dd4e544f811
mode 100644,000000..100644
--- /dev/null
@@@ -1,2182 -1,0 +1,2182 @@@
- #include "BLI_jitter.h"
 +/*
 + * ***** BEGIN GPL LICENSE BLOCK *****
 + *
 + * This program is free software; you can redistribute it and/or
 + * modify it under the terms of the GNU General Public License
 + * as published by the Free Software Foundation; either version 2
 + * of the License, or (at your option) any later version. 
 + *
 + * This program is distributed in the hope that it will be useful,
 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 + * GNU General Public License for more details.
 + *
 + * You should have received a copy of the GNU General Public License
 + * along with this program; if not, write to the Free Software Foundation,
 + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 + *
 + * The Original Code is Copyright (C) 2008 Blender Foundation.
 + * All rights reserved.
 + *
 + * 
 + * Contributor(s): Blender Foundation
 + *
 + * ***** END GPL LICENSE BLOCK *****
 + */
 +
 +/** \file blender/editors/space_view3d/view3d_draw_legacy.c
 + *  \ingroup spview3d
 + */
 +
 +#include <string.h>
 +#include <stdio.h>
 +#include <math.h>
 +
 +#include "DNA_armature_types.h"
 +#include "DNA_camera_types.h"
 +#include "DNA_customdata_types.h"
 +#include "DNA_object_types.h"
 +#include "DNA_group_types.h"
 +#include "DNA_mesh_types.h"
 +#include "DNA_key_types.h"
 +#include "DNA_lamp_types.h"
 +#include "DNA_scene_types.h"
 +#include "DNA_world_types.h"
 +#include "DNA_brush_types.h"
 +
 +#include "MEM_guardedalloc.h"
 +
 +#include "BLI_blenlib.h"
 +#include "BLI_math.h"
++#include "BLI_jitter_2d.h"
 +#include "BLI_utildefines.h"
 +#include "BLI_endian_switch.h"
 +#include "BLI_threads.h"
 +
 +#include "BKE_anim.h"
 +#include "BKE_camera.h"
 +#include "BKE_context.h"
 +#include "BKE_customdata.h"
 +#include "BKE_DerivedMesh.h"
 +#include "BKE_image.h"
 +#include "BKE_key.h"
 +#include "BKE_layer.h"
 +#include "BKE_main.h"
 +#include "BKE_object.h"
 +#include "BKE_global.h"
 +#include "BKE_paint.h"
 +#include "BKE_scene.h"
 +#include "BKE_screen.h"
 +#include "BKE_unit.h"
 +#include "BKE_movieclip.h"
 +
 +#include "DEG_depsgraph.h"
 +
 +#include "IMB_imbuf_types.h"
 +#include "IMB_imbuf.h"
 +#include "IMB_colormanagement.h"
 +
 +#include "BIF_glutil.h"
 +
 +#include "WM_api.h"
 +#include "WM_types.h"
 +
 +#include "BLF_api.h"
 +#include "BLT_translation.h"
 +
 +#include "ED_armature.h"
 +#include "ED_keyframing.h"
 +#include "ED_gpencil.h"
 +#include "ED_screen.h"
 +#include "ED_space_api.h"
 +#include "ED_screen_types.h"
 +#include "ED_transform.h"
 +
 +#include "UI_interface.h"
 +#include "UI_interface_icons.h"
 +#include "UI_resources.h"
 +
 +#include "GPU_draw.h"
 +#include "GPU_framebuffer.h"
 +#include "GPU_lamp.h"
 +#include "GPU_material.h"
 +#include "GPU_compositing.h"
 +#include "GPU_extensions.h"
 +#include "GPU_immediate.h"
 +#include "GPU_immediate_util.h"
 +#include "GPU_select.h"
 +#include "GPU_matrix.h"
 +
 +#include "RE_engine.h"
 +
 +#include "DRW_engine.h"
 +
 +#include "view3d_intern.h"  /* own include */
 +
 +/* ********* custom clipping *********** */
 +
 +static void view3d_draw_clipping(RegionView3D *rv3d)
 +{
 +      BoundBox *bb = rv3d->clipbb;
 +
 +      if (bb) {
 +              const unsigned int clipping_index[6][4] = {
 +                      {0, 1, 2, 3},
 +                      {0, 4, 5, 1},
 +                      {4, 7, 6, 5},
 +                      {7, 3, 2, 6},
 +                      {1, 5, 6, 2},
 +                      {7, 4, 0, 3}
 +              };
 +
 +              /* fill in zero alpha for rendering & re-projection [#31530] */
 +              unsigned char col[4];
 +              UI_GetThemeColor4ubv(TH_V3D_CLIPPING_BORDER, col);
 +              glColor4ubv(col);
 +
 +              glEnable(GL_BLEND);
 +              glEnableClientState(GL_VERTEX_ARRAY);
 +              glVertexPointer(3, GL_FLOAT, 0, bb->vec);
 +              glDrawElements(GL_QUADS, sizeof(clipping_index) / sizeof(unsigned int), GL_UNSIGNED_INT, clipping_index);
 +              glDisableClientState(GL_VERTEX_ARRAY);
 +              glDisable(GL_BLEND);
 +      }
 +}
 +
 +void ED_view3d_clipping_set(RegionView3D *rv3d)
 +{
 +      double plane[4];
 +      const unsigned int tot = (rv3d->viewlock & RV3D_BOXCLIP) ? 4 : 6;
 +
 +      for (unsigned a = 0; a < tot; a++) {
 +              copy_v4db_v4fl(plane, rv3d->clip[a]);
 +              glClipPlane(GL_CLIP_PLANE0 + a, plane);
 +              glEnable(GL_CLIP_PLANE0 + a);
 +      }
 +}
 +
 +/* use these to temp disable/enable clipping when 'rv3d->rflag & RV3D_CLIPPING' is set */
 +void ED_view3d_clipping_disable(void)
 +{
 +      for (unsigned a = 0; a < 6; a++) {
 +              glDisable(GL_CLIP_PLANE0 + a);
 +      }
 +}
 +void ED_view3d_clipping_enable(void)
 +{
 +      for (unsigned a = 0; a < 6; a++) {
 +              glEnable(GL_CLIP_PLANE0 + a);
 +      }
 +}
 +
 +static bool view3d_clipping_test(const float co[3], const float clip[6][4])
 +{
 +      if (plane_point_side_v3(clip[0], co) > 0.0f)
 +              if (plane_point_side_v3(clip[1], co) > 0.0f)
 +                      if (plane_point_side_v3(clip[2], co) > 0.0f)
 +                              if (plane_point_side_v3(clip[3], co) > 0.0f)
 +                                      return false;
 +
 +      return true;
 +}
 +
 +/* for 'local' ED_view3d_clipping_local must run first
 + * then all comparisons can be done in localspace */
 +bool ED_view3d_clipping_test(const RegionView3D *rv3d, const float co[3], const bool is_local)
 +{
 +      return view3d_clipping_test(co, is_local ? rv3d->clip_local : rv3d->clip);
 +}
 +
 +/* ********* end custom clipping *********** */
 +
 +static void draw_view_icon(RegionView3D *rv3d, rcti *rect)
 +{
 +      BIFIconID icon;
 +      
 +      if (ELEM(rv3d->view, RV3D_VIEW_TOP, RV3D_VIEW_BOTTOM))
 +              icon = ICON_AXIS_TOP;
 +      else if (ELEM(rv3d->view, RV3D_VIEW_FRONT, RV3D_VIEW_BACK))
 +              icon = ICON_AXIS_FRONT;
 +      else if (ELEM(rv3d->view, RV3D_VIEW_RIGHT, RV3D_VIEW_LEFT))
 +              icon = ICON_AXIS_SIDE;
 +      else return;
 +      
 +      glEnable(GL_BLEND);
 +      glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA); 
 +      
 +      UI_icon_draw(5.0 + rect->xmin, 5.0 + rect->ymin, icon);
 +      
 +      glDisable(GL_BLEND);
 +}
 +
 +/* *********************** backdraw for selection *************** */
 +
 +static void backdrawview3d(
 +        const struct EvaluationContext *eval_ctx, Scene *scene, ViewLayer *view_layer,
 +        ARegion *ar, View3D *v3d)
 +{
 +      RegionView3D *rv3d = ar->regiondata;
 +      struct Base *base = view_layer->basact;
 +
 +      BLI_assert(ar->regiontype == RGN_TYPE_WINDOW);
 +
 +      if (base && (eval_ctx->object_mode & (OB_MODE_VERTEX_PAINT | OB_MODE_WEIGHT_PAINT) ||
 +                   BKE_paint_select_face_test(base->object, eval_ctx->object_mode)))
 +      {
 +              /* do nothing */
 +      }
 +      /* texture paint mode sampling */
 +      else if (base && (eval_ctx->object_mode & OB_MODE_TEXTURE_PAINT) &&
 +               (v3d->drawtype > OB_WIRE))
 +      {
 +              /* do nothing */
 +      }
 +      else if ((base && (eval_ctx->object_mode & OB_MODE_PARTICLE_EDIT)) &&
 +               V3D_IS_ZBUF(v3d))
 +      {
 +              /* do nothing */
 +      }
 +      else if ((eval_ctx->object_mode & OB_MODE_EDIT) &&
 +               V3D_IS_ZBUF(v3d))
 +      {
 +              /* do nothing */
 +      }
 +      else {
 +              v3d->flag &= ~V3D_INVALID_BACKBUF;
 +              return;
 +      }
 +
 +      if (!(v3d->flag & V3D_INVALID_BACKBUF))
 +              return;
 +
 +#if 0
 +      if (test) {
 +              if (qtest()) {
 +                      addafterqueue(ar->win, BACKBUFDRAW, 1);
 +                      return;
 +              }
 +      }
 +#endif
 +
 +      if (v3d->drawtype > OB_WIRE) v3d->zbuf = true;
 +      
 +      /* dithering and AA break color coding, so disable */
 +      glDisable(GL_DITHER);
 +
 +      if (false) {
 +              /* for multisample we use an offscreen FBO. multisample drawing can fail
 +               * with color coded selection drawing, and reading back depths from such
 +               * a buffer can also cause a few seconds freeze on OS X / NVidia.
 +               *
 +               * NOTE: code is no longer used now, but offscreen drawing is likely
 +               * what we will always want to do for the new viewport. */
 +              int w = BLI_rcti_size_x(&ar->winrct);
 +              int h = BLI_rcti_size_y(&ar->winrct);
 +              char error[256];
 +
 +              if (rv3d->gpuoffscreen) {
 +                      if (GPU_offscreen_width(rv3d->gpuoffscreen)  != w ||
 +                          GPU_offscreen_height(rv3d->gpuoffscreen) != h)
 +                      {
 +                              GPU_offscreen_free(rv3d->gpuoffscreen);
 +                              rv3d->gpuoffscreen = NULL;
 +                      }
 +              }
 +
 +              if (!rv3d->gpuoffscreen) {
 +                      rv3d->gpuoffscreen = GPU_offscreen_create(w, h, 0, true, false, error);
 +
 +                      if (!rv3d->gpuoffscreen)
 +                              fprintf(stderr, "Failed to create offscreen selection buffer for multisample: %s\n", error);
 +              }
 +      }
 +
 +      if (rv3d->gpuoffscreen)
 +              GPU_offscreen_bind(rv3d->gpuoffscreen, true);
 +      else
 +              glScissor(ar->winrct.xmin, ar->winrct.ymin, BLI_rcti_size_x(&ar->winrct), BLI_rcti_size_y(&ar->winrct));
 +
 +      glClearColor(0.0, 0.0, 0.0, 0.0);
 +      if (v3d->zbuf) {
 +              glEnable(GL_DEPTH_TEST);
 +              glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 +      }
 +      else {
 +              glClear(GL_COLOR_BUFFER_BIT);
 +              glDisable(GL_DEPTH_TEST);
 +      }
 +      
 +      if (rv3d->rflag & RV3D_CLIPPING)
 +              ED_view3d_clipping_set(rv3d);
 +      
 +      G.f |= G_BACKBUFSEL;
 +      
 +      if (base && ((base->flag & BASE_VISIBLED) != 0))
 +              draw_object_backbufsel(eval_ctx, scene, v3d, rv3d, base->object);
 +      
 +      if (rv3d->gpuoffscreen)
 +              GPU_offscreen_unbind(rv3d->gpuoffscreen, true);
 +      else
 +              ar->swap = 0; /* mark invalid backbuf for wm draw */
 +
 +      v3d->flag &= ~V3D_INVALID_BACKBUF;
 +
 +      G.f &= ~G_BACKBUFSEL;
 +      v3d->zbuf = false;
 +      glDisable(GL_DEPTH_TEST);
 +      glEnable(GL_DITHER);
 +
 +      if (rv3d->rflag & RV3D_CLIPPING)
 +              ED_view3d_clipping_disable();
 +}
 +
 +void view3d_opengl_read_pixels(ARegion *ar, int x, int y, int w, int h, int format, int type, void *data)
 +{
 +      RegionView3D *rv3d = ar->regiondata;
 +
 +      if (rv3d->gpuoffscreen) {
 +              GPU_offscreen_bind(rv3d->gpuoffscreen, true);
 +              glReadBuffer(GL_COLOR_ATTACHMENT0);
 +              glReadPixels(x, y, w, h, format, type, data);
 +              GPU_offscreen_unbind(rv3d->gpuoffscreen, true);
 +      }
 +      else {
 +              glReadPixels(ar->winrct.xmin + x, ar->winrct.ymin + y, w, h, format, type, data);
 +      }
 +}
 +
 +/* XXX depth reading exception, for code not using gpu offscreen */
 +static void view3d_opengl_read_Z_pixels(ARegion *ar, int x, int y, int w, int h, int format, int type, void *data)
 +{
 +      glReadPixels(ar->winrct.xmin + x, ar->winrct.ymin + y, w, h, format, type, data);
 +}
 +
 +void ED_view3d_backbuf_validate(const struct EvaluationContext *eval_ctx, ViewContext *vc)
 +{
 +      if (vc->v3d->flag & V3D_INVALID_BACKBUF) {
 +              backdrawview3d(eval_ctx, vc->scene, vc->view_layer, vc->ar, vc->v3d);
 +      }
 +}
 +
 +/**
 + * allow for small values [0.5 - 2.5],
 + * and large values, FLT_MAX by clamping by the area size
 + */
 +int ED_view3d_backbuf_sample_size_clamp(ARegion *ar, const float dist)
 +{
 +      return (int)min_ff(ceilf(dist), (float)max_ii(ar->winx, ar->winx));
 +}
 +
 +/* samples a single pixel (copied from vpaint) */
 +unsigned int ED_view3d_backbuf_sample(
 +        const EvaluationContext *eval_ctx, ViewContext *vc, int x, int y)
 +{
 +      if (x >= vc->ar->winx || y >= vc->ar->winy) {
 +              return 0;
 +      }
 +
 +      ED_view3d_backbuf_validate(eval_ctx, vc);
 +
 +      unsigned int col;
 +      view3d_opengl_read_pixels(vc->ar, x, y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, &col);
 +      glReadBuffer(GL_BACK);
 +
 +      if (ENDIAN_ORDER == B_ENDIAN) {
 +              BLI_endian_switch_uint32(&col);
 +      }
 +
 +      return GPU_select_to_index(col);
 +}
 +
 +/* reads full rect, converts indices */
 +ImBuf *ED_view3d_backbuf_read(
 +        const EvaluationContext *eval_ctx, ViewContext *vc, int xmin, int ymin, int xmax, int ymax)
 +{
 +      /* clip */
 +      const rcti clip = {
 +          max_ii(xmin, 0), min_ii(xmax, vc->ar->winx - 1),
 +          max_ii(ymin, 0), min_ii(ymax, vc->ar->winy - 1)};
 +      const int size_clip[2] = {
 +          BLI_rcti_size_x(&clip) + 1,
 +          BLI_rcti_size_y(&clip) + 1};
 +
 +      if (UNLIKELY((clip.xmin > clip.xmax) ||
 +                   (clip.ymin > clip.ymax)))
 +      {
 +              return NULL;
 +      }
 +
 +      ImBuf *ibuf_clip = IMB_allocImBuf(size_clip[0], size_clip[1], 32, IB_rect);
 +
 +      ED_view3d_backbuf_validate(eval_ctx, vc);
 +
 +      view3d_opengl_read_pixels(vc->ar, clip.xmin, clip.ymin, size_clip[0], size_clip[1], GL_RGBA, GL_UNSIGNED_BYTE, ibuf_clip->rect);
 +
 +      glReadBuffer(GL_BACK);
 +
 +      if (ENDIAN_ORDER == B_ENDIAN) {
 +              IMB_convert_rgba_to_abgr(ibuf_clip);
 +      }
 +
 +      GPU_select_to_index_array(ibuf_clip->rect, size_clip[0] * size_clip[1]);
 +      
 +      if ((clip.xmin == xmin) &&
 +          (clip.xmax == xmax) &&
 +          (clip.ymin == ymin) &&
 +          (clip.ymax == ymax))
 +      {
 +              return ibuf_clip;
 +      }
 +      else {
 +              /* put clipped result into a non-clipped buffer */
 +              const int size[2] = {
 +                  (xmax - xmin + 1),
 +                  (ymax - ymin + 1)};
 +
 +              ImBuf *ibuf_full = IMB_allocImBuf(size[0], size[1], 32, IB_rect);
 +
 +              IMB_rectcpy(
 +                      ibuf_full, ibuf_clip,
 +                      clip.xmin - xmin, clip.ymin - ymin,
 +                      0, 0,
 +                      size_clip[0], size_clip[1]);
 +              IMB_freeImBuf(ibuf_clip);
 +              return ibuf_full;
 +      }
 +}
 +
 +/* smart function to sample a rect spiralling outside, nice for backbuf selection */
 +unsigned int ED_view3d_backbuf_sample_rect(
 +        const EvaluationContext *eval_ctx, ViewContext *vc, const int mval[2], int size,
 +        unsigned int min, unsigned int max, float *r_dist)
 +{
 +      int dirvec[4][2];
 +
 +      const int amount = (size - 1) / 2;
 +
 +      const int minx = mval[0] - (amount + 1);
 +      const int miny = mval[1] - (amount + 1);
 +      ImBuf *buf = ED_view3d_backbuf_read(eval_ctx, vc, minx, miny, minx + size - 1, miny + size - 1);
 +      if (!buf) return 0;
 +
 +      unsigned index = 0;
 +      int rc = 0;
 +      
 +      dirvec[0][0] = 1; dirvec[0][1] = 0;
 +      dirvec[1][0] = 0; dirvec[1][1] = -size;
 +      dirvec[2][0] = -1; dirvec[2][1] = 0;
 +      dirvec[3][0] = 0; dirvec[3][1] = size;
 +      
 +      const unsigned *bufmin = buf->rect;
 +      const unsigned *tbuf = buf->rect;
 +      const unsigned *bufmax = buf->rect + size * size;
 +      tbuf += amount * size + amount;
 +      
 +      for (int nr = 1; nr <= size; nr++) {
 +              for (int a = 0; a < 2; a++) {
 +                      for (int b = 0; b < nr; b++) {
 +                              if (*tbuf && *tbuf >= min && *tbuf < max) {
 +                                      /* we got a hit */
 +
 +                                      /* get x,y pixel coords from the offset
 +                                       * (manhatten distance in keeping with other screen-based selection) */
 +                                      *r_dist = (float)(
 +                                              abs(((int)(tbuf - buf->rect) % size) - (size / 2)) +
 +                                              abs(((int)(tbuf - buf->rect) / size) - (size / 2)));
 +
 +                                      /* indices start at 1 here */
 +                                      index = (*tbuf - min) + 1;
 +                                      goto exit;
 +                              }
 +                              
 +                              tbuf += (dirvec[rc][0] + dirvec[rc][1]);
 +                              
 +                              if (tbuf < bufmin || tbuf >= bufmax) {
 +                                      goto exit;
 +                              }
 +                      }
 +                      rc++;
 +                      rc &= 3;
 +              }
 +      }
 +
 +exit:
 +      IMB_freeImBuf(buf);
 +      return index;
 +}
 +
 +
 +/* ************************************************************* */
 +
 +static void view3d_stereo_bgpic_setup(Scene *scene, View3D *v3d, Image *ima, ImageUser *iuser)
 +{
 +      if (BKE_image_is_stereo(ima)) {
 +              iuser->flag |= IMA_SHOW_STEREO;
 +
 +              if ((scene->r.scemode & R_MULTIVIEW) == 0) {
 +                      iuser->multiview_eye = STEREO_LEFT_ID;
 +              }
 +              else if (v3d->stereo3d_camera != STEREO_3D_ID) {
 +                      /* show only left or right camera */
 +                      iuser->multiview_eye = v3d->stereo3d_camera;
 +              }
 +
 +              BKE_image_multiview_index(ima, iuser);
 +      }
 +      else {
 +              iuser->flag &= ~IMA_SHOW_STEREO;
 +      }
 +}
 +
 +static void view3d_draw_bgpic(Scene *scene, const Depsgraph *depsgraph,
 +                              ARegion *ar, View3D *v3d,
 +                              const bool do_foreground, const bool do_camera_frame)
 +{
 +      RegionView3D *rv3d = ar->regiondata;
 +      int fg_flag = do_foreground ? CAM_BGIMG_FLAG_FOREGROUND : 0;
 +      if (v3d->camera == NULL || v3d->camera->type != OB_CAMERA) {
 +              return;
 +      }
 +      Camera *cam = v3d->camera->data;
 +
 +      for (CameraBGImage *bgpic = cam->bg_images.first; bgpic; bgpic = bgpic->next) {
 +              bgpic->iuser.scene = scene;  /* Needed for render results. */
 +
 +              if ((bgpic->flag & CAM_BGIMG_FLAG_FOREGROUND) != fg_flag)
 +                      continue;
 +
 +              {
 +                      float image_aspect[2];
 +                      float x1, y1, x2, y2, centx, centy;
 +
 +                      void *lock;
 +
 +                      Image *ima = NULL;
 +
 +                      /* disable individual images */
 +                      if ((bgpic->flag & CAM_BGIMG_FLAG_DISABLED))
 +                              continue;
 +
 +                      ImBuf *ibuf = NULL;
 +                      ImBuf *freeibuf = NULL;
 +                      ImBuf *releaseibuf = NULL;
 +                      if (bgpic->source == CAM_BGIMG_SOURCE_IMAGE) {
 +                              ima = bgpic->ima;
 +                              if (ima == NULL)
 +                                      continue;
 +                              BKE_image_user_frame_calc(&bgpic->iuser, CFRA, 0);
 +                              if (ima->source == IMA_SRC_SEQUENCE && !(bgpic->iuser.flag & IMA_USER_FRAME_IN_RANGE)) {
 +                                      ibuf = NULL; /* frame is out of range, dont show */
 +                              }
 +                              else {
 +                                      view3d_stereo_bgpic_setup(scene, v3d, ima, &bgpic->iuser);
 +                                      ibuf = BKE_image_acquire_ibuf(ima, &bgpic->iuser, &lock);
 +                                      releaseibuf = ibuf;
 +                              }
 +
 +                              image_aspect[0] = ima->aspx;
 +                              image_aspect[1] = ima->aspy;
 +                      }
 +                      else if (bgpic->source == CAM_BGIMG_SOURCE_MOVIE) {
 +                              /* TODO: skip drawing when out of frame range (as image sequences do above) */
 +                              MovieClip *clip = NULL;
 +
 +                              if (bgpic->flag & CAM_BGIMG_FLAG_CAMERACLIP) {
 +                                      if (scene->camera)
 +                                              clip = BKE_object_movieclip_get(scene, scene->camera, true);
 +                              }
 +                              else {
 +                                      clip = bgpic->clip;
 +                              }
 +
 +                              if (clip == NULL)
 +                                      continue;
 +
 +                              BKE_movieclip_user_set_frame(&bgpic->cuser, CFRA);
 +                              ibuf = BKE_movieclip_get_ibuf(clip, &bgpic->cuser);
 +
 +                              image_aspect[0] = clip->aspx;
 +                              image_aspect[1] = clip->aspy;
 +
 +                              /* working with ibuf from image and clip has got different workflow now.
 +                               * ibuf acquired from clip is referenced by cache system and should
 +                               * be dereferenced after usage. */
 +                              freeibuf = ibuf;
 +                      }
 +                      else {
 +                              /* perhaps when loading future files... */
 +                              BLI_assert(0);
 +                              copy_v2_fl(image_aspect, 1.0f);
 +                      }
 +
 +                      if (ibuf == NULL)
 +                              continue;
 +
 +                      if ((ibuf->rect == NULL && ibuf->rect_float == NULL) || ibuf->channels != 4) { /* invalid image format */
 +                              if (freeibuf)
 +                                      IMB_freeImBuf(freeibuf);
 +                              if (releaseibuf)
 +                                      BKE_image_release_ibuf(ima, releaseibuf, lock);
 +
 +                              continue;
 +                      }
 +
 +                      if (ibuf->rect == NULL)
 +                              IMB_rect_from_float(ibuf);
 +
 +                      BLI_assert(rv3d->persp == RV3D_CAMOB);
 +                      {
 +                              if (do_camera_frame) {
 +                                      rctf vb;
 +                                      ED_view3d_calc_camera_border(scene, depsgraph, ar, v3d, rv3d, &vb, false);
 +                                      x1 = vb.xmin;
 +                                      y1 = vb.ymin;
 +                                      x2 = vb.xmax;
 +                                      y2 = vb.ymax;
 +                              }
 +                              else {
 +                                      x1 = ar->winrct.xmin;
 +                                      y1 = ar->winrct.ymin;
 +                                      x2 = ar->winrct.xmax;
 +                                      y2 = ar->winrct.ymax;
 +                              }
 +
 +                              /* apply offset last - camera offset is different to offset in blender units */
 +                              /* so this has some sane way of working - this matches camera's shift _exactly_ */
 +                              {
 +                                      const float max_dim = max_ff(x2 - x1, y2 - y1);
 +                                      const float xof_scale = bgpic->offset[0] * max_dim;
 +                                      const float yof_scale = bgpic->offset[1] * max_dim;
 +
 +                                      x1 += xof_scale;
 +                                      y1 += yof_scale;
 +                                      x2 += xof_scale;
 +                                      y2 += yof_scale;
 +                              }
 +
 +                              centx = (x1 + x2) * 0.5f;
 +                              centy = (y1 + y2) * 0.5f;
 +
 +                              /* aspect correction */
 +                              if (bgpic->flag & CAM_BGIMG_FLAG_CAMERA_ASPECT) {
 +                                      /* apply aspect from clip */
 +                                      const float w_src = ibuf->x * image_aspect[0];
 +                                      const float h_src = ibuf->y * image_aspect[1];
 +
 +                                      /* destination aspect is already applied from the camera frame */
 +                                      const float w_dst = x1 - x2;
 +                                      const float h_dst = y1 - y2;
 +
 +                                      const float asp_src = w_src / h_src;
 +                                      const float asp_dst = w_dst / h_dst;
 +
 +                                      if (fabsf(asp_src - asp_dst) >= FLT_EPSILON) {
 +                                              if ((asp_src > asp_dst) == ((bgpic->flag & CAM_BGIMG_FLAG_CAMERA_CROP) != 0)) {
 +                                                      /* fit X */
 +                                                      const float div = asp_src / asp_dst;
 +                                                      x1 = ((x1 - centx) * div) + centx;
 +                                                      x2 = ((x2 - centx) * div) + centx;
 +                                              }
 +                                              else {
 +                                                      /* fit Y */
 +                                                      const float div = asp_dst / asp_src;
 +                                                      y1 = ((y1 - centy) * div) + centy;
 +                                                      y2 = ((y2 - centy) * div) + centy;
 +                                              }
 +                                      }
 +                              }
 +                      }
 +
 +                      /* complete clip? */
 +                      rctf clip_rect;
 +                      BLI_rctf_init(&clip_rect, x1, x2, y1, y2);
 +                      if (bgpic->rotation) {
 +                              BLI_rctf_rotate_expand(&clip_rect, &clip_rect, bgpic->rotation);
 +                      }
 +
 +                      if (clip_rect.xmax < 0 || clip_rect.ymax < 0 || clip_rect.xmin > ar->winx || clip_rect.ymin > ar->winy) {
 +                              if (freeibuf)
 +                                      IMB_freeImBuf(freeibuf);
 +                              if (releaseibuf)
 +                                      BKE_image_release_ibuf(ima, releaseibuf, lock);
 +
 +                              continue;
 +                      }
 +
 +                      float zoomx = (x2 - x1) / ibuf->x;
 +                      float zoomy = (y2 - y1) / ibuf->y;
 +
 +                      /* for some reason; zoomlevels down refuses to use GL_ALPHA_SCALE */
 +                      if (zoomx < 1.0f || zoomy < 1.0f) {
 +                              float tzoom = min_ff(zoomx, zoomy);
 +                              int mip = 0;
 +
 +                              if ((ibuf->userflags & IB_MIPMAP_INVALID) != 0) {
 +                                      IMB_remakemipmap(ibuf, 0);
 +                                      ibuf->userflags &= ~IB_MIPMAP_INVALID;
 +                              }
 +                              else if (ibuf->mipmap[0] == NULL)
 +                                      IMB_makemipmap(ibuf, 0);
 +
 +                              while (tzoom < 1.0f && mip < 8 && ibuf->mipmap[mip]) {
 +                                      tzoom *= 2.0f;
 +                                      zoomx *= 2.0f;
 +                                      zoomy *= 2.0f;
 +                                      mip++;
 +                              }
 +                              if (mip > 0)
 +                                      ibuf = ibuf->mipmap[mip - 1];
 +                      }
 +
 +                      if (v3d->zbuf) glDisable(GL_DEPTH_TEST);
 +                      glDepthMask(GL_FALSE);
 +
 +                      glEnable(GL_BLEND);
 +                      glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
 +
 +                      gpuPushProjectionMatrix();
 +                      gpuPushMatrix();
 +                      ED_region_pixelspace(ar);
 +
 +                      gpuTranslate2f(centx, centy);
 +                      gpuScaleUniform(bgpic->scale);
 +                      gpuRotate2D(RAD2DEGF(-bgpic->rotation));
 +
 +                      if (bgpic->flag & CAM_BGIMG_FLAG_FLIP_X) {
 +                              zoomx *= -1.0f;
 +                              x1 = x2;
 +                      }
 +                      if (bgpic->flag & CAM_BGIMG_FLAG_FLIP_Y) {
 +                              zoomy *= -1.0f;
 +                              y1 = y2;
 +                      }
 +
 +                      float col[4] = {1.0f, 1.0f, 1.0f, bgpic->alpha};
 +                      IMMDrawPixelsTexState state = immDrawPixelsTexSetup(GPU_SHADER_2D_IMAGE_COLOR);
 +                      immDrawPixelsTex(&state, x1 - centx, y1 - centy, ibuf->x, ibuf->y, GL_RGBA, GL_UNSIGNED_BYTE, GL_LINEAR, ibuf->rect,
 +                                       zoomx, zoomy, col);
 +
 +                      gpuPopProjectionMatrix();
 +                      gpuPopMatrix();
 +
 +                      glDisable(GL_BLEND);
 +
 +                      glDepthMask(GL_TRUE);
 +                      if (v3d->zbuf) glEnable(GL_DEPTH_TEST);
 +
 +                      if (freeibuf)
 +                              IMB_freeImBuf(freeibuf);
 +                      if (releaseibuf)
 +                              BKE_image_release_ibuf(ima, releaseibuf, lock);
 +              }
 +      }
 +}
 +
 +void ED_view3d_draw_bgpic_test(
 +        Scene *scene, const Depsgraph *depsgraph,
 +        ARegion *ar, View3D *v3d,
 +        const bool do_foreground, const bool do_camera_frame)
 +{
 +      RegionView3D *rv3d = ar->regiondata;
 +
 +      if ((rv3d->persp == RV3D_CAMOB) && v3d->camera && (v3d->camera->type == OB_CAMERA)) {
 +              Camera *cam = v3d->camera->data;
 +              if ((cam->flag & CAM_SHOW_BG_IMAGE) == 0) {
 +                      return;
 +              }
 +      }
 +      else {
 +              return;
 +      }
 +
 +      /* disabled - mango request, since footage /w only render is quite useful
 +       * and this option is easy to disable all background images at once */
 +#if 0
 +      if (v3d->flag2 & V3D_RENDER_OVERRIDE)
 +              return;
 +#endif
 +
 +      if ((rv3d->view == RV3D_VIEW_USER) || (rv3d->persp != RV3D_ORTHO)) {
 +              if (rv3d->persp == RV3D_CAMOB) {
 +                      view3d_draw_bgpic(scene, depsgraph, ar, v3d, do_foreground, do_camera_frame);
 +              }
 +      }
 +      else {
 +              view3d_draw_bgpic(scene, depsgraph, ar, v3d, do_foreground, do_camera_frame);
 +      }
 +}
 +
 +/* ****************** View3d afterdraw *************** */
 +
 +typedef struct View3DAfter {
 +      struct View3DAfter *next, *prev;
 +      struct Base *base;
 +      short dflag;
 +} View3DAfter;
 +
 +/* temp storage of Objects that need to be drawn as last */
 +void ED_view3d_after_add(ListBase *lb, Base *base, const short dflag)
 +{
 +      View3DAfter *v3da = MEM_callocN(sizeof(View3DAfter), "View 3d after");
 +      BLI_assert((base->flag_legacy & OB_FROMDUPLI) == 0);
 +      BLI_addtail(lb, v3da);
 +      v3da->base = base;
 +      v3da->dflag = dflag;
 +}
 +
 +/* disables write in zbuffer and draws it over */
 +static void view3d_draw_transp(
 +        const EvaluationContext *eval_ctx, Scene *scene, ViewLayer *view_layer, ARegion *ar, View3D *v3d)
 +{
 +      View3DAfter *v3da;
 +      
 +      glDepthMask(GL_FALSE);
 +      v3d->transp = true;
 +      
 +      while ((v3da = BLI_pophead(&v3d->afterdraw_transp))) {
 +              draw_object(eval_ctx, scene, view_layer, ar, v3d, v3da->base, v3da->dflag);
 +              MEM_freeN(v3da);
 +      }
 +      v3d->transp = false;
 +      
 +      glDepthMask(GL_TRUE);
 +      
 +}
 +
 +/* clears zbuffer and draws it over */
 +static void view3d_draw_xray(
 +        const EvaluationContext *eval_ctx, Scene *scene, ViewLayer *view_layer, ARegion *ar, View3D *v3d, bool *clear)
 +{
 +      if (*clear && v3d->zbuf) {
 +              glClear(GL_DEPTH_BUFFER_BIT);
 +              *clear = false;
 +      }
 +
 +      v3d->xray = true;
 +      View3DAfter *v3da;
 +      while ((v3da = BLI_pophead(&v3d->afterdraw_xray))) {
 +              draw_object(eval_ctx, scene, view_layer, ar, v3d, v3da->base, v3da->dflag);
 +              MEM_freeN(v3da);
 +      }
 +      v3d->xray = false;
 +}
 +
 +
 +/* clears zbuffer and draws it over */
 +static void view3d_draw_xraytransp(
 +        const EvaluationContext *eval_ctx, Scene *scene, ViewLayer *view_layer, ARegion *ar, View3D *v3d, const bool clear)
 +{
 +      if (clear && v3d->zbuf)
 +              glClear(GL_DEPTH_BUFFER_BIT);
 +
 +      v3d->xray = true;
 +      v3d->transp = true;
 +      
 +      glDepthMask(GL_FALSE);
 +
 +      View3DAfter *v3da;
 +      while ((v3da = BLI_pophead(&v3d->afterdraw_xraytransp))) {
 +              draw_object(eval_ctx, scene, view_layer, ar, v3d, v3da->base, v3da->dflag);
 +              MEM_freeN(v3da);
 +      }
 +
 +      v3d->transp = false;
 +      v3d->xray = false;
 +
 +      glDepthMask(GL_TRUE);
 +}
 +
 +/* clears zbuffer and draws it over,
 + * note that in the select version we don't care about transparent flag as with regular drawing */
 +static void view3d_draw_xray_select(
 +        const struct EvaluationContext *eval_ctx, Scene *scene, ViewLayer *view_layer, ARegion *ar, View3D *v3d, bool *clear)
 +{
 +      /* Not ideal, but we need to read from the previous depths before clearing
 +       * otherwise we could have a function to load the depths after drawing.
 +       *
 +       * Clearing the depth buffer isn't all that common between drawing objects so accept this for now.
 +       */
 +      if (U.gpu_select_pick_deph) {
 +              GPU_select_load_id(-1);
 +      }
 +
 +      View3DAfter *v3da;
 +      if (*clear && v3d->zbuf) {
 +              glClear(GL_DEPTH_BUFFER_BIT);
 +              *clear = false;
 +      }
 +
 +      v3d->xray = true;
 +      while ((v3da = BLI_pophead(&v3d->afterdraw_xray))) {
 +              if (GPU_select_load_id(v3da->base->object->select_color)) {
 +                      draw_object_select(eval_ctx, scene, view_layer, ar, v3d, v3da->base, v3da->dflag);
 +              }
 +              MEM_freeN(v3da);
 +      }
 +      v3d->xray = false;
 +}
 +
 +/* *********************** */
 +
 +/*
 + * In most cases call draw_dupli_objects,
 + * draw_dupli_objects_color was added because when drawing set dupli's
 + * we need to force the color
 + */
 +
 +#if 0
 +int dupli_ob_sort(void *arg1, void *arg2)
 +{
 +      void *p1 = ((DupliObject *)arg1)->ob;
 +      void *p2 = ((DupliObject *)arg2)->ob;
 +      int val = 0;
 +      if (p1 < p2) val = -1;
 +      else if (p1 > p2) val = 1;
 +      return val;
 +}
 +#endif
 +
 +
 +static DupliObject *dupli_step(DupliObject *dob)
 +{
 +      while (dob && dob->no_draw)
 +              dob = dob->next;
 +      return dob;
 +}
 +
 +static void draw_dupli_objects_color(
 +        const EvaluationContext *eval_ctx, Scene *scene, ViewLayer *view_layer, ARegion *ar, View3D *v3d, Base *base,
 +        const short dflag, const int color)
 +{
 +      RegionView3D *rv3d = ar->regiondata;
 +      ListBase *lb;
 +      LodLevel *savedlod;
 +      Base tbase = {NULL};
 +      BoundBox bb, *bb_tmp; /* use a copy because draw_object, calls clear_mesh_caches */
 +      unsigned char color_rgb[3];
 +      const short dflag_dupli = dflag | DRAW_CONSTCOLOR;
 +      short transflag;
 +      char dt;
 +      short dtx;
 +      DupliApplyData *apply_data;
 +
 +      if ((base->flag & BASE_VISIBLED) == 0) return;
 +      if ((base->object->restrictflag & OB_RESTRICT_RENDER) && (v3d->flag2 & V3D_RENDER_OVERRIDE)) return;
 +
 +      if (dflag & DRAW_CONSTCOLOR) {
 +              BLI_assert(color == TH_UNDEFINED);
 +      }
 +      else {
 +              UI_GetThemeColorBlend3ubv(color, TH_BACK, 0.5f, color_rgb);
 +      }
 +
 +      tbase.flag_legacy = OB_FROMDUPLI | base->flag_legacy;
 +      tbase.flag = base->flag;
 +      lb = object_duplilist(eval_ctx, scene, base->object);
 +      // BLI_listbase_sort(lb, dupli_ob_sort); /* might be nice to have if we have a dupli list with mixed objects. */
 +
 +      apply_data = duplilist_apply(eval_ctx, base->object, scene, lb);
 +
 +      DupliObject *dob_next = NULL;
 +      DupliObject *dob = dupli_step(lb->first);
 +      if (dob) dob_next = dupli_step(dob->next);
 +
 +      for (; dob; dob = dob_next, dob_next = dob_next ? dupli_step(dob_next->next) : NULL) {
 +              bool testbb = false;
 +
 +              tbase.object = dob->ob;
 +
 +              /* Make sure lod is updated from dupli's position */
 +              savedlod = dob->ob->currentlod;
 +
 +#ifdef WITH_GAMEENGINE
 +              if (rv3d->rflag & RV3D_IS_GAME_ENGINE) {
 +                      BKE_object_lod_update(dob->ob, rv3d->viewinv[3]);
 +              }
 +#endif
 +
 +              /* extra service: draw the duplicator in drawtype of parent, minimum taken
 +               * to allow e.g. boundbox box objects in groups for LOD */
 +              dt = tbase.object->dt;
 +              tbase.object->dt = MIN2(tbase.object->dt, base->object->dt);
 +
 +              /* inherit draw extra, but not if a boundbox under the assumption that this
 +               * is intended to speed up drawing, and drawing extra (especially wire) can
 +               * slow it down too much */
 +              dtx = tbase.object->dtx;
 +              if (tbase.object->dt != OB_BOUNDBOX)
 +                      tbase.object->dtx = base->object->dtx;
 +
 +              /* negative scale flag has to propagate */
 +              transflag = tbase.object->transflag;
 +
 +              if (is_negative_m4(dob->mat))
 +                      tbase.object->transflag |= OB_NEG_SCALE;
 +              else
 +                      tbase.object->transflag &= ~OB_NEG_SCALE;
 +              
 +              /* should move outside the loop but possible color is set in draw_object still */
 +              if ((dflag & DRAW_CONSTCOLOR) == 0) {
 +                      glColor3ubv(color_rgb);
 +              }
 +              
 +              if ((bb_tmp = BKE_object_boundbox_get(dob->ob))) {
 +                      bb = *bb_tmp; /* must make a copy  */
 +                      testbb = true;
 +              }
 +
 +              if (!testbb || ED_view3d_boundbox_clip_ex(rv3d, &bb, dob->mat)) {
 +                      copy_m4_m4(dob->ob->obmat, dob->mat);
 +                      GPU_begin_dupli_object(dob);
 +                      draw_object(eval_ctx, scene, view_layer, ar, v3d, &tbase, dflag_dupli);
 +                      GPU_end_dupli_object();
 +              }
 +              
 +              tbase.object->dt = dt;
 +              tbase.object->dtx = dtx;
 +              tbase.object->transflag = transflag;
 +              tbase.object->currentlod = savedlod;
 +      }
 +
 +      if (apply_data) {
 +              duplilist_restore(lb, apply_data);
 +              duplilist_free_apply_data(apply_data);
 +      }
 +
 +      free_object_duplilist(lb);
 +}
 +
 +void draw_dupli_objects(const EvaluationContext *eval_ctx, Scene *scene, ViewLayer *view_layer, ARegion *ar, View3D *v3d, Base *base)
 +{
 +      /* define the color here so draw_dupli_objects_color can be called
 +       * from the set loop */
 +      
 +      int color = (base->flag & BASE_SELECTED) ? TH_SELECT : TH_WIRE;
 +      /* debug */
 +      if (base->object->dup_group && base->object->dup_group->id.us < 1)
 +              color = TH_REDALERT;
 +      
 +      draw_dupli_objects_color(eval_ctx, scene, view_layer, ar, v3d, base, 0, color);
 +}
 +
 +/* XXX warning, not using gpu offscreen here */
 +void view3d_update_depths_rect(ARegion *ar, ViewDepths *d, rcti *rect)
 +{
 +      /* clamp rect by region */
 +      rcti r = {
 +              .xmin = 0,
 +              .xmax = ar->winx - 1,
 +              .ymin = 0,
 +              .ymax = ar->winy - 1
 +      };
 +
 +      /* Constrain rect to depth bounds */
 +      BLI_rcti_isect(&r, rect, rect);
 +
 +      /* assign values to compare with the ViewDepths */
 +      int x = rect->xmin;
 +      int y = rect->ymin;
 +
 +      int w = BLI_rcti_size_x(rect);
 +      int h = BLI_rcti_size_y(rect);
 +
 +      if (w <= 0 || h <= 0) {
 +              if (d->depths)
 +                      MEM_freeN(d->depths);
 +              d->depths = NULL;
 +
 +              d->damaged = false;
 +      }
 +      else if (d->w != w ||
 +               d->h != h ||
 +               d->x != x ||
 +               d->y != y ||
 +               d->depths == NULL
 +               )
 +      {
 +              d->x = x;
 +              d->y = y;
 +              d->w = w;
 +              d->h = h;
 +
 +              if (d->depths)
 +                      MEM_freeN(d->depths);
 +
 +              d->depths = MEM_mallocN(sizeof(float) * d->w * d->h, "View depths Subset");
 +              
 +              d->damaged = true;
 +      }
 +
 +      if (d->damaged) {
 +              /* XXX using special function here, it doesn't use the gpu offscreen system */
 +              view3d_opengl_read_Z_pixels(ar, d->x, d->y, d->w, d->h, GL_DEPTH_COMPONENT, GL_FLOAT, d->depths);
 +              glGetDoublev(GL_DEPTH_RANGE, d->depth_range);
 +              d->damaged = false;
 +      }
 +}
 +
 +/* note, with nouveau drivers the glReadPixels() is very slow. [#24339] */
 +void ED_view3d_depth_update(ARegion *ar)
 +{
 +      RegionView3D *rv3d = ar->regiondata;
 +      
 +      /* Create storage for, and, if necessary, copy depth buffer */
 +      if (!rv3d->depths) rv3d->depths = MEM_callocN(sizeof(ViewDepths), "ViewDepths");
 +      if (rv3d->depths) {
 +              ViewDepths *d = rv3d->depths;
 +              if (d->w != ar->winx ||
 +                  d->h != ar->winy ||
 +                  !d->depths)
 +              {
 +                      d->w = ar->winx;
 +                      d->h = ar->winy;
 +                      if (d->depths)
 +                              MEM_freeN(d->depths);
 +                      d->depths = MEM_mallocN(sizeof(float) * d->w * d->h, "View depths");
 +                      d->damaged = true;
 +              }
 +              
 +              if (d->damaged) {
 +                      view3d_opengl_read_pixels(ar, 0, 0, d->w, d->h, GL_DEPTH_COMPONENT, GL_FLOAT, d->depths);
 +                      glGetDoublev(GL_DEPTH_RANGE, d->depth_range);
 +                      
 +                      d->damaged = false;
 +              }
 +      }
 +}
 +
 +/* utility function to find the closest Z value, use for autodepth */
 +float view3d_depth_near(ViewDepths *d)
 +{
 +      /* convert to float for comparisons */
 +      const float near = (float)d->depth_range[0];
 +      const float far_real = (float)d->depth_range[1];
 +      float far = far_real;
 +
 +      const float *depths = d->depths;
 +      float depth = FLT_MAX;
 +      int i = (int)d->w * (int)d->h; /* cast to avoid short overflow */
 +
 +      /* far is both the starting 'far' value
 +       * and the closest value found. */
 +      while (i--) {
 +              depth = *depths++;
 +              if ((depth < far) && (depth > near)) {
 +                      far = depth;
 +              }
 +      }
 +
 +      return far == far_real ? FLT_MAX : far;
 +}
 +
 +void ED_view3d_draw_depth_gpencil(
 +        const EvaluationContext *eval_ctx, Scene *scene, ARegion *ar, View3D *v3d)
 +{
 +      bool zbuf = v3d->zbuf;
 +
 +      /* Setup view matrix. */
 +      ED_view3d_draw_setup_view(NULL, eval_ctx, scene, ar, v3d, NULL, NULL, NULL);
 +
 +      glClear(GL_DEPTH_BUFFER_BIT);
 +
 +      v3d->zbuf = true;
 +      glEnable(GL_DEPTH_TEST);
 +
 +      if (v3d->flag2 & V3D_SHOW_GPENCIL) {
 +              ED_gpencil_draw_view3d(NULL, scene, eval_ctx->view_layer, eval_ctx->depsgraph, v3d, ar, true);
 +      }
 +
 +      v3d->zbuf = zbuf;
 +      if (!zbuf) glDisable(GL_DEPTH_TEST);
 +}
 +
 +void ED_view3d_draw_depth_loop(const EvaluationContext *eval_ctx, Scene *scene, ARegion *ar, View3D *v3d)
 +{
 +      Base *base;
 +      ViewLayer *view_layer = eval_ctx->view_layer;
 +      /* no need for color when drawing depth buffer */
 +      const short dflag_depth = DRAW_CONSTCOLOR;
 +
 +      /* draw set first */
 +      if (scene->set) {
 +              Scene *sce_iter;
 +              for (SETLOOPER(scene->set, sce_iter, base)) {
 +                      if ((base->flag & BASE_VISIBLED) != 0) {
 +                              draw_object(eval_ctx, scene, view_layer, ar, v3d, base, 0);
 +                              if (base->object->transflag & OB_DUPLI) {
 +                                      draw_dupli_objects_color(eval_ctx, scene, view_layer, ar, v3d, base, dflag_depth, TH_UNDEFINED);
 +                              }
 +                      }
 +              }
 +      }
 +      
 +      for (base = view_layer->object_bases.first; base; base = base->next) {
 +              if ((base->flag & BASE_VISIBLED) != 0) {
 +                      /* dupli drawing */
 +                      if (base->object->transflag & OB_DUPLI) {
 +                              draw_dupli_objects_color(eval_ctx, scene, view_layer, ar, v3d, base, dflag_depth, TH_UNDEFINED);
 +                      }
 +                      draw_object(eval_ctx, scene, view_layer, ar, v3d, base, dflag_depth);
 +              }
 +      }
 +      
 +      /* this isn't that nice, draw xray objects as if they are normal */
 +      if (v3d->afterdraw_transp.first ||
 +          v3d->afterdraw_xray.first ||
 +          v3d->afterdraw_xraytransp.first)
 +      {
 +              View3DAfter *v3da;
 +              int mask_orig;
 +
 +              v3d->xray = true;
 +              
 +              /* transp materials can change the depth mask, see #21388 */
 +              glGetIntegerv(GL_DEPTH_WRITEMASK, &mask_orig);
 +
 +
 +              if (v3d->afterdraw_xray.first || v3d->afterdraw_xraytransp.first) {
 +                      glDepthFunc(GL_ALWAYS); /* always write into the depth bufer, overwriting front z values */
 +                      for (v3da = v3d->afterdraw_xray.first; v3da; v3da = v3da->next) {
 +                              draw_object(eval_ctx, scene, view_layer, ar, v3d, v3da->base, dflag_depth);
 +                      }
 +                      glDepthFunc(GL_LEQUAL); /* Now write the depth buffer normally */
 +              }
 +
 +              /* draw 3 passes, transp/xray/xraytransp */
 +              v3d->xray = false;
 +              v3d->transp = true;
 +              while ((v3da = BLI_pophead(&v3d->afterdraw_transp))) {
 +                      draw_object(eval_ctx, scene, view_layer, ar, v3d, v3da->base, dflag_depth);
 +                      MEM_freeN(v3da);
 +              }
 +
 +              v3d->xray = true;
 +              v3d->transp = false;
 +              while ((v3da = BLI_pophead(&v3d->afterdraw_xray))) {
 +                      draw_object(eval_ctx, scene, view_layer, ar, v3d, v3da->base, dflag_depth);
 +                      MEM_freeN(v3da);
 +              }
 +
 +              v3d->xray = true;
 +              v3d->transp = true;
 +              while ((v3da = BLI_pophead(&v3d->afterdraw_xraytransp))) {
 +                      draw_object(eval_ctx, scene, view_layer, ar, v3d, v3da->base, dflag_depth);
 +                      MEM_freeN(v3da);
 +              }
 +
 +              
 +              v3d->xray = false;
 +              v3d->transp = false;
 +
 +              glDepthMask(mask_orig);
 +      }
 +}
 +
 +void ED_view3d_draw_select_loop(
 +        const struct EvaluationContext *eval_ctx, ViewContext *vc, Scene *scene, ViewLayer *view_layer,
 +        View3D *v3d, ARegion *ar, bool use_obedit_skip, bool use_nearest)
 +{
 +      struct bThemeState theme_state;
 +
 +      short code = 1;
 +      const short dflag = DRAW_PICKING | DRAW_CONSTCOLOR;
 +
 +      /* Tools may request depth outside of regular drawing code. */
 +      UI_Theme_Store(&theme_state);
 +      UI_SetTheme(SPACE_VIEW3D, RGN_TYPE_WINDOW);
 +
 +      if (vc->obedit && vc->obedit->type == OB_MBALL) {
 +              draw_object(eval_ctx, scene, view_layer, ar, v3d, BASACT(view_layer), dflag);
 +      }
 +      else if ((vc->obedit && vc->obedit->type == OB_ARMATURE)) {
 +              /* if not drawing sketch, draw bones */
 +              if (!BDR_drawSketchNames(vc)) {
 +                      draw_object(eval_ctx, scene, view_layer, ar, v3d, BASACT(view_layer), dflag);
 +              }
 +      }
 +      else {
 +              Base *base;
 +
 +              for (base = view_layer->object_bases.first; base; base = base->next) {
 +                      if ((base->flag & BASE_VISIBLED) != 0) {
 +                              if (((base->flag & BASE_SELECTABLED) == 0) ||
 +                                  (use_obedit_skip && (vc->obedit->data == base->object->data)))
 +                              {
 +                                      base->object->select_color = 0;
 +                              }
 +                              else {
 +                                      base->object->select_color = code;
 +
 +                                      if (use_nearest && (base->object->dtx & OB_DRAWXRAY)) {
 +                                              ED_view3d_after_add(&v3d->afterdraw_xray, base, dflag);
 +                                      }
 +                                      else {
 +                                              if (GPU_select_load_id(code)) {
 +                                                      draw_object(eval_ctx, scene, view_layer, ar, v3d, base, dflag);
 +                                              }
 +                                      }
 +                                      code++;
 +                              }
 +                      }
 +              }
 +
 +              if (use_nearest) {
 +                      bool xrayclear = true;
 +                      if (v3d->afterdraw_xray.first) {
 +                              view3d_draw_xray_select(eval_ctx, scene, view_layer, ar, v3d, &xrayclear);
 +                      }
 +              }
 +      }
 +
 +      UI_Theme_Restore(&theme_state);
 +}
 +
 +typedef struct View3DShadow {
 +      struct View3DShadow *next, *prev;
 +      GPULamp *lamp;
 +} View3DShadow;
 +
 +static void gpu_render_lamp_update(Scene *scene, View3D *v3d,
 +                                   Object *ob, Object *par,
 +                                   float obmat[4][4], unsigned int lay,
 +                                   ListBase *shadows)
 +{
 +      GPULamp *lamp = GPU_lamp_from_blender(scene, ob, par);
 +      
 +      if (lamp) {
 +              Lamp *la = (Lamp *)ob->data;
 +
 +              GPU_lamp_update(lamp, lay, (ob->restrictflag & OB_RESTRICT_RENDER), obmat);
 +              GPU_lamp_update_colors(lamp, la->r, la->g, la->b, la->energy);
 +              
 +              unsigned int layers = lay & v3d->lay;
 +
 +              if (layers &&
 +                  GPU_lamp_has_shadow_buffer(lamp) &&
 +                  /* keep last, may do string lookup */
 +                  GPU_lamp_visible(lamp, NULL))
 +              {
 +                      View3DShadow *shadow = MEM_callocN(sizeof(View3DShadow), "View3DShadow");
 +                      shadow->lamp = lamp;
 +                      BLI_addtail(shadows, shadow);
 +              }
 +      }
 +}
 +
 +static void gpu_update_lamps_shadows_world(const EvaluationContext *eval_ctx, Scene *scene, View3D *v3d)
 +{
 +      ListBase shadows;
 +      Scene *sce_iter;
 +      Base *base;
 +      World *world = scene->world;
 +      
 +      BLI_listbase_clear(&shadows);
 +      
 +      /* update lamp transform and gather shadow lamps */
 +      for (SETLOOPER(scene, sce_iter, base)) {
 +              Object *ob = base->object;
 +              
 +              if (ob->type == OB_LAMP)
 +                      gpu_render_lamp_update(scene, v3d, ob, NULL, ob->obmat, ob->lay, &shadows);
 +              
 +              if (ob->transflag & OB_DUPLI) {
 +                      DupliObject *dob;
 +                      ListBase *lb = object_duplilist(G.main->eval_ctx, scene, ob);
 +                      
 +                      for (dob = lb->first; dob; dob = dob->next)
 +                              if (dob->ob->type == OB_LAMP)
 +                                      gpu_render_lamp_update(scene, v3d, dob->ob, ob, dob->mat, ob->lay, &shadows);
 +                      
 +                      free_object_duplilist(lb);
 +              }
 +      }
 +      
 +      /* render shadows after updating all lamps, nested object_duplilist
 +       * don't work correct since it's replacing object matrices */
 +      for (View3DShadow *shadow = shadows.first; shadow; shadow = shadow->next) {
 +              /* this needs to be done better .. */
 +              float viewmat[4][4], winmat[4][4];
 +              ARegion ar = {NULL};
 +              RegionView3D rv3d = {{{0}}};
 +
 +              int drawtype = v3d->drawtype;
 +              int lay = v3d->lay;
 +              int flag2 = v3d->flag2;
 +
 +              v3d->drawtype = OB_SOLID;
 +              v3d->lay &= GPU_lamp_shadow_layer(shadow->lamp);
 +              v3d->flag2 &= ~(V3D_SOLID_TEX | V3D_SHOW_SOLID_MATCAP);
 +              v3d->flag2 |= V3D_RENDER_OVERRIDE | V3D_RENDER_SHADOW;
 +              
 +              int winsize;
 +              GPU_lamp_shadow_buffer_bind(shadow->lamp, viewmat, &winsize, winmat);
 +
 +              ar.regiondata = &rv3d;
 +              ar.regiontype = RGN_TYPE_WINDOW;
 +              rv3d.persp = RV3D_CAMOB;
 +              copy_m4_m4(rv3d.winmat, winmat);
 +              copy_m4_m4(rv3d.viewmat, viewmat);
 +              invert_m4_m4(rv3d.viewinv, rv3d.viewmat);
 +              mul_m4_m4m4(rv3d.persmat, rv3d.winmat, rv3d.viewmat);
 +              invert_m4_m4(rv3d.persinv, rv3d.viewinv);
 +
 +              /* no need to call ED_view3d_draw_offscreen_init since shadow buffers were already updated */
 +              ED_view3d_draw_offscreen(
 +                          eval_ctx, scene, eval_ctx->view_layer, v3d, &ar, winsize, winsize, viewmat, winmat,
 +                          false, false, true,
 +                          NULL, NULL, NULL, NULL, NULL);
 +              GPU_lamp_shadow_buffer_unbind(shadow->lamp);
 +              
 +              v3d->drawtype = drawtype;
 +              v3d->lay = lay;
 +              v3d->flag2 = flag2;
 +      }
 +
 +      BLI_freelistN(&shadows);
 +
 +      /* update world values */
 +      if (world) {
 +              GPU_mist_update_enable(world->mode & WO_MIST);
 +              GPU_mist_update_values(world->mistype, world->miststa, world->mistdist, world->misi, &world->horr);
 +              GPU_horizon_update_color(&world->horr);
 +              GPU_ambient_update_color(&world->ambr);
 +              GPU_zenith_update_color(&world->zenr);
 +      }
 +}
 +
 +/* *********************** customdata **************** */
 +
 +CustomDataMask ED_view3d_datamask(const Scene *scene, const View3D *v3d)
 +{
 +      CustomDataMask mask = 0;
 +      const int drawtype = view3d_effective_drawtype(v3d);
 +
 +      if (ELEM(drawtype, OB_TEXTURE, OB_MATERIAL) ||
 +          ((drawtype == OB_SOLID) && (v3d->flag2 & V3D_SOLID_TEX)))
 +      {
 +              mask |= CD_MASK_MLOOPUV | CD_MASK_MLOOPCOL;
 +
 +              if (BKE_scene_use_new_shading_nodes(scene)) {
 +                      if (drawtype == OB_MATERIAL)
 +                              mask |= CD_MASK_ORCO;
 +              }
 +              else {
 +                      if ((scene->gm.matmode == GAME_MAT_GLSL && drawtype == OB_TEXTURE) || 
 +                          (drawtype == OB_MATERIAL))
 +                      {
 +                              mask |= CD_MASK_ORCO;
 +                      }
 +              }
 +      }
 +
 +      return mask;
 +}
 +
 +/* goes over all modes and view3d settings */
 +CustomDataMask ED_view3d_screen_datamask(const Scene *scene, const bScreen *screen)
 +{
 +      CustomDataMask mask = CD_MASK_BAREMESH;
 +      
 +      /* check if we need tfaces & mcols due to view mode */
 +      for (const ScrArea *sa = screen->areabase.first; sa; sa = sa->next) {
 +              if (sa->spacetype == SPACE_VIEW3D) {
 +                      mask |= ED_view3d_datamask(scene, sa->spacedata.first);
 +              }
 +      }
 +
 +      return mask;
 +}
 +
 +/**
 + * Shared by #ED_view3d_draw_offscreen and #view3d_main_region_draw_objects
 + *
 + * \note \a C and \a grid_unit will be NULL when \a draw_offscreen is set.
 + * \note Drawing lamps and opengl render uses this, so dont do grease pencil or view widgets here.
 + */
 +static void view3d_draw_objects(
 +        const bContext *C,
 +        const EvaluationContext *eval_ctx,
 +        Scene *scene, View3D *v3d, ARegion *ar,
 +        const char **grid_unit,
 +        const bool do_bgpic, const bool draw_offscreen, GPUFX *fx)
 +{
 +      ViewLayer *view_layer = C ? CTX_data_view_layer(C) : BKE_view_layer_from_scene_get(scene);
 +      Depsgraph *depsgraph = CTX_data_depsgraph(C);
 +      RegionView3D *rv3d = ar->regiondata;
 +      Base *base;
 +      Object *obedit = OBEDIT_FROM_EVAL_CTX(eval_ctx);
 +      const bool do_camera_frame = !draw_offscreen;
 +      const bool draw_grids = !draw_offscreen && (v3d->flag2 & V3D_RENDER_OVERRIDE) == 0;
 +      const bool draw_floor = (rv3d->view == RV3D_VIEW_USER) || (rv3d->persp != RV3D_ORTHO);
 +      /* only draw grids after in solid modes, else it hovers over mesh wires */
 +      const bool draw_grids_after = draw_grids && draw_floor && (v3d->drawtype > OB_WIRE) && fx;
 +      bool do_composite_xray = false;
 +      bool xrayclear = true;
 +
 +      if (!draw_offscreen) {
 +              ED_region_draw_cb_draw(C, ar, REGION_DRAW_PRE_VIEW);
 +      }
 +
 +      if (rv3d->rflag & RV3D_CLIPPING)
 +              view3d_draw_clipping(rv3d);
 +
 +      /* set zbuffer after we draw clipping region */
 +      v3d->zbuf = VP_legacy_use_depth(v3d, obedit);
 +
 +      if (v3d->zbuf) {
 +              glEnable(GL_DEPTH_TEST);
 +      }
 +
 +      /* ortho grid goes first, does not write to depth buffer and doesn't need depth test so it will override
 +       * objects if done last */
 +      if (draw_grids) {
 +              /* needs to be done always, gridview is adjusted in drawgrid() now, but only for ortho views. */
 +              rv3d->gridview = ED_view3d_grid_scale(scene, v3d, grid_unit);
 +
 +              if (!draw_floor) {
 +                      ED_region_pixelspace(ar);
 +                      *grid_unit = NULL;  /* drawgrid need this to detect/affect smallest valid unit... */
 +                      VP_legacy_drawgrid(&scene->unit, ar, v3d, grid_unit);
 +                      gpuLoadProjectionMatrix(rv3d->winmat);
 +                      gpuLoadMatrix(rv3d->viewmat);
 +              }
 +              else if (!draw_grids_after) {
 +                      VP_legacy_drawfloor(scene, v3d, grid_unit, true);
 +              }
 +      }
 +
 +      /* important to do before clipping */
 +      if (do_bgpic) {
 +              ED_view3d_draw_bgpic_test(scene, depsgraph, ar, v3d, false, do_camera_frame);
 +      }
 +
 +      if (rv3d->rflag & RV3D_CLIPPING) {
 +              ED_view3d_clipping_set(rv3d);
 +      }
 +
 +      /* draw set first */
 +      if (scene->set) {
 +              const short dflag = DRAW_CONSTCOLOR | DRAW_SCENESET;
 +              Scene *sce_iter;
 +              for (SETLOOPER(scene->set, sce_iter, base)) {
 +                      if ((base->flag & BASE_VISIBLED) != 0) {
 +                              UI_ThemeColorBlend(TH_WIRE, TH_BACK, 0.6f);
 +                              draw_object(eval_ctx, scene, view_layer, ar, v3d, base, dflag);
 +
 +                              if (base->object->transflag & OB_DUPLI) {
 +                                      draw_dupli_objects_color(eval_ctx, scene, view_layer, ar, v3d, base, dflag, TH_UNDEFINED);
 +                              }
 +                      }
 +              }
 +
 +              /* Transp and X-ray afterdraw stuff for sets is done later */
 +      }
 +
 +      if (draw_offscreen) {
 +              for (base = view_layer->object_bases.first; base; base = base->next) {
 +                      if ((base->flag & BASE_VISIBLED) != 0) {
 +                              /* dupli drawing */
 +                              if (base->object->transflag & OB_DUPLI) {
 +                                      draw_dupli_objects(eval_ctx, scene, view_layer, ar, v3d, base);
 +                              }
 +
 +                              draw_object(eval_ctx, scene, view_layer, ar, v3d, base, 0);
 +                      }
 +              }
 +      }
 +      else {
 +              unsigned int lay_used = 0;
 +
 +              /* then draw not selected and the duplis, but skip editmode object */
 +              for (base = view_layer->object_bases.first; base; base = base->next) {
 +                      lay_used |= base->lay;
 +
 +                      if ((base->flag & BASE_VISIBLED) != 0) {
 +
 +                              /* dupli drawing */
 +                              if (base->object->transflag & OB_DUPLI) {
 +                                      draw_dupli_objects(eval_ctx, scene, view_layer, ar, v3d, base);
 +                              }
 +                              if ((base->flag & BASE_SELECTED) == 0) {
 +                                      if (base->object != obedit)
 +                                              draw_object(eval_ctx, scene, view_layer, ar, v3d, base, 0);
 +                              }
 +                      }
 +              }
 +
 +              /* mask out localview */
 +              v3d->lay_used = lay_used & ((1 << 20) - 1);
 +
 +              /* draw selected and editmode */
 +              for (base = view_layer->object_bases.first; base; base = base->next) {
 +                      if ((base->flag & BASE_VISIBLED) != 0) {
 +                              if (base->object == obedit || (base->flag & BASE_SELECTED)) {
 +                                      draw_object(eval_ctx, scene, view_layer, ar, v3d, base, 0);
 +                              }
 +                      }
 +              }
 +      }
 +
 +      /* perspective floor goes last to use scene depth and avoid writing to depth buffer */
 +      if (draw_grids_after) {
 +              VP_legacy_drawfloor(scene, v3d, grid_unit, false);
 +      }
 +
 +      /* must be before xray draw which clears the depth buffer */
 +      if (v3d->flag2 & V3D_SHOW_GPENCIL) {
 +              wmWindowManager *wm = (C != NULL) ? CTX_wm_manager(C) : NULL;
 +              
 +              /* must be before xray draw which clears the depth buffer */
 +              if (v3d->zbuf) glDisable(GL_DEPTH_TEST);
 +              ED_gpencil_draw_view3d(wm, scene, view_layer, depsgraph, v3d, ar, true);
 +              if (v3d->zbuf) glEnable(GL_DEPTH_TEST);
 +      }
 +
 +      /* transp and X-ray afterdraw stuff */
 +      if (v3d->afterdraw_transp.first)     view3d_draw_transp(eval_ctx, scene, view_layer, ar, v3d);
 +
 +      /* always do that here to cleanup depth buffers if none needed */
 +      if (fx) {
 +              do_composite_xray = v3d->zbuf && (v3d->afterdraw_xray.first || v3d->afterdraw_xraytransp.first);
 +              GPU_fx_compositor_setup_XRay_pass(fx, do_composite_xray);
 +      }
 +
 +      if (v3d->afterdraw_xray.first)       view3d_draw_xray(eval_ctx, scene, view_layer, ar, v3d, &xrayclear);
 +      if (v3d->afterdraw_xraytransp.first) view3d_draw_xraytransp(eval_ctx, scene, view_layer, ar, v3d, xrayclear);
 +
 +      if (fx && do_composite_xray) {
 +              GPU_fx_compositor_XRay_resolve(fx);
 +      }
 +
 +      if (!draw_offscreen) {
 +              ED_region_draw_cb_draw(C, ar, REGION_DRAW_POST_VIEW);
 +      }
 +
 +      if (rv3d->rflag & RV3D_CLIPPING)
 +              ED_view3d_clipping_disable();
 +
 +      /* important to do after clipping */
 +      if (do_bgpic) {
 +              ED_view3d_draw_bgpic_test(scene, depsgraph, ar, v3d, true, do_camera_frame);
 +      }
 +
 +      /* cleanup */
 +      if (v3d->zbuf) {
 +              v3d->zbuf = false;
 +              glDisable(GL_DEPTH_TEST);
 +      }
 +
 +      if ((v3d->flag2 & V3D_RENDER_SHADOW) == 0) {
 +              GPU_free_images_old();
 +      }
 +}
 +
 +/**
 + * Store values from #RegionView3D, set when drawing.
 + * This is needed when we draw with to a viewport using a different matrix (offscreen drawing for example).
 + *
 + * Values set by #ED_view3d_update_viewmat should be handled here.
 + */
 +struct RV3DMatrixStore {
 +      float winmat[4][4];
 +      float viewmat[4][4];
 +      float viewinv[4][4];
 +      float persmat[4][4];
 +      float persinv[4][4];
 +      float viewcamtexcofac[4];
 +      float pixsize;
 +};
 +
 +struct RV3DMatrixStore *ED_view3d_mats_rv3d_backup(struct RegionView3D *rv3d)
 +{
 +      struct RV3DMatrixStore *rv3dmat = MEM_mallocN(sizeof(*rv3dmat), __func__);
 +      copy_m4_m4(rv3dmat->winmat, rv3d->winmat);
 +      copy_m4_m4(rv3dmat->viewmat, rv3d->viewmat);
 +      copy_m4_m4(rv3dmat->persmat, rv3d->persmat);
 +      copy_m4_m4(rv3dmat->persinv, rv3d->persinv);
 +      copy_m4_m4(rv3dmat->viewinv, rv3d->viewinv);
 +      copy_v4_v4(rv3dmat->viewcamtexcofac, rv3d->viewcamtexcofac);
 +      rv3dmat->pixsize = rv3d->pixsize;
 +      return (void *)rv3dmat;
 +}
 +
 +void ED_view3d_mats_rv3d_restore(struct RegionView3D *rv3d, struct RV3DMatrixStore *rv3dmat_pt)
 +{
 +      struct RV3DMatrixStore *rv3dmat = rv3dmat_pt;
 +      copy_m4_m4(rv3d->winmat, rv3dmat->winmat);
 +      copy_m4_m4(rv3d->viewmat, rv3dmat->viewmat);
 +      copy_m4_m4(rv3d->persmat, rv3dmat->persmat);
 +      copy_m4_m4(rv3d->persinv, rv3dmat->persinv);
 +      copy_m4_m4(rv3d->viewinv, rv3dmat->viewinv);
 +      copy_v4_v4(rv3d->viewcamtexcofac, rv3dmat->viewcamtexcofac);
 +      rv3d->pixsize = rv3dmat->pixsize;
 +}
 +
 +/**
 + * \note The info that this uses is updated in #ED_refresh_viewport_fps,
 + * which currently gets called during #SCREEN_OT_animation_step.
 + */
 +void ED_scene_draw_fps(Scene *scene, const rcti *rect)
 +{
 +      ScreenFrameRateInfo *fpsi = scene->fps_info;
 +      char printable[16];
 +      
 +      if (!fpsi || !fpsi->lredrawtime || !fpsi->redrawtime)
 +              return;
 +      
 +      printable[0] = '\0';
 +      
 +#if 0
 +      /* this is too simple, better do an average */
 +      fps = (float)(1.0 / (fpsi->lredrawtime - fpsi->redrawtime))
 +#else
 +      fpsi->redrawtimes_fps[fpsi->redrawtime_index] = (float)(1.0 / (fpsi->lredrawtime - fpsi->redrawtime));
 +      
 +      float fps = 0.0f;
 +      int tot = 0;
 +      for (int i = 0; i < REDRAW_FRAME_AVERAGE; i++) {
 +              if (fpsi->redrawtimes_fps[i]) {
 +                      fps += fpsi->redrawtimes_fps[i];
 +                      tot++;
 +              }
 +      }
 +      if (tot) {
 +              fpsi->redrawtime_index = (fpsi->redrawtime_index + 1) % REDRAW_FRAME_AVERAGE;
 +              
 +              //fpsi->redrawtime_index++;
 +              //if (fpsi->redrawtime >= REDRAW_FRAME_AVERAGE)
 +              //      fpsi->redrawtime = 0;
 +              
 +              fps = fps / tot;
 +      }
 +#endif
 +
 +      const int font_id = BLF_default();
 +
 +      /* is this more than half a frame behind? */
 +      if (fps + 0.5f < (float)(FPS)) {
 +              UI_FontThemeColor(font_id, TH_REDALERT);
 +              BLI_snprintf(printable, sizeof(printable), IFACE_("fps: %.2f"), fps);
 +      }
 +      else {
 +              UI_FontThemeColor(font_id, TH_TEXT_HI);
 +              BLI_snprintf(printable, sizeof(printable), IFACE_("fps: %i"), (int)(fps + 0.5f));
 +      }
 +
 +#ifdef WITH_INTERNATIONAL
 +      BLF_draw_default(rect->xmin + U.widget_unit,  rect->ymax - U.widget_unit, 0.0f, printable, sizeof(printable));
 +#else
 +      BLF_draw_default_ascii(rect->xmin + U.widget_unit,  rect->ymax - U.widget_unit, 0.0f, printable, sizeof(printable));
 +#endif
 +}
 +
 +static bool view3d_main_region_do_render_draw(const Scene *scene)
 +{
 +      RenderEngineType *type = RE_engines_find(scene->view_render.engine_id);
 +      return (type && type->view_update && type->render_to_view);
 +}
 +
 +bool ED_view3d_calc_render_border(const Scene *scene, const Depsgraph *depsgraph, View3D *v3d, ARegion *ar, rcti *rect)
 +{
 +      RegionView3D *rv3d = ar->regiondata;
 +      bool use_border;
 +
 +      /* test if there is a 3d view rendering */
 +      if (v3d->drawtype != OB_RENDER || !view3d_main_region_do_render_draw(scene))
 +              return false;
 +
 +      /* test if there is a border render */
 +      if (rv3d->persp == RV3D_CAMOB)
 +              use_border = (scene->r.mode & R_BORDER) != 0;
 +      else
 +              use_border = (v3d->flag2 & V3D_RENDER_BORDER) != 0;
 +      
 +      if (!use_border)
 +              return false;
 +
 +      /* compute border */
 +      if (rv3d->persp == RV3D_CAMOB) {
 +              rctf viewborder;
 +              ED_view3d_calc_camera_border(scene, depsgraph, ar, v3d, rv3d, &viewborder, false);
 +
 +              rect->xmin = viewborder.xmin + scene->r.border.xmin * BLI_rctf_size_x(&viewborder);
 +              rect->ymin = viewborder.ymin + scene->r.border.ymin * BLI_rctf_size_y(&viewborder);
 +              rect->xmax = viewborder.xmin + scene->r.border.xmax * BLI_rctf_size_x(&viewborder);
 +              rect->ymax = viewborder.ymin + scene->r.border.ymax * BLI_rctf_size_y(&viewborder);
 +      }
 +      else {
 +              rect->xmin = v3d->render_border.xmin * ar->winx;
 +              rect->xmax = v3d->render_border.xmax * ar->winx;
 +              rect->ymin = v3d->render_border.ymin * ar->winy;
 +              rect->ymax = v3d->render_border.ymax * ar->winy;
 +      }
 +
 +      BLI_rcti_translate(rect, ar->winrct.xmin, ar->winrct.ymin);
 +      BLI_rcti_isect(&ar->winrct, rect, rect);
 +
 +      return true;
 +}
 +
 +/**
 +  * IMPORTANT: this is deprecated, any changes made in this function should
 +  * be mirrored in view3d_draw_render_draw() in view3d_draw.c
 +  */
 +static bool view3d_main_region_draw_engine(
 +        const bContext *C, const EvaluationContext *eval_ctx, Scene *scene,
 +        ARegion *ar, View3D *v3d,
 +        bool clip_border, const rcti *border_rect)
 +{
 +      const Depsgraph *depsgraph = CTX_data_depsgraph(C);
 +      RegionView3D *rv3d = ar->regiondata;
 +      RenderEngineType *type;
 +      GLint scissor[4];
 +
 +      /* create render engine */
 +      if (!rv3d->render_engine) {
 +              RenderEngine *engine;
 +              type = RE_engines_find(scene->view_render.engine_id);
 +
 +              if (!(type->view_update && type->render_to_view))
 +                      return false;
 +
 +              engine = RE_engine_create_ex(type, true);
 +
 +              engine->tile_x = scene->r.tilex;
 +              engine->tile_y = scene->r.tiley;
 +
 +              type->view_update(engine, C);
 +
 +              rv3d->render_engine = engine;
 +      }
 +
 +      /* setup view matrices */
 +      VP_legacy_view3d_main_region_setup_view(eval_ctx, scene, v3d, ar, NULL, NULL);
 +
 +      /* background draw */
 +      ED_region_pixelspace(ar);
 +
 +      if (clip_border) {
 +              /* for border draw, we only need to clear a subset of the 3d view */
 +              if (border_rect->xmax > border_rect->xmin && border_rect->ymax > border_rect->ymin) {
 +                      glGetIntegerv(GL_SCISSOR_BOX, scissor);
 +                      glScissor(border_rect->xmin, border_rect->ymin,
 +                                BLI_rcti_size_x(border_rect), BLI_rcti_size_y(border_rect));
 +              }
 +              else {
 +                      return false;
 +              }
 +      }
 +
 +      glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
 +      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
 +
 +      bool show_image = false;
 +      {
 +              Camera *cam = ED_view3d_camera_data_get(v3d, rv3d);
 +              if (cam->flag & CAM_SHOW_BG_IMAGE) {
 +                      show_image = true;
 +                      ED_view3d_draw_bgpic_test(scene, depsgraph, ar, v3d, false, true);
 +              }
 +              else {
 +                      imm_draw_box_checker_2d(0, 0, ar->winx, ar->winy);
 +              }
 +      }
 +
 +      if (show_image) {
 +              ED_view3d_draw_bgpic_test(scene, depsgraph, ar, v3d, false, true);
 +      }
 +      else {
 +              imm_draw_box_checker_2d(0, 0, ar->winx, ar->winy);
 +      }
 +
 +      /* render result draw */
 +      type = rv3d->render_engine->type;
 +      type->render_to_view(rv3d->render_engine, C);
 +
 +      if (show_image) {
 +              ED_view3d_draw_bgpic_test(scene, depsgraph, ar, v3d, true, true);
 +      }
 +
 +      if (clip_border) {
 +              /* restore scissor as it was before */
 +              glScissor(scissor[0], scissor[1], scissor[2], scissor[3]);
 +      }
 +
 +      return true;
 +}
 +
 +static void view3d_main_region_draw_engine_info(View3D *v3d, RegionView3D *rv3d, ARegion *ar, bool render_border)
 +{
 +      float fill_color[4] = {0.0f, 0.0f, 0.0f, 0.25f};
 +
 +      if (!rv3d->render_engine || !rv3d->render_engine->text[0])
 +              return;
 +      
 +      if (render_border) {
 +              /* draw darkened background color. no alpha because border render does
 +               * partial redraw and will not redraw the region behind this info bar */
 +              float alpha = 1.0f - fill_color[3];
 +              Camera *camera = ED_view3d_camera_data_get(v3d, rv3d);
 +
 +              if (camera) {
 +                      if (camera->flag & CAM_SHOWPASSEPARTOUT) {
 +                              alpha *= (1.0f - camera->passepartalpha);
 +                      }
 +              }
 +
 +              UI_GetThemeColor3fv(TH_HIGH_GRAD, fill_color);
 +              mul_v3_fl(fill_color, alpha);
 +              fill_color[3] = 1.0f;
 +      }
 +
 +      ED_region_info_draw(ar, rv3d->render_engine->text, fill_color, true);
 +}
 +
 +#ifdef WITH_GAMEENGINE
 +static void update_lods(Scene *scene, float camera_pos[3])
 +{
 +      Scene *sce_iter;
 +      Base *base;
 +
 +      for (SETLOOPER(scene, sce_iter, base)) {
 +              Object *ob = base->object;
 +              BKE_object_lod_update(ob, camera_pos);
 +      }
 +}
 +#endif
 +
 +static void view3d_main_region_draw_objects(
 +        const bContext *C, Scene *scene, ViewLayer *view_layer, View3D *v3d,
 +        ARegion *ar, const char **grid_unit)
 +{
 +      wmWindow *win = CTX_wm_window(C);
 +      EvaluationContext eval_ctx;
 +      RegionView3D *rv3d = ar->regiondata;
 +      unsigned int lay_used = v3d->lay_used;
 +      
 +      CTX_data_eval_ctx(C, &eval_ctx);
 +
 +      /* post processing */
 +      bool do_compositing = false;
 +      
 +      /* shadow buffers, before we setup matrices */
 +      if (draw_glsl_material(&eval_ctx, scene, view_layer, NULL, v3d, v3d->drawtype))
 +              gpu_update_lamps_shadows_world(&eval_ctx, scene, v3d);
 +
 +      /* reset default OpenGL lights if needed (i.e. after preferences have been altered) */
 +      if (rv3d->rflag & RV3D_GPULIGHT_UPDATE) {
 +              rv3d->rflag &= ~RV3D_GPULIGHT_UPDATE;
 +              GPU_default_lights();
 +      }
 +
 +      /* setup the view matrix */
 +      if (VP_legacy_view3d_stereo3d_active(win, scene, v3d, rv3d)) {
 +              VP_legacy_view3d_stereo3d_setup(&eval_ctx, scene, v3d, ar);
 +      }
 +      else {
 +              VP_legacy_view3d_main_region_setup_view(&eval_ctx, scene, v3d, ar, NULL, NULL);
 +      }
 +
 +      rv3d->rflag &= ~RV3D_IS_GAME_ENGINE;
 +#ifdef WITH_GAMEENGINE
 +      if (STREQ(scene->view_render.engine_id, RE_engine_id_BLENDER_GAME)) {
 +              rv3d->rflag |= RV3D_IS_GAME_ENGINE;
 +
 +              /* Make sure LoDs are up to date */
 +              update_lods(scene, rv3d->viewinv[3]);
 +      }
 +#endif
 +
 +      /* framebuffer fx needed, we need to draw offscreen first */
 +      if (v3d->fx_settings.fx_flag && v3d->drawtype >= OB_SOLID) {
 +              BKE_screen_gpu_fx_validate(&v3d->fx_settings);
 +              GPUFXSettings fx_settings = v3d->fx_settings;
 +              if (!rv3d->compositor)
 +                      rv3d->compositor = GPU_fx_compositor_create();
 +              
 +              if (rv3d->persp == RV3D_CAMOB && v3d->camera)
 +                      BKE_camera_to_gpu_dof(v3d->camera, &fx_settings);
 +              else {
 +                      fx_settings.dof = NULL;
 +              }
 +
 +              do_compositing = GPU_fx_compositor_initialize_passes(rv3d->compositor, &ar->winrct, &ar->drawrct, &fx_settings);
 +      }
 +      
 +      /* main drawing call */
 +      view3d_draw_objects(C, &eval_ctx, scene, v3d, ar, grid_unit, true, false, do_compositing ? rv3d->compositor : NULL);
 +
 +      /* post process */
 +      if (do_compositing) {
 +              GPU_fx_do_composite_pass(rv3d->compositor, rv3d->winmat, rv3d->is_persp, scene, NULL);
 +      }
 +
 +      if (v3d->lay_used != lay_used) { /* happens when loading old files or loading with UI load */
 +              /* find header and force tag redraw */
 +              ScrArea *sa = CTX_wm_area(C);
 +              ARegion *ar_header = BKE_area_find_region_type(sa, RGN_TYPE_HEADER);
 +              ED_region_tag_redraw(ar_header); /* can be NULL */
 +      }
 +
 +      if ((v3d->flag2 & V3D_RENDER_OVERRIDE) == 0) {
 +              BDR_drawSketch(C);
 +      }
 +}
 +
 +static void view3d_main_region_draw_info(
 +        const bContext *C, Scene *scene,
 +        ARegion *ar, View3D *v3d,
 +        const char *grid_unit, bool render_border)
 +{
 +      EvaluationContext eval_ctx;
 +      CTX_data_eval_ctx(C, &eval_ctx);
 +      const Depsgraph *depsgraph = CTX_data_depsgraph(C);
 +      ViewLayer *view_layer = CTX_data_view_layer(C);
 +      wmWindowManager *wm = CTX_wm_manager(C);
 +      RegionView3D *rv3d = ar->regiondata;
 +      rcti rect;
 +      
 +      /* local coordinate visible rect inside region, to accomodate overlapping ui */
 +      ED_region_visible_rect(ar, &rect);
 +
 +      if (rv3d->persp == RV3D_CAMOB) {
 +              VP_drawviewborder(scene, CTX_data_depsgraph(C), ar, v3d);
 +      }
 +      else if (v3d->flag2 & V3D_RENDER_BORDER) {
 +              VP_drawrenderborder(ar, v3d);
 +      }
 +
 +      if (v3d->flag2 & V3D_SHOW_GPENCIL) {
 +              /* draw grease-pencil stuff - needed to get paint-buffer shown too (since it's 2D) */
 +              ED_gpencil_draw_view3d(wm, scene, view_layer, depsgraph, v3d, ar, false);
 +      }
 +
 +      if ((v3d->flag2 & V3D_RENDER_OVERRIDE) == 0) {
 +              VP_legacy_drawcursor(&eval_ctx, scene, view_layer, ar, v3d); /* 3D cursor */
 +
 +              if (U.uiflag & USER_SHOW_ROTVIEWICON)
 +                      VP_legacy_draw_view_axis(rv3d, &rect);
 +              else
 +                      draw_view_icon(rv3d, &rect);
 +
 +              if (U.uiflag & USER_DRAWVIEWINFO) {
 +                      Object *ob = OBACT(view_layer);
 +                      VP_legacy_draw_selected_name(scene, ob, eval_ctx.object_mode, &rect);
 +              }
 +      }
 +
 +      if (rv3d->render_engine) {
 +              view3d_main_region_draw_engine_info(v3d, rv3d, ar, render_border);
 +              return;
 +      }
 +
 +      if ((v3d->flag2 & V3D_RENDER_OVERRIDE) == 0) {
 +              if ((U.uiflag & USER_SHOW_FPS) && ED_screen_animation_no_scrub(wm)) {
 +                      ED_scene_draw_fps(scene, &rect);
 +              }
 +              else if (U.uiflag & USER_SHOW_VIEWPORTNAME) {
 +                      VP_legacy_draw_viewport_name(ar, v3d, &rect);
 +              }
 +
 +              if (grid_unit) { /* draw below the viewport name */
 +                      char numstr[32] = "";
 +
 +                      UI_FontThemeColor(BLF_default(), TH_TEXT_HI);
 +                      if (v3d->grid != 1.0f) {
 +                              BLI_snprintf(numstr, sizeof(numstr), "%s x %.4g", grid_unit, v3d->grid);
 +                      }
 +
 +                      BLF_draw_default_ascii(rect.xmin + U.widget_unit,
 +                                             rect.ymax - (USER_SHOW_VIEWPORTNAME ? 2 * U.widget_unit : U.widget_unit), 0.0f,
 +                                             numstr[0] ? numstr : grid_unit, sizeof(numstr));
 +              }
 +      }
 +}
 +
 +void view3d_main_region_draw_legacy(const bContext *C, ARegion *ar)
 +{
 +      const Depsgraph *depsgraph = CTX_data_depsgraph(C);
 +      EvaluationContext eval_ctx;
 +      Scene *scene = CTX_data_scene(C);
 +      ViewLayer *view_layer = CTX_data_view_layer(C);
 +      View3D *v3d = CTX_wm_view3d(C);
 +      const char *grid_unit = NULL;
 +      rcti border_rect;
 +
 +      /* if we only redraw render border area, skip opengl draw and also
 +       * don't do scissor because it's already set */
 +      bool render_border = ED_view3d_calc_render_border(scene, depsgraph, v3d, ar, &border_rect);
 +      bool clip_border = (render_border && !BLI_rcti_compare(&ar->drawrct, &border_rect));
 +
 +      gpuPushProjectionMatrix();
 +      gpuLoadIdentityProjectionMatrix();
 +      gpuPushMatrix();
 +      gpuLoadIdentity();
 +
 +      CTX_data_eval_ctx(C, &eval_ctx);
 +
 +      /* draw viewport using opengl */
 +      if (v3d->drawtype != OB_RENDER || !view3d_main_region_do_render_draw(scene) || clip_border) {
 +              VP_view3d_main_region_clear(scene, v3d, ar); /* background */
 +              view3d_main_region_draw_objects(C, scene, view_layer, v3d, ar, &grid_unit);
 +
 +              if (G.debug & G_DEBUG_SIMDATA)
 +                      draw_sim_debug_data(scene, v3d, ar);
 +
 +              glDisable(GL_DEPTH_TEST);
 +              ED_region_pixelspace(ar);
 +      }
 +
 +      /* draw viewport using external renderer */
 +      if (v3d->drawtype == OB_RENDER) {
 +              view3d_main_region_draw_engine(C, &eval_ctx, scene, ar, v3d, clip_border, &border_rect);
 +      }
 +
 +      VP_legacy_view3d_main_region_setup_view(&eval_ctx, scene, v3d, ar, NULL, NULL);
 +      glClear(GL_DEPTH_BUFFER_BIT);
 +
 +      WM_manipulatormap_draw(ar->manipulator_map, C, WM_MANIPULATORMAP_DRAWSTEP_3D);
 +
 +      ED_region_pixelspace(ar);
 +
 +      view3d_main_region_draw_info(C, scene, ar, v3d, grid_unit, render_border);
 +
 +      WM_manipulatormap_draw(ar->manipulator_map, C, WM_MANIPULATORMAP_DRAWSTEP_2D);
 +
 +      gpuPopProjectionMatrix();
 +      gpuPopMatrix();
 +
 +      v3d->flag |= V3D_INVALID_BACKBUF;
 +
 +      BLI_assert(BLI_listbase_is_empty(&v3d->afterdraw_transp));
 +      BLI_assert(BLI_listbase_is_empty(&v3d->afterdraw_xray));
 +      BLI_assert(BLI_listbase_is_empty(&v3d->afterdraw_xraytransp));
 +}
 +
 +
 +/* -------------------------------------------------------------------- */
 +
 +/** \name Deprecated Interface
 + *
 + * New viewport sometimes has a check for new/old viewport code.
 + * Use these functions so new viewport can *optionally* call.
 + *
 + * \{ */
 +
 +
 +void VP_deprecated_view3d_draw_objects(
 +        const bContext *C,
 +        const EvaluationContext *eval_ctx,
 +        Scene *scene, View3D *v3d, ARegion *ar,
 +        const char **grid_unit,
 +        const bool do_bgpic, const bool draw_offscreen, GPUFX *fx)
 +{
 +      view3d_draw_objects(C, eval_ctx, scene, v3d, ar, grid_unit, do_bgpic, draw_offscreen, fx);
 +}
 +
 +void VP_deprecated_gpu_update_lamps_shadows_world(const EvaluationContext *eval_ctx, Scene *scene, View3D *v3d)
 +{
 +      gpu_update_lamps_shadows_world(eval_ctx, scene, v3d);
 +}
 +
 +/** \} */
index 0400fc1025bb4347776ecc0d8594a6e568dde4a0,0000000000000000000000000000000000000000..332102aca468cb6831adc720ad68142f15874c10
mode 100644,000000..100644
--- /dev/null
@@@ -1,281 -1,0 +1,281 @@@
- #include "BLI_polyfill2d.h"
 +/*
 + * ***** BEGIN GPL LICENSE BLOCK *****
 + *
 + * This program is free software; you can redistribute it and/or
 + * modify it under the terms of the GNU General Public License
 + * as published by the Free Software Foundation; either version 2
 + * of the License, or (at your option) any later version.
 + *
 + * This program is distributed in the hope that it will be useful,
 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 + * GNU General Public License for more details.
 + *
 + * You should have received a copy of the GNU General Public License
 + * along with this program; if not, write to the Free Software Foundation,
 + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 + *
 + * The Original Code is Copyright (C) 2016 Blender Foundation.
 + * All rights reserved.
 + *
 + * The Original Code is: all of this file.
 + *
 + * Contributor(s): Mike Erwin
 + *
 + * ***** END GPL LICENSE BLOCK *****
 + */
 +
 +/** \file blender/gpu/intern/gpu_basic_shader.c
 + *  \ingroup gpu
 + */
 +
 +#include "MEM_guardedalloc.h"
 +
 +#include "BLI_utildefines.h"
 +#include "BLI_rect.h"
 +#include "BLI_math.h"
++#include "BLI_polyfill_2d.h"
 +#include "BLI_sort_utils.h"
 +
 +
 +#include "GPU_batch.h"  /* own include */
 +#include "gpu_shader_private.h"
 +
 +/* -------------------------------------------------------------------- */
 +/** \name Utilities
 + * \{ */
 +
 +void GWN_batch_program_set_builtin(Gwn_Batch *batch, GPUBuiltinShader shader_id)
 +{
 +      GPUShader *shader = GPU_shader_get_builtin_shader(shader_id);
 +      GWN_batch_program_set(batch, shader->program, shader->interface);
 +}
 +
 +/** \} */
 +
 +
 +
 +/* -------------------------------------------------------------------- */
 +/** \name Batch Creation
 + * \{ */
 +
 +/**
 + * Creates triangles from a byte-array of polygons.
 + *
 + * See 'make_shape_2d_from_blend.py' utility to create data to pass to this function.
 + *
 + * \param polys_flat: Pairs of X, Y coordinates (repeating to signify closing the polygon).
 + * \param polys_flat_len: Length of the array (must be an even number).
 + * \param rect: Optional region to map the byte 0..255 coords to. When not set use -1..1.
 + */
 +Gwn_Batch *GPU_batch_tris_from_poly_2d_encoded(
 +        const uchar *polys_flat, uint polys_flat_len, const rctf *rect)
 +{
 +      const uchar (*polys)[2] = (const void *)polys_flat;
 +      const uint polys_len = polys_flat_len / 2;
 +      BLI_assert(polys_flat_len == polys_len * 2);
 +
 +      /* Over alloc in both cases */
 +      float (*verts)[2] = MEM_mallocN(sizeof(*verts) * polys_len, __func__);
 +      float (*verts_step)[2] = verts;
 +      uint (*tris)[3] = MEM_mallocN(sizeof(*tris) * polys_len, __func__);
 +      uint (*tris_step)[3] = tris;
 +
 +      const float range_uchar[2] = {
 +              (rect ? (rect->xmax - rect->xmin) : 2.0f) / 255.0f,
 +              (rect ? (rect->ymax - rect->ymin) : 2.0f) / 255.0f,
 +      };
 +      const float min_uchar[2] = {
 +              (rect ? rect->xmin : -1.0f),
 +              (rect ? rect->ymin : -1.0f),
 +      };
 +
 +      uint i_poly = 0;
 +      uint i_vert = 0;
 +      while (i_poly != polys_len) {
 +              for (uint j = 0; j < 2; j++) {
 +                      verts[i_vert][j] = min_uchar[j] + ((float)polys[i_poly][j] * range_uchar[j]);
 +              }
 +              i_vert++;
 +              i_poly++;
 +              if (polys[i_poly - 1][0] == polys[i_poly][0] &&
 +                  polys[i_poly - 1][1] == polys[i_poly][1])
 +              {
 +                      const uint verts_step_len = (&verts[i_vert]) - verts_step;
 +                      BLI_assert(verts_step_len >= 3);
 +                      const uint tris_len = (verts_step_len - 2);
 +                      BLI_polyfill_calc(verts_step, verts_step_len, -1, tris_step);
 +                      /* offset indices */
 +                      if (verts_step != verts) {
 +                              uint *t = tris_step[0];
 +                              const uint offset = (verts_step - verts);
 +                              uint tot = tris_len * 3;
 +                              while (tot--) {
 +                                      *t += offset;
 +                                      t++;
 +                              }
 +                              BLI_assert(t == tris_step[tris_len]);
 +                      }
 +                      verts_step += verts_step_len;
 +                      tris_step += tris_len;
 +                      i_poly++;
 +                      /* ignore the duplicate point */
 +              }
 +      }
 +
 +      /* We have vertices and tris, make a batch from this. */
 +      static Gwn_VertFormat format = {0};
 +      static struct { uint pos; } attr_id;
 +      if (format.attrib_ct == 0) {
 +              attr_id.pos = GWN_vertformat_attr_add(&format, "pos", GWN_COMP_F32, 2, GWN_FETCH_FLOAT);
 +      }
 +
 +      const uint verts_len = (verts_step - verts);
 +      const uint tris_len = (tris_step - tris);
 +      Gwn_VertBuf *vbo = GWN_vertbuf_create_with_format(&format);
 +      GWN_vertbuf_data_alloc(vbo, verts_len);
 +
 +      Gwn_VertBufRaw pos_step;
 +      GWN_vertbuf_attr_get_raw_data(vbo, attr_id.pos, &pos_step);
 +
 +      for (uint i = 0; i < verts_len; i++) {
 +              copy_v2_v2(GWN_vertbuf_raw_step(&pos_step), verts[i]);
 +      }
 +
 +      Gwn_IndexBufBuilder elb;
 +      GWN_indexbuf_init(&elb, GWN_PRIM_TRIS, tris_len, verts_len);
 +      for (uint i = 0; i < tris_len; i++) {
 +              GWN_indexbuf_add_tri_verts(&elb, UNPACK3(tris[i]));
 +      }
 +      Gwn_IndexBuf *indexbuf = GWN_indexbuf_build(&elb);
 +
 +      MEM_freeN(tris);
 +      MEM_freeN(verts);
 +
 +      return GWN_batch_create_ex(
 +              GWN_PRIM_TRIS, vbo,
 +              indexbuf,
 +              GWN_BATCH_OWNS_VBO | GWN_BATCH_OWNS_INDEX);
 +}
 +
 +Gwn_Batch *GPU_batch_wire_from_poly_2d_encoded(
 +        const uchar *polys_flat, uint polys_flat_len, const rctf *rect)
 +{
 +      const uchar (*polys)[2] = (const void *)polys_flat;
 +      const uint polys_len = polys_flat_len / 2;
 +      BLI_assert(polys_flat_len == polys_len * 2);
 +
 +      /* Over alloc */
 +      /* Lines are pairs of (x, y) byte locations packed into an int32_t. */
 +      int32_t *lines = MEM_mallocN(sizeof(*lines) * polys_len, __func__);
 +      int32_t *lines_step = lines;
 +
 +      const float range_uchar[2] = {
 +              (rect ? (rect->xmax - rect->xmin) : 2.0f) / 255.0f,
 +              (rect ? (rect->ymax - rect->ymin) : 2.0f) / 255.0f,
 +      };
 +      const float min_uchar[2] = {
 +              (rect ? rect->xmin : -1.0f),
 +              (rect ? rect->ymin : -1.0f),
 +      };
 +
 +      uint i_poly_prev = 0;
 +      uint i_poly = 0;
 +      while (i_poly != polys_len) {
 +              i_poly++;
 +              if (polys[i_poly - 1][0] == polys[i_poly][0] &&
 +                  polys[i_poly - 1][1] == polys[i_poly][1])
 +              {
 +                      const uchar (*polys_step)[2] = polys + i_poly_prev;
 +                      const uint polys_step_len = i_poly - i_poly_prev;
 +                      BLI_assert(polys_step_len >= 2);
 +                      for (uint i_prev = polys_step_len - 1, i = 0; i < polys_step_len; i_prev = i++) {
 +                              union {
 +                                      uint8_t  as_u8[4];
 +                                      uint16_t as_u16[2];
 +                                      uint32_t as_u32;
 +                              } data;
 +                              data.as_u16[0] = *((const uint16_t *)polys_step[i_prev]);
 +                              data.as_u16[1] = *((const uint16_t *)polys_step[i]);
 +                              if (data.as_u16[0] > data.as_u16[1]) {
 +                                      SWAP(uint16_t, data.as_u16[0], data.as_u16[1]);
 +                              }
 +                              *lines_step = data.as_u32;
 +                              lines_step++;
 +                      }
 +                      i_poly++;
 +                      i_poly_prev = i_poly;
 +                      /* ignore the duplicate point */
 +              }
 +      }
 +
 +      uint lines_len = lines_step - lines;
 +
 +      /* Hide Lines (we could make optional) */
 +      {
 +              qsort(lines, lines_len, sizeof(int32_t), BLI_sortutil_cmp_int);
 +              lines_step = lines;
 +
 +              if (lines[0] != lines[1]) {
 +                      *lines_step++ = lines[0];
 +              }
 +              for (uint i_prev = 0, i = 1; i < lines_len; i_prev = i++) {
 +                      if (lines[i] != lines[i_prev]) {
 +                              *lines_step++ = lines[i];
 +                      }
 +              }
 +              lines_len = lines_step - lines;
 +      }
 +
 +      /* We have vertices and tris, make a batch from this. */
 +      static Gwn_VertFormat format = {0};
 +      static struct { uint pos; } attr_id;
 +      if (format.attrib_ct == 0) {
 +              attr_id.pos = GWN_vertformat_attr_add(&format, "pos", GWN_COMP_F32, 2, GWN_FETCH_FLOAT);
 +      }
 +
 +      Gwn_VertBuf *vbo = GWN_vertbuf_create_with_format(&format);
 +      const uint vbo_len_capacity = lines_len * 2;
 +      GWN_vertbuf_data_alloc(vbo, vbo_len_capacity);
 +
 +      Gwn_VertBufRaw pos_step;
 +      GWN_vertbuf_attr_get_raw_data(vbo, attr_id.pos, &pos_step);
 +
 +      for (uint i = 0; i < lines_len; i++) {
 +              union {
 +                      uint8_t  as_u8_pair[2][2];
 +                      uint32_t as_u32;
 +              } data;
 +              data.as_u32 = lines[i];
 +              for (uint k = 0; k < 2; k++) {
 +                      float *pos_v2 = GWN_vertbuf_raw_step(&pos_step);
 +                      for (uint j = 0; j < 2; j++) {
 +                              pos_v2[j] = min_uchar[j] + ((float)data.as_u8_pair[k][j] * range_uchar[j]);
 +                      }
 +              }
 +      }
 +      BLI_assert(vbo_len_capacity == GWN_vertbuf_raw_used(&pos_step));
 +      MEM_freeN(lines);
 +      return GWN_batch_create_ex(
 +              GWN_PRIM_LINES, vbo,
 +              NULL,
 +              GWN_BATCH_OWNS_VBO);
 +}
 +
 +/** \} */
 +
 +/* -------------------------------------------------------------------- */
 +/** \name Init/Exit
 + * \{ */
 +
 +void gpu_batch_init(void)
 +{
 +      gpu_batch_presets_init();
 +}
 +
 +void gpu_batch_exit(void)
 +{
 +      gpu_batch_presets_exit();
 +}
 +
 +/** \} */
index 001f1d5f7b3e2ff5e438cf12d60f197d55431121,cc1c95bd0fe1e88683bbac9757605aab034e9324..ea2b2741f8c69ca0c862bb1c4133967c86815b50
@@@ -51,9 -51,8 +51,9 @@@ BLENDER_TEST(BLI_listbase "bf_blenlib"
  BLENDER_TEST(BLI_math_base "bf_blenlib")
  BLENDER_TEST(BLI_math_color "bf_blenlib")
  BLENDER_TEST(BLI_math_geom "bf_blenlib")
 +BLENDER_TEST(BLI_memiter "bf_blenlib")
  BLENDER_TEST(BLI_path_util "${BLI_path_util_extra_libs}")
- BLENDER_TEST(BLI_polyfill2d "bf_blenlib")
+ BLENDER_TEST(BLI_polyfill_2d "bf_blenlib")
  BLENDER_TEST(BLI_stack "bf_blenlib")
  BLENDER_TEST(BLI_string "bf_blenlib")
  BLENDER_TEST(BLI_string_utf8 "bf_blenlib")