[Bf-blender-cvs] [42a5bc3b03f] uvimage-editor-drawing: Use premultiplied alpha

Jeroen Bakker noreply at git.blender.org
Mon Aug 17 09:33:47 CEST 2020


Commit: 42a5bc3b03faba868b515dec2507aeb5c29095fe
Author: Jeroen Bakker
Date:   Mon Aug 17 09:33:16 2020 +0200
Branches: uvimage-editor-drawing
https://developer.blender.org/rB42a5bc3b03faba868b515dec2507aeb5c29095fe

Use premultiplied alpha

===================================================================

M	source/blender/draw/engines/editors/editors_image.c
M	source/blender/draw/engines/editors/shaders/editors_image_frag.glsl
M	source/blender/draw/engines/overlay2d/shaders/overlay2d_background_frag.glsl

===================================================================

diff --git a/source/blender/draw/engines/editors/editors_image.c b/source/blender/draw/engines/editors/editors_image.c
index 66e36c42b21..d1ae4566f5f 100644
--- a/source/blender/draw/engines/editors/editors_image.c
+++ b/source/blender/draw/engines/editors/editors_image.c
@@ -112,6 +112,7 @@ static void editors_image_cache_image(EDITORS_PassList *psl,
     static float shuffle[4] = {1.0f, 1.0f, 1.0f, 1.0f};
     int draw_flags = 0;
     static float far_near[2] = {100.0f, 0.0f};
+    const bool use_premul_alpha = ima->alpha_mode == IMA_ALPHA_PREMUL;
 
     if (scene->camera && scene->camera->type == OB_CAMERA) {
       far_near[1] = ((Camera *)scene->camera->data)->clip_start;
@@ -156,6 +157,7 @@ static void editors_image_cache_image(EDITORS_PassList *psl,
     DRW_shgroup_uniform_vec4_copy(shgrp, "color", color);
     DRW_shgroup_uniform_vec4_copy(shgrp, "shuffle", shuffle);
     DRW_shgroup_uniform_int_copy(shgrp, "drawFlags", draw_flags);
+    DRW_shgroup_uniform_bool_copy(shgrp, "imgPremultiplied", use_premul_alpha);
 
     DRW_shgroup_call_instances_with_attrs(
         shgrp, NULL, e_data.gpu_batch_image, e_data.gpu_batch_instances);
@@ -251,7 +253,7 @@ void EDITORS_image_cache_init(EDITORS_Data *vedata)
     /* Write depth is needed for background rendering. Near depth is used for transparency
      * checker and Far depth is used for indicating the image size. */
     DRWState state = DRW_STATE_WRITE_COLOR | DRW_STATE_WRITE_DEPTH | DRW_STATE_DEPTH_ALWAYS |
-                     DRW_STATE_BLEND_ALPHA;
+                     DRW_STATE_BLEND_ALPHA_PREMUL;
     psl->image_pass = DRW_pass_create("Image", state);
   }
 
diff --git a/source/blender/draw/engines/editors/shaders/editors_image_frag.glsl b/source/blender/draw/engines/editors/shaders/editors_image_frag.glsl
index ea532c5d45d..677537b0edc 100644
--- a/source/blender/draw/engines/editors/shaders/editors_image_frag.glsl
+++ b/source/blender/draw/engines/editors/shaders/editors_image_frag.glsl
@@ -65,11 +65,13 @@ void main()
   }
   else {
     vec2 uvs_clamped = clamp(uvs, 0.0, 1.0);
-    tex_color = texture_read_as_linearrgb(imageTexture, imgPremultiplied, uvs_clamped);
+    tex_color = texture(imageTexture, uvs_clamped);
   }
 
   if ((drawFlags & SIMA_DRAW_FLAG_APPLY_ALPHA) != 0) {
-    tex_color.rgb *= tex_color.a;
+    if (!imgPremultiplied && tex_color.a != 0.0 && tex_color.a != 1.0) {
+      tex_color.rgb *= tex_color.a;
+    }
   }
   if ((drawFlags & SIMA_DRAW_FLAG_DEPTH) != 0) {
     tex_color = smoothstep(FAR_DISTANCE, NEAR_DISTANCE, tex_color);
diff --git a/source/blender/draw/engines/overlay2d/shaders/overlay2d_background_frag.glsl b/source/blender/draw/engines/overlay2d/shaders/overlay2d_background_frag.glsl
index 8808026efdf..21f49628666 100644
--- a/source/blender/draw/engines/overlay2d/shaders/overlay2d_background_frag.glsl
+++ b/source/blender/draw/engines/overlay2d/shaders/overlay2d_background_frag.glsl
@@ -29,7 +29,12 @@ void main()
    * This removes the alpha channel and put the background behind reference images
    * while masking the reference images by the render alpha.
    */
-  float alpha = texture(colorBuffer, uvcoordsvar.st).a;
+  vec4 color = texture(colorBuffer, uvcoordsvar.st);
+  float alpha = color.a;
+  /* color is premultiplied. extract alpha from emission when alpha is 0. */
+  if (alpha == 0.0) {
+    alpha = min((color.r + color.g + color.b) / 3.0, 1.0);
+  }
   float depth = texture(depthBuffer, uvcoordsvar.st).r;
 
   vec3 bg_col;



More information about the Bf-blender-cvs mailing list