Eevee: Add support for alpha background in viewport

Viewport now displays alpha checkerboard pattern like Cycles does when
film alpha is set to "Transparent".

Some small workarounds were necessary for Depth of Field and correct TAA
support.
This commit is contained in:
Clément Foucault 2019-05-01 11:01:01 +02:00
parent 47717060af
commit b581f19292
Notes: blender-bot 2023-02-14 08:06:38 +01:00
Referenced by issue #63601, Eevee does not respect Alpha Transparent setting in Rendered Viewport shading mode with an Environment Texture (HDRI) as the background
6 changed files with 62 additions and 3 deletions

View File

@ -241,6 +241,7 @@ void EEVEE_depth_of_field_cache_init(EEVEE_ViewLayerData *UNUSED(sldata), EEVEE_
if (use_alpha) {
DRW_shgroup_uniform_texture_ref(grp, "scatterAlphaBuffer", &effects->dof_blur_alpha);
DRW_shgroup_uniform_bool_copy(grp, "unpremult", DRW_state_is_image_render());
}
}
}

View File

@ -168,6 +168,11 @@ void EEVEE_effects_init(EEVEE_ViewLayerData *sldata,
effects->enabled_effects |= EFFECT_NORMAL_BUFFER;
}
/* Alpha checker if background is not drawn in viewport. */
if (!DRW_state_is_image_render() && !DRW_state_draw_background()) {
effects->enabled_effects |= EFFECT_ALPHA_CHECKER;
}
/**
* Ping Pong buffer
*/
@ -361,6 +366,23 @@ void EEVEE_effects_cache_init(EEVEE_ViewLayerData *sldata, EEVEE_Data *vedata)
DRW_shgroup_uniform_mat4(grp, "pastPersmat", effects->velocity_past_persmat);
DRW_shgroup_call_add(grp, quad, NULL);
}
if ((effects->enabled_effects & EFFECT_ALPHA_CHECKER) != 0) {
psl->alpha_checker = DRW_pass_create("Alpha Checker",
DRW_STATE_WRITE_COLOR | DRW_STATE_BLEND_PREMUL_UNDER);
GPUShader *checker_sh = GPU_shader_get_builtin_shader(GPU_SHADER_2D_CHECKER);
DRWShadingGroup *grp = DRW_shgroup_create(checker_sh, psl->alpha_checker);
copy_v4_fl4(effects->color_checker_dark, 0.15f, 0.15f, 0.15f, 1.0f);
copy_v4_fl4(effects->color_checker_light, 0.2f, 0.2f, 0.2f, 1.0f);
DRW_shgroup_uniform_vec4(grp, "color1", effects->color_checker_dark, 1);
DRW_shgroup_uniform_vec4(grp, "color2", effects->color_checker_light, 1);
DRW_shgroup_uniform_int_copy(grp, "size", 8);
DRW_shgroup_call_add(grp, quad, NULL);
}
}
#if 0 /* Not required for now */
@ -491,6 +513,26 @@ void EEVEE_downsample_cube_buffer(EEVEE_Data *vedata, GPUTexture *texture_src, i
DRW_stats_group_end();
}
void EEVEE_draw_alpha_checker(EEVEE_Data *vedata)
{
EEVEE_PassList *psl = vedata->psl;
EEVEE_StorageList *stl = vedata->stl;
EEVEE_EffectsInfo *effects = stl->effects;
if ((effects->enabled_effects & EFFECT_ALPHA_CHECKER) != 0) {
float mat[4][4];
unit_m4(mat);
/* Fragile, rely on the fact that GPU_SHADER_2D_CHECKER
* only use the persmat. */
DRW_viewport_matrix_override_set(mat, DRW_MAT_PERS);
DRW_draw_pass(psl->alpha_checker);
DRW_viewport_matrix_override_unset(DRW_MAT_PERS);
}
}
void EEVEE_draw_effects(EEVEE_ViewLayerData *UNUSED(sldata), EEVEE_Data *vedata)
{
EEVEE_PassList *psl = vedata->psl;

View File

@ -311,6 +311,9 @@ static void eevee_draw_background(void *vedata)
GPU_framebuffer_bind(dfbl->default_fb);
DRW_transform_to_display(stl->effects->final_tx, true, use_render_settings);
/* Draw checkerboard with alpha under. */
EEVEE_draw_alpha_checker(vedata);
/* Debug : Output buffer to view. */
switch (G.debug_value) {
case 1:

View File

@ -246,6 +246,7 @@ typedef struct EEVEE_PassList {
struct DRWPass *color_downsample_cube_ps;
struct DRWPass *velocity_resolve;
struct DRWPass *taa_resolve;
struct DRWPass *alpha_checker;
/* HiZ */
struct DRWPass *minz_downlevel_ps;
@ -537,6 +538,7 @@ typedef enum EEVEE_EffectsFlag {
EFFECT_VELOCITY_BUFFER = (1 << 12), /* Not really an effect but a feature */
EFFECT_TAA_REPROJECT = (1 << 13), /* should be mutually exclusive with EFFECT_TAA */
EFFECT_DEPTH_DOUBLE_BUFFER = (1 << 14), /* Not really an effect but a feature */
EFFECT_ALPHA_CHECKER = (1 << 15), /* Not really an effect but a feature */
} EEVEE_EffectsFlag;
typedef struct EEVEE_EffectsInfo {
@ -598,6 +600,9 @@ typedef struct EEVEE_EffectsInfo {
struct GPUTexture *dof_coc;
struct GPUTexture *dof_blur;
struct GPUTexture *dof_blur_alpha;
/* Alpha Checker */
float color_checker_dark[4];
float color_checker_light[4];
/* Other */
float prev_persmat[4][4];
/* Bloom */
@ -1073,6 +1078,7 @@ void EEVEE_effects_cache_init(EEVEE_ViewLayerData *sldata, EEVEE_Data *vedata);
void EEVEE_create_minmax_buffer(EEVEE_Data *vedata, struct GPUTexture *depth_src, int layer);
void EEVEE_downsample_buffer(EEVEE_Data *vedata, struct GPUTexture *texture_src, int level);
void EEVEE_downsample_cube_buffer(EEVEE_Data *vedata, struct GPUTexture *texture_src, int level);
void EEVEE_draw_alpha_checker(EEVEE_Data *vedata);
void EEVEE_draw_effects(EEVEE_ViewLayerData *sldata, EEVEE_Data *vedata);
void EEVEE_effects_free(void);

View File

@ -5,6 +5,7 @@ uniform sampler2D colorBuffer;
uniform sampler2D depthBuffer;
uniform vec2 dofParams;
uniform bool unpremult;
#define dof_mul dofParams.x /* distance * aperturesize * invsensorsize */
#define dof_bias dofParams.y /* aperturesize * invsensorsize */
@ -241,8 +242,12 @@ void main(void)
fragColor = (far_col + near_col + focus_col) * inv_weight_sum;
# ifdef USE_ALPHA_DOF
/* Unpremult */
fragColor.rgb /= (fragColor.a > 0.0) ? fragColor.a : 1.0;
/* Sigh... viewport expect premult output but
* the final render output needs to be with
* associated alpha. */
if (unpremult) {
fragColor.rgb /= (fragColor.a > 0.0) ? fragColor.a : 1.0;
}
# endif
}

View File

@ -39,7 +39,6 @@ vec3 clip_to_aabb(vec3 color, vec3 minimum, vec3 maximum, vec3 average)
void main()
{
ivec2 texel = ivec2(gl_FragCoord.xy);
float depth = texelFetch(depthBuffer, texel, 0).r;
vec2 motion = texelFetch(velocityBuffer, texel, 0).rg;
/* Decode from unsigned normalized 16bit texture. */
@ -96,6 +95,9 @@ void main()
color_history = (out_of_view) ? color : color_history;
FragColor = safe_color(color_history);
/* There is some ghost issue if we use the alpha
* in the viewport. Overwritting alpha fixes it. */
FragColor.a = color.a;
}
#else