简体   繁体   English

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

[英]DirectX11 Map / Unmap Runtime Error

Whenever I use the map / unmap functions per frame my program errors with the warning 每当我每帧使用map / unmap函数时,程序错误并显示警告

"Unhandled exception at 0x0F285A07 (atidxx32.dll) in Engine.exe: 0xC0000005: Access violation reading location 0x00000000 “ Engine.exe中的0x0F285A07(atidxx32.dll)处未处理的异常:0xC0000005:访问冲突读取位置0x00000000

after about 20 seconds. 大约20秒后。

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

I believe this is because of the renderTargetTexture not being accessible in some way. 我相信这是因为无法以某种方式访问​​renderTargetTexture。

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. 如果我不更新每一帧,该程序都可以正常工作,但是我需要这样做才能将数组传递给GPU。

¬Setup Texture Description ¬设置纹理说明

¬¬Code breaks on last line of setting texture (CreateTexture2D). ¬代码在设置纹理(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
}

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM