15 int xSize,
int ySize,
int numXThreads,
int numYThreads,
16 std::shared_ptr<ConstantBuffer>
const& parameters,
int numIterations)
18 mNumXGroups(xSize/numXThreads),
19 mNumYGroups(ySize/numYThreads),
20 mNumIterations(numIterations)
23 mPoisson0->SetUsage(Resource::SHADER_OUTPUT);
25 mPoisson1->SetUsage(Resource::SHADER_OUTPUT);
27 int i = factory->GetAPI();
28 factory->PushDefines();
29 factory->defines.Set(
"NUM_X_THREADS", numXThreads);
30 factory->defines.Set(
"NUM_Y_THREADS", numYThreads);
46 factory->defines.Clear();
47 factory->defines.Set(
"USE_ZERO_X_EDGE", 1);
48 factory->defines.Set(
"NUM_Y_THREADS", numYThreads);
51 factory->defines.Clear();
52 factory->defines.Set(
"USE_ZERO_Y_EDGE", 1);
53 factory->defines.Set(
"NUM_X_THREADS", numXThreads);
56 factory->PopDefines();
60 std::shared_ptr<Texture2>
const& divergence)
62 std::shared_ptr<ComputeShader> solve =
mSolvePoisson->GetCShader();
63 std::shared_ptr<ComputeShader> xwrite =
mWriteXEdge->GetCShader();
64 std::shared_ptr<ComputeShader> ywrite =
mWriteYEdge->GetCShader();
66 solve->Set(
"divergence", divergence);
87 "layout(r32f) uniform writeonly image2D poisson;\n" 89 "layout (local_size_x = NUM_X_THREADS, local_size_y = NUM_Y_THREADS, local_size_z = 1) in;\n" 92 " ivec2 c = ivec2(gl_GlobalInvocationID.xy);\n" 93 " imageStore(poisson, c, vec4(0.0f, 0.0f, 0.0f, 0.0f));\n" 97 "uniform Parameters\n" 99 " vec4 spaceDelta; // (dx, dy, 0, 0)\n" 100 " vec4 halfDivDelta; // (0.5/dx, 0.5/dy, 0, 0)\n" 101 " vec4 timeDelta; // (dt/dx, dt/dy, 0, dt)\n" 102 " vec4 viscosityX; // (velVX, velVX, 0, denVX)\n" 103 " vec4 viscosityY; // (velVX, velVY, 0, denVY)\n" 104 " vec4 epsilon; // (epsilonX, epsilonY, 0, epsilon0)\n" 107 "layout(r32f) uniform readonly image2D divergence;\n" 108 "layout(r32f) uniform readonly image2D poisson;\n" 109 "layout(r32f) uniform writeonly image2D outPoisson;\n" 111 "layout (local_size_x = NUM_X_THREADS, local_size_y = NUM_Y_THREADS, local_size_z = 1) in;\n" 114 " ivec2 c = ivec2(gl_GlobalInvocationID.xy);\n" 115 " ivec2 dim = imageSize(divergence);\n" 117 " int x = int(c.x);\n" 118 " int y = int(c.y);\n" 119 " int xm = max(x - 1, 0);\n" 120 " int xp = min(x + 1, dim.x - 1);\n" 121 " int ym = max(y - 1, 0);\n" 122 " int yp = min(y + 1, dim.y - 1);\n" 124 " // Sample the divergence at (x,y).\n" 125 " float div = imageLoad(divergence, c).x;\n" 127 " // Sample Poisson values at (x+dx,y), (x-dx,y), (x,y+dy), (x,y-dy).\n" 128 " float poisPZ = imageLoad(poisson, ivec2(xp, y)).x;\n" 129 " float poisMZ = imageLoad(poisson, ivec2(xm, y)).x;\n" 130 " float poisZP = imageLoad(poisson, ivec2(x, yp)).x;\n" 131 " float poisZM = imageLoad(poisson, ivec2(x, ym)).x;\n" 133 " vec4 temp = vec4(poisPZ + poisMZ, poisZP + poisZM, 0.0f, div);\n" 134 " float outPoissonValue = dot(epsilon, temp);\n" 135 " imageStore(outPoisson, c, vec4(outPoissonValue, 0.0f, 0.0f, 0.0f));\n" 139 "#if USE_ZERO_X_EDGE\n" 140 "layout(r32f) uniform writeonly image2D image;\n" 142 "layout (local_size_x = 1, local_size_y = NUM_Y_THREADS, local_size_z = 1) in;\n" 145 " ivec2 c = ivec2(gl_GlobalInvocationID.xy);\n" 146 " ivec2 dim = imageSize(image);\n" 147 " imageStore(image, ivec2(0, c.y), vec4(0.0f, 0.0f, 0.0f, 0.0f));\n" 148 " imageStore(image, ivec2(dim.x - 1, c.y), vec4(0.0f, 0.0f, 0.0f, 0.0f));\n" 152 "#if USE_ZERO_Y_EDGE\n" 153 "layout(r32f) uniform writeonly image2D image;\n" 155 "layout (local_size_x = NUM_X_THREADS, local_size_y = 1, local_size_z = 1) in;\n" 158 " ivec2 c = ivec2(gl_GlobalInvocationID.xy);\n" 159 " ivec2 dim = imageSize(image);\n" 160 " imageStore(image, ivec2(c.x, 0), vec4(0.0f, 0.0f, 0.0f, 0.0f));\n" 161 " imageStore(image, ivec2(c.x, dim.y - 1), vec4(0.0f, 0.0f, 0.0f, 0.0f));\n" 166 "RWTexture2D<float> poisson;\n" 168 "[numthreads(NUM_X_THREADS, NUM_Y_THREADS, 1)]\n" 169 "void CSMain(uint2 c : SV_DispatchThreadID)\n" 171 " poisson[c] = 0.0f;\n" 175 "cbuffer Parameters\n" 177 " float4 spaceDelta; // (dx, dy, 0, 0)\n" 178 " float4 halfDivDelta; // (0.5/dx, 0.5/dy, 0, 0)\n" 179 " float4 timeDelta; // (dt/dx, dt/dy, 0, dt)\n" 180 " float4 viscosityX; // (velVX, velVX, 0, denVX)\n" 181 " float4 viscosityY; // (velVX, velVY, 0, denVY)\n" 182 " float4 epsilon; // (epsilonX, epsilonY, 0, epsilon0)\n" 185 "Texture2D<float> divergence;\n" 186 "Texture2D<float> poisson;\n" 187 "RWTexture2D<float> outPoisson;\n" 189 "[numthreads(NUM_X_THREADS, NUM_Y_THREADS, 1)]\n" 190 "void CSMain(uint2 c : SV_DispatchThreadID)\n" 193 " divergence.GetDimensions(dim.x, dim.y);\n" 195 " int x = int(c.x);\n" 196 " int y = int(c.y);\n" 197 " int xm = max(x - 1, 0);\n" 198 " int xp = min(x + 1, dim.x - 1);\n" 199 " int ym = max(y - 1, 0);\n" 200 " int yp = min(y + 1, dim.y - 1);\n" 202 " // Sample the divergence at (x,y).\n" 203 " float div = divergence[int2(x, y)];\n" 205 " // Sample Poisson values at (x+dx,y), (x-dx,y), (x,y+dy), (x,y-dy).\n" 206 " float poisPZ = poisson[int2(xp, y)];\n" 207 " float poisMZ = poisson[int2(xm, y)];\n" 208 " float poisZP = poisson[int2(x, yp)];\n" 209 " float poisZM = poisson[int2(x, ym)];\n" 211 " float4 temp = float4(poisPZ + poisMZ, poisZP + poisZM, 0.0f, div);\n" 212 " outPoisson[c] = dot(epsilon, temp);\n" 216 "#if USE_ZERO_X_EDGE\n" 217 "RWTexture2D<float> image;\n" 219 "[numthreads(1, NUM_Y_THREADS, 1)]\n" 220 "void CSMain(uint2 c : SV_DispatchThreadID)\n" 223 " image.GetDimensions(dim.x, dim.y);\n" 224 " image[uint2(0, c.y)] = 0.0f;\n" 225 " image[uint2(dim.x - 1, c.y)] = 0.0f;\n" 229 "#if USE_ZERO_Y_EDGE\n" 230 "RWTexture2D<float> image;\n" 232 "[numthreads(NUM_X_THREADS, 1, 1)]\n" 233 "void CSMain(uint2 c : SV_DispatchThreadID)\n" 236 " image.GetDimensions(dim.x, dim.y);\n" 237 " image[uint2(c.x, 0)] = 0.0f;\n" 238 " image[uint2(c.x, dim.y - 1)] = 0.0f;\n"
static std::string const msHLSLSolveSource
std::shared_ptr< ComputeProgram > mSolvePoisson
static std::string const * msSolveSource[ProgramFactory::PF_NUM_API]
static std::string const msHLSLZeroSource
void Execute(std::shared_ptr< GraphicsEngine > const &engine, std::shared_ptr< Texture2 > const &divergence)
static std::string const * msZeroSource[ProgramFactory::PF_NUM_API]
static std::string const msGLSLEnforceSource
std::shared_ptr< ComputeProgram > mWriteXEdge
static std::string const * msEnforceSource[ProgramFactory::PF_NUM_API]
GLsizei const GLchar *const * string
Fluid2SolvePoisson(std::shared_ptr< ProgramFactory > const &factory, int xSize, int ySize, int numXThreads, int numYThreads, std::shared_ptr< ConstantBuffer > const ¶meters, int numIterations)
std::shared_ptr< ComputeProgram > mZeroPoisson
std::shared_ptr< Texture2 > mPoisson0
static std::string const msGLSLZeroSource
std::shared_ptr< Texture2 > mPoisson1
std::shared_ptr< ComputeProgram > mWriteYEdge
static std::string const msHLSLEnforceSource
static std::string const msGLSLSolveSource