0030153: Visualization, TKOpenGl - AIS_ColoredShape::SynchronizeAspects() doesn't...
[occt.git] / src / OpenGl / OpenGl_FrameStats.hxx
1 // Copyright (c) 2017 OPEN CASCADE SAS
2 //
3 // This file is part of Open CASCADE Technology software library.
4 //
5 // This library is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU Lesser General Public License version 2.1 as published
7 // by the Free Software Foundation, with special exception defined in the file
8 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
9 // distribution for complete text of the license and disclaimer of any warranty.
10 //
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
13
14 #ifndef _OpenGl_FrameStats_HeaderFile
15 #define _OpenGl_FrameStats_HeaderFile
16
17 #include <Graphic3d_RenderingParams.hxx>
18 #include <NCollection_IndexedMap.hxx>
19 #include <OSD_Timer.hxx>
20 #include <Standard_Type.hxx>
21 #include <Standard_Transient.hxx>
22
23 class OpenGl_Workspace;
24 class OpenGl_Structure;
25 typedef NCollection_IndexedMap<const OpenGl_Structure*> OpenGl_IndexedMapOfStructure;
26
27 //! Class storing the frame statistics.
28 class OpenGl_FrameStats : public Standard_Transient
29 {
30   DEFINE_STANDARD_RTTIEXT(OpenGl_FrameStats, Standard_Transient)
31 public:
32
33   //! Stats counter.
34   enum Counter
35   {
36     Counter_NbLayers = 0,           //!< number of ZLayers
37     Counter_NbLayersNotCulled,      //!< number of not culled ZLayers
38     Counter_NbStructs,              //!< number of defined OpenGl_Structure
39     Counter_NbStructsNotCulled,     //!< number of not culled OpenGl_Structure
40     Counter_NbGroupsNotCulled,      //!< number of not culled OpenGl_Group
41     Counter_NbElemsNotCulled,       //!< number of not culled OpenGl_Element
42     Counter_NbElemsFillNotCulled,   //!< number of not culled OpenGl_PrimitiveArray drawing triangles
43     Counter_NbElemsLineNotCulled,   //!< number of not culled OpenGl_PrimitiveArray drawing lines
44     Counter_NbElemsPointNotCulled,  //!< number of not culled OpenGl_PrimitiveArray drawing points
45     Counter_NbElemsTextNotCulled,   //!< number of not culled OpenGl_Text
46     Counter_NbTrianglesNotCulled,   //!< number of not culled (as structure) triangles
47     Counter_NbPointsNotCulled,      //!< number of not culled (as structure) points
48     Counter_EstimatedBytesGeom,     //!< estimated GPU memory used for geometry
49     Counter_EstimatedBytesFbos,     //!< estimated GPU memory used for FBOs
50     Counter_EstimatedBytesTextures, //!< estimated GPU memory used for textures
51   };
52   enum { Counter_NB = Counter_EstimatedBytesTextures + 1 };
53
54 public:
55
56   //! Default constructor.
57   Standard_EXPORT OpenGl_FrameStats();
58
59   //! Destructor.
60   Standard_EXPORT virtual ~OpenGl_FrameStats();
61
62   //! Returns interval in seconds for updating meters across several frames; 1 second by default.
63   Standard_Real UpdateInterval() const { return myUpdateInterval; }
64
65   //! Sets interval in seconds for updating values.
66   void SetUpdateInterval (Standard_Real theInterval) { myUpdateInterval = theInterval; }
67
68   //! Prefer longer lines over more greater of lines.
69   Standard_Boolean IsLongLineFormat() const { return myIsLongLineFormat; }
70
71   //! Set if format should prefer longer lines over greater number of lines.
72   void SetLongLineFormat (Standard_Boolean theValue) { myIsLongLineFormat = theValue; }
73
74   //! Frame redraw started.
75   Standard_EXPORT virtual void FrameStart (const Handle(OpenGl_Workspace)& theWorkspace = Handle(OpenGl_Workspace)());
76
77   //! Frame redraw finished.
78   Standard_EXPORT virtual void FrameEnd (const Handle(OpenGl_Workspace)& theWorkspace = Handle(OpenGl_Workspace)());
79
80 public:
81
82   //! Returns formatted string.
83   Standard_EXPORT virtual TCollection_AsciiString FormatStats (Graphic3d_RenderingParams::PerfCounters theFlags) const;
84
85   //! Returns duration of the last frame in seconds.
86   Standard_Real FrameDuration() const { return myFrameDuration; }
87
88   //! Returns FPS (frames per seconds, elapsed time).
89   //! This number indicates an actual frame rate averaged for several frames within UpdateInterval() duration,
90   //! basing on a real elapsed time between updates.
91   Standard_Real FrameRate() const { return myFps; }
92
93   //! Returns CPU FPS (frames per seconds, CPU time).
94   //! This number indicates a PREDICTED frame rate,
95   //! basing on CPU elapsed time between updates and NOT real elapsed time (which might include periods of CPU inactivity).
96   //! Number is expected to be greater then actual frame rate returned by FrameRate().
97   //! Values significantly greater actual frame rate indicate that rendering is limited by GPU performance (CPU is stalled in-between),
98   //! while values around actual frame rate indicate rendering being limited by CPU performance (GPU is stalled in-between).
99   Standard_Real FrameRateCpu() const { return myFpsCpu; }
100
101   //! Returns value of specified counter, cached between stats updates.
102   //! Should NOT be called between ::FrameStart() and ::FrameEnd() calls.
103   Standard_Size CounterValue (OpenGl_FrameStats::Counter theCounter) const { return myCounters[theCounter]; }
104
105   //! Returns TRUE if some Layers have been culled.
106   Standard_Boolean HasCulledLayers() const { return myCounters[Counter_NbLayersNotCulled] != myCounters[Counter_NbLayers]; }
107
108   //! Returns TRUE if some structures have been culled.
109   Standard_Boolean HasCulledStructs() const { return myCounters[Counter_NbStructsNotCulled] != myCounters[Counter_NbStructs]; }
110
111 public:
112
113   //! Returns TRUE if this stats are equal to another.
114   virtual Standard_Boolean IsEqual (const Handle(OpenGl_FrameStats)& theOther) const
115   {
116     // check just a couple of major counters
117     return Abs (myFps    - theOther->myFps)    <= 0.001
118         && Abs (myFpsCpu - theOther->myFpsCpu) <= 0.001
119         && myCounters[Counter_NbLayers] == theOther->myCounters[Counter_NbLayers]
120         && myCounters[Counter_NbLayersNotCulled] == theOther->myCounters[Counter_NbLayersNotCulled]
121         && myCounters[Counter_NbStructs] == theOther->myCounters[Counter_NbStructs]
122         && myCounters[Counter_NbStructsNotCulled] == theOther->myCounters[Counter_NbStructsNotCulled];
123   }
124
125   //! Copy stats values from another instance
126   virtual void CopyFrom (const Handle(OpenGl_FrameStats)& theOther)
127   {
128     myFps    = theOther->myFps;
129     myFpsCpu = theOther->myFpsCpu;
130     memcpy (myCounters, theOther->myCounters, sizeof(myCounters));
131   }
132
133   //! Returns value of specified counter for modification, should be called between ::FrameStart() and ::FrameEnd() calls.
134   Standard_Size& ChangeCounter (OpenGl_FrameStats::Counter theCounter) { return myCountersTmp[theCounter]; }
135
136 protected:
137
138   //! Updates counters for structures.
139   Standard_EXPORT virtual void updateStructures (Standard_Integer theViewId,
140                                                  const OpenGl_IndexedMapOfStructure& theStructures,
141                                                  Standard_Boolean theToCountElems,
142                                                  Standard_Boolean theToCountTris,
143                                                  Standard_Boolean theToCountMem);
144
145 protected:
146
147   OSD_Timer        myFpsTimer;                //!< timer for FPS measurements
148   Standard_Real    myFrameStartTime;          //!< time at the beginning of frame redraw
149   Standard_Real    myFrameDuration;           //!< frame duration
150   Standard_Real    myFps;                     //!< FPS     meter (frames per seconds, elapsed time)
151   Standard_Real    myFpsCpu;                  //!< CPU FPS meter (frames per seconds, CPU time)
152   Standard_Real    myUpdateInterval;          //!< interval to update meters
153   Standard_Size    myFpsFrameCount;           //!< FPS counter (within short measurement time slice)
154   Standard_Size    myCounters   [Counter_NB]; //!< counter values cached between updates
155   Standard_Size    myCountersTmp[Counter_NB]; //!< counter values filled during
156   Standard_Boolean myIsLongLineFormat;        //!< prefer longer lines over greater number of lines
157
158 };
159
160 DEFINE_STANDARD_HANDLE(OpenGl_FrameStats, Standard_Transient)
161
162 #endif // _OpenGl_FrameStats_HeaderFile