Blender  V3.3
Classes | Macros | Typedefs | Functions
gpu_matrix.cc File Reference
#include "gpu_context_private.hh"
#include "gpu_matrix_private.h"
#include "GPU_matrix.h"
#include "BLI_math_matrix.h"
#include "BLI_math_rotation.h"
#include "BLI_math_vector.h"
#include "MEM_guardedalloc.h"

Go to the source code of this file.

Classes

struct  MatrixStack
 
struct  GPUMatrixState
 

Macros

#define SUPPRESS_GENERIC_MATRIX_API
 
#define USE_GPU_PY_MATRIX_API   /* only so values are declared */
 
#define MATRIX_STACK_DEPTH   32
 
#define ModelViewStack   Context::get()->matrix_state->model_view_stack
 
#define ModelView   ModelViewStack.stack[ModelViewStack.top]
 
#define ProjectionStack   Context::get()->matrix_state->projection_stack
 
#define Projection   ProjectionStack.stack[ProjectionStack.top]
 
#define MATRIX_4X4_IDENTITY
 
#define CHECKMAT(m)
 

Typedefs

using Mat4 = float[4][4]
 
using Mat3 = float[3][3]
 

Functions

GPUMatrixStateGPU_matrix_state_create ()
 
void GPU_matrix_state_discard (GPUMatrixState *state)
 
static void gpu_matrix_state_active_set_dirty (bool value)
 
void GPU_matrix_reset ()
 
void GPU_matrix_push ()
 
void GPU_matrix_pop ()
 
void GPU_matrix_push_projection ()
 
void GPU_matrix_pop_projection ()
 
void GPU_matrix_set (const float m[4][4])
 
void GPU_matrix_identity_projection_set ()
 
void GPU_matrix_projection_set (const float m[4][4])
 
void GPU_matrix_identity_set ()
 
void GPU_matrix_translate_2f (float x, float y)
 
void GPU_matrix_translate_2fv (const float vec[2])
 
void GPU_matrix_translate_3f (float x, float y, float z)
 
void GPU_matrix_translate_3fv (const float vec[3])
 
void GPU_matrix_scale_1f (float factor)
 
void GPU_matrix_scale_2f (float x, float y)
 
void GPU_matrix_scale_2fv (const float vec[2])
 
void GPU_matrix_scale_3f (float x, float y, float z)
 
void GPU_matrix_scale_3fv (const float vec[3])
 
void GPU_matrix_mul (const float m[4][4])
 
void GPU_matrix_rotate_2d (float deg)
 
void GPU_matrix_rotate_3f (float deg, float x, float y, float z)
 
void GPU_matrix_rotate_3fv (float deg, const float axis[3])
 
void GPU_matrix_rotate_axis (float deg, char axis)
 
static void mat4_ortho_set (float m[4][4], float left, float right, float bottom, float top, float near, float far)
 
static void mat4_frustum_set (float m[4][4], float left, float right, float bottom, float top, float near, float far)
 
static void mat4_look_from_origin (float m[4][4], float lookdir[3], float camup[3])
 
void GPU_matrix_ortho_set (float left, float right, float bottom, float top, float near, float far)
 
void GPU_matrix_ortho_set_z (float near, float far)
 
void GPU_matrix_ortho_2d_set (float left, float right, float bottom, float top)
 
void GPU_matrix_frustum_set (float left, float right, float bottom, float top, float near, float far)
 
void GPU_matrix_perspective_set (float fovy, float aspect, float near, float far)
 
void GPU_matrix_look_at (float eyeX, float eyeY, float eyeZ, float centerX, float centerY, float centerZ, float upX, float upY, float upZ)
 
void GPU_matrix_project_3fv (const float world[3], const float model[4][4], const float proj[4][4], const int view[4], float win[3])
 
void GPU_matrix_project_2fv (const float world[3], const float model[4][4], const float proj[4][4], const int view[4], float win[2])
 
bool GPU_matrix_unproject_3fv (const float win[3], const float model_inverted[4][4], const float proj[4][4], const int view[4], float r_world[3])
 
const float(* GPU_matrix_model_view_get (float m[4][4]))[4]
 
const float(* GPU_matrix_projection_get (float m[4][4]))[4]
 
