[Bf-blender-cvs] [41cef139adc] greasepencil-refactor: GPencil: Refactor: Move batch cache functions to DRW module

Clément Foucault noreply at git.blender.org
Tue Jan 7 15:31:25 CET 2020


Commit: 41cef139adc7ad61252930f355ada9f814d50a28
Author: Clément Foucault
Date:   Mon Jan 6 11:42:36 2020 +0100
Branches: greasepencil-refactor
https://developer.blender.org/rB41cef139adc7ad61252930f355ada9f814d50a28

GPencil: Refactor: Move batch cache functions to DRW module

This is in preparation of drawing the overlays.

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

M	source/blender/draw/CMakeLists.txt
M	source/blender/draw/engines/gpencil/gpencil_cache_utils.c
M	source/blender/draw/engines/gpencil/gpencil_draw_cache_impl.c
M	source/blender/draw/engines/gpencil/gpencil_engine.c
M	source/blender/draw/engines/gpencil/gpencil_engine.h
M	source/blender/draw/intern/draw_cache.c
M	source/blender/draw/intern/draw_cache.h
A	source/blender/draw/intern/draw_cache_impl_gpencil.c

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

diff --git a/source/blender/draw/CMakeLists.txt b/source/blender/draw/CMakeLists.txt
index 3434456c685..5c2cc1ce8a6 100644
--- a/source/blender/draw/CMakeLists.txt
+++ b/source/blender/draw/CMakeLists.txt
@@ -52,6 +52,7 @@ set(SRC
   intern/draw_cache_extract_mesh.c
   intern/draw_cache_impl_curve.c
   intern/draw_cache_impl_displist.c
+  intern/draw_cache_impl_gpencil.c
   intern/draw_cache_impl_lattice.c
   intern/draw_cache_impl_mesh.c
   intern/draw_cache_impl_metaball.c
diff --git a/source/blender/draw/engines/gpencil/gpencil_cache_utils.c b/source/blender/draw/engines/gpencil/gpencil_cache_utils.c
index b7c3f37df8c..0114d6e059d 100644
--- a/source/blender/draw/engines/gpencil/gpencil_cache_utils.c
+++ b/source/blender/draw/engines/gpencil/gpencil_cache_utils.c
@@ -517,38 +517,4 @@ GpencilBatchCache *gpencil_batch_cache_get(Object *ob, int cfra)
   else {
     return cache;
   }
-}
-
-/* set cache as dirty */
-void DRW_gpencil_batch_cache_dirty_tag(bGPdata *gpd)
-{
-  gpd->flag |= GP_DATA_CACHE_IS_DIRTY;
-}
-
-/* free batch cache */
-void DRW_gpencil_batch_cache_free(bGPdata *UNUSED(gpd))
-{
-  return;
-}
-
-/* wrapper to clear cache */
-void DRW_gpencil_freecache(struct Object *ob)
-{
-  if ((ob) && (ob->type == OB_GPENCIL)) {
-    gpencil_batch_cache_clear(ob->runtime.gpencil_cache);
-    MEM_SAFE_FREE(ob->runtime.gpencil_cache);
-    bGPdata *gpd = (bGPdata *)ob->data;
-    if (gpd) {
-      gpd->flag |= GP_DATA_CACHE_IS_DIRTY;
-    }
-  }
-
-  /* clear all frames evaluated data */
-  for (int i = 0; i < ob->runtime.gpencil_tot_layers; i++) {
-    bGPDframe *gpf_eval = &ob->runtime.gpencil_evaluated_frames[i];
-    BKE_gpencil_free_frame_runtime_data(gpf_eval);
-  }
-
-  ob->runtime.gpencil_tot_layers = 0;
-  MEM_SAFE_FREE(ob->runtime.gpencil_evaluated_frames);
-}
+}
\ No newline at end of file
diff --git a/source/blender/draw/engines/gpencil/gpencil_draw_cache_impl.c b/source/blender/draw/engines/gpencil/gpencil_draw_cache_impl.c
index cb2ae1db374..d90f2b20980 100644
--- a/source/blender/draw/engines/gpencil/gpencil_draw_cache_impl.c
+++ b/source/blender/draw/engines/gpencil/gpencil_draw_cache_impl.c
@@ -43,314 +43,6 @@
 
 #include "gpencil_engine.h"
 
