[Bf-blender-cvs] [a580934b7a0] greasepencil-object: GPencil: Fix depth being written outside of masks boundaries

Clément Foucault noreply at git.blender.org
Wed Feb 12 03:04:23 CET 2020


Commit: a580934b7a08d4f2b232f696085a6c0b5000e220
Author: Clément Foucault
Date:   Wed Feb 12 03:04:14 2020 +0100
Branches: greasepencil-object
https://developer.blender.org/rBa580934b7a08d4f2b232f696085a6c0b5000e220

GPencil: Fix depth being written outside of masks boundaries

This fix requires yet another texture sample in the fragment shader.
Not so good for performance but from code simplicity perspective it will
do for now.

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

M	source/blender/draw/engines/gpencil/gpencil_engine.c
M	source/blender/draw/engines/gpencil/shaders/gpencil_frag.glsl

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

diff --git a/source/blender/draw/engines/gpencil/gpencil_engine.c b/source/blender/draw/engines/gpencil/gpencil_engine.c
index b646ee5829f..c869d998839 100644
--- a/source/blender/draw/engines/gpencil/gpencil_engine.c
+++ b/source/blender/draw/engines/gpencil/gpencil_engine.c
@@ -465,6 +465,7 @@ static void gp_layer_cache_populate(bGPDlayer *gpl,
   iter->ubo_lights = (use_lights) ? iter->pd->global_light_pool->ubo :
                                     iter->pd->shadeless_light_pool->ubo;
   const bool is_in_front = (iter->ob->dtx & OB_DRAWXRAY);
+  const bool is_masked = tgp_layer->mask_bits != NULL;
 
   bool overide_vertcol = (iter->pd->v3d_color_type != -1);
   bool is_vert_col_mode = (iter->pd->v3d_color_type == V3D_SHADING_VERTEX_COLOR) ||
@@ -474,6 +475,7 @@ static void gp_layer_cache_populate(bGPDlayer *gpl,
 
   /* Check if object is defined in front. */
   GPUTexture *depth_tex = (is_in_front) ? iter->pd->dummy_tx : iter->pd->scene_depth_tx;
+  GPUTexture **mask_tex = (is_masked) ? &iter->pd->mask_tx : &iter->pd->dummy_tx;
 
   struct GPUShader *sh = GPENCIL_shader_geometry_get();
   iter->grp = DRW_shgroup_create(sh, tgp_layer->geom_ps);
@@ -482,6 +484,7 @@ static void gp_layer_cache_populate(bGPDlayer *gpl,
   DRW_shgroup_uniform_texture(iter->grp, "gpFillTexture", iter->tex_fill);
   DRW_shgroup_uniform_texture(iter->grp, "gpStrokeTexture", iter->tex_stroke);
   DRW_shgroup_uniform_texture(iter->grp, "gpSceneDepthTexture", depth_tex);
+  DRW_shgroup_uniform_texture_ref(iter->grp, "gpMaskTexture", mask_tex);
   DRW_shgroup_uniform_int_copy(iter->grp, "gpMaterialOffset", iter->mat_ofs);
   DRW_shgroup_uniform_bool_copy(iter->grp, "strokeOrder3d", is_stroke_order_3d);
   DRW_shgroup_uniform_vec3_copy(iter->grp, "gpNormal", iter->tgp_ob->plane_normal);
diff --git a/source/blender/draw/engines/gpencil/shaders/gpencil_frag.glsl b/source/blender/draw/engines/gpencil/shaders/gpencil_frag.glsl
index 43a9286f7d5..8f528dfb7db 100644
--- a/source/blender/draw/engines/gpencil/shaders/gpencil_frag.glsl
+++ b/source/blender/draw/engines/gpencil/shaders/gpencil_frag.glsl
@@ -2,6 +2,7 @@
 uniform sampler2D gpFillTexture;
 uniform sampler2D gpStrokeTexture;
 uniform sampler2D gpSceneDepthTexture;
+uniform sampler2D gpMaskTexture;
 uniform vec3 gpNormal;
 
 layout(location = 0) out vec4 fragColor;
@@ -104,6 +105,13 @@ void main()
     discard;
   }
 
+  /* FIXME(fclem) Grrr. This is bad for performance but it's the easiest way to not get
+   * depth written where the mask obliterate the layer. */
+  float mask = texture(gpMaskTexture, uvs).r;
+  if (mask < 0.001) {
+    discard;
+  }
+
   /* We override the fragment depth using the fragment shader to ensure a constant value.
    * This has a cost as the depth test cannot happen early.
    * We could do this in the vertex shader but then perspective interpolation of uvs and



More information about the Bf-blender-cvs mailing list