Eevee: Use DRW_view_* API instead of DRW_viewport_matrix_*
authorClément Foucault <foucault.clem@gmail.com>
Tue, 21 May 2019 10:34:48 +0000 (12:34 +0200)
committerClément Foucault <foucault.clem@gmail.com>
Wed, 22 May 2019 11:29:05 +0000 (13:29 +0200)
source/blender/draw/engines/eevee/eevee_effects.c
source/blender/draw/engines/eevee/eevee_engine.c
source/blender/draw/engines/eevee/eevee_lightprobes.c
source/blender/draw/engines/eevee/eevee_lights.c
source/blender/draw/engines/eevee/eevee_lookdev.c
source/blender/draw/engines/eevee/eevee_materials.c
source/blender/draw/engines/eevee/eevee_motion_blur.c
source/blender/draw/engines/eevee/eevee_private.h
source/blender/draw/engines/eevee/eevee_render.c
source/blender/draw/engines/eevee/eevee_temporal_sampling.c
source/blender/draw/engines/eevee/eevee_volumes.c

index 8a85c66..3562afc 100644 (file)
@@ -360,6 +360,13 @@ void EEVEE_effects_cache_init(EEVEE_ViewLayerData *sldata, EEVEE_Data *vedata)
     DRW_shgroup_uniform_vec4(grp, "color2", effects->color_checker_light, 1);
     DRW_shgroup_uniform_int_copy(grp, "size", 8);
     DRW_shgroup_call(grp, quad, NULL);
+
+    float mat[4][4];
+    unit_m4(mat);
+
+    /* Using default view bypasses the culling. */
+    const DRWView *default_view = DRW_view_default_get();
+    effects->checker_view = DRW_view_create_sub(default_view, mat, mat);
   }
 }
 
@@ -515,16 +522,11 @@ void EEVEE_draw_alpha_checker(EEVEE_Data *vedata)
   EEVEE_EffectsInfo *effects = stl->effects;
 
   if ((effects->enabled_effects & EFFECT_ALPHA_CHECKER) != 0) {
-    float mat[4][4];
-    unit_m4(mat);
-
-    /* Fragile, rely on the fact that GPU_SHADER_2D_CHECKER
-     * only use the persmat. */
-    DRW_viewport_matrix_override_set(mat, DRW_MAT_PERS);
+    DRW_view_set_active(effects->checker_view);
 
     DRW_draw_pass(psl->alpha_checker);
 
-    DRW_viewport_matrix_override_unset(DRW_MAT_PERS);
+    DRW_view_set_active(NULL);
   }
 }
 
@@ -534,16 +536,17 @@ static void EEVEE_velocity_resolve(EEVEE_Data *vedata)
   EEVEE_FramebufferList *fbl = vedata->fbl;
   EEVEE_StorageList *stl = vedata->stl;
   EEVEE_EffectsInfo *effects = stl->effects;
+  struct DRWView *view = effects->taa_view;
 
   if ((effects->enabled_effects & EFFECT_VELOCITY_BUFFER) != 0) {
     DefaultTextureList *dtxl = DRW_viewport_texture_list_get();
     e_data.depth_src = dtxl->depth;
-    DRW_viewport_matrix_get(effects->velocity_curr_persinv, DRW_MAT_PERSINV);
+    DRW_view_persmat_get(view, effects->velocity_curr_persinv, true);
 
     GPU_framebuffer_bind(fbl->velocity_resolve_fb);
     DRW_draw_pass(psl->velocity_resolve);
   }
-  DRW_viewport_matrix_get(effects->velocity_past_persmat, DRW_MAT_PERS);
+  DRW_view_persmat_get(view, effects->velocity_past_persmat, false);
 }
 
 void EEVEE_draw_effects(EEVEE_ViewLayerData *UNUSED(sldata), EEVEE_Data *vedata)
@@ -594,7 +597,7 @@ void EEVEE_draw_effects(EEVEE_ViewLayerData *UNUSED(sldata), EEVEE_Data *vedata)
   }
 
   /* Record pers matrix for the next frame. */
-  DRW_viewport_matrix_get(stl->effects->prev_persmat, DRW_MAT_PERS);
+  DRW_view_persmat_get(effects->taa_view, effects->prev_persmat, false);
 
   /* Update double buffer status if render mode. */
   if (DRW_state_is_image_render()) {
index 481b566..35614ac 100644 (file)
@@ -88,11 +88,6 @@ static void eevee_engine_init(void *ved)
   EEVEE_materials_init(sldata, stl, fbl);
   EEVEE_lights_init(sldata);
   EEVEE_lightprobes_init(sldata, vedata);
-
-  if ((stl->effects->taa_current_sample > 1) && !DRW_state_is_image_render()) {
-    /* XXX otherwise it would break the other engines. */
-    DRW_viewport_matrix_override_unset_all();
-  }
 }
 
 static void eevee_cache_init(void *vedata)
