[Bf-blender-cvs] [c2abb6d3fc0] blender2.8: Particles: Use local RNG

Sergey Sharybin noreply at git.blender.org
Tue Jun 12 14:31:11 CEST 2018


Commit: c2abb6d3fc0238cc1a8526d9fc3f6b603ee51f3a
Author: Sergey Sharybin
Date:   Tue Jun 12 14:20:46 2018 +0200
Branches: blender2.8
https://developer.blender.org/rBc2abb6d3fc0238cc1a8526d9fc3f6b603ee51f3a

Particles: Use local RNG

Currently only obvious cases, but is already a progress.

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

M	source/blender/blenkernel/intern/particle_distribute.c
M	source/blender/blenkernel/intern/particle_system.c

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

diff --git a/source/blender/blenkernel/intern/particle_distribute.c b/source/blender/blenkernel/intern/particle_distribute.c
index 846afd48064..6c23036256b 100644
--- a/source/blender/blenkernel/intern/particle_distribute.c
+++ b/source/blender/blenkernel/intern/particle_distribute.c
@@ -85,6 +85,7 @@ static void distribute_simple_children(Scene *scene, Object *ob, Mesh *final_mes
 	int i, p;
 	int child_nbr= psys_get_child_number(scene, psys, use_render_params);
 	int totpart= psys_get_tot_child(scene, psys, use_render_params);
+	RNG *rng = BLI_rng_new_srandom(31415926 + psys->seed + psys->child_seed);
 
 	alloc_child_particles(psys, totpart);
 
@@ -96,9 +97,9 @@ static void distribute_simple_children(Scene *scene, Object *ob, Mesh *final_mes
 					
 			/* create even spherical distribution inside unit sphere */
 			while (length>=1.0f) {
-				cpa->fuv[0]=2.0f*BLI_frand()-1.0f;
-				cpa->fuv[1]=2.0f*BLI_frand()-1.0f;
-				cpa->fuv[2]=2.0f*BLI_frand()-1.0f;
+				cpa->fuv[0]=2.0f*BLI_rng_get_float(rng)-1.0f;
+				cpa->fuv[1]=2.0f*BLI_rng_get_float(rng)-1.0f;
+				cpa->fuv[2]=2.0f*BLI_rng_get_float(rng)-1.0f;
 				length=len_v3(cpa->fuv);
 			}
 
@@ -107,6 +108,8 @@ static void distribute_simple_children(Scene *scene, Object *ob, Mesh *final_mes
 	}
 	/* dmcache must be updated for parent particles if children from faces is used */
 	psys_calc_dmcache(ob, final_mesh, deform_mesh, psys);
+
+	BLI_rng_free(rng);
 }
 static void distribute_grid(Mesh *mesh, ParticleSystem *psys)
 {
@@ -818,6 +821,7 @@ static int psys_thread_context_init_distribute(ParticleThreadContext *ctx, Parti
 	int jitlevel= 1, distr;
 	float *element_weight=NULL,*jitter_offset=NULL, *vweight=NULL;
 	float cur, maxweight=0.0, tweight, totweight, inv_totweight, co[3], nor[3], orco[3];
+	RNG *rng = NULL;
 	
 	if (ELEM(NULL, ob, psys, psys->part))
 		return 0;
@@ -846,7 +850,6 @@ static int psys_thread_context_init_distribute(ParticleThreadContext *ctx, Parti
 	if (from == PART_FROM_CHILD) {
 		/* Simple children */
 		if (part->childtype != PART_CHILD_FACES) {
-			BLI_srandom(31415926 + psys->seed + psys->child_seed);
 			distribute_simple_children(scene, ob, final_mesh, sim->psmd->mesh_original, psys, use_render_params);
 			return 0;
 		}
@@ -854,8 +857,6 @@ static int psys_thread_context_init_distribute(ParticleThreadContext *ctx, Parti
 	else {
 		/* Grid distribution */
 		if (part->distr==PART_DISTR_GRID && from != PART_FROM_VERT) {
-			BLI_srandom(31415926 + psys->seed);
-
 			if (psys->part->use_modifier_stack) {
 				mesh = final_mesh;
 			}
@@ -882,8 +883,8 @@ static int psys_thread_context_init_distribute(ParticleThreadContext *ctx, Parti
 	
 	/* Create trees and original coordinates if needed */
 	if (from == PART_FROM_CHILD) {
-		distr=PART_DISTR_RAND;
-		BLI_srandom(31415926 + psys->seed + psys->child_seed);
+		distr = PART_DISTR_RAND;
+		rng = BLI_rng_new_srandom(31415926 + psys->seed + psys->child_seed);
 		mesh= final_mesh;
 
 		/* BMESH ONLY */
@@ -906,7 +907,8 @@ static int psys_thread_context_init_distribute(ParticleThreadContext *ctx, Parti
 	}
 	else {
 		distr = part->distr;
-		BLI_srandom(31415926 + psys->seed);
+
+		rng = BLI_rng_new_srandom(31415926 + psys->seed);
 		
 		if (psys->part->use_modifier_stack)
 			mesh = final_mesh;
@@ -958,6 +960,7 @@ static int psys_thread_context_init_distribute(ParticleThreadContext *ctx, Parti
 		if (mesh != final_mesh) BKE_id_free(NULL, mesh);
 
 		BLI_kdtree_free(tree);
+		BLI_rng_free(rng);
 
 		return 0;
 	}
@@ -1098,7 +1101,7 @@ static int psys_thread_context_init_distribute(ParticleThreadContext *ctx, Parti
 		for (p = 0; p < totpart; p++) {
 			/* In theory element_sum[totmapped - 1] should be 1.0,
 			 * but due to float errors this is not necessarily always true, so scale pos accordingly. */
-			const float pos = BLI_frand() * element_sum[totmapped - 1];
+			const float pos = BLI_rng_get_float(rng) * element_sum[totmapped - 1];
 			const int eidx = distribute_binary_search(element_sum, totmapped, pos);
 			particle_element[p] = element_map[eidx];
 			BLI_assert(pos <= element_sum[eidx]);
@@ -1191,6 +1194,8 @@ static int psys_thread_context_init_distribute(ParticleThreadContext *ctx, Parti
 		alloc_child_particles(psys, totpart);
 	}
 
+	BLI_rng_free(rng);
+
 	return 1;
 }
 
diff --git a/source/blender/blenkernel/intern/particle_system.c b/source/blender/blenkernel/intern/particle_system.c
index 8d6991ff9f4..514f4408568 100644
--- a/source/blender/blenkernel/intern/particle_system.c
+++ b/source/blender/blenkernel/intern/particle_system.c
@@ -3529,7 +3529,6 @@ static void dynamics_step(ParticleSimulationData *sim, float cfra)
 		return;
 	}
 
-	BLI_srandom(31415926 + (int)cfra + psys->seed);
 	/* for now do both, boids us 'rng' */
 	rng = BLI_rng_new_srandom(31415926 + (int)cfra + psys->seed);



More information about the Bf-blender-cvs mailing list