|
|
|
@ -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); |
|
|
|
|
|
|
|
|
|