15 int xSize,
int ySize,
int zSize,
int numXThreads,
int numYThreads,
int numZThreads,
16 std::shared_ptr<ConstantBuffer>
const& parameters,
int numIterations)
18 mNumXGroups(xSize/numXThreads),
19 mNumYGroups(ySize/numYThreads),
20 mNumZGroups(zSize/numZThreads),
21 mNumIterations(numIterations)
24 mPoisson0->SetUsage(Resource::SHADER_OUTPUT);
26 mPoisson1->SetUsage(Resource::SHADER_OUTPUT);
28 int i = factory->GetAPI();
29 factory->PushDefines();
30 factory->defines.Set(
"NUM_X_THREADS", numXThreads);
31 factory->defines.Set(
"NUM_Y_THREADS", numYThreads);
32 factory->defines.Set(
"NUM_Z_THREADS", numZThreads);
48 factory->defines.Clear();
49 factory->defines.Set(
"USE_ZERO_X_FACE", 1);
50 factory->defines.Set(
"NUM_Y_THREADS", numYThreads);
51 factory->defines.Set(
"NUM_Z_THREADS", numZThreads);
54 factory->defines.Clear();
55 factory->defines.Set(
"USE_ZERO_Y_FACE", 1);
56 factory->defines.Set(
"NUM_X_THREADS", numXThreads);
57 factory->defines.Set(
"NUM_Z_THREADS", numZThreads);
60 factory->defines.Clear();
61 factory->defines.Set(
"USE_ZERO_Z_FACE", 1);
62 factory->defines.Set(
"NUM_X_THREADS", numXThreads);
63 factory->defines.Set(
"NUM_Y_THREADS", numYThreads);
66 factory->PopDefines();
70 std::shared_ptr<Texture3>
const& divergence)
72 std::shared_ptr<ComputeShader> solve =
mSolvePoisson->GetCShader();
73 std::shared_ptr<ComputeShader> xwrite =
mWriteXFace->GetCShader();
74 std::shared_ptr<ComputeShader> ywrite =
mWriteYFace->GetCShader();
75 std::shared_ptr<ComputeShader> zwrite =
mWriteZFace->GetCShader();
77 solve->Set(
"divergence", divergence);
100 "layout(r32f) uniform writeonly image3D poisson;\n" 102 "layout (local_size_x = NUM_X_THREADS, local_size_y = NUM_Y_THREADS, local_size_z = NUM_Z_THREADS) in;\n" 105 " ivec3 c = ivec3(gl_GlobalInvocationID.xyz);\n" 106 " imageStore(poisson, c, vec4(0.0f, 0.0f, 0.0f, 0.0f));\n" 110 "uniform Parameters\n" 112 " vec4 spaceDelta; // (dx, dy, dz, 0)\n" 113 " vec4 halfDivDelta; // (0.5/dx, 0.5/dy, 0.5/dz, 0)\n" 114 " vec4 timeDelta; // (dt/dx, dt/dy, dt/dz, dt)\n" 115 " vec4 viscosityX; // (velVX, velVX, velVX, denVX)\n" 116 " vec4 viscosityY; // (velVX, velVY, velVY, denVY)\n" 117 " vec4 viscosityZ; // (velVZ, velVZ, velVZ, denVZ)\n" 118 " vec4 epsilon; // (epsilonX, epsilonY, epsilonZ, epsilon0)\n" 121 "layout(r32f) uniform readonly image3D divergence;\n" 122 "layout(r32f) uniform readonly image3D poisson;\n" 123 "layout(r32f) uniform writeonly image3D outPoisson;\n" 125 "layout (local_size_x = NUM_X_THREADS, local_size_y = NUM_Y_THREADS, local_size_z = NUM_Z_THREADS) in;\n" 128 " ivec3 c = ivec3(gl_GlobalInvocationID.xyz);\n" 129 " ivec3 dim = imageSize(divergence);\n" 131 " int x = int(c.x);\n" 132 " int y = int(c.y);\n" 133 " int z = int(c.z);\n" 134 " int xm = max(x - 1, 0);\n" 135 " int xp = min(x + 1, dim.x - 1);\n" 136 " int ym = max(y - 1, 0);\n" 137 " int yp = min(y + 1, dim.y - 1);\n" 138 " int zm = max(z - 1, 0);\n" 139 " int zp = min(z + 1, dim.z - 1);\n" 141 " // Sample the divergence at (x,y,z).\n" 142 " float div = imageLoad(divergence, c).x;\n" 144 " // Sample Poisson values at immediate neighbors of (x,y,z).\n" 145 " float poisPZZ = imageLoad(poisson, ivec3(xp, y, z)).x;\n" 146 " float poisMZZ = imageLoad(poisson, ivec3(xm, y, z)).x;\n" 147 " float poisZPZ = imageLoad(poisson, ivec3(x, yp, z)).x;\n" 148 " float poisZMZ = imageLoad(poisson, ivec3(x, ym, z)).x;\n" 149 " float poisZZP = imageLoad(poisson, ivec3(x, y, zp)).x;\n" 150 " float poisZZM = imageLoad(poisson, ivec3(x, y, zm)).x;\n" 152 " vec4 temp = vec4(poisPZZ + poisMZZ, poisZPZ + poisZMZ, poisZZP + poisZZM, div);\n" 153 " float outPoissonValue = dot(epsilon, temp);\n" 154 " imageStore(outPoisson, c, vec4(outPoissonValue, 0.0f, 0.0f, 0.0f));\n" 158 "#if USE_ZERO_X_FACE\n" 159 "layout(r32f) uniform writeonly image3D image;\n" 161 "layout (local_size_x = 1, local_size_y = NUM_Y_THREADS, local_size_z = NUM_Z_THREADS) in;\n" 164 " ivec3 c = ivec3(gl_GlobalInvocationID.xyz);\n" 165 " ivec3 dim = imageSize(image);\n" 166 " imageStore(image, ivec3(0, c.y, c.z), vec4(0.0f));\n" 167 " imageStore(image, ivec3(dim.x - 1, c.y, c.z), vec4(0.0f));\n" 171 "#if USE_ZERO_Y_FACE\n" 172 "layout(r32f) uniform writeonly image3D image;\n" 174 "layout (local_size_x = NUM_X_THREADS, local_size_y = 1, local_size_z = NUM_Z_THREADS) in;\n" 177 " ivec3 c = ivec3(gl_GlobalInvocationID.xyz);\n" 178 " ivec3 dim = imageSize(image);\n" 179 " imageStore(image, ivec3(c.x, 0, c.z), vec4(0.0f));\n" 180 " imageStore(image, ivec3(c.x, dim.y - 1, c.z), vec4(0.0f));\n" 184 "#if USE_ZERO_Z_FACE\n" 185 "layout(r32f) uniform writeonly image3D image;\n" 187 "layout (local_size_x = NUM_X_THREADS, local_size_y = NUM_Y_THREADS, local_size_z = 1) in;\n" 190 " ivec3 c = ivec3(gl_GlobalInvocationID.xyz);\n" 191 " ivec3 dim = imageSize(image);\n" 192 " imageStore(image, ivec3(c.x, c.y, 0), vec4(0.0f));\n" 193 " imageStore(image, ivec3(c.x, c.y, dim.z - 1), vec4(0.0f));\n" 198 "RWTexture3D<float> poisson;\n" 200 "[numthreads(NUM_X_THREADS, NUM_Y_THREADS, NUM_Z_THREADS)]\n" 201 "void CSMain(uint3 c : SV_DispatchThreadID)\n" 203 " poisson[c.xyz] = 0.0f;\n" 207 "cbuffer Parameters\n" 209 " float4 spaceDelta; // (dx, dy, dz, 0)\n" 210 " float4 halfDivDelta; // (0.5/dx, 0.5/dy, 0.5/dz, 0)\n" 211 " float4 timeDelta; // (dt/dx, dt/dy, dt/dz, dt)\n" 212 " float4 viscosityX; // (velVX, velVX, velVX, denVX)\n" 213 " float4 viscosityY; // (velVX, velVY, velVY, denVY)\n" 214 " float4 viscosityZ; // (velVZ, velVZ, velVZ, denVZ)\n" 215 " float4 epsilon; // (epsilonX, epsilonY, epsilonZ, epsilon0)\n" 218 "Texture3D<float> divergence;\n" 219 "Texture3D<float> poisson;\n" 220 "RWTexture3D<float> outPoisson;\n" 222 "[numthreads(NUM_X_THREADS, NUM_Y_THREADS, NUM_Z_THREADS)]\n" 223 "void CSMain(uint3 c : SV_DispatchThreadID)\n" 226 " divergence.GetDimensions(dim.x, dim.y, dim.z);\n" 228 " int x = int(c.x);\n" 229 " int y = int(c.y);\n" 230 " int z = int(c.z);\n" 231 " int xm = max(x - 1, 0);\n" 232 " int xp = min(x + 1, dim.x - 1);\n" 233 " int ym = max(y - 1, 0);\n" 234 " int yp = min(y + 1, dim.y - 1);\n" 235 " int zm = max(z - 1, 0);\n" 236 " int zp = min(z + 1, dim.z - 1);\n" 238 " // Sample the divergence at (x,y,z).\n" 239 " float div = divergence[c];\n" 241 " // Sample Poisson values at immediate neighbors of (x,y,z).\n" 242 " float poisPZZ = poisson[int3(xp, y, z)];\n" 243 " float poisMZZ = poisson[int3(xm, y, z)];\n" 244 " float poisZPZ = poisson[int3(x, yp, z)];\n" 245 " float poisZMZ = poisson[int3(x, ym, z)];\n" 246 " float poisZZP = poisson[int3(x, y, zp)];\n" 247 " float poisZZM = poisson[int3(x, y, zm)];\n" 249 " float4 temp = float4(poisPZZ + poisMZZ, poisZPZ + poisZMZ, poisZZP + poisZZM, div);\n" 250 " outPoisson[c] = dot(epsilon, temp);\n" 254 "#if USE_ZERO_X_FACE\n" 255 "RWTexture3D<float> image;\n" 257 "[numthreads(1, NUM_Y_THREADS, NUM_Z_THREADS)]\n" 258 "void CSMain(uint3 c : SV_DispatchThreadID)\n" 261 " image.GetDimensions(dim.x, dim.y, dim.z);\n" 262 " image[uint3(0, c.y, c.z)] = 0.0f;\n" 263 " image[uint3(dim.x - 1, c.y, c.z)] = 0.0f;\n" 267 "#if USE_ZERO_Y_FACE\n" 268 "RWTexture3D<float> image;\n" 270 "[numthreads(NUM_X_THREADS, 1, NUM_Z_THREADS)]\n" 271 "void CSMain(uint3 c : SV_DispatchThreadID)\n" 274 " image.GetDimensions(dim.x, dim.y, dim.z);\n" 275 " image[uint3(c.x, 0, c.z)] = 0.0f;\n" 276 " image[uint3(c.x, dim.y - 1, c.z)] = 0.0f;\n" 280 "#if USE_ZERO_Z_FACE\n" 281 "RWTexture3D<float> image;\n" 283 "[numthreads(NUM_X_THREADS, NUM_Y_THREADS, 1)]\n" 284 "void CSMain(uint3 c : SV_DispatchThreadID)\n" 287 " image.GetDimensions(dim.x, dim.y, dim.z);\n" 288 " image[uint3(c.x, c.y, 0)] = 0.0f;\n" 289 " image[uint3(c.x, c.y, dim.z - 1)] = 0.0f;\n" std::shared_ptr< Texture3 > mPoisson1
static std::string const msGLSLEnforceSource
static std::string const msHLSLEnforceSource
void Execute(std::shared_ptr< GraphicsEngine > const &engine, std::shared_ptr< Texture3 > const &divergence)
static std::string const msGLSLSolveSource
std::shared_ptr< ComputeProgram > mWriteYFace
static std::string const * msEnforceSource[ProgramFactory::PF_NUM_API]
static std::string const * msSolveSource[ProgramFactory::PF_NUM_API]
std::shared_ptr< ComputeProgram > mZeroPoisson
Fluid3SolvePoisson(std::shared_ptr< ProgramFactory > const &factory, int xSize, int ySize, int zSize, int numXThreads, int numYThreads, int numZThreads, std::shared_ptr< ConstantBuffer > const ¶meters, int numIterations)
GLsizei const GLchar *const * string
static std::string const msHLSLZeroSource
std::shared_ptr< ComputeProgram > mWriteXFace
static std::string const msGLSLZeroSource
std::shared_ptr< ComputeProgram > mWriteZFace
static std::string const msHLSLSolveSource
std::shared_ptr< ComputeProgram > mSolvePoisson
std::shared_ptr< Texture3 > mPoisson0
static std::string const * msZeroSource[ProgramFactory::PF_NUM_API]