Workbench: Use DRWView instead of DRW_viewport_matrix_*
authorClément Foucault <foucault.clem@gmail.com>
Mon, 20 May 2019 18:33:56 +0000 (20:33 +0200)
committerClément Foucault <foucault.clem@gmail.com>
Wed, 22 May 2019 11:29:04 +0000 (13:29 +0200)
Continuing the transition to the new API

source/blender/draw/engines/workbench/workbench_data.c
source/blender/draw/engines/workbench/workbench_effect_taa.c
source/blender/draw/engines/workbench/workbench_private.h
source/blender/draw/engines/workbench/workbench_render.c

index e4b2d09..fce1d72 100644 (file)
@@ -147,7 +147,7 @@ void workbench_private_data_init(WORKBENCH_PrivateData *wpd)
     const int ssao_samples = scene->display.matcap_ssao_samples;
 
     float invproj[4][4];
-    const bool is_persp = DRW_viewport_is_persp_get();
+    const bool is_persp = DRW_view_is_persp_get(NULL);
     /* view vectors for the corners of the view frustum.
      * Can be used to recreate the world space position easily */
     float viewvecs[3][4] = {
@@ -170,9 +170,8 @@ void workbench_private_data_init(WORKBENCH_PrivateData *wpd)
                 wpd->shading.cavity_ridge_factor,
                 scene->display.matcap_ssao_attenuation);
 
