GteFluid2UpdateState.cpp
Go to the documentation of this file.
1 // David Eberly, Geometric Tools, Redmond WA 98052
2 // Copyright (c) 1998-2017
3 // Distributed under the Boost Software License, Version 1.0.
4 // http://www.boost.org/LICENSE_1_0.txt
5 // http://www.geometrictools.com/License/Boost/LICENSE_1_0.txt
6 // File Version: 3.0.0 (2016/06/19)
7 
8 #include <GTEnginePCH.h>
12 using namespace gte;
13 
14 Fluid2UpdateState::Fluid2UpdateState(std::shared_ptr<ProgramFactory> const& factory,
15  int xSize, int ySize, int numXThreads, int numYThreads,
16  std::shared_ptr<ConstantBuffer> const& parameters)
17  :
18  mNumXGroups(xSize/numXThreads),
19  mNumYGroups(ySize/numYThreads)
20 {
21  mUpdateState = std::make_shared<Texture2>(DF_R32G32B32A32_FLOAT, xSize, ySize);
22  mUpdateState->SetUsage(Resource::SHADER_OUTPUT);
23 
24  mAdvectionSampler = std::make_shared<SamplerState>();
28 
29  // Create the shader for generating velocity from vortices.
30  int i = factory->GetAPI();
31  factory->PushDefines();
32  factory->defines.Set("NUM_X_THREADS", numXThreads);
33  factory->defines.Set("NUM_Y_THREADS", numYThreads);
34 
35  mComputeUpdateState = factory->CreateFromSource(*msSource[i]);
37  {
38  std::shared_ptr<ComputeShader> cshader = mComputeUpdateState->GetCShader();
39  cshader->Set("Parameters", parameters);
40 #if defined(GTE_DEV_OPENGL)
41  cshader->Set("stateTm1", mAdvectionSampler);
42 #else
43  cshader->Set("advectionSampler", mAdvectionSampler);
44 #endif
45  cshader->Set("updateState", mUpdateState);
46  }
47 
48  factory->PopDefines();
49 }
50 
51 void Fluid2UpdateState::Execute(std::shared_ptr<GraphicsEngine> const& engine,
52  std::shared_ptr<Texture2> const& source,
53  std::shared_ptr<Texture2> const& stateTm1,
54  std::shared_ptr<Texture2> const& stateT)
55 {
56  std::shared_ptr<ComputeShader> cshader = mComputeUpdateState->GetCShader();
57  cshader->Set("source", source);
58  cshader->Set("stateTm1", stateTm1);
59  cshader->Set("stateT", stateT);
60  engine->Execute(mComputeUpdateState, mNumXGroups, mNumYGroups, 1);
61 }
62 
63 
65 "uniform Parameters\n"
66 "{\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"
73 "};\n"
74 "\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"
79 "\n"
80 "layout (local_size_x = NUM_X_THREADS, local_size_y = NUM_Y_THREADS, local_size_z = 1) in;\n"
81 "void main()\n"
82 "{\n"
83 " ivec2 c = ivec2(gl_GlobalInvocationID.xy);\n"
84 " ivec2 dim = imageSize(stateT);\n"
85 "\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"
92 "\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"
99 "\n"
100 " // Sample the source state at (x,y).\n"
101 " vec4 src = imageLoad(source, c);\n"
102 "\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"
106 "\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"
110 "\n"
111 " // Update the state.\n"
112 " imageStore(updateState, c, advection +\n"
113 " (viscosityX*stateDXX + viscosityY*stateDYY + timeDelta.w*src));\n"
114 "}\n";
115 
117 "cbuffer Parameters\n"
118 "{\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"
125 "};\n"
126 "\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"
132 "\n"
133 "[numthreads(NUM_X_THREADS, NUM_Y_THREADS, 1)]\n"
134 "void CSMain(uint2 c : SV_DispatchThreadID)\n"
135 "{\n"
136 " uint2 dim;\n"
137 " stateT.GetDimensions(dim.x, dim.y);\n"
138 "\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"
145 "\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"
152 "\n"
153 " // Sample the source state at (x,y).\n"
154 " float4 src = source[int2(x, y)];\n"
155 "\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"
159 "\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"
163 "\n"
164 " // Update the state.\n"
165 " updateState[c] = advection +\n"
166 " (viscosityX*stateDXX + viscosityY*stateDYY + timeDelta.w*src);\n"
167 "}\n";
168 
170 {
171  &msGLSLSource,
172  &msHLSLSource
173 };
Fluid2UpdateState(std::shared_ptr< ProgramFactory > const &factory, int xSize, int ySize, int numXThreads, int numYThreads, std::shared_ptr< ConstantBuffer > const &parameters)
static std::string const msHLSLSource
GLsizei GLsizei GLchar * source
Definition: glcorearb.h:798
GLsizei const GLchar *const * string
Definition: glcorearb.h:809
DF_R32G32B32A32_FLOAT
Definition: GteDataFormat.h:20
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
MIN_L_MAG_L_MIP_P
std::shared_ptr< ComputeProgram > mComputeUpdateState
std::shared_ptr< Texture2 > mUpdateState
CLAMP


geometric_tools_engine
Author(s): Yijiang Huang
autogenerated on Thu Jul 18 2019 03:59:59