1 // Copyright (c) 2017 OPEN CASCADE SAS
3 // This file is part of Open CASCADE Technology software library.
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.
11 // Alternatively, this file may be used under the terms of Open CASCADE
12 // commercial license or contractual agreement.
14 #include <OpenGl_FrameStats.hxx>
16 #include <OpenGl_GlCore20.hxx>
17 #include <OpenGl_View.hxx>
18 #include <OpenGl_Workspace.hxx>
20 IMPLEMENT_STANDARD_RTTIEXT(OpenGl_FrameStats, Graphic3d_FrameStats)
24 //! Return estimated data size.
25 static Standard_Size estimatedDataSize (const Handle(OpenGl_Resource)& theRes)
27 return !theRes.IsNull() ? theRes->EstimatedDataSize() : 0;
31 // =======================================================================
32 // function : OpenGl_FrameStats
34 // =======================================================================
35 OpenGl_FrameStats::OpenGl_FrameStats()
40 // =======================================================================
41 // function : ~OpenGl_FrameStats
43 // =======================================================================
44 OpenGl_FrameStats::~OpenGl_FrameStats()
49 // =======================================================================
50 // function : IsFrameUpdated
52 // =======================================================================
53 bool OpenGl_FrameStats::IsFrameUpdated (Handle(OpenGl_FrameStats)& thePrev) const
55 const Graphic3d_FrameStatsData& aFrame = LastDataFrame();
58 thePrev = new OpenGl_FrameStats();
60 // check just a couple of major counters
61 else if (myLastFrameIndex == thePrev->myLastFrameIndex
62 && Abs (aFrame.FrameRate() - thePrev->myCountersTmp.FrameRate()) <= 0.001
63 && Abs (aFrame.FrameRateCpu() - thePrev->myCountersTmp.FrameRateCpu()) <= 0.001
64 && Abs (aFrame.ImmediateFrameRate() - thePrev->myCountersTmp.ImmediateFrameRate()) <= 0.001
65 && Abs (aFrame.ImmediateFrameRateCpu() - thePrev->myCountersTmp.ImmediateFrameRateCpu()) <= 0.001
66 && aFrame[Graphic3d_FrameStatsCounter_NbLayers] == thePrev->myCountersTmp[Graphic3d_FrameStatsCounter_NbLayers]
67 && aFrame[Graphic3d_FrameStatsCounter_NbLayersNotCulled] == thePrev->myCountersTmp[Graphic3d_FrameStatsCounter_NbLayersNotCulled]
68 && aFrame[Graphic3d_FrameStatsCounter_NbStructs] == thePrev->myCountersTmp[Graphic3d_FrameStatsCounter_NbStructs]
69 && aFrame[Graphic3d_FrameStatsCounter_NbStructsNotCulled] == thePrev->myCountersTmp[Graphic3d_FrameStatsCounter_NbStructsNotCulled])
74 thePrev->myLastFrameIndex = myLastFrameIndex;
75 thePrev->myCountersTmp = aFrame;
79 // =======================================================================
80 // function : updateStatistics
82 // =======================================================================
83 void OpenGl_FrameStats::updateStatistics (const Handle(Graphic3d_CView)& theView,
84 bool theIsImmediateOnly)
86 const OpenGl_View* aView = dynamic_cast<const OpenGl_View*> (theView.get());
89 myCounters.SetValue (myLastFrameIndex, myCountersTmp);
90 myCountersTmp.Reset();
94 const Graphic3d_RenderingParams::PerfCounters aBits = theView->RenderingParams().CollectedStats;
95 const Standard_Boolean toCountMem = (aBits & Graphic3d_RenderingParams::PerfCounters_EstimMem) != 0;
96 const Standard_Boolean toCountTris = (aBits & Graphic3d_RenderingParams::PerfCounters_Triangles) != 0
97 || (aBits & Graphic3d_RenderingParams::PerfCounters_Lines) != 0
98 || (aBits & Graphic3d_RenderingParams::PerfCounters_Points) != 0;
99 const Standard_Boolean toCountElems = (aBits & Graphic3d_RenderingParams::PerfCounters_GroupArrays) != 0 || toCountTris || toCountMem;
100 const Standard_Boolean toCountGroups = (aBits & Graphic3d_RenderingParams::PerfCounters_Groups) != 0 || toCountElems;
101 const Standard_Boolean toCountStructs = (aBits & Graphic3d_RenderingParams::PerfCounters_Structures) != 0
102 || (aBits & Graphic3d_RenderingParams::PerfCounters_Layers) != 0 || toCountGroups;
104 myCountersTmp[Graphic3d_FrameStatsCounter_NbLayers] = aView->LayerList().Layers().Size();
106 || (aBits & Graphic3d_RenderingParams::PerfCounters_Layers) != 0)
108 const Standard_Integer aViewId = aView->Identification();
109 for (NCollection_List<Handle(Graphic3d_Layer)>::Iterator aLayerIter (aView->LayerList().Layers()); aLayerIter.More(); aLayerIter.Next())
111 const Handle(OpenGl_Layer)& aLayer = aLayerIter.Value();
112 myCountersTmp[Graphic3d_FrameStatsCounter_NbStructs] += aLayer->NbStructures();
113 if (theIsImmediateOnly && !aLayer->LayerSettings().IsImmediate())
118 if (!aLayer->IsCulled())
120 ++myCountersTmp[Graphic3d_FrameStatsCounter_NbLayersNotCulled];
122 myCountersTmp[Graphic3d_FrameStatsCounter_NbStructsNotCulled] += aLayer->NbStructuresNotCulled();
125 updateStructures (aViewId, aLayer->CullableStructuresBVH().Structures(), toCountElems, toCountTris, toCountMem);
126 updateStructures (aViewId, aLayer->CullableTrsfPersStructuresBVH().Structures(), toCountElems, toCountTris, toCountMem);
127 updateStructures (aViewId, aLayer->NonCullableStructures(), toCountElems, toCountTris, toCountMem);
133 for (OpenGl_Context::OpenGl_ResourcesMap::Iterator aResIter (aView->GlWindow()->GetGlContext()->SharedResources());
134 aResIter.More(); aResIter.Next())
136 myCountersTmp[Graphic3d_FrameStatsCounter_EstimatedBytesTextures] += aResIter.Value()->EstimatedDataSize();
140 Standard_Size& aMemFbos = myCountersTmp[Graphic3d_FrameStatsCounter_EstimatedBytesFbos];
142 aMemFbos += estimatedDataSize (aView->myMainSceneFbos[0]);
143 aMemFbos += estimatedDataSize (aView->myMainSceneFbos[1]);
144 aMemFbos += estimatedDataSize (aView->myImmediateSceneFbos[0]);
145 aMemFbos += estimatedDataSize (aView->myImmediateSceneFbos[1]);
147 aMemFbos += estimatedDataSize (aView->myMainSceneFbosOit[0]);
148 aMemFbos += estimatedDataSize (aView->myMainSceneFbosOit[1]);
149 aMemFbos += estimatedDataSize (aView->myImmediateSceneFbosOit[0]);
150 aMemFbos += estimatedDataSize (aView->myImmediateSceneFbosOit[1]);
152 aMemFbos += estimatedDataSize (aView->myFBO);
154 aMemFbos += estimatedDataSize (aView->myOpenGlFBO);
155 aMemFbos += estimatedDataSize (aView->myOpenGlFBO2);
156 aMemFbos += estimatedDataSize (aView->myRaytraceFBO1[0]);
157 aMemFbos += estimatedDataSize (aView->myRaytraceFBO1[1]);
158 aMemFbos += estimatedDataSize (aView->myRaytraceFBO2[0]);
159 aMemFbos += estimatedDataSize (aView->myRaytraceFBO2[1]);
161 aMemFbos += estimatedDataSize (aView->myRaytraceOutputTexture[0]);
162 aMemFbos += estimatedDataSize (aView->myRaytraceOutputTexture[1]);
163 aMemFbos += estimatedDataSize (aView->myRaytraceVisualErrorTexture[0]);
164 aMemFbos += estimatedDataSize (aView->myRaytraceVisualErrorTexture[1]);
165 aMemFbos += estimatedDataSize (aView->myRaytraceTileOffsetsTexture[0]);
166 aMemFbos += estimatedDataSize (aView->myRaytraceTileOffsetsTexture[1]);
167 aMemFbos += estimatedDataSize (aView->myRaytraceTileSamplesTexture[0]);
168 aMemFbos += estimatedDataSize (aView->myRaytraceTileSamplesTexture[1]);
171 // Ray Tracing geometry
172 Standard_Size& aMemGeom = myCountersTmp[Graphic3d_FrameStatsCounter_EstimatedBytesGeom];
173 aMemGeom += estimatedDataSize (aView->mySceneNodeInfoTexture);
174 aMemGeom += estimatedDataSize (aView->mySceneMinPointTexture);
175 aMemGeom += estimatedDataSize (aView->mySceneMaxPointTexture);
176 aMemGeom += estimatedDataSize (aView->mySceneTransformTexture);
177 aMemGeom += estimatedDataSize (aView->myGeometryVertexTexture);
178 aMemGeom += estimatedDataSize (aView->myGeometryNormalTexture);
179 aMemGeom += estimatedDataSize (aView->myGeometryTexCrdTexture);
180 aMemGeom += estimatedDataSize (aView->myGeometryTriangTexture);
181 aMemGeom += estimatedDataSize (aView->myRaytraceMaterialTexture);
182 aMemGeom += estimatedDataSize (aView->myRaytraceLightSrcTexture);
187 // =======================================================================
188 // function : updateStructures
190 // =======================================================================
191 void OpenGl_FrameStats::updateStructures (Standard_Integer theViewId,
192 const NCollection_IndexedMap<const Graphic3d_CStructure*>& theStructures,
193 Standard_Boolean theToCountElems,
194 Standard_Boolean theToCountTris,
195 Standard_Boolean theToCountMem)
197 for (OpenGl_Structure::StructIterator aStructIter (theStructures); aStructIter.More(); aStructIter.Next())
199 const OpenGl_Structure* aStruct = aStructIter.Value();
200 const bool isStructHidden = aStruct->IsCulled()
201 || !aStruct->IsVisible (theViewId);
202 for (; aStruct != NULL; aStruct = aStruct->InstancedStructure())
208 for (OpenGl_Structure::GroupIterator aGroupIter (aStruct->Groups()); aGroupIter.More(); aGroupIter.Next())
210 const OpenGl_Group* aGroup = aGroupIter.Value();
211 for (const OpenGl_ElementNode* aNodeIter = aGroup->FirstNode(); aNodeIter != NULL; aNodeIter = aNodeIter->next)
213 aNodeIter->elem->UpdateMemStats (myCountersTmp);
220 myCountersTmp[Graphic3d_FrameStatsCounter_NbGroupsNotCulled] += aStruct->Groups().Size();
221 if (!theToCountElems)
226 for (OpenGl_Structure::GroupIterator aGroupIter (aStruct->Groups()); aGroupIter.More(); aGroupIter.Next())
228 const OpenGl_Group* aGroup = aGroupIter.Value();
229 for (const OpenGl_ElementNode* aNodeIter = aGroup->FirstNode(); aNodeIter != NULL; aNodeIter = aNodeIter->next)
233 aNodeIter->elem->UpdateMemStats (myCountersTmp);
235 aNodeIter->elem->UpdateDrawStats (myCountersTmp, theToCountTris);