GteFluid2EnforceStateBoundary.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 Fluid2EnforceStateBoundary::Fluid2EnforceStateBoundary(std::shared_ptr<ProgramFactory> const& factory,
15  int xSize, int ySize, int numXThreads, int numYThreads)
16  :
17  mNumXGroups(xSize/numXThreads),
18  mNumYGroups(ySize/numYThreads)
19 {
20  mXMin = std::make_shared<Texture1>(DF_R32_FLOAT, ySize);
21  mXMin->SetUsage(Resource::SHADER_OUTPUT);
22  mXMax = std::make_shared<Texture1>(DF_R32_FLOAT, ySize);
23  mXMax->SetUsage(Resource::SHADER_OUTPUT);
24  mYMin = std::make_shared<Texture1>(DF_R32_FLOAT, xSize);
25  mYMin->SetUsage(Resource::SHADER_OUTPUT);
26  mYMax = std::make_shared<Texture1>(DF_R32_FLOAT, xSize);
27  mYMax->SetUsage(Resource::SHADER_OUTPUT);
28 
29  int i = factory->GetAPI();
30  factory->PushDefines();
31  factory->defines.Set("USE_COPY_X_EDGE", 1);
32  factory->defines.Set("NUM_Y_THREADS", numYThreads);
33  mCopyXEdge = factory->CreateFromSource(*msSource[i]);
34  if (mCopyXEdge)
35  {
36  mCopyXEdge->GetCShader()->Set("xMin", mXMin);
37  mCopyXEdge->GetCShader()->Set("xMax", mXMax);
38  }
39 
40  factory->defines.Clear();
41  factory->defines.Set("USE_WRITE_X_EDGE", 1);
42  factory->defines.Set("NUM_Y_THREADS", numYThreads);
43  mWriteXEdge = factory->CreateFromSource(*msSource[i]);
44  if (mWriteXEdge)
45  {
46  mWriteXEdge->GetCShader()->Set("xMin", mXMin);
47  mWriteXEdge->GetCShader()->Set("xMax", mXMax);
48  }
49 
50  factory->defines.Clear();
51  factory->defines.Set("USE_COPY_Y_EDGE", 1);
52  factory->defines.Set("NUM_X_THREADS", numXThreads);
53  mCopyYEdge = factory->CreateFromSource(*msSource[i]);
54  if (mCopyYEdge)
55  {
56  mCopyYEdge->GetCShader()->Set("yMin", mYMin);
57  mCopyYEdge->GetCShader()->Set("yMax", mYMax);
58  }
59 
60  factory->defines.Clear();
61  factory->defines.Set("USE_WRITE_Y_EDGE", 1);
62  factory->defines.Set("NUM_X_THREADS", numXThreads);
63  mWriteYEdge = factory->CreateFromSource(*msSource[i]);
64  if (mWriteYEdge)
65  {
66  mWriteYEdge->GetCShader()->Set("yMin", mYMin);
67  mWriteYEdge->GetCShader()->Set("yMax", mYMax);
68  }
69 
70  factory->PopDefines();
71 }
72 
73 void Fluid2EnforceStateBoundary::Execute(std::shared_ptr<GraphicsEngine> const& engine,
74  std::shared_ptr<Texture2> const& state)
75 {
76  // in: state
77  // out: mXMin, mXMax
78  mCopyXEdge->GetCShader()->Set("state", state);
79  engine->Execute(mCopyXEdge, 1, mNumYGroups, 1);
80 
81  // in: mXMin, mXMax
82  // out: state
83  mWriteXEdge->GetCShader()->Set("state", state);
84  engine->Execute(mWriteXEdge, 1, mNumYGroups, 1);
85 
86  // in: state
87  // out: mYMin, mYMax
88  mCopyYEdge->GetCShader()->Set("state", state);
89  engine->Execute(mCopyYEdge, mNumXGroups, 1, 1);
90 
91  // in: mYMin, mYMax
92  // out: state
93  mWriteYEdge->GetCShader()->Set("state", state);
94  engine->Execute(mWriteYEdge, mNumXGroups, 1, 1);
95 }
96 
97 
99 "#if USE_COPY_X_EDGE\n"
100 "layout(rgba32f) uniform readonly image2D state;\n"
101 "layout(r32f) uniform writeonly image1D xMin;\n"
102 "layout(r32f) uniform writeonly image1D xMax;\n"
103 "\n"
104 "layout (local_size_x = 1, local_size_y = NUM_Y_THREADS, local_size_z = 1) in;\n"
105 "void main()\n"
106 "{\n"
107 " ivec2 c = ivec2(gl_GlobalInvocationID.xy);\n"
108 " ivec2 dim = imageSize(state);\n"
109 " float xMinValue = imageLoad(state, ivec2(1, c.y)).y;\n"
110 " float xMaxValue = imageLoad(state, ivec2(dim.x - 2, c.y)).y;\n"
111 " imageStore(xMin, c.y, vec4(xMinValue, 0.0f, 0.0f, 0.0f));\n"
112 " imageStore(xMax, c.y, vec4(xMaxValue, 0.0f, 0.0f, 0.0f));\n"
113 "}\n"
114 "#endif\n"
115 "\n"
116 "#if USE_WRITE_X_EDGE\n"
117 "layout(r32f) uniform readonly image1D xMin;\n"
118 "layout(r32f) uniform readonly image1D xMax;\n"
119 "layout(rgba32f) uniform writeonly image2D state;\n"
120 "\n"
121 "layout (local_size_x = 1, local_size_y = NUM_Y_THREADS, local_size_z = 1) in;\n"
122 "void main()\n"
123 "{\n"
124 " ivec2 c = ivec2(gl_GlobalInvocationID.xy);\n"
125 " ivec2 dim = imageSize(state);\n"
126 " float xMinValue = imageLoad(xMin, c.y).x;\n"
127 " float xMaxValue = imageLoad(xMax, c.y).x;\n"
128 " imageStore(state, ivec2(0, c.y), vec4(0.0f, xMinValue, 0.0f, 0.0f));\n"
129 " imageStore(state, ivec2(dim.x - 1, c.y), vec4(0.0f, xMaxValue, 0.0f, 0.0f));\n"
130 "}\n"
131 "#endif\n"
132 "\n"
133 "#if USE_COPY_Y_EDGE\n"
134 "layout(rgba32f) uniform readonly image2D state;\n"
135 "layout(r32f) uniform writeonly image1D yMin;\n"
136 "layout(r32f) uniform writeonly image1D yMax;\n"
137 "\n"
138 "layout (local_size_x = NUM_X_THREADS, local_size_y = 1, local_size_z = 1) in;\n"
139 "void main()\n"
140 "{\n"
141 " ivec2 c = ivec2(gl_GlobalInvocationID.xy);\n"
142 " ivec2 dim = imageSize(state);\n"
143 " float yMinValue = imageLoad(state, ivec2(c.x, 1)).x;\n"
144 " float yMaxValue = imageLoad(state, ivec2(c.x, dim.y - 2)).x;\n"
145 " imageStore(yMin, c.x, vec4(yMinValue, 0.0f, 0.0f, 0.0f));\n"
146 " imageStore(yMax, c.x, vec4(yMaxValue, 0.0f, 0.0f, 0.0f));\n"
147 "}\n"
148 "#endif\n"
149 "\n"
150 "#if USE_WRITE_Y_EDGE\n"
151 "layout(r32f) uniform readonly image1D yMin;\n"
152 "layout(r32f) uniform readonly image1D yMax;\n"
153 "layout(rgba32f) uniform writeonly image2D state;\n"
154 "\n"
155 "layout (local_size_x = NUM_X_THREADS, local_size_y = 1, local_size_z = 1) in;\n"
156 "void main()\n"
157 "{\n"
158 " ivec2 c = ivec2(gl_GlobalInvocationID.xy);\n"
159 " ivec2 dim = imageSize(state);\n"
160 " float yMinValue = imageLoad(yMin, c.x).x;\n"
161 " float yMaxValue = imageLoad(yMax, c.x).x;\n"
162 " imageStore(state, ivec2(c.x, 0), vec4(yMinValue, 0.0f, 0.0f, 0.0f));\n"
163 " imageStore(state, ivec2(c.x, dim.y - 1), vec4(yMaxValue, 0.0f, 0.0f, 0.0f));\n"
164 "}\n"
165 "#endif\n";
166 
168 "#if USE_COPY_X_EDGE\n"
169 "Texture2D<float4> state;\n"
170 "RWTexture1D<float> xMin;\n"
171 "RWTexture1D<float> xMax;\n"
172 "\n"
173 "[numthreads(1, NUM_Y_THREADS, 1)]\n"
174 "void CSMain(uint2 c : SV_DispatchThreadID)\n"
175 "{\n"
176 " uint2 dim;\n"
177 " state.GetDimensions(dim.x, dim.y);\n"
178 " xMin[c.y] = state[uint2(1, c.y)].y;\n"
179 " xMax[c.y] = state[uint2(dim.x - 2, c.y)].y;\n"
180 "}\n"
181 "#endif\n"
182 "\n"
183 "#if USE_WRITE_X_EDGE\n"
184 "Texture1D<float> xMin;\n"
185 "Texture1D<float> xMax;\n"
186 "RWTexture2D<float4> state;\n"
187 "\n"
188 "[numthreads(1, NUM_Y_THREADS, 1)]\n"
189 "void CSMain(uint2 c : SV_DispatchThreadID)\n"
190 "{\n"
191 " uint2 dim;\n"
192 " state.GetDimensions(dim.x, dim.y);\n"
193 " state[uint2(0, c.y)] = float4(0.0f, xMin[c.y], 0.0f, 0.0f);\n"
194 " state[uint2(dim.x - 1, c.y)] = float4(0.0f, xMax[c.y], 0.0f, 0.0f);\n"
195 "}\n"
196 "#endif\n"
197 "\n"
198 "#if USE_COPY_Y_EDGE\n"
199 "Texture2D<float4> state;\n"
200 "RWTexture1D<float> yMin;\n"
201 "RWTexture1D<float> yMax;\n"
202 "\n"
203 "[numthreads(NUM_X_THREADS, 1, 1)]\n"
204 "void CSMain(uint2 c : SV_DispatchThreadID)\n"
205 "{\n"
206 " uint2 dim;\n"
207 " state.GetDimensions(dim.x, dim.y);\n"
208 " yMin[c.x] = state[uint2(c.x, 1)].x;\n"
209 " yMax[c.x] = state[uint2(c.x, dim.y - 2)].x;\n"
210 "}\n"
211 "#endif\n"
212 "\n"
213 "#if USE_WRITE_Y_EDGE\n"
214 "Texture1D<float> yMin;\n"
215 "Texture1D<float> yMax;\n"
216 "RWTexture2D<float4> state;\n"
217 "\n"
218 "[numthreads(NUM_X_THREADS, 1, 1)]\n"
219 "void CSMain(uint2 c : SV_DispatchThreadID)\n"
220 "{\n"
221 " uint2 dim;\n"
222 " state.GetDimensions(dim.x, dim.y);\n"
223 " state[uint2(c.x, 0)] = float4(yMin[c.x], 0.0f, 0.0f, 0.0f);\n"
224 " state[uint2(c.x, dim.y - 1)] = float4(yMax[c.x], 0.0f, 0.0f, 0.0f);\n"
225 "}\n"
226 "#endif\n";
227 
229 {
230  &msGLSLSource,
231  &msHLSLSource
232 };
DF_R32_FLOAT
Definition: GteDataFormat.h:20
std::shared_ptr< ComputeProgram > mCopyXEdge
std::shared_ptr< ComputeProgram > mWriteXEdge
std::shared_ptr< ComputeProgram > mWriteYEdge
GLsizei const GLchar *const * string
Definition: glcorearb.h:809
Fluid2EnforceStateBoundary(std::shared_ptr< ProgramFactory > const &factory, int xSize, int ySize, int numXThreads, int numYThreads)
std::shared_ptr< ComputeProgram > mCopyYEdge
static std::string const * msSource[ProgramFactory::PF_NUM_API]
void Execute(std::shared_ptr< GraphicsEngine > const &engine, std::shared_ptr< Texture2 > const &state)


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