Browse Source

refactoring debug stats

dmuratshin 9 years ago
parent
commit
36d2f4ee09

+ 26 - 17
oxygine/src/DebugActor.cpp

@@ -257,8 +257,6 @@ namespace oxygine
     {
     }
 
-    extern IVideoDriver::Stats _videoStats;
-
     std::string aligned(int v, int width)
     {
         char str[32];
@@ -274,16 +272,21 @@ namespace oxygine
 
     void DebugActor::doUpdate(const UpdateState& us)
     {
+    }
+
+    void DebugActor::render(RenderState const& parentRS)
+    {
+        timeMS tm = getTimeMS();
+
         static int fps = 0;
         ++_frames;
         if (_frames > 50)
         {
-            timeMS this_time = getTimeMS();
-            if (this_time != _startTime)
+            if (tm != _startTime)
             {
-                fps = int(((float)_frames / (this_time - _startTime)) * 1000);
+                fps = int(((float)_frames / (tm - _startTime)) * 1000);
             }
-            _startTime = this_time;
+            _startTime = tm;
             _frames = 0;
         }
 
@@ -302,17 +305,21 @@ namespace oxygine
         s << "mfree=" << mem_free << " mem=" << mem_used << std::endl;
 #endif
 
-
+        const IVideoDriver::Stats& vstats = IVideoDriver::_stats;
 
 #ifdef OXYGINE_DEBUG_TRACE_LEAKS
         s << "objects=" << (int)ObjectBase::__getCreatedObjects().size() << std::endl;
 #endif
-#ifdef OXYGINE_TRACE_VIDEO_STATS
-        s << "batches=" << aligned(_videoStats.batches, 3) << " triangles=" << aligned(_videoStats.triangles, 3) << std::endl;
+
+#if OXYGINE_TRACE_VIDEO_STATS
+        int primitives = 0;
+        primitives += vstats.elements[IVideoDriver::PT_TRIANGLES] / 3;
+        primitives += vstats.elements[IVideoDriver::PT_TRIANGLE_STRIP] - 2;
+        s << "batches=" << aligned(vstats.batches, 3) << " primitives=" << aligned(primitives, 3) << std::endl;
 #endif
 
         s << "update=" << aligned(getStage()->_statUpdate, 2) << "ms ";
-        s << "render=" << aligned(getStage()->_statRender, 2) << "ms ";
+        s << "render=" << aligned(vstats.duration, 2) << "ms ";
         s << "textures=" << aligned(NativeTexture::created, 2) << " ";
 
 #ifdef __APPLE__
@@ -320,7 +327,6 @@ namespace oxygine
         iosGetMemoryUsage(mem);
         s << "memory=" << mem / 1024 << "kb ";
 #endif
-        //s << "\nlisteners=" << getStage()->getListenersCount() << "";
 
         if (!_debugText.empty())
         {
@@ -371,19 +377,18 @@ namespace oxygine
 
         setPosition(pos);
         setScale(1.0f / getStage()->getScaleX());
-    }
 
-    void DebugActor::render(RenderState const& parentRS)
-    {
+
         RenderState rs = parentRS;
         parentRS.material->finish();
 
         STDRenderer renderer;
         STDMaterial mat(&renderer);
         mat.apply(0);
-        //STDRenderer* renderer = mat.getRenderer();
+
+
         IVideoDriver* driver = renderer.getDriver();
-        driver->setDebugStats(false);
+
 
         Rect vp(Point(0, 0), core::getDisplaySize());
         driver->setViewport(vp);
@@ -392,10 +397,14 @@ namespace oxygine
         rs.material = &mat;
         Actor::render(rs);
         renderer.drawBatch();
-        driver->setDebugStats(true);
+
         mat.finish();
 
         Material::setCurrent(0);
+
+        timeMS dur = getTimeMS() - tm;
+
+        IVideoDriver::_stats.start += dur;
     }
 
     void DebugActor::showTexel2PixelErrors(bool show)

+ 1 - 3
oxygine/src/Stage.cpp

@@ -14,7 +14,7 @@ namespace oxygine
 {
     spStage Stage::instance;
 
-    Stage::Stage(bool autoReset) : _statUpdate(0), _statRender(0), _clipOuter(false), _viewport(0, 0, 0, 0) //, _active(true)
+    Stage::Stage(bool autoReset) : _statUpdate(0), _clipOuter(false), _viewport(0, 0, 0, 0) //, _active(true)
     {
         spClock clock = new Clock();
         setClock(clock);
@@ -161,8 +161,6 @@ namespace oxygine
         Actor::render(rs);
 
         mat.finish();
-
-        _statRender = getTimeMS() - t;
     }
 
     void Stage::render(const Color& clearColor, const Rect& viewport)

+ 0 - 1
oxygine/src/Stage.h

@@ -69,7 +69,6 @@ namespace oxygine
     private:
 
         timeMS _statUpdate;
-        timeMS _statRender;
         bool    _clipOuter;
         Rect    _viewport;
 

+ 11 - 6
oxygine/src/core/VideoDriver.cpp

@@ -8,6 +8,17 @@ namespace oxygine
 {
     IVideoDriver* IVideoDriver::instance = 0;
 
+    void IVideoDriver::_debugAddPrimitives(PRIMITIVE_TYPE pt, int primitives)
+    {
+#if OXYGINE_TRACE_VIDEO_STATS
+        _stats.elements[pt] += primitives;
+        _stats.batches += 1;
+#endif
+
+    }
+
+    IVideoDriver::Stats IVideoDriver::_stats;
+
     spNativeTexture VideoDriverNull::createTexture()
     {
         //return new NativeTextureNull;
@@ -19,12 +30,6 @@ namespace oxygine
 
     }
 
-    void VideoDriverNull::getStats(Stats& s) const
-    {
-        s.batches = 0;
-        s.triangles = 0;
-    }
-
     void VideoDriverNull::getViewport(Rect& r) const
     {
 

+ 15 - 12
oxygine/src/core/VideoDriver.h

@@ -18,13 +18,6 @@ namespace oxygine
     class IVideoDriver
     {
     public:
-        class Stats
-        {
-        public:
-            Stats(): batches(0), triangles(0) {}
-            int batches;
-            int triangles;
-        };
 
         enum PRIMITIVE_TYPE
         {
@@ -35,8 +28,21 @@ namespace oxygine
             PT_TRIANGLES,
             PT_TRIANGLE_STRIP,
             PT_TRIANGLE_FAN,
+            PT_COUNT,
         };
 
+
+        class Stats
+        {
+        public:
+            Stats() : batches(0), duration(0), start(0) { memset(elements, 0, sizeof(elements)); }
+            int batches;
+            int elements[PT_COUNT];
+            timeMS start;
+            timeMS duration;
+        };
+        static Stats _stats;
+
         enum BLEND_TYPE
         {
             BT_ZERO,
@@ -79,7 +85,7 @@ namespace oxygine
         virtual void draw(PRIMITIVE_TYPE pt, const VertexDeclaration* decl, const void* verticesData, unsigned int verticesDataSize) = 0;
         virtual void draw(PRIMITIVE_TYPE pt, const VertexDeclaration* decl, const void* verticesData, unsigned int verticesDataSize, const unsigned short* indicesData, unsigned int numIndices) = 0;
 
-        virtual void            getStats(Stats& s) const = 0;
+
         virtual void            getViewport(Rect& r) const = 0;
         virtual bool            getScissorRect(Rect&) const = 0;
         virtual spNativeTexture getRenderTarget() const = 0;
@@ -102,9 +108,8 @@ namespace oxygine
         virtual void setUniform(const char* id, float v) = 0;
         virtual void setUniformInt(const char* id, int v) = 0;
 
-        virtual void setDebugStats(bool enable) = 0;
 
-        virtual void swapped() = 0;
+        void _debugAddPrimitives(PRIMITIVE_TYPE pt, int num);
     };
 
     class VideoDriverNull: public IVideoDriver
@@ -116,7 +121,6 @@ namespace oxygine
         void clear(const Color& color) {}
         void begin(const Rect& viewport, const Color* clearColor);
         bool isReady() const {return true;}
-        void getStats(Stats& s) const;
         void getViewport(Rect& r) const;
         bool getScissorRect(Rect&) const;
         ShaderProgram*  getShaderProgram() const { return 0; }
@@ -143,7 +147,6 @@ namespace oxygine
         void setTexture(int sampler, spNativeTexture);
         void setState(STATE, unsigned int value) {}
         void setBlendFunc(BLEND_TYPE src, BLEND_TYPE dest) {}
-        void swapped() {}
         void setDebugStats(bool enable) {}
 
         void reset() {}

+ 1 - 26
oxygine/src/core/gl/VideoDriverGL.cpp

@@ -6,8 +6,7 @@
 
 namespace oxygine
 {
-    VideoDriverGL::VideoDriverGL(): _batches(0), _triangles(0),
-        _traceStats(true)
+    VideoDriverGL::VideoDriverGL()
     {
         _rt = new NativeTextureGLES;
         GLint fbo = 0;
@@ -15,12 +14,6 @@ namespace oxygine
         _rt->_fbo = fbo;
     }
 
-    void    VideoDriverGL::getStats(Stats& s) const
-    {
-        s.batches = _batches;
-        s.triangles = _triangles;
-    }
-
     unsigned int VideoDriverGL::getPT(IVideoDriver::PRIMITIVE_TYPE pt)
     {
         switch (pt)
@@ -73,24 +66,6 @@ namespace oxygine
         return GL_ONE;
     }
 
-    void VideoDriverGL::_debugAddPrimitives(IVideoDriver::PRIMITIVE_TYPE pt, int num)
-    {
-        if (!_traceStats)
-            return;
-
-        switch (pt)
-        {
-            case PT_TRIANGLE_STRIP:
-                _triangles += num - 2;
-                break;
-            case PT_TRIANGLES:
-                _triangles += num / 3;
-                break;
-        }
-
-        _batches++;
-    }
-
     bool VideoDriverGL::getScissorRect(Rect& r) const
     {
         GLboolean scrTest = glIsEnabled(GL_SCISSOR_TEST);

+ 0 - 7
oxygine/src/core/gl/VideoDriverGL.h

@@ -16,7 +16,6 @@ namespace oxygine
         VideoDriverGL();
 
 
-        void    getStats(Stats& s) const;
         void    getViewport(Rect& r) const;
         bool    getScissorRect(Rect&) const;
         spNativeTexture getRenderTarget() const;
@@ -28,20 +27,14 @@ namespace oxygine
         void setBlendFunc(BLEND_TYPE src, BLEND_TYPE dest);
         void setState(STATE, unsigned int value);
 
-        void swapped() {_triangles = 0; _batches = 0;}
 
     protected:
         unsigned int getPT(IVideoDriver::PRIMITIVE_TYPE pt);
         unsigned int getBT(IVideoDriver::BLEND_TYPE pt);
-        void _debugAddPrimitives(IVideoDriver::PRIMITIVE_TYPE pt, int num);
-        void setDebugStats(bool enable) {_traceStats = enable;}
 
         void _begin(const Rect& viewport, const Color* clearColor);
         spNativeTextureGLES _rt;
 
         mutable VertexDeclarations<VertexDeclarationGL> _vdeclarations;
-        bool _traceStats;
-        int _batches;
-        int _triangles;
     };
 }

+ 3 - 5
oxygine/src/core/gl/VideoDriverGLES20.cpp

@@ -21,6 +21,7 @@
 
 namespace oxygine
 {
+
     VideoDriverGLES20::VideoDriverGLES20(): _programID(0), _p(0)
     {
     }
@@ -126,9 +127,7 @@ namespace oxygine
         }
 
 
-#if OXYGINE_TRACE_VIDEO_STATS
-        _debugAddPrimitives(pt, (int)(GLsizei)primitives);
-#endif
+        _debugAddPrimitives(pt, primitives);
 
         CHECKGL();
     }
@@ -157,9 +156,8 @@ namespace oxygine
         }
 
 
-#if OXYGINE_TRACE_VIDEO_STATS
         _debugAddPrimitives(pt, numIndices);
-#endif
+
         CHECKGL();
     }
 

+ 6 - 6
oxygine/src/core/oxygine.cpp

@@ -81,8 +81,6 @@ extern "C"
 
 namespace oxygine
 {
-    IVideoDriver::Stats _videoStats;
-
     static ThreadDispatcher _threadMessages;
     static ThreadDispatcher _uiMessages;
     Mutex mutexAlloc;
@@ -544,6 +542,7 @@ namespace oxygine
             bool ready = STDRenderer::isReady();
             if (ready)
             {
+                IVideoDriver::_stats.start = getTimeMS();
                 updatePortProcessItems();
             }
 
@@ -574,12 +573,9 @@ namespace oxygine
                 SDL_GL_SwapWindow(wnd);
             }
 #endif
-
-            IVideoDriver::instance->getStats(_videoStats);
-            IVideoDriver::instance->swapped();
-
             CHECKGL();
 
+            IVideoDriver::_stats.duration = getTimeMS() - IVideoDriver::_stats.start;
             //sleep(1000/50);
         }
 
@@ -721,6 +717,10 @@ namespace oxygine
 
         bool update()
         {
+            timeMS duration = IVideoDriver::_stats.duration;
+            IVideoDriver::_stats = IVideoDriver::Stats();
+            IVideoDriver::_stats.duration = duration;
+
             ThreadDispatcher::peekMessage msg;
             while (_threadMessages.peek(msg, true)) {}