[Bf-blender-cvs] [18c6bb2] strand_gpu: Shifted the dispatch from the algorithm side to the storage side of the sampling system to keep the code simple.

Lukas Tönne noreply at git.blender.org
Tue Jul 5 09:56:25 CEST 2016


Commit: 18c6bb243677f7b0a694b9d5f9207c799b1aa3a2
Author: Lukas Tönne
Date:   Wed Sep 24 17:01:56 2014 +0200
Branches: strand_gpu
https://developer.blender.org/rB18c6bb243677f7b0a694b9d5f9207c799b1aa3a2

Shifted the dispatch from the algorithm side to the storage side of the
sampling system to keep the code simple.

Now there is a MSurfaceSampleStorage struct that encodes the storage
details, which the algorithms don't have to care about.

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

M	source/blender/blenkernel/BKE_mesh_sample.h
M	source/blender/blenkernel/intern/mesh_sample.c

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

diff --git a/source/blender/blenkernel/BKE_mesh_sample.h b/source/blender/blenkernel/BKE_mesh_sample.h
index 9706bfc..7bcce9f 100644
--- a/source/blender/blenkernel/BKE_mesh_sample.h
+++ b/source/blender/blenkernel/BKE_mesh_sample.h
@@ -29,53 +29,24 @@ struct DerivedMesh;
 
 struct MSurfaceSample;
 
-/* Evaluate */
+/* ==== Evaluate ==== */
 
 bool BKE_mesh_sample_eval(struct DerivedMesh *dm, const struct MSurfaceSample *sample, float loc[3], float nor[3]);
 
 
-/* Iterators */
+/* ==== Sampling ==== */
 
-#if 0
-struct MSampleIterator;
+/* Storage descriptor to allow generic data storage by arbitrary algorithms */
+typedef struct MSurfaceSampleStorage {
+	bool (*store_sample)(void *data, int capacity, int index, const struct MSurfaceSample *sample);
+	void *data;
+	int capacity;
+	int free_data;
+} MSurfaceSampleStorage;
 
-typedef void (*MSampleIteratorNextFunc)(MSampleIterator *iter);
-typedef bool (*MSampleIteratorValidFunc)(MSampleIterator *iter);
-typedef void (*MSampleIteratorFreeFunc)(MSampleIterator *iter);
+void BKE_mesh_sample_storage_array(struct MSurfaceSampleStorage *storage, struct MSurfaceSample *samples, int capacity);
+void BKE_mesh_sample_storage_release(struct MSurfaceSampleStorage *storage);
 
-typedef struct MSampleIterator {
-	MSampleIteratorNextFunc next;
-	MSampleIteratorValidFunc valid;
-	MSampleIteratorFreeFunc free;
-} MSampleIterator;
-
-typedef struct MSampleArrayIterator {
-	MSampleIterator base;
-	
-	MSurfaceSample *cur;
-	int remaining;
-} MSampleArrayIterator;
-
-void BKE_mesh_sample_surface_array_begin(MSurfaceSampleArrayIterator *iter, MSurfaceSample *array, int totarray);
-#endif
-
-/* Sampling */
-
-typedef enum eMSurfaceSampleAlgorithm {
-	MSS_RANDOM
-} eMSurfaceSampleAlgorithm;
-
-typedef struct MSurfaceSampleInfo {
-	eMSurfaceSampleAlgorithm algorithm;
-	struct DerivedMesh *dm;
-	
-	struct RNG *rng;
-} MSurfaceSampleInfo;
-
-void BKE_mesh_sample_info_random(struct MSurfaceSampleInfo *info, struct DerivedMesh *dm, unsigned int seed);
-void BKE_mesh_sample_info_release(struct MSurfaceSampleInfo *info);
-
-void BKE_mesh_sample_surface_array(const struct MSurfaceSampleInfo *info, struct MSurfaceSample *samples, int totsample);
-void BKE_mesh_sample_surface_array_stride(const struct MSurfaceSampleInfo *info, struct MSurfaceSample *first, int stride, int totsample);
+void BKE_mesh_sample_generate_random(MSurfaceSampleStorage *dst, struct DerivedMesh *dm, unsigned int seed, int totsample);
 
 #endif  /* __BKE_MESH_SAMPLE_H__ */
diff --git a/source/blender/blenkernel/intern/mesh_sample.c b/source/blender/blenkernel/intern/mesh_sample.c
index a1c3e71..99f512e 100644
--- a/source/blender/blenkernel/intern/mesh_sample.c
+++ b/source/blender/blenkernel/intern/mesh_sample.c
@@ -39,7 +39,7 @@
 
 #include "BLI_strict_flags.h"
 
