[Bf-blender-cvs] [2d3e86f2b0f] greasepencil-refactor: GPencil: Refactor: Add new basic light system

Clément Foucault noreply at git.blender.org
Thu Dec 19 22:39:17 CET 2019


Commit: 2d3e86f2b0f985afcf2fb5dee563c7754c06b4c6
Author: Clément Foucault
Date:   Thu Dec 19 17:03:17 2019 +0100
Branches: greasepencil-refactor
https://developer.blender.org/rB2d3e86f2b0f985afcf2fb5dee563c7754c06b4c6

GPencil: Refactor: Add new basic light system

This new light system for now only support point lights.

The number of lights is limited to 128.

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

M	source/blender/draw/engines/gpencil/gpencil_draw_data.c
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/shaders/gpencil_common_lib.glsl
M	source/blender/draw/engines/gpencil/shaders/gpencil_frag.glsl
M	source/blender/draw/engines/gpencil/shaders/gpencil_vert.glsl

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

diff --git a/source/blender/draw/engines/gpencil/gpencil_draw_data.c b/source/blender/draw/engines/gpencil/gpencil_draw_data.c
index eec037041f5..c9bf6ff214a 100644
--- a/source/blender/draw/engines/gpencil/gpencil_draw_data.c
+++ b/source/blender/draw/engines/gpencil/gpencil_draw_data.c
@@ -22,6 +22,8 @@
 
 #include "DRW_render.h"
 
+#include "DNA_light_types.h"
+
 #include "BKE_image.h"
 
 #include "BLI_memblock.h"
@@ -32,6 +34,20 @@
 
 #include "gpencil_engine.h"
 
+GPENCIL_LightPool *gpencil_light_pool_add(GPENCIL_PrivateData *pd)
+{
+  GPENCIL_LightPool *lightpool = BLI_memblock_alloc(pd->gp_light_pool);
+  lightpool->next = NULL;
+  lightpool->light_used = 0;
+  /* Tag light list end. */
+  lightpool->light_data[0].color[0] = -1.0;
+  if (lightpool->ubo == NULL) {
+    lightpool->ubo = GPU_uniformbuffer_create(sizeof(lightpool->light_data), NULL, NULL);
+  }
+  pd->last_light_pool = lightpool;
+  return lightpool;
+}
+
 static GPENCIL_MaterialPool *gpencil_material_pool_add(GPENCIL_PrivateData *pd)
 {
   GPENCIL_MaterialPool *matpool = BLI_memblock_alloc(pd->gp_material_pool);
@@ -207,16 +223,60 @@ void gpencil_material_resources_get(GPENCIL_MaterialPool *first_pool,
   }
 }
 
+void gpencil_light_pool_populate(GPENCIL_LightPool *lightpool, Object *ob)
+{
+  Light *la = (Light *)ob->data;
+
+  if (lightpool->light_used >= GPENCIL_LIGHT_BUFFER_LEN) {
+    return;
+  }
+
+  gpLight *gp_light = &lightpool->light_data[lightpool->light_used];
+  copy_v3_v3(gp_light->position, ob->obmat[3]);
+  copy_v3_v3(gp_light->color, &la->r);
+  mul_v3_fl(gp_light->color, la->energy);
+
+  lightpool->light_used++;
+
+  if (lightpool->light_used < GPENCIL_LIGHT_BUFFER_LEN) {
+    /* Tag light list end. */
+    gp_light[1].color[0] = -1.0f;
+  }
+}
+
+/**
+ * Creates a single pool containing all lights assigned (light linked) for a given object.
+ **/
+GPENCIL_LightPool *gpencil_light_pool_create(GPENCIL_PrivateData *pd, Object *UNUSED(ob))
+{
+  GPENCIL_LightPool *lightpool = pd->last_light_pool;
+
+  if (lightpool == NULL) {
+    lightpool = gpencil_light_pool_add(pd);
+  }
+  /* TODO */
+  // gpencil_light_pool_populate(lightpool, ob);
+
+  return lightpool;
+}
+
 void gpencil_material_pool_free(void *storage)
 {
   GPENCIL_MaterialPool *matpool = (GPENCIL_MaterialPool *)storage;
   DRW_UBO_FREE_SAFE(matpool->ubo);
 }
 
