d3d10_interop.cpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  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 <d3d10.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. class D3D10WinApp : public D3DSample
  17. {
  18. public:
  19. D3D10WinApp(int width, int height, std::string& window_name, cv::VideoCapture& cap) :
  20. D3DSample(width, height, window_name, cap) {}
  21. ~D3D10WinApp() {}
  22. int create(void)
  23. {
  24. // base initialization
  25. D3DSample::create();
  26. // initialize DirectX
  27. HRESULT r;
  28. DXGI_SWAP_CHAIN_DESC scd;
  29. ZeroMemory(&scd, sizeof(DXGI_SWAP_CHAIN_DESC));
  30. scd.BufferCount = 1; // one back buffer
  31. scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; // use 32-bit color
  32. scd.BufferDesc.Width = m_width; // set the back buffer width
  33. scd.BufferDesc.Height = m_height; // set the back buffer height
  34. scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT; // how swap chain is to be used
  35. scd.OutputWindow = m_hWnd; // the window to be used
  36. scd.SampleDesc.Count = 1; // how many multisamples
  37. scd.Windowed = TRUE; // windowed/full-screen mode
  38. scd.SwapEffect = DXGI_SWAP_EFFECT_DISCARD;
  39. scd.Flags = DXGI_SWAP_CHAIN_FLAG_ALLOW_MODE_SWITCH; // allow full-screen switching
  40. r = ::D3D10CreateDeviceAndSwapChain(
  41. NULL,
  42. D3D10_DRIVER_TYPE_HARDWARE,
  43. NULL,
  44. 0,
  45. D3D10_SDK_VERSION,
  46. &scd,
  47. &m_pD3D10SwapChain,
  48. &m_pD3D10Dev);
  49. if (FAILED(r))
  50. {
  51. return EXIT_FAILURE;
  52. }
  53. r = m_pD3D10SwapChain->GetBuffer(0, __uuidof(ID3D10Texture2D), (LPVOID*)&m_pBackBuffer);
  54. if (FAILED(r))
  55. {
  56. return EXIT_FAILURE;
  57. }
  58. r = m_pD3D10Dev->CreateRenderTargetView(m_pBackBuffer, NULL, &m_pRenderTarget);
  59. if (FAILED(r))
  60. {
  61. return EXIT_FAILURE;
  62. }
  63. m_pD3D10Dev->OMSetRenderTargets(1, &m_pRenderTarget, NULL);
  64. D3D10_VIEWPORT viewport;
  65. ZeroMemory(&viewport, sizeof(D3D10_VIEWPORT));
  66. viewport.Width = m_width;
  67. viewport.Height = m_height;
  68. viewport.MinDepth = 0.0f;
  69. viewport.MaxDepth = 0.0f;
  70. m_pD3D10Dev->RSSetViewports(1, &viewport);
  71. D3D10_TEXTURE2D_DESC desc = { 0 };
  72. desc.Width = m_width;
  73. desc.Height = m_height;
  74. desc.MipLevels = 1;
  75. desc.ArraySize = 1;
  76. desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
  77. desc.SampleDesc.Count = 1;
  78. desc.BindFlags = D3D10_BIND_SHADER_RESOURCE;
  79. desc.Usage = D3D10_USAGE_DYNAMIC;
  80. desc.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE;
  81. r = m_pD3D10Dev->CreateTexture2D(&desc, NULL, &m_pSurface);
  82. if (FAILED(r))
  83. {
  84. std::cerr << "Can't create texture with input image" << std::endl;
  85. return EXIT_FAILURE;
  86. }
  87. // initialize OpenCL context of OpenCV lib from DirectX
  88. if (cv::ocl::haveOpenCL())
  89. {
  90. m_oclCtx = cv::directx::ocl::initializeContextFromD3D10Device(m_pD3D10Dev);
  91. }
  92. m_oclDevName = cv::ocl::useOpenCL() ?
  93. cv::ocl::Context::getDefault().device(0).name() :
  94. "No OpenCL device";
  95. return EXIT_SUCCESS;
  96. } // create()
  97. // get media data on DX surface for further processing
  98. int get_surface(ID3D10Texture2D** ppSurface)
  99. {
  100. HRESULT r;
  101. if (!m_cap.read(m_frame_bgr))
  102. return EXIT_FAILURE;
  103. cv::cvtColor(m_frame_bgr, m_frame_rgba, cv::COLOR_BGR2RGBA);
  104. UINT subResource = ::D3D10CalcSubresource(0, 0, 1);
  105. D3D10_MAPPED_TEXTURE2D mappedTex;
  106. r = m_pSurface->Map(subResource, D3D10_MAP_WRITE_DISCARD, 0, &mappedTex);
  107. if (FAILED(r))
  108. {
  109. return r;
  110. }
  111. cv::Mat m(m_height, m_width, CV_8UC4, mappedTex.pData, (int)mappedTex.RowPitch);
  112. // copy video frame data to surface
  113. m_frame_rgba.copyTo(m);
  114. m_pSurface->Unmap(subResource);
  115. *ppSurface = m_pSurface;
  116. return EXIT_SUCCESS;
  117. } // get_surface()
  118. // process and render media data
  119. int render()
  120. {
  121. try
  122. {
  123. if (m_shutdown)
  124. return EXIT_SUCCESS;
  125. // capture user input once
  126. MODE mode = (m_mode == MODE_GPU_NV12) ? MODE_GPU_RGBA : m_mode;
  127. HRESULT r;
  128. ID3D10Texture2D* pSurface;
  129. r = get_surface(&pSurface);
  130. if (FAILED(r))
  131. {
  132. return EXIT_FAILURE;
  133. }
  134. m_timer.reset();
  135. m_timer.start();
  136. switch (mode)
  137. {
  138. case MODE_CPU:
  139. {
  140. // process video frame on CPU
  141. UINT subResource = ::D3D10CalcSubresource(0, 0, 1);
  142. D3D10_MAPPED_TEXTURE2D mappedTex;
  143. r = pSurface->Map(subResource, D3D10_MAP_WRITE_DISCARD, 0, &mappedTex);
  144. if (FAILED(r))
  145. {
  146. return r;
  147. }
  148. cv::Mat m(m_height, m_width, CV_8UC4, mappedTex.pData, (int)mappedTex.RowPitch);
  149. if (m_demo_processing)
  150. {
  151. // blur D3D10 surface with OpenCV on CPU
  152. cv::blur(m, m, cv::Size(15, 15));
  153. }
  154. m_timer.stop();
  155. cv::String strMode = cv::format("mode: %s", m_modeStr[MODE_CPU].c_str());
  156. cv::String strProcessing = m_demo_processing ? "blur frame" : "copy frame";
  157. cv::String strTime = cv::format("time: %4.3f msec", m_timer.getTimeMilli());
  158. cv::String strDevName = cv::format("OpenCL device: %s", m_oclDevName.c_str());
  159. cv::putText(m, strMode, cv::Point(0, 20), cv::FONT_HERSHEY_SIMPLEX, 0.8, cv::Scalar(0, 0, 200), 2);
  160. cv::putText(m, strProcessing, cv::Point(0, 40), cv::FONT_HERSHEY_SIMPLEX, 0.8, cv::Scalar(0, 0, 200), 2);
  161. cv::putText(m, strTime, cv::Point(0, 60), cv::FONT_HERSHEY_SIMPLEX, 0.8, cv::Scalar(0, 0, 200), 2);
  162. cv::putText(m, strDevName, cv::Point(0, 80), cv::FONT_HERSHEY_SIMPLEX, 0.8, cv::Scalar(0, 0, 200), 2);
  163. pSurface->Unmap(subResource);
  164. break;
  165. }
  166. case MODE_GPU_RGBA:
  167. {
  168. // process video frame on GPU
  169. cv::UMat u;
  170. cv::directx::convertFromD3D10Texture2D(pSurface, u);
  171. if (m_demo_processing)
  172. {
  173. // blur D3D10 surface with OpenCV on GPU with OpenCL
  174. cv::blur(u, u, cv::Size(15, 15));
  175. }
  176. m_timer.stop();
  177. cv::String strMode = cv::format("mode: %s", m_modeStr[MODE_GPU_RGBA].c_str());
  178. cv::String strProcessing = m_demo_processing ? "blur frame" : "copy frame";
  179. cv::String strTime = cv::format("time: %4.3f msec", m_timer.getTimeMilli());
  180. cv::String strDevName = cv::format("OpenCL device: %s", m_oclDevName.c_str());
  181. cv::putText(u, strMode, cv::Point(0, 20), cv::FONT_HERSHEY_SIMPLEX, 0.8, cv::Scalar(0, 0, 200), 2);
  182. cv::putText(u, strProcessing, cv::Point(0, 40), cv::FONT_HERSHEY_SIMPLEX, 0.8, cv::Scalar(0, 0, 200), 2);
  183. cv::putText(u, strTime, cv::Point(0, 60), cv::FONT_HERSHEY_SIMPLEX, 0.8, cv::Scalar(0, 0, 200), 2);
  184. cv::putText(u, strDevName, cv::Point(0, 80), cv::FONT_HERSHEY_SIMPLEX, 0.8, cv::Scalar(0, 0, 200), 2);
  185. cv::directx::convertToD3D10Texture2D(u, pSurface);
  186. break;
  187. }
  188. } // switch
  189. // traditional DX render pipeline:
  190. // BitBlt surface to backBuffer and flip backBuffer to frontBuffer
  191. m_pD3D10Dev->CopyResource(m_pBackBuffer, pSurface);
  192. // present the back buffer contents to the display
  193. // switch the back buffer and the front buffer
  194. r = m_pD3D10SwapChain->Present(0, 0);
  195. if (FAILED(r))
  196. {
  197. return EXIT_FAILURE;
  198. }
  199. } // try
  200. catch (const cv::Exception& e)
  201. {
  202. std::cerr << "Exception: " << e.what() << std::endl;
  203. return 10;
  204. }
  205. return EXIT_SUCCESS;
  206. } // render()
  207. int cleanup(void)
  208. {
  209. SAFE_RELEASE(m_pSurface);
  210. SAFE_RELEASE(m_pBackBuffer);
  211. SAFE_RELEASE(m_pD3D10SwapChain);
  212. SAFE_RELEASE(m_pRenderTarget);
  213. SAFE_RELEASE(m_pD3D10Dev);
  214. D3DSample::cleanup();
  215. return EXIT_SUCCESS;
  216. } // cleanup()
  217. private:
  218. ID3D10Device* m_pD3D10Dev;
  219. IDXGISwapChain* m_pD3D10SwapChain;
  220. ID3D10Texture2D* m_pBackBuffer;
  221. ID3D10Texture2D* m_pSurface;
  222. ID3D10RenderTargetView* m_pRenderTarget;
  223. cv::ocl::Context m_oclCtx;
  224. cv::String m_oclPlatformName;
  225. cv::String m_oclDevName;
  226. };
  227. // main func
  228. int main(int argc, char** argv)
  229. {
  230. std::string title = "D3D10 interop sample";
  231. return d3d_app<D3D10WinApp>(argc, argv, title);
  232. }