[Bf-blender-cvs] [2d751d44ee8] greasepencil-refactor: GPencil: Refactor: Base implementation of VFX for gpencil

Clément Foucault noreply at git.blender.org
Wed Dec 18 03:14:21 CET 2019


Commit: 2d751d44ee86a9ab5f1bed27ca9fc10293d69890
Author: Clément Foucault
Date:   Mon Dec 16 18:59:55 2019 +0100
Branches: greasepencil-refactor
https://developer.blender.org/rB2d751d44ee86a9ab5f1bed27ca9fc10293d69890

GPencil: Refactor: Base implementation of VFX for gpencil

This new implementation is clearer and also process the colored alpha
compoment to preserve blending informations.

This is the base implementation containing a placeholder blur.

All the real effect implementation will follow in separate commits.

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

M	source/blender/draw/CMakeLists.txt
M	source/blender/draw/engines/gpencil/gpencil_engine.c
M	source/blender/draw/engines/gpencil/gpencil_engine.h
M	source/blender/draw/engines/gpencil/gpencil_shader.c
M	source/blender/draw/engines/gpencil/gpencil_shader_fx.c
A	source/blender/draw/engines/gpencil/shaders/gpencil_vfx_frag.glsl

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

diff --git a/source/blender/draw/CMakeLists.txt b/source/blender/draw/CMakeLists.txt
index e92bb5fea68..885213dccb4 100644
--- a/source/blender/draw/CMakeLists.txt
+++ b/source/blender/draw/CMakeLists.txt
@@ -282,6 +282,7 @@ data_to_c_simple(engines/gpencil/shaders/gpencil_composite_frag.glsl SRC)
 data_to_c_simple(engines/gpencil/shaders/gpencil_layer_blend_frag.glsl SRC)
 data_to_c_simple(engines/gpencil/shaders/gpencil_layer_mask_frag.glsl SRC)
 data_to_c_simple(engines/gpencil/shaders/gpencil_depth_merge_frag.glsl SRC)
+data_to_c_simple(engines/gpencil/shaders/gpencil_vfx_frag.glsl SRC)
 
 data_to_c_simple(engines/gpencil/shaders/gpencil_fill_vert.glsl SRC)
 data_to_c_simple(engines/gpencil/shaders/gpencil_fill_frag.glsl SRC)
diff --git a/source/blender/draw/engines/gpencil/gpencil_engine.c b/source/blender/draw/engines/gpencil/gpencil_engine.c
index a1d643d170a..418e0944140 100644
--- a/source/blender/draw/engines/gpencil/gpencil_engine.c
+++ b/source/blender/draw/engines/gpencil/gpencil_engine.c
@@ -309,6 +309,7 @@ static void GPENCIL_engine_init_new(void *ved)
   stl->pd->tobjects.last = NULL;
   stl->pd->draw_depth_only = !DRW_state_is_fbo();
   stl->pd->scene_depth_tx = stl->pd->draw_depth_only ? txl->dummy_texture : dtxl->depth;
+  stl->pd->is_render = true; /* TODO */
 
   float viewmatinv[4][4];
   DRW_view_viewmat_get(NULL, viewmatinv, true);
@@ -358,6 +359,9 @@ static void GPENCIL_engine_free(void)
   DRW_SHADER_FREE_SAFE(e_data.layer_mask_sh);
   DRW_SHADER_FREE_SAFE(e_data.depth_merge_sh);
 
+  DRW_SHADER_FREE_SAFE(e_data.fx_blur_sh);
+  DRW_SHADER_FREE_SAFE(e_data.fx_composite_sh);
+
   DRW_TEXTURE_FREE_SAFE(e_data.gpencil_blank_texture);
 
   GPU_VERTBUF_DISCARD_SAFE(e_data.quad);
@@ -919,7 +923,7 @@ static void GPENCIL_cache_populate_new(void *ved, Object *ob)
     gpencil_object_visible_stroke_iter(
         ob, gp_layer_cache_populate, gp_stroke_cache_populate, &iter);
 
-    /* TODO VFX */
+    gpencil_vfx_cache_populate(vedata, ob, iter.tgp_ob);
   }
 }
 
@@ -1052,7 +1056,7 @@ static void GPENCIL_cache_finish_new(void *ved)
     /* TODO(fclem) Detect use of layers and vfx. */
     bool use_layer_fb = true;
     bool use_mask_fb = true;
