[Bf-blender-cvs] [9f69daf] strand_gpu: Use a converter class for handling differences between DNA and BMesh strands data.

Lukas Tönne noreply at git.blender.org
Sat Jul 16 12:20:51 CEST 2016


Commit: 9f69daff40d8e562088c87661d8a8d7ed7abad38
Author: Lukas Tönne
Date:   Sat Jul 16 12:19:02 2016 +0200
Branches: strand_gpu
https://developer.blender.org/rB9f69daff40d8e562088c87661d8a8d7ed7abad38

Use a converter class for handling differences between DNA and BMesh strands data.

This moves most of the data access logic for this data out of the GPU code and into
the respective BKE files. The concept is similar to how DerivedMesh is used in other
GPU code.

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

M	source/blender/blenkernel/BKE_editstrands.h
M	source/blender/blenkernel/BKE_strands.h
M	source/blender/blenkernel/intern/editstrands.c
M	source/blender/blenkernel/intern/strands.c
M	source/blender/editors/space_view3d/drawstrands.c
M	source/blender/gpu/GPU_strands.h
M	source/blender/gpu/intern/gpu_strands_buffer.c
M	source/blender/gpu/intern/gpu_strands_shader.c

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

diff --git a/source/blender/blenkernel/BKE_editstrands.h b/source/blender/blenkernel/BKE_editstrands.h
index 850a728..2fc0728 100644
--- a/source/blender/blenkernel/BKE_editstrands.h
+++ b/source/blender/blenkernel/BKE_editstrands.h
@@ -43,6 +43,7 @@
 
 struct BMesh;
 struct DerivedMesh;
+struct GPUStrandsConverter;
 struct Mesh;
 struct Object;
 struct Strands;
@@ -101,4 +102,8 @@ void BKE_editstrands_mesh_from_bmesh(struct Object *ob);
 struct BMesh *BKE_editstrands_strands_to_bmesh(struct Strands *strands, struct DerivedMesh *root_dm);
 void BKE_editstrands_strands_from_bmesh(struct Strands *strands, struct BMesh *bm, struct DerivedMesh *root_dm);
 
+/* === gpu buffer conversion === */
+struct GPUStrandsConverter *BKE_editstrands_get_gpu_converter(struct BMEditStrands *edit, struct DerivedMesh *root_dm,
+                                                              int subdiv, int fiber_primitive, bool use_geomshader);
+
 #endif
diff --git a/source/blender/blenkernel/BKE_strands.h b/source/blender/blenkernel/BKE_strands.h
index b167ba9..cdfb8ef 100644
--- a/source/blender/blenkernel/BKE_strands.h
+++ b/source/blender/blenkernel/BKE_strands.h
@@ -39,6 +39,7 @@
 struct BMesh;
 struct BMVert;
 struct DerivedMesh;
+struct GPUStrandsConverter;
 struct GPUStrandsShader;
 struct GPUDrawStrands;
 
@@ -91,19 +92,11 @@ void BKE_strands_free_fibers(struct Strands *strands);
 
 void BKE_strands_free_drawdata(struct GPUDrawStrands *gpu_buffer);
 
-#if 0
-typedef struct StrandCurveParams {
-	struct DerivedMesh *scalp;
-	unsigned int max_verts;
-} StrandCurveParams;
-
-struct StrandData *BKE_strand_data_interpolate(struct StrandInfo *strands, unsigned int num_strands,
-                                               const StrandCurve *controls, struct StrandCurveParams *params);
-void BKE_strand_data_free(struct StrandData *data);
-#endif
-
 /* ------------------------------------------------------------------------- */
 
 void BKE_strands_invalidate_shader(struct Strands *strands);
 
+struct GPUStrandsConverter *BKE_strands_get_gpu_converter(struct Strands *strands, struct DerivedMesh *root_dm,
+                                                          int subdiv, int fiber_primitive, bool use_geomshader);
+
 #endif
diff --git a/source/blender/blenkernel/intern/editstrands.c b/source/blender/blenkernel/intern/editstrands.c
index f6fb3db..a8e180c 100644
--- a/source/blender/blenkernel/intern/editstrands.c
+++ b/source/blender/blenkernel/intern/editstrands.c
@@ -56,6 +56,7 @@
 #include "BPH_strands.h"
 
 #include "GPU_buffers.h"
+#include "GPU_strands.h"
 
 #include "intern/bmesh_mesh_conv.h"
 #include "intern/bmesh_strands_conv.h"
@@ -368,3 +369,173 @@ void BKE_editstrands_strands_from_bmesh(Strands *strands, BMesh *bm, DerivedMesh
 		BM_bm_to_strands(bm, strands, root_dm);
 	}
 }
