Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClément Foucault <foucault.clem@gmail.com>2021-02-13 00:35:18 +0300
committerClément Foucault <foucault.clem@gmail.com>2021-02-13 00:35:52 +0300
commit000a340afa67a12a38c103e19f7d52ed42c4492e (patch)
treed59ea49aedfba450d7de20240004399e1ee30dc8 /source/blender/draw/engines/eevee/eevee_shaders.c
parentdd2ff266acf999dbb3a37b70162f5ca7c029426a (diff)
EEVEE: Depth of field: New implementation
This is a complete refactor over the old system. The goal was to increase quality first and then have something more flexible and optimised. |{F9603145} | {F9603142}|{F9603147}| This fixes issues we had with the old system which were: - Too much overdraw (low performance). - Not enough precision in render targets (hugly color banding/drifting). - Poor resolution near in-focus regions. - Wrong support of orthographic views. - Missing alpha support in viewport. - Missing bokeh shape inversion on foreground field. - Issues on some GPUs. (see T72489) (But I'm sure this one will have other issues as well heh...) - Fix T81092 I chose Unreal's Diaphragm DOF as a reference / goal implementation. It is well described in the presentation "A Life of a Bokeh" by Guillaume Abadie. You can check about it here https://epicgames.ent.box.com/s/s86j70iamxvsuu6j35pilypficznec04 Along side the main implementation we provide a way to increase the quality by jittering the camera position for each sample (the ones specified under the Sampling tab). The jittering is dividing the actual post processing dof radius so that it fills the undersampling. The user can still add more overblur to have a noiseless image, but reducing bokeh shape sharpness. Effect of overblur (left without, right with): | {F9603122} | {F9603123}| The actual implementation differs a bit: - Foreground gather implementation uses the same "ring binning" accumulator as background but uses a custom occlusion method. This gives the problem of inflating the foreground elements when they are over background or in-focus regions. This is was a hard decision but this was preferable to the other method that was giving poor opacity masks for foreground and had other more noticeable issues. Do note it is possible to improve this part in the future if a better alternative is found. - Use occlusion texture for foreground. Presentation says it wasn't really needed for them. - The TAA stabilisation pass is replace by a simple neighborhood clamping at the reduce copy stage for simplicity. - We don't do a brute-force in-focus separate gather pass. Instead we just do the brute force pass during resolve. Using the separate pass could be a future optimization if needed but might give less precise results. - We don't use compute shaders at all so shader branching might not be optimal. But performance is still way better than our previous implementation. - We mainly rely on density change to fix all undersampling issues even for foreground (which is something the reference implementation is not doing strangely). Remaining issues (not considered blocking for me): - Slight defocus stability: Due to slight defocus bruteforce gather using the bare scene color, highlights are dilated and make convergence quite slow or imposible when using jittered DOF (or gives ) - ~~Slight defocus inflating: There seems to be a 1px inflation discontinuity of the slight focus convolution compared to the half resolution. This is not really noticeable if using jittered camera.~~ Fixed - Foreground occlusion approximation is a bit glitchy and gives incorrect result if the a defocus foreground element overlaps a farther foreground element. Note that this is easily mitigated using the jittered camera position. |{F9603114}|{F9603115}|{F9603116}| - Foreground is inflating, not revealing background. However this avoids some other bugs too as discussed previously. Also mitigated with jittered camera position. |{F9603130}|{F9603129}| - Sensor vertical fit is still broken (does not match cycles). - Scattred bokeh shapes can be a bit strange at polygon vertices. This is due to the distance field stored in the Bokeh LUT which is not rounded at the edges. This is barely noticeable if the shape does not rotate. - ~~Sampling pattern of the jittered camera position is suboptimal. Could try something like hammersley or poisson disc distribution.~~Used hexaweb sampling pattern which is not random but has better stability and overall coverage. - Very large bokeh (> 300 px) can exhibit undersampling artifact in gather pass and quite a bit of bleeding. But at this size it is preferable to use jittered camera position. Codewise the changes are pretty much self contained and each pass are well documented. However the whole pipeline is quite complex to understand from bird's-eye view. Notes: - There is the possibility of using arbitrary bokeh texture with this implementation. However implementation is a bit involved. - Gathering max sample count is hardcoded to avoid to deal with shader variations. The actual max sample count is already quite high but samples are not evenly distributed due to the ring binning method. - While this implementation does not need 32bit/channel textures to render correctly it does use many other textures so actual VRAM usage is higher than previous method for viewport but less for render. Textures are reused to avoid many allocations. - Bokeh LUT computation is fast and done for each redraw because it can be animated. Also the texture can be shared with other viewport with different camera settings.
Diffstat (limited to 'source/blender/draw/engines/eevee/eevee_shaders.c')
-rw-r--r--source/blender/draw/engines/eevee/eevee_shaders.c236
1 files changed, 198 insertions, 38 deletions
diff --git a/source/blender/draw/engines/eevee/eevee_shaders.c b/source/blender/draw/engines/eevee/eevee_shaders.c
index 82b2395cc6e..ec058afd58e 100644
--- a/source/blender/draw/engines/eevee/eevee_shaders.c
+++ b/source/blender/draw/engines/eevee/eevee_shaders.c
@@ -77,9 +77,16 @@ static struct {
struct GPUShader *bloom_resolve_sh[2];
/* Depth Of Field */
- struct GPUShader *dof_downsample_sh[2];
- struct GPUShader *dof_scatter_sh[2];
- struct GPUShader *dof_resolve_sh[2];
+ struct GPUShader *dof_bokeh_sh;
+ struct GPUShader *dof_setup_sh;
+ struct GPUShader *dof_flatten_tiles_sh;
+ struct GPUShader *dof_dilate_tiles_sh[2];
+ struct GPUShader *dof_downsample_sh;
+ struct GPUShader *dof_reduce_sh[2];
+ struct GPUShader *dof_gather_sh[DOF_GATHER_MAX_PASS][2];
+ struct GPUShader *dof_filter_sh;
+ struct GPUShader *dof_scatter_sh[2][2];
+ struct GPUShader *dof_resolve_sh[2][2];
/* General purpose Shaders. */
struct GPUShader *lookdev_background;
@@ -192,8 +199,18 @@ extern char datatoc_cubemap_lib_glsl[];
extern char datatoc_default_frag_glsl[];
extern char datatoc_lookdev_world_frag_glsl[];
extern char datatoc_effect_bloom_frag_glsl[];
-extern char datatoc_effect_dof_frag_glsl[];
-extern char datatoc_effect_dof_vert_glsl[];
+extern char datatoc_effect_dof_bokeh_frag_glsl[];
+extern char datatoc_effect_dof_dilate_tiles_frag_glsl[];
+extern char datatoc_effect_dof_downsample_frag_glsl[];
+extern char datatoc_effect_dof_filter_frag_glsl[];
+extern char datatoc_effect_dof_flatten_tiles_frag_glsl[];
+extern char datatoc_effect_dof_gather_frag_glsl[];
+extern char datatoc_effect_dof_lib_glsl[];
+extern char datatoc_effect_dof_reduce_frag_glsl[];
+extern char datatoc_effect_dof_resolve_frag_glsl[];
+extern char datatoc_effect_dof_scatter_frag_glsl[];
+extern char datatoc_effect_dof_scatter_vert_glsl[];
+extern char datatoc_effect_dof_setup_frag_glsl[];
extern char datatoc_effect_downsample_cube_frag_glsl[];
extern char datatoc_effect_downsample_frag_glsl[];
extern char datatoc_effect_gtao_frag_glsl[];
@@ -281,6 +298,7 @@ static void eevee_shader_library_ensure(void)
DRW_SHADER_LIB_ADD(e_data.lib, volumetric_lib);
DRW_SHADER_LIB_ADD(e_data.lib, closure_lib);
DRW_SHADER_LIB_ADD(e_data.lib, ssr_lib);
+ DRW_SHADER_LIB_ADD(e_data.lib, effect_dof_lib);
/* Add one for each Closure */
e_data.closure_lit_lib = BLI_string_joinN(datatoc_closure_lit_lib_glsl,
@@ -1010,48 +1028,172 @@ GPUShader *EEVEE_shaders_bloom_resolve_get(bool high_quality)
/** \name Depth of field
* \{ */
-GPUShader *EEVEE_shaders_depth_of_field_downsample_get(bool use_alpha)
+GPUShader *EEVEE_shaders_depth_of_field_bokeh_get(void)
{
- int index = use_alpha ? 1 : 0;
- if (e_data.dof_downsample_sh[index] == NULL) {
- e_data.dof_downsample_sh[index] = DRW_shader_create_fullscreen_with_shaderlib(
- datatoc_effect_dof_frag_glsl,
+ if (e_data.dof_bokeh_sh == NULL) {
+ e_data.dof_bokeh_sh = DRW_shader_create_fullscreen_with_shaderlib(
+ datatoc_effect_dof_bokeh_frag_glsl, e_data.lib, DOF_SHADER_DEFINES);
+ }
+ return e_data.dof_bokeh_sh;
+}
+
+GPUShader *EEVEE_shaders_depth_of_field_setup_get(void)
+{
+ if (e_data.dof_setup_sh == NULL) {
+ e_data.dof_setup_sh = DRW_shader_create_fullscreen_with_shaderlib(
+ datatoc_effect_dof_setup_frag_glsl, e_data.lib, DOF_SHADER_DEFINES);
+ }
+ return e_data.dof_setup_sh;
+}
+
+GPUShader *EEVEE_shaders_depth_of_field_flatten_tiles_get(void)
+{
+ if (e_data.dof_flatten_tiles_sh == NULL) {
+ e_data.dof_flatten_tiles_sh = DRW_shader_create_fullscreen_with_shaderlib(
+ datatoc_effect_dof_flatten_tiles_frag_glsl, e_data.lib, DOF_SHADER_DEFINES);
+ }
+ return e_data.dof_flatten_tiles_sh;
+}
+
+GPUShader *EEVEE_shaders_depth_of_field_dilate_tiles_get(bool b_pass)
+{
+ int pass = b_pass;
+ if (e_data.dof_dilate_tiles_sh[pass] == NULL) {
+ e_data.dof_dilate_tiles_sh[pass] = DRW_shader_create_fullscreen_with_shaderlib(
+ datatoc_effect_dof_dilate_tiles_frag_glsl,
+ e_data.lib,
+ (pass == 0) ? DOF_SHADER_DEFINES "#define DILATE_MODE_MIN_MAX\n" :
+ DOF_SHADER_DEFINES "#define DILATE_MODE_MIN_ABS\n");
+ }
+ return e_data.dof_dilate_tiles_sh[pass];
+}
+
+GPUShader *EEVEE_shaders_depth_of_field_downsample_get(void)
+{
+ if (e_data.dof_downsample_sh == NULL) {
+ e_data.dof_downsample_sh = DRW_shader_create_fullscreen_with_shaderlib(
+ datatoc_effect_dof_downsample_frag_glsl, e_data.lib, DOF_SHADER_DEFINES);
+ }
+ return e_data.dof_downsample_sh;
+}
+
+GPUShader *EEVEE_shaders_depth_of_field_reduce_get(bool b_is_copy_pass)
+{
+ int is_copy_pass = b_is_copy_pass;
+ if (e_data.dof_reduce_sh[is_copy_pass] == NULL) {
+ e_data.dof_reduce_sh[is_copy_pass] = DRW_shader_create_fullscreen_with_shaderlib(
+ datatoc_effect_dof_reduce_frag_glsl,
e_data.lib,
- use_alpha ? "#define USE_ALPHA_DOF\n"
- "#define STEP_DOWNSAMPLE\n" :
- "#define STEP_DOWNSAMPLE\n");
+ (is_copy_pass) ? DOF_SHADER_DEFINES "#define COPY_PASS\n" :
+ DOF_SHADER_DEFINES "#define REDUCE_PASS\n");
+ }
+ return e_data.dof_reduce_sh[is_copy_pass];
+}
+
+GPUShader *EEVEE_shaders_depth_of_field_gather_get(EEVEE_DofGatherPass pass, bool b_use_bokeh_tx)
+{
+ int use_bokeh_tx = b_use_bokeh_tx;
+ if (e_data.dof_gather_sh[pass][use_bokeh_tx] == NULL) {
+ DynStr *ds = BLI_dynstr_new();
+
+ BLI_dynstr_append(ds, DOF_SHADER_DEFINES);
+
+ switch (pass) {
+ case DOF_GATHER_FOREGROUND:
+ BLI_dynstr_append(ds, "#define DOF_FOREGROUND_PASS\n");
+ break;
+ case DOF_GATHER_BACKGROUND:
+ BLI_dynstr_append(ds, "#define DOF_BACKGROUND_PASS\n");
+ break;
+ case DOF_GATHER_HOLEFILL:
+ BLI_dynstr_append(ds,
+ "#define DOF_BACKGROUND_PASS\n"
+ "#define DOF_HOLEFILL_PASS\n");
+ break;
+ default:
+ break;
+ }
+
+ if (use_bokeh_tx) {
+ BLI_dynstr_append(ds, "#define DOF_BOKEH_TEXTURE\n");
+ }
+
+ char *define = BLI_dynstr_get_cstring(ds);
+ BLI_dynstr_free(ds);
+
+ e_data.dof_gather_sh[pass][use_bokeh_tx] = DRW_shader_create_fullscreen_with_shaderlib(
+ datatoc_effect_dof_gather_frag_glsl, e_data.lib, define);
+
+ MEM_freeN(define);
}
- return e_data.dof_downsample_sh[index];
+ return e_data.dof_gather_sh[pass][use_bokeh_tx];
}
-GPUShader *EEVEE_shaders_depth_of_field_scatter_get(bool use_alpha)
+GPUShader *EEVEE_shaders_depth_of_field_filter_get(void)
{
- int index = use_alpha ? 1 : 0;
- if (e_data.dof_scatter_sh[index] == NULL) {
- e_data.dof_scatter_sh[index] = DRW_shader_create_with_shaderlib(datatoc_effect_dof_vert_glsl,
- NULL,
- datatoc_effect_dof_frag_glsl,
- e_data.lib,
- use_alpha ?
- "#define USE_ALPHA_DOF\n"
- "#define STEP_SCATTER\n" :
- "#define STEP_SCATTER\n");
+ if (e_data.dof_filter_sh == NULL) {
+ e_data.dof_filter_sh = DRW_shader_create_fullscreen_with_shaderlib(
+ datatoc_effect_dof_filter_frag_glsl, e_data.lib, DOF_SHADER_DEFINES);
}
- return e_data.dof_scatter_sh[index];
+ return e_data.dof_filter_sh;
}
-GPUShader *EEVEE_shaders_depth_of_field_resolve_get(bool use_alpha)
+GPUShader *EEVEE_shaders_depth_of_field_scatter_get(bool b_is_foreground, bool b_use_bokeh_tx)
{
- int index = use_alpha ? 1 : 0;
- if (e_data.dof_resolve_sh[index] == NULL) {
- e_data.dof_resolve_sh[index] = DRW_shader_create_fullscreen_with_shaderlib(
- datatoc_effect_dof_frag_glsl,
+ int is_foreground = b_is_foreground;
+ int use_bokeh_tx = b_use_bokeh_tx;
+ if (e_data.dof_scatter_sh[is_foreground][use_bokeh_tx] == NULL) {
+ DynStr *ds = BLI_dynstr_new();
+
+ BLI_dynstr_append(ds, DOF_SHADER_DEFINES);
+ BLI_dynstr_append(
+ ds, (is_foreground) ? "#define DOF_FOREGROUND_PASS\n" : "#define DOF_BACKGROUND_PASS\n");
+
+ if (use_bokeh_tx) {
+ BLI_dynstr_append(ds, "#define DOF_BOKEH_TEXTURE\n");
+ }
+
+ char *define = BLI_dynstr_get_cstring(ds);
+ BLI_dynstr_free(ds);
+
+ e_data.dof_scatter_sh[is_foreground][use_bokeh_tx] = DRW_shader_create_with_shaderlib(
+ datatoc_effect_dof_scatter_vert_glsl,
+ NULL,
+ datatoc_effect_dof_scatter_frag_glsl,
e_data.lib,
- use_alpha ? "#define USE_ALPHA_DOF\n"
- "#define STEP_RESOLVE\n" :
- "#define STEP_RESOLVE\n");
+ define);
+
+ MEM_freeN(define);
+ }
+ return e_data.dof_scatter_sh[is_foreground][use_bokeh_tx];
+}
+
+GPUShader *EEVEE_shaders_depth_of_field_resolve_get(bool b_use_bokeh_tx, bool b_use_hq_gather)
+{
+ int use_hq_gather = b_use_hq_gather;
+ int use_bokeh_tx = b_use_bokeh_tx;
+ if (e_data.dof_resolve_sh[use_bokeh_tx][use_hq_gather] == NULL) {
+ DynStr *ds = BLI_dynstr_new();
+
+ BLI_dynstr_append(ds, DOF_SHADER_DEFINES);
+ BLI_dynstr_append(ds, "#define DOF_RESOLVE_PASS\n");
+
+ if (use_bokeh_tx) {
+ BLI_dynstr_append(ds, "#define DOF_BOKEH_TEXTURE\n");
+ }
+
+ BLI_dynstr_appendf(ds, "#define DOF_SLIGHT_FOCUS_DENSITY %d\n", use_hq_gather ? 4 : 2);
+
+ char *define = BLI_dynstr_get_cstring(ds);
+ BLI_dynstr_free(ds);
+
+ e_data.dof_resolve_sh[use_bokeh_tx][use_hq_gather] =
+ DRW_shader_create_fullscreen_with_shaderlib(
+ datatoc_effect_dof_resolve_frag_glsl, e_data.lib, define);
+
+ MEM_freeN(define);
}
- return e_data.dof_resolve_sh[index];
+ return e_data.dof_resolve_sh[use_bokeh_tx][use_hq_gather];
}
/* \} */
@@ -1458,6 +1600,27 @@ void EEVEE_shaders_free(void)
DRW_SHADER_FREE_SAFE(e_data.velocity_resolve_sh);
DRW_SHADER_FREE_SAFE(e_data.taa_resolve_sh);
DRW_SHADER_FREE_SAFE(e_data.taa_resolve_reproject_sh);
+ DRW_SHADER_FREE_SAFE(e_data.dof_bokeh_sh);
+ DRW_SHADER_FREE_SAFE(e_data.dof_setup_sh);
+ DRW_SHADER_FREE_SAFE(e_data.dof_flatten_tiles_sh);
+ DRW_SHADER_FREE_SAFE(e_data.dof_dilate_tiles_sh[0]);
+ DRW_SHADER_FREE_SAFE(e_data.dof_dilate_tiles_sh[1]);
+ DRW_SHADER_FREE_SAFE(e_data.dof_downsample_sh);
+ DRW_SHADER_FREE_SAFE(e_data.dof_reduce_sh[0]);
+ DRW_SHADER_FREE_SAFE(e_data.dof_reduce_sh[1]);
+ for (int i = 0; i < DOF_GATHER_MAX_PASS; i++) {
+ DRW_SHADER_FREE_SAFE(e_data.dof_gather_sh[i][0]);
+ DRW_SHADER_FREE_SAFE(e_data.dof_gather_sh[i][1]);
+ }
+ DRW_SHADER_FREE_SAFE(e_data.dof_filter_sh);
+ DRW_SHADER_FREE_SAFE(e_data.dof_scatter_sh[0][0]);
+ DRW_SHADER_FREE_SAFE(e_data.dof_scatter_sh[0][1]);
+ DRW_SHADER_FREE_SAFE(e_data.dof_scatter_sh[1][0]);
+ DRW_SHADER_FREE_SAFE(e_data.dof_scatter_sh[1][1]);
+ DRW_SHADER_FREE_SAFE(e_data.dof_resolve_sh[0][0]);
+ DRW_SHADER_FREE_SAFE(e_data.dof_resolve_sh[0][1]);
+ DRW_SHADER_FREE_SAFE(e_data.dof_resolve_sh[1][0]);
+ DRW_SHADER_FREE_SAFE(e_data.dof_resolve_sh[1][1]);
DRW_SHADER_FREE_SAFE(e_data.cryptomatte_sh[0]);
DRW_SHADER_FREE_SAFE(e_data.cryptomatte_sh[1]);
for (int i = 0; i < 2; i++) {
@@ -1465,9 +1628,6 @@ void EEVEE_shaders_free(void)
DRW_SHADER_FREE_SAFE(e_data.bloom_downsample_sh[i]);
DRW_SHADER_FREE_SAFE(e_data.bloom_upsample_sh[i]);
DRW_SHADER_FREE_SAFE(e_data.bloom_resolve_sh[i]);
- DRW_SHADER_FREE_SAFE(e_data.dof_downsample_sh[i]);
- DRW_SHADER_FREE_SAFE(e_data.dof_scatter_sh[i]);
- DRW_SHADER_FREE_SAFE(e_data.dof_resolve_sh[i]);
}
for (int i = 0; i < SSR_MAX_SHADER; i++) {
DRW_SHADER_FREE_SAFE(e_data.ssr_sh[i]);