Eevee: HiZ buffer: Split into two 24bit depth buffer
authorClément Foucault <foucault.clem@gmail.com>
Fri, 21 Jul 2017 12:27:16 +0000 (14:27 +0200)
committerClément Foucault <foucault.clem@gmail.com>
Mon, 24 Jul 2017 13:28:27 +0000 (15:28 +0200)
This way we don't have float precision issue we had before and we save some bandwidth.

source/blender/draw/engines/eevee/eevee_effects.c
source/blender/draw/engines/eevee/eevee_lightprobes.c
source/blender/draw/engines/eevee/eevee_materials.c
source/blender/draw/engines/eevee/eevee_private.h
source/blender/draw/engines/eevee/shaders/ambient_occlusion_lib.glsl
source/blender/draw/engines/eevee/shaders/effect_minmaxz_frag.glsl
source/blender/gpu/intern/gpu_framebuffer.c

index 9e221210d6e51b465f33a196108c5849d8cf84dd..af3d073a6cf7ae1ab984127fbbf16c083591acfb 100644 (file)
@@ -34,6 +34,7 @@
 #include "DNA_view3d_types.h"
 #include "DNA_world_types.h"
 
+#include "BKE_global.h" /* for G.debug_value */
 #include "BKE_camera.h"
 #include "BKE_object.h"
 #include "BKE_animsys.h"
@@ -59,9 +60,12 @@ typedef struct EEVEE_LightProbeData {
 
 static struct {
        /* Downsample Depth */
-       struct GPUShader *minmaxz_downlevel_sh;
-       struct GPUShader *minmaxz_downdepth_sh;
-       struct GPUShader *minmaxz_copydepth_sh;
+       struct GPUShader *minz_downlevel_sh;
+       struct GPUShader *maxz_downlevel_sh;
+       struct GPUShader *minz_downdepth_sh;
+       struct GPUShader *maxz_downdepth_sh;
+       struct GPUShader *minz_copydepth_sh;
+       struct GPUShader *maxz_copydepth_sh;
 
        /* Motion Blur */
        struct GPUShader *motion_blur_sh;
@@ -84,6 +88,7 @@ static struct {
        struct GPUShader *ssr_raytrace_sh;
        struct GPUShader *ssr_resolve_sh;
 
+       /* Simple Downsample */
        struct GPUShader *downsample_sh;
 
        struct GPUTexture *depth_src;
@@ -200,10 +205,18 @@ void EEVEE_effects_init(EEVEE_SceneLayerData *sldata, EEVEE_Data *vedata)
 
                e_data.volumetric_upsample_sh = DRW_shader_create_fullscreen(datatoc_volumetric_frag_glsl, "#define STEP_UPSAMPLE\n");
 
-               e_data.minmaxz_downlevel_sh = DRW_shader_create_fullscreen(datatoc_effect_minmaxz_frag_glsl, NULL);
-               e_data.minmaxz_downdepth_sh = DRW_shader_create_fullscreen(datatoc_effect_minmaxz_frag_glsl, "#define INPUT_DEPTH\n");
-               e_data.minmaxz_copydepth_sh = DRW_shader_create_fullscreen(datatoc_effect_minmaxz_frag_glsl, "#define INPUT_DEPTH\n"
-                                                                                                            "#define COPY_DEPTH\n");
+               e_data.minz_downlevel_sh = DRW_shader_create_fullscreen(datatoc_effect_minmaxz_frag_glsl, "#define MIN_PASS\n");
+               e_data.maxz_downlevel_sh = DRW_shader_create_fullscreen(datatoc_effect_minmaxz_frag_glsl, "#define MAX_PASS\n");
+               e_data.minz_downdepth_sh = DRW_shader_create_fullscreen(datatoc_effect_minmaxz_frag_glsl, "#define MIN_PASS\n"
+                                                                                                         "#define INPUT_DEPTH\n");
+               e_data.maxz_downdepth_sh = DRW_shader_create_fullscreen(datatoc_effect_minmaxz_frag_glsl, "#define MAX_PASS\n"
+                                                                                                         "#define INPUT_DEPTH\n");
+               e_data.minz_copydepth_sh = DRW_shader_create_fullscreen(datatoc_effect_minmaxz_frag_glsl, "#define MIN_PASS\n"
+                                                                                                         "#define INPUT_DEPTH\n"
+                                                                                                         "#define COPY_DEPTH\n");
+               e_data.maxz_copydepth_sh = DRW_shader_create_fullscreen(datatoc_effect_minmaxz_frag_glsl, "#define MAX_PASS\n"
+                                                                                                         "#define INPUT_DEPTH\n"
+                                                                                                         "#define COPY_DEPTH\n");
 
                e_data.motion_blur_sh = DRW_shader_create_fullscreen(datatoc_effect_motion_blur_frag_glsl, NULL);
 
@@ -440,12 +453,16 @@ void EEVEE_effects_init(EEVEE_SceneLayerData *sldata, EEVEE_Data *vedata)
        }
 
        /* MinMax Pyramid */
-       /* TODO reduce precision */
-       DRWFboTexture tex = {&stl->g_data->minmaxz, DRW_TEX_RG_32, DRW_TEX_MIPMAP | DRW_TEX_TEMP};
+       DRWFboTexture texmin = {&stl->g_data->minzbuffer, DRW_TEX_DEPTH_24, DRW_TEX_MIPMAP | DRW_TEX_TEMP};
 
-       DRW_framebuffer_init(&fbl->minmaxz_fb, &draw_engine_eevee_type,
+       DRW_framebuffer_init(&fbl->downsample_fb, &draw_engine_eevee_type,
                            (int)viewport_size[0] / 2, (int)viewport_size[1] / 2,
-                           &tex, 1);
+                           &texmin, 1);
+
+       /* Cannot define 2 depth texture for one framebuffer. So allocate ourself. */
+       if (txl->maxzbuffer == NULL) {
+               txl->maxzbuffer = DRW_texture_create_2D((int)viewport_size[0] / 2, (int)viewport_size[1] / 2, DRW_TEX_DEPTH_24, DRW_TEX_MIPMAP, NULL);
+       }
 
        if (BKE_collection_engine_property_value_get_bool(props, "volumetric_enable")) {
                World *wo = scene->world;
@@ -724,18 +741,36 @@ void EEVEE_effects_cache_init(EEVEE_SceneLayerData *sldata, EEVEE_Data *vedata)
        }
 
        {
-               psl->minmaxz_downlevel = DRW_pass_create("HiZ Down Level", DRW_STATE_WRITE_COLOR);
-               DRWShadingGroup *grp = DRW_shgroup_create(e_data.minmaxz_downlevel_sh, psl->minmaxz_downlevel);
-               DRW_shgroup_uniform_buffer(grp, "depthBuffer", &stl->g_data->minmaxz);
+               /* Perform min/max downsample */
+               psl->minz_downlevel_ps = DRW_pass_create("HiZ Min Down Level", DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_ALWAYS);
+               DRWShadingGroup *grp = DRW_shgroup_create(e_data.minz_downlevel_sh, psl->minz_downlevel_ps);
+               DRW_shgroup_uniform_buffer(grp, "depthBuffer", &stl->g_data->minzbuffer);
+               DRW_shgroup_call_add(grp, quad, NULL);
+
+               psl->maxz_downlevel_ps = DRW_pass_create("HiZ Max Down Level", DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_ALWAYS);
+               grp = DRW_shgroup_create(e_data.maxz_downlevel_sh, psl->maxz_downlevel_ps);
+               DRW_shgroup_uniform_buffer(grp, "depthBuffer", &txl->maxzbuffer);
                DRW_shgroup_call_add(grp, quad, NULL);
 
-               psl->minmaxz_downdepth = DRW_pass_create("HiZ Down Depth", DRW_STATE_WRITE_COLOR);
-               grp = DRW_shgroup_create(e_data.minmaxz_downdepth_sh, psl->minmaxz_downdepth);
+               /* Copy depth buffer to halfres top level of HiZ */
+               psl->minz_downdepth_ps = DRW_pass_create("HiZ Min Copy Depth Halfres", DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_ALWAYS);
+               grp = DRW_shgroup_create(e_data.minz_downdepth_sh, psl->minz_downdepth_ps);
                DRW_shgroup_uniform_buffer(grp, "depthBuffer", &e_data.depth_src);
                DRW_shgroup_call_add(grp, quad, NULL);
 
-               psl->minmaxz_copydepth = DRW_pass_create("HiZ Copy Depth", DRW_STATE_WRITE_COLOR);
-               grp = DRW_shgroup_create(e_data.minmaxz_copydepth_sh, psl->minmaxz_copydepth);
+               psl->maxz_downdepth_ps = DRW_pass_create("HiZ Max Copy Depth Halfres", DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_ALWAYS);
+               grp = DRW_shgroup_create(e_data.maxz_downdepth_sh, psl->maxz_downdepth_ps);
+               DRW_shgroup_uniform_buffer(grp, "depthBuffer", &e_data.depth_src);
+               DRW_shgroup_call_add(grp, quad, NULL);
+
+               /* Copy depth buffer to halfres top level of HiZ */
+               psl->minz_copydepth_ps = DRW_pass_create("HiZ Min Copy Depth Fullres", DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_ALWAYS);
+               grp = DRW_shgroup_create(e_data.minz_copydepth_sh, psl->minz_copydepth_ps);
+               DRW_shgroup_uniform_buffer(grp, "depthBuffer", &e_data.depth_src);
+               DRW_shgroup_call_add(grp, quad, NULL);
+
+               psl->maxz_copydepth_ps = DRW_pass_create("HiZ Max Copy Depth Fullres", DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_ALWAYS);
+               grp = DRW_shgroup_create(e_data.maxz_copydepth_sh, psl->maxz_copydepth_ps);
                DRW_shgroup_uniform_buffer(grp, "depthBuffer", &e_data.depth_src);
                DRW_shgroup_call_add(grp, quad, NULL);
        }
@@ -840,10 +875,16 @@ void EEVEE_effects_cache_init(EEVEE_SceneLayerData *sldata, EEVEE_Data *vedata)
        }
 }
 
-static void minmax_downsample_cb(void *vedata, int UNUSED(level))
+static void min_downsample_cb(void *vedata, int UNUSED(level))
 {
        EEVEE_PassList *psl = ((EEVEE_Data *)vedata)->psl;
-       DRW_draw_pass(psl->minmaxz_downlevel);
+       DRW_draw_pass(psl->minz_downlevel_ps);
+}
+
+static void max_downsample_cb(void *vedata, int UNUSED(level))
+{
+       EEVEE_PassList *psl = ((EEVEE_Data *)vedata)->psl;
+       DRW_draw_pass(psl->maxz_downlevel_ps);
 }
 
 static void simple_downsample_cb(void *vedata, int UNUSED(level))
@@ -857,17 +898,27 @@ void EEVEE_create_minmax_buffer(EEVEE_Data *vedata, GPUTexture *depth_src)
        EEVEE_PassList *psl = vedata->psl;
        EEVEE_FramebufferList *fbl = vedata->fbl;
        EEVEE_StorageList *stl = vedata->stl;
+       EEVEE_TextureList *txl = vedata->txl;
 
        e_data.depth_src = depth_src;
 
-       /* Copy depth buffer to minmax texture top level */
-       DRW_framebuffer_texture_attach(fbl->minmaxz_fb, stl->g_data->minmaxz, 0, 0);
-       DRW_framebuffer_bind(fbl->minmaxz_fb);
-       DRW_draw_pass(psl->minmaxz_downdepth);
-       DRW_framebuffer_texture_detach(stl->g_data->minmaxz);
+       /* Copy depth buffer to min texture top level */
+       DRW_framebuffer_texture_attach(fbl->downsample_fb, stl->g_data->minzbuffer, 0, 0);
+       DRW_framebuffer_bind(fbl->downsample_fb);
+       DRW_draw_pass(psl->minz_downdepth_ps);
+       DRW_framebuffer_texture_detach(stl->g_data->minzbuffer);
+
+       /* Create lower levels */
+       DRW_framebuffer_recursive_downsample(fbl->downsample_fb, stl->g_data->minzbuffer, 8, &min_downsample_cb, vedata);
+
+       /* Copy depth buffer to max texture top level */
+       DRW_framebuffer_texture_attach(fbl->downsample_fb, txl->maxzbuffer, 0, 0);
+       DRW_framebuffer_bind(fbl->downsample_fb);
+       DRW_draw_pass(psl->maxz_downdepth_ps);
+       DRW_framebuffer_texture_detach(txl->maxzbuffer);
 
        /* Create lower levels */
-       DRW_framebuffer_recursive_downsample(fbl->minmaxz_fb, stl->g_data->minmaxz, 6, &minmax_downsample_cb, vedata);
+       DRW_framebuffer_recursive_downsample(fbl->downsample_fb, txl->maxzbuffer, 8, &max_downsample_cb, vedata);
 }
 
 /**
@@ -933,7 +984,7 @@ void EEVEE_effects_do_ssr(EEVEE_SceneLayerData *UNUSED(sldata), EEVEE_Data *veda
                DefaultTextureList *dtxl = DRW_viewport_texture_list_get();
 
                if (stl->g_data->valid_double_buffer) {
-                       EEVEE_downsample_buffer(vedata, fbl->minmaxz_fb, txl->color_double_buffer, 9);
+                       EEVEE_downsample_buffer(vedata, fbl->downsample_fb, txl->color_double_buffer, 9);
 
                        /* Raytrace at halfres. */
                        e_data.depth_src = dtxl->depth;
@@ -1108,11 +1159,34 @@ void EEVEE_draw_effects(EEVEE_Data *vedata)
        /* If no post processes is enabled, buffers are still not swapped, do it now. */
        SWAP_DOUBLE_BUFFERS();
 
-       if (!stl->g_data->valid_double_buffer) {
+       if (!stl->g_data->valid_double_buffer && ((effects->enabled_effects & EFFECT_DOUBLE_BUFFER) != 0)) {
                /* If history buffer is not valid request another frame.
                 * This fix black reflections on area resize. */
                DRW_viewport_request_redraw();
        }
+
+       /* Debug : Ouput buffer to view. */
+       if ((G.debug_value > 0) && (G.debug_value <= 5)) {
+               switch (G.debug_value) {
+                       case 1:
+                               if (stl->g_data->minzbuffer) DRW_transform_to_display(stl->g_data->minzbuffer);
+                               break;
+                       case 2:
+                               if (txl->ssr_hit_output) DRW_transform_to_display(txl->ssr_hit_output);
+                               break;
+                       case 3:
+                               if (txl->ssr_pdf_output) DRW_transform_to_display(txl->ssr_pdf_output);
+                               break;
+                       case 4:
+                               if (txl->ssr_normal_input) DRW_transform_to_display(txl->ssr_normal_input);
+                               break;
+                       case 5:
+                               if (txl->ssr_specrough_input) DRW_transform_to_display(txl->ssr_specrough_input);
+                               break;
+                       default:
+                               break;
+               }
+       }
 }
 
 void EEVEE_effects_free(void)
@@ -1123,9 +1197,12 @@ void EEVEE_effects_free(void)
 
        DRW_SHADER_FREE_SAFE(e_data.volumetric_upsample_sh);
 
-       DRW_SHADER_FREE_SAFE(e_data.minmaxz_downlevel_sh);
-       DRW_SHADER_FREE_SAFE(e_data.minmaxz_downdepth_sh);
-       DRW_SHADER_FREE_SAFE(e_data.minmaxz_copydepth_sh);
+       DRW_SHADER_FREE_SAFE(e_data.minz_downlevel_sh);
+       DRW_SHADER_FREE_SAFE(e_data.maxz_downlevel_sh);
+       DRW_SHADER_FREE_SAFE(e_data.minz_downdepth_sh);
+       DRW_SHADER_FREE_SAFE(e_data.maxz_downdepth_sh);
+       DRW_SHADER_FREE_SAFE(e_data.minz_copydepth_sh);
+       DRW_SHADER_FREE_SAFE(e_data.maxz_copydepth_sh);
 
        DRW_SHADER_FREE_SAFE(e_data.motion_blur_sh);
        DRW_SHADER_FREE_SAFE(e_data.dof_downsample_sh);
