cellular sim deterministic work
Some checks failed
studiorailgun/Renderer/pipeline/head There was a failure building this commit

This commit is contained in:
austin 2024-12-07 15:12:29 -05:00
parent 6cd97ec13b
commit 01a2ab603c
14 changed files with 198 additions and 20 deletions

View File

@ -36,6 +36,8 @@
"dispatcher.h": "c", "dispatcher.h": "c",
"cellular.h": "c", "cellular.h": "c",
"limits": "c", "limits": "c",
"boundsolver.h": "c" "boundsolver.h": "c",
"randutils.h": "c",
"mathutils.h": "c"
} }
} }

View File

@ -1265,6 +1265,8 @@ Add hard walls to bounds solver
Cellular bounds transfer properly Cellular bounds transfer properly
Fluid chunk terrain bounds transfer Fluid chunk terrain bounds transfer
Cellular transfer behavior work Cellular transfer behavior work
Native math utils
Frame tracking on native side
# TODO # TODO

View File

@ -71,7 +71,8 @@ typedef struct {
double existingDensity; double existingDensity;
double newDensity; double newDensity;
float normalizationRatio; float normalizationRatio;
} FluidDensityTracking; int frame;
} FluidSimState;
/** /**
* Stores data about the simulation environment * Stores data about the simulation environment
@ -80,7 +81,7 @@ typedef struct {
JNILookupTable lookupTable; JNILookupTable lookupTable;
FluidSimQueue queue; FluidSimQueue queue;
FluidSimConsts consts; FluidSimConsts consts;
FluidDensityTracking densityTracking; FluidSimState state;
} Environment; } Environment;
/** /**

View File

@ -0,0 +1,23 @@
#ifndef MATHUTILS_H
#define MATHUTILS_H
#include "public.h"
/**
* Calculates the fractional component of a float
*/
LIBRARY_API float fract(float x);
/**
* Calculates the dot product of 2D vectors
*/
LIBRARY_API float dot2(float x1, float y1, float x2, float y2);
/**
* Calculates the dot product of 3D vectors
*/
LIBRARY_API float dot3(float x1, float y1, float z1, float x2, float y2, float z2);
#endif

View File

@ -0,0 +1,28 @@
#ifndef RANDUTILS_H
#define RANDUTILS_H
#include "public.h"
/**
* Generates a random number given a seed value
*/
LIBRARY_API float randutils_rand1(float x);
/**
* Generates a random number given two seed values
*/
LIBRARY_API float randutils_rand2(float x, float y);
/**
* Generates a random number given three seed values
*/
LIBRARY_API float randutils_rand3(float x, float y, float z);
/**
* Maps a float of range [0,1] to an integer range
*/
LIBRARY_API float randutils_map(float x, int min, int max);
#endif

View File

@ -100,9 +100,10 @@ JNIEXPORT void JNICALL Java_electrosphere_server_fluid_simulator_FluidAccelerate
//store variables from java side //store variables from java side
environment->consts.gravity = gravity; environment->consts.gravity = gravity;
environment->densityTracking.existingDensity = 0; environment->state.existingDensity = 0;
environment->densityTracking.newDensity = 0; environment->state.newDensity = 0;
environment->densityTracking.normalizationRatio = 0; environment->state.normalizationRatio = 0;
environment->state.frame = 0;
//store jni lookup tables //store jni lookup tables
jclass listClass = (*env)->FindClass(env,"java/util/List"); jclass listClass = (*env)->FindClass(env,"java/util/List");

View File

@ -89,7 +89,11 @@ void updateMetadata(JNIEnv * env, int numChunks, Chunk ** passedInChunks, Enviro
} }
//alert java side to updated static values //alert java side to updated static values
float normalizationRatio = environment->densityTracking.normalizationRatio; float normalizationRatio = environment->state.normalizationRatio;
(*env)->SetStaticFloatField(env,environment->lookupTable.serverFluidChunkClass,environment->lookupTable.serverFluidChunkTable.normalizationRatioId,normalizationRatio); (*env)->SetStaticFloatField(env,environment->lookupTable.serverFluidChunkClass,environment->lookupTable.serverFluidChunkTable.normalizationRatioId,normalizationRatio);
//update frame state
environment->state.frame += 1;
} }

View File

