[Bf-blender-cvs] [4ea9f4259c6] temp-angavrilov-material-uniforms: Materials: support the custom uniform attributes in Eevee.

Alexander Gavrilov noreply at git.blender.org
Mon Oct 12 13:15:35 CEST 2020


Commit: 4ea9f4259c6247ef4956a17baa9a699d6dbaf5f3
Author: Alexander Gavrilov
Date:   Fri Aug 7 18:36:52 2020 +0300
Branches: temp-angavrilov-material-uniforms
https://developer.blender.org/rB4ea9f4259c6247ef4956a17baa9a699d6dbaf5f3

Materials: support the custom uniform attributes in Eevee.

The attributes are provided to the shader via a UBO indexed with
resource_id, similar to the existing Object Info data. Unlike that,
however, it is necessary to maintain a separate buffer for every
requested combination of attributes.

This is done using a hash table with the attribute set as the key,
as it is not inconceivable that technically different materials may
use the same set of attributes. In addition, in order to minimize
wasted memory, a sparse UBO pool is implemented, so that chunks that
don't require that set of data don't have to allocate any memory.

Differential Revision: https://developer.blender.org/D2057

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

M	intern/cycles/blender/blender_object.cpp
M	source/blender/blenlib/BLI_bitmap.h
M	source/blender/draw/DRW_engine.h
M	source/blender/draw/intern/draw_instance_data.c
M	source/blender/draw/intern/draw_instance_data.h
M	source/blender/draw/intern/draw_manager.c
M	source/blender/draw/intern/draw_manager.h
M	source/blender/draw/intern/draw_manager_data.c
M	source/blender/draw/intern/draw_manager_exec.c
M	source/blender/gpu/GPU_material.h
M	source/blender/gpu/GPU_shader.h
M	source/blender/gpu/GPU_uniform_buffer.h
M	source/blender/gpu/GPU_viewport.h
M	source/blender/gpu/intern/gpu_codegen.c
M	source/blender/gpu/intern/gpu_material.c
M	source/blender/gpu/intern/gpu_node_graph.c
M	source/blender/gpu/intern/gpu_node_graph.h
M	source/blender/gpu/intern/gpu_viewport.c
M	source/blender/nodes/shader/nodes/node_shader_attribute.c

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

diff --git a/intern/cycles/blender/blender_object.cpp b/intern/cycles/blender/blender_object.cpp
index 9931eeab17a..d42fe804368 100644
--- a/intern/cycles/blender/blender_object.cpp
+++ b/intern/cycles/blender/blender_object.cpp
@@ -329,6 +329,7 @@ Object *BlenderSync::sync_object(BL::Depsgraph &b_depsgraph,
   return object;
 }
 
+/* This function mirrors drw_uniform_property_lookup in draw_instance_data.cpp */
 static bool lookup_property(BL::ID b_id, const string &name, float4 *r_value)
 {
   PointerRNA ptr;
@@ -363,6 +364,7 @@ static bool lookup_property(BL::ID b_id, const string &name, float4 *r_value)
   return false;
 }
 
+/* This function mirrors drw_uniform_attribute_lookup in draw_instance_data.cpp */
 static float4 lookup_instance_property(BL::DepsgraphObjectInstance &b_instance,
                                        const string &name,
                                        bool use_instancer)
diff --git a/source/blender/blenlib/BLI_bitmap.h b/source/blender/blenlib/BLI_bitmap.h
index 960ce44c58c..c97be6eed3c 100644
--- a/source/blender/blenlib/BLI_bitmap.h
+++ b/source/blender/blenlib/BLI_bitmap.h
@@ -106,7 +106,7 @@ typedef unsigned int BLI_bitmap;
 #define BLI_BITMAP_RESIZE(_bitmap, _tot) \
   { \
     CHECK_TYPE(_bitmap, BLI_bitmap *); \
-    (_bitmap) = MEM_reallocN(_bitmap, BLI_BITMAP_SIZE(_tot)); \
+    (_bitmap) = MEM_recallocN(_bitmap, BLI_BITMAP_SIZE(_tot)); \
   } \
   (void)0
 
diff --git a/source/blender/draw/DRW_engine.h b/source/blender/draw/DRW_engine.h
index ca5c2c94b40..2d5b93f4272 100644
--- a/source/blender/draw/DRW_engine.h
+++ b/source/blender/draw/DRW_engine.h
@@ -36,6 +36,7 @@ struct ARegion;
 struct DRWInstanceDataList;
 struct Depsgraph;
 struct DrawEngineType;
+struct GHash;
 struct GPUMaterial;
 struct GPUOffScreen;
 struct GPUViewport;
@@ -140,6 +141,7 @@ void DRW_render_gpencil(struct RenderEngine *engine, struct Depsgraph *depsgraph
 /* This is here because GPUViewport needs it */
 struct DRWInstanceDataList *DRW_instance_data_list_create(void);
 void DRW_instance_data_list_free(struct DRWInstanceDataList *idatalist);
+void DRW_uniform_attrs_pool_free(struct GHash *table);
 
 void DRW_render_context_enable(struct Render *render);
 void DRW_render_context_disable(struct Render *render);
diff --git a/source/blender/draw/intern/draw_instance_data.c b/source/blender/draw/intern/draw_instance_data.c
index f341d16838e..0496caaece3 100644
--- a/source/blender/draw/intern/draw_instance_data.c
+++ b/source/blender/draw/intern/draw_instance_data.c
@@ -30,9 +30,20 @@
  */
 
 #include "draw_instance_data.h"
+#include "draw_manager.h"
+
 #include "DRW_engine.h"
 #include "DRW_render.h" /* For DRW_shgroup_get_instance_count() */
 
+#include "GPU_material.h"
+
+#include "DNA_particle_types.h"
+
+#include "BKE_duplilist.h"
+
+#include "RNA_access.h"
+
+#include "BLI_bitmap.h"
 #include "BLI_memblock.h"
 #include "BLI_mempool.h"
 #include "BLI_utildefines.h"
@@ -408,3 +419,359 @@ void DRW_instance_data_list_resize(DRWInstanceDataList *idatalist)
 }
 
 /** \} */