const float(* GPU_matrix_model_view_projection_get (float m[4][4]))[4]
 
const float(* GPU_matrix_normal_get (float m[3][3]))[3]
 
const float(* GPU_matrix_normal_inverse_get (float m[3][3]))[3]
 
void GPU_matrix_bind (GPUShader *shader)
 
bool GPU_matrix_dirty_get ()
 
Python API Helpers
 BLI_STATIC_ASSERT (GPU_PY_MATRIX_STACK_LEN+1==MATRIX_STACK_DEPTH, "define mismatch")
 
int GPU_matrix_stack_level_get_model_view ()
 
int GPU_matrix_stack_level_get_projection ()
 
Polygon Offset Hack

Workaround the fact that polygon-offset is implementation dependent. We modify the projection matrix winmat in order to change the final depth a tiny amount.

float GPU_polygon_offset_calc (const float(*winmat)[4], float viewdist, float dist)
 
void GPU_polygon_offset (float viewdist, float dist)
 

Macro Definition Documentation

◆ CHECKMAT

#define CHECKMAT (   m)

Definition at line 115 of file gpu_matrix.cc.

◆ MATRIX_4X4_IDENTITY

#define MATRIX_4X4_IDENTITY
Value:
{ \
{1.0f, 0.0f, 0.0f, 0.0f}, {0.0f, 1.0f, 0.0f, 0.0f}, {0.0f, 0.0f, 1.0f, 0.0f}, \
{ \
0.0f, 0.0f, 0.0f, 1.0f \
} \
}

◆ MATRIX_STACK_DEPTH

#define MATRIX_STACK_DEPTH   32

Definition at line 24 of file gpu_matrix.cc.

◆ ModelView

#define ModelView   ModelViewStack.stack[ModelViewStack.top]

Definition at line 49 of file gpu_matrix.cc.

◆ ModelViewStack

#define ModelViewStack   Context::get()->matrix_state->model_view_stack

Definition at line 48 of file gpu_matrix.cc.

◆ Projection

#define Projection   ProjectionStack.stack[ProjectionStack.top]

Definition at line 52 of file gpu_matrix.cc.

◆ ProjectionStack

#define ProjectionStack   Context::get()->matrix_state->projection_stack

Definition at line 51 of file gpu_matrix.cc.

◆ SUPPRESS_GENERIC_MATRIX_API

#define SUPPRESS_GENERIC_MATRIX_API

Definition at line 11 of file gpu_matrix.cc.

◆ USE_GPU_PY_MATRIX_API

#define USE_GPU_PY_MATRIX_API   /* only so values are declared */

Definition at line 12 of file gpu_matrix.cc.

Typedef Documentation

◆ Mat3

using Mat3 = float[3][3]

Definition at line 27 of file gpu_matrix.cc.

◆ Mat4

using Mat4 = float[4][4]

Definition at line 26 of file gpu_matrix.cc.

Function Documentation

◆ BLI_STATIC_ASSERT()

BLI_STATIC_ASSERT ( GPU_PY_MATRIX_STACK_LEN+  1 = =MATRIX_STACK_DEPTH,
"define mismatch"   
)

◆ GPU_matrix_bind()

void GPU_matrix_bind ( struct GPUShader shader)

◆ GPU_matrix_dirty_get()

bool GPU_matrix_dirty_get ( void  )

Definition at line 655 of file gpu_matrix.cc.

References blender::gpu::Context::get(), blender::gpu::Context::matrix_state, and state.

Referenced by GPU_shader_bind().

◆ GPU_matrix_frustum_set()

void GPU_matrix_frustum_set ( float  left,
float  right,
float  bottom,
float  top,
float  near,
float  far 
)

◆ GPU_matrix_identity_projection_set()

void GPU_matrix_identity_projection_set ( void  )

◆ GPU_matrix_identity_set()

void GPU_matrix_identity_set ( void  )

◆ GPU_matrix_look_at()

void GPU_matrix_look_at ( float  eyeX,
float  eyeY,
float  eyeZ,
float  centerX,
float  centerY,
float  centerZ,
float  upX,
float  upY,
float  upZ 
)

◆ GPU_matrix_model_view_get()

const float(* GPU_matrix_model_view_get ( float  m[4][4]) )[4]

