Sunday, July 31, 2016

Height Map Through Compute Shader, Need Some Pointers

Height Map Through Compute Shader, Need Some Pointers

Hi all,

For about 2 weeks now I've been struggling with an exercise I can't figure out so far.

Here's the goal, in short:

 

- render waves through a vertex and pixel shader

-- where the height of the waves vertices are read from a texture, containing floats (each float contains the height of a wave vertex)

- use a compute shader to update the wave heights

- update them, and render again etc.

 

My first goal is to achieve a situation where at least I get no errors/ warnings and the Compute shader works, stores the new texture, which then is read by the rendering effect, fetching these height map values for the wave vertices.

 

In my book it says to do this, I would need 4 textures in the compute shader:

// textures containing single floats
Texture2D       gPrevSolInput;
Texture2D       gCurrSolInput;

RWTexture2D<float> gCurrSolOutput;
RWTexture2D<float> gNextSolOutput;

Here are some relevant code snippers of my current (non-working) approach.
Let me know if you need other parts to be able to identify the issue.

 

The extra line in the existing rendering function, right before drawint the waves:

        Effects::mBasicFX->SetWavesHeightMap(mWavesCS.mOutputNextSolUAV);

The basic rendering effects (VS and PS), which should fetch the height map:

Texture2D gWavesHeightMap;

SamplerState samHeightMap
{
        Filter = MIN_MAG_MIP_POINT;

        AddressU = WRAP;
        AddressV = WRAP;
};

