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


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