GteFluid2AdjustVelocity.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 Fluid2AdjustVelocity::Fluid2AdjustVelocity(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  int i = factory->GetAPI();
22  factory->PushDefines();
23  factory->defines.Set("NUM_X_THREADS", numXThreads);
24  factory->defines.Set("NUM_Y_THREADS", numYThreads);
25 
26  mAdjustVelocity = factory->CreateFromSource(*msSource[i]);
27  if (mAdjustVelocity)
28  {
29  mAdjustVelocity->GetCShader()->Set("Parameters", parameters);
30  }
31 
32  factory->PopDefines();
33 }
34 
35 void Fluid2AdjustVelocity::Execute(std::shared_ptr<GraphicsEngine> const& engine,
36  std::shared_ptr<Texture2> const& inState,
37  std::shared_ptr<Texture2> const& poisson,
38  std::shared_ptr<Texture2> const& outState)
39 
40 {
41  std::shared_ptr<ComputeShader> cshader = mAdjustVelocity->GetCShader();
42  cshader->Set("inState", inState);
43  cshader->Set("poisson", poisson);
44  cshader->Set("outState", outState);
45  engine->Execute(mAdjustVelocity, mNumXGroups, mNumYGroups, 1);
46 }
47 
48 
50 "uniform Parameters\n"
51 "{\n"
52 " vec4 spaceDelta; // (dx, dy, 0, 0)\n"
53 " vec4 halfDivDelta; // (0.5/dx, 0.5/dy, 0, 0)\n"
54 " vec4 timeDelta; // (dt/dx, dt/dy, 0, dt)\n"
55 " vec4 viscosityX; // (velVX, velVX, 0, denVX)\n"
56 " vec4 viscosityY; // (velVX, velVY, 0, denVY)\n"
57 " vec4 epsilon; // (epsilonX, epsilonY, 0, epsilon0)\n"
58 "};\n"
59 "\n"
60 "layout(rgba32f) uniform readonly image2D inState;\n"
61 "layout(r32f) uniform readonly image2D poisson;\n"
62 "layout(rgba32f) uniform writeonly image2D outState;\n"
63 "\n"
64 "layout (local_size_x = NUM_X_THREADS, local_size_y = NUM_Y_THREADS, local_size_z = 1) in;\n"
65 "void main()\n"
66 "{\n"
67 " ivec2 c = ivec2(gl_GlobalInvocationID.xy);\n"
68 " ivec2 dim = imageSize(inState);\n"
69 "\n"
70 " int x = int(c.x);\n"
71 " int y = int(c.y);\n"
72 " int xm = max(x - 1, 0);\n"
73 " int xp = min(x + 1, dim.x - 1);\n"
74 " int ym = max(y - 1, 0);\n"
75 " int yp = min(y + 1, dim.y - 1);\n"
76 "\n"
77 " // Sample the state at (x,y).\n"
78 " vec4 state = imageLoad(inState, c);\n"
79 "\n"
80 " // Sample Poisson values at immediate neighbors of (x,y).\n"
81 " float poisPZ = imageLoad(poisson, ivec2(xp, y)).x;\n"
82 " float poisMZ = imageLoad(poisson, ivec2(xm, y)).x;\n"
83 " float poisZP = imageLoad(poisson, ivec2(x, yp)).x;\n"
84 " float poisZM = imageLoad(poisson, ivec2(x, ym)).x;\n"
85 "\n"
86 " vec4 diff = vec4(poisPZ - poisMZ, poisZP - poisZM, 0.0f, 0.0f);\n"
87 " imageStore(outState, c, state + halfDivDelta*diff);\n"
88 "}\n";
89 
91 "cbuffer Parameters\n"
92 "{\n"
93 " float4 spaceDelta; // (dx, dy, 0, 0)\n"
94 " float4 halfDivDelta; // (0.5/dx, 0.5/dy, 0, 0)\n"
95 " float4 timeDelta; // (dt/dx, dt/dy, 0, dt)\n"
96 " float4 viscosityX; // (velVX, velVX, 0, denVX)\n"
97 " float4 viscosityY; // (velVX, velVY, 0, denVY)\n"
98 " float4 epsilon; // (epsilonX, epsilonY, 0, epsilon0)\n"
99 "};\n"
100 "\n"
101 "Texture2D<float4> inState;\n"
102 "Texture2D<float> poisson;\n"
103 "RWTexture2D<float4> outState;\n"
104 "\n"
105 "[numthreads(NUM_X_THREADS, NUM_Y_THREADS, 1)]\n"
106 "void CSMain(uint2 c : SV_DispatchThreadID)\n"
107 "{\n"
108 " uint2 dim;\n"
109 " inState.GetDimensions(dim.x, dim.y);\n"
110 "\n"
111 " int x = int(c.x);\n"
112 " int y = int(c.y);\n"
113 " int xm = max(x - 1, 0);\n"
114 " int xp = min(x + 1, dim.x - 1);\n"
115 " int ym = max(y - 1, 0);\n"
116 " int yp = min(y + 1, dim.y - 1);\n"
117 "\n"
118 " // Sample the state at (x,y).\n"
119 " float4 state = inState[c];\n"
120 "\n"
121 " // Sample Poisson values at immediate neighbors of (x,y).\n"
122 " float poisPZ = poisson[int2(xp, y)];\n"
123 " float poisMZ = poisson[int2(xm, y)];\n"
124 " float poisZP = poisson[int2(x, yp)];\n"
125 " float poisZM = poisson[int2(x, ym)];\n"
126 "\n"
127 " float4 diff = float4(poisPZ - poisMZ, poisZP - poisZM, 0.0f, 0.0f);\n"
128 " outState[c] = state + halfDivDelta*diff;\n"
129 "}\n";
130 
132 {
133  &msGLSLSource,
134  &msHLSLSource
135 };
Fluid2AdjustVelocity(std::shared_ptr< ProgramFactory > const &factory, int xSize, int ySize, int numXThreads, int numYThreads, std::shared_ptr< ConstantBuffer > const &parameters)
std::shared_ptr< ComputeProgram > mAdjustVelocity
GLsizei const GLchar *const * string
Definition: glcorearb.h:809
static std::string const msGLSLSource
void Execute(std::shared_ptr< GraphicsEngine > const &engine, std::shared_ptr< Texture2 > const &inState, std::shared_ptr< Texture2 > const &poisson, std::shared_ptr< Texture2 > const &outState)
static std::string const * msSource[ProgramFactory::PF_NUM_API]
static std::string const msHLSLSource


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