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