compute fps changed (now only interop counts). fixed d3d10 and d3d11 print info metho...
authorVladimir Dudnik <vladimir.dudnik@itseez.com>
Mon, 13 Jul 2015 20:40:45 +0000 (23:40 +0300)
committerVladimir Dudnik <vladimir.dudnik@itseez.com>
Mon, 13 Jul 2015 20:40:45 +0000 (23:40 +0300)
modules/core/include/opencv2/core/directx.hpp
samples/directx/d3d10_interop.cpp
samples/directx/d3d11_interop.cpp
samples/directx/d3d9_interop.cpp
samples/directx/d3d9ex_interop.cpp
samples/directx/d3dsample.hpp

index 275df72..bb61675 100644 (file)
@@ -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
index a4409c2..d66417a 100644 (file)
@@ -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);
index d44ea21..0972970 100644 (file)
@@ -98,17 +98,19 @@ public:
 
         m_pD3D11Ctx->RSSetViewports(1, &viewport);
 
-        D3D11_TEXTURE2D_DESC desc = { 0 };
-
-        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;
+        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.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);
index 5998070..aa5c3cc 100644 (file)
@@ -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
index 38bc703..5b5565e 100644 (file)
@@ -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
index f83adc9..ddfed2e 100644 (file)
 #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<int64> time_queue;
+};
+
+
 class D3DSample : public WinApp
 {
 public:
@@ -47,27 +87,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 +136,7 @@ protected:
     cv::VideoCapture   m_cap;
     cv::Mat            m_frame_bgr;
     cv::Mat            m_frame_rgba;
+    Timer              m_timer;
 };