DirectX11映射/取消映射运行时错误

时间:2014-03-16 23:27:46

标签: c++ directx shader directx-11

每当我每帧使用map / unmap函数时,我的程序错误并带有警告

“Engine.exe中0x0F285A07(atidxx32.dll)处的未处理异常:0xC0000005:访问冲突读取位置0x00000000

大约20秒后。

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

我认为这是因为renderTargetTexture无法以某种方式访问​​。

我在初始化时按照以下方式设置纹理,然后每帧。如果我不更新每一帧,程序工作正常,但我需要这样做才能将数组传递给GPU。

¬设置纹理描述

¬¬Code在设置纹理的最后一行(CreateTexture2D)中断。似乎是渲染目标。

¬映射和取消映射纹理

¬设置着色器资源


设置纹理

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
}

映射和取消映射

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);

设置渲染目标

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 个答案:

答案 0 :(得分:0)

不确定错误是什么,但我似乎通过消除设置纹理描述和设置着色器资源步骤来修复它。

现在,每一帧代码都只是映射和取消映射纹理,其他代码似乎是......不必要的。

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);