0033661: Data Exchange, Step Import - Tessellated GDTs are not imported
[occt.git] / src / OpenGl / OpenGl_FrameStats.cxx
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 #include <OpenGl_FrameStats.hxx>
15
16 #include <OpenGl_GlCore20.hxx>
17 #include <OpenGl_View.hxx>
18 #include <OpenGl_DepthPeeling.hxx>
19 #include <OpenGl_ShadowMap.hxx>
20 #include <OpenGl_Workspace.hxx>
21
22 IMPLEMENT_STANDARD_RTTIEXT(OpenGl_FrameStats, Graphic3d_FrameStats)
23
24 namespace
25 {
26   //! Return estimated data size.
27   static Standard_Size estimatedDataSize (const Handle(OpenGl_Resource)& theRes)
28   {
29     return !theRes.IsNull() ? theRes->EstimatedDataSize() : 0;
30   }
31 }
32
33 // =======================================================================
34 // function : OpenGl_FrameStats
35 // purpose  :
36 // =======================================================================
37 OpenGl_FrameStats::OpenGl_FrameStats()
38 {
39   //
40 }
41
42 // =======================================================================
43 // function : ~OpenGl_FrameStats
44 // purpose  :
45 // =======================================================================
46 OpenGl_FrameStats::~OpenGl_FrameStats()
47 {
48   //
49 }
50
51 // =======================================================================
52 // function : IsFrameUpdated
53 // purpose  :
54 // =======================================================================
55 bool OpenGl_FrameStats::IsFrameUpdated (Handle(OpenGl_FrameStats)& thePrev) const
56 {
57   const Graphic3d_FrameStatsData& aFrame = LastDataFrame();
58   if (thePrev.IsNull())
59   {
60     thePrev = new OpenGl_FrameStats();
61   }
62   // check just a couple of major counters
63   else if (myLastFrameIndex == thePrev->myLastFrameIndex
64         && Abs (aFrame.FrameRate()    - thePrev->myCountersTmp.FrameRate())    <= 0.001
65         && Abs (aFrame.FrameRateCpu() - thePrev->myCountersTmp.FrameRateCpu()) <= 0.001
66         && Abs (aFrame.ImmediateFrameRate()    - thePrev->myCountersTmp.ImmediateFrameRate())    <= 0.001
67         && Abs (aFrame.ImmediateFrameRateCpu() - thePrev->myCountersTmp.ImmediateFrameRateCpu()) <= 0.001
68         && aFrame[Graphic3d_FrameStatsCounter_NbLayers]           == thePrev->myCountersTmp[Graphic3d_FrameStatsCounter_NbLayers]
69         && aFrame[Graphic3d_FrameStatsCounter_NbLayersNotCulled]  == thePrev->myCountersTmp[Graphic3d_FrameStatsCounter_NbLayersNotCulled]
70         && aFrame[Graphic3d_FrameStatsCounter_NbStructs]          == thePrev->myCountersTmp[Graphic3d_FrameStatsCounter_NbStructs]
71         && aFrame[Graphic3d_FrameStatsCounter_NbStructsNotCulled] == thePrev->myCountersTmp[Graphic3d_FrameStatsCounter_NbStructsNotCulled])
72   {
73     return false;
74   }
75
76   thePrev->myLastFrameIndex = myLastFrameIndex;
77   thePrev->myCountersTmp = aFrame;
78   return true;
79 }
80
81 // =======================================================================
82 // function : updateStatistics
83 // purpose  :
84 // =======================================================================
85 void OpenGl_FrameStats::updateStatistics (const Handle(Graphic3d_CView)& theView,
86                                           bool theIsImmediateOnly)
87 {
88   const OpenGl_View* aView = dynamic_cast<const OpenGl_View*> (theView.get());
89   if (aView == NULL)
90   {
91     myCounters.SetValue (myLastFrameIndex, myCountersTmp);
92     myCountersTmp.Reset();
93     return;
94   }
95
96   const Graphic3d_RenderingParams::PerfCounters aBits = theView->RenderingParams().CollectedStats;
97   const Standard_Boolean toCountMem     = (aBits & Graphic3d_RenderingParams::PerfCounters_EstimMem)  != 0;
98   const Standard_Boolean toCountTris    = (aBits & Graphic3d_RenderingParams::PerfCounters_Triangles) != 0
99                                        || (aBits & Graphic3d_RenderingParams::PerfCounters_Lines)     != 0
100                                        || (aBits & Graphic3d_RenderingParams::PerfCounters_Points)    != 0;
101   const Standard_Boolean toCountElems   = (aBits & Graphic3d_RenderingParams::PerfCounters_GroupArrays) != 0 || toCountTris || toCountMem;
102   const Standard_Boolean toCountGroups  = (aBits & Graphic3d_RenderingParams::PerfCounters_Groups)      != 0 || toCountElems;
103   const Standard_Boolean toCountStructs = (aBits & Graphic3d_RenderingParams::PerfCounters_Structures)  != 0
104                                        || (aBits & Graphic3d_RenderingParams::PerfCounters_Layers)      != 0 || toCountGroups;
105
106   myCountersTmp[Graphic3d_FrameStatsCounter_NbLayers] = aView->LayerList().Layers().Size();
107   if (toCountStructs
108    || (aBits & Graphic3d_RenderingParams::PerfCounters_Layers)    != 0)
109   {
110     const Standard_Integer aViewId = aView->Identification();
111     for (NCollection_List<Handle(Graphic3d_Layer)>::Iterator aLayerIter (aView->LayerList().Layers()); aLayerIter.More(); aLayerIter.Next())
112     {
113       const Handle(OpenGl_Layer)& aLayer = aLayerIter.Value();
114       myCountersTmp[Graphic3d_FrameStatsCounter_NbStructs] += aLayer->NbStructures();
115       if (theIsImmediateOnly && !aLayer->LayerSettings().IsImmediate())
116       {
117         continue;
118       }
119
120       if (!aLayer->IsCulled())
121       {
122         ++myCountersTmp[Graphic3d_FrameStatsCounter_NbLayersNotCulled];
123       }
124       myCountersTmp[Graphic3d_FrameStatsCounter_NbStructsNotCulled] += aLayer->NbStructuresNotCulled();
125       if (toCountGroups)
126       {
127         updateStructures (aViewId, aLayer->CullableStructuresBVH().Structures(), toCountElems, toCountTris, toCountMem);
128         updateStructures (aViewId, aLayer->CullableTrsfPersStructuresBVH().Structures(), toCountElems, toCountTris, toCountMem);
129         updateStructures (aViewId, aLayer->NonCullableStructures(), toCountElems, toCountTris, toCountMem);
130       }
131     }
132   }
133   if (toCountMem)
134   {
135     for (OpenGl_Context::OpenGl_ResourcesMap::Iterator aResIter (aView->GlWindow()->GetGlContext()->SharedResources());
136          aResIter.More(); aResIter.Next())
137     {
138       myCountersTmp[Graphic3d_FrameStatsCounter_EstimatedBytesTextures] += aResIter.Value()->EstimatedDataSize();
139     }
140
141     {
142       Standard_Size& aMemFbos = myCountersTmp[Graphic3d_FrameStatsCounter_EstimatedBytesFbos];
143       // main FBOs
144       aMemFbos += estimatedDataSize (aView->myMainSceneFbos[0]);
145       aMemFbos += estimatedDataSize (aView->myMainSceneFbos[1]);
146       aMemFbos += estimatedDataSize (aView->myImmediateSceneFbos[0]);
147       aMemFbos += estimatedDataSize (aView->myImmediateSceneFbos[1]);
148       // OIT FBOs
149       aMemFbos += estimatedDataSize (aView->myMainSceneFbosOit[0]);
150       aMemFbos += estimatedDataSize (aView->myMainSceneFbosOit[1]);
151       aMemFbos += estimatedDataSize (aView->myImmediateSceneFbosOit[0]);
152       aMemFbos += estimatedDataSize (aView->myImmediateSceneFbosOit[1]);
153       aMemFbos += estimatedDataSize (aView->myDepthPeelingFbos);
154       // shadowmap FBOs
155       aMemFbos += aView->myShadowMaps->EstimatedDataSize();
156       // dump FBO
157       aMemFbos += estimatedDataSize (aView->myFBO);
158       // RayTracing FBO
159       aMemFbos += estimatedDataSize (aView->myOpenGlFBO);
160       aMemFbos += estimatedDataSize (aView->myOpenGlFBO2);
161       aMemFbos += estimatedDataSize (aView->myRaytraceFBO1[0]);
162       aMemFbos += estimatedDataSize (aView->myRaytraceFBO1[1]);
163       aMemFbos += estimatedDataSize (aView->myRaytraceFBO2[0]);
164       aMemFbos += estimatedDataSize (aView->myRaytraceFBO2[1]);
165       // also RayTracing
166       aMemFbos += estimatedDataSize (aView->myRaytraceOutputTexture[0]);
167       aMemFbos += estimatedDataSize (aView->myRaytraceOutputTexture[1]);
168       aMemFbos += estimatedDataSize (aView->myRaytraceVisualErrorTexture[0]);
169       aMemFbos += estimatedDataSize (aView->myRaytraceVisualErrorTexture[1]);
170       aMemFbos += estimatedDataSize (aView->myRaytraceTileOffsetsTexture[0]);
171       aMemFbos += estimatedDataSize (aView->myRaytraceTileOffsetsTexture[1]);
172       aMemFbos += estimatedDataSize (aView->myRaytraceTileSamplesTexture[0]);
173       aMemFbos += estimatedDataSize (aView->myRaytraceTileSamplesTexture[1]);
174     }
175     {
176       // Ray Tracing geometry
177       Standard_Size& aMemGeom = myCountersTmp[Graphic3d_FrameStatsCounter_EstimatedBytesGeom];
178       aMemGeom += estimatedDataSize (aView->mySceneNodeInfoTexture);
179       aMemGeom += estimatedDataSize (aView->mySceneMinPointTexture);
180       aMemGeom += estimatedDataSize (aView->mySceneMaxPointTexture);
181       aMemGeom += estimatedDataSize (aView->mySceneTransformTexture);
182       aMemGeom += estimatedDataSize (aView->myGeometryVertexTexture);
183       aMemGeom += estimatedDataSize (aView->myGeometryNormalTexture);
184       aMemGeom += estimatedDataSize (aView->myGeometryTexCrdTexture);
185       aMemGeom += estimatedDataSize (aView->myGeometryTriangTexture);
186       aMemGeom += estimatedDataSize (aView->myRaytraceMaterialTexture);
187       aMemGeom += estimatedDataSize (aView->myRaytraceLightSrcTexture);
188     }
189   }
190 }
191
192 // =======================================================================
193 // function : updateStructures
194 // purpose  :
195 // =======================================================================
196 void OpenGl_FrameStats::updateStructures (Standard_Integer theViewId,
197                                           const NCollection_IndexedMap<const Graphic3d_CStructure*>& theStructures,
198                                           Standard_Boolean theToCountElems,
199                                           Standard_Boolean theToCountTris,
200                                           Standard_Boolean theToCountMem)
201 {
202   for (OpenGl_Structure::StructIterator aStructIter (theStructures); aStructIter.More(); aStructIter.Next())
203   {
204     const OpenGl_Structure* aStruct = aStructIter.Value();
205     const bool isStructHidden = aStruct->IsCulled()
206                             || !aStruct->IsVisible (theViewId);
207     for (; aStruct != NULL; aStruct = aStruct->InstancedStructure())
208     {
209       if (isStructHidden)
210       {
211         if (theToCountMem)
212         {
213           for (OpenGl_Structure::GroupIterator aGroupIter (aStruct->Groups()); aGroupIter.More(); aGroupIter.Next())
214           {
215             const OpenGl_Group* aGroup = aGroupIter.Value();
216             for (const OpenGl_ElementNode* aNodeIter = aGroup->FirstNode(); aNodeIter != NULL; aNodeIter = aNodeIter->next)
217             {
218               aNodeIter->elem->UpdateMemStats (myCountersTmp);
219             }
220           }
221         }
222         continue;
223       }
224
225       myCountersTmp[Graphic3d_FrameStatsCounter_NbGroupsNotCulled] += aStruct->Groups().Size();
226       if (!theToCountElems)
227       {
228         continue;
229       }
230
231       for (OpenGl_Structure::GroupIterator aGroupIter (aStruct->Groups()); aGroupIter.More(); aGroupIter.Next())
232       {
233         const OpenGl_Group* aGroup = aGroupIter.Value();
234         for (const OpenGl_ElementNode* aNodeIter = aGroup->FirstNode(); aNodeIter != NULL; aNodeIter = aNodeIter->next)
235         {
236           if (theToCountMem)
237           {
238             aNodeIter->elem->UpdateMemStats (myCountersTmp);
239           }
240           aNodeIter->elem->UpdateDrawStats (myCountersTmp, theToCountTris);
241         }
242       }
243     }
244   }
245 }