[Bf-blender-cvs] [5b5de356045] blender2.8: Support for Batchs split by material for objects of type Curve, Surface, and Text

Germano noreply at git.blender.org
Thu Dec 14 15:21:54 CET 2017


Commit: 5b5de35604569bfe5007a7ed427c702775c7871c
Author: Germano
Date:   Thu Dec 14 12:21:38 2017 -0200
Branches: blender2.8
https://developer.blender.org/rB5b5de35604569bfe5007a7ed427c702775c7871c

Support for Batchs split by material for objects of type Curve, Surface, and Text

**ToDo:**
- add vertbuff for UV (what can be adapted from `dl_surf_to_renderdata`)

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

M	source/blender/draw/intern/draw_cache.c
M	source/blender/draw/intern/draw_cache.h
M	source/blender/draw/intern/draw_cache_impl.h
M	source/blender/draw/intern/draw_cache_impl_curve.c
M	source/blender/draw/intern/draw_cache_impl_displist.c
M	source/blender/draw/intern/draw_cache_impl_metaball.c

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

diff --git a/source/blender/draw/intern/draw_cache.c b/source/blender/draw/intern/draw_cache.c
index 8010a232f89..e02f6bd6306 100644
--- a/source/blender/draw/intern/draw_cache.c
+++ b/source/blender/draw/intern/draw_cache.c
@@ -520,6 +520,12 @@ Gwn_Batch **DRW_cache_object_surface_material_get(
 	switch (ob->type) {
 		case OB_MESH:
 			return DRW_cache_mesh_surface_shaded_get(ob, gpumat_array, gpumat_array_len);
+		case OB_CURVE:
+			return DRW_cache_curve_surface_shaded_get(ob, gpumat_array, gpumat_array_len);
+		case OB_SURF:
+			return DRW_cache_surf_surface_shaded_get(ob, gpumat_array, gpumat_array_len);
+		case OB_FONT:
+			return DRW_cache_text_surface_shaded_get(ob, gpumat_array, gpumat_array_len);
 		default:
 			return NULL;
 	}
@@ -2334,6 +2340,16 @@ Gwn_Batch *DRW_cache_curve_surface_get(Object *ob)
 	return DRW_curve_batch_cache_get_triangles_with_normals(cu, ob->curve_cache);
 }
 
+/* Return list of batches */
+Gwn_Batch **DRW_cache_curve_surface_shaded_get(
+        Object *ob, struct GPUMaterial **gpumat_array, uint gpumat_array_len)
+{
+	BLI_assert(ob->type == OB_CURVE);
+
+	struct Curve *cu = ob->data;
+	return DRW_curve_batch_cache_get_surface_shaded(cu, ob->curve_cache, gpumat_array, gpumat_array_len);
+}
+
 /** \} */
 
 /* -------------------------------------------------------------------- */
@@ -2372,6 +2388,17 @@ Gwn_Batch *DRW_cache_text_surface_get(Object *ob)
 	return DRW_curve_batch_cache_get_triangles_with_normals(cu, ob->curve_cache);
 }
 
+Gwn_Batch **DRW_cache_text_surface_shaded_get(
+        Object *ob, struct GPUMaterial **gpumat_array, uint gpumat_array_len)
+{
+	BLI_assert(ob->type == OB_FONT);
+	struct Curve *cu = ob->data;
+	if (cu->editfont && (cu->flag & CU_FAST)) {
+		return NULL;
+	}
+	return DRW_curve_batch_cache_get_surface_shaded(cu, ob->curve_cache, gpumat_array, gpumat_array_len);
+}
+
 Gwn_Batch *DRW_cache_text_cursor_overlay_get(Object *ob)
 {
 	BLI_assert(ob->type == OB_FONT);
@@ -2401,6 +2428,16 @@ Gwn_Batch *DRW_cache_surf_surface_get(Object *ob)
 	return DRW_curve_batch_cache_get_triangles_with_normals(cu, ob->curve_cache);
 }
 
+/* Return list of batches */
+Gwn_Batch **DRW_cache_surf_surface_shaded_get(
+        Object *ob, struct GPUMaterial **gpumat_array, uint gpumat_array_len)
+{
+	BLI_assert(ob->type == OB_SURF);
+
+	struct Curve *cu = ob->data;
+	return DRW_curve_batch_cache_get_surface_shaded(cu, ob->curve_cache, gpumat_array, gpumat_array_len);
+}
+
 /** \} */
 
 /* -------------------------------------------------------------------- */
diff --git a/source/blender/draw/intern/draw_cache.h b/source/blender/draw/intern/draw_cache.h
index 35ac8f4a35d..3564178dba9 100644
--- a/source/blender/draw/intern/draw_cache.h
+++ b/source/blender/draw/intern/draw_cache.h
@@ -130,6 +130,8 @@ void DRW_cache_mesh_sculpt_coords_ensure(struct Object *ob);
 
 /* Curve */
 struct Gwn_Batch *DRW_cache_curve_surface_get(struct Object *ob);
