d3d11_interop.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. /*
  2. // A sample program demonstrating interoperability of OpenCV cv::UMat with Direct X surface
  3. // At first, the data obtained from video file or camera and placed onto Direct X surface,
  4. // following mapping of this Direct X surface to OpenCV cv::UMat and call cv::Blur function.
  5. // The result is mapped back to Direct X surface and rendered through Direct X API.
  6. */
  7. #define WIN32_LEAN_AND_MEAN
  8. #include <windows.h>
  9. #include <d3d11.h>
  10. #include "opencv2/core.hpp"
  11. #include "opencv2/core/directx.hpp"
  12. #include "opencv2/core/ocl.hpp"
  13. #include "opencv2/imgproc.hpp"
  14. #include "opencv2/videoio.hpp"
  15. #include "d3dsample.hpp"
  16. #pragma comment (lib, "d3d11.lib")
  17. class D3D11WinApp : public D3DSample
  18. {
  19. public:
  20. D3D11WinApp(int width, int height, std::string& window_name, cv::VideoCapture& cap)
  21. : D3DSample(width, height, window_name, cap),
  22. m_nv12_available(false)
  23. {}
  24. ~D3D11WinApp() {}
  25. int create(void)
  26. {
  27. // base initialization
  28. D3DSample::create();
  29. // initialize DirectX
  30. HRESULT r;
  31. DXGI_SWAP_CHAIN_DESC scd;
  32. ZeroMemory(&scd, sizeof(DXGI_SWAP_CHAIN_DESC));
  33. scd.BufferCount = 1; // one back buffer
  34. scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; // use 32-bit color
  35. scd.BufferDesc.Width = m_width; // set the back buffer width
  36. scd.BufferDesc.Height = m_height; // set the back buffer height
  37. scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; // how swap chain is to be used
  38. scd.OutputWindow = m_hWnd; // the window to be used
  39. scd.SampleDesc.Count = 1; // how many multisamples
  40. scd.Windowed = TRUE; // windowed/full-screen mode
  41. scd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
  42. scd.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH; // allow full-screen switching
  43. r = ::D3D11CreateDeviceAndSwapChain(
  44. NULL,
  45. D3D_DRIVER_TYPE_HARDWARE,
  46. NULL,
  47. 0,
  48. NULL,
  49. 0,
  50. D3D11_SDK_VERSION,
  51. &scd,
  52. &m_pD3D11SwapChain,
  53. &m_pD3D11Dev,
  54. NULL,
  55. &m_pD3D11Ctx);
  56. if (FAILED(r))
  57. {
  58. throw std::runtime_error("D3D11CreateDeviceAndSwapChain() failed!");
  59. }
  60. #if defined(_WIN32_WINNT_WIN8) && _WIN32_WINNT >= _WIN32_WINNT_WIN8
  61. UINT fmt = 0;
  62. r = m_pD3D11Dev->CheckFormatSupport(DXGI_FORMAT_NV12, &fmt);
  63. if (SUCCEEDED(r))
  64. {
  65. m_nv12_available = true;
  66. }
  67. #endif
  68. r = m_pD3D11SwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&m_pBackBuffer);
  69. if (FAILED(r))
  70. {
  71. throw std::runtime_error("GetBuffer() failed!");
  72. }
  73. r = m_pD3D11Dev->CreateRenderTargetView(m_pBackBuffer, NULL, &m_pRenderTarget);
  74. if (FAILED(r))
  75. {
  76. throw std::runtime_error("CreateRenderTargetView() failed!");
  77. }
  78. m_pD3D11Ctx->OMSetRenderTargets(1, &m_pRenderTarget, NULL);
  79. D3D11_VIEWPORT viewport;
  80. ZeroMemory(&viewport, sizeof(D3D11_VIEWPORT));
  81. viewport.Width = (float)m_width;
  82. viewport.Height = (float)m_height;
  83. viewport.MinDepth = 0.0f;
  84. viewport.MaxDepth = 0.0f;
  85. m_pD3D11Ctx->RSSetViewports(1, &viewport);
  86. m_pSurfaceRGBA = 0;
  87. m_pSurfaceNV12 = 0;
  88. m_pSurfaceNV12_cpu_copy = 0;
  89. D3D11_TEXTURE2D_DESC desc_rgba;
  90. desc_rgba.Width = m_width;
  91. desc_rgba.Height = m_height;
  92. desc_rgba.MipLevels = 1;
  93. desc_rgba.ArraySize = 1;
  94. desc_rgba.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
  95. desc_rgba.SampleDesc.Count = 1;
  96. desc_rgba.SampleDesc.Quality = 0;
  97. desc_rgba.BindFlags = D3D11_BIND_SHADER_RESOURCE;
  98. desc_rgba.Usage = D3D11_USAGE_DYNAMIC;
  99. desc_rgba.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
  100. desc_rgba.MiscFlags = 0;
  101. r = m_pD3D11Dev->CreateTexture2D(&desc_rgba, 0, &m_pSurfaceRGBA);
  102. if (FAILED(r))
  103. {
  104. throw std::runtime_error("Can't create DX texture");
  105. }
  106. #if defined(_WIN32_WINNT_WIN8) && _WIN32_WINNT >= _WIN32_WINNT_WIN8
  107. if(m_nv12_available)
  108. {
  109. D3D11_TEXTURE2D_DESC desc_nv12;
  110. desc_nv12.Width = m_width;
  111. desc_nv12.Height = m_height;
  112. desc_nv12.MipLevels = 1;
  113. desc_nv12.ArraySize = 1;
  114. desc_nv12.Format = DXGI_FORMAT_NV12;
  115. desc_nv12.SampleDesc.Count = 1;
  116. desc_nv12.SampleDesc.Quality = 0;
  117. desc_nv12.BindFlags = D3D11_BIND_SHADER_RESOURCE;
  118. desc_nv12.Usage = D3D11_USAGE_DEFAULT;
  119. desc_nv12.CPUAccessFlags = 0;
  120. desc_nv12.MiscFlags = D3D11_RESOURCE_MISC_SHARED;
  121. r = m_pD3D11Dev->CreateTexture2D(&desc_nv12, 0, &m_pSurfaceNV12);
  122. if (FAILED(r))
  123. {
  124. throw std::runtime_error("Can't create DX NV12 texture");
  125. }
  126. D3D11_TEXTURE2D_DESC desc_nv12_cpu_copy;
  127. desc_nv12_cpu_copy.Width = m_width;
  128. desc_nv12_cpu_copy.Height = m_height;
  129. desc_nv12_cpu_copy.MipLevels = 1;
  130. desc_nv12_cpu_copy.ArraySize = 1;
  131. desc_nv12_cpu_copy.Format = DXGI_FORMAT_NV12;
  132. desc_nv12_cpu_copy.SampleDesc.Count = 1;
  133. desc_nv12_cpu_copy.SampleDesc.Quality = 0;
  134. desc_nv12_cpu_copy.BindFlags = 0;
  135. desc_nv12_cpu_copy.Usage = D3D11_USAGE_STAGING;
  136. desc_nv12_cpu_copy.CPUAccessFlags = /*D3D11_CPU_ACCESS_WRITE | */D3D11_CPU_ACCESS_READ;
  137. desc_nv12_cpu_copy.MiscFlags = 0;
  138. r = m_pD3D11Dev->CreateTexture2D(&desc_nv12_cpu_copy, 0, &m_pSurfaceNV12_cpu_copy);
  139. if (FAILED(r))
  140. {
  141. throw std::runtime_error("Can't create DX NV12 texture");
  142. }
  143. }
  144. #endif
  145. // initialize OpenCL context of OpenCV lib from DirectX
  146. if (cv::ocl::haveOpenCL())
  147. {
  148. m_oclCtx = cv::directx::ocl::initializeContextFromD3D11Device(m_pD3D11Dev);
  149. }
  150. m_oclDevName = cv::ocl::useOpenCL() ?
  151. cv::ocl::Context::getDefault().device(0).name() :
  152. "No OpenCL device";
  153. return EXIT_SUCCESS;
  154. } // create()
  155. // get media data on DX surface for further processing
  156. int get_surface(ID3D11Texture2D** ppSurface, bool use_nv12)
  157. {
  158. HRESULT r;
  159. if (!m_cap.read(m_frame_bgr))
  160. return EXIT_FAILURE;
  161. if (use_nv12)
  162. {
  163. cv::cvtColor(m_frame_bgr, m_frame_i420, cv::COLOR_BGR2YUV_I420);
  164. convert_I420_to_NV12(m_frame_i420, m_frame_nv12, m_width, m_height);
  165. m_pD3D11Ctx->UpdateSubresource(m_pSurfaceNV12, 0, 0, m_frame_nv12.data, (UINT)m_frame_nv12.step[0], (UINT)m_frame_nv12.total());
  166. }
  167. else
  168. {
  169. cv::cvtColor(m_frame_bgr, m_frame_rgba, cv::COLOR_BGR2RGBA);
  170. // process video frame on CPU
  171. UINT subResource = ::D3D11CalcSubresource(0, 0, 1);
  172. D3D11_MAPPED_SUBRESOURCE mappedTex;
  173. r = m_pD3D11Ctx->Map(m_pSurfaceRGBA, subResource, D3D11_MAP_WRITE_DISCARD, 0, &mappedTex);
  174. if (FAILED(r))
  175. {
  176. throw std::runtime_error("surface mapping failed!");
  177. }
  178. cv::Mat m(m_height, m_width, CV_8UC4, mappedTex.pData, mappedTex.RowPitch);
  179. m_frame_rgba.copyTo(m);
  180. m_pD3D11Ctx->Unmap(m_pSurfaceRGBA, subResource);
  181. }
  182. *ppSurface = use_nv12 ? m_pSurfaceNV12 : m_pSurfaceRGBA;
  183. return EXIT_SUCCESS;
  184. } // get_surface()
  185. // process and render media data
  186. int render()
  187. {
  188. try
  189. {
  190. if (m_shutdown)
  191. return EXIT_SUCCESS;
  192. // capture user input once
  193. MODE mode = (m_mode == MODE_GPU_NV12 && !m_nv12_available) ? MODE_GPU_RGBA : m_mode;
  194. HRESULT r;
  195. ID3D11Texture2D* pSurface = 0;
  196. r = get_surface(&pSurface, mode == MODE_GPU_NV12);
  197. if (FAILED(r))
  198. {
  199. throw std::runtime_error("get_surface() failed!");
  200. }
  201. m_timer.reset();
  202. m_timer.start();
  203. switch (mode)
  204. {
  205. case MODE_CPU:
  206. {
  207. // process video frame on CPU
  208. UINT subResource = ::D3D11CalcSubresource(0, 0, 1);
  209. D3D11_MAPPED_SUBRESOURCE mappedTex;
  210. r = m_pD3D11Ctx->Map(pSurface, subResource, D3D11_MAP_WRITE_DISCARD, 0, &mappedTex);
  211. if (FAILED(r))
  212. {
  213. throw std::runtime_error("surface mapping failed!");
  214. }
  215. cv::Mat m(m_height, m_width, CV_8UC4, mappedTex.pData, (int)mappedTex.RowPitch);
  216. if (m_demo_processing)
  217. {
  218. // blur data from D3D11 surface with OpenCV on CPU
  219. cv::blur(m, m, cv::Size(15, 15));
  220. }
  221. m_timer.stop();
  222. cv::String strMode = cv::format("mode: %s", m_modeStr[MODE_CPU].c_str());
  223. cv::String strProcessing = m_demo_processing ? "blur frame" : "copy frame";
  224. cv::String strTime = cv::format("time: %4.3f msec", m_timer.getTimeMilli());
  225. cv::String strDevName = cv::format("OpenCL device: %s", m_oclDevName.c_str());
  226. cv::putText(m, strMode, cv::Point(0, 20), cv::FONT_HERSHEY_SIMPLEX, 0.8, cv::Scalar(0, 0, 200), 2);
  227. cv::putText(m, strProcessing, cv::Point(0, 40), cv::FONT_HERSHEY_SIMPLEX, 0.8, cv::Scalar(0, 0, 200), 2);
  228. cv::putText(m, strTime, cv::Point(0, 60), cv::FONT_HERSHEY_SIMPLEX, 0.8, cv::Scalar(0, 0, 200), 2);
  229. cv::putText(m, strDevName, cv::Point(0, 80), cv::FONT_HERSHEY_SIMPLEX, 0.8, cv::Scalar(0, 0, 200), 2);
  230. m_pD3D11Ctx->Unmap(pSurface, subResource);
  231. break;
  232. }
  233. case MODE_GPU_RGBA:
  234. case MODE_GPU_NV12:
  235. {
  236. // process video frame on GPU
  237. cv::UMat u;
  238. cv::directx::convertFromD3D11Texture2D(pSurface, u);
  239. if (m_demo_processing)
  240. {
  241. // blur data from D3D11 surface with OpenCV on GPU with OpenCL
  242. cv::blur(u, u, cv::Size(15, 15));
  243. }
  244. m_timer.stop();
  245. cv::String strMode = cv::format("mode: %s", m_modeStr[mode].c_str());
  246. cv::String strProcessing = m_demo_processing ? "blur frame" : "copy frame";
  247. cv::String strTime = cv::format("time: %4.3f msec", m_timer.getTimeMilli());
  248. cv::String strDevName = cv::format("OpenCL device: %s", m_oclDevName.c_str());
  249. cv::putText(u, strMode, cv::Point(0, 20), cv::FONT_HERSHEY_SIMPLEX, 0.8, cv::Scalar(0, 0, 200), 2);
  250. cv::putText(u, strProcessing, cv::Point(0, 40), cv::FONT_HERSHEY_SIMPLEX, 0.8, cv::Scalar(0, 0, 200), 2);
  251. cv::putText(u, strTime, cv::Point(0, 60), cv::FONT_HERSHEY_SIMPLEX, 0.8, cv::Scalar(0, 0, 200), 2);
  252. cv::putText(u, strDevName, cv::Point(0, 80), cv::FONT_HERSHEY_SIMPLEX, 0.8, cv::Scalar(0, 0, 200), 2);
  253. cv::directx::convertToD3D11Texture2D(u, pSurface);
  254. if (mode == MODE_GPU_NV12)
  255. {
  256. // just for rendering, we need to convert NV12 to RGBA.
  257. m_pD3D11Ctx->CopyResource(m_pSurfaceNV12_cpu_copy, m_pSurfaceNV12);
  258. // process video frame on CPU
  259. {
  260. UINT subResource = ::D3D11CalcSubresource(0, 0, 1);
  261. D3D11_MAPPED_SUBRESOURCE mappedTex;
  262. r = m_pD3D11Ctx->Map(m_pSurfaceNV12_cpu_copy, subResource, D3D11_MAP_READ, 0, &mappedTex);
  263. if (FAILED(r))
  264. {
  265. throw std::runtime_error("surface mapping failed!");
  266. }
  267. cv::Mat frame_nv12(m_height + (m_height / 2), m_width, CV_8UC1, mappedTex.pData, mappedTex.RowPitch);
  268. cv::cvtColor(frame_nv12, m_frame_rgba, cv::COLOR_YUV2RGBA_NV12);
  269. m_pD3D11Ctx->Unmap(m_pSurfaceNV12_cpu_copy, subResource);
  270. }
  271. {
  272. UINT subResource = ::D3D11CalcSubresource(0, 0, 1);
  273. D3D11_MAPPED_SUBRESOURCE mappedTex;
  274. r = m_pD3D11Ctx->Map(m_pSurfaceRGBA, subResource, D3D11_MAP_WRITE_DISCARD, 0, &mappedTex);
  275. if (FAILED(r))
  276. {
  277. throw std::runtime_error("surface mapping failed!");
  278. }
  279. cv::Mat m(m_height, m_width, CV_8UC4, mappedTex.pData, mappedTex.RowPitch);
  280. m_frame_rgba.copyTo(m);
  281. m_pD3D11Ctx->Unmap(m_pSurfaceRGBA, subResource);
  282. }
  283. pSurface = m_pSurfaceRGBA;
  284. }
  285. break;
  286. }
  287. } // switch
  288. // traditional DX render pipeline:
  289. // BitBlt surface to backBuffer and flip backBuffer to frontBuffer
  290. m_pD3D11Ctx->CopyResource(m_pBackBuffer, pSurface);
  291. // present the back buffer contents to the display
  292. // switch the back buffer and the front buffer
  293. r = m_pD3D11SwapChain->Present(0, 0);
  294. if (FAILED(r))
  295. {
  296. throw std::runtime_error("switch betweem fronat and back buffers failed!");
  297. }
  298. } // try
  299. catch (const cv::Exception& e)
  300. {
  301. std::cerr << "Exception: " << e.what() << std::endl;
  302. cleanup();
  303. return 10;
  304. }
  305. catch (const std::exception& e)
  306. {
  307. std::cerr << "Exception: " << e.what() << std::endl;
  308. cleanup();
  309. return 11;
  310. }
  311. return EXIT_SUCCESS;
  312. } // render()
  313. int cleanup(void)
  314. {
  315. SAFE_RELEASE(m_pSurfaceRGBA);
  316. SAFE_RELEASE(m_pSurfaceNV12);
  317. SAFE_RELEASE(m_pSurfaceNV12_cpu_copy);
  318. SAFE_RELEASE(m_pBackBuffer);
  319. SAFE_RELEASE(m_pD3D11SwapChain);
  320. SAFE_RELEASE(m_pRenderTarget);
  321. SAFE_RELEASE(m_pD3D11Dev);
  322. SAFE_RELEASE(m_pD3D11Ctx);
  323. D3DSample::cleanup();
  324. return EXIT_SUCCESS;
  325. } // cleanup()
  326. protected:
  327. void convert_I420_to_NV12(cv::Mat& i420, cv::Mat& nv12, int width, int height)
  328. {
  329. nv12.create(i420.rows, i420.cols, CV_8UC1);
  330. unsigned char* pSrcY = i420.data;
  331. unsigned char* pDstY = nv12.data;
  332. size_t srcStep = i420.step[0];
  333. size_t dstStep = nv12.step[0];
  334. {
  335. unsigned char* src;
  336. unsigned char* dst;
  337. // copy Y plane
  338. for (int i = 0; i < height; i++)
  339. {
  340. src = pSrcY + i*srcStep;
  341. dst = pDstY + i*dstStep;
  342. for (int j = 0; j < width; j++)
  343. {
  344. dst[j] = src[j];
  345. }
  346. }
  347. }
  348. {
  349. // copy U/V planes to UV plane
  350. unsigned char* pSrcU;
  351. unsigned char* pSrcV;
  352. unsigned char* pDstUV;
  353. size_t uv_offset = height * dstStep;
  354. for (int i = 0; i < height / 2; i++)
  355. {
  356. pSrcU = pSrcY + height*width + i*(width / 2);
  357. pSrcV = pSrcY + height*width + (height / 2) * (width / 2) + i*(width / 2);
  358. pDstUV = pDstY + uv_offset + i*dstStep;
  359. for (int j = 0; j < width / 2; j++)
  360. {
  361. pDstUV[j*2 + 0] = pSrcU[j];
  362. pDstUV[j*2 + 1] = pSrcV[j];
  363. }
  364. }
  365. }
  366. return;
  367. }
  368. private:
  369. ID3D11Device* m_pD3D11Dev;
  370. IDXGISwapChain* m_pD3D11SwapChain;
  371. ID3D11DeviceContext* m_pD3D11Ctx;
  372. ID3D11Texture2D* m_pBackBuffer;
  373. ID3D11Texture2D* m_pSurfaceRGBA;
  374. ID3D11Texture2D* m_pSurfaceNV12;
  375. ID3D11Texture2D* m_pSurfaceNV12_cpu_copy;
  376. ID3D11RenderTargetView* m_pRenderTarget;
  377. cv::ocl::Context m_oclCtx;
  378. cv::String m_oclPlatformName;
  379. cv::String m_oclDevName;
  380. bool m_nv12_available;
  381. cv::Mat m_frame_i420;
  382. cv::Mat m_frame_nv12;
  383. };
  384. // main func
  385. int main(int argc, char** argv)
  386. {
  387. std::string title = "D3D11 interop sample";
  388. return d3d_app<D3D11WinApp>(argc, argv, title);
  389. }