GteFluid3InitializeState.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 Fluid3InitializeState::Fluid3InitializeState(std::shared_ptr<ProgramFactory> const& factory,
16  int xSize, int ySize, int zSize, int numXThreads, int numYThreads, int numZThreads)
17  :
18  mNumXGroups(xSize/numXThreads),
19  mNumYGroups(ySize/numYThreads),
20  mNumZGroups(zSize/numZThreads)
21 {
22  // Use a Mersenne twister engine for random numbers.
23  std::mt19937 mte;
24  std::uniform_real_distribution<float> unirnd(0.0f, 1.0f);
25 
26  // Initial density values are randomly generated.
27  mDensity = std::make_shared<Texture3>(DF_R32_FLOAT, xSize, ySize, zSize);
28  float* data = mDensity->Get<float>();
29  for (unsigned int i = 0; i < mDensity->GetNumElements(); ++i, ++data)
30  {
31  *data = unirnd(mte);
32  }
33 
34  // Initial velocity values are zero.
35  mVelocity = std::make_shared<Texture3>(DF_R32G32B32A32_FLOAT, xSize, ySize, zSize);
36  mVelocity->SetUsage(Resource::SHADER_OUTPUT);
37  memset(mVelocity->GetData(), 0, mVelocity->GetNumBytes());
38 
39  mStateTm1 = std::make_shared<Texture3>(DF_R32G32B32A32_FLOAT, xSize, ySize, zSize);
40  mStateTm1->SetUsage(Resource::SHADER_OUTPUT);
41 
42  mStateT = std::make_shared<Texture3>(DF_R32G32B32A32_FLOAT, xSize, ySize, zSize);
43  mStateT->SetUsage(Resource::SHADER_OUTPUT);
44 
45  // Create the shader for generating velocity from vortices.
46  int i = factory->GetAPI();
47  factory->PushDefines();
48  factory->defines.Set("NUM_X_THREADS", numXThreads);
49  factory->defines.Set("NUM_Y_THREADS", numYThreads);
50  factory->defines.Set("NUM_Z_THREADS", numZThreads);
51  mInitializeState = factory->CreateFromSource(*msSource[i]);
52  if (mInitializeState)
53  {
54  std::shared_ptr<ComputeShader> cshader = mInitializeState->GetCShader();
55  cshader->Set("density", mDensity);
56  cshader->Set("velocity", mVelocity);
57  cshader->Set("stateTm1", mStateTm1);
58  cshader->Set("stateT", mStateT);
59  }
60  factory->PopDefines();
61 }
62 
63 void Fluid3InitializeState::Execute(std::shared_ptr<GraphicsEngine> const& engine)
64 {
66 }
67 
68 
70 "layout(r32f) uniform readonly image3D density;\n"
71 "layout(rgba32f) uniform readonly image3D velocity;\n"
72 "layout(rgba32f) uniform writeonly image3D stateTm1;\n"
73 "layout(rgba32f) uniform writeonly image3D stateT;\n"
74 "\n"
75 "layout (local_size_x = NUM_X_THREADS, local_size_y = NUM_Y_THREADS, local_size_z = NUM_Z_THREADS) in;\n"
76 "void main()\n"
77 "{\n"
78 " ivec3 c = ivec3(gl_GlobalInvocationID.xyz);\n"
79 " vec4 initial = vec4(imageLoad(velocity, c).xyz, imageLoad(density, c).x);\n"
80 " imageStore(stateTm1, c, initial);\n"
81 " imageStore(stateT, c, initial);\n"
82 "}\n";
83 
85 "Texture3D<float> density;\n"
86 "Texture3D<float4> velocity;\n"
87 "RWTexture3D<float4> stateTm1;\n"
88 "RWTexture3D<float4> stateT;\n"
89 "\n"
90 "[numthreads(NUM_X_THREADS, NUM_Y_THREADS, NUM_Z_THREADS)]\n"
91 "void CSMain(uint3 c : SV_DispatchThreadID)\n"
92 "{\n"
93 " float4 initial = float4(velocity[c].xyz, density[c]);\n"
94 " stateTm1[c] = initial;\n"
95 " stateT[c] = initial;\n"
96 "}\n";
97 
99 {
100  &msGLSLSource,
101  &msHLSLSource
102 };
static std::string const * msSource[ProgramFactory::PF_NUM_API]
DF_R32_FLOAT
Definition: GteDataFormat.h:20
std::shared_ptr< ComputeProgram > mInitializeState
std::shared_ptr< Texture3 > mDensity
GLsizei const GLchar *const * string
Definition: glcorearb.h:809
std::shared_ptr< Texture3 > mVelocity
GLboolean * data
Definition: glcorearb.h:126
static std::string const msGLSLSource
DF_R32G32B32A32_FLOAT
Definition: GteDataFormat.h:20
Fluid3InitializeState(std::shared_ptr< ProgramFactory > const &factory, int xSize, int ySize, int zSize, int numXThreads, int numYThreads, int numZThreads)
GLfloat f
Definition: glcorearb.h:1921
static std::string const msHLSLSource
std::shared_ptr< Texture3 > mStateTm1
std::shared_ptr< Texture3 > mStateT
void Execute(std::shared_ptr< GraphicsEngine > const &engine)


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