GP: Improve Blur FX quality in transparency.
[blender.git] / source / blender / draw / engines / eevee / eevee_mist.c
1 /*
2  * Copyright 2016, Blender Foundation.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * Contributor(s): Blender Institute
19  *
20  */
21
22 /** \file eevee_mist.c
23  *  \ingroup draw_engine
24  *
25  * Implementation of Blender Mist pass.
26  * IMPORTANT: This is a "post process" of the Z depth so it will lack any transparent objects.
27  */
28
29 #include "DRW_engine.h"
30 #include "DRW_render.h"
31
32 #include "DNA_world_types.h"
33
34 #include "BLI_string_utils.h"
35
36 #include "eevee_private.h"
37
38 extern char datatoc_common_view_lib_glsl[];
39 extern char datatoc_common_uniforms_lib_glsl[];
40 extern char datatoc_bsdf_common_lib_glsl[];
41 extern char datatoc_effect_mist_frag_glsl[];
42
43 static struct {
44         struct GPUShader *mist_sh;
45 } e_data = {NULL}; /* Engine data */
46
47 void EEVEE_mist_output_init(EEVEE_ViewLayerData *sldata, EEVEE_Data *vedata)
48 {
49         const DRWContextState *draw_ctx = DRW_context_state_get();
50         EEVEE_FramebufferList *fbl = vedata->fbl;
51         DefaultTextureList *dtxl = DRW_viewport_texture_list_get();
52         EEVEE_TextureList *txl = vedata->txl;
53         EEVEE_StorageList *stl = vedata->stl;
54         EEVEE_PassList *psl = vedata->psl;
55         EEVEE_PrivateData *g_data = stl->g_data;
56         Scene *scene = draw_ctx->scene;
57
58         float clear[4] = {0.0f, 0.0f, 0.0f, 0.0f};
59
60         if (e_data.mist_sh == NULL) {
61                 char *frag_str = BLI_string_joinN(
62                         datatoc_common_view_lib_glsl,
63                         datatoc_common_uniforms_lib_glsl,
64                         datatoc_bsdf_common_lib_glsl,
65                         datatoc_effect_mist_frag_glsl);
66
67                 e_data.mist_sh = DRW_shader_create_fullscreen(frag_str, "#define FIRST_PASS\n");
68
69                 MEM_freeN(frag_str);
70         }
71
72         /* Create FrameBuffer. */
73         DRW_texture_ensure_fullscreen_2D(&txl->mist_accum, GPU_R32F, 0); /* Should be enough precision for many samples. */
74
75         GPU_framebuffer_ensure_config(&fbl->mist_accum_fb, {
76                 GPU_ATTACHMENT_NONE,
77                 GPU_ATTACHMENT_TEXTURE(txl->mist_accum)
78         });
79
80         /* Clear texture. */
81         GPU_framebuffer_bind(fbl->mist_accum_fb);
82         GPU_framebuffer_clear_color(fbl->mist_accum_fb, clear);
83
84         /* Mist settings. */
85         if (scene && scene->world) {
86                 g_data->mist_start = scene->world->miststa;
87                 g_data->mist_inv_dist = (scene->world->mistdist > 0.0f) ? 1.0f / scene->world->mistdist : 0.0f;
88
89                 switch (scene->world->mistype) {
90                         case WO_MIST_QUADRATIC:
91                                 g_data->mist_falloff = 2.0f;
92                                 break;
93                         case WO_MIST_LINEAR:
94                                 g_data->mist_falloff = 1.0f;
95                                 break;
96                         case WO_MIST_INVERSE_QUADRATIC:
97                                 g_data->mist_falloff = 0.5f;
98                                 break;
99                 }
100         }
101         else {
102                 float near = -sldata->common_data.view_vecs[0][2];
103                 float range = sldata->common_data.view_vecs[1][2];
104                 /* Fallback */
105                 g_data->mist_start = near;
106                 g_data->mist_inv_dist = 1.0f / fabsf(range);
107                 g_data->mist_falloff = 1.0f;
108         }
109
110         /* XXX ??!! WHY? If not it does not match cycles. */
111         g_data->mist_falloff *= 0.5f;
112
113         /* Create Pass and shgroup. */
114         psl->mist_accum_ps = DRW_pass_create("Mist Accum", DRW_STATE_WRITE_COLOR | DRW_STATE_ADDITIVE);
115         DRWShadingGroup *grp = DRW_shgroup_create(e_data.mist_sh, psl->mist_accum_ps);
116         DRW_shgroup_uniform_texture_ref(grp, "depthBuffer", &dtxl->depth);
117         DRW_shgroup_uniform_block(grp, "common_block", sldata->common_ubo);
118         DRW_shgroup_uniform_vec3(grp, "mistSettings", &g_data->mist_start, 1);
119         DRW_shgroup_call_add(grp, DRW_cache_fullscreen_quad_get(), NULL);
120 }
121
122 void EEVEE_mist_output_accumulate(EEVEE_ViewLayerData *UNUSED(sldata), EEVEE_Data *vedata)
123 {
124         EEVEE_FramebufferList *fbl = vedata->fbl;
125         EEVEE_PassList *psl = vedata->psl;
126
127         if (fbl->mist_accum_fb != NULL) {
128                 GPU_framebuffer_bind(fbl->mist_accum_fb);
129                 DRW_draw_pass(psl->mist_accum_ps);
130
131                 /* Restore */
132                 GPU_framebuffer_bind(fbl->main_fb);
133         }
134 }
135
136 void EEVEE_mist_free(void)
137 {
138         DRW_SHADER_FREE_SAFE(e_data.mist_sh);
139 }