+
+
+/* === gpu buffer conversion === */
+
+typedef struct BMStrandCurve
+{
+	BMVert *root;
+	int verts_begin;
+	int num_verts;
+} BMStrandCurve;
+
+static BMStrandCurve *editstrands_build_curves(BMesh *bm, int *r_totcurves)
+{
+	BMVert *root;
+	BMIter iter;
+	
+	int totstrands = BM_strands_count(bm);
+	BMStrandCurve *curves = MEM_mallocN(sizeof(BMStrandCurve) * totstrands, "BMStrandCurve");
+	
+	BMStrandCurve *curve = curves;
+	BM_ITER_STRANDS(root, &iter, bm, BM_STRANDS_OF_MESH) {
+		curve->root = root;
+		curve->verts_begin = BM_elem_index_get(root);
+		curve->num_verts = BM_strand_verts_count(root);
+		
+		++curve;
+	}
+	
+	if (r_totcurves) *r_totcurves = totstrands;
+	return curves;
+}
+
+typedef struct BMEditStrandsConverter {
+	GPUStrandsConverter base;
+	BMEditStrands *edit;
+	BMStrandCurve *curves;
+	int totcurves;
+} BMEditStrandsConverter;
+
+static void BMEditStrandsConverter_free(GPUStrandsConverter *_conv)
+{
+	BMEditStrandsConverter *conv = (BMEditStrandsConverter *)_conv;
+	if (conv->curves)
+		MEM_freeN(conv->curves);
+	MEM_freeN(conv);
+}
+
+static int BMEditStrandsConverter_getNumFibers(GPUStrandsConverter *_conv)
+{
+	BMEditStrandsConverter *conv = (BMEditStrandsConverter *)_conv;
+	return conv->edit->totfibers;
+}
+
+static StrandFiber *BMEditStrandsConverter_getFiberArray(GPUStrandsConverter *_conv)
+{
+	BMEditStrandsConverter *conv = (BMEditStrandsConverter *)_conv;
+	return conv->edit->fibers;
+}
+
+static int BMEditStrandsConverter_getNumStrandVerts(GPUStrandsConverter *_conv)
+{
+	BMEditStrandsConverter *conv = (BMEditStrandsConverter *)_conv;
+	return conv->edit->base.bm->totvert;
+}
+
+static int BMEditStrandsConverter_getNumStrandCurves(GPUStrandsConverter *_conv)
+{
+	BMEditStrandsConverter *conv = (BMEditStrandsConverter *)_conv;
+	return conv->totcurves;
+}
+
+static int BMEditStrandsConverter_getNumStrandCurveVerts(GPUStrandsConverter *_conv, int curve_index)
+{
+	BMEditStrandsConverter *conv = (BMEditStrandsConverter *)_conv;
+	BLI_assert(curve_index < conv->totcurves);
+	return conv->curves[curve_index].num_verts;
+}
+
+static void BMEditStrandsConverter_foreachStrandVertex(GPUStrandsConverter *_conv, GPUStrandsVertexFunc cb, void *userdata)
+{
+	BMEditStrandsConverter *conv = (BMEditStrandsConverter *)_conv;
+	BMesh *bm = conv->edit->base.bm;
+	BMIter iter;
+	BMVert *vert;
+	int i;
+	
+	BM_ITER_MESH_INDEX(vert, &iter, bm, BM_VERTS_OF_MESH, i) {
+		cb(userdata, i, vert->co, NULL);
+	}
+}
+
+static void BMEditStrandsConverter_foreachStrandEdge(GPUStrandsConverter *_conv, GPUStrandsEdgeFunc cb, void *userdata)
+{
+	BMEditStrandsConverter *conv = (BMEditStrandsConverter *)_conv;
+	BMesh *bm = conv->edit->base.bm;
+	BMIter iter;
+	BMEdge *edge;
+	
+	BM_ITER_MESH(edge, &iter, bm, BM_EDGES_OF_MESH) {
+		cb(userdata, BM_elem_index_get(edge->v1), BM_elem_index_get(edge->v2));
+	}
+}
+
+static void BMEditStrandsConverter_foreachCurve(GPUStrandsConverter *_conv, GPUStrandsCurveFunc cb, void *userdata)
+{
+	BMEditStrandsConverter *conv = (BMEditStrandsConverter *)_conv;
+	BMesh *bm = conv->edit->base.bm;
+	BMIter iter;
+	BMVert *root;
+	
+	int verts_begin = 0;
+	BM_ITER_STRANDS(root, &iter, bm, BM_STRANDS_OF_MESH) {
+		int orig_num_verts = BM_strand_verts_count(root);
+		int num_verts = BKE_strand_curve_cache_size(orig_num_verts, conv->base.subdiv);
+		
+		cb(userdata, verts_begin, num_verts);
+		
+		verts_begin += num_verts;
+	}
+}
+
+static void BMEditStrandsConverter_foreachCurveCache(GPUStrandsConverter *_conv, GPUStrandsCurveCacheFunc cb, void *userdata)
+{
+	BMEditStrandsConverter *conv = (BMEditStrandsConverter *)_conv;
+	BMesh *bm = conv->edit->base.bm;
+	
+	StrandCurveCache *cache = BKE_strand_curve_cache_create_bm(bm, conv->base.subdiv);
+	
+	BMIter iter;
+	BMVert *root;
+	BM_ITER_STRANDS(root, &iter, bm, BM_STRANDS_OF_MESH) {
+		float rootmat[4][4];
+		BKE_editstrands_get_matrix(conv->edit, root, rootmat);
+		
+		int orig_num_verts = BM_strand_verts_count(root);
+		int num_verts = BKE_strand_curve_cache_calc_bm(root, orig_num_verts, cache, rootmat, conv->base.subdiv);
+		BLI_assert(orig_num_verts >= 2);
+		
+		cb(userdata, cache, num_verts);
+	}
+	
+	BKE_strand_curve_cache_free(cache);
+}
+
+
+GPUStrandsConverter *BKE_editstrands_get_gpu_converter(BMEditStrands *edit, struct DerivedMesh *root_dm,
+                                                       int subdiv, int fiber_primitive, bool use_geomshader)
+{
+	BMEditStrandsConverter *conv = MEM_callocN(sizeof(BMEditStrandsConverter), "BMEditStrandsConverter");
+	conv->base.free = BMEditStrandsConverter_free;
+	conv->base.getNumFibers = BMEditStrandsConverter_getNumFibers;
+	conv->base.getFiberArray = BMEditStrandsConverter_getFiberArray;
+	conv->base.getNumStrandVerts = BMEditStrandsConverter_getNumStrandVerts;
+	conv->base.getNumStrandCurves = BMEditStrandsConverter_getNumStrandCurves;
+	conv->base.getNumStrandCurveVerts = BMEditStrandsConverter_getNumStrandCurveVerts;
+	
+	conv->base.foreachStrandVertex = BMEditStrandsConverter_foreachStrandVertex;
+	conv->base.foreachStrandEdge = BMEditStrandsConverter_foreachStrandEdge;
+	conv->base.foreachCurve = BMEditStrandsConverter_foreachCurve;
+	conv->base.foreachCurveCache = BMEditStrandsConverter_foreachCurveCache;
+	
+	conv->base.root_dm = root_dm;
+	conv->base.subdiv = subdiv;
+	conv->base.fiber_primitive = fiber_primitive;
+	conv->base.use_geomshader = use_geomshader;
+	
+	conv->edit = edit;
+	conv->curves = editstrands_build_curves(edit->base.bm, &conv->totcurves);
+	return (GPUStrandsConverter *)conv;
+}
diff --git a/source/blender/blenkernel/intern/strands.c b/source/blender/blenkernel/intern/strands.c
index 9a8c603..056c48b 100644
--- a/source/blender/blenkernel/intern/strands.c
+++ b/source/blender/blenkernel/intern/strands.c
@@ -571,71 +571,169 @@ void BKE_strands_free_drawdata(struct GPUDrawStrands *gpu_buffer)
 	GPU_strands_buffer_free(gpu_buffer);
 }
 
