0
votes

Whenever I use the map / unmap functions per frame my program errors with the warning

"Unhandled exception at 0x0F285A07 (atidxx32.dll) in Engine.exe: 0xC0000005: Access violation reading location 0x00000000

after about 20 seconds.

On the line result = device->CreateTexture2D(&textureDesc, NULL, &renderTargetTexture);

I believe this is because of the renderTargetTexture not being accessible in some way.

I set the texture in the following way at initialisation and then every frame. The program works fine if I do not update every frame but I need to do this to pass arrays to the GPU.

¬Setup Texture Description

¬¬Code breaks on last line of setting texture (CreateTexture2D). Seems to be the render target.

¬Map and Unmap the texture

¬Set the shader resource


Setting up the texture

bool setupTextureDesc(ID3D11Device* device, ID3D11DeviceContext* deviceContext, D3D11_TEXTURE2D_DESC& textureDesc)
{
    HRESULT result;
    // Initialize the render target texture description.
    ZeroMemory(&textureDesc, sizeof(textureDesc));

    // Setup the render target texture description.
    textureDesc.Width = fluidBufferObj.width;
    textureDesc.Height = fluidBufferObj.height;
    textureDesc.MipLevels = 1;
    textureDesc.ArraySize = 1;
    textureDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
    textureDesc.Usage = D3D11_USAGE_DYNAMIC;;
    textureDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
    textureDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;;
    textureDesc.MiscFlags = 0;
    textureDesc.SampleDesc.Count = 1;
    textureDesc.SampleDesc.Quality = 0;
    // Create the render target texture.
    result = device->CreateTexture2D(&textureDesc, NULL, &renderTargetTexture);
    HRFAIL
}

Map and Unmap

D3D11_MAPPED_SUBRESOURCE mappedResource;
    //Map the resources. Blocks the GPU from accessing the file. 
    result = deviceContext->Map(renderTargetTexture, 0, D3D11_MAP_WRITE_DISCARD, 0, &mappedResource);
    HRFAIL
    //Set the pixels
    UCHAR* pTexels = (UCHAR*)mappedResource.pData;
    //For 3D do the same but add "depthStart" as mappedResource.DepthPitch * depth

    int startIndex = (float)( ((float)percentage / 100) * (float)textureDesc.Height );

    for( UINT row = 0; row < textureDesc.Height; row++ )
    {
        //Row number * height
        UINT rowStart = row * mappedResource.RowPitch;
        for( UINT col = 0; col < textureDesc.Width; col++ )
        {
            if( row >= startIndex && row <= (startIndex + 10) )
            {
                //width * number of channels (r,g,b,a)
                UINT colStart = col * 4;
                pTexels[rowStart + colStart + 0] = 0; // Red
                pTexels[rowStart + colStart + 1] = 0; // Green
                pTexels[rowStart + colStart + 2] = 255; // Blue
                pTexels[rowStart + colStart + 3] = 255; // Alpha
            }
            else
            {
                //width * number of channels (r,g,b,a)
                UINT colStart = col * 4;
                pTexels[rowStart + colStart + 0] = 255; // Red
                pTexels[rowStart + colStart + 1] = 0; // Green
                pTexels[rowStart + colStart + 2] = 0; // Blue
                pTexels[rowStart + colStart + 3] = 255; // Alpha
            }
        }
    }
    //Free the resource
    deviceContext->Unmap(renderTargetTexture, 0);

Setting render target

bool setupTextureDesc(ID3D11Device* device, ID3D11DeviceContext* deviceContext, D3D11_TEXTURE2D_DESC& textureDesc)
{
    HRESULT result;
    // Initialize the render target texture description.
    ZeroMemory(&textureDesc, sizeof(textureDesc));

    // Setup the render target texture description.
    textureDesc.Width = fluidBufferObj.width;
    textureDesc.Height = fluidBufferObj.height;
    textureDesc.MipLevels = 1;
    textureDesc.ArraySize = 1;
    textureDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
    textureDesc.Usage = D3D11_USAGE_DYNAMIC;;
    textureDesc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
    textureDesc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;;
    textureDesc.MiscFlags = 0;
    textureDesc.SampleDesc.Count = 1;
    textureDesc.SampleDesc.Quality = 0;
    // Create the render target texture.
    result = device->CreateTexture2D(&textureDesc, NULL, &renderTargetTexture);
    HRFAIL
}
1
Kill FRAPS! :) It's my first advice for any DX issue.CodeAngry
Not using FRAPS, I assume you mean the screen recording software?Questioning
Access violation reading location 0x00000000, this is because you are reading an pointer point to NULL, it seems renderTargetTexture was not correctly initialized. but you didn't paste enough code.zdd
The render target is initialised fine as it crashes after 20 seconds, unless it fails to initialise from the previous frame.Questioning
It is not failing to be reinitialised. I suspect it is being caught by the GPU or something strange but I don't see how that's possible.Questioning

1 Answers

0
votes

Not sure what the error was but I seem to have fixed it by eliminating the setting up texture description and setting shader resource steps.

The every frame code is simply mapping and unmapping the texture now, seems the others were...unnecessary.

D3D11_MAPPED_SUBRESOURCE mappedResource;
//Map the resources. Blocks the GPU from accessing the file. 
result = deviceContext->Map(renderTargetTexture, 0, D3D11_MAP_WRITE_DISCARD, 0,             &mappedResource);
HRFAIL

D3D11_TEXTURE2D_DESC* tDesc = new D3D11_TEXTURE2D_DESC();
renderTargetTexture->GetDesc(tDesc);

//Set the pixels
UCHAR* pTexels = (UCHAR*)mappedResource.pData;
//For 3D do the same but add "depthStart" as mappedResource.DepthPitch * depth

int startIndex = (float)( ((float)percentage / 100) * (float)tDesc->Height );

for( UINT row = 0; row < tDesc->Height; row++ )
{
    //Row number * height
    UINT rowStart = row * mappedResource.RowPitch;
    for( UINT col = 0; col < tDesc->Width; col++ )
    {
        if( row >= startIndex && row <= (startIndex + 10) )
        {
            //width * number of channels (r,g,b,a)
            UINT colStart = col * 4;
            pTexels[rowStart + colStart + 0] = 0; // Red
            pTexels[rowStart + colStart + 1] = 0; // Green
            pTexels[rowStart + colStart + 2] = 255; // Blue
            pTexels[rowStart + colStart + 3] = 255; // Alpha
        }
        else
        {
            //width * number of channels (r,g,b,a)
            UINT colStart = col * 4;
            pTexels[rowStart + colStart + 0] = 255; // Red
            pTexels[rowStart + colStart + 1] = 0; // Green
            pTexels[rowStart + colStart + 2] = 0; // Blue
            pTexels[rowStart + colStart + 3] = 255; // Alpha
        }
    }
}
//Free the resource
deviceContext->Unmap(renderTargetTexture, 0);