+/* -------------------------------------------------------------------- */
+/** \name Sparse Uniform Buffer
+ * \{ */
+
+#define CHUNK_LIST_STEP (1 << 4)
+
+/** A chunked UBO manager that doesn't actually allocate unneeded chunks. */
+typedef struct DRWSparseUniformBuf {
+  /* Memory buffers used to stage chunk data before transfer to UBOs. */
+  char **chunk_buffers;
+  /* Uniform buffer objects with flushed data. */
+  struct GPUUniformBuf **chunk_ubos;
+  /* True if the relevant chunk contains data (distinct from simply being allocated). */
+  BLI_bitmap *chunk_used;
+
+  int num_chunks;
+  unsigned int item_size, chunk_size, chunk_bytes;
+} DRWSparseUniformBuf;
+
+static void drw_sparse_uniform_buffer_init(DRWSparseUniformBuf *buffer,
+                                           unsigned int item_size,
+                                           unsigned int chunk_size)
+{
+  buffer->chunk_buffers = NULL;
+  buffer->chunk_used = NULL;
+  buffer->chunk_ubos = NULL;
+  buffer->num_chunks = 0;
+  buffer->item_size = item_size;
+  buffer->chunk_size = chunk_size;
+  buffer->chunk_bytes = item_size * chunk_size;
+}
+
+/** Allocate a chunked UBO with the specified item and chunk size. */
+DRWSparseUniformBuf *DRW_sparse_uniform_buffer_new(unsigned int item_size, unsigned int chunk_size)
+{
+  DRWSparseUniformBuf *buf = MEM_mallocN(sizeof(DRWSparseUniformBuf), __func__);
+  drw_sparse_uniform_buffer_init(buf, item_size, chunk_size);
+  return buf;
+}
+
+/** Flush data from ordinary memory to UBOs. */
+void DRW_sparse_uniform_buffer_flush(DRWSparseUniformBuf *buffer)
+{
+  for (int i = 0; i < buffer->num_chunks; i++) {
+    if (BLI_BITMAP_TEST(buffer->chunk_used, i)) {
+      if (buffer->chunk_ubos[i] == NULL) {
+        buffer->chunk_ubos[i] = GPU_uniformbuf_create(buffer->chunk_bytes);
+      }
+      GPU_uniformbuf_update(buffer->chunk_ubos[i], buffer->chunk_buffers[i]);
+    }
+  }
+}
+
+/** Clean all buffers and free unused ones. */
+void DRW_sparse_uniform_buffer_clear(DRWSparseUniformBuf *buffer, bool free_all)
+{
+  int max_used_chunk = 0;
+
+  for (int i = 0; i < buffer->num_chunks; i++) {
+    /* Delete buffers that were not used since the last clear call. */
+    if (free_all || !BLI_BITMAP_TEST(buffer->chunk_used, i)) {
+      MEM_SAFE_FREE(buffer->chunk_buffers[i]);
+
+      if (buffer->chunk_ubos[i]) {
+        GPU_uniformbuf_free(buffer->chunk_ubos[i]);
+        buffer->chunk_ubos[i] = NULL;
+      }
+    }
+    else {
+      max_used_chunk = i + 1;
+    }
+  }
+
+  /* Shrink the chunk array if appropriate. */
+  const int old_num_chunks = buffer->num_chunks;
+
+  buffer->num_chunks = (max_used_chunk + CHUNK_LIST_STEP - 1) & ~(CHUNK_LIST_STEP - 1);
+
+  if (buffer->num_chunks == 0) {
+    /* Ensure that an empty pool holds no memory allocations. */
+    MEM_SAFE_FREE(buffer->chunk_buffers);
+    MEM_SAFE_FREE(buffer->chunk_used);
+    MEM_SAFE_FREE(buffer->chunk_ubos);
+    return;
+  }
+
+  if (buffer->num_chunks != old_num_chunks) {
+    buffer->chunk_buffers = MEM_recallocN(buffer->chunk_buffers,
+                                          buffer->num_chunks * sizeof(void *));
+    buffer->chunk_ubos = MEM_recallocN(buffer->chunk_ubos, buffer->num_chunks * sizeof(void *));
+    BLI_BITMAP_RESIZE(buffer->chunk_used, buffer->num_chunks);
+  }
+
+  BLI_bitmap_set_all(buffer->chunk_used, false, buffer->num_chunks);
+}
+
+/** Frees the buffer. */
+void DRW_sparse_uniform_buffer_free(DRWSparseUniformBuf *buffer)
+{
+  DRW_sparse_uniform_buffer_clear(buffer, true);
+  MEM_freeN(buffer);
+}
+
+/** Checks if the buffer contains any allocated chunks. */
+bool DRW_sparse_uniform_buffer_is_empty(DRWSparseUniformBuf *buffer)
+{
+  return buffer->num_chunks == 0;
+}
+
+static GPUUniformBuf *drw_sparse_uniform_buffer_get_ubo(DRWSparseUniformBuf *buffer, int chunk)
+{
+  if (buffer && chunk < buffer->num_chunks && BLI_BITMAP_TEST(buffer->chunk_used, chunk)) {
+    return buffer->chunk_ubos[chunk];
+  }
+  else {
+    return NULL;
+  }
+}
+
+/** Bind the UBO for the given chunk, if present. A NULL buffer pointer is handled as empty. */
+void DRW_sparse_uniform_buffer_bind(DRWSparseUniformBuf *buffer, int chunk, int location)
+{
+  GPUUniformBuf *ubo = drw_sparse_uniform_buffer_get_ubo(buffer, chunk);
+  if (ubo) {
+    GPU_uniformbuf_bind(ubo, location);
+  }
+}
+
+/** Unbind the UBO for the given chunk, if present. A NULL buffer pointer is handled as empty. */
+void DRW_sparse_uniform_buffer_unbind(DRWSparseUniformBuf *buffer, int chunk)
+{
+  GPUUniformBuf *ubo = drw_sparse_uniform_buffer_get_ubo(buffer, chunk);
+  if (ubo) {
+    GPU_uniformbuf_unbind(ubo);
+  }
+}
+
+/** Returns a pointer to the given item of the given chunk, allocating memory if necessary. */
+void *DRW_sparse_uniform_buffer_ensure_item(DRWSparseUniformBuf *pool, int chunk, int item)
+{
+  if (chunk >= pool->num_chunks) {
+    pool->num_chunks = (chunk + CHUNK_LIST_STEP) & ~(CHUNK_LIST_STEP - 1);
+    pool->chunk_buffers = MEM_recallocN(pool->chunk_buffers, pool->num_chunks * sizeof(void *));
+    pool->chunk_ubos = MEM_recallocN(pool->chunk_ubos, pool->num_chunks * sizeof(void *));
+    BLI_BITMAP_RESIZE(pool->chunk_used, pool->num_chunks);
+  }
+
+  char *buffer = pool->chunk_buffers[chunk];
+
+  if (buffer == NULL) {
+    pool->chunk_buffers[chunk] = buffer = MEM_callocN(pool->chunk_bytes, __func__);
+  }
+  else if (!BLI_BITMAP_TEST(pool->chunk_used, chunk)) {
+    memset(buffer, 0, pool->chunk_bytes);
+  }
+
+  BLI_BITMAP_ENABLE(pool->chunk_used, chunk);
+
+  return buffer + pool->item_size * item;
+}
+
+/** \} */
+
+/* -------------------------------------------------------------------- */
+/** \name Uniform Attribute Buffers
+ * \{ */
+
+/** Sparse UBO buffer for a specific uniform attribute list. */
+typedef struct DRWUniformAttrBuf {
+  /* Attribute list (also used as hash table key) handled by this buffer. */
+  GPUUniformAttrList key;
+  /* Sparse UBO buffer containing the attribute values. */
+  DRWSparseUniformBuf ubos;
+  /* Last handle used to update the buffer, checked for avoiding redundant updates. */
+  DRWResourceHandle last_handle;
+  /* Linked list pointer used for freeing the empty unneeded buffers. */
+  struct DRWUniformAttrBuf *next_empty;
+} DRWUniformAttrBuf;
+
+static DRWUniformAttrBuf *drw_uniform_attrs_pool_ensure(GHash *table, GPUUniformAttrList *key)
+{
+  void **pkey, **pval;
+
+  if (!BLI_ghash_ensure_p_ex(table, key, &pkey, &pval)) {
+    DRWUniformAttrBuf *buffer = MEM_callocN(sizeof(*buffer), __func__);
+
+    *pkey = &buffer->key;
+    *pval = buffer;
+
+    GPU_uniform_attr_list_copy(&buffer->key, key);
+    drw_sparse_uniform_buffer_init(
+        &buffer->ubos, key->count * sizeof(float[4]), DRW_RESOURCE_CHUNK_LEN);
+
+    buffer->last_handle = (DRWResourceHandle)-1;
+  }
+
+  return (DRWUniformAttrBuf *)*pval;
+}
+
+/* This function mirrors lookup_property in cycles/blender/blender_object.cpp */
+static bool drw_uniform_property_lookup(ID *id, const char *name, float r_data[4])
+{
+  PointerRNA ptr, id_ptr;
+  PropertyRNA *prop;
+
+  if (!id) {
+    return false;
+  }
+
+  RNA_id_pointer_create(id, &id_ptr);
+
+  if (!RNA_path_resolve(&id_ptr, name,

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list