d3d10_interop.cpp 10.0 KB

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