-    bool use_object_fb = false;
+    bool use_object_fb = true;
 
     const float *size = DRW_viewport_size_get();
     pd->depth_tx = DRW_texture_pool_query_2d(
@@ -1089,7 +1093,7 @@ static void GPENCIL_cache_finish_new(void *ved)
       pd->reveal_object_tx = DRW_texture_pool_query_2d(
           size[0], size[1], GPU_R11F_G11F_B10F, &draw_engine_gpencil_type);
 
-      GPU_framebuffer_ensure_config(&fbl->layer_fb,
+      GPU_framebuffer_ensure_config(&fbl->object_fb,
                                     {
                                         GPU_ATTACHMENT_TEXTURE(pd->depth_tx),
                                         GPU_ATTACHMENT_TEXTURE(pd->color_object_tx),
@@ -1347,15 +1351,15 @@ static void GPENCIL_draw_scene_new(void *ved)
   for (GPENCIL_tObject *ob = pd->tobjects.first; ob; ob = ob->next) {
     DRW_stats_group_start("GPencil Object");
 
-    GPUFrameBuffer *fb_object = fbl->gpencil_fb;
-
-    if (ob->vfx.first) {
-      /* TODO vfx */
-    }
+    GPUFrameBuffer *fb_object = (ob->vfx.first) ? fbl->object_fb : fbl->gpencil_fb;
 
     GPU_framebuffer_bind(fb_object);
     GPU_framebuffer_clear_depth_stencil(fb_object, ob->is_drawmode3d ? 1.0f : 0.0f, 0x00);
 
+    if (ob->vfx.first) {
+      GPU_framebuffer_multi_clear(fb_object, clear_cols);
+    }
+
     for (GPENCIL_tLayer *layer = ob->layers.first; layer; layer = layer->next) {
       if (layer->blend_ps) {
         GPU_framebuffer_bind(fbl->layer_fb);
@@ -1387,8 +1391,9 @@ static void GPENCIL_draw_scene_new(void *ved)
       }
     }
 
-    if (ob->vfx.first) {
-      /* TODO vfx */
+    for (GPENCIL_tVfx *vfx = ob->vfx.first; vfx; vfx = vfx->next) {
+      GPU_framebuffer_bind(*(vfx->target_fb));
+      DRW_draw_pass(vfx->vfx_ps);
     }
 
     pd->object_depth = ob->camera_z - pd->camera_z_offset;
diff --git a/source/blender/draw/engines/gpencil/gpencil_engine.h b/source/blender/draw/engines/gpencil/gpencil_engine.h
index 8ffb1721b0a..ad0ce84fb56 100644
--- a/source/blender/draw/engines/gpencil/gpencil_engine.h
+++ b/source/blender/draw/engines/gpencil/gpencil_engine.h
@@ -169,8 +169,9 @@ typedef struct GPENCIL_ViewLayerData {
 typedef struct GPENCIL_tVfx {
   /** Linklist */
   struct GPENCIL_tVfx *next;
-  /* List of passes for this fx. */
-  DRWPass *vfx_ps[4];
+  DRWPass *vfx_ps;
+  /* Framebuffer reference since it may not be allocated yet. */
+  GPUFrameBuffer **target_fb;
 } GPENCIL_tVfx;
 
 typedef struct GPENCIL_tLayer {
@@ -431,6 +432,8 @@ typedef struct GPENCIL_PrivateData {
   GPUTexture *scene_depth_tx;
   /* Current frame */
   int cfra;
+  /* If we are rendering for final render (F12). */
+  bool is_render;
   /* True in selection and auto_depth drawing */
   bool draw_depth_only;
   /* Used by the depth merge step. */
@@ -463,6 +466,9 @@ typedef struct GPENCIL_e_data {
   struct GPUShader *layer_mask_sh;
   /* Merge the final object depth to the depth buffer. */
   struct GPUShader *depth_merge_sh;
+  /* Effects. */
+  struct GPUShader *fx_blur_sh;
+  struct GPUShader *fx_composite_sh;
 
   /* general drawing shaders */
   struct GPUShader *gpencil_fill_sh;
@@ -692,12 +698,16 @@ void gpencil_fx_draw(struct GPENCIL_e_data *e_data,
                      struct GPENCIL_Data *vedata,
                      struct tGPencilObjectCache *cache_ob);
 
+void gpencil_vfx_cache_populate(GPENCIL_Data *vedata, Object *ob, GPENCIL_tObject *tgp_ob);
+
 /* Shaders */
 struct GPUShader *GPENCIL_shader_geometry_get(GPENCIL_e_data *e_data);
 struct GPUShader *GPENCIL_shader_composite_get(GPENCIL_e_data *e_data);
 struct GPUShader *GPENCIL_shader_layer_blend_get(GPENCIL_e_data *e_data);
 struct GPUShader *GPENCIL_shader_layer_mask_get(GPENCIL_e_data *e_data);
 struct GPUShader *GPENCIL_shader_depth_merge_get(GPENCIL_e_data *e_data);
+struct GPUShader *GPENCIL_shader_fx_composite_get(GPENCIL_e_data *e_data);
+struct GPUShader *GPENCIL_shader_fx_blur_get(GPENCIL_e_data *e_data);
 
 /* main functions */
 void GPENCIL_engine_init(void *vedata);
diff --git a/source/blender/draw/engines/gpencil/gpencil_shader.c b/source/blender/draw/engines/gpencil/gpencil_shader.c
index edb94d983d5..b98819e06fb 100644
--- a/source/blender/draw/engines/gpencil/gpencil_shader.c
+++ b/source/blender/draw/engines/gpencil/gpencil_shader.c
@@ -30,6 +30,7 @@ extern char datatoc_gpencil_composite_frag_glsl[];
 extern char datatoc_gpencil_layer_blend_frag_glsl[];
 extern char datatoc_gpencil_layer_mask_frag_glsl[];
 extern char datatoc_gpencil_depth_merge_frag_glsl[];
+extern char datatoc_gpencil_vfx_frag_glsl[];
 
 extern char datatoc_common_colormanagement_lib_glsl[];
 extern char datatoc_common_fullscreen_vert_glsl[];
@@ -108,3 +109,23 @@ struct GPUShader *GPENCIL_shader_depth_merge_get(GPENCIL_e_data *e_data)
   }
   return e_data->depth_merge_sh;
 }
+
+/* ------- FX Shaders --------- */
+
+struct GPUShader *GPENCIL_shader_fx_composite_get(GPENCIL_e_data *e_data)
+{
+  if (!e_data->fx_composite_sh) {
+    e_data->fx_composite_sh = DRW_shader_create_fullscreen(datatoc_gpencil_vfx_frag_glsl,
+                                                           "#define COMPOSITE\n");
+  }
+  return e_data->fx_composite_sh;
+}
+
+struct GPUShader *GPENCIL_shader_fx_blur_get(GPENCIL_e_data *e_data)
+{
+  if (!e_data->fx_blur_sh) {
+    e_data->fx_blur_sh = DRW_shader_create_fullscreen(datatoc_gpencil_vfx_frag_glsl,
+                                                      "#define BLUR\n");
+  }
+  return e_data->fx_blur_sh;
+}
\ No newline at end of file
diff --git a/source/blender/draw/engines/gpencil/gpencil_shader_fx.c b/source/blender/draw/engines/gpencil/gpencil_shader_fx.c
index f6a62e0d472..a5264c24d09 100644
--- a/source/blender/draw/engines/gpencil/gpencil_shader_fx.c
+++ b/source/blender/draw/engines/gpencil/gpencil_shader_fx.c
@@ -26,6 +26,9 @@
 
 #include "BKE_gpencil.h"
 
+#include "BLI_link_utils.h"
+#include "BLI_memblock.h"
+
 #include "DRW_render.h"
 
 #include "BKE_camera.h"
@@ -1062,3 +1065,123 @@ void gpencil_fx_draw(GPENCIL_e_data *UNUSED(e_data),
     }
   }
 }
+
+/* ------------- Refactored Code ------------ */
+
+typedef struct gpIterVfxData {
+  GPENCIL_PrivateData *pd;
+  GPENCIL_tObject *tgp_ob;
+  GPUFrameBuffer **target_fb;
+  GPUFrameBuffer **source_fb;
+  GPUTexture **target_color_tx;
+  GPUTexture **source_color_tx;
+  GPUTexture **target_reveal_tx;
+  GPUTexture **source_reveal_tx;
+} gpIterVfxData;
+
+static DRWShadingGroup *gpencil_vfx_pass_create(const char *name,
+                                                DRWState state,
+                                                gpIterVfxData *iter,
+                                                GPUShader *sh)
+{
+  DRWPass *pass = DRW_pass_create(name, state);
+  DRWShadingGroup *grp = DRW_shgroup_create(sh, pass);
+  DRW_shgroup_uniform_texture_ref(grp, "colorBuf", iter->source_color_tx);
+  DRW_shgroup_uniform_texture_ref(grp, "revealBuf", iter->source_reveal_tx);
+
+  GPENCIL_tVfx *tgp_vfx = BLI_memblock_alloc(iter->pd->gp_vfx_pool);
+  tgp_vfx->target_fb = iter->target_fb;
+  tgp_vfx->vfx_ps = pass;
+
+  SWAP(GPUFrameBuffer **, iter->target_fb, iter->source_fb);
+  SWAP(GPUTexture **, iter->target_color_tx, iter->source_color_tx);
+  SWAP(GPUTexture **, iter->target_reveal_tx, iter->source_reveal_tx);
+
+  BLI_LINKS_APPEND(&iter->tgp_ob->vfx, tgp_vfx);
+
+  return grp;
+}
+
+static void gpencil_vfx_blur(BlurShaderFxData *fx, Object *UNUSED(ob), gpIterVfxData *iter)
+{
+  DRWShadingGroup *grp;
+
+  GPUShader *sh = GPENCIL_shader_fx_blur_get(&en_data);
+
+  DRWState state = DRW_STATE_WRITE_COLOR;
+  grp = gpencil_vfx_pass_create("Fx Blur H", state, iter, sh);
+  DRW_shgroup_uniform_vec2_copy(grp, "offset", (float[2]){fx->radius[0], 0.0f});
+  DRW_shgroup_call_procedural_triangles(grp, NULL, 1);
+
+  grp = gpencil_vfx_pass_create("Fx Blur V", state, iter, sh);
+  DRW_shgroup_uniform_vec2_copy(grp, "offset", (float[2]){0.0f, fx->radius[1]});
+  DRW_shgroup_call_procedural_triangles(grp, NULL, 1);
+}
+
+void gpencil_vfx_cache_populate(GPENCIL_Data *vedata, Object *ob, GPENCIL_tOb

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list