GteFluid2ComputeDivergence.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 Fluid2ComputeDivergence::Fluid2ComputeDivergence(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  mDivergence = std::make_shared<Texture2>(DF_R32_FLOAT, xSize, ySize);
22  mDivergence->SetUsage(Resource::SHADER_OUTPUT);
23 
24  int i = factory->GetAPI();
25  factory->PushDefines();
26  factory->defines.Set("NUM_X_THREADS", numXThreads);
27  factory->defines.Set("NUM_Y_THREADS", numYThreads);
28  mComputeDivergence = factory->CreateFromSource(*msSource[i]);
30  {
31  mComputeDivergence->GetCShader()->Set("Parameters", parameters);
32  mComputeDivergence->GetCShader()->Set("divergence", mDivergence);
33  }
34 
35  factory->PopDefines();
36 }
37 
38 void Fluid2ComputeDivergence::Execute(std::shared_ptr<GraphicsEngine> const& engine,
39  std::shared_ptr<Texture2> const& state)
40 {
41  std::shared_ptr<ComputeShader> cshader = mComputeDivergence->GetCShader();
42  cshader->Set("state", state);
43  engine->Execute(mComputeDivergence, mNumXGroups, mNumYGroups, 1);
44 }
45 
46 
48 "uniform Parameters\n"
49 "{\n"
50 " vec4 spaceDelta; // (dx, dy, 0, 0)\n"
51 " vec4 halfDivDelta; // (0.5/dx, 0.5/dy, 0, 0)\n"
52 " vec4 timeDelta; // (dt/dx, dt/dy, 0, dt)\n"
53 " vec4 viscosityX; // (velVX, velVX, 0, denVX)\n"
54 " vec4 viscosityY; // (velVX, velVY, 0, denVY)\n"
55 " vec4 epsilon; // (epsilonX, epsilonY, 0, epsilon0)\n"
56 "};\n"
57 "\n"
58 "layout(rgba32f) uniform readonly image2D state;\n"
59 "layout(r32f) uniform writeonly image2D divergence;\n"
60 "\n"
61 "layout (local_size_x = NUM_X_THREADS, local_size_y = NUM_Y_THREADS, local_size_z = 1) in;\n"
62 "void main()\n"
63 "{\n"
64 " ivec2 c = ivec2(gl_GlobalInvocationID.xy);\n"
65 " ivec2 dim = imageSize(state);\n"
66 "\n"
67 " int x = int(c.x);\n"
68 " int y = int(c.y);\n"
69 " int xm = max(x - 1, 0);\n"
70 " int xp = min(x + 1, dim.x - 1);\n"
71 " int ym = max(y - 1, 0);\n"
72 " int yp = min(y + 1, dim.y - 1);\n"
73 "\n"
74 " vec2 velocityGradient = vec2\n"
75 " (\n"
76 " imageLoad(state, ivec2(xp, y)).x - imageLoad(state, ivec2(xm, y)).x,\n"
77 " imageLoad(state, ivec2(x, yp)).y - imageLoad(state, ivec2(x, ym)).y\n"
78 " );\n"
79 "\n"
80 " float divergenceValue = dot(halfDivDelta.xy, velocityGradient);\n"
81 " imageStore(divergence, c, vec4(divergenceValue, 0.0f, 0.0f, 0.0f));\n"
82 "}\n";
83 
85 "cbuffer Parameters\n"
86 "{\n"
87 " float4 spaceDelta; // (dx, dy, 0, 0)\n"
88 " float4 halfDivDelta; // (0.5/dx, 0.5/dy, 0, 0)\n"
89 " float4 timeDelta; // (dt/dx, dt/dy, 0, dt)\n"
90 " float4 viscosityX; // (velVX, velVX, 0, denVX)\n"
91 " float4 viscosityY; // (velVX, velVY, 0, denVY)\n"
92 " float4 epsilon; // (epsilonX, epsilonY, 0, epsilon0)\n"
93 "};\n"
94 "\n"
95 "Texture2D<float4> state;\n"
96 "RWTexture2D<float> divergence;\n"
97 "\n"
98 "[numthreads(NUM_X_THREADS, NUM_Y_THREADS, 1)]\n"
99 "void CSMain(uint2 c : SV_DispatchThreadID)\n"
100 "{\n"
101 " uint2 dim;\n"
102 " state.GetDimensions(dim.x, dim.y);\n"
103 "\n"
104 " int x = int(c.x);\n"
105 " int y = int(c.y);\n"
106 " int xm = max(x - 1, 0);\n"
107 " int xp = min(x + 1, dim.x - 1);\n"
108 " int ym = max(y - 1, 0);\n"
109 " int yp = min(y + 1, dim.y - 1);\n"
110 "\n"
111 " float2 velocityGradient = float2(\n"
112 " state[int2(xp, y)].x - state[int2(xm, y)].x,\n"
113 " state[int2(x, yp)].y - state[int2(x, ym)].y\n"
114 " );\n"
115 "\n"
116 " divergence[c] = dot(halfDivDelta.xy, velocityGradient);\n"
117 "}\n";
118 
120 {
121  &msGLSLSource,
122  &msHLSLSource
123 };
DF_R32_FLOAT
Definition: GteDataFormat.h:20
static std::string const msHLSLSource
static std::string const * msSource[ProgramFactory::PF_NUM_API]
std::shared_ptr< Texture2 > mDivergence
Fluid2ComputeDivergence(std::shared_ptr< ProgramFactory > const &factory, int xSize, int ySize, int numXThreads, int numYThreads, std::shared_ptr< ConstantBuffer > const &parameters)
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 &state)
std::shared_ptr< ComputeProgram > mComputeDivergence


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