15 int xSize,
int ySize,
int numXThreads,
int numYThreads,
16 std::shared_ptr<ConstantBuffer>
const& parameters)
18 mNumXGroups(xSize/numXThreads),
19 mNumYGroups(ySize/numYThreads)
30 int i = factory->GetAPI();
31 factory->PushDefines();
32 factory->defines.Set(
"NUM_X_THREADS", numXThreads);
33 factory->defines.Set(
"NUM_Y_THREADS", numYThreads);
39 cshader->Set(
"Parameters", parameters);
40 #if defined(GTE_DEV_OPENGL) 48 factory->PopDefines();
52 std::shared_ptr<Texture2>
const&
source,
53 std::shared_ptr<Texture2>
const& stateTm1,
54 std::shared_ptr<Texture2>
const& stateT)
57 cshader->Set(
"source", source);
58 cshader->Set(
"stateTm1", stateTm1);
59 cshader->Set(
"stateT", stateT);
65 "uniform Parameters\n" 67 " vec4 spaceDelta; // (dx, dy, 0, 0)\n" 68 " vec4 halfDivDelta; // (0.5/dx, 0.5/dy, 0, 0)\n" 69 " vec4 timeDelta; // (dt/dx, dt/dy, 0, dt)\n" 70 " vec4 viscosityX; // (velVX, velVX, 0, denVX)\n" 71 " vec4 viscosityY; // (velVX, velVY, 0, denVY)\n" 72 " vec4 epsilon; // (epsilonX, epsilonY, 0, epsilon0)\n" 75 "layout(rgba32f) uniform readonly image2D source;\n" 76 "layout(rgba32f) uniform readonly image2D stateT;\n" 77 "uniform sampler2D stateTm1;\n" 78 "layout(rgba32f) uniform writeonly image2D updateState;\n" 80 "layout (local_size_x = NUM_X_THREADS, local_size_y = NUM_Y_THREADS, local_size_z = 1) in;\n" 83 " ivec2 c = ivec2(gl_GlobalInvocationID.xy);\n" 84 " ivec2 dim = imageSize(stateT);\n" 86 " int x = int(c.x);\n" 87 " int y = int(c.y);\n" 88 " int xm = max(x - 1, 0);\n" 89 " int xp = min(x + 1, dim.x - 1);\n" 90 " int ym = max(y - 1, 0);\n" 91 " int yp = min(y + 1, dim.y - 1);\n" 93 " // Sample states at (x,y), (x+dx,y), (x-dx,y), (x,y+dy), (x,y-dy).\n" 94 " vec4 stateZZ = imageLoad(stateT, c);\n" 95 " vec4 statePZ = imageLoad(stateT, ivec2(xp, y));\n" 96 " vec4 stateMZ = imageLoad(stateT, ivec2(xm, y));\n" 97 " vec4 stateZP = imageLoad(stateT, ivec2(x, yp));\n" 98 " vec4 stateZM = imageLoad(stateT, ivec2(x, ym));\n" 100 " // Sample the source state at (x,y).\n" 101 " vec4 src = imageLoad(source, c);\n" 103 " // Estimate second-order derivatives of state at (x,y).\n" 104 " vec4 stateDXX = statePZ - 2.0f*stateZZ + stateMZ;\n" 105 " vec4 stateDYY = stateZP - 2.0f*stateZZ + stateZM;\n" 107 " // Compute advection.\n" 108 " vec2 tcd = spaceDelta.xy*(c.xy - timeDelta.xy*stateZZ.xy + 0.5f);\n" 109 " vec4 advection = textureLod(stateTm1, tcd, 0.0f);\n" 111 " // Update the state.\n" 112 " imageStore(updateState, c, advection +\n" 113 " (viscosityX*stateDXX + viscosityY*stateDYY + timeDelta.w*src));\n" 117 "cbuffer Parameters\n" 119 " float4 spaceDelta; // (dx, dy, 0, 0)\n" 120 " float4 halfDivDelta; // (0.5/dx, 0.5/dy, 0, 0)\n" 121 " float4 timeDelta; // (dt/dx, dt/dy, 0, dt)\n" 122 " float4 viscosityX; // (velVX, velVX, 0, denVX)\n" 123 " float4 viscosityY; // (velVX, velVY, 0, denVY)\n" 124 " float4 epsilon; // (epsilonX, epsilonY, 0, epsilon0)\n" 127 "Texture2D<float4> source;\n" 128 "Texture2D<float4> stateTm1;\n" 129 "Texture2D<float4> stateT;\n" 130 "SamplerState advectionSampler; // bilinear, clamp\n" 131 "RWTexture2D<float4> updateState;\n" 133 "[numthreads(NUM_X_THREADS, NUM_Y_THREADS, 1)]\n" 134 "void CSMain(uint2 c : SV_DispatchThreadID)\n" 137 " stateT.GetDimensions(dim.x, dim.y);\n" 139 " int x = int(c.x);\n" 140 " int y = int(c.y);\n" 141 " int xm = max(x - 1, 0);\n" 142 " int xp = min(x + 1, dim.x - 1);\n" 143 " int ym = max(y - 1, 0);\n" 144 " int yp = min(y + 1, dim.y - 1);\n" 146 " // Sample states at (x,y), (x+dx,y), (x-dx,y), (x,y+dy), (x,y-dy).\n" 147 " float4 stateZZ = stateT[int2(x, y)];\n" 148 " float4 statePZ = stateT[int2(xp, y)];\n" 149 " float4 stateMZ = stateT[int2(xm, y)];\n" 150 " float4 stateZP = stateT[int2(x, yp)];\n" 151 " float4 stateZM = stateT[int2(x, ym)];\n" 153 " // Sample the source state at (x,y).\n" 154 " float4 src = source[int2(x, y)];\n" 156 " // Estimate second-order derivatives of state at (x,y).\n" 157 " float4 stateDXX = statePZ - 2.0f*stateZZ + stateMZ;\n" 158 " float4 stateDYY = stateZP - 2.0f*stateZZ + stateZM;\n" 160 " // Compute advection.\n" 161 " float2 tcd = spaceDelta.xy*(c - timeDelta.xy*stateZZ.xy + 0.5f);\n" 162 " float4 advection = stateTm1.SampleLevel(advectionSampler, tcd, 0.0f);\n" 164 " // Update the state.\n" 165 " updateState[c] = advection +\n" 166 " (viscosityX*stateDXX + viscosityY*stateDYY + timeDelta.w*src);\n"
Fluid2UpdateState(std::shared_ptr< ProgramFactory > const &factory, int xSize, int ySize, int numXThreads, int numYThreads, std::shared_ptr< ConstantBuffer > const ¶meters)
static std::string const msHLSLSource
GLsizei GLsizei GLchar * source
GLsizei const GLchar *const * string
void Execute(std::shared_ptr< GraphicsEngine > const &engine, std::shared_ptr< Texture2 > const &source, std::shared_ptr< Texture2 > const &stateTm1, std::shared_ptr< Texture2 > const &stateT)
static std::string const msGLSLSource
static std::string const * msSource[ProgramFactory::PF_NUM_API]
std::shared_ptr< SamplerState > mAdvectionSampler
std::shared_ptr< ComputeProgram > mComputeUpdateState
std::shared_ptr< Texture2 > mUpdateState