index b2c21278cddff7e6dd48e883bcba91c966878aa4..6356bd8b17f51d7c4de2d93ca5b99b4bd8af64b2 100644 (file)
@@ -64,6 +64,7 @@ static struct {
        struct GPUTexture *planar_depth;
        struct GPUTexture *planar_minmaxz;
        struct GPUTexture *planar_pool_placeholder;
+       struct GPUTexture *depth_placeholder;
        struct GPUTexture *cube_face_depth;
        struct GPUTexture *cube_face_minmaxz;
 
@@ -303,14 +304,17 @@ void EEVEE_lightprobes_init(EEVEE_SceneLayerData *sldata, EEVEE_Data *UNUSED(ved
        }
 
        /* Minmaxz Pyramid */
-       /* Highjacking the minmaxz_fb but that's ok because it's reconfigured before usage. */
        // DRWFboTexture tex_minmaxz = {&e_data.cube_face_minmaxz, DRW_TEX_RG_32, DRW_TEX_MIPMAP | DRW_TEX_TEMP};
-       // DRW_framebuffer_init(&vedata->fbl->minmaxz_fb, &draw_engine_eevee_type, PROBE_RT_SIZE / 2, PROBE_RT_SIZE / 2, &tex_minmaxz, 1);
+       // DRW_framebuffer_init(&vedata->fbl->downsample_fb, &draw_engine_eevee_type, PROBE_RT_SIZE / 2, PROBE_RT_SIZE / 2, &tex_minmaxz, 1);
 
        /* Placeholder planar pool: used when rendering planar reflections (avoid dependency loop). */
        if (!e_data.planar_pool_placeholder) {
                e_data.planar_pool_placeholder = DRW_texture_create_2D_array(1, 1, 1, DRW_TEX_RGBA_8, DRW_TEX_FILTER, NULL);
        }
+
+       if (!e_data.depth_placeholder) {
+               e_data.depth_placeholder = DRW_texture_create_2D(1, 1, DRW_TEX_DEPTH_24, 0, NULL);
+       }
 }
 
 void EEVEE_lightprobes_cache_init(EEVEE_SceneLayerData *sldata, EEVEE_Data *vedata)
@@ -972,9 +976,11 @@ static void render_scene_to_probe(
        /* Avoid using the texture attached to framebuffer when rendering. */
        /* XXX */
        GPUTexture *tmp_planar_pool = txl->planar_pool;
-       GPUTexture *tmp_minmaxz = stl->g_data->minmaxz;
+       GPUTexture *tmp_minz = stl->g_data->minzbuffer;
+       GPUTexture *tmp_maxz = txl->maxzbuffer;
        txl->planar_pool = e_data.planar_pool_placeholder;
-       // stl->g_data->minmaxz = e_data.cube_face_minmaxz;
+       stl->g_data->minzbuffer = e_data.depth_placeholder;
+       txl->maxzbuffer = e_data.depth_placeholder;
 
        /* Detach to rebind the right cubeface. */
        DRW_framebuffer_bind(sldata->probe_fb);
@@ -1030,7 +1036,8 @@ static void render_scene_to_probe(
        /* Restore */
        sldata->probes->specular_toggle = true;
        txl->planar_pool = tmp_planar_pool;
-       stl->g_data->minmaxz = tmp_minmaxz;
+       stl->g_data->minzbuffer = tmp_minz;
+       txl->maxzbuffer = tmp_maxz;
        stl->effects->ao_dist = tmp_ao_dist;
        stl->effects->ao_samples = tmp_ao_samples;
 }
@@ -1061,9 +1068,11 @@ static void render_scene_to_planar(
        /* Avoid using the texture attached to framebuffer when rendering. */
        /* XXX */
        GPUTexture *tmp_planar_pool = txl->planar_pool;
-       GPUTexture *tmp_minmaxz = stl->g_data->minmaxz;
+       GPUTexture *tmp_minz = stl->g_data->minzbuffer;
+       GPUTexture *tmp_maxz = txl->maxzbuffer;
        txl->planar_pool = e_data.planar_pool_placeholder;
-       stl->g_data->minmaxz = e_data.planar_minmaxz;
+       stl->g_data->minzbuffer = e_data.depth_placeholder;
+       txl->maxzbuffer = e_data.depth_placeholder;
        stl->g_data->background_alpha = FLT_MAX; /* Alpha is distance for planar reflections. */
 
        DRW_viewport_matrix_override_set(persmat, DRW_MAT_PERS);
@@ -1097,7 +1106,8 @@ static void render_scene_to_planar(
 
        /* Restore */
        txl->planar_pool = tmp_planar_pool;
-       stl->g_data->minmaxz = tmp_minmaxz;
+       stl->g_data->minzbuffer = tmp_minz;
+       txl->maxzbuffer = tmp_maxz;
        stl->g_data->background_alpha = 1.0;
        DRW_viewport_matrix_override_unset(DRW_MAT_PERS);
        DRW_viewport_matrix_override_unset(DRW_MAT_PERSINV);
@@ -1257,6 +1267,9 @@ void EEVEE_lightprobes_refresh(EEVEE_SceneLayerData *sldata, EEVEE_Data *vedata)
                                                ped->need_update = false;
                                        }
 
+                                       printf("Updated Grid %d : cell %d / %d, bounce %d / %d\n",
+                                               i, ped->updated_cells, ped->num_cell, pinfo->updated_bounce + 1, max_bounce);
+
                                        /* Only do one probe per frame */
                                        DRW_viewport_request_redraw();
                                        goto update_planar;
@@ -1294,6 +1307,8 @@ void EEVEE_lightprobes_refresh(EEVEE_SceneLayerData *sldata, EEVEE_Data *vedata)
                                        ped->ready_to_shade = true;
                                }
 
+                               printf("Update Cubemap %d\n", i);
+
                                DRW_viewport_request_redraw();
 
                                /* Only do one probe per frame */
@@ -1325,7 +1340,7 @@ update_planar:
        /* If there is at least one planar probe */
        if (pinfo->num_planar > 0) {
                const int max_lod = 5;
-               DRW_framebuffer_recursive_downsample(vedata->fbl->minmaxz_fb, txl->planar_pool, max_lod, &downsample_planar, vedata);
+               DRW_framebuffer_recursive_downsample(vedata->fbl->downsample_fb, txl->planar_pool, max_lod, &downsample_planar, vedata);
                /* For shading, save max level of the planar map */
                pinfo->lod_planar_max = (float)(max_lod);
        }
@@ -1342,4 +1357,5 @@ void EEVEE_lightprobes_free(void)
        DRW_SHADER_FREE_SAFE(e_data.probe_cube_display_sh);
        DRW_TEXTURE_FREE_SAFE(e_data.hammersley);
        DRW_TEXTURE_FREE_SAFE(e_data.planar_pool_placeholder);
+       DRW_TEXTURE_FREE_SAFE(e_data.depth_placeholder);
 }
index e7c066404e2bc880c37be779f08eb8c0f478fc49..eae9331fedc5a5465a27d082a7c97e87518317d5 100644 (file)
@@ -283,7 +283,7 @@ static void add_standard_uniforms(DRWShadingGroup *shgrp, EEVEE_SceneLayerData *
        DRW_shgroup_uniform_int(shgrp, "outputSsrId", ssr_id, 1);
        if (vedata->stl->effects->use_ao) {
                DRW_shgroup_uniform_vec4(shgrp, "viewvecs[0]", (float *)&vedata->stl->g_data->viewvecs, 2);
-               DRW_shgroup_uniform_buffer(shgrp, "minMaxDepthTex", &vedata->stl->g_data->minmaxz);
+               DRW_shgroup_uniform_buffer(shgrp, "minMaxDepthTex", &vedata->txl->maxzbuffer);
                DRW_shgroup_uniform_vec3(shgrp, "aoParameters", &vedata->stl->effects->ao_dist, 1);
        }
 }
index f31f4f9fe6c4d57637356b61c3b28db6315af0a2..eec264f1711097ad3b432a1e172d881ca34fb536 100644 (file)
@@ -103,9 +103,6 @@ typedef struct EEVEE_PassList {
        struct DRWPass *dof_down;
        struct DRWPass *dof_scatter;
        struct DRWPass *dof_resolve;
-       struct DRWPass *minmaxz_downlevel;
-       struct DRWPass *minmaxz_downdepth;
-       struct DRWPass *minmaxz_copydepth;
        struct DRWPass *volumetric_integrate_ps;
        struct DRWPass *volumetric_resolve_ps;
        struct DRWPass *volumetric_resolve_transmit_ps;
@@ -113,6 +110,14 @@ typedef struct EEVEE_PassList {
        struct DRWPass *ssr_resolve;
        struct DRWPass *color_downsample_ps;
 
+       /* HiZ */
+       struct DRWPass *minz_downlevel_ps;
+       struct DRWPass *maxz_downlevel_ps;
+       struct DRWPass *minz_downdepth_ps;
+       struct DRWPass *maxz_downdepth_ps;
+       struct DRWPass *minz_copydepth_ps;
+       struct DRWPass *maxz_copydepth_ps;
+
        struct DRWPass *depth_pass;
        struct DRWPass *depth_pass_cull;
        struct DRWPass *depth_pass_clip;
@@ -125,7 +130,7 @@ typedef struct EEVEE_PassList {
 
 typedef struct EEVEE_FramebufferList {
        /* Effects */
-       struct GPUFrameBuffer *minmaxz_fb;
+       struct GPUFrameBuffer *downsample_fb;
        struct GPUFrameBuffer *effect_fb;
        struct GPUFrameBuffer *bloom_blit_fb;
        struct GPUFrameBuffer *bloom_down_fb[MAX_BLOOM_STEP];
@@ -161,6 +166,8 @@ typedef struct EEVEE_TextureList {
 
        struct GPUTexture *planar_pool;
 
+       struct GPUTexture *maxzbuffer;
+
        struct GPUTexture *color; /* R16_G16_B16 */
        struct GPUTexture *color_double_buffer;
 } EEVEE_TextureList;
@@ -444,7 +451,7 @@ typedef struct EEVEE_PrivateData {
        struct DRWShadingGroup *planar_downsample;
        struct GHash *material_hash;
        struct GHash *hair_material_hash;
-       struct GPUTexture *minmaxz;
+       struct GPUTexture *minzbuffer;
        struct GPUTexture *volumetric;
        struct GPUTexture *volumetric_transmit;
        float background_alpha; /* TODO find a better place for this. */
index 77bae174985fc6842b94258e67576e80a3467313..5a59e362fba1eb140196ff43f280862de223d4c4 100644 (file)
@@ -16,7 +16,7 @@ uniform vec3 aoParameters;
 
 float get_max_horizon(vec2 co, vec3 x, float h, float lod)
 {
-       float depth = textureLod(minMaxDepthTex, co, floor(lod)).g;
+       float depth = textureLod(minMaxDepthTex, co, floor(lod)).r;
 
        /* Background case */
        /* this is really slow and is only a problem
index 9f81206070b42d1a824384b57863e9d20019316f..f9478f6770e44b3c96087c049ba3d198e06153f6 100644 (file)
@@ -6,61 +6,50 @@
 
 uniform sampler2D depthBuffer;
 
-out vec4 FragMinMax;
-
-vec2 sampleLowerMip(ivec2 texel)
+float sampleLowerMip(ivec2 texel)
 {
-#ifdef INPUT_DEPTH
-       return texelFetch(depthBuffer, texel, 0).rr;
-#else
-       return texelFetch(depthBuffer, texel, 0).rg;
-#endif
+       return texelFetch(depthBuffer, texel, 0).r;
 }
 
-void minmax(inout vec2 val[2])
+void minmax(inout float out_val, float in_val)
 {
-       val[0].x = min(val[0].x, val[1].x);
-       val[0].y = max(val[0].y, val[1].y);
+#ifdef MIN_PASS
+       out_val = min(out_val, in_val);
+#else /* MAX_PASS */
+       out_val = max(out_val, in_val);
+#endif
 }
 
 void main()
 {
-       vec2 val[2];
        ivec2 texelPos = ivec2(gl_FragCoord.xy);
        ivec2 mipsize = textureSize(depthBuffer, 0);
+
 #ifndef COPY_DEPTH
        texelPos *= 2;
 #endif
 
-       val[0] = sampleLowerMip(texelPos);
+       float val = sampleLowerMip(texelPos);
 #ifndef COPY_DEPTH
-       val[1] = sampleLowerMip(texelPos + ivec2(1, 0));
-       minmax(val);
-       val[1] = sampleLowerMip(texelPos + ivec2(1, 1));
-       minmax(val);
-       val[1] = sampleLowerMip(texelPos + ivec2(0, 1));
-       minmax(val);
+       minmax(val, sampleLowerMip(texelPos + ivec2(1, 0)));
+       minmax(val, sampleLowerMip(texelPos + ivec2(1, 1)));
+       minmax(val, sampleLowerMip(texelPos + ivec2(0, 1)));
 
        /* if we are reducing an odd-width texture then fetch the edge texels */
        if (((mipsize.x & 1) != 0) && (int(gl_FragCoord.x) == mipsize.x-3)) {
                /* if both edges are odd, fetch the top-left corner texel */
                if (((mipsize.y & 1) != 0) && (int(gl_FragCoord.y) == mipsize.y-3)) {
-                       val[1] = sampleLowerMip(texelPos + ivec2(-1, -1));
-                       minmax(val);
+                       minmax(val, sampleLowerMip(texelPos + ivec2(-1, -1)));
                }
-               val[1] = sampleLowerMip(texelPos + ivec2(0, -1));
-               minmax(val);
-               val[1] = sampleLowerMip(texelPos + ivec2(1, -1));
-               minmax(val);
+               minmax(val, sampleLowerMip(texelPos + ivec2(0, -1)));
+               minmax(val, sampleLowerMip(texelPos + ivec2(1, -1)));
        }
        /* if we are reducing an odd-height texture then fetch the edge texels */
        else if (((mipsize.y & 1) != 0) && (int(gl_FragCoord.y) == mipsize.y-3)) {
-               val[1] = sampleLowerMip(texelPos + ivec2(0, -1));
-               minmax(val);
-               val[1] = sampleLowerMip(texelPos + ivec2(1, -1));
-               minmax(val);
+               minmax(val, sampleLowerMip(texelPos + ivec2(0, -1)));
+               minmax(val, sampleLowerMip(texelPos + ivec2(1, -1)));
        }
 #endif
 
-       FragMinMax = vec4(val[0], 0.0, 1.0);
+       gl_FragDepth = val;
 }
\ No newline at end of file
index cb782358f91e9e755adc699f52847c85c6784935..5a64b0ec78153e55b2a07396ce58a2ec933c24f0 100644 (file)
@@ -558,8 +558,14 @@ void GPU_framebuffer_recursive_downsample(
                attachment = GL_COLOR_ATTACHMENT0;
 
        /* last bound prevails here, better allow explicit control here too */
-       glDrawBuffer(GL_COLOR_ATTACHMENT0);
-       glReadBuffer(GL_COLOR_ATTACHMENT0);
+       if (GPU_texture_depth(tex)) {
+               glDrawBuffer(GL_NONE);
+               glReadBuffer(GL_NONE);
+       }
+       else {
+               glDrawBuffer(GL_COLOR_ATTACHMENT0);
+               glReadBuffer(GL_COLOR_ATTACHMENT0);
+       }
 
        for (int i=1; i < num_iter+1 && (current_dim[0] > 1 && current_dim[1] > 1); i++) {