16 int xSize,
int ySize,
int numXThreads,
int numYThreads,
17 std::shared_ptr<ConstantBuffer>
const& parameters)
19 mNumXGroups(xSize/numXThreads),
20 mNumYGroups(ySize/numYThreads)
23 mVortex = std::make_shared<ConstantBuffer>(
sizeof(
Vortex),
true);
34 e.
gravity = { 0.0f, 0.0f, 0.0f, 0.0f };
35 e.
wind = { 0.0f, 0.5f, 0.001f, 32.0f };
37 mSource->SetUsage(Resource::SHADER_OUTPUT);
40 int i = factory->GetAPI();
41 factory->PushDefines();
42 factory->defines.Set(
"NUM_X_THREADS", numXThreads);
43 factory->defines.Set(
"NUM_Y_THREADS", numYThreads);
44 std::shared_ptr<ComputeShader> cshader;
50 cshader->Set(
"Parameters", parameters);
51 cshader->Set(
"Vortex",
mVortex);
61 cshader->Set(
"Parameters", parameters);
63 cshader->Set(
"source",
mSource);
66 factory->PopDefines();
73 std::uniform_real_distribution<float> unirnd(0.0
f, 1.0
f);
74 std::uniform_real_distribution<float> symrnd(-1.0
f, 1.0
f);
75 std::uniform_real_distribution<float> posrnd0(0.001
f, 0.01
f);
76 std::uniform_real_distribution<float> posrnd1(128.0
f, 256.0
f);
85 v.
data[0] = unirnd(mte);
86 v.data[1] = unirnd(mte);
87 v.data[2] = posrnd0(mte);
88 v.data[3] = posrnd1(mte);
89 if (symrnd(mte) < 0.0
f)
91 v.data[3] = -v.data[3];
111 "uniform Parameters\n" 113 " vec4 spaceDelta; // (dx, dy, 0, 0)\n" 114 " vec4 halfDivDelta; // (0.5/dx, 0.5/dy, 0, 0)\n" 115 " vec4 timeDelta; // (dt/dx, dt/dy, 0, dt)\n" 116 " vec4 viscosityX; // (velVX, velVX, 0, denVX)\n" 117 " vec4 viscosityY; // (velVX, velVY, 0, denVY)\n" 118 " vec4 epsilon; // (epsilonX, epsilonY, 0, epsilon0)\n" 123 " vec4 data; // (x, y, variance, amplitude)\n" 126 "layout(rg32f) uniform readonly image2D inVelocity;\n" 127 "layout(rg32f) uniform writeonly image2D outVelocity;\n" 129 "layout (local_size_x = NUM_X_THREADS, local_size_y = NUM_Y_THREADS, local_size_z = 1) in;\n" 132 " ivec2 c = ivec2(gl_GlobalInvocationID.xy);\n" 133 " vec2 location = spaceDelta.xy*(c + 0.5f);\n" 134 " vec2 diff = location - data.xy;\n" 135 " float arg = -dot(diff, diff) / data.z;\n" 136 " float magnitude = data.w*exp(arg);\n" 137 " vec2 vortexVelocity = magnitude*vec2(diff.y, -diff.x);\n" 138 " imageStore(outVelocity, c, vec4(imageLoad(inVelocity, c).xy + vortexVelocity, 0.0f, 0.0f));\n" 142 "uniform Parameters\n" 144 " vec4 spaceDelta; // (dx, dy, 0, 0)\n" 145 " vec4 halfDivDelta; // (0.5/dx, 0.5/dy, 0, 0)\n" 146 " vec4 timeDelta; // (dt/dx, dt/dy, 0, dt)\n" 147 " vec4 viscosityX; // (velVX, velVX, 0, denVX)\n" 148 " vec4 viscosityY; // (velVX, velVY, 0, denVY)\n" 149 " vec4 epsilon; // (epsilonX, epsilonY, 0, epsilon0)\n" 154 " vec4 densityProducer; // (x, y, variance, amplitude)\n" 155 " vec4 densityConsumer; // (x, y, variance, amplitude)\n" 156 " vec4 gravity; // (x, y, *, *)\n" 157 " vec4 wind; // (x, y, variance, amplitude)\n" 160 "layout(rg32f) uniform readonly image2D vortexVelocity;\n" 161 "layout(rgba32f) uniform writeonly image2D source;\n" 163 "layout (local_size_x = NUM_X_THREADS, local_size_y = NUM_Y_THREADS, local_size_z = 1) in;\n" 166 " ivec2 c = ivec2(gl_GlobalInvocationID.xy);\n" 168 " // Compute the location of the pixel (x,y) in normalized [0,1]^2.\n" 169 " vec2 location = spaceDelta.xy*(c + 0.5f);\n" 171 " // Compute an input to the fluid simulation consisting of a producer of\n" 172 " // density and a consumer of density.\n" 173 " vec2 diff = location - densityProducer.xy;\n" 174 " float arg = -dot(diff, diff) / densityProducer.z;\n" 175 " float density = densityProducer.w*exp(arg);\n" 176 " diff = location - densityConsumer.xy;\n" 177 " arg = -dot(diff, diff) / densityConsumer.z;\n" 178 " density -= densityConsumer.w*exp(arg);\n" 180 " // Compute an input to the fluid simulation consisting of gravity,\n" 181 " // a single wind source, and vortex impulses.\n" 182 " float windDiff = location.y - wind.y;\n" 183 " float windArg = -windDiff*windDiff / wind.z;\n" 184 " vec2 windVelocity = vec2(wind.w*exp(windArg), 0.0f);\n" 185 " vec2 velocity = gravity.xy + windVelocity + imageLoad(vortexVelocity, c).xy;\n" 187 " imageStore(source, c, vec4(velocity, 0.0f, density));\n" 191 "cbuffer Parameters\n" 193 " float4 spaceDelta; // (dx, dy, 0, 0)\n" 194 " float4 halfDivDelta; // (0.5/dx, 0.5/dy, 0, 0)\n" 195 " float4 timeDelta; // (dt/dx, dt/dy, 0, dt)\n" 196 " float4 viscosityX; // (velVX, velVX, 0, denVX)\n" 197 " float4 viscosityY; // (velVX, velVY, 0, denVY)\n" 198 " float4 epsilon; // (epsilonX, epsilonY, 0, epsilon0)\n" 203 " float4 data; // (x, y, variance, amplitude)\n" 206 "Texture2D<float2> inVelocity;\n" 207 "RWTexture2D<float2> outVelocity;\n" 209 "[numthreads(NUM_X_THREADS, NUM_Y_THREADS, 1)]\n" 210 "void CSMain(uint3 c : SV_DispatchThreadID)\n" 212 " float2 location = spaceDelta.xy*(c.xy + 0.5f);\n" 213 " float2 diff = location - data.xy;\n" 214 " float arg = -dot(diff, diff) / data.z;\n" 215 " float magnitude = data.w*exp(arg);\n" 216 " float2 vortexVelocity = magnitude*float2(diff.y, -diff.x);\n" 217 " outVelocity[c.xy] = inVelocity[c.xy] + vortexVelocity;\n" 221 "cbuffer Parameters\n" 223 " float4 spaceDelta; // (dx, dy, 0, 0)\n" 224 " float4 halfDivDelta; // (0.5/dx, 0.5/dy, 0, 0)\n" 225 " float4 timeDelta; // (dt/dx, dt/dy, 0, dt)\n" 226 " float4 viscosityX; // (velVX, velVX, 0, denVX)\n" 227 " float4 viscosityY; // (velVX, velVY, 0, denVY)\n" 228 " float4 epsilon; // (epsilonX, epsilonY, 0, epsilon0)\n" 233 " float4 densityProducer; // (x, y, variance, amplitude)\n" 234 " float4 densityConsumer; // (x, y, variance, amplitude)\n" 235 " float4 gravity; // (x, y, *, *)\n" 236 " float4 wind; // (x, y, variance, amplitude)\n" 239 "Texture2D<float2> vortexVelocity;\n" 240 "RWTexture2D<float4> source;\n" 242 "[numthreads(NUM_X_THREADS, NUM_Y_THREADS, 1)]\n" 243 "void CSMain(uint2 c : SV_DispatchThreadID)\n" 245 " // Compute the location of the pixel (x,y) in normalized [0,1]^2.\n" 246 " float2 location = spaceDelta.xy*(c + 0.5f);\n" 248 " // Compute an input to the fluid simulation consisting of a producer of\n" 249 " // density and a consumer of density.\n" 250 " float2 diff = location - densityProducer.xy;\n" 251 " float arg = -dot(diff, diff) / densityProducer.z;\n" 252 " float density = densityProducer.w*exp(arg);\n" 253 " diff = location - densityConsumer.xy;\n" 254 " arg = -dot(diff, diff) / densityConsumer.z;\n" 255 " density -= densityConsumer.w*exp(arg);\n" 257 " // Compute an input to the fluid simulation consisting of gravity,\n" 258 " // a single wind source, and vortex impulses.\n" 259 " float windDiff = location.y - wind.y;\n" 260 " float windArg = -windDiff*windDiff / wind.z;\n" 261 " float2 windVelocity = float2(wind.w*exp(windArg), 0.0f);\n" 262 " float2 velocity = gravity.xy + windVelocity + vortexVelocity[c];\n" 264 " source[c] = float4(velocity, 0.0f, density);\n" static std::string const msHLSLInitializeSource
void Execute(std::shared_ptr< GraphicsEngine > const &engine)
static std::string const msGLSLGenerateSource
Fluid2InitializeSource(std::shared_ptr< ProgramFactory > const &factory, int xSize, int ySize, int numXThreads, int numYThreads, std::shared_ptr< ConstantBuffer > const ¶meters)
std::shared_ptr< Texture2 > mVelocity0
static std::string const * msInitializeSource[ProgramFactory::PF_NUM_API]
Vector4< float > densityProducer
static std::string const msHLSLGenerateSource
std::shared_ptr< ConstantBuffer > mExternal
GLsizei const GLchar *const * string
std::shared_ptr< Texture2 > mSource
Vector4< float > densityConsumer
std::shared_ptr< ConstantBuffer > mVortex
std::shared_ptr< ComputeProgram > mInitializeSource
std::shared_ptr< ComputeProgram > mGenerateVortex
static std::string const msGLSLInitializeSource
std::shared_ptr< Texture2 > mVelocity1
static std::string const * msGenerateSource[ProgramFactory::PF_NUM_API]