SimpleBumpMapEffect::SimpleBumpMapEffect(ProgramFactory& factory,
    Environment const& environment, bool& created)
    :
    mPVWMatrix(nullptr)
{
    created = false;

    // Load and compile the shaders.
    std::string path = environment.GetPath("SimpleBumpMap.hlsl");
#if !defined(GTE_DEV_OPENGL)
    // The flags are chosen to allow you to debug the shaders through MSVS.
    // The menu path is "Debug | Graphics | Start Diagnostics" (ALT+F5).
    factory.PushFlags();
    factory.flags =
        D3DCOMPILE_ENABLE_STRICTNESS |
        D3DCOMPILE_IEEE_STRICTNESS |
        D3DCOMPILE_DEBUG |
        D3DCOMPILE_SKIP_OPTIMIZATION;
#endif
    mProgram = factory.CreateFromFiles(path, path, "");
#if !defined(GTE_DEV_OPENGL)
    factory.PopFlags();
#endif
    if (!mProgram)
    {
        // The program factory will generate Log* messages.
        return;
    }

    // Load the textures.
    path = environment.GetPath("Bricks.png");
    mBaseTexture.reset(WICFileIO::Load(path, true));
    mBaseTexture->AutogenerateMipmaps();

    path = environment.GetPath("BricksNormal.png");
    mNormalTexture.reset(WICFileIO::Load(path, true));
    mNormalTexture->AutogenerateMipmaps();

    // Create the shader constants.
    mPVWMatrixConstant =
        std::make_shared<ConstantBuffer>(sizeof(Matrix4x4<float>), true);
    mPVWMatrix = mPVWMatrixConstant->Get<Matrix4x4<float>>();
    *mPVWMatrix = Matrix4x4<float>::Identity();

    // Create the texture sampler for mipmapping.
    mCommonSampler = std::make_shared<SamplerState>();
    mCommonSampler->filter = SamplerState::MIN_L_MAG_L_MIP_L;
    mCommonSampler->mode[0] = SamplerState::WRAP;
    mCommonSampler->mode[1] = SamplerState::WRAP;

    // Set the resources for the shaders.
    std::shared_ptr<VertexShader> vshader = mProgram->GetVShader();
    std::shared_ptr<PixelShader> pshader = mProgram->GetPShader();
    vshader->Set("PVWMatrix", mPVWMatrixConstant);
    pshader->Set("baseTexture", mBaseTexture);
    pshader->Set("normalTexture", mNormalTexture);
    pshader->Set("commonSampler", mCommonSampler);

    created = true;
}
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;
}
BlendedTerrainEffect::BlendedTerrainEffect(ProgramFactory& factory,
    Environment const& environment, bool& created)
    :
    mPVWMatrix(nullptr),
    mFlowDirection(nullptr),
    mPowerFactor(nullptr)
{
    created = false;

    // Load and compile the shaders.
    std::string path = environment.GetPath("BlendedTerrain.hlsl");
#if !defined(GTE_DEV_OPENGL)
    // The flags are chosen to allow you to debug the shaders through MSVS.
    // The menu path is "Debug | Graphics | Start Diagnostics" (ALT+F5).
    factory.PushFlags();
    factory.flags = 
        D3DCOMPILE_ENABLE_STRICTNESS |
        D3DCOMPILE_IEEE_STRICTNESS |
        D3DCOMPILE_DEBUG |
        D3DCOMPILE_SKIP_OPTIMIZATION;
#endif
    mProgram = factory.CreateFromFiles(path, path, "");
#if !defined(GTE_DEV_OPENGL)
    factory.PopFlags();
#endif
    if (!mProgram)
    {
        // The program factory will generate Log* messages.
        return;
    }

    // Load the textures.
    path = environment.GetPath("BTGrass.png");
    mGrassTexture.reset(WICFileIO::Load(path, true));
    mGrassTexture->AutogenerateMipmaps();

    path = environment.GetPath("BTStone.png");
    mStoneTexture.reset(WICFileIO::Load(path, true));
    mStoneTexture->AutogenerateMipmaps();

    path = environment.GetPath("BTCloud.png");
    mCloudTexture.reset(WICFileIO::Load(path, true));
    mCloudTexture->AutogenerateMipmaps();

    // Create the shader constants.
    mPVWMatrixConstant = std::make_shared<ConstantBuffer>(sizeof(Matrix4x4<float>), true);
    mPVWMatrix = mPVWMatrixConstant->Get<Matrix4x4<float>>();
    *mPVWMatrix = Matrix4x4<float>::Identity();

    mFlowDirectionConstant = std::make_shared<ConstantBuffer>(sizeof(Vector2<float>), true);
    mFlowDirection = mFlowDirectionConstant->Get<Vector2<float>>();
    *mFlowDirection = { 0.0f, 0.0f };

    mPowerFactorConstant = std::make_shared<ConstantBuffer>(sizeof(float), true);
    mPowerFactor = mPowerFactorConstant->Get<float>();
    *mPowerFactor = 1.0f;

    // Create a 1-dimensional texture whose intensities are proportional to
    // height.
    unsigned int const numTexels = 256;
    mBlendTexture = std::make_shared<Texture1>(DF_R8_UNORM, numTexels);
    unsigned char* texels = mBlendTexture->Get<unsigned char>();
    for (unsigned int i = 0; i < numTexels; ++i, ++texels)
    {
        *texels = static_cast<unsigned char>(i);
    }

    // Create the texture samplers.  The common sampler uses trilinear
    // interpolation (mipmapping).  The blend sample uses bilinear
    // interpolation (no mipmapping).
    mCommonSampler = std::make_shared<SamplerState>();
    mCommonSampler->filter = SamplerState::MIN_L_MAG_L_MIP_L;
    mCommonSampler->mode[0] = SamplerState::WRAP;
    mCommonSampler->mode[1] = SamplerState::WRAP;
    mBlendSampler = std::make_shared<SamplerState>();
    mBlendSampler->filter = SamplerState::MIN_L_MAG_L_MIP_P;
    mBlendSampler->mode[0] = SamplerState::WRAP;

    // Set the resources for the shaders.
    std::shared_ptr<VertexShader> vshader = mProgram->GetVShader();
    std::shared_ptr<PixelShader> pshader = mProgram->GetPShader();
    vshader->Set("PVWMatrix", mPVWMatrixConstant);
    vshader->Set("FlowDirection", mFlowDirectionConstant);
    pshader->Set("PowerFactor", mPowerFactorConstant);
    pshader->Set("grassTexture", mGrassTexture);
    pshader->Set("stoneTexture", mStoneTexture);
    pshader->Set("blendTexture", mBlendTexture);
    pshader->Set("cloudTexture", mCloudTexture);
    pshader->Set("commonSampler", mCommonSampler);
    pshader->Set("blendSampler", mBlendSampler);

    created = true;
}
Beispiel #4
0
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;
}