-/* Evaluate */
+/* ==== Evaluate ==== */
 
 bool BKE_mesh_sample_eval(DerivedMesh *dm, const MSurfaceSample *sample, float loc[3], float nor[3])
 {
@@ -77,100 +77,77 @@ bool BKE_mesh_sample_eval(DerivedMesh *dm, const MSurfaceSample *sample, float l
 }
 
 
-/* Iterators */
+/* ==== Sampling ==== */
 
-#if 0
-static void mesh_sample_surface_array_iterator_next(MSurfaceSampleArrayIterator *iter)
+static bool mesh_sample_store_array_sample(void *vdata, int capacity, int index, const MSurfaceSample *sample)
 {
-	++iter->cur;
-	--iter->remaining;
-}
-
-static bool mesh_sample_surface_array_iterator_valid(MSurfaceSampleArrayIterator *iter)
-{
-	return (iter->remaining > 0);
-}
-
-static void mesh_sample_surface_array_iterator_free(MSurfaceSampleArrayIterator *iter)
-{
-}
-
-void BKE_mesh_sample_surface_array_begin(MSurfaceSampleArrayIterator *iter, MSurfaceSample *array, int totarray)
-{
-	iter->cur = array;
-	iter->remaining = totarray;
+	MSurfaceSample *data = vdata;
+	if (index >= capacity)
+		return false;
 	
-	iter->base.next = 
+	data[index] = *sample;
+	return true;
 }
-#endif
-
 
-/* Sampling */
-
-void BKE_mesh_sample_info_random(MSurfaceSampleInfo *info, DerivedMesh *dm, unsigned int seed)
+void BKE_mesh_sample_storage_array(MSurfaceSampleStorage *storage, MSurfaceSample *samples, int capacity)
 {
-	info->algorithm = MSS_RANDOM;
-	info->dm = dm;
-	
-	info->rng = BLI_rng_new(seed);
+	storage->store_sample = mesh_sample_store_array_sample;
+	storage->capacity = capacity;
+	storage->data = samples;
+	storage->free_data = false;
 }
 
-void BKE_mesh_sample_info_release(MSurfaceSampleInfo *info)
+void BKE_mesh_sample_storage_release(MSurfaceSampleStorage *storage)
 {
-	if (info->rng) {
-		BLI_rng_free(info->rng);
-		info->rng = NULL;
-	}
+	if (storage->free_data)
+		MEM_freeN(storage->data);
 }
 
 
-static void mesh_sample_surface_random(const MSurfaceSampleInfo *info, MSurfaceSample *sample)
+void BKE_mesh_sample_generate_random(MSurfaceSampleStorage *dst, DerivedMesh *dm, unsigned int seed, int totsample)
 {
-	MFace *mfaces = info->dm->getTessFaceArray(info->dm);
-	int totfaces = info->dm->getNumTessFaces(info->dm);
+	MFace *mfaces;
+	int totfaces;
+	RNG *rng;
 	MFace *mface;
 	float a, b;
+	int i;
 	
-	mface = &mfaces[BLI_rng_get_int(info->rng) % totfaces];
-	
-	if (mface->v4 && BLI_rng_get_int(info->rng) % 2 == 0) {
-		sample->orig_verts[0] = mface->v3;
-		sample->orig_verts[1] = mface->v4;
-		sample->orig_verts[2] = mface->v1;
-	}
-	else {
-		sample->orig_verts[0] = mface->v1;
-		sample->orig_verts[1] = mface->v2;
-		sample->orig_verts[2] = mface->v3;
-	}
+	rng = BLI_rng_new(seed);
 	
-	a = BLI_rng_get_float(info->rng);
-	b = BLI_rng_get_float(info->rng);
-	if (a + b > 1.0f) {
-		a = 1.0f - a;
-		b = 1.0f - b;
-	}
-	sample->orig_weights[0] = 1.0f - (a + b);
-	sample->orig_weights[1] = a;
-	sample->orig_weights[2] = b;
-}
-
-void BKE_mesh_sample_surface_array(const MSurfaceSampleInfo *info, MSurfaceSample *samples, int totsample)
-{
-	BKE_mesh_sample_surface_array_stride(info, samples, (int)sizeof(MSurfaceSample), totsample);
-}
-
-void BKE_mesh_sample_surface_array_stride(const struct MSurfaceSampleInfo *info, struct MSurfaceSample *first, int stride, int totsample)
-{
-	MSurfaceSample *sample;
-	int i;
+	DM_ensure_tessface(dm);
+	mfaces = dm->getTessFaceArray(dm);
+	totfaces = dm->getNumTessFaces(dm);
 	
-	switch (info->algorithm) {
-		case MSS_RANDOM: {
-			DM_ensure_tessface(info->dm);
-			for (sample = first, i = 0; i < totsample; sample = (MSurfaceSample *)((char *)sample + stride), ++i)
-				mesh_sample_surface_random(info, sample);
-			break;
+	for (i = 0; i < totsample; ++i) {
+		MSurfaceSample sample = {0};
+		
+		mface = &mfaces[BLI_rng_get_int(rng) % totfaces];
+		
+		if (mface->v4 && BLI_rng_get_int(rng) % 2 == 0) {
+			sample.orig_verts[0] = mface->v3;
+			sample.orig_verts[1] = mface->v4;
+			sample.orig_verts[2] = mface->v1;
 		}
+		else {
+			sample.orig_verts[0] = mface->v1;
+			sample.orig_verts[1] = mface->v2;
+			sample.orig_verts[2] = mface->v3;
+		}
+		
+		a = BLI_rng_get_float(rng);
+		b = BLI_rng_get_float(rng);
+		if (a + b > 1.0f) {
+			a = 1.0f - a;
+			b = 1.0f - b;
+		}
+		sample.orig_weights[0] = 1.0f - (a + b);
+		sample.orig_weights[1] = a;
+		sample.orig_weights[2] = b;
+		
+		if (!dst->store_sample(dst->data, dst->capacity, i, &sample))
+			break;
 	}
+	
+	BLI_rng_free(rng);
 }




More information about the Bf-blender-cvs mailing list