From dee05f982b9503f910fb36ed96de938bd0a377ac Mon Sep 17 00:00:00 2001 From: Vladimir Dudnik Date: Mon, 13 Jul 2015 23:40:45 +0300 Subject: [PATCH 1/2] compute fps changed (now only interop counts). fixed d3d10 and d3d11 print info method (there were issues with texture mapping) --- modules/core/include/opencv2/core/directx.hpp | 7 ++ samples/directx/d3d10_interop.cpp | 59 ++++++------- samples/directx/d3d11_interop.cpp | 83 +++++++++---------- samples/directx/d3d9_interop.cpp | 6 +- samples/directx/d3d9ex_interop.cpp | 6 +- samples/directx/d3dsample.hpp | 62 +++++++++----- 6 files changed, 121 insertions(+), 102 deletions(-) diff --git a/modules/core/include/opencv2/core/directx.hpp b/modules/core/include/opencv2/core/directx.hpp index 275df720d..bb6167511 100644 --- a/modules/core/include/opencv2/core/directx.hpp +++ b/modules/core/include/opencv2/core/directx.hpp @@ -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 diff --git a/samples/directx/d3d10_interop.cpp b/samples/directx/d3d10_interop.cpp index a4409c2ff..d66417a16 100644 --- a/samples/directx/d3d10_interop.cpp +++ b/samples/directx/d3d10_interop.cpp @@ -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("%s", m_modeStr[MODE_CPU].c_str()); + cv::String strProcessing = m_demo_processing ? "blur frame" : "copy frame"; + cv::String strFPS = cv::format("%2.1f", m_timer.fps()); + cv::String strDevName = cv::format("%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, 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)); + + 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("%s", m_modeStr[MODE_GPU].c_str()); + cv::String strProcessing = m_demo_processing ? "blur frame" : "copy frame"; + cv::String strFPS = cv::format("%2.1f", m_timer.fps()); + cv::String strDevName = cv::format("%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, strFPS, 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); diff --git a/samples/directx/d3d11_interop.cpp b/samples/directx/d3d11_interop.cpp index d44ea2178..0972970cf 100644 --- a/samples/directx/d3d11_interop.cpp +++ b/samples/directx/d3d11_interop.cpp @@ -98,17 +98,19 @@ public: m_pD3D11Ctx->RSSetViewports(1, &viewport); - D3D11_TEXTURE2D_DESC desc = { 0 }; + D3D11_TEXTURE2D_DESC desc; - desc.Width = m_width; - desc.Height = m_height; - desc.MipLevels = 1; - desc.ArraySize = 1; - desc.Format = DXGI_FORMAT_R8G8B8A8_UNORM; - desc.SampleDesc.Count = 1; - desc.BindFlags = D3D11_BIND_SHADER_RESOURCE; - desc.Usage = D3D11_USAGE_DYNAMIC; - desc.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE; + desc.Width = m_width; + desc.Height = m_height; + desc.MipLevels = 1; + 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("%s", m_modeStr[MODE_CPU].c_str()); + cv::String strProcessing = m_demo_processing ? "blur frame" : "copy frame"; + cv::String strFPS = cv::format("%2.1f", m_timer.fps()); + cv::String strDevName = cv::format("%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, 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); break; } @@ -218,6 +232,16 @@ public: cv::blur(u, u, cv::Size(15, 15), cv::Point(-7, -7)); } + cv::String strMode = cv::format("%s", m_modeStr[MODE_GPU].c_str()); + cv::String strProcessing = m_demo_processing ? "blur frame" : "copy frame"; + cv::String strFPS = cv::format("%2.1f", m_timer.fps()); + cv::String strDevName = cv::format("%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, strFPS, 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); diff --git a/samples/directx/d3d9_interop.cpp b/samples/directx/d3d9_interop.cpp index 599807083..aa5c3ccd1 100644 --- a/samples/directx/d3d9_interop.cpp +++ b/samples/directx/d3d9_interop.cpp @@ -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.fps(), m_oclDevName); // traditional DX render pipeline: // BitBlt surface to backBuffer and flip backBuffer to frontBuffer diff --git a/samples/directx/d3d9ex_interop.cpp b/samples/directx/d3d9ex_interop.cpp index 38bc703c4..5b5565eb3 100644 --- a/samples/directx/d3d9ex_interop.cpp +++ b/samples/directx/d3d9ex_interop.cpp @@ -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.fps(), m_oclDevName); // traditional DX render pipeline: // BitBlt surface to backBuffer and flip backBuffer to frontBuffer diff --git a/samples/directx/d3dsample.hpp b/samples/directx/d3dsample.hpp index f83adc97a..ddfed2e55 100644 --- a/samples/directx/d3dsample.hpp +++ b/samples/directx/d3dsample.hpp @@ -17,6 +17,46 @@ #define SAFE_RELEASE(p) if (p) { p->Release(); p = NULL; } +class Timer +{ +public: + Timer() : m_t0(0), m_t1(0) + { + m_tick_frequency = (float)cv::getTickFrequency(); + } + + void start() + { + m_t0 = cv::getTickCount(); + time_queue.push(m_t0); + } + + void stop() + { + if (time_queue.size() > 1) + m_t1 = time_queue.front(); + + if (time_queue.size() >= 25) + time_queue.pop(); + } + + float fps() + { + size_t sz = time_queue.size(); + + float fps = sz * m_tick_frequency / (m_t0 - m_t1); + + return fps; + } + +public: + float m_tick_frequency; + int64 m_t0; + int64 m_t1; + std::queue time_queue; +}; + + class D3DSample : public WinApp { public: @@ -47,27 +87,6 @@ public: return WinApp::cleanup(); } - static float getFps() - { - static std::queue 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 +136,7 @@ protected: cv::VideoCapture m_cap; cv::Mat m_frame_bgr; cv::Mat m_frame_rgba; + Timer m_timer; }; From 6c452addfeb88b8a11a9e8a3c429e3b084548358 Mon Sep 17 00:00:00 2001 From: Vladimir Dudnik Date: Mon, 20 Jul 2015 19:05:05 +0300 Subject: [PATCH 2/2] changed output from fps to time (in msec). Notes for dx10 and dx11: the first measure is wrong (zero), the followiing are correct; measured time includes output text to surface. --- samples/directx/d3d10_interop.cpp | 16 +++++++-------- samples/directx/d3d11_interop.cpp | 16 +++++++-------- samples/directx/d3d9_interop.cpp | 8 ++++---- samples/directx/d3d9ex_interop.cpp | 8 ++++---- samples/directx/d3dsample.hpp | 32 +++++++++++++++++------------- 5 files changed, 42 insertions(+), 38 deletions(-) diff --git a/samples/directx/d3d10_interop.cpp b/samples/directx/d3d10_interop.cpp index d66417a16..2869e6b86 100644 --- a/samples/directx/d3d10_interop.cpp +++ b/samples/directx/d3d10_interop.cpp @@ -199,14 +199,14 @@ public: cv::blur(m, m, cv::Size(15, 15), cv::Point(-7, -7)); } - cv::String strMode = cv::format("%s", m_modeStr[MODE_CPU].c_str()); + 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 strFPS = cv::format("%2.1f", m_timer.fps()); - cv::String strDevName = cv::format("%s", m_oclDevName.c_str()); + 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, strFPS, cv::Point(0, 48), 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); @@ -227,14 +227,14 @@ public: cv::blur(u, u, cv::Size(15, 15), cv::Point(-7, -7)); } - cv::String strMode = cv::format("%s", m_modeStr[MODE_GPU].c_str()); + 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 strFPS = cv::format("%2.1f", m_timer.fps()); - cv::String strDevName = cv::format("%s", m_oclDevName.c_str()); + 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, strFPS, cv::Point(0, 48), 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); diff --git a/samples/directx/d3d11_interop.cpp b/samples/directx/d3d11_interop.cpp index 0972970cf..3ac2b063f 100644 --- a/samples/directx/d3d11_interop.cpp +++ b/samples/directx/d3d11_interop.cpp @@ -204,14 +204,14 @@ public: cv::blur(m, m, cv::Size(15, 15), cv::Point(-7, -7)); } - cv::String strMode = cv::format("%s", m_modeStr[MODE_CPU].c_str()); + 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 strFPS = cv::format("%2.1f", m_timer.fps()); - cv::String strDevName = cv::format("%s", m_oclDevName.c_str()); + 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, strFPS, cv::Point(0, 48), 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); @@ -232,14 +232,14 @@ public: cv::blur(u, u, cv::Size(15, 15), cv::Point(-7, -7)); } - cv::String strMode = cv::format("%s", m_modeStr[MODE_GPU].c_str()); + 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 strFPS = cv::format("%2.1f", m_timer.fps()); - cv::String strDevName = cv::format("%s", m_oclDevName.c_str()); + 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, strFPS, cv::Point(0, 48), 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); diff --git a/samples/directx/d3d9_interop.cpp b/samples/directx/d3d9_interop.cpp index aa5c3ccd1..afe12b3de 100644 --- a/samples/directx/d3d9_interop.cpp +++ b/samples/directx/d3d9_interop.cpp @@ -207,7 +207,7 @@ public: m_timer.stop(); - print_info(pSurface, m_mode, m_timer.fps(), m_oclDevName); + 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 @@ -235,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; @@ -258,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; @@ -268,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; diff --git a/samples/directx/d3d9ex_interop.cpp b/samples/directx/d3d9ex_interop.cpp index 5b5565eb3..187177061 100644 --- a/samples/directx/d3d9ex_interop.cpp +++ b/samples/directx/d3d9ex_interop.cpp @@ -207,7 +207,7 @@ public: m_timer.stop(); - print_info(pSurface, m_mode, m_timer.fps(), m_oclDevName); + 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 @@ -236,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; @@ -259,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; @@ -269,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; diff --git a/samples/directx/d3dsample.hpp b/samples/directx/d3dsample.hpp index ddfed2e55..4b7545fc6 100644 --- a/samples/directx/d3dsample.hpp +++ b/samples/directx/d3dsample.hpp @@ -20,40 +20,44 @@ class Timer { public: - Timer() : m_t0(0), m_t1(0) + 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(); - time_queue.push(m_t0); } void stop() { - if (time_queue.size() > 1) - m_t1 = time_queue.front(); - - if (time_queue.size() >= 25) - time_queue.pop(); + m_diff = cv::getTickCount() - m_t0; } - float fps() + float time(UNITS u = UNITS::MSEC) { - size_t sz = time_queue.size(); + float sec = m_diff / m_tick_frequency; - float fps = sz * m_tick_frequency / (m_t0 - m_t1); - - return fps; + return sec * m_unit_mul[u]; } public: float m_tick_frequency; int64 m_t0; - int64 m_t1; - std::queue time_queue; + int64 m_diff; + int m_unit_mul[3]; };