-    /* invert the view matrix */
-    DRW_viewport_matrix_get(wpd->winmat, DRW_MAT_WIN);
-    invert_m4_m4(invproj, wpd->winmat);
+    DRW_view_winmat_get(NULL, wpd->winmat, false);
+    DRW_view_winmat_get(NULL, invproj, true);
 
     /* convert the view vectors to view space */
     for (i = 0; i < 3; i++) {
@@ -212,7 +211,7 @@ void workbench_private_data_get_light_direction(WORKBENCH_PrivateData *wpd,
   Scene *scene = draw_ctx->scene;
   WORKBENCH_UBO_World *wd = &wpd->world_data;
   float view_matrix[4][4];
-  DRW_viewport_matrix_get(view_matrix, DRW_MAT_VIEW);
+  DRW_view_viewmat_get(NULL, view_matrix, false);
 
   copy_v3_v3(r_light_direction, scene->display.light_direction);
   SWAP(float, r_light_direction[2], r_light_direction[1]);
index 7b2faf0..69f41f5 100644 (file)
@@ -135,6 +135,8 @@ void workbench_taa_engine_init(WORKBENCH_Data *vedata)
     workbench_taa_jitter_init();
   }
 
+  effect_info->view = NULL;
+
   /* reset complete drawing when navigating. */
   if (effect_info->jitter_index != 0) {
     if (rv3d && rv3d->rflag & RV3D_NAVIGATING) {
@@ -148,12 +150,10 @@ void workbench_taa_engine_init(WORKBENCH_Data *vedata)
   }
 
   {
-    float view[4][4];
-    float win[4][4];
-    DRW_viewport_matrix_get(view, DRW_MAT_VIEW);
-    DRW_viewport_matrix_get(win, DRW_MAT_WIN);
-    mul_m4_m4m4(effect_info->curr_mat, view, win);
-    if (!equals_m4m4(effect_info->curr_mat, effect_info->last_mat)) {
+    float persmat[4][4];
+    DRW_view_persmat_get(NULL, persmat, false);
+    if (!equals_m4m4(persmat, effect_info->last_mat)) {
+      copy_m4_m4(effect_info->last_mat, persmat);
       effect_info->jitter_index = 0;
     }
   }
@@ -217,9 +217,9 @@ void workbench_taa_draw_scene_start(WORKBENCH_Data *vedata)
   WORKBENCH_StorageList *stl = vedata->stl;
   WORKBENCH_EffectInfo *effect_info = stl->effects;
   const float *viewport_size = DRW_viewport_size_get();
+  const DRWView *default_view = DRW_view_default_get();
   int num_samples = 8;
   float(*samples)[2];
-  float mix_factor;
 
   num_samples = workbench_taa_calculate_num_iterations(vedata);
   switch (num_samples) {
@@ -241,41 +241,38 @@ void workbench_taa_draw_scene_start(WORKBENCH_Data *vedata)
       break;
   }
 
-  mix_factor = 1.0f / (effect_info->jitter_index + 1);
-
   const int jitter_index = effect_info->jitter_index;
   const float *transform_offset = samples[jitter_index];
+  effect_info->taa_mix_factor = 1.0f / (effect_info->jitter_index + 1);
   effect_info->jitter_index = (jitter_index + 1) % num_samples;
 
   /* construct new matrices from transform delta */
-  float viewmat[4][4];
-  float persmat[4][4];
-  DRW_viewport_matrix_get(viewmat, DRW_MAT_VIEW);
-  DRW_viewport_matrix_get(persmat, DRW_MAT_PERS);
-  DRW_viewport_matrix_get(effect_info->override_winmat, DRW_MAT_WIN);
+  float winmat[4][4], viewmat[4][4], persmat[4][4];
+  DRW_view_winmat_get(default_view, winmat, false);
+  DRW_view_viewmat_get(default_view, viewmat, false);
+  DRW_view_persmat_get(default_view, persmat, false);
 
-  window_translate_m4(effect_info->override_winmat,
+  window_translate_m4(winmat,
                       persmat,
                       transform_offset[0] / viewport_size[0],
                       transform_offset[1] / viewport_size[1]);
 
-  mul_m4_m4m4(effect_info->override_persmat, effect_info->override_winmat, viewmat);
-  invert_m4_m4(effect_info->override_persinv, effect_info->override_persmat);
-  invert_m4_m4(effect_info->override_wininv, effect_info->override_winmat);
-
-  DRW_viewport_matrix_override_set(effect_info->override_persmat, DRW_MAT_PERS);
-  DRW_viewport_matrix_override_set(effect_info->override_persinv, DRW_MAT_PERSINV);
-  DRW_viewport_matrix_override_set(effect_info->override_winmat, DRW_MAT_WIN);
-  DRW_viewport_matrix_override_set(effect_info->override_wininv, DRW_MAT_WININV);
-
-  /* weight the mix factor by the jitter index */
-  effect_info->taa_mix_factor = mix_factor;
+  if (effect_info->view) {
+    /* When rendering just update the view. This avoids recomputing the culling. */
+    DRW_view_update_sub(effect_info->view, viewmat, winmat);
+  }
+  else {
+    /* TAA is not making a big change to the matrices.
+     * Reuse the main view culling by creating a subview. */
+    effect_info->view = DRW_view_create_sub(default_view, viewmat, winmat);
+  }
+  DRW_view_set_active(effect_info->view);
 }
 
 void workbench_taa_draw_scene_end(WORKBENCH_Data *vedata)
 {
   /*
-   * If first frame than the offset is 0.0 and its depth is the depth buffer to use
+   * If first frame then the offset is 0.0 and its depth is the depth buffer to use
    * for the rest of the draw engines. We store it in a persistent buffer.
    *
    * If it is not the first frame we copy the persistent buffer back to the
@@ -296,10 +293,9 @@ void workbench_taa_draw_scene_end(WORKBENCH_Data *vedata)
   GPU_framebuffer_blit(dfbl->color_only_fb, 0, fbl->effect_taa_fb, 0, GPU_COLOR_BIT);
 
   if (!DRW_state_is_image_render()) {
-    DRW_viewport_matrix_override_unset_all();
+    DRW_view_set_active(NULL);
   }
 
-  copy_m4_m4(effect_info->last_mat, effect_info->curr_mat);
   if (effect_info->jitter_index != 0 && !DRW_state_is_image_render()) {
     DRW_viewport_request_redraw();
   }
index 09d9ad6..af94019 100644 (file)
@@ -273,12 +273,10 @@ typedef struct WORKBENCH_PrivateData {
 } WORKBENCH_PrivateData; /* Transient data */
 
 typedef struct WORKBENCH_EffectInfo {
-  float override_persmat[4][4];
-  float override_persinv[4][4];
-  float override_winmat[4][4];
-  float override_wininv[4][4];
+  /** View */
+  struct DRWView *view;
+  /** Last projection matrix to see if view is still valid. */
   float last_mat[4][4];
-  float curr_mat[4][4];
   int jitter_index;
   float taa_mix_factor;
   bool view_updated;
index 1497ef4..46d78ca 100644 (file)
@@ -63,24 +63,16 @@ static void workbench_render_matrices_init(RenderEngine *engine, Depsgraph *deps
   float frame = BKE_scene_frame_get(scene);
 
   /* Set the persective, view and window matrix. */
-  float winmat[4][4], wininv[4][4];
-  float viewmat[4][4], viewinv[4][4];
-  float persmat[4][4], persinv[4][4];
+  float winmat[4][4], viewmat[4][4], viewinv[4][4];
 
   RE_GetCameraWindow(engine->re, ob_camera_eval, frame, winmat);
   RE_GetCameraModelMatrix(engine->re, ob_camera_eval, viewinv);
 
   invert_m4_m4(viewmat, viewinv);
-  mul_m4_m4m4(persmat, winmat, viewmat);
-  invert_m4_m4(persinv, persmat);
-  invert_m4_m4(wininv, winmat);
-
-  DRW_viewport_matrix_override_set(persmat, DRW_MAT_PERS);
-  DRW_viewport_matrix_override_set(persinv, DRW_MAT_PERSINV);
-  DRW_viewport_matrix_override_set(winmat, DRW_MAT_WIN);
-  DRW_viewport_matrix_override_set(wininv, DRW_MAT_WININV);
-  DRW_viewport_matrix_override_set(viewmat, DRW_MAT_VIEW);
-  DRW_viewport_matrix_override_set(viewinv, DRW_MAT_VIEWINV);
+
+  DRWView *view = DRW_view_create(viewmat, winmat, NULL, NULL, NULL);
+  DRW_view_default_set(view);
+  DRW_view_set_active(view);
 }
 
 static bool workbench_render_framebuffers_init(void)
@@ -159,9 +151,6 @@ void workbench_render(WORKBENCH_Data *data,
       if (RE_engine_test_break(engine)) {
         break;
       }
-      /* TODO: Save matrices instead of recomputing them for each samples. */
-      workbench_render_matrices_init(engine, depsgraph);
-
       workbench_deferred_draw_background(data);
       workbench_deferred_draw_scene(data);
     }