@ -4,6 +4,7 @@
#include "fluid/sim/cellular/cellular.h" #include "fluid/sim/cellular/cellular.h"
#include "fluid/queue/chunkmask.h" #include "fluid/queue/chunkmask.h"
#include "fluid/env/utilities.h" #include "fluid/env/utilities.h"
#include "math/randutils.h"
#define FLUID_CELLULAR_DIFFUSE_RATE 0.001 #define FLUID_CELLULAR_DIFFUSE_RATE 0.001
@ -43,8 +44,10 @@ LIBRARY_API void fluid_cellular_simulate(Environment * environment){
int transferred = 0; int transferred = 0;
// printf("%f %f %f %d %d %d\n",bounds[IX(0,1,1)],bounds[IX(1,0,1)],bounds[IX(1,1,0)],currentChunk->x,currentChunk->y,currentChunk->z); // printf("%f %f %f %d %d %d\n",bounds[IX(0,1,1)],bounds[IX(1,0,1)],bounds[IX(1,1,0)],currentChunk->x,currentChunk->y,currentChunk->z);
for(int x = 0; x < DIM; x++){ for(int y = 0; y < DIM; y++){
for(int y = 0; y < DIM; y++){ int shift = randutils_map(randutils_rand2(environment->state.frame,y),0,FLUID_CELLULAR_KERNEL_PERMUTATIONS - 1);
int permutation = randutils_map(randutils_rand2(environment->state.frame,y + 1),0,FLUID_CELLULAR_KERNEL_PERMUTATIONS - 1);
for(int x = 0; x < DIM; x++){
for(int z = 0; z < DIM; z++){ for(int z = 0; z < DIM; z++){
//diffuse density //diffuse density
@ -116,10 +119,10 @@ LIBRARY_API void fluid_cellular_simulate(Environment * environment){
} }
} }
//transfer laterally //transfer laterally
int permutation = (z % (FLUID_CELLULAR_KERNEL_PERMUTATIONS / 2)) + ((x % (FLUID_CELLULAR_KERNEL_PERMUTATIONS / 2)) * (FLUID_CELLULAR_KERNEL_PERMUTATIONS / 2)); // int permutation = (z % (FLUID_CELLULAR_KERNEL_PERMUTATIONS / 2)) + ((x % (FLUID_CELLULAR_KERNEL_PERMUTATIONS / 2)) * (FLUID_CELLULAR_KERNEL_PERMUTATIONS / 2));
for(int j = 0; j < FLUID_CELLULAR_KERNEL_SIZE; j++){ // for(int j = 0; j < FLUID_CELLULAR_KERNEL_SIZE; j++){
int nX = x + fluid_cellular_kernel_x[permutation][j]; int nX = x + fluid_cellular_kernel_x[permutation][shift];
int nZ = z + fluid_cellular_kernel_z[permutation][j]; int nZ = z + fluid_cellular_kernel_z[permutation][shift];
if(nX < 0 || nX >= DIM || nZ < 0 || nZ >= DIM){ if(nX < 0 || nX >= DIM || nZ < 0 || nZ >= DIM){
continue; continue;
} }
@ -130,7 +133,7 @@ LIBRARY_API void fluid_cellular_simulate(Environment * environment){
break; break;
} }
} }
} // }
} }
} }
} }

View File

