GteFluid2InitializeState.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 #include <random>
13 using namespace gte;
14 
15 Fluid2InitializeState::Fluid2InitializeState(std::shared_ptr<ProgramFactory> const& factory,
16  int xSize, int ySize, int numXThreads, int numYThreads)
17  :
18  mNumXGroups(xSize/numXThreads),
19  mNumYGroups(ySize/numYThreads)
20 {
21  // Use a Mersenne twister engine for random numbers.
22  std::mt19937 mte;
23  std::uniform_real_distribution<float> unirnd(0.0f, 1.0f);
24 
25  // Initial density values are randomly generated.
26  mDensity = std::make_shared<Texture2>(DF_R32_FLOAT, xSize, ySize);
27  float* data = mDensity->Get<float>();
28  for (unsigned int i = 0; i < mDensity->GetNumElements(); ++i, ++data)
29  {
30  *data = unirnd(mte);
31  }
32 
33  // Initial velocity values are zero.
34  mVelocity = std::make_shared<Texture2>(DF_R32G32_FLOAT, xSize, ySize);
35  memset(mVelocity->GetData(), 0, mVelocity->GetNumBytes());
36 
37  // The states at time 0 and time -dt are initialized by a compute shader.
38  mStateTm1 = std::make_shared<Texture2>(DF_R32G32B32A32_FLOAT, xSize, ySize);
39  mStateTm1->SetUsage(Resource::SHADER_OUTPUT);
40 
41  mStateT = std::make_shared<Texture2>(DF_R32G32B32A32_FLOAT, xSize, ySize);
42  mStateT->SetUsage(Resource::SHADER_OUTPUT);
43 
44  // Create the shader for initializing velocity and density.
45  int i = factory->GetAPI();
46  factory->PushDefines();
47  factory->defines.Set("NUM_X_THREADS", numXThreads);
48  factory->defines.Set("NUM_Y_THREADS", numYThreads);
49  mInitializeState = factory->CreateFromSource(*msSource[i]);
50  if (mInitializeState)
51  {
52  std::shared_ptr<ComputeShader> cshader = mInitializeState->GetCShader();
53  cshader->Set("density", mDensity);
54  cshader->Set("velocity", mVelocity);
55  cshader->Set("stateTm1", mStateTm1);
56  cshader->Set("stateT", mStateT);
57  }
58  factory->PopDefines();
59 }
60 
61 void Fluid2InitializeState::Execute(std::shared_ptr<GraphicsEngine> const& engine)
62 {
63  engine->Execute(mInitializeState, mNumXGroups, mNumYGroups, 1);
64 }
65 
66 
68 "layout(r32f) uniform readonly image2D density;\n"
69 "layout(rg32f) uniform readonly image2D velocity;\n"
70 "layout(rgba32f) uniform writeonly image2D stateTm1;\n"
71 "layout(rgba32f) uniform writeonly image2D stateT;\n"
72 "\n"
73 "layout (local_size_x = NUM_X_THREADS, local_size_y = NUM_Y_THREADS, local_size_z = 1) in;\n"
74 "void main()\n"
75 "{\n"
76 " ivec2 c = ivec2(gl_GlobalInvocationID.xy);\n"
77 " vec4 initial = vec4(imageLoad(velocity, c).xy, 0.0f, imageLoad(density, c).x);\n"
78 " imageStore(stateTm1, c, initial);\n"
79 " imageStore(stateT, c, initial);\n"
80 "}\n";
81 
83 "Texture2D<float> density;\n"
84 "Texture2D<float2> velocity;\n"
85 "RWTexture2D<float4> stateTm1;\n"
86 "RWTexture2D<float4> stateT;\n"
87 "\n"
88 "[numthreads(NUM_X_THREADS, NUM_Y_THREADS, 1)]\n"
89 "void CSMain(uint2 c : SV_DispatchThreadID)\n"
90 "{\n"
91 " float4 initial = float4(velocity[c], 0.0f, density[c]);\n"
92 " stateTm1[c] = initial;\n"
93 " stateT[c] = initial;\n"
94 "}\n";
95 
97 {
98  &msGLSLSource,
100 };
DF_R32_FLOAT
Definition: GteDataFormat.h:20
static std::string const msHLSLSource
std::shared_ptr< Texture2 > mStateTm1
static std::string const msGLSLSource
std::shared_ptr< Texture2 > mVelocity
std::shared_ptr< Texture2 > mDensity
static std::string const * msSource[ProgramFactory::PF_NUM_API]
std::shared_ptr< ComputeProgram > mInitializeState
GLsizei const GLchar *const * string
Definition: glcorearb.h:809
GLboolean * data
Definition: glcorearb.h:126
DF_R32G32B32A32_FLOAT
Definition: GteDataFormat.h:20
std::shared_ptr< Texture2 > mStateT
Fluid2InitializeState(std::shared_ptr< ProgramFactory > const &factory, int xSize, int ySize, int numXThreads, int numYThreads)
DF_R32G32_FLOAT
Definition: GteDataFormat.h:20
GLfloat f
Definition: glcorearb.h:1921
void Execute(std::shared_ptr< GraphicsEngine > const &engine)


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