GpuMassSpringVolume::GpuMassSpringVolume(ProgramFactory& factory, int numColumns, int numRows, int numSlices, float step, float viscosity, Environment& environment, bool& created) : mNumColumns(numColumns), mNumRows(numRows), mNumSlices(numSlices) { created = false; // Create the shaders. std::string path = environment.GetPath("RungeKutta.hlsl"); int const numThreads = 4; factory.PushDefines(); factory.defines.Set("NUM_X_THREADS", numThreads); factory.defines.Set("NUM_Y_THREADS", numThreads); factory.defines.Set("NUM_Z_THREADS", numThreads); for (int i = 0; i < 8; ++i) { factory.csEntry = "RK4Step"; factory.csEntry += std::to_string(1 + i/2); factory.csEntry += ((i & 1) == 0 ? "a" : "b"); mRK4Shader[i] = factory.CreateFromFile(path); if (!mRK4Shader[i]) { return; } } // The cbuffer is tightly packed. Only time, halfTime, and fullTime vary. mParameters = std::make_shared<ConstantBuffer>(sizeof(SimulationParameters), true); SimulationParameters& p = *mParameters->Get<SimulationParameters>(); p.dimensions[0] = numColumns; p.dimensions[1] = numRows; p.dimensions[2] = numSlices; p.dimensions[3] = numColumns * numRows; p.viscosity = viscosity; p.time = 0.0f; p.delta = step; p.halfDelta = p.delta / 2.0f; p.sixthDelta = p.delta / 6.0f; p.halfTime = p.time + p.halfDelta; p.fullTime = p.time + p.delta; unsigned int const numParticles = p.dimensions[2] * p.dimensions[3]; size_t const vecsize = sizeof(Vector3<float>); mMass = std::make_shared<StructuredBuffer>(numParticles, sizeof(float)); mInvMass = std::make_shared<StructuredBuffer>(numParticles, sizeof(float)); mPosition = std::make_shared<StructuredBuffer>(numParticles, vecsize); mPosition->SetUsage(Resource::SHADER_OUTPUT); mPosition->SetCopyType(Resource::COPY_STAGING_TO_CPU); mVelocity = std::make_shared<StructuredBuffer>(numParticles, vecsize); mVelocity->SetUsage(Resource::SHADER_OUTPUT); mConstantC = std::make_shared<StructuredBuffer>(numParticles, sizeof(float)); mLengthC = std::make_shared<StructuredBuffer>(numParticles, sizeof(float)); mConstantR = std::make_shared<StructuredBuffer>(numParticles, sizeof(float)); mLengthR = std::make_shared<StructuredBuffer>(numParticles, sizeof(float)); mConstantS = std::make_shared<StructuredBuffer>(numParticles, sizeof(float)); mLengthS = std::make_shared<StructuredBuffer>(numParticles, sizeof(float)); mPTmp = std::make_shared<StructuredBuffer>(numParticles, vecsize, true); mPTmp->SetUsage(Resource::SHADER_OUTPUT); mPTmp->SetCopyType(Resource::COPY_STAGING_TO_CPU); mPAllTmp = std::make_shared<StructuredBuffer>(numParticles, 4 * vecsize, true); mPAllTmp->SetUsage(Resource::SHADER_OUTPUT); mPAllTmp->SetCopyType(Resource::COPY_STAGING_TO_CPU); mVTmp = std::make_shared<StructuredBuffer>(numParticles, vecsize, true); mVTmp->SetUsage(Resource::SHADER_OUTPUT); mVTmp->SetCopyType(Resource::COPY_STAGING_TO_CPU); mVAllTmp = std::make_shared<StructuredBuffer>(numParticles, 4 * vecsize, true); mVAllTmp->SetUsage(Resource::SHADER_OUTPUT); mVAllTmp->SetCopyType(Resource::COPY_STAGING_TO_CPU); mNumXGroups = p.dimensions[0] / numThreads; mNumYGroups = p.dimensions[1] / numThreads; mNumZGroups = p.dimensions[2] / numThreads; std::shared_ptr<ComputeShader> cshader = mRK4Shader[0]->GetCShader(); cshader->Set("SimulationParameters", mParameters); cshader->Set("invMass", mInvMass); cshader->Set("constantC", mConstantC); cshader->Set("lengthC", mLengthC); cshader->Set("constantR", mConstantR); cshader->Set("lengthR", mLengthR); cshader->Set("constantS", mConstantS); cshader->Set("lengthS", mLengthS); cshader->Set("pAllTmp", mPAllTmp); cshader->Set("vAllTmp", mVAllTmp); cshader->Set("position", mPosition); cshader->Set("velocity", mVelocity); cshader = mRK4Shader[1]->GetCShader(); cshader->Set("SimulationParameters", mParameters); cshader->Set("invMass", mInvMass); cshader->Set("pTmp", mPTmp); cshader->Set("vTmp", mVTmp); cshader->Set("pAllTmp", mPAllTmp); cshader->Set("vAllTmp", mVAllTmp); cshader->Set("position", mPosition); cshader->Set("velocity", mVelocity); cshader = mRK4Shader[2]->GetCShader(); cshader->Set("SimulationParameters", mParameters); cshader->Set("invMass", mInvMass); cshader->Set("constantC", mConstantC); cshader->Set("lengthC", mLengthC); cshader->Set("constantR", mConstantR); cshader->Set("lengthR", mLengthR); cshader->Set("constantS", mConstantS); cshader->Set("lengthS", mLengthS); cshader->Set("pTmp", mPTmp); cshader->Set("vTmp", mVTmp); cshader->Set("pAllTmp", mPAllTmp); cshader->Set("vAllTmp", mVAllTmp); cshader->Set("velocity", mVelocity); cshader = mRK4Shader[3]->GetCShader(); cshader->Set("SimulationParameters", mParameters); cshader->Set("invMass", mInvMass); cshader->Set("pTmp", mPTmp); cshader->Set("vTmp", mVTmp); cshader->Set("pAllTmp", mPAllTmp); cshader->Set("vAllTmp", mVAllTmp); cshader->Set("position", mPosition); cshader->Set("velocity", mVelocity); cshader = mRK4Shader[4]->GetCShader(); cshader->Set("SimulationParameters", mParameters); cshader->Set("invMass", mInvMass); cshader->Set("constantC", mConstantC); cshader->Set("lengthC", mLengthC); cshader->Set("constantR", mConstantR); cshader->Set("lengthR", mLengthR); cshader->Set("constantS", mConstantS); cshader->Set("lengthS", mLengthS); cshader->Set("pTmp", mPTmp); cshader->Set("vTmp", mVTmp); cshader->Set("pAllTmp", mPAllTmp); cshader->Set("vAllTmp", mVAllTmp); cshader->Set("velocity", mVelocity); cshader = mRK4Shader[5]->GetCShader(); cshader->Set("SimulationParameters", mParameters); cshader->Set("invMass", mInvMass); cshader->Set("pTmp", mPTmp); cshader->Set("vTmp", mVTmp); cshader->Set("pAllTmp", mPAllTmp); cshader->Set("vAllTmp", mVAllTmp); cshader->Set("position", mPosition); cshader->Set("velocity", mVelocity); cshader = mRK4Shader[6]->GetCShader(); cshader->Set("SimulationParameters", mParameters); cshader->Set("invMass", mInvMass); cshader->Set("constantC", mConstantC); cshader->Set("lengthC", mLengthC); cshader->Set("constantR", mConstantR); cshader->Set("lengthR", mLengthR); cshader->Set("constantS", mConstantS); cshader->Set("lengthS", mLengthS); cshader->Set("pTmp", mPTmp); cshader->Set("vTmp", mVTmp); cshader->Set("pAllTmp", mPAllTmp); cshader->Set("vAllTmp", mVAllTmp); cshader->Set("velocity", mVelocity); cshader = mRK4Shader[7]->GetCShader(); cshader->Set("SimulationParameters", mParameters); cshader->Set("invMass", mInvMass); cshader->Set("position", mPosition); cshader->Set("velocity", mVelocity); cshader->Set("pAllTmp", mPAllTmp); cshader->Set("vAllTmp", mVAllTmp); factory.PopDefines(); created = true; }
GpuShortestPath::GpuShortestPath(ProgramFactory& factory, std::shared_ptr<Texture2> const& weights, Environment const& env, bool& created) : mSize(static_cast<int>(weights->GetWidth())) { created = false; mLogSize = Log2OfPowerOfTwo(mSize); mDistance = std::make_shared<Texture2>(DF_R32_FLOAT, mSize, mSize); mDistance->SetUsage(Resource::SHADER_OUTPUT); memset(mDistance->GetData(), 0, mDistance->GetNumBytes()); mPrevious = std::make_shared<Texture2>(DF_R32G32_SINT, mSize, mSize); mPrevious->SetUsage(Resource::SHADER_OUTPUT); mPrevious->SetCopyType(Resource::COPY_STAGING_TO_CPU); mSegment = std::make_shared<ConstantBuffer>(3 * sizeof(int), true); factory.PushDefines(); factory.defines.Set("ISIZE", mSize); mInitializeDiagToRow = factory.CreateFromFile( env.GetPath("InitializeDiagToRow.hlsl")); if (!mInitializeDiagToRow) { return; } std::shared_ptr<ComputeShader> cshader = mInitializeDiagToRow->GetCShader(); cshader->Set("weights", weights); cshader->Set("previous", mPrevious); cshader->Set("sum", mDistance); mInitializeDiagToCol = factory.CreateFromFile( env.GetPath("InitializeDiagToCol.hlsl")); if (!mInitializeDiagToCol) { return; } cshader = mInitializeDiagToCol->GetCShader(); cshader->Set("weights", weights); cshader->Set("previous", mPrevious); cshader->Set("sum", mDistance); mPartialSumDiagToRow.resize(mLogSize); mPartialSumDiagToCol.resize(mLogSize); for (int i = 0; i < mLogSize; ++i) { factory.defines.Set("LOGN", mLogSize); factory.defines.Set("P", i + 1); mPartialSumDiagToRow[i] = factory.CreateFromFile( env.GetPath("PartialSumsDiagToRow.hlsl")); if (!mPartialSumDiagToRow[i]) { return; } mPartialSumDiagToRow[i]->GetCShader()->Set("sum", mDistance); mPartialSumDiagToCol[i] = factory.CreateFromFile( env.GetPath("PartialSumsDiagToCol.hlsl")); if (!mPartialSumDiagToCol[i]) { return; } mPartialSumDiagToCol[i]->GetCShader()->Set("sum", mDistance); } mUpdate = factory.CreateFromFile(env.GetPath("UpdateShader.hlsl")); if (!mUpdate) { return; } cshader = mUpdate->GetCShader(); cshader->Set("Segment", mSegment); cshader->Set("weights", weights); cshader->Set("distance", mDistance); cshader->Set("previous", mPrevious); factory.PopDefines(); created = true; }