Definition at line 552 of file gpu_matrix.cc.

References copy_m4_m4(), and ModelView.

Referenced by GPU_matrix_bind(), and GPU_matrix_normal_get().

◆ GPU_matrix_model_view_projection_get()

const float(* GPU_matrix_model_view_projection_get ( float  m[4][4]) )[4]

Definition at line 572 of file gpu_matrix.cc.

References ModelView, mul_m4_m4m4(), and Projection.

Referenced by GPU_matrix_bind().

◆ GPU_matrix_mul()

void GPU_matrix_mul ( const float  m[4][4])

◆ GPU_matrix_normal_get()

const float(* GPU_matrix_normal_get ( float  m[3][3]) )[3]

◆ GPU_matrix_normal_inverse_get()

const float(* GPU_matrix_normal_inverse_get ( float  m[3][3]) )[3]

Definition at line 598 of file gpu_matrix.cc.

References GPU_matrix_normal_get(), and invert_m3().

◆ GPU_matrix_ortho_2d_set()

void GPU_matrix_ortho_2d_set ( float  left,
float  right,
float  bottom,
float  top 
)

◆ GPU_matrix_ortho_set()

void GPU_matrix_ortho_set ( float  left,
float  right,
float  bottom,
float  top,
float  near,
float  far 
)

◆ GPU_matrix_ortho_set_z()

void GPU_matrix_ortho_set_z ( float  near,
float  far 
)

Definition at line 406 of file gpu_matrix.cc.

References CHECKMAT, gpu_matrix_state_active_set_dirty(), and Projection.

Referenced by gizmo_axis_draw(), and ui_draw_but_UNITVEC().

◆ GPU_matrix_perspective_set()

void GPU_matrix_perspective_set ( float  fovy,
float  aspect,
float  near,
float  far 
)

Definition at line 430 of file gpu_matrix.cc.

References GPU_matrix_frustum_set(), M_PI, and tanf.

◆ GPU_matrix_pop()

void GPU_matrix_pop ( void  )

Definition at line 126 of file gpu_matrix.cc.

References BLI_assert, gpu_matrix_state_active_set_dirty(), and ModelViewStack.

Referenced by arrow_draw_geom(), arrow_draw_intern(), blf_batch_draw_begin(), blf_draw_gl__end(), button2d_draw_intern(), clip_draw_grease_pencil(), clip_main_region_draw(), curve_draw_stroke_3d(), dial_3d_draw_util(), dial_ghostarc_draw_helpline(), draw_channel_headers(), draw_distortion(), draw_fcurve(), draw_fcurve_curve_bezts(), draw_fcurve_curve_samples(), draw_fcurve_sample_control(), draw_marker_areas(), draw_marker_outline(), draw_marker_slide_zones(), blender::ed::space_node::draw_nodespace_back_pix(), draw_plane_marker_image(), draw_render_info(), draw_seq_in_view(), draw_stabilization_border(), draw_tracking_tracks(), drawEdgeSlide(), drawLine(), drawPropCircle(), drawVertSlide(), DRW_draw_cursor(), DRW_draw_cursor_2d_ex(), drw_text_cache_draw_ex(), dyntopo_detail_size_edit_draw(), ED_gizmo_draw_preset_facemap(), ed_gizmo_draw_preset_geometry(), ED_markers_draw(), ED_mask_draw_region(), ED_region_image_metadata_draw(), ED_view3d_draw_offscreen(), EDBM_preselect_edgering_draw(), EDBM_preselect_elem_draw(), external_draw_scene_do_image(), external_draw_scene_do_v3d(), gizmo_axis_draw(), gizmo_cage2d_draw_intern(), gizmo_cage3d_draw_intern(), gizmo_primitive_draw_intern(), gizmo_ruler_draw(), gpencil_render_offscreen(), GPU_viewport_stereo_composite(), knifetool_draw_angle(), knifetool_draw_visible_distances(), blender::ed::sculpt_paint::min_distance_edit::min_distance_edit_draw(), move3d_draw_intern(), blender::ed::space_node::node_draw_space(), paint_cursor_cursor_draw_3d_view_brush_cursor_active(), paint_cursor_draw_3d_view_brush_cursor_inactive(), paint_draw_alpha_overlay(), paint_draw_cursor_overlay(), paint_draw_curve_cursor(), paint_draw_tex_overlay(), panel_draw_aligned_widgets(), playanim_toscreen(), pygpu_matrix_pop(), pygpu_matrix_stack_context_exit(), radial_control_paint_cursor(), radial_control_paint_tex(), region_draw_azones(), screen_preview_draw(), SCULPT_cloth_simulation_limits_draw(), SCULPT_layer_brush_height_preview_draw(), sequencer_draw_display_buffer(), timeline_cache_draw_single(), tracking_segment_knot_cb(), transform_draw_cursor_draw(), UI_block_draw(), ui_draw_but_UNITVEC(), ui_draw_pie_center(), voxel_size_edit_draw(), wm_software_cursor_draw_bitmap(), and wm_xr_controller_model_draw().