@@ -217,10 +212,7 @@ static void eevee_draw_background(void *vedata)
     if (((stl->effects->enabled_effects & EFFECT_TAA) != 0) &&
         (stl->effects->taa_current_sample > 1) && !DRW_state_is_image_render() &&
         !taa_use_reprojection) {
-      DRW_viewport_matrix_override_set(stl->effects->overide_persmat, DRW_MAT_PERS);
-      DRW_viewport_matrix_override_set(stl->effects->overide_persinv, DRW_MAT_PERSINV);
-      DRW_viewport_matrix_override_set(stl->effects->overide_winmat, DRW_MAT_WIN);
-      DRW_viewport_matrix_override_set(stl->effects->overide_wininv, DRW_MAT_WININV);
+      DRW_view_set_active(stl->effects->taa_view);
     }
 
     /* when doing viewport rendering the overrides needs to be recalculated for
@@ -243,7 +235,7 @@ static void eevee_draw_background(void *vedata)
 
     /* Refresh shadows */
     DRW_stats_group_start("Shadows");
-    EEVEE_draw_shadows(sldata, vedata);
+    EEVEE_draw_shadows(sldata, vedata, stl->effects->taa_view);
     DRW_stats_group_end();
 
     /* Set ray type. */
@@ -310,7 +302,7 @@ static void eevee_draw_background(void *vedata)
     DRW_stats_group_end();
 
     if ((stl->effects->taa_current_sample > 1)) {
-      DRW_viewport_matrix_override_unset_all();
+      DRW_view_set_active(NULL);
     }
   }
 
index 8597703..5603889 100644 (file)
@@ -167,6 +167,9 @@ void EEVEE_lightprobes_init(EEVEE_ViewLayerData *sldata, EEVEE_Data *vedata)
     e_data.hammersley = create_hammersley_sample_texture(HAMMERSLEY_SIZE);
   }
 
