DRW: Add DRWMatrixState to manage all matrices together.
authorClément Foucault <foucault.clem@gmail.com>
Fri, 2 Mar 2018 17:05:42 +0000 (18:05 +0100)
committerClément Foucault <foucault.clem@gmail.com>
Fri, 2 Mar 2018 17:35:59 +0000 (18:35 +0100)
source/blender/draw/intern/DRW_render.h
source/blender/draw/intern/draw_manager.c
source/blender/draw/intern/draw_manager.h
source/blender/draw/intern/draw_manager_data.c
source/blender/draw/intern/draw_manager_exec.c

index 1713960597eed472b5cb9870ae52c62b03fcc4ec..d8885bd77c50a5b049abb7757ccde425d71d5bfd 100644 (file)
@@ -398,12 +398,22 @@ typedef enum {
        DRW_MAT_VIEWINV,
        DRW_MAT_WIN,
        DRW_MAT_WININV,
+
+       DRW_MAT_COUNT, // Don't use this.
 } DRWViewportMatrixType;
 
+typedef struct DRWMatrixState{
+       float mat[DRW_MAT_COUNT][4][4];
+} DRWMatrixState;
+
 void DRW_viewport_init(const bContext *C);
 void DRW_viewport_matrix_get(float mat[4][4], DRWViewportMatrixType type);
+void DRW_viewport_matrix_get_all(DRWMatrixState *state);
 void DRW_viewport_matrix_override_set(float mat[4][4], DRWViewportMatrixType type);
+void DRW_viewport_matrix_override_set_all(DRWMatrixState *state);
 void DRW_viewport_matrix_override_unset(DRWViewportMatrixType type);
+void DRW_viewport_matrix_override_unset_all(void);
+
 const float *DRW_viewport_size_get(void);
 const float *DRW_viewport_screenvecs_get(void);
 const float *DRW_viewport_pixelsize_get(void);
index aa0ddd0d31ba82ce5d108b94f6e9529c07d2c9b5..8feffa246eff442f2e0ecbe0808262bacc5e773c 100644 (file)
@@ -405,14 +405,14 @@ static void drw_viewport_var_init(void)
                /* Refresh DST.pixelsize */
                DST.pixsize = rv3d->pixsize;
 
-               copy_m4_m4(DST.original_mat[DRW_MAT_PERS], rv3d->persmat);
-               copy_m4_m4(DST.original_mat[DRW_MAT_PERSINV], rv3d->persinv);
-               copy_m4_m4(DST.original_mat[DRW_MAT_VIEW], rv3d->viewmat);
-               copy_m4_m4(DST.original_mat[DRW_MAT_VIEWINV], rv3d->viewinv);
-               copy_m4_m4(DST.original_mat[DRW_MAT_WIN], rv3d->winmat);
-               invert_m4_m4(DST.original_mat[DRW_MAT_WININV], rv3d->winmat);
+               copy_m4_m4(DST.original_mat.mat[DRW_MAT_PERS], rv3d->persmat);
+               copy_m4_m4(DST.original_mat.mat[DRW_MAT_PERSINV], rv3d->persinv);
+               copy_m4_m4(DST.original_mat.mat[DRW_MAT_VIEW], rv3d->viewmat);
+               copy_m4_m4(DST.original_mat.mat[DRW_MAT_VIEWINV], rv3d->viewinv);
+               copy_m4_m4(DST.original_mat.mat[DRW_MAT_WIN], rv3d->winmat);
+               invert_m4_m4(DST.original_mat.mat[DRW_MAT_WININV], rv3d->winmat);
 
-               memcpy(DST.view_data.mat, DST.original_mat, sizeof(DST.original_mat));
+               memcpy(DST.view_data.matstate.mat, DST.original_mat.mat, sizeof(DST.original_mat.mat));
 
                copy_v4_v4(DST.view_data.viewcamtexcofac, rv3d->viewcamtexcofac);
        }
@@ -448,26 +448,46 @@ static void drw_viewport_var_init(void)
 
 void DRW_viewport_matrix_get(float mat[4][4], DRWViewportMatrixType type)
 {
-       BLI_assert(type >= DRW_MAT_PERS && type <= DRW_MAT_WININV);
+       BLI_assert(type >= 0 && type < DRW_MAT_COUNT);
        BLI_assert(((DST.override_mat & (1 << type)) != 0)|| DST.draw_ctx.rv3d != NULL); /* Can't use this in render mode. */
 
-       copy_m4_m4(mat, DST.view_data.mat[type]);
+       copy_m4_m4(mat, DST.view_data.matstate.mat[type]);
+}
+
+void DRW_viewport_matrix_get_all(DRWMatrixState *state)
+{
+       memcpy(state, DST.view_data.matstate.mat, sizeof(DRWMatrixState));
 }
 
 void DRW_viewport_matrix_override_set(float mat[4][4], DRWViewportMatrixType type)
 {
-       copy_m4_m4(DST.view_data.mat[type], mat);
+       BLI_assert(type < DRW_MAT_COUNT);
+       copy_m4_m4(DST.view_data.matstate.mat[type], mat);
        DST.override_mat |= (1 << type);
        DST.dirty_mat = true;
 }
 
 void DRW_viewport_matrix_override_unset(DRWViewportMatrixType type)
 {
-       copy_m4_m4(DST.view_data.mat[type], DST.original_mat[type]);
+       BLI_assert(type < DRW_MAT_COUNT);
+       copy_m4_m4(DST.view_data.matstate.mat[type], DST.original_mat.mat[type]);
        DST.override_mat &= ~(1 << type);
        DST.dirty_mat = true;
 }
 