◆ GPU_matrix_pop_projection()

void GPU_matrix_pop_projection ( void  )

◆ GPU_matrix_project_2fv()

void GPU_matrix_project_2fv ( const float  world[3],
const float  model[4][4],
const float  proj[4][4],
const int  view[4],
float  win[2] 
)

Definition at line 481 of file gpu_matrix.cc.

References mul_m4_v4(), mul_v2_fl(), mul_v4_m4v3(), v, view, and world.

Referenced by ED_view3d_project_v2().

◆ GPU_matrix_project_3fv()

void GPU_matrix_project_3fv ( const float  world[3],
const float  model[4][4],
const float  proj[4][4],
const int  view[4],
float  win[3] 
)

Definition at line 461 of file gpu_matrix.cc.

References mul_m4_v4(), mul_v3_fl(), mul_v4_m4v3(), v, view, and world.

Referenced by ED_view3d_project_v3().

◆ GPU_matrix_projection_get()

const float(* GPU_matrix_projection_get ( float  m[4][4]) )[4]

Definition at line 562 of file gpu_matrix.cc.

References copy_m4_m4(), and Projection.

Referenced by GPU_matrix_bind(), and GPU_polygon_offset().

◆ GPU_matrix_projection_set()

void GPU_matrix_projection_set ( const float  m[4][4])

Definition at line 161 of file gpu_matrix.cc.

References CHECKMAT, copy_m4_m4(), gpu_matrix_state_active_set_dirty(), and Projection.

Referenced by GPU_polygon_offset().

◆ GPU_matrix_push()

void GPU_matrix_push ( void  )

Definition at line 119 of file gpu_matrix.cc.

References BLI_assert, copy_m4_m4(), MATRIX_STACK_DEPTH, ModelView, and ModelViewStack.

Referenced by arrow_draw_geom(), arrow_draw_intern(), blf_batch_draw_begin(), blf_draw_gl__start(), button2d_draw_intern(), clip_draw_grease_pencil(), clip_main_region_draw(), curve_draw_stroke_3d(), dial_3d_draw_util(), dial_ghostarc_draw_helpline(), draw_channel_headers(), draw_distortion(), draw_fcurve(), draw_fcurve_curve_bezts(), draw_fcurve_curve_samples(), draw_fcurve_sample_control(), draw_marker_areas(), draw_marker_outline(), draw_marker_slide_zones(), blender::ed::space_node::draw_nodespace_back_pix(), draw_plane_marker_image(), draw_render_info(), draw_seq_in_view(), draw_stabilization_border(), draw_tracking_tracks(), drawEdgeSlide(), drawLine(), drawPropCircle(), drawVertSlide(), DRW_draw_cursor(), DRW_draw_cursor_2d_ex(), drw_text_cache_draw_ex(), dyntopo_detail_size_edit_draw(), ED_gizmo_draw_preset_facemap(), ed_gizmo_draw_preset_geometry(), ED_markers_draw(), ED_mask_draw_region(), ED_region_image_metadata_draw(), ED_view3d_draw_offscreen(), EDBM_preselect_edgering_draw(), EDBM_preselect_elem_draw(), external_draw_scene_do_image(), external_draw_scene_do_v3d(), gizmo_axis_draw(), gizmo_cage2d_draw_intern(), gizmo_cage3d_draw_intern(), gizmo_primitive_draw_intern(), gizmo_ruler_draw(), gpencil_render_offscreen(), GPU_viewport_stereo_composite(), knifetool_draw_angle(), knifetool_draw_visible_distances(), blender::ed::sculpt_paint::min_distance_edit::min_distance_edit_draw(), move3d_draw_intern(), blender::ed::space_node::node_draw_space(), paint_cursor_cursor_draw_3d_view_brush_cursor_active(), paint_cursor_draw_3d_view_brush_cursor_inactive(), paint_draw_alpha_overlay(), paint_draw_cursor_overlay(), paint_draw_curve_cursor(), paint_draw_tex_overlay(), panel_draw_aligned_widgets(), playanim_toscreen(), pygpu_matrix_push(), pygpu_matrix_stack_context_enter(), radial_control_paint_cursor(), radial_control_paint_tex(), region_draw_azones(), screen_preview_draw(), SCULPT_cloth_simulation_limits_draw(), SCULPT_layer_brush_height_preview_draw(), sequencer_draw_display_buffer(), timeline_cache_draw_single(), tracking_segment_knot_cb(), transform_draw_cursor_draw(), UI_block_draw(), ui_draw_but_UNITVEC(), ui_draw_pie_center(), voxel_size_edit_draw(), wm_software_cursor_draw_bitmap(), and wm_xr_controller_model_draw().