+  memset(stl->g_data->cube_views, 0, sizeof(stl->g_data->cube_views));
+  memset(stl->g_data->planar_views, 0, sizeof(stl->g_data->planar_views));
+
   /* Use fallback if we don't have gpu texture allocated an we cannot restore them. */
   bool use_fallback_lightcache = (scene_eval->eevee.light_cache == NULL) ||
                                  ((scene_eval->eevee.light_cache->grid_tx.tex == NULL) &&
@@ -487,6 +490,8 @@ void EEVEE_lightprobes_cache_add(EEVEE_ViewLayerData *sldata, EEVEE_Data *vedata
   }
 
   if (probe->type == LIGHTPROBE_TYPE_PLANAR) {
+    /* TODO(fclem): Culling should be done after cache generation.
+     * This is needed for future draw cache persistence. */
     if (!eevee_lightprobes_culling_test(ob)) {
       return; /* Culled */
     }
@@ -868,7 +873,7 @@ static void lightbake_render_scene_face(int face, EEVEE_BakeRenderData *user_dat
   struct GPUFrameBuffer **face_fb = user_data->face_fb;
 
   /* Be sure that cascaded shadow maps are updated. */
-  EEVEE_draw_shadows(sldata, user_data->vedata);
+  EEVEE_draw_shadows(sldata, user_data->vedata, NULL /* TODO */);
 
   GPU_framebuffer_bind(face_fb[face]);
   GPU_framebuffer_clear_depth(face_fb[face], 1.0f);
@@ -928,7 +933,7 @@ static void lightbake_render_scene_reflected(int layer, EEVEE_BakeRenderData *us
   DRW_stats_group_start("Planar Reflection");
 
   /* Be sure that cascaded shadow maps are updated. */
-  EEVEE_draw_shadows(sldata, vedata);
+  EEVEE_draw_shadows(sldata, vedata, NULL /* TODO */);
 
   /* Compute offset plane equation (fix missing texels near reflection plane). */
   copy_v4_v4(sldata->clip_data.clip_planes[0], eplanar->plane_equation);
index 7106cf0..b4eda1b 100644 (file)
@@ -970,7 +970,7 @@ static void frustum_min_bounding_sphere(const float corners[8][3],
 static void eevee_shadow_cascade_setup(Object *ob,
                                        EEVEE_LightsInfo *linfo,
                                        EEVEE_LightEngineData *led,
-                                       DRWMatrixState *saved_mats,
+                                       DRWView *view,
                                        float view_near,
                                        float view_far,
                                        int sample_ofs)
@@ -978,9 +978,10 @@ static void eevee_shadow_cascade_setup(Object *ob,
   Light *la = (Light *)ob->data;
 
   /* Camera Matrices */
-  float(*persinv)[4] = saved_mats->mat[DRW_MAT_PERSINV];
-  float(*vp_projmat)[4] = saved_mats->mat[DRW_MAT_WIN];
-  bool is_persp = DRW_viewport_is_persp_get();
+  float persinv[4][4], vp_projmat[4][4];
+  DRW_view_persmat_get(view, persinv, true);
+  DRW_view_winmat_get(view, vp_projmat, false);
+  bool is_persp = DRW_view_is_persp_get(view);
 
   /* Lights Matrices */
   int cascade_nbr = la->cascade_count;
@@ -1286,19 +1287,52 @@ void EEVEE_lights_update(EEVEE_ViewLayerData *sldata, EEVEE_Data *vedata)
   }
 }
 
+static void eevee_ensure_cube_views(float near, float far, const float pos[3], DRWView *view[6])
+{
+  float winmat[4][4], viewmat[4][4];
+  perspective_m4(winmat, -near, near, -near, near, near, far);
+
+  for (int i = 0; i < 6; i++) {
+    unit_m4(viewmat);
+    negate_v3_v3(viewmat[3], pos);
+    mul_m4_m4m4(viewmat, cubefacemat[i], viewmat);
+
+    if (view[i] == NULL) {
+      view[i] = DRW_view_create(viewmat, winmat, NULL, NULL, NULL);
+    }
+    else {
+      DRW_view_update(view[i], viewmat, winmat, NULL, NULL);
+    }
+  }
+}
+
+static void eevee_ensure_cascade_views(EEVEE_ShadowCascadeData *cascade_data,
+                                       int cascade_count,
+                                       DRWView *view[4])
+{
+  for (int i = 0; i < cascade_count; i++) {
+    if (view[i] == NULL) {
+      view[i] = DRW_view_create(cascade_data->viewmat, cascade_data->projmat[i], NULL, NULL, NULL);
+    }
+    else {
+      DRW_view_update(view[i], cascade_data->viewmat, cascade_data->projmat[i], NULL, NULL);
+    }
+  }
+}
+
 /* this refresh lights shadow buffers */
-void EEVEE_draw_shadows(EEVEE_ViewLayerData *sldata, EEVEE_Data *vedata)
+void EEVEE_draw_shadows(EEVEE_ViewLayerData *sldata, EEVEE_Data *vedata, DRWView *view)
 {
   EEVEE_PassList *psl = vedata->psl;
   EEVEE_StorageList *stl = vedata->stl;
   EEVEE_EffectsInfo *effects = stl->effects;
+  EEVEE_PrivateData *g_data = stl->g_data;
   EEVEE_LightsInfo *linfo = sldata->lights;
   const DRWContextState *draw_ctx = DRW_context_state_get();
   const float light_threshold = draw_ctx->scene->eevee.light_threshold;
   Object *ob;
   int i;
 
-  DRWMatrixState saved_mats;
   int saved_ray_type = sldata->common_data.ray_type;
 
   /* TODO: make it optionnal if we don't draw shadows. */
@@ -1328,9 +1362,6 @@ void EEVEE_draw_shadows(EEVEE_ViewLayerData *sldata, EEVEE_Data *vedata)
     cascade_visible[i] = DRW_culling_plane_test(plane);
   }
 
-  /* We need to save the Matrices before overidding them */
-  DRW_viewport_matrix_get_all(&saved_mats);
-
   /* Cube Shadow Maps */
   DRW_stats_group_start("Cube Shadow Maps");
   /* Render each shadow to one layer of the array */
@@ -1342,11 +1373,6 @@ void EEVEE_draw_shadows(EEVEE_ViewLayerData *sldata, EEVEE_Data *vedata)
       continue;
     }
 
-    DRWMatrixState render_mats;
-    float(*winmat)[4] = render_mats.mat[DRW_MAT_WIN];
-    float(*viewmat)[4] = render_mats.mat[DRW_MAT_VIEW];
-    float(*persmat)[4] = render_mats.mat[DRW_MAT_PERS];
-
     EEVEE_ShadowRender *srd = &linfo->shadow_render_data;
     EEVEE_ShadowCubeData *evscd = &led->data.scd;
     EEVEE_ShadowCube *cube_data = linfo->shadow_cube_data + evscd->cube_id;
@@ -1357,32 +1383,16 @@ void EEVEE_draw_shadows(EEVEE_ViewLayerData *sldata, EEVEE_Data *vedata)
     srd->exponent = la->bleedexp;
     copy_v3_v3(srd->position, cube_data->position);
 
-    perspective_m4(winmat,
-                   -srd->clip_near,
-                   srd->clip_near,
-                   -srd->clip_near,
-                   srd->clip_near,
-                   srd->clip_near,
-                   srd->clip_far);
-
     DRW_uniformbuffer_update(sldata->shadow_render_ubo, srd);
 
+    eevee_ensure_cube_views(srd->clip_near, srd->clip_far, srd->position, g_data->cube_views);
+
     /* Render shadow cube */
     /* Render 6 faces separately: seems to be faster for the general case.
      * The only time it's more beneficial is when the CPU culling overhead
      * outweigh the instancing overhead. which is rarely the case. */
     for (int j = 0; j < 6; j++) {
-      /* TODO optimize */
-      float tmp[4][4];
-      unit_m4(tmp);
-      negate_v3_v3(tmp[3], srd->position);
-      mul_m4_m4m4(viewmat, cubefacemat[j], tmp);
-      mul_m4_m4m4(persmat, winmat, viewmat);
-      invert_m4_m4(render_mats.mat[DRW_MAT_WININV], winmat);
-      invert_m4_m4(render_mats.mat[DRW_MAT_VIEWINV], viewmat);
-      invert_m4_m4(render_mats.mat[DRW_MAT_PERSINV], persmat);
-
-      DRW_viewport_matrix_override_set_all(&render_mats);
+      DRW_view_set_active(g_data->cube_views[j]);
 
       GPU_framebuffer_texture_cubeface_attach(
           sldata->shadow_cube_target_fb, sldata->shadow_cube_target, 0, j, 0);
@@ -1448,9 +1458,8 @@ void EEVEE_draw_shadows(EEVEE_ViewLayerData *sldata, EEVEE_Data *vedata)
   linfo->update_flag &= ~LIGHT_UPDATE_SHADOW_CUBE;
   DRW_stats_group_end();
 
-  DRW_viewport_matrix_override_set_all(&saved_mats);
-  float near = DRW_viewport_near_distance_get();
-  float far = DRW_viewport_far_distance_get();
+  float near = DRW_view_near_distance_get(view);
+  float far = DRW_view_far_distance_get(view);
 
   /* Cascaded Shadow Maps */
   DRW_stats_group_start("Cascaded Shadow Maps");
@@ -1465,34 +1474,24 @@ void EEVEE_draw_shadows(EEVEE_ViewLayerData *sldata, EEVEE_Data *vedata)
     EEVEE_ShadowCascadeData *evscd = &led->data.scad;
     EEVEE_ShadowRender *srd = &linfo->shadow_render_data;
 
-    DRWMatrixState render_mats;
-    float(*winmat)[4] = render_mats.mat[DRW_MAT_WIN];
-    float(*viewmat)[4] = render_mats.mat[DRW_MAT_VIEW];
-    float(*persmat)[4] = render_mats.mat[DRW_MAT_PERS];
-
-    eevee_shadow_cascade_setup(
-        ob, linfo, led, &saved_mats, near, far, effects->taa_current_sample - 1);
-
     srd->clip_near = la->clipsta;
     srd->clip_far = la->clipend;
     srd->stored_texel_size = 1.0 / (float)linfo->shadow_cascade_size;
 
     DRW_uniformbuffer_update(sldata->shadow_render_ubo, &linfo->shadow_render_data);
 
-    copy_m4_m4(viewmat, evscd->viewmat);
-    invert_m4_m4(render_mats.mat[DRW_MAT_VIEWINV], viewmat);
+    eevee_shadow_cascade_setup(ob, linfo, led, view, near, far, effects->taa_current_sample - 1);
+
+    /* Meh, Reusing the cube views. */
+    BLI_assert(MAX_CASCADE_NUM <= 6);
+    eevee_ensure_cascade_views(evscd, la->cascade_count, g_data->cube_views);
 
     /* Render shadow cascades */
     /* Render cascade separately: seems to be faster for the general case.
      * The only time it's more beneficial is when the CPU culling overhead
      * outweigh the instancing overhead. which is rarely the case. */
     for (int j = 0; j < la->cascade_count; j++) {
-      copy_m4_m4(winmat, evscd->projmat[j]);
-      copy_m4_m4(persmat, evscd->viewprojmat[j]);
-      invert_m4_m4(render_mats.mat[DRW_MAT_WININV], winmat);
-      invert_m4_m4(render_mats.mat[DRW_MAT_PERSINV], persmat);
-
-      DRW_viewport_matrix_override_set_all(&render_mats);
+      DRW_view_set_active(g_data->cube_views[j]);
 
       GPU_framebuffer_texture_layer_attach(
           sldata->shadow_cascade_target_fb, sldata->shadow_cascade_target, 0, j, 0);
@@ -1554,7 +1553,7 @@ void EEVEE_draw_shadows(EEVEE_ViewLayerData *sldata, EEVEE_Data *vedata)
 
   DRW_stats_group_end();
 
-  DRW_viewport_matrix_override_set_all(&saved_mats);
+  DRW_view_set_active(view);
 
   DRW_uniformbuffer_update(sldata->light_ubo, &linfo->light_data);
   DRW_uniformbuffer_update(sldata->shadow_ubo, &linfo->shadow_data); /* Update all data at once */
index 277caf8..a998bd3 100644 (file)
@@ -223,22 +223,28 @@ void EEVEE_lookdev_draw(EEVEE_Data *vedata)
     DRW_uniformbuffer_update(sldata->common_ubo, common);
 
     /* override matrices */
-    DRWMatrixState matstate;
-    unit_m4(matstate.winmat);
+    float winmat[4][4], viewmat[4][4];
+    unit_m4(winmat);
     /* Look through the negative Z. */
-    negate_v3(matstate.winmat[2]);
+    negate_v3(winmat[2]);
 
-    eevee_lookdev_apply_taa(effects, effects->sphere_size, matstate.winmat);
+    eevee_lookdev_apply_taa(effects, effects->sphere_size, winmat);
 
     /* "Remove" view matrix location. Leaving only rotation. */
-    DRW_viewport_matrix_get(matstate.viewmat, DRW_MAT_VIEW);
-    zero_v3(matstate.viewmat[3]);
-    mul_m4_m4m4(matstate.persmat, matstate.winmat, matstate.viewmat);
-    invert_m4_m4(matstate.wininv, matstate.winmat);
-    invert_m4_m4(matstate.viewinv, matstate.viewmat);
-    invert_m4_m4(matstate.persinv, matstate.persmat);
+    DRW_view_viewmat_get(NULL, viewmat, false);
+    zero_v3(viewmat[3]);
 
-    DRW_viewport_matrix_override_set_all(&matstate);
+    if (effects->lookdev_view) {
+      /* When rendering just update the view. This avoids recomputing the culling. */
+      DRW_view_update_sub(effects->lookdev_view, viewmat, winmat);
+    }
+    else {
+      /* Using default view bypasses the culling. */
+      const DRWView *default_view = DRW_view_default_get();
+      effects->lookdev_view = DRW_view_create_sub(default_view, viewmat, winmat);
+    }
+
+    DRW_view_set_active(effects->lookdev_view);
 
     /* Find the right framebuffers to render to. */
     GPUFrameBuffer *fb = (effects->target_buffer == fbl->effect_color_fb) ? fbl->main_fb :
@@ -272,6 +278,6 @@ void EEVEE_lookdev_draw(EEVEE_Data *vedata)
 
     DRW_stats_group_end();
 
-    DRW_viewport_matrix_override_unset_all();
+    DRW_view_set_active(NULL);
   }
 }
index 6e0bd7f..45b0077 100644 (file)
@@ -642,8 +642,8 @@ void EEVEE_materials_init(EEVEE_ViewLayerData *sldata,
   {
     /* Update view_vecs */
     float invproj[4][4], winmat[4][4];
-    DRW_viewport_matrix_get(winmat, DRW_MAT_WIN);
-    DRW_viewport_matrix_get(invproj, DRW_MAT_WININV);
+    DRW_view_winmat_get(NULL, winmat, false);
+    DRW_view_winmat_get(NULL, invproj, true);
 
     EEVEE_update_viewvecs(invproj, winmat, sldata->common_data.view_vecs);
   }
index ad4f72d..abd5bb8 100644 (file)
@@ -127,7 +127,7 @@ int EEVEE_motion_blur_init(EEVEE_ViewLayerData *UNUSED(sldata), EEVEE_Data *veda
 
       /* Viewport Matrix */
       /* Note: This does not have TAA jitter applied. */
-      DRW_viewport_matrix_get(persmat, DRW_MAT_PERS);
+      DRW_view_persmat_get(NULL, persmat, false);
 
       bool view_is_valid = (stl->g_data->view_updated == false);
 
index e213061..c64f489 100644 (file)
@@ -571,10 +571,7 @@ typedef struct EEVEE_EffectsInfo {
   float taa_alpha;
   bool prev_drw_support;
   float prev_drw_persmat[4][4];
-  float overide_persmat[4][4];
-  float overide_persinv[4][4];
-  float overide_winmat[4][4];
-  float overide_wininv[4][4];
+  struct DRWView *taa_view;
   /* Ambient Occlusion */
   int ao_depth_layer;
   struct GPUTexture *ao_src_depth;  /* pointer copy */
@@ -604,11 +601,13 @@ typedef struct EEVEE_EffectsInfo {
   /* Alpha Checker */
   float color_checker_dark[4];
   float color_checker_light[4];
+  struct DRWView *checker_view;
   /* Other */
   float prev_persmat[4][4];
   /* Lookdev */
   int sphere_size;
   int anchor[2];
+  struct DRWView *lookdev_view;
   /* Bloom */
   int bloom_iteration_len;
   float source_texel_size[2];
@@ -754,7 +753,7 @@ typedef struct EEVEE_ShadowCubeData {
 typedef struct EEVEE_ShadowCascadeData {
   short light_id, shadow_id, cascade_id, layer_id;
   /* World->Light->NDC : used for rendering the shadow map. */
-  float viewprojmat[MAX_CASCADE_NUM][4][4];
+  float viewprojmat[MAX_CASCADE_NUM][4][4]; /* Could be removed. */
   float projmat[MAX_CASCADE_NUM][4][4];
   float viewmat[4][4], viewinv[4][4];
   float radius[MAX_CASCADE_NUM];
@@ -829,9 +828,6 @@ typedef struct EEVEE_PrivateData {
   bool valid_double_buffer;
   bool valid_taa_history;
   /* Render Matrices */
-  float persmat[4][4], persinv[4][4];
-  float viewmat[4][4], viewinv[4][4];
-  float winmat[4][4], wininv[4][4];
   float studiolight_matrix[3][3];
   float overscan, overscan_pixels;
   float size_orig[2];
@@ -849,6 +845,9 @@ typedef struct EEVEE_PrivateData {
   float studiolight_glossy_clamp;
   float studiolight_filter_quality;
 
+  /** For rendering probes and shadows. */
+  struct DRWView *cube_views[6];
+  struct DRWView *planar_views[MAX_PLANAR];
 } EEVEE_PrivateData; /* Transient data */
 
 /* eevee_data.c */
@@ -919,7 +918,7 @@ void EEVEE_lights_cache_shcaster_material_add(EEVEE_ViewLayerData *sldata,
 void EEVEE_lights_cache_shcaster_object_add(EEVEE_ViewLayerData *sldata, struct Object *ob);
 void EEVEE_lights_cache_finish(EEVEE_ViewLayerData *sldata, EEVEE_Data *vedata);
 void EEVEE_lights_update(EEVEE_ViewLayerData *sldata, EEVEE_Data *vedata);
-void EEVEE_draw_shadows(EEVEE_ViewLayerData *sldata, EEVEE_Data *vedata);
+void EEVEE_draw_shadows(EEVEE_ViewLayerData *sldata, EEVEE_Data *vedata, struct DRWView *view);
 void EEVEE_lights_free(void);
 
 /* eevee_shaders.c */
@@ -1055,10 +1054,7 @@ int EEVEE_temporal_sampling_init(EEVEE_ViewLayerData *sldata, EEVEE_Data *vedata
 void EEVEE_temporal_sampling_offset_calc(const double ht_point[2],
                                          const float filter_size,
                                          float r_offset[2]);
-void EEVEE_temporal_sampling_matrices_calc(EEVEE_EffectsInfo *effects,
-                                           float viewmat[4][4],
-                                           float persmat[4][4],
-                                           const double ht_point[2]);
+void EEVEE_temporal_sampling_matrices_calc(EEVEE_EffectsInfo *effects, const double ht_point[2]);
 void EEVEE_temporal_sampling_update_matrices(EEVEE_Data *vedata);
 void EEVEE_temporal_sampling_cache_init(EEVEE_ViewLayerData *sldata, EEVEE_Data *vedata);
 void EEVEE_temporal_sampling_draw(EEVEE_Data *vedata);
index 8e3857a..e083d16 100644 (file)
@@ -105,25 +105,20 @@ void EEVEE_render_init(EEVEE_Data *ved, RenderEngine *engine, struct Depsgraph *
   }
 
   /* Set the pers & view matrix. */
+  float winmat[4][4], viewmat[4][4], viewinv[4][4];
   /* TODO(sergey): Shall render hold pointer to an evaluated camera instead? */
   struct Object *ob_camera_eval = DEG_get_evaluated_object(depsgraph, RE_GetCamera(engine->re));
   float frame = BKE_scene_frame_get(scene);
-  RE_GetCameraWindow(engine->re, ob_camera_eval, frame, g_data->winmat);
-  RE_GetCameraModelMatrix(engine->re, ob_camera_eval, g_data->viewinv);
 
-  RE_GetCameraWindowWithOverscan(engine->re, g_data->winmat, g_data->overscan);
+  RE_GetCameraWindow(engine->re, ob_camera_eval, frame, winmat);
+  RE_GetCameraWindowWithOverscan(engine->re, winmat, g_data->overscan);
+  RE_GetCameraModelMatrix(engine->re, ob_camera_eval, viewinv);
 
-  invert_m4_m4(g_data->viewmat, g_data->viewinv);
-  mul_m4_m4m4(g_data->persmat, g_data->winmat, g_data->viewmat);
-  invert_m4_m4(g_data->persinv, g_data->persmat);
-  invert_m4_m4(g_data->wininv, g_data->winmat);
+  invert_m4_m4(viewmat, viewinv);
 
-  DRW_viewport_matrix_override_set(g_data->persmat, DRW_MAT_PERS);
-  DRW_viewport_matrix_override_set(g_data->persinv, DRW_MAT_PERSINV);
-  DRW_viewport_matrix_override_set(g_data->winmat, DRW_MAT_WIN);
-  DRW_viewport_matrix_override_set(g_data->wininv, DRW_MAT_WININV);
-  DRW_viewport_matrix_override_set(g_data->viewmat, DRW_MAT_VIEW);
-  DRW_viewport_matrix_override_set(g_data->viewinv, DRW_MAT_VIEWINV);
+  DRWView *view = DRW_view_create(viewmat, winmat, NULL, NULL, NULL);
+  DRW_view_default_set(view);
+  DRW_view_set_active(view);
 
   /* EEVEE_effects_init needs to go first for TAA */
   EEVEE_effects_init(sldata, vedata, ob_camera_eval, false);
@@ -304,6 +299,9 @@ static void eevee_render_result_normal(RenderLayer *rl,
                                1,
                                rp->rect);
 
+    float viewinv[4][4];
+    DRW_view_viewmat_get(NULL, viewinv, true);
+
     /* Convert Eevee encoded normals to Blender normals. */
     for (int i = 0; i < rp->rectx * rp->recty * 3; i += 3) {
       if (rp->rect[i] == 0.0f && rp->rect[i + 1] == 0.0f) {
@@ -322,7 +320,7 @@ static void eevee_render_result_normal(RenderLayer *rl,
       rp->rect[i + 1] = fenc[1] * g;
       rp->rect[i + 2] = 1.0f - f / 2.0f;
 
-      mul_mat3_m4_v3(g_data->viewinv, &rp->rect[i]);
+      mul_mat3_m4_v3(viewinv, &rp->rect[i]);
     }
   }
 }
@@ -357,6 +355,9 @@ static void eevee_render_result_z(RenderLayer *rl,
 
     bool is_persp = DRW_viewport_is_persp_get();
 
+    float viewmat[4][4];
+    DRW_view_viewmat_get(NULL, viewmat, false);
+
     /* Convert ogl depth [0..1] to view Z [near..far] */
     for (int i = 0; i < rp->rectx * rp->recty; ++i) {
       if (rp->rect[i] == 1.0f) {
@@ -365,7 +366,7 @@ static void eevee_render_result_z(RenderLayer *rl,
       else {
         if (is_persp) {
           rp->rect[i] = rp->rect[i] * 2.0f - 1.0f;
-          rp->rect[i] = g_data->winmat[3][2] / (rp->rect[i] + g_data->winmat[2][2]);
+          rp->rect[i] = viewmat[3][2] / (rp->rect[i] + viewmat[2][2]);
         }
         else {
           rp->rect[i] = -common_data->view_vecs[0][2] +
@@ -533,24 +534,17 @@ void EEVEE_render_draw(EEVEE_Data *vedata, RenderEngine *engine, RenderLayer *rl
     double offset[3] = {0.0, 0.0, 0.0};
     double r[3];
 
-    /* Restore winmat before jittering again. */
-    copy_m4_m4(stl->effects->overide_winmat, g_data->winmat);
     /* Copy previous persmat to UBO data */
     copy_m4_m4(sldata->common_data.prev_persmat, stl->effects->prev_persmat);
 
     BLI_halton_3d(primes, offset, stl->effects->taa_current_sample, r);
     EEVEE_update_noise(psl, fbl, r);
-    EEVEE_temporal_sampling_matrices_calc(stl->effects, g_data->viewmat, g_data->persmat, r);
+    EEVEE_temporal_sampling_matrices_calc(stl->effects, r);
     EEVEE_volumes_set_jitter(sldata, stl->effects->taa_current_sample - 1);
     EEVEE_materials_init(sldata, stl, fbl);
 
     /* Set matrices. */
-    DRW_viewport_matrix_override_set(stl->effects->overide_persmat, DRW_MAT_PERS);
-    DRW_viewport_matrix_override_set(stl->effects->overide_persinv, DRW_MAT_PERSINV);
-    DRW_viewport_matrix_override_set(stl->effects->overide_winmat, DRW_MAT_WIN);
-    DRW_viewport_matrix_override_set(stl->effects->overide_wininv, DRW_MAT_WININV);
-    DRW_viewport_matrix_override_set(g_data->viewmat, DRW_MAT_VIEW);
-    DRW_viewport_matrix_override_set(g_data->viewinv, DRW_MAT_VIEWINV);
+    DRW_view_set_active(stl->effects->taa_view);
 
     /* Refresh Probes */
     EEVEE_lightprobes_refresh(sldata, vedata);
@@ -566,7 +560,7 @@ void EEVEE_render_draw(EEVEE_Data *vedata, RenderEngine *engine, RenderLayer *rl
 
     /* Refresh Shadows */
     EEVEE_lights_update(sldata, vedata);
-    EEVEE_draw_shadows(sldata, vedata);
+    EEVEE_draw_shadows(sldata, vedata, stl->effects->taa_view);
 
     /* Set ray type. */
     sldata->common_data.ray_type = EEVEE_RAY_CAMERA;
index a488e16..099147a 100644 (file)
@@ -139,25 +139,27 @@ void EEVEE_temporal_sampling_offset_calc(const double ht_point[2],
   r_offset[1] = eval_table(e_data.inverted_cdf, (float)(ht_point[1])) * filter_size;
 }
 
-void EEVEE_temporal_sampling_matrices_calc(EEVEE_EffectsInfo *effects,
-                                           float viewmat[4][4],
-                                           float persmat[4][4],
-                                           const double ht_point[2])
+void EEVEE_temporal_sampling_matrices_calc(EEVEE_EffectsInfo *effects, const double ht_point[2])
 {
   const float *viewport_size = DRW_viewport_size_get();
   const DRWContextState *draw_ctx = DRW_context_state_get();
   Scene *scene = draw_ctx->scene;
   RenderData *rd = &scene->r;
 
+  float persmat[4][4], viewmat[4][4], winmat[4][4];
+  DRW_view_persmat_get(NULL, persmat, false);
+  DRW_view_viewmat_get(NULL, viewmat, false);
+  DRW_view_winmat_get(NULL, winmat, false);
+
   float ofs[2];
   EEVEE_temporal_sampling_offset_calc(ht_point, rd->gauss, ofs);
 
-  window_translate_m4(
-      effects->overide_winmat, persmat, ofs[0] / viewport_size[0], ofs[1] / viewport_size[1]);
+  window_translate_m4(winmat, persmat, ofs[0] / viewport_size[0], ofs[1] / viewport_size[1]);
+
+  BLI_assert(effects->taa_view != NULL);
 
-  mul_m4_m4m4(effects->overide_persmat, effects->overide_winmat, viewmat);
-  invert_m4_m4(effects->overide_persinv, effects->overide_persmat);
-  invert_m4_m4(effects->overide_wininv, effects->overide_winmat);
+  /* When rendering just update the view. This avoids recomputing the culling. */
+  DRW_view_update_sub(effects->taa_view, viewmat, winmat);
 }
 
 /* Update the matrices based on the current sample.
@@ -167,23 +169,15 @@ void EEVEE_temporal_sampling_update_matrices(EEVEE_Data *vedata)
   EEVEE_StorageList *stl = ((EEVEE_Data *)vedata)->stl;
   EEVEE_EffectsInfo *effects = stl->effects;
 
-  float persmat[4][4], viewmat[4][4];
   double ht_point[2];
   double ht_offset[2] = {0.0, 0.0};
   uint ht_primes[2] = {2, 3};
 
-  DRW_viewport_matrix_get(persmat, DRW_MAT_PERS);
-  DRW_viewport_matrix_get(viewmat, DRW_MAT_VIEW);
-  DRW_viewport_matrix_get(effects->overide_winmat, DRW_MAT_WIN);
-
   BLI_halton_2d(ht_primes, ht_offset, effects->taa_current_sample - 1, ht_point);
 
-  EEVEE_temporal_sampling_matrices_calc(effects, viewmat, persmat, ht_point);
+  EEVEE_temporal_sampling_matrices_calc(effects, ht_point);
 
-  DRW_viewport_matrix_override_set(effects->overide_persmat, DRW_MAT_PERS);
-  DRW_viewport_matrix_override_set(effects->overide_persinv, DRW_MAT_PERSINV);
-  DRW_viewport_matrix_override_set(effects->overide_winmat, DRW_MAT_WIN);
-  DRW_viewport_matrix_override_set(effects->overide_wininv, DRW_MAT_WININV);
+  DRW_view_set_active(effects->taa_view);
 }
 
 void EEVEE_temporal_sampling_reset(EEVEE_Data *vedata)
@@ -195,8 +189,6 @@ void EEVEE_temporal_sampling_reset(EEVEE_Data *vedata)
 int EEVEE_temporal_sampling_init(EEVEE_ViewLayerData *UNUSED(sldata), EEVEE_Data *vedata)
 {
   EEVEE_StorageList *stl = vedata->stl;
-  // EEVEE_FramebufferList *fbl = vedata->fbl;
-  // EEVEE_TextureList *txl = vedata->txl;
   EEVEE_EffectsInfo *effects = stl->effects;
   int repro_flag = 0;
 
@@ -204,17 +196,26 @@ int EEVEE_temporal_sampling_init(EEVEE_ViewLayerData *UNUSED(sldata), EEVEE_Data
     eevee_create_cdf_table_temporal_sampling();
   }
 
-  /* Reset for each "redraw". When rendering using ogl render,
+  /**
+   * Reset for each "redraw". When rendering using ogl render,
    * we accumulate the redraw inside the drawing loop in eevee_draw_background().
-   * But we do NOT accumulate between "redraw" (as in full draw manager drawloop)
-   * because the opengl render already does that. */
+   **/
   effects->taa_render_sample = 1;
+  effects->taa_view = NULL;
+
+  /* Create a sub view to disable clipping planes (if any). */
+  const DRWView *default_view = DRW_view_default_get();
+  float viewmat[4][4], winmat[4][4];
+  DRW_view_viewmat_get(default_view, viewmat, false);
+  DRW_view_winmat_get(default_view, winmat, false);
+  effects->taa_view = DRW_view_create_sub(default_view, viewmat, winmat);
+  DRW_view_clip_planes_set(effects->taa_view, NULL, 0);
 
   const DRWContextState *draw_ctx = DRW_context_state_get();
   const Scene *scene_eval = DEG_get_evaluated_scene(draw_ctx->depsgraph);
 
   if ((scene_eval->eevee.taa_samples != 1) || DRW_state_is_image_render()) {
-    float persmat[4][4], viewmat[4][4];
+    float persmat[4][4];
 
     if (!DRW_state_is_image_render() && (scene_eval->eevee.flag & SCE_EEVEE_TAA_REPROJECTION)) {
       repro_flag = EFFECT_TAA_REPROJECT | EFFECT_VELOCITY_BUFFER | EFFECT_DEPTH_DOUBLE_BUFFER |
@@ -236,8 +237,7 @@ int EEVEE_temporal_sampling_init(EEVEE_ViewLayerData *UNUSED(sldata), EEVEE_Data
     effects->taa_total_sample = scene_eval->eevee.taa_samples;
     MAX2(effects->taa_total_sample, 0);
 
-    DRW_viewport_matrix_get(persmat, DRW_MAT_PERS);
-    DRW_viewport_matrix_get(viewmat, DRW_MAT_VIEW);
+    DRW_view_persmat_get(NULL, persmat, false);
     view_is_valid = view_is_valid && compare_m4m4(persmat, effects->prev_drw_persmat, FLT_MIN);
     copy_m4_m4(effects->prev_drw_persmat, persmat);
 
index 9a59b95..e4d95ee 100644 (file)
@@ -242,8 +242,8 @@ void EEVEE_volumes_init(EEVEE_ViewLayerData *sldata, EEVEE_Data *vedata)
 
   /* Update view_vecs */
   float invproj[4][4], winmat[4][4];
-  DRW_viewport_matrix_get(winmat, DRW_MAT_WIN);
-  DRW_viewport_matrix_get(invproj, DRW_MAT_WININV);
+  DRW_view_winmat_get(NULL, winmat, false);
+  DRW_view_winmat_get(NULL, invproj, true);
   EEVEE_update_viewvecs(invproj, winmat, sldata->common_data.view_vecs);
 
   if (DRW_viewport_is_persp_get()) {