OpenCV convertToD3D11Texture2D function causes access violation

0

I'm creating a C++ Windows app using DirectX to display PPM images in HDR. I'm using cv::imread to read the file into a Mat, and then I create a ID3D11Texture2D* texture. However, when I run it I get an access violation when running cv::directx::convertToD3D11Texture2D(mat, tex), which says tex is NULL.

To my understanding, CreateTexture2D initializes the texture, and convertToD3D11Texture2D copies the image to the texture.

This is the relevant code:

auto mat = cv::imread("fileL.ppm", cv::IMREAD_ANYCOLOR | cv::IMREAD_ANYDEPTH);

D3D11_TEXTURE2D_DESC desc;
desc.Width = 3840;
desc.Height = 2160;
desc.MipLevels = desc.ArraySize = 1;
desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
desc.SampleDesc.Count = 1;
desc.Usage = D3D11_USAGE_DYNAMIC;
desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
desc.MiscFlags = 0;

ID3D11Texture2D* tex = nullptr;

m_resources->GetDevice()->CreateTexture2D(&desc, nullptr, &tex);


cv::directx::convertToD3D11Texture2D(mat, tex);

Specifically, the errors are:

0x00007FFC4750A839 in file.exe: Microsoft C++ exception: _com_error at memory location 0x00000045A28FF340.
Exception thrown at 0x00007FFBCD08A390 (opencv_world410d.dll) in file.exe: 0xC0000005: Access violation reading location 0x0000000000000000.

Edit: After adding desc.SampleDesc.quality = 0, I no longer get the errors for CreateTexture2D but I do get a generic exception at convertToD3D11Texture2D: Exception at memory location 0x0000002A3836E8D0

Edit 2: After caching the exception, the exception message is:

OpenCV(4.1.0) c:\build\master_winpack-build-win64-vc15\opencv\modules\core\src\directx.cpp:1040: error: (-215:Assertion failed) textureType == srcType in function 'cv::directx::__convertToD3D11Texture2DNV'
c++
windows
opencv
graphics
directx
asked on Stack Overflow Jul 11, 2019 by Richard Robinson • edited Jul 12, 2019 by Richard Robinson

1 Answer

0

After finally being able to properly debug the error, I discovered the problem was a result of my input matrix having 3 channels, not 4. To fix it, I used cv::mixChannels to add an empty alpha channel, and so now the type of my texture and matrix match.

Working code:

cv::Mat mat = cv::imread("file.ppm", cv::IMREAD_ANYCOLOR | cv::IMREAD_ANYDEPTH);
    cv::Mat as4channelMat(mat.size(), CV_MAKE_TYPE(mat.depth(), 4));

    int conversion[] = { 0, 0, 1, 1, 2, 2, -1, 3 };
    cv::mixChannels(&mat, 1, &as4channelMat, 1, conversion, 4);

    D3D11_TEXTURE2D_DESC desc;
    desc.Width = 3840;
    desc.Height = 2160;
    desc.MipLevels = desc.ArraySize = 1;
    desc.Format = DXGI_FORMAT_R16G16B16A16_UNORM;
    desc.SampleDesc.Count = 1;
    desc.SampleDesc.Quality = 0;
    desc.Usage = D3D11_USAGE_DEFAULT;
    desc.BindFlags = 0;
    desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE | D3D11_CPU_ACCESS_READ;
    desc.MiscFlags = 0;

    ID3D11Texture2D* tex = nullptr;

    auto hr = m_resources->GetDevice()->CreateTexture2D(&desc, nullptr, &tex);

    if FAILED(hr)
    {
        _com_error err(hr);
        LPCTSTR errMsg = err.ErrorMessage();
    }

    try {
        cv::directx::convertToD3D11Texture2D(as4channelMat, tex);
    } catch (cv::Exception& e)
    {
        std::cerr << "ERROR: " << e.msg << std::endl;
        throw e;
    }
answered on Stack Overflow Jul 12, 2019 by Richard Robinson

User contributions licensed under CC BY-SA 3.0