Eevee: Add mist pass support.

Eevee: Render: Fix crash when not enabling mist pass.
This commit is contained in:
Clément Foucault 2018-02-01 18:09:06 +01:00
parent 12bd214b9a
commit e52c5bcdb5
8 changed files with 244 additions and 0 deletions

View File

@ -467,6 +467,7 @@ class VIEWLAYER_PT_eevee_layer_passes(ViewLayerButtonsPanel, Panel):
col = split.column()
col.prop(view_layer, "use_pass_combined")
col.prop(view_layer, "use_pass_z")
col.prop(view_layer, "use_pass_mist")
col.prop(view_layer, "use_pass_normal")
col = split.column()

View File

@ -243,6 +243,33 @@ class WORLD_PT_mist(WorldButtonsPanel, Panel):
layout.prop(world.mist_settings, "falloff")
class EEVEE_WORLD_PT_mist(WorldButtonsPanel, Panel):
bl_label = "Mist Pass"
bl_options = {'DEFAULT_CLOSED'}
COMPAT_ENGINES = {'BLENDER_EEVEE'}
@classmethod
def poll(cls, context):
engine = context.engine
if context.world and (engine in cls.COMPAT_ENGINES):
for view_layer in context.scene.view_layers:
if view_layer.use_pass_mist:
return True
return False
def draw(self, context):
layout = self.layout
world = context.world
split = layout.split(align=True)
split.prop(world.mist_settings, "start")
split.prop(world.mist_settings, "depth")
layout.prop(world.mist_settings, "falloff")
class WORLD_PT_custom_props(WorldButtonsPanel, PropertyPanel, Panel):
COMPAT_ENGINES = {'BLENDER_RENDER', 'BLENDER_GAME', 'BLENDER_EEVEE'}
_context_path = "world"
@ -294,6 +321,7 @@ classes = (
WORLD_PT_mist,
WORLD_PT_custom_props,
EEVEE_WORLD_PT_surface,
EEVEE_WORLD_PT_mist,
)
if __name__ == "__main__": # only for live edit.

View File

@ -92,6 +92,7 @@ set(SRC
engines/eevee/eevee_lightprobes.c
engines/eevee/eevee_lights.c
engines/eevee/eevee_materials.c
engines/eevee/eevee_mist.c
engines/eevee/eevee_motion_blur.c
engines/eevee/eevee_occlusion.c
engines/eevee/eevee_render.c
@ -161,6 +162,7 @@ data_to_c_simple(engines/eevee/shaders/effect_downsample_frag.glsl SRC)
data_to_c_simple(engines/eevee/shaders/effect_downsample_cube_frag.glsl SRC)
data_to_c_simple(engines/eevee/shaders/effect_gtao_frag.glsl SRC)
data_to_c_simple(engines/eevee/shaders/effect_minmaxz_frag.glsl SRC)
data_to_c_simple(engines/eevee/shaders/effect_mist_frag.glsl SRC)
data_to_c_simple(engines/eevee/shaders/effect_motion_blur_frag.glsl SRC)
data_to_c_simple(engines/eevee/shaders/effect_ssr_frag.glsl SRC)
data_to_c_simple(engines/eevee/shaders/effect_subsurface_frag.glsl SRC)

View File

@ -390,6 +390,7 @@ static void eevee_engine_free(void)
EEVEE_lightprobes_free();
EEVEE_lights_free();
EEVEE_materials_free();
EEVEE_mist_free();
EEVEE_motion_blur_free();
EEVEE_occlusion_free();
EEVEE_screen_raytrace_free();

View File

@ -0,0 +1,135 @@
/*
* Copyright 2016, Blender Foundation.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Contributor(s): Blender Institute
*
*/
/** \file eevee_mist.c
* \ingroup draw_engine
*
* Implementation of Blender Mist pass.
* IMPORTANT: This is a "post process" of the Z depth so it will lack any transparent objects.
*/
#include "DRW_engine.h"
#include "DRW_render.h"
#include "DNA_world_types.h"
#include "BLI_string_utils.h"
#include "eevee_private.h"
extern char datatoc_common_uniforms_lib_glsl[];
extern char datatoc_bsdf_common_lib_glsl[];
extern char datatoc_effect_mist_frag_glsl[];
static struct {
struct GPUShader *mist_sh;
} e_data = {NULL}; /* Engine data */
void EEVEE_mist_output_init(EEVEE_ViewLayerData *sldata, EEVEE_Data *vedata)
{
const DRWContextState *draw_ctx = DRW_context_state_get();
EEVEE_FramebufferList *fbl = vedata->fbl;
DefaultTextureList *dtxl = DRW_viewport_texture_list_get();
EEVEE_TextureList *txl = vedata->txl;
EEVEE_StorageList *stl = vedata->stl;
EEVEE_PassList *psl = vedata->psl;
EEVEE_PrivateData *g_data = stl->g_data;
Scene *scene = draw_ctx->scene;
const float *viewport_size = DRW_viewport_size_get();
float clear[4] = {0.0f, 0.0f, 0.0f, 0.0f};
if (e_data.mist_sh == NULL) {
char *frag_str = BLI_string_joinN(
datatoc_common_uniforms_lib_glsl,
datatoc_bsdf_common_lib_glsl,
datatoc_effect_mist_frag_glsl);
e_data.mist_sh = DRW_shader_create_fullscreen(frag_str, "#define FIRST_PASS\n");
MEM_freeN(frag_str);
}
/* Create FrameBuffer. */
DRWFboTexture tex_data = {&txl->mist_accum, DRW_TEX_R_32, 0}; /* Should be enough precision for many samples. */
DRW_framebuffer_init(&fbl->mist_accum_fb, &draw_engine_eevee_type, (int)viewport_size[0], (int)viewport_size[1],
&tex_data, 1);
/* Clear texture. */
DRW_framebuffer_bind(fbl->mist_accum_fb);
DRW_framebuffer_clear(true, false, false, clear, 0.0f);
/* Mist settings. */
if (scene && scene->world) {
g_data->mist_start = scene->world->miststa;
g_data->mist_inv_dist = (scene->world->mistdist > 0.0f) ? 1.0f / scene->world->mistdist : 0.0f;
switch (scene->world->mistype) {
case WO_MIST_QUADRATIC:
g_data->mist_falloff = 2.0f;
break;
case WO_MIST_LINEAR:
g_data->mist_falloff = 1.0f;
break;
case WO_MIST_INVERSE_QUADRATIC:
g_data->mist_falloff = 0.5f;
break;
}
}
else {
float near = -sldata->common_data.view_vecs[0][2];
float range = sldata->common_data.view_vecs[1][2];
/* Fallback */
g_data->mist_start = near;
g_data->mist_inv_dist = 1.0f / fabsf(range);
g_data->mist_falloff = 1.0f;
}
/* XXX ??!! WHY? If not it does not match cycles. */
g_data->mist_falloff *= 0.5f;
/* Create Pass and shgroup. */
psl->mist_accum_ps = DRW_pass_create("Mist Accum", DRW_STATE_WRITE_COLOR | DRW_STATE_ADDITIVE);
DRWShadingGroup *grp = DRW_shgroup_create(e_data.mist_sh, psl->mist_accum_ps);
DRW_shgroup_uniform_buffer(grp, "depthBuffer", &dtxl->depth);
DRW_shgroup_uniform_block(grp, "common_block", sldata->common_ubo);
DRW_shgroup_uniform_vec3(grp, "mistSettings", &g_data->mist_start, 1);
DRW_shgroup_call_add(grp, DRW_cache_fullscreen_quad_get(), NULL);
}
void EEVEE_mist_output_accumulate(EEVEE_ViewLayerData *UNUSED(sldata), EEVEE_Data *vedata)
{
EEVEE_FramebufferList *fbl = vedata->fbl;
EEVEE_PassList *psl = vedata->psl;
if (fbl->mist_accum_fb != NULL) {
DRW_framebuffer_bind(fbl->mist_accum_fb);
DRW_draw_pass(psl->mist_accum_ps);
/* Restore */
DRW_framebuffer_bind(fbl->main);
}
}
void EEVEE_mist_free(void)
{
DRW_SHADER_FREE_SAFE(e_data.mist_sh);
}

View File

@ -160,6 +160,7 @@ typedef struct EEVEE_PassList {
struct DRWPass *ao_horizon_search;
struct DRWPass *ao_horizon_search_layer;
struct DRWPass *ao_horizon_debug;
struct DRWPass *mist_accum_ps;
struct DRWPass *motion_blur;
struct DRWPass *bloom_blit;
struct DRWPass *bloom_downsample_first;
@ -230,6 +231,7 @@ typedef struct EEVEE_FramebufferList {
struct GPUFrameBuffer *volumetric_integ_fb;
struct GPUFrameBuffer *screen_tracing_fb;
struct GPUFrameBuffer *refract_fb;
struct GPUFrameBuffer *mist_accum_fb;
struct GPUFrameBuffer *update_noise_fb;
@ -251,6 +253,7 @@ typedef struct EEVEE_TextureList {
struct GPUTexture *bloom_blit; /* R16_G16_B16 */
struct GPUTexture *bloom_downsample[MAX_BLOOM_STEP]; /* R16_G16_B16 */
struct GPUTexture *bloom_upsample[MAX_BLOOM_STEP - 1]; /* R16_G16_B16 */
struct GPUTexture *mist_accum;
struct GPUTexture *sss_dir_accum;
struct GPUTexture *sss_col_accum;
struct GPUTexture *ssr_normal_input;
@ -740,6 +743,8 @@ typedef struct EEVEE_PrivateData {
float persmat[4][4], persinv[4][4];
float viewmat[4][4], viewinv[4][4];
float winmat[4][4], wininv[4][4];
/* Mist Settings */
float mist_start, mist_inv_dist, mist_falloff;
} EEVEE_PrivateData; /* Transient data */
/* eevee_data.c */
@ -838,6 +843,11 @@ void EEVEE_motion_blur_cache_init(EEVEE_ViewLayerData *sldata, EEVEE_Data *vedat
void EEVEE_motion_blur_draw(EEVEE_Data *vedata);
void EEVEE_motion_blur_free(void);
/* eevee_mist.c */
void EEVEE_mist_output_init(EEVEE_ViewLayerData *sldata, EEVEE_Data *vedata);
void EEVEE_mist_output_accumulate(EEVEE_ViewLayerData *sldata, EEVEE_Data *vedata);;
void EEVEE_mist_free(void);
/* eevee_temporal_sampling.c */
int EEVEE_temporal_sampling_init(EEVEE_ViewLayerData *sldata, EEVEE_Data *vedata);
void EEVEE_temporal_sampling_matrices_calc(

View File

@ -167,6 +167,11 @@ static void eevee_render_result_subsurface(
const DRWContextState *draw_ctx = DRW_context_state_get();
ViewLayer *view_layer = draw_ctx->view_layer;
if (vedata->fbl->sss_accum_fb == NULL) {
/* SSS is not enabled. */
return;
}
if ((view_layer->passflag & SCE_PASS_SUBSURFACE_COLOR) != 0) {
RenderLayer *rl = rr->layers.first;
RenderPass *rp = RE_pass_find_by_name(rl, RE_PASSNAME_SUBSURFACE_COLOR, viewname);
@ -282,6 +287,30 @@ static void eevee_render_result_z(
}
}
static void eevee_render_result_mist(
RenderResult *rr, const char *viewname,
EEVEE_Data *vedata, EEVEE_ViewLayerData *UNUSED(sldata))
{
const DRWContextState *draw_ctx = DRW_context_state_get();
ViewLayer *view_layer = draw_ctx->view_layer;
if ((view_layer->passflag & SCE_PASS_MIST) != 0) {
RenderLayer *rl = rr->layers.first;
RenderPass *rp = RE_pass_find_by_name(rl, RE_PASSNAME_MIST, viewname);
IDProperty *props = BKE_view_layer_engine_evaluated_get(view_layer, COLLECTION_MODE_NONE, RE_engine_id_BLENDER_EEVEE);
float render_samples = (float)BKE_collection_engine_property_value_get_int(props, "taa_render_samples");
DRW_framebuffer_bind(vedata->fbl->mist_accum_fb);
DRW_framebuffer_read_data(rr->xof, rr->yof, rr->rectx, rr->recty, 1, 0, rp->rect);
/* This is the accumulated color. Divide by the number of samples. */
for (int i = 0; i < rr->rectx * rr->recty; i++) {
rp->rect[i] /= render_samples;
}
}
}
void EEVEE_render_draw(EEVEE_Data *vedata, struct RenderEngine *engine, struct Depsgraph *UNUSED(depsgraph))
{
const DRWContextState *draw_ctx = DRW_context_state_get();
@ -305,6 +334,10 @@ void EEVEE_render_draw(EEVEE_Data *vedata, struct RenderEngine *engine, struct D
EEVEE_subsurface_output_init(sldata, vedata);
}
if ((view_layer->passflag & SCE_PASS_MIST) != 0) {
EEVEE_mist_output_init(sldata, vedata);
}
/* Init render result. */
const char *viewname = NULL;
const float *render_size = DRW_viewport_size_get();
@ -366,6 +399,8 @@ void EEVEE_render_draw(EEVEE_Data *vedata, struct RenderEngine *engine, struct D
eevee_render_result_normal(rr, viewname, vedata, sldata);
/* Volumetrics Resolve Opaque */
EEVEE_volumes_resolve(sldata, vedata);
/* Mist output */
EEVEE_mist_output_accumulate(sldata, vedata);
/* Transparent */
DRW_pass_sort_shgroup_z(psl->transparent_pass);
DRW_draw_pass(psl->transparent_pass);
@ -377,6 +412,7 @@ void EEVEE_render_draw(EEVEE_Data *vedata, struct RenderEngine *engine, struct D
eevee_render_result_combined(rr, viewname, vedata, sldata);
eevee_render_result_subsurface(rr, viewname, vedata, sldata);
eevee_render_result_mist(rr, viewname, vedata, sldata);
RE_engine_end_result(engine, rr, false, false, false);
}

View File

@ -0,0 +1,31 @@
/* Convert depth to Mist factor */
uniform vec3 mistSettings;
#define mistStart mistSettings.x
#define mistInvDistance mistSettings.y
#define mistFalloff mistSettings.z
out vec4 fragColor;
void main()
{
vec2 texel_size = 1.0 / vec2(textureSize(depthBuffer, 0)).xy;
vec2 uvs = gl_FragCoord.xy * texel_size;
float depth = textureLod(depthBuffer, uvs, 0.0).r;
vec3 co = get_view_space_from_depth(uvs, depth);
float zcor = (ProjectionMatrix[3][3] == 0.0) ? length(co) : -co.z;
/* bring depth into 0..1 range */
float mist = saturate((zcor - mistStart) * mistInvDistance);
/* falloff */
mist = pow(mist, mistFalloff);
fragColor = vec4(mist);
// if (mist > 0.999) fragColor = vec4(1.0);
// else if (mist > 0.0001) fragColor = vec4(0.5);
// else fragColor = vec4(0.0);
}