+void DRW_viewport_matrix_override_set_all(DRWMatrixState *state)
+{
+       memcpy(DST.view_data.matstate.mat, state, sizeof(DRWMatrixState));
+       DST.dirty_mat = true;
+}
+
+void DRW_viewport_matrix_override_unset_all(void)
+{
+       memcpy(DST.view_data.matstate.mat, DST.original_mat.mat, sizeof(DRWMatrixState));
+       DST.override_mat = 0;
+       DST.dirty_mat = true;
+}
+
 bool DRW_viewport_is_persp_get(void)
 {
        RegionView3D *rv3d = DST.draw_ctx.rv3d;
@@ -475,7 +495,7 @@ bool DRW_viewport_is_persp_get(void)
                return rv3d->is_persp;
        }
        else {
-               return DST.view_data.mat[DRW_MAT_WIN][3][3] == 0.0f;
+               return DST.view_data.matstate.mat[DRW_MAT_WIN][3][3] == 0.0f;
        }
        BLI_assert(0);
        return false;
index 58973471c4e062a58be3085663768609c6773dd3..f8989a0703a948106770a9feed9e583543113b64 100644 (file)
@@ -305,13 +305,13 @@ typedef struct DRWManager {
        double cache_time;
 
        /* View dependant uniforms. */
-       float original_mat[6][4][4]; /* Original rv3d matrices. */
+       DRWMatrixState original_mat; /* Original rv3d matrices. */
        int override_mat;            /* Bitflag of which matrices are overriden. */
        int num_clip_planes;         /* Number of active clipplanes. */
        bool dirty_mat;
 
        struct {
-               float mat[6][4][4];
+               DRWMatrixState matstate;
                float viewcamtexcofac[4];
                float clip_planes_eq[MAX_CLIP_PLANES][4];
        } view_data;
index fd4652a030577e521f2c26873983c410f4a46efc..8a6440054846918945eba113534a2f32565b2816 100644 (file)
@@ -458,12 +458,12 @@ static void drw_interface_init(DRWShadingGroup *shgroup, GPUShader *shader)
 #endif
 
        /* TODO : They should be grouped inside a UBO updated once per redraw. */
-       drw_interface_builtin_uniform(shgroup, GWN_UNIFORM_VIEW, DST.view_data.mat[DRW_MAT_VIEW], 16, 1);
-       drw_interface_builtin_uniform(shgroup, GWN_UNIFORM_VIEW_INV, DST.view_data.mat[DRW_MAT_VIEWINV], 16, 1);
-       drw_interface_builtin_uniform(shgroup, GWN_UNIFORM_VIEWPROJECTION, DST.view_data.mat[DRW_MAT_PERS], 16, 1);
-       drw_interface_builtin_uniform(shgroup, GWN_UNIFORM_VIEWPROJECTION_INV, DST.view_data.mat[DRW_MAT_PERSINV], 16, 1);
-       drw_interface_builtin_uniform(shgroup, GWN_UNIFORM_PROJECTION, DST.view_data.mat[DRW_MAT_WIN], 16, 1);
-       drw_interface_builtin_uniform(shgroup, GWN_UNIFORM_PROJECTION_INV, DST.view_data.mat[DRW_MAT_WININV], 16, 1);
+       drw_interface_builtin_uniform(shgroup, GWN_UNIFORM_VIEW, DST.view_data.matstate.mat[DRW_MAT_VIEW], 16, 1);
+       drw_interface_builtin_uniform(shgroup, GWN_UNIFORM_VIEW_INV, DST.view_data.matstate.mat[DRW_MAT_VIEWINV], 16, 1);
+       drw_interface_builtin_uniform(shgroup, GWN_UNIFORM_VIEWPROJECTION, DST.view_data.matstate.mat[DRW_MAT_PERS], 16, 1);
+       drw_interface_builtin_uniform(shgroup, GWN_UNIFORM_VIEWPROJECTION_INV, DST.view_data.matstate.mat[DRW_MAT_PERSINV], 16, 1);
+       drw_interface_builtin_uniform(shgroup, GWN_UNIFORM_PROJECTION, DST.view_data.matstate.mat[DRW_MAT_WIN], 16, 1);
+       drw_interface_builtin_uniform(shgroup, GWN_UNIFORM_PROJECTION_INV, DST.view_data.matstate.mat[DRW_MAT_WININV], 16, 1);
        drw_interface_builtin_uniform(shgroup, GWN_UNIFORM_CAMERATEXCO, DST.view_data.viewcamtexcofac, 3, 2);
        drw_interface_builtin_uniform(shgroup, GWN_UNIFORM_CLIPPLANES, DST.view_data.clip_planes_eq, 4, DST.num_clip_planes); /* TO REMOVE */
 
@@ -895,7 +895,7 @@ static int pass_shgroup_dist_sort(void *thunk, const void *a, const void *b)
 void DRW_pass_sort_shgroup_z(DRWPass *pass)
 {
        float (*viewinv)[4];
-       viewinv = DST.view_data.mat[DRW_MAT_VIEWINV];
+       viewinv = DST.view_data.matstate.mat[DRW_MAT_VIEWINV];
 
        ZSortData zsortdata = {viewinv[2], viewinv[3]};
 
index 055baff6a967b4da0483cd7cd591a3a8ba512cd0..6cf8dfc7904bd65b159e9b8b200580bb92490a02 100644 (file)
@@ -388,10 +388,10 @@ static void draw_clipping_setup_from_view(void)
        if (DST.clipping.updated)
                return;
 
-       float (*viewprojinv)[4] = DST.view_data.mat[DRW_MAT_PERSINV];
-       float (*viewinv)[4] = DST.view_data.mat[DRW_MAT_VIEWINV];
-       float (*projmat)[4] = DST.view_data.mat[DRW_MAT_WIN];
-       float (*projinv)[4] = DST.view_data.mat[DRW_MAT_WININV];
+       float (*viewprojinv)[4] = DST.view_data.matstate.mat[DRW_MAT_PERSINV];
+       float (*viewinv)[4] = DST.view_data.matstate.mat[DRW_MAT_VIEWINV];
+       float (*projmat)[4] = DST.view_data.matstate.mat[DRW_MAT_WIN];
+       float (*projinv)[4] = DST.view_data.matstate.mat[DRW_MAT_WININV];
        BoundSphere *bsphere = &DST.clipping.frustum_bsphere;
 
        /* Extract Clipping Planes */
@@ -555,13 +555,13 @@ static void draw_matrices_model_prepare(DRWCallState *st)
        if (st->matflag & (DRW_CALL_MODELVIEW | DRW_CALL_MODELVIEWINVERSE |
                          DRW_CALL_NORMALVIEW | DRW_CALL_EYEVEC))
        {
-               mul_m4_m4m4(st->modelview, DST.view_data.mat[DRW_MAT_VIEW], st->model);
+               mul_m4_m4m4(st->modelview, DST.view_data.matstate.mat[DRW_MAT_VIEW], st->model);
        }
        if (st->matflag & DRW_CALL_MODELVIEWINVERSE) {
                invert_m4_m4(st->modelviewinverse, st->modelview);
        }
        if (st->matflag & DRW_CALL_MODELVIEWPROJECTION) {
-               mul_m4_m4m4(st->modelviewprojection, DST.view_data.mat[DRW_MAT_PERS], st->model);
+               mul_m4_m4m4(st->modelviewprojection, DST.view_data.matstate.mat[DRW_MAT_PERS], st->model);
        }
        if (st->matflag & DRW_CALL_NORMALVIEW) {
                copy_m3_m4(st->normalview, st->modelview);
@@ -610,9 +610,9 @@ static void draw_geometry_prepare(DRWShadingGroup *shgroup, DRWCallState *state)
                unit_m4(unitmat);
                GPU_shader_uniform_vector(shgroup->shader, shgroup->model, 16, 1, (float *)unitmat);
                GPU_shader_uniform_vector(shgroup->shader, shgroup->modelinverse, 16, 1, (float *)unitmat);
-               GPU_shader_uniform_vector(shgroup->shader, shgroup->modelview, 16, 1, (float *)DST.view_data.mat[DRW_MAT_VIEW]);
-               GPU_shader_uniform_vector(shgroup->shader, shgroup->modelviewinverse, 16, 1, (float *)DST.view_data.mat[DRW_MAT_VIEWINV]);
-               GPU_shader_uniform_vector(shgroup->shader, shgroup->modelviewprojection, 16, 1, (float *)DST.view_data.mat[DRW_MAT_PERS]);
+               GPU_shader_uniform_vector(shgroup->shader, shgroup->modelview, 16, 1, (float *)DST.view_data.matstate.mat[DRW_MAT_VIEW]);
+               GPU_shader_uniform_vector(shgroup->shader, shgroup->modelviewinverse, 16, 1, (float *)DST.view_data.matstate.mat[DRW_MAT_VIEWINV]);
+               GPU_shader_uniform_vector(shgroup->shader, shgroup->modelviewprojection, 16, 1, (float *)DST.view_data.matstate.mat[DRW_MAT_PERS]);
                GPU_shader_uniform_vector(shgroup->shader, shgroup->orcotexfac, 3, 2, (float *)shgroup->instance_orcofac);
        }
 }