+void gpencil_light_pool_free(void *storage)
+{
+  GPENCIL_LightPool *lightpool = (GPENCIL_LightPool *)storage;
+  DRW_UBO_FREE_SAFE(lightpool->ubo);
+}
+
 static void gpencil_view_layer_data_free(void *storage)
 {
   GPENCIL_ViewLayerData *vldata = (GPENCIL_ViewLayerData *)storage;
 
+  BLI_memblock_destroy(vldata->gp_light_pool, gpencil_light_pool_free);
   BLI_memblock_destroy(vldata->gp_material_pool, gpencil_material_pool_free);
   BLI_memblock_destroy(vldata->gp_object_pool, NULL);
   BLI_memblock_destroy(vldata->gp_layer_pool, NULL);
@@ -234,6 +294,7 @@ GPENCIL_ViewLayerData *GPENCIL_view_layer_data_ensure(void)
   if (*vldata == NULL) {
     *vldata = MEM_callocN(sizeof(**vldata), "GPENCIL_ViewLayerData");
 
+    (*vldata)->gp_light_pool = BLI_memblock_create(sizeof(GPENCIL_LightPool));
     (*vldata)->gp_material_pool = BLI_memblock_create(sizeof(GPENCIL_MaterialPool));
     (*vldata)->gp_object_pool = BLI_memblock_create(sizeof(GPENCIL_tObject));
     (*vldata)->gp_layer_pool = BLI_memblock_create(sizeof(GPENCIL_tLayer));
diff --git a/source/blender/draw/engines/gpencil/gpencil_engine.c b/source/blender/draw/engines/gpencil/gpencil_engine.c
index 5e5960c536b..9e5ce788b3c 100644
--- a/source/blender/draw/engines/gpencil/gpencil_engine.c
+++ b/source/blender/draw/engines/gpencil/gpencil_engine.c
@@ -295,21 +295,28 @@ static void GPENCIL_engine_init_new(void *ved)
   GPENCIL_ViewLayerData *vldata = GPENCIL_view_layer_data_ensure();
 
   /* Resize and reset memblocks. */
+  BLI_memblock_clear(vldata->gp_light_pool, gpencil_light_pool_free);
   BLI_memblock_clear(vldata->gp_material_pool, gpencil_material_pool_free);
   BLI_memblock_clear(vldata->gp_object_pool, NULL);
   BLI_memblock_clear(vldata->gp_layer_pool, NULL);
   BLI_memblock_clear(vldata->gp_vfx_pool, NULL);
 
+  stl->pd->gp_light_pool = vldata->gp_light_pool;
   stl->pd->gp_material_pool = vldata->gp_material_pool;
   stl->pd->gp_object_pool = vldata->gp_object_pool;
   stl->pd->gp_layer_pool = vldata->gp_layer_pool;
   stl->pd->gp_vfx_pool = vldata->gp_vfx_pool;
+  stl->pd->last_light_pool = NULL;
   stl->pd->last_material_pool = NULL;
   stl->pd->tobjects.first = NULL;
   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 */
+  stl->pd->global_light_pool = gpencil_light_pool_add(stl->pd);
+  /* Small HACK: we don't want the global pool to be reused,
+   * so we set the last light pool to NULL. */
+  stl->pd->last_light_pool = NULL;
 
   float viewmatinv[4][4];
   DRW_view_viewmat_get(NULL, viewmatinv, true);
@@ -830,6 +837,7 @@ static void gp_layer_cache_populate(bGPDlayer *gpl,
   struct GPUShader *sh = GPENCIL_shader_geometry_get(&en_data);
   iter->grp = DRW_shgroup_create(sh, tgp_layer->geom_ps);
   DRW_shgroup_uniform_block(iter->grp, "gpMaterialBlock", ubo_mat);
+  DRW_shgroup_uniform_block(iter->grp, "gpLightBlock", iter->pd->global_light_pool->ubo);
   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);
@@ -931,6 +939,10 @@ static void GPENCIL_cache_populate_new(void *ved, Object *ob)
 
     gpencil_vfx_cache_populate(vedata, ob, iter.tgp_ob);
   }
+
+  if (ob->type == OB_LAMP) {
+    gpencil_light_pool_populate(pd->global_light_pool, ob);
+  }
 }
 
 void GPENCIL_cache_populate(void *vedata, Object *ob)
@@ -1049,11 +1061,16 @@ static void GPENCIL_cache_finish_new(void *ved)
   BLI_memblock_iter iter;
   BLI_memblock_iternew(pd->gp_material_pool, &iter);
   GPENCIL_MaterialPool *pool;
-
   while ((pool = (GPENCIL_MaterialPool *)BLI_memblock_iterstep(&iter))) {
     GPU_uniformbuffer_update(pool->ubo, pool->mat_data);
   }
 
+  BLI_memblock_iternew(pd->gp_light_pool, &iter);
+  GPENCIL_LightPool *lpool;
+  while ((lpool = (GPENCIL_LightPool *)BLI_memblock_iterstep(&iter))) {
+    GPU_uniformbuffer_update(lpool->ubo, lpool->light_data);
+  }
+
   /* Sort object by distance to the camera. */
   pd->tobjects.first = gpencil_tobject_sort_fn_r(pd->tobjects.first, gpencil_tobject_dist_sort);
 
diff --git a/source/blender/draw/engines/gpencil/gpencil_engine.h b/source/blender/draw/engines/gpencil/gpencil_engine.h
index a03897c4413..846af2df9f4 100644
--- a/source/blender/draw/engines/gpencil/gpencil_engine.h
+++ b/source/blender/draw/engines/gpencil/gpencil_engine.h
@@ -87,7 +87,16 @@ typedef struct gpMaterial {
 #define GP_FILL_TEXTURE_PREMUL (1 << 11)
 #define GP_FILL_TEXTURE_CLIP (1 << 12)
 
+#define GPENCIL_LIGHT_BUFFER_LEN 128
+
+/* UBO structure. Watch out for padding. Must match GLSL declaration. */
+typedef struct gpLight {
+  float color[4];
+  float position[4];
+} gpLight;
+
 BLI_STATIC_ASSERT_ALIGN(gpMaterial, 16)
+BLI_STATIC_ASSERT_ALIGN(gpLight, 16)
 
 /* *********** OBJECTS CACHE *********** */
 typedef struct tGPencilObjectCache_shgrp {
@@ -156,6 +165,17 @@ typedef struct GPENCIL_MaterialPool {
   struct GPUTexture *tex_stroke[GPENCIL_MATERIAL_BUFFER_LEN];
 } GPENCIL_MaterialPool;
 
+typedef struct GPENCIL_LightPool {
+  /* Linklist. */
+  struct GPENCIL_LightPool *next;
+  /* GPU representatin of materials. */
+  gpLight light_data[GPENCIL_LIGHT_BUFFER_LEN];
+  /* Matching ubo. */
+  struct GPUUniformBuffer *ubo;
+  /* Number of light in the pool. */
+  int light_used;
+} GPENCIL_LightPool;
+
 typedef struct GPENCIL_ViewLayerData {
   /* GPENCIL_tObject */
   struct BLI_memblock *gp_object_pool;
@@ -165,6 +185,8 @@ typedef struct GPENCIL_ViewLayerData {
   struct BLI_memblock *gp_vfx_pool;
   /* GPENCIL_MaterialPool */
   struct BLI_memblock *gp_material_pool;
+  /* GPENCIL_LightPool */
+  struct BLI_memblock *gp_light_pool;
 } GPENCIL_ViewLayerData;
 
 /* *********** GPencil  *********** */
@@ -414,8 +436,13 @@ typedef struct GPENCIL_PrivateData {
   struct BLI_memblock *gp_layer_pool;
   struct BLI_memblock *gp_vfx_pool;
   struct BLI_memblock *gp_material_pool;
+  struct BLI_memblock *gp_light_pool;
   /* Last used material pool. */
   GPENCIL_MaterialPool *last_material_pool;
+  /* Last used light pool. */
+  GPENCIL_LightPool *last_light_pool;
+  /* Common lightpool containing all lights in the scene. */
+  GPENCIL_LightPool *global_light_pool;
   /* Linked list of tObjects. */
   struct {
     GPENCIL_tObject *first, *last;
@@ -694,6 +721,10 @@ void gpencil_material_resources_get(GPENCIL_MaterialPool *first_pool,
                                     struct GPUTexture **r_tex_stroke,
                                     struct GPUTexture **r_tex_fill,
                                     struct GPUUniformBuffer **r_ubo_mat);
+/*  Meh, TODO fix naming...*/
+void gpencil_light_pool_populate(GPENCIL_LightPool *matpool, Object *ob);
+GPENCIL_LightPool *gpencil_light_pool_add(GPENCIL_PrivateData *pd);
+GPENCIL_LightPool *gpencil_light_pool_create(GPENCIL_PrivateData *pd, Object *ob);
 
 /* effects */
 void GPENCIL_create_fx_shaders(struct GPENCIL_e_data *e_data);
@@ -741,6 +772,7 @@ void GPENCIL_render_to_image(void *vedata,
                              const rcti *rect);
 
 /* Draw Data. */
+void gpencil_light_pool_free(void *storage);
 void gpencil_material_pool_free(void *storage);
 GPENCIL_ViewLayerData *GPENCIL_view_layer_data_ensure(void);
 
diff --git a/source/blender/draw/engines/gpencil/gpencil_shader.c b/source/blender/draw/engines/gpencil/gpencil_shader.c
index 10ef5e193ba..8445bd0dfd6 100644
--- a/source/blender/draw/engines/gpencil/gpencil_shader.c
+++ b/source/blender/draw/engines/gpencil/gpencil_shader.c
@@ -57,6 +57,7 @@ struct GPUShader *GPENCIL_shader_geometry_get(GPENCIL_e_data *e_data)
         .defs =
             (const char *[]){
                 "#define GPENCIL_MATERIAL_BUFFER_LEN " ST

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list