-#if 0
-StrandData *BKE_strand_data_interpolate(StrandInfo *strands, unsigned int num_strands,
-                                        const StrandCurve *controls, struct StrandCurveParams *params)
-{
-	StrandData *data = MEM_callocN(sizeof(StrandData), "strand interpolation data");
-	StrandInfo *s;
-	StrandCurve *c;
-	StrandVertex *v;
-	unsigned int verts_begin;
-	unsigned int i;
-	
-	data->totcurves = num_strands;
-	data->totverts = num_strands * params->max_verts;
-	data->curves = MEM_mallocN(sizeof(StrandCurve) * data->totcurves, "strand curves");
-	data->verts = MEM_mallocN(sizeof(StrandVertex) * data->totverts, "strand vertices");
-	
-	UNUSED_VARS(controls);
-	verts_begin = 0;
-	v = data->verts;
-	for (i = 0, s = strands, c = data->curves; i < num_strands; ++i) {
-		unsigned int k;
+
+/* ------------------------------------------------------------------------- */
+
+void BKE_strands_invalidate_shader(Strands *strands)
+{
+	if (strands->gpu_shader) {
+		GPU_strand_shader_free(strands->gpu_shader);
+		strands->gpu_shader = NULL;
+	}
+}
+
+
+/* gpu buffer conversion */
+
+typedef struct DNAStrandsConverter {
+	GPUStrandsConverter base;
+	Strands *strands;
+} DNAStrandsConverter;
+
+static void DNAStrandsConverter_free(GPUStrandsConverter *_conv)
+{
+	DNAStrandsConverter *conv = (DNAStrandsConverter *)_conv;
+	MEM_freeN(conv);
+}
+
+static int DNAStrandsConverter_getNumFibers(GPUStrandsConverter *_conv)
+{
+	DNAStrandsConverter *conv = (DNAStrandsConverter *)_conv;
+	return conv->strands-

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list