diff --git a/samples/directx/d3d10_interop.cpp b/samples/directx/d3d10_interop.cpp index d308797c0..a4409c2ff 100644 --- a/samples/directx/d3d10_interop.cpp +++ b/samples/directx/d3d10_interop.cpp @@ -177,10 +177,6 @@ public: switch (m_mode) { - case MODE_NOP: - // no processing - break; - case MODE_CPU: { // process video frame on CPU @@ -195,7 +191,7 @@ public: cv::Mat m(m_height, m_width, CV_8UC4, mappedTex.pData, (int)mappedTex.RowPitch); - if (!m_disableProcessing) + if (m_demo_processing) { // blur D3D10 surface with OpenCV on CPU cv::blur(m, m, cv::Size(15, 15), cv::Point(-7, -7)); @@ -213,9 +209,9 @@ public: cv::directx::convertFromD3D10Texture2D(pSurface, u); - if (!m_disableProcessing) + if (m_demo_processing) { - // blur D3D9 surface with OpenCV on GPU with OpenCL + // blur D3D10 surface with OpenCV on GPU with OpenCL cv::blur(u, u, cv::Size(15, 15), cv::Point(-7, -7)); } @@ -266,13 +262,15 @@ public: cv::Mat m(m_height, m_width, CV_8UC4, mappedTex.pData, (int)mappedTex.RowPitch); - cv::String strMode = cv::format("%s", m_modeStr[mode].c_str()); - cv::String strFPS = cv::format("%2.1f", fps); - cv::String strDevName = cv::format("%s", oclDevName.c_str()); + cv::String strMode = cv::format("%s", m_modeStr[mode].c_str()); + cv::String strProcessing = m_demo_processing ? "blur frame" : "copy frame"; + cv::String strFPS = cv::format("%2.1f", fps); + cv::String strDevName = cv::format("%s", oclDevName.c_str()); cv::putText(m, strMode, cv::Point(0, 16), 1, 0.8, cv::Scalar(0, 0, 0)); - cv::putText(m, strFPS, cv::Point(0, 32), 1, 0.8, cv::Scalar(0, 0, 0)); - cv::putText(m, strDevName, cv::Point(0, 48), 1, 0.8, cv::Scalar(0, 0, 0)); + cv::putText(m, strProcessing, cv::Point(0, 32), 1, 0.8, cv::Scalar(0, 0, 0)); + cv::putText(m, strFPS, cv::Point(0, 48), 1, 0.8, cv::Scalar(0, 0, 0)); + cv::putText(m, strDevName, cv::Point(0, 64), 1, 0.8, cv::Scalar(0, 0, 0)); m_pSurface->Unmap(subResource); diff --git a/samples/directx/d3d11_interop.cpp b/samples/directx/d3d11_interop.cpp index 5e8bc9090..d44ea2178 100644 --- a/samples/directx/d3d11_interop.cpp +++ b/samples/directx/d3d11_interop.cpp @@ -71,19 +71,19 @@ public: &m_pD3D11Ctx); if (FAILED(r)) { - return -1; + throw std::runtime_error("D3D11CreateDeviceAndSwapChain() failed!"); } r = m_pD3D11SwapChain->GetBuffer(0, __uuidof(ID3D11Texture2D), (LPVOID*)&m_pBackBuffer); if (FAILED(r)) { - return -1; + throw std::runtime_error("GetBufer() failed!"); } r = m_pD3D11Dev->CreateRenderTargetView(m_pBackBuffer, NULL, &m_pRenderTarget); if (FAILED(r)) { - return -1; + throw std::runtime_error("CreateRenderTargetView() failed!"); } m_pD3D11Ctx->OMSetRenderTargets(1, &m_pRenderTarget, NULL); @@ -113,8 +113,7 @@ public: r = m_pD3D11Dev->CreateTexture2D(&desc, NULL, &m_pSurface); if (FAILED(r)) { - std::cerr << "Can't create texture with input image" << std::endl; - return -1; + throw std::runtime_error("Can't create texture with input image"); } // initialize OpenCL context of OpenCV lib from DirectX @@ -137,7 +136,7 @@ public: HRESULT r; if (!m_cap.read(m_frame_bgr)) - return -1; + throw std::runtime_error("Can't get frame"); cv::cvtColor(m_frame_bgr, m_frame_rgba, CV_RGB2BGRA); @@ -147,7 +146,7 @@ public: r = m_pD3D11Ctx->Map(m_pSurface, subResource, D3D11_MAP_WRITE_DISCARD, 0, &mappedTex); if (FAILED(r)) { - return r; + throw std::runtime_error("surface mapping failed!"); } cv::Mat m(m_height, m_width, CV_8UC4, mappedTex.pData, (int)mappedTex.RowPitch); @@ -176,15 +175,11 @@ public: r = get_surface(&pSurface); if (FAILED(r)) { - return -1; + throw std::runtime_error("get_surface() failed!"); } switch (m_mode) { - case MODE_NOP: - // no processing - break; - case MODE_CPU: { // process video frame on CPU @@ -194,14 +189,14 @@ public: r = m_pD3D11Ctx->Map(m_pSurface, subResource, D3D11_MAP_WRITE_DISCARD, 0, &mappedTex); if (FAILED(r)) { - return r; + throw std::runtime_error("surface mapping failed!"); } cv::Mat m(m_height, m_width, CV_8UC4, mappedTex.pData, (int)mappedTex.RowPitch); - if (!m_disableProcessing) + if (m_demo_processing) { - // blur D3D10 surface with OpenCV on CPU + // blur data from D3D11 surface with OpenCV on CPU cv::blur(m, m, cv::Size(15, 15), cv::Point(-7, -7)); } @@ -217,9 +212,9 @@ public: cv::directx::convertFromD3D11Texture2D(pSurface, u); - if (!m_disableProcessing) + if (m_demo_processing) { - // blur D3D9 surface with OpenCV on GPU with OpenCL + // blur data from D3D11 surface with OpenCV on GPU with OpenCL cv::blur(u, u, cv::Size(15, 15), cv::Point(-7, -7)); } @@ -241,7 +236,7 @@ public: r = m_pD3D11SwapChain->Present(0, 0); if (FAILED(r)) { - return -1; + throw std::runtime_error("switch betweem fronat and back buffers failed!"); } } // try @@ -251,6 +246,12 @@ public: return 10; } + catch (const std::exception& e) + { + std::cerr << "Exception: " << e.what() << std::endl; + return 11; + } + return 0; } // render() @@ -265,18 +266,20 @@ public: r = m_pD3D11Ctx->Map(pSurface, subResource, D3D11_MAP_WRITE_DISCARD, 0, &mappedTex); if (FAILED(r)) { - return; + throw std::runtime_error("surface mapping failed!"); } cv::Mat m(m_height, m_width, CV_8UC4, mappedTex.pData, (int)mappedTex.RowPitch); - cv::String strMode = cv::format("%s", m_modeStr[mode].c_str()); - cv::String strFPS = cv::format("%2.1f", fps); - cv::String strDevName = cv::format("%s", oclDevName.c_str()); + cv::String strMode = cv::format("%s", m_modeStr[mode].c_str()); + cv::String strProcessing = m_demo_processing ? "blur frame" : "copy frame"; + cv::String strFPS = cv::format("%2.1f", fps); + cv::String strDevName = cv::format("%s", oclDevName.c_str()); cv::putText(m, strMode, cv::Point(0, 16), 1, 0.8, cv::Scalar(0, 0, 0)); - cv::putText(m, strFPS, cv::Point(0, 32), 1, 0.8, cv::Scalar(0, 0, 0)); - cv::putText(m, strDevName, cv::Point(0, 48), 1, 0.8, cv::Scalar(0, 0, 0)); + cv::putText(m, strProcessing, cv::Point(0, 32), 1, 0.8, cv::Scalar(0, 0, 0)); + cv::putText(m, strFPS, cv::Point(0, 48), 1, 0.8, cv::Scalar(0, 0, 0)); + cv::putText(m, strDevName, cv::Point(0, 64), 1, 0.8, cv::Scalar(0, 0, 0)); m_pD3D11Ctx->Unmap(pSurface, subResource); diff --git a/samples/directx/d3d9_interop.cpp b/samples/directx/d3d9_interop.cpp index 851bd7d52..599807083 100644 --- a/samples/directx/d3d9_interop.cpp +++ b/samples/directx/d3d9_interop.cpp @@ -154,10 +154,6 @@ public: switch (m_mode) { - case MODE_NOP: - // no processing - break; - case MODE_CPU: { // process video frame on CPU @@ -172,7 +168,7 @@ public: cv::Mat m(m_height, m_width, CV_8UC4, memDesc.pBits, memDesc.Pitch); - if (!m_disableProcessing) + if (m_demo_processing) { // blur D3D9 surface with OpenCV on CPU cv::blur(m, m, cv::Size(15, 15), cv::Point(-7, -7)); @@ -194,7 +190,7 @@ public: cv::directx::convertFromDirect3DSurface9(pSurface, u); - if (!m_disableProcessing) + if (m_demo_processing) { // blur D3D9 surface with OpenCV on GPU with OpenCL cv::blur(u, u, cv::Size(15, 15), cv::Point(-7, -7)); @@ -261,6 +257,11 @@ public: sprintf(buf, "Mode: %s", m_modeStr[mode].c_str()); ::TextOut(hDC, 0, y, buf, (int)strlen(buf)); + y += tm.tmHeight; + buf[0] = 0; + sprintf(buf, m_demo_processing ? "blur frame" : "copy frame"); + ::TextOut(hDC, 0, y, buf, (int)strlen(buf)); + y += tm.tmHeight; buf[0] = 0; sprintf(buf, "FPS: %2.1f", fps); diff --git a/samples/directx/d3d9ex_interop.cpp b/samples/directx/d3d9ex_interop.cpp index d59416bf0..38bc703c4 100644 --- a/samples/directx/d3d9ex_interop.cpp +++ b/samples/directx/d3d9ex_interop.cpp @@ -154,10 +154,6 @@ public: switch (m_mode) { - case MODE_NOP: - // no processing - break; - case MODE_CPU: { // process video frame on CPU @@ -172,7 +168,7 @@ public: cv::Mat m(m_height, m_width, CV_8UC4, memDesc.pBits, memDesc.Pitch); - if (!m_disableProcessing) + if (m_demo_processing) { // blur D3D9 surface with OpenCV on CPU cv::blur(m, m, cv::Size(15, 15), cv::Point(-7, -7)); @@ -194,7 +190,7 @@ public: cv::directx::convertFromDirect3DSurface9(pSurface, u); - if (!m_disableProcessing) + if (m_demo_processing) { // blur D3D9 surface with OpenCV on GPU with OpenCL cv::blur(u, u, cv::Size(15, 15), cv::Point(-7, -7)); @@ -262,6 +258,11 @@ public: sprintf(buf, "Mode: %s", m_modeStr[mode].c_str()); ::TextOut(hDC, 0, y, buf, (int)strlen(buf)); + y += tm.tmHeight; + buf[0] = 0; + sprintf(buf, m_demo_processing ? "blur frame" : "copy frame"); + ::TextOut(hDC, 0, y, buf, (int)strlen(buf)); + y += tm.tmHeight; buf[0] = 0; sprintf(buf, "FPS: %2.1f", fps); diff --git a/samples/directx/d3dsample.hpp b/samples/directx/d3dsample.hpp index 8fc429a1e..f83adc97a 100644 --- a/samples/directx/d3dsample.hpp +++ b/samples/directx/d3dsample.hpp @@ -22,7 +22,6 @@ class D3DSample : public WinApp public: enum MODE { - MODE_NOP, MODE_CPU, MODE_GPU }; @@ -31,11 +30,10 @@ public: WinApp(width, height, window_name) { m_shutdown = false; - m_mode = MODE_NOP; - m_modeStr[0] = cv::String("No processing"); - m_modeStr[1] = cv::String("Processing on CPU"); - m_modeStr[2] = cv::String("Processing on GPU"); - m_disableProcessing = false; + m_mode = MODE_CPU; + m_modeStr[0] = cv::String("Processing on CPU"); + m_modeStr[1] = cv::String("Processing on GPU"); + m_demo_processing = false; m_cap = cap; } @@ -76,14 +74,19 @@ protected: switch (message) { case WM_CHAR: - if (wParam >= '0' && wParam <= '2') + if (wParam == '1') { - m_mode = static_cast((char)wParam - '0'); + m_mode = MODE_CPU; + return 0; + } + if (wParam == '2') + { + m_mode = MODE_GPU; return 0; } else if (wParam == VK_SPACE) { - m_disableProcessing = !m_disableProcessing; + m_demo_processing = !m_demo_processing; return 0; } else if (wParam == VK_ESCAPE) @@ -108,9 +111,9 @@ protected: protected: bool m_shutdown; - bool m_disableProcessing; + bool m_demo_processing; MODE m_mode; - cv::String m_modeStr[3]; + cv::String m_modeStr[2]; cv::VideoCapture m_cap; cv::Mat m_frame_bgr; cv::Mat m_frame_rgba; @@ -122,10 +125,10 @@ static void help() printf( "\nSample demonstrating interoperability of DirectX and OpenCL with OpenCV.\n" "Hot keys: \n" - " 0 - no processing\n" - " 1 - blur DX surface on CPU through OpenCV\n" - " 2 - blur DX surface on GPU through OpenCV using OpenCL\n" - " ESC - exit\n\n"); + " SPACE - turn processing on/off\n" + " 1 - process DX surface through OpenCV on CPU\n" + " 2 - process DX surface through OpenCV on GPU (via OpenCL)\n" + " ESC - exit\n\n"); }