@ -26,8 +26,8 @@ void addDensity(
float * x = GET_ARR_RAW(d,CENTER_LOC); float * x = GET_ARR_RAW(d,CENTER_LOC);
float * s = GET_ARR_RAW(d0,CENTER_LOC); float * s = GET_ARR_RAW(d0,CENTER_LOC);
for(i=0; i<size; i++){ for(i=0; i<size; i++){
environment->densityTracking.newDensity = environment->densityTracking.newDensity + dt * s[i]; environment->state.newDensity = environment->state.newDensity + dt * s[i];
environment->densityTracking.existingDensity = environment->densityTracking.existingDensity + x[i]; environment->state.existingDensity = environment->state.existingDensity + x[i];
x[i] += dt*s[i]; x[i] += dt*s[i];
if(x[i] < MIN_FLUID_VALUE){ if(x[i] < MIN_FLUID_VALUE){
x[i] = MIN_FLUID_VALUE; x[i] = MIN_FLUID_VALUE;

View File

@ -445,8 +445,8 @@ void fluid_grid_simulate(
//add density //add density
{ {
double deltaDensity = 0; double deltaDensity = 0;
environment->densityTracking.existingDensity = 0; environment->state.existingDensity = 0;
environment->densityTracking.newDensity = 0; environment->state.newDensity = 0;
for(int i = 0; i < numChunks; i++){ for(int i = 0; i < numChunks; i++){
Chunk * currentChunk = chunks[i]; Chunk * currentChunk = chunks[i];
addDensity(environment,DIM,currentChunk->chunkMask,currentChunk->d,currentChunk->d0,timestep); addDensity(environment,DIM,currentChunk->chunkMask,currentChunk->d,currentChunk->d0,timestep);
@ -525,8 +525,8 @@ void fluid_grid_simulate(
} }
float normalizationRatio = 0; float normalizationRatio = 0;
if(transformedDensity != 0){ if(transformedDensity != 0){
normalizationRatio = (environment->densityTracking.existingDensity + environment->densityTracking.newDensity) / transformedDensity; normalizationRatio = (environment->state.existingDensity + environment->state.newDensity) / transformedDensity;
environment->densityTracking.normalizationRatio = normalizationRatio; environment->state.normalizationRatio = normalizationRatio;
} }
for(int i = 0; i < numChunks; i++){ for(int i = 0; i < numChunks; i++){
Chunk * currentChunk = chunks[i]; Chunk * currentChunk = chunks[i];

View File

@ -0,0 +1,27 @@
#include <math.h>
#include "math/mathutils.h"
/**
* Calculates the fractional component of a float
*/
LIBRARY_API float fract(float x){
return x - floor(x);
}
/**
* Calculates the dot product of 2D vectors
*/
LIBRARY_API float dot2(float x1, float y1, float x2, float y2){
return (x1 * x2) + (y1 * y2);
}
/**
* Calculates the dot product of 3D vectors
*/
LIBRARY_API float dot3(float x1, float y1, float z1, float x2, float y2, float z2){
return (x1 * x2) + (y1 * y2) + (z1 * z2);
}

View File

@ -0,0 +1,46 @@
#include <math.h>
#include "math/mathutils.h"
#include "math/randutils.h"
/**
* The magnitude of the random oscillator
*/
#define MATHUTILS_RAND_MAG 100000.0f
/**
* Vectors used for prng generation
*/
#define MATHUTILS_RAND_VEC_X 111.154315f
#define MATHUTILS_RAND_VEC_Y 123.631631f
#define MATHUTILS_RAND_VEC_Z 117.724545f
/**
* Generates a random number given a seed value
*/
LIBRARY_API float randutils_rand1(float x){
return fract(sin(x) * MATHUTILS_RAND_MAG);
}
/**
* Generates a random number given two seed values
*/
LIBRARY_API float randutils_rand2(float x, float y){
return fract(sin(dot2(x,y,MATHUTILS_RAND_VEC_X,MATHUTILS_RAND_VEC_Y)) * MATHUTILS_RAND_MAG);
}
/**
* Generates a random number given three seed values
*/
LIBRARY_API float randutils_rand3(float x, float y, float z){
return fract(sin(dot3(x,y,z,MATHUTILS_RAND_VEC_X,MATHUTILS_RAND_VEC_Y,MATHUTILS_RAND_VEC_Z)) * MATHUTILS_RAND_MAG);
}
/**
* Maps a float of range [0,1] to an integer range
*/
LIBRARY_API float randutils_map(float x, int min, int max){
return (int)(x * (max - min) + min);
}

View File

@ -0,0 +1,26 @@
#include "math/mathutils.h"
#include "../util/test.h"
int math_mathutils_tests(){
int rVal = 0;
//test fract()
rVal += assertEqualsFloat(0.5,fract(1.5),"Fract failed to calculate correctly! %f %f \n");
rVal += assertEqualsFloat(0.0,fract(1.0),"Fract failed to calculate correctly! %f %f \n");
//test dot2()
rVal += assertEqualsFloat(1,dot2(1,0,1,0),"dot2 should be 1! %f %f \n");
rVal += assertEqualsFloat(-1,dot2(1,0,-1,0),"dot2 should be -1! %f %f \n");
rVal += assertEqualsFloat(0,dot2(1,0,0,1),"dot2 should be 0! %f %f \n");
//test dot3()
rVal += assertEqualsFloat(1,dot3(1,0,0,1,0,0),"dot3 should be 1! %f %f \n");
rVal += assertEqualsFloat(-1,dot3(1,0,0,-1,0,0),"dot3 should be -1! %f %f \n");
rVal += assertEqualsFloat(0,dot3(1,0,0,0,1,0),"dot3 should be 0! %f %f \n");
return rVal;
}

View File

@ -0,0 +1,15 @@
#include "math/mathutils.h"
#include "math/randutils.h"
#include "../util/test.h"
int math_randutils_tests(){
int rVal = 0;
randutils_rand1(1);
randutils_rand2(1,2);
randutils_rand3(1,2,3);
return rVal;
}