cv::Mat to DirectX11 Texture (OpenCV 3.1.0)

asked 2016-11-29 02:14:09 -0600

closecall gravatar image

updated 2016-11-29 08:01:24 -0600

Let's say I'm trying to display my webcam image in a game in DirectX11. I am using the prebuilt 3.1.0 libraries and I seem to be having difficulties with OpenCL while converting a Mat to a DirectX texture. I am using VS2013 Update 5 under Win10 and I am building a x64 windows application. The machine uses 2 x Nvidia Geforce GTX1080.

// CREATING DIRECTX TEXTURE
ID3D11Texture2D* myTexture = 0;
D3D11_TEXTURE2D_DEC desc_rgba;
desc_rgba.Width = 1024;
desc_rgba.Height = 768;
desc_rgba.MipLevels =1;
desc_rgba.ArraySize =1;
desc_rgba.Format = DXGI_FORMAT_R8G8B8A8_UNORM; // should be CV_8UC4
desc_rgba.SampleDesc.Count = 1;
desc_rgba.SampleDesc.Quality = 0;
desc_rgba.BindFlags = D3D11_BIND_SHADER_RESOURCE;
desc_rgba.Usage = D3D11_USAGE_DYNAMIC;
desc_rgba.CPUAccessFalgs = D3D11_CPU_ACCESS_WRITE;
desc_rgba.MiscFlags = 0;

HRESULT r = DIRECTX.Device->CreateTexture2D(&desc_rgba, 0, &myTexture);
if (FAILED(r)){
    throw std::runtime_error("Can't create DX texture!");
}

cv::ocl::Context oclContext;
if (cv::ocl::haveOpenCL()){// Returns 1        
    // NEW: Exception right here, writing at 0x0000...0
    oclContext = cv::directx::ocl::initializeContextFromD3D11Device(DIRECTX.Device); 
} 

//...

// GAME LOOP
// CV::MAT --> DIRECTX
cv::Mat webCamImg = ...; // get valid image somehow
cv::Mat convertedImg;
cv::cvtColor(webCamImg, convertedImage, CV_BGR2BGRA); //CV_8UC3 -> CV_8UC4
cv::directx::convertToD3D11Texture2D(convertedImg, myTexture); // (OLD: Throws exception!)

initializeContextFromD3D11Device is troubling me. Even all the samples are not working for me because of this line, but the OpenGL one does.

Here's the config log: http://pastebin.com/8i34sSRM

Update Edit 2: After reinstalling my graphics drivers and CUDA 7.5, haveOpenCL() returns 1 suddenly. Also, I don't jump to the code anymore when I get a cv::Exception(). What's up with that?


OLD! (EDIT1: Added cv::ocl::Context code and new exception (-222).)

EDIT2: Problem shifted to the cv::ocl::Context initialization after updating drivers.

EDIT3: Tried out the official DirectX11 sample code (EXAMPLE_DIRECTX d3d11_interop) and I get the same exception at the same place.

EDIT4:

  • EXAMPLE_DIRECTX d3d10_interop fails at the same line.
  • EXAMPLE_DIRECTX d3d9_interop fails at the same line.
  • EXAMPLE_DIRECTX d3d9ex_interop fails at the same line.
  • EXAMPLE _OPENGL opengl_interop actually works!
edit retag flag offensive close merge delete