◆ GPU_matrix_push_projection()

void GPU_matrix_push_projection ( void  )

◆ GPU_matrix_reset()

void GPU_matrix_reset ( void  )

◆ GPU_matrix_rotate_2d()

void GPU_matrix_rotate_2d ( float  deg)

Definition at line 253 of file gpu_matrix.cc.

References DEG2RADF, ModelView, and rotate_m4().

Referenced by blf_draw_gl__start(), paint_draw_tex_overlay(), and radial_control_paint_tex().

◆ GPU_matrix_rotate_3f()

void GPU_matrix_rotate_3f ( float  deg,
float  x,
float  y,
float  z 
)

Axis of rotation should be a unit vector.

Definition at line 261 of file gpu_matrix.cc.

References GPU_matrix_rotate_3fv(), x, y, and z.

Referenced by dial_ghostarc_draw_helpline(), and radial_control_paint_cursor().

◆ GPU_matrix_rotate_3fv()

void GPU_matrix_rotate_3fv ( float  deg,
const float  axis[3] 
)

Axis of rotation should be a unit vector.

Definition at line 267 of file gpu_matrix.cc.

References axis_angle_to_mat4(), DEG2RADF, and GPU_matrix_mul().

Referenced by GPU_matrix_rotate_3f().

◆ GPU_matrix_rotate_axis()

void GPU_matrix_rotate_axis ( float  deg,
char  axis 
)

◆ GPU_matrix_scale_1f()

void GPU_matrix_scale_1f ( float  factor)

◆ GPU_matrix_scale_2f()

void GPU_matrix_scale_2f ( float  x,
float  y 
)

◆ GPU_matrix_scale_2fv()

void GPU_matrix_scale_2fv ( const float  vec[2])

Definition at line 226 of file gpu_matrix.cc.

References GPU_matrix_scale_2f().

Referenced by pygpu_matrix_scale(), and radial_control_paint_cursor().

◆ GPU_matrix_scale_3f()

void GPU_matrix_scale_3f ( float  x,
float  y,
float  z 
)

Definition at line 231 of file gpu_matrix.cc.

References GPU_matrix_mul(), x, y, and z.

Referenced by arrow_draw_geom(), and GPU_matrix_scale_3fv().

◆ GPU_matrix_scale_3fv()

void GPU_matrix_scale_3fv ( const float  vec[3])

Definition at line 241 of file gpu_matrix.cc.

References GPU_matrix_scale_3f().

Referenced by blf_draw_gl__start(), and pygpu_matrix_scale().

◆ GPU_matrix_set()

void GPU_matrix_set ( const float  m[4][4])

Definition at line 147 of file gpu_matrix.cc.

References CHECKMAT, copy_m4_m4(), gpu_matrix_state_active_set_dirty(), and ModelView.

◆ GPU_matrix_stack_level_get_model_view()

int GPU_matrix_stack_level_get_model_view ( )

◆ GPU_matrix_stack_level_get_projection()