-/* -------------------------------------------------------------------- */
-/** \name Dummy vbos
- *
- * We need a dummy vbo containing the vertex count to draw instances ranges.
- *
- * \{ */
-
-static GPUVertBuf *gpencil_dummy_buffer_get(void)
-{
-  if (en_data.quad == NULL) {
-    GPUVertFormat format = {0};
-    GPU_vertformat_attr_add(&format, "dummy", GPU_COMP_U8, 1, GPU_FETCH_INT);
-    en_data.quad = GPU_vertbuf_create_with_format(&format);
-    GPU_vertbuf_data_alloc(en_data.quad, 4);
-  }
-  return en_data.quad;
-}
-
-/** \} */
-
-/* -------------------------------------------------------------------- */
-/** \name Vertex Formats.
- * \{ */
-
-/* MUST match the format below. */
-typedef struct gpStrokeVert {
-  /** Mat is float because we need to pack other float attribs with it. */
-  float mat, strength, stroke_id, point_id;
-  /** Position and thickness packed in the same attribute. */
-  float pos[3], thickness;
-  float col[4];
-  float uv_fill[2], u_stroke, v_rot;
-} gpStrokeVert;
-
-static GPUVertFormat *gpencil_stroke_format(void)
-{
-  static GPUVertFormat format = {0};
-  if (format.attr_len == 0) {
-    GPU_vertformat_attr_add(&format, "ma", GPU_COMP_F32, 4, GPU_FETCH_FLOAT);
-    GPU_vertformat_attr_add(&format, "pos", GPU_COMP_F32, 4, GPU_FETCH_FLOAT);
-    GPU_vertformat_attr_add(&format, "col", GPU_COMP_F32, 4, GPU_FETCH_FLOAT);
-    GPU_vertformat_attr_add(&format, "uv", GPU_COMP_F32, 4, GPU_FETCH_FLOAT);
-    /* IMPORTANT: This means having only 4 attributes to fit into opengl limit of 16 attrib. */
-    GPU_vertformat_multiload_enable(&format, 4);
-  }
-  return &format;
-}
-
-/** \} */
-
-/* -------------------------------------------------------------------- */
-/** \name Vertex Buffers.
- * \{ */
-
-typedef struct gpIterData {
-  bGPdata *gpd;
-  gpStrokeVert *verts;
-  GPUIndexBufBuilder ibo;
-  int vert_len;
-  int tri_len;
-} gpIterData;
-
-static int gpencil_stroke_is_cyclic(const bGPDstroke *gps)
-{
-  return ((gps->flag & GP_STROKE_CYCLIC) != 0) && (gps->totpoints > 2);
-}
-
-static void gpencil_buffer_add_point(
-    gpStrokeVert *verts, const bGPDstroke *gps, const bGPDspoint *pt, int v, bool is_endpoint)
-{
-  /* Note: we use the sign of stength and thickness to pass cap flag. */
-  const bool round_cap0 = (gps->caps[0] == GP_STROKE_CAP_ROUND);
-  const bool round_cap1 = (gps->caps[1] == GP_STROKE_CAP_ROUND);
-  gpStrokeVert *vert = &verts[v];
-  copy_v3_v3(vert->pos, &pt->x);
-  copy_v2_v2(vert->uv_fill, pt->uv_fill);
-  copy_v4_v4(vert->col, pt->mix_color);
-  vert->strength = (round_cap0) ? pt->strength : -pt->strength;
-  vert->u_stroke = pt->uv_fac;
-  vert->stroke_id = gps->runtime.stroke_start;
-  vert->point_id = v;
-  /* Rotation are in [-90°..90°] range, so we can encode the sign of the angle + the cosine
-   * because the cosine will always be positive. */
-  vert->v_rot = cosf(pt->uv_rot) * signf(pt->uv_rot);
-  vert->thickness = max_ff(0.0f, gps->thickness * pt->pressure) * (round_cap1 ? 1.0 : -1.0);
-  /* Tag endpoint material to -1 so they get discarded by vertex shader. */
-  vert->mat = (is_endpoint) ? -1 : (gps->mat_nr % GP_MATERIAL_BUFFER_LEN);
-}
-
-static void gpencil_buffer_add_stroke(gpStrokeVert *verts, const bGPDstroke *gps)
-{
-  const bGPDspoint *pts = gps->points;
-  int pts_len = gps->totpoints;
-  bool is_cyclic = gpencil_stroke_is_cyclic(gps);
-  int v = gps->runtime.stroke_start;
-
-  /* First point for adjacency (not drawn). */
-  int adj_idx = (is_cyclic) ? (pts_len - 1) : min_ii(pts_len - 1, 1);
-  gpencil_buffer_add_point(verts, gps, &pts[adj_idx], v++, true);
-
-  for (int i = 0; i < pts_len; i++) {
-    gpencil_buffer_add_point(verts, gps, &pts[i], v++, false);
-  }
-  /* Draw line to first point to complete the loop for cyclic strokes. */
-  if (is_cyclic) {
-    gpencil_buffer_add_point(verts, gps, &pts[0], v++, false);
-  }
-  /* Last adjacency point (not drawn). */
-  adj_idx = (is_cyclic) ? 1 : max_ii(0, pts_len - 2);
-  gpencil_buffer_add_point(verts, gps, &pts[adj_idx], v++, true);
-}
-
-static void gpencil_buffer_add_fill(GPUIndexBufBuilder *ibo, const bGPDstroke *gps)
-{
-  int tri_len = gps->tot_triangles;
-  int v = gps->runtime.stroke_start;
-  for (int i = 0; i < tri_len; i++) {
-    uint *tri = gps->triangles[i].verts;
-    GPU_indexbuf_add_tri_verts(ibo, v + tri[0], v + tri[1], v + tri[2]);
-  }
-}
-
-static void gpencil_stroke_iter_cb(bGPDlayer *UNUSED(gpl),
-                                   bGPDframe *UNUSED(gpf),
-                                   bGPDstroke *gps,
-                                   void *thunk)
-{
-  gpIterData *iter = (gpIterData *)thunk;
-  gpencil_buffer_add_stroke(iter->verts, gps);
-  if (gps->tot_triangles > 0) {
-    gpencil_buffer_add_fill(&iter->ibo, gps);
-  }
-}
-
-static void gp_object_verts_count_cb(bGPDlayer *UNUSED(gpl),
-                                     bGPDframe *UNUSED(gpf),
-                                     bGPDstroke *gps,
-                                     void *thunk)
-{
-  gpIterData *iter = (gpIterData *)thunk;
-
-  /* Store first index offset */
-  gps->runtime.stroke_start = iter->vert_len;
-  gps->runtime.fill_start = iter->tri_len;
-  iter->vert_len += gps->totpoints + 2 + gpencil_stroke_is_cyclic(gps);
-  iter->tri_len += gps->tot_triangles;
-}
-
-static void gpencil_batches_ensure(Object *ob, GpencilBatchCache *cache, int cfra)
-{
-  bGPdata *gpd = (bGPdata *)ob->data;
-
-  if (cache->vbo == NULL) {
-    /* Should be discarded together. */
-    BLI_assert(cache->vbo == NULL && cache->ibo == NULL);
-    BLI_assert(cache->stroke_batch == NULL && cache->stroke_batch == NULL);
-    /* TODO/PERF: Could be changed to only do it if needed.
-     * For now it's simpler to assume we always need it
-     * since multiple viewport could or could not need it.
-     * Ideally we should have a dedicated onion skin geom batch. */
-    bool do_onion = true;
-
-    /* First count how many vertices and triangles are needed for the whole object. */
-    gpIterData iter = {
-        .gpd = gpd,
-        .verts = NULL,
-        .ibo = {0},
-        .vert_len = 1, /* Start at 1 for the gl_InstanceID trick to work (see vert shader). */
-        .tri_len = 0,
-    };
-    BKE_gpencil_visible_stroke_iter(ob, NULL, gp_object_verts_count_cb, &iter, do_onion, cfra);
-
-    /* Create VBO. */
-    GPUVertFormat *format = gpencil_stroke_format();
-    cache->vbo = GPU_vertbuf_create_with_format(format);
-    /* Add extra space at the end of the buffer because of quad load. */
-    GPU_vertbuf_data_alloc(cache->vbo, iter.vert_len + 2);
-    iter.verts = (gpStrokeVert *)cache->vbo->data;
-    /* Create IBO. */
-    GPU_indexbuf_init(&iter.ibo, GPU_PRIM_TRIS, iter.tri_len, iter.vert_len);
-
-    /* Fill buffers with data. */
-    BKE_gpencil_visible_stroke_iter(ob, NULL, gpencil_stroke_iter_cb, &iter, do_onion, cfra);
-
-    /* Finish the IBO. */
-    cache->ibo = GPU_indexbuf_build(&iter.ibo);
-
-    /* Create the batches */
-    cache->fill_batch = GPU_batch_create(GPU_PRIM_TRIS, cache->vbo, cache->ibo);
-    cache->stroke_batch = GPU_batch_create(GPU_PRIM_TRI_STRIP, gpencil_dummy_buffer_get(), NULL);
-    GPU_batch_instbuf_add_ex(cache->stroke_batch, cache->vbo, 0);
-
-    gpd->flag &= ~GP_DATA_CACHE_IS_DIRTY;
-    cache->is_dirty = false;
-  }
-}
-
-GPUBatch *GPENCIL_batch_cache_strokes(Object *ob, int cfra)
-{
-  GpencilBatchCache *cache = gpencil_batch_cache_get(ob, cfra);
-  gpencil_batches_ensure(ob, cache, cfra);
-
-  return cache->stroke_batch;
-}
-
-GPUBatch *GPENCIL_batch_cache_fills(Object *ob, int cfra)
-{
-  GpencilBatchCache *cache = gpencil_batch_cache_get(ob, cfra);
-  gpencil_batches_ensure(ob, cache, cfra);
-
-  return cache->fill_batch;
-}
-
-/* Return true if there is anything to draw. */
-bool GPENCIL_batch_from_sbuffer(Object *ob,
-                                GPUBatch **r_stroke_batch,
-                                GPUBatch **r_fill_batch,
-                                bGPDstroke **r_stroke)
-{
-  bGPdata *gpd = (bGPdata *)ob->da

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list