GteFluid3AdjustVelocity.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 Fluid3AdjustVelocity::Fluid3AdjustVelocity(std::shared_ptr<ProgramFactory> const& factory,
15  int xSize, int ySize, int zSize, int numXThreads, int numYThreads, int numZThreads,
16  std::shared_ptr<ConstantBuffer> const& parameters)
17  :
18  mNumXGroups(xSize/numXThreads),
19  mNumYGroups(ySize/numYThreads),
20  mNumZGroups(zSize/numZThreads)
21 {
22  int i = factory->GetAPI();
23  factory->PushDefines();
24  factory->defines.Set("NUM_X_THREADS", numXThreads);
25  factory->defines.Set("NUM_Y_THREADS", numYThreads);
26  factory->defines.Set("NUM_Z_THREADS", numZThreads);
27 
28  mAdjustVelocity = factory->CreateFromSource(*msSource[i]);
29  if (mAdjustVelocity)
30  {
31  mAdjustVelocity->GetCShader()->Set("Parameters", parameters);
32  }
33 
34  factory->PopDefines();
35 }
36 
37 void Fluid3AdjustVelocity::Execute(std::shared_ptr<GraphicsEngine> const& engine,
38  std::shared_ptr<Texture3> const& inState,
39  std::shared_ptr<Texture3> const& poisson,
40  std::shared_ptr<Texture3> const& outState)
41 {
42  std::shared_ptr<ComputeShader> cshader = mAdjustVelocity->GetCShader();
43  cshader->Set("inState", inState);
44  cshader->Set("poisson", poisson);
45  cshader->Set("outState", outState);
47 }
48 
49 
51 "uniform Parameters\n"
52 "{\n"
53 " vec4 spaceDelta; // (dx, dy, dz, 0)\n"
54 " vec4 halfDivDelta; // (0.5/dx, 0.5/dy, 0.5/dz, 0)\n"
55 " vec4 timeDelta; // (dt/dx, dt/dy, dt/dz, dt)\n"
56 " vec4 viscosityX; // (velVX, velVX, velVX, denVX)\n"
57 " vec4 viscosityY; // (velVX, velVY, velVY, denVY)\n"
58 " vec4 viscosityZ; // (velVZ, velVZ, velVZ, denVZ)\n"
59 " vec4 epsilon; // (epsilonX, epsilonY, epsilonZ, epsilon0)\n"
60 "};\n"
61 "\n"
62 "layout(rgba32f) uniform readonly image3D inState;\n"
63 "layout(r32f) uniform readonly image3D poisson;\n"
64 "layout(rgba32f) uniform writeonly image3D outState;\n"
65 "\n"
66 "layout (local_size_x = NUM_X_THREADS, local_size_y = NUM_Y_THREADS, local_size_z = NUM_Z_THREADS) in;\n"
67 "void main()\n"
68 "{\n"
69 " ivec3 c = ivec3(gl_GlobalInvocationID.xyz);\n"
70 " ivec3 dim = imageSize(inState);\n"
71 "\n"
72 " int x = int(c.x);\n"
73 " int y = int(c.y);\n"
74 " int z = int(c.z);\n"
75 " int xm = max(x - 1, 0);\n"
76 " int xp = min(x + 1, dim.x - 1);\n"
77 " int ym = max(y - 1, 0);\n"
78 " int yp = min(y + 1, dim.y - 1);\n"
79 " int zm = max(z - 1, 0);\n"
80 " int zp = min(z + 1, dim.z - 1);\n"
81 "\n"
82 " // Sample the state at (x,y,z).\n"
83 " vec4 state = imageLoad(inState, c);\n"
84 "\n"
85 " // Sample Poisson values at immediate neighbors of (x,y,z).\n"
86 " float poisPZZ = imageLoad(poisson, ivec3(xp, y, z)).x;\n"
87 " float poisMZZ = imageLoad(poisson, ivec3(xm, y, z)).x;\n"
88 " float poisZPZ = imageLoad(poisson, ivec3(x, yp, z)).x;\n"
89 " float poisZMZ = imageLoad(poisson, ivec3(x, ym, z)).x;\n"
90 " float poisZZP = imageLoad(poisson, ivec3(x, y, zp)).x;\n"
91 " float poisZZM = imageLoad(poisson, ivec3(x, y, zm)).x;\n"
92 "\n"
93 " vec4 diff = vec4(poisPZZ - poisMZZ, poisZPZ - poisZMZ, poisZZP - poisZZM, 0.0f);\n"
94 " imageStore(outState, c, state + halfDivDelta*diff);\n"
95 "}\n";
96 
98 "cbuffer Parameters\n"
99 "{\n"
100 " float4 spaceDelta; // (dx, dy, dz, 0)\n"
101 " float4 halfDivDelta; // (0.5/dx, 0.5/dy, 0.5/dz, 0)\n"
102 " float4 timeDelta; // (dt/dx, dt/dy, dt/dz, dt)\n"
103 " float4 viscosityX; // (velVX, velVX, velVX, denVX)\n"
104 " float4 viscosityY; // (velVX, velVY, velVY, denVY)\n"
105 " float4 viscosityZ; // (velVZ, velVZ, velVZ, denVZ)\n"
106 " float4 epsilon; // (epsilonX, epsilonY, epsilonZ, epsilon0)\n"
107 "};\n"
108 "\n"
109 "Texture3D<float4> inState;\n"
110 "Texture3D<float> poisson;\n"
111 "RWTexture3D<float4> outState;\n"
112 "\n"
113 "[numthreads(NUM_X_THREADS, NUM_Y_THREADS, NUM_Z_THREADS)]\n"
114 "void CSMain(uint3 c : SV_DispatchThreadID)\n"
115 "{\n"
116 " uint3 dim;\n"
117 " inState.GetDimensions(dim.x, dim.y, dim.z);\n"
118 "\n"
119 " int x = int(c.x);\n"
120 " int y = int(c.y);\n"
121 " int z = int(c.z);\n"
122 " int xm = max(x - 1, 0);\n"
123 " int xp = min(x + 1, dim.x - 1);\n"
124 " int ym = max(y - 1, 0);\n"
125 " int yp = min(y + 1, dim.y - 1);\n"
126 " int zm = max(z - 1, 0);\n"
127 " int zp = min(z + 1, dim.z - 1);\n"
128 "\n"
129 " // Sample the state at (x,y,z).\n"
130 " float4 state = inState[c];\n"
131 "\n"
132 " // Sample Poisson values at immediate neighbors of (x,y,z).\n"
133 " float poisPZZ = poisson[int3(xp, y, z)];\n"
134 " float poisMZZ = poisson[int3(xm, y, z)];\n"
135 " float poisZPZ = poisson[int3(x, yp, z)];\n"
136 " float poisZMZ = poisson[int3(x, ym, z)];\n"
137 " float poisZZP = poisson[int3(x, y, zp)];\n"
138 " float poisZZM = poisson[int3(x, y, zm)];\n"
139 "\n"
140 " float4 diff = float4(poisPZZ - poisMZZ, poisZPZ - poisZMZ, poisZZP - poisZZM, 0.0f);\n"
141 " outState[c] = state + halfDivDelta*diff;\n"
142 "}\n";
143 
145 {
146  &msGLSLSource,
147  &msHLSLSource
148 };
void Execute(std::shared_ptr< GraphicsEngine > const &engine, std::shared_ptr< Texture3 > const &inState, std::shared_ptr< Texture3 > const &poisson, std::shared_ptr< Texture3 > const &outState)
static std::string const msGLSLSource
std::shared_ptr< ComputeProgram > mAdjustVelocity
GLsizei const GLchar *const * string
Definition: glcorearb.h:809
static std::string const msHLSLSource
static std::string const * msSource[ProgramFactory::PF_NUM_API]
Fluid3AdjustVelocity(std::shared_ptr< ProgramFactory > const &factory, int xSize, int ySize, int zSize, int numXThreads, int numYThreads, int numZThreads, std::shared_ptr< ConstantBuffer > const &parameters)


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