int GPU_matrix_stack_level_get_projection ( )

◆ gpu_matrix_state_active_set_dirty()

static void gpu_matrix_state_active_set_dirty ( bool  value)
static

◆ GPU_matrix_state_create()

GPUMatrixState* GPU_matrix_state_create ( void  )

Definition at line 54 of file gpu_matrix.cc.

References MATRIX_4X4_IDENTITY, MEM_mallocN, and state.

Referenced by blender::gpu::Context::Context().

◆ GPU_matrix_state_discard()

void GPU_matrix_state_discard ( GPUMatrixState state)

Definition at line 75 of file gpu_matrix.cc.

References MEM_freeN, and state.

Referenced by blender::gpu::Context::~Context().

◆ GPU_matrix_translate_2f()

void GPU_matrix_translate_2f ( float  x,
float  y 
)

◆ GPU_matrix_translate_2fv()

void GPU_matrix_translate_2fv ( const float  vec[2])

◆ GPU_matrix_translate_3f()

void GPU_matrix_translate_3f ( float  x,
float  y,
float  z 
)

◆ GPU_matrix_translate_3fv()

void GPU_matrix_translate_3fv ( const float  vec[3])

◆ GPU_matrix_unproject_3fv()

bool GPU_matrix_unproject_3fv ( const float  win[3],
const float  model_inverted[4][4],
const float  proj[4][4],
const int  view[4],
float  r_world[3] 
)

The same result could be obtained as follows:

float projinv[4][4];
invert_m4_m4(projinv, projview);
copy_v3_v3(r_world, in);
mul_project_m4_v3(projinv, r_world);
void mul_project_m4_v3(const float M[4][4], float vec[3])
Definition: math_matrix.c:820
bool invert_m4_m4(float R[4][4], const float A[4][4])
Definition: math_matrix.c:1287
MINLINE void copy_v3_v3(float r[3], const float a[3])

But that solution loses much precision. Therefore, get the same result without inverting the project view matrix.

Definition at line 500 of file gpu_matrix.cc.

References is_finite_v3(), mul_v3_m4v3(), usdtokens::out(), view, and zero_v3().

Referenced by ED_view3d_unproject_v3(), and gizmo_find_intersected_3d_intern().

◆ GPU_polygon_offset()

void GPU_polygon_offset ( float  viewdist,
float  dist 
)
Note
viewdist is only for orthographic projections at the moment.

Definition at line 721 of file gpu_matrix.cc.

References GPU_matrix_projection_get(), GPU_matrix_projection_set(), GPU_polygon_offset_calc(), and offset.

Referenced by annotation_draw_strokes(), ED_view3d_polygon_offset(), gpencil_draw_strokes(), and knifetool_draw().

◆ GPU_polygon_offset_calc()

float GPU_polygon_offset_calc ( const float(*)  winmat[4],
float  viewdist,
float  dist 
)

Own working polygon offset.

Definition at line 690 of file gpu_matrix.cc.

References float(), and UNUSED_VARS.

Referenced by DRW_view_create_with_zoffset(), and GPU_polygon_offset().

◆ mat4_frustum_set()

static void mat4_frustum_set ( float  m[4][4],
float  left,
float  right,
float  bottom,
float  top,
float  near,
float  far 
)
static

Definition at line 308 of file gpu_matrix.cc.

References bottom, gpu_matrix_state_active_set_dirty(), left, right, and top.

Referenced by GPU_matrix_frustum_set().

◆ mat4_look_from_origin()

static void mat4_look_from_origin ( float  m[4][4],
float  lookdir[3],
float  camup[3] 
)
static

Definition at line 334 of file gpu_matrix.cc.

References cross_v3_v3v3(), gpu_matrix_state_active_set_dirty(), and normalize_v3().

Referenced by GPU_matrix_look_at().

◆ mat4_ortho_set()

static void mat4_ortho_set ( float  m[4][4],
float  left,
float  right,
float  bottom,
float  top,
float  near,
float  far 
)
static

Definition at line 282 of file gpu_matrix.cc.

References bottom, gpu_matrix_state_active_set_dirty(), left, right, and top.

Referenced by GPU_matrix_ortho_2d_set(), and GPU_matrix_ortho_set().