Commit 03824682 authored by Vadim Pisarevsky's avatar Vadim Pisarevsky

Merge pull request #4189 from vladimir-dudnik:update_dx_samples

parents a7805301 6c452add
......@@ -68,6 +68,13 @@ namespace ocl {
using namespace cv::ocl;
//! @addtogroup core_directx
// This section describes OpenCL and DirectX interoperability.
//
// To enable DirectX support, configure OpenCV using CMake with WITH_DIRECTX=ON . Note, DirectX is
// supported only on Windows.
//
// To use OpenCL functionality you should first initialize OpenCL context from DirectX resource.
//
//! @{
// TODO static functions in the Context class
......
......@@ -175,6 +175,8 @@ public:
return -1;
}
m_timer.start();
switch (m_mode)
{
case MODE_CPU:
......@@ -183,7 +185,7 @@ public:
UINT subResource = ::D3D10CalcSubresource(0, 0, 1);
D3D10_MAPPED_TEXTURE2D mappedTex;
r = m_pSurface->Map(subResource, D3D10_MAP_WRITE_DISCARD, 0, &mappedTex);
r = pSurface->Map(subResource, D3D10_MAP_WRITE_DISCARD, 0, &mappedTex);
if (FAILED(r))
{
return r;
......@@ -197,7 +199,17 @@ public:
cv::blur(m, m, cv::Size(15, 15), cv::Point(-7, -7));
}
m_pSurface->Unmap(subResource);
cv::String strMode = cv::format("mode: %s", m_modeStr[MODE_CPU].c_str());
cv::String strProcessing = m_demo_processing ? "blur frame" : "copy frame";
cv::String strTime = cv::format("time: %4.1f msec", m_timer.time(Timer::UNITS::MSEC));
cv::String strDevName = cv::format("OpenCL device: %s", m_oclDevName.c_str());
cv::putText(m, strMode, cv::Point(0, 16), 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, strTime, 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));
pSurface->Unmap(subResource);
break;
}
......@@ -215,6 +227,16 @@ public:
cv::blur(u, u, cv::Size(15, 15), cv::Point(-7, -7));
}
cv::String strMode = cv::format("mode: %s", m_modeStr[MODE_GPU].c_str());
cv::String strProcessing = m_demo_processing ? "blur frame" : "copy frame";
cv::String strTime = cv::format("time: %4.1f msec", m_timer.time(Timer::UNITS::MSEC));
cv::String strDevName = cv::format("OpenCL device: %s", m_oclDevName.c_str());
cv::putText(u, strMode, cv::Point(0, 16), 1, 0.8, cv::Scalar(0, 0, 0));
cv::putText(u, strProcessing, cv::Point(0, 32), 1, 0.8, cv::Scalar(0, 0, 0));
cv::putText(u, strTime, cv::Point(0, 48), 1, 0.8, cv::Scalar(0, 0, 0));
cv::putText(u, strDevName, cv::Point(0, 64), 1, 0.8, cv::Scalar(0, 0, 0));
cv::directx::convertToD3D10Texture2D(u, pSurface);
break;
......@@ -222,7 +244,7 @@ public:
} // switch
print_info(pSurface, m_mode, getFps(), m_oclDevName);
m_timer.stop();
// traditional DX render pipeline:
// BitBlt surface to backBuffer and flip backBuffer to frontBuffer
......@@ -247,37 +269,6 @@ public:
} // render()
void print_info(ID3D10Texture2D* pSurface, int mode, float fps, cv::String oclDevName)
{
HRESULT r;
UINT subResource = ::D3D10CalcSubresource(0, 0, 1);
D3D10_MAPPED_TEXTURE2D mappedTex;
r = pSurface->Map(subResource, D3D10_MAP_WRITE_DISCARD, 0, &mappedTex);
if (FAILED(r))
{
return;
}
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 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, 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);
return;
} // print_info()
int cleanup(void)
{
SAFE_RELEASE(m_pSurface);
......
......@@ -98,7 +98,7 @@ public:
m_pD3D11Ctx->RSSetViewports(1, &viewport);
D3D11_TEXTURE2D_DESC desc = { 0 };
D3D11_TEXTURE2D_DESC desc;
desc.Width = m_width;
desc.Height = m_height;
......@@ -106,9 +106,11 @@ public:
desc.ArraySize = 1;
desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
desc.SampleDesc.Count = 1;
desc.SampleDesc.Quality = 0;
desc.BindFlags = D3D11_BIND_SHADER_RESOURCE;
desc.Usage = D3D11_USAGE_DYNAMIC;
desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
desc.MiscFlags = 0;
r = m_pD3D11Dev->CreateTexture2D(&desc, NULL, &m_pSurface);
if (FAILED(r))
......@@ -170,7 +172,7 @@ public:
return 0;
HRESULT r;
ID3D11Texture2D* pSurface;
ID3D11Texture2D* pSurface = 0;
r = get_surface(&pSurface);
if (FAILED(r))
......@@ -178,6 +180,8 @@ public:
throw std::runtime_error("get_surface() failed!");
}
m_timer.start();
switch (m_mode)
{
case MODE_CPU:
......@@ -186,7 +190,7 @@ public:
UINT subResource = ::D3D11CalcSubresource(0, 0, 1);
D3D11_MAPPED_SUBRESOURCE mappedTex;
r = m_pD3D11Ctx->Map(m_pSurface, subResource, D3D11_MAP_WRITE_DISCARD, 0, &mappedTex);
r = m_pD3D11Ctx->Map(pSurface, subResource, D3D11_MAP_WRITE_DISCARD, 0, &mappedTex);
if (FAILED(r))
{
throw std::runtime_error("surface mapping failed!");
......@@ -200,7 +204,17 @@ public:
cv::blur(m, m, cv::Size(15, 15), cv::Point(-7, -7));
}
m_pD3D11Ctx->Unmap(m_pSurface, subResource);
cv::String strMode = cv::format("mode: %s", m_modeStr[MODE_CPU].c_str());
cv::String strProcessing = m_demo_processing ? "blur frame" : "copy frame";
cv::String strTime = cv::format("time: %4.1f msec", m_timer.time(Timer::UNITS::MSEC));
cv::String strDevName = cv::format("OpenCL device: %s", m_oclDevName.c_str());
cv::putText(m, strMode, cv::Point(0, 16), 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, strTime, 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);
break;
}
......@@ -218,6 +232,16 @@ public:
cv::blur(u, u, cv::Size(15, 15), cv::Point(-7, -7));
}
cv::String strMode = cv::format("mode: %s", m_modeStr[MODE_GPU].c_str());
cv::String strProcessing = m_demo_processing ? "blur frame" : "copy frame";
cv::String strTime = cv::format("time: %4.1f msec", m_timer.time(Timer::UNITS::MSEC));
cv::String strDevName = cv::format("OpenCL device: %s", m_oclDevName.c_str());
cv::putText(u, strMode, cv::Point(0, 16), 1, 0.8, cv::Scalar(0, 0, 0));
cv::putText(u, strProcessing, cv::Point(0, 32), 1, 0.8, cv::Scalar(0, 0, 0));
cv::putText(u, strTime, cv::Point(0, 48), 1, 0.8, cv::Scalar(0, 0, 0));
cv::putText(u, strDevName, cv::Point(0, 64), 1, 0.8, cv::Scalar(0, 0, 0));
cv::directx::convertToD3D11Texture2D(u, pSurface);
break;
......@@ -225,7 +249,7 @@ public:
} // switch
print_info(pSurface, m_mode, getFps(), m_oclDevName);
m_timer.stop();
// traditional DX render pipeline:
// BitBlt surface to backBuffer and flip backBuffer to frontBuffer
......@@ -256,37 +280,6 @@ public:
} // render()
void print_info(ID3D11Texture2D* pSurface, int mode, float fps, cv::String oclDevName)
{
HRESULT r;
UINT subResource = ::D3D11CalcSubresource(0, 0, 1);
D3D11_MAPPED_SUBRESOURCE mappedTex;
r = m_pD3D11Ctx->Map(pSurface, subResource, D3D11_MAP_WRITE_DISCARD, 0, &mappedTex);
if (FAILED(r))
{
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 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, 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);
return;
} // printf_info()
int cleanup(void)
{
SAFE_RELEASE(m_pSurface);
......
......@@ -152,6 +152,8 @@ public:
return -1;
}
m_timer.start();
switch (m_mode)
{
case MODE_CPU:
......@@ -203,7 +205,9 @@ public:
} // switch
print_info(pSurface, m_mode, getFps(), m_oclDevName);
m_timer.stop();
print_info(pSurface, m_mode, m_timer.time(Timer::UNITS::MSEC), m_oclDevName);
// traditional DX render pipeline:
// BitBlt surface to backBuffer and flip backBuffer to frontBuffer
......@@ -231,7 +235,7 @@ public:
} // render()
void print_info(LPDIRECT3DSURFACE9 pSurface, int mode, float fps, cv::String oclDevName)
void print_info(LPDIRECT3DSURFACE9 pSurface, int mode, float time, cv::String oclDevName)
{
HDC hDC;
......@@ -254,7 +258,7 @@ public:
int y = 0;
buf[0] = 0;
sprintf(buf, "Mode: %s", m_modeStr[mode].c_str());
sprintf(buf, "mode: %s", m_modeStr[mode].c_str());
::TextOut(hDC, 0, y, buf, (int)strlen(buf));
y += tm.tmHeight;
......@@ -264,7 +268,7 @@ public:
y += tm.tmHeight;
buf[0] = 0;
sprintf(buf, "FPS: %2.1f", fps);
sprintf(buf, "time: %4.1f msec", time);
::TextOut(hDC, 0, y, buf, (int)strlen(buf));
y += tm.tmHeight;
......
......@@ -152,6 +152,8 @@ public:
return -1;
}
m_timer.start();
switch (m_mode)
{
case MODE_CPU:
......@@ -203,7 +205,9 @@ public:
} // switch
print_info(pSurface, m_mode, getFps(), m_oclDevName);
m_timer.stop();
print_info(pSurface, m_mode, m_timer.time(Timer::UNITS::MSEC), m_oclDevName);
// traditional DX render pipeline:
// BitBlt surface to backBuffer and flip backBuffer to frontBuffer
......@@ -232,7 +236,7 @@ public:
} // render()
void print_info(LPDIRECT3DSURFACE9 pSurface, int mode, float fps, cv::String oclDevName)
void print_info(LPDIRECT3DSURFACE9 pSurface, int mode, float time, cv::String oclDevName)
{
HDC hDC;
......@@ -255,7 +259,7 @@ public:
int y = 0;
buf[0] = 0;
sprintf(buf, "Mode: %s", m_modeStr[mode].c_str());
sprintf(buf, "mode: %s", m_modeStr[mode].c_str());
::TextOut(hDC, 0, y, buf, (int)strlen(buf));
y += tm.tmHeight;
......@@ -265,7 +269,7 @@ public:
y += tm.tmHeight;
buf[0] = 0;
sprintf(buf, "FPS: %2.1f", fps);
sprintf(buf, "time: %4.1f msec", time);
::TextOut(hDC, 0, y, buf, (int)strlen(buf));
y += tm.tmHeight;
......
......@@ -17,6 +17,50 @@
#define SAFE_RELEASE(p) if (p) { p->Release(); p = NULL; }
class Timer
{
public:
enum UNITS
{
USEC = 0,
MSEC,
SEC
};
Timer() : m_t0(0), m_diff(0)
{
m_tick_frequency = (float)cv::getTickFrequency();
m_unit_mul[USEC] = 1000000;
m_unit_mul[MSEC] = 1000;
m_unit_mul[SEC] = 1;
}
void start()
{
m_t0 = cv::getTickCount();
}
void stop()
{
m_diff = cv::getTickCount() - m_t0;
}
float time(UNITS u = UNITS::MSEC)
{
float sec = m_diff / m_tick_frequency;
return sec * m_unit_mul[u];
}
public:
float m_tick_frequency;
int64 m_t0;
int64 m_diff;
int m_unit_mul[3];
};
class D3DSample : public WinApp
{
public:
......@@ -47,27 +91,6 @@ public:
return WinApp::cleanup();
}
static float getFps()
{
static std::queue<int64> time_queue;
int64 now = cv::getTickCount();
int64 then = 0;
time_queue.push(now);
if (time_queue.size() >= 2)
then = time_queue.front();
if (time_queue.size() >= 25)
time_queue.pop();
size_t sz = time_queue.size();
float fps = sz * (float)cv::getTickFrequency() / (now - then);
return fps;
}
protected:
virtual LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
......@@ -117,6 +140,7 @@ protected:
cv::VideoCapture m_cap;
cv::Mat m_frame_bgr;
cv::Mat m_frame_rgba;
Timer m_timer;
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment