[Bf-blender-cvs] [b9bcc12eb9d] greasepencil-refactor: GPencil: Refactor: Add depth test in the fragment shader

Clément Foucault noreply at git.blender.org
Fri Dec 13 21:25:19 CET 2019


Commit: b9bcc12eb9d6ca646350f80bfdfdd7c2714e96d0
Author: Clément Foucault
Date:   Fri Dec 13 21:24:12 2019 +0100
Branches: greasepencil-refactor
https://developer.blender.org/rBb9bcc12eb9d6ca646350f80bfdfdd7c2714e96d0

GPencil: Refactor: Add depth test in the fragment shader

Now the GPencil object can correctly intersect the scene.

Previously (old implementation) 2D GPencil objects would poke through
in front meshes in certain situations (depending on stroke order). This is
now fixed.

This does not fix the intersection with the overlays.

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

M	source/blender/draw/engines/gpencil/gpencil_engine.c
M	source/blender/draw/engines/gpencil/gpencil_engine.h
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 ff2f7e883fe..5b50dfc3a8a 100644
--- a/source/blender/draw/engines/gpencil/gpencil_engine.c
+++ b/source/blender/draw/engines/gpencil/gpencil_engine.c
@@ -281,6 +281,7 @@ static void GPENCIL_engine_init_new(void *ved)
   GPENCIL_Data *vedata = (GPENCIL_Data *)ved;
   GPENCIL_StorageList *stl = vedata->stl;
   GPENCIL_TextureList *txl = vedata->txl;
+  DefaultTextureList *dtxl = DRW_viewport_texture_list_get();
 
   if (!stl->pd) {
     stl->pd = MEM_callocN(sizeof(GPENCIL_PrivateData), "GPENCIL_PrivateData");
@@ -308,6 +309,7 @@ static void GPENCIL_engine_init_new(void *ved)
   stl->pd->last_material_pool = NULL;
   stl->pd->tobjects.first = NULL;
   stl->pd->tobjects.last = NULL;
+  stl->pd->scene_depth_tx = dtxl->depth;
 
   float viewmatinv[4][4];
   DRW_view_viewmat_get(NULL, viewmatinv, true);
@@ -803,6 +805,7 @@ static void gp_layer_cache_populate(bGPDlayer *gpl,
   DRW_shgroup_uniform_block(iter->grp, "gpMaterialBlock", ubo_mat);
   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", iter->pd->scene_depth_tx);
   DRW_shgroup_uniform_bool_copy(iter->grp, "strokeOrder3D", is_stroke_order_3D);
   DRW_shgroup_uniform_vec4_copy(iter->grp, "gpModelMatrix[0]", iter->ob->obmat[0]);
   DRW_shgroup_uniform_vec4_copy(iter->grp, "gpModelMatrix[1]", iter->ob->obmat[1]);
diff --git a/source/blender/draw/engines/gpencil/gpencil_engine.h b/source/blender/draw/engines/gpencil/gpencil_engine.h
index d4ee4260f6d..8f8eb9289c4 100644
--- a/source/blender/draw/engines/gpencil/gpencil_engine.h
+++ b/source/blender/draw/engines/gpencil/gpencil_engine.h
@@ -418,6 +418,8 @@ typedef struct GPENCIL_PrivateData {
   GPUTexture *reveal_tx;
   GPUTexture *reveal_layer_tx;
   GPUTexture *reveal_object_tx;
+  /* Pointer to dtxl->depth */
+  GPUTexture *scene_depth_tx;
   /* Current frame */
   int cfra;
   /* Used for computing object distance to camera. */
diff --git a/source/blender/draw/engines/gpencil/shaders/gpencil_frag.glsl b/source/blender/draw/engines/gpencil/shaders/gpencil_frag.glsl
index bb84d0fe930..56adb099af1 100644
--- a/source/blender/draw/engines/gpencil/shaders/gpencil_frag.glsl
+++ b/source/blender/draw/engines/gpencil/shaders/gpencil_frag.glsl
@@ -1,6 +1,7 @@
 
 uniform sampler2D gpFillTexture;
 uniform sampler2D gpStrokeTexture;
+uniform sampler2D gpSceneDepthTexture;
 
 in vec4 finalColorMul;
 in vec4 finalColorAdd;
@@ -53,6 +54,12 @@ void main()
     discard;
   }
 
+  /* Manual depth test */
+  float scene_depth = texelFetch(gpSceneDepthTexture, ivec2(gl_FragCoord.xy), 0).r;
+  if (gl_FragCoord.z > scene_depth) {
+    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