diff options
author | Brecht Van Lommel <brechtvanlommel@gmail.com> | 2019-06-07 18:49:58 +0300 |
---|---|---|
committer | Brecht Van Lommel <brechtvanlommel@gmail.com> | 2019-06-07 21:51:40 +0300 |
commit | fb03f50e069d66c99391e4796e1b9eaa2b4cc133 (patch) | |
tree | 10c761cf548c6d4aa9160eed7c2798a96b798ccb /source/blender/draw/engines/gpencil | |
parent | d05f27c7b95371db9e7e4e34a919a3623682a114 (diff) |
Fix T64625: Eevee image textures with alpha have dark edges
Now texture storage of images is defined by the alpha mode of the image. The
downside of this is that there can be artifacts near alpha edges where pixels
with zero alpha bleed in. It also adds more code complexity since image textures
are no longer all stored the same way.
This changes allows us to keep using sRGB texture formats, which have edge
darkening when stored with premultiplied alpha. Game engines seems to generally
do the same thing, and we want to be compatible with them.
Diffstat (limited to 'source/blender/draw/engines/gpencil')
4 files changed, 33 insertions, 11 deletions
diff --git a/source/blender/draw/engines/gpencil/gpencil_draw_utils.c b/source/blender/draw/engines/gpencil/gpencil_draw_utils.c index cd35395064b..9babd8cd058 100644 --- a/source/blender/draw/engines/gpencil/gpencil_draw_utils.c +++ b/source/blender/draw/engines/gpencil/gpencil_draw_utils.c @@ -467,6 +467,8 @@ static DRWShadingGroup *DRW_gpencil_shgroup_fill_create(GPENCIL_e_data *e_data, else { GPUTexture *texture = GPU_texture_from_blender(gp_style->ima, &iuser, GL_TEXTURE_2D); DRW_shgroup_uniform_texture(grp, "myTexture", texture); + DRW_shgroup_uniform_bool_copy( + grp, "myTexturePremultiplied", (image->alpha_mode == IMA_ALPHA_PREMUL)); stl->shgroups[id].texture_clamp = gp_style->flag & GP_STYLE_COLOR_TEX_CLAMP ? 1 : 0; DRW_shgroup_uniform_int(grp, "texture_clamp", &stl->shgroups[id].texture_clamp, 1); @@ -633,6 +635,8 @@ DRWShadingGroup *DRW_gpencil_shgroup_stroke_create(GPENCIL_e_data *e_data, else { GPUTexture *texture = GPU_texture_from_blender(gp_style->sima, &iuser, GL_TEXTURE_2D); DRW_shgroup_uniform_texture(grp, "myTexture", texture); + DRW_shgroup_uniform_bool_copy( + grp, "myTexturePremultiplied", (image->alpha_mode == IMA_ALPHA_PREMUL)); BKE_image_release_ibuf(image, ibuf, NULL); } @@ -789,6 +793,8 @@ static DRWShadingGroup *DRW_gpencil_shgroup_point_create(GPENCIL_e_data *e_data, else { GPUTexture *texture = GPU_texture_from_blender(gp_style->sima, &iuser, GL_TEXTURE_2D); DRW_shgroup_uniform_texture(grp, "myTexture", texture); + DRW_shgroup_uniform_bool_copy( + grp, "myTexturePremultiplied", (image->alpha_mode == IMA_ALPHA_PREMUL)); BKE_image_release_ibuf(image, ibuf, NULL); } diff --git a/source/blender/draw/engines/gpencil/shaders/gpencil_fill_frag.glsl b/source/blender/draw/engines/gpencil/shaders/gpencil_fill_frag.glsl index 192720a4e98..1fdfd05332e 100644 --- a/source/blender/draw/engines/gpencil/shaders/gpencil_fill_frag.glsl +++ b/source/blender/draw/engines/gpencil/shaders/gpencil_fill_frag.glsl @@ -19,6 +19,7 @@ uniform int drawmode; uniform float layer_opacity; uniform sampler2D myTexture; +uniform bool myTexturePremultiplied; uniform int texture_clamp; uniform int viewport_xray; @@ -99,11 +100,15 @@ float linearrgb_to_srgb(float c) } } -vec4 texture_read_as_srgb(sampler2D tex, vec2 co) +vec4 texture_read_as_srgb(sampler2D tex, bool premultiplied, vec2 co) { /* By convention image textures return scene linear colors, but * grease pencil still works in srgb. */ vec4 color = texture(tex, co); + /* Unpremultiply if stored multiplied, since straight alpha is expected by shaders. */ + if (premultiplied && !(color.a == 0.0 || color.a == 1.0)) { + color.rgb = color.rgb / color.a; + } color.r = linearrgb_to_srgb(color.r); color.g = linearrgb_to_srgb(color.g); color.b = linearrgb_to_srgb(color.b); @@ -118,8 +123,10 @@ void main() vec2 rot_tex = (matrot_tex * (texCoord_interp - t_center)) + t_center + texture_offset; vec4 tmp_color; tmp_color = (texture_clamp == 0) ? - texture_read_as_srgb(myTexture, rot_tex * texture_scale) : - texture_read_as_srgb(myTexture, clamp(rot_tex * texture_scale, 0.0, 1.0)); + texture_read_as_srgb( + myTexture, myTexturePremultiplied, rot_tex * texture_scale) : + texture_read_as_srgb( + myTexture, myTexturePremultiplied, clamp(rot_tex * texture_scale, 0.0, 1.0)); vec4 text_color = vec4(tmp_color[0], tmp_color[1], tmp_color[2], tmp_color[3] * texture_opacity); vec4 chesscolor; diff --git a/source/blender/draw/engines/gpencil/shaders/gpencil_point_frag.glsl b/source/blender/draw/engines/gpencil/shaders/gpencil_point_frag.glsl index 569a68679b4..7fed42aca0d 100644 --- a/source/blender/draw/engines/gpencil/shaders/gpencil_point_frag.glsl +++ b/source/blender/draw/engines/gpencil/shaders/gpencil_point_frag.glsl @@ -1,6 +1,7 @@ uniform int color_type; uniform int mode; uniform sampler2D myTexture; +uniform bool myTexturePremultiplied; uniform float gradient_f; uniform vec2 gradient_s; @@ -57,11 +58,15 @@ float linearrgb_to_srgb(float c) } } -vec4 texture_read_as_srgb(sampler2D tex, vec2 co) +vec4 texture_read_as_srgb(sampler2D tex, bool premultiplied, vec2 co) { /* By convention image textures return scene linear colors, but * grease pencil still works in srgb. */ vec4 color = texture(tex, co); + /* Unpremultiply if stored multiplied, since straight alpha is expected by shaders. */ + if (premultiplied && !(color.a == 0.0 || color.a == 1.0)) { + color.rgb = color.rgb / color.a; + } color.r = linearrgb_to_srgb(color.r); color.g = linearrgb_to_srgb(color.g); color.b = linearrgb_to_srgb(color.b); @@ -86,15 +91,13 @@ void main() } } - vec4 tmp_color = texture_read_as_srgb(myTexture, mTexCoord); - /* Solid */ if ((color_type == GPENCIL_COLOR_SOLID) || (no_texture)) { fragColor = mColor; } /* texture */ if ((color_type == GPENCIL_COLOR_TEXTURE) && (!no_texture)) { - vec4 text_color = texture_read_as_srgb(myTexture, mTexCoord); + vec4 text_color = texture_read_as_srgb(myTexture, myTexturePremultiplied, mTexCoord); if (mix_stroke_factor > 0.0) { fragColor.rgb = mix(text_color.rgb, colormix.rgb, mix_stroke_factor); fragColor.a = text_color.a; @@ -108,7 +111,7 @@ void main() } /* pattern */ if ((color_type == GPENCIL_COLOR_PATTERN) && (!no_texture)) { - vec4 text_color = texture_read_as_srgb(myTexture, mTexCoord); + vec4 text_color = texture_read_as_srgb(myTexture, myTexturePremultiplied, mTexCoord); fragColor = mColor; /* mult both alpha factor to use strength factor with color alpha limit */ fragColor.a = min(text_color.a * mColor.a, mColor.a); diff --git a/source/blender/draw/engines/gpencil/shaders/gpencil_stroke_frag.glsl b/source/blender/draw/engines/gpencil/shaders/gpencil_stroke_frag.glsl index 7d0ebe88aa4..bc703d2a078 100644 --- a/source/blender/draw/engines/gpencil/shaders/gpencil_stroke_frag.glsl +++ b/source/blender/draw/engines/gpencil/shaders/gpencil_stroke_frag.glsl @@ -1,5 +1,6 @@ uniform int color_type; uniform sampler2D myTexture; +uniform bool myTexturePremultiplied; uniform float gradient_f; @@ -37,11 +38,15 @@ float linearrgb_to_srgb(float c) } } -vec4 texture_read_as_srgb(sampler2D tex, vec2 co) +vec4 texture_read_as_srgb(sampler2D tex, bool premultiplied, vec2 co) { /* By convention image textures return scene linear colors, but * grease pencil still works in srgb. */ vec4 color = texture(tex, co); + /* Unpremultiply if stored multiplied, since straight alpha is expected by shaders. */ + if (premultiplied && !(color.a == 0.0 || color.a == 1.0)) { + color.rgb = color.rgb / color.a; + } color.r = linearrgb_to_srgb(color.r); color.g = linearrgb_to_srgb(color.g); color.b = linearrgb_to_srgb(color.b); @@ -68,10 +73,11 @@ void main() /* texture for endcaps */ vec4 text_color; if (uvfac[1] == ENDCAP) { - text_color = texture_read_as_srgb(myTexture, vec2(mTexCoord.x, mTexCoord.y)); + text_color = texture_read_as_srgb( + myTexture, myTexturePremultiplied, vec2(mTexCoord.x, mTexCoord.y)); } else { - text_color = texture_read_as_srgb(myTexture, mTexCoord); + text_color = texture_read_as_srgb(myTexture, myTexturePremultiplied, mTexCoord); } /* texture */ |