VertexOut VS(VertexIn vin)
{
        VertexOut vout;

        // Retrieve wave height map data
        if(gIsWaves)
        {
                float heightMapVal = gWavesHeightMap.SampleLevel(samHeightMap, vin.Tex, 0.0f).r;
                vin.PosL.y = heightMapVal;
        }

Calling the compute shader and the compute shader itself:

void CSWrapper::ExecuteCS(ID3D11DeviceContext *dc)
{
        ID3D11ShaderResourceView *nullSRV[2] = { 0, 0 };
        ID3D11UnorderedAccessView *nullUAV[2] = { 0, 0 };

        D3DX11_TECHNIQUE_DESC techDesc;
        Effects::mWavesCSFX->mWavesTech->GetDesc(&techDesc);

        for(UINT p=0;p<techDesc.Passes;++p)
        {
                Effects::mWavesCSFX->SetInputPrevSolSRV(mInputPrevSolSRV);
                Effects::mWavesCSFX->SetInputCurrSolSRV(mInputCurrSolSRV);

                Effects::mWavesCSFX->SetOutputCurrSolUAV(mOutputCurrSolUAV);
                Effects::mWavesCSFX->SetOutputNextSolUAV(mOutputNextSolUAV);

                Effects::mWavesCSFX->mWavesTech->GetPassByIndex(p)->Apply(0, dc);

                // grid 160x160 = 25.600 threads needed, /64 (group size 64,1,1) = 400
                dc->Dispatch(400, 1, 1);
        }

        // clean up
        dc->CSSetShaderResources(0, 2, nullSRV);
        dc->CSSetUnorderedAccessViews(0, 2, nullUAV, 0);

        // disable compute shader
        dc->CSSetShader(0, 0, 0);
}

[numthreads(64, 1, 1)]
void CS(int3 dispatchThreadID : SV_DispatchThreadID)
{
        gNextSolOutput[dispatchThreadID.xy] = gCurrSolInput[dispatchThreadID.xy] + 0.01f;

        gCurrSolOutput[dispatchThreadID.xy] = gNextSolOutput[dispatchThreadID.xy];
}

Creating the 4 textures and SRV/UAV's for (compute) shader:

void CSWrapper::Init(ID3D11Device *pDevice, ID3D11DeviceContext *dc, DirectX::XMFLOAT3* pPrevSolution, DirectX::XMFLOAT3* pCurrSolution, const int pM, const int pN)
{
        // There are 4 views

        // 2 input SRV's        -> prev and curr solution
        // 2 output UAV's       -> curr and next solution

        // The SRV's has to initially be filled and created: textures
        
        /***************************************/
        /**** 1: input SRV (prev solution)      ****/
        /***************************************/

        // create texture
        D3D11_TEXTURE2D_DESC inputPrevSolTexDesc;
        inputPrevSolTexDesc.Width                               = pM;           // grid size of the waves, rows
        inputPrevSolTexDesc.Height                              = pN;           // grid size of the waves, colums
        inputPrevSolTexDesc.MipLevels                   = 1;
        inputPrevSolTexDesc.ArraySize                   = 1;
        inputPrevSolTexDesc.Format                              = DXGI_FORMAT_R32_FLOAT;
        inputPrevSolTexDesc.SampleDesc.Count    = 1;
        inputPrevSolTexDesc.SampleDesc.Quality  = 0;
        inputPrevSolTexDesc.Usage                               = D3D11_USAGE_DEFAULT;
        inputPrevSolTexDesc.BindFlags                   = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_UNORDERED_ACCESS;
        inputPrevSolTexDesc.CPUAccessFlags              = 0;
        inputPrevSolTexDesc.MiscFlags                   = 0;

        // initial texture data
        float *tempArrayA = new float[pM*pN];
        for(int i=0;i<pM*pN;++i) tempArrayA[i] = pPrevSolution[i].y;
        
        D3D11_SUBRESOURCE_DATA vinitDataA;
        vinitDataA.SysMemPitch = sizeof(float) * pM;
        vinitDataA.pSysMem = &tempArrayA[0];

        // create the texture
        ID3D11Texture2D *inputPrevSolTex = 0;
        HR(pDevice->CreateTexture2D(&inputPrevSolTexDesc, &vinitDataA, &inputPrevSolTex));
        
        delete[] tempArrayA;

        // create SRV
        D3D11_SHADER_RESOURCE_VIEW_DESC prevSolSrvDesc;
        prevSolSrvDesc.Format                                   = DXGI_FORMAT_R32_FLOAT;
        prevSolSrvDesc.ViewDimension                    = D3D11_SRV_DIMENSION_TEXTURE2D;
        prevSolSrvDesc.BufferEx.FirstElement    = 0;
        prevSolSrvDesc.BufferEx.Flags                   = 0;
        prevSolSrvDesc.BufferEx.NumElements             = pM * pN;

        prevSolSrvDesc.Texture2D.MipLevels                      = 1;
        prevSolSrvDesc.Texture2D.MostDetailedMip        = 0;

        pDevice->CreateShaderResourceView(inputPrevSolTex, &prevSolSrvDesc, &mInputPrevSolSRV);
        ReleaseCOM(inputPrevSolTex);

        /***************************************/
        /**** 2: input SRV (curr solution)      ****/
        /***************************************/

        // create texture
        D3D11_TEXTURE2D_DESC inputCurrSolTexDesc;
        inputCurrSolTexDesc.Width                               = pM;           // grid size of the waves, rows
        inputCurrSolTexDesc.Height                              = pN;           // grid size of the waves, colums
        inputCurrSolTexDesc.MipLevels                   = 1;
        inputCurrSolTexDesc.ArraySize                   = 1;
        inputCurrSolTexDesc.Format                              = DXGI_FORMAT_R32_FLOAT;
        inputCurrSolTexDesc.SampleDesc.Count    = 1;
        inputCurrSolTexDesc.SampleDesc.Quality  = 0;
        inputCurrSolTexDesc.Usage                               = D3D11_USAGE_DEFAULT;
        inputCurrSolTexDesc.BindFlags                   = D3D11_BIND_SHADER_RESOURCE | D3D11_BIND_UNORDERED_ACCESS;
        inputCurrSolTexDesc.CPUAccessFlags              = 0;
        inputCurrSolTexDesc.MiscFlags                   = 0;

        // initial texture data
        float *tempArrayB = new float[pM*pN];
        for(int i=0;i<pM*pN;++i) tempArrayB[i] = pCurrSolution[i].y;
        
        D3D11_SUBRESOURCE_DATA vinitDataB;
        vinitDataB.SysMemPitch = sizeof(float) * pM;
        vinitDataB.pSysMem = &tempArrayB[0];

        // create the texture
        ID3D11Texture2D *inputCurrSolTex = 0;
        HR(pDevice->CreateTexture2D(&inputCurrSolTexDesc, &vinitDataB, &inputCurrSolTex));
        
        delete[] tempArrayB;

        // create SRV
        D3D11_SHADER_RESOURCE_VIEW_DESC currSolSrvDesc;
        currSolSrvDesc.Format                                   = DXGI_FORMAT_R32_FLOAT;
        currSolSrvDesc.ViewDimension                    = D3D11_SRV_DIMENSION_TEXTURE2D;
        currSolSrvDesc.BufferEx.FirstElement    = 0;
        currSolSrvDesc.BufferEx.Flags                   = 0;
        currSolSrvDesc.BufferEx.NumElements             = pM * pN;

        currSolSrvDesc.Texture2D.MipLevels                      = 1;
        currSolSrvDesc.Texture2D.MostDetailedMip        = 0;

        pDevice->CreateShaderResourceView(inputCurrSolTex, &currSolSrvDesc, &mInputCurrSolSRV);

        /***************************************/
        /**** 3: output UAV (curr solution)     ****/
        /***************************************/

        D3D11_UNORDERED_ACCESS_VIEW_DESC uavCurrDesc;
        uavCurrDesc.Format                                      = DXGI_FORMAT_R32_FLOAT;
        uavCurrDesc.ViewDimension                       = D3D11_UAV_DIMENSION_TEXTURE2D;
        uavCurrDesc.Buffer.FirstElement         = 0;
        uavCurrDesc.Buffer.Flags                        = 0;
        uavCurrDesc.Buffer.NumElements          = pM * pN;

        pDevice->CreateUnorderedAccessView(inputCurrSolTex, &uavCurrDesc, &mOutputCurrSolUAV);

//      ReleaseCOM(inputCurrSolTex);            // release texture, both SRV and UAV created

        /***************************************/
        /**** 4: output UAV (next solution)     ****/
        /***************************************/

        D3D11_UNORDERED_ACCESS_VIEW_DESC uavNextDesc;
        uavNextDesc.Format                                      = DXGI_FORMAT_R32_FLOAT;
        uavNextDesc.ViewDimension                       = D3D11_UAV_DIMENSION_TEXTURE2D;
        uavNextDesc.Buffer.FirstElement         = 0;
        uavNextDesc.Buffer.Flags                        = 0;
        uavNextDesc.Buffer.NumElements          = pM * pN;

        pDevice->CreateUnorderedAccessView(inputCurrSolTex, &uavNextDesc, &mOutputNextSolUAV);

        ReleaseCOM(inputCurrSolTex);            // release texture, both SRV and UAV created

}

I would really appreciate if someone could help me in the right direction, to finally get this case solved..

Thanks in advance.


1 comment:

  1. Thank you for the blog regarding app mobile web mobile app hire developers android ios windows phone. For Technical academic projects Matlab Projects for ECE On Takeoff Edu Group.

    ReplyDelete