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_DepthPeeling.hxx>
19 #include <OpenGl_ShadowMap.hxx>
20 #include <OpenGl_Workspace.hxx>
22 IMPLEMENT_STANDARD_RTTIEXT(OpenGl_FrameStats, Graphic3d_FrameStats)
26 //! Return estimated data size.
27 static Standard_Size estimatedDataSize (const Handle(OpenGl_Resource)& theRes)
29 return !theRes.IsNull() ? theRes->EstimatedDataSize() : 0;
33 // =======================================================================
34 // function : OpenGl_FrameStats
36 // =======================================================================
37 OpenGl_FrameStats::OpenGl_FrameStats()
42 // =======================================================================
43 // function : ~OpenGl_FrameStats
45 // =======================================================================
46 OpenGl_FrameStats::~OpenGl_FrameStats()
51 // =======================================================================
52 // function : IsFrameUpdated
54 // =======================================================================
55 bool OpenGl_FrameStats::IsFrameUpdated (Handle(OpenGl_FrameStats)& thePrev) const
57 const Graphic3d_FrameStatsData& aFrame = LastDataFrame();
60 thePrev = new OpenGl_FrameStats();
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])
76 thePrev->myLastFrameIndex = myLastFrameIndex;
77 thePrev->myCountersTmp = aFrame;
81 // =======================================================================
82 // function : updateStatistics
84 // =======================================================================
85 void OpenGl_FrameStats::updateStatistics (const Handle(Graphic3d_CView)& theView,
86 bool theIsImmediateOnly)
88 const OpenGl_View* aView = dynamic_cast<const OpenGl_View*> (theView.get());
91 myCounters.SetValue (myLastFrameIndex, myCountersTmp);
92 myCountersTmp.Reset();
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;
106 myCountersTmp[Graphic3d_FrameStatsCounter_NbLayers] = aView->LayerList().Layers().Size();
108 || (aBits & Graphic3d_RenderingParams::PerfCounters_Layers) != 0)
110 const Standard_Integer aViewId = aView->Identification();
111 for (NCollection_List<Handle(Graphic3d_Layer)>::Iterator aLayerIter (aView->LayerList().Layers()); aLayerIter.More(); aLayerIter.Next())
113 const Handle(OpenGl_Layer)& aLayer = aLayerIter.Value();
114 myCountersTmp[Graphic3d_FrameStatsCounter_NbStructs] += aLayer->NbStructures();
115 if (theIsImmediateOnly && !aLayer->LayerSettings().IsImmediate())
120 if (!aLayer->IsCulled())
122 ++myCountersTmp[Graphic3d_FrameStatsCounter_NbLayersNotCulled];
124 myCountersTmp[Graphic3d_FrameStatsCounter_NbStructsNotCulled] += aLayer->NbStructuresNotCulled();
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);
135 for (OpenGl_Context::OpenGl_ResourcesMap::Iterator aResIter (aView->GlWindow()->GetGlContext()->SharedResources());
136 aResIter.More(); aResIter.Next())
138 myCountersTmp[Graphic3d_FrameStatsCounter_EstimatedBytesTextures] += aResIter.Value()->EstimatedDataSize();
142 Standard_Size& aMemFbos = myCountersTmp[Graphic3d_FrameStatsCounter_EstimatedBytesFbos];
144 aMemFbos += estimatedDataSize (aView->myMainSceneFbos[0]);
145 aMemFbos += estimatedDataSize (aView->myMainSceneFbos[1]);
146 aMemFbos += estimatedDataSize (aView->myImmediateSceneFbos[0]);
147 aMemFbos += estimatedDataSize (aView->myImmediateSceneFbos[1]);
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);
155 aMemFbos += aView->myShadowMaps->EstimatedDataSize();
157 aMemFbos += estimatedDataSize (aView->myFBO);
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]);
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]);
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);
192 // =======================================================================
193 // function : updateStructures
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)
202 for (OpenGl_Structure::StructIterator aStructIter (theStructures); aStructIter.More(); aStructIter.Next())
204 const OpenGl_Structure* aStruct = aStructIter.Value();
205 const bool isStructHidden = aStruct->IsCulled()
206 || !aStruct->IsVisible (theViewId);
207 for (; aStruct != NULL; aStruct = aStruct->InstancedStructure())
213 for (OpenGl_Structure::GroupIterator aGroupIter (aStruct->Groups()); aGroupIter.More(); aGroupIter.Next())
215 const OpenGl_Group* aGroup = aGroupIter.Value();
216 for (const OpenGl_ElementNode* aNodeIter = aGroup->FirstNode(); aNodeIter != NULL; aNodeIter = aNodeIter->next)
218 aNodeIter->elem->UpdateMemStats (myCountersTmp);
225 myCountersTmp[Graphic3d_FrameStatsCounter_NbGroupsNotCulled] += aStruct->Groups().Size();
226 if (!theToCountElems)
231 for (OpenGl_Structure::GroupIterator aGroupIter (aStruct->Groups()); aGroupIter.More(); aGroupIter.Next())
233 const OpenGl_Group* aGroup = aGroupIter.Value();
234 for (const OpenGl_ElementNode* aNodeIter = aGroup->FirstNode(); aNodeIter != NULL; aNodeIter = aNodeIter->next)
238 aNodeIter->elem->UpdateMemStats (myCountersTmp);
240 aNodeIter->elem->UpdateDrawStats (myCountersTmp, theToCountTris);