+struct Gwn_Batch **DRW_cache_curve_surface_shaded_get(
+        struct Object *ob, struct GPUMaterial **gpumat_array, uint gpumat_array_len);
 struct Gwn_Batch *DRW_cache_curve_surface_verts_get(struct Object *ob);
 struct Gwn_Batch *DRW_cache_curve_edge_wire_get(struct Object *ob);
 /* edit-mode */
@@ -140,12 +142,16 @@ struct Gwn_Batch *DRW_cache_curve_vert_overlay_get(struct Object *ob);
 /* Font */
 struct Gwn_Batch *DRW_cache_text_edge_wire_get(struct Object *ob);
 struct Gwn_Batch *DRW_cache_text_surface_get(struct Object *ob);
+struct Gwn_Batch **DRW_cache_text_surface_shaded_get(
+        struct Object *ob, struct GPUMaterial **gpumat_array, uint gpumat_array_len);
 /* edit-mode */
 struct Gwn_Batch *DRW_cache_text_cursor_overlay_get(struct Object *ob);
 struct Gwn_Batch *DRW_cache_text_select_overlay_get(struct Object *ob);
 
 /* Surface */
 struct Gwn_Batch *DRW_cache_surf_surface_get(struct Object *ob);
+struct Gwn_Batch **DRW_cache_surf_surface_shaded_get(
+        struct Object *ob, struct GPUMaterial **gpumat_array, uint gpumat_array_len);
 
 /* Lattice */
 struct Gwn_Batch *DRW_cache_lattice_verts_get(struct Object *ob);
diff --git a/source/blender/draw/intern/draw_cache_impl.h b/source/blender/draw/intern/draw_cache_impl.h
index c998282ae1f..5188ac0bd1a 100644
--- a/source/blender/draw/intern/draw_cache_impl.h
+++ b/source/blender/draw/intern/draw_cache_impl.h
@@ -29,14 +29,16 @@
 struct CurveCache;
 struct GPUMaterial;
 struct Gwn_Batch;
+struct Gwn_IndexBuf;
+struct Gwn_VertBuf;
 struct ListBase;
-struct MetaBall;
 struct ModifierData;
 struct ParticleSystem;
 
 struct Curve;
 struct Lattice;
 struct Mesh;
+struct MetaBall;
 
 /* Expose via BKE callbacks */
 void DRW_mball_batch_cache_dirty(struct MetaBall *mb, int mode);
@@ -62,6 +64,9 @@ struct Gwn_Batch *DRW_curve_batch_cache_get_overlay_edges(struct Curve *cu);
 struct Gwn_Batch *DRW_curve_batch_cache_get_overlay_verts(struct Curve *cu);
 
 struct Gwn_Batch *DRW_curve_batch_cache_get_triangles_with_normals(struct Curve *cu, struct CurveCache *ob_curve_cache);
+struct Gwn_Batch **DRW_curve_batch_cache_get_surface_shaded(
+        struct Curve *cu, struct CurveCache *ob_curve_cache,
+        struct GPUMaterial **gpumat_array, uint gpumat_array_len);
 
 /* Metaball */
 struct Gwn_Batch *DRW_metaball_batch_cache_get_triangles_with_normals(struct Object *ob);
@@ -71,7 +76,9 @@ struct Gwn_Batch *DRW_curve_batch_cache_get_overlay_cursor(struct Curve *cu);
 struct Gwn_Batch *DRW_curve_batch_cache_get_overlay_select(struct Curve *cu);
 
 /* DispList */
-struct Gwn_Batch *BLI_displist_batch_calc_surface(struct ListBase *lb);
+struct Gwn_VertBuf *DRW_displist_vertbuf_calc_pos_with_normals(struct ListBase *lb);
+struct Gwn_IndexBuf *DRW_displist_indexbuf_calc_triangles_in_order(struct ListBase *lb);
+struct Gwn_IndexBuf **DRW_displist_indexbuf_calc_triangles_in_order_split_by_material(struct ListBase *lb, uint gpumat_array_len);
 
 /* Lattice */
 struct Gwn_Batch *DRW_lattice_batch_cache_get_all_edges(struct Lattice *lt, bool use_weight, const int actdef);
diff --git a/source/blender/draw/intern/draw_cache_impl_curve.c b/source/blender/draw/intern/draw_cache_impl_curve.c
index e6e52fe4579..8e38e22a210 100644
--- a/source/blender/draw/intern/draw_cache_impl_curve.c
+++ b/source/blender/draw/intern/draw_cache_impl_curve.c
@@ -310,7 +310,11 @@ typedef struct CurveBatchCache {
 	} overlay;
 
 	struct {
+		Gwn_VertBuf *verts;
+		Gwn_IndexBuf *triangles_in_order;
+		Gwn_Batch **shaded_triangles;
 		Gwn_Batch *batch;
+		int mat_len;
 	} surface;
 
 	/* 3d text */
@@ -444,6 +448,14 @@ static void curve_batch_cache_clear(Curve *cu)
 	GWN_BATCH_DISCARD_SAFE(cache->overlay.verts);
 	GWN_BATCH_DISCARD_SAFE(cache->overlay.edges);
 
+	GWN_VERTBUF_DISCARD_SAFE(cache->surface.verts);
+	GWN_INDEXBUF_DISCARD_SAFE(cache->surface.triangles_in_order);
+	if (cache->surface.shaded_triangles) {
+		for (int i = 0; i < cache->surface.mat_len; ++i) {
+			GWN_BATCH_DISCARD_SAFE(cache->surface.shaded_triangles[i]);
+		}
+	}
+	MEM_SAFE_FREE(cache->surface.shaded_triangles);
 	GWN_BATCH_DISCARD_SAFE(cache->surface.batch);
 
 	/* don't own vbo & elems */
@@ -796,8 +808,20 @@ static Gwn_Batch *curve_batch_cache_get_pos_and_normals(CurveRenderData *rdata,
 {
 	BLI_assert(rdata->types & CU_DATATYPE_SURFACE);
 	if (cache->surface.batch == NULL) {
-		cache->surface.batch = BLI_displist_batch_calc_surface(&rdata->ob_curve_cache->disp);
+		ListBase *lb = &rdata->ob_curve_cache->disp;
+
+		if (cache->surface.verts == NULL) {
+			cache->surface.verts = DRW_displist_vertbuf_calc_pos_with_normals(lb);
+		}
+		if (cache->surface.verts) {
+			if (cache->surface.triangles_in_order == NULL) {
+				cache->surface.triangles_in_order = DRW_displist_indexbuf_calc_triangles_in_order(lb);
+			}
+			cache->surface.batch = GWN_batch_create_ex(
+			        GWN_PRIM_TRIS, cache->surface.verts, cache->surface.triangles_in_order, 0);
+		}
 	}
+
 	return cache->surface.batch;
 }
 
@@ -997,6 +1021,48 @@ Gwn_Batch *DRW_curve_batch_cache_get_triangles_with_normals(
 	return cache->surface.batch;
 }
 
+Gwn_Batch **DRW_curve_batch_cache_get_surface_shaded(
+        struct Curve *cu, struct CurveCache *ob_curve_cache,
+        struct GPUMaterial **UNUSED(gpumat_array), uint gpumat_array_len)
+{
+	CurveBatchCache *cache = curve_batch_cache_get(cu);
+
+	if (cache->surface.mat_len != gpumat_array_len) {
+		/* TODO: deduplicate code */
+		GWN_INDEXBUF_DISCARD_SAFE(cache->surface.triangles_in_order);
+		if (cache->surface.shaded_triangles) {
+			for (int i = 0; i < cache->surface.mat_len; ++i) {
+				GWN_BATCH_DISCARD_SAFE(cache->surface.shaded_triangles[i]);
+			}
+		}
+		MEM_SAFE_FREE(cache->surface.shaded_triangles);
+	}
+
+	if (cache->surface.shaded_triangles == NULL) {
+		CurveRenderData *rdata = curve_render_data_create(cu, ob_curve_cache, CU_DATATYPE_SURFACE);
+		ListBase *lb = &rdata->ob_curve_cache->disp;
+
+		cache->surface.mat_len = gpumat_array_len;
+		Gwn_IndexBuf **el = DRW_displist_indexbuf_calc_triangles_in_order_split_by_material(lb, gpumat_array_len);
+		cache->surface.shaded_triangles = MEM_mallocN(sizeof(*cache->surface.shaded_triangles) * gpumat_array_len, __func__);
+
+		if (cache->surface.verts == NULL) {
+			cache->surface.verts = DRW_displist_vertbuf_calc_pos_with_normals(lb);
+		}
+
+		for (int i = 0; i < gpumat_array_len; ++i) {
+			cache->surface.shaded_triangles[i] = GWN_batch_create(
+			        GWN_PRIM_TRIS, cache->surface.verts, el[i]);
+			/* TODO: Add vertbuff for UV */
+		}
+
+		MEM_freeN(el); /* Save `el` in cache? */
+		curve_render_data_free(rdata);
+	}
+
+	return cache->surface.shaded_triangles;
+}
+
 
 /* -------------------------------------------------------------------- */
 
diff --git a/source/blender/draw/intern/draw_cache_impl_displist.c b/source/blender/draw/intern/draw_cache_impl_displist.c
index 96386f82faf..87ff52c951c 100644
--- a/source/blender/draw/intern/draw_cache_impl_displist.c
+++ b/source/blender/draw/intern/draw_cache_impl_displist.c
@@ -31,6 +31,7 @@
 
 #include "MEM_guardedalloc.h"
 
+#include "BLI_alloca.h"
 #include "BLI_utildefines.h"
 #include "BLI_math_vector.h"
 
@@ -86,7 +87,38 @@ static int curve_render_surface_tri_len_get(const ListBase *lb)
 	return tri_len;
 }
 
-Gwn_Batch *BLI